sim-main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* Copyright (C) 1998, Cygnus Solutions
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #ifndef SIM_MAIN_C
  14. #define SIM_MAIN_C
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include "sim-assert.h"
  19. #include <stdlib.h>
  20. /*---------------------------------------------------------------------------*/
  21. /*-- simulator engine -------------------------------------------------------*/
  22. /*---------------------------------------------------------------------------*/
  23. /* Description from page A-22 of the "MIPS IV Instruction Set" manual
  24. (revision 3.1) */
  25. /* Load a value from memory. Use the cache and main memory as
  26. specified in the Cache Coherence Algorithm (CCA) and the sort of
  27. access (IorD) to find the contents of AccessLength memory bytes
  28. starting at physical location pAddr. The data is returned in the
  29. fixed width naturally-aligned memory element (MemElem). The
  30. low-order two (or three) bits of the address and the AccessLength
  31. indicate which of the bytes within MemElem needs to be given to the
  32. processor. If the memory access type of the reference is uncached
  33. then only the referenced bytes are read from memory and valid
  34. within the memory element. If the access type is cached, and the
  35. data is not present in cache, an implementation specific size and
  36. alignment block of memory is read and loaded into the cache to
  37. satisfy a load reference. At a minimum, the block is the entire
  38. memory element. */
  39. INLINE_SIM_MAIN (void)
  40. load_memory (SIM_DESC SD,
  41. sim_cpu *CPU,
  42. address_word cia,
  43. uword64* memvalp,
  44. uword64* memval1p,
  45. int CCA,
  46. unsigned int AccessLength,
  47. address_word pAddr,
  48. address_word vAddr,
  49. int IorD)
  50. {
  51. uword64 value = 0;
  52. uword64 value1 = 0;
  53. #ifdef DEBUG
  54. sim_io_printf(sd,"DBG: LoadMemory(%p,%p,%d,%d,0x%s,0x%s,%s)\n",memvalp,memval1p,CCA,AccessLength,pr_addr(pAddr),pr_addr(vAddr),(IorD ? "isDATA" : "isINSTRUCTION"));
  55. #endif /* DEBUG */
  56. #if defined(WARN_MEM)
  57. if (CCA != uncached)
  58. sim_io_eprintf(sd,"LoadMemory CCA (%d) is not uncached (currently all accesses treated as cached)\n",CCA);
  59. #endif /* WARN_MEM */
  60. if (((pAddr & LOADDRMASK) + AccessLength) > LOADDRMASK)
  61. {
  62. /* In reality this should be a Bus Error */
  63. sim_io_error (SD, "LOAD AccessLength of %d would extend over %d bit aligned boundary for physical address 0x%s\n",
  64. AccessLength,
  65. (LOADDRMASK + 1) << 3,
  66. pr_addr (pAddr));
  67. }
  68. dotrace (SD, CPU, tracefh,((IorD == isDATA) ? 0 : 2),(unsigned int)(pAddr&0xFFFFFFFF),(AccessLength + 1),"load%s",((IorD == isDATA) ? "" : " instruction"));
  69. /* Read the specified number of bytes from memory. Adjust for
  70. host/target byte ordering/ Align the least significant byte
  71. read. */
  72. switch (AccessLength)
  73. {
  74. case AccessLength_QUADWORD:
  75. {
  76. unsigned_16 val = sim_core_read_aligned_16 (CPU, cia, read_map, pAddr);
  77. value1 = VH8_16 (val);
  78. value = VL8_16 (val);
  79. break;
  80. }
  81. case AccessLength_DOUBLEWORD:
  82. value = sim_core_read_aligned_8 (CPU, cia, read_map, pAddr);
  83. break;
  84. case AccessLength_SEPTIBYTE:
  85. value = sim_core_read_misaligned_7 (CPU, cia, read_map, pAddr);
  86. break;
  87. case AccessLength_SEXTIBYTE:
  88. value = sim_core_read_misaligned_6 (CPU, cia, read_map, pAddr);
  89. break;
  90. case AccessLength_QUINTIBYTE:
  91. value = sim_core_read_misaligned_5 (CPU, cia, read_map, pAddr);
  92. break;
  93. case AccessLength_WORD:
  94. value = sim_core_read_aligned_4 (CPU, cia, read_map, pAddr);
  95. break;
  96. case AccessLength_TRIPLEBYTE:
  97. value = sim_core_read_misaligned_3 (CPU, cia, read_map, pAddr);
  98. break;
  99. case AccessLength_HALFWORD:
  100. value = sim_core_read_aligned_2 (CPU, cia, read_map, pAddr);
  101. break;
  102. case AccessLength_BYTE:
  103. value = sim_core_read_aligned_1 (CPU, cia, read_map, pAddr);
  104. break;
  105. default:
  106. abort ();
  107. }
  108. #ifdef DEBUG
  109. printf("DBG: LoadMemory() : (offset %d) : value = 0x%s%s\n",
  110. (int)(pAddr & LOADDRMASK),pr_uword64(value1),pr_uword64(value));
  111. #endif /* DEBUG */
  112. /* See also store_memory. Position data in correct byte lanes. */
  113. if (AccessLength <= LOADDRMASK)
  114. {
  115. if (BigEndianMem)
  116. /* for big endian target, byte (pAddr&LOADDRMASK == 0) is
  117. shifted to the most significant byte position. */
  118. value <<= (((LOADDRMASK - (pAddr & LOADDRMASK)) - AccessLength) * 8);
  119. else
  120. /* For little endian target, byte (pAddr&LOADDRMASK == 0)
  121. is already in the correct postition. */
  122. value <<= ((pAddr & LOADDRMASK) * 8);
  123. }
  124. #ifdef DEBUG
  125. printf("DBG: LoadMemory() : shifted value = 0x%s%s\n",
  126. pr_uword64(value1),pr_uword64(value));
  127. #endif /* DEBUG */
  128. *memvalp = value;
  129. if (memval1p) *memval1p = value1;
  130. }
  131. /* Description from page A-23 of the "MIPS IV Instruction Set" manual
  132. (revision 3.1) */
  133. /* Store a value to memory. The specified data is stored into the
  134. physical location pAddr using the memory hierarchy (data caches and
  135. main memory) as specified by the Cache Coherence Algorithm
  136. (CCA). The MemElem contains the data for an aligned, fixed-width
  137. memory element (word for 32-bit processors, doubleword for 64-bit
  138. processors), though only the bytes that will actually be stored to
  139. memory need to be valid. The low-order two (or three) bits of pAddr
  140. and the AccessLength field indicates which of the bytes within the
  141. MemElem data should actually be stored; only these bytes in memory
  142. will be changed. */
  143. INLINE_SIM_MAIN (void)
  144. store_memory (SIM_DESC SD,
  145. sim_cpu *CPU,
  146. address_word cia,
  147. int CCA,
  148. unsigned int AccessLength,
  149. uword64 MemElem,
  150. uword64 MemElem1, /* High order 64 bits */
  151. address_word pAddr,
  152. address_word vAddr)
  153. {
  154. #ifdef DEBUG
  155. sim_io_printf(sd,"DBG: StoreMemory(%d,%d,0x%s,0x%s,0x%s,0x%s)\n",CCA,AccessLength,pr_uword64(MemElem),pr_uword64(MemElem1),pr_addr(pAddr),pr_addr(vAddr));
  156. #endif /* DEBUG */
  157. #if defined(WARN_MEM)
  158. if (CCA != uncached)
  159. sim_io_eprintf(sd,"StoreMemory CCA (%d) is not uncached (currently all accesses treated as cached)\n",CCA);
  160. #endif /* WARN_MEM */
  161. if (((pAddr & LOADDRMASK) + AccessLength) > LOADDRMASK)
  162. sim_io_error (SD, "STORE AccessLength of %d would extend over %d bit aligned boundary for physical address 0x%s\n",
  163. AccessLength,
  164. (LOADDRMASK + 1) << 3,
  165. pr_addr(pAddr));
  166. dotrace (SD, CPU, tracefh,1,(unsigned int)(pAddr&0xFFFFFFFF),(AccessLength + 1),"store");
  167. #ifdef DEBUG
  168. printf("DBG: StoreMemory: offset = %d MemElem = 0x%s%s\n",(unsigned int)(pAddr & LOADDRMASK),pr_uword64(MemElem1),pr_uword64(MemElem));
  169. #endif /* DEBUG */
  170. /* See also load_memory. Position data in correct byte lanes. */
  171. if (AccessLength <= LOADDRMASK)
  172. {
  173. if (BigEndianMem)
  174. /* for big endian target, byte (pAddr&LOADDRMASK == 0) is
  175. shifted to the most significant byte position. */
  176. MemElem >>= (((LOADDRMASK - (pAddr & LOADDRMASK)) - AccessLength) * 8);
  177. else
  178. /* For little endian target, byte (pAddr&LOADDRMASK == 0)
  179. is already in the correct postition. */
  180. MemElem >>= ((pAddr & LOADDRMASK) * 8);
  181. }
  182. #ifdef DEBUG
  183. printf("DBG: StoreMemory: shift = %d MemElem = 0x%s%s\n",shift,pr_uword64(MemElem1),pr_uword64(MemElem));
  184. #endif /* DEBUG */
  185. switch (AccessLength)
  186. {
  187. case AccessLength_QUADWORD:
  188. {
  189. unsigned_16 val = U16_8 (MemElem1, MemElem);
  190. sim_core_write_aligned_16 (CPU, cia, write_map, pAddr, val);
  191. break;
  192. }
  193. case AccessLength_DOUBLEWORD:
  194. sim_core_write_aligned_8 (CPU, cia, write_map, pAddr, MemElem);
  195. break;
  196. case AccessLength_SEPTIBYTE:
  197. sim_core_write_misaligned_7 (CPU, cia, write_map, pAddr, MemElem);
  198. break;
  199. case AccessLength_SEXTIBYTE:
  200. sim_core_write_misaligned_6 (CPU, cia, write_map, pAddr, MemElem);
  201. break;
  202. case AccessLength_QUINTIBYTE:
  203. sim_core_write_misaligned_5 (CPU, cia, write_map, pAddr, MemElem);
  204. break;
  205. case AccessLength_WORD:
  206. sim_core_write_aligned_4 (CPU, cia, write_map, pAddr, MemElem);
  207. break;
  208. case AccessLength_TRIPLEBYTE:
  209. sim_core_write_misaligned_3 (CPU, cia, write_map, pAddr, MemElem);
  210. break;
  211. case AccessLength_HALFWORD:
  212. sim_core_write_aligned_2 (CPU, cia, write_map, pAddr, MemElem);
  213. break;
  214. case AccessLength_BYTE:
  215. sim_core_write_aligned_1 (CPU, cia, write_map, pAddr, MemElem);
  216. break;
  217. default:
  218. abort ();
  219. }
  220. return;
  221. }
  222. INLINE_SIM_MAIN (uint32_t)
  223. ifetch32 (SIM_DESC SD,
  224. sim_cpu *CPU,
  225. address_word cia,
  226. address_word vaddr)
  227. {
  228. /* Copy the action of the LW instruction */
  229. address_word mask = LOADDRMASK;
  230. address_word access = AccessLength_WORD;
  231. address_word reverseendian = (ReverseEndian ? (mask ^ access) : 0);
  232. address_word bigendiancpu = (BigEndianCPU ? (mask ^ access) : 0);
  233. unsigned int byte;
  234. address_word paddr = vaddr;
  235. uint64_t memval;
  236. if ((vaddr & access) != 0)
  237. SignalExceptionInstructionFetch ();
  238. paddr = ((paddr & ~mask) | ((paddr & mask) ^ reverseendian));
  239. LoadMemory (&memval, NULL, access, paddr, vaddr, isINSTRUCTION, isREAL);
  240. byte = ((vaddr & mask) ^ bigendiancpu);
  241. return (memval >> (8 * byte));
  242. }
  243. INLINE_SIM_MAIN (uint16_t)
  244. ifetch16 (SIM_DESC SD,
  245. sim_cpu *CPU,
  246. address_word cia,
  247. address_word vaddr)
  248. {
  249. /* Copy the action of the LH instruction */
  250. address_word mask = LOADDRMASK;
  251. address_word access = AccessLength_HALFWORD;
  252. address_word reverseendian = (ReverseEndian ? (mask ^ access) : 0);
  253. address_word bigendiancpu = (BigEndianCPU ? (mask ^ access) : 0);
  254. unsigned int byte;
  255. address_word paddr = vaddr;
  256. uint64_t memval;
  257. if ((vaddr & access) != 0)
  258. SignalExceptionInstructionFetch ();
  259. paddr = ((paddr & ~mask) | ((paddr & mask) ^ reverseendian));
  260. LoadMemory (&memval, NULL, access, paddr, vaddr, isINSTRUCTION, isREAL);
  261. byte = ((vaddr & mask) ^ bigendiancpu);
  262. return (memval >> (8 * byte));
  263. }
  264. /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
  265. /* Order loads and stores to synchronise shared memory. Perform the
  266. action necessary to make the effects of groups of synchronizable
  267. loads and stores indicated by stype occur in the same order for all
  268. processors. */
  269. INLINE_SIM_MAIN (void)
  270. sync_operation (SIM_DESC sd,
  271. sim_cpu *cpu,
  272. address_word cia,
  273. int stype)
  274. {
  275. #ifdef DEBUG
  276. sim_io_printf(sd,"SyncOperation(%d) : TODO\n",stype);
  277. #endif /* DEBUG */
  278. return;
  279. }
  280. INLINE_SIM_MAIN (void)
  281. cache_op (SIM_DESC SD,
  282. sim_cpu *CPU,
  283. address_word cia,
  284. int op,
  285. address_word pAddr,
  286. address_word vAddr,
  287. unsigned int instruction)
  288. {
  289. #if 1 /* stop warning message being displayed (we should really just remove the code) */
  290. static int icache_warning = 1;
  291. static int dcache_warning = 1;
  292. #else
  293. static int icache_warning = 0;
  294. static int dcache_warning = 0;
  295. #endif
  296. /* If CP0 is not useable (User or Supervisor mode) and the CP0
  297. enable bit in the Status Register is clear - a coprocessor
  298. unusable exception is taken. */
  299. #if 0
  300. sim_io_printf(SD,"TODO: Cache availability checking (PC = 0x%s)\n",pr_addr(cia));
  301. #endif
  302. switch (op & 0x3) {
  303. case 0: /* instruction cache */
  304. switch (op >> 2) {
  305. case 0: /* Index Invalidate */
  306. case 1: /* Index Load Tag */
  307. case 2: /* Index Store Tag */
  308. case 4: /* Hit Invalidate */
  309. case 5: /* Fill */
  310. case 6: /* Hit Writeback */
  311. if (!icache_warning)
  312. {
  313. sim_io_eprintf(SD,"Instruction CACHE operation %d to be coded\n",(op >> 2));
  314. icache_warning = 1;
  315. }
  316. break;
  317. default:
  318. SignalException(ReservedInstruction,instruction);
  319. break;
  320. }
  321. break;
  322. case 1: /* data cache */
  323. case 3: /* secondary data cache */
  324. switch (op >> 2) {
  325. case 0: /* Index Writeback Invalidate */
  326. case 1: /* Index Load Tag */
  327. case 2: /* Index Store Tag */
  328. case 3: /* Create Dirty */
  329. case 4: /* Hit Invalidate */
  330. case 5: /* Hit Writeback Invalidate */
  331. case 6: /* Hit Writeback */
  332. if (!dcache_warning)
  333. {
  334. sim_io_eprintf(SD,"Data CACHE operation %d to be coded\n",(op >> 2));
  335. dcache_warning = 1;
  336. }
  337. break;
  338. default:
  339. SignalException(ReservedInstruction,instruction);
  340. break;
  341. }
  342. break;
  343. default: /* unrecognised cache ID */
  344. SignalException(ReservedInstruction,instruction);
  345. break;
  346. }
  347. return;
  348. }
  349. INLINE_SIM_MAIN (void)
  350. pending_tick (SIM_DESC SD,
  351. sim_cpu *CPU,
  352. address_word cia)
  353. {
  354. if (PENDING_TRACE)
  355. sim_io_eprintf (SD, "PENDING_DRAIN - 0x%lx - pending_in = %d, pending_out = %d, pending_total = %d\n", (unsigned long) cia, PENDING_IN, PENDING_OUT, PENDING_TOTAL);
  356. if (PENDING_OUT != PENDING_IN)
  357. {
  358. int loop;
  359. int index = PENDING_OUT;
  360. int total = PENDING_TOTAL;
  361. if (PENDING_TOTAL == 0)
  362. sim_engine_abort (SD, CPU, cia, "PENDING_DRAIN - Mis-match on pending update pointers\n");
  363. for (loop = 0, index = PENDING_OUT;
  364. (loop < total);
  365. loop++, index = (index + 1) % PSLOTS)
  366. {
  367. if (PENDING_SLOT_DEST[index] != NULL)
  368. {
  369. PENDING_SLOT_DELAY[index] -= 1;
  370. if (PENDING_SLOT_DELAY[index] == 0)
  371. {
  372. if (PENDING_TRACE)
  373. sim_io_eprintf (SD, "PENDING_DRAIN - drained - index %d, dest %p, bit %d, val %" PRIx64 ", size %d\n",
  374. index,
  375. PENDING_SLOT_DEST[index],
  376. PENDING_SLOT_BIT[index],
  377. PENDING_SLOT_VALUE[index],
  378. PENDING_SLOT_SIZE[index]);
  379. if (PENDING_SLOT_BIT[index] >= 0)
  380. switch (PENDING_SLOT_SIZE[index])
  381. {
  382. case 4:
  383. if (PENDING_SLOT_VALUE[index])
  384. *(uint32_t*)PENDING_SLOT_DEST[index] |=
  385. BIT32 (PENDING_SLOT_BIT[index]);
  386. else
  387. *(uint32_t*)PENDING_SLOT_DEST[index] &=
  388. BIT32 (PENDING_SLOT_BIT[index]);
  389. break;
  390. case 8:
  391. if (PENDING_SLOT_VALUE[index])
  392. *(uint64_t*)PENDING_SLOT_DEST[index] |=
  393. BIT64 (PENDING_SLOT_BIT[index]);
  394. else
  395. *(uint64_t*)PENDING_SLOT_DEST[index] &=
  396. BIT64 (PENDING_SLOT_BIT[index]);
  397. break;
  398. }
  399. else
  400. switch (PENDING_SLOT_SIZE[index])
  401. {
  402. case 4:
  403. *(uint32_t*)PENDING_SLOT_DEST[index] =
  404. PENDING_SLOT_VALUE[index];
  405. break;
  406. case 8:
  407. *(uint64_t*)PENDING_SLOT_DEST[index] =
  408. PENDING_SLOT_VALUE[index];
  409. break;
  410. }
  411. if (PENDING_OUT == index)
  412. {
  413. PENDING_SLOT_DEST[index] = NULL;
  414. PENDING_OUT = (PENDING_OUT + 1) % PSLOTS;
  415. PENDING_TOTAL--;
  416. }
  417. }
  418. else if (PENDING_TRACE && PENDING_SLOT_DELAY[index] > 0)
  419. sim_io_eprintf (SD, "PENDING_DRAIN - queued - index %d, delay %d, dest %p, bit %d, val %" PRIx64 ", size %d\n",
  420. index, PENDING_SLOT_DELAY[index],
  421. PENDING_SLOT_DEST[index],
  422. PENDING_SLOT_BIT[index],
  423. PENDING_SLOT_VALUE[index],
  424. PENDING_SLOT_SIZE[index]);
  425. }
  426. }
  427. }
  428. }
  429. #endif