Add launch argument parser, add debug parameter to machine

burritos now launch binary
This commit is contained in:
Quentin Legot 2023-04-06 13:27:03 +02:00
parent 2f0b9e6592
commit 9cc57e7f03
9 changed files with 459 additions and 60 deletions

371
Cargo.lock generated
View File

@ -2,11 +2,58 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "anstream"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "342258dd14006105c2b75ab1bd7543a03bdf0cfc94383303ac212a04939dff6f"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-wincon",
"concolor-override",
"concolor-query",
"is-terminal",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23ea9e81bd02e310c216d080f6223c179012256e5151c41db88d12c88a1684d2"
[[package]]
name = "anstyle-parse"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7d1bb534e9efed14f3e5f44e7dd1a4f709384023a4165199a4241e18dff0116"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-wincon"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3127af6145b149f3287bb9a0d10ad9c5692dba8c53ad48285e5bec4063834fa"
dependencies = [
"anstyle",
"windows-sys 0.45.0",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "burritos" name = "burritos"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"cc", "cc",
"clap",
] ]
[[package]] [[package]]
@ -14,3 +61,327 @@ name = "cc"
version = "1.0.79" version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
[[package]]
name = "clap"
version = "4.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "046ae530c528f252094e4a77886ee1374437744b2bff1497aa898bbddbbb29b3"
dependencies = [
"clap_builder",
"clap_derive",
"once_cell",
]
[[package]]
name = "clap_builder"
version = "4.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "223163f58c9a40c3b0a43e1c4b50a9ce09f007ea2cb1ec258a687945b4b7929f"
dependencies = [
"anstream",
"anstyle",
"bitflags",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9644cd56d6b87dbe899ef8b053e331c0637664e9e21a33dfcdc36093f5c5c4"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1"
[[package]]
name = "concolor-override"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a855d4a1978dc52fb0536a04d384c2c0c1aa273597f08b77c8c4d3b2eec6037f"
[[package]]
name = "concolor-query"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88d11d52c3d7ca2e6d0040212be9e4dbbcd78b6447f535b6b561f449427944cf"
dependencies = [
"windows-sys 0.45.0",
]
[[package]]
name = "errno"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d6a0976c999d473fe89ad888d5a284e55366d9dc9038b1ba2aa15128c4afa0"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys 0.45.0",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "io-lifetimes"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "is-terminal"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "256017f749ab3117e93acb91063009e1f1bb56d03965b14c2c8df4eb02c524d8"
dependencies = [
"hermit-abi",
"io-lifetimes",
"rustix",
"windows-sys 0.45.0",
]
[[package]]
name = "libc"
version = "0.2.141"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5"
[[package]]
name = "linux-raw-sys"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f"
[[package]]
name = "once_cell"
version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "proc-macro2"
version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rustix"
version = "0.37.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2aae838e49b3d63e9274e1c01833cc8139d3fec468c3b84688c628f44b1ae11d"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys 0.45.0",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "windows-sys"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [
"windows-targets 0.42.2",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.0",
]
[[package]]
name = "windows-targets"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
dependencies = [
"windows_aarch64_gnullvm 0.42.2",
"windows_aarch64_msvc 0.42.2",
"windows_i686_gnu 0.42.2",
"windows_i686_msvc 0.42.2",
"windows_x86_64_gnu 0.42.2",
"windows_x86_64_gnullvm 0.42.2",
"windows_x86_64_msvc 0.42.2",
]
[[package]]
name = "windows-targets"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5"
dependencies = [
"windows_aarch64_gnullvm 0.48.0",
"windows_aarch64_msvc 0.48.0",
"windows_i686_gnu 0.48.0",
"windows_i686_msvc 0.48.0",
"windows_x86_64_gnu 0.48.0",
"windows_x86_64_gnullvm 0.48.0",
"windows_x86_64_msvc 0.48.0",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_i686_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"

View File

@ -7,4 +7,7 @@ edition = "2021"
protocol = "sparse" protocol = "sparse"
[build-dependencies] [build-dependencies]
cc = "1.0" cc = "1.0"
[dependencies]
clap = { version = "4.2.1", features = ["derive"] }

View File

@ -245,7 +245,7 @@ mod test {
#[test] #[test]
fn test_sc_shutdown() { fn test_sc_shutdown() {
let mut machine = Machine::init_machine(); let mut machine = Machine::new(true);
machine.write_int_register(17, SC_SHUTDOWN as i64); // Set type to shutdown machine.write_int_register(17, SC_SHUTDOWN as i64); // Set type to shutdown
let ecall = Instruction::new(0b000000000000_00000_000_00000_1110011); let ecall = Instruction::new(0b000000000000_00000_000_00000_1110011);
@ -261,7 +261,7 @@ mod test {
#[test] #[test]
#[ignore] #[ignore]
fn test_sc_print() { fn test_sc_print() {
let mut machine = Machine::init_machine(); let mut machine = Machine::new(true);
let address = machine.read_int_register(10); let address = machine.read_int_register(10);
// Write string 'HELLO' in memory // Write string 'HELLO' in memory

View File

@ -277,7 +277,7 @@ mod test {
#[test] #[test]
fn test_semaphore_single() { fn test_semaphore_single() {
// Init // Init
let mut machine = Machine::init_machine(); let mut machine = Machine::new(true);
let mut thread_manager = ThreadManager::new(); let mut thread_manager = ThreadManager::new();
let mut semaphore = Semaphore::new(1); let mut semaphore = Semaphore::new(1);
let thread = Rc::new(RefCell::new(Thread::new("test_semaphore"))); let thread = Rc::new(RefCell::new(Thread::new("test_semaphore")));
@ -297,7 +297,7 @@ mod test {
fn test_semaphore_multiple() { fn test_semaphore_multiple() {
// Init // Init
let mut tm = ThreadManager::new(); let mut tm = ThreadManager::new();
let mut machine = Machine::init_machine(); let mut machine = Machine::new(true);
let mut semaphore = Semaphore::new(2); let mut semaphore = Semaphore::new(2);
let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1"))); let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1")));
let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2"))); let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2")));
@ -342,7 +342,7 @@ mod test {
#[test] #[test]
fn test_lock_simple() { fn test_lock_simple() {
let mut machine = Machine::init_machine(); let mut machine = Machine::new(true);
let mut tm = ThreadManager::new(); let mut tm = ThreadManager::new();
let thread = Rc::new(RefCell::new(Thread::new("test_lock"))); let thread = Rc::new(RefCell::new(Thread::new("test_lock")));
tm.ready_to_run(Rc::clone(&thread)); tm.ready_to_run(Rc::clone(&thread));
@ -364,7 +364,7 @@ mod test {
let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1"))); let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1")));
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2"))); let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2")));
let mut machine = Machine::init_machine(); let mut machine = Machine::new(true);
let mut tm = ThreadManager::new(); let mut tm = ThreadManager::new();
tm.ready_to_run(Rc::clone(&thread1)); tm.ready_to_run(Rc::clone(&thread1));

View File

@ -206,7 +206,7 @@ mod test {
#[test] #[test]
fn test_thread_context() { fn test_thread_context() {
let mut machine = Machine::default(); let mut machine = Machine::new(true);
let (loader, ptr) = loader::Loader::new("./target/guac/halt.guac", &mut machine, 0).expect("IO Error"); let (loader, ptr) = loader::Loader::new("./target/guac/halt.guac", &mut machine, 0).expect("IO Error");
let start_pc = loader.elf_header.entrypoint; let start_pc = loader.elf_header.entrypoint;

View File

@ -13,11 +13,45 @@ 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;
use kernel::system::System; use std::{rc::Rc, cell::RefCell};
use simulator::machine::Machine;
use kernel::{system::System, thread::Thread, process::Process};
use simulator::{machine::Machine, loader};
use clap::Parser;
#[derive(Parser, Debug)]
#[command(name = "BurritOS", author, version, about = "Burritos (BurritOS Using Rust Really Improves The Operating System)
Burritos is an educational operating system written in Rust
running on RISC-V emulator.", long_about = None)]
struct Args {
/// Enable debug mode
#[arg(short, long)]
debug: bool,
/// Path to the executable binary file to execute
#[arg(short = 'x', long, value_name = "PATH")]
executable: String
}
fn main() { fn main() {
let mut machine = Machine::init_machine(); let args = Args::parse();
let mut machine = Machine::new(args.debug);
let (loader, ptr) = loader::Loader::new(args.executable.as_str(), &mut machine, 0).expect("IO Error");
let mut system = System::default(); let mut system = System::default();
let thread_exec = Thread::new(args.executable.as_str());
let thread_exec = Rc::new(RefCell::new(thread_exec));
system.get_thread_manager().get_g_alive().push(Rc::clone(&thread_exec));
let owner1 = Process { num_thread: 0 };
system.get_thread_manager().start_thread(Rc::clone(&thread_exec), owner1, loader.elf_header.entrypoint, ptr, -1);
let to_run = system.get_thread_manager().find_next_to_run().unwrap();
system.get_thread_manager().switch_to(&mut machine, Rc::clone(&to_run));
machine.run(&mut system); machine.run(&mut system);
} }

View File

@ -612,7 +612,7 @@ mod test {
#[test] #[test]
fn test_parse_elf() { fn test_parse_elf() {
let mut machine = Machine::default(); let mut machine = Machine::new(true);
let loader = Loader::load_and_parse("./target/guac/unsigned_addition.guac").expect("IO Error"); let loader = Loader::load_and_parse("./target/guac/unsigned_addition.guac").expect("IO Error");
loader.load_into_machine(&mut machine, 0).expect("Parsing error"); loader.load_into_machine(&mut machine, 0).expect("Parsing error");
assert!(!loader.elf_header.is_32bits); assert!(!loader.elf_header.is_32bits);
@ -633,7 +633,7 @@ mod test {
#[test] #[test]
fn test_parse_section() { fn test_parse_section() {
let mut machine = Machine::default(); let mut machine = Machine::new(true);
let loader = Loader::load_and_parse("./target/guac/unsigned_addition.guac").expect("IO Error"); let loader = Loader::load_and_parse("./target/guac/unsigned_addition.guac").expect("IO Error");
loader.load_into_machine(&mut machine, 0).expect("Parsing error"); loader.load_into_machine(&mut machine, 0).expect("Parsing error");
assert_eq!(9, loader.sections.len()); assert_eq!(9, loader.sections.len());

View File

@ -80,6 +80,8 @@ pub const MEM_SIZE : usize = (PAGE_SIZE*NUM_PHY_PAGE*100_000) as usize;
/// RISC-V Simulator /// RISC-V Simulator
pub struct Machine { pub struct Machine {
/// Debug mode of the machine
debug: bool,
/// Program counter /// Program counter
pub pc : u64, pub pc : u64,
/// Stack pointer /// Stack pointer
@ -107,9 +109,28 @@ pub struct Machine {
impl Machine { impl Machine {
/// Machine constructor /// Machine constructor
#[deprecated] pub fn new(debug: bool) -> Self {
pub fn init_machine() -> Machine { let mut shiftmask : [u64 ; 64] = [0 ; 64];
Self::default() let mut value : u64 = 0xffffffff;
value = (value << 32) + value;
for item in &mut shiftmask {
*item = value;
value >>= 1;
}
Machine {
debug,
pc : 0,
sp: 0,
int_reg : { let mut r = Register::<i64>::init(); r.set_reg(10, -1); r },
fp_reg : Register::<f32>::init(),
main_memory : vec![0_u8; MEM_SIZE],
shiftmask,
interrupt: Interrupt::new(),
registers_trace : String::from(""),
status: MachineStatus::SystemMode
}
} }
/// Read from main memory of the machine /// Read from main memory of the machine
@ -189,10 +210,8 @@ impl Machine {
println!(); println!();
} }
println!("________________SP________________"); println!("________________SP________________");
let sp_index = self.int_reg.get_reg(2); let sp = self.int_reg.get_reg(2);
/* for i in 0..5 { println!("SP: {:16x}", self.read_memory(8, sp as usize));
println!("SP+{:<2} : {:16x}", i*8, self.read_memory(8, (sp_index + i*8) as usize));
} */
println!("##################################"); println!("##################################");
} }
@ -267,11 +286,13 @@ impl Machine {
let val = u32::from_be_bytes(val) as u64; let val = u32::from_be_bytes(val) as u64;
let inst : Instruction = Instruction::new(val); let inst : Instruction = Instruction::new(val);
self.print_status(); if self.debug {
println!("executing instruction : {:016x} at pc {:x}", val, self.pc); self.print_status();
println!("{}", instruction::instruction_debug(&inst, self.pc as i32)); println!("executing instruction : {:016x} at pc {:x}", val, self.pc);
let trace = Self::string_registers(self); println!("{}", instruction::instruction_debug(&inst, self.pc as i32));
self.registers_trace.push_str(format!("{}\n", trace).as_str()); let trace = Self::string_registers(self);
self.registers_trace.push_str(format!("{}\n", trace).as_str());
}
self.pc += 4; self.pc += 4;
@ -680,42 +701,12 @@ impl Machine {
} }
} }
impl Default for Machine {
/// Machine constructor
fn default() -> Self {
let mut shiftmask : [u64 ; 64] = [0 ; 64];
let mut value : u64 = 0xffffffff;
value = (value << 32) + value;
for item in &mut shiftmask {
*item = value;
value >>= 1;
}
Machine {
pc : 0,
sp: 0,
int_reg : { let mut r = Register::<i64>::init(); r.set_reg(10, -1); r },
fp_reg : Register::<f32>::init(),
main_memory : vec![0_u8; MEM_SIZE],
shiftmask,
interrupt: Interrupt::new(),
registers_trace : String::from(""),
status: MachineStatus::SystemMode
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::fs; use std::fs;
use crate::simulator::{machine::Machine, mem_cmp}; use crate::simulator::{machine::Machine, mem_cmp};
use crate::kernel::thread::Thread;
use crate::kernel::process::Process;
use std::rc::Rc;
use std::cell::RefCell;
macro_rules! get_full_path { macro_rules! get_full_path {
($prefix: expr, $test_name:expr) => {{ ($prefix: expr, $test_name:expr) => {{
@ -729,7 +720,7 @@ mod test {
macro_rules! init_test { macro_rules! init_test {
($a:expr) => {{ ($a:expr) => {{
let mut m = Machine::init_machine(); let mut m = Machine::new(true);
let end_file_name = { let mut s = String::from($a); s.push_str("End"); s }; let end_file_name = { let mut s = String::from($a); s.push_str("End"); s };
let memory_before = mem_cmp::MemChecker::from(get_full_path!("memory", $a)).unwrap(); let memory_before = mem_cmp::MemChecker::from(get_full_path!("memory", $a)).unwrap();
let memory_after = mem_cmp::MemChecker::from(get_full_path!("memory", &end_file_name)).unwrap(); let memory_after = mem_cmp::MemChecker::from(get_full_path!("memory", &end_file_name)).unwrap();
@ -744,12 +735,12 @@ mod test {
#[test] #[test]
fn test_init_machine() { fn test_init_machine() {
let _ = Machine::init_machine(); let _ = Machine::new(true);
} }
#[test] #[test]
fn test_read_memory() { fn test_read_memory() {
let mut m = Machine::init_machine(); let mut m = Machine::new(true);
m.main_memory[4] = 43; m.main_memory[4] = 43;
m.main_memory[5] = 150; m.main_memory[5] = 150;
assert_eq!((43 << 8) + 150, m.read_memory(2, 4)); assert_eq!((43 << 8) + 150, m.read_memory(2, 4));
@ -757,7 +748,7 @@ mod test {
#[test] #[test]
fn test_write_memory() { fn test_write_memory() {
let mut m = Machine::init_machine(); let mut m = Machine::new(true);
m.write_memory(2, 6, (43 << 8) + 150); m.write_memory(2, 6, (43 << 8) + 150);
assert_eq!(43, m.main_memory[6]); assert_eq!(43, m.main_memory[6]);
assert_eq!(150, m.main_memory[7]); assert_eq!(150, m.main_memory[7]);

View File

@ -211,7 +211,7 @@ mod tests {
#[test] #[test]
fn test_fill_memory(){ fn test_fill_memory(){
let m_c = MemChecker::from("test/machine/memoryAdd.txt").unwrap(); let m_c = MemChecker::from("test/machine/memoryAdd.txt").unwrap();
let mut machine = Machine::init_machine(); let mut machine = Machine::new(true);
MemChecker::fill_memory_from_mem_checker(&m_c, &mut machine); MemChecker::fill_memory_from_mem_checker(&m_c, &mut machine);
MemChecker::compare_print_m_c_machine(&m_c, &mut machine); MemChecker::compare_print_m_c_machine(&m_c, &mut machine);
} }