Skip to content

Commit

Permalink
Merge pull request #101 from DvvCz/syscall-readint-32
Browse files Browse the repository at this point in the history
Fix 32 bit mode reading long in readInt syscall
  • Loading branch information
privat authored Nov 29, 2024
2 parents b0cb517 + 4716fa4 commit ef563eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/rars/riscv/syscalls/SyscallReadInt.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rars.riscv.syscalls;

import rars.*;
import rars.riscv.InstructionSet;
import rars.riscv.AbstractSyscall;
import rars.riscv.hardware.RegisterFile;
import rars.util.SystemIO;
Expand All @@ -12,7 +13,11 @@ public SyscallReadInt() {

public void simulate(ProgramStatement statement) throws SimulationException {
try {
RegisterFile.updateRegister("a0", SystemIO.readInteger(this.getNumber()));
if (InstructionSet.rv64) {
RegisterFile.updateRegister("a0", SystemIO.readLong(this.getNumber()));
} else {
RegisterFile.updateRegister("a0", SystemIO.readInteger(this.getNumber()));
}
} catch (NumberFormatException e) {
if (e.getMessage().equals("Cancel"))
throw new CancelException();
Expand Down
9 changes: 8 additions & 1 deletion src/rars/util/SystemIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ public class SystemIO {
* @return int value corresponding to user input
*/

public static long readInteger(int serviceNumber) throws CancelException {
public static int 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 Integer.parseInt(input.trim());
}

public static long readLong(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();
Expand Down

0 comments on commit ef563eb

Please sign in to comment.