1 `include "ARM_Constants.v"
3 `define SWP_READING 2'b01
4 `define SWP_WRITING 2'b10
6 `define LSRH_MEMIO 3'b001
7 `define LSRH_BASEWB 3'b010
8 `define LSRH_WBFLUSH 3'b100
10 `define LSR_MEMIO 4'b0001
11 `define LSR_STRB_WR 4'b0010
12 `define LSR_BASEWB 4'b0100
13 `define LSR_WBFLUSH 4'b1000
15 `define LSM_SETUP 4'b0001
16 `define LSM_MEMIO 4'b0010
17 `define LSM_BASEWB 4'b0100
18 `define LSM_WBFLUSH 4'b1000
28 output reg [31:0] busaddr,
32 output reg [31:0] wr_data,
34 output reg [2:0] data_size,
36 /* regfile interface */
37 output reg [3:0] st_read,
40 /* Coprocessor interface */
44 output reg cp_rnw, /* 1 = read from CP, 0 = write to CP */
46 output reg [31:0] cp_write,
59 input [3:0] write_num,
60 input [31:0] write_data,
65 output reg [31:0] outpc,
66 output reg [31:0] outinsn,
67 output reg out_write_reg = 1'b0,
68 output reg [3:0] out_write_num = 4'bxxxx,
69 output reg [31:0] out_write_data = 32'hxxxxxxxx,
70 output reg [31:0] outspsr = 32'hxxxxxxxx,
71 output reg [31:0] outcpsr = 32'hxxxxxxxx,
72 output reg outcpsrup = 1'hx
75 reg [31:0] addr, raddr, prev_raddr, next_regdata, next_outcpsr;
78 reg [3:0] next_regsel, cur_reg, prev_reg;
83 reg [3:0] next_write_num;
84 reg [31:0] next_write_data;
86 reg [3:0] lsr_state = 4'b0001, next_lsr_state;
87 reg [31:0] align_s1, align_s2, align_rddata;
89 reg [2:0] lsrh_state = 3'b001, next_lsrh_state;
90 reg [31:0] lsrh_rddata;
91 reg [15:0] lsrh_rddata_s1;
92 reg [7:0] lsrh_rddata_s2;
94 reg [15:0] regs, next_regs;
95 reg [3:0] lsm_state = 4'b0001, next_lsm_state;
96 reg [5:0] offset, prev_offset, offset_sel;
98 reg [31:0] swp_oldval, next_swp_oldval;
99 reg [1:0] swp_state = 2'b01, next_swp_state;
101 reg do_rd_data_latch;
102 reg [31:0] rd_data_latch = 32'hxxxxxxxx;
104 always @(posedge clk)
108 outbubble <= next_outbubble;
109 out_write_reg <= next_write_reg;
110 out_write_num <= next_write_num;
111 out_write_data <= next_write_data;
115 prev_offset <= offset;
117 outcpsr <= next_outcpsr;
119 outcpsrup <= next_outcpsrup;
120 swp_state <= next_swp_state;
121 lsm_state <= next_lsm_state;
122 lsr_state <= next_lsr_state;
123 lsrh_state <= next_lsrh_state;
124 if (do_rd_data_latch)
125 rd_data_latch <= rd_data;
129 reg delayedflush = 0;
130 always @(posedge clk)
131 if (flush && outstall /* halp! I can't do it now, maybe later? */)
133 else if (!outstall /* anything has been handled this time around */)
136 /* Drive the state machines and stall. */
140 next_lsm_state = lsm_state;
141 next_lsr_state = lsr_state;
142 next_lsrh_state = lsrh_state;
143 next_swp_state = swp_state;
145 `DECODE_ALU_SWP: if(!inbubble) begin
150 next_swp_state = `SWP_WRITING;
151 $display("SWP: read stage");
156 next_swp_state = `SWP_READING;
157 $display("SWP: write stage");
161 next_swp_state = 2'bxx;
165 `DECODE_ALU_MULT: begin end
166 `DECODE_ALU_HDATA_REG,
167 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
171 if(insn[21] | !insn[24]) begin
174 next_lsrh_state = `LSRH_BASEWB;
177 if (flush) /* special case! */ begin
179 next_lsrh_state = `LSRH_MEMIO;
182 $display("ALU_LDRSTRH: rd_req %d, wr_req %d", rd_req, wr_req);
186 next_lsrh_state = `LSRH_WBFLUSH;
190 next_lsrh_state = `LSRH_MEMIO;
194 next_lsrh_state = 3'bxxx;
198 `DECODE_LDRSTR_UNDEFINED: begin end
199 `DECODE_LDRSTR: if(!inbubble) begin
204 next_lsr_state = `LSR_MEMIO;
205 if (insn[22] /* B */ && !insn[20] /* L */) begin /* i.e., strb */
208 next_lsr_state = `LSR_STRB_WR;
209 end else if (insn[21] /* W */ || !insn[24] /* P */) begin /* writeback needed */
212 next_lsr_state = `LSR_BASEWB;
217 next_lsr_state = `LSR_MEMIO;
219 $display("LDRSTR: rd_req %d, wr_req %d, raddr %08x, wait %d", rd_req, wr_req, raddr, rw_wait);
223 if(insn[21] /* W */ | !insn[24] /* P */) begin
225 next_lsr_state = `LSR_BASEWB;
226 end else if (!rw_wait)
227 next_lsr_state = `LSR_WBFLUSH;
228 $display("LDRSTR: Handling STRB");
232 next_lsr_state = `LSR_WBFLUSH;
236 next_lsr_state = `LSR_MEMIO;
240 next_lsr_state = 4'bxxxx;
243 $display("LDRSTR: Decoded, bubble %d, insn %08x, lsm state %b -> %b, stall %d", inbubble, insn, lsr_state, next_lsr_state, outstall);
245 `DECODE_LDMSTM: if(!inbubble) begin
250 next_lsm_state = `LSM_MEMIO;
253 next_lsm_state = `LSM_SETUP;
255 $display("LDMSTM: Round 1: base register: %08x, reg list %b", op0, op1[15:0]);
259 if(next_regs == 16'b0) begin
260 next_lsm_state = `LSM_BASEWB;
263 $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);
267 next_lsm_state = `LSM_WBFLUSH;
268 $display("LDMSTM: Stage 3: Writing back");
272 next_lsm_state = `LSM_SETUP;
276 next_lsm_state = 4'bxxxx;
279 $display("LDMSTM: Decoded, bubble %d, insn %08x, lsm state %b -> %b, stall %d", inbubble, insn, lsm_state, next_lsm_state, outstall);
281 `DECODE_LDCSTC: if(!inbubble) begin
282 $display("WARNING: Unimplemented LDCSTC");
284 `DECODE_CDP: if (!inbubble) begin
289 /* XXX undefined instruction trap */
290 $display("WARNING: Possible CDP undefined instruction");
293 `DECODE_MRCMCR: if (!inbubble) begin
298 $display("WARNING: Possible MRCMCR undefined instruction: cp_ack %d, cp_busy %d",cp_ack, cp_busy);
300 $display("MRCMCR: ack %d, busy %d", cp_ack, cp_busy);
306 /* Coprocessor input. */
311 cp_write = 32'hxxxxxxxx;
313 `DECODE_CDP: if(!inbubble) begin
316 `DECODE_MRCMCR: if(!inbubble) begin
318 cp_rnw = insn[20] /* L */;
319 if (insn[20] == 0 /* store to coprocessor */)
325 /* Register output logic. */
328 next_write_reg = write_reg;
329 next_write_num = write_num;
330 next_write_data = write_data;
331 next_outcpsr = lsm_state == 4'b0010 ? outcpsr : cpsr;
332 next_outcpsrup = cpsrup;
335 `DECODE_ALU_SWP: if (!inbubble) begin
336 next_write_reg = 1'bx;
337 next_write_num = 4'bxxxx;
338 next_write_data = 32'hxxxxxxxx;
341 next_write_reg = 1'b0;
343 next_write_reg = 1'b1;
344 next_write_num = insn[15:12];
345 next_write_data = insn[22] ? {24'b0, swp_oldval[7:0]} : swp_oldval;
350 `DECODE_ALU_MULT: begin end
351 `DECODE_ALU_HDATA_REG,
352 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
353 next_write_reg = 1'bx;
354 next_write_num = 4'bxxxx;
355 next_write_data = 32'hxxxxxxxx;
358 next_write_num = insn[15:12];
359 next_write_data = lsrh_rddata;
361 next_write_reg = 1'b1;
365 next_write_reg = 1'b1;
366 next_write_num = insn[19:16];
367 next_write_data = addr;
370 next_write_reg = 1'b0;
374 `DECODE_LDRSTR_UNDEFINED: begin end
375 `DECODE_LDRSTR: if(!inbubble) begin
376 next_write_reg = 1'bx;
377 next_write_num = 4'bxxxx;
378 next_write_data = 32'hxxxxxxxx;
381 next_write_reg = insn[20] /* L */;
382 next_write_num = insn[15:12];
383 if(insn[20] /* L */) begin
384 next_write_data = insn[22] /* B */ ? {24'h0, align_rddata[7:0]} : align_rddata;
388 next_write_reg = 1'b0;
390 next_write_reg = 1'b1;
391 next_write_num = insn[19:16];
392 next_write_data = addr;
395 next_write_reg = 1'b0;
399 `DECODE_LDMSTM: if(!inbubble) begin
400 next_write_reg = 1'bx;
401 next_write_num = 4'bxxxx;
402 next_write_data = 32'hxxxxxxxx;
405 next_write_reg = 1'b0;
408 next_write_reg = !rw_wait;
409 next_write_num = cur_reg;
410 next_write_data = rd_data;
412 next_write_reg = 1'b0;
415 next_write_reg = insn[21] /* writeback */;
416 next_write_num = insn[19:16];
417 next_write_data = insn[23] ? op0 + {26'b0, prev_offset} : op0 - {26'b0, prev_offset};
418 if(cur_reg == 4'hF && insn[22]) begin
424 next_write_reg = 1'b0;
428 `DECODE_MRCMCR: if(!inbubble) begin
429 next_write_reg = 1'bx;
430 next_write_num = 4'bxxxx;
431 next_write_data = 32'hxxxxxxxx;
432 next_outcpsr = 32'hxxxxxxxx;
433 next_outcpsrup = 1'bx;
434 if (insn[20] == 1 /* load from coprocessor */)
435 if (insn[15:12] != 4'hF /* Fuck you ARM */) begin
436 next_write_reg = 1'b1;
437 next_write_num = insn[15:12];
438 next_write_data = cp_read;
440 next_outcpsr = {cp_read[31:28], cpsr[27:0]};
447 /* Bus control logic. */
452 wr_data = 32'hxxxxxxxx;
453 busaddr = 32'hxxxxxxxx;
457 `DECODE_ALU_SWP: if(!inbubble) begin
458 busaddr = {op0[31:2], 2'b0};
459 data_size = insn[22] ? 3'b001 : 3'b100;
465 wr_data = insn[22] ? {4{op1[7:0]}} : op1;
470 `DECODE_ALU_MULT: begin end
471 `DECODE_ALU_HDATA_REG,
472 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
474 /* rotate to correct position */
476 2'b01: begin /* unsigned half */
477 wr_data = {2{op2[15:0]}}; /* XXX need to store halfword */
480 2'b10: begin /* signed byte */
481 wr_data = {4{op2[7:0]}};
484 2'b11: begin /* signed half */
485 wr_data = {2{op2[15:0]}};
489 wr_data = 32'hxxxxxxxx;
499 `LSRH_BASEWB: begin end
500 `LSRH_WBFLUSH: begin end
504 `DECODE_LDRSTR_UNDEFINED: begin end
505 `DECODE_LDRSTR: if(!inbubble) begin
507 wr_data = insn[22] ? {24'h0, {op2[7:0]}} : op2;
508 data_size = insn[22] ? 3'b001 : 3'b100;
511 rd_req = insn[20] /* L */ || insn[22] /* B */;
512 wr_req = !insn[20] /* L */ && !insn[22]/* B */;
517 2'b00: wr_data = {rd_data_latch[31:8], op2[7:0]};
518 2'b01: wr_data = {rd_data_latch[31:16], op2[7:0], rd_data_latch[7:0]};
519 2'b10: wr_data = {rd_data_latch[31:24], op2[7:0], rd_data_latch[15:0]};
520 2'b11: wr_data = {op2[7:0], rd_data_latch[23:0]};
523 `LSR_BASEWB: begin end
524 `LSR_WBFLUSH: begin end
528 `DECODE_LDMSTM: if (!inbubble) begin
531 `LSM_SETUP: begin end
535 wr_data = (cur_reg == 4'hF) ? (pc + 12) : st_data;
538 `LSM_BASEWB: begin end
539 `LSM_WBFLUSH: begin end
543 `DECODE_LDCSTC: begin end
544 `DECODE_CDP: begin end
545 `DECODE_MRCMCR: begin end
553 raddr = 32'hxxxxxxxx;
555 do_rd_data_latch = 0;
557 next_outbubble = inbubble;
560 offset = prev_offset;
561 lsrh_rddata = 32'hxxxxxxxx;
562 lsrh_rddata_s1 = 16'hxxxx;
563 lsrh_rddata_s2 = 8'hxx;
564 next_swp_oldval = swp_oldval;
567 /* XXX shit not given about endianness */
569 `DECODE_ALU_SWP: if(!inbubble) begin
570 next_outbubble = rw_wait;
574 next_swp_oldval = rd_data;
575 `SWP_WRITING: begin end
579 `DECODE_ALU_MULT: begin end
580 `DECODE_ALU_HDATA_REG,
581 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
582 next_outbubble = rw_wait;
583 addr = insn[23] ? op0 + op1 : op0 - op1; /* up/down select */
584 raddr = insn[24] ? op0 : addr; /* pre/post increment */
585 /* rotate to correct position */
587 2'b01: begin /* unsigned half */
588 lsrh_rddata = {16'b0, raddr[1] ? rd_data[31:16] : rd_data[15:0]};
590 2'b10: begin /* signed byte */
591 lsrh_rddata_s1 = raddr[1] ? rd_data[31:16] : rd_data[15:0];
592 lsrh_rddata_s2 = raddr[0] ? lsrh_rddata_s1[15:8] : lsrh_rddata_s1[7:0];
593 lsrh_rddata = {{24{lsrh_rddata_s2[7]}}, lsrh_rddata_s2};
595 2'b11: begin /* signed half */
596 lsrh_rddata = raddr[1] ? {{16{rd_data[31]}}, rd_data[31:16]} : {{16{rd_data[15]}}, rd_data[15:0]};
599 lsrh_rddata = 32'hxxxxxxxx;
604 `LSRH_MEMIO: begin end
606 next_outbubble = 1'b0;
607 `LSRH_WBFLUSH: begin end
611 `DECODE_LDRSTR_UNDEFINED: begin end
612 `DECODE_LDRSTR: if(!inbubble) begin
613 next_outbubble = rw_wait;
614 addr = insn[23] ? op0 + op1 : op0 - op1; /* up/down select */
615 raddr = insn[24] ? addr : op0; /* pre/post increment */
616 /* rotate to correct position */
617 align_s1 = raddr[1] ? {rd_data[15:0], rd_data[31:16]} : rd_data;
618 align_s2 = raddr[0] ? {align_s1[7:0], align_s1[31:8]} : align_s1;
619 /* select byte or word */
620 align_rddata = insn[22] ? {24'b0, align_s2[7:0]} : align_s2;
623 if (insn[22] /* B */ && !insn[20] /* L */)
624 do_rd_data_latch = 1;
625 `LSR_STRB_WR: begin end
628 `LSR_WBFLUSH: begin end
632 /* XXX ldm/stm incorrect in that stupid case where one of the listed regs is the base reg */
633 `DECODE_LDMSTM: if(!inbubble) begin
634 next_outbubble = rw_wait;
637 // next_regs = insn[23] ? op1[15:0] : op1[0:15];
638 /** verilator can suck my dick */
639 next_regs = insn[23] /* U */ ? op1[15:0] : {op1[0], op1[1], op1[2], op1[3], op1[4], op1[5], op1[6], op1[7],
640 op1[8], op1[9], op1[10], op1[11], op1[12], op1[13], op1[14], op1[15]};
645 16'b???????????????1: begin
647 next_regs = {regs[15:1], 1'b0};
649 16'b??????????????10: begin
651 next_regs = {regs[15:2], 2'b0};
653 16'b?????????????100: begin
655 next_regs = {regs[15:3], 3'b0};
657 16'b????????????1000: begin
659 next_regs = {regs[15:4], 4'b0};
661 16'b???????????10000: begin
663 next_regs = {regs[15:5], 5'b0};
665 16'b??????????100000: begin
667 next_regs = {regs[15:6], 6'b0};
669 16'b?????????1000000: begin
671 next_regs = {regs[15:7], 7'b0};
673 16'b????????10000000: begin
675 next_regs = {regs[15:8], 8'b0};
677 16'b???????100000000: begin
679 next_regs = {regs[15:9], 9'b0};
681 16'b??????1000000000: begin
683 next_regs = {regs[15:10], 10'b0};
685 16'b?????10000000000: begin
687 next_regs = {regs[15:11], 11'b0};
689 16'b????100000000000: begin
691 next_regs = {regs[15:12], 12'b0};
693 16'b???1000000000000: begin
695 next_regs = {regs[15:13], 13'b0};
697 16'b??10000000000000: begin
699 next_regs = {regs[15:14], 14'b0};
701 16'b?100000000000000: begin
703 next_regs = {regs[15], 15'b0};
705 16'b1000000000000000: begin
714 cur_reg = insn[23] ? cur_reg : 4'hF - cur_reg;
716 offset = prev_offset + 6'h4;
717 offset_sel = insn[24] ? offset : prev_offset;
718 raddr = insn[23] ? op0 + {26'b0, offset_sel} : op0 - {26'b0, offset_sel};
721 cur_reg = prev_reg; /* whoops, do this one again */
728 `LSM_WBFLUSH: begin end
732 `DECODE_LDCSTC: begin end
733 `DECODE_CDP: if(!inbubble) begin
738 `DECODE_MRCMCR: if(!inbubble) begin
746 if ((flush || delayedflush) && !outstall)
747 next_outbubble = 1'b1;