From a8ff66c3a02e2121ca3c3900ba02704f8ec156eb Mon Sep 17 00:00:00 2001 From: Lua MacDougall Date: Sat, 16 Apr 2022 23:30:54 -0700 Subject: [PATCH] fox32: fix stack overflow when initializing memory --- src/memory.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/memory.rs b/src/memory.rs index f645f94..80f624c 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -23,8 +23,10 @@ struct MemoryInner { impl MemoryInner { pub fn new(rom: &[u8]) -> Self { let mut this = Self { - ram: Box::new([0u8; MEMORY_RAM_SIZE]), - rom: Box::new([0u8; MEMORY_ROM_SIZE]), + // HACK: allocate directly on the heap to avoid a stack overflow + // at runtime while trying to move around a 64MB array + ram: unsafe { Box::from_raw(Box::into_raw(vec![0u8; MEMORY_RAM_SIZE].into_boxed_slice()) as *mut MemoryRam) }, + rom: unsafe { Box::from_raw(Box::into_raw(vec![0u8; MEMORY_ROM_SIZE].into_boxed_slice()) as *mut MemoryRom) }, }; this.rom.as_mut_slice().write(rom).expect("failed to copy ROM to memory"); this @@ -107,4 +109,4 @@ impl Memory { self.write_8(address + 2, ((word & 0x00FF0000) >> 16) as u8); self.write_8(address + 3, ((word & 0xFF000000) >> 24) as u8); } -} \ No newline at end of file +}