jetcrab\vm/
registers.rs

1use crate::vm::types::{CodeAddress, FramePointer, StackIndex};
2use crate::vm::value::Value;
3
4pub struct Registers {
5    pub accumulator: Value,
6    pub program_counter: CodeAddress,
7    pub stack_pointer: StackIndex,
8    pub base_pointer: FramePointer,
9}
10
11impl Default for Registers {
12    fn default() -> Self {
13        Self::new()
14    }
15}
16
17impl Registers {
18    pub fn new() -> Self {
19        Self {
20            accumulator: Value::Undefined,
21            program_counter: CodeAddress::new(0),
22            stack_pointer: StackIndex::new(0),
23            base_pointer: FramePointer::new(0),
24        }
25    }
26
27    pub fn reset(&mut self) {
28        self.accumulator = Value::Undefined;
29        self.program_counter = CodeAddress::new(0);
30        self.stack_pointer = StackIndex::new(0);
31        self.base_pointer = FramePointer::new(0);
32    }
33
34    pub fn set_accumulator(&mut self, value: Value) {
35        self.accumulator = value;
36    }
37
38    pub fn get_accumulator(&self) -> &Value {
39        &self.accumulator
40    }
41
42    pub fn increment_pc(&mut self) {
43        self.program_counter.increment();
44    }
45
46    pub fn set_pc(&mut self, address: CodeAddress) {
47        self.program_counter = address;
48    }
49
50    pub fn get_pc(&self) -> CodeAddress {
51        self.program_counter
52    }
53}