-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCFSMGT.cpp
216 lines (181 loc) · 5.91 KB
/
CFSMGT.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
//////////////////////////////////////////////////////////////////////////
//MGT file system parser. George Chirtoaca 2014.
//Specifications used: http://web.archive.org/web/20080514125600/http://www.ramsoft.bbk.org/tech/mgt_tech.txt .
//////////////////////////////////////////////////////////////////////////
#include "CFSMGT.h"
bool CFSMGT::Init()
{
word dirSize = DIR_ENTRY_COUNT * sizeof(MGTDirEntry);
byte* dirBuf = new byte[dirSize];
memset(dirBuf, 0, dirSize);
word trkSize = Disk->DiskDefinition.SPT * Disk->DiskDefinition.SectSize;
byte dirTracks = dirSize / trkSize;
bool res = true;
FSParams.BlockSize = BLOCK_SIZE;
FSParams.BlockCount = ((Disk->DiskDefinition.TrackCnt * Disk->DiskDefinition.SideCnt - dirTracks) *
Disk->DiskDefinition.SPT * Disk->DiskDefinition.SectSize )/BLOCK_SIZE;
FSParams.BootTrackCount = 0;
FSParams.DirEntryCount = DIR_ENTRY_COUNT;
FS_DirEntryMap = vector<bool>(FSParams.DirEntryCount, false);
FS_BlockMap = vector<bool>(FSParams.BlockCount, false);
for (byte trk = 0; trk < dirTracks && res; trk++)
res = Disk->ReadTrack(dirBuf + trk * trkSize, trk, 0);
if (res)
for (byte dirEntIdx = 0; dirEntIdx < DIR_ENTRY_COUNT; dirEntIdx++)
{
MGTDirEntry* dirEnt = (MGTDirEntry*)(dirBuf + dirEntIdx * sizeof(MGTDirEntry));
if (dirEnt->TypeMGT > MGT_FT_ERASED && dirEnt->TypeMGT <= MGT_FT_CREATE)
{
CFileMGT* f = CreateFileMGT(*dirEnt);
if (f != NULL)
{
f->FileDirEntries.push_back(dirEntIdx);
word sectCnt = dirEnt->SectorCntLow + 0x100 * dirEnt->SectorCntHi;
word sectIdx = 0;
byte trackStart = (dirEnt->StartTrack - dirTracks) % 80;
byte sideStart = ((trackStart & 0x80) > 0 ? 1 : 0);
word blockIdx = trackStart * Disk->DiskDefinition.SPT + sideStart * Disk->DiskDefinition.TrackCnt +
dirEnt->StartSector - 1;
//While block map is not finished, and the file sector count is not reached...
while (blockIdx < sizeof(dirEnt->AllocMap) * 8 && sectIdx < sectCnt)
{
//If the sector is marked as used for this file, assign it to the file.
if (dirEnt->AllocMap[blockIdx/8] & (1 << (blockIdx % 8)))
{
f->FileBlocks.push_back(blockIdx);
FS_BlockMap[blockIdx] = true;
sectIdx++;
}
blockIdx++;
}
MGTFiles.push_back(*f);
delete f;
}
FS_DirEntryMap[dirEntIdx] = true;
}
}
delete dirBuf;
return res;
}
CFileMGT* CFSMGT::CreateFileMGT(MGTDirEntry dir)
{
CFileMGT* f = new CFileMGT();
f->SetFileName(dir.Name);
f->Length = dir.FileHdrSpectrum.Length;
if (dir.TypeMGT - 1 < CFileSpectrum::SPECTRUM_UNTYPED)
{
f->SpectrumType = (CFileSpectrum::SpectrumFileType)(dir.FileHdrSpectrum.TypeSpectrum);
f->SpectrumType = f->GetType();
f->SpectrumVarLength = 0;
if (f->SpectrumType != CFileSpectrum::SPECTRUM_UNTYPED)
{
f->SpectrumLength = dir.FileHdrSpectrum.Length;
if (f->SpectrumType == CFileSpectrum::SPECTRUM_PROGRAM)
{
f->SpectrumStart = dir.FileHdrSpectrum.StartRun;
f->SpectrumVarLength = f->SpectrumLength - dir.FileHdrSpectrum.ProgLen;
}
else if (f->SpectrumType == CFileSpectrum::SPECTRUM_CHAR_ARRAY ||
f->SpectrumType == CFileSpectrum::SPECTRUM_NUMBER_ARRAY)
f->SpectrumArrayVarName = dir.FileHdrSpectrum.ArrVarName;
else
f->SpectrumStart = dir.FileHdrSpectrum.StartAddr;
}
}
else
{
f->SpectrumType = CFileSpectrum::SPECTRUM_UNTYPED;
if (dir.TypeMGT == MGT_FT_SCR)
{
f->SpectrumType = CFileSpectrum::SPECTRUM_BYTES;
f->SpectrumLength = 6912;
f->SpectrumStart = 16384;
}
else if (dir.TypeMGT == MGT_FT_OPEN)
{
f->Length = (dir.FileHdrOpen.BlockCount64K - 1) * 64 * 1024 + dir.FileHdrOpen.LenLastBlock;
}
else if (dir.TypeMGT == MGT_FT_EXE || dir.TypeMGT == MGT_FT_CREATE)
{
f->SpectrumType = CFileSpectrum::SPECTRUM_BYTES;
f->SpectrumLength = dir.FileHdrSpectrum.Length;
f->SpectrumStart = dir.FileHdrSpectrum.StartAddr;
}
else
{
f->SpectrumLength = f->SpectrumStart = 0;
f->Length = (dir.SectorCntLow + 0x100 * dir.SectorCntHi) * BLOCK_SIZE;
}
}
return f;
}
CFile* CFSMGT::FindFirst(char* pattern)
{
FindIdx = 0;
FindPattern = pattern;
return FindNext();
}
CFile* CFSMGT::FindNext()
{
CFileMGT* f = NULL;
bool found = false;
while (FindIdx < MGTFiles.size() && !found)
{
f = &MGTFiles[FindIdx];
FileNameType fn;
f->GetFileName(fn);
found = WildCmp(FindPattern, fn);
FindIdx++;
}
if (found)
return f;
else
{
LastError = ERR_FILE_NOT_FOUND;
return NULL;
}
}
bool CFSMGT::ReadBlock(byte* buf, word blkIdx, byte sectCnt)
{
byte side = blkIdx / (Disk->DiskDefinition.TrackCnt * Disk->DiskDefinition.SPT);
byte track = (DIR_TRACKS + blkIdx / Disk->DiskDefinition.SPT) % Disk->DiskDefinition.TrackCnt;
byte sector = blkIdx % Disk->DiskDefinition.SPT + 1;
return Disk->ReadSectors(buf, track, side, sector, sectCnt == 0 ? 1 : sectCnt);
}
bool CFSMGT::OpenFile(CFile* file)
{
CFileMGT* f = (CFileMGT*)file;
byte* buf = new byte[FSParams.BlockSize];
bool res = f->FileBlocks.size() > 0;
if (res)
res = ReadBlock(buf, f->FileBlocks[0]);
//Check if the header from the directory is the same as the header from file.
if (res && f->SpectrumType != CFileSpectrum::SPECTRUM_UNTYPED)
res = ((MGTFileHdrSpectrum*)buf)->TypeSpectrum == f->SpectrumType;
delete buf;
return res;
}
bool CFSMGT::CloseFile(CFile* file)
{
CFileMGT* f = (CFileMGT*)file;
if (f->buffer != NULL)
{
delete f->buffer;
f->buffer = NULL;
return NULL;
}
else
return false;
}
bool CFSMGT::ReadFile(CFile* file)
{
CFileMGT* f = (CFileMGT*)file;
bool res = true;
f->buffer = new byte[GetFileSize(f, true)];
for (byte blkIdx = 0; blkIdx < f->FileBlocks.size() && res; blkIdx++)
res = ReadBlock(f->buffer + blkIdx * (this->FSParams.BlockSize - 2), f->FileBlocks[blkIdx]);
//Skip the file header.
if (f->SpectrumType != CFileSpectrum::SPECTRUM_UNTYPED)
memmove(f->buffer, f->buffer + sizeof(MGTFileHdrSpectrum), f->GetLength());
return res;
}