Skip to content

Commit

Permalink
Refactor DevKBD
Browse files Browse the repository at this point in the history
  • Loading branch information
sevonj committed Dec 10, 2023
1 parent f1f2fc3 commit 6787a8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
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(())
}
}
33 changes: 0 additions & 33 deletions src/emulator/devices/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,6 @@ use crate::emulator::{cpu::CPU, devices::PMIO};

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

///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 6787a8f

Please sign in to comment.