diff --git a/src/emulator.rs b/src/emulator.rs index 82c2696..6e04525 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -3,7 +3,7 @@ use log::{debug, info}; use std::fs::File; use std::io::Read; use std::path::{Path}; -use crate::display::{Display, TerminalDisplay}; +use crate::display::{Display}; const MEMORY_SIZE: usize = 4096; const NUMBER_OF_REGISTERS: usize = 16; @@ -27,7 +27,7 @@ const FONT_SPRITES: [u8; 80] = [ ]; /// Emulator emulates the Chip8 CPU. -pub struct Emulator { +pub struct Emulator where D: Display { /// Memory represents the emulator's memory. memory: [u8; MEMORY_SIZE], /// Registers holds the general purpose registers. @@ -44,13 +44,13 @@ pub struct Emulator { /// The stack pointer register. stack_pointer: u8, /// The display_data holds all the data associated with the display - display: Box + display: D } -impl Emulator { +impl Emulator where D: Display { /// Creates a new `Emulator` instance. /// - pub fn new(display: Box) -> Emulator { + pub fn new(display: D) -> Emulator { let mut emulator = Emulator { memory: [0; MEMORY_SIZE], registers: [0; NUMBER_OF_REGISTERS], @@ -113,11 +113,12 @@ impl Emulator { #[cfg(test)] mod tests { use super::*; + use crate::display::{TerminalDisplay}; use pretty_assertions::assert_eq; #[test] fn test_load_font_data() { - let emulator = Emulator::new(Box::from(TerminalDisplay::new())); + let emulator = Emulator::new(TerminalDisplay::new()); assert_eq!(emulator.memory[0xf0..0xf0 + 80], FONT_SPRITES) } @@ -130,7 +131,7 @@ mod tests { .expect("Failed to read test ROM"); // Test - let mut emulator = Emulator::new(Box::from(TerminalDisplay::new())); + let mut emulator = Emulator::new(TerminalDisplay::new()); emulator .load_rom("roms/ibm-logo.ch8") .expect("failed to load ROM"); diff --git a/src/main.rs b/src/main.rs index f8288ed..fc617f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ mod display; fn main() -> Result<(), anyhow::Error> { env_logger::init(); - let mut emulator = Emulator::new(Box::from(TerminalDisplay::new())); + let mut emulator = Emulator::new(TerminalDisplay::new()); emulator.emulate(String::from("./roms/ibm-logo.ch8"))?;