From d8771a27a33b4d3dea017b54cebea37d2801b8b1 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Fri, 3 Mar 2023 09:36:04 +1100 Subject: [PATCH] ?? --- src/processor/memory/rom.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/processor/memory/rom.rs b/src/processor/memory/rom.rs index 841bf44..2af2dd1 100644 --- a/src/processor/memory/rom.rs +++ b/src/processor/memory/rom.rs @@ -21,7 +21,7 @@ const NUM_WRITES_TO_FLUSH: usize = 256; impl MaybeBufferedSram { fn new(path: Option, length: usize) -> Self { - let mut buf = vec![0; 8 * mbcs::KB]; + let mut buf = vec![]; let inner = if let Some(path) = path { if path.exists() { let mut writer = OpenOptions::new() @@ -33,6 +33,7 @@ impl MaybeBufferedSram { writer.read_to_end(&mut buf).unwrap(); Some(writer) } else { + buf.resize(8 * mbcs::KB, 0); let writer = OpenOptions::new() .write(true) .create_new(true) @@ -66,9 +67,8 @@ impl MaybeBufferedSram { fn set(&mut self, addr: usize, data: u8) { self.unbuffered_writes += 1; - if addr >= self.buf.len() { - self.buf - .resize((addr as f64 / (mbcs::KB as f64 * 8.)).ceil() as usize, 0); + while addr >= self.buf.len() { + self.buf.resize(self.buf.len() + (8 * mbcs::KB), 0); } self.buf[addr] = data; if self.unbuffered_writes >= NUM_WRITES_TO_FLUSH { @@ -79,6 +79,7 @@ impl MaybeBufferedSram { fn flush(&mut self) { if let Some(ref mut writer) = self.inner { writer.seek(SeekFrom::Start(0)).unwrap(); + writer.set_len(self.buf.len() as u64).unwrap(); writer.write_all(&self.buf).unwrap(); } }