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
|
module grom_cpu(
input clk,
input reset,
output reg [11:0] addr,
input [7:0] data_in,
output reg [7:0] data_out,
output reg we,
output reg ioreq,
output reg hlt
);
reg[11:0] PC /* verilator public_flat */; // Program counter
reg[7:0] IR /* verilator public_flat */; // Instruction register
reg[7:0] VALUE /* verilator public_flat */; // Temp reg for storing 2nd operand
reg[3:0] CS /* verilator public_flat */; // Code segment regiser
reg[3:0] DS /* verilator public_flat */; // Data segment regiser
reg[11:0] SP /* verilator public_flat */; // Stack pointer regiser
reg[7:0] R[0:3] /* verilator public_flat */; // General purpose registers
reg[11:0] FUTURE_PC /* verilator public_flat */; // PC to jump to
localparam STATE_RESET /*verilator public_flat*/ = 5'b00000;
localparam STATE_FETCH_PREP /*verilator public_flat*/ = 5'b00001;
localparam STATE_FETCH_WAIT /*verilator public_flat*/ = 5'b00010;
localparam STATE_FETCH /*verilator public_flat*/ = 5'b00011;
localparam STATE_EXECUTE /*verilator public_flat*/ = 5'b00100;
localparam STATE_FETCH_VALUE_PREP /*verilator public_flat*/ = 5'b00101;
localparam STATE_FETCH_VALUE /*verilator public_flat*/ = 5'b00110;
localparam STATE_EXECUTE_DBL /*verilator public_flat*/ = 5'b00111;
localparam STATE_LOAD_VALUE /*verilator public_flat*/ = 5'b01000;
localparam STATE_LOAD_VALUE_WAIT /*verilator public_flat*/ = 5'b01001;
localparam STATE_ALU_RESULT_WAIT /*verilator public_flat*/ = 5'b01010;
localparam STATE_ALU_RESULT /*verilator public_flat*/ = 5'b01011;
localparam STATE_PUSH_PC_LOW /*verilator public_flat*/ = 5'b01100;
localparam STATE_JUMP /*verilator public_flat*/ = 5'b01101;
localparam STATE_RET_VALUE_WAIT /*verilator public_flat*/ = 5'b01110;
localparam STATE_RET_VALUE /*verilator public_flat*/ = 5'b01111;
localparam STATE_RET_VALUE_WAIT2 /*verilator public_flat*/ = 5'b10000;
localparam STATE_RET_VALUE2 /*verilator public_flat*/ = 5'b10001;
reg [4:0] state /* verilator public_flat */ = STATE_RESET;
reg [7:0] alu_a /* verilator public_flat */;
reg [7:0] alu_b /* verilator public_flat */;
reg [3:0] alu_op /* verilator public_flat */;
reg [1:0] RESULT_REG /* verilator public_flat */;
wire [7:0] alu_res /* verilator public_flat */;
wire alu_CF /* verilator public_flat */;
wire alu_ZF /* verilator public_flat */;
wire alu_SF /* verilator public_flat */;
reg jump;
alu alu(.clk(clk),.A(alu_a),.B(alu_b),.operation(alu_op),.result(alu_res),.CF(alu_CF),.ZF(alu_ZF),.SF(alu_SF));
always @(posedge clk)
begin
if (reset)
begin
state <= STATE_RESET;
hlt <= 0;
end
else
begin
case (state)
STATE_RESET :
begin
PC <= 12'h000;
state <= STATE_FETCH_PREP;
CS <= 4'h0;
DS <= 4'h0;
R[0] <= 8'h00;
R[1] <= 8'h00;
R[2] <= 8'h00;
R[3] <= 8'h00;
SP <= 12'hfff;
end
STATE_FETCH_PREP :
begin
addr <= PC;
we <= 0;
ioreq <= 0;
state <= STATE_FETCH_WAIT;
end
STATE_FETCH_WAIT :
begin
// Sync with memory due to CLK
state <= (hlt) ? STATE_FETCH_PREP : STATE_FETCH;
end
STATE_FETCH :
begin
IR <= data_in;
PC <= PC + 1;
state <= STATE_EXECUTE;
end
STATE_EXECUTE :
begin
`ifdef DISASSEMBLY
$display(" PC %h R0 %h R1 %h R2 %h R3 %h CS %h DS %h SP %h ALU [%d %d %d]", PC, R[0], R[1], R[2], R[3], CS, DS, SP, alu_CF,alu_SF,alu_ZF);
`endif
if (IR[7])
begin
addr <= PC;
state <= STATE_FETCH_VALUE_PREP;
PC <= PC + 1;
end
else
begin
case(IR[6:4])
3'b000 :
begin
`ifdef DISASSEMBLY
$display("MOV R%d,R%d",IR[3:2],IR[1:0]);
`endif
R[IR[3:2]] <= R[IR[1:0]];
state <= STATE_FETCH_PREP;
end
3'b001 :
begin
alu_a <= R[0]; // first input R0
alu_b <= R[IR[1:0]];
RESULT_REG <= 0; // result in R0
alu_op <= { 2'b00, IR[3:2] };
state <= STATE_ALU_RESULT_WAIT;
`ifdef DISASSEMBLY
case(IR[3:2])
2'b00 : begin
$display("ADD R%d",IR[1:0]);
end
2'b01 : begin
$display("SUB R%d",IR[1:0]);
end
2'b10 : begin
$display("ADC R%d",IR[1:0]);
end
2'b11 : begin
$display("SBC R%d",IR[1:0]);
end
endcase
`endif
end
3'b010 :
begin
alu_a <= R[0]; // first input R0
alu_b <= R[IR[1:0]];
RESULT_REG <= 0; // result in R0
alu_op <= { 2'b01, IR[3:2] };
state <= STATE_ALU_RESULT_WAIT;
`ifdef DISASSEMBLY
case(IR[3:2])
2'b00 : begin
$display("AND R%d",IR[1:0]);
end
2'b01 : begin
$display("OR R%d",IR[1:0]);
end
2'b10 : begin
$display("NOT R%d",IR[1:0]);
end
2'b11 : begin
$display("XOR R%d",IR[1:0]);
end
endcase
`endif
end
3'b011 :
begin
RESULT_REG <= IR[1:0]; // result in REG
// CMP and TEST are not storing result
state <= IR[3] ? STATE_FETCH_PREP : STATE_ALU_RESULT_WAIT;
// CMP and TEST are having first input R0, for INC and DEC is REG
alu_a <= IR[3] ? R[0] : R[IR[1:0]];
// CMP and TEST are having second input REG, for INC and DEC is 1
alu_b <= IR[3] ? R[IR[1:0]] : 8'b00000001;
case(IR[3:2])
2'b00 : begin
`ifdef DISASSEMBLY
$display("INC R%d",IR[1:0]);
`endif
alu_op <= 4'b0000; // ALU_OP_ADD
end
2'b01 : begin
`ifdef DISASSEMBLY
$display("DEC R%d",IR[1:0]);
`endif
alu_op <= 4'b0001; // ALU_OP_SUB
end
2'b10 : begin
`ifdef DISASSEMBLY
$display("CMP R%d",IR[1:0]);
`endif
alu_op <= 4'b0001; // ALU_OP_SUB
end
2'b11 : begin
`ifdef DISASSEMBLY
$display("TST R%d",IR[1:0]);
`endif
alu_op <= 4'b0100; // ALU_OP_AND
end
endcase
end
3'b100 :
begin
if (IR[3]==0)
begin
alu_a <= R[0]; // first input R0
// no 2nd input
RESULT_REG <= 0; // result in R0
alu_op <= { 1'b1, IR[2:0] };
`ifdef DISASSEMBLY
case(IR[2:0])
3'b000 : begin
$display("SHL");
end
3'b001 : begin
$display("SHR");
end
3'b010 : begin
$display("SAL");
end
3'b011 : begin
$display("SAR");
end
3'b100 : begin
$display("ROL");
end
3'b101 : begin
$display("ROR");
end
3'b110 : begin
$display("RCL");
end
3'b111 : begin
$display("RCR");
end
endcase
`endif
state <= STATE_ALU_RESULT_WAIT;
end
else
begin
if (IR[2]==0)
begin
`ifdef DISASSEMBLY
$display("PUSH R%d",IR[1:0]);
`endif
addr <= SP;
we <= 1;
ioreq <= 0;
data_out <= R[IR[1:0]];
SP <= SP - 1;
state <= STATE_FETCH_PREP;
end
else
begin
`ifdef DISASSEMBLY
$display("POP R%d",IR[1:0]);
`endif
addr <= SP + 1;
we <= 0;
ioreq <= 0;
RESULT_REG <= IR[1:0];
SP <= SP + 1;
state <= STATE_LOAD_VALUE_WAIT;
end
end
end
3'b101 :
begin
`ifdef DISASSEMBLY
$display("LOAD R%d,[R%d]", IR[3:2], IR[1:0]);
`endif
addr <= { DS, R[IR[1:0]] };
we <= 0;
ioreq <= 0;
RESULT_REG <= IR[3:2];
state <= STATE_LOAD_VALUE_WAIT;
end
3'b110 :
begin
`ifdef DISASSEMBLY
$display("STORE [R%d],R%d", IR[3:2], IR[1:0]);
`endif
addr <= { DS, R[IR[3:2]] };
we <= 1;
ioreq <= 0;
data_out <= R[IR[1:0]];
state <= STATE_FETCH_PREP;
end
3'b111 :
begin
// Special instuctions
case(IR[3:2])
2'b00 : begin
CS <= R[IR[1:0]][3:0];
state <= STATE_FETCH_PREP;
`ifdef DISASSEMBLY
$display("MOV CS,R%d",IR[1:0]);
`endif
end
2'b01 : begin
DS <= R[IR[1:0]][3:0];
state <= STATE_FETCH_PREP;
`ifdef DISASSEMBLY
$display("MOV DS,R%d",IR[1:0]);
`endif
end
2'b10 : begin
case(IR[1:0])
2'b00 : begin
`ifdef DISASSEMBLY
$display("PUSH CS");
`endif
addr <= SP;
we <= 1;
ioreq <= 0;
data_out <= { 4'b0000, CS};
SP <= SP - 1;
state <= STATE_FETCH_PREP;
end
2'b01 : begin
`ifdef DISASSEMBLY
$display("PUSH DS");
`endif
addr <= SP;
we <= 1;
ioreq <= 0;
data_out <= { 4'b0000, DS};
SP <= SP - 1;
state <= STATE_FETCH_PREP;
end
2'b10 : begin
`ifdef DISASSEMBLY
$display("Unused opcode");
`endif
end
2'b11 : begin
`ifdef DISASSEMBLY
$display("Unused opcode");
`endif
end
endcase
state <= STATE_FETCH_PREP;
end
2'b11 : begin
case(IR[1:0])
2'b00 : begin
`ifdef DISASSEMBLY
$display("Unused opcode");
`endif
state <= STATE_FETCH_PREP;
end
2'b01 : begin
`ifdef DISASSEMBLY
$display("Unused opcode");
`endif
state <= STATE_FETCH_PREP;
end
2'b10 : begin
`ifdef DISASSEMBLY
$display("RET");
`endif
addr <= SP + 1;
we <= 0;
ioreq <= 0;
SP <= SP + 1;
state <= STATE_RET_VALUE_WAIT;
end
2'b11 : begin
hlt <= 1;
`ifdef DISASSEMBLY
$display("HALT");
`endif
state <= STATE_FETCH_PREP;
end
endcase
end
endcase
end
endcase
end
end
STATE_FETCH_VALUE_PREP :
begin
// Sync with memory due to CLK
state <= STATE_FETCH_VALUE;
end
STATE_FETCH_VALUE :
begin
VALUE <= data_in;
state <= STATE_EXECUTE_DBL;
end
STATE_EXECUTE_DBL :
begin
case(IR[6:4])
3'b000 :
begin
if (IR[3]==0)
begin
case(IR[2:0])
3'b000 :
begin
`ifdef DISASSEMBLY
$display("JMP %h ",{ CS, VALUE[7:0] });
`endif
jump = 1;
end
3'b001 :
begin
`ifdef DISASSEMBLY
$display("JC %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_CF==1);
end
3'b010 :
begin
`ifdef DISASSEMBLY
$display("JNC %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_CF==0);
end
3'b011 :
begin
`ifdef DISASSEMBLY
$display("JM %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_SF==1);
end
3'b100 :
begin
`ifdef DISASSEMBLY
$display("JP %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_SF==0);
end
3'b101 :
begin
`ifdef DISASSEMBLY
$display("JZ %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_ZF==1);
end
3'b110 :
begin
`ifdef DISASSEMBLY
$display("JNZ %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_ZF==0);
end
3'b111 :
begin
`ifdef DISASSEMBLY
$display("Unused opcode %h",IR);
`endif
jump = 0;
end
endcase
if (jump)
begin
PC <= { CS, VALUE[7:0] };
addr <= { CS, VALUE[7:0] };
we <= 0;
ioreq <= 0;
end
state <= STATE_FETCH_PREP;
end
else
begin
case(IR[2:0])
3'b000 :
begin
`ifdef DISASSEMBLY
$display("JR %h ", PC + {VALUE[7],VALUE[7],VALUE[7],VALUE[7],VALUE[7:0]} );
`endif
jump = 1;
end
3'b001 :
begin
`ifdef DISASSEMBLY
$display("JRC %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_CF==1);
end
3'b010 :
begin
`ifdef DISASSEMBLY
$display("JRNC %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_CF==0);
end
3'b011 :
begin
`ifdef DISASSEMBLY
$display("JRM %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_SF==1);
end
3'b100 :
begin
`ifdef DISASSEMBLY
$display("JRP %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_SF==0);
end
3'b101 :
begin
`ifdef DISASSEMBLY
$display("JRZ %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_ZF==1);
end
3'b110 :
begin
`ifdef DISASSEMBLY
$display("JRNZ %h ",{CS, VALUE[7:0] });
`endif
jump = (alu_ZF==0);
end
3'b111 :
begin
`ifdef DISASSEMBLY
$display("Unused opcode %h",IR);
`endif
jump = 0;
end
endcase
if (jump)
begin
PC <= PC + {VALUE[7],VALUE[7],VALUE[7],VALUE[7],VALUE[7:0]};
addr <= PC + {VALUE[7],VALUE[7],VALUE[7],VALUE[7],VALUE[7:0]};
we <= 0;
ioreq <= 0;
end
state <= STATE_FETCH_PREP;
end
end
3'b001 :
begin
`ifdef DISASSEMBLY
$display("JUMP %h ",{ IR[3:0], VALUE[7:0] });
`endif
PC <= { IR[3:0], VALUE[7:0] };
addr <= { IR[3:0], VALUE[7:0] };
we <= 0;
ioreq <= 0;
state <= STATE_FETCH_PREP;
end
3'b010 :
begin
`ifdef DISASSEMBLY
$display("CALL %h ",{ IR[3:0], VALUE[7:0] });
`endif
FUTURE_PC <= { IR[3:0], VALUE[7:0] };
addr <= SP;
we <= 1;
ioreq <= 0;
data_out <= { 4'b0000, PC[11:8]};
SP <= SP - 1;
state <= STATE_PUSH_PC_LOW;
end
3'b011 :
begin
`ifdef DISASSEMBLY
$display("MOV SP,%h ",{ IR[3:0], VALUE[7:0] });
`endif
SP <= { IR[3:0], VALUE[7:0] };
state <= STATE_FETCH_PREP;
end
3'b100 :
begin
`ifdef DISASSEMBLY
$display("IN R%d,[0x%h]",IR[1:0], VALUE);
`endif
ioreq <= 1;
we <= 0;
addr <= { 4'b0000, VALUE };
RESULT_REG <= IR[1:0];
state <= STATE_LOAD_VALUE_WAIT;
end
3'b101 :
begin
`ifdef DISASSEMBLY
$display("OUT [0x%h],R%d",VALUE,IR[1:0]);
`endif
ioreq <= 1;
we <= 1;
addr <= { 4'b0000, VALUE };
data_out <= R[IR[1:0]];
state <= STATE_FETCH_PREP;
end
3'b110 :
begin
// Special instuctions
case(IR[1:0])
2'b00 : begin
`ifdef DISASSEMBLY
$display("MOV CS,0x%h",VALUE);
`endif
CS <= VALUE[3:0];
state <= STATE_FETCH_PREP;
end
2'b01 : begin
`ifdef DISASSEMBLY
$display("MOV DS,0x%h",VALUE);
`endif
DS <= VALUE[3:0];
state <= STATE_FETCH_PREP;
end
2'b10 : begin
`ifdef DISASSEMBLY
$display("Unused opcode %h",IR);
`endif
state <= STATE_FETCH_PREP;
end
2'b11 : begin
`ifdef DISASSEMBLY
$display("Unused opcode %h",IR);
`endif
state <= STATE_FETCH_PREP;
end
endcase
end
3'b111 :
begin
case(IR[3:2])
2'b00 : begin
`ifdef DISASSEMBLY
$display("MOV R%d,0x%h",IR[1:0],VALUE);
`endif
R[IR[1:0]] <= VALUE;
state <= STATE_FETCH_PREP;
end
2'b01 : begin
`ifdef DISASSEMBLY
$display("LOAD R%d,[0x%h]",IR[1:0], {DS, VALUE});
`endif
addr <= { DS, VALUE };
we <= 0;
ioreq <= 0;
RESULT_REG <= IR[1:0];
state <= STATE_LOAD_VALUE_WAIT;
end
2'b10 : begin
`ifdef DISASSEMBLY
$display("STORE [0x%h],R%d", {DS, VALUE}, IR[1:0]);
`endif
addr <= { DS, VALUE };
we <= 1;
ioreq <= 0;
data_out <= R[IR[1:0]];
state <= STATE_FETCH_PREP;
end
2'b11 : begin
`ifdef DISASSEMBLY
$display("Unused opcode %h",IR);
`endif
state <= STATE_FETCH_PREP;
end
endcase
end
endcase
end
STATE_LOAD_VALUE_WAIT :
begin
// Sync with memory due to CLK
state <= STATE_LOAD_VALUE;
end
STATE_LOAD_VALUE :
begin
R[RESULT_REG] <= data_in;
we <= 0;
state <= STATE_FETCH_PREP;
end
STATE_ALU_RESULT_WAIT :
begin
state <= STATE_ALU_RESULT;
end
STATE_ALU_RESULT :
begin
R[RESULT_REG] <= alu_res;
state <= STATE_FETCH_PREP;
end
STATE_PUSH_PC_LOW :
begin
addr <= SP;
we <= 1;
ioreq <= 0;
data_out <= PC[7:0];
SP <= SP - 1;
state <= STATE_JUMP;
end
STATE_JUMP :
begin
`ifdef DISASSEMBLY
$display("Jumping to %h",FUTURE_PC);
`endif
PC <= FUTURE_PC;
state <= STATE_FETCH_PREP;
end
STATE_RET_VALUE_WAIT :
begin
// Sync with memory due to CLK
state <= STATE_RET_VALUE;
end
STATE_RET_VALUE :
begin
FUTURE_PC <= { 4'b0000, data_in };
we <= 0;
state <= STATE_RET_VALUE_WAIT2;
addr <= SP + 1;
we <= 0;
ioreq <= 0;
SP <= SP + 1;
end
STATE_RET_VALUE_WAIT2 :
begin
// Sync with memory due to CLK
state <= STATE_RET_VALUE2;
end
STATE_RET_VALUE2 :
begin
FUTURE_PC <= FUTURE_PC | ({ 4'b0000, data_in } << 8);
we <= 0;
state <= STATE_JUMP;
end
default :
begin
state <= STATE_FETCH_PREP;
end
endcase
end
end
endmodule
|