2 * Fast486 386/486 CPU Emulation Library
5 * Copyright (C) 2013 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 /* PUBLIC FUNCTIONS ***********************************************************/
28 Fast486GetCurrentPrivLevel(PFAST486_STATE State)
30 /* Return the CPL, or 3 if we're in virtual 8086 mode */
31 return (!State->Flags.Vm) ? State->Cpl : 3;
36 Fast486GetPageTableEntry(PFAST486_STATE State,
40 ULONG PdeIndex = GET_ADDR_PDE(VirtualAddress);
41 ULONG PteIndex = GET_ADDR_PTE(VirtualAddress);
42 FAST486_PAGE_DIR DirectoryEntry;
43 FAST486_PAGE_TABLE TableEntry;
44 ULONG PageDirectory = State->ControlRegisters[FAST486_REG_CR3];
46 if ((State->Tlb != NULL)
47 && (State->Tlb[VirtualAddress >> 12] != INVALID_TLB_FIELD))
49 /* Return the cached entry */
50 return State->Tlb[VirtualAddress >> 12];
53 /* Read the directory entry */
54 State->MemReadCallback(State,
55 PageDirectory + PdeIndex * sizeof(ULONG),
56 &DirectoryEntry.Value,
57 sizeof(DirectoryEntry));
59 /* Make sure it is present */
60 if (!DirectoryEntry.Present) return 0;
62 /* Was the directory entry accessed before? */
63 if (!DirectoryEntry.Accessed)
66 DirectoryEntry.Accessed = TRUE;
68 /* Write back the directory entry */
69 State->MemWriteCallback(State,
70 PageDirectory + PdeIndex * sizeof(ULONG),
71 &DirectoryEntry.Value,
72 sizeof(DirectoryEntry));
75 /* Read the table entry */
76 State->MemReadCallback(State,
77 (DirectoryEntry.TableAddress << 12)
78 + PteIndex * sizeof(ULONG),
82 /* Make sure it is present */
83 if (!TableEntry.Present) return 0;
85 if (MarkAsDirty) TableEntry.Dirty = TRUE;
87 /* Was the table entry accessed before? */
88 if (!TableEntry.Accessed)
91 TableEntry.Accessed = TRUE;
93 /* Write back the table entry */
94 State->MemWriteCallback(State,
95 (DirectoryEntry.TableAddress << 12)
96 + PteIndex * sizeof(ULONG),
102 * The resulting permissions depend on the permissions
103 * in the page directory table too
105 TableEntry.Writeable &= DirectoryEntry.Writeable;
106 TableEntry.Usermode &= DirectoryEntry.Usermode;
108 if (State->Tlb != NULL)
110 /* Set the TLB entry */
111 State->Tlb[VirtualAddress >> 12] = TableEntry.Value;
114 /* Return the table entry */
115 return TableEntry.Value;
120 Fast486ReadLinearMemory(PFAST486_STATE State,
125 /* Check if paging is enabled */
126 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
129 FAST486_PAGE_TABLE TableEntry;
130 INT Cpl = Fast486GetCurrentPrivLevel(State);
132 for (Page = PAGE_ALIGN(LinearAddress);
133 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
136 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
138 /* Get the table entry */
139 TableEntry.Value = Fast486GetPageTableEntry(State, Page, FALSE);
141 if (!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
144 Fast486ExceptionWithErrorCode(State,
145 FAST486_EXCEPTION_PF,
146 TableEntry.Value & 0x07);
150 /* Check if this is the first page */
151 if (Page == PAGE_ALIGN(LinearAddress))
153 /* Start reading from the offset from the beginning of the page */
154 PageOffset = PAGE_OFFSET(LinearAddress);
157 /* Check if this is the last page */
158 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
160 /* Read only a part of the page */
161 PageLength = PAGE_OFFSET(LinearAddress) + Size - PageOffset;
164 /* Read the memory */
165 State->MemReadCallback(State,
166 (TableEntry.Address << 12) | PageOffset,
173 /* Read the memory */
174 State->MemReadCallback(State, LinearAddress, Buffer, Size);
182 Fast486WriteLinearMemory(PFAST486_STATE State,
187 /* Check if paging is enabled */
188 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PG)
191 FAST486_PAGE_TABLE TableEntry;
192 INT Cpl = Fast486GetCurrentPrivLevel(State);
194 for (Page = PAGE_ALIGN(LinearAddress);
195 Page <= PAGE_ALIGN(LinearAddress + Size - 1);
198 ULONG PageOffset = 0, PageLength = PAGE_SIZE;
200 /* Get the table entry */
201 TableEntry.Value = Fast486GetPageTableEntry(State, Page, TRUE);
203 if ((!TableEntry.Present || (!TableEntry.Usermode && (Cpl > 0)))
204 || ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_WP)
205 && !TableEntry.Writeable))
208 Fast486ExceptionWithErrorCode(State,
209 FAST486_EXCEPTION_PF,
210 TableEntry.Value & 0x07);
214 /* Check if this is the first page */
215 if (Page == PAGE_ALIGN(LinearAddress))
217 /* Start writing from the offset from the beginning of the page */
218 PageOffset = PAGE_OFFSET(LinearAddress);
221 /* Check if this is the last page */
222 if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
224 /* Write only a part of the page */
225 PageLength = PAGE_OFFSET(LinearAddress) + Size - PageOffset;
228 /* Write the memory */
229 State->MemWriteCallback(State,
230 (TableEntry.Address << 12) | PageOffset,
237 /* Write the memory */
238 State->MemWriteCallback(State, LinearAddress, Buffer, Size);
246 Fast486Exception(PFAST486_STATE State,
247 FAST486_EXCEPTIONS ExceptionCode)
249 /* Call the internal function */
250 Fast486ExceptionWithErrorCode(State, ExceptionCode, 0);
255 Fast486StackPush(PFAST486_STATE State,
258 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
260 /* The OPSIZE prefix toggles the size */
261 if (State->PrefixFlags & FAST486_PREFIX_OPSIZE) Size = !Size;
267 /* Check if ESP is between 1 and 3 */
268 if (State->GeneralRegs[FAST486_REG_ESP].Long >= 1
269 && State->GeneralRegs[FAST486_REG_ESP].Long <= 3)
271 Fast486Exception(State, FAST486_EXCEPTION_SS);
275 /* Subtract ESP by 4 */
276 State->GeneralRegs[FAST486_REG_ESP].Long -= 4;
278 /* Store the value in SS:ESP */
279 return Fast486WriteMemory(State,
281 State->GeneralRegs[FAST486_REG_ESP].Long,
288 USHORT ShortValue = LOWORD(Value);
290 /* Check if SP is 1 */
291 if (State->GeneralRegs[FAST486_REG_ESP].Long == 1)
293 Fast486Exception(State, FAST486_EXCEPTION_SS);
297 /* Subtract SP by 2 */
298 State->GeneralRegs[FAST486_REG_ESP].LowWord -= 2;
300 /* Store the value in SS:SP */
301 return Fast486WriteMemory(State,
303 State->GeneralRegs[FAST486_REG_ESP].LowWord,
311 Fast486StackPop(PFAST486_STATE State,
316 BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
318 /* The OPSIZE prefix toggles the size */
325 /* Check if ESP is 0xFFFFFFFF */
326 if (State->GeneralRegs[FAST486_REG_ESP].Long == 0xFFFFFFFF)
328 Fast486Exception(State, FAST486_EXCEPTION_SS);
332 /* Read the value from SS:ESP */
333 if (!Fast486ReadMemory(State,
335 State->GeneralRegs[FAST486_REG_ESP].Long,
340 /* An exception occurred */
344 /* Increment ESP by 4 */
345 State->GeneralRegs[FAST486_REG_ESP].Long += 4;
347 /* Store the value in the result */
354 /* Check if SP is 0xFFFF */
355 if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 0xFFFF)
357 Fast486Exception(State, FAST486_EXCEPTION_SS);
361 /* Read the value from SS:SP */
362 if (!Fast486ReadMemory(State,
364 State->GeneralRegs[FAST486_REG_ESP].LowWord,
369 /* An exception occurred */
373 /* Increment SP by 2 */
374 State->GeneralRegs[FAST486_REG_ESP].LowWord += 2;
376 /* Store the value in the result */
385 Fast486LoadSegment(PFAST486_STATE State,
386 FAST486_SEG_REGS Segment,
389 PFAST486_SEG_REG CachedDescriptor;
390 FAST486_GDT_ENTRY GdtEntry;
392 ASSERT(Segment < FAST486_NUM_SEG_REGS);
394 /* Get the cached descriptor */
395 CachedDescriptor = &State->SegmentRegs[Segment];
397 /* Check for protected mode */
398 if ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE) && !State->Flags.Vm)
400 if (!(Selector & SEGMENT_TABLE_INDICATOR))
402 /* Make sure the GDT contains the entry */
403 if (GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
405 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
410 if (!Fast486ReadLinearMemory(State,
412 + GET_SEGMENT_INDEX(Selector),
416 /* Exception occurred */
422 /* Make sure the LDT contains the entry */
423 if (GET_SEGMENT_INDEX(Selector) >= (State->Ldtr.Limit + 1))
425 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
430 if (!Fast486ReadLinearMemory(State,
432 + GET_SEGMENT_INDEX(Selector),
436 /* Exception occurred */
441 if (Segment == FAST486_REG_SS)
443 /* Loading the stack segment */
445 if (GET_SEGMENT_INDEX(Selector) == 0)
447 Fast486Exception(State, FAST486_EXCEPTION_GP);
451 if (!GdtEntry.SystemType)
453 /* This is a special descriptor */
454 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
458 if (GdtEntry.Executable || !GdtEntry.ReadWrite)
460 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
464 if ((GET_SEGMENT_RPL(Selector) != Fast486GetCurrentPrivLevel(State))
465 || (GET_SEGMENT_RPL(Selector) != GdtEntry.Dpl))
467 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
471 if (!GdtEntry.Present)
473 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_SS, Selector);
477 else if (Segment == FAST486_REG_CS)
479 /* Loading the code segment */
481 if (GET_SEGMENT_INDEX(Selector) == 0)
483 Fast486Exception(State, FAST486_EXCEPTION_GP);
487 if (!GdtEntry.SystemType)
489 // TODO: Call/interrupt/task gates NOT IMPLEMENTED!
494 if (!GdtEntry.Present)
496 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
500 if (!GdtEntry.Executable)
502 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
506 if (GdtEntry.DirConf)
508 /* Conforming Code Segment */
510 if (GdtEntry.Dpl > Fast486GetCurrentPrivLevel(State))
512 /* Must be accessed from lower-privileged code */
513 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
519 /* Regular code segment */
521 if ((GET_SEGMENT_RPL(Selector) > Fast486GetCurrentPrivLevel(State))
522 || (Fast486GetCurrentPrivLevel(State) != GdtEntry.Dpl))
524 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
530 State->Cpl = GET_SEGMENT_RPL(Selector);
535 /* Loading a data segment */
537 if (!GdtEntry.SystemType)
539 /* This is a special descriptor */
540 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
544 if ((GET_SEGMENT_RPL(Selector) > GdtEntry.Dpl)
545 || (Fast486GetCurrentPrivLevel(State) > GdtEntry.Dpl))
547 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
551 if (!GdtEntry.Present)
553 Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
558 /* Update the cache entry */
559 CachedDescriptor->Selector = Selector;
560 CachedDescriptor->Base = GdtEntry.Base | (GdtEntry.BaseMid << 16) | (GdtEntry.BaseHigh << 24);
561 CachedDescriptor->Limit = GdtEntry.Limit | (GdtEntry.LimitHigh << 16);
562 CachedDescriptor->Accessed = GdtEntry.Accessed;
563 CachedDescriptor->ReadWrite = GdtEntry.ReadWrite;
564 CachedDescriptor->DirConf = GdtEntry.DirConf;
565 CachedDescriptor->Executable = GdtEntry.Executable;
566 CachedDescriptor->SystemType = GdtEntry.SystemType;
567 CachedDescriptor->Dpl = GdtEntry.Dpl;
568 CachedDescriptor->Present = GdtEntry.Present;
569 CachedDescriptor->Size = GdtEntry.Size;
571 /* Check for page granularity */
572 if (GdtEntry.Granularity) CachedDescriptor->Limit <<= 12;
576 /* Update the selector and base */
577 CachedDescriptor->Selector = Selector;
578 CachedDescriptor->Base = Selector << 4;
586 Fast486FetchByte(PFAST486_STATE State,
589 PFAST486_SEG_REG CachedDescriptor;
591 /* Get the cached descriptor of CS */
592 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
594 /* Read from memory */
595 if (!Fast486ReadMemory(State,
597 (CachedDescriptor->Size) ? State->InstPtr.Long
598 : State->InstPtr.LowWord,
603 /* Exception occurred during instruction fetch */
607 /* Advance the instruction pointer */
608 if (CachedDescriptor->Size) State->InstPtr.Long++;
609 else State->InstPtr.LowWord++;
616 Fast486FetchWord(PFAST486_STATE State,
619 PFAST486_SEG_REG CachedDescriptor;
621 /* Get the cached descriptor of CS */
622 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
624 /* Read from memory */
625 // FIXME: Fix byte order on big-endian machines
626 if (!Fast486ReadMemory(State,
628 (CachedDescriptor->Size) ? State->InstPtr.Long
629 : State->InstPtr.LowWord,
634 /* Exception occurred during instruction fetch */
638 /* Advance the instruction pointer */
639 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(USHORT);
640 else State->InstPtr.LowWord += sizeof(USHORT);
647 Fast486FetchDword(PFAST486_STATE State,
650 PFAST486_SEG_REG CachedDescriptor;
652 /* Get the cached descriptor of CS */
653 CachedDescriptor = &State->SegmentRegs[FAST486_REG_CS];
655 /* Read from memory */
656 // FIXME: Fix byte order on big-endian machines
657 if (!Fast486ReadMemory(State,
659 (CachedDescriptor->Size) ? State->InstPtr.Long
660 : State->InstPtr.LowWord,
665 /* Exception occurred during instruction fetch */
669 /* Advance the instruction pointer */
670 if (CachedDescriptor->Size) State->InstPtr.Long += sizeof(ULONG);
671 else State->InstPtr.LowWord += sizeof(ULONG);
678 Fast486GetIntVector(PFAST486_STATE State,
680 PFAST486_IDT_ENTRY IdtEntry)
684 /* Check for protected mode */
685 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
687 /* Read from the IDT */
688 if (!Fast486ReadLinearMemory(State,
690 + Number * sizeof(*IdtEntry),
694 /* Exception occurred */
700 /* Read from the real-mode IVT */
702 /* Paging is always disabled in real mode */
703 State->MemReadCallback(State,
705 + Number * sizeof(FarPointer),
709 /* Fill a fake IDT entry */
710 IdtEntry->Offset = LOWORD(FarPointer);
711 IdtEntry->Selector = HIWORD(FarPointer);
713 IdtEntry->Type = FAST486_IDT_INT_GATE;
714 IdtEntry->Storage = FALSE;
716 IdtEntry->Present = TRUE;
717 IdtEntry->OffsetHigh = 0;
725 Fast486CalculateParity(UCHAR Number)
727 // See http://graphics.stanford.edu/~seander/bithacks.html#ParityLookupTable too...
728 return (0x9669 >> ((Number & 0x0F) ^ (Number >> 4))) & 1;
733 Fast486ParseModRegRm(PFAST486_STATE State,
735 PFAST486_MOD_REG_RM ModRegRm)
737 UCHAR ModRmByte, Mode, RegMem;
739 /* Fetch the MOD REG R/M byte */
740 if (!Fast486FetchByte(State, &ModRmByte))
742 /* Exception occurred */
746 /* Unpack the mode and R/M */
747 Mode = ModRmByte >> 6;
748 RegMem = ModRmByte & 0x07;
750 /* Set the register operand */
751 ModRegRm->Register = (ModRmByte >> 3) & 0x07;
754 if ((ModRmByte >> 6) == 3)
756 /* The second operand is also a register */
757 ModRegRm->Memory = FALSE;
758 ModRegRm->SecondRegister = RegMem;
764 /* The second operand is memory */
765 ModRegRm->Memory = TRUE;
769 if (RegMem == FAST486_REG_ESP)
772 ULONG Scale, Index, Base;
774 /* Fetch the SIB byte */
775 if (!Fast486FetchByte(State, &SibByte))
777 /* Exception occurred */
781 /* Unpack the scale, index and base */
782 Scale = 1 << (SibByte >> 6);
783 Index = (SibByte >> 3) & 0x07;
784 if (Index != FAST486_REG_ESP) Index = State->GeneralRegs[Index].Long;
787 if (((SibByte & 0x07) != FAST486_REG_EBP) || (Mode != 0))
789 /* Use the register a base */
790 Base = State->GeneralRegs[SibByte & 0x07].Long;
795 if (!Fast486FetchDword(State, &Base))
797 /* Exception occurred */
802 if ((SibByte & 0x07) == FAST486_REG_ESP)
804 /* Check if there is no segment override */
805 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
807 /* Add a SS: prefix */
808 State->PrefixFlags |= FAST486_PREFIX_SEG;
809 State->SegmentOverride = FAST486_REG_SS;
813 /* Calculate the address */
814 ModRegRm->MemoryAddress = Base + Index * Scale;
816 else if (RegMem == FAST486_REG_EBP)
818 if (Mode) ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].Long;
819 else ModRegRm->MemoryAddress = 0;
823 /* Get the base from the register */
824 ModRegRm->MemoryAddress = State->GeneralRegs[RegMem].Long;
827 /* Check if there is no segment override */
828 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
830 /* Check if the default segment should be SS */
831 if ((RegMem == FAST486_REG_EBP) && Mode)
833 /* Add a SS: prefix */
834 State->PrefixFlags |= FAST486_PREFIX_SEG;
835 State->SegmentOverride = FAST486_REG_SS;
844 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
846 /* Exception occurred */
850 /* Add the signed offset to the address */
851 ModRegRm->MemoryAddress += (LONG)Offset;
853 else if ((Mode == 2) || ((Mode == 0) && (RegMem == FAST486_REG_EBP)))
857 /* Fetch the dword */
858 if (!Fast486FetchDword(State, (PULONG)&Offset))
860 /* Exception occurred */
864 /* Add the signed offset to the address */
865 ModRegRm->MemoryAddress += Offset;
870 /* Check the operand */
876 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
877 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
885 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord
886 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
894 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
895 + State->GeneralRegs[FAST486_REG_ESI].LowWord;
903 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord
904 + State->GeneralRegs[FAST486_REG_EDI].LowWord;
912 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_ESI].LowWord;
920 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EDI].LowWord;
930 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBP].LowWord;
934 /* [constant] (added later) */
935 ModRegRm->MemoryAddress = 0;
944 ModRegRm->MemoryAddress = State->GeneralRegs[FAST486_REG_EBX].LowWord;
950 /* Check if there is no segment override */
951 if (!(State->PrefixFlags & FAST486_PREFIX_SEG))
953 /* Check if the default segment should be SS */
954 if ((RegMem == 2) || (RegMem == 3) || ((RegMem == 6) && Mode))
956 /* Add a SS: prefix */
957 State->PrefixFlags |= FAST486_PREFIX_SEG;
958 State->SegmentOverride = FAST486_REG_SS;
967 if (!Fast486FetchByte(State, (PUCHAR)&Offset))
969 /* Exception occurred */
973 /* Add the signed offset to the address */
974 ModRegRm->MemoryAddress += (LONG)Offset;
976 else if ((Mode == 2) || ((Mode == 0) && (RegMem == 6)))
981 if (!Fast486FetchWord(State, (PUSHORT)&Offset))
983 /* Exception occurred */
987 /* Add the signed offset to the address */
988 ModRegRm->MemoryAddress += (LONG)Offset;
991 /* Clear the top 16 bits */
992 ModRegRm->MemoryAddress &= 0x0000FFFF;
1000 Fast486ReadModrmByteOperands(PFAST486_STATE State,
1001 PFAST486_MOD_REG_RM ModRegRm,
1005 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1009 /* Get the register value */
1010 if (ModRegRm->Register & 0x04)
1012 /* AH, CH, DH, BH */
1013 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].HighByte;
1017 /* AL, CL, DL, BL */
1018 *RegValue = State->GeneralRegs[ModRegRm->Register & 0x03].LowByte;
1024 if (!ModRegRm->Memory)
1026 /* Get the second register value */
1027 if (ModRegRm->SecondRegister & 0x04)
1029 /* AH, CH, DH, BH */
1030 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte;
1034 /* AL, CL, DL, BL */
1035 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte;
1040 /* Check for the segment override */
1041 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1043 /* Use the override segment instead */
1044 Segment = State->SegmentOverride;
1048 if (!Fast486ReadMemory(State,
1050 ModRegRm->MemoryAddress,
1055 /* Exception occurred */
1066 Fast486ReadModrmWordOperands(PFAST486_STATE State,
1067 PFAST486_MOD_REG_RM ModRegRm,
1071 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1075 /* Get the register value */
1076 *RegValue = State->GeneralRegs[ModRegRm->Register].LowWord;
1081 if (!ModRegRm->Memory)
1083 /* Get the second register value */
1084 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].LowWord;
1088 /* Check for the segment override */
1089 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1091 /* Use the override segment instead */
1092 Segment = State->SegmentOverride;
1096 if (!Fast486ReadMemory(State,
1098 ModRegRm->MemoryAddress,
1103 /* Exception occurred */
1114 Fast486ReadModrmDwordOperands(PFAST486_STATE State,
1115 PFAST486_MOD_REG_RM ModRegRm,
1119 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1123 /* Get the register value */
1124 *RegValue = State->GeneralRegs[ModRegRm->Register].Long;
1129 if (!ModRegRm->Memory)
1131 /* Get the second register value */
1132 *RmValue = State->GeneralRegs[ModRegRm->SecondRegister].Long;
1136 /* Check for the segment override */
1137 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1139 /* Use the override segment instead */
1140 Segment = State->SegmentOverride;
1144 if (!Fast486ReadMemory(State,
1146 ModRegRm->MemoryAddress,
1151 /* Exception occurred */
1162 Fast486WriteModrmByteOperands(PFAST486_STATE State,
1163 PFAST486_MOD_REG_RM ModRegRm,
1164 BOOLEAN WriteRegister,
1167 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1171 /* Store the value in the register */
1172 if (ModRegRm->Register & 0x04)
1174 /* AH, CH, DH, BH */
1175 State->GeneralRegs[ModRegRm->Register & 0x03].HighByte = Value;
1179 /* AL, CL, DL, BL */
1180 State->GeneralRegs[ModRegRm->Register & 0x03].LowByte = Value;
1185 if (!ModRegRm->Memory)
1187 /* Store the value in the second register */
1188 if (ModRegRm->SecondRegister & 0x04)
1190 /* AH, CH, DH, BH */
1191 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].HighByte = Value;
1195 /* AL, CL, DL, BL */
1196 State->GeneralRegs[ModRegRm->SecondRegister & 0x03].LowByte = Value;
1201 /* Check for the segment override */
1202 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1204 /* Use the override segment instead */
1205 Segment = State->SegmentOverride;
1209 if (!Fast486WriteMemory(State,
1211 ModRegRm->MemoryAddress,
1215 /* Exception occurred */
1226 Fast486WriteModrmWordOperands(PFAST486_STATE State,
1227 PFAST486_MOD_REG_RM ModRegRm,
1228 BOOLEAN WriteRegister,
1231 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1235 /* Store the value in the register */
1236 State->GeneralRegs[ModRegRm->Register].LowWord = Value;
1240 if (!ModRegRm->Memory)
1242 /* Store the value in the second register */
1243 State->GeneralRegs[ModRegRm->SecondRegister].LowWord = Value;
1247 /* Check for the segment override */
1248 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1250 /* Use the override segment instead */
1251 Segment = State->SegmentOverride;
1255 if (!Fast486WriteMemory(State,
1257 ModRegRm->MemoryAddress,
1261 /* Exception occurred */
1272 Fast486WriteModrmDwordOperands(PFAST486_STATE State,
1273 PFAST486_MOD_REG_RM ModRegRm,
1274 BOOLEAN WriteRegister,
1277 FAST486_SEG_REGS Segment = FAST486_REG_DS;
1281 /* Store the value in the register */
1282 State->GeneralRegs[ModRegRm->Register].Long = Value;
1286 if (!ModRegRm->Memory)
1288 /* Store the value in the second register */
1289 State->GeneralRegs[ModRegRm->SecondRegister].Long = Value;
1293 /* Check for the segment override */
1294 if (State->PrefixFlags & FAST486_PREFIX_SEG)
1296 /* Use the override segment instead */
1297 Segment = State->SegmentOverride;
1301 if (!Fast486WriteMemory(State,
1303 ModRegRm->MemoryAddress,
1307 /* Exception occurred */