1 `include "ARM_Constants.v"
10 output reg [31:0] op0,
11 output reg [31:0] op1,
12 output reg [31:0] op2,
14 output reg [31:0] outcpsr,
15 output reg [31:0] outspsr,
17 output reg [3:0] read_0,
18 output reg [3:0] read_1,
19 output reg [3:0] read_2,
25 wire [31:0] regs0, regs1, regs2;
27 reg [31:0] op0_out, op1_out, op2_out;
31 wire [31:0] shift_oper;
32 wire [31:0] shift_res;
34 wire [31:0] rotate_res;
36 assign regs0 = (read_0 == 4'b1111) ? rpc : rdata_0;
37 assign regs1 = (read_1 == 4'b1111) ? rpc : rdata_1;
38 assign regs2 = rdata_2; /* use regs2 for things that cannot be r15 */
40 IREALLYHATEARMSHIFT shift(.insn(insn),
43 .cflag_in(incpsr[`CPSR_C]),
45 .cflag_out(shift_cflag_out));
47 SuckLessRotator whirr(.oper({24'b0, insn[7:0]}),
53 `DECODE_ALU_MULT, /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
54 // `DECODE_ALU_MUL_LONG, /* Multiply long */
55 `DECODE_ALU_MRS, /* MRS (Transfer PSR to register) */
56 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
57 `DECODE_ALU_MSR_FLAGS, /* MSR (Transfer register or immediate to PSR, flag bits only) */
58 `DECODE_ALU_SWP, /* Atomic swap */
59 `DECODE_ALU_BX, /* Branch and exchange */
60 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
61 `DECODE_ALU_HDATA_IMM, /* Halfword transfer - register offset */
62 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
63 `DECODE_LDRSTR, /* Single data transfer */
64 `DECODE_LDMSTM, /* Block data transfer */
65 `DECODE_BRANCH, /* Branch */
66 `DECODE_LDCSTC, /* Coprocessor data transfer */
67 `DECODE_CDP, /* Coprocessor data op */
68 `DECODE_SWI: /* SWI */
70 `DECODE_MRCMCR: /* Coprocessor register transfer */
72 `DECODE_ALU: /* ALU */
73 rpc = inpc + (insn[25] ? 8 : (insn[4] ? 12 : 8));
74 default: /* X everything else out */
84 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
86 read_0 = insn[15:12]; /* Rn */
87 read_1 = insn[3:0]; /* Rm */
88 read_2 = insn[11:8]; /* Rs */
90 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
92 `DECODE_ALU_MSR: /* MSR (Transfer register to PSR) */
93 read_0 = insn[3:0]; /* Rm */
94 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
95 read_0 = insn[3:0]; /* Rm */
96 `DECODE_ALU_SWP: /* Atomic swap */
98 read_0 = insn[19:16]; /* Rn */
99 read_1 = insn[3:0]; /* Rm */
101 `DECODE_ALU_BX: /* Branch and exchange */
102 read_0 = insn[3:0]; /* Rn */
103 `DECODE_ALU_HDATA_REG: /* Halfword transfer - register offset */
105 read_0 = insn[19:16];
107 read_2 = insn[15:12];
109 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
111 read_0 = insn[19:16];
112 read_1 = insn[15:12];
114 `DECODE_ALU: /* ALU */
116 read_0 = insn[19:16]; /* Rn */
117 read_1 = insn[3:0]; /* Rm */
118 read_2 = insn[11:8]; /* Rs for shift */
120 `DECODE_LDRSTR_UNDEFINED: /* Undefined. I hate ARM */
122 `DECODE_LDRSTR: /* Single data transfer */
124 read_0 = insn[19:16]; /* Rn */
125 read_1 = insn[3:0]; /* Rm */
126 read_2 = insn[15:12];
128 `DECODE_LDMSTM: /* Block data transfer */
129 read_0 = insn[19:16];
130 `DECODE_BRANCH: /* Branch */
132 `DECODE_LDCSTC: /* Coprocessor data transfer */
133 read_0 = insn[19:16];
134 `DECODE_CDP: /* Coprocessor data op */
136 `DECODE_MRCMCR: /* Coprocessor register transfer */
137 read_0 = insn[15:12];
138 `DECODE_SWI: /* SWI */
141 $display("Undecoded instruction");
146 op0_out = 32'hxxxxxxxx;
147 op1_out = 32'hxxxxxxxx;
148 op2_out = 32'hxxxxxxxx;
152 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
158 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
160 `DECODE_ALU_MSR: /* MSR (Transfer register to PSR) */
162 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
163 if(insn[25]) begin /* the constant case */
164 op0_out = rotate_res;
168 `DECODE_ALU_SWP: /* Atomic swap */
173 `DECODE_ALU_BX: /* Branch and exchange */
175 `DECODE_ALU_HDATA_REG: /* Halfword transfer - register offset */
181 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
184 op1_out = {24'b0, insn[11:8], insn[3:0]};
187 `DECODE_ALU: /* ALU */
190 if(insn[25]) begin /* the constant case */
191 carry_out = incpsr[`CPSR_C];
192 op1_out = rotate_res;
194 carry_out = shift_cflag_out;
198 `DECODE_LDRSTR: /* Single data transfer */
201 if(!insn[25] /* immediate */) begin
202 op1_out = {20'b0, insn[11:0]};
203 carry_out = incpsr[`CPSR_C];
206 carry_out = shift_cflag_out;
210 `DECODE_LDMSTM: /* Block data transfer */
213 op1_out = {16'b0, insn[15:0]};
215 `DECODE_BRANCH: /* Branch */
216 op0_out = {{6{insn[23]}}, insn[23:0], 2'b0};
217 `DECODE_LDCSTC: /* Coprocessor data transfer */
220 op1_out = {24'b0, insn[7:0]};
222 `DECODE_CDP: /* Coprocessor data op */
224 `DECODE_MRCMCR: /* Coprocessor register transfer */
226 `DECODE_SWI: /* SWI */
231 always @ (posedge clk) begin
234 op0 <= op0_out; /* Rn - always */
235 op1 <= op1_out; /* 'operand 2' - Rm */
236 op2 <= op2_out; /* thirdedge - Rs */
245 module IREALLYHATEARMSHIFT(
247 input [31:0] operand,
248 input [31:0] reg_amt,
250 output reg [31:0] res,
253 wire [5:0] shift_amt;
254 reg is_arith, is_rot;
256 wire [31:0] rshift_res;
258 assign shift_amt = insn[4] ? {|reg_amt[7:5], reg_amt[4:0]} /* reg-specified shift */
259 : {insn[11:7] == 5'b0, insn[11:7]}; /* immediate shift */
261 SuckLessShifter barrel(.oper(operand),
267 .carryout(rshift_cout));
291 case (insn[6:5]) /* shift type */
293 {cflag_out, res} = {cflag_in, operand} << {insn[4] & shift_amt[5], shift_amt[4:0]};
296 cflag_out = rshift_cout;
300 cflag_out = rshift_cout;
303 if(!insn[4] && shift_amt[4:0] == 5'b0) begin /* RRX x.x */
304 res = {cflag_in, operand[31:1]};
305 cflag_out = operand[0];
308 cflag_out = rshift_cout;
314 module SuckLessShifter(
320 output wire [31:0] res,
324 wire [32:0] stage1, stage2, stage3, stage4, stage5;
326 wire pushbits = is_arith & oper[31];
328 /* do a barrel shift */
329 assign stage1 = amt[5] ? {is_rot ? oper : {32{pushbits}}, oper[31]} : {oper, carryin};
330 assign stage2 = amt[4] ? {is_rot ? stage1[16:1] : {16{pushbits}}, stage1[32:17], stage1[16]} : stage1;
331 assign stage3 = amt[3] ? {is_rot ? stage2[8:1] : {8{pushbits}}, stage2[32:9], stage2[8]} : stage2;
332 assign stage4 = amt[2] ? {is_rot ? stage3[4:1] : {4{pushbits}}, stage3[32:5], stage3[4]} : stage3;
333 assign stage5 = amt[1] ? {is_rot ? stage4[2:1] : {2{pushbits}}, stage4[32:3], stage4[2]} : stage4;
334 assign {res, carryout} = amt[0] ? {is_rot ? stage5[1] : pushbits, stage5[32:2], stage5[1]} : stage5;
338 module SuckLessRotator(
341 output wire [31:0] res
344 wire [31:0] stage1, stage2, stage3;
345 assign stage1 = amt[3] ? {oper[15:0], oper[31:16]} : oper;
346 assign stage2 = amt[2] ? {stage1[7:0], stage1[31:8]} : stage1;
347 assign stage3 = amt[1] ? {stage2[3:0], stage2[31:4]} : stage2;
348 assign res = amt[0] ? {stage3[1:0], stage3[31:2]} : stage3;