forked from CorshamTech/6502-Tiny-BASIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.asm
216 lines (211 loc) · 4.01 KB
/
storage.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
;
;=====================================================
;=====================================================
;=====================================================
; This file contains the functions for saving and
; restoring programs from some sort of mass storage
; device. This particular version is for using the
; Corsham Tech SD Card System.
;=====================================================
;=====================================================
;=====================================================
bss
diskBufLength ds 1
diskBufOffset ds 1
code
;
;=====================================================
; Open a file for reading as a program. The next
; thing on the line should be the filename.
;
iOPENREAD
if XKIM || CTMON65
ldy CUROFF
lda (CURPTR),y
bne iOPENfn ;might be filename
;
; No filename supplied.
;
iOPENnofn lda #0
ldx #ERR_NO_FILENAME
jmp iErr2
;
; Add the offset into the buffer start
;
iOPENfn clc
tya
adc CURPTR
tay ;LSB
lda CURPTR+1
adc #0
tax
jsr DiskOpenRead ;attempt to open file
bcc Ropenok ;branch if opened ok
;
; Open failed
;
Rdfail ldx #ERR_READ_FAIL
Rdfail2 lda #0
jmp iErr2
;
; Clear counts and offsets so the next read will
; cause the file to be read.
;
Ropenok lda #0
sta diskBufOffset
sta diskBufLength
jmp NextIL
endif
;
;=====================================================
iOPENWRITE
if XKIM || CTMON65
ldy CUROFF
lda (CURPTR),y
beq iOPENnofn
;
clc
tya
adc CURPTR
tay ;LSB
lda CURPTR+1
adc #0
tax
jsr DiskOpenWrite ;attempt to open file
bcc Wopenok ;branch if opened ok
;
; Open failed
;
Wdfail lda #0
ldx #ERR_WRITE_FAIL
jmp iErr2
;
Wopenok jmp NextIL
endif
;
;=====================================================
; Gets a line of input from the disk file and puts it
; into LINBUF.
;
; On exit:
; CURPTR points to LINBUF
; LINBUF contains the line with 0 at the end.
; Y has offset to first non-space character
; CURROFF has the same as Y.
;
iDGETLINE
if XKIM || CTMON65
ldx #LINBUF&$ff
stx CURPTR
ldx #LINBUF>>8
stx CURPTR+1
;
ldx #0 ;offset
iDgetLoop stx getlinx
jsr getNextFileByte
bcs iGetEOF
cmp #CR
beq iGetEOL
cmp #LF
beq iGetEOL
ldx getlinx
sta LINBUF,x
inx
bne iDgetLoop
;
; Handle end of line. If the line has nothing, loop
; back and get another line.
;
iGetEOL ldx getlinx ;blank line?
beq iDgetLoop ;yes, ignore it
;
; This can fall through when there is a line, or
; called directly when EOF is encountered.
;
iGetEOF ldx getlinx
lda #0
sta LINBUF,x
sta CUROFF
ldy #0
jsr SkipSpaces
jmp NextIL
endif
;
;=====================================================
; Does a LIST to a file file.
;
iDLIST
if XKIM || CTMON65
jsr SetOutDisk
jmp iLST2
endif
;
;=====================================================
; Closes any pending disk file. Okay to call if there
; is no open file.
;
iDCLOSE
if XKIM || CTMON65
jsr DiskClose
jmp NextIL
endif
;
;=====================================================
; This gets the next byte from an open disk file. If
; there are no more bytes left, this returns C set.
; Else, C is clear and A contains the character.
;
getNextFileByte
if XKIM || CTMON65
ldx diskBufOffset
cpx diskBufLength
bne hasdata ;branch if still data
;
; There is no data left in the buffer, so read a
; block from the SD system.
;
lda #BUFFER_SIZE
ldx #buffer>>8
ldy #buffer&$ff
jsr DiskRead
bcs getNextEof
;
; A contains the number of bytes actually read.
;
sta diskBufLength ;save length
cmp #0 ;shouldn't happen
beq getNextEof
;
ldx #0
hasdata lda buffer,x
inx
stx diskBufOffset
clc
rts
;
getNextEof lda #0
sta diskBufOffset
sta diskBufLength
sec
rts
;
;=====================================================
; Set output vector to the disk output function
;
SetOutDisk lda #DOUT&$ff
sta BOutVec
lda #DOUT/256
sta BOutVec+1
rts
;
;=====================================================
DOUT sta buffer
lda #1
ldy #buffer&$ff
ldx #buffer/256
jsr DiskWrite
;
; need error checking here
;
rts
endif