summaryrefslogtreecommitdiff
path: root/meap/cpu/src
diff options
context:
space:
mode:
authorAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
committerAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
commit7e8ee5ed9cad6484e9f13f81731b102ced58402e (patch)
tree5395402ab07bbb5a659dbd68c701e22a1227202f /meap/cpu/src
downloadlearning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.tar.xz
learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.zip
Init.
Diffstat (limited to 'meap/cpu/src')
-rwxr-xr-xmeap/cpu/src/main.rs199
1 files changed, 199 insertions, 0 deletions
diff --git a/meap/cpu/src/main.rs b/meap/cpu/src/main.rs
new file mode 100755
index 0000000..2feb29d
--- /dev/null
+++ b/meap/cpu/src/main.rs
@@ -0,0 +1,199 @@
+//const ADD_XY: u8 = 0x8;
+//
+//struct CPU {
+// current_operation: u16,
+// registers: [u8; 2],
+//}
+//
+//impl CPU {
+// fn run(&mut self) {
+// let encoded_op = self.current_operation;
+// let op = ((encoded_op & 0xf000) >> 12) as u8;
+// let x = ((encoded_op & 0x0f00) >> 8) as u8;
+// let y = ((encoded_op & 0x00f0) >> 4) as u8;
+//
+// match op {
+// ADD_XY => {
+// self.add_xy(x, y);
+// },
+// _ => unimplemented!(),
+// }
+// }
+//
+// fn add_xy(&mut self, x: u8, y: u8) {
+// self.registers[x as usize] += self.registers[y as usize];
+// }
+//}
+//
+//
+//fn main() {
+// let mut cpu = CPU {
+// current_operation: 0x8014,
+// registers: [0; 2],
+// };
+//
+// cpu.registers[0] = 5;
+// cpu.registers[1] = 10;
+// cpu.run();
+//
+// assert_eq!(cpu.registers[0], 15);
+// println!("5 + 10 = {}", cpu.registers[0]);
+//}
+
+//const ARITHMETIC_AND_LOGIC: u8 = 0x8;
+//const HALT: u8 = 0x0;
+//const ADD_XY: u8 = 0x4;
+//
+//struct CPU {
+// // current_operation: u16
+// registers: [u8; 16],
+// position_in_memory: usize,
+// memory: [u8; 4096],
+//}
+//
+//impl CPU {
+// fn run(&mut self) {
+// loop {
+// let op_byte1 = self.memory[self.position_in_memory] as u16;
+// let op_byte2 = self.memory[self.position_in_memory + 1] as u16;
+// let raw_op = op_byte1 << 8 | op_byte2;
+//
+// let op_major = ((raw_op & 0xf000) >> 12) as u8;
+// let x = ((raw_op & 0x0f00) >> 8) as u8;
+// let y = ((raw_op & 0x00f0) >> 4) as u8;
+// let op_minor = (raw_op & 0x000f) as u8;
+//
+// self.position_in_memory += 2;
+//
+// match (op_major, op_minor) {
+// (HALT, HALT) => { return; },
+// (ARITHMETIC_AND_LOGIC, ADD_XY) => self.add_xy(x, y),
+// _ => unimplemented!("opcode: {:04x}", raw_op),
+// }
+// }
+// }
+//
+// fn add_xy(&mut self, x: u8, y: u8) {
+// self.registers[x as usize] += self.registers[y as usize];
+// }
+//}
+//
+//fn main() {
+// let mut cpu = CPU {
+// registers: [0; 16],
+// memory: [0; 4096],
+// position_in_memory: 0,
+// };
+//
+// cpu.registers[0] = 5;
+// cpu.registers[1] = 10;
+// cpu.registers[2] = 10;
+// cpu.registers[3] = 10;
+//
+// cpu.memory[0] = 0x80;
+// cpu.memory[1] = 0x14;
+// cpu.memory[2] = 0x80;
+// cpu.memory[3] = 0x24;
+// cpu.memory[4] = 0x80;
+// cpu.memory[5] = 0x34;
+//
+// cpu.run();
+//
+// assert_eq!(cpu.registers[0], 35);
+// println!("5 + 10 + 10 + 10 = {}", cpu.registers[0]);
+//
+//}
+
+struct CPU {
+ registers: [u8; 16],
+ position_in_memory: usize,
+ memory: [u8; 4096],
+ stack: [u16; 16],
+ stack_pointer: usize,
+}
+
+impl CPU {
+ fn run(&mut self) {
+ loop {
+ let op_byte1 = self.memory[self.position_in_memory] as u16;
+ let op_byte2 = self.memory[self.position_in_memory + 1] as u16;
+ let opcode = op_byte1 << 8 | op_byte2;
+
+ let x = ((opcode & 0x0f00) >> 8) as u8;
+ let y = ((opcode & 0x00f0) >> 4) as u8;
+ let op_minor = (opcode & 0x000f) as u8;
+ let addr = opcode & 0x0fff;
+
+ self.position_in_memory += 2;
+
+ match opcode {
+ 0x0000 => { return; },
+ 0x00ee => { self.ret(); },
+ 0x2000...0x2fff => { self.call(addr); },
+ 0x8000...0x8fff => {
+ match op_minor {
+ 4 => { self.add_xy(x, y); }
+ _ => { unimplemented!("opcode: {:04x}", opcode); },
+ }
+ },
+ _ => unimplemented!("opcode {:04x}", opcode),
+ }
+ }
+ }
+
+ fn call(&mut self, addr: u16) {
+ let sp = self.stack_pointer;
+ let stack = &mut self.stack;
+
+ if sp > stack.len() {
+ panic!("stack overflow!");
+ }
+
+ stack[sp] = self.position_in_memory as u16;
+ self.stack_pointer += 1;
+ self.position_in_memory = addr as usize;
+
+ }
+
+ fn ret(&mut self) {
+ if self.stack_pointer == 0 {
+ panic!("stack underflow!");
+ }
+
+ self.stack_pointer -= 1;
+ self.position_in_memory = self.stack[self.stack_pointer] as usize;
+ }
+
+ fn add_xy(&mut self, x: u8, y: u8) {
+ self.registers[x as usize] += self.registers[y as usize];
+ }
+}
+
+fn main() {
+ let mut cpu = CPU {
+ registers: [0; 16],
+ memory: [0; 4096],
+ position_in_memory: 0,
+ stack: [0; 16],
+ stack_pointer: 0,
+ };
+
+ cpu.registers[0] = 5;
+ cpu.registers[1] = 10;
+
+ cpu.memory[0x000] = 0x21;
+ cpu.memory[0x001] = 0x00;
+ cpu.memory[0x002] = 0x21;
+ cpu.memory[0x003] = 0x00;
+ cpu.memory[0x100] = 0x80;
+ cpu.memory[0x101] = 0x14;
+ cpu.memory[0x102] = 0x80;
+ cpu.memory[0x103] = 0x14;
+ cpu.memory[0x104] = 0x00;
+ cpu.memory[0x105] = 0xee;
+
+ cpu.run();
+
+ assert_eq!(cpu.registers[0], 45);
+ dbg!(cpu.registers[0]);
+}