summaryrefslogtreecommitdiff
path: root/test/input.s
blob: a8b0830a449a33ef1f895c895b337f9cdae50ebd (plain)
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
; Testing input.
;
; Writen in Sux assembly by
; mr b0nk 500 <b0nk@b0nk.xyz>


; Input buffer.
.org $4000
buffer:

; Initalize some variables.
.org $0
scr_row:
	.byte $0
scr_col:
	.byte $0
a:
	.byte $0
b:
	.byte $0
c:
	.byte $0
d:
	.byte $0
e:
	.byte $0
string:
	.byte "Please, type something.\n"
string2:
	.byte "You typed, "
end:
	.byte $0

; String Pointer
ptr:
	.qword buffer

; Main program
.org $8000
reset:
	cps
	ldx.w #$FFFF
	txs
	ldy #0
	lda #0
clr_buf:
	cpy.w #$FFF
	beq start
	sta (ptr), y
	iny
	jmp clr_buf

start:
	tax		; Reset x.
	tay		; Reset y.
	jmp print

rset_a:
	lda #1

read:
	lda $C000	; Get control register.
	beq rset_a	; Loop until we get a character.
	lda #0
	sta $C000
	jmp getchar	; We got a key.
sleep:
	lda end
	bne spin	; Are we done with getting input?

print:
	lda string, x	; Get character at offset x.
	beq rset_a	; Did we find a null terminator?
	sta $C001	; Print character.
	inx		; Increment offset.
	cmp #$A
	beq inc_row
	inc scr_col
	jmp print
inc_row:
	inc scr_row
	lda #0
	sta scr_col
	jmp print	; Keep printing more characters.

getchar:
	lda $C002	; Get typed character.
	cmp #$1B
	beq esc
	cmp #$A
	beq nl		; Did the user type a newline?
	cmp #8
	beq bs		; Did the user type a backspace?
	cmp #$7F
	beq bs		; Did the user type a backspace?
echo:
	sta a
	ldx scr_col
	cpx #79
	bne echo_print
linewrap:
	inc scr_row
	ldx #0
	stx scr_col
	lda scr_row
	jsl update_pos
echo_print:
	lda a
	sta $C001	; Echo typed character.
	inc scr_col	; Increment the cursor's x coordinate.
	sta (ptr), y	; Store typed character into the input buffer.
	iny
	jmp rset_a	; Start getting user input.

esc:
	lda $C000	; Skip the '['.
	lda $C000	; Get the next character.
	beq read	; We have an error, so discard it, and go back to getting user input.
	lda $C002	; Get the escape code.
	sta c		; Store the escape code, until we need it.
	jsl isup	; Check if the user pressed up.
	lda d
	cmp #0
	bne esc_end
	jsl isdown	; Check if the user pressed down.
	lda d
	cmp #0
	bne esc_end
	lda #0
	jsl isleft	; Check if the user pressed left.
	lda d
	cmp #0
	bne esc_end
	jsl isright	; Check if the user pressed right.
esc_end:
	lda #0
	sta d
	jmp rset_a	; Go back to getting user input.

isup:
	lda scr_row	; Is the cursor at the top of the screen?
	beq isup_done	; Yes, so return.
	lda c		; No, so load the escape code back into the accumulator.
	cmp #$41	; Did the user press the up arrow key?
	beq up		; Yes, so move the cursor up.
isup_done:
	rtl		; End of isup.

isdown:
	lda scr_row	; Start checking the y coordinate of the cursor.
	cmp #23		; Is the cursor at the bottom of the screen?
	beq isdown_done	; Yes, so return.
	lda c		; No, so load the escape code back into the accumulator.
	cmp #$42	; Did the user press the down arrow key?
	beq down	; Yes, so move the cursor down.
isdown_done:
	rtl		; End of isdown.

isright:
	lda scr_col	; Start checking the x coordinate of the cursor.
	cmp #79		; Is the cursor at the far right of the screen?
	beq isright_end	; Yes, so return.
	lda c		; No, so load the escape code back into the accumulator.
	cmp #$43	; Did the user press the right arrow key?
	beq right	; Yes, so move the cursor right.
isright_end:
	rtl		; End of isright.

isleft:
	lda scr_col	; Is the cursor at the far left of the screen?
	beq isleft_done	; Yes, so return.
	lda c		; No, so load the escape code back into the accumulator.
	cmp #$44	; Did the user press the left arrow key?
	beq left	; Yes, so move the cursor left.
isleft_done:
	rtl		; End of isleft.

up:
	dec scr_row
	jsl update_pos
	lda #1
	sta d
	jmp isup_done
down:
	inc scr_row
	jsl update_pos
	lda #1
	sta d
	jmp isdown_done
right:
	inc scr_col
	jsl update_pos
	jmp isright_end
left:
	dec scr_col
	jsl update_pos
	lda #1
	sta d
	jmp isleft_done

update_pos:
	lda #$1B	; Print an escape character
	sta $C001	; to the screen.
	lda #$5B	; Print '['
	sta $C001	; to the screen, and start the escape sequence.
	jsl getrow	; Start printing the row number to the screen.
	jsl getcol	; Start printing the column number to the screen.
	lda #$48	; Print 'H'
	sta $C001	; to the screen.
	rtl		; End of update_pos.
getrow:
	lda scr_row	; Get the cursor's y coordinate.
	div #10		; Divide A by 10.
	adc #$30	; Convert it to ascii, and
	sta $C001	; print to the screen.
	tba		; Get the remainder.
	adc #$30	; Convert it to ascii, and
	sta $C001	; print to the screen.
	rtl		; End of getrow.
getcol:
	lda #$3B	; Print ';'
	sta $C001	; to the screen.
	lda scr_col	; Get the cursor's x coordinate.
	div #10		; Divide A by 10.
	adc #$30	; Convert it to ascii, and
	sta $C001	; print to the screen.
	tba		; Get the remainder.
	adc #$30	; Convert it to ascii, and
	sta $C001	; print to the screen.
	rtl		; End of getrow.


nl:
	lda #$A
	sta $C001
	inc scr_row
	lda #0		; Replace newline with a null terminator.
	sta (ptr), y	; Store said terminator into the input buffer.
	sta scr_col
	ldy.w #0	; Reset y, to print the result.
	jmp result

back:
	sta $C001	; Print backspace.
	lda #0		; Put a null terminator, in place of the backspace.
	sta (ptr), y	; Place it into the input buffer.
	dey		; Decrement buffer offset.
	dec scr_col
	jmp read	; Get next character.

bs:
	cpy #0		; Are we at the start of the buffer?
	beq rset_a	; We are, so do not store the backspace into the buffer.
	jmp back	; We are not, so add the backspace to the buffer.

result:
	ldx scr_col
	cpx #79
	bne result_print
linewrap2:
	inc scr_row
	ldx #$0
	stx scr_col
	jsl update_pos
result_print:
	lda string2, y
	beq rset_y	; Reset y, if we hit the null terminator.
	sta $C001	; Print 'You have typed, '
	inc scr_col	; Increment the cursor's x coordinate.
	iny		; Increment offset.
	jmp result	; Keep printing.


rset_y:
	ldy.w #0	; Reset y.
	jmp print_buf	; Print the input buffer.

print_buf:
	ldx scr_col
	cpx #79
	bne buf_print
linewrap3:
	inc scr_row
	ldx #0
	stx scr_col
	jsl update_pos
buf_print:
	lda (ptr), y	; Get a character from the input buffer.
	beq spin	; Are we done with printing the buffer?
	sta $C001	; Print said character.
	inc scr_col	; Increment the cursor's x coordinate.
	iny
	jmp print_buf	; Keep printing the buffer.

scr_to_buf:
	tax
	mul #80
	adc scr_col
	tay
	txa
	rtl

spin:
	nop
	nop
	nop
	jmp spin

.org $1000
v
.org $1100
v
.org $1200
v
.org $8000
v
.org $8100
v
.org $8200
v
.org $8300
v
.org $FFC0
.qword reset

.org $FF50
.qword spin
.qword spin
.qword spin
.qword spin
.qword spin
.qword spin
.qword spin
.org $FF50
v
;done
q