Skip to content

Commit 1dfa0e5

Browse files
committed
refactor
1 parent 065e555 commit 1dfa0e5

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/run.rs renamed to src/instructions.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,27 +242,27 @@ impl C8Kernel {
242242
}
243243
}
244244

245-
pub enum HaltSignal {
245+
pub enum HaltSig {
246246
Halt,
247247
}
248-
pub enum InputSignal {
248+
pub enum InputSig {
249249
Exit,
250250
}
251-
pub enum ExitKind {
251+
pub enum ExitType {
252252
Forced,
253253
Natural,
254254
}
255255

256256
pub struct ChannelHandler {
257-
timer_tx: Sender<HaltSignal>,
258-
input_tx: Sender<HaltSignal>,
259-
input_rx: Receiver<InputSignal>,
257+
timer_tx: Sender<HaltSig>,
258+
input_tx: Sender<HaltSig>,
259+
input_rx: Receiver<InputSig>,
260260
}
261261

262262
impl ChannelHandler {
263263
pub fn halt(&self) -> CustomResult<()> {
264264
for tx in &[&self.input_tx, &self.timer_tx] {
265-
tx.send(HaltSignal::Halt).c8_err("Could not send message to thread")?;
265+
tx.send(HaltSig::Halt).c8_err("Could not send message to thread")?;
266266
}
267267
Ok(())
268268
}
@@ -277,7 +277,7 @@ impl C8Kernel {
277277
..Default::default()
278278
})
279279
}
280-
pub fn run_from_args(args: Args) -> CustomResult<ExitKind> {
280+
pub fn run_from_args(args: Args) -> CustomResult<ExitType> {
281281
let executor = if args.hex { Self::run_hex } else { Self::run_binary };
282282
let mut buf = vec![];
283283
File::open(args.file)
@@ -286,7 +286,7 @@ impl C8Kernel {
286286
.c8_err("Error occurred trying to read the file")?;
287287
executor(&buf)
288288
}
289-
pub fn run_hex(rom: &[u8]) -> CustomResult<ExitKind> {
289+
pub fn run_hex(rom: &[u8]) -> CustomResult<ExitType> {
290290
let mut bin = vec![];
291291
let hex = |c: u8| {
292292
match c {
@@ -307,7 +307,7 @@ impl C8Kernel {
307307
let mut machine = Self::new(rom)?;
308308
machine.run()
309309
}
310-
pub fn run_binary(rom: &[u8]) -> CustomResult<ExitKind> {
310+
pub fn run_binary(rom: &[u8]) -> CustomResult<ExitType> {
311311
let mut machine = Self::new(rom)?;
312312
machine.run()
313313
}
@@ -349,14 +349,14 @@ impl C8Kernel {
349349
input_rx: display_rx,
350350
})
351351
}
352-
pub fn spawn_timer(&self) -> CustomResult<Sender<HaltSignal>> {
352+
pub fn spawn_timer(&self) -> CustomResult<Sender<HaltSig>> {
353353
let dt = Arc::clone(&self.timers.dt);
354354
let st = Arc::clone(&self.timers.st);
355355
let (timer_tx, timer_rx) = mpsc::channel();
356356
thread::spawn(move || {
357357
loop {
358358
match timer_rx.try_recv() {
359-
Ok(HaltSignal::Halt) | Err(TryRecvError::Disconnected) => {
359+
Ok(HaltSig::Halt) | Err(TryRecvError::Disconnected) => {
360360
break;
361361
}
362362
Err(TryRecvError::Empty) => (),
@@ -374,15 +374,15 @@ impl C8Kernel {
374374
});
375375
Ok(timer_tx)
376376
}
377-
pub fn spawn_display(&self) -> CustomResult<(Receiver<InputSignal>, Sender<HaltSignal>)> {
377+
pub fn spawn_display(&self) -> CustomResult<(Receiver<InputSig>, Sender<HaltSig>)> {
378378
let input = Arc::clone(&self.input.bits);
379379
let (display_tx, display_rx) = mpsc::channel();
380380
let (input_tx, input_rx) = mpsc::channel();
381381
thread::spawn(move || {
382382
loop {
383383
let mut starts: [Option<Instant>; 16] = [None; 16];
384384
match input_rx.try_recv() {
385-
Ok(HaltSignal::Halt) | Err(TryRecvError::Disconnected) => {
385+
Ok(HaltSig::Halt) | Err(TryRecvError::Disconnected) => {
386386
break;
387387
}
388388
Err(TryRecvError::Empty) => (),
@@ -413,7 +413,7 @@ impl C8Kernel {
413413
} else {
414414
match k.code {
415415
KeyCode::Char('c') => {
416-
display_tx.send(InputSignal::Exit).unwrap();
416+
display_tx.send(InputSig::Exit).unwrap();
417417
// prevents race conditions as the thread can be killed at any point after this line
418418
thread::sleep(Duration::from_secs(300));
419419
}
@@ -431,23 +431,23 @@ impl C8Kernel {
431431
});
432432
Ok((display_rx, input_tx))
433433
}
434-
pub fn run(&mut self) -> CustomResult<ExitKind> {
434+
pub fn run(&mut self) -> CustomResult<ExitType> {
435435
self.setup_terminal()?;
436436
let channels = self.spawn_extra_threads()?;
437437
self.buf.clear();
438438
loop {
439439
match channels.input_rx.try_recv() {
440-
Ok(InputSignal::Exit) => {
440+
Ok(InputSig::Exit) => {
441441
self.teardown_terminal()?;
442442
channels.halt()?;
443-
return Ok(ExitKind::Forced);
443+
return Ok(ExitType::Forced);
444444
}
445445
_ => (),
446446
}
447447
if self.pc >= (MEM_SIZE as u16) {
448448
self.teardown_terminal()?;
449449
channels.halt()?;
450-
return Ok(ExitKind::Natural);
450+
return Ok(ExitType::Natural);
451451
}
452452
let high = *self.mem.get(self.pc);
453453
let low = *self.mem.get(self.pc + 1);

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
mod utils;
22
mod memory;
33
mod cpu;
4-
mod run;
4+
mod instructions;
55

66
use cpu::C8Kernel;
7-
use run::{ Args, ExitKind };
7+
use instructions::{ Args, ExitType };
88
use std::process::exit;
99

1010
fn main() {
1111
let args: Args = argh::from_env();
1212
match C8Kernel::run_from_args(args) {
1313
Ok(kind) =>
1414
match kind {
15-
ExitKind::Natural => {
15+
ExitType::Natural => {
1616
exit(0);
1717
}
18-
ExitKind::Forced => {
18+
ExitType::Forced => {
1919
exit(130);
2020
}
2121
}

0 commit comments

Comments
 (0)