1 `include "ARM_Constants.v"
10 output reg [31:0] op1,
11 output reg [31:0] op2,
13 output reg [31:0] outcpsr,
14 output reg [31:0] outspsr,
16 output reg [3:0] read_0,
17 output reg [3:0] read_1,
18 output reg [3:0] read_2,
24 wire [31:0] regs0, regs1, regs2;
26 reg [31:0] op0_out, op1_out, op2_out;
30 wire [31:0] shift_oper;
31 wire [31:0] shift_res;
33 wire [31:0] rotate_res;
35 assign regs0 = (read_0 == 4'b1111) ? rpc : rdata_0;
36 assign regs1 = (read_1 == 4'b1111) ? rpc : rdata_1;
37 assign regs2 = rdata_2; /* use regs2 for things that cannot be r15 */
39 IREALLYHATEARMSHIFT shift(.insn(insn),
42 .cflag_in(incpsr[`CPSR_C]),
44 .cflag_out(shift_cflag_out));
46 SuckLessRotator whirr(.oper({24'b0, insn[7:0]}),
52 `DECODE_ALU_MULT, /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
53 // `DECODE_ALU_MUL_LONG, /* Multiply long */
54 `DECODE_ALU_MRS, /* MRS (Transfer PSR to register) */
55 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
56 `DECODE_ALU_MSR_FLAGS, /* MSR (Transfer register or immediate to PSR, flag bits only) */
57 `DECODE_ALU_SWP, /* Atomic swap */
58 `DECODE_ALU_BX, /* Branch and exchange */
59 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
60 `DECODE_ALU_HDATA_IMM, /* Halfword transfer - register offset */
61 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
62 `DECODE_LDRSTR, /* Single data transfer */
63 `DECODE_LDMSTM, /* Block data transfer */
64 `DECODE_BRANCH, /* Branch */
65 `DECODE_LDCSTC, /* Coprocessor data transfer */
66 `DECODE_CDP, /* Coprocessor data op */
67 `DECODE_SWI: /* SWI */
69 `DECODE_MRCMCR: /* Coprocessor register transfer */
71 `DECODE_ALU: /* ALU */
72 rpc = inpc + (insn[25] ? 8 : (insn[4] ? 12 : 8));
73 default: /* X everything else out */
82 op0_out = 32'hxxxxxxxx;
83 op1_out = 32'hxxxxxxxx;
84 op2_out = 32'hxxxxxxxx;
88 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
90 read_0 = insn[15:12]; /* Rn */
91 read_1 = insn[3:0]; /* Rm */
92 read_2 = insn[11:8]; /* Rs */
98 // `DECODE_ALU_MUL_LONG: /* Multiply long */
100 // read_0 = insn[11:8]; /* Rn */
101 // read_1 = insn[3:0]; /* Rm */
102 // read_2 = 4'b0; /* anyus */
106 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
108 `DECODE_ALU_MSR: /* MSR (Transfer register to PSR) */
110 read_0 = insn[3:0]; /* Rm */
114 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
116 read_0 = insn[3:0]; /* Rm */
118 if(insn[25]) begin /* the constant case */
119 op0_out = rotate_res;
124 `DECODE_ALU_SWP: /* Atomic swap */
126 read_0 = insn[19:16]; /* Rn */
127 read_1 = insn[3:0]; /* Rm */
132 `DECODE_ALU_BX: /* Branch and exchange */
134 read_0 = insn[3:0]; /* Rn */
138 `DECODE_ALU_HDATA_REG: /* Halfword transfer - register offset */
140 read_0 = insn[19:16];
142 read_2 = insn[15:12];
148 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
150 read_0 = insn[19:16];
151 read_1 = insn[15:12];
154 op1_out = {24'b0, insn[11:8], insn[3:0]};
157 `DECODE_ALU: /* ALU */
159 read_0 = insn[19:16]; /* Rn */
160 read_1 = insn[3:0]; /* Rm */
161 read_2 = insn[11:8]; /* Rs for shift */
164 if(insn[25]) begin /* the constant case */
165 carry_out = incpsr[`CPSR_C];
166 op1_out = rotate_res;
168 carry_out = shift_cflag_out;
172 `DECODE_LDRSTR_UNDEFINED: /* Undefined. I hate ARM */
176 `DECODE_LDRSTR: /* Single data transfer */
178 read_0 = insn[19:16]; /* Rn */
179 read_1 = insn[3:0]; /* Rm */
180 read_2 = insn[15:12];
184 op1_out = {20'b0, insn[11:0]};
185 carry_out = incpsr[`CPSR_C];
188 carry_out = shift_cflag_out;
192 `DECODE_LDMSTM: /* Block data transfer */
194 read_0 = insn[19:16];
197 op1_out = {16'b0, insn[15:0]};
199 `DECODE_BRANCH: /* Branch */
201 op0_out = {{6{insn[23]}}, insn[23:0], 2'b0};
203 `DECODE_LDCSTC: /* Coprocessor data transfer */
205 read_0 = insn[19:16];
208 op1_out = {24'b0, insn[7:0]};
210 `DECODE_CDP: /* Coprocessor data op */
213 `DECODE_MRCMCR: /* Coprocessor register transfer */
215 read_0 = insn[15:12];
219 `DECODE_SWI: /* SWI */
223 $display("Undecoded instruction");
228 always @ (posedge clk) begin
229 op0 <= op0_out; /* Rn - always */
230 op1 <= op1_out; /* 'operand 2' - Rm */
231 op2 <= op2_out; /* thirdedge - Rs */
239 module IREALLYHATEARMSHIFT(
241 input [31:0] operand,
242 input [31:0] reg_amt,
244 output reg [31:0] res,
247 wire [5:0] shift_amt;
248 reg is_arith, is_rot;
250 wire [31:0] rshift_res;
252 assign shift_amt = insn[4] ? {|reg_amt[7:5], reg_amt[4:0]} /* reg-specified shift */
253 : {insn[11:7] == 5'b0, insn[11:7]}; /* immediate shift */
255 SuckLessShifter barrel(.oper(operand),
261 .carryout(rshift_cout));
285 case (insn[6:5]) /* shift type */
287 {cflag_out, res} = {cflag_in, operand} << {insn[4] & shift_amt[5], shift_amt[4:0]};
290 cflag_out = rshift_cout;
294 cflag_out = rshift_cout;
297 if(!insn[4] && shift_amt[4:0] == 5'b0) begin /* RRX x.x */
298 res = {cflag_in, operand[31:1]};
299 cflag_out = operand[0];
302 cflag_out = rshift_cout;
308 module SuckLessShifter(
314 output wire [31:0] res,
318 wire [32:0] stage1, stage2, stage3, stage4, stage5;
320 wire pushbits = is_arith & oper[31];
322 /* do a barrel shift */
323 assign stage1 = amt[5] ? {is_rot ? oper : {32{pushbits}}, oper[31]} : {oper, carryin};
324 assign stage2 = amt[4] ? {is_rot ? stage1[16:1] : {16{pushbits}}, stage1[32:17], stage1[16]} : stage1;
325 assign stage3 = amt[3] ? {is_rot ? stage2[8:1] : {8{pushbits}}, stage2[32:9], stage2[8]} : stage2;
326 assign stage4 = amt[2] ? {is_rot ? stage3[4:1] : {4{pushbits}}, stage3[32:5], stage3[4]} : stage3;
327 assign stage5 = amt[1] ? {is_rot ? stage4[2:1] : {2{pushbits}}, stage4[32:3], stage4[2]} : stage4;
328 assign {res, carryout} = amt[0] ? {is_rot ? stage5[1] : pushbits, stage5[32:2], stage5[1]} : stage5;
332 module SuckLessRotator(
335 output wire [31:0] res
338 wire [31:0] stage1, stage2, stage3;
339 assign stage1 = amt[3] ? {oper[15:0], oper[31:16]} : oper;
340 assign stage2 = amt[2] ? {stage1[7:0], stage1[31:8]} : stage1;
341 assign stage3 = amt[1] ? {stage2[3:0], stage2[31:4]} : stage2;
342 assign res = amt[0] ? {stage3[1:0], stage3[31:2]} : stage3;