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