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;
113 prev_offset <= offset;
115 outcpsr <= next_outcpsr;
117 outcpsrup <= next_outcpsrup;
118 swp_state <= next_swp_state;
119 lsm_state <= next_lsm_state;
120 lsr_state <= next_lsr_state;
121 lsrh_state <= next_lsrh_state;
122 if (do_rd_data_latch)
123 rd_data_latch <= rd_data;
124 swp_oldval <= next_swp_oldval;
128 reg delayedflush = 0;
129 always @(posedge clk)
130 if (flush && outstall /* halp! I can't do it now, maybe later? */)
132 else if (!outstall /* anything has been handled this time around */)
135 /* Drive the state machines and stall. */
139 next_lsm_state = lsm_state;
140 next_lsr_state = lsr_state;
141 next_lsrh_state = lsrh_state;
142 next_swp_state = swp_state;
144 `DECODE_ALU_SWP: if(!inbubble) begin
149 next_swp_state = `SWP_WRITING;
150 $display("SWP: read stage");
155 next_swp_state = `SWP_READING;
156 $display("SWP: write stage");
160 next_swp_state = 2'bxx;
164 `DECODE_ALU_MULT: begin
165 outstall = 1'b0; /* XXX work around for Xilinx bug */
166 next_lsrh_state = lsrh_state;
168 `DECODE_ALU_HDATA_REG,
169 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
173 if(insn[21] | !insn[24]) begin
176 next_lsrh_state = `LSRH_BASEWB;
179 if (flush) /* special case! */ begin
181 next_lsrh_state = `LSRH_MEMIO;
184 $display("ALU_LDRSTRH: rd_req %d, wr_req %d", rd_req, wr_req);
188 next_lsrh_state = `LSRH_WBFLUSH;
192 next_lsrh_state = `LSRH_MEMIO;
196 next_lsrh_state = 3'bxxx;
200 `DECODE_LDRSTR_UNDEFINED: begin end
201 `DECODE_LDRSTR: if(!inbubble) begin
206 next_lsr_state = `LSR_MEMIO;
207 if (insn[22] /* B */ && !insn[20] /* L */) begin /* i.e., strb */
210 next_lsr_state = `LSR_STRB_WR;
211 end else if (insn[21] /* W */ || !insn[24] /* P */) begin /* writeback needed */
214 next_lsr_state = `LSR_BASEWB;
219 next_lsr_state = `LSR_MEMIO;
221 $display("LDRSTR: rd_req %d, wr_req %d, raddr %08x, wait %d", rd_req, wr_req, raddr, rw_wait);
225 if(insn[21] /* W */ | !insn[24] /* P */) begin
227 next_lsr_state = `LSR_BASEWB;
228 end else if (!rw_wait)
229 next_lsr_state = `LSR_WBFLUSH;
230 $display("LDRSTR: Handling STRB");
234 next_lsr_state = `LSR_WBFLUSH;
238 next_lsr_state = `LSR_MEMIO;
242 next_lsr_state = 4'bxxxx;
245 $display("LDRSTR: Decoded, bubble %d, insn %08x, lsm state %b -> %b, stall %d", inbubble, insn, lsr_state, next_lsr_state, outstall);
247 `DECODE_LDMSTM: if(!inbubble) begin
252 next_lsm_state = `LSM_MEMIO;
255 next_lsm_state = `LSM_SETUP;
257 $display("LDMSTM: Round 1: base register: %08x, reg list %b", op0, op1[15:0]);
261 if(next_regs == 16'b0 && !rw_wait) begin
262 next_lsm_state = `LSM_BASEWB;
265 $display("LDMSTM: Stage 2: Writing: regs %b, next_regs %b, reg %d, wr_data %08x, addr %08x", regs, next_regs, cur_reg, st_data, busaddr);
269 next_lsm_state = `LSM_WBFLUSH;
270 $display("LDMSTM: Stage 3: Writing back");
274 next_lsm_state = `LSM_SETUP;
278 next_lsm_state = 4'bxxxx;
281 $display("LDMSTM: Decoded, bubble %d, insn %08x, lsm state %b -> %b, stall %d", inbubble, insn, lsm_state, next_lsm_state, outstall);
283 `DECODE_LDCSTC: if(!inbubble) begin
284 $display("WARNING: Unimplemented LDCSTC");
286 `DECODE_CDP: if (!inbubble) begin
291 /* XXX undefined instruction trap */
292 $display("WARNING: Possible CDP undefined instruction");
295 `DECODE_MRCMCR: if (!inbubble) begin
300 $display("WARNING: Possible MRCMCR undefined instruction: cp_ack %d, cp_busy %d",cp_ack, cp_busy);
302 $display("MRCMCR: ack %d, busy %d", cp_ack, cp_busy);
308 /* Coprocessor input. */
313 cp_write = 32'hxxxxxxxx;
315 `DECODE_CDP: if(!inbubble) begin
318 `DECODE_MRCMCR: if(!inbubble) begin
320 cp_rnw = insn[20] /* L */;
321 if (insn[20] == 0 /* store to coprocessor */)
327 /* Register output logic. */
330 next_write_reg = write_reg;
331 next_write_num = write_num;
332 next_write_data = write_data;
333 next_outcpsr = lsm_state == 4'b0010 ? outcpsr : cpsr;
334 next_outcpsrup = cpsrup;
337 `DECODE_ALU_SWP: if (!inbubble) begin
338 next_write_reg = 1'bx;
339 next_write_num = 4'bxxxx;
340 next_write_data = 32'hxxxxxxxx;
343 next_write_reg = 1'b0;
345 next_write_reg = 1'b1;
346 next_write_num = insn[15:12];
347 next_write_data = insn[22] ? {24'b0, swp_oldval[7:0]} : swp_oldval;
352 `DECODE_ALU_MULT: begin
353 next_write_reg = write_reg; /* XXX workaround for ISE 10.1 bug */
354 next_write_num = write_num;
355 next_write_data = write_data;
356 next_outcpsr = lsm_state == 4'b0010 ? outcpsr : cpsr;
357 next_outcpsrup = cpsrup;
359 `DECODE_ALU_HDATA_REG,
360 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
361 next_write_reg = 1'bx;
362 next_write_num = 4'bxxxx;
363 next_write_data = 32'hxxxxxxxx;
366 next_write_num = insn[15:12];
367 next_write_data = lsrh_rddata;
369 next_write_reg = 1'b1;
373 next_write_reg = 1'b1;
374 next_write_num = insn[19:16];
375 next_write_data = addr;
378 next_write_reg = 1'b0;
382 `DECODE_LDRSTR_UNDEFINED: begin end
383 `DECODE_LDRSTR: if(!inbubble) begin
384 next_write_reg = 1'bx;
385 next_write_num = 4'bxxxx;
386 next_write_data = 32'hxxxxxxxx;
389 next_write_reg = insn[20] /* L */;
390 next_write_num = insn[15:12];
391 if(insn[20] /* L */) begin
392 next_write_data = insn[22] /* B */ ? {24'h0, align_rddata[7:0]} : align_rddata;
396 next_write_reg = 1'b0;
398 next_write_reg = 1'b1;
399 next_write_num = insn[19:16];
400 next_write_data = addr;
403 next_write_reg = 1'b0;
407 `DECODE_LDMSTM: if(!inbubble) begin
408 next_write_reg = 1'bx;
409 next_write_num = 4'bxxxx;
410 next_write_data = 32'hxxxxxxxx;
413 next_write_reg = 1'b0;
415 if(insn[20] /* L */) begin
416 next_write_reg = !rw_wait;
417 next_write_num = cur_reg;
418 next_write_data = rd_data;
420 next_write_reg = 1'b0;
423 next_write_reg = insn[21] /* writeback */;
424 next_write_num = insn[19:16];
425 next_write_data = insn[23] ? op0 + {26'b0, prev_offset} : op0 - {26'b0, prev_offset};
426 if(cur_reg == 4'hF && insn[22]) begin
432 next_write_reg = 1'b0;
436 `DECODE_MRCMCR: if(!inbubble) begin
437 next_write_reg = 1'bx;
438 next_write_num = 4'bxxxx;
439 next_write_data = 32'hxxxxxxxx;
440 next_outcpsr = 32'hxxxxxxxx;
441 next_outcpsrup = 1'bx;
442 if (insn[20] == 1 /* load from coprocessor */)
443 if (insn[15:12] != 4'hF /* Fuck you ARM */) begin
444 next_write_reg = 1'b1;
445 next_write_num = insn[15:12];
446 next_write_data = cp_read;
448 next_outcpsr = {cp_read[31:28], cpsr[27:0]};
455 /* Bus/address control logic. */
460 offset = prev_offset;
462 raddr = 32'hxxxxxxxx;
463 busaddr = 32'hxxxxxxxx;
467 `DECODE_ALU_SWP: if(!inbubble) begin
468 busaddr = {op0[31:2], 2'b0};
469 data_size = insn[22] ? 3'b001 : 3'b100;
478 `DECODE_ALU_MULT: begin
479 rd_req = 1'b0; /* XXX workaround for Xilinx bug */
481 offset = prev_offset;
484 `DECODE_ALU_HDATA_REG,
485 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
486 addr = insn[23] ? op0 + op1 : op0 - op1; /* up/down select */
487 raddr = insn[24] ? op0 : addr; /* pre/post increment */
489 /* rotate to correct position */
491 2'b01: /* unsigned half */
493 2'b10: /* signed byte */
495 2'b11: /* signed half */
507 `LSRH_BASEWB: begin end
508 `LSRH_WBFLUSH: begin end
512 `DECODE_LDRSTR_UNDEFINED: begin end
513 `DECODE_LDRSTR: if(!inbubble) begin
514 addr = insn[23] ? op0 + op1 : op0 - op1; /* up/down select */
515 raddr = insn[24] ? addr : op0; /* pre/post increment */
517 data_size = insn[22] ? 3'b001 : 3'b100;
520 rd_req = insn[20] /* L */ || insn[22] /* B */;
521 wr_req = !insn[20] /* L */ && !insn[22]/* B */;
525 `LSR_BASEWB: begin end
526 `LSR_WBFLUSH: begin end
530 `DECODE_LDMSTM: if (!inbubble) begin
538 offset = prev_offset + 6'h4;
539 offset_sel = insn[24] ? offset : prev_offset;
540 raddr = insn[23] ? op0 + {26'b0, offset_sel} : op0 - {26'b0, offset_sel};
543 `LSM_BASEWB: begin end
544 `LSM_WBFLUSH: begin end
548 `DECODE_LDCSTC: begin end
549 `DECODE_CDP: begin end
550 `DECODE_MRCMCR: begin end
555 /* Bus data control logic. */
558 wr_data = 32'hxxxxxxxx;
561 `DECODE_ALU_SWP: if(!inbubble)
562 if (swp_state == `SWP_WRITING)
563 wr_data = insn[22] ? {4{op1[7:0]}} : op1;
564 `DECODE_ALU_MULT: begin end
565 `DECODE_ALU_HDATA_REG,
566 `DECODE_ALU_HDATA_IMM: if(!inbubble)
568 2'b01: /* unsigned half */
569 wr_data = {2{op2[15:0]}}; /* XXX need to store halfword */
570 2'b10: /* signed byte */
571 wr_data = {4{op2[7:0]}};
572 2'b11: /* signed half */
573 wr_data = {2{op2[15:0]}};
576 `DECODE_LDRSTR_UNDEFINED: begin end
577 `DECODE_LDRSTR: if(!inbubble) begin
578 wr_data = insn[22] ? {24'h0, {op2[7:0]}} : op2;
579 if (lsr_state == `LSR_STRB_WR)
581 2'b00: wr_data = {rd_data_latch[31:8], op2[7:0]};
582 2'b01: wr_data = {rd_data_latch[31:16], op2[7:0], rd_data_latch[7:0]};
583 2'b10: wr_data = {rd_data_latch[31:24], op2[7:0], rd_data_latch[15:0]};
584 2'b11: wr_data = {op2[7:0], rd_data_latch[23:0]};
587 `DECODE_LDMSTM: if (!inbubble)
588 if (lsm_state == `LSM_MEMIO)
589 wr_data = (cur_reg == 4'hF) ? (pc + 12) : st_data;
590 `DECODE_LDCSTC: begin end
591 `DECODE_CDP: begin end
592 `DECODE_MRCMCR: begin end
597 /* LDM/STM register control logic. */
598 always @(posedge clk)
599 if (!rw_wait || lsm_state != `LSM_MEMIO)
612 `DECODE_LDMSTM: if(!inbubble) begin
615 next_regs = insn[23] /* U */ ? op1[15:0] : {op1[0], op1[1], op1[2], op1[3], op1[4], op1[5], op1[6], op1[7],
616 op1[8], op1[9], op1[10], op1[11], op1[12], op1[13], op1[14], op1[15]};
619 16'b???????????????1: begin
621 next_regs = {regs[15:1], 1'b0};
623 16'b??????????????10: begin
625 next_regs = {regs[15:2], 2'b0};
627 16'b?????????????100: begin
629 next_regs = {regs[15:3], 3'b0};
631 16'b????????????1000: begin
633 next_regs = {regs[15:4], 4'b0};
635 16'b???????????10000: begin
637 next_regs = {regs[15:5], 5'b0};
639 16'b??????????100000: begin
641 next_regs = {regs[15:6], 6'b0};
643 16'b?????????1000000: begin
645 next_regs = {regs[15:7], 7'b0};
647 16'b????????10000000: begin
649 next_regs = {regs[15:8], 8'b0};
651 16'b???????100000000: begin
653 next_regs = {regs[15:9], 9'b0};
655 16'b??????1000000000: begin
657 next_regs = {regs[15:10], 10'b0};
659 16'b?????10000000000: begin
661 next_regs = {regs[15:11], 11'b0};
663 16'b????100000000000: begin
665 next_regs = {regs[15:12], 12'b0};
667 16'b???1000000000000: begin
669 next_regs = {regs[15:13], 13'b0};
671 16'b??10000000000000: begin
673 next_regs = {regs[15:14], 14'b0};
675 16'b?100000000000000: begin
677 next_regs = {regs[15], 15'b0};
679 16'b1000000000000000: begin
688 cur_reg = insn[23] ? cur_reg : 4'hF - cur_reg;
692 `LSM_BASEWB: begin end
693 `LSM_WBFLUSH: begin end
702 do_rd_data_latch = 0;
704 next_outbubble = inbubble;
706 lsrh_rddata = 32'hxxxxxxxx;
707 lsrh_rddata_s1 = 16'hxxxx;
708 lsrh_rddata_s2 = 8'hxx;
709 next_swp_oldval = swp_oldval;
711 align_s1 = 32'hxxxxxxxx;
712 align_s2 = 32'hxxxxxxxx;
713 align_rddata = 32'hxxxxxxxx;
715 /* XXX shit not given about endianness */
717 `DECODE_ALU_SWP: if(!inbubble) begin
718 next_outbubble = rw_wait;
722 next_swp_oldval = rd_data;
723 `SWP_WRITING: begin end
727 `DECODE_ALU_MULT: begin
728 next_outbubble = inbubble; /* XXX workaround for Xilinx bug */
730 `DECODE_ALU_HDATA_REG,
731 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
732 next_outbubble = rw_wait;
734 /* rotate to correct position */
736 2'b01: begin /* unsigned half */
737 lsrh_rddata = {16'b0, raddr[1] ? rd_data[31:16] : rd_data[15:0]};
739 2'b10: begin /* signed byte */
740 lsrh_rddata_s1 = raddr[1] ? rd_data[31:16] : rd_data[15:0];
741 lsrh_rddata_s2 = raddr[0] ? lsrh_rddata_s1[15:8] : lsrh_rddata_s1[7:0];
742 lsrh_rddata = {{24{lsrh_rddata_s2[7]}}, lsrh_rddata_s2};
744 2'b11: begin /* signed half */
745 lsrh_rddata = raddr[1] ? {{16{rd_data[31]}}, rd_data[31:16]} : {{16{rd_data[15]}}, rd_data[15:0]};
748 lsrh_rddata = 32'hxxxxxxxx;
753 `LSRH_MEMIO: begin end
755 next_outbubble = 1'b0;
756 `LSRH_WBFLUSH: begin end
760 `DECODE_LDRSTR_UNDEFINED: begin end
761 `DECODE_LDRSTR: if(!inbubble) begin
762 next_outbubble = rw_wait;
763 /* rotate to correct position */
764 align_s1 = raddr[1] ? {rd_data[15:0], rd_data[31:16]} : rd_data;
765 align_s2 = raddr[0] ? {align_s1[7:0], align_s1[31:8]} : align_s1;
766 /* select byte or word */
767 align_rddata = insn[22] ? {24'b0, align_s2[7:0]} : align_s2;
770 if (insn[22] /* B */ && !insn[20] /* L */)
771 do_rd_data_latch = 1;
772 `LSR_STRB_WR: begin end
775 `LSR_WBFLUSH: begin end
779 /* XXX ldm/stm incorrect in that stupid case where one of the listed regs is the base reg */
780 `DECODE_LDMSTM: if(!inbubble) begin
781 next_outbubble = rw_wait;
783 `LSM_SETUP: begin end
784 `LSM_MEMIO: begin end
787 `LSM_WBFLUSH: begin end
791 `DECODE_LDCSTC: begin end
792 `DECODE_CDP: if(!inbubble) begin
797 `DECODE_MRCMCR: if(!inbubble) begin
805 if ((flush || delayedflush) && !outstall)
806 next_outbubble = 1'b1;