-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBBCDOS.C
1559 lines (1027 loc) · 32.8 KB
/
BBCDOS.C
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
/* ******************************************************************** *\
* *
* Title : BBC/DOS File I/O For Protected Mode Monitor. *
* Created : 25/10/92 *
* Author : Tony Hanratty *
* *
* *
* Written to allow copying of files *FROM* BBC disks *TO* IBM disks. *
* Only one file can be 'open' at a time to make my life simpler. *
* *
* N.B. The processor is in protected mode and there is no DOS in the *
* machine so you cant call any C library functions that use DOS *
* or the BIOS. So if you want to do anything ether do it yourself *
* or ask the monitor to do it for you. *
* *
* N.B. These functions must ONLY be call from MONBBC.ASM which does *
* all the necessary interfacing between the C and assembler. To *
* read BBC files or write DOS files, use the assembler routines *
* in MONBBC.ASM, done call these directly. *
* *
\* ******************************************************************** */
/*
(This bit is to remind me how it works...)
BBC files are read as sectors because the files are stored contiguously
so you just have to find the start sector and keep going till you have
read it all. The DOS output on the other hand is more akin to very simple
file handling due to the FAT being written for each file created. Physical
output to the DOS disk is not sector but cluster based.
Presume BBC filelength <= 65535 bytes
Presume BBC load & exec addresses <= FFFF (see BBC tech info)
*/
/* Programme constants first
*/
#define DOS_DRIVE 1 /* hard coded for now, must */
#define BBC_DRIVE 0 /* move to config menu */
#define BBC_SEC_TRK 10 /* BBC physical secs/track */
#define BBC_NUM_HEADS 1 /* each side is a separate disk */
#define BBC_SECTOR_SIZE 256
#define MAX_BBC_FILES 31
#define FREE_CLUSTER 0x0000 /* FAT cluster entries */
#define LAST_CLUSTER 0xFFF8
/* Return error code definitions
*/
#define CE_NO_ERROR 0 /* >>MUST<< be zero */
#define CE_FILE_OPEN 1 /* 1 file at a time */
#define CE_NOFILE_OPEN 2
#define CE_MAX_FILES 3 /* too many files */
#define CE_DOS_READ_ERROR 4
#define CE_DOS_WRITE_ERROR 5
#define CE_BBC_READ_ERROR 6
#define CE_BBC_WRITE_ERROR 7
#define CE_DOS_FAT_WRITE_ERR 8
#define CE_DOS_ROOT_WRITE_ERR 9
#define CE_BBC_CATALOG_ERR 10 /* Xlinked BBC files */
#define CE_ALLOC_ERR 11 /* cant alloc mem */
#define CE_DEALLOC_ERR 12 /* cant dealloc mem */
#define CE_BREAK_ERR 13
/* A few handy typedefs
*/
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long DWORD;
/* A few handy debugging macros
*/
#define dbgmessval(mess,aword) { asm_pstring(mess); \
asm_phexword(aword); \
C_pcrlf(1); }
#define dbgmess(mess) { asm_pstring(mess); }
#define dbgmessaddr(mess,addr) { asm_pstring(mess);\
asm_phexword(* ( (WORD *) addr+1));\
asm_pchar(':');\
asm_phexword(* ( (WORD *) addr));\
C_pcrlf(1);}
/* BBC linked sector list node structure for checking
* for cross linked files or nonsense catalog entries.
*/
typedef struct {
unsigned int catnum;
unsigned int start_sector;
unsigned int end_sector;
} BBCSectorNode;
/* DOS Boot sector structure
*/
typedef struct {
BYTE jump_instruction [3];
char oem_name [8];
WORD bytes_per_sector;
BYTE sectors_per_cluster;
WORD reserved_sectors;
BYTE no_of_FATs;
WORD no_of_root_entries;
WORD total_sectors;
BYTE media_descriptor;
WORD sectors_per_FAT;
WORD sectors_per_track;
WORD no_of_heads;
WORD no_of_hidden_sectors;
BYTE loader_routine [512 - 0x1E];
} DOS_BOOT_SECTOR;
/* DOS Directory Entry Structure
*/
typedef struct {
char filename [11];
BYTE attribute;
BYTE reserved [10];
WORD time;
WORD date;
WORD first_cluster;
DWORD filesize;
} DIRECTORY_ENTRY;
/* BBC Sector 0 part structure
*/
typedef struct {
char BBCfilename0 [7];
BYTE BBCdirectory;
} BBCdirentry0;
/* BBC Sector 1 part structure
*/
typedef struct {
WORD BBCload;
WORD BBCexec;
WORD BBClength;
BYTE BBChibits;
BYTE BBCstartsector;
} BBCdirentry1;
/* Combine BBC secs 0 & 1 into following 512 byte, 2 sector structure
*/
typedef struct {
char BBCdiskname0 [8];
BBCdirentry0 BBCfiles0 [31];
char BBCdiskname1 [4];
BYTE BBCsequence;
BYTE BBCnumfiles;
BYTE BBCtotsecsHi_OPT;
BYTE BBCtotsecsLo;
BBCdirentry1 BBCfiles1 [31];
} BBC_BOOT;
/* Local Function Prototypes
*/
int alloc_DOS_boot (void);
int alloc_BBC_boot (void);
int alloc_cluster_buffer (void);
int alloc_disk_buffer (void);
int wipe_DOS_root (void);
int write_DOS_root (int);
int write_DOS_FATs (void);
int read_DOS_abs_sectors (int, int, BYTE far *);
int write_DOS_abs_sectors (int, int, BYTE far *);
int output_DOS_sector (BYTE far *);
int output_DOS_cluster (void);
int init_DOS_disk (void);
int inti_BBC_disk (void);
int open_DOS_file (char far *, DWORD);
int copy_file_contents (unsigned int, unsigned int);
int close_DOS_file (void);
void C_pcrlf (int);
void C_pspace (int);
void C_pstring_n (char far *, int);
void C_dealloc_buffers (void);
void pBBCDiskInfo (void);
int CheckNodeTree (int);
void FillNodeTree (int);
void SortNodeTree (int);
void pCatalog (int);
void SwapNodes (int,int);
void pNodeDetail (int);
void pNodesError (int);
/* External Assembler Function Prototypes (in the monitor file MONBBC.ASM)
*/
/* Dsk C H S # buff */
extern int read_DOS_CHS (int, int,int,int, int, BYTE far *);
extern int write_DOS_CHS (int, int,int,int, int, BYTE far *);
extern int read_BBC_CHS (int, int,int,int, int, BYTE far *);
extern void asm_pstring (char *);
extern void asm_pchar (char);
extern void asm_phexbyte (BYTE);
extern void asm_phexword (WORD);
extern char asm_gkey (void);
/* Global Buffers For DOS (all near pointers to far data items)
*
* DIRECTORY_ENTRY DOSrootdir [no_of_root_files] = {0};
* int clusters [max_cluster_no] = {0};
* DOS_BOOT_SECTOR DOSbootsector = {0};
* BYTE twelve_bit [max_cluster_no * 3 / 2) + 1] = {0};
* BYTE clusterbuffer [4096] = {0};
*
* N.B. THESE FAR POINTERS ARE 'SELECTOR:OFFSET', NOT 'SEGMENT:OFFSET'. WE
* CALL THE MONITOR 16M MEMORY MANAGER ROUTINES TO ALLOCATE MEMORY WHICH
* RETURNS A SELECTOR INTO THE GDT. SO ANY POINTER ARITHMATIC MUST ONLY
* OPERATE ON THE OFFSET PORTION, NOT THE GDT SELECTOR OTHERWISE YOU'LL
* GENERATE A GENERAL PROTECTION FAILURE AND WARM BOOT THE MONITOR.
*
* All globals must be initialised so they appear in the correct data segment.
* Unfortunately the Aztec C compiler uses non-standard segment names, so these
* are grouped together with the main monitor segments into one unified code
* segment and one unified data segment, namely DGROUP and PGROUP.
*
*/
DIRECTORY_ENTRY far * DOSrootdir = (far *) 0;
int far * clusters = (far *) 0;
DOS_BOOT_SECTOR far * DOSbootsector = (far *) 0;
BYTE far * twelve_bit = (far *) 0;
BYTE far * clusterbuffer = (far *) 0;
/* Globale variables for DOS
*/
int DOS_file_open = 0;
int no_of_files = 0;
int this_cluster_no = 2;
int sector_number = 0;
int no_of_clusters = 0;
int max_cluster_no = 0;
/* Global Variables For BBC
*/
BBC_BOOT far *BBCboot = (far *) 0;
BBCSectorNode SectorNodes [MAX_BBC_FILES] = {0};
BYTE BBCside = 0;
/* Global common variables/buffers for DOS & BBC
*/
BYTE far *DiskBuffer = (far *) 0; /* allocated by mem manager */
BYTE show_DCHS = 0; /* show low level disk I/O */
WORD Selectors [20] = {0}; /* allocated memory block selectors */
int selectors_allocated = 0; /* # entries in selector array */
char upper (ch) /* convert char to uppercase */
char ch;
{
if (ch>='a' && ch<='z')
return (ch & 0xdf);
return (ch);
}
char GetYN()
{
char akey;
for (akey=' ' ; akey != 'Y' && akey != 'N' ; akey=upper(asm_gkey()) );
asm_pchar (akey );
return ( akey );
}
/****************************************************************************\
*
* Top Level Routine - Called From The Monitor
*
\****************************************************************************/
void C_BeebCopyTop()
{
int rcode;
WORD selector;
/* Reset global selector array
*/
selectors_allocated = 0;
/* 1st allocated the memory blocks that are independent of DOS/BBC disk sizes
*/
if (rcode=alloc_DOS_boot())
asm_pstring("\n\rCant allocate memory for DOS boot sector\n\r");
else if (rcode=alloc_BBC_boot())
asm_pstring("\n\rCant allocate memory for BBC boot sector\n\r");
else if (rcode=alloc_disk_buffer())
asm_pstring("\n\rCant allocate memory for disk buffer\n\r");
else {
asm_pstring("Static buffers allocated\n\r");
rcode = C_BeebCopy();
}
asm_pstring("\n\rReturn code=");
asm_phexword (rcode);
C_pcrlf (1);
C_dealloc_buffers();
}
int C_BeebCopy ()
{
BYTE akey;
int rcode;
asm_pstring ("\n\rSelect BBC disk side 0 or 1 : ");
for (akey=7 ; akey>1 ; akey=asm_gkey()-'0') ;
BBCside = akey;
asm_pchar (akey+'0');
asm_pstring ("\n\rShow disk access info (Y/N) ? : ");
show_DCHS = ( GetYN() == 'Y' ? 1 : 0 );
asm_pstring ("\n\n\rPlace BBC disk in drive ");
asm_pchar ('A'+BBC_DRIVE);
asm_pstring (": Press any key to continue...");
asm_gkey();
C_pcrlf (1);
if (rcode=init_BBC_disk()) {
asm_pstring("\n\rCant read BBC catalog\n\r\007");
return ( rcode );
}
if (rcode=C_copy_option()) {
asm_pstring("\n\rCopy Failed... ");
asm_phexword(rcode);
C_pcrlf(1);
}
return ( rcode );
}
/****************************************************************************\
*
* Little Farty Routines Up At The Top Here
*
\****************************************************************************/
void C_pcrlf(n) /* Print CR/LF 'n' Times */
int n;
{ while (n--) asm_pstring("\n\r"); }
void C_pspace(n) /* Print 'n' spaces */
int n;
{ while (n--) asm_pchar(' '); }
void C_pstring_n (str, n) /* Print n chars of a string */
char far *str; int n;
{ while (n--) asm_pchar(*str++); }
/* tstst
BYTE far *aptr = { (BYTE far *) 0};
*/
void memset (addr, size, fill) /* Fill memory with a byte */
BYTE far *addr;
BYTE fill;
int size;
{
/* tstst
WORD aword;
asm_pstring("memset called with address = ");
aword = * ( (WORD far *) &addr+1);
asm_phexword(aword);
asm_pchar(':');
aword = * ( (WORD far *) &addr);
asm_phexword(aword);
C_pcrlf(1);
*/
if (size) while (size--) *addr++ = fill; }
void memcpy (source, dest, size) /* copy memory */
BYTE far *source;
BYTE far *dest;
int size;
{ if (size) while (size--) *dest++ = *source++; }
/****************************************************************************\
*
* Convert All Chars In A DOS Filename To Uppercase
*
\****************************************************************************/
void upDOSfile(fp)
char *fp;
{
int charnum;
/* DOS filenames are 12 bytes long, so count from 0 to 11
*/
for (charnum=0 ; charnum<11 ; charnum++)
*(fp+charnum) = upper( *(fp+charnum) );
}
/****************************************************************************\
*
* Write_DOS_fats - write out the FATs to the DOS disk
*
\****************************************************************************/
int write_DOS_FATs ()
{
int fat_no, pair_no, cluster;
int left, right;
BYTE *fp;
int rcode;
DOS_BOOT_SECTOR far *bsp = DOSbootsector;
/* Mark all remaining clusters as unused
*/
for (cluster = this_cluster_no ; cluster <= max_cluster_no ; cluster++)
clusters [cluster] = FREE_CLUSTER;
twelve_bit [0] = bsp->media_descriptor;
twelve_bit [1] = 0xFF;
twelve_bit [2] = 0xFF;
for (pair_no = 1; pair_no < no_of_clusters / 2; pair_no++) {
left = clusters [pair_no*2] & 0xFFF;
right = clusters [pair_no*2+1] & 0xFFF;
twelve_bit [pair_no*3+0] = left;
twelve_bit [pair_no*3+1] = (left >> 8) + ((right & 0x0F) << 4);
twelve_bit [pair_no*3+2] = right >> 4;
}
for (fat_no = 0 ; fat_no < bsp->no_of_FATs ; fat_no++) {
rcode = write_DOS_abs_sectors (1 + fat_no * bsp->sectors_per_FAT,
bsp->sectors_per_FAT,
twelve_bit);
if (rcode) return (rcode);
}
return ( CE_NO_ERROR );
}
/****************************************************************************\
* Write The Root Directory Out Onto The DOS Disk
*
* LOCAL routine
\****************************************************************************/
int write_DOS_root (files_left)
int files_left;
{
DOS_BOOT_SECTOR far *bsp = DOSbootsector;
int start_sec, num_secs;
start_sec = 1 + bsp->no_of_FATs * bsp->sectors_per_FAT;
num_secs = (bsp->no_of_root_entries*sizeof(DIRECTORY_ENTRY) ) / bsp->bytes_per_sector ;
return ( write_DOS_abs_sectors (start_sec, num_secs, (BYTE far *) DOSrootdir) );
}
int wipe_DOS_root()
{
int numsecs, startsec, sec_count, rcode;
DOS_BOOT_SECTOR far *bsp = DOSbootsector;
memset ( DiskBuffer, 512, 0 );
startsec = 1 + bsp->no_of_FATs * bsp->sectors_per_FAT;
numsecs = bsp->no_of_root_entries*sizeof(DIRECTORY_ENTRY) / bsp->bytes_per_sector;
for (sec_count=0 ; sec_count<numsecs ; sec_count++)
if ( rcode=write_DOS_abs_sectors (startsec+sec_count, 1, DiskBuffer) )
break;
return ( rcode ? rcode : CE_NO_ERROR );
}
/****************************************************************************\
* Take a formatted DOS disk and wipe the FATs and directory ready
* for file creation. Read in its bootsector for BPB values.
*
* Any non zero return value is a fault
*
* PUBLIC routine called in MONBBC.ASM
\****************************************************************************/
int init_DOS_disk()
{
int sector, fat, cluster, rcode;
DOS_BOOT_SECTOR far *bsp = DOSbootsector;
/* tstst */
dbgmess("In init_DOS_disk()\n\r");
/* must reset global vars first
*/
DOS_file_open = 0;
no_of_files = 0;
this_cluster_no = 2;
sector_number = 0;
/* read in the boot sector for disk BPB
*/
if ( read_DOS_CHS (DOS_DRIVE, 0,0,1, 1, (BYTE far *) bsp) )
return ( CE_DOS_READ_ERROR );
/* now allocate the size dependent buffers
*/
if (rcode = alloc_sized_buffers() )
return ( rcode );
/* Now wipe internal buffers and arrays
*/
memset ( (BYTE far *) clusterbuffer ,
bsp->sectors_per_cluster * bsp->bytes_per_sector ,
(BYTE) 0 );
memset ( (BYTE far *) DOSrootdir ,
bsp->no_of_root_entries * sizeof(DIRECTORY_ENTRY) ,
(BYTE) 0 );
for (cluster=2 ; cluster <= max_cluster_no ; cluster++)
clusters [cluster] = 0;
/* Now physically blank the FATs and the root on the disk
*/
if ( write_DOS_FATs() )
return ( CE_DOS_FAT_WRITE_ERR );
if ( wipe_DOS_root() )
return ( CE_DOS_ROOT_WRITE_ERR );
return ( CE_NO_ERROR );
}
/****************************************************************************\
* Creates an entry in the root directory for a new file. The directory
* and FATs are written when the file is closed. The filename is passed
* in BBC format, 7 bytes long, and the 8th byte is the BBC catalog
* directory. In DOS we use the directory as the file extension to advoid
* duplicate file names. No physical write occurs to disk in this routine.
*
* LOCAL
\****************************************************************************/
int open_DOS_file (fname, flen)
char far *fname;
DWORD flen;
{
int namelen;
char far *nameptr;
if (DOS_file_open)
return ( CE_FILE_OPEN );
if (no_of_files == DOSbootsector->no_of_root_entries-1)
return ( CE_MAX_FILES );
/* copy 7 byte BBC file name into DOS directory, pad with spaces to 11 chars
* and make uppercase because BBC filenames can be have lowercase chars.
*/
nameptr = DOSrootdir [no_of_files].filename; /* point to DOS dir entry */
memset (nameptr, 11, 32); /* fill name with spaces */
memcpy (fname, nameptr, 7); /* copy in BBC file name */
nameptr[8] = upper((char)(fname[7] & 0x7f)); /* extension=BBC directory */
upDOSfile (nameptr); /* make sure uppercase */
/* fill in rest of DOS dir entry now
*/
DOSrootdir [no_of_files].filesize = flen;
DOSrootdir [no_of_files].time = 0;
DOSrootdir [no_of_files].date = 0;
DOSrootdir [no_of_files].first_cluster = this_cluster_no;
DOSrootdir [no_of_files].attribute = 0x20;
/* zero the cluster buffer
*/
memset ( (BYTE far *) clusterbuffer ,
DOSbootsector->sectors_per_cluster*DOSbootsector->bytes_per_sector,
(BYTE) 0 );
/* init cluster sector counter to 0 & flag file open
*/
sector_number=0;
DOS_file_open++;
return ( CE_NO_ERROR );
}
/****************************************************************************\
* Append sector to cluster buffer. Physical write if a complete DOS cluster.
*
* LOCAL routine
\****************************************************************************/
int output_DOS_sector (dataptr)
BYTE far *dataptr;
{
BYTE far *dp;
int bcnt, rc;
/* copy data into clusterbuffer
*/
dp = clusterbuffer + (DOSbootsector->bytes_per_sector*sector_number);
for (bcnt = 0 ; bcnt < DOSbootsector->bytes_per_sector ; bcnt++ )
*dp++ = *dataptr++;
/* If we have a full cluster, write it to the disk. The FAT is updated
* when the file is closed.
*/
if (++sector_number == DOSbootsector->sectors_per_cluster) {
sector_number = 0;
return (output_DOS_cluster());
}
return ( CE_NO_ERROR );
}
/****************************************************************************\
* Output cluster buffer to next free DOS cluster. ie Append to DOS file.
*
* LOCAL routine
\****************************************************************************/
int output_DOS_cluster()
{
int spc = DOSbootsector->sectors_per_cluster;
int rcode, abs_sec, data_sector_start;
DOS_BOOT_SECTOR far *dbs = DOSbootsector;
/* make sure a file is open
*/
if ( DOS_file_open == 0 )
return ( CE_NOFILE_OPEN );
/* calculate abs logical sector number of this cluster
*/
data_sector_start = dbs->no_of_root_entries * sizeof(DIRECTORY_ENTRY) / dbs->bytes_per_sector;
data_sector_start += (dbs->sectors_per_FAT * dbs->no_of_FATs + 1);
abs_sec = (this_cluster_no-2)*spc + data_sector_start;
/* flag this cluster as used and link to next
*/
clusters [this_cluster_no] = this_cluster_no+1;
this_cluster_no++;
/* ask for physical write to disk
*/
rcode = write_DOS_abs_sectors (abs_sec, spc, clusterbuffer);
return ( rcode );
}
/****************************************************************************\
* Call monitor for physical write to DOS disk at absolute sector number.
* Passed start sector number, # sectors to write, buffer address.
*
* LOCAL routine
\****************************************************************************/
int write_DOS_abs_sectors (abs_sec, numsecs, buffer)
int abs_sec, numsecs;
BYTE far *buffer;
{
int cyl, head, sec;
int spt = DOSbootsector->sectors_per_track,
noh = DOSbootsector->no_of_heads;
/* calculate cylinder, head, sector disk address
*/
sec = (abs_sec % spt) + 1;
head = (abs_sec / spt) % noh;
cyl = abs_sec / (noh*spt);
/* call monitor for physical write
*/
if ( write_DOS_CHS (DOS_DRIVE, cyl,head,sec, numsecs, buffer) )
return ( CE_DOS_WRITE_ERROR );
else
return ( CE_NO_ERROR );
}
/****************************************************************************
* Read abs BBC logical sector number into buffer.
* N.B. Head number is patched in MONBBC.ASM !!
*
* LOCAL routine
****************************************************************************/
int read_BBC_abs_sectors (abs_sec, numsecs, buffer)
int abs_sec, numsecs;
BYTE far *buffer;
{
int cyl, head, sec;
/* calculate cylinder, head, sector
*/
sec = (abs_sec % BBC_SEC_TRK);
head = (abs_sec / BBC_SEC_TRK) % BBC_NUM_HEADS;
cyl = abs_sec / (BBC_SEC_TRK * BBC_NUM_HEADS);
/* ask monitor to do physical read
*/
if ( read_BBC_CHS (BBC_DRIVE, cyl,head,sec, numsecs, buffer) )
return ( CE_BBC_READ_ERROR );
else
return ( CE_NO_ERROR );
}
/****************************************************************************
* Close currently open DOS file. Flush cluster buffer to disk if it
* contains unwritten data.
*
* LOCAL routine
****************************************************************************/
int close_DOS_file()
{
int rcode;
if (DOS_file_open == 0)
return ( CE_NOFILE_OPEN );
/* tstst lots of these in this routine */
dbgmess("\n\rClosing DOS file\n\r");
/* flush out anything in the cluster buffer
*/
if (sector_number) {
asm_pstring("\n\rFlushing First\n\r");
if ( rcode=output_DOS_cluster() )
return ( rcode );
}
clusters [this_cluster_no-1] = LAST_CLUSTER;
DOS_file_open--;
no_of_files++;
dbgmess("Writing FATs\n\r"); /* tstst */
if ( rcode=write_DOS_FATs() )
return ( rcode );
dbgmess("Writing Root\n\r"); /* tstst */
if ( rcode=write_DOS_root(no_of_files) )
return ( rcode );
dbgmess("File Closed\n\r"); /* tstst */
return ( CE_NO_ERROR );
}
/****************************************************************************
* Read in directory sectors 0 & 1 to the BBC 'boot' structure.
*
* PUBLIC routine - called from MONBBC.ASM
****************************************************************************/
int init_BBC_disk()
{
int filenum, charnum;
char *nameptr;
/* tstst */
dbgmess("In init_BBC_disk\n\r");
/* zero the memory 1st */
memset ( (BYTE far *) BBCboot, sizeof(BBC_BOOT), (BYTE) 0 );
if ( read_BBC_CHS (BBC_DRIVE, 0,0,0, 2, (BYTE far *) BBCboot) )
return ( CE_BBC_READ_ERROR );
else
return ( CE_NO_ERROR );
}
/****************************************************************************
* Step through all files in the BBC disk catalog and copy them to the
* DOS disk.
*
* PUBLIC routine - called from MONBBC.ASM
****************************************************************************/
int C_copy_option()
{
unsigned int startsec, filelen;
char far *filename;
char akey;
BYTE directory;
unsigned int totfiles, filenum, rcode;
BBCdirentry1 far *dir1;
BBCdirentry0 far *dir0;
totfiles = BBCboot->BBCnumfiles >> 3;
pBBCDiskInfo();
pCatalog (totfiles);
if (rcode=CheckNodeTree (totfiles))
return ( rcode );
else
asm_pstring ("File allocation chain OK\n\r");