summaryrefslogtreecommitdiff
path: root/programs/subasm.s
blob: 46effcedb5b25e7f06ffc1f83361dcf76692d2d8 (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
; SuBAsm
; The Sux Bootstrapped Assembler.
;
; by mr b0nk 500 <b0nk@b0nk.xyz>

; Variables
.org $1000
prg_name:
	.byte "SuBAsm"
ver_txt:
	.byte ", version "
ver_num:
	.byte "0.1"
x:
	.word $0
y:
	.word $0

str_buf:

; Input buffer.
.org $2000
buf:

; Control Register.
.org $C000
ctrl_reg:

; Screen.
.org $C001
scr:

; Keyboard.
.org $C002
kbd:

; Main program.
.org $0
reset:
	cps
	ldx.w #$FFFF
	txs
	ldy #$0
	jsr clr_buf
	ldx.w #$0	; Reset x.
	ldy #$0		; Reset y.
	jmp print_title

read:
	lda ctrl_reg	; Is the keyboard ready?
	beq read	; Loop until the keyboard is ready.
	lda #$0		; Start resetting the control register.
	sta ctrl_reg	; Reset the control register.
	jmp getchar	; We got a key.

rset_x:

	ldx #$0		; Reset x.
	stx.w x
	rts

print_title:
	lda prg_name, x
	beq print_ver	; Did we find a null terminator?
	sta		; Print character.
	inx		; Increment offset.
	inc x
	jmp print_title	; Keep printing more characters.

print_ver
	jsr rset_x
	lda ver_txt, x
	beq print_num	; Did we find a null terminator?
	sta scr		; Print character.
	inx		; Increment offset.
	jmp print_ver	; Keep printing more characters.

print_num:
	lda ver_num, x
	beq getline	; Did we find a null terminator?
	sta scr		; Print character.
	inx		; Increment offset.
	jmp print_num	; Keep printing more characters.
getline:
	lda #$A
	sta scr		; Print newline
	inc scr_row
	lda #$0
	sta scr_col
	inc y
	jsr rset_x
	jmp read

getchar:
	lda kbd		; Get typed character.
	cmp #$1B	; Did the user type an escape?
	beq esc		; Yes, so start getting the escape code.
	cmp #$A		; Did the user type a newline?
	beq nl		; Yes, so start parsing the input.
	cmp #$8		; Did the user type a backspace?
	beq bs		; Yes, so start checking the buffer.
	jsr echo	; Print character to screen.

store_char:
	sta buf, y	; Store typed character into the input buffer.
	iny		; Increment buffer offset.
	jmp read	; Get another character.

esc:
	lda ctrl_reg	; Skip the '['.
	lda ctrl_reg	; Get the next character.
	beq read	; We have an error, so discard it, and read the next character.
	lda kbd		; 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 left arrow?
	beq left	; Yes, so move the cursor left.
	cmp #$44	; Did the user press the right arrow?
	beq right	; Yes, so move the cursor right.

up:
	lda #$1B
	sta scr
	lda #$5B
	sta scr
	lda #$41
	sta scr
	jmp read
down:
	lda #$1B
	sta scr
	lda #$5B
	sta scr
	lda #$42
	sta scr
	jmp read
left:
	lda #$1B
	sta scr
	lda #$5B
	sta scr
	lda #$43
	sta scr
	jmp read
right:
	lda #$1B
	sta scr
	lda #$5B
	sta scr
	lda #$44
	sta scr
	jmp read



nl:
	sta scr		; Print newline.
	lda #$0		; Put a null terminator, in place of the newline.
	sta buf, y	; Place it into the input buffer.
	ldy.w #$0	; Reset y, to parse the input.
	jmp parse

back:
	jsr echo	; Print backspace.
	lda #$0		; Put a null terminator, in place of the backspace.
	sta buf, 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 read	; We are, so do not store the backspace into the buffer.
	jmp back	; We are not, so add the backspace to the buffer.

parse:
	lda buf, y
	beq clr_buf	; Reset y, if we hit the null terminator.
	sta scr		; 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 buf, y	; Get a character from the input buffer.
	beq fin		; Are we done with printing the buffer?
	sta scr		; Print said character.
	iny
	jmp print_buf	; Keep printing the buffer.

spin:
	nop
	nop
	nop
	jmp spin

clr_buf:
	lda #$0
	cpy.w #$1000
	beq clr_end
	sta buf, y
	iny
	jmp clr_buf
clr_sbuf:
	lda #$0
	cpy.w #$40
	beq clr_end
	sta str_buf, y
	iny
	jmp clr_sbuf

clr_end:
	rts

echo:
	sta scr		; Echo typed character.
	rts		; Return.

.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 $0
;viewmem
;.org $100
;viewmem
;q
done