-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisp03.a
496 lines (459 loc) · 11.2 KB
/
lisp03.a
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
; Filename: LISP03 - Warm start, supervisor and stack
; ******************************
; Routine to give optionsl warm
; start (ie OBLIST retained)
; ******************************
WRMCHK
HIWARM = WRMCHK+HILISP-LISVAL ; XXX moved here to after WRMCHK defined
lda STATYP
cmp #WARMST
bne NOTWRM
jmp WARM ; Offer warm start
NOTWRM
lda TUBE
bne COLD1
lda #WARMST ; We're in tube and
sta STATYP ; it's first time through
jmp INUREL
COLD1
jmp COLD
WARM
lda MEMINV
bne REBOOT
ldx #WRMOFF
jsr MESSAH
jsr OSRDCH ; Read a character from the current input stream
and #$7f
cmp #'C'
beq REBOOT
cmp #'c'
beq REBOOT
lda #'W'
jsr OSWRCH ; Write character 87
jmp INIT
; **** Reload from ROM
REBOOT
lda #'C'
jsr OSWRCH ; Write character 67
jsr CROUT
jsr CROUT
ldx #<(LISTR)
ldy #>(LISTR)
jmp OSCLI
; ******************************
; This is the main LISP
; supervisor loop which is
; entered when LISP is called.
; ******************************
SUPER
lda #0 ; Reset stack
sta SP
lda ARETOP
sta SP+1
ldx #$ff ; Reset hardware stack
txs
lda MODEF ; Check mode flag
bpl EVPR
jsr MODCHN ; Change mode
EVPR
ldx #EVOFF
jsr MESSAH ; Evaluate:
jsr RSREAD ; Read expression
jsr EVALU ; Evaluate it
ldx #VALOFF ; Value is:
jsr MESSAH
jsr PRINA ; and print it
jmp SUPER ; Repeat ad infinitum
; ******************************
; Here are some condition
; routines taking advantage
; of POP
; ******************************
; **** ATOM
ATOM
lda ARGA+1 ; Is ARGA atom?
beq YES ; NIL => yes
ldy #0
lda (ARGA),y ; check bit 7
bpl YES
bmi NO
; **** EQ
EQ
lda ARGB+1 ; ARGA = ARGB?
beq NULL ; ARGB NIL => ARGA NIL
cmp ARGA+1
bne EQUATE ; But they are numeric
lda ARGB
cmp ARGA
beq YES
EQUATE
ldy #0 ; Fudge for nums
lda (ARGA),y
cmp #NUMF ; Both must be nums
bne NO
cmp (ARGB),y
bne NO
iny
lda (ARGA),y
cmp (ARGB),y ; Same length?
bne NO
tay
dey
EQUINE
lda (ARGA),y ; Compare value
cmp (ARGB),y
bne NO
dey
bne EQUINE
beq YES
; **** NULL
NULL
lda ARGA+1 ; Is ARGA NIL?
beq YES
NO
lda #0 ; Returns NIL
beq POPA
YES
lda #<s_T ; Returns T
sta ARG
lda TRUVAL
POPA
sta ARG+1
; ******************************
; POP is one of the two LISP
; stack handling routines. It
; restores old binding values
; and WSA, WSB and WSC from the
; stack and POP the stack. ARG
; is unchanged.
; ******************************
POP
ldy #0 ; Get binding size
lda (SP),y
beq NOBOUN ; No bindings to do
tay
POPPLE
lda (SP),y
sta RETADD+1 ; Get atom
dey
lda (SP),y
sta RETADD
dey
lda (SP),y ; Get old value
tax ; into A,X
dey
lda (SP),y
dey
sty POPPY ; Save Y
ldy #2 ; Put value back
sta (RETADD),y ; into atom value
iny ; cell
txa
sta (RETADD),y
ldy POPPY ; Fetch Y back
bne POPPLE ; More bindings?
lda (SP),y
clc ; Set stack pointer
adc SP ; to bottom of work-
sta SP ; Space area as if
bcc NOBOUN ; there were no binds
inc SP+1
NOBOUN
iny ; Now Y = 1
lda (SP),y
tay ; Index for top space
dey
lda (SP),y ; Push return addr
pha
dey
lda (SP),y
pha
dey
MORSP
lda (SP),y ; Copy back TVS
sta BINDER,y ; WSA, WSB and WSC
dey
bne MORSP
sec ; Add TVSEXT + 2 to stack
lda SP ; pointer to POP the stack
adc TVSEXT
bcs TVF
adc #1
bcc TVG
clc
TVF
adc #0
inc SP+1
TVG
sta SP
rts
; ******************************
; STACK stores:
; Extent of TVS, WSA, WSB,
; WSC, ARG and return addr
; on the LISP stack
; ******************************
STACK
lda SP
clc
sbc TVSEXT
tax
bne EXTRAM
clc
EXTRAM
dex
lda SP+1
sbc #0
cmp AREVAL+1
bcc SQUAT
bne SROOM
cpx AREVAL
bcs SROOM
SQUAT
jsr RUBBSH
bne STACK
STIR
brk ; None found
!byte 0
!text "No room"
!byte 0
SROOM
sta SP+1
stx SP
pla
sta RETADD
pla
tax
ldy TVSEXT
iny
lda ARG+1 ; Store ARG
sta (SP),y
dey
lda ARG
sta (SP),y
dey
dey
pla ; Store return addr
sta (SP),y
iny
pla
sta (SP),y
dey
dey
PILE
lda BINDER,y
sta (SP),y
dey
bpl PILE
txa
pha
lda RETADD
pha
rts
; ******************************
; BIND adds a 'new value' to the
; bottom of the stack:
; WSD - Pointer to atom
; TVS + X - New value
; ******************************
BIND
lda SP
sec
sbc #4
tay
lda SP+1
sbc #0
cmp AREVAL+1
bcc SQUASH
bne XROOM
cpy AREVAL
bcs XROOM
SQUASH
jsr RUBBSH
bne BIND
BEAR
brk ; None found
!byte 1
!text "No room"
!byte 0
XROOM
sta SP+1
sty SP
ldy #4
lda (SP),y
adc #3 ; Carry is set
pha
lda WSD+1
sta (SP),y
dey
lda WSD
sta (SP),y
lda (WSD),y ; Old value and atom
dey
sta (SP),y ; on stack
lda (WSD),y
dey
sta (SP),y
dey
pla
sta (SP),y ; Bound var size
ldy #2
lda TVS,x ; New value in atom
sta (WSD),y
iny
lda TVS+1,x
sta (WSD),y
rts
; ******************************
; Ensure that a apecified amount
; of space will be available
; without needing to call the GC
; ******************************
RESERV
jsr ALVEC ; allocate it
lda POINT ; then give it back
sta AREVAL
lda POINT+1
sta AREVAL+1
rts
; ******************************
; Here is the space allocator
; routine. If allocates up to
; 256 bytes of initialized store
; ******************************
ALNUM
lda #4
ALVEC
ldx #NUMF
bne SPACE
ALCHAR
ldx #CHARF
beq SPACE
ALFSBR
ldx #FSUBRF
lda #6
bne SPACE
ALSUBR
ldx #SUBRF
lda #6
bne SPACE
ALPAIR
lda #5
ldx #$80 ; Pointer space
SPACE
sta SIZE
SPACEB
clc
lda AREVAL
sta POINT
adc SIZE
tay
lda AREVAL+1
sta POINT+1
adc #0
cmp SP+1
bcc ROOM
bne SQUID
cpy SP
bcc ROOM
SQUID
jsr RUBBSH
bne SPACEB
ALLO
brk ; None found
!byte 2
!text "No room"
!byte 0
ROOM
sta AREVAL+1
sty AREVAL
ldy #0
txa
sta (POINT),y
bmi PINS
bne OBSCUR
ldy #5
lda #0
sta (POINT),y
ldy #2
lda #<s_UNDEFINED ; UNDEFINED
sta (POINT),y
iny
lda ZAVAL
sta (POINT),y
OBSCUR
ldy #1
lda SIZE
sta (POINT),y
rts
PINS
lda #0
ldy #2
sta (POINT),y ; Initial NIL point
ldy #4
sta (POINT),y
rts
; ******************************
; Here are the initialisation
; routines
; ******************************
INIT
lda #<ERROR
sta BRKVEC ; Error handling
lda #>ERROR
sta BRKVEC+1
lda #osbyte_read_himem ; Find end of memory
jsr OSBYTE ; Read top of user memory (HIMEM)
sty ARETOP
; **** Set up various values
lda #$fc ; Messages except GC
sta LEVEL
ldx #0
stx HANDLE ; Output to screen
stx GCNO ; Zero collections
stx GCNO+1
stx ERRCNT ; Zero error count
stx ERRCNT+1
ldy #$10 ; Clear TVS etc.
ZLP
stx TVS,y
dey
bpl ZLP
lda #$0a ; Initially no args
sta TVSEXT
jsr GCTIMZ ; Zero GC time
jsr STCLK ; Zero time
; no longer needed, see note at KBCHK
; lda #<KBD ; Set up Escape check
; sta IODCB
; lda #>KBD
; sta IODCB+1
; lda #0
; sta IODCB+2
; sta IODCB+3
jmp SUPER ; Enter supervisor
; **** Message handler
MESSAH
ldy #0
MESSAI
sty HANDLE
MESSAG
lda TEXT,x ; Print message
php
and #$7f ; Remove flag bit
jsr OUT
inx
plp ; Retrieve flag
bpl MESSAG ; Bit 7 not set
rts
OUT
ldy HANDLE ; to screen?
beq OUTSCR
jmp OSBPUT ; Write a single byte A to an open file Y
OUTSCR
cmp #$0d
bne NCR
CROUT
lda #$0d
NCR
jmp OSASCI ; Print the char; Write character 13