forked from TCShenanigans/symphony_stdlib
Compare commits
45
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7621187e6 | ||
|
|
560855f6ac | ||
|
|
0ad8cf639c | ||
|
|
51c7869ce4 | ||
|
|
443623c945 | ||
|
|
a91895b8ac | ||
|
|
ef80825f40 | ||
|
|
d0afb22568 | ||
|
|
a4ecadfad5 | ||
|
|
6e5d0acfeb | ||
|
|
1508c22fc7 | ||
|
|
2b85e5a851 | ||
|
|
cbdcec77b5 | ||
|
|
6686bc54fc | ||
|
|
eb9dbfb8ad | ||
|
|
75da3d730f | ||
|
|
267dcba57d | ||
|
|
0894cdca40 | ||
|
|
a3f74a1f05 | ||
|
|
f91c56deb1 | ||
|
|
63e9d43440 | ||
|
|
8a463207ae | ||
|
|
98ecc3619c | ||
|
|
6be5e520bc | ||
|
|
bb4095b8fc | ||
|
|
bda60bd486 | ||
|
|
46e8fad974 | ||
|
|
993bbb797d | ||
|
|
7a9f2da1c0 | ||
|
|
db8462a2fe | ||
|
|
aa5cc1dbe0 | ||
|
|
f7d3ca2ec5 | ||
|
|
da811fd65d | ||
|
|
49767b61df | ||
|
|
6eb91f3433 | ||
|
|
9220e3dc28 | ||
|
|
25ecd155e1 | ||
|
|
c071a00746 | ||
|
|
5400da0157 | ||
|
|
f19dfd3611 | ||
|
|
a9fa16a241 | ||
|
|
de9ac7fe72 | ||
|
|
1fc4515f6b | ||
|
|
f4ae43bbc1 | ||
|
|
f27715b5b3 |
@@ -0,0 +1,21 @@
|
|||||||
|
# Memory Map
|
||||||
|
|
||||||
|
To not have to allocate a ton of things at run time the Standard Library uses a static memory map for some purposes
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
| Start | Use |
|
||||||
|
| ---- | ---- |
|
||||||
|
| `0x0` | Reset Vector |
|
||||||
|
| `0x16` | Zero Page |
|
||||||
|
| `0x100` | User Code |
|
||||||
|
| `?` | Library Code |
|
||||||
|
| `0x1_0000` | LUTs |
|
||||||
|
| `?` | heap |
|
||||||
|
| `0xXXF0_0000` | Stack |
|
||||||
|
| `0xXXFF_0000` | quick access |
|
||||||
|
|
||||||
|
## Zero Page
|
||||||
|
Some values are needed not often enough to get their own special register, but often enough that it makes sense to keep them at a quickly accessible location.
|
||||||
|
This is what the zero page is for. its position in memory allows us to load them into a register with a single instruction.
|
||||||
|
|
||||||
@@ -3,7 +3,8 @@
|
|||||||
This is a standard library for symphony.
|
This is a standard library for symphony.
|
||||||
It is both intended as a practical toolkit to develop more complex software as well as a teaching resource.
|
It is both intended as a practical toolkit to develop more complex software as well as a teaching resource.
|
||||||
|
|
||||||
If you just want to use the standard library [[src/stdlib.asm]] is your main header, include it after your code.
|
If you just want to use the standard library [[stdlib.asm]] is your main header, include it after your code.
|
||||||
|
You also need to include [[globals.asm]] as the first line in your assembly file.
|
||||||
|
|
||||||
If you are using it as a learning resource have a look at the [teaching folder](teaching).
|
If you are using it as a learning resource have a look at the [teaching folder](teaching).
|
||||||
|
|
||||||
|
|||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
jmp 0x100
|
||||||
|
|
||||||
|
@0x10
|
||||||
|
screen:
|
||||||
|
frambuffer_ptr: U32 0x0
|
||||||
|
size: U32 0x0
|
||||||
|
position_xy: U32 0x0
|
||||||
|
mode: U32 0x0
|
||||||
|
|
||||||
|
@0x100
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
pub include bit
|
pub include bit
|
||||||
pub include imath
|
pub include imath
|
||||||
pub include array
|
|
||||||
pub include console
|
pub include console
|
||||||
pub include strint
|
|
||||||
|
|
||||||
; Needs to be last!
|
; Needs to be last!
|
||||||
pub include LUTs
|
pub include LUTs
|
||||||
|
|||||||
-188
@@ -1,188 +0,0 @@
|
|||||||
; Internal register assignments:
|
|
||||||
; r1 - Partially parsed output integer
|
|
||||||
; r2 - Parsing position
|
|
||||||
; r3 - Character being parsed
|
|
||||||
; r4 - Set to -1 if the input is negative
|
|
||||||
|
|
||||||
; Convert string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
; r4 - Negative marker
|
|
||||||
; Note: Unless the input is "0", tail-calls into a base-specific specialization.
|
|
||||||
pub atoi:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
mov r4, 0
|
|
||||||
load_8 r3, [r2]
|
|
||||||
cmp r3, 0x2D ; '-'
|
|
||||||
jne atoi_positive
|
|
||||||
sub r4, zr, 1 ; Set r4 to -1
|
|
||||||
add r2, r2, 1
|
|
||||||
atoi_positive:
|
|
||||||
load_16 r3, [r2] ; 2-byte prefix "0b", "0o", "0x", etc.
|
|
||||||
or r3, r3, 0x20 ; 2nd char to lower case
|
|
||||||
add r2, r2, 2
|
|
||||||
cmp r3, 0x3062 ; "0b"
|
|
||||||
je atoi_bin_loop
|
|
||||||
cmp r3, 0x306F ; "0o"
|
|
||||||
je atoi_oct_loop
|
|
||||||
cmp r3, 0x3078 ; "0x"
|
|
||||||
je atoi_hex_loop
|
|
||||||
sub r2, r2, 2 ; no matching prefix, move pointer back
|
|
||||||
jmp atoi_dec_loop
|
|
||||||
|
|
||||||
; Epilogue
|
|
||||||
atoi_done:
|
|
||||||
add r1, r1, r4 ; If r4 is -1, negate r1. Else it's 0 and no effect.
|
|
||||||
xor r1, r1, r4
|
|
||||||
mov flags, 0 ; No error
|
|
||||||
jmp r13
|
|
||||||
|
|
||||||
; Convert decimal string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
; r4 - Negative marker
|
|
||||||
pub atoi_dec:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
mov r4, 0
|
|
||||||
load_8 r3, [r2]
|
|
||||||
cmp r3, 0x2D ; '-'
|
|
||||||
jne atoi_dec_positive
|
|
||||||
sub r4, zr, 1 ; Set r4 to -1
|
|
||||||
add r2, r2, 1
|
|
||||||
atoi_dec_loop:
|
|
||||||
load_8 r3, [r2]
|
|
||||||
atoi_dec_positive:
|
|
||||||
sub r3, r3, 0x30 ; '0'
|
|
||||||
cmp r3, 9
|
|
||||||
ja atoi_done
|
|
||||||
add r2, r2, 1
|
|
||||||
lsl flags, r1, 2 ; Use flags to help multiply by 10
|
|
||||||
add r1, r1, flags
|
|
||||||
lsl r1, r1, 1
|
|
||||||
add r1, r1, r3
|
|
||||||
jmp atoi_dec_loop
|
|
||||||
|
|
||||||
; Convert binary string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
; r4 - Negative marker
|
|
||||||
pub atoi_bin:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
mov r4, 0
|
|
||||||
load_8 r3, [r2]
|
|
||||||
cmp r3, 0x2D ; '-'
|
|
||||||
jne atoi_bin_positive
|
|
||||||
sub r4, zr, 1 ; Set r4 to -1
|
|
||||||
add r2, r2, 1
|
|
||||||
atoi_bin_positive:
|
|
||||||
load_16 r3, [r2] ; check for prefix
|
|
||||||
or r3, r3, 0x20 ; 2nd char to lower case
|
|
||||||
cmp r3, 0x3062 ; "0b"
|
|
||||||
jne atoi_bin_loop
|
|
||||||
add r2, r2, 2
|
|
||||||
atoi_bin_loop:
|
|
||||||
load_8 r3, [r2]
|
|
||||||
sub r3, r3, 0x30 ; '0'
|
|
||||||
cmp r3, 1
|
|
||||||
ja atoi_done
|
|
||||||
add r2, r2, 1
|
|
||||||
lsl r1, r1, 1
|
|
||||||
add r1, r1, r3
|
|
||||||
jmp atoi_bin_loop
|
|
||||||
|
|
||||||
; Convert octal string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
; r4 - Negative marker
|
|
||||||
pub atoi_oct:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
mov r4, 0
|
|
||||||
load_8 r3, [r2]
|
|
||||||
cmp r3, 0x2D ; '-'
|
|
||||||
jne atoi_oct_positive
|
|
||||||
sub r4, zr, 1 ; Set r4 to -1
|
|
||||||
add r2, r2, 1
|
|
||||||
atoi_oct_positive:
|
|
||||||
load_16 r3, [r2] ; check for prefix
|
|
||||||
or r3, r3, 0x20 ; 2nd char to lower case
|
|
||||||
cmp r3, 0x306F ; "0o"
|
|
||||||
jne atoi_oct_loop
|
|
||||||
add r2, r2, 2
|
|
||||||
atoi_oct_loop:
|
|
||||||
load_8 r3, [r2]
|
|
||||||
sub r3, r3, 0x30 ; '0'
|
|
||||||
cmp r3, 7
|
|
||||||
ja atoi_done
|
|
||||||
add r2, r2, 1
|
|
||||||
lsl r1, r1, 3
|
|
||||||
add r1, r1, r3
|
|
||||||
jmp atoi_oct_loop
|
|
||||||
|
|
||||||
; Convert hexadecimal string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
; r4 - Negative marker
|
|
||||||
pub atoi_hex:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
mov r4, 0
|
|
||||||
load_8 r3, [r2]
|
|
||||||
cmp r3, 0x2D ; '-'
|
|
||||||
jne atoi_hex_positive
|
|
||||||
sub r4, zr, 1 ; Set r4 to -1
|
|
||||||
add r2, r2, 1
|
|
||||||
atoi_hex_positive:
|
|
||||||
load_16 r3, [r2] ; check for prefix
|
|
||||||
or r3, r3, 0x20 ; 2nd char to lower case
|
|
||||||
cmp r3, 0x3078 ; "0x"
|
|
||||||
jne atoi_hex_loop
|
|
||||||
add r2, r2, 2
|
|
||||||
atoi_hex_loop:
|
|
||||||
load_8 r3, [r2]
|
|
||||||
sub r3, r3, 0x30 ; '0'
|
|
||||||
cmp r3, 10
|
|
||||||
jb atoi_hex_add
|
|
||||||
sub r3, r3, 0x11 ; 'A' - '0'
|
|
||||||
and r3, r3, 0xdf ; to lower case
|
|
||||||
cmp r3, 5 ; 0-5: 6 letters
|
|
||||||
ja atoi_done
|
|
||||||
add r3, r3, 10 ; Adjust for digits below
|
|
||||||
atoi_hex_add:
|
|
||||||
add r2, r2, 1
|
|
||||||
lsl r1, r1, 4
|
|
||||||
add r1, r1, r3
|
|
||||||
jmp atoi_hex_loop
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
; test harness for strint.atoi
|
|
||||||
; Registers:
|
|
||||||
; r8 - test address
|
|
||||||
; r9 - expected result
|
|
||||||
; r10 - actual length
|
|
||||||
; r11 - expected length
|
|
||||||
li r8, tests
|
|
||||||
li r12, end_of_tests
|
|
||||||
next_test:
|
|
||||||
; Set up arguments and run test
|
|
||||||
add r1, r8, 5 ; Start of test string
|
|
||||||
mov r13, atoi_test_ret
|
|
||||||
jmp stdlib.strint.atoi
|
|
||||||
atoi_test_ret:
|
|
||||||
|
|
||||||
; Load reference data
|
|
||||||
load_32 r9, [r8] ; Expected result
|
|
||||||
sub r10, r2, r8
|
|
||||||
sub r10, r10, 5 ; Actual string length
|
|
||||||
add r11, r8, 4 ; Address of expected length
|
|
||||||
load_8 r11, [r11] ; Expected length
|
|
||||||
|
|
||||||
; Verify results
|
|
||||||
cmp r1, r9
|
|
||||||
incorrect_result: jne incorrect_result
|
|
||||||
cmp r10, r11
|
|
||||||
incorrect_length: jne incorrect_length
|
|
||||||
|
|
||||||
; Next test
|
|
||||||
add r8, r8, 0x10
|
|
||||||
cmp r8, r12
|
|
||||||
jl next_test
|
|
||||||
|
|
||||||
; Done
|
|
||||||
success: jmp success
|
|
||||||
|
|
||||||
include ../src/stdlib
|
|
||||||
|
|
||||||
@0x20000
|
|
||||||
tests:
|
|
||||||
; inlen is the number of input bytes the function is expected to consume
|
|
||||||
; addr result inlen instr
|
|
||||||
@0x20000 U32 0 U8 0 "\0"
|
|
||||||
@0x20010 U32 0 U8 1 "0\0"
|
|
||||||
@0x20020 U32 1 U8 1 "1\0"
|
|
||||||
@0x20030 U32 2 U8 1 "2:\0"
|
|
||||||
@0x20040 U32 42 U8 2 "42\0"
|
|
||||||
@0x20050 U32 67 U8 2 "67lol\0"
|
|
||||||
@0x20060 U32 0x69a U8 5 "0x69a@\0"
|
|
||||||
@0x20070 U32 0x4B4 U8 5 "0x4B4g\0"
|
|
||||||
@0x20080 U32 0o23 U8 4 "0o239\0"
|
|
||||||
@0x20090 U32 0b1011 U8 6 "0b1011\0"
|
|
||||||
@0x200a0 U32 0b10001 U8 7 "0b100012\0"
|
|
||||||
@0x200b0 U32 0xFFFFFFEB U8 3 "-21\0" ; Yuk
|
|
||||||
@0x200c0
|
|
||||||
end_of_tests:
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user