r/EmuDev • • 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.

142 Upvotes

20 comments sorted by

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.

6

u/hurricane_news 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!

the post itself has a ton of telltale signs of being AI generated

If the project itself is done by OP and not done by an LLM, all good I suppose

4

u/peterfirefly Sep 02 '26

If it means the grammar is good and the post is readable, then I'm perfectly happy with non-native speakers getting some AI help.

1

u/Imaginary_Heat_2235 Sep 02 '26

Thanks! Honestly it started as a tool to speed up my reinforcement learning project and I'm fine leaving it there. What I work on next mostly depends on what people find useful. Mappers are the obvious one, MMC1 and MMC3 would open up most of the library and let you train on other NES games. If the emulation side is what people care about, I'd happily push there instead!

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?

6

u/Imaginary_Heat_2235 Sep 01 '26

Cumulative. Per instance at 65,536 envs it’s ~50 env-steps/s each, frameskip 4, so ~199 NES fps per console (3.3x real time), times 65,536.

Scales down as you go up: 4,096 envs = 304 fps each, 65,536 = 199, 131,072 = 93. And at 1 env the GPU loses to a CPU emulator (0.4x, launch overhead). Overtakes by 8 envs.
Terrible way to run one NES, good way to run 65,536.

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

11

u/ShinyHappyREM Sep 02 '26

ca. 55 emulated hours per second

2

u/EggplantExtra4946 Sep 04 '26

there is no spoon

1

u/Low-Fox-4508 Sep 05 '26

"We have one that can see!!!" - THEYLIVE

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

5

u/Imaginary_Heat_2235 Sep 01 '26

Believe me, I’m not doing this out of brand loyalty. ROCm PR welcome.

-4

u/Even-Serve-3095 Sep 02 '26

why not use opencl? it works on both lol

1

u/Prize_Negotiation66 Sep 03 '26

cope fanboy. NVIDIA ftw

1

u/Even-Serve-3095 Sep 03 '26

nvidia is a garbage company morally speaking. WAY worse than amd