use crate::{opcode::Opcode, registers::Registers, state::VMState}; pub struct BCNVM { pub regs: Registers, pub state: VMState, pub pc: usize, pub program: Vec, } impl BCNVM { pub fn new(program: Vec) -> Self { Self { regs: Registers::new(), state: VMState::new(), pc: 0, program, } } pub fn run(&mut self) -> Result<(), String> { while self.pc < self.program.len() { if self.pc + 8 > self.program.len() { return Err("Instruction out of bounds".into()); } let instr: [u8; 8] = self.program[self.pc..self.pc + 8] .try_into() .map_err(|_| "Invalid instruction")?; let opcode = Opcode::from_byte(instr[0]) .ok_or("Invalid opcode")?; match opcode { Opcode::INVCHK => self.invchk()?, Opcode::HALT => break, Opcode::NOOP => {} _ => {} } self.pc += 8; } Ok(()) } fn invchk(&self) -> Result<(), String> { if self.state.trust < 0 { return Err("Trust underflow".into()); } Ok(()) } }