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_RETCC 8'b110xx000
40 `define INSN_CALL 8'b11001101
41 `define INSN_CALLCC 8'b110xx100 // Not that call/cc.
42 `define INSN_JP_imm 8'b11000011
43 `define INSN_JPCC_imm 8'b110xx010
44 `define INSN_ALU_A 8'b00xxx111
45 `define INSN_JP_HL 8'b11101001
46 `define INSN_JR_imm 8'b00011000
47 `define INSN_JRCC_imm 8'b001xx000
48 `define INSN_INCDEC16 8'b00xxx011
49 `define INSN_VOP_INTR 8'b11111100 // 0xFC is grabbed by the fetch if there is an interrupt pending.
50 `define INSN_DI 8'b11110011
51 `define INSN_EI 8'b11111011
53 `define INSN_cc_NZ 2'b00
54 `define INSN_cc_Z 2'b01
55 `define INSN_cc_NC 2'b10
56 `define INSN_cc_C 2'b11
58 `define INSN_reg_A 3'b111
59 `define INSN_reg_B 3'b000
60 `define INSN_reg_C 3'b001
61 `define INSN_reg_D 3'b010
62 `define INSN_reg_E 3'b011
63 `define INSN_reg_H 3'b100
64 `define INSN_reg_L 3'b101
65 `define INSN_reg_dHL 3'b110
66 `define INSN_reg16_BC 2'b00
67 `define INSN_reg16_DE 2'b01
68 `define INSN_reg16_HL 2'b10
69 `define INSN_reg16_SP 2'b11
70 `define INSN_stack_AF 2'b11
71 `define INSN_stack_BC 2'b00
72 `define INSN_stack_DE 2'b01
73 `define INSN_stack_HL 2'b10
74 `define INSN_alu_ADD 3'b000
75 `define INSN_alu_ADC 3'b001
76 `define INSN_alu_SUB 3'b010
77 `define INSN_alu_SBC 3'b011
78 `define INSN_alu_AND 3'b100
79 `define INSN_alu_XOR 3'b101
80 `define INSN_alu_OR 3'b110
81 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
82 `define INSN_alu_RLCA 3'b000
83 `define INSN_alu_RRCA 3'b001
84 `define INSN_alu_RLA 3'b010
85 `define INSN_alu_RRA 3'b011
86 `define INSN_alu_DAA 3'b100
87 `define INSN_alu_CPL 3'b101
88 `define INSN_alu_SCF 3'b110
89 `define INSN_alu_CCF 3'b111
93 output reg [15:0] busaddress, /* BUS_* is latched on STATE_FETCH. */
95 output reg buswr, output reg busrd,
96 input irq, input [7:0] jaddr);
98 reg [1:0] state; /* State within this bus cycle (see STATE_*). */
99 reg [2:0] cycle; /* Cycle for instructions. */
101 reg [7:0] registers[11:0];
103 reg [15:0] address; /* Address for the next bus operation. */
105 reg [7:0] opcode; /* Opcode from the current machine cycle. */
107 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
108 reg rd, wr, newcycle;
110 reg [7:0] tmp, tmp2; /* Generic temporary regs. */
113 assign busdata = buswr ? buswdata : 8'bzzzzzzzz;
141 state <= `STATE_WRITEBACK;
145 always @(posedge clk)
149 busaddress <= {registers[`REG_PCH], registers[`REG_PCL]};
153 busaddress <= address;
159 state <= `STATE_DECODE;
164 opcode <= `INSN_VOP_INTR;
171 if (rd) rdata <= busdata;
182 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
183 wdata <= 8'bxxxxxxxx;
184 state <= `STATE_EXECUTE;
186 `STATE_EXECUTE: begin
187 `define EXEC_INC_PC \
188 {registers[`REG_PCH], registers[`REG_PCL]} <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
189 `define EXEC_NEXTADDR_PCINC \
190 address <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
191 `define EXEC_NEWCYCLE \
192 newcycle <= 1; rd <= 1; wr <= 0
195 `include "allinsns.v"
200 `EXEC_INC_PC; // This goes FIRST in RST
204 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
205 wdata <= registers[`REG_PCH];
209 address <= {registers[`REG_SPH],registers[`REG_SPL]}-2;
210 wdata <= registers[`REG_PCL];
214 {registers[`REG_PCH],registers[`REG_PCL]} <=
215 {10'b0,opcode[5:3],3'b0};
219 `INSN_RET,`INSN_RETCC: begin
223 address <= {registers[`REG_SPH],registers[`REG_SPL]};
225 1: begin // SPECIAL CASE: cycle does NOT increase linearly with ret!
226 `EXEC_INC_PC; // cycle 1 is skipped if we are not retcc
228 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
229 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
230 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
231 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
234 address <= {registers[`REG_SPH],registers[`REG_SPL]};
238 address <= {registers[`REG_SPH],registers[`REG_SPL]} + 1;
240 3: begin /* twiddle thumbs */ end
243 // do NOT increment PC!
247 `INSN_CALL,`INSN_CALLCC: begin
251 `EXEC_NEXTADDR_PCINC;
256 `EXEC_NEXTADDR_PCINC;
261 if (!opcode[0]) // i.e., is callcc
262 /* We need to check the condition code to bail out. */
264 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
265 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
266 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
267 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
271 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 1;
272 wdata <= registers[`REG_PCH];
276 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 2;
277 wdata <= registers[`REG_PCL];
281 `EXEC_NEWCYCLE; /* do NOT increment the PC */
285 `INSN_JP_imm,`INSN_JPCC_imm: begin
289 `EXEC_NEXTADDR_PCINC;
294 `EXEC_NEXTADDR_PCINC;
299 if (!opcode[0]) begin // i.e., JP cc,nn
300 /* We need to check the condition code to bail out. */
302 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
303 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
304 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
305 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
317 `INSN_JR_imm,`INSN_JRCC_imm: begin
321 `EXEC_NEXTADDR_PCINC;
326 if (opcode[5]) begin // i.e., JP cc,nn
327 /* We need to check the condition code to bail out. */
329 `INSN_cc_NZ: if (registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
330 `INSN_cc_Z: if (~registers[`REG_F][7]) begin `EXEC_NEWCYCLE; end
331 `INSN_cc_NC: if (registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
332 `INSN_cc_C: if (~registers[`REG_F][4]) begin `EXEC_NEWCYCLE; end
341 `INSN_INCDEC16: begin
345 `INSN_reg16_BC: begin
346 tmp <= registers[`REG_B];
347 tmp2 <= registers[`REG_C];
349 `INSN_reg16_DE: begin
350 tmp <= registers[`REG_D];
351 tmp2 <= registers[`REG_E];
353 `INSN_reg16_HL: begin
354 tmp <= registers[`REG_H];
355 tmp2 <= registers[`REG_L];
357 `INSN_reg16_SP: begin
358 tmp <= registers[`REG_SPH];
359 tmp2 <= registers[`REG_SPL];
369 `INSN_VOP_INTR: begin
372 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 1;
373 wdata <= registers[`REG_PCH];
377 address <= {registers[`REG_SPH],registers[`REG_SPL]} - 2;
378 wdata <= registers[`REG_PCL];
397 state <= `STATE_WRITEBACK;
399 `STATE_WRITEBACK: begin
402 `include "allinsns.v"
406 0: begin /* type F */ end
407 1: begin /* type F */ end
408 2: begin /* type F */ end
409 3: {registers[`REG_SPH],registers[`REG_SPL]} <=
410 {registers[`REG_SPH],registers[`REG_SPL]}-2;
413 `INSN_RET,`INSN_RETCC: begin
415 0: if (opcode[0]) // i.e., not RETCC
416 cycle <= 1; // Skip cycle 1; it gets incremented on the next round.
417 1: begin /* Nothing need happen here. */ end
418 2: registers[`REG_PCL] <= rdata;
419 3: registers[`REG_PCH] <= rdata;
421 {registers[`REG_SPH],registers[`REG_SPL]} <=
422 {registers[`REG_SPH],registers[`REG_SPL]} + 2;
423 if (opcode[4] && opcode[0]) /* RETI */
428 `INSN_CALL,`INSN_CALLCC: begin
430 0: begin /* type F */ end
431 1: tmp <= rdata; // tmp contains newpcl
432 2: tmp2 <= rdata; // tmp2 contains newpch
433 3: begin /* type F */ end
434 4: registers[`REG_PCH] <= tmp2;
436 {registers[`REG_SPH],registers[`REG_SPL]} <=
437 {registers[`REG_SPH],registers[`REG_SPL]} - 2;
438 registers[`REG_PCL] <= tmp;
442 `INSN_JP_imm,`INSN_JPCC_imm: begin
444 0: begin /* type F */ end
445 1: tmp <= rdata; // tmp contains newpcl
446 2: tmp2 <= rdata; // tmp2 contains newpch
447 3: {registers[`REG_PCH],registers[`REG_PCL]} <=
452 {registers[`REG_PCH],registers[`REG_PCL]} <=
453 {registers[`REG_H],registers[`REG_L]};
455 `INSN_JR_imm,`INSN_JRCC_imm: begin
457 0: begin /* type F */ end
459 2: {registers[`REG_PCH],registers[`REG_PCL]} <=
460 {registers[`REG_PCH],registers[`REG_PCL]} +
461 {tmp[7]?8'hFF:8'h00,tmp};
464 `INSN_INCDEC16: begin
466 0: {tmp,tmp2} <= {tmp,tmp2} +
467 (opcode[3] ? 16'hFFFF : 16'h0001);
470 `INSN_reg16_BC: begin
471 registers[`REG_B] <= tmp;
472 registers[`REG_C] <= tmp2;
474 `INSN_reg16_DE: begin
475 registers[`REG_D] <= tmp;
476 registers[`REG_E] <= tmp2;
478 `INSN_reg16_HL: begin
479 registers[`REG_H] <= tmp;
480 registers[`REG_L] <= tmp2;
482 `INSN_reg16_SP: begin
483 registers[`REG_SPH] <= tmp;
484 registers[`REG_SPL] <= tmp2;
490 `INSN_VOP_INTR: begin
496 {registers[`REG_PCH],registers[`REG_PCL]} <=
498 {registers[`REG_SPH],registers[`REG_SPL]} <=
499 {registers[`REG_SPH],registers[`REG_SPL]} - 2;
504 `INSN_EI: iedelay <= 1;
508 state <= `STATE_FETCH;