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,
23 output reg [31:0] outpc,
28 reg [31:0] mult_acc0, mult_in0, mult_in1;
30 wire [31:0] mult_result;
32 reg [31:0] alu_in0, alu_in1;
35 wire [31:0] alu_result, alu_outcpsr;
39 reg [31:0] next_outcpsr;
41 reg [3:0] next_write_num;
42 reg [31:0] next_write_data;
44 Multiplier multiplier(
45 .clk(clk), .Nrst(Nrst),
46 .start(mult_start), .acc0(mult_acc0), .in0(mult_in0),
47 .in1(mult_in1), .done(mult_done), .result(mult_result));
50 .clk(clk), .Nrst(Nrst),
51 .in0(alu_in0), .in1(alu_in1), .cpsr(cpsr), .op(alu_op),
52 .setflags(alu_setflags), .shifter_carry(carry),
53 .result(alu_result), .cpsr_out(alu_outcpsr), .setres(alu_setres));
59 outbubble <= next_outbubble;
60 outcpsr <= next_outcpsr;
61 write_reg <= next_write_reg;
62 write_num <= next_write_num;
63 write_data <= next_write_data;
69 prevstall <= outstall;
74 next_outbubble = inbubble;
77 next_write_num = 4'hx;
78 next_write_data = 32'hxxxxxxxx;
81 mult_acc0 = 32'hxxxxxxxx;
82 mult_in0 = 32'hxxxxxxxx;
83 mult_in1 = 32'hxxxxxxxx;
85 alu_in0 = 32'hxxxxxxxx;
86 alu_in1 = 32'hxxxxxxxx;
87 alu_op = 4'hx; /* hax! */
91 `DECODE_ALU_MULT: /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
93 if (!prevstall && !inbubble)
96 mult_acc0 = insn[21] /* A */ ? op0 /* Rn */ : 32'h0;
97 mult_in0 = op1 /* Rm */;
98 mult_in1 = op2 /* Rs */;
99 $display("New MUL instruction");
101 outstall = stall | ((!prevstall | !mult_done) && !inbubble);
102 next_outbubble = inbubble | !mult_done | !prevstall;
103 next_outcpsr = insn[20] /* S */ ? {mult_result[31] /* N */, mult_result == 0 /* Z */, 1'b0 /* C */, cpsr[28] /* V */, cpsr[27:0]} : cpsr;
105 next_write_num = insn[19:16] /* Rd -- why the fuck isn't this the same place as ALU */;
106 next_write_data = mult_result;
108 // `DECODE_ALU_MUL_LONG, /* Multiply long */
109 `DECODE_ALU_MRS, /* MRS (Transfer PSR to register) */
110 `DECODE_ALU_MSR, /* MSR (Transfer register to PSR) */
111 `DECODE_ALU_MSR_FLAGS, /* MSR (Transfer register or immediate to PSR, flag bits only) */
112 `DECODE_ALU_SWP, /* Atomic swap */
113 `DECODE_ALU_BX, /* Branch */
114 `DECODE_ALU_HDATA_REG, /* Halfword transfer - register offset */
115 `DECODE_ALU_HDATA_IMM: /* Halfword transfer - immediate offset */
117 `DECODE_ALU: /* ALU */
121 alu_op = insn[24:21];
122 alu_setflags = insn[20] /* I */;
124 if (alu_setres) begin
126 next_write_num = insn[15:12] /* Rd */;
127 next_write_data = alu_result;
130 next_outcpsr = alu_outcpsr;
132 `DECODE_LDRSTR_UNDEFINED, /* Undefined. I hate ARM */
133 `DECODE_LDRSTR, /* Single data transfer */
134 `DECODE_LDMSTM: /* Block data transfer */
141 next_write_num = 4'hE; /* link register */
142 next_write_data = pc + 32'h4;
145 `DECODE_LDCSTC, /* Coprocessor data transfer */
146 `DECODE_CDP, /* Coprocessor data op */
147 `DECODE_MRCMCR, /* Coprocessor register transfer */
148 `DECODE_SWI: /* SWI */
150 default: /* X everything else out */
158 input Nrst, /* XXX not used yet */
166 output reg [31:0] result);
169 reg [31:0] multiplicand;
172 always @(posedge clk)
180 bitfield <= {2'b00, bitfield[31:2]};
181 multiplicand <= {multiplicand[29:0], 2'b00};
183 (bitfield[0] ? multiplicand : 0) +
184 (bitfield[1] ? {multiplicand[30:0], 1'b0} : 0);
185 if (bitfield == 0) begin
195 input Nrst, /* XXX not used yet */
204 output reg [31:0] result,
205 output reg [31:0] cpsr_out,
209 wire flag_n, flag_z, flag_c, flag_v, setres;
210 wire [32:0] sum, diff, rdiff;
211 wire sum_v, diff_v, rdiff_v;
213 assign sum = {1'b0, in0} + {1'b0, in1};
214 assign diff = {1'b0, in0} - {1'b0, in1};
215 assign rdiff = {1'b0, in1} + {1'b0, in0};
216 assign sum_v = (in0[31] ^~ in1[31]) & (sum[31] ^ in0[31]);
217 assign diff_v = (in0[31] ^ in1[31]) & (diff[31] ^ in0[31]);
218 assign rdiff_v = (in0[31] ^ in1[31]) & (rdiff[31] ^ in1[31]);
223 flag_c = cpsr[`CPSR_C];
224 flag_v = cpsr[`CPSR_V];
228 flag_c = shifter_carry;
233 flag_c = shifter_carry;
237 {flag_c, result} = diff;
242 {flag_c, result} = rdiff;
247 {flag_c, result} = sum;
252 {flag_c, result} = sum + {32'b0, cpsr[`CPSR_C]};
253 flag_v = sum_v | (~sum[31] & result[31]);
257 {flag_c, result} = diff - {32'b0, (~cpsr[`CPSR_C])};
258 flag_v = diff_v | (diff[31] & ~result[31]);
262 {flag_c, result} = rdiff - {32'b0, (~cpsr[`CPSR_C])};
263 flag_v = rdiff_v | (rdiff[31] & ~result[31]);
268 flag_c = shifter_carry;
273 flag_c = shifter_carry;
277 {flag_c, result} = diff;
282 {flag_c, result} = sum;
288 flag_c = shifter_carry;
293 flag_c = shifter_carry;
297 result = in0 & (~in1);
298 flag_c = shifter_carry;
303 flag_c = shifter_carry;
308 flag_z = (result == 0);
311 cpsr_out = setflags ? {flag_n, flag_z, flag_c, flag_v, cpsr[27:0]} : cpsr;