-
Notifications
You must be signed in to change notification settings - Fork 0
/
subasic.sleq
253 lines (196 loc) · 8.96 KB
/
subasic.sleq
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
.include MACROS.inc
.org 0
JMP Start
T: .data 0 ; Temporary variable used in lieu of Z
tMov: .data 0 ; Temporary variable used in lieu of Z for MOV macro
tIdx: .data 0 ; Temporary variable used in lieu of Z for INDEXEDRD/WR macro
tmpA: .data 0 ; Used by macros
tmpB: .data 0 ; Used by macros
tmpC: .data 0 ; Used by macros
atoiVal: .data 0 ; Result from ATOI operation
compResult: .data 0 ; Result from string compare operations
pFound: .data 0 ; Result from FindLine operation
lineNo: .data 0 ; The current line number executing
offsetNextLine: .data 0 ; Offset to the next line
addrNextLine: .data 0 ; The computed address of the next line
bitopResult: .data 0 ; Result from bit operations
program_: .data program ; program_ is a constant holding the address of the program array
seed1: .data 0x1234 ; Seed/state value for the LGC part of the PRNG algorithm
seed2: .data 0x5678 ; Seed/state value for the LFSR part of the PRNG algorithm (can't be 0)
prngVal: .data 0 ; 16-bit pseudo-random number generated by the PRNG algorithm
rndVal: .data 0 ; Random number generated
divRemainder: .data 0 ; Remainder from DIV operation to be used for modulus operations
;-----------------------------------------------------------------------------
; START OF CODE
;-----------------------------------------------------------------------------
x: .data 0x650318
Start:
MOV stack_ SP ; Initialize the stack pointer
PUSH msgLogo
CALL PrintString
DEBUGPRINT "[Debug mode enabled]\r\n"
;-----------------------------------------------------------------------------
; COMMAND LINE PROMPT WITH RESETTING THE STACK AFTER ERROR
;-----------------------------------------------------------------------------
Restart:
CTRLC x ; Clear pending Ctrl-C
MOV program_ pCode
MOV stack_ SP ; Initialize the stack pointer
;-----------------------------------------------------------------------------
; COMMAND LINE PROMPT
;-----------------------------------------------------------------------------
Prompt:
PRINTCRLF
OUT ASCII_GREATER
CALL InputString
MOV buf pCode
CALL GetParsedToken
CMPEQ tokenType TT_EOL Prompt ; Empty line, prompt again
CMPEQ tokenType TT_NUMBER Editor ; Starts with a number, so it's an editor command
PRINTCRLF
CMPEQ tokenValue kwLIST PromptLIST ; LIST command
CMPEQ tokenValue kwRUN PromptRUN ; RUN command
CMPEQ tokenValue kwNEW cmdNEW ; NEW command
PUSH msgInvalidPrompt ; All other things are invalid
JMP SyntaxError
;-----------------------------------------------------------------------------
Error:
;-----------------------------------------------------------------------------
CALL PrintString
PUSH lineNo
CALL PrintInteger
PRINTCRLF
JMP Restart
SyntaxError:
PUSH msgSyntaxError
JMP Error
VarNotFoundError:
PUSH msgVarNotFoundError
JMP Error
;-----------------------------------------------------------------------------
ControlC:
;-----------------------------------------------------------------------------
PUSH msgCtrlC
CALL PrintString
JMP Restart
;-----------------------------------------------------------------------------
PromptRUN:
;-----------------------------------------------------------------------------
CALL GetParsedToken ; Get the line number to start running from
PUSH tokenValue
CALL FindLine
MOV pFound pCode
ProgramLine:
CLR tokenType ; Clear the token type so we don't trigger the EOL check in the loop
INDEXEDRD pCode lineNo ; Get Linenumber of current line as a binary value
JMP0 lineNo cmdEND ; If we reach the end of the code it's an implicit END instruction
INC pCode
INDEXEDRD pCode offsetNextLine ; Get offset to next line
MOV pCode addrNextLine ; Calculate the address of the next line by adding the offset to the current location
ADD offsetNextLine addrNextLine
INC addrNextLine
INC pCode
Statement:
CTRLC x ; Check for Ctrl-C
JMPN0 x ControlC
CALL GetParsedToken
CMPEQ tokenType TT_EOL ProgramLine ; End-Of-Line?
CMPNE tokenType TT_MISC notMisc
CMPEQ tokenValue ASCII_COLON Statement ; End-Of-Statement?
JMP SyntaxError
notMisc:
; Check for keywords and handle them
CMPEQ tokenValue kwEND cmdEND
CMPEQ tokenValue kwREM cmdREM
CMPEQ tokenValue kwPRINT cmdPRINT
CMPEQ tokenValue kwGOTO cmdGOTO
CMPEQ tokenValue kwLET cmdLET
CMPEQ tokenValue kwGOSUB cmdGOSUB
CMPEQ tokenValue kwRETURN cmdRETURN
CMPEQ tokenValue kwIF cmdIF
CMPEQ tokenValue kwFOR cmdFOR
CMPEQ tokenValue kwNEXT cmdNEXT
CMPEQ tokenValue kwINPUT cmdINPUT
CMPEQ tokenValue kwPRHEX cmdPRHEX
CMPEQ tokenValue kwDIM cmdDIM
JMP SyntaxError
;-----------------------------------------------------------------------------
; MESSAGE STRINGS
;-----------------------------------------------------------------------------
msgLogo:
.data $+1
.data "\r\n"
.data " __ __ ___ __ ___ __ __ __ " ,"\r\n"
.data "/__` | | |__) | |__ / \\ | | |\\ | \\ / |__) /\\ /__` | / ` " ,"\r\n"
.data ".__/ \\__/ |__) |___ |___ \\__\\\\ | | | \\| | |__) /--\\ .__/ | \\__, " ,"\r\n"
.data "\r\n"
.data "[[ version 0.06 - (c) 2024 Mats Engstrom - github.com/mengstr/SUBASIC ]]\r\n", 0
.data "\r\n"
.data 0
msgLineNotFound: .data $+1, "\r\nLine not found\r\n", 0
msgVarNotFoundError: .data $+1, "\r\nVariable not found at line ", 0
msgSyntaxError: .data $+1, "\r\nSyntax error at line ", 0
msgUnknownCommand: .data $+1, "Unknown command", 0
msgInvalidToken: .data $+1, "Invalid token,0
msgInvalidPrompt: .data $+1, "Invalid command",0
msgInvalidOperator: .data $+1, "Invalid operator", 0
msgInvalidTokenType: .data $+1, "Invalid token type", 0
msgDivZeroError: .data $+1, "\r\nDivision by zero\r\n", 0
msgCtrlC: .data $+1, "\r\n\r\nHalt by ^C\r\n", 0
;-----------------------------------------------------------------------------
; INCLUDE FILES
;-----------------------------------------------------------------------------
;
; Standard SUBLEQ macros and functions
;
.include IO.inc
.include STRING.inc
.include ATOI.inc
.include RANDOM.inc
.include CONSTANTS.inc
;
; Subroutines for the BASIC commands
;
.include cmdEND.inc
.include cmdINPUT.inc
.include cmdREM.inc
.include cmdPRINT.inc
.include cmdGOTO.inc
.include cmdIF.inc
.include cmdLET.inc
.include cmdGOSUB.inc
.include cmdRETURN.inc
.include cmdFOR.inc
.include cmdNEXT.inc
.include cmdLIST.inc
.include cmdNEW.inc
.include cmdDIM.inc
;
; Subroutines for the BASIC utilities
;
.include EDITOR.inc
.include KEYBOARD.inc
.include FINDLINE.inc
.include PARSEEXPRESSION.inc
.include GETTOKEN.inc
.include VARIABLES.inc
.INCLUDE BITOPS.inc
.include debug.inc
;-----------------------------------------------------------------------------
; BASIC PROGRAM SOURCE CODE
;-----------------------------------------------------------------------------
program: ; The program source code to be run
L10: .data 10, L20-$-1, "DIM ARR(10)" ,0
L20: .data 20, L30-$-1, "LET ARR(5)=17" ,0
L30: .data 30, L40-$-1, "FOR I=0 to 10" ,0
L40: .data 40, L50-$-1, "PRINT I;\" \";ARR(I)" ,0
L50: .data 50, L60-$-1, "NEXT" ,0
L60: .data 60, Lend-$-1, "END" ,0
Lend:.data 0
; .include macrotests/testcasegen/multest.inc
; .include macrotests/testcasegen/divtest.inc
.org $+16384 ; Reserve 16K for the BASIC Source code
;-----------------------------------------------------------------------------
; The stack should end up at the absolute top
;-----------------------------------------------------------------------------
.include STACK.inc