News for hackers. Stuff that matters.
Re:Project-Nested: An NES Emulator Running on the SNES by derefr on Sunday July 11, 2021 @01:58PM (#27803093)
You're talking about this stuff: https://andrewkelley.me/post/jamulator.html#dirty-assembly-t...

Personally, I disagree with the author of that article's conclusion here, that the [only] "solution is to embed an interpreter runtime in the generated binary." You can statically recompile this code!

You "just" need to run a concolic interpreter over the code, generating out new static instruction streams for each possible phi-node (branch) state, and then coalescing them back together where you find you've generated the same instruction stream. You can early-terminate these spec-exec paths when you find yourself back at an instruction postdominated by entirely static code [within the body of the unrolled runtime coroutine sequence].

Might sound like an unbounded exponential check, but in practice 1. merging back together will usually happen within, at most, five instructions, because these are almost always last-minute bit-packing-hacks to get code to fit within a given constrained ROM image size; and 2. there are always only two concolically-discoverable valid interpretations of a given instruction stream (if doing mid-instruction jumps), and only one concolically-discoverable valid interpretation of data-as-code / RAM-as-code. (NES games don't assemble and JIT arbitrary code streams. They don't have the work RAM for it. They're at most unpacking existing, non-arbitrary code streams; or taking something that is primarily a code stream, and then reusing it as data elsewhere, maybe a PRNG seed, or a 1-bit noise texture.)

A large part of my job lately is decompiling and "reconstructing meaning" for object code compiled for lousy underspecified abstract-machine architectures. There are a lot of modern compiler-theoretic tools and tricks that can make short work of "statically" recompiling any but the hardest of hard cases. They're sadly virtually unknown outside of academia, though (probably because they're based on various graphical transformations of the code proposed after the Dragon Book was published, which is the point in history where most people in industry's knowledge of compilers seems to stop.)

Re:Project-Nested: An NES Emulator Running on the SNES by wk_end on Sunday July 11, 2021 @02:35PM (#27803445)
Well, I know a good deal about old video game systems but you clearly know more about (re/de)compilers than I do, so although still skeptical I’ll defer to your expertise, while vaguely gesturing towards “lots of people would love the performance gains of static recompilation in our emulators, if it’s feasible please advance the state of the art” :)

Pretty sure you’re still going to be stymied by things like cycle-counted code to line up I/O writes with the state of the hardware, though. I don’t suppose there’s tricks for that.

Re:Project-Nested: An NES Emulator Running on the SNES by anyfoo on Sunday July 11, 2021 @03:15PM (#27803806)
Cycle-accurate transpiling, now that sounds like a fun project. I think it would "only" have to be cycle-accurate between specific events, e.g. access to specific registers. (No idea how feasible this whole thing could be.)
Re:Project-Nested: An NES Emulator Running on the SNES by NobodyNada on Sunday July 11, 2021 @10:04PM (#27806180)
> I think it would "only" have to be cycle-accurate between specific events, e.g. access to specific registers.

That’s correct — but one of those events is external interrupts (from the graphics, audio, or cartridge hardware), which complicates things a bit since your “checkpoints” can be anywhere in the code rather than just at a specific set of register accesses.

You could work around this by tracking cycles per-basic-block while running transpiled code, and falling back to cycle-accurate software emulation when you get close to a hardware interrupt.

Re:Project-Nested: An NES Emulator Running on the SNES by monocasa on Sunday July 11, 2021 @03:34PM (#27803950)
I remain skeptical (but would love to be proven wrong!).

Most of the point of recompiling (JIT or AOT) is to amortize instruction decode and dispatch costs versus an interpreter. If you have to do the cycle accurate bookkeeping anyway, you're most of the way to the overhead of an interpreter again. There'll be some savings around partial instruction streams that don't have effects outside the CPU core that can just be run as a burst, but I'm not sure it'll be a game changing amount.

Re:Project-Nested: An NES Emulator Running on the SNES by bonzini on Sunday July 11, 2021 @03:11PM (#27803766)
I expect that some games would put subroutines once in the zero page and then self-modify addresses in there.

Instructions are only written once but could potentially be written by any store with zero page indirect[1] or indexed addressing.

[1] that is ($AA,X) or ($AA),Y

Re:Project-Nested: An NES Emulator Running on the SNES by stormbrew on Sunday July 11, 2021 @05:17PM (#27804583)
The zero page is very small, I don't think this was done that much. You'd constantly be writing large gobs of stuff into it and that'd be very slow.

That said, the stack was used as a fast indirect jump address location pretty frequently, as described here, essentially abusing the fact that PHS and RTS are 1 byte instructions: http://www.6502.org/tutorials/6502opcodes.html#RTS

And really since basically all PC-modifying instructions operate on either relative (b*), absolute-indirect (jmp), absolute (jmp, jsr), or stack-indirect (rts, rti) there's no special benefit you get from having code in the zero page anyways afaik.

Re:Project-Nested: An NES Emulator Running on the SNES by a1369209993 on Sunday July 11, 2021 @06:34PM (#27805008)
> there's no special benefit you get from having code in the zero page anyways afaik.

You save one cycle off each instruction that modifies another instruction, which can important for inner loops. See eg http://www.linusakesson.net/programming/gcr-decoding/index.p..., although that's a disk-induced realtime requirement, rather than hblank/beam-induced.

Re:Project-Nested: An NES Emulator Running on the SNES by stormbrew on Monday July 12, 2021 @12:02AM (#27806917)
ah ok yeah that makes sense. If you're directly modifying the instructions then yeah there's a big benefit to the zero page. I think I misread the GP post and missed the self-modifying context somehow.

Thanks for clarifying :)

Re:Project-Nested: An NES Emulator Running on the SNES by bonzini on Monday July 12, 2021 @02:55AM (#27807798)
For an example, search for 0073 or CHRGET at https://www.pagetable.com/c64ref/c64disasm/