Skip to content

Commit 7248444

Browse files
committed
Starting work on a simple logic simulator.
0 parents  commit 7248444

8 files changed

+644
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/dist
2+
/node_modules
3+
*.swp

LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Copyright 2018 Marek Materzok
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# DigitalJS

package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "digitaljs",
3+
"version": "0.0.1",
4+
"description": "Digital logic simulator",
5+
"main": "index.js",
6+
"scripts": {
7+
"dev": "webpack --mode development",
8+
"prod": "webpack --mode production"
9+
},
10+
"author": "Marek Materzok",
11+
"license": "BSD-2-Clause",
12+
"devDependencies": {
13+
"babel-core": "^6.26.3",
14+
"babel-loader": "^7.1.5",
15+
"babel-preset-env": "^1.7.0",
16+
"css-loader": "^1.0.0",
17+
"expose-loader": "^0.7.5",
18+
"html-webpack-plugin": "^3.2.0",
19+
"style-loader": "^0.21.0",
20+
"webpack": "^4.16.3",
21+
"webpack-cli": "^3.1.0"
22+
},
23+
"dependencies": {
24+
"jointjs": "^2.1.3",
25+
"jquery": "^3.3.1"
26+
}
27+
}

src/index.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"use strict";
2+
3+
import joint from 'jointjs';
4+
import './style.css';
5+
6+
function getCellType(tp) {
7+
const types = {
8+
'$and': joint.shapes.logic.And,
9+
'$or': joint.shapes.logic.Or,
10+
'$xor': joint.shapes.logic.Xor,
11+
'$not': joint.shapes.logic.Not
12+
};
13+
if (tp in types) return types[tp];
14+
else return null; // TODO
15+
}
16+
17+
function makeGraph(data) {
18+
const graph = new joint.dia.Graph();
19+
for (const dev of data.devices) {
20+
const cellType = getCellType(dev.type);
21+
const cell = new cellType({ position: {x: 0, y: 0} });
22+
graph.addCell(cell);
23+
}
24+
joint.layout.DirectedGraph.layout(graph, {
25+
nodeSep: 20,
26+
edgeSep: 30,
27+
rankDir: "LR"
28+
});
29+
return graph;
30+
}
31+
32+
export class Circuit {
33+
constructor(data) {
34+
this.graph = makeGraph(data);
35+
}
36+
displayOn(elem) {
37+
const paper = new joint.dia.Paper({
38+
el: elem,
39+
model: this.graph,
40+
width: 1000, height: 600, gridSize: 5,
41+
snapLinks: true,
42+
linkPinning: false,
43+
defaultLink: new joint.shapes.logic.Wire,
44+
validateConnection: function(vs, ms, vt, mt, e, vl) {
45+
return true;
46+
}
47+
});
48+
this.graph.resetCells(this.graph.getCells());
49+
return paper;
50+
}
51+
};
52+

0 commit comments

Comments
 (0)