Skip to content

Commit

Permalink
DevCRT refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sevonj committed Dec 10, 2023
1 parent b0e5261 commit 5cc78aa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
28 changes: 28 additions & 0 deletions src/emulator/devices/dev_crt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//!
//! Legacy output device =crt
//! Communication happens via an mpsc channel, which could be refactored away.
//!
//!
use super::PMIO;
Expand All @@ -22,6 +23,7 @@ impl DevCRT {
}
}

/// Port 0: crt output
impl PMIO for DevCRT {
fn read_port(&mut self, _port: u8) -> Result<i32, ()> {
Err(()) // You can't read from the crt!
Expand All @@ -38,3 +40,29 @@ impl PMIO for DevCRT {
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_dev_crt() -> Result<(), ()> {
let mut crt = DevCRT::default();
let (tx, rx) = std::sync::mpsc::channel();
crt.connect(tx);

// Correct port.
crt.write_port(0, 55)?;
assert!(rx.try_recv().unwrap() == 55);

// Incorrect port.
assert!(crt.write_port(1, 55).is_err());
assert!(crt.write_port(2, 55).is_err());
assert!(crt.write_port(3, 55).is_err());

// Incorrect port: Nothing was sent.
assert!(rx.try_recv().is_err());

Ok(())
}
}
20 changes: 0 additions & 20 deletions src/emulator/devices/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,6 @@ use crate::emulator::{cpu::CPU, devices::PMIO};

use super::{dev_pic::DevPIC, Bus};

/// This test has the CPU output to CRT. The test listens if CRT sends the value via the channel.
#[test]
fn test_dev_crt() -> Result<(), ()> {
let mut cpu = CPU::new();
let mut bus = Bus::new();

let (tx, rx) = std::sync::mpsc::channel();
bus.crt.connect(tx);

bus.write(0, 0x02200037)?; // LOAD R1, =55
bus.write(1, 0x04200000)?; // OUT R1, =0
cpu.tick(&mut bus);
cpu.tick(&mut bus);

if rx.try_recv().unwrap() == 55 {
return Ok(());
}
Err(())
}

///Test KBD. The test listens for input request, sends a value and checks if it gets loaded.
#[test]
fn test_dev_kbd() -> Result<(), ()> {
Expand Down

0 comments on commit 5cc78aa

Please sign in to comment.