FlightControl-User / x16-flash

Contains the source code of the commander x16 rom flashing utility
GNU General Public License v3.0
4 stars 2 forks source link

Improve performance of the flashing ... #4

Closed FlightControl-User closed 1 year ago

FlightControl-User commented 1 year ago

For every byte locked, unlocked, flashed, avoid having to do bit shifts (which take time). So instead of the program working with absolute rom addresses, convert the logic to make the program use rom bank variables and then rom 14 bit addresses and adjust the bank with limit checks (like the ram does).

So either improve or get rid of the usage of these functions below:

/**
 * @brief Calculates the 22 bit ROM size from the 8 bit ROM banks.
 * The ROM size is calcuated by taking the 8 bits and shifing those 14 bits to the left (bit 21-14).
 *
 * @param rom_bank The 8 bit ROM banks.
 * @return unsigned long The resulting 22 bit ROM address.
 */
unsigned long rom_size(unsigned char rom_banks) { return ((unsigned long)(rom_banks)) << 14; }

/**
 * @brief Calculates the 22 bit ROM address from the 8 bit ROM bank.
 * The ROM bank number is calcuated by taking the 8 bits and shifing those 14 bits to the left (bit 21-14).
 *
 * @param rom_bank The 8 bit ROM address.
 * @return unsigned long The 22 bit ROM address.
 */
inline unsigned long rom_address(unsigned char rom_bank) { return ((unsigned long)(rom_bank)) << 14; }

/**
 * @brief Calculates the 8 bit ROM bank from the 22 bit ROM address.
 * The ROM bank number is calcuated by taking the upper 8 bits (bit 18-14) and shifing those 14 bits to the right.
 *
 * @param address The 22 bit ROM address.
 * @return unsigned char The ROM bank number for usage in ZP $01.
 */
inline unsigned char rom_bank(unsigned long address) { return (char)((unsigned long)(address & ROM_BANK_MASK) >> 14); }

/**
 * @brief Calcuates the 16 bit ROM pointer from the ROM using the 22 bit address.
 * The 16 bit ROM pointer is calculated by masking the lower 14 bits (bit 13-0), and then adding $C000 to it.
 * The 16 bit ROM pointer is returned as a char* (brom_ptr_t).
 * @param address The 22 bit ROM address.
 * @return brom_ptr_t The 16 bit ROM pointer for the main CPU addressing.
 */
inline brom_ptr_t rom_ptr(unsigned long address) { return (brom_ptr_t)((unsigned int)(address & ROM_PTR_MASK) + ROM_BASE); }
FlightControl-User commented 1 year ago

Improved the flashing performance by avoiding long lasting bit shifts of 14 positions to calculate the bank for each rom byte to be flashed. The bank is now calculated through shifting byte 1 and 2 of the 22 bit rom address, shifting this 2 bits to the left, and then taking the byte 2 which contains the bank... (Note that we count the bytes from zero and little endian).