forked from CorshamTech/6502-Tiny-BASIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.il
286 lines (270 loc) · 4.59 KB
/
basic.il
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
LET;
;=====================================================
;=====================================================
;=====================================================
; This is the IL of the BASIC (or whatever) language.
; Because of the way macros are implemented by as65,
; labels can't be on the same line as a macro
; invocation, so that's why labels are on separate
; lines.
;
IL equ *
;THE IL CONTROL SECTION
START:
INIT ;INITIALIZE
NLINE ;WRITE CRLF
ERRGOTO CO ;where to go after an error
VINIT ;clear all variables
;
; This is where we jump to get a line of commands or
; a program from the user.
;
CO:
GETLINE ;WRITE PROMPT AND GET LINE
TSTL XEC ;TEST FOR LINE NUMBER
INSERT ;INSERT IT (MAY BE DELETE)
IJMP CO
XEC:
XINIT ;INITIALIZE
;STATEMENT EXECUTOR
STMT:
TST S1,"LET" ;IS STATEMENT A LET
TSTV ERRVEC ;YES, PLACE VAR ADDRESS ON AESTK
TST ERRVEC,"=" ;(This line originally omitted)
CALL EXPR ;PLACE EXPR VALUE ON AESTK
DONE ;REPORT ERROR IF NOT NEXT
STORE ;STORE RESULT
NXT CO ;AND SEQUENCE TO NEXT
IJMP STMT
S1:
TST S3,"GO" ;GOTO OT GOSUB?
TST S2,"TO" ;YES...TO, OR...SUB
CALL EXPR ;GET LABEL
DONE ;ERROR IF CR NOT NEXT
XFER ;SET UP AND JUMP
S2:
TST ERRVEC,"SUB" ;ERROR IF NO MATCH
CALL EXPR ;GET DESTINATION
DONE ;ERROR IF CR NOT NEXT
SAV ;SAVE RETURN LINE
XFER ;AND JUMP
S3:
TST S8,"PRINT" ;PRINT
S4:
TST S7,QUOTE ;TEST FOR QUOTE
PRS ;PRINT STRING
S5:
TST S6A,COMMA ;IS THERE MORE?
SPC ;SPACE TO NEXT ZONE
IJMP S4 ;YES JUMP BACK
;
; If a semicolon, don't do anything.
;
S6A:
TST S6,SEMICOLON
IJMP S4
S6:
DONE ;ERROR IF CR NOT NEXT
NLINE
NXT CO
IJMP STMT
;
; A jump for code too far away for relative branch
;
ERRVEC:
IJMP UNKNOWN
;
S7:
CALL EXPR
PRN ;PRINT IT
IJMP S5 ;IS THERE MORE?
S8:
TST S9,"IF" ;IF STATEMENT
CALL EXPR ;GET EXPRESSION
CALL RELOP ;DETERMINE OPR AND PUT ON STK
CALL EXPR ;GET EXPRESSION
TST UNKNOWN,"THEN" ;(This line originally omitted)
CMPR ;PERFORM COMPARISON -- PERFORMS NXT IF FALSE
IJMP STMT
S9:
TST S12,"INPUT" ;INPUT STATEMENT
S10:
TSTV UNKNOWN ;GET VAR ADDRESS (Originally CALL VAR = nonexist)
INNUM ;MOVE NUMBER FROM TTY TO AESTK
STORE ;STORE IT
TST S11,COMMA ;IS THERE MORE?
IJMP S10 ;YES
S11:
DONE ;MUST BE CR
NXT CO ;SEQUENCE TO NEXT
IJMP STMT
S12:
TST S13,"RETURN" ;RETURN STATEMENT
DONE ;MUST BE CR
RSTR ;RESTORE LINE NUMBER OF CALL
NXT CO ;SEQUENCE TO NEXT STATEMENT
IJMP STMT
S13:
TST S14,"END"
FIN
S14:
TST S15,"LIST" ;LIST COMMAND
DONE
LST
IJMP CO
S15:
TST S16,"RUN" ;RUN COMMAND
DONE
VINIT ;clear variables
LIT 1 ;GOTO line 1
XFER ;Bob's addition
; EXIT
IJMP STMT ;and run!
S16:
TST S17A,"NEW" ;clear program
DONE
IJMP START
S17A:
TST S17,"EXIT" ;allow them to exit BASIC
EXIT
S17:
TST S17B,"REM" ;REMark. Skip rest of line
NXT CO
IJMP STMT
;
; Commands related to saving/restoring programs
; to/from mass storage.
;
S17B:
if (XKIM || CTMON65) && DISK_ACCESS
TST S17C,"SAVE"
OPENWRITE
DLIST
DCLOSE
IJMP CO
S17C:
TST UNKNOWN,"LOAD"
OPENREAD
S17CLP:
DGETLINE ;get line from file
TSTL S17EOL ;no line num means EOL
INSERT ;put it into the program
IJMP S17CLP ;keep going
S17EOL
DCLOSE ;close disk file
IJMP CO ;back to start
endif
;
; Else, unknown command.
;
UNKNOWN:
ERR ERR_SYNTAX ;SYNTAX ERROR
;-----------------------------------------------------
EXPR:
TST E0,"-"
CALL TERM ;TEST FOR UNARY -.
NEG ;GET VALUE
IJMP E1 ;NEGATE IT
E0:
TST E1A,"+" ;LOOK FOR MORE
E1A:
CALL TERM ;TEST FOR UNARY +
E1:
TST E2,"+" ;LEADING TERM
CALL TERM
ADD
IJMP E1
E2:
TST E3,"-" ;ANY MORE?
CALL TERM ;DIFFERENCE TERM
SUB
IJMP E1
E3:
T2:
RTN ;ANY MORE?
TERM:
CALL FACT
T0:
TST T1,"*"
CALL FACT ;PRODUCT FACTOR.
MUL
IJMP T0
T1:
TST T2,"/"
CALL FACT ;QUOTIENT FACTOR.
DIV
IJMP T0
UNKNOWNVEC:
IJMP UNKNOWN
;
; Factor an expression. Always test for functions
; first or else they'll be confused for variables.
;
FACT:
TST F2A,"FREE()"
FREE
RTN
;
; RND() is supposed to have an argument but if none
; was provided, just assume a large value.
;
F2A:
TST F2B,"RND("
TST F2A1,")"
LIT 32766
RANDOM
RTN
F2A1:
CALL FACT ;GET RANGE
TST UNKNOWN,")"
RANDOM
RTN
F2B:
TST F2C,"ABS("
CALL FACT ;get value
TST UNKNOWN,")"
ABS
RTN
F2C:
TSTV F0
IND ;YES, GET THE VALUE.
RTN
F0:
TSTN F1 ;NUMBER, GET ITS VALUE.
RTN
F1:
TST F2A,"(" ;PARENTHESIZED EXPR.
CALL EXPR
TST F2,")"
RTN
F2:
ERR ERR_SYNTAX ;ERROR.
RELOP:
TST iR0,"="
LIT 2 ;=
RTN
iR0:
TST R4,"<"
TST iR1,"="
LIT 3 ;<=
RTN
iR1:
TST R3,">"
LIT 5 ;<>
RTN
R3:
LIT 1 ;<
RTN
R4:
TST UNKNOWNVEC,">"
TST R5,"="
LIT 6 ;>=
RTN
R5:
TST R6,"<"
LIT 1
RTN ;(This line originally omitted)
R6:
LIT 4 ;>???
RTN
ILEND equ *