@@ 1,7 1,7 @@
struct Processor {
stack_a: Vec<u16>,
stack_b: Vec<u16>,
- pub memory: [u16;1024],
+ pub memory: Vec<u16>,
carry: bool,
pub pc: u16 //program counter
}
@@ 19,8 19,11 @@ impl Processor {
let conditional = (instruction & 0x40) != 0;
let relative = (instruction & 0x20) != 0;
let save_pc = (instruction & 0x10) != 0;
+ let long_address = (instruction & 0x8) != 0;
if !conditional || self.carry {
- let address = self.stack_a.pop().expect("End of stack");
+ let address;
+ if long_address {address = self.stack_a.pop().unwrap_or(0) | (self.stack_a.pop().unwrap_or(0) << 10)} //lower value on stack is high half of long address
+ else {address = self.stack_a.pop().unwrap_or(0)};
if save_pc {self.stack_a.push(self.pc+1)};
if relative {self.pc += address}
else {self.pc = address};
@@ 29,41 32,49 @@ impl Processor {
}
else {
let toggle = (instruction & 0x40) != 0;
- let mut stack;
+ let stack;
if toggle {stack = &mut self.stack_b;}
else {stack = &mut self.stack_a;};
match instruction & 0x3F {
0 => { //swap top 2
- let a = stack.pop().expect("End of stack");
- let b = stack.pop().expect("End of stack");
+ let a = stack.pop().unwrap_or(0);
+ let b = stack.pop().unwrap_or(0);
stack.push(a);
stack.push(b);
}
1 => { //clone top
- let a = stack.pop().expect("End of stack");
+ let a = stack.pop().unwrap_or(0);
stack.push(a);
stack.push(a);
}
2 => { //clone second
- let a = stack.pop().expect("End of stack");
- let b = stack.pop().expect("End of stack");
+ let a = stack.pop().unwrap_or(0);
+ let b = stack.pop().unwrap_or(0);
stack.push(b);
stack.push(a);
stack.push(b);
}
3 => { //move between stacks
- if toggle {self.stack_a.push(self.stack_b.pop().expect("End of stack"));}
- else {self.stack_b.push(self.stack_a.pop().expect("End of stack"));};
+ if toggle {self.stack_a.push(self.stack_b.pop().unwrap_or(0));}
+ else {self.stack_b.push(self.stack_a.pop().unwrap_or(0));};
}
4 => { //push/pop
- let addr = self.stack_a.pop().expect("End of stack") as usize;
+ let addr = self.stack_a.pop().unwrap_or(0) as usize;
if toggle {
- let value = self.stack_a.pop().expect("End of stack");
+ let value = self.stack_a.pop().unwrap_or(0);
self.memory[addr] = value;
}
- else {self.stack_a.push(self.memory[addr]);};
+ else {self.stack_a.push(self.memory[addr])};
}
- 5 => { //set/clear carry
+ 5 => { //long addr push/pop
+ let addr = (self.stack_a.pop().unwrap_or(0) | (self.stack_a.pop().unwrap_or(0) << 10)) as usize;
+ if toggle {
+ let value = self.stack_a.pop().unwrap_or(0);
+ self.memory[addr] = value;
+ }
+ else {self.stack_a.push(self.memory[addr])};
+ }
+ 6 => { //set/clear carry
self.carry = toggle;
}
_ => ()
@@ 77,8 88,8 @@ impl Processor {
}
fn calc(stack: &mut Vec::<u16>, instruction: u16, carry_in: bool) -> bool {
- let operand_a = stack.pop().expect("End of stack");
- let operand_b = stack.pop().expect("End of stack");
+ let operand_a = stack.pop().unwrap_or(0);
+ let operand_b = stack.pop().unwrap_or(0);
let mut carry = carry_in;
let mut result = 0;
for i in 0..10 {