/* INCLUDES *******************************************************************/
+#define NDEBUG
+
#include "emulator.h"
#include "bios.h"
+#include "bop.h"
#include "dos.h"
+#include "speaker.h"
+#include "vga.h"
#include "pic.h"
#include "ps2.h"
#include "timer.h"
+#include "cmos.h"
/* PRIVATE VARIABLES **********************************************************/
-#ifndef NEW_EMULATOR
-softx86_ctx EmulatorContext;
-softx87_ctx FpuEmulatorContext;
-#else
-EMULATOR_CONTEXT EmulatorContext;
-#endif
+FAST486_STATE EmulatorContext;
static BOOLEAN A20Line = FALSE;
/* PRIVATE FUNCTIONS **********************************************************/
-#ifndef NEW_EMULATOR
-
-static VOID EmulatorReadMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
+VOID WINAPI EmulatorReadMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
{
+ UNREFERENCED_PARAMETER(State);
+
/* If the A20 line is disabled, mask bit 20 */
if (!A20Line) Address &= ~(1 << 20);
/* Make sure the requested address is valid */
if ((Address + Size) >= MAX_ADDRESS) return;
- /* Are we reading some of the console video memory? */
- if (((Address + Size) >= BiosGetVideoMemoryStart())
- && (Address < CONSOLE_VIDEO_MEM_END))
- {
- /* Call the VDM BIOS to update the video memory */
- BiosUpdateVideoMemory(max(Address, BiosGetVideoMemoryStart()),
- min(Address + Size, CONSOLE_VIDEO_MEM_END));
- }
-
/* Read the data from the virtual address space and store it in the buffer */
RtlCopyMemory(Buffer, (LPVOID)((ULONG_PTR)BaseAddress + Address), Size);
+
+ /* Check if we modified the console video memory */
+ if (((Address + Size) >= VgaGetVideoBaseAddress())
+ && (Address < VgaGetVideoLimitAddress()))
+ {
+ DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
+ DWORD ActualSize = min(Address + Size - 1, VgaGetVideoLimitAddress())
+ - VgaAddress + 1;
+ LPBYTE VgaBuffer = (LPBYTE)((ULONG_PTR)Buffer + VgaAddress - Address);
+
+ /* Read from the VGA memory */
+ VgaReadMemory(VgaAddress, VgaBuffer, ActualSize);
+ }
}
-static VOID EmulatorWriteMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
+VOID WINAPI EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
{
+ UNREFERENCED_PARAMETER(State);
+
/* If the A20 line is disabled, mask bit 20 */
if (!A20Line) Address &= ~(1 << 20);
RtlCopyMemory((LPVOID)((ULONG_PTR)BaseAddress + Address), Buffer, Size);
/* Check if we modified the console video memory */
- if (((Address + Size) >= BiosGetVideoMemoryStart())
- && (Address < CONSOLE_VIDEO_MEM_END))
+ if (((Address + Size) >= VgaGetVideoBaseAddress())
+ && (Address < VgaGetVideoLimitAddress()))
{
- /* Call the VDM BIOS to update the screen */
- BiosUpdateConsole(max(Address, BiosGetVideoMemoryStart()),
- min(Address + Size, CONSOLE_VIDEO_MEM_END));
+ DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
+ DWORD ActualSize = min(Address + Size - 1, VgaGetVideoLimitAddress())
+ - VgaAddress + 1;
+ LPBYTE VgaBuffer = (LPBYTE)((ULONG_PTR)Buffer + VgaAddress - Address);
+
+ /* Write to the VGA memory */
+ VgaWriteMemory(VgaAddress, VgaBuffer, ActualSize);
}
}
-static VOID EmulatorReadIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
+VOID WINAPI EmulatorReadIo(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
{
- switch (Address)
- {
- case PIC_MASTER_CMD:
- case PIC_SLAVE_CMD:
- {
- *Buffer = PicReadCommand(Address);
- break;
- }
+ INT i, j;
+ LPBYTE Address = (LPBYTE)Buffer;
- case PIC_MASTER_DATA:
- case PIC_SLAVE_DATA:
- {
- *Buffer = PicReadData(Address);
- break;
- }
+ UNREFERENCED_PARAMETER(State);
- case PIT_DATA_PORT(0):
- case PIT_DATA_PORT(1):
- case PIT_DATA_PORT(2):
- {
- *Buffer = PitReadData(Address - PIT_DATA_PORT(0));
- break;
- }
+ for (i = 0; i < DataCount; i++) for (j = 0; j < DataSize; j++)
+ {
+ ULONG CurrentPort = Port + j;
- case PS2_CONTROL_PORT:
+ switch (CurrentPort)
{
- *Buffer = KeyboardReadStatus();
- break;
- }
+ case PIC_MASTER_CMD:
+ case PIC_SLAVE_CMD:
+ {
+ *(Address++) = PicReadCommand(CurrentPort);
+ break;
+ }
- case PS2_DATA_PORT:
- {
- *Buffer = KeyboardReadData();
- break;
- }
- }
-}
+ case PIC_MASTER_DATA:
+ case PIC_SLAVE_DATA:
+ {
+ *(Address++) = PicReadData(CurrentPort);
+ break;
+ }
-static VOID EmulatorWriteIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
-{
- BYTE Byte = *Buffer;
+ case PIT_DATA_PORT(0):
+ case PIT_DATA_PORT(1):
+ case PIT_DATA_PORT(2):
+ {
+ *(Address++) = PitReadData(CurrentPort - PIT_DATA_PORT(0));
+ break;
+ }
- switch (Address)
- {
- case PIT_COMMAND_PORT:
- {
- PitWriteCommand(Byte);
- break;
- }
+ case PS2_CONTROL_PORT:
+ {
+ *(Address++) = KeyboardReadStatus();
+ break;
+ }
- case PIT_DATA_PORT(0):
- case PIT_DATA_PORT(1):
- case PIT_DATA_PORT(2):
- {
- PitWriteData(Address - PIT_DATA_PORT(0), Byte);
- break;
- }
+ case PS2_DATA_PORT:
+ {
+ *(Address++) = KeyboardReadData();
+ break;
+ }
- case PIC_MASTER_CMD:
- case PIC_SLAVE_CMD:
- {
- PicWriteCommand(Address, Byte);
- break;
- }
+ case CMOS_DATA_PORT:
+ {
+ *(Address++) = CmosReadData();
+ break;
+ }
- case PIC_MASTER_DATA:
- case PIC_SLAVE_DATA:
- {
- PicWriteData(Address, Byte);
- break;
- }
+ case SPEAKER_CONTROL_PORT:
+ {
+ *(Address++) = SpeakerReadStatus();
+ break;
+ }
- case PS2_CONTROL_PORT:
- {
- KeyboardWriteCommand(Byte);
- break;
- }
+ 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:
+ {
+ *(Address++) = VgaReadPort(CurrentPort);
+ break;
+ }
- case PS2_DATA_PORT:
- {
- KeyboardWriteData(Byte);
- break;
+ default:
+ {
+ DPRINT1("Read from unknown port: 0x%X\n", CurrentPort);
+ }
}
}
}
-static VOID EmulatorSoftwareInt(PVOID Context, BYTE Number)
+VOID WINAPI EmulatorWriteIo(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
{
- WORD StackSegment, StackPointer, CodeSegment, InstructionPointer;
- BYTE IntNum;
+ INT i, j;
+ LPBYTE Address = (LPBYTE)Buffer;
- /* Check if this is the special interrupt */
- if (Number == SPECIAL_INT_NUM)
+ UNREFERENCED_PARAMETER(State);
+
+ for (i = 0; i < DataCount; i++) for (j = 0; j < DataSize; j++)
{
- /* 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.Registers[EMULATOR_REG_SS].LowWord;
- StackPointer = EmulatorContext.Registers[EMULATOR_REG_SP].LowWord;
-#endif
-
- /* Get the interrupt number */
- IntNum = *(LPBYTE)((ULONG_PTR)BaseAddress + TO_LINEAR(StackSegment, StackPointer));
-
- /* Move the stack pointer forward one word to skip the interrupt number */
- StackPointer += sizeof(WORD);
-
- /* Get the CS:IP */
- InstructionPointer = *(LPWORD)((ULONG_PTR)BaseAddress
- + TO_LINEAR(StackSegment, StackPointer));
- CodeSegment = *(LPWORD)((ULONG_PTR)BaseAddress
- + TO_LINEAR(StackSegment, StackPointer + sizeof(WORD)));
-
- /* 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;
- }
+ ULONG CurrentPort = Port + j;
- /* 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 */
- BiosHandleIrq(IntNum - BIOS_PIC_MASTER_INT);
- return;
- }
- else if (IntNum >= BIOS_PIC_SLAVE_INT && IntNum < BIOS_PIC_SLAVE_INT + 8)
+ switch (CurrentPort)
{
- /* It was an IRQ from the slave PIC */
- BiosHandleIrq(IntNum - BIOS_PIC_SLAVE_INT + 8);
- return;
- }
+ case PIT_COMMAND_PORT:
+ {
+ PitWriteCommand(*(Address++));
+ break;
+ }
- switch (IntNum)
- {
- case BIOS_VIDEO_INTERRUPT:
+ case PIT_DATA_PORT(0):
+ case PIT_DATA_PORT(1):
+ case PIT_DATA_PORT(2):
{
- /* This is the video BIOS interrupt, call the BIOS */
- BiosVideoService();
+ PitWriteData(CurrentPort - PIT_DATA_PORT(0), *(Address++));
break;
}
- case BIOS_EQUIPMENT_INTERRUPT:
+
+ case PIC_MASTER_CMD:
+ case PIC_SLAVE_CMD:
{
- /* This is the BIOS "get equipment" command, call the BIOS */
- BiosEquipmentService();
+ PicWriteCommand(CurrentPort, *(Address++));
break;
}
- case BIOS_KBD_INTERRUPT:
+
+ case PIC_MASTER_DATA:
+ case PIC_SLAVE_DATA:
{
- /* This is the keyboard BIOS interrupt, call the BIOS */
- BiosKeyboardService();
+ PicWriteData(CurrentPort, *(Address++));
break;
}
- case BIOS_TIME_INTERRUPT:
+
+ case PS2_CONTROL_PORT:
{
- /* This is the time BIOS interrupt, call the BIOS */
- BiosTimeService();
+ KeyboardWriteCommand(*(Address++));
break;
}
- case BIOS_SYS_TIMER_INTERRUPT:
+
+ case PS2_DATA_PORT:
{
- /* BIOS timer update */
- BiosSystemTimerInterrupt();
+ KeyboardWriteData(*(Address++));
break;
}
- case 0x20:
+
+ case CMOS_ADDRESS_PORT:
{
- DosInt20h(CodeSegment);
+ CmosWriteAddress(*(Address++));
break;
}
- case 0x21:
+
+ case CMOS_DATA_PORT:
{
- DosInt21h(CodeSegment);
+ CmosWriteData(*(Address++));
break;
}
- case 0x23:
+
+ case SPEAKER_CONTROL_PORT:
{
- DosBreakInterrupt();
+ SpeakerWriteCommand(*(Address++));
break;
}
- default:
+
+ 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:
{
- DPRINT1("Unhandled interrupt: 0x%02X\n", IntNum);
+ VgaWritePort(CurrentPort, *(Address++));
break;
}
+
+ default:
+ {
+ DPRINT1("Write to unknown port: 0x%X\n", CurrentPort);
+ }
}
}
}
-static VOID EmulatorHardwareInt(PVOID Context, BYTE Number)
+VOID WINAPI EmulatorBiosOperation(PFAST486_STATE State, UCHAR BopCode)
{
- /* Do nothing */
+ WORD StackSegment, StackPointer;
+ LPWORD Stack;
+
+ /* Get the SS:SP */
+ StackSegment = State->SegmentRegs[FAST486_REG_SS].Selector;
+ StackPointer = State->GeneralRegs[FAST486_REG_ESP].LowWord;
+
+ /* Get the stack */
+ Stack = (LPWORD)SEG_OFF_TO_PTR(StackSegment, StackPointer);
+
+ if (BopProc[BopCode] != NULL)
+ BopProc[BopCode](Stack);
+ else
+ DPRINT1("Invalid BOP code %u\n", BopCode);
}
-static VOID EmulatorHardwareIntAck(PVOID Context, BYTE Number)
+UCHAR WINAPI EmulatorIntAcknowledge(PFAST486_STATE State)
{
- /* Do nothing */
-}
+ UNREFERENCED_PARAMETER(State);
-#endif
+ /* Get the interrupt number from the PIC */
+ return PicGetInterrupt();
+}
/* PUBLIC FUNCTIONS ***********************************************************/
-BOOLEAN EmulatorInitialize()
+BOOLEAN EmulatorInitialize(VOID)
{
/* Allocate memory for the 16-bit address space */
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
- // TODO: NOT IMPLEMENTED
-#endif
+ /* Initialize the CPU */
+ Fast486Initialize(&EmulatorContext,
+ EmulatorReadMemory,
+ EmulatorWriteMemory,
+ EmulatorReadIo,
+ EmulatorWriteIo,
+ NULL,
+ EmulatorBiosOperation,
+ EmulatorIntAcknowledge);
/* Enable interrupts */
EmulatorSetFlag(EMULATOR_FLAG_IF);
return TRUE;
}
-VOID EmulatorSetStack(WORD Segment, WORD Offset)
+VOID EmulatorCleanup(VOID)
+{
+ /* Free the memory allocated for the 16-bit address space */
+ if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
+}
+
+VOID EmulatorSetStack(WORD Segment, DWORD Offset)
{
-#ifndef NEW_EMULATOR
- /* Call the softx86 API */
- softx86_set_stack_ptr(&EmulatorContext, Segment, Offset);
-#else
- // TODO: NOT IMPLEMENTED
-#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
- // TODO: NOT IMPLEMENTED
-#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;
+ return EmulatorContext.GeneralRegs[Register].Long;
}
else
{
- return EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val;
+ return EmulatorContext.SegmentRegs[Register - EMULATOR_REG_ES].Selector;
}
-#else
- return EmulatorContext.Registers[Register].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 = 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 EmulatorStep(VOID)
{
-#ifndef NEW_EMULATOR
- /* Print the current position - useful for debugging */
- DPRINT("Executing at CS:IP = %04X:%04X\n",
- EmulatorGetRegister(EMULATOR_REG_CS),
- EmulatorContext.state->reg_ip);
-
- /* Call the softx86 API */
- if (!softx86_step(&EmulatorContext))
- {
- /* Invalid opcode */
- EmulatorInterrupt(EMULATOR_EXCEPTION_INVALID_OPCODE);
- }
-#else
- // TODO: NOT IMPLEMENTED
-#endif
-}
-
-VOID EmulatorCleanup()
-{
- /* Free the memory allocated for the 16-bit address space */
- if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
+ /* Dump the state for debugging purposes */
+ // Fast486DumpState(&EmulatorContext);
-#ifndef NEW_EMULATOR
- /* Free the softx86 CPU and FPU emulator */
- softx86_free(&EmulatorContext);
- softx87_free(&FpuEmulatorContext);
-#endif
+ /* Execute the next instruction */
+ Fast486StepInto(&EmulatorContext);
}
VOID EmulatorSetA20(BOOLEAN Enabled)