Krolyxon
Commit be61e5a

Branches

Tags

Refs →

bench51srcmain.rs

61 lines · 1.5 KB · Rust

1mod cpu;
2mod instructions;
3mod memory;
4
5use cpu::CPU;
6use memory::Memory;
7use instructions::Instruction;
8
9fn 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, 3);
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 // JMP to halt
31 mem.write(0x0009, Instruction::JNZ as u8);
32 mem.write(0x000a, 0x0f); // Low
33 mem.write(0x000b, 0x00); // High
34
35 // set b = 0
36 mem.write(0x000c, Instruction::MOV as u8);
37 mem.write(0x000d, 1);
38 mem.write(0x000e, 0);
39
40 // halt
41 mem.write(0x000f, Instruction::HLT as u8);
42
43 while !cpu.halted {
44 let opcode = mem.read(cpu.pc);
45 cpu.inc_pc();
46
47 match opcode {
48 x if x == Instruction::MOV as u8 => cpu.mov(&mut mem),
49 x if x == Instruction::ADD as u8 => cpu.add(&mut mem),
50 x if x == Instruction::SUB as u8 => cpu.sub(&mut mem),
51 x if x == Instruction::JMP as u8 => cpu.jmp(&mut mem),
52 x if x == Instruction::JZ as u8 => cpu.jz(&mut mem),
53 x if x == Instruction::JNZ as u8 => cpu.jnz(&mut mem),
54 x if x == Instruction::HLT as u8 => cpu.halt(),
55 _ => panic!("Unknown opcode {:02X}", opcode),
56 }
57 }
58
59 println!("{:#?}", cpu);
60
61}