-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.asm
290 lines (247 loc) · 5.05 KB
/
util.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
;******************************************************************************
; Function name.......: util_print_str
; Purpose.............: Prints a null terminated string
; Input...............: X = Pointer to string, address low
; Y = Pointer to string, address high
; Returns.............: Nothing
.proc util_print_str
; Store input
stx tmp1
sty tmp1+1
sta align
; Get screen width
sec
jsr KERNAL_SCREEN_MODE
stx screen_width
begin_line:
ldy #0
stz breakat
stz controlchars
search:
lda (tmp1),y
beq setend
cmp #13
beq setend
cmp #32
bne :+
; Remember index of last blank space
sty breakat
: ; Check if control char
lda (tmp1),y
cmp #COL_DEFAULT
beq :+
cmp #COL_BG
beq :+
cmp #COL_WARN
beq :+
cmp #COL_OK
beq :+
cmp #COL_ERR
beq :+
cmp #COL_SWAP
beq :+
cmp #SCR_CLS
beq :+
cmp #SCR_PETSCII
beq :+
cmp #SCR_LOWER
beq :+
bra :++
: inc controlchars
; Check if at end of line
: iny
sec
tya
sbc controlchars
cmp screen_width
bcs output
bra search
setend:
sty breakat
output:
ldy #0
: lda (tmp1),y
beq exit
jsr KERNAL_CHROUT
cmp #13
beq advance
cpy breakat
beq breakhere
iny
bra :-
breakhere:
sec
tya
ina
sbc controlchars
cmp screen_width
bcs advance
lda #13
jsr KERNAL_CHROUT
advance:
clc
tya
ina
adc tmp1
sta tmp1
lda tmp1+1
adc #0
sta tmp1+1
jmp begin_line
exit:
rts
breakat: .res 1
controlchars: .res 1
screen_width: .res 1
align: .res 1
.endproc
;******************************************************************************
; Function name.......: util_print_num
; Purpose.............: Prints a byte value as decimal number
; Input...............: A = byte value
; Returns.............: Nothing
.proc util_print_num
; Store 8 bit input
sta input
; Clear 16 bit output
stz output
stz output+1
; Number of input bits
ldx #8
; Set decimal mode
sed
loop:
; Rotate input, leftmost bit -> C
asl input
; 16 bit addition. Value of C from previous operation is the actual input. Therefore C is not cleared.
lda output
adc output
sta output
lda output+1
adc output+1
sta output+1
; Decrease bit counter, continue if > 0
dex
bne loop
; Go back to binary mode
cld
; Print
ldy #0
lda output+1 ; Third digit from right
and #15
beq :+
clc
adc #48
jsr KERNAL_CHROUT
iny
: lda output ; Second digit from right
lsr
lsr
lsr
lsr
cpy #0
bne :+
cmp #0
beq :++
: clc
adc #48
jsr KERNAL_CHROUT
iny
: lda output ; First digit from right
and #15
clc
adc #48
jmp KERNAL_CHROUT
input: .res 1
output: .res 2
.endproc
;******************************************************************************
; Function name.......: util_input
; Purpose.............: Read string from keyboard
; Input...............: Nothing
; Returns.............: Y = string len
;
.proc util_input
ldy #0
: jsr KERNAL_CHRIN
cmp #13
beq exit
sta util_input_buf,y
iny
bra :-
exit:
rts
.endproc
;******************************************************************************
; Function name.......: util_stepdown
; Purpose.............:
; Input...............: X = Start value, or 0 if no value
; Returns.............: X = Current value
.proc util_stepdown
; Set start value if X != 0
cpx #0
beq delete
inx
stx value
bra printval
delete:
ldx value
lda #20
jsr KERNAL_CHROUT
cpx #10
bcc printval
lda #20
jsr KERNAL_CHROUT
cpx #100
bcc printval
lda #20
jsr KERNAL_CHROUT
printval:
lda value
beq exit
dea
sta value
jsr util_print_num
exit:
ldx value
rts
value: .res 1
.endproc
;******************************************************************************
; Function name.......: util_countdown
; Purpose.............:
; Input...............: X = Countdown start value
; Returns.............: Nothing
.proc util_countdown
jsr util_stepdown
cpx #0
beq exit
jsr util_delay
ldx #0
bra util_countdown
exit:
rts
.endproc
;******************************************************************************
;Function name.......: util_delay
;Purpose.............: Delay approx one second
;Input...............: Nothing
;Returns.............: Nothing
.proc util_delay
lda #168
sta counter
lda #70
sta counter+1
lda #14
sta counter+2
loop:
dec counter ; 6
bne loop ; 3 9x
dec counter+1 ; 6
bne loop ; 3 9x / 256
dec counter+2 ; 6
bne loop ; 3 9x / 65535
rts ; => 9x + 9x/256 + 9x/65536 = 8000000 => 65536x + 256x + x = 8000000/9*65536 => x = 885416
counter: .res 3
.endproc
util_input_buf: .res 256