-
Notifications
You must be signed in to change notification settings - Fork 0
/
Um32.js
219 lines (197 loc) · 5.57 KB
/
Um32.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
var Um32 = {
_register: [0,0,0,0,0,0,0,0],
_platters: [],
_finger: 0,
_running: false,
_debug: true,
_cmdA: 0,
_cmdB: 0,
_cmdC: 0,
_clock: 0,
_op0: function(){
// Conditional Move
if(Um32._register[Um32._cmdC] !== 0)
{
Um32._register[Um32._cmdA] = Um32._register[Um32._cmdB];
}
},
_op1: function(){
// Array index
Um32._register[Um32._cmdA] = Um32._platters[Um32._register[Um32._cmdB]][Um32._register[Um32._cmdC]];
},
_op2: function(){
// Array ammendment
Um32._platters[Um32._register[Um32._cmdA]][Um32._register[Um32._cmdB]] = Um32._register[Um32._cmdC];
},
_op3: function(){
// Addition
Um32._register[Um32._cmdA] = (Um32._register[Um32._cmdB] + Um32._register[Um32._cmdC]) % Math.pow(2,32);
},
_op4: function(){
// Multiplication
Um32._register[Um32._cmdA] = (Um32._register[Um32._cmdB] * Um32._register[Um32._cmdC]) % Math.pow(2,32);
},
_op5: function(){
// Division
if(Um32._register[Um32._cmdC] > 0){
var res = Um32._register[Um32._cmdB]>>>0 / Um32._register[Um32._cmdC]>>>0;
Um32._register[Um32._cmdA] = res>>>0;
} else {
Um32._register[Um32._cmdA] = 0;
}
},
_op6: function(){
// Not-And
Um32._register[Um32._cmdA] = ~(Um32._register[Um32._cmdB]>>>0 & Um32._register[Um32._cmdC]>>>0);
},
_op7: function(){
// Halt
Um32._running = false;
alert('halted at clock ' + Um32._clock);
},
_op8: function(){
// Allocation
var maxIndex = Um32._platters.length;
var index;
for(index = 0; index < maxIndex; index++){
if(Um32._platters[index] === null){
break;
}
}
Um32._platters[index] = [];
for(var i = 0; i < Um32._register[Um32._cmdC]; i++){
Um32._platters[index][i] = 0;
}
Um32._register[Um32._cmdB] = index;
},
_op9: function(){
// Abandonment
Um32._platters[Um32._register[Um32._cmdC]] = null;
},
_op10: function(){
// Output
userConsole.writeChar(Um32._register[Um32._cmdC]);
},
_op11: function(){
// Input
//var res = prompt();
//Um32._register[Um32._cmdC] = res.charCodeAt(0);
Um32._running = false;
userConsole.getChar(function(e){
Um32._register[Um32._cmdC] = e;
Um32._running = true;
Um32._opDone();
});
},
_op12: function(){
// Load Program if required
if(Um32._register[Um32._cmdB] > 0){
Um32._platters[0] = Um32._platters[Um32._register[Um32._cmdB]].slice(0);
}
// Jump
Um32._finger = Um32._register[Um32._cmdC];
},
_op13: function(){
// Orthography
var currentPlatter = Um32._platters[0][Um32._finger-1];
Um32._cmdA = (currentPlatter>>>25)&7;
var val = currentPlatter>>>0 & (Math.pow(2,25)-1);
Um32._register[Um32._cmdA] = val;
},
_spin: function(){
var currentPlatter = Um32._platters[0][Um32._finger];
Um32._finger++;
Um32._clock++;
var instruction = currentPlatter>>>28;
Um32._cmdC = currentPlatter>>>0 & 7>>>0;
Um32._cmdB = (currentPlatter>>>3 & 7);
Um32._cmdA = (currentPlatter>>>6 & 7);
var op = Um32._map[instruction];
if(op == undefined){
alert('Undefined op');
Um32._running = false;
} else {
op();
Um32._displayRegisters();
}
Um32._opDone();
},
_opDone: function(){
if(Um32._running) { // && (!Um32._debug || confirm('Continue?'))){
//alert('Run ' + instruction);
if(Um32._debug && (Um32._clock % 1000 == 0)) {
setTimeout(Um32._spin, 0);
} else {
Um32._spin();
}
}
},
_map: [],
start: function(rom){
Um32._clock = 0;
Um32._finger = 0;
Um32._loadRom(rom);
//Um32._platters[0] = rom;
Um32._register[0] = 0;
Um32._register[1] = 0;
Um32._register[2] = 0;
Um32._register[3] = 0;
Um32._register[4] = 0;
Um32._register[5] = 0;
Um32._register[6] = 0;
Um32._register[7] = 0;
Um32._running = true;
Um32._spin();
},
_loadRom: function(rom) {
Um32._platters = [];
Um32._platters[0] = [];
// TODO: This needs to bit shift 32 bits at a time adding a new platter each time...
var i = 0;
var j = 0;
while(i < rom.length) {
var a = rom.charCodeAt(i);
var b = rom.charCodeAt(i+1);
var c = rom.charCodeAt(i+2);
var d = rom.charCodeAt(i+3);
var as = a<<24>>>0;
var bs = b<<16>>>0;
var cs = c<<8>>>0;
var ds = d>>>0;
var res = (as|bs|cs|ds)>>>0;
Um32._platters[0][j] = res;
i = i + 4;
j++;
}
},
_displayRegisters: function() {
userConsole.displayRegisters(
Um32._register[0],
Um32._register[1],
Um32._register[2],
Um32._register[3],
Um32._register[4],
Um32._register[5],
Um32._register[6],
Um32._register[7],
Um32._platters.length
);
}
};
Um32.
_map = [
Um32._op0,
Um32._op1,
Um32._op2,
Um32._op3,
Um32._op4,
Um32._op5,
Um32._op6,
Um32._op7,
Um32._op8,
Um32._op9,
Um32._op10,
Um32._op11,
Um32._op12,
Um32._op13
];