| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| `timescale 1ns / 1ps |
| `default_nettype none |
| `include "rv32i_header.vh" |
|
|
| module rv32i_alu( |
| input wire i_clk,i_rst_n, |
| input wire[`ALU_WIDTH-1:0] i_alu, |
| input wire[4:0] i_rs1_addr, |
| output reg[4:0] o_rs1_addr, |
| input wire[31:0] i_rs1, |
| output reg[31:0] o_rs1, |
| input wire[31:0] i_rs2, |
| output reg[31:0] o_rs2, |
| input wire[31:0] i_imm, |
| output reg[11:0] o_imm, |
| input wire[2:0] i_funct3, |
| output reg[2:0] o_funct3, |
| input wire[`OPCODE_WIDTH-1:0] i_opcode, |
| output reg[`OPCODE_WIDTH-1:0] o_opcode, |
| input wire[`EXCEPTION_WIDTH-1:0] i_exception, |
| output reg[`EXCEPTION_WIDTH-1:0] o_exception, |
| output reg[31:0] o_y, |
| |
| input wire[31:0] i_pc, |
| output reg[31:0] o_pc, |
| output reg[31:0] o_next_pc, |
| output reg o_change_pc, |
| |
| output reg o_wr_rd, |
| input wire[4:0] i_rd_addr, |
| output reg[4:0] o_rd_addr, |
| output reg[31:0] o_rd, |
| output reg o_rd_valid, |
| |
| output reg o_stall_from_alu, |
| input wire i_ce, |
| output reg o_ce, |
| input wire i_stall, |
| input wire i_force_stall, |
| output reg o_stall, |
| input wire i_flush, |
| output reg o_flush |
| ); |
|
|
| wire alu_add = i_alu[`ADD]; |
| wire alu_sub = i_alu[`SUB]; |
| wire alu_slt = i_alu[`SLT]; |
| wire alu_sltu = i_alu[`SLTU]; |
| wire alu_xor = i_alu[`XOR]; |
| wire alu_or = i_alu[`OR]; |
| wire alu_and = i_alu[`AND]; |
| wire alu_sll = i_alu[`SLL]; |
| wire alu_srl = i_alu[`SRL]; |
| wire alu_sra = i_alu[`SRA]; |
| wire alu_eq = i_alu[`EQ]; |
| wire alu_neq = i_alu[`NEQ]; |
| wire alu_ge = i_alu[`GE]; |
| wire alu_geu = i_alu[`GEU]; |
| wire opcode_rtype = i_opcode[`RTYPE]; |
| wire opcode_itype = i_opcode[`ITYPE]; |
| wire opcode_load = i_opcode[`LOAD]; |
| wire opcode_store = i_opcode[`STORE]; |
| wire opcode_branch = i_opcode[`BRANCH]; |
| wire opcode_jal = i_opcode[`JAL]; |
| wire opcode_jalr = i_opcode[`JALR]; |
| wire opcode_lui = i_opcode[`LUI]; |
| wire opcode_auipc = i_opcode[`AUIPC]; |
| wire opcode_system = i_opcode[`SYSTEM]; |
| wire opcode_fence = i_opcode[`FENCE]; |
|
|
| reg[31:0] a; |
| reg[31:0] b; |
| reg[31:0] y_d; |
| reg[31:0] rd_d; |
| reg wr_rd_d; |
| reg rd_valid_d; |
| reg[31:0] a_pc; |
| wire[31:0] sum; |
| wire stall_bit = o_stall || i_stall; |
|
|
| |
| always @(posedge i_clk, negedge i_rst_n) begin |
| if(!i_rst_n) begin |
| o_exception <= 0; |
| o_ce <= 0; |
| o_stall_from_alu <= 0; |
| end |
| else begin |
| if(i_ce && !stall_bit) begin |
| o_opcode <= i_opcode; |
| o_exception <= i_exception; |
| o_y <= y_d; |
| o_rs1_addr <= i_rs1_addr; |
| o_rs1 <= i_rs1; |
| o_rs2 <= i_rs2; |
| o_rd_addr <= i_rd_addr; |
| o_imm <= i_imm[11:0]; |
| o_funct3 <= i_funct3; |
| o_rd <= rd_d; |
| o_rd_valid <= rd_valid_d; |
| o_wr_rd <= wr_rd_d; |
| o_stall_from_alu <= i_opcode[`STORE] || i_opcode[`LOAD]; |
| o_pc <= i_pc; |
| end |
| if(i_flush && !stall_bit) begin |
| o_ce <= 0; |
| end |
| else if(!stall_bit) begin |
| o_ce <= i_ce; |
| end |
| else if(stall_bit && !i_stall) o_ce <= 0; |
| |
| end |
| |
| end |
|
|
| |
| always @* begin |
| y_d = 0; |
| |
| a = (opcode_jal || opcode_auipc)? i_pc:i_rs1; |
| b = (opcode_rtype || opcode_branch)? i_rs2:i_imm; |
| |
| if(alu_add) y_d = a + b; |
| if(alu_sub) y_d = a - b; |
| if(alu_slt || alu_sltu) begin |
| y_d = {31'b0, (a < b)}; |
| if(alu_slt) y_d = (a[31] ^ b[31])? {31'b0,a[31]}:y_d; |
| end |
| if(alu_xor) y_d = a ^ b; |
| if(alu_or) y_d = a | b; |
| if(alu_and) y_d = a & b; |
| if(alu_sll) y_d = a << b[4:0]; |
| if(alu_srl) y_d = a >> b[4:0]; |
| if(alu_sra) y_d = $signed(a) >>> b[4:0]; |
| if(alu_eq || alu_neq) begin |
| y_d = {31'b0, (a == b)}; |
| if(alu_neq) y_d = {31'b0,!y_d[0]}; |
| end |
| if(alu_ge || alu_geu) begin |
| y_d = {31'b0, (a >= b)}; |
| if(alu_ge) y_d = (a[31] ^ b[31])? {31'b0, b[31]}:y_d; |
| end |
| end |
|
|
| |
| |
| always @* begin |
| o_flush = i_flush; |
| rd_d = 0; |
| rd_valid_d = 0; |
| o_change_pc = 0; |
| o_next_pc = 0; |
| wr_rd_d = 0; |
| a_pc = i_pc; |
| if(!i_flush) begin |
| if(opcode_rtype || opcode_itype) rd_d = y_d; |
| if(opcode_branch && y_d[0]) begin |
| o_next_pc = sum; |
| o_change_pc = i_ce; |
| o_flush = i_ce; |
| end |
| if(opcode_jal || opcode_jalr) begin |
| if(opcode_jalr) a_pc = i_rs1; |
| o_next_pc = sum; |
| o_change_pc = i_ce; |
| o_flush = i_ce; |
| rd_d = i_pc + 4; |
| end |
| end |
| if(opcode_lui) rd_d = i_imm; |
| if(opcode_auipc) rd_d = sum; |
|
|
| if(opcode_branch || opcode_store || (opcode_system && i_funct3 == 0) || opcode_fence ) wr_rd_d = 0; |
| else wr_rd_d = 1; |
|
|
| if(opcode_load || (opcode_system && i_funct3!=0)) rd_valid_d = 0; |
| else rd_valid_d = 1; |
|
|
| |
| o_stall = (i_stall || i_force_stall) && !i_flush; |
| end |
| |
| assign sum = a_pc + i_imm; |
|
|
| `ifdef FORMAL |
| |
| wire[4:0] f_alu=i_alu[`ADD]+i_alu[`SUB]+i_alu[`SLT]+i_alu[`SLTU]+i_alu[`XOR]+i_alu[`OR]+i_alu[`AND]+i_alu[`SLL]+i_alu[`SRL]+i_alu[`SRA]+i_alu[`EQ]+i_alu[`NEQ]+i_alu[`GE]+i_alu[`GEU]+0; |
| wire[4:0] f_opcode=i_opcode[`RTYPE]+i_opcode[`ITYPE]+i_opcode[`LOAD]+i_opcode[`STORE]+i_opcode[`BRANCH]+i_opcode[`JAL]+i_opcode[`JALR]+i_opcode[`LUI]+i_opcode[`AUIPC]+i_opcode[`SYSTEM]+i_opcode[`FENCE]; |
|
|
| always @* begin |
| assume(f_alu <= 1); |
| assume(f_opcode <= 1); |
| end |
|
|
| |
| always @* begin |
| if(i_alu[`SLTU]) assert(y_d[0] == $unsigned(a) < $unsigned(b)); |
| if(i_alu[`SLT]) assert(y_d[0] == $signed(a) < $signed(b)); |
| if(i_alu[`SLL]) assert($unsigned(y_d) == $unsigned(a) << $unsigned(b[4:0])); |
| if(i_alu[`SRL]) assert($unsigned(y_d) == $unsigned(a) >> $unsigned(b[4:0])); |
| if(i_alu[`SRA]) assert($signed(y_d) == ($signed(a) >>> $unsigned(b[4:0]))); |
| if(i_alu[`GEU]) assert(y_d[0] == ($unsigned(a) >= $unsigned(b))); |
| if(i_alu[`GE]) assert(y_d[0] == ($signed(a) >= $signed(b))); |
| end |
| |
| `endif |
| endmodule |
|
|