top | item 43673011

Making SNES ROMs using C#

92 points| nor0x | 11 months ago |reddit.com | reply

18 comments

order
[+] valiant55|11 months ago|reply
Neat, but with restrictions like no reference types and looking at the examples, they are far from idiomatic C# as to be almost indistinguishable from C I'm trying to figure out the motivation. Maybe it's "because you can" which is fine, but are there any advantages to this approach over using C directly?

EDIT: I overlooked the reddit post and comments and jumped straight into the repo, it looks like the creator is on an insane quest to get C# to run anywhere haha, I didn't realize the C tanspiler was by the same author.

[+] KallDrexx|11 months ago|reply
Author here, yes I am on an insane quest to get C# to run anywhere and SNES was just a random side tangent to that :D.

So the code in the game example is very much a "port directly from the example C code to make sure it's possible and it works". The next step was to start looking at where things can be made more idiomatic.

For example, C# supports static methods in interfaces now. This means instead of passing function pointers around for things you can create a class that implements an interface and call `InitObjects<T>()` instead of `InitObjects(&initFunction, &updateFunction, null)`, and I think I can do that in a no overhead way.

There are some other ideas I have for making things more idiomatic, but I really wanted to make sure I had a working ROM first before I started mangling the examples and trying to figure out if it's my transpiler or my logic that's breaking things.

There is a limit to how idiomatic I can make it though. Since the SNES has limited memory, any stack variable you create risks accidentally overwriting other memory. So it seems like you want to limit how many stack variables you use, which ends up meaning storing things in pointers and globals when using them.

There are some other slight advantages, like better enums, but yeah. At the end of the day this is a usecase of my transpiler allowing C# to be used on non-standard platforms.

[+] neonsunset|11 months ago|reply
> far from idiomatic C# as to be almost indistinguishable from C

C# could be much nicer to write than C. "Low-level" C# is still idiomatic, even if you use memory-safe constructs i.e. Span<T> and friends.

There is nothing wrong with writing C# that has structs, ref Ts and not a single class.

At work, I have ported a certain C dependency to C# to fix long-standing bugs, get rid of C build systems and portability problems and gain 2-10x performance improvement (by fixing suboptimal data structure use and switching to text scanning with SearchValues<char> which is heavily vectorized).

The end result is still idiomatic and arguably easier to read. But yes, it looks very different to the average enterprise codebase.

[+] ivraatiems|11 months ago|reply
This is firmly in "they asked whether they could but not whether they should" territory, and I love it for that.
[+] DobarDabar|11 months ago|reply
How about il2cpp? Then you could just use LLVM to compile it to just about anything
[+] AlienRobot|11 months ago|reply
>are there any advantages to this approach over using C directly?

Presumably the fact that you don't have to use C.

[+] CodeCube|11 months ago|reply
This is so friggin' cool!! I was wondering recently why Nintendo doesn't use it's already-existing channels of the on-Switch emulators, to let gamedevs make _new_ games for those platforms. That would be such a cool, nostalgic, and potentially lucrative market!
[+] mystified5016|11 months ago|reply
Why exactly is there no heap? I can understand stack size being limited, that's largely hardware dependent, but the heap?

Does the SNES not have enough RAM to be worth it or did you just not port the heap allocator? Or is there some other reason?

[+] tombert|11 months ago|reply
This is genuinely pretty cool. Without dynamic allocations and garbage collection, I'm not sure how much C# buys you, but it's still neat and I think further updates might end up making this much neater.
[+] KallDrexx|11 months ago|reply
Author here.

One thing it potentially buys you (besides things mentioned in other posts) is faster iteration cycles.

The C stubbed methods can potentially be populated with logic that sets up a fake SNES logic in it, and makes calls to a custom rendering and input handling (monogame calls for example).

This means you can test a bunch of your logic in a monogame app for easier logic debugging, then use the transpiler to test it on a real system.

[+] pjmlp|11 months ago|reply
Kind of safer C++, with less foot guns