diff --git a/src/vm.rs b/src/vm.rs index 8abb406..1a29867 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -31,12 +31,12 @@ impl From for UxnError { #[derive(Debug)] pub struct Uxn { - memory: Rc>, + pub memory: Rc>, // Note: Using Rc so we can start with many NullDevs and replace them - devices: [Rc>; 16], - pc: u16, // Program counter - wst: Stack, // Data stack - rst: Stack, // Return stack pointer + pub devices: [Rc>; 16], + pub pc: u16, // Program counter + pub wst: Stack, // Data stack + pub rst: Stack, // Return stack pointer } impl Uxn { @@ -136,15 +136,13 @@ impl Uxn { let kflag: u8 = opcode & 0x80 >> 7; let icode: u8 = opcode & 0x1F; - let wst = || if rflag == 1 { self.wst } else { self.rst }; - let mut wd = 0u8; // Accumulated pop - let mut widx = || { - assert!(wd <= wst().idx); - wst().idx - wd - }; // Current index - let rst = || if rflag == 1 { self.rst } else { self.wst }; - let mut rd = 0u8; // Accumulated pop - let mut ridx = || rst().idx - rd; // Current index + let [wst, rst] = { + if rflag == 0 { + [&mut self.wst, &mut self.rst] + } else { + [&mut self.wst, &mut self.rst] + } + }; // FIXME: Keep flag is unimplemented match (kflag, rflag, sflag, icode) { @@ -154,140 +152,140 @@ impl Uxn { } (_, _, 0, 0x00) => { // LIT1 - wst().push1(self.lda1(self.pc + 1)?); + wst.push1(self.lda1(self.pc + 1)?); self.pc += 1; continue 'run; } (_, _, 1, 0x00) => { // LIT2 - wst().push2(self.lda2(self.pc + 1)?); + wst.push2(self.lda2(self.pc + 1)?); self.pc += 2; continue 'run; } (_, _, 0, 0x01) => { // INC1 - let a = wst().pop1()?; - wst().push1(a + 1); + let a = wst.pop1()?; + wst.push1(a + 1); } (_, _, 1, 0x01) => { // INC1 - let a = wst().pop2()?; - wst().push2(a + 1); + let a = wst.pop2()?; + wst.push2(a + 1); } (_, _, 0, 0x02) => { // POP1 - wst().pop1(); + wst.pop1(); } (_, _, 1, 0x02) => { // POP2 - wst().pop2(); + wst.pop2(); } (_, _, 0, 0x03) => { // NIP1 a b -- a - let keep = wst().pop1()?; - wst().pop1(); - wst().push1(keep)?; + let keep = wst.pop1()?; + wst.pop1(); + wst.push1(keep)?; } (_, _, 1, 0x03) => { // NIP2 a b -- a - let keep = wst().pop2()?; - wst().pop2()?; - wst().push2(keep)?; + let keep = wst.pop2()?; + wst.pop2()?; + wst.push2(keep)?; } (_, _, 0, 0x04) => { // SWP1 - let a = wst().pop1()?; - let b = wst().pop1()?; - wst().push1(a)?; - wst().push1(b)?; + let a = wst.pop1()?; + let b = wst.pop1()?; + wst.push1(a)?; + wst.push1(b)?; } (_, _, 1, 0x04) => { // SWP2 - let a = wst().pop2()?; - let b = wst().pop2()?; - wst().push2(a)?; - wst().push2(b)?; + let a = wst.pop2()?; + let b = wst.pop2()?; + wst.push2(a)?; + wst.push2(b)?; } (_, _, 0, 0x05) => { // ROT - let a = wst().pop1()?; - let b = wst().pop1()?; - let c = wst().pop1()?; - wst().push1(b)?; - wst().push1(c)?; - wst().push1(a)?; + let a = wst.pop1()?; + let b = wst.pop1()?; + let c = wst.pop1()?; + wst.push1(b)?; + wst.push1(c)?; + wst.push1(a)?; } (_, _, 1, 0x05) => { // ROT - let a = wst().pop2()?; - let b = wst().pop2()?; - let c = wst().pop2()?; - wst().push2(b)?; - wst().push2(c)?; - wst().push2(a)?; + let a = wst.pop2()?; + let b = wst.pop2()?; + let c = wst.pop2()?; + wst.push2(b)?; + wst.push2(c)?; + wst.push2(a)?; } (_, _, 0, 0x06) => { // DUP2 - let a = wst().peek1()?; - wst().push1(a)?; + let a = wst.peek1()?; + wst.push1(a)?; } (_, _, 1, 0x06) => { // DUP2 - let a = wst().peek2()?; - wst().push2(a)?; + let a = wst.peek2()?; + wst.push2(a)?; } (_, _, 0, 0x07) => { // OVR1 a b -- a b a - if wst().idx < 2 { + if wst.idx < 2 { return Err(UxnError::StackError(StackError::StackUnderflow)); } - wst().push1(wst().get1(wst().idx - 2)?)?; + wst.push1(wst.get1(wst.idx - 2)?)?; } (_, _, 1, 0x07) => { // OVR2 a b -- a b a - if wst().idx < 4 { + if wst.idx < 4 { return Err(UxnError::StackError(StackError::StackUnderflow)); } - wst().push2(wst().get2(wst().idx - 4)?)?; + wst.push2(wst.get2(wst.idx - 4)?)?; } (_, _, 0, 0x08) => { // EQU1 - let a = wst().pop1()?; - let b = wst().pop1()?; + let a = wst.pop1()?; + let b = wst.pop1()?; if a == b { - wst().push1(1)?; + wst.push1(1)?; } else { - wst().push1(0)?; + wst.push1(0)?; } } (_, _, 1, 0x08) => { // EQU2 - let a = wst().pop2()?; - let b = wst().pop2()?; + let a = wst.pop2()?; + let b = wst.pop2()?; if a == b { - wst().push1(1)?; + wst.push1(1)?; } else { - wst().push1(0)?; + wst.push1(0)?; } } (_, _, 0, 0x09) => { // NEQ1 - let a = wst().pop1()?; - let b = wst().pop1()?; + let a = wst.pop1()?; + let b = wst.pop1()?; if a == b { - wst().push1(0)?; + wst.push1(0)?; } else { - wst().push1(1)?; + wst.push1(1)?; } } (_, _, 1, 0x09) => { // NEQ2 - let a = wst().pop2()?; - let b = wst().pop2()?; + let a = wst.pop2()?; + let b = wst.pop2()?; if a == b { - wst().push1(0)?; + wst.push1(0)?; } else { - wst().push1(1)?; + wst.push1(1)?; } } (_, _, _, 0x0a) => {