Advertisement
ZirconiumX

Untitled

Dec 28th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. uint32_t EmotionEngine::translate_address(uint32_t address, bool write)
  2. {
  3.     // Is this address legal to access?
  4.     if (!cp0->status.exception && !cp0->status.error) {
  5.         // It is illegal to access kernel and supervisor segments in user mode, assuming we're not in an exception.
  6.         if (cp0->status.mode == 2 && address >= 0x80000000) {
  7.             if (write)
  8.                 address_error_store();
  9.             else
  10.                 address_error_load();
  11.         }
  12.         // It is illegal to access kernel segments in supervisor mode.
  13.         if (cp0->status.mode == 1 && address >= 0x80000000 && !(address >= 0xC0000000 && address < 0xE0000000)) {
  14.             if (write)
  15.                 address_error_store();
  16.             else
  17.                 address_error_load();
  18.         }
  19.     }
  20.  
  21.     // kseg0 and kseg1 are not mapped; just translate to physical address.
  22.     if (address >= 0x80000000 && address < 0xC0000000) {
  23.         return address & 0x1FFFFFFF;
  24.     }
  25.  
  26.     int tlb_virtual_page_number = cp0->gpr[10] >> 13;
  27.     int tlb_address_space_id = cp0->gpr[10] & 0xFF;
  28.     int tlb_page_mask = cp0->gpr[5];
  29.  
  30.     // Need to work out how to get the current virtual page number.
  31.     //int virtual_page_number = (address & ~tlb_page_mask) >> 12;
  32.  
  33.     // Search for a matching TLB entry.
  34.     int entry = 0;
  35.     for (; entry < 48; entry++) {
  36.         // Does the virtual page number match?
  37.         if (cp0->tlb[entry].virtual_page_number != tlb_virtual_page_number)
  38.             continue;
  39.         // Does the address space ID match?
  40.         if (cp0->tlb[entry].global && cp0->tlb[entry].address_space_id != tlb_address_space_id)
  41.             continue;
  42.         // We have a matching entry.
  43.         break;
  44.     }
  45.  
  46.     // If we have no matching entries, this is a TLB Miss exception.
  47.     if (entry == 48) {
  48.         tlb_miss();
  49.     }
  50.  
  51.     // If the matching entry has a zero valid bit, this is a TLB Invalid exception.
  52.     if (!cp0->tlb[entry].valid[0]) {
  53.         tlb_invalid();
  54.     }
  55.  
  56.     // If the matching entry has a zero dirty bit, and we are trying to write to it, this is a TLB Modified exception.
  57.     if (write && !cp0->tlb[entry].dirty[0]) {
  58.         tlb_modified();
  59.     }
  60.  
  61.     if (cp0->tlb[entry].scratchpad) {
  62.         Errors::die("[MMU] Unimplemented scratchpad mapping");
  63.     }
  64.  
  65.     return cp0->tlb[entry].page_frame_number[0] | (address & 0x1FFF);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement