Fix stoarge card IO register positions and allow reading off the end of the disk

This commit is contained in:
pjht 2022-11-29 16:28:55 -06:00
parent 76e54acfeb
commit 40ea7eae82

View File

@ -13,7 +13,7 @@ use std::{
collections::VecDeque, collections::VecDeque,
fmt::Display, fmt::Display,
fs::File, fs::File,
io::{Read, Seek, SeekFrom}, io::{self, Read, Seek, SeekFrom},
}; };
use toml::Value; use toml::Value;
@ -48,7 +48,7 @@ pub struct Storage {
// 0x4-0x8: Sector count // 0x4-0x8: Sector count
// 0x8-0xA: Command (W) / Status(R) // 0x8-0xA: Command (W) / Status(R)
// 0xA-0xC: Data // 0xA-0xC: Data
// 0xC-0x11: Start address for DMA // 0xC-0x10: Start address for DMA
impl Card for Storage { impl Card for Storage {
fn new(data: Value) -> anyhow::Result<(Self, Option<Box<dyn DMAHandler>>)> { fn new(data: Value) -> anyhow::Result<(Self, Option<Box<dyn DMAHandler>>)> {
@ -86,7 +86,7 @@ impl Card for Storage {
} }
NullableResult::Ok(self.read_data.pop_front().unwrap_or(0)) NullableResult::Ok(self.read_data.pop_front().unwrap_or(0))
} }
0xC..=0x10 => NullableResult::Ok(u32_get_be_byte(self.start_addresss, address - 0xD)), 0xC..=0xF => NullableResult::Ok(u32_get_be_byte(self.start_addresss, address - 0xC)),
(0xFE..=0xFF) => NullableResult::Ok(u16_get_be_byte(ID, address - 0xFE)), (0xFE..=0xFF) => NullableResult::Ok(u16_get_be_byte(ID, address - 0xFE)),
_ => NullableResult::Null, _ => NullableResult::Null,
} }
@ -109,7 +109,11 @@ impl Card for Storage {
.unwrap(); .unwrap();
let mut buf = Vec::new(); let mut buf = Vec::new();
buf.resize(self.count as usize * SECTOR_SIZE as usize, 0); buf.resize(self.count as usize * SECTOR_SIZE as usize, 0);
file.read_exact(&mut buf).unwrap(); match file.read_exact(&mut buf) {
Ok(_) => (),
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => (),
Err(e) => Err(e).unwrap(),
}
self.read_data.extend(buf); self.read_data.extend(buf);
self.status.set(Status::DATA_READY, true); self.status.set(Status::DATA_READY, true);
} }
@ -120,7 +124,11 @@ impl Card for Storage {
.unwrap(); .unwrap();
let mut buf = Vec::new(); let mut buf = Vec::new();
buf.resize(self.count as usize * SECTOR_SIZE as usize, 0); buf.resize(self.count as usize * SECTOR_SIZE as usize, 0);
file.read_exact(&mut buf).unwrap(); match file.read_exact(&mut buf) {
Ok(_) => (),
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => (),
Err(e) => Err(e).unwrap(),
}
self.read_data.extend(buf); self.read_data.extend(buf);
self.status.set(Status::BUSY, true); self.status.set(Status::BUSY, true);
self.transfer = true; self.transfer = true;