|
|
@ -114,7 +114,7 @@ impl Assembler { |
|
|
|
pub fn assemble(&self, input: &str) -> Result<[u8; 0x100], Box<dyn Error>> { |
|
|
|
let mut ret = [0u8; 0x100]; |
|
|
|
let mut i: usize = 0; |
|
|
|
for line in input.lines().map(|line| line.trim()) { |
|
|
|
for line in input.lines().map(|line| strip_comment(line).trim()) { |
|
|
|
match line { |
|
|
|
"" => (), |
|
|
|
line => { |
|
|
@ -146,6 +146,13 @@ impl Default for Assembler { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn strip_comment(input: &str) -> &str { |
|
|
|
match input.find('#') { |
|
|
|
Some(x) => &input[0..x], |
|
|
|
None => input, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn parse_u128(src: &str) -> Result<u128, Box<dyn Error>> { |
|
|
|
if let Some(s) = src.strip_prefix("0b") { |
|
|
|
Ok(u128::from_str_radix(s, 2)?) |
|
|
@ -368,7 +375,7 @@ fn parse_zero(line: &str, regex: &Regex) -> Result<Instruction, Box<dyn Error>> |
|
|
|
#[cfg(test)] |
|
|
|
mod tests { |
|
|
|
use crate::{ |
|
|
|
assembler::Assembler, |
|
|
|
assembler::{strip_comment, Assembler}, |
|
|
|
instruction::{decode, Instruction, Register, ShiftMode}, |
|
|
|
}; |
|
|
|
use lazy_static::lazy_static; |
|
|
@ -722,4 +729,60 @@ mod tests { |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#[test] |
|
|
|
fn asm_assemble_empty() { |
|
|
|
assert_eq!(ASM.assemble("").unwrap(), [0u8; 256]); |
|
|
|
} |
|
|
|
|
|
|
|
#[test] |
|
|
|
fn asm_assemble() { |
|
|
|
let expected = { |
|
|
|
let mut code = [0u8; 256]; |
|
|
|
code[0..6].copy_from_slice(&[0x4F, 0x53, 0xE4, 0x20, 0xF8, 0xF5]); |
|
|
|
code |
|
|
|
}; |
|
|
|
assert_eq!( |
|
|
|
ASM.assemble( |
|
|
|
r#" |
|
|
|
MOV R0.l, 0xF |
|
|
|
MOV R1.l, 0x3 |
|
|
|
ADD |
|
|
|
MOV [0], R0 |
|
|
|
ZERO R0 |
|
|
|
HALT |
|
|
|
"# |
|
|
|
) |
|
|
|
.unwrap(), |
|
|
|
expected |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
#[test] |
|
|
|
fn asm_strip_comment() { |
|
|
|
assert_eq!(strip_comment("MOV [2], R1 # comment"), "MOV [2], R1 "); |
|
|
|
} |
|
|
|
|
|
|
|
#[test] |
|
|
|
fn asm_assemble_comments() { |
|
|
|
let expected = { |
|
|
|
let mut code = [0u8; 256]; |
|
|
|
code[0..6].copy_from_slice(&[0x4F, 0x53, 0xE4, 0x20, 0xF8, 0xF5]); |
|
|
|
code |
|
|
|
}; |
|
|
|
assert_eq!( |
|
|
|
ASM.assemble( |
|
|
|
r#" |
|
|
|
MOV R0.l, 0xF # R0 = 15 |
|
|
|
MOV R1.l, 0x3 # R1 = 3 |
|
|
|
ADD # R0 += R1 (18) |
|
|
|
MOV [0], R0 # MEM[0] = R0 (18) |
|
|
|
ZERO R0 # R0 = 0 |
|
|
|
HALT # EXIT |
|
|
|
"# |
|
|
|
) |
|
|
|
.unwrap(), |
|
|
|
expected |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|