~maddie/STT-implementation

ref: 8960e8f41dc3e6bdf89d2b086d647b1118f1b912 STT-implementation/src/main.rs -rw-r--r-- 1.6 KiB
8960e8f4 — kn0000 Changed file input to commandline arg, and now takes binary files as input, assembler functionality is split off into another program 2 months ago
                                                                                
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
use std::{fs, env};
use minifb::{Window, WindowOptions};
use implementation::*;

fn main() {
    let argument: String = env::args().collect::<Vec<String>>()[1].clone();
    if argument == "-h" || argument == "--help" {
        println!("Takes the path of a binary file and runs it in the STT vm thing");
        return
    }

    let program = fs::read(argument).expect("Unable to read file");

    let mut i = 0;
    let mut memory = [0u16;MEMORY_SIZE];
    
    while program.get(2*i) != None {
        memory[i] = merge(program[2*i], *program.get(2*i + 1).expect("Bad parity"));
        i += 1;
    }

    let mut processor = Processor {
        stack_a: new_stack(),
        stack_b: new_stack(),
        memory,
        carry: false,
        pc: 0
    };
    
    let mut window = Window::new("STT Display", SCREEN_WIDTH, SCREEN_HEIGHT, WindowOptions::default()).unwrap();
    window.update_with_buffer(&[0;(1<<14)],128,128).unwrap();
    while window.is_open() && processor.run() {
        if processor.memory[SCREEN_UPDATE_ADDRESS] != 0 {
            processor.memory[SCREEN_UPDATE_ADDRESS] = 0;
            window.update_with_buffer(&mem_to_buf(&processor.memory), SCREEN_WIDTH, SCREEN_HEIGHT).unwrap();
        }
        
        if processor.memory[KEYBOARD_INTERFACE_ADDRESS] != 0 { //update keyboard state
            processor.memory[KEYBOARD_INTERFACE_ADDRESS] = 0;
            for (i, value) in keys_to_state(window.get_keys()).iter().enumerate() {
                processor.memory[KEYBOARD_INTERFACE_ADDRESS+i+1] = *value;
            }
        }
    }
    
    println!("Stack A: {0:?}\nStack B: {1:?}", processor.stack_a, processor.stack_b);
}