-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdisk.asm
163 lines (145 loc) · 3.97 KB
/
disk.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
;TRACE_FLOPPY = 1
;TRACE_FLOPPY_VERBOSE = 1
readblocks_numblocks !byte 0
readblocks_currentblock !byte 0,0 ; 257 = ff 1
readblocks_mempos !byte 0,0 ; $2000 = 00 20
readblocks
; read <n> blocks (each 256 bytes) from disc to memory
; set values in readblocks_* before calling this function
!ifdef TRACE_FLOPPY {
jsr newline
jsr print_following_string
!pet "readblocks (n,curr,pos) ",0
lda readblocks_numblocks
jsr printa
jsr comma
lda readblocks_currentblock
jsr print_byte_as_hex
lda readblocks_currentblock + 1
jsr print_byte_as_hex
jsr comma
lda readblocks_mempos + 1
jsr print_byte_as_hex
lda readblocks_mempos
jsr print_byte_as_hex
jsr newline
}
- jsr .readblock ; read block
inc readblocks_mempos + 1 ; update mempos,block for next iteration
inc readblocks_currentblock
bne +
inc readblocks_currentblock + 1
+ dec readblocks_numblocks ; loop
bne -
; clear arguments for next call
lda #0
sta readblocks_currentblock + 1
rts
.readblock
; read 1 block from floppy
; $mempos (contains address to store in) [in]
; convert block to track/sector
; (assuming each tracks has 16 sectors, skipping track18)
lda readblocks_currentblock
and #$0f
sta .sector
lda readblocks_currentblock
sta .track
lda readblocks_currentblock + 1
ldx #4
- lsr
ror .track
dex
bne -
inc .track ; tracks are 1..
lda .track
cmp #18
bcc +
inc .track ; skip track 18
; convert track/sector to ascii and update drive command
+ lda .track
jsr conv2dec
stx .uname_track
sta .uname_track + 1
lda .sector
jsr conv2dec
stx .uname_sector
sta .uname_sector + 1
!ifdef TRACE_FLOPPY_VERBOSE {
jsr space
jsr dollar
lda readblocks_mempos + 1
jsr print_byte_as_hex
lda readblocks_mempos
jsr print_byte_as_hex
jsr comma
ldx readblocks_currentblock
jsr printx
;jsr comma
;lda #<.uname
;ldy #>.uname
;jsr printstring
jsr newline
}
; open the channel file
lda #cname_len
ldx #<.cname
ldy #>.cname
jsr kernel_setnam ; call SETNAM
lda #$02 ; file number 2
ldx $BA ; last used device number
bne +
ldx #$08 ; default to device 8
+ ldy #$02 ; secondary address 2
jsr kernel_setlfs ; call SETLFS
jsr kernel_open ; call OPEN
bcs .error ; if carry set, the file could not be opened
; open the command channel
lda #uname_len
ldx #<.uname
ldy #>.uname
jsr kernel_setnam ; call SETNAM
lda #$0F ; file number 15
ldx $BA ; last used device number
ldy #$0F ; secondary address 15
jsr kernel_setlfs ; call SETLFS
jsr kernel_open ; call OPEN (open command channel and send U1 command)
bcs .error ; if carry set, the file could not be opened
; check drive error channel here to test for
; FILE NOT FOUND error etc.
ldx #$02 ; filenumber 2
jsr kernel_chkin ; call CHKIN (file 2 now used as input)
lda readblocks_mempos
sta zp_mempos
lda readblocks_mempos+1
sta zp_mempos + 1
ldy #$00
- jsr kernel_readchar ; call CHRIN (get a byte from file)
sta (zp_mempos),Y ; write byte to memory
iny
bne - ; next byte, end when 256 bytes are read
.close
lda #$0F ; filenumber 15
jsr kernel_close ; call CLOSE
lda #$02 ; filenumber 2
jsr kernel_close ; call CLOSE
jsr kernel_clrchn ; call CLRCHN
rts
.error
; accumulator contains BASIC error code
; most likely errors:
; A = $05 (DEVICE NOT PRESENT)
jsr .close ; even if OPEN failed, the file has to be closed
lda #ERROR_FLOPPY_READ_ERROR
jsr fatalerror
.cname !text "#"
cname_len = * - .cname
.uname !text "U1 2 0 "
.uname_track !text "18 "
.uname_sector !text "00"
;!ifdef DEBUG {
!byte 0 ; end of string, so we can print debug messages
;}
uname_len = * - .uname
.track !byte 0
.sector !byte 0