2 * Fast486 386/486 CPU Emulation Library
5 * Copyright (C) 2013 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 /* PUBLIC FUNCTIONS ***********************************************************/
28 Fast486GetCurrentPrivLevel(PFAST486_STATE State)
30 /* Return the CPL, or 3 if we're in virtual 8086 mode */
31 return (!State->Flags.Vm) ? State->Cpl : 3;
36 Fast486GetPageTableEntry(PFAST486_STATE State,
40 ULONG PdeIndex = GET_ADDR_PDE(VirtualAddress);
41 ULONG PteIndex = GET_ADDR_PTE(VirtualAddress);
42 FAST486_PAGE_DIR DirectoryEntry;
43 FAST486_PAGE_TABLE TableEntry;
44 ULONG PageDirectory = State->ControlRegisters[FAST486_REG_CR3];
46 if ((State->Tlb != NULL)
47 && (State->Tlb[VirtualAddress >> 12] != INVALID_TLB_FIELD))
49 /* Return the cached entry */
50 return State->Tlb[VirtualAddress >> 12];
53 /* Read the directory entry */
54 State->MemReadCallback(State,
55 PageDirectory + PdeIndex * sizeof(ULONG),
56 &DirectoryEntry.Value,
57 sizeof(DirectoryEntry));
59 /* Make sure it is present */
60 if (!DirectoryEntry.Present) return 0;
62 /* Was the directory entry accessed before? */
63 if (!DirectoryEntry.Accessed)
66 DirectoryEntry.Accessed = TRUE;
68 /* Write back the directory entry */
69 State->MemWriteCallback(State,
70 PageDirectory + PdeIndex * sizeof(ULONG),
71 &DirectoryEntry.Value,
72 sizeof(DirectoryEntry));
75 /* Read the table entry */
76 State->MemReadCallback(State,
77 (DirectoryEntry.TableAddress << 12)
78 + PteIndex * sizeof(ULONG),
82 /* Make sure it is present */
83 if (!TableEntry.Present) return 0;
85 if (MarkAsDirty) TableEntry.Dirty = TRUE;
87 /* Was the table entry accessed before? */
88 if (!TableEntry.Accessed)
91 TableEntry.Accessed = TRUE;
93 /* Write back the table entry */
94 State->MemWriteCallback(State,
95 (DirectoryEntry.TableAddress << 12)
96 + PteIndex * sizeof(ULONG),
102 * The resulting permissions depend on the permissions
103 * in the page directory table too
105 TableEntry.Writeable &= DirectoryEntry.Writeable;
106 TableEntry.Usermode &= DirectoryEntry.Usermode;
108 if (State->Tlb != NULL)
110 /* Set the TLB entry */
111 State->Tlb[VirtualAddress >> 12] = TableEntry.Value;
114 /* Return the table entry */
115 return TableEntry.Value;
120 Fast486ReadLinearMemory(PFAST486_STATE State,
125 /* Check if paging is enabled */
126 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
129 FAST486_PAGE_TABLE TableEntry;
130 INT Cpl = Fast486GetCurrentPrivLevel(State);
131 ULONG BufferOffset = 0;
133 for (Page = PAGE_ALIGN(LinearAddress);
134 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
137 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
139 /* Get the table entry */
140 TableEntry.Value = Fast486GetPageTableEntry(State, Page, FALSE);
142 if (!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
145 Fast486ExceptionWithErrorCode(State,
146 FAST486_EXCEPTION_PF,
147 TableEntry.Value & 0x07);
151 /* Check if this is the first page */
152 if (Page == PAGE_ALIGN(LinearAddress))
154 /* Start reading from the offset from the beginning of the page */
155 PageOffset = PAGE_OFFSET(LinearAddress);
156 PageLength -= PageOffset;
159 /* Check if this is the last page */
160 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
162 /* Read only a part of the page */
163 PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
166 /* Read the memory */
167 State->MemReadCallback(State,
168 (TableEntry.Address << 12) | PageOffset,
169 (PVOID)((ULONG_PTR)Buffer + BufferOffset),
172 BufferOffset += PageLength;
177 /* Read the memory */
178 State->MemReadCallback(State, LinearAddress, Buffer, Size);
186 Fast486WriteLinearMemory(PFAST486_STATE State,
191 /* Check if paging is enabled */
192 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
195 FAST486_PAGE_TABLE TableEntry;
196 INT Cpl = Fast486GetCurrentPrivLevel(State);
197 ULONG BufferOffset = 0;
199 for (Page = PAGE_ALIGN(LinearAddress);
200 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
203 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
205 /* Get the table entry */
206 TableEntry.Value = Fast486GetPageTableEntry(State, Page, TRUE);
208 if ((!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
209 || ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_WP)
210 && !TableEntry.Writeable))
213 Fast486ExceptionWithErrorCode(State,
214 FAST486_EXCEPTION_PF,
215 TableEntry.Value & 0x07);
219 /* Check if this is the first page */
220 if (Page == PAGE_ALIGN(LinearAddress))
222 /* Start writing from the offset from the beginning of the page */
223 PageOffset = PAGE_OFFSET(LinearAddress);
224 PageLength -= PageOffset;
227 /* Check if this is the last page */
228 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
230 /* Write only a part of the page */
231 PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
234 /* Write the memory */
235 State->MemWriteCallback(State,
236 (TableEntry.Address << 12) | PageOffset,
237 (PVOID)((ULONG_PTR)Buffer + BufferOffset),
240 BufferOffset += PageLength;
245 /* Write the memory */
246 State->MemWriteCallback(State, LinearAddress, Buffer, Size);
254 Fast486Exception(PFAST486_STATE State,
255 FAST486_EXCEPTIONS ExceptionCode)
257 /* Call the internal function */
258 Fast486ExceptionWithErrorCode(State, ExceptionCode, 0);
263 Fast486StackPush(PFAST486_STATE State,
266 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
268 /* The OPSIZE prefix toggles the size */
269 if (State->PrefixFlags & FAST486_PREFIX_OPSIZE) Size = !Size;
275 /* Check if ESP is between 1 and 3 */
276 if (State->GeneralRegs[FAST486_REG_ESP].Long >= 1
277 && State->GeneralRegs[FAST486_REG_ESP].Long <= 3)
279 Fast486Exception(State, FAST486_EXCEPTION_SS);
283 /* Subtract ESP by 4 */
284 State->GeneralRegs[FAST486_REG_ESP].Long -= sizeof(ULONG);
286 /* Store the value in SS:ESP */
287 return Fast486WriteMemory(State,
289 State->GeneralRegs[FAST486_REG_ESP].Long,
296 USHORT ShortValue = LOWORD(Value);
298 /* Check if SP is 1 */
299 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 1)
301 Fast486Exception(State, FAST486_EXCEPTION_SS);
305 /* Subtract SP by 2 */
306 State->GeneralRegs[FAST486_REG_ESP].LowWord -= sizeof(USHORT);
308 /* Store the value in SS:SP */
309 return Fast486WriteMemory(State,
311 State->GeneralRegs[FAST486_REG_ESP].LowWord,
319 Fast486StackPop(PFAST486_STATE State,
322 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
324 /* The OPSIZE prefix toggles the size */
332 /* Check if ESP is 0xFFFFFFFF */
333 if (State->GeneralRegs[FAST486_REG_ESP].Long == 0xFFFFFFFF)
335 Fast486Exception(State, FAST486_EXCEPTION_SS);
339 /* Read the value from SS:ESP */
340 if (!Fast486ReadMemory(State,
342 State->GeneralRegs[FAST486_REG_ESP].Long,
347 /* An exception occurred */
351 /* Increment ESP by 4 */
352 State->GeneralRegs[FAST486_REG_ESP].Long += sizeof(ULONG);
354 /* Store the value in the result */
362 /* Check if SP is 0xFFFF */
363 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 0xFFFF)
365 Fast486Exception(State, FAST486_EXCEPTION_SS);
369 /* Read the value from SS:SP */
370 if (!Fast486ReadMemory(State,
372 State->GeneralRegs[FAST486_REG_ESP].LowWord,
377 /* An exception occurred */
381 /* Increment SP by 2 */
382 State->GeneralRegs[FAST486_REG_ESP].LowWord += sizeof(USHORT);
384 /* Store the value in the result */
393 Fast486LoadSegment(PFAST486_STATE State,
394 FAST486_SEG_REGS Segment,
397 PFAST486_SEG_REG CachedDescriptor;
398 FAST486_GDT_ENTRY GdtEntry;
400 ASSERT(Segment < FAST486_NUM_SEG_REGS);
402 /* Get the cached descriptor */
403 CachedDescriptor = &State->SegmentRegs[Segment];
405 /* Check for protected mode */
406 if ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE) && !State->Flags.Vm)
408 if (!(Selector & SEGMENT_TABLE_INDICATOR))
410 /* Make sure the GDT contains the entry */
411 if (GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
413 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
418 if (!Fast486ReadLinearMemory(State,
420 + GET_SEGMENT_INDEX(Selector),
424 /* Exception occurred */
430 /* Make sure the LDT contains the entry */
431 if (GET_SEGMENT_INDEX(Selector) >= (State->Ldtr.Limit + 1))
433 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
438 if (!Fast486ReadLinearMemory(State,
440 + GET_SEGMENT_INDEX(Selector),
444 /* Exception occurred */
449 if (Segment == FAST486_REG_SS)
451 /* Loading the stack segment */
453 if (GET_SEGMENT_INDEX(Selector) == 0)
455 Fast486Exception(State, FAST486_EXCEPTION_GP);
459 if (!GdtEntry.SystemType)
461 /* This is a special descriptor */
462 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
466 if (GdtEntry.Executable || !GdtEntry.ReadWrite)
468 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
472 if ((GET_SEGMENT_RPL(Selector) != Fast486GetCurrentPrivLevel(State))
473 || (GET_SEGMENT_RPL(Selector) != GdtEntry.Dpl))
475 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
479 if (!GdtEntry.Present)
481 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_SS, Selector);
485 else if (Segment == FAST486_REG_CS)
487 /* Loading the code segment */
489 if (GET_SEGMENT_INDEX(Selector) == 0)
491 Fast486Exception(State, FAST486_EXCEPTION_GP);
495 if (!GdtEntry.SystemType)
497 // TODO: Call/interrupt/task gates NOT IMPLEMENTED!
502 if (!GdtEntry.Present)
504 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
508 if (!GdtEntry.Executable)
510 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
514 if (GdtEntry.DirConf)
516 /* Conforming Code Segment */
518 if (GdtEntry.Dpl > Fast486GetCurrentPrivLevel(State))
520 /* Must be accessed from lower-privileged code */
521 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
527 /* Regular code segment */
529 if ((GET_SEGMENT_RPL(Selector) > Fast486GetCurrentPrivLevel(State))
530 || (Fast486GetCurrentPrivLevel(State) != GdtEntry.Dpl))
532 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
538 State->Cpl = GET_SEGMENT_RPL(Selector);
543 /* Loading a data segment */
545 if (GET_SEGMENT_INDEX(Selector) != 0)
547 if (!GdtEntry.SystemType)
549 /* This is a special descriptor */
550 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
554 if ((GET_SEGMENT_RPL(Selector) > GdtEntry.Dpl)
555 || (Fast486GetCurrentPrivLevel(State) > GdtEntry.Dpl))
557 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
561 if (!GdtEntry.Present)
563 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
569 /* This is a NULL selector */
570 RtlZeroMemory(&GdtEntry, sizeof(GdtEntry));
574 /* Update the cache entry */
575 CachedDescriptor->Selector = Selector;
576 CachedDescriptor->Base = GdtEntry.Base | (GdtEntry.BaseMid << 16) | (GdtEntry.BaseHigh << 24);
577 CachedDescriptor->Limit = GdtEntry.Limit | (GdtEntry.LimitHigh << 16);
578 CachedDescriptor->Accessed = GdtEntry.Accessed;
579 CachedDescriptor->ReadWrite = GdtEntry.ReadWrite;
580 CachedDescriptor->DirConf = GdtEntry.DirConf;
581 CachedDescriptor->Executable = GdtEntry.Executable;
582 CachedDescriptor->SystemType = GdtEntry.SystemType;
583 CachedDescriptor->Rpl = GET_SEGMENT_RPL(Selector);
584 CachedDescriptor->Dpl = GdtEntry.Dpl;
585 CachedDescriptor->Present = GdtEntry.Present;
586 CachedDescriptor->Size = GdtEntry.Size;
588 /* Check for page granularity */
589 if (GdtEntry.Granularity) CachedDescriptor->Limit <<= 12;
593 /* Update the selector and base */
594 CachedDescriptor->Selector = Selector;
595 CachedDescriptor->Base = Selector << 4;
603 Fast486FetchByte(PFAST486_STATE State,
606 PFAST486_SEG_REG CachedDescriptor;
608 /* Get the cached descriptor of CS */
609 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
611 /* Read from memory */
612 if (!Fast486ReadMemory(State,
614 (CachedDescriptor->Size) ? State->InstPtr.Long
615 : State->InstPtr.LowWord,
620 /* Exception occurred during instruction fetch */
624 /* Advance the instruction pointer */
625 if (CachedDescriptor->Size) State->InstPtr.Long++;
626 else State->InstPtr.LowWord++;
633 Fast486FetchWord(PFAST486_STATE State,
636 PFAST486_SEG_REG CachedDescriptor;
638 /* Get the cached descriptor of CS */
639 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
641 /* Read from memory */
642 // FIXME: Fix byte order on big-endian machines
643 if (!Fast486ReadMemory(State,
645 (CachedDescriptor->Size) ? State->InstPtr.Long
646 : State->InstPtr.LowWord,
651 /* Exception occurred during instruction fetch */
655 /* Advance the instruction pointer */
656 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(USHORT);
657 else State->InstPtr.LowWord += sizeof(USHORT);
664 Fast486FetchDword(PFAST486_STATE State,
667 PFAST486_SEG_REG CachedDescriptor;
669 /* Get the cached descriptor of CS */
670 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
672 /* Read from memory */
673 // FIXME: Fix byte order on big-endian machines
674 if (!Fast486ReadMemory(State,
676 (CachedDescriptor->Size) ? State->InstPtr.Long
677 : State->InstPtr.LowWord,
682 /* Exception occurred during instruction fetch */
686 /* Advance the instruction pointer */
687 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(ULONG);
688 else State->InstPtr.LowWord += sizeof(ULONG);
695 Fast486GetIntVector(PFAST486_STATE State,
697 PFAST486_IDT_ENTRY IdtEntry)
701 /* Check for protected mode */
702 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
704 /* Read from the IDT */
705 if (!Fast486ReadLinearMemory(State,
707 + Number * sizeof(*IdtEntry),
711 /* Exception occurred */
717 /* Read from the real-mode IVT */
719 /* Paging is always disabled in real mode */
720 State->MemReadCallback(State,
722 + Number * sizeof(FarPointer),
726 /* Fill a fake IDT entry */
727 IdtEntry->Offset = LOWORD(FarPointer);
728 IdtEntry->Selector = HIWORD(FarPointer);
730 IdtEntry->Type = FAST486_IDT_INT_GATE;
731 IdtEntry->Storage = FALSE;
733 IdtEntry->Present = TRUE;
734 IdtEntry->OffsetHigh = 0;
742 Fast486CalculateParity(UCHAR Number)
744 // See http://graphics.stanford.edu/~seander/bithacks.html#ParityLookupTable too...
745 return (0x9669 >> ((Number & 0x0F) ^ (Number >> 4))) & 1;
750 Fast486ParseModRegRm(PFAST486_STATE State,
752 PFAST486_MOD_REG_RM ModRegRm)
754 UCHAR ModRmByte, Mode, RegMem;
756 /* Fetch the MOD REG R/M byte */
757 if (!Fast486FetchByte(State, &ModRmByte))
759 /* Exception occurred */
763 /* Unpack the mode and R/M */
764 Mode = ModRmByte >> 6;
765 RegMem = ModRmByte & 0x07;
767 /* Set the register operand */
768 ModRegRm->Register = (ModRmByte >> 3) & 0x07;
771 if ((ModRmByte >> 6) == 3)
773 /* The second operand is also a register */
774 ModRegRm->Memory = FALSE;
775 ModRegRm->SecondRegister = RegMem;
781 /* The second operand is memory */
782 ModRegRm->Memory = TRUE;
786 if (RegMem == FAST486_REG_ESP)
789 ULONG Scale, Index, Base;
791 /* Fetch the SIB byte */
792 if (!Fast486FetchByte(State, &SibByte))
794 /* Exception occurred */
798 /* Unpack the scale, index and base */
799 Scale = 1 << (SibByte >> 6);
800 Index = (SibByte >> 3) & 0x07;
801 if (Index != FAST486_REG_ESP) Index = State->GeneralRegs[Index].Long;
804 if (((SibByte & 0x07) != FAST486_REG_EBP) || (Mode != 0))
806 /* Use the register a base */
807 Base = State->GeneralRegs[SibByte & 0x07].Long;
812 if (!Fast486FetchDword(State, &Base))
814 /* Exception occurred */
819 if ((SibByte & 0x07) == FAST486_REG_ESP)
821 /* Check if there is no segment override */
822 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
824 /* Add a SS: prefix */
825 State->PrefixFlags |= FAST486_PREFIX_SEG;
826 State->SegmentOverride = FAST486_REG_SS;
830 /* Calculate the address */
831 ModRegRm->MemoryAddress = Base + Index * Scale;
833 else if (RegMem == FAST486_REG_EBP)
835 if (Mode) ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].Long;
836 else ModRegRm->MemoryAddress = 0;
840 /* Get the base from the register */
841 ModRegRm->MemoryAddress = State->GeneralRegs[RegMem].Long;
844 /* Check if there is no segment override */
845 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
847 /* Check if the default segment should be SS */
848 if ((RegMem == FAST486_REG_EBP) && Mode)
850 /* Add a SS: prefix */
851 State->PrefixFlags |= FAST486_PREFIX_SEG;
852 State->SegmentOverride = FAST486_REG_SS;
861 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
863 /* Exception occurred */
867 /* Add the signed offset to the address */
868 ModRegRm->MemoryAddress += (LONG)Offset;
870 else if ((Mode == 2) || ((Mode == 0) && (RegMem == FAST486_REG_EBP)))
874 /* Fetch the dword */
875 if (!Fast486FetchDword(State, (PULONG)&Offset))
877 /* Exception occurred */
881 /* Add the signed offset to the address */
882 ModRegRm->MemoryAddress += Offset;
887 /* Check the operand */
893 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
894 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
902 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
903 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
911 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
912 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
920 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
921 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
929 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_ESI].LowWord;
937 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EDI].LowWord;
947 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord;
951 /* [constant] (added later) */
952 ModRegRm->MemoryAddress = 0;
961 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord;
967 /* Check if there is no segment override */
968 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
970 /* Check if the default segment should be SS */
971 if ((RegMem == 2) || (RegMem == 3) || ((RegMem == 6) && Mode))
973 /* Add a SS: prefix */
974 State->PrefixFlags |= FAST486_PREFIX_SEG;
975 State->SegmentOverride = FAST486_REG_SS;
984 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
986 /* Exception occurred */
990 /* Add the signed offset to the address */
991 ModRegRm->MemoryAddress += (LONG)Offset;
993 else if ((Mode == 2) || ((Mode == 0) && (RegMem == 6)))
998 if (!Fast486FetchWord(State, (PUSHORT)&Offset))
1000 /* Exception occurred */
1004 /* Add the signed offset to the address */
1005 ModRegRm->MemoryAddress += (LONG)Offset;
1008 /* Clear the top 16 bits */
1009 ModRegRm->MemoryAddress &= 0x0000FFFF;
1017 Fast486ReadModrmByteOperands(PFAST486_STATE State,
1018 PFAST486_MOD_REG_RM ModRegRm,
1022 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1026 /* Get the register value */
1027 if (ModRegRm->Register & 0x04)
1029 /* AH, CH, DH, BH */
1030 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].HighByte;
1034 /* AL, CL, DL, BL */
1035 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].LowByte;
1041 if (!ModRegRm->Memory)
1043 /* Get the second register value */
1044 if (ModRegRm->SecondRegister & 0x04)
1046 /* AH, CH, DH, BH */
1047 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte;
1051 /* AL, CL, DL, BL */
1052 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte;
1057 /* Check for the segment override */
1058 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1060 /* Use the override segment instead */
1061 Segment = State->SegmentOverride;
1065 if (!Fast486ReadMemory(State,
1067 ModRegRm->MemoryAddress,
1072 /* Exception occurred */
1083 Fast486ReadModrmWordOperands(PFAST486_STATE State,
1084 PFAST486_MOD_REG_RM ModRegRm,
1088 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1092 /* Get the register value */
1093 *RegValue = State->GeneralRegs[ModRegRm->Register].LowWord;
1098 if (!ModRegRm->Memory)
1100 /* Get the second register value */
1101 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].LowWord;
1105 /* Check for the segment override */
1106 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1108 /* Use the override segment instead */
1109 Segment = State->SegmentOverride;
1113 if (!Fast486ReadMemory(State,
1115 ModRegRm->MemoryAddress,
1120 /* Exception occurred */
1131 Fast486ReadModrmDwordOperands(PFAST486_STATE State,
1132 PFAST486_MOD_REG_RM ModRegRm,
1136 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1140 /* Get the register value */
1141 *RegValue = State->GeneralRegs[ModRegRm->Register].Long;
1146 if (!ModRegRm->Memory)
1148 /* Get the second register value */
1149 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].Long;
1153 /* Check for the segment override */
1154 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1156 /* Use the override segment instead */
1157 Segment = State->SegmentOverride;
1161 if (!Fast486ReadMemory(State,
1163 ModRegRm->MemoryAddress,
1168 /* Exception occurred */
1179 Fast486WriteModrmByteOperands(PFAST486_STATE State,
1180 PFAST486_MOD_REG_RM ModRegRm,
1181 BOOLEAN WriteRegister,
1184 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1188 /* Store the value in the register */
1189 if (ModRegRm->Register & 0x04)
1191 /* AH, CH, DH, BH */
1192 State->GeneralRegs[ModRegRm->Register & 0x03].HighByte = Value;
1196 /* AL, CL, DL, BL */
1197 State->GeneralRegs[ModRegRm->Register & 0x03].LowByte = Value;
1202 if (!ModRegRm->Memory)
1204 /* Store the value in the second register */
1205 if (ModRegRm->SecondRegister & 0x04)
1207 /* AH, CH, DH, BH */
1208 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte = Value;
1212 /* AL, CL, DL, BL */
1213 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte = Value;
1218 /* Check for the segment override */
1219 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1221 /* Use the override segment instead */
1222 Segment = State->SegmentOverride;
1226 if (!Fast486WriteMemory(State,
1228 ModRegRm->MemoryAddress,
1232 /* Exception occurred */
1243 Fast486WriteModrmWordOperands(PFAST486_STATE State,
1244 PFAST486_MOD_REG_RM ModRegRm,
1245 BOOLEAN WriteRegister,
1248 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1252 /* Store the value in the register */
1253 State->GeneralRegs[ModRegRm->Register].LowWord = Value;
1257 if (!ModRegRm->Memory)
1259 /* Store the value in the second register */
1260 State->GeneralRegs[ModRegRm->SecondRegister].LowWord = Value;
1264 /* Check for the segment override */
1265 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1267 /* Use the override segment instead */
1268 Segment = State->SegmentOverride;
1272 if (!Fast486WriteMemory(State,
1274 ModRegRm->MemoryAddress,
1278 /* Exception occurred */
1289 Fast486WriteModrmDwordOperands(PFAST486_STATE State,
1290 PFAST486_MOD_REG_RM ModRegRm,
1291 BOOLEAN WriteRegister,
1294 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1298 /* Store the value in the register */
1299 State->GeneralRegs[ModRegRm->Register].Long = Value;
1303 if (!ModRegRm->Memory)
1305 /* Store the value in the second register */
1306 State->GeneralRegs[ModRegRm->SecondRegister].Long = Value;
1310 /* Check for the segment override */
1311 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1313 /* Use the override segment instead */
1314 Segment = State->SegmentOverride;
1318 if (!Fast486WriteMemory(State,
1320 ModRegRm->MemoryAddress,
1324 /* Exception occurred */