Refactoring

This commit is contained in:
2022-03-12 15:39:29 -06:00
parent 90f2177e5b
commit 19eb4d57f3
14 changed files with 444 additions and 22 deletions
+6 -6
View File
@@ -154,8 +154,8 @@ impl CPU {
Opcode::Store => self.memory.poke8(x, y.to_bytes()[0]),
Opcode::Storew => self.memory.poke24(x, y),
Opcode::Setsdp => {
self.dp = x.into();
self.sp = y.into()
self.dp = x;
self.sp = y
}
Opcode::Brz => {
if y == 0 {
@@ -195,7 +195,7 @@ impl CPU {
self.push_data(x);
self.push_data(z)
}
Opcode::Jmp => return self.pop_data().into(),
Opcode::Jmp => return self.pop_data(),
Opcode::Jmpr => {
let x = i32::from(self.pop_data());
return self.pc + x;
@@ -203,9 +203,9 @@ impl CPU {
Opcode::Call => {
let x = self.pop_data();
self.push_call(self.pc + instruction.length as i32);
return x.into();
return x;
}
Opcode::Ret => return self.pop_call().into(),
Opcode::Ret => return self.pop_call(),
Opcode::Hlt => self.halted = true,
Opcode::Load => {
let x = self.pop_data();
@@ -217,7 +217,7 @@ impl CPU {
}
Opcode::Inton => self.int_enabled = true,
Opcode::Intoff => self.int_enabled = false,
Opcode::Setiv => self.iv = self.pop_data().into(),
Opcode::Setiv => self.iv = self.pop_data(),
Opcode::Sdp => {
self.push_data(self.sp);
self.push_data(self.dp + 3) // The +3 accounts for the word we're about to push
+54
View File
@@ -163,6 +163,60 @@ impl TryFrom<u8> for Opcode {
}
}
impl TryFrom<&str> for Opcode {
type Error = InvalidOpcode;
fn try_from(value: &str) -> Result<Self, Self::Error> {
use Opcode::*;
Ok(match value {
"nop" => Nop,
"add" => Add,
"sub" => Sub,
"mul" => Mul,
"div" => Div,
"mod" => Mod,
"rand" => Rand,
"and" => And,
"or" => Or,
"xor" => Xor,
"not" => Not,
"gt" => Gt,
"lt" => Lt,
"agt" => Agt,
"alt" => Alt,
"lshift" => Lshift,
"rshift" => Rshift,
"arshift" => Arshift,
"pop" => Pop,
"dup" => Dup,
"swap" => Swap,
"pick" => Pick,
"rot" => Rot,
"jmp" => Jmp,
"jmpr" => Jmpr,
"call" => Call,
"ret" => Ret,
"brz" => Brz,
"brnz" => Brnz,
"hlt" => Hlt,
"load" => Load,
"loadw" => Loadw,
"store" => Store,
"storew" => Storew,
"inton" => Inton,
"intoff" => Intoff,
"setiv" => Setiv,
"sdp" => Sdp,
"setsdp" => Setsdp,
"pushr" => Pushr,
"popr" => Popr,
"peekr" => Peekr,
"debug" => Debug,
_ => return Err(InvalidOpcode(255))
})
}
}
#[test]
fn test_decode() {
assert_eq!(Opcode::try_from(18), Ok(Opcode::Pop));
+5 -5
View File
@@ -1,7 +1,7 @@
// 128k, the amount of memory in a standard Vulcan machine
pub const MEM_SIZE: u32 = 128 * 1024;
#[derive(Debug, Copy, Clone, Eq, Ord)]
#[derive(Debug, Copy, Clone, Eq)]
pub struct Word(u32);
impl Word {
@@ -10,9 +10,9 @@ impl Word {
/// # Example
///
/// ```
/// use vulcan_emu::Word;
/// use vcore::word::Word;
///
/// assert_eq!(Word::from_bytes([0x01, 0x02, 0x03]) == 0x010203);
/// assert!(Word::from_bytes([0x01, 0x02, 0x03]) == 0x030201);
/// ```
pub fn from_bytes(bytes: [u8; 3]) -> Self {
let [a, b, c] = bytes;
@@ -24,9 +24,9 @@ impl Word {
/// # Example
///
/// ```
/// use vulcan_emu::Word;
/// use vcore::word::Word;
///
/// assert_eq!(Word::from(0x010203).to_bytes(), [0x01, 0x02, 0x03]);
/// assert_eq!(Word::from(0x010203).to_bytes(), [0x03, 0x02, 0x01]);
/// ```
pub fn to_bytes(self) -> [u8; 3] {
let [a, b, c, _] = self.0.to_le_bytes();