Skip to content

Commit

Permalink
Legacyterm refactor (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
sevonj authored Dec 29, 2023
1 parent 9b17902 commit 13d3d6c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 53 deletions.
32 changes: 32 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,33 @@ 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);

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

// Write to 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());
assert!(rx.try_recv().is_err());

// Try reading from it
assert!(crt.read_port(0).is_err());
assert!(crt.read_port(1).is_err());
assert!(crt.read_port(2).is_err());
assert!(rx.try_recv().is_err());

Ok(())
}
}
34 changes: 34 additions & 0 deletions src/emulator/devices/dev_kbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,37 @@ impl PMIO for DevKBD {
Err(()) // You can't write into the keyboard!
}
}

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

#[test]
fn test_dev_kbd() -> Result<(), ()> {
let mut kbd = DevKBD::default();
let (input_tx, input_rx) = std::sync::mpsc::channel();
let (requester_tx, requester_rx) = std::sync::mpsc::channel();
kbd.connect(input_rx, requester_tx);

// Test wrong usage
assert!(kbd.read_port(1).is_err());
assert!(kbd.read_port(2).is_err());
assert!(kbd.read_port(3).is_err());
assert!(kbd.write_port(0, 0).is_err());
assert!(kbd.write_port(1, 55).is_err());
assert!(kbd.write_port(2, -33).is_err());
assert!(requester_rx.try_recv().is_err());

// Test read correctly
// Because KBD device locks the program, we have to put it into another thread.
let cpu_thread = thread::spawn(move || {
return kbd.read_port(0);
});
input_tx.send(55).unwrap();
assert!(cpu_thread.join().unwrap() == Ok(55));
assert!(requester_rx.try_recv().is_ok());

Ok(())
}
}
53 changes: 0 additions & 53 deletions src/emulator/devices/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +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<(), ()> {
let mut cpu = CPU::new();

let (tx, rx) = std::sync::mpsc::channel();
let (tx_req, rx_req) = std::sync::mpsc::channel();

// Because KBD device locks the program, we have to put it into another thread.
let cpu_thread = thread::spawn(move || {
let mut bus = Bus::new();

bus.kbd.connect(rx, tx_req);
bus.write(0, 0x03400001); // IN R2, =1

cpu.tick(&mut bus);
cpu.debug_get_gpr(2)
});

// Send reply to the input request.
// Because of thread timing, we don't check if kbd has requested for input.
tx.send(55).unwrap();

if cpu_thread.join().unwrap() == 55 {
// Check if kbd ever sent the request.
if let Err(_) = rx_req.try_recv() {
return Err(());
}
return Ok(());
}
Err(())
}

/// PIC timer test.
#[test]
fn test_dev_pic_timer() -> Result<(), ()> {
Expand Down

0 comments on commit 13d3d6c

Please sign in to comment.