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 INT Cpl = Fast486GetCurrentPrivLevel(State);
127 /* Check if paging is enabled */
128 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
131 FAST486_PAGE_TABLE TableEntry;
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 copying from the offset from the beginning of the page */
155 PageOffset = PAGE_OFFSET(LinearAddress);
158 /* Check if this is the last page */
159 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
161 /* Copy only a part of the page */
162 PageLength = PAGE_OFFSET(LinearAddress + Size);
165 /* Read the memory */
166 State->MemReadCallback(State,
167 (TableEntry.Address << 12) | PageOffset,
174 /* Read the memory */
175 State->MemReadCallback(State, LinearAddress, Buffer, Size);
183 Fast486WriteLinearMemory(PFAST486_STATE State,
188 INT Cpl = Fast486GetCurrentPrivLevel(State);
190 /* Check if paging is enabled */
191 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
194 FAST486_PAGE_TABLE TableEntry;
196 for (Page = PAGE_ALIGN(LinearAddress);
197 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
200 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
202 /* Get the table entry */
203 TableEntry.Value = Fast486GetPageTableEntry(State, Page, TRUE);
205 if ((!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
206 || ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_WP)
207 && !TableEntry.Writeable))
210 Fast486ExceptionWithErrorCode(State,
211 FAST486_EXCEPTION_PF,
212 TableEntry.Value & 0x07);
216 /* Check if this is the first page */
217 if (Page == PAGE_ALIGN(LinearAddress))
219 /* Start copying from the offset from the beginning of the page */
220 PageOffset = PAGE_OFFSET(LinearAddress);
223 /* Check if this is the last page */
224 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
226 /* Copy only a part of the page */
227 PageLength = PAGE_OFFSET(LinearAddress + Size);
230 /* Write the memory */
231 State->MemWriteCallback(State,
232 (TableEntry.Address << 12) | PageOffset,
239 /* Write the memory */
240 State->MemWriteCallback(State, LinearAddress, Buffer, Size);
248 Fast486Exception(PFAST486_STATE State,
249 FAST486_EXCEPTIONS ExceptionCode)
251 /* Call the internal function */
252 Fast486ExceptionWithErrorCode(State, ExceptionCode, 0);
257 Fast486StackPush(PFAST486_STATE State,
260 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
262 /* The OPSIZE prefix toggles the size */
263 if (State->PrefixFlags & FAST486_PREFIX_OPSIZE) Size = !Size;
269 /* Check if ESP is between 1 and 3 */
270 if (State->GeneralRegs[FAST486_REG_ESP].Long >= 1
271 && State->GeneralRegs[FAST486_REG_ESP].Long <= 3)
273 Fast486Exception(State, FAST486_EXCEPTION_SS);
277 /* Subtract ESP by 4 */
278 State->GeneralRegs[FAST486_REG_ESP].Long -= 4;
280 /* Store the value in SS:ESP */
281 return Fast486WriteMemory(State,
283 State->GeneralRegs[FAST486_REG_ESP].Long,
290 USHORT ShortValue = LOWORD(Value);
292 /* Check if SP is 1 */
293 if (State->GeneralRegs[FAST486_REG_ESP].Long == 1)
295 Fast486Exception(State, FAST486_EXCEPTION_SS);
299 /* Subtract SP by 2 */
300 State->GeneralRegs[FAST486_REG_ESP].LowWord -= 2;
302 /* Store the value in SS:SP */
303 return Fast486WriteMemory(State,
305 State->GeneralRegs[FAST486_REG_ESP].LowWord,
313 Fast486StackPop(PFAST486_STATE State,
318 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
320 /* The OPSIZE prefix toggles the size */
327 /* Check if ESP is 0xFFFFFFFF */
328 if (State->GeneralRegs[FAST486_REG_ESP].Long == 0xFFFFFFFF)
330 Fast486Exception(State, FAST486_EXCEPTION_SS);
334 /* Read the value from SS:ESP */
335 if (!Fast486ReadMemory(State,
337 State->GeneralRegs[FAST486_REG_ESP].Long,
342 /* An exception occurred */
346 /* Increment ESP by 4 */
347 State->GeneralRegs[FAST486_REG_ESP].Long += 4;
349 /* Store the value in the result */
356 /* Check if SP is 0xFFFF */
357 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 0xFFFF)
359 Fast486Exception(State, FAST486_EXCEPTION_SS);
363 /* Read the value from SS:SP */
364 if (!Fast486ReadMemory(State,
366 State->GeneralRegs[FAST486_REG_ESP].LowWord,
371 /* An exception occurred */
375 /* Increment SP by 2 */
376 State->GeneralRegs[FAST486_REG_ESP].LowWord += 2;
378 /* Store the value in the result */
387 Fast486LoadSegment(PFAST486_STATE State,
391 PFAST486_SEG_REG CachedDescriptor;
392 FAST486_GDT_ENTRY GdtEntry;
394 ASSERT(Segment < FAST486_NUM_SEG_REGS);
396 /* Get the cached descriptor */
397 CachedDescriptor = &State->SegmentRegs[Segment];
399 /* Check for protected mode */
400 if ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE) && !State->Flags.Vm)
402 /* Make sure the GDT contains the entry */
403 if (GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
405 Fast486Exception(State, FAST486_EXCEPTION_GP);
410 if (!Fast486ReadLinearMemory(State,
412 + GET_SEGMENT_INDEX(Selector),
416 /* Exception occurred */
420 if (Segment == FAST486_REG_SS)
422 /* Loading the stack segment */
424 if (GET_SEGMENT_INDEX(Selector) == 0)
426 Fast486Exception(State, FAST486_EXCEPTION_GP);
430 if (!GdtEntry.SystemType)
432 /* This is a special descriptor */
433 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
437 if (GdtEntry.Executable || !GdtEntry.ReadWrite)
439 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
443 if ((GET_SEGMENT_RPL(Selector) != Fast486GetCurrentPrivLevel(State))
444 || (GET_SEGMENT_RPL(Selector) != GdtEntry.Dpl))
446 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
450 if (!GdtEntry.Present)
452 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_SS, Selector);
456 else if (Segment == FAST486_REG_CS)
458 /* Loading the code segment */
460 if (GET_SEGMENT_INDEX(Selector) == 0)
462 Fast486Exception(State, FAST486_EXCEPTION_GP);
466 if (GdtEntry.SystemType)
468 // TODO: Call/interrupt/task gates NOT IMPLEMENTED!
473 if (!GdtEntry.Present)
475 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
479 if (!GdtEntry.Executable)
481 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
485 if (GdtEntry.DirConf)
487 /* Conforming Code Segment */
489 if (GdtEntry.Dpl > Fast486GetCurrentPrivLevel(State))
491 /* Must be accessed from lower-privileged code */
492 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
498 /* Regular code segment */
500 if ((GET_SEGMENT_RPL(Selector) > Fast486GetCurrentPrivLevel(State))
501 || (Fast486GetCurrentPrivLevel(State) != GdtEntry.Dpl))
503 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
510 State->Cpl = GET_SEGMENT_RPL(Selector);
515 /* Loading a data segment */
517 if (!GdtEntry.SystemType)
519 /* This is a special descriptor */
520 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
524 if ((GET_SEGMENT_RPL(Selector) > GdtEntry.Dpl)
525 || (Fast486GetCurrentPrivLevel(State) > GdtEntry.Dpl))
527 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
531 if (!GdtEntry.Present)
533 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
538 /* Update the cache entry */
539 CachedDescriptor->Selector = Selector;
540 CachedDescriptor->Base = GdtEntry.Base | (GdtEntry.BaseMid << 16) | (GdtEntry.BaseHigh << 24);
541 CachedDescriptor->Limit = GdtEntry.Limit | (GdtEntry.LimitHigh << 16);
542 CachedDescriptor->Accessed = GdtEntry.Accessed;
543 CachedDescriptor->ReadWrite = GdtEntry.ReadWrite;
544 CachedDescriptor->DirConf = GdtEntry.DirConf;
545 CachedDescriptor->Executable = GdtEntry.Executable;
546 CachedDescriptor->SystemType = GdtEntry.SystemType;
547 CachedDescriptor->Dpl = GdtEntry.Dpl;
548 CachedDescriptor->Present = GdtEntry.Present;
549 CachedDescriptor->Size = GdtEntry.Size;
551 /* Check for page granularity */
552 if (GdtEntry.Granularity) CachedDescriptor->Limit <<= 12;
556 /* Update the selector and base */
557 CachedDescriptor->Selector = Selector;
558 CachedDescriptor->Base = Selector << 4;
566 Fast486FetchByte(PFAST486_STATE State,
569 PFAST486_SEG_REG CachedDescriptor;
571 /* Get the cached descriptor of CS */
572 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
574 /* Read from memory */
575 if (!Fast486ReadMemory(State,
577 (CachedDescriptor->Size) ? State->InstPtr.Long
578 : State->InstPtr.LowWord,
583 /* Exception occurred during instruction fetch */
587 /* Advance the instruction pointer */
588 if (CachedDescriptor->Size) State->InstPtr.Long++;
589 else State->InstPtr.LowWord++;
596 Fast486FetchWord(PFAST486_STATE State,
599 PFAST486_SEG_REG CachedDescriptor;
601 /* Get the cached descriptor of CS */
602 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
604 /* Read from memory */
605 // FIXME: Fix byte order on big-endian machines
606 if (!Fast486ReadMemory(State,
608 (CachedDescriptor->Size) ? State->InstPtr.Long
609 : State->InstPtr.LowWord,
614 /* Exception occurred during instruction fetch */
618 /* Advance the instruction pointer */
619 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(USHORT);
620 else State->InstPtr.LowWord += sizeof(USHORT);
627 Fast486FetchDword(PFAST486_STATE State,
630 PFAST486_SEG_REG CachedDescriptor;
632 /* Get the cached descriptor of CS */
633 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
635 /* Read from memory */
636 // FIXME: Fix byte order on big-endian machines
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 += sizeof(ULONG);
651 else State->InstPtr.LowWord += sizeof(ULONG);
658 Fast486GetIntVector(PFAST486_STATE State,
660 PFAST486_IDT_ENTRY IdtEntry)
664 /* Check for protected mode */
665 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
667 /* Read from the IDT */
668 if (!Fast486ReadLinearMemory(State,
670 + Number * sizeof(*IdtEntry),
674 /* Exception occurred */
680 /* Read from the real-mode IVT */
682 /* Paging is always disabled in real mode */
683 State->MemReadCallback(State,
685 + Number * sizeof(FarPointer),
689 /* Fill a fake IDT entry */
690 IdtEntry->Offset = LOWORD(FarPointer);
691 IdtEntry->Selector = HIWORD(FarPointer);
693 IdtEntry->Type = FAST486_IDT_INT_GATE;
694 IdtEntry->Storage = FALSE;
696 IdtEntry->Present = TRUE;
697 IdtEntry->OffsetHigh = 0;
705 Fast486CalculateParity(UCHAR Number)
707 // See http://graphics.stanford.edu/~seander/bithacks.html#ParityLookupTable too...
708 return (0x9669 >> ((Number & 0x0F) ^ (Number >> 4))) & 1;
713 Fast486ParseModRegRm(PFAST486_STATE State,
715 PFAST486_MOD_REG_RM ModRegRm)
717 UCHAR ModRmByte, Mode, RegMem;
719 /* Fetch the MOD REG R/M byte */
720 if (!Fast486FetchByte(State, &ModRmByte))
722 /* Exception occurred */
726 /* Unpack the mode and R/M */
727 Mode = ModRmByte >> 6;
728 RegMem = ModRmByte & 0x07;
730 /* Set the register operand */
731 ModRegRm->Register = (ModRmByte >> 3) & 0x07;
734 if ((ModRmByte >> 6) == 3)
736 /* The second operand is also a register */
737 ModRegRm->Memory = FALSE;
738 ModRegRm->SecondRegister = RegMem;
744 /* The second operand is memory */
745 ModRegRm->Memory = TRUE;
749 if (RegMem == FAST486_REG_ESP)
752 ULONG Scale, Index, Base;
754 /* Fetch the SIB byte */
755 if (!Fast486FetchByte(State, &SibByte))
757 /* Exception occurred */
761 /* Unpack the scale, index and base */
762 Scale = 1 << (SibByte >> 6);
763 Index = (SibByte >> 3) & 0x07;
764 if (Index != FAST486_REG_ESP) Index = State->GeneralRegs[Index].Long;
767 if (((SibByte & 0x07) != FAST486_REG_EBP) || (Mode != 0))
769 /* Use the register a base */
770 Base = State->GeneralRegs[SibByte & 0x07].Long;
775 if (!Fast486FetchDword(State, &Base))
777 /* Exception occurred */
782 if ((SibByte & 0x07) == FAST486_REG_ESP)
784 /* Check if there is no segment override */
785 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
787 /* Add a SS: prefix */
788 State->PrefixFlags |= FAST486_PREFIX_SEG;
789 State->SegmentOverride = FAST486_REG_SS;
793 /* Calculate the address */
794 ModRegRm->MemoryAddress = Base + Index * Scale;
796 else if (RegMem == FAST486_REG_EBP)
798 if (Mode) ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].Long;
799 else ModRegRm->MemoryAddress = 0;
803 /* Get the base from the register */
804 ModRegRm->MemoryAddress = State->GeneralRegs[RegMem].Long;
807 /* Check if there is no segment override */
808 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
810 /* Check if the default segment should be SS */
811 if ((RegMem == FAST486_REG_EBP) && Mode)
813 /* Add a SS: prefix */
814 State->PrefixFlags |= FAST486_PREFIX_SEG;
815 State->SegmentOverride = FAST486_REG_SS;
824 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
826 /* Exception occurred */
830 /* Add the signed offset to the address */
831 ModRegRm->MemoryAddress += (LONG)Offset;
833 else if ((Mode == 2) || ((Mode == 0) && (RegMem == FAST486_REG_EBP)))
837 /* Fetch the dword */
838 if (!Fast486FetchDword(State, (PULONG)&Offset))
840 /* Exception occurred */
844 /* Add the signed offset to the address */
845 ModRegRm->MemoryAddress += Offset;
850 /* Check the operand */
856 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
857 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
865 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
866 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
874 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
875 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
883 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
884 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
892 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_ESI].LowWord;
900 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EDI].LowWord;
910 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord;
914 /* [constant] (added later) */
915 ModRegRm->MemoryAddress = 0;
924 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord;
930 /* Check if there is no segment override */
931 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
933 /* Check if the default segment should be SS */
934 if ((RegMem == 2) || (RegMem == 3) || ((RegMem == 6) && Mode))
936 /* Add a SS: prefix */
937 State->PrefixFlags |= FAST486_PREFIX_SEG;
938 State->SegmentOverride = FAST486_REG_SS;
947 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
949 /* Exception occurred */
953 /* Add the signed offset to the address */
954 ModRegRm->MemoryAddress += (LONG)Offset;
956 else if ((Mode == 2) || ((Mode == 0) && (RegMem == 6)))
961 if (!Fast486FetchWord(State, (PUSHORT)&Offset))
963 /* Exception occurred */
967 /* Add the signed offset to the address */
968 ModRegRm->MemoryAddress += (LONG)Offset;
971 /* Clear the top 16 bits */
972 ModRegRm->MemoryAddress &= 0x0000FFFF;
980 Fast486ReadModrmByteOperands(PFAST486_STATE State,
981 PFAST486_MOD_REG_RM ModRegRm,
985 FAST486_SEG_REGS Segment = FAST486_REG_DS;
989 /* Get the register value */
990 if (ModRegRm->Register & 0x04)
993 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].HighByte;
998 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].LowByte;
1004 if (!ModRegRm->Memory)
1006 /* Get the second register value */
1007 if (ModRegRm->SecondRegister & 0x04)
1009 /* AH, CH, DH, BH */
1010 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte;
1014 /* AL, CL, DL, BL */
1015 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte;
1020 /* Check for the segment override */
1021 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1023 /* Use the override segment instead */
1024 Segment = State->SegmentOverride;
1028 if (!Fast486ReadMemory(State,
1030 ModRegRm->MemoryAddress,
1035 /* Exception occurred */
1046 Fast486ReadModrmWordOperands(PFAST486_STATE State,
1047 PFAST486_MOD_REG_RM ModRegRm,
1051 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1055 /* Get the register value */
1056 *RegValue = State->GeneralRegs[ModRegRm->Register].LowWord;
1061 if (!ModRegRm->Memory)
1063 /* Get the second register value */
1064 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].LowWord;
1068 /* Check for the segment override */
1069 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1071 /* Use the override segment instead */
1072 Segment = State->SegmentOverride;
1076 if (!Fast486ReadMemory(State,
1078 ModRegRm->MemoryAddress,
1083 /* Exception occurred */
1094 Fast486ReadModrmDwordOperands(PFAST486_STATE State,
1095 PFAST486_MOD_REG_RM ModRegRm,
1099 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1103 /* Get the register value */
1104 *RegValue = State->GeneralRegs[ModRegRm->Register].Long;
1109 if (!ModRegRm->Memory)
1111 /* Get the second register value */
1112 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].Long;
1116 /* Check for the segment override */
1117 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1119 /* Use the override segment instead */
1120 Segment = State->SegmentOverride;
1124 if (!Fast486ReadMemory(State,
1126 ModRegRm->MemoryAddress,
1131 /* Exception occurred */
1142 Fast486WriteModrmByteOperands(PFAST486_STATE State,
1143 PFAST486_MOD_REG_RM ModRegRm,
1144 BOOLEAN WriteRegister,
1147 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1151 /* Store the value in the register */
1152 if (ModRegRm->Register & 0x04)
1154 /* AH, CH, DH, BH */
1155 State->GeneralRegs[ModRegRm->Register & 0x03].HighByte = Value;
1159 /* AL, CL, DL, BL */
1160 State->GeneralRegs[ModRegRm->Register & 0x03].LowByte = Value;
1165 if (!ModRegRm->Memory)
1167 /* Store the value in the second register */
1168 if (ModRegRm->SecondRegister & 0x04)
1170 /* AH, CH, DH, BH */
1171 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte = Value;
1175 /* AL, CL, DL, BL */
1176 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte = Value;
1181 /* Check for the segment override */
1182 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1184 /* Use the override segment instead */
1185 Segment = State->SegmentOverride;
1189 if (!Fast486WriteMemory(State,
1191 ModRegRm->MemoryAddress,
1195 /* Exception occurred */
1206 Fast486WriteModrmWordOperands(PFAST486_STATE State,
1207 PFAST486_MOD_REG_RM ModRegRm,
1208 BOOLEAN WriteRegister,
1211 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1215 /* Store the value in the register */
1216 State->GeneralRegs[ModRegRm->Register].LowWord = Value;
1220 if (!ModRegRm->Memory)
1222 /* Store the value in the second register */
1223 State->GeneralRegs[ModRegRm->SecondRegister].LowWord = Value;
1227 /* Check for the segment override */
1228 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1230 /* Use the override segment instead */
1231 Segment = State->SegmentOverride;
1235 if (!Fast486WriteMemory(State,
1237 ModRegRm->MemoryAddress,
1241 /* Exception occurred */
1252 Fast486WriteModrmDwordOperands(PFAST486_STATE State,
1253 PFAST486_MOD_REG_RM ModRegRm,
1254 BOOLEAN WriteRegister,
1257 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1261 /* Store the value in the register */
1262 State->GeneralRegs[ModRegRm->Register].Long = Value;
1266 if (!ModRegRm->Memory)
1268 /* Store the value in the second register */
1269 State->GeneralRegs[ModRegRm->SecondRegister].Long = Value;
1273 /* Check for the segment override */
1274 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1276 /* Use the override segment instead */
1277 Segment = State->SegmentOverride;
1281 if (!Fast486WriteMemory(State,
1283 ModRegRm->MemoryAddress,
1287 /* Exception occurred */