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)
29 #elif (_MSC_VER >= 1500) && defined(_WIN64)
30 #define CountLeadingZeros64(x) __lzcnt64(x)
31 #elif (_MSC_VER >= 1500)
32 #define CountLeadingZeros64(x) ((x) > 0xFFFFFFFFULL) ? __lzcnt((x) >> 32) \
36 ULONG CountLeadingZeros64(ULONGLONG Value)
39 ULONGLONG Mask = 1ULL << 63;
41 while (!(Value & Mask))
53 Fast486GetCurrentPrivLevel(PFAST486_STATE State)
55 /* Return the CPL, or 3 if we're in virtual 8086 mode */
56 return (!State->Flags.Vm) ? State->Cpl : 3;
61 Fast486GetPageTableEntry(PFAST486_STATE State,
65 ULONG PdeIndex = GET_ADDR_PDE(VirtualAddress);
66 ULONG PteIndex = GET_ADDR_PTE(VirtualAddress);
67 FAST486_PAGE_DIR DirectoryEntry;
68 FAST486_PAGE_TABLE TableEntry;
69 ULONG PageDirectory = State->ControlRegisters[FAST486_REG_CR3];
71 if ((State->Tlb != NULL)
72 && (State->Tlb[VirtualAddress >> 12] != INVALID_TLB_FIELD))
74 /* Return the cached entry */
75 return State->Tlb[VirtualAddress >> 12];
78 /* Read the directory entry */
79 State->MemReadCallback(State,
80 PageDirectory + PdeIndex * sizeof(ULONG),
81 &DirectoryEntry.Value,
82 sizeof(DirectoryEntry));
84 /* Make sure it is present */
85 if (!DirectoryEntry.Present) return 0;
87 /* Was the directory entry accessed before? */
88 if (!DirectoryEntry.Accessed)
91 DirectoryEntry.Accessed = TRUE;
93 /* Write back the directory entry */
94 State->MemWriteCallback(State,
95 PageDirectory + PdeIndex * sizeof(ULONG),
96 &DirectoryEntry.Value,
97 sizeof(DirectoryEntry));
100 /* Read the table entry */
101 State->MemReadCallback(State,
102 (DirectoryEntry.TableAddress << 12)
103 + PteIndex * sizeof(ULONG),
107 /* Make sure it is present */
108 if (!TableEntry.Present) return 0;
110 if (MarkAsDirty) TableEntry.Dirty = TRUE;
112 /* Was the table entry accessed before? */
113 if (!TableEntry.Accessed)
115 /* Well, it is now */
116 TableEntry.Accessed = TRUE;
118 /* Write back the table entry */
119 State->MemWriteCallback(State,
120 (DirectoryEntry.TableAddress << 12)
121 + PteIndex * sizeof(ULONG),
127 * The resulting permissions depend on the permissions
128 * in the page directory table too
130 TableEntry.Writeable &= DirectoryEntry.Writeable;
131 TableEntry.Usermode &= DirectoryEntry.Usermode;
133 if (State->Tlb != NULL)
135 /* Set the TLB entry */
136 State->Tlb[VirtualAddress >> 12] = TableEntry.Value;
139 /* Return the table entry */
140 return TableEntry.Value;
145 Fast486ReadLinearMemory(PFAST486_STATE State,
150 /* Check if paging is enabled */
151 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
154 FAST486_PAGE_TABLE TableEntry;
155 INT Cpl = Fast486GetCurrentPrivLevel(State);
156 ULONG BufferOffset = 0;
158 for (Page = PAGE_ALIGN(LinearAddress);
159 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
162 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
164 /* Get the table entry */
165 TableEntry.Value = Fast486GetPageTableEntry(State, Page, FALSE);
167 if (!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
170 Fast486ExceptionWithErrorCode(State,
171 FAST486_EXCEPTION_PF,
172 TableEntry.Value & 0x07);
176 /* Check if this is the first page */
177 if (Page == PAGE_ALIGN(LinearAddress))
179 /* Start reading from the offset from the beginning of the page */
180 PageOffset = PAGE_OFFSET(LinearAddress);
181 PageLength -= PageOffset;
184 /* Check if this is the last page */
185 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
187 /* Read only a part of the page */
188 PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
191 /* Read the memory */
192 State->MemReadCallback(State,
193 (TableEntry.Address << 12) | PageOffset,
194 (PVOID)((ULONG_PTR)Buffer + BufferOffset),
197 BufferOffset += PageLength;
202 /* Read the memory */
203 State->MemReadCallback(State, LinearAddress, Buffer, Size);
211 Fast486WriteLinearMemory(PFAST486_STATE State,
216 /* Check if paging is enabled */
217 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
220 FAST486_PAGE_TABLE TableEntry;
221 INT Cpl = Fast486GetCurrentPrivLevel(State);
222 ULONG BufferOffset = 0;
224 for (Page = PAGE_ALIGN(LinearAddress);
225 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
228 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
230 /* Get the table entry */
231 TableEntry.Value = Fast486GetPageTableEntry(State, Page, TRUE);
233 if ((!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
234 || ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_WP)
235 && !TableEntry.Writeable))
238 Fast486ExceptionWithErrorCode(State,
239 FAST486_EXCEPTION_PF,
240 TableEntry.Value & 0x07);
244 /* Check if this is the first page */
245 if (Page == PAGE_ALIGN(LinearAddress))
247 /* Start writing from the offset from the beginning of the page */
248 PageOffset = PAGE_OFFSET(LinearAddress);
249 PageLength -= PageOffset;
252 /* Check if this is the last page */
253 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
255 /* Write only a part of the page */
256 PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
259 /* Write the memory */
260 State->MemWriteCallback(State,
261 (TableEntry.Address << 12) | PageOffset,
262 (PVOID)((ULONG_PTR)Buffer + BufferOffset),
265 BufferOffset += PageLength;
270 /* Write the memory */
271 State->MemWriteCallback(State, LinearAddress, Buffer, Size);
279 Fast486Exception(PFAST486_STATE State,
280 FAST486_EXCEPTIONS ExceptionCode)
282 /* Call the internal function */
283 Fast486ExceptionWithErrorCode(State, ExceptionCode, 0);
288 Fast486StackPush(PFAST486_STATE State,
291 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
293 /* The OPSIZE prefix toggles the size */
294 if (State->PrefixFlags & FAST486_PREFIX_OPSIZE) Size = !Size;
300 /* Check if ESP is between 1 and 3 */
301 if (State->GeneralRegs[FAST486_REG_ESP].Long >= 1
302 && State->GeneralRegs[FAST486_REG_ESP].Long <= 3)
304 Fast486Exception(State, FAST486_EXCEPTION_SS);
308 /* Subtract ESP by 4 */
309 State->GeneralRegs[FAST486_REG_ESP].Long -= sizeof(ULONG);
311 /* Store the value in SS:ESP */
312 return Fast486WriteMemory(State,
314 State->GeneralRegs[FAST486_REG_ESP].Long,
321 USHORT ShortValue = LOWORD(Value);
323 /* Check if SP is 1 */
324 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 1)
326 Fast486Exception(State, FAST486_EXCEPTION_SS);
330 /* Subtract SP by 2 */
331 State->GeneralRegs[FAST486_REG_ESP].LowWord -= sizeof(USHORT);
333 /* Store the value in SS:SP */
334 return Fast486WriteMemory(State,
336 State->GeneralRegs[FAST486_REG_ESP].LowWord,
344 Fast486StackPop(PFAST486_STATE State,
347 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
349 /* The OPSIZE prefix toggles the size */
357 /* Check if ESP is 0xFFFFFFFF */
358 if (State->GeneralRegs[FAST486_REG_ESP].Long == 0xFFFFFFFF)
360 Fast486Exception(State, FAST486_EXCEPTION_SS);
364 /* Read the value from SS:ESP */
365 if (!Fast486ReadMemory(State,
367 State->GeneralRegs[FAST486_REG_ESP].Long,
372 /* An exception occurred */
376 /* Increment ESP by 4 */
377 State->GeneralRegs[FAST486_REG_ESP].Long += sizeof(ULONG);
379 /* Store the value in the result */
387 /* Check if SP is 0xFFFF */
388 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 0xFFFF)
390 Fast486Exception(State, FAST486_EXCEPTION_SS);
394 /* Read the value from SS:SP */
395 if (!Fast486ReadMemory(State,
397 State->GeneralRegs[FAST486_REG_ESP].LowWord,
402 /* An exception occurred */
406 /* Increment SP by 2 */
407 State->GeneralRegs[FAST486_REG_ESP].LowWord += sizeof(USHORT);
409 /* Store the value in the result */
418 Fast486LoadSegment(PFAST486_STATE State,
419 FAST486_SEG_REGS Segment,
422 PFAST486_SEG_REG CachedDescriptor;
423 FAST486_GDT_ENTRY GdtEntry;
425 ASSERT(Segment < FAST486_NUM_SEG_REGS);
427 /* Get the cached descriptor */
428 CachedDescriptor = &State->SegmentRegs[Segment];
430 /* Check for protected mode */
431 if ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE) && !State->Flags.Vm)
433 if (!(Selector & SEGMENT_TABLE_INDICATOR))
435 /* Make sure the GDT contains the entry */
436 if (GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
438 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
443 if (!Fast486ReadLinearMemory(State,
445 + GET_SEGMENT_INDEX(Selector),
449 /* Exception occurred */
455 /* Make sure the LDT contains the entry */
456 if (GET_SEGMENT_INDEX(Selector) >= (State->Ldtr.Limit + 1))
458 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
463 if (!Fast486ReadLinearMemory(State,
465 + GET_SEGMENT_INDEX(Selector),
469 /* Exception occurred */
474 if (Segment == FAST486_REG_SS)
476 /* Loading the stack segment */
478 if (GET_SEGMENT_INDEX(Selector) == 0)
480 Fast486Exception(State, FAST486_EXCEPTION_GP);
484 if (!GdtEntry.SystemType)
486 /* This is a special descriptor */
487 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
491 if (GdtEntry.Executable || !GdtEntry.ReadWrite)
493 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
497 if ((GET_SEGMENT_RPL(Selector) != Fast486GetCurrentPrivLevel(State))
498 || (GET_SEGMENT_RPL(Selector) != GdtEntry.Dpl))
500 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
504 if (!GdtEntry.Present)
506 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_SS, Selector);
510 else if (Segment == FAST486_REG_CS)
512 /* Loading the code segment */
514 if (GET_SEGMENT_INDEX(Selector) == 0)
516 Fast486Exception(State, FAST486_EXCEPTION_GP);
520 if (!GdtEntry.SystemType)
522 // TODO: Call/interrupt/task gates NOT IMPLEMENTED!
527 if (!GdtEntry.Present)
529 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
533 if (!GdtEntry.Executable)
535 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
539 if (GdtEntry.DirConf)
541 /* Conforming Code Segment */
543 if (GdtEntry.Dpl > Fast486GetCurrentPrivLevel(State))
545 /* Must be accessed from lower-privileged code */
546 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
552 /* Regular code segment */
554 if ((GET_SEGMENT_RPL(Selector) > Fast486GetCurrentPrivLevel(State))
555 || (Fast486GetCurrentPrivLevel(State) != GdtEntry.Dpl))
557 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
563 State->Cpl = GET_SEGMENT_RPL(Selector);
568 /* Loading a data segment */
570 if (GET_SEGMENT_INDEX(Selector) != 0)
572 if (!GdtEntry.SystemType)
574 /* This is a special descriptor */
575 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
579 if ((GET_SEGMENT_RPL(Selector) > GdtEntry.Dpl)
580 || (Fast486GetCurrentPrivLevel(State) > GdtEntry.Dpl))
582 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
586 if (!GdtEntry.Present)
588 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
594 /* This is a NULL selector */
595 RtlZeroMemory(&GdtEntry, sizeof(GdtEntry));
599 /* Update the cache entry */
600 CachedDescriptor->Selector = Selector;
601 CachedDescriptor->Base = GdtEntry.Base | (GdtEntry.BaseMid << 16) | (GdtEntry.BaseHigh << 24);
602 CachedDescriptor->Limit = GdtEntry.Limit | (GdtEntry.LimitHigh << 16);
603 CachedDescriptor->Accessed = GdtEntry.Accessed;
604 CachedDescriptor->ReadWrite = GdtEntry.ReadWrite;
605 CachedDescriptor->DirConf = GdtEntry.DirConf;
606 CachedDescriptor->Executable = GdtEntry.Executable;
607 CachedDescriptor->SystemType = GdtEntry.SystemType;
608 CachedDescriptor->Rpl = GET_SEGMENT_RPL(Selector);
609 CachedDescriptor->Dpl = GdtEntry.Dpl;
610 CachedDescriptor->Present = GdtEntry.Present;
611 CachedDescriptor->Size = GdtEntry.Size;
613 /* Check for page granularity */
614 if (GdtEntry.Granularity) CachedDescriptor->Limit <<= 12;
618 /* Update the selector and base */
619 CachedDescriptor->Selector = Selector;
620 CachedDescriptor->Base = Selector << 4;
628 Fast486FetchByte(PFAST486_STATE State,
631 PFAST486_SEG_REG CachedDescriptor;
633 /* Get the cached descriptor of CS */
634 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
636 /* Read from memory */
637 if (!Fast486ReadMemory(State,
639 (CachedDescriptor->Size) ? State->InstPtr.Long
640 : State->InstPtr.LowWord,
645 /* Exception occurred during instruction fetch */
649 /* Advance the instruction pointer */
650 if (CachedDescriptor->Size) State->InstPtr.Long++;
651 else State->InstPtr.LowWord++;
658 Fast486FetchWord(PFAST486_STATE State,
661 PFAST486_SEG_REG CachedDescriptor;
663 /* Get the cached descriptor of CS */
664 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
666 /* Read from memory */
667 // FIXME: Fix byte order on big-endian machines
668 if (!Fast486ReadMemory(State,
670 (CachedDescriptor->Size) ? State->InstPtr.Long
671 : State->InstPtr.LowWord,
676 /* Exception occurred during instruction fetch */
680 /* Advance the instruction pointer */
681 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(USHORT);
682 else State->InstPtr.LowWord += sizeof(USHORT);
689 Fast486FetchDword(PFAST486_STATE State,
692 PFAST486_SEG_REG CachedDescriptor;
694 /* Get the cached descriptor of CS */
695 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
697 /* Read from memory */
698 // FIXME: Fix byte order on big-endian machines
699 if (!Fast486ReadMemory(State,
701 (CachedDescriptor->Size) ? State->InstPtr.Long
702 : State->InstPtr.LowWord,
707 /* Exception occurred during instruction fetch */
711 /* Advance the instruction pointer */
712 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(ULONG);
713 else State->InstPtr.LowWord += sizeof(ULONG);
720 Fast486CalculateParity(UCHAR Number)
722 // See http://graphics.stanford.edu/~seander/bithacks.html#ParityLookupTable too...
723 return (0x9669 >> ((Number & 0x0F) ^ (Number >> 4))) & 1;
728 Fast486ParseModRegRm(PFAST486_STATE State,
730 PFAST486_MOD_REG_RM ModRegRm)
732 UCHAR ModRmByte, Mode, RegMem;
734 /* Fetch the MOD REG R/M byte */
735 if (!Fast486FetchByte(State, &ModRmByte))
737 /* Exception occurred */
741 /* Unpack the mode and R/M */
742 Mode = ModRmByte >> 6;
743 RegMem = ModRmByte & 0x07;
745 /* Set the register operand */
746 ModRegRm->Register = (ModRmByte >> 3) & 0x07;
749 if ((ModRmByte >> 6) == 3)
751 /* The second operand is also a register */
752 ModRegRm->Memory = FALSE;
753 ModRegRm->SecondRegister = RegMem;
759 /* The second operand is memory */
760 ModRegRm->Memory = TRUE;
764 if (RegMem == FAST486_REG_ESP)
767 ULONG Scale, Index, Base;
769 /* Fetch the SIB byte */
770 if (!Fast486FetchByte(State, &SibByte))
772 /* Exception occurred */
776 /* Unpack the scale, index and base */
777 Scale = 1 << (SibByte >> 6);
778 Index = (SibByte >> 3) & 0x07;
779 if (Index != FAST486_REG_ESP) Index = State->GeneralRegs[Index].Long;
782 if (((SibByte & 0x07) != FAST486_REG_EBP) || (Mode != 0))
784 /* Use the register a base */
785 Base = State->GeneralRegs[SibByte & 0x07].Long;
790 if (!Fast486FetchDword(State, &Base))
792 /* Exception occurred */
797 if ((SibByte & 0x07) == FAST486_REG_ESP)
799 /* Check if there is no segment override */
800 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
802 /* Add a SS: prefix */
803 State->PrefixFlags |= FAST486_PREFIX_SEG;
804 State->SegmentOverride = FAST486_REG_SS;
808 /* Calculate the address */
809 ModRegRm->MemoryAddress = Base + Index * Scale;
811 else if (RegMem == FAST486_REG_EBP)
813 if (Mode) ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].Long;
814 else ModRegRm->MemoryAddress = 0;
818 /* Get the base from the register */
819 ModRegRm->MemoryAddress = State->GeneralRegs[RegMem].Long;
822 /* Check if there is no segment override */
823 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
825 /* Check if the default segment should be SS */
826 if ((RegMem == FAST486_REG_EBP) && Mode)
828 /* Add a SS: prefix */
829 State->PrefixFlags |= FAST486_PREFIX_SEG;
830 State->SegmentOverride = FAST486_REG_SS;
839 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
841 /* Exception occurred */
845 /* Add the signed offset to the address */
846 ModRegRm->MemoryAddress += (LONG)Offset;
848 else if ((Mode == 2) || ((Mode == 0) && (RegMem == FAST486_REG_EBP)))
852 /* Fetch the dword */
853 if (!Fast486FetchDword(State, (PULONG)&Offset))
855 /* Exception occurred */
859 /* Add the signed offset to the address */
860 ModRegRm->MemoryAddress += Offset;
865 /* Check the operand */
871 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
872 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
880 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
881 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
889 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
890 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
898 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
899 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
907 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_ESI].LowWord;
915 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EDI].LowWord;
925 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord;
929 /* [constant] (added later) */
930 ModRegRm->MemoryAddress = 0;
939 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord;
945 /* Check if there is no segment override */
946 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
948 /* Check if the default segment should be SS */
949 if ((RegMem == 2) || (RegMem == 3) || ((RegMem == 6) && Mode))
951 /* Add a SS: prefix */
952 State->PrefixFlags |= FAST486_PREFIX_SEG;
953 State->SegmentOverride = FAST486_REG_SS;
962 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
964 /* Exception occurred */
968 /* Add the signed offset to the address */
969 ModRegRm->MemoryAddress += (LONG)Offset;
971 else if ((Mode == 2) || ((Mode == 0) && (RegMem == 6)))
976 if (!Fast486FetchWord(State, (PUSHORT)&Offset))
978 /* Exception occurred */
982 /* Add the signed offset to the address */
983 ModRegRm->MemoryAddress += (LONG)Offset;
986 /* Clear the top 16 bits */
987 ModRegRm->MemoryAddress &= 0x0000FFFF;
995 Fast486ReadModrmByteOperands(PFAST486_STATE State,
996 PFAST486_MOD_REG_RM ModRegRm,
1000 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1004 /* Get the register value */
1005 if (ModRegRm->Register & 0x04)
1007 /* AH, CH, DH, BH */
1008 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].HighByte;
1012 /* AL, CL, DL, BL */
1013 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].LowByte;
1019 if (!ModRegRm->Memory)
1021 /* Get the second register value */
1022 if (ModRegRm->SecondRegister & 0x04)
1024 /* AH, CH, DH, BH */
1025 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte;
1029 /* AL, CL, DL, BL */
1030 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte;
1035 /* Check for the segment override */
1036 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1038 /* Use the override segment instead */
1039 Segment = State->SegmentOverride;
1043 if (!Fast486ReadMemory(State,
1045 ModRegRm->MemoryAddress,
1050 /* Exception occurred */
1061 Fast486ReadModrmWordOperands(PFAST486_STATE State,
1062 PFAST486_MOD_REG_RM ModRegRm,
1066 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1070 /* Get the register value */
1071 *RegValue = State->GeneralRegs[ModRegRm->Register].LowWord;
1076 if (!ModRegRm->Memory)
1078 /* Get the second register value */
1079 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].LowWord;
1083 /* Check for the segment override */
1084 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1086 /* Use the override segment instead */
1087 Segment = State->SegmentOverride;
1091 if (!Fast486ReadMemory(State,
1093 ModRegRm->MemoryAddress,
1098 /* Exception occurred */
1109 Fast486ReadModrmDwordOperands(PFAST486_STATE State,
1110 PFAST486_MOD_REG_RM ModRegRm,
1114 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1118 /* Get the register value */
1119 *RegValue = State->GeneralRegs[ModRegRm->Register].Long;
1124 if (!ModRegRm->Memory)
1126 /* Get the second register value */
1127 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].Long;
1131 /* Check for the segment override */
1132 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1134 /* Use the override segment instead */
1135 Segment = State->SegmentOverride;
1139 if (!Fast486ReadMemory(State,
1141 ModRegRm->MemoryAddress,
1146 /* Exception occurred */
1157 Fast486WriteModrmByteOperands(PFAST486_STATE State,
1158 PFAST486_MOD_REG_RM ModRegRm,
1159 BOOLEAN WriteRegister,
1162 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1166 /* Store the value in the register */
1167 if (ModRegRm->Register & 0x04)
1169 /* AH, CH, DH, BH */
1170 State->GeneralRegs[ModRegRm->Register & 0x03].HighByte = Value;
1174 /* AL, CL, DL, BL */
1175 State->GeneralRegs[ModRegRm->Register & 0x03].LowByte = Value;
1180 if (!ModRegRm->Memory)
1182 /* Store the value in the second register */
1183 if (ModRegRm->SecondRegister & 0x04)
1185 /* AH, CH, DH, BH */
1186 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte = Value;
1190 /* AL, CL, DL, BL */
1191 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte = Value;
1196 /* Check for the segment override */
1197 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1199 /* Use the override segment instead */
1200 Segment = State->SegmentOverride;
1204 if (!Fast486WriteMemory(State,
1206 ModRegRm->MemoryAddress,
1210 /* Exception occurred */
1221 Fast486WriteModrmWordOperands(PFAST486_STATE State,
1222 PFAST486_MOD_REG_RM ModRegRm,
1223 BOOLEAN WriteRegister,
1226 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1230 /* Store the value in the register */
1231 State->GeneralRegs[ModRegRm->Register].LowWord = Value;
1235 if (!ModRegRm->Memory)
1237 /* Store the value in the second register */
1238 State->GeneralRegs[ModRegRm->SecondRegister].LowWord = Value;
1242 /* Check for the segment override */
1243 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1245 /* Use the override segment instead */
1246 Segment = State->SegmentOverride;
1250 if (!Fast486WriteMemory(State,
1252 ModRegRm->MemoryAddress,
1256 /* Exception occurred */
1267 Fast486WriteModrmDwordOperands(PFAST486_STATE State,
1268 PFAST486_MOD_REG_RM ModRegRm,
1269 BOOLEAN WriteRegister,
1272 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1276 /* Store the value in the register */
1277 State->GeneralRegs[ModRegRm->Register].Long = Value;
1281 if (!ModRegRm->Memory)
1283 /* Store the value in the second register */
1284 State->GeneralRegs[ModRegRm->SecondRegister].Long = Value;
1288 /* Check for the segment override */
1289 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1291 /* Use the override segment instead */
1292 Segment = State->SegmentOverride;
1296 if (!Fast486WriteMemory(State,
1298 ModRegRm->MemoryAddress,
1302 /* Exception occurred */
1311 #ifndef FAST486_NO_FPU
1315 Fast486FpuNormalize(PFAST486_STATE State, PFAST486_FPU_DATA_REG Data)
1319 if (FPU_IS_NORMALIZED(Data)) return;
1320 if (FPU_IS_ZERO(Data))
1326 LeadingZeros = CountLeadingZeros64(Data->Mantissa);
1328 if (LeadingZeros < Data->Exponent)
1330 Data->Mantissa <<= LeadingZeros;
1331 Data->Exponent -= LeadingZeros;
1335 /* Make it denormalized */
1336 Data->Mantissa <<= Data->Exponent - 1;
1340 State->FpuStatus.Ue = TRUE;
1346 Fast486GetValueTag(PFAST486_FPU_DATA_REG Data)
1348 if (FPU_IS_ZERO(Data)) return FPU_TAG_ZERO;
1349 else if (FPU_IS_NAN(Data)) return FPU_TAG_SPECIAL;
1350 else return FPU_TAG_VALID;
1355 Fast486FpuPush(PFAST486_STATE State,
1356 PFAST486_FPU_DATA_REG Data)
1358 State->FpuStatus.Top--;
1360 if (FPU_GET_TAG(0) == FPU_TAG_EMPTY)
1363 FPU_SET_TAG(0, Fast486GetValueTag(Data));
1365 else State->FpuStatus.Ie = TRUE;
1370 Fast486FpuPop(PFAST486_STATE State)
1372 if (FPU_GET_TAG(0) != FPU_TAG_EMPTY)
1374 FPU_SET_TAG(0, FPU_TAG_EMPTY);
1375 State->FpuStatus.Top++;
1377 else State->FpuStatus.Ie = TRUE;