C Development Environment with z88dk

 


Hello again and hope you have been enjoying my blog. I had been thinking, yes I know I shouldn't do it too often as it could be dangerous, but so far I have set up my coding environment for using z80, Boriel with NextBuild and Next Basic, all using VSCode and CSpect.
I have described in detail how to set up z80, but not Boriel or Next Basic; however, these were quite simple to set up and flowed well using information gained in my previous posts, some other tutorials on line and some assistance from my Speccy Next friend, John Weatherley.
Thanks John, I'm finding this community is very friendly and helpful so far. 
If however you are struggling with setting Boriel or Next Basic up with VSCode, then just let me know and I'll sort some more posts with some assistance.

One area which I found a little bit more tricky, is setting up for C using z88dk, so I thought I would describe this step here.

Look at my other blog posts for the z80 set up to get yourself familiar with all steps so far.

Dev Folders Reminder

This is how I now have my C: directory set up for all the environments:
C:\SpecNextDev 
        \cspect 
        \sjasmplus \SDCard
        \Projects   \nextBasic \nextBuild \z80 \.vscode tasks.json \Project1 \Project2 main.asm \Project3 \z88dk \z88dk (Contains all the files downloaded for z88dk) \.vscode tasks.json (Modified for building with z88dk) \Project1 main.c \Project2
 As you can see, I have moved all my individual z80 projects into a z80 folder within the main Projects folder along with the .vscode folder. Now when I open VSCode and want to develop using z80, I open the z80 folder in VSCode and find my individual project there to work on.
I also have 2 new folders for nextBasic and nextBuild. In these I have set up a similar environment to my z80 one, with projects for each in them, so again, if I want to develop using Next Basic, I just open that folder in VSCode and so on. This makes things much simpler and flexible.

z88dk

If you have set up your folders like mine, then lets go on to downloading z88dk.
You can find the latest nightly build here:

I downloaded the one that said - "z88dk-win32-latest.zip - Latest win32 build" at the top of the page and once downloaded I extracted the contents of the z88dk folder to my newly created z88dk folder above, in "Projects\z88dk\z88dk".

Next there are a couple of configuration things that need sorting. (Don't forget I am using Windows.)
Press Win + R and type "sysdm.cpl" then hit enter. Go to the advanced tab and click on Environment Variables.
Under system variables click on new and in the variable and value settings enter the following:
Variable name: ZCCCFG
Variable value: C:\SpecNextDev\Projects\z88dk\z88dk\lib\config

Now find Path under system variables and select it, then press edit. Click new and add the following: (I used this for 64-bit version of Windows.)
C:\SpecNextDev\Projects\z88dk\z88dk\bin

Looking at the instructions it should be the following for 32-bit Windows: (I have not tried this.)
C:\SpecNextDev\Projects\z88dk\z88dk\bin.x86

From the command prompt, you should be able to enter "zcc --version" to see if worked.
That should be it for the z88dk set up, so lets move to creating the JSON file and a main.c test file in VSCode.

VSCode

Using notepad or any other text editor, create a blank file called "tasks.json" and save it in the previously created ".vscode" folder inside Projects\z88dk. Ensure you use the extension ".json".
Now copy and paste the JSON code below into this file and save it. This will be used by VSCode to build and run the source C files when you open the Projects folder in VSCode.
{
  "version": "2.0.0",
  "tasks": [
    // This compiles with z88dk, copies the .nex file to the SD image with hdfmonkey and then runs cspect with the SD image (Use RUN BUILD TASK or F5)
    {
      "label": "Compile to SD Image",
      "type": "shell",
      "command": "cmd.exe",
      "args": [
        "/c",
        "zcc +zxn -subtype=nex -o ${fileDirname}\\${fileBasenameNoExtension} -create-app ${file} -m && \"C:\\SpecNextDev\\cspect\\hdfmonkey.exe\" put C:\\SpecNextDev\\cspect\\sdcard.mmc ${fileDirname}\\${fileBasenameNoExtension}.nex \\ && \"C:\\SpecNextDev\\cspect\\CSpect.exe\" -brk -w3 -50 -vsync -zxnext -nextrom -map=${fileDirname}\\${fileBasenameNoExtension}.map -mmc=C:\\SpecNextDev\\cspect\\sdcard.mmc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": []
    }
  ]
}
 
What the JSON file does for the build/compile process is basically this:
   zcc +zxn -subtype=nex -o main -create-app main.c -m

+zxn is the target platform for the Speccy Next, -subtype and -m generate the .nex and .map files.

All relevant files created in the build/run process, will be placed in our specific project folder. 
For example:
\Project1\ 
        main.c  - [our test code]
        main.nex - [created by VSCode]   main.map - [created by VSCode]
As I am using the same CSpect folder and sdcard.mmc as I did in my z80 set up, it all runs automatically with the autoexec.bas file I created in my post:

Nearly there!
Now all that is left is to create a bit of C code and run.

Code example

As previously mentioned all our code will be saved in their own folder within "Projects\z88dk", so create a folder called "HelloWorld" and then a source file inside called "main.c".
When saving a ".c" file, VSCode should use syntax highlight or will ask to install it. 

We should now see something like this: 
        C:\SpecNextDev\Projects\z88dk\HelloWorld\main.c

Now that is all complete, we are ready to put some code in this new ".c" file. 
Copy and paste the code and save with File, Save. 
#include <stdio.h>

void main(void) {
    printf("Hello, ZX Spectrum Next!\n");
}
 
Now the fun bit! 
From within VSCode, select from the menu, Terminal, Run Build Task or press F5. (May not be F5 on your version.) 
You should now see VSCode build and run our code! 
 
That is it, you should now see your program running within CSpect!!!

Have fun. 👾







Comments

Popular posts from this blog

Platypus Next Peril - Sprites!

Spectrum Next Z80 dev environment

Welcome to Pandapus