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
39 `define INSN_reg_A 3'b111
40 `define INSN_reg_B 3'b000
41 `define INSN_reg_C 3'b001
42 `define INSN_reg_D 3'b010
43 `define INSN_reg_E 3'b011
44 `define INSN_reg_H 3'b100
45 `define INSN_reg_L 3'b101
46 `define INSN_reg_dHL 3'b110
47 `define INSN_reg16_BC 2'b00
48 `define INSN_reg16_DE 2'b01
49 `define INSN_reg16_HL 2'b10
50 `define INSN_reg16_SP 2'b11
51 `define INSN_stack_AF 2'b11
52 `define INSN_stack_BC 2'b00
53 `define INSN_stack_DE 2'b01
54 `define INSN_stack_HL 2'b10
55 `define INSN_alu_ADD 3'b000
56 `define INSN_alu_ADC 3'b001
57 `define INSN_alu_SUB 3'b010
58 `define INSN_alu_SBC 3'b011
59 `define INSN_alu_AND 3'b100
60 `define INSN_alu_XOR 3'b101
61 `define INSN_alu_OR 3'b110
62 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
66 output reg [15:0] busaddress, /* BUS_* is latched on STATE_FETCH. */
68 output reg buswr, output reg busrd);
70 reg [1:0] state = 0; /* State within this bus cycle (see STATE_*). */
71 reg [2:0] cycle = 0; /* Cycle for instructions. */
73 reg [7:0] registers[11:0];
75 reg [15:0] address; /* Address for the next bus operation. */
77 reg [7:0] opcode; /* Opcode from the current machine cycle. */
79 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
80 reg rd = 1, wr = 0, newcycle = 1;
82 reg [7:0] tmp; /* Generic temporary reg. */
85 assign busdata = buswr ? buswdata : 8'bzzzzzzzz;
102 always @(posedge clk)
108 busaddress <= {registers[`REG_PCH], registers[`REG_PCL]};
110 busaddress <= address;
113 state <= `STATE_DECODE;
122 if (rd) rdata <= busdata;
127 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
128 wdata <= 8'bxxxxxxxx;
129 state <= `STATE_EXECUTE;
131 `STATE_EXECUTE: begin
132 `define EXEC_INC_PC \
133 {registers[`REG_PCH], registers[`REG_PCL]} <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
134 `define EXEC_NEXTADDR_PCINC \
135 address <= {registers[`REG_PCH], registers[`REG_PCL]} + 1
136 `define EXEC_NEWCYCLE \
137 newcycle <= 1; rd <= 1; wr <= 0
139 `INSN_LD_reg_imm8: begin
143 `EXEC_NEXTADDR_PCINC;
148 if (opcode[5:3] == `INSN_reg_dHL) begin
149 address <= {registers[`REG_H], registers[`REG_L]};
164 /* XXX Interrupts needed for HALT. */
166 `INSN_LD_HL_reg: begin
170 `INSN_reg_A: begin wdata <= registers[`REG_A]; end
171 `INSN_reg_B: begin wdata <= registers[`REG_B]; end
172 `INSN_reg_C: begin wdata <= registers[`REG_C]; end
173 `INSN_reg_D: begin wdata <= registers[`REG_D]; end
174 `INSN_reg_E: begin wdata <= registers[`REG_E]; end
175 `INSN_reg_H: begin wdata <= registers[`REG_H]; end
176 `INSN_reg_L: begin wdata <= registers[`REG_L]; end
178 address <= {registers[`REG_H], registers[`REG_L]};
187 `INSN_LD_reg_HL: begin
190 address <= {registers[`REG_H], registers[`REG_L]};
200 `INSN_LD_reg_reg: begin
204 `INSN_reg_A: begin tmp <= registers[`REG_A]; end
205 `INSN_reg_B: begin tmp <= registers[`REG_B]; end
206 `INSN_reg_C: begin tmp <= registers[`REG_C]; end
207 `INSN_reg_D: begin tmp <= registers[`REG_D]; end
208 `INSN_reg_E: begin tmp <= registers[`REG_E]; end
209 `INSN_reg_H: begin tmp <= registers[`REG_H]; end
210 `INSN_reg_L: begin tmp <= registers[`REG_L]; end
213 `INSN_LD_reg_imm16: begin
217 `EXEC_NEXTADDR_PCINC;
221 `EXEC_NEXTADDR_PCINC;
224 2: begin `EXEC_NEWCYCLE; end
227 `INSN_LD_SP_HL: begin
230 tmp <= registers[`REG_H];
235 tmp <= registers[`REG_L];
239 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
243 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
245 `INSN_stack_AF: wdata <= registers[`REG_A];
246 `INSN_stack_BC: wdata <= registers[`REG_B];
247 `INSN_stack_DE: wdata <= registers[`REG_D];
248 `INSN_stack_HL: wdata <= registers[`REG_H];
253 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
255 `INSN_stack_AF: wdata <= registers[`REG_F];
256 `INSN_stack_BC: wdata <= registers[`REG_C];
257 `INSN_stack_DE: wdata <= registers[`REG_E];
258 `INSN_stack_HL: wdata <= registers[`REG_L];
261 2: begin /* TWIDDLE OUR FUCKING THUMBS! */ end
268 `INSN_POP_reg: begin /* POP is 12 cycles! */
272 address <= {registers[`REG_SPH],registers[`REG_SPL]};
276 address <= {registers[`REG_SPH],registers[`REG_SPL]};
287 address <= {8'hFF,registers[`REG_C]};
288 if (opcode[4]) begin // LD A,(C)
292 wdata <= registers[`REG_A];
304 address <= {registers[`REG_H],registers[`REG_L]};
305 if (opcode[3]) begin // LDx A, (HL)
309 wdata <= registers[`REG_A];
319 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
320 // fffffffff fuck your shit, read from (HL) :(
322 address <= {registers[`REG_H], registers[`REG_L]};
327 `INSN_reg_A: begin tmp <= registers[`REG_A]; end
328 `INSN_reg_B: begin tmp <= registers[`REG_B]; end
329 `INSN_reg_C: begin tmp <= registers[`REG_C]; end
330 `INSN_reg_D: begin tmp <= registers[`REG_D]; end
331 `INSN_reg_E: begin tmp <= registers[`REG_E]; end
332 `INSN_reg_H: begin tmp <= registers[`REG_H]; end
333 `INSN_reg_L: begin tmp <= registers[`REG_L]; end
334 `INSN_reg_dHL: begin tmp <= rdata; end
346 address <= {registers[`REG_SPH],registers[`REG_SPL]}-1;
347 wdata <= registers[`REG_PCH];
351 address <= {registers[`REG_SPH],registers[`REG_SPL]}-2;
352 wdata <= registers[`REG_PCL];
354 2: begin /* wee */ end
357 {registers[`REG_PCH],registers[`REG_PCL]} <=
358 {10'b0,opcode[5:3],3'b0};
365 state <= `STATE_WRITEBACK;
367 `STATE_WRITEBACK: begin
372 1: case (opcode[5:3])
373 `INSN_reg_A: begin registers[`REG_A] <= rdata; cycle <= 0; end
374 `INSN_reg_B: begin registers[`REG_B] <= rdata; cycle <= 0; end
375 `INSN_reg_C: begin registers[`REG_C] <= rdata; cycle <= 0; end
376 `INSN_reg_D: begin registers[`REG_D] <= rdata; cycle <= 0; end
377 `INSN_reg_E: begin registers[`REG_E] <= rdata; cycle <= 0; end
378 `INSN_reg_H: begin registers[`REG_H] <= rdata; cycle <= 0; end
379 `INSN_reg_L: begin registers[`REG_L] <= rdata; cycle <= 0; end
380 `INSN_reg_dHL: cycle <= 2;
385 /* Nothing needs happen here. */
386 /* XXX Interrupts needed for HALT. */
388 `INSN_LD_HL_reg: begin
394 `INSN_LD_reg_HL: begin
399 `INSN_reg_A: begin registers[`REG_A] <= tmp; end
400 `INSN_reg_B: begin registers[`REG_B] <= tmp; end
401 `INSN_reg_C: begin registers[`REG_C] <= tmp; end
402 `INSN_reg_D: begin registers[`REG_D] <= tmp; end
403 `INSN_reg_E: begin registers[`REG_E] <= tmp; end
404 `INSN_reg_H: begin registers[`REG_H] <= tmp; end
405 `INSN_reg_L: begin registers[`REG_L] <= tmp; end
411 `INSN_LD_reg_reg: begin
413 `INSN_reg_A: begin registers[`REG_A] <= tmp; end
414 `INSN_reg_B: begin registers[`REG_B] <= tmp; end
415 `INSN_reg_C: begin registers[`REG_C] <= tmp; end
416 `INSN_reg_D: begin registers[`REG_D] <= tmp; end
417 `INSN_reg_E: begin registers[`REG_E] <= tmp; end
418 `INSN_reg_H: begin registers[`REG_H] <= tmp; end
419 `INSN_reg_L: begin registers[`REG_L] <= tmp; end
422 `INSN_LD_reg_imm16: begin
427 `INSN_reg16_BC: registers[`REG_C] <= rdata;
428 `INSN_reg16_DE: registers[`REG_E] <= rdata;
429 `INSN_reg16_HL: registers[`REG_L] <= rdata;
430 `INSN_reg16_SP: registers[`REG_SPL] <= rdata;
436 `INSN_reg16_BC: registers[`REG_B] <= rdata;
437 `INSN_reg16_DE: registers[`REG_D] <= rdata;
438 `INSN_reg16_HL: registers[`REG_H] <= rdata;
439 `INSN_reg16_SP: registers[`REG_SPH] <= rdata;
445 `INSN_LD_SP_HL: begin
449 registers[`REG_SPH] <= tmp;
453 registers[`REG_SPL] <= tmp;
457 `INSN_PUSH_reg: begin /* PUSH is 16 cycles! */
460 {registers[`REG_SPH],registers[`REG_SPL]} <=
461 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
465 {registers[`REG_SPH],registers[`REG_SPL]} <=
466 {registers[`REG_SPH],registers[`REG_SPL]} - 1;
473 `INSN_POP_reg: begin /* POP is 12 cycles! */
477 {registers[`REG_SPH],registers[`REG_SPL]} <=
478 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
482 `INSN_stack_AF: registers[`REG_F] <= rdata;
483 `INSN_stack_BC: registers[`REG_C] <= rdata;
484 `INSN_stack_DE: registers[`REG_E] <= rdata;
485 `INSN_stack_HL: registers[`REG_L] <= rdata;
487 {registers[`REG_SPH],registers[`REG_SPL]} <=
488 {registers[`REG_SPH],registers[`REG_SPL]} + 1;
493 `INSN_stack_AF: registers[`REG_A] <= rdata;
494 `INSN_stack_BC: registers[`REG_B] <= rdata;
495 `INSN_stack_DE: registers[`REG_D] <= rdata;
496 `INSN_stack_HL: registers[`REG_H] <= rdata;
508 registers[`REG_A] <= rdata;
518 registers[`REG_A] <= rdata;
519 {registers[`REG_H],registers[`REG_L]} <=
520 opcode[4] ? // if set, LDD, else LDI
521 ({registers[`REG_H],registers[`REG_L]} - 1) :
522 ({registers[`REG_H],registers[`REG_L]} + 1);
527 if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0)) begin
528 /* Sit on our asses. */
530 end else begin /* Actually do the computation! */
534 registers[`REG_A] + tmp;
536 { /* Z */ ((registers[`REG_A] + tmp) == 0) ? 1'b1 : 1'b0,
538 /* H */ (({1'b0,registers[`REG_A][3:0]} + {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
539 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
540 registers[`REG_F][3:0]
545 registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]};
547 { /* Z */ ((registers[`REG_A] + tmp + {7'b0,registers[`REG_F][4]}) == 0) ? 1'b1 : 1'b0,
549 /* 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,
550 /* C */ (({1'b0,registers[`REG_A]} + {1'b0,tmp} + {8'b0,registers[`REG_F][4]}) >> 8 == 1) ? 1'b1 : 1'b0,
551 registers[`REG_F][3:0]
556 registers[`REG_A] & tmp;
558 { /* Z */ ((registers[`REG_A] & tmp) == 0) ? 1'b1 : 1'b0,
560 registers[`REG_F][3:0]
565 registers[`REG_A] | tmp;
567 { /* Z */ ((registers[`REG_A] | tmp) == 0) ? 1'b1 : 1'b0,
569 registers[`REG_F][3:0]
574 registers[`REG_A] ^ tmp;
576 { /* Z */ ((registers[`REG_A] ^ tmp) == 0) ? 1'b1 : 1'b0,
578 registers[`REG_F][3:0]
586 `INSN_NOP: begin /* NOP! */ end
594 {registers[`REG_SPH],registers[`REG_SPL]} <=
595 {registers[`REG_SPH],registers[`REG_SPL]}-2;
600 state <= `STATE_FETCH;
611 reg [7:0] rom [2047:0];
613 initial $readmemh("rom.hex", rom);
614 always #10 clk <= ~clk;
621 assign data = rd ? rom[addr] : 8'bzzzzzzzz;