mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 16:21:33 +11:00
Do the padding correctly
This commit is contained in:
parent
aad5aaf26d
commit
1c56de287a
|
@ -16,7 +16,7 @@ fn main() -> Result<()> {
|
||||||
.arg(arg!(-c --gamecode <GAME_CODE> "Sets the game code, 4 bytes"))
|
.arg(arg!(-c --gamecode <GAME_CODE> "Sets the game code, 4 bytes"))
|
||||||
.arg(arg!(-m --makercode <MAKER_CODE> "Set the maker code, 2 bytes"))
|
.arg(arg!(-m --makercode <MAKER_CODE> "Set the maker code, 2 bytes"))
|
||||||
.arg(arg!(-r --gameversion <VERSION> "Set the version of the game, 0-255").value_parser(value_parser!(u8)))
|
.arg(arg!(-r --gameversion <VERSION> "Set the version of the game, 0-255").value_parser(value_parser!(u8)))
|
||||||
.arg(arg!(-p "Ignored for compatibility with gbafix"))
|
.arg(arg!(-p --padding "Ignored for compatibility with gbafix"))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let input = matches.get_one::<PathBuf>("INPUT").unwrap();
|
let input = matches.get_one::<PathBuf>("INPUT").unwrap();
|
||||||
|
@ -89,9 +89,7 @@ fn write_gba_file<W: Write>(
|
||||||
.section_headers()
|
.section_headers()
|
||||||
.ok_or_else(|| anyhow!("Failed to parse as elf file"))?;
|
.ok_or_else(|| anyhow!("Failed to parse as elf file"))?;
|
||||||
|
|
||||||
const GBA_START_ADDRESS: u64 = 0x8000000;
|
let mut bytes_written = 0;
|
||||||
let mut address = GBA_START_ADDRESS;
|
|
||||||
|
|
||||||
for section_header in section_headers.iter() {
|
for section_header in section_headers.iter() {
|
||||||
const SHT_NOBITS: u32 = 8;
|
const SHT_NOBITS: u32 = 8;
|
||||||
const SHT_NULL: u32 = 0;
|
const SHT_NULL: u32 = 0;
|
||||||
|
@ -103,12 +101,12 @@ fn write_gba_file<W: Write>(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if address < section_header.sh_addr {
|
let align = bytes_written % section_header.sh_addralign;
|
||||||
for _ in address..section_header.sh_addr {
|
if align != 0 {
|
||||||
|
for _ in 0..(section_header.sh_addralign - align) {
|
||||||
output.write_all(&[0])?;
|
output.write_all(&[0])?;
|
||||||
|
bytes_written += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
address = section_header.sh_addr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let (mut data, compression) = elf_file.section_data(§ion_header)?;
|
let (mut data, compression) = elf_file.section_data(§ion_header)?;
|
||||||
|
@ -116,7 +114,7 @@ fn write_gba_file<W: Write>(
|
||||||
bail!("Cannot decompress elf content, but got compression header {compression:?}");
|
bail!("Cannot decompress elf content, but got compression header {compression:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
if address == GBA_START_ADDRESS {
|
if bytes_written == 0 {
|
||||||
const GBA_HEADER_SIZE: usize = 192;
|
const GBA_HEADER_SIZE: usize = 192;
|
||||||
|
|
||||||
ensure!(
|
ensure!(
|
||||||
|
@ -131,17 +129,15 @@ fn write_gba_file<W: Write>(
|
||||||
output.write_all(header_bytes)?;
|
output.write_all(header_bytes)?;
|
||||||
|
|
||||||
data = &data[GBA_HEADER_SIZE..];
|
data = &data[GBA_HEADER_SIZE..];
|
||||||
address += GBA_HEADER_SIZE as u64;
|
bytes_written += GBA_HEADER_SIZE as u64;
|
||||||
}
|
}
|
||||||
|
|
||||||
output.write_all(data)?;
|
output.write_all(data)?;
|
||||||
address += data.len() as u64;
|
bytes_written += data.len() as u64;
|
||||||
}
|
}
|
||||||
|
|
||||||
let length = address - GBA_START_ADDRESS;
|
if !bytes_written.is_power_of_two() {
|
||||||
|
let required_padding = bytes_written.next_power_of_two() - bytes_written;
|
||||||
if !length.is_power_of_two() {
|
|
||||||
let required_padding = length.next_power_of_two() - length;
|
|
||||||
|
|
||||||
for _ in 0..required_padding {
|
for _ in 0..required_padding {
|
||||||
output.write_all(&[0])?;
|
output.write_all(&[0])?;
|
||||||
|
|
Loading…
Reference in a new issue