Cross Compiled C code for Amstrad CPC

So, the Atari 800 XL is running a 6502 CPU and previously I setup the C cross-compiler in Linux / Windows to write simple C code, compile the code, deploy to an emulator and to real hardware.

Now...as I may have mentioned, my first computer was an Amstrad CPC 464 back in xmas 1984 - I was a little too young to really appreciate coding in anything other than BASIC at the time but I made a good go of it and did some interesting database / filing system type things that emulated PRESTEL screens and made some very slow BASIC games and played a lot of TAPE games.  I never really had the opportunity to write any code in C or Assembler for that hardware.

Time zoomed along and I moved onto an Atari 520 STFM, then onto a 286 PC, then 386, then 486, then, well, y'know....


Around the 2007/2008/2009 period of time, I was having an external garage of the house I was living in at the time converted into an "office", but it had quite a lot of space, so I thought wouldn't it be good to have an "8-bit area" where I could have the old 8-bit Amstrad computer that started me off... eBay was my friend at the time and I was able to buy a CPC 464 and colour monitor for about £20.

I then went on a bit of an adventure (and spent FAR TOO MUCH money) on buying every variant, every hardware extension, ROM boards, every ROM, all the physical magazines from the era, all the physical discs (I had every AMSOFT game in all the packaging), I had a CPC 664 (the unicorn!), I had several variants of 464s, I also had numeroud CPC 6128s - the one's with the disc drives - I had English versions, I had German Schneider versions, I basically had EVERYTHING you could possibly get for the Amstrad computing range.

I also picked up lots of software along the way, office software and programming software as well as LOADS of physical books.  At the time, I was working away from home, a lot! and travelling around the world, doing my thing, so all this retro-goodness was basically gathering dust.  I joined the infamous CPCWiki forums, I shared ideas about hooking up arduino devices to the CPCs and doing amazing things (that amusingly nowdays have come true - but that is for a later post).  I even purchased a Tesseract (?) FPGA board from Japan(?) that allowed me to run an emulated/simulated version of the Amstrad on it that allowed me to use a modern LCD monitor and USB keyboard / mouse (this was ground-breaking back then!) - I even setup and got SymbOS working on it all - I believe that was some of the first YouTube videos that I uploaded (awful, but a record of time).

UPDATE: I found the CPCWik post from 2013 where I listed all the items for sale.  It's a sad read, but y'know, it is what it is.

Amstrad Collection:


464 - sticky up keys (in a genuine cardboard box too!)
464 - sticky up keys (v1?) - has a removable 40025 chip inside, so can be put back to a v1, currently shows as v3
2x464 - flat keys
464 - Schneider - grey keys

6128 - HXC SD internal (replaces drive A:)
6128 - 3" disc drive (faulty?used to work doesn't boot anymore)
6128 - Schneider - 3" disc drive- HXC SD external
3x6128 casing for spares/repairs

2x3" external disc drives for 464 + adaptor

664 - blue keys (darn it - used to work fine, some keys not working now - probably needs the new £25 membrane)
664 - Schneider - grey keys (minor crack in rear corner) - seems to start reading disc but then fails (just a new drive belt?) - all keys seem to work though - oddly this has edge connectors and not the other type like the Schneider 464 has - makes it more appealing as no need for different cables/connectors
664 - spares (in a genuine cardboard box too!)

Original Colour monitor
Colour monitor (for spares)
Green screen monitor

6128+ and monitor - game controller
6128+ - game controller (with a 464+ container box)

2xPPC640 + PSU (faulty?)

PC1640HD20 and monitor/keyboard/mouse/joystick/external 3.5" disc drive - (has Windows v1.3 installed and working on it!)
2xPC1640 - for spares/repair

T-REX Development board running Amstrad emulator - all setup and usable
(http://www.symbos.de/trex.htm)


6128+ Cartridges:
crazy cars II (with case)
batman (with case)
burnin' rubber
robocop2
plotting
pang
parados v1.1

6128+ adaptor (to connect 6128 peripherals)
2xFD1 connectors (for 464)

peripherals:
DMP 2000 printer
3xexternal tape recorders with cables
3.5" external disc drive in case with PSU and cable
3.5" external disc drive


MP3 TV adaptor and a separate radio tuner one

2xMultifaceII - one works fine, the other appears not to :-(

AMX Mouse - Amx art software & AMX mouse in box
3xAMX Mouse (2 white, 1 black)

dk'tronics 256K memory - large case
dk'tronics 256K memory (in a smaller 64K case)
dk'tronics 64K memory

RS232 Serial interface + PSU (with original box)
PACE RS232 Serial interface
SSA-1 and speakers (with original box)

CPC IDE/8255 adaptor - with 2 ROMS: HDOS
Bryce MegaRom RomBox thingy
ROMBO Rombox (ROMS: Locksmith, Rodos, Nirvana, Maxam, Utopia, +1 other?)
Maplin Rombox (ROMS: X-Ddos, Promerge, ProSpell, Nirvana, Protext, Programming Toolkit)
ROM : Disc Power (in box with manual)

JY1 joystick (with original box)
JY3 joystick (with original box)

a NC200 Notebook in leather case and a 128Kb RAM card.

3xAmstrad 'luggable' laptops ALT-386 (not tested, but at least one was working fine with a 40Mb HDD and Windows v3.1)

...and an Amstrad Pen Pad PDA 600 (a little sticky if I recall)


books:
24 books

15 of the big blue CPC books - the firmware, etc..etc... (I'll post a photo)

manuals:
operating cp/m 2.2
2xcpc 6128
cpc 664
5xcpc 464
2xDDI-1
3xAMX mouse
2xDMP 2000
DMP 2160

magazines:
computing with the amstrad - in 3 binders
WACCI - in 2 big binders
Amstrad - Cent Pour Cent
Amst*r
Amstrad Action - all editions and cover tapes

games:
nearly all the AMSOFT games
4-5 boxes various tape games in original boxes
50+ 3" discs (various content)
20+ 3" discs with boxes
loads of 5.25" for the PC1640

I did, however, scratch an itch that I had had for decades and that was to write a game for the Amstrad.  It was written in Assembler (it was a collaboration / assistance) from a guy in Australia, Lachlan (who is & still is active in the CPC community), he provided me with the basic structure of the ASM code to get going - I then coded, designed and made a game called "SCARAB" - it had an Egyptian feel to it - I'll insert a video of it in here someplace.  However, all I have from that exercise is the output, a .DSK file, in fact it is called 'scarab2.dsk', so it was an updated version - I don't have the source-code anymore, I've searched all the hardware machines / emulators where I have CPC emulator files, it's gone.  BUT, as I say, I do have the final output and it still plays well (if I say so myself).


and here's a snapshot video showing it in action... wow... 2009.... and for some odd reason (okay, I know the reason, the key [G] is used to start the game - it is in the center of the keyboard.  I should have used the [space bar] or a [fire button] then I could start the game on the video cabinet I have!)

It's actually quite tough - as you can see, I do recall there is a keypress to jump to the next level, but darned if I can remember what it was, something like H+x where x was the cavern number, maybe, maybe not? I'll remember one day.  Or maybe I'll disassemble the code via the emulator and try and figure it out.  I know I designed something like 20 levels, all different and rather sneaky!


Then I moved house.  I moved all this hardware / kit / etc.. into a much smaller room, I was continuing to work around the world, so was rarely at home, a lot of dust was being gathered.  I also had changed interests into making my Hot Rod Custom car.... I can't blame my missus at all, however, she wanted to use the room for arts&crafts and I had to move everything into a different room on the other side of the house... as I was packing things to move them, I decided to move them into the garage & offer the entire collection up for sale.  I was asking £1000.  That was a small fraction of what I had spent, but I wanted it to go to a collector and not someone who'll be eBaying everything the next day.  I sold the entire collection to a guy from the CPCWiki forum, he filled an entire Vauxhall afira (with seats down) from top to bottom, front to back AND  the roof box.  He had everything.  It all went.  I closed the door on the Amstrad world, I disconnected from the CPCWiki forum world and moved on.

Then, at the start of 2023, I "found" the Atari 800 XL devices in the loft, got them up & running, figured out how to write C code for them and to do some interesting stuff with them... I restrained myself from falling into the 'obsession' route with the Ataris, they are good as they are - I've bought a few (6) original ROM cartridge games, I've bought an S-Drive Max and a FujiNet and that is it.  I don't need anything more.

Then, I was up in the loft and I found the carcass of a CPC 6128 - well, it was literally the top keyboard part of the case.  No actual machine or motherboard or disc drive, just the top case and keyboard.  It has the 2 ribbon connectors that you connect to the motherboard.  I brought it down into the "office".

I ponder it for a short time, I'm pretty sure that technology has moved on a LOT and I bet there are things that  I can now plug that keyboard into that would allow me to use it as an external USB keyboard - with little to no effort.  That would be cool!  anyway, a quick look on the inter-webs and yep, of course there were things... in fact, I sold off my collection in about 2015/2016.  Then from 2017 onwards there was an explosion of people doing loads of hardware things with arduinos / esp8266s etc..etc.. that propelled these old CPCs into a different era.

This guy built and sold the exact device I want now.

https://web.archive.org/web/20201001120949/http://blog.tynemouthsoftware.co.uk/2016/02/amstrad-cpc6128-usb-keyboard.html

However, he has not only designed, built, made, sold these - he has now stopped doing this - a complete lifecycle has occurred & I missed it totally!  Hence the usage of archive.org in the URL above.


Okay, so what has this got to do with Cross compiled C code for the Amstrad?

Well, I was just setting context of how I got too where I am.  I found an old CPC 6128 keyboard.  That then cross-linked to the Atari 800 C coding experience.  I then decided to setup my UBuntu linux laptop to allow me to use it to compile C code and deploy it to the emulator.  I'll step through the setup and execution below.

I then thought to myself, wouldn't it be "nice" to then be able to deploy it to a real piece of hardware, like I was doing with the Atari 800 XLs.  I then went online.  F*%& me!  The prices of this stuff is now INSANE!  If I had kept my collection, it would have a valuation of over £10,000+ now.  OMG!

Anyway, long story short, I purchased a CPC 646 unit (as-is), the long one with a tape-drive.  I bought all the cables, that are now, just available (didn't have that back in the day, you HAD to have the monitor to power the computer or it didn't work), it can also be powered by a 5v USB cable! I have the monitor cables for output to SCART (there is also a HDMI adapter available too) and I bought a Tape replacement drive - bit like a gotek but for tape drives, just stick an SD card in with all the files and it loads digitally.

I then spotted a CPC 6128 and 50x3" discs (these are rare) on fbook marketplace, did a deal with the seller - who is the original owner of the machine from the late 80s! it includes a colour monitor and has the original disc drive all fully working and immaculate - I'll pick that up this Satruday!

I may either purchase a gotek external drive (or remove it from the Atari ST I have in the loft - don't ask!), apart from that, I don't plan on purchasing anything else... well, maybe this device that allows the CPC machines to connect to the inter-webs (like the FujiNet does for the Atari 800 XL) - although I'm pretty competent nowadays so could probably figure out how to do this myself, it's the ASM coding stuff that would probably trip me up.  we'll see.


However, once I have the 2 physical devices, I then need to be able to CODE C for them - to do "useful" things with the ability to connect over the network etc..etc...

and that is where being able to write C code and deploy onto the CPC hardware comes into play.

Journey steps:

The CPCWiki has a great page HERE:


However, what I was really looking for was: what tools do I need to download in order to do the Z80 compilation of C code?

I got distracted a bit by this GITHUB REPO - it failed to compile/make when I tried it, I investigated it for about 10minutes and then gave up.  The interesting thing was all of the sub-components that were used, they proved very insightful later on.

This then led me onto this C library for handling sprites.  again, very interesting, but distractional.

This then led me to CPCTeleta which is HERE:  (Hello Lachlan Keown! He was the nice chap that helped me out with the ASM for my 'scarb' game! looks like he then went on a lot further journey to make a platform engine to create apps/games etc.. with a bunch of other smart people).  Alas, again, this was a distraction.

Ah, ha! well, there we go, eventually made it to the Z88DK website.

(why Z88? Well, it was originally written for the Cambridge Z88 and was then opened up to handle a lot more Z80 based CPU machines... interesting stuff!)

More info. available about Z88DK HERE on the CPCWiki:


So... whilst the installation instructions for Linux stated to download the source, compile, build it etc..etc... I checked and for UBuntu Linux 22.04+ there is a snap available... well, it would be rude not to...

Easy-peasy pressing of 1-button and download done.  However... there is one subtle little task that needs to be done.  As it is a snap, it is namespaced.  meaning that to call the "zcc" compiler command you have to type "z88dk.zcc" and need to prefix all the other commands with the "z88dk.", which means anything you find or attempt to re-use off the inter-webs is likely to fail (also any guides / walkthroughs will fail), good news is you can just create some alias commands (now, where are they in my history?)

In fact it is explained on this page HERE:


That now means we can type:

$ zcc

and it will kick off the compilation that we need.

Right, that is all the setup that is needed, isn't it?  Well... yes & no.  I originally thought so and went off and created an app1.c file, used the zcc compile command to make a app1.bin file, then... ah, how do I get that file "inside" the emulator? hmmm.... well, it needs to be put onto a virtual Disc... it needs to go inside a .dsk file.

Well, that seems easy enough, along the journey, I discovered iDSK and Sector-CPC, I would just use one or both or all of those apps to make the app1.dsk file, add the app1.bin file into it and then load that app1.dsk inside the emulator and away we go.... Well.... again, yes & no.

That's where I fell into the 'late at night' trap :-D  I was able to do the above and then from within the CPC emulator, loading the app1.dsk and then from the prompt:

Ready:

run"app1.bin"

this would do a variety of things, it could cause a blank screen, could cause a lock up or a reset or show the [press PLAY to start the tape]

I went round and round in circles, trying different tooling, different parameters, etc..etc... and one morning at around 01:30am the penny dropped - I was treating the CPC as an Atari 800 XL! D'oh!

Here are the tools that 'can' be used for creating .dsk files:

Sector-CPC

iDSK   (CPCWiki reference)

CPCDiskXP.exe  (Windows executable)

To create the blank/empty .dsk file using these tools you can do the following:

$ ./sector-cpc --file app1.dsk new

or

$ ./iDSK app1.dsk -n

This will create an app1.dsk file that you can then place the compiled .bin file into.


First off you must compile the app1.c code, like so:

$ zcc +cpc -create-app -unsigned -o app1.bin app1.c -lndos -O3 -zorg=16384

now, that set of parameters is the evolution of learning.  It will create app1.bin that then needs to be copied into the app1.dsk file.

Now, you can do this via command-line, such as:

$ ./iDSK app1.dsk -i app1.bin -c 4000 -e 4000 -t 1

That will detect if that file exists, if it does it will overwrite and then the output will be:

------------------------------------

DSK : app1.dsk

Amsdos file : app1.bin

Creation automatique d'une en-tete pour le fichier ...

APP1    .BIN 0

------------------------------------

The subtle point here is the -c 4000 and -e 4000 parameters above, this is telling the tool to use AMSDOS and place the app1.bin file at location &4000.

Why is this key?  Well, you know earlier when I was just trying to do:

Ready:

run"app1.bin"

and it was crashing, well, this is NOT how you invoke that app1.bin file.  You need to do the following:

Ready:

memory &3fff

load"app1.bin", &4000

call &4000

and THIS will then execute the application and you will see the output!

I eventually found all of this out via various web-sites, after the fact, but I wish I would have found the information sooner, so I could have saved myself a lot of round and round time.

OKAY - so I did start to cheat a little bit.  My UBuntu linux machine already had WINE installed from something I was doing previously, so it made sense to download it and see if I could run it.

I downloaded the file from HERE:


and run it from the command-line with:

$ wine cpcdiskxp.exe

and up it pops as a mock 3" disc!


Pressing the [ DSK EDITOR ] in the bottom middle, select the [app3.dsk] file from early, it should be empty, then select to add the file, locate and select the app3.bin file, it will prompt if you want to add the AMSDOS settings, select [yes] then you will see this screen:

Notice I changed the default 0x6000 values to be 0x4000 - this aligns with the text from earlier.

Once finished, press [Save] and save the app3.dsk so that you can load it into the emulator.

Let's take a look to see what is inside the floppy disk:


Then typing the program loader code - this we could put into a LOADER.BAS file and then just run that rather than having to type these lines everytime.


this this produces:


I have noticed that the keyboard is sometimes unresponsive or it outputs a random set of number values such as 6's, 2's or 3's... until the keyboard buffer is full.  Which means that the C code is waiting for a keypress and then it will reboot after displaying the alphabet above - this can make it difficult sometimes to see the output as it reboots too quickly.  Am sure that is just a Caprice emulator with UBuntu issue.  probably needs a reboot :-D

Oh, what emulator did I install & use? Caprice32 v4.6.0 - why that version? Again it was available as a snap install for UBuntu linux, made sense to use it.

So, there we have it.... I can now write C code using whatever I want as a text editor, sve the .C file, then compile it using 'zcc' to generate a .bin file, then insert that file into a .dsk file, it can also be put into a TAPE file, then once I have that .DSK file I can either put it into an emulator on Linux or Windows or A.N.Other or copy it to a microSD card that I then insert into a device on real hardware and then run the same thing.  sweet.

Well, this is just the start.  I've just ordered some new desk(s) for the "new 8-bit room", a different room in the house that I'm now going to use as a "retro-room" that will have the Atari 800 XL machines, the Amstrad CPC machines... and I found a few ZX Spectrum devices and some Toshiba MSX machines too, so it could become very interesting... as the Speccie & MSX devices are also Z80 based, the same process above with a different output flag? +zx or +msx will generate the right output file to use on those devices too.... let's see where this goes!

I've finally re-scratched that coding itch and this time I'm doing it in C, which is a lot easier for me to switch to as I've been a lot more familiar with that for the past 30years than writing Assembler.


This webpage HERE gives an indication of the parameter usage:


and also the fact you can generate an app1.cpc file that does not need the above, it will just run - however, I believe that will only work in the emulator rather than on real hardware.


I can see that the -zorg=xxxx parameter is used to define the memory location, however I cannot find where / what this value relates to?

Looking at this webpage HERE, it explains the fact you need to do the memory setting and calling, but again, it is not clear above the value for -zorg.  hmmm....

This webpage HERE, whilst it refers to Raspberry Pi, it is still valid in the content:


This webpage HERE, was also a nice little find, if you translate from German to English (if you need to), explained what a couple of the parameters were and also about using different library for different size outputs.  interesting.


UPDATE:

Well, I figured if I could run the CPCWinXP.exe appliation via wine, then maybe I could run WinApe.exe too... and guess what?! yep, can do that - why would I do that?  Well, it allows me to use the Disassembler!


I have no idea anymore what I am looking at, but hopefully this webpage and articles may help me one day reverse engineer some of the code and may even let me re-make and recompile it again using a MAXAM ROM or something.  We'll see.  All I'm really looking for is that 'cheat code' I remember building in :-D

Y'know the more I use Winape.exe, the more I have a sneaky feeling that I coded this game with this emulator.  So, on a machine somewhere, someplace that I was using back in 2009, I have text files that I wrote the code in and then copy & pasted into WinApe Assembler and then executed / debugged - I have a vague memory of doing that... wow... let me do the maths, 2023-2009 = 14years... now, what laptop would I have been using? I was working for Lombardi software and was working over in Paris, France for COTY.... hmmm... maybe I have a backup of that era before IBM purchased Lombardi software in 2010.  It actually might exist!?  need to look around.


Comments