refactor: split "plat" binaries into separate crates

Former-commit-id: 73554b0128e036279f7ac895acb57d8f89cb1c96
This commit is contained in:
Michel Heily 2020-02-23 00:19:55 +02:00 committed by MishMish
parent c50ab1ecd8
commit 778274b5ae
14 changed files with 97 additions and 72 deletions

View file

@ -3,8 +3,12 @@ name = "rustboyadvance-ng"
version = "0.1.0" version = "0.1.0"
authors = ["Michel Heily <michelheily@gmail.com>"] authors = ["Michel Heily <michelheily@gmail.com>"]
edition = "2018" edition = "2018"
default-run= "rba-sdl2"
build = "src/build.rs" [workspace]
members = [
"rustboyadvance-sdl2",
"rustboyadvance-minifb",
]
[dependencies] [dependencies]
serde = {version = "1.0.104", features = ["derive"] } serde = {version = "1.0.104", features = ["derive"] }
@ -14,43 +18,25 @@ num = "0.2.1"
num-traits = "0.2" num-traits = "0.2"
enum-primitive-derive = "^0.1" enum-primitive-derive = "^0.1"
bit = "^0.1" bit = "^0.1"
clap = {version = "2.33", features = ["color", "yaml"]}
colored = "1.9" colored = "1.9"
ansi_term = "0.12.1" ansi_term = "0.12.1"
hexdump = "0.1.0" hexdump = "0.1.0"
sdl2 = {version = "0.33.0", features = ["image"]}
minifb = "0.11.2"
time = "0.2.6" time = "0.2.6"
bitfield = "0.13.1" bitfield = "0.13.1"
bitflags = "1.2.1" bitflags = "1.2.1"
zip = "0.5.4" zip = "0.5.4"
ctrlc = "3.1.3" ctrlc = "3.1.3"
spin_sleep="0.3.7"
bit-set = "0.5.1" bit-set = "0.5.1"
ringbuf = "0.2.1"
debug_stub_derive = "0.3.0" debug_stub_derive = "0.3.0"
bytesize = "1.0.0" bytesize = "1.0.0"
memmem = "0.1.1" memmem = "0.1.1"
log = "0.4.8" log = "0.4.8"
flexi_logger = {version = "0.14", features = ["colors"]}
arrayvec = "0.5.1" arrayvec = "0.5.1"
rustyline = {version = "6.0.0", optional = true} rustyline = {version = "6.0.0", optional = true}
nom = {version = "5.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"} 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] [features]
debugger = ["nom", "rustyline"] debugger = ["nom", "rustyline"]
gdb = ["gdbstub"] gdb = ["gdbstub"]

View file

@ -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) Build and run in release mode (performance is terrible in the `dev` profile)
```bash ```bash
$ cargo run --release -- path/to/rom $ cargo run --release -p rustboyadvance-sdl2 -- path/to/rom
``` ```

View file

@ -0,0 +1,11 @@
[package]
name = "rustboyadvance-minifb"
version = "0.1.0"
authors = ["Michel Heily <michelheily@gmail.com>"]
edition = "2018"
[dependencies]
rustboyadvance-ng = {path = "../"}
minifb = "0.11.2"
clap = {version = "2.33", features = ["color", "yaml"]}
bit = "^0.1"

View file

@ -0,0 +1,24 @@
[package]
name = "rustboyadvance-sdl2"
version = "0.1.0"
authors = ["Michel Heily <michelheily@gmail.com>"]
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"]

View file

@ -1,9 +1,12 @@
#[cfg(windows)]
use winres;
#[cfg(windows)] #[cfg(windows)]
fn main() { fn main() {
let mut res = winres::WindowsResource::new(); let mut res = winres::WindowsResource::new();
res.set_icon("assets/icon.ico"); res.set_icon("../assets/icon.ico");
res.compile().unwrap(); res.compile().unwrap();
} }
#[cfg(unix)] #[cfg(not(windows))]
fn main() {} fn main() {}

View file

@ -27,9 +27,6 @@ extern crate log;
use flexi_logger; use flexi_logger;
use flexi_logger::*; use flexi_logger::*;
#[cfg(feature = "gdb")]
use gdbstub;
mod audio; mod audio;
mod input; mod input;
mod video; mod video;
@ -39,12 +36,12 @@ use input::create_input;
use video::{create_video_interface, SCREEN_HEIGHT, SCREEN_WIDTH}; use video::{create_video_interface, SCREEN_HEIGHT, SCREEN_WIDTH};
use rustboyadvance_ng::core::cartridge::BackupType; use rustboyadvance_ng::core::cartridge::BackupType;
#[cfg(feature = "gdb")]
use rustboyadvance_ng::gdb::spawn_gdb_server;
use rustboyadvance_ng::prelude::*; use rustboyadvance_ng::prelude::*;
use rustboyadvance_ng::util::spawn_and_run_gdb_server;
use rustboyadvance_ng::util::FpsCounter; use rustboyadvance_ng::util::FpsCounter;
const LOG_DIR: &str = ".logs"; const LOG_DIR: &str = ".logs";
const DEFAULT_GDB_SERVER_ADDR: &'static str = "localhost:1337";
fn get_savestate_path(rom_filename: &Path) -> PathBuf { fn get_savestate_path(rom_filename: &Path) -> PathBuf {
rom_filename.with_extension("savestate") 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<dyn std::error::Error>> {
#[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<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
fs::create_dir_all(LOG_DIR).expect(&format!("could not create log directory ({})", LOG_DIR)); fs::create_dir_all(LOG_DIR).expect(&format!("could not create log directory ({})", LOG_DIR));
flexi_logger::Logger::with_env_or_str("info") flexi_logger::Logger::with_env_or_str("info")
@ -131,7 +105,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let icon_texture = texture_creator let icon_texture = texture_creator
.load_texture("assets/icon.png") .load_texture("assets/icon.png")
.expect("failed to load icon"); .expect("failed to load icon");
canvas.copy(&icon_texture, None, None).unwrap(); canvas.copy(&icon_texture, None, None)?;
canvas.present(); canvas.present();
// TODO also set window icon // TODO also set window icon
@ -141,7 +115,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = Rc::new(RefCell::new(create_input())); let input = Rc::new(RefCell::new(create_input()));
let bios_path = Path::new(matches.value_of("bios").unwrap_or_default()); 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") { let mut rom_path = match matches.value_of("game_rom") {
Some(path) => path.to_string(), Some(path) => path.to_string(),
@ -191,7 +165,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
if with_gdbserver { 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(); let mut fps_counter = FpsCounter::default();
@ -230,7 +204,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
keycode: Some(Keycode::F2), keycode: Some(Keycode::F2),
.. ..
} => { } => {
spawn_and_run_gdb_server(&mut gba)?; spawn_and_run_gdb_server(&mut gba, DEFAULT_GDB_SERVER_ADDR)?;
} }
Event::KeyUp { Event::KeyUp {
keycode: Some(Keycode::F5), keycode: Some(Keycode::F5),

View file

@ -6,25 +6,9 @@ use super::core::Bus;
use super::core::GameBoyAdvance; use super::core::GameBoyAdvance;
use byteorder::{LittleEndian, ReadBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
use gdbstub::{Access, GdbStub, Target, TargetState}; use gdbstub::{Access, Target, TargetState};
use std::fmt;
use std::io::Cursor; use std::io::Cursor;
use std::net::{TcpListener, TcpStream, ToSocketAddrs};
pub type GdbServer = GdbStub<GameBoyAdvance, TcpStream>;
pub fn spawn_gdb_server<A: ToSocketAddrs + fmt::Display>(
addr: A,
) -> Result<GdbServer, Box<dyn std::error::Error>> {
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 { impl Target for GameBoyAdvance {
type Usize = u32; type Usize = u32;

View file

@ -4,6 +4,49 @@ use std::io::prelude::*;
use std::path::Path; use std::path::Path;
use std::time; 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<A: ToSocketAddrs + fmt::Display>(
target: &mut GameBoyAdvance,
addr: A,
) -> Result<(), Box<dyn std::error::Error>> {
#[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<Vec<u8>> { pub fn read_bin_file(filename: &Path) -> io::Result<Vec<u8>> {
let mut buf = Vec::new(); let mut buf = Vec::new();
let mut file = File::open(filename)?; let mut file = File::open(filename)?;