Renamed Machine::init_machine and fixed stack overflow by allocating main_memory to the heap
This commit is contained in:
parent
1f54ed35db
commit
177abfe846
@ -6,6 +6,6 @@ use kernel::system::System;
|
|||||||
use simulator::machine::Machine;
|
use simulator::machine::Machine;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let machine = Machine::_init_machine();
|
let machine = Machine::init_machine();
|
||||||
let _system = System::new(machine);
|
let _system = System::new(machine);
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ use std::io::BufRead;
|
|||||||
pub fn _load(path : &str, instruction_size: i32) -> Machine {
|
pub fn _load(path : &str, instruction_size: i32) -> Machine {
|
||||||
let file = fs::File::open(path).expect("Wrong filename");
|
let file = fs::File::open(path).expect("Wrong filename");
|
||||||
let reader = io::BufReader::new(file);
|
let reader = io::BufReader::new(file);
|
||||||
let mut machine = Machine::_init_machine();
|
let mut machine = Machine::init_machine();
|
||||||
|
|
||||||
for (i,line) in reader.lines().enumerate() {
|
for (i,line) in reader.lines().enumerate() {
|
||||||
let res = u64::from_str_radix(&line.unwrap(), 16);
|
let res = u64::from_str_radix(&line.unwrap(), 16);
|
||||||
|
@ -73,7 +73,7 @@ pub struct Machine {
|
|||||||
pub sp: usize,
|
pub sp: usize,
|
||||||
pub int_reg : Register<i64>,
|
pub int_reg : Register<i64>,
|
||||||
pub fp_reg : Register<f32>,
|
pub fp_reg : Register<f32>,
|
||||||
pub main_memory : [u8 ; MEM_SIZE],
|
pub main_memory : Vec<u8>,
|
||||||
pub shiftmask : [u64 ; 64],
|
pub shiftmask : [u64 ; 64],
|
||||||
pub registers_trace : String, // for tests
|
pub registers_trace : String, // for tests
|
||||||
pub interrupt: Interrupt
|
pub interrupt: Interrupt
|
||||||
@ -84,7 +84,7 @@ pub struct Machine {
|
|||||||
|
|
||||||
impl Machine {
|
impl Machine {
|
||||||
|
|
||||||
pub fn _init_machine() -> Machine {
|
pub fn init_machine() -> Machine {
|
||||||
let mut shiftmask : [u64 ; 64] = [0 ; 64];
|
let mut shiftmask : [u64 ; 64] = [0 ; 64];
|
||||||
let mut value : u64 = 0xffffffff;
|
let mut value : u64 = 0xffffffff;
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ impl Machine {
|
|||||||
sp: 0,
|
sp: 0,
|
||||||
int_reg : Register::<i64>::init(),
|
int_reg : Register::<i64>::init(),
|
||||||
fp_reg : Register::<f32>::init(),
|
fp_reg : Register::<f32>::init(),
|
||||||
main_memory : [0; MEM_SIZE],
|
main_memory : vec![0_u8; MEM_SIZE],
|
||||||
shiftmask,
|
shiftmask,
|
||||||
interrupt: Interrupt::new(),
|
interrupt: Interrupt::new(),
|
||||||
registers_trace : String::from("")
|
registers_trace : String::from("")
|
||||||
@ -700,9 +700,14 @@ mod test {
|
|||||||
|
|
||||||
use crate::simulator::{machine::Machine, mem_cmp};
|
use crate::simulator::{machine::Machine, mem_cmp};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_init_machine() {
|
||||||
|
let _ = Machine::init_machine();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_read_memory() {
|
fn test_read_memory() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
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, Machine::read_memory(&mut m, 2, 4));
|
assert_eq!((43 << 8) + 150, Machine::read_memory(&mut m, 2, 4));
|
||||||
@ -710,7 +715,7 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_write_memory() {
|
fn test_write_memory() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
Machine::write_memory(&mut m, 2, 6, (43 << 8) + 150);
|
Machine::write_memory(&mut m, 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]);
|
||||||
@ -723,11 +728,11 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_comp() {
|
fn test_comp() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
let path_before = "memoryComp.txt".to_string();
|
let path_before = "memoryComp.txt".to_string();
|
||||||
let path_after = "memoryCompEnd.txt".to_string();
|
let path_after = "memoryCompEnd.txt".to_string();
|
||||||
let memory_before = mem_cmp::MemChecker::from(&path_before);
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
||||||
let memory_after = mem_cmp::MemChecker::from(&path_after);
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
Machine::run(&mut m);
|
Machine::run(&mut m);
|
||||||
|
|
||||||
@ -740,11 +745,11 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_div() {
|
fn test_div() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
let path_before = "memoryDiv.txt".to_string();
|
let path_before = "memoryDiv.txt";
|
||||||
let path_after = "memoryDivEnd.txt".to_string();
|
let path_after = "memoryDivEnd.txt";
|
||||||
let memory_before = mem_cmp::MemChecker::from(&path_before);
|
let memory_before = mem_cmp::MemChecker::from(path_before).unwrap();
|
||||||
let memory_after = mem_cmp::MemChecker::from(&path_after);
|
let memory_after = mem_cmp::MemChecker::from(path_after).unwrap();
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
Machine::run(&mut m);
|
Machine::run(&mut m);
|
||||||
|
|
||||||
@ -757,11 +762,11 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_if() {
|
fn test_if() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
let path_before = "memoryIf.txt".to_string();
|
let path_before = "memoryIf.txt".to_string();
|
||||||
let path_after = "memoryIfEnd.txt".to_string();
|
let path_after = "memoryIfEnd.txt".to_string();
|
||||||
let memory_before = mem_cmp::MemChecker::from(&path_before);
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
||||||
let memory_after = mem_cmp::MemChecker::from(&path_after);
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
Machine::run(&mut m);
|
Machine::run(&mut m);
|
||||||
|
|
||||||
@ -774,11 +779,11 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_jump() {
|
fn test_jump() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
let path_before = "memoryJump.txt".to_string();
|
let path_before = "memoryJump.txt".to_string();
|
||||||
let path_after = "memoryJumpEnd.txt".to_string();
|
let path_after = "memoryJumpEnd.txt".to_string();
|
||||||
let memory_before = mem_cmp::MemChecker::from(&path_before);
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
||||||
let memory_after = mem_cmp::MemChecker::from(&path_after);
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
Machine::run(&mut m);
|
Machine::run(&mut m);
|
||||||
|
|
||||||
@ -791,11 +796,11 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mul() {
|
fn test_mul() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
let path_before = "memoryMul.txt".to_string();
|
let path_before = "memoryMul.txt".to_string();
|
||||||
let path_after = "memoryMulEnd.txt".to_string();
|
let path_after = "memoryMulEnd.txt".to_string();
|
||||||
let memory_before = mem_cmp::MemChecker::from(&path_before);
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
||||||
let memory_after = mem_cmp::MemChecker::from(&path_after);
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
Machine::run(&mut m);
|
Machine::run(&mut m);
|
||||||
|
|
||||||
@ -808,11 +813,11 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ret() {
|
fn test_ret() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
let path_before = "memoryRet.txt".to_string();
|
let path_before = "memoryRet.txt".to_string();
|
||||||
let path_after = "memoryRetEnd.txt".to_string();
|
let path_after = "memoryRetEnd.txt".to_string();
|
||||||
let memory_before = mem_cmp::MemChecker::from(&path_before);
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
||||||
let memory_after = mem_cmp::MemChecker::from(&path_after);
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
Machine::run(&mut m);
|
Machine::run(&mut m);
|
||||||
|
|
||||||
@ -825,11 +830,11 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sub() {
|
fn test_sub() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
let path_before = "memorySub.txt".to_string();
|
let path_before = "memorySub.txt".to_string();
|
||||||
let path_after = "memorySubEnd.txt".to_string();
|
let path_after = "memorySubEnd.txt".to_string();
|
||||||
let memory_before = mem_cmp::MemChecker::from(&path_before);
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
||||||
let memory_after = mem_cmp::MemChecker::from(&path_after);
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
Machine::run(&mut m);
|
Machine::run(&mut m);
|
||||||
|
|
||||||
@ -842,11 +847,11 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_switch() {
|
fn test_switch() {
|
||||||
let mut m = Machine::_init_machine();
|
let mut m = Machine::init_machine();
|
||||||
let path_before = "memorySwitch.txt".to_string();
|
let path_before = "memorySwitch.txt".to_string();
|
||||||
let path_after = "memorySwitchEnd.txt".to_string();
|
let path_after = "memorySwitchEnd.txt".to_string();
|
||||||
let memory_before = mem_cmp::MemChecker::from(&path_before);
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
||||||
let memory_after = mem_cmp::MemChecker::from(&path_after);
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
Machine::run(&mut m);
|
Machine::run(&mut m);
|
||||||
|
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
use std::fs;
|
use std::{fs, io::{BufRead, BufReader, Lines, Error}};
|
||||||
use std::io::BufRead;
|
|
||||||
use std::io::BufReader;
|
|
||||||
use std::io::Lines;
|
|
||||||
use crate::Machine;
|
use crate::Machine;
|
||||||
|
|
||||||
const MEM_SIZE : usize = 4096;
|
const MEM_SIZE : usize = 4096;
|
||||||
@ -117,9 +114,9 @@ impl MemChecker{
|
|||||||
///
|
///
|
||||||
/// ### Return
|
/// ### Return
|
||||||
/// Mem-checker filled
|
/// Mem-checker filled
|
||||||
pub fn from(path: &String) -> MemChecker {
|
pub fn from(path: &str) -> Result<MemChecker, Error> {
|
||||||
|
|
||||||
let file = fs::File::open(path).expect("Wrong filename");
|
let file = fs::File::open(path)?;
|
||||||
|
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let mut lines = reader.lines();
|
let mut lines = reader.lines();
|
||||||
@ -155,7 +152,7 @@ impl MemChecker{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MemChecker{pc, sp, sections}
|
Ok(MemChecker{pc, sp, sections})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -311,8 +308,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_fill_memory(){
|
fn test_fill_memory(){
|
||||||
let path = "osef".to_string();
|
let path = "osef".to_string();
|
||||||
let m_c = MemChecker::from(&path);
|
let m_c = MemChecker::from(&path).unwrap();
|
||||||
let mut machine = Machine::_init_machine();
|
let mut machine = Machine::init_machine();
|
||||||
|
|
||||||
MemChecker::fill_memory_from_mem_checker(&m_c, &mut machine);
|
MemChecker::fill_memory_from_mem_checker(&m_c, &mut machine);
|
||||||
|
|
||||||
@ -334,7 +331,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_create_mem_checker(){
|
fn test_create_mem_checker(){
|
||||||
let path: String = "osef".to_string();
|
let path: String = "osef".to_string();
|
||||||
let m_c = MemChecker::from(&path);
|
let m_c = MemChecker::from(&path).unwrap();
|
||||||
MemChecker::print_mem_checker(&m_c);
|
MemChecker::print_mem_checker(&m_c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user