Skip to content

Commit

Permalink
Cancel syscall TheThirdOne#69 from privat
Browse files Browse the repository at this point in the history
The user can now cancel sycalls with popup. A cancel button is present and has the same effect that a breakpoint.
  • Loading branch information
privat authored Aug 9, 2024
2 parents de9ee8e + 5968dd1 commit c62eaec
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 26 deletions.
7 changes: 7 additions & 0 deletions src/rars/CancelException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package rars;

/**
* This exception is only used to trigger breakpoints for I/O and other system call where the user selected cancel or equivalent.
*/
public class CancelException extends SimulationException {
}
4 changes: 2 additions & 2 deletions src/rars/riscv/AbstractSyscall.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package rars.riscv;

import rars.ExitingException;
import rars.ProgramStatement;
import rars.SimulationException;
/*
Copyright (c) 2003-2006, Pete Sanderson and Kenneth Vollmar
Expand Down Expand Up @@ -137,7 +137,7 @@ public int getNumber() {
* @param statement ProgramStatement object for this syscall instruction.
*/
public abstract void simulate(ProgramStatement statement)
throws ExitingException;
throws SimulationException;

public int compareTo(AbstractSyscall other) {
if (this == other) return 0;
Expand Down
7 changes: 3 additions & 4 deletions src/rars/riscv/syscalls/SyscallReadChar.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rars.riscv.syscalls;

import rars.ExitingException;
import rars.ProgramStatement;
import rars.*;
import rars.riscv.AbstractSyscall;
import rars.riscv.hardware.RegisterFile;
import rars.util.SystemIO;
Expand Down Expand Up @@ -39,7 +38,7 @@ public SyscallReadChar() {
super("ReadChar", "Reads a character from input console", "N/A", "a0 = the character or -1 if end of input.");
}

public void simulate(ProgramStatement statement) throws ExitingException {
public void simulate(ProgramStatement statement) throws SimulationException {
int character = SystemIO.readChar(this.getNumber());
if (character == SystemIO.NOTASCII) {
throw new ExitingException(statement,
Expand All @@ -48,4 +47,4 @@ public void simulate(ProgramStatement statement) throws ExitingException {
RegisterFile.updateRegister("a0", character);
}

}
}
3 changes: 2 additions & 1 deletion src/rars/riscv/syscalls/SyscallReadDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import rars.ExitingException;
import rars.ProgramStatement;
import rars.SimulationException;
import rars.riscv.hardware.FloatingPointRegisterFile;
import rars.riscv.AbstractSyscall;
import rars.util.SystemIO;
Expand Down Expand Up @@ -52,7 +53,7 @@ public SyscallReadDouble() {
/**
* Performs syscall function to read the bits of input double into $f0 and $f1.
*/
public void simulate(ProgramStatement statement) throws ExitingException {
public void simulate(ProgramStatement statement) throws SimulationException {
double doubleValue = 0;
try {
doubleValue = SystemIO.readDouble(this.getNumber());
Expand Down
3 changes: 2 additions & 1 deletion src/rars/riscv/syscalls/SyscallReadFloat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import rars.ExitingException;
import rars.ProgramStatement;
import rars.SimulationException;
import rars.riscv.AbstractSyscall;
import rars.riscv.hardware.FloatingPointRegisterFile;
import rars.util.SystemIO;
Expand Down Expand Up @@ -39,7 +40,7 @@ public SyscallReadFloat() {
super("ReadFloat", "Reads a float from input console", "N/A", "fa0 = the float");
}

public void simulate(ProgramStatement statement) throws ExitingException {
public void simulate(ProgramStatement statement) throws SimulationException {
float floatValue;
try {
floatValue = SystemIO.readFloat(this.getNumber());
Expand Down
8 changes: 4 additions & 4 deletions src/rars/riscv/syscalls/SyscallReadInt.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package rars.riscv.syscalls;

import rars.ExitingException;
import rars.Globals;
import rars.ProgramStatement;
import rars.*;
import rars.riscv.AbstractSyscall;
import rars.riscv.hardware.RegisterFile;
import rars.util.SystemIO;
Expand Down Expand Up @@ -40,10 +38,12 @@ public SyscallReadInt() {
super("ReadInt", "Reads an int from input console", "N/A", "a0 = the int");
}

public void simulate(ProgramStatement statement) throws ExitingException {
public void simulate(ProgramStatement statement) throws SimulationException {
try {
RegisterFile.updateRegister("a0", SystemIO.readInteger(this.getNumber()));
} catch (NumberFormatException e) {
if (e.getMessage().equals("Cancel"))
throw new CancelException();
throw new ExitingException(statement,
"invalid integer input (syscall " + this.getNumber() + ")");
}
Expand Down
6 changes: 2 additions & 4 deletions src/rars/riscv/syscalls/SyscallReadString.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package rars.riscv.syscalls;

import rars.ExitingException;
import rars.Globals;
import rars.ProgramStatement;
import rars.*;
import rars.riscv.AbstractSyscall;
import rars.riscv.hardware.AddressErrorException;
import rars.riscv.hardware.RegisterFile;
Expand Down Expand Up @@ -53,7 +51,7 @@ public SyscallReadString() {
"a0 = address of input buffer<br>a1 = size of the buffer", "N/A");
}

public void simulate(ProgramStatement statement) throws ExitingException {
public void simulate(ProgramStatement statement) throws SimulationException {
String inputString = "";
int buf = RegisterFile.getValue("a0"); // buf addr
int maxLength = RegisterFile.getValue("a1") - 1;
Expand Down
4 changes: 4 additions & 0 deletions src/rars/simulator/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ public void run() {
Globals.program.getBackStepper().addDoNothing(pc);
}
ebreak = true;
} catch (CancelException b) {
// Reset the pc in front of the instruction
RegisterFile.setProgramCounter(pc);
ebreak = true;
} catch (WaitException w) {
if (Globals.getSettings().getBackSteppingEnabled()) {
Globals.program.getBackStepper().addDoNothing(pc);
Expand Down
15 changes: 9 additions & 6 deletions src/rars/util/SystemIO.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package rars.util;

import rars.CancelException;
import rars.Globals;
import rars.Settings;
import rars.SimulationException;

import java.io.*;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -90,13 +92,14 @@ public class SystemIO {
* @return int value corresponding to user input
*/

public static long readInteger(int serviceNumber) {
public static long readInteger(int serviceNumber) throws CancelException {
String input = readStringInternal("0", "Enter an integer value (syscall " + serviceNumber + ")", -1);
// Client is responsible for catching NumberFormatException
if (input == null) throw new CancelException();
return Long.parseLong(input.trim());
}

private static String readStringInternal(String init, String prompt, int maxlength) {
private static String readStringInternal(String init, String prompt, int maxlength) throws CancelException {
String input = init;
if (Globals.getGui() == null) {
try {
Expand Down Expand Up @@ -130,7 +133,7 @@ private static String readStringInternal(String init, String prompt, int maxleng
* @return float value corresponding to user input
* Feb 14 2005 Ken Vollmar
*/
public static float readFloat(int serviceNumber) {
public static float readFloat(int serviceNumber) throws CancelException {
String input = readStringInternal("0", "Enter a float value (syscall " + serviceNumber + ")", -1);
return Float.parseFloat(input.trim());
}
Expand All @@ -143,7 +146,7 @@ public static float readFloat(int serviceNumber) {
* @return double value corresponding to user input
* Feb 14 2005 Ken Vollmar
*/
public static double readDouble(int serviceNumber) {
public static double readDouble(int serviceNumber) throws CancelException {
String input = readStringInternal("0", "Enter a Double value (syscall " + serviceNumber + ")", -1);
return Double.parseDouble(input.trim());
}
Expand Down Expand Up @@ -171,7 +174,7 @@ public static void printString(String string) {
* @param maxLength the maximum string length
* @return the entered string, truncated to maximum length if necessary
*/
public static String readString(int serviceNumber, int maxLength) {
public static String readString(int serviceNumber, int maxLength) throws CancelException {
String input = readStringInternal("", "Enter a string of maximum length " + maxLength
+ " (syscall " + serviceNumber + ")", maxLength);
if (input.endsWith("\n")) {
Expand All @@ -193,7 +196,7 @@ public static String readString(int serviceNumber, int maxLength) {
* @param serviceNumber the number assigned to Read Char syscall (default 12)
* @return int value with lowest byte corresponding to user input, EOF on end of data, or NOTASCII on invalid ASCII character.
*/
public static int readChar(int serviceNumber) {
public static int readChar(int serviceNumber) throws CancelException {
int returnValue;

// Need a popup?
Expand Down
12 changes: 9 additions & 3 deletions src/rars/venus/MessagesPane.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package rars.venus;

import rars.CancelException;
import rars.ErrorList;
import rars.Globals;
import rars.Settings;
import rars.simulator.ProgramArgumentList;
import rars.simulator.Simulator;
import rars.util.SystemIO;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
Expand Down Expand Up @@ -512,21 +514,25 @@ public void processProgramArgumentsIfAny() {
* @param prompt Prompt to display to the user.
* @return User input.
*/
public String getInputString(String prompt) {
public String getInputString(String prompt) throws CancelException {
String input;
boolean lock = Globals.memoryAndRegistersLock.isHeldByCurrentThread();
if (lock) {
Globals.memoryAndRegistersLock.unlock();
}
JOptionPane pane = new JOptionPane(prompt, JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION);
JOptionPane pane = new JOptionPane(prompt, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(Globals.getGui(), "Keyboard Input");
dialog.setVisible(true);
input = (String) pane.getInputValue();
this.postRunMessage(Globals.userInputAlert + input + "\n");
if (lock) {
Globals.memoryAndRegistersLock.lock();
}
if (input == JOptionPane.UNINITIALIZED_VALUE) {
throw new CancelException();
} else {
this.postRunMessage(Globals.userInputAlert + input + "\n");
}
return input;
}

Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
javac -cp rars.jar test/RarsTest.java
java -cp test:rars.jar RarsTest
java -cp test:rars.jar RarsTest "$@"
err=$?
rm test/RarsTest.class
exit "$err"
14 changes: 14 additions & 0 deletions test/loopchar.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#stdin:10\n20\n0
#stdout:1020
loop:
li a7, 5
ecall

beqz a0, end

li a7, 1
ecall
j loop
end:
li a7, 10
ecall
15 changes: 15 additions & 0 deletions test/loopint.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#stdin:10\n20\n0
#stdout:1020
loop:
li a7, 5
ecall

beqz a0, end

li a7, 1
ecall
j loop

end:
li a7, 10
ecall
24 changes: 24 additions & 0 deletions test/loopstring.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#stdin:Hello\nthe\nworld
#stdout:Hello\nthe\nworld
.data
buffer: .space 256

.text
loop:
li a7, 8
la a0, buffer
li a1, 256
ecall

lb t0, 0(a0)
li t1, '\n'
beq t0, t1, end

li a7, 4
la a0, buffer
ecall
j loop

end:
li a7, 10
ecall

0 comments on commit c62eaec

Please sign in to comment.