cpu/crates/cpu/src/config.rs

45 lines
1.3 KiB
Rust

// SPDX-License-Identifier: LGPL-3.0-or-later
// See Notices.txt for copyright information
use crate::{
instruction::{PRegNum, UnitNum, CONST_ZERO_UNIT_NUM},
unit::UnitKind,
};
use fayalite::prelude::*;
use std::num::NonZeroUsize;
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub struct CpuConfig {
pub unit_kinds: Vec<UnitKind>,
pub out_reg_num_width: usize,
pub fetch_width: NonZeroUsize,
}
impl CpuConfig {
pub const DEFAULT_OUT_REG_NUM_WIDTH: usize = 4;
pub const DEFAULT_FETCH_WIDTH: NonZeroUsize = {
let Some(v) = NonZeroUsize::new(1) else {
unreachable!();
};
v
};
pub fn new(unit_kinds: Vec<UnitKind>) -> Self {
Self {
unit_kinds,
out_reg_num_width: Self::DEFAULT_OUT_REG_NUM_WIDTH,
fetch_width: Self::DEFAULT_FETCH_WIDTH,
}
}
pub fn non_const_unit_nums(&self) -> std::ops::Range<usize> {
(CONST_ZERO_UNIT_NUM + 1)..(self.unit_kinds.len() + 1)
}
pub fn unit_num_width(&self) -> usize {
UInt::range(self.non_const_unit_nums()).width()
}
pub fn unit_num(&self) -> UnitNum<DynSize> {
UnitNum[self.unit_num_width()]
}
pub fn p_reg_num(&self) -> PRegNum<DynSize, DynSize> {
PRegNum[self.unit_num_width()][self.out_reg_num_width]
}
}