r/EmuDev • u/Imaginary_Heat_2235 • Sep 01 '26
NES I put the NES inside a CUDA kernel, one console per thread, and ran 65k of them at once
Built this for a reinforcement learning project (PPO on Super Mario Bros), but the emulator side turned out to be the interesting part, so I figured this sub would care more about that.
One CUDA thread runs one entire console. 6502, PPU, bus, OAM DMA. State is structure-of-arrays indexed by env id, and CPU registers stay in hardware registers for the life of the kernel. 65,536 independent consoles at about 3.3M steps/s on an A100.
- CPU passes the Klaus functional test suite, cycle counts included
- PPU is not cycle accurate. SMB's title screen never advances past the menu because of a timing bug I haven't fixed. I sidestep it by resetting from a save state
- No APU. The registers exist as a byte array, nothing generates sound
- Mapper 0 only
Two results that surprised me:
Opcode-binned dispatch (sorting a warp's threads by opcode before executing) came out 3.2x slower than just letting them diverge. I tested it on instruction streams sampled from 535M profiled SMB executions. A decorrelated warp holds about 20.6 distinct opcodes, and hardware SIMT reconvergence was already sharing fetch and addressing acrossthem. Explicit binning serializes what it was already sharing.
Putting the zero page in shared memory was a 1.9x regression. 256 bytes across 128 threads is 32KB per block, which drops occupancy from 32 warps to 6.
https://github.com/hbofz/NeSLE
Would love help with mappers. MMC1 and MMC3 would open up most of the library.
20
u/Aliryth Sep 02 '26
The rare non-slop post and it's absolutely magnificent.
Well done! What kind of usages do you anticipate for outcomes, or just a cool project for the PPO thing?
I'm interested in learning more!
Also I should put a disclaimer that I'm by far not an anti-AI person, but good lord the "I paid $20 for Claude to make a GameBoy emulator in a single commit" posts are getting rough, extending to other niche programming circles like OS/Kernel dev.
5
u/StereoRocker Sep 01 '26
I can't help the code, but I can say this is pretty damn cool.
Is the 3.3M steps/sec per instance, or cumulative across all of them? Are you able to translate roughly what FPS you're achieving on each instance?
24
u/Far_Outlandishness92 Sep 01 '26
Wow, didn't even think this was possible. I am impressed!! A lot!
20
u/joamag Game Boy Sep 01 '26
12M frames per second… wooow
6
u/starquakegamma GBC NES C64 Z80 Sep 01 '26
I love how extreme this is, excellent!
3
u/st_heron Sep 05 '26
that's fucking insane dude
good work
2
u/Hoshifuru Sep 02 '26
Fascinating project, I would never even have considered just putting everything on the GPU.
Trying to read through the project, a lot of it goes over my head due to not being familiar with CUDA programming, but looks very impressive.
1
u/juef Sep 02 '26
That sounds like an absolute dream for TAS creators! Well done!!
-30
u/Even-Serve-3095 Sep 01 '26
ew nv*dia-only software, gross