-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ch376.asm
1650 lines (1352 loc) · 36.3 KB
/
ch376.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Rookie Drive USB FDD BIOS
; By Konamiman, 2018
;
; This file contains all the code that depends on the CH376.
; To adapt the ROM to use a different USB host controller you need to:
;
; 1. Create a new source file.
; 2. Copy the HW_IMPL_* constants and set their values as appropriate,
; depending on which routines you are implementing.
;
; 3. Implement all the HW_* routines in the new file,
; except those for which you have set HW_IMPL_* to 0.
;
; 4. Include the new file in rookiefdd.asm, replacing the file labeled as
; "USB host hardware dependant code".
;
; All the code in this file is stateless: work area is not used,
; all the required information is passed in registers or buffers.
; -----------------------------------------------------------------------------
; Optional routine implementation flags
; -----------------------------------------------------------------------------
;
; The CH376 has built-in shortcuts for some common USB operations,
; and this BIOS take advantage of this.
; If you are adapting this BIOS to a different USB host hardware,
; you can implement the same routines if the hardware provides the same
; shortcuts, or leave the constants to 0 if not.
;
; HW_IMPL_<routine> needs to be 1 if HW_<routine> is implemented.
HW_IMPL_GET_DEV_DESCR: equ 1
HW_IMPL_GET_CONFIG_DESCR: equ 1
HW_IMPL_SET_CONFIG: equ 1
HW_IMPL_SET_ADDRESS: equ 1
HW_IMPL_CONFIGURE_NAK_RETRY: equ 1
; -----------------------------------------------------------------------------
; Constant definitions
; -----------------------------------------------------------------------------
;--- CH376 port to Z80 ports mapping
if USE_ALTERNATIVE_PORTS=1
CH_DATA_PORT: equ 22h
CH_COMMAND_PORT: equ 23h
else
CH_DATA_PORT: equ 20h
CH_COMMAND_PORT: equ 21h
endif
;--- Commands
CH_CMD_GET_IC_VER: equ 01h
CH_CMD_RESET_ALL: equ 05h
CH_CMD_CHECK_EXIST: equ 06h
CH_CMD_READ_VAR8: equ 0Ah
CH_CMD_SET_RETRY: equ 0Bh
CH_CMD_WRITE_VAR8: equ 0Bh
CH_CMD_READ_VAR32: equ 0Ch
CH_CMD_WRITE_VAR32: equ 0Dh
CH_CMD_DELAY_100US: equ 0Fh
CH_CMD_SET_USB_ADDR: equ 13h
CH_CMD_SET_USB_MODE: equ 15h
CH_CMD_TEST_CONNECT: equ 16h
CH_CMD_ABORT_NAK: equ 17h
CH_CMD_GET_STATUS: equ 22h
CH_CMD_RD_USB_DATA0: equ 27h
CH_CMD_WR_HOST_DATA: equ 2Ch
CH_CMD_WR_REQ_DATA: equ 2Dh
CH_CMD_SET_FILE_NAME: equ 2Fh
CH_CMD_DISK_CONNECT: equ 30h
CH_CMD_DISK_MOUNT: equ 31h
CH_CMD_FILE_OPEN: equ 32h
CH_CMD_FILE_ENUM_GO: equ 33h
CH_CMD_FILE_CREATE: equ 34h
CH_CMD_FILE_ERASE: equ 35h
CH_CMD_FILE_CLOSE: equ 36h
CH_CMD_DIR_INFO_READ: equ 37h
CH_CMD_BYTE_LOCATE: equ 39h
CH_CMD_BYTE_READ: equ 3Ah
CH_CMD_BYTE_RD_GO: equ 3Bh
CH_CMD_BYTE_WRITE: equ 3Ch
CH_CMD_BYTE_WRITE_GO: equ 3Dh
CH_CMD_DIR_CREATE: equ 40h
CH_CMD_SET_ADDRESS: equ 45h
CH_CMD_GET_DESCR: equ 46h
CH_CMD_SET_CONFIG: equ 49h
CH_CMD_ISSUE_TKN_X: equ 4Eh
;--- PIDs
CH_PID_SETUP: equ 0Dh
CH_PID_IN: equ 09h
CH_PID_OUT: equ 01h
;--- Status codes
CH_ST_INT_SUCCESS: equ 14h
CH_ST_INT_CONNECT: equ 15h
CH_ST_INT_DISCONNECT: equ 16h
CH_ST_INT_BUF_OVER: equ 17h
CH_ST_INT_DISK_READ: equ 1Dh
CH_ST_INT_DISK_WRITE: equ 1Eh
CH_ST_INT_DISK_ERR: equ 1Fh
CH_ST_RET_SUCCESS: equ 51h
CH_ST_RET_ABORT: equ 5Fh
; -----------------------------------------------------------------------------
; Mandatory routines
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
; HW_TEST: Check if the USB host controller hardware is operational
; -----------------------------------------------------------------------------
; Output: Cy = 0 if hardware is operational, 1 if it's not
HW_TEST:
if USE_FAKE_STORAGE_DEVICE=1
or a
ret
endif
ld a,34h
call _HW_TEST_DO
scf
ret nz
ld a,89h
call _HW_TEST_DO
scf
ret nz
or a
ret
_HW_TEST_DO:
ld b,a
ld a,CH_CMD_CHECK_EXIST
out (CH_COMMAND_PORT),a
ld a,b
xor 0FFh
out (CH_DATA_PORT),a
in a,(CH_DATA_PORT)
cp b
ret
; -----------------------------------------------------------------------------
; HW_RESET: Reset the USB controller hardware
;
; If a device is connected performs a bus reset that leaves the device
; in the "Default" state.
; -----------------------------------------------------------------------------
; Input: -
; Output: A = 1 if a USB device is connected
; -1 if no USB device is connected
; Cy = 1 if reset failed
HW_RESET:
;Clear the CH376 data buffer in case a reset was made
;while it was in the middle of a data transfer operation
;ld b,64
_HW_RESET_CLEAR_DATA_BUF:
in a,(CH_DATA_PORT)
djnz _HW_RESET_CLEAR_DATA_BUF
ld a,CH_CMD_RESET_ALL
out (CH_COMMAND_PORT),a
if USING_ARDUINO_BOARD=1
ld bc,1000
_HW_RESET_WAIT:
dec bc
ld a,b
or c
jr nz,_HW_RESET_WAIT
else
ld bc,350
call CH_DELAY
endif
call CH_DO_SET_NOSOF_MODE
ret c
ld a,CH_CMD_TEST_CONNECT
out (CH_COMMAND_PORT),a
_CH_WAIT_TEST_CONNECT:
in a,(CH_DATA_PORT)
or a
jr z,_CH_WAIT_TEST_CONNECT
cp CH_ST_INT_DISCONNECT
ld a,-1
ret z
jp HW_BUS_RESET
; -----------------------------------------------------------------------------
; HW_DEV_CHANGE: Check for changes in the device connection
;
; The returned status is relative to the last time that the routine
; was called.
;
; If a device has been connected it performs a bus reset that leaves the device
; in the "Default" state.
; -----------------------------------------------------------------------------
; Input: -
; Output: A = 1 if a USB device has been connected
; 0 if no change has been detected
; -1 if the USB device has been disconnected
; Cy = 1 if bus reset failed
HW_DEV_CHANGE:
call CH_CHECK_INT_IS_ACTIVE
ld a,0
ret nz
call CH_GET_STATUS
cp CH_ST_INT_CONNECT
jp z,HW_BUS_RESET
cp CH_ST_INT_DISCONNECT
jp z,CH_DO_SET_NOSOF_MODE
xor a
ret
; -----------------------------------------------------------------------------
; HW_CONTROL_TRANSFER: Perform a USB control transfer on endpoint 0
;
; The size and direction of the transfer are taken from the contents
; of the setup packet.
; -----------------------------------------------------------------------------
; Input: HL = Address of a 8 byte buffer with the setup packet
; DE = Address of the input or output data buffer
; A = Device address
; B = Maximum packet size for endpoint 0
; Output: A = USB error code
; BC = Amount of data actually transferred (if IN transfer and no error)
HW_CONTROL_TRANSFER:
call CH_SET_TARGET_DEVICE_ADDRESS
push hl
push bc
push de
ld b,8
call CH_WRITE_DATA ;Write SETUP data packet
xor a
ld e,0
ld b,CH_PID_SETUP
call CH_ISSUE_TOKEN
call CH_WAIT_INT_AND_GET_RESULT
pop hl ;HL = Data address (was DE)
pop de ;D = Endpoint size (was B)
pop ix ;IX = Address of setup packet (was HL)
or a
ld bc,0
ret nz ;DONE if error
ld c,(ix+6)
ld b,(ix+7) ;BC = Data length
ld a,b
or c
jr z,_CH_CONTROL_STATUS_IN_TRANSFER
ld e,0 ;E = Endpoint number
scf ;Use toggle = 1
bit 7,(ix)
jr z,_CH_CONTROL_OUT_TRANSFER
_CH_CONTROL_IN_TRANSFER:
call CH_DATA_IN_TRANSFER
or a
ret nz
push bc
ld b,0
call CH_WRITE_DATA
ld e,0
ld b,CH_PID_OUT
ld a,40h ;Toggle bit = 1
call CH_ISSUE_TOKEN
call CH_WAIT_INT_AND_GET_RESULT
pop bc
ret
_CH_CONTROL_OUT_TRANSFER:
call CH_DATA_OUT_TRANSFER
or a
ret nz
_CH_CONTROL_STATUS_IN_TRANSFER:
push bc
ld e,0
ld b,CH_PID_IN
ld a,80h ;Toggle bit = 1
call CH_ISSUE_TOKEN
ld hl,0
call CH_READ_DATA
call CH_WAIT_INT_AND_GET_RESULT
pop bc
ret
; -----------------------------------------------------------------------------
; HW_DATA_IN_TRANSFER: Perform a USB data IN transfer
; -----------------------------------------------------------------------------
; Input: HL = Address of a buffer for the received data
; BC = Data length
; A = Device address
; D = Maximum packet size for the endpoint
; E = Endpoint number
; Cy = Current state of the toggle bit
; Output: A = USB error code
; BC = Amount of data actually received (only if no error)
; Cy = New state of the toggle bit (even on error)
HW_DATA_IN_TRANSFER:
call CH_SET_TARGET_DEVICE_ADDRESS
; This entry point is used when target device address is already set
CH_DATA_IN_TRANSFER:
ld a,0
rra ;Toggle to bit 7 of A
ld ix,0 ;IX = Received so far count
push de
pop iy ;IY = EP size + EP number
_CH_DATA_IN_LOOP:
push af ;Toggle in bit 7
push bc ;Remaining length
ld e,iyl
ld b,CH_PID_IN
call CH_ISSUE_TOKEN
call CH_WAIT_INT_AND_GET_RESULT
or a
jr nz,_CH_DATA_IN_ERR ;DONE if error
call CH_READ_DATA
ld c,b
ld b,0
add ix,bc ;Update received so far count
_CH_DATA_IN_NO_MORE_DATA:
pop de
pop af
xor 80h ;Update toggle
push af
push de
ld a,c
or a
jr z,_CH_DATA_IN_DONE ;DONE if no data received
ex (sp),hl ;Now HL = Remaining data length
or a
sbc hl,bc ;Now HL = Updated remaning data length
ld a,h
or l
ex (sp),hl ;Remaining data length is back on the stack
jr z,_CH_DATA_IN_DONE ;DONE if no data remaining
ld a,c
cp iyh
jr c,_CH_DATA_IN_DONE ;DONE if transferred less than the EP size
pop bc
pop af ;We need this to pass the next toggle to CH_ISSUE_TOKEN
jr _CH_DATA_IN_LOOP
;Input: A=Error code (if ERR), in stack: remaining length, new toggle
_CH_DATA_IN_DONE:
xor a
_CH_DATA_IN_ERR:
ld d,a
pop bc
pop af
rla ;Toggle back to Cy
ld a,d
push ix
pop bc
ret
; -----------------------------------------------------------------------------
; HW_DATA_OUT_TRANSFER: Perform a USB data OUT transfer
; -----------------------------------------------------------------------------
; Input: HL = Address of a buffer for the data to be sent
; BC = Data length
; A = Device address
; D = Maximum packet size for the endpoint
; E = Endpoint number
; Cy = Current state of the toggle bit
; Output: A = USB error code
; Cy = New state of the toggle bit (even on error)
HW_DATA_OUT_TRANSFER:
call CH_SET_TARGET_DEVICE_ADDRESS
; This entry point is used when target device address is already set
CH_DATA_OUT_TRANSFER:
ld a,0
rra ;Toggle to bit 6 of A
rra
push de
pop iy ;IY = EP size + EP number
_CH_DATA_OUT_LOOP:
push af ;Toggle in bit 6
push bc ;Remaining length
ld a,b
or a
ld a,iyh
jr nz,_CH_DATA_OUT_DO
ld a,c
cp iyh
jr c,_CH_DATA_OUT_DO
ld a,iyh
_CH_DATA_OUT_DO:
;Here, A = Length of the next transfer: min(remaining length, EP size)
ex (sp),hl
ld e,a
ld d,0
or a
sbc hl,de
ex (sp),hl ;Updated remaining data length to the stack
ld b,a
call CH_WRITE_DATA
pop bc
pop af ;Retrieve toggle
push af
push bc
ld e,iyl
ld b,CH_PID_OUT
call CH_ISSUE_TOKEN
call CH_WAIT_INT_AND_GET_RESULT
or a
jr nz,_CH_DATA_OUT_DONE ;DONE if error
pop bc
pop af
xor 40h ;Update toggle
push af
ld a,b
or c
jr z,_CH_DATA_OUT_DONE_2 ;DONE if no more data to transfer
pop af ;We need this to pass the next toggle to CH_ISSUE_TOKEN
jr _CH_DATA_OUT_LOOP
;Input: A=Error code, in stack: remaining length, new toggle
_CH_DATA_OUT_DONE:
pop bc
_CH_DATA_OUT_DONE_2:
ld d,a
pop af
rla ;Toggle back to Cy
rla
ld a,d
ret
; -----------------------------------------------------------------------------
; HW_BUS_RESET: Performs a USB bus reset.
;
; This needs to run when a device connection is detected.
; -----------------------------------------------------------------------------
; Output: A = 1
; Cy = 1 on error
HW_BUS_RESET:
ld a,7
call CH_SET_USB_MODE
ld a,1
ret c
if USING_ARDUINO_BOARD = 0
ld bc,150
call CH_DELAY
endif
ld a,6
call CH_SET_USB_MODE
ld a,1
ret c
xor a
inc a
ret
;Input: BC = Delay duration in units of 0.1ms
CH_DELAY:
ld a,CH_CMD_DELAY_100US
out (CH_COMMAND_PORT),a
_CH_DELAY_LOOP:
in a,(CH_DATA_PORT)
or a
jr z,_CH_DELAY_LOOP
dec bc
ld a,b
or c
jr nz,CH_DELAY
ret
; -----------------------------------------------------------------------------
; File management routines
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
; HWF_MOUNT_DISK: Mounts a storage device if present.
;
; On success it also opens the root directory
; -----------------------------------------------------------------------------
; Input: HL = address for the name of the device found
; Output: Cy = 0: ok
; 1: error, no device present or it's not a storage device
HWF_MOUNT_DISK:
if USE_FAKE_STORAGE_DEVICE=1
ex de,hl
ld hl,FAKE_DEV_NAME
ld bc,24
ldir
xor a
ld (de),a
ret
FAKE_DEV_NAME:
db "TheStorageThing 0.05"
endif
ld a,CH_CMD_DISK_CONNECT
out (CH_COMMAND_PORT),a
call CH_WAIT_INT_AND_GET_RESULT
cp USB_ERR_OK
scf
ret nz
ld a,6
call CH_SET_USB_MODE
ld a,CH_CMD_DISK_MOUNT
out (CH_COMMAND_PORT),a
call CH_WAIT_INT_AND_GET_RESULT
cp USB_ERR_OK
scf
ret nz
push hl
call CH_READ_DATA
pop hl
ld d,h
ld e,l
ld bc,8
add hl,bc
ld bc,36-8
ldir
xor a
ld (de),a
ret
; -----------------------------------------------------------------------------
; HWF_OPEN_FILE_DIR: Open a file or enter a directory from the current one
; -----------------------------------------------------------------------------
; Input: HL = Address of file or directory name, relative to current
; Output: A = 0: ok, file or directory open
; 1: generic error (e.g. no device found)
; 2: file or directory not found
;
; Cy = 0: file open (if no error)
; 1: directory open (if no error)
; Z if ok, NZ if error
; HL = Pointer to terminator of file or directory name
HWF_OPEN_FILE_DIR:
if USE_FAKE_STORAGE_DEVICE=1
;ld a,27
;call CHPUT
;ld a,'j'
;call CHPUT
;call PRINT
xor a
ret
endif
ld a,CH_CMD_SET_FILE_NAME
out (CH_COMMAND_PORT),a
call CH_WRITE_STRING
push hl
ld a,CH_CMD_FILE_OPEN
out (CH_COMMAND_PORT),a
call CH_WAIT_INT_AND_GET_RESULT
pop hl
ld b,a
cp USB_ERR_OK
ld a,0
scf
ccf
ret z ;NC, Z
ld a,b
cp USB_ERR_OPEN_DIR
ld a,0
scf
ret z ;C, Z
ld a,b
cp USB_ERR_MISS_FILE
ld a,2
jr z,_HWF_OPEN_FILE_DIR_END
dec a
_HWF_OPEN_FILE_DIR_END:
or a ;Force NZ
ret
; -----------------------------------------------------------------------------
; HWF_CREATE_FILE: Create a new file in current directory, overwrite if exists
; -----------------------------------------------------------------------------
; Input: HL = Address of file or directory name, relative to current
; Output: A = 0: ok, file open
; 1: error (might be that a directory with the same name exists)
HWF_CREATE_FILE:
if 0
push hl
call HWF_CLOSE_FILE
pop hl
push hl
ld a,CH_CMD_SET_FILE_NAME
out (CH_COMMAND_PORT),a
call CH_WRITE_STRING
ld a,CH_CMD_FILE_ERASE
out (CH_COMMAND_PORT),a
call CH_WAIT_INT_AND_GET_RESULT
pop hl
endif
ld a,CH_CMD_SET_FILE_NAME
out (CH_COMMAND_PORT),a
call CH_WRITE_STRING
ld a,CH_CMD_FILE_CREATE
out (CH_COMMAND_PORT),a
call CH_WAIT_INT_AND_GET_RESULT
cp USB_ERR_OK
ld a,0
ret z
inc a
ret
; -----------------------------------------------------------------------------
; HWF_CREATE_DIR: Create a new directory in current directory, open if exists
; -----------------------------------------------------------------------------
; Input: HL = Address of file or directory name, relative to current
; Output: A = 0: ok, file open
; 1: error (might be that a file with the same name exists)
HWF_CREATE_DIR:
ld a,CH_CMD_SET_FILE_NAME
out (CH_COMMAND_PORT),a
call CH_WRITE_STRING
ld a,CH_CMD_DIR_CREATE
out (CH_COMMAND_PORT),a
call CH_WAIT_INT_AND_GET_RESULT
cp USB_ERR_OK
ld a,0
ret z
inc a
ret
; -----------------------------------------------------------------------------
; HWF_CLOSE_FILE: Close open file or directory, update size in dir entry
; -----------------------------------------------------------------------------
HWF_CLOSE_FILE:
ld a,CH_CMD_FILE_CLOSE
out (CH_COMMAND_PORT),a
ld a,1
out (CH_DATA_PORT),a
jp CH_WAIT_INT_AND_GET_RESULT
; -----------------------------------------------------------------------------
; HWF_DELETE_FILE: Delete file or directory
; -----------------------------------------------------------------------------
; Input: HL = File or directory name
; Output: A = 0: Ok
; 1: Error
HWF_DELETE_FILE:
;Very important: close any open file first, or it will be deleted!
push hl
call HWF_CLOSE_FILE
pop hl
ld a,CH_CMD_SET_FILE_NAME
out (CH_COMMAND_PORT),a
call CH_WRITE_STRING
ld a,CH_CMD_FILE_ERASE
out (CH_COMMAND_PORT),a
call CH_WAIT_INT_AND_GET_RESULT
or a
ret z
ld a,1
ret
; -----------------------------------------------------------------------------
; HWF_ENUM_FILES: Enumerate files and directories in the current directory
;
; Files/directories whose first character is "_" will be skipped.
;
; The name of each files/directory will be put at the specified address,
; sequentially and with no separators, as a fixed 11 bytes string in the
; same format as in the directory entry (e.g. "FILE EXT"); after the last
; entry a 0 byte will be placed.
; -----------------------------------------------------------------------------
; Input: HL = Address of buffer to get file info into
; BC = Maximum number of files/directories to enumerate
; Output: HL = Pointer to the 0 byte at the end of the list
; BC = Number of filenames found
HWF_ENUM_FILES:
if USE_FAKE_STORAGE_DEVICE = 1
push bc
call _HWF_ENUM_FILES
pop bc
ret
_HWF_ENUM_FILES:
ld de,0
push hl
pop ix
_HWF_ENUM_FILES_LOOP:
push bc
push de
push ix
pop de
ld hl,FILE_S
ld bc,3
ldir
pop hl
push hl
call Num2Hex
ld a,' '
ld (de),a
inc de
ld hl,EXT_S
ld bc,3
ldir
push de
pop ix
pop de
inc de
ld a,e
and 15
jr nz,_HWF_ENUM_FILES_NODIR
ld (ix-4),' '
ld (ix-3),' '
ld (ix-2),' '
ld (ix-1),128+32 ;Space with bit 7 set
_HWF_ENUM_FILES_NODIR:
pop bc
dec bc
ld a,b
or c
jr nz,_HWF_ENUM_FILES_LOOP
xor a
ld (ix),a
push ix
pop hl
ret
FILE_S: db "FIL"
EXT_S: db "EXT"
;Input: HL = number to convert
; DE = location of ASCII string
Num2Hex:
ld a,h
call Num1
ld a,h
call Num2
ld a,l
call Num1
ld a,l
jr Num2
Num1:
rra
rra
rra
rra
Num2:
or 0F0h
daa
add a,0A0h
adc a,040h
ld (de),a
inc de
ret
endif
push iy
ld iy,0
call _HWF_ENUM_FILES_CORE
pop iy
ret
; -----------------------------------------------------------------------------
; HWF_FIND_NTH_FILE: Find the Nth file in the current directory
;
; Files whose first character is "_" will be skipped.
;
; The name of the files will be put at the specified address,
; sequentially and with no separators, as a fixed 11 bytes string in the
; same format as in the directory entry (e.g. "FILE EXT").
; -----------------------------------------------------------------------------
; Input: HL = Address of buffer to get file info into
; A = Index of file to find (first is 0)
; Output: A = 0: Ok
; 1: Generic error
; 2: File not found
; HL = Pointer after the filename
HWF_FIND_NTH_FILE:
push iy
ld iy,1
ld c,a
ld b,0
inc bc
call _HWF_ENUM_FILES_CORE
pop iy
or a
ret z
cp USB_ERR_MISS_FILE
ld a,2
ret z
dec a
ret
_HWF_ENUM_FILES_CORE:
if USE_FAKE_STORAGE_DEVICE = 0
ld a,CH_CMD_SET_FILE_NAME
out (CH_COMMAND_PORT),a
ld a,'*'
out (CH_DATA_PORT),a
xor a
out (CH_DATA_PORT),a
ld de,0 ;Number of files found
ld a,CH_CMD_FILE_OPEN
out (CH_COMMAND_PORT),a
_HWF_ENUM_FILES_LOOP:
push hl
push de
push bc
call CH_WAIT_INT_AND_GET_RESULT
pop bc
pop de
pop hl
cp CH_ST_INT_DISK_READ
jr nz,_HWF_ENUM_FILES_END
push bc
push hl
push de
call CH_READ_DATA
pop de
pop hl
pop bc
ld a,(hl)
cp "_"
jr z,_HWF_ENUM_DIR_NEXT
cp "." ;Skip "." and ".." entries
jr z,_HWF_ENUM_DIR_NEXT
push bc
ld bc,11
add hl,bc ;Point to file attributes byte
pop bc
ld a,(hl)
and 1110b ;"Hidden", "System" or "Volume" attribute set?
jr nz,_HWF_ENUM_SKIP
ld a,iyl
or a
jr z,_HWF_ENUM_USE
bit 4,(hl) ;"Directory" attribute set?
jr z,_HWF_ENUM_DIR_OK
_HWF_ENUM_SKIP:
push bc
ld bc,-11
add hl,bc
pop bc
jr _HWF_ENUM_DIR_NEXT
_HWF_ENUM_USE:
bit 4,(hl) ;"Directory" attribute set?
jr z,_HWF_ENUM_DIR_OK
dec hl
set 7,(hl)
inc hl
_HWF_ENUM_DIR_OK:
inc de
dec bc
ld a,b
or c
jr z,_HWF_ENUM_FILES_END
ld a,iyl
or a
jr nz,_HWF_ENUM_SKIP
_HWF_ENUM_DIR_NEXT:
ld a,CH_CMD_FILE_ENUM_GO
out (CH_COMMAND_PORT),a
jp _HWF_ENUM_FILES_LOOP
_HWF_ENUM_FILES_END:
push af
ld a,iyl
or a
jr nz,_HWF_ENUM_FILES_END_2
ld (hl),0
_HWF_ENUM_FILES_END_2:
pop af
push de
pop bc
ret
endif
; -----------------------------------------------------------------------------