gdb/
[binutils-gdb.git] / sim / arm / armvirt.c
blob969085d24f4da16d087653ddc41cabf6ee8a28e6
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. */
26 #include "armopts.h"
27 #include "armos.h"
28 #include "armdefs.h"
29 #include "ansidecl.h"
31 #ifdef VALIDATE /* for running the validate suite */
32 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
33 #define ABORTS 1
34 #endif
36 /* #define ABORTS */
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
46 #endif
48 #define NUMPAGES 64 * 1024
49 #define PAGESIZE 64 * 1024
50 #define PAGEBITS 16
51 #define OFFSETBITS 0xffff
53 int SWI_vector_installed = FALSE;
55 /***************************************************************************\
56 * Get a Word from Virtual Memory, maybe allocating the page *
57 \***************************************************************************/
59 static ARMword
60 GetWord (ARMul_State * state, ARMword address, int check)
62 ARMword page;
63 ARMword offset;
64 ARMword **pagetable;
65 ARMword *pageptr;
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);
75 if (pageptr == NULL)
77 pageptr = (ARMword *) malloc (PAGESIZE);
79 if (pageptr == NULL)
81 perror ("ARMulator can't allocate VM page");
82 exit (12);
85 *(pagetable + page) = pageptr;
88 return *(pageptr + offset);
91 /***************************************************************************\
92 * Put a Word into Virtual Memory, maybe allocating the page *
93 \***************************************************************************/
95 static void
96 PutWord (ARMul_State * state, ARMword address, ARMword data, int check)
98 ARMword page;
99 ARMword offset;
100 ARMword **pagetable;
101 ARMword *pageptr;
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);
111 if (pageptr == NULL)
113 pageptr = (ARMword *) malloc (PAGESIZE);
114 if (pageptr == NULL)
116 perror ("ARMulator can't allocate VM page");
117 exit (13);
120 *(pagetable + page) = pageptr;
123 if (address == 0x8)
124 SWI_vector_installed = TRUE;
126 *(pageptr + offset) = data;
129 /***************************************************************************\
130 * Initialise the memory interface *
131 \***************************************************************************/
133 unsigned
134 ARMul_MemoryInit (ARMul_State * state, unsigned long initmemsize)
136 ARMword **pagetable;
137 unsigned page;
139 if (initmemsize)
140 state->MemSize = initmemsize;
142 pagetable = (ARMword **) malloc (sizeof (ARMword *) * NUMPAGES);
144 if (pagetable == NULL)
145 return FALSE;
147 for (page = 0; page < NUMPAGES; page++)
148 *(pagetable + page) = NULL;
150 state->MemDataPtr = (unsigned char *) pagetable;
152 ARMul_ConsolePrint (state, ", 4 Gb memory");
154 return TRUE;
157 /***************************************************************************\
158 * Remove the memory interface *
159 \***************************************************************************/
161 void
162 ARMul_MemoryExit (ARMul_State * state)
164 ARMword page;
165 ARMword **pagetable;
166 ARMword *pageptr;
168 pagetable = (ARMword **) state->MemDataPtr;
169 for (page = 0; page < NUMPAGES; page++)
171 pageptr = *(pagetable + page);
172 if (pageptr != NULL)
173 free ((char *) pageptr);
175 free ((char *) pagetable);
176 return;
179 /***************************************************************************\
180 * ReLoad Instruction *
181 \***************************************************************************/
183 ARMword
184 ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
186 #ifdef ABORTS
187 if (address >= LOWABORT && address < HIGHABORT)
189 ARMul_PREFETCHABORT (address);
190 return ARMul_ABORTWORD;
192 else
194 ARMul_CLEARABORT;
196 #endif
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);
206 else
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)
219 state->NumScycles++;
221 #ifdef HOURGLASS
222 if ((state->NumScycles & HOURGLASS_RATE) == 0)
224 HOURGLASS;
226 #endif
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)
237 state->NumNcycles++;
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)
248 #ifdef ABORTS
249 if (address >= LOWABORT && address < HIGHABORT)
251 ARMul_DATAABORT (address);
252 return ARMul_ABORTWORD;
254 else
256 ARMul_CLEARABORT;
258 #endif
260 return GetWord (state, address, TRUE);
263 /***************************************************************************\
264 * Load Word, Sequential Cycle *
265 \***************************************************************************/
267 ARMword ARMul_LoadWordS (ARMul_State * state, ARMword address)
269 state->NumScycles++;
271 return ARMul_ReadWord (state, address);
274 /***************************************************************************\
275 * Load Word, Non Sequential Cycle *
276 \***************************************************************************/
278 ARMword ARMul_LoadWordN (ARMul_State * state, ARMword address)
280 state->NumNcycles++;
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;
293 state->NumNcycles++;
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)
321 state->NumNcycles++;
323 return ARMul_ReadByte (state, address);
326 /***************************************************************************\
327 * Write Word (but don't tell anyone!) *
328 \***************************************************************************/
330 void
331 ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
333 #ifdef ABORTS
334 if (address >= LOWABORT && address < HIGHABORT)
336 ARMul_DATAABORT (address);
337 return;
339 else
341 ARMul_CLEARABORT;
343 #endif
345 PutWord (state, address, data, TRUE);
348 /***************************************************************************\
349 * Store Word, Sequential Cycle *
350 \***************************************************************************/
352 void
353 ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
355 state->NumScycles++;
357 ARMul_WriteWord (state, address, data);
360 /***************************************************************************\
361 * Store Word, Non Sequential Cycle *
362 \***************************************************************************/
364 void
365 ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
367 state->NumNcycles++;
369 ARMul_WriteWord (state, address, data);
372 /***************************************************************************\
373 * Store HalfWord, (Non Sequential Cycle) *
374 \***************************************************************************/
376 void
377 ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
379 ARMword temp, offset;
381 state->NumNcycles++;
383 #ifdef VALIDATE
384 if (address == TUBE)
386 if (data == 4)
387 state->Emulate = FALSE;
388 else
389 (void) putc ((char) data, stderr); /* Write Char */
390 return;
392 #endif
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),
399 TRUE);
402 /***************************************************************************\
403 * Write Byte (but don't tell anyone!) *
404 \***************************************************************************/
406 void
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),
416 TRUE);
419 /***************************************************************************\
420 * Store Byte, (Non Sequential Cycle) *
421 \***************************************************************************/
423 void
424 ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
426 state->NumNcycles++;
428 #ifdef VALIDATE
429 if (address == TUBE)
431 if (data == 4)
432 state->Emulate = FALSE;
433 else
434 (void) putc ((char) data, stderr); /* Write Char */
435 return;
437 #endif
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)
448 ARMword temp;
450 state->NumNcycles++;
452 temp = ARMul_ReadWord (state, address);
454 state->NumNcycles++;
456 PutWord (state, address, data, TRUE);
458 return temp;
461 /***************************************************************************\
462 * Swap Byte, (Two Non Sequential Cycles) *
463 \***************************************************************************/
465 ARMword ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
467 ARMword temp;
469 temp = ARMul_LoadByte (state, address);
470 ARMul_StoreByte (state, address, data);
472 return temp;
475 /***************************************************************************\
476 * Count I Cycles *
477 \***************************************************************************/
479 void
480 ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
482 state->NumIcycles += number;
483 ARMul_CLEARABORT;
486 /***************************************************************************\
487 * Count C Cycles *
488 \***************************************************************************/
490 void
491 ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
493 state->NumCcycles += number;
494 ARMul_CLEARABORT;
498 /* Read a byte. Do not check for alignment or access errors. */
500 ARMword
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);
511 void
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),
521 FALSE);