-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathems.py
executable file
·184 lines (141 loc) · 4.87 KB
/
ems.py
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
#!/usr/bin/python
'''
ems - EMS flashcart utility
Copyright (C) 2014, Tomasz Finc <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'''
import usb.core
import usb.util
import argparse
import io, sys
from EMSCart import EMSCart
from EMSCart import GameBoyRom
PARSER = argparse.ArgumentParser(
description='EMS flashcart utility')
PARSER.add_argument('-t', '--header', action="store_true",
help='read header')
PARSER.add_argument('-s', '--sram', action="store_true",
help='read sram')
PARSER.add_argument('-r', '--read', action="store_true",
help='read bank')
PARSER.add_argument('-b', '--bank', type=int, choices=[1, 2],
default=1)
PARSER.add_argument('-o', '--output', help='output filename',
default="out.rom")
PARSER.add_argument('-d', action="store_true",
help='dry run')
ARGS = PARSER.parse_args()
ems = EMSCart()
gb = GameBoyRom()
BLOCK_READ = 4096
DEV = usb.core.find(idVendor=ems.VENDOR, idProduct=ems.PRODUCT)
### UTIL ###
def _init():
'''_init - initalize and capture the usb device'''
if DEV is None:
sys.exit('Aborting: EMS cart not found')
DEV.set_configuration()
# get an endpoint instance
cfg = DEV.get_active_configuration()
intf = cfg[(0, 0)]
ep_ = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match= (
lambda e:
usb.util.endpoint_direction(e.bEndpointAddress) ==
usb.util.ENDPOINT_OUT))
assert ep_ is not None
def _buildcmd(cmd, addr, end, addl=''):
'''_buildcmd - contruct an ems command string'''
msg = (format(cmd, 'x') + format(addr, 'x').zfill(8) +
format(end, 'x').zfill(8) + addl)
return _format(msg)
def _format(buffer):
'''_format - convert hex string to byte string'''
return buffer.decode('hex')
def _sendcmd(buffer, length):
'''_send - send byte string to usb device'''
if len(buffer) == 9:
return _usbbulktransfer(buffer, length)
else:
print "Error: Incorrect buffer command length %i" % (len(buffer))
print "Command: %s" % (buffer)
def _usbbulktransfer(msg, length):
'''_usbbulktransfer - send the given msg to the USB device'''
sret = ''
if not ARGS.d:
DEV.write(ems.WRITE_ENDPOINT, msg, ems.WRITE_TIMEOUT)
ret = DEV.read(ems.READ_ENDPOINT, length, ems.WRITE_TIMEOUT)
sret = ''.join([chr(x) for x in ret])
return sret
def _write(data):
'''_write - write out data to file'''
output = io.FileIO(ARGS.output, 'wb')
output.write(data)
output.close()
### END OF UTIL ###
### CART ###
def _readheader():
'''_readheader - read bank 1 and 2 headers'''
print "Reading EMS Cart Headers"
for bank in ems.BANKS:
addr = ems.BANK_START[bank]
cmd = _buildcmd(ems.READ_ROM, addr, ems.END_ROM)
resp = _sendcmd(cmd, gb.HEADER_LENGTH)
print "Bank: %i %s" % (bank, resp[gb.ROM_HEADER_START:0x144])
def _readsram():
'''_readsram - read cart sram'''
print "Reading SRAM"
offset = 0
output = ''
while offset < ems.SRAM_SIZE:
addr = offset + ems.SRAM_START
print "Reading SRAM Address: 0x%x" % (addr)
cmd = _buildcmd(ems.READ_SRAM, addr, ems.END_SRAM)
resp = _sendcmd(cmd, BLOCK_READ)
output += resp
offset += BLOCK_READ
return output
def _readcart(bank):
'''_readcart - read one cart bank'''
print "Reading bank: %i" % (bank)
start = ems.BANK_START[bank]
offset = 0
output = ''
while offset < ems.BANK_SIZE:
addr = offset + start
print "Reading Address: 0x%x" % (addr)
cmd = _buildcmd(ems.READ_ROM, addr, ems.END_ROM_READ)
resp = _sendcmd(cmd, BLOCK_READ)
output += resp
offset += BLOCK_READ
return output
### END OF CART ###
### MAIN ###
def main():
'''main - master of all'''
if not ARGS.d:
_init()
if ARGS.header:
_readheader()
elif ARGS.sram:
sram = _readsram()
if sram:
_write(sram)
elif ARGS.read:
bank = _readcart(ARGS.bank)
if bank:
_write(bank)
### END OF MAIN ###
main()