From b152a69f814cf9048b5fae7673fd0c7fbabe7d12 Mon Sep 17 00:00:00 2001 From: Plopez Date: Fri, 27 Jan 2023 01:50:30 +0100 Subject: [PATCH] Implement basic I/O devices. --- samples/test.siso | 1 - src/module-info.java | 5 +++ src/sisc/Computer.java | 5 ++- src/sisc/devices/TerminalInput.java | 44 ++++++++++++++++++++++ src/sisc/devices/TerminalNumberOutput.java | 29 ++++++++++++++ 5 files changed, 82 insertions(+), 2 deletions(-) delete mode 100644 samples/test.siso create mode 100644 src/sisc/devices/TerminalInput.java create mode 100644 src/sisc/devices/TerminalNumberOutput.java diff --git a/samples/test.siso b/samples/test.siso deleted file mode 100644 index 7182fca..0000000 --- a/samples/test.siso +++ /dev/null @@ -1 +0,0 @@ -¹ diff --git a/src/module-info.java b/src/module-info.java index 79b6d70..737d181 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -1,5 +1,7 @@ import sisc.api.io.InputDevice; import sisc.api.io.OutputDevice; +import sisc.devices.TerminalInput; +import sisc.devices.TerminalNumberOutput; /** * @author plopez @@ -11,4 +13,7 @@ uses OutputDevice; exports sisc.api.io; + + provides OutputDevice with TerminalNumberOutput; + provides InputDevice with TerminalInput; } diff --git a/src/sisc/Computer.java b/src/sisc/Computer.java index f948f60..7369443 100644 --- a/src/sisc/Computer.java +++ b/src/sisc/Computer.java @@ -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; @@ -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(); } } diff --git a/src/sisc/devices/TerminalInput.java b/src/sisc/devices/TerminalInput.java new file mode 100644 index 0000000..1f92ee0 --- /dev/null +++ b/src/sisc/devices/TerminalInput.java @@ -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); + } +} diff --git a/src/sisc/devices/TerminalNumberOutput.java b/src/sisc/devices/TerminalNumberOutput.java new file mode 100644 index 0000000..c11af59 --- /dev/null +++ b/src/sisc/devices/TerminalNumberOutput.java @@ -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"; + } + +}