forked from pepijndevos/arduino-boy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameboy.ino
217 lines (196 loc) · 5.99 KB
/
gameboy.ino
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
#include <EEPROM.h>
#include "pokemon.h"
#include "output.h"
#define MOSI_ 22
#define MISO_ 23
#define SCLK_ 24
int bytes = 0;
uint8_t shift = 0;
uint8_t in_data = 0;
uint8_t out_data = 0;
connection_state_t connection_state = NOT_CONNECTED;
trade_centre_state_t trade_centre_state = INIT;
int counter = 0;
int trade_pokemon = -1;
unsigned long last_bit;
void transferBit(void) {
in_data |= digitalRead(MISO_) << (7-shift);
if(++shift > 7) {
shift = 0;
bytes++;
out_data = handleIncomingByte(in_data);
//Serial.print(bytes);
//Serial.print(" ");
Serial.print(trade_centre_state);
Serial.print(" ");
Serial.print(in_data, HEX);
Serial.print(" ");
Serial.print(out_data, HEX);
Serial.print("\n");
in_data = 0;
}
while(!digitalRead(SCLK_));
//Serial.print(out_data & 0x80 ? HIGH : LOW);
digitalWrite(MOSI_, out_data & 0x80 ? HIGH : LOW);
out_data <<= 1;
}
void setup() {
Serial.begin(115200);
pinMode(SCLK_, INPUT);
pinMode(MISO_, INPUT);
pinMode(MOSI_, OUTPUT);
Serial.print("hello world\n");
for (int i=0; i<44+11+11; i++) {
Serial.print(" ");
Serial.print(EEPROM.read(i), HEX);
}
Serial.print("\n");
digitalWrite(MOSI_, LOW);
out_data <<= 1;
}
void loop() {
last_bit = micros();
while(digitalRead(SCLK_)) {
if (micros() - last_bit > 1000000) {
// the Game Boy is silent. A good time to do book keeping.
Serial.print("idle\n");
last_bit = micros();
shift = 0;
in_data = 0;
if(trade_pokemon >= 0 && trade_centre_state < TRADE_PENDING) {
// a trade has been confrimed
int i;
int start = 19 + (trade_pokemon * 44);
for (i=0; i<44; i++) {
EEPROM.write(i, INPUT_BLOCK[start+i]);
Serial.print(" ");
Serial.print(INPUT_BLOCK[start+i], HEX);
}
Serial.print("\nOT\n");
start = 283 + (trade_pokemon * 11);
for (i=0; i<11; i++) {
EEPROM.write(i+44, INPUT_BLOCK[start+i]);
Serial.print(" ");
Serial.print(INPUT_BLOCK[start+i], HEX);
}
Serial.print("\nnick\n");
start = 349 + (trade_pokemon * 11);
for (i=0; i<11; i++) {
EEPROM.write(i+44+11, INPUT_BLOCK[start+i]);
Serial.print(" ");
Serial.print(INPUT_BLOCK[start+i], HEX);
}
trade_pokemon = -1;
Serial.print("\ntrade saved\n");
}
}
}
transferBit();
}
byte handleIncomingByte(byte in) {
byte send = 0x00;
switch(connection_state) {
case NOT_CONNECTED:
if(in == PKMN_MASTER)
send = PKMN_SLAVE;
else if(in == PKMN_BLANK)
send = PKMN_BLANK;
else if(in == PKMN_CONNECTED) {
send = PKMN_CONNECTED;
connection_state = CONNECTED;
}
break;
case CONNECTED:
if(in == PKMN_CONNECTED)
send = PKMN_CONNECTED;
else if(in == PKMN_TRADE_CENTRE)
connection_state = TRADE_CENTRE;
else if(in == PKMN_COLOSSEUM)
connection_state = COLOSSEUM;
else if(in == PKMN_BREAK_LINK || in == PKMN_MASTER) {
connection_state = NOT_CONNECTED;
send = PKMN_BREAK_LINK;
} else {
send = in;
}
break;
case TRADE_CENTRE:
if(trade_centre_state == INIT && in == 0x00) {
trade_centre_state = READY_TO_GO;
send = 0x00;
} else if(trade_centre_state == READY_TO_GO && in == 0xFD) {
trade_centre_state = SEEN_FIRST_WAIT;
send = 0xFD;
} else if(trade_centre_state == SEEN_FIRST_WAIT && in != 0xFD) {
// random data of slave is ignored.
send = in;
trade_centre_state = SENDING_RANDOM_DATA;
} else if(trade_centre_state == SENDING_RANDOM_DATA && in == 0xFD) {
trade_centre_state = WAITING_TO_SEND_DATA;
send = 0xFD;
} else if(trade_centre_state == WAITING_TO_SEND_DATA && in != 0xFD) {
counter = 0;
// send first byte
send = pgm_read_byte(&(DATA_BLOCK[counter]));
INPUT_BLOCK[counter] = in;
trade_centre_state = SENDING_DATA;
counter++;
} else if(trade_centre_state == SENDING_DATA) {
// if EEPROM is not initialised, please use the pgm data only.
if (counter == 12) {
send = EEPROM.read(0); // pokemon species
} else if(counter >= 19 && counter < 19+44) {
send = EEPROM.read(counter-19); // pokemon data
} else if(counter >= 283 && counter < 283+11) {
send = EEPROM.read((counter-283)+44); // trainer name
} else if(counter >= 349 && counter < 349+11) {
send = EEPROM.read((counter-349)+44+11); // nickname
} else {
send = pgm_read_byte(&(DATA_BLOCK[counter]));
}
INPUT_BLOCK[counter] = in;
counter++;
if(counter == PLAYER_LENGHT) {
trade_centre_state = SENDING_PATCH_DATA;
}
} else if(trade_centre_state == SENDING_PATCH_DATA && in == 0xFD) {
counter = 0;
send = 0xFD;
} else if(trade_centre_state == SENDING_PATCH_DATA && in != 0xFD) {
send = in;
counter++;
if(counter == 197) {
trade_centre_state = TRADE_PENDING;
}
} else if(trade_centre_state == TRADE_PENDING && (in & 0x60) == 0x60) {
if (in == 0x6f) {
trade_centre_state = READY_TO_GO;
send = 0x6f;
} else {
send = 0x60; // first pokemon
trade_pokemon = in - 0x60;
}
} else if(trade_centre_state == TRADE_PENDING && in == 0x00) {
send = 0;
trade_centre_state = TRADE_CONFIRMATION;
} else if(trade_centre_state == TRADE_CONFIRMATION && (in & 0x60) == 0x60) {
send = in;
if (in == 0x61) {
trade_pokemon = -1;
trade_centre_state = TRADE_PENDING;
} else {
trade_centre_state = DONE;
}
} else if(trade_centre_state == DONE && in == 0x00) {
send = 0;
trade_centre_state = INIT;
} else {
send = in;
}
break;
default:
send = in;
break;
}
return send;
}