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


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

; Input buffer.
.org $2000
buffer:

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

start:
	ldx.w #$0	; Reset x.
	ldy #$0		; Reset y.
	jmp print

rset_a:
	lda #$10
delay:
	beq read
	dec
	jmp delay
sleep:
	lda end
	bne spin	; Are we done with getting input?
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.

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.
	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 $C001	; Echo typed character.
	sta buffer, y	; Store typed character into the input buffer.
	iny		; Increment buffer offset.
	jmp rset_a	; We are not, so add the backspace to the buffer.

esc:
	lda $C000	; Skip the '['.
	lda $C000	; Get the next character.
	beq read	; We have an error, so discard it, and read the next character.
	lda $C002	; Get the escape code.
	cmp #$41	; Did the user press the up arrow?
	beq up		; Yes, so move the cursor up.
	cmp #$42	; Did the user press the down arrow?
	beq down	; Yes, so move the cursor down.
	cmp #$43	; Did the user press the right arrow?
	beq right	; Yes, so move the cursor right.
	cmp #$44	; Did the user press the left arrow?
	beq left	; Yes, so move the cursor left.
up:
	lda scr_row
	beq rset_a
	dec scr_row
	jsr update_pos
	lda #$1B
	sta $C001
	lda #$5B
	sta $C001
	lda #$41
	sta $C001
	jmp rset_a
down:
	inc scr_row
	jsr update_pos
	lda #$1B
	sta $C001
	lda #$5B
	sta $C001
	lda #$42
	sta $C001
	jmp rset_a
right:
	lda scr_col
	cmp #$50
	beq rset_a
	inc scr_col
	jsr update_pos
	lda #$1B
	sta $C001
	lda #$5B
	sta $C001
	lda #$43
	sta $C001
	jmp rset_a
left:
	lda scr_col
	beq rset_a
	dec scr_col
	jsr update_pos
	lda #$1B
	sta $C001
	lda #$5B
	sta $C001
	lda #$44
	sta $C001
	jmp rset_a

update_pos:
	lda #$1B
	sta $C001
	lda #$5B
	sta $C001
	lda scr_row
	jsr uint_to_bcd
	jmp spin

uint_to_bcd:
	div #$A
	lsl #$4
	sta a
	tba
	sta b
	lda a
	ora b
	rts

nl:
	lda #$A
	sta $C001
	inc scr_row
	jsr update_pos
	lda #$0		; Replace newline with a null terminator.
	sta buffer, y	; Store said terminator into the input buffer.
	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 buffer, y	; Place it into the input buffer.
	dey		; Decrement buffer offset.
	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:
	lda string2, y
	beq rset_y	; Reset y, if we hit the null terminator.
	sta $C001	; Print 'You have typed, '
	iny		; Increment offset.
	jmp result	; Keep printing.


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

print_buf:
	lda buffer, y	; Get a character from the input buffer.
	beq spin	; Are we done with printing the buffer?
	sta $C001	; Print said character.
	iny
	jmp print_buf	; Keep printing the buffer.

spin:
	nop
	nop
	nop
	jmp spin

.org $FFC0
.qword reset

.org $FF50
.qword spin
.qword spin
.qword spin
.qword spin
.qword spin
.qword spin
.qword spin

.org $FFA0
.qword irq_routine
.org $8000
viewmem
.org $8100
viewmem
;q
done