-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogramstate.rs
64 lines (56 loc) · 1.78 KB
/
programstate.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
// TODO: Get rid of this file and implement all this functionality in a nicer way
// Alternative TODO: Never clean this up, but make sure to do things better next time you write an emulator
// Not sure where the most appropriate place for this code is,
// so it is separated it into its own file for now
use utils::*;
#[derive(Debug)]
// Is it worth it to just use a bit array?
pub struct ProgramState {
pub debug: bool,
pub done: bool,
pub paused: bool,
pub adv_frame: bool,
pub debug_regs: bool,
pub speed: u64,
}
impl ProgramState {
// TODO: Replace parameterless new() with Default trait
pub fn new() -> ProgramState {
ProgramState {
debug: false,
done: false,
paused: false,
adv_frame: false,
debug_regs: false,
speed: 1
}
}
}
#[derive(Debug)]
pub struct DebugState {
pub buffer: String,
pub cursor: usize,
pub num_lines: usize,
}
impl DebugState {
pub fn new() -> DebugState {
DebugState {
buffer: String::new(),
cursor: 0,
num_lines: 0,
}
}
// TODO: Make faster. Maybe replace buffer with array and keep track of "top" of buffer
pub fn add_text(&mut self, text: &str, num_lines: usize) {
if self.num_lines + num_lines > MAX_DEBUG_BUFFER_SIZE {
let extra = self.num_lines + num_lines - MAX_DEBUG_BUFFER_SIZE;
let extra_lines = self.buffer.match_indices('\n').skip(extra).next().unwrap().0;
self.buffer = self.buffer.split_off(extra_lines);
self.num_lines -= extra;
self.cursor = max(0, self.cursor - extra);
}
self.buffer += text;
self.cursor += if self.cursor == self.num_lines {num_lines} else {0};
self.num_lines += num_lines;
}
}