2010-05-22 04:47:15 +00:00
|
|
|
#include "stdafx.h"
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
CTLB::CTLB(CTLB_CB * CallBack ):
|
|
|
|
m_CB(CallBack)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
WriteTrace(TraceTLB,"CTLB::CTLB - Start");
|
2010-06-04 06:25:07 +00:00
|
|
|
memset(m_tlb,0,sizeof(m_tlb));
|
|
|
|
memset(m_FastTlb,0,sizeof(m_FastTlb));
|
2010-05-22 04:47:15 +00:00
|
|
|
Reset(true);
|
2008-09-18 03:15:49 +00:00
|
|
|
WriteTrace(TraceTLB,"CTLB::CTLB - Done");
|
|
|
|
}
|
|
|
|
|
|
|
|
CTLB::~CTLB (void) {
|
|
|
|
WriteTrace(TraceTLB,"CTLB::~CTLB - Done");
|
|
|
|
WriteTrace(TraceTLB,"CTLB::~CTLB - Done");
|
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
void CTLB::Reset (bool InvalidateTLB) {
|
2008-09-18 03:15:49 +00:00
|
|
|
DWORD count;
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
for (count = 0; count < 64; count++)
|
|
|
|
{
|
|
|
|
m_FastTlb[count].ValidEntry = false;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
if (InvalidateTLB)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2010-05-22 04:47:15 +00:00
|
|
|
for (count = 0; count < 32; count++)
|
|
|
|
{
|
|
|
|
m_tlb[count].EntryDefined = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (count = 0; count < 32; count ++)
|
|
|
|
{
|
|
|
|
SetupTLB_Entry(count,false);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
bool CTLB::AddressDefined ( DWORD VAddr) {
|
2008-09-18 03:15:49 +00:00
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
if (VAddr >= 0x80000000 && VAddr <= 0xBFFFFFFF) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
{
|
|
|
|
if (m_FastTlb[i].ValidEntry &&
|
|
|
|
VAddr >= m_FastTlb[i].VSTART &&
|
|
|
|
VAddr <= m_FastTlb[i].VEND)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-05-22 04:47:15 +00:00
|
|
|
return false;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
void CTLB::Probe (void) {
|
2008-09-18 03:15:49 +00:00
|
|
|
int Counter;
|
|
|
|
|
|
|
|
WriteTrace(TraceTLB,"TLB Probe");
|
2010-05-22 04:47:15 +00:00
|
|
|
_Reg->INDEX_REGISTER |= 0x80000000;
|
|
|
|
for (Counter = 0; Counter < 32; Counter ++)
|
|
|
|
{
|
|
|
|
if (!m_tlb[Counter].EntryDefined)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD & TlbEntryHiValue = m_tlb[Counter].EntryHi.Value;
|
|
|
|
DWORD Mask = ~m_tlb[Counter].PageMask.Mask << 13;
|
|
|
|
DWORD TlbValueMasked = TlbEntryHiValue & Mask;
|
|
|
|
DWORD EntryHiMasked = _Reg->ENTRYHI_REGISTER & Mask;
|
|
|
|
|
|
|
|
if (TlbValueMasked == EntryHiMasked) {
|
|
|
|
if ((TlbEntryHiValue & 0x100) != 0 || //Global
|
|
|
|
((TlbEntryHiValue & 0xFF) == (_Reg->ENTRYHI_REGISTER & 0xFF))) //SameAsid
|
|
|
|
{
|
|
|
|
_Reg->INDEX_REGISTER = Counter;
|
|
|
|
int FastIndx = Counter << 1;
|
|
|
|
m_FastTlb[FastIndx].Probed = true;
|
|
|
|
m_FastTlb[FastIndx + 1].Probed = true;
|
2008-09-18 03:15:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
void CTLB::ReadEntry (void) {
|
|
|
|
DWORD index = _Reg->INDEX_REGISTER & 0x1F;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
_Reg->PAGE_MASK_REGISTER = m_tlb[index].PageMask.Value ;
|
|
|
|
_Reg->ENTRYHI_REGISTER = (m_tlb[index].EntryHi.Value & ~m_tlb[index].PageMask.Value) ;
|
|
|
|
_Reg->ENTRYLO0_REGISTER = m_tlb[index].EntryLo0.Value;
|
|
|
|
_Reg->ENTRYLO1_REGISTER = m_tlb[index].EntryLo1.Value;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
void CTLB::WriteEntry (int index, bool Random) {
|
2008-09-18 03:15:49 +00:00
|
|
|
int FastIndx;
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
WriteTraceF(TraceTLB,"Write Entry %02d %d %08X %08X %08X %08X ",index,Random,_Reg->PAGE_MASK_REGISTER,_Reg->ENTRYHI_REGISTER,_Reg->ENTRYLO0_REGISTER,_Reg->ENTRYLO1_REGISTER);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Check to see if entry is unmapping it self
|
2010-05-22 04:47:15 +00:00
|
|
|
if (m_tlb[index].EntryDefined) {
|
2008-09-18 03:15:49 +00:00
|
|
|
FastIndx = index << 1;
|
2010-05-22 04:47:15 +00:00
|
|
|
if (*_PROGRAM_COUNTER >= m_FastTlb[FastIndx].VSTART &&
|
|
|
|
*_PROGRAM_COUNTER < m_FastTlb[FastIndx].VEND &&
|
|
|
|
m_FastTlb[FastIndx].ValidEntry && m_FastTlb[FastIndx].VALID)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2010-05-22 04:47:15 +00:00
|
|
|
WriteTraceF(TraceTLB,"Write Entry: Ignored PC: %X VAddr Start: %X VEND: %X",*_PROGRAM_COUNTER,m_FastTlb[FastIndx].VSTART,m_FastTlb[FastIndx].VEND);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (*_PROGRAM_COUNTER >= m_FastTlb[FastIndx + 1].VSTART &&
|
|
|
|
*_PROGRAM_COUNTER < m_FastTlb[FastIndx + 1].VEND &&
|
|
|
|
m_FastTlb[FastIndx + 1].ValidEntry && m_FastTlb[FastIndx + 1].VALID)
|
|
|
|
{
|
|
|
|
WriteTraceF(TraceTLB,"Write Entry: Ignored PC: %X VAddr Start: %X VEND: %X",*_PROGRAM_COUNTER,m_FastTlb[FastIndx + 1].VSTART,m_FastTlb[FastIndx + 1].VEND);
|
2008-09-18 03:15:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Reset old addresses
|
2010-05-22 04:47:15 +00:00
|
|
|
if (m_tlb[index].EntryDefined)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
for ( FastIndx = index << 1; FastIndx <= (index << 1) + 1; FastIndx++) {
|
2010-05-22 04:47:15 +00:00
|
|
|
if (!m_FastTlb[FastIndx].ValidEntry) { continue; }
|
|
|
|
if (!m_FastTlb[FastIndx].VALID) { continue; }
|
|
|
|
if (m_tlb[index].PageMask.Value == _Reg->PAGE_MASK_REGISTER &&
|
|
|
|
m_tlb[index].EntryHi.Value == _Reg->ENTRYHI_REGISTER)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2010-05-22 04:47:15 +00:00
|
|
|
if (FastIndx == (index << 1) && m_tlb[index].EntryLo0.Value == _Reg->ENTRYLO0_REGISTER) {
|
2008-09-18 03:15:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-22 04:47:15 +00:00
|
|
|
if (FastIndx != (index << 1) && m_tlb[index].EntryLo1.Value == _Reg->ENTRYLO1_REGISTER) {
|
2008-09-18 03:15:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-05-22 04:47:15 +00:00
|
|
|
m_CB->TLB_Unmaped(m_FastTlb[FastIndx].VSTART,m_FastTlb[FastIndx].Length);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
//fill in m_tlb entry
|
|
|
|
m_tlb[index].PageMask.Value = _Reg->PAGE_MASK_REGISTER;
|
|
|
|
m_tlb[index].EntryHi.Value = _Reg->ENTRYHI_REGISTER;
|
|
|
|
m_tlb[index].EntryLo0.Value = _Reg->ENTRYLO0_REGISTER;
|
|
|
|
m_tlb[index].EntryLo1.Value = _Reg->ENTRYLO1_REGISTER;
|
|
|
|
m_tlb[index].EntryDefined = true;
|
2008-09-18 03:15:49 +00:00
|
|
|
SetupTLB_Entry(index,Random);
|
2010-05-22 04:47:15 +00:00
|
|
|
m_CB->TLB_Changed();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTLB::SetupTLB_Entry (int index, bool Random) {
|
|
|
|
//Fix up Fast TLB entries
|
2010-05-22 04:47:15 +00:00
|
|
|
if (!m_tlb[index].EntryDefined) { return; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
int FastIndx = index << 1;
|
2010-05-22 04:47:15 +00:00
|
|
|
m_FastTlb[FastIndx].Length = (m_tlb[index].PageMask.Mask << 12) + 0xFFF;
|
|
|
|
m_FastTlb[FastIndx].VSTART=m_tlb[index].EntryHi.VPN2 << 13;
|
|
|
|
m_FastTlb[FastIndx].VEND = m_FastTlb[FastIndx].VSTART + m_FastTlb[FastIndx].Length;
|
|
|
|
m_FastTlb[FastIndx].PHYSSTART = m_tlb[index].EntryLo0.PFN << 12;
|
|
|
|
m_FastTlb[FastIndx].PHYSEND = m_FastTlb[FastIndx].PHYSSTART + m_FastTlb[FastIndx].Length;
|
|
|
|
m_FastTlb[FastIndx].VALID = m_tlb[index].EntryLo0.V;
|
|
|
|
m_FastTlb[FastIndx].DIRTY = m_tlb[index].EntryLo0.D;
|
|
|
|
m_FastTlb[FastIndx].GLOBAL = m_tlb[index].EntryLo0.GLOBAL & m_tlb[index].EntryLo1.GLOBAL;
|
|
|
|
m_FastTlb[FastIndx].ValidEntry = FALSE;
|
|
|
|
m_FastTlb[FastIndx].Random = Random;
|
|
|
|
m_FastTlb[FastIndx].Probed = false;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
FastIndx = (index << 1) + 1;
|
2010-05-22 04:47:15 +00:00
|
|
|
m_FastTlb[FastIndx].Length = (m_tlb[index].PageMask.Mask << 12) + 0xFFF;
|
|
|
|
m_FastTlb[FastIndx].VSTART=(m_tlb[index].EntryHi.VPN2 << 13) + (m_FastTlb[FastIndx].Length + 1);
|
|
|
|
m_FastTlb[FastIndx].VEND = m_FastTlb[FastIndx].VSTART + m_FastTlb[FastIndx].Length;
|
|
|
|
m_FastTlb[FastIndx].PHYSSTART = m_tlb[index].EntryLo1.PFN << 12;
|
|
|
|
m_FastTlb[FastIndx].PHYSEND = m_FastTlb[FastIndx].PHYSSTART + m_FastTlb[FastIndx].Length;
|
|
|
|
m_FastTlb[FastIndx].VALID = m_tlb[index].EntryLo1.V;
|
|
|
|
m_FastTlb[FastIndx].DIRTY = m_tlb[index].EntryLo1.D;
|
|
|
|
m_FastTlb[FastIndx].GLOBAL = m_tlb[index].EntryLo0.GLOBAL & m_tlb[index].EntryLo1.GLOBAL;
|
|
|
|
m_FastTlb[FastIndx].ValidEntry = FALSE;
|
|
|
|
m_FastTlb[FastIndx].Random = Random;
|
|
|
|
m_FastTlb[FastIndx].Probed = false;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Test both entries to see if they are valid
|
|
|
|
for ( FastIndx = index << 1; FastIndx <= (index << 1) + 1; FastIndx++) {
|
2010-05-22 04:47:15 +00:00
|
|
|
if (!m_FastTlb[FastIndx].VALID) {
|
|
|
|
m_FastTlb[FastIndx].ValidEntry = true;
|
2008-09-18 03:15:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
if (m_FastTlb[FastIndx].VEND <= m_FastTlb[FastIndx].VSTART) {
|
2008-09-18 03:15:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-22 04:47:15 +00:00
|
|
|
if (m_FastTlb[FastIndx].VSTART >= 0x80000000 && m_FastTlb[FastIndx].VEND <= 0xBFFFFFFF) {
|
2008-09-18 03:15:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-22 04:47:15 +00:00
|
|
|
if (m_FastTlb[FastIndx].PHYSSTART > 0x1FFFFFFF) {
|
2008-09-18 03:15:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-22 04:47:15 +00:00
|
|
|
//MAP the new m_tlb entry for reading and writing
|
|
|
|
m_FastTlb[FastIndx].ValidEntry = true;
|
|
|
|
m_CB->TLB_Mapped(m_FastTlb[FastIndx].VSTART,m_FastTlb[FastIndx].Length,m_FastTlb[FastIndx].PHYSSTART, !m_FastTlb[FastIndx].DIRTY);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CTLB::PAddrToVAddr(DWORD PAddr, DWORD & VAddr, DWORD & Index )
|
|
|
|
{
|
|
|
|
for (int i = Index; i < 64; i++)
|
|
|
|
{
|
2010-05-22 04:47:15 +00:00
|
|
|
if (m_FastTlb[i].ValidEntry == false) { continue; }
|
|
|
|
if (PAddr >= m_FastTlb[i].PHYSSTART && PAddr < m_FastTlb[i].PHYSEND)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2010-05-22 04:47:15 +00:00
|
|
|
VAddr = m_FastTlb[i].VSTART + (PAddr - m_FastTlb[i].PHYSSTART);
|
2008-09-18 03:15:49 +00:00
|
|
|
Index = i + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|