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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
;-------------------------------------------------
; Drive Code for UltiCopy
;
; Programmed by Gideon Zweijtzer
;
; Copyright (c) 2013 - Gideon Zweijtzer
;
;-------------------------------------------------
    *=$0400
drive_code_start
	.logical $0400

    iec_datain = $01
    iec_dataout = $02
    iec_clkin = $04
    iec_clkout = $08
    iec_atna = $10
    
    ctrl_step = $01
    ctrl_motor = $04
    ctrl_led = $08
    ctrl_wprot = $10
    ctrl_density = $20
    ctrl_sync = $80
        
    via_iec = $1800 ; port B cia1
    via_sysctrl = $1801 ; port A cia1 (track0 sense, 1/2 MHz, side ...)
    via_timer = $1805 ; cia1 timer
    via_ctrl = $1c00 ; port B cia2 (sync, motor, etc)
    via_data = $1c01 ; port A cia2
    via_data_dir = $1c03; port A cia 2
    via_mode = $1c0c
        
    data_buffer = $0300
    shifted_table = $0700
    sectors_to_read = $06c0
    last_part = $01ba
       
    bin_to_gcr = $f77f
    region_limits = $fed7
    region_sectors = $fed1
    
    attempts = $8c
    temp1    = $8d
    sectors_read = $8e
    sectors_on_this_track = $8f
    current_track = $90
    zone = $91
    transfer_pointer = $92
    transfer_end = $94
    
drive_start
    jmp start_copy
    jmp read_disk
    
send_block
    ; do synchronization with receiver
    ; set data, wait for clock set
    ; clear data, wait for clock clear
    ; pulse data+clock together
    ; clear data+clock
        
    lda #iec_dataout
    sta via_iec
-
    lda #iec_clkin
    and via_iec
    beq -
    lda #0
    sta via_iec
    lda #iec_clkin
-
    and via_iec
    bne -

    ;; setup pointer
    lda #$80
    ldx #<data_buffer
    ldy #>data_buffer
    jsr send_sub_block
    lda #$80
    ldx #<(data_buffer+$80)
    ldy #>(data_buffer+$80)
    jsr send_sub_block
    lda #$44
    ldx #<last_part
    ldy #>last_part
    jsr send_sub_block
    rts

send_sub_block
    sta transfer_end
    stx transfer_pointer
    sty transfer_pointer+1
    
    lda #(iec_clkout + iec_dataout) ;2
    sta via_iec                     ;4 <-- start
    lda #0                          ;2
    sta via_iec                     ;4 -- 6 us pulse

    ;; pulse (6) -- 15 -- bits: 8 8 8 21
    ;; sample +4us (center of 8)
    ;; wait for both clock and data low (start of 6 us pulse)
    ;; then wait 6+15+4 = 25 us, then cycle through pattern 8 8 8 21 8 8 8 21
    
    ;; transfer block
    ldy #$00                 ; 2
-
    lda (transfer_pointer),y ; 5
    tax                      ; 2
    and #$0f                 ; 2
    sta via_iec ; bit 3/1    ; 4
    asl                      ; 2
    and #$0f                 ; 2
    sta via_iec ; bit 2/0    ; 4
    lda shifted_table,x      ; 4
    sta via_iec ; bit 7/5    ; 4
    asl                      ; 2
    and #$0f                 ; 2
    sta via_iec ; bit 6/4    ; 4
    iny                      ; 2
    cpy transfer_end         ; 3
    bne -                    ; 3*
                             ; -- 45 --
    lda #0
    sta via_iec
    rts

send_error
    sta temp1

    ; do synchronization with receiver
    ; set clock, wait for data set
    ; clear clock, wait for data clear
    lda #iec_clkout
    sta via_iec
-   lda #iec_datain
    and via_iec
    beq -
    lda #0
    sta via_iec
-   lda #iec_datain
    and via_iec
    bne -

    lda #1
    ldx #<temp1
    ldy #>temp1
    jmp send_sub_block

start_copy
    jsr create_shift_table
    lda #0 ; remove ourselves from the bus
    sta via_iec
    jsr wait_bus_free
    jsr seek_track_1
    cmp #1
    beq read_disk
    sei
    jsr send_error ; failed
    cli
    rts

read_disk
    jsr $f97e ; turn motor on
    jsr wait_a_little
    sei
    lda #$08
    sta via_sysctrl ; make sure we're running at 1 MHz and try to read side 0
    lda #1
-
    jsr calculate_zone ; sets current track, zone and sectors on track
    jsr read_track ; read and transfer track
    beq +
    ora #$c0
    jsr send_error
+   jsr step_in ; move head half track
    jsr step_in ; move head half track
    lda current_track
    clc
    adc #$01
    cmp #36
    bne -
    tay
    dey
-   jsr step_out
    jsr step_out
    dey
    bne -    
    lda #0
    jsr send_error ; done!
    cli
    jmp $f98f ; turn off drive motor and exit

calculate_zone ; (track number in a)
    sta current_track
    ldx #$04
-   cmp region_limits-1,x
    dex
    bcs -
    lda region_sectors,x
    sta sectors_on_this_track
    txa
    asl
    asl
    asl
    asl
    asl
    sta zone
    lda via_ctrl
    and #$9f
    ora zone
    sta via_ctrl
    rts
    
seek_track_1
    sei
    lda #$c0
    sta $01
    cli
-   lda $01
    bmi -
    lda $01
    cmp #$01
    beq +
    ora #$20
    rts
+   sei
    lda #$b0
    sta $01
    lda #$01
    sta $08
    lda #$00
    sta $09
    cli
-   lda $01
    bmi -
    lda $01
    rts

step_in
    lda via_ctrl
    tax
    and #$fc
    sta temp1
    inx
sssi
    txa
    and #$03
    ora temp1
    sta via_ctrl
    lda #$98
    sta via_timer
-   bit via_timer
    bmi -
    rts
    
step_out
    lda via_ctrl
    tax
    and #$fc
    sta temp1
    dex
    jmp sssi

read_track
    lda sectors_on_this_track
    jsr prepare_sector_table
    lda #$ee
    sta via_mode
    lda #$00
    sta via_data_dir
    lda #$00
    sta sectors_read
-   jsr read_sector
    bne +

    jsr send_block
    lda sectors_read
    cmp sectors_on_this_track
    bne -
    lda #0
+   rts
    
create_shift_table
    ldx #0
-
    txa
    lsr
    lsr
    lsr
    lsr
    sta shifted_table,x
    inx
    bne -
    rts

read_sector
    lda #100
    sta attempts
_next_attempt
    dec attempts
    lda attempts
    bpl +
_out
    lda #1 ; SECTOR NOT FOUND
    rts    
    ; turn on LED
+   lda #ctrl_led
    ora via_ctrl
    sta via_ctrl
    ; set timer and wait for sync
    lda #$d0
    sta via_timer
-   bit via_timer
    bmi +
    lda #2 ; NO SYNC
    rts
+   bit via_ctrl
    bmi -
    ; check for header
    lda via_data
    clv
-   bvc -
    clv
    lda via_data
    cmp #$52
    bne _next_attempt
-   bvc -
    clv
    lda via_data
    and #$c0
    eor #$40
    bne _next_attempt
-   bvc -
    clv
    lda via_data
    and #$0f
    eor #$05
    bne _next_attempt
-   bvc -
    clv
    lda via_data
    lsr
    lsr
    tax
    lda sectors_to_read,x
    bmi _next_attempt    
    ; note: x holds GCR code for sector number
    ; we know enough about the header now
    ; skip the rest
    ; wait for sync and data block
-   bit via_ctrl
    bmi -
    lda via_data
    clv
    ldy #0
-   bvc -
    clv
    lda via_data
    cmp #$55
    bne _next_attempt
-   bvc -
    clv
    lda via_data
    sta data_buffer,y
    iny
    bne -
    ldy #$bf
-   bvc -
    clv
    lda via_data
    sta last_part-$bf,y
    iny
    bne -
-   bvc -
    clv
    lda via_data
    and #$f0
    sta last_part+$41
    lda sectors_to_read,x
    tay
    lda #$ee ; done!
    sta sectors_to_read,x
    sty last_part+$42 ; current sector (binary)
    lda current_track
    sta last_part+$43 
    ;; checksum to last_part+$43.. but why, for transfer errors?
    ; turn led off
    lda #(255-ctrl_led)
    and via_ctrl
    sta via_ctrl
    inc sectors_read
    lda #0 ; OK
    rts
    
; prepare sector table (a = number of sectors)
; the result of this function is a 64-byte table
; that has a valid binary sector number stored on
; the position indexed by a 6-bit GCR pattern.
; all other patterns are $ff
prepare_sector_table
    tay
    lda #$ff
    ldx #$3f
-   sta sectors_to_read,x
    dex
    bpl -
-   dey
    tya
    and #$0f
    tax
    lda bin_to_gcr,x
    sta temp1
    tya
    and #$10
    asl
    ora temp1
    tax
    tya
    sta sectors_to_read,x    
    cpy #$00
    bne -
    rts

wait_bus_free
    lda #ctrl_led
    ora via_ctrl
    sta via_ctrl
    lda #0
    sta via_iec
-   lda via_iec
    and #$0f
    bne -
    lda #255-ctrl_led
    and via_ctrl
    sta via_ctrl
    rts

wait_a_little
    ldy #200
-   inx
    bne -
    dey
    bne -
    rts
    
    .cerror *>$6c0
    .here      
drive_code_end