cpu/crates/cpu/src/instruction.rs
Jacob Lifshay 12481cfab3
All checks were successful
/ deps (push) Successful in 14s
/ test (push) Successful in 25m15s
start debugging reg_alloc with simulator
2024-12-20 00:28:22 -08:00

267 lines
8.6 KiB
Rust

// SPDX-License-Identifier: LGPL-3.0-or-later
// See Notices.txt for copyright information
use crate::unit::UnitMOp;
use fayalite::{expr::ops::ArrayLiteral, intern::Interned, prelude::*};
use std::marker::PhantomData;
pub mod power_isa;
#[hdl]
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_SRC_LEN: usize = 3;
pub const COMMON_MOP_MIN_SRC_LEN_WITH_FULL_IMM: usize = 2;
pub const COMMON_MOP_IMM_LOW_WIDTH: usize = CommonMOpWithMaxSrcCount::IMM_WIDTH - 1;
#[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_LEN }>,
pub imm_low: UInt<{ COMMON_MOP_IMM_LOW_WIDTH }>,
pub imm_sign: SInt<1>,
pub _phantom: PhantomData<SrcCount>,
}
#[hdl]
pub struct CommonMOpImmParts<ImmInSrcCount: Size> {
// fields must be in this exact order
pub imm_low: UInt<{ COMMON_MOP_IMM_LOW_WIDTH }>,
pub reversed_src: ArrayType<UInt<{ MOP_MIN_REG_WIDTH }>, ImmInSrcCount>,
pub imm_sign: SInt<1>,
}
type CommonMOpWithMaxSrcCount = CommonMOpForImm<{ COMMON_MOP_SRC_LEN }>;
type CommonMOpForImm<const SRC_COUNT: usize> =
CommonMOp<ConstUsize<0>, ConstUsize<{ MOP_MIN_REG_WIDTH }>, ConstUsize<SRC_COUNT>>;
pub const COMMON_MOP_0_IMM_WIDTH: usize = CommonMOpForImm::<0>::IMM_WIDTH;
pub const COMMON_MOP_1_IMM_WIDTH: usize = CommonMOpForImm::<1>::IMM_WIDTH;
pub const COMMON_MOP_2_IMM_WIDTH: usize = CommonMOpForImm::<2>::IMM_WIDTH;
pub const COMMON_MOP_3_IMM_WIDTH: usize = CommonMOpForImm::<3>::IMM_WIDTH;
const COMMON_MOP_0_IMM_IN_SRC_COUNT: usize = CommonMOpForImm::<0>::IMM_IN_SRC_COUNT;
impl<PrefixPad: KnownSize, RegWidth: Size, SrcCount: KnownSize>
CommonMOp<PrefixPad, RegWidth, SrcCount>
{
pub const IMM_IN_SRC_COUNT: usize = {
assert!(SrcCount::VALUE <= COMMON_MOP_SRC_LEN, "too many sources");
const _: () = assert!(COMMON_MOP_MIN_SRC_LEN_WITH_FULL_IMM <= COMMON_MOP_SRC_LEN);
(COMMON_MOP_SRC_LEN - COMMON_MOP_MIN_SRC_LEN_WITH_FULL_IMM)
- SrcCount::VALUE.saturating_sub(COMMON_MOP_MIN_SRC_LEN_WITH_FULL_IMM)
};
pub const IMM_IN_SRC_RANGE: std::ops::Range<usize> =
(COMMON_MOP_SRC_LEN - Self::IMM_IN_SRC_COUNT)..COMMON_MOP_SRC_LEN;
pub const IMM_WIDTH: usize = {
MOP_IMM_WIDTH - (COMMON_MOP_0_IMM_IN_SRC_COUNT - Self::IMM_IN_SRC_COUNT) * MOP_MIN_REG_WIDTH
};
pub fn imm_ty() -> SInt {
SInt::new(Self::IMM_WIDTH)
}
pub fn imm_parts_ty() -> CommonMOpImmParts<DynSize> {
let retval = CommonMOpImmParts[Self::IMM_IN_SRC_COUNT];
assert_eq!(
retval.canonical().bit_width(),
Self::IMM_WIDTH,
"{retval:#?}"
);
retval
}
#[hdl]
pub fn new(
prefix_pad: impl ToExpr<Type = UIntType<PrefixPad>>,
dest: impl ToExpr<Type = UIntType<RegWidth>>,
src: impl ToExpr<Type = ArrayType<UIntType<RegWidth>, SrcCount>>,
imm: impl ToExpr<Type = SInt>,
) -> Expr<Self> {
let prefix_pad = prefix_pad.to_expr();
let dest = dest.to_expr();
let src_in = src.to_expr();
let imm = imm.to_expr();
assert_eq!(Expr::ty(imm), Self::imm_ty());
let reg_ty = Expr::ty(dest);
assert_eq!(reg_ty, Expr::ty(src_in).element());
let imm_parts = imm.cast_to_bits().cast_bits_to(Self::imm_parts_ty());
let mut src = [0_hdl_u0.cast_to(reg_ty); COMMON_MOP_SRC_LEN];
for i in 0..SrcCount::VALUE {
src[i] = src_in[i];
}
for (reversed_src_index, src_index) in Self::IMM_IN_SRC_RANGE.rev().enumerate() {
src[src_index] = imm_parts.reversed_src[reversed_src_index].cast_to(reg_ty);
}
#[hdl]
Self {
prefix_pad,
dest,
src: ArrayLiteral::new(
reg_ty,
Interned::from_iter(src.iter().map(|v| Expr::canonical(*v))),
)
.to_expr(),
imm_low: Expr::from_dyn_int(imm[..COMMON_MOP_IMM_LOW_WIDTH]),
imm_sign: Expr::from_dyn_int(imm >> (Self::IMM_WIDTH - 1)),
_phantom: PhantomData,
}
}
#[hdl]
pub fn imm(expr: impl ToExpr<Type = Self>) -> Expr<SInt> {
let expr = expr.to_expr();
let reversed_src = Vec::from_iter(
Self::IMM_IN_SRC_RANGE
.rev()
.map(|src_index| expr.src[src_index].cast_to_static()),
);
let imm_parts = {
#[hdl]
CommonMOpImmParts {
imm_low: expr.imm_low,
reversed_src,
imm_sign: expr.imm_sign,
}
};
imm_parts.cast_to_bits().cast_bits_to(Self::imm_ty())
}
#[hdl]
pub fn connect_to_imm(expr: impl ToExpr<Type = Self>, imm: impl ToExpr<Type = SInt>) {
let expr = expr.to_expr();
let reg_ty = Expr::ty(expr).dest;
assert_eq!(reg_ty, Expr::ty(expr).src.element());
let imm = imm.to_expr();
assert_eq!(Expr::ty(imm), Self::imm_ty());
let imm_parts = imm.cast_to_bits().cast_bits_to(Self::imm_parts_ty());
let mut src = [Some(0_hdl_u0.cast_to(reg_ty)); COMMON_MOP_SRC_LEN];
for i in 0..SrcCount::VALUE {
src[i] = None;
}
for (reversed_src_index, src_index) in Self::IMM_IN_SRC_RANGE.rev().enumerate() {
src[src_index] = Some(imm_parts.reversed_src[reversed_src_index].cast_to(reg_ty));
}
for i in 0..COMMON_MOP_SRC_LEN {
if let Some(v) = src[i] {
connect(expr.src[i], v);
}
}
}
}
#[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>,
}
#[hdl]
pub struct BranchMOp<RegWidth: Size> {
pub alu_common: AluCommonMOp<RegWidth, ConstUsize<2>>,
pub lut: UInt<4>,
}
#[hdl]
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>>),
}
#[hdl]
/// 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
pub struct UnitNum<Width: Size> {
pub value: UIntType<Width>,
}
pub const CONST_ZERO_UNIT_NUM: usize = 0;
#[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]
/// µOp Register Number -- register in a micro-operation before register renaming
#[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 {
pub value: UInt<{ MOpRegNum::WIDTH }>,
}
impl MOpRegNum {
pub const WIDTH: usize = 8;
pub const CONST_ZERO_REG_NUM: u32 = 0;
}
#[hdl]
pub type MOp = UnitMOp<ConstUsize<{ MOpRegNum::WIDTH }>>;