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 3 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, see <http://www.gnu.org/licenses/>. */
17 /* This file contains a complete ARMulator memory model, modelling a
18 "virtual memory" system. A much simpler model can be found in armfast.c,
19 and that model goes faster too, but has a fixed amount of memory. This
20 model's memory has 64K pages, allocated on demand from a 64K entry page
21 table. The routines PutWord and GetWord implement this. Pages are never
22 freed as they might be needed again. A single area of memory may be
23 defined to generate aborts. */
30 #ifdef VALIDATE /* for running the validate suite */
31 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
37 #ifdef ABORTS /* the memory system will abort */
38 /* For the old test suite Abort between 32 Kbytes and 32 Mbytes
39 For the new test suite Abort between 8 Mbytes and 26 Mbytes */
40 /* #define LOWABORT 32 * 1024
41 #define HIGHABORT 32 * 1024 * 1024 */
42 #define LOWABORT 8 * 1024 * 1024
43 #define HIGHABORT 26 * 1024 * 1024
47 #define NUMPAGES 64 * 1024
48 #define PAGESIZE 64 * 1024
50 #define OFFSETBITS 0xffff
52 int SWI_vector_installed = FALSE;
54 /***************************************************************************\
55 * Get a Word from Virtual Memory, maybe allocating the page *
56 \***************************************************************************/
59 GetWord (ARMul_State * state, ARMword address, int check)
66 if (check && state->is_XScale)
67 XScale_check_memacc (state, &address, 0);
69 page = address >> PAGEBITS;
70 offset = (address & OFFSETBITS) >> 2;
71 pagetable = (ARMword **) state->MemDataPtr;
72 pageptr = *(pagetable + page);
76 pageptr = (ARMword *) malloc (PAGESIZE);
80 perror ("ARMulator can't allocate VM page");
84 *(pagetable + page) = pageptr;
87 return *(pageptr + offset);
90 /***************************************************************************\
91 * Put a Word into Virtual Memory, maybe allocating the page *
92 \***************************************************************************/
95 PutWord (ARMul_State * state, ARMword address, ARMword data, int check)
102 if (check && state->is_XScale)
103 XScale_check_memacc (state, &address, 1);
105 page = address >> PAGEBITS;
106 offset = (address & OFFSETBITS) >> 2;
107 pagetable = (ARMword **) state->MemDataPtr;
108 pageptr = *(pagetable + page);
112 pageptr = (ARMword *) malloc (PAGESIZE);
115 perror ("ARMulator can't allocate VM page");
119 *(pagetable + page) = pageptr;
123 SWI_vector_installed = TRUE;
125 *(pageptr + offset) = data;
128 /***************************************************************************\
129 * Initialise the memory interface *
130 \***************************************************************************/
133 ARMul_MemoryInit (ARMul_State * state, unsigned long initmemsize)
139 state->MemSize = initmemsize;
141 pagetable = (ARMword **) malloc (sizeof (ARMword *) * NUMPAGES);
143 if (pagetable == NULL)
146 for (page = 0; page < NUMPAGES; page++)
147 *(pagetable + page) = NULL;
149 state->MemDataPtr = (unsigned char *) pagetable;
151 ARMul_ConsolePrint (state, ", 4 Gb memory");
156 /***************************************************************************\
157 * Remove the memory interface *
158 \***************************************************************************/
161 ARMul_MemoryExit (ARMul_State * state)
167 pagetable = (ARMword **) state->MemDataPtr;
168 for (page = 0; page < NUMPAGES; page++)
170 pageptr = *(pagetable + page);
172 free ((char *) pageptr);
174 free ((char *) pagetable);
178 /***************************************************************************\
179 * ReLoad Instruction *
180 \***************************************************************************/
183 ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
186 if (address >= LOWABORT && address < HIGHABORT)
188 ARMul_PREFETCHABORT (address);
189 return ARMul_ABORTWORD;
197 if ((isize == 2) && (address & 0x2))
199 /* We return the next two halfwords: */
200 ARMword lo = GetWord (state, address, FALSE);
201 ARMword hi = GetWord (state, address + 4, FALSE);
203 if (state->bigendSig == HIGH)
204 return (lo << 16) | (hi >> 16);
206 return ((hi & 0xFFFF) << 16) | (lo >> 16);
209 return GetWord (state, address, TRUE);
212 /***************************************************************************\
213 * Load Instruction, Sequential Cycle *
214 \***************************************************************************/
216 ARMword ARMul_LoadInstrS (ARMul_State * state, ARMword address, ARMword isize)
221 if ((state->NumScycles & HOURGLASS_RATE) == 0)
227 return ARMul_ReLoadInstr (state, address, isize);
230 /***************************************************************************\
231 * Load Instruction, Non Sequential Cycle *
232 \***************************************************************************/
234 ARMword ARMul_LoadInstrN (ARMul_State * state, ARMword address, ARMword isize)
238 return ARMul_ReLoadInstr (state, address, isize);
241 /***************************************************************************\
242 * Read Word (but don't tell anyone!) *
243 \***************************************************************************/
245 ARMword ARMul_ReadWord (ARMul_State * state, ARMword address)
248 if (address >= LOWABORT && address < HIGHABORT)
250 ARMul_DATAABORT (address);
251 return ARMul_ABORTWORD;
259 return GetWord (state, address, TRUE);
262 /***************************************************************************\
263 * Load Word, Sequential Cycle *
264 \***************************************************************************/
266 ARMword ARMul_LoadWordS (ARMul_State * state, ARMword address)
270 return ARMul_ReadWord (state, address);
273 /***************************************************************************\
274 * Load Word, Non Sequential Cycle *
275 \***************************************************************************/
277 ARMword ARMul_LoadWordN (ARMul_State * state, ARMword address)
281 return ARMul_ReadWord (state, address);
284 /***************************************************************************\
285 * Load Halfword, (Non Sequential Cycle) *
286 \***************************************************************************/
288 ARMword ARMul_LoadHalfWord (ARMul_State * state, ARMword address)
290 ARMword temp, offset;
294 temp = ARMul_ReadWord (state, address);
295 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
297 return (temp >> offset) & 0xffff;
300 /***************************************************************************\
301 * Read Byte (but don't tell anyone!) *
302 \***************************************************************************/
304 ARMword ARMul_ReadByte (ARMul_State * state, ARMword address)
306 ARMword temp, offset;
308 temp = ARMul_ReadWord (state, address);
309 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
311 return (temp >> offset & 0xffL);
314 /***************************************************************************\
315 * Load Byte, (Non Sequential Cycle) *
316 \***************************************************************************/
318 ARMword ARMul_LoadByte (ARMul_State * state, ARMword address)
322 return ARMul_ReadByte (state, address);
325 /***************************************************************************\
326 * Write Word (but don't tell anyone!) *
327 \***************************************************************************/
330 ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
333 if (address >= LOWABORT && address < HIGHABORT)
335 ARMul_DATAABORT (address);
344 PutWord (state, address, data, TRUE);
347 /***************************************************************************\
348 * Store Word, Sequential Cycle *
349 \***************************************************************************/
352 ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
356 ARMul_WriteWord (state, address, data);
359 /***************************************************************************\
360 * Store Word, Non Sequential Cycle *
361 \***************************************************************************/
364 ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
368 ARMul_WriteWord (state, address, data);
371 /***************************************************************************\
372 * Store HalfWord, (Non Sequential Cycle) *
373 \***************************************************************************/
376 ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
378 ARMword temp, offset;
386 state->Emulate = FALSE;
388 (void) putc ((char) data, stderr); /* Write Char */
393 temp = ARMul_ReadWord (state, address);
394 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
396 PutWord (state, address,
397 (temp & ~(0xffffL << offset)) | ((data & 0xffffL) << offset),
401 /***************************************************************************\
402 * Write Byte (but don't tell anyone!) *
403 \***************************************************************************/
406 ARMul_WriteByte (ARMul_State * state, ARMword address, ARMword data)
408 ARMword temp, offset;
410 temp = ARMul_ReadWord (state, address);
411 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
413 PutWord (state, address,
414 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),
418 /***************************************************************************\
419 * Store Byte, (Non Sequential Cycle) *
420 \***************************************************************************/
423 ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
431 state->Emulate = FALSE;
433 (void) putc ((char) data, stderr); /* Write Char */
438 ARMul_WriteByte (state, address, data);
441 /***************************************************************************\
442 * Swap Word, (Two Non Sequential Cycles) *
443 \***************************************************************************/
445 ARMword ARMul_SwapWord (ARMul_State * state, ARMword address, ARMword data)
451 temp = ARMul_ReadWord (state, address);
455 PutWord (state, address, data, TRUE);
460 /***************************************************************************\
461 * Swap Byte, (Two Non Sequential Cycles) *
462 \***************************************************************************/
464 ARMword ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
468 temp = ARMul_LoadByte (state, address);
469 ARMul_StoreByte (state, address, data);
474 /***************************************************************************\
476 \***************************************************************************/
479 ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
481 state->NumIcycles += number;
485 /***************************************************************************\
487 \***************************************************************************/
490 ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
492 state->NumCcycles += number;
497 /* Read a byte. Do not check for alignment or access errors. */
500 ARMul_SafeReadByte (ARMul_State * state, ARMword address)
502 ARMword temp, offset;
504 temp = GetWord (state, address, FALSE);
505 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
507 return (temp >> offset & 0xffL);
511 ARMul_SafeWriteByte (ARMul_State * state, ARMword address, ARMword data)
513 ARMword temp, offset;
515 temp = GetWord (state, address, FALSE);
516 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
518 PutWord (state, address,
519 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),