-
Notifications
You must be signed in to change notification settings - Fork 0
/
DE10_NANO_SoC_GHRD.sv
2956 lines (2586 loc) · 108 KB
/
DE10_NANO_SoC_GHRD.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=======================================================
// This code *was, at some time, * generated by Terasic System Builder
//=======================================================
module DE10_NANO_SoC_GHRD(
//////////// CLOCK //////////
input logic FPGA_CLK1_50,
input logic FPGA_CLK2_50,
input logic FPGA_CLK3_50,
//////////// HPS //////////
output logic [14: 0] HPS_DDR3_ADDR,
output logic [ 2: 0] HPS_DDR3_BA,
output logic HPS_DDR3_CAS_N,
output logic HPS_DDR3_CK_N,
output logic HPS_DDR3_CK_P,
output logic HPS_DDR3_CKE,
output logic HPS_DDR3_CS_N,
output logic [ 3: 0] HPS_DDR3_DM,
inout logic [31: 0] HPS_DDR3_DQ,
inout logic [ 3: 0] HPS_DDR3_DQS_N,
inout logic [ 3: 0] HPS_DDR3_DQS_P,
output logic HPS_DDR3_ODT,
output logic HPS_DDR3_RAS_N,
output logic HPS_DDR3_RESET_N,
input logic HPS_DDR3_RZQ,
output logic HPS_DDR3_WE_N,
//////////// GPIO //////////
inout logic [35:0] GPIO_D,
//////////// LED //////////
output logic [ 7: 0] LED,
//////////// SWITCHES //////////
input logic [ 3: 0] SW,
//////////// SWITCHES //////////
input logic [ 1: 0] KEY
);
//=======================================================
// reg/wire declarations
//=======================================================
logic hps_fpga_reset_n;
logic fpga_clk_50;
//=======================================================
// RAM variables
//=======================================================
logic [21:0] address = 16'd0;
logic read = 1'b0;
logic write = 1'b0;
logic acknowledge;
logic [31:0] read_data = 32'd0;
logic [31:0] write_data = 32'd0;
logic [3:0] byte_enable = 4'b1111;
//=======================================================
// UART variables
//=======================================================
logic [7:0] uart_wdata;
logic uart_write;
logic uart_in_error;
logic uart_in_ready;
logic [7:0] uart_rdata;
logic uart_out_ready;
logic uart_out_error;
logic uart_out_valid;
logic [31:0] GPIOS;
// connection of internal logics
assign fpga_clk_50 = FPGA_CLK1_50;
assign GPIO_D[34:3] = GPIOS;
//=======================================================
// Structural coding
//=======================================================
soc_system u_u0(
//Clock&Resetread_data[6:0]
.clk_clk(FPGA_CLK1_50), // clk.clk
.reset_reset_n(hps_fpga_reset_n), // reset.reset_n
//HPS ddr3
.memory_mem_a(HPS_DDR3_ADDR), // memory.mem_a
.memory_mem_ba(HPS_DDR3_BA), // .mem_ba
.memory_mem_ck(HPS_DDR3_CK_P), // .mem_ck
.memory_mem_ck_n(HPS_DDR3_CK_N), // .mem_ck_n
.memory_mem_cke(HPS_DDR3_CKE), // .mem_cke
.memory_mem_cs_n(HPS_DDR3_CS_N), // .mem_cs_n
.memory_mem_ras_n(HPS_DDR3_RAS_N), // .mem_ras_n
.memory_mem_cas_n(HPS_DDR3_CAS_N), // .mem_cas_n
.memory_mem_we_n(HPS_DDR3_WE_N), // .mem_we_n
.memory_mem_reset_n(HPS_DDR3_RESET_N), // .mem_reset_n
.memory_mem_dq(HPS_DDR3_DQ), // .mem_dq
.memory_mem_dqs(HPS_DDR3_DQS_P), // .mem_dqs
.memory_mem_dqs_n(HPS_DDR3_DQS_N), // .mem_dqs_n
.memory_mem_odt(HPS_DDR3_ODT), // .mem_odt
.memory_mem_dm(HPS_DDR3_DM), // .mem_dm
.memory_oct_rzqin(HPS_DDR3_RZQ), // .oct_rzqin
.hps_0_h2f_reset_reset_n(hps_fpga_reset_n), // hps_0_h2f_reset.reset_n
.sdram_address(address),
.sdram_byte_enable(byte_enable), // .byte_enable
.sdram_read(read), // .read
.sdram_write(write), // .write
.sdram_write_data(write_data), // .write_data
.sdram_acknowledge(acknowledge), // .acknowledge
.sdram_read_data(read_data),
.uart_RXD(GPIO_D[0]), // uart_0_external_connection.rxd
.uart_TXD(GPIO_D[1]), // .txd
.uart_in_data(uart_wdata),
.uart_in_error(uart_in_error),
.uart_in_valid(uart_write),
.uart_in_ready(uart_in_ready),
.uart_out_ready(uart_out_ready),
.uart_out_data(uart_rdata),
.uart_out_error(uart_out_error),
.uart_out_valid(uart_out_valid)
);
//=======================================================
// Memory read/write finite automata
//=======================================================
enum int unsigned {
MEM_STATE_INIT = 1,
MEM_STATE_READ_PENDING = 2,
MEM_STATE_WRITE_PENDING = 3
} cur_mem_state, next_mem_state;
logic read_req = 0;
logic write_req = 0;
// State switch logic
always_comb begin
next_mem_state = cur_mem_state;
case (cur_mem_state)
MEM_STATE_INIT: begin
if (read_req) next_mem_state = MEM_STATE_READ_PENDING;
else if (write_req) next_mem_state = MEM_STATE_WRITE_PENDING;
end
MEM_STATE_READ_PENDING: begin
if (acknowledge) next_mem_state = MEM_STATE_INIT;
end
MEM_STATE_WRITE_PENDING: begin
if (acknowledge) next_mem_state = MEM_STATE_INIT;
end
default: begin
next_mem_state = MEM_STATE_INIT;
end
endcase
end
always_comb begin
read = 0;
write = 0;
case (cur_mem_state)
MEM_STATE_READ_PENDING: begin
read = 1;
end
MEM_STATE_WRITE_PENDING: begin
write = 1;
end
default: begin
end
endcase
end
//=======================================================
// CPU state enum and regs / wires
//=======================================================
enum int unsigned {
CPU_STATE_INSTR_FETCH = 1,
CPU_STATE_INSTR_FETCH_1 = 2,
CPU_STATE_INSTR_DECODE = 3,
CPU_STATE_INSTR_DECODE_1 = 4,
CPU_STATE_INSTR_IMM_FETCH = 5,
CPU_STATE_INSTR_OPERAND_FETCH = 6,
CPU_STATE_INSTR_EXEC = 7,
CPU_STATE_INSTR_WRITEBACK = 8
} cur_cpu_state;
//=======================================================
// CPU single-step button setup.
//=======================================================
// Cpu single-step clock setup
// If a SW[3] is set to 1,
// then the cpu cycles by pressing KEY[1]
logic [1:0] debounced_keys = 2'd0;
debounce #(2, "LOW", 50000, 16) debouncer(
.clk(fpga_clk_50),
.reset_n(hps_fpga_reset_n),
.data_in(KEY),
.data_out(debounced_keys)
);
reg is_halted = 0;
reg cycle_done = 1;
reg prev_pressed = 0;
//=======================================================
// CPU instruction decoder / registers
//=======================================================
parameter MEM_STACK_BASE = 16'h4000;
reg [23:0] cur_instruction = 24'd0;
reg [15:0] PC = 16'd0;
reg [15:0] IMM_ADDR = 16'd0;
reg [15:0] FR = 16'd0;
reg [15:0] SP = MEM_STACK_BASE;
reg [15:0] ACC = 0;
reg [15:0] IR1 = 0;
reg [15:0] IR2 = 0;
// CPU variables
reg [31:0] data = 32'd0;
reg [15:0] tmp_address = 16'd0;
reg [15:0] tmp_word = 16'd0;
reg [15:0] cur_imm = 16'd0;
reg [15:0] snd_imm = 16'd0;
assign LED[7: 0] = PC[7:0];
//=======================================================
// UART I/O variables and logic
//=======================================================
logic uart_write_req = 0;
logic uart_read_req = 0;
logic [7:0] uart_data = 0;
always_ff @(posedge fpga_clk_50 or negedge hps_fpga_reset_n) begin
if (~hps_fpga_reset_n) begin
cur_mem_state <= MEM_STATE_INIT;
cur_cpu_state <= CPU_STATE_INSTR_FETCH;
cycle_done <= 1;
read_req <= '0;
write_req <= '0;
prev_pressed <= debounced_keys[1];
cur_instruction <= 6'd0;
PC <= '0;
FR <= 16'd0;
SP <= MEM_STACK_BASE;
uart_out_ready <= '1;
uart_write <= '0;
end else begin
if (uart_out_valid) begin
uart_data <= uart_rdata;
uart_read_req <= 0;
uart_out_ready <= 0;
uart_write <= 0;
end
if (uart_write) begin
uart_write_req <= 0;
uart_out_ready <= 0;
uart_write <= 0;
end
if (uart_write_req & uart_in_ready & ~uart_out_valid & ~uart_write) begin
uart_out_ready <= 0;
uart_write <= 1;
end else if (uart_read_req & ~uart_out_valid) begin
uart_out_ready <= 1;
uart_write <= 0;
end
// Lotsa logic, sorry
cur_mem_state <= next_mem_state;
if (~debounced_keys[0]) is_halted <= 0;
if (~debounced_keys[0] & SW[1]) begin
SP <= MEM_STACK_BASE;
PC <= '0;
end
prev_pressed <= debounced_keys[1];
case (cur_mem_state)
MEM_STATE_INIT: begin
end
MEM_STATE_READ_PENDING: begin
if (acknowledge) data <= read_data;
read_req <= 0;
write_req <= 0;
end
MEM_STATE_WRITE_PENDING: begin
read_req <= 0;
write_req <= 0;
end
default: begin
read_req <= 0;
write_req <= 0;
end
endcase
//=======================================================
// The instruction decoder itself. Finally )
//=======================================================
if (
~SW[1] & // If SW[1] is on, we wait for button 0 to set PC to 0
cur_mem_state == MEM_STATE_INIT & ~is_halted & // Only do cpu stuff when memory is not read/written to
~read_req & ~write_req & // Same as line one
~uart_write_req & ~uart_read_req &// Can't do UART either (
(~SW[3] | (prev_pressed & ~debounced_keys[1] & cycle_done) | ~cycle_done) // Button checker
) begin
cycle_done <= cur_cpu_state == CPU_STATE_INSTR_WRITEBACK;
case (cur_cpu_state)
CPU_STATE_INSTR_FETCH: begin
cur_cpu_state <= CPU_STATE_INSTR_FETCH_1;
end
CPU_STATE_INSTR_FETCH_1: begin
cur_cpu_state <= CPU_STATE_INSTR_DECODE;
end
CPU_STATE_INSTR_DECODE: begin
cur_cpu_state <= CPU_STATE_INSTR_DECODE_1;
end
CPU_STATE_INSTR_DECODE_1: begin
cur_cpu_state <= CPU_STATE_INSTR_IMM_FETCH;
end
CPU_STATE_INSTR_IMM_FETCH: begin
cur_cpu_state <= CPU_STATE_INSTR_OPERAND_FETCH;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
cur_cpu_state <= CPU_STATE_INSTR_EXEC;
end
CPU_STATE_INSTR_EXEC: begin
cur_cpu_state <= CPU_STATE_INSTR_WRITEBACK;
end
CPU_STATE_INSTR_WRITEBACK: begin
cur_cpu_state <= CPU_STATE_INSTR_FETCH;
end
default: begin
cur_cpu_state <= CPU_STATE_INSTR_FETCH;
end
endcase
if (cur_cpu_state == CPU_STATE_INSTR_FETCH) begin
address <= {PC[15:2], 2'b00};
byte_enable <= 4'b1111;
read_req <= 1;
end else if (cur_cpu_state == CPU_STATE_INSTR_FETCH_1) begin
case (PC[1:0])
2'b00: cur_instruction <= data[23:0];
2'b01: cur_instruction <= data[31:8];
2'b10: begin
cur_instruction[15:0] <= data[31:16];
address <= {PC[15:2], 2'b00} + 16'd4;
read_req <= 1;
byte_enable <= 4'b1111;
end
2'b11: begin
cur_instruction[7:0] <= data[31:24];
address <= {PC[15:2], 2'b00} + 16'd4;
read_req <= 1;
byte_enable <= 4'b1111;
end
default: begin
end
endcase
end else if (cur_cpu_state == CPU_STATE_INSTR_DECODE) begin
case (PC[1:0])
2'b10: cur_instruction[23:16] <= data[7:0];
2'b11: cur_instruction[23:8] <= data[15:0];
default: begin
end
endcase
end else if (cur_cpu_state == CPU_STATE_INSTR_DECODE_1) begin
if (cur_instruction[7]) PC <= PC + 16'd3;
else PC <= PC + 16'd1;
end else begin
case (cur_instruction[7:0])
// NOP
default: begin
if (cur_cpu_state == CPU_STATE_INSTR_WRITEBACK) PC <= PC + 1;
end
// HALT
8'b00000000: begin
if (cur_cpu_state == CPU_STATE_INSTR_WRITEBACK) is_halted <= 1;
end
// LOAD
8'b00000001: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
read_req <= 1;
byte_enable <= 4'b1111;
address <= {ACC[15:2], 2'b00};
tmp_address <= ACC;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
case (ACC[1:0])
2'b00: ACC <= data[15:0];
2'b01: ACC <= data[23:8];
2'b10: ACC <= data[31:16];
2'b11: begin
ACC[7:0] <= data[31:24];
read_req <= 1;
byte_enable <= 4'b1111;
address <= {tmp_address[15:2], 2'b00} + 16'd4;
end
default: begin
end
endcase
end
CPU_STATE_INSTR_EXEC: begin
if (tmp_address[1:0] == 2'b11) ACC[15:8] <= data[7:0];
end
CPU_STATE_INSTR_WRITEBACK: begin
ACC <= {ACC[7:0], ACC[15:8]};
end
default: begin
end
endcase
end
// LOAD $IMM
8'b10010000: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) ACC <= cur_instruction[23:8];
else if (cur_cpu_state == CPU_STATE_INSTR_WRITEBACK) ACC <= {ACC[7:0], ACC[15:8]};
end
// LOADF
8'b00000010: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) ACC <= FR;
end
// LOAD1
8'b01000001: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
read_req <= 1;
byte_enable <= 4'b1111;
address <= {IR1[15:2], 2'b00};
tmp_address <= IR1;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
case (IR1[1:0])
2'b00: ACC <= data[15:0];
2'b01: ACC <= data[23:8];
2'b10: ACC <= data[31:16];
2'b11: begin
ACC[7:0] <= data[31:24];
read_req <= 1;
byte_enable <= 4'b1111;
address <= {tmp_address[15:2], 2'b00} + 16'd4;
end
default: begin
end
endcase
end
CPU_STATE_INSTR_EXEC: begin
if (tmp_address[1:0] == 2'b11) ACC[15:8] <= data[7:0];
end
CPU_STATE_INSTR_WRITEBACK: begin
ACC <= {ACC[7:0], ACC[15:8]};
end
default: begin
end
endcase
end
// LOAD2
8'b01000010: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
read_req <= 1;
byte_enable <= 4'b1111;
address <= {IR2[15:2], 2'b00};
tmp_address <= IR2;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
case (IR2[1:0])
2'b00: ACC <= data[15:0];
2'b01: ACC <= data[23:8];
2'b10: ACC <= data[31:16];
2'b11: begin
ACC[7:0] <= data[31:24];
read_req <= 1;
byte_enable <= 4'b1111;
address <= {tmp_address[15:2], 2'b00} + 16'd4;
end
default: begin
end
endcase
end
CPU_STATE_INSTR_EXEC: begin
if (tmp_address[1:0] == 2'b11) ACC[15:8] <= data[7:0];
end
CPU_STATE_INSTR_WRITEBACK: begin
ACC <= {ACC[7:0], ACC[15:8]};
end
default: begin
end
endcase
end
// LOADI1
8'b00011111: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) ACC <= IR1;
end
// LOADI2
8'b00100000: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) ACC <= IR2;
end
// STORE1
8'b00000011: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
ACC <= {ACC[7:0], ACC[15:8]};
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
write_req <= 1;
address <= {IR1[15:2], 2'b00};
case (IR1[1:0])
2'b00: begin
byte_enable <= 4'b0011;
write_data <= {2{ACC[7:0], ACC[15:8]}};
end
2'b10: begin
byte_enable <= 4'b1100;
write_data <= {2{ACC[7:0], ACC[15:8]}};
end
2'b01: begin
byte_enable <= 4'b0010;
write_data <= {4{ACC[7:0]}};
end
2'b11: begin
byte_enable <= 4'b1000;
write_data <= {4{ACC[7:0]}};
end
default: begin
end
endcase
end
CPU_STATE_INSTR_EXEC: begin
case (IR1[1:0])
2'b01: begin
write_req <= 1;
address <= {IR1[15:2], 2'b00};
byte_enable <= 4'b0100;
write_data <= {4{ACC[15:8]}};
end
2'b11: begin
write_req <= 1;
address <= {IR1[15:2], 2'b00} + 16'd4;
byte_enable <= 4'b0001;
write_data <= {4{ACC[15:8]}};
end
default: begin
end
endcase
end
default: begin
end
endcase
end
// STORE1 $IMM
8'b10010001: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
write_req <= 1;
address <= {IR1[15:2], 2'b00};
case (IR1[1:0])
2'b00: begin
byte_enable <= 4'b0011;
write_data <= {2{cur_instruction[23:8]}};
end
2'b10: begin
byte_enable <= 4'b1100;
write_data <= {2{cur_instruction[23:8]}};
end
2'b01: begin
byte_enable <= 4'b0010;
write_data <= {4{cur_instruction[15:8]}};
end
2'b11: begin
byte_enable <= 4'b1000;
write_data <= {4{cur_instruction[15:8]}};
end
default: begin
end
endcase
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
case (IR1[1:0])
2'b01: begin
write_req <= 1;
address <= {IR1[15:2], 2'b00};
byte_enable <= 4'b0100;
write_data <= {4{cur_instruction[23:16]}};
end
2'b11: begin
write_req <= 1;
address <= {IR1[15:2], 2'b00} + 16'd4;
byte_enable <= 4'b0001;
write_data <= {4{cur_instruction[23:16]}};
end
default: begin
end
endcase
end
default: begin
end
endcase
end
// STORE2
8'b00100011: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
ACC <= {ACC[7:0], ACC[15:8]};
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
write_req <= 1;
address <= {IR2[15:2], 2'b00};
case (IR2[1:0])
2'b00: begin
byte_enable <= 4'b0011;
write_data <= {2{ACC[7:0], ACC[15:8]}};
end
2'b10: begin
byte_enable <= 4'b1100;
write_data <= {2{ACC[7:0], ACC[15:8]}};
end
2'b01: begin
byte_enable <= 4'b0010;
write_data <= {4{ACC[7:0]}};
end
2'b11: begin
byte_enable <= 4'b1000;
write_data <= {4{ACC[7:0]}};
end
default: begin
end
endcase
end
CPU_STATE_INSTR_EXEC: begin
case (IR2[1:0])
2'b01: begin
write_req <= 1;
address <= {IR2[15:2], 2'b00};
byte_enable <= 4'b0100;
write_data <= {4{ACC[15:8]}};
end
2'b11: begin
write_req <= 1;
address <= {IR2[15:2], 2'b00} + 16'd4;
byte_enable <= 4'b0001;
write_data <= {4{ACC[15:8]}};
end
default: begin
end
endcase
end
default: begin
end
endcase
end
// STORE2 $IMM
8'b11110010: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
write_req <= 1;
address <= {IR2[15:2], 2'b00};
case (IR2[1:0])
2'b00: begin
byte_enable <= 4'b0011;
write_data <= {2{cur_instruction[23:8]}};
end
2'b10: begin
byte_enable <= 4'b1100;
write_data <= {2{cur_instruction[23:8]}};
end
2'b01: begin
byte_enable <= 4'b0010;
write_data <= {4{cur_instruction[15:8]}};
end
2'b11: begin
byte_enable <= 4'b1000;
write_data <= {4{cur_instruction[15:8]}};
end
default: begin
end
endcase
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
case (IR2[1:0])
2'b01: begin
write_req <= 1;
address <= {IR2[15:2], 2'b00};
byte_enable <= 4'b0100;
write_data <= {4{cur_instruction[23:16]}};
end
2'b11: begin
write_req <= 1;
address <= {IR2[15:2], 2'b00} + 16'd4;
byte_enable <= 4'b0001;
write_data <= {4{cur_instruction[23:16]}};
end
default: begin
end
endcase
end
default: begin
end
endcase
end
// STOREF
8'b00000100: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) FR <= ACC;
end
// STOREI1
8'b00100001: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) IR1 <= ACC;
end
// STOREI2
8'b00100010: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) IR2 <= ACC;
end
// TI12
8'b00111111: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) IR2 <= IR1;
end
// TI21
8'b01000000: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) IR1 <= IR2;
end
// MOV $IMM
8'b10000000: begin
if (cur_cpu_state == CPU_STATE_INSTR_EXEC) ACC <= {cur_instruction[15:8], cur_instruction[23:16]};
end
// PUSH
8'b00000101: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
SP <= SP - 16'd2;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
write_req <= 1;
write_data <= {2{ACC[7:0], ACC[15:8]}};
address <= {SP[15:2], 2'b00};
if (SP[1]) byte_enable <= 4'b1100;
else byte_enable <= 4'b0011;
end
default: begin
end
endcase
end
// POP
8'b00000111: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
SP <= SP + 16'd2;
address <= {SP[15:2], 2'b00};
read_req <= 1;
byte_enable <= 4'b1111;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
if (SP[1]) ACC <= {data[23:16], data[31:24]};
else ACC <= {data[7:0], data[15:8]};
end
default: begin
end
endcase
end
// PUSHF
8'b00000110: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
SP <= SP - 16'd2;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
write_req <= 1;
write_data <= {2{FR[7:0], FR[15:8]}};
address <= {SP[15:2], 2'b00};
if (SP[1]) byte_enable <= 4'b1100;
else byte_enable <= 4'b0011;
end
default: begin
end
endcase
end
// POPF
8'b00001000: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
SP <= SP + 16'd2;
address <= {SP[15:2], 2'b00};
read_req <= 1;
byte_enable <= 4'b1111;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
if (SP[1]) FR <= {data[23:16], data[31:24]};
else FR <= {data[7:0], data[15:8]};
end
default: begin
end
endcase
end
// PUSHI1
8'b00111101: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
SP <= SP - 16'd2;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
write_req <= 1;
write_data <= {2{IR1[7:0], IR1[15:8]}};
address <= {SP[15:2], 2'b00};
if (SP[1]) byte_enable <= 4'b1100;
else byte_enable <= 4'b0011;
end
default: begin
end
endcase
end
// POPI1
8'b00001001: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
SP <= SP + 16'd2;
address <= {SP[15:2], 2'b00};
read_req <= 1;
byte_enable <= 4'b1111;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
if (SP[1]) IR1 <= {data[23:16], data[31:24]};
else IR1 <= {data[7:0], data[15:8]};
end
default: begin
end
endcase
end
// PUSHI2
8'b00100100: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
SP <= SP - 16'd2;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
write_req <= 1;
write_data <= {2{IR2[7:0], IR2[15:8]}};
address <= {SP[15:2], 2'b00};
if (SP[1]) byte_enable <= 4'b1100;
else byte_enable <= 4'b0011;
end
default: begin
end
endcase
end
// POPI2
8'b00100101: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
SP <= SP + 16'd2;
address <= {SP[15:2], 2'b00};
read_req <= 1;
byte_enable <= 4'b1111;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
if (SP[1]) IR2 <= {data[23:16], data[31:24]};
else IR2 <= {data[7:0], data[15:8]};
end
default: begin
end
endcase
end
// ADD [$MEM]
8'b00001010: begin
case (cur_cpu_state)
CPU_STATE_INSTR_IMM_FETCH: begin
read_req <= 1;
byte_enable <= 4'b1111;
tmp_address <= {cur_instruction[15:8], cur_instruction[23:16]};
address <= {cur_instruction[15:8], cur_instruction[23:16], 2'b00};
PC <= PC + 16'd2;
end
CPU_STATE_INSTR_OPERAND_FETCH: begin
case (tmp_address[1:0])
2'b00: tmp_word <= data[15:0];