nouvelle version du driver et du disk
This commit is contained in:
parent
66eaa8a64f
commit
4c81f0591a
@ -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
1
src/drivers/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod drv_disk;
|
26
src/main.rs
26
src/main.rs
@ -12,15 +12,39 @@ mod simulator;
|
|||||||
mod kernel;
|
mod kernel;
|
||||||
/// module containing useful tools which can be use in most part of the OS to ease the development of the OS
|
/// module containing useful tools which can be use in most part of the OS to ease the development of the OS
|
||||||
pub mod utility;
|
pub mod utility;
|
||||||
|
/// module containing drivers
|
||||||
|
pub mod drivers;
|
||||||
|
|
||||||
use std::{rc::Rc, cell::RefCell};
|
use std::{rc::Rc, cell::RefCell};
|
||||||
|
|
||||||
use kernel::system::System;
|
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() {
|
fn main() {
|
||||||
let machine = Machine::init_machine();
|
let machine = Machine::init_machine();
|
||||||
let system = Rc::new(RefCell::new(System::new(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);
|
System::freeze(system);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
use core::panic;
|
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 SECTORS_PER_TRACK : i32 = 32;
|
||||||
pub const NUM_TRACKS : i32 = 64;
|
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 const SECTOR_SIZE : i32 = 128;
|
||||||
|
|
||||||
pub struct Disk {
|
pub struct Disk {
|
||||||
pub file : File,
|
|
||||||
pub last_selector : i32,
|
pub last_selector : i32,
|
||||||
pub active : bool
|
pub active : bool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Disk {
|
impl Disk {
|
||||||
|
|
||||||
pub fn init_disk(path : String) -> Disk {
|
pub fn init_disk() -> Disk {
|
||||||
Disk {
|
Disk {
|
||||||
file : fs::File::open(path).unwrap(),
|
|
||||||
last_selector : 0,
|
last_selector : 0,
|
||||||
active : false
|
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 {
|
if disk.active {
|
||||||
panic!("Only one request at time");
|
panic!("Only one request at time");
|
||||||
}
|
}
|
||||||
@ -31,22 +35,36 @@ impl Disk {
|
|||||||
panic!("sector_number isn't right");
|
panic!("sector_number isn't right");
|
||||||
}
|
}
|
||||||
|
|
||||||
disk.file.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64));
|
let mut f = File::open("test/disk")?;
|
||||||
let mut reader = BufReader::new(&disk.file);
|
f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?;
|
||||||
reader.read(data);
|
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 {
|
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.file.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64));
|
let mut f = File::create("test/disk")?;
|
||||||
disk.file.write(data);
|
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(())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user