base core state & instruction decoder

This commit is contained in:
2025-09-27 21:43:10 +02:00
parent 5919041f07
commit 3163b43fa4
4 changed files with 130 additions and 1 deletions

58
src/core.rs Normal file
View File

@@ -0,0 +1,58 @@
use crate::{
consts::{Addr, RegId, RegValue},
mem::MemConfig,
};
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum ExecutionStatus {
Running,
Paused,
Halted,
}
pub struct Core {
x_regs: [RegValue; 32],
pc: Addr,
mem: MemConfig,
exec_status: ExecutionStatus,
}
impl Core {
pub fn new(mem: MemConfig) -> Self {
Self {
x_regs: [0; 32],
pc: 0,
mem,
exec_status: ExecutionStatus::Halted,
}
}
pub fn reset(&mut self, pc: Addr) {
self.pc = pc;
self.exec_status = ExecutionStatus::Running;
}
pub fn resume(&mut self) -> ExecutionStatus {
if self.exec_status == ExecutionStatus::Halted {
ExecutionStatus::Halted
} else {
self.exec_status = ExecutionStatus::Running;
ExecutionStatus::Running
}
}
pub fn exec_status(&self) -> ExecutionStatus {
self.exec_status
}
fn reg_read(&self, id: RegId) -> RegValue {
self.x_regs[id as usize]
}
fn reg_write(&mut self, id: RegId, value: RegValue) {
if id == 0 {
return;
}
self.x_regs[id as usize] = value;
}
}