-
Notifications
You must be signed in to change notification settings - Fork 1
/
GBADumper.abp
232 lines (197 loc) · 5.84 KB
/
GBADumper.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
/* FT232HL GBA Dumper
* Programmed by RGBA_CRT 2018 09
*
* GAMEBOY A0-A15 ::: GBA D0-D15
* GAMEBOY D0-D7 ::: GBA A16-A23
*
*/
'AB-FT232HLib
#include "..\D2XX.sbp"
#include "..\MCP23017_Lib.sbp"
#include "..\FT232HLib_debug.sbp"
'RGBALib
#include <RGBALib.sbp>
#include "ROM_Class_GBA.sbp"
#console
Const FT_COMMAND_BUFFER_SIZE = 1024*200
main()
Sub main()
Print "FT232H-GBAdvance-Dumper v0.3 RGBA_CRT 2018"
'Open FT232H
Dim ft AS *FT232H
ft=new FT232H(FT_COMMAND_BUFFER_SIZE)
if ft->listupDeviceToConsole()=FALSE Then Input "[PRESS ENTER]",ft:End
Dim ftID As DWord
if ft->getNumberOfDevice() > 1 Then input "Select device >",ftID
if ft->openDevice(ftID)=FALSE Then Print "OpenError" : End
printf(ex"device #%02d online.\n\n",ftID)
'init GBA Bus Class
Dim fe AS *FT232H_GBA_ROM
fe=New FT232H_GBA_ROM(ft)
fe->FT_SendCommands()
'Read Header
Dim header AS GBA_ROM_HEADER
Dim romSize AS DWord
Dim cmd AS Long
Do
GetHeaderFromFT(fe,VarPtr(header))
Dump(VarPtr(header)+&HA0,32,&HA0)
Print
'Print Header
Dim title[16] AS Byte, i AS Long
memcpy(title,header.Title,12)
printf(ex"GBA ROM INFO:\n Title: %s\n GameCode : %s\n MakerCode : %c%c\n Version : 1.%d\n UnitCode : %02X (dev:%02X)\n",
title,
Dw2Str(GetDWord(VarPtr(header.GameCode))),
header.MakerCode[0],header.MakerCode[1],
header.Version,
header.DeviceType, header.MainUnitCode
)
'DS Memory Expansion Cartridge
if isSameBin(header.Fixed2,ex"\x24\x24\x24\xFF",4) Then
printf(ex"\nDSメモリー拡張カートリッジは非対応です。\nThis cartridge not supported.\n")
if (GetKeyState(VK_ESCAPE) And &H80)=0 Then Goto *EXIT_PROGRAM
End If
'Logo & Header check
if (CheckNLogo(VarPtr(header)) or CheckHeaderSum(VarPtr(header)))=FALSE Then
colorPrint(PH_ERROR,ex">> [接触不良] Contact failue!\n")
'ESC押してたら強制的に選択画面に
if (GetKeyState(VK_ESCAPE) And &H80)=0 Then Goto *EXIT_PROGRAM
Else
colorPrint(PH_SUCCESS,ex">> GBA cartridge detected!\n")
Endif
'Slove ROM size
romSize=fe->SloveRomSize()
printf(ex"[推定]RomSize 0x%08X -> %dMB \n\n",romSize,romSize/(1024*1024))
ft->flush()
Print "1.DumpRom 2.RomViewer 3.ManualRomSize"
Input "> ",cmd
Select Case cmd
Case 1
'DumpRom
For i = 0 To Len(title)
if title[i] = GetByte("/") Then _
title[i] = GetByte("_")
if title[i] = GetByte(":") Then _
title[i] = GetByte("_")
Next i
DumpRom(fe, MakeStr(title)+".gba",romSize)
Case 2
RomViewer(fe)
Case 3
Input ex"Input Rom Size (ex:&H00800000) \n> ",romSize
DumpRom(fe, MakeStr(title)+".gba",romSize)
Case 4
debug
End Select
if cmd<>0 Then
*EXIT_PROGRAM
Input ex"\n\n[PRESS ENTER]",cmd
Endif
Print ex"\n\n"
Loop
End Sub
'------------------
'Sub routines
'------------------
Sub GetHeaderFromFT(fe As *FT232H_GBA_ROM, header AS *GBA_ROM_HEADER)
fe->AreaReadRequest(0,sizeof(GBA_ROM_HEADER))
fe->FT_SendCommands()
fe->FT_ReceiveData(header,sizeof(GBA_ROM_HEADER))
End Sub
Const GBAD_BUFFER_SIZE=&H4000 '1024*16
sub DumpRom(fe As *FT232H_GBA_ROM, filename AS BytePtr,length AS DWord)
Dim rxbuf AS BytePtr
rxbuf=calloc(GBAD_BUFFER_SIZE)
Dim i AS DWord, lt AS DWord,procesTime As DWord,adr AS DWord,nt AS DWord
Dim out AS File
if out.openFile(filename,GENERIC_WRITE)=FALSE Then printf(ex"File write error! GLE=%d\n",GetLastError()):ExitSub
lt = GetTickCount()
adr=&H00000000
Do
fe->AreaReadRequest(adr,GBAD_BUFFER_SIZE)
fe->FT_SendCommands()
fe->FT_ReceiveData(rxbuf,GBAD_BUFFER_SIZE)
out.write(rxbuf,GBAD_BUFFER_SIZE)
i++
adr=i*GBAD_BUFFER_SIZE
procesTime=GetTickCount()-lt
printf(ex"[DumpROM] %08Xh %d/%dMB %d%% ::: Time:%ds %dkb/s delta:%d \r",
adr, 'Address
adr/&H100000,length/&H100000, 'Dumped Megabyte
(adr/length*100) AS DWord, 'Total Megabyte
procesTime/1000, 'Processing Time
(adr/procesTime) As DWord, 'Throughput
GetTickCount()-nt
)
nt=GetTickCount()
if adr>=length Then Exitdo
if CheckCancelKey() Then printf("\n\n\n>>>>> Canceled by user.\n"):exitdo
Loop
out.close()
free(rxbuf)
printf(ex"\nRom saved to %s\n",filename)
End Sub
'NLogoのチェックサム
Function CheckNLogo(header AS *GBA_ROM_HEADER) AS BOOL
Dim i aS Long,sum AS DWord
For i=0 To 155
sum+=header->NLogo[i]
Next i
printf(ex" NLogoSum : %x\n",sum)
if sum=&H4B1B Then CheckNLogo=TRUE
End Function
'ヘッダのチェックサム
Function CheckHeaderSum(header AS *GBA_ROM_HEADER) AS BOOL
Dim i aS Long,sum AS DWord
For i=&HA0 To &HBC
sum=sum-GetByte(header+i)
Next i
sum= (sum - &H19) AND &HFF
printf(ex" HeaderSum : %x %x\n",sum, header->HeaderSum)
if sum=header->HeaderSum Then CheckHeaderSum=TRUE
End Function
'指定領域が特定のバイトで埋められているかチェック
Function isFilled(adr As BytePtr,size As DWord,checkByte As Byte) As Long
Dim i As Long
For i=0 To size-1
if adr[i]<>checkByte Then ExitFunction
Next
isFilled=TRUE
End Function
'GBAのアドレス空間を眺める
Sub RomViewer(fe AS *FT232H_GBA_ROM)
Dim buf[256] AS Byte
Do
Dim adr aS Long
input "ADDRESS > ",adr
if adr=-1 Then ExitDo
fe->AreaReadRequest(adr,128)
fe->FT_SendCommands()
fe->FT_ReceiveData(buf,128)
Dump(buf,128,adr)
fe->ft->flush()
Loop
End Sub
'キャンセルキーが押されているか判断
Function CheckCancelKey() AS BOOL
if GetKeyState(VK_ESCAPE) And &H80 Then
CheckCancelKey=TRUE
Else
CheckCancelKey=FALSE
End If
End Function
'https://problemkaputt.de/gbatek.htm#gbacartridgeheader
Type GBA_ROM_HEADER
EntryPoint AS DWord
NLogo[155] AS Byte
Title[11] AS Byte
GameCode[3] AS Byte
MakerCode[1] AS Byte
Fixed1 AS Byte
MainUnitCode AS Byte
DeviceType AS Byte
Fixed2[6] AS Byte
Version AS Byte
HeaderSum AS Byte
End Type