From 778274b5aec5ea086614f68c7d0048c872568c57 Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Sun, 23 Feb 2020 00:19:55 +0200 Subject: [PATCH] refactor: split "plat" binaries into separate crates Former-commit-id: 73554b0128e036279f7ac895acb57d8f89cb1c96 --- Cargo.toml | 26 +++-------- README.md | 2 +- rustboyadvance-minifb/Cargo.toml | 11 +++++ .../src}/cli.yml | 0 .../src}/main.rs | 0 rustboyadvance-sdl2/Cargo.toml | 24 +++++++++++ {src => rustboyadvance-sdl2}/build.rs | 7 ++- .../sdl2 => rustboyadvance-sdl2/src}/audio.rs | 0 .../sdl2 => rustboyadvance-sdl2/src}/cli.yml | 0 .../sdl2 => rustboyadvance-sdl2/src}/input.rs | 0 .../sdl2 => rustboyadvance-sdl2/src}/main.rs | 38 +++------------- .../sdl2 => rustboyadvance-sdl2/src}/video.rs | 0 src/gdb.rs | 18 +------- src/util.rs | 43 +++++++++++++++++++ 14 files changed, 97 insertions(+), 72 deletions(-) create mode 100644 rustboyadvance-minifb/Cargo.toml rename {src/plat/minifb => rustboyadvance-minifb/src}/cli.yml (100%) rename {src/plat/minifb => rustboyadvance-minifb/src}/main.rs (100%) create mode 100644 rustboyadvance-sdl2/Cargo.toml rename {src => rustboyadvance-sdl2}/build.rs (57%) rename {src/plat/sdl2 => rustboyadvance-sdl2/src}/audio.rs (100%) rename {src/plat/sdl2 => rustboyadvance-sdl2/src}/cli.yml (100%) rename {src/plat/sdl2 => rustboyadvance-sdl2/src}/input.rs (100%) rename {src/plat/sdl2 => rustboyadvance-sdl2/src}/main.rs (89%) rename {src/plat/sdl2 => rustboyadvance-sdl2/src}/video.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 5771469..6c1dd65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,8 +3,12 @@ name = "rustboyadvance-ng" version = "0.1.0" authors = ["Michel Heily "] edition = "2018" -default-run= "rba-sdl2" -build = "src/build.rs" + +[workspace] +members = [ + "rustboyadvance-sdl2", + "rustboyadvance-minifb", +] [dependencies] serde = {version = "1.0.104", features = ["derive"] } @@ -14,43 +18,25 @@ num = "0.2.1" num-traits = "0.2" enum-primitive-derive = "^0.1" bit = "^0.1" -clap = {version = "2.33", features = ["color", "yaml"]} colored = "1.9" ansi_term = "0.12.1" hexdump = "0.1.0" -sdl2 = {version = "0.33.0", features = ["image"]} -minifb = "0.11.2" time = "0.2.6" bitfield = "0.13.1" bitflags = "1.2.1" zip = "0.5.4" ctrlc = "3.1.3" -spin_sleep="0.3.7" bit-set = "0.5.1" -ringbuf = "0.2.1" debug_stub_derive = "0.3.0" bytesize = "1.0.0" memmem = "0.1.1" log = "0.4.8" -flexi_logger = {version = "0.14", features = ["colors"]} arrayvec = "0.5.1" rustyline = {version = "6.0.0", optional = true} nom = {version = "5.0.0", optional = true} gdbstub = {git = "https://github.com/daniel5151/gdbstub.git", optional = true, features = ["std"], rev = "9686df6d74c4bf45cbc5746273b82640e8852b6d"} - -[[bin]] -name = "rba-sdl2" -path = "src/plat/sdl2/main.rs" - -[target.'cfg(windows)'.build-dependencies] -winres = "0.1" - -[[bin]] -name = "rba-minifb" -path = "src/plat/minifb/main.rs" - [features] debugger = ["nom", "rustyline"] gdb = ["gdbstub"] diff --git a/README.md b/README.md index 8a1b60e..36802a1 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Place the bios file in the repository root and name it `gba_bios.bin` (or altern Build and run in release mode (performance is terrible in the `dev` profile) ```bash -$ cargo run --release -- path/to/rom +$ cargo run --release -p rustboyadvance-sdl2 -- path/to/rom ``` diff --git a/rustboyadvance-minifb/Cargo.toml b/rustboyadvance-minifb/Cargo.toml new file mode 100644 index 0000000..6ee9572 --- /dev/null +++ b/rustboyadvance-minifb/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rustboyadvance-minifb" +version = "0.1.0" +authors = ["Michel Heily "] +edition = "2018" + +[dependencies] +rustboyadvance-ng = {path = "../"} +minifb = "0.11.2" +clap = {version = "2.33", features = ["color", "yaml"]} +bit = "^0.1" diff --git a/src/plat/minifb/cli.yml b/rustboyadvance-minifb/src/cli.yml similarity index 100% rename from src/plat/minifb/cli.yml rename to rustboyadvance-minifb/src/cli.yml diff --git a/src/plat/minifb/main.rs b/rustboyadvance-minifb/src/main.rs similarity index 100% rename from src/plat/minifb/main.rs rename to rustboyadvance-minifb/src/main.rs diff --git a/rustboyadvance-sdl2/Cargo.toml b/rustboyadvance-sdl2/Cargo.toml new file mode 100644 index 0000000..6d2d3ee --- /dev/null +++ b/rustboyadvance-sdl2/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "rustboyadvance-sdl2" +version = "0.1.0" +authors = ["Michel Heily "] +edition = "2018" + +[dependencies] +rustboyadvance-ng = {path = "../"} +sdl2 = {version = "0.33.0", features = ["image"]} +ringbuf = "0.2.1" +bytesize = "1.0.0" +clap = {version = "2.33", features = ["color", "yaml"]} +log = "0.4.8" +flexi_logger = {version = "0.14", features = ["colors"]} +bit = "^0.1" +spin_sleep="0.3.7" + + +[target.'cfg(windows)'.build-dependencies] +winres = "0.1" + +[features] +debugger = ["rustboyadvance-ng/debugger"] +gdb = ["rustboyadvance-ng/gdb"] \ No newline at end of file diff --git a/src/build.rs b/rustboyadvance-sdl2/build.rs similarity index 57% rename from src/build.rs rename to rustboyadvance-sdl2/build.rs index cfa9215..cb153e8 100644 --- a/src/build.rs +++ b/rustboyadvance-sdl2/build.rs @@ -1,9 +1,12 @@ +#[cfg(windows)] +use winres; + #[cfg(windows)] fn main() { let mut res = winres::WindowsResource::new(); - res.set_icon("assets/icon.ico"); + res.set_icon("../assets/icon.ico"); res.compile().unwrap(); } -#[cfg(unix)] +#[cfg(not(windows))] fn main() {} diff --git a/src/plat/sdl2/audio.rs b/rustboyadvance-sdl2/src/audio.rs similarity index 100% rename from src/plat/sdl2/audio.rs rename to rustboyadvance-sdl2/src/audio.rs diff --git a/src/plat/sdl2/cli.yml b/rustboyadvance-sdl2/src/cli.yml similarity index 100% rename from src/plat/sdl2/cli.yml rename to rustboyadvance-sdl2/src/cli.yml diff --git a/src/plat/sdl2/input.rs b/rustboyadvance-sdl2/src/input.rs similarity index 100% rename from src/plat/sdl2/input.rs rename to rustboyadvance-sdl2/src/input.rs diff --git a/src/plat/sdl2/main.rs b/rustboyadvance-sdl2/src/main.rs similarity index 89% rename from src/plat/sdl2/main.rs rename to rustboyadvance-sdl2/src/main.rs index 9d5d1e2..15bd69a 100644 --- a/src/plat/sdl2/main.rs +++ b/rustboyadvance-sdl2/src/main.rs @@ -27,9 +27,6 @@ extern crate log; use flexi_logger; use flexi_logger::*; -#[cfg(feature = "gdb")] -use gdbstub; - mod audio; mod input; mod video; @@ -39,12 +36,12 @@ use input::create_input; use video::{create_video_interface, SCREEN_HEIGHT, SCREEN_WIDTH}; use rustboyadvance_ng::core::cartridge::BackupType; -#[cfg(feature = "gdb")] -use rustboyadvance_ng::gdb::spawn_gdb_server; use rustboyadvance_ng::prelude::*; +use rustboyadvance_ng::util::spawn_and_run_gdb_server; use rustboyadvance_ng::util::FpsCounter; const LOG_DIR: &str = ".logs"; +const DEFAULT_GDB_SERVER_ADDR: &'static str = "localhost:1337"; fn get_savestate_path(rom_filename: &Path) -> PathBuf { rom_filename.with_extension("savestate") @@ -65,29 +62,6 @@ fn wait_for_rom(event_pump: &mut EventPump) -> String { } } -fn spawn_and_run_gdb_server(gba: &mut GameBoyAdvance) -> Result<(), Box> { - #[cfg(feature = "gdb")] - { - let mut gdb = spawn_gdb_server(format!("localhost:{}", 1337))?; - let result = match gdb.run(gba) { - Ok(state) => { - info!("Disconnected from GDB. Target state: {:?}", state); - Ok(()) - } - Err(gdbstub::Error::TargetError(e)) => Err(e), - Err(e) => return Err(e.into()), - }; - - info!("Debugger session ended, result={:?}", result); - } - #[cfg(not(feature = "gdb"))] - { - error!("Please compile me with 'gdb' feature") - } - - Ok(()) -} - fn main() -> Result<(), Box> { fs::create_dir_all(LOG_DIR).expect(&format!("could not create log directory ({})", LOG_DIR)); flexi_logger::Logger::with_env_or_str("info") @@ -131,7 +105,7 @@ fn main() -> Result<(), Box> { let icon_texture = texture_creator .load_texture("assets/icon.png") .expect("failed to load icon"); - canvas.copy(&icon_texture, None, None).unwrap(); + canvas.copy(&icon_texture, None, None)?; canvas.present(); // TODO also set window icon @@ -141,7 +115,7 @@ fn main() -> Result<(), Box> { let input = Rc::new(RefCell::new(create_input())); let bios_path = Path::new(matches.value_of("bios").unwrap_or_default()); - let bios_bin = read_bin_file(bios_path).unwrap(); + let bios_bin = read_bin_file(bios_path).expect("cannot read bios file"); let mut rom_path = match matches.value_of("game_rom") { Some(path) => path.to_string(), @@ -191,7 +165,7 @@ fn main() -> Result<(), Box> { } if with_gdbserver { - spawn_and_run_gdb_server(&mut gba)?; + spawn_and_run_gdb_server(&mut gba, DEFAULT_GDB_SERVER_ADDR)?; } let mut fps_counter = FpsCounter::default(); @@ -230,7 +204,7 @@ fn main() -> Result<(), Box> { keycode: Some(Keycode::F2), .. } => { - spawn_and_run_gdb_server(&mut gba)?; + spawn_and_run_gdb_server(&mut gba, DEFAULT_GDB_SERVER_ADDR)?; } Event::KeyUp { keycode: Some(Keycode::F5), diff --git a/src/plat/sdl2/video.rs b/rustboyadvance-sdl2/src/video.rs similarity index 100% rename from src/plat/sdl2/video.rs rename to rustboyadvance-sdl2/src/video.rs diff --git a/src/gdb.rs b/src/gdb.rs index 27fdb8a..9e86337 100644 --- a/src/gdb.rs +++ b/src/gdb.rs @@ -6,25 +6,9 @@ use super::core::Bus; use super::core::GameBoyAdvance; use byteorder::{LittleEndian, ReadBytesExt}; -use gdbstub::{Access, GdbStub, Target, TargetState}; +use gdbstub::{Access, Target, TargetState}; -use std::fmt; use std::io::Cursor; -use std::net::{TcpListener, TcpStream, ToSocketAddrs}; - -pub type GdbServer = GdbStub; - -pub fn spawn_gdb_server( - addr: A, -) -> Result> { - info!("spawning gdbserver, listening on {}", addr); - - let sock = TcpListener::bind(addr)?; - let (stream, addr) = sock.accept()?; - info!("got connection from {}", addr); - - Ok(GdbServer::new(stream)) -} impl Target for GameBoyAdvance { type Usize = u32; diff --git a/src/util.rs b/src/util.rs index 193eecc..8b0de94 100644 --- a/src/util.rs +++ b/src/util.rs @@ -4,6 +4,49 @@ use std::io::prelude::*; use std::path::Path; use std::time; +use crate::core::GameBoyAdvance; +#[cfg(feature = "gdb")] +use gdbstub; +#[cfg(feature = "gdb")] +use gdbstub::GdbStub; +use std::fmt; +use std::net::ToSocketAddrs; +#[cfg(feature = "gdb")] +use std::net::TcpListener; + +pub fn spawn_and_run_gdb_server( + target: &mut GameBoyAdvance, + addr: A, +) -> Result<(), Box> { + #[cfg(feature = "gdb")] + { + info!("spawning gdbserver, listening on {}", addr); + + let sock = TcpListener::bind(addr)?; + let (stream, addr) = sock.accept()?; + + info!("got connection from {}", addr); + + let mut gdb = GdbStub::new(stream); + let result = match gdb.run(target) { + Ok(state) => { + info!("Disconnected from GDB. Target state: {:?}", state); + Ok(()) + } + Err(gdbstub::Error::TargetError(e)) => Err(e), + Err(e) => return Err(e.into()), + }; + + info!("Debugger session ended, result={:?}", result); + } + #[cfg(not(feature = "gdb"))] + { + error!("failed. please compile me with 'gdb' feature") + } + + Ok(()) +} + pub fn read_bin_file(filename: &Path) -> io::Result> { let mut buf = Vec::new(); let mut file = File::open(filename)?;