URL
https://opencores.org/ocsvn/forwardcom/forwardcom/trunk
Subversion Repositories forwardcom
[/] [forwardcom/] [trunk/] [alu.sv] - Rev 41
Go to most recent revision | Compare with Previous | Blame | View Log
//////////////////////////////////////////////////////////////////////////////////// Engineer: Agner Fog//// Create Date: 2020-06-06// Last modified: 2021-08-03// Module Name: decoder// Project Name: ForwardCom soft core// Target Devices: Artix 7// Tool Versions: Vivado v. 2020.1// License: CERN-OHL-W// Description: Arithmetic-logic unit for general purpose registers.// Executes add, subtract, bit manipulation, etc.//////////////////////////////////////////////////////////////////////////////////`include "defines.vh"`include "subfunctions.vh"module alu (input clock, // system clockinput clock_enable, // clock enable. Used when single-steppinginput reset, // system resetinput valid_in, // data from previous stage readyinput stall_in, // pipeline is stalledinput [`CODE_ADDR_WIDTH-1:0] instruction_pointer_in, // address of current instructioninput [31:0] instruction_in, // current instruction, only first word used hereinput [`TAG_WIDTH-1:0] tag_val_in, // instruction tag valueinput [1:0] category_in, // 00: multiformat, 01: single format, 10: jumpinput mask_alternative_in, // mask register and fallback register used for alternative purposesinput [1:0] result_type_in, // type of result: 0: register, 1: system register, 2: memory, 3: other or nothinginput vector_in, // vector registers usedinput [6:0] opx_in, // operation ID in execution unit. This is mostly equal to op1 for multiformat instructionsinput [5:0] opj_in, // operation ID for conditional jump instructionsinput [2:0] ot_in, // operand typeinput [5:0] option_bits_in, // option bits from IM3 or maskinput [15:0] im2_bits_in, // constant bits from IM2 as extra operand// monitor result buses:input write_en1, // a result is written to writeport1input [`TAG_WIDTH-1:0] write_tag1_in, // tag of result inwriteport1input [`RB1:0] writeport1_in, // result bus 1input write_en2, // a result is written to writeport2input [`TAG_WIDTH-1:0] write_tag2_in, // tag of result inwriteport2input [`RB1:0] writeport2_in, // result bus 2input [`TAG_WIDTH-1:0] predict_tag1_in, // result tag value on writeport1 in next clock cycleinput [`TAG_WIDTH-1:0] predict_tag2_in, // result tag value on writeport2 in next clock cycle// Register values sampled from result bus in previous stagesinput [`RB:0] operand1_in, // first register operand or fallbackinput [`RB:0] operand2_in, // second register operand RSinput [`RB:0] operand3_in, // last register operand RTinput [`MASKSZ:0] regmask_val_in, // mask registerinput [`RB1:0] ram_data_in, // memory operand from data raminput opr2_from_ram_in, // value of operand 2 comes from data raminput opr3_from_ram_in, // value of last operand comes from data raminput opr1_used_in, // operand1_in is neededinput opr2_used_in, // operand2_in is neededinput opr3_used_in, // operand3_in is neededinput regmask_used_in, // regmask_val_in is neededoutput reg valid_out, // for debug display: alu is activeoutput reg register_write_out,output reg [5:0] register_a_out, // register to writeoutput reg [`RB1:0] result_out, // output result to destination registeroutput reg [`TAG_WIDTH-1:0] tag_val_out,// instruction tag valueoutput reg jump_out, // jump instruction: jump takenoutput reg nojump_out, // jump instruction: jump not takenoutput reg [`CODE_ADDR_WIDTH-1:0] jump_pointer_out, // jump target to fetch unitoutput reg stall_out, // alu is waiting for an operand or not ready to receive a new instructionoutput reg stall_next_out, // alu will be waiting in next clock cycleoutput reg error_out, // unknown instructionoutput reg error_parm_out, // wrong parameter for instruction// outputs for debugger:output reg [31:0] debug1_out, // debug informationoutput reg [31:0] debug2_out // temporary debug information);logic [`RB1:0] operand1; // first register operand RD or RU. bit `RB is 1 if invalidlogic [`RB1:0] operand2; // second register operand RS. bit `RB is 1 if invalidlogic [`RB1:0] operand3; // last register operand RT. bit `RB is 1 if invalidlogic [`MASKSZ:0] regmask_val; // mask registerlogic [1:0] otout; // operand type for outputlogic [5:0] msb; // index to most significant bitlogic signbit2, signbit3; // sign bits of operandslogic [`RB1:0] sbit; // position of sign bitlogic [`RB1:0] result; // result for outputlogic [1:0] result_type; // type of resultlogic [6:0] opx; // operation ID in execution unit. This is mostly equal to op1 for multiformat instructionslogic [6:0] opj; // operation ID for conditional jumplogic jump_result; // result of jump condition (needs inversion if opj[0])logic mask_off; // result is masked offlogic stall; // waiting for operandslogic stall_next; // will be waiting for operands in next clock cyclelogic error; // unknown instructionlogic error_parm; // wrong parameter for instructionlogic jump_taken; // conditional jump is jumpinglogic jump_not_taken; // conditional jump is not jumping or target follows immediatelylogic normal_output; // normal register outputlogic [`CODE_ADDR_WIDTH-1:0] nojump_target; // next address if not jumpinglogic [`CODE_ADDR_WIDTH-1:0] relative_jump_target; // jump target for multiway relative jump// It seems to be more efficient to truncate operands locally by ANDing with sizemask than to// make separate wires for the truncated operands, because wiring is more expensive than logic:logic [`RB1:0] sizemask; // mask for operand typealways_comb beginstall = 0;stall_next = 0;regmask_val = 0;// get all inputsif (regmask_val_in[`MASKSZ]) begin // value missingif (write_en1 && regmask_val_in[`TAG_WIDTH-1:0] == write_tag1_in) beginregmask_val = writeport1_in; // obtained from result bus 1 (which may be my own output)end else if (write_en2 && regmask_val_in[`TAG_WIDTH-1:0] == write_tag2_in) beginregmask_val = writeport2_in[(`MASKSZ-1):0]; // obtained from result bus 2end else beginif (regmask_used_in) beginstall = 1; // operand not readyif (regmask_val_in[`TAG_WIDTH-1:0] != predict_tag1_in && regmask_val_in[`TAG_WIDTH-1:0] != predict_tag2_in) beginstall_next = 1; // operand not ready in next clock cycleendendendend else begin // value availableregmask_val = regmask_val_in;end// result is masked offmask_off = regmask_used_in && regmask_val[`MASKSZ] == 0 && regmask_val[0] == 0 && !mask_alternative_in;operand1 = 0;if (operand1_in[`RB]) begin // value missingif (write_en1 && operand1_in[`TAG_WIDTH-1:0] == write_tag1_in) beginoperand1 = writeport1_in; // obtained from result bus 1 (which may be my own output)end else if (write_en2 && operand1_in[`TAG_WIDTH-1:0] == write_tag2_in) beginoperand1 = writeport2_in; // obtained from result bus 2end else beginif (opr1_used_in) beginstall = 1; // operand not readyif (operand1_in[`TAG_WIDTH-1:0] != predict_tag1_in && operand1_in[`TAG_WIDTH-1:0] != predict_tag2_in) beginstall_next = 1; // operand not ready in next clock cycleendendendend else beginoperand1 = operand1_in[`RB1:0];endoperand2 = 0;if (opr2_from_ram_in) beginoperand2 = ram_data_in;end else if (operand2_in[`RB]) begin // value missingif (write_en1 && operand2_in[`TAG_WIDTH-1:0] == write_tag1_in) beginoperand2 = writeport1_in; // obtained from result bus 1 (which may be my own output)end else if (write_en2 && operand2_in[`TAG_WIDTH-1:0] == write_tag2_in) beginoperand2 = writeport2_in; // obtained from result bus 2end else beginif (opr2_used_in /*&& !mask_off*/) begin // mask_off removed because of critical timingstall = 1; // operand not readyif (operand2_in[`TAG_WIDTH-1:0] != predict_tag1_in && operand2_in[`TAG_WIDTH-1:0] != predict_tag2_in) beginstall_next = 1; // operand not ready in next clock cycleendendendend else begin // value availableoperand2 = operand2_in[`RB1:0];endoperand3 = 0;if (opr3_from_ram_in) beginoperand3 = ram_data_in;end else if (operand3_in[`RB]) begin // value missingif (write_en1 && operand3_in[`TAG_WIDTH-1:0] == write_tag1_in) beginoperand3 = writeport1_in; // obtained from result bus 1 (which may be my own output)end else if (write_en2 && operand3_in[`TAG_WIDTH-1:0] == write_tag2_in) beginoperand3 = writeport2_in; // obtained from result bus 2end else beginif (opr3_used_in /*&& !mask_off*/) begin // mask_off removed because of critical timingstall = 1; // operand not readyif (operand3_in[`TAG_WIDTH-1:0] != predict_tag1_in && operand3_in[`TAG_WIDTH-1:0] != predict_tag2_in) beginstall_next = 1; // operand not ready in next clock cycleendendendend else begin // value availableoperand3 = operand3_in[`RB1:0];endopx = opx_in; // operation ID in execution unit. This is mostly equal to op1 for multiformat instructionsopj = opj_in; // operation ID for conditional jumpresult = 0;jump_result = 0;otout = ot_in[1:0]; // operand type for outputresult_type = result_type_in;jump_taken = 0;jump_not_taken = 0;nojump_target = 0;relative_jump_target = 0;error = 0;error_parm = 0;// auxiliary variables depending on operand typecase (ot_in[1:0])0: begin // 8 bitmsb = 7; // most significant bitsbit = 8'H80; // sign bitsizemask = 8'HFF; // mask off unused bitssignbit2 = operand2[7]; // sign bit of operand 2signbit3 = operand3[7]; // sign bit of operand 3end1: begin // 16 bitmsb = 15; // most significant bitsbit = 16'H8000; // sign bitsizemask = 16'HFFFF; // mask off unused bitssignbit2 = operand2[15]; // sign bit of operand 2signbit3 = operand3[15]; // sign bit of operand 3end2: begin // 32 bitmsb = 31; // most significant bitsbit = 32'H80000000; // sign bitsizemask = 32'HFFFFFFFF; // mask off unused bitssignbit2 = operand2[31]; // sign bit of operand 2signbit3 = operand3[31]; // sign bit of operand 3end3: begin // 64 bit, or 32 if 64 bit not supportedmsb = `RB1; // most significant bitsbit = {1'b1,{(`RB-1){1'b0}}}; // sign bitsizemask = ~(`RB'b0); // mask off unused bitssignbit2 = operand2[`RB1]; // sign bit of operand 2signbit3 = operand3[`RB1]; // sign bit of operand 3endendcase////////////////////////////////////////////////// Select ALU operation////////////////////////////////////////////////if (opx == `II_MOVE || opx == `II_STORE) begin// simple move instructionsresult = operand3;end else if (opx == `IX_READ_SPEC || opx == `IX_WRITE_SPEC) begin// read or write special registersresult = operand2;end else if (opx == `II_SIGN_EXTEND || opx == `II_SIGN_EXTEND_ADD || opx == `IX_RELATIVE_JUMP) begin// instructions involving sign extensionlogic [`RB1:0] sign_ex; // result of sign extensionlogic [`RB1:0] sign_ex_sc; // result of sign extension and scalingotout = 3; // 64 bit output// sign extend:case (ot_in[1:0])0: sign_ex = {{56{operand3[ 7]}},operand3[7:0]}; // 8 bit1: sign_ex = {{48{operand3[15]}},operand3[15:0]}; // 16 bit2: sign_ex = {{32{operand3[31]}},operand3[31:0]}; // 32 bit3: sign_ex = operand3[`RB1:0]; // 64 bitendcaseif (opx == `II_SIGN_EXTEND_ADD) begin// scale sign_ex.// The scale factor is limited to 3 here for timing reasons so that it fits a 6-input LUT// A full barrel shifter takes too much timecase (option_bits_in[1:0]) // optional shift count in option bits0: sign_ex_sc = sign_ex; // scale factor 11: sign_ex_sc = {sign_ex,1'b0}; // scale factor 22: sign_ex_sc = {sign_ex,2'b0}; // scale factor 43: sign_ex_sc = {sign_ex,3'b0}; // scale factor 8endcaseresult = sign_ex_sc + operand2; // addif (|(option_bits_in[5:2])) error_parm = 1; // shift count > 3end else beginresult = sign_ex;endif (opx == `IX_RELATIVE_JUMP) beginrelative_jump_target = sign_ex + operand2[`RB1:2] - {1'b1,{(`CODE_ADDR_START-2){1'b0}}}; // subtract (code memory start)/4if (|(operand2[1:0])) error_parm = 1; // jump to misaligned addressendend else if (opx == `II_COMPARE || (opx >= `II_MIN && opx <= `II_MAX_U)) begin// instructions involving signed and unsigned compare. operation defined by option bitslogic b1, b2, b3, eq, less; // intermediate resultslogic [`RB1:0] sbit1;b1 = 0; b2 = 0; b3 = 0; eq = 0; less = 0;// flip a 1 in the sign bit position if comparison is signed (option_bits_in[3] = 0)sbit1 = option_bits_in[3] ? `RB'b0 : sbit; // sign bit if signedeq = (operand2 & sizemask) == (operand3 & sizemask); // operands are equalless = ((operand2 & sizemask) ^ sbit1) < ((operand3 & sizemask) ^ sbit1); // a < b, signed or unsignedif (option_bits_in[2:1] == 0) beginb1 = eq; // a == bend else if (option_bits_in[2:1] == 1) beginb1 = less; // a < bend else if (option_bits_in[2:1] == 2) beginb1 = ~less & ~eq; // a > bend else beginlogic [`RB1:0] absa;logic [`RB1:0] absb;absa = signbit2 ? -operand2 : operand2; // abs(a)absb = signbit3 ? -operand3 : operand3; // abs(b)b1 = (absa & sizemask) < (absb & sizemask); // abs(a) < abs(b)endjump_result = b1; // result for conditional jumpb2 = b1 ^ option_bits_in[0]; // bit 0 of condition code inverts the result// alternative use of maskcase (option_bits_in[5:4])2'b00: b3 = regmask_val[0] ? b2 : operand1[0]; // normal fallback2'b01: b3 = regmask_val[0] & b2 & operand1[0]; // mask & result & fallback2'b10: b3 = regmask_val[0] & (b2 | operand1[0]); // mask & (result | fallback)2'b11: b3 = regmask_val[0] & (b2 ^ operand1[0]); // mask & (result ^ fallback)endcase// copy remaining bits from maskif (opx == `II_COMPARE && instruction_in[`MASK] != 3'b111) beginresult[`RB1:1] = regmask_val[(`MASKSZ-1):1];endif (opx >= `II_MIN) begin// min and max instructionsresult = b1 ? operand2 : operand3;end else if (regmask_used_in | mask_alternative_in) begin// combine result with rest of mask or NUMCONTRresult = {regmask_val[(`MASKSZ-1):1],b3}; // get remaining bits from maskend else begin// normal compareresult = b3;endend else if (opx == `II_ADD || opx == `II_SUB) begin// addition, subtraction, and conditional jumps involving addition or subtractionlogic [`RB:0] bigresult; // one extra bit on result for carrylogic zero; // result is zerologic sign; // sign of resultlogic carry; // unsigned carry/borrowlogic overflow; // signed overflowif (~opx[0]) bigresult = operand2 + operand3; // addelse bigresult = operand2 - operand3; // subtractresult = bigresult[`RB1:0]; // result without extra carry bitcase (ot_in[1:0])0: begin // 8 bitsign = bigresult[7]; // sign bitcarry = bigresult[8]; // carry out (unsigned overflow)end1: begin // 16 bitsign = bigresult[15]; // sign bitcarry = bigresult[16]; // carry out (unsigned overflow)end2: begin // 32 bitsign = bigresult[31]; // sign bitcarry = bigresult[32]; // carry out (unsigned overflow)end3: begin // 64 bit (or 32)sign = bigresult[`RB1]; // sign bitcarry = bigresult[`RB]; // carry out (unsigned overflow)endendcasezero = ~|(result & sizemask); // result is zerooverflow = (signbit2 ^ signbit3 ^ ~opx[0]) & (signbit2 ^ sign); // signed overflow// jump conditioncase (opj[3:1])`IJ_SUB_JZ >> 1: jump_result = zero;`IJ_SUB_JNEG >> 1: jump_result = sign;`IJ_SUB_JPOS >> 1: jump_result = ~sign & ~zero;`IJ_SUB_JOVFLW >> 1: jump_result = overflow;`IJ_SUB_JBORROW >> 1: jump_result = carry;default: jump_result = 0;endcaseend else if (opx == `II_AND || opx == `II_OR || opx == `II_XOR) beginif (opx == `II_AND) begin// bitwise AND, and conditional jumps involving thisresult = operand2[`RB1:0] & operand3[`RB1:0];end else if (opx == `II_OR) begin// bitwise OR, and conditional jumps involving thisresult = operand2[`RB1:0] | operand3[`RB1:0];end else if (opx == `II_XOR) begin// bitwise XOR, and conditional jumps involving thisresult = operand2[`RB1:0] ^ operand3[`RB1:0];endjump_result = ~|(result & sizemask); // zero condition for conditional jumpend else if (opx >= `II_CLEAR_BIT && opx <= `II_TEST_BITS_OR) begin// various bit manipulation instructionslogic [`RB1:0] onebit; // 1 in the position indicated by opr3logic rbit; // result bit from testrbit = 0;onebit = 0;if ((operand3 & sizemask) <= msb) onebit[operand3[5:0]] = 1'b1;// onebit = 1 ** opr3case (opx)`II_CLEAR_BIT: result = operand2 & ~ onebit;`II_SET_BIT: result = operand2 | onebit;`II_TOGGLE_BIT: result = operand2 ^ onebit;`II_TEST_BIT: beginrbit = |(operand2 & onebit);end`II_TEST_BITS_OR: beginrbit = |(operand2 & operand3 & sizemask);end`II_TEST_BITS_AND: beginrbit = ~|(((operand2 & operand3) ^ operand3) & sizemask);endendcasejump_result = rbit; // jump condition for bit testsif (opx >= `II_TEST_BIT && opx <= `II_TEST_BITS_OR) begin// alternative use of mask and fallback in bit test instructionslogic a, b, c;a = regmask_val[0] ^ option_bits_in[4]; // mask bit flipped by option bit 4b = rbit ^ option_bits_in[2]; // result bit flipped by option bit 2c = operand1[0] ^ option_bits_in[3]; // fallback bit flipped by option bit 3case (option_bits_in[1:0]) // boolean operations controlled by option bits 1-02'b00: result[0] = a ? b : c; // normal fallback2'b01: result[0] = a & (b & c); // mask & result & fallback2'b10: result[0] = a & (b | c); // mask & (result | fallback)2'b11: result[0] = a & (b ^ c); // mask & (result ^ fallback)endcaseif (option_bits_in[5]) begin // copy remaining bits from mask or NUMCONTRresult[`RB1:1] = regmask_val[(`MASKSZ-1):1];endendend else if ((opx >= `II_SHIFT_LEFT && opx <= `II_SHIFT_RIGHT_U) || opx == `II_FUNNEL_SHIFT|| opx == `IX_MOVE_BITS1 || opx == `IX_MOVE_BITS2) begin// shift instructions and other instruction involving shift and rotate// Barrel shifters are expensive in terms of LUT use.// Make one universal barrel shifter to use for all shift and rotate instructionslogic [(`RB*2-1):0] barrel; // input to barrel shifter. 2x32 or 2x64 bitslogic [`RB1:0] barrel_out; // output from barrel shifter. 32 or 64 bitslogic [5:0] shift_count1; // shift count for barrel shifterlogic [5:0] shift_count2; // shift count for barrel shifter, limitedlogic overfl; // shift count overflowsif (opx == `II_SHIFT_LEFT || opx == `II_ROTATE) beginshift_count1 = -operand3[5:0];end else beginshift_count1 = operand3[5:0];end// select input for barrel shifterbarrel = 0;if (ot_in[1:0] == 0) begin // 8 bitsshift_count2 = shift_count1[2:0];if (opx == `II_SHIFT_LEFT || opx == `IX_MOVE_BITS1) beginbarrel[15:8] = operand2[7:0];if (operand3[5:0] == 0) barrel[7:0] = operand2[7:0]; // no shiftend else if (opx == `II_SHIFT_RIGHT_S) beginbarrel[7:0] = operand2[7:0];barrel[15:8] = {8{operand2[7]}}; // sign bitend else if (opx == `II_SHIFT_RIGHT_U || opx == `IX_MOVE_BITS2) beginbarrel[7:0] = operand2[7:0];end else if (opx == `II_ROTATE) beginbarrel[7:0] = operand2[7:0];barrel[15:8] = operand2[7:0];end else begin // funnel shiftbarrel[7:0] = operand1[7:0];barrel[15:8] = operand2[7:0];endend else if (ot_in[1:0] == 1) begin // 16 bitsshift_count2 = shift_count1[3:0];if (opx == `II_SHIFT_LEFT || opx == `IX_MOVE_BITS1) beginbarrel[31:16] = operand2[15:0];if (operand3[5:0] == 0) barrel[15:0] = operand2[15:0]; // no shiftend else if (opx == `II_SHIFT_RIGHT_S) beginbarrel[15:0] = operand2[15:0];barrel[31:16] = {16{operand2[15]}}; // sign bitend else if (opx == `II_SHIFT_RIGHT_U || opx == `IX_MOVE_BITS2) beginbarrel[15:0] = operand2[15:0];end else if (opx == `II_ROTATE) beginbarrel[15:0] = operand2[15:0];barrel[31:16] = operand2[15:0];end else begin // funnel shiftbarrel[15:0] = operand1[15:0];barrel[31:16] = operand2[15:0];endend else if (ot_in[1:0] == 2 || `RB <= 32) begin // 32 bits (or 64 bits if not supported)shift_count2 = shift_count1[4:0];if (opx == `II_SHIFT_LEFT || opx == `IX_MOVE_BITS1) beginbarrel[63:32] = operand2[31:0];if (operand3[5:0] == 0) barrel[31:0] = operand2[31:0]; // no shiftend else if (opx == `II_SHIFT_RIGHT_S) beginbarrel[31:0] = operand2[31:0];barrel[63:32] = {32{operand2[31]}}; // sign bitend else if (opx == `II_SHIFT_RIGHT_U || opx == `IX_MOVE_BITS2) beginbarrel[31:0] = operand2[31:0];end else if (opx == `II_ROTATE) beginbarrel[31:0] = operand2[31:0];barrel[63:32] = operand2[31:0];end else begin // funnel shiftbarrel[31:0] = operand1[31:0];barrel[63:32] = operand2[31:0];endend else begin // 64 bits (if supported)shift_count2 = shift_count1[5:0];if (opx == `II_SHIFT_LEFT || opx == `IX_MOVE_BITS1) beginbarrel[127:64] = operand2[63:0];if (operand3[5:0] == 0) barrel[63:0] = operand2[63:0]; // no shiftend else if (opx == `II_SHIFT_RIGHT_S) beginbarrel[63:0] = operand2[63:0];barrel[127:64] = {64{operand2[63]}}; // sign bitend else if (opx == `II_SHIFT_RIGHT_U || opx == `IX_MOVE_BITS2) beginbarrel[63:0] = operand2[63:0];end else if (opx == `II_ROTATE) beginbarrel[63:0] = operand2[63:0];barrel[127:64] = operand2[63:0];end else begin // funnel shiftbarrel[63:0] = operand1[63:0];barrel[127:64] = operand2[63:0];endend// big barrel shifterbarrel_out = barrel[shift_count2+:`RB];// select outputoverfl = (operand3 & sizemask) > msb; // check if shift count overflowsif (opx == `IX_MOVE_BITS1 || opx == `IX_MOVE_BITS2) begin // move_bits instruction// insert shift result in destination bit fieldinteger i;for (i = 0; i < `RB; i++) beginif (i >= im2_bits_in[13:8] && i <= option_bits_in) result[i] = barrel_out[i];else result[i] = operand1[i];endend else if (overfl) beginif (opx == `II_SHIFT_RIGHT_S) result = {`RB{signbit2}}; // shift right overflows to sign bitelse if (opx == `II_ROTATE) result = barrel_out; // rotate has no overflowelse result = 0; // all other shifts overflow to zeroend else beginresult = barrel_out; // result of shift or rotateendend else if (opx == `II_ADD_ADD) begin// 3-operand add. signs are controlled by option bits// (this is separate from the add and subtract operations with conditional jumps because the timing is critical)logic [`RB1:0] r1, r2, r3;r1 = option_bits_in[0] ? -operand1[`RB1:0] : operand1[`RB1:0];r2 = option_bits_in[1] ? -operand2[`RB1:0] : operand2[`RB1:0];r3 = option_bits_in[2] ? -operand3[`RB1:0] : operand3[`RB1:0];result = r1 + r2 + r3;end else if (opx == `II_SELECT_BITS) begin// select_bits instructionresult = (operand1[`RB1:0] & operand3[`RB1:0]) | (operand2[`RB1:0] & ~operand3[`RB1:0]);// bit scan is critical in terms of timing. Several different implementations tried here:`define BITSCAN_BASED_ON_ROUNDP2`ifdef BITSCAN_BASED_ON_ROUNDP2 // bit scan and roundp2 instructions combined. This takes less resourcesend else if (opx == `IX_BIT_SCAN || opx == `IX_ROUNDP2) begin//// using bit index method because this makes roundp2 simplelogic [`RB1:0] a; // intermediate resultslogic [`RB1:0] b;logic [`RB1:0] c;logic [`RB1:0] d;logic [6:0] bitscan_result;logic [5:0] r;logic iszero; // input is zerologic ispow2; // input is a power of 2r = 0; iszero = 0;a = operand2 & sizemask;ispow2 = ~|(a & (a-1)); // a is a power of 2if (opx == `IX_ROUNDP2 || operand3[0]) begin// bitscan reverse scan`ifdef SUPPORT_64BITb = reversebits64(a); // reverse order of bits (in subfunctions.vh)c = b & ~(b-1); // isolate lowest 1-bitd = reversebits64(c); // reverse back again`elseb = reversebits32(a); // reverse order of bits (in subfunctions.vh)c = b & ~(b-1); // isolate lowest 1-bitd = reversebits32(c); // reverse back again`endifend else begin// bitscan forward scand = a & ~(a-1); // isolate lowest 1-bitend// bitindex implemented in subfunctions.vhbitscan_result = bitindex(d);r = bitscan_result[6:1];iszero = bitscan_result[0];if (iszero) begin // input is zero. output determined by option bit 1if (operand3[4]) beginresult = ~(`RB'b0); // return -1 if zeroend else beginresult = `RB'b0; // return 0 if zeroendend else if (opx == `IX_BIT_SCAN) beginresult = r; // output resultend else if (!operand3[0] || ispow2) begin// roundp2 round down to nearest power of 2result = d;end else begin// round up to nearest power of 2if (signbit2) begin // overflowresult = operand3[5] ? ~(`RB'b0) : 0; // return 0 or -1 if overflowend else beginresult = {d,1'b0}; // round upendend`else // bit scan and roundp2 instructions implemented separatelyend else if (opx == `IX_ROUNDP2) beginlogic [`RB1:0] a; // intermediate resultslogic [`RB1:0] b;logic [`RB1:0] c;logic [`RB1:0] d;logic iszero; // input is zerologic ispow2; // input is a power of 2a = operand2 & sizemask; // cut off input to desired operand sizeiszero = ~|a; // input is zeroispow2 = ~|(a & (a-1)); // input is a power of 2`ifdef SUPPORT_64BITb = reversebits64(a); // reverse order of bits (in subfunctions.vh)c = b & ~(b-1); // isolate lowest 1-bitd = reversebits64(c); // reverse back again`elseb = reversebits32(a); // reverse order of bits (in subfunctions.vh)c = b & ~(b-1); // isolate lowest 1-bitd = reversebits32(c); // reverse back again`endifif (iszero) begin // input is zero. output determined by option bit 4if (operand3[4]) beginresult = ~(`RB'b0); // return -1 if zeroend else beginresult = 0; // return 0 if zeroendend else if (~operand3[0] | ispow2) begin// roundp2 round down to nearest power of 2result = d;end else begin// round up to nearest power of 2if (signbit2) begin // overflowresult = operand3[5] ? ~(`RB'b0) : 0; // return 0 or -1 if overflowend else beginresult = {d,1'b0}; // round upendendend else if (opx == `IX_BIT_SCAN) beginlogic [`RB1:0] a; // input cut off to desired operand sizelogic [`RB1:0] b; // input with bits reversedlogic [`RB1:0] c; // input bits reversed if forward scanlogic [6:0] r; // bitscan resultlogic iszero; // input is zeroa = operand2 & sizemask; // cut off input to desired operand size// reverse bits if forward scancase (ot_in[1:0])0: b = reversebits8(operand2[7:0]); // 8 bit1: b = reversebits16(operand2[15:0]); // 16 bit`ifdef SUPPORT_64BIT3: b = reversebits64(operand2[63:0]); // 64 bit`endifdefault: b = reversebits32(operand2[31:0]); // 32 bitendcaseif (operand3[0]) c = a; // reverse scanelse c = b; // forward scan// bitscan function defined in subfunctions.vhr = bitscan64A(a); // this implementation may be faster?//r = bitscan64C(c); // alternative implementationiszero = r[0]; // input is zeroif (iszero) begin // input is zero. output determined by option bit 4if (operand3[4]) beginresult = ~(`RB'b0); // return -1 if zeroend else beginresult = 0; // return 0 if zeroendend else beginresult = r[6:1]; // normal bitscan resultend`endifend else if (opx == `IX_POPCOUNT) begin// popcount instruction. functions are is in subfunctions.vhif (`RB <= 32) result = popcount32(operand2 & sizemask);else result = popcount64(operand2 & sizemask);end else if (opx == `IX_ABS) begin// abs instructionif (~signbit2) beginresult = operand2; // input is not negativeend else if ((operand2 & ~sbit & sizemask) == 0) begin// overflowcase (operand3[1:0]) // last operand determines what to do with overflow0: result = operand2; // overfloaw wraps around1: result = ~sbit; // overfloaw gives saturation2: result = 0; // overflow gives 0endcaseend else beginresult = -operand2; // input is negative. change signendend else if (opx == `IX_TRUTH_TAB3) begin// truth_tab3 instruction// truth_table_lookup is in subfunctions.vhresult = truth_table_lookup(operand1, operand2, operand3, im2_bits_in[7:0]);if (option_bits_in[0]) result[`RB1:1] = 0; // output only bit 0else if (option_bits_in[1]) result[`RB1:1] = regmask_val[(`MASKSZ-1):1]; // remaining bits from maskend else if (opx == `IX_INSERT_HI) begin// insert constant into high 32 bits, leave low 32 bit unchanged`ifdef SUPPORT_64BITresult = {operand3[31:0],operand2[31:0]};`elseresult = operand2;`endifend else if (category_in == `CAT_JUMP) begin// jump instructions that have no corresponding general instructionif (opj[5:0] >= `IJ_INC_COMP_JBELOW && opj[5:0] <= `IJ_INC_COMP_JABOVE+1) begin// loop instruction: increment and jump if below/above`ifdef THIS_VERSION_IS_SLOW__IT_IS_NOT_USED// This version is slow because the addition and the compare both involve a big carry-lookahead circuit.// Use this version only if timing is not criticallogic eq, less;result = operand2 + 1; // incrementeq = (result & sizemask) == (operand3 & sizemask); // operands are equalless = ((result & sizemask) ^ sbit) < ((operand3 & sizemask) ^ sbit); // a+1 < b, signedif (opj[1]) beginjump_result = ~less & ~eq; // aboveend else beginjump_result = less; // belowend`else// This version is faster because it does most of the compare in parallel with the additionlogic less; // a < b, signedlogic result_equal_limit; // a + 1 == blogic b_is_min; // the limit b is INT_MIN. a+1 < b always falselogic overflow1; // a+1 overflows// The overflow check may not be important, but we want to make sure that the result is always// the same as if the increment and the compare are coded as two separate instructionsresult = operand2 + 1; // incrementless = ((operand2 & sizemask) ^ sbit) < ((operand3 & sizemask) ^ sbit); // a < b, signedoverflow1 = ((operand2 & sizemask) ^ sbit) == sizemask; // a+1 overflowsb_is_min = ((operand3 & sizemask) ^ sbit) == 0; // limit is INT_MIN, nothing is less than limitresult_equal_limit = ((result ^ operand3) & sizemask) == 0; // a + 1 == bif (opj[1]) begin // increment_compare/jump_above// check if a+1 > b <=> !(a+1 <= b) <=> !(a < b || overflow)jump_result = ~(less | overflow1); // a+1 > bend else begin // increment_compare/jump_below// check if a+1 < b <=> (a < b && a+1 != b) || (overflow && b != INT_MIN)jump_result = (less & ~result_equal_limit) | (overflow1 & ~b_is_min); // a + 1 < bend`endifend else if (opj[5:1] == `IJ_SUB_MAXLEN_JPOS >> 1) begin// vector loop instruction: subtract maximum vector length and jump if positivelogic [`RB1:0] max_vector_length;logic sign; // sign of resultlogic zero; // result is zeroif (`NUM_VECTOR_UNITS > 0) max_vector_length = `NUM_VECTOR_UNITS * 8;else max_vector_length = 8; // make sure max_vector_length is not zero to avoid infinite loopresult = operand2 - max_vector_length;zero = ~|(result & sizemask);case (ot_in[1:0])0: sign = result[7]; // 8 bit1: sign = result[15]; // 16 bit2: sign = result[31]; // 32 bit3: sign = result[`RB1]; // 64 bit (or 32)endcase`ifdef SUPPORT_64BITif (instruction_in[`IL] == 1) begin// 64 bits in format Cotout = 3; // 64 bit outputsign = result[`RB1];zero = ~|result;end`endifjump_result = ~sign & ~zero;endend else if (opx == `II_NOP) begin// nop instruction. do nothingend else begin// unknown instruction. errorerror = 1;endif (vector_in) error = 1; // Vector instructions not supported yetif (category_in == `CAT_JUMP) begin// manage conditional jump conditionslogic [1:0] il;logic [2:0] mode;il = instruction_in[`IL];mode = instruction_in[`MODE];// calculate target if not jumping//instruction_length = il[1] ? il : 1; // il cannot be 0 for jump instructions)nojump_target = instruction_pointer_in + il;// treat jump as not taken if jump target is equal to nojump targetjump_not_taken = nojump_target == operand1_in;// detect jump resultif (jump_result ^ opj[0]) jump_taken = 1; // bit 0 of opj inverts the conditionif (opj > `IJ_LAST_CONDITIONAL) jump_taken = 1; // unconditional jump always takenif (opj == `IJ_TRAP) begin // trap and IJ_SYSCALL have same opj. Both will stop debuggerjump_taken = 0; // use trap as debug breakpoint. Resume execution in next instructionend// compare, test and indirect jumps have no register return. The decoder takes care of result_type = `RESULT_NONE;end// normal register output// regmask_used_in removed from this equation because of critical timing:normal_output = valid_in & ~stall & ~stall_in& (result_type == `RESULT_REG | result_type == `RESULT_SYS)& (regmask_val[0] | mask_alternative_in) & ~vector_in;end// outputsalways_ff @(posedge clock) if (clock_enable) beginif (normal_output) begin// normal register outputcase (otout)0: result_out <= result[7:0];1: result_out <= result[15:0];2: result_out <= result[31:0];3: result_out <= result[`RB1:0];endcaseregister_write_out <= ~reset;tag_val_out <= tag_val_in;// destination register number. high bit is 1 for system registersregister_a_out <= {result_type[0],instruction_in[`RD]};end else if (!valid_in || stall || stall_in || result_type == `RESULT_MEM || result_type == `RESULT_NONE || vector_in) begin// stall_in must disable the output to avoid executing the same instruction twice.// note: the FPGA has no internal tri-state buffers. We need to simulate result bus by or'ing outputsregister_write_out <= 0;result_out <= 0;register_a_out <= 0;tag_val_out <= 0;end else /*if (!regmask_val[0] && !mask_alternative_in) */ begin// mask is zero. output is fallbackcase (otout)0: result_out <= operand1[7:0];1: result_out <= operand1[15:0];2: result_out <= operand1[31:0];3: result_out <= operand1[`RB1:0];endcaseregister_write_out <= ~reset;register_a_out <= {1'b0,instruction_in[`RD]};tag_val_out <= tag_val_in;endif (stall || stall_in || !valid_in) beginjump_out <= 0;nojump_out <= 0;end else if (category_in == `CAT_JUMP) begin// additional output for conditional jump instructionsif (jump_not_taken | ~jump_taken) beginjump_out <= 0;nojump_out <= valid_in;jump_pointer_out <= nojump_target;end else begin // jump takenjump_out <= valid_in && !reset;nojump_out <= 0;endend else begin// not a jump instructionjump_out <= 0;nojump_out <= 0;jump_pointer_out <= 0;end// special cases for indirect jumpsif (opx == `IX_INDIRECT_JUMP) beginjump_pointer_out <= operand3[`RB1:2] - {1'b1,{(`CODE_ADDR_START-2){1'b0}}}; // jump target = (last operand - code memory start)/ 4if (|(operand3[1:0])) error_parm_out <= 1; // misaligned jump targetend else if (opx == `IX_RELATIVE_JUMP) begin // jump target is calculatedjump_pointer_out <= relative_jump_target;end else beginjump_pointer_out <= operand1_in; // jump target is calculated in previous stageend// other outputsvalid_out <= !stall & valid_in & !reset; // a valid output is producedstall_out <= stall & valid_in & !reset; // stalled. waiting for operandstall_next_out <= stall_next & valid_in & !reset; // predict stall in next clock cycleerror_out <= error & valid_in & !reset; // unknown instructionerror_parm_out <= error_parm & valid_in & !reset; // wrong parameter// outputs for debugger:debug1_out <= 0;debug1_out[6:0] <= opx;debug1_out[14:8] <= opj;debug1_out[21:20] <= category_in;debug1_out[24] <= stall;debug1_out[25] <= stall_next;debug1_out[27] <= error;debug1_out[28] <= jump_taken;debug1_out[29] <= jump_not_taken;debug1_out[30] <= jump_result;debug1_out[31] <= valid_in;debug2_out[16] <= opr1_used_in;debug2_out[17] <= opr2_used_in;debug2_out[18] <= opr3_used_in;debug2_out[19] <= regmask_used_in;debug2_out[20] <= mask_alternative_in;debug2_out[21] <= mask_off;/*debug2_out[22] <= regmask_val_in[0];debug2_out[23] <= regmask_val_in[`MASKSZ];debug2_out[27:24] <= regmask_val[3:0];debug2_out[28] <= regmask_val[`MASKSZ];*/endendmodule
Go to most recent revision | Compare with Previous | Blame | View Log
