Skip to content

Commit 1a4f094

Browse files
committed
Speicher Simmulation Hinzugefügt
1 parent 6ec90ed commit 1a4f094

8 files changed

Lines changed: 681 additions & 0 deletions

File tree

programs/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<a href="https://github.com/TUBAF-IfI-LiaScript/VL_EingebetteteSysteme">VL_EingebetteteSysteme Repository</a><br><br>
88

99
<a href="./rechenwerk/index.html">Rechenwerk</a><br>
10+
<a href="./rechenwerk/memory.html">Speicher</a><br>
1011
<!-- Links zu weiteren Programmen -->
1112
</body>
1213
</html>

programs/memory/basictypes.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
toUInt(value) {
65+
let ret = 0;
66+
for (let i = 0; i < value.length; i++) if (value[i]) ret |= (1 << i);
67+
return ret;
68+
}
69+
}
70+
/*
71+
class WordsDatatype extends Datatype {
72+
constructor(size,wordDatatype) {
73+
this._size = size;
74+
this._wordDatatype = wordDatatype;
75+
}
76+
getName() {
77+
return "words:" + this._size + ":" + this._wordDatatype.getName();
78+
}
79+
is(value) {
80+
if (!(value instanceof Array)) return false;
81+
if (value.length !== this._size) return false;
82+
for (let w of value) this._wordDatatype.check(b);
83+
return true;
84+
}
85+
}
86+
87+
class BitsWordsDatatype extends WordsDatatype {
88+
constructor(size,bitsLength) {
89+
super(size,new BitsDatatype(bitsLength));
90+
}
91+
toLinesString(value, reverse) {
92+
if (value.length === 0) return "";
93+
let ret = "";
94+
for (let i = 0; i < value.length; i++) {
95+
if (i > 1) ret += "\n";
96+
ret += this._wordDatatype.to01String(value[i],reverse);
97+
}
98+
}
99+
parseLinesString(linesString, reverse) {
100+
let li = linesString.split("\n");
101+
let words = [];
102+
for (let l of li) ret.push(this._wordDatatype.parseFrom01String(l));
103+
this.check(words);
104+
return words;
105+
}
106+
}
107+
*/
108+
class ElementOfSetDatatype extends Datatype {
109+
constructor(elements,setName) {
110+
super();
111+
console.log(elements,setName);
112+
if (setName === undefined) setName = "set";
113+
this._elements = elements;
114+
this._setName = setName;
115+
}
116+
getName() {
117+
return "element of " + this.setName;
118+
}
119+
is(value) {
120+
return this._elements.indexOf(value) !== -1;
121+
}
122+
getDefaultValue() {
123+
return this._elements[0];
124+
}
125+
126+
getElements() {
127+
return this._elements;
128+
}
129+
}

programs/memory/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/memory/fjangnodes.js

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

programs/memory/img/mem.png

27.4 KB
Loading

0 commit comments

Comments
 (0)