Skip to content

Commit

Permalink
[wip] rework on GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Ledmington committed Sep 26, 2024
1 parent e3d05c5 commit 80bf6af
Show file tree
Hide file tree
Showing 9 changed files with 567 additions and 128 deletions.
190 changes: 97 additions & 93 deletions emu-cli/src/main/java/com/ledmington/emu/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,107 +107,111 @@ public static void main(final String[] args) {
final String shortVersionFlag = "-V";
final String longVersionFlag = "--version";

label:
for (int i = 0; i < args.length; i++) {
final String arg = args[i];

if (shortHelpFlag.equals(arg) || longHelpFlag.equals(arg)) {
out.print(String.join(
"\n",
"",
" emu - CPU emulator",
"",
" Usage: emu [OPTIONS] FILE",
"",
" Command line options:",
"",
" -h, --help Shows this help message and exits.",
" -q, --quiet Only errors are reported.",
" -v Errors, warnings and info messages are reported.",
" -vv All messages are reported.",
"",
" --mem-init-random Uninitialized memory has random values (default).",
" --mem-init-zero Uninitialized memory contains binary zero.",
" --stack-size N Number of bytes to allocate for the stack. Accepts only integers.",
" Can accept different forms like '1KB', '2MiB', '3Gb', '4Tib'.",
" --base-address X Memory location where to load the executable file (hexadecimal 64-bits).",
" FILE The ELF executable file to emulate.",
""));
out.flush();
System.exit(0);
} else if (shortQuietFlag.equals(arg) || longQuietFlag.equals(arg)) {
MiniLogger.setMinimumLevel(MiniLogger.LoggingLevel.ERROR);
} else if (verboseFlag.equals(arg)) {
MiniLogger.setMinimumLevel(MiniLogger.LoggingLevel.INFO);
} else if (veryVerboseFlag.equals(arg)) {
MiniLogger.setMinimumLevel(MiniLogger.LoggingLevel.DEBUG);
} else if (memoryInitializerRandomFlag.equals(arg)) {
EmulatorConstants.setMemoryInitializer(MemoryInitializer::random);
} else if (memoryInitializerZeroFlag.equals(arg)) {
EmulatorConstants.setMemoryInitializer(MemoryInitializer::zero);
} else if (stackSizeFlag.equals(arg)) {
i++;
if (i >= args.length) {
throw new IllegalArgumentException(String.format("Expected an argument after '%s'", stackSizeFlag));
switch (arg) {
case shortHelpFlag, longHelpFlag -> {
out.print(String.join(
"\n",
"",
" emu - CPU emulator",
"",
" Usage: emu [OPTIONS] FILE",
"",
" Command line options:",
"",
" -h, --help Shows this help message and exits.",
" -q, --quiet Only errors are reported.",
" -v Errors, warnings and info messages are reported.",
" -vv All messages are reported.",
"",
" --mem-init-random Uninitialized memory has random values (default).",
" --mem-init-zero Uninitialized memory contains binary zero.",
" --stack-size N Number of bytes to allocate for the stack. Accepts only integers.",
" Can accept different forms like '1KB', '2MiB', '3Gb', '4Tib'.",
" --base-address X Memory location where to load the executable file (hexadecimal 64-bits).",
" FILE The ELF executable file to emulate.",
""));
out.flush();
System.exit(0);
}

String s = args[i];
if (s.chars().allMatch(Character::isDigit)) {
EmulatorConstants.setStackSize(Long.parseLong(s));
continue;
case shortQuietFlag, longQuietFlag -> MiniLogger.setMinimumLevel(MiniLogger.LoggingLevel.ERROR);
case verboseFlag -> MiniLogger.setMinimumLevel(MiniLogger.LoggingLevel.INFO);
case veryVerboseFlag -> MiniLogger.setMinimumLevel(MiniLogger.LoggingLevel.DEBUG);
case memoryInitializerRandomFlag -> EmulatorConstants.setMemoryInitializer(MemoryInitializer::random);
case memoryInitializerZeroFlag -> EmulatorConstants.setMemoryInitializer(MemoryInitializer::zero);
case stackSizeFlag -> {
i++;
if (i >= args.length) {
throw new IllegalArgumentException(
String.format("Expected an argument after '%s'", stackSizeFlag));
}

String s = args[i];
if (s.chars().allMatch(Character::isDigit)) {
EmulatorConstants.setStackSize(Long.parseLong(s));
continue;
}

int k = 0;
long bytes = 0L;
while (k < s.length() && Character.isDigit(s.charAt(k))) {
bytes = bytes * 10L + (s.charAt(k) - '0');
k++;
}

s = s.substring(k);

bytes = switch (s) {
case "B" -> bytes;
case "KB" -> bytes * 1_000L;
case "MB" -> bytes * 1_000_000L;
case "GB" -> bytes * 1_000_000_000L;
case "TB" -> bytes * 1_000_000_000_000L;
case "KiB" -> bytes * 1_024L;
case "MiB" -> bytes * 1_024L * 1_024L;
case "GiB" -> bytes * 1_024L * 1_024L * 1_024L;
case "TiB" -> bytes * 1_024L * 1_024L * 1_024L * 1_024L;
case "b" -> bytes / 8L;
case "Kb" -> bytes * 1_000L / 8L;
case "Mb" -> bytes * 1_000_000L / 8L;
case "Gb" -> bytes * 1_000_000_000L / 8L;
case "Tb" -> bytes * 1_000_000_000_000L / 8L;
case "Kib" -> bytes * 1_024L / 8L;
case "Mib" -> bytes * 1_024L * 1_024L / 8L;
case "Gib" -> bytes * 1_024L * 1_024L * 1_024L / 8L;
case "Tib" -> bytes * 1_024L * 1_024L * 1_024L * 1_024L / 8L;
default -> throw new IllegalArgumentException(
String.format("Invalid stack size '%s'", args[i]));
};

EmulatorConstants.setStackSize(bytes);
}

int k = 0;
long bytes = 0L;
while (k < s.length() && Character.isDigit(s.charAt(k))) {
bytes = bytes * 10L + (s.charAt(k) - '0');
k++;
case baseAddressFlag -> {
i++;
if (i >= args.length) {
throw new IllegalArgumentException(
String.format("Expected an argument after '%s'", baseAddressFlag));
}

EmulatorConstants.setBaseAddress(
Long.parseLong(args[i].startsWith("0x") ? args[i].substring(2) : args[i], 16));
}

s = s.substring(k, s.length());

bytes = switch (s) {
case "B" -> bytes * 1L;
case "KB" -> bytes * 1_000L;
case "MB" -> bytes * 1_000_000L;
case "GB" -> bytes * 1_000_000_000L;
case "TB" -> bytes * 1_000_000_000_000L;
case "KiB" -> bytes * 1_024L;
case "MiB" -> bytes * 1_024L * 1_024L;
case "GiB" -> bytes * 1_024L * 1_024L * 1_024L;
case "TiB" -> bytes * 1_024L * 1_024L * 1_024L * 1_024L;
case "b" -> bytes / 8L;
case "Kb" -> bytes * 1_000L / 8L;
case "Mb" -> bytes * 1_000_000L / 8L;
case "Gb" -> bytes * 1_000_000_000L / 8L;
case "Tb" -> bytes * 1_000_000_000_000L / 8L;
case "Kib" -> bytes * 1_024L / 8L;
case "Mib" -> bytes * 1_024L * 1_024L / 8L;
case "Gib" -> bytes * 1_024L * 1_024L * 1_024L / 8L;
case "Tib" -> bytes * 1_024L * 1_024L * 1_024L * 1_024L / 8L;
default -> throw new IllegalArgumentException(String.format("Invalid stack size '%s'", args[i]));
};

EmulatorConstants.setStackSize(bytes);
} else if (baseAddressFlag.equals(arg)) {
i++;
if (i >= args.length) {
throw new IllegalArgumentException(
String.format("Expected an argument after '%s'", baseAddressFlag));
case shortVersionFlag, longVersionFlag -> {
out.print(String.join("\n", "", " emu - CPU emulator", " v0.0.0", ""));
out.flush();
System.exit(0);
}
default -> {
filename = arg;

EmulatorConstants.setBaseAddress(
Long.parseLong(args[i].startsWith("0x") ? args[i].substring(2) : args[i], 16));
} else if (shortVersionFlag.equals(arg) || longVersionFlag.equals(arg)) {
out.print(String.join("\n", "", " emu - CPU emulator", " v0.0.0", ""));
out.flush();
System.exit(0);
} else {
filename = arg;

// all the next command-line arguments are considered to be related to the
// executable to be emulated
innerArgs = Arrays.copyOfRange(args, i + 1, args.length);
break;
// all the next command-line arguments are considered to be related to the
// executable to be emulated
innerArgs = Arrays.copyOfRange(args, i + 1, args.length);
break label;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion emu-gui/src/main/java/com/ledmington/emu/AppConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ private AppConstants() {}
/** Shorthand monospace font family. */
private static String MONOSPACE_FONT_FAMILY = Font.getFamilies().contains("Consolas")
? "Consolas"
: Font.getDefault().getFamily();
: (Font.getFamilies().contains("Cousine")
? "Cousine"
: Font.getDefault().getFamily());

public static String getDefaultMonospaceFont() {
return MONOSPACE_FONT_FAMILY;
Expand Down
Loading

0 comments on commit 80bf6af

Please sign in to comment.