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

Subversion Repositories eco32

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

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

powered by: WebSVN 2.1.0

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