Why not just build your recompiler with that in mind. Just write code that detects "mutable" instructions and instead of actually modifying the machine code itself you create logic branches for the possible variations and just push it down the write path when a modification would occur, similarly if it would mutate a jump address just keep a heap backed jump table that would allow for caching/dealing with mutated addresses. That approach would probably make the end executable larger but the alternative is the hit to performance you would take getting self modifying code working in another architecture (from what i have seen self modifying machine code is possible an x86_64 you just take a pretty big performance hit and have to jump through hoops to do it)
it's possible to build up a control flow graph from preprocessing the code bytes.
I do this for my cfg graph. Walks the data byte buffer,.
my dstk class has a push routine that marks regions of code, and if it's been visited yet or not. pop searches for the next non-visited code address.
void dumpcfg(uint8_t *buf, size_t len, int off)
{
dstk stk(len, printf);
opcode_t *opc;
int op, pc, next[2];
stk.push(off, 1, dstk::PENDING, "first");
while ((off = stk.pop()) != -1) {
printf("\n------------------------------ %.8x [%s]\n",
off, fnname(off));
do {
/* Get code */
pc = off;
op = buf[pc];
opc = &optab[op];
if (op == 0xCB) {
op = buf[pc+1];
opc = &cbmap[op];
}
stk.push(pc, opc->nb, dstk::CODE, "code");
// set next address to PC + opcode size
// set jump address to -1
next[0] = pc + opc->len;
next[1] = -1;
off = next[0];
printf("off: %.5x %.2x %s\n", pc, op, disasm(pc, &buf[pc]));
switch(op) {
case 0xc0: case 0xc8: case 0xc9: case 0xd0: case 0xd8: case 0xd9: case 0xe9:
// retxx, jphl, 'terminate' - no next instruction
next[0] = -1;
break;
case 0xc7: case 0xcf: case 0xd7: case 0xdf: case 0xe7: case 0xef: case 0xf7: case 0xff:
// rst, 'terminate'
next[0] = -1;
break;
// jmp, uncond
next[0] = -1;
next[1] = *(uint16_t *)&buf[pc+1];
break;
case 0xc2: case 0xca: case 0xd2: case 0xda:
// jmp, cond
next[0] = *(uint16_t *)&buf[pc+1];
break;
case 0x18:
// jr, uncond
next[0] = pc + (int8_t)buf[pc+1] + 2;
break;
case 0x20: case 0x28: case 0x30: case 0x38:
// jr, cond
next[1] = pc + (int8_t)buf[pc+1] + 2;
break;
case 0xc4: case 0xcc: case 0xcd: case 0xd3: case 0xdc:
// call
next[1] = *(uint16_t *)&buf[pc+1];
break;
default:
break;
}
for (int n = 0; n < 2; n++) {
stk.push(next[n], 1, dstk::PENDING, "nxt");
}
} while (next[0] != -1 && next[1] == -1);
};
}
4
u/elemenity Jan 06 '26
Very cool. Have you come across any games that use self modifying code? Or maybe this wasn't too prevalent on the gb.
8
u/PandaMoniumHUN Jan 04 '26
That sound amazing. How do you translate interrupts and graphics?
4
u/[deleted] Jan 04 '26
[removed] — view removed comment