Skip to content

Commit 98032e0

Browse files
committed
Remove some useless testing code, fix latency issue after loading saves multiple times
1 parent 43f7760 commit 98032e0

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

src/org/the429ers/gameboy/CPU.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ public CPU(MMU mem) {
2525
mem.setCPU(this);
2626
timer = new Timer(mem);
2727
}
28-
29-
public CPU() {
30-
mem = new MMU();
31-
mem.setCPU(this);
32-
timer = new Timer(mem);
33-
}
3428

3529
public void setMMU(MMU mmu) {
3630
this.mem = mmu;
@@ -55,18 +49,6 @@ public int getClockCycleDelta() {
5549
public static final int NOJUMP = -1;
5650
public static final int RELJUMP = 0;
5751
public static final int ABSJUMP = 1;
58-
59-
public static void main(String[] args) throws IOException {
60-
//System.setOut(new PrintStream(new File("test.out")));
61-
62-
new CPU().test();
63-
}
64-
65-
public void test() {
66-
while(regs.PC.read() < 256){
67-
executeOneInstruction(true, false);
68-
}
69-
}
7052

7153
public void coreDump() {
7254
int opcode = mem.readByte(regs.PC.read());

src/org/the429ers/gameboy/GameBoy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public void switchRom(String newRom) {
261261
this.sourceDL = mmu.soundChip.getSourceDL();
262262
this.romFileName = newRom;
263263
if(mmu != null) mmu.cleanUp();
264-
mmu = new MMU(newRom);
264+
mmu = new MMU(newRom, sourceDL);
265265
this.isCGB = mmu.isCGB();
266266
cpu = new CPU(mmu);
267267
if (isCGB) {
@@ -275,7 +275,6 @@ public void switchRom(String newRom) {
275275
ppu = new PPU(mmu, gbs);
276276
}
277277
mmu.setPPU(ppu);
278-
mmu.soundChip.setSourceDL(this.sourceDL);
279278
cable = new LinkCable(mmu, cpu.interruptHandler);
280279
joypad = new Joypad(mmu, cpu.interruptHandler);
281280
gbs.addKeyListener(joypad);

src/org/the429ers/gameboy/MMU.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.the429ers.gameboy;
22

3+
import javax.sound.sampled.SourceDataLine;
34
import java.io.Serializable;
45
import java.util.Base64;
56

@@ -51,7 +52,7 @@ public class MMU implements Serializable {
5152
private ColorPaletteManager spritePaletteManager;
5253
private TileSetManager tileSetManager;
5354
private SpriteManager spriteManager;
54-
SoundChip soundChip = new SoundChip();
55+
SoundChip soundChip;
5556

5657
public void setSpriteManager(SpriteManager manager) {
5758
this.spriteManager = manager;
@@ -89,14 +90,17 @@ public Cartridge getROM() {
8990
return this.rom;
9091
}
9192

92-
public MMU() {
93-
//no need to copy boot rom since that is intercepted by readByte
94-
}
95-
9693
// Load rom from disk
9794
public MMU(String fileName) {
9895
this.rom = Cartridge.fromFile(fileName);
9996
this.isCGB = rom != null && rom.isGBC();
97+
this.soundChip = new SoundChip();
98+
}
99+
100+
public MMU(String fileName, SourceDataLine sourceDL){
101+
this.rom = Cartridge.fromFile(fileName);
102+
this.isCGB = rom != null && rom.isGBC();
103+
this.soundChip = new SoundChip(sourceDL);
100104
}
101105

102106
public void cleanUp() {

src/org/the429ers/gameboy/SoundChannel.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SoundChip implements Serializable {
2828
public static final int WAVE = 2;
2929
public static final int NOISE = 3;
3030

31-
transient SourceDataLine sourceDL;
31+
private transient SourceDataLine sourceDL;
3232

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

4545
public void setSourceDL(SourceDataLine sourceDL){
4646
this.sourceDL = sourceDL;
47-
this.totalSamplesWritten = sourceDL.getLongFramePosition();
4847
}
4948

5049
public SourceDataLine getSourceDL(){
5150
return this.sourceDL;
5251
}
5352

54-
SoundChip() {
53+
public SoundChip() {
5554
try {
5655
sourceDL = AudioSystem.getSourceDataLine(AUDIO_FORMAT);
5756
sourceDL.open(AUDIO_FORMAT);
@@ -61,6 +60,10 @@ public SourceDataLine getSourceDL(){
6160
}
6261
}
6362

63+
public SoundChip(SourceDataLine sourceDL) {
64+
this.sourceDL = sourceDL;
65+
}
66+
6467
//handle the NR51 register
6568
public void handleStereo(int val) {
6669
for(int i = 0; i < 4; i++){
@@ -85,6 +88,10 @@ public void tick() {
8588
int samplesToWrite = Math.max(0, (int)(3 * SAMPLES_PER_FRAME - residualSamples)); //try to keep 3 frames buffered at all times
8689
samplesToWrite = Math.min(sourceDL.available() / 2, Math.min(3 * SAMPLES_PER_FRAME, samplesToWrite)); //never want to block here
8790
totalSamplesWritten += samplesToWrite;
91+
if(totalSamplesWritten < sourceDL.getLongFramePosition()) {
92+
sourceDL.drain();
93+
totalSamplesWritten = sourceDL.getLongFramePosition();
94+
}
8895

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

0 commit comments

Comments
 (0)