Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zk(Zbk, Zkn, Zks)/Zb: Scalar Cryptography/Bitmanip Extension #2950

Merged
merged 104 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 89 commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
ee289d4
add the Zbk extension for Bitmanip instructions supporting cryptography
phthinh Oct 25, 2021
5932acb
Zbk: implement rotate
ZenithalHourlyRate Mar 15, 2022
5f4fcbb
Zbk: implement logic
ZenithalHourlyRate Mar 15, 2022
da9d6b8
Zbk: implement pack
ZenithalHourlyRate Mar 15, 2022
428f808
Zbk: implement rev
ZenithalHourlyRate Mar 15, 2022
58dc1b5
Zbk: implement zip
ZenithalHourlyRate Mar 15, 2022
a786f23
Zbk: implement xperm
ZenithalHourlyRate Mar 15, 2022
2eefbe0
Zbk: xperm cheaper impl
ZenithalHourlyRate Mar 15, 2022
386e5e9
Zbk: add zbkc decode
phthinh Nov 1, 2021
c5e0bd1
Zbk: implement clmul
ZenithalHourlyRate Mar 15, 2022
af97f3a
Zbk: implement out mux
ZenithalHourlyRate Mar 15, 2022
0b46e1d
Zbk: clmul: fix off by one
ZenithalHourlyRate Mar 15, 2022
380bb42
zbk: fix compile
ZenithalHourlyRate Mar 15, 2022
a3ba274
Zbk: update instruction decode for Zbk
phthinh Nov 2, 2021
009f0ed
bk: fix config
ZenithalHourlyRate Mar 16, 2022
35f02e1
Zbk: a small correction
phthinh Nov 3, 2021
c327863
decode: add the support for the Zkn extension
phthinh Nov 8, 2021
f431ffd
Use asBytes/asNibbles helper
ZenithalHourlyRate Mar 16, 2022
7ae146a
zkn: implement aes enc/dec
ZenithalHourlyRate Mar 16, 2022
e2c8185
zkn: implement aes ks
ZenithalHourlyRate Mar 16, 2022
2faae3f
zkn: aes ks1 reuse sbox in enc
ZenithalHourlyRate Mar 16, 2022
265d4ee
zkn: implement sha
ZenithalHourlyRate Mar 16, 2022
36350c9
ZK: add the support for the Zks extension.
phthinh Nov 10, 2021
0ce3f74
zks: implement sm3/4
ZenithalHourlyRate Mar 16, 2022
d26e561
zkn, zks: implement out mux
ZenithalHourlyRate Mar 16, 2022
76db33a
zk: fix compile
ZenithalHourlyRate Mar 18, 2022
d4acc70
zbk: fix packw/xperm
ZenithalHourlyRate Mar 19, 2022
7631b8a
zkn: fix aes64ks1m due to not merged datapath
ZenithalHourlyRate Mar 19, 2022
8240daf
zks: sm4: fix UInt length after left shift
ZenithalHourlyRate Mar 19, 2022
38728ef
zkn, zks: aes32, sm4 fix leftRotate to rightRotate
ZenithalHourlyRate Mar 19, 2022
65bc35c
zkn: xperm: fix input
ZenithalHourlyRate Mar 19, 2022
d25c4b9
zkn: fix sha512 for rv32
ZenithalHourlyRate Mar 19, 2022
dce44b8
Zb: move andn/orn/xnor into ALU
ZenithalHourlyRate Apr 1, 2022
0a8d59f
Zb: move rotate into ALU
ZenithalHourlyRate Apr 1, 2022
784b0e6
ALU: stop using when
ZenithalHourlyRate Apr 1, 2022
ab51f3f
Zb: add max/min
ZenithalHourlyRate Apr 1, 2022
5fcd554
Zb: add clz/cpop/ctz
ZenithalHourlyRate Apr 1, 2022
3c29480
Zb: add sext/bext
ZenithalHourlyRate Apr 1, 2022
960cf3e
Zb: add rev8/orc
ZenithalHourlyRate Apr 1, 2022
9494980
Zb: add Zba
ZenithalHourlyRate Apr 2, 2022
cd52e3e
Zb: add Zbs
ZenithalHourlyRate Apr 2, 2022
da39720
Zb: add clmulr
ZenithalHourlyRate Apr 2, 2022
74f17e7
Zbb: clz/ctz now do not need PopCount
ZenithalHourlyRate Apr 3, 2022
de9efc4
ALU: refactor
ZenithalHourlyRate Apr 4, 2022
181c1ad
Rename refactored ALU into ABLU
ZenithalHourlyRate Apr 9, 2022
978118b
ABLU: ctz: Fix when all 0
ZenithalHourlyRate Apr 16, 2022
fd7b7be
ABLU: implment ALU side pla
ZenithalHourlyRate Apr 16, 2022
81db153
ABLU: fix compile
ZenithalHourlyRate Apr 16, 2022
906cb6c
Instructions: sync from riscv-opcodes
ZenithalHourlyRate Apr 2, 2022
06688eb
CSR: rename dscratch to dscratch0
ZenithalHourlyRate Apr 2, 2022
f39d059
Instructions: Separate rocket custom instructions
ZenithalHourlyRate Apr 2, 2022
c770dd9
Instructions: Separate NMI insts and CSRs
ZenithalHourlyRate Apr 2, 2022
374de8f
Instructions: use chisel3
ZenithalHourlyRate Apr 2, 2022
8bab78e
ABLU: fill the decode table for Zb
ZenithalHourlyRate Apr 17, 2022
c0f2260
IDecode: add Zb/Zk
ZenithalHourlyRate Apr 18, 2022
cc504cf
IDecode: fix for rv32
ZenithalHourlyRate Apr 18, 2022
773a119
add zba64 decode
ZenithalHourlyRate Apr 18, 2022
ad3edff
ABLU: fix ctrl sig for maxu/min
ZenithalHourlyRate Apr 18, 2022
18927be
ABLU: fix ctwz/clzw zba64 and zbs
ZenithalHourlyRate Apr 18, 2022
8240ffd
zk: sbox use ny93 instead of ROM
ZenithalHourlyRate Apr 23, 2022
fcc711f
IDecode: cleanup zkn/zks
ZenithalHourlyRate Apr 23, 2022
8541647
Add Zb/Zk related configs; cleanup code according to config
ZenithalHourlyRate Apr 23, 2022
37f089a
Merge remote-tracking branch 'origin/master' into Zk-chisel
ZenithalHourlyRate Apr 25, 2022
4f81143
Add aes64ks1i exception
ZenithalHourlyRate May 6, 2022
c4c641a
Merge branch 'master' into Zk-chisel
ZenithalHourlyRate May 6, 2022
1916f3a
IDecode: Fix Zb due to Instructions32 change
ZenithalHourlyRate May 6, 2022
1a76f5d
Fix error from scalafix
ZenithalHourlyRate May 6, 2022
f2cab56
Move pla into decode
ZenithalHourlyRate May 6, 2022
84574f8
fix ALU.FN when using ABLU
cyyself May 15, 2022
076d041
Merge pull request #2 from cyyself/Zk-chisel
ZenithalHourlyRate May 16, 2022
aa52e72
Fix license
ZenithalHourlyRate May 16, 2022
d4726bc
Move BarrelShifter into util
ZenithalHourlyRate May 16, 2022
b980b3c
Rename ALUClass to AbstractAlu
ZenithalHourlyRate May 16, 2022
03aa2cd
Move usingABLU to HasRocketCoreParams
ZenithalHourlyRate May 17, 2022
b145184
Merge remote-tracking branch 'origin/master' into Zk-chisel
ZenithalHourlyRate Jun 13, 2022
c8aaa14
IDecode: use DW_XPR for branch insts
ZenithalHourlyRate Jun 15, 2022
9e52f46
CryptoNIST: refactor ctrl signals
ZenithalHourlyRate Jun 13, 2022
8a5127f
CryptoSM: refactor ctrl signals
ZenithalHourlyRate Jun 15, 2022
f34d61f
Move pla into decode
ZenithalHourlyRate Jun 15, 2022
4740f16
BitManipCrypto: use Mux1H for output
ZenithalHourlyRate Jun 15, 2022
196c372
BitManipCrypto: only instantiate clmul when not usingBitManipCrypto
ZenithalHourlyRate Jun 15, 2022
d481fb6
RocketCore: use Mux1H for mem_reg_wdata to reduce latency
ZenithalHourlyRate Jun 15, 2022
2dfd5aa
ABLU: cleanup
ZenithalHourlyRate Jun 15, 2022
95dd0ac
Merge branch 'master' into Zk-chisel
ZenithalHourlyRate Jan 5, 2023
46217ee
Merge branch 'master' into Zk-chisel-new
ZenithalHourlyRate Jan 7, 2023
7a8abd8
Fix for chisel3 style and new PRs
ZenithalHourlyRate Jan 5, 2023
f4d74d7
Fix decoding for zbs imm
ZenithalHourlyRate Jan 6, 2023
058ea26
Merge branch 'master' into Zk-chisel-new
ZenithalHourlyRate Jan 10, 2023
ef03f08
Add CI for Zb/Zk arch-test
ZenithalHourlyRate Jan 6, 2023
9dff941
Remove useABLU
ZenithalHourlyRate Jan 14, 2023
df3fbd5
Fix Bitmanip name
ZenithalHourlyRate Jan 14, 2023
a0ae1e9
Add Bitmanip/Crypto related isa string in DTS
ZenithalHourlyRate Jan 14, 2023
58ff23f
Instantiate BitManipCrypto earlier
ZenithalHourlyRate Jan 14, 2023
9229556
Merge branch 'master' into Zk-chisel
ZenithalHourlyRate Jan 14, 2023
beb3a11
Add comment on B misa bit
ZenithalHourlyRate Jan 15, 2023
590fbb5
Refactor ALUFN
ZenithalHourlyRate Jan 15, 2023
879f6c3
Refactor other ctrl signals
ZenithalHourlyRate Jan 15, 2023
e5a5edc
Pass alufn to Multipler
ZenithalHourlyRate Jan 15, 2023
c04a3a6
More default ALUFN
ZenithalHourlyRate Jan 16, 2023
be84dc0
Refactors to ease migration burden
jerryz123 Jan 17, 2023
57b1259
Merge pull request #4 from chipsalliance/Zk-chisel-fixes
ZenithalHourlyRate Jan 17, 2023
bccdd5f
Fix format
ZenithalHourlyRate Jan 17, 2023
6abee57
Fix int ctrl sigs fn width
ZenithalHourlyRate Jan 17, 2023
871e13c
Add isa string in bulk
ZenithalHourlyRate Jan 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/mill-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
runs-on: [self-hosted, linux]
strategy:
matrix:
config: ["DefaultRV32Config,32,RV32IMAC", "DefaultConfig,64,RV64IMAC"]
config: ["DefaultRV32Config,32,RV32IMAC", "DefaultConfig,64,RV64IMAC", "BitmanipCryptoConfig,64,RV64IZba_Zbb_Zbc_Zbkb_Zbkc_Zbkx_Zbs_Zknd_Zkne_Zknh_Zksed_Zksh", "BitmanipCrypto32Config,32,RV32IZba_Zbb_Zbc_Zbkb_Zbkc_Zbkx_Zbs_Zknd_Zkne_Zknh_Zksed_Zksh"]
steps:
- uses: actions/checkout@v2
with:
Expand Down
5 changes: 5 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ object emulator extends mill.Cross[Emulator](
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.DefaultRV32Config"),
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.TinyConfig"),
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.DefaultFP16Config"),
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.BitmanipCryptoConfig"),
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.BitmanipCrypto32Config"),
// Misc
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.DefaultSmallConfig"),
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.DualBankConfig"),
Expand Down Expand Up @@ -338,6 +340,9 @@ object `runnable-arch-test` extends mill.Cross[ArchTest](
// zicsr is disabled due to ebreak issue
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.DefaultConfig", "64", "RV64IMAC"),
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.DefaultRV32Config", "32", "RV32IMAC"),

("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.BitmanipCryptoConfig", "64", "RV64IZba_Zbb_Zbc_Zbkb_Zbkc_Zbkx_Zbs_Zknd_Zkne_Zknh_Zksed_Zksh"),
("freechips.rocketchip.system.TestHarness", "freechips.rocketchip.system.BitmanipCrypto32Config", "32", "RV32IZba_Zbb_Zbc_Zbkb_Zbkc_Zbkx_Zbs_Zknd_Zkne_Zknh_Zksed_Zksh"),
)
class ArchTest(top: String, config: String, xlen: String, isa: String) extends Module {
def ispecString = T {
Expand Down
26 changes: 25 additions & 1 deletion scripts/arch-test/emulator/riscof_emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ def build(self, isa_yaml, platform_yaml):
self.isa += 'd'
if "C" in ispec["ISA"]:
self.isa += 'c'
if "Zba" in ispec["ISA"]:
self.isa += '_Zba'
if "Zbb" in ispec["ISA"]:
self.isa += '_Zbb'
if "Zbc" in ispec["ISA"]:
self.isa += '_Zbc'
if "Zbkb" in ispec["ISA"]:
self.isa += '_Zbkb'
if "Zbkc" in ispec["ISA"]:
self.isa += '_Zbkc'
if "Zbkx" in ispec["ISA"]:
self.isa += '_Zbkx'
if "Zbs" in ispec["ISA"]:
self.isa += '_Zbs'
if "Zknd" in ispec["ISA"]:
self.isa += '_Zknd'
if "Zkne" in ispec["ISA"]:
self.isa += '_Zkne'
if "Zknh" in ispec["ISA"]:
self.isa += '_Zknh'
if "Zksed" in ispec["ISA"]:
self.isa += '_Zksed'
if "Zksh" in ispec["ISA"]:
self.isa += '_Zksh'

#TODO: The following assumes you are using the riscv-gcc toolchain. If
# not please change appropriately
Expand Down Expand Up @@ -153,7 +177,7 @@ def runTests(self, testList):

# substitute all variables in the compile command that we created in the initialize
# function
cmd = self.compile_cmd.format(testentry['isa'].lower(), test, elf, compile_macros)
cmd = self.compile_cmd.format(self.isa.lower(), test, elf, compile_macros)

# if the user wants to disable running the tests and only compile the tests, then
# the "else" clause is executed below assigning the sim command to simple no action
Expand Down
26 changes: 25 additions & 1 deletion scripts/arch-test/spike/riscof_spike.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ def build(self, isa_yaml, platform_yaml):
self.isa += 'd'
if "C" in ispec["ISA"]:
self.isa += 'c'
if "Zba" in ispec["ISA"]:
self.isa += '_Zba'
if "Zbb" in ispec["ISA"]:
self.isa += '_Zbb'
if "Zbc" in ispec["ISA"]:
self.isa += '_Zbc'
if "Zbkb" in ispec["ISA"]:
self.isa += '_Zbkb'
if "Zbkc" in ispec["ISA"]:
self.isa += '_Zbkc'
if "Zbkx" in ispec["ISA"]:
self.isa += '_Zbkx'
if "Zbs" in ispec["ISA"]:
self.isa += '_Zbs'
if "Zknd" in ispec["ISA"]:
self.isa += '_Zknd'
if "Zkne" in ispec["ISA"]:
self.isa += '_Zkne'
if "Zknh" in ispec["ISA"]:
self.isa += '_Zknh'
if "Zksed" in ispec["ISA"]:
self.isa += '_Zksed'
if "Zksh" in ispec["ISA"]:
self.isa += '_Zksh'

#TODO: The following assumes you are using the riscv-gcc toolchain. If
# not please change appropriately
Expand Down Expand Up @@ -153,7 +177,7 @@ def runTests(self, testList):

# substitute all variables in the compile command that we created in the initialize
# function
cmd = self.compile_cmd.format(testentry['isa'].lower(), test, elf, compile_macros)
cmd = self.compile_cmd.format(self.isa.lower(), test, elf, compile_macros)

# if the user wants to disable running the tests and only compile the tests, then
# the "else" clause is executed below assigning the sim command to simple no action
Expand Down
278 changes: 278 additions & 0 deletions src/main/scala/rocket/ABLU.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
// SPDX-License-Identifier: Apache-2.0

package freechips.rocketchip.rocket

import chisel3._
import chisel3.util._
import freechips.rocketchip.config.Parameters
import freechips.rocketchip.tile.CoreModule

object ABLU extends ALUFN
{
override val SZ_ALU_FN = 39
override def FN_X = BitPat("b??_???_????_????_????__????__??_????_????_????_????")
override def FN_ADD = "b00_000_0000_0000_0000__0001__00_0000_0000_0000_0001".U(SZ_ALU_FN.W)
override def FN_SL = "b00_000_0000_1100_0000__0001__00_0000_0000_0000_0010".U(SZ_ALU_FN.W)
override def FN_SEQ = "b00_100_0000_0000_0000__0001__00_0000_0000_0100_0000".U(SZ_ALU_FN.W)
override def FN_SNE = "b00_110_0000_0000_0000__0001__00_0000_0000_0100_0000".U(SZ_ALU_FN.W)
override def FN_XOR = "b00_000_0000_0000_0000__0001__00_0000_0000_0000_1000".U(SZ_ALU_FN.W)
override def FN_SR = "b00_000_0000_0000_0000__0001__00_0000_0000_0000_0010".U(SZ_ALU_FN.W)
override def FN_OR = "b00_000_0000_0000_0000__0001__00_0000_0000_0001_0000".U(SZ_ALU_FN.W)
override def FN_AND = "b00_000_0000_0000_0000__0001__00_0000_0000_0000_0100".U(SZ_ALU_FN.W)
override def FN_SUB = "b00_000_0000_0000_0011__0001__00_0000_0000_0000_0001".U(SZ_ALU_FN.W)
override def FN_SRA = "b00_000_0000_0001_0000__0001__00_0000_0000_0000_0010".U(SZ_ALU_FN.W)
override def FN_SLT = "b00_000_0000_0000_0011__0001__00_0000_0000_0100_0000".U(SZ_ALU_FN.W)
override def FN_SGE = "b00_010_0000_0000_0011__0001__00_0000_0000_0100_0000".U(SZ_ALU_FN.W)
override def FN_SLTU = "b00_001_0000_0000_0011__0001__00_0000_0000_0100_0000".U(SZ_ALU_FN.W)
override def FN_SGEU = "b00_011_0000_0000_0011__0001__00_0000_0000_0100_0000".U(SZ_ALU_FN.W)

// from Zb
// Zba: UW is encoded here becuase it is DW_64
override def FN_ADDUW = "b00_000_0000_0000_1000__0001__00_0000_0000_0000_0001".U(SZ_ALU_FN.W)
override def FN_SLLIUW = "b00_000_0000_1100_1000__0001__00_0000_0000_0000_0010".U(SZ_ALU_FN.W)
override def FN_SH1ADD = "b00_000_0000_0000_0000__0010__00_0000_0000_0000_0001".U(SZ_ALU_FN.W)
override def FN_SH1ADDUW = "b00_000_0000_0000_1000__0010__00_0000_0000_0000_0001".U(SZ_ALU_FN.W)
override def FN_SH2ADD = "b00_000_0000_0000_0000__0100__00_0000_0000_0000_0001".U(SZ_ALU_FN.W)
override def FN_SH2ADDUW = "b00_000_0000_0000_1000__0100__00_0000_0000_0000_0001".U(SZ_ALU_FN.W)
override def FN_SH3ADD = "b00_000_0000_0000_0000__1000__00_0000_0000_0000_0001".U(SZ_ALU_FN.W)
override def FN_SH3ADDUW = "b00_000_0000_0000_1000__1000__00_0000_0000_0000_0001".U(SZ_ALU_FN.W)
// Zbb
override def FN_ROR = "b00_000_0000_0010_0000__0001__00_0000_0000_0000_0010".U(SZ_ALU_FN.W)
override def FN_ROL = "b00_000_0000_1110_0000__0001__00_0000_0000_0000_0010".U(SZ_ALU_FN.W)
override def FN_ANDN = "b00_000_0000_0000_0010__0001__00_0000_0000_0000_0100".U(SZ_ALU_FN.W)
override def FN_ORN = "b00_000_0000_0000_0010__0001__00_0000_0000_0001_0000".U(SZ_ALU_FN.W)
override def FN_XNOR = "b00_000_0000_0000_0010__0001__00_0000_0000_0000_1000".U(SZ_ALU_FN.W)
override def FN_REV8 = "b00_000_0000_0000_0000__0001__00_0001_0000_0000_0000".U(SZ_ALU_FN.W)
override def FN_ORCB = "b10_000_0000_0000_0000__0001__00_0010_0000_0000_0000".U(SZ_ALU_FN.W)
override def FN_SEXTB = "b00_000_0000_0000_0000__0001__00_0000_1000_0000_0000".U(SZ_ALU_FN.W)
override def FN_SEXTH = "b01_000_0000_0000_0000__0001__00_0000_0100_0000_0000".U(SZ_ALU_FN.W)
override def FN_ZEXTH = "b00_000_0000_0000_0000__0001__00_0000_0100_0000_0000".U(SZ_ALU_FN.W)
override def FN_MAX = "b00_000_0000_0000_0011__0001__00_0000_0000_1000_0000".U(SZ_ALU_FN.W)
override def FN_MAXU = "b00_001_0000_0000_0011__0001__00_0000_0000_1000_0000".U(SZ_ALU_FN.W)
override def FN_MIN = "b00_010_0000_0000_0011__0001__00_0000_0000_1000_0000".U(SZ_ALU_FN.W)
override def FN_MINU = "b00_011_0000_0000_0011__0001__00_0000_0000_1000_0000".U(SZ_ALU_FN.W)
override def FN_CPOP = "b00_000_0000_0000_0000__0001__00_0000_0001_0000_0000".U(SZ_ALU_FN.W)
override def FN_CLZ = "b00_000_1101_1110_0000__0001__00_0000_0010_0000_0000".U(SZ_ALU_FN.W)
override def FN_CTZ = "b00_000_1101_0000_0000__0001__00_0000_0010_0000_0000".U(SZ_ALU_FN.W)
// Zbs
override def FN_BCLR = "b00_000_1110_1000_0100__0001__00_0000_0000_0000_0100".U(SZ_ALU_FN.W)
override def FN_BEXT = "b00_000_1000_1000_0100__0001__00_0000_0000_0010_0000".U(SZ_ALU_FN.W)
override def FN_BINV = "b00_000_1000_1000_0100__0001__00_0000_0000_0000_1000".U(SZ_ALU_FN.W)
override def FN_BSET = "b00_000_1000_1000_0100__0001__00_0000_0000_0001_0000".U(SZ_ALU_FN.W)
// Zbk
override def FN_BREV8 = "b00_000_0000_0000_0000__0001__00_0010_0000_0000_0000".U(SZ_ALU_FN.W)
override def FN_PACK = "b00_000_0000_0000_0000__0001__00_0100_0000_0000_0000".U(SZ_ALU_FN.W)
override def FN_PACKH = "b00_000_0000_0000_0000__0001__00_1000_0000_0000_0000".U(SZ_ALU_FN.W)
override def FN_ZIP = "b00_000_0000_0000_0000__0001__01_0000_0000_0000_0000".U(SZ_ALU_FN.W)
override def FN_UNZIP = "b00_000_0000_0000_0000__0001__10_0000_0000_0000_0000".U(SZ_ALU_FN.W)

override def FN_DIV = FN_XOR
override def FN_DIVU = FN_SR
override def FN_REM = FN_OR
override def FN_REMU = FN_AND

override def FN_MUL = FN_ADD
override def FN_MULH = FN_SL
override def FN_MULHSU = FN_SEQ
override def FN_MULHU = FN_SNE
}

class ABLU(implicit p: Parameters) extends CoreModule()(p) with HasALUIO {
// note that it is reversed
val isSub :: isIn2Inv :: isZBS :: isUW :: Nil = io.fn(25,22).asBools
ZenithalHourlyRate marked this conversation as resolved.
Show resolved Hide resolved
val isSRA :: isRotate :: isLeft :: isLeftZBS :: Nil = io.fn(29,26).asBools
val isCZ :: isBCLR :: isCZBCLR :: isCZZBS :: Nil = io.fn(33,30).asBools
val isUnsigned :: isInverted :: isSEQSNE :: Nil = io.fn(36,34).asBools
val isSEXT :: isORC :: Nil = io.fn(38,37).asBools
val shxadd1H = io.fn(21,18) // 4 bit
val out1H = io.fn(17,0)

// process input
// used by SUB, ANDN, ORN, XNOR
val in2_inv = Mux(isIn2Inv, ~io.in2, io.in2)
val shamt =
if (xLen == 32) io.in2(4,0)
else {
require(xLen == 64)
Cat(io.in2(5) & (io.dw === DW_64), io.in2(4,0))
}
val in1_ext =
if (xLen == 32) io.in1
else {
require(xLen == 64)
val in1_hi_orig = io.in1(63,32)
// note that CLZW uses rotate
val in1_hi_rotate = io.in1(31,0)
// note that sext fills 0 for ADDW/SUBW, but it works
val in1_hi_sext = Fill(32, isSRA & io.in1(31)) // 31 to 63 then to 64 in shout_r
val in1_hi_zext = Fill(32, 0.U)
val in1_hi = Mux(io.dw === DW_64,
Mux(isUW, in1_hi_zext, in1_hi_orig),
Mux(isRotate, in1_hi_rotate, in1_hi_sext))
Cat(in1_hi, io.in1(31,0))
}
// one arm: SL, ROL, SLLIUW, CLZ
// another arm: SR, SRA, ROR, CTZ, ADD, SUB, ZBS
// note that CLZW is not included here
// in1 capable of right hand operation
// isLeft
val in1_r = Mux(isLeft, Reverse(in1_ext), in1_ext)

// shifter
val shin = Mux(isZBS,
if (xLen == 32) (BigInt(1) << 31).U(32.W)
else {
require(xLen == 64)
(BigInt(1) << 63).U(64.W)
}, in1_r)
// TODO: Merge shift and rotate (manual barrel or upstream to Chisel)
val shout_r = (Cat(isSRA & shin(xLen-1), shin).asSInt >> shamt)(xLen-1,0)
val roout_r = shin.rotateRight(shamt)(xLen-1,0)
val shro_r = Mux(isRotate, roout_r, shout_r)
// one arm: SL, ROL, SLLIUW, ZBS
// another arm: SR, SRA, ROR
val shro = Mux(isLeftZBS, Reverse(shro_r), shro_r)

// adder
val adder_in1 =
Mux1H(shxadd1H, Seq(
if (xLen == 32) in1_r
else {
require(xLen == 64)
// for CLZW/CTZW
Mux(io.dw === DW_64, in1_r, Cat(Fill(32, 1.U(1.W)), in1_r(31,0)))
},
(in1_ext << 1)(xLen-1,0),
(in1_ext << 2)(xLen-1,0),
(in1_ext << 3)(xLen-1,0)))
// out = in1 - 1 when isCLZ/isCTZ
// note that when isCZ, isSub is 0 as ~0 = ~1+1 = -1
val adder_in2 = Mux(isCZ,
~0.U(xLen.W), in2_inv)
// adder_out = adder_in1 + adder_in2 + isSub
val adder_out = (Cat(adder_in1, 1.U(1.W)) + Cat(adder_in2, isSub))(xLen,1)
io.adder_out := adder_out

// logic
// AND, OR, XOR
// ANDN, ORN, XNOR
// BCLR, BEXT, BINV, BSET
val out_inv = Mux(isCZBCLR, ~Mux(isBCLR, shro, adder_out), shro)
val logic_in2 = Mux(isCZZBS, out_inv, in2_inv)
// also BINV
val xor = adder_in1 ^ logic_in2
// also BCLR
val and = adder_in1 & logic_in2
// also BSET
val or = adder_in1 | logic_in2
val bext = and.orR

// SLT, SLTU
// BEQ, BNE, BLT, BGE
// MAX, MIN
val slt =
Mux(io.in1(xLen-1) === io.in2(xLen-1), adder_out(xLen-1),
Mux(isUnsigned, io.in2(xLen-1), io.in1(xLen-1)))
val cmp = isInverted ^ Mux(isSEQSNE, ~(xor.orR), slt)
io.cmp_out := cmp
// MAX, MAXU, MIN, MINU
val max_min = Mux(cmp, io.in2, io.in1)

// counter
// CLZ, CPOP, CTZ
val cpop = PopCount(
if (xLen == 32) io.in1
else {
require(xLen == 64)
Mux(io.dw === DW_64, io.in1, Cat(Fill(32, 0.U(1.W)), io.in1(31,0)))
})
// ctz_in = ~adder_out & adder_in1 // all zero or one hot
val ctz_in = and
val ctz_out = Cat(~ctz_in.orR, VecInit((0 to log2Ceil(xLen)-1).map(
x => {
val bits = ctz_in.asBools.zipWithIndex
VecInit(
bits
filter { case (_, i) => i % (1 << (x + 1)) >= (1 << x) }
map { case (b, _) => b }
).asUInt.orR
}
).toSeq).asUInt)

// ZEXT/SEXT
val exth = Cat(Fill(xLen-16, Mux(isSEXT, io.in1(15), 0.U)), io.in1(15,0))
val extb = Cat(Fill(xLen-8, io.in1(7)), io.in1(7,0))

// REV/ORC
def asBytes(in: UInt): Vec[UInt] = VecInit(in.asBools.grouped(8).map(VecInit(_).asUInt).toSeq)
val in1_bytes = asBytes(io.in1)
val rev8 = VecInit(in1_bytes.reverse.toSeq).asUInt
val orc_brev8 = VecInit(in1_bytes.map(x => {
val orc = Mux(x.orR, 0xFF.U(8.W), 0.U(8.W))
// BREV8 only in Zbk
if (usingBitManipCrypto)
Mux(isORC, orc, Reverse(x))
else orc
}).toSeq).asUInt

// pack
def sext(in: UInt): UInt = {
val in_hi_32 = Fill(32, in(31))
Cat(in_hi_32, in)
}
val pack = if (usingBitManipCrypto) {
if (xLen == 32) Cat(io.in2(xLen/2-1,0), io.in1(xLen/2-1,0))
else {
require(xLen == 64)
Mux(io.dw === DW_64,
Cat(io.in2(xLen/2-1,0), io.in1(xLen/2-1,0)),
sext(Cat(io.in2(xLen/4-1,0), io.in1(xLen/4-1,0))))
}
} else 0.U
val packh = if (usingBitManipCrypto) Cat(0.U((xLen-16).W), io.in2(7,0), io.in1(7,0)) else 0.U

// zip
val zip = if (xLen == 32 && usingBitManipCrypto) {
val lo = io.in1(15,0).asBools
val hi = io.in1(31,16).asBools
VecInit(lo.zip(hi).map { case (l, h) => VecInit(Seq(l, h)).asUInt }).asUInt
} else 0.U
val unzip = if (xLen == 32 && usingBitManipCrypto) {
val bits = io.in1.asBools.zipWithIndex
val lo = VecInit(bits filter { case (_, i) => i % 2 == 0 } map { case (b, _) => b }).asUInt
val hi = VecInit(bits filter { case (_, i) => i % 2 != 0 } map { case (b, _) => b }).asUInt
Cat(hi, lo)
} else 0.U

val out = Mux1H(out1H, Seq(
adder_out,
shro,
and,
xor,
//
or,
bext,
cmp,
max_min,
//
cpop,
ctz_out,
exth,
extb,
//
rev8,
orc_brev8,
pack,
packh,
//
zip,
unzip))
Comment on lines +334 to +356
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be a high fanout out...
But I don't have so much idea here to provide better circuit performance...


val out_w =
if (xLen == 32) out
else {
require(xLen == 64)
Mux(io.dw === DW_64, out, Cat(Fill(32, out(31)), out(31,0)))
}
io.out := out_w
}
Loading