disk doc
This commit is contained in:
parent
c5c82ac567
commit
274a8d2c0e
@ -2,7 +2,7 @@ use crate::Disk;
|
||||
|
||||
/// driver disk
|
||||
pub struct DrvDisk {
|
||||
disk: Disk
|
||||
disk: Disk,
|
||||
}
|
||||
|
||||
impl DrvDisk {
|
||||
@ -11,34 +11,33 @@ impl DrvDisk {
|
||||
DrvDisk { disk: disk }
|
||||
}
|
||||
|
||||
///read inside the disk
|
||||
///
|
||||
/// read inside the disk
|
||||
///
|
||||
/// ### Parameters
|
||||
///
|
||||
/// - **self** driver disk
|
||||
///
|
||||
/// - **self** driver disk
|
||||
/// - **sector_number** sector where to read the data
|
||||
/// - **data** where the readed data will be stored
|
||||
pub fn read_sector(&mut self, sector_number : i32, data : &mut Vec<u8>) {
|
||||
match Disk::read_request(&mut self.disk,sector_number, data) {
|
||||
pub fn read_sector(&mut self, sector_number: i32, data: &mut Vec<u8>) {
|
||||
match Disk::read_request(&mut self.disk, sector_number, data) {
|
||||
Err(e) => println!("{:?}", e),
|
||||
_ => ()
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
///write inside the disk
|
||||
///
|
||||
/// write inside the disk
|
||||
///
|
||||
/// ### Parameters
|
||||
///
|
||||
/// - **self** driver disk
|
||||
///
|
||||
/// - **self** driver disk
|
||||
/// - **sector_number** sector where to write the data
|
||||
/// - **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) {
|
||||
Err(e) => println!("{:?}", e),
|
||||
_ => ()
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -58,10 +57,16 @@ mod test {
|
||||
let mut drv_disk = DrvDisk::init_drv_disk(disk);
|
||||
|
||||
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();
|
||||
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();
|
||||
|
||||
@ -69,10 +74,8 @@ mod test {
|
||||
drv_disk.write_sector(1, &mut data1);
|
||||
drv_disk.read_sector(1, &mut data2);
|
||||
|
||||
assert_eq!(data1,data2);
|
||||
assert_ne!(data,data1);
|
||||
assert_ne!(data,data2);
|
||||
assert_eq!(data1, data2);
|
||||
assert_ne!(data, data1);
|
||||
assert_ne!(data, data2);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +1,37 @@
|
||||
use core::panic;
|
||||
use std::io;
|
||||
use std::io::BufReader;
|
||||
use std::io::prelude::*;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::io::SeekFrom;
|
||||
|
||||
|
||||
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 = 4;
|
||||
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 = 4;
|
||||
|
||||
pub struct Disk {
|
||||
pub last_selector : i32,
|
||||
pub active : bool
|
||||
pub last_selector: i32,
|
||||
pub active: bool,
|
||||
}
|
||||
|
||||
impl Disk {
|
||||
|
||||
pub fn init_disk() -> Disk {
|
||||
Disk {
|
||||
last_selector : 0,
|
||||
active : false
|
||||
last_selector: 0,
|
||||
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 {
|
||||
panic!("Only one request at time");
|
||||
}
|
||||
@ -34,6 +40,7 @@ impl Disk {
|
||||
panic!("sector_number isn't right");
|
||||
}
|
||||
|
||||
disk.active = true;
|
||||
let mut f = File::open("test/disk.txt")?;
|
||||
f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?;
|
||||
let mut buffer = [0; SECTOR_SIZE as usize];
|
||||
@ -41,37 +48,50 @@ impl Disk {
|
||||
for byte in buffer {
|
||||
data.push(byte);
|
||||
}
|
||||
disk.active = false;
|
||||
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 {
|
||||
panic!("Only one request at time");
|
||||
}
|
||||
if sector_number < 0 || sector_number >= NUM_SECTORS {
|
||||
panic!("sector_number isn't right");
|
||||
}
|
||||
|
||||
|
||||
disk.active = true;
|
||||
let mut f = File::create("test/disk.txt")?;
|
||||
f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?;
|
||||
let mut i = 0;
|
||||
let mut buff = Vec::new();
|
||||
for value in data {
|
||||
buff.push(*value);
|
||||
i = i+1;
|
||||
i = i + 1;
|
||||
if i >= SECTOR_SIZE {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let res = f.write(&buff);
|
||||
match res {
|
||||
Ok(_) => println!("Data written successfully"),
|
||||
Err(e) => println!("{:?}", e),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
disk.active = false;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user