diff --git a/README.md b/README.md index de769a9..89b776d 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,10 @@ A virtual machine for controlling a wifi led strip. | load (rgp, rgd) | loads the value the pointer register points to into the data register | 0x04 | | clear \ | clears a register (sets it to 0x00) | 0x05 | | write (rgd, rgp) | writes the value in the data register to the address of the pointer register | 0x06 | -| label | creates a label at the current position. | 0x07 | +| label | creates a label at the current position. | 0x07 | | goto (rgl) | goes to the label with name rgl | 0x08 | | debug (*) | prints out the state for debug information | 0x09 | +| print | prints the value of a register | 0x0A | | add (rgd, rgi, rgo) | adds the value of rgi to the value of rgd and writes the result into rgo | 0x10 | | sub (rgd, rgi, rgo) | substracts rgi from rgd and writes the result into rgo | 0x11 | | mul (rgd, rgi, rgo) | multiplies rgd by rgi and writes the result to rgo | 0x12 | diff --git a/src/bin/lsambler.rs b/src/bin/lsambler.rs index cfff0e0..e2b764f 100644 --- a/src/bin/lsambler.rs +++ b/src/bin/lsambler.rs @@ -1,8 +1,8 @@ use ledstrip_vm::registers::get_register_code_by_name; use ledstrip_vm::tokens::{ AddToken, ClearToken, CmdToken, CopyToken, DebugToken, DivToken, ExitToken, GotoToken, JeToken, - JgToken, JlToken, LabelToken, LoadToken, LshToken, ModToken, MulToken, PauseToken, RshToken, - SendToken, SetToken, SubToken, Token, WriteToken, + JgToken, JlToken, LabelToken, LoadToken, LshToken, ModToken, MulToken, PauseToken, PrintToken, + RshToken, SendToken, SetToken, SubToken, Token, WriteToken, }; use std::fs::{read_to_string, File}; use std::io; @@ -85,6 +85,9 @@ fn get_token(line: &str) -> Option> { }), "goto" => some_box!(GotoToken), "debug" => some_box!(DebugToken), + "print" => some_box!(PrintToken { + register: get_register_code_by_name(instr_parts.next()?)? + }), "add" => some_box!(AddToken), "sub" => some_box!(SubToken), "mul" => some_box!(MulToken), diff --git a/src/runtime.rs b/src/runtime.rs index f0612fc..ce0ea02 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -5,9 +5,9 @@ use crate::registers::{ use crate::tokens::{ AddToken, ClearToken, CmdToken, CopyToken, DebugToken, DivToken, ExitToken, FromBytecode, GotoToken, JeToken, JgToken, JlToken, LabelToken, LoadToken, LshToken, ModToken, MulToken, - PauseToken, RshToken, SendToken, SetToken, SubToken, Token, WriteToken, T_ADD, T_CLEAR, T_CMD, - T_COPY, T_DEBUG, T_DIV, T_EXIT, T_GOTO, T_JE, T_JG, T_JL, T_LABEL, T_LOAD, T_LSH, T_MOD, T_MUL, - T_PAUSE, T_RSH, T_SEND, T_SET, T_SUB, T_WRITE, + PauseToken, PrintToken, RshToken, SendToken, SetToken, SubToken, Token, WriteToken, T_ADD, + T_CLEAR, T_CMD, T_COPY, T_DEBUG, T_DIV, T_EXIT, T_GOTO, T_JE, T_JG, T_JL, T_LABEL, T_LOAD, + T_LSH, T_MOD, T_MUL, T_PAUSE, T_PRINT, T_RSH, T_SEND, T_SET, T_SUB, T_WRITE, }; use std::cell::RefCell; use std::collections::HashMap; @@ -100,6 +100,10 @@ impl Runtime { } T_GOTO => text.push(Box::new(GotoToken)), T_DEBUG => text.push(Box::new(DebugToken)), + T_PRINT => text.push(Box::new(PrintToken::from_bytecode(&[ + instruction, + code_iter.next().unwrap(), + ]))), T_ADD => text.push(Box::new(AddToken)), T_SUB => text.push(Box::new(SubToken)), T_MUL => text.push(Box::new(MulToken)), diff --git a/src/tokens.rs b/src/tokens.rs index 3d6f633..03e383b 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -15,6 +15,7 @@ pub const T_WRITE: u8 = 0x06; pub const T_LABEL: u8 = 0x07; pub const T_GOTO: u8 = 0x08; pub const T_DEBUG: u8 = 0x09; +pub const T_PRINT: u8 = 0x0A; pub const T_ADD: u8 = 0x10; pub const T_SUB: u8 = 0x11; pub const T_MUL: u8 = 0x12; @@ -311,9 +312,36 @@ impl Token for DebugToken { } } -impl FromBytecode for GotoToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self +#[derive(Debug, Clone)] +pub struct PrintToken { + pub register: u8, +} + +impl Token for PrintToken { + fn to_bytecode(&self) -> Vec { + vec![T_PRINT, self.register] + } + + fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> { + let mut value = if let Some(rg) = runtime.get_1byte_register(self.register) { + rg.get() as u32 + } else if let Some(rg) = runtime.get_4byte_register(self.register) { + rg.get() + } else if self.register == RCS { + runtime.rcs.get() as u32 + } else { + 0 + }; + + println!("{}", value); + + Ok(()) + } +} + +impl FromBytecode for PrintToken { + fn from_bytecode(code: &[&u8]) -> Self { + Self { register: *code[1] } } } @@ -331,13 +359,6 @@ impl Token for AddToken { Ok(()) } } - -impl FromBytecode for AddToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct SubToken; @@ -353,12 +374,6 @@ impl Token for SubToken { } } -impl FromBytecode for SubToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct MulToken; @@ -374,12 +389,6 @@ impl Token for MulToken { } } -impl FromBytecode for MulToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct DivToken; @@ -395,12 +404,6 @@ impl Token for DivToken { } } -impl FromBytecode for DivToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct ModToken; @@ -416,12 +419,6 @@ impl Token for ModToken { } } -impl FromBytecode for ModToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct LshToken; @@ -437,12 +434,6 @@ impl Token for LshToken { } } -impl FromBytecode for LshToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct RshToken; @@ -458,12 +449,6 @@ impl Token for RshToken { } } -impl FromBytecode for RshToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct JgToken; @@ -481,12 +466,6 @@ impl Token for JgToken { } } -impl FromBytecode for JgToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct JlToken; @@ -504,12 +483,6 @@ impl Token for JlToken { } } -impl FromBytecode for JlToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct JeToken; @@ -527,12 +500,6 @@ impl Token for JeToken { } } -impl FromBytecode for JeToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct PauseToken; @@ -548,12 +515,6 @@ impl Token for PauseToken { } } -impl FromBytecode for PauseToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - #[derive(Debug, Clone)] pub struct CmdToken; @@ -569,12 +530,6 @@ impl Token for CmdToken { } } -impl FromBytecode for CmdToken { - fn from_bytecode(_: &[&u8]) -> Self { - Self - } -} - pub struct SendToken; impl Token for SendToken {