disk doc
This commit is contained in:
@ -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(())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user