]> Joshua Wise's Git repositories - fpgaboy.git/blame - core/GBZ80Core.v
Add set/res, which fixes some crashes in the test suite
[fpgaboy.git] / core / GBZ80Core.v
CommitLineData
df770340
JW
1`define REG_A 0
2`define REG_B 1
3`define REG_C 2
4`define REG_D 3
5`define REG_E 4
6`define REG_F 5
7`define REG_H 6
8`define REG_L 7
9`define REG_SPH 8
10`define REG_SPL 9
11`define REG_PCH 10
12`define REG_PCL 11
2f55f809 13
5509558d
JW
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}
32
df770340
JW
33`define FLAG_Z 8'b10000000
34`define FLAG_N 8'b01000000
35`define FLAG_H 8'b00100000
36`define FLAG_C 8'b00010000
2f55f809 37
df770340
JW
38`define STATE_FETCH 2'h0
39`define STATE_DECODE 2'h1
2f55f809
JW
40`define STATE_EXECUTE 2'h2
41`define STATE_WRITEBACK 2'h3
42
decafd62 43`define INSN_VOP_INTR 9'b011111100 // 0xFC is grabbed by the fetch if there is an interrupt pending.
decafd62
JW
44`define INSN_RES 9'b110xxxxxx
45`define INSN_SET 9'b111xxxxxx
a85b19a7 46
df770340
JW
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
fa136d63 51
b85870e0
JW
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
df770340
JW
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
94522011
JW
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?
decafd62
JW
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
94522011 84
5c33c5c0
JW
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
decafd62 88`define EXEC_NEWCYCLE_TWOBYTE begin newcycle <= 1; rd <= 1; wr <= 0; twobyte <= 1; end
e7fe9dc2 89`ifdef isim
e7fb589a
JW
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
92`else
e7fe9dc2
JW
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
e7fb589a 96`endif
5509558d 97
2f55f809
JW
98module GBZ80Core(
99 input clk,
91c74a3f
JW
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,
d1b40456 106 input irq, output reg irqack, input [7:0] jaddr,
6c46357c 107 output reg [1:0] state);
decafd62 108
6c46357c 109// reg [1:0] state; /* State within this bus cycle (see STATE_*). */
9c834ff2 110 reg [2:0] cycle; /* Cycle for instructions. */
2f55f809
JW
111
112 reg [7:0] registers[11:0];
113
114 reg [15:0] address; /* Address for the next bus operation. */
115
decafd62
JW
116 reg [8:0] opcode; /* Opcode from the current machine cycle. */
117
2f55f809 118 reg [7:0] rdata, wdata; /* Read data from this bus cycle, or write data for the next. */
decafd62 119 reg rd, wr, newcycle, twobyte;
2f55f809 120
ef6fbe31 121 reg [7:0] tmp, tmp2; /* Generic temporary regs. */
b85870e0 122
2f55f809 123 reg [7:0] buswdata;
91c74a3f
JW
124 wire [7:0] busdata;
125
126 reg [15:0] busaddress;
127 reg buswr, busrd;
128
129 reg bootstrap_enb;
130
e29171aa
JW
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 */
132 `ifdef isim
133 || (busaddress === 16'hxxxx) /* To avoid simulator glomulation. */
134 `endif
135 ;
91c74a3f
JW
136
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;
e29171aa
JW
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;
decafd62 146
eb0f2fe1 147 reg ie, iedelay;
decafd62 148
5c834537
JW
149`define LOCALWIRES
150`include "allinsns.v"
151`undef LOCALWIRES
152
decafd62
JW
153 wire [7:0] rlc,rrc,rl,rr,sla,sra,swap,srl;
154 wire [3:0] rlcf,rrcf,rlf,rrf,slaf,sraf,swapf,srlf;
155 wire [7:0] alu_res;
156 wire [3:0] f_res;
157
158 assign rlc = {tmp[6:0],tmp[7]};
159 assign rlcf = {(tmp == 0 ? 1'b1 : 1'b0)
160 ,2'b0,
161 tmp[7]};
162
163 assign rrc = {tmp[0],tmp[7:1]};
164 assign rrcf = {(tmp == 0 ? 1'b1 : 1'b0),
165 2'b0,
166 tmp[0]};
167
168 assign rl = {tmp[6:0],`_F[4]};
169 assign rlf = {({tmp[6:0],`_F[4]} == 0 ? 1'b1 : 1'b0),
170 2'b0,
171 tmp[7]};
172
173 assign rr = {`_F[4],tmp[7:1]};
174 assign rrf = {({tmp[4],tmp[7:1]} == 0 ? 1'b1 : 1'b0),
175 2'b0,
176 tmp[0]};
177
e7fb589a 178 assign sla = {tmp[6:0],1'b0};
decafd62
JW
179 assign slaf = {(tmp[6:0] == 0 ? 1'b1 : 1'b0),
180 2'b0,
181 tmp[7]};
182
183 assign sra = {tmp[7],tmp[7:1]};
184// assign sraf = {(tmp[7:1] == 0 ? 1'b1 : 1'b0),2'b0,tmp[0]}; now in assign srlf =
185
186 assign swap = {tmp[3:0],tmp[7:4]};
e7fb589a 187 assign swapf = {(tmp == 1'b0 ? 1'b1 : 1'b0),
decafd62
JW
188 3'b0};
189
e7fb589a 190 assign srl = {1'b0,tmp[7:1]};
decafd62
JW
191 assign srlf = {(tmp[7:1] == 0 ? 1'b1 : 1'b0),
192 2'b0,
193 tmp[0]};
194 assign sraf = srlf;
195
196 /* Y U Q */
197 assign {alu_res,f_res} =
198 opcode[5] ? (
199 opcode[4] ? (
200 opcode[3] ? {srl,srlf} : {swap,swapf}
201 ) : (
202 opcode[3] ? {sra,sraf} : {sla,slaf}
203 )
204 ) : (
205 opcode[4] ? (
206 opcode[3] ? {rr,rrf} : {rl,rlf}
207 ) : (
208 opcode[3] ? {rrc,rrcf} : {rlc,rlcf}
209 )
210 );
211
2f55f809 212 initial begin
b4f3ac35
JW
213 `_A <= 0;
214 `_B <= 0;
215 `_C <= 0;
216 `_D <= 0;
217 `_E <= 0;
218 `_F <= 0;
219 `_H <= 0;
220 `_L <= 0;
221 `_PCH <= 0;
222 `_PCL <= 0;
223 `_SPH <= 0;
224 `_SPL <= 0;
2e642f1f
JW
225 rd <= 1;
226 wr <= 0;
227 newcycle <= 1;
228 state <= 0;
229 cycle <= 0;
f8db6448
JW
230 busrd <= 0;
231 buswr <= 0;
232 busaddress <= 0;
9c834ff2 233 ie <= 0;
f8db6448 234 iedelay <= 0;
9c834ff2
JW
235 opcode <= 0;
236 state <= `STATE_WRITEBACK;
237 cycle <= 0;
decafd62 238 twobyte <= 0;
91c74a3f 239 bootstrap_enb <= 1;
d1b40456 240 irqack <= 0;
2f55f809
JW
241 end
242
b338a0b6 243 always @(negedge clk) /* Set things up at the negedge to prepare for the posedge. */
2f55f809
JW
244 case (state)
245 `STATE_FETCH: begin
2e642f1f 246 if (newcycle) begin
decafd62 247 busaddress <= `_PC;
2e642f1f
JW
248 buswr <= 0;
249 busrd <= 1;
250 end else begin
2f55f809 251 busaddress <= address;
2e642f1f
JW
252 buswr <= wr;
253 busrd <= rd;
1eefdc8e 254 if (wr) begin
2e642f1f 255 buswdata <= wdata;
1eefdc8e
JW
256 if (address == 16'hFF50)
257 bootstrap_enb <= 0;
258 end
2e642f1f 259 end
b338a0b6
JW
260 end
261 `STATE_DECODE: begin /* Make sure this only happens for one clock. */
e29171aa
JW
262 buswr <= 0;
263 busrd <= 0;
b338a0b6
JW
264 end
265 endcase
266
267 always @(posedge clk)
268 case (state)
269 `STATE_FETCH: begin
270 /* Things are set up in negedge so that something looking on posedge will get his shit. */
2f55f809
JW
271 state <= `STATE_DECODE;
272 end
273 `STATE_DECODE: begin
274 if (newcycle) begin
decafd62 275 if (twobyte) begin
e7fb589a 276 opcode <= {1'b1,busdata};
decafd62
JW
277 twobyte <= 0;
278 end else if (ie && irq)
f8db6448
JW
279 opcode <= `INSN_VOP_INTR;
280 else
e7fb589a 281 opcode <= {1'b0,busdata};
b85870e0 282 newcycle <= 0;
2854e399 283 rdata <= busdata;
2f55f809 284 cycle <= 0;
2e642f1f 285 end else begin
e29171aa 286 if (rd) rdata <= busdata; /* Still valid because peripherals are now expected to keep it held valid. */
2e642f1f
JW
287 cycle <= cycle + 1;
288 end
f8db6448
JW
289 if (iedelay) begin
290 ie <= 1;
291 iedelay <= 0;
292 end
97649fed
JW
293 wr <= 0;
294 rd <= 0;
295 address <= 16'bxxxxxxxxxxxxxxxx; // Make it obvious if something of type has happened.
296 wdata <= 8'bxxxxxxxx;
2f55f809
JW
297 state <= `STATE_EXECUTE;
298 end
299 `STATE_EXECUTE: begin
d1b40456 300 `ifdef isim
e29171aa
JW
301 if (opcode[7:0] === 8'bxxxxxxxx)
302 $stop;
d1b40456 303 `endif
2f55f809 304 casex (opcode)
81358c71
JW
305 `define EXECUTE
306 `include "allinsns.v"
307 `undef EXECUTE
634ce02c
JW
308 default:
309 $stop;
2f55f809
JW
310 endcase
311 state <= `STATE_WRITEBACK;
312 end
313 `STATE_WRITEBACK: begin
314 casex (opcode)
81358c71
JW
315 `define WRITEBACK
316 `include "allinsns.v"
317 `undef WRITEBACK
ef6fbe31
JW
318 default:
319 $stop;
2f55f809
JW
320 endcase
321 state <= `STATE_FETCH;
322 end
323 endcase
324endmodule
This page took 0.084378 seconds and 4 git commands to generate.