Skip to content

Commit

Permalink
💥 improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
GZYangKui committed Nov 28, 2023
1 parent a62f93f commit 03b1cf1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
20 changes: 19 additions & 1 deletion bin/src/main/java/cn/navclub/nes4j/bin/ppu/PPU.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.navclub.nes4j.bin.ppu;


import cn.navclub.nes4j.bin.config.NMapper;
import cn.navclub.nes4j.bin.core.Component;
import cn.navclub.nes4j.bin.NesConsole;
import cn.navclub.nes4j.bin.logging.LoggerDelegate;
Expand Down Expand Up @@ -161,7 +162,8 @@ public class PPU implements Component {
//Suppress val or nmi flag
private boolean suppress;
private long lastFrameTime;

private int CpuM2;
private int PPU_A12;

public PPU(final NesConsole console, NameMirror mirrors) {
this.console = console;
Expand All @@ -185,6 +187,8 @@ public void reset() {
this.v = 0;
this.w = 0;
this.x = 0;
this.CpuM2 = 0;
this.PPU_A12 = 0;
this.oamAddr = 0;
this.byteBuf = 0;
this.render.reset();
Expand All @@ -200,8 +204,22 @@ public void tick() {
if (this.lastFrameTime == 0) {
this.lastFrameTime = System.nanoTime();
}
if (this.PPU_A12 == 0) {
this.CpuM2 = this.CpuM2 + 1;
}
var mapper = this.console.getMapper();
for (int i = 0; i < 3; i++) {
this.render.tick();
if (mapper.type() == NMapper.MMC3) {
var tmp = (this.v >> 12) & 1;
if ((this.PPU_A12 ^ 1 ^ tmp) == 0 && this.CpuM2 >= 3) {
mapper.tick();
}
if ((tmp ^ this.PPU_A12) == 1) {
this.CpuM2 = 0;
}
this.PPU_A12 = tmp;
}
}
}

Expand Down
33 changes: 0 additions & 33 deletions bin/src/main/java/cn/navclub/nes4j/bin/ppu/Render.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,39 +332,6 @@ public void render() {
if (preLine && this.cycles >= 280 && this.cycles <= 304) {
this.ppu.v = uint16((this.ppu.v & 0x841f) | (this.ppu.t & 0x7be0));
}

var mapper = this.ppu.console.getMapper();
if (mapper.type() == NMapper.MMC3 && this.mask.enableRender()) {
var ctr = this.ppu.getCtr();
//
// When using 8x8 sprites, if the BG uses $0000, and the sprites use $1000, the IRQ counter should
// decrement on PPU cycle 260, right after the visible part of the target scanline has ended.
//

if (ctr.spriteSize() == 8 && ctr.backgroundNameTable() == 0 && ctr.spritePattern8() == 0x1000 && this.cycles == 260) {
mapper.tick();
}
// When using 8x8 sprites, if the BG uses $1000, and the sprites use $0000, the IRQ counter should decrement
// on PPU cycle 324 of the previous scanline (as in, right before the target scanline is about to be drawn).
// However, the 2C02's pre-render scanline will decrement the counter twice every other vertical redraw,
// so the IRQ will shake one scanline. This is visible in Wario's Woods: with some PPU-CPU reset alignments
// the bottom line of the green grass of the play area may flicker black on the rightmost ~48 pixels, due to
// an extra count firing the IRQ one line earlier than expected.
if (ctr.spriteSize() == 8 && ctr.backgroundNameTable() == 0x1000 && ctr.spritePattern8() == 0 & this.cycles == 324) {
if (preLine) mapper.tick();
mapper.tick();
}

//
// When using 8x16 sprites PPU A12 must be explicitly tracked. The exact time and number of times
// the counter is clocked will depend on the specific set of sprites present on every scanline.
// Specific combinations of sprites could cause the counter to decrement up to four times,
// or the IRQ to be delayed or early by some multiple of 8 pixels. If there are fewer than 8 sprites
// on a scanline, the PPU fetches tile $FF ($1FF0-$1FFF) for each leftover sprite and discards
// its value. Thus if a game uses 8x16 sprites with its background and sprites from PPU $0000, then
// the MMC3 ends up counting each scanline that doesn't use all eight sprites.
//
}
}

private void tileMut() {
Expand Down

0 comments on commit 03b1cf1

Please sign in to comment.