This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GBA.py
195 lines (156 loc) · 4.33 KB
/
GBA.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
185
186
187
188
189
190
191
192
193
194
import os
import time
import binascii
import RPi.GPIO as GPIO
import sys
import time
class progressBar():
def __init__(self, currentValue, maxValue):
self.currentValue = currentValue
self.maxValue = maxValue
self.size = 50
def tick(self):
self.currentValue += 1
progress = int(float(self.currentValue)/float(self.maxValue)*self.size)
paddingSize = len(str(self.maxValue))
currentProgress = str(int(self.currentValue)).rjust(paddingSize,'0')
goalProgress = str(int(self.maxValue)).rjust(paddingSize,'0')
sys.stdout.write("\r" + (currentProgress +"/"+ goalProgress) + ' [' + '#' * progress + ' ' * (self.size-progress) + ']')
if(self.currentValue == self.maxValue):
print("")
class Cartridge():
def __init__(self):
GPIO.setmode(GPIO.BOARD)
self.progress = None
# CS2 connected to 3.3 v.
# WR connected to ground.
self.CS = 10
self.RD = 12
self.IO = [
8,
22,
24,
26,
32,
36,
38,
40,
37,
35,
33,
31,
29,
23,
21,
19,
15,
13,
11,
7,
5,
3,
18,
16
]
GPIO.setup(self.CS, GPIO.OUT)
GPIO.setup(self.RD, GPIO.OUT)
GPIO.output(self.CS, 1 )
GPIO.output(self.RD, 1 )
def intToBinary(self, integer, maxSize=None):
if(maxSize == None):
maxSize = len(self.IO)
binary = "{0:b}".format(integer)
padding = ((maxSize-len(binary)) * "0")
binary = padding + binary
return binary
def getAddress(self, intaddress):
GPIO.output(self.CS, 1 )
GPIO.output(self.RD, 1 )
'''
intToBinary gives the least significant bit on the right side.
We want A0=the least significant bit -> A15=the most significant bit, the easiest way to
align the bit values to the correct line is to just reverse the output from intToBinary.
'''
address = list(reversed(self.intToBinary(intaddress)))
for bit in range(len(address)):
GPIO.setup(self.IO[bit], GPIO.OUT)
if(int(address[bit]) == 1):
GPIO.output(self.IO[bit], 1)
else:
GPIO.output(self.IO[bit], 0)
time.sleep(0.001)
GPIO.output(self.CS, 0 )
time.sleep(0.001)
for pin in self.IO:
GPIO.setup(pin, GPIO.IN)
time.sleep(0.001)
GPIO.output(self.RD, 0 )
time.sleep(0.001)
def getBus(self):
valueAtAddress = 0
for index, inputPin in enumerate(self.IO):
if(GPIO.input(inputPin) == 1):
valueAtAddress += 2 ** index
value1 = (valueAtAddress << 8 & 0xFF00) >> 8
value2 = (valueAtAddress >> 8 & 0x00FF)
value1 = hex(value1).replace("0x", "")
value2 = hex(value2).replace("0x", "")
if(len(value1) % 2 == 1):
value1 = "0" + value1
if(len(value2) % 2 == 1):
value2 = "0" + value2
return value1, value2
def highLowRD(self):
GPIO.output(self.RD, 1 )
time.sleep(0.01)
GPIO.output(self.RD, 0 )
time.sleep(0.01)
def sequential(self, start, finish):
self.getAddress(start)
for i in range(start, finish):
busvalue = self.getBus()
yield (binascii.a2b_hex(busvalue[0]))
yield (binascii.a2b_hex(busvalue[1]))
if(self.progress != None):
self.progress.tick()
self.highLowRD()
def nonSequential(self, start, finish):
for address in range(start, finish):
self.getAddress(address)
busvalue = self.getBus()
yield (binascii.a2b_hex(busvalue[0]))
yield (binascii.a2b_hex(busvalue[1]))
if(self.progress != None):
self.progress.tick()
def decideName(self, inputName):
response = input("Do you want to call the rom file, '{}' ?[Y/n]\n".format(inputName))
if(response.lower() == "y"):
return inputName
elif(response.lower() == "n"):
newName = input("What do you want the file to be called ?\n")
if(len(newName) > 0):
return self.decideName(newName)
else:
return self.decideName(inputName)
else:
return self.decideName(inputName)
def getName(self):
name = []
for byte in self.nonSequential(0x50, 0x50 + 6):
name.append(byte.decode())
return "".join(name).replace("\x00", "").replace(" ", "_")
def getROM(self):
name = self.decideName(self.getName())
outputFile = open(name, "wb")
startTime = time.time()
maxAddress = 0x800000
self.progress = progressBar(0, maxAddress)
for byte in cartridge.nonSequential(0x000, maxAddress):
outputFile.write(byte)
outputFile.close()
GPIO.cleanup()
print("")
print("Done (time {} mins)".format( int(time.time()-startTime) / 60) )
if __name__ == "__main__":
cartridge = Cartridge()
cartridge.getROM()