Port in and out from C

358 views
Skip to first unread message

Willem Schreuder

unread,
Feb 16, 2021, 10:23:00 AM2/16/21
to RC2014-Z80
I am looking for C functions that performs the same functions as the BASIC in and out functions to write or read ports on an RC2014 under CP/M.  I've been using the HiTech C 3.09 compiler, but I'm not wedded to it.  There is an inout.asm in the library source that seems to do this, but I don't see the interface definition for it.

Sorry for the dumb question but I'm trying to clear out 40 years worth of cobwebs since I last peeked and poked Z80 ports.

Chris Brunner

unread,
Feb 16, 2021, 10:43:19 AM2/16/21
to rc201...@googlegroups.com
stdio in the Z88DK has inp(<address>) and outp(<address>, <value>) methods for reading and writing ports.  I've been using those without any issue to interface with Stephen Cousins' SC129 Digital I/O board.  I wrote a very small and simple wrapper library for that board that you could use as an example here: https://github.com/cyrusbuilt/SC129DigitalIO

The makefile compiles a test program that targets CP/M.

On Tue, Feb 16, 2021 at 10:23 AM Willem Schreuder <vlak...@gmail.com> wrote:
I am looking for C functions that performs the same functions as the BASIC in and out functions to write or read ports on an RC2014 under CP/M.  I've been using the HiTech C 3.09 compiler, but I'm not wedded to it.  There is an inout.asm in the library source that seems to do this, but I don't see the interface definition for it.

Sorry for the dumb question but I'm trying to clear out 40 years worth of cobwebs since I last peeked and poked Z80 ports.

--
You received this message because you are subscribed to the Google Groups "RC2014-Z80" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rc2014-z80+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/rc2014-z80/10ef0ec0-bc80-4d08-a21f-f26c5cb9f96an%40googlegroups.com.

Phillip Stevens

unread,
Feb 16, 2021, 7:22:11 PM2/16/21
to RC2014-Z80
Willem wrote:
I am looking for C functions that performs the same functions as the BASIC in and out functions to write or read ports on an RC2014 under CP/M.  I've been using the HiTech C 3.09 compiler, but I'm not wedded to it.

If you're happy to use a cross-compiler and move the resultant BIN/HEX over to your RC2014 using xmodem or similar, then imho z88dk is the best bet. And specifically using the (z)sdcc compiler for example.

z88dk has the standard inp() aond outp() functions for compliance, but there's a much faster method described in the SDCC Manual in section 3.5.2, which we use almost exclusively.
The compiler recognises these special function registers ports and generates in a,(*) and out (*),a (or the relevant register where the info is to be accessed) directly.
This takes 18 cycles to output a byte, typically.

Example of writing 0x01 to the ROM toggle port, and then reading the ACIA status register, and using the compiler to do this.

<arch.h>    // brings in the predefined RC2014 standard ports from the target build.
<arch/rc2014.h>   // defines __sfr for these ports (special function registers)

uint8_t status;

io_rom_toggle = 1;    // writes 0x01 to the port
status = io_acia_status;     // reads the ACIA status port

You can also do this via the standard inp() and outp() functions, but it is much much slower.
It takes approximately 100 cycles to achieve the same outcome.

If we take an example of using the outp() callee function, the resulting assembly looks like this:

call _z80_outp_callee ; 17

_z80_outp_callee:
  pop af  ;10
  pop bc  ;10
  dec sp  ;6
  pop hl  ;10
  push af  ;11
  ld l,h  ;4
  jp asm_z80_outp ;10

asm_z80_outp:
; enter : bc = port
; l = data
; uses : none
  out (c),l  ;12
  ret  ;10


Sorry for the dumb question but I'm trying to clear out 40 years worth of cobwebs since I last peeked and poked Z80 ports.

There are no dumb questions.
P.

Patrick Jackson

unread,
Feb 16, 2021, 8:45:32 PM2/16/21
to RC2014-Z80
Is there an in/out function in aztec c?

Phillip Stevens

unread,
Feb 17, 2021, 12:14:57 AM2/17/21
to RC2014-Z80
Willem wrote:
I am looking for C functions that performs the same functions as the BASIC in and out functions to write or read ports on an RC2014 under CP/M.  I've been using the HiTech C 3.09 compiler, but I'm not wedded to it.

If you're happy to use a cross-compiler and move the resultant BIN/HEX over to your RC2014 using xmodem or similar, then imho z88dk is the best bet. And specifically using the (z)sdcc compiler for example.

I added a section on Hardware I/O to the RC2014 z88dk Wiki page for future reference.

Groups is devilishly hard to search. I don't know why Google make it so hard.
P.

Jim McGinnis

unread,
Feb 17, 2021, 7:22:33 PM2/17/21
to RC2014-Z80
You might want to check page 134 in the HI-TECH C USER'S MANUAL.

I did do some preliminary quick tests and the functions appear to be proper.
INP(unsigned port) and OUTP(unsigned, unsigned data)

Cheers
Jim

Willem Schreuder

unread,
Feb 17, 2021, 11:42:47 PM2/17/21
to RC2014-Z80
Thanks for all the replies.  Jim McGinnis' had the correct answer for HiTech C.  There is an INP(port) and OUTP(port,value) in stdio.  If it was a snake it would have bit me...  Much appreciated!
Reply all
Reply to author
Forward
0 new messages