summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 05b32c2ba2ead9b37ad2383a4ba7fba788c72144 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
extern crate clap;

use std::fs;
use std::io;
use std::io::prelude::*;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread;
use std::time::Duration;

mod core;
mod rom;
mod memory;

use self::core::Core;
use self::rom::Rom;

enum Control {
    Run(bool),
}

enum SimData {}

struct BoundController {
    tx: Sender<Control>,
    _rx: Receiver<SimData>,
}

struct BoundCore {
    _tx: Sender<SimData>,
    rx: Receiver<Control>,
}

fn new_bound_pair() -> (BoundCore, BoundController) {
    let (tx_c, rx_c): (Sender<Control>, Receiver<Control>) = channel();
    let (tx_d, rx_d): (Sender<SimData>, Receiver<SimData>) = channel();
    (
        BoundCore {
            _tx: tx_d,
            rx: rx_c,
        },
        BoundController {
            tx: tx_c,
            _rx: rx_d,
        },
    )
}

fn cli_controller(bound: BoundController) {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        match line.as_ref() {
            "stop" => {
                println!("stop");
                bound.tx.send(Control::Run(false)).unwrap();
            }
            "start" => {
                println!("start");
                bound.tx.send(Control::Run(true)).unwrap();
            }
            _ => (),
        }
    }
}

fn ws_controller(_bound: BoundController) {
    // TODO implement
    println!("AAA");
}

fn new_controller(name: &str) -> Result<&dyn Fn(BoundController), i32> {
    match name {
        "cli" => Ok(&cli_controller),
        "ws" => Ok(&ws_controller),
        _ => Err(1),
    }
}

fn core_worker(mut core: Core, bound: BoundCore) {
    let mut should_stop = false;
    loop {
        if should_stop {
            if let Ok(msg) = bound.rx.recv() {
                match msg {
                    Control::Run(run) => should_stop = !run,
                }
            }
        } else {
            println!("0x{:08x}: {}", core.pc(), core.op());
            core.step();
            if let Ok(msg) = bound.rx.recv_timeout(Duration::from_millis(1000)) {
                match msg {
                    Control::Run(run) => should_stop = !run,
                }
            }
        }
    }
}

fn main() {
    /* Parse args*/

    let cli_args_matches = clap::App::new("x51emu")
        .version("0.1.0")
        .author("oxore")
        .about("MCS51 emulator")
        .arg(
            clap::Arg::with_name("file")
                .short("f")
                .long("file")
                .value_name("FILE")
                .help("Sets a file with program to load")
                .takes_value(true)
                .required(true),
        )
        .arg(
            clap::Arg::with_name("interface")
                .short("i")
                .long("interface")
                .value_name("INTERFACE")
                .help("Sets a controlling and I/O interface")
                .takes_value(true)
                .default_value("cli")
                .possible_values(&["cli", "ws"]),
        )
        .get_matches();

    let filename = cli_args_matches
        .value_of("file")
        .expect("No file name provided");

    /* Read program and instantiate a simulator */

    let data =
        fs::read_to_string(filename).unwrap_or_else(|_| panic!("Unable to read file {}", filename));

    let core = Core::new().rom(match Rom::from_hex(data) {
        Ok(value) => value,
        Err(err_string) => {
            println!("{}", err_string);
            return;
        }
    });

    /* Run core worker and controller */

    let (bound_core, bound_controller) = new_bound_pair();

    core_worker(core, bound_core);

    thread::spawn(move || {
        let controller = {
            let controller_type_name = cli_args_matches
                .value_of("interface")
                .expect("No interface type specified");
            new_controller(controller_type_name)
                .unwrap_or_else(|_| panic!("Unknown controller type \"{}\"", controller_type_name))
        };

        controller(bound_controller);
    });
}