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) \
33 ? __lzcnt((x) >> 32) : (__lzcnt(x) + 32)
37 ULONG CountLeadingZeros64(ULONGLONG Value)
39 ULONG LeadingZeros = 0;
41 while (!(Value & (1 << (63 - LeadingZeros)))) LeadingZeros++;
48 Fast486GetCurrentPrivLevel(PFAST486_STATE State)
50 /* Return the CPL, or 3 if we're in virtual 8086 mode */
51 return (!State->Flags.Vm) ? State->Cpl : 3;
56 Fast486GetPageTableEntry(PFAST486_STATE State,
60 ULONG PdeIndex = GET_ADDR_PDE(VirtualAddress);
61 ULONG PteIndex = GET_ADDR_PTE(VirtualAddress);
62 FAST486_PAGE_DIR DirectoryEntry;
63 FAST486_PAGE_TABLE TableEntry;
64 ULONG PageDirectory = State->ControlRegisters[FAST486_REG_CR3];
66 if ((State->Tlb != NULL)
67 && (State->Tlb[VirtualAddress >> 12] != INVALID_TLB_FIELD))
69 /* Return the cached entry */
70 return State->Tlb[VirtualAddress >> 12];
73 /* Read the directory entry */
74 State->MemReadCallback(State,
75 PageDirectory + PdeIndex * sizeof(ULONG),
76 &DirectoryEntry.Value,
77 sizeof(DirectoryEntry));
79 /* Make sure it is present */
80 if (!DirectoryEntry.Present) return 0;
82 /* Was the directory entry accessed before? */
83 if (!DirectoryEntry.Accessed)
86 DirectoryEntry.Accessed = TRUE;
88 /* Write back the directory entry */
89 State->MemWriteCallback(State,
90 PageDirectory + PdeIndex * sizeof(ULONG),
91 &DirectoryEntry.Value,
92 sizeof(DirectoryEntry));
95 /* Read the table entry */
96 State->MemReadCallback(State,
97 (DirectoryEntry.TableAddress << 12)
98 + PteIndex * sizeof(ULONG),
102 /* Make sure it is present */
103 if (!TableEntry.Present) return 0;
105 if (MarkAsDirty) TableEntry.Dirty = TRUE;
107 /* Was the table entry accessed before? */
108 if (!TableEntry.Accessed)
110 /* Well, it is now */
111 TableEntry.Accessed = TRUE;
113 /* Write back the table entry */
114 State->MemWriteCallback(State,
115 (DirectoryEntry.TableAddress << 12)
116 + PteIndex * sizeof(ULONG),
122 * The resulting permissions depend on the permissions
123 * in the page directory table too
125 TableEntry.Writeable &= DirectoryEntry.Writeable;
126 TableEntry.Usermode &= DirectoryEntry.Usermode;
128 if (State->Tlb != NULL)
130 /* Set the TLB entry */
131 State->Tlb[VirtualAddress >> 12] = TableEntry.Value;
134 /* Return the table entry */
135 return TableEntry.Value;
140 Fast486ReadLinearMemory(PFAST486_STATE State,
145 /* Check if paging is enabled */
146 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
149 FAST486_PAGE_TABLE TableEntry;
150 INT Cpl = Fast486GetCurrentPrivLevel(State);
151 ULONG BufferOffset = 0;
153 for (Page = PAGE_ALIGN(LinearAddress);
154 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
157 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
159 /* Get the table entry */
160 TableEntry.Value = Fast486GetPageTableEntry(State, Page, FALSE);
162 if (!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
165 Fast486ExceptionWithErrorCode(State,
166 FAST486_EXCEPTION_PF,
167 TableEntry.Value & 0x07);
171 /* Check if this is the first page */
172 if (Page == PAGE_ALIGN(LinearAddress))
174 /* Start reading from the offset from the beginning of the page */
175 PageOffset = PAGE_OFFSET(LinearAddress);
176 PageLength -= PageOffset;
179 /* Check if this is the last page */
180 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
182 /* Read only a part of the page */
183 PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
186 /* Read the memory */
187 State->MemReadCallback(State,
188 (TableEntry.Address << 12) | PageOffset,
189 (PVOID)((ULONG_PTR)Buffer + BufferOffset),
192 BufferOffset += PageLength;
197 /* Read the memory */
198 State->MemReadCallback(State, LinearAddress, Buffer, Size);
206 Fast486WriteLinearMemory(PFAST486_STATE State,
211 /* Check if paging is enabled */
212 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
215 FAST486_PAGE_TABLE TableEntry;
216 INT Cpl = Fast486GetCurrentPrivLevel(State);
217 ULONG BufferOffset = 0;
219 for (Page = PAGE_ALIGN(LinearAddress);
220 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
223 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
225 /* Get the table entry */
226 TableEntry.Value = Fast486GetPageTableEntry(State, Page, TRUE);
228 if ((!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
229 || ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_WP)
230 && !TableEntry.Writeable))
233 Fast486ExceptionWithErrorCode(State,
234 FAST486_EXCEPTION_PF,
235 TableEntry.Value & 0x07);
239 /* Check if this is the first page */
240 if (Page == PAGE_ALIGN(LinearAddress))
242 /* Start writing from the offset from the beginning of the page */
243 PageOffset = PAGE_OFFSET(LinearAddress);
244 PageLength -= PageOffset;
247 /* Check if this is the last page */
248 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
250 /* Write only a part of the page */
251 PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
254 /* Write the memory */
255 State->MemWriteCallback(State,
256 (TableEntry.Address << 12) | PageOffset,
257 (PVOID)((ULONG_PTR)Buffer + BufferOffset),
260 BufferOffset += PageLength;
265 /* Write the memory */
266 State->MemWriteCallback(State, LinearAddress, Buffer, Size);
274 Fast486Exception(PFAST486_STATE State,
275 FAST486_EXCEPTIONS ExceptionCode)
277 /* Call the internal function */
278 Fast486ExceptionWithErrorCode(State, ExceptionCode, 0);
283 Fast486StackPush(PFAST486_STATE State,
286 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
288 /* The OPSIZE prefix toggles the size */
289 if (State->PrefixFlags & FAST486_PREFIX_OPSIZE) Size = !Size;
295 /* Check if ESP is between 1 and 3 */
296 if (State->GeneralRegs[FAST486_REG_ESP].Long >= 1
297 && State->GeneralRegs[FAST486_REG_ESP].Long <= 3)
299 Fast486Exception(State, FAST486_EXCEPTION_SS);
303 /* Subtract ESP by 4 */
304 State->GeneralRegs[FAST486_REG_ESP].Long -= sizeof(ULONG);
306 /* Store the value in SS:ESP */
307 return Fast486WriteMemory(State,
309 State->GeneralRegs[FAST486_REG_ESP].Long,
316 USHORT ShortValue = LOWORD(Value);
318 /* Check if SP is 1 */
319 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 1)
321 Fast486Exception(State, FAST486_EXCEPTION_SS);
325 /* Subtract SP by 2 */
326 State->GeneralRegs[FAST486_REG_ESP].LowWord -= sizeof(USHORT);
328 /* Store the value in SS:SP */
329 return Fast486WriteMemory(State,
331 State->GeneralRegs[FAST486_REG_ESP].LowWord,
339 Fast486StackPop(PFAST486_STATE State,
342 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
344 /* The OPSIZE prefix toggles the size */
352 /* Check if ESP is 0xFFFFFFFF */
353 if (State->GeneralRegs[FAST486_REG_ESP].Long == 0xFFFFFFFF)
355 Fast486Exception(State, FAST486_EXCEPTION_SS);
359 /* Read the value from SS:ESP */
360 if (!Fast486ReadMemory(State,
362 State->GeneralRegs[FAST486_REG_ESP].Long,
367 /* An exception occurred */
371 /* Increment ESP by 4 */
372 State->GeneralRegs[FAST486_REG_ESP].Long += sizeof(ULONG);
374 /* Store the value in the result */
382 /* Check if SP is 0xFFFF */
383 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 0xFFFF)
385 Fast486Exception(State, FAST486_EXCEPTION_SS);
389 /* Read the value from SS:SP */
390 if (!Fast486ReadMemory(State,
392 State->GeneralRegs[FAST486_REG_ESP].LowWord,
397 /* An exception occurred */
401 /* Increment SP by 2 */
402 State->GeneralRegs[FAST486_REG_ESP].LowWord += sizeof(USHORT);
404 /* Store the value in the result */
413 Fast486LoadSegment(PFAST486_STATE State,
414 FAST486_SEG_REGS Segment,
417 PFAST486_SEG_REG CachedDescriptor;
418 FAST486_GDT_ENTRY GdtEntry;
420 ASSERT(Segment < FAST486_NUM_SEG_REGS);
422 /* Get the cached descriptor */
423 CachedDescriptor = &State->SegmentRegs[Segment];
425 /* Check for protected mode */
426 if ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE) && !State->Flags.Vm)
428 if (!(Selector & SEGMENT_TABLE_INDICATOR))
430 /* Make sure the GDT contains the entry */
431 if (GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
433 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
438 if (!Fast486ReadLinearMemory(State,
440 + GET_SEGMENT_INDEX(Selector),
444 /* Exception occurred */
450 /* Make sure the LDT contains the entry */
451 if (GET_SEGMENT_INDEX(Selector) >= (State->Ldtr.Limit + 1))
453 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
458 if (!Fast486ReadLinearMemory(State,
460 + GET_SEGMENT_INDEX(Selector),
464 /* Exception occurred */
469 if (Segment == FAST486_REG_SS)
471 /* Loading the stack segment */
473 if (GET_SEGMENT_INDEX(Selector) == 0)
475 Fast486Exception(State, FAST486_EXCEPTION_GP);
479 if (!GdtEntry.SystemType)
481 /* This is a special descriptor */
482 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
486 if (GdtEntry.Executable || !GdtEntry.ReadWrite)
488 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
492 if ((GET_SEGMENT_RPL(Selector) != Fast486GetCurrentPrivLevel(State))
493 || (GET_SEGMENT_RPL(Selector) != GdtEntry.Dpl))
495 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
499 if (!GdtEntry.Present)
501 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_SS, Selector);
505 else if (Segment == FAST486_REG_CS)
507 /* Loading the code segment */
509 if (GET_SEGMENT_INDEX(Selector) == 0)
511 Fast486Exception(State, FAST486_EXCEPTION_GP);
515 if (!GdtEntry.SystemType)
517 // TODO: Call/interrupt/task gates NOT IMPLEMENTED!
522 if (!GdtEntry.Present)
524 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
528 if (!GdtEntry.Executable)
530 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
534 if (GdtEntry.DirConf)
536 /* Conforming Code Segment */
538 if (GdtEntry.Dpl > Fast486GetCurrentPrivLevel(State))
540 /* Must be accessed from lower-privileged code */
541 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
547 /* Regular code segment */
549 if ((GET_SEGMENT_RPL(Selector) > Fast486GetCurrentPrivLevel(State))
550 || (Fast486GetCurrentPrivLevel(State) != GdtEntry.Dpl))
552 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
558 State->Cpl = GET_SEGMENT_RPL(Selector);
563 /* Loading a data segment */
565 if (GET_SEGMENT_INDEX(Selector) != 0)
567 if (!GdtEntry.SystemType)
569 /* This is a special descriptor */
570 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
574 if ((GET_SEGMENT_RPL(Selector) > GdtEntry.Dpl)
575 || (Fast486GetCurrentPrivLevel(State) > GdtEntry.Dpl))
577 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
581 if (!GdtEntry.Present)
583 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
589 /* This is a NULL selector */
590 RtlZeroMemory(&GdtEntry, sizeof(GdtEntry));
594 /* Update the cache entry */
595 CachedDescriptor->Selector = Selector;
596 CachedDescriptor->Base = GdtEntry.Base | (GdtEntry.BaseMid << 16) | (GdtEntry.BaseHigh << 24);
597 CachedDescriptor->Limit = GdtEntry.Limit | (GdtEntry.LimitHigh << 16);
598 CachedDescriptor->Accessed = GdtEntry.Accessed;
599 CachedDescriptor->ReadWrite = GdtEntry.ReadWrite;
600 CachedDescriptor->DirConf = GdtEntry.DirConf;
601 CachedDescriptor->Executable = GdtEntry.Executable;
602 CachedDescriptor->SystemType = GdtEntry.SystemType;
603 CachedDescriptor->Rpl = GET_SEGMENT_RPL(Selector);
604 CachedDescriptor->Dpl = GdtEntry.Dpl;
605 CachedDescriptor->Present = GdtEntry.Present;
606 CachedDescriptor->Size = GdtEntry.Size;
608 /* Check for page granularity */
609 if (GdtEntry.Granularity) CachedDescriptor->Limit <<= 12;
613 /* Update the selector and base */
614 CachedDescriptor->Selector = Selector;
615 CachedDescriptor->Base = Selector << 4;
623 Fast486FetchByte(PFAST486_STATE State,
626 PFAST486_SEG_REG CachedDescriptor;
628 /* Get the cached descriptor of CS */
629 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
631 /* Read from memory */
632 if (!Fast486ReadMemory(State,
634 (CachedDescriptor->Size) ? State->InstPtr.Long
635 : State->InstPtr.LowWord,
640 /* Exception occurred during instruction fetch */
644 /* Advance the instruction pointer */
645 if (CachedDescriptor->Size) State->InstPtr.Long++;
646 else State->InstPtr.LowWord++;
653 Fast486FetchWord(PFAST486_STATE State,
656 PFAST486_SEG_REG CachedDescriptor;
658 /* Get the cached descriptor of CS */
659 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
661 /* Read from memory */
662 // FIXME: Fix byte order on big-endian machines
663 if (!Fast486ReadMemory(State,
665 (CachedDescriptor->Size) ? State->InstPtr.Long
666 : State->InstPtr.LowWord,
671 /* Exception occurred during instruction fetch */
675 /* Advance the instruction pointer */
676 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(USHORT);
677 else State->InstPtr.LowWord += sizeof(USHORT);
684 Fast486FetchDword(PFAST486_STATE State,
687 PFAST486_SEG_REG CachedDescriptor;
689 /* Get the cached descriptor of CS */
690 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
692 /* Read from memory */
693 // FIXME: Fix byte order on big-endian machines
694 if (!Fast486ReadMemory(State,
696 (CachedDescriptor->Size) ? State->InstPtr.Long
697 : State->InstPtr.LowWord,
702 /* Exception occurred during instruction fetch */
706 /* Advance the instruction pointer */
707 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(ULONG);
708 else State->InstPtr.LowWord += sizeof(ULONG);
715 Fast486CalculateParity(UCHAR Number)
717 // See http://graphics.stanford.edu/~seander/bithacks.html#ParityLookupTable too...
718 return (0x9669 >> ((Number & 0x0F) ^ (Number >> 4))) & 1;
723 Fast486ParseModRegRm(PFAST486_STATE State,
725 PFAST486_MOD_REG_RM ModRegRm)
727 UCHAR ModRmByte, Mode, RegMem;
729 /* Fetch the MOD REG R/M byte */
730 if (!Fast486FetchByte(State, &ModRmByte))
732 /* Exception occurred */
736 /* Unpack the mode and R/M */
737 Mode = ModRmByte >> 6;
738 RegMem = ModRmByte & 0x07;
740 /* Set the register operand */
741 ModRegRm->Register = (ModRmByte >> 3) & 0x07;
744 if ((ModRmByte >> 6) == 3)
746 /* The second operand is also a register */
747 ModRegRm->Memory = FALSE;
748 ModRegRm->SecondRegister = RegMem;
754 /* The second operand is memory */
755 ModRegRm->Memory = TRUE;
759 if (RegMem == FAST486_REG_ESP)
762 ULONG Scale, Index, Base;
764 /* Fetch the SIB byte */
765 if (!Fast486FetchByte(State, &SibByte))
767 /* Exception occurred */
771 /* Unpack the scale, index and base */
772 Scale = 1 << (SibByte >> 6);
773 Index = (SibByte >> 3) & 0x07;
774 if (Index != FAST486_REG_ESP) Index = State->GeneralRegs[Index].Long;
777 if (((SibByte & 0x07) != FAST486_REG_EBP) || (Mode != 0))
779 /* Use the register a base */
780 Base = State->GeneralRegs[SibByte & 0x07].Long;
785 if (!Fast486FetchDword(State, &Base))
787 /* Exception occurred */
792 if ((SibByte & 0x07) == FAST486_REG_ESP)
794 /* Check if there is no segment override */
795 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
797 /* Add a SS: prefix */
798 State->PrefixFlags |= FAST486_PREFIX_SEG;
799 State->SegmentOverride = FAST486_REG_SS;
803 /* Calculate the address */
804 ModRegRm->MemoryAddress = Base + Index * Scale;
806 else if (RegMem == FAST486_REG_EBP)
808 if (Mode) ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].Long;
809 else ModRegRm->MemoryAddress = 0;
813 /* Get the base from the register */
814 ModRegRm->MemoryAddress = State->GeneralRegs[RegMem].Long;
817 /* Check if there is no segment override */
818 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
820 /* Check if the default segment should be SS */
821 if ((RegMem == FAST486_REG_EBP) && Mode)
823 /* Add a SS: prefix */
824 State->PrefixFlags |= FAST486_PREFIX_SEG;
825 State->SegmentOverride = FAST486_REG_SS;
834 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
836 /* Exception occurred */
840 /* Add the signed offset to the address */
841 ModRegRm->MemoryAddress += (LONG)Offset;
843 else if ((Mode == 2) || ((Mode == 0) && (RegMem == FAST486_REG_EBP)))
847 /* Fetch the dword */
848 if (!Fast486FetchDword(State, (PULONG)&Offset))
850 /* Exception occurred */
854 /* Add the signed offset to the address */
855 ModRegRm->MemoryAddress += Offset;
860 /* Check the operand */
866 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
867 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
875 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
876 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
884 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
885 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
893 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
894 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
902 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_ESI].LowWord;
910 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EDI].LowWord;
920 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord;
924 /* [constant] (added later) */
925 ModRegRm->MemoryAddress = 0;
934 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord;
940 /* Check if there is no segment override */
941 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
943 /* Check if the default segment should be SS */
944 if ((RegMem == 2) || (RegMem == 3) || ((RegMem == 6) && Mode))
946 /* Add a SS: prefix */
947 State->PrefixFlags |= FAST486_PREFIX_SEG;
948 State->SegmentOverride = FAST486_REG_SS;
957 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
959 /* Exception occurred */
963 /* Add the signed offset to the address */
964 ModRegRm->MemoryAddress += (LONG)Offset;
966 else if ((Mode == 2) || ((Mode == 0) && (RegMem == 6)))
971 if (!Fast486FetchWord(State, (PUSHORT)&Offset))
973 /* Exception occurred */
977 /* Add the signed offset to the address */
978 ModRegRm->MemoryAddress += (LONG)Offset;
981 /* Clear the top 16 bits */
982 ModRegRm->MemoryAddress &= 0x0000FFFF;
990 Fast486ReadModrmByteOperands(PFAST486_STATE State,
991 PFAST486_MOD_REG_RM ModRegRm,
995 FAST486_SEG_REGS Segment = FAST486_REG_DS;
999 /* Get the register value */
1000 if (ModRegRm->Register & 0x04)
1002 /* AH, CH, DH, BH */
1003 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].HighByte;
1007 /* AL, CL, DL, BL */
1008 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].LowByte;
1014 if (!ModRegRm->Memory)
1016 /* Get the second register value */
1017 if (ModRegRm->SecondRegister & 0x04)
1019 /* AH, CH, DH, BH */
1020 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte;
1024 /* AL, CL, DL, BL */
1025 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte;
1030 /* Check for the segment override */
1031 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1033 /* Use the override segment instead */
1034 Segment = State->SegmentOverride;
1038 if (!Fast486ReadMemory(State,
1040 ModRegRm->MemoryAddress,
1045 /* Exception occurred */
1056 Fast486ReadModrmWordOperands(PFAST486_STATE State,
1057 PFAST486_MOD_REG_RM ModRegRm,
1061 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1065 /* Get the register value */
1066 *RegValue = State->GeneralRegs[ModRegRm->Register].LowWord;
1071 if (!ModRegRm->Memory)
1073 /* Get the second register value */
1074 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].LowWord;
1078 /* Check for the segment override */
1079 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1081 /* Use the override segment instead */
1082 Segment = State->SegmentOverride;
1086 if (!Fast486ReadMemory(State,
1088 ModRegRm->MemoryAddress,
1093 /* Exception occurred */
1104 Fast486ReadModrmDwordOperands(PFAST486_STATE State,
1105 PFAST486_MOD_REG_RM ModRegRm,
1109 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1113 /* Get the register value */
1114 *RegValue = State->GeneralRegs[ModRegRm->Register].Long;
1119 if (!ModRegRm->Memory)
1121 /* Get the second register value */
1122 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].Long;
1126 /* Check for the segment override */
1127 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1129 /* Use the override segment instead */
1130 Segment = State->SegmentOverride;
1134 if (!Fast486ReadMemory(State,
1136 ModRegRm->MemoryAddress,
1141 /* Exception occurred */
1152 Fast486WriteModrmByteOperands(PFAST486_STATE State,
1153 PFAST486_MOD_REG_RM ModRegRm,
1154 BOOLEAN WriteRegister,
1157 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1161 /* Store the value in the register */
1162 if (ModRegRm->Register & 0x04)
1164 /* AH, CH, DH, BH */
1165 State->GeneralRegs[ModRegRm->Register & 0x03].HighByte = Value;
1169 /* AL, CL, DL, BL */
1170 State->GeneralRegs[ModRegRm->Register & 0x03].LowByte = Value;
1175 if (!ModRegRm->Memory)
1177 /* Store the value in the second register */
1178 if (ModRegRm->SecondRegister & 0x04)
1180 /* AH, CH, DH, BH */
1181 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte = Value;
1185 /* AL, CL, DL, BL */
1186 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte = Value;
1191 /* Check for the segment override */
1192 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1194 /* Use the override segment instead */
1195 Segment = State->SegmentOverride;
1199 if (!Fast486WriteMemory(State,
1201 ModRegRm->MemoryAddress,
1205 /* Exception occurred */
1216 Fast486WriteModrmWordOperands(PFAST486_STATE State,
1217 PFAST486_MOD_REG_RM ModRegRm,
1218 BOOLEAN WriteRegister,
1221 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1225 /* Store the value in the register */
1226 State->GeneralRegs[ModRegRm->Register].LowWord = Value;
1230 if (!ModRegRm->Memory)
1232 /* Store the value in the second register */
1233 State->GeneralRegs[ModRegRm->SecondRegister].LowWord = Value;
1237 /* Check for the segment override */
1238 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1240 /* Use the override segment instead */
1241 Segment = State->SegmentOverride;
1245 if (!Fast486WriteMemory(State,
1247 ModRegRm->MemoryAddress,
1251 /* Exception occurred */
1262 Fast486WriteModrmDwordOperands(PFAST486_STATE State,
1263 PFAST486_MOD_REG_RM ModRegRm,
1264 BOOLEAN WriteRegister,
1267 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1271 /* Store the value in the register */
1272 State->GeneralRegs[ModRegRm->Register].Long = Value;
1276 if (!ModRegRm->Memory)
1278 /* Store the value in the second register */
1279 State->GeneralRegs[ModRegRm->SecondRegister].Long = Value;
1283 /* Check for the segment override */
1284 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1286 /* Use the override segment instead */
1287 Segment = State->SegmentOverride;
1291 if (!Fast486WriteMemory(State,
1293 ModRegRm->MemoryAddress,
1297 /* Exception occurred */
1306 #ifndef FAST486_NO_FPU
1310 Fast486FpuNormalize(PFAST486_STATE State, PFAST486_FPU_DATA_REG Data)
1314 if (FPU_IS_NORMALIZED(Data)) return;
1315 if (FPU_IS_ZERO(Data))
1321 LeadingZeros = CountLeadingZeros64(Data->Mantissa);
1323 if (LeadingZeros < Data->Exponent)
1325 Data->Mantissa <<= LeadingZeros;
1326 Data->Exponent -= LeadingZeros;
1330 /* Make it denormalized */
1331 Data->Mantissa <<= Data->Exponent - 1;
1335 State->FpuStatus.Ue = TRUE;
1341 Fast486GetValueTag(PFAST486_FPU_DATA_REG Data)
1343 if (FPU_IS_ZERO(Data)) return FPU_TAG_ZERO;
1344 else if (FPU_IS_NAN(Data)) return FPU_TAG_SPECIAL;
1345 else return FPU_TAG_VALID;
1350 Fast486FpuPush(PFAST486_STATE State,
1351 PFAST486_FPU_DATA_REG Data)
1353 State->FpuStatus.Top--;
1355 if (FPU_GET_TAG(0) == FPU_TAG_EMPTY)
1358 FPU_SET_TAG(0, Fast486GetValueTag(Data));
1360 else State->FpuStatus.Ie = TRUE;
1365 Fast486FpuPop(PFAST486_STATE State)
1367 if (FPU_GET_TAG(0) != FPU_TAG_EMPTY)
1369 FPU_SET_TAG(0, FPU_TAG_EMPTY);
1370 State->FpuStatus.Top++;
1372 else State->FpuStatus.Ie = TRUE;