3 input Nrst, /* XXX not used yet */
18 output reg outstall_2a = 0,
19 output reg bubble_3a = 1,
20 output reg [31:0] cpsr_3a = 0,
21 output reg [31:0] spsr_3a = 0,
22 output reg cpsrup_3a = 0,
23 output reg write_reg_3a = 1'bx,
24 output reg [3:0] write_num_3a = 4'bxxxx,
25 output reg [31:0] write_data_3a = 32'hxxxxxxxx,
26 output reg [31:0] jmppc_2a,
28 output reg [31:0] pc_3a,
29 output reg [31:0] insn_3a,
30 output reg [31:0] op0_3a, op1_3a, op2_3a
34 reg [31:0] mult_acc0, mult_in0, mult_in1;
36 wire [31:0] mult_result;
38 reg [31:0] alu_in0_2a, alu_in1_2a;
41 wire [31:0] alu_result_2a, alu_outcpsr_2a;
45 reg [31:0] next_cpsr_3a, next_spsr_3a;
48 reg next_write_reg_3a;
49 reg [3:0] next_write_num_3a;
50 reg [31:0] next_write_data_3a;
52 Multiplier multiplier(
53 .clk(clk), .Nrst(Nrst),
54 .start(mult_start), .acc0(mult_acc0), .in0(mult_in0),
55 .in1(mult_in1), .done(mult_done), .result(mult_result));
58 .clk(clk), .Nrst(Nrst),
59 .in0(alu_in0_2a), .in1(alu_in1_2a), .cpsr(cpsr_2a), .op(alu_op_2a),
60 .setflags(alu_setflags_2a), .shifter_carry(carry_2a),
61 .result(alu_result_2a), .cpsr_out(alu_outcpsr_2a), .setres(alu_setres_2a));
67 bubble_3a <= next_bubble_3a;
68 cpsr_3a <= next_cpsr_3a;
69 spsr_3a <= next_spsr_3a;
70 cpsrup_3a <= next_cpsrup_3a;
71 write_reg_3a <= next_write_reg_3a;
72 write_num_3a <= next_write_num_3a;
73 write_data_3a <= next_write_data_3a;
82 reg delayedflush_2a = 0;
84 if (flush_2a && outstall_2a /* halp! I can't do it now, maybe later? */)
86 else if (!outstall_2a /* anything has been handled this time around */)
91 outstall_3a <= outstall_2a;
95 outstall_2a = stall_2a;
98 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
99 outstall_2a = outstall_2a | ((!outstall_3a | !mult_done) && !bubble_2a);
108 alu_op_2a = insn_2a[24:21];
109 alu_setflags_2a = insn_2a[20] /* S */;
112 /* Register outputs */
115 next_cpsr_3a = cpsr_2a;
116 next_spsr_3a = spsr_2a;
118 next_write_reg_3a = 0;
119 next_write_num_3a = 4'hx;
120 next_write_data_3a = 32'hxxxxxxxx;
123 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
125 next_cpsr_3a = insn_2a[20] /* S */ ? {mult_result[31] /* N */, mult_result == 0 /* Z */, 1'b0 /* C */, cpsr_2a[28] /* V */, cpsr_2a[27:0]} : cpsr_2a;
126 next_cpsrup_3a = insn_2a[20] /* S */;
127 next_write_reg_3a = 1;
128 next_write_num_3a = insn_2a[19:16] /* Rd -- why the fuck isn't this the same place as ALU */;
129 next_write_data_3a = mult_result;
131 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
133 next_write_reg_3a = 1;
134 next_write_num_3a = insn_2a[15:12];
135 if (insn_2a[22] /* Ps */)
136 next_write_data_3a = spsr_2a;
138 next_write_data_3a = cpsr_2a;
140 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
141 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
143 if ((cpsr_2a[4:0] == `MODE_USR) || (insn_2a[16] /* that random bit */ == 1'b0)) /* flags only */
145 if (insn_2a[22] /* Ps */)
146 next_spsr_3a = {op0_2a[31:29], spsr_2a[28:0]};
148 next_cpsr_3a = {op0_2a[31:29], cpsr_2a[28:0]};
150 if (insn_2a[22] /* Ps */)
151 next_spsr_3a = op0_2a;
153 next_cpsr_3a = op0_2a;
157 `DECODE_ALU_SWP, /* Atomic swap */
158 `DECODE_ALU_BX, /* Branch */
159 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
160 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
162 `DECODE_ALU: /* ALU */
164 if (alu_setres_2a) begin
165 next_write_reg_3a = 1;
166 next_write_num_3a = insn_2a[15:12] /* Rd */;
167 next_write_data_3a = alu_result_2a;
170 if (insn_2a[20] /* S */) begin
172 next_cpsr_3a = ((insn_2a[15:12] == 4'b1111) && insn_2a[20]) ? spsr_2a : alu_outcpsr_2a;
175 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
176 `DECODE_LDRSTR, /* Single data transfer */
177 `DECODE_LDMSTM: /* Block data transfer */
179 `DECODE_BRANCH: /* Branch */
181 if(insn_2a[24] /* L */) begin
182 next_write_reg_3a = 1;
183 next_write_num_3a = 4'hE; /* link register */
184 next_write_data_3a = pc_2a + 32'h4;
190 /* Multiplier inputs */
194 mult_acc0 = 32'hxxxxxxxx;
195 mult_in0 = 32'hxxxxxxxx;
196 mult_in1 = 32'hxxxxxxxx;
201 if (!outstall_3a /* i.e., this is a new one */ && !bubble_2a /* i.e., this is a real one */)
204 mult_acc0 = insn_2a[21] /* A */ ? op0_2a /* Rn */ : 32'h0;
205 mult_in0 = op1_2a /* Rm */;
206 mult_in1 = op2_2a /* Rs */;
207 $display("New MUL instruction");
213 /* Miscellaneous cleanup. */
216 next_bubble_3a = bubble_2a | flush_2a | delayedflush_2a;
219 jmppc_2a = 32'h00000000;
222 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
223 next_bubble_3a = next_bubble_3a | !mult_done | !outstall_3a;
224 `DECODE_ALU_MRS, /* MRS (Transfer PSR to register) */
225 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
226 `DECODE_ALU_MSR_FLAGS, /* MSR (Transfer register or immediate to PSR, flag bits only) */
227 `DECODE_ALU_SWP, /* Atomic swap */
228 `DECODE_ALU_BX, /* Branch */
229 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
230 `DECODE_ALU_HDATA_IMM, /* Halfword transfer - immediate offset */
231 `DECODE_ALU, /* ALU */
232 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
233 `DECODE_LDRSTR, /* Single data transfer */
234 `DECODE_LDMSTM: /* Block data transfer */
238 if(!bubble_2a && !flush_2a && !delayedflush_2a && !outstall_2a /* Let someone else take precedence. */) begin
239 jmppc_2a = pc_2a + op0_2a + 32'h8;
243 `DECODE_LDCSTC, /* Coprocessor data transfer */
244 `DECODE_CDP, /* Coprocessor data op */
245 `DECODE_MRCMCR, /* Coprocessor register transfer */
246 `DECODE_SWI: /* SWI */
248 default: /* X everything else out */
256 input Nrst, /* XXX not used yet */
264 output reg [31:0] result);
267 reg [31:0] multiplicand;
270 always @(posedge clk)
278 bitfield <= {2'b00, bitfield[31:2]};
279 multiplicand <= {multiplicand[29:0], 2'b00};
281 (bitfield[0] ? multiplicand : 0) +
282 (bitfield[1] ? {multiplicand[30:0], 1'b0} : 0);
283 if (bitfield == 0) begin
293 input Nrst, /* XXX not used yet */
302 output reg [31:0] result,
303 output reg [31:0] cpsr_out,
307 reg flag_n, flag_z, flag_c, flag_v;
308 wire [32:0] sum, diff, rdiff;
309 wire sum_v, diff_v, rdiff_v;
311 assign sum = {1'b0, in0} + {1'b0, in1};
312 assign diff = {1'b0, in0} - {1'b0, in1};
313 assign rdiff = {1'b0, in1} - {1'b0, in0};
314 assign sum_v = (in0[31] ^~ in1[31]) & (sum[31] ^ in0[31]);
315 assign diff_v = (in0[31] ^ in1[31]) & (diff[31] ^ in0[31]);
316 assign rdiff_v = (in0[31] ^ in1[31]) & (rdiff[31] ^ in1[31]);
321 flag_c = cpsr[`CPSR_C];
322 flag_v = cpsr[`CPSR_V];
326 flag_c = shifter_carry;
331 flag_c = shifter_carry;
335 {flag_c, result} = diff;
341 {flag_c, result} = rdiff;
347 {flag_c, result} = sum;
352 {flag_c, result} = sum + {32'b0, cpsr[`CPSR_C]};
353 flag_v = sum_v | (~sum[31] & result[31]);
357 {flag_c, result} = diff - {32'b0, (~cpsr[`CPSR_C])};
359 flag_v = diff_v | (diff[31] & ~result[31]);
363 {flag_c, result} = rdiff - {32'b0, (~cpsr[`CPSR_C])};
365 flag_v = rdiff_v | (rdiff[31] & ~result[31]);
370 flag_c = shifter_carry;
375 flag_c = shifter_carry;
379 {flag_c, result} = diff;
385 {flag_c, result} = sum;
391 flag_c = shifter_carry;
396 flag_c = shifter_carry;
400 result = in0 & (~in1);
401 flag_c = shifter_carry;
406 flag_c = shifter_carry;
411 flag_z = (result == 0);
414 cpsr_out = setflags ? {flag_n, flag_z, flag_c, flag_v, cpsr[27:0]} : cpsr;