Skip to content

Commit

Permalink
Implement basic I/O devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
plopez01 committed Jul 9, 2023
1 parent 05d3399 commit b152a69
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
1 change: 0 additions & 1 deletion samples/test.siso

This file was deleted.

5 changes: 5 additions & 0 deletions src/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sisc.api.io.InputDevice;
import sisc.api.io.OutputDevice;
import sisc.devices.TerminalInput;
import sisc.devices.TerminalNumberOutput;

/**
* @author plopez
Expand All @@ -11,4 +13,7 @@
uses OutputDevice;

exports sisc.api.io;

provides OutputDevice with TerminalNumberOutput;
provides InputDevice with TerminalInput;
}
5 changes: 4 additions & 1 deletion src/sisc/Computer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import static sisc.instructions.Instructions.*;

import java.util.Arrays;

public class Computer {
private final Memory memory = new Memory();
private final InstructionStorage instructions;
Expand Down Expand Up @@ -33,7 +35,8 @@ public void run() {
System.out.println("Contents: " + instructions);

while (true) {
System.out.println("PC addr: " + PC.peekStr());
//System.out.println("PC addr: " + PC.peekStr());
//System.out.println(Arrays.toString(RegFile.REGS));
runNext();
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/sisc/devices/TerminalInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sisc.devices;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import sisc.api.io.InputDevice;
import sisc.api.io.InputPair;

public class TerminalInput implements InputDevice {
private boolean closed;
@Override
public void start(InputPair pair) {
pair.connectTo(2, 16, 3);
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader bReader = new BufferedReader(reader);

while (!closed) {
try {
pair.offer((short)Short.parseShort(bReader.readLine()));
} catch (IOException e) {
throw new AssertionError();
}
}
}

@Override
public void onClose() {
// haha yes. This doesn't work because reader.read waits indefinitely for input.
// would need to do some kind-of active wait, or just set ourselves as daemon somehow
// edit: done this is now a daemon thread
closed = true;
}

@Override
public String name() {
return "Console";
}

@Override
public void configure(Thread t) {
t.setDaemon(true);
}
}
29 changes: 29 additions & 0 deletions src/sisc/devices/TerminalNumberOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sisc.devices;

import sisc.api.io.OutputDevice;
import sisc.api.io.OutputPair;

public class TerminalNumberOutput implements OutputDevice {

@Override
public void start(OutputPair pair) {
// TODO Auto-generated method stub
pair.connectTo(0, 50, 1);
while (true) {
System.out.println(pair.recieve());
}
}

@Override
public void onClose() {
// TODO Auto-generated method stub

}

@Override
public String name() {
// TODO Auto-generated method stub
return "Terminal";
}

}

0 comments on commit b152a69

Please sign in to comment.