llvm-mos / llvm-mos-sdk

SDK for developing with the llvm-mos compiler
https://www.llvm-mos.org
Other
266 stars 53 forks source link

Added initial Atari 2600 support (4K and 3E mappers) #237

Closed sehugg closed 9 months ago

sehugg commented 10 months ago

This is an initial attempt at an Atari 2600 target based on CC65 VCSLib: https://8bitworkshop.com/docs/posts/2023/vcslib-c-programming-for-atari-2600.html

It only supports 4K (non-banked) and 3E (2K fixed, 2K banked ROM+RAM) mappers for now.

One wrinkle with 3E is that the "write port" for RAM is $400 bytes above the "read port". So to use this area like a normal data segment, you'd have to declare a pair of variables, one read-only and one write-only and the compiler would have to make sure they are $400 bytes apart.

The 32 bytes of virtual registers is kind of a lot for the VCS but I assume the clobber attribute will let asm developers safely use this space. I'm guessing there won't be a soft stack.

The shared ROM area will be a scarce resource, especially if it is 2 KB or less. For banked mappers, it would be good to put all initialization code into rom0.

Display kernels will still probably be written in asm, except for very simple ones.

Since 2600 carts have no header, auto-detection of 3E by Stella et al is iffy. When most of the banks are empty, it triggers the wrong mapper: https://github.com/stella-emu/stella/blob/d6224a8a6e30b4b323cde86ade2f05f75dcdfbec/src/emucore/CartDetector.cxx#L294

asiekierka commented 10 months ago

Just to verify, what is the license on VCSLib-derived code?

sehugg commented 10 months ago

Just to verify, what is the license on VCSLib-derived code?

Much of it is from CC65, I see that other CC65-derived files have an Apache notice so I can add it if that's the right approach. I'm happy to license my original code under Apache 2.

mysterymath commented 10 months ago

Amazing to see a 2600 port, with a C demo! I remember 7 or 8 years ago having a conversation with a 2600 scener at a silicon valley VR meetup that I was working on a 6502 C compiler, and him mentioning that it was doubtful to be possible to get even simple things working on that platform with C. This was before llvm-mos by quite a bit, and codegen still isn't 100% what we'd need for this to be completely practical, but it's still neat to see something like this!

mysterymath commented 10 months ago

The 32 bytes of virtual registers is kind of a lot for the VCS but I assume the clobber attribute will let asm developers safely use this space. I'm guessing there won't be a soft stack.

Yep, the imaginary registers can be used arbitrarily in assembly, so long as the C calling convention is followed. That is, C code assumes anything it calls preserves __rc20 to __rc31. Assembly can rely on this for its callees, but it must ensure it for its callers. Similarly, assembly can arbitrarily clobber any other imaginary register, but it must assume any C routines it calls can do the same.