implement condition register

This commit is contained in:
dogeystamp 2024-01-05 11:46:24 -05:00
parent 298d6d5c4c
commit c8dfa736fd
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38

View File

@ -1,9 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
#![allow(unused_variables)] #![allow(unused_variables)]
const MEM_SIZE: usize = 1 << 16;
const PC_START: usize = 0x3000;
enum OpCodes { enum OpCodes {
// branch // branch
BR = 0, BR = 0,
@ -39,6 +36,23 @@ enum OpCodes {
TRAP, TRAP,
} }
////////////////
// registers
////////////////
//
// condition flags
//
enum CondFlags {
// positive (P)
POS = 1 << 0,
// zero (Z)
ZRO = 1 << 1,
// negative (N)
NEG = 1 << 2,
}
struct Registers { struct Registers {
r0: u16, r0: u16,
r1: u16, r1: u16,
@ -53,6 +67,8 @@ struct Registers {
count: u16, count: u16,
} }
const PC_START: usize = 0x3000;
impl Registers { impl Registers {
fn new() -> Registers { fn new() -> Registers {
Registers { Registers {
@ -94,8 +110,32 @@ impl Registers {
let reg = &*self.register_reference(idx); let reg = &*self.register_reference(idx);
*reg *reg
} }
fn set_cond(&mut self, idx: u16) {
let val = self.get_reg(idx);
if (val as i16) > 0 {
self.cond = CondFlags::POS as u16;
} else if (val as i16) < 0 {
self.cond = CondFlags::NEG as u16;
} else if val == 0 {
self.cond = CondFlags::ZRO as u16;
}
}
fn set_reg_with_cond(&mut self, idx: u16, val: u16) {
self.set_reg(idx, val);
self.set_cond(idx);
}
} }
////////////////
// VM interface
////////////////
const MEM_SIZE: usize = 1 << 16;
struct VM { struct VM {
mem: [u16; MEM_SIZE] mem: [u16; MEM_SIZE]
} }
@ -105,6 +145,7 @@ fn main() {
let mut regs = Registers::new(); let mut regs = Registers::new();
println!("was {}", regs.get_reg(0)); println!("was {}", regs.get_reg(0));
regs.set_reg(0, 3); regs.set_reg_with_cond(0, (0i16) as u16);
println!("set to {}", regs.get_reg(0)); println!("set to {}", regs.get_reg(0));
println!("condition: {}", regs.get_reg(9));
} }