44 #include "avrmalloc.h" 70 static Port *port_new (
int addr,
char *name);
71 static void port_construct (Port *p,
int addr,
char *name);
72 static void port_destroy (
void *p);
74 static uint8_t port_reg_read (VDevice *dev,
int addr);
75 static void port_reg_write (VDevice *dev,
int addr, uint8_t val);
76 static void port_reset (VDevice *dev);
78 static uint8_t port_read_pin (Port *p,
int addr);
80 static void port_write_port (Port *p,
int addr, uint8_t val);
82 static void port_write_ddr (Port *p,
int addr, uint8_t val);
84 static void port_add_addr (VDevice *vdev,
int addr,
char *name,
int rel_addr,
102 return (VDevice *)port_new (addr, name);
110 port_new (
int addr,
char *name)
115 port_construct (p, addr, name);
126 port_construct (Port *p,
int addr,
char *name)
131 vdev_construct ((VDevice *)p, port_reg_read, port_reg_write, port_reset,
134 port_add_addr ((VDevice *)p, addr, name, 0, NULL);
139 port_reset ((VDevice *)p);
143 port_add_addr (VDevice *vdev,
int addr,
char *name,
int rel_addr,
void *data)
145 Port *p = (Port *)vdev;
147 if (strncmp (
"PORT", name, 4) == 0)
152 else if (strncmp (
"DDR", name, 3) == 0)
157 else if (strncmp (
"PIN", name, 3) == 0)
164 avr_error (
"invalid port register name: '%s' @ 0x%04x", name, addr);
169 port_reset (VDevice *dev)
171 Port *p = (Port *)dev;
187 port_destroy (
void *p)
238 port_read_pin (Port *p,
int addr)
243 if (p->ext_rd && p->ext_enable)
244 data = p->ext_rd (addr);
265 port_write_port (Port *p,
int addr, uint8_t val)
274 if (p->ext_wr && p->ext_enable)
275 p->ext_wr (addr, (p->port & p->ddr));
279 port_write_ddr (Port *p,
int addr, uint8_t val)
289 if (p->ext_wr && p->ext_enable)
290 p->ext_wr (addr, (p->port & p->ddr));
295 port_reg_read (VDevice *dev,
int addr)
297 Port *p = (Port *)dev;
299 if (addr == p->ddr_addr)
302 else if (addr == p->pin_addr)
303 return port_read_pin (p, addr);
305 else if (addr == p->port_addr)
309 avr_error (
"Invalid Port Address: 0x%02x", addr);
315 port_reg_write (VDevice *dev,
int addr, uint8_t val)
317 Port *p = (Port *)dev;
319 if (addr == p->pin_addr)
321 avr_warning (
"Attempt to write to readonly PINx register\n");
324 else if (addr == p->ddr_addr)
326 port_write_ddr ((Port *)p, addr, val);
329 else if (addr == p->port_addr)
331 port_write_port ((Port *)p, addr, val);
336 avr_error (
"Invalid Port Address: 0x%02x", addr);
void vdev_destroy(void *dev)
Destructor for a VDevice.
void port_ext_disable(Port *p)
Disable external port functionality.
#define avr_warning(fmt, args...)
Print a warning message to stderr.
void vdev_construct(VDevice *dev, VDevFP_Read rd, VDevFP_Write wr, VDevFP_Reset reset, VDevFP_AddAddr add_addr)
Constructor for a VDevice.
void port_add_ext_rd_wr(Port *p, PortFP_ExtRd ext_rd, PortFP_ExtWr ext_wr)
Attaches read and write functions to a particular port.
#define avr_error(fmt, args...)
Print an error message to stderr and terminate program.
VDevice * port_create(int addr, char *name, int rel_addr, void *data)
Create a new Port instance.
void class_overload_destroy(AvrClass *klass, AvrClassFP_Destroy destroy)
Overload the default destroy method.
void port_ext_enable(Port *p)
Enable external port functionality.
#define avr_new0(type, count)
Macro for allocating memory and initializing it to zero.