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
38 `define INSN_reg_A 3'b111
39 `define INSN_reg_B 3'b000
40 `define INSN_reg_C 3'b001
41 `define INSN_reg_D 3'b010
42 `define INSN_reg_E 3'b011
43 `define INSN_reg_H 3'b100
44 `define INSN_reg_L 3'b101
45 `define INSN_reg_dHL 3'b110
46 `define INSN_reg16_BC 2'b00
47 `define INSN_reg16_DE 2'b01
48 `define INSN_reg16_HL 2'b10
49 `define INSN_reg16_SP 2'b11
50 `define INSN_stack_AF 2'b11
51 `define INSN_stack_BC 2'b00
52 `define INSN_stack_DE 2'b01
53 `define INSN_stack_HL 2'b10
54 `define INSN_alu_ADD 3'b000
55 `define INSN_alu_ADC 3'b001
56 `define INSN_alu_SUB 3'b010
57 `define INSN_alu_SBC 3'b011
58 `define INSN_alu_AND 3'b100
59 `define INSN_alu_XOR 3'b101
60 `define INSN_alu_OR 3'b110
61 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
65 output reg [15:0] busaddress, /* BUS_* is latched on STATE_FETCH. */
67 output reg buswr, output reg busrd);
69 reg [1:0] state = 0; /* State within this bus cycle (see STATE_*). */
70 reg [2:0] cycle = 0; /* Cycle for instructions. */
72 reg [7:0] registers[11:0];
74 reg [15:0] address; /* Address for the next bus operation. */
76 reg [7:0] opcode; /* Opcode from the current machine cycle. */
78 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
79 reg rd = 1, wr = 0, newcycle = 1;
81 reg [7:0] tmp; /* Generic temporary reg. */
84 assign busdata = buswr ? buswdata : 8'bzzzzzzzz;
101 always @(posedge clk)
107 busaddress <= {registers[`REG_PCH], registers[`REG_PCL]};
109 busaddress <= address;
112 state <= `STATE_DECODE;
121 if (rd) rdata <= busdata;
126 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
127 wdata <= 8'bxxxxxxxx;
128 state <= `STATE_EXECUTE;
130 `STATE_EXECUTE: begin
131 `define EXEC_INC_PC \
132 {registers[`REG_PCH], registers[`REG_PCL]} <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
133 `define EXEC_NEXTADDR_PCINC \
134 address <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
135 `define EXEC_NEWCYCLE \
136 newcycle <= 1; rd <= 1; wr <= 0
138 `INSN_LD_reg_imm8: begin
142 `EXEC_NEXTADDR_PCINC;
147 if (opcode[5:3] == `INSN_reg_dHL) begin
148 address <= {registers[`REG_H], registers[`REG_L]};
163 /* XXX Interrupts needed for HALT. */
165 `INSN_LD_HL_reg: begin
169 `INSN_reg_A: begin wdata <= registers[`REG_A]; end
170 `INSN_reg_B: begin wdata <= registers[`REG_B]; end
171 `INSN_reg_C: begin wdata <= registers[`REG_C]; end
172 `INSN_reg_D: begin wdata <= registers[`REG_D]; end
173 `INSN_reg_E: begin wdata <= registers[`REG_E]; end
174 `INSN_reg_H: begin wdata <= registers[`REG_H]; end
175 `INSN_reg_L: begin wdata <= registers[`REG_L]; end
177 address <= {registers[`REG_H], registers[`REG_L]};
186 `INSN_LD_reg_HL: begin
189 address <= {registers[`REG_H], registers[`REG_L]};
199 `INSN_LD_reg_reg: begin
203 `INSN_reg_A: begin tmp <= registers[`REG_A]; end
204 `INSN_reg_B: begin tmp <= registers[`REG_B]; end
205 `INSN_reg_C: begin tmp <= registers[`REG_C]; end
206 `INSN_reg_D: begin tmp <= registers[`REG_D]; end
207 `INSN_reg_E: begin tmp <= registers[`REG_E]; end
208 `INSN_reg_H: begin tmp <= registers[`REG_H]; end
209 `INSN_reg_L: begin tmp <= registers[`REG_L]; end
212 `INSN_LD_reg_imm16: begin
216 `EXEC_NEXTADDR_PCINC;
220 `EXEC_NEXTADDR_PCINC;
223 2: begin `EXEC_NEWCYCLE; end
226 `INSN_LD_SP_HL: begin
229 tmp <= registers[`REG_H];
234 tmp <= registers[`REG_L];
238 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
242 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
244 `INSN_stack_AF: wdata <= registers[`REG_A];
245 `INSN_stack_BC: wdata <= registers[`REG_B];
246 `INSN_stack_DE: wdata <= registers[`REG_D];
247 `INSN_stack_HL: wdata <= registers[`REG_H];
252 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
254 `INSN_stack_AF: wdata <= registers[`REG_F];
255 `INSN_stack_BC: wdata <= registers[`REG_C];
256 `INSN_stack_DE: wdata <= registers[`REG_E];
257 `INSN_stack_HL: wdata <= registers[`REG_L];
260 2: begin /* TWIDDLE OUR FUCKING THUMBS! */ end
267 `INSN_POP_reg: begin /* POP is 12 cycles! */
271 address <= {registers[`REG_SPH],registers[`REG_SPL]};
275 address <= {registers[`REG_SPH],registers[`REG_SPL]};
286 address <= {8'hFF,registers[`REG_C]};
287 if (opcode[4]) begin // LD A,(C)
291 wdata <= registers[`REG_A];
303 address <= {registers[`REG_H],registers[`REG_L]};
304 if (opcode[3]) begin // LDx A, (HL)
308 wdata <= registers[`REG_A];
318 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
319 // fffffffff fuck your shit, read from (HL) :(
321 address <= {registers[`REG_H], registers[`REG_L]};
326 `INSN_reg_A: begin tmp <= registers[`REG_A]; end
327 `INSN_reg_B: begin tmp <= registers[`REG_B]; end
328 `INSN_reg_C: begin tmp <= registers[`REG_C]; end
329 `INSN_reg_D: begin tmp <= registers[`REG_D]; end
330 `INSN_reg_E: begin tmp <= registers[`REG_E]; end
331 `INSN_reg_H: begin tmp <= registers[`REG_H]; end
332 `INSN_reg_L: begin tmp <= registers[`REG_L]; end
333 `INSN_reg_dHL: begin tmp <= rdata; end
344 state <= `STATE_WRITEBACK;
346 `STATE_WRITEBACK: begin
351 1: case (opcode[5:3])
352 `INSN_reg_A: begin registers[`REG_A] <= rdata; cycle <= 0; end
353 `INSN_reg_B: begin registers[`REG_B] <= rdata; cycle <= 0; end
354 `INSN_reg_C: begin registers[`REG_C] <= rdata; cycle <= 0; end
355 `INSN_reg_D: begin registers[`REG_D] <= rdata; cycle <= 0; end
356 `INSN_reg_E: begin registers[`REG_E] <= rdata; cycle <= 0; end
357 `INSN_reg_H: begin registers[`REG_H] <= rdata; cycle <= 0; end
358 `INSN_reg_L: begin registers[`REG_L] <= rdata; cycle <= 0; end
359 `INSN_reg_dHL: cycle <= 2;
364 /* Nothing needs happen here. */
365 /* XXX Interrupts needed for HALT. */
367 `INSN_LD_HL_reg: begin
373 `INSN_LD_reg_HL: begin
378 `INSN_reg_A: begin registers[`REG_A] <= tmp; end
379 `INSN_reg_B: begin registers[`REG_B] <= tmp; end
380 `INSN_reg_C: begin registers[`REG_C] <= tmp; end
381 `INSN_reg_D: begin registers[`REG_D] <= tmp; end
382 `INSN_reg_E: begin registers[`REG_E] <= tmp; end
383 `INSN_reg_H: begin registers[`REG_H] <= tmp; end
384 `INSN_reg_L: begin registers[`REG_L] <= tmp; end
390 `INSN_LD_reg_reg: begin
392 `INSN_reg_A: begin registers[`REG_A] <= tmp; end
393 `INSN_reg_B: begin registers[`REG_B] <= tmp; end
394 `INSN_reg_C: begin registers[`REG_C] <= tmp; end
395 `INSN_reg_D: begin registers[`REG_D] <= tmp; end
396 `INSN_reg_E: begin registers[`REG_E] <= tmp; end
397 `INSN_reg_H: begin registers[`REG_H] <= tmp; end
398 `INSN_reg_L: begin registers[`REG_L] <= tmp; end
401 `INSN_LD_reg_imm16: begin
406 `INSN_reg16_BC: registers[`REG_C] <= rdata;
407 `INSN_reg16_DE: registers[`REG_E] <= rdata;
408 `INSN_reg16_HL: registers[`REG_L] <= rdata;
409 `INSN_reg16_SP: registers[`REG_SPL] <= rdata;
415 `INSN_reg16_BC: registers[`REG_B] <= rdata;
416 `INSN_reg16_DE: registers[`REG_D] <= rdata;
417 `INSN_reg16_HL: registers[`REG_H] <= rdata;
418 `INSN_reg16_SP: registers[`REG_SPH] <= rdata;
424 `INSN_LD_SP_HL: begin
428 registers[`REG_SPH] <= tmp;
432 registers[`REG_SPL] <= tmp;
436 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
439 {registers[`REG_SPH],registers[`REG_SPL]} <=
440 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
444 {registers[`REG_SPH],registers[`REG_SPL]} <=
445 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
452 `INSN_POP_reg: begin /* POP is 12 cycles! */
456 {registers[`REG_SPH],registers[`REG_SPL]} <=
457 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
461 `INSN_stack_AF: registers[`REG_F] <= rdata;
462 `INSN_stack_BC: registers[`REG_C] <= rdata;
463 `INSN_stack_DE: registers[`REG_E] <= rdata;
464 `INSN_stack_HL: registers[`REG_L] <= rdata;
466 {registers[`REG_SPH],registers[`REG_SPL]} <=
467 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
472 `INSN_stack_AF: registers[`REG_A] <= rdata;
473 `INSN_stack_BC: registers[`REG_B] <= rdata;
474 `INSN_stack_DE: registers[`REG_D] <= rdata;
475 `INSN_stack_HL: registers[`REG_H] <= rdata;
487 registers[`REG_A] <= rdata;
497 registers[`REG_A] <= rdata;
498 {registers[`REG_H],registers[`REG_L]} <=
499 opcode[4] ? // if set, LDD, else LDI
500 ({registers[`REG_H],registers[`REG_L]} - 1) :
501 ({registers[`REG_H],registers[`REG_L]} + 1);
506 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
507 /* Sit on our asses. */
509 end else begin /* Actually do the computation! */
513 registers[`REG_A] + tmp;
515 { /* Z */ ((registers[`REG_A] + tmp) == 0) ? 1'b1 : 1'b0,
517 /* H */ (({1'b0,registers[`REG_A][3:0]} + {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
518 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
519 registers[`REG_F][3:0]
524 registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]};
526 { /* Z */ ((registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]}) == 0) ? 1'b1 : 1'b0,
528 /* 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,
529 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp} + {8'b0,registers[`REG_F][4]}) >> 8 == 1) ? 1'b1 : 1'b0,
530 registers[`REG_F][3:0]
535 registers[`REG_A] & tmp;
537 { /* Z */ ((registers[`REG_A] & tmp) == 0) ? 1'b1 : 1'b0,
539 registers[`REG_F][3:0]
544 registers[`REG_A] | tmp;
546 { /* Z */ ((registers[`REG_A] | tmp) == 0) ? 1'b1 : 1'b0,
548 registers[`REG_F][3:0]
553 registers[`REG_A] ^ tmp;
555 { /* Z */ ((registers[`REG_A] ^ tmp) == 0) ? 1'b1 : 1'b0,
557 registers[`REG_F][3:0]
565 `INSN_NOP: begin /* NOP! */ end
567 state <= `STATE_FETCH;
578 reg [7:0] rom [2047:0];
580 initial $readmemh("rom.hex", rom);
581 always #10 clk <= ~clk;
588 assign data = rd ? rom[addr] : 8'bzzzzzzzz;