Monday 6 September 2021

z88dk adt p_forward_list demo

 Demo of how to use the z88dk adt p_forward_list

#include <stdio.h> 
#include <stdint.h>     // standard names for ints with no ambiguity 
#include <adt/p_forward_list.h>

static p_forward_list_t src;

typedef struct
{
    void *next;

    uint8_t v;
item_t;

int main()
{
    uint8_t l;

    item_t item1;
    item_t item2;
    item_t item3;

    item1.v = 1;
    item2.v = 2;
    item3.v = 3;

    item_t *item_ptr;

    p_forward_list_init(&src);

    printf("Push 1, 2, 3 to front\n");
    p_forward_list_push_front(&src,&item1);
    p_forward_list_push_front(&src,&item2);
    p_forward_list_push_front(&src,&item3);        

    l = p_forward_list_size(&src);
    printf("List length = %d\n"l);
    printf("Read items from list\n");

    for (item_ptr = p_forward_list_front(&src); item_ptritem_ptr = p_forward_list_next(item_ptr))
    {
        printf("Item = %d\n"item_ptr->v);
    }

    printf("Clear list\n"); 
    p_forward_list_clear(&src);
    l = p_forward_list_size(&src);
    printf("List length = %d\n"l);     

    printf("Push 1, 2, 3 to back\n");
    p_forward_list_push_back(&src,&item1);
    p_forward_list_push_back(&src,&item2);
    p_forward_list_push_back(&src,&item3);    

    for (item_ptr = p_forward_list_front(&src); item_ptritem_ptr = p_forward_list_next(item_ptr))
    {
        printf("Item = %d\n"item_ptr->v);
    }

    printf("Remove 2\n");
    p_forward_list_remove(&src,&item2);

    l = p_forward_list_size(&src);
    printf("List length = %d\n"l);
    printf("Read items from list\n");
    for (item_ptr = p_forward_list_front(&src); item_ptritem_ptr = p_forward_list_next(item_ptr))
    {
        printf("Item = %d\n"item_ptr->v);
    }

    while(1)
    {

    }

    return 0;
}

 

Sunday 5 September 2021

ZX Spectrum Next Hello World C program using z88dk

Hello World C program for the ZX Spectrum Next using the z88dk compiler.

Z88DK

Z88DK is a collection of software development tools that targets the 8080 and z80 family of machines. It allows development of programs in C, assembly language or any mixture of the two. What makes z88dk unique is its ease of use, built-in support for many z80 machines, including ZX Spectrum Next, and its extensive set of assembly language library subroutines implementing the C standard and extensions.

The Z88DK offers several C Run Times (CRT) that can be selected using -startup at compile time. These CRTs provide the initialisation code that is called before main(). Some of them provide a simple tty device which supports controls codes such as \x16nn move cursor to X,Y.

NB when installing z88dk on Windows you need to set up the environment variables

  1. create a environment variable to point to the z88dk root folder e.g. called z88dk
  2. include z88dk\bin in your PATH 
  3. set Z80_OZFILES to point to \lib e.g. $Env:Z80_OZFILES=$Env:z88dk+"\Lib\"
  4. ZCCCFG to point to the config folder e.g. $Env:ZCCCFG=$Env:z88dk+"\Lib\Config"

Hello World C program

The following program prints "Hello World" near the middle of the screen and then loops forever changing the border between red and yellow. 

The resultant .nex file can be loaded by a ZX Spectrum emulator such as CSpect or copied over and run on real ZX Spectrum Next hardware.

/* =========================================================================
    ZX Spectrum Next Hello World C program
    Compile using z88dk
    zcc +zxn -vn -SO3 -clib=sdcc_iy -startup=1 --max-allocs-per-node200000 .\helloworldSpectumNext.c -create-app -subtype=nex

    +zxn            target ZX Spectrum Next
    -vn             no verbose
    -SO3            set optimisation level 3
    -clib           use sdcc_iy library (and use the recommended zsdcc compiler which produces smaller code)
    -start          set the C Run Time. 1 is standard 32 column display tty_z88dk terminal
    --max-allocs-per-node   controls how deeply zsdcc looks at code alternatives
    -create-app     run the program to create an executable that can be run by an emulator
    -subtype=nex    Create a nex file executable
*/

#include <arch/zxn.h>   // ZX Spectrum Next architecture specfic functions
#include <stdio.h>      

// Define some macros to make use of tty_z88dk control codes
// Program must be compiled with a CRT that supports tty_z88dk e.g. -startup=1
#define printInk(k)          printf("\x10%c"'0'+(k))
#define printPaper(k)        printf("\x11%c"'0'+(k))
#define printAt(rowcol)    printf("\x16%c%c", (col)+1, (row)+1)

int main()
{
    printAt(10,10);                 // move cursor 
    puts("Hello World!");       

    while(1) {                      // loop for ever
            zx_border(INK_RED);     // set border red
            zx_border(INK_YELLOW);  // set border yellow
    };
    
    return 0;
}