-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuits.html
803 lines (698 loc) · 18.9 KB
/
circuits.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<style>
#display, #src {
font-family: monospace;
}
#display {
border: 1px solid;
font-weight: bold;
}
#log {
height: 200px;
overflow: auto;
}
</style>
<script>
// Class matrix (used to get references to neighbouring classes)
var clsMatrix = [];
/** Convert textarea 'src' into a matrix that we can step through **/
function mkMatrix() {
var src = $("textarea#src").val();
var lines = [];
var current = [];
src = src.split('');
for (var i = 0; i < src.length; i++) {
switch(src[i]) {
case '\n':
lines.push(current);
current = [];
continue;
default:
current.push(src[i]);
break;
}
}
lines.push(current);
return lines;
}
function Dbg(x) {
$("div#log").prepend($("<div></div>").append(x));
}
/**
* Applies inheritance to a class (first argument) from classes (second, third, and
* so on, arguments).
*
* Notes:
* - This WILL BREAK the instanceof operator! Use the isInstanceOf() function instead.
* - Parent classes must be fully declared before calling this function.
* - Multiple classes will be copied in sequence.
* - Properties that already exists in class will not be copied.
*/
function applyInheritance() {
// Validate arguments
if (arguments.length < 2) {
throw new Error("No inheritance classes given");
}
var toClass = arguments[0];
var fromClasses = Array.prototype.slice.call(arguments, 1, arguments.length);
// Check if class referencer has been created
if (applyInheritance.allClasses === undefined) {
applyInheritance.allClasses = [];
}
// Check for inheritance metadata in toClass
if (toClass.meta === undefined) {
toClass.meta = {
index: applyInheritance.allClasses.length,
fromClasses : [],
toClasses: []
};
toClass.meta.fromClasses[toClass.meta.index] = true; // class links to itself
applyInheritance.allClasses.push(toClass);
}
// Apply inheritance fromClasses
var fromClass = null;
for (var i = 0; i < fromClasses.length; i++) {
fromClass = fromClasses[i];
// Check for inheritance metadata in fromClass
if (fromClass.meta === undefined) {
fromClass.meta = {
index: applyInheritance.allClasses.length,
fromClasses: [],
toClasses: []
};
fromClasses[i].meta.fromClasses[fromClass.meta.index] = true; // class links to itself
applyInheritance.allClasses.push(fromClass);
}
// Link toClass and fromClass
toClass.meta.fromClasses[fromClass.meta.index] = true;
fromClass.meta.toClasses[toClass.meta.index] = true;
// Copy prototype fromClass toClass
for (var property in fromClass.prototype) {
if (toClass.prototype.hasOwnProperty(property) === false) {
// Copy missing property from the parent class to the inheritance class
toClass.prototype[property] = fromClass.prototype[property];
}
}
}
}
var currentSpeed = 1;
var currentRunInstance = 0;
var rate = 0; // Rate per second
setInterval(function() {
$("#rateCounter").text(rate.toString());
//rate = 0;
}, 250);
// Neighbours
var N_NW = 0, N_N = 1, N_NW = 2,
N_W = 3,/*This,*/ N_E = 4,
N_SW = 5, N_S = 6, N_SE = 7;
// Generic wire
function Wire () {
this.pos_y = 0;
this.pos_x = 0;
this.element = null;
this.current = -1; // Not set
this.direction = '*';
this.drawNow = 0;
return this;
}
Wire.prototype.setHtmlElement = function(ele, symbol) {
// Child classes should do something with ele
$(ele).text(symbol || '.');
this.element = ele;
switch(symbol) {
case '-':
case '|':
case '\\':
case '/':
this.direction = symbol;
break;
}
};
Wire.prototype.setPosition = function(y, x) {
this.pos_y = y;
this.pos_x = x;
};
// Get neighbouring classes
Wire.prototype.getNeighbours = function(callee) {
// Protection against endless runs
if (this.instanceId != currentRunInstance) return [];
var self = this;
// Helper function that gets the neighbour at the specified offset (-1, 0, or +1 in X or Y)
var _ = function(offy, offx) {
var ay = self.pos_y + offy;
var ax = self.pos_x + offx;
if (ay < 0 || ay > clsMatrix.length - 1)
return null;
if (ax < 0 || ax > clsMatrix[ay].length - 1)
return null;
if (clsMatrix[ay][ax] == callee) return null;
return clsMatrix[ay][ax];
};
switch(self.direction) {
case '|':
return [
_(-1, 0),
_( 1, 0),
];
case '-':
return [
_(0, -1), _(0, 1),
];
case '\\':
return [
_(-1, -1),
/* Middle */
_( 1, 1),
];
case '/':
return [
_(-1, 1),
/* Middle */
_( 1, -1),
];
case '+':
return [
_(-1, 0),
_(0, -1), _(0, 1),
_( 1, 0),
];
case 'x':
case 'X':
return [
_(-1, -1), _(-1, 1),
/* Middle */
_( 1, -1), _( 1, 1),
];
case 'V':
case 'v':
return [
_(-1, -1), _(-1, 1),
/* Middle */
_( 1, 0)
];
case 'A': // Inverse to V, but also does across (it has a line through it!)
return [
_(-1, 0),
_( 0, -1), _( 0, 1),
_( 1, -1), _( 1, 1),
];
case '>':
return [
_(-1, -1),
_( 0, 1),
_( 1, -1),
];
case '-': // Single direction
case ']': // Read only mid left and mid right
return [
_( 0, -1), _( 0, 1),
];
case '<':
return [
_(-1, 1),
_( 0, -1),
_( 1, 1),
];
case '*':
default:
return [
_(-1, -1), _(-1, 0), _(-1, 1),
_( 0, -1), _( 0, 1),
_( 1, -1), _( 1, 0), _( 1, 1)
];
}
};
Wire.prototype.setCurrent = function(current, callee) {
var self = this;
rate++;
if (current == undefined) {
Dbg("UNDEFINED CURRENT??");
return;
}
//this.drawNow = !this.drawNow;
//Dbg(this.drawNow);
if (self.current == current)
return;
self.current = current;
$(self.element).css('color', (current > 0 ? 'green' : 'red'));
var neighbours = self.getNeighbours();
$.each(neighbours, function(i, neighbour) {
if (neighbour && neighbour != callee) {
var fn = function() {
neighbour.setCurrent(current, self);
};
if (currentSpeed > 0) {
setTimeout(fn, currentSpeed);
} else {
fn();
}
}
});
};
function CornerWire () {
this.direction = '*';
this.input = 0;
this.output = 0;
}
applyInheritance(CornerWire, Wire);
CornerWire.prototype.setHtmlElement = function(ele, symbol) {
this.element = ele;
$(ele).html($('<span></span>').text(symbol));
switch(symbol) {
case '.': // west to south
this.input = N_W;
this.output = N_S;
break;
case '`': // north to east
this.input = N_N;
this.output = N_E;
break;
case ',': // south to east
this.input = N_S;
this.output = N_E;
break;
case "'": // west to north
this.input = N_W;
this.output = N_N;
break;
case "f": // east to south
this.input = N_E;
this.output = N_S;
break;
default:
Dbg('CornerWire doesnt know type ' + symbol);
break;
}
};
CornerWire.prototype.setCurrent = function(current, callee) {
var neighbours = this.getNeighbours();
rate++;
if (neighbours[this.output]) {
neighbours[this.output].setCurrent((neighbours[this.input] || {current:0}).current, this);
} else {
Dbg('Dont have output wire');
}
};
function OneWayWire () {
this.direction = '*';
this.input = 0;
this.output = 0;
}
applyInheritance(OneWayWire, Wire);
OneWayWire.prototype.setHtmlElement = function(ele, symbol) {
this.element = ele;
$(ele).html($('<span></span>').text(symbol));
switch(symbol) {
case "{": // only allow from east to west
this.input = N_E;
this.output = N_W;
break;
case "}": // only allow from west to east
this.input = N_W;
this.output = N_E;
break;
case '^': // only allow from south to north
this.input = N_S;
this.output = N_N;
break;
case '?': // only allow from north to south
this.input = N_N;
this.output = N_S;
break;
default:
Dbg('OneWayWire doesnt know type ' + symbol);
break;
}
};
OneWayWire.prototype.setCurrent = function(current, callee) {
var neighbours = this.getNeighbours();
rate++;
if (callee == neighbours[this.input] && neighbours[this.output]) {
neighbours[this.output].setCurrent(callee.current, this);
} // Don't do anything if callee is not input
};
function Battery () {
this.powered = false;
}
applyInheritance(Battery, Wire);
Battery.prototype.setHtmlElement = function(ele, symbol) {
var self = this;
self.element = ele;
$(ele).html($('<a></a>').text(symbol || '*').attr('title', 'Battery'));
$(ele).click(function() {
self.powered = !self.powered;
Dbg('Battery is now ' + (self.powered ? 'on' : 'off'));
self.element.css('color', (self.powered ? 'orange' : 'black'));
self.setCurrent(self.powered ? 1 : 0, self);
});
}
Battery.prototype.superSetCurrent = Battery.prototype.setCurrent;
Battery.prototype.setCurrent = function(current, callee) {
this.powered = (current > 0);
Wire.prototype.setCurrent.call(this, current, callee);
};
function Light () {
}
applyInheritance(Light, Wire);
Light.prototype.setCurrent = function(current) {
var self = this;
rate++;
if (self.current == current)
return;
self.current = current;
$(self.element).css('color', (current > 0 ? 'orange' : 'black'));
// Don't pass current to neighbours
}
Light.prototype.setHtmlElement = function(ele, symbol) {
var self = this;
self.element = ele;
$(ele).text(symbol || '@');
};
function Gate () {
this.current = 0;
this.direction = '-';
this.inputs = []; // 0 = a, 1 = b, ...
this.output = null; // The index of the output neighbour
// Define only the it
this.truthTable = [];
this.title = 'Gate';
this.customCurrent = false;
this.customCurrentValue = 0;
}
applyInheritance(Gate, Wire);
Gate.prototype.setHtmlElement = function(ele, symbol) {
this.element = ele;
$(ele).html($('<a></a>').text(symbol || '>').attr('title', this.title));
};
Gate.prototype.setCurrent = function(current) {
var neighbours = this.getNeighbours();
rate++;
var truth = this.truthSatisfied(neighbours);
$(this.element).css('color', current ? (truth ? 'lime' : 'red') : 'red');
if (this.output != null && neighbours[this.output]) {
//Dbg("Send output " + current);
neighbours[this.output].setCurrent(truth, this);
} else {
Dbg("Output neighbour not found");
}
};
Gate.prototype.truthSatisfied = function(neighbours) {
var inputTable = {};
for (var i = 0; i < this.inputs.length; i++) {
inputTable[i] = (neighbours[this.inputs[i]] || {current: -1}).current;
//Dbg("input " + i + " = " + inputTable[i]);
}
// Look for matching entry in truth table
for (var i = 0; i < this.truthTable.length; i++) {
var match = true;
for (var key in this.truthTable[i]) {
if (key != 'o' && inputTable[key] != this.truthTable[i][key]) {
match = false;
break;
}
}
if (match) {
return this.truthTable[i]['o']; // Output
}
}
Dbg("Truth entry not found");
return 0;
};
// Symbol: ]
// Only allows current to pass left to right
function GuardGateRight () {
this.direction = '-';
this.inputs = [0];
this.output = 1;
this.current = 0;
this.truthTable = [
{ 0: 0, o: 0 },
{ 0: 1, o: 1 },
];
}
applyInheritance(GuardGateRight, Gate);
// Symbol: !
// Negate
function NotGate () {
this.direction = '-';
this.inputs = [0];
this.output = 1;
this.current = 0;
this.truthTable = [
{ 0: 0, o: 1 },
{ 0: 1, o: 0 },
];
this.title = 'Not Gate';
}
applyInheritance(NotGate, Gate);
// Symbol: &
function NAndGate () {
this.direction = '>';
this.inputs = [0, 2]; // top, bottom of >
this.output = 1;
this.current = 0;
this.truthTable = [
{ 0: 0, 1: 0, o: 1 },
{ 0: 0, 1: 1, o: 1 },
{ 0: 1, 1: 0, o: 1 },
{ 0: 1, 1: 1, o: 0 },
];
this.title = 'NAND Gate';
}
applyInheritance(NAndGate, Gate);
// Symbol: >
function AndGate () {
this.direction = '>';
this.inputs = [0, 2];
this.output = 1;
this.current = 0;
this.truthTable = [
{ 0: 0, 1: 0, o: 0 },
{ 0: 1, 1: 0, o: 0 },
{ 0: 0, 1: 1, o: 0 },
{ 0: 1, 1: 1, o: 1 },
];
this.title = 'AND Gate';
};
applyInheritance(AndGate, Gate);
// Symbol: )
function OrGate () {
this.direction = '>';
this.inputs = [0, 2];
this.output = 1;
this.current = 0;
this.truthTable = [
{ 0: 0, 1: 0, o: 0 },
{ 0: 1, 1: 0, o: 1 },
{ 0: 0, 1: 1, o: 1 },
{ 0: 1, 1: 1, o: 1 },
];
this.title = 'OR Gate';
}
applyInheritance(OrGate, Gate);
// Symbol: $
function XorGate () {
this.direction = '>';
this.inputs = [0, 2];
this.output = 1;
this.current = 0;
this.truthTable = [
{ 0: 0, 1: 0, o: 0 },
{ 0: 1, 1: 0, o: 1 },
{ 0: 0, 1: 1, o: 1 },
{ 0: 1, 1: 1, o: 0 },
];
this.title = 'XOR Gate';
}
applyInheritance(XorGate, Gate);
// Symbol: #
// Jump wire. Anything passing left goes to right
// Anything passing top goes to bottom
function JumpGate () {
this.direction = '+';
this.title = 'Jump Gate';
}
applyInheritance(JumpGate, Wire);
JumpGate.prototype.setCurrent = function(current, callee) {
var neighbours = this.getNeighbours(); // Ignore callee
rate++;
// Find direction
if (callee == neighbours[0]) {
// Top
neighbours[3].setCurrent(neighbours[0].current);
} else if (callee == neighbours[3]) {
// Bottom
neighbours[0].setCurrent(neighbours[3].current);
} else if (callee == neighbours[1]) {
// Left
neighbours[2].setCurrent(neighbours[1].current);
} else if (callee == neighbours[2]) {
// Right
neighbours[1].setCurrent(neighbours[2].current);
} else {
Dbg("Unknown direction");
}
};
$(function() {
$("button#update").click(function() {
currentRunInstance++;
$("div#log").text('');
var matrix = mkMatrix();
var batteries = [];
clsMatrix = [];
clsMatrix.length = matrix.length;
var canvas = $('<span class="output"></span>');
var ele, wire;
for (var y = 0; y < matrix.length; y++) {
clsMatrix[y] = [];
clsMatrix[y].length = matrix[y].length;
for (var x = 0; x < matrix[y].length; x++) {
wire = null;
ele = $('<span></span>');
switch(matrix[y][x]) {
// Switches are for now considered batteries
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '*':
wire = new Battery();
batteries.push(wire);
break;
case ',':
case '.':
case '`':
case "'":
case 'f':
wire = new CornerWire();
break;
case '}':
case '{':
case '^':
case '?':
wire = new OneWayWire();
break;
case ' ':
// Empty
ele.html(' ');
break;
case '@':
// Light
wire = new Light();
break;
case '>':
// AndGate
wire = new AndGate();
break;
case ')':
wire = new OrGate();
break;
case '$':
wire = new XorGate();
break;
case ']':
// GuardGateRight
wire = new GuardGateRight();
break;
case '!':
// NotGate
wire = new NotGate();
break;
case '#':
// JumpGate (vertical, horizontal)
wire = new JumpGate();
break;
case '&':
// NAndGate
wire = new NAndGate();
break;
default: // Assume wire
wire = new Wire();
break;
}
clsMatrix[y][x] = wire;
if (wire) {
wire.instanceId = currentRunInstance;
wire.setHtmlElement(ele, matrix[y][x]);
wire.setPosition(y, x);
}
canvas.append(ele);
}
// New line
canvas.append($('<br/>'));
}
$("div#display").html(canvas);
rate = 0;
for (var i = 0; i < batteries.length; i++)
batteries[i].setCurrent(0);
}).click();
$("input#speed").change(function() {
currentSpeed = parseInt($(this).val());
});
try {
currentSpeed = parseInt($("input#speed").val());
} catch (e) {
currentSpeed = 0;
}
});
</script>
</head>
<body>
<textarea id="src" rows=10 cols=80>
3:Test 4:Enable
| |
| +---.
| | |
| \ | @
+------\ >--#--+5:True
| >------/ |
---#------/ |
| | \ @
| -------\ >--+6:False
| >----------/
+-----!---/
| /-------------------------------------------
| / --. | The loop there is to synchronise
V | / | | -----\ | the pulse with the OR gate over here.
|------------- | ------------------------/ )-' Using a direct line causes the OR to
| / output the incorrect value for 4 pulses.
--!----------------------------#---+------------------------. |
| | | | |
0:Select-+--------------------------\ | | | |
| >--| | \ |
| / | >-'
\ | \ /
>-*---\ *:SetT1-\ | >---+-\ *:SetT0-\ |
1:Set--*-/ | >------/ )-*-. / | >----/ )-*-.
| | / / | | --#-/ / |
2:SetV-#-*--#--/ Cell 1 | \ | | | Cell 0 | \
| | | | >-. | | ------\ | >-.
| | -----\ *:SetF1-#--!-/ | | | >-*:SetF0-#--!-/ |
| | >-----/ | | | +----!--/ | |
| *----!--/ |-------- | | |--------
| | | |
--#----------------------------------' |
--------------------------------------'
</textarea>
<span>Instructions run: <span id="rateCounter">0</span></span>
<button id="update">Update</button>
Current Speed: <input type="text" id="speed" value="0"/>
<div id="display"></div>
<div id="log"></div>
</body>
</html>