diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-08-28 08:39:08 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-08-28 09:00:56 +0200 |
| commit | b93267c56565582b9a85e7a1255568e16abcafd2 (patch) | |
| tree | c987acc61a8f83694be425df440e472068f99582 /crates/nasm/src/main.rs | |
| parent | 165cbcf2f92ea0aa992e96d5feb8aacd9752ac47 (diff) | |
| download | tools.nes-b93267c56565582b9a85e7a1255568e16abcafd2.tar.gz tools.nes-b93267c56565582b9a85e7a1255568e16abcafd2.zip | |
nasm: Error out when using WRAM when not available
This commit introduces the ability to inspect the temptative header
before producing the actual output, and with that it checks whether the
Working RAM is being advertised or not. If it is not being advertised
but the assembler detected memory accesses to that region, then we are
in trouble and we should error out.
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'crates/nasm/src/main.rs')
| -rw-r--r-- | crates/nasm/src/main.rs | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/crates/nasm/src/main.rs b/crates/nasm/src/main.rs index 3864587..6ca6d27 100644 --- a/crates/nasm/src/main.rs +++ b/crates/nasm/src/main.rs @@ -1,3 +1,4 @@ +use header::Header; use std::fs::File; use std::io::{self, Write}; use std::path::Path; @@ -205,8 +206,36 @@ fn main() { error_count += 1; } - // If everything was right, just deliver the bundles. + // Deliver the bundles unless the header is borked. if error_count == 0 { + // Fetch the header first. + let mut temptative_header = vec![]; + for b in &res.bundles { + for i in 0..b.size { + temptative_header.push(b.bytes[i as usize]); + } + if temptative_header.len() >= 0x10 { + break; + } + } + + // Validate the resulting header. + match Header::try_from(temptative_header.as_slice()) { + Ok(header) => { + if res.accessing_working_ram && !header.has_persistent_memory { + die( + "requires Working RAM but the ROM header does not advertise it".to_string(), + ); + } + } + Err(e) => { + die(format!( + "output would produce a malformed NES/Famicom ROM: {e}" + )); + } + }; + + // And now deliver the bundles. for b in res.bundles { for i in 0..b.size { if let Err(e) = output.write_all(&[b.bytes[i as usize]]) { |
