Main Page

- Main Page
- Related Pages
- Files

- File List
- Globals

flash.c
Go to the documentation of this file.
1 /*
2 * $Id: flash.c,v 1.12 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 /**
27 * \file flash.c
28 * \brief Flash memory methods
29 *
30 * This module provides functions for reading and writing to flash memory.
31 * Flash memory is the program (.text) memory in AVR's Harvard architecture.
32 * It is completely separate from RAM, which is simulated in the memory.c
33 * file.
34 */
35
36 #include <config.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <errno.h>
46
47 #include "avrerror.h"
48 #include "avrmalloc.h"
49 #include "avrclass.h"
50 #include "utils.h"
51
52 #include "storage.h"
53 #include "flash.h"
54
55 #include "display.h"
56
57 /***************************************************************************\
58 *
59 * Local Static Function Prototypes
60 *
61 \***************************************************************************/
62
63 static int flash_load_from_bin_file (Flash *flash, char *file);
64
65 /***************************************************************************\
66 *
67 * Flash(Storage) Methods
68 *
69 \***************************************************************************/
70
71 /**
72 * \brief Reads a 16-bit word from flash.
73 * \return A word.
74 */
75
76 extern inline uint16_t flash_read (Flash *flash, int addr);
77
78 /**
79 * \brief Reads a 16-bit word from flash.
80 * \param flash A pointer to a flash object.
81 * \param addr The address to which to write.
82 * \param val The byte to write there.
83 */
84
85 void
86 flash_write (Flash *flash, int addr, uint16_t val)
87 {
88 display_flash (addr, 1, &val);
89 storage_writew ((Storage *)flash, addr * 2, val);
90 }
91
92 /** \brief Write the low-order byte of an address.
93 *
94 * AVRs are little-endian, so lo8 bits in odd addresses.
95 */
96
97 void
98 flash_write_lo8 (Flash *flash, int addr, uint8_t val)
99 {
100 storage_writeb ((Storage *)flash, addr * 2 + 1, val);
101 }
102
103 /** \brief Write the high-order byte of an address.
104 *
105 * AVRs are little-endian, so hi8 bits in even addresses.
106 */
107
108 void
109 flash_write_hi8 (Flash *flash, int addr, uint8_t val)
110 {
111 storage_writeb ((Storage *)flash, addr * 2, val);
112 }
113
114 /** \brief Allocate a new Flash object. */
115
116 Flash *
117 flash_new (int size)
118 {
119 Flash *flash;
120
121 flash = avr_new (Flash, 1);
122 flash_construct (flash, size);
123 class_overload_destroy ((AvrClass *)flash, flash_destroy);
124
125 return flash;
126 }
127
128 /** \brief Constructor for the flash object. */
129
130 void
131 flash_construct (Flash *flash, int size)
132 {
133 int base = 0;
134 int i;
135
136 if (flash == NULL)
137 avr_error ("passed null ptr");
138
139 storage_construct ((Storage *)flash, base, size);
140
141 /* Init the flash to ones. */
142 for (i = 0; i < size; i++)
143 storage_writeb ((Storage *)flash, i, 0xff);
144 }
145
146 /**
147 * \brief Destructor for the flash class.
148 *
149 * Not to be called directly, except by a derived class.
150 * Called via class_unref.
151 */
152 void
153 flash_destroy (void *flash)
154 {
155 if (flash == NULL)
156 return;
157
158 storage_destroy (flash);
159 }
160
161 /** \brief Load program data into flash from a file. */
162
163 int
164 flash_load_from_file (Flash *flash, char *file, int format)
165 {
166 switch (format)
167 {
168 case FFMT_BIN:
169 return flash_load_from_bin_file (flash, file);
170 case FFMT_IHEX:
171 case FFMT_ELF:
172 default:
173 avr_warning ("Unsupported file format\n");
174 }
175
176 return -1;
177 }
178
179 static int
180 flash_load_from_bin_file (Flash *flash, char *file)
181 {
182 int fd, res;
183 int addr = 0;
184 uint16_t inst;
185
186 fd = open (file, O_RDONLY);
187 if (fd < 0)
188 avr_error ("Couldn't open binary flash image file: %s: %s", file,
189 strerror (errno));
190
191 while ((res = read (fd, &inst, sizeof (inst))) != 0)
192 {
193 if (res == -1)
194 avr_error ("Error reading binary flash image file: %s: %s", file,
195 strerror (errno));
196
197 flash_write (flash, addr, inst);
198
199 addr++;
200 }
201
202 close (fd);
203
204 return 0;
205 }
206
207 /** \brief Accessor method to get the size of a flash. */
208
209 int
210 flash_get_size (Flash *flash)
211 {
212 return storage_get_size ((Storage *)flash);
213 }
214
215 /**
216 * \brief Dump the contents of the flash to a file descriptor in text format.
217 *
218 * \param flash A pointer to a flash object.
219 * \param f_core An open file descriptor.
220 */
221
222 void
223 flash_dump_core (Flash *flash, FILE * f_core)
224 {
225 int size = storage_get_size ((Storage *)flash) / 2;
226 int i;
227 int dup = 0;
228 int ndat = 8;
229 char line[80];
230 char last_line[80];
231 char buf[80];
232
233 line[0] = last_line[0] = '\0';
234
235 fprintf (f_core, "Program Flash Memory Dump:\n");
236 for (i = 0; i < size; i++)
237 {
238 if (((i % ndat) == 0) && strlen (line))
239 {
240 if (strncmp (line, last_line, 80) == 0)
241 {
242 dup++;
243 }
244 else
245 {
246 if (dup > 0)
247 fprintf (f_core, " -- last line repeats --\n");
248 fprintf (f_core, "%04x : %s\n", i - ndat, line);
249 dup = 0;
250 }
251 strncpy (last_line, line, 80);
252 line[0] = '\0';
253 }
254 snprintf (buf, 80, "%04x ", flash_read (flash, i));
255 strncat (line, buf, 80 - strlen(line) - 1);
256 }
257 if (dup > 0)
258 {
259 fprintf (f_core, " -- last line repeats --\n");
260 fprintf (f_core, "%04x : %s\n", i - ndat, line);
261 }
262 }

Automatically generated by Doxygen 1.8.2 on Mon Aug 18 2014.