Skip to content

Commit

Permalink
💥 Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
GZYangKui committed Dec 10, 2023
1 parent 1e32d2d commit 392cce3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
27 changes: 12 additions & 15 deletions bin/src/main/java/cn/navclub/nes4j/bin/ppu/PPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,24 +303,21 @@ private void openBusUpdateVideoAddr() {
setBusAddr(this.v & 0x3FFF);
}


protected int iRead(int address) {
/**
* {@link Render} fetch each scanline needed data
*
* @param addr data address
* @return data
*/
protected int fetchScanlineData(int addr) {
final byte b;
//Read chr-rom data
if (address < 0x2000) {
b = this.console.getMapper().CHRead(address);
}
//Read name table data
else if (address < 0x3f00) {
this.setBusAddr(address);
b = this.vram[this.VRAMirror(address)];
}
//unknown ppu read memory
else {
if (addr < 0x2000) {
b = this.console.getMapper().CHRead(addr);
} else if (addr < 0x3f00) {
b = this.vram[this.VRAMirror(addr)];
} else {
b = 0;
log.warning("Read:unknown ppu internal address:[{}]", Integer.toHexString(address));
}

return uint8(b);
}

Expand Down
12 changes: 6 additions & 6 deletions bin/src/main/java/cn/navclub/nes4j/bin/ppu/Render.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ private void tileMut() {
*/
private void readTileIdx(int v) {
var address = 0x2000 | (v & 0x0fff);
this.tileIdx = this.ppu.iRead(address);
this.tileIdx = this.ppu.fetchScanlineData(address);
}

/**
Expand All @@ -413,7 +413,7 @@ private void readTileIdx(int v) {
*/
private void readTileAttr(int v) {
var address = 0x23c0 | (v & 0x0c00) | (v >> 4) & 0x38 | (v >> 2) & 0x07;
this.tileAttr = this.ppu.iRead(address);
this.tileAttr = this.ppu.fetchScanlineData(address);
}

/**
Expand Down Expand Up @@ -445,9 +445,9 @@ private void readTileByte(int v, boolean high) {
var table = ppu.ctr.backgroundNameTable();
var address = table + this.tileIdx * 16 + fineY;
if (!high) {
this.leftByte = this.ppu.iRead(address);
this.leftByte = this.ppu.fetchScanlineData(address);
} else {
this.rightByte = this.ppu.iRead(address + 8);
this.rightByte = this.ppu.fetchScanlineData(address + 8);
}
}

Expand Down Expand Up @@ -621,8 +621,8 @@ private void spriteEval() {

address = bank + idx * 16 + df;

var l = uint8(this.ppu.iRead(address));
var r = uint8(this.ppu.iRead(address + 8));
var l = uint8(this.ppu.fetchScanlineData(address));
var r = uint8(this.ppu.fetchScanlineData(address + 8));

//Faster copy palette data
System.arraycopy(ppu.palette, 0x11 + (attr & 0x03) * 4, this.spritePalette, 0, 3);
Expand Down

0 comments on commit 392cce3

Please sign in to comment.