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
|
use crate::*;
pub struct HeadlessEmulator {
pub br: BedrockEmulator<HeadlessDeviceBus>,
pub debug: DebugState,
}
impl HeadlessEmulator {
pub fn new(config: &EmulatorConfig, debug: bool) -> Self {
Self {
br: BedrockEmulator::new(HeadlessDeviceBus::new(config)),
debug: DebugState::new(debug, config.symbols_path.as_ref()),
}
}
pub fn load_program(&mut self, bytecode: &[u8]) {
self.br.core.mem.load_program(bytecode);
}
fn sleep(&mut self) {
loop {
if self.br.dev.wake() { break; }
std::thread::sleep(TICK_DURATION);
}
}
fn halt(&mut self) {
self.br.dev.stream.flush();
info!("Program halted, exiting.");
self.debug.debug_full(&self.br.core);
std::process::exit(0);
}
pub fn run(&mut self) -> ! {
loop {
if let Some(signal) = self.br.evaluate(BATCH_SIZE, self.debug.enabled) {
match signal {
Signal::Fork | Signal::Reset => self.br.reset(),
Signal::Sleep => self.sleep(),
Signal::Halt => self.halt(),
Signal::Debug(debug_signal) => match debug_signal {
Debug::Debug1 => self.debug.debug_full(&self.br.core),
Debug::Debug2 => self.debug.debug_timing(&self.br.core),
_ => (),
}
_ => (),
}
}
}
}
}
pub struct HeadlessDeviceBus {
pub system: SystemDevice,
pub memory: MemoryDevice,
pub math: MathDevice,
pub clock: ClockDevice,
pub stream: StreamDevice,
pub file: FileDevice,
pub wake_queue: WakeQueue,
}
impl HeadlessDeviceBus {
pub fn new(config: &EmulatorConfig) -> Self {
Self {
system: SystemDevice::new(0b1111_0000_1100_0000),
memory: MemoryDevice::new(),
math: MathDevice::new(),
clock: ClockDevice::new(),
stream: StreamDevice::new(&config),
file: FileDevice::new(),
wake_queue: WakeQueue::new(),
}
}
}
impl DeviceBus for HeadlessDeviceBus {
fn get_device(&mut self, slot: u8) -> Option<&mut dyn Device> {
match slot {
0x0 => Some(&mut self.system),
0x1 => Some(&mut self.memory),
0x2 => Some(&mut self.math ),
0x3 => Some(&mut self.clock ),
0x8 => Some(&mut self.stream),
0x9 => Some(&mut self.file ),
_ => None
}
}
fn wake(&mut self) -> bool {
for slot in self.wake_queue.iter(self.system.wake_mask) {
if let Some(device) = self.get_device(slot) {
if device.wake() {
self.system.wake_slot = slot;
self.system.asleep = false;
self.wake_queue.wake(slot);
return true;
}
}
}
return false;
}
}
|