2023-02-17 15:50:59 +00:00
///////////////////////////////////////////
2023-03-05 06:44:03 +00:00
// bmuctrl.sv
2023-02-17 15:50:59 +00:00
//
2023-02-17 15:52:54 +00:00
// Written: Kevin Kim <kekim@hmc.edu>
// Created: 16 February 2023
2023-03-06 13:41:53 +00:00
// Modified: 6 March 2023
2023-02-17 15:50:59 +00:00
//
2023-03-06 13:41:53 +00:00
// Purpose: Top level bit manipulation instruction decoder
2023-02-17 15:50:59 +00:00
//
// Documentation: RISC-V System on Chip Design Chapter 4 (Section 4.1.4, Figure 4.8, Table 4.5)
//
// A component of the CORE-V-WALLY configurable RISC-V project.
//
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
//
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
//
// Licensed under the Solderpad Hardware License v 2.1 (the “License”); you may not use this file
// except in compliance with the License, or, at your option, the Apache License version 2.0. You
// may obtain a copy of the License at
//
// https://solderpad.org/licenses/SHL-2.1/
//
// Unless required by applicable law or agreed to in writing, any work distributed under the
// License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
////////////////////////////////////////////////////////////////////////////////////////////////
`include " wally-config.vh "
// NOTE: DO we want to make this XLEN parameterized?
module bmuctrl (
input logic clk , reset ,
// Decode stage control signals
input logic StallD , FlushD , // Stall, flush Decode stage
input logic [ 31 : 0 ] InstrD , // Instruction in Decode stage
2023-03-03 16:41:47 +00:00
output logic [ 2 : 0 ] ALUSelectD , // ALU Mux select signal in Decode Stage
2023-03-05 07:19:31 +00:00
output logic [ 1 : 0 ] BSelectD , // Indicates if ZBA_ZBB_ZBC_ZBS instruction in one-hot encoding in Decode stage
2023-02-19 03:44:14 +00:00
output logic [ 2 : 0 ] ZBBSelectD , // ZBB mux select signal in Decode stage NOTE: do we need this in decode?
2023-03-03 16:41:47 +00:00
output logic BRegWriteD , // Indicates if it is a R type B instruction in Decode Stage
output logic BW64D , // Indiciates if it is a W type B instruction in Decode Stage
output logic BALUOpD , // Indicates if it is an ALU B instruction in Decode Stage
output logic BSubArithD , // TRUE if ext, clr, andn, orn, xnor instruction in Decode Stage
output logic IllegalBitmanipInstrD , // Indicates if it is unrecognized B instruction in Decode Stage
2023-02-17 15:50:59 +00:00
// Execute stage control signals
input logic StallE , FlushE , // Stall, flush Execute stage
2023-02-17 16:21:55 +00:00
output logic [ 2 : 0 ] ALUSelectE ,
2023-03-05 07:19:31 +00:00
output logic [ 1 : 0 ] BSelectE , // Indicates if ZBA_ZBB_ZBC_ZBS instruction in one-hot encoding
2023-03-02 20:15:22 +00:00
output logic [ 2 : 0 ] ZBBSelectE , // ZBB mux select signal
2023-03-04 01:12:29 +00:00
output logic BRegWriteE , // Indicates if it is a R type B instruction in Execute
2023-03-04 06:57:49 +00:00
output logic BComparatorSignedE , // Indicates if comparator signed in Execute Stage
2023-03-05 06:44:03 +00:00
output logic [ 2 : 0 ] BALUControlE // ALU Control signals for B instructions in Execute Stage
2023-02-17 15:50:59 +00:00
) ;
logic [ 6 : 0 ] OpD ; // Opcode in Decode stage
logic [ 2 : 0 ] Funct3D ; // Funct3 field in Decode stage
logic [ 6 : 0 ] Funct7D ; // Funct7 field in Decode stage
2023-02-19 03:44:14 +00:00
logic [ 4 : 0 ] Rs2D ; // Rs2 source register in Decode stage
2023-03-04 01:12:29 +00:00
logic BComparatorSignedD ; // Indicates if comparator signed (max, min instruction) in Decode Stage
2023-03-04 06:57:49 +00:00
logic RotateD ; // Indicates if rotate instruction in Decode Stage
2023-03-05 06:44:03 +00:00
logic MaskD ; // Indicates if zbs instruction in Decode Stage
logic PreShiftD ; // Indicates if sh1add, sh2add, sh3add instruction in Decode Stage
logic [ 2 : 0 ] BALUControlD ; // ALU Control signals for B instructions
2023-03-05 07:19:31 +00:00
`define BMUCTRLW 16
2023-02-17 15:50:59 +00:00
logic [ `BMUCTRLW - 1 : 0 ] BMUControlsD ; // Main B Instructions Decoder control signals
// Extract fields
assign OpD = InstrD [ 6 : 0 ] ;
assign Funct3D = InstrD [ 14 : 12 ] ;
assign Funct7D = InstrD [ 31 : 25 ] ;
2023-02-19 03:44:14 +00:00
assign Rs2D = InstrD [ 24 : 20 ] ;
2023-02-17 15:50:59 +00:00
// Main Instruction Decoder
always_comb
casez ( { OpD , Funct7D , Funct3D } )
2023-03-05 06:44:03 +00:00
// ALUSelect_BSelect_ZBBSelect_BRegWrite_BW64_BALUOp_BSubArithD_RotateD_MaskD_PreShiftD_IllegalBitmanipInstrD
2023-02-19 03:57:10 +00:00
// ZBS
2023-03-05 07:19:31 +00:00
17 'b0010011 _0100100_001: BMUControlsD = `BMUCTRLW 'b111 _01_000_1_0_1_1_0_1_0_0 ; // bclri
2023-02-17 20:54:08 +00:00
17 'b0010011 _0100101_001: if ( `XLEN = = 64 )
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b111 _01_000_1_0_1_1_0_1_0_0 ; // bclri (rv64)
2023-02-17 20:54:08 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
17 'b0010011 _0100100_101: BMUControlsD = `BMUCTRLW 'b101 _01_000_1_0_1_1_0_1_0_0 ; // bexti
2023-02-17 20:54:08 +00:00
17 'b0010011 _0100101_101: if ( `XLEN = = 64 )
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b101 _01_000_1_0_1_1_0_1_0_0 ; // bexti (rv64)
2023-02-17 20:54:08 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
17 'b0010011 _0110100_001: BMUControlsD = `BMUCTRLW 'b100 _01_000_1_0_1_0_0_1_0_0 ; // binvi
2023-02-17 20:54:08 +00:00
17 'b0010011 _0110101_001: if ( `XLEN = = 64 )
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b100 _01_000_1_0_1_0_0_1_0_0 ; // binvi (rv64)
2023-02-17 20:54:08 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
17 'b0010011 _0010100_001: BMUControlsD = `BMUCTRLW 'b110 _01_000_1_0_1_0_0_1_0_0 ; // bseti
2023-02-17 20:54:08 +00:00
17 'b0010011 _0010101_001: if ( `XLEN = = 64 )
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b110 _01_000_1_0_1_0_0_1_0_0 ; // bseti (rv64)
2023-02-17 20:54:08 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
17 'b0110011 _0100100_001: BMUControlsD = `BMUCTRLW 'b111 _01_000_1_0_1_1_0_1_0_0 ; // bclr
17 'b0110011 _0100100_101: BMUControlsD = `BMUCTRLW 'b101 _01_000_1_0_1_1_0_1_0_0 ; // bext
17 'b0110011 _0110100_001: BMUControlsD = `BMUCTRLW 'b100 _01_000_1_0_1_0_0_1_0_0 ; // binv
17 'b0110011 _0010100_001: BMUControlsD = `BMUCTRLW 'b110 _01_000_1_0_1_0_0_1_0_0 ; // bset
17 'b0 ? 1 ? 011 _0 ? 0000 ? _ ? 01 : BMUControlsD = `BMUCTRLW 'b001 _00_000_1_0_1_0_0_0_0_0 ; // sra, srai, srl, srli, sll, slli
2023-02-19 03:57:10 +00:00
// ZBC
2023-03-05 07:19:31 +00:00
17 'b0110011 _0000101_0 ? ? : BMUControlsD = `BMUCTRLW 'b000 _11_000_1_0_1_0_0_0_0_0 ; // ZBC instruction
2023-02-19 03:57:10 +00:00
// ZBA
2023-03-05 07:19:31 +00:00
17 'b0110011 _0010000_010: BMUControlsD = `BMUCTRLW 'b000 _01_000_1_0_1_0_0_0_1_0 ; // sh1add
17 'b0110011 _0010000_100: BMUControlsD = `BMUCTRLW 'b000 _01_000_1_0_1_0_0_0_1_0 ; // sh2add
17 'b0110011 _0010000_110: BMUControlsD = `BMUCTRLW 'b000 _01_000_1_0_1_0_0_0_1_0 ; // sh3add
17 'b0111011 _0010000_010: BMUControlsD = `BMUCTRLW 'b000 _01_000_1_1_1_0_0_0_1_0 ; // sh1add.uw
17 'b0111011 _0010000_100: BMUControlsD = `BMUCTRLW 'b000 _01_000_1_1_1_0_0_0_1_0 ; // sh2add.uw
17 'b0111011 _0010000_110: BMUControlsD = `BMUCTRLW 'b000 _01_000_1_1_1_0_0_0_1_0 ; // sh3add.uw
17 'b0111011 _0000100_000: BMUControlsD = `BMUCTRLW 'b000 _01_000_1_1_1_0_0_0_0_0 ; // add.uw
17 'b0011011 _000010 ? _001: BMUControlsD = `BMUCTRLW 'b001 _01_000_1_1_1_0_0_0_0_0 ; // slli.uw
2023-02-19 03:50:36 +00:00
// ZBB
2023-03-05 22:57:30 +00:00
17 'b0110011 _0110000_001: BMUControlsD = `BMUCTRLW 'b001 _01_111_1_0_1_0_1_0_0_0 ; // rol
17 'b0111011 _0110000_001: BMUControlsD = `BMUCTRLW 'b001 _00_111_1_1_1_0_1_0_0_0 ; // rolw
17 'b0110011 _0110000_101: BMUControlsD = `BMUCTRLW 'b001 _01_111_1_0_1_0_1_0_0_0 ; // ror
17 'b0111011 _0110000_101: BMUControlsD = `BMUCTRLW 'b001 _00_111_1_1_1_0_1_0_0_0 ; // rorw
17 'b0010011 _0110000_101: BMUControlsD = `BMUCTRLW 'b001 _00_111_1_0_1_0_1_0_0_0 ; // rori (rv32)
2023-02-18 05:57:19 +00:00
17 'b0010011 _0110001_101: if ( `XLEN = = 64 )
2023-03-05 22:57:30 +00:00
BMUControlsD = `BMUCTRLW 'b001 _00_111_1_0_1_0_1_0_0_0 ; // rori (rv64)
2023-02-18 05:57:19 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
2023-02-19 00:26:16 +00:00
17 'b0011011 _0110000_101: if ( `XLEN = = 64 )
2023-03-05 22:57:30 +00:00
BMUControlsD = `BMUCTRLW 'b001 _00_111_1_1_1_0_1_0_0_0 ; // roriw
2023-02-19 00:26:16 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
2023-02-19 03:44:14 +00:00
17 'b0010011 _0110000_001: if ( Rs2D [ 2 ] )
2023-03-05 22:57:30 +00:00
BMUControlsD = `BMUCTRLW 'b000 _10_001_1_0_1_0_0_0_0_0 ; // sign extend instruction
2023-02-19 03:44:14 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _10_000_1_0_1_0_0_0_0_0 ; // count instruction
17 'b0011011 _0110000_001: BMUControlsD = `BMUCTRLW 'b000 _10_000_1_1_1_0_0_0_0_0 ; // count word instruction
2023-02-19 04:32:40 +00:00
17 'b0111011 _0000100_100: if ( `XLEN = = 64 )
2023-03-05 22:57:30 +00:00
BMUControlsD = `BMUCTRLW 'b000 _10_001_1_0_1_0_0_0_0_0 ; // zexth (rv64)
2023-02-19 04:32:40 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
2023-02-19 04:32:40 +00:00
17 'b0110011 _0000100_100: if ( `XLEN = = 32 )
2023-03-05 22:57:30 +00:00
BMUControlsD = `BMUCTRLW 'b000 _10_001_1_0_1_0_0_0_0_0 ; // zexth (rv32)
2023-02-19 04:32:40 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
2023-03-05 22:57:30 +00:00
17 'b0110011 _0100000_111: BMUControlsD = `BMUCTRLW 'b111 _01_111_1_0_1_1_0_0_0_0 ; // andn
17 'b0110011 _0100000_110: BMUControlsD = `BMUCTRLW 'b110 _01_111_1_0_1_1_0_0_0_0 ; // orn
17 'b0110011 _0100000_100: BMUControlsD = `BMUCTRLW 'b100 _01_111_1_0_1_1_0_0_0_0 ; // xnor
2023-02-19 05:06:55 +00:00
17 'b0010011 _0110101_101: if ( `XLEN = = 64 )
2023-03-05 22:57:30 +00:00
BMUControlsD = `BMUCTRLW 'b000 _10_010_1_0_1_0_0_0_0_0 ; // rev8 (rv64)
2023-02-19 05:06:55 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
2023-02-19 05:06:55 +00:00
17 'b0010011 _0110100_101: if ( `XLEN = = 32 )
2023-03-05 22:57:30 +00:00
BMUControlsD = `BMUCTRLW 'b000 _10_010_1_0_1_0_0_0_0_0 ; // rev8 (rv32)
2023-02-19 05:06:55 +00:00
else
2023-03-05 07:19:31 +00:00
BMUControlsD = `BMUCTRLW 'b000 _00_000_0_0_0_0_0_0_0_1 ; // illegal instruction
2023-03-05 22:57:30 +00:00
17 'b0010011 _0010100_101: BMUControlsD = `BMUCTRLW 'b000 _10_010_1_0_1_0_0_0_0_0 ; // orc.b
17 'b0110011 _0000101_110: BMUControlsD = `BMUCTRLW 'b000 _10_100_1_0_1_0_0_0_0_0 ; // max
17 'b0110011 _0000101_111: BMUControlsD = `BMUCTRLW 'b000 _10_100_1_0_1_0_0_0_0_0 ; // maxu
17 'b0110011 _0000101_100: BMUControlsD = `BMUCTRLW 'b000 _10_011_1_0_1_0_0_0_0_0 ; // min
17 'b0110011 _0000101_101: BMUControlsD = `BMUCTRLW 'b000 _10_011_1_0_1_0_0_0_0_0 ; // minu
2023-03-05 07:19:31 +00:00
default : BMUControlsD = { Funct3D , { 12 'b0 } , { 1 'b1 } } ; // not B instruction or shift
2023-02-17 15:50:59 +00:00
endcase
// Unpack Control Signals
2023-03-05 06:44:03 +00:00
assign { ALUSelectD , BSelectD , ZBBSelectD , BRegWriteD , BW64D , BALUOpD , BSubArithD , RotateD , MaskD , PreShiftD , IllegalBitmanipInstrD } = BMUControlsD ;
// Pack BALUControl Signals
assign BALUControlD = { RotateD , MaskD , PreShiftD } ;
2023-02-17 15:50:59 +00:00
2023-03-04 01:12:29 +00:00
// Comparator should perform signed comparison when min/max instruction. We have overlap in funct3 with some branch instructions so we use opcode to differentiate betwen min/max and branches
assign BComparatorSignedD = ( Funct3D [ 2 ] ^ Funct3D [ 0 ] ) & ~ OpD [ 6 ] ;
2023-02-17 15:50:59 +00:00
// BMU Execute stage pipieline control register
2023-03-05 07:19:31 +00:00
flopenrc # ( 13 ) controlregBMU ( clk , reset , FlushE , ~ StallE , { ALUSelectD , BSelectD , ZBBSelectD , BRegWriteD , BComparatorSignedD , BALUControlD } , { ALUSelectE , BSelectE , ZBBSelectE , BRegWriteE , BComparatorSignedE , BALUControlE } ) ;
2023-02-17 15:50:59 +00:00
endmodule