N64.ino: Optimise controller CRC functions for speed

Also, shave off 5 bytes from a local initializer.
Saves 22 bytes of program space and of global ram space.
This commit is contained in:
Vincent Pelletier 2022-10-22 05:22:59 +00:00
parent 3826f5aa10
commit a43b2553cd

View File

@ -597,35 +597,25 @@ void writeWord_N64(word myWord) {
N64 Controller CRC Functions N64 Controller CRC Functions
*****************************************/ *****************************************/
static word addrCRC(word address) { static word addrCRC(word address) {
// CRC table const char n64_address_crc_table[] = { 0x15, 0x1F, 0x0B, 0x16, 0x19, 0x07, 0x0E, 0x1C, 0x0D, 0x1A, 0x01 };
word xor_table[16] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x15, 0x1F, 0x0B, 0x16, 0x19, 0x07, 0x0E, 0x1C, 0x0D, 0x1A, 0x01 }; const char *cur_xor = n64_address_crc_table;
word crc = 0; byte crc = 0;
// Make sure we have a valid address for (word mask = 0x0020; mask; mask <<= 1, cur_xor++) {
address &= ~0x1F; if (address & mask) {
// Go through each bit in the address, and if set, xor the right value into the output crc ^= *cur_xor;
for (int i = 15; i >= 5; i--) {
// Is this bit set?
if (((address >> i) & 0x1)) {
crc ^= xor_table[i];
} }
} }
// Just in case return (address & 0xFFE0) | crc;
crc &= 0x1F;
// Create a new address with the CRC appended
return address | crc;
} }
static uint8_t dataCRC(uint8_t* data) { static uint8_t dataCRC(uint8_t* data) {
uint8_t ret = 0; uint8_t ret = 0;
for (int i = 0; i <= 32; i++) { for (uint8_t i = 0; i <= 32; i++) {
for (int j = 7; j >= 0; j--) { for (uint8_t mask = 0x80; mask; mask >>= 1) {
int tmp = 0; uint8_t tmp = ret & 0x80 ? 0x85 : 0;
if (ret & 0x80) {
tmp = 0x85;
}
ret <<= 1; ret <<= 1;
if (i < 32) { if (i < 32) {
if (data[i] & (0x01 << j)) { if (data[i] & mask) {
ret |= 0x1; ret |= 0x1;
} }
} }