Skip to content

Commit 0d247f2

Browse files
committed
Add some new feature
- Add Load and Save file for the current work. - Wires can change color when mouse is over on it. - Wires can change color when nodes change value.
1 parent d849652 commit 0d247f2

File tree

17 files changed

+1864
-808
lines changed

17 files changed

+1864
-808
lines changed

index.html

Lines changed: 251 additions & 201 deletions
Large diffs are not rendered by default.

simulator/css/simstyle.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.navbar-brand {
2+
font-size: 12px;
3+
padding-top: 0px !important;
4+
}
5+
.navGroupTools {
6+
padding-left: 2px;
7+
padding-right: 2px;
8+
border-right-style: dotted;
9+
border-color:gray;
10+
}
11+
12+
.githubSection {
13+
display: flex;
14+
padding-left: 2px;
15+
padding-right: 2px;
16+
}
17+
18+
.nav-item {
19+
display: inline-flex;
20+
}
21+
22+
input[type="file"] {
23+
display: none;
24+
}

simulator/js/FileManager.js

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
import { logicInput, logicOutput, gate, flipflop, logicClock, srLatch, wireMng } from "./simulator.js"
2+
import { LogicInput } from "./circuit_components/LogicInput.js"
3+
import { LogicOutput } from "./circuit_components/LogicOutput.js";
4+
import { Clock } from "./circuit_components/Clock.js";
5+
import { Gate } from "./circuit_components/Gate.js";
6+
import { Integrated } from "./circuit_components/Integrated.js";
7+
import { IC_type } from "./circuit_components/Enums.js";
8+
import { FF_D_Single, FF_D_MasterSlave } from "./circuit_components/FF_D.js";
9+
import { FF_T } from "./circuit_components/FF_T.js";
10+
import { FF_JK } from "./circuit_components/FF_JK.js";
11+
import { SR_LatchAsync, SR_LatchSync, SR_Latch } from "./circuit_components/SR_Latch.js";
12+
import { nodeList } from "./circuit_components/Node.js";
13+
14+
let eventHistory = [];
15+
16+
/**
17+
* @todo TODO
18+
*/
19+
export class FileManager {
20+
21+
/**
22+
* @todo TODO
23+
*/
24+
constructor()
25+
{
26+
this.isLoadingState = false;
27+
}
28+
29+
/**
30+
* @todo TODO
31+
*/
32+
saveState() {
33+
/* TODO
34+
if(this.isLoadingState)
35+
return;
36+
37+
eventHistory.unshift(FileManager.getJSON_Workspace());
38+
if (eventHistory.length > 10) {
39+
delete eventHistory[10];
40+
eventHistory.length = 10;
41+
}
42+
console.log(eventHistory);*/
43+
}
44+
45+
/**
46+
* @todo TODO
47+
*/
48+
loadFile(e) {
49+
this.isLoadingState = true;
50+
51+
flipflop.splice(0, flipflop.length);
52+
srLatch.splice(0, srLatch.length);
53+
gate.splice(0, gate.length);
54+
wireMng.wire.splice(0, wireMng.wire.length);
55+
logicClock.splice(0, logicClock.length);
56+
logicInput.splice(0, logicInput.length);
57+
logicOutput.splice(0, logicOutput.length);
58+
nodeList.splice(0, nodeList.length);
59+
60+
//this.e = e;
61+
62+
let file = e.target.files.item(0);
63+
64+
let reader = new FileReader();
65+
reader.onload = function () {
66+
67+
let contentFile = reader.result;
68+
//console.log(contentFile);
69+
70+
// logic input
71+
if ("logicInput" in JSON.parse(contentFile)) {
72+
for (let i = 0; i < contentFile.length; i++) {
73+
let objectParsed = JSON.parse(contentFile).logicInput[i];
74+
75+
if (objectParsed == undefined)
76+
break;
77+
78+
console.log(objectParsed);
79+
logicInput.push(new LogicInput());
80+
Object.assign(logicInput[i], objectParsed);
81+
logicInput[i].refreshNodes();
82+
}
83+
}
84+
// logic output
85+
//console.log(logicOutput);
86+
if ("logicOutput" in JSON.parse(contentFile)) {
87+
for (let i = 0; i < contentFile.length; i++) {
88+
89+
let objectParsed = JSON.parse(contentFile).logicOutput[i];
90+
91+
if (objectParsed == undefined)
92+
break;
93+
94+
console.log(objectParsed);
95+
logicOutput.push(new LogicOutput());
96+
Object.assign(logicOutput[i], objectParsed);
97+
logicOutput[i].refreshNodes();
98+
}
99+
}
100+
101+
if ("logicClock" in JSON.parse(contentFile)) {
102+
for (let i = 0; i < contentFile.length; i++) {
103+
104+
let objectParsed = JSON.parse(contentFile).logicClock[i];
105+
106+
if (objectParsed == undefined)
107+
break;
108+
109+
console.log(objectParsed);
110+
logicClock.push(new Clock());
111+
Object.assign(logicClock[i], objectParsed);
112+
logicClock[i].refreshNodes();
113+
}
114+
}
115+
116+
if ("gate" in JSON.parse(contentFile)) {
117+
for (let i = 0; i < contentFile.length; i++) {
118+
119+
let objectParsed = JSON.parse(contentFile).gate[i];
120+
121+
if (objectParsed == undefined)
122+
break;
123+
124+
console.log(objectParsed);
125+
gate.push(new Gate(JSON.parse(contentFile).gate[i].strType));
126+
Object.assign(gate[i], objectParsed);
127+
gate[i].refreshNodes();
128+
}
129+
}
130+
131+
if ("srLatch" in JSON.parse(contentFile)) {
132+
for (let i = 0; i < contentFile.length; i++) {
133+
134+
let objectParsed = JSON.parse(contentFile).srLatch[i];
135+
136+
if (objectParsed == undefined)
137+
break;
138+
139+
console.log(objectParsed);
140+
141+
switch (JSON.parse(contentFile).srLatch[i].type) {
142+
case IC_type.SR_LATCH_ASYNC:
143+
srLatch.push(new SR_LatchAsync(JSON.parse(contentFile).srLatch[i].gateType,
144+
JSON.parse(contentFile).srLatch[i].stabilize));
145+
break;
146+
case IC_type.SR_LATCH_SYNC:
147+
srLatch.push(new SR_LatchSync(JSON.parse(contentFile).srLatch[i].gateType,
148+
JSON.parse(contentFile).srLatch[i].stabilize));
149+
break;
150+
}
151+
Object.assign(srLatch[i], objectParsed);
152+
srLatch[i].refreshNodes();
153+
}
154+
}
155+
156+
if ("flipflop" in JSON.parse(contentFile)) {
157+
for (let i = 0; i < contentFile.length; i++) {
158+
159+
let objectParsed = JSON.parse(contentFile).flipflop[i];
160+
161+
if (objectParsed == undefined)
162+
break;
163+
164+
console.log(objectParsed);
165+
166+
switch (JSON.parse(contentFile).flipflop[i].type) {
167+
case IC_type.FF_D_SINGLE:
168+
flipflop.push(new FF_D_Single(JSON.parse(contentFile).flipflop[i].type));
169+
break;
170+
case IC_type.FF_D_MASTERSLAVE:
171+
flipflop.push(new FF_D_MasterSlave(JSON.parse(contentFile).flipflop[i].type));
172+
break;
173+
case IC_type.FF_T:
174+
flipflop.push(new FF_T(JSON.parse(contentFile).flipflop[i].type));
175+
break;
176+
case IC_type.FF_JK:
177+
flipflop.push(new FF_JK(JSON.parse(contentFile).flipflop[i].type));
178+
break;
179+
}
180+
Object.assign(flipflop[i], objectParsed);
181+
flipflop[i].refreshNodes();
182+
}
183+
}
184+
185+
if ("wire" in JSON.parse(contentFile)) {
186+
for (let i = 0; i < contentFile.length; i++) {
187+
let objectParsed = JSON.parse(contentFile).wire[i];
188+
189+
if (objectParsed == undefined)
190+
break;
191+
192+
console.log(objectParsed);
193+
194+
wireMng.addNode(nodeList[objectParsed.startID]);
195+
wireMng.addNode(nodeList[objectParsed.endID]);
196+
//Object.assign(gate[i], objectParsed);
197+
}
198+
}
199+
200+
};
201+
reader.readAsText(file);
202+
}
203+
204+
205+
/**
206+
* @todo TODO
207+
*/
208+
saveFile(e) {
209+
210+
let jsonWorkspace = FileManager.getJSON_Workspace();
211+
let blob = new Blob([jsonWorkspace], { type: 'application/json' });
212+
saveProjectFile.href = URL.createObjectURL(blob);
213+
//console.log(jsonWorkspace);
214+
}
215+
216+
/**
217+
* @todo TODO
218+
*/
219+
static getJSON_Workspace() {
220+
let workspace = new Object();
221+
222+
workspace["logicInput"] = logicInput;
223+
workspace["logicOutput"] = logicOutput;
224+
workspace["flipflop"] = flipflop;
225+
workspace["logicClock"] = logicClock;
226+
workspace["gate"] = gate;
227+
workspace["srLatch"] = srLatch;
228+
workspace["wire"] = wireMng.wire;
229+
230+
let jsonWorkspace = JSON.stringify(workspace,
231+
function (key, value) {
232+
switch (key) {
233+
case "output":
234+
case "input":
235+
case "nodeSet":
236+
case "nodeReset":
237+
case "nodeClock":
238+
case "nodeD":
239+
case "nodeT":
240+
case "nodeJ":
241+
case "nodeK":
242+
case "nodeQ":
243+
case "nodeNotQ":
244+
case "andGate_NotQ":
245+
case "andGate_Q":
246+
case "ff_D":
247+
case "orGate":
248+
case "gateSet":
249+
case "gateReset":
250+
case "asyncLatch":
251+
case "master":
252+
case "slave":
253+
case "srLatchSync":
254+
case "startNode":
255+
case "endNode":
256+
return undefined;
257+
}
258+
259+
// other things which is not possible to export on JSON
260+
return value;
261+
}, '\t');
262+
return jsonWorkspace;
263+
}
264+
}

simulator/js/circuit_components/Clock.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
1-
class Clock extends LogicInput
2-
{
3-
constructor(period, dutycycle)
4-
{
1+
import { LogicInput } from "./LogicInput.js";
2+
3+
/**
4+
* Digital Clock
5+
* @classdesc Digital Clock
6+
* @extends LogicInput
7+
*/
8+
export class Clock extends LogicInput {
9+
/**
10+
* @param {*} period TODO
11+
* @param {*} dutycycle TODO
12+
*/
13+
constructor(period, dutycycle) {
514
super();
615
this.truePeriod = period * dutycycle / 100;
716
this.falsePeriod = period * (100 - dutycycle) / 100;
8-
this.lastTick = new Date();
9-
this.strInfo = "CLOCK \nT = " + period + " ms\nD% = " + dutycycle + "%";
17+
this.lastTick = new Date().getTime();
18+
this.strInfo = "CLOCK \nT = " + period + " ms\nD% = " + dutycycle;
1019
}
1120

12-
13-
draw()
14-
{
15-
const currTick = new Date();
21+
/**
22+
* Function to call for drawing the Clock
23+
*/
24+
draw() {
25+
const currTick = new Date().getTime();
1626

1727
const period = (this.value) ? this.truePeriod : this.falsePeriod;
18-
19-
if(currTick - this.lastTick > period)
20-
{
28+
if (currTick - this.lastTick > period) {
2129
this.toggle();
2230
this.lastTick = currTick;
2331
}
24-
32+
2533
super.draw();
2634
}
2735

28-
printInfo()
29-
{
36+
/**
37+
* Function to display Clock Info
38+
*/
39+
printInfo() {
3040
noStroke();
3141
fill(0);
3242
textSize(12);

simulator/js/circuit_components/Enums.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const MouseAction =
1+
export const MouseAction =
22
{
33
EDIT: 0,
44
MOVE: 1,
55
DELETE: 2
66
}
77

8-
const gateType =
8+
export const gateType =
99
{
1010
NONE: 0, // for testing usage
1111
NOT: 1,
@@ -17,7 +17,7 @@ const gateType =
1717
XNOR: 7
1818
};
1919

20-
const IC_type =
20+
export const IC_type =
2121
{
2222
NONE: 0, // for testing usage
2323
SR_LATCH_ASYNC: 1,
@@ -28,8 +28,23 @@ const IC_type =
2828
FF_JK: 6
2929
}
3030

31-
const syncType =
31+
export const ElementType =
32+
{
33+
NONE: 0, // for testing usage
34+
LOGIC_GATE: 1,
35+
FLIP_FLOP: 2,
36+
LOGIC_INPUT: 3,
37+
LOGIC_OUTPUT: 4
38+
}
39+
40+
export const syncType =
3241
{
3342
ASYNC: 0,
3443
SYNC: 1
44+
}
45+
46+
export const INPUT_STATE =
47+
{
48+
FREE: 0,
49+
TAKEN: 1,
3550
}

0 commit comments

Comments
 (0)