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

Subversion Repositories eco32

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

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

powered by: WebSVN 2.1.0

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