This commit is contained in:
Baptiste 2023-03-17 17:39:14 +01:00
parent c5c82ac567
commit 274a8d2c0e
2 changed files with 69 additions and 46 deletions

View File

@ -2,7 +2,7 @@ use crate::Disk;
/// driver disk /// driver disk
pub struct DrvDisk { pub struct DrvDisk {
disk: Disk disk: Disk,
} }
impl DrvDisk { impl DrvDisk {
@ -11,34 +11,33 @@ impl DrvDisk {
DrvDisk { disk: disk } DrvDisk { disk: disk }
} }
///read inside the disk /// read inside the disk
/// ///
/// ### Parameters /// ### Parameters
/// ///
/// - **self** driver disk /// - **self** driver disk
/// - **sector_number** sector where to read the data /// - **sector_number** sector where to read the data
/// - **data** where the readed data will be stored /// - **data** where the readed data will be stored
pub fn read_sector(&mut self, sector_number : i32, data : &mut Vec<u8>) { pub fn read_sector(&mut self, sector_number: i32, data: &mut Vec<u8>) {
match Disk::read_request(&mut self.disk,sector_number, data) { match Disk::read_request(&mut self.disk, sector_number, data) {
Err(e) => println!("{:?}", e), Err(e) => println!("{:?}", e),
_ => () _ => (),
} }
} }
///write inside the disk /// write inside the disk
/// ///
/// ### Parameters /// ### Parameters
/// ///
/// - **self** driver disk /// - **self** driver disk
/// - **sector_number** sector where to write the data /// - **sector_number** sector where to write the data
/// - **data** where the data to write is stored /// - **data** where the data to write is stored
pub fn write_sector(&mut self, sector_number : i32, data : &mut Vec<u8>) { pub fn write_sector(&mut self, sector_number: i32, data: &mut Vec<u8>) {
match Disk::write_request(&mut self.disk, sector_number, data) { match Disk::write_request(&mut self.disk, sector_number, data) {
Err(e) => println!("{:?}", e), Err(e) => println!("{:?}", e),
_ => () _ => (),
} }
} }
} }
#[cfg(test)] #[cfg(test)]
@ -58,10 +57,16 @@ mod test {
let mut drv_disk = DrvDisk::init_drv_disk(disk); let mut drv_disk = DrvDisk::init_drv_disk(disk);
let mut data = Vec::new(); let mut data = Vec::new();
data.push(0 as u8); data.push(0 as u8); data.push(0 as u8); data.push(0 as u8); data.push(0 as u8);
data.push(0 as u8);
data.push(0 as u8);
data.push(0 as u8);
let mut data1 = Vec::new(); let mut data1 = Vec::new();
data1.push(1 as u8); data1.push(1 as u8); data1.push(1 as u8); data1.push(1 as u8); data1.push(1 as u8);
data1.push(1 as u8);
data1.push(1 as u8);
data1.push(1 as u8);
let mut data2: Vec<u8> = Vec::new(); let mut data2: Vec<u8> = Vec::new();
@ -69,10 +74,8 @@ mod test {
drv_disk.write_sector(1, &mut data1); drv_disk.write_sector(1, &mut data1);
drv_disk.read_sector(1, &mut data2); drv_disk.read_sector(1, &mut data2);
assert_eq!(data1,data2); assert_eq!(data1, data2);
assert_ne!(data,data1); assert_ne!(data, data1);
assert_ne!(data,data2); assert_ne!(data, data2);
} }
}
}

View File

@ -1,31 +1,37 @@
use core::panic; use core::panic;
use std::io;
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File; use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::SeekFrom; use std::io::SeekFrom;
pub const SECTORS_PER_TRACK: i32 = 32;
pub const SECTORS_PER_TRACK : i32 = 32; pub const NUM_TRACKS: i32 = 64;
pub const NUM_TRACKS : i32 = 64; pub const NUM_SECTORS: i32 = SECTORS_PER_TRACK * NUM_TRACKS;
pub const NUM_SECTORS : i32 = SECTORS_PER_TRACK * NUM_TRACKS; pub const SECTOR_SIZE: i32 = 4;
pub const SECTOR_SIZE : i32 = 4;
pub struct Disk { pub struct Disk {
pub last_selector : i32, pub last_selector: i32,
pub active : bool pub active: bool,
} }
impl Disk { impl Disk {
pub fn init_disk() -> Disk { pub fn init_disk() -> Disk {
Disk { Disk {
last_selector : 0, last_selector: 0,
active : false active: false,
} }
} }
pub fn read_request(disk : &mut Disk, sector_number : i32, data : &mut Vec<u8>) -> io::Result<()> { /// read data from a disk, at a certain sector number
///
/// `panic!` when the disk is already active or the sector number is impossible
///
/// ### parameters
///
/// - **disk** to read from
/// - **sector_number** sector where to read the data
/// - **data** where the readed data will be stored
pub fn read_request(disk: &mut Disk, sector_number: i32, data: &mut Vec<u8>) -> io::Result<()> {
if disk.active { if disk.active {
panic!("Only one request at time"); panic!("Only one request at time");
} }
@ -34,6 +40,7 @@ impl Disk {
panic!("sector_number isn't right"); panic!("sector_number isn't right");
} }
disk.active = true;
let mut f = File::open("test/disk.txt")?; let mut f = File::open("test/disk.txt")?;
f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?; f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?;
let mut buffer = [0; SECTOR_SIZE as usize]; let mut buffer = [0; SECTOR_SIZE as usize];
@ -41,37 +48,50 @@ impl Disk {
for byte in buffer { for byte in buffer {
data.push(byte); data.push(byte);
} }
disk.active = false;
Ok(()) Ok(())
} }
pub fn write_request(disk : &mut Disk, sector_number : i32, data : &mut Vec<u8>) -> io::Result<()> { /// write data into a disk, at a certain sector number
///
/// `panic!` when the disk is already active or the sector number is impossible
///
/// ### parameters
///
/// - **disk** to write data into
/// - **sector_number** sector where to write the data
/// - **data** where the data to write is stored
pub fn write_request(
disk: &mut Disk,
sector_number: i32,
data: &mut Vec<u8>,
) -> io::Result<()> {
if disk.active { if disk.active {
panic!("Only one request at time"); panic!("Only one request at time");
} }
if sector_number < 0 || sector_number >= NUM_SECTORS { if sector_number < 0 || sector_number >= NUM_SECTORS {
panic!("sector_number isn't right"); panic!("sector_number isn't right");
} }
disk.active = true;
let mut f = File::create("test/disk.txt")?; let mut f = File::create("test/disk.txt")?;
f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?; f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?;
let mut i = 0; let mut i = 0;
let mut buff = Vec::new(); let mut buff = Vec::new();
for value in data { for value in data {
buff.push(*value); buff.push(*value);
i = i+1; i = i + 1;
if i >= SECTOR_SIZE { if i >= SECTOR_SIZE {
break; break;
} }
} }
let res = f.write(&buff); let res = f.write(&buff);
match res { match res {
Ok(_) => println!("Data written successfully"), Ok(_) => println!("Data written successfully"),
Err(e) => println!("{:?}", e), Err(e) => println!("{:?}", e),
_ => () }
} disk.active = false;
Ok(()) Ok(())
} }
}
}