Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- switch (hh) {
- case "0":
- ll = opcode.charAt(3);
- switch (ll) {
- case "0":
- draw.fillStyle = "#000";
- draw.fillRect(0, 0, display.width, display.height);
- break;
- case "E":
- this.pc = this.callstack[this.callpointer];
- this.callpointer--;
- break;
- default:
- throw {
- name: "opcodeDecodeError",
- message: "unknown error decoding opcode " + opcode
- };
- }
- break;
- case "1":
- addr = parseInt(opcode.substring(1, 4), 16);
- this.pc = addr;
- jumpflag = true;
- break;
- case "2":
- addr = parseInt(opcode.substring(1, 4), 16);
- this.callpointer++;
- this.callstack[this.callpointer] = this.pc;
- this.pc = addr;
- jumpflag = true;
- break;
- case "3":
- vx = parseInt(opcode.charAt(1), 16);
- l = parseInt(opcode.substring(2, 4), 16);
- if (this.register[vx] == l) {
- this.pc += 2;
- }
- break;
- case "4":
- vx = parseInt(opcode.charAt(1), 16);
- l = parseInt(opcode.substring(2, 4), 16);
- if (this.register[vx] != l) {
- this.pc += 2;
- }
- break;
- case "5":
- vx = parseInt(opcode.charAt(1), 16);
- vy = parseInt(opcode.charAt(2), 16);
- if (this.register[vx] == this.register[vy]) {
- this.pc += 2;
- }
- break;
- case "6":
- vx = parseInt(opcode.charAt(1), 16);
- l = parseInt(opcode.substring(2, 4), 16);
- this.register[vx] = l;
- break;
- case "7":
- vx = parseInt(opcode.charAt(1), 16);
- l = parseInt(opcode.substring(2, 4), 16);
- this.register[vx] += l;
- break;
- case "8":
- ll = opcode.charAt(3);
- vx = parseInt(opcode.charAt(1), 16);
- vy = parseInt(opcode.charAt(2), 16);
- switch (ll) {
- case "0":
- this.register[vx] = this.register[vy];
- break;
- case "1":
- this.register[vx] = this.register[vx] | this.register[vy];
- break;
- case "2":
- this.register[vx] = this.register[vx] & this.register[vy];
- break;
- case "3":
- this.register[vx] = this.register[vx] ^ this.register[vy];
- break;
- case "4":
- this.register[vx] = this.register[vx] + this.register[vy];
- break;
- case "5":
- this.register[vx] = this.register[vx] - this.register[vy];
- if (vx > vy) {
- this.register[15] = 1;
- } else {
- this.register[15] = 0;
- }
- break;
- case "6":
- this.register[15] = this.register[vy] & 0xFFFE;
- this.register[vx] = this.register[vy] >>> 1;
- break;
- case "7":
- this.register[vx] = this.register[vy] - this.register[vx];
- if (vy > vx) {
- this.register[15] = 1;
- } else {
- this.register[15] = 0;
- }
- break;
- case "E":
- this.register[15] = (this.register[vy] >> 7) & 0xFFFE;
- this.register[vx] = this.register[vy] << 1;
- break;
- default:
- throw {
- name: "opcodeDecodeError",
- message: "unknown error decoding opcode " + opcode
- };
- }
- break;
- case "9":
- vx = parseInt(opcode.charAt(1), 16);
- vy = parseInt(opcode.charAt(2), 16);
- if (this.register[vx] != this.register[vy]) {
- this.pc += 2;
- }
- break;
- case "A":
- addr = parseInt(opcode.substring(1, 4), 16);
- this.i = addr;
- break;
- case "B":
- addr = parseInt(opcode.substring(1, 4), 16);
- this.pc = addr + this.register[0];
- jumpflag = true;
- break;
- case "C":
- vx = parseInt(opcode.charAt(1), 16);
- l = parseInt(opcode.substring(2, 4), 16);
- this.register[vx] = getRandomInt(0, 256) & l;
- break;
- case "D":
- vx = parseInt(opcode.charAt(1), 16);
- vy = parseInt(opcode.charAt(2), 16);
- ll = parseInt(opcode.charAt(3), 16);
- image = draw.getImageData(0, 0, display.width, display.height);
- for (i = 0; i < ll; i++) {
- linebyte = this.memory[this.i + i];
- for (j = 0; j < 8; j++) {
- bit = linebyte & (0x80 >>> j);
- cx = this.register[vx] * this.displayscale;
- cy = this.register[vy] * this.displayscale;
- data = imageDataReadPixel(cx, cy, image);
- if (bit){
- if (JSON.stringify(data) == JSON.stringify([0, 0, 0, 255])) {
- draw.fillStyle = "#FFF";
- } else if (JSON.stringify(data) == JSON.stringify([255, 255, 255, 255])) {
- drawColor = 0;
- draw.fillStyle = "#000";
- } else {
- throw {
- name: "imageDataError",
- message: "encountered error proccessing image data"
- };
- }
- draw.fillRect(cx + j * this.displayscale, cy + i * this.displayscale, this.displayscale, this.displayscale);
- }
- }
- }
- break;
- case "E":
- vx = parseInt(opcode.charAt(1), 16);
- l = parseInt(opcode.substring(2, 4), 16);
- switch (l) {
- case "9E":
- //EX9E
- //skip next instruction if key value in VX is pressed
- break;
- case "A1":
- //EXA1
- //skip next instruction if key value in VX is not pressed
- break;
- default:
- throw {
- name: "opcodeDecodeError",
- message: "unknown error decoding opcode " + opcode
- };
- }
- break;
- case "F":
- vx = parseInt(opcode.charAt(1), 16);
- l = opcode.substring(2, 4);
- switch (l) {
- case "07":
- this.register[vx] = this.delaytimer;
- break;
- case "0A":
- if (keypress) {
- this.register[vx] = keypress;
- } else {
- jumpflag = true;
- }
- break;
- case "15":
- this.delaytimer = this.register[vx];
- break;
- case "18":
- this.soundtimer = this.register[vx];
- break;
- case "1E":
- this.i += this.register[vx];
- break;
- case "29":
- if ((this.register[vx] > 15) | (this.register[vx]) < 0) {
- throw {
- name: "characterFetchError",
- message: "Character code out of range"
- };
- }
- this.i = 5 * this.register[vx];
- break;
- case "33":
- num = this.register[vx].toString(10);
- if (num.length < 3) {
- num = "0".repeat(3 - num.length) + num;
- }
- for (i = 0; i < 3; i++) {
- this.memory[this.i + i] = parseInt(num.charAt(i), 10);
- }
- break;
- case "55":
- for (i = 0; i <= vx; i++) {
- this.memory[this.i + i] = this.register[i];
- }
- this.i += i;
- break;
- case "65":
- for (i = 0; i <= vx; i++) {
- this.register[i] = this.memory[this.i + i];
- }
- this.i += i;
- break;
- default:
- throw {
- name: "opcodeDecodeError",
- message: "unknown error decoding opcode " + opcode
- };
- }
- break;
- default:
- throw {
- name: "opcodeDecodeError",
- message: "unknown error decoding opcode " + opcode
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement