Skip to content

Commit 687efa2

Browse files
committed
add chr_rom debug
1 parent df763bd commit 687efa2

4 files changed

Lines changed: 351 additions & 12 deletions

File tree

Cargo.lock

Lines changed: 263 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ edition = "2021"
55

66
[dependencies]
77
anyhow = "1.0.100"
8+
macroquad = "0.4.14"

src/main.rs

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,98 @@
11
mod cpu;
22
mod nes_rom;
3-
mod ram;
43
mod ppu;
4+
mod ram;
55

6-
use cpu::cpu_memory::CpuMemory;
76
use crate::nes_rom::NesRom;
7+
use crate::ppu::Ppu;
8+
use cpu::cpu_memory::CpuMemory;
89
use cpu::Cpu;
10+
use macroquad::prelude::*;
11+
12+
const RENDER_SCALE: f32 = 4.;
13+
const TILE_SIZE: f32 = RENDER_SCALE * 8.;
14+
const COLORS: [Color; 4] = [BLACK, DARKGRAY, LIGHTGRAY, WHITE];
915

10-
fn main() -> Result<(), anyhow::Error> {
16+
#[macroquad::main("emurs")]
17+
async fn main() -> Result<(), anyhow::Error> {
1118
println!("Starting Emulator!");
1219

13-
let rom = NesRom::read_from_file("./vendor/nestest/nestest.nes")?;
20+
let rom = NesRom::read_from_file("./tetris.nes")?;
1421
println!("{rom:#?}");
1522

16-
let memory_map = CpuMemory::new(rom);
23+
let memory_map = CpuMemory::new(rom.clone());
1724
println!("Entry point: {:#X}", memory_map.reset_vector());
1825

1926
let mut cpu = Cpu::with_nes_options(memory_map, 1 << 16);
20-
// cpu.run();
27+
28+
debug_chr_rom(rom).await;
2129

2230
Ok(())
2331
}
2432

33+
async fn debug_chr_rom(rom: NesRom) {
34+
request_new_screen_size(
35+
RENDER_SCALE * (2 * 8 * 16) as f32,
36+
RENDER_SCALE * (8 * 16) as f32,
37+
);
38+
39+
let chr_rom = rom.chr_rom;
40+
loop {
41+
fn calc_screen_pos(tile_index: usize, pixel_index: usize) -> (f32, f32) {
42+
let tile_x = (tile_index % 16) as f32;
43+
let tile_y = (tile_index / 16) as f32;
44+
let base_x = tile_x * TILE_SIZE;
45+
let base_y = tile_y * TILE_SIZE;
46+
let pixel_x = (pixel_index % 8) as f32;
47+
let pixel_y = (pixel_index / 8) as f32;
48+
let screen_x = base_x + pixel_x * RENDER_SCALE;
49+
let screen_y = base_y + pixel_y * RENDER_SCALE;
50+
(screen_x, screen_y)
51+
}
52+
53+
for tile in 0..256 {
54+
let chr_data = chr_rom[tile * 16..(tile + 1) * 16].to_vec();
55+
let pixels = chr_data_to_pixels(chr_data);
56+
for i in 0..pixels.len() {
57+
let (screen_x, screen_y) = calc_screen_pos(tile, i);
58+
draw_rectangle(
59+
screen_x,
60+
screen_y,
61+
RENDER_SCALE,
62+
RENDER_SCALE,
63+
COLORS[pixels[i] as usize],
64+
)
65+
}
66+
}
67+
for tile in 256..512 {
68+
let chr_data = chr_rom[tile * 16..(tile + 1) * 16].to_vec();
69+
let pixels = chr_data_to_pixels(chr_data);
70+
for i in 0..pixels.len() {
71+
let (mut screen_x, mut screen_y) = calc_screen_pos(tile, i);
72+
screen_x += TILE_SIZE * 16.;
73+
screen_y -= TILE_SIZE * 16.;
74+
draw_rectangle(
75+
screen_x,
76+
screen_y,
77+
RENDER_SCALE,
78+
RENDER_SCALE,
79+
COLORS[pixels[i] as usize],
80+
)
81+
}
82+
}
83+
84+
next_frame().await;
85+
}
86+
}
87+
88+
fn chr_data_to_pixels(chr_data: Vec<u8>) -> Vec<u8> {
89+
let mut pixels = Vec::with_capacity(64);
90+
for byte in 0..8 {
91+
for bit in (0..8).rev() {
92+
let pixel_value =
93+
(chr_data[byte] >> bit & 1) | (((chr_data[byte + 8] >> bit) & 1) << 1);
94+
pixels.push(pixel_value);
95+
}
96+
}
97+
pixels
98+
}

0 commit comments

Comments
 (0)