I wanted to have some Arduino C style functions, for example I/O bit functions, Defines for port setup using words vs. numbers.
Example:
// initialize PIO portsz80_outp( Z80_PIO_A_CTL, Z80_PIO_M0); // set mode 0 byte outputz80_outp( Z80_PIO_B_CTL, Z80_PIO_M1); // set mode 1 byte input
* z80_pio.c *//* Z80 PIO Library by Eric Stringer *//* eric.stringer@gmail.com version 1.0 March 2017 */
#include "z80_pio.h"#include <z80.h>
unsigned char Bit_mask[8] = {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000};
void z80_pio_write_bit(unsigned char port, unsigned char bit, unsigned bit_state){unsigned char data; if (bit > 7) bit = 7; if (bit_state == 0) { data = z80_inp(port) & ~Bit_mask[bit]; z80_outp(port, data); }else{ data = z80_inp(port) | Bit_mask[bit]; z80_outp(port, data); } } unsigned char z80_pio_read_bit(unsigned char port, unsigned char bit){unsigned char data;
if (bit > 7 ) bit = 7; data = z80_inp(port) & Bit_mask[bit];if (data != 0) data = 1;return(data);}
/* z80_pio.h *//* Z80 PIO DEF */
/* define logic mask */
#define Bit_mask0 0b00000001#define Bit_mask1 0b00000010#define Bit_mask2 0b00000100#define Bit_mask3 0b00001000#define Bit_mask4 0b00010000#define Bit_mask5 0b00100000#define Bit_mask6 0b01000000#define Bit_mask7 0b10000000
// User system Define Z80 PIO location#define Z80_PIO_A 0x00 // Port A#define Z80_PIO_B 0x01 // Port B#define Z80_PIO_A_CTL 0x02 // Port Control A#define Z80_PIO_B_CTL 0x03 // Port Control B
// Z80 PIO Mode define#define Z80_PIO_M0 0x0f // Mode 0 Port is output only#define Z80_PIO_M1 0x4f // Mode 1 Port is input only#define Z80_PIO_M2 0x8f // Mode 2 Port is Bidirectonal operation#define Z80_PIO_M3 0xCf // Mode 3 Port is port direction can be // set per bit via direction mask. // this command expects a mask byte to follow // 0 bit = output and 1 bit = input //
// Z80 PIO interruot setup
#define Z80_PIO_EI 0x87 // Enable PIO interrupts// The following bit wise OR with above
// Mode 3 interrupt options// Bit D5 Look for port to be High or low state.// where set to 0 for low, 1 for high// Bit D6 AND/OR state, AND all ports must be in// active state, or only one need be in active state // for interrupt. Bit D4 is mask byte to follow,// Bit set to 1 that port bit is ignored, 0 watch port bit.
#define Z80_PIO_EI_MF 0b00010000 // Mask follows command#define Z80_PIO_EI_AND 0b01000000 // AND Bits #define Z80_PIO_EI_OR 0b00000000 // OR Bits #define Z80_PIO_EI_HIGH 0b00100000 // HIGH Transistion triggers#define Z80_PIO_EI_LOW 0b00000000 // LOW Transistion triggers
#define Z80_PIO_DI 0x07 // Disable PIO interrupts
void z80_pio_write_bit(unsigned char port, unsigned char bit, unsigned bit_state);unsigned char z80_pio_read_bit(unsigned char port, unsigned char bit);