Skip to content

Instantly share code, notes, and snippets.

@robherley
Last active December 19, 2022 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robherley/836369cbd8eb73a286d017626b8376c1 to your computer and use it in GitHub Desktop.
Save robherley/836369cbd8eb73a286d017626b8376c1 to your computer and use it in GitHub Desktop.
GB Instructions JSON to Go maps
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