nouvelle version du driver et du disk

This commit is contained in:
amaury 2023-03-16 15:59:58 +01:00
parent 66eaa8a64f
commit 4c81f0591a
5 changed files with 100 additions and 14 deletions

View File

@ -0,0 +1,42 @@
use crate::Disk;
/// driver disk
pub struct DrvDisk {
disk: Disk
}
impl DrvDisk {
///initialize the disk ok the current driver
pub fn init_drv_disk(disk: Disk) -> DrvDisk {
DrvDisk { disk: disk }
}
///read inside the disk
///
/// ### Parameters
///
/// - **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) {
Err(e) => println!("{:?}", e),
_ => ()
}
}
///write inside the disk
///
/// ### Parameters
///
/// - **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>) {
match Disk::write_request(&mut self.disk, sector_number, data) {
Err(e) => println!("{:?}", e),
_ => ()
}
}
}

1
src/drivers/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod drv_disk;

View File

@ -12,15 +12,39 @@ mod simulator;
mod kernel;
/// module containing useful tools which can be use in most part of the OS to ease the development of the OS
pub mod utility;
/// module containing drivers
pub mod drivers;
use std::{rc::Rc, cell::RefCell};
use kernel::system::System;
use simulator::machine::Machine;
use simulator::{machine::Machine, print::print};
use drivers::drv_disk::DrvDisk;
use simulator::disk::Disk;
fn main() {
let machine = Machine::init_machine();
let system = Rc::new(RefCell::new(System::new(machine)));
let disk = Disk::init_disk();
let mut drv_disk = DrvDisk::init_drv_disk(disk);
let mut data = Vec::new();
data.push(1); data.push(1); data.push(1);
let mut data2: Vec<u8> = Vec::new();
drv_disk.write_sector(0, &mut data);
drv_disk.read_sector(0, &mut data2);
if data == data2 {
print!("success");
}else {
print!("fail");
}
System::freeze(system);
}

View File

@ -1,5 +1,11 @@
use core::panic;
use std::{fs::{File, self}, io::{BufReader, Read, Seek, SeekFrom, Write}};
use std::io;
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;
use std::io::SeekFrom;
use libc::close;
pub const SECTORS_PER_TRACK : i32 = 32;
pub const NUM_TRACKS : i32 = 64;
@ -7,22 +13,20 @@ 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 {
pub fn init_disk() -> 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>) {
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");
}
@ -31,22 +35,36 @@ impl Disk {
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);
let mut f = File::open("test/disk")?;
f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?;
let mut reader = BufReader::new(f);
match reader.read(data) {
Ok(_) => println!("Data has been read successfully"),
Err(e) => println!("{:?}", e),
_ => ()
}
Ok(())
}
pub fn write_request(disk : &mut Disk, sector_number : i32, data : &mut Vec<u8>) {
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.file.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64));
disk.file.write(data);
let mut f = File::create("test/disk")?;
f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?;
let res = f.write(data);
match res {
Ok(_) => println!("Data written successfully"),
Err(e) => println!("{:?}", e),
_ => ()
}
Ok(())
}
}

1
test/disk Normal file
View File

@ -0,0 +1 @@