| 1 | package com.rox.debug.cpu; | |
| 2 | ||
| 3 | import com.rox.debug.BeamPosition; | |
| 4 | import com.rox.cpu.mos6502.MOS6502Snapshot; | |
| 5 | import com.rox.cpu.mos6502.assembler.DisassembledInstruction; | |
| 6 | import com.rox.debug.breakpoints.BreakHit; | |
| 7 | ||
| 8 | import java.util.List; | |
| 9 | import java.util.Optional; | |
| 10 | import java.util.Locale; | |
| 11 | import java.util.OptionalInt; | |
| 12 | import java.util.Set; | |
| 13 | import java.util.regex.Matcher; | |
| 14 | import java.util.regex.Pattern; | |
| 15 | ||
| 16 | /** | |
| 17 | * One-line text renderings of CPU state, shared by every CPU view - the flagged-issue {@code state.txt} | |
| 18 | * and the live CPU state panel - so both read the same way. | |
| 19 | */ | |
| 20 | public final class CpuStateText { | |
| 21 | /** Where a line's state starts: wide enough for the longest instruction, {@code $FFFF ORA ($40),Y}. */ | |
| 22 | private static final int STATE_COLUMN = 24; | |
| 23 | /** Marks an instruction line with a breakpoint, in place of the space after the {@code >} column. */ | |
| 24 | public static final char BREAKPOINT_MARK = '\u25CF'; | |
| 25 | /** Starts the line saying what stopped the emulation - see {@link #hit}. */ | |
| 26 | public static final String HIT_PREFIX = "Stopped by: "; | |
| 27 | private static final Pattern INSTRUCTION_ADDRESS = Pattern.compile("^[> ][ " + BREAKPOINT_MARK + "]\\$([0-9A-F]{4}) "); | |
| 28 | ||
| 29 | private CpuStateText(){ | |
| 30 | } | |
| 31 | ||
| 32 | /** {@code PC: $8123 A: $01 X: $02 Y: $03 SP: $FD} */ | |
| 33 | public static String registers(final MOS6502Snapshot cpu){ | |
| 34 |
1
1. registers : replaced return value with "" for com/rox/debug/cpu/CpuStateText::registers → KILLED |
return String.format("PC: $%04X A: $%02X X: $%02X Y: $%02X SP: $%02X", |
| 35 | cpu.pc(), cpu.a(), cpu.x(), cpu.y(), cpu.stackPointer()); | |
| 36 | } | |
| 37 | ||
| 38 | /** {@code Nv-BdIzC} - the status register's {@code NV-BDIZC} bits, uppercase when set. */ | |
| 39 | public static String flags(final MOS6502Snapshot cpu){ | |
| 40 |
1
1. flags : replaced return value with "" for com/rox/debug/cpu/CpuStateText::flags → KILLED |
return new StringBuilder() |
| 41 |
1
1. flags : negated conditional → KILLED |
.append(cpu.negative() ? 'N' : 'n') |
| 42 |
1
1. flags : negated conditional → KILLED |
.append(cpu.signedOverflow() ? 'V' : 'v') |
| 43 | .append('-') | |
| 44 |
1
1. flags : negated conditional → KILLED |
.append(cpu.breakFlag() ? 'B' : 'b') |
| 45 |
1
1. flags : negated conditional → KILLED |
.append(cpu.decimal() ? 'D' : 'd') |
| 46 |
1
1. flags : negated conditional → KILLED |
.append(cpu.interruptDisable() ? 'I' : 'i') |
| 47 |
1
1. flags : negated conditional → KILLED |
.append(cpu.zero() ? 'Z' : 'z') |
| 48 |
1
1. flags : negated conditional → KILLED |
.append(cpu.carry() ? 'C' : 'c') |
| 49 | .toString(); | |
| 50 | } | |
| 51 | ||
| 52 | /** {@code IRQ line: clear NMI pending: yes} */ | |
| 53 | public static String interruptLines(final MOS6502Snapshot cpu){ | |
| 54 |
3
1. interruptLines : replaced return value with "" for com/rox/debug/cpu/CpuStateText::interruptLines → KILLED 2. interruptLines : negated conditional → KILLED 3. interruptLines : negated conditional → KILLED |
return "IRQ line: " + (cpu.irqLineAsserted() ? "asserted" : "clear") + " NMI pending: " + (cpu.nmiPending() ? "yes" : "no"); |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * {@code > $E45B JMP $E45B A:B0 X:0F Y:26 SP:FF Nv-bdIzc} - the instruction, then the state it started | |
| 59 | * with (no PC: that's the instruction's own address), plus {@code irq}/{@code nmi} when active. | |
| 60 | */ | |
| 61 | public static String instruction(final DisassembledInstruction instruction, final boolean atPc, final MOS6502Snapshot before){ | |
| 62 |
1
1. instruction : replaced return value with "" for com/rox/debug/cpu/CpuStateText::instruction → KILLED |
return String.format("%-" + STATE_COLUMN + "s%s", instruction(instruction, atPc), state(before)); |
| 63 | } | |
| 64 | ||
| 65 | /** {@code A:B0 X:0F Y:26 SP:FF Nv-bdIzc}, plus {@code irq}/{@code nmi} when active. */ | |
| 66 | public static String state(final MOS6502Snapshot cpu){ | |
| 67 |
1
1. state : replaced return value with "" for com/rox/debug/cpu/CpuStateText::state → KILLED |
return String.format("A:%02X X:%02X Y:%02X SP:%02X %s%s%s", cpu.a(), cpu.x(), cpu.y(), cpu.stackPointer(), flags(cpu), |
| 68 |
2
1. state : negated conditional → KILLED 2. state : negated conditional → KILLED |
cpu.irqLineAsserted() ? " irq" : "", cpu.nmiPending() ? " nmi" : ""); |
| 69 | } | |
| 70 | ||
| 71 | /** {@code > $8123 JMP $9000} for the instruction at the PC, {@code $8126 INX} otherwise. */ | |
| 72 | public static String instruction(final DisassembledInstruction instruction, final boolean atPc){ | |
| 73 |
2
1. instruction : replaced return value with "" for com/rox/debug/cpu/CpuStateText::instruction → KILLED 2. instruction : negated conditional → KILLED |
return (atPc ? "> " : " ") + String.format("$%04X %s", instruction.address(), instruction.formatted()); |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * {@code Breakpoint at $C123}, {@code Read $0042 = $05 by $C0F1} or {@code Write $0042 = $05 by $C0F1} - each | |
| 78 | * followed by {@code (scanline 241, dot 6)} when the beam position is known. | |
| 79 | */ | |
| 80 | public static String hit(final BreakHit hit){ | |
| 81 | final String what = switch (hit.cause()){ | |
| 82 | case BREAKPOINT -> String.format("Breakpoint at $%04X", hit.address()); | |
| 83 | case READ -> String.format("Read $%04X = $%02X by $%04X", hit.address(), hit.value(), hit.pc()); | |
| 84 | case WRITE -> String.format("Write $%04X = $%02X by $%04X", hit.address(), hit.value(), hit.pc()); | |
| 85 | }; | |
| 86 |
2
1. hit : replaced return value with "" for com/rox/debug/cpu/CpuStateText::hit → KILLED 2. hit : negated conditional → KILLED |
return hit.beam().isKnown() |
| 87 | ? what + String.format(" (scanline %d, dot %d)", hit.beam().scanline(), hit.beam().dot()) | |
| 88 | : what; | |
| 89 | } | |
| 90 | ||
| 91 | /** {@code Scanline: 241 Dot: 6} - where the PPU is drawing, as a flagged issue's {@code state.txt} puts it. */ | |
| 92 | public static String beam(final BeamPosition beam){ | |
| 93 |
1
1. beam : replaced return value with "" for com/rox/debug/cpu/CpuStateText::beam → KILLED |
return String.format("Scanline: %d Dot: %d", beam.scanline(), beam.dot()); |
| 94 | } | |
| 95 | ||
| 96 | /** The address of the instruction on a {@link #panel} line, if it's an instruction line. */ | |
| 97 | public static OptionalInt instructionAddress(final String line){ | |
| 98 | final Matcher address = INSTRUCTION_ADDRESS.matcher(line); | |
| 99 |
2
1. instructionAddress : negated conditional → KILLED 2. instructionAddress : replaced return value with null for com/rox/debug/cpu/CpuStateText::instructionAddress → KILLED |
return address.find() ? OptionalInt.of(Integer.parseInt(address.group(1), 16)) : OptionalInt.empty(); |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * {@code Clock: 1,789,773 Hz of 1,789,773 Hz (100.0%)} - below 100% while a game slows down means the | |
| 104 | * emulator can't keep up; near 100% means the slowdown is the game's own. | |
| 105 | */ | |
| 106 | public static String clockRate(final double measuredHz, final long intendedHz){ | |
| 107 | //Locale.ROOT: the same ',' grouping and '.' decimal point whatever the machine's locale | |
| 108 |
3
1. clockRate : Replaced double multiplication with division → KILLED 2. clockRate : replaced return value with "" for com/rox/debug/cpu/CpuStateText::clockRate → KILLED 3. clockRate : Replaced double division with multiplication → KILLED |
return String.format(Locale.ROOT, "Clock: %,.0f Hz of %,d Hz (%.1f%%)", measuredHz, intendedHz, 100.0 * measuredHz / intendedHz); |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * The whole CPU state view as text: run state and clock rate, registers, flags, interrupt lines, | |
| 113 | * then the instructions on one time axis running bottom to top - the oldest recent instruction at | |
| 114 | * the bottom, the latest above it, then the current one ({@code >}) and the next | |
| 115 | * {@code upcomingCount - 1} above that, furthest ahead at the top. Lines with a breakpoint carry | |
| 116 | * {@link #BREAKPOINT_MARK}; while something has stopped the emulation, a {@link #HIT_PREFIX} line says what. | |
| 117 | */ | |
| 118 | public static String panel(final CpuDebugSource source, final int upcomingCount){ | |
| 119 |
1
1. panel : replaced return value with "" for com/rox/debug/cpu/CpuStateText::panel → KILLED |
return panel(source.state(), source, upcomingCount); |
| 120 | } | |
| 121 | ||
| 122 | /** As {@link #panel(CpuDebugSource, int)}, for a CPU state already read - so a caller can use the same read elsewhere. */ | |
| 123 | public static String panel(final MOS6502Snapshot cpu, final CpuDebugSource source, final int upcomingCount){ | |
| 124 | final StringBuilder out = new StringBuilder(); | |
| 125 |
1
1. panel : negated conditional → KILLED |
out.append(source.isPaused() ? "PAUSED" : "Running").append(" ") |
| 126 | .append(clockRate(source.measuredHz(), source.intendedHz())).append('\n'); | |
| 127 |
1
1. panel : removed call to java/util/Optional::ifPresent → KILLED |
source.lastHit().ifPresent(hit -> out.append(HIT_PREFIX).append(hit(hit)).append('\n')); |
| 128 | final Set<Integer> breakpoints = source.breakpoints().breakpoints(); | |
| 129 | out.append(registers(cpu)).append('\n'); | |
| 130 | out.append("Flags: ").append(flags(cpu)).append(" (NV-BDIZC)\n"); | |
| 131 | out.append(interruptLines(cpu)).append('\n'); | |
| 132 | out.append(beam(source.beam())).append("\n\n"); | |
| 133 | out.append("--- Next (furthest ahead at top) ---\n"); | |
| 134 | final List<DisassembledInstruction> upcoming = source.upcomingInstructions(upcomingCount); | |
| 135 | final Optional<MOS6502Snapshot> currentState = source.currentInstructionState(); | |
| 136 |
3
1. panel : Replaced integer subtraction with addition → KILLED 2. panel : changed conditional boundary → KILLED 3. panel : negated conditional → KILLED |
for (int i = upcoming.size() - 1; i >= 0; i--){ |
| 137 | //only the current instruction has a state - the rest haven't run yet | |
| 138 |
2
1. panel : negated conditional → KILLED 2. panel : negated conditional → KILLED |
final String line = i == 0 && currentState.isPresent() |
| 139 | ? instruction(upcoming.get(i), true, currentState.get()) | |
| 140 |
1
1. panel : negated conditional → KILLED |
: instruction(upcoming.get(i), i == 0); |
| 141 | out.append(marked(line, breakpoints.contains(upcoming.get(i).address()))).append('\n'); | |
| 142 | } | |
| 143 | out.append("--- Recent (latest at top) ---\n"); | |
| 144 | final List<TracedInstruction> recent = source.recentInstructions(); | |
| 145 |
3
1. panel : Replaced integer subtraction with addition → KILLED 2. panel : changed conditional boundary → KILLED 3. panel : negated conditional → KILLED |
for (int i = recent.size() - 1; i >= 0; i--){ |
| 146 | final DisassembledInstruction instruction = recent.get(i).instruction(); | |
| 147 | out.append(marked(instruction(instruction, false, recent.get(i).before()), breakpoints.contains(instruction.address()))) | |
| 148 | .append('\n'); | |
| 149 | } | |
| 150 |
1
1. panel : replaced return value with "" for com/rox/debug/cpu/CpuStateText::panel → KILLED |
return out.toString(); |
| 151 | } | |
| 152 | ||
| 153 | private static String marked(final String line, final boolean breakpoint){ | |
| 154 |
2
1. marked : replaced return value with "" for com/rox/debug/cpu/CpuStateText::marked → KILLED 2. marked : negated conditional → KILLED |
return breakpoint ? line.charAt(0) + String.valueOf(BREAKPOINT_MARK) + line.substring(2) : line; |
| 155 | } | |
| 156 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 40 |
1.1 |
|
| 41 |
1.1 |
|
| 42 |
1.1 |
|
| 44 |
1.1 |
|
| 45 |
1.1 |
|
| 46 |
1.1 |
|
| 47 |
1.1 |
|
| 48 |
1.1 |
|
| 54 |
1.1 2.2 3.3 |
|
| 62 |
1.1 |
|
| 67 |
1.1 |
|
| 68 |
1.1 2.2 |
|
| 73 |
1.1 2.2 |
|
| 86 |
1.1 2.2 |
|
| 93 |
1.1 |
|
| 99 |
1.1 2.2 |
|
| 108 |
1.1 2.2 3.3 |
|
| 119 |
1.1 |
|
| 125 |
1.1 |
|
| 127 |
1.1 |
|
| 136 |
1.1 2.2 3.3 |
|
| 138 |
1.1 2.2 |
|
| 140 |
1.1 |
|
| 145 |
1.1 2.2 3.3 |
|
| 150 |
1.1 |
|
| 154 |
1.1 2.2 |