cpu/crates/cpu/src/instruction.rs

187 lines
5.5 KiB
Rust
Raw Normal View History

2024-10-14 16:28:13 -07:00
// SPDX-License-Identifier: LGPL-3.0-or-later
// See Notices.txt for copyright information
2024-10-14 21:19:45 -07:00
use crate::unit::UnitMOp;
2024-10-10 22:57:11 -07:00
use fayalite::prelude::*;
use std::marker::PhantomData;
2024-10-08 20:22:15 -07:00
pub mod power_isa;
#[hdl]
2024-10-10 22:57:11 -07:00
pub enum OutputIntegerMode {
Full64,
DupLow32,
ZeroExt32,
SignExt32,
ZeroExt16,
SignExt16,
ZeroExt8,
SignExt8,
}
pub const MOP_IMM_WIDTH: usize = 34;
pub const MOP_MIN_REG_WIDTH: usize = 8;
pub const COMMON_MOP_OTHER_WIDTH: usize =
MOP_IMM_WIDTH - (COMMON_MOP_SRC_REG_COUNT - COMMON_MOP_IMM_SRC_REG_COUNT) * MOP_MIN_REG_WIDTH;
pub const COMMON_MOP_SRC_REG_COUNT: usize = 3;
pub const COMMON_MOP_IMM_SRC_REG_COUNT: usize = 2;
#[hdl]
pub struct CommonMOp<PrefixPad: KnownSize, RegWidth: Size, SrcCount: KnownSize> {
pub prefix_pad: UIntType<PrefixPad>,
pub dest: UIntType<RegWidth>,
pub src: Array<UIntType<RegWidth>, { COMMON_MOP_SRC_REG_COUNT }>,
pub other: UInt<{ COMMON_MOP_OTHER_WIDTH }>,
pub _phantom: PhantomData<SrcCount>,
}
impl<PrefixPad: KnownSize, RegWidth: Size, SrcCount: KnownSize>
CommonMOp<PrefixPad, RegWidth, SrcCount>
{
pub fn imm_ty(self) -> SInt {
assert!(self.src.element().width() >= MOP_MIN_REG_WIDTH);
assert!(SrcCount::VALUE <= COMMON_MOP_SRC_REG_COUNT);
SInt[(COMMON_MOP_SRC_REG_COUNT - SrcCount::VALUE) * MOP_MIN_REG_WIDTH
+ COMMON_MOP_OTHER_WIDTH]
}
#[hdl]
pub fn imm(expr: impl ToExpr<Type = Self>) -> Expr<SInt> {
let expr = expr.to_expr();
assert!(Expr::ty(expr).src.element().width() >= MOP_MIN_REG_WIDTH);
let mut acc = expr.other[..COMMON_MOP_OTHER_WIDTH - 1];
for i in SrcCount::VALUE..COMMON_MOP_SRC_REG_COUNT {
acc = (acc, expr.src[i][..MOP_MIN_REG_WIDTH]).cast_to_bits();
}
acc = (acc, expr.other[COMMON_MOP_OTHER_WIDTH - 1]).cast_to_bits();
#[hdl]
let imm = wire(Expr::ty(expr).imm_ty());
debug_assert_eq!(Expr::ty(acc).width(), Expr::ty(imm).width());
connect(imm, acc.cast_to(Expr::ty(imm)));
imm
}
#[hdl]
pub fn connect_to_imm(expr: impl ToExpr<Type = Self>, imm: impl ToExpr<Type = SInt>) {
let expr = expr.to_expr();
let imm = imm.to_expr();
let imm_ty = Expr::ty(expr).imm_ty();
assert_eq!(Expr::ty(imm), imm_ty);
let mut pos = COMMON_MOP_OTHER_WIDTH - 1;
connect_any(
expr.other,
(imm[..pos], imm[imm_ty.width() - 1]).cast_to_bits(),
);
for i in SrcCount::VALUE..COMMON_MOP_SRC_REG_COUNT {
connect_any(expr.src[i], imm[pos..pos + MOP_MIN_REG_WIDTH]);
pos += MOP_MIN_REG_WIDTH;
}
}
}
#[hdl]
pub struct AluCommonMOp<RegWidth: Size, SrcCount: KnownSize> {
pub common: CommonMOp<ConstUsize<0>, RegWidth, SrcCount>,
pub output_integer_mode: OutputIntegerMode,
}
#[hdl]
pub struct AddSubMOp<RegWidth: Size, SrcCount: KnownSize> {
pub alu_common: AluCommonMOp<RegWidth, SrcCount>,
pub invert_src0: Bool,
pub invert_carry_in: Bool,
pub invert_carry_out: Bool,
pub add_pc: Bool,
}
#[hdl]
pub struct LogicalMOp<RegWidth: Size> {
pub alu_common: AluCommonMOp<RegWidth, ConstUsize<2>>,
pub lut: UInt<4>,
2024-10-08 20:22:15 -07:00
}
#[hdl]
2024-10-10 22:57:11 -07:00
pub struct BranchMOp<RegWidth: Size> {
pub alu_common: AluCommonMOp<RegWidth, ConstUsize<2>>,
pub lut: UInt<4>,
2024-10-08 20:22:15 -07:00
}
#[hdl]
2024-10-10 22:57:11 -07:00
pub enum AluBranchMOp<RegWidth: Size> {
AddSub(AddSubMOp<RegWidth, ConstUsize<3>>),
AddSubI(AddSubMOp<RegWidth, ConstUsize<2>>),
Logical(LogicalMOp<RegWidth>),
}
#[hdl]
pub struct ReadL2RegMOp<RegWidth: Size> {
pub common: CommonMOp<ConstUsize<1>, RegWidth, ConstUsize<0>>,
}
#[hdl]
pub struct WriteL2RegMOp<RegWidth: Size> {
pub common: CommonMOp<ConstUsize<1>, RegWidth, ConstUsize<1>>,
}
#[hdl]
pub enum L2RegisterFileMOp<RegWidth: Size> {
ReadL2Reg(ReadL2RegMOp<RegWidth>),
WriteL2Reg(WriteL2RegMOp<RegWidth>),
}
#[hdl]
pub struct LoadStoreCommonMOp<RegWidth: Size, SrcCount: KnownSize> {
pub common: CommonMOp<ConstUsize<1>, RegWidth, SrcCount>,
}
#[hdl]
pub struct LoadMOp<RegWidth: Size> {
pub load_store_common: LoadStoreCommonMOp<RegWidth, ConstUsize<1>>,
}
#[hdl]
pub struct StoreMOp<RegWidth: Size> {
pub load_store_common: LoadStoreCommonMOp<RegWidth, ConstUsize<2>>,
}
#[hdl]
pub enum LoadStoreMOp<RegWidth: Size> {
Load(CommonMOp<ConstUsize<1>, RegWidth, ConstUsize<0>>),
Store(CommonMOp<ConstUsize<1>, RegWidth, ConstUsize<1>>),
2024-10-08 20:22:15 -07:00
}
#[hdl]
2024-10-14 21:19:45 -07:00
/// there may be more than one unit of a given kind, so UnitNum is not the same as UnitKind.
/// zero is used for built-in constants, such as the zero register
2024-10-08 20:22:15 -07:00
pub struct UnitNum<Width: Size> {
pub value: UIntType<Width>,
}
2024-10-14 21:19:45 -07:00
pub const CONST_ZERO_UNIT_NUM: usize = 0;
2024-10-08 20:22:15 -07:00
#[hdl]
pub struct UnitOutRegNum<Width: Size> {
pub value: UIntType<Width>,
}
#[hdl]
/// Physical Register Number -- registers in the CPU's backend
pub struct PRegNum<UnitNumWidth: Size, OutRegNumWidth: Size> {
pub unit_num: UnitNum<UnitNumWidth>,
pub unit_out_reg: UnitOutRegNum<OutRegNumWidth>,
}
#[hdl]
2024-10-13 01:37:36 -07:00
/// µOp Register Number -- register in a micro-operation before register renaming
2024-10-08 20:22:15 -07:00
#[doc(alias = "UOpRegNum")] // help you find it in the docs if you mis-spell it
#[doc(alias = "\u{B5}OpRegNum")] // micro sign
#[doc(alias = "\u{39C}OpRegNum")] // greek capital letter mu
#[doc(alias = "\u{3BC}OpRegNum")] // greek small letter mu
pub struct MOpRegNum {
2024-10-13 01:37:36 -07:00
pub value: UInt<{ MOpRegNum::WIDTH }>,
2024-10-08 20:22:15 -07:00
}
2024-10-13 01:37:36 -07:00
impl MOpRegNum {
pub const WIDTH: usize = 8;
pub const CONST_ZERO_REG_NUM: u32 = 0;
}
pub type MOp = UnitMOp<ConstUsize<{ MOpRegNum::WIDTH }>>;