1 `include "ARM_Constants.v"
10 output reg [31:0] op1,
11 output reg [31:0] op2,
13 output reg [31:0] outspsr,
15 output reg [3:0] read_0,
16 output reg [3:0] read_1,
17 output reg [3:0] read_2,
23 wire [31:0] regs0, regs1, regs2;
25 reg [31:0] op0_out, op1_out, op2_out;
29 wire [31:0] shift_oper;
30 wire [31:0] shift_res;
32 wire [31:0] rotate_res;
34 assign regs0 = (read_0 == 4'b1111) ? rpc : rdata_0;
35 assign regs1 = (read_1 == 4'b1111) ? rpc : rdata_1;
36 assign regs2 = rdata_2; /* use regs2 for things that cannot be r15 */
38 IREALLYHATEARMSHIFT shift(.insn(insn),
41 .cflag_in(incpsr[`CPSR_C]),
43 .cflag_out(shift_cflag_out));
45 SuckLessRotator whirr(.oper({24'b0, insn[7:0]}),
51 `DECODE_ALU_MULT, /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
52 // `DECODE_ALU_MUL_LONG, /* Multiply long */
53 `DECODE_ALU_MRS, /* MRS (Transfer PSR to register) */
54 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
55 `DECODE_ALU_MSR_FLAGS, /* MSR (Transfer register or immediate to PSR, flag bits only) */
56 `DECODE_ALU_SWP, /* Atomic swap */
57 `DECODE_ALU_BX, /* Branch and exchange */
58 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
59 `DECODE_ALU_HDATA_IMM, /* Halfword transfer - register offset */
60 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
61 `DECODE_LDRSTR, /* Single data transfer */
62 `DECODE_LDMSTM, /* Block data transfer */
63 `DECODE_BRANCH, /* Branch */
64 `DECODE_LDCSTC, /* Coprocessor data transfer */
65 `DECODE_CDP, /* Coprocessor data op */
66 `DECODE_MRCMCR, /* Coprocessor register transfer */
67 `DECODE_SWI: /* SWI */
69 `DECODE_ALU: /* ALU */
70 rpc = inpc - (insn[25] ? 8 : (insn[4] ? 12 : 8));
71 default: /* X everything else out */
80 op0_out = 32'hxxxxxxxx;
81 op1_out = 32'hxxxxxxxx;
82 op2_out = 32'hxxxxxxxx;
86 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
88 read_0 = insn[15:12]; /* Rn */
89 read_1 = insn[3:0]; /* Rm */
90 read_2 = insn[11:8]; /* Rs */
96 // `DECODE_ALU_MUL_LONG: /* Multiply long */
98 // read_0 = insn[11:8]; /* Rn */
99 // read_1 = insn[3:0]; /* Rm */
100 // read_2 = 4'b0; /* anyus */
104 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
106 `DECODE_ALU_MSR: /* MSR (Transfer register to PSR) */
108 read_0 = insn[3:0]; /* Rm */
112 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
114 read_0 = insn[3:0]; /* Rm */
116 if(insn[25]) begin /* the constant case */
117 op0_out = rotate_res;
122 `DECODE_ALU_SWP: /* Atomic swap */
124 read_0 = insn[19:16]; /* Rn */
125 read_1 = insn[3:0]; /* Rm */
130 `DECODE_ALU_BX: /* Branch and exchange */
132 read_0 = insn[3:0]; /* Rn */
136 `DECODE_ALU_HDATA_REG: /* Halfword transfer - register offset */
138 read_0 = insn[19:16];
144 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
146 read_0 = insn[19:16];
149 op1_out = {24'b0, insn[11:8], insn[3:0]};
151 `DECODE_ALU: /* ALU */
153 read_0 = insn[19:16]; /* Rn */
154 read_1 = insn[3:0]; /* Rm */
155 read_2 = insn[11:8]; /* Rs for shift */
158 if(insn[25]) begin /* the constant case */
159 carry_out = incpsr[`CPSR_C];
160 op1_out = rotate_res;
162 carry_out = shift_cflag_out;
166 `DECODE_LDRSTR_UNDEFINED: /* Undefined. I hate ARM */
170 `DECODE_LDRSTR: /* Single data transfer */
172 read_0 = insn[19:16]; /* Rn */
173 read_1 = insn[3:0]; /* Rm */
177 op1_out = {20'b0, insn[11:0]};
178 carry_out = incpsr[`CPSR_C];
181 carry_out = shift_cflag_out;
184 `DECODE_LDMSTM: /* Block data transfer */
186 read_0 = insn[19:16];
189 op1_out = {16'b0, insn[15:0]};
191 `DECODE_BRANCH: /* Branch */
193 op0_out = {{6{insn[23]}}, insn[23:0], 2'b0};
195 `DECODE_LDCSTC: /* Coprocessor data transfer */
197 read_0 = insn[19:16];
200 op1_out = {24'b0, insn[7:0]};
202 `DECODE_CDP: /* Coprocessor data op */
205 `DECODE_MRCMCR: /* Coprocessor register transfer */
207 read_0 = insn[15:12];
211 `DECODE_SWI: /* SWI */
215 $display("Undecoded instruction");
220 always @ (posedge clk) begin
221 op0 <= op0_out; /* Rn - always */
222 op1 <= op1_out; /* 'operand 2' - Rm */
223 op2 <= op2_out; /* thirdedge - Rs */
230 module IREALLYHATEARMSHIFT(
232 input [31:0] operand,
233 input [31:0] reg_amt,
235 output reg [31:0] res,
238 wire [5:0] shift_amt;
239 reg is_arith, is_rot;
241 wire [31:0] rshift_res;
243 assign shift_amt = insn[4] ? {|reg_amt[7:5], reg_amt[4:0]} /* reg-specified shift */
244 : {insn[11:7] == 5'b0, insn[11:7]}; /* immediate shift */
246 SuckLessShifter barrel(.oper(operand),
252 .carryout(rshift_cout));
276 case (insn[6:5]) /* shift type */
278 {cflag_out, res} = {cflag_in, operand} << {insn[4] & shift_amt[5], shift_amt[4:0]};
281 cflag_out = rshift_cout;
285 cflag_out = rshift_cout;
288 if(!insn[4] && shift_amt[4:0] == 5'b0) begin /* RRX x.x */
289 res = {cflag_in, operand[31:1]};
290 cflag_out = operand[0];
293 cflag_out = rshift_cout;
299 module SuckLessShifter(
305 output wire [31:0] res,
309 wire [32:0] stage1, stage2, stage3, stage4, stage5;
311 wire pushbits = is_arith & oper[31];
313 /* do a barrel shift */
314 assign stage1 = amt[5] ? {is_rot ? oper : {32{pushbits}}, oper[31]} : {oper, carryin};
315 assign stage2 = amt[4] ? {is_rot ? stage1[16:1] : {16{pushbits}}, stage1[32:17], stage1[16]} : stage1;
316 assign stage3 = amt[3] ? {is_rot ? stage2[8:1] : {8{pushbits}}, stage2[32:9], stage2[8]} : stage2;
317 assign stage4 = amt[2] ? {is_rot ? stage3[4:1] : {4{pushbits}}, stage3[32:5], stage3[4]} : stage3;
318 assign stage5 = amt[1] ? {is_rot ? stage4[2:1] : {2{pushbits}}, stage4[32:3], stage4[2]} : stage4;
319 assign {res, carryout} = amt[0] ? {is_rot ? stage5[1] : pushbits, stage5[32:2], stage5[1]} : stage5;
323 module SuckLessRotator(
326 output wire [31:0] res
329 wire [31:0] stage1, stage2, stage3;
330 assign stage1 = amt[3] ? {oper[15:0], oper[31:16]} : oper;
331 assign stage2 = amt[2] ? {stage1[7:0], stage1[31:8]} : stage1;
332 assign stage3 = amt[1] ? {stage2[3:0], stage2[31:4]} : stage2;
333 assign res = amt[0] ? {stage3[1:0], stage3[31:2]} : stage3;