00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
#include <config.h>
00027
00028
#include <stdio.h>
00029
#include <stdlib.h>
00030
00031
#include "avrerror.h"
00032
#include "avrmalloc.h"
00033
#include "avrclass.h"
00034
#include "storage.h"
00035
00036
00037
00038
00039
00040
00041
00042 Storage *
00043 storage_new (
int base,
int size)
00044 {
00045 Storage *stor;
00046
00047 stor =
avr_new (Storage, 1);
00048 storage_construct (stor, base, size);
00049
class_overload_destroy ((AvrClass *)stor, storage_destroy);
00050
00051
return stor;
00052 }
00053
00054
void
00055 storage_construct (Storage *stor,
int base,
int size)
00056 {
00057
if (stor == NULL)
00058
avr_error (
"passed null ptr");
00059
00060
class_construct ((AvrClass *)stor);
00061
00062 stor->base = base;
00063 stor->size = size;
00064
00065 stor->data =
avr_new0 (uint8_t, size);
00066 }
00067
00068
00069
00070
00071
00072
void
00073 storage_destroy (
void *stor)
00074 {
00075
if (stor == NULL)
00076
return;
00077
00078
avr_free (((Storage *)stor)->data);
00079
class_destroy (stor);
00080 }
00081
00082
extern inline uint8_t
00083 storage_readb (Storage *stor,
int addr);
00084
00085
extern inline uint16_t
00086 storage_readw (Storage *stor,
int addr);
00087
00088
void
00089 storage_writeb (Storage *stor,
int addr, uint8_t val)
00090 {
00091
int _addr = addr - stor->base;
00092
00093
if (stor == NULL)
00094
avr_error (
"passed null ptr");
00095
00096
if ((_addr < 0) || (_addr >= stor->size))
00097
avr_error (
"address out of bounds: 0x%x", addr);
00098
00099 stor->data[_addr] = val;
00100 }
00101
00102
void
00103 storage_writew (Storage *stor,
int addr, uint16_t val)
00104 {
00105
int _addr = addr - stor->base;
00106
00107
if (stor == NULL)
00108
avr_error (
"passed null ptr");
00109
00110
if ((_addr < 0) || (_addr >= stor->size))
00111
avr_error (
"address out of bounds: 0x%x", addr);
00112
00113 stor->data[_addr] = (uint8_t) (val >> 8 & 0xff);
00114 stor->data[_addr + 1] = (uint8_t) (val & 0xff);
00115 }
00116
00117
int
00118 storage_get_size (Storage *stor)
00119 {
00120
return stor->size;
00121 }
00122
00123
int
00124 storage_get_base (Storage *stor)
00125 {
00126
return stor->base;
00127 }