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_SWI: /* SWI */
68 `DECODE_MRCMCR: /* Coprocessor register transfer */
70 `DECODE_ALU: /* ALU */
71 rpc = inpc + (insn[25] ? 8 : (insn[4] ? 12 : 8));
72 default: /* X everything else out */
81 op0_out = 32'hxxxxxxxx;
82 op1_out = 32'hxxxxxxxx;
83 op2_out = 32'hxxxxxxxx;
87 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
89 read_0 = insn[15:12]; /* Rn */
90 read_1 = insn[3:0]; /* Rm */
91 read_2 = insn[11:8]; /* Rs */
97 // `DECODE_ALU_MUL_LONG: /* Multiply long */
99 // read_0 = insn[11:8]; /* Rn */
100 // read_1 = insn[3:0]; /* Rm */
101 // read_2 = 4'b0; /* anyus */
105 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
107 `DECODE_ALU_MSR: /* MSR (Transfer register to PSR) */
109 read_0 = insn[3:0]; /* Rm */
113 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
115 read_0 = insn[3:0]; /* Rm */
117 if(insn[25]) begin /* the constant case */
118 op0_out = rotate_res;
123 `DECODE_ALU_SWP: /* Atomic swap */
125 read_0 = insn[19:16]; /* Rn */
126 read_1 = insn[3:0]; /* Rm */
131 `DECODE_ALU_BX: /* Branch and exchange */
133 read_0 = insn[3:0]; /* Rn */
137 `DECODE_ALU_HDATA_REG: /* Halfword transfer - register offset */
139 read_0 = insn[19:16];
145 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
147 read_0 = insn[19:16];
150 op1_out = {24'b0, insn[11:8], insn[3:0]};
152 `DECODE_ALU: /* ALU */
154 read_0 = insn[19:16]; /* Rn */
155 read_1 = insn[3:0]; /* Rm */
156 read_2 = insn[11:8]; /* Rs for shift */
159 if(insn[25]) begin /* the constant case */
160 carry_out = incpsr[`CPSR_C];
161 op1_out = rotate_res;
163 carry_out = shift_cflag_out;
167 `DECODE_LDRSTR_UNDEFINED: /* Undefined. I hate ARM */
171 `DECODE_LDRSTR: /* Single data transfer */
173 read_0 = insn[19:16]; /* Rn */
174 read_1 = insn[3:0]; /* Rm */
175 read_2 = insn[15:12];
179 op1_out = {20'b0, insn[11:0]};
180 carry_out = incpsr[`CPSR_C];
183 carry_out = shift_cflag_out;
187 `DECODE_LDMSTM: /* Block data transfer */
189 read_0 = insn[19:16];
192 op1_out = {16'b0, insn[15:0]};
194 `DECODE_BRANCH: /* Branch */
196 op0_out = {{6{insn[23]}}, insn[23:0], 2'b0};
198 `DECODE_LDCSTC: /* Coprocessor data transfer */
200 read_0 = insn[19:16];
203 op1_out = {24'b0, insn[7:0]};
205 `DECODE_CDP: /* Coprocessor data op */
208 `DECODE_MRCMCR: /* Coprocessor register transfer */
210 read_0 = insn[15:12];
214 `DECODE_SWI: /* SWI */
218 $display("Undecoded instruction");
223 always @ (posedge clk) begin
224 op0 <= op0_out; /* Rn - always */
225 op1 <= op1_out; /* 'operand 2' - Rm */
226 op2 <= op2_out; /* thirdedge - Rs */
233 module IREALLYHATEARMSHIFT(
235 input [31:0] operand,
236 input [31:0] reg_amt,
238 output reg [31:0] res,
241 wire [5:0] shift_amt;
242 reg is_arith, is_rot;
244 wire [31:0] rshift_res;
246 assign shift_amt = insn[4] ? {|reg_amt[7:5], reg_amt[4:0]} /* reg-specified shift */
247 : {insn[11:7] == 5'b0, insn[11:7]}; /* immediate shift */
249 SuckLessShifter barrel(.oper(operand),
255 .carryout(rshift_cout));
279 case (insn[6:5]) /* shift type */
281 {cflag_out, res} = {cflag_in, operand} << {insn[4] & shift_amt[5], shift_amt[4:0]};
284 cflag_out = rshift_cout;
288 cflag_out = rshift_cout;
291 if(!insn[4] && shift_amt[4:0] == 5'b0) begin /* RRX x.x */
292 res = {cflag_in, operand[31:1]};
293 cflag_out = operand[0];
296 cflag_out = rshift_cout;
302 module SuckLessShifter(
308 output wire [31:0] res,
312 wire [32:0] stage1, stage2, stage3, stage4, stage5;
314 wire pushbits = is_arith & oper[31];
316 /* do a barrel shift */
317 assign stage1 = amt[5] ? {is_rot ? oper : {32{pushbits}}, oper[31]} : {oper, carryin};
318 assign stage2 = amt[4] ? {is_rot ? stage1[16:1] : {16{pushbits}}, stage1[32:17], stage1[16]} : stage1;
319 assign stage3 = amt[3] ? {is_rot ? stage2[8:1] : {8{pushbits}}, stage2[32:9], stage2[8]} : stage2;
320 assign stage4 = amt[2] ? {is_rot ? stage3[4:1] : {4{pushbits}}, stage3[32:5], stage3[4]} : stage3;
321 assign stage5 = amt[1] ? {is_rot ? stage4[2:1] : {2{pushbits}}, stage4[32:3], stage4[2]} : stage4;
322 assign {res, carryout} = amt[0] ? {is_rot ? stage5[1] : pushbits, stage5[32:2], stage5[1]} : stage5;
326 module SuckLessRotator(
329 output wire [31:0] res
332 wire [31:0] stage1, stage2, stage3;
333 assign stage1 = amt[3] ? {oper[15:0], oper[31:16]} : oper;
334 assign stage2 = amt[2] ? {stage1[7:0], stage1[31:8]} : stage1;
335 assign stage3 = amt[1] ? {stage2[3:0], stage2[31:4]} : stage2;
336 assign res = amt[0] ? {stage3[1:0], stage3[31:2]} : stage3;