-
Notifications
You must be signed in to change notification settings - Fork 3
/
text-io.dasm
261 lines (246 loc) · 6.11 KB
/
text-io.dasm
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
;; Buffered Text I/O Primitives
;; by Kang Seonghoon (lifthrasiir, @senokay)
;; licensed under WTFPL. feel free to use.
;;
;; declares: newline, scroll, clear, putc, rawputc,
;; getc, fillbuffer, getline
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; base screen routines
:screenoff
; keeps an offset to the current cursor.
; ranges from 0 to 0x17f; 0x180 causes the screen
; to scroll.
DAT 0
; moves the cursor to the first letter of next
; line. automatically scrolls the screen.
;
; clobbers: C
:newline
SET C, [screenoff]
AND C, 0xffe0
ADD C, 0x20
SET [screenoff], C
IFG 0x180, C
SET PC, POP
; *falls through*
; scrolls the screen up by one line.
;
; cycles: <= 1703
; clobbers: C
:scroll
SET C, 0x8000
:scroll_copy
SET [C], [C+0x20]
SET [C+1], [C+0x21]
SET [C+2], [C+0x22]
SET [C+3], [C+0x23]
ADD C, 4
IFG 0x8160, C
SET PC, scroll_copy
:scroll_fill
SET [C], 0
SET [C+1], 0
SET [C+2], 0
SET [C+3], 0
ADD C, 4
IFG 0x8180, C
SET PC, scroll_fill
SUB [screenoff], 0x20
SET PC, POP
; entirely clears the screen.
;
; cycles: <= 1349
; clobbers: C
:clear
SET C, 0x8000
:clear_loop
SET [C], 0
SET [C+1], 0
SET [C+2], 0
SET [C+3], 0
ADD C, 4
IFG 0x8180, C
SET PC, clear_loop
SET [screenoff], 0
SET PC, POP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; putc, rawputc
;; depends: base screen routines
; writes one character to the current position.
; supports a newline character (when A=10).
; automatically scrolls the screen whenever needed.
;
; receives: A (character to be written)
; clobbers: A, C
:putc
IFE A, 10
SET PC, newline
AND A, 0x7f
BOR A, 0xf000
; *falls through*
; same as putc but does not process newlines.
; also does not strip color and blink bits.
;
; receives: A (word to be written)
; clobbers: A, C
:rawputc
; we need a room for a visible cursor in getc,
; so it is not 0x17f.
IFG [screenoff], 0x17e
JSR scroll
SET C, [screenoff]
SET [C+0x8000], A
ADD C, 1
SET [screenoff], C
SET PC, POP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; getc
;; depends: base screen routines
; reads one character from the line buffer.
; line buffer is automatically filled as needed;
; the visible cursor will appear[*], the screen
; will scroll as needed, and the user can input
; any amount of text until the line buffer fills.
; backspace is supported as well.
;
; [*] currently implemented as XORing the cursor
; position with 0xff00. may behave weirdly if
; you are using a colored text; search for the
; word "cursor" if you want to fix it.
;
; the size of the line buffer is currently 128
; (including a newline character) but easily
; configurable. see lines commented with "BUFSIZE".
; be careful however: BUFSIZE should not exceed 352
; (=11*32) anyway!
;
; clobbers: A, B, C
; returns: A (read character)
:getc
; just consume the line buffer if any
IFG [inputsize], [inputoff]
SET PC, getc_buffered
JSR fillbuffer
:getc_buffered
SET B, inputoff
SET A, [B]
SET A, [A+inputbuf]
ADD [B], 1
SET PC, POP
; discards the line buffer and reads one line up to
; the next newline. for internal uses only.
:fillbuffer
; register uses:
; A = the current buffer size.
; eventually dumped to [inputsize]
; B = the current character
; C = the current keyboard buffer position,
; or the pointer to the current cursor
;
; C is clobbered by scroll and newline, but
; the code does not use C permanently anyway.
SET A, 0
SET C, [screenoff]
; draws the cursor
XOR [C+0x8000], 0xff00
:fillbuffer_wait
SET C, [keyoff]
:fillbuffer_busywait
; 3(!) cycles per iteration
IFE [C], 0
SET PC, fillbuffer_busywait
ADD [keyoff], 1
AND [keyoff], 0x900f
SET B, [C]
SET [C], 0
IFE B, 10
SET PC, fillbuffer_commit
IFE B, 8
SET PC, fillbuffer_bksp
; these things avoid problems with other events
; or punctuations (may have a high bit set)
IFG B, 127
SET PC, fillbuffer_wait
IFG 32, B
SET PC, fillbuffer_wait
IFG A, 126 ; replace 126 with BUFSIZE-2
SET PC, fillbuffer_wait
SET [A+inputbuf], B
SET C, [screenoff]
BOR B, 0xf000
; will erase the cursor as well
SET [C+0x8000], B
; see putc comment
IFG C, 0x17e
JSR scroll
ADD [screenoff], 1
SET C, [screenoff]
; draws the cursor
XOR [C+0x8000], 0xff00
ADD A, 1
SET PC, fillbuffer_wait
:fillbuffer_bksp
IFE A, 0
SET PC, fillbuffer_wait
SUB A, 1
SET C, [screenoff]
; erases the cursor (cannot set to 0 since
; we keep the original screen contents as long
; as the line buffer does not overwrite it...)
XOR [C+0x8000], 0xff00
; the previous position is replaced with
; an empty cursor
SET [C+0x7fff], 0x0f20
SUB [screenoff], 1
SET PC, fillbuffer_wait
:fillbuffer_commit
SET C, [screenoff]
; erases the cursor
XOR [C+0x8000], 0xff00
JSR newline
SET [A+inputbuf], 10
ADD A, 1
SET [inputsize], A
SET [inputoff], 0
SET PC, POP
:keyoff
DAT 0x9000
:inputoff
DAT 0
:inputsize
DAT 0
:inputbuf
; should have BUFSIZE zeroes here
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; getline
;; depends: getc (and base screen routines)
; reads one line (i.e. all remaining characters
; up to the next newline), trims a trailing
; newline and returns the line string.
; the string is valid until next getc or getline.
;
; clobbers: A, B, C
; returns: A (pointer to str), B (string length)
:getline
; fill the buffer first if it is empty
IFG [inputsize], [inputoff]
SET PC, getline_buffered
JSR fillbuffer
:getline_buffered
; now the final character *has* to be a newline.
; ignore it and return the remainder.
SET B, [inputsize]
SET A, [inputoff]
SET [inputsize], A
SUB B, A
SUB B, 1
ADD A, inputbuf
SET PC, POP