forked from vgath-8086/vgath-8086.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.js
More file actions
65 lines (46 loc) · 1.55 KB
/
Copy pathMemory.js
File metadata and controls
65 lines (46 loc) · 1.55 KB
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
class Memory{
constructor(size){
this.size = size;//Size est donné en octect
this._buffer = new Array(this.size);
for (let i = 0; i < size; i++)
this._buffer[i] = 0;
}
readByte(address){
address = this.testAddress(address);
return this._buffer[address];
}
readWord(address){
address = this.testAddress(address);
return this._buffer[address] + (this._buffer[address+1] << 8);
}
writeByte(address, value){
address = this.testAddress(address);
if (value >> 8 != 0)
console.log("Error: Trying to write more than a byte.");
else
this._buffer[address] = value;
}
writeWord(address, value){
address = this.testAddress(address);
if (value >> 16 != 0)
console.log("Error: Trying to write more than a word.");
else
{
this._buffer[address + 1] = value >> 8; //Le byte de poid fort
this._buffer[address] = value % 256; //Le byte de poid faible
}
}
testAddress(address){
if (address > this.size) {
console.log("Warning: Trying to access an unmapped address of the memory ");
address %= this.size;
}
return address;
}
dump(){
var copy = new Array(this.size);
for (let i = 0; i < this.size; i++)
copy[i] = this._buffer[i];
return copy;
}
}