aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 15b4fbae64571eb7cabbcae79ad4e9da2ee1d23a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
extern crate docopt;
extern crate rustc_serialize;
extern crate rustv;

use std::fs::File;
use std::io::Read;
use std::rc::Rc;
use std::cell::RefCell;

use docopt::Docopt;

use rustv::elfloader;
use rustv::isa;
use rustv::memory;
use rustv::simulator;

const USAGE: &'static str = "
cacheracer - A F/OSS implementation of the CS 3410 CacheRacer

Usage:
  cacheracer <program1> <program2>

Options:
  -h --help    Show this screen.
  --version    Show version.
";

#[derive(Debug, RustcDecodable)]
struct Args {
    arg_program1: String,
    arg_program2: String,
}

fn load_program<T: memory::Mmu>(memory: &mut memory::Memory,
                                mmu: &T, path: &str) -> isa::Address {
    let mut f = File::open(path).unwrap();
    let mut buffer = Vec::new();

    f.read_to_end(&mut buffer).unwrap();

    let elf = elfloader::ElfBinary::new("test", &buffer).unwrap();
    let start = elf.file_header().entry as isa::Address;

    let mut text = None;
    let mut data = None;
    for p in elf.section_headers() {
        if p.name.0 == 0x1b {
            text = Some((elf.section_data(p), p.addr));
        }
        else if p.name.0 == 0x33 {
            data = Some((elf.section_data(p), p.addr));
        }
    }

    let (text, text_offset) = text.unwrap();
    let (data, data_offset) = data.unwrap();

    memory.write_segment(mmu, text, text_offset as usize);
    memory.write_segment(mmu, data, data_offset as usize);

    start
}

fn main() {
    let args: Args = Docopt::new(USAGE)
        .and_then(|d| d.decode())
        .unwrap_or_else(|e| e.exit());

    let mmu = memory::IdentityMmu::new();
    let mmu2 = memory::ReverseMmu::new(0x8000);
    let mut memory = memory::Memory::new(0x10000);

    let start1 = load_program(&mut memory, &mmu, &args.arg_program1);
    let start2 = load_program(&mut memory, &mmu2, &args.arg_program2);

    let memory_box = Box::new(memory) as Box<memory::MemoryInterface>;
    let memory_ref = Rc::new(RefCell::new(memory_box));
    let cache = memory::DirectMappedCache::new(4, 4, memory_ref.clone());
    let cache_box = Box::new(cache) as Box<memory::MemoryInterface>;
    let cache_ref = Rc::new(RefCell::new(cache_box));
    let core = simulator::Core::new(
        start1, 0x1000,
        cache_ref.clone(), Box::new(mmu));
    let core2 = simulator::Core::new(
        start2, 0x1000,
        cache_ref.clone(), Box::new(mmu2));
    let cores = vec![core, core2];
    let mut simulator = simulator::Simulator::new(cores, memory_ref.clone());
    simulator.run();
}