/* PRIVATE VARIABLES **********************************************************/
-#ifndef NEW_EMULATOR
-softx86_ctx EmulatorContext;
-softx87_ctx FpuEmulatorContext;
-#else
-SOFT386_STATE EmulatorContext;
-#endif
+FAST486_STATE EmulatorContext;
static BOOLEAN A20Line = FALSE;
/* PRIVATE FUNCTIONS **********************************************************/
-static VOID EmulatorReadMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
+static VOID WINAPI EmulatorReadMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
{
- UNREFERENCED_PARAMETER(Context);
+ UNREFERENCED_PARAMETER(State);
/* If the A20 line is disabled, mask bit 20 */
if (!A20Line) Address &= ~(1 << 20);
&& (Address < VgaGetVideoLimitAddress()))
{
DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
- LPBYTE VgaBuffer = &Buffer[VgaAddress - Address];
+ LPBYTE VgaBuffer = (LPBYTE)((ULONG_PTR)Buffer + VgaAddress - Address);
/* Read from the VGA memory */
VgaReadMemory(VgaAddress, VgaBuffer, Size);
}
}
-static VOID EmulatorWriteMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
+static VOID WINAPI EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
{
- UNREFERENCED_PARAMETER(Context);
+ UNREFERENCED_PARAMETER(State);
/* If the A20 line is disabled, mask bit 20 */
if (!A20Line) Address &= ~(1 << 20);
&& (Address < VgaGetVideoLimitAddress()))
{
DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
- LPBYTE VgaBuffer = &Buffer[VgaAddress - Address];
+ LPBYTE VgaBuffer = (LPBYTE)((ULONG_PTR)Buffer + VgaAddress - Address);
/* Write to the VGA memory */
VgaWriteMemory(VgaAddress, VgaBuffer, Size);
}
}
-static VOID EmulatorReadIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
+static VOID WINAPI EmulatorReadIo(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG Size)
{
- UNREFERENCED_PARAMETER(Context);
+ LPBYTE Address = (LPBYTE)Buffer;
+
+ UNREFERENCED_PARAMETER(State);
UNREFERENCED_PARAMETER(Size);
- switch (Address)
+ switch (Port)
{
case PIC_MASTER_CMD:
case PIC_SLAVE_CMD:
{
- *Buffer = PicReadCommand(Address);
+ *Address = PicReadCommand(Port);
break;
}
case PIC_MASTER_DATA:
case PIC_SLAVE_DATA:
{
- *Buffer = PicReadData(Address);
+ *Address = PicReadData(Port);
break;
}
case PIT_DATA_PORT(1):
case PIT_DATA_PORT(2):
{
- *Buffer = PitReadData(Address - PIT_DATA_PORT(0));
+ *Address = PitReadData(Port - PIT_DATA_PORT(0));
break;
}
case PS2_CONTROL_PORT:
{
- *Buffer = KeyboardReadStatus();
+ *Address = KeyboardReadStatus();
break;
}
case PS2_DATA_PORT:
{
- *Buffer = KeyboardReadData();
+ *Address = KeyboardReadData();
break;
}
case VGA_STAT_MONO:
case VGA_STAT_COLOR:
{
- *Buffer = VgaReadPort(Address);
+ *Address = VgaReadPort(Port);
break;
}
default:
{
- DPRINT1("Read from unknown port: 0x%X\n", Address);
+ DPRINT1("Read from unknown port: 0x%X\n", Port);
}
}
}
-static VOID EmulatorWriteIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
+static VOID WINAPI EmulatorWriteIo(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG Size)
{
- BYTE Byte = *Buffer;
+ BYTE Byte = *(LPBYTE)Buffer;
- UNREFERENCED_PARAMETER(Context);
+ UNREFERENCED_PARAMETER(State);
UNREFERENCED_PARAMETER(Size);
- switch (Address)
+ switch (Port)
{
case PIT_COMMAND_PORT:
{
case PIT_DATA_PORT(1):
case PIT_DATA_PORT(2):
{
- PitWriteData(Address - PIT_DATA_PORT(0), Byte);
+ PitWriteData(Port - PIT_DATA_PORT(0), Byte);
break;
}
case PIC_MASTER_CMD:
case PIC_SLAVE_CMD:
{
- PicWriteCommand(Address, Byte);
+ PicWriteCommand(Port, Byte);
break;
}
case PIC_MASTER_DATA:
case PIC_SLAVE_DATA:
{
- PicWriteData(Address, Byte);
+ PicWriteData(Port, Byte);
break;
}
case VGA_STAT_MONO:
case VGA_STAT_COLOR:
{
- VgaWritePort(Address, Byte);
+ VgaWritePort(Port, Byte);
break;
}
default:
{
- DPRINT1("Write to unknown port: 0x%X\n", Address);
+ DPRINT1("Write to unknown port: 0x%X\n", Port);
}
}
}
-#ifndef NEW_EMULATOR
-
-static VOID EmulatorBop(WORD Code)
+static VOID WINAPI EmulatorBiosOperation(PFAST486_STATE State, USHORT BopCode)
{
WORD StackSegment, StackPointer, CodeSegment, InstructionPointer;
BYTE IntNum;
LPWORD Stack;
/* Get the SS:SP */
-#ifndef NEW_EMULATOR
- StackSegment = EmulatorContext.state->segment_reg[SX86_SREG_SS].val;
- StackPointer = EmulatorContext.state->general_reg[SX86_REG_SP].val;
-#else
- StackSegment = EmulatorContext.SegmentRegs[SOFT386_REG_SS].LowWord;
- StackPointer = EmulatorContext.SegmentRegs[SOFT386_REG_SP].LowWord;
-#endif
+ StackSegment = State->SegmentRegs[FAST486_REG_SS].Selector;
+ StackPointer = State->GeneralRegs[FAST486_REG_ESP].LowWord;
/* Get the stack */
- Stack = (LPWORD)((ULONG_PTR)BaseAddress + TO_LINEAR(StackSegment, StackPointer));
+ Stack = (LPWORD)SEG_OFF_TO_PTR(StackSegment, StackPointer);
- if (Code == EMULATOR_INT_BOP)
+ if (BopCode == EMULATOR_INT_BOP)
{
/* Get the interrupt number */
IntNum = LOBYTE(Stack[STACK_INT_NUM]);
}
}
-static VOID EmulatorSoftwareInt(PVOID Context, BYTE Number)
-{
- UNREFERENCED_PARAMETER(Context);
- UNREFERENCED_PARAMETER(Number);
-
- /* Do nothing */
-}
-
-static VOID EmulatorHardwareInt(PVOID Context, BYTE Number)
+static UCHAR WINAPI EmulatorIntAcknowledge(PFAST486_STATE State)
{
- UNREFERENCED_PARAMETER(Context);
- UNREFERENCED_PARAMETER(Number);
+ UNREFERENCED_PARAMETER(State);
- /* Do nothing */
+ /* Get the interrupt number from the PIC */
+ return PicGetInterrupt();
}
-static VOID EmulatorHardwareIntAck(PVOID Context, BYTE Number)
-{
- UNREFERENCED_PARAMETER(Context);
- UNREFERENCED_PARAMETER(Number);
-
- /* Do nothing */
-}
-
-#endif
-
/* PUBLIC FUNCTIONS ***********************************************************/
BOOLEAN EmulatorInitialize()
BaseAddress = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_ADDRESS);
if (BaseAddress == NULL) return FALSE;
-#ifndef NEW_EMULATOR
- /* Initialize the softx86 CPU emulator */
- if (!softx86_init(&EmulatorContext, SX86_CPULEVEL_80286))
- {
- HeapFree(GetProcessHeap(), 0, BaseAddress);
- return FALSE;
- }
-
- /* Initialize the softx87 FPU emulator*/
- if(!softx87_init(&FpuEmulatorContext, SX87_FPULEVEL_8087))
- {
- softx86_free(&EmulatorContext);
- HeapFree(GetProcessHeap(), 0, BaseAddress);
- return FALSE;
- }
-
- /* Set memory read/write callbacks */
- EmulatorContext.callbacks->on_read_memory = EmulatorReadMemory;
- EmulatorContext.callbacks->on_write_memory = EmulatorWriteMemory;
-
- /* Set MMIO read/write callbacks */
- EmulatorContext.callbacks->on_read_io = EmulatorReadIo;
- EmulatorContext.callbacks->on_write_io = EmulatorWriteIo;
-
- /* Set interrupt callbacks */
- EmulatorContext.callbacks->on_sw_int = EmulatorSoftwareInt;
- EmulatorContext.callbacks->on_hw_int = EmulatorHardwareInt;
- EmulatorContext.callbacks->on_hw_int_ack = EmulatorHardwareIntAck;
-
- /* Connect the emulated FPU to the emulated CPU */
- softx87_connect_to_CPU(&EmulatorContext, &FpuEmulatorContext);
-#else
/* Set the callbacks */
- EmulatorContext.MemReadCallback = (SOFT386_MEM_READ_PROC)EmulatorReadMemory;
- EmulatorContext.MemWriteCallback = (SOFT386_MEM_WRITE_PROC)EmulatorWriteMemory;
- EmulatorContext.IoReadCallback = (SOFT386_IO_READ_PROC)EmulatorReadIo;
- EmulatorContext.IoWriteCallback = (SOFT386_IO_WRITE_PROC)EmulatorWriteIo;
+ EmulatorContext.MemReadCallback = EmulatorReadMemory;
+ EmulatorContext.MemWriteCallback = EmulatorWriteMemory;
+ EmulatorContext.IoReadCallback = EmulatorReadIo;
+ EmulatorContext.IoWriteCallback = EmulatorWriteIo;
+ EmulatorContext.BopCallback = EmulatorBiosOperation;
+ EmulatorContext.IntAckCallback = EmulatorIntAcknowledge;
/* Reset the CPU */
- Soft386Reset(&EmulatorContext);
-#endif
+ Fast486Reset(&EmulatorContext);
/* Enable interrupts */
EmulatorSetFlag(EMULATOR_FLAG_IF);
VOID EmulatorSetStack(WORD Segment, DWORD Offset)
{
-#ifndef NEW_EMULATOR
- /* Call the softx86 API */
- softx86_set_stack_ptr(&EmulatorContext, Segment, Offset);
-#else
- Soft386SetStack(&EmulatorContext, Segment, Offset);
-#endif
+ Fast486SetStack(&EmulatorContext, Segment, Offset);
}
// FIXME: This function assumes 16-bit mode!!!
VOID EmulatorExecute(WORD Segment, WORD Offset)
{
-#ifndef NEW_EMULATOR
- /* Call the softx86 API */
- softx86_set_instruction_ptr(&EmulatorContext, Segment, Offset);
-#else
- /* Tell Soft386 to move the instruction pointer */
- Soft386ExecuteAt(&EmulatorContext, Segment, Offset);
-#endif
+ /* Tell Fast486 to move the instruction pointer */
+ Fast486ExecuteAt(&EmulatorContext, Segment, Offset);
}
VOID EmulatorInterrupt(BYTE Number)
{
- LPDWORD IntVecTable = (LPDWORD)((ULONG_PTR)BaseAddress);
- UINT Segment, Offset;
-
- /* Get the segment and offset */
- Segment = HIWORD(IntVecTable[Number]);
- Offset = LOWORD(IntVecTable[Number]);
-
-#ifndef NEW_EMULATOR
- /* Call the softx86 API */
- softx86_make_simple_interrupt_call(&EmulatorContext, &Segment, &Offset);
-#else
- UNREFERENCED_PARAMETER(Segment);
- UNREFERENCED_PARAMETER(Offset);
- // TODO: NOT IMPLEMENTED
-#endif
+ /* Call the Fast486 API */
+ Fast486Interrupt(&EmulatorContext, Number);
}
-VOID EmulatorExternalInterrupt(BYTE Number)
+VOID EmulatorInterruptSignal(VOID)
{
-#ifndef NEW_EMULATOR
- /* Call the softx86 API */
- softx86_ext_hw_signal(&EmulatorContext, Number);
-#endif
+ /* Call the Fast486 API */
+ Fast486InterruptSignal(&EmulatorContext);
}
ULONG EmulatorGetRegister(ULONG Register)
{
-#ifndef NEW_EMULATOR
- if (Register < EMULATOR_REG_ES)
- {
- return EmulatorContext.state->general_reg[Register].val;
- }
- else
- {
- return EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val;
- }
-#else
if (Register < EMULATOR_REG_ES)
{
return EmulatorContext.GeneralRegs[Register].Long;
{
return EmulatorContext.SegmentRegs[Register - EMULATOR_REG_ES].Selector;
}
-#endif
-}
-
-ULONG EmulatorGetProgramCounter(VOID)
-{
-#ifndef NEW_EMULATOR
- return EmulatorContext.state->reg_ip;
-#else
- return EmulatorContext.InstPtr.Long;
-#endif
}
VOID EmulatorSetRegister(ULONG Register, ULONG Value)
{
-#ifndef NEW_EMULATOR
- if (Register < EMULATOR_REG_CS)
+ if (Register < EMULATOR_REG_ES)
{
- EmulatorContext.state->general_reg[Register].val = Value;
+ EmulatorContext.GeneralRegs[Register].Long = Value;
}
else
{
- EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val = (WORD)Value;
+ Fast486SetSegment(&EmulatorContext, Register - EMULATOR_REG_ES, (USHORT)Value);
}
-#else
- // TODO: NOT IMPLEMENTED
-#endif
+}
+
+ULONG EmulatorGetProgramCounter(VOID)
+{
+ return EmulatorContext.InstPtr.Long;
}
BOOLEAN EmulatorGetFlag(ULONG Flag)
{
-#ifndef NEW_EMULATOR
- return (EmulatorContext.state->reg_flags.val & Flag) ? TRUE : FALSE;
-#else
return (EmulatorContext.Flags.Long & Flag) ? TRUE : FALSE;
-#endif
}
VOID EmulatorSetFlag(ULONG Flag)
{
-#ifndef NEW_EMULATOR
- EmulatorContext.state->reg_flags.val |= Flag;
-#else
EmulatorContext.Flags.Long |= Flag;
-#endif
}
VOID EmulatorClearFlag(ULONG Flag)
{
-#ifndef NEW_EMULATOR
- EmulatorContext.state->reg_flags.val &= ~Flag;
-#else
EmulatorContext.Flags.Long &= ~Flag;
-#endif
}
VOID EmulatorStep(VOID)
{
-#ifndef NEW_EMULATOR
- LPWORD Instruction;
-
- /* Print the current position - useful for debugging */
- DPRINT("Executing at CS:IP = %04X:%04X\n",
- EmulatorGetRegister(EMULATOR_REG_CS),
- EmulatorContext.state->reg_ip);
-
- Instruction = (LPWORD)((ULONG_PTR)BaseAddress
- + TO_LINEAR(EmulatorGetRegister(EMULATOR_REG_CS),
- EmulatorContext.state->reg_ip));
-
- /* Check for the BIOS operation (BOP) sequence */
- if (Instruction[0] == EMULATOR_BOP)
- {
- /* Skip the opcodes */
- EmulatorContext.state->reg_ip += 4;
-
- // HACK: Refresh the display because the called function may wait.
- VgaRefreshDisplay();
-
- /* Call the BOP handler */
- EmulatorBop(Instruction[1]);
- }
-
- /* Call the softx86 API */
- if (!softx86_step(&EmulatorContext))
- {
- /* Invalid opcode */
- EmulatorInterrupt(EMULATOR_EXCEPTION_INVALID_OPCODE);
- }
-#else
/* Dump the state for debugging purposes */
- Soft386DumpState(&EmulatorContext);
+ // Fast486DumpState(&EmulatorContext);
/* Execute the next instruction */
- Soft386StepInto(&EmulatorContext);
-#endif
+ Fast486StepInto(&EmulatorContext);
}
VOID EmulatorCleanup(VOID)
{
-#ifndef NEW_EMULATOR
- /* Free the softx86 CPU and FPU emulator */
- softx87_free(&FpuEmulatorContext);
- softx86_free(&EmulatorContext);
-#endif
-
/* Free the memory allocated for the 16-bit address space */
if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
}