2 * Fast486 386/486 CPU Emulation Library
5 * Copyright (C) 2014 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.
25 /* PUBLIC FUNCTIONS ***********************************************************/
27 #if defined (__GNUC__)
28 #define CountLeadingZeros64(x) __builtin_clzll(x)
31 #elif (_MSC_VER >= 1500) && defined(_WIN64)
32 #define CountLeadingZeros64(x) __lzcnt64(x)
33 #elif (_MSC_VER >= 1500)
34 #define CountLeadingZeros64(x) ((x) > 0xFFFFFFFFULL) ? __lzcnt((x) >> 32) \
41 CountLeadingZeros64(ULONGLONG Value)
45 while ((LONGLONG)Value < 0)
57 Fast486GetCurrentPrivLevel(PFAST486_STATE State)
59 /* Return the CPL, or 3 if we're in virtual 8086 mode */
60 return (!State->Flags.Vm) ? State->Cpl : 3;
66 Fast486GetPageTableEntry(PFAST486_STATE State,
70 ULONG PdeIndex = GET_ADDR_PDE(VirtualAddress);
71 ULONG PteIndex = GET_ADDR_PTE(VirtualAddress);
72 FAST486_PAGE_DIR DirectoryEntry;
73 FAST486_PAGE_TABLE TableEntry;
74 ULONG PageDirectory = State->ControlRegisters[FAST486_REG_CR3];
76 if ((State->Tlb != NULL)
77 && (State->Tlb[VirtualAddress >> 12] != INVALID_TLB_FIELD))
79 /* Return the cached entry */
80 return State->Tlb[VirtualAddress >> 12];
83 /* Read the directory entry */
84 State->MemReadCallback(State,
85 PageDirectory + PdeIndex * sizeof(ULONG),
86 &DirectoryEntry.Value,
87 sizeof(DirectoryEntry));
89 /* Make sure it is present */
90 if (!DirectoryEntry.Present) return 0;
92 /* Was the directory entry accessed before? */
93 if (!DirectoryEntry.Accessed)
96 DirectoryEntry.Accessed = TRUE;
98 /* Write back the directory entry */
99 State->MemWriteCallback(State,
100 PageDirectory + PdeIndex * sizeof(ULONG),
101 &DirectoryEntry.Value,
102 sizeof(DirectoryEntry));
105 /* Read the table entry */
106 State->MemReadCallback(State,
107 (DirectoryEntry.TableAddress << 12)
108 + PteIndex * sizeof(ULONG),
112 /* Make sure it is present */
113 if (!TableEntry.Present) return 0;
115 if (MarkAsDirty) TableEntry.Dirty = TRUE;
117 /* Was the table entry accessed before? */
118 if (!TableEntry.Accessed)
120 /* Well, it is now */
121 TableEntry.Accessed = TRUE;
123 /* Write back the table entry */
124 State->MemWriteCallback(State,
125 (DirectoryEntry.TableAddress << 12)
126 + PteIndex * sizeof(ULONG),
132 * The resulting permissions depend on the permissions
133 * in the page directory table too
135 TableEntry.Writeable &= DirectoryEntry.Writeable;
136 TableEntry.Usermode &= DirectoryEntry.Usermode;
138 if (State->Tlb != NULL)
140 /* Set the TLB entry */
141 State->Tlb[VirtualAddress >> 12] = TableEntry.Value;
144 /* Return the table entry */
145 return TableEntry.Value;
151 Fast486ReadLinearMemory(PFAST486_STATE State,
156 /* Check if paging is enabled */
157 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
160 FAST486_PAGE_TABLE TableEntry;
161 INT Cpl = Fast486GetCurrentPrivLevel(State);
162 ULONG BufferOffset = 0;
164 for (Page = PAGE_ALIGN(LinearAddress);
165 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
168 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
170 /* Get the table entry */
171 TableEntry.Value = Fast486GetPageTableEntry(State, Page, FALSE);
173 if (!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
176 Fast486ExceptionWithErrorCode(State,
177 FAST486_EXCEPTION_PF,
178 TableEntry.Value & 0x07);
182 /* Check if this is the first page */
183 if (Page == PAGE_ALIGN(LinearAddress))
185 /* Start reading from the offset from the beginning of the page */
186 PageOffset = PAGE_OFFSET(LinearAddress);
187 PageLength -= PageOffset;
190 /* Check if this is the last page */
191 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
193 /* Read only a part of the page */
194 PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
197 /* Read the memory */
198 State->MemReadCallback(State,
199 (TableEntry.Address << 12) | PageOffset,
200 (PVOID)((ULONG_PTR)Buffer + BufferOffset),
203 BufferOffset += PageLength;
208 /* Read the memory */
209 State->MemReadCallback(State, LinearAddress, Buffer, Size);
218 Fast486WriteLinearMemory(PFAST486_STATE State,
223 /* Check if paging is enabled */
224 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
227 FAST486_PAGE_TABLE TableEntry;
228 INT Cpl = Fast486GetCurrentPrivLevel(State);
229 ULONG BufferOffset = 0;
231 for (Page = PAGE_ALIGN(LinearAddress);
232 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
235 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
237 /* Get the table entry */
238 TableEntry.Value = Fast486GetPageTableEntry(State, Page, TRUE);
240 if ((!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
241 || ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_WP)
242 && !TableEntry.Writeable))
245 Fast486ExceptionWithErrorCode(State,
246 FAST486_EXCEPTION_PF,
247 TableEntry.Value & 0x07);
251 /* Check if this is the first page */
252 if (Page == PAGE_ALIGN(LinearAddress))
254 /* Start writing from the offset from the beginning of the page */
255 PageOffset = PAGE_OFFSET(LinearAddress);
256 PageLength -= PageOffset;
259 /* Check if this is the last page */
260 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
262 /* Write only a part of the page */
263 PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
266 /* Write the memory */
267 State->MemWriteCallback(State,
268 (TableEntry.Address << 12) | PageOffset,
269 (PVOID)((ULONG_PTR)Buffer + BufferOffset),
272 BufferOffset += PageLength;
277 /* Write the memory */
278 State->MemWriteCallback(State, LinearAddress, Buffer, Size);
287 Fast486Exception(PFAST486_STATE State,
288 FAST486_EXCEPTIONS ExceptionCode)
290 /* Call the internal function */
291 Fast486ExceptionWithErrorCode(State, ExceptionCode, 0);
297 Fast486StackPush(PFAST486_STATE State,
300 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
302 /* The OPSIZE prefix toggles the size */
303 if (State->PrefixFlags & FAST486_PREFIX_OPSIZE) Size = !Size;
309 /* Check if ESP is between 1 and 3 */
310 if (State->GeneralRegs[FAST486_REG_ESP].Long >= 1
311 && State->GeneralRegs[FAST486_REG_ESP].Long <= 3)
313 Fast486Exception(State, FAST486_EXCEPTION_SS);
317 /* Subtract ESP by 4 */
318 State->GeneralRegs[FAST486_REG_ESP].Long -= sizeof(ULONG);
320 /* Store the value in SS:ESP */
321 return Fast486WriteMemory(State,
323 State->GeneralRegs[FAST486_REG_ESP].Long,
330 USHORT ShortValue = LOWORD(Value);
332 /* Check if SP is 1 */
333 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 1)
335 Fast486Exception(State, FAST486_EXCEPTION_SS);
339 /* Subtract SP by 2 */
340 State->GeneralRegs[FAST486_REG_ESP].LowWord -= sizeof(USHORT);
342 /* Store the value in SS:SP */
343 return Fast486WriteMemory(State,
345 State->GeneralRegs[FAST486_REG_ESP].LowWord,
354 Fast486StackPop(PFAST486_STATE State,
357 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
359 /* The OPSIZE prefix toggles the size */
367 /* Check if ESP is 0xFFFFFFFF */
368 if (State->GeneralRegs[FAST486_REG_ESP].Long == 0xFFFFFFFF)
370 Fast486Exception(State, FAST486_EXCEPTION_SS);
374 /* Read the value from SS:ESP */
375 if (!Fast486ReadMemory(State,
377 State->GeneralRegs[FAST486_REG_ESP].Long,
382 /* An exception occurred */
386 /* Increment ESP by 4 */
387 State->GeneralRegs[FAST486_REG_ESP].Long += sizeof(ULONG);
389 /* Store the value in the result */
397 /* Check if SP is 0xFFFF */
398 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 0xFFFF)
400 Fast486Exception(State, FAST486_EXCEPTION_SS);
404 /* Read the value from SS:SP */
405 if (!Fast486ReadMemory(State,
407 State->GeneralRegs[FAST486_REG_ESP].LowWord,
412 /* An exception occurred */
416 /* Increment SP by 2 */
417 State->GeneralRegs[FAST486_REG_ESP].LowWord += sizeof(USHORT);
419 /* Store the value in the result */
429 Fast486LoadSegment(PFAST486_STATE State,
430 FAST486_SEG_REGS Segment,
433 PFAST486_SEG_REG CachedDescriptor;
434 FAST486_GDT_ENTRY GdtEntry;
436 ASSERT(Segment < FAST486_NUM_SEG_REGS);
438 /* Get the cached descriptor */
439 CachedDescriptor = &State->SegmentRegs[Segment];
441 /* Check for protected mode */
442 if ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE) && !State->Flags.Vm)
444 if (!(Selector & SEGMENT_TABLE_INDICATOR))
446 /* Make sure the GDT contains the entry */
447 if (GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
449 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
454 if (!Fast486ReadLinearMemory(State,
456 + GET_SEGMENT_INDEX(Selector),
460 /* Exception occurred */
466 /* Make sure the LDT contains the entry */
467 if (GET_SEGMENT_INDEX(Selector) >= (State->Ldtr.Limit + 1))
469 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
474 if (!Fast486ReadLinearMemory(State,
476 + GET_SEGMENT_INDEX(Selector),
480 /* Exception occurred */
485 if (Segment == FAST486_REG_SS)
487 /* Loading the stack segment */
489 if (GET_SEGMENT_INDEX(Selector) == 0)
491 Fast486Exception(State, FAST486_EXCEPTION_GP);
495 if (!GdtEntry.SystemType)
497 /* This is a special descriptor */
498 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
502 if (GdtEntry.Executable || !GdtEntry.ReadWrite)
504 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
508 if ((GET_SEGMENT_RPL(Selector) != Fast486GetCurrentPrivLevel(State))
509 || (GET_SEGMENT_RPL(Selector) != GdtEntry.Dpl))
511 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
515 if (!GdtEntry.Present)
517 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_SS, Selector);
521 else if (Segment == FAST486_REG_CS)
523 /* Loading the code segment */
525 if (GET_SEGMENT_INDEX(Selector) == 0)
527 Fast486Exception(State, FAST486_EXCEPTION_GP);
531 if (!GdtEntry.SystemType)
533 // TODO: Call/interrupt/task gates NOT IMPLEMENTED!
538 if (!GdtEntry.Present)
540 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
544 if (!GdtEntry.Executable)
546 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
550 if (GdtEntry.DirConf)
552 /* Conforming Code Segment */
554 if (GdtEntry.Dpl > Fast486GetCurrentPrivLevel(State))
556 /* Must be accessed from lower-privileged code */
557 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
563 /* Regular code segment */
565 if ((GET_SEGMENT_RPL(Selector) > Fast486GetCurrentPrivLevel(State))
566 || (Fast486GetCurrentPrivLevel(State) != GdtEntry.Dpl))
568 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
574 State->Cpl = GET_SEGMENT_RPL(Selector);
579 /* Loading a data segment */
581 if (GET_SEGMENT_INDEX(Selector) != 0)
583 if (!GdtEntry.SystemType)
585 /* This is a special descriptor */
586 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
590 if ((GET_SEGMENT_RPL(Selector) > GdtEntry.Dpl)
591 || (Fast486GetCurrentPrivLevel(State) > GdtEntry.Dpl))
593 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
597 if (!GdtEntry.Present)
599 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
605 /* This is a NULL selector */
606 RtlZeroMemory(&GdtEntry, sizeof(GdtEntry));
610 /* Update the cache entry */
611 CachedDescriptor->Selector = Selector;
612 CachedDescriptor->Base = GdtEntry.Base | (GdtEntry.BaseMid << 16) | (GdtEntry.BaseHigh << 24);
613 CachedDescriptor->Limit = GdtEntry.Limit | (GdtEntry.LimitHigh << 16);
614 CachedDescriptor->Accessed = GdtEntry.Accessed;
615 CachedDescriptor->ReadWrite = GdtEntry.ReadWrite;
616 CachedDescriptor->DirConf = GdtEntry.DirConf;
617 CachedDescriptor->Executable = GdtEntry.Executable;
618 CachedDescriptor->SystemType = GdtEntry.SystemType;
619 CachedDescriptor->Rpl = GET_SEGMENT_RPL(Selector);
620 CachedDescriptor->Dpl = GdtEntry.Dpl;
621 CachedDescriptor->Present = GdtEntry.Present;
622 CachedDescriptor->Size = GdtEntry.Size;
624 /* Check for page granularity */
625 if (GdtEntry.Granularity) CachedDescriptor->Limit <<= 12;
629 /* Update the selector and base */
630 CachedDescriptor->Selector = Selector;
631 CachedDescriptor->Base = Selector << 4;
640 Fast486FetchByte(PFAST486_STATE State,
643 PFAST486_SEG_REG CachedDescriptor;
645 /* Get the cached descriptor of CS */
646 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
648 /* Read from memory */
649 if (!Fast486ReadMemory(State,
651 (CachedDescriptor->Size) ? State->InstPtr.Long
652 : State->InstPtr.LowWord,
657 /* Exception occurred during instruction fetch */
661 /* Advance the instruction pointer */
662 if (CachedDescriptor->Size) State->InstPtr.Long++;
663 else State->InstPtr.LowWord++;
671 Fast486FetchWord(PFAST486_STATE State,
674 PFAST486_SEG_REG CachedDescriptor;
676 /* Get the cached descriptor of CS */
677 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
679 /* Read from memory */
680 // FIXME: Fix byte order on big-endian machines
681 if (!Fast486ReadMemory(State,
683 (CachedDescriptor->Size) ? State->InstPtr.Long
684 : State->InstPtr.LowWord,
689 /* Exception occurred during instruction fetch */
693 /* Advance the instruction pointer */
694 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(USHORT);
695 else State->InstPtr.LowWord += sizeof(USHORT);
703 Fast486FetchDword(PFAST486_STATE State,
706 PFAST486_SEG_REG CachedDescriptor;
708 /* Get the cached descriptor of CS */
709 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
711 /* Read from memory */
712 // FIXME: Fix byte order on big-endian machines
713 if (!Fast486ReadMemory(State,
715 (CachedDescriptor->Size) ? State->InstPtr.Long
716 : State->InstPtr.LowWord,
721 /* Exception occurred during instruction fetch */
725 /* Advance the instruction pointer */
726 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(ULONG);
727 else State->InstPtr.LowWord += sizeof(ULONG);
735 Fast486CalculateParity(UCHAR Number)
737 // See http://graphics.stanford.edu/~seander/bithacks.html#ParityLookupTable too...
738 return (0x9669 >> ((Number & 0x0F) ^ (Number >> 4))) & 1;
744 Fast486ParseModRegRm(PFAST486_STATE State,
746 PFAST486_MOD_REG_RM ModRegRm)
748 UCHAR ModRmByte, Mode, RegMem;
750 /* Fetch the MOD REG R/M byte */
751 if (!Fast486FetchByte(State, &ModRmByte))
753 /* Exception occurred */
757 /* Unpack the mode and R/M */
758 Mode = ModRmByte >> 6;
759 RegMem = ModRmByte & 0x07;
761 /* Set the register operand */
762 ModRegRm->Register = (ModRmByte >> 3) & 0x07;
765 if ((ModRmByte >> 6) == 3)
767 /* The second operand is also a register */
768 ModRegRm->Memory = FALSE;
769 ModRegRm->SecondRegister = RegMem;
775 /* The second operand is memory */
776 ModRegRm->Memory = TRUE;
780 if (RegMem == FAST486_REG_ESP)
783 ULONG Scale, Index, Base;
785 /* Fetch the SIB byte */
786 if (!Fast486FetchByte(State, &SibByte))
788 /* Exception occurred */
792 /* Unpack the scale, index and base */
793 Scale = 1 << (SibByte >> 6);
794 Index = (SibByte >> 3) & 0x07;
795 if (Index != FAST486_REG_ESP) Index = State->GeneralRegs[Index].Long;
798 if (((SibByte & 0x07) != FAST486_REG_EBP) || (Mode != 0))
800 /* Use the register a base */
801 Base = State->GeneralRegs[SibByte & 0x07].Long;
806 if (!Fast486FetchDword(State, &Base))
808 /* Exception occurred */
813 if ((SibByte & 0x07) == FAST486_REG_ESP)
815 /* Check if there is no segment override */
816 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
818 /* Add a SS: prefix */
819 State->PrefixFlags |= FAST486_PREFIX_SEG;
820 State->SegmentOverride = FAST486_REG_SS;
824 /* Calculate the address */
825 ModRegRm->MemoryAddress = Base + Index * Scale;
827 else if (RegMem == FAST486_REG_EBP)
829 if (Mode) ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].Long;
830 else ModRegRm->MemoryAddress = 0;
834 /* Get the base from the register */
835 ModRegRm->MemoryAddress = State->GeneralRegs[RegMem].Long;
838 /* Check if there is no segment override */
839 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
841 /* Check if the default segment should be SS */
842 if ((RegMem == FAST486_REG_EBP) && Mode)
844 /* Add a SS: prefix */
845 State->PrefixFlags |= FAST486_PREFIX_SEG;
846 State->SegmentOverride = FAST486_REG_SS;
855 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
857 /* Exception occurred */
861 /* Add the signed offset to the address */
862 ModRegRm->MemoryAddress += (LONG)Offset;
864 else if ((Mode == 2) || ((Mode == 0) && (RegMem == FAST486_REG_EBP)))
868 /* Fetch the dword */
869 if (!Fast486FetchDword(State, (PULONG)&Offset))
871 /* Exception occurred */
875 /* Add the signed offset to the address */
876 ModRegRm->MemoryAddress += Offset;
881 /* Check the operand */
887 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
888 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
896 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
897 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
905 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
906 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
914 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
915 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
923 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_ESI].LowWord;
931 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EDI].LowWord;
941 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord;
945 /* [constant] (added later) */
946 ModRegRm->MemoryAddress = 0;
955 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord;
961 /* Check if there is no segment override */
962 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
964 /* Check if the default segment should be SS */
965 if ((RegMem == 2) || (RegMem == 3) || ((RegMem == 6) && Mode))
967 /* Add a SS: prefix */
968 State->PrefixFlags |= FAST486_PREFIX_SEG;
969 State->SegmentOverride = FAST486_REG_SS;
978 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
980 /* Exception occurred */
984 /* Add the signed offset to the address */
985 ModRegRm->MemoryAddress += (LONG)Offset;
987 else if ((Mode == 2) || ((Mode == 0) && (RegMem == 6)))
992 if (!Fast486FetchWord(State, (PUSHORT)&Offset))
994 /* Exception occurred */
998 /* Add the signed offset to the address */
999 ModRegRm->MemoryAddress += (LONG)Offset;
1002 /* Clear the top 16 bits */
1003 ModRegRm->MemoryAddress &= 0x0000FFFF;
1012 Fast486ReadModrmByteOperands(PFAST486_STATE State,
1013 PFAST486_MOD_REG_RM ModRegRm,
1017 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1021 /* Get the register value */
1022 if (ModRegRm->Register & 0x04)
1024 /* AH, CH, DH, BH */
1025 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].HighByte;
1029 /* AL, CL, DL, BL */
1030 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].LowByte;
1036 if (!ModRegRm->Memory)
1038 /* Get the second register value */
1039 if (ModRegRm->SecondRegister & 0x04)
1041 /* AH, CH, DH, BH */
1042 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte;
1046 /* AL, CL, DL, BL */
1047 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte;
1052 /* Check for the segment override */
1053 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1055 /* Use the override segment instead */
1056 Segment = State->SegmentOverride;
1060 if (!Fast486ReadMemory(State,
1062 ModRegRm->MemoryAddress,
1067 /* Exception occurred */
1079 Fast486ReadModrmWordOperands(PFAST486_STATE State,
1080 PFAST486_MOD_REG_RM ModRegRm,
1084 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1088 /* Get the register value */
1089 *RegValue = State->GeneralRegs[ModRegRm->Register].LowWord;
1094 if (!ModRegRm->Memory)
1096 /* Get the second register value */
1097 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].LowWord;
1101 /* Check for the segment override */
1102 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1104 /* Use the override segment instead */
1105 Segment = State->SegmentOverride;
1109 if (!Fast486ReadMemory(State,
1111 ModRegRm->MemoryAddress,
1116 /* Exception occurred */
1128 Fast486ReadModrmDwordOperands(PFAST486_STATE State,
1129 PFAST486_MOD_REG_RM ModRegRm,
1133 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1137 /* Get the register value */
1138 *RegValue = State->GeneralRegs[ModRegRm->Register].Long;
1143 if (!ModRegRm->Memory)
1145 /* Get the second register value */
1146 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].Long;
1150 /* Check for the segment override */
1151 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1153 /* Use the override segment instead */
1154 Segment = State->SegmentOverride;
1158 if (!Fast486ReadMemory(State,
1160 ModRegRm->MemoryAddress,
1165 /* Exception occurred */
1177 Fast486WriteModrmByteOperands(PFAST486_STATE State,
1178 PFAST486_MOD_REG_RM ModRegRm,
1179 BOOLEAN WriteRegister,
1182 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1186 /* Store the value in the register */
1187 if (ModRegRm->Register & 0x04)
1189 /* AH, CH, DH, BH */
1190 State->GeneralRegs[ModRegRm->Register & 0x03].HighByte = Value;
1194 /* AL, CL, DL, BL */
1195 State->GeneralRegs[ModRegRm->Register & 0x03].LowByte = Value;
1200 if (!ModRegRm->Memory)
1202 /* Store the value in the second register */
1203 if (ModRegRm->SecondRegister & 0x04)
1205 /* AH, CH, DH, BH */
1206 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte = Value;
1210 /* AL, CL, DL, BL */
1211 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte = Value;
1216 /* Check for the segment override */
1217 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1219 /* Use the override segment instead */
1220 Segment = State->SegmentOverride;
1224 if (!Fast486WriteMemory(State,
1226 ModRegRm->MemoryAddress,
1230 /* Exception occurred */
1242 Fast486WriteModrmWordOperands(PFAST486_STATE State,
1243 PFAST486_MOD_REG_RM ModRegRm,
1244 BOOLEAN WriteRegister,
1247 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1251 /* Store the value in the register */
1252 State->GeneralRegs[ModRegRm->Register].LowWord = Value;
1256 if (!ModRegRm->Memory)
1258 /* Store the value in the second register */
1259 State->GeneralRegs[ModRegRm->SecondRegister].LowWord = Value;
1263 /* Check for the segment override */
1264 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1266 /* Use the override segment instead */
1267 Segment = State->SegmentOverride;
1271 if (!Fast486WriteMemory(State,
1273 ModRegRm->MemoryAddress,
1277 /* 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 */
1333 #ifndef FAST486_NO_FPU
1338 Fast486FpuNormalize(PFAST486_STATE State,
1339 PFAST486_FPU_DATA_REG Data)
1343 if (FPU_IS_NORMALIZED(Data)) return;
1344 if (FPU_IS_ZERO(Data))
1350 LeadingZeros = CountLeadingZeros64(Data->Mantissa);
1352 if (LeadingZeros < Data->Exponent)
1354 Data->Mantissa <<= LeadingZeros;
1355 Data->Exponent -= LeadingZeros;
1359 /* Make it denormalized */
1360 Data->Mantissa <<= Data->Exponent - 1;
1364 State->FpuStatus.Ue = TRUE;
1371 Fast486GetValueTag(PFAST486_FPU_DATA_REG Data)
1373 if (FPU_IS_ZERO(Data)) return FPU_TAG_ZERO;
1374 else if (FPU_IS_NAN(Data)) return FPU_TAG_SPECIAL;
1375 else return FPU_TAG_VALID;
1381 Fast486FpuPush(PFAST486_STATE State,
1382 PFAST486_FPU_DATA_REG Data)
1384 State->FpuStatus.Top--;
1386 if (FPU_GET_TAG(0) == FPU_TAG_EMPTY)
1389 FPU_SET_TAG(0, Fast486GetValueTag(Data));
1391 else State->FpuStatus.Ie = TRUE;
1397 Fast486FpuPop(PFAST486_STATE State)
1399 if (FPU_GET_TAG(0) != FPU_TAG_EMPTY)
1401 FPU_SET_TAG(0, FPU_TAG_EMPTY);
1402 State->FpuStatus.Top++;
1404 else State->FpuStatus.Ie = TRUE;