-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.s
189 lines (166 loc) · 5.11 KB
/
utils.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
; -----------------------------------------------------------------------------
; variables
SAVX: .res 1 ; 1 byte temp storage, often to save X register
SAVY: .res 1 ; 1 byte temp storage, often to save Y register
CHRPNT: .res 1 ; current position in input buffer
; -----------------------------------------------------------------------------
; print address
SHOWAD: LDA TMP2
LDX TMP2+1
WRADDR: PHA ; save low byte
TXA ; put high byte in A
JSR WRTWO ; output high byte
PLA ; restore low byte
WRBYTE: JSR WRTWO ; output byte in A
SPACE: LDA #$20 ; output space
BNE FLIP
CHOUT: CMP #$0D ; output char with special handling of CR
BNE FLIP
CRLF:
LDA #$0D ; load CR in A
BIT $13 ; check default channel
BPL FLIP ; if high bit is clear output CR only
JSR CHROUT ; otherwise output CR+LF
LDA #$0A ; output LF
FLIP: JMP CHROUT
.proc FRESH
JSR CRLF ; output CR
LDA #$20 ; load space in A
JSR CHROUT
; JMP SNCLR
RTS
.endproc
; -----------------------------------------------------------------------------
; output two hex digits for byte
.proc WRTWO
STX SAVX ; save X
JSR ASCTWO ; get hex chars for byte in X (lower) and A (upper)
JSR CHROUT ; output upper nybble
TXA ; transfer lower to A
LDX SAVX ; restore X
JMP CHROUT ; output lower nybble
.endproc
; -----------------------------------------------------------------------------
; convert byte in A to hex digits
.proc ASCTWO
PHA ; save byte
JSR ASCII ; do low nybble
TAX ; save in X
PLA ; restore byte
LSR A ; shift upper nybble down
LSR A
LSR A
LSR A
; convert low nybble in A to hex digit
ASCII: AND #$0F ; clear upper nibble
CMP #$0A ; if less than A, skip next step
BCC ASC1
ADC #6 ; skip ascii chars between 9 and A
ASC1: ADC #$30 ; add ascii char 0 to value
RTS
.endproc
; -----------------------------------------------------------------------------
; get prev char from input buffer
GOTCHR: DEC CHRPNT
; get next char from input buffer
.proc GETCHR
STX SAVX
LDX CHRPNT ; get pointer to next char
LDA BUF,X ; load next char in A
BEQ NOCHAR ; null, :, or ? signal end of buffer
CMP #':'
BEQ NOCHAR
CMP #'?'
NOCHAR: PHP
; BEQ @nc1
INC CHRPNT ; next char
@nc1: LDX SAVX
PLP ; Z flag will signal last character
RTS
.endproc
; -----------------------------------------------------------------------------
; skip white space
.proc GETSPC
JSR GETCHR
BEQ done ; end of string
CMP #$20 ; skip leading spaces
BEQ GETSPC
done:
rts
.endproc
; -----------------------------------------------------------------------------
; parse optionally quoted filename from command line
.proc GETFNADR
lda #$20
sta GETFNTERM
GETFNADR1:
JSR GETCHR
BEQ GETFNADR2 ; end of string
CMP #$20 ; skip leading spaces
BEQ GETFNADR1
CMP #'"' ;"
BNE GETFNADR2 ; not a quoted string
sta GETFNTERM
JSR GETCHR ; skip quote
GETFNADR2: ; get up to the terminator char
dec CHRPNT
lda #<BUF
clc
adc CHRPNT
tax
lda #>BUF
adc #0
tay
stx FNADR
sty FNADR+1
inc CHRPNT
ldy #0 ; compute file name length
@loop: lda (FNADR),y
beq GETFNADRE
CMP GETFNTERM ; compare with terminator
BEQ GETFNADRE
iny
bpl @loop
GETFNADRE:
tya
clc
adc CHRPNT
sta CHRPNT
tya
ldx FNADR
ldy FNADR+1
cmp #0
rts
GETFNTERM:
.byte 0
.endproc ; GETFNADR
; -----------------------------------------------------------------------------
; print and clear routines
CLINE:
JSR CRLF ; send CR+LF
JMP SNCLR ; clear line
SNDCLR: JSR SNDMSG
.proc SNCLR
LDY #$28 ; loop 40 times
SNCLP: LDA #$20 ; output space character
JSR CHROUT
LDA #$14 ; output delete character
JSR CHROUT
DEY
BNE SNCLP
RTS
.endproc
; -----------------------------------------------------------------------------
; display message from table
;.ifdef MSGBAS
.proc SNDMSG
LDA MSGBAS,Y ; Y contains offset in msg table
PHP
AND #$7F ; strip high bit before output
JSR CHOUT
INY
PLP
BPL SNDMSG ; loop until high bit is set
RTS
.endproc
;.endif