Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Berti committed Mar 15, 2022
1 parent 0fa17ed commit 428920d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/main/java/omegadrive/SystemLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public SystemProvider createSystemProvider(Path file, boolean debugPerf) {
}
boolean is32x = Arrays.stream(s32xBinaryTypes).anyMatch(lowerCaseName::endsWith);
if (is32x) {
systemProvider = Md32x.createNewInstance(emuFrame, debugPerf);
systemProvider = Md32x.createNewInstance32x(emuFrame, debugPerf);
}
if (systemProvider == null) {
LOG.error("Unable to find a system to load: {}", file.toAbsolutePath());
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/omegadrive/system/Genesis.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
import omegadrive.savestate.BaseStateHandler;
import omegadrive.sound.SoundProvider;
import omegadrive.sound.javasound.AbstractSoundManager;
import omegadrive.system.perf.GenesisPerf;
import omegadrive.ui.DisplayWindow;
import omegadrive.util.RegionDetector;
import omegadrive.util.Util;
import omegadrive.vdp.model.BaseVdpProvider;
import omegadrive.vdp.model.GenesisVdpProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import sh2.Md32x;
import sh2.Md32xRuntimeData;

import static sh2.S32xUtil.CpuDeviceAccess.M68K;
Expand Down Expand Up @@ -73,8 +73,8 @@ public class Genesis extends BaseSystem<GenesisBusProvider> {
protected Ssp16 ssp16 = Ssp16.NO_SVP;
protected boolean hasSvp = ssp16 != Ssp16.NO_SVP;
protected double nextVdpCycle = vdpVals[0];
private int next68kCycle = M68K_DIVIDER;
private int nextZ80Cycle = Z80_DIVIDER;
protected int next68kCycle = M68K_DIVIDER;
protected int nextZ80Cycle = Z80_DIVIDER;

protected Genesis(DisplayWindow emuFrame) {
super(emuFrame);
Expand All @@ -85,7 +85,7 @@ public static SystemProvider createNewInstance(DisplayWindow emuFrame) {
}

public static SystemProvider createNewInstance(DisplayWindow emuFrame, boolean debugPerf) {
return debugPerf ? null : new Md32x(emuFrame);
return debugPerf ? new GenesisPerf(emuFrame) : new Genesis(emuFrame);
}

@Override
Expand Down Expand Up @@ -136,9 +136,12 @@ protected void loop() {
LOG.info("Exiting rom thread loop");
}

int cVdp;

protected final void runVdp(int counter) {
if (counter >= nextVdpCycle) {
int vdpMclk = vdp.runSlot();
cVdp++;
nextVdpCycle += vdpVals[vdpMclk - 4];
}
}
Expand All @@ -160,6 +163,7 @@ protected final void run68k(int counter) {
}
cycleDelay = Math.max(1, cycleDelay);
next68kCycle += M68K_DIVIDER * cycleDelay;
assert Md32xRuntimeData.resetCpuDelayExt() == 0;
}
}

Expand Down Expand Up @@ -226,9 +230,9 @@ private void checkSvp() {

@Override
protected void resetCycleCounters(int counter) {
nextZ80Cycle -= counter;
next68kCycle -= counter;
nextVdpCycle -= counter;
nextZ80Cycle = Math.max(1, nextZ80Cycle - counter);
next68kCycle = Math.max(1, next68kCycle - counter);
nextVdpCycle = Math.max(1, nextVdpCycle - counter);
}

@Override
Expand Down
59 changes: 31 additions & 28 deletions src/main/java/sh2/Md32x.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import omegadrive.bus.model.GenesisBusProvider;
import omegadrive.sound.PwmProvider;
import omegadrive.system.Genesis;
import omegadrive.system.SystemProvider;
import omegadrive.ui.DisplayWindow;
import omegadrive.vdp.md.GenesisVdp;
import org.apache.logging.log4j.LogManager;
Expand All @@ -31,22 +32,24 @@ public class Md32x extends Genesis {

private static final Logger LOG = LogManager.getLogger(Md32x.class.getSimpleName());

private static final boolean ENABLE_FM, ENABLE_PWM;

//23.01Mhz NTSC
protected final static int SH2_CYCLES_PER_STEP;
protected final static int SH2_CYCLE_RATIO;
//3 cycles @ 23Mhz = 1 cycle @ 7.67, 23.01/7.67 = 3
protected final static int SH2_CYCLE_RATIO = 3;
private Md32xRuntimeData rt;
private static boolean enable_fm = false, enable_pwm = true;

static {
SH2_CYCLES_PER_STEP = 3; //64;
ENABLE_FM = Boolean.parseBoolean(System.getProperty("helios.32x.fm.enable", "false"));
ENABLE_PWM = Boolean.parseBoolean(System.getProperty("helios.32x.pwm.enable", "true"));
SH2_CYCLES_PER_STEP = Integer.parseInt(System.getProperty("helios.32x.sh2.cycles", "64")); //64;
Sh2Context.burstCycles = SH2_CYCLES_PER_STEP;
//3 cycles @ 23Mhz = 1 cycle @ 7.67
SH2_CYCLE_RATIO = 3; //23.01/7.67 = 3
// System.setProperty("68k.debug", "true");
// System.setProperty("z80.debug", "true");
// System.setProperty("z80.debug", "true"); wee
// System.setProperty("sh2.master.debug", "true");
// System.setProperty("sh2.slave.debug", "true");
// enable_fm = true;
LOG.info("Enable FM: {}, Enable PWM: {}, Sh2Cycles: {}", ENABLE_FM, ENABLE_PWM, SH2_CYCLES_PER_STEP);
}

private int nextMSh2Cycle = 0, nextSSh2Cycle = 0;
Expand All @@ -73,44 +76,44 @@ protected void initAfterRomLoad() {
marsVdp.updateDebugView(((GenesisVdp) vdp).getDebugViewer());
super.initAfterRomLoad(); //needs to be last
//TODO super inits the soundProvider
ctx.pwm.setPwmProvider(enable_pwm ? sound.getPwm() : PwmProvider.NO_SOUND);
sound.setEnabled(sound.getFm(), enable_fm);
ctx.pwm.setPwmProvider(ENABLE_PWM ? sound.getPwm() : PwmProvider.NO_SOUND);
sound.setEnabled(sound.getFm(), ENABLE_FM);
}

public static SystemProvider createNewInstance32x(DisplayWindow emuFrame, boolean debugPerf) {
return debugPerf ? null : new Md32x(emuFrame);
}

@Override
protected void loop() {
LOG.info("Starting game loop");
updateVideoMode(true);
int cnt;

try {
do {
cnt = counter;
run68k(cnt);
runZ80(cnt);
runFM(cnt);
runVdp(cnt);
runSh2(cnt);
runDevices();
counter++;
} while (!futureDoneFlag);
} catch (Exception e) {
LOG.error("Error main cycle", e);
}
LOG.info("Exiting rom thread loop");
do {
cnt = counter;
run68k(cnt);
runZ80(cnt);
runFM(cnt);
runVdp(cnt);
runSh2(cnt);
runDevices();
counter++;
} while (!futureDoneFlag);
}

//PAL: 1/3.0 gives ~ 450k per frame, 22.8Mhz. but the games are too slow!!!
//53/7*burstCycles = if burstCycles = 3 -> 23.01Mhz
protected final void runSh2(int counter) {
if (nextMSh2Cycle == counter) {
rt.setAccessType(MASTER);
sh2.run(masterCtx);
nextMSh2Cycle += Math.max(1, ((masterCtx.cycles_ran + rt.resetCpuDelay()) * 5) >> 5); //5/16 ~= 1/3
nextMSh2Cycle += Math.max(1, (masterCtx.cycles_ran * 5) >> 5); //5/16 ~= 1/3
assert Md32xRuntimeData.resetCpuDelayExt() == 0;
}
if (nextSSh2Cycle == counter) {
rt.setAccessType(SLAVE);
sh2.run(slaveCtx);
nextSSh2Cycle += Math.max(1, ((slaveCtx.cycles_ran + rt.resetCpuDelay()) * 5) >> 5);
nextSSh2Cycle += Math.max(1, (slaveCtx.cycles_ran * 5) >> 5);
assert Md32xRuntimeData.resetCpuDelayExt() == 0;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/sh2/Sh2Memory.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public final class Sh2Memory implements IMemory {
public static final int START_DRAM_MODE = 0xFFFF_8000;
public static final int END_DRAM_MODE = 0xFFFF_C000;

private static final boolean SH2_ENABLE_CACHE = true;
private static final boolean SH2_ENABLE_PREFETCH = true;
private static final boolean SH2_ENABLE_CACHE = Boolean.parseBoolean(System.getProperty("helios.32x.sh2.cache", "true"));
private static final boolean SH2_ENABLE_PREFETCH = Boolean.parseBoolean(System.getProperty("helios.32x.sh2.prefetch", "true"));

public ByteBuffer[] bios = new ByteBuffer[2];
private ByteBuffer sdram;
Expand Down
67 changes: 35 additions & 32 deletions src/main/java/sh2/sh2/Sh2Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ public static final int RM(int x) {
return ((x >> 4) & 0xf);
}

// get interrupt masks bits int the SR register
private int getIMASK() {
return (ctx.SR & flagIMASK) >>> 4;
}

public void reset(Sh2Context ctx) {
Md32xRuntimeData.setAccessTypeExt(ctx.cpuAccess);
ctx.VBR = 0;
Expand All @@ -69,31 +64,6 @@ public void reset(Sh2Context ctx) {
LOG.info("{} Reset, PC: {}, SP: {}", ctx.cpuAccess, th(ctx.PC), th(ctx.registers[15]));
}

private boolean acceptInterrupts(final int level) {
if (level > getIMASK()) {
processInterrupt(ctx, level);
//TODO this should only happen for internal (ie.DMA,SCI, etc) interrupts
ctx.devices.intC.clearCurrentInterrupt(); //DoomRes1.5
return true;
}
return false;
}

private void processInterrupt(final Sh2Context ctx, final int level) {
// System.out.println(ctx.cpuAccess + " Interrupt processed: " + level);
Md32xRuntimeData.setAccessTypeExt(ctx.cpuAccess);
push(ctx.SR);
push(ctx.PC); //stores the next inst to be executed
//SR 7-4
ctx.SR &= 0xF0F;
ctx.SR |= (level << 4);

int vectorNum = ctx.devices.intC.getVectorNumber();
ctx.PC = memory.read32i(ctx.VBR + (vectorNum << 2));
//5 + 3 mem accesses
ctx.cycles -= 5;
}

//push to stack
private void push(int data) {
ctx.registers[15] -= 4;
Expand Down Expand Up @@ -1941,6 +1911,36 @@ protected final void TRAPA(int code) {
protected void printDebugMaybe(Sh2Context ctx) {
}

// get interrupt masks bits int the SR register
private int getIMASK() {
return (ctx.SR & flagIMASK) >>> 4;
}

private boolean acceptInterrupts(final int level) {
if (level > getIMASK()) {
processInterrupt(ctx, level);
//TODO this should only happen for internal (ie.DMA,SCI, etc) interrupts
ctx.devices.intC.clearCurrentInterrupt(); //DoomRes1.5
return true;
}
return false;
}

private void processInterrupt(final Sh2Context ctx, final int level) {
// System.out.println(ctx.cpuAccess + " Interrupt processed: " + level);
assert Md32xRuntimeData.getAccessTypeExt() == ctx.cpuAccess;
push(ctx.SR);
push(ctx.PC); //stores the next inst to be executed
//SR 7-4
ctx.SR &= 0xF0F;
ctx.SR |= (level << 4);

int vectorNum = ctx.devices.intC.getVectorNumber();
ctx.PC = memory.read32i(ctx.VBR + (vectorNum << 2));
//5 + 3 mem accesses
ctx.cycles -= 5;
}

/*
* Because an instruction in a delay slot cannot alter the PC we can do this.
* Perf: better to keep run() close to decode()
Expand All @@ -1951,9 +1951,12 @@ public void run(final Sh2Context ctx) {
final IntControl intControl = ctx.devices.intC;
for (; ctx.cycles >= 0; ) {
decode(memory.fetch(ctx.PC, ctx.cpuAccess));
ctx.cycles -= Md32xRuntimeData.resetCpuDelayExt(); //TODO check perf
sh2MMREG.deviceStep();
if (acceptInterrupts(intControl.getInterruptLevel())) break;
ctx.cycles -= Md32xRuntimeData.resetCpuDelayExt(); //TODO check perf
if (acceptInterrupts(intControl.getInterruptLevel())) {
ctx.cycles -= Md32xRuntimeData.resetCpuDelayExt();
break;
}
}
ctx.cycles_ran = Sh2Context.burstCycles - ctx.cycles;
ctx.cycles = Sh2Context.burstCycles;
Expand Down

0 comments on commit 428920d

Please sign in to comment.