-
Notifications
You must be signed in to change notification settings - Fork 0
/
VDrive.asm
1586 lines (1341 loc) · 35.3 KB
/
VDrive.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
; ==========================================================================
; VDrive
; --------------------------------------------------------------------------
; Provides access to files on a Vinculum device (e.g. VDrive/VMusic)
; --------------------------------------------------------------------------
; Pin connections are similar to the RS-232 adaptor:
;
; SMS VDrive
; 1 Up <- 2 RTS#
; 2 Down <- 5 TxD
; 5 Vcc -- 3 5V0
; 7 TH -> 4 RxD
; 8 GND -- 1 GND
; 9 TR -> 6 CTS#
;
; SMS pins 3 (Left), 4 (Right) and 6 (TL) are not connected.
; VDrive pins 7 (NC) and 8 (RI#/WU) are not connected.
; VDrive jumper must be in default UART position.
; ==========================================================================
.module VDrive
.module Commands
; Monitor commands
ShortCommandSet = $10
ExtendedCommandSet = $11
MonitorAscii = $90
MonitorBinary = $91
FirmwareVersion = $13
EchoUppercase = $45
EchoLowercase = $65
; Disk commands
ListDirectory = $01
ChangeDirectory = $02
ReadFile = $04
DeleteDirectory = $05
MakeDirectory = $06
DeleteFile = $07
WriteFileData = $08
OpenFileWriting = $09
CloseFile = $0A
ReadFileData = $0B
Rename = $0C
OpenFileReading = $0E
Seek = $28
GetFreeSpaceUnder4GB = $12
GetFreeSpace = $93
DisplayInformationUnder4GB = $0F
DisplayInformation = $94
DisplaySerialNumber = $2D
DisplayVolumeLabel = $2E
ListFileInformation = $2E
.endmodule
; ==========================================================================
; Reset
; --------------------------------------------------------------------------
; Resets the VDrive system to its default state.
; --------------------------------------------------------------------------
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
Reset:
call Serial.Reset
ret
; ==========================================================================
; SendCommandByte
; --------------------------------------------------------------------------
; Sends a single command byte followed by CR.
; --------------------------------------------------------------------------
; Inputs: A: Command byte to send.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
SendCommandByte:
call Serial.SendByte
ld a,'\r'
jp Serial.SendByte
; ==========================================================================
; SendCommandString
; --------------------------------------------------------------------------
; Sends a command byte followed by a space, a string parameter, then CR.
; --------------------------------------------------------------------------
; Inputs: A: Command byte to send.
; HL: Pointer to string argument to send.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
SendCommandString:
push hl
call Serial.SendByte
ld a,' '
call Serial.SendByte
-: pop hl
ld a,(hl)
inc hl
or a
jr z,+
cp '\r'
jr z,+
push hl
call Serial.SendByte
jr -
+: ld a,'\r'
jp Serial.SendByte
; ==========================================================================
; SendCommandInt
; --------------------------------------------------------------------------
; Sends a command byte followed by a space, a 32-bit integer, then CR.
; --------------------------------------------------------------------------
; Inputs: A: Command byte to send.
; DEHL: 32-bit integer to send.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
SendCommandInt:
push hl
push de
call Serial.SendByte
ld a,' '
call Serial.SendByte
pop af
push af
call Serial.SendByte
pop de
ld a,e
call Serial.SendByte
pop af
push af
call Serial.SendByte
pop hl
ld a,l
call Serial.SendByte
ld a,'\r'
jp Serial.SendByte
; ==========================================================================
; CheckForPrompt
; --------------------------------------------------------------------------
; Checks that we've received the prompt.
; --------------------------------------------------------------------------
; Outputs: F: Z set if the response was a '>' prompt, NZ if not.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
CheckForPrompt:
ld a,'>'
; Fall-through to CheckCommandResponseByte
; ==========================================================================
; CheckCommandResponseByte
; --------------------------------------------------------------------------
; Gets a command response byte, checks that is it valid, then checks that it
; is followed by CR.
; --------------------------------------------------------------------------
; Inputs: A: Command response byte to check for.
; Outputs: F: Z set if the response byte matched, NZ if not.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
CheckCommandResponseByte:
call GetAndCheckByte
jr nz,FlushSerialToCR
ld a,'\r'
call GetAndCheckByte
ret z
; Fall-through to FlushSerialToCR
; ==========================================================================
; FlushSerialToCR
; --------------------------------------------------------------------------
; Read from the serial port until CR or no data is received.
; --------------------------------------------------------------------------
; Destroyed: BC, DE, HL.
; ==========================================================================
FlushSerialToCR:
push af
-: ld de,1
push bc
call Serial.GetByteWithTimeout
pop bc
jr nz,+
cp '\r'
jr z,+
ld b,c
ld c,a
jr -
+: pop af
ret
; ==========================================================================
; GetByteSkipCR
; --------------------------------------------------------------------------
; Read from the serial port, but ignore the byte if it's CR.
; --------------------------------------------------------------------------
; Outputs: F: Z if a non-CR byte was received, NZ otherwise.
; A: The non-CR byte that was read.
; Destroyed: BC, DE, HL.
; ==========================================================================
GetByteSkipCR:
call Serial.GetByte
ret nz
cp '\r'
jr z,GetByteSkipCR
cp a
ret
; ==========================================================================
; GetAndCheckByte
; --------------------------------------------------------------------------
; Gets a single byte from the serial port and checks the value.
; --------------------------------------------------------------------------
; Inputs: A: Response byte to check.
; Outputs: F: Z if the expected value was received, NZ if not.
; C: The value that was received.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
GetAndCheckByte:
push af
call Serial.GetByte
pop bc
ld c,a
ret nz
cp b
ret
; ==========================================================================
; CheckEcho
; --------------------------------------------------------------------------
; Checks to see if the drive is responding to echo requests.
; --------------------------------------------------------------------------
; Inputs: A: Echo type ('E' or 'e').
; Outputs: F: Z if the echo was received, NZ if not.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
CheckEcho:
push af
push af
call SendCommandByte
pop af
call CheckCommandResponseByte
jr z,GotFirstEcho
pop bc
ret
GotFirstEcho:
; VDAP2 firmware seems to have a bug where it responds to echo twice.
ld de,1
call Serial.GetByteWithTimeout
jr nz,NoDoubleEcho
pop bc
cp b
ret nz
call FlushSerialToCR
ret
NoDoubleEcho:
pop bc
xor a
ret
; ==========================================================================
; Sync
; --------------------------------------------------------------------------
; Synchronises the VDrive device, ready to send a command.
; --------------------------------------------------------------------------
; Outputs: F: Z set on success, NZ on failure.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
Sync:
; Hopefully one sync attempt will be enough.
call SingleSyncAttempt
ret z
.bcall "VDU.BeginBlinkingCursor"
ld b,10
SyncRepeatedAttempt:
push bc
call SingleSyncAttempt
pop bc
jr nz,SyncFailed
.bcall "VDU.EndBlinkingCursor"
xor a
ret
SyncFailed:
push bc
.bcall "VDU.DrawBlinkingCursor"
ei
halt
call Host.CheckEscape
pop bc
djnz SyncRepeatedAttempt
.bcall "VDU.EndBlinkingCursor"
xor a
dec a
ret
; ==========================================================================
; SingleSyncAttempt
; --------------------------------------------------------------------------
; Performs a single synchronisation attempt.
; --------------------------------------------------------------------------
; Outputs: F: Z set on success, NZ on failure.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
SingleSyncAttempt:
ld a,'\r'
call Serial.SendByte
; Flush any incoming data.
-: ld de,1
call Serial.GetByteWithTimeout
jr z,-
; Send E for Echo and check we get E back.
ld a,'E'
call CheckEcho
ret nz
; Switch to the short command set and check we get a > prompt back.
ld a,Commands.ShortCommandSet
call SendCommandByte
call CheckForPrompt
ret nz
; Switch to binary mode.
ld a,Commands.MonitorBinary
call SendCommandByte
call CheckForPrompt
ret
; ==========================================================================
; SyncOrDeviceFault
; --------------------------------------------------------------------------
; Synchronises the VDrive device, ready to send a command, or triggers a
; device fault error if the VDrive could not be synchronised.
; --------------------------------------------------------------------------
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
SyncOrDeviceFault:
call Sync
ret z
jp Host.DeviceFault
; ==========================================================================
; TriggerDirectoryError
; --------------------------------------------------------------------------
; Triggers a directory-related error based on an error code.
; --------------------------------------------------------------------------
; Inputs: BC: Error code.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
TriggerDirectoryError:
ld hl,'C'*256+'F'*1
or a
sbc hl,bc
jr nz,TriggerError
ld bc,'F'*256+'I'*1
; Fall-through to TriggerError
; ==========================================================================
; TriggerError
; --------------------------------------------------------------------------
; Triggers an error based on an error code.
; --------------------------------------------------------------------------
; Inputs: BC: Error code.
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
TriggerError:
ld hl,Errors
ld a,(hl)
-: inc hl
cp b
ld a,(hl)
inc hl
jr nz,+
cp c
jr nz,+
ld e,(hl)
inc hl
ld d,(hl)
ex de,hl
jp (hl)
+: inc hl
inc hl
ld a,(hl)
or a
jr nz,-
jp Host.DeviceFault
Errors:
.db "BC" \ .dw Host.DeviceFault
.db "CF" \ .dw File.FileNotFound
.db "DF" \ .dw File.DiskFull
.db "FI" \ .dw File.BadDirectory
.db "RO" \ .dw File.AccessDenied
.db "FO" \ .dw File.TooManyOpenFiles
.db "NE" \ .dw File.BadDirectory
.db "FN" \ .dw File.FileNotFound
.db "ND" \ .dw File.DiskFault
.db 0
; ==========================================================================
; Catalogue
; --------------------------------------------------------------------------
; Displays a file and directory listing.
; --------------------------------------------------------------------------
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
Catalogue:
call SyncOrDeviceFault
; Request a directory listing.
ld a,Commands.ListDirectory
call SendCommandByte
; CR before the file listing.
ld a,'\r'
call GetAndCheckByte
jr z,+
call FlushSerialToCR
jp TriggerError
+:
-: call Serial.GetByte
jp nz,Host.DeviceFault
; Is it the end of the file list?
cp '>'
jp z,FlushSerialToCR
.bcall "VDU.PutChar"
jr -
ret
; ==========================================================================
; ValidateDirectoryName
; --------------------------------------------------------------------------
; Checks that a directory name is valid.
; --------------------------------------------------------------------------
; Inputs: HL: The directory name to validate.
; Outputs: F: Z set if the directory name is valid, NZ if it is not.
; Destroyed: AF.
; ==========================================================================
ValidateDirectoryName:
ld a,(hl)
cp '.'
jr z,ValidateDirectoryName.Dot
cp '/'
jr z,ValidateDirectoryName.Slash
jr ValidateFilename
ValidateDirectoryName.Dot:
push hl
push bc
; Check for a maximum of 2 dots (+terminator).
ld b,2+1
-: ld a,(hl)
call File.NormaliseFilenameCharacter
or a
jr z,+
cp '.'
jr nz,+
inc hl
djnz -
dec b
+:
pop bc
pop hl
ret
ValidateDirectoryName.Slash:
push hl
inc hl
ld a,(hl)
call File.NormaliseFilenameCharacter
or a ; Slash MUST be followed by a terminator.
pop hl
ret
; ==========================================================================
; ValidateDirectoryNameOrBadDirectory
; --------------------------------------------------------------------------
; Checks that a directory name is valid, or triggers "Bad directory" if not.
; --------------------------------------------------------------------------
; Inputs: HL: The directory name to validate.
; Destroyed: AF.
; ==========================================================================
ValidateDirectoryNameOrBadDirectory:
call ValidateDirectoryName
ret z
jp File.BadName
; ==========================================================================
; ValidateFilename
; --------------------------------------------------------------------------
; Checks that a filename is valid.
; --------------------------------------------------------------------------
; Inputs: HL: The filename to validate.
; Outputs: F: Z set if the file name is valid, NZ if it is not.
; Destroyed: AF.
; ==========================================================================
ValidateFilename:
; Quick check for filenames that start with a dot.
; Technically seem to be allowed as hidden files, but not documented!
ld a,(hl)
cp '.'
jr nz,+
or a
ret
+: push hl
push bc
; Check the part before the extension.
ld b,8+1
call ValidateFilenamePart
jr z,+
; There's an error. Was it because of a '.'?
cp '.'
jr nz,+
; Check the extension, if there was a '.'
inc hl
ld b,3+1
call ValidateFilenamePart
+:
pop bc
pop hl
ret
ValidateFilenamePart:
; The filename part cannot be empty.
ld a,(hl)
call File.NormaliseFilenameCharacter
or a
jr nz,+
ld b,a
inc a
ret
+:
; Check each character up to the maximum length.
-: ld a,(hl)
call File.NormaliseFilenameCharacter
or a
ret z
; Check that the character is valid.
push hl
push bc
ld hl,ValidCharacters
ld bc,ValidCharacters.Count
cpir
pop bc
pop hl
ret nz
inc hl
djnz -
; If we get this far, the part is too long.
dec b
ret
ValidCharacters:
.db "$%'-_@~`!(){}^#&ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
ValidCharacters.Count = $-ValidCharacters
; ==========================================================================
; ValidateFilenameOrBadName
; --------------------------------------------------------------------------
; Checks that a filename is valid, and triggers a "Bad name" error if not.
; --------------------------------------------------------------------------
; Inputs: HL: The filename to validate.
; Destroyed: AF.
; ==========================================================================
ValidateFilenameOrBadName:
call ValidateFilename
ret z
jp File.BadName
; ==========================================================================
; ChangeDirectory
; --------------------------------------------------------------------------
; Changes the current directory.
; --------------------------------------------------------------------------
; Inputs: HL: Pointer to new directory name NUL/CR terminated.
; Destroyed: AF, BC, DE, HL.
; Interrupts: Disabled.
; ==========================================================================
ChangeDirectory:
call ValidateDirectoryNameOrBadDirectory
call SyncOrDeviceFault
ld a,Commands.ChangeDirectory
call SendCommandString
call CheckForPrompt
ret z
jp TriggerDirectoryError
; ==========================================================================
; GetFileSize
; --------------------------------------------------------------------------
; Gets a size of a file from the VDrive.
; --------------------------------------------------------------------------
; Inputs: HL: Pointer to file name NUL/CR terminated.
; Outputs: F: NZ if there was an error.
; DEHL: Size of the file if there was no error.
; Destroyed: AF, BC, DE, HL.
; Interrupts: Disabled.
; ==========================================================================
GetFileSize:
push hl
call Sync
pop hl
ret nz
ld a,Commands.ListDirectory
push hl
call SendCommandString
; Check we get the supplied file name back.
push bc
call GetByteSkipCR
pop bc
ld c,a
-: pop hl
ret nz
ld b,c
ld c,a
cp ' '
jr z,GotFileName
call File.NormaliseFilenameCharacter
ld d,a
ld a,(hl)
call File.NormaliseFilenameCharacter
cp d
inc hl
jp nz,FlushSerialToCR
push hl
call Serial.GetByte
jr -
GotFileName:
; After the file name, four bytes of file size data.
ld b,4
ld hl,VDU.TempTile
-: push hl
push bc
call Serial.GetByte
pop bc
pop hl
ret nz
ld (hl),a
inc hl
djnz -
call Serial.GetByte
ret nz
cp '\r'
jp nz,FlushSerialToCR
ld hl,(VDU.TempTile+0)
ld de,(VDU.TempTile+2)
ret
; ==========================================================================
; GetFile
; --------------------------------------------------------------------------
; Gets a file from the VDrive.
; --------------------------------------------------------------------------
; Inputs: HL: Pointer to file name NUL/CR terminated.
; DE: Pointer to RAM to store the file in.
; BC: Maximum file size that can be loaded.
; Outputs: F: NZ if there was a protocol/receive error.
; If no error, C is set if the transfer was cancelled.
; If there is an error, C is set if the error is "No room".
; BC: Actual size of the loaded file.
; Destroyed: AF, BC, DE, HL.
; Interrupts: Disabled.
; ==========================================================================
GetFile:
call ValidateFilenameOrBadName
ld (TempPtr),hl
ld (TempCapacity),bc
ld (TempSize),de
; Get the file size.
ld hl,(TempPtr)
call GetFileSize
jp nz,TriggerError
; Ensure that files are under 64KB.
ld a,d
or e
jr z,+
scf
ret
+:
; Do we have enough room?
ld de,(TempCapacity)
or a
ex de,hl
sbc hl,de
ret c
; Store the actual size.
ld (TempCapacity),de
; Start reading the file.
call Sync
ret nz
ld a,Commands.ReadFile
ld hl,(TempPtr)
call SendCommandString
; Retrieve the size and capacity.
ld hl,(TempSize)
ld bc,(TempCapacity)
-: push hl
push bc
call Serial.GetByte
pop bc
pop hl
jr z,+
scf
ccf
ret
+: ld (hl),a
inc hl
dec bc
ld a,b
or c
jr nz,-
; Return the actual file size.
ld bc,(TempCapacity)
ret
; ==========================================================================
; WriteFile
; --------------------------------------------------------------------------
; Writes a file to the VDrive.
; --------------------------------------------------------------------------
; Inputs: HL: Pointer to the file name NUL/CR terminated.
; DE: Pointer to RAM to get the file data from.
; BC: Size of the file to write.
; Destroyed: AF, BC, DE, HL.
; Interrupts: Disabled.
; ==========================================================================
WriteFile:
call ValidateFilenameOrBadName
; We'll need to remember the file location and size.
ld (TempCapacity),hl
ld (TempPtr),de
ld (TempSize),bc
call SyncOrDeviceFault
; Open the file for writing.
ld a,Commands.OpenFileWriting
ld hl,(TempCapacity)
call SendCommandString
call CheckForPrompt
jp nz,TriggerError
; Seek to the start of the file.
ld a,Commands.Seek
ld de,0
ld hl,0
call SendCommandInt
call CheckForPrompt
jp nz,TriggerError
; How much data are we writing?
ld hl,(TempSize)
ld a,l
or h
jr z,WriteFile.Empty
ld de,0
ld a,Commands.WriteFileData
call SendCommandInt
; Write the file data.
ld de,(TempPtr)
ld bc,(TempSize)
-: ld a,(de)
inc de
push de
push bc
call Serial.SendByte
pop bc
pop de
dec bc
ld a,b
or c
jr nz,-
WriteFile.Empty:
; Close the file.
ld a,Commands.CloseFile
ld hl,(TempCapacity)
call SendCommandString
call CheckForPrompt
ret z
jp TriggerError
; ==========================================================================
; FileOpen
; --------------------------------------------------------------------------
; Opens a file.
; --------------------------------------------------------------------------
; Inputs: IX: Pointer to the file variable data, where
; IX+0: LSB of pointer to block storage data.
; IX+1: MSB of pointer to block storage data.
; IX+2: File system.
; IX+3: File status (0:closed, 1:OPENOUT, 2:OPENIN, 3:OPENUP).
; HL: Pointer to CR-terminated filename.
; Outputs: The file should be opened, if not IX+3 should be set to 0.
; Destroyed: AF, HL.
; ==========================================================================
FileOpen:
call ValidateFilename
jr z,FileOpen.FilenameApproved
ld (ix+3),0
jp File.BadName
FileOpen.FilenameApproved:
; Data storage for opened files:
; 16 bytes of filename (CR-terminated)
; 4 bytes for current EXT#
; 4 bytes for current PTR#
; 4 bytes for block address currently fetched into RAM
; 256 bytes block data storage
ld e,(ix+0)
ld d,(ix+1)
ld bc,16
push hl
ldir
; Clear EXT# and PTR#
xor a
ld (de),a
ld l,e
ld h,d
inc de
ld bc,8
ldir
; Set block address to -1
dec a
ld (hl),a
ld bc,4
ldir
pop hl
bit 1,(ix+3)
jr nz,FileOpen.Read
bit 0,(ix+3)
jr nz,FileOpen.Write
ld (ix+3),0
ret
FileOpen.Read:
; OPENIN or OPENUP require that the file already exists.
call GetFileSize
jr z,+
ld (ix+3),0
ret
+: ; DEHL = file size
push hl
ld l,(ix+0)
ld h,(ix+1)
ld bc,16+3
add hl,bc
ld (hl),d
dec hl
ld (hl),e
dec hl
pop de
ld (hl),d
dec hl
ld (hl),e
ret
FileOpen.Write:
; OPENOUT will create a new blank file.
; Pretend the write fails.
ld a,(ix+3)
push af
ld (ix+3),0
; Write a dummy blank file.
ld bc,0
call WriteFile
; Now restore the file mode.
pop af
ld (ix+3),a
ret
; ==========================================================================
; FileClose
; --------------------------------------------------------------------------
; Close a file previously opened with FileOpen.
; --------------------------------------------------------------------------
; Inputs: IX: Pointer to the file variable data, where
; IX+0: LSB of pointer to block storage data.
; IX+1: MSB of pointer to block storage data.
; IX+2: File system.
; IX+3: File status (0:closed, 1:OPENOUT, 2:OPENIN, 3:OPENUP).
; Destroyed: AF, BC, DE, HL.
; ==========================================================================
FileClose:
bit 1,(ix+3)
jr nz,FileClose.Read
FileClose.Write:
bit 7,(ix+3)
jr z,FileClose.NotDirty
; Pretend we're about to fetch some new data by setting PTR#=-1
ld l,(ix+0)
ld h,(ix+1)
ld de,16+4
add hl,de
ld b,4
-: ld (hl),$FF
inc hl
djnz -
; Mark the file as closed so we don't actually fetch any data.
res 0,(ix+3)
res 1,(ix+3)
; Update the current block to flush the data to disk.
call UpdateCurrentBlock
FileClose.NotDirty:
ld (ix+3),0
ret
FileClose.Read: