eeprom saves (#4)

* eeprom start

* finish
This commit is contained in:
Logan McNaughton 2023-10-05 10:01:33 -06:00 committed by GitHub
parent 06738ea965
commit 7dc0a3902a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 96 additions and 11 deletions

View File

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dirs = "5.0"
sha2 = "0.10"
sdl2-sys = { git = "https://github.com/Rust-SDL2/rust-sdl2.git" }

View File

@ -47,7 +47,7 @@ pub struct Device {
ai: ai::Ai,
si: si::Si,
ri: ri::Ri,
game: String,
pub game: String,
}
impl Device {

View File

@ -1,4 +1,5 @@
use crate::device;
use crate::ui;
pub const JCMD_STATUS: u8 = 0x00;
pub const JCMD_EEPROM_READ: u8 = 0x04;
@ -9,7 +10,7 @@ pub const JCMD_RESET: u8 = 0xff;
pub const JDT_EEPROM_4K: u16 = 0x8000; /* 4k EEPROM */
//pub const JDT_EEPROM_16K: u16 = 0xc000; /* 16k EEPROM */
pub const EEPROM_TYPE: u16 = JDT_EEPROM_4K; // todo, support 16k eeprom
//pub const EEPROM_BLOCK_SIZE: usize = 8;
pub const EEPROM_BLOCK_SIZE: usize = 8;
pub fn process(device: &mut device::Device, channel: usize) {
let cmd = device.pif.ram[device.pif.channels[channel].tx_buf.unwrap()];
@ -41,15 +42,34 @@ pub fn process(device: &mut device::Device, channel: usize) {
}
}
pub fn eeprom_read_block(_device: &mut device::Device, _block: usize, _data: usize) {
panic!("eeprom read")
pub fn eeprom_read_block(device: &mut device::Device, block: usize, offset: usize) {
let address = device.pif.ram[block as usize] as usize * EEPROM_BLOCK_SIZE;
if address < device.ui.saves.eeprom.len() {
device.pif.ram[offset..offset + EEPROM_BLOCK_SIZE]
.copy_from_slice(&device.ui.saves.eeprom[address..address + EEPROM_BLOCK_SIZE]);
} else {
for i in 0..EEPROM_BLOCK_SIZE {
device.pif.ram[offset + i] = 0;
}
}
}
pub fn eeprom_write_block(
_device: &mut device::Device,
_block: usize,
_data: usize,
_status: usize,
) {
panic!("eeprom write")
pub fn eeprom_write_block(device: &mut device::Device, block: usize, offset: usize, status: usize) {
let address = device.pif.ram[block as usize] as usize * EEPROM_BLOCK_SIZE;
if address + EEPROM_BLOCK_SIZE > device.ui.saves.eeprom.len() {
device
.ui
.saves
.eeprom
.resize(address + EEPROM_BLOCK_SIZE, 0)
}
device.ui.saves.eeprom[address..address + EEPROM_BLOCK_SIZE]
.copy_from_slice(&device.pif.ram[offset..offset + EEPROM_BLOCK_SIZE]);
device.pif.ram[status as usize] = 0x00;
ui::storage::write_save(&mut device.ui, ui::storage::SaveTypes::Eeprom);
}

View File

@ -26,5 +26,9 @@ fn main() {
device::rdp::init(&mut device);
device::vi::init(&mut device);
device::cpu::init(&mut device);
ui::storage::init(&mut device.ui, device.game.as_str());
ui::storage::load_saves(&mut device.ui);
device::cpu::run(&mut device);
}

View File

@ -1,8 +1,11 @@
pub mod audio;
pub mod input;
pub mod storage;
pub mod video;
pub struct Ui {
pub paths: storage::Paths,
pub saves: storage::Saves,
pub sdl_context: Option<sdl2::Sdl>,
pub video_subsystem: Option<sdl2::VideoSubsystem>,
pub audio_subsystem: Option<sdl2::AudioSubsystem>,
@ -22,6 +25,10 @@ impl Ui {
.build()
.unwrap();
Ui {
paths: storage::Paths {
eep_file_path: std::path::PathBuf::new(),
},
saves: storage::Saves { eeprom: Vec::new() },
sdl_context: Some(sdl_context),
video_subsystem: Some(video_subsystem),
audio_subsystem: Some(audio_subsystem),

53
src/ui/storage.rs Normal file
View File

@ -0,0 +1,53 @@
use crate::ui;
pub enum SaveTypes {
Eeprom,
}
pub struct Paths {
pub eep_file_path: std::path::PathBuf,
}
pub struct Saves {
pub eeprom: Vec<u8>,
}
pub fn init(ui: &mut ui::Ui, game_name: &str) {
let mut base_path = dirs::data_dir().unwrap();
base_path.push("gopher64");
base_path.push("saves");
let result = std::fs::create_dir_all(base_path.clone());
if result.is_err() {
panic!("could not create save dir")
}
ui.paths.eep_file_path = base_path;
ui.paths.eep_file_path.push(game_name.to_owned() + ".eep");
}
pub fn load_saves(ui: &mut ui::Ui) {
let eep = std::fs::read(&mut ui.paths.eep_file_path);
if eep.is_ok() {
ui.saves.eeprom = eep.unwrap();
}
}
pub fn write_save(ui: &mut ui::Ui, save_type: SaveTypes) {
let path: &std::path::Path;
let data: &Vec<u8>;
match save_type {
SaveTypes::Eeprom => {
path = ui.paths.eep_file_path.as_ref();
data = ui.saves.eeprom.as_ref();
}
}
let result = std::fs::write(path, data);
if result.is_err() {
panic!(
"could not save {} {}",
path.display(),
result.err().unwrap()
)
}
}