Krolyxon
Commit d05f298

Branches

Tags

Refs →

bench51srcmain.rs

58 lines · 1.5 KB · Rust

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