From 8960e8f41dc3e6bdf89d2b086d647b1118f1b912 Mon Sep 17 00:00:00 2001 From: kn0000 Date: Wed, 17 Sep 2025 17:07:29 +0100 Subject: [PATCH] Changed file input to commandline arg, and now takes binary files as input, assembler functionality is split off into another program --- src/lib.rs | 4 +++- src/main.rs | 36 ++++++++++++------------------------ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 840fccea720868e63d83e084f285d67c9b9333b1..4eabdd4ad50990cdcd2dba12755055985357e700 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -156,4 +156,6 @@ pub fn keys_to_state(keys: Vec) -> Vec { //output is using only 10 bit output } - +pub fn merge(a: u8, b: u8) -> u16 { + ((a as u16) << 8) | (b as u16) +} diff --git a/src/main.rs b/src/main.rs index 154a901d51bafff50c9e2b9f7aa932a22c964b1b..062c7afcfa29761064563c4c68ca76f925ecb321 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,36 +1,24 @@ -use std::{fs, io}; +use std::{fs, env}; use minifb::{Window, WindowOptions}; use implementation::*; fn main() { - let mut filename = String::new(); - println!("Enter filename of the program to load into memory"); - io::stdin().read_line(&mut filename).unwrap(); - let program = fs::read_to_string(filename.trim()).unwrap(); + let argument: String = env::args().collect::>()[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]; - for line in program.lines() { - let mut byte = 0u16; - let mut count = 0; - for digit in line.chars() { - match digit { - '0' => { - byte = byte << 1; - count += 1; - } - '1' => { - byte = (byte << 1) | 1; - count += 1; - } - _ => () - } - if count > 10 {panic!("10 bits only :)\nFailed at line {i}")}; - } - memory[i] = byte; + + 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(),