Last active
December 19, 2022 02:16
-
-
Save robherley/836369cbd8eb73a286d017626b8376c1 to your computer and use it in GitHub Desktop.
GB Instructions JSON to Go maps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { unprefixed, cbprefixed } = require("./Opcodes.json"); | |
const opToCode = (mnemonic, op) => { | |
const name = op.name.toUpperCase(); | |
// for hex used in RST | |
if (mnemonic === "RST" && name.endsWith("H")) { | |
return `Byte(0x${name.slice(0, -1)})`; | |
} | |
// special case since we have conditional 'C' and register 'C' | |
if (mnemonic === "JP" && name === "C") { | |
return "Ca"; | |
} | |
if (!isNaN(parseInt(name))) { | |
return `Byte(${name})`; | |
} | |
return name; | |
}; | |
const generate = (instructions) => | |
Object.entries(instructions) | |
.map(([key, val]) => { | |
let operands = "nil"; | |
if (val.operands.length) { | |
const ops = | |
val.operands | |
.map((op) => { | |
let struct = `{Symbol: ${opToCode(val.mnemonic, op)}`; | |
if (op.increment) { | |
struct += ", Inc: true"; | |
} | |
if (op.decrement) { | |
struct += ", Dec: true"; | |
} | |
if (!op.immediate) { | |
struct += ", Deref: true"; | |
} | |
return struct + "}"; | |
}) | |
.join(",\n ") + ","; | |
operands = `[]Operand{ | |
${ops} | |
}`; | |
} | |
return ` | |
${key}: { | |
${val.mnemonic}, | |
${operands}, | |
[]int{${val.cycles.join(", ")}}, | |
},`; | |
}) | |
.join(""); | |
console.log(`var unprefixed = map[byte]Instruction{${generate(unprefixed)}\n}\n`); | |
console.log(`var cbprefixed = map[byte]Instruction{${generate(cbprefixed)}\n}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment