cgb palette

This commit is contained in:
Alex Janka 2023-03-03 17:46:57 +11:00
parent 3a4656076f
commit 953964ff34

View file

@ -107,7 +107,15 @@ impl Rom {
} }
} }
let title = unsafe { from_utf8_unchecked(&data[0x134..title_length]).to_string() }; let title = unsafe { from_utf8_unchecked(&data[0x134..title_length]).to_string() };
let _gbc_flag = data[0x143]; let _gbc_flag = data[0x143];
// if _gbc_flag & 0b10000000 == 0 {
// get_cgb_compat_palette(&data);
// } else {
// println!("CGB game");
// }
let _sgb_flag = data[0x146]; let _sgb_flag = data[0x146];
let rom_size = data[0x148]; let rom_size = data[0x148];
let ram_size = data[0x149]; let ram_size = data[0x149];
@ -170,3 +178,44 @@ impl Rom {
self.mbc.flush(); self.mbc.flush();
} }
} }
const CHECKSUM_TABLE: [u8; 79] = [
0x00, 0x88, 0x16, 0x36, 0xD1, 0xDB, 0xF2, 0x3C, 0x8C, 0x92, 0x3D, 0x5C, 0x58, 0xC9, 0x3E, 0x70,
0x1D, 0x59, 0x69, 0x19, 0x35, 0xA8, 0x14, 0xAA, 0x75, 0x95, 0x99, 0x34, 0x6F, 0x15, 0xFF, 0x97,
0x4B, 0x90, 0x17, 0x10, 0x39, 0xF7, 0xF6, 0xA2, 0x49, 0x4E, 0x43, 0x68, 0xE0, 0x8B, 0xF0, 0xCE,
0x0C, 0x29, 0xE8, 0xB7, 0x86, 0x9A, 0x52, 0x01, 0x9D, 0x71, 0x9C, 0xBD, 0x5D, 0x6D, 0x67, 0x3F,
0x6B, 0xB3, 0x46, 0x28, 0xA5, 0xC6, 0xD3, 0x27, 0x61, 0x18, 0x66, 0x6A, 0xBF, 0x0D, 0xF4,
];
const TIEBREAKER_TABLE: [u8; 29] = [
0x42, 0x45, 0x46, 0x41, 0x41, 0x52, 0x42, 0x45, 0x4B, 0x45, 0x4B, 0x20, 0x52, 0x2D, 0x55, 0x52,
0x41, 0x52, 0x20, 0x49, 0x4E, 0x41, 0x49, 0x4C, 0x49, 0x43, 0x45, 0x20, 0x52,
];
#[allow(dead_code)]
fn get_cgb_compat_palette(data: &[u8]) {
if data[0x14B] == 0x01 || (data[0x14B] == 0x33 && data[0x144] == 0x30 && data[0x145] == 0x31) {
let checksum = data
.iter()
.take(0x143)
.skip(0x134)
.fold(0_u8, |acc, val| acc.wrapping_add(*val));
let index = CHECKSUM_TABLE
.iter()
.position(|v| *v == checksum)
.unwrap_or(0);
if index <= 64 {
println!("checksum: {checksum:#X}, index: {index:#X}");
} else {
let fourth = data[0x137];
let tiebreaker = TIEBREAKER_TABLE
.iter()
.position(|v| *v == fourth)
.unwrap_or(0);
println!("checksum: {checksum:#X}, index: {index:#X}, fourth: {fourth:#X}, tiebreaker: {tiebreaker:#X}");
}
} else {
// zero
}
}