dlcs
v1.1.0
Published
Digital Logic Circuit Simulator in Javascript.
Downloads
7
Maintainers
Readme
DLCS
Digital Logic Circuit Simulation library in javascript.
Simulate nodes, logic gates, and ICs.
Install
npm install dlcs
var DLCS = require('dlcs');
Clientside
<script src="funary.js"></script>
<script src="DLCS.js"></script>
Example Simple AND with OR gate:
var DLCS = require('dlcs');
//New output node
var outputnode = new DLCS(DLCS.outputnode);
//New OR gate
var orgate = new DLCS(DLCS.orgate);
//New AND gate
var andgate = new DLCS(DLCS.andgate);
//Set inputs paths
outputnode.addInput(orgate).addInput(andgate);
//New input nodes
var inputnode0 = new DLCS(DLCS.inputnode);
var inputnode1 = new DLCS(DLCS.inputnode);
var inputnode2 = new DLCS(DLCS.inputnode);
//Connect to AND
andgate.addInput(inputnode0);
andgate.addInput(inputnode1);
orgate.addInput(inputnode2);
//Set single values
inputnode0.setValue(1,0); //1
inputnode1.setValue(1,0); //1
inputnode2.setValue(0,0); //0
//Simulate circuit
var result = outputnode.simulate().getOutputValues(); //[1]
Example Full adder IC
Full adder takes in inputs A , B, and CIN(Carry in). S(Sum) is addition of inputs. COUT(Carry out) is the overflow of the addition of inputs.
| A | B | CIN | COUT | S | |---|---|-----|------|---| | 0 | 0 | 0 | 0 | 0 | | 0 | 0 | 1 | 0 | 1 | | 0 | 1 | 0 | 0 | 1 | | 0 | 1 | 1 | 1 | 0 | | 1 | 0 | 0 | 0 | 1 | | 1 | 0 | 1 | 1 | 0 | | 1 | 1 | 0 | 1 | 0 | | 1 | 1 | 1 | 1 | 1 |
var DLCS = require('dlcs');
//New Full adder
var fa = new DLCS(DLCS.fa);
//Auto generate array of input and output nodes
var innodes = fa.addInputNodes();
var outnodes = fa.addOutputNodes();
//Set input values
innodes[0].setOutputValues([0]); //0
innodes[1].setOutputValues([1]); //1
innodes[2].setOutputValues([1]); //1
//Little endianess
var result0 = outnodes[0].simulate().getOutputValues(); //[0]
var result1 = outnodes[1].simulate().getOutputValues(); //[1]
N-bit adder
I0(least significant) to IN(most significant) is the first binary number.
IN+1(least significant) to I2N(most significant) is the second binary number.
N-bit adder takes both and adds them.
N-bit adder produces U0 to UN+1 output.
//New adder with 8 inputs and 5 outputs
var nadder = new DLCS(DLCS.nadder,0,8,5);
//Auto generate array of input and output nodes
var innodes = nadder.addInputNodes();
//Set input values of one number
innodes[0].setOutputValues([0]); //0
innodes[1].setOutputValues([1]); //1
innodes[2].setOutputValues([0]); //0
innodes[3].setOutputValues([1]); //1
//Set input values of one number
innodes[4].setOutputValues([1]); //1
innodes[5].setOutputValues([1]); //1
innodes[6].setOutputValues([1]); //1
innodes[7].setOutputValues([1]); //1
//Little endianess
var result = nadder.simulate().getOutputValues(); //[1, 0, 0, 1, 1]
Extending DLCS for custom components
//New function of XOR and AND
var customfunction = function (input) {
return [((input[0] ^ input[1]) & input[2])];
}
//New custom IC data
var customICdata = {"inputs":3,
"name": "custom",
"outputs":1,
"outputvalues":[-1],
"outputfunction": customfunction
};
//New custom IC data
var customIC = new DLCS(customICdata);
//Generate input nodes
var innodes = customIC.addInputNodes();
innodes[0].setOutputValues([0]); //0
innodes[1].setOutputValues([1]); //1
innodes[2].setOutputValues([0]); //0
customIC.simulate();
var result = customIC.getOutputValues(); //[0]
dlcircuit (Main object)
var DLCS = require('dlcs');
//The object returned from DLC or DLCSim is dlcircuit
//dlcircuit is the object that forms every gate, node, and IC
var andgate = new DLCS(DLCS.andgate);
//Each object contains an output array
//This returns the the output array
andgate.getOutputValues();
//Setting the output array
andgate.setOutputValues([1]);
//Setting individual element
andgate.setValue(1,0);
//Instantaneous simulation of object
//The values will be set and stored until some other operation happens
andgate.simulate();
Related projects
Arbitrary precision binary operations library for Javascript.