Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ file is where they live.
moment rather than a dozen.
- A debugger with breakpoints (conditions included, like `$C000 if a == $10`), read/write
watchpoints, single stepping, a syntax-highlighted disassembly, the stack, and a hex view of
the address space.
the address space. A ca65/ld65 build can be attached from the Source tab with **Attach Debug Info…**: build
objects with `ca65 -g`, link with `ld65 --dbgfile game.dbg`, and the debugger follows the current
source line in a syntax-highlighted 6502 editor, shows symbols in the disassembly, and sets
breakpoints from the source gutter. Those breakpoints name bytes of PRG ROM rather than
`$8000`-style windows, so they follow the right code through bank switches. If the checkout moved
since the build, **Find Sources…** accepts either the project or a source folder and also finds
generated includes in nearby build folders. Hovering a symbol shows its linked value and source
definition; Cmd/Ctrl-click or **Go to Definition** follows it, including ca65's `@` local labels.
- CPU tracing in nestest's log format, so a trace diffs cleanly against other emulators.
- **Start Music...** writes what the sound chip plays to a MIDI file — one part per pulse and one
for the triangle, read a frame at a time so nothing in the melody is missed, with the tempo set so
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.dimiro1.mynes.MMU;
import com.github.dimiro1.mynes.NES;
import com.github.dimiro1.mynes.PPU;
import com.github.dimiro1.mynes.mappers.Mapper;

import java.util.Collections;
import java.util.Locale;
Expand Down Expand Up @@ -261,7 +262,18 @@ public interface EventSink {
private final boolean[] watchReadAt = new boolean[0x10000];
private final boolean[] watchWriteAt = new boolean[0x10000];

/**
* Breakpoints on bytes of the program ROM rather than on CPU addresses.
* <p>
* A CPU address is not the identity of a line of code on a banked cartridge: half a dozen
* different banks may all spend part of a frame at $8000. The offset is. Kept as the same flat
* lookup the address breakpoints use, because this is asked once per instruction while any one
* of these is down.
*/
private boolean[] breakAtPRG = new boolean[0];

private final Set<Integer> breakpoints = new TreeSet<>();
private final Set<Integer> prgBreakpoints = new TreeSet<>();

/**
* The conditional breakpoints only, which is nearly always none of them.
Expand Down Expand Up @@ -296,6 +308,7 @@ public interface EventSink {
private MMU memory;
private CPU cpu;
private PPU ppu;
private Mapper mapper;

/**
* Whoever is collecting a frame's worth of what the machine did, or null when nobody is.
Expand Down Expand Up @@ -347,6 +360,15 @@ public void attach(final NES nes) {
memory = nes.getMemory();
cpu = nes.getCPU();
ppu = nes.getPPU();
mapper = nes.getBus().getMapper();

// A debugger outlives a power cycle, so rebuild the fast lookup over the new cartridge
// without throwing away the user's points. A genuinely new cartridge is cleared by the
// window before it is attached; discard impossible points for direct callers too.
breakAtPRG = new boolean[nes.getCart().prgROM().length];
prgBreakpoints.removeIf(offset -> offset >= breakAtPRG.length);
prgBreakpoints.stream()
.forEach(offset -> breakAtPRG[offset] = true);

wireHooks();
}
Expand All @@ -364,6 +386,7 @@ public boolean isArmed() {
return stepping != Stepping.NONE
|| haltAsked
|| !breakpoints.isEmpty()
|| !prgBreakpoints.isEmpty()
|| !watchpoints.isEmpty();
}

Expand Down Expand Up @@ -406,6 +429,16 @@ public Stop afterInstruction(final int pc, final int wasPC) {
return new Stop(Reason.BREAKPOINT, pc, null, -1, -1, -1);
}

if (pc >= 0x8000 && !prgBreakpoints.isEmpty()) {
var offset = mapper.prgOffset(pc);

if (offset >= 0 && offset < breakAtPRG.length && breakAtPRG[offset]) {
stepping = Stepping.NONE;

return new Stop(Reason.BREAKPOINT, pc, null, -1, -1, -1);
}
}

if (stepping == Stepping.INSTRUCTION) {
stepping = Stepping.NONE;

Expand Down Expand Up @@ -580,6 +613,39 @@ public boolean toggleBreakpoint(final int pc) {
return true;
}

/**
* Stops before whichever CPU address is currently backed by this byte of program ROM.
* <p>
* This is the breakpoint a source line wants. On a banked board it follows the code rather
* than the window the code happens to be showing through; on a mirrored NROM it deliberately
* fires through either alias, since both execute the same byte.
*/
public void addPRGBreakpoint(final int offset) {
checkPRGOffset(offset);
prgBreakpoints.add(offset);
breakAtPRG[offset] = true;
}

public void removePRGBreakpoint(final int offset) {
prgBreakpoints.remove(offset);

if (offset >= 0 && offset < breakAtPRG.length) {
breakAtPRG[offset] = false;
}
}

public boolean togglePRGBreakpoint(final int offset) {
if (prgBreakpoints.contains(offset)) {
removePRGBreakpoint(offset);

return false;
}

addPRGBreakpoint(offset);

return true;
}

/**
* Stops the machine after an instruction writes to this address.
*/
Expand Down Expand Up @@ -713,12 +779,18 @@ public void unwatched(final Runnable work) {
*/
public void clear() {
breakpoints.forEach(pc -> breakAt[pc] = false);
prgBreakpoints.forEach(offset -> {
if (offset < breakAtPRG.length) {
breakAtPRG[offset] = false;
}
});
watchpoints.keySet().forEach(address -> {
watchReadAt[address] = false;
watchWriteAt[address] = false;
});

breakpoints.clear();
prgBreakpoints.clear();
conditions.clear();
watchpoints.clear();

Expand All @@ -732,6 +804,13 @@ public Set<Integer> breakpoints() {
return Collections.unmodifiableSet(breakpoints);
}

/**
* Breakpoints whose identity is an offset into {@link com.github.dimiro1.mynes.Cart#prgROM()}.
*/
public Set<Integer> prgBreakpoints() {
return Collections.unmodifiableSet(prgBreakpoints);
}

/**
* The conditions, by the address they are on. Only the conditional breakpoints are in here; an
* address in {@link #breakpoints()} and not in this stops on every pass.
Expand Down Expand Up @@ -785,6 +864,15 @@ private boolean conditionHolds(final int pc) {
return condition == null || condition.holds(cpu.getState(), memory::peek);
}

private void checkPRGOffset(final int offset) {
if (offset < 0 || offset >= breakAtPRG.length) {
throw new IllegalArgumentException(String.format(
"PRG offset $%X is outside this cartridge's $%X bytes",
offset,
breakAtPRG.length));
}
}

/**
* Puts the three hooks down or picks them up, according to whether anything is still watching.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,50 @@ void aBreakpointSomewhereTheProgramNeverGoesNeverFires() {
assertNull(run(20));
}

@Test
void aProgramRomBreakpointStopsAtTheByteBehindTheCpuAddress() {
debugger.addPRGBreakpoint(8);

var stop = run(10);

assertNotNull(stop);
assertEquals(LDX, stop.pc());
assertEquals(java.util.Set.of(8), debugger.prgBreakpoints());
}

@Test
void aProgramRomBreakpointArmsAndCanBeRemoved() {
debugger.addPRGBreakpoint(8);

assertTrue(debugger.isArmed());

debugger.removePRGBreakpoint(8);

assertFalse(debugger.isArmed());
assertNull(run(20));
}

@Test
void aProgramRomBreakpointFollowsTheBankRatherThanTheCpuAddress() {
var banked = new NES(Cart.load(mapper2ROM(), "banked-debugger.nes"));
var watching = new Debugger();

watching.attach(banked);
watching.addPRGBreakpoint(0);

assertNotNull(watching.afterInstruction(0x8000, 0xC000),
"bank zero initially occupies $8000");

banked.getMemory().write(0x8000, 1);

assertNull(watching.afterInstruction(0x8000, 0xC000),
"the same CPU address now names bank one");

watching.addPRGBreakpoint(0x4000);
assertNotNull(watching.afterInstruction(0x8000, 0xC000),
"and a point on bank one follows it into the window");
}

@Test
void aRemovedBreakpointStopsStopping() {
debugger.addBreakpoint(LDX);
Expand Down Expand Up @@ -309,11 +353,13 @@ void theTrailRemembersWhatActuallyRan() {
@Test
void clearingForgetsEveryPoint() {
debugger.addBreakpoint(LDX);
debugger.addPRGBreakpoint(8);
debugger.addWatchpoint(0x0300);

debugger.clear();

assertEquals(java.util.Set.of(), debugger.breakpoints());
assertEquals(java.util.Set.of(), debugger.prgBreakpoints());
assertEquals(java.util.Map.of(), debugger.watchpoints());
assertFalse(debugger.isArmed());
assertNull(run(20));
Expand Down Expand Up @@ -464,4 +510,19 @@ private static byte[] rom() {

return image;
}

/** A two-bank UxROM image: bank zero is switchable at $8000, bank one fixed at $C000. */
private static byte[] mapper2ROM() {
var image = new byte[16 + 0x8000 + 0x2000];

image[0] = 'N';
image[1] = 'E';
image[2] = 'S';
image[3] = 0x1A;
image[4] = 2;
image[5] = 1;
image[6] = 0x20;

return image;
}
}
4 changes: 4 additions & 0 deletions mynes-desktop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
<groupId>com.miglayout</groupId>
<artifactId>miglayout-swing</artifactId>
</dependency>
<dependency>
<groupId>com.fifesoft</groupId>
<artifactId>rsyntaxtextarea</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ public void setMachine(
observeTheMachine();

if (debugger == null) {
debugger = new DebuggerPanel(nes, runner, points);
debugger = new DebuggerPanel(nes, runner, points, cart);
debugger.installKeysIn(getRootPane());
} else {
debugger.setMachine(nes, runner);
debugger.setMachine(nes, runner, cart);
}

var inFront = tabs.getSelectedIndex();
Expand Down
Loading
Loading