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_CALL 8'b11001101
40 `define INSN_CALLCC 8'b110xx100 // Not that call/cc.
41 `define INSN_JP_imm 8'b11000011
42 `define INSN_JPCC_imm 8'b110xx010
43 `define INSN_ALU_A 8'b00xxx111
44 `define INSN_JP_HL 8'b11101001
45 `define INSN_JR_imm 8'b00011000
46 `define INSN_JRCC_imm 8'b001xx000
48 `define INSN_cc_NZ 2'b00
49 `define INSN_cc_Z 2'b01
50 `define INSN_cc_NC 2'b10
51 `define INSN_cc_C 2'b11
53 `define INSN_reg_A 3'b111
54 `define INSN_reg_B 3'b000
55 `define INSN_reg_C 3'b001
56 `define INSN_reg_D 3'b010
57 `define INSN_reg_E 3'b011
58 `define INSN_reg_H 3'b100
59 `define INSN_reg_L 3'b101
60 `define INSN_reg_dHL 3'b110
61 `define INSN_reg16_BC 2'b00
62 `define INSN_reg16_DE 2'b01
63 `define INSN_reg16_HL 2'b10
64 `define INSN_reg16_SP 2'b11
65 `define INSN_stack_AF 2'b11
66 `define INSN_stack_BC 2'b00
67 `define INSN_stack_DE 2'b01
68 `define INSN_stack_HL 2'b10
69 `define INSN_alu_ADD 3'b000
70 `define INSN_alu_ADC 3'b001
71 `define INSN_alu_SUB 3'b010
72 `define INSN_alu_SBC 3'b011
73 `define INSN_alu_AND 3'b100
74 `define INSN_alu_XOR 3'b101
75 `define INSN_alu_OR 3'b110
76 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
77 `define INSN_alu_RLCA 3'b000
78 `define INSN_alu_RRCA 3'b001
79 `define INSN_alu_RLA 3'b010
80 `define INSN_alu_RRA 3'b011
81 `define INSN_alu_DAA 3'b100
82 `define INSN_alu_CPL 3'b101
83 `define INSN_alu_SCF 3'b110
84 `define INSN_alu_CCF 3'b111
88 output reg [15:0] busaddress, /* BUS_* is latched on STATE_FETCH. */
90 output reg buswr, output reg busrd);
92 reg [1:0] state = 0; /* State within this bus cycle (see STATE_*). */
93 reg [2:0] cycle = 0; /* Cycle for instructions. */
95 reg [7:0] registers[11:0];
97 reg [15:0] address; /* Address for the next bus operation. */
99 reg [7:0] opcode; /* Opcode from the current machine cycle. */
101 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
102 reg rd = 1, wr = 0, newcycle = 1;
104 reg [7:0] tmp, tmp2; /* Generic temporary regs. */
107 assign busdata = buswr ? buswdata : 8'bzzzzzzzz;
132 always @(posedge clk)
136 busaddress <= {registers[`REG_PCH], registers[`REG_PCL]};
140 busaddress <= address;
146 state <= `STATE_DECODE;
155 if (rd) rdata <= busdata;
162 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
163 wdata <= 8'bxxxxxxxx;
164 state <= `STATE_EXECUTE;
166 `STATE_EXECUTE: begin
167 `define EXEC_INC_PC \
168 {registers[`REG_PCH], registers[`REG_PCL]} <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
169 `define EXEC_NEXTADDR_PCINC \
170 address <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
171 `define EXEC_NEWCYCLE \
172 newcycle <= 1; rd <= 1; wr <= 0
174 `INSN_LD_reg_imm8: begin
178 `EXEC_NEXTADDR_PCINC;
183 if (opcode[5:3] == `INSN_reg_dHL) begin
184 address <= {registers[`REG_H], registers[`REG_L]};
199 /* XXX Interrupts needed for HALT. */
201 `INSN_LD_HL_reg: begin
205 `INSN_reg_A: wdata <= registers[`REG_A];
206 `INSN_reg_B: wdata <= registers[`REG_B];
207 `INSN_reg_C: wdata <= registers[`REG_C];
208 `INSN_reg_D: wdata <= registers[`REG_D];
209 `INSN_reg_E: wdata <= registers[`REG_E];
210 `INSN_reg_H: wdata <= registers[`REG_H];
211 `INSN_reg_L: wdata <= registers[`REG_L];
213 address <= {registers[`REG_H], registers[`REG_L]};
222 `INSN_LD_reg_HL: begin
225 address <= {registers[`REG_H], registers[`REG_L]};
235 `INSN_LD_reg_reg: begin
239 `INSN_reg_A: tmp <= registers[`REG_A];
240 `INSN_reg_B: tmp <= registers[`REG_B];
241 `INSN_reg_C: tmp <= registers[`REG_C];
242 `INSN_reg_D: tmp <= registers[`REG_D];
243 `INSN_reg_E: tmp <= registers[`REG_E];
244 `INSN_reg_H: tmp <= registers[`REG_H];
245 `INSN_reg_L: tmp <= registers[`REG_L];
248 `INSN_LD_reg_imm16: begin
252 `EXEC_NEXTADDR_PCINC;
256 `EXEC_NEXTADDR_PCINC;
259 2: begin `EXEC_NEWCYCLE; end
262 `INSN_LD_SP_HL: begin
265 tmp <= registers[`REG_H];
270 tmp <= registers[`REG_L];
274 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
278 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
280 `INSN_stack_AF: wdata <= registers[`REG_A];
281 `INSN_stack_BC: wdata <= registers[`REG_B];
282 `INSN_stack_DE: wdata <= registers[`REG_D];
283 `INSN_stack_HL: wdata <= registers[`REG_H];
288 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
290 `INSN_stack_AF: wdata <= registers[`REG_F];
291 `INSN_stack_BC: wdata <= registers[`REG_C];
292 `INSN_stack_DE: wdata <= registers[`REG_E];
293 `INSN_stack_HL: wdata <= registers[`REG_L];
296 2: begin /* Twiddle thumbs. */ end
303 `INSN_POP_reg: begin /* POP is 12 cycles! */
307 address <= {registers[`REG_SPH],registers[`REG_SPL]};
311 address <= {registers[`REG_SPH],registers[`REG_SPL]};
322 address <= {8'hFF,registers[`REG_C]};
323 if (opcode[4]) begin // LD A,(C)
327 wdata <= registers[`REG_A];
339 address <= {registers[`REG_H],registers[`REG_L]};
340 if (opcode[3]) begin // LDx A, (HL)
344 wdata <= registers[`REG_A];
354 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
355 // fffffffff fuck your shit, read from (HL) :(
357 address <= {registers[`REG_H], registers[`REG_L]};
362 `INSN_reg_A: tmp <= registers[`REG_A];
363 `INSN_reg_B: tmp <= registers[`REG_B];
364 `INSN_reg_C: tmp <= registers[`REG_C];
365 `INSN_reg_D: tmp <= registers[`REG_D];
366 `INSN_reg_E: tmp <= registers[`REG_E];
367 `INSN_reg_H: tmp <= registers[`REG_H];
368 `INSN_reg_L: tmp <= registers[`REG_L];
369 `INSN_reg_dHL: tmp <= rdata;
384 `EXEC_INC_PC; // This goes FIRST in RST
388 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
389 wdata <= registers[`REG_PCH];
393 address <= {registers[`REG_SPH],registers[`REG_SPL]}-2;
394 wdata <= registers[`REG_PCL];
398 {registers[`REG_PCH],registers[`REG_PCL]} <=
399 {10'b0,opcode[5:3],3'b0};
407 address <= {registers[`REG_SPH],registers[`REG_SPL]};
411 address <= {registers[`REG_SPH],registers[`REG_SPL]} + 1;
413 2: begin /* twiddle thumbs */ end
416 // do NOT increment PC!
420 `INSN_CALL,`INSN_CALLCC: begin
424 `EXEC_NEXTADDR_PCINC;
429 `EXEC_NEXTADDR_PCINC;
434 if (!opcode[0]) // i.e., is callcc
435 /* We need to check the condition code to bail out. */
437 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
438 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
439 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
440 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
444 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 1;
445 wdata <= registers[`REG_PCH];
449 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 2;
450 wdata <= registers[`REG_PCL];
454 `EXEC_NEWCYCLE; /* do NOT increment the PC */
458 `INSN_JP_imm,`INSN_JPCC_imm: begin
462 `EXEC_NEXTADDR_PCINC;
467 `EXEC_NEXTADDR_PCINC;
472 if (!opcode[0]) begin // i.e., JP cc,nn
473 /* We need to check the condition code to bail out. */
475 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
476 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
477 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
478 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
490 `INSN_JR_imm,`INSN_JRCC_imm: begin
494 `EXEC_NEXTADDR_PCINC;
499 if (opcode[5]) begin // i.e., JP cc,nn
500 /* We need to check the condition code to bail out. */
502 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
503 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
504 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
505 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
517 state <= `STATE_WRITEBACK;
519 `STATE_WRITEBACK: begin
524 1: case (opcode[5:3])
525 `INSN_reg_A: begin registers[`REG_A] <= rdata; end
526 `INSN_reg_B: begin registers[`REG_B] <= rdata; end
527 `INSN_reg_C: begin registers[`REG_C] <= rdata; end
528 `INSN_reg_D: begin registers[`REG_D] <= rdata; end
529 `INSN_reg_E: begin registers[`REG_E] <= rdata; end
530 `INSN_reg_H: begin registers[`REG_H] <= rdata; end
531 `INSN_reg_L: begin registers[`REG_L] <= rdata; end
532 `INSN_reg_dHL: begin /* Go off to cycle 2 */ end
537 /* Nothing needs happen here. */
538 /* XXX Interrupts needed for HALT. */
540 `INSN_LD_HL_reg: begin
541 /* Nothing of interest here */
543 `INSN_LD_reg_HL: begin
548 `INSN_reg_A: registers[`REG_A] <= tmp;
549 `INSN_reg_B: registers[`REG_B] <= tmp;
550 `INSN_reg_C: registers[`REG_C] <= tmp;
551 `INSN_reg_D: registers[`REG_D] <= tmp;
552 `INSN_reg_E: registers[`REG_E] <= tmp;
553 `INSN_reg_H: registers[`REG_H] <= tmp;
554 `INSN_reg_L: registers[`REG_L] <= tmp;
559 `INSN_LD_reg_reg: begin
561 `INSN_reg_A: registers[`REG_A] <= tmp;
562 `INSN_reg_B: registers[`REG_B] <= tmp;
563 `INSN_reg_C: registers[`REG_C] <= tmp;
564 `INSN_reg_D: registers[`REG_D] <= tmp;
565 `INSN_reg_E: registers[`REG_E] <= tmp;
566 `INSN_reg_H: registers[`REG_H] <= tmp;
567 `INSN_reg_L: registers[`REG_L] <= tmp;
570 `INSN_LD_reg_imm16: begin
575 `INSN_reg16_BC: registers[`REG_C] <= rdata;
576 `INSN_reg16_DE: registers[`REG_E] <= rdata;
577 `INSN_reg16_HL: registers[`REG_L] <= rdata;
578 `INSN_reg16_SP: registers[`REG_SPL] <= rdata;
583 `INSN_reg16_BC: registers[`REG_B] <= rdata;
584 `INSN_reg16_DE: registers[`REG_D] <= rdata;
585 `INSN_reg16_HL: registers[`REG_H] <= rdata;
586 `INSN_reg16_SP: registers[`REG_SPH] <= rdata;
591 `INSN_LD_SP_HL: begin
593 0: registers[`REG_SPH] <= tmp;
594 1: registers[`REG_SPL] <= tmp;
597 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
599 0: {registers[`REG_SPH],registers[`REG_SPL]} <=
600 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
601 1: {registers[`REG_SPH],registers[`REG_SPL]} <=
602 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
603 2: begin /* type F */ end
604 3: begin /* type F */ end
607 `INSN_POP_reg: begin /* POP is 12 cycles! */
609 0: {registers[`REG_SPH],registers[`REG_SPL]} <=
610 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
613 `INSN_stack_AF: registers[`REG_F] <= rdata;
614 `INSN_stack_BC: registers[`REG_C] <= rdata;
615 `INSN_stack_DE: registers[`REG_E] <= rdata;
616 `INSN_stack_HL: registers[`REG_L] <= rdata;
618 {registers[`REG_SPH],registers[`REG_SPL]} <=
619 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
623 `INSN_stack_AF: registers[`REG_A] <= rdata;
624 `INSN_stack_BC: registers[`REG_B] <= rdata;
625 `INSN_stack_DE: registers[`REG_D] <= rdata;
626 `INSN_stack_HL: registers[`REG_H] <= rdata;
633 0: begin /* Type F */ end
635 registers[`REG_A] <= rdata;
640 0: begin /* Type F */ end
643 registers[`REG_A] <= rdata;
644 {registers[`REG_H],registers[`REG_L]} <=
645 opcode[4] ? // if set, LDD, else LDI
646 ({registers[`REG_H],registers[`REG_L]} - 1) :
647 ({registers[`REG_H],registers[`REG_L]} + 1);
652 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
653 /* Sit on our asses. */
654 end else begin /* Actually do the computation! */
658 registers[`REG_A] + tmp;
660 { /* Z */ ((registers[`REG_A] + tmp) == 0) ? 1'b1 : 1'b0,
662 /* H */ (({1'b0,registers[`REG_A][3:0]} + {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
663 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
664 registers[`REG_F][3:0]
669 registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]};
671 { /* Z */ ((registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]}) == 0) ? 1'b1 : 1'b0,
673 /* 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,
674 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp} + {8'b0,registers[`REG_F][4]}) >> 8 == 1) ? 1'b1 : 1'b0,
675 registers[`REG_F][3:0]
680 registers[`REG_A] - tmp;
682 { /* Z */ ((registers[`REG_A] - tmp) == 0) ? 1'b1 : 1'b0,
684 /* H */ (({1'b0,registers[`REG_A][3:0]} - {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
685 /* C */ (({1'b0,registers[`REG_A]} - {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
686 registers[`REG_F][3:0]
691 registers[`REG_A] - (tmp + {7'b0,registers[`REG_F][4]});
693 { /* Z */ ((registers[`REG_A] - (tmp + {7'b0,registers[`REG_F][4]})) == 0) ? 1'b1 : 1'b0,
695 /* 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,
696 /* C */ (({1'b0,registers[`REG_A]} - ({1'b0,tmp} + {8'b0,registers[`REG_F][4]})) >> 8 == 1) ? 1'b1 : 1'b0,
697 registers[`REG_F][3:0]
702 registers[`REG_A] & tmp;
704 { /* Z */ ((registers[`REG_A] & tmp) == 0) ? 1'b1 : 1'b0,
706 registers[`REG_F][3:0]
711 registers[`REG_A] | tmp;
713 { /* Z */ ((registers[`REG_A] | tmp) == 0) ? 1'b1 : 1'b0,
715 registers[`REG_F][3:0]
720 registers[`REG_A] ^ tmp;
722 { /* Z */ ((registers[`REG_A] ^ tmp) == 0) ? 1'b1 : 1'b0,
724 registers[`REG_F][3:0]
729 { /* Z */ ((registers[`REG_A] - tmp) == 0) ? 1'b1 : 1'b0,
731 /* H */ (({1'b0,registers[`REG_A][3:0]} - {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
732 /* C */ (({1'b0,registers[`REG_A]} - {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
733 registers[`REG_F][3:0]
743 `INSN_alu_RLCA: begin
744 registers[`REG_A] <= {registers[`REG_A][6:0],registers[`REG_A][7]};
745 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][7],registers[`REG_F][3:0]};
747 `INSN_alu_RRCA: begin
748 registers[`REG_A] <= {registers[`REG_A][0],registers[`REG_A][7:1]};
749 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][0],registers[`REG_F][3:0]};
752 registers[`REG_A] <= {registers[`REG_A][6:0],registers[`REG_F][4]};
753 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][7],registers[`REG_F][3:0]};
756 registers[`REG_A] <= {registers[`REG_A][4],registers[`REG_A][7:1]};
757 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][0],registers[`REG_F][3:0]};
760 registers[`REG_A] <= ~registers[`REG_A];
761 registers[`REG_F] <= {registers[`REG_F][7],1'b1,1'b1,registers[`REG_F][4:0]};
764 registers[`REG_F] <= {registers[`REG_F][7:5],1,registers[`REG_F][3:0]};
767 registers[`REG_F] <= {registers[`REG_F][7:5],~registers[`REG_F][4],registers[`REG_F][3:0]};
771 `INSN_NOP: begin /* NOP! */ end
774 0: begin /* type F */ end
775 1: begin /* type F */ end
776 2: begin /* type F */ end
777 3: {registers[`REG_SPH],registers[`REG_SPL]} <=
778 {registers[`REG_SPH],registers[`REG_SPL]}-2;
783 0: begin /* type F */ end
784 1: registers[`REG_PCL] <= rdata;
785 2: registers[`REG_PCH] <= rdata;
787 {registers[`REG_SPH],registers[`REG_SPL]} <=
788 {registers[`REG_SPH],registers[`REG_SPL]} + 2;
789 if (opcode[4]) /* RETI */
794 `INSN_CALL,`INSN_CALLCC: begin
796 0: begin /* type F */ end
797 1: tmp <= rdata; // tmp contains newpcl
798 2: tmp2 <= rdata; // tmp2 contains newpch
799 3: begin /* type F */ end
800 4: registers[`REG_PCH] <= tmp2;
802 {registers[`REG_SPH],registers[`REG_SPL]} <=
803 {registers[`REG_SPH],registers[`REG_SPL]} - 2;
804 registers[`REG_PCL] <= tmp;
808 `INSN_JP_imm,`INSN_JPCC_imm: begin
810 0: begin /* type F */ end
811 1: tmp <= rdata; // tmp contains newpcl
812 2: tmp2 <= rdata; // tmp2 contains newpch
813 3: {registers[`REG_PCH],registers[`REG_PCL]} <=
818 {registers[`REG_PCH],registers[`REG_PCL]} <=
819 {registers[`REG_H],registers[`REG_L]};
821 `INSN_JR_imm,`INSN_JRCC_imm: begin
823 0: begin /* type F */ end
825 2: {registers[`REG_PCH],registers[`REG_PCL]} <=
826 {registers[`REG_PCH],registers[`REG_PCL]} +
827 {tmp[7]?8'hFF:8'h00,tmp};
833 state <= `STATE_FETCH;