diff options
author | Determinant <[email protected]> | 2017-11-14 17:38:40 -0500 |
---|---|---|
committer | Determinant <[email protected]> | 2017-11-14 17:38:40 -0500 |
commit | 076dd5317af6061d916b3b119ded30dac8b5ac7e (patch) | |
tree | 6a6a5caee79f4d3a1683dc41498271d86865d83b | |
parent | dfdeb66b10c1c1af8243c475bca69839e2bdd6e8 (diff) |
fix rol ror and rendering bugs
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/memory.rs | 15 | ||||
-rw-r--r-- | src/mos6502.rs | 32 | ||||
-rw-r--r-- | src/ppu.rs | 36 |
4 files changed, 48 insertions, 38 deletions
diff --git a/src/main.rs b/src/main.rs index cef6d4c..eb327ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,7 +75,7 @@ impl SDLWindow { } } -const PIXEL_SIZE: u32 = 2; +const PIXEL_SIZE: u32 = 4; const COLORS: [u32; 64] = [ 0x666666, 0x002A88, 0x1412A7, 0x3B00A4, 0x5C007E, 0x6E0040, 0x6C0600, 0x561D00, 0x333500, 0x0B4800, 0x005200, 0x004F08, 0x00404D, 0x000000, 0x000000, 0x000000, @@ -204,6 +204,7 @@ fn main() { cnt2 = 0; cnt = 0; flag = true; + //pmem.dump(); } } else { flag = false; diff --git a/src/memory.rs b/src/memory.rs index 9b5f322..a3e9d70 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -84,7 +84,7 @@ impl<'a> VMem for CPUMemory<'a> { pub struct PPUMemory<'a> { pattern_table: RefCell<[u8; 0x2000]>, - nametable: RefCell<[u8; 0x1000]>, + nametable: RefCell<[u8; 0x800]>, palette: RefCell<[u8; 0x20]>, cart: &'a Cartridge, mapper: &'a VMem, @@ -95,11 +95,22 @@ impl<'a> PPUMemory<'a> { cart: &'a Cartridge) -> Self { PPUMemory{ pattern_table: RefCell::new([0; 0x2000]), - nametable: RefCell::new([0; 0x1000]), + nametable: RefCell::new([0; 0x800]), palette: RefCell::new([0; 0x20]), cart, mapper} } + + pub fn dump(&self) { + for (i, v) in self.palette.borrow().iter().enumerate() { + print!("{:02x} ", *v); + if (i & 0x7) == 0x7 {println!("@{:02x}", i)} + } + for (i, v) in self.nametable.borrow().iter().enumerate() { + print!("{:02x} ", *v); + if (i & 0x1f) == 0x1f {println!("@{:02x}", i)} + } + } } const MIRROR_IDX: [[u8; 4]; 5] = [ diff --git a/src/mos6502.rs b/src/mos6502.rs index 45c4166..59e17de 100644 --- a/src/mos6502.rs +++ b/src/mos6502.rs @@ -388,22 +388,19 @@ mod ops { } fn rol(cpu: &mut CPU) { - let mut status = cpu.status & !(CARRY_FLAG | ZERO_FLAG | NEG_FLAG); let res = match cpu.acc { true => { - let old = cpu.a; - let t = old.rotate_left(1); - cpu.a = t as u8; - status |= old >> 7 as u8; /* carry flag */ + let t = (cpu.a as u16) << 1; + cpu.a = t as u8 | cpu.get_carry(); t }, false => { - let old = cpu.mem.read(cpu.ea); - let t = old.rotate_left(1); - cpu.mem.write(cpu.ea, t as u8); - status |= old >> 7 as u8; /* carry flag */ + let t = (cpu.mem.read(cpu.ea) as u16) << 1; + cpu.mem.write(cpu.ea, t as u8 | cpu.get_carry()); t }}; + let mut status = cpu.status & !(CARRY_FLAG | ZERO_FLAG | NEG_FLAG); + status |= (res >> 8) as u8; /* carry flag */ check_zero!(status, res); check_neg!(status, res); cpu.status = status; @@ -414,16 +411,16 @@ mod ops { let res = match cpu.acc { true => { let old = cpu.a; - let t = old.rotate_right(1); - cpu.a = t as u8; - status |= old & 1 as u8; /* carry flag */ + let t = old >> 1; + cpu.a = t as u8 | (cpu.get_carry() << 7); + status |= (old & 1) as u8; /* carry flag */ t }, false => { let old = cpu.mem.read(cpu.ea); - let t = old.rotate_right(1); - cpu.mem.write(cpu.ea, t as u8); - status |= old & 1 as u8; /* carry flag */ + let t = old >> 1; + cpu.mem.write(cpu.ea, t as u8 | (cpu.get_carry() << 7)); + status |= (old & 1) as u8; /* carry flag */ t }}; check_zero!(status, res); @@ -751,8 +748,9 @@ impl<'a> CPU<'a> { for i in 0..len as u16 { code[i as usize] = self.mem.read(pc + i); } - println!("0x{:04x} {} a:{} x:{} y:{}", - pc, disasm::parse(opcode as u8, &code[1..]), self.a, self.x, self.y); + //println!("0x{:04x} {} a:{:02x} x:{:02x} y:{:02x} s: {:02x} sp: {:02x}", + // pc, disasm::parse(opcode as u8, &code[1..]), + // self.a, self.x, self.y, self.status, self.sp); /* update opr pointing to operands of current inst */ self.opr = pc.wrapping_add(1); /* update program counter pointing to next inst */ @@ -230,10 +230,10 @@ impl<'a> PPU<'a> { // self.bg_palette[0] >> 8 == 0 && // self.bg_palette[1] >> 8 == 0)) { // println!("at cycle {} {}", self.scanline, self.cycle) } - //assert!(self.bg_bitmap[0] >> 8 == 0 && - // self.bg_bitmap[1] >> 8 == 0 && - // self.bg_palette[0] >> 8 == 0 && - // self.bg_palette[1] >> 8 == 0); + assert!(self.bg_bitmap[0] >> 8 == 0 && + self.bg_bitmap[1] >> 8 == 0 && + self.bg_palette[0] >> 8 == 0 && + self.bg_palette[1] >> 8 == 0); self.bg_bitmap[0] |= (PPU::reverse_byte(self.bg_bit_low) as u16) << 8; self.bg_bitmap[1] |= (PPU::reverse_byte(self.bg_bit_high) as u16) << 8; self.bg_palette[0] |= (self.bg_attr & 1) as u16 * 0xff00; @@ -407,7 +407,6 @@ impl<'a> PPU<'a> { } fn render_pixel(&mut self) { - //println!("ppuctl:{} ppumask:{}", self.ppuctl, self.ppumask); let x = self.cycle - 1; let bg_pidx = if x >= 8 || self.get_show_leftmost_bg() {self.get_bg_pidx()} @@ -439,16 +438,17 @@ impl<'a> PPU<'a> { assert!(self.scanline < 240); self.scr.put((self.cycle - 1) as u8, self.scanline as u8, - if (pri == 0 || bg_pidx == 0) && sp_pidx != 0 { - self.mem.read(0x3f10 | - ((self.oam[self.sp_idx[sp_id]].attr & 3) << 2) as u16 | - sp_pidx as u16) + self.mem.read(if (pri == 0 || bg_pidx == 0) && sp_pidx != 0 { + 0x3f10 | ((self.oam[self.sp_idx[sp_id]].attr & 3) << 2) as u16 | + sp_pidx as u16 } else { - self.mem.read(0x3f00 | - ((((self.bg_palette[1] >> self.x) & 1) << 3) | - (((self.bg_palette[0] >> self.x) & 1) << 2)) as u16 | - bg_pidx as u16) - }); + 0x3f00 | match bg_pidx { + 0 => 0, + _ => ((((self.bg_palette[1] >> self.x) & 1) << 3) | + (((self.bg_palette[0] >> self.x) & 1) << 2)) as u16 | + bg_pidx as u16 + } + })); } pub fn new(mem: &'a VMem, scr: &'a Screen) -> Self { @@ -515,10 +515,6 @@ impl<'a> PPU<'a> { } else { let visible_cycle = 0 < cycle && cycle < 257; /* 1..256 */ let fetch_cycle = visible_cycle || (320 < cycle && cycle < 337); - if visible_line && visible_cycle { - self.render_pixel(); - self.shift_sprites(); - } if fetch_cycle { /* 1..256 and 321..336 */ match cycle & 0x7 { 1 => { @@ -537,6 +533,10 @@ impl<'a> PPU<'a> { 256 => self.wrapping_inc_y(), _ => () } + if visible_line && visible_cycle { + self.render_pixel(); + self.shift_sprites(); + } self.shift_bgtile(); } else if cycle > 336 { /* 337..340 */ if cycle & 1 == 1 { |