-
Notifications
You must be signed in to change notification settings - Fork 1
/
BSMemWnd.sbp
379 lines (307 loc) · 10.8 KB
/
BSMemWnd.sbp
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
'-----------------------------------------------------------------------------
' イベント プロシージャ
'-----------------------------------------------------------------------------
' このファイルには、ウィンドウ [BSMem] に関するイベントをコーディングします。
' ウィンドウ ハンドル: hBSMem
' TODO: この位置にグローバルな変数、構造体、定数、関数を定義します。
'-----------------------------------------------------------------------------
' ウィンドウメッセージを処理するためのコールバック関数
Function BSMemProc(hWnd As HWND, dwMsg As DWord, wParam As WPARAM, lParam As LPARAM) As DWord
' TODO: この位置にウィンドウメッセージを処理するためのコードを記述します。
' イベントプロシージャの呼び出しを行います。
BSMemProc=EventCall_BSMemWnd(hWnd,dwMsg,wParam,lParam)
End Function
'-----------------------------------------------------------------------------
' ここから下は、イベントプロシージャを記述するための領域になります。
Sub BSM_open()
If hBSMem = NULL Then CreateDialog(hMainWnd, "BSMemWnd")
MainWnd_Move(0, 0)
BSMW_bMove = FALSE
End Sub
Sub BSM_close()
If hBSMem = NULL Then ExitSub
SendMessage(hBSMem, WM_CLOSE, 0, 0)
End Sub
Dim BSMW_bMove AS BOOL
Sub BSMemWnd_Move(x As Integer, y As Integer)
If BSMW_bMove = TRUE Then ExitSub
Dim sfrect aS RECT
GetWindowRect(hBSMem, sfrect)
Dim mwrect aS RECT, mw_width AS Long
GetWindowRect(hMainWnd, mwrect)
Dim wndframe_x AS Long
wndframe_x = GetSystemMetrics(SM_CXSIZEFRAME) * 2
BSMW_bMove = TRUE
mw_width = mwrect.right - mwrect.left
SetWindowPos(hMainWnd, NULL, sfrect.left - mw_width - wndframe_x, sfrect.top, 0, 0, SWP_NOSIZE or SWP_NOACTIVATE)
BSMW_bMove = FALSE
End Sub
Sub BSMemWnd_ListBox1_SelChange()
If headerList[0] = NULL Then ExitSub
Dim idx As Long
idx = SendMessage(BSMWnd(ListBox1), LB_GETCURSEL, 0, 0)
If idx<0 or idx>BSM_SLOT_MAX + 1 then ExitSub
Dim str AS BytePtr
If headerList[idx]<>NULL Then
DBM("BSM File #" + Str$(idx))
str = BSM_printInfo(headerList[idx])
DBM(str)
free(str)
Else
DBM("BSMem Full Dump")
End If
End Sub
Function BSMWnd(child AS Long) AS HWND
BSMWnd = GetDlgItem(hBSMem, child)
End Function
Sub BSMemWnd_Create(ByRef CreateStruct As CREATESTRUCT)
Dim i = 0 As Long
while slotNameTable[i]<>0
SendMessage(BSMWnd(BSM_CT), CB_ADDSTRING, 0, slotNameTable[i] As Long)
i++
Wend
SendMessage(BSMWnd(BSM_CT), CB_SETCURSEL, 0, 0)
i = 0
while packNameTable[i]<>0
SendMessage(BSMWnd(BSM_MT), CB_ADDSTRING, 0, packNameTable[i] As DWord)
i++
Wend
SendMessage(BSMWnd(BSM_MT), CB_SETCURSEL, BSM_UNKNOWN, 0)
'BSMの中身を一覧表示する。
'CREATE時点ではスロットタイプがMainWndから送られてきていないのでメッセージ送るだけ
'PostMessage(BSMWnd(CommandButton3),BM_CLICK,0,0)
SendMessage(BSMWnd(ListBox1), LB_ADDSTRING, 0, "(Press detect button)" As Long)
End Sub
Sub BSM_createCartInfoForBSX(ByRef virtualHeader As SFC_CART_INFO,ByRef mmapInfo AS SFC_MMAP_MODEL)
Dim slot_idx As Long
Dim pack_idx AS Long
slot_idx = SendMessage(BSMWnd(BSM_CT), CB_GETCURSEL, 0, 0)
pack_idx = SendMessage(BSMWnd(BSM_MT), CB_GETCURSEL, 0, 0)
If slot_idx=-1 Then DBM("Slot Type not selected."):exitsub
If pack_idx=-1 Then DBM("Mempack Type not selected."):exitsub
With virtualHeader
lstrcpy(.Title, "BSX Mempack")
.enableChecksum = FALSE
.RomSize = BSM_GetMempackSize(pack_idx)
.MapType = SFC_MAP_HiROM
End With
With mmapInfo
.rom.adr = BSM_GetMempackAdress(slot_idx)
.rom.isLoROM = BSM_isLoROM(slot_idx)
End With
if mmapInfo.rom.isLoROM then
mmapInfo.rom.next_cb = AddressOf(LoROM_nextAddress)
Else
mmapInfo.rom.next_cb = AddressOf(HiROM_nextAddress)
endif
If pack_idx = BSM_SAMEGAME or pack_idx = BSM_GNEXT Then
virtualHeader.RomSize = 512 * 1024
if pack_idx = BSM_SAMEGAME then
virtualHeader.enableChecksum = TRUE
virtualHeader.CheckSum1 = &H235C
virtualHeader.CheckSum2 = &HDCA3
endif
End If
End Sub
Sub BSMemWnd_CommandButton1_Click()
Dim slot_idx As Long
slot_idx = SendMessage(BSMWnd(BSM_CT), CB_GETCURSEL, 0, 0)
If slot_idx = BSM_SLOT_SA1 Then
BSM_SA1_MemPackAccess(TRUE)
End If
'ダイアログの設定から仮想SFCヘッダを生成してダンプ
Dim virtualHeader As SFC_CART_INFO, mmap As SFC_MMAP_MODEL
BSM_createCartInfoForBSX(virtualHeader, mmap)
Dim path AS BytePtr
#ifndef _DEBUG
path = SaveDialogCalloc(ex"BSMイメージ(*.bs)\0*.bs\0SFC ROMイメージ(*.sfc)\0*.bs\0すべてのファイル(*.*)\0*.*\0\0", "bs", virtualHeader.Title)
If path = 0 Then ExitSub
#else
path = "bsdump.bs"
#endif
MainWnd_DumpRomToFile(VarPtr(virtualHeader), VarPtr(mmap), path)
#ifndef _DEBUG
free(path)
#endif
If slot_idx = BSM_SLOT_SA1 Then
BSM_SA1_MemPackAccess(FALSE)
End If
End sub
Sub BSMemWnd_Destroy()
hBSMem = NULL
End Sub
Sub BSMemWnd_BSM_MT_SelChange()
If SendMessage(BSMWnd(BSM_MT), CB_GETCURSEL, 0, 0) > BSM_8M_TYPE4 Then
SetWindowText(BSMWnd(EditBox1), "512")
Else
SetWindowText(BSMWnd(EditBox1), "1024")
End If
End Sub
'SA-1のバンク切り替えでメモリパックにアクセスできるようにする
Sub BSM_SA1_MemPackAccess(isStart AS BOOL)
If isStart Then
SetCPU_Clock(hCOM, HKAC_CLOCK_CPU_OVERCLOCKED)
Sleep(500)
SetCartRegister(&H00, &H2223, &H04, FALSE)
Else
SetCartRegister(&H00, &H2223, &H00, FALSE)
SetCPU_Clock(hCOM, HKAC_CLOCK_NORMAL)
End If
End Sub
Sub BSMemWnd_CommandButton3_Click()
If hCOM = NULL Then ExitSub
Dim info As * BSM_MEMPACK_INFO
info = calloc(sizeof(BSM_MEMPACK_INFO))
BSM_GetMemPackInfo(info)
Dim mpstatus[15] AS Byte
If info->MP = &H504D Then
lstrcpy(mpstatus,"OK (..)")
Else
lstrcpy(mpstatus,"NG (..)")
End If
if (info->MP And &H00FF) >= &H20 Then mpstatus[4] = (info->MP) As Byte
if (info->MP >> 8) >= &H20 Then mpstatus[5] = (info->MP >> 8) As Byte
Dim serial_broken="" As BytePtr
Dim i As Long
for i=0 to 7
if IsCharAlpha(info->Serial[i]) then
serial_broken="(invalid)"
endif
Next
If SendMessage(BSMWnd(BSM_CT), CB_GETCURSEL, 0, 0) = BSM_SLOT_SA1 Then
DBM("MemoryPackInfo is not supported on SA-1")
Else
DBM(sprintfStr(ex"MemoryPackInfo:\r\n MagicNum : %s\r\n FlashType : %d\r\n Capacity : %dKB\r\n BlockNum? : %d\r\n SerialNo? : %s%s\r\n", _
mpstatus As DWord, (info->DeviceInfo>>4) As DWord, _
(1 << (info->DeviceInfo And &H0F) ) As DWord, _
1<<info->Block,
info->Serial, serial_broken
))
End If
Dim typeNumber AS Long
typeNumber = info->DeviceInfo>>4
If typeNumber>=1 And typeNumber<=4 Then
SendMessage(BSMWnd(BSM_MT), CB_SETCURSEL, typeNumber -1, 0)
Else If headerList[0]<>NULL Then
If lstrcmpi(headerList[0]->raw.title, "さめがめROM!") = 0 Then
SendMessage(BSMWnd(BSM_MT), CB_SETCURSEL, BSM_SAMEGAME, 0)
End If
Else
SendMessage(BSMWnd(BSM_MT), CB_SETCURSEL, BSM_UNKNOWN, 0)
DBM("Illegal Memory Pack Type")
End If
End sub
Function bcd2char(digit as byte) As Byte
if digit<10 then
bcd2char=GetByte("0") + digit
Else if digit < 16 then
bcd2char=GetByte("A") + digit - 10
Else
bcd2char=bcd2char(digit And &HF)
endif
End Function
'メモリパック自体の情報と、中身のファイルリストを取得する(いつか分離する)
Function BSM_GetMemPackInfo(mempackinfo AS * BSM_MEMPACK_INFO) AS BOOL
Dim idx AS DWord, cmdBank AS Byte, isLoROM AS BOOL
Dim info[32] AS Byte
Dim cmdAddress AS DWord, cmdOffset As Word
idx = SendMessage(BSMWnd(BSM_CT), CB_GETCURSEL, 0, 0)
cmdAddress = BSM_GetMempackAdress(idx)
cmdBank = (cmdAddress>>16) AS Byte
cmdOffset = (cmdAddress And &HFFFF) As Word
' isLoROM = BSM_isLoROM(idx)
' if isLoROM Then cmdBank >>= 1
isLoROM = FALSE
FlushCom(hCOM)
' Todo: LoROM対応をまともに
DBM("Bank:"+Hex$(cmdBank)+" isLoROM:"+Str$(isLoROM))
If idx = BSM_SLOT_BIOS Then
'Disable BS-X MCC Write Protects
SetCPU_Clock(hCOM, HKAC_CLOCK_CPU_ENABLED)
Sleep(100)
'C0:0000- WriteEnable
SetCartRegister(&H0C, cmdOffset + &H5000, &H80, FALSE)
SetCartRegister(&H0D, cmdOffset + &H5000, &H80, FALSE)
SetCartRegister(&H0E, cmdOffset + &H5000, &H80, FALSE)
Sleep(3)
Else If idx = BSM_SLOT_SA1 Then
BSM_SA1_MemPackAccess(TRUE)
End If
'request chip info part1
SetCartRegister(cmdBank, cmdOffset + &H0000, &H38, isLoROM)
SetCartRegister(cmdBank, cmdOffset + &H0000, &HD0, isLoROM)
'WAIT(コマンドにオーバーヘッドあるから大丈夫)
ReadROM(hCOM, info, 0, cmdAddress + 2, 1, isLoROM)
'enter status mode
SetCartRegister(cmdBank, cmdOffset + &H0000, &H71, isLoROM)
'WAIT
ReadROM(hCOM, info, 0, cmdAddress or &H00FF00, 16, isLoROM)
Dump(info,16)
'request chip info part2
SetCartRegister(cmdBank, cmdOffset + &H0000, &H72, isLoROM)
SetCartRegister(cmdBank, cmdOffset + &H0000, &H75, isLoROM)
ReadROM(hCOM, info, 0, cmdAddress or &H00FF00, 16, isLoROM)
Dump(info, 16)
mempackinfo->MP = (info[0] Or info[2]<<8) AS Word
mempackinfo->Block = info[4]
mempackinfo->DeviceInfo = info[6]
Dim i as long
for i=0 to 4
mempackinfo->Serial[i*2+0] = bcd2char(info[8+i*2]>>4)
mempackinfo->Serial[i*2+1] = bcd2char(info[8+i*2] And &HF)
' Dump(mempackinfo,sizeof(BSM_MEMPACK_INFO))
Next i
mempackinfo->Serial[8] = 0
memcpy(mempackinfo->raw, info,16)
'exit status mode
SetCartRegister(cmdBank, cmdOffset + &H0000, &HFF, isLoROM)
Dim virtualHeader As SFC_CART_INFO, mmap AS SFC_MMAP_MODEL
BSM_createCartInfoForBSX(virtualHeader, mmap)
if mempackinfo->MP = GetWord("MP") then
DBM(ex"MemoryPack has found!\r\n")
Else
DBM(ex"MemoryPack is not working.\r\n")
endif
If BSM_genHeaderList(BSM_HANDLE_SERIAL, hCOM, mmap.rom.adr, mmap.rom.next_cb) = FALSE Then
SendMessage(BSMWnd(ListBox1), LB_RESETCONTENT, 0, 0)
SendMessage(BSMWnd(ListBox1), LB_ADDSTRING, 0, "(Empty)" As Long)
Goto * BSM_DITECT_BTN_EXIT
End If
BSM_listupFiles()
* BSM_DITECT_BTN_EXIT
If idx = BSM_SLOT_BIOS Then
SetCPU_Clock(hCOM, HKAC_CLOCK_NORMAL)
ElseIf idx = BSM_SLOT_SA1 Then
BSM_SA1_MemPackAccess(FALSE)
End If
End Function
Sub BSMemWnd_DropFiles(hDrop As HDROP)
Dim buf[MAX_PATH] As Byte
Dim i as long
Do
If DragQueryFile(hDrop, i, buf, MAX_PATH) = 0 Then Exit Do
i++
Loop
DragFinish(hDrop)
If BSM_genHeaderList(BSM_HANDLE_FILEPATH, buf, 0, AddressOf(RomImage_nextAddress)) = FALSE Then
DBM("This is not BS Memory Image File.")
ExitSub
End If
BSM_listupFiles()
End Sub
Sub BSM_listupFiles()
SendMessage(BSMWnd(ListBox1), LB_RESETCONTENT, 0, 0)
If headerList[0] = NULL Then
SendMessage(BSMWnd(ListBox1), LB_ADDSTRING, 0, "(Press detect button)" As Long)
ExitSub
End If
Dim str AS StringClass, title[16] AS Byte
Dim i = 0 As Long
while headerList[i]<>0
memcpy(title, headerList[i]->raw.title, 16)
str.set("")
str.sprintf("#%d %s %d/%d", i, title As DWord, headerList[i]->raw.month >> 4, headerList[i]->raw.day >> 3)
SendMessage(BSMWnd(ListBox1), LB_ADDSTRING, 0, str.ptr As Long)
i++
Wend
End Sub