2013-06-17 00:00:36 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: GPL - See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS Virtual DOS Machine
|
|
|
|
* FILE: emulator.c
|
|
|
|
* PURPOSE: Minimal x86 machine emulator for the VDM
|
|
|
|
* PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
2013-07-22 13:51:26 +00:00
|
|
|
#define NDEBUG
|
|
|
|
|
2013-06-26 22:58:41 +00:00
|
|
|
#include "emulator.h"
|
|
|
|
#include "bios.h"
|
|
|
|
#include "dos.h"
|
2013-08-05 23:20:25 +00:00
|
|
|
#include "vga.h"
|
2013-06-26 22:58:41 +00:00
|
|
|
#include "pic.h"
|
|
|
|
#include "ps2.h"
|
|
|
|
#include "timer.h"
|
2013-06-17 00:00:36 +00:00
|
|
|
|
2013-06-26 22:58:41 +00:00
|
|
|
/* PRIVATE VARIABLES **********************************************************/
|
|
|
|
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
|
|
|
softx86_ctx EmulatorContext;
|
|
|
|
softx87_ctx FpuEmulatorContext;
|
|
|
|
#else
|
2013-08-16 19:33:04 +00:00
|
|
|
SOFT386_STATE EmulatorContext;
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-26 17:15:45 +00:00
|
|
|
static BOOLEAN A20Line = FALSE;
|
2013-06-17 00:00:36 +00:00
|
|
|
|
2013-06-26 22:58:41 +00:00
|
|
|
/* PRIVATE FUNCTIONS **********************************************************/
|
|
|
|
|
2013-09-28 00:29:16 +00:00
|
|
|
static VOID NTVDMCALL EmulatorReadMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
2013-08-07 19:56:28 +00:00
|
|
|
UNREFERENCED_PARAMETER(Context);
|
|
|
|
|
2013-06-26 17:15:45 +00:00
|
|
|
/* If the A20 line is disabled, mask bit 20 */
|
|
|
|
if (!A20Line) Address &= ~(1 << 20);
|
|
|
|
|
2013-06-17 00:00:36 +00:00
|
|
|
/* Make sure the requested address is valid */
|
|
|
|
if ((Address + Size) >= MAX_ADDRESS) return;
|
|
|
|
|
|
|
|
/* Read the data from the virtual address space and store it in the buffer */
|
|
|
|
RtlCopyMemory(Buffer, (LPVOID)((ULONG_PTR)BaseAddress + Address), Size);
|
2013-08-05 23:20:25 +00:00
|
|
|
|
|
|
|
/* Check if we modified the console video memory */
|
|
|
|
if (((Address + Size) >= VgaGetVideoBaseAddress())
|
|
|
|
&& (Address < VgaGetVideoLimitAddress()))
|
|
|
|
{
|
|
|
|
DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
|
|
|
|
LPBYTE VgaBuffer = &Buffer[VgaAddress - Address];
|
|
|
|
|
|
|
|
/* Read from the VGA memory */
|
|
|
|
VgaReadMemory(VgaAddress, VgaBuffer, Size);
|
|
|
|
}
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-09-28 00:29:16 +00:00
|
|
|
static VOID NTVDMCALL EmulatorWriteMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
2013-08-07 19:56:28 +00:00
|
|
|
UNREFERENCED_PARAMETER(Context);
|
|
|
|
|
2013-06-26 17:15:45 +00:00
|
|
|
/* If the A20 line is disabled, mask bit 20 */
|
|
|
|
if (!A20Line) Address &= ~(1 << 20);
|
|
|
|
|
2013-06-17 00:00:36 +00:00
|
|
|
/* Make sure the requested address is valid */
|
|
|
|
if ((Address + Size) >= MAX_ADDRESS) return;
|
|
|
|
|
|
|
|
/* Make sure we don't write to the ROM area */
|
|
|
|
if ((Address + Size) >= ROM_AREA_START && (Address < ROM_AREA_END)) return;
|
|
|
|
|
|
|
|
/* Read the data from the buffer and store it in the virtual address space */
|
|
|
|
RtlCopyMemory((LPVOID)((ULONG_PTR)BaseAddress + Address), Buffer, Size);
|
|
|
|
|
|
|
|
/* Check if we modified the console video memory */
|
2013-08-05 23:20:25 +00:00
|
|
|
if (((Address + Size) >= VgaGetVideoBaseAddress())
|
|
|
|
&& (Address < VgaGetVideoLimitAddress()))
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
2013-08-05 23:20:25 +00:00
|
|
|
DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
|
|
|
|
LPBYTE VgaBuffer = &Buffer[VgaAddress - Address];
|
|
|
|
|
|
|
|
/* Write to the VGA memory */
|
|
|
|
VgaWriteMemory(VgaAddress, VgaBuffer, Size);
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 00:29:16 +00:00
|
|
|
static VOID NTVDMCALL EmulatorReadIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
2013-08-07 19:56:28 +00:00
|
|
|
UNREFERENCED_PARAMETER(Context);
|
|
|
|
UNREFERENCED_PARAMETER(Size);
|
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
switch (Address)
|
|
|
|
{
|
|
|
|
case PIC_MASTER_CMD:
|
|
|
|
case PIC_SLAVE_CMD:
|
|
|
|
{
|
|
|
|
*Buffer = PicReadCommand(Address);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
case PIC_MASTER_DATA:
|
|
|
|
case PIC_SLAVE_DATA:
|
|
|
|
{
|
|
|
|
*Buffer = PicReadData(Address);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-26 17:15:45 +00:00
|
|
|
|
|
|
|
case PIT_DATA_PORT(0):
|
|
|
|
case PIT_DATA_PORT(1):
|
|
|
|
case PIT_DATA_PORT(2):
|
|
|
|
{
|
|
|
|
*Buffer = PitReadData(Address - PIT_DATA_PORT(0));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PS2_CONTROL_PORT:
|
|
|
|
{
|
|
|
|
*Buffer = KeyboardReadStatus();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PS2_DATA_PORT:
|
|
|
|
{
|
|
|
|
*Buffer = KeyboardReadData();
|
|
|
|
break;
|
|
|
|
}
|
2013-07-23 21:43:57 +00:00
|
|
|
|
2013-08-05 23:20:25 +00:00
|
|
|
case VGA_AC_WRITE:
|
|
|
|
case VGA_AC_READ:
|
|
|
|
case VGA_SEQ_INDEX:
|
|
|
|
case VGA_SEQ_DATA:
|
|
|
|
case VGA_DAC_READ_INDEX:
|
|
|
|
case VGA_DAC_WRITE_INDEX:
|
|
|
|
case VGA_DAC_DATA:
|
|
|
|
case VGA_MISC_READ:
|
|
|
|
case VGA_MISC_WRITE:
|
|
|
|
case VGA_CRTC_INDEX:
|
|
|
|
case VGA_CRTC_DATA:
|
|
|
|
case VGA_GC_INDEX:
|
|
|
|
case VGA_GC_DATA:
|
|
|
|
case VGA_STAT_MONO:
|
|
|
|
case VGA_STAT_COLOR:
|
|
|
|
{
|
|
|
|
*Buffer = VgaReadPort(Address);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-23 21:43:57 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
DPRINT1("Read from unknown port: 0x%X\n", Address);
|
|
|
|
}
|
2013-06-21 00:47:07 +00:00
|
|
|
}
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-09-28 00:29:16 +00:00
|
|
|
static VOID NTVDMCALL EmulatorWriteIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
2013-06-21 00:47:07 +00:00
|
|
|
BYTE Byte = *Buffer;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-08-07 19:56:28 +00:00
|
|
|
UNREFERENCED_PARAMETER(Context);
|
|
|
|
UNREFERENCED_PARAMETER(Size);
|
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
switch (Address)
|
|
|
|
{
|
|
|
|
case PIT_COMMAND_PORT:
|
|
|
|
{
|
|
|
|
PitWriteCommand(Byte);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
case PIT_DATA_PORT(0):
|
|
|
|
case PIT_DATA_PORT(1):
|
|
|
|
case PIT_DATA_PORT(2):
|
|
|
|
{
|
|
|
|
PitWriteData(Address - PIT_DATA_PORT(0), Byte);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
case PIC_MASTER_CMD:
|
|
|
|
case PIC_SLAVE_CMD:
|
|
|
|
{
|
|
|
|
PicWriteCommand(Address, Byte);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
case PIC_MASTER_DATA:
|
|
|
|
case PIC_SLAVE_DATA:
|
|
|
|
{
|
|
|
|
PicWriteData(Address, Byte);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-26 17:15:45 +00:00
|
|
|
|
|
|
|
case PS2_CONTROL_PORT:
|
|
|
|
{
|
|
|
|
KeyboardWriteCommand(Byte);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PS2_DATA_PORT:
|
|
|
|
{
|
|
|
|
KeyboardWriteData(Byte);
|
|
|
|
break;
|
|
|
|
}
|
2013-07-23 21:43:57 +00:00
|
|
|
|
2013-08-05 23:20:25 +00:00
|
|
|
case VGA_AC_WRITE:
|
|
|
|
case VGA_AC_READ:
|
|
|
|
case VGA_SEQ_INDEX:
|
|
|
|
case VGA_SEQ_DATA:
|
|
|
|
case VGA_DAC_READ_INDEX:
|
|
|
|
case VGA_DAC_WRITE_INDEX:
|
|
|
|
case VGA_DAC_DATA:
|
|
|
|
case VGA_MISC_READ:
|
|
|
|
case VGA_MISC_WRITE:
|
|
|
|
case VGA_CRTC_INDEX:
|
|
|
|
case VGA_CRTC_DATA:
|
|
|
|
case VGA_GC_INDEX:
|
|
|
|
case VGA_GC_DATA:
|
|
|
|
case VGA_STAT_MONO:
|
|
|
|
case VGA_STAT_COLOR:
|
|
|
|
{
|
|
|
|
VgaWritePort(Address, Byte);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-23 21:43:57 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
DPRINT1("Write to unknown port: 0x%X\n", Address);
|
|
|
|
}
|
2013-06-21 00:47:07 +00:00
|
|
|
}
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
static VOID EmulatorBop(WORD Code)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
|
|
|
WORD StackSegment, StackPointer, CodeSegment, InstructionPointer;
|
|
|
|
BYTE IntNum;
|
2013-07-23 19:31:00 +00:00
|
|
|
LPWORD Stack;
|
2013-06-17 00:00:36 +00:00
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
/* Get the SS:SP */
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-07-23 19:31:00 +00:00
|
|
|
StackSegment = EmulatorContext.state->segment_reg[SX86_SREG_SS].val;
|
|
|
|
StackPointer = EmulatorContext.state->general_reg[SX86_REG_SP].val;
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
2013-09-28 00:29:16 +00:00
|
|
|
StackSegment = EmulatorContext.SegmentRegs[SOFT386_REG_SS].Selector;
|
|
|
|
StackPointer = EmulatorContext.GeneralRegs[SOFT386_REG_ESP].LowWord;
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
/* Get the stack */
|
|
|
|
Stack = (LPWORD)((ULONG_PTR)BaseAddress + TO_LINEAR(StackSegment, StackPointer));
|
2013-06-17 00:00:36 +00:00
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
if (Code == EMULATOR_INT_BOP)
|
|
|
|
{
|
|
|
|
/* Get the interrupt number */
|
2013-07-23 21:43:57 +00:00
|
|
|
IntNum = LOBYTE(Stack[STACK_INT_NUM]);
|
2013-06-17 00:00:36 +00:00
|
|
|
|
|
|
|
/* Get the CS:IP */
|
2013-07-23 21:43:57 +00:00
|
|
|
InstructionPointer = Stack[STACK_IP];
|
|
|
|
CodeSegment = Stack[STACK_CS];
|
2013-06-17 00:00:36 +00:00
|
|
|
|
|
|
|
/* Check if this was an exception */
|
|
|
|
if (IntNum < 8)
|
|
|
|
{
|
|
|
|
/* Display a message to the user */
|
|
|
|
DisplayMessage(L"Exception: %s occured at %04X:%04X",
|
|
|
|
ExceptionName[IntNum],
|
|
|
|
CodeSegment,
|
|
|
|
InstructionPointer);
|
|
|
|
|
|
|
|
/* Stop the VDM */
|
|
|
|
VdmRunning = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
/* Check if this was an PIC IRQ */
|
|
|
|
if (IntNum >= BIOS_PIC_MASTER_INT && IntNum < BIOS_PIC_MASTER_INT + 8)
|
|
|
|
{
|
|
|
|
/* It was an IRQ from the master PIC */
|
2013-07-23 21:43:57 +00:00
|
|
|
BiosHandleIrq(IntNum - BIOS_PIC_MASTER_INT, Stack);
|
2013-06-29 21:37:44 +00:00
|
|
|
return;
|
2013-06-21 00:47:07 +00:00
|
|
|
}
|
|
|
|
else if (IntNum >= BIOS_PIC_SLAVE_INT && IntNum < BIOS_PIC_SLAVE_INT + 8)
|
|
|
|
{
|
|
|
|
/* It was an IRQ from the slave PIC */
|
2013-07-23 21:43:57 +00:00
|
|
|
BiosHandleIrq(IntNum - BIOS_PIC_SLAVE_INT + 8, Stack);
|
2013-06-29 21:37:44 +00:00
|
|
|
return;
|
2013-06-21 00:47:07 +00:00
|
|
|
}
|
2013-06-17 00:00:36 +00:00
|
|
|
|
|
|
|
switch (IntNum)
|
|
|
|
{
|
2013-06-29 21:37:44 +00:00
|
|
|
case BIOS_VIDEO_INTERRUPT:
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
|
|
|
/* This is the video BIOS interrupt, call the BIOS */
|
2013-07-23 21:43:57 +00:00
|
|
|
BiosVideoService(Stack);
|
2013-06-17 00:00:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-07-02 02:08:30 +00:00
|
|
|
case BIOS_EQUIPMENT_INTERRUPT:
|
|
|
|
{
|
|
|
|
/* This is the BIOS "get equipment" command, call the BIOS */
|
2013-07-23 21:43:57 +00:00
|
|
|
BiosEquipmentService(Stack);
|
2013-07-02 02:08:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-29 21:37:44 +00:00
|
|
|
case BIOS_KBD_INTERRUPT:
|
2013-06-28 20:52:40 +00:00
|
|
|
{
|
|
|
|
/* This is the keyboard BIOS interrupt, call the BIOS */
|
2013-07-23 21:43:57 +00:00
|
|
|
BiosKeyboardService(Stack);
|
2013-06-28 20:52:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-29 21:37:44 +00:00
|
|
|
case BIOS_TIME_INTERRUPT:
|
|
|
|
{
|
|
|
|
/* This is the time BIOS interrupt, call the BIOS */
|
2013-07-23 21:43:57 +00:00
|
|
|
BiosTimeService(Stack);
|
2013-06-29 21:37:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-07-05 01:31:50 +00:00
|
|
|
case BIOS_SYS_TIMER_INTERRUPT:
|
|
|
|
{
|
|
|
|
/* BIOS timer update */
|
2013-07-23 21:43:57 +00:00
|
|
|
BiosSystemTimerInterrupt(Stack);
|
2013-07-05 01:31:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-17 00:00:36 +00:00
|
|
|
case 0x20:
|
|
|
|
{
|
2013-07-23 21:43:57 +00:00
|
|
|
DosInt20h(Stack);
|
2013-06-17 00:00:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 0x21:
|
|
|
|
{
|
2013-07-23 21:43:57 +00:00
|
|
|
DosInt21h(Stack);
|
2013-06-17 00:00:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 0x23:
|
|
|
|
{
|
2013-07-23 21:43:57 +00:00
|
|
|
DosBreakInterrupt(Stack);
|
2013-06-17 00:00:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-29 21:37:44 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
DPRINT1("Unhandled interrupt: 0x%02X\n", IntNum);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 00:29:16 +00:00
|
|
|
#ifdef NEW_EMULATOR
|
|
|
|
static VOID WINAPI EmulatorBiosOperation(PSOFT386_STATE State, WORD Code)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* HACK: To maintain softx86 compatbility, just call the old EmulatorBop here.
|
|
|
|
* Later on, when softx86 is no longer needed, the code from EmulatorBop should
|
|
|
|
* be moved here and should use the "State" variable.
|
|
|
|
*/
|
|
|
|
EmulatorBop(Code);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NEW_EMULATOR
|
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
static VOID EmulatorSoftwareInt(PVOID Context, BYTE Number)
|
|
|
|
{
|
2013-08-07 19:56:28 +00:00
|
|
|
UNREFERENCED_PARAMETER(Context);
|
|
|
|
UNREFERENCED_PARAMETER(Number);
|
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
|
2013-06-26 17:15:45 +00:00
|
|
|
static VOID EmulatorHardwareInt(PVOID Context, BYTE Number)
|
|
|
|
{
|
2013-08-07 19:56:28 +00:00
|
|
|
UNREFERENCED_PARAMETER(Context);
|
|
|
|
UNREFERENCED_PARAMETER(Number);
|
|
|
|
|
2013-06-26 17:15:45 +00:00
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID EmulatorHardwareIntAck(PVOID Context, BYTE Number)
|
|
|
|
{
|
2013-08-07 19:56:28 +00:00
|
|
|
UNREFERENCED_PARAMETER(Context);
|
|
|
|
UNREFERENCED_PARAMETER(Number);
|
|
|
|
|
2013-06-26 17:15:45 +00:00
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-17 00:00:36 +00:00
|
|
|
/* PUBLIC FUNCTIONS ***********************************************************/
|
|
|
|
|
|
|
|
BOOLEAN EmulatorInitialize()
|
|
|
|
{
|
|
|
|
/* Allocate memory for the 16-bit address space */
|
2013-07-02 02:08:30 +00:00
|
|
|
BaseAddress = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_ADDRESS);
|
2013-06-17 00:00:36 +00:00
|
|
|
if (BaseAddress == NULL) return FALSE;
|
|
|
|
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-06-17 00:00:36 +00:00
|
|
|
/* Initialize the softx86 CPU emulator */
|
2013-06-26 17:15:45 +00:00
|
|
|
if (!softx86_init(&EmulatorContext, SX86_CPULEVEL_80286))
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
|
|
|
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;
|
2013-06-26 17:15:45 +00:00
|
|
|
EmulatorContext.callbacks->on_hw_int = EmulatorHardwareInt;
|
|
|
|
EmulatorContext.callbacks->on_hw_int_ack = EmulatorHardwareIntAck;
|
2013-06-17 00:00:36 +00:00
|
|
|
|
|
|
|
/* Connect the emulated FPU to the emulated CPU */
|
|
|
|
softx87_connect_to_CPU(&EmulatorContext, &FpuEmulatorContext);
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
2013-08-31 19:18:12 +00:00
|
|
|
/* 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;
|
2013-09-28 00:29:16 +00:00
|
|
|
EmulatorContext.BopCallback = (SOFT386_BOP_PROC)EmulatorBiosOperation;
|
2013-08-31 19:18:12 +00:00
|
|
|
|
|
|
|
/* Reset the CPU */
|
|
|
|
Soft386Reset(&EmulatorContext);
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
|
2013-06-26 17:15:45 +00:00
|
|
|
/* Enable interrupts */
|
|
|
|
EmulatorSetFlag(EMULATOR_FLAG_IF);
|
|
|
|
|
2013-06-17 00:00:36 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-08-09 23:52:08 +00:00
|
|
|
VOID EmulatorSetStack(WORD Segment, DWORD Offset)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-06-17 00:00:36 +00:00
|
|
|
/* Call the softx86 API */
|
|
|
|
softx86_set_stack_ptr(&EmulatorContext, Segment, Offset);
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
2013-09-21 00:41:41 +00:00
|
|
|
Soft386SetStack(&EmulatorContext, Segment, Offset);
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-08-31 19:18:12 +00:00
|
|
|
// FIXME: This function assumes 16-bit mode!!!
|
2013-06-17 00:00:36 +00:00
|
|
|
VOID EmulatorExecute(WORD Segment, WORD Offset)
|
|
|
|
{
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-06-17 00:00:36 +00:00
|
|
|
/* Call the softx86 API */
|
|
|
|
softx86_set_instruction_ptr(&EmulatorContext, Segment, Offset);
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
2013-08-31 19:18:12 +00:00
|
|
|
/* Tell Soft386 to move the instruction pointer */
|
|
|
|
Soft386ExecuteAt(&EmulatorContext, Segment, Offset);
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID EmulatorInterrupt(BYTE Number)
|
|
|
|
{
|
2013-10-12 13:58:34 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-06-21 00:47:07 +00:00
|
|
|
LPDWORD IntVecTable = (LPDWORD)((ULONG_PTR)BaseAddress);
|
2013-06-17 00:00:36 +00:00
|
|
|
UINT Segment, Offset;
|
|
|
|
|
|
|
|
/* Get the segment and offset */
|
|
|
|
Segment = HIWORD(IntVecTable[Number]);
|
|
|
|
Offset = LOWORD(IntVecTable[Number]);
|
|
|
|
|
|
|
|
/* Call the softx86 API */
|
|
|
|
softx86_make_simple_interrupt_call(&EmulatorContext, &Segment, &Offset);
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
2013-10-12 13:58:34 +00:00
|
|
|
/* Call the Soft386 API */
|
2013-10-18 22:50:00 +00:00
|
|
|
Soft386Interrupt(&EmulatorContext, Number);
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 17:15:45 +00:00
|
|
|
VOID EmulatorExternalInterrupt(BYTE Number)
|
|
|
|
{
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-06-26 17:15:45 +00:00
|
|
|
/* Call the softx86 API */
|
|
|
|
softx86_ext_hw_signal(&EmulatorContext, Number);
|
2013-10-12 13:58:34 +00:00
|
|
|
#else
|
|
|
|
/* Call the Soft386 API */
|
2013-10-18 22:50:00 +00:00
|
|
|
Soft386Interrupt(&EmulatorContext, Number);
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-06-26 17:15:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-17 00:00:36 +00:00
|
|
|
ULONG EmulatorGetRegister(ULONG Register)
|
|
|
|
{
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-06-24 01:59:09 +00:00
|
|
|
if (Register < EMULATOR_REG_ES)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
|
|
|
return EmulatorContext.state->general_reg[Register].val;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-24 01:59:09 +00:00
|
|
|
return EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val;
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
2013-08-16 19:33:04 +00:00
|
|
|
if (Register < EMULATOR_REG_ES)
|
|
|
|
{
|
|
|
|
return EmulatorContext.GeneralRegs[Register].Long;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return EmulatorContext.SegmentRegs[Register - EMULATOR_REG_ES].Selector;
|
|
|
|
}
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-08-09 23:52:08 +00:00
|
|
|
ULONG EmulatorGetProgramCounter(VOID)
|
|
|
|
{
|
|
|
|
#ifndef NEW_EMULATOR
|
|
|
|
return EmulatorContext.state->reg_ip;
|
|
|
|
#else
|
2013-08-16 19:33:04 +00:00
|
|
|
return EmulatorContext.InstPtr.Long;
|
2013-08-09 23:52:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-06-17 00:00:36 +00:00
|
|
|
VOID EmulatorSetRegister(ULONG Register, ULONG Value)
|
|
|
|
{
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-10-09 21:48:52 +00:00
|
|
|
if (Register < EMULATOR_REG_ES)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
|
|
|
EmulatorContext.state->general_reg[Register].val = Value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-09 21:48:52 +00:00
|
|
|
EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val = (USHORT)Value;
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
2013-10-09 21:48:52 +00:00
|
|
|
if (Register < EMULATOR_REG_ES)
|
|
|
|
{
|
|
|
|
EmulatorContext.GeneralRegs[Register].Long = Value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Soft386SetSegment(&EmulatorContext, Register - EMULATOR_REG_ES, (USHORT)Value);
|
|
|
|
}
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN EmulatorGetFlag(ULONG Flag)
|
|
|
|
{
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
|
|
|
return (EmulatorContext.state->reg_flags.val & Flag) ? TRUE : FALSE;
|
|
|
|
#else
|
|
|
|
return (EmulatorContext.Flags.Long & Flag) ? TRUE : FALSE;
|
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID EmulatorSetFlag(ULONG Flag)
|
|
|
|
{
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-06-17 00:00:36 +00:00
|
|
|
EmulatorContext.state->reg_flags.val |= Flag;
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
|
|
|
EmulatorContext.Flags.Long |= Flag;
|
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID EmulatorClearFlag(ULONG Flag)
|
|
|
|
{
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-06-17 00:00:36 +00:00
|
|
|
EmulatorContext.state->reg_flags.val &= ~Flag;
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
|
|
|
EmulatorContext.Flags.Long &= ~Flag;
|
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
VOID EmulatorStep(VOID)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
2013-08-09 23:52:08 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-07-23 19:31:00 +00:00
|
|
|
LPWORD Instruction;
|
|
|
|
|
2013-07-02 21:40:11 +00:00
|
|
|
/* Print the current position - useful for debugging */
|
|
|
|
DPRINT("Executing at CS:IP = %04X:%04X\n",
|
|
|
|
EmulatorGetRegister(EMULATOR_REG_CS),
|
|
|
|
EmulatorContext.state->reg_ip);
|
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
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;
|
|
|
|
|
2013-08-05 23:20:25 +00:00
|
|
|
// HACK: Refresh the display because the called function may wait.
|
|
|
|
VgaRefreshDisplay();
|
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
/* Call the BOP handler */
|
|
|
|
EmulatorBop(Instruction[1]);
|
|
|
|
}
|
|
|
|
|
2013-06-17 00:00:36 +00:00
|
|
|
/* Call the softx86 API */
|
2013-06-26 17:15:45 +00:00
|
|
|
if (!softx86_step(&EmulatorContext))
|
|
|
|
{
|
|
|
|
/* Invalid opcode */
|
|
|
|
EmulatorInterrupt(EMULATOR_EXCEPTION_INVALID_OPCODE);
|
|
|
|
}
|
2013-07-07 20:53:23 +00:00
|
|
|
#else
|
2013-08-31 19:18:12 +00:00
|
|
|
/* Dump the state for debugging purposes */
|
2013-10-09 21:48:52 +00:00
|
|
|
// Soft386DumpState(&EmulatorContext);
|
2013-08-31 19:18:12 +00:00
|
|
|
|
|
|
|
/* Execute the next instruction */
|
|
|
|
Soft386StepInto(&EmulatorContext);
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 19:31:00 +00:00
|
|
|
VOID EmulatorCleanup(VOID)
|
2013-06-17 00:00:36 +00:00
|
|
|
{
|
2013-07-07 20:53:23 +00:00
|
|
|
#ifndef NEW_EMULATOR
|
2013-06-17 00:00:36 +00:00
|
|
|
/* Free the softx86 CPU and FPU emulator */
|
|
|
|
softx87_free(&FpuEmulatorContext);
|
2013-08-06 20:01:05 +00:00
|
|
|
softx86_free(&EmulatorContext);
|
2013-07-07 20:53:23 +00:00
|
|
|
#endif
|
2013-08-06 20:01:05 +00:00
|
|
|
|
|
|
|
/* Free the memory allocated for the 16-bit address space */
|
|
|
|
if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
|
2013-06-17 00:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 17:15:45 +00:00
|
|
|
VOID EmulatorSetA20(BOOLEAN Enabled)
|
|
|
|
{
|
|
|
|
A20Line = Enabled;
|
|
|
|
}
|
|
|
|
|
2013-06-17 00:00:36 +00:00
|
|
|
/* EOF */
|