+/// Operations are coded on 16 bits (Four nibbles, a nibble = half-byte):
+/// 1) Opcode group (c).
+/// 2) First register (x). / Memory address (nnn).
+/// 3) Second register (y). / Integer (kk) / Memory address (nnn).
+/// 4) Opcode subgroup (d) / Number of bytes (n). / Integer (kk) / Memory address (nnn).
struct CPU {
- current_operation: u16,
- registers: [u8; 2],
+ registers: [u8; 16],
+ position_in_memory: usize,
+ memory: [u8; 0x1000], // 4 KB of memory.
}
+impl CPU {
+ fn read_opcode(&self) -> u16 {
+ self.current_operation
+ }
+
+ fn run(&mut self) {
+ // loop {
+ let opcode = self.read_opcode();
+ let c = ((opcode & 0xF000) >> 12) as u8;
+ let x = ((opcode & 0x0F00) >> 8) as u8;
+ let y = ((opcode & 0x00F0) >> 4) as u8;
+ let d = ((opcode & 0x000F) >> 0) as u8;
+
+ match (c, x, y, d) {
+ (0x8, _, _, 0x4) => self.add_xy(x, y),
+ _ => todo!("opcode: {:04x}", opcode),
+ }
+ //}
+ }
+
+ fn add_xy(&mut self, x: u8, y: u8) {
+ self.registers[x as usize] += self.registers[y as usize];
+ }
+}
+
+mod tests {
+ use super::*;
+
+ #[test]
+ fn addition() {
+ let mut cpu = CPU {
+ current_operation: 0,
+ registers: [0; 2]
+ };
+
+ cpu.current_operation = 0x8014;
+ cpu.registers[0] = 5;
+ cpu.registers[1] = 10;
+
+ cpu.run();
+
+ assert_eq!(cpu.registers[0], 15);
+ }
+}
\ No newline at end of file