Skip to content

Commit

Permalink
[#8] Support for MBD image files of MB-02 disk interface (Part 2: Add…
Browse files Browse the repository at this point in the history
…ed possibility to create and use just one FAT copy)
  • Loading branch information
tomas-nestorovic committed Dec 25, 2019
1 parent 265c07b commit 7f9bfee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
49 changes: 21 additions & 28 deletions Main/src/BSDOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
CBSDOS308::TFatValue CBSDOS308::__getLogicalSectorFatItem__(TLogSector logSector) const{
// returns the value in FAT of the specified LogicalSector; returns BSDOS_FAT_ERROR if FAT Sector read error
if (const PCBootSector bootSector=boot.GetSectorData())
for( BYTE fatCopy=0; fatCopy<2; fatCopy++ ){
for( BYTE fatCopy=0; fatCopy<BSDOS_FAT_COPIES_MAX; fatCopy++ ){
TLogSector lsFat=bootSector->fatStarts[fatCopy];
if (PCFatValue fat=reinterpret_cast<PCFatValue>( __getHealthyLogicalSectorData__(lsFat) ))
for( TLogSector index=logSector; lsFat<BSDOS_FAT_ITEMS_PER_SECTOR; index-=BSDOS_FAT_ITEMS_PER_SECTOR ){
Expand Down Expand Up @@ -141,7 +141,7 @@ systemSector: *buffer++=TSectorStatus::SYSTEM; // ... are always reserved for
if (ls==bootSector->dirsLogSector)
goto systemSector;
// . FAT Sectors are System ones
for( BYTE fatCopy=0; fatCopy<2; fatCopy++ ){
for( BYTE fatCopy=0; fatCopy<BSDOS_FAT_COPIES_MAX; fatCopy++ ){
TLogSector lsFat=bootSector->fatStarts[fatCopy];
if (const PCFatValue fat=reinterpret_cast<PCFatValue>(__getHealthyLogicalSectorData__(lsFat)))
for( BYTE n=bootSector->nSectorsPerFat; n>0; n-- ){
Expand Down Expand Up @@ -173,7 +173,7 @@ systemSector: *buffer++=TSectorStatus::SYSTEM; // ... are always reserved for
// returns the value in FAT of the specified LogicalSector; returns BSDOS_FAT_ERROR if FAT Sector read error
bool valueWritten=false; // assumption (the Value couldn't be written into any FatCopy)
if (const PCBootSector bootSector=boot.GetSectorData())
for( BYTE fatCopy=0; fatCopy<2; fatCopy++ ){
for( BYTE fatCopy=0; fatCopy<BSDOS_FAT_COPIES_MAX; fatCopy++ ){
const TLogSector lsFat0=bootSector->fatStarts[fatCopy];
if (PFatValue fat=reinterpret_cast<PFatValue>(__getHealthyLogicalSectorData__(lsFat0)))
for( TLogSector index=logSector,lsFat=lsFat0; lsFat<BSDOS_FAT_ITEMS_PER_SECTOR; index-=BSDOS_FAT_ITEMS_PER_SECTOR ){
Expand Down Expand Up @@ -1032,45 +1032,38 @@ systemSector: *buffer++=TSectorStatus::SYSTEM; // ... are always reserved for
TFatValue fatFirstSector[BSDOS_FAT_ITEMS_PER_SECTOR];
::ZeroMemory( fatFirstSector, sizeof(fatFirstSector) );
fatFirstSector[1]=TFatValue::SystemSector;
for( BYTE fatCopy=0; fatCopy<params->nAllocationTables; fatCopy++ ){
if (boot->fatStarts[fatCopy]=__getNextHealthySectorWithoutFat__(ls,(BYTE)-1))
for( BYTE s=0; s<boot->nSectorsPerFat-1; s++ ){
const TLogSector curr=ls;
if (const TLogSector next=__getNextHealthySectorWithoutFat__(ls,(BYTE)-1))
fatFirstSector[curr]=TFatValue( true, true, boot->fatSectorsListing[fatCopy+2*s]=next );
else
for( BYTE fatCopy=0,fatSectors[UCHAR_MAX]; fatCopy<BSDOS_FAT_COPIES_MAX; fatCopy++ ){
if (fatCopy<params->nAllocationTables)
for( BYTE s=0; s<boot->nSectorsPerFat; s++ )
if (!( fatSectors[s]=__getNextHealthySectorWithoutFat__(ls,UCHAR_MAX) ))
return;
}
else
return;
TLogSector curr = boot->fatStarts[fatCopy] = fatSectors[0];
for( BYTE s=1; s<boot->nSectorsPerFat; s++ ){
const TLogSector next=fatSectors[s];
fatFirstSector[curr]=TFatValue( true, true, boot->fatSectorsListing[fatCopy+2*(s-1)]=next );
curr=next;
}
fatFirstSector[ls]=TFatValue(true,false,BSDOS_SECTOR_LENGTH_STD);
}
::memcpy( __getHealthyLogicalSectorData__(boot->fatStarts[0]),
fatFirstSector,
sizeof(fatFirstSector)
);
switch (params->nAllocationTables){
case 1:
// just a single FAT copy wanted
boot->fatStarts[1]=boot->fatStarts[0]; // formally setting the second copy identical to the first one
break;
case 2:
// two FAT copies wanted
::memcpy( __getHealthyLogicalSectorData__(boot->fatStarts[1]),
fatFirstSector,
sizeof(fatFirstSector)
);
break;
default:
ASSERT(FALSE); // we shouldn't end up here!
}
::memcpy( __getHealthyLogicalSectorData__(boot->fatStarts[1]),
fatFirstSector,
sizeof(fatFirstSector)
);
__setLogicalSectorFatItem__( 0, TFatValue( MAKEWORD(0,__getFatChecksum__(0)) ) ); // both FAT copies are the same at the moment, hence getting checksum of one of them
for( TLogSector lsUnknown=boot->nBytesInFat/sizeof(TFatValue); lsUnknown>nSectorsTotal; __setLogicalSectorFatItem__(--lsUnknown,TFatValue::SectorUnknown) );
// - root Directory
if (boot->dirsLogSector=__getNextHealthySectorWithoutFat__(ls,nSectorsTotal))
__setLogicalSectorFatItem__( ls, TFatValue(true,false,BSDOS_SECTOR_LENGTH_STD) );
else
return;
// - marking unhealthy Sectors encountered thus far as Bad
while (--ls>=2)
if (__getLogicalSectorFatItem__(ls)==TFatValue::SectorEmpty) // an unhealthy Empty Sector
__setLogicalSectorFatItem__( ls, TFatValue::SectorErrorInDataField );
}

bool CBSDOS308::ValidateFormatChangeAndReportProblem(bool reformatting,PCFormat f) const{
Expand Down
7 changes: 5 additions & 2 deletions Main/src/BSDOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define BSDOS_SECTOR_NUMBER_LAST 11
#define BSDOS_SECTOR_NUMBER_TEMP (BSDOS_SECTOR_NUMBER_FIRST+1)

#define BSDOS_FAT_COPIES_MAX 2

#define BSDOS_DIRS_SLOTS_COUNT (BSDOS_SECTOR_LENGTH_STD/sizeof(CDirsSector::TSlot))

#define BSDOS_DIR_CORRUPTED _T("« Corrupted »")
Expand Down Expand Up @@ -39,8 +41,9 @@
TLogSector dirsLogSector;
WORD nSectorsPerFat;
WORD nBytesInFat;
TLogSector fatStarts[2]; // first LogicalSectors of two FAT copies
DWORD reserved2;
TLogSector fatStarts[BSDOS_FAT_COPIES_MAX]; // first LogicalSectors of individual FAT copies
BYTE diskIdChecksum;
BYTE reserved2[3];
BYTE fatSectorsListing[6];
BYTE signature2;
DWORD formattedDateTime;
Expand Down
2 changes: 1 addition & 1 deletion Main/src/BSDOS_Boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
5, // minimal total number of Sectors required
1, // maximum number of Sector in one Cluster (must be power of 2)
BSDOS_SECTOR_LENGTH_STD, // maximum size of a Cluster (in Bytes)
2,2, // range of supported number of allocation tables (FATs)
1,2, // range of supported number of allocation tables (FATs)
32,12640, // range of supported number of root Directory entries
1, // lowest Sector number on each Track
0x00,0x00, // regular Sector and Directory Sector filler Byte
Expand Down

0 comments on commit 7f9bfee

Please sign in to comment.