Browse code

Extracted heap library from asma

Andrew Alderwick authored on 06/10/2021 05:42:19
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,52 @@
1
+(
2
+	heap functions
3
+
4
+	The heap is an area of memory that is written from the bottom up. These
5
+	are a few convenience functions to do that writing.
6
+
7
+	There is a global short called "heap" that must be written to before using
8
+	these functions, otherwise the zero page and program memory could be
9
+	overwritten.
10
+
11
+	A simple program could use all unallocated memory for the heap like so:
12
+
13
+		|0100 @reset
14
+			;my-heap ;heap STA2
15
+
16
+			(the rest of your code)
17
+
18
+		@my-heap
19
+
20
+	Note that if there is a risk that the heap may overflow its bounds, it is
21
+	strongly recommended to check where it is writing to. ";heap LDA2" will
22
+	tell you where the next byte is written.
23
+)
24
+
25
+@heap $2
26
+
27
+@append-heap-byte ( byte -- )
28
+	,heap LDR2 ( byte heap* )
29
+	INC2k ,heap STR2
30
+	STA
31
+	JMP2r
32
+
33
+@append-heap-short ( short^ -- )
34
+	,heap LDR2 ( short^ heap* )
35
+	INC2k INC2 ,heap STR2
36
+	STA2
37
+	JMP2r
38
+
39
+@append-heap-string ( string* -- )
40
+	( copies a null-terminated string onto the heap, including the null )
41
+	STH2 ,heap LDR2 ( heap* / string* )
42
+	#01 JMP ( skip past INC2r )
43
+
44
+	&loop
45
+	INC2r ( heap* / string* )
46
+	LDAkr DUPr STH2k STAr ( heap* / string* byte )
47
+	INC2
48
+	LITr f7 JCNr ( f7 is the value ",&loop" would produce )
49
+	POP2r ( heap* )
50
+	,heap STR2
51
+	JMP2r
52
+
... ...
@@ -143,9 +143,9 @@
143 143
 
144 144
 @asma-print-heap-usage ( -- )
145 145
 	;asma/log-level LDA #08 AND #00 EQU ,&skip JCN
146
-	;asma/heap LDA2 ;asma-heap SUB2 ;asma-print-short JSR2
146
+	;heap LDA2 ;asma-heap SUB2 ;asma-print-short JSR2
147 147
 	;&str1 ;asma-print-string JSR2
148
-	;asma-heap/end ;asma/heap LDA2 SUB2 ;asma-print-short JSR2
148
+	;asma-heap/end ;heap LDA2 SUB2 ;asma-print-short JSR2
149 149
 	;&str2 ;asma-print-string JSR2
150 150
 	&skip
151 151
 	JMP2r
... ...
@@ -242,7 +242,7 @@
242 242
 		;asma/error STA2
243 243
 		;asma-trees/labels STA2
244 244
 		;asma-trees/macros STA2
245
-	;asma-heap ;asma/heap STA2
245
+	;asma-heap ;heap STA2
246 246
 	;asma-opcodes/_entry ;asma-trees/opcodes STA2
247 247
 	( fall through )
248 248
 
... ...
@@ -339,7 +339,7 @@ include projects/library/file-read-chunks.tal
339 339
 @asma [
340 340
 	&pass $1 &state $1 &line $2 &break $1 &eof $1
341 341
 	&token $2 &orig-token $2
342
-	&heap $2 &addr $2 &written-addr $2 &flush-fn $2
342
+	&addr $2 &written-addr $2 &flush-fn $2
343 343
 	&src-filename $2 &dest-filename $2
344 344
 	&error $2 &log-level $1
345 345
 ]
... ...
@@ -559,27 +559,7 @@ include projects/library/file-read-chunks.tal
559 559
 	;asma-write-buffer .File/save DEO2
560 560
 	JMP2r
561 561
 
562
-@asma-append-heap-byte ( dummy byte -- dummy )
563
-	;asma/heap LDA2
564
-	OVR2 OVR2 STA POP
565
-	INC2 ;asma/heap STA2
566
-	POP
567
-	JMP2r
568
-
569
-@asma-append-heap-short ( dummy short* -- dummy )
570
-	SWP
571
-	,asma-append-heap-byte JSR
572
-	,asma-append-heap-byte JMP ( tail call )
573
-
574
-@asma-append-heap-string ( string* -- )
575
-	LDAk
576
-	DUP ,asma-append-heap-byte JSR
577
-	,&keep-going JCN
578
-	POP2 JMP2r
579
-
580
-	&keep-going
581
-	INC2
582
-	,asma-append-heap-string JMP
562
+include projects/library/heap.tal
583 563
 
584 564
 @asma-traverse-tree ( incoming-ptr* -- binary-ptr* 00 if key found
585 565
                                     OR node-incoming-ptr* 01 if key not found )
... ...
@@ -615,12 +595,12 @@ include projects/library/binary-tree.tal
615 595
 	;asma-msg-macro ;asma/error STA2
616 596
 	JMP2r
617 597
 
618
-	&not-exist
598
+	&not-exist ( incoming-ptr* )
619 599
 	( define macro by creating new node )
620
-	;asma/heap LDA2 SWP2 STA2
621
-	#0000 ;asma-append-heap-short JSR2 ( less-than pointer )
622
-	#0000 ;asma-append-heap-short JSR2 ( greater-than pointer )
623
-	;asma/token LDA2 ;asma-append-heap-string JSR2 ( key )
600
+	;heap LDA2 SWP2 STA2
601
+	#0000 ;append-heap-short JSR2 ( less-than pointer )
602
+	#0000 ;append-heap-short JSR2 ( greater-than pointer )
603
+	;asma/token LDA2 ;append-heap-string JSR2 ( key )
624 604
 	#04 asma-STATE-SET
625 605
 	JMP2r
626 606
 
... ...
@@ -630,12 +610,12 @@ include projects/library/binary-tree.tal
630 610
 
631 611
 @asma-macro-body
632 612
 	;asma/state LDA #10 AND ,&skip JCN
633
-	;asma/token LDA2 ;asma-append-heap-string JSR2
613
+	;asma/token LDA2 ;append-heap-string JSR2
634 614
 	&skip
635 615
 	JMP2r
636 616
 
637 617
 @asma-macro-end
638
-	#00 ;asma-append-heap-byte JSR2
618
+	#00 ;append-heap-byte JSR2
639 619
 	#14 asma-STATE-CLEAR
640 620
 	JMP2r
641 621
 
... ...
@@ -643,7 +623,7 @@ include projects/library/binary-tree.tal
643 623
 	;asma-trees/labels ,asma-label-helper JSR
644 624
 	,&already-existed JCN
645 625
 
646
-	#0000 ;asma-append-heap-short JSR2 ( data2: subtree incoming ptr )
626
+	#0000 ;append-heap-short JSR2 ( data2: subtree incoming ptr )
647 627
 
648 628
 	&already-existed
649 629
 	#0002 ADD2 ;asma-trees/scope STA2
... ...
@@ -665,14 +645,14 @@ include projects/library/binary-tree.tal
665 645
 
666 646
 	&new-label ( incoming-ptr* )
667 647
 	( define label by creating new node )
668
-	;asma/heap LDA2 SWP2 STA2
669
-	#0000 ;asma-append-heap-short JSR2 ( less-than pointer )
670
-	#0000 ;asma-append-heap-short JSR2 ( greater-than pointer )
671
-	;asma/token LDA2 ;asma-append-heap-string JSR2 ( key )
648
+	;heap LDA2 SWP2 STA2
649
+	#0000 ;append-heap-short JSR2 ( less-than pointer )
650
+	#0000 ;append-heap-short JSR2 ( greater-than pointer )
651
+	;asma/token LDA2 ;append-heap-string JSR2 ( key )
672 652
 
673
-	;asma/heap LDA2
653
+	;heap LDA2
674 654
 
675
-	;asma/addr LDA2 ;asma-append-heap-short JSR2 ( data1: address )
655
+	;asma/addr LDA2 ;append-heap-short JSR2 ( data1: address )
676 656
 	#00 JMP2r
677 657
 
678 658
 @asma-pad-absolute
... ...
@@ -862,8 +842,8 @@ include projects/library/binary-tree.tal
862 842
 
863 843
 @asma-include-filename
864 844
 	#08 asma-STATE-CLEAR
865
-	;asma/heap LDA2
866
-		;asma/token LDA2 ;asma-append-heap-string JSR2
845
+	;heap LDA2
846
+		;asma/token LDA2 ;append-heap-string JSR2
867 847
 		;asma-assemble-file-pass JSR2
868 848
 	;asma/break LDAk INC ROT ROT STA
869 849
 	JMP2r