-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpm.cpp
1458 lines (1073 loc) · 45.1 KB
/
cpm.cpp
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
/**
@file cpm.cpp
@brief based on TRS-80 Virtual Disk Kit v1.7 for Windows by Miguel Dutra
Linux port VDK-80-Linux done by Mike Gore, 2016
@par Tools to Read and Write files inside common TRS-80 emulator images
@par Copyright © 2016 Miguel Dutra, GPL License
@par You are free to use this code under the terms of GPL
please retain a copy of this notice in any code you use it in.
This is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
The software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
@par Original Windows Code
@see http://www.trs-80.com/wordpress/category/contributors/miguel-dutra/
@see http://www.trs-80.com/wordpress/dsk-and-dmk-image-utilities/
@see Miguel Dutra www.mdutra.com
*/
//---------------------------------------------------------------------------------
// Operating System Interface for CP/M
//---------------------------------------------------------------------------------
#include "windows.h"
#include <math.h>
#include <stdio.h>
#include "v80.h"
#include "vdi.h"
#include "osi.h"
#include "cpm.h"
void Dump(unsigned char* pBuffer, int nSize);
void PrintError(DWORD dwError);
//---------------------------------------------------------------------------------
// Initialize member variables
//---------------------------------------------------------------------------------
CCPM::CCPM()
: m_pDir(NULL), m_DPB(), m_nSectorsPerBlock(0), m_nReservedSectors(0), m_dwFilePos(0), m_wSector(0), m_Buffer()
{
}
//---------------------------------------------------------------------------------
// Release allocated memory
//---------------------------------------------------------------------------------
CCPM::~CCPM()
{
if (m_pDir != NULL)
free(m_pDir);
}
//---------------------------------------------------------------------------------
// Validate DOS version and define operating parameters
//---------------------------------------------------------------------------------
DWORD CCPM::Load(CVDI* pVDI, DWORD dwFlags)
{
DWORD dwBytes;
DWORD dwError = NO_ERROR;
// Copy VDI pointer and user flags to member variables
m_pVDI = pVDI;
m_dwFlags = dwFlags;
// Get disk geometry
m_pVDI->GetDG(m_DG);
// Check internal buffer size (just in case)
if (sizeof(m_Buffer) < m_DG.LT.wSectorSize)
{
dwError = ERROR_INVALID_USER_BUFFER;
goto Done;
}
// If not first Load, release the previously allocated memory
if (m_pDir != NULL)
free(m_pDir);
// Calculate needed memory to hold the largest CP/M directory
dwBytes = sizeof(CPM_FCB) * 512 + (V80_MEM - ((sizeof(CPM_FCB) * 512) % V80_MEM));
// Allocate memory for the entire directory
if ((m_pDir = (BYTE*)calloc(dwBytes,1)) == NULL)
{
dwError = ERROR_OUTOFMEMORY;
goto Done;
}
if ((dwError = GetDiskParams()) != NO_ERROR)
goto Done;
if (m_dwFlags & V80_FLAG_INFO)
{
printf("DOS: BLS RPT BSH BLM EXM DSM DRM AL0 AL1 CKS OFF SSZ SKF OPT\r\n");
printf("DOS: %5d %3d %3d %3d %3d %3d %3d %02X %02X %3d %3d %3d %3d %02X\r\n",
m_DPB.wBLS, m_DPB.nRPT, m_DPB.nBSH, m_DPB.nBLM, m_DPB.nEXM, m_DPB.wDSM, m_DPB.wDRM, m_DPB.nAL0, m_DPB.nAL1, m_DPB.nCKS, m_DPB.nOFF, m_DPB.nSSZ, m_DPB.nSKF, m_DPB.nOPT);
}
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Return a pointer to the first/next directory entry
//---------------------------------------------------------------------------------
DWORD CCPM::Dir(void** pFile, OSI_DIR nFlag)
{
static WORD wFCB = -1;
DWORD dwError = NO_ERROR;
// Reset index if FindFirst has been requested
if (nFlag == OSI_DIR_FIND_FIRST)
wFCB = -1;
while (++wFCB <= m_DPB.wDRM)
{
// Get a pointer to the next entry
*pFile = &((CPM_FCB*)m_pDir)[wFCB];
// Check whether entry is deleted
if (((CPM_FCB*)(*pFile))->nET == 0xE5)
continue;
// Check whether entry is primary
if (((CPM_FCB*)(*pFile))->nRC != 0x80)
goto Done;
}
dwError = ERROR_NO_MORE_FILES;
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Return a pointer to the directory entry matching the file name
//---------------------------------------------------------------------------------
DWORD CCPM::Open(void** pFile, const char cName[11])
{
/*
DWORD dwError = NO_ERROR;
WORD x, y;
for (x = 0; x <= m_DPB.wDRM; x++)
{
// Get a pointer to entry[x]
*pFile = &((CPM_FCB*)m_pDir)[x];
// Compare File Name and Type
for (y = 0; y < sizeof(cName) && ((((CPM_FCB*)(*pFile))->cFN[y] & 0x7F) == cName[y]); y++);
// If not equal, skip to the next
if (y < sizeof(cName))
continue;
// Check whether entry is primary
if (((CPM_FCB*)(*pFile))->nRC != 0x80)
goto Done;
}
dwError = ERROR_FILE_NOT_FOUND;
Done:
return dwError;
*/
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Create a new file with the indicated name and size
//---------------------------------------------------------------------------------
DWORD CCPM::Create(void** pFile, OSI_FILE& File)
{
/*
CPM_EXTENT Extent;
void* pPrevious;
BYTE nGranules;
BYTE nExtent = 0;
DWORD dwError = NO_ERROR;
// Invalidate any previous Seek()
m_wSector = 0xFFFF;
// Get a new directory entry
if ((dwError = GetFDE(pFile)) != NO_ERROR)
goto Abort;
// Set the directory entry as Active
((CPM_FPDE*)(*pFile))->nAttributes[0] = CPM_ATTR0_ACTIVE;
// Set the file password
((CPM_FPDE*)(*pFile))->wOwnerHash = 0x4296;
((CPM_FPDE*)(*pFile))->wUserHash = 0x4296;
// Set the file properties as indicated by the caller
SetFile(*pFile, File, false);
// Calculate the number of granules needed
nGranules = ((CPM_FPDE*)(*pFile))->wERN / m_nSectorsPerGranule + (((CPM_FPDE*)(*pFile))->wERN % m_nSectorsPerGranule > 0 ? 1 : 0);
if (nGranules == 0 && ((CPM_FPDE*)(*pFile))->nEOF > 0)
nGranules++;
// Repeat while the number of needed granules is greater than zero
while (nGranules > 0)
{
// Create one extent with as many granules as possible, based on the calculated quantity
if ((dwError = CreateExtent(Extent, nGranules)) != NO_ERROR)
goto Abort;
// Increment extent counter
nExtent++;
// Copy the extent data to the directory entry
if ((dwError = CopyExtent(*pFile, CPM_EXTENT_SET, nExtent, Extent)) != NO_ERROR)
{
// Abort if error is other than extent not found
if (dwError != ERROR_NOT_FOUND)
goto Abort;
// Preserve current file handle
pPrevious = *pFile;
// Get an additional directory entry
if ((dwError = GetFDE(pFile)) != NO_ERROR)
goto Abort;
// Set a forward link to the newly created directory entry
((CPM_FPDE*)pPrevious)->Link.nCylinder = 0xFE;
((CPM_FPDE*)pPrevious)->Link.nGranules = FDE2DEC(*pFile);
// Set a backward link from the previously existing directory entry
((CPM_FPDE*)(*pFile))->nAttributes[0] = CPM_ATTR0_ACTIVE|CPM_ATTR0_EXTENDED;
((CPM_FPDE*)(*pFile))->nAttributes[1] = FDE2DEC(pPrevious);
// Copy the file name and extention to the new directory entry
memcpy(((CPM_FPDE*)pFile)->cName, File.szName, sizeof(File.szName) - 1);
memcpy(((CPM_FPDE*)pFile)->cType, File.szType, sizeof(File.szType) - 1);
// Set the corresponding HIT DEC with the calculated name hash
m_pDir[m_DG.LT.wSectorSize + FDE2DEC(pFile)] = Hash(((CPM_FPDE*)pFile)->cName);
continue;
}
// Subtract the number of granules allocated in this extent from the total required
nGranules -= (Extent.nGranules & CPM_GRANULE_COUNT) + 1;
}
// Write the updated directory data and exit
dwError = DirRW(CPM_DIR_WRITE);
goto Done;
// Restore the previous directory state
Abort:
DirRW(CPM_DIR_READ);
Done:
return dwError;
*/
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Move the file pointer
//---------------------------------------------------------------------------------
DWORD CCPM::Seek(void* pFile, DWORD dwPos)
{
/*
WORD wBlock;
DWORD dwError = NO_ERROR;
// Calculate how many bytes each extent can hold
DWORD dwExCap = m_DPB.wBLS * (m_DPB.b8Bit ? 16 : 8);
// Calculate in which extent the required position is
BYTE nExtent = dwPos / dwExCap;
// Calculate from which extent's vector to get the block number
BYTE nVector = (dwPos % dwExCap) / m_DPB.wBLS;
// Calculate in which sector, inside the block, the position is
BYTE nSector = (dwPos - nExtent * dwExCap - nVector * m_DPB.wBLS) / m_DG.LT.wSectorSize;
// Locate file extent
if ((dwError = FindExtent(&pFile, nExtent)) != NO_ERROR)
goto Done;
// Get block number from the disk allocation map
wBlock = (m_DPB.b8Bit ? ((CPM_FCB*)pFile)->DM.nDM[nVector] : ((CPM_FCB*)pFile)->DM.wDM[nVector]);
// If there is no block number then position can't be determined
if (wBlock == 0)
{
m_wSector = 0xFFFF;
goto Done;
}
// Convert block into relative sector
m_wSector = (((m_DPB.wDRM + 1) * sizeof(CPM_FCB) + (wBlock - 2) * m_DPB.wBLS) / m_DG.LT.wSectorSize) + nSector;
m_dwFilePos = dwPos;
Done:
return dwError;
*/
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Read data from file
//---------------------------------------------------------------------------------
DWORD CCPM::Read(void* pFile, BYTE* pBuffer, DWORD& dwBytes)
{
/*
VDI_TRACK* pTrack;
BYTE nTrack;
BYTE nSide;
BYTE nSector;
DWORD dwFileSize;
WORD wSectorBegin;
WORD wSectorEnd;
WORD wLength;
DWORD dwRead = 0;
DWORD dwError = NO_ERROR;
// Get file size
dwFileSize = GetFileSize(pFile);
// Repeat while there is something left to read
while (dwBytes - dwRead > 0)
{
// Check whether the last Seek() was successful
if (m_wSector == 0xFFFF)
{
dwError = ERROR_SEEK;
break;
}
// Check whether the requested position doesn't exceed the file size
if (m_dwFilePos > dwFileSize)
{
dwError = ERROR_READ_FAULT;
break;
}
// Convert relative sector into Track, Side, Sector
CHS(m_wSector, nTrack, nSide, nSector);
// Get a pointer to the correct track descriptor
pTrack = (nTrack == m_DG.FT.nTrack ? &m_DG.FT : &m_DG.LT);
// Read sector into internal buffer
if ((dwError = m_pVDI->Read(nTrack, nSide, nSector, m_Buffer, pTrack->wSectorSize)) != NO_ERROR)
break;
// Calculate starting transfer point
wSectorBegin = m_dwFilePos % pTrack->wSectorSize;
// Calculate ending transfer point
wSectorEnd = (dwFileSize - m_dwFilePos >= pTrack->wSectorSize ? pTrack->wSectorSize : dwFileSize % pTrack->wSectorSize);
// Calculate transfer length
wLength = ((wSectorEnd - wSectorBegin) <= (dwBytes - dwRead) ? wSectorEnd - wSectorBegin : dwBytes - dwRead);
// Copy data from internal buffer to the caller's buffer
memcpy(pBuffer, &m_Buffer[wSectorBegin], wLength);
// Update bytes count and caller's buffer pointer
dwRead += wLength;
pBuffer += wLength;
// Advance file pointer
Seek(pFile, m_dwFilePos + wLength);
}
dwBytes = dwRead;
return dwError;
*/
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Save data to file
//---------------------------------------------------------------------------------
DWORD CCPM::Write(void* pFile, BYTE* pBuffer, DWORD& dwBytes)
{
/*
VDI_TRACK* pTrack;
BYTE nTrack;
BYTE nSide;
BYTE nSector;
DWORD dwFileSize;
WORD wSectorBegin;
WORD wSectorEnd;
WORD wLength;
DWORD dwWritten = 0;
DWORD dwError = NO_ERROR;
dwFileSize = GetFileSize(pFile);
while (dwBytes - dwWritten > 0)
{
// Check whether the last Seek() was successful
if (m_wSector == 0xFFFF)
{
dwError = ERROR_SEEK;
break;
}
// Check whether the requested position doesn't exceed the file size
if (m_dwFilePos > dwFileSize)
{
dwError = ERROR_WRITE_FAULT;
break;
}
// Convert relative sector into Track, Side, Sector
CHS(m_wSector, nTrack, nSide, nSector);
// Get a pointer to the correct track descriptor
pTrack = (nTrack == m_DG.FT.nTrack ? &m_DG.FT : &m_DG.LT);
// Calculate the starting transfer point
wSectorBegin = m_dwFilePos % pTrack->wSectorSize;
// Calculate ending transfer point
wSectorEnd = (dwFileSize - m_dwFilePos >= pTrack->wSectorSize ? pTrack->wSectorSize : dwFileSize % pTrack->wSectorSize);
// Calculate transfer length
wLength = ((wSectorEnd - wSectorBegin) <= (dwBytes - dwWritten) ? wSectorEnd - wSectorBegin : dwBytes - dwWritten);
// If needed, read the sector before writing to it
if (wLength < pTrack->wSectorSize)
{
if ((dwError = m_pVDI->Read(nTrack, nSide, nSector, m_Buffer, pTrack->wSectorSize)) != NO_ERROR)
break;
}
// Copy data from the caller's buffer to the internal buffer
memcpy(&m_Buffer[wSectorBegin], pBuffer, wLength);
// Write sector from internal buffer
if ((dwError = m_pVDI->Write(nTrack, nSide, nSector, m_Buffer, pTrack->wSectorSize)) != NO_ERROR)
break;
// Update bytes count and caller's buffer pointer
dwWritten += wLength;
pBuffer += wLength;
// Advance the file pointer
Seek(pFile, m_dwFilePos + wLength);
}
dwBytes = dwWritten;
return dwError;
*/
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Delete file
//---------------------------------------------------------------------------------
DWORD CCPM::Delete(void* pFile)
{
/*
CPM_EXTENT Extent;
DWORD dwError = NO_ERROR;
// Invalidate any previous Seek()
m_wSector = 0xFFFF;
// Inactivate the directory entry
((CPM_FPDE*)pFile)->nAttributes[0] &= !CPM_ATTR0_ACTIVE;
// Release the corresponding HIT slot
m_pDir[m_DG.LT.wSectorSize + FDE2DEC(pFile)] = 0;
// Loop through all extents releasing every allocated granule
for (int x = 1; (dwError = CopyExtent(pFile, CPM_EXTENT_GET, x, Extent)) == NO_ERROR; x++)
{
if ((dwError = DeleteExtent(Extent)) != NO_ERROR)
break;
}
// If exited the loop because reached the end of the extents table
if (dwError == ERROR_NO_MATCH)
dwError = DirRW(CPM_DIR_WRITE); // Save the directory
else
DirRW(CPM_DIR_READ); // Otherwise, restore its previous state
return dwError;
*/
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Get DOS information
//---------------------------------------------------------------------------------
void CCPM::GetDOS(OSI_DOS& DOS)
{
CPM_FCB* pEntry;
// Zero the DOS structure
memset(&DOS, 0, sizeof(OSI_DOS));
// Search through all directory entries
for (int x = 0; x <= m_DPB.wDRM; x++)
{
pEntry = &((CPM_FCB*)m_pDir)[x];
// Look for a Disk Label entry type
if (pEntry->nET == 0x20)
{
// Get disk name
memcpy(DOS.szName, pEntry->cFN, sizeof(DOS.szName) - 1);
// Strip the 8th bit from each byte
for (BYTE y = 0; y < sizeof(DOS.szName); y++)
DOS.szName[y] &= 0x7F;
break;
}
}
}
//---------------------------------------------------------------------------------
// Set DOS information
//---------------------------------------------------------------------------------
DWORD CCPM::SetDOS(OSI_DOS& DOS)
{
/*
// Set DOS version
((CPM_GAT*)m_pDir)->nDosVersion = DOS.nVersion;
// Set the disk name
memcpy(((CPM_GAT*)m_pDir)->cDiskName, DOS.szName, sizeof(DOS.szName) - 1);
// Set the disk date
memcpy(((CPM_GAT*)m_pDir)->cDiskDate, &DOS.szDate, sizeof(DOS.szDate) - 1);
return DirRW(CPM_DIR_WRITE);
*/
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Get file information
//---------------------------------------------------------------------------------
void CCPM::GetFile(void* pFile, OSI_FILE& File)
{
// Zero the structure
memset(&File, 0, sizeof(OSI_FILE));
// Copy file name and extention
memcpy(File.szName, ((CPM_FCB*)pFile)->cFN, sizeof(File.szName) - 1);
memcpy(File.szType, ((CPM_FCB*)pFile)->cFT, sizeof(File.szType) - 1);
// Remove attribute information from extension
File.szType[0] &= 0x7F;
File.szType[1] &= 0x7F;
File.szType[2] &= 0x7F;
// Get file size
File.dwSize = GetFileSize(pFile);
// Get file date
// File.Date.nMonth = (((CPM_FPDE*)pFile)->nAttributes[1] & CPM_ATTR1_MONTH);
// File.Date.nDay = (((CPM_FPDE*)pFile)->nAttributes[2] & CPM_ATTR2_DAY) >> 3;
// File.Date.wYear = (((CPM_FPDE*)pFile)->nAttributes[2] & CPM_ATTR2_YEAR) + (File.Date.nMonth > 0 ? 1980 : 0);
// If not a valid date, make it all zeroes
if (File.Date.nMonth < 1 || File.Date.nMonth > 12 || File.Date.nDay < 1 || File.Date.nDay > 31)
memset(&File.Date, 0, sizeof(File.Date));
// Get file attributes
File.nAccess = (((CPM_FCB*)pFile)->cFT[0] & 0x80 ? 5 : 0);
File.bSystem = (((CPM_FCB*)pFile)->cFT[1] & 0x80);
File.bInvisible = (((CPM_FCB*)pFile)->cFT[1] & 0x80) || (((CPM_FCB*)pFile)->nET == 0x80);
File.bModified = ((CPM_FCB*)pFile)->cFT[2] & 0x80;;
}
//---------------------------------------------------------------------------------
// Set file information (public function)
//---------------------------------------------------------------------------------
DWORD CCPM::SetFile(void* pFile, OSI_FILE& File)
{
return SetFile(pFile, File, true);
}
//---------------------------------------------------------------------------------
// Set file information (protected)
//---------------------------------------------------------------------------------
DWORD CCPM::SetFile(void* pFile, OSI_FILE& File, bool bCommit)
{
/*
// Copy the file name and extention
memcpy(((CPM_FPDE*)pFile)->cName, File.szName, sizeof(File.szName) - 1);
memcpy(((CPM_FPDE*)pFile)->cType, File.szType, sizeof(File.szType) - 1);
// Set the corresponding HIT DEC to the newly calculated name hash
m_pDir[m_DG.LT.wSectorSize + FDE2DEC(pFile)] = Hash(((CPM_FPDE*)pFile)->cName);
// Set the file size
((CPM_FPDE*)pFile)->nEOF = File.dwSize % m_DG.LT.wSectorSize;
((CPM_FPDE*)pFile)->wERN = File.dwSize / m_DG.LT.wSectorSize + (((CPM_FPDE*)pFile)->nEOF > 0 ? 1 : 0);
// Set the file date
((CPM_FPDE*)pFile)->nAttributes[1] &= ~CPM_ATTR1_MONTH;
((CPM_FPDE*)pFile)->nAttributes[1] |= File.Date.nMonth;
((CPM_FPDE*)pFile)->nAttributes[2] &= ~CPM_ATTR2_DAY;
((CPM_FPDE*)pFile)->nAttributes[2] |= (File.Date.nDay << 3);
((CPM_FPDE*)pFile)->nAttributes[2] &= ~CPM_ATTR2_YEAR;
((CPM_FPDE*)pFile)->nAttributes[2] |= (File.Date.wYear - (File.Date.nMonth > 0 ? 1980 : 0));
// Set the file attributes
((CPM_FPDE*)pFile)->nAttributes[0] &= ~CPM_ATTR0_ACCESS;
((CPM_FPDE*)pFile)->nAttributes[0] |= File.nAccess;
((CPM_FPDE*)pFile)->nAttributes[0] &= ~CPM_ATTR0_SYSTEM;
((CPM_FPDE*)pFile)->nAttributes[0] |= (CPM_ATTR0_SYSTEM * File.bSystem);
((CPM_FPDE*)pFile)->nAttributes[0] &= ~CPM_ATTR0_INVISIBLE;
((CPM_FPDE*)pFile)->nAttributes[0] |= (CPM_ATTR0_INVISIBLE * File.bInvisible);
((CPM_FPDE*)pFile)->nAttributes[1] &= ~CPM_ATTR1_MODIFIED;
((CPM_FPDE*)pFile)->nAttributes[1] |= (CPM_ATTR1_MODIFIED * File.bModified);
return (bCommit ? DirRW(CPM_DIR_WRITE) : NO_ERROR);
*/
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Get file size
//---------------------------------------------------------------------------------
DWORD CCPM::GetFileSize(void* pFile)
{
DWORD dwSize = 0; // Zero the file size accumulator
BYTE nEX = ((CPM_FCB*)pFile)->nEX; // Extent counter
while (true)
{
BYTE nRC = ((CPM_FCB*)pFile)->nRC; // Record counter
BYTE nS1 = ((CPM_FCB*)pFile)->nS1; // Last record byte count
// *** A formula varia conforme o sistema ***
// *** Alguns consideram o S1 como bytes a mais, outros a menos ***
// *** Verificar também diferentes interpretações do RC ***
dwSize += (nRC - (nRC == 0x80 ? 1 : 0)) * 128 - nS1; // Add this entry's size to the accumulator
if (nRC < 0x80) // Check whether this is an extended entry
break;
if (FindExtent(&pFile, ++nEX) != NO_ERROR) // Locate the next entry and continue
break;
}
return dwSize; // Return the accumulated size for all entries belonging to the file
}
//---------------------------------------------------------------------------------
// Discover the disk parameters
//---------------------------------------------------------------------------------
DWORD CCPM::GetDiskParams()
{
int iTracks, iSides, iSize;
DWORD dwError = NO_ERROR;
// Set DPB option flags
if (m_DG.LT.nDensity == VDI_DENSITY_DOUBLE) // Set flag for Double Density disks
m_DPB.nOPT |= CPM_OPT_DD;
if (m_dwFlags & V80_FLAG_SS) // Set flag for Single or Double-Sided disks
m_DPB.nOPT &= ~CPM_OPT_DS;
else if (m_dwFlags & V80_FLAG_DS)
m_DPB.nOPT |= CPM_OPT_DS;
else
m_DPB.nOPT |= (m_DG.LT.nFirstSide != m_DG.LT.nLastSide ? CPM_OPT_DS : 0);
// if (m_DG.LT.nFirstSide != m_DG.LT.nLastSide) // Set flag for Double-Sided disks
// m_DPB.nOPT |= CPM_OPT_DS;
// Try several values until the directory table is found
m_DPB.nSPT = m_DG.LT.nLastSector - m_DG.LT.nFirstSector + 1; // SPT = Sectors Per Track
for (m_DPB.nOFF = 0; m_DPB.nOFF < 4; m_DPB.nOFF++) // OFF = System reserved cylinders
{
for (int nDRM = m_DPB.nSPT; nDRM > 0; nDRM--) // Start with a full track directory and decrease to up one sector
{
for (m_DPB.nSKF = 0; m_DPB.nSKF < (m_DPB.nSPT >> 1); m_DPB.nSKF++) // SKF = Skew Factor (sector interleaving)
{
InitXLT();
m_DPB.wDRM = nDRM * (m_DG.LT.wSectorSize / sizeof(CPM_FCB)) - 1;// DRM = Sectors * Entries Per Sector - 1
if (DirRW(CPM_DIR_READ) == NO_ERROR) // Load directory into memory and check its structure
if (CheckDir() == NO_ERROR)
goto Found;
}
}
}
dwError = ERROR_NOT_DOS_DISK;
goto Done;
Found:
// Calculate disk size in bytes (excluding system reserved cylinders)
iTracks = m_DG.LT.nTrack - m_DG.FT.nTrack + 1;
iSides = m_DG.LT.nLastSide - m_DG.LT.nFirstSide + 1;
iSize = (iTracks * iSides - m_DPB.nOFF) * m_DPB.nSPT * m_DG.LT.wSectorSize;
// Set DPB parameters
m_DPB.b8Bit = Is8Bit();
m_DPB.wBLS = GetBLS(m_DPB.b8Bit);
m_DPB.nRPT = m_DPB.nSPT * (m_DG.LT.wSectorSize / 128); // RPT = 128-byte records per track
m_DPB.nSSZ = Log2(m_DG.LT.wSectorSize) - 6; // SSZ = Sector Size (0=128, 1=256, 2=512, 3=1024)
m_DPB.wDSM = (iSize / m_DPB.wBLS) - 1; // DSM = Disk Storage Max (max blocks minus 1)
m_DPB.nBSH = Log2(m_DPB.wBLS) - 6; // BSH = Block Shift Factor
m_DPB.nBLM = pow(2, m_DPB.nBSH) - 1; // BLM = Block Mask
m_DPB.nEXM = pow(2, Log2(m_DPB.wBLS) - (m_DPB.wDSM < 256 ? 9 : 10)) - 1; // EXM = Extent Mask
m_DPB.nCKS = (m_DPB.wDRM + 1) / 4; // CKS = Directory Check Size
m_DPB.nAL0 = GetALM(m_DPB.wDRM, m_DPB.wBLS) >> 8; // AL0 = Directory Allocation Bitmap #0
m_DPB.nAL1 = GetALM(m_DPB.wDRM, m_DPB.wBLS) & 0xFF; // AL1 = Directory Allocation Bitmap #1
// printf(" BLS RPT BSH BLM EXM DSM DRM AL0 AL1 CKS OFF SSZ SKF OPT\r\n");
// printf("%5d %3d %3d %3d %3d %3d %3d %02X %02X %3d %3d %3d %3d %02X\r\n",
// m_DPB.wBLS, m_DPB.nRPT, m_DPB.nBSH, m_DPB.nBLM, m_DPB.nEXM, m_DPB.wDSM, m_DPB.wDRM, m_DPB.nAL0, m_DPB.nAL1, m_DPB.nCKS, m_DPB.nOFF, m_DPB.nSSZ, m_DPB.nSKF, m_DPB.nOPT);
// Dump(m_pDir, m_DG.LT.wSectorSize);
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Read or Write the entire directory
//---------------------------------------------------------------------------------
DWORD CCPM::DirRW(CPM_DIR nMode)
{
BYTE nSectors = ((m_DPB.wDRM + 1) * sizeof(CPM_FCB)) / m_DG.LT.wSectorSize;
DWORD dwOffset = 0;
DWORD dwError = NO_ERROR;
for (BYTE nExSide = m_DG.LT.nFirstSide; nExSide <= (m_DPB.nOPT & CPM_OPT_SSEL ? m_DG.LT.nLastSide : m_DG.LT.nFirstSide) && nSectors > 0; nExSide++)
{
for (BYTE nTrack = m_DG.FT.nTrack + m_DPB.nOFF; nTrack <= m_DG.LT.nTrack && nSectors > 0; nTrack++)
{
for (BYTE nInSide = m_DG.LT.nFirstSide; nInSide <= (m_DPB.nOPT & CPM_OPT_SSEL ? m_DG.LT.nFirstSide : m_DG.LT.nLastSide) && nSectors > 0; nInSide++)
{
for (BYTE nSector = m_DG.LT.nFirstSector; nSector <= m_DG.LT.nLastSector && nSectors > 0; nSector++, nSectors--, dwOffset += m_DG.LT.wSectorSize)
{
if (nMode == CPM_DIR_WRITE)
{
if ((dwError = m_pVDI->Write(nTrack, (m_DPB.nOPT & CPM_OPT_SSEL ? nExSide : nInSide), XLT(nSector), &m_pDir[dwOffset], m_DG.LT.wSectorSize)) != NO_ERROR)
goto Done;
}
else
{
if ((dwError = m_pVDI->Read(nTrack, (m_DPB.nOPT & CPM_OPT_SSEL ? nExSide : nInSide), XLT(nSector), &m_pDir[dwOffset], m_DG.LT.wSectorSize)) != NO_ERROR)
goto Done;
// printf("[%02d:%d:%02d]\r\n", nTrack, (m_DPB.nOPT & CPM_OPT_SSEL ? nExSide : nInSide), L2P(nSector));
// Dump(&m_pDir[dwOffset], m_DG.LT.wSectorSize);
}
}
}
}
}
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Check the directory structure
//---------------------------------------------------------------------------------
DWORD CCPM::CheckDir(void)
{
BYTE nSector;
BYTE nLastSector = 0xFF;
bool bIsEmpty;
bool bAnyEmpty = false;
WORD wFiles = 0;
CPM_FCB* pFCB;
char szFileNameChars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$&+-@_~";
DWORD dwError = NO_ERROR;
WORD x;
// For each entry in the directory...
for (WORD wFCB = 0; wFCB <= m_DPB.wDRM; wFCB++)
{
//printf("1");
// Check sector interleaving (there may be no empty sectors between used ones)
// Calculate current FCB's sector
nSector = (wFCB * sizeof(CPM_FCB)) / m_DG.LT.wSectorSize;
// Check each sector only once
if (nSector != nLastSector)
{
nLastSector = nSector;
// Check whether sector is filled with E5 (except for the last 16 byte that may contain the Tandy Copyright)
for (x = 0; x < (m_DG.LT.wSectorSize - 16); x++)
if (m_pDir[wFCB * sizeof(CPM_FCB) + x] != 0xE5) // Need test for 1A (complement)?
break;
bIsEmpty = (x < (m_DG.LT.wSectorSize - 16) ? false : true);
// If current sector is used but some empty sector has been detected before, then we have an error
if (!bIsEmpty && bAnyEmpty)
{
dwError = ERROR_DISK_TOO_FRAGMENTED;
goto Done;
}
// If sector is empty, there should be only empty sectors from now on
if (bIsEmpty)
bAnyEmpty = true;
}
//printf(", 2");
// Get a pointer to the directory entry
pFCB = &((CPM_FCB*)m_pDir)[wFCB];
// If entry is inactive, skip to the next
if (pFCB->nET == 0xE5)
continue;
//printf(", 3");
// First 8 characters of file name can be non-blanks
for (x = 0; x < sizeof(pFCB->cFN) && memchr(szFileNameChars, (pFCB->cFN[x] & 0x7F), strlen(szFileNameChars)) != NULL; x++);
// But at least the first one must be non-blank for the name to be valid
if (x == 0 && !(m_dwFlags & V80_FLAG_CHKDIR))
{
dwError = ERROR_INVALID_NAME;
goto Done;
}
//printf(", 4");
// If a blank is found, then only blanks can exist to the end of the name
for ( ; x < sizeof(pFCB->cFN) && (pFCB->cFN[x] & 0x7F) == ' '; x++);
// If not, then this name is invalid
if (x < sizeof(pFCB->cFN))
{
dwError = ERROR_INVALID_NAME;
goto Done;
}
//printf(", 5");
// The file extension can have up to 3x 7-bit non-blank characters
for (x = 0; x < sizeof(pFCB->cFT) && memchr(szFileNameChars, (pFCB->cFT[x] & 0x7F), strlen(szFileNameChars)) != NULL; x++);
// If a blank is found, then only blanks can exist to the end of the extension
for ( ; x < sizeof(pFCB->cFT) && (pFCB->cFT[x] & 0x7F) == ' '; x++);
// If not, then this extension is invalid
if (x < sizeof(pFCB->cFT))
{
dwError = ERROR_INVALID_NAME;
goto Done;
}
//printf(", 6\r\n");
// Increment file count
wFiles++;
}
if (wFiles == 0)
dwError = ERROR_EMPTY;
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Scan the Directory
//---------------------------------------------------------------------------------
DWORD CCPM::FindExtent(void** pFile, BYTE nEX)
{
DWORD dwError = NO_ERROR;
// Cast a pointer to the target FCB
CPM_FCB* pTarget = (CPM_FCB*)*pFile;
for (int x = 0; x <= m_DPB.wDRM; x++)
{
// Point to the directory entry[x]
CPM_FCB* pSource = &((CPM_FCB*)m_pDir)[x];
// Check whether Entry Type, File Name and File Type match the entry we're looking for
if (memcmp(pSource, pTarget, 12) != 0)
continue;
// Check whether Extent Count matches the one we're looking for
if (pSource->nEX == nEX)
{
*pFile = pSource;
goto Done;
}
}
dwError = ERROR_NO_MORE_FILES;
Done:
return dwError;
}