From e57d400d16ef5c7756d320b7b040757cdad9ff28 Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Thu, 26 Dec 2019 11:58:12 +0100 Subject: [PATCH 1/3] messing around with glyphbrush --- Cargo.lock | 75 ++++++++ Cargo.toml | 5 +- src/main.rs | 461 ++++++++++++++++++++++++++----------------------- src/nes/cpu.rs | 8 +- src/render.rs | 42 +++++ 5 files changed, 374 insertions(+), 217 deletions(-) create mode 100644 src/render.rs diff --git a/Cargo.lock b/Cargo.lock index 11eeab4..2fa2b34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,10 +6,13 @@ version = "0.1.0" dependencies = [ "failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "gfx_core 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", + "gfx_device_gl 0.16.2 (registry+https://github.com/rust-lang/crates.io-index)", + "gfx_glyph 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "piston2d-opengl_graphics 0.70.0 (registry+https://github.com/rust-lang/crates.io-index)", "piston_window 0.105.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "simple_logger 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -366,6 +369,18 @@ dependencies = [ "gl_generator 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "gfx_glyph" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)", + "gfx 0.18.2 (registry+https://github.com/rust-lang/crates.io-index)", + "gfx_core 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", + "glyph_brush 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gif" version = "0.10.3" @@ -486,6 +501,28 @@ dependencies = [ "gl_generator 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "glyph_brush" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glyph_brush_layout 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rusttype 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", + "twox-hash 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "glyph_brush_layout" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rusttype 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", + "xi-unicode 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "hermit-abi" version = "0.1.5" @@ -569,6 +606,11 @@ dependencies = [ "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "linked-hash-map" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lock_api" version = "0.3.2" @@ -1083,6 +1125,14 @@ name = "rustc-demangle" version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rustc-hash" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rustc_version" version = "0.2.3" @@ -1106,7 +1156,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "approx 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-deque 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "stb_truetype 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1272,6 +1327,14 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "twox-hash" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-xid" version = "0.1.0" @@ -1431,6 +1494,11 @@ name = "xdg" version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "xi-unicode" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "xml-rs" version = "0.8.0" @@ -1481,6 +1549,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gfx_core 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "75fbddaef2e12b4995900539d7209d947b988a3d87ee8737484d049b526e5441" "checksum gfx_device_gl 0.16.2 (registry+https://github.com/rust-lang/crates.io-index)" = "109c385fa380c18888633aa27d1e16cbae518469702a2f69dcb5f52d5378bebc" "checksum gfx_gl 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8525888d909a6424b04f9136976f07a85fc1f3704555c1a73897e258326c8319" +"checksum gfx_glyph 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d327b1b1d6c3846a48a931eb2d793c0a35a7e094a5ca29cc9770432dc2192c32" "checksum gif 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)" = "471d90201b3b223f3451cd4ad53e34295f16a1df17b1edf3736d47761c3981af" "checksum gl 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d7d8c8e25e8ed44d4813809205090162723a866fb4be3a9d8bb983c9a0bf98f1" "checksum gl_generator 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a0ffaf173cf76c73a73e080366bf556b4776ece104b06961766ff11449f38604" @@ -1493,6 +1562,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum glutin_gles2_sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "89996c30857ae1b4de4b5189abf1ea822a20a9fe9e1c93e5e7b862ff0bdd5cdf" "checksum glutin_glx_sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1290a5ca5e46fcfa7f66f949cc9d9194b2cb6f2ed61892c8c2b82343631dba57" "checksum glutin_wgl_sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f801bbc91efc22dd1c4818a47814fc72bf74d024510451b119381579bfa39021" +"checksum glyph_brush 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6ec4a0787fffc6692ec1ccef9fe09a8e02df1b7fc8fadd5af96662dc979473f9" +"checksum glyph_brush_layout 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "62b6d460a62dc8a2d3402689b02824ca03d40ec6f1673b9d2016d1f30ce5cb62" "checksum hermit-abi 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f629dc602392d3ec14bfc8a09b5e644d7ffd725102b48b81e59f90f2633621d7" "checksum image 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4be8aaefbe7545dc42ae925afb55a0098f226a3fe5ef721872806f44f57826" "checksum inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff" @@ -1504,6 +1575,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" "checksum libloading 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753" "checksum line_drawing 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5cc7ad3d82c845bdb5dde34ffdcc7a5fb4d2996e1e1ee0f19c33bc80e15196b9" +"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83" "checksum lock_api 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e57b3997725d2b60dbec1297f6c2e2957cc383db1cebd6be812163f969c7d586" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" @@ -1562,6 +1634,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum read_color 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9f4c8858baa4ad3c8bcc156ae91a0ffe22b76a3975c40c49b4f04c15c6bce0da" "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" +"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum rusttype 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)" = "310942406a39981bed7e12b09182a221a29e0990f3e7e0c971f131922ed135d5" "checksum rusttype 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "14a911032fb5791ccbeec9f28fdcb9bf0983b81f227bafdfd227c658d0731c8a" @@ -1584,6 +1657,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" "checksum tiff 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b7c2cfc4742bd8a32f2e614339dd8ce30dbcf676bb262bd63a2327bc5df57d" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +"checksum twox-hash 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3bfd5b7557925ce778ff9b9ef90e3ade34c524b5ff10e239c69a42d546d2af56" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum vecmath 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "956ae1e0d85bca567dee1dcf87fb1ca2e792792f66f87dced8381f99cd91156a" @@ -1602,4 +1676,5 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum winit 0.19.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1e96eb4bb472fa43e718e8fa4aef82f86cd9deac9483a1e1529230babdb394a8" "checksum x11-dl 2.18.4 (registry+https://github.com/rust-lang/crates.io-index)" = "be65e1342a3baae65439cd03306778831a3d133b0d20243a7fb83fd5cf403c58" "checksum xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" +"checksum xi-unicode 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7395cdb9d0a6219fa0ea77d08c946adf9c1984c72fcd443ace30365f3daadef7" "checksum xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "541b12c998c5b56aa2b4e6f18f03664eef9a4fd0a246a55594efae6cc2d964b5" diff --git a/Cargo.toml b/Cargo.toml index 38f394a..9dd26e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,7 @@ phf = { version = "0.8", features = ['macros'] } failure = "0.1.*" piston_window = "0.105.*" piston2d-opengl_graphics = "0.70.*" -gfx_core = "0.9.2" \ No newline at end of file +gfx_core = "0.9.2" +gfx_device_gl = "*" +gfx_glyph = "*" +rand = "*" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a935956..8072930 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,8 @@ extern crate simple_logger; extern crate phf; extern crate piston_window; -mod nes; +mod nes; +mod render; use std::path::Path; use piston_window::*; @@ -16,6 +17,11 @@ use nes::disasm::*; use opengl_graphics::OpenGL; use log::Level; use piston_window::Text; +use render::*; + +use gfx_glyph::{Section, GlyphBrushBuilder,GlyphBrush}; +use gfx_device_gl::{Factory,Resources}; + // font options const FT_SIZE_PT: u32 = 6; @@ -25,14 +31,8 @@ const FT_COLOR_RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; const FT_COLOR_GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0]; const FT_LINE_DISTANCE: f64 = FT_SIZE_PX * 0.5; -// font for text drawing -fn get_glyphs(window: &mut PistonWindow) -> Glyphs { - let font_path = Path::new("resources/fonts/PressStart2P.ttf"); - window.load_font(font_path).unwrap() -} - fn main() { - simple_logger::init_with_level(Level::Debug).unwrap(); + simple_logger::init_with_level(Level::Error).unwrap(); let mut bus = MemoryBus::new(); let cartridge = Path::new("test_roms/registers.nes"); @@ -71,250 +71,283 @@ fn main() { .exit_on_esc(true).graphics_api(OpenGL::V3_2).build().unwrap(); let mut event_settings = EventSettings::new(); event_settings.max_fps = 60; + event_settings.ups = 107400; let mut events = Events::new(event_settings); - let mut glyphs = get_glyphs(&mut window); + let ft: &[u8] = include_bytes!("../resources/fonts/PressStart2P.ttf"); + let mut glyphs = GlyphBrushBuilder::using_font_bytes(ft) + .initial_cache_size((256, 256)) + .build(window.factory.clone()); + let mut cpu_renderer = CPURenderer::new(&cpu); // Main loop + let mut run = false; while let Some(event) = events.next(&mut window) { if let Some(_) = event.render_args() { - render(&mut window, &event, &mut glyphs, &cpu, &bus, &disasm); + cpu_renderer.update(&cpu); + render(&mut window, &event, &mut glyphs, &mut cpu_renderer, &bus, &disasm); + } + if let Some(_) = event.update_args() { + if run { + cpu.clock(&mut bus); + // while cpu.is_ahead() { + // cpu.clock(&mut bus); + // } + } } if let Some(Button::Keyboard(key)) = event.press_args() { match key { Key::C => cpu.clock(&mut bus), // advance one clock - Key::Space => { // advance to next instruction + Key::S => { // advance to next instruction cpu.clock(&mut bus); while cpu.is_ahead() { cpu.clock(&mut bus); } } Key::R => cpu.reset(&mut bus), + Key::Space => run = !run, + Key::Up => { + event_settings.ups = (event_settings.ups as f64 * 1.1) as u64 + 1; + events = Events::new(event_settings); + debug!("UPS: {}", event_settings.ups); + } + Key::Down => { + event_settings.ups = (event_settings.ups as f64 * 0.91) as u64; + events = Events::new(event_settings); + debug!("UPS: {}", event_settings.ups); + } _ => { } } - } + } } } -fn render(window: &mut PistonWindow, event: &Event, glyphs: &mut Glyphs, cpu: &CPU, bus: &MemoryBus, disasm: &Disasm) { +fn render(window: &mut PistonWindow, event: &Event, + glyphs: &mut GlyphBrush, + cpu_renderer: &mut CPURenderer, bus: &MemoryBus, disasm: &Disasm) { window.draw_2d(event, |c, g, d| { clear([0.0, 0.0, 1.0, 1.0], g); - let debug_offset = [10.0, 10.0]; - render_cpu(&c, g, glyphs, cpu, debug_offset); - render_disasm(&c, g, glyphs, disasm, cpu.regs.pc, - [debug_offset[0], debug_offset[1] + (7.0 * (FT_LINE_DISTANCE+FT_SIZE_PX))]); - render_memory(&c, g, glyphs, bus, - [debug_offset[0] + 300.0, debug_offset[1]]); + + // let debug_offset = [10.0, 10.0]; + // render_cpu(&c, g, glyphs, cpu, debug_offset); + // render_disasm(&c, g, glyphs, disasm, cpu.regs.pc, + // [debug_offset[0], debug_offset[1] + (7.0 * (FT_LINE_DISTANCE+FT_SIZE_PX))]); + // render_memory(&c, g, glyphs, bus, + // [debug_offset[0] + 300.0, debug_offset[1]]); - glyphs.factory.encoder.flush(d); + + // glyphs.factory.encoder.flush(d); }); + cpu_renderer.draw(glyphs); + glyphs.use_queue().draw(&mut window.encoder, &window.output_color); + window.encoder.flush(&mut window.device); } -fn render_cpu(c: &Context, g: &mut G2d, glyphs: &mut Glyphs, cpu: &CPU, offset: [f64; 2]) { - let mut transform = c.transform - .trans(offset[0], offset[1] + FT_SIZE_PX); - let text_white = Text::new_color(FT_COLOR_WHITE, FT_SIZE_PT); - let text_on = Text::new_color(FT_COLOR_GREEN, FT_SIZE_PT); - let text_off = Text::new_color(FT_COLOR_RED, FT_SIZE_PT); +// fn render_cpu(c: &Context, g: &mut G2d, glyphs: &mut Glyphs, cpu: &CPU, offset: [f64; 2]) { +// let mut transform = c.transform +// .trans(offset[0], offset[1] + FT_SIZE_PX); +// let text_white = Text::new_color(FT_COLOR_WHITE, FT_SIZE_PT); +// let text_on = Text::new_color(FT_COLOR_GREEN, FT_SIZE_PT); +// let text_off = Text::new_color(FT_COLOR_RED, FT_SIZE_PT); - let mut text = text_white; - text.draw( - &"Flags:", - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); +// let mut text = text_white; +// text.draw( +// &"Flags:", +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); - transform = transform.trans(70.0, 0.0); - text = if cpu.get_flag(NEGATIVE) == 1 { text_on } else { text_off }; - text.draw( - &"N", - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - transform = transform.trans(16.0, 0.0); - text = if cpu.get_flag(OVERFLOW) == 1 { text_on } else { text_off }; - text.draw( - &"V", - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - transform = transform.trans(16.0, 0.0); - text_white.draw( - &"-", - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - transform = transform.trans(16.0, 0.0); - text = if cpu.get_flag(BREAK) == 1 { text_on } else { text_off }; - text.draw( - &"B", - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - transform = transform.trans(16.0, 0.0); - text = if cpu.get_flag(DECIMAL) == 1 { text_on } else { text_off }; - text.draw( - &"D", - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - transform = transform.trans(16.0, 0.0); - text = if cpu.get_flag(IRQ) == 1 { text_on } else { text_off }; - text.draw( - &"I", - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - transform = transform.trans(16.0, 0.0); - text = if cpu.get_flag(ZERO) == 1 { text_on } else { text_off }; - text.draw( - &"Z", - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - transform = transform.trans(16.0, 0.0); - text = if cpu.get_flag(CARRY) == 1 { text_on } else { text_off }; - text.draw( - &"C", - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - transform = transform.trans(16.0, 0.0); - text_white.draw( - &format!("({:#4x})", cpu.regs.flags), - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); +// transform = transform.trans(70.0, 0.0); +// text = if cpu.get_flag(NEGATIVE) == 1 { text_on } else { text_off }; +// text.draw( +// &"N", +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// transform = transform.trans(16.0, 0.0); +// text = if cpu.get_flag(OVERFLOW) == 1 { text_on } else { text_off }; +// text.draw( +// &"V", +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// transform = transform.trans(16.0, 0.0); +// text_white.draw( +// &"-", +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// transform = transform.trans(16.0, 0.0); +// text = if cpu.get_flag(BREAK) == 1 { text_on } else { text_off }; +// text.draw( +// &"B", +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// transform = transform.trans(16.0, 0.0); +// text = if cpu.get_flag(DECIMAL) == 1 { text_on } else { text_off }; +// text.draw( +// &"D", +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// transform = transform.trans(16.0, 0.0); +// text = if cpu.get_flag(IRQ) == 1 { text_on } else { text_off }; +// text.draw( +// &"I", +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// transform = transform.trans(16.0, 0.0); +// text = if cpu.get_flag(ZERO) == 1 { text_on } else { text_off }; +// text.draw( +// &"Z", +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// transform = transform.trans(16.0, 0.0); +// text = if cpu.get_flag(CARRY) == 1 { text_on } else { text_off }; +// text.draw( +// &"C", +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// transform = transform.trans(16.0, 0.0); +// text_white.draw( +// &format!("({:#4x})", cpu.regs.flags), +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); - let cpu_register_texts = [ - &format!("A: {0:#x} ({0})", cpu.regs.a), - &format!("X: {0:#x} ({0})", cpu.regs.x), - &format!("Y: {0:#x} ({0})", cpu.regs.y), - &format!("Stack P: {0:#x} ({0})", cpu.regs.sp), - &format!("Program P: {:#x}", cpu.regs.pc), - ]; +// let cpu_register_texts = [ +// &format!("A: {0:#x} ({0})", cpu.regs.a), +// &format!("X: {0:#x} ({0})", cpu.regs.x), +// &format!("Y: {0:#x} ({0})", cpu.regs.y), +// &format!("Stack P: {0:#x} ({0})", cpu.regs.sp), +// &format!("Program P: {:#x}", cpu.regs.pc), +// ]; - for (i, &text) in cpu_register_texts.iter().enumerate() { - let y_offset = (i+2) as f64 * (FT_LINE_DISTANCE + FT_SIZE_PX); - let transform = c.transform.trans(offset[0], offset[1] + y_offset); - text_white.draw( - &text, - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - } -} +// for (i, &text) in cpu_register_texts.iter().enumerate() { +// let y_offset = (i+2) as f64 * (FT_LINE_DISTANCE + FT_SIZE_PX); +// let transform = c.transform.trans(offset[0], offset[1] + y_offset); +// text_white.draw( +// &text, +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// } +// } -fn render_disasm(c: &Context, g: &mut G2d, glyphs: &mut Glyphs, disasm: &Disasm, pc: Addr, offset: [f64; 2]) { - let pc_position = disasm.addresses.iter().position(|&pos| pos == pc); - if let None = pc_position { - return; - } - let pc_position = pc_position.unwrap(); - let lines_above_below: usize = 12; +// fn render_disasm(c: &Context, g: &mut G2d, glyphs: &mut Glyphs, disasm: &Disasm, pc: Addr, offset: [f64; 2]) { +// let pc_position = disasm.addresses.iter().position(|&pos| pos == pc); +// if let None = pc_position { +// return; +// } +// let pc_position = pc_position.unwrap(); +// let lines_above_below: usize = 12; - let start = if (pc_position as i32 - lines_above_below as i32) < 0 { - 0 - } else { - pc_position - lines_above_below - }; +// let start = if (pc_position as i32 - lines_above_below as i32) < 0 { +// 0 +// } else { +// pc_position - lines_above_below +// }; - let mut transform = c.transform.trans(offset[0], offset[1]); - let text_current = Text::new_color(FT_COLOR_RED, FT_SIZE_PT); - let text_other = Text::new_color(FT_COLOR_WHITE, FT_SIZE_PT); +// let mut transform = c.transform.trans(offset[0], offset[1]); +// let text_current = Text::new_color(FT_COLOR_RED, FT_SIZE_PT); +// let text_other = Text::new_color(FT_COLOR_WHITE, FT_SIZE_PT); - for i in (start..disasm.addresses.len()).take(lines_above_below*2+1) { - let addr = disasm.addresses[i]; - let txt = &disasm.instructions[i]; +// for i in (start..disasm.addresses.len()).take(lines_above_below*2+1) { +// let addr = disasm.addresses[i]; +// let txt = &disasm.instructions[i]; - transform = transform.trans(0.0, FT_LINE_DISTANCE + FT_SIZE_PX); - let text = if addr == pc { - text_current - } else { - text_other - }; - text.draw( - txt, - glyphs, - &c.draw_state, - transform, - g - ).unwrap(); - } +// transform = transform.trans(0.0, FT_LINE_DISTANCE + FT_SIZE_PX); +// let text = if addr == pc { +// text_current +// } else { +// text_other +// }; +// text.draw( +// txt, +// glyphs, +// &c.draw_state, +// transform, +// g +// ).unwrap(); +// } -} +// } -fn render_memory(c: &Context, g: &mut G2d, glyphs: &mut Glyphs, bus: &MemoryBus, offset: [f64; 2]) { - let mut transform_y = c.transform.trans(offset[0], offset[1]); - let text = Text::new_color(FT_COLOR_WHITE, FT_SIZE_PT); - for page in (0x0000..0x01FF).step_by(16) { - transform_y = transform_y.trans(0.0, FT_LINE_DISTANCE+FT_SIZE_PX); - text.draw( - &format!("{:#06x}:", page), - glyphs, - &c.draw_state, - transform_y, - g - ).unwrap(); - let mut transform_x = transform_y.trans(40.0, 0.0); - for offset in 0u16..16u16 { - transform_x = transform_x.trans(20.0, 0.0); - let addr = page + offset; - text.draw( - &format!("{:02x}", bus.readb(addr)), - glyphs, - &c.draw_state, - transform_x, - g - ).unwrap(); - } - } +// fn render_memory(c: &Context, g: &mut G2d, glyphs: &mut Glyphs, bus: &MemoryBus, offset: [f64; 2]) { +// let mut transform_y = c.transform.trans(offset[0], offset[1]); +// let text = Text::new_color(FT_COLOR_WHITE, FT_SIZE_PT); +// for page in (0x0000..0x01FF).step_by(16) { +// transform_y = transform_y.trans(0.0, FT_LINE_DISTANCE+FT_SIZE_PX); +// text.draw( +// &format!("{:#06x}:", page), +// glyphs, +// &c.draw_state, +// transform_y, +// g +// ).unwrap(); +// let mut transform_x = transform_y.trans(40.0, 0.0); +// for offset in 0u16..16u16 { +// transform_x = transform_x.trans(20.0, 0.0); +// let addr = page + offset; +// text.draw( +// &format!("{:02x}", bus.readb(addr)), +// glyphs, +// &c.draw_state, +// transform_x, +// g +// ).unwrap(); +// } +// } - transform_y = transform_y.trans(0.0, (FT_LINE_DISTANCE+FT_SIZE_PX) * 1.5); - for page in (0x6000..0x6030).step_by(16) { - transform_y = transform_y.trans(0.0, (FT_LINE_DISTANCE+FT_SIZE_PX)); - text.draw( - &format!("{:#06x}:", page), - glyphs, - &c.draw_state, - transform_y, - g - ).unwrap(); - let mut transform_x = transform_y.trans(40.0, 0.0); - for offset in 0u16..16u16 { - transform_x = transform_x.trans(20.0, 0.0); - let addr = page + offset; - text.draw( - &format!("{:02x}", bus.readb(addr)), - glyphs, - &c.draw_state, - transform_x, - g - ).unwrap(); - } - } -} \ No newline at end of file +// transform_y = transform_y.trans(0.0, (FT_LINE_DISTANCE+FT_SIZE_PX) * 1.5); +// for page in (0x6000..0x6030).step_by(16) { +// transform_y = transform_y.trans(0.0, (FT_LINE_DISTANCE+FT_SIZE_PX)); +// text.draw( +// &format!("{:#06x}:", page), +// glyphs, +// &c.draw_state, +// transform_y, +// g +// ).unwrap(); +// let mut transform_x = transform_y.trans(40.0, 0.0); +// for offset in 0u16..16u16 { +// transform_x = transform_x.trans(20.0, 0.0); +// let addr = page + offset; +// text.draw( +// &format!("{:02x}", bus.readb(addr)), +// glyphs, +// &c.draw_state, +// transform_x, +// g +// ).unwrap(); +// } +// } +// } \ No newline at end of file diff --git a/src/nes/cpu.rs b/src/nes/cpu.rs index 86c2672..bd34154 100644 --- a/src/nes/cpu.rs +++ b/src/nes/cpu.rs @@ -454,8 +454,12 @@ impl CPU { unimplemented!() } - fn op_INC(&mut self, bus: &T, val: Word) { - unimplemented!() + fn op_INC(&mut self, bus: &mut T, addr: Word) { + let val = self.readb(bus, addr); + let val = val.wrapping_add(1); + self.writeb(bus, addr, val); + self.set_flag(ZERO, val == 0); + self.set_flag(NEGATIVE, (val & 0x80) != 0) } fn op_INX(&mut self, bus: &T) { diff --git a/src/render.rs b/src/render.rs new file mode 100644 index 0000000..b9b392a --- /dev/null +++ b/src/render.rs @@ -0,0 +1,42 @@ +use gfx_device_gl::Factory; +use gfx_device_gl::Resources; +use crate::nes::cpu::CPU; +use gfx_glyph::{Section,GlyphBrush}; + +pub trait Draw { + fn draw(&mut self, glyphs: &mut GlyphBrush); +} + +pub struct CPURenderer<'a> { + registers_text: String, + registers_section: Section<'a>, +} + +impl<'a> CPURenderer<'a> { + pub fn new(cpu: &CPU) -> Self { + let registers_text = String::from("A: 0x00 (000)"); + let registers_section = Section { + text: "xxx", + screen_position: (100.0, 100.0), + color: [1.0; 4], + ..Section::default() + }; + CPURenderer { + registers_text: registers_text, + registers_section: registers_section, + } + } +} + +impl<'a> CPURenderer<'a> { + pub fn update(&mut self, cpu: &CPU) { + self.registers_text.replace_range(3..7, "txtx"); + // self.registers_section.text = &self.registers_text; + } +} + +impl<'a> Draw for CPURenderer<'a> { + fn draw(&mut self, glyphs: &mut GlyphBrush) { + glyphs.queue(self.registers_section); + } +} \ No newline at end of file From 9b2c18f8e8bdaf3ccf26da3f827c25bb1fd0b89c Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Thu, 26 Dec 2019 13:58:14 +0100 Subject: [PATCH 2/3] use GlyphBrush for more efficient rendering --- Cargo.lock | 9 +- Cargo.toml | 6 +- src/main.rs | 423 +++++++++++++++++++++++--------------------------- src/render.rs | 42 ----- 4 files changed, 199 insertions(+), 281 deletions(-) delete mode 100644 src/render.rs diff --git a/Cargo.lock b/Cargo.lock index 2fa2b34..e1d712c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,11 +8,11 @@ dependencies = [ "gfx_core 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "gfx_device_gl 0.16.2 (registry+https://github.com/rust-lang/crates.io-index)", "gfx_glyph 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "piston2d-opengl_graphics 0.70.0 (registry+https://github.com/rust-lang/crates.io-index)", "piston_window 0.105.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "simple_logger 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -232,9 +232,10 @@ dependencies = [ [[package]] name = "crossbeam-queue" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-utils 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1104,7 +1105,7 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "crossbeam-deque 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", - "crossbeam-queue 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-queue 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-utils 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1532,7 +1533,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" "checksum crossbeam-deque 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3aa945d63861bfe624b55d153a39684da1e8c0bc8fba932f7ee3a3c16cea3ca" "checksum crossbeam-epoch 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5064ebdbf05ce3cb95e45c8b086f72263f4166b29b97f6baff7ef7fe047b55ac" -"checksum crossbeam-queue 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dfd6515864a82d2f877b42813d4553292c6659498c9a2aa31bab5a15243c2700" +"checksum crossbeam-queue 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c695eeca1e7173472a32221542ae469b3e9aac3a4fc81f7696bcad82029493db" "checksum crossbeam-utils 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce446db02cdc3165b94ae73111e570793400d0794e46125cc4056c81cbb039f4" "checksum deflate 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)" = "707b6a7b384888a70c8d2e8650b3e60170dfc6a67bb4aa67b6dfca57af4bedb4" "checksum dlib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77e51249a9d823a4cb79e3eca6dcd756153e8ed0157b6c04775d04bf1b13b76a" diff --git a/Cargo.toml b/Cargo.toml index 9dd26e5..846b935 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,9 @@ log = "0.4.*" simple_logger = "1.3.*" phf = { version = "0.8", features = ['macros'] } failure = "0.1.*" +lazy_static = "1.4.*" piston_window = "0.105.*" piston2d-opengl_graphics = "0.70.*" gfx_core = "0.9.2" -gfx_device_gl = "*" -gfx_glyph = "*" -rand = "*" \ No newline at end of file +gfx_device_gl = "0.16.*" +gfx_glyph = "0.16.*" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8072930..e3657e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ #[macro_use] extern crate log; #[macro_use] extern crate failure; +#[macro_use] extern crate lazy_static; extern crate simple_logger; extern crate phf; extern crate piston_window; mod nes; -mod render; use std::path::Path; use piston_window::*; @@ -16,20 +16,19 @@ use nes::cartridge::*; use nes::disasm::*; use opengl_graphics::OpenGL; use log::Level; -use piston_window::Text; -use render::*; - -use gfx_glyph::{Section, GlyphBrushBuilder,GlyphBrush}; -use gfx_device_gl::{Factory,Resources}; +use gfx_glyph::{Section, GlyphBrushBuilder,GlyphBrush, Scale}; +use gfx_device_gl::{Resources,Factory}; // font options -const FT_SIZE_PT: u32 = 6; -const FT_SIZE_PX: f64 = 8.0; +const FT_SIZE_PX: f32 = 11.0; const FT_COLOR_WHITE: [f32; 4] = [1.0; 4]; const FT_COLOR_RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; const FT_COLOR_GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0]; -const FT_LINE_DISTANCE: f64 = FT_SIZE_PX * 0.5; +const FT_LINE_DISTANCE: f32 = FT_SIZE_PX * 0.5; +lazy_static! { + static ref FT_SCALE: Scale = Scale::uniform(FT_SIZE_PX); +} fn main() { simple_logger::init_with_level(Level::Error).unwrap(); @@ -39,7 +38,6 @@ fn main() { let cartridge = Cartridge::new(cartridge).unwrap(); bus.insert_cartrige(cartridge); - // Load little test program into ram // let test_prog: [Byte; 28] = [0xA2, 0x0A, 0x8E, 0x00, 0x00, 0xA2, 0x03, 0x8E, 0x01, 0x00, 0xAC, 0x00, 0x00, 0xA9, // 0x00, 0x18, 0x6D, 0x01, 0x00, 0x88, 0xD0, 0xFA, 0x8D, 0x02, 0x00, 0xEA, 0xEA, 0xEA]; @@ -63,7 +61,6 @@ fn main() { // Create and reset CPU let mut cpu: CPU = CPU::new(); cpu.find_pc_addr(&bus); - // cpu.reset(&bus); // Prepare window and drawing resources @@ -73,27 +70,24 @@ fn main() { event_settings.max_fps = 60; event_settings.ups = 107400; let mut events = Events::new(event_settings); - let ft: &[u8] = include_bytes!("../resources/fonts/PressStart2P.ttf"); - let mut glyphs = GlyphBrushBuilder::using_font_bytes(ft) - .initial_cache_size((256, 256)) + // let mut glyphs = get_glyphs(&mut window); + let font: &[u8] = include_bytes!("../resources/fonts/PressStart2P.ttf"); + let mut glyph_brush: GlyphBrush = GlyphBrushBuilder::using_font_bytes(font) + .initial_cache_size((1024, 1024)) .build(window.factory.clone()); - let mut cpu_renderer = CPURenderer::new(&cpu); + // Main loop let mut run = false; while let Some(event) = events.next(&mut window) { - if let Some(_) = event.render_args() { - cpu_renderer.update(&cpu); - render(&mut window, &event, &mut glyphs, &mut cpu_renderer, &bus, &disasm); - } if let Some(_) = event.update_args() { if run { cpu.clock(&mut bus); - // while cpu.is_ahead() { - // cpu.clock(&mut bus); - // } } } + if let Some(_) = event.render_args() { + render(&mut window, &event, &mut glyph_brush, &cpu, &bus, &disasm); + } if let Some(Button::Keyboard(key)) = event.press_args() { match key { Key::C => cpu.clock(&mut bus), // advance one clock @@ -123,231 +117,196 @@ fn main() { fn render(window: &mut PistonWindow, event: &Event, glyphs: &mut GlyphBrush, - cpu_renderer: &mut CPURenderer, bus: &MemoryBus, disasm: &Disasm) { + cpu: &CPU, bus: &MemoryBus, disasm: &Disasm) { window.draw_2d(event, |c, g, d| { clear([0.0, 0.0, 1.0, 1.0], g); - // let debug_offset = [10.0, 10.0]; - // render_cpu(&c, g, glyphs, cpu, debug_offset); - // render_disasm(&c, g, glyphs, disasm, cpu.regs.pc, - // [debug_offset[0], debug_offset[1] + (7.0 * (FT_LINE_DISTANCE+FT_SIZE_PX))]); - // render_memory(&c, g, glyphs, bus, - // [debug_offset[0] + 300.0, debug_offset[1]]); + let debug_offset = [10.0, 10.0]; + render_cpu(glyphs, cpu, debug_offset); + render_disasm(glyphs, disasm, cpu.regs.pc, + [debug_offset[0], debug_offset[1] + (7.0 * (FT_LINE_DISTANCE+FT_SIZE_PX))]); + render_memory(glyphs, bus, + [debug_offset[0] + 400.0, debug_offset[1]]); - // glyphs.factory.encoder.flush(d); }); - cpu_renderer.draw(glyphs); - glyphs.use_queue().draw(&mut window.encoder, &window.output_color); + glyphs.use_queue().draw(&mut window.encoder, &window.output_color).unwrap(); window.encoder.flush(&mut window.device); } -// fn render_cpu(c: &Context, g: &mut G2d, glyphs: &mut Glyphs, cpu: &CPU, offset: [f64; 2]) { -// let mut transform = c.transform -// .trans(offset[0], offset[1] + FT_SIZE_PX); -// let text_white = Text::new_color(FT_COLOR_WHITE, FT_SIZE_PT); -// let text_on = Text::new_color(FT_COLOR_GREEN, FT_SIZE_PT); -// let text_off = Text::new_color(FT_COLOR_RED, FT_SIZE_PT); - -// let mut text = text_white; -// text.draw( -// &"Flags:", -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); +fn render_cpu(glyphs: &mut GlyphBrush, cpu: &CPU, offset: [f32; 2]) { + // draw flags + let mut position = (offset[0], offset[1] + FT_SIZE_PX); + glyphs.queue(Section { + text: "Flags:", + scale: *FT_SCALE, + screen_position: position, + color: FT_COLOR_WHITE, + ..Section::default() + }); -// transform = transform.trans(70.0, 0.0); -// text = if cpu.get_flag(NEGATIVE) == 1 { text_on } else { text_off }; -// text.draw( -// &"N", -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// transform = transform.trans(16.0, 0.0); -// text = if cpu.get_flag(OVERFLOW) == 1 { text_on } else { text_off }; -// text.draw( -// &"V", -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// transform = transform.trans(16.0, 0.0); -// text_white.draw( -// &"-", -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// transform = transform.trans(16.0, 0.0); -// text = if cpu.get_flag(BREAK) == 1 { text_on } else { text_off }; -// text.draw( -// &"B", -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// transform = transform.trans(16.0, 0.0); -// text = if cpu.get_flag(DECIMAL) == 1 { text_on } else { text_off }; -// text.draw( -// &"D", -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// transform = transform.trans(16.0, 0.0); -// text = if cpu.get_flag(IRQ) == 1 { text_on } else { text_off }; -// text.draw( -// &"I", -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// transform = transform.trans(16.0, 0.0); -// text = if cpu.get_flag(ZERO) == 1 { text_on } else { text_off }; -// text.draw( -// &"Z", -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// transform = transform.trans(16.0, 0.0); -// text = if cpu.get_flag(CARRY) == 1 { text_on } else { text_off }; -// text.draw( -// &"C", -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// transform = transform.trans(16.0, 0.0); -// text_white.draw( -// &format!("({:#4x})", cpu.regs.flags), -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); + position.0 += 70.0; + let color = if cpu.get_flag(NEGATIVE) == 1 { FT_COLOR_GREEN } else { FT_COLOR_RED }; + glyphs.queue(Section { + text: "N", + scale: *FT_SCALE, + screen_position: position, + color: color, + ..Section::default() + }); + position.0 += 16.0; + let color = if cpu.get_flag(OVERFLOW) == 1 { FT_COLOR_GREEN } else { FT_COLOR_RED }; + glyphs.queue(Section { + text: "V", + scale: *FT_SCALE, + screen_position: position, + color: color, + ..Section::default() + }); + position.0 += 16.0; + let color = FT_COLOR_WHITE; + glyphs.queue(Section { + text: "-", + scale: *FT_SCALE, + screen_position: position, + color: color, + ..Section::default() + }); + position.0 += 16.0; + let color = if cpu.get_flag(BREAK) == 1 { FT_COLOR_GREEN } else { FT_COLOR_RED }; + glyphs.queue(Section { + text: "B", + scale: *FT_SCALE, + screen_position: position, + color: color, + ..Section::default() + }); + position.0 += 16.0; + let color = if cpu.get_flag(DECIMAL) == 1 { FT_COLOR_GREEN } else { FT_COLOR_RED }; + glyphs.queue(Section { + text: "D", + scale: *FT_SCALE, + screen_position: position, + color: color, + ..Section::default() + }); + position.0 += 16.0; + let color = if cpu.get_flag(IRQ) == 1 { FT_COLOR_GREEN } else { FT_COLOR_RED }; + glyphs.queue(Section { + text: "I", + scale: *FT_SCALE, + screen_position: position, + color: color, + ..Section::default() + }); + position.0 += 16.0; + let color = if cpu.get_flag(ZERO) == 1 { FT_COLOR_GREEN } else { FT_COLOR_RED }; + glyphs.queue(Section { + text: "Z", + scale: *FT_SCALE, + screen_position: position, + color: color, + ..Section::default() + }); + position.0 += 16.0; + let color = if cpu.get_flag(CARRY) == 1 { FT_COLOR_GREEN } else { FT_COLOR_RED }; + glyphs.queue(Section { + text: "C", + scale: *FT_SCALE, + screen_position: position, + color: color, + ..Section::default() + }); + let cpu_register_texts = [ + &format!("A: {0:#x} ({0})", cpu.regs.a), + &format!("X: {0:#x} ({0})", cpu.regs.x), + &format!("Y: {0:#x} ({0})", cpu.regs.y), + &format!("Stack P: {0:#x} ({0})", cpu.regs.sp), + &format!("Program P: {:#x}", cpu.regs.pc), + ]; -// let cpu_register_texts = [ -// &format!("A: {0:#x} ({0})", cpu.regs.a), -// &format!("X: {0:#x} ({0})", cpu.regs.x), -// &format!("Y: {0:#x} ({0})", cpu.regs.y), -// &format!("Stack P: {0:#x} ({0})", cpu.regs.sp), -// &format!("Program P: {:#x}", cpu.regs.pc), -// ]; + for (i, &text) in cpu_register_texts.iter().enumerate() { + let y_offset = (i+2) as f32 * (FT_LINE_DISTANCE + FT_SIZE_PX); + let position = (offset[0], offset[1] + y_offset); + glyphs.queue(Section { + text: text, + scale: *FT_SCALE, + screen_position: position, + color: FT_COLOR_WHITE, + ..Section::default() + }); + } +} -// for (i, &text) in cpu_register_texts.iter().enumerate() { -// let y_offset = (i+2) as f64 * (FT_LINE_DISTANCE + FT_SIZE_PX); -// let transform = c.transform.trans(offset[0], offset[1] + y_offset); -// text_white.draw( -// &text, -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// } -// } +fn render_disasm(glyphs: &mut GlyphBrush, + disasm: &Disasm, pc: Addr, offset: [f32; 2]) { + let pc_position = disasm.addresses.iter().position(|&pos| pos == pc); + if let None = pc_position { + return; + } + let pc_position = pc_position.unwrap(); + let lines_above_below: usize = 12; -// fn render_disasm(c: &Context, g: &mut G2d, glyphs: &mut Glyphs, disasm: &Disasm, pc: Addr, offset: [f64; 2]) { -// let pc_position = disasm.addresses.iter().position(|&pos| pos == pc); -// if let None = pc_position { -// return; -// } -// let pc_position = pc_position.unwrap(); -// let lines_above_below: usize = 12; + let start = if (pc_position as i32 - lines_above_below as i32) < 0 { + 0 + } else { + pc_position - lines_above_below + }; -// let start = if (pc_position as i32 - lines_above_below as i32) < 0 { -// 0 -// } else { -// pc_position - lines_above_below -// }; + let text_position_x = offset[0]; + let mut text_position_y = offset[1]; + for i in (start..disasm.addresses.len()).take(lines_above_below*2+1) { + let txt = &disasm.instructions[i]; + text_position_y += FT_LINE_DISTANCE + FT_SIZE_PX; + let color = if i == pc_position { + FT_COLOR_RED + } else { + FT_COLOR_WHITE + }; + glyphs.queue(Section { + text: txt, + scale: *FT_SCALE, + screen_position: (text_position_x, text_position_y), + color: color, + ..Section::default() + }); + } +} -// let mut transform = c.transform.trans(offset[0], offset[1]); -// let text_current = Text::new_color(FT_COLOR_RED, FT_SIZE_PT); -// let text_other = Text::new_color(FT_COLOR_WHITE, FT_SIZE_PT); - -// for i in (start..disasm.addresses.len()).take(lines_above_below*2+1) { -// let addr = disasm.addresses[i]; -// let txt = &disasm.instructions[i]; - -// transform = transform.trans(0.0, FT_LINE_DISTANCE + FT_SIZE_PX); -// let text = if addr == pc { -// text_current -// } else { -// text_other -// }; -// text.draw( -// txt, -// glyphs, -// &c.draw_state, -// transform, -// g -// ).unwrap(); -// } +fn render_memory(glyphs: &mut GlyphBrush, bus: &MemoryBus, offset: [f32; 2]) { + let mut position_y = (offset[0], offset[1]); + for page in (0x0100..0x01FF).step_by(16) { + position_y.1 += FT_LINE_DISTANCE + FT_SIZE_PX; + let mut line = format!("{:#06x}:", page); + (0u16..16u16).map(|offset| offset + page) + .map(|addr| bus.readb(addr)) + .map(|val| format!(" {:02x}", val)) + .for_each(|s| line.push_str(&s)); + glyphs.queue(Section { + text: &line, + scale: *FT_SCALE, + screen_position: position_y, + color: FT_COLOR_WHITE, + ..Section::default() + }); + } - -// } - -// fn render_memory(c: &Context, g: &mut G2d, glyphs: &mut Glyphs, bus: &MemoryBus, offset: [f64; 2]) { -// let mut transform_y = c.transform.trans(offset[0], offset[1]); -// let text = Text::new_color(FT_COLOR_WHITE, FT_SIZE_PT); -// for page in (0x0000..0x01FF).step_by(16) { -// transform_y = transform_y.trans(0.0, FT_LINE_DISTANCE+FT_SIZE_PX); -// text.draw( -// &format!("{:#06x}:", page), -// glyphs, -// &c.draw_state, -// transform_y, -// g -// ).unwrap(); -// let mut transform_x = transform_y.trans(40.0, 0.0); -// for offset in 0u16..16u16 { -// transform_x = transform_x.trans(20.0, 0.0); -// let addr = page + offset; -// text.draw( -// &format!("{:02x}", bus.readb(addr)), -// glyphs, -// &c.draw_state, -// transform_x, -// g -// ).unwrap(); -// } -// } - -// transform_y = transform_y.trans(0.0, (FT_LINE_DISTANCE+FT_SIZE_PX) * 1.5); -// for page in (0x6000..0x6030).step_by(16) { -// transform_y = transform_y.trans(0.0, (FT_LINE_DISTANCE+FT_SIZE_PX)); -// text.draw( -// &format!("{:#06x}:", page), -// glyphs, -// &c.draw_state, -// transform_y, -// g -// ).unwrap(); -// let mut transform_x = transform_y.trans(40.0, 0.0); -// for offset in 0u16..16u16 { -// transform_x = transform_x.trans(20.0, 0.0); -// let addr = page + offset; -// text.draw( -// &format!("{:02x}", bus.readb(addr)), -// glyphs, -// &c.draw_state, -// transform_x, -// g -// ).unwrap(); -// } -// } -// } \ No newline at end of file + position_y.1 += FT_LINE_DISTANCE+FT_SIZE_PX * 1.5; + for page in (0x6000..0x60FF).step_by(16) { + position_y.1 += FT_LINE_DISTANCE + FT_SIZE_PX; + let mut line = format!("{:#06x}:", page); + (0u16..16u16).map(|offset| offset + page) + .map(|addr| bus.readb(addr)) + .map(|val| format!(" {:02x}", val)) + .for_each(|s| line.push_str(&s)); + glyphs.queue(Section { + text: &line, + scale: *FT_SCALE, + screen_position: position_y, + color: FT_COLOR_WHITE, + ..Section::default() + }); + } +} \ No newline at end of file diff --git a/src/render.rs b/src/render.rs deleted file mode 100644 index b9b392a..0000000 --- a/src/render.rs +++ /dev/null @@ -1,42 +0,0 @@ -use gfx_device_gl::Factory; -use gfx_device_gl::Resources; -use crate::nes::cpu::CPU; -use gfx_glyph::{Section,GlyphBrush}; - -pub trait Draw { - fn draw(&mut self, glyphs: &mut GlyphBrush); -} - -pub struct CPURenderer<'a> { - registers_text: String, - registers_section: Section<'a>, -} - -impl<'a> CPURenderer<'a> { - pub fn new(cpu: &CPU) -> Self { - let registers_text = String::from("A: 0x00 (000)"); - let registers_section = Section { - text: "xxx", - screen_position: (100.0, 100.0), - color: [1.0; 4], - ..Section::default() - }; - CPURenderer { - registers_text: registers_text, - registers_section: registers_section, - } - } -} - -impl<'a> CPURenderer<'a> { - pub fn update(&mut self, cpu: &CPU) { - self.registers_text.replace_range(3..7, "txtx"); - // self.registers_section.text = &self.registers_text; - } -} - -impl<'a> Draw for CPURenderer<'a> { - fn draw(&mut self, glyphs: &mut GlyphBrush) { - glyphs.queue(self.registers_section); - } -} \ No newline at end of file From 70249d9b3b9a450e11b51384d97aef91eea177c0 Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Thu, 26 Dec 2019 14:00:47 +0100 Subject: [PATCH 3/3] remove unused stuff in cartridge --- src/nes/cartridge.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/nes/cartridge.rs b/src/nes/cartridge.rs index f352d87..03ddf44 100644 --- a/src/nes/cartridge.rs +++ b/src/nes/cartridge.rs @@ -32,7 +32,7 @@ impl Header { f.read_exact(&mut flags)?; // Unused - f.seek(SeekFrom::Current(5)); + f.seek(SeekFrom::Current(5))?; Ok(Header { prg_rom_chunks: rom_sizes[0], @@ -69,11 +69,10 @@ impl Cartridge { let header = Header::new(&mut f)?; debug!("{:?}", header); - let mut current_pos = 0; - current_pos = f.seek(SeekFrom::Start(16))?; + f.seek(SeekFrom::Start(16))?; if header.has_trainer() { - current_pos = f.seek(SeekFrom::Current(512))?; + f.seek(SeekFrom::Current(512))?; } let mut prg_rom = vec!(0; header.prg_rom_chunks as usize * 16384);