Comparing version 0.0.20 to 0.0.21
{ | ||
"name": "hdl-js", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Hardware definition language (HDL) and Hardware simulator", |
@@ -21,2 +21,4 @@ # hdl-js | ||
- [Testing gates on passed data](#testing-gates-on-passed-data) | ||
- [Creating gates from default spec](#creating-gates-from-default-spec) | ||
- [Exec on set of data](#exec-on-set-of-data) | ||
- [Validating passed data on gate logic](#validating-passed-data-on-gate-logic) | ||
@@ -528,5 +530,38 @@ - [Main chip groups](#main-chip-groups) | ||
It is possible to execute and test gate logic on the set of data: | ||
### Creating gates from default spec | ||
All gates known their own specification, so we can omit passing explicit pins info, and use a constructor without parameters, or create gates via the `defaultFromSpec` method: | ||
```js | ||
const hdl = require('hdl-js'); | ||
const {And} = hdl.emulator.BuiltInGates; | ||
// Creates input `a` and `b` pins, and | ||
// ouput `out` pin automatically: | ||
const and1 = new And(); | ||
and1 | ||
.setPinValues({a: 1, b: 1}); | ||
.eval(); | ||
console.log(and1.getPin(0).getValue()); // 1 | ||
// The same as: | ||
const and2 = And.defaultFromSpec(); | ||
and2 | ||
.setPinValues({a: 1, b: 0}); | ||
.eval(); | ||
console.log(and2.getPin(0).getValue()); // 0 | ||
``` | ||
### Exec on set of data | ||
It is also possible to execute and test gate logic on the set of data: | ||
```js | ||
// const and = new And({ ... }); | ||
@@ -533,0 +568,0 @@ |
@@ -49,2 +49,8 @@ /** | ||
} | ||
And.Spec = { | ||
inputPins: ['a', 'b'], | ||
outputPins: ['out'], | ||
}; | ||
expect((new And()).getName()).toBe('And'); | ||
@@ -307,2 +313,57 @@ }); | ||
it('default from spec', () => { | ||
class And extends Gate { | ||
static isClocked() { | ||
return false; | ||
} | ||
} | ||
And.Spec = { | ||
inputPins: ['a', 'b'], | ||
outputPins: ['out'], | ||
}; | ||
const and1 = And.defaultFromSpec(); | ||
expect(and1.getName()).toBe(And.name); | ||
expect(and1.getInputPins().length).toEqual(2); | ||
expect(and1.getOutputPins().length).toEqual(1); | ||
expect(() => and1.getPin('a')).not.toThrow(); | ||
expect(() => and1.getPin('b')).not.toThrow(); | ||
expect(() => and1.getPin('out')).not.toThrow(); | ||
const and2 = new And(); | ||
expect(and2.getName()).toBe(And.name); | ||
expect(and2.getInputPins().length).toEqual(2); | ||
expect(and2.getOutputPins().length).toEqual(1); | ||
// Pin[16]: | ||
class Not16 extends Gate { | ||
static isClocked() { | ||
return false; | ||
} | ||
} | ||
Not16.Spec = { | ||
inputPins: [ | ||
{name: 'in', size: 16}, | ||
], | ||
outputPins: [ | ||
{name: 'out', size: 16}, | ||
], | ||
}; | ||
const not16 = Not16.defaultFromSpec(); | ||
expect(not16.getName()).toBe(Not16.name); | ||
expect(not16.getInputPins().length).toEqual(1); | ||
expect(not16.getOutputPins().length).toEqual(1); | ||
expect(not16.getPin('in').getSize()).toBe(16); | ||
expect(not16.getPin('out').getSize()).toBe(16); | ||
}); | ||
}); |
@@ -8,5 +8,45 @@ /** | ||
const Gate = require('../../Gate'); | ||
const GateTestUtil = require('../../gate-test-util'); | ||
const PC = require('../PC'); | ||
const GateTestUtil = require('../../gate-test-util'); | ||
const {int16Table} = require('../../../../util/typed-numbers'); | ||
/** | ||
* Testing data. | ||
*/ | ||
const data = int16Table([ | ||
{$clock: -0, in: 0, reset: 0, load: 0, inc: 0, out: 0}, | ||
{$clock: +0, in: 0, reset: 0, load: 0, inc: 0, out: 0}, | ||
{$clock: -1, in: 0, reset: 0, load: 0, inc: 0, out: 0}, | ||
{$clock: +1, in: 0, reset: 0, load: 0, inc: 1, out: 0}, | ||
{$clock: -2, in: 0, reset: 0, load: 0, inc: 1, out: 1}, | ||
{$clock: +2, in: -32123, reset: 0, load: 0, inc: 1, out: 1}, | ||
{$clock: -3, in: -32123, reset: 0, load: 0, inc: 1, out: 2}, | ||
{$clock: +3, in: -32123, reset: 0, load: 1, inc: 1, out: 2}, | ||
{$clock: -4, in: -32123, reset: 0, load: 1, inc: 1, out: -32123}, | ||
{$clock: +4, in: -32123, reset: 0, load: 0, inc: 1, out: -32123}, | ||
{$clock: -5, in: -32123, reset: 0, load: 0, inc: 1, out: -32122}, | ||
{$clock: +5, in: -32123, reset: 0, load: 0, inc: 1, out: -32122}, | ||
{$clock: -6, in: -32123, reset: 0, load: 0, inc: 1, out: -32121}, | ||
{$clock: +6, in: 12345, reset: 0, load: 1, inc: 0, out: -32121}, | ||
{$clock: -7, in: 12345, reset: 0, load: 1, inc: 0, out: 12345}, | ||
{$clock: +7, in: 12345, reset: 1, load: 1, inc: 0, out: 12345}, | ||
{$clock: -8, in: 12345, reset: 1, load: 1, inc: 0, out: 0}, | ||
{$clock: +8, in: 12345, reset: 0, load: 1, inc: 1, out: 0}, | ||
{$clock: -9, in: 12345, reset: 0, load: 1, inc: 1, out: 12345}, | ||
{$clock: +9, in: 12345, reset: 1, load: 1, inc: 1, out: 12345}, | ||
{$clock: -10, in: 12345, reset: 1, load: 1, inc: 1, out: 0}, | ||
{$clock: +10, in: 12345, reset: 0, load: 0, inc: 1, out: 0}, | ||
{$clock: -11, in: 12345, reset: 0, load: 0, inc: 1, out: 1}, | ||
{$clock: +11, in: 12345, reset: 1, load: 0, inc: 1, out: 1}, | ||
{$clock: -12, in: 12345, reset: 1, load: 0, inc: 1, out: 0}, | ||
{$clock: +12, in: 0, reset: 0, load: 1, inc: 1, out: 0}, | ||
{$clock: -13, in: 0, reset: 0, load: 1, inc: 1, out: 0}, | ||
{$clock: +13, in: 0, reset: 0, load: 0, inc: 1, out: 0}, | ||
{$clock: -14, in: 0, reset: 0, load: 0, inc: 1, out: 1}, | ||
{$clock: +14, in: 22222, reset: 1, load: 0, inc: 0, out: 1}, | ||
{$clock: -15, in: 22222, reset: 1, load: 0, inc: 0, out: 0}, | ||
]); | ||
describe('PC', () => { | ||
@@ -17,2 +57,9 @@ it('PC interface', () => { | ||
}); | ||
it('testing data', () => { | ||
Gate.resetClock(); | ||
expect(() => GateTestUtil.testTruthTable(data, PC.defaultFromSpec())) | ||
.not.toThrow(); | ||
}); | ||
}); |
@@ -9,3 +9,2 @@ /** | ||
const Gate = require('./Gate'); | ||
const Pin = require('./Pin'); | ||
@@ -29,23 +28,2 @@ /** | ||
/** | ||
* Creates an default instance of this gate from the spec. | ||
*/ | ||
static defaultFromSpec() { | ||
const { | ||
inputPins, | ||
outputPins, | ||
} = BuiltInGate.validateSpec(this.Spec); | ||
const toPin = name => { | ||
return typeof name === 'string' | ||
? new Pin({name}) | ||
: new Pin({name: name.name, size: name.size}); | ||
}; | ||
return new this({ | ||
inputPins: inputPins.map(toPin), | ||
outputPins: outputPins.map(toPin), | ||
}); | ||
} | ||
/** | ||
* Validates inputs, and outputs of this gate. | ||
@@ -52,0 +30,0 @@ */ |
@@ -41,7 +41,13 @@ /** | ||
*/ | ||
constructor({ | ||
name = null, | ||
inputPins = [], | ||
outputPins = [], | ||
} = {}) { | ||
constructor(options = null) { | ||
if (!options) { | ||
return this.getClass().defaultFromSpec(); | ||
} | ||
let { | ||
name = null, | ||
inputPins = [], | ||
outputPins = [], | ||
} = options; | ||
// Infer name from the class if not passed explicitly. | ||
@@ -71,2 +77,23 @@ if (!name) { | ||
/** | ||
* Creates an default instance of this gate from the spec. | ||
*/ | ||
static defaultFromSpec() { | ||
const { | ||
inputPins, | ||
outputPins, | ||
} = this.validateSpec(this.Spec); | ||
const toPin = name => { | ||
return typeof name === 'string' | ||
? new Pin({name}) | ||
: new Pin({name: name.name, size: name.size}); | ||
}; | ||
return new this({ | ||
inputPins: inputPins.map(toPin), | ||
outputPins: outputPins.map(toPin), | ||
}); | ||
} | ||
/** | ||
* Returns the name of this gate. | ||
@@ -73,0 +100,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
187065
4921
840