diff --git a/Cargo.lock b/Cargo.lock index b453756..35686c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -287,6 +287,16 @@ dependencies = [ "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", + "num-iter 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "num" version = "0.2.0" @@ -407,6 +417,33 @@ dependencies = [ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_jitter 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_chacha" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand_core" version = "0.3.1" @@ -420,6 +457,32 @@ name = "rand_core" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rand_hc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_isaac" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_jitter" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand_os" version = "0.1.3" @@ -433,6 +496,23 @@ dependencies = [ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_pcg" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_xorshift" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rdrand" version = "0.4.0" @@ -485,6 +565,7 @@ dependencies = [ "num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "rustyline 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sdl2 0.32.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -526,6 +607,28 @@ name = "scoped_threadpool" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "sdl2" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "sdl2-sys 0.32.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sdl2-sys" +version = "0.32.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "semver" version = "0.9.0" @@ -736,6 +839,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" "checksum nom 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e9761d859320e381010a4f7f8ed425f2c924de33ad121ace447367c713ad561b" +"checksum num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e" "checksum num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf4825417e1e1406b3782a8ce92f4d53f26ec055e3622e1881ca8e9f5f9e08db" "checksum num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "57450397855d951f1a41305e54851b1a7b8f5d2e349543a02a2effe25459f718" "checksum num-complex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fcb0cf31fb3ff77e6d2a6ebd6800df7fdcd106f2ad89113c9130bcd07f93dffc" @@ -750,9 +854,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db" "checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +"checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" +"checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" "checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" "checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0" +"checksum rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" +"checksum rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" +"checksum rand_jitter 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" "checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +"checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" +"checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)" = "12229c14a0f65c4f1cb046a3b52047cdd9da1f4b30f8a39c5063c8bae515e252" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" @@ -763,6 +874,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustyline 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "67e12e40e0240de07f0dab4f4dd01bdb15d74dc977026d4ba91666c41c679ade" "checksum ryu 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b96a9549dc8d48f2c283938303c4b5a77aa29bfbc5b54b084fb1630408899a8f" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" +"checksum sdl2 0.32.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d051a07231e303f5f719da78cb6f7394f6d5b54f733aef5b0b447804a83edd7b" +"checksum sdl2-sys 0.32.6 (registry+https://github.com/rust-lang/crates.io-index)" = "34e71125077d297d57e4c1acfe8981b5bdfbf5a20e7b589abfdcb33bf1127f86" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum stackvector 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "1c4725650978235083241fab0fdc8e694c3de37821524e7534a1a9061d1068af" diff --git a/Cargo.toml b/Cargo.toml index 2d0a4e2..c3982e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ nom = "5.0.0" colored = "1.8" ansi_term = "0.11.0" hexdump = "0.1.0" +sdl2 = "0.32.2" [profile.dev] opt-level = 0 \ No newline at end of file diff --git a/src/debugger/command.rs b/src/debugger/command.rs index 21f98d8..f2d032e 100644 --- a/src/debugger/command.rs +++ b/src/debugger/command.rs @@ -4,6 +4,7 @@ use crate::disass::Disassembler; use crate::GBAError; use super::{parser::Value, Debugger, DebuggerError, DebuggerResult}; +use super::palette_view::create_palette_view; use ansi_term::Colour; @@ -25,6 +26,7 @@ pub enum Command { Disass(DisassMode, Addr, usize), AddBreakpoint(Addr), DelBreakpoint(Addr), + PaletteView, ClearBreakpoints, ListBreakpoints, Reset, @@ -138,6 +140,7 @@ impl Command { println!("[{}] 0x{:08x}", i, b) } } + PaletteView => create_palette_view(debugger.gba.sysbus.get_bytes(0x0500_0000)), Reset => { println!("resetting cpu..."); debugger.gba.cpu.reset(); @@ -266,6 +269,7 @@ impl Debugger { "breakdel [addr]", ))), }, + "palette-view" => Ok(Command::PaletteView), "bl" => Ok(Command::ListBreakpoints), "q" | "quit" => Ok(Command::Quit), "r" | "reset" => Ok(Command::Reset), diff --git a/src/debugger/mod.rs b/src/debugger/mod.rs index 959241e..e1e1f6f 100644 --- a/src/debugger/mod.rs +++ b/src/debugger/mod.rs @@ -12,6 +12,8 @@ use parser::{parse_expr, DerefType, Expr, Value}; mod command; use command::Command; +mod palette_view; + #[derive(Debug, PartialEq)] pub enum DebuggerError { ParsingError(String), diff --git a/src/debugger/palette_view.rs b/src/debugger/palette_view.rs new file mode 100644 index 0000000..fae92c5 --- /dev/null +++ b/src/debugger/palette_view.rs @@ -0,0 +1,121 @@ +use sdl2::event::Event; +use sdl2::pixels::Color; +use sdl2::rect::{Point, Rect}; +use sdl2::render::Canvas; + +use super::super::palette::{Palette, Rgb15}; + +const PALETTE_RECT_WIDTH: u32 = 20; + +const SCREEN_WIDTH: u32 = 900; +const SCREEN_HEIGHT: u32 = 500; + +struct ColoredRect { + index: usize, + rect: Rect, + color: Rgb15, +} + +impl ColoredRect { + fn new(index: usize, x: i32, y: i32, c: Rgb15) -> ColoredRect { + ColoredRect { + index: index, + rect: Rect::new(x, y, PALETTE_RECT_WIDTH, PALETTE_RECT_WIDTH), + color: c, + } + } + + fn draw(&self, canvas: &mut Canvas) { + canvas.set_draw_color(Color::RGB(0, 0, 0)); + canvas + .fill_rect(Rect::new( + self.rect.x() - 1, + self.rect.y() - 1, + PALETTE_RECT_WIDTH + 2, + PALETTE_RECT_WIDTH + 2, + )) + .unwrap(); + + let (r, g, b) = self.color.get_rgb24(); + canvas.set_draw_color(Color::RGB(r, g, b)); + canvas.fill_rect(self.rect).unwrap(); + } +} + +pub fn create_palette_view(palette_ram: &[u8]) { + + let palette = Palette::from(palette_ram); + + let sdl_context = sdl2::init().unwrap(); + let video_subsystem = sdl_context.video().unwrap(); + + let window = video_subsystem + .window("PaletteView", SCREEN_WIDTH, SCREEN_HEIGHT) + .position_centered() + .build() + .unwrap(); + + let mut canvas = window.into_canvas().build().unwrap(); + + canvas.set_draw_color(Color::RGB(0xfa, 0xfa, 0xfa)); + canvas.clear(); + + let mut bg_colors: Vec = Vec::with_capacity(256); + let mut fg_colors: Vec = Vec::with_capacity(256); + + let initial_x = 30u32; + let mut y = 20u32; + let mut x = initial_x; + for i in 0..256 { + bg_colors.push(ColoredRect::new( + i, + x as i32, + y as i32, + palette.bg_colors[i], + )); + fg_colors.push(ColoredRect::new( + i, + x as i32 + 450, + y as i32, + palette.fg_colors[i], + )); + + x = if (i + 1) % 16 == 0 { + y += 24; + initial_x + } else { + x + PALETTE_RECT_WIDTH + 4 + } + } + + for bgc in &bg_colors { + bgc.draw(&mut canvas); + } + for fgc in &fg_colors { + fgc.draw(&mut canvas); + } + + canvas.present(); + + let mut event_pump = sdl_context.event_pump().unwrap(); + 'running: loop { + for event in event_pump.poll_iter() { + match event { + Event::Quit { .. } => break 'running, + Event::MouseButtonDown { x, y, .. } => { + for bgc in &bg_colors { + if bgc.rect.contains_point(Point::new(x, y)) { + println!("BG Color #{}: {}", bgc.index, bgc.color); + } + } + for fgc in &fg_colors { + if fgc.rect.contains_point(Point::new(x, y)) { + println!("FG Color #{}: {}", fgc.index, fgc.color); + } + } + } + _ => {} + } + } + } +}