uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit be2e01e9b3adca7c8b6a71b2e59c2f9612da25bd
parent 7058122d251b232772e4f8f1161f22ed746e0577
Author: Andrew Alderwick <andrew@alderwick.co.uk>
Date:   Wed,  6 Oct 2021 06:42:19 +0100

Extracted heap library from asma

Diffstat:
Aprojects/library/heap.tal | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Mprojects/software/asma.tal | 62+++++++++++++++++++++-----------------------------------------
2 files changed, 73 insertions(+), 41 deletions(-)

diff --git a/projects/library/heap.tal b/projects/library/heap.tal @@ -0,0 +1,52 @@ +( + heap functions + + The heap is an area of memory that is written from the bottom up. These + are a few convenience functions to do that writing. + + There is a global short called "heap" that must be written to before using + these functions, otherwise the zero page and program memory could be + overwritten. + + A simple program could use all unallocated memory for the heap like so: + + |0100 @reset + ;my-heap ;heap STA2 + + (the rest of your code) + + @my-heap + + Note that if there is a risk that the heap may overflow its bounds, it is + strongly recommended to check where it is writing to. ";heap LDA2" will + tell you where the next byte is written. +) + +@heap $2 + +@append-heap-byte ( byte -- ) + ,heap LDR2 ( byte heap* ) + INC2k ,heap STR2 + STA + JMP2r + +@append-heap-short ( short^ -- ) + ,heap LDR2 ( short^ heap* ) + INC2k INC2 ,heap STR2 + STA2 + JMP2r + +@append-heap-string ( string* -- ) + ( copies a null-terminated string onto the heap, including the null ) + STH2 ,heap LDR2 ( heap* / string* ) + #01 JMP ( skip past INC2r ) + + &loop + INC2r ( heap* / string* ) + LDAkr DUPr STH2k STAr ( heap* / string* byte ) + INC2 + LITr f7 JCNr ( f7 is the value ",&loop" would produce ) + POP2r ( heap* ) + ,heap STR2 + JMP2r + diff --git a/projects/software/asma.tal b/projects/software/asma.tal @@ -143,9 +143,9 @@ @asma-print-heap-usage ( -- ) ;asma/log-level LDA #08 AND #00 EQU ,&skip JCN - ;asma/heap LDA2 ;asma-heap SUB2 ;asma-print-short JSR2 + ;heap LDA2 ;asma-heap SUB2 ;asma-print-short JSR2 ;&str1 ;asma-print-string JSR2 - ;asma-heap/end ;asma/heap LDA2 SUB2 ;asma-print-short JSR2 + ;asma-heap/end ;heap LDA2 SUB2 ;asma-print-short JSR2 ;&str2 ;asma-print-string JSR2 &skip JMP2r @@ -242,7 +242,7 @@ ;asma/error STA2 ;asma-trees/labels STA2 ;asma-trees/macros STA2 - ;asma-heap ;asma/heap STA2 + ;asma-heap ;heap STA2 ;asma-opcodes/_entry ;asma-trees/opcodes STA2 ( fall through ) @@ -339,7 +339,7 @@ include projects/library/file-read-chunks.tal @asma [ &pass $1 &state $1 &line $2 &break $1 &eof $1 &token $2 &orig-token $2 - &heap $2 &addr $2 &written-addr $2 &flush-fn $2 + &addr $2 &written-addr $2 &flush-fn $2 &src-filename $2 &dest-filename $2 &error $2 &log-level $1 ] @@ -559,27 +559,7 @@ include projects/library/file-read-chunks.tal ;asma-write-buffer .File/save DEO2 JMP2r -@asma-append-heap-byte ( dummy byte -- dummy ) - ;asma/heap LDA2 - OVR2 OVR2 STA POP - INC2 ;asma/heap STA2 - POP - JMP2r - -@asma-append-heap-short ( dummy short* -- dummy ) - SWP - ,asma-append-heap-byte JSR - ,asma-append-heap-byte JMP ( tail call ) - -@asma-append-heap-string ( string* -- ) - LDAk - DUP ,asma-append-heap-byte JSR - ,&keep-going JCN - POP2 JMP2r - - &keep-going - INC2 - ,asma-append-heap-string JMP +include projects/library/heap.tal @asma-traverse-tree ( incoming-ptr* -- binary-ptr* 00 if key found OR node-incoming-ptr* 01 if key not found ) @@ -615,12 +595,12 @@ include projects/library/binary-tree.tal ;asma-msg-macro ;asma/error STA2 JMP2r - &not-exist + &not-exist ( incoming-ptr* ) ( define macro by creating new node ) - ;asma/heap LDA2 SWP2 STA2 - #0000 ;asma-append-heap-short JSR2 ( less-than pointer ) - #0000 ;asma-append-heap-short JSR2 ( greater-than pointer ) - ;asma/token LDA2 ;asma-append-heap-string JSR2 ( key ) + ;heap LDA2 SWP2 STA2 + #0000 ;append-heap-short JSR2 ( less-than pointer ) + #0000 ;append-heap-short JSR2 ( greater-than pointer ) + ;asma/token LDA2 ;append-heap-string JSR2 ( key ) #04 asma-STATE-SET JMP2r @@ -630,12 +610,12 @@ include projects/library/binary-tree.tal @asma-macro-body ;asma/state LDA #10 AND ,&skip JCN - ;asma/token LDA2 ;asma-append-heap-string JSR2 + ;asma/token LDA2 ;append-heap-string JSR2 &skip JMP2r @asma-macro-end - #00 ;asma-append-heap-byte JSR2 + #00 ;append-heap-byte JSR2 #14 asma-STATE-CLEAR JMP2r @@ -643,7 +623,7 @@ include projects/library/binary-tree.tal ;asma-trees/labels ,asma-label-helper JSR ,&already-existed JCN - #0000 ;asma-append-heap-short JSR2 ( data2: subtree incoming ptr ) + #0000 ;append-heap-short JSR2 ( data2: subtree incoming ptr ) &already-existed #0002 ADD2 ;asma-trees/scope STA2 @@ -665,14 +645,14 @@ include projects/library/binary-tree.tal &new-label ( incoming-ptr* ) ( define label by creating new node ) - ;asma/heap LDA2 SWP2 STA2 - #0000 ;asma-append-heap-short JSR2 ( less-than pointer ) - #0000 ;asma-append-heap-short JSR2 ( greater-than pointer ) - ;asma/token LDA2 ;asma-append-heap-string JSR2 ( key ) + ;heap LDA2 SWP2 STA2 + #0000 ;append-heap-short JSR2 ( less-than pointer ) + #0000 ;append-heap-short JSR2 ( greater-than pointer ) + ;asma/token LDA2 ;append-heap-string JSR2 ( key ) - ;asma/heap LDA2 + ;heap LDA2 - ;asma/addr LDA2 ;asma-append-heap-short JSR2 ( data1: address ) + ;asma/addr LDA2 ;append-heap-short JSR2 ( data1: address ) #00 JMP2r @asma-pad-absolute @@ -862,8 +842,8 @@ include projects/library/binary-tree.tal @asma-include-filename #08 asma-STATE-CLEAR - ;asma/heap LDA2 - ;asma/token LDA2 ;asma-append-heap-string JSR2 + ;heap LDA2 + ;asma/token LDA2 ;append-heap-string JSR2 ;asma-assemble-file-pass JSR2 ;asma/break LDAk INC ROT ROT STA JMP2r