Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-hoffman committed Jul 17, 2014
2 parents 9410842 + 8d6754c commit c4f2d22
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/com/grapeshot/halfnes/APU.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ private void setvolumes() {
private final static int[] dmcperiods = {428, 380, 340, 320, 286, 254,
226, 214, 190, 160, 142, 128, 106, 84, 72, 54};
private int dmcrate = 0x36, dmcpos = 0, dmcshiftregister = 0, dmcbuffer = 0,
dmcvalue = 0, dmcsamplelength = 0, dmcsamplesleft = 0,
dmcvalue = 0, dmcsamplelength = 1, dmcsamplesleft = 0,
dmcstartaddr = 0xc000, dmcaddr = 0xc000, dmcbitsleft = 8;
private boolean dmcsilence = true, dmcirq = false, dmcloop = false, dmcBufferEmpty = true;

Expand Down Expand Up @@ -512,7 +512,7 @@ private void dmcfillbuffer() {
if (dmcsamplesleft > 0) {
dmcbuffer = cpuram.read(dmcaddr++);
dmcBufferEmpty = false;
cpu.stealcycles(2);
cpu.stealcycles(4);
//DPCM Does steal cpu cycles - this should actually vary between 1-4
//can't do this properly without a cycle accurate cpu/ppu
if (dmcaddr > 0xffff) {
Expand Down
7 changes: 4 additions & 3 deletions src/com/grapeshot/halfnes/CPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public final class CPU {
private boolean carryFlag = false, zeroFlag = false,
interruptsDisabled = true, decimalModeFlag = false;
private boolean overflowFlag = false, negativeFlag = false,
previntflag = false, nmi = false, prevnmi = false, nmiNext = false;
previntflag = false, nmi = false, prevnmi = false;
private int pb = 0;// set to 1 if access crosses page boundary
public int interrupt = 0;
public boolean nmiNext = false;
private final static int ntscframe = 29780;
private final static boolean logging = false, decimalModeEnable = false;
//NES 6502 is missing decimal mode, but most other 6502s have it
Expand Down Expand Up @@ -61,7 +62,7 @@ public void init() {// different than reset
ram.write(0x4015, 0x00);
ram.write(0x4017, 0x00);

clocks = 27394; //correct for position we start vblank in
//clocks = 27393; //correct for position we start vblank in
A = 0;
X = 0;
Y = 0;
Expand Down Expand Up @@ -95,7 +96,7 @@ public final void runcycle(final int scanline, final int pixel) {
if (ram.apu.sprdma_count > 0) {
ram.apu.sprdma_count--;
if (ram.apu.sprdma_count == 0) {
cycles += 514;
cycles += 513;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/com/grapeshot/halfnes/NES.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class NES {
private boolean frameLimiterOn = true;
private String curRomPath, curRomName;
private final GUIInterface gui = new GUIImpl(this);
private FrameLimiterInterface limiter = new FrameLimiterImpl(this);
private final FrameLimiterInterface limiter = new FrameLimiterImpl(this);
// Pro Action Replay device
private ActionReplay actionReplay;

Expand Down Expand Up @@ -79,6 +79,9 @@ public void run() {
private synchronized void runframe() {
//the main method sequencing everything that has to happen in the nes each frame
//loops unrolled a bit to avoid some conditionals every cycle
for (int scanline = 0; scanline <= 240; ++scanline) {
runLine(scanline);
}

//run for scanlines of vblank
for (int scanline = 241; scanline < 262; ++scanline) {
Expand All @@ -95,9 +98,6 @@ private synchronized void runframe() {
cpu.modcycles();

//run cpu, ppu for active drawing time
for (int scanline = 0; scanline <= 240; ++scanline) {
runLine(scanline);
}

//render the frame
ppu.renderFrame(gui);
Expand Down
49 changes: 31 additions & 18 deletions src/com/grapeshot/halfnes/PPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PPU {
private int loopyV = 0x0;//ppu memory pointer
private int loopyT = 0x0;//temp pointer
private int loopyX = 0;//fine x scroll
private int scanline = 241;
private int scanline = 0;
private int cycles = 0;
private int framecount = 0;
private int div = 2;
Expand Down Expand Up @@ -59,11 +59,21 @@ public final int read(final int regnum) {
switch (regnum) {
case 2:
even = true;
if (scanline == 241 && cycles == 1) {
//suppress NMI flag if it was just turned on this same cycle
setvblankflag(false);
if (scanline == 241) {
if (cycles == 1) {//suppress NMI flag if it was just turned on this same cycle
setvblankflag(false);
}
//OK, uncommenting this makes blargg's NMI suppression test
//work but breaks Antarctic Adventure.
//I'm going to need a cycle accurate CPU to fix that...
// if (cycles < 4) {
// //show vblank flag but cancel pending NMI before the CPU
// //can actually do anything with it
// //TODO: use proper interface for this
// mapper.cpu.nmiNext = false;
// }
}
final int tmp = ppuregs[2];
final int tmp = (ppuregs[2] & ~0x1f) + (openbus & 0x1f);
setvblankflag(false);
openbus = tmp;
break;
Expand Down Expand Up @@ -184,17 +194,19 @@ public final void write(final int regnum, final int data) {

/**
* PPU is on if either background or sprites are enabled
* @return true
*
* @return true
*/
private boolean ppuIsOn() {
return getbit(ppuregs[1], 3) || getbit(ppuregs[1], 4);
}

/**
* MMC3 scan line counter isn't clocked if background and sprites are using
* the same half of the pattern table
* @return true if PPU is rendering and BG and sprites are
* using different pattern tables
*
* @return true if PPU is rendering and BG and sprites are using different
* pattern tables
*/
public final boolean mmc3CounterClocking() {
return (bgpattern != sprpattern) && ppuIsOn();
Expand Down Expand Up @@ -228,31 +240,32 @@ public final void clock() {
sprite0hit = false;
ppuregs[2] |= 0x40;
}
//handle nmi
if (vblankflag && getbit(ppuregs[0], 7)) {
//pull NMI line on when conditions are right
mapper.cpu.setNMI(true);
} else {
mapper.cpu.setNMI(false);
}
//handle vblank on / off
if (scanline < 240 || scanline == 261) {
//on all rendering lines
if (cycles >= 257 && cycles <= 341) {
//clear the oam address from pxls 257-341 continuously
ppuregs[3] = 0;
}
if (scanline == 261 && cycles == 1) {
if (scanline == 261 && cycles == 0) {
// turn off vblank, sprite 0, sprite overflow flags
setvblankflag(false);
ppuregs[2] &= 0x9F;
}
} else if (scanline == 241 && cycles == 1) {
setvblankflag(true);
}
//handle nmi
if (vblankflag && getbit(ppuregs[0], 7)) {
//pull NMI line on when conditions are right
mapper.cpu.setNMI(true);
} else {
mapper.cpu.setNMI(false);
}

//clock CPU, once every 3 ppu cycles
if ((++div % 3) == 0) {
div = (div + 1) % 3;
if (div == 0) {
mapper.cpu.runcycle(scanline, cycles);
}
if (cycles == 257) {
Expand Down

0 comments on commit c4f2d22

Please sign in to comment.