Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ledmington committed Oct 14, 2024
1 parent f070608 commit 848678e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
22 changes: 21 additions & 1 deletion emu-cli/src/main/java/com/ledmington/emu/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ private static void run(final String filename, final String... commandLineArgume
ELFLoader.unload(elf);
}

private static long parseLongHex(final String s) {
if (s.isEmpty() || s.length() > 16) {
throw new IllegalArgumentException(String.format("'%s' is an invalid 64-bit hex value.", s));
}
long x = 0L;
for (int i = s.length() - 1; i >= 0; i--) {
final char c = s.charAt(i);
final long y =
switch (c) {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> (long) (c - '0');
case 'a', 'b', 'c', 'd', 'e', 'f' -> (long) (c - 'a');
case 'A', 'B', 'C', 'D', 'E', 'F' -> (long) (c - 'A');
default -> throw new IllegalArgumentException(
String.format("'%c' is not a hexadecimal character.", c));
};
x = (x << 4) | y;
}
return x;
}

@SuppressWarnings("PMD.AvoidCatchingThrowable")
public static void main(final String[] args) {
MiniLogger.setMinimumLevel(MiniLogger.LoggingLevel.WARNING);
Expand Down Expand Up @@ -197,7 +217,7 @@ public static void main(final String[] args) {
}

EmulatorConstants.setBaseAddress(
Long.parseLong(args[i].startsWith("0x") ? args[i].substring(2) : args[i], 16));
args[i].startsWith("0x") ? parseLongHex(args[i].substring(2)) : parseLongHex(args[i]));
}
case shortVersionFlag, longVersionFlag -> {
out.print(String.join("\n", "", " emu - CPU emulator", " v0.0.0", ""));
Expand Down
53 changes: 28 additions & 25 deletions emu-gui/src/main/java/com/ledmington/emu/EmulatorView.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package com.ledmington.emu;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -32,6 +35,7 @@
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

Expand All @@ -40,6 +44,7 @@
import com.ledmington.cpu.x86.Register64;
import com.ledmington.cpu.x86.exc.ReservedOpcode;
import com.ledmington.cpu.x86.exc.UnknownOpcode;
import com.ledmington.elf.ELFReader;
import com.ledmington.mem.MemoryController;

public final class EmulatorView extends Stage {
Expand Down Expand Up @@ -98,40 +103,34 @@ public EmulatorView() {
final BorderPane codePane = new BorderPane();
codePane.setTop(new Label("Code"));
this.codeArea = new TextArea();
this.codeArea.setFont(new Font(AppConstants.getDefaultMonospaceFont(), AppConstants.getDefaultFontSize()));
codePane.setCenter(this.codeArea);
centerPane.setCenter(codePane);

final BorderPane memoryPane = new BorderPane();
memoryPane.setTop(new Label("Memory"));
this.memoryArea = new TextArea();
this.memoryArea.setFont(new Font(AppConstants.getDefaultMonospaceFont(), AppConstants.getDefaultFontSize()));
memoryPane.setCenter(this.memoryArea);
centerPane.setRight(memoryPane);

centerPane.setPadding(new Insets(5));
mainPane.setCenter(centerPane);

final FlowPane bottomPane = new FlowPane();
final int maxSize = 20;
final int maxIconSize = 20;
final Button step = new Button();
final ImageView imageStep = new ImageView(new Image(
Thread.currentThread().getContextClassLoader().getResourceAsStream("icons/step.png"),
maxSize,
maxSize,
true,
true));
final ImageView imageStep =
new ImageView(new Image(getResourceStream("icons/step.png"), maxIconSize, maxIconSize, true, true));
imageStep.setPreserveRatio(true);
imageStep.setSmooth(true);
imageStep.setCache(true);
step.setGraphic(imageStep);
step.setOnMouseClicked(e -> System.out.println("Clicked step"));
step.setTooltip(new Tooltip("Step"));
final Button run = new Button();
final ImageView imageRun = new ImageView(new Image(
Thread.currentThread().getContextClassLoader().getResourceAsStream("icons/run.png"),
maxSize,
maxSize,
true,
true));
final ImageView imageRun =
new ImageView(new Image(getResourceStream("icons/run.png"), maxIconSize, maxIconSize, true, true));
imageRun.setPreserveRatio(true);
imageRun.setSmooth(true);
imageRun.setCache(true);
Expand All @@ -154,24 +153,28 @@ public EmulatorView() {
this.show();
}

private InputStream getResourceStream(final String name) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
}

private void loadFile(final File file) {
System.out.printf("Loading file '%s'\n", file.toString());
this.mem = new MemoryController(EmulatorConstants.getMemoryInitializer(), false);
this.regFile = new X86RegisterFile();
final InstructionFetcher instructionFetcher = new InstructionFetcher(this.mem, this.regFile);
this.decoder = new InstructionDecoderV1(instructionFetcher);
final String[] commandLineArguments = new String[0];
// try {
// ELFLoader.load(
// ELFReader.read(Files.readAllBytes(file.toPath())),
// mem,
// commandLineArguments,
// EmulatorConstants.getbaseAddress(),
// EmulatorConstants.getStackSize(),
// this.regFile);
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
try {
ELFLoader.load(
ELFReader.read(Files.readAllBytes(file.toPath())),
mem,
commandLineArguments,
EmulatorConstants.getbaseAddress(),
EmulatorConstants.getStackSize(),
this.regFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
updateRegisters();
updateCode();
updateMemory();
Expand Down Expand Up @@ -209,7 +212,7 @@ private void updateMemory() {
final int n = AppConstants.getMaxMemoryLines();
final int k = AppConstants.getMemoryBytesPerLine();
for (int i = 0; i < n * k; i++) {
final long address = baseAddress + i * k;
final long address = baseAddress + (long) i * k;
if (i % k == 0) {
sb.append(" 0x").append(String.format("%016x", address)).append(" : ");
}
Expand Down

0 comments on commit 848678e

Please sign in to comment.