| 1 |
/* |
| 2 |
* Copyright (C) 2002-2010 The DOSBox Team |
| 3 |
* |
| 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 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 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. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* $Id: core_prefetch.cpp,v 1.3 2009-06-26 16:43:30 c2woody Exp $ */ |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
|
| 23 |
#include "dosbox.h" |
| 24 |
#include "mem.h" |
| 25 |
#include "cpu.h" |
| 26 |
#include "lazyflags.h" |
| 27 |
#include "inout.h" |
| 28 |
#include "callback.h" |
| 29 |
#include "pic.h" |
| 30 |
#include "fpu.h" |
| 31 |
#include "paging.h" |
| 32 |
|
| 33 |
#if C_DEBUG |
| 34 |
#include "debug.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#if (!C_CORE_INLINE) |
| 38 |
#define LoadMb(off) mem_readb(off) |
| 39 |
#define LoadMw(off) mem_readw(off) |
| 40 |
#define LoadMd(off) mem_readd(off) |
| 41 |
#define SaveMb(off,val) mem_writeb(off,val) |
| 42 |
#define SaveMw(off,val) mem_writew(off,val) |
| 43 |
#define SaveMd(off,val) mem_writed(off,val) |
| 44 |
#else |
| 45 |
#include "paging.h" |
| 46 |
#define LoadMb(off) mem_readb_inline(off) |
| 47 |
#define LoadMw(off) mem_readw_inline(off) |
| 48 |
#define LoadMd(off) mem_readd_inline(off) |
| 49 |
#define SaveMb(off,val) mem_writeb_inline(off,val) |
| 50 |
#define SaveMw(off,val) mem_writew_inline(off,val) |
| 51 |
#define SaveMd(off,val) mem_writed_inline(off,val) |
| 52 |
#endif |
| 53 |
|
| 54 |
extern Bitu cycle_count; |
| 55 |
|
| 56 |
#if C_FPU |
| 57 |
#define CPU_FPU 1 //Enable FPU escape instructions |
| 58 |
#endif |
| 59 |
|
| 60 |
#define CPU_PIC_CHECK 1 |
| 61 |
#define CPU_TRAP_CHECK 1 |
| 62 |
|
| 63 |
#define OPCODE_NONE 0x000 |
| 64 |
#define OPCODE_0F 0x100 |
| 65 |
#define OPCODE_SIZE 0x200 |
| 66 |
|
| 67 |
#define PREFIX_ADDR 0x1 |
| 68 |
#define PREFIX_REP 0x2 |
| 69 |
|
| 70 |
#define TEST_PREFIX_ADDR (core.prefixes & PREFIX_ADDR) |
| 71 |
#define TEST_PREFIX_REP (core.prefixes & PREFIX_REP) |
| 72 |
|
| 73 |
#define DO_PREFIX_SEG(_SEG) \ |
| 74 |
BaseDS=SegBase(_SEG); \ |
| 75 |
BaseSS=SegBase(_SEG); \ |
| 76 |
core.base_val_ds=_SEG; \ |
| 77 |
goto restart_opcode; |
| 78 |
|
| 79 |
#define DO_PREFIX_ADDR() \ |
| 80 |
core.prefixes=(core.prefixes & ~PREFIX_ADDR) | \ |
| 81 |
(cpu.code.big ^ PREFIX_ADDR); \ |
| 82 |
core.ea_table=&EATable[(core.prefixes&1) * 256]; \ |
| 83 |
goto restart_opcode; |
| 84 |
|
| 85 |
#define DO_PREFIX_REP(_ZERO) \ |
| 86 |
core.prefixes|=PREFIX_REP; \ |
| 87 |
core.rep_zero=_ZERO; \ |
| 88 |
goto restart_opcode; |
| 89 |
|
| 90 |
typedef PhysPt (*GetEAHandler)(void); |
| 91 |
|
| 92 |
static const Bit32u AddrMaskTable[2]={0x0000ffff,0xffffffff}; |
| 93 |
|
| 94 |
static struct { |
| 95 |
Bitu opcode_index; |
| 96 |
PhysPt cseip; |
| 97 |
PhysPt base_ds,base_ss; |
| 98 |
SegNames base_val_ds; |
| 99 |
bool rep_zero; |
| 100 |
Bitu prefixes; |
| 101 |
GetEAHandler * ea_table; |
| 102 |
} core; |
| 103 |
|
| 104 |
#define GETIP (core.cseip-SegBase(cs)) |
| 105 |
#define SAVEIP reg_eip=GETIP; |
| 106 |
#define LOADIP core.cseip=(SegBase(cs)+reg_eip); |
| 107 |
|
| 108 |
#define SegBase(c) SegPhys(c) |
| 109 |
#define BaseDS core.base_ds |
| 110 |
#define BaseSS core.base_ss |
| 111 |
|
| 112 |
|
| 113 |
#define MAX_PQ_SIZE 32 |
| 114 |
static Bit8u prefetch_buffer[MAX_PQ_SIZE]; |
| 115 |
static bool pq_valid=false; |
| 116 |
static Bitu pq_start; |
| 117 |
|
| 118 |
static Bit8u Fetchb() { |
| 119 |
Bit8u temp; |
| 120 |
if (pq_valid && (core.cseip>=pq_start) && (core.cseip<pq_start+CPU_PrefetchQueueSize)) { |
| 121 |
temp=prefetch_buffer[core.cseip-pq_start]; |
| 122 |
if ((core.cseip+1>=pq_start+CPU_PrefetchQueueSize-4) && |
| 123 |
(core.cseip+1<pq_start+CPU_PrefetchQueueSize)) { |
| 124 |
Bitu remaining_bytes=pq_start+CPU_PrefetchQueueSize-(core.cseip+1); |
| 125 |
for (Bitu i=0; i<remaining_bytes; i++) prefetch_buffer[i]=prefetch_buffer[core.cseip+1-pq_start+i]; |
| 126 |
for (Bitu i=remaining_bytes; i<CPU_PrefetchQueueSize; i++) prefetch_buffer[i]=LoadMb(core.cseip+1+i); |
| 127 |
pq_start=core.cseip+1; |
| 128 |
pq_valid=true; |
| 129 |
} |
| 130 |
} else { |
| 131 |
for (Bitu i=0; i<CPU_PrefetchQueueSize; i++) prefetch_buffer[i]=LoadMb(core.cseip+i); |
| 132 |
pq_start=core.cseip; |
| 133 |
pq_valid=true; |
| 134 |
temp=prefetch_buffer[0]; |
| 135 |
} |
| 136 |
/* if (temp!=LoadMb(core.cseip)) { |
| 137 |
LOG_MSG("prefetch queue content!=memory at %x:%x",SegValue(cs),reg_eip); |
| 138 |
} */ |
| 139 |
core.cseip+=1; |
| 140 |
return temp; |
| 141 |
} |
| 142 |
|
| 143 |
static Bit16u Fetchw() { |
| 144 |
Bit16u temp; |
| 145 |
if (pq_valid && (core.cseip>=pq_start) && (core.cseip+2<pq_start+CPU_PrefetchQueueSize)) { |
| 146 |
temp=prefetch_buffer[core.cseip-pq_start]| |
| 147 |
(prefetch_buffer[core.cseip-pq_start+1]<<8); |
| 148 |
if ((core.cseip+2>=pq_start+CPU_PrefetchQueueSize-4) && |
| 149 |
(core.cseip+2<pq_start+CPU_PrefetchQueueSize)) { |
| 150 |
Bitu remaining_bytes=pq_start+CPU_PrefetchQueueSize-(core.cseip+2); |
| 151 |
for (Bitu i=0; i<remaining_bytes; i++) prefetch_buffer[i]=prefetch_buffer[core.cseip+2-pq_start+i]; |
| 152 |
for (Bitu i=remaining_bytes; i<CPU_PrefetchQueueSize; i++) prefetch_buffer[i]=LoadMb(core.cseip+2+i); |
| 153 |
pq_start=core.cseip+2; |
| 154 |
pq_valid=true; |
| 155 |
} |
| 156 |
} else { |
| 157 |
for (Bitu i=0; i<CPU_PrefetchQueueSize; i++) prefetch_buffer[i]=LoadMb(core.cseip+i); |
| 158 |
pq_start=core.cseip; |
| 159 |
pq_valid=true; |
| 160 |
temp=prefetch_buffer[0] | (prefetch_buffer[1]<<8); |
| 161 |
} |
| 162 |
/* if (temp!=LoadMw(core.cseip)) { |
| 163 |
LOG_MSG("prefetch queue content!=memory at %x:%x",SegValue(cs),reg_eip); |
| 164 |
} */ |
| 165 |
core.cseip+=2; |
| 166 |
return temp; |
| 167 |
} |
| 168 |
|
| 169 |
static Bit32u Fetchd() { |
| 170 |
Bit32u temp; |
| 171 |
if (pq_valid && (core.cseip>=pq_start) && (core.cseip+4<pq_start+CPU_PrefetchQueueSize)) { |
| 172 |
temp=prefetch_buffer[core.cseip-pq_start]| |
| 173 |
(prefetch_buffer[core.cseip-pq_start+1]<<8)| |
| 174 |
(prefetch_buffer[core.cseip-pq_start+2]<<16)| |
| 175 |
(prefetch_buffer[core.cseip-pq_start+3]<<24); |
| 176 |
if ((core.cseip+4>=pq_start+CPU_PrefetchQueueSize-4) && |
| 177 |
(core.cseip+4<pq_start+CPU_PrefetchQueueSize)) { |
| 178 |
Bitu remaining_bytes=pq_start+CPU_PrefetchQueueSize-(core.cseip+4); |
| 179 |
for (Bitu i=0; i<remaining_bytes; i++) prefetch_buffer[i]=prefetch_buffer[core.cseip+4-pq_start+i]; |
| 180 |
for (Bitu i=remaining_bytes; i<CPU_PrefetchQueueSize; i++) prefetch_buffer[i]=LoadMb(core.cseip+4+i); |
| 181 |
pq_start=core.cseip+4; |
| 182 |
pq_valid=true; |
| 183 |
} |
| 184 |
} else { |
| 185 |
for (Bitu i=0; i<CPU_PrefetchQueueSize; i++) prefetch_buffer[i]=LoadMb(core.cseip+i); |
| 186 |
pq_start=core.cseip; |
| 187 |
pq_valid=true; |
| 188 |
temp=prefetch_buffer[0] | (prefetch_buffer[1]<<8) | |
| 189 |
(prefetch_buffer[2]<<16) | (prefetch_buffer[3]<<24); |
| 190 |
} |
| 191 |
/* if (temp!=LoadMd(core.cseip)) { |
| 192 |
LOG_MSG("prefetch queue content!=memory at %x:%x",SegValue(cs),reg_eip); |
| 193 |
} */ |
| 194 |
core.cseip+=4; |
| 195 |
return temp; |
| 196 |
} |
| 197 |
|
| 198 |
#define Push_16 CPU_Push16 |
| 199 |
#define Push_32 CPU_Push32 |
| 200 |
#define Pop_16 CPU_Pop16 |
| 201 |
#define Pop_32 CPU_Pop32 |
| 202 |
|
| 203 |
#include "instructions.h" |
| 204 |
#include "core_normal/support.h" |
| 205 |
#include "core_normal/string.h" |
| 206 |
|
| 207 |
|
| 208 |
#define EALookupTable (core.ea_table) |
| 209 |
|
| 210 |
Bits CPU_Core_Prefetch_Run(void) { |
| 211 |
bool invalidate_pq=false; |
| 212 |
while (CPU_Cycles-->0) { |
| 213 |
if (invalidate_pq) { |
| 214 |
pq_valid=false; |
| 215 |
invalidate_pq=false; |
| 216 |
} |
| 217 |
LOADIP; |
| 218 |
core.opcode_index=cpu.code.big*0x200; |
| 219 |
core.prefixes=cpu.code.big; |
| 220 |
core.ea_table=&EATable[cpu.code.big*256]; |
| 221 |
BaseDS=SegBase(ds); |
| 222 |
BaseSS=SegBase(ss); |
| 223 |
core.base_val_ds=ds; |
| 224 |
#if C_DEBUG |
| 225 |
#if C_HEAVY_DEBUG |
| 226 |
if (DEBUG_HeavyIsBreakpoint()) { |
| 227 |
FillFlags(); |
| 228 |
return debugCallback; |
| 229 |
}; |
| 230 |
#endif |
| 231 |
cycle_count++; |
| 232 |
#endif |
| 233 |
restart_opcode: |
| 234 |
Bit8u next_opcode=Fetchb(); |
| 235 |
invalidate_pq=false; |
| 236 |
if (core.opcode_index&OPCODE_0F) invalidate_pq=true; |
| 237 |
else switch (next_opcode) { |
| 238 |
case 0x70: case 0x71: case 0x72: case 0x73: |
| 239 |
case 0x74: case 0x75: case 0x76: case 0x77: |
| 240 |
case 0x78: case 0x79: case 0x7a: case 0x7b: |
| 241 |
case 0x7c: case 0x7d: case 0x7e: case 0x7f: // jcc |
| 242 |
case 0x9a: // call |
| 243 |
case 0xc2: case 0xc3: // retn |
| 244 |
case 0xc8: // enter |
| 245 |
case 0xc9: // leave |
| 246 |
case 0xca: case 0xcb: // retf |
| 247 |
case 0xcc: // int3 |
| 248 |
case 0xcd: // int |
| 249 |
case 0xce: // into |
| 250 |
case 0xcf: // iret |
| 251 |
case 0xe0: // loopnz |
| 252 |
case 0xe1: // loopz |
| 253 |
case 0xe2: // loop |
| 254 |
case 0xe3: // jcxz |
| 255 |
case 0xe8: // call |
| 256 |
case 0xe9: case 0xea: case 0xeb: // jmp |
| 257 |
case 0xff: |
| 258 |
invalidate_pq=true; |
| 259 |
break; |
| 260 |
default: |
| 261 |
break; |
| 262 |
} |
| 263 |
switch (core.opcode_index+next_opcode) { |
| 264 |
#include "core_normal/prefix_none.h" |
| 265 |
#include "core_normal/prefix_0f.h" |
| 266 |
#include "core_normal/prefix_66.h" |
| 267 |
#include "core_normal/prefix_66_0f.h" |
| 268 |
default: |
| 269 |
illegal_opcode: |
| 270 |
#if C_DEBUG |
| 271 |
{ |
| 272 |
Bitu len=(GETIP-reg_eip); |
| 273 |
LOADIP; |
| 274 |
if (len>16) len=16; |
| 275 |
char tempcode[16*2+1];char * writecode=tempcode; |
| 276 |
for (;len>0;len--) { |
| 277 |
sprintf(writecode,"%02X",mem_readb(core.cseip++)); |
| 278 |
writecode+=2; |
| 279 |
} |
| 280 |
LOG(LOG_CPU,LOG_NORMAL)("Illegal/Unhandled opcode %s",tempcode); |
| 281 |
} |
| 282 |
#endif |
| 283 |
CPU_Exception(6,0); |
| 284 |
invalidate_pq=true; |
| 285 |
continue; |
| 286 |
} |
| 287 |
SAVEIP; |
| 288 |
} |
| 289 |
FillFlags(); |
| 290 |
return CBRET_NONE; |
| 291 |
decode_end: |
| 292 |
SAVEIP; |
| 293 |
FillFlags(); |
| 294 |
return CBRET_NONE; |
| 295 |
} |
| 296 |
|
| 297 |
Bits CPU_Core_Prefetch_Trap_Run(void) { |
| 298 |
Bits oldCycles = CPU_Cycles; |
| 299 |
CPU_Cycles = 1; |
| 300 |
cpu.trap_skip = false; |
| 301 |
|
| 302 |
Bits ret=CPU_Core_Prefetch_Run(); |
| 303 |
if (!cpu.trap_skip) CPU_HW_Interrupt(1); |
| 304 |
CPU_Cycles = oldCycles-1; |
| 305 |
cpudecoder = &CPU_Core_Prefetch_Run; |
| 306 |
|
| 307 |
return ret; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
void CPU_Core_Prefetch_Init(void) { |
| 313 |
|
| 314 |
} |
| 315 |
|