Skip to content

Commit 19dcf11

Browse files
committed
Rechenwerk Simmulation Hinzugefügt
1 parent ea5c29b commit 19dcf11

7 files changed

Lines changed: 655 additions & 0 deletions

File tree

programs/rechenwerk/basictypes.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class BitDatatype extends Datatype {
2+
constructor() {
3+
super();
4+
}
5+
getName() {
6+
return "bit";
7+
}
8+
is(value) {
9+
return value === true || value === false;
10+
}
11+
getDefaultValue() {
12+
return false;
13+
}
14+
15+
parse(x) {
16+
return x ? true : false;
17+
}
18+
}
19+
BitDatatype.STATIC = new BitDatatype();
20+
21+
class BitsDatatype extends Datatype {
22+
constructor(bitsLength) {
23+
super();
24+
this._bitsLength = bitsLength;
25+
}
26+
getName() {
27+
return "bits:" + this._bitsLength;
28+
}
29+
is(value) {
30+
if (!(value instanceof Array)) return false;
31+
if (value.length !== this._bitsLength) return false;
32+
for (let b of value) BitDatatype.STATIC.check(b);
33+
return true;
34+
}
35+
getDefaultValue() {
36+
let ret = [];
37+
for (let i = 0; i < this._bitsLength; i++) ret.push(false);
38+
return ret;
39+
}
40+
41+
getBitsLength() {
42+
return this._bitsLength;
43+
}
44+
parseFromArray(arr) {
45+
let ret = [];
46+
for (let i = 0; i < this._bitsLength; i++) ret.push(BitDatatype.STATIC.parse(arr[i]));
47+
return ret;
48+
}
49+
parseFrom01String(str,reverse) {
50+
let bits = [];
51+
for (let c of str) {
52+
bits.push(c === "1");
53+
}
54+
if (reverse) bits.reverse();
55+
this.check(bits);
56+
return bits;
57+
}
58+
to01String(value,reverse) {
59+
let ret = "";
60+
for (let i = 0; i < this._bitsLength; i++) ret += value[i] ? "1" : "0";
61+
if (reverse) ret = ret.split("").reverse().join("");
62+
return ret;
63+
}
64+
}
65+
66+
class ElementOfSetDatatype extends Datatype {
67+
constructor(elements,setName) {
68+
super();
69+
console.log(elements,setName);
70+
if (setName === undefined) setName = "set";
71+
this._elements = elements;
72+
this._setName = setName;
73+
}
74+
getName() {
75+
return "element of " + this.setName;
76+
}
77+
is(value) {
78+
return this._elements.indexOf(value) !== -1;
79+
}
80+
getDefaultValue() {
81+
return this._elements[0];
82+
}
83+
84+
getElements() {
85+
return this._elements;
86+
}
87+
}

programs/rechenwerk/component.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class FjangComponent {
2+
constructor() {
3+
4+
}
5+
getNodes() {
6+
throw new Error("FjangComponent.getNodes() is abstract");
7+
}
8+
tick() {
9+
10+
}
11+
}
12+
13+
class FjangWorld {
14+
constructor(components) {
15+
this._components = components;
16+
this._graph = new FjangGraph();
17+
for (let c of components) this._graph.addNodes(c.getNodes());
18+
}
19+
tick() {
20+
this._graph.tick();
21+
for (let c of this._components) c.tick();
22+
// this._graph.tick();
23+
}
24+
}

programs/rechenwerk/fjangnodes.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
class FjangGraph {
2+
constructor(nodes) {
3+
if (nodes === undefined) nodes = [];
4+
this.nodes = nodes;
5+
}
6+
isInGraph(n) {
7+
return this.nodes.indexOf(n) !== -1;
8+
}
9+
addNode(n) {
10+
if (this.isInGraph(n)) return;
11+
this.nodes.push(n);
12+
}
13+
removeNode(n) {
14+
if (!this.isInGraph(n)) return;
15+
for (let i = 0; i < this.nodes.length; i++) {
16+
if (this.nodes[i] === n) {
17+
this.nodes.splice(i,1);
18+
i--;
19+
}
20+
}
21+
}
22+
addNodes(ns) {
23+
for (let n of ns) this.addNode(n);
24+
}
25+
removeNodes(ns) {
26+
for (let n of ns) this.removeNode(n);
27+
}
28+
tick() {
29+
for (let n of this.nodes) n.tick();
30+
}
31+
}
32+
33+
class FjangNode {
34+
constructor(datatype) {
35+
this._datatype = datatype;
36+
this._valueOld = datatype.getDefaultValue();
37+
this._valueNew = datatype.getDefaultValue();
38+
this._connectedNodes = [];
39+
this._setBlocker = false;
40+
// this._wasSetedOld = true;//true, if set()
41+
// this._wasSetedNew = true;
42+
}
43+
get() {
44+
return this._valueOld;
45+
}
46+
set(value) {
47+
if (this._setBlocker) return;
48+
49+
this._datatype.check(value);//throws DatatypeError, if value is not valid
50+
51+
this._valueNew = value;
52+
53+
this._setBlocker = true;
54+
this._send();
55+
this._setBlocker = false;
56+
}
57+
tick() {
58+
this._valueOld = this._valueNew;
59+
// this._wasSetedOld = this._wasSetedNew;
60+
// this._wasSetedNew = false;
61+
}
62+
/* wasSeted() {
63+
return this._wasSetedOld;
64+
}*/
65+
isConnected(n) {
66+
return this._connectedNodes.indexOf(n) !== -1;
67+
}
68+
addConnection(n) {
69+
if (!(n instanceof FjangNode)) throw new Error("FjangNode.addConnection(n): n is not a FjangNode!");
70+
if (n === this) return;
71+
if (this.isConnected(n)) return;
72+
this._connectedNodes.push(n);
73+
}
74+
removeConnection(n) {
75+
if (!this.isConnected(n)) return;
76+
for (let i = 0; i < this._connectedNodes.length; i++) {
77+
if (this._connectedNodes[i] === n) {
78+
this._connectedNodes.splice(i,1);
79+
i--;
80+
}
81+
}
82+
}
83+
84+
_send() {
85+
for (let n of this._connectedNodes) n.set(this._valueNew);
86+
}
87+
88+
static connectFromTo(a,b) {
89+
a.addConnection(b);
90+
}
91+
static connectBi(a,b) {
92+
a.addConnection(b);
93+
b.addConnection(a);
94+
}
95+
static connectOnlyFromTo(a,b) {
96+
a.addConnection(b);
97+
b.removeConnection(a);
98+
}
99+
static removeConnectionFromTo(a,b) {
100+
a.removeConnection(b);
101+
}
102+
static removeConnectionBi(a,b) {
103+
a.removeConnection(b);
104+
b.removeConnection(a);
105+
}
106+
}
107+
108+
//or interface
109+
class Datatype {
110+
constructor() {
111+
112+
}
113+
getName() {
114+
return "abstract datatype";
115+
}
116+
//abstract, returns true, if value is valid
117+
is(value) {
118+
throw new Error("Datatype.is() is abstract");
119+
}
120+
121+
//throws DatatypeError, if value is not valid, otherwise returns undefined
122+
getDefaultValue() {
123+
throw new Error("Datatype.getDefaultValue() is abstract");
124+
}
125+
126+
check(value) {
127+
if (!this.is(value)) throw new DatatypeError(this,value,"Check Failed (value=" + value + ", datatype=" + this.getName() + ")");
128+
}
129+
}
130+
131+
//example:
132+
/*
133+
* class BitDatatype extends Datatype {
134+
* constructor() {
135+
* super();
136+
* }
137+
* getName() {
138+
* return "bit";
139+
* }
140+
* is(value) {
141+
* return value === true || value === false;
142+
* }
143+
* parse(x) {
144+
* return x ? true : false;
145+
* }
146+
* }
147+
*/
148+
149+
class DatatypeError extends Error {
150+
constructor(datatype,invalidValue,msg) {
151+
if (msg === undefined) msg = "Invalid Data";
152+
super(msg);
153+
this.datatype = datatype;
154+
this.invalidValue = invalidValue;
155+
}
156+
}
48.7 KB
Loading

programs/rechenwerk/index.html

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8"/>
4+
<title>Rechenwerk</title>
5+
<script type="text/javascript" src="fjangnodes.js"></script>
6+
<script type="text/javascript" src="basictypes.js"></script>
7+
<script type="text/javascript" src="component.js"></script>
8+
<script type="text/javascript" src="webinterfacenodes.js"></script>
9+
<script type="text/javascript" src="modell.js"></script>
10+
<script type="text/javascript">
11+
12+
const BITLENGTH = 16;
13+
14+
15+
onload = function() {
16+
let checkboxClassName = "cbx";
17+
let inputTextClassName = "inptext";
18+
let selectClassName = "";
19+
20+
21+
22+
let arc = BITLENGTH;
23+
let bitsDatatype = new BitsDatatype(arc);
24+
25+
let alu = new AluSimple(arc);
26+
let a = new BusPlusExtraInputRegister(bitsDatatype);
27+
let z = new Register(bitsDatatype);
28+
29+
let op = new ElementOfSetIO(AluSimple.OPERATION_DATATYPE.getElements(),false,selectClassName);
30+
let ci = new BitIO(false,checkboxClassName);//carryIn
31+
let clk = new BitIO(false,checkboxClassName);//clock
32+
let aIn = new BitIO(false,checkboxClassName);//a input enable
33+
let aOut = new BitIO(false,checkboxClassName);//a output enable
34+
let aToZ = new BitIO(false,checkboxClassName);//a --> z transfer enable
35+
let oToA = new BitIO(false,checkboxClassName);//alu out --> a transfer enable
36+
let valBus = new Bits10IO(arc,false,inputTextClassName);
37+
let valA = new Bits10IO(arc,true,inputTextClassName);
38+
let valZ = new Bits10IO(arc,true,inputTextClassName);
39+
let valAluOut = new Bits10IO(arc,true,inputTextClassName);
40+
let co = new BitIO(true,checkboxClassName);//carryOut
41+
let ov = new BitIO(true,checkboxClassName);//overflow
42+
43+
let world = new FjangWorld([alu,a,z,op,ci,clk,aIn,aOut,aToZ,oToA,valBus,valA,valZ,valAluOut,co,ov]);
44+
45+
//datensignale
46+
FjangNode.connectBi(valBus.bitsNode,a.busNode);
47+
FjangNode.connectFromTo(valBus.bitsNode,alu.yNode);
48+
FjangNode.connectFromTo(a.valueNode,z.busNode);
49+
FjangNode.connectFromTo(z.valueNode,alu.xNode);
50+
FjangNode.connectFromTo(alu.outNode,a.secondInputNode);
51+
52+
FjangNode.connectFromTo(a.valueNode,valA.bitsNode);
53+
FjangNode.connectFromTo(z.valueNode,valZ.bitsNode);
54+
FjangNode.connectFromTo(alu.outNode,valAluOut.bitsNode);
55+
56+
//steuersignale
57+
58+
FjangNode.connectFromTo(clk.bitNode,a.clockNode);
59+
FjangNode.connectFromTo(clk.bitNode,z.clockNode);
60+
FjangNode.connectFromTo(op.eosNode,alu.opNode);
61+
FjangNode.connectFromTo(aIn.bitNode,a.inputEnableBusNode);
62+
FjangNode.connectFromTo(aOut.bitNode,a.outputEnableNode);
63+
FjangNode.connectFromTo(aToZ.bitNode,z.inputEnableNode);
64+
FjangNode.connectFromTo(oToA.bitNode,a.inputEnableSecondNode);
65+
FjangNode.connectFromTo(ci.bitNode,alu.carryInNode);
66+
67+
68+
//output flags
69+
FjangNode.connectFromTo(alu.carryOutNode,co.bitNode);
70+
FjangNode.connectFromTo(alu.overflowNode,ov.bitNode);
71+
72+
73+
document.getElementById("valBus").appendChild(valBus.getHtmlElement());
74+
document.getElementById("valA").appendChild(valA.getHtmlElement());
75+
document.getElementById("valZ").appendChild(valZ.getHtmlElement());
76+
document.getElementById("valAluOut").appendChild(valAluOut.getHtmlElement());
77+
document.getElementById("clk").appendChild(clk.getHtmlElement());
78+
document.getElementById("aIn").appendChild(aIn.getHtmlElement());
79+
document.getElementById("aOut").appendChild(aOut.getHtmlElement());
80+
document.getElementById("aToZ").appendChild(aToZ.getHtmlElement());
81+
document.getElementById("oToA").appendChild(oToA.getHtmlElement());
82+
document.getElementById("ci").appendChild(ci.getHtmlElement());
83+
document.getElementById("co").appendChild(co.getHtmlElement());
84+
document.getElementById("ov").appendChild(ov.getHtmlElement());
85+
document.getElementById("op").appendChild(op.getHtmlElement());
86+
87+
88+
window.setInterval(function() {world.tick()},100);
89+
}
90+
91+
</script>
92+
<style>
93+
.cbx {
94+
margin:0px;
95+
}
96+
.inptext {
97+
width:220px;
98+
min-width:215px;
99+
max-width:215px;
100+
font-size:20px;
101+
overflow:hidden;
102+
}
103+
104+
.fliege {
105+
position: absolute;
106+
}
107+
108+
</style>
109+
110+
</head>
111+
<body>
112+
<img src="img/rechenwerk.png" class="fliege" style="top:0px; left:0px; width:512px; height:512px">
113+
<span id="valBus" class="fliege" style="top:0px; left:0px;"></span>
114+
<span id="valA" class="fliege" style="top:55px; left:155px;"></span>
115+
<span id="valZ" class="fliege" style="top:165px; left:155px;"></span>
116+
<span id="valAluOut" class="fliege" style="top:360px; left:360px;"></span>
117+
<span class="fliege" style="top:5px; left:370px;">clock:<span id="clk"></span></span>
118+
<span id="aIn" class="fliege" style="top:52px; left:124px;"></span>
119+
<span id="aOut" class="fliege" style="top:84px; left:80px;"></span>
120+
<span id="aToZ" class="fliege" style="top:124px; left:245px;"></span>
121+
<span id="oToA" class="fliege" style="top:70px; left:389px;"></span>
122+
<span class="fliege" style="top:290px; left:90px;">carry in:<span id="ci"></span></span>
123+
<span class="fliege" style="top:430px; left:340px;">carry out:<span id="co"></span></span>
124+
<span class="fliege" style="top:460px; left:340px;">overflow:<span id="ov"></span></span>
125+
<span id="op" class="fliege" style="top:343px; left:286px;"></span>
126+
127+
128+
</body>
129+
</html>

0 commit comments

Comments
 (0)