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: #include "hw.h"
26: #include "mips.h"
27: #include "nvram.h"
28:
29: typedef enum
30: {
31: none = 0,
32: readmode,
33: writemode,
34: } nvram_open_mode;
35:
36: struct ds1225y_t
37: {
38: target_phys_addr_t mem_base;
39: uint32_t capacity;
40: const char *filename;
41: QEMUFile *file;
42: nvram_open_mode open_mode;
43: };
44:
45: static int ds1225y_set_to_mode(ds1225y_t *NVRAM, nvram_open_mode mode, const char *filemode)
46: {
47: if (NVRAM->open_mode != mode)
48: {
49: if (NVRAM->file)
50: qemu_fclose(NVRAM->file);
51: NVRAM->file = qemu_fopen(NVRAM->filename, filemode);
52: NVRAM->open_mode = mode;
53: }
54: return (NVRAM->file != NULL);
55: }
56:
57: static uint32_t nvram_readb (void *opaque, target_phys_addr_t addr)
58: {
59: ds1225y_t *NVRAM = opaque;
60: int64_t pos;
61:
62: pos = addr - NVRAM->mem_base;
63: if (addr >= NVRAM->capacity)
64: addr -= NVRAM->capacity;
65:
66: if (!ds1225y_set_to_mode(NVRAM, readmode, "rb"))
67: return 0;
68: qemu_fseek(NVRAM->file, pos, SEEK_SET);
69: return (uint32_t)qemu_get_byte(NVRAM->file);
70: }
71:
72: static void nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
73: {
74: ds1225y_t *NVRAM = opaque;
75: int64_t pos;
76:
77: pos = addr - NVRAM->mem_base;
78: if (ds1225y_set_to_mode(NVRAM, writemode, "wb"))
79: {
80: qemu_fseek(NVRAM->file, pos, SEEK_SET);
81: qemu_put_byte(NVRAM->file, (int)value);
82: }
83: }
84:
85: static CPUReadMemoryFunc *nvram_read[] = {
86: &nvram_readb,
87: NULL,
88: NULL,
89: };
90:
91: static CPUWriteMemoryFunc *nvram_write[] = {
92: &nvram_writeb,
93: NULL,
94: NULL,
95: };
96:
97: static CPUWriteMemoryFunc *nvram_none[] = {
98: NULL,
99: NULL,
100: NULL,
101: };
102:
103:
104: ds1225y_t *ds1225y_init(target_phys_addr_t mem_base, const char *filename)
105: {
106: ds1225y_t *s;
107: int mem_index1, mem_index2;
108:
109: s = qemu_mallocz(sizeof(ds1225y_t));
110: if (!s)
111: return NULL;
112: s->mem_base = mem_base;
113: s->capacity = 0x2000;
114: s->filename = filename;
115:
116:
117: mem_index1 = cpu_register_io_memory(0, nvram_read, nvram_write, s);
118: cpu_register_physical_memory(mem_base, s->capacity, mem_index1);
119:
120: mem_index2 = cpu_register_io_memory(0, nvram_read, nvram_none, s);
121: cpu_register_physical_memory(mem_base + s->capacity, s->capacity, mem_index2);
122: return s;
123: }