added serial-out support to emulator + serial lib & command line mode for dsa emulator
This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
// lib:
|
||||
// print_serial.dsa
|
||||
|
||||
// usage:
|
||||
//
|
||||
// include print_serial "<relative path>"
|
||||
//
|
||||
// usage for print:
|
||||
// push (register containing address of string)
|
||||
// push pcx
|
||||
// jmp print_serial::print
|
||||
//
|
||||
// usage for print_byte:
|
||||
// push (register containing byte)
|
||||
// push pcx
|
||||
// jmp print_serial::print_byte
|
||||
//
|
||||
// usage for print_word:
|
||||
// push (register containing word)
|
||||
// push pcx
|
||||
// jmp print_serial::print_word
|
||||
//
|
||||
// usage for print_hex_byte:
|
||||
// push (register containing byte)
|
||||
// push pcx
|
||||
// jmp print_serial::print_hex_byte
|
||||
//
|
||||
// usage for print_hex_word:
|
||||
// push (register containing word)
|
||||
// push pcx
|
||||
// jmp print_serial::print_hex_word
|
||||
//
|
||||
// usage for print_num:
|
||||
// push (register containing number to print in decimal)
|
||||
// push pcx
|
||||
// jmp print_serial::print_num
|
||||
//
|
||||
// usage for println:
|
||||
// push (register containing address of string)
|
||||
// push pcx
|
||||
// jmp print_serial::println
|
||||
//
|
||||
|
||||
include maths "../maths/core.dsa"
|
||||
|
||||
dw serial: 0x207D0 // 0x20000 + 2000
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the string at addr(arg[0]) to the serial port.
|
||||
print:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
_print_loop:
|
||||
ldb rg0, acc
|
||||
cmp acc, zero
|
||||
jeq _end
|
||||
stb acc, rg1
|
||||
|
||||
addi rg0, 1
|
||||
jmp _print_loop
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the string at addr(arg[0]) followed by a newline to the serial port.
|
||||
println:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
_println_loop:
|
||||
ldb rg0, acc
|
||||
cmp acc, zero
|
||||
jeq _println_end
|
||||
stb acc, rg1
|
||||
|
||||
addi rg0, 1
|
||||
jmp _println_loop
|
||||
|
||||
_println_end:
|
||||
lli 0x0A, rg2 // newline character
|
||||
stb rg2, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the word in arg[0] as 4 raw bytes to the serial port.
|
||||
print_word:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
stb rg0, rg1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the last byte of arg[0] to the serial port.
|
||||
print_byte:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
stb rg0, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the value of arg[0] to the serial port in hex.
|
||||
print_hex_word:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
ldb bpr, rg0, 8
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 9
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 10
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 11
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the last byte of arg[0] to the serial port in hex.
|
||||
print_hex_byte:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
call _print_hex_byte
|
||||
jmp _end
|
||||
|
||||
// function body
|
||||
_print_hex_byte:
|
||||
lli 0xF, rg2
|
||||
push rg0
|
||||
|
||||
shr rg0, 4
|
||||
and rg0, rg2, rg0
|
||||
call _print_hex_nibble
|
||||
pop rg0
|
||||
|
||||
and rg0, rg2, rg0
|
||||
call _print_hex_nibble
|
||||
return
|
||||
|
||||
// print a hex digit
|
||||
_print_hex_nibble:
|
||||
lli 10, rg3
|
||||
cmp rg0, rg3
|
||||
jlt _print_hex_nibble_number
|
||||
addi rg0, 0x37, rg0
|
||||
stb rg0, rg1
|
||||
return
|
||||
|
||||
_print_hex_nibble_number:
|
||||
addi rg0, 0x30, rg0
|
||||
stb rg0, rg1
|
||||
return
|
||||
|
||||
// ------------------------------------------
|
||||
// prints arg[0] as a decimal number to the serial port.
|
||||
print_num:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lli 0, rg5
|
||||
|
||||
cmp rg0, zero
|
||||
jne _print_num_extract_digits
|
||||
|
||||
lli 0x30, rg6
|
||||
push rg6
|
||||
lli 1, rg5
|
||||
jmp _print_num_output
|
||||
|
||||
_print_num_extract_digits:
|
||||
cmp rg0, zero
|
||||
jeq _print_num_output
|
||||
|
||||
push rg0
|
||||
lli 10, rg1
|
||||
push rg1
|
||||
call maths::divmod
|
||||
pop rg0
|
||||
pop rg1
|
||||
|
||||
addi rg1, 0x30, rg6
|
||||
push rg6
|
||||
inc rg5
|
||||
|
||||
jmp _print_num_extract_digits
|
||||
|
||||
_print_num_output:
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
_print_num_output_loop:
|
||||
cmp rg5, zero
|
||||
jeq _print_num_done
|
||||
|
||||
pop rg6
|
||||
stb rg6, rg1
|
||||
dec rg5
|
||||
|
||||
jmp _print_num_output_loop
|
||||
|
||||
_print_num_done:
|
||||
// fall through to _end
|
||||
|
||||
// ------------------------------------------
|
||||
// return
|
||||
_end:
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
+140
-9
@@ -1,12 +1,143 @@
|
||||
// program to just test compute power
|
||||
// GENERATED BY DSC COMPILER
|
||||
// Generated at 2026-02-23 17:29:47
|
||||
|
||||
dw large_num: 0x333333 // 333,333 instructions
|
||||
start:
|
||||
ldw large_num, rg0
|
||||
// Imports
|
||||
include print: "./lib/io/print.dsa"
|
||||
include alloc: "./lib/memory/block_alloc.dsa"
|
||||
|
||||
// run approx 1m instructions
|
||||
loop:
|
||||
dec rg0
|
||||
cmp rg0, zero
|
||||
jgt loop
|
||||
// Globals & Reserved Memory
|
||||
|
||||
// Entry Point
|
||||
dw stack: 0x010000
|
||||
db message: "Process Exited with code:"
|
||||
_init:
|
||||
ldw stack, bpr, 0
|
||||
mov bpr, spr
|
||||
push zero
|
||||
call main
|
||||
call print::print_newline
|
||||
lwi message, rg0
|
||||
push rg0
|
||||
call print::print
|
||||
pop zero
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
hlt
|
||||
|
||||
// Return
|
||||
_ret:
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
|
||||
db str_16: "successful free of ptr"
|
||||
// fn main() -> u32
|
||||
main:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
lli 32, rg0
|
||||
lli 64, rg1
|
||||
// push arg 1
|
||||
push rg0
|
||||
// push arg 0
|
||||
push rg1
|
||||
call alloc::init
|
||||
pop rg2
|
||||
pop zero
|
||||
push rg2
|
||||
// push arg 0
|
||||
push rg2
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
call print::print_newline
|
||||
ldw spr, rg0, 0
|
||||
stw rg0, spr, 0
|
||||
// push arg 0
|
||||
push rg0
|
||||
call alloc::alloc
|
||||
pop rg1
|
||||
push rg1
|
||||
// push arg 0
|
||||
push rg1
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
lli 200, rg0
|
||||
ldw spr, rg1, 0
|
||||
stw rg0, rg1, 0
|
||||
stw rg1, spr, 0
|
||||
call print::print_newline
|
||||
ldw spr, rg0, 4
|
||||
stw rg0, spr, 4
|
||||
// push arg 0
|
||||
push rg0
|
||||
call alloc::alloc
|
||||
pop rg2
|
||||
push rg2
|
||||
// push arg 0
|
||||
push rg2
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
call print::print_newline
|
||||
ldw spr, rg0, 4
|
||||
ldw rg0, rg2, 0
|
||||
stw rg0, spr, 4
|
||||
// push arg 0
|
||||
push rg2
|
||||
call print::print_num
|
||||
pop zero
|
||||
ldw spr, rg2, 4
|
||||
stw rg2, spr, 4
|
||||
addi spr, 4, rg3
|
||||
ldw spr, rg2, 8
|
||||
stw rg2, spr, 8
|
||||
// push arg 1
|
||||
push rg3
|
||||
// push arg 0
|
||||
push rg2
|
||||
call alloc::free
|
||||
pop zero
|
||||
pop zero
|
||||
ldw spr, rg2, 8
|
||||
stw rg2, spr, 8
|
||||
// push arg 0
|
||||
push rg2
|
||||
call alloc::alloc
|
||||
pop rg3
|
||||
push rg3
|
||||
call print::print_newline
|
||||
ldw spr, rg2, 0
|
||||
stw rg2, spr, 0
|
||||
// push arg 0
|
||||
push rg2
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
call print::print_newline
|
||||
ldw spr, rg2, 8
|
||||
stw rg2, spr, 8
|
||||
// push arg 0
|
||||
push rg2
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
ldw spr, rg2, 8
|
||||
lli 0, rg4
|
||||
cmp rg2, rg4
|
||||
lli 1, rg5
|
||||
jeq _cmp_end_12
|
||||
lli 0, rg5
|
||||
_cmp_end_12:
|
||||
cmp rg5, zero
|
||||
jeq _else_14
|
||||
_then_13:
|
||||
lwi str_16, rg4
|
||||
stw rg2, spr, 8
|
||||
// push arg 0
|
||||
push rg4
|
||||
call print::print
|
||||
pop zero
|
||||
jmp _end_15
|
||||
_else_14:
|
||||
nop
|
||||
_end_15:
|
||||
lli 0, rg4
|
||||
stw rg4, bpr, 8
|
||||
jmp _ret
|
||||
Reference in New Issue
Block a user