/
sky8.js
304 lines (287 loc) · 10.5 KB
/
sky8.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* eslint-disable no-case-declarations */
const mem=4096;
const registers=16;
class Sky8{
constructor(Screen,Keyboard){
this.memory= new Uint8Array(mem);
this.v= new Uint8Array(registers);
this.index=0;
this.pc=0x200;
this.stack=[];
this.sp=0;
this.delayTimer=0;
this.soundTimer=0;
this.keyboard=Keyboard;
this.screen=Screen;
this.paused=false;
this.speed=10;
}
spritesToMem() {
const sprites = [
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
];
for (let i = 0; i < sprites.length; i++) {
this.memory[i] = sprites[i];
}
}
programToMem(program){
for(let i=0; i<program.length;i++){
this.memory[0x200+i]=program[i];
}
}
cycle(){
for(let i=0;i<this.speed;i++){
if(!this.paused){
let opcode=(this.memory[this.pc]<<8 |this.memory[this.pc+1]);
this.interpret(opcode);
}
}
if(!this.paused)
this.updateTimer();
//work on it this.sound();
this.screen.fill();
}
updateTimer(){
if(this.delayTimer>0)
this.delayTimer-=1;
if(this.soundTimer>0)
this.soundTimer-=1;
}
interpret(instruction){
this.pc +=2;
let x=(instruction & 0x0F00) >>8;
let y=(instruction & 0x00F0) >>4;
switch(instruction & 0xF000){
case 0x0000:
switch(instruction){
case 0x00E0:
this.screen.clearscr();
console.log(instruction,this.pc)
break;
case 0x0EE:
this.pc=this.stack.pop();
break;
}
console.log(instruction,this.pc)
break;
case 0x1000:
this.pc=instruction & 0xFFF;
console.log(instruction,this.pc)
break;
case 0x2000:
this.stack.push(this.pc);
this.pc=(instruction & 0xFFF);
console.log(instruction,this.pc)
break;
case 0x3000:
if(this.v[x]==(instruction & 0xFF)){
this.pc+=2;
}
console.log(instruction,this.pc)
break;
case 0x4000:
if(this.v[x] !=(instruction & 0xFF)){
this.pc+=2;
}
console.log(instruction,this.pc)
break;
case 0x5000:
if(this.v[x]==this.v[y]){
this.pc+=2;
}
console.log(instruction,this.pc)
break;
case 0x6000:
this.v[x]=(instruction & 0xFF);
console.log(instruction,this.pc)
break;
case 0x7000:
this.v[x]+=(instruction & 0xFF);
console.log(instruction,this.pc)
break;
case 0x8000:
switch(instruction & 0xF){
case 0x0:
this.v[x]=this.v[y];
console.log(instruction,this.pc)
break;
case 0x1:
this.v[x]=this.v[x] | this.v[y];
break;
case 0x2:
this.v[x]=this.v[x] & this.v[y];
break;
case 0x3:
this.v[x]=this.v[x] ^ this.v[y];
break;
case 0x4:
let add=this.v[x]+this.v[y];
this.v[0xF]=0;
if(add>0xFF){
this.v[0xF]=1;
}
this.v[x]=add;
console.log(instruction,this.pc)
break;
case 0x5:
let sub=this.v[x]-this.v[y];
this.v[0xF]=1;
if(this.v[x]<this.v[y]){
this.v[0xF]=0;
}
this.v[x]=sub;
console.log(instruction,this.pc)
break;
case 0x6:
//work on it
this.v[0xF] = this.v[x] & 0x1;
this.v[x] >>=1;
console.log(instruction,this.pc)
break;
case 0x7:
this.v[0xF]=0;
if(this.v[x]<this.v[y]){
this.v[0xF]=1;
}
this.v[x]=this.v[y]-this.v[x];
console.log(instruction,this.pc)
break;
case 0xE:
//work on it
this.v[0xF]=this.v[x] & 0x80;
this.v[x] <<=1;
console.log(instruction,this.pc)
break;
default:
throw new Error('BAD OPCODE');
}
break;
case 0x9000:
if(this.v[x]!=this.v[y]){
this.pc+=2;
}
console.log(instruction,this.pc)
break;
case 0xA000:
this.index=(instruction & 0x0FFF);
console.log(instruction,this.pc)
break;
case 0xB000:
this.pc=(instruction & 0xFFF)+this.v[0];
console.log(instruction,this.pc)
break;
case 0xC000:
let rand=Math.floor(Math.random()*0xFF);
this.v[x]=(instruction & 0xFF) & rand;
console.log(instruction,this.pc)
break;
case 0xD000:
let width = 8;
let height = (instruction & 0xF);
this.v[0xF] = 0;
for(let rows = 0; rows < height; rows++) {
let sprite = this.memory[this.index + rows];
for(let cols = 0; cols < width; cols++) {
if((sprite & 0x80) > 0) {
if(this.screen.setPixel(this.v[x] + cols, this.v[y] + rows)) {
this.v[0xF] = 1;
}
}
sprite <<= 1;
}
}
console.log(instruction,this.pc)
break;
case 0xE000:
switch(instruction & 0xF){
case 0xE:
if(this.keyboard.isKeyPressed(this.v[x])){
this.pc+=2;
}
console.log(instruction,this.pc)
break;
case 0x1:
if(!this.keyboard.isKeyPressed(this.v[x])){
this.pc+=2;
}
console.log(instruction,this.pc)
break;
default:
throw new Error("BAD OPCODE");
}
break;
case 0xF000:
switch(instruction & 0xFF){
case 0x07:
this.v[x]=this.delayTimer;
console.log(instruction,this.pc)
break;
case 0x0A:
this.paused=true;
let nextKeyPress=(key)=>{
this.v[x]=key;
this.paused=false;
};
this.keyboard.onNextKeyPress=nextKeyPress.bind(this);
console.log(instruction,this.pc)
break;
case 0x15:
this.delayTimer=this.v[x];
console.log(instruction,this.pc)
break;
case 0x18:
this.soundTimer=this.v[x];
console.log(instruction,this.pc)
break;
case 0x1E:
this.index+=this.v[x];
console.log(instruction,this.pc)
break;
case 0x29:
this.index=this.v[x]*5;
console.log(instruction,this.pc)
break;
case 0x33:
this.memory[this.index]=parseInt(this.v[x]/100);
this.memory[this.index+1]=parseInt((this.v[x]%100)/10);
this.memory[this.index+2]=parseInt(this.v[x]%10);
console.log(instruction,this.pc)
break;
case 0x55:
for(let i=0;i<=x;i++){
this.memory[this.index+i]=this.v[i];
}
console.log(instruction,this.pc)
break;
case 0x65:
for(let i=0;i<=x;i++){
this.v[i]=this.memory[this.index+i];
}
console.log(instruction,this.pc)
break;
default:
throw new Error("0xF BAD OPCODE "+instruction);
}
console.log(instruction,this.pc)
break;
default:
throw new Error("BAD OPCODE");
}
}
}
export default Sky8;