Fix up ALU order in Issue.
[firearm.git] / Decode.v
CommitLineData
26049339
CL
1`include "ARM_Constants.v"
2
bae77231
CL
3module Decode(
4 input clk,
be64a9df 5 input [31:0] insn,
bae77231 6 input [31:0] inpc,
821617bb 7 input [31:0] incpsr,
bae77231
CL
8 output reg [31:0] op0,
9 output reg [31:0] op1,
10 output reg [31:0] op2,
42c1e610 11 output reg carry,
bae77231 12
fbe84cc1
JW
13 output reg [3:0] read_0,
14 output reg [3:0] read_1,
15 output reg [3:0] read_2,
821617bb
JW
16 input [31:0] rdata_0,
17 input [31:0] rdata_1,
18 input [31:0] rdata_2
bae77231
CL
19 );
20
fbe84cc1
JW
21 wire [31:0] regs0, regs1, regs2;
22 reg [31:0] rpc;
23 reg [31:0] op0_out, op1_out, op2_out;
24 reg carry_out;
bae77231
CL
25
26 /* shifter stuff */
27 wire [31:0] shift_oper;
28 wire [31:0] shift_res;
29 wire shift_cflag_out;
a0c8a75c 30 wire [31:0] rotate_res;
bae77231 31
821617bb
JW
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 */
bae77231 35
e2c5d224
CL
36 IREALLYHATEARMSHIFT blowme(.insn(insn),
37 .operand(regs1),
38 .reg_amt(regs2),
39 .cflag_in(incpsr[`CPSR_C]),
40 .res(shift_res),
41 .cflag_out(shift_cflag_out));
42
a0c8a75c
CL
43 SuckLessRotator whirr(.oper({24'b0, insn[7:0]}),
44 .amt(insn[11:8]),
45 .res(rotate_res));
46
be64a9df
JW
47 always @(*)
48 casez (insn)
2c523f8a
JW
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 */
be64a9df 66 rpc = inpc - 8;
2c523f8a 67 `DECODE_ALU: /* ALU */
be64a9df 68 rpc = inpc - (insn[25] ? 8 : (insn[4] ? 12 : 8));
2c523f8a 69 default: /* X everything else out */
be64a9df
JW
70 rpc = 32'hxxxxxxxx;
71 endcase
bae77231 72
326fd4c3
JW
73 always @(*) begin
74 read_0 = 4'hx;
75 read_1 = 4'hx;
76 read_2 = 4'hx;
77
0bc7ede9 78 casez (insn)
2c523f8a 79 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
326fd4c3 80 begin
0bc7ede9 81 read_0 = insn[15:12]; /* Rn */
326fd4c3
JW
82 read_1 = insn[3:0]; /* Rm */
83 read_2 = insn[11:8]; /* Rs */
84 end
2c523f8a 85// `DECODE_ALU_MUL_LONG: /* Multiply long */
0bc7ede9 86// read_0 = insn[11:8]; /* Rn */
326fd4c3
JW
87// read_1 = insn[3:0]; /* Rm */
88// read_2 = 4'b0; /* anyus */
2c523f8a 89 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
42c1e610 90 begin end
2c523f8a
JW
91 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
92 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
42c1e610 93 read_0 = insn[3:0]; /* Rm */
2c523f8a 94 `DECODE_ALU_SWP: /* Atomic swap */
326fd4c3 95 begin
0bc7ede9 96 read_0 = insn[19:16]; /* Rn */
326fd4c3 97 read_1 = insn[3:0]; /* Rm */
326fd4c3 98 end
2c523f8a 99 `DECODE_ALU_BX: /* Branch and exchange */
0bc7ede9 100 read_0 = insn[3:0]; /* Rn */
2c523f8a 101 `DECODE_ALU_HDATA_REG: /* Halfword transfer - register offset */
326fd4c3 102 begin
0bc7ede9 103 read_0 = insn[19:16];
326fd4c3
JW
104 read_1 = insn[3:0];
105 end
2c523f8a 106 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
326fd4c3 107 begin
0bc7ede9 108 read_0 = insn[19:16];
326fd4c3 109 end
2c523f8a
JW
110 `DECODE_ALU: /* ALU */
111 begin
112 read_0 = insn[19:16]; /* Rn */
113 read_1 = insn[3:0]; /* Rm */
114 read_2 = insn[11:8]; /* Rs for shift */
115 end
116 `DECODE_LDRSTR_UNDEFINED: /* Undefined. I hate ARM */
326fd4c3 117 begin end
2c523f8a 118 `DECODE_LDRSTR: /* Single data transfer */
326fd4c3 119 begin
0bc7ede9 120 read_0 = insn[19:16]; /* Rn */
326fd4c3
JW
121 read_1 = insn[3:0]; /* Rm */
122 end
2c523f8a 123 `DECODE_LDMSTM: /* Block data transfer */
0bc7ede9 124 read_0 = insn[19:16];
2c523f8a 125 `DECODE_BRANCH: /* Branch */
326fd4c3 126 begin end
2c523f8a 127 `DECODE_LDCSTC: /* Coprocessor data transfer */
0bc7ede9 128 read_0 = insn[19:16];
2c523f8a 129 `DECODE_CDP: /* Coprocessor data op */
42c1e610 130 begin end
2c523f8a 131 `DECODE_MRCMCR: /* Coprocessor register transfer */
42c1e610 132 read_0 = insn[15:12];
2c523f8a 133 `DECODE_SWI: /* SWI */
326fd4c3 134 begin end
b215c5ff 135 default:
326fd4c3 136 $display("Undecoded instruction");
b215c5ff 137 endcase
326fd4c3 138 end
b215c5ff 139
326fd4c3 140 always @(*) begin
42c1e610
JW
141 op0_out = 32'hxxxxxxxx;
142 op1_out = 32'hxxxxxxxx;
143 op2_out = 32'hxxxxxxxx;
144 carry_out = 1'bx;
be64a9df 145 casez (insn)
2c523f8a 146 `DECODE_ALU_MULT: begin /* Multiply */
42c1e610
JW
147 op0_out = regs0;
148 op1_out = regs1;
149 op2_out = regs2;
bae77231 150 end
2c523f8a 151// `DECODE_ALU_MULT_LONG: begin /* Multiply long */
b215c5ff
JW
152// op1_res = regs1;
153// end
2c523f8a 154 `DECODE_ALU_MRS: begin /* MRS (Transfer PSR to register) */
bae77231 155 end
2c523f8a 156 `DECODE_ALU_MSR: begin /* MSR (Transfer register to PSR) */
42c1e610 157 op0_out = regs0;
bae77231 158 end
2c523f8a 159 `DECODE_ALU_MSR_FLAGS: begin /* MSR (Transfer register or immediate to PSR, flag bits only) */
42c1e610 160 if(insn[25]) begin /* the constant case */
a0c8a75c 161 op0_out = rotate_res;
42c1e610
JW
162 end else begin
163 op0_out = regs0;
164 end
bae77231 165 end
2c523f8a 166 `DECODE_ALU_SWP: begin /* Atomic swap */
42c1e610
JW
167 op0_out = regs0;
168 op1_out = regs1;
bae77231 169 end
2c523f8a 170 `DECODE_ALU_BX: begin /* Branch and exchange */
42c1e610 171 op0_out = regs0;
bae77231 172 end
2c523f8a 173 `DECODE_ALU_HDATA_REG: begin /* Halfword transfer - register offset */
42c1e610
JW
174 op0_out = regs0;
175 op1_out = regs1;
bae77231 176 end
2c523f8a 177 `DECODE_ALU_HDATA_IMM: begin /* Halfword transfer - immediate offset */
42c1e610
JW
178 op0_out = regs0;
179 op1_out = {24'b0, insn[11:8], insn[3:0]};
bae77231 180 end
2c523f8a
JW
181 `DECODE_ALU: begin /* ALU */
182 op0_out = regs0;
183 if(insn[25]) begin /* the constant case */
184 carry_out = incpsr[`CPSR_C];
185 op1_out = rotate_res;
186 end else begin
187 carry_out = shift_cflag_out;
188 op1_out = shift_res;
189 end
190 end
191 `DECODE_LDRSTR_UNDEFINED: begin /* Undefined. I hate ARM */
bae77231
CL
192 /* eat shit */
193 end
2c523f8a 194 `DECODE_LDRSTR: begin /* Single data transfer */
42c1e610 195 op0_out = regs0;
be64a9df 196 if(insn[25]) begin
42c1e610
JW
197 op1_out = {20'b0, insn[11:0]};
198 carry_out = incpsr[`CPSR_C];
bae77231 199 end else begin
42c1e610
JW
200 op1_out = shift_res;
201 carry_out = shift_cflag_out;
bae77231
CL
202 end
203 end
2c523f8a 204 `DECODE_LDMSTM: begin /* Block data transfer */
42c1e610
JW
205 op0_out = regs0;
206 op1_out = {16'b0, insn[15:0]};
bae77231 207 end
2c523f8a 208 `DECODE_BRANCH: begin /* Branch */
42c1e610 209 op0_out = {{6{insn[23]}}, insn[23:0], 2'b0};
bae77231 210 end
2c523f8a 211 `DECODE_LDCSTC: begin /* Coprocessor data transfer */
42c1e610
JW
212 op0_out = regs0;
213 op1_out = {24'b0, insn[7:0]};
bae77231 214 end
2c523f8a 215 `DECODE_CDP: begin /* Coprocessor data op */
bae77231 216 end
2c523f8a 217 `DECODE_MRCMCR: begin /* Coprocessor register transfer */
42c1e610 218 op0_out = regs0;
bae77231 219 end
2c523f8a 220 `DECODE_SWI: begin /* SWI */
bae77231 221 end
26049339 222 default: begin end
bae77231
CL
223 endcase
224 end
225
226 always @ (posedge clk) begin
42c1e610
JW
227 op0 <= op0_out; /* Rn - always */
228 op1 <= op1_out; /* 'operand 2' - Rm */
229 op2 <= op2_out; /* thirdedge - Rs */
230 carry <= carry_out;
bae77231
CL
231 end
232
233endmodule
234
e2c5d224 235module IREALLYHATEARMSHIFT(
bae77231
CL
236 input [31:0] insn,
237 input [31:0] operand,
238 input [31:0] reg_amt,
239 input cflag_in,
fbe84cc1
JW
240 output reg [31:0] res,
241 output reg cflag_out
bae77231 242);
bae77231 243 wire [5:0] shift_amt;
f61f8d6f
JW
244 reg is_arith, is_rot;
245 wire rshift_cout;
246 wire [31:0] rshift_res;
e2c5d224
CL
247
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 */
bae77231 250
e2c5d224
CL
251 SuckLessShifter biteme(.oper(operand),
252 .carryin(cflag_in),
253 .amt(shift_amt),
254 .is_arith(is_arith),
255 .is_rot(is_rot),
256 .res(rshift_res),
257 .carryout(rshift_cout));
bae77231 258
821617bb 259 always @(*)
e2c5d224
CL
260 case (insn[6:5])
261 `SHIFT_LSL: begin
e5fb7d86 262 /* meaningless */
e2c5d224
CL
263 is_rot = 1'b0;
264 is_arith = 1'b0;
26049339 265 end
e2c5d224
CL
266 `SHIFT_LSR: begin
267 is_rot = 1'b0;
268 is_arith = 1'b0;
269 end
270 `SHIFT_ASR: begin
271 is_rot = 1'b0;
272 is_arith = 1'b1;
273 end
274 `SHIFT_ROR: begin
275 is_rot = 1'b1;
276 is_arith = 1'b0;
277 end
278 endcase
279
821617bb 280 always @(*)
26049339 281 case (insn[6:5]) /* shift type */
e2c5d224
CL
282 `SHIFT_LSL:
283 {cflag_out, res} = {cflag_in, operand} << {insn[4] & shift_amt[5], shift_amt[4:0]};
bae77231 284 `SHIFT_LSR: begin
e2c5d224
CL
285 res = rshift_res;
286 cflag_out = rshift_cout;
bae77231
CL
287 end
288 `SHIFT_ASR: begin
e2c5d224
CL
289 res = rshift_res;
290 cflag_out = rshift_cout;
bae77231
CL
291 end
292 `SHIFT_ROR: begin
e2c5d224 293 if(!insn[4] && shift_amt[4:0] == 5'b0) begin /* RRX x.x */
bae77231
CL
294 res = {cflag_in, operand[31:1]};
295 cflag_out = operand[0];
e5fb7d86 296 end else begin
e2c5d224
CL
297 res = rshift_res;
298 cflag_out = rshift_cout;
bae77231
CL
299 end
300 end
26049339 301 endcase
bae77231 302endmodule
e2c5d224
CL
303
304module SuckLessShifter(
305 input [31:0] oper,
306 input carryin,
307 input [5:0] amt,
308 input is_arith,
309 input is_rot,
f61f8d6f
JW
310 output wire [31:0] res,
311 output wire carryout
e2c5d224
CL
312);
313
314 wire [32:0] stage1, stage2, stage3, stage4, stage5;
315
e5fb7d86 316 wire pushbits = is_arith & oper[31];
e2c5d224
CL
317
318 /* do a barrel shift */
319 assign stage1 = amt[5] ? {is_rot ? oper : {32{pushbits}}, oper[31]} : {oper, carryin};
6c715b10
CL
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;
e2c5d224
CL
325
326endmodule
a0c8a75c
CL
327
328module SuckLessRotator(
329 input [31:0] oper,
330 input [3:0] amt,
f61f8d6f 331 output wire [31:0] res
a0c8a75c
CL
332);
333
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;
339
340endmodule
This page took 0.064276 seconds and 4 git commands to generate.