| 1 | mod cpu; |
| 2 | mod instructions; |
| 3 | mod memory; |
| 4 | |
| 5 | use cpu::CPU; |
| 6 | use memory::Memory; |
| 7 | use instructions::Instruction; |
| 8 | |
| 9 | fn main() { |
| 10 | let mut cpu = CPU::default(); |
| 11 | let mut mem = Memory::new(); |
| 12 | |
| 13 | |
| 14 | |
| 15 | // a = 10 |
| 16 | mem.write(0x0000, Instruction::MOV as u8); |
| 17 | mem.write(0x0001, 0); |
| 18 | mem.write(0x0002, 5); |
| 19 | |
| 20 | // b = 2 |
| 21 | mem.write(0x0003, Instruction::MOV as u8); |
| 22 | mem.write(0x0004, 1); |
| 23 | mem.write(0x0005, 1); |
| 24 | |
| 25 | // a = a + b |
| 26 | mem.write(0x0006, Instruction::SUB as u8); |
| 27 | mem.write(0x0007, 0); |
| 28 | mem.write(0x0008, 1); |
| 29 | |
| 30 | // set b = 0 |
| 31 | mem.write(0x0009, Instruction::MOV as u8); |
| 32 | mem.write(0x000a, 1); |
| 33 | mem.write(0x000b, 0); |
| 34 | |
| 35 | // halt |
| 36 | mem.write(0x000c, Instruction::HLT as u8); |
| 37 | |
| 38 | while !cpu.halted { |
| 39 | let opcode = mem.read(cpu.pc); |
| 40 | cpu.inc_cp(); |
| 41 | |
| 42 | match opcode { |
| 43 | x if x == Instruction::MOV as u8 => cpu.mov(&mut mem), |
| 44 | x if x == Instruction::ADD as u8 => cpu.add(&mut mem), |
| 45 | x if x == Instruction::SUB as u8 => cpu.sub(&mut mem), |
| 46 | x if x == Instruction::JMP as u8 => {} |
| 47 | x if x == Instruction::JZ as u8 => {} |
| 48 | x if x == Instruction::HLT as u8 => cpu.halt(), |
| 49 | _ => panic!("Unknown opcode {:02X}", opcode), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | println!("{:#?}", cpu); |
| 54 | |
| 55 | } |