-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc-core.js
More file actions
240 lines (177 loc) · 6.26 KB
/
rc-core.js
File metadata and controls
240 lines (177 loc) · 6.26 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
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
class ALU {
static normalize(address) {
const val = address % ALU.core_size
return val
}
static sanitize(address) {
const cs = ALU.core_size
return (address + cs * cs) % cs
}
}
class RAM {
constructor(environment) {
this.current_process_index = -1
this.environment = environment
this.clear()
}
clear() {
this.memory = []
for (var a = 0; a < this.environment.core_size; a++) {
this.memory[a] = new Instruction(DAT, null, null)
}
}
r(address) {
address = ALU.sanitize(address)
this.memory[address].read_flag = this.current_process_index
return this.memory[address]
}
w(address, instruction) {
address = ALU.sanitize(address)
// copy values individually to prevent override
this.memory[address] = instruction.copy()
this.memory[address].read_flag = -1
this.memory[address].write_flag = this.current_process_index
}
random_address(size) {
while (1) {
const address = ~~(Math.random() * this.memory.length)
var fits = true
for (var o = 0; o < size; o++) {
const a = ALU.sanitize(o + address)
if (this.memory[a].write_flag !== -1) {
fits = false
break
}
}
if (fits) {
return address
}
}
}
}
class Core {
constructor(environment) {
ALU.core_size = environment.core_size
this.environment = environment
this.cu = new ControlUnit()
this.ram = new RAM(environment)
this.reset()
}
set_current_process_index(i) {
this.current_process_index = i
this.ram.current_process_index = i
}
reset() {
this.cycle = 0
this.active = true
this.processes = []
this.ram.clear()
this.set_current_process_index(0)
}
load_program(program, address) {
if (this.processes.length == 0) {
// first program always loads at address 0
address = 0
}
else if (address === undefined) {
// generate random address if none is specified
address = this.ram.random_address(program.instructions.length)
}
const pid = this.processes.length
this.ram.current_process_index = pid
for (var a = 0; a < program.instructions.length; a++) {
this.ram.w(a + address, program.instructions[a])
}
this.processes[pid] = new Process(program, this.environment.max_threads)
this.processes[pid].push(address + program.load_address)
this.set_current_process_index(0)
}
current_process() {
return this.processes[this.current_process_index]
}
step() {
// run the currently selected thread
this.cycle++
if (this.processes.length) {
// get next instruction address
const addr = this.current_process().pop()
// fetch and execute
this.step_instruction(addr)
// prepare next cycle process
this.set_current_process_index((1 + this.current_process_index) % this.processes.length)
}
return this.active
}
step_instruction(address) {
// INSTRUCTION FETCH executes all pre/post-inc/decrements
const instruction = this.cu.fetch(address, this.ram)
if (instruction === undefined) {
throw 'MemoryCorruptedException'
}
// special handling for the for opcodes that spawn new threads
if (instruction.op.forks) {
this.current_process().push(address + 1)
}
if (this.ram.memory[address] === undefined) {
console.log('address: ' + address)
console.log('mem size' + this.ram.memory.length)
}
// INSTRUCTION EXECUTION returns the next program counter
this.ram.memory[address].execution_flag = this.current_process_index
var next_pc = instruction.execute(address, this.ram)
if (next_pc != null) {
this.current_process().push(address + next_pc)
} else {
// instruction could not be executed and returned null
const id = this.current_process_index
if (this.current_process().num_threads() == 0) {
// remove process
this.processes.splice(id, 1)
// select next process
if (id >= this.processes.length) {
this.set_current_process_index(0)
}
if (this.processes.length == 1) {
// console.log(`End of Game: Process #${0} wins after ${this.cycle} cycles`)
this.active = false
}
}
}
}
/* UI IO stuff that kinda needs to be moved out of here *******************/
next_instruction() {
const p = this.current_process()
if (p === undefined) {
return null
}
const n = p.next()
if (n === undefined) {
return null
}
return this.ram.memory[n]
}
dump() {
return this.memory_dump(0, core.environment.core_size)
}
print_memory(start, end) {
console.log(this.memory_dump(start, end).join('\n'))
}
memory_dump(start, end) {
function padln(num, count) {
num = '' + num
while (num.length < count) {
num = '0' + num
}
return num
}
var output = new Array()
const max_width = ('' + (core.environment.core_size - 1)).length
for (var a = start; a < end; a++) {
var addr = ALU.sanitize(a)
var i = this.ram.memory[addr]
var out = padln(addr, max_width) + ' ' + i.to_string(max_width + 1)
output.push(out)
}
return output
}
}