-
Notifications
You must be signed in to change notification settings - Fork 5
/
cpuville.rs
147 lines (126 loc) · 3.73 KB
/
cpuville.rs
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
/*
http://cpuville.com/Code/CPM-on-a-new-computer.html
http://cpuville.com/Code/Tiny-BASIC.html
*/
use std::io::{Write, stdin, stdout};
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use std::sync::mpsc::TryRecvError;
use std::thread;
use std::time::Duration;
use iz80::Cpu;
use iz80::Machine;
use iz80::TimedRunner;
static TINY_BASIC: &[u8] = include_bytes!("rom/tinybasic2dms.bin");
const MHZ: f64 = 4.0;
fn main() {
let mut machine = VilleMachine::new();
let mut cpu = Cpu::new();
let mut timed_runner = TimedRunner::default();
timed_runner.set_mhz(&cpu, MHZ, 1000);
// Init console
let mut stdout = stdout();
let stdin_channel = spawn_stdin_channel();
let mut in_char_waiting = false;
// Load program
let code = TINY_BASIC;
for (i, e) in code.iter().enumerate() {
machine.poke(i as u16, *e);
}
// Init
cpu.registers().set_pc(0x0000);
machine.in_values[3] = 1; // TX Ready
loop {
timed_runner.execute(&mut cpu, &mut machine);
if let Some(port) = machine.out_port {
match port {
2 => {
print!("{}", machine.out_value as char);
stdout.flush().unwrap();
},
3 => {},
_ => panic!("BDOS command not implemented")
}
machine.out_port = None;
}
if let Some(port) = machine.in_port {
match port {
2 => {
in_char_waiting = false;
},
3 => {},
_ => panic!("BDOS command not implemented")
}
machine.in_port = None;
// Avoid 100% CPU usage waiting for input.
if MHZ == 0.0 {
thread::sleep(Duration::from_millis(1));
}
}
if !in_char_waiting {
// Let's get another char if available
match stdin_channel.try_recv() {
Ok(key) => {
machine.in_values[2] = key;
in_char_waiting = true;
machine.in_values[3] = 3; // RX Ready
},
Err(TryRecvError::Empty) => {
machine.in_values[3] = 1; // RX Not ready
},
Err(TryRecvError::Disconnected) => {},
}
}
}
}
fn spawn_stdin_channel() -> Receiver<u8> {
let (tx, rx) = mpsc::channel::<u8>();
thread::spawn(move || loop {
let mut buffer = String::new();
stdin().read_line(&mut buffer).unwrap();
for mut c in buffer.bytes() {
if c == 10 {c = 13};
tx.send(c).unwrap();
}
});
rx
}
struct VilleMachine {
mem: [u8; 65536],
in_values: [u8; 256],
in_port: Option<u8>,
out_port: Option<u8>,
out_value: u8
}
impl VilleMachine {
pub fn new() -> VilleMachine {
VilleMachine {
mem: [0; 65536],
in_values: [0; 256],
out_port: None,
out_value: 0,
in_port: None
}
}
}
impl Machine for VilleMachine {
fn peek(&self, address: u16) -> u8 {
self.mem[address as usize]
}
fn poke(&mut self, address: u16, value: u8) {
self.mem[address as usize] = value;
}
fn port_in(&mut self, address: u16) -> u8 {
let value = self.in_values[address as u8 as usize];
if value != 1 {
//print!("Port {:04x} in {:02x}\n", address, value);
}
self.in_port = Some(address as u8);
value
}
fn port_out(&mut self, address: u16, value: u8) {
//print!("Port {:04x} out {:02x} {}\n", address, value, value as char);
self.out_port = Some(address as u8);
self.out_value = value;
}
}