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 7, 2023
1 parent 001b314 commit df58bb4
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ public void update(NesConsole console) {
this.x.setText(Integer.toHexString(cpu.getRx()));
this.y.setText(Integer.toHexString(cpu.getRy()));
this.a.setText(Integer.toHexString(cpu.getRa()));
this.cycles.setText(String.format("%d", cpu.getCycles()));
this.instructions.setText(Long.toString(cpu.getInstructions()));
this.stackFlag.setText("Stack:$%s".formatted(Integer.toHexString(cpu.getSp())));
this.cycles.setText(String.format("%d(+%d)", console.getCycles(), console.getDcycle()));

var length = 0xff - cpu.getSp();
if (length > 0) {
Expand Down
40 changes: 11 additions & 29 deletions bin/src/main/java/cn/navclub/nes4j/bin/NesConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ public class NesConsole {
private int tfps;
//cpu stall cycle
private int stall;
// @Getter
// private int speed;
//APU mute
@Setter
@Getter
private boolean mute;
private long cycles;
private long dcycle;
private Debugger debugger;
private long lastFrameTime;
private volatile boolean stop;
Expand Down Expand Up @@ -88,27 +84,16 @@ public void execute() {
while ((interrupt = queue.poll()) != null) {
this.stall += this.cpu.NMI_IRQ_BRKInterrupt(interrupt);
}
this.execute0();
}
}

private void execute0() {
var tmp = this.stall;
if (tmp == 0) {
var tmp = this.stall;
this.stall = 0;
while (tmp-- > 0) {
this.APU_PPuSync();
}
//Test line number has break point and block game loop
if (this.debugger != null && this.debugger.hack(this)) {
LockSupport.park();
this.dcycle = 0;
}
tmp = this.cpu.next();
this.cycles += this.cpu.getCycle();
this.dcycle += this.cpu.getCycle();
} else {
this.stall = 0;
}
while (--tmp >= 0) {
this.apu.tick();
this.ppu.tick();
this.cpu.next();
}
}

Expand Down Expand Up @@ -142,8 +127,6 @@ private void reset() {
this.fps = 0;
this.tfps = 0;
this.stall = 0;
this.dcycle = 0;
this.cycles = 0;
this.apu.reset();
this.ppu.reset();
this.cpu.reset();
Expand Down Expand Up @@ -182,7 +165,6 @@ public void videoOutput(long nano, boolean renderEnable, Frame frame) {

public void setStall(int span) {
this.stall += span;
this.dcycle += span;
}


Expand All @@ -204,15 +186,15 @@ public synchronized void release() {
LockSupport.unpark(this.thread);
}


public NMapper cartridgeMapper() {
return this.getCartridge().getMapper();
}

public int TVFps() {
return this.cartridge.getTv() == TV.NTSC ? 60 : 50;
}

public void APU_PPuSync() {
this.apu.tick();
this.ppu.tick();
}


public static class Builder {
private File file;
Expand Down
19 changes: 8 additions & 11 deletions bin/src/main/java/cn/navclub/nes4j/bin/core/CPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ public class CPU {
@Getter
//Stack pointer
private int sp;
@Getter
//Record single instruction consumer cycle
private int cycle;
//Record game execute instruction number
@Getter
private long instructions;
Expand Down Expand Up @@ -590,14 +587,14 @@ public byte getStatus() {
return status.getBits();
}

public int next() {
public void next() {
var openCode = this.bus.directRead(this.pc);
var state = (++this.pc);

var wrap = MWS6502.get(openCode);
if (wrap == null) {
logger.warning("Unknown opecode 0x{} in address 0x{}", Integer.toHexString(uint8(openCode)), Integer.toHexString(state - 1));
return 0;
return;
}
var mode = wrap.addrMode();
var instruction = wrap.instruction();
Expand All @@ -620,8 +617,6 @@ public int next() {
);
}

this.bus.reset();

switch (instruction) {
case RTI -> this.RTImpl();
case JSR -> this.JSRImpl();
Expand Down Expand Up @@ -672,18 +667,20 @@ public int next() {
case CLC, CLD, CLI, CLV -> this.CLC_D_I_VImpl(instruction);
}

this.instructions++;
this.bus._finally(wrap);

//
// Judge whether it is necessary to change the value of the program counter according to whether the
// redirection occurs
//
if (this.pc == state) {
this.pc += (wrap.size() - 1);
}
}

this.instructions++;
this.cycle = wrap.cycle() + this.bus.getBulking();

return wrap.cycle() + this.bus.calOffset();
public long getCycles() {
return this.bus.getCycles();
}

public static WS6502 IS6502Get(byte openCode) {
Expand Down
9 changes: 1 addition & 8 deletions bin/src/main/java/cn/navclub/nes4j/bin/core/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,11 @@ public final int chrSize() {
return this.cartridge.getChSize();
}

public final byte[] getRgbrom() {
return this.cartridge.getRgbrom();
}

public final byte[] getChrom() {
return this.cartridge.getChrom();
}

/**
* Some mapper implement need use extern {@link Component} cycle driver
*/
public void tick() {
public void PPUVideoAddrState(int addr) {

}
}
46 changes: 22 additions & 24 deletions bin/src/main/java/cn/navclub/nes4j/bin/core/MemoryBusAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.navclub.nes4j.bin.NesConsole;
import cn.navclub.nes4j.bin.config.AddressMode;
import cn.navclub.nes4j.bin.config.WS6502;
import lombok.Getter;


Expand All @@ -14,17 +15,16 @@
* @author <a href="https://github.com/GZYangKui">GZYangKui</a>
*/
public class MemoryBusAdapter implements Bus {
@Getter
private long cycles;
private final CPU cpu;
private int variation;
private final MemoryBus bus;
private final NesConsole console;
//Page cross product extra cycle
@Getter
private int bulking;
//Sync system other component consumer cycle
private int decrement;

public MemoryBusAdapter(CPU cpu, NesConsole console) {
this.cpu = cpu;
this.cycles = 0;
this.console = console;
this.bus = console.getBus();
}
Expand Down Expand Up @@ -90,56 +90,54 @@ public void pageCross(int base, int addr) {

@Override
public void WriteU8(int address, int value) {
this.APU_PPUSync();
this.SyncOtherComponent();
this.bus.WriteU8(address, value);
}

@Override
public int ReadU8(int address) {
this.APU_PPUSync();
this.SyncOtherComponent();
return this.bus.ReadU8(address);
}

@Override
public byte read(int address) {
this.APU_PPUSync();
this.SyncOtherComponent();
return this.bus.read(address);
}

@Override
public void write(int address, byte value) {
this.APU_PPUSync();
this.SyncOtherComponent();
this.bus.write(address, value);
}

public void increment() {
this.bulking++;
this.APU_PPUSync();
this.variation++;
this.SyncOtherComponent();
}

@Override
public int readInt(int address) {
this.APU_PPUSync();
this.SyncOtherComponent();
return this.bus.readInt(address);
}

private void APU_PPUSync() {
this.decrement--;
this.console.getPpu().tick();
this.console.getApu().tick();
}


public void reset() {
this.bulking = 0;
this.decrement = 0;
private void SyncOtherComponent() {
this.cycles++;
this.variation--;
this.console.APU_PPuSync();
}

public byte directRead(int addr) {
return this.bus.read(addr);
}

public int calOffset() {
return this.decrement + this.bulking;
protected void _finally(WS6502 ws6502) {
var tmp = ws6502.cycle() + this.variation;
while (tmp-- > 0) {
this.SyncOtherComponent();
}
this.variation = 0;
}
}
23 changes: 21 additions & 2 deletions bin/src/main/java/cn/navclub/nes4j/bin/core/impl/MMC3Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ public class MMC3Mapper extends Mapper {

private final int PRGMode;

private long a12LowCycle;

public MMC3Mapper(Cartridge cartridge, NesConsole console) {
super(cartridge, console);
this.r = 0;
this.latch = 0;
this.counter = 0;
this.a12LowCycle = 0;
this.IRQEnable = false;
this.reloadFlag = false;
this.PRGBank = new int[4];
Expand Down Expand Up @@ -267,13 +270,15 @@ public byte CHRead(int address) {
}

@Override
public void tick() {
public void PPUVideoAddrState(int addr) {
if (!this.isRisingEdge(addr)) {
return;
}
// When the IRQ is clocked (filtered A12 0→1), the counter value is checked - if zero
// or the reload flag is true, it's reloaded with the IRQ latched value at $C000; otherwise,
// it decrements.
if (this.counter == 0 || reloadFlag) {
this.counter = this.latch;
this.reloadFlag = false;
} else {
this.counter--;
}
Expand All @@ -284,5 +289,19 @@ public void tick() {
if (this.counter == 0 && this.IRQEnable) {
this.console.hardwareInterrupt(CPUInterrupt.IRQ);
}

this.reloadFlag = false;
}

private boolean isRisingEdge(int addr) {
var risingEdge = false;
var cycles = this.console.getCpu().getCycles();
if ((addr & 0x1000) == 0x1000) {
risingEdge = this.a12LowCycle > 0 && (cycles - this.a12LowCycle) >= 3;
this.a12LowCycle = 0;
} else if (a12LowCycle == 0) {
a12LowCycle = cycles;
}
return risingEdge;
}
}
Loading

0 comments on commit df58bb4

Please sign in to comment.