Skip to content

Commit

Permalink
Minor improvement to x86 cpu adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ledmington committed Nov 6, 2024
1 parent 9942273 commit a83fa2b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
4 changes: 3 additions & 1 deletion emu-gui/src/main/java/com/ledmington/view/ELFView.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ private void initializeStringTable(final TreeItem<String> root, final StringTabl
int i = 0;
while (i < strtab.getHeader().getSectionSize()) {
final String s = strtab.getString(i);
root.getChildren().add(getTreeItem(s, start + i, s.length()));
if (!s.isEmpty()) {
root.getChildren().add(getTreeItem(s, start + i, s.length()));
}
i += s.length() + 1;
}
}
Expand Down
5 changes: 4 additions & 1 deletion emu-gui/src/main/java/com/ledmington/view/X86CpuAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void doExecuteOne() {
}

public void doExecute() {
throw new Error("Not implemented");
super.state = State.RUNNING;
while (state != State.HALTED) {
this.doExecuteOne();
}
}
}
28 changes: 20 additions & 8 deletions emu/src/main/java/com/ledmington/emu/X86Cpu.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class X86Cpu implements X86Emulator {

private static final MiniLogger logger = MiniLogger.getLogger("x86-emu");

private enum State {
protected enum State {
RUNNING,
HALTED
}
Expand All @@ -48,7 +48,7 @@ private enum State {
private final MemoryController mem;
private final InstructionFetcher instFetch;
private final InstructionDecoder dec;
private State state = State.RUNNING;
protected State state = State.RUNNING;
private long entryPointVirtualAddress = 0L;

/**
Expand All @@ -64,6 +64,7 @@ public X86Cpu(final MemoryController mem) {

@Override
public void execute() {
state = State.RUNNING;
while (state != State.HALTED) {
executeOne();
}
Expand All @@ -79,10 +80,15 @@ public void executeOne(final Instruction inst) {
logger.debug(inst.toIntelSyntax());
switch (inst.opcode()) {
case SUB -> {
if (inst.firstOperand() instanceof Register64) {
final long r1 = rf.get((Register64) inst.firstOperand());
final long r2 = rf.get((Register64) inst.secondOperand());
rf.set((Register64) inst.firstOperand(), r1 - r2);
if (inst.firstOperand() instanceof Register64 op1) {
final long r1 = rf.get(op1);
final long r2 =
switch (inst.secondOperand()) {
case Register64 op2 -> rf.get(op2);
case Immediate imm -> imm.asLong();
default -> throw new IllegalArgumentException();
};
rf.set(op1, r1 - r2);
} else {
throw new IllegalArgumentException(String.format(
"Don't know what to do when SUB has %,d bits", ((Register) inst.firstOperand()).bits()));
Expand Down Expand Up @@ -212,8 +218,14 @@ public void executeOne(final Instruction inst) {
}
case CALL -> {
// TODO: check this
final IndirectOperand src = (IndirectOperand) inst.firstOperand();
final long result = computeIndirectOperand(rf, src);
final long result;
if (inst.firstOperand() instanceof IndirectOperand iop) {
result = computeIndirectOperand(rf, iop);
} else if (inst.firstOperand() instanceof Register64 r64) {
result = rf.get(r64);
} else {
throw new IllegalArgumentException();
}
rf.set(Register64.RIP, result);
}
case RET -> {
Expand Down
8 changes: 3 additions & 5 deletions id/src/test/java/FormatTestFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,13 @@ private static List<String> readAllLines(final String filepath) {
continue;
}
// comments are stripped before being added
if (line.startsWith("#")) {
if (line.charAt(0) == '#') {
lines.add(Set.of(line.strip()));
isGroupEnded = true;
continue;
}

final String[] splitted = line.split("\\|");

if (splitted.length != 2) {
if (line.indexOf('|') < 0 || line.indexOf('|') != line.lastIndexOf('|')) {
throw new IllegalArgumentException(
String.format("Line %,d: '%s' is not formatted correctly", lineIndex, line));
}
Expand Down Expand Up @@ -143,7 +141,7 @@ private static List<String> readAllLines(final String filepath) {

private static void writeAllLines(final List<String> lines, final String filePath) {
try {
Files.writeString(Path.of(filePath), String.join("%n", lines), StandardOpenOption.WRITE);
Files.writeString(Path.of(filePath), String.join("\n", lines), StandardOpenOption.WRITE);
} catch (final IOException e) {
throw new RuntimeException(e);
}
Expand Down
11 changes: 6 additions & 5 deletions id/src/test/java/com/ledmington/cpu/x86/X86Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ static void setup() {
.toAbsolutePath())) {
int i = 0;
for (String line = br.readLine(); line != null; line = br.readLine(), i++) {
if (line.isEmpty() || line.isBlank() || line.startsWith("#")) {
if (line.isEmpty() || line.isBlank() || line.charAt(0) == '#') {
continue;
}

final String[] splitted = line.split("\\|");
final int expectedSides = 2;
if (splitted.length != expectedSides) {
final int idx = line.indexOf('|');
if (idx < 0 || idx != line.lastIndexOf('|')) {
throw new IllegalArgumentException(
String.format("Line %,d: '%s' is not formatted correctly", i, line));
}
args.add(Arguments.of(splitted[0].strip(), splitted[1].strip()));
args.add(Arguments.of(
line.substring(0, idx - 1).strip(),
line.substring(idx + 1).strip()));
}
} catch (final IOException | URISyntaxException e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit a83fa2b

Please sign in to comment.