File: ports.c

package info (click to toggle)
simulavr 0.1.2.2-6.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,756 kB
  • ctags: 3,179
  • sloc: ansic: 19,987; sh: 3,623; python: 3,528; makefile: 406; asm: 308; yacc: 145; lex: 48
file content (339 lines) | stat: -rw-r--r-- 7,892 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * $Id: ports.c,v 1.17 2004/01/30 07:09:56 troth Exp $
 *
 ****************************************************************************
 *
 * simulavr - A simulator for the Atmel AVR family of microcontrollers.
 * Copyright (C) 2001, 2002, 2003, 2004  Theodore A. Roth
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 ****************************************************************************
 */

/**
 * \file ports.c
 * \brief Module for accessing simulated I/O ports.
 *
 * Defines an abstract Port class as well as subclasses for each individual
 * port.
 *
 * \todo Remove the pins argument and the mask field. That's handled at a
 * higher level so is obsolete here now.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "avrerror.h"
#include "avrmalloc.h"
#include "avrclass.h"
#include "utils.h"
#include "callback.h"
#include "op_names.h"

#include "storage.h"
#include "flash.h"

#include "vdevs.h"
#include "memory.h"
#include "stack.h"
#include "register.h"
#include "sram.h"
#include "eeprom.h"
#include "timers.h"
#include "ports.h"

#include "avrcore.h"

/****************************************************************************\
 *
 * Local static prototypes.
 *
\****************************************************************************/

static Port *port_new (int addr, char *name);
static void port_construct (Port *p, int addr, char *name);
static void port_destroy (void *p);

static uint8_t port_reg_read (VDevice *dev, int addr);
static void port_reg_write (VDevice *dev, int addr, uint8_t val);
static void port_reset (VDevice *dev);

static uint8_t port_read_pin (Port *p, int addr);

static void port_write_port (Port *p, int addr, uint8_t val);

static void port_write_ddr (Port *p, int addr, uint8_t val);

static void port_add_addr (VDevice *vdev, int addr, char *name, int rel_addr,
                           void *data);

/****************************************************************************\
 *
 * Port(VDevice) : I/O Port registers
 *
\****************************************************************************/

/**
 * \brief Create a new Port instance.
 *
 * This should only be used in DevSuppDefn initializers.
 */

VDevice *
port_create (int addr, char *name, int rel_addr, void *data)
{
    return (VDevice *)port_new (addr, name);
}

/**
 * \brief Allocates a new Port object.
 */

static Port *
port_new (int addr, char *name)
{
    Port *p;

    p = avr_new0 (Port, 1);
    port_construct (p, addr, name);
    class_overload_destroy ((AvrClass *)p, port_destroy);

    return p;
}

/**
 * \brief Constructor for the Port object.
 */

static void
port_construct (Port *p, int addr, char *name)
{
    if (p == NULL)
        avr_error ("passed null ptr");

    vdev_construct ((VDevice *)p, port_reg_read, port_reg_write, port_reset,
                    port_add_addr);

    port_add_addr ((VDevice *)p, addr, name, 0, NULL);

    p->ext_rd = NULL;
    p->ext_wr = NULL;

    port_reset ((VDevice *)p);
}

static void
port_add_addr (VDevice *vdev, int addr, char *name, int rel_addr, void *data)
{
    Port *p = (Port *)vdev;

    if (strncmp ("PORT", name, 4) == 0)
    {
        p->port_addr = addr;
    }

    else if (strncmp ("DDR", name, 3) == 0)
    {
        p->ddr_addr = addr;
    }

    else if (strncmp ("PIN", name, 3) == 0)
    {
        p->pin_addr = addr;
    }

    else
    {
        avr_error ("invalid port register name: '%s' @ 0x%04x", name, addr);
    }
}

static void
port_reset (VDevice *dev)
{
    Port *p = (Port *)dev;

    p->port = 0;
    p->ddr = 0;
    p->pin = 0;

    p->ext_enable = 1;
}

/**
 * \brief Destructor for the Port object
 *
 * This is a virtual method for higher level port implementations and as such
 * should not be used directly.
 */
void
port_destroy (void *p)
{
    if (p == NULL)
        return;

    vdev_destroy (p);
}

/** \brief Disable external port functionality.
 *
 * This is only used when dumping memory to core file. See mem_io_fetch().
 */
void
port_ext_disable (Port *p)
{
    p->ext_enable = 0;
}

/** \brief Enable external port functionality.
 *
 * This is only used when dumping memory to core file. See mem_io_fetch().
 */
void
port_ext_enable (Port *p)
{
    p->ext_enable = 1;
}

/**
 * \brief Attaches read and write functions to a particular port
 *
 * I think I may have this backwards. Having the virtual hardware supply
 * functions for the core to call on every io read/write, might cause missed
 * events (like edge triggered). I'm really not too sure how to handle this.
 *
 * In the future, it might be better to have the core supply a function for
 * the virtual hardware to call when data is written to the device. The device
 * supplied function could then check if an interrupt should be generated or
 * just simply write to the port data register.
 *
 * For now, leave it as is since it's easier to test if you can block when the
 * device is reading from the virtual hardware.
 */
void
port_add_ext_rd_wr (Port *p, PortFP_ExtRd ext_rd, PortFP_ExtWr ext_wr)
{
    p->ext_rd = ext_rd;
    p->ext_wr = ext_wr;
}

static uint8_t
port_read_pin (Port *p, int addr)
{
    uint8_t data;

    /* get the data from the external virtual hardware if connected */
    if (p->ext_rd && p->ext_enable)
        data = p->ext_rd (addr);
    else
        data = 0;

    /*
     * For a pin n to be enabled as input, DDRn == 0,
     * otherwise it will always read 0.
     */
    data &= ~(p->ddr);

    /*
     * Pass data to alternate read so as to check alternate functions of
     * pins for that port.
     */
/*      if (p->alt_rd) */
/*          data = p->alt_rd(p, addr, data); */

    return data;
}

static void
port_write_port (Port *p, int addr, uint8_t val)
{
    /* update port register */
    p->port = val;

    /*
     * Since changing p->port might change what the virtual hardware
     * sees, we need to call ext_wr() to pass change along.
     */
    if (p->ext_wr && p->ext_enable)
        p->ext_wr (addr, (p->port & p->ddr));
}

static void
port_write_ddr (Port *p, int addr, uint8_t val)
{
    /* update ddr register */
    p->ddr = val;

#if 0
    /*
     * Since changing p->ddr might change what the virtual hardware
     * sees, we need to call ext_wr() to pass change allong.
     */
    if (p->ext_wr && p->ext_enable)
        p->ext_wr (addr, (p->port & p->ddr));
#endif
}

static uint8_t
port_reg_read (VDevice *dev, int addr)
{
    Port *p = (Port *)dev;

    if (addr == p->ddr_addr)
        return p->ddr;

    else if (addr == p->pin_addr)
        return port_read_pin (p, addr);

    else if (addr == p->port_addr)
        return p->port;

    else
        avr_error ("Invalid Port Address: 0x%02x", addr);

    return 0;
}

static void
port_reg_write (VDevice *dev, int addr, uint8_t val)
{
    Port *p = (Port *)dev;

    if (addr == p->pin_addr)
    {
        avr_warning ("Attempt to write to readonly PINx register\n");
    }

    else if (addr == p->ddr_addr)
    {
        port_write_ddr ((Port *)p, addr, val);
    }

    else if (addr == p->port_addr)
    {
        port_write_port ((Port *)p, addr, val);
    }

    else
    {
        avr_error ("Invalid Port Address: 0x%02x", addr);
    }
}