diff --git a/src/machine.rs b/src/machine.rs index 4afbd8a..558f936 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -1,24 +1,28 @@ use std::io::{BufReader, BufWriter, Read, Write}; - use crate::instruction::{Instruction, InstructionType}; +const CELL_SIZE: u8 = 255; +const ADDRESS_SIZE: usize = 30000; + pub struct Machine<'a> { ip: usize, mp: usize, buf: [u8; 1], - memory: [u32; 50000], + memory: [u8; ADDRESS_SIZE], code: Vec, input: &'a mut BufReader>, output: &'a mut BufWriter>, } impl<'a> Machine<'a> { - pub fn new(code: Vec, input: &'a mut BufReader>, output: &'a mut BufWriter>) -> Self { + pub fn new(code: Vec, + input: &'a mut BufReader>, + output: &'a mut BufWriter>) -> Self { Machine { ip: 0, mp: 0, buf: [0; 1], - memory: [0; 50000], + memory: [0; ADDRESS_SIZE], code, input, output, @@ -30,7 +34,7 @@ impl<'a> Machine<'a> { while self.ip < self.code.len() { // get current instruction let instruction = &self.code[self.ip]; - println!("{:?}", instruction); + println!("{:?} ; {}", instruction, self.mp); // match type and execute match instruction.itype { InstructionType::Increment => self.do_increment(instruction.argument), @@ -55,21 +59,41 @@ impl<'a> Machine<'a> { self.ip += 1; } } - + + // safely increment cell value pub fn do_increment(&mut self, argument: usize){ - self.memory[self.mp] += argument as u32 + if((self.memory[self.mp] + argument as u8) >= CELL_SIZE ) { + self.memory[self.mp] = CELL_SIZE - (self.memory[self.mp] + argument as u8); + } else { + self.memory[self.mp] += argument as u8; + } } - + + // safely decrement cell value pub fn do_decrement(&mut self, argument: usize){ - self.memory[self.mp] -= argument as u32 - } - - pub fn do_leftshift(&mut self, argument: usize){ - self.mp += argument + if( self.memory[self.mp] < argument as u8 ){ + self.memory[self.mp] = CELL_SIZE - argument as u8; + } else { + self.memory[self.mp] -= argument as u8; + } } + // safely increment memory pointer address pub fn do_rightshift(&mut self, argument: usize){ - self.mp -= argument + if((self.mp + argument) >= ADDRESS_SIZE ){ + self.mp = ADDRESS_SIZE - (self.mp + argument); + } else { + self.mp += argument; + } + } + + // safely decrement memory pointer address + pub fn do_leftshift(&mut self, argument: usize){ + if( self.mp < argument ) { + self.mp = ADDRESS_SIZE - argument; + } else { + self.mp -= argument; + } } pub fn do_get_char(&mut self, argument: usize){ @@ -83,7 +107,7 @@ impl<'a> Machine<'a> { panic!("input read error") } - self.memory[self.mp] = self.buf[0] as u32; + self.memory[self.mp] = self.buf[0] as u8; } }