diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-08-16 11:54:23 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-08-16 11:54:23 +0800 |
commit | 9542deeb483a00b6fabed7574720926ce97d7511 (patch) | |
tree | 0f2c1f72c03dd4693fd59df67544d2a4dddc5494 /projects/12/KeyboardTest | |
parent | 9c0cb1d1c32724fc95ac9548e4f8d873d3adaccc (diff) |
Projects, 01-06 completed
Diffstat (limited to 'projects/12/KeyboardTest')
-rw-r--r-- | projects/12/KeyboardTest/KeyboardTestOutput.gif | bin | 0 -> 12048 bytes | |||
-rw-r--r-- | projects/12/KeyboardTest/Main.jack | 93 |
2 files changed, 93 insertions, 0 deletions
diff --git a/projects/12/KeyboardTest/KeyboardTestOutput.gif b/projects/12/KeyboardTest/KeyboardTestOutput.gif Binary files differnew file mode 100644 index 0000000..944983a --- /dev/null +++ b/projects/12/KeyboardTest/KeyboardTestOutput.gif diff --git a/projects/12/KeyboardTest/Main.jack b/projects/12/KeyboardTest/Main.jack new file mode 100644 index 0000000..e89182c --- /dev/null +++ b/projects/12/KeyboardTest/Main.jack @@ -0,0 +1,93 @@ +// This file is part of www.nand2tetris.org
+// and the book "The Elements of Computing Systems"
+// by Nisan and Schocken, MIT Press.
+// File name: projects/12/KeyboardTest/Main.jack
+
+/** Test program for the OS Keyboard class. */
+class Main {
+
+ /** Gets input from the user and verifies its contents. */
+ function void main() {
+ var char c, key;
+ var String s;
+ var int i;
+ var boolean ok;
+
+ let ok = false;
+ do Output.printString("keyPressed test:");
+ do Output.println();
+ while (~ok) {
+ do Output.printString("Please press the 'Page Down' key");
+ while (key = 0) {
+ let key = Keyboard.keyPressed();
+ }
+ let c = key;
+ while (~(key = 0)) {
+ let key = Keyboard.keyPressed();
+ }
+
+ do Output.println();
+
+ if (c = 137) {
+ do Output.printString("ok");
+ do Output.println();
+ let ok = true;
+ }
+ }
+
+ let ok = false;
+ do Output.printString("readChar test:");
+ do Output.println();
+ do Output.printString("(Verify that the pressed character is echoed to the screen)");
+ do Output.println();
+ while (~ok) {
+ do Output.printString("Please press the number '3': ");
+ let c = Keyboard.readChar();
+
+ do Output.println();
+
+ if (c = 51) {
+ do Output.printString("ok");
+ do Output.println();
+ let ok = true;
+ }
+ }
+
+ let ok = false;
+ do Output.printString("readLine test:");
+ do Output.println();
+ do Output.printString("(Verify echo and usage of 'backspace')");
+ do Output.println();
+ while (~ok) {
+ let s = Keyboard.readLine("Please type 'JACK' and press enter: ");
+
+ if (s.length() = 4) {
+ if ((s.charAt(0) = 74) & (s.charAt(1) = 65) & (s.charAt(2) = 67) & (s.charAt(3) = 75)) {
+ do Output.printString("ok");
+ do Output.println();
+ let ok = true;
+ }
+ }
+ }
+
+ let ok = false;
+ do Output.printString("readInt test:");
+ do Output.println();
+ do Output.printString("(Verify echo and usage of 'backspace')");
+ do Output.println();
+ while (~ok) {
+ let i = Keyboard.readInt("Please type '-32123' and press enter: ");
+
+ if (i = (-32123)) {
+ do Output.printString("ok");
+ do Output.println();
+ let ok = true;
+ }
+ }
+
+ do Output.println();
+ do Output.printString("Test completed successfully");
+
+ return;
+ }
+}
|