|
1 | 1 | mod cpu; |
2 | 2 | mod nes_rom; |
3 | | -mod ram; |
4 | 3 | mod ppu; |
| 4 | +mod ram; |
5 | 5 |
|
6 | | -use cpu::cpu_memory::CpuMemory; |
7 | 6 | use crate::nes_rom::NesRom; |
| 7 | +use crate::ppu::Ppu; |
| 8 | +use cpu::cpu_memory::CpuMemory; |
8 | 9 | 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]; |
9 | 15 |
|
10 | | -fn main() -> Result<(), anyhow::Error> { |
| 16 | +#[macroquad::main("emurs")] |
| 17 | +async fn main() -> Result<(), anyhow::Error> { |
11 | 18 | println!("Starting Emulator!"); |
12 | 19 |
|
13 | | - let rom = NesRom::read_from_file("./vendor/nestest/nestest.nes")?; |
| 20 | + let rom = NesRom::read_from_file("./tetris.nes")?; |
14 | 21 | println!("{rom:#?}"); |
15 | 22 |
|
16 | | - let memory_map = CpuMemory::new(rom); |
| 23 | + let memory_map = CpuMemory::new(rom.clone()); |
17 | 24 | println!("Entry point: {:#X}", memory_map.reset_vector()); |
18 | 25 |
|
19 | 26 | let mut cpu = Cpu::with_nes_options(memory_map, 1 << 16); |
20 | | - // cpu.run(); |
| 27 | + |
| 28 | + debug_chr_rom(rom).await; |
21 | 29 |
|
22 | 30 | Ok(()) |
23 | 31 | } |
24 | 32 |
|
| 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