File: memory.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 (489 lines) | stat: -rw-r--r-- 11,794 bytes parent folder | download | duplicates (3)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * $Id: memory.c,v 1.19 2004/05/19 23:02:11 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 memory.c
 * \brief Memory access functions.
 *
 * This module provides functions for reading and writing to simulated memory.
 * The Memory class is a subclass of AvrClass.
 */

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

#include "display.h"

/** \brief Allocates memory for a new memory object. */

Memory *
mem_new (int gpwr_end, int io_reg_end, int sram_end, int xram_end)
{
    Memory *mem;

    mem = avr_new0 (Memory, 1);
    mem_construct (mem, gpwr_end, io_reg_end, sram_end, xram_end);
    class_overload_destroy ((AvrClass *)mem, mem_destroy);

    return mem;
}

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

void
mem_construct (Memory *mem, int gpwr_end, int io_reg_end, int sram_end,
               int xram_end)
{
    if (mem == NULL)
        avr_error ("passed null ptr");

    mem->gpwr_end = gpwr_end;
    mem->io_reg_end = io_reg_end;
    mem->sram_end = sram_end;
    mem->xram_end = xram_end;

    mem->cell = avr_new0 (MemoryCell, xram_end + 1);

    class_construct ((AvrClass *)mem);
}

/** \brief Descructor for the memory object. */

void
mem_destroy (void *mem)
{
    int i;

    Memory *this = (Memory *)mem;

    if (mem == NULL)
        return;

    for (i = 0; i < this->xram_end; i++)
    {
        if (this->cell[i].vdev)
        {
            class_unref ((AvrClass *)this->cell[i].vdev);
        }
    }

    avr_free (this->cell);

    class_destroy (mem);
}

/** \brief Attach a device to the device list.
 
  Devices that are accessed more often should be attached
  last so that they will be at the front of the list.
 
  A default virtual device can be overridden by attaching
  a new device ahead of it in the list.  */

void
mem_attach (Memory *mem, int addr, char *name, VDevice *vdev, int flags,
            uint8_t reset_value, uint8_t rd_mask, uint8_t wr_mask)
{
    MemoryCell *cell;

    if (mem == NULL)
        avr_error ("passed null ptr");

    if (vdev == NULL)
        avr_error ("attempt to attach null device");

    if ((addr < 0) || (addr >= mem->xram_end))
        avr_error ("address out of range");

    cell = &mem->cell[addr];

    cell->name = name;
    cell->flags = flags;
    cell->reset_value = reset_value;
    cell->rd_mask = rd_mask;
    cell->wr_mask = wr_mask;

    class_ref ((AvrClass *)vdev);
    cell->vdev = vdev;
}

/** \brief Find the VDevice associated with the given address. */

VDevice *
mem_get_vdevice_by_addr (Memory *mem, int addr)
{
    return mem->cell[addr].vdev;
}

/** \brief Find the VDevice associated with the given name. 

    \deprecated */

VDevice *
mem_get_vdevice_by_name (Memory *mem, char *name)
{
#if 0
    return (VDevice *)dlist_lookup (mem->dev, (AvrClass *)name,
                                    vdev_name_cmp);
#else
    avr_error ("use of deprecated interface");
    return NULL;
#endif
}

static inline MemoryCell *
mem_get_cell (Memory *mem, int addr)
{
    return mem->cell + addr;
}

static inline int
mem_is_io_reg (Memory *mem, int addr)
{
    return ((addr > mem->gpwr_end) && (addr <= mem->io_reg_end));
}

static inline char *
mem_get_name (Memory *mem, int addr)
{
    return mem->cell[addr].name;
}

void
mem_set_addr_name (Memory *mem, int addr, char *name)
{
    mem->cell[addr].name = name;
}

/** \brief Reads byte from memory and sanity-checks for valid address. 
 * 
 * \param mem A pointer to the memory object
 * \param addr The address to be read 
 * \return The byte found at that address addr
 */

uint8_t
mem_read (Memory *mem, int addr)
{
    MemoryCell *cell = mem_get_cell (mem, addr);

    if (cell->vdev == NULL)
    {
        char *name = mem_get_name (mem, addr);

        if (name)
        {
            avr_warning ("**** Attempt to read invalid %s: %s at 0x%04x\n",
                         mem_is_io_reg (mem, addr) ? "io reg" : "mem addr",
                         name, addr);
        }
        else
        {
            avr_warning ("**** Attempt to read invalid %s: 0x%04x\n",
                         mem_is_io_reg (mem, addr) ? "io reg" : "mem addr",
                         addr);
        }

        return 0;
    }

    return (vdev_read (cell->vdev, addr) & cell->rd_mask);
}

/** \brief Writes byte to memory and updates display for io registers. 
 * 
 * \param mem A pointer to a memory object
 * \param addr The address to be written to
 * \param val The value to be written there
 */

void
mem_write (Memory *mem, int addr, uint8_t val)
{
    MemoryCell *cell = mem_get_cell (mem, addr);

    if (cell->vdev == NULL)
    {
        char *name = mem_get_name (mem, addr);

        if (name)
        {
            avr_warning ("**** Attempt to write invalid %s: %s at 0x%04x\n",
                         mem_is_io_reg (mem, addr) ? "io reg" : "mem addr",
                         name, addr);
        }
        else
        {
            avr_warning ("**** Attempt to write invalid %s: 0x%04x\n",
                         mem_is_io_reg (mem, addr) ? "io reg" : "mem addr",
                         addr);
        }

        return;
    }

    /* update the display for io registers here */

    if (mem_is_io_reg (mem, addr))
        display_io_reg (addr - (mem->gpwr_end + 1), val & cell->wr_mask);

    vdev_write (cell->vdev, addr, val & cell->wr_mask);
}

/** \brief Resets every device in the memory object.
 * \param mem A pointer to the memory object.
 */

void
mem_reset (Memory *mem)
{
    int i;

    for (i = 0; i < mem->xram_end; i++)
    {
        MemoryCell *cell = mem_get_cell (mem, i);

        if (cell->vdev)
            vdev_reset (cell->vdev);
    }
}

static void
mem_reg_dump_core (Memory *mem, FILE * f_core)
{
    int i, j;

    fprintf (f_core, "General Purpose Register Dump:\n");
    for (i = 0; i < 32; i += 8)
    {
        for (j = i; j < (i + 8); j++)
            fprintf (f_core, "r%02d=%02x  ", j, mem_read (mem, j));
        fprintf (f_core, "\n");
    }
    fprintf (f_core, "\n");
}

/** \brief Fetch the name and value of the io register (addr). 
 *
 * \param mem A pointer to the memory object.
 * \param addr The address to fetch from.
 * \param val A pointer where the value of the register is to be copied.
 * \param buf A pointer to where the name of the register should be copied.
 * \param bufsiz The maximum size of the the buf string.
 */

void
mem_io_fetch (Memory *mem, int addr, uint8_t * val, char *buf, int bufsiz)
{
    MemoryCell *cell;

    if (mem_is_io_reg (mem, addr))
    {
        cell = mem_get_cell (mem, addr);

        if (cell->name == NULL)
        {
            strncpy (buf, "Reserved", bufsiz);
            *val = 0;
        }
        else
        {
            strncpy (buf, cell->name, bufsiz);

            if (cell->vdev)
            {
                /* FIXME: Add vdev_read_no_ext () interface to avoid calling
                   the external functions during a read. This will require a
                   reworking of how the vdev invokes the external read
                   method. */

                *val = (vdev_read (cell->vdev, addr) & cell->rd_mask);
            }
            else
            {
                *val = 0;
            }
        }
    }
    else
    {
        *val = 0;
        strncpy (buf, "NOT AN IO REG", bufsiz);
    }
}

static void
mem_io_reg_dump_core (Memory *mem, FILE * f_core)
{
    int i, j;
    char name[80];
    uint8_t val;

    int begin = mem->gpwr_end + 1;
    int end = mem->io_reg_end;
    int half = (end - begin) / 2;
    int mid = begin + half;

    fprintf (f_core, "IO Register Dump:\n");
    for (i = begin; i < mid; i++)
    {
        for (j = i; j < end; j += half)
        {
            memset (name, '\0', sizeof (name));
            mem_io_fetch (mem, j, &val, name, sizeof (name) - 1);

            fprintf (f_core, "%02x : %-10s : 0x%02x               ", j - half,
                     name, val);
        }
        fprintf (f_core, "\n");
    }
    fprintf (f_core, "\n");
}

static void
mem_sram_display (Memory *mem, FILE * f_core, int base, int size)
{
    int i;
    int dup = 0;
    int ndat = 16;
    char line[80];
    char last_line[80];
    char buf[80];
    line[0] = last_line[0] = '\0';

    for (i = base; i < (base + size); i++)
    {
        if (((i % ndat) == 0) && strlen (line))
        {
            if (strncmp (line, last_line, 80) == 0)
            {
                dup++;
            }
            else
            {
                if (dup > 0)
                    fprintf (f_core, "  -- last line repeats --\n");
                fprintf (f_core, "%04x : %s\n", i - ndat, line);
                dup = 0;
            }
            strncpy (last_line, line, 80);
            line[0] = '\0';
        }
        snprintf (buf, 80, "%02x ", mem_read (mem, i));
        strncat (line, buf, 80);
    }
    if (dup > 0)
    {
        fprintf (f_core, "  -- last line repeats --\n");
        fprintf (f_core, "%04x : %s\n", i - ndat, line);
    }
    fprintf (f_core, "\n");
}

static void
mem_sram_dump_core (Memory *mem, FILE * f_core)
{
    int size, base;

    /*
     * Dump the internal sram
     */

    if (mem->io_reg_end == mem->sram_end)
        return;                 /* device has no sram */

    fprintf (f_core, "Internal SRAM Memory Dump:\n");
    base = mem->io_reg_end + 1;
    size = mem->sram_end;
    mem_sram_display (mem, f_core, base, size);

    /*
     * If external sram present, dump it too.
     */

    if (mem->sram_end == mem->xram_end)
        return;                 /* device has no xram */

    fprintf (f_core, "External SRAM Memory Dump:\n");
    base = mem->sram_end + 1;
    size = mem->xram_end;
    mem_sram_display (mem, f_core, base, size);

}

#if 0

/* FIXME: Still need to figure out a sane way to look up a specific type of
   vdev by generic name. */

static void
mem_eeprom_dump_core (Memory *mem, FILE * f_core)
{
    VDevice *dev = mem_get_vdevice_by_name (mem, "EEProm");

    if (dev != NULL)
        eeprom_dump_core ((EEProm *)dev, f_core);
}
#endif

/** \brief Dump all the various memory locations to a file descriptor 
 *         in text format.
 *
 *  \param mem A memory object.
 *  \param f_core An open file descriptor.
 */

void
mem_dump_core (Memory *mem, FILE * f_core)
{
    mem_reg_dump_core (mem, f_core);
    mem_io_reg_dump_core (mem, f_core);
    mem_sram_dump_core (mem, f_core);
/*     mem_eeprom_dump_core (mem, f_core); */
}