-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem_OPD.cpp
More file actions
164 lines (146 loc) · 3.54 KB
/
Copy pathFileSystem_OPD.cpp
File metadata and controls
164 lines (146 loc) · 3.54 KB
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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Daniele Terdina
#include "MdvDecode.h"
class FileSystem_OPD : public FileSystem
{
public:
FileSystem_OPD()
{
headerSize = 14;
blockHeaderSize = 4;
sectorSize = 514;
sectorNumberOffset = 1;
hasMap = true;
}
bool IsGoodBlock(const BYTE* p, int size)
{
if (size == 14 && p[0] != 0xFF) // Sector header must start with FF
return false;
// Verify checksum
size -= 2;
int checksum = 0x0F0F;
for (int i = 0; i < size; i++)
checksum += *p++;
return p[0] == (BYTE)checksum && p[1] == (BYTE)(checksum >> 8);
}
int GetMapSectorNum(const vector<int>& sectorMap, const vector<Sector>& sectorList)
{
// Search for the block header of the start of file number 1 or 2
for (int sectorId = 0; sectorId < sectorMap.size(); sectorId++)
{
if (sectorMap[sectorId] >= 0)
{
const Sector& s = sectorList[sectorMap[sectorId]];
if (s.block.size == 4 && IsGoodBlock(s.block.pData, s.block.size) && s.block.pData[1] == 1)
{
int fileNum = s.block.pData[0];
if (fileNum == 1 || fileNum == 2 && s.data.size >= 512)
{
if (memcmp(s.data.pData + 24, "ICL ", 4) == 0)
return sectorId;
}
}
}
}
return -1;
}
void ListSectorTypes(const BYTE* pMap, vector<SECTOR_MAP_TYPE>& type)
{
numSectors = pMap[0x16];
if (numSectors < 100 || numSectors > 236)
numSectors = 236;
pMap += 0x28;
for (int s = 0; s < numSectors; s++)
{
switch (pMap[s << 1])
{
case 0:
type[s] = SMT_EMPTY;
break;
case 1:
case 2:
type[s] = SMT_MAP;
break;
case 0xFF:
type[s] = SMT_MARKED_BAD;
break;
default:
type[s] = SMT_FILE;
break;
}
}
for (int s = numSectors; s < 256; s++)
type[s] = SMT_NOT_FOUND;
}
int ListFiles(const vector<int>& sectorMap, const vector<Sector>& sectorList, const vector<bool>& isGoodSector, vector<File>& fileList)
{
int mapSectorNum = GetMapSectorNum(sectorMap, sectorList);
const BYTE* pMap = sectorList[sectorMap[mapSectorNum]].data.pData + 0x28;
int numErrors = 0;
int dirLen = 0;
bool hasDirLen = false;
bool end = false;
for (int dirBlockNum = 2; dirBlockNum < 32 && !end; dirBlockNum++)
{
int s = FindFileBlock(pMap, 1, dirBlockNum);
if (s < 0 || !isGoodSector[s])
s = FindFileBlock(pMap, 2, dirBlockNum); // There are two copies of the catalog
if (s < 0 || !isGoodSector[s])
{
numErrors++;
}
else
{
BYTE* p = sectorList[sectorMap[s]].data.pData;
for (int i = 0; i < 11; i++)
{
int len = GetLong(p + 0x16);
int nameLen = 20;
while (nameLen > 0 && p[nameLen - 1] == ' ')
nameLen--;
int fileId = p[0x1A];
if (i == 0 && dirBlockNum == 2)
{
dirLen = len - 512;
hasDirLen = true;
}
else if (len && nameLen && fileId)
{
File f;
f.length = len;
f.name = string((char*)p, nameLen);
len = (len + 511) / 512;
for (int k = 1; k <= len; k++)
f.sectors.push_back(FindFileBlock(pMap, fileId, k));
fileList.emplace_back(f);
}
p += 44;
}
dirLen -= 512;
if (hasDirLen && dirLen <= 0)
{
end = true;
break;
}
}
}
return numErrors;
}
private:
// Sectors on this cartridge, from byte 0x16 of the map
int numSectors = 236;
int FindFileBlock(const BYTE* pMap, BYTE fileNum, BYTE blockNum)
{
for (int s = 0; s < numSectors; s++)
{
if (pMap[0] == fileNum && pMap[1] == blockNum)
return s;
pMap += 2;
}
return -1;
}
};
FileSystem* CreateFileSystem_OPD()
{
return new FileSystem_OPD();
}