forked from jkotlinski/durexforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.asm
302 lines (278 loc) · 5.22 KB
/
io.asm
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
;{{{ The MIT License
;
;Copyright (c) 2008 Johan Kotlinski
;
;Permission is hereby granted, free of charge, to any person obtaining a copy
;of this software and associated documentation files (the "Software"), to deal
;in the Software without restriction, including without limitation the rights
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;copies of the Software, and to permit persons to whom the Software is
;furnished to do so, subject to the following conditions:
;
;The above copyright notice and this permission notice shall be included in
;all copies or substantial portions of the Software.
;
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;THE SOFTWARE. }}}
; TYPE EMIT PAGE KEY? KEY REFILL SOURCE SOURCE-ID >IN GETC CHAR
+BACKLINK
!byte 4
!text "emit"
EMIT
lda LSB, x
inx
jmp PUTCHR
+BACKLINK
!byte 4
!text "page"
PAGE
lda #K_CLRSCR
jmp PUTCHR
+BACKLINK
!byte 4
!text "type"
TYPE ; ( caddr u -- )
lda #0 ; quote mode off
sta $d4
- lda LSB,x
ora MSB,x
bne +
inx
inx
rts
+ jsr OVER
jsr FETCHBYTE
jsr EMIT
jsr ONE
jsr SLASH_STRING
jmp -
+BACKLINK
!byte 4
!text "key?"
lda $c6 ; Number of characters in keyboard buffer
beq +
.pushtrue
lda #$ff
+ tay
jmp pushya
+BACKLINK
!byte 3
!text "key"
- lda $c6
beq -
stx W
jsr $e5b4 ; Get character from keyboard buffer
ldx W
ldy #0
jmp pushya
+BACKLINK
!byte 6
!text "refill" ; ( -- )
REFILL
READ_EOF = * + 1
lda #0
beq +
; handle EOF
stx W
lda SOURCE_ID_LSB
jsr CLOSE
dec SOURCE_ID_LSB
ldx SOURCE_ID_LSB
jsr CHKIN
ldx W
jmp RESTORE_INPUT ; exit
+
lda SOURCE_ID_MSB
beq +
jsr evaluate_consume_tib
jmp evaluate_get_new_line
+
ldy #0
sty TO_IN_W
sty TO_IN_W + 1
sty TIB_SIZE
sty TIB_SIZE + 1
lda SOURCE_ID_LSB
beq .getLineFromConsole
lda SOURCE_ID_MSB
beq .getLineFromDisk
.getLineFromConsole
stx W
ldx #0
- jsr $e112 ; Input Character
cmp #$d
beq .gotReturn
sta TIB,x
cpx #$58 ; Default TIB area is $200-$258
beq -
inx
jmp -
.gotReturn
jsr PUTCHR
; Set TIB_SIZE to number of chars fetched.
stx TIB_SIZE
ldx W
rts
.getLineFromDisk
lda TIB_PTR
sta W
lda TIB_PTR + 1
sta W+1
- stx W2
jsr CHRIN
ldx W2
pha
jsr READST
sta READ_EOF
pla
ora #0
beq -
cmp #K_RETURN
beq +
inc $d020
ldy TIB_SIZE
sta (W),y
inc TIB_SIZE
dec $d020
jmp -
+ rts
GET_CHAR_FROM_TIB
lda TO_IN_W
cmp TIB_SIZE
bne +
lda TO_IN_W + 1
cmp TIB_SIZE + 1
bne +
lda #0
rts
+
clc
lda TIB_PTR
adc TO_IN_W
sta W
lda TIB_PTR + 1
adc TO_IN_W + 1
sta W + 1
ldy #0
lda (W),y
inc TO_IN_W
bne +
inc TO_IN_W + 1
+ rts
+BACKLINK
!byte 6
!text "source"
SOURCE
dex
dex
lda TIB_PTR
sta LSB+1, x
lda TIB_PTR + 1
sta MSB+1, x
lda TIB_SIZE
sta LSB, x
lda TIB_SIZE + 1
sta MSB, x
rts
TIB_PTR
!word 0
TIB_SIZE
!word 0
+BACKLINK
!byte 9
!text "source-id"
SOURCE_ID_LSB = * + 1
SOURCE_ID_MSB = * + 3
; -1 : string (via evaluate)
; 0 : keyboard
; 1+ : file id
+VALUE 0
+BACKLINK
!byte 3
!text ">in"
TO_IN
+VALUE TO_IN_W
TO_IN_W
!word 0
+BACKLINK
!byte 4
!text "getc"
jsr GET_CHAR_FROM_TIB
bne +
jsr REFILL
lda #K_RETURN
+ ldy #0
jmp pushya
+BACKLINK
!byte 4
!text "char"
CHAR ; ( name -- char )
- jsr PARSE_NAME
lda LSB,x
bne +
inx
inx
jsr REFILL
jmp -
+ inx
jmp FETCHBYTE
SAVE_INPUT_STACK
!fill 9*4
SAVE_INPUT_STACK_DEPTH
!byte 0
push_input_stack
ldy SAVE_INPUT_STACK_DEPTH
sta SAVE_INPUT_STACK, y
inc SAVE_INPUT_STACK_DEPTH
rts
pop_input_stack
dec SAVE_INPUT_STACK_DEPTH
ldy SAVE_INPUT_STACK_DEPTH
lda SAVE_INPUT_STACK, y
rts
SAVE_INPUT
lda READ_EOF
jsr push_input_stack
lda #0
sta READ_EOF
lda TO_IN_W
jsr push_input_stack
lda TO_IN_W+1
jsr push_input_stack
lda SOURCE_ID_LSB
jsr push_input_stack
lda SOURCE_ID_MSB
jsr push_input_stack
lda TIB_PTR
jsr push_input_stack
lda TIB_PTR+1
jsr push_input_stack
lda TIB_SIZE
jsr push_input_stack
lda TIB_SIZE+1
jmp push_input_stack
RESTORE_INPUT
jsr pop_input_stack
sta TIB_SIZE+1
jsr pop_input_stack
sta TIB_SIZE
jsr pop_input_stack
sta TIB_PTR+1
jsr pop_input_stack
sta TIB_PTR
jsr pop_input_stack
sta SOURCE_ID_MSB
jsr pop_input_stack
sta SOURCE_ID_LSB
jsr pop_input_stack
sta TO_IN_W+1
jsr pop_input_stack
sta TO_IN_W
jsr pop_input_stack
sta READ_EOF
rts