This commit is contained in:
Alex Janka 2023-03-03 09:36:04 +11:00
parent 105fca6624
commit d8771a27a3

View file

@ -21,7 +21,7 @@ const NUM_WRITES_TO_FLUSH: usize = 256;
impl MaybeBufferedSram { impl MaybeBufferedSram {
fn new(path: Option<PathBuf>, length: usize) -> Self { fn new(path: Option<PathBuf>, length: usize) -> Self {
let mut buf = vec![0; 8 * mbcs::KB]; let mut buf = vec![];
let inner = if let Some(path) = path { let inner = if let Some(path) = path {
if path.exists() { if path.exists() {
let mut writer = OpenOptions::new() let mut writer = OpenOptions::new()
@ -33,6 +33,7 @@ impl MaybeBufferedSram {
writer.read_to_end(&mut buf).unwrap(); writer.read_to_end(&mut buf).unwrap();
Some(writer) Some(writer)
} else { } else {
buf.resize(8 * mbcs::KB, 0);
let writer = OpenOptions::new() let writer = OpenOptions::new()
.write(true) .write(true)
.create_new(true) .create_new(true)
@ -66,9 +67,8 @@ impl MaybeBufferedSram {
fn set(&mut self, addr: usize, data: u8) { fn set(&mut self, addr: usize, data: u8) {
self.unbuffered_writes += 1; self.unbuffered_writes += 1;
if addr >= self.buf.len() { while addr >= self.buf.len() {
self.buf self.buf.resize(self.buf.len() + (8 * mbcs::KB), 0);
.resize((addr as f64 / (mbcs::KB as f64 * 8.)).ceil() as usize, 0);
} }
self.buf[addr] = data; self.buf[addr] = data;
if self.unbuffered_writes >= NUM_WRITES_TO_FLUSH { if self.unbuffered_writes >= NUM_WRITES_TO_FLUSH {
@ -79,6 +79,7 @@ impl MaybeBufferedSram {
fn flush(&mut self) { fn flush(&mut self) {
if let Some(ref mut writer) = self.inner { if let Some(ref mut writer) = self.inner {
writer.seek(SeekFrom::Start(0)).unwrap(); writer.seek(SeekFrom::Start(0)).unwrap();
writer.set_len(self.buf.len() as u64).unwrap();
writer.write_all(&self.buf).unwrap(); writer.write_all(&self.buf).unwrap();
} }
} }