1 `include "ARM_Constants.v"
10 output reg [31:0] op2,
13 output reg [3:0] read_0,
14 output reg [3:0] read_1,
15 output reg [3:0] read_2,
21 wire [31:0] regs0, regs1, regs2;
23 reg [31:0] op0_out, op1_out, op2_out;
27 wire [31:0] shift_oper;
28 wire [31:0] shift_res;
30 wire [31:0] rotate_res;
32 assign regs0 = (read_0 == 4'b1111) ? rpc : rdata_0;
33 assign regs1 = (read_1 == 4'b1111) ? rpc : rdata_1;
34 assign regs2 = rdata_2; /* use regs2 for things that cannot be r15 */
36 IREALLYHATEARMSHIFT blowme(.insn(insn),
39 .cflag_in(incpsr[`CPSR_C]),
41 .cflag_out(shift_cflag_out));
43 SuckLessRotator whirr(.oper({24'b0, insn[7:0]}),
49 `DECODE_ALU_MULT, /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
50 // `DECODE_ALU_MUL_LONG, /* Multiply long */
51 `DECODE_ALU_MRS, /* MRS (Transfer PSR to register) */
52 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
53 `DECODE_ALU_MSR_FLAGS, /* MSR (Transfer register or immediate to PSR, flag bits only) */
54 `DECODE_ALU_SWP, /* Atomic swap */
55 `DECODE_ALU_BX, /* Branch and exchange */
56 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
57 `DECODE_ALU_HDATA_IMM, /* Halfword transfer - register offset */
58 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
59 `DECODE_LDRSTR, /* Single data transfer */
60 `DECODE_LDMSTM, /* Block data transfer */
61 `DECODE_BRANCH, /* Branch */
62 `DECODE_LDCSTC, /* Coprocessor data transfer */
63 `DECODE_CDP, /* Coprocessor data op */
64 `DECODE_MRCMCR, /* Coprocessor register transfer */
65 `DECODE_SWI: /* SWI */
67 `DECODE_ALU: /* ALU */
68 rpc = inpc - (insn[25] ? 8 : (insn[4] ? 12 : 8));
69 default: /* X everything else out */
79 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
81 read_0 = insn[15:12]; /* Rn */
82 read_1 = insn[3:0]; /* Rm */
83 read_2 = insn[11:8]; /* Rs */
85 // `DECODE_ALU_MUL_LONG: /* Multiply long */
86 // read_0 = insn[11:8]; /* Rn */
87 // read_1 = insn[3:0]; /* Rm */
88 // read_2 = 4'b0; /* anyus */
89 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
91 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
92 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
93 read_0 = insn[3:0]; /* Rm */
94 `DECODE_ALU_SWP: /* Atomic swap */
96 read_0 = insn[19:16]; /* Rn */
97 read_1 = insn[3:0]; /* Rm */
99 `DECODE_ALU_BX: /* Branch and exchange */
100 read_0 = insn[3:0]; /* Rn */
101 `DECODE_ALU_HDATA_REG: /* Halfword transfer - register offset */
103 read_0 = insn[19:16];
106 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
108 read_0 = insn[19:16];
110 `DECODE_ALU: /* ALU */
112 read_0 = insn[19:16]; /* Rn */
113 read_1 = insn[3:0]; /* Rm */
114 read_2 = insn[11:8]; /* Rs for shift */
116 `DECODE_LDRSTR_UNDEFINED: /* Undefined. I hate ARM */
118 `DECODE_LDRSTR: /* Single data transfer */
120 read_0 = insn[19:16]; /* Rn */
121 read_1 = insn[3:0]; /* Rm */
123 `DECODE_LDMSTM: /* Block data transfer */
124 read_0 = insn[19:16];
125 `DECODE_BRANCH: /* Branch */
127 `DECODE_LDCSTC: /* Coprocessor data transfer */
128 read_0 = insn[19:16];
129 `DECODE_CDP: /* Coprocessor data op */
131 `DECODE_MRCMCR: /* Coprocessor register transfer */
132 read_0 = insn[15:12];
133 `DECODE_SWI: /* SWI */
136 $display("Undecoded instruction");
141 op0_out = 32'hxxxxxxxx;
142 op1_out = 32'hxxxxxxxx;
143 op2_out = 32'hxxxxxxxx;
146 `DECODE_ALU_MULT: begin /* Multiply */
151 // `DECODE_ALU_MULT_LONG: begin /* Multiply long */
154 `DECODE_ALU_MRS: begin /* MRS (Transfer PSR to register) */
156 `DECODE_ALU_MSR: begin /* MSR (Transfer register to PSR) */
159 `DECODE_ALU_MSR_FLAGS: begin /* MSR (Transfer register or immediate to PSR, flag bits only) */
160 if(insn[25]) begin /* the constant case */
161 op0_out = rotate_res;
166 `DECODE_ALU_SWP: begin /* Atomic swap */
170 `DECODE_ALU_BX: begin /* Branch and exchange */
173 `DECODE_ALU_HDATA_REG: begin /* Halfword transfer - register offset */
177 `DECODE_ALU_HDATA_IMM: begin /* Halfword transfer - immediate offset */
179 op1_out = {24'b0, insn[11:8], insn[3:0]};
181 `DECODE_ALU: begin /* ALU */
183 if(insn[25]) begin /* the constant case */
184 carry_out = incpsr[`CPSR_C];
185 op1_out = rotate_res;
187 carry_out = shift_cflag_out;
191 `DECODE_LDRSTR_UNDEFINED: begin /* Undefined. I hate ARM */
194 `DECODE_LDRSTR: begin /* Single data transfer */
197 op1_out = {20'b0, insn[11:0]};
198 carry_out = incpsr[`CPSR_C];
201 carry_out = shift_cflag_out;
204 `DECODE_LDMSTM: begin /* Block data transfer */
206 op1_out = {16'b0, insn[15:0]};
208 `DECODE_BRANCH: begin /* Branch */
209 op0_out = {{6{insn[23]}}, insn[23:0], 2'b0};
211 `DECODE_LDCSTC: begin /* Coprocessor data transfer */
213 op1_out = {24'b0, insn[7:0]};
215 `DECODE_CDP: begin /* Coprocessor data op */
217 `DECODE_MRCMCR: begin /* Coprocessor register transfer */
220 `DECODE_SWI: begin /* SWI */
226 always @ (posedge clk) begin
227 op0 <= op0_out; /* Rn - always */
228 op1 <= op1_out; /* 'operand 2' - Rm */
229 op2 <= op2_out; /* thirdedge - Rs */
235 module IREALLYHATEARMSHIFT(
237 input [31:0] operand,
238 input [31:0] reg_amt,
240 output reg [31:0] res,
243 wire [5:0] shift_amt;
244 reg is_arith, is_rot;
246 wire [31:0] rshift_res;
248 assign shift_amt = insn[4] ? {|reg_amt[7:5], reg_amt[4:0]} /* reg-specified shift */
249 : {insn[11:7] == 5'b0, insn[11:7]}; /* immediate shift */
251 SuckLessShifter biteme(.oper(operand),
257 .carryout(rshift_cout));
281 case (insn[6:5]) /* shift type */
283 {cflag_out, res} = {cflag_in, operand} << {insn[4] & shift_amt[5], shift_amt[4:0]};
286 cflag_out = rshift_cout;
290 cflag_out = rshift_cout;
293 if(!insn[4] && shift_amt[4:0] == 5'b0) begin /* RRX x.x */
294 res = {cflag_in, operand[31:1]};
295 cflag_out = operand[0];
298 cflag_out = rshift_cout;
304 module SuckLessShifter(
310 output wire [31:0] res,
314 wire [32:0] stage1, stage2, stage3, stage4, stage5;
316 wire pushbits = is_arith & oper[31];
318 /* do a barrel shift */
319 assign stage1 = amt[5] ? {is_rot ? oper : {32{pushbits}}, oper[31]} : {oper, carryin};
320 assign stage2 = amt[4] ? {is_rot ? stage1[16:1] : {16{pushbits}}, stage1[32:17], stage1[16]} : stage1;
321 assign stage3 = amt[3] ? {is_rot ? stage2[8:1] : {8{pushbits}}, stage2[32:9], stage2[8]} : stage2;
322 assign stage4 = amt[2] ? {is_rot ? stage3[4:1] : {4{pushbits}}, stage3[32:5], stage3[4]} : stage3;
323 assign stage5 = amt[1] ? {is_rot ? stage4[2:1] : {2{pushbits}}, stage4[32:3], stage4[2]} : stage4;
324 assign {res, carryout} = amt[0] ? {is_rot ? stage5[1] : pushbits, stage5[32:2], stage5[1]} : stage5;
328 module SuckLessRotator(
331 output wire [31:0] res
334 wire [31:0] stage1, stage2, stage3;
335 assign stage1 = amt[3] ? {oper[15:0], oper[31:16]} : oper;
336 assign stage2 = amt[2] ? {stage1[7:0], stage1[31:8]} : stage1;
337 assign stage3 = amt[1] ? {stage2[3:0], stage2[31:4]} : stage2;
338 assign res = amt[0] ? {stage3[1:0], stage3[31:2]} : stage3;