mirror of
https://github.com/GearsProgress/Poke_Transporter_GB.git
synced 2026-04-26 02:05:09 -05:00
There was a crash happening with ptgb::vector when you'd press A on the CONFIRM button of the box screen. It only occurred on actual gba hardware and was a real heisenbug: as soon as you'd add code to display logs on screen, the problem would disappear. So it was very difficult to figure this one out. We're not even entirely sure why, but it looks like the malloc/realloc/free use in ptgb::vector would cause issues. Maybe it was alignment, but after messing with the code we also saw a warning appear in the terminal telling us that realloc wouldn't properly deal with non-POD types. It complained about this very thing while referring to the add_track() function, which stores ptgb::vectors inside another ptgb::vector. We also didn't have a custom copy constructor yet to actually copy the buffer instead of its pointer. All of these could potentially have led to the crash. But debugging during the link cable flow was difficult, so we were never able to confirm it in a debugger, log or dump. Because I suspected the high IWRAM consumption (especially now with ZX0 decompression) for a while, I also did an optimization in mystery_gift_builder to pass global_memory_buffer as its section_30_data buffer instead. This reduces IWRAM consumption by 4 KB. There was another problem I discovered during my crash hunt: the out_array (now payload_buffer) was allocated as a 672 byte array, but the payloads were actually 707 bytes. Therefore writing this to the buffer caused a buffer overflow, thereby corrupting the global variables appearing after it in IWRAM. It turned out eventually that none of these variables were really critical, but it could explain some minor bugs GearsProgress has seen. I also did a few performance optimizations: - At various stages in the code, for loops were used to copy data from one buffer into another byte-by-byte. This was far from optimal because the gba cpu can load/copy 4 bytes at a time if you ask it to. So I replaced those with memcpy(), which is a hand-optimized assembly function to copy data using this principle. - generate_payload was being called twice: once at start_link and once at continue_link, giving the exact same result, even though it was already being stored in a global buffer allocated in IWRAM. This was also a fairly heavy function. So I optimized the code to only initialize it once in the script chain and then just retrieve the buffer. - generate_payload was constructing the eventual payload twice even within the same call. That's because it first merged z80_rng_seed, z80_payload and z80_patchlist into a full_data ptgb::vector, after which it then copied the data again to out_array (now called payload_buffer). I eliminated the full_data vector now.
150 lines
4.1 KiB
C++
150 lines
4.1 KiB
C++
#ifndef Z80_ASM_H
|
|
#define Z80_ASM_H
|
|
|
|
#include <stdarg.h>
|
|
#include "libstd_replacements.h"
|
|
|
|
/*
|
|
All registers are above 16 to not confuse them with u8 or u16
|
|
u8 registers have a 0x0, while u16 have 0x1
|
|
u16 values pointing to a byte have 0x2, sans HL_PTR
|
|
*/
|
|
|
|
#define T_U8 0x00000000
|
|
#define T_I8 0x01000000
|
|
#define T_U16 0x02000000
|
|
#define T_8BIT_REG 0x03000000
|
|
#define T_16BIT_REG 0x04000000
|
|
#define T_16BIT_PTR 0x05000000
|
|
#define T_BIT 0x06000000
|
|
#define T_FLAG 0x07000000
|
|
|
|
#define B (0x00 | T_8BIT_REG)
|
|
#define C (0x01 | T_8BIT_REG)
|
|
#define D (0x02 | T_8BIT_REG)
|
|
#define E (0x03 | T_8BIT_REG)
|
|
#define H (0x04 | T_8BIT_REG)
|
|
#define L (0x05 | T_8BIT_REG)
|
|
#define HL_PTR (0x06 | T_8BIT_REG)
|
|
#define A (0x07 | T_8BIT_REG)
|
|
|
|
#define BC (0x00 | T_16BIT_REG)
|
|
#define DE (0x01 | T_16BIT_REG)
|
|
#define HL (0x02 | T_16BIT_REG)
|
|
#define SP (0x03 | T_16BIT_REG)
|
|
#define AF (0x03 | T_16BIT_REG) // AF is only used instead of SP in the PUSH commands
|
|
|
|
#define BC_PTR (0x00 | T_16BIT_PTR)
|
|
#define DE_PTR (0x01 | T_16BIT_PTR)
|
|
#define HLI_PTR (0x02 | T_16BIT_PTR)
|
|
#define HLD_PTR (0x03 | T_16BIT_PTR)
|
|
|
|
#define NZ_F (0x00 | T_FLAG)
|
|
#define Z_F (0x01 | T_FLAG)
|
|
#define NC_F (0x02 | T_FLAG)
|
|
#define C_F (0x03 | T_FLAG)
|
|
|
|
#define TYPE(a) (a & 0xFF000000)
|
|
|
|
typedef unsigned char u8; // Issues with including Tonc again with the test payload script
|
|
typedef unsigned char byte; // Issues with including Tonc again with the test payload script
|
|
typedef unsigned short u16; // Issues with including Tonc again with the test payload script
|
|
|
|
class z80_asm_handler
|
|
{
|
|
public:
|
|
int index;
|
|
int memory_offset;
|
|
ptgb::vector<byte> data_vector;
|
|
|
|
z80_asm_handler(int data_size, int mem_offset);
|
|
void add_byte(u8 value);
|
|
void add_bytes(int num_bytes, ...);
|
|
void add_bytes(const u8 *data, u16 data_size);
|
|
void generate_patchlist(z80_asm_handler *bytes_to_patch);
|
|
void LD(int destination, int source);
|
|
void HALT();
|
|
void ADD(int destination, int source);
|
|
void ADC(int destination, int source);
|
|
void SUB(int destination, int source);
|
|
void SBC(int destination, int source);
|
|
void AND(int destination, int source);
|
|
void XOR(int destination, int source);
|
|
void OR(int destination, int source);
|
|
void CP(int destination, int source);
|
|
void NOP();
|
|
void STOP();
|
|
void INC(int reg);
|
|
void DEC(int reg);
|
|
void RLC(int reg);
|
|
void RRC(int reg);
|
|
void RL(int reg);
|
|
void RR(int reg);
|
|
void JR(int distance);
|
|
void JR(int flag, int distance);
|
|
void DDA();
|
|
void CPL();
|
|
void SCF();
|
|
void CCF();
|
|
void RET();
|
|
void RET(int flag);
|
|
void RETI();
|
|
void PUSH(int source);
|
|
void POP(int destination);
|
|
void JP(int destination);
|
|
void JP(int flag, int destination);
|
|
void CALL(int destination);
|
|
void CALL(int flag, int destination);
|
|
void RST(int value);
|
|
void LDH(int source, int destination);
|
|
void DI();
|
|
void EI();
|
|
void LDHL(int offset);
|
|
void SLA(int reg);
|
|
void SRA(int reg);
|
|
void SWAP(int reg);
|
|
void SRL(int reg);
|
|
void BIT(int bit, int reg);
|
|
void RES(int bit, int reg);
|
|
void SET(int bit, int reg);
|
|
|
|
private:
|
|
void ROT(int reg, int info);
|
|
};
|
|
|
|
class z80_variable
|
|
{
|
|
public:
|
|
ptgb::vector<byte> data;
|
|
int size;
|
|
z80_variable(ptgb::vector<z80_variable*> *var_vec, int data_size, ...);
|
|
z80_variable(ptgb::vector<z80_variable*> *var_vec);
|
|
void load_data(int data_size, byte array_data[]);
|
|
int place_ptr(z80_asm_handler *z80_instance);
|
|
void insert_variable(z80_asm_handler *var);
|
|
void update_ptrs();
|
|
|
|
private:
|
|
ptgb::vector<int> ptr_locations;
|
|
ptgb::vector<z80_asm_handler *> asm_handlers;
|
|
int var_mem_location;
|
|
};
|
|
|
|
class z80_jump
|
|
{
|
|
public:
|
|
z80_jump(ptgb::vector<z80_jump*> *jump_vec);
|
|
int place_relative_jump(z80_asm_handler *z80_instance);
|
|
int place_direct_jump(z80_asm_handler *z80_instance);
|
|
int place_pointer(z80_asm_handler *z80_instance);
|
|
void set_start(z80_asm_handler *var);
|
|
void update_jumps();
|
|
|
|
private:
|
|
ptgb::vector<int> ptr_locations;
|
|
ptgb::vector<z80_asm_handler *> asm_handlers;
|
|
ptgb::vector<bool> jump_types;
|
|
int jump_mem_location;
|
|
};
|
|
|
|
#endif |