14 `define _A registers[`REG_A]
15 `define _B registers[`REG_B]
16 `define _C registers[`REG_C]
17 `define _D registers[`REG_D]
18 `define _E registers[`REG_E]
19 `define _F registers[`REG_F]
20 `define _H registers[`REG_H]
21 `define _L registers[`REG_L]
22 `define _SPH registers[`REG_SPH]
23 `define _SPL registers[`REG_SPL]
24 `define _PCH registers[`REG_PCH]
25 `define _PCL registers[`REG_PCL]
26 `define _AF {`_A, `_F}
27 `define _BC {`_B, `_C}
28 `define _DE {`_D, `_E}
29 `define _HL {`_H, `_L}
30 `define _SP {`_SPH, `_SPL}
31 `define _PC {`_PCH, `_PCL}
33 `define FLAG_Z 8'b10000000
34 `define FLAG_N 8'b01000000
35 `define FLAG_H 8'b00100000
36 `define FLAG_C 8'b00010000
38 `define STATE_FETCH 2'h0
39 `define STATE_DECODE 2'h1
40 `define STATE_EXECUTE 2'h2
41 `define STATE_WRITEBACK 2'h3
43 `define INSN_VOP_INTR 9'b011111100 // 0xFC is grabbed by the fetch if there is an interrupt pending.
44 `define INSN_RES 9'b110xxxxxx
45 `define INSN_SET 9'b111xxxxxx
47 `define INSN_cc_NZ 2'b00
48 `define INSN_cc_Z 2'b01
49 `define INSN_cc_NC 2'b10
50 `define INSN_cc_C 2'b11
52 `define INSN_reg_A 3'b111
53 `define INSN_reg_B 3'b000
54 `define INSN_reg_C 3'b001
55 `define INSN_reg_D 3'b010
56 `define INSN_reg_E 3'b011
57 `define INSN_reg_H 3'b100
58 `define INSN_reg_L 3'b101
59 `define INSN_reg_dHL 3'b110
60 `define INSN_reg16_BC 2'b00
61 `define INSN_reg16_DE 2'b01
62 `define INSN_reg16_HL 2'b10
63 `define INSN_reg16_SP 2'b11
64 `define INSN_stack_AF 2'b11
65 `define INSN_stack_BC 2'b00
66 `define INSN_stack_DE 2'b01
67 `define INSN_stack_HL 2'b10
68 `define INSN_alu_ADD 3'b000
69 `define INSN_alu_ADC 3'b001
70 `define INSN_alu_SUB 3'b010
71 `define INSN_alu_SBC 3'b011
72 `define INSN_alu_AND 3'b100
73 `define INSN_alu_XOR 3'b101
74 `define INSN_alu_OR 3'b110
75 `define INSN_alu_CP 3'b111 // Oh lawd, is dat some CP?
76 `define INSN_alu_RLC 3'b000
77 `define INSN_alu_RRC 3'b001
78 `define INSN_alu_RL 3'b010
79 `define INSN_alu_RR 3'b011
80 `define INSN_alu_DA_SLA 3'b100
81 `define INSN_alu_CPL_SRA 3'b101
82 `define INSN_alu_SCF_SWAP 3'b110
83 `define INSN_alu_CCF_SRL 3'b111
85 `define EXEC_INC_PC `_PC <= `_PC + 1;
86 `define EXEC_NEXTADDR_PCINC address <= `_PC + 1;
87 `define EXEC_NEWCYCLE begin newcycle <= 1; rd <= 1; wr <= 0; end
88 `define EXEC_NEWCYCLE_TWOBYTE begin newcycle <= 1; rd <= 1; wr <= 0; twobyte <= 1; end
90 `define EXEC_WRITE(ad, da) begin address <= (ad); wdata <= (da); wr <= 1; end
91 `define EXEC_READ(ad) begin address <= (ad); rd <= 1; end
93 /* Work around XST's retarded bugs :\ */
94 `define EXEC_WRITE(ad, da) begin address <= (ad); wdata <= (da); wr <= 1; end end
95 `define EXEC_READ(ad) begin address <= (ad); rd <= 1; end end
100 inout [15:0] bus0address, /* BUS_* is latched on STATE_FETCH. */
101 inout [7:0] bus0data,
102 inout bus0wr, bus0rd,
103 inout [15:0] bus1address, /* BUS_* is latched on STATE_FETCH. */
104 inout [7:0] bus1data,
105 inout bus1wr, bus1rd,
106 input irq, output reg irqack, input [7:0] jaddr,
107 output reg [1:0] state);
109 // reg [1:0] state; /* State within this bus cycle (see STATE_*). */
110 reg [2:0] cycle; /* Cycle for instructions. */
112 reg [7:0] registers[11:0];
114 reg [15:0] address; /* Address for the next bus operation. */
116 reg [8:0] opcode; /* Opcode from the current machine cycle. */
118 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
119 reg rd, wr, newcycle, twobyte;
121 reg [7:0] tmp, tmp2; /* Generic temporary regs. */
126 reg [15:0] busaddress;
131 wire bus = ((busaddress[15:8] == 8'h00) && bootstrap_enb) || ((busaddress[15:7] == 9'b111111111) && (busaddress != 16'hFFFF)) /* 0 or 1 depending on which bus */
133 || (busaddress === 16'hxxxx) /* To avoid simulator glomulation. */
137 assign bus0address = (bus == 0) ? busaddress : 16'bzzzzzzzzzzzzzzz;
138 assign bus1address = (bus == 1) ? busaddress : 16'bzzzzzzzzzzzzzzz;
139 assign bus0data = ((bus == 0) && buswr) ? buswdata : 8'bzzzzzzzz;
140 assign bus1data = ((bus == 1) && buswr) ? buswdata : 8'bzzzzzzzz;
141 assign busdata = (bus == 0) ? bus0data : bus1data;
142 assign bus0rd = (bus == 0) ? busrd : 1'b0;
143 assign bus1rd = (bus == 1) ? busrd : 1'b0;
144 assign bus0wr = (bus == 0) ? buswr : 1'b0;
145 assign bus1wr = (bus == 1) ? buswr : 1'b0;
150 `include "allinsns.v"
177 state <= `STATE_WRITEBACK;
184 always @(negedge clk) /* Set things up at the negedge to prepare for the posedge. */
192 busaddress <= address;
197 if (address == 16'hFF50)
202 `STATE_DECODE: begin /* Make sure this only happens for one clock. */
208 always @(posedge clk)
211 /* Things are set up in negedge so that something looking on posedge will get his shit. */
212 state <= `STATE_DECODE;
217 opcode <= {1'b1,busdata};
219 end else if (ie && irq)
220 opcode <= `INSN_VOP_INTR;
222 opcode <= {1'b0,busdata};
227 if (rd) rdata <= busdata; /* Still valid because peripherals are now expected to keep it held valid. */
237 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
238 wdata <= 8'bxxxxxxxx;
240 state <= `STATE_EXECUTE;
242 `STATE_EXECUTE: begin
244 if (opcode[7:0] === 8'bxxxxxxxx)
249 `include "allinsns.v"
253 address <= {7'h78,opcode}; // Have the CPU tell you F0xx if something's gone wrong.
257 state <= `STATE_WRITEBACK;
259 `STATE_WRITEBACK: begin
262 `include "allinsns.v"
267 state <= `STATE_FETCH;