14 `define FLAG_Z 8'b10000000
15 `define FLAG_N 8'b01000000
16 `define FLAG_H 8'b00100000
17 `define FLAG_C 8'b00010000
19 `define STATE_FETCH 2'h0
20 `define STATE_DECODE 2'h1
21 `define STATE_EXECUTE 2'h2
22 `define STATE_WRITEBACK 2'h3
24 `define INSN_LD_reg_imm8 8'b00xxx110
25 `define INSN_HALT 8'b01110110
26 `define INSN_LD_HL_reg 8'b01110xxx
27 `define INSN_LD_reg_HL 8'b01xxx110
28 `define INSN_LD_reg_reg 8'b01xxxxxx
29 `define INSN_LD_reg_imm16 8'b00xx0001
30 `define INSN_LD_SP_HL 8'b11111001
31 `define INSN_PUSH_reg 8'b11xx0101
32 `define INSN_POP_reg 8'b11xx0001
33 `define INSN_LDH_AC 8'b111x0010 // Either LDH A,(C) or LDH (C),A
34 `define INSN_LDx_AHL 8'b001xx010 // LDD/LDI A,(HL) / (HL),A
35 `define INSN_ALU8 8'b10xxxxxx // 10 xxx yyy
36 `define INSN_NOP 8'b00000000
37 `define INSN_RST 8'b11xxx111
38 `define INSN_RET 8'b110x1001 // 1 = RETI, 0 = RET
39 `define INSN_RETCC 8'b110xx000
40 `define INSN_CALL 8'b11001101
41 `define INSN_CALLCC 8'b110xx100 // Not that call/cc.
42 `define INSN_JP_imm 8'b11000011
43 `define INSN_JPCC_imm 8'b110xx010
44 `define INSN_ALU_A 8'b00xxx111
45 `define INSN_JP_HL 8'b11101001
46 `define INSN_JR_imm 8'b00011000
47 `define INSN_JRCC_imm 8'b001xx000
49 `define INSN_cc_NZ 2'b00
50 `define INSN_cc_Z 2'b01
51 `define INSN_cc_NC 2'b10
52 `define INSN_cc_C 2'b11
54 `define INSN_reg_A 3'b111
55 `define INSN_reg_B 3'b000
56 `define INSN_reg_C 3'b001
57 `define INSN_reg_D 3'b010
58 `define INSN_reg_E 3'b011
59 `define INSN_reg_H 3'b100
60 `define INSN_reg_L 3'b101
61 `define INSN_reg_dHL 3'b110
62 `define INSN_reg16_BC 2'b00
63 `define INSN_reg16_DE 2'b01
64 `define INSN_reg16_HL 2'b10
65 `define INSN_reg16_SP 2'b11
66 `define INSN_stack_AF 2'b11
67 `define INSN_stack_BC 2'b00
68 `define INSN_stack_DE 2'b01
69 `define INSN_stack_HL 2'b10
70 `define INSN_alu_ADD 3'b000
71 `define INSN_alu_ADC 3'b001
72 `define INSN_alu_SUB 3'b010
73 `define INSN_alu_SBC 3'b011
74 `define INSN_alu_AND 3'b100
75 `define INSN_alu_XOR 3'b101
76 `define INSN_alu_OR 3'b110
77 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
78 `define INSN_alu_RLCA 3'b000
79 `define INSN_alu_RRCA 3'b001
80 `define INSN_alu_RLA 3'b010
81 `define INSN_alu_RRA 3'b011
82 `define INSN_alu_DAA 3'b100
83 `define INSN_alu_CPL 3'b101
84 `define INSN_alu_SCF 3'b110
85 `define INSN_alu_CCF 3'b111
89 output reg [15:0] busaddress, /* BUS_* is latched on STATE_FETCH. */
91 output reg buswr, output reg busrd);
93 reg [1:0] state = 0; /* State within this bus cycle (see STATE_*). */
94 reg [2:0] cycle = 0; /* Cycle for instructions. */
96 reg [7:0] registers[11:0];
98 reg [15:0] address; /* Address for the next bus operation. */
100 reg [7:0] opcode; /* Opcode from the current machine cycle. */
102 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
103 reg rd = 1, wr = 0, newcycle = 1;
105 reg [7:0] tmp, tmp2; /* Generic temporary regs. */
108 assign busdata = buswr ? buswdata : 8'bzzzzzzzz;
133 always @(posedge clk)
137 busaddress <= {registers[`REG_PCH], registers[`REG_PCL]};
141 busaddress <= address;
147 state <= `STATE_DECODE;
156 if (rd) rdata <= busdata;
163 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
164 wdata <= 8'bxxxxxxxx;
165 state <= `STATE_EXECUTE;
167 `STATE_EXECUTE: begin
168 `define EXEC_INC_PC \
169 {registers[`REG_PCH], registers[`REG_PCL]} <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
170 `define EXEC_NEXTADDR_PCINC \
171 address <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
172 `define EXEC_NEWCYCLE \
173 newcycle <= 1; rd <= 1; wr <= 0
175 `INSN_LD_reg_imm8: begin
179 `EXEC_NEXTADDR_PCINC;
184 if (opcode[5:3] == `INSN_reg_dHL) begin
185 address <= {registers[`REG_H], registers[`REG_L]};
200 /* XXX Interrupts needed for HALT. */
202 `INSN_LD_HL_reg: begin
206 `INSN_reg_A: wdata <= registers[`REG_A];
207 `INSN_reg_B: wdata <= registers[`REG_B];
208 `INSN_reg_C: wdata <= registers[`REG_C];
209 `INSN_reg_D: wdata <= registers[`REG_D];
210 `INSN_reg_E: wdata <= registers[`REG_E];
211 `INSN_reg_H: wdata <= registers[`REG_H];
212 `INSN_reg_L: wdata <= registers[`REG_L];
214 address <= {registers[`REG_H], registers[`REG_L]};
223 `INSN_LD_reg_HL: begin
226 address <= {registers[`REG_H], registers[`REG_L]};
236 `INSN_LD_reg_reg: begin
240 `INSN_reg_A: tmp <= registers[`REG_A];
241 `INSN_reg_B: tmp <= registers[`REG_B];
242 `INSN_reg_C: tmp <= registers[`REG_C];
243 `INSN_reg_D: tmp <= registers[`REG_D];
244 `INSN_reg_E: tmp <= registers[`REG_E];
245 `INSN_reg_H: tmp <= registers[`REG_H];
246 `INSN_reg_L: tmp <= registers[`REG_L];
249 `INSN_LD_reg_imm16: begin
253 `EXEC_NEXTADDR_PCINC;
257 `EXEC_NEXTADDR_PCINC;
260 2: begin `EXEC_NEWCYCLE; end
263 `INSN_LD_SP_HL: begin
266 tmp <= registers[`REG_H];
271 tmp <= registers[`REG_L];
275 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
279 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
281 `INSN_stack_AF: wdata <= registers[`REG_A];
282 `INSN_stack_BC: wdata <= registers[`REG_B];
283 `INSN_stack_DE: wdata <= registers[`REG_D];
284 `INSN_stack_HL: wdata <= registers[`REG_H];
289 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
291 `INSN_stack_AF: wdata <= registers[`REG_F];
292 `INSN_stack_BC: wdata <= registers[`REG_C];
293 `INSN_stack_DE: wdata <= registers[`REG_E];
294 `INSN_stack_HL: wdata <= registers[`REG_L];
297 2: begin /* Twiddle thumbs. */ end
304 `INSN_POP_reg: begin /* POP is 12 cycles! */
308 address <= {registers[`REG_SPH],registers[`REG_SPL]};
312 address <= {registers[`REG_SPH],registers[`REG_SPL]};
323 address <= {8'hFF,registers[`REG_C]};
324 if (opcode[4]) begin // LD A,(C)
328 wdata <= registers[`REG_A];
340 address <= {registers[`REG_H],registers[`REG_L]};
341 if (opcode[3]) begin // LDx A, (HL)
345 wdata <= registers[`REG_A];
355 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
356 // fffffffff fuck your shit, read from (HL) :(
358 address <= {registers[`REG_H], registers[`REG_L]};
363 `INSN_reg_A: tmp <= registers[`REG_A];
364 `INSN_reg_B: tmp <= registers[`REG_B];
365 `INSN_reg_C: tmp <= registers[`REG_C];
366 `INSN_reg_D: tmp <= registers[`REG_D];
367 `INSN_reg_E: tmp <= registers[`REG_E];
368 `INSN_reg_H: tmp <= registers[`REG_H];
369 `INSN_reg_L: tmp <= registers[`REG_L];
370 `INSN_reg_dHL: tmp <= rdata;
385 `EXEC_INC_PC; // This goes FIRST in RST
389 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
390 wdata <= registers[`REG_PCH];
394 address <= {registers[`REG_SPH],registers[`REG_SPL]}-2;
395 wdata <= registers[`REG_PCL];
399 {registers[`REG_PCH],registers[`REG_PCL]} <=
400 {10'b0,opcode[5:3],3'b0};
404 `INSN_RET,`INSN_RETCC: begin
408 address <= {registers[`REG_SPH],registers[`REG_SPL]};
410 1: begin // SPECIAL CASE: cycle does NOT increase linearly with ret!
412 case (opcode[4:3]) // cycle 1 is skipped if we are not retcc
413 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
414 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
415 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
416 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
419 address <= {registers[`REG_SPH],registers[`REG_SPL]};
423 address <= {registers[`REG_SPH],registers[`REG_SPL]} + 1;
425 3: begin /* twiddle thumbs */ end
428 // do NOT increment PC!
432 `INSN_CALL,`INSN_CALLCC: begin
436 `EXEC_NEXTADDR_PCINC;
441 `EXEC_NEXTADDR_PCINC;
446 if (!opcode[0]) // i.e., is callcc
447 /* We need to check the condition code to bail out. */
449 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
450 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
451 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
452 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
456 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 1;
457 wdata <= registers[`REG_PCH];
461 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 2;
462 wdata <= registers[`REG_PCL];
466 `EXEC_NEWCYCLE; /* do NOT increment the PC */
470 `INSN_JP_imm,`INSN_JPCC_imm: begin
474 `EXEC_NEXTADDR_PCINC;
479 `EXEC_NEXTADDR_PCINC;
484 if (!opcode[0]) begin // i.e., JP cc,nn
485 /* We need to check the condition code to bail out. */
487 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
488 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
489 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
490 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
502 `INSN_JR_imm,`INSN_JRCC_imm: begin
506 `EXEC_NEXTADDR_PCINC;
511 if (opcode[5]) begin // i.e., JP cc,nn
512 /* We need to check the condition code to bail out. */
514 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
515 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
516 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
517 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
529 state <= `STATE_WRITEBACK;
531 `STATE_WRITEBACK: begin
536 1: case (opcode[5:3])
537 `INSN_reg_A: begin registers[`REG_A] <= rdata; end
538 `INSN_reg_B: begin registers[`REG_B] <= rdata; end
539 `INSN_reg_C: begin registers[`REG_C] <= rdata; end
540 `INSN_reg_D: begin registers[`REG_D] <= rdata; end
541 `INSN_reg_E: begin registers[`REG_E] <= rdata; end
542 `INSN_reg_H: begin registers[`REG_H] <= rdata; end
543 `INSN_reg_L: begin registers[`REG_L] <= rdata; end
544 `INSN_reg_dHL: begin /* Go off to cycle 2 */ end
549 /* Nothing needs happen here. */
550 /* XXX Interrupts needed for HALT. */
552 `INSN_LD_HL_reg: begin
553 /* Nothing of interest here */
555 `INSN_LD_reg_HL: begin
560 `INSN_reg_A: registers[`REG_A] <= tmp;
561 `INSN_reg_B: registers[`REG_B] <= tmp;
562 `INSN_reg_C: registers[`REG_C] <= tmp;
563 `INSN_reg_D: registers[`REG_D] <= tmp;
564 `INSN_reg_E: registers[`REG_E] <= tmp;
565 `INSN_reg_H: registers[`REG_H] <= tmp;
566 `INSN_reg_L: registers[`REG_L] <= tmp;
571 `INSN_LD_reg_reg: begin
573 `INSN_reg_A: registers[`REG_A] <= tmp;
574 `INSN_reg_B: registers[`REG_B] <= tmp;
575 `INSN_reg_C: registers[`REG_C] <= tmp;
576 `INSN_reg_D: registers[`REG_D] <= tmp;
577 `INSN_reg_E: registers[`REG_E] <= tmp;
578 `INSN_reg_H: registers[`REG_H] <= tmp;
579 `INSN_reg_L: registers[`REG_L] <= tmp;
582 `INSN_LD_reg_imm16: begin
587 `INSN_reg16_BC: registers[`REG_C] <= rdata;
588 `INSN_reg16_DE: registers[`REG_E] <= rdata;
589 `INSN_reg16_HL: registers[`REG_L] <= rdata;
590 `INSN_reg16_SP: registers[`REG_SPL] <= rdata;
595 `INSN_reg16_BC: registers[`REG_B] <= rdata;
596 `INSN_reg16_DE: registers[`REG_D] <= rdata;
597 `INSN_reg16_HL: registers[`REG_H] <= rdata;
598 `INSN_reg16_SP: registers[`REG_SPH] <= rdata;
603 `INSN_LD_SP_HL: begin
605 0: registers[`REG_SPH] <= tmp;
606 1: registers[`REG_SPL] <= tmp;
609 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
611 0: {registers[`REG_SPH],registers[`REG_SPL]} <=
612 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
613 1: {registers[`REG_SPH],registers[`REG_SPL]} <=
614 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
615 2: begin /* type F */ end
616 3: begin /* type F */ end
619 `INSN_POP_reg: begin /* POP is 12 cycles! */
621 0: {registers[`REG_SPH],registers[`REG_SPL]} <=
622 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
625 `INSN_stack_AF: registers[`REG_F] <= rdata;
626 `INSN_stack_BC: registers[`REG_C] <= rdata;
627 `INSN_stack_DE: registers[`REG_E] <= rdata;
628 `INSN_stack_HL: registers[`REG_L] <= rdata;
630 {registers[`REG_SPH],registers[`REG_SPL]} <=
631 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
635 `INSN_stack_AF: registers[`REG_A] <= rdata;
636 `INSN_stack_BC: registers[`REG_B] <= rdata;
637 `INSN_stack_DE: registers[`REG_D] <= rdata;
638 `INSN_stack_HL: registers[`REG_H] <= rdata;
645 0: begin /* Type F */ end
647 registers[`REG_A] <= rdata;
652 0: begin /* Type F */ end
655 registers[`REG_A] <= rdata;
656 {registers[`REG_H],registers[`REG_L]} <=
657 opcode[4] ? // if set, LDD, else LDI
658 ({registers[`REG_H],registers[`REG_L]} - 1) :
659 ({registers[`REG_H],registers[`REG_L]} + 1);
664 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
665 /* Sit on our asses. */
666 end else begin /* Actually do the computation! */
670 registers[`REG_A] + tmp;
672 { /* Z */ ((registers[`REG_A] + tmp) == 0) ? 1'b1 : 1'b0,
674 /* H */ (({1'b0,registers[`REG_A][3:0]} + {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
675 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
676 registers[`REG_F][3:0]
681 registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]};
683 { /* Z */ ((registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]}) == 0) ? 1'b1 : 1'b0,
685 /* H */ (({1'b0,registers[`REG_A][3:0]} + {1'b0,tmp[3:0]} + {4'b0,registers[`REG_F][4]}) >> 4 == 1) ? 1'b1 : 1'b0,
686 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp} + {8'b0,registers[`REG_F][4]}) >> 8 == 1) ? 1'b1 : 1'b0,
687 registers[`REG_F][3:0]
692 registers[`REG_A] - tmp;
694 { /* Z */ ((registers[`REG_A] - tmp) == 0) ? 1'b1 : 1'b0,
696 /* H */ (({1'b0,registers[`REG_A][3:0]} - {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
697 /* C */ (({1'b0,registers[`REG_A]} - {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
698 registers[`REG_F][3:0]
703 registers[`REG_A] - (tmp + {7'b0,registers[`REG_F][4]});
705 { /* Z */ ((registers[`REG_A] - (tmp + {7'b0,registers[`REG_F][4]})) == 0) ? 1'b1 : 1'b0,
707 /* H */ (({1'b0,registers[`REG_A][3:0]} - ({1'b0,tmp[3:0]} + {4'b0,registers[`REG_F][4]})) >> 4 == 1) ? 1'b1 : 1'b0,
708 /* C */ (({1'b0,registers[`REG_A]} - ({1'b0,tmp} + {8'b0,registers[`REG_F][4]})) >> 8 == 1) ? 1'b1 : 1'b0,
709 registers[`REG_F][3:0]
714 registers[`REG_A] & tmp;
716 { /* Z */ ((registers[`REG_A] & tmp) == 0) ? 1'b1 : 1'b0,
718 registers[`REG_F][3:0]
723 registers[`REG_A] | tmp;
725 { /* Z */ ((registers[`REG_A] | tmp) == 0) ? 1'b1 : 1'b0,
727 registers[`REG_F][3:0]
732 registers[`REG_A] ^ tmp;
734 { /* Z */ ((registers[`REG_A] ^ tmp) == 0) ? 1'b1 : 1'b0,
736 registers[`REG_F][3:0]
741 { /* Z */ ((registers[`REG_A] - tmp) == 0) ? 1'b1 : 1'b0,
743 /* H */ (({1'b0,registers[`REG_A][3:0]} - {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
744 /* C */ (({1'b0,registers[`REG_A]} - {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
745 registers[`REG_F][3:0]
755 `INSN_alu_RLCA: begin
756 registers[`REG_A] <= {registers[`REG_A][6:0],registers[`REG_A][7]};
757 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][7],registers[`REG_F][3:0]};
759 `INSN_alu_RRCA: begin
760 registers[`REG_A] <= {registers[`REG_A][0],registers[`REG_A][7:1]};
761 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][0],registers[`REG_F][3:0]};
764 registers[`REG_A] <= {registers[`REG_A][6:0],registers[`REG_F][4]};
765 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][7],registers[`REG_F][3:0]};
768 registers[`REG_A] <= {registers[`REG_A][4],registers[`REG_A][7:1]};
769 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][0],registers[`REG_F][3:0]};
772 registers[`REG_A] <= ~registers[`REG_A];
773 registers[`REG_F] <= {registers[`REG_F][7],1'b1,1'b1,registers[`REG_F][4:0]};
776 registers[`REG_F] <= {registers[`REG_F][7:5],1,registers[`REG_F][3:0]};
779 registers[`REG_F] <= {registers[`REG_F][7:5],~registers[`REG_F][4],registers[`REG_F][3:0]};
783 `INSN_NOP: begin /* NOP! */ end
786 0: begin /* type F */ end
787 1: begin /* type F */ end
788 2: begin /* type F */ end
789 3: {registers[`REG_SPH],registers[`REG_SPL]} <=
790 {registers[`REG_SPH],registers[`REG_SPL]}-2;
793 `INSN_RET,`INSN_RETCC: begin
795 0: if (opcode[0]) // i.e., not RETCC
796 cycle <= 1; // Skip cycle 1; it gets incremented on the next round.
797 1: begin /* Nothing need happen here. */ end
798 2: registers[`REG_PCL] <= rdata;
799 3: registers[`REG_PCH] <= rdata;
801 {registers[`REG_SPH],registers[`REG_SPL]} <=
802 {registers[`REG_SPH],registers[`REG_SPL]} + 2;
803 if (opcode[4] && (opcode != `INSN_RETCC)) /* RETI */
808 `INSN_CALL,`INSN_CALLCC: begin
810 0: begin /* type F */ end
811 1: tmp <= rdata; // tmp contains newpcl
812 2: tmp2 <= rdata; // tmp2 contains newpch
813 3: begin /* type F */ end
814 4: registers[`REG_PCH] <= tmp2;
816 {registers[`REG_SPH],registers[`REG_SPL]} <=
817 {registers[`REG_SPH],registers[`REG_SPL]} - 2;
818 registers[`REG_PCL] <= tmp;
822 `INSN_JP_imm,`INSN_JPCC_imm: begin
824 0: begin /* type F */ end
825 1: tmp <= rdata; // tmp contains newpcl
826 2: tmp2 <= rdata; // tmp2 contains newpch
827 3: {registers[`REG_PCH],registers[`REG_PCL]} <=
832 {registers[`REG_PCH],registers[`REG_PCL]} <=
833 {registers[`REG_H],registers[`REG_L]};
835 `INSN_JR_imm,`INSN_JRCC_imm: begin
837 0: begin /* type F */ end
839 2: {registers[`REG_PCH],registers[`REG_PCL]} <=
840 {registers[`REG_PCH],registers[`REG_PCL]} +
841 {tmp[7]?8'hFF:8'h00,tmp};
847 state <= `STATE_FETCH;