From 93d331bb078c75b7ab030c4a6509306c3a4e9cd0 Mon Sep 17 00:00:00 2001 From: Ry Date: Sat, 12 Mar 2022 13:12:10 -0800 Subject: [PATCH] fox32+fox32rom: Use DMA for the disk controller Instead of accessing one byte at a time, use a DMA-like system to read/write a whole sector at a time directly to/from memory. --- src/bus.rs | 22 ++++++++-------------- src/disk.rs | 14 ++++++++------ 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/bus.rs b/src/bus.rs index 9c38b63..a842165 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -88,11 +88,8 @@ impl Bus { } } 0x20 => { - // we're reading from the sector buffer - if address_or_id > 512 { - panic!("attempted to read past the end of the disk controller sector buffer"); - } - self.disk_controller.sector_buffer[address_or_id] as u32 + // we're getting the location of the memory sector buffer + self.disk_controller.buffer_pointer as u32 } _ => panic!("invalid disk controller port"), } @@ -174,27 +171,24 @@ impl Bus { }; } 0x20 => { - // we're writing to the sector buffer - if address_or_id > 512 { - panic!("attempted to read past the end of the disk controller sector buffer"); - } - self.disk_controller.sector_buffer[address_or_id] = word as u8; + // we're setting the location of the memory sector buffer + self.disk_controller.buffer_pointer = word as usize; } 0x30 => { - // we're reading the specified sector of the specified disk id into the sector buffer + // we're reading the specified sector of the specified disk id into the memory sector buffer if address_or_id > 3 { panic!("invalid disk ID"); } self.disk_controller.set_current_sector(address_or_id as u8, word); - self.disk_controller.read_into_buffer(address_or_id as u8); + self.disk_controller.read_into_memory(address_or_id as u8, self.memory.ram()); } 0x40 => { - // we're writing the specified sector to the specified disk id from the sector buffer + // we're writing the specified sector to the specified disk id from the memory sector buffer if address_or_id > 3 { panic!("invalid disk ID"); } self.disk_controller.set_current_sector(address_or_id as u8, word); - self.disk_controller.write_from_buffer(address_or_id as u8); + self.disk_controller.write_from_memory(address_or_id as u8, self.memory.ram()); } _ => panic!("invalid disk controller port"), } diff --git a/src/disk.rs b/src/disk.rs index 291d2e7..ed0228b 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -1,5 +1,7 @@ // disk.rs +use crate::memory::MemoryRam; + use std::fs::File; use std::io::{Seek, SeekFrom, Read, Write}; use rfd::FileDialog; @@ -22,14 +24,14 @@ impl Disk { pub struct DiskController { pub disk: [Option; 4], - pub sector_buffer: [u8; 512], + pub buffer_pointer: usize, } impl DiskController { pub fn new() -> Self { DiskController { disk: [None, None, None, None], - sector_buffer: [0; 512] + buffer_pointer: 0x00000000 } } pub fn select_file(&self) -> Option { @@ -64,18 +66,18 @@ impl DiskController { let disk = self.disk[disk_id as usize].as_mut().expect("attempted to access unmounted disk"); disk.current_sector } - pub fn read_into_buffer(&mut self, disk_id: u8) -> usize { + pub fn read_into_memory(&mut self, disk_id: u8, ram: &mut MemoryRam) -> usize { let disk = self.disk[disk_id as usize].as_mut().expect("attempted to access unmounted disk"); let mut temp_buffer = [0u8; 512]; let number_of_bytes_read = disk.file.read(&mut temp_buffer).unwrap(); - self.sector_buffer = temp_buffer; + ram[self.buffer_pointer..self.buffer_pointer+512].copy_from_slice(&temp_buffer); number_of_bytes_read } - pub fn write_from_buffer(&mut self, disk_id: u8) -> usize { + pub fn write_from_memory(&mut self, disk_id: u8, ram: &MemoryRam) -> usize { let disk = self.disk[disk_id as usize].as_mut().expect("attempted to access unmounted disk"); - let number_of_bytes_written = disk.file.write(&mut self.sector_buffer).unwrap(); + let number_of_bytes_written = disk.file.write(ram.get(self.buffer_pointer..self.buffer_pointer+512).unwrap()).unwrap(); number_of_bytes_written } } \ No newline at end of file