3 input Nrst, /* XXX not used yet */
18 output reg outstall = 0,
19 output reg outbubble = 1,
20 output reg [31:0] outcpsr = 0,
21 output reg [31:0] outspsr = 0,
22 output reg outcpsrup = 0,
23 output reg write_reg = 1'bx,
24 output reg [3:0] write_num = 4'bxxxx,
25 output reg [31:0] write_data = 32'hxxxxxxxx,
26 output reg [31:0] jmppc,
28 output reg [31:0] outpc,
29 output reg [31:0] outinsn,
30 output reg [31:0] outop0, outop1, outop2
34 reg [31:0] mult_acc0, mult_in0, mult_in1;
36 wire [31:0] mult_result;
38 reg [31:0] alu_in0, alu_in1;
41 wire [31:0] alu_result, alu_outcpsr;
45 reg [31:0] next_outcpsr, next_outspsr;
48 reg [3:0] next_write_num;
50 reg [31:0] next_write_data;
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), .in1(alu_in1), .cpsr(cpsr), .op(alu_op),
60 .setflags(alu_setflags), .shifter_carry(carry),
61 .result(alu_result), .cpsr_out(alu_outcpsr), .setres(alu_setres));
67 outbubble <= next_outbubble;
68 outcpsr <= next_outcpsr;
69 outspsr <= next_outspsr;
70 outcpsrup <= next_outcpsrup;
71 write_reg <= next_write_reg;
72 write_num <= next_write_num;
73 write_data <= next_write_data;
84 if (flush && outstall /* halp! I can't do it now, maybe later? */)
86 else if (!outstall /* anything has been handled this time around */)
91 prevstall <= outstall;
96 next_outbubble = inbubble | flush | delayedflush;
101 next_write_num = 4'hx;
102 next_write_data = 32'hxxxxxxxx;
105 mult_acc0 = 32'hxxxxxxxx;
106 mult_in0 = 32'hxxxxxxxx;
107 mult_in1 = 32'hxxxxxxxx;
109 alu_in0 = 32'hxxxxxxxx;
110 alu_in1 = 32'hxxxxxxxx;
111 alu_op = 4'hx; /* hax! */
115 jmppc = 32'h00000000;
118 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
120 if (!prevstall && !inbubble)
123 mult_acc0 = insn[21] /* A */ ? op0 /* Rn */ : 32'h0;
124 mult_in0 = op1 /* Rm */;
125 mult_in1 = op2 /* Rs */;
126 $display("New MUL instruction");
128 outstall = outstall | ((!prevstall | !mult_done) && !inbubble);
129 next_outbubble = next_outbubble | !mult_done | !prevstall;
130 next_outcpsr = insn[20] /* S */ ? {mult_result[31] /* N */, mult_result == 0 /* Z */, 1'b0 /* C */, cpsr[28] /* V */, cpsr[27:0]} : cpsr;
131 next_outcpsrup = insn[20] /* S */;
133 next_write_num = insn[19:16] /* Rd -- why the fuck isn't this the same place as ALU */;
134 next_write_data = mult_result;
136 // `DECODE_ALU_MUL_LONG, /* Multiply long */
137 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
140 next_write_num = insn[15:12];
141 if (insn[22] /* Ps */)
142 next_write_data = spsr;
144 next_write_data = cpsr;
147 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
148 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
150 if ((cpsr[4:0] == `MODE_USR) || (insn[16] /* that random bit */ == 1'b0)) /* flags only */
152 if (insn[22] /* Ps */)
153 next_outspsr = {op0[31:29], spsr[28:0]};
155 next_outcpsr = {op0[31:29], cpsr[28:0]};
157 if (insn[22] /* Ps */)
164 `DECODE_ALU_SWP, /* Atomic swap */
165 `DECODE_ALU_BX, /* Branch */
166 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
167 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
169 `DECODE_ALU: /* ALU */
173 alu_op = insn[24:21];
174 alu_setflags = insn[20] /* S */;
176 if (alu_setres) begin
178 next_write_num = insn[15:12] /* Rd */;
179 next_write_data = alu_result;
182 next_outcpsr = ((insn[15:12] == 4'b1111) && insn[20]) ? spsr : alu_outcpsr;
183 next_outcpsrup = insn[20] /* S */;
185 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
186 `DECODE_LDRSTR, /* Single data transfer */
187 `DECODE_LDMSTM: /* Block data transfer */
191 if(!inbubble && !flush && !delayedflush && !outstall /* Let someone else take precedence. */) begin
192 jmppc = pc + op0 + 32'h8;
195 next_write_num = 4'hE; /* link register */
196 next_write_data = pc + 32'h4;
201 `DECODE_LDCSTC, /* Coprocessor data transfer */
202 `DECODE_CDP, /* Coprocessor data op */
203 `DECODE_MRCMCR, /* Coprocessor register transfer */
204 `DECODE_SWI: /* SWI */
206 default: /* X everything else out */
214 input Nrst, /* XXX not used yet */
222 output reg [31:0] result);
225 reg [31:0] multiplicand;
228 always @(posedge clk)
236 bitfield <= {2'b00, bitfield[31:2]};
237 multiplicand <= {multiplicand[29:0], 2'b00};
239 (bitfield[0] ? multiplicand : 0) +
240 (bitfield[1] ? {multiplicand[30:0], 1'b0} : 0);
241 if (bitfield == 0) begin
251 input Nrst, /* XXX not used yet */
260 output reg [31:0] result,
261 output reg [31:0] cpsr_out,
265 reg flag_n, flag_z, flag_c, flag_v;
266 wire [32:0] sum, diff, rdiff;
267 wire sum_v, diff_v, rdiff_v;
269 assign sum = {1'b0, in0} + {1'b0, in1};
270 assign diff = {1'b0, in0} - {1'b0, in1};
271 assign rdiff = {1'b0, in1} - {1'b0, in0};
272 assign sum_v = (in0[31] ^~ in1[31]) & (sum[31] ^ in0[31]);
273 assign diff_v = (in0[31] ^ in1[31]) & (diff[31] ^ in0[31]);
274 assign rdiff_v = (in0[31] ^ in1[31]) & (rdiff[31] ^ in1[31]);
279 flag_c = cpsr[`CPSR_C];
280 flag_v = cpsr[`CPSR_V];
284 flag_c = shifter_carry;
289 flag_c = shifter_carry;
293 {flag_c, result} = diff;
299 {flag_c, result} = rdiff;
305 {flag_c, result} = sum;
310 {flag_c, result} = sum + {32'b0, cpsr[`CPSR_C]};
311 flag_v = sum_v | (~sum[31] & result[31]);
315 {flag_c, result} = diff - {32'b0, (~cpsr[`CPSR_C])};
317 flag_v = diff_v | (diff[31] & ~result[31]);
321 {flag_c, result} = rdiff - {32'b0, (~cpsr[`CPSR_C])};
323 flag_v = rdiff_v | (rdiff[31] & ~result[31]);
328 flag_c = shifter_carry;
333 flag_c = shifter_carry;
337 {flag_c, result} = diff;
343 {flag_c, result} = sum;
349 flag_c = shifter_carry;
354 flag_c = shifter_carry;
358 result = in0 & (~in1);
359 flag_c = shifter_carry;
364 flag_c = shifter_carry;
369 flag_z = (result == 0);
372 cpsr_out = setflags ? {flag_n, flag_z, flag_c, flag_v, cpsr[27:0]} : cpsr;