Browse code

Added public interface to asma

Andrew Alderwick authored on 15/05/2021 18:58:50
Showing 1 changed files
... ...
@@ -5,14 +5,15 @@
5 5
 
6 6
 ( vectors )
7 7
 
8
-|0100
9
-	;reset JMP2
8
+|0100 @reset
9
+	;&source-file ;&dest-file ;asma-assemble-file JSR2
10
+	BRK
10 11
 
11
-(
12
-	Asma's public interface.
13
-	These routines are what are expected to be called from programs that bundle
14
-	Asma into bigger projects.
15
-)
12
+	&source-file
13
+		"test.usm 00
14
+		"projects/demos/piano.usm 00
15
+	&dest-file
16
+		"bin/asma-boot.rom 00
16 17
 
17 18
 (
18 19
 	Common macros for use later on.
... ...
@@ -29,22 +30,67 @@
29 30
 %asma-DEO2 { asma-LOG NEQ JMP DEO2k POP POP2 }
30 31
 %asma-DEO { asma-LOG NEQ JMP DEOk POP2 }
31 32
 
33
+(
34
+	Asma's public interface.
35
+	These routines are what are expected to be called from programs that bundle
36
+	Asma into bigger projects.
37
+)
38
+
39
+@asma-assemble-file ( src-filename* dest-filename* -- )
40
+	;asma/dest-filename STA2 ;asma/src-filename STA2
41
+
42
+	;asma-init-first-pass JSR2
43
+	;asma/src-filename LDA2 ;asma-assemble-file-pass JSR2
44
+	asma-IF-ERROR ,&error JCN
45
+
46
+	;asma-init-next-pass JSR2
47
+	;asma/src-filename LDA2 ;asma-assemble-file-pass JSR2
48
+	asma-IF-ERROR ,&error JCN
49
+
50
+	;asma-trees/labels ;asma-print-labels JSR2 ( DEBUG )
51
+	;asma-print-linecount JSR2 ( DEBUG )
52
+	JMP2r
53
+
54
+	&error
55
+	;asma-print-error JSR2 ( DEBUG )
56
+	JMP2r
57
+
32 58
 (
33 59
 	Debugging routines. These all output extra information to the Console.
34 60
 	These can be stripped out to save space, once the references to them are
35
-	removed. Look for the word DEBUG later on to find these references: the
36
-	lines that contain that word can be deleted to strip out the functionality
61
+	removed. Look for the word DEBUG above to find these references: the lines
62
+	that contain that word can be deleted to strip out the functionality
37 63
 	cleanly.
38 64
 )
39 65
 
40
-@asma-dump-sublabels ( incoming-ptr* -- )
66
+@asma-print-error ( -- )
67
+	;asma/error LDA2 .Console/string DEO2
68
+	#3a .Console/char DEO
69
+	#20 .Console/char DEO
70
+	;asma/orig-token LDA2 .Console/string DEO2
71
+	;&line .Console/string DEO2
72
+	;asma/line LDA2 .Console/short DEO2
73
+	#2e .Console/char DEO
74
+	#0a .Console/char DEO
75
+	BRK
76
+
77
+	&line 20 "on 20 "line 20 00
78
+
79
+@asma-print-linecount ( -- )
80
+	;asma/line LDA2 .Console/short #04 asma-DEO2
81
+	;&lines .Console/string #04 asma-DEO2
82
+	JMP2r
83
+
84
+	&lines [ 20 "lines 20 "in 20 "total. 0a 00 ]
85
+
86
+@asma-print-sublabels ( incoming-ptr* -- )
41 87
 	LDA2
42 88
 	ORAk ,&valid-incoming-ptr JCN
43 89
 	POP2 JMP2r
44 90
 
45 91
 	&valid-incoming-ptr
46 92
 	( left node )
47
-	DUP2 ,asma-dump-sublabels JSR
93
+	DUP2 ,asma-print-sublabels JSR
48 94
 	( here )
49 95
 	#09 .Console/char #04 asma-DEO
50 96
 	DUP2 #0004 ADD2
... ...
@@ -60,17 +106,17 @@
60 106
 	#0a .Console/char #04 asma-DEO
61 107
 
62 108
 	( right node )
63
-	#0002 ADD2 ,asma-dump-sublabels JSR
109
+	#0002 ADD2 ,asma-print-sublabels JSR
64 110
 	JMP2r
65 111
 
66
-@asma-dump-labels ( incoming-ptr* -- )
112
+@asma-print-labels ( incoming-ptr* -- )
67 113
 	LDA2
68 114
 	ORAk ,&valid-incoming-ptr JCN
69 115
 	POP2 JMP2r
70 116
 
71 117
 	&valid-incoming-ptr
72 118
 	( left node )
73
-	DUP2 ,asma-dump-labels JSR
119
+	DUP2 ,asma-print-labels JSR
74 120
 	( here )
75 121
 	DUP2 #0004 ADD2
76 122
 	&loop
... ...
@@ -84,42 +130,36 @@
84 130
 	LDA2k .Console/short #04 asma-DEO2
85 131
 	#0a .Console/char #04 asma-DEO
86 132
 	( subtree )
87
-	#0002 ADD2 ;asma-dump-sublabels JSR2
133
+	#0002 ADD2 ;asma-print-sublabels JSR2
88 134
 
89 135
 	( right node )
90
-	#0002 ADD2 ,asma-dump-labels JSR
136
+	#0002 ADD2 ,asma-print-labels JSR
91 137
 	JMP2r
92 138
 
93
-@reset
94
-	;asma-init-assembler JSR2
95
-	;&filename ;asma-assemble-file-pass JSR2
96
-	asma-IF-ERROR ,asma-print-error JCN
97
-	;asma-init-assembler-pass JSR2
98
-	;&filename ;asma-assemble-file-pass JSR2
99
-	asma-IF-ERROR ,asma-print-error JCN
100
-	;asma-trees/labels ;asma-dump-labels JSR2
101
-	;asma/line LDA2 .Console/short #04 asma-DEO2
102
-	;&lines .Console/string #04 asma-DEO2
103
-	BRK
104
-
105
-	&filename
106
-		"test.usm 00
107
-		"projects/examples/gui/label.usm 00
139
+(
140
+	Initialise the assembler state before loading a file or chunk.
141
+)
108 142
 
109
-	&lines [ 20 "lines 20 "in 20 "total. 0a 00 ]
143
+@asma-init-first-pass ( -- )
144
+	#ff ;asma/pass STA
145
+	#0000 ;asma/error STA2
146
+	;asma-heap ;asma/heap STA2
147
+	#0000 ;asma-trees/labels STA2
148
+	;asma-opcodes/_entry ;asma-trees/opcodes STA2
149
+	#0000 ;asma-trees/macros STA2
150
+	( fall through )
110 151
 
111
-@asma-print-error ( -- )
112
-	;asma/error LDA2 .Console/string DEO2
113
-	#3a .Console/char DEO
114
-	#20 .Console/char DEO
115
-	;asma/orig-token LDA2 .Console/string DEO2
116
-	;&line .Console/string DEO2
117
-	;asma/line LDA2 .Console/short DEO2
118
-	#2e .Console/char DEO
119
-	#0a .Console/char DEO
120
-	BRK
152
+@asma-init-next-pass ( -- )
153
+	;asma/pass LDA #01 ADD ;asma/pass STA
154
+	#00 ;asma/state STA
155
+	#0000 ;asma/addr STA2
156
+	#0100 ;asma/written-addr STA2
157
+	#0001 ;asma/line STA2
158
+	JMP2r
121 159
 
122
-	&line 20 "on 20 "line 20 00
160
+(
161
+	Divide a file up into chunks, and pass each chunk to asma-assemble-chunk.
162
+)
123 163
 
124 164
 @asma-assemble-file-pass ( filename-ptr* -- )
125 165
 	#0000
... ...
@@ -144,21 +184,14 @@
144 184
 	POP2 POP2 POP2
145 185
 	JMP2r
146 186
 
147
-@asma-init-assembler ( -- )
148
-	#ff ;asma/pass STA
149
-	#0000 ;asma/error STA2
150
-	;asma-heap ;asma/heap STA2
151
-	#0000 ;asma-trees/labels STA2
152
-	;asma-opcodes/_entry ;asma-trees/opcodes STA2
153
-	#0000 ;asma-trees/macros STA2
154
-
155
-@asma-init-assembler-pass ( -- )
156
-	;asma/pass LDA #01 ADD ;asma/pass STA
157
-	#00 ;asma/state STA
158
-	#0000 ;asma/addr STA2
159
-	#0100 ;asma/written-addr STA2
160
-	#0001 ;asma/line STA2
161
-	JMP2r
187
+(
188
+	Assemble a chunk of source code, which begins with whitespace or the start
189
+	of a token and is divided up into tokens separated by whitespace. If the
190
+	chunk ends with whitespace, assembled-up-to-ptr* will equal ptr* + len* and
191
+	every token in the chunk will have been assembled. If the chunk ends with a
192
+	non-whitespace character, assembled-up-to-ptr* will point to the beginning
193
+	of the last token in the chunk.
194
+)
162 195
 
163 196
 @asma-assemble-chunk ( ptr* len* -- assembled-up-to-ptr* )
164 197
 	OVR2 ADD2 #0001 SUB2 SWP2 STH2k
... ...
@@ -191,7 +224,7 @@
191 224
 	POP POP2 POP2
192 225
 	JMP2r
193 226
 
194
-@asma [ &pass $1 &state $1 &line $2 &token $2 &orig-token $2 &heap $2 &addr $2 &written-addr $2 &flush-fn $2 &error $2 ]
227
+@asma [ &pass $1 &state $1 &line $2 &token $2 &orig-token $2 &heap $2 &addr $2 &written-addr $2 &flush-fn $2 &src-filename $2 &dest-filename $2 &error $2 ]
195 228
 @asma-trees [ &labels $2 &macros $2 &opcodes $2 &scope $2 ]
196 229
 
197 230
 (