3 input Nrst, /* XXX not used yet */
17 output reg outstall = 0,
18 output reg outbubble = 1,
19 output reg [31:0] outcpsr = 0,
20 output reg write_reg = 1'bx,
21 output reg [3:0] write_num = 4'bxxxx,
22 output reg [31:0] write_data = 32'hxxxxxxxx
26 reg [31:0] mult_acc0, mult_in0, mult_in1;
28 wire [31:0] mult_result;
30 reg [31:0] alu_in0, alu_in1;
33 wire [31:0] alu_result, alu_outcpsr;
37 reg [31:0] next_outcpsr;
39 reg [3:0] next_write_num;
40 reg [31:0] next_write_data;
42 Multiplier multiplier(
43 .clk(clk), .Nrst(Nrst),
44 .start(mult_start), .acc0(mult_acc0), .in0(mult_in0),
45 .in1(mult_in1), .done(mult_done), .result(mult_result));
48 .clk(clk), .Nrst(Nrst),
49 .in0(alu_in0), .in1(alu_in1), .cpsr(cpsr), .op(alu_op),
50 .setflags(alu_setflags), .shifter_carry(carry),
51 .result(alu_result), .cpsr_out(alu_outcpsr), .setres(alu_setres));
57 outbubble <= next_outbubble;
58 outcpsr <= next_outcpsr;
59 write_reg <= next_write_reg;
60 write_num <= next_write_num;
61 write_data <= next_write_data;
67 prevstall <= outstall;
72 next_outbubble = inbubble;
75 next_write_num = 4'hx;
76 next_write_data = 32'hxxxxxxxx;
79 mult_acc0 = 32'hxxxxxxxx;
80 mult_in0 = 32'hxxxxxxxx;
81 mult_in1 = 32'hxxxxxxxx;
83 alu_in0 = 32'hxxxxxxxx;
84 alu_in1 = 32'hxxxxxxxx;
85 alu_op = 4'hx; /* hax! */
89 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
91 if (!prevstall && !inbubble)
94 mult_acc0 = insn[21] /* A */ ? op0 /* Rn */ : 32'h0;
95 mult_in0 = op1 /* Rm */;
96 mult_in1 = op2 /* Rs */;
97 $display("New MUL instruction");
99 outstall = stall | ((!prevstall | !mult_done) && !inbubble);
100 next_outbubble = inbubble | !mult_done | !prevstall;
101 next_outcpsr = insn[20] /* S */ ? {mult_result[31] /* N */, mult_result == 0 /* Z */, 1'b0 /* C */, cpsr[28] /* V */, cpsr[27:0]} : cpsr;
103 next_write_num = insn[19:16] /* Rd -- why the fuck isn't this the same place as ALU */;
104 next_write_data = mult_result;
106 // `DECODE_ALU_MUL_LONG, /* Multiply long */
107 `DECODE_ALU_MRS, /* MRS (Transfer PSR to register) */
108 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
109 `DECODE_ALU_MSR_FLAGS, /* MSR (Transfer register or immediate to PSR, flag bits only) */
110 `DECODE_ALU_SWP, /* Atomic swap */
111 `DECODE_ALU_BX, /* Branch */
112 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
113 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
115 `DECODE_ALU: /* ALU */
119 alu_op = insn[24:21];
120 alu_setflags = insn[20] /* I */;
122 if (alu_setres) begin
124 next_write_num = insn[15:12] /* Rd */;
125 next_write_data = alu_result;
128 next_outcpsr = alu_outcpsr;
130 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
131 `DECODE_LDRSTR, /* Single data transfer */
132 `DECODE_LDMSTM, /* Block data transfer */
133 `DECODE_BRANCH, /* Branch */
134 `DECODE_LDCSTC, /* Coprocessor data transfer */
135 `DECODE_CDP, /* Coprocessor data op */
136 `DECODE_MRCMCR, /* Coprocessor register transfer */
137 `DECODE_SWI: /* SWI */
139 default: /* X everything else out */
147 input Nrst, /* XXX not used yet */
155 output reg [31:0] result);
158 reg [31:0] multiplicand;
161 always @(posedge clk)
169 bitfield <= {2'b00, bitfield[31:2]};
170 multiplicand <= {multiplicand[29:0], 2'b00};
172 (bitfield[0] ? multiplicand : 0) +
173 (bitfield[1] ? {multiplicand[30:0], 1'b0} : 0);
174 if (bitfield == 0) begin
184 input Nrst, /* XXX not used yet */
193 output reg [31:0] result,
194 output reg [31:0] cpsr_out,
198 wire flag_n, flag_z, flag_c, flag_v, setres;
199 wire [32:0] sum, diff, rdiff;
200 wire sum_v, diff_v, rdiff_v;
202 assign sum = {1'b0, in0} + {1'b0, in1};
203 assign diff = {1'b0, in0} - {1'b0, in1};
204 assign rdiff = {1'b0, in1} + {1'b0, in0};
205 assign sum_v = (in0[31] ^~ in1[31]) & (sum[31] ^ in0[31]);
206 assign diff_v = (in0[31] ^ in1[31]) & (diff[31] ^ in0[31]);
207 assign rdiff_v = (in0[31] ^ in1[31]) & (rdiff[31] ^ in1[31]);
212 flag_c = cpsr[`CPSR_C];
213 flag_v = cpsr[`CPSR_V];
217 flag_c = shifter_carry;
222 flag_c = shifter_carry;
226 {flag_c, result} = diff;
231 {flag_c, result} = rdiff;
236 {flag_c, result} = sum;
241 {flag_c, result} = sum + {32'b0, cpsr[`CPSR_C]};
242 flag_v = sum_v | (~sum[31] & result[31]);
246 {flag_c, result} = diff - {32'b0, (~cpsr[`CPSR_C])};
247 flag_v = diff_v | (diff[31] & ~result[31]);
251 {flag_c, result} = rdiff - {32'b0, (~cpsr[`CPSR_C])};
252 flag_v = rdiff_v | (rdiff[31] & ~result[31]);
257 flag_c = shifter_carry;
262 flag_c = shifter_carry;
266 {flag_c, result} = diff;
271 {flag_c, result} = sum;
277 flag_c = shifter_carry;
282 flag_c = shifter_carry;
286 result = in0 & (~in1);
287 flag_c = shifter_carry;
292 flag_c = shifter_carry;
297 flag_z = (result == 0);
300 cpsr_out = setflags ? {flag_n, flag_z, flag_c, flag_v, cpsr[27:0]} : cpsr;