[PATCH 22/57][Arm][GAS] Add support for MVE instructions: vmlaldav, vmlalv, vmlsldav...
[binutils-gdb.git] / sim / arm / armvirt.c
blob4f95ed838f6854a8061b60273b0f3e3145d2d8af
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. */
25 #include "armos.h"
26 #include "armdefs.h"
27 #include "ansidecl.h"
29 #ifdef VALIDATE /* for running the validate suite */
30 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
31 #define ABORTS 1
32 #endif
34 /* #define ABORTS */
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
44 #endif
46 #define NUMPAGES 64 * 1024
47 #define PAGESIZE 64 * 1024
48 #define PAGEBITS 16
49 #define OFFSETBITS 0xffff
51 int SWI_vector_installed = FALSE;
53 /***************************************************************************\
54 * Get a Word from Virtual Memory, maybe allocating the page *
55 \***************************************************************************/
57 static ARMword
58 GetWord (ARMul_State * state, ARMword address, int check)
60 ARMword page;
61 ARMword offset;
62 ARMword **pagetable;
63 ARMword *pageptr;
65 if (check && state->is_XScale)
66 XScale_check_memacc (state, &address, 0);
68 page = address >> PAGEBITS;
69 offset = (address & OFFSETBITS) >> 2;
70 pagetable = (ARMword **) state->MemDataPtr;
71 pageptr = *(pagetable + page);
73 if (pageptr == NULL)
75 pageptr = (ARMword *) malloc (PAGESIZE);
77 if (pageptr == NULL)
79 perror ("ARMulator can't allocate VM page");
80 exit (12);
83 *(pagetable + page) = pageptr;
86 return *(pageptr + offset);
89 /***************************************************************************\
90 * Put a Word into Virtual Memory, maybe allocating the page *
91 \***************************************************************************/
93 static void
94 PutWord (ARMul_State * state, ARMword address, ARMword data, int check)
96 ARMword page;
97 ARMword offset;
98 ARMword **pagetable;
99 ARMword *pageptr;
101 if (check && state->is_XScale)
102 XScale_check_memacc (state, &address, 1);
104 page = address >> PAGEBITS;
105 offset = (address & OFFSETBITS) >> 2;
106 pagetable = (ARMword **) state->MemDataPtr;
107 pageptr = *(pagetable + page);
109 if (pageptr == NULL)
111 pageptr = (ARMword *) malloc (PAGESIZE);
112 if (pageptr == NULL)
114 perror ("ARMulator can't allocate VM page");
115 exit (13);
118 *(pagetable + page) = pageptr;
121 if (address == 0x8)
122 SWI_vector_installed = TRUE;
124 *(pageptr + offset) = data;
127 /***************************************************************************\
128 * Initialise the memory interface *
129 \***************************************************************************/
131 unsigned
132 ARMul_MemoryInit (ARMul_State * state, unsigned long initmemsize)
134 ARMword **pagetable;
135 unsigned page;
137 if (initmemsize)
138 state->MemSize = initmemsize;
140 pagetable = (ARMword **) malloc (sizeof (ARMword *) * NUMPAGES);
142 if (pagetable == NULL)
143 return FALSE;
145 for (page = 0; page < NUMPAGES; page++)
146 *(pagetable + page) = NULL;
148 state->MemDataPtr = (unsigned char *) pagetable;
150 ARMul_ConsolePrint (state, ", 4 Gb memory");
152 return TRUE;
155 /***************************************************************************\
156 * Remove the memory interface *
157 \***************************************************************************/
159 void
160 ARMul_MemoryExit (ARMul_State * state)
162 ARMword page;
163 ARMword **pagetable;
164 ARMword *pageptr;
166 pagetable = (ARMword **) state->MemDataPtr;
167 for (page = 0; page < NUMPAGES; page++)
169 pageptr = *(pagetable + page);
170 if (pageptr != NULL)
171 free ((char *) pageptr);
173 free ((char *) pagetable);
174 return;
177 /***************************************************************************\
178 * ReLoad Instruction *
179 \***************************************************************************/
181 ARMword
182 ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
184 #ifdef ABORTS
185 if (address >= LOWABORT && address < HIGHABORT)
187 ARMul_PREFETCHABORT (address);
188 return ARMul_ABORTWORD;
190 else
192 ARMul_CLEARABORT;
194 #endif
196 if ((isize == 2) && (address & 0x2))
198 /* We return the next two halfwords: */
199 ARMword lo = GetWord (state, address, FALSE);
200 ARMword hi = GetWord (state, address + 4, FALSE);
202 if (state->bigendSig == HIGH)
203 return (lo << 16) | (hi >> 16);
204 else
205 return ((hi & 0xFFFF) << 16) | (lo >> 16);
208 return GetWord (state, address, TRUE);
211 /***************************************************************************\
212 * Load Instruction, Sequential Cycle *
213 \***************************************************************************/
215 ARMword ARMul_LoadInstrS (ARMul_State * state, ARMword address, ARMword isize)
217 state->NumScycles++;
219 return ARMul_ReLoadInstr (state, address, isize);
222 /***************************************************************************\
223 * Load Instruction, Non Sequential Cycle *
224 \***************************************************************************/
226 ARMword ARMul_LoadInstrN (ARMul_State * state, ARMword address, ARMword isize)
228 state->NumNcycles++;
230 return ARMul_ReLoadInstr (state, address, isize);
233 /***************************************************************************\
234 * Read Word (but don't tell anyone!) *
235 \***************************************************************************/
237 ARMword ARMul_ReadWord (ARMul_State * state, ARMword address)
239 #ifdef ABORTS
240 if (address >= LOWABORT && address < HIGHABORT)
242 ARMul_DATAABORT (address);
243 return ARMul_ABORTWORD;
245 else
247 ARMul_CLEARABORT;
249 #endif
251 return GetWord (state, address, TRUE);
254 /***************************************************************************\
255 * Load Word, Sequential Cycle *
256 \***************************************************************************/
258 ARMword ARMul_LoadWordS (ARMul_State * state, ARMword address)
260 state->NumScycles++;
262 return ARMul_ReadWord (state, address);
265 /***************************************************************************\
266 * Load Word, Non Sequential Cycle *
267 \***************************************************************************/
269 ARMword ARMul_LoadWordN (ARMul_State * state, ARMword address)
271 state->NumNcycles++;
273 return ARMul_ReadWord (state, address);
276 /***************************************************************************\
277 * Load Halfword, (Non Sequential Cycle) *
278 \***************************************************************************/
280 ARMword ARMul_LoadHalfWord (ARMul_State * state, ARMword address)
282 ARMword temp, offset;
284 state->NumNcycles++;
286 temp = ARMul_ReadWord (state, address);
287 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
289 return (temp >> offset) & 0xffff;
292 /***************************************************************************\
293 * Read Byte (but don't tell anyone!) *
294 \***************************************************************************/
296 ARMword ARMul_ReadByte (ARMul_State * state, ARMword address)
298 ARMword temp, offset;
300 temp = ARMul_ReadWord (state, address);
301 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
303 return (temp >> offset & 0xffL);
306 /***************************************************************************\
307 * Load Byte, (Non Sequential Cycle) *
308 \***************************************************************************/
310 ARMword ARMul_LoadByte (ARMul_State * state, ARMword address)
312 state->NumNcycles++;
314 return ARMul_ReadByte (state, address);
317 /***************************************************************************\
318 * Write Word (but don't tell anyone!) *
319 \***************************************************************************/
321 void
322 ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
324 #ifdef ABORTS
325 if (address >= LOWABORT && address < HIGHABORT)
327 ARMul_DATAABORT (address);
328 return;
330 else
332 ARMul_CLEARABORT;
334 #endif
336 PutWord (state, address, data, TRUE);
339 /***************************************************************************\
340 * Store Word, Sequential Cycle *
341 \***************************************************************************/
343 void
344 ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
346 state->NumScycles++;
348 ARMul_WriteWord (state, address, data);
351 /***************************************************************************\
352 * Store Word, Non Sequential Cycle *
353 \***************************************************************************/
355 void
356 ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
358 state->NumNcycles++;
360 ARMul_WriteWord (state, address, data);
363 /***************************************************************************\
364 * Store HalfWord, (Non Sequential Cycle) *
365 \***************************************************************************/
367 void
368 ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
370 ARMword temp, offset;
372 state->NumNcycles++;
374 #ifdef VALIDATE
375 if (address == TUBE)
377 if (data == 4)
378 state->Emulate = FALSE;
379 else
380 (void) putc ((char) data, stderr); /* Write Char */
381 return;
383 #endif
385 temp = ARMul_ReadWord (state, address);
386 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
388 PutWord (state, address,
389 (temp & ~(0xffffL << offset)) | ((data & 0xffffL) << offset),
390 TRUE);
393 /***************************************************************************\
394 * Write Byte (but don't tell anyone!) *
395 \***************************************************************************/
397 void
398 ARMul_WriteByte (ARMul_State * state, ARMword address, ARMword data)
400 ARMword temp, offset;
402 temp = ARMul_ReadWord (state, address);
403 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
405 PutWord (state, address,
406 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),
407 TRUE);
410 /***************************************************************************\
411 * Store Byte, (Non Sequential Cycle) *
412 \***************************************************************************/
414 void
415 ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
417 state->NumNcycles++;
419 #ifdef VALIDATE
420 if (address == TUBE)
422 if (data == 4)
423 state->Emulate = FALSE;
424 else
425 (void) putc ((char) data, stderr); /* Write Char */
426 return;
428 #endif
430 ARMul_WriteByte (state, address, data);
433 /***************************************************************************\
434 * Swap Word, (Two Non Sequential Cycles) *
435 \***************************************************************************/
437 ARMword ARMul_SwapWord (ARMul_State * state, ARMword address, ARMword data)
439 ARMword temp;
441 state->NumNcycles++;
443 temp = ARMul_ReadWord (state, address);
445 state->NumNcycles++;
447 PutWord (state, address, data, TRUE);
449 return temp;
452 /***************************************************************************\
453 * Swap Byte, (Two Non Sequential Cycles) *
454 \***************************************************************************/
456 ARMword ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
458 ARMword temp;
460 temp = ARMul_LoadByte (state, address);
461 ARMul_StoreByte (state, address, data);
463 return temp;
466 /***************************************************************************\
467 * Count I Cycles *
468 \***************************************************************************/
470 void
471 ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
473 state->NumIcycles += number;
474 ARMul_CLEARABORT;
477 /***************************************************************************\
478 * Count C Cycles *
479 \***************************************************************************/
481 void
482 ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
484 state->NumCcycles += number;
485 ARMul_CLEARABORT;
489 /* Read a byte. Do not check for alignment or access errors. */
491 ARMword
492 ARMul_SafeReadByte (ARMul_State * state, ARMword address)
494 ARMword temp, offset;
496 temp = GetWord (state, address, FALSE);
497 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
499 return (temp >> offset & 0xffL);
502 void
503 ARMul_SafeWriteByte (ARMul_State * state, ARMword address, ARMword data)
505 ARMword temp, offset;
507 temp = GetWord (state, address, FALSE);
508 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
510 PutWord (state, address,
511 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),
512 FALSE);