-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBSMem.abp
344 lines (294 loc) · 9.07 KB
/
BSMem.abp
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
Const BSM_FILE_HEADER_ADR_Lo = &H7fb0
Const BSM_FILE_HEADER_ADR_Hi = &Hffb0
Type Align(1) BSM_FILE_HEADER
'ExtendHeader=02の時にそんざい
maker AS Word
programType As DWord
dummy1[9] AS Byte
'--------------
title[15] AS Byte
blockAllocation AS DWord
limit AS Word
month AS Byte
day AS Byte
mapMode As Byte
exeFlags As Byte
dummy33 AS Byte
extendHeaderFlag As Byte
checksum1 AS Word
checksum2 aS Word
EndType
'互換あり
Type BSM_FILE_INFO
raw AS BSM_FILE_HEADER
isLoROM AS BOOL
offset AS DWord
End Type
Enum BSM_SLOT_TYPE
BSM_SLOT_BIOS =0
BSM_SLOT_SA1
BSM_SLOT_HiROM
BSM_SLOT_SpLoROM
BSM_SLOT_TYPE_NUM
End Enum
Type BSM_MEMPACK_INFO
MP AS Word
Block AS Byte
DeviceInfo AS Byte
Serial[12] As Byte
raw[15] AS Byte
End Type
'LoROMモードにおけるBSMCの位置Cn:8000h
Const BSX_MEMPACK_ADR_LoROM = &HC08000
Const BSX_MEMPACK_ADR_HiROM = &HE00000
Const BSX_MEMPACK_ADR_SA1 = &HF00000
Const BSX_MEMPACK_ADR_BIOS = &HC00000
Dim slotNameTable[BSM_SLOT_TYPE_NUM] = [
"BS-X BIOS",
"SA-1 (バス釣 ガンダム)[非推奨]",
"HiROM (鮫亀 天牌 奏)",
"LoROM (ダビスタ96 ノベル ツクール)",
0
] AS BytePtr
Enum BSM_PACK_TYPE
BSM_8M_TYPE1 =0
BSM_8M_TYPE2
BSM_8M_TYPE3
BSM_8M_TYPE4
BSM_GNEXT
BSM_SAMEGAME
BSM_UNKNOWN
BSM_PACK_TYPE_NUM
End Enum
Dim packNameTable[BSM_PACK_TYPE_NUM] = [
"8Mメモリパック TYPE 1",
"8Mメモリパック TYPE 2",
"8Mメモリパック TYPE 3",
"8Mメモリパック TYPE 4",
"GNEXT ユニット&マップコレクション",
"鮫亀キャラカセット",
"(不明)",
0
] AS BytePtr
Dim BSX_MEMPACK_ADR_TABLE[BSM_PACK_TYPE_NUM] = [
BSX_MEMPACK_ADR_BIOS,
BSX_MEMPACK_ADR_SA1,
BSX_MEMPACK_ADR_HiROM,
BSX_MEMPACK_ADR_LoROM
] AS DWord
Function BSM_GetMempackAdress(slotTypeIdx AS DWord) AS DWord
If slotTypeIdx < BSM_SLOT_TYPE_NUM Then
BSM_GetMempackAdress=BSX_MEMPACK_ADR_TABLE[slotTypeIdx]
Else
BSM_GetMempackAdress=0
End If
End Function
Function BSM_GetMempackSize(packTypeIdx AS DWord) AS DWord
If packTypeIdx < BSM_PACK_TYPE_NUM Then
If packTypeIdx=BSM_GNEXT or packTypeIdx=BSM_SAMEGAME Then
BSM_GetMempackSize=512*1024
Else
BSM_GetMempackSize=1024*1024
End If
Else
BSM_GetMempackSize=0
End If
End Function
Function BSM_isLoROM(slotTypeIdx AS DWord) AS BOOL
If slotTypeIdx < BSM_SLOT_TYPE_NUM And slotTypeIdx=BSM_SLOT_SpLoROM Then
BSM_isLoROM=TRUE
Else
BSM_isLoROM=FALSE
End If
End Function
Const BSM_SLOT_MAX = 8
Const BSM_CAP_MAX = 1024*1024
Dim headerList[BSM_SLOT_MAX+1] AS *BSM_FILE_INFO
Enum BSM_HANDLE_MODE
BSM_HANDLE_FILEPATH
BSM_HANDLE_FILE
BSM_HANDLE_SERIAL
End Enum
Function BSM_genHeaderList(mode AS BSM_HANDLE_MODE,path_or_handle AS VoidPtr,baseAddress AS DWord,nextAdrFunc AS SfcMmcNextAddressFunc) As BOOL
Dim tmp aS *BSM_FILE_INFO, i AS Long,nextOffset AS DWord
Dim file AS File
If mode=BSM_HANDLE_FILEPATH Then
file.openFile(path_or_handle,GENERIC_READ)
nextAdrFunc = AddressOf(RomImage_nextAddress)
endif
Dim offset As DWord, curAdr AS DWord
curAdr=baseAddress
offset=0
while offset < BSM_CAP_MAX
If mode=BSM_HANDLE_SERIAL Then
tmp=BSM_getHeaderFromSerial(path_or_handle,curAdr,nextAdrFunc)
Elseif mode = BSM_HANDLE_FILEPATH then
tmp=BSM_getHeader(file.getHandle(),curAdr)
Else
tmp=BSM_getHeader(path_or_handle,curAdr)
End If
If tmp=NULL Then
offset += (128 * 1024) As DWord
curAdr = nextAdrFunc(path_or_handle, curAdr, (128 * 1024) As DWord)'curAdr + 128 * 1024
continue
End If
If headerList[i]<> NULL Then free(headerList[i])
headerList[i]=tmp
i++
If i>BSM_SLOT_MAX then exit while
nextOffset=BSM_getNextFileOffset(tmp As *BSM_FILE_HEADER)
If nextOffset=0 Then ExitWhile
'curAdr+=nextOffset
offset += nextOffset
if nextAdrFunc then
curAdr = nextAdrFunc(path_or_handle, curAdr, nextOffset)
Else
curAdr+=nextOffset
endif
Wend
If mode = BSM_HANDLE_FILEPATH then file.close()
'terminate
headerList[i]=NULL
If i=0 Then
BSM_genHeaderList=FALSE
Else
BSM_genHeaderList=TRUE
End If
End Function
function BSM_getNextFileOffset(cur As *BSM_FILE_HEADER) AS DWord
BSM_getNextFileOffset=countBits(cur->blockAllocation And &H7FFF) * 128 * 1024
End Function
function BSM_getHeader(hFile AS HANDLE,baseAddress AS DWord) AS *BSM_FILE_INFO
Dim tmp AS BSM_FILE_INFO
Dim _file AS File, isLoROM=TRUE AS BOOL
_file.setHandle(hFile)
'check lorom
_file.setFilePtr(baseAddress+BSM_FILE_HEADER_ADR_Lo)
_file.read(VarPtr(tmp),sizeof(BSM_FILE_INFO))
If BSM_isHeader(VarPtr(tmp)) =FALSE Then
'check hirom
_file.setFilePtr(baseAddress+BSM_FILE_HEADER_ADR_Hi)
_file.read(VarPtr(tmp),sizeof(BSM_FILE_INFO))
'not bsm image
If BSM_isHeader(VarPtr(tmp)) =FALSE Then
BSM_getHeader=NULL
ExitFunction
End If
isLoROM=FALSE
End If
BSM_getHeader=malloc(sizeof(BSM_FILE_INFO))
memcpy(VarPtr(BSM_getHeader->raw),VarPtr(tmp),sizeof(BSM_FILE_INFO))
BSM_getHeader->offset=baseAddress
BSM_getHeader->isLoROM = isLoROM
End Function
'めんどくさいので複製
function BSM_getHeaderFromSerial(hFile AS HANDLE,baseAddress AS DWord,nextAdrFunc AS SfcMmcNextAddressFunc) AS *BSM_FILE_INFO
Dim tmp AS BSM_FILE_INFO
Dim adr As DWord
Dim isLoROM As BOOL
'check lorom
adr = nextAdrFunc(hFile,baseAddress,BSM_FILE_HEADER_ADR_Lo)
ReadROM(hFile,VarPtr(tmp),0,adr,sizeof(BSM_FILE_HEADER),FALSE)
Dump(VarPtr(tmp),sizeof(BSM_FILE_HEADER),adr)
If BSM_isHeader(VarPtr(tmp)) =FALSE Then
'check hirom
adr = nextAdrFunc(hFile,baseAddress,BSM_FILE_HEADER_ADR_Hi)
ReadROM(hFile,VarPtr(tmp),0,adr,sizeof(BSM_FILE_HEADER),FALSE)
Dump(VarPtr(tmp),sizeof(BSM_FILE_HEADER),adr)
'not bsm image
If BSM_isHeader(VarPtr(tmp)) =FALSE Then
BSM_getHeaderFromSerial=NULL
ExitFunction
End If
isLoROM=FALSE
else
isLoROM=TRUE
End If
BSM_getHeaderFromSerial=malloc(sizeof(BSM_FILE_INFO))
memcpy(VarPtr(BSM_getHeaderFromSerial->raw),VarPtr(tmp),sizeof(BSM_FILE_INFO))
BSM_getHeaderFromSerial->offset=baseAddress
BSM_getHeaderFromSerial->isLoROM = isLoROM
End Function
Function BSM_isHeader(header As *BSM_FILE_HEADER) AS BOOL
BSM_isHeader=FALSE
If header->checksum1 <> (NOT(header->checksum2) And &HFFFF) Then
' If deleted, the checksum 4 bytes are zero cleared.
If (header->checksum1 <> 0) And (header->checksum2 <> 0) Then ExitFunction
Endif
If (header->mapMode And &HE0) <> &H20 Then ExitFunction
BSM_isHeader=TRUE
End Function
Function BSM_printInfo(_header As *BSM_FILE_INFO) AS BytePtr
Dim str AS StringClass
Dim title[16] AS Byte, blocks AS Long, limit As StringClass, status As StringClass
Dim header AS *BSM_FILE_HEADER
header=_header AS *BSM_FILE_HEADER
memcpy(title,header->title,16)
blocks=countBits(header->blockAllocation)
'generate status string
If (header->limit And &H8000) Then
limit.sprintf("left %d ",countBits(header->limit And &H7FFF))
Else
limit.cat("unlimited")
End If
If header->month = &HFF _
And header->day=&HFF _
And header->dummy33=&HFF And _
header->blockAllocation=&HFFFFFFFF Then
status.cat(ex"deleted, \r\n Download not completed.")
Else
If header->dummy33=&HFF Then
status.cat(ex"deleted, \r\n may be download was not completed.")
Else If header->limit=&H8000 Then
status.cat(ex"deleted,\r\n playable count is zero.")
Else If header->dummy33=&H00 Then
status.cat(ex"deleted,\r\n fixed 33h is zero cleared.")
Else If header->dummy33=&H33 Then
status.cat(ex"playable")
Else
status.cat(ex"may be deleted\r\n unknown status.")
Dump(header As BytePtr,sizeof(BSM_FILE_HEADER),BSM_FILE_HEADER_ADR_Lo And &H0FFF)
PrintPtr("-GUIDE-- |BlockAlloca|制 限|月|日|図|旗|33|拡|cksum|/sum |")
End If
End if
'Flag
Dim flagStr AS StringClass
flagStr.cat(" - Sound Link: ")
If header->exeFlags And &H10 Then
flagStr.cat(ex"NO (RSV OFF)\r\n")
Else
flagStr.cat(ex"YES (RSV ON)\r\n")
End If
flagStr.cat(" - St.GIGA Logo: ")
If (header->exeFlags And &H80) Then
flagStr.cat(ex"Skip\r\n")
Else
flagStr.cat(ex"Display\r\n")
End If
flagStr.cat(" - Execution Area: ")
If (header->exeFlags And &H60) >> 5 = 0 Then
flagStr.cat(ex"Memory Pack")
Else
flagStr.cat(ex"PSRAM")
End If
str.sprintf(ex"- Name : %s\r\n- Date : %02d月%02d日\r\n- Usage : %d blocks (%d KB)\r\n- Offset: %Xh\r\n", _
(title) As DWord,_
(header->month >> 4) As DWord , (header->day >> 3) As DWord , _
(blocks) As DWord , (blocks*128) As DWord , _
(_header->offset) As DWord , _
)
str.sprintf(ex"- RomType: Mode%02X %s%s\r\n- Limit : %s\r\n- Status : %s\r\n- Flag: %02Xh \r\n%s\r\n", _
header->mapMode, (RawHeader_GetRomSpeedString(header->mapMode)) As DWord , (RawHeader_GetRomMapString(header->mapMode)) As DWord , _
(limit.ptr) As DWord , _
(status.ptr) As DWord , _
(header->exeFlags) As DWord , (flagStr.ptr) As DWord _
)
str.copyToNewPtr(BSM_printInfo)
End Function
'ブロック数とか使用数はビットのカウントで表現
Function countBits(int As DWord) AS DWord
Dim i As Long
for i=0 To 31
If int And (1<<i) Then countBits++
Next i
End Function