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] outpc
30 reg [31:0] mult_acc0, mult_in0, mult_in1;
32 wire [31:0] mult_result;
34 reg [31:0] alu_in0, alu_in1;
37 wire [31:0] alu_result, alu_outcpsr;
41 reg [31:0] next_outcpsr, next_outspsr;
43 reg [3:0] next_write_num;
44 reg [31:0] next_write_data;
46 Multiplier multiplier(
47 .clk(clk), .Nrst(Nrst),
48 .start(mult_start), .acc0(mult_acc0), .in0(mult_in0),
49 .in1(mult_in1), .done(mult_done), .result(mult_result));
52 .clk(clk), .Nrst(Nrst),
53 .in0(alu_in0), .in1(alu_in1), .cpsr(cpsr), .op(alu_op),
54 .setflags(alu_setflags), .shifter_carry(carry),
55 .result(alu_result), .cpsr_out(alu_outcpsr), .setres(alu_setres));
61 outbubble <= next_outbubble;
62 outcpsr <= next_outcpsr;
63 outspsr <= next_outspsr;
64 write_reg <= next_write_reg;
65 write_num <= next_write_num;
66 write_data <= next_write_data;
72 prevstall <= outstall;
77 next_outbubble = inbubble;
81 next_write_num = 4'hx;
82 next_write_data = 32'hxxxxxxxx;
85 mult_acc0 = 32'hxxxxxxxx;
86 mult_in0 = 32'hxxxxxxxx;
87 mult_in1 = 32'hxxxxxxxx;
89 alu_in0 = 32'hxxxxxxxx;
90 alu_in1 = 32'hxxxxxxxx;
91 alu_op = 4'hx; /* hax! */
95 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
97 if (!prevstall && !inbubble)
100 mult_acc0 = insn[21] /* A */ ? op0 /* Rn */ : 32'h0;
101 mult_in0 = op1 /* Rm */;
102 mult_in1 = op2 /* Rs */;
103 $display("New MUL instruction");
105 outstall = stall | ((!prevstall | !mult_done) && !inbubble);
106 next_outbubble = inbubble | !mult_done | !prevstall;
107 next_outcpsr = insn[20] /* S */ ? {mult_result[31] /* N */, mult_result == 0 /* Z */, 1'b0 /* C */, cpsr[28] /* V */, cpsr[27:0]} : cpsr;
109 next_write_num = insn[19:16] /* Rd -- why the fuck isn't this the same place as ALU */;
110 next_write_data = mult_result;
112 // `DECODE_ALU_MUL_LONG, /* Multiply long */
113 `DECODE_ALU_MRS: /* MRS (Transfer PSR to register) */
116 next_write_num = insn[15:12];
117 if (insn[22] /* Ps */)
118 next_write_data = spsr;
120 next_write_data = cpsr;
122 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
123 `DECODE_ALU_MSR_FLAGS: /* MSR (Transfer register or immediate to PSR, flag bits only) */
124 if ((cpsr[4:0] == `MODE_USR) || (insn[16] /* that random bit */ == 1'b0)) /* flags only */
126 if (insn[22] /* Ps */)
127 next_outspsr = {op0[31:29], spsr[28:0]};
129 next_outcpsr = {op0[31:29], cpsr[28:0]};
131 if (insn[22] /* Ps */)
136 `DECODE_ALU_SWP, /* Atomic swap */
137 `DECODE_ALU_BX, /* Branch */
138 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
139 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
141 `DECODE_ALU: /* ALU */
145 alu_op = insn[24:21];
146 alu_setflags = insn[20] /* S */;
148 if (alu_setres) begin
150 next_write_num = insn[15:12] /* Rd */;
151 next_write_data = alu_result;
154 next_outcpsr = ((insn[15:12] == 4'b1111) && insn[20]) ? spsr : alu_outcpsr;
156 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
157 `DECODE_LDRSTR, /* Single data transfer */
158 `DECODE_LDMSTM: /* Block data transfer */
165 next_write_num = 4'hE; /* link register */
166 next_write_data = pc + 32'h4;
169 `DECODE_LDCSTC, /* Coprocessor data transfer */
170 `DECODE_CDP, /* Coprocessor data op */
171 `DECODE_MRCMCR, /* Coprocessor register transfer */
172 `DECODE_SWI: /* SWI */
174 default: /* X everything else out */
182 input Nrst, /* XXX not used yet */
190 output reg [31:0] result);
193 reg [31:0] multiplicand;
196 always @(posedge clk)
204 bitfield <= {2'b00, bitfield[31:2]};
205 multiplicand <= {multiplicand[29:0], 2'b00};
207 (bitfield[0] ? multiplicand : 0) +
208 (bitfield[1] ? {multiplicand[30:0], 1'b0} : 0);
209 if (bitfield == 0) begin
219 input Nrst, /* XXX not used yet */
228 output reg [31:0] result,
229 output reg [31:0] cpsr_out,
233 wire flag_n, flag_z, flag_c, flag_v, setres;
234 wire [32:0] sum, diff, rdiff;
235 wire sum_v, diff_v, rdiff_v;
237 assign sum = {1'b0, in0} + {1'b0, in1};
238 assign diff = {1'b0, in0} - {1'b0, in1};
239 assign rdiff = {1'b0, in1} + {1'b0, in0};
240 assign sum_v = (in0[31] ^~ in1[31]) & (sum[31] ^ in0[31]);
241 assign diff_v = (in0[31] ^ in1[31]) & (diff[31] ^ in0[31]);
242 assign rdiff_v = (in0[31] ^ in1[31]) & (rdiff[31] ^ in1[31]);
247 flag_c = cpsr[`CPSR_C];
248 flag_v = cpsr[`CPSR_V];
252 flag_c = shifter_carry;
257 flag_c = shifter_carry;
261 {flag_c, result} = diff;
266 {flag_c, result} = rdiff;
271 {flag_c, result} = sum;
276 {flag_c, result} = sum + {32'b0, cpsr[`CPSR_C]};
277 flag_v = sum_v | (~sum[31] & result[31]);
281 {flag_c, result} = diff - {32'b0, (~cpsr[`CPSR_C])};
282 flag_v = diff_v | (diff[31] & ~result[31]);
286 {flag_c, result} = rdiff - {32'b0, (~cpsr[`CPSR_C])};
287 flag_v = rdiff_v | (rdiff[31] & ~result[31]);
292 flag_c = shifter_carry;
297 flag_c = shifter_carry;
301 {flag_c, result} = diff;
306 {flag_c, result} = sum;
312 flag_c = shifter_carry;
317 flag_c = shifter_carry;
321 result = in0 & (~in1);
322 flag_c = shifter_carry;
327 flag_c = shifter_carry;
332 flag_z = (result == 0);
335 cpsr_out = setflags ? {flag_n, flag_z, flag_c, flag_v, cpsr[27:0]} : cpsr;