chore(ppu): improve code quality
This commit is contained in:
parent
7620633116
commit
8a06fe9ff8
71
src/ppu.rs
71
src/ppu.rs
|
@ -194,29 +194,46 @@ impl Ppu {
|
||||||
let window_y = self.pos.window_y;
|
let window_y = self.pos.window_y;
|
||||||
let is_window = self.control.window_enabled() && window_y <= line_y;
|
let is_window = self.control.window_enabled() && window_y <= line_y;
|
||||||
|
|
||||||
// Determine whether we need to enable sprite fetching
|
let iter = &mut self.obj_buffer.iter();
|
||||||
let mut obj_attr = None;
|
|
||||||
|
|
||||||
for attr in self.obj_buffer.iter().flatten() {
|
let obj_attr = loop {
|
||||||
|
match iter.flatten().next() {
|
||||||
|
Some(attr) => {
|
||||||
if attr.x <= (self.x_pos + 8) {
|
if attr.x <= (self.x_pos + 8) {
|
||||||
// self.fetcher.obj.resume(); TODO: Try running only when there's a sprite
|
|
||||||
self.fetcher.bg.reset();
|
self.fetcher.bg.reset();
|
||||||
self.fetcher.bg.pause();
|
self.fetcher.bg.pause();
|
||||||
self.fifo.pause();
|
self.fifo.pause();
|
||||||
|
|
||||||
obj_attr = Some(*attr);
|
break Some(*attr);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
None => break None,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// // Determine whether we need to enable sprite fetching
|
||||||
|
// let mut obj_attr = None;
|
||||||
|
|
||||||
|
// for attr in self.obj_buffer.iter().flatten() {
|
||||||
|
// if attr.x <= (self.x_pos + 8) {
|
||||||
|
// // self.fetcher.obj.resume(); TODO: Try running only when there's a sprite
|
||||||
|
// self.fetcher.bg.reset();
|
||||||
|
// self.fetcher.bg.pause();
|
||||||
|
// self.fifo.pause();
|
||||||
|
|
||||||
|
// obj_attr = Some(*attr);
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
if let Some(attr) = obj_attr {
|
if let Some(attr) = obj_attr {
|
||||||
match self.fetcher.obj.state {
|
match self.fetcher.obj.state {
|
||||||
TileNumber => {
|
TileNumber => {
|
||||||
self.fetcher.obj.tile.with_id(attr.tile_index);
|
self.fetcher.obj.tile.with_id(attr.tile_index);
|
||||||
self.fetcher.obj.next(SleepZero);
|
self.fetcher.obj.next(ToLowByteSleep);
|
||||||
}
|
}
|
||||||
SleepZero => self.fetcher.obj.next(TileDataLow),
|
ToLowByteSleep => self.fetcher.obj.next(TileLowByte),
|
||||||
TileDataLow => {
|
TileLowByte => {
|
||||||
let obj_size = self.control.obj_size().as_u8();
|
let obj_size = self.control.obj_size().as_u8();
|
||||||
|
|
||||||
let addr = PixelFetcher::get_obj_low_addr(&attr, &self.pos, obj_size);
|
let addr = PixelFetcher::get_obj_low_addr(&attr, &self.pos, obj_size);
|
||||||
|
@ -224,10 +241,10 @@ impl Ppu {
|
||||||
let byte = self.read_byte(addr);
|
let byte = self.read_byte(addr);
|
||||||
self.fetcher.obj.tile.with_low_byte(byte);
|
self.fetcher.obj.tile.with_low_byte(byte);
|
||||||
|
|
||||||
self.fetcher.obj.next(SleepOne);
|
self.fetcher.obj.next(ToHighByteSleep);
|
||||||
}
|
}
|
||||||
SleepOne => self.fetcher.obj.next(TileDataHigh),
|
ToHighByteSleep => self.fetcher.obj.next(TileHighByte),
|
||||||
TileDataHigh => {
|
TileHighByte => {
|
||||||
let obj_size = self.control.obj_size().as_u8();
|
let obj_size = self.control.obj_size().as_u8();
|
||||||
|
|
||||||
let addr = PixelFetcher::get_obj_low_addr(&attr, &self.pos, obj_size);
|
let addr = PixelFetcher::get_obj_low_addr(&attr, &self.pos, obj_size);
|
||||||
|
@ -235,9 +252,9 @@ impl Ppu {
|
||||||
let byte = self.read_byte(addr + 1);
|
let byte = self.read_byte(addr + 1);
|
||||||
self.fetcher.obj.tile.with_high_byte(byte);
|
self.fetcher.obj.tile.with_high_byte(byte);
|
||||||
|
|
||||||
self.fetcher.obj.next(SleepTwo);
|
self.fetcher.obj.next(ToFifoSleep);
|
||||||
}
|
}
|
||||||
SleepTwo => self.fetcher.obj.next(SendToFifoOne),
|
ToFifoSleep => self.fetcher.obj.next(SendToFifoOne),
|
||||||
SendToFifoOne => {
|
SendToFifoOne => {
|
||||||
// Load into Fifo
|
// Load into Fifo
|
||||||
let maybe_low = self.fetcher.obj.tile.low;
|
let maybe_low = self.fetcher.obj.tile.low;
|
||||||
|
@ -301,27 +318,27 @@ impl Ppu {
|
||||||
self.fetcher.bg.tile.with_id(id);
|
self.fetcher.bg.tile.with_id(id);
|
||||||
|
|
||||||
// Move on to the Next state in 2 T-cycles
|
// Move on to the Next state in 2 T-cycles
|
||||||
self.fetcher.bg.next(SleepZero);
|
self.fetcher.bg.next(ToLowByteSleep);
|
||||||
}
|
}
|
||||||
SleepZero => self.fetcher.bg.next(TileDataLow),
|
ToLowByteSleep => self.fetcher.bg.next(TileLowByte),
|
||||||
TileDataLow => {
|
TileLowByte => {
|
||||||
let addr = self.fetcher.bg_byte_low_addr(control, pos, is_window);
|
let addr = self.fetcher.bg_byte_low_addr(control, pos, is_window);
|
||||||
|
|
||||||
let low = self.read_byte(addr);
|
let low = self.read_byte(addr);
|
||||||
self.fetcher.bg.tile.with_low_byte(low);
|
self.fetcher.bg.tile.with_low_byte(low);
|
||||||
|
|
||||||
self.fetcher.bg.next(SleepOne);
|
self.fetcher.bg.next(ToHighByteSleep);
|
||||||
}
|
}
|
||||||
SleepOne => self.fetcher.bg.next(TileDataHigh),
|
ToHighByteSleep => self.fetcher.bg.next(TileHighByte),
|
||||||
TileDataHigh => {
|
TileHighByte => {
|
||||||
let addr = self.fetcher.bg_byte_low_addr(control, pos, is_window);
|
let addr = self.fetcher.bg_byte_low_addr(control, pos, is_window);
|
||||||
|
|
||||||
let high = self.read_byte(addr + 1);
|
let high = self.read_byte(addr + 1);
|
||||||
self.fetcher.bg.tile.with_high_byte(high);
|
self.fetcher.bg.tile.with_high_byte(high);
|
||||||
|
|
||||||
self.fetcher.bg.next(SleepTwo);
|
self.fetcher.bg.next(ToFifoSleep);
|
||||||
}
|
}
|
||||||
SleepTwo => self.fetcher.bg.next(SendToFifoOne),
|
ToFifoSleep => self.fetcher.bg.next(SendToFifoOne),
|
||||||
SendToFifoOne => {
|
SendToFifoOne => {
|
||||||
self.fetcher.bg.next(SendToFifoTwo);
|
self.fetcher.bg.next(SendToFifoTwo);
|
||||||
}
|
}
|
||||||
|
@ -840,11 +857,11 @@ impl WindowLineCounter {
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum FetcherState {
|
pub enum FetcherState {
|
||||||
TileNumber,
|
TileNumber,
|
||||||
SleepZero,
|
ToLowByteSleep,
|
||||||
TileDataLow,
|
TileLowByte,
|
||||||
SleepOne,
|
ToHighByteSleep,
|
||||||
TileDataHigh,
|
TileHighByte,
|
||||||
SleepTwo,
|
ToFifoSleep,
|
||||||
SendToFifoOne,
|
SendToFifoOne,
|
||||||
SendToFifoTwo,
|
SendToFifoTwo,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue