Skip to content

Commit 8c9ba99

Browse files
committed
now with 150% more self hatred
1 parent e58821e commit 8c9ba99

File tree

7 files changed

+702
-111
lines changed

7 files changed

+702
-111
lines changed

arduino/pksploit_link_bridge/pksploit_link_bridge.ino

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ volatile trade_centre_state_t nextstate = PKSPLOIT_MENU;
5454
volatile int counter = 0;
5555
volatile byte command = 0x00;
5656
volatile int counter2 = 00;
57+
volatile int cmddata = -1;
5758
volatile connection_state_t connection_state = NOT_CONNECTED;
5859
volatile trade_centre_state_t trade_centre_state = INIT;
59-
60+
char senddata[1024];
61+
volatile int counter3=0;
62+
volatile boolean fillbuffer = false;
6063

6164

6265
void setup() {
@@ -106,7 +109,9 @@ void clockInterrupt(void) {
106109
if(outputBuffer & 0x80 ? SO_PIN : 0!=0)
107110
{
108111
digitalHigh(SO_PIN);
109-
}else{digitalLow(SO_PIN);}
112+
digitalHigh(ledStatus);
113+
}else{digitalLow(SO_PIN);
114+
digitalLow(ledStatus);}
110115

111116
outputBuffer = outputBuffer << 1;
112117

@@ -117,10 +122,12 @@ byte handleIncomingByte(byte in) {
117122

118123
switch(connection_state) {
119124
case NOT_CONNECTED:
125+
Serial.write(".");
120126
if (in==0xCD)
121127
{connection_state = TRADE_CENTRE;
122128
trade_centre_state = PKSPLOIT_MENU;
123129
Serial.print("StatusMQ");}
130+
124131
if(in == PKMN_MASTER)
125132
send = PKMN_SLAVE;
126133
else if(in == PKMN_BLANK)
@@ -171,10 +178,10 @@ byte handleIncomingByte(byte in) {
171178
send = in;
172179
} else if(trade_centre_state == WAITING_TO_SEND_DATA && (in & 0xF0) != 0xF0) {
173180
counter = 0;
174-
send = DATA_BLOCK[counter++];
181+
send = pgm_read_byte_near(DATA_BLOCK+counter++);
175182
trade_centre_state = SENDING_DATA;
176183
} else if(trade_centre_state == SENDING_DATA) {
177-
send = DATA_BLOCK[counter++];
184+
send = pgm_read_byte_near(DATA_BLOCK+counter++);
178185
if(counter == 619) {
179186
trade_centre_state = PKSPLOIT_MENU;
180187
Serial.print("StatusMenu");
@@ -191,25 +198,41 @@ byte handleIncomingByte(byte in) {
191198
case 34: //Internal CMD 0x22, set nextstate to PKSPLOIT_DUMP_BLOCK
192199
nextstate=PKSPLOIT_DUMP_BLOCK;
193200
send=00;
201+
194202
break;
195203
case 68: //Internal CMD 0x44, set nextstate to PKSPLOIT_SEND_BLOCK
196204
nextstate=PKSPLOIT_SEND_BLOCK;
205+
//Serial.print("e");
197206
send=00;
207+
counter3=0;
208+
cmddata=5;
209+
break;
210+
case 119:
211+
//Failsafe to ensure we're still in sync, we don't want to write off by one data or garbage data to the gameboy
212+
if(Serial.read()!=0xDE){Serial.print("no");return 0x00;}
213+
if(Serial.read()!=0xAD){Serial.print("no");return 0x00;}
214+
if(Serial.read()!=0xBE){Serial.print("no");return 0x00;}
215+
if(Serial.read()!=0xEF){Serial.print("no");return 0x00;}
216+
cli();
217+
fillbuffer=true;
218+
return 0x00;
198219
break;
199220
case 17: //Internal CMD 0x11
200221
counter2=Serial.read()*256+Serial.read();
201222
send=00;
223+
202224
break;
203225
}
204226

205227
sending=true;}
206-
228+
if(cmddata>0){cmddata--;}
229+
if(cmddata==0){sending=false;}
207230
if(!Serial.available()){sending=false;}
208231

209232
}
210233
else if(trade_centre_state == PKSPLOIT_DUMP_BLOCK) {
211234
Serial.write(in);
212-
235+
send=Serial.read();
213236
if(counter2==0)
214237
{
215238
trade_centre_state = PKSPLOIT_MENU;
@@ -220,6 +243,25 @@ byte handleIncomingByte(byte in) {
220243

221244

222245

246+
}else if(trade_centre_state == PKSPLOIT_SEND_BLOCK) {
247+
send=senddata[counter3];
248+
249+
if(counter2==counter3)
250+
{
251+
//Serial.print(counter2);
252+
//Serial.print("-");
253+
//Serial.print(counter3);
254+
counter2=0;
255+
counter3=0;
256+
trade_centre_state = PKSPLOIT_MENU;
257+
nextstate = PKSPLOIT_MENU;
258+
cmddata=-1;
259+
Serial.print("StatusMenu");
260+
}
261+
counter3++;
262+
return send;
263+
264+
223265
} else {
224266
send = in;
225267
}
@@ -237,5 +279,24 @@ byte handleIncomingByte(byte in) {
237279
}
238280

239281
void loop() {
282+
if(fillbuffer==true)
283+
{
284+
Serial.print("buffer");
285+
Serial.print(counter2);
286+
Serial.flush();
287+
connection_state = NOT_CONNECTED;
288+
289+
trade_centre_state = INIT;
290+
291+
//Disconnect because of possible desync. reconnect should be seemless (fingers crossed)
292+
Serial.readBytes(senddata,counter2);
293+
294+
fillbuffer=false;
240295

296+
sei();
297+
298+
299+
300+
301+
}
241302
}

build/build.py

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
#!python2
12
import os
23
import sys
34
import shutil
45
import ConfigParser
56

67

7-
print "PkSploit Build Script - Revision 24-09-17"
8+
print "PkSploit Build Script - Revision 24-11-17"
89

910
if not os.path.exists("config.ini"):
1011
print "No config.ini file! Rename and edit sample_config.ini in this folder!"
@@ -30,21 +31,23 @@ def ConfigSectionMap(section):
3031
return dict1
3132

3233

34+
3335
choice=""
34-
everything=4
36+
everything=5
3537
if len(sys.argv) > 1:
3638
if sys.argv[1] == "full":
3739
choice=everything
3840

3941
odir=ConfigSectionMap("General")["outputdir"]
40-
while choice not in range(1,5):
42+
while choice not in range(1,6):
4143
print "---------------------------"
4244
print "What do you want to build?"
4345
print ""
4446
print "1. Assemble Gameboy ASM"
45-
print "2. Prepare Arduino Project"
46-
print "3. Build and Upload Arduino Project"
47-
print "4. Everything (run \"py build.py full\" to skip this menu in the future)"
47+
print "2. Assemble Gameboy ASM (Patch Savefile for quick start)"
48+
print "3. Prepare Arduino Project"
49+
print "4. Build and Upload Arduino Project"
50+
print "5. Everything (run \"py build.py full\" to skip this menu in the future)"
4851
print ""
4952
print "9. Exit"
5053
try:
@@ -58,9 +61,17 @@ def ConfigSectionMap(section):
5861
choice = 0
5962

6063
def assemble():
61-
print "Assembling Gameboy Code"
62-
63-
os.system("rgbasm.exe -o "+odir+"temp.o ../gb_asm/main.asm")
64+
print "Assembling Gameboy Code with trade offsets"
65+
if os.path.exists(odir+"/gb_asm_trade/"):
66+
shutil.rmtree(odir+"/gb_asm_trade/")
67+
shutil.copytree("../gb_asm/",odir+"/gb_asm_trade/")
68+
fa=open(odir+"/gb_asm_trade/main.asm","rb")
69+
code=fa.read()
70+
fa.close()
71+
fa=open(odir+"/gb_asm_trade/main.asm","wb")
72+
fa.write("rOFFSET EQUS \"$c486\"\n\rrEXTRA EQUS \"\""+code)
73+
fa.close()
74+
os.system("rgbasm.exe -o "+odir+"temp.o "+odir+"gb_asm_trade/main.asm")
6475
os.system("rgblink.exe -o "+odir+"temp.gb "+odir+"temp.o")
6576
fi = open(odir+"temp.gb","rb")
6677
gb_rom = fi.read()
@@ -71,6 +82,55 @@ def assemble():
7182
print "Done Assembling Gameboy Code"
7283
return
7384

85+
def makesave():
86+
print "Assembling Gameboy Code with save file offsets"
87+
if os.path.exists(odir+"/gb_asm_save/"):
88+
shutil.rmtree(odir+"/gb_asm_save/")
89+
shutil.copytree("../gb_asm/",odir+"/gb_asm_save/")
90+
fa=open(odir+"/gb_asm_save/main.asm","rb")
91+
code=fa.read()
92+
fa.close()
93+
fa=open(odir+"/gb_asm_save/main.asm","wb")
94+
fa.write("rOFFSET EQUS \"$d280\"\n\rrEXTRA EQUS \"jr .turnoff\""+code)
95+
fa.close()
96+
os.system("rgbasm.exe -o "+odir+"temp_save.o "+odir+"gb_asm_save/main.asm")
97+
os.system("rgblink.exe -o "+odir+"temp_save.gb "+odir+"temp_save.o")
98+
fi = open(odir+"temp_save.gb","rb")
99+
gb_rom = fi.read()
100+
fi.close()
101+
fo = open(odir+"main_save.bin", "wb")
102+
fo.write(gb_rom[0x150:0x214])
103+
fo.close()
104+
print "Done Assembling Gameboy Code"
105+
106+
fsg=open("../savefile_templates/pokemon_blue_german.sav","rb")
107+
save=fsg.read()
108+
109+
110+
111+
fsg.close()
112+
fp = open(odir+"main_save.bin","rb")
113+
code=fp.read()
114+
fp.close()
115+
116+
save=save[:9847]+code+save[10043:]
117+
118+
#generate valid checksum
119+
checksum=0
120+
save_data = map(ord, save[9624:13602])
121+
for num,bb in enumerate(save_data):
122+
checksum=checksum+bb
123+
flip=0xFF
124+
checksum=chr((checksum%256)^flip)
125+
126+
save=save[:13603]+checksum+save[13604:]
127+
128+
fsgs = open(odir+"pokemon_blue_german_pksploit.sav", "wb")
129+
fsgs.write(save)
130+
fsgs.close()
131+
return
132+
133+
74134
def prepare():
75135
print "Preparing Arduino Project"
76136

@@ -111,7 +171,7 @@ def prepare():
111171

112172
party_and_patchlist = ", ".join(map(str, party + [253, 253, 253, 253, 253] + patchlist))
113173
fileo = open(odir+ConfigSectionMap("Arduino")["projectname"]+"/data.h","wb")
114-
fileo.write("unsigned char DATA_BLOCK[619] = {" + party_and_patchlist + "};")
174+
fileo.write("unsigned const char DATA_BLOCK[] PROGMEM = {" + party_and_patchlist + "};")
115175
fileo.close()
116176
print "Done Preparing Arduino Project"
117177
return
@@ -134,9 +194,12 @@ def buildarduino():
134194
assemble()
135195

136196
if choice == 2 or choice == everything:
197+
makesave()
198+
199+
if choice == 3 or choice == everything:
137200
prepare()
138201

139-
if choice == 3 or choice == everything:
202+
if choice == 4 or choice == everything:
140203
buildarduino()
141204

142205

build/sample_config.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#EDIT THIS CONFIG AND SAVE IT AS config.ini
2+
13
[General]
24
outputdir=dist/
35
[Arduino]

build/sample_config.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)