Source: chip8-cpu.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
var chip8;

(function (chip8) {
    'use strict';

    var
        /**
         * Memory size in bytes.
         * @type {number}
         * @private
         */
        MEM_SIZE = 4096, // 4K

        /**
         * Memory location at which programs will be loaded.
         * @type {number}
         * @private
         */
        PROGRAM_START_ADDR = 0x0200,

        /**
         * Timers update frequency.
         * @type {number}
         * @private
         */
        SPEED = 60, // Hz

        /**
         * Number of steps performed in one cycle.
         * @type {number}
         * @private
         */
        STEPS_PER_CYCLE = 10,

        /**
         * Stack size in bytes
         * @type {number}
         * @private
         */
        STACK_SIZE = 16;

    /**
     * Defines font sprites.
     * @type {number[]}
     * @private
     */
    var FONTS = [
        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
    ];

    /**
     * Creates CPU instance.
     * @param params
     * @param {chip8.Keyboard} params.keyboard - Keyboard which handles input.
     * @param {chip8.Screen} params.screen - Screen which handles output.
     * @param {chip8.Audio} params.audio - Audio module which handles emitting sounds.
     * @class chip8.CPU
     */
    function CPU(params) {
        this.keyboard = params.keyboard;
        this.screen = params.screen;
        this.audio = params.audio;
        this.reset();
    }

    /**
     * Loads font set into memory.
     * @function loadFontSet
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.loadFontSet = function () {
        for (var i = 0; i < FONTS.length; ++i) {
            this.mem[i] = FONTS[i];
        }
    };

    /**
     * Loads binary data into memory starting at location 0x0200
     * @param {ArrayBuffer} binData - data to load
     * @param {number} [offset] - offset added to starting address
     * @function loadToMemory
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.loadToMemory = function (binData, offset) {
        for (var i = 0; i < binData.byteLength; ++i) {
            this.mem[PROGRAM_START_ADDR + (offset || 0) + i] = binData[i];
        }
    };

    /**
     * Resets CPU state.
     * Clears memory and loads font set, clears stack, registers and pointers.
     * Resets state of keyboard, screen and audio.
     * @function reset
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.reset = function () {
        this.mem = new Uint8Array(MEM_SIZE);
        this.stack = new Array(STACK_SIZE);

        /**
         * Enables/disabled CPU quirks
         * @memberof chip8.CPU
         * @instance
         * @type {object}
         * @name chip8.CPU#quirks
         * @property {boolean} shift - If enabled, VX is shifted and VY remains unchanged (default: false)
         * @property {boolean} loadStore - If enabled, I is not incremented during load/store (default: false)
         */
        this.quirks = {
            shift: false,
            loadStore: false
        };

        this.V = new Uint8Array(16);
        this.i = 0;
        this.pc = PROGRAM_START_ADDR;
        this.dt = 0;
        this.st = 0;

        this.sp = 0;
        this.repaint = false;

        this.halted = false;
        this.screen.clear();
        this.screen.repaint();
        this.keyboard.reset();

        this.loadFontSet();
    };

    /**
     * Runs CPU.
     * @function run
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.run = function () {
        var self = this;

        function tick() {
            self.timeoutHandle = setTimeout(function() {
                self.rafHandle = window.requestAnimationFrame(tick);
                self.cycle();
            }, 1000 / SPEED);
        }

        tick();
    };

    /**
     * Stops CPU.
     * @function stop
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.stop = function () {
        if (this.timeoutHandle) {
            window.clearTimeout(this.timeoutHandle);
        }
        if (this.rafHandle) {
            window.cancelAnimationFrame(this.rafHandle);
        }
    };

    /**
     * Halts CPU.
     * Halted CPU differs from stopped CPU in a way that halted CPU performs idle loop,
     * and stopped CPU does nothing.
     * @function halt
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.halt = function () {
        this.halted = true;
    };

    /**
     * Resumes CPU from halted state.
     * @function resume
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.resume = function () {
        this.halted = false;
    };

    /**
     * Performs one CPU cycle.
     * @function cycle
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.cycle = function () {
        if (this.halted) {
            return;
        }

        for (var i = 0; i < STEPS_PER_CYCLE; i++) {
            this.step();
        }

        this.updateTimers();
        if (this.st > 0) {
            this.audio.play();
        } else {
            this.audio.stop();
        }
    };

    /**
     * Updates CPU timers.
     * @function updateTimers
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.updateTimers = function () {
        if (this.dt > 0) {
            this.dt--;
        }

        if (this.st > 0) {
            this.st--;
        }
    };

    /**
     * Performs one CPU step.
     * One step means one fetch-decode-execute cycle.
     * @function step
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.step = function () {
        this.execute(this.decode(this.fetch()));

        if (this.repaint) {
            this.screen.repaint();
            this.repaint = false;
        }
    };

    /**
     * Fetches next instruction code
     * @returns {number} instruction code
     * @function fetch
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.fetch = function () {
        return this.mem[this.pc] << 8 | this.mem[this.pc + 1];
    };

    /**
     * Decodes given instruction code and returns function which performs that instruction.
     * Throws exception if can't decode code.
     * @param {number} opCode - instruction code
     * @returns {function|undefined} function performing operation
     * @function decode
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.decode = function (opCode) {
        var x   = (opCode & 0x0F00) >> 8,
            y   = (opCode & 0x00F0) >> 4,
            kk  = opCode & 0x00FF,
            nnn = opCode & 0x0FFF,
            n   = opCode & 0x000F;

        switch (opCode & 0xF000) {
            case 0x0000:
                switch (opCode) {
                    case 0x00E0: return CPU.IS.CLS(); // 0x00E0: CLS
                    case 0x00EE: return CPU.IS.RET(); // 0x00EE: RET
                    default:     return CPU.IS.SYS();     // 0x0nnn: SYS
                }
                break;

            case 0x1000:
                return CPU.IS.JP_nnn(nnn); // 0x1xxx: JP nnn

            case 0x2000:
                return CPU.IS.CALL_nnn(nnn); // 0x2nnn: CALL nnn

            case 0x3000:
                return CPU.IS.SE_Vx_kk(x, kk); // 0x3xkk: SE Vx, kk

            case 0x4000:
                return CPU.IS.SNE_Vx_kk(x, kk); // 0x4xkk: SNE Vx, kk

            case 0x5000:
                return CPU.IS.SE_Vx_Vy(x, y); // 0x5xy0: SE Vx, Vy

            case 0x6000:
                return CPU.IS.LD_Vx_kk(x, kk); // 0x6xkk: LD Vx, kk

            case 0x7000:
                return CPU.IS.ADD_Vx_kk(x, kk); // 0x7xkk: ADD Vx, kk

            case 0x8000:
                switch (opCode & 0x000F) {
                    case 0x0000: return CPU.IS.LD_Vx_Vy(x, y);   // 0x8xy0:  LD Vx, Vy
                    case 0x0001: return CPU.IS.OR_Vx_Vy(x, y);   // 0x8xy1:  OR Vx, Vy
                    case 0x0002: return CPU.IS.AND_Vx_Vy(x, y);  // 0x8xy2: AND Vx, Vy
                    case 0x0003: return CPU.IS.XOR_Vx_Vy(x, y);  // 0x8xy3: XOR Vx, Vy
                    case 0x0004: return CPU.IS.ADD_Vx_Vy(x, y);  // 0x8xy4: ADD Vx, Vy
                    case 0x0005: return CPU.IS.SUB_Vx_Vy(x, y);  // 0x8xy5: SUB Vx, Vy
                    case 0x0006: return CPU.IS.SHR_Vx_Vy(x, y);  // 0x8xy6: SHR Vx, Vy
                    case 0x0007: return CPU.IS.SUBN_Vx_Vy(x, y); // 0x8xy5: SUB Vx, Vy
                    case 0x000E: return CPU.IS.SHL_Vx_Vy(x,y);   // 0x8xyE: SHL Vx, Vy
                }
                break;

            case 0x9000:
                return CPU.IS.SNE_Vx_Vy(x, y); // 0x9xy0: SNE Vx, Vy

            case 0xA000:
                return CPU.IS.LD_I_nnn(nnn); // 0xAnnn: LD I, nnn

            case 0xB000:
                return CPU.IS.JP_V0_nnn(nnn); // 0xBnnn: JP V0, nnn

            case 0xC000:
                return CPU.IS.RND_Vx_kk(x, kk); // 0xCxkk: RND Vx, kk

            case 0xD000:
                return CPU.IS.DRW_Vx_Vy_n(x, y, n); // 0xDxyn: DRW Vx, Vy, n

            case 0xE000:
                switch (opCode & 0x00FF) {
                    case 0x009E: return CPU.IS.SKP_Vx(x);  // 0xEx9E: SKP Vx
                    case 0x00A1: return CPU.IS.SKNP_Vx(x); // 0xExA1: SKNP Vx
                }
                break;

            case 0xF000:
                switch (opCode & 0x00FF) {
                    case 0x0007: return CPU.IS.LD_Vx_DT(x); // 0xFx07: LD Vx, DT
                    case 0x000A: return CPU.IS.LD_Vx_K(x);  // 0xFx0A: LD Vx, k
                    case 0x0015: return CPU.IS.LD_DT_Vx(x); // 0xFx15: LD DT, Vx
                    case 0x0018: return CPU.IS.LD_ST_Vx(x); // 0xFx18: LD ST, Vx
                    case 0x001E: return CPU.IS.ADD_I_Vx(x); // 0xFx1E: ADD I, Vx
                    case 0x0029: return CPU.IS.LD_F_Vx(x);  // 0xFx29: LD F, Vx
                    case 0x0033: return CPU.IS.LD_B_Vx(x);  // 0xFx33: LD B, Vx
                    case 0x0055: return CPU.IS.LD_I_Vx(x);  // 0xFx55: LD [I], Vx
                    case 0x0065: return CPU.IS.LD_Vx_I(x);  // 0xFx65: LD Vx, [I]
                }
                break;
        }

        this.stop();
        throw 'Unknown opcode: 0x' + opCode.toString(16);
    };

    /**
     * Executes given operation.
     * @param {function} op - operation to execute.
     * @function execute
     * @memberof chip8.CPU
     * @instance
     */
    CPU.prototype.execute = function (op) {
        op(this);
    };

    chip8.CPU = CPU;

})(chip8 || (chip8 = {}));