1 `include "ARM_Constants.v"
10 output reg [31:0] busaddr,
14 output reg [31:0] wr_data,
16 output reg [2:0] data_size,
18 /* regfile interface */
19 output reg [3:0] st_read,
22 /* Coprocessor interface */
26 output reg cp_rnw, /* 1 = read from CP, 0 = write to CP */
28 output reg [31:0] cp_write,
40 input [3:0] write_num,
41 input [31:0] write_data,
46 output reg [31:0] outpc,
47 output reg [31:0] outinsn,
48 output reg out_write_reg = 1'b0,
49 output reg [3:0] out_write_num = 4'bxxxx,
50 output reg [31:0] out_write_data = 32'hxxxxxxxx,
51 output reg [31:0] outspsr = 32'hxxxxxxxx,
52 output reg [31:0] outcpsr = 32'hxxxxxxxx
55 reg [31:0] addr, raddr, prev_raddr, next_regdata, next_outcpsr;
57 reg [3:0] next_regsel, cur_reg, prev_reg;
62 reg [3:0] next_write_num;
63 reg [31:0] next_write_data;
65 reg [2:0] lsr_state = 3'b001, next_lsr_state;
66 reg [31:0] align_s1, align_s2, align_rddata;
68 reg [2:0] lsrh_state = 3'b001, next_lsrh_state;
69 reg [31:0] lsrh_rddata;
70 reg [15:0] lsrh_rddata_s1;
71 reg [7:0] lsrh_rddata_s2;
73 reg [15:0] regs, next_regs;
74 reg [3:0] lsm_state = 4'b0001, next_lsm_state;
75 reg [5:0] offset, prev_offset, offset_sel;
77 reg [31:0] swp_oldval, next_swp_oldval;
78 reg [1:0] swp_state = 2'b01, next_swp_state;
84 outbubble <= next_outbubble;
85 out_write_reg <= next_write_reg;
86 out_write_num <= next_write_num;
87 out_write_data <= next_write_data;
91 prev_offset <= offset;
93 outcpsr <= next_outcpsr;
95 swp_state <= next_swp_state;
96 lsm_state <= next_lsm_state;
97 lsr_state <= next_lsr_state;
98 lsrh_state <= next_lsrh_state;
102 reg delayedflush = 0;
103 always @(posedge clk)
104 if (flush && outstall /* halp! I can't do it now, maybe later? */)
106 else if (!outstall /* anything has been handled this time around */)
112 raddr = 32'hxxxxxxxx;
115 wr_data = 32'hxxxxxxxx;
116 busaddr = 32'hxxxxxxxx;
119 next_write_reg = write_reg;
120 next_write_num = write_num;
121 next_write_data = write_data;
122 next_outbubble = inbubble;
126 cp_write = 32'hxxxxxxxx;
127 offset = prev_offset;
128 next_outcpsr = lsm_state == 4'b0010 ? outcpsr : cpsr;
129 lsrh_rddata = 32'hxxxxxxxx;
130 lsrh_rddata_s1 = 16'hxxxx;
131 lsrh_rddata_s2 = 8'hxx;
132 next_lsm_state = lsm_state;
133 next_lsr_state = lsr_state;
134 next_lsrh_state = lsrh_state;
135 next_swp_oldval = swp_oldval;
136 next_swp_state = swp_state;
139 /* XXX shit not given about endianness */
141 `DECODE_ALU_SWP: if(!inbubble) begin
143 next_outbubble = rw_wait;
144 busaddr = {op0[31:2], 2'b0};
145 data_size = insn[22] ? 3'b001 : 3'b100;
151 next_swp_state = 2'b10;
152 next_swp_oldval = rd_data;
154 $display("SWP: read stage");
158 wr_data = insn[22] ? {4{op1[7:0]}} : op1;
159 next_write_reg = 1'b1;
160 next_write_num = insn[15:12];
161 next_write_data = insn[22] ? {24'b0, swp_oldval[7:0]} : swp_oldval;
163 next_swp_state = 2'b01;
164 $display("SWP: write stage");
169 `DECODE_ALU_MULT: begin end
170 `DECODE_ALU_HDATA_REG,
171 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
172 next_outbubble = rw_wait;
174 addr = insn[23] ? op0 + op1 : op0 - op1; /* up/down select */
175 raddr = insn[24] ? op0 : addr; /* pre/post increment */
177 /* rotate to correct position */
179 2'b00: begin end /* swp */
180 2'b01: begin /* unsigned half */
181 wr_data = {2{op2[15:0]}}; /* XXX need to store halfword */
183 lsrh_rddata = {16'b0, raddr[1] ? rd_data[31:16] : rd_data[15:0]};
185 2'b10: begin /* signed byte */
186 wr_data = {4{op2[7:0]}};
188 lsrh_rddata_s1 = raddr[1] ? rd_data[31:16] : rd_data[15:0];
189 lsrh_rddata_s2 = raddr[0] ? lsrh_rddata_s1[15:8] : lsrh_rddata_s1[7:0];
190 lsrh_rddata = {{24{lsrh_rddata_s2[7]}}, lsrh_rddata_s2};
192 2'b11: begin /* signed half */
193 wr_data = {2{op2[15:0]}};
195 lsrh_rddata = raddr[1] ? {{16{rd_data[31]}}, rd_data[31:16]} : {{16{rd_data[15]}}, rd_data[15:0]};
203 next_write_num = insn[15:12];
204 next_write_data = lsrh_rddata;
206 next_write_reg = 1'b1;
208 if(insn[21] | !insn[24]) begin
211 next_lsrh_state = 3'b010;
213 $display("ALU_LDRSTRH: rd_req %d, wr_req %d", rd_req, wr_req);
216 next_outbubble = 1'b0;
217 next_write_reg = 1'b1;
218 next_write_num = insn[19:16];
219 next_write_data = addr;
220 next_lsrh_state = 3'b100;
224 next_lsrh_state = 3'b001;
229 if ((lsrh_state == 3'b001) && flush) begin /* Reject it. */
231 next_lsrh_state = 3'b001;
234 `DECODE_LDRSTR_UNDEFINED: begin end
235 `DECODE_LDRSTR: if(!inbubble) begin
236 next_outbubble = rw_wait;
238 addr = insn[23] ? op0 + op1 : op0 - op1; /* up/down select */
239 raddr = insn[24] ? addr : op0; /* pre/post increment */
241 /* rotate to correct position */
242 align_s1 = raddr[1] ? {rd_data[15:0], rd_data[31:16]} : rd_data;
243 align_s2 = raddr[0] ? {align_s1[7:0], align_s1[31:8]} : align_s1;
244 /* select byte or word */
245 align_rddata = insn[22] ? {24'b0, align_s2[7:0]} : align_s2;
246 wr_data = insn[22] ? {4{op2[7:0]}} : op2; /* XXX need to actually store just a byte */
247 data_size = insn[22] ? 3'b001 : 3'b100;
250 rd_req = insn[20] /* L */;
251 wr_req = ~insn[20] /* L */;
252 next_write_reg = insn[20] /* L */;
253 next_write_num = insn[15:12];
254 if(insn[20] /* L */) begin
255 next_write_data = align_rddata;
257 if(insn[21] /* W */ | !insn[24] /* P */) begin
260 next_lsr_state = 3'b010;
262 $display("LDRSTR: rd_req %d, wr_req %d, raddr %08x, wait %d", rd_req, wr_req, raddr, rw_wait);
267 next_write_reg = 1'b1;
268 next_write_num = insn[19:16];
269 next_write_data = addr;
270 next_lsr_state = 3'b100;
274 next_lsr_state = 3'b001;
279 if ((lsr_state == 3'b001) && flush) begin /* Reject it. */
281 next_lsr_state = 3'b001;
284 /* XXX ldm/stm incorrect in that stupid case where one of the listed regs is the base reg */
285 `DECODE_LDMSTM: if(!inbubble) begin
287 next_outbubble = rw_wait;
291 // next_regs = insn[23] ? op1[15:0] : op1[0:15];
292 /** verilator can suck my dick */
293 $display("LDMSTM: Round 1: base register: %08x, reg list %b", op0, op1[15:0]);
294 next_regs = insn[23] /* U */ ? op1[15:0] : {op1[0], op1[1], op1[2], op1[3], op1[4], op1[5], op1[6], op1[7],
295 op1[8], op1[9], op1[10], op1[11], op1[12], op1[13], op1[14], op1[15]};
298 next_lsm_state = 4'b0010;
304 16'b???????????????1: begin
306 next_regs = {regs[15:1], 1'b0};
308 16'b??????????????10: begin
310 next_regs = {regs[15:2], 2'b0};
312 16'b?????????????100: begin
314 next_regs = {regs[15:3], 3'b0};
316 16'b????????????1000: begin
318 next_regs = {regs[15:4], 4'b0};
320 16'b???????????10000: begin
322 next_regs = {regs[15:5], 5'b0};
324 16'b??????????100000: begin
326 next_regs = {regs[15:6], 6'b0};
328 16'b?????????1000000: begin
330 next_regs = {regs[15:7], 7'b0};
332 16'b????????10000000: begin
334 next_regs = {regs[15:8], 8'b0};
336 16'b???????100000000: begin
338 next_regs = {regs[15:9], 9'b0};
340 16'b??????1000000000: begin
342 next_regs = {regs[15:10], 10'b0};
344 16'b?????10000000000: begin
346 next_regs = {regs[15:11], 11'b0};
348 16'b????100000000000: begin
350 next_regs = {regs[15:12], 12'b0};
352 16'b???1000000000000: begin
354 next_regs = {regs[15:13], 13'b0};
356 16'b??10000000000000: begin
358 next_regs = {regs[15:14], 14'b0};
360 16'b?100000000000000: begin
362 next_regs = {regs[15], 15'b0};
364 16'b1000000000000000: begin
373 cur_reg = insn[23] ? cur_reg : 4'hF - cur_reg;
374 if(cur_reg == 4'hF && insn[22]) begin
378 offset = prev_offset + 6'h4;
379 offset_sel = insn[24] ? offset : prev_offset;
380 raddr = insn[23] ? op0 + {26'b0, offset_sel} : op0 - {26'b0, offset_sel};
382 next_write_reg = !rw_wait;
383 next_write_num = cur_reg;
384 next_write_data = rd_data;
388 cur_reg = prev_reg; /* whoops, do this one again */
392 wr_data = (cur_reg == 4'hF) ? (pc + 12) : st_data;
395 $display("LDMSTM: Stage 2: Writing: regs %b, next_regs %b, reg %d, wr_data %08x, addr %08x", regs, next_regs, cur_reg, wr_data, busaddr);
399 if(next_regs == 16'b0) begin
400 next_lsm_state = 4'b0100;
406 next_write_reg = insn[21] /* writeback */;
407 next_write_num = insn[19:16];
408 next_write_data = insn[23] ? op0 + {26'b0, prev_offset} : op0 - {26'b0, prev_offset};
409 next_lsm_state = 4'b1000;
410 $display("LDMSTM: Stage 3: Writing back");
414 next_lsm_state = 4'b0001;
418 if ((lsm_state == 4'b0001) && flush) begin /* Reject it. */
420 next_lsm_state = 4'b0001;
422 $display("LDMSTM: Decoded, bubble %d, insn %08x, lsm state %b -> %b, stall %d", inbubble, insn, lsm_state, next_lsm_state, outstall);
424 `DECODE_LDCSTC: if(!inbubble) begin
425 $display("WARNING: Unimplemented LDCSTC");
427 `DECODE_CDP: if(!inbubble) begin
434 /* XXX undefined instruction trap */
435 $display("WARNING: Possible CDP undefined instruction");
438 `DECODE_MRCMCR: if(!inbubble) begin
440 cp_rnw = insn[20] /* L */;
441 if (insn[20] == 0 /* store to coprocessor */)
444 if (insn[15:12] != 4'hF /* Fuck you ARM */) begin
445 next_write_reg = 1'b1;
446 next_write_num = insn[15:12];
447 next_write_data = cp_read;
449 next_outcpsr = {cp_read[31:28], cpsr[27:0]};
456 $display("WARNING: Possible MRCMCR undefined instruction: cp_ack %d, cp_busy %d",cp_ack, cp_busy);
458 $display("MRCMCR: ack %d, busy %d", cp_ack, cp_busy);
463 if ((flush || delayedflush) && !outstall)
464 next_outbubble = 1'b1;