drcjt / CSharp-80

C# AOT compiler for Z80 based computers including the TRS-80 and ZX Spectrum
https://drcjt.github.io/CSharp-80/
GNU General Public License v3.0
51 stars 3 forks source link

How to compile? #281

Closed SaymanNsk closed 1 year ago

SaymanNsk commented 1 year ago

Hello. I`m download your compiler and compile ILCompiler to EXE file. how i can use it for compile cs files to get asm listing? I have some error: Error: Invalid DOS signature System.BadImageFormatException: Invalid DOS signature

command line: ILCompiler hello.cs -a CPM -o hello.com

drcjt commented 1 year ago

Hi @SaymanNsk thanks for your interest. It's been a while since I tried things on CPM. After a quick look to see if it still works or not I can see that some work is required on the System.Private.CoreLib project. I'll try and put in a PR soon to complete this to re-enable at least basic support for CPM - e.g. so HelloWorld will function.

In order to target CPM you need to define the TargetPlatform property when building - the easiest way to do this is to build from the command line using dotnet build e.g.

dotnet build -p:TargetPlatform=CPM CSharp-80.sln

The output will be a .hex file that you can then deploy to your CPM system. So for the hello world sample the hex file will be /Samples/Hello/bin/CPM/debug/net7.0/Hello.hex

Using the ilcompiler standalone requires a bit more work as you'll need to make sure you have a matching version of System.Private.CoreLib for the platform you are targetting. As such I'd advise against this for the time being.

It's worth noting the last time I tried running anything on CPM I was using a SC126 single board computer - https://smallcomputercentral.com/sc126-z180-motherboard-rc2014/ I did get some very simple samples working e.g. hello world, calculate fib, but don't expect too much from this.

drcjt commented 1 year ago

PR #282 gets CPM working again but with some missing functionality, e.g. no support for reading characters/lines. However, the samples not requiring input do work. Don't forget to use CPM's LOAD command to convert the HEX files to COM though.

Here is Hello World:

image

And here is ascii Mandelbrot:

image

drcjt commented 1 year ago

@SaymanNsk Just realised you may have been asking how to compile not specifically for CPM. The easiest way is to clone the repo and then load the solution in Visual Studio. This will default to targetting the TRS-80 platform. A full build will build all the samples and you'll be able to see the generated asm listing files in the debug folders of the samples, e.g. for Hello world this would be in the /Samples/Hello/bin/Trs80/Debug/net7.0/Hello.lst

SaymanNsk commented 1 year ago

Well, I initially thought that it was the compiler. I collected an EXE file and in the command line with it I try to compile a cs file for z80. But, I see that there is a different logic here and you need to use Visual Studio + this "module". Well, and most importantly, I am not interested in TRS80 or CPM, but in the assembly option for another system, for a Sprinter 2000 (z80 based) computer.

drcjt commented 1 year ago

I suspect you are misunderstanding the way the compiler works here - it won't compile cs files directly - you will need to use the regular roslyn compiler to compile the cs files into an exe or dll which contains MSIL. The ILCompiler takes these exe's/dll's and converts the MSIL in them into z80. The "module" is a bespoke core library - in effect a cut down version of the standard core library from the real dotnet. This is necessary as the ILCompiler is not sufficiently advanced to be able to deal with the standard core library from the real dotnet and also because it needs bespoke modifications to support the different target platforms here.

It looks like the Sprinter is a similar to the ZX Spectrum so shouldn't be too difficult to support - you'd have to create an appropriate PAL implementation for things like writing to the console and reading input.

drcjt commented 1 year ago

I've updated the readme to add some "how to use" instructions.