-
Notifications
You must be signed in to change notification settings - Fork 10
/
Tlb.cpp
247 lines (218 loc) · 8.19 KB
/
Tlb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Project 64 - A Nintendo 64 emulator.
*
* (c) Copyright 2001 zilmar ([email protected]) and
* Jabo ([email protected]).
*
* pj64 homepage: www.pj64.net
*
* Permission to use, copy, modify and distribute Project64 in both binary and
* source form, for non-commercial purposes, is hereby granted without fee,
* providing that this license information and copyright notice appear with
* all copies and any derived work.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event shall the authors be held liable for any damages
* arising from the use of this software.
*
* Project64 is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for Project64 or software derived from Project64.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so if they want them.
*
*/
#include <Windows.h>
#include "main.h"
#include "debugger.h"
#include "cpu.h"
void SetupTLB_Entry (int Entry);
FASTTLB FastTlb[64];
TLB tlb[32];
BOOL AddressDefined ( DWORD VAddr) {
DWORD i;
if (VAddr >= 0x80000000 && VAddr <= 0xBFFFFFFF) {
return TRUE;
}
for (i = 0; i < 64; i++) {
if (FastTlb[i].ValidEntry == FALSE) { continue; }
if (VAddr >= FastTlb[i].VSTART && VAddr <= FastTlb[i].VEND) {
return TRUE;
}
}
return FALSE;
}
void InitilizeTLB (void) {
DWORD count;
for (count = 0; count < 32; count++) { tlb[count].EntryDefined = FALSE; }
for (count = 0; count < 64; count++) { FastTlb[count].ValidEntry = FALSE; }
SetupTLB();
}
void SetupTLB (void) {
DWORD count;
memset(TLB_ReadMap,0,(0xFFFFF * sizeof(DWORD)));
memset(TLB_WriteMap,0,(0xFFFFF * sizeof(DWORD)));
for (count = 0x80000000; count < 0xC0000000; count += 0x1000) {
TLB_ReadMap[count >> 12] = ((DWORD)N64MEM + (count & 0x1FFFFFFF)) - count;
TLB_WriteMap[count >> 12] = ((DWORD)N64MEM + (count & 0x1FFFFFFF)) - count;
}
for (count = 0; count < 32; count ++) { SetupTLB_Entry(count); }
//GE Hack
//for (count = 0x7F000000; count < 0x80000000; count += 0x1000) {
// TLB_ReadMap[count >> 12] = ((DWORD)N64MEM + (count - 0x7F000000 + 0x10034b30)) - count;
// TLB_WriteMap[count >> 12] = ((DWORD)N64MEM + (count - 0x7F000000 + 0x10034b30)) - count;
//}
}
void SetupTLB_Entry (int Entry) {
int FastIndx;
if (!tlb[Entry].EntryDefined) { return; }
FastIndx = Entry << 1;
FastTlb[FastIndx].VSTART=tlb[Entry].EntryHi.VPN2 << 13;
FastTlb[FastIndx].VEND = FastTlb[FastIndx].VSTART + (tlb[Entry].PageMask.Mask << 12) + 0xFFF;
FastTlb[FastIndx].PHYSSTART = tlb[Entry].EntryLo0.PFN << 12;
FastTlb[FastIndx].VALID = tlb[Entry].EntryLo0.V;
FastTlb[FastIndx].DIRTY = tlb[Entry].EntryLo0.D;
FastTlb[FastIndx].GLOBAL = tlb[Entry].EntryLo0.GLOBAL & tlb[Entry].EntryLo1.GLOBAL;
FastTlb[FastIndx].ValidEntry = FALSE;
FastIndx = (Entry << 1) + 1;
FastTlb[FastIndx].VSTART=(tlb[Entry].EntryHi.VPN2 << 13) + ((tlb[Entry].PageMask.Mask << 12) + 0xFFF + 1);
FastTlb[FastIndx].VEND = FastTlb[FastIndx].VSTART + (tlb[Entry].PageMask.Mask << 12) + 0xFFF;
FastTlb[FastIndx].PHYSSTART = tlb[Entry].EntryLo1.PFN << 12;
FastTlb[FastIndx].VALID = tlb[Entry].EntryLo1.V;
FastTlb[FastIndx].DIRTY = tlb[Entry].EntryLo1.D;
FastTlb[FastIndx].GLOBAL = tlb[Entry].EntryLo0.GLOBAL & tlb[Entry].EntryLo1.GLOBAL;
FastTlb[FastIndx].ValidEntry = FALSE;
for ( FastIndx = Entry << 1; FastIndx <= (Entry << 1) + 1; FastIndx++) {
DWORD count;
if (!FastTlb[FastIndx].VALID) {
FastTlb[FastIndx].ValidEntry = TRUE;
continue;
}
if (FastTlb[FastIndx].VEND <= FastTlb[FastIndx].VSTART) {
#ifndef EXTERNAL_RELEASE
DisplayError("Vstart = Vend for tlb mapping");
#endif
continue;
}
if (FastTlb[FastIndx].VSTART >= 0x80000000 && FastTlb[FastIndx].VEND <= 0xBFFFFFFF) {
continue;
}
if (FastTlb[FastIndx].PHYSSTART > 0x1FFFFFFF) {
continue;
}
//if (FastTlb[FastIndx].GLOBAL == 0) {
// DisplayError("Non Global TLB Entry ???");
// continue;
//}
//test if overlap
FastTlb[FastIndx].ValidEntry = TRUE;
for (count = FastTlb[FastIndx].VSTART; count < FastTlb[FastIndx].VEND; count += 0x1000) {
TLB_ReadMap[count >> 12] = ((DWORD)N64MEM + (count - FastTlb[FastIndx].VSTART + FastTlb[FastIndx].PHYSSTART)) - count;
if (!FastTlb[FastIndx].DIRTY) { continue; }
TLB_WriteMap[count >> 12] = ((DWORD)N64MEM + (count - FastTlb[FastIndx].VSTART + FastTlb[FastIndx].PHYSSTART)) - count;
}
}
}
void TLB_Probe (void) {
int Counter;
#ifndef EXTERNAL_RELEASE
if (LogOptions.GenerateLog) {
if (LogOptions.LogTLB) {
LogMessage("%08X: TLB Probe: EntryHI :%X",PROGRAM_COUNTER,ENTRYHI_REGISTER);
}
}
#endif
INDEX_REGISTER |= 0x80000000;
for (Counter = 0; Counter < 32; Counter ++) {
DWORD TlbValue = tlb[Counter].EntryHi.Value & (~tlb[Counter].PageMask.Mask << 13);
DWORD EntryHi = ENTRYHI_REGISTER & (~tlb[Counter].PageMask.Mask << 13);
if (TlbValue == EntryHi) {
BOOL Global = (tlb[Counter].EntryHi.Value & 0x100) != 0;
BOOL SameAsid = ((tlb[Counter].EntryHi.Value & 0xFF) == (ENTRYHI_REGISTER & 0xFF));
if (Global || SameAsid) {
INDEX_REGISTER = Counter;
return;
}
}
}
}
void TLB_Read (void) {
DWORD index = INDEX_REGISTER & 0x1F;
#ifndef EXTERNAL_RELEASE
if (LogOptions.GenerateLog) {
if (LogOptions.LogTLB) {
LogMessage("%08X: TLB Read: index :%X %X - %X",PROGRAM_COUNTER,INDEX_REGISTER,
tlb[index].EntryHi.Value,(tlb[index].EntryHi.Value & ~tlb[index].PageMask.Value));
}
}
#endif
PAGE_MASK_REGISTER = tlb[index].PageMask.Value ;
ENTRYHI_REGISTER = (tlb[index].EntryHi.Value & ~tlb[index].PageMask.Value) ;
ENTRYLO0_REGISTER = tlb[index].EntryLo0.Value;
ENTRYLO1_REGISTER = tlb[index].EntryLo1.Value;
}
BOOL TranslateVaddr ( DWORD * Addr) {
if (TLB_ReadMap[*Addr >> 12] == 0) { return FALSE; }
*Addr = (DWORD)((BYTE *)(TLB_ReadMap[*Addr >> 12] + *Addr) - N64MEM);
return TRUE;
}
void _fastcall WriteTLBEntry (int index) {
int FastIndx;
#ifdef TLB_HACK
FastIndx = index << 1;
if ((PROGRAM_COUNTER >= FastTlb[FastIndx].VSTART &&
PROGRAM_COUNTER < FastTlb[FastIndx].VEND &&
FastTlb[FastIndx].ValidEntry && FastTlb[FastIndx].VALID)
||
(PROGRAM_COUNTER >= FastTlb[FastIndx + 1].VSTART &&
PROGRAM_COUNTER < FastTlb[FastIndx + 1].VEND &&
FastTlb[FastIndx + 1].ValidEntry && FastTlb[FastIndx + 1].VALID))
{
#ifndef EXTERNAL_RELEASE
if (LogOptions.GenerateLog && LogOptions.LogTLB) {
LogMessage("%08X: TLB write: Index: %d PageMask: %X EntryLo0: %X EntryLo1: %X EntryHi: %X",PROGRAM_COUNTER,
index,PAGE_MASK_REGISTER,ENTRYLO0_REGISTER,ENTRYLO1_REGISTER,ENTRYHI_REGISTER);
LogMessage(" Being Ignored");
LogMessage("");
}
#endif
return;
}
#endif
if (tlb[index].EntryDefined) {
DWORD count;
for ( FastIndx = index << 1; FastIndx <= (index << 1) + 1; FastIndx++) {
if (!FastTlb[FastIndx].ValidEntry) { continue; }
if (!FastTlb[FastIndx].VALID) { continue; }
for (count = FastTlb[FastIndx].VSTART; count < FastTlb[FastIndx].VEND; count += 0x1000) {
TLB_ReadMap[count >> 12] = 0;
TLB_WriteMap[count >> 12] = 0;
}
}
}
tlb[index].PageMask.Value = PAGE_MASK_REGISTER;
tlb[index].EntryHi.Value = ENTRYHI_REGISTER;
tlb[index].EntryLo0.Value = ENTRYLO0_REGISTER;
tlb[index].EntryLo1.Value = ENTRYLO1_REGISTER;
tlb[index].EntryDefined = TRUE;
#ifndef EXTERNAL_RELEASE
if (LogOptions.GenerateLog && LogOptions.LogTLB) {
LogMessage("%08X: TLB write: Index: %d PageMask: %X EntryLo0: %X EntryLo1: %X EntryHi: %X",PROGRAM_COUNTER,
index,PAGE_MASK_REGISTER,ENTRYLO0_REGISTER,ENTRYLO1_REGISTER,ENTRYHI_REGISTER);
LogMessage(" Entry 1: VStart: %X VEnd: %X Physical Start: %X",
tlb[index].EntryHi.VPN2 << 13, //VStart
(tlb[index].EntryHi.VPN2 << 13) + (tlb[index].PageMask.Mask << 12) + 0xFFF, //Vend
tlb[index].EntryLo0.PFN << 12);
LogMessage(" Entry 2: VStart: %X VEnd: %X Physical Start: %X",
(tlb[index].EntryHi.VPN2 << 13) + ((tlb[index].PageMask.Mask << 12) + 0xFFF + 1), //VStart
(tlb[index].EntryHi.VPN2 << 13) + ((tlb[index].PageMask.Mask << 12) + 0xFFF + 1) + (tlb[index].PageMask.Mask << 12) + 0xFFF, //Vend
tlb[index].EntryLo1.PFN << 12);
LogMessage("");
}
#endif
SetupTLB_Entry(index);
#if (!defined(EXTERNAL_RELEASE))
RefreshTLBWindow();
#endif
}