1 /* armvirt.c -- ARMulator virtual memory interace: ARM6 Instruction Emulator.
2 Copyright (C) 1994 Advanced RISC Machines Ltd.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* This file contains a complete ARMulator memory model, modelling a
19 "virtual memory" system. A much simpler model can be found in armfast.c,
20 and that model goes faster too, but has a fixed amount of memory. This
21 model's memory has 64K pages, allocated on demand from a 64K entry page
22 table. The routines PutWord and GetWord implement this. Pages are never
23 freed as they might be needed again. A single area of memory may be
24 defined to generate aborts. */
29 #ifdef VALIDATE /* for running the validate suite */
30 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
36 #ifdef ABORTS /* the memory system will abort */
37 /* For the old test suite Abort between 32 Kbytes and 32 Mbytes
38 For the new test suite Abort between 8 Mbytes and 26 Mbytes */
39 /* #define LOWABORT 32 * 1024
40 #define HIGHABORT 32 * 1024 * 1024 */
41 #define LOWABORT 8 * 1024 * 1024
42 #define HIGHABORT 26 * 1024 * 1024
46 #define NUMPAGES 64 * 1024
47 #define PAGESIZE 64 * 1024
49 #define OFFSETBITS 0xffff
51 /***************************************************************************\
52 * Get a Word from Virtual Memory, maybe allocating the page *
53 \***************************************************************************/
56 GetWord (ARMul_State * state, ARMword address)
63 page = address >> PAGEBITS;
64 offset = (address & OFFSETBITS) >> 2;
65 pagetable = (ARMword **) state->MemDataPtr;
66 pageptr = *(pagetable + page);
70 pageptr = (ARMword *) malloc (PAGESIZE);
74 perror ("ARMulator can't allocate VM page");
78 *(pagetable + page) = pageptr;
81 return *(pageptr + offset);
84 /***************************************************************************\
85 * Put a Word into Virtual Memory, maybe allocating the page *
86 \***************************************************************************/
89 PutWord (ARMul_State * state, ARMword address, ARMword data)
96 page = address >> PAGEBITS;
97 offset = (address & OFFSETBITS) >> 2;
98 pagetable = (ARMword **) state->MemDataPtr;
99 pageptr = *(pagetable + page);
103 pageptr = (ARMword *) malloc (PAGESIZE);
106 perror ("ARMulator can't allocate VM page");
110 *(pagetable + page) = pageptr;
113 *(pageptr + offset) = data;
116 /***************************************************************************\
117 * Initialise the memory interface *
118 \***************************************************************************/
121 ARMul_MemoryInit (ARMul_State * state, unsigned long initmemsize)
127 state->MemSize = initmemsize;
129 pagetable = (ARMword **) malloc (sizeof (ARMword) * NUMPAGES);
131 if (pagetable == NULL)
134 for (page = 0; page < NUMPAGES; page++)
135 *(pagetable + page) = NULL;
137 state->MemDataPtr = (unsigned char *) pagetable;
139 ARMul_ConsolePrint (state, ", 4 Gb memory");
144 /***************************************************************************\
145 * Remove the memory interface *
146 \***************************************************************************/
149 ARMul_MemoryExit (ARMul_State * state)
155 pagetable = (ARMword **) state->MemDataPtr;
156 for (page = 0; page < NUMPAGES; page++)
158 pageptr = *(pagetable + page);
160 free ((char *) pageptr);
162 free ((char *) pagetable);
166 /***************************************************************************\
167 * ReLoad Instruction *
168 \***************************************************************************/
171 ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
174 if (address >= LOWABORT && address < HIGHABORT)
176 ARMul_PREFETCHABORT (address);
177 return ARMul_ABORTWORD;
185 if ((isize == 2) && (address & 0x2))
187 /* We return the next two halfwords: */
188 ARMword lo = GetWord (state, address);
189 ARMword hi = GetWord (state, address + 4);
191 if (state->bigendSig == HIGH)
192 return (lo << 16) | (hi >> 16);
194 return ((hi & 0xFFFF) << 16) | (lo >> 16);
197 return GetWord (state, address);
200 /***************************************************************************\
201 * Load Instruction, Sequential Cycle *
202 \***************************************************************************/
204 ARMword ARMul_LoadInstrS (ARMul_State * state, ARMword address, ARMword isize)
209 if ((state->NumScycles & HOURGLASS_RATE) == 0)
215 return ARMul_ReLoadInstr (state, address, isize);
218 /***************************************************************************\
219 * Load Instruction, Non Sequential Cycle *
220 \***************************************************************************/
222 ARMword ARMul_LoadInstrN (ARMul_State * state, ARMword address, ARMword isize)
226 return ARMul_ReLoadInstr (state, address, isize);
229 /***************************************************************************\
230 * Read Word (but don't tell anyone!) *
231 \***************************************************************************/
233 ARMword ARMul_ReadWord (ARMul_State * state, ARMword address)
236 if (address >= LOWABORT && address < HIGHABORT)
238 ARMul_DATAABORT (address);
239 return ARMul_ABORTWORD;
247 return GetWord (state, address);
250 /***************************************************************************\
251 * Load Word, Sequential Cycle *
252 \***************************************************************************/
254 ARMword ARMul_LoadWordS (ARMul_State * state, ARMword address)
258 return ARMul_ReadWord (state, address);
261 /***************************************************************************\
262 * Load Word, Non Sequential Cycle *
263 \***************************************************************************/
265 ARMword ARMul_LoadWordN (ARMul_State * state, ARMword address)
269 return ARMul_ReadWord (state, address);
272 /***************************************************************************\
273 * Load Halfword, (Non Sequential Cycle) *
274 \***************************************************************************/
276 ARMword ARMul_LoadHalfWord (ARMul_State * state, ARMword address)
278 ARMword temp, offset;
282 temp = ARMul_ReadWord (state, address);
283 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
285 return (temp >> offset) & 0xffff;
288 /***************************************************************************\
289 * Read Byte (but don't tell anyone!) *
290 \***************************************************************************/
292 ARMword ARMul_ReadByte (ARMul_State * state, ARMword address)
294 ARMword temp, offset;
296 temp = ARMul_ReadWord (state, address);
297 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
299 return (temp >> offset & 0xffL);
302 /***************************************************************************\
303 * Load Byte, (Non Sequential Cycle) *
304 \***************************************************************************/
306 ARMword ARMul_LoadByte (ARMul_State * state, ARMword address)
310 return ARMul_ReadByte (state, address);
313 /***************************************************************************\
314 * Write Word (but don't tell anyone!) *
315 \***************************************************************************/
318 ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
321 if (address >= LOWABORT && address < HIGHABORT)
323 ARMul_DATAABORT (address);
332 PutWord (state, address, data);
335 /***************************************************************************\
336 * Store Word, Sequential Cycle *
337 \***************************************************************************/
340 ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
344 ARMul_WriteWord (state, address, data);
347 /***************************************************************************\
348 * Store Word, Non Sequential Cycle *
349 \***************************************************************************/
352 ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
356 ARMul_WriteWord (state, address, data);
359 /***************************************************************************\
360 * Store HalfWord, (Non Sequential Cycle) *
361 \***************************************************************************/
364 ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
366 ARMword temp, offset;
374 state->Emulate = FALSE;
376 (void) putc ((char) data, stderr); /* Write Char */
381 temp = ARMul_ReadWord (state, address);
382 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
384 PutWord (state, address,
385 (temp & ~(0xffffL << offset)) | ((data & 0xffffL) << offset));
388 /***************************************************************************\
389 * Write Byte (but don't tell anyone!) *
390 \***************************************************************************/
393 ARMul_WriteByte (ARMul_State * state, ARMword address, ARMword data)
395 ARMword temp, offset;
397 temp = ARMul_ReadWord (state, address);
398 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
400 PutWord (state, address,
401 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset));
404 /***************************************************************************\
405 * Store Byte, (Non Sequential Cycle) *
406 \***************************************************************************/
409 ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
417 state->Emulate = FALSE;
419 (void) putc ((char) data, stderr); /* Write Char */
424 ARMul_WriteByte (state, address, data);
427 /***************************************************************************\
428 * Swap Word, (Two Non Sequential Cycles) *
429 \***************************************************************************/
431 ARMword ARMul_SwapWord (ARMul_State * state, ARMword address, ARMword data)
437 temp = ARMul_ReadWord (state, address);
441 PutWord (state, address, data);
446 /***************************************************************************\
447 * Swap Byte, (Two Non Sequential Cycles) *
448 \***************************************************************************/
450 ARMword ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
454 temp = ARMul_LoadByte (state, address);
455 ARMul_StoreByte (state, address, data);
460 /***************************************************************************\
462 \***************************************************************************/
465 ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address)
467 state->NumIcycles += number;
471 /***************************************************************************\
473 \***************************************************************************/
476 ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address)
478 state->NumCcycles += number;