Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/sic/loader/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,35 @@
* @author: jure
*/
public class Loader {
private static boolean isWhitespace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}

public static int skipWhitespace(Reader r) throws IOException {
int c;
do
{
c = r.read();
}
while (isWhitespace((char)c));

public static void skipWhitespace(Reader r) throws IOException {
//TODO: while (Character.isWhitespace((char)r.read()));
return c;
}

public static String readString(Reader r, int len) throws IOException {
skipWhitespace(r);
//skipWhitespace(r);
StringBuilder buf = new StringBuilder();
while (len-- > 0) buf.append((char)r.read());
return buf.toString();
}

public static int readByte(Reader r) throws IOException {
skipWhitespace(r);
//skipWhitespace(r);
return Integer.parseInt(readString(r, 2), 16);
}

public static int readWord(Reader r) throws IOException {
skipWhitespace(r);
//skipWhitespace(r);
return Integer.parseInt(readString(r, 6), 16);
}

Expand All @@ -48,12 +58,10 @@ public static boolean loadSection(Machine machine, Reader r) {
readString(r, 6); // name is ignored
int start = readWord(r);
int length = readWord(r);
if (r.read() == '\r') // EOL
r.read();

Memory mem = machine.memory;
// text records
int ch = r.read();
int ch = skipWhitespace(r);
while (ch == 'T') {
int loc = readWord(r);
int len = readByte(r);
Expand All @@ -62,18 +70,14 @@ public static boolean loadSection(Machine machine, Reader r) {
byte val = (byte)readByte(r);
mem.setByteRaw(loc++, val);
}
if (r.read() == '\r') // EOL
r.read();
ch = r.read();
ch = skipWhitespace(r);
}

// modification records
while (ch == 'M') {
readWord(r); // addr
readByte(r); // len
if (r.read() == '\r') // EOL
r.read();
ch = r.read();
ch = skipWhitespace(r);
}

// load end record
Expand Down