dlcsimdeprecated

Digital Logic Circuit Simulator in Javascript.

Usage no npm install needed!

<script type="module">
  import dlcsim from 'https://cdn.skypack.dev/dlcsim';
</script>

README

DLCSim

npm version

Digital Logic Circuit Simulation library in javascript.

DLCSim.js is main library and DLC.js is the wrapper.

The library has XOR,AND,OR,NAND,XNOR, and NOR gates.It also has half adders.

NPM Install

npm install dlcsim

Using wrapper.

Example Simple AND gate:


var DLCSim = require('dlcsim'); 
var DLC = require('dlcsim/src/DLC.js');

//New output node
var outputnode = new DLC("output","R");
//AND gate
var andgate = outputnode.addComponent("and","AND GATE");
//Inputs to AND gate
var inputnode0 = andgate.addComponent("input","X");
var inputnode1 = andgate.addComponent("input","Y");

//All binary input combinations
//combs [[1,1],[0,1],[1,0],[0,0]]
//Simulate and get data
var result = DLCSim.defaultSimulation([inputnode0,inputnode1],[outputnode]);
//Result [[1],[0],[0],[0]]

Example 3 input XOR gate:


var DLCSim = require('dlcsim'); 
var DLC = require('dlcsim/src/DLC.js');
//New output node
var outputnode = new DLC("output","R");
//XOR 3 input gate
var xorgate = outputnode.addComponent("xor","XOR GATE").setInputSize(3);
//Inputs
var inputnode0 = xorgate.addComponent("input","X");
var inputnode1 = xorgate.addComponent("input","Y");
var inputnode2 = xorgate.addComponent("input","Z");

//All binary input combinations
//combs [[1,1,1],[0,1,1],[1,0,1],[0,0,1],[1,1,0],[0,1,0],[1,0,0],[0,0,0]]
var result = DLCSim.defaultSimulation([inputnode0,inputnode1,inputnode2],[outputnode]);
//result [[1],[0],[0],[1],[0],[1],[1],[0]],

Example Add custom IC:

var DLCSim = require('dlcsim'); 
var DLC = require('dlcsim/src/DLC.js');
/*
* Function to process inputs
* @param inputs Array of input bits
* @param number Output number of IC
* @return Computed bit corresponding to output number
*/
function blashfunc(inputs,number) {
    if (number === 0)
    {
        return inputs[0] | inputs[1] | inputs[2];
    }
    if (number === 1)
    {
        return inputs[0] ^ inputs[2];
    }
};

/*
* Classes
* Class 0 Intergrated circuits
* Class 1 Logic gates
* Class 2 Input Output NOT
*/

//Add New IC
DLCSim.extend({"name":"blah", //Name of IC
               "inputs":3, // Number of inputs
               "outputs":2, // Number of outputs
               "outputvalues":[-1,-1], //Output
               "outputfunction": blashfunc, //function
               "class":0 //IC class
              });

//New output nodes
var outputnode0 = new DLC("output","A");
var outputnode1 = new DLC("output","B");

//Blah IC
var newic = new DLC("blah","blah IC");
//Attach
outputnode0.addInput(newic,0,0);
outputnode1.addInput(newic,1,0);

var inputnode0 = newic.addComponent("input","X");
var inputnode1 = newic.addComponent("input","Y");
var inputnode2 = newic.addComponent("input","Z");

//All binary input combinations
var result = DLCSim.defaultSimulation([inputnode0,inputnode1,inputnode2],[outputnode0,outputnode1]);
//combs [[1,1,1],[0,1,1],[1,0,1],[0,0,1],[1,1,0],[0,1,0],[1,0,0],[0,0,0]]
//result [[1,0],[1,1],[1,0],[1,1],[1,1],[1,0],[1,1],[0,0]]

Logic gate functions:


DLCSim.gates.and([1,1,1,1,1,0]); //0
DLCSim.gates.xor([0,0,0,0,1,0]); //1
DLCSim.gates.xor([1,0,0,0,0,1]); //0

Using the main library.

Example RS latch with NAND:

var DLCSim = require('dlcsim'); 
//New output nodes
var outputnode0 = new DLCSim(0,0,1,0);

var outputnode1 = new DLCSim(0,20,1,0);

//New NAND gate
var nandgate0 = new DLCSim(6,1,2,1);
var nandgate1 = new DLCSim(6,123,2,1);


//Attach NAND
outputnode0.addInput(nandgate0,0,0);

outputnode1.addInput(nandgate1,0,0);


//New input nodes
var inputnode0 = new DLCSim(1,2,0,1);
var inputnode1 = new DLCSim(1,3,0,1);

//Connect to NAND
nandgate0.addInput(inputnode0,0,0);
nandgate0.inputs[1] = [nandgate1,0];

nandgate1.inputs[0] = [nandgate0,0];
nandgate1.addInput(inputnode1,0,1);

//All binary input combinations
var combs = DLCSim.getCombinations(2);
//combs [[1,1],[0,1],[1,0],[0,0]]
var result = DLCSim.simulateWithTable([inputnode0,inputnode1],combs,[outputnode0,outputnode1]);
//result [[1,0],[1,0],[0,1],[1,1]]

Example Half adder IC:

var DLCSim = require('dlcsim'); 
//New output node
var outputnode0 = new DLCSim(0,0,1,0);
var outputnode1 = new DLCSim(0,23,1,0);
//New HA
var ha = new DLCSim(9,1,2,2);
//Output of IC to input node
outputnode0.addInput(ha,0,0);
outputnode1.addInput(ha,1,0);
//New input nodes
var inputnode0 = new DLCSim(1,2,0,1);
var inputnode1 = new DLCSim(1,3,0,1);
//Connect to HA
ha.addInput(inputnode0,0,0);
ha.addInput(inputnode1,0,1);

//All binary input combinations
var combs = DLCSim.getCombinations(2);
//combs [[1,1],[0,1],[1,0],[0,0]]
var result = DLCSim.simulateWithTable([inputnode0,inputnode1],combs,[outputnode0,outputnode1]);
//result [[0,1],[1,0],[1,0],[0,0]]