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_JP_imm 8'b11000011
41 `define INSN_JPCC_imm 8'b110xx010
42 `define INSN_ALU_A 8'b00xxx111
44 `define INSN_cc_NZ 2'b00
45 `define INSN_cc_Z 2'b01
46 `define INSN_cc_NC 2'b10
47 `define INSN_cc_C 2'b11
49 `define INSN_reg_A 3'b111
50 `define INSN_reg_B 3'b000
51 `define INSN_reg_C 3'b001
52 `define INSN_reg_D 3'b010
53 `define INSN_reg_E 3'b011
54 `define INSN_reg_H 3'b100
55 `define INSN_reg_L 3'b101
56 `define INSN_reg_dHL 3'b110
57 `define INSN_reg16_BC 2'b00
58 `define INSN_reg16_DE 2'b01
59 `define INSN_reg16_HL 2'b10
60 `define INSN_reg16_SP 2'b11
61 `define INSN_stack_AF 2'b11
62 `define INSN_stack_BC 2'b00
63 `define INSN_stack_DE 2'b01
64 `define INSN_stack_HL 2'b10
65 `define INSN_alu_ADD 3'b000
66 `define INSN_alu_ADC 3'b001
67 `define INSN_alu_SUB 3'b010
68 `define INSN_alu_SBC 3'b011
69 `define INSN_alu_AND 3'b100
70 `define INSN_alu_XOR 3'b101
71 `define INSN_alu_OR 3'b110
72 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
73 `define INSN_alu_RLCA 3'b000
74 `define INSN_alu_RRCA 3'b001
75 `define INSN_alu_RLA 3'b010
76 `define INSN_alu_RRA 3'b011
77 `define INSN_alu_DAA 3'b100
78 `define INSN_alu_CPL 3'b101
79 `define INSN_alu_SCF 3'b110
80 `define INSN_alu_CCF 3'b111
84 output reg [15:0] busaddress, /* BUS_* is latched on STATE_FETCH. */
86 output reg buswr, output reg busrd);
88 reg [1:0] state = 0; /* State within this bus cycle (see STATE_*). */
89 reg [2:0] cycle = 0; /* Cycle for instructions. */
91 reg [7:0] registers[11:0];
93 reg [15:0] address; /* Address for the next bus operation. */
95 reg [7:0] opcode; /* Opcode from the current machine cycle. */
97 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
98 reg rd = 1, wr = 0, newcycle = 1;
100 reg [7:0] tmp, tmp2; /* Generic temporary regs. */
103 assign busdata = buswr ? buswdata : 8'bzzzzzzzz;
128 always @(posedge clk)
132 busaddress <= {registers[`REG_PCH], registers[`REG_PCL]};
136 busaddress <= address;
142 state <= `STATE_DECODE;
151 if (rd) rdata <= busdata;
158 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
159 wdata <= 8'bxxxxxxxx;
160 state <= `STATE_EXECUTE;
162 `STATE_EXECUTE: begin
163 `define EXEC_INC_PC \
164 {registers[`REG_PCH], registers[`REG_PCL]} <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
165 `define EXEC_NEXTADDR_PCINC \
166 address <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
167 `define EXEC_NEWCYCLE \
168 newcycle <= 1; rd <= 1; wr <= 0
170 `INSN_LD_reg_imm8: begin
174 `EXEC_NEXTADDR_PCINC;
179 if (opcode[5:3] == `INSN_reg_dHL) begin
180 address <= {registers[`REG_H], registers[`REG_L]};
195 /* XXX Interrupts needed for HALT. */
197 `INSN_LD_HL_reg: begin
201 `INSN_reg_A: wdata <= registers[`REG_A];
202 `INSN_reg_B: wdata <= registers[`REG_B];
203 `INSN_reg_C: wdata <= registers[`REG_C];
204 `INSN_reg_D: wdata <= registers[`REG_D];
205 `INSN_reg_E: wdata <= registers[`REG_E];
206 `INSN_reg_H: wdata <= registers[`REG_H];
207 `INSN_reg_L: wdata <= registers[`REG_L];
209 address <= {registers[`REG_H], registers[`REG_L]};
218 `INSN_LD_reg_HL: begin
221 address <= {registers[`REG_H], registers[`REG_L]};
231 `INSN_LD_reg_reg: begin
235 `INSN_reg_A: tmp <= registers[`REG_A];
236 `INSN_reg_B: tmp <= registers[`REG_B];
237 `INSN_reg_C: tmp <= registers[`REG_C];
238 `INSN_reg_D: tmp <= registers[`REG_D];
239 `INSN_reg_E: tmp <= registers[`REG_E];
240 `INSN_reg_H: tmp <= registers[`REG_H];
241 `INSN_reg_L: tmp <= registers[`REG_L];
244 `INSN_LD_reg_imm16: begin
248 `EXEC_NEXTADDR_PCINC;
252 `EXEC_NEXTADDR_PCINC;
255 2: begin `EXEC_NEWCYCLE; end
258 `INSN_LD_SP_HL: begin
261 tmp <= registers[`REG_H];
266 tmp <= registers[`REG_L];
270 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
274 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
276 `INSN_stack_AF: wdata <= registers[`REG_A];
277 `INSN_stack_BC: wdata <= registers[`REG_B];
278 `INSN_stack_DE: wdata <= registers[`REG_D];
279 `INSN_stack_HL: wdata <= registers[`REG_H];
284 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
286 `INSN_stack_AF: wdata <= registers[`REG_F];
287 `INSN_stack_BC: wdata <= registers[`REG_C];
288 `INSN_stack_DE: wdata <= registers[`REG_E];
289 `INSN_stack_HL: wdata <= registers[`REG_L];
292 2: begin /* TWIDDLE OUR FUCKING THUMBS! */ end
299 `INSN_POP_reg: begin /* POP is 12 cycles! */
303 address <= {registers[`REG_SPH],registers[`REG_SPL]};
307 address <= {registers[`REG_SPH],registers[`REG_SPL]};
318 address <= {8'hFF,registers[`REG_C]};
319 if (opcode[4]) begin // LD A,(C)
323 wdata <= registers[`REG_A];
335 address <= {registers[`REG_H],registers[`REG_L]};
336 if (opcode[3]) begin // LDx A, (HL)
340 wdata <= registers[`REG_A];
350 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
351 // fffffffff fuck your shit, read from (HL) :(
353 address <= {registers[`REG_H], registers[`REG_L]};
358 `INSN_reg_A: tmp <= registers[`REG_A];
359 `INSN_reg_B: tmp <= registers[`REG_B];
360 `INSN_reg_C: tmp <= registers[`REG_C];
361 `INSN_reg_D: tmp <= registers[`REG_D];
362 `INSN_reg_E: tmp <= registers[`REG_E];
363 `INSN_reg_H: tmp <= registers[`REG_H];
364 `INSN_reg_L: tmp <= registers[`REG_L];
365 `INSN_reg_dHL: tmp <= rdata;
380 `EXEC_INC_PC; // This goes FIRST in RST
384 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
385 wdata <= registers[`REG_PCH];
389 address <= {registers[`REG_SPH],registers[`REG_SPL]}-2;
390 wdata <= registers[`REG_PCL];
394 {registers[`REG_PCH],registers[`REG_PCL]} <=
395 {10'b0,opcode[5:3],3'b0};
403 address <= {registers[`REG_SPH],registers[`REG_SPL]};
407 address <= {registers[`REG_SPH],registers[`REG_SPL]} + 1;
409 2: begin /* twiddle thumbs */ end
412 // do NOT increment PC!
420 `EXEC_NEXTADDR_PCINC;
425 `EXEC_NEXTADDR_PCINC;
432 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 1;
433 wdata <= registers[`REG_PCH];
437 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 2;
438 wdata <= registers[`REG_PCL];
442 `EXEC_NEWCYCLE; /* do NOT increment the PC */
446 `INSN_JP_imm,`INSN_JPCC_imm: begin
450 `EXEC_NEXTADDR_PCINC;
455 `EXEC_NEXTADDR_PCINC;
460 if (!opcode[0]) begin // i.e., JP cc,nn
461 /* We need to check the condition code to bail out. */
463 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
464 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
465 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
466 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
478 state <= `STATE_WRITEBACK;
480 `STATE_WRITEBACK: begin
485 1: case (opcode[5:3])
486 `INSN_reg_A: begin registers[`REG_A] <= rdata; end
487 `INSN_reg_B: begin registers[`REG_B] <= rdata; end
488 `INSN_reg_C: begin registers[`REG_C] <= rdata; end
489 `INSN_reg_D: begin registers[`REG_D] <= rdata; end
490 `INSN_reg_E: begin registers[`REG_E] <= rdata; end
491 `INSN_reg_H: begin registers[`REG_H] <= rdata; end
492 `INSN_reg_L: begin registers[`REG_L] <= rdata; end
493 `INSN_reg_dHL: begin /* Go off to cycle 2 */ end
498 /* Nothing needs happen here. */
499 /* XXX Interrupts needed for HALT. */
501 `INSN_LD_HL_reg: begin
502 /* Nothing of interest here */
504 `INSN_LD_reg_HL: begin
509 `INSN_reg_A: registers[`REG_A] <= tmp;
510 `INSN_reg_B: registers[`REG_B] <= tmp;
511 `INSN_reg_C: registers[`REG_C] <= tmp;
512 `INSN_reg_D: registers[`REG_D] <= tmp;
513 `INSN_reg_E: registers[`REG_E] <= tmp;
514 `INSN_reg_H: registers[`REG_H] <= tmp;
515 `INSN_reg_L: registers[`REG_L] <= tmp;
520 `INSN_LD_reg_reg: begin
522 `INSN_reg_A: registers[`REG_A] <= tmp;
523 `INSN_reg_B: registers[`REG_B] <= tmp;
524 `INSN_reg_C: registers[`REG_C] <= tmp;
525 `INSN_reg_D: registers[`REG_D] <= tmp;
526 `INSN_reg_E: registers[`REG_E] <= tmp;
527 `INSN_reg_H: registers[`REG_H] <= tmp;
528 `INSN_reg_L: registers[`REG_L] <= tmp;
531 `INSN_LD_reg_imm16: begin
536 `INSN_reg16_BC: registers[`REG_C] <= rdata;
537 `INSN_reg16_DE: registers[`REG_E] <= rdata;
538 `INSN_reg16_HL: registers[`REG_L] <= rdata;
539 `INSN_reg16_SP: registers[`REG_SPL] <= rdata;
544 `INSN_reg16_BC: registers[`REG_B] <= rdata;
545 `INSN_reg16_DE: registers[`REG_D] <= rdata;
546 `INSN_reg16_HL: registers[`REG_H] <= rdata;
547 `INSN_reg16_SP: registers[`REG_SPH] <= rdata;
552 `INSN_LD_SP_HL: begin
554 0: registers[`REG_SPH] <= tmp;
555 1: registers[`REG_SPL] <= tmp;
558 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
560 0: {registers[`REG_SPH],registers[`REG_SPL]} <=
561 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
562 1: {registers[`REG_SPH],registers[`REG_SPL]} <=
563 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
564 2: begin /* type F */ end
565 3: begin /* type F */ end
568 `INSN_POP_reg: begin /* POP is 12 cycles! */
570 0: {registers[`REG_SPH],registers[`REG_SPL]} <=
571 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
574 `INSN_stack_AF: registers[`REG_F] <= rdata;
575 `INSN_stack_BC: registers[`REG_C] <= rdata;
576 `INSN_stack_DE: registers[`REG_E] <= rdata;
577 `INSN_stack_HL: registers[`REG_L] <= rdata;
579 {registers[`REG_SPH],registers[`REG_SPL]} <=
580 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
584 `INSN_stack_AF: registers[`REG_A] <= rdata;
585 `INSN_stack_BC: registers[`REG_B] <= rdata;
586 `INSN_stack_DE: registers[`REG_D] <= rdata;
587 `INSN_stack_HL: registers[`REG_H] <= rdata;
594 0: begin /* Type F */ end
596 registers[`REG_A] <= rdata;
601 0: begin /* Type F */ end
604 registers[`REG_A] <= rdata;
605 {registers[`REG_H],registers[`REG_L]} <=
606 opcode[4] ? // if set, LDD, else LDI
607 ({registers[`REG_H],registers[`REG_L]} - 1) :
608 ({registers[`REG_H],registers[`REG_L]} + 1);
613 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
614 /* Sit on our asses. */
615 end else begin /* Actually do the computation! */
619 registers[`REG_A] + tmp;
621 { /* Z */ ((registers[`REG_A] + tmp) == 0) ? 1'b1 : 1'b0,
623 /* H */ (({1'b0,registers[`REG_A][3:0]} + {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
624 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
625 registers[`REG_F][3:0]
630 registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]};
632 { /* Z */ ((registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]}) == 0) ? 1'b1 : 1'b0,
634 /* 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,
635 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp} + {8'b0,registers[`REG_F][4]}) >> 8 == 1) ? 1'b1 : 1'b0,
636 registers[`REG_F][3:0]
641 registers[`REG_A] - tmp;
643 { /* Z */ ((registers[`REG_A] - tmp) == 0) ? 1'b1 : 1'b0,
645 /* H */ (({1'b0,registers[`REG_A][3:0]} - {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
646 /* C */ (({1'b0,registers[`REG_A]} - {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
647 registers[`REG_F][3:0]
652 registers[`REG_A] - (tmp + {7'b0,registers[`REG_F][4]});
654 { /* Z */ ((registers[`REG_A] - (tmp + {7'b0,registers[`REG_F][4]})) == 0) ? 1'b1 : 1'b0,
656 /* 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,
657 /* C */ (({1'b0,registers[`REG_A]} - ({1'b0,tmp} + {8'b0,registers[`REG_F][4]})) >> 8 == 1) ? 1'b1 : 1'b0,
658 registers[`REG_F][3:0]
663 registers[`REG_A] & tmp;
665 { /* Z */ ((registers[`REG_A] & tmp) == 0) ? 1'b1 : 1'b0,
667 registers[`REG_F][3:0]
672 registers[`REG_A] | tmp;
674 { /* Z */ ((registers[`REG_A] | tmp) == 0) ? 1'b1 : 1'b0,
676 registers[`REG_F][3:0]
681 registers[`REG_A] ^ tmp;
683 { /* Z */ ((registers[`REG_A] ^ tmp) == 0) ? 1'b1 : 1'b0,
685 registers[`REG_F][3:0]
690 { /* Z */ ((registers[`REG_A] - tmp) == 0) ? 1'b1 : 1'b0,
692 /* H */ (({1'b0,registers[`REG_A][3:0]} - {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
693 /* C */ (({1'b0,registers[`REG_A]} - {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
694 registers[`REG_F][3:0]
704 `INSN_alu_RLCA: begin
705 registers[`REG_A] <= {registers[`REG_A][6:0],registers[`REG_A][7]};
706 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][7],registers[`REG_F][3:0]};
708 `INSN_alu_RRCA: begin
709 registers[`REG_A] <= {registers[`REG_A][0],registers[`REG_A][7:1]};
710 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][0],registers[`REG_F][3:0]};
713 registers[`REG_A] <= {registers[`REG_A][6:0],registers[`REG_F][4]};
714 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][7],registers[`REG_F][3:0]};
717 registers[`REG_A] <= {registers[`REG_A][4],registers[`REG_A][7:1]};
718 registers[`REG_F] <= {registers[`REG_F][7:5],registers[`REG_A][0],registers[`REG_F][3:0]};
721 registers[`REG_A] <= ~registers[`REG_A];
722 registers[`REG_F] <= {registers[`REG_F][7],1'b1,1'b1,registers[`REG_F][4:0]};
725 registers[`REG_F] <= {registers[`REG_F][7:5],1,registers[`REG_F][3:0]};
728 registers[`REG_F] <= {registers[`REG_F][7:5],~registers[`REG_F][4],registers[`REG_F][3:0]};
732 `INSN_NOP: begin /* NOP! */ end
735 0: begin /* type F */ end
736 1: begin /* type F */ end
737 2: begin /* type F */ end
738 3: {registers[`REG_SPH],registers[`REG_SPL]} <=
739 {registers[`REG_SPH],registers[`REG_SPL]}-2;
744 0: begin /* type F */ end
745 1: registers[`REG_PCL] <= rdata;
746 2: registers[`REG_PCH] <= rdata;
748 {registers[`REG_SPH],registers[`REG_SPL]} <=
749 {registers[`REG_SPH],registers[`REG_SPL]} + 2;
750 if (opcode[4]) /* RETI */
757 0: begin /* type F */ end
758 1: tmp <= rdata; // tmp contains newpcl
759 2: tmp2 <= rdata; // tmp2 contains newpch
760 3: begin /* type F */ end
761 4: registers[`REG_PCH] <= tmp2;
763 {registers[`REG_SPH],registers[`REG_SPL]} <=
764 {registers[`REG_SPH],registers[`REG_SPL]} - 2;
765 registers[`REG_PCL] <= tmp;
769 `INSN_JP_imm,`INSN_JPCC_imm: begin
771 0: begin /* type F */ end
772 1: tmp <= rdata; // tmp contains newpcl
773 2: tmp2 <= rdata; // tmp2 contains newpch
774 3: {registers[`REG_PCH],registers[`REG_PCL]} <=
781 state <= `STATE_FETCH;