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
40 `define INSN_reg_A 3'b111
41 `define INSN_reg_B 3'b000
42 `define INSN_reg_C 3'b001
43 `define INSN_reg_D 3'b010
44 `define INSN_reg_E 3'b011
45 `define INSN_reg_H 3'b100
46 `define INSN_reg_L 3'b101
47 `define INSN_reg_dHL 3'b110
48 `define INSN_reg16_BC 2'b00
49 `define INSN_reg16_DE 2'b01
50 `define INSN_reg16_HL 2'b10
51 `define INSN_reg16_SP 2'b11
52 `define INSN_stack_AF 2'b11
53 `define INSN_stack_BC 2'b00
54 `define INSN_stack_DE 2'b01
55 `define INSN_stack_HL 2'b10
56 `define INSN_alu_ADD 3'b000
57 `define INSN_alu_ADC 3'b001
58 `define INSN_alu_SUB 3'b010
59 `define INSN_alu_SBC 3'b011
60 `define INSN_alu_AND 3'b100
61 `define INSN_alu_XOR 3'b101
62 `define INSN_alu_OR 3'b110
63 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
67 output reg [15:0] busaddress, /* BUS_* is latched on STATE_FETCH. */
69 output reg buswr, output reg busrd);
71 reg [1:0] state = 0; /* State within this bus cycle (see STATE_*). */
72 reg [2:0] cycle = 0; /* Cycle for instructions. */
74 reg [7:0] registers[11:0];
76 reg [15:0] address; /* Address for the next bus operation. */
78 reg [7:0] opcode; /* Opcode from the current machine cycle. */
80 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
81 reg rd = 1, wr = 0, newcycle = 1;
83 reg [7:0] tmp; /* Generic temporary reg. */
86 assign busdata = buswr ? buswdata : 8'bzzzzzzzz;
105 always @(posedge clk)
111 busaddress <= {registers[`REG_PCH], registers[`REG_PCL]};
113 busaddress <= address;
116 state <= `STATE_DECODE;
125 if (rd) rdata <= busdata;
130 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
131 wdata <= 8'bxxxxxxxx;
132 state <= `STATE_EXECUTE;
134 `STATE_EXECUTE: begin
135 `define EXEC_INC_PC \
136 {registers[`REG_PCH], registers[`REG_PCL]} <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
137 `define EXEC_NEXTADDR_PCINC \
138 address <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
139 `define EXEC_NEWCYCLE \
140 newcycle <= 1; rd <= 1; wr <= 0
142 `INSN_LD_reg_imm8: begin
146 `EXEC_NEXTADDR_PCINC;
151 if (opcode[5:3] == `INSN_reg_dHL) begin
152 address <= {registers[`REG_H], registers[`REG_L]};
167 /* XXX Interrupts needed for HALT. */
169 `INSN_LD_HL_reg: begin
173 `INSN_reg_A: wdata <= registers[`REG_A];
174 `INSN_reg_B: wdata <= registers[`REG_B];
175 `INSN_reg_C: wdata <= registers[`REG_C];
176 `INSN_reg_D: wdata <= registers[`REG_D];
177 `INSN_reg_E: wdata <= registers[`REG_E];
178 `INSN_reg_H: wdata <= registers[`REG_H];
179 `INSN_reg_L: wdata <= registers[`REG_L];
181 address <= {registers[`REG_H], registers[`REG_L]};
190 `INSN_LD_reg_HL: begin
193 address <= {registers[`REG_H], registers[`REG_L]};
203 `INSN_LD_reg_reg: begin
207 `INSN_reg_A: tmp <= registers[`REG_A];
208 `INSN_reg_B: tmp <= registers[`REG_B];
209 `INSN_reg_C: tmp <= registers[`REG_C];
210 `INSN_reg_D: tmp <= registers[`REG_D];
211 `INSN_reg_E: tmp <= registers[`REG_E];
212 `INSN_reg_H: tmp <= registers[`REG_H];
213 `INSN_reg_L: tmp <= registers[`REG_L];
216 `INSN_LD_reg_imm16: begin
220 `EXEC_NEXTADDR_PCINC;
224 `EXEC_NEXTADDR_PCINC;
227 2: begin `EXEC_NEWCYCLE; end
230 `INSN_LD_SP_HL: begin
233 tmp <= registers[`REG_H];
238 tmp <= registers[`REG_L];
242 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
246 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
248 `INSN_stack_AF: wdata <= registers[`REG_A];
249 `INSN_stack_BC: wdata <= registers[`REG_B];
250 `INSN_stack_DE: wdata <= registers[`REG_D];
251 `INSN_stack_HL: wdata <= registers[`REG_H];
256 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
258 `INSN_stack_AF: wdata <= registers[`REG_F];
259 `INSN_stack_BC: wdata <= registers[`REG_C];
260 `INSN_stack_DE: wdata <= registers[`REG_E];
261 `INSN_stack_HL: wdata <= registers[`REG_L];
264 2: begin /* TWIDDLE OUR FUCKING THUMBS! */ end
271 `INSN_POP_reg: begin /* POP is 12 cycles! */
275 address <= {registers[`REG_SPH],registers[`REG_SPL]};
279 address <= {registers[`REG_SPH],registers[`REG_SPL]};
290 address <= {8'hFF,registers[`REG_C]};
291 if (opcode[4]) begin // LD A,(C)
295 wdata <= registers[`REG_A];
307 address <= {registers[`REG_H],registers[`REG_L]};
308 if (opcode[3]) begin // LDx A, (HL)
312 wdata <= registers[`REG_A];
322 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
323 // fffffffff fuck your shit, read from (HL) :(
325 address <= {registers[`REG_H], registers[`REG_L]};
330 `INSN_reg_A: tmp <= registers[`REG_A];
331 `INSN_reg_B: tmp <= registers[`REG_B];
332 `INSN_reg_C: tmp <= registers[`REG_C];
333 `INSN_reg_D: tmp <= registers[`REG_D];
334 `INSN_reg_E: tmp <= registers[`REG_E];
335 `INSN_reg_H: tmp <= registers[`REG_H];
336 `INSN_reg_L: tmp <= registers[`REG_L];
337 `INSN_reg_dHL: tmp <= rdata;
348 `EXEC_INC_PC; // This goes FIRST in RST
352 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
353 wdata <= registers[`REG_PCH];
357 address <= {registers[`REG_SPH],registers[`REG_SPL]}-2;
358 wdata <= registers[`REG_PCL];
362 {registers[`REG_PCH],registers[`REG_PCL]} <=
363 {10'b0,opcode[5:3],3'b0};
371 address <= {registers[`REG_SPH],registers[`REG_SPL]};
375 address <= {registers[`REG_SPH],registers[`REG_SPL]} + 1;
377 2: begin /* twiddle thumbs */ end
380 // do NOT increment PC!
387 state <= `STATE_WRITEBACK;
389 `STATE_WRITEBACK: begin
394 1: case (opcode[5:3])
395 `INSN_reg_A: begin registers[`REG_A] <= rdata; cycle <= 0; end
396 `INSN_reg_B: begin registers[`REG_B] <= rdata; cycle <= 0; end
397 `INSN_reg_C: begin registers[`REG_C] <= rdata; cycle <= 0; end
398 `INSN_reg_D: begin registers[`REG_D] <= rdata; cycle <= 0; end
399 `INSN_reg_E: begin registers[`REG_E] <= rdata; cycle <= 0; end
400 `INSN_reg_H: begin registers[`REG_H] <= rdata; cycle <= 0; end
401 `INSN_reg_L: begin registers[`REG_L] <= rdata; cycle <= 0; end
402 `INSN_reg_dHL: cycle <= 2;
407 /* Nothing needs happen here. */
408 /* XXX Interrupts needed for HALT. */
410 `INSN_LD_HL_reg: begin
416 `INSN_LD_reg_HL: begin
421 `INSN_reg_A: registers[`REG_A] <= tmp;
422 `INSN_reg_B: registers[`REG_B] <= tmp;
423 `INSN_reg_C: registers[`REG_C] <= tmp;
424 `INSN_reg_D: registers[`REG_D] <= tmp;
425 `INSN_reg_E: registers[`REG_E] <= tmp;
426 `INSN_reg_H: registers[`REG_H] <= tmp;
427 `INSN_reg_L: registers[`REG_L] <= tmp;
433 `INSN_LD_reg_reg: begin
435 `INSN_reg_A: registers[`REG_A] <= tmp;
436 `INSN_reg_B: registers[`REG_B] <= tmp;
437 `INSN_reg_C: registers[`REG_C] <= tmp;
438 `INSN_reg_D: registers[`REG_D] <= tmp;
439 `INSN_reg_E: registers[`REG_E] <= tmp;
440 `INSN_reg_H: registers[`REG_H] <= tmp;
441 `INSN_reg_L: registers[`REG_L] <= tmp;
444 `INSN_LD_reg_imm16: begin
449 `INSN_reg16_BC: registers[`REG_C] <= rdata;
450 `INSN_reg16_DE: registers[`REG_E] <= rdata;
451 `INSN_reg16_HL: registers[`REG_L] <= rdata;
452 `INSN_reg16_SP: registers[`REG_SPL] <= rdata;
458 `INSN_reg16_BC: registers[`REG_B] <= rdata;
459 `INSN_reg16_DE: registers[`REG_D] <= rdata;
460 `INSN_reg16_HL: registers[`REG_H] <= rdata;
461 `INSN_reg16_SP: registers[`REG_SPH] <= rdata;
467 `INSN_LD_SP_HL: begin
471 registers[`REG_SPH] <= tmp;
475 registers[`REG_SPL] <= tmp;
479 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
482 {registers[`REG_SPH],registers[`REG_SPL]} <=
483 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
487 {registers[`REG_SPH],registers[`REG_SPL]} <=
488 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
495 `INSN_POP_reg: begin /* POP is 12 cycles! */
499 {registers[`REG_SPH],registers[`REG_SPL]} <=
500 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
504 `INSN_stack_AF: registers[`REG_F] <= rdata;
505 `INSN_stack_BC: registers[`REG_C] <= rdata;
506 `INSN_stack_DE: registers[`REG_E] <= rdata;
507 `INSN_stack_HL: registers[`REG_L] <= rdata;
509 {registers[`REG_SPH],registers[`REG_SPL]} <=
510 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
515 `INSN_stack_AF: registers[`REG_A] <= rdata;
516 `INSN_stack_BC: registers[`REG_B] <= rdata;
517 `INSN_stack_DE: registers[`REG_D] <= rdata;
518 `INSN_stack_HL: registers[`REG_H] <= rdata;
530 registers[`REG_A] <= rdata;
540 registers[`REG_A] <= rdata;
541 {registers[`REG_H],registers[`REG_L]} <=
542 opcode[4] ? // if set, LDD, else LDI
543 ({registers[`REG_H],registers[`REG_L]} - 1) :
544 ({registers[`REG_H],registers[`REG_L]} + 1);
549 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
550 /* Sit on our asses. */
552 end else begin /* Actually do the computation! */
556 registers[`REG_A] + tmp;
558 { /* Z */ ((registers[`REG_A] + tmp) == 0) ? 1'b1 : 1'b0,
560 /* H */ (({1'b0,registers[`REG_A][3:0]} + {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
561 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
562 registers[`REG_F][3:0]
567 registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]};
569 { /* Z */ ((registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]}) == 0) ? 1'b1 : 1'b0,
571 /* 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,
572 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp} + {8'b0,registers[`REG_F][4]}) >> 8 == 1) ? 1'b1 : 1'b0,
573 registers[`REG_F][3:0]
578 registers[`REG_A] & tmp;
580 { /* Z */ ((registers[`REG_A] & tmp) == 0) ? 1'b1 : 1'b0,
582 registers[`REG_F][3:0]
587 registers[`REG_A] | tmp;
589 { /* Z */ ((registers[`REG_A] | tmp) == 0) ? 1'b1 : 1'b0,
591 registers[`REG_F][3:0]
596 registers[`REG_A] ^ tmp;
598 { /* Z */ ((registers[`REG_A] ^ tmp) == 0) ? 1'b1 : 1'b0,
600 registers[`REG_F][3:0]
608 `INSN_NOP: begin /* NOP! */ end
616 {registers[`REG_SPH],registers[`REG_SPL]} <=
617 {registers[`REG_SPH],registers[`REG_SPL]}-2;
626 registers[`REG_PCL] <= rdata;
630 registers[`REG_PCH] <= rdata;
634 {registers[`REG_SPH],registers[`REG_SPL]} <=
635 {registers[`REG_SPH],registers[`REG_SPL]} + 2;
636 if (opcode[4]) /* RETI */
642 state <= `STATE_FETCH;
653 reg [7:0] rom [2047:0];
655 initial $readmemh("rom.hex", rom);
656 always #10 clk <= ~clk;
663 assign data = rd ? rom[addr] : 8'bzzzzzzzz;