Comparing version 0.0.69 to 0.0.70
@@ -12,2 +12,3 @@ /** | ||
var path = require('path'); | ||
var TablePrinter = require('../table-printer'); | ||
var vm = require('vm'); | ||
@@ -187,2 +188,3 @@ | ||
var isBuiltIn = Object.getPrototypeOf(GateClass) === BuiltInGate; | ||
var isKeyboard = isBuiltIn && GateClass.name === 'Keyboard'; | ||
var spec = GateClass.Spec; | ||
@@ -208,6 +210,10 @@ | ||
var inputPins = spec.inputPins.map(function (input) { | ||
var inputPins = spec.inputPins.length > 0 ? spec.inputPins.map(function (input) { | ||
return toFullName(input); | ||
}).join('\n'); | ||
}).join('\n') : ' None'; | ||
if (isKeyboard) { | ||
inputPins = ' Keyboard input'; | ||
} | ||
console.info('\n' + colors.bold('Inputs:\n\n') + inputPins); | ||
@@ -234,2 +240,29 @@ | ||
// Special truth table for Keyboard. | ||
if (isKeyboard) { | ||
var keyboard = GateClass.defaultFromSpec().listen(); | ||
console.info(colors.bold('Truth table:') + ' press any key...\n'); | ||
var printKeyboardTable = function printKeyboardTable(ch, code) { | ||
var printer = new TablePrinter({ | ||
head: ['char', 'out'] | ||
}); | ||
printer.push([{ content: ch.trim() || ' ', hAlign: 'center' }, { content: code, hAlign: 'center' }]); | ||
console.info(printer.toString() + '\n'); | ||
console.info('Ctrl-c to exit...\n'); | ||
}; | ||
printKeyboardTable('?', '?'); | ||
keyboard.getPin('out').on('change', function (value) { | ||
clearPreviousLines(8); | ||
printKeyboardTable(String.fromCharCode(value), value); | ||
}); | ||
return; | ||
} | ||
var truthTable = spec.truthTable; | ||
@@ -294,3 +327,3 @@ | ||
// Clear 6 previous lines, which take a previous table row. | ||
process.stdout.write('\r\x1B[K\r\x1B[1A'.repeat(6)); | ||
clearPreviousLines(6); | ||
runSlice(data, index + 1, action); | ||
@@ -300,2 +333,9 @@ }, 1000 / SystemClock.getRate()); | ||
/** | ||
* Clears previous lines in the terminal. | ||
*/ | ||
function clearPreviousLines(count) { | ||
process.stdout.write('\r\x1B[K\r\x1B[1A'.repeat(count)); | ||
} | ||
function execScript(script) { | ||
@@ -302,0 +342,0 @@ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, |
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
['ALU', 'ARegister', 'Add16', 'And', 'And16', 'Bit', 'DFF', 'DMux', 'DMux4Way', 'DMux8Way', 'DRegister', 'FullAdder', 'HalfAdder', 'Inc16', 'MipsAlu', 'Mux', 'Mux16', 'Mux4Way16', 'Mux8Way16', 'Nand', 'Nor', 'Nor16Way', 'Not', 'Not16', 'Or', 'Or16', 'Or8Way', 'PC', 'RAM', 'RAM16K', 'RAM4K', 'RAM512', 'RAM64', 'RAM8', 'Register', 'Screen', 'Xor'].forEach(function (gate) { | ||
['ALU', 'ARegister', 'Add16', 'And', 'And16', 'Bit', 'DFF', 'DMux', 'DMux4Way', 'DMux8Way', 'DRegister', 'FullAdder', 'HalfAdder', 'Inc16', 'Keyboard', 'MipsAlu', 'Mux', 'Mux16', 'Mux4Way16', 'Mux8Way16', 'Nand', 'Nor', 'Nor16Way', 'Not', 'Not16', 'Or', 'Or16', 'Or8Way', 'PC', 'RAM', 'RAM16K', 'RAM4K', 'RAM512', 'RAM64', 'RAM8', 'Register', 'Screen', 'Xor'].forEach(function (gate) { | ||
return BuiltInGates[gate] = require('./' + gate); | ||
@@ -20,0 +20,0 @@ }); |
{ | ||
"name": "hdl-js", | ||
"version": "0.0.69", | ||
"version": "0.0.70", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Hardware definition language (HDL) and Hardware simulator", |
@@ -1405,3 +1405,3 @@ # hdl-js | ||
- [Inc16](https://github.com/DmitrySoshnikov/hdl-js/blob/master/src/emulator/hardware/builtin-gates/Inc16.js) | ||
- ALU | ||
- [ALU](https://github.com/DmitrySoshnikov/hdl-js/blob/master/src/emulator/hardware/builtin-gates/ALU.js) | ||
@@ -1435,2 +1435,98 @@ The ALU chip itself evaluates both, arithmetic (such as addition), and logic (such as `And`, `Or`, etc) operations. | ||
#### Interface chips | ||
The interface chips include the gates, which allow communicating to user input and output. These are: | ||
- [Screen](https://github.com/DmitrySoshnikov/hdl-js/blob/master/src/emulator/hardware/builtin-gates/Screen.js) | ||
- [Keyboard](https://github.com/DmitrySoshnikov/hdl-js/blob/master/src/emulator/hardware/builtin-gates/Keyboard.js) | ||
##### Screen | ||
The _Screen_ chip represents 256 x 512 video memory, implemented with 8K registers. The gate can manipulate individual pixels using `getPixelAt`, and `setPixelAt` methods. | ||
```js | ||
... | ||
const screen = Screen | ||
.defaultFromSpec() | ||
.clear(); | ||
console.log(screen.getPixelAt(1, 16)); // 0 | ||
screen.setPixelAt(/* row */ 1, /* column */ 16, 1); | ||
console.log(screen.getPixelAt(1, 16)); // 1 | ||
``` | ||
##### Keyboard | ||
The _Keyboard_ chip is special, and requires callers to implement the actual keyboard listener, depending on a system where the chip is used. Such caller listeners should call `Keyboard.emit('key', key)` even, and the key code is propagated to the output pin: | ||
Example using from a browser environment: | ||
``` | ||
... | ||
const keyboard = Keyboard.defaultFromSpec(); | ||
keyboard.getPin('out').on('change', value => { | ||
console.log('Char code: ' + value); | ||
}); | ||
document.body.addEventListener('keypress', event => { | ||
Keyboard.emit('key', event.key); | ||
}); | ||
``` | ||
The `Keyboard` also provides default (blocking) `listen` method, which spawns Node's stdin keyboard input listening: | ||
```js | ||
... | ||
const keyboard = Keyboard.defaultFromSpec(); | ||
keyboard.getPin('out').on('change', value => { | ||
console.log('Char code: ' + value); | ||
}); | ||
// Listen to stdin. | ||
keyboard.listen(); | ||
``` | ||
We can introspect keyboard events using `--describe` option of the Keyboard gate: | ||
``` | ||
hdl-js --gate Keyboard --describe | ||
``` | ||
Result: | ||
``` | ||
BuiltIn "Keyboard" gate: | ||
Description: | ||
A keyboard, implemented as a 16 bit register that stores | ||
the currently pressed key code. | ||
Inputs: | ||
Keyboard input | ||
Outputs: | ||
- out[16] | ||
Truth table: press any key... | ||
┌──────┬─────┐ | ||
│ char │ out │ | ||
├──────┼─────┤ | ||
│ A │ 65 │ | ||
└──────┴─────┘ | ||
Ctrl-c to exit... | ||
``` | ||
### Clock | ||
@@ -1437,0 +1533,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
393170
82
8518
2144