From 304165e407faec47f18e7ab9f98ddc1300e74710 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 22 Jan 2023 14:37:09 +0100 Subject: [PATCH] Encode word size into OverlayLBA structure --- include/PSX/Overlay/overlay.hpp | 4 +-- .../psxcdgen_ex/src/types/overlay/mod.rs | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/include/PSX/Overlay/overlay.hpp b/include/PSX/Overlay/overlay.hpp index 8e3f6356..1903782a 100644 --- a/include/PSX/Overlay/overlay.hpp +++ b/include/PSX/Overlay/overlay.hpp @@ -5,13 +5,13 @@ namespace JabyEngine { struct __attribute__((packed)) OverlayHeader { void (*execute)(); - uint16_t lba_size; + uint16_t lba_size; //< The size of the OverlayLBA section }; // Maybe encode attributes like "isLZ4" into size parameter struct __attribute__((packed)) OverlayLBA { uint16_t lba; - uint16_t size; + uint16_t size_words; }; } #endif //!__JABYENGINE_OVERLAY__HPP__ \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs index c1d46a23..8a77014b 100644 --- a/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs @@ -20,17 +20,27 @@ impl OverlayHeader { #[repr(packed)] struct LBAEntry { - lba: u16, - sectors: u16, + lba: u16, + size_words: u16, } impl LBAEntry { - pub fn write_entry(&mut self, lba: u16, sectors: u16) { - let lba = lba.to_le_bytes(); - let sectors = sectors.to_le_bytes(); + pub fn write_entry(&mut self, lba: u16, mut size_bytes: usize) -> Result<(), Error> { + const WORD_SIZE:usize = std::mem::size_of::(); - self.lba = u16::from_ne_bytes(lba); - self.sectors = u16::from_ne_bytes(sectors); + size_bytes = (size_bytes + (WORD_SIZE - 1))/WORD_SIZE; + + if (size_bytes as u16) as usize != size_bytes { + return Err(Error::from_text(format!("{} words can not be incoded into 16bit", size_bytes))); + } + + let lba = lba.to_le_bytes(); + let size_bytes = (size_bytes as u16).to_le_bytes(); + + self.lba = u16::from_ne_bytes(lba); + self.size_words = u16::from_ne_bytes(size_bytes); + + Ok(()) } } @@ -49,13 +59,13 @@ pub fn update_content(content: &mut Vec, lba_names: &LBANameVec, file_map: & let mut idx = 0; for lba_name in lba_names { if let Some(file) = file_map.get(lba_name) { - let (lba, sector_count) = (file.borrow().get_track_rel_lba(), length_func(&Layout::File(file.clone())).sectors); + let (lba, bytes) = (file.borrow().get_track_rel_lba(), length_func(&Layout::File(file.clone())).bytes); if idx >= lba_count { return Err(Error::from_text(format!("Trying to write more LBAs then there is space!"))); } - lba_header[idx].write_entry(lba as u16, sector_count as u16); + lba_header[idx].write_entry(lba as u16, bytes.ok_or(Error::from_text(format!("{} does not contain a size!", lba_name)))?)?; idx += 1; }