Break out step()

This commit is contained in:
Reid 'arrdem' McKenzie 2022-12-23 00:13:19 -07:00
parent 09bb1854bf
commit b7ade42661

View file

@ -12,6 +12,7 @@ pub enum UxnError {
StackError(StackError), StackError(StackError),
MemoryError(MemoryError), MemoryError(MemoryError),
ExecutionLimit(u16), ExecutionLimit(u16),
Break,
ArithmeticOverflow, ArithmeticOverflow,
ArithmeticUnderflow, ArithmeticUnderflow,
DivisionByZero, DivisionByZero,
@ -111,22 +112,6 @@ impl Uxn {
// Run one clock cycle (instruction) // Run one clock cycle (instruction)
pub fn step(&mut self) -> Result<(), UxnError> { pub fn step(&mut self) -> Result<(), UxnError> {
Ok(())
}
pub fn run(&mut self, limit: u16) -> Result<(), UxnError> {
let mut executed = 0;
'run: loop {
if executed == limit {
return Err(UxnError::ExecutionLimit(executed));
}
if self.is_halted() {
break;
}
executed += 1;
match self.lda1(self.pc) { match self.lda1(self.pc) {
Err(e) => return Err(UxnError::MemoryError(e)), Err(e) => return Err(UxnError::MemoryError(e)),
Ok(opcode) => { Ok(opcode) => {
@ -189,7 +174,7 @@ impl Uxn {
match (kflag, rflag, sflag, icode) { match (kflag, rflag, sflag, icode) {
(0, 0, 0, 0x00) => { (0, 0, 0, 0x00) => {
// BRK // BRK
break 'run; return Err(UxnError::Break);
} }
(_, _, _, 0x00) => { (_, _, _, 0x00) => {
// LIT // LIT
@ -408,6 +393,26 @@ impl Uxn {
} }
} }
} }
Ok(())
}
pub fn run(&mut self, limit: u16) -> Result<(), UxnError> {
let mut executed = 0;
loop {
if executed == limit {
return Err(UxnError::ExecutionLimit(executed));
}
if self.is_halted() {
break;
}
executed += 1;
match self.step() {
Err(e) => return Err(e),
Ok(()) => continue,
};
} }
Ok(()) Ok(())
} }