File: device.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 (234 lines) | stat: -rw-r--r-- 5,536 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
/*
 * $Id: device.c,v 1.15 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
 *
 ****************************************************************************
 */

#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"

/** \file device.c
  * \brief VDevice methods
  *
  * These functions are the base for all other devices
  * mapped into the device space.
  */

/** \brief Create a new VDevice. */
VDevice *
vdev_new (char *name, VDevFP_Read rd, VDevFP_Write wr, VDevFP_Reset reset,
          VDevFP_AddAddr add_addr)
{
    VDevice *dev;

    dev = avr_new0 (VDevice, 1);
    vdev_construct (dev, rd, wr, reset, add_addr);
    class_overload_destroy ((AvrClass *)dev, vdev_destroy);

    return dev;
}

/** \brief Default AddAddr method.

    This generate a warning that the should let the developer know that the
    vdev needs to be updated. */

void
vdev_def_AddAddr (VDevice *dev, int addr, char *name, int related_addr,
                  void *data)
{
    avr_warning ("Default AddAddr called [addr=0x%x; '%s']\n", addr, name);
}


/** \brief Constructor for a VDevice. */
void
vdev_construct (VDevice *dev, VDevFP_Read rd, VDevFP_Write wr,
                VDevFP_Reset reset, VDevFP_AddAddr add_addr)
{
    if (dev == NULL)
        avr_error ("passed null ptr");

    class_construct ((AvrClass *)dev);

    dev->read = rd;
    dev->write = wr;
    dev->reset = reset;
    dev->add_addr = add_addr;
}

/** \brief Destructor for a VDevice. */
void
vdev_destroy (void *dev)
{
    if (dev == NULL)
        return;

    class_destroy (dev);
}

#if 0

/** \brief Compare the names of 2 devices
  * \param c1 The first device.
  * \param c2 is a string and not an AvrClass object, because this function is
  * called by dlist_lookup() which passes two AvrClass pointers. So the string
  * is casted to an *AvrClass.
  */
int
vdev_name_cmp (AvrClass *c1, AvrClass *c2)
{
    return strcmp (((VDevice *)c1)->name, (char *)c2);
}

#endif

#if 0
/** \brief Checks if a address is in the device's address range
  * \param c1 \c AvrClass to check.
  * \param c2 The address to check.
  *
  * \return The different between the device's address bounds and \a c2 or if
  * \a c2 is within the address range 0.
  *
  * \note When comparing an addr, c2 is really just a pointer (see
  * vdev_name_cmp() for details) to int and then we see if d1->base <= addr <
  * (d1->base+d1->size).
  */
int
vdev_addr_cmp (AvrClass *c1, AvrClass *c2)
{
    VDevice *d1 = (VDevice *)c1;
    int addr = *(int *)c2;

    if (addr < d1->base)
        return (addr - d1->base);

    if (addr >= (d1->base + d1->size))
        /* Add one to ensure we don't return zero. */
        return (1 + addr - (d1->base + d1->size));

    /* addr is in device's range */
    return 0;
}
#endif

/** \brief Reads the device's value in the register at \a addr. */
uint8_t
vdev_read (VDevice *dev, int addr)
{
    return dev->read (dev, addr);
}

/** \brief Writes an value to the register at \a addr. */
void
vdev_write (VDevice *dev, int addr, uint8_t val)
{
    dev->write (dev, addr, val);
}

/** \brief Resets a device. */
void
vdev_reset (VDevice *dev)
{
    dev->reset (dev);
}

/** \brief Set the core field. */
void
vdev_set_core (VDevice *dev, AvrClass *core)
{
    dev->core = (AvrClass *)core;
}

/** \brief Get the core field. */
extern inline AvrClass *vdev_get_core (VDevice *dev);

/** \brief Inform the vdevice that it needs to handle another address.

    This is primarily used when creating the core in dev_supp_create_core(). */

extern void
vdev_add_addr (VDevice *dev, int addr, char *name, int rel_addr, void *data)
{
    if (dev->add_addr)
    {
        dev->add_addr (dev, addr, name, rel_addr, data);
    }
    else
    {
        avr_warning ("attempt to add addr to vdev with no add_addr() method: "
                     "%s [0x%04x]\n", name, addr);
    }
}

#if 0
/** \brief Get the device's base address. */
int
vdev_get_base (VDevice *dev)
{
    return dev->base;
}
#endif

#if 0
/** \brief Set the device's size (the number of bytes of the address space it
 *  consumes). */
int
vdev_get_size (VDevice *dev)
{
    return dev->size;
}
#endif

#if 0
/** \brief Get the device's name. */
char *
vdev_get_name (VDevice *dev)
{
    return dev->name;
}
#endif