Skip to content

Commit

Permalink
Remove some useless testing code, fix latency issue after loading sav…
Browse files Browse the repository at this point in the history
…es multiple times
  • Loading branch information
garrettgu10 committed Aug 19, 2019
1 parent 43f7760 commit 98032e0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
18 changes: 0 additions & 18 deletions src/org/the429ers/gameboy/CPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ public CPU(MMU mem) {
mem.setCPU(this);
timer = new Timer(mem);
}

public CPU() {
mem = new MMU();
mem.setCPU(this);
timer = new Timer(mem);
}

public void setMMU(MMU mmu) {
this.mem = mmu;
Expand All @@ -55,18 +49,6 @@ public int getClockCycleDelta() {
public static final int NOJUMP = -1;
public static final int RELJUMP = 0;
public static final int ABSJUMP = 1;

public static void main(String[] args) throws IOException {
//System.setOut(new PrintStream(new File("test.out")));

new CPU().test();
}

public void test() {
while(regs.PC.read() < 256){
executeOneInstruction(true, false);
}
}

public void coreDump() {
int opcode = mem.readByte(regs.PC.read());
Expand Down
3 changes: 1 addition & 2 deletions src/org/the429ers/gameboy/GameBoy.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public void switchRom(String newRom) {
this.sourceDL = mmu.soundChip.getSourceDL();
this.romFileName = newRom;
if(mmu != null) mmu.cleanUp();
mmu = new MMU(newRom);
mmu = new MMU(newRom, sourceDL);
this.isCGB = mmu.isCGB();
cpu = new CPU(mmu);
if (isCGB) {
Expand All @@ -275,7 +275,6 @@ public void switchRom(String newRom) {
ppu = new PPU(mmu, gbs);
}
mmu.setPPU(ppu);
mmu.soundChip.setSourceDL(this.sourceDL);
cable = new LinkCable(mmu, cpu.interruptHandler);
joypad = new Joypad(mmu, cpu.interruptHandler);
gbs.addKeyListener(joypad);
Expand Down
14 changes: 9 additions & 5 deletions src/org/the429ers/gameboy/MMU.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.the429ers.gameboy;

import javax.sound.sampled.SourceDataLine;
import java.io.Serializable;
import java.util.Base64;

Expand Down Expand Up @@ -51,7 +52,7 @@ public class MMU implements Serializable {
private ColorPaletteManager spritePaletteManager;
private TileSetManager tileSetManager;
private SpriteManager spriteManager;
SoundChip soundChip = new SoundChip();
SoundChip soundChip;

public void setSpriteManager(SpriteManager manager) {
this.spriteManager = manager;
Expand Down Expand Up @@ -89,14 +90,17 @@ public Cartridge getROM() {
return this.rom;
}

public MMU() {
//no need to copy boot rom since that is intercepted by readByte
}

// Load rom from disk
public MMU(String fileName) {
this.rom = Cartridge.fromFile(fileName);
this.isCGB = rom != null && rom.isGBC();
this.soundChip = new SoundChip();
}

public MMU(String fileName, SourceDataLine sourceDL){
this.rom = Cartridge.fromFile(fileName);
this.isCGB = rom != null && rom.isGBC();
this.soundChip = new SoundChip(sourceDL);
}

public void cleanUp() {
Expand Down
13 changes: 10 additions & 3 deletions src/org/the429ers/gameboy/SoundChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SoundChip implements Serializable {
public static final int WAVE = 2;
public static final int NOISE = 3;

transient SourceDataLine sourceDL;
private transient SourceDataLine sourceDL;

byte[] masterBuffer = new byte[6 * SAMPLES_PER_FRAME];
byte[] tempBuffer = new byte[3 * SAMPLES_PER_FRAME];
Expand All @@ -44,14 +44,13 @@ class SoundChip implements Serializable {

public void setSourceDL(SourceDataLine sourceDL){
this.sourceDL = sourceDL;
this.totalSamplesWritten = sourceDL.getLongFramePosition();
}

public SourceDataLine getSourceDL(){
return this.sourceDL;
}

SoundChip() {
public SoundChip() {
try {
sourceDL = AudioSystem.getSourceDataLine(AUDIO_FORMAT);
sourceDL.open(AUDIO_FORMAT);
Expand All @@ -61,6 +60,10 @@ public SourceDataLine getSourceDL(){
}
}

public SoundChip(SourceDataLine sourceDL) {
this.sourceDL = sourceDL;
}

//handle the NR51 register
public void handleStereo(int val) {
for(int i = 0; i < 4; i++){
Expand All @@ -85,6 +88,10 @@ public void tick() {
int samplesToWrite = Math.max(0, (int)(3 * SAMPLES_PER_FRAME - residualSamples)); //try to keep 3 frames buffered at all times
samplesToWrite = Math.min(sourceDL.available() / 2, Math.min(3 * SAMPLES_PER_FRAME, samplesToWrite)); //never want to block here
totalSamplesWritten += samplesToWrite;
if(totalSamplesWritten < sourceDL.getLongFramePosition()) {
sourceDL.drain();
totalSamplesWritten = sourceDL.getLongFramePosition();
}

Arrays.fill(masterBuffer, (byte) 0);

Expand Down

0 comments on commit 98032e0

Please sign in to comment.