Split up into pim-os, pim-vm and pim-isa crate
This commit is contained in:
179
pim-vm/src/lib.rs
Normal file
179
pim-vm/src/lib.rs
Normal file
@@ -0,0 +1,179 @@
|
||||
use half::f16;
|
||||
use pim_isa::{BankMode, File, Instruction, Kernel, PimConfig};
|
||||
use std::io::Cursor;
|
||||
|
||||
#[cxx::bridge(namespace = "pim_vm")]
|
||||
mod ffi {
|
||||
pub enum BankMode {
|
||||
SingleBank,
|
||||
AllBank,
|
||||
PimAllBank,
|
||||
}
|
||||
|
||||
extern "Rust" {
|
||||
type PimVM;
|
||||
|
||||
fn new_pim_vm(num_pim_units: u32) -> Box<PimVM>;
|
||||
fn reset(&mut self);
|
||||
fn apply_config(&mut self, config: &str);
|
||||
fn bank_mode(&self) -> BankMode;
|
||||
fn execute_read(&mut self, bank_index: u32, data: &[u8]);
|
||||
fn execute_write(&mut self, bank_index: u32) -> [u8; 32];
|
||||
|
||||
fn init_logger();
|
||||
}
|
||||
}
|
||||
|
||||
fn init_logger() {
|
||||
env_logger::init();
|
||||
}
|
||||
|
||||
const GRF_NUM_REGISTERS: usize = 16;
|
||||
const SRF_A_NUM_REGISTERS: usize = 8;
|
||||
const SRF_M_NUM_REGISTERS: usize = 8;
|
||||
|
||||
const REG_NUM_ENTRIES: usize = 16;
|
||||
type Register = [f16; REG_NUM_ENTRIES];
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct PimUnit {
|
||||
grf: [Register; GRF_NUM_REGISTERS],
|
||||
srf_a: [Register; SRF_A_NUM_REGISTERS],
|
||||
srf_m: [Register; SRF_A_NUM_REGISTERS],
|
||||
pc: u8,
|
||||
}
|
||||
|
||||
impl Default for PimUnit {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
grf: [[f16::PI; REG_NUM_ENTRIES]; GRF_NUM_REGISTERS],
|
||||
srf_a: [[f16::ZERO; REG_NUM_ENTRIES]; SRF_A_NUM_REGISTERS],
|
||||
srf_m: [[f16::ZERO; REG_NUM_ENTRIES]; SRF_M_NUM_REGISTERS],
|
||||
pc: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct PimVM {
|
||||
pim_units: Vec<PimUnit>,
|
||||
pim_config: pim_isa::PimConfig,
|
||||
}
|
||||
|
||||
impl PimVM {
|
||||
fn reset(&mut self) {
|
||||
for unit in self.pim_units.iter_mut() {
|
||||
unit.pc = 0;
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_config(&mut self, config_str: &str) {
|
||||
log::debug!("Config string:\n{config_str}");
|
||||
|
||||
self.pim_config = serde_json::from_str::<pim_isa::PimConfig>(config_str).unwrap();
|
||||
self.reset();
|
||||
|
||||
log::debug!("Apply pim config:\n{:?}", self.pim_config);
|
||||
}
|
||||
|
||||
fn bank_mode(&self) -> ffi::BankMode {
|
||||
match self.pim_config.bank_mode {
|
||||
BankMode::SingleBank => ffi::BankMode::SingleBank,
|
||||
BankMode::AllBank => ffi::BankMode::AllBank,
|
||||
BankMode::PimAllBank => ffi::BankMode::PimAllBank,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn new_pim_vm(num_pim_units: u32) -> Box<PimVM> {
|
||||
Box::new(PimVM {
|
||||
pim_units: vec![PimUnit::default(); num_pim_units as _],
|
||||
pim_config: PimConfig {
|
||||
bank_mode: BankMode::SingleBank,
|
||||
kernel: Kernel::NOP,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct BankData([f16; REG_NUM_ENTRIES]);
|
||||
|
||||
impl PimVM {
|
||||
pub fn execute_read(&mut self, bank_index: u32, bank_data: &[u8]) {
|
||||
assert_eq!(bank_data.len(), 32);
|
||||
|
||||
let pim_unit = &mut self.pim_units[bank_index as usize];
|
||||
|
||||
let current_pc = pim_unit.pc;
|
||||
pim_unit.pc += 1;
|
||||
|
||||
let inst = &self.pim_config.kernel.0[current_pc as usize];
|
||||
|
||||
log::debug!("PimUnit {bank_index} Execute PC {current_pc}: {inst:?}");
|
||||
|
||||
match inst {
|
||||
Instruction::NOP => (),
|
||||
Instruction::EXIT => (),
|
||||
Instruction::JUMP { offset, count } => todo!(),
|
||||
Instruction::MOV { src, dst } => {
|
||||
let data: [f16; REG_NUM_ENTRIES] = match src {
|
||||
File::Grf { index } => pim_unit.grf[*index as usize],
|
||||
File::Bank => unsafe {
|
||||
std::ptr::read(bank_data.as_ptr() as *const BankData).0
|
||||
},
|
||||
_ => panic!("Unsupported src operand: {src:?}"),
|
||||
};
|
||||
|
||||
match dst {
|
||||
File::Grf { index } => pim_unit.grf[*index as usize] = data,
|
||||
File::SrfM { index } => pim_unit.srf_m[*index as usize] = data,
|
||||
File::SrfA { index } => pim_unit.srf_a[*index as usize] = data,
|
||||
_ => panic!("Unsupported dst operand: {dst:?}"),
|
||||
}
|
||||
}
|
||||
Instruction::FILL { src, dst } => {
|
||||
let data: [f16; REG_NUM_ENTRIES] = match src {
|
||||
File::Grf { index } => pim_unit.grf[*index as usize],
|
||||
File::Bank => unsafe {
|
||||
std::ptr::read(bank_data.as_ptr() as *const BankData).0
|
||||
},
|
||||
_ => panic!("Unsupported src operand: {src:?}"),
|
||||
};
|
||||
|
||||
match dst {
|
||||
File::Grf { index } => pim_unit.grf[*index as usize] = data,
|
||||
_ => panic!("Unsupported dst operand: {dst:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute_write(&mut self, bank_index: u32) -> [u8; 32] {
|
||||
let pim_unit = &mut self.pim_units[bank_index as usize];
|
||||
|
||||
let current_pc = pim_unit.pc;
|
||||
pim_unit.pc += 1;
|
||||
|
||||
let inst = &self.pim_config.kernel.0[current_pc as usize];
|
||||
|
||||
log::debug!("PimUnit {bank_index} Execute PC {current_pc}: {inst:?}");
|
||||
|
||||
let data = match inst {
|
||||
Instruction::FILL { src, dst } => {
|
||||
let data: [f16; REG_NUM_ENTRIES] = match src {
|
||||
File::Grf { index } => pim_unit.grf[*index as usize],
|
||||
_ => panic!("Unsupported src operand: {src:?}"),
|
||||
};
|
||||
|
||||
if *dst != File::Bank {
|
||||
panic!("Unsupported dst operand: {dst:?}")
|
||||
}
|
||||
|
||||
data
|
||||
}
|
||||
_ => panic!("Unsupported instruction for write: {inst:?}"),
|
||||
};
|
||||
|
||||
unsafe { std::mem::transmute(data) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user