chunk save files by 8kb

This commit is contained in:
Alex Janka 2023-03-02 20:07:48 +11:00
parent 717bba9352
commit 7a8076bc8f
2 changed files with 16 additions and 6 deletions

View file

@ -12,6 +12,7 @@ mod mbcs;
struct MaybeBufferedSram { struct MaybeBufferedSram {
buf: Vec<u8>, buf: Vec<u8>,
length: usize,
inner: Option<File>, inner: Option<File>,
unbuffered_writes: usize, unbuffered_writes: usize,
} }
@ -20,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; length]; let mut buf = vec![0; 8 * mbcs::KB];
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()
@ -28,7 +29,8 @@ impl MaybeBufferedSram {
.read(true) .read(true)
.open(path) .open(path)
.unwrap(); .unwrap();
writer.read_exact(&mut buf).unwrap(); // writer.read_exact(&mut buf).unwrap();
writer.read_to_end(&mut buf).unwrap();
Some(writer) Some(writer)
} else { } else {
let writer = OpenOptions::new() let writer = OpenOptions::new()
@ -36,7 +38,6 @@ impl MaybeBufferedSram {
.create_new(true) .create_new(true)
.open(path) .open(path)
.unwrap(); .unwrap();
writer.set_len(length as u64).unwrap();
Some(writer) Some(writer)
} }
} else { } else {
@ -45,21 +46,30 @@ impl MaybeBufferedSram {
Self { Self {
buf, buf,
length,
inner, inner,
unbuffered_writes: 0, unbuffered_writes: 0,
} }
} }
fn len(&self) -> usize { fn len(&self) -> usize {
self.buf.len() self.length
} }
fn get(&self, addr: usize) -> u8 { fn get(&self, addr: usize) -> u8 {
self.buf[addr] if addr >= self.buf.len() {
0
} else {
self.buf[addr]
}
} }
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() {
self.buf
.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 {
self.flush(); self.flush();

View file

@ -11,7 +11,7 @@ pub use mbc3::Mbc3;
pub use mbc5::Mbc5; pub use mbc5::Mbc5;
pub use none::None; pub use none::None;
const KB: usize = 1024; pub(super) const KB: usize = 1024;
const ROM_BANK_SIZE: usize = 16 * KB; const ROM_BANK_SIZE: usize = 16 * KB;
const RAM_BANK_SIZE: usize = 8 * KB; const RAM_BANK_SIZE: usize = 8 * KB;