-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutility.s
235 lines (200 loc) · 3.95 KB
/
utility.s
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
;
; utility.s
; General utilities for 6502
;
; Created by Quinn Dunki on 8/15/14.
; Copyright (c) 2014 One Girl, One Laptop Productions. All rights reserved.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; delayShort
; Sleeps for ~1/30th second
;
delayShort:
SAVE_AXY
ldy #$06 ; Loop a bit
delayShortOuter:
ldx #$ff
delayShortInner:
nop
nop
nop
nop
nop
nop
nop
dex
bne delayShortInner
dey
bne delayShortOuter
RESTORE_AXY
rts
.if 0
;;;;;;;;;;;;;;;;;;;;;;;
; scanHexDigit
; Scans a 4 bit hex value from an ASCII character
; A: ASCII character
; Out A: Hex value
;
scanHexDigit:
cmp #'a'
bcs scanHexDigitLowCase
cmp #'A'
bcs scanHexDigitLetter
sec
sbc #'0'
jmp scanHexDigitDone
scanHexDigitLowCase:
sec
sbc #32
scanHexDigitLetter:
sec
sbc #55
scanHexDigitDone:
rts
;;;;;;;;;;;;;;;;;;;;;;;
; scanHex8
; Scans an 8 bit hex value from a string
; PARAM0: Pointer to string (LSB)
; PARAM1: Pointer to string (MSB)
; Y: Offset into string
; Out A: 8-bit hex value
; Y: One past what we scanned
; Side effects: Clobbers S0
;
scanHex8:
lda (PARAM0),y
jsr scanHexDigit
asl
asl
asl
asl
sta SCRATCH0 ; Stash first digit for later
iny
lda (PARAM0),y
jsr scanHexDigit
ora SCRATCH0
iny ; Be nice and advance Y to end
rts
.endif
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; WGStrLen
; Finds the length of a null-terminated string
; PARAM0: String pointer, LSB
; PARAM1: String pointer, MSB
; Return: A: String length, not including null
;
WGStrLen:
phy
ldy #$ff
WGStrLen_loop:
iny
lda (PARAM0),y
bne WGStrLen_loop
WGStrLen_done:
tya
ply
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; WGAllocStr
; Finds room in our block string allocator
; Return: PARAM0: Stored string, LSB
; PARAM1: Stored string, MSB
; Null if pool is full
; X: Pool index of free space
; Side Effects: Clobbers AX
;
WGAllocStr:
ldx #0
WGAllocStr_findEmptyLoop:
lda WG_STRINGS,x
beq WGAllocStr_done
txa
clc
adc #16 ; String blocks are 16 bytes wide
bcs WGAllocStr_noRoom
tax
bra WGAllocStr_findEmptyLoop
WGAllocStr_noRoom:
lda #0
sta PARAM0
sta PARAM1
WGAllocStr_done:
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; WGStoreStr
; Finds room in our block allocator and copies the given string.
; A: Terminator character
; PARAM0: String pointer, LSB
; PARAM1: String pointer, MSB
; Return: PARAM0: Stored string, LSB
; PARAM1: Stored string, MSB
; Side Effects: Clobbers SA
;
WGStoreStr:
sta WG_SCRATCHA
SAVE_AXY
jsr WGAllocStr
lda PARAM1
beq WGStoreStr_done
ldy #0
phx ; Remember the start of our string
WGStoreStr_copyLoop:
lda (PARAM0),y
cmp WG_SCRATCHA
beq WGStoreStr_terminate
sta WG_STRINGS,x
inx
iny
cpy #15 ; Clip string to maximum block size
bne WGStoreStr_copyLoop
WGStoreStr_terminate:
stz WG_STRINGS,x ; Terminate the stored string
pla ; Return pointer to the start of the block
clc
adc #<WG_STRINGS
sta PARAM0
lda #0
adc #>WG_STRINGS
sta PARAM1
WGStoreStr_done:
RESTORE_AXY
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; WGStorePascalStr
; Finds room in our block allocator and copies the given string.
; X: Length
; PARAM0: String pointer, LSB
; PARAM1: String pointer, MSB
; Return: PARAM0: Stored string, LSB
; PARAM1: Stored string, MSB
; Side Effects: Clobbers SA
;
WGStorePascalStr:
stx WG_SCRATCHA
SAVE_AXY
jsr WGAllocStr
lda PARAM1
beq WGStorePascalStr_done
ldy #0
phx ; Remember the start of our string
WGStorePascalStr_copyLoop:
cpy WG_SCRATCHA
beq WGStorePascalStr_terminate
lda (PARAM0),y
sta WG_STRINGS,x
inx
iny
cpy #15 ; Clip string to maximum block size
bne WGStorePascalStr_copyLoop
WGStorePascalStr_terminate:
stz WG_STRINGS,x ; Terminate the stored string
pla ; Return pointer to the start of the block
clc
adc #<WG_STRINGS
sta PARAM0
lda #0
adc #>WG_STRINGS
sta PARAM1
WGStorePascalStr_done:
RESTORE_AXY
rts