1 `include "ARM_Constants.v"
5 input Nrst, /* XXX not used yet */
7 input stall_1a, /* pipeline control */
8 input flush, /* XXX not used yet */
10 input bubble_1a, /* stage inputs */
15 output wire stall_0a, /* stage outputs */
16 output reg bubble_2a = 1,
17 output reg [31:0] pc_2a = 0,
18 output reg [31:0] insn_2a = 0
22 `ifdef COPY_PASTA_FODDER
23 /* from page 2 of ARM7TDMIvE2.pdf */
25 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
26 // `DECODE_ALU_MUL_LONG: /* Multiply long */
27 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
28 `DECODE_ALU_MSR: /* MSR (Transfer register to PSR) */
29 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
30 `DECODE_ALU_SWP: /* Atomic swap */
31 `DECODE_ALU_BX: /* Branch */
32 `DECODE_ALU_HDATA_REG: /* Halfword transfer - register offset */
33 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
34 `DECODE_ALU: /* ALU */
35 `DECODE_LDRSTR_UNDEFINED: /* Undefined. I hate ARM */
36 `DECODE_LDRSTR: /* Single data transfer */
37 `DECODE_LDMSTM: /* Block data transfer */
38 `DECODE_BRANCH: /* Branch */
39 `DECODE_LDCSTC: /* Coprocessor data transfer */
40 `DECODE_CDP: /* Coprocessor data op */
41 `DECODE_MRCMCR: /* Coprocessor register transfer */
42 `DECODE_SWI: /* SWI */
43 default: /* X everything else out */
53 function [15:0] idxbit;
58 idxbit = (16'b1) << r;
61 wire [3:0] rn = insn_1a[19:16];
62 wire [3:0] rd = insn_1a[15:12];
63 wire [3:0] rs = insn_1a[11:8];
64 wire [3:0] rm = insn_1a[3:0];
65 wire [3:0] cond = insn_1a[31:28];
67 wire [3:0] rd_mul = insn_1a[19:16];
68 wire [3:0] rn_mul = insn_1a[15:12];
69 wire [3:0] rs_mul = insn_1a[11:8];
71 wire [3:0] alu_opc = insn_1a[24:21];
73 function alu_is_logical;
77 `ALU_AND,`ALU_EOR,`ALU_TST,`ALU_TEQ,`ALU_ORR,`ALU_MOV,`ALU_BIC,`ALU_MVN: alu_is_logical = 1;
78 default: alu_is_logical = 0;
82 function alu_flags_only;
86 `ALU_TST,`ALU_TEQ,`ALU_CMP,`ALU_CMN: alu_flags_only = 1;
87 default: alu_flags_only = 0;
91 function shift_requires_carry;
95 `SHIFT_LSL: shift_requires_carry = (shift[7:2] == 0);
96 `SHIFT_LSR: shift_requires_carry = 0;
97 `SHIFT_ASR: shift_requires_carry = 0;
98 `SHIFT_ROR: shift_requires_carry = (shift[7:2] == 0);
104 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
106 use_cpsr = `COND_MATTERS(cond);
107 use_regs = (insn_1a[21] /* accum */ ? idxbit(rn_mul) : 0) | idxbit(rs_mul) | idxbit(rm);
108 def_cpsr = insn_1a[20] /* setcc */;
109 def_regs = idxbit(rd_mul);
111 // `DECODE_ALU_MUL_LONG: /* Multiply long */
112 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
114 use_cpsr = `COND_MATTERS(cond) || (insn_1a[22] == 0) /* Source = CPSR */;
117 def_regs = idxbit(rd);
119 `DECODE_ALU_MSR: /* MSR (Transfer register to PSR) */
121 use_cpsr = `COND_MATTERS(cond);
122 use_regs = idxbit(rm);
126 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
128 use_cpsr = `COND_MATTERS(cond);
129 use_regs = insn_1a[25] ? 0 : idxbit(rm);
133 `DECODE_ALU_SWP: /* Atomic swap */
135 use_cpsr = `COND_MATTERS(cond);
136 use_regs = idxbit(rn) | idxbit(rm);
138 def_regs = idxbit(rd);
140 `DECODE_ALU_BX: /* Branch */
142 use_cpsr = `COND_MATTERS(cond);
143 use_regs = idxbit(rm);
144 def_cpsr = 0; // don't care, we'll never get there
147 `DECODE_ALU_HDATA_REG: /* Halfword transfer - register offset */
149 use_cpsr = `COND_MATTERS(cond);
150 use_regs = idxbit(rn) | idxbit(rm) | (insn_1a[20] /* L */ ? 0 : idxbit(rd));
152 def_regs = insn_1a[20] /* L */ ? idxbit(rd) : 0;
154 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
156 use_cpsr = `COND_MATTERS(cond);
157 use_regs = idxbit(rn) | (insn_1a[20] /* L */ ? 0 : idxbit(rd));
159 def_regs = insn_1a[20] /* L */ ? idxbit(rd) : 0;
161 `DECODE_ALU: /* ALU */
163 use_cpsr = `COND_MATTERS(cond) | (!insn_1a[25] /* I */ && shift_requires_carry(insn_1a[11:4]));
165 (insn_1a[25] /* I */ ? 0 :
166 (insn_1a[4] /* shift by reg */ ?
167 (idxbit(rs) | idxbit(rm)) :
169 (((alu_opc != `ALU_MOV) && (alu_opc != `ALU_MVN)) ? idxbit(rn) : 0);
170 def_cpsr = insn_1a[20] /* S */;
171 def_regs = alu_flags_only(alu_opc) ? 0 : idxbit(rd);
173 `DECODE_LDRSTR_UNDEFINED: /* Undefined. I hate ARM */
182 use_cpsr = `COND_MATTERS(cond);
183 use_regs = idxbit(rn) | (insn_1a[25] /* I */ ? idxbit(rm) : 0) | (insn_1a[20] /* L */ ? 0 : idxbit(rd));
185 def_regs = insn_1a[20] /* L */ ? idxbit(rd) : 0;
187 `DECODE_LDMSTM: /* Block data transfer */
189 use_cpsr = `COND_MATTERS(cond);
190 use_regs = idxbit(rn) | (insn_1a[20] /* L */ ? 0 : insn_1a[15:0]);
191 def_cpsr = insn_1a[22]; /* This is a superset of all cases, anyway. */
192 def_regs = (insn_1a[21] /* W */ ? idxbit(rn) : 0) | (insn_1a[20] /* L */ ? insn_1a[15:0] : 0);
194 `DECODE_BRANCH: /* Branch */
196 use_cpsr = `COND_MATTERS(cond);
199 def_regs = insn_1a[24] /* L */ ? (16'b1 << 14) : 0;
201 `DECODE_LDCSTC: /* Coprocessor data transfer */
203 use_cpsr = `COND_MATTERS(cond);
204 use_regs = idxbit(rn);
206 def_regs = insn_1a[21] /* W */ ? idxbit(rn) : 0;
208 `DECODE_CDP: /* Coprocessor data op */
210 use_cpsr = `COND_MATTERS(cond);
215 `DECODE_MRCMCR: /* Coprocessor register transfer */
217 use_cpsr = `COND_MATTERS(cond);
218 use_regs = insn_1a[20] /* L */ ? 0 : idxbit(rd);
220 def_regs = insn_1a[20] /* L */ ? idxbit(rd) : 0;
222 `DECODE_SWI: /* SWI */
224 use_cpsr = `COND_MATTERS(cond);
229 default: /* X everything else out */
232 use_regs = 16'bxxxxxxxxxxxxxxxx;
234 def_regs = 16'bxxxxxxxxxxxxxxxx;
238 /* Condition checking logic */
239 reg condition_met_1a;
241 casez(insn_1a[31:28])
242 `COND_EQ: condition_met_1a = cpsr_1a[`CPSR_Z];
243 `COND_NE: condition_met_1a = !cpsr_1a[`CPSR_Z];
244 `COND_CS: condition_met_1a = cpsr_1a[`CPSR_C];
245 `COND_CC: condition_met_1a = !cpsr_1a[`CPSR_C];
246 `COND_MI: condition_met_1a = cpsr_1a[`CPSR_N];
247 `COND_PL: condition_met_1a = !cpsr_1a[`CPSR_N];
248 `COND_VS: condition_met_1a = cpsr_1a[`CPSR_V];
249 `COND_VC: condition_met_1a = !cpsr_1a[`CPSR_V];
250 `COND_HI: condition_met_1a = cpsr_1a[`CPSR_C] && !cpsr_1a[`CPSR_Z];
251 `COND_LS: condition_met_1a = !cpsr_1a[`CPSR_C] || cpsr_1a[`CPSR_Z];
252 `COND_GE: condition_met_1a = cpsr_1a[`CPSR_N] == cpsr_1a[`CPSR_V];
253 `COND_LT: condition_met_1a = cpsr_1a[`CPSR_N] != cpsr_1a[`CPSR_V];
254 `COND_GT: condition_met_1a = !cpsr_1a[`CPSR_Z] && (cpsr_1a[`CPSR_N] == cpsr_1a[`CPSR_V]);
255 `COND_LE: condition_met_1a = cpsr_1a[`CPSR_Z] || (cpsr_1a[`CPSR_N] != cpsr_1a[`CPSR_V]);
256 `COND_AL: condition_met_1a = 1;
257 `COND_NV: condition_met_1a = 0;
258 default: condition_met_1a = 1'bx;
262 /* Once it's hit writeback, it's hit the regfile via forwarding so you're done. */
263 reg cpsr_inflight_2a = 0, cpsr_inflight_3a = 0;
264 reg [15:0] regs_inflight_2a = 0, regs_inflight_3a = 0;
266 wire waiting_cpsr_1a = use_cpsr & (cpsr_inflight_2a | cpsr_inflight_3a);
267 wire waiting_regs_1a = |(use_regs & (regs_inflight_2a | regs_inflight_3a));
268 wire waiting_1a = waiting_cpsr_1a | waiting_regs_1a;
269 assign stall_0a = (waiting_1a && !bubble_1a && !flush) || stall_1a;
271 reg delayedflush = 0;
272 always @(posedge clk/* or negedge Nrst*/)
275 else if (flush && stall_0a /* halp! I can't do it now, maybe later? */)
277 else if (!stall_0a /* anything has been handled this time around */)
280 /* Actually do the issue. */
281 always @(posedge clk or negedge Nrst)
284 $display("ISSUE: Stalling instruction %08x because %d/%d", insn_1a, waiting_cpsr_1a, waiting_regs_1a);
289 // Beginning of autoreset for uninitialized flops
290 cpsr_inflight_2a <= 1'h0;
291 cpsr_inflight_3a <= 1'h0;
294 regs_inflight_2a <= 16'h0;
295 regs_inflight_3a <= 16'h0;
297 end else if (!stall_1a)
299 cpsr_inflight_3a <= cpsr_inflight_2a; /* I'm not sure how well selects work with arrays, and that seems like a dumb thing to get anusulated by. */
300 cpsr_inflight_2a <= (waiting_1a || bubble_1a || !condition_met_1a) ? 0 : def_cpsr;
301 regs_inflight_3a <= regs_inflight_2a;
302 regs_inflight_2a <= (waiting_1a || bubble_1a || !condition_met_1a) ? 0 : def_regs;
304 bubble_2a <= bubble_1a | waiting_1a | !condition_met_1a | flush | delayedflush;