start of Disk

This commit is contained in:
Baptiste 2023-03-15 17:53:10 +01:00
parent 38004e0bc4
commit 66eaa8a64f
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,52 @@
use core::panic;
use std::{fs::{File, self}, io::{BufReader, Read, Seek, SeekFrom, Write}};
pub const SECTORS_PER_TRACK : i32 = 32;
pub const NUM_TRACKS : i32 = 64;
pub const NUM_SECTORS : i32 = SECTORS_PER_TRACK * NUM_TRACKS;
pub const SECTOR_SIZE : i32 = 128;
pub struct Disk {
pub file : File,
pub last_selector : i32,
pub active : bool
}
impl Disk {
pub fn init_disk(path : String) -> Disk {
Disk {
file : fs::File::open(path).unwrap(),
last_selector : 0,
active : false
}
}
pub fn read_request(disk : &mut Disk, sector_number : i32, data : &mut Vec<u8>) {
if disk.active {
panic!("Only one request at time");
}
if sector_number < 0 || sector_number >= NUM_SECTORS {
panic!("sector_number isn't right");
}
disk.file.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64));
let mut reader = BufReader::new(&disk.file);
reader.read(data);
}
pub fn write_request(disk : &mut Disk, sector_number : i32, data : &mut Vec<u8>) {
if disk.active {
panic!("Only one request at time");
}
if sector_number < 0 || sector_number >= NUM_SECTORS {
panic!("sector_number isn't right");
}
disk.file.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64));
disk.file.write(data);
}
}

View File

@ -6,6 +6,7 @@ pub mod loader;
pub mod interrupt;
pub mod translationtable;
pub mod mmu;
pub mod disk;
pub mod global {