aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 7c638bab3c00ccc91529ec059cfc894d88c6d98f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
mod cpu;
mod instructions;
mod memory;

use cpu::CPU;
use memory::Memory;
use instructions::Instruction;

fn main() {
    let mut cpu = CPU::default();
    let mut mem = Memory::new();



    // a = 10
    mem.write(0x0000, Instruction::MOV as u8);
    mem.write(0x0001, 0);
    mem.write(0x0002, 5);

    // b = 2
    mem.write(0x0003, Instruction::MOV as u8);
    mem.write(0x0004, 1);
    mem.write(0x0005, 1);

    // a = a + b
    mem.write(0x0006, Instruction::SUB as u8);
    mem.write(0x0007, 0);
    mem.write(0x0008, 1);

    // set b = 0
    mem.write(0x0009, Instruction::MOV as u8);
    mem.write(0x000a, 1);
    mem.write(0x000b, 0);

    // halt
    mem.write(0x000c, Instruction::HLT as u8);

    while !cpu.halted {
        let opcode = mem.read(cpu.pc);
        cpu.inc_cp();

        match opcode {
            x if x == Instruction::MOV as u8 =>  cpu.mov(&mut mem),
            x if x == Instruction::ADD as u8 => cpu.add(&mut mem),
            x if x == Instruction::SUB as u8 => cpu.sub(&mut mem),
            x if x == Instruction::JMP as u8 => {}
            x if x == Instruction::JZ  as u8 => {}
            x if x == Instruction::HLT as u8 => cpu.halt(),
            _ => panic!("Unknown opcode {:02X}", opcode),
        }
    }

    println!("{:#?}", cpu);

}