1 `include "ARM_Constants.v"
8 output reg [31:0] busaddr,
12 output reg [31:0] wr_data,
15 /* regfile interface */
16 output reg [3:0] st_read,
19 /* Coprocessor interface */
23 output cp_rnw, /* 1 = read from CP, 0 = write to CP */
25 output reg [31:0] cp_write,
37 input [3:0] write_num,
38 input [31:0] write_data,
43 output reg [31:0] outpc,
44 output reg [31:0] outinsn,
45 output reg out_write_reg = 1'b0,
46 output reg [3:0] out_write_num = 4'bxxxx,
47 output reg [31:0] out_write_data = 32'hxxxxxxxx,
48 output reg [31:0] out_spsr = 32'hxxxxxxxx,
49 output reg [31:0] out_cpsr = 32'hxxxxxxxx
52 reg [31:0] addr, raddr, prev_raddr, next_regdata, next_outcpsr;
54 reg [3:0] next_regsel, cur_reg, prev_reg;
59 wire [3:0] next_write_num;
60 wire [31:0] next_write_data;
62 reg [1:0] lsr_state = 2'b01, next_lsr_state;
63 reg [31:0] align_s1, align_s2, align_rddata;
65 reg [1:0] lsrh_state = 2'b01, next_lsrh_state;
66 reg [31:0] lsrh_rddata;
67 reg [15:0] lsrh_rddata_s1;
68 reg [7:0] lsrh_rddata_s2;
70 reg [15:0] regs, next_regs;
71 reg [2:0] lsm_state = 3'b001, next_lsm_state;
72 reg [5:0] offset, prev_offset, offset_sel;
74 reg [31:0] swp_oldval, next_swp_oldval;
75 reg [1:0] swp_state = 2'b01, next_swp_state;
81 outbubble <= next_outbubble;
82 out_write_reg <= next_write_reg;
83 out_write_num <= next_write_num;
84 out_write_data <= next_write_data;
87 prev_offset <= offset;
89 out_cpsr <= next_outcpsr;
91 swp_state <= next_swp_state;
92 lsm_state <= next_lsm_state;
93 lsr_state <= next_lsr_state;
94 lsrh_state <= next_lsrh_state;
101 raddr = 32'hxxxxxxxx;
104 wr_data = 32'hxxxxxxxx;
105 busaddr = 32'hxxxxxxxx;
107 next_write_reg = write_reg;
108 next_write_num = write_num;
109 next_write_data = write_data;
110 next_outbubble = inbubble;
115 cp_write = 32'hxxxxxxxx;
116 offset = prev_offset;
117 next_outcpsr = lsm_state == 3'b010 ? out_cpsr : cpsr;
118 lsrh_rddata = 32'hxxxxxxxx;
119 next_lsm_state = lsm_state;
120 next_lsr_state = lsr_state;
121 next_lsrh_state = lsrh_state;
122 next_swp_oldval = swp_oldval;
123 next_swp_state = swp_state;
126 /* XXX shit not given about endianness */
129 `DECODE_ALU_SWP: if(!inbubble) begin
131 next_outbubble = rw_wait;
132 busaddr = {op0[31:2], 2'b0};
138 next_swp_state = 2'b10;
139 next_swp_oldval = rd_data;
145 next_write_reg = 1'b1;
146 next_write_num = insn[15:12];
147 next_write_data = swp_oldval;
149 next_swp_state = 2'b01;
154 `DECODE_ALU_HDATA_REG,
155 `DECODE_ALU_HDATA_IMM: if(!inbubble) begin
156 next_outbubble = rw_wait;
158 addr = insn[23] ? op0 + op1 : op0 - op1; /* up/down select */
159 raddr = insn[24] ? op0 : addr; /* pre/post increment */
161 /* rotate to correct position */
163 2'b00: begin end /* swp */
164 2'b01: begin /* unsigned half */
165 wr_data = {2{op2[15:0]}}; /* XXX need to store halfword */
166 lsrh_rddata = {16'b0, raddr[1] ? rd_data[31:16] : rd_data[15:0]};
168 2'b10: begin /* signed byte */
169 wr_data = {4{op2[7:0]}};
170 lsrh_rddata_s1 = raddr[1] ? rd_data[31:16] : rd_data[15:0];
171 lsrh_rddata_s2 = raddr[0] ? lsrh_rddata_s1[15:8] : lsrh_rddata_s1[7:0];
172 lsrh_rddata = {{24{lsrh_rddata_s2[7]}}, lsrh_rddata_s2};
174 2'b11: begin /* signed half */
175 wr_data = {2{op2[15:0]}};
176 lsrh_rddata = raddr[1] ? {{16{rd_data[31]}}, rd_data[31:16]} : {{16{rd_data[15]}}, rd_data[15:0]};
184 next_write_num = insn[15:12];
185 next_write_data = lsrh_rddata;
187 next_write_reg = 1'b1;
189 if(insn[21] | !insn[24]) begin
192 next_lsrh_state = 2'b10;
196 next_write_reg = 1'b1;
197 next_write_num = insn[19:16];
198 next_write_data = addr;
199 next_lsrh_state = 2'b10;
204 `DECODE_LDRSTR_UNDEFINED: begin end
205 `DECODE_LDRSTR: if(!inbubble) begin
206 next_outbubble = rw_wait;
208 addr = insn[23] ? op0 + op1 : op0 - op1; /* up/down select */
209 raddr = insn[24] ? op0 : addr; /* pre/post increment */
211 /* rotate to correct position */
212 align_s1 = raddr[1] ? {rd_data[15:0], rd_data[31:16]} : rd_data;
213 align_s2 = raddr[0] ? {align_s1[7:0], align_s1[31:8]} : align_s1;
214 /* select byte or word */
215 align_rddata = insn[22] ? {24'b0, align_s2[7:0]} : align_s2;
216 wr_data = insn[22] ? {4{op2[7:0]}} : op2; /* XXX need to actually store just a byte */
221 next_write_reg = 1'b1;
222 next_write_num = insn[15:12];
224 next_write_data = align_rddata;
226 if(insn[21] | !insn[24]) begin
229 next_lsr_state = 2'b10;
233 next_write_reg = 1'b1;
234 next_write_num = insn[19:16];
235 next_write_data = addr;
236 next_lsr_state = 2'b10;
241 /* XXX ldm/stm incorrect in that stupid case where one of the listed regs is the base reg */
242 `DECODE_LDMSTM: if(!inbubble) begin
244 next_outbubble = rw_wait;
247 // next_regs = insn[23] ? op1[15:0] : op1[0:15];
248 /** verilator can suck my dick */
249 next_regs = insn[23] ? op1[15:0] : {op1[0], op1[1], op1[2], op1[3], op1[4], op1[5], op1[6], op1[7],
250 op1[8], op1[9], op1[10], op1[11], op1[12], op1[13], op1[14], op1[15]};
253 next_lsm_state = 3'b010;
259 16'b???????????????1: begin
261 next_regs = {regs[15:1], 1'b0};
263 16'b??????????????10: begin
265 next_regs = {regs[15:2], 2'b0};
267 16'b?????????????100: begin
269 next_regs = {regs[15:3], 3'b0};
271 16'b????????????1000: begin
273 next_regs = {regs[15:4], 4'b0};
275 16'b???????????10000: begin
277 next_regs = {regs[15:5], 5'b0};
279 16'b??????????100000: begin
281 next_regs = {regs[15:6], 6'b0};
283 16'b?????????1000000: begin
285 next_regs = {regs[15:7], 7'b0};
287 16'b????????10000000: begin
289 next_regs = {regs[15:8], 8'b0};
291 16'b???????100000000: begin
293 next_regs = {regs[15:9], 9'b0};
295 16'b??????1000000000: begin
297 next_regs = {regs[15:10], 10'b0};
299 16'b?????10000000000: begin
301 next_regs = {regs[15:11], 11'b0};
303 16'b????100000000000: begin
305 next_regs = {regs[15:12], 12'b0};
307 16'b???1000000000000: begin
309 next_regs = {regs[15:13], 13'b0};
311 16'b??10000000000000: begin
313 next_regs = {regs[15:14], 14'b0};
315 16'b?100000000000000: begin
317 next_regs = {regs[15], 15'b0};
319 16'b1000000000000000: begin
328 cur_reg = insn[23] ? 4'hF - cur_reg : cur_reg;
329 if(cur_reg == 4'hF && insn[22]) begin
339 offset = prev_offset + 6'h4;
340 offset_sel = insn[24] ? offset : prev_offset;
341 raddr = insn[23] ? op0 + {26'b0, offset_sel} : op0 - {26'b0, offset_sel};
343 next_write_reg = 1'b1;
344 next_write_num = cur_reg;
345 next_write_data = rd_data;
355 if(next_regs == 16'b0) begin
356 next_lsm_state = 3'b100;
360 next_write_reg = 1'b1;
361 next_write_num = insn[19:16];
362 next_write_data = insn[23] ? op0 + {26'b0, prev_offset} : op0 - {26'b0, prev_offset};
363 next_lsm_state = 3'b001;
368 `DECODE_LDCSTC: if(!inbubble) begin
369 $display("WARNING: Unimplemented LDCSTC");
371 `DECODE_CDP: if(!inbubble) begin
378 /* XXX undefined instruction trap */
379 $display("WARNING: Possible CDP undefined instruction");
382 `DECODE_MRCMCR: if(!inbubble) begin
384 cp_rnw = insn[20] /* L */;
385 if (insn[20] == 0 /* store to coprocessor */)
388 if (insn[15:12] != 4'hF /* Fuck you ARM */) begin
389 next_write_reg = 1'b1;
390 next_write_num = insn[15:12];
391 next_write_data = cp_read;
393 next_outcpsr = {cp_read[31:28], cpsr[27:0]};
400 $display("WARNING: Possible MRCMCR undefined instruction: cp_ack %d, cp_busy %d",cp_ack, cp_busy);
402 $display("MRCMCR: ack %d, busy %d", cp_ack, cp_busy);