From 337b1ab05b177208736cc0e953f8ab5e20d8d092 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sat, 11 Jan 2025 17:06:59 +0200 Subject: [PATCH] reafactor code: split tui bin into separated crated --- .idea/Chip8Emulator.iml | 3 ++- Cargo.lock | 28 +++++++++++++-------- Cargo.toml | 5 ++-- README.md | 2 +- {emulator => chip8_core}/Cargo.toml | 8 ------ chip8_core/src/display.rs | 13 ++++++++++ {emulator => chip8_core}/src/emulator.rs | 6 ++--- chip8_core/src/input.rs | 16 ++++++++++++ {emulator => chip8_core}/src/instruction.rs | 0 chip8_core/src/lib.rs | 6 +++++ chip8_core/src/sound.rs | 5 ++++ {emulator => chip8_core}/src/stack.rs | 0 chip8_tui/Cargo.toml | 16 ++++++++++++ {emulator => chip8_tui}/src/display.rs | 17 +++---------- {emulator => chip8_tui}/src/input.rs | 18 +------------ {emulator => chip8_tui}/src/main.rs | 15 +++++------ {emulator => chip8_tui}/src/sound.rs | 6 +---- 17 files changed, 94 insertions(+), 70 deletions(-) rename {emulator => chip8_core}/Cargo.toml (56%) create mode 100644 chip8_core/src/display.rs rename {emulator => chip8_core}/src/emulator.rs (99%) create mode 100644 chip8_core/src/input.rs rename {emulator => chip8_core}/src/instruction.rs (100%) create mode 100644 chip8_core/src/lib.rs create mode 100644 chip8_core/src/sound.rs rename {emulator => chip8_core}/src/stack.rs (100%) create mode 100644 chip8_tui/Cargo.toml rename {emulator => chip8_tui}/src/display.rs (85%) rename {emulator => chip8_tui}/src/input.rs (84%) rename {emulator => chip8_tui}/src/main.rs (86%) rename {emulator => chip8_tui}/src/sound.rs (57%) diff --git a/.idea/Chip8Emulator.iml b/.idea/Chip8Emulator.iml index f8a19b8..e5ed26c 100644 --- a/.idea/Chip8Emulator.iml +++ b/.idea/Chip8Emulator.iml @@ -2,7 +2,8 @@ - + + diff --git a/Cargo.lock b/Cargo.lock index 374bd03..202d2a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -111,11 +111,23 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chip8_tui" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "crossterm", + "emulator", + "env_logger", + "ratatui", +] + [[package]] name = "clap" -version = "4.5.23" +version = "4.5.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" +checksum = "a8eb5e908ef3a6efbe1ed62520fb7287959888c88485abe072543190ecc66783" dependencies = [ "clap_builder", "clap_derive", @@ -123,9 +135,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.23" +version = "4.5.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" +checksum = "96b01801b5fc6a0a232407abc821660c9c6d25a1cafc0d4f85f29fb8d9afc121" dependencies = [ "anstream", "anstyle", @@ -135,9 +147,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.18" +version = "4.5.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +checksum = "54b755194d6389280185988721fffba69495eed5ee9feeee9a599b53db80318c" dependencies = [ "heck", "proc-macro2", @@ -248,13 +260,9 @@ name = "emulator" version = "0.1.0" dependencies = [ "anyhow", - "clap", - "crossterm", - "env_logger", "log", "pretty_assertions", "rand", - "ratatui", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 3c375d7..ea6c4a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,6 @@ resolver = "2" members = [ - "emulator", -] \ No newline at end of file + "chip8_tui", + "chip8_core", +] diff --git a/README.md b/README.md index 6d75390..f9ffc2e 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ The project is written in Rust and it's organized in the following modules: │   └── ibm-logo.ch8 # IBM Logo Test ROM ├── src │   ├── display.rs # The screen / display module. -│   ├── emulator.rs # The emulator logic which emulates the CPU. +│   ├── chip8_core.rs # The chip8_core logic which emulates the CPU. │   ├── input.rs # The input logic. │   ├── instruction.rs # The instruction decoding logic. │   ├── main.rs # The main file. This is the entrypoint. diff --git a/emulator/Cargo.toml b/chip8_core/Cargo.toml similarity index 56% rename from emulator/Cargo.toml rename to chip8_core/Cargo.toml index 0bb5a93..78f413d 100644 --- a/emulator/Cargo.toml +++ b/chip8_core/Cargo.toml @@ -6,18 +6,10 @@ edition = "2021" [lints.rust] dead_code = "allow" -[profile.release] -lto = true -codegen-units = 1 - [dependencies] log = "0.4.22" -env_logger = "0.11.5" anyhow = "1.0.93" -ratatui = "0.29.0" -crossterm = "0.28.1" rand = "0.8.5" -clap = { version = "4.5.23", features = ["derive"] } [dev-dependencies] pretty_assertions = "1.4.1" diff --git a/chip8_core/src/display.rs b/chip8_core/src/display.rs new file mode 100644 index 0000000..5202e9c --- /dev/null +++ b/chip8_core/src/display.rs @@ -0,0 +1,13 @@ +/// Represents the display's width in pixels. +pub const DISPLAY_WIDTH: usize = 64; + +/// Represents the display's height pixels. +pub const DISPLAY_HEIGHT: usize = 32; + +/// Display trait +pub trait Display { + /// Re-draws the display. + fn clear(&mut self); + /// Renders the display data on screen. + fn render(&mut self, display_data: &[bool; DISPLAY_WIDTH * DISPLAY_HEIGHT]); +} diff --git a/emulator/src/emulator.rs b/chip8_core/src/emulator.rs similarity index 99% rename from emulator/src/emulator.rs rename to chip8_core/src/emulator.rs index 2a1f931..c0dc75b 100644 --- a/emulator/src/emulator.rs +++ b/chip8_core/src/emulator.rs @@ -42,7 +42,7 @@ where S: SoundModule, I: InputModule, { - /// Memory represents the emulator's memory. + /// Memory represents the chip8_core's memory. memory: [u8; MEMORY_SIZE], /// Registers holds the general purpose registers. registers: [u8; NUMBER_OF_REGISTERS], @@ -63,7 +63,7 @@ where sound_module: S, /// The module responsible for receiving user input. input_module: I, - /// The stack of the emulator. + /// The stack of the chip8_core. stack: Stack, /// Holds the display data, each bit corresponds to a pixel. display_data: [bool; DISPLAY_WIDTH * DISPLAY_HEIGHT], @@ -467,7 +467,7 @@ where ])) } - /// Loads the ROM found at the rom path in the emulator's RAM memory. + /// Loads the ROM found at the rom path in the chip8_core's RAM memory. fn load_rom(&mut self, mut rom: T) -> Result<(), anyhow::Error> where T: Read, diff --git a/chip8_core/src/input.rs b/chip8_core/src/input.rs new file mode 100644 index 0000000..3712ce5 --- /dev/null +++ b/chip8_core/src/input.rs @@ -0,0 +1,16 @@ +/// InputModule retrieves the keys from the hardware or software input control module. +pub trait InputModule { + /// Returns the key value of the corresponding pressed key. + /// None if no key is pressed. + fn get_key_pressed(&mut self) -> Option; +} + +/// NoInput always returns none when queried for input. +#[derive(Clone)] +pub struct NoInput; + +impl InputModule for NoInput { + fn get_key_pressed(&mut self) -> Option { + None + } +} diff --git a/emulator/src/instruction.rs b/chip8_core/src/instruction.rs similarity index 100% rename from emulator/src/instruction.rs rename to chip8_core/src/instruction.rs diff --git a/chip8_core/src/lib.rs b/chip8_core/src/lib.rs new file mode 100644 index 0000000..78dd143 --- /dev/null +++ b/chip8_core/src/lib.rs @@ -0,0 +1,6 @@ +pub mod display; +pub mod emulator; +pub mod input; +pub mod instruction; +pub mod sound; +pub mod stack; diff --git a/chip8_core/src/sound.rs b/chip8_core/src/sound.rs new file mode 100644 index 0000000..35a0b5f --- /dev/null +++ b/chip8_core/src/sound.rs @@ -0,0 +1,5 @@ +/// SoundModule represents a module which can produce sound. +pub trait SoundModule { + /// beep makes a beep sound. + fn beep(&mut self); +} diff --git a/emulator/src/stack.rs b/chip8_core/src/stack.rs similarity index 100% rename from emulator/src/stack.rs rename to chip8_core/src/stack.rs diff --git a/chip8_tui/Cargo.toml b/chip8_tui/Cargo.toml new file mode 100644 index 0000000..0c3f6f8 --- /dev/null +++ b/chip8_tui/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "chip8_tui" +version = "0.1.0" +edition = "2021" + +[profile.release] +lto = true +codegen-units = 1 + +[dependencies] +emulator = { path = "../chip8_core" } +clap = { version = "4.5.26", features = ["derive"] } +anyhow = "1.0.95" +env_logger = "0.11.6" +ratatui = "0.29.0" +crossterm = "0.28.1" \ No newline at end of file diff --git a/emulator/src/display.rs b/chip8_tui/src/display.rs similarity index 85% rename from emulator/src/display.rs rename to chip8_tui/src/display.rs index bf20af4..038e7a9 100644 --- a/emulator/src/display.rs +++ b/chip8_tui/src/display.rs @@ -3,23 +3,12 @@ use ratatui::style::{Style, Stylize}; use ratatui::widgets::{Block, Borders}; use ratatui::DefaultTerminal; -/// Represents the display's width in pixels. -pub const DISPLAY_WIDTH: usize = 64; +use emulator::display::{Display, DISPLAY_HEIGHT, DISPLAY_WIDTH}; -/// Represents the display's height pixels. -pub const DISPLAY_HEIGHT: usize = 32; - -/// Display trait -pub trait Display { - /// Re-draws the display. - fn clear(&mut self); - /// Renders the display data on screen. - fn render(&mut self, display_data: &[bool; DISPLAY_WIDTH * DISPLAY_HEIGHT]); -} - -/// Simple terminal display for the Chip8's emulator. +/// Simple terminal display for the Chip8's chip8_core. pub struct TerminalDisplay {} +#[allow(dead_code)] impl TerminalDisplay { pub fn new() -> TerminalDisplay { TerminalDisplay {} diff --git a/emulator/src/input.rs b/chip8_tui/src/input.rs similarity index 84% rename from emulator/src/input.rs rename to chip8_tui/src/input.rs index 6a0c7df..f124355 100644 --- a/emulator/src/input.rs +++ b/chip8_tui/src/input.rs @@ -1,24 +1,8 @@ use crossterm::event::{poll, read, Event, KeyCode}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; +use emulator::input::InputModule; use std::time::Duration; -/// InputModule retrieves the keys from the hardware or software input control module. -pub trait InputModule { - /// Returns the key value of the corresponding pressed key. - /// None if no key is pressed. - fn get_key_pressed(&mut self) -> Option; -} - -/// NoInput always returns none when queried for input. -#[derive(Clone)] -pub struct NoInput; - -impl InputModule for NoInput { - fn get_key_pressed(&mut self) -> Option { - None - } -} - /// CrossTermInput implements input events via the crossterm crate. #[derive(Clone)] pub struct CrossTermInput { diff --git a/emulator/src/main.rs b/chip8_tui/src/main.rs similarity index 86% rename from emulator/src/main.rs rename to chip8_tui/src/main.rs index c22ff89..18b310b 100644 --- a/emulator/src/main.rs +++ b/chip8_tui/src/main.rs @@ -1,21 +1,18 @@ +mod display; +mod input; +mod sound; + use crate::display::RatatuiDisplay; -use crate::emulator::Emulator; use crate::input::CrossTermInput; use crate::sound::TerminalSound; use clap::Parser; +use emulator::emulator::Emulator; use std::fs::File; -mod display; -mod emulator; -mod input; -mod instruction; -mod sound; -mod stack; - #[derive(Parser, Debug)] #[command( version = "1.0", - about = "A Chip8 emulator.", + about = "A Chip8 chip8_core.", long_about = "A program which emulates the Chip8 system." )] struct CliArgs { diff --git a/emulator/src/sound.rs b/chip8_tui/src/sound.rs similarity index 57% rename from emulator/src/sound.rs rename to chip8_tui/src/sound.rs index d41b617..49514d1 100644 --- a/emulator/src/sound.rs +++ b/chip8_tui/src/sound.rs @@ -1,8 +1,4 @@ -/// SoundModule represents a module which can produce sound. -pub trait SoundModule { - /// beep makes a beep sound. - fn beep(&mut self); -} +use emulator::sound::SoundModule; /// TerminalSound is a simple module that makes terminal beep sound. pub struct TerminalSound;