Skip to content

Commit

Permalink
Finish up #28
Browse files Browse the repository at this point in the history
  • Loading branch information
TheThirdOne committed Jul 16, 2019
1 parent f6a368e commit 9317aca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
13 changes: 8 additions & 5 deletions rars/riscv/syscalls/SyscallGetCWD.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import rars.riscv.hardware.AddressErrorException;
import rars.riscv.hardware.RegisterFile;

import java.nio.charset.StandardCharsets;

/*
Copyright (c) 20017, Benjamin Landers
Expand Down Expand Up @@ -45,19 +47,20 @@ public void simulate(ProgramStatement statement) throws ExitingException {
String path = System.getProperty("user.dir");
int buf = RegisterFile.getValue("a0");
int length = RegisterFile.getValue("a1");
if(length < path.length()+1){

byte[] utf8BytesList = path.getBytes(StandardCharsets.UTF_8);
if(length < utf8BytesList.length+1){
// This should be -34 (ERANGE) for compatability with spike, but until other syscalls are ready with compatable
// error codes, lets keep internal consitency.
RegisterFile.updateRegister("a0",-1);
return;
}
//TODO: update this to allow for utf-8 encoded directories
try {
for (int index = 0; index < path.length(); index++) {
for (int index = 0; index < utf8BytesList.length; index++) {
Globals.memory.setByte(buf + index,
path.charAt(index));
utf8BytesList[index]);
}
Globals.memory.setByte(buf + path.length(), 0);
Globals.memory.setByte(buf + utf8BytesList.length, 0);
} catch (AddressErrorException e) {
throw new ExitingException(statement, e);
}
Expand Down
19 changes: 11 additions & 8 deletions rars/riscv/syscalls/SyscallInputDialogString.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import rars.riscv.AbstractSyscall;

import javax.swing.*;
import java.nio.charset.StandardCharsets;

/*
Copyright (c) 2003-2008, Pete Sanderson and Kenneth Vollmar
Expand Down Expand Up @@ -77,19 +78,21 @@ public void simulate(ProgramStatement statement) throws ExitingException {
{
RegisterFile.updateRegister("a1", -3);
} else {
// TODO: update this to allow for utf-8 encoded characters
byte[] utf8BytesList = inputString.getBytes(StandardCharsets.UTF_8);
// The buffer will contain characters, a '\n' character, and the null character
// Copy the input data to buffer as space permits
for (int index = 0; (index < inputString.length()) && (index < maxLength - 1); index++) {
Globals.memory.setByte(byteAddress + index,
inputString.charAt(index));
int stringLength = Math.min(maxLength-1, utf8BytesList.length);
for (int index = 0; index < stringLength; index++) {
Globals.memory.setByte(byteAddress+ index,
utf8BytesList[index]);
}
if (inputString.length() < maxLength - 1) {
Globals.memory.setByte(byteAddress + Math.min(inputString.length(), maxLength - 2), '\n'); // newline at string end
if (stringLength < maxLength-1) {
Globals.memory.setByte(byteAddress + stringLength, '\n');
stringLength++;
}
Globals.memory.setByte(byteAddress + Math.min((inputString.length() + 1), maxLength - 1), 0); // null char to end string
Globals.memory.setByte(byteAddress + stringLength, 0);

if (inputString.length() > maxLength - 1) {
if (utf8BytesList.length > maxLength - 1) {
// length of the input string exceeded the specified maximum
RegisterFile.updateRegister("a1", -4);
} else {
Expand Down
8 changes: 6 additions & 2 deletions rars/riscv/syscalls/SyscallReadString.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import rars.riscv.hardware.RegisterFile;
import rars.util.SystemIO;

import java.nio.charset.StandardCharsets;

/*
Copyright (c) 2003-2006, Pete Sanderson and Kenneth Vollmar
Expand Down Expand Up @@ -62,12 +64,14 @@ public void simulate(ProgramStatement statement) throws ExitingException {
addNullByte = false;
}
inputString = SystemIO.readString(this.getNumber(), maxLength);

byte[] utf8BytesList = inputString.getBytes(StandardCharsets.UTF_8);
// TODO: allow for utf-8 encoded strings
int stringLength = Math.min(maxLength, inputString.length());
int stringLength = Math.min(maxLength, utf8BytesList.length);
try {
for (int index = 0; index < stringLength; index++) {
Globals.memory.setByte(buf + index,
inputString.charAt(index));
utf8BytesList[index]);
}
if (stringLength < maxLength) {
Globals.memory.setByte(buf + stringLength, '\n');
Expand Down

0 comments on commit 9317aca

Please sign in to comment.