Compare commits

..

No commits in common. "main" and "v0.0.1" have entirely different histories.
main ... v0.0.1

4 changed files with 119 additions and 108 deletions

2
.gitignore vendored
View File

@ -63,5 +63,3 @@ a.ihx
.vscode/settings.json
.vscode/c_cpp_properties.json
.DS_Store
pokedistro.gb
pokedistro2.gb

View File

@ -1,5 +1,4 @@
# CC = /opt/gbdk/bin/lcc -Wa-l -Wl-m -Wl-j
CC = ~/gbdk-2020/build/gbdk/bin/lcc -Wa-l -Wl-m -Wl-j
CC = /opt/gbdk/bin/lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG=1
BINS = comm.gb

181
comm.c
View File

@ -18,6 +18,7 @@
enum connection_state_t connection_state = NOT_CONNECTED;
enum trade_state_t trade_state = INIT;
uint32_t emu_var = 0x12345678;
uint8_t INPUT_BLOCK[418];
uint8_t DATA_BLOCK[418];
int trade_pokemon = -1;
@ -27,11 +28,11 @@ unsigned char name[11] = {
pokechar_e,
pokechar_a,
pokechar_d,
pokechar_C,
pokechar_B,
pokechar_o,
pokechar_d,
pokechar_r,
pokechar_e,
pokechar_s,
pokechar_d,
pokechar_STOP_BYTE
};
unsigned char nicknames[11] = {
@ -40,18 +41,18 @@ unsigned char nicknames[11] = {
pokechar_E,
pokechar_W,
pokechar_STOP_BYTE,
pokechar_STOP_BYTE,
pokechar_STOP_BYTE,
pokechar_STOP_BYTE,
pokechar_STOP_BYTE,
pokechar_STOP_BYTE,
pokechar_STOP_BYTE,
pokechar_STOP_BYTE,
pokechar_NULL_BYTE,
pokechar_NULL_BYTE,
pokechar_NULL_BYTE,
pokechar_NULL_BYTE,
pokechar_NULL_BYTE,
pokechar_NULL_BYTE,
pokechar_NULL_BYTE,
};
typedef struct TraderPacket {
// Name must not exceed 10 characters + 1 STOP_BYTE
// Any leftover space must be filled with STOP_BYTE
// Any leftover space after STOP_BYTE must be filled with NULL_BYTE
unsigned char name[11];
struct SelectedPokemon selected_pokemon;
struct PartyMember pokemon[6];
@ -75,9 +76,9 @@ void party_member_to_bytes(struct PartyMember *pPartyMember, uint8_t *out) {
pPartyMember->move4,
(uint8_t) (pPartyMember->original_trainer_id >> 8),
(uint8_t) (pPartyMember->original_trainer_id & 0x00FF),
(uint8_t) ((pPartyMember->experience & 0x00FF0000) >> 16),
(uint8_t) ((pPartyMember->experience & 0x0000FF00) >> 8),
(uint8_t) (pPartyMember->experience & 0x000000FF),
(uint8_t) (pPartyMember->experience & 0x0000FF00) >> 8,
(uint8_t) (pPartyMember->experience & 0x00FF0000) >> 16,
(uint8_t) (pPartyMember->HP_ev >> 8),
(uint8_t) (pPartyMember->HP_ev & 0x00FF),
(uint8_t) (pPartyMember->attack_ev >> 8),
@ -143,6 +144,12 @@ void trader_packet_to_bytes(struct TraderPacket *pTraderPacket, uint8_t *out) {
for (size_t i = 0; i < PARTY_SIZE; i++) {
uint8_t poke[POKE_SIZE];
party_member_to_bytes(&pTraderPacket->pokemon[i], poke);
// Selected Pokemon Data (for listing and ordering without stats)
if (poke[0] != 0x00) {
pTraderPacket->selected_pokemon.number++;
pTraderPacket->selected_pokemon.pokemon[i] = poke[0];
}
// Full Party Data (all stats and such)
for (size_t j = 0; j < POKE_SIZE; j++) {
@ -197,64 +204,65 @@ uint8_t handle_byte(uint8_t in, size_t *counter) {
switch (connection_state)
{
case NOT_CONNECTED:
switch (in)
{
case PKMN_MASTER:
out[0] = PKMN_SLAVE;
break;
case PKMN_BLANK:
out[0] = PKMN_BLANK;
break;
case PKMN_CONNECTED:
connection_state = CONNECTED;
out[0] = PKMN_CONNECTED;
break;
// printf("Not connected...\n");
if(in == PKMN_MASTER) {
out[0] = PKMN_SLAVE;
// printf("We are slave\n");
}
else if(in == PKMN_BLANK) {
out[0] = PKMN_BLANK;
// printf("Pokemon Blank\n");
}
else if(in == PKMN_CONNECTED) {
out[0] = PKMN_CONNECTED;
connection_state = CONNECTED;
// printf("We are connected!\n");
}
break;
case CONNECTED:
switch (in)
{
case PKMN_CONNECTED:
out[0] = PKMN_CONNECTED;
break;
case PKMN_TRADE_CENTRE:
// No byte known, just move on the next case
connection_state = TRADE_CENTRE;
break;
case PKMN_COLOSSEUM:
// No byte known, just move on the next case
// This case is not built out and I have no intention to do it
connection_state = COLOSSEUM;
break;
case PKMN_BREAK_LINK:
case PKMN_MASTER:
connection_state = NOT_CONNECTED;
out[0] = PKMN_BREAK_LINK;
break;
default:
out[0] = in;
break;
// printf("Connected...\n");
if(in == PKMN_CONNECTED) {
out[0] = PKMN_CONNECTED;
// printf("Confirmed connected.\n");
}
else if(in == PKMN_TRADE_CENTRE) {
connection_state = TRADE_CENTRE;
// printf("Trade Center\n");
}
else if(in == PKMN_COLOSSEUM) {
connection_state = COLOSSEUM;
// printf("COLOSSEUM\n");
}
else if(in == PKMN_BREAK_LINK || in == PKMN_MASTER) {
connection_state = NOT_CONNECTED;
out[0] = PKMN_BREAK_LINK;
// printf(in == PKMN_MASTER ? "PKMN_MASTER\n" : "PKMN_BREAK_LINK\n");
} else {
out[0] = in;
// printf("echoing back after connected: %x\n", in);
}
break;
case TRADE_CENTRE:
// printf("Trade Center...\n");
if(trade_state == INIT && in == 0x00) {
trade_state = READY;
out[0] = 0x00;
// printf("Init, now ready\n");
} else if(trade_state == READY && in == 0xFD) {
trade_state = DETECTED;
trade_state = FIRST_DETECTED_WAIT;
out[0] = 0xFD;
} else if(trade_state == DETECTED && in != 0xFD) {
// printf("Ready, begin waiting\n");
} else if(trade_state == FIRST_DETECTED_WAIT && in != 0xFD) {
// random data of slave is ignored.
out[0] = in;
trade_state = DATA_TX_RANDOM;
// printf("Detected, start random data\n");
} else if(trade_state == DATA_TX_RANDOM && in == 0xFD) {
trade_state = DATA_TX_WAIT;
out[0] = 0xFD;
(*counter) = 0;
} else if (trade_state == DATA_TX_WAIT && in == 0xFD) {
out[0] = 0x00;
// printf("Random data sent, wait\n");
} else if(trade_state == DATA_TX_WAIT && in != 0xFD) {
(*counter) = 0;
// send first byte
@ -262,6 +270,7 @@ uint8_t handle_byte(uint8_t in, size_t *counter) {
INPUT_BLOCK[(*counter)] = in;
trade_state = DATA_TX;
(*counter)++;
// printf("Sending data (not 0xFD)\n");
} else if(trade_state == DATA_TX) {
out[0] = DATA_BLOCK[(*counter)];
INPUT_BLOCK[(*counter)] = in;
@ -269,44 +278,57 @@ uint8_t handle_byte(uint8_t in, size_t *counter) {
if((*counter) == 418) {
trade_state = DATA_TX_PATCH;
}
// printf("Sending data\n");
} else if(trade_state == DATA_TX_PATCH && in == 0xFD) {
(*counter) = 0;
out[0] = 0xFD;
// printf("Send patch\n");
} else if(trade_state == DATA_TX_PATCH && in != 0xFD) {
out[0] = in;
(*counter)++;
if((*counter) == 197) {
trade_state = TRADE_WAIT;
}
// printf("Send patch trade wait\n");
} else if(trade_state == TRADE_WAIT && (in & 0x60) == 0x60) {
if (in == 0x6f) {
trade_state = READY;
out[0] = 0x6f;
// printf("Master ready\n");
} else {
out[0] = 0x60;
out[0] = 0x60; // first pokemon
trade_pokemon = in - 0x60;
// printf("Master first pokemon\n");
}
} else if(trade_state == TRADE_WAIT && in == 0x00) {
out[0] = 0;
trade_state = TRADE_DONE;
// printf("Sent the Gameboy:\n");
// printf("Gameboy Sent:\n");
// printf("Trade done\n");
} else if(trade_state == TRADE_DONE && (in & 0x60) == 0x60) {
out[0] = in;
if (in == 0x61) {
trade_pokemon = -1;
trade_state = TRADE_WAIT;
// printf("Trade done, wait\n");
} else {
trade_state = DONE;
// printf("Trade done, mark done\n");
}
} else if(trade_state == DONE && in == 0x00) {
out[0] = 0;
trade_state = INIT;
// printf("Done, reinit\n");
} else {
out[0] = in;
// printf("Echo back\n");
}
break;
default:
out[0] = in;
// printf("Echo back 2\n");
break;
}
@ -348,7 +370,7 @@ void main(void)
pPartyMember->move2 = PSYWAVE;
pPartyMember->move3 = PSYCHIC;
pPartyMember->move4 = FLY;
pPartyMember->original_trainer_id = 0xA455; // In decimal, these are the funny numbers
pPartyMember->original_trainer_id = 0xFFDE;//0xA455; // In decimal, these are the funny numbers
// - Experience is complicated. You must look up the Pokemon you are trying to trade
// in the following table and apply the experience points that match the level.
@ -396,41 +418,34 @@ void main(void)
}
}
size_t trade_counter = 0;
trader_packet_to_bytes(&traderPacket, DATA_BLOCK);
puts("Poke Distribution");
puts("Copyright 2023");
puts("BreadCodes");
size_t trade_counter = 0;
int debug_last_byte = 0x00;
while(1) {
uint8_t in = _io_in;
receive_byte();
while(_io_status == IO_RECEIVING);
uint8_t in = _io_in; //SER_REG_DIR;
_io_out = handle_byte(in, &trade_counter);
_io_out = handle_byte(in, trade_counter);
__asm
LD A,#0x01
LD (__io_status),A ; Store status
LD A,#0x01
LDH (0x02),A ; Use internal clock
LD A,(__io_out)
LDH (0x01),A ; Send data byte
LD A,#0x80
LDH (0x02),A ; Use internal clock
__endasm;
// No-Op loop to delay the bytes being sent.
// We do this because the serial interface halts operations on
// the other Gameboy and freezes the game, so we need to give it
// time to think.
for (int i = 0; i < 500; i++) {
__asm
NOP
__endasm;
}
// trade_byte_primary();
// See https://github.com/gbdk-2020/gbdk-2020/pull/577
__asm
LD A,#0x02 ; .IO_RECEIVING
LD (__io_status),A ; Store status
LD A,#0x01
LDH (0x02),A ; (.SC) Use external clock
LD A,(__io_out)
LDH (0x01),A ; (.SB) Send __io_out byte
LD A,#0x81
LDH (0x02),A ; (.SC) Use external clock
__endasm;
while(_io_status == IO_RECEIVING || _io_status == IO_SENDING);
while(_io_status == IO_SENDING);
while((joypad() > 0)); // Pause output to read the screen
}
}

41
gen1.h
View File

@ -195,19 +195,18 @@ enum connection_state_t {
enum trade_state_t {
INIT = 0x00,
READY = 0x01,
DETECTED = 0x02,
FIRST_DETECTED_WAIT = 0x02,
DATA_TX = 0x03,
DATA_TX_WAIT = 0x04,
DATA_TX_WAIT_HOLD = 0x05,
DATA_TX_START = 0x06,
DATA_TX_RANDOM = 0x07,
DATA_TX_PATCH = 0x08,
TRADE_WAIT = 0x09,
TRADE_DONE = 0x0A,
DONE = 0x0B
DATA_TX_START = 0x05,
DATA_TX_RANDOM = 0x06,
DATA_TX_PATCH = 0x07,
TRADE_WAIT = 0x08,
TRADE_DONE = 0x09,
DONE = 0x0A
};
enum gen_one_dex_t {
enum GenOneDex {
NULL_POKEMON = 0xFF,
RHYDON = 0x01,
KANGASKHAN = 0x02,
@ -362,7 +361,7 @@ enum gen_one_dex_t {
VICTREEBEL = 0xBE,
};
enum poke_move_t {
enum PokemonMove {
ABSORB = 0x47,
ACID = 0x33,
ACID_ARMOR = 0x97,
@ -530,7 +529,7 @@ enum poke_move_t {
WRAP = 0x23,
};
enum status_condition_t {
enum StatusCondition {
NONE = 0x00,
ASLEEP = 0x04,
BURNED = 0x10,
@ -539,7 +538,7 @@ enum status_condition_t {
POISONED = 0x08,
};
enum poke_type_t {
enum PokemonType {
NORMAL = 0x00,
FIGHTING = 0x01,
FLYING = 0x02,
@ -560,25 +559,25 @@ enum poke_type_t {
typedef struct SelectedPokemon {
// Number of pokemon in the trainer's party
uint8_t number;
enum gen_one_dex_t pokemon[6];
enum GenOneDex pokemon[6];
} SelectedPokemon;
// Luckily it follows the save file structure closely enough
// https://bulbapedia.bulbagarden.net/wiki/Pok%C3%A9mon_data_structure_(Generation_I)
typedef struct PartyMember {
enum gen_one_dex_t pokemon;
enum GenOneDex pokemon;
uint16_t current_hp;
uint16_t max_hp;
uint8_t level;
enum status_condition_t status;
enum poke_type_t type1;
enum poke_type_t type2; // If only one type, copy the first
enum StatusCondition status;
enum PokemonType type1;
enum PokemonType type2; // If only one type, copy the first
uint8_t catch_rate_or_held_item; // R/G/B/Y (catch rate), G/S/C (held item), and Stadium (held item) use this byte differently
enum poke_move_t move1;
enum poke_move_t move2;
enum poke_move_t move3;
enum poke_move_t move4;
enum PokemonMove move1;
enum PokemonMove move2;
enum PokemonMove move3;
enum PokemonMove move4;
uint16_t original_trainer_id; // In decimal, these are the funny numbers
// - Experience is complicated. You must look up the Pokemon you are trying to trade