#概要
jsdoでchip8やってみた。
ディスアセンブラ作ってみた。
#写真
#サンプルコード
function decode(instr) {
var x = (instr & 0x0F00) >> 8,
y = (instr & 0x00F0) >> 4,
kk = instr & 0x00FF,
nnn = instr & 0x0FFF,
n = instr & 0x000F;
switch (instr & 0xF000)
{
case 0x0000:
switch (instr)
{
case 0x00E0:
return "cls";
case 0x00EE:
return "ret";
case 0x0000:
return "nop";
default:
return "sys " + nnn;
}
break;
case 0x1000:
return "goto " + nnn;
case 0x2000:
return "call " + nnn;
case 0x3000:
return "eq v" + x + " " + kk;
case 0x4000:
return "neq v" + x + " " + kk;
case 0x5000:
return "eq v" + x + " v" + y;
case 0x6000:
return "load v" + x + " " + kk;
case 0x7000:
return "add v" + x + " " + kk;
case 0x8000:
switch (instr & 0x000F)
{
case 0x0000:
return "load v" + x + " v" + y;
case 0x0001:
return "or v" + x + " v" + y;
case 0x0002:
return "and v" + x + " v" + y;
case 0x0003:
return "xor v" + x + " v" + y;
case 0x0004:
return "add v" + x + " v" + y;
case 0x0005:
return "sub v" + x + " v" + y;
case 0x0006:
return "rshift v" + x + " v" + y;
case 0x0007:
return "nsub v" + x + " v" + y;
case 0x000E:
return "lshift v" + x + " v" + y;
}
break;
case 0x9000:
return "neq v" + x + " v" + y;
case 0xA000:
return "load I " + nnn;
case 0xB000:
return "goto v0 " + nnn;
case 0xC000:
return "rand v" + x + " " + kk;
case 0xD000:
return "disp v" + x + " v" + y + " " + n;
case 0xE000:
switch (instr & 0x00FF)
{
case 0x009E:
return "keq v" + x;
case 0x00A1:
return "kneq v" + x;
}
break;
case 0xF000:
switch (instr & 0x00FF)
{
case 0x0007:
return "load v" + x + " DT";
case 0x000A:
return "load v" + x + " K";
case 0x0015:
return "load DT v" + x;
case 0x0018:
return "load ST v" + x;
case 0x001E:
return "add I v" + x;
case 0x0029:
return "load F v" + x;
case 0x0033:
return "load B v" + x;
case 0x0055:
return "load I v" + x;
case 0x0065:
return "load v" + x + " I";
}
break;
}
};
var j = 0x200;
var out = document.getElementById('out');
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://jsrun.it/assets/y/6/t/z/y6tzO', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
var dv = new DataView(this.response);
for (var i = 0, len = dv.byteLength; i < len; i += 2)
{
var m0 = dv.getUint8(i * Uint8Array.BYTES_PER_ELEMENT, false);
var m1 = dv.getUint8((i + 1) * Uint8Array.BYTES_PER_ELEMENT, false);
var opcode = m0 << 8 | m1;
var asm = decode(opcode);
out.value += (j + i) + ": " + asm + "\n";
}
}
xhr.send();
#成果物
以上。