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
43 `define INSN_cc_NZ 2'b00
44 `define INSN_cc_Z 2'b01
45 `define INSN_cc_NC 2'b10
46 `define INSN_cc_C 2'b11
48 `define INSN_reg_A 3'b111
49 `define INSN_reg_B 3'b000
50 `define INSN_reg_C 3'b001
51 `define INSN_reg_D 3'b010
52 `define INSN_reg_E 3'b011
53 `define INSN_reg_H 3'b100
54 `define INSN_reg_L 3'b101
55 `define INSN_reg_dHL 3'b110
56 `define INSN_reg16_BC 2'b00
57 `define INSN_reg16_DE 2'b01
58 `define INSN_reg16_HL 2'b10
59 `define INSN_reg16_SP 2'b11
60 `define INSN_stack_AF 2'b11
61 `define INSN_stack_BC 2'b00
62 `define INSN_stack_DE 2'b01
63 `define INSN_stack_HL 2'b10
64 `define INSN_alu_ADD 3'b000
65 `define INSN_alu_ADC 3'b001
66 `define INSN_alu_SUB 3'b010
67 `define INSN_alu_SBC 3'b011
68 `define INSN_alu_AND 3'b100
69 `define INSN_alu_XOR 3'b101
70 `define INSN_alu_OR 3'b110
71 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
75 output reg [15:0] busaddress, /* BUS_* is latched on STATE_FETCH. */
77 output reg buswr, output reg busrd);
79 reg [1:0] state = 0; /* State within this bus cycle (see STATE_*). */
80 reg [2:0] cycle = 0; /* Cycle for instructions. */
82 reg [7:0] registers[11:0];
84 reg [15:0] address; /* Address for the next bus operation. */
86 reg [7:0] opcode; /* Opcode from the current machine cycle. */
88 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
89 reg rd = 1, wr = 0, newcycle = 1;
91 reg [7:0] tmp, tmp2; /* Generic temporary regs. */
94 assign busdata = buswr ? buswdata : 8'bzzzzzzzz;
119 always @(posedge clk)
123 busaddress <= {registers[`REG_PCH], registers[`REG_PCL]};
127 busaddress <= address;
133 state <= `STATE_DECODE;
142 if (rd) rdata <= busdata;
149 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
150 wdata <= 8'bxxxxxxxx;
151 state <= `STATE_EXECUTE;
153 `STATE_EXECUTE: begin
154 `define EXEC_INC_PC \
155 {registers[`REG_PCH], registers[`REG_PCL]} <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
156 `define EXEC_NEXTADDR_PCINC \
157 address <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
158 `define EXEC_NEWCYCLE \
159 newcycle <= 1; rd <= 1; wr <= 0
161 `INSN_LD_reg_imm8: begin
165 `EXEC_NEXTADDR_PCINC;
170 if (opcode[5:3] == `INSN_reg_dHL) begin
171 address <= {registers[`REG_H], registers[`REG_L]};
186 /* XXX Interrupts needed for HALT. */
188 `INSN_LD_HL_reg: begin
192 `INSN_reg_A: wdata <= registers[`REG_A];
193 `INSN_reg_B: wdata <= registers[`REG_B];
194 `INSN_reg_C: wdata <= registers[`REG_C];
195 `INSN_reg_D: wdata <= registers[`REG_D];
196 `INSN_reg_E: wdata <= registers[`REG_E];
197 `INSN_reg_H: wdata <= registers[`REG_H];
198 `INSN_reg_L: wdata <= registers[`REG_L];
200 address <= {registers[`REG_H], registers[`REG_L]};
209 `INSN_LD_reg_HL: begin
212 address <= {registers[`REG_H], registers[`REG_L]};
222 `INSN_LD_reg_reg: begin
226 `INSN_reg_A: tmp <= registers[`REG_A];
227 `INSN_reg_B: tmp <= registers[`REG_B];
228 `INSN_reg_C: tmp <= registers[`REG_C];
229 `INSN_reg_D: tmp <= registers[`REG_D];
230 `INSN_reg_E: tmp <= registers[`REG_E];
231 `INSN_reg_H: tmp <= registers[`REG_H];
232 `INSN_reg_L: tmp <= registers[`REG_L];
235 `INSN_LD_reg_imm16: begin
239 `EXEC_NEXTADDR_PCINC;
243 `EXEC_NEXTADDR_PCINC;
246 2: begin `EXEC_NEWCYCLE; end
249 `INSN_LD_SP_HL: begin
252 tmp <= registers[`REG_H];
257 tmp <= registers[`REG_L];
261 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
265 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
267 `INSN_stack_AF: wdata <= registers[`REG_A];
268 `INSN_stack_BC: wdata <= registers[`REG_B];
269 `INSN_stack_DE: wdata <= registers[`REG_D];
270 `INSN_stack_HL: wdata <= registers[`REG_H];
275 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
277 `INSN_stack_AF: wdata <= registers[`REG_F];
278 `INSN_stack_BC: wdata <= registers[`REG_C];
279 `INSN_stack_DE: wdata <= registers[`REG_E];
280 `INSN_stack_HL: wdata <= registers[`REG_L];
283 2: begin /* TWIDDLE OUR FUCKING THUMBS! */ end
290 `INSN_POP_reg: begin /* POP is 12 cycles! */
294 address <= {registers[`REG_SPH],registers[`REG_SPL]};
298 address <= {registers[`REG_SPH],registers[`REG_SPL]};
309 address <= {8'hFF,registers[`REG_C]};
310 if (opcode[4]) begin // LD A,(C)
314 wdata <= registers[`REG_A];
326 address <= {registers[`REG_H],registers[`REG_L]};
327 if (opcode[3]) begin // LDx A, (HL)
331 wdata <= registers[`REG_A];
341 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
342 // fffffffff fuck your shit, read from (HL) :(
344 address <= {registers[`REG_H], registers[`REG_L]};
349 `INSN_reg_A: tmp <= registers[`REG_A];
350 `INSN_reg_B: tmp <= registers[`REG_B];
351 `INSN_reg_C: tmp <= registers[`REG_C];
352 `INSN_reg_D: tmp <= registers[`REG_D];
353 `INSN_reg_E: tmp <= registers[`REG_E];
354 `INSN_reg_H: tmp <= registers[`REG_H];
355 `INSN_reg_L: tmp <= registers[`REG_L];
356 `INSN_reg_dHL: tmp <= rdata;
367 `EXEC_INC_PC; // This goes FIRST in RST
371 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
372 wdata <= registers[`REG_PCH];
376 address <= {registers[`REG_SPH],registers[`REG_SPL]}-2;
377 wdata <= registers[`REG_PCL];
381 {registers[`REG_PCH],registers[`REG_PCL]} <=
382 {10'b0,opcode[5:3],3'b0};
390 address <= {registers[`REG_SPH],registers[`REG_SPL]};
394 address <= {registers[`REG_SPH],registers[`REG_SPL]} + 1;
396 2: begin /* twiddle thumbs */ end
399 // do NOT increment PC!
407 `EXEC_NEXTADDR_PCINC;
412 `EXEC_NEXTADDR_PCINC;
419 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 1;
420 wdata <= registers[`REG_PCH];
424 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 2;
425 wdata <= registers[`REG_PCL];
429 `EXEC_NEWCYCLE; /* do NOT increment the PC */
433 `INSN_JP_imm,`INSN_JPCC_imm: begin
437 `EXEC_NEXTADDR_PCINC;
442 `EXEC_NEXTADDR_PCINC;
446 if (!opcode[0]) begin // i.e., JP cc,nn
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
464 state <= `STATE_WRITEBACK;
466 `STATE_WRITEBACK: begin
471 1: case (opcode[5:3])
472 `INSN_reg_A: begin registers[`REG_A] <= rdata; end
473 `INSN_reg_B: begin registers[`REG_B] <= rdata; end
474 `INSN_reg_C: begin registers[`REG_C] <= rdata; end
475 `INSN_reg_D: begin registers[`REG_D] <= rdata; end
476 `INSN_reg_E: begin registers[`REG_E] <= rdata; end
477 `INSN_reg_H: begin registers[`REG_H] <= rdata; end
478 `INSN_reg_L: begin registers[`REG_L] <= rdata; end
479 `INSN_reg_dHL: begin /* Go off to cycle 2 */ end
484 /* Nothing needs happen here. */
485 /* XXX Interrupts needed for HALT. */
487 `INSN_LD_HL_reg: begin
488 /* Nothing of interest here */
490 `INSN_LD_reg_HL: begin
495 `INSN_reg_A: registers[`REG_A] <= tmp;
496 `INSN_reg_B: registers[`REG_B] <= tmp;
497 `INSN_reg_C: registers[`REG_C] <= tmp;
498 `INSN_reg_D: registers[`REG_D] <= tmp;
499 `INSN_reg_E: registers[`REG_E] <= tmp;
500 `INSN_reg_H: registers[`REG_H] <= tmp;
501 `INSN_reg_L: registers[`REG_L] <= tmp;
506 `INSN_LD_reg_reg: begin
508 `INSN_reg_A: registers[`REG_A] <= tmp;
509 `INSN_reg_B: registers[`REG_B] <= tmp;
510 `INSN_reg_C: registers[`REG_C] <= tmp;
511 `INSN_reg_D: registers[`REG_D] <= tmp;
512 `INSN_reg_E: registers[`REG_E] <= tmp;
513 `INSN_reg_H: registers[`REG_H] <= tmp;
514 `INSN_reg_L: registers[`REG_L] <= tmp;
517 `INSN_LD_reg_imm16: begin
522 `INSN_reg16_BC: registers[`REG_C] <= rdata;
523 `INSN_reg16_DE: registers[`REG_E] <= rdata;
524 `INSN_reg16_HL: registers[`REG_L] <= rdata;
525 `INSN_reg16_SP: registers[`REG_SPL] <= rdata;
530 `INSN_reg16_BC: registers[`REG_B] <= rdata;
531 `INSN_reg16_DE: registers[`REG_D] <= rdata;
532 `INSN_reg16_HL: registers[`REG_H] <= rdata;
533 `INSN_reg16_SP: registers[`REG_SPH] <= rdata;
538 `INSN_LD_SP_HL: begin
540 0: registers[`REG_SPH] <= tmp;
541 1: registers[`REG_SPL] <= tmp;
544 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
546 0: {registers[`REG_SPH],registers[`REG_SPL]} <=
547 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
548 1: {registers[`REG_SPH],registers[`REG_SPL]} <=
549 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
550 2: begin /* type F */ end
551 3: begin /* type F */ end
554 `INSN_POP_reg: begin /* POP is 12 cycles! */
556 0: {registers[`REG_SPH],registers[`REG_SPL]} <=
557 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
560 `INSN_stack_AF: registers[`REG_F] <= rdata;
561 `INSN_stack_BC: registers[`REG_C] <= rdata;
562 `INSN_stack_DE: registers[`REG_E] <= rdata;
563 `INSN_stack_HL: registers[`REG_L] <= rdata;
565 {registers[`REG_SPH],registers[`REG_SPL]} <=
566 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
570 `INSN_stack_AF: registers[`REG_A] <= rdata;
571 `INSN_stack_BC: registers[`REG_B] <= rdata;
572 `INSN_stack_DE: registers[`REG_D] <= rdata;
573 `INSN_stack_HL: registers[`REG_H] <= rdata;
580 0: begin /* Type F */ end
582 registers[`REG_A] <= rdata;
587 0: begin /* Type F */ end
590 registers[`REG_A] <= rdata;
591 {registers[`REG_H],registers[`REG_L]} <=
592 opcode[4] ? // if set, LDD, else LDI
593 ({registers[`REG_H],registers[`REG_L]} - 1) :
594 ({registers[`REG_H],registers[`REG_L]} + 1);
599 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
600 /* Sit on our asses. */
601 end else begin /* Actually do the computation! */
605 registers[`REG_A] + tmp;
607 { /* Z */ ((registers[`REG_A] + tmp) == 0) ? 1'b1 : 1'b0,
609 /* H */ (({1'b0,registers[`REG_A][3:0]} + {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
610 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
611 registers[`REG_F][3:0]
616 registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]};
618 { /* Z */ ((registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]}) == 0) ? 1'b1 : 1'b0,
620 /* 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,
621 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp} + {8'b0,registers[`REG_F][4]}) >> 8 == 1) ? 1'b1 : 1'b0,
622 registers[`REG_F][3:0]
627 registers[`REG_A] & tmp;
629 { /* Z */ ((registers[`REG_A] & tmp) == 0) ? 1'b1 : 1'b0,
631 registers[`REG_F][3:0]
636 registers[`REG_A] | tmp;
638 { /* Z */ ((registers[`REG_A] | tmp) == 0) ? 1'b1 : 1'b0,
640 registers[`REG_F][3:0]
645 registers[`REG_A] ^ tmp;
647 { /* Z */ ((registers[`REG_A] ^ tmp) == 0) ? 1'b1 : 1'b0,
649 registers[`REG_F][3:0]
657 `INSN_NOP: begin /* NOP! */ end
660 0: begin /* type F */ end
661 1: begin /* type F */ end
662 2: begin /* type F */ end
663 3: {registers[`REG_SPH],registers[`REG_SPL]} <=
664 {registers[`REG_SPH],registers[`REG_SPL]}-2;
669 0: begin /* type F */ end
670 1: registers[`REG_PCL] <= rdata;
671 2: registers[`REG_PCH] <= rdata;
673 {registers[`REG_SPH],registers[`REG_SPL]} <=
674 {registers[`REG_SPH],registers[`REG_SPL]} + 2;
675 if (opcode[4]) /* RETI */
682 0: begin /* type F */ end
683 1: tmp <= rdata; // tmp contains newpcl
684 2: tmp2 <= rdata; // tmp2 contains newpch
685 3: begin /* type F */ end
686 4: registers[`REG_PCH] <= tmp2;
688 {registers[`REG_SPH],registers[`REG_SPL]} <=
689 {registers[`REG_SPH],registers[`REG_SPL]} - 2;
690 registers[`REG_PCL] <= tmp;
694 `INSN_JP_imm,`INSN_JPCC_imm: begin
696 0: begin /* type F */ end
697 1: tmp <= rdata; // tmp contains newpcl
698 2: tmp2 <= rdata; // tmp2 contains newpch
699 3: {registers[`REG_PCH],registers[`REG_PCL]} <=
706 state <= `STATE_FETCH;