-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keyboard.jack
112 lines (103 loc) · 3.06 KB
/
Keyboard.jack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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/Keyboard.jack
/**
* A library for handling user input from the keyboard.
*/
class Keyboard {
static int KEYBOARD_ADDRESS;
static int LINE_BUFFER_LENGTH;
/** Initializes the keyboard. */
function void init() {
let KEYBOARD_ADDRESS = 24576;
let LINE_BUFFER_LENGTH = 64;
return;
}
/**
* Returns the character of the currently pressed key on the keyboard;
* if no key is currently pressed, returns 0.
*
* Recognizes all ASCII characters, as well as the following keys:
* new line = 128 = String.newline()
* backspace = 129 = String.backspace()
* left arrow = 130
* up arrow = 131
* right arrow = 132
* down arrow = 133
* home = 134
* End = 135
* page up = 136
* page down = 137
* insert = 138
* delete = 139
* ESC = 140
* F1 - F12 = 141 - 152
*/
function char keyPressed() {
return Memory.peek(KEYBOARD_ADDRESS);
}
/**
* Waits until a key is pressed on the keyboard and released,
* then echoes the key to the screen, and returns the character
* of the pressed key.
*/
function char readChar() {
var char key;
while (key = 0) {
let key = Keyboard.keyPressed();
}
while (~(Keyboard.keyPressed() = 0)) {}
return key;
}
/**
* Displays the message on the screen, reads from the keyboard the entered
* text until a newline character is detected, echoes the text to the screen,
* and returns its value. Also handles user backspaces.
*/
function String readLine(String message) {
return Keyboard._readLine(message, false);
}
/**
* Displays the message on the screen, reads from the keyboard the entered
* text until a newline character is detected, echoes the text to the screen,
* and returns its integer value (until the first non-digit character in the
* entered text is detected). Also handles user backspaces.
*/
function int readInt(String message) {
var String str;
let str = Keyboard._readLine(message, true);
while(str.length() = 0){
let str = Keyboard._readLine(message, true);
}
return str.intValue();
}
function String _readLine(String message, boolean onlyDigits) {
var char key;
var String ret;
let ret = String.new(LINE_BUFFER_LENGTH);
do Output.printString(message);
let key = Keyboard.readChar();
while ((~(key = 128)) & (~(ret.length() > LINE_BUFFER_LENGTH))) { // intro
if(key = 129) { // backspace
do Output.backSpace();
do Output.printChar(32);
do Output.backSpace();
do ret.eraseLastChar();
} else {
if((~onlyDigits) | ((key > 47) & (key < 58))){
do ret.appendChar(key);
do Output.printChar(key);
} else {
if((ret.length() = 0) & (key = 45)){
do ret.appendChar(key);
do Output.printChar(key);
}
}
}
let key = Keyboard.readChar();
}
do Output.println();
return ret;
}
}