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
41 `define INSN_reg_A 3'b111
42 `define INSN_reg_B 3'b000
43 `define INSN_reg_C 3'b001
44 `define INSN_reg_D 3'b010
45 `define INSN_reg_E 3'b011
46 `define INSN_reg_H 3'b100
47 `define INSN_reg_L 3'b101
48 `define INSN_reg_dHL 3'b110
49 `define INSN_reg16_BC 2'b00
50 `define INSN_reg16_DE 2'b01
51 `define INSN_reg16_HL 2'b10
52 `define INSN_reg16_SP 2'b11
53 `define INSN_stack_AF 2'b11
54 `define INSN_stack_BC 2'b00
55 `define INSN_stack_DE 2'b01
56 `define INSN_stack_HL 2'b10
57 `define INSN_alu_ADD 3'b000
58 `define INSN_alu_ADC 3'b001
59 `define INSN_alu_SUB 3'b010
60 `define INSN_alu_SBC 3'b011
61 `define INSN_alu_AND 3'b100
62 `define INSN_alu_XOR 3'b101
63 `define INSN_alu_OR 3'b110
64 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
68 output reg [15:0] busaddress, /* BUS_* is latched on STATE_FETCH. */
70 output reg buswr, output reg busrd);
72 reg [1:0] state = 0; /* State within this bus cycle (see STATE_*). */
73 reg [2:0] cycle = 0; /* Cycle for instructions. */
75 reg [7:0] registers[11:0];
77 reg [15:0] address; /* Address for the next bus operation. */
79 reg [7:0] opcode; /* Opcode from the current machine cycle. */
81 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
82 reg rd = 1, wr = 0, newcycle = 1;
84 reg [7:0] tmp, tmp2; /* Generic temporary regs. */
87 assign busdata = buswr ? buswdata : 8'bzzzzzzzz;
106 always @(posedge clk)
112 busaddress <= {registers[`REG_PCH], registers[`REG_PCL]};
114 busaddress <= address;
117 state <= `STATE_DECODE;
126 if (rd) rdata <= busdata;
131 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
132 wdata <= 8'bxxxxxxxx;
133 state <= `STATE_EXECUTE;
135 `STATE_EXECUTE: begin
136 `define EXEC_INC_PC \
137 {registers[`REG_PCH], registers[`REG_PCL]} <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
138 `define EXEC_NEXTADDR_PCINC \
139 address <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
140 `define EXEC_NEWCYCLE \
141 newcycle <= 1; rd <= 1; wr <= 0
143 `INSN_LD_reg_imm8: begin
147 `EXEC_NEXTADDR_PCINC;
152 if (opcode[5:3] == `INSN_reg_dHL) begin
153 address <= {registers[`REG_H], registers[`REG_L]};
168 /* XXX Interrupts needed for HALT. */
170 `INSN_LD_HL_reg: begin
174 `INSN_reg_A: wdata <= registers[`REG_A];
175 `INSN_reg_B: wdata <= registers[`REG_B];
176 `INSN_reg_C: wdata <= registers[`REG_C];
177 `INSN_reg_D: wdata <= registers[`REG_D];
178 `INSN_reg_E: wdata <= registers[`REG_E];
179 `INSN_reg_H: wdata <= registers[`REG_H];
180 `INSN_reg_L: wdata <= registers[`REG_L];
182 address <= {registers[`REG_H], registers[`REG_L]};
191 `INSN_LD_reg_HL: begin
194 address <= {registers[`REG_H], registers[`REG_L]};
204 `INSN_LD_reg_reg: begin
208 `INSN_reg_A: tmp <= registers[`REG_A];
209 `INSN_reg_B: tmp <= registers[`REG_B];
210 `INSN_reg_C: tmp <= registers[`REG_C];
211 `INSN_reg_D: tmp <= registers[`REG_D];
212 `INSN_reg_E: tmp <= registers[`REG_E];
213 `INSN_reg_H: tmp <= registers[`REG_H];
214 `INSN_reg_L: tmp <= registers[`REG_L];
217 `INSN_LD_reg_imm16: begin
221 `EXEC_NEXTADDR_PCINC;
225 `EXEC_NEXTADDR_PCINC;
228 2: begin `EXEC_NEWCYCLE; end
231 `INSN_LD_SP_HL: begin
234 tmp <= registers[`REG_H];
239 tmp <= registers[`REG_L];
243 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
247 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
249 `INSN_stack_AF: wdata <= registers[`REG_A];
250 `INSN_stack_BC: wdata <= registers[`REG_B];
251 `INSN_stack_DE: wdata <= registers[`REG_D];
252 `INSN_stack_HL: wdata <= registers[`REG_H];
257 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
259 `INSN_stack_AF: wdata <= registers[`REG_F];
260 `INSN_stack_BC: wdata <= registers[`REG_C];
261 `INSN_stack_DE: wdata <= registers[`REG_E];
262 `INSN_stack_HL: wdata <= registers[`REG_L];
265 2: begin /* TWIDDLE OUR FUCKING THUMBS! */ end
272 `INSN_POP_reg: begin /* POP is 12 cycles! */
276 address <= {registers[`REG_SPH],registers[`REG_SPL]};
280 address <= {registers[`REG_SPH],registers[`REG_SPL]};
291 address <= {8'hFF,registers[`REG_C]};
292 if (opcode[4]) begin // LD A,(C)
296 wdata <= registers[`REG_A];
308 address <= {registers[`REG_H],registers[`REG_L]};
309 if (opcode[3]) begin // LDx A, (HL)
313 wdata <= registers[`REG_A];
323 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
324 // fffffffff fuck your shit, read from (HL) :(
326 address <= {registers[`REG_H], registers[`REG_L]};
331 `INSN_reg_A: tmp <= registers[`REG_A];
332 `INSN_reg_B: tmp <= registers[`REG_B];
333 `INSN_reg_C: tmp <= registers[`REG_C];
334 `INSN_reg_D: tmp <= registers[`REG_D];
335 `INSN_reg_E: tmp <= registers[`REG_E];
336 `INSN_reg_H: tmp <= registers[`REG_H];
337 `INSN_reg_L: tmp <= registers[`REG_L];
338 `INSN_reg_dHL: tmp <= rdata;
349 `EXEC_INC_PC; // This goes FIRST in RST
353 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
354 wdata <= registers[`REG_PCH];
358 address <= {registers[`REG_SPH],registers[`REG_SPL]}-2;
359 wdata <= registers[`REG_PCL];
363 {registers[`REG_PCH],registers[`REG_PCL]} <=
364 {10'b0,opcode[5:3],3'b0};
372 address <= {registers[`REG_SPH],registers[`REG_SPL]};
376 address <= {registers[`REG_SPH],registers[`REG_SPL]} + 1;
378 2: begin /* twiddle thumbs */ end
381 // do NOT increment PC!
389 `EXEC_NEXTADDR_PCINC;
394 `EXEC_NEXTADDR_PCINC;
401 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 1;
402 wdata <= registers[`REG_PCH];
406 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 2;
407 wdata <= registers[`REG_PCL];
411 `EXEC_NEWCYCLE; /* do NOT increment the PC */
418 state <= `STATE_WRITEBACK;
420 `STATE_WRITEBACK: begin
425 1: case (opcode[5:3])
426 `INSN_reg_A: begin registers[`REG_A] <= rdata; cycle <= 0; end
427 `INSN_reg_B: begin registers[`REG_B] <= rdata; cycle <= 0; end
428 `INSN_reg_C: begin registers[`REG_C] <= rdata; cycle <= 0; end
429 `INSN_reg_D: begin registers[`REG_D] <= rdata; cycle <= 0; end
430 `INSN_reg_E: begin registers[`REG_E] <= rdata; cycle <= 0; end
431 `INSN_reg_H: begin registers[`REG_H] <= rdata; cycle <= 0; end
432 `INSN_reg_L: begin registers[`REG_L] <= rdata; cycle <= 0; end
433 `INSN_reg_dHL: cycle <= 2;
438 /* Nothing needs happen here. */
439 /* XXX Interrupts needed for HALT. */
441 `INSN_LD_HL_reg: begin
447 `INSN_LD_reg_HL: begin
452 `INSN_reg_A: registers[`REG_A] <= tmp;
453 `INSN_reg_B: registers[`REG_B] <= tmp;
454 `INSN_reg_C: registers[`REG_C] <= tmp;
455 `INSN_reg_D: registers[`REG_D] <= tmp;
456 `INSN_reg_E: registers[`REG_E] <= tmp;
457 `INSN_reg_H: registers[`REG_H] <= tmp;
458 `INSN_reg_L: registers[`REG_L] <= tmp;
464 `INSN_LD_reg_reg: begin
466 `INSN_reg_A: registers[`REG_A] <= tmp;
467 `INSN_reg_B: registers[`REG_B] <= tmp;
468 `INSN_reg_C: registers[`REG_C] <= tmp;
469 `INSN_reg_D: registers[`REG_D] <= tmp;
470 `INSN_reg_E: registers[`REG_E] <= tmp;
471 `INSN_reg_H: registers[`REG_H] <= tmp;
472 `INSN_reg_L: registers[`REG_L] <= tmp;
475 `INSN_LD_reg_imm16: begin
480 `INSN_reg16_BC: registers[`REG_C] <= rdata;
481 `INSN_reg16_DE: registers[`REG_E] <= rdata;
482 `INSN_reg16_HL: registers[`REG_L] <= rdata;
483 `INSN_reg16_SP: registers[`REG_SPL] <= rdata;
489 `INSN_reg16_BC: registers[`REG_B] <= rdata;
490 `INSN_reg16_DE: registers[`REG_D] <= rdata;
491 `INSN_reg16_HL: registers[`REG_H] <= rdata;
492 `INSN_reg16_SP: registers[`REG_SPH] <= rdata;
498 `INSN_LD_SP_HL: begin
502 registers[`REG_SPH] <= tmp;
506 registers[`REG_SPL] <= tmp;
510 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
513 {registers[`REG_SPH],registers[`REG_SPL]} <=
514 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
518 {registers[`REG_SPH],registers[`REG_SPL]} <=
519 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
526 `INSN_POP_reg: begin /* POP is 12 cycles! */
530 {registers[`REG_SPH],registers[`REG_SPL]} <=
531 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
535 `INSN_stack_AF: registers[`REG_F] <= rdata;
536 `INSN_stack_BC: registers[`REG_C] <= rdata;
537 `INSN_stack_DE: registers[`REG_E] <= rdata;
538 `INSN_stack_HL: registers[`REG_L] <= rdata;
540 {registers[`REG_SPH],registers[`REG_SPL]} <=
541 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
546 `INSN_stack_AF: registers[`REG_A] <= rdata;
547 `INSN_stack_BC: registers[`REG_B] <= rdata;
548 `INSN_stack_DE: registers[`REG_D] <= rdata;
549 `INSN_stack_HL: registers[`REG_H] <= rdata;
561 registers[`REG_A] <= rdata;
571 registers[`REG_A] <= rdata;
572 {registers[`REG_H],registers[`REG_L]} <=
573 opcode[4] ? // if set, LDD, else LDI
574 ({registers[`REG_H],registers[`REG_L]} - 1) :
575 ({registers[`REG_H],registers[`REG_L]} + 1);
580 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
581 /* Sit on our asses. */
583 end else begin /* Actually do the computation! */
587 registers[`REG_A] + tmp;
589 { /* Z */ ((registers[`REG_A] + tmp) == 0) ? 1'b1 : 1'b0,
591 /* H */ (({1'b0,registers[`REG_A][3:0]} + {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
592 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
593 registers[`REG_F][3:0]
598 registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]};
600 { /* Z */ ((registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]}) == 0) ? 1'b1 : 1'b0,
602 /* 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,
603 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp} + {8'b0,registers[`REG_F][4]}) >> 8 == 1) ? 1'b1 : 1'b0,
604 registers[`REG_F][3:0]
609 registers[`REG_A] & tmp;
611 { /* Z */ ((registers[`REG_A] & tmp) == 0) ? 1'b1 : 1'b0,
613 registers[`REG_F][3:0]
618 registers[`REG_A] | tmp;
620 { /* Z */ ((registers[`REG_A] | tmp) == 0) ? 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]
639 `INSN_NOP: begin /* NOP! */ end
647 {registers[`REG_SPH],registers[`REG_SPL]} <=
648 {registers[`REG_SPH],registers[`REG_SPL]}-2;
657 registers[`REG_PCL] <= rdata;
661 registers[`REG_PCH] <= rdata;
665 {registers[`REG_SPH],registers[`REG_SPL]} <=
666 {registers[`REG_SPH],registers[`REG_SPL]} + 2;
667 if (opcode[4]) /* RETI */
677 tmp <= rdata; // tmp contains newpcl
681 tmp2 <= rdata; // tmp2 contains newpch
688 registers[`REG_PCH] <= tmp2;
691 {registers[`REG_SPH],registers[`REG_SPL]} <=
692 {registers[`REG_SPH],registers[`REG_SPL]} - 2;
693 registers[`REG_PCL] <= tmp;
701 state <= `STATE_FETCH;
708 input [15:0] address,
712 reg [7:0] rom [2047:0];
713 initial $readmemh("rom.hex", rom);
715 wire decode = address[15:13] == 0;
718 assign data = (rd && decode) ? odata : 8'bzzzzzzzz;
722 odata <= rom[address];
726 input [15:0] address,
731 reg [7:0] ram [8191:0];
733 wire decode = (address >= 16'hC000) && (address < 16'hFE00);
736 assign data = (rd && decode) ? odata : 8'bzzzzzzzz;
740 for (diq = 0; diq < 8191; diq = diq + 1)
743 always @(negedge clk)
746 odata <= ram[address[12:0]];
748 ram[address[12:0]] <= data;
758 always #10 clk <= ~clk;