aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mssola@mssola.com>2026-09-07 23:41:48 +0200
committerMiquel Sabaté Solà <mssola@mssola.com>2026-09-07 23:41:48 +0200
commit6000556d5005b5cd5faf72cb290e1d9a72beee35 (patch)
treefd7ac9aceacb88fe96673f9a07f782a5084a5c33
parente2a20dedc354c0b8d2e5253ffd4ac22d2b2898aa (diff)
downloadtools.nes-6000556d5005b5cd5faf72cb290e1d9a72beee35.tar.gz
tools.nes-6000556d5005b5cd5faf72cb290e1d9a72beee35.zip
vnf: validate the minimum stack value
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
-rw-r--r--lib/vnf/src/lib.rs15
1 files changed, 10 insertions, 5 deletions
diff --git a/lib/vnf/src/lib.rs b/lib/vnf/src/lib.rs
index 534fc1c..729bd58 100644
--- a/lib/vnf/src/lib.rs
+++ b/lib/vnf/src/lib.rs
@@ -384,9 +384,14 @@ pub struct Machine {
pub halt_on_brk: bool,
}
-// Returns a vector of MemoryCell representing the RAM for a Machine, which
-// follows the memory policy as defined in 'policy'.
-fn init_memory(policy: &MemoryPolicy) -> Vec<MemoryCell> {
+// On success, returns a vector of MemoryCell representing the RAM for a
+// Machine, which follows the memory policy as defined in 'policy'. Otherwie it
+// returns a string error.
+fn init_memory(policy: &MemoryPolicy) -> Result<Vec<MemoryCell>, String> {
+ if policy.minimum_stack_value > 0xFD {
+ return Err("minimum stack value is too high".to_string());
+ }
+
let mut vec = Vec::with_capacity(0x800);
for i in 0..0x800 {
@@ -410,7 +415,7 @@ fn init_memory(policy: &MemoryPolicy) -> Vec<MemoryCell> {
vec[0x2FF].value = 0x00;
vec[0x2FE].value = 0x00;
- vec
+ Ok(vec)
}
/// For a given u16 expression, return a tuple formatted like so:
@@ -482,7 +487,7 @@ impl Machine {
y: 0,
s: 0xFD, // NOTE: as per 6502 initialization process.
initial_stack_value: 0xFD,
- ram: init_memory(&policy),
+ ram: init_memory(&policy)?,
status_register: StatusRegister::default(),
apu: APU::default(),
ppu: PPU::default(),