Developing in C

edited February 2015 in Development
I am beginning a small project for the ZX Spectrum. I intend to write the thing in C, doing the entire development on PC and then transferring somehow to an emulated ZX Spectrum.

My question is, how best to read and write to the I/O? The operations equivalent to assembly language IN and OUT?
Post edited by wilsonsamm on

Comments

  • edited February 2015
    wilsonsamm wrote: »
    My question is, how best to read and write to the I/O? The operations equivalent to assembly language IN and OUT?

    What sort of i/o? There may be functions that do some things for you already.

    If you want to do low level i/o as in asm you can:

    1. Use inline asm to do the i/o. You may or may not have to do some fiddling to make the C compiler aware of results or to gather the operands for the i/o.
    main()
    {
       ...
    
       #asm
    
       ld bc,$xxxx
       out (c),l   ;; where did the value of L come from -- that depends
    
       #endasm
    
       ...
    
    }
    

    Related to this is you could make a macro that inlines asm i/o instructions but the operands to the macro must resolve to constants at compile time.


    2. The C compiler may have functions that expose z80 i/o. z88dk has the following in the new clib:
    extern uint8_t    z80_inp(uint16_t port);
    extern void      *z80_inir(void *dst, uint16_t port);
    extern void      *z80_indr(void *dst, uint16_t port);
    extern void       z80_outp(uint16_t port, uint16_t data);
    extern void      *z80_otir(void *src, uint16_t port);
    extern void      *z80_otdr(void *src, uint16_t port);
    

    The downside is these are functions so the compiler must push params on the stack (sccz80 no if it's one parameter) and call which is much slower than a simple 'out'.


    3. sdcc calls the i/o space "special function registers (sfr)" and has special syntax to declare their associate i/o addresses.
    __sfr __at 0x78 IoPort;     // an 8-bit i/o port at address 0x78
    
    __sfr __banked __at 0x00fe Io_Keys;   // a 16-bit i/o port at address
    
    main()
    {
       ...
    
       IoPort = 200;  // write 200 to port 0x78
       a = Io_Keys;  // read port 0x00fe
    
       ...
    }
    

    The resulting code is composed of i/o instructions just like you would want.
  • fogfog
    edited February 2015
    wilson did you look at http://www.z88dk.org/wiki/doku.php

    just curious
Sign In or Register to comment.