Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ae92510fb8 | |||
| 7c63340888 |
@@ -7,7 +7,6 @@ use common::prelude::Register;
|
|||||||
pub fn lexer(mut program: String, module: u64) -> Result<Vec<Token>, AssembleError> {
|
pub fn lexer(mut program: String, module: u64) -> Result<Vec<Token>, AssembleError> {
|
||||||
let mut tokens = Vec::new();
|
let mut tokens = Vec::new();
|
||||||
|
|
||||||
program = program.replace(',', "");
|
|
||||||
let lines = program.lines();
|
let lines = program.lines();
|
||||||
let mut literal = String::new();
|
let mut literal = String::new();
|
||||||
|
|
||||||
@@ -39,6 +38,11 @@ pub fn lexer(mut program: String, module: u64) -> Result<Vec<Token>, AssembleErr
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let token = token.trim_end_matches(',');
|
||||||
|
if token.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(token) = parse_register(token)? {
|
if let Some(token) = parse_register(token)? {
|
||||||
tokens.push(token);
|
tokens.push(token);
|
||||||
} else if let Some(token) = parse_opcode(token)? {
|
} else if let Some(token) = parse_opcode(token)? {
|
||||||
@@ -61,6 +65,8 @@ pub fn lexer(mut program: String, module: u64) -> Result<Vec<Token>, AssembleErr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("{:#?}", tokens);
|
||||||
|
|
||||||
Ok(tokens)
|
Ok(tokens)
|
||||||
}
|
}
|
||||||
pub fn parse_register(token: &str) -> Result<Option<Token>, AssembleError> {
|
pub fn parse_register(token: &str) -> Result<Option<Token>, AssembleError> {
|
||||||
|
|||||||
@@ -51,19 +51,26 @@ impl fmt::Display for Node {
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or_else(String::new, |symbol| format!("{symbol}:\n"));
|
.map_or_else(String::new, |symbol| format!("{symbol}:\n"));
|
||||||
|
|
||||||
|
let args = self
|
||||||
|
.args()
|
||||||
|
.into_iter()
|
||||||
|
.map(|arg| arg.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" ");
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"\x1b[93m{} \t\x1b[94m{} \x1b[37m{:?} \x1b[0m",
|
"\x1b[93m{} \t\x1b[94m{} \x1b[37m{} \x1b[0m",
|
||||||
symbol,
|
symbol,
|
||||||
self.opcode(),
|
self.opcode(),
|
||||||
self.args()
|
args,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Symbol {
|
impl fmt::Display for Symbol {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{} ( module: {})", self.name, self.module)
|
write!(f, "{} [ID:{}]", self.name, self.module)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,6 +181,18 @@ pub enum Token {
|
|||||||
Opcode(Opcode),
|
Opcode(Opcode),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Token {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Symbol(symbol) => write!(f, "{}", symbol),
|
||||||
|
Self::Register(register) => write!(f, "{}", register),
|
||||||
|
Self::Immediate(immediate) => write!(f, "{}", immediate),
|
||||||
|
Self::StringLit(string_lit) => write!(f, "{}", string_lit),
|
||||||
|
Self::Opcode(opcode) => write!(f, "{}", opcode),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||||
pub enum TokenType {
|
pub enum TokenType {
|
||||||
Symbol,
|
Symbol,
|
||||||
|
|||||||
@@ -65,7 +65,19 @@ print_word:
|
|||||||
ldw bpr, rg0, 8
|
ldw bpr, rg0, 8
|
||||||
ldw current, rg1
|
ldw current, rg1
|
||||||
|
|
||||||
stw rg0, rg1
|
addi rg1, 3
|
||||||
|
|
||||||
|
stb rg0, rg1
|
||||||
|
subi rg1, 1
|
||||||
|
shr rg0, 8
|
||||||
|
stb rg0, rg1
|
||||||
|
subi rg1, 1
|
||||||
|
shr rg0, 8
|
||||||
|
stb rg0, rg1
|
||||||
|
subi rg1, 1
|
||||||
|
shr rg0, 8
|
||||||
|
stb rg0, rg1
|
||||||
|
|
||||||
addi rg1, 4
|
addi rg1, 4
|
||||||
jmp _end
|
jmp _end
|
||||||
|
|
||||||
@@ -175,18 +187,24 @@ print_newline:
|
|||||||
push bpr
|
push bpr
|
||||||
mov spr, bpr
|
mov spr, bpr
|
||||||
|
|
||||||
|
// load variables into registers
|
||||||
ldw display, rg0
|
ldw display, rg0
|
||||||
ldw current, rg1
|
ldw current, rg1
|
||||||
|
|
||||||
|
// get the offset from the display base
|
||||||
sub rg1, rg0, rg0
|
sub rg1, rg0, rg0
|
||||||
|
|
||||||
lwi 80, rg2
|
lwi 80, rg2
|
||||||
|
pusha 3
|
||||||
push rg0
|
push rg0
|
||||||
push rg2
|
push rg2
|
||||||
call maths::divmod
|
call maths::divmod
|
||||||
pop rg2
|
pop zero // result
|
||||||
addi rg2, 80
|
pop rg3 // remainder
|
||||||
ldw display, rg1
|
popa 3
|
||||||
add rg1, rg2, rg1
|
|
||||||
|
sub rg1, rg3, rg2
|
||||||
|
addi rg2, 80, rg1
|
||||||
|
|
||||||
// _end saves the display state
|
// _end saves the display state
|
||||||
jmp _end
|
jmp _end
|
||||||
|
|||||||
@@ -4,24 +4,25 @@ fib_n:
|
|||||||
push bpr
|
push bpr
|
||||||
mov spr, bpr
|
mov spr, bpr
|
||||||
|
|
||||||
ldw bpr, rg1, 8 // load op 2
|
ldw bpr, rg0, 8 // load arg
|
||||||
|
mov rg1, rg2
|
||||||
|
lwi 1, rg1
|
||||||
|
|
||||||
start:
|
start:
|
||||||
mov rg1, rg2
|
add rg1, rg2, rg3
|
||||||
add rg1, rg2, acc
|
|
||||||
|
|
||||||
pusha 4
|
pusha 4
|
||||||
push rg1
|
push rg1
|
||||||
call print::print_hex_word
|
call print::print_hex_byte
|
||||||
pop rg1
|
call print::print_newline
|
||||||
|
pop zero
|
||||||
popa 4
|
popa 4
|
||||||
|
|
||||||
mov rg2, rg1
|
mov rg2, rg1
|
||||||
mov acc, rg2
|
mov rg3, rg2
|
||||||
|
|
||||||
cmp rg0, zero
|
|
||||||
dec rg0
|
dec rg0
|
||||||
|
cmp rg0, zero
|
||||||
jgt start
|
jgt start
|
||||||
|
|
||||||
stw rg1, bpr, 8
|
stw rg1, bpr, 8
|
||||||
|
|||||||
+55
-3
@@ -1,3 +1,7 @@
|
|||||||
|
include fib: "./lib/maths/fib.dsa"
|
||||||
|
include maths: "./lib/maths/core.dsa"
|
||||||
|
include print: "./lib/io/print.dsa"
|
||||||
|
|
||||||
dw idt: 0xFFFF0000
|
dw idt: 0xFFFF0000
|
||||||
dw stack: 0x10000
|
dw stack: 0x10000
|
||||||
init:
|
init:
|
||||||
@@ -9,11 +13,59 @@ init:
|
|||||||
ldw stack, bpr
|
ldw stack, bpr
|
||||||
mov bpr, spr
|
mov bpr, spr
|
||||||
|
|
||||||
|
dw string: "hello world"
|
||||||
start:
|
start:
|
||||||
lwi 10, rg0
|
|
||||||
push rg0
|
lwi 37, rg0
|
||||||
call maths::fib_n
|
lwi 12, rg1
|
||||||
|
push rg0
|
||||||
|
push rg1
|
||||||
|
call maths::divmod
|
||||||
|
pop rg0 // result
|
||||||
|
pop rg1 // remainder
|
||||||
|
|
||||||
|
push rg1
|
||||||
|
push rg0
|
||||||
|
call print::print_hex_byte
|
||||||
|
call print::print_whitespace
|
||||||
|
pop zero
|
||||||
|
call print::print_hex_byte
|
||||||
|
call print::print_newline
|
||||||
|
|
||||||
|
lwi string, rg0
|
||||||
|
//lwi 10, rg0
|
||||||
|
pusha 4
|
||||||
|
push rg0
|
||||||
|
call print::print
|
||||||
|
//call fib::fib_n
|
||||||
pop zero
|
pop zero
|
||||||
|
call print::print_newline
|
||||||
|
popa 4
|
||||||
|
|
||||||
|
pusha 4
|
||||||
|
push rg0
|
||||||
|
call print::print
|
||||||
|
//call fib::fib_n
|
||||||
|
pop zero
|
||||||
|
call print::print_newline
|
||||||
|
popa 4
|
||||||
|
|
||||||
|
pusha 4
|
||||||
|
push rg0
|
||||||
|
call print::print
|
||||||
|
//call fib::fib_n
|
||||||
|
pop zero
|
||||||
|
call print::print_newline
|
||||||
|
popa 4
|
||||||
|
|
||||||
|
pusha 4
|
||||||
|
push rg0
|
||||||
|
call print::print
|
||||||
|
//call fib::fib_n
|
||||||
|
pop zero
|
||||||
|
call print::print_newline
|
||||||
|
popa 4
|
||||||
|
|
||||||
hlt
|
hlt
|
||||||
|
|
||||||
// fault handler in case we fail DSA.
|
// fault handler in case we fail DSA.
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ init:
|
|||||||
mov bpr, spr
|
mov bpr, spr
|
||||||
|
|
||||||
|
|
||||||
db string: "I won, the game!"
|
db string: "I won, the game!"
|
||||||
db hexbyte: 0xab
|
db hexbyte: 0xab
|
||||||
dw hexword: 0x1234abcd
|
dw hexword: 0x1234abcd
|
||||||
db replace: "I lost"
|
db replace: "I lost"
|
||||||
@@ -64,9 +64,6 @@ start:
|
|||||||
call print::print
|
call print::print
|
||||||
pop zero
|
pop zero
|
||||||
|
|
||||||
lli 3, rg0
|
|
||||||
ldw rg0, rg0
|
|
||||||
|
|
||||||
hlt
|
hlt
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user