OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [sim/] [memory.c] - Blame information for rev 219

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 hellwig
/*
2
 * memory.c -- physical memory simulation
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <setjmp.h>
10
 
11
#include "common.h"
12
#include "console.h"
13
#include "error.h"
14
#include "except.h"
15
#include "cpu.h"
16
#include "memory.h"
17
#include "timer.h"
18
#include "dspkbd.h"
19
#include "term.h"
20
#include "disk.h"
21
#include "output.h"
22 25 hellwig
#include "shutdown.h"
23 8 hellwig
#include "graph.h"
24
 
25
 
26
static Byte *rom;
27
static Byte *mem;
28
static unsigned int memSize;
29
static FILE *romImage;
30
static unsigned int romSize;
31
static FILE *progImage;
32
static unsigned int progSize;
33 91 hellwig
static unsigned int progAddr;
34 8 hellwig
 
35
 
36
Word memoryReadWord(Word pAddr) {
37
  Word data;
38
 
39
  if (pAddr <= memSize - 4) {
40
    data = ((Word) *(mem + pAddr + 0)) << 24 |
41
           ((Word) *(mem + pAddr + 1)) << 16 |
42
           ((Word) *(mem + pAddr + 2)) <<  8 |
43
           ((Word) *(mem + pAddr + 3)) <<  0;
44
    return data;
45
  }
46
  if (pAddr >= ROM_BASE &&
47
      pAddr <= ROM_BASE + ROM_SIZE - 4) {
48
    data = ((Word) *(rom + (pAddr - ROM_BASE) + 0)) << 24 |
49
           ((Word) *(rom + (pAddr - ROM_BASE) + 1)) << 16 |
50
           ((Word) *(rom + (pAddr - ROM_BASE) + 2)) <<  8 |
51
           ((Word) *(rom + (pAddr - ROM_BASE) + 3)) <<  0;
52
    return data;
53
  }
54
  if ((pAddr & IO_DEV_MASK) == TIMER_BASE) {
55
    data = timerRead(pAddr & IO_REG_MASK);
56
    return data;
57
  }
58
  if ((pAddr & IO_DEV_MASK) == DISPLAY_BASE) {
59
    data = displayRead(pAddr & IO_REG_MASK);
60
    return data;
61
  }
62
  if ((pAddr & IO_DEV_MASK) == KEYBOARD_BASE) {
63
    data = keyboardRead(pAddr & IO_REG_MASK);
64
    return data;
65
  }
66
  if ((pAddr & IO_DEV_MASK) == TERM_BASE) {
67
    data = termRead(pAddr & IO_REG_MASK);
68
    return data;
69
  }
70
  if ((pAddr & IO_DEV_MASK) == DISK_BASE) {
71
    data = diskRead(pAddr & IO_REG_MASK);
72
    return data;
73
  }
74
  if ((pAddr & IO_DEV_MASK) == OUTPUT_BASE) {
75
    data = outputRead(pAddr & IO_REG_MASK);
76
    return data;
77
  }
78 25 hellwig
  if ((pAddr & IO_DEV_MASK) == SHUTDOWN_BASE) {
79
    data = shutdownRead(pAddr & IO_REG_MASK);
80
    return data;
81
  }
82 8 hellwig
  if ((pAddr & IO_DEV_MASK) >= GRAPH_BASE) {
83
    data = graphRead(pAddr & IO_GRAPH_MASK);
84
    return data;
85
  }
86
  /* throw bus timeout exception */
87
  throwException(EXC_BUS_TIMEOUT);
88
  /* not reached */
89
  data = 0;
90
  return data;
91
}
92
 
93
 
94
Half memoryReadHalf(Word pAddr) {
95
  Half data;
96
 
97
  if (pAddr <= memSize - 2) {
98
    data = ((Half) *(mem + pAddr + 0)) << 8 |
99
           ((Half) *(mem + pAddr + 1)) << 0;
100
    return data;
101
  }
102
  if (pAddr >= ROM_BASE &&
103
      pAddr <= ROM_BASE + ROM_SIZE - 2) {
104
    data = ((Half) *(rom + (pAddr - ROM_BASE) + 0)) << 8 |
105
           ((Half) *(rom + (pAddr - ROM_BASE) + 1)) << 0;
106
    return data;
107
  }
108
  /* throw bus timeout exception */
109
  throwException(EXC_BUS_TIMEOUT);
110
  /* not reached */
111
  data = 0;
112
  return data;
113
}
114
 
115
 
116
Byte memoryReadByte(Word pAddr) {
117
  Byte data;
118
 
119
  if (pAddr <= memSize - 1) {
120
    data = ((Byte) *(mem + pAddr + 0)) << 0;
121
    return data;
122
  }
123
  if (pAddr >= ROM_BASE &&
124
      pAddr <= ROM_BASE + ROM_SIZE - 1) {
125
    data = ((Byte) *(rom + (pAddr - ROM_BASE) + 0)) << 0;
126
    return data;
127
  }
128
  /* throw bus timeout exception */
129
  throwException(EXC_BUS_TIMEOUT);
130
  /* not reached */
131
  data = 0;
132
  return data;
133
}
134
 
135
 
136
void memoryWriteWord(Word pAddr, Word data) {
137
  if (pAddr <= memSize - 4) {
138
    *(mem + pAddr + 0) = (Byte) (data >> 24);
139
    *(mem + pAddr + 1) = (Byte) (data >> 16);
140
    *(mem + pAddr + 2) = (Byte) (data >>  8);
141
    *(mem + pAddr + 3) = (Byte) (data >>  0);
142
    return;
143
  }
144
  if ((pAddr & IO_DEV_MASK) == TIMER_BASE) {
145
    timerWrite(pAddr & IO_REG_MASK, data);
146
    return;
147
  }
148
  if ((pAddr & IO_DEV_MASK) == DISPLAY_BASE) {
149
    displayWrite(pAddr & IO_REG_MASK, data);
150
    return;
151
  }
152
  if ((pAddr & IO_DEV_MASK) == KEYBOARD_BASE) {
153
    keyboardWrite(pAddr & IO_REG_MASK, data);
154
    return;
155
  }
156
  if ((pAddr & IO_DEV_MASK) == TERM_BASE) {
157
    termWrite(pAddr & IO_REG_MASK, data);
158
    return;
159
  }
160
  if ((pAddr & IO_DEV_MASK) == DISK_BASE) {
161
    diskWrite(pAddr & IO_REG_MASK, data);
162
    return;
163
  }
164
  if ((pAddr & IO_DEV_MASK) == OUTPUT_BASE) {
165
    outputWrite(pAddr & IO_REG_MASK, data);
166
    return;
167
  }
168 25 hellwig
  if ((pAddr & IO_DEV_MASK) == SHUTDOWN_BASE) {
169
    shutdownWrite(pAddr & IO_REG_MASK, data);
170
    return;
171
  }
172 8 hellwig
  if ((pAddr & IO_DEV_MASK) >= GRAPH_BASE) {
173
    graphWrite(pAddr & IO_GRAPH_MASK, data);
174
    return;
175
  }
176
  /* throw bus timeout exception */
177
  throwException(EXC_BUS_TIMEOUT);
178
}
179
 
180
 
181
void memoryWriteHalf(Word pAddr, Half data) {
182
  if (pAddr <= memSize - 2) {
183
    *(mem + pAddr + 0) = (Byte) (data >> 8);
184
    *(mem + pAddr + 1) = (Byte) (data >> 0);
185
    return;
186
  }
187
  /* throw bus timeout exception */
188
  throwException(EXC_BUS_TIMEOUT);
189
}
190
 
191
 
192
void memoryWriteByte(Word pAddr, Byte data) {
193
  if (pAddr <= memSize - 1) {
194
    *(mem + pAddr + 0) = (Byte) (data >> 0);
195
    return;
196
  }
197
  /* throw bus timeout exception */
198
  throwException(EXC_BUS_TIMEOUT);
199
}
200
 
201
 
202
void memoryReset(void) {
203
  unsigned int i;
204
 
205
  cPrintf("Resetting Memory...\n");
206
  for (i = 0; i < memSize; i++) {
207
    mem[i] = rand();
208
  }
209
  cPrintf("%6d MB RAM installed", memSize / M);
210
  if (progImage != NULL) {
211
    fseek(progImage, 0, SEEK_SET);
212 91 hellwig
    if (fread(mem + progAddr, progSize, 1, progImage) != 1) {
213 8 hellwig
      error("cannot read program image file");
214
    }
215
    cPrintf(", %d bytes loaded", progSize);
216
  }
217
  cPrintf(".\n");
218
  for (i = 0; i < ROM_SIZE; i++) {
219
    rom[i] = 0xFF;
220
  }
221
  if (romImage != NULL) {
222
    fseek(romImage, 0, SEEK_SET);
223
    if (fread(rom, romSize, 1, romImage) != 1) {
224
      error("cannot read ROM image file");
225
    }
226
    cPrintf("%6d KB ROM installed, %d bytes programmed.\n",
227
            ROM_SIZE / K, romSize);
228
  }
229
}
230
 
231
 
232
void memoryInit(unsigned int memorySize,
233
                char *progImageName,
234 91 hellwig
                unsigned int loadAddr,
235 8 hellwig
                char *romImageName) {
236
  /* allocate RAM */
237
  memSize = memorySize;
238
  mem = malloc(memSize);
239
  if (mem == NULL) {
240
    error("cannot allocate RAM");
241
  }
242
  /* possibly load program image */
243
  if (progImageName == NULL) {
244
    /* no program to load */
245
    progImage = NULL;
246
  } else {
247
    /* load program */
248
    progImage = fopen(progImageName, "rb");
249
    if (progImage == NULL) {
250
      error("cannot open program file '%s'", progImageName);
251
    }
252
    fseek(progImage, 0, SEEK_END);
253
    progSize = ftell(progImage);
254 91 hellwig
    progAddr = loadAddr;
255
    if (progAddr + progSize > memSize) {
256
      error("program file or load address too big");
257 8 hellwig
    }
258
    /* do actual loading of image in memoryReset() */
259
  }
260
  /* allocate ROM */
261
  rom = malloc(ROM_SIZE);
262
  if (rom == NULL) {
263
    error("cannot allocate ROM");
264
  }
265
  /* possibly load ROM image */
266
  if (romImageName == NULL) {
267
    /* no ROM to plug in */
268
    romImage = NULL;
269
  } else {
270
    /* plug in ROM */
271
    romImage = fopen(romImageName, "rb");
272
    if (romImage == NULL) {
273
      error("cannot open ROM image '%s'", romImageName);
274
    }
275
    fseek(romImage, 0, SEEK_END);
276
    romSize = ftell(romImage);
277
    if (romSize > ROM_SIZE) {
278
      error("ROM image too big");
279
    }
280
    /* do actual loading of image in memoryReset() */
281
  }
282
  memoryReset();
283
}
284
 
285
 
286
void memoryExit(void) {
287
  free(mem);
288
  free(rom);
289
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.