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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Look for license phrase
run: |
outfile=$(mktemp)
find . -name "*.java" -not -name "module-info.java" | xargs grep -L "GNU General Public License" > ${outfile}
./scripts/license-check.sh > ${outfile}
if [ -s "${outfile}" ]
then
echo "License headers missing on the following files:"
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ bin/
/*.bin
.gradle/
/build/
/.idea
/.idea
/*.data
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ The disassembler can be used in any Java application via the [API](https://apple
$ acdasm --help
Usage: acdasm [-hV] [--debug] [--[no-]descriptions] [--[no-]labels]
[-a=<startAddress>] [-n=<length>] [--offset=<offset>]
[-l=<library>[,<library>...]]... [--6502 | --sweet16 | --6502s |
--6502x | --65c02 | --pcode | --z80 | --codefile] <file>
[--code=<entryPoints>[,<entryPoints>...]]... [-l=<library>[,
<library>...]]... [--6502 | --sweet16 | --6502s | --6502x |
--65c02 | --pcode | --z80 | --codefile] <file>

AppleCommander Disassembler.

Expand All @@ -25,6 +26,10 @@ AppleCommander Disassembler.
Options:
-a, --addr, --origin=<startAddress>
Set start address for application.
--code=<entryPoints>[,<entryPoints>...]
Code entry point. Enables data detection. If this
is specified, beginning of file is NOT
automatically entered.
--debug Print stack traces
--[no-]descriptions Include opcode descriptions.
-h, --help Show this help message and exit.
Expand All @@ -51,12 +56,12 @@ Processor Defaults:
Default Value 6502 6502X 6502S 65C02 SWEET-16 Z80 P-CODE
--------------------- ----- ----- ----- ----- -------- ----- ------
Start Address $0300 $0300 $0300 $0300 $0300 0100H $0000
Library Labels All All All All All None None
Library Labels All All All All All CPM None
Max Bytes/Instruction 3 3 3 3 3 5 8
Show Descriptions? No No No No No No Yes

Library Groups:
DISKII, DOS, F800, Applesoft, ProDOS, Softswitches
CPM, F800, ProDOS, DISKII, DOS, Applesoft, Softswitches
```

Sample runs:
Expand Down
165 changes: 148 additions & 17 deletions api/src/main/java/org/applecommander/disassembler/api/Disassembler.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,26 @@ public static Set<String> labelGroups() {
return ADDRESSES.keySet();
}

private final int bytesToSkip;
private final int bytesToDecode;
private final InstructionSet instructionSet;
private final List<Integer> entryPoints;
private final BitSet dataFlags;
private final List<Integer> labelAddresses;
private int startAddress;
private int bytesToSkip;
private int bytesToDecode;
private byte[] code;
private InstructionSet instructionSet;

public Disassembler(int startAddress, int bytesToSkip, int bytesToDecode, byte[] code,
InstructionSet instructionSet, List<Integer> entryPoints) {
this.startAddress = startAddress;
this.bytesToSkip = bytesToSkip;
this.bytesToDecode = bytesToDecode;
this.code = code;
this.instructionSet = instructionSet;
this.entryPoints = Collections.unmodifiableList(entryPoints);
this.dataFlags = new BitSet(code.length);
this.labelAddresses = new ArrayList<>();
}

/** Initiate the disassembly. */
public static Builder with(byte[] code) {
Expand All @@ -79,29 +94,138 @@ private List<Instruction> decode(Map<Integer,String> labels) {
startAddress+= bytesToSkip;
}

// Assume all is code
dataFlags.clear(0, code.length);
Program program = new Program(code,startAddress);
List<Instruction> assembly = instructionSet.decode(program);
if (!entryPoints.isEmpty()) {
// If we have entry points, we need to scan the program and identify data versus code.
prescan(program);
program.currentAddress(program.baseAddress());
}
List<Instruction> assembly = new ArrayList<>();
while (program.hasMore()) {
if (dataFlags.get(program.currentOffset())) {
assembly.add(parseData(program));
} else {
assembly.add(instructionSet.decodeOne(program));
}
}

// Gather all the instructions and identify all target addresses
assembly.forEach(instruction -> {
instruction.addressRef().flatMap(Instruction.Operand::address).ifPresent(address -> {
if ((address >= startAddress) && (address < startAddress + code.length)) {
instruction.addressRef().flatMap(Instruction.Operand::target).ifPresent(target -> {
int address = target.address();
if (program.inRange(address)) {
labels.computeIfAbsent(address, addr -> String.format("L%04X", addr));
}
});
});

return assembly;
}

public Instruction parseData(Program program) {
int currentAddress = program.currentAddress();
int nextLabel = labelAddresses.stream().filter(n -> n > currentAddress)
.findFirst().orElse(program.lastAddress()+1);
int bytes = nextLabel - currentAddress;
int n = 0;
int ch = Byte.toUnsignedInt(program.peek(n)) & 0x7f;
if (ch >= 0x20 && ch < 0x7f) {
// Assume we have a string. Scan ahead to see how far we get.
final int expectedHighBit = Byte.toUnsignedInt(program.peek(n)) & 0x80;
StringBuilder sb = new StringBuilder();
while (ch >= 0x20 && ch < 0x7f && n < bytes) {
int currentHighBit = Byte.toUnsignedInt(program.peek(n)) & 0x80;
sb.append((char)ch);
n++;
// Exit if we get to end of range -or- if the high bit switched
if (n == bytes || currentHighBit != expectedHighBit) break;
ch = Byte.toUnsignedInt(program.peek(n)) & 0x7f;
}
return Instruction.at(currentAddress)
.code(program.read(n))
.mnemonic(".string")
.opValue("'%s'", sb.toString())
.get();
} else {
while ((n < bytes && ch < 0x20) || ch == 0x7f) {
n++;
if (n == bytes) break;
ch = Byte.toUnsignedInt(program.peek(n)) & 0x7f;
}
byte[] data = program.read(n);
return Instruction.at(currentAddress)
.code(data)
.mnemonic(".hex")
.opValue(HexFormat.of().formatHex(data))
.get();
}
}

public void prescan(Program program) {
// Assume all is data and we'll figure out the code.
dataFlags.set(0, code.length);
labelAddresses.clear();

Set<Integer> completedAddresses = new HashSet<>();
List<Integer> codeAddresses = new ArrayList<>();
if (entryPoints.isEmpty()) {
codeAddresses.add(program.baseAddress());
} else {
codeAddresses.addAll(entryPoints);
}
while (!codeAddresses.isEmpty()) {
// Only review an address once.
int address = codeAddresses.removeFirst();
if (completedAddresses.contains(address)) {
continue;
}
completedAddresses.add(address);
program.currentAddress(address);
// Run until we see a terminal instruction.
while (program.hasMore()) {
int offset = program.currentOffset();
completedAddresses.add(program.currentAddress());
Instruction inst = instructionSet.decodeOne(program);
dataFlags.clear(offset, offset+inst.code().length);
Optional<Instruction.Operand> addressRef = inst.addressRef();
// Handle (simple) code branches; nothing indirect
if (addressRef.isPresent()) {
Instruction.Operand operand = addressRef.get();
if (operand.target().isPresent()) {
Instruction.Target target = operand.target().get();
int opAddr = target.address();
if (program.inRange(opAddr) && !labelAddresses.contains(opAddr)) {
labelAddresses.add(opAddr);
}
if (target.type() == Instruction.Type.CODE && program.inRange(opAddr)) {
codeAddresses.add(opAddr);
}
}
}
// If code does NOT continue, stop processing.
if (inst.terminal()) {
break;
}
}
}
labelAddresses.sort(Integer::compare);
}

public static class Builder {
private final Set<String> sections = new HashSet<>();
private final Disassembler disassembler = new Disassembler();

private final byte[] code;
private int startAddress;
private int bytesToSkip;
private int bytesToDecode;
private InstructionSet instructionSet;
private List<Integer> entryPoints = new ArrayList<>();

public Builder(byte[] code) {
disassembler.startAddress = 0x300;
disassembler.code = code;
disassembler.instructionSet = InstructionSet6502.for6502();
this.startAddress = 0x300;
this.code = code;
this.instructionSet = InstructionSet6502.for6502();
}
/** Decode without preserving labels. */
public List<Instruction> decode() {
Expand All @@ -110,7 +234,7 @@ public List<Instruction> decode() {
/** Decode and preserve labels. Will also include any label groups requested. */
public List<Instruction> decode(Map<Integer,String> labels) {
assert labels != null;
assert disassembler.instructionSet != null;
assert this.instructionSet != null;
// merge in all selected sections
for (String name : sections) {
Map<String,Integer> section = ADDRESSES.get(name);
Expand All @@ -121,27 +245,34 @@ public List<Instruction> decode(Map<Integer,String> labels) {
labels.putIfAbsent(address, label);
});
}


Disassembler disassembler = new Disassembler(startAddress, bytesToSkip, bytesToDecode, code,
instructionSet, entryPoints);
return disassembler.decode(labels);
}
/** The starting address for the disassembly. */
public Builder startingAddress(int address) {
disassembler.startAddress = address;
this.startAddress = address;
return this;
}
/** Any bytes to skip in the program. Defaults to 0. */
public Builder bytesToSkip(int skip) {
disassembler.bytesToSkip = skip;
this.bytesToSkip = skip;
return this;
}
/** How much of the program to disassemble. Defaults to entire program. */
public Builder bytesToDecode(int length) {
disassembler.bytesToDecode = length;
this.bytesToDecode = length;
return this;
}
/** Select the instruction set. Required. */
public Builder use(InstructionSet instructionSet) {
disassembler.instructionSet = instructionSet;
this.instructionSet = instructionSet;
return this;
}
/** Add a code entry point to the list for processing. May be used multiple times. */
public Builder entryPoint(int address) {
this.entryPoints.add(address);
return this;
}
/** Add any label groups requested. Allows "All" and "None". */
Expand Down
Loading
Loading