Big refactoring, invalid assembly is now unrepresentable assembly

This commit is contained in:
2022-03-15 22:50:19 -05:00
parent abf184f6fc
commit 683ff3095e
8 changed files with 228 additions and 239 deletions
+11 -3
View File
@@ -112,6 +112,10 @@ impl CPU {
}
}
#[allow(clippy::cmp_owned)]
// This is needed because clippy can't figure out, on alt / agt, that even though yes you can
// directly compare two Words, that means something different than comparing the i32s those
// Words represent. That difference is the entire point of the agt / alt instructions.
fn execute(&mut self, instruction: Instruction) -> Word {
if let Some(arg) = instruction.arg {
self.push_data(arg)
@@ -242,7 +246,9 @@ impl CPU {
}
pub fn tick(&mut self) {
if self.halted { return }
if self.halted {
return;
}
match self.fetch() {
Ok(instr) => {
@@ -329,7 +335,7 @@ mod tests {
let mut cpu = CPU::new(Memory::default());
given(&mut cpu);
let new_pc = cpu.execute(Instruction {
opcode: opcode,
opcode,
arg: None,
length: 1,
});
@@ -501,8 +507,10 @@ mod tests {
simple_opcode_test(vec![5, 3], Lt, vec![0]);
simple_opcode_test(vec![5, 7], Lt, vec![1]);
simple_opcode_test(vec![5, to_word(-3)], Agt, vec![1]);
simple_opcode_test(vec![to_word(-3), 5], Agt, vec![0]);
simple_opcode_test(vec![5, 10], Agt, vec![0]);
simple_opcode_test(vec![5, to_word(-3)], Alt, vec![0]);
simple_opcode_test(vec![to_word(-3), 5], Alt, vec![1]);
simple_opcode_test(vec![5, 10], Alt, vec![1]);
simple_opcode_test(vec![0b1100, 2], Rshift, vec![3]);
simple_opcode_test(vec![0b1100, 2], Lshift, vec![0b110000]);
@@ -541,7 +549,7 @@ mod tests {
fn test_cpu_new() {
let cpu = CPU::new(Memory::default());
assert_eq!(cpu.pc, 1024);
assert_eq!(cpu.halted, true);
assert!(cpu.halted);
}
#[test]
+4 -4
View File
@@ -12,7 +12,7 @@ impl Word {
/// ```
/// use vcore::word::Word;
///
/// assert!(Word::from_bytes([0x01, 0x02, 0x03]) == 0x030201);
/// assert_eq!(Word::from_bytes([0x01, 0x02, 0x03]), 0x030201);
/// ```
pub fn from_bytes(bytes: [u8; 3]) -> Self {
let [a, b, c] = bytes;
@@ -130,9 +130,9 @@ impl From<Word> for bool {
#[test]
fn to_from_bool() {
assert_eq!(bool::from(Word::from(0)), false);
assert_eq!(bool::from(Word::from(1)), true);
assert_eq!(bool::from(Word::from(0x123456)), true);
assert!(!bool::from(Word::from(0)));
assert!(bool::from(Word::from(1)));
assert!(bool::from(Word::from(0x123456)));
assert_eq!(Word::from(false), Word::from(0));
assert_eq!(Word::from(true), Word::from(1));