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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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. */
31 #ifdef VALIDATE /* for running the validate suite */
32 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
38 #ifdef ABORTS /* the memory system will abort */
39 /* For the old test suite Abort between 32 Kbytes and 32 Mbytes
40 For the new test suite Abort between 8 Mbytes and 26 Mbytes */
41 /* #define LOWABORT 32 * 1024
42 #define HIGHABORT 32 * 1024 * 1024 */
43 #define LOWABORT 8 * 1024 * 1024
44 #define HIGHABORT 26 * 1024 * 1024
48 #define NUMPAGES 64 * 1024
49 #define PAGESIZE 64 * 1024
51 #define OFFSETBITS 0xffff
53 int SWI_vector_installed = FALSE;
55 /***************************************************************************\
56 * Get a Word from Virtual Memory, maybe allocating the page *
57 \***************************************************************************/
60 GetWord (ARMul_State * state, ARMword address, int check)
67 if (check && state->is_XScale)
68 XScale_check_memacc (state, &address, 0);
70 page = address >> PAGEBITS;
71 offset = (address & OFFSETBITS) >> 2;
72 pagetable = (ARMword **) state->MemDataPtr;
73 pageptr = *(pagetable + page);
77 pageptr = (ARMword *) malloc (PAGESIZE);
81 perror ("ARMulator can't allocate VM page");
85 *(pagetable + page) = pageptr;
88 return *(pageptr + offset);
91 /***************************************************************************\
92 * Put a Word into Virtual Memory, maybe allocating the page *
93 \***************************************************************************/
96 PutWord (ARMul_State * state, ARMword address, ARMword data, int check)
103 if (check && state->is_XScale)
104 XScale_check_memacc (state, &address, 1);
106 page = address >> PAGEBITS;
107 offset = (address & OFFSETBITS) >> 2;
108 pagetable = (ARMword **) state->MemDataPtr;
109 pageptr = *(pagetable + page);
113 pageptr = (ARMword *) malloc (PAGESIZE);
116 perror ("ARMulator can't allocate VM page");
120 *(pagetable + page) = pageptr;
124 SWI_vector_installed = TRUE;
126 *(pageptr + offset) = data;
129 /***************************************************************************\
130 * Initialise the memory interface *
131 \***************************************************************************/
134 ARMul_MemoryInit (ARMul_State * state, unsigned long initmemsize)
140 state->MemSize = initmemsize;
142 pagetable = (ARMword **) malloc (sizeof (ARMword *) * NUMPAGES);
144 if (pagetable == NULL)
147 for (page = 0; page < NUMPAGES; page++)
148 *(pagetable + page) = NULL;
150 state->MemDataPtr = (unsigned char *) pagetable;
152 ARMul_ConsolePrint (state, ", 4 Gb memory");
157 /***************************************************************************\
158 * Remove the memory interface *
159 \***************************************************************************/
162 ARMul_MemoryExit (ARMul_State * state)
168 pagetable = (ARMword **) state->MemDataPtr;
169 for (page = 0; page < NUMPAGES; page++)
171 pageptr = *(pagetable + page);
173 free ((char *) pageptr);
175 free ((char *) pagetable);
179 /***************************************************************************\
180 * ReLoad Instruction *
181 \***************************************************************************/
184 ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
187 if (address >= LOWABORT && address < HIGHABORT)
189 ARMul_PREFETCHABORT (address);
190 return ARMul_ABORTWORD;
198 if ((isize == 2) && (address & 0x2))
200 /* We return the next two halfwords: */
201 ARMword lo = GetWord (state, address, FALSE);
202 ARMword hi = GetWord (state, address + 4, FALSE);
204 if (state->bigendSig == HIGH)
205 return (lo << 16) | (hi >> 16);
207 return ((hi & 0xFFFF) << 16) | (lo >> 16);
210 return GetWord (state, address, TRUE);
213 /***************************************************************************\
214 * Load Instruction, Sequential Cycle *
215 \***************************************************************************/
217 ARMword ARMul_LoadInstrS (ARMul_State * state, ARMword address, ARMword isize)
222 if ((state->NumScycles & HOURGLASS_RATE) == 0)
228 return ARMul_ReLoadInstr (state, address, isize);
231 /***************************************************************************\
232 * Load Instruction, Non Sequential Cycle *
233 \***************************************************************************/
235 ARMword ARMul_LoadInstrN (ARMul_State * state, ARMword address, ARMword isize)
239 return ARMul_ReLoadInstr (state, address, isize);
242 /***************************************************************************\
243 * Read Word (but don't tell anyone!) *
244 \***************************************************************************/
246 ARMword ARMul_ReadWord (ARMul_State * state, ARMword address)
249 if (address >= LOWABORT && address < HIGHABORT)
251 ARMul_DATAABORT (address);
252 return ARMul_ABORTWORD;
260 return GetWord (state, address, TRUE);
263 /***************************************************************************\
264 * Load Word, Sequential Cycle *
265 \***************************************************************************/
267 ARMword ARMul_LoadWordS (ARMul_State * state, ARMword address)
271 return ARMul_ReadWord (state, address);
274 /***************************************************************************\
275 * Load Word, Non Sequential Cycle *
276 \***************************************************************************/
278 ARMword ARMul_LoadWordN (ARMul_State * state, ARMword address)
282 return ARMul_ReadWord (state, address);
285 /***************************************************************************\
286 * Load Halfword, (Non Sequential Cycle) *
287 \***************************************************************************/
289 ARMword ARMul_LoadHalfWord (ARMul_State * state, ARMword address)
291 ARMword temp, offset;
295 temp = ARMul_ReadWord (state, address);
296 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
298 return (temp >> offset) & 0xffff;
301 /***************************************************************************\
302 * Read Byte (but don't tell anyone!) *
303 \***************************************************************************/
305 ARMword ARMul_ReadByte (ARMul_State * state, ARMword address)
307 ARMword temp, offset;
309 temp = ARMul_ReadWord (state, address);
310 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
312 return (temp >> offset & 0xffL);
315 /***************************************************************************\
316 * Load Byte, (Non Sequential Cycle) *
317 \***************************************************************************/
319 ARMword ARMul_LoadByte (ARMul_State * state, ARMword address)
323 return ARMul_ReadByte (state, address);
326 /***************************************************************************\
327 * Write Word (but don't tell anyone!) *
328 \***************************************************************************/
331 ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
334 if (address >= LOWABORT && address < HIGHABORT)
336 ARMul_DATAABORT (address);
345 PutWord (state, address, data, TRUE);
348 /***************************************************************************\
349 * Store Word, Sequential Cycle *
350 \***************************************************************************/
353 ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
357 ARMul_WriteWord (state, address, data);
360 /***************************************************************************\
361 * Store Word, Non Sequential Cycle *
362 \***************************************************************************/
365 ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
369 ARMul_WriteWord (state, address, data);
372 /***************************************************************************\
373 * Store HalfWord, (Non Sequential Cycle) *
374 \***************************************************************************/
377 ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
379 ARMword temp, offset;
387 state->Emulate = FALSE;
389 (void) putc ((char) data, stderr); /* Write Char */
394 temp = ARMul_ReadWord (state, address);
395 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
397 PutWord (state, address,
398 (temp & ~(0xffffL << offset)) | ((data & 0xffffL) << offset),
402 /***************************************************************************\
403 * Write Byte (but don't tell anyone!) *
404 \***************************************************************************/
407 ARMul_WriteByte (ARMul_State * state, ARMword address, ARMword data)
409 ARMword temp, offset;
411 temp = ARMul_ReadWord (state, address);
412 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
414 PutWord (state, address,
415 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),
419 /***************************************************************************\
420 * Store Byte, (Non Sequential Cycle) *
421 \***************************************************************************/
424 ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
432 state->Emulate = FALSE;
434 (void) putc ((char) data, stderr); /* Write Char */
439 ARMul_WriteByte (state, address, data);
442 /***************************************************************************\
443 * Swap Word, (Two Non Sequential Cycles) *
444 \***************************************************************************/
446 ARMword ARMul_SwapWord (ARMul_State * state, ARMword address, ARMword data)
452 temp = ARMul_ReadWord (state, address);
456 PutWord (state, address, data, TRUE);
461 /***************************************************************************\
462 * Swap Byte, (Two Non Sequential Cycles) *
463 \***************************************************************************/
465 ARMword ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
469 temp = ARMul_LoadByte (state, address);
470 ARMul_StoreByte (state, address, data);
475 /***************************************************************************\
477 \***************************************************************************/
480 ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
482 state->NumIcycles += number;
486 /***************************************************************************\
488 \***************************************************************************/
491 ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
493 state->NumCcycles += number;
498 /* Read a byte. Do not check for alignment or access errors. */
501 ARMul_SafeReadByte (ARMul_State * state, ARMword address)
503 ARMword temp, offset;
505 temp = GetWord (state, address, FALSE);
506 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
508 return (temp >> offset & 0xffL);
512 ARMul_SafeWriteByte (ARMul_State * state, ARMword address, ARMword data)
514 ARMword temp, offset;
516 temp = GetWord (state, address, FALSE);
517 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
519 PutWord (state, address,
520 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),