-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.js
279 lines (224 loc) · 6.7 KB
/
machine.js
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
// Private helpers
var _new_thread = function(program_name) {
return {
program_name: program_name,
ip: 0,
runnable: true
};
};
var _new_semaphore = function(name, initial_count) {
return {
name: name,
count: initial_count,
thread_waiting_list: []
};
};
var _set_ip = function(m, tid, to) {
var thread = m.threads[tid];
var program_name = thread.program_name;
var program = m.programs[program_name];
if (to <= program.length) { thread.ip = to; }
};
// increment instruction pointer for thread tid
var _incr_ip = function(m, tid) {
var thread = m.threads[tid];
_set_ip(m, tid, thread.ip + 1);
};
// Instructions
var I_semaphore_make = function(name, initial_count) {
this.name = name;
this.count = initial_count;
this.apply = function(m, current_thread_id) {
m[name] = _new_semaphore(name, initial_count);
_incr_ip(m, current_thread_id);
};
this.human_readable_string = function() {
return "semaphore_make " + this.name;
};
};
var I_semaphore_p = function(name) {
this.apply = function(m, current_thread_id) {
var semaphore = m[name];
semaphore.count--;
if (semaphore.count < 0) {
semaphore.thread_waiting_list.push(current_thread_id);
m.threads[current_thread_id].runnable = false;
}
_incr_ip(m, current_thread_id);
};
this.human_readable_string = function() {
return "semaphore_p " + name;
};
};
var I_semaphore_v = function(name) {
this.apply = function(m, current_thread_id) {
var semaphore = m[name];
if (semaphore.count < 0) {
var thread_id = semaphore.thread_waiting_list.pop();
m.threads[thread_id].runnable = true;
}
semaphore.count++;
_incr_ip(m, current_thread_id);
};
this.human_readable_string = function() {
return "semaphore_v " + name;
};
};
var I_print = function(s) {
this.s = s;
this.apply = function(m, current_thread_id) {
println(s);
_incr_ip(m, current_thread_id);
};
this.human_readable_string = function() {
return "print '" + this.s + "'";
};
};
var I_spawn_thread = function(program_name) {
this.apply = function(m, current_thread_id) {
m.threads.push(_new_thread(program_name));
_incr_ip(m, current_thread_id);
};
this.human_readable_string = function() {
return "spawn_thread '" + program_name + "'";
};
};
var I_jmp = function(to) {
this.apply = function(m, current_thread_id) {
_set_ip(m, current_thread_id, to);
};
this.human_readable_string = function() {
return "jmp " + to + "";
};
};
// Working memory
var M = {
programs: {},
threads: [ _new_thread("start") ]
};
// Code
// entry point
M.programs.start = [
new I_semaphore_make("s", 1),
new I_spawn_thread("program0"),
new I_spawn_thread("program1"),
new I_print("started!"),
new I_jmp(0)
];
M.programs.program0 = [
new I_semaphore_p("s"),
new I_print("program0"),
new I_semaphore_v("s")
];
M.programs.program1 = [
new I_semaphore_p("s"),
new I_print("program1"),
new I_semaphore_v("s")
];
// x-ray vision into the CPU
var draw_instruction = function(instruction, current, runnable, r) {
var padding = 10;
var y = r.y + padding;
var x = r.x + padding;
var instr_width = r.width - 2 * padding;
var fontSize = instr_width / 15;
// border
if (current === true) {
if (runnable) {
fill(101, 189, 0);
} else {
fill(128, 128, 128);
}
} else {
noFill();
}
r.height = padding * 2 + fontSize;
rect(r.x, r.y, r.width, r.height);
// text
if (current === true) {
fill(255, 255, 255);
} else {
fill(0, 0, 0);
}
textFont(createFont("monospace"), fontSize);
textAlign(CENTER, TOP);
text(instruction.human_readable_string(), x + instr_width / 2, y);
return r.height;
};
var draw_ip_arrow = function(x, y, side) {
fill(255, 0, 0);
noStroke();
triangle(x, y, x - side, y - side / 2, x - side, y + side / 2);
stroke(0, 0, 0);
};
var draw_thread = function(m, thread, r) {
if (thread.runnable) {
fill(255, 255, 255);
} else {
fill(192, 192, 192);
}
rect(r.x, r.y, r.width, r.height);
var padding = r.width / 20;
var width = r.width - 2 * padding;
var x = r.x + padding;
var y = r.y + padding;
var program = m.programs[thread.program_name];
for (var i = 0; i < program.length; i++) {
var is_ip_at_instruction = i === thread.ip;
var height = draw_instruction(program[i], is_ip_at_instruction, thread.runnable, { x: x, y: y, width: width });
if (is_ip_at_instruction) { draw_ip_arrow(x, y, padding); }
y += height + padding;
}
};
var Layout = function(m) {
var margin = 10;
var padding = 10;
var n = m.threads.length;
var avail_height = height - 2 * margin;
var avail_width = width - 2 * margin;
var total_padding_width = (n - 1) * padding; // yeah, n > 0
this.thread_width = (avail_width - total_padding_width) / n;
this.thread_height = avail_height - 20;
this.rect = { x: margin, y: margin, width: avail_width, height: avail_height };
this.padding = padding;
this.tick_button_i = function(x, y) {
var i_fraction = x / (width / n); // approximately!
return i_fraction | 0; // float -> int in javascript...
};
};
var draw_canvas = function(m) {
background(255, 255, 255);
var layout = new Layout(m);
var rect = layout.rect;
var x = rect.x;
var y = rect.y;
for (var i = 0; i < m.threads.length; i++) {
draw_thread(m, m.threads[i], { x: x, y: y, width: layout.thread_width, height: layout.thread_height });
x += layout.padding + layout.thread_width;
}
};
// CPU
var core_count = 1; // unused
var tick_thread = function(m, thread_i) {
var thread = m.threads[thread_i];
var program = m.programs[thread.program_name];
if (thread.runnable && thread.ip < program.length) {
// get instruction
var instruction = program[thread.ip];
// execute instruction
instruction.apply(m, thread_i);
}
draw_canvas(m);
};
var boot = function(m) {
draw_canvas(m);
};
// BIOS (I might be taking this analogy too far...)
boot(M);
// quartz crystal + scheduler
var mouseClicked = function() {
var layout = new Layout(M);
var thread_i = layout.tick_button_i(mouseX, mouseY);
debug(thread_i);
tick_thread(M, thread_i);
};