HN.zip

Show HN: NES Contra (1987) port (ASM –> C and then C –> WebAssembly)

Based on vermiceli's annotated disassembly of Contra: https://github.com/vermiceli/nes-contra-us. Thanks for the great work.I ported it in two steps. First to C: the game logic is translated routine by routine from the 6502 assembly, and checked frame by frame against the original in the Mesen emulator. Then the C code to WebAssembly so it runs in the browser.The cherry on top: online multiplayer, so two players can play co-op over the internet.Source: https://github.com/epoyraz/nes-contra-us

83 points by tevlon - 57 comments

57 Comments

throwaway2037 [3 hidden]5 mins ago
Wow, the source 6502 assembly with comments is incredible: https://github.com/vermiceli/nes-contra-us/blob/main/src/ban...

The whole thing (all banks) is more than 10,000 lines of assembly code. When I wrote assembly code for a university computer science course, it was hard to manage even 200 lines. I cannot imagine writing and managing 10,000 lines!

zahlman [3 hidden]5 mins ago
As much as I wouldn't want to write that much assembly, you make it less daunting the same way that you would with any other programming language. There are simple idioms for loops that you can macro-ize; similarly for function setup and teardown; more importantly, the concept of a function still exists at that level.
raldi [3 hidden]5 mins ago
But the Japanese Contra was so much better! https://www.youtube.com/shorts/CiXzONyCDW0
davesque [3 hidden]5 mins ago
Unfortunately, I'm a bit concerned that cool labor of love hacker projects like this will become much less common now that Anthropic (and possibly soon others) is moving to restrict this kind of usage. Oh well, so much for cool fun stuff.
Panzerschrek [3 hidden]5 mins ago
Recently I did something similar (or even more labor-intensive). I recreated an existing game from scratch by manually rewriting its code without even thinking about LLM/"AI" tools usage. (see my submissions).

But I agree, that using an "AI" tool may be a serious temptation and thus way less new porting projects will be done with such care.

Buttons840 [3 hidden]5 mins ago
A silver lining is that porting games will serve as a catalyst to help the self-hosted LLM ecosystem grow.
em3rgent0rdr [3 hidden]5 mins ago
The first project like this (of using AI to port old software from assembly) was interesting and novel. But every subsequent post has me wondering what new insight or lessons we have learned and if doing more of these is worth the energy burn.
EmX_3000 [3 hidden]5 mins ago
It's not really that cool to get a bot to do this work for you, anybody can ask for that assuming they have the tokens to burn.
davesque [3 hidden]5 mins ago
I could write a well crafted reply, with a convincing argument, but why bother? You're just trolling. The whole anti AI coding thing is such a meme. I suppose you'd like to spend all the time it would take to reverse engineer a classic NES game by hand? Go familiarize yourself with 6502 assembly and everything else you'd need to know?
tadfisher [3 hidden]5 mins ago
I would rather read a write up of someone doing just that. This exact thing has been done many times now and no one is really learning or demonstrating anything new. It is cool, insofar as AI tools are capable of reverse-engineering, mechanical translation, and automatic verification, but it is no longer _novel_ and borders on spam.
pjmlp [3 hidden]5 mins ago
Yes, that is exactly the fun, not contracting someone else to do it.
Buttons840 [3 hidden]5 mins ago
You are free to do so. Have fun!
pjmlp [3 hidden]5 mins ago
I certainly am, only pointing out the comments vibe.

When someone commissions a painting, or a sculpture, they might cut the ribbon that holds the curtain on the opening day, that is all.

jkahrs595 [3 hidden]5 mins ago
“It’s not really that cool” they said on hacker news.
gnabgib [3 hidden]5 mins ago
Long standing account freaks out about latest GenAI/green account making vapid statement? Where are you going here?
jkahrs595 [3 hidden]5 mins ago
I’m going to sleep actually. Goodnight
userbinator [3 hidden]5 mins ago
Local models will never be restricted, and I suspect the Chinese ones wouldn't try to do so either.
pansa2 [3 hidden]5 mins ago
> Anthropic is moving to restrict this kind of usage

I hadn’t heard about that - any details?

gnabgib [3 hidden]5 mins ago
A short search away Anthropic takes steps to block Claude from reverse-engineering video games (106 points, 71 comments) https://news.ycombinator.com/item?id=50320255
shinryudbz [3 hidden]5 mins ago
Is the audio not supposed to work? I was hoping to hear the legendary tracks!
msephton [3 hidden]5 mins ago
A couple months ago I wrote about this kind of transcompilation hop-skip-jump: https://news.ycombinator.com/item?id=49643284

I used it to get games written in Lua running on SNES and in my own language/engine running on Dreamcast, Wii, Web and more.

gwbas1c [3 hidden]5 mins ago
One of the things that I'm curious about: How easy / hard is it to use the same NES game with higher resolution / color graphics, and sample-based sound?

Are there any emulators out there that let you swap in higher resolution assets so a game can be freshened without a complete port?

vunderba [3 hidden]5 mins ago
You might like 3dSen, which came out several years ago. It does some pretty crazy stuff, converting NES games into essentially 3D voxel environments.

https://store.steampowered.com/app/1147940/3dSen_PC

tevlon [3 hidden]5 mins ago
whoa, that's cool. I didn't know about this. Thanks for sharing!
throwaway2037 [3 hidden]5 mins ago
How would that work for hit detection algorithms in the code?
unleaded [3 hidden]5 mins ago
Mesen has texture packs. Don't know about sound, you'd have to reverse each game's sound driver first. https://vgmpf.com/Wiki/index.php?title=Famicom/NES_Sound_Dri...
BSPtree [3 hidden]5 mins ago
I would not think so, at least not in a meaningful way. That would require to edit the cartridge data thus the ROM, as all sprite data was contained within the cartridge. Sample based sound is impossible natively, as NES and older game consoles don't work with digital data but synthesizers. You could perhaps synthesize the music to WAV files and then enchance it. But then again, NES and thus its emulators have no way of playing digitized audio!
cowlevel [3 hidden]5 mins ago
The NES in particular is probably not too difficult to upscale, because the graphics data is stored on its own ROM chip (sometimes RAM) in a well structured layout. SMB1 just uses a ROM chip containing many tile graphics which the CPU code addresses by tile index. It would be really easy for an emulator to use the same tile indices to address any arbitrary set of tiles loaded from a PNG. Some games use RAM, which makes it harder as you'd have to index the PNG by hash or something.
kevin_thibedeau [3 hidden]5 mins ago
> Sample based sound is impossible natively, as NES and older game consoles don't work with digital data

The NES supports PCM audio. It was notably implemented wrong in the title screen of Double Dribble and fixed with a patch that produces clearer audio after bit reversal into the correct encoding for the hardware:

https://www.youtube.com/watch?v=Jli0RGpuZDg

pantelisk [3 hidden]5 mins ago
Compute motion vectors, faux depth, and add DLLS5 to it maybe? Could be pretty cool, or pretty horrific. Only one way to find out :)
kekebo [3 hidden]5 mins ago
Idk about swapping assets but zsnes used to have a range of video filters that could improve the experience on a modern screen quite a bit.

Edit: For SNES, not NES, my bad.

gwbas1c [3 hidden]5 mins ago
Yeah, its upscaler is nicer than the Switch and the mini SNES.
sfblah [3 hidden]5 mins ago
No sound and the a and b buttons should be the other direction (a is to the right of b) on the keyboard.
egonschiele [3 hidden]5 mins ago
Contra is my favorite game of all time so this is a real treat, and the co-op idea is fantastic. Not to be greedy, but how hard would it be to add music? :)
BSPtree [3 hidden]5 mins ago
Cool, how did you build the multiplayer mode? And how much AI was used?
esikich [3 hidden]5 mins ago
100% AI I'm sure. I played around with this kind of thing a month or so ago and Codex had no problems mashing games up from just the ROMs. In about 30 minutes I had the contra guys running through SMB 1 shooting enemies.
waltbosz [3 hidden]5 mins ago
Can you elaborate on the process? Do you just provide it with the ROMs and it is able to pull apart the relevant sprites and code pieces, and recombine into a new ROM?
esikich [3 hidden]5 mins ago
Yep. Fire up a new project, give it the two ROMs and the emulator to use and tell it what you what. I had to guide it a little bit about the sprite size and collison logic, but it was extremely painless.
tevlon [3 hidden]5 mins ago
For the Multiplayer part: It's deterministic lockstep. Both browsers run the same C core (compiled to WebAssembly), and the only thing sent over the network is each player's button bitmask per frame. Local input is delayed by 4 frames (~67 ms), which hides normal internet latency; if the other player's input for a frame hasn't arrived yet, the game waits for it. Every 2 seconds both sides compare a hash of the game RAM to catch desyncs. Transport is a WebRTC data channel via PeerJS (its free cloud server handles matchmaking with a room code), with Google's STUN server for NAT traversal and Metered's free-tier TURN relay as a fallback when a direct connection isn't possible.

For AI usage: It was all done by Claude/Codex, but it wasn't that straight forward. First: i started in April and the models weren't good enough and would make errors. You could see those immediately in glitches. Then, i spent some time in building a harness and collect telemetry data in Mesen. Once, i had that: i just played the game in the emulator and asked Claude Opus 5.5 to use this as verification. The rest was smooth.

Emjayen [3 hidden]5 mins ago
[flagged]
esikich [3 hidden]5 mins ago
I'm so glad that things being hard has never been connected to my self worth. Maybe someday you'll be able to enjoy just playing with technology rather than using it to prove how verysmart you are. Work smarter not harder.
Emjayen [3 hidden]5 mins ago
It's a cute rationalization, albeit not particularly novel unsurprisingly.
pmarreck [3 hidden]5 mins ago
Where's the sound? You can likely ask AI to emulate the sound hardware with WASM!
Dwedit [3 hidden]5 mins ago
All that work and then somehow transposing the A and B keys at the very end.
erictheguy [3 hidden]5 mins ago
Nostalgic!! Took me a few tries to beat the first level haha
pxx [3 hidden]5 mins ago
No controller support?
gosen [3 hidden]5 mins ago
thanks for bringing back my childhood memories!
einpoklum [3 hidden]5 mins ago
Most of the keys aren't working for me in Firefox though :-(
tevlon [3 hidden]5 mins ago
i only testing in chrome. Will look into it!
jasonlotito [3 hidden]5 mins ago
Whenever I see a game that uses the arrow keys and doesn't offer WASD as well, I can't help but see a game written by a non-gamer. I consider these fundamentals, and a shibboleth of whether I should care.
lightedman [3 hidden]5 mins ago
Literally 95% of 2D games on a PC were controlled via arrow keys. Not many allowed rebinding. Even in emulators, until later on.

You sure you're a gamer? I've been at it since about 1986.

pjmlp [3 hidden]5 mins ago
Very true, WASD only became a thing with FPS games.
anonymous908213 [3 hidden]5 mins ago
While I'm not defending a slop port, I have to question your gamer credentials. Maybe by gamer you mean a Call of Duty player? WASD is traditional for FPS/TPS games that expect you to have one hand on the mouse. Most 2D games, and every emulator I've ever used, have never offered WASD.
literallyanythi [3 hidden]5 mins ago
correct me if i'm wrong, gamer credential questioner, but WASD was immortalized by the id Software "win Carmack's ferrari" tournament. Before then EDSF was also popular because it gave you more keys on the left side for hotkeys.

I do think you're objectively correct in your post, but I think what the grandparent meant was that "The author isn't actually interested in playing Contra on his website" which is why QoL stuff like that wasn't added and it "smells" like it should be there if you cared about playing on the thing you actually shared to HN. I know the first thing I would do would be to add wasd + mouse support to Contra, since the control scheme is the reason I don't try to play it. But at the same time I have never been interested in games that deliberately limit your controls as a form of difficulty (feels like a Japanese gamedev thing, from Resident Evil to Dark Souls)

justsomehnguy [3 hidden]5 mins ago
NB: WASD won because most (like 99%) of the keyboards did had (and I can bet cheap shit still have) some dead keys aka 'key ghosting' on ESDF.
em3rgent0rdr [3 hidden]5 mins ago
I think because the computer mouse is typically on the right side of the keyboard (because most people are right-handed), WASD has the advantage of allowing your mouse to be controlled by your right hand and you can keep your left hand in the normal position it is used for typing and not have to have your left hand move all the way to the arrow keys to control directions.
literallyanythi [3 hidden]5 mins ago
You awoke negative-nostalgia to membrane keyboards where I remember I couldn't hold Shift + W + A at the same time in Minecraft as a kid so you're definitely right about bad keyboards, but I remember dead keys on WASD as well. I wasn't there, but the largest consensus seems to be that Quake popularized it, plain and simple. I'm sure other factors like what you mention helped it become the dominant layout as well

edit: figured out why that nostalgia happened. I was revisiting an old flash game I liked [0] and people were complaining to the dev about this exact key ghosting issue to him when it wasn't his fault but a fault of their keyboards, and in the Newgrounds description he links to a forum post [1] elaborating on the issue

[0] https://www.newgrounds.com/portal/view/547881

[1] https://board.flashkit.com/board/showthread.php?789015-wont-...