41 #include "avrmalloc.h" 66 mem_new (
int gpwr_end,
int io_reg_end,
int sram_end,
int xram_end)
71 mem_construct (mem, gpwr_end, io_reg_end, sram_end, xram_end);
86 mem->gpwr_end = gpwr_end;
87 mem->io_reg_end = io_reg_end;
88 mem->sram_end = sram_end;
89 mem->xram_end = xram_end;
91 mem->cell =
avr_new0 (MemoryCell, xram_end + 1);
103 Memory *
this = (Memory *)mem;
108 for (i = 0; i < this->xram_end; i++)
110 if (this->cell[i].vdev)
130 mem_attach (Memory *mem,
int addr,
char *name, VDevice *vdev,
int flags,
131 uint8_t reset_value, uint8_t rd_mask, uint8_t wr_mask)
139 avr_error (
"attempt to attach null device");
141 if ((addr < 0) || (addr > mem->xram_end))
144 cell = &mem->cell[addr];
148 cell->reset_value = reset_value;
149 cell->rd_mask = rd_mask;
150 cell->wr_mask = wr_mask;
161 return mem->cell[addr].vdev;
172 return (VDevice *)
dlist_lookup (mem->dev, (AvrClass *)name,
175 avr_error (
"use of deprecated interface");
181 mem_get_cell (Memory *mem,
int addr)
188 if ((addr < 0) || (addr > mem->xram_end))
189 addr = mem->xram_end - 1;
191 return mem->cell + addr;
195 mem_is_io_reg (Memory *mem,
int addr)
197 return ((addr > mem->gpwr_end) && (addr <= mem->io_reg_end));
201 mem_get_name (Memory *mem,
int addr)
203 return mem->cell[addr].name;
207 mem_set_addr_name (Memory *mem,
int addr,
char *name)
209 mem->cell[addr].name = name;
222 MemoryCell *cell = mem_get_cell (mem, addr);
224 if (cell->vdev == NULL)
226 char *name = mem_get_name (mem, addr);
230 avr_warning (
"**** Attempt to read invalid %s: %s at 0x%04x\n",
231 mem_is_io_reg (mem, addr) ?
"io reg" :
"mem addr",
236 avr_warning (
"**** Attempt to read invalid %s: 0x%04x\n",
237 mem_is_io_reg (mem, addr) ?
"io reg" :
"mem addr",
244 return (
vdev_read (cell->vdev, addr) & cell->rd_mask);
257 MemoryCell *cell = mem_get_cell (mem, addr);
259 if (cell->vdev == NULL)
261 char *name = mem_get_name (mem, addr);
265 avr_warning (
"**** Attempt to write invalid %s: %s at 0x%04x\n",
266 mem_is_io_reg (mem, addr) ?
"io reg" :
"mem addr",
271 avr_warning (
"**** Attempt to write invalid %s: 0x%04x\n",
272 mem_is_io_reg (mem, addr) ?
"io reg" :
"mem addr",
281 if (mem_is_io_reg (mem, addr))
284 vdev_write (cell->vdev, addr, val & cell->wr_mask);
296 for (i = 0; i < mem->xram_end; i++)
298 MemoryCell *cell = mem_get_cell (mem, i);
306 mem_reg_dump_core (Memory *mem, FILE * f_core)
310 fprintf (f_core,
"General Purpose Register Dump:\n");
311 for (i = 0; i < 32; i += 8)
313 for (j = i; j < (i + 8); j++)
314 fprintf (f_core,
"r%02d=%02x ", j,
mem_read (mem, j));
315 fprintf (f_core,
"\n");
317 fprintf (f_core,
"\n");
330 mem_io_fetch (Memory *mem,
int addr, uint8_t * val,
char *buf,
int bufsiz)
334 if (mem_is_io_reg (mem, addr))
336 cell = mem_get_cell (mem, addr);
338 if (cell->name == NULL)
340 strncpy (buf,
"Reserved", bufsiz);
345 strncpy (buf, cell->name, bufsiz);
354 *val = (
vdev_read (cell->vdev, addr) & cell->rd_mask);
365 strncpy (buf,
"NOT AN IO REG", bufsiz);
370 mem_io_reg_dump_core (Memory *mem, FILE * f_core)
376 unsigned int begin = (unsigned)mem->gpwr_end + 1;
377 unsigned int end = (
unsigned)mem->io_reg_end;
378 unsigned int half = (end - begin + 1) / 2;
379 unsigned int mid = begin + half;
381 fprintf (f_core,
"IO Register Dump:\n");
382 for (i = begin; i < mid; i++)
384 for (j = i; j <= end; j += half)
386 memset (name,
'\0',
sizeof (name));
389 fprintf (f_core,
"%02x : %-10s : 0x%02x ", j,
392 fprintf (f_core,
"\n");
394 fprintf (f_core,
"\n");
398 mem_sram_display (Memory *mem, FILE * f_core,
int base,
int size)
406 line[0] = last_line[0] =
'\0';
408 for (i = base; i < (base + size); i++)
410 if (((i % ndat) == 0) && strlen (line))
412 if (strncmp (line, last_line, 80) == 0)
419 fprintf (f_core,
" -- last line repeats --\n");
420 fprintf (f_core,
"%04x : %s\n", i - ndat, line);
423 strncpy (last_line, line, 80);
426 snprintf (buf, 80,
"%02x ",
mem_read (mem, i));
427 strncat (line, buf, 80 - strlen(line) - 1);
431 fprintf (f_core,
" -- last line repeats --\n");
432 fprintf (f_core,
"%04x : %s\n", i - ndat, line);
434 fprintf (f_core,
"\n");
438 mem_sram_dump_core (Memory *mem, FILE * f_core)
446 if (mem->io_reg_end == mem->sram_end)
449 fprintf (f_core,
"Internal SRAM Memory Dump:\n");
450 base = mem->io_reg_end + 1;
451 size = mem->sram_end - base + 1;
452 mem_sram_display (mem, f_core, base, size);
458 if (mem->sram_end == mem->xram_end)
461 fprintf (f_core,
"External SRAM Memory Dump:\n");
462 base = mem->sram_end + 1;
463 size = mem->xram_end - base + 1;
464 mem_sram_display (mem, f_core, base, size);
474 mem_eeprom_dump_core (Memory *mem, FILE * f_core)
479 eeprom_dump_core ((EEProm *)dev, f_core);
493 mem_reg_dump_core (mem, f_core);
494 mem_io_reg_dump_core (mem, f_core);
495 mem_sram_dump_core (mem, f_core);
VDevice * mem_get_vdevice_by_name(Memory *mem, char *name)
Find the VDevice associated with the given name.
AvrClass * dlist_lookup(DList *head, AvrClass *data, DListFP_Cmp cmp)
Lookup an item in the list.
void mem_destroy(void *mem)
Descructor for the memory object.
void mem_construct(Memory *mem, int gpwr_end, int io_reg_end, int sram_end, int xram_end)
Constructor for the memory object.
void mem_write(Memory *mem, int addr, uint8_t val)
Writes byte to memory and updates display for io registers.
void class_ref(AvrClass *klass)
Increments the reference count for the klass object.
VDevice * mem_get_vdevice_by_addr(Memory *mem, int addr)
Find the VDevice associated with the given address.
void mem_io_fetch(Memory *mem, int addr, uint8_t *val, char *buf, int bufsiz)
Fetch the name and value of the io register (addr).
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)
Attach a device to the device list.
void avr_free(void *ptr)
Free malloc'd memory.
void class_destroy(void *klass)
Releases resources allocated by class's <klass>_new() function.
uint8_t mem_read(Memory *mem, int addr)
Reads byte from memory and sanity-checks for valid address.
#define avr_warning(fmt, args...)
Print a warning message to stderr.
void class_unref(AvrClass *klass)
Decrements the reference count for the klass object.
#define avr_error(fmt, args...)
Print an error message to stderr and terminate program.
Memory * mem_new(int gpwr_end, int io_reg_end, int sram_end, int xram_end)
Allocates memory for a new memory object.
void display_io_reg(int reg, uint8_t val)
Update an IO register in the display.
void mem_dump_core(Memory *mem, FILE *f_core)
Dump all the various memory locations to a file descriptor in text format.
uint8_t vdev_read(VDevice *dev, int addr)
Reads the device's value in the register at addr.
void class_overload_destroy(AvrClass *klass, AvrClassFP_Destroy destroy)
Overload the default destroy method.
void mem_reset(Memory *mem)
Resets every device in the memory object.
void class_construct(AvrClass *klass)
Initializes the AvrClass data structure.
#define avr_new0(type, count)
Macro for allocating memory and initializing it to zero.
void vdev_reset(VDevice *dev)
Resets a device.
void vdev_write(VDevice *dev, int addr, uint8_t val)
Writes an value to the register at addr.