Main Page

storage.c
1 /*
2 * $Id: storage.c,v 1.8 2003/12/02 08:25:00 troth Exp $
3 *
4 ****************************************************************************
5 *
6 * simulavr - A simulator for the Atmel AVR family of microcontrollers.
7 * Copyright (C) 2001, 2002, 2003 Theodore A. Roth
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 ****************************************************************************
24 */
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "avrerror.h"
32 #include "avrmalloc.h"
33 #include "avrclass.h"
34 #include "storage.h"
35
36 /***************************************************************************\
37 *
38 * Storage(AvrClass) Methods
39 *
40 \***************************************************************************/
41
42 Storage *
43 storage_new (int base, int size)
44 {
45 Storage *stor;
46
47 stor = avr_new (Storage, 1);
48 storage_construct (stor, base, size);
49 class_overload_destroy ((AvrClass *)stor, storage_destroy);
50
51 return stor;
52 }
53
54 void
55 storage_construct (Storage *stor, int base, int size)
56 {
57 if (stor == NULL)
58 avr_error ("passed null ptr");
59
60 class_construct ((AvrClass *)stor);
61
62 stor->base = base; /* address */
63 stor->size = size; /* bytes */
64
65 stor->data = avr_new0 (uint8_t, size);
66 }
67
68 /*
69 * Not to be called directly, except by a derived class.
70 * Called via class_unref.
71 */
72 void
73 storage_destroy (void *stor)
74 {
75 if (stor == NULL)
76 return;
77
78 avr_free (((Storage *)stor)->data);
79 class_destroy (stor);
80 }
81
82 extern inline uint8_t
83 storage_readb (Storage *stor, int addr);
84
85 extern inline uint16_t
86 storage_readw (Storage *stor, int addr);
87
88 void
89 storage_writeb (Storage *stor, int addr, uint8_t val)
90 {
91 int _addr = addr - stor->base;
92
93 if (stor == NULL)
94 avr_error ("passed null ptr");
95
96 if ((_addr < 0) || (_addr >= stor->size))
97 avr_error ("address out of bounds: 0x%x", addr);
98
99 stor->data[_addr] = val;
100 }
101
102 void
103 storage_writew (Storage *stor, int addr, uint16_t val)
104 {
105 int _addr = addr - stor->base;
106
107 if (stor == NULL)
108 avr_error ("passed null ptr");
109
110 if ((_addr < 0) || (_addr >= stor->size))
111 avr_error ("address out of bounds: 0x%x", addr);
112
113 stor->data[_addr] = (uint8_t) (val >> 8 & 0xff);
114 stor->data[_addr + 1] = (uint8_t) (val & 0xff);
115 }
116
117 int
118 storage_get_size (Storage *stor)
119 {
120 return stor->size;
121 }
122
123 int
124 storage_get_base (Storage *stor)
125 {
126 return stor->base;
127 }
avr_new
#define avr_new(type, count)
Macro for allocating memory.
Definition: avrmalloc.c:57
avr_free
void avr_free(void *ptr)
Free malloc&#39;d memory.
Definition: avrmalloc.c:187
class_destroy
void class_destroy(void *klass)
Releases resources allocated by class&#39;s <klass>_new() function.
Definition: avrclass.c:78
avr_error
#define avr_error(fmt, args...)
Print an error message to stderr and terminate program.
Definition: avrerror.c:50
class_overload_destroy
void class_overload_destroy(AvrClass *klass, AvrClassFP_Destroy destroy)
Overload the default destroy method.
Definition: avrclass.c:92
class_construct
void class_construct(AvrClass *klass)
Initializes the AvrClass data structure.
Definition: avrclass.c:61
avr_new0
#define avr_new0(type, count)
Macro for allocating memory and initializing it to zero.
Definition: avrmalloc.c:67

Automatically generated by Doxygen 1.8.12 on Tue Apr 18 2017.