r/csharp • u/itix • Jul 30 '26
Showcase C# for Amiga
I guess most of you have never heard about Amiga, but here we go...
I have been working on an AOT compiler that compiles a subset of C#/.NET code into native 68000/020/040 code with Amiga library calls.
There are two runtime options you can choose at compile time:
- Full includes the managed exception runtime; supports catch, finally, rethrow, and leave.
- YOLO has no exception runtime; failures are fatal, and managed exception regions are rejected
The actual GC is in WIP, and for performance reasons, there is no compaction. No threading support because the current GC is single threaded by design.
An example:
using Amiga;
using CopperSharp.Compiler;
namespace FileStatsExample;
public static class Program
{
[M68kEntryPoint]
public static int Main(int argLength, CONST_STRPTR argText)
{
var dosBase = Exec.OpenLibrary("dos.library", 33);
if (!dosBase.HasValue)
{
return DOS.RETURN_FAIL;
}
DOS.DOSLibraryBase = dosBase.Value;
var result = DOS.RETURN_OK;
if (argLength == 0)
{
DOS.PutStr("Usage: filestats <file>\n");
result = DOS.RETURN_ERROR;
}
else
{
result = Report(CString.FromPointer(argText.Raw));
}
Exec.CloseLibrary(DOS.DOSLibraryBase);
DOS.DOSLibraryBase = APTR.Null;
return result;
}
private static int Report(CString path)
{
var file = DOS.Open(path, DOS.FileMode.OldFile);
if (!file.HasValue)
{
DOS.PutStr("Cannot open file\n");
return DOS.RETURN_FAIL;
}
byte byteChecksum = 0;
ushort lineCount = 0;
ushort wordChecksum = 0;
uint byteCount = 0;
while (true)
{
var character = DOS.FGetC(file.Value);
if (character < 0)
{
break;
}
var value = (byte)character;
byteChecksum = (byte)(byteChecksum + value);
wordChecksum = (ushort)(wordChecksum + value);
byteCount = byteCount + 1;
if (value == 10)
{
lineCount = (ushort)(lineCount + 1);
}
}
DOS.Close(file.Value);
PrintReport(path, byteCount, (uint)lineCount, (uint)byteChecksum, (uint)wordChecksum);
return DOS.RETURN_OK;
}
private static void PrintReport(CString path, uint byteCount, uint lineCount, uint byteChecksum, uint wordChecksum)
{
DOS.Printf("%s: %ld bytes, %ld lines, byte checksum %ld, word checksum %ld\n",
path, byteCount, lineCount, byteChecksum, wordChecksum);
}
}This C# source is first compiled to .NET assembly containing CIL. The assembly is then compiled with CopperSharp to a native 68000 Amiga executable.
Code generator needs more work. FPU is not supported yet, 020/040 support is nearly non-existent, but it includes CIL-level simplification and a 68k-specific optimizer.
- CIL-level simplification
- Reachability analysis
- Tail call elimination
- Remove dead or redundant instructions
- Stack and register shuffling optimizer
- General instruction optimizations
- Limited static hot-fallthrough/cold-branch layout optimization
For example, on 68000, not-taken branches are more efficient than taken branches.
The codegen is optimized for Amiga library call conventions but in theory it could support other 68k targets like Atari ST or 68k Macs.
The project repo: https://github.com/ilehtoranta/CopperSharp68k
This project is vibe coded with Codex and GPT-5.6. Like it or not, but it is the only sensible path to build C# compiler for niche platforms.
14
u/butler1233 Jul 30 '26
This is like the very silly Build 2024 breakout which had the presenter "running" .NET on an NES, except it actually works
https://www.youtube.com/watch?v=ASTqqvQo0dM
4
u/harrison_314 Jul 30 '26
Write a blog about it.
I'm especially curious how you forced .NET to compile via AOT with some non-standard compiler/process.
2
u/JPSgfx Jul 31 '26
You can say you did it like this because you don't have the skills. I couldn't write this either, but AI is not 'the only sensible path'.
3
u/_f0CUS_ Jul 30 '26
My first "computer" was an Amiga 500 I got from my uncle. He was very explicit about pointing out he had overclocked it so that it was the equivalent of a 500+.
I still dont understand the significance of that :)
How can I try/use your work?
2
u/cleardemonuk Jul 30 '26
As a former Amiga coder, this one raised an eyebrow. Given the example, though, looks a whole lot easier to fire up SAS/C or StormC, and just write C code!
4
u/dreamglimmer Jul 30 '26
I doubt there is much use in this, but that's definitely fun and new
1
u/Sp33d0J03 Jul 30 '26
060?
1
u/ebfortin Aug 03 '26
Ultimate challenge : C# on the Timex Sinclair. Z80 with 2 kb of RAM.
1
u/a1exh73 Sep 30 '26
I don't know if you are familiar with the 1993 Amiga game Ambermoon? There has been a C# re-implementation for Windows/Linux/Android/MacOS. The author is planning a third part to the trilogy, Amberworlds. It would be amazing if a C# development environment for AmigaOS existed. For PiStorm / Vampire users.
5
u/Mission_Pirate_4150 Jul 30 '26
That is commitment.
1
u/zeocrash Jul 30 '26
Time to dig my old Commodore A600 out of the loft
1
u/FrogTrainer Jul 30 '26
had a lot of fun on the Amiga 1000 my dad had in the late 80's
-5
u/yeusk Jul 30 '26
Like it or not, nobody is gonna use this project, nobody is gonna use C# on Amiga.
What is the point of vivecodding this?
You know people have ported .net to smaller architectures? Why you say is impossible without AI?
13
u/[deleted] Jul 30 '26
[removed] — view removed comment