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 write_reg = 1'bx,
23 output reg [3:0] write_num = 4'bxxxx,
24 output reg [31:0] write_data = 32'hxxxxxxxx,
25 output reg [31:0] jmppc,
27 output reg [31:0] outpc,
28 output reg [31:0] outinsn
32 reg [31:0] mult_acc0, mult_in0, mult_in1;
34 wire [31:0] mult_result;
36 reg [31:0] alu_in0, alu_in1;
39 wire [31:0] alu_result, alu_outcpsr;
43 reg [31:0] next_outcpsr, next_outspsr;
45 reg [3:0] next_write_num;
47 reg [31:0] next_write_data;
49 Multiplier multiplier(
50 .clk(clk), .Nrst(Nrst),
51 .start(mult_start), .acc0(mult_acc0), .in0(mult_in0),
52 .in1(mult_in1), .done(mult_done), .result(mult_result));
55 .clk(clk), .Nrst(Nrst),
56 .in0(alu_in0), .in1(alu_in1), .cpsr(cpsr), .op(alu_op),
57 .setflags(alu_setflags), .shifter_carry(carry),
58 .result(alu_result), .cpsr_out(alu_outcpsr), .setres(alu_setres));
64 outbubble <= next_outbubble;
65 outcpsr <= next_outcpsr;
66 outspsr <= next_outspsr;
67 write_reg <= next_write_reg;
68 write_num <= next_write_num;
69 write_data <= next_write_data;
77 prevstall <= outstall;
82 next_outbubble = inbubble | flush;
86 next_write_num = 4'hx;
87 next_write_data = 32'hxxxxxxxx;
90 mult_acc0 = 32'hxxxxxxxx;
91 mult_in0 = 32'hxxxxxxxx;
92 mult_in1 = 32'hxxxxxxxx;
94 alu_in0 = 32'hxxxxxxxx;
95 alu_in1 = 32'hxxxxxxxx;
96 alu_op = 4'hx; /* hax! */
100 jmppc = 32'hxxxxxxxx;
103 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
105 if (!prevstall && !inbubble)
108 mult_acc0 = insn[21] /* A */ ? op0 /* Rn */ : 32'h0;
109 mult_in0 = op1 /* Rm */;
110 mult_in1 = op2 /* Rs */;
111 $display("New MUL instruction");
113 outstall = stall | ((!prevstall | !mult_done) && !inbubble);
114 next_outbubble = inbubble | !mult_done | !prevstall;
115 next_outcpsr = insn[20] /* S */ ? {mult_result[31] /* N */, mult_result == 0 /* Z */, 1'b0 /* C */, cpsr[28] /* V */, cpsr[27:0]} : cpsr;
117 next_write_num = insn[19:16] /* Rd -- why the fuck isn't this the same place as ALU */;
118 next_write_data = mult_result;
120 // `DECODE_ALU_MUL_LONG, /* Multiply long */
121 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
124 next_write_num = insn[15:12];
125 if (insn[22] /* Ps */)
126 next_write_data = spsr;
128 next_write_data = cpsr;
130 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
131 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
132 if ((cpsr[4:0] == `MODE_USR) || (insn[16] /* that random bit */ == 1'b0)) /* flags only */
134 if (insn[22] /* Ps */)
135 next_outspsr = {op0[31:29], spsr[28:0]};
137 next_outcpsr = {op0[31:29], cpsr[28:0]};
139 if (insn[22] /* Ps */)
144 `DECODE_ALU_SWP, /* Atomic swap */
145 `DECODE_ALU_BX, /* Branch */
146 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
147 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
149 `DECODE_ALU: /* ALU */
153 alu_op = insn[24:21];
154 alu_setflags = insn[20] /* S */;
156 if (alu_setres) begin
158 next_write_num = insn[15:12] /* Rd */;
159 next_write_data = alu_result;
162 next_outcpsr = ((insn[15:12] == 4'b1111) && insn[20]) ? spsr : alu_outcpsr;
164 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
165 `DECODE_LDRSTR, /* Single data transfer */
166 `DECODE_LDMSTM: /* Block data transfer */
171 jmppc = pc + op0 + 32'h8;
174 next_write_num = 4'hE; /* link register */
175 next_write_data = pc - 32'h4;
180 `DECODE_LDCSTC, /* Coprocessor data transfer */
181 `DECODE_CDP, /* Coprocessor data op */
182 `DECODE_MRCMCR, /* Coprocessor register transfer */
183 `DECODE_SWI: /* SWI */
185 default: /* X everything else out */
193 input Nrst, /* XXX not used yet */
201 output reg [31:0] result);
204 reg [31:0] multiplicand;
207 always @(posedge clk)
215 bitfield <= {2'b00, bitfield[31:2]};
216 multiplicand <= {multiplicand[29:0], 2'b00};
218 (bitfield[0] ? multiplicand : 0) +
219 (bitfield[1] ? {multiplicand[30:0], 1'b0} : 0);
220 if (bitfield == 0) begin
230 input Nrst, /* XXX not used yet */
239 output reg [31:0] result,
240 output reg [31:0] cpsr_out,
244 reg flag_n, flag_z, flag_c, flag_v;
245 wire [32:0] sum, diff, rdiff;
246 wire sum_v, diff_v, rdiff_v;
248 assign sum = {1'b0, in0} + {1'b0, in1};
249 assign diff = {1'b0, in0} - {1'b0, in1};
250 assign rdiff = {1'b0, in1} + {1'b0, in0};
251 assign sum_v = (in0[31] ^~ in1[31]) & (sum[31] ^ in0[31]);
252 assign diff_v = (in0[31] ^ in1[31]) & (diff[31] ^ in0[31]);
253 assign rdiff_v = (in0[31] ^ in1[31]) & (rdiff[31] ^ in1[31]);
258 flag_c = cpsr[`CPSR_C];
259 flag_v = cpsr[`CPSR_V];
263 flag_c = shifter_carry;
268 flag_c = shifter_carry;
272 {flag_c, result} = diff;
277 {flag_c, result} = rdiff;
282 {flag_c, result} = sum;
287 {flag_c, result} = sum + {32'b0, cpsr[`CPSR_C]};
288 flag_v = sum_v | (~sum[31] & result[31]);
292 {flag_c, result} = diff - {32'b0, (~cpsr[`CPSR_C])};
293 flag_v = diff_v | (diff[31] & ~result[31]);
297 {flag_c, result} = rdiff - {32'b0, (~cpsr[`CPSR_C])};
298 flag_v = rdiff_v | (rdiff[31] & ~result[31]);
303 flag_c = shifter_carry;
308 flag_c = shifter_carry;
312 {flag_c, result} = diff;
317 {flag_c, result} = sum;
323 flag_c = shifter_carry;
328 flag_c = shifter_carry;
332 result = in0 & (~in1);
333 flag_c = shifter_carry;
338 flag_c = shifter_carry;
343 flag_z = (result == 0);
346 cpsr_out = setflags ? {flag_n, flag_z, flag_c, flag_v, cpsr[27:0]} : cpsr;