--- np2/i386c/ia32/ia32.c 2004/03/12 13:34:08 1.13 +++ np2/i386c/ia32/ia32.c 2008/03/22 04:03:07 1.21 @@ -1,4 +1,4 @@ -/* $Id: ia32.c,v 1.13 2004/03/12 13:34:08 monaka Exp $ */ +/* $Id: ia32.c,v 1.21 2008/03/22 04:03:07 monaka Exp $ */ /* * Copyright (c) 2002-2003 NONAKA Kimihiro @@ -12,8 +12,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES @@ -31,7 +29,6 @@ #include "cpu.h" #include "ia32.mcr" - I386CORE i386core; UINT8 *reg8_b20[0x100]; @@ -41,7 +38,6 @@ UINT16 *reg16_b53[0x100]; UINT32 *reg32_b20[0x100]; UINT32 *reg32_b53[0x100]; - void ia32_init(void) { @@ -78,9 +74,6 @@ ia32_init(void) } resolve_init(); -#ifdef USE_FPU - fpu_init(); -#endif } void @@ -88,34 +81,73 @@ ia32_setextsize(UINT32 size) { if (CPU_EXTMEMSIZE != size) { - if (CPU_EXTMEM) { - _MFREE(CPU_EXTMEM); - CPU_EXTMEM = NULL; + UINT8 *extmem; + extmem = CPU_EXTMEM; + if (extmem != NULL) { + _MFREE(extmem); + extmem = NULL; } - if (size) { - CPU_EXTMEM = (BYTE *)_MALLOC(size + 16, "EXTMEM"); - if (CPU_EXTMEM == NULL) { - size = 0; - } - ZeroMemory(CPU_EXTMEM, size + 16); + if (size != 0) { + extmem = (UINT8 *)_MALLOC(size + 16, "EXTMEM"); + } + if (extmem != NULL) { + ZeroMemory(extmem, size + 16); + CPU_EXTMEM = extmem; + CPU_EXTMEMSIZE = size; + CPU_EXTMEMBASE = CPU_EXTMEM - 0x100000; + CPU_EXTLIMIT16 = min(size + 0x100000, 0xf00000); + CPU_EXTLIMIT = size + 0x100000; } - CPU_EXTMEMSIZE = size; + else { + CPU_EXTMEM = NULL; + CPU_EXTMEMSIZE = 0; + CPU_EXTMEMBASE = NULL; + CPU_EXTLIMIT16 = 0; + CPU_EXTLIMIT = 0; + } + } + CPU_EMSPTR[0] = mem + 0xc0000; + CPU_EMSPTR[1] = mem + 0xc4000; + CPU_EMSPTR[2] = mem + 0xc8000; + CPU_EMSPTR[3] = mem + 0xcc000; +} + +void +ia32_setemm(UINT frame, UINT32 addr) { + + BYTE *ptr; + + frame &= 3; + if (addr < USE_HIMEM) { + ptr = mem + addr; } + else if ((addr - 0x100000 + 0x4000) <= CPU_EXTMEMSIZE) { + ptr = CPU_EXTMEM + (addr - 0x100000); + } + else { + ptr = mem + 0xc0000 + (frame << 14); + } + CPU_EMSPTR[frame] = ptr; } + /* * ¥â¡¼¥ÉÁ«°Ü */ void FASTCALL change_pm(BOOL onoff) { +#if 0 int i; +#endif if (onoff) { +#if 0 for (i = 0; i < CPU_SEGREG_NUM; i++) { CPU_STAT_SREG(i).valid = 1; CPU_STAT_SREG(i).dpl = 0; } +#endif VERBOSE(("Entering to Protected-Mode...")); } else { VERBOSE(("Leaveing from Protected-Mode...")); @@ -125,10 +157,8 @@ change_pm(BOOL onoff) CPU_STATSAVE.cpu_inst_default.op_32 = CPU_STATSAVE.cpu_inst_default.as_32 = 0; CPU_STAT_SS32 = 0; - CPU_SET_CPL(0); + set_cpl(0); CPU_STAT_PM = onoff; - - tlb_flush(TRUE); } void FASTCALL @@ -140,9 +170,8 @@ change_pg(BOOL onoff) } else { VERBOSE(("Leaveing from Paging-Mode...")); } - CPU_STAT_PAGING = onoff; - tlb_flush(TRUE); + CPU_STAT_PAGING = onoff; } void FASTCALL @@ -152,15 +181,16 @@ change_vm(BOOL onoff) CPU_STAT_VM86 = onoff; if (onoff) { + VERBOSE(("Entering to Virtual-8086-Mode...")); for (i = 0; i < CPU_SEGREG_NUM; i++) { - CPU_SET_SEGREG(i, CPU_REGS_SREG(i)); + CPU_STAT_SREGLIMIT(i) = 0xffff; + LOAD_SEGREG(i, CPU_REGS_SREG(i)); } CPU_INST_OP32 = CPU_INST_AS32 = CPU_STATSAVE.cpu_inst_default.op_32 = CPU_STATSAVE.cpu_inst_default.as_32 = 0; CPU_STAT_SS32 = 0; - CPU_SET_CPL(3); - VERBOSE(("Entering to Virtual-8086-Mode...")); + set_cpl(3); } else { VERBOSE(("Leaveing from Virtual-8086-Mode...")); } @@ -169,7 +199,7 @@ change_vm(BOOL onoff) /* * flags */ -static void +static void FASTCALL modify_eflags(UINT32 new_flags, UINT32 mask) { UINT32 orig = CPU_EFLAG; @@ -191,8 +221,7 @@ modify_eflags(UINT32 new_flags, UINT32 m } } -#if !defined(IA32_DONT_USE_SET_EFLAGS_FUNCTION) -void +void FASTCALL set_flags(UINT16 new_flags, UINT16 mask) { @@ -201,7 +230,7 @@ set_flags(UINT16 new_flags, UINT16 mask) modify_eflags(new_flags, mask); } -void +void FASTCALL set_eflags(UINT32 new_flags, UINT32 mask) { @@ -210,4 +239,29 @@ set_eflags(UINT32 new_flags, UINT32 mask mask |= AC_FLAG|ID_FLAG; modify_eflags(new_flags, mask); } -#endif /* !IA32_DONT_USE_SET_EFLAGS_FUNCTION */ + +/* + * CR3 (Page Directory Entry base physical address) + */ +void FASTCALL +set_cr3(UINT32 new_cr3) +{ + + VERBOSE(("set_CR3: old = %08x, new = 0x%08x", CPU_CR3, new_cr3)); + + CPU_CR3 = new_cr3 & CPU_CR3_MASK; + CPU_STAT_PDE_BASE = CPU_CR3 & CPU_CR3_PD_MASK; + tlb_flush(0); +} + +/* + * CPL (Current Privilege Level) + */ +void FASTCALL +set_cpl(int new_cpl) +{ + int cpl = new_cpl & 3; + + CPU_STAT_CPL = (UINT8)cpl; + CPU_STAT_USER_MODE = (cpl == 3) ? CPU_MODE_USER : CPU_MODE_SUPERVISER; +}