Previous 15994 Revisions Next

r14119 Sunday 22nd January, 2012 at 10:40:22 UTC by Barry Rodewald
i386: Reworked stack segment limit checks to add basic support for expand-down
segments, and added limit checks to PUSH and POP. [Barry Rodewald]
Fix for limit wrap and selector 0 in the LDT. Added more privileged
instruction checks, divide by zero traps. [Carl]
[src/emu/cpu/i386]i386.c i386op16.c i386op32.c i386ops.c

src/emu/cpu/i386/i386op16.c
r14118r14119
11891189static void I386OP(loop16)(i386_state *cpustate)         // Opcode 0xe2
11901190{
11911191   INT8 disp = FETCH(cpustate);
1192   REG16(CX)--;
1193   if( REG16(CX) != 0 ) {
1192   INT32 val = (cpustate->address_size)?(--REG32(ECX)):(--REG16(CX));
1193   if( val != 0 ) {
11941194      if (cpustate->sreg[CS].d)
11951195      {
11961196         cpustate->eip += disp;
r14118r14119
12071207static void I386OP(loopne16)(i386_state *cpustate)         // Opcode 0xe0
12081208{
12091209   INT8 disp = FETCH(cpustate);
1210   REG16(CX)--;
1211   if( REG16(CX) != 0 && cpustate->ZF == 0 ) {
1210   INT32 val = (cpustate->address_size)?(--REG32(ECX)):(--REG16(CX));
1211   if( val != 0 && cpustate->ZF == 0 ) {
12121212      if (cpustate->sreg[CS].d)
12131213      {
12141214         cpustate->eip += disp;
r14118r14119
12251225static void I386OP(loopz16)(i386_state *cpustate)         // Opcode 0xe1
12261226{
12271227   INT8 disp = FETCH(cpustate);
1228   REG16(CX)--;
1229   if( REG16(CX) != 0 && cpustate->ZF != 0 ) {
1228   INT32 val = (cpustate->address_size)?(--REG32(ECX)):(--REG16(CX));
1229   if( val != 0 && cpustate->ZF != 0 ) {
12301230      if (cpustate->sreg[CS].d)
12311231      {
12321232         cpustate->eip += disp;
r14118r14119
14861486
14871487static void I386OP(pop_ax)(i386_state *cpustate)         // Opcode 0x58
14881488{
1489   REG16(AX) = POP16(cpustate);
1489   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1490   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1491      REG16(AX) = POP16(cpustate);
1492   else
1493      FAULT(FAULT_SS,0)
14901494   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
14911495}
14921496
14931497static void I386OP(pop_cx)(i386_state *cpustate)         // Opcode 0x59
14941498{
1495   REG16(CX) = POP16(cpustate);
1499   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1500   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1501      REG16(CX) = POP16(cpustate);
1502   else
1503      FAULT(FAULT_SS,0)
14961504   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
14971505}
14981506
14991507static void I386OP(pop_dx)(i386_state *cpustate)         // Opcode 0x5a
15001508{
1501   REG16(DX) = POP16(cpustate);
1509   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1510   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1511      REG16(DX) = POP16(cpustate);
1512   else
1513      FAULT(FAULT_SS,0)
15021514   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
15031515}
15041516
15051517static void I386OP(pop_bx)(i386_state *cpustate)         // Opcode 0x5b
15061518{
1507   REG16(BX) = POP16(cpustate);
1519   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1520   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1521      REG16(BX) = POP16(cpustate);
1522   else
1523      FAULT(FAULT_SS,0)
15081524   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
15091525}
15101526
15111527static void I386OP(pop_sp)(i386_state *cpustate)         // Opcode 0x5c
15121528{
1513   REG16(SP) = POP16(cpustate);
1529   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1530   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1531      REG16(SP) = POP16(cpustate);
1532   else
1533      FAULT(FAULT_SS,0)
15141534   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
15151535}
15161536
15171537static void I386OP(pop_bp)(i386_state *cpustate)         // Opcode 0x5d
15181538{
1519   REG16(BP) = POP16(cpustate);
1539   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1540   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1541      REG16(BP) = POP16(cpustate);
1542   else
1543      FAULT(FAULT_SS,0)
15201544   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
15211545}
15221546
15231547static void I386OP(pop_si)(i386_state *cpustate)         // Opcode 0x5e
15241548{
1525   REG16(SI) = POP16(cpustate);
1549   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1550   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1551      REG16(SI) = POP16(cpustate);
1552   else
1553      FAULT(FAULT_SS,0)
15261554   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
15271555}
15281556
15291557static void I386OP(pop_di)(i386_state *cpustate)         // Opcode 0x5f
15301558{
1531   REG16(DI) = POP16(cpustate);
1559   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1560   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1561      REG16(DI) = POP16(cpustate);
1562   else
1563      FAULT(FAULT_SS,0)
15321564   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
15331565}
15341566
15351567static void I386OP(pop_ds16)(i386_state *cpustate)         // Opcode 0x1f
15361568{
1537   cpustate->sreg[DS].selector = POP16(cpustate);
1538   if( PROTECTED_MODE ) {
1539      i386_load_segment_descriptor(cpustate,DS);
1540   } else {
1541      i386_load_segment_descriptor(cpustate,DS);
1569   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1570   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1571   {
1572      cpustate->sreg[DS].selector = POP16(cpustate);
1573      if( PROTECTED_MODE ) {
1574         i386_load_segment_descriptor(cpustate,DS);
1575      } else {
1576         i386_load_segment_descriptor(cpustate,DS);
1577      }
15421578   }
1579   else
1580      FAULT(FAULT_SS,0)
15431581   CYCLES(cpustate,CYCLES_POP_SREG);
15441582}
15451583
15461584static void I386OP(pop_es16)(i386_state *cpustate)         // Opcode 0x07
15471585{
1548   cpustate->sreg[ES].selector = POP16(cpustate);
1549   if( PROTECTED_MODE ) {
1550      i386_load_segment_descriptor(cpustate,ES);
1551   } else {
1552      i386_load_segment_descriptor(cpustate,ES);
1586   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1587   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1588   {
1589      cpustate->sreg[ES].selector = POP16(cpustate);
1590      if( PROTECTED_MODE ) {
1591         i386_load_segment_descriptor(cpustate,ES);
1592      } else {
1593         i386_load_segment_descriptor(cpustate,ES);
1594      }
15531595   }
1596   else
1597      FAULT(FAULT_SS,0)
15541598   CYCLES(cpustate,CYCLES_POP_SREG);
15551599}
15561600
15571601static void I386OP(pop_fs16)(i386_state *cpustate)         // Opcode 0x0f a1
15581602{
1559   cpustate->sreg[FS].selector = POP16(cpustate);
1560   if( PROTECTED_MODE ) {
1561      i386_load_segment_descriptor(cpustate,FS);
1562   } else {
1563      i386_load_segment_descriptor(cpustate,FS);
1603   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1604   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1605   {
1606      cpustate->sreg[FS].selector = POP16(cpustate);
1607      if( PROTECTED_MODE ) {
1608         i386_load_segment_descriptor(cpustate,FS);
1609      } else {
1610         i386_load_segment_descriptor(cpustate,FS);
1611      }
15641612   }
1613   else
1614      FAULT(FAULT_SS,0)
15651615   CYCLES(cpustate,CYCLES_POP_SREG);
15661616}
15671617
15681618static void I386OP(pop_gs16)(i386_state *cpustate)         // Opcode 0x0f a9
15691619{
1570   cpustate->sreg[GS].selector = POP16(cpustate);
1571   if( PROTECTED_MODE ) {
1572      i386_load_segment_descriptor(cpustate,GS);
1573   } else {
1574      i386_load_segment_descriptor(cpustate,GS);
1620   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1621   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1622   {
1623      cpustate->sreg[GS].selector = POP16(cpustate);
1624      if( PROTECTED_MODE ) {
1625         i386_load_segment_descriptor(cpustate,GS);
1626      } else {
1627         i386_load_segment_descriptor(cpustate,GS);
1628      }
15751629   }
1630   else
1631      FAULT(FAULT_SS,0)
15761632   CYCLES(cpustate,CYCLES_POP_SREG);
15771633}
15781634
15791635static void I386OP(pop_ss16)(i386_state *cpustate)         // Opcode 0x17
15801636{
1637   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
15811638   if(cpustate->IF != 0) // if external interrupts are enabled
15821639   {
15831640      cpustate->IF = 0;  // reset IF for the next instruction
15841641      cpustate->delayed_interrupt_enable = 1;
15851642   }
15861643
1587   cpustate->sreg[SS].selector = POP16(cpustate);
1588   if( PROTECTED_MODE ) {
1589      i386_load_segment_descriptor(cpustate,SS);
1590   } else {
1591      i386_load_segment_descriptor(cpustate,SS);
1644   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1645   {
1646      cpustate->sreg[SS].selector = POP16(cpustate);
1647      if( PROTECTED_MODE ) {
1648         i386_load_segment_descriptor(cpustate,SS);
1649      } else {
1650         i386_load_segment_descriptor(cpustate,SS);
1651      }
15921652   }
1653   else
1654      FAULT(FAULT_SS,0)
15931655   CYCLES(cpustate,CYCLES_POP_SREG);
15941656}
15951657
r14118r14119
15971659{
15981660   UINT8 modrm = FETCH(cpustate);
15991661   UINT16 value;
1662   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
16001663
1601   value = POP16(cpustate);
1664   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1665   {
1666      value = POP16(cpustate);
16021667
1603   if( modrm >= 0xc0 ) {
1604      STORE_RM16(modrm, value);
1605   } else {
1606      UINT32 ea = GetEA(cpustate,modrm);
1607      WRITE16(cpustate,ea, value);
1668      if( modrm >= 0xc0 ) {
1669         STORE_RM16(modrm, value);
1670      } else {
1671         UINT32 ea = GetEA(cpustate,modrm);
1672         WRITE16(cpustate,ea, value);
1673      }
16081674   }
1675   else
1676      FAULT(FAULT_SS,0)
16091677   CYCLES(cpustate,CYCLES_POP_RM);
16101678}
16111679
16121680static void I386OP(popa)(i386_state *cpustate)            // Opcode 0x61
16131681{
1614   REG16(DI) = POP16(cpustate);
1615   REG16(SI) = POP16(cpustate);
1616   REG16(BP) = POP16(cpustate);
1617   REG16(SP) += 2;
1618   REG16(BX) = POP16(cpustate);
1619   REG16(DX) = POP16(cpustate);
1620   REG16(CX) = POP16(cpustate);
1621   REG16(AX) = POP16(cpustate);
1682   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1683
1684   if(i386_limit_check(cpustate,SS,offset+15) == 0)
1685   {
1686      REG16(DI) = POP16(cpustate);
1687      REG16(SI) = POP16(cpustate);
1688      REG16(BP) = POP16(cpustate);
1689      REG16(SP) += 2;
1690      REG16(BX) = POP16(cpustate);
1691      REG16(DX) = POP16(cpustate);
1692      REG16(CX) = POP16(cpustate);
1693      REG16(AX) = POP16(cpustate);
1694   }
1695   else
1696      FAULT(FAULT_SS,0)
16221697   CYCLES(cpustate,CYCLES_POPA);
16231698}
16241699
r14118r14119
16281703   UINT32 current = get_flags(cpustate);
16291704   UINT8 IOPL = (current >> 12) & 0x03;
16301705   UINT32 mask = 0x7fd5;
1706   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
16311707
16321708   // IOPL can only change if CPL is 0
16331709   if(cpustate->CPL != 0)
r14118r14119
16471723      mask &= ~0x00003000;  // IOPL cannot be changed while in V8086 mode
16481724   }
16491725
1650   value = POP16(cpustate);
1651   set_flags(cpustate,(current & ~mask) | (value & mask));  // mask out reserved bits
1726   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1727   {
1728      value = POP16(cpustate);
1729      set_flags(cpustate,(current & ~mask) | (value & mask));  // mask out reserved bits
1730   }
1731   else
1732      FAULT(FAULT_SS,0)
16521733   CYCLES(cpustate,CYCLES_POPF);
16531734}
16541735
16551736static void I386OP(push_ax)(i386_state *cpustate)         // Opcode 0x50
16561737{
1657   PUSH16(cpustate, REG16(AX) );
1738   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1739   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1740      PUSH16(cpustate, REG16(AX) );
1741   else
1742      FAULT(FAULT_SS,0)
16581743   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
16591744}
16601745
16611746static void I386OP(push_cx)(i386_state *cpustate)         // Opcode 0x51
16621747{
1663   PUSH16(cpustate, REG16(CX) );
1748   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1749   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1750      PUSH16(cpustate, REG16(CX) );
1751   else
1752      FAULT(FAULT_SS,0)
16641753   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
16651754}
16661755
16671756static void I386OP(push_dx)(i386_state *cpustate)         // Opcode 0x52
16681757{
1669   PUSH16(cpustate, REG16(DX) );
1758   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1759   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1760      PUSH16(cpustate, REG16(DX) );
1761   else
1762      FAULT(FAULT_SS,0)
16701763   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
16711764}
16721765
16731766static void I386OP(push_bx)(i386_state *cpustate)         // Opcode 0x53
16741767{
1675   PUSH16(cpustate, REG16(BX) );
1768   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1769   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1770      PUSH16(cpustate, REG16(BX) );
1771   else
1772      FAULT(FAULT_SS,0)
16761773   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
16771774}
16781775
16791776static void I386OP(push_sp)(i386_state *cpustate)         // Opcode 0x54
16801777{
1681   PUSH16(cpustate, REG16(SP) );
1778   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1779   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1780      PUSH16(cpustate, REG16(SP) );
1781   else
1782      FAULT(FAULT_SS,0)
16821783   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
16831784}
16841785
16851786static void I386OP(push_bp)(i386_state *cpustate)         // Opcode 0x55
16861787{
1687   PUSH16(cpustate, REG16(BP) );
1788   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1789   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1790      PUSH16(cpustate, REG16(BP) );
1791   else
1792      FAULT(FAULT_SS,0)
16881793   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
16891794}
16901795
16911796static void I386OP(push_si)(i386_state *cpustate)         // Opcode 0x56
16921797{
1693   PUSH16(cpustate, REG16(SI) );
1798   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1799   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1800      PUSH16(cpustate, REG16(SI) );
1801   else
1802      FAULT(FAULT_SS,0)
16941803   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
16951804}
16961805
16971806static void I386OP(push_di)(i386_state *cpustate)         // Opcode 0x57
16981807{
1699   PUSH16(cpustate, REG16(DI) );
1808   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1809   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1810      PUSH16(cpustate, REG16(DI) );
1811   else
1812      FAULT(FAULT_SS,0)
17001813   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
17011814}
17021815
17031816static void I386OP(push_cs16)(i386_state *cpustate)         // Opcode 0x0e
17041817{
1705   PUSH16(cpustate, cpustate->sreg[CS].selector );
1818   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1819   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1820      PUSH16(cpustate, cpustate->sreg[CS].selector );
1821   else
1822      FAULT(FAULT_SS,0)
17061823   CYCLES(cpustate,CYCLES_PUSH_SREG);
17071824}
17081825
17091826static void I386OP(push_ds16)(i386_state *cpustate)         // Opcode 0x1e
17101827{
1711   PUSH16(cpustate, cpustate->sreg[DS].selector );
1828   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1829   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1830      PUSH16(cpustate, cpustate->sreg[DS].selector );
1831   else
1832      FAULT(FAULT_SS,0)
17121833   CYCLES(cpustate,CYCLES_PUSH_SREG);
17131834}
17141835
17151836static void I386OP(push_es16)(i386_state *cpustate)         // Opcode 0x06
17161837{
1717   PUSH16(cpustate, cpustate->sreg[ES].selector );
1838   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1839   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1840      PUSH16(cpustate, cpustate->sreg[ES].selector );
1841   else
1842      FAULT(FAULT_SS,0)
17181843   CYCLES(cpustate,CYCLES_PUSH_SREG);
17191844}
17201845
17211846static void I386OP(push_fs16)(i386_state *cpustate)         // Opcode 0x0f a0
17221847{
1723   PUSH16(cpustate, cpustate->sreg[FS].selector );
1848   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1849   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1850      PUSH16(cpustate, cpustate->sreg[FS].selector );
1851   else
1852      FAULT(FAULT_SS,0)
17241853   CYCLES(cpustate,CYCLES_PUSH_SREG);
17251854}
17261855
17271856static void I386OP(push_gs16)(i386_state *cpustate)         // Opcode 0x0f a8
17281857{
1729   PUSH16(cpustate, cpustate->sreg[GS].selector );
1858   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1859   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1860      PUSH16(cpustate, cpustate->sreg[GS].selector );
1861   else
1862      FAULT(FAULT_SS,0)
17301863   CYCLES(cpustate,CYCLES_PUSH_SREG);
17311864}
17321865
17331866static void I386OP(push_ss16)(i386_state *cpustate)         // Opcode 0x16
17341867{
1735   PUSH16(cpustate, cpustate->sreg[SS].selector );
1868   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1869   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1870      PUSH16(cpustate, cpustate->sreg[SS].selector );
1871   else
1872      FAULT(FAULT_SS,0)
17361873   CYCLES(cpustate,CYCLES_PUSH_SREG);
17371874}
17381875
17391876static void I386OP(push_i16)(i386_state *cpustate)         // Opcode 0x68
17401877{
17411878   UINT16 value = FETCH16(cpustate);
1742   PUSH16(cpustate,value);
1879   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1880   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1881      PUSH16(cpustate,value);
1882   else
1883      FAULT(FAULT_SS,0)
17431884   CYCLES(cpustate,CYCLES_PUSH_IMM);
17441885}
17451886
17461887static void I386OP(pusha)(i386_state *cpustate)            // Opcode 0x60
17471888{
17481889   UINT16 temp = REG16(SP);
1749   PUSH16(cpustate, REG16(AX) );
1750   PUSH16(cpustate, REG16(CX) );
1751   PUSH16(cpustate, REG16(DX) );
1752   PUSH16(cpustate, REG16(BX) );
1753   PUSH16(cpustate, temp );
1754   PUSH16(cpustate, REG16(BP) );
1755   PUSH16(cpustate, REG16(SI) );
1756   PUSH16(cpustate, REG16(DI) );
1890   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1891   if(i386_limit_check(cpustate,SS,offset-16) == 0)
1892   {
1893      PUSH16(cpustate, REG16(AX) );
1894      PUSH16(cpustate, REG16(CX) );
1895      PUSH16(cpustate, REG16(DX) );
1896      PUSH16(cpustate, REG16(BX) );
1897      PUSH16(cpustate, temp );
1898      PUSH16(cpustate, REG16(BP) );
1899      PUSH16(cpustate, REG16(SI) );
1900      PUSH16(cpustate, REG16(DI) );
1901   }
1902   else
1903      FAULT(FAULT_SS,0)
17571904   CYCLES(cpustate,CYCLES_PUSHA);
17581905}
17591906
17601907static void I386OP(pushf)(i386_state *cpustate)            // Opcode 0x9c
17611908{
1762   PUSH16(cpustate, get_flags(cpustate) & 0xffff );
1909   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1910   if(i386_limit_check(cpustate,SS,offset-2) == 0)
1911      PUSH16(cpustate, get_flags(cpustate) & 0xffff );
1912   else
1913      FAULT(FAULT_SS,0)
17631914   CYCLES(cpustate,CYCLES_PUSHF);
17641915}
17651916
r14118r14119
26722823                     cpustate->CF = 1;
26732824               }
26742825            } else {
2675               /* TODO: Divide by zero */
2826               cpustate->ext = 0;
2827               i386_trap(cpustate, 0, 0, 0);
2828               cpustate->ext = 1;
26762829            }
26772830         }
26782831         break;
r14118r14119
27042857                     cpustate->CF = 1;
27052858               }
27062859            } else {
2707               /* TODO: Divide by zero */
2860               cpustate->ext = 0;
2861               i386_trap(cpustate, 0, 0, 0);
2862               cpustate->ext = 1;
27082863            }
27092864         }
27102865         break;
r14118r14119
29043059      case 2:         /* LLDT */
29053060         if ( PROTECTED_MODE && !V8086_MODE )
29063061         {
3062            if(cpustate->CPL)
3063               FAULT(FAULT_GP,0)
29073064            if( modrm >= 0xc0 ) {
29083065               address = LOAD_RM16(modrm);
29093066               cpustate->ldtr.segment = address;
r14118r14119
29293086      case 3:         /* LTR */
29303087         if ( PROTECTED_MODE && !V8086_MODE )
29313088         {
3089            if(cpustate->CPL)
3090               FAULT(FAULT_GP,0)
29323091            if( modrm >= 0xc0 ) {
29333092               address = LOAD_RM16(modrm);
29343093               cpustate->task.segment = address;
r14118r14119
31173276         }
31183277      case 2:         /* LGDT */
31193278         {
3279            if(PROTECTED_MODE && cpustate->CPL)
3280               FAULT(FAULT_GP,0)
31203281            if( modrm >= 0xc0 ) {
31213282               address = LOAD_RM16(modrm);
31223283               ea = i386_translate(cpustate, CS, address );
r14118r14119
31303291         }
31313292      case 3:         /* LIDT */
31323293         {
3294            if(PROTECTED_MODE && cpustate->CPL)
3295               FAULT(FAULT_GP,0)
31333296            if( modrm >= 0xc0 ) {
31343297               address = LOAD_RM16(modrm);
31353298               ea = i386_translate(cpustate, CS, address );
r14118r14119
31553318         }
31563319      case 6:         /* LMSW */
31573320         {
3158            // TODO: Check for protection fault
3321            if(PROTECTED_MODE && cpustate->CPL)
3322               FAULT(FAULT_GP,0)
31593323            UINT16 b;
31603324            if( modrm >= 0xc0 ) {
31613325               b = LOAD_RM16(modrm);
src/emu/cpu/i386/i386.c
r14118r14119
363363   /* Must be within the relevant descriptor table limits */
364364   if(selector & 0x04)
365365   {
366      if((selector & ~0x07) > cpustate->ldtr.limit)
366      if((selector & ~0x07) >= cpustate->ldtr.limit)
367367         invalid = 1;
368368   }
369369   else
370370   {
371      if((selector & ~0x07) > cpustate->gdtr.limit)
371      if((selector & ~0x07) >= cpustate->gdtr.limit)
372372         invalid = 1;
373373   }
374374
r14118r14119
393393   }
394394}
395395
396#if 0
397// this will be more useful once expand-down segments are supported (the FM-Towns uses these for the stack)
398static void i386_stack_check(i386_state *cpustate, INT16 offset)
396static int i386_limit_check(i386_state *cpustate, int seg, UINT32 offset)
399397{
400398   if(PROTECTED_MODE && !V8086_MODE)
401399   {
402      // Check that both current and eventual stack pointers are within the segment limits
403      if(REG32(ESP) > cpustate->sreg[SS].limit)
400      if((cpustate->sreg[seg].flags & 0x0018) == 0x0010 && cpustate->sreg[seg].flags & 0x0004) // if expand-down data segment
404401      {
405         logerror("Stack (%08x): ESP is outside stack segment limit.\n",cpustate->pc);
406         FAULT(FAULT_SS,0);
402         if(offset <= cpustate->sreg[seg].limit)
403         {
404            logerror("Limit check at 0x%08x failed. Segment %04x, limit %08x, offset %08x (expand-down)\n",cpustate->pc,cpustate->sreg[seg].selector,cpustate->sreg[seg].limit,offset);
405            return 1;
406         }
407407      }
408      if(REG32(ESP) + offset > cpustate->sreg[SS].limit)
408      else
409409      {
410         logerror("Stack (%08x): ESP + offset (%i) is outside stack segment limit.\n",cpustate->pc,offset);
411         FAULT(FAULT_SS,0);
410         if(offset > cpustate->sreg[seg].limit)
411         {
412            logerror("Limit check at 0x%08x failed. Segment %04x, limit %08x, offset %08x\n",cpustate->pc,cpustate->sreg[seg].selector,cpustate->sreg[seg].limit,offset);
413            return 1;
414         }
412415      }
413416   }
417   return 0;
414418}
415#endif
416419
417420static void i386_protected_mode_sreg_load(i386_state *cpustate, UINT16 selector, UINT8 reg)
418421{
r14118r14119
431434      i386_load_protected_mode_segment(cpustate,&stack);
432435      DPL = (stack.flags >> 5) & 0x03;
433436
434      if((selector & ~0x0007) == 0)
437      if((selector & ~0x0003) == 0)
435438      {
436439         logerror("SReg Load (%08x): Selector is null.\n",cpustate->pc);
437440         FAULT(FAULT_GP,0)
438441      }
439442      if(selector & 0x0004)  // LDT
440443      {
441         if((selector & ~0x0007) > cpustate->ldtr.limit)
444         if((selector & ~0x0007) >= cpustate->ldtr.limit)
442445         {
443446            logerror("SReg Load (%08x): Selector is out of LDT bounds.\n",cpustate->pc);
444447            FAULT(FAULT_GP,selector & ~0x03)
r14118r14119
446449      }
447450      else  // GDT
448451      {
449         if((selector & ~0x0007) > cpustate->gdtr.limit)
452         if((selector & ~0x0007) >= cpustate->gdtr.limit)
450453         {
451454            logerror("SReg Load (%08x): Selector is out of GDT bounds.\n",cpustate->pc);
452455            FAULT(FAULT_GP,selector & ~0x03)
r14118r14119
477480   {
478481      I386_SREG desc;
479482
480      if((selector & ~0x0007) == 0)
483      if((selector & ~0x0003) == 0)
481484      {
482485         cpustate->sreg[reg].selector = selector;
483486         i386_load_segment_descriptor(cpustate, reg );
r14118r14119
492495
493496      if(selector & 0x0004)  // LDT
494497      {
495         if((selector & ~0x0007) > cpustate->ldtr.limit)
498         if((selector & ~0x0007) >= cpustate->ldtr.limit)
496499         {
497500            logerror("SReg Load (%08x): Selector is out of LDT bounds.\n",cpustate->pc);
498501            FAULT(FAULT_GP,selector & ~0x03)
r14118r14119
500503      }
501504      else  // GDT
502505      {
503         if((selector & ~0x0007) > cpustate->gdtr.limit)
506         if((selector & ~0x0007) >= cpustate->gdtr.limit)
504507         {
505508            logerror("SReg Load (%08x): Selector is out of GDT bounds.\n",cpustate->pc);
506509            FAULT(FAULT_GP,selector & ~0x03)
r14118r14119
607610      }
608611
609612      /* segment privilege checks */
610      if(entry > cpustate->idtr.limit)
613      if(entry >= cpustate->idtr.limit)
611614      {
612615         logerror("IRQ (%08x): Vector %02xh is past IDT limit.\n",cpustate->pc,entry);
613616         FAULT_EXP(FAULT_GP,entry+2)
r14118r14119
700703         }
701704         else
702705         {
703            if(segment > cpustate->gdtr.limit)
706            if(segment >= cpustate->gdtr.limit)
704707            {
705708               logerror("IRQ: Task gate: TSS is past GDT limit.\n");
706709               FAULT_EXP(FAULT_TS,segment & ~0x07);
r14118r14119
739742         }
740743         if(segment & 0x04)
741744         {
742            if((segment & ~0x07) > cpustate->ldtr.limit)
745            if((segment & ~0x07) >= cpustate->ldtr.limit)
743746            {
744747               logerror("IRQ: Gate segment is past LDT limit.\n");
745748               FAULT_EXP(FAULT_GP,(segment & 0x07)+cpustate->ext)
r14118r14119
747750         }
748751         else
749752         {
750            if((segment & ~0x07) > cpustate->gdtr.limit)
753            if((segment & ~0x07) >= cpustate->gdtr.limit)
751754            {
752755               logerror("IRQ: Gate segment is past GDT limit.\n");
753756               FAULT_EXP(FAULT_GP,(segment & 0x07)+cpustate->ext)
r14118r14119
788791            }
789792            if(stack.selector & 0x04)
790793            {
791               if((stack.selector & ~0x07) > cpustate->ldtr.base)
794               if((stack.selector & ~0x07) >= cpustate->ldtr.base)
792795               {
793796                  logerror("IRQ: New stack selector is past LDT limit.\n");
794797                  FAULT_EXP(FAULT_TS,(stack.selector & ~0x07)+cpustate->ext)
r14118r14119
796799            }
797800            else
798801            {
799               if((stack.selector & ~0x07) > cpustate->gdtr.base)
802               if((stack.selector & ~0x07) >= cpustate->gdtr.base)
800803               {
801804                  logerror("IRQ: New stack selector is past GDT limit.\n");
802805                  FAULT_EXP(FAULT_TS,(stack.selector & ~0x07)+cpustate->ext)
r14118r14119
11981201   UINT32 offset = off;
11991202
12001203   /* Check selector is not null */
1201   if((segment & ~0x07) == 0)
1204   if((segment & ~0x03) == 0)
12021205   {
12031206      logerror("JMP: Segment is null.\n");
12041207      FAULT(FAULT_GP,0)
r14118r14119
12071210   if((segment & 0x04) == 0)
12081211   {
12091212      /* check GDT limit */
1210      if((segment & ~0x07) > (cpustate->gdtr.limit))
1213      if((segment & ~0x07) >= (cpustate->gdtr.limit))
12111214      {
12121215         logerror("JMP: Segment is past GDT limit.\n");
12131216         FAULT(FAULT_GP,segment & 0xfffc)
r14118r14119
12161219   else
12171220   {
12181221      /* check LDT limit */
1219      if((segment & ~0x07) > (cpustate->ldtr.limit))
1222      if((segment & ~0x07) >= (cpustate->ldtr.limit))
12201223      {
12211224         logerror("JMP: Segment is past LDT limit.\n");
12221225         FAULT(FAULT_GP,segment & 0xfffc)
r14118r14119
14741477   UINT32 offset = off;
14751478   int x;
14761479
1477   if((selector & ~0x07) == 0)
1480   if((selector & ~0x03) == 0)
14781481   {
14791482      logerror("CALL (%08x): Selector is null.\n",cpustate->pc);
14801483      FAULT(FAULT_GP,0)  // #GP(0)
14811484   }
14821485   if(selector & 0x04)
14831486   {
1484      if((selector & ~0x07) > cpustate->ldtr.limit)
1487      if((selector & ~0x07) >= cpustate->ldtr.limit)
14851488      {
14861489         logerror("CALL: Selector is past LDT limit.\n");
14871490         FAULT(FAULT_GP,selector & ~0x03)  // #GP(selector)
r14118r14119
14891492   }
14901493   else
14911494   {
1492      if((selector & ~0x07) > cpustate->gdtr.limit)
1495      if((selector & ~0x07) >= cpustate->gdtr.limit)
14931496      {
14941497         logerror("CALL: Selector is past GDT limit.\n");
14951498         FAULT(FAULT_GP,selector & ~0x03)  // #GP(selector)
r14118r14119
19421945   if(RPL == CPL)
19431946   {
19441947      /* same privilege level */
1945      if((newCS & ~0x07) == 0)
1948      if((newCS & ~0x03) == 0)
19461949      {
19471950         logerror("RETF: Return segment is null.\n");
19481951         FAULT(FAULT_GP,0)
19491952      }
19501953      if(newCS & 0x04)
19511954      {
1952         if((newCS & ~0x07) > cpustate->ldtr.limit)
1955         if((newCS & ~0x07) >= cpustate->ldtr.limit)
19531956         {
19541957            logerror("RETF: Return segment is past LDT limit.\n");
19551958            FAULT(FAULT_GP,newCS & ~0x03)
r14118r14119
19571960      }
19581961      else
19591962      {
1960         if((newCS & ~0x07) > cpustate->gdtr.limit)
1963         if((newCS & ~0x07) >= cpustate->gdtr.limit)
19611964         {
19621965            logerror("RETF: Return segment is past GDT limit.\n");
19631966            FAULT(FAULT_GP,newCS & ~0x03)
r14118r14119
19961999      }
19972000      if(operand32 == 0)
19982001      {
1999         if(REG16(SP) > (cpustate->sreg[SS].limit & 0xffff)+1)
2002         if(i386_limit_check(cpustate,SS,REG16(SP)+count+3) != 0)
20002003         {
20012004            logerror("RETF (%08x): SP is past stack segment limit.\n",cpustate->pc);
20022005            FAULT(FAULT_SS,0)
r14118r14119
20042007      }
20052008      else
20062009      {
2007         if(REG32(ESP) > cpustate->sreg[SS].limit+1)
2010         if(i386_limit_check(cpustate,SS,REG32(ESP)+count+7) != 0)
20082011         {
20092012            logerror("RETF: ESP is past stack segment limit.\n");
20102013            FAULT(FAULT_SS,0)
r14118r14119
20202023      /* outer privilege level */
20212024      if(operand32 == 0)
20222025      {
2023         if(REG16(SP)+8+count > cpustate->sreg[SS].limit+1)
2026         if(i386_limit_check(cpustate,SS,REG16(SP)+count+7) != 0)
20242027         {
20252028            logerror("RETF (%08x): SP is past stack segment limit.\n",cpustate->pc);
20262029            FAULT(FAULT_SS,0)
r14118r14119
20282031      }
20292032      else
20302033      {
2031         if(REG32(ESP)+16+count > cpustate->sreg[SS].limit+1)
2034         if(i386_limit_check(cpustate,SS,REG32(SP)+count+15) != 0)
20322035         {
20332036            logerror("RETF: ESP is past stack segment limit.\n");
20342037            FAULT(FAULT_SS,0)
r14118r14119
20362039      }
20372040
20382041      /* Check CS selector and descriptor */
2039      if((newCS & ~0x07) == 0)
2042      if((newCS & ~0x03) == 0)
20402043      {
20412044         logerror("RETF: CS segment is null.\n");
20422045         FAULT(FAULT_GP,0)
20432046      }
20442047      if(newCS & 0x04)
20452048      {
2046         if((newCS & ~0x07) > cpustate->ldtr.limit)
2049         if((newCS & ~0x07) >= cpustate->ldtr.limit)
20472050         {
20482051            logerror("RETF: CS segment selector is past LDT limit.\n");
20492052            FAULT(FAULT_GP,newCS & ~0x07)
r14118r14119
20512054      }
20522055      else
20532056      {
2054         if((newCS & ~0x07) > cpustate->gdtr.limit)
2057         if((newCS & ~0x07) >= cpustate->gdtr.limit)
20552058         {
20562059            logerror("RETF: CS segment selector is past GDT limit.\n");
20572060            FAULT(FAULT_GP,newCS & ~0x07)
r14118r14119
22322235         logerror("IRET: Task return: Back-linked TSS is not in GDT.\n");
22332236         FAULT(FAULT_TS,task & ~0x07)
22342237      }
2235      if((task & ~0x07) > cpustate->gdtr.limit)
2238      if((task & ~0x07) >= cpustate->gdtr.limit)
22362239      {
22372240         logerror("IRET: Task return: Back-linked TSS is not in GDT.\n");
22382241         FAULT(FAULT_TS,task & ~0x07)
r14118r14119
24082411      {
24092412         if(operand32 == 0)
24102413         {
2411            if(REG16(SP)+4 > cpustate->sreg[SS].limit+1)
2414            if(i386_limit_check(cpustate,SS,REG16(SP)+3) != 0)
24122415            {
24132416               logerror("IRET: Data on stack is past SS limit.\n");
24142417               FAULT(FAULT_SS,0)
r14118r14119
24162419         }
24172420         else
24182421         {
2419            if(REG32(ESP)+6 > cpustate->sreg[SS].limit+1)
2422            if(i386_limit_check(cpustate,SS,REG32(ESP)+7) != 0)
24202423            {
24212424               logerror("IRET: Data on stack is past SS limit.\n");
24222425               FAULT(FAULT_SS,0)
r14118r14119
24332436            /* return to same privilege level */
24342437            if(operand32 == 0)
24352438            {
2436               if(REG16(SP)+6 > cpustate->sreg[SS].limit+1)
2439               if(i386_limit_check(cpustate,SS,REG16(SP)+5) != 0)
24372440               {
24382441                  logerror("IRET (%08x): Data on stack is past SS limit.\n",cpustate->pc);
24392442                  FAULT(FAULT_SS,0)
r14118r14119
24412444            }
24422445            else
24432446            {
2444               if(REG32(ESP)+12 > cpustate->sreg[SS].limit+1)
2447               if(i386_limit_check(cpustate,SS,REG32(ESP)+11) != 0)
24452448               {
24462449                  logerror("IRET (%08x): Data on stack is past SS limit.\n",cpustate->pc);
24472450                  FAULT(FAULT_SS,0)
24482451               }
24492452            }
2450            if((newCS & ~0x07) == 0)
2453            if((newCS & ~0x03) == 0)
24512454            {
24522455               logerror("IRET: Return CS selector is null.\n");
24532456               FAULT(FAULT_GP,0)
24542457            }
24552458            if(newCS & 0x04)
24562459            {
2457               if((newCS & ~0x07) > cpustate->ldtr.limit)
2460               if((newCS & ~0x07) >= cpustate->ldtr.limit)
24582461               {
24592462                  logerror("IRET: Return CS selector (%04x) is past LDT limit.\n",newCS);
24602463                  FAULT(FAULT_GP,newCS & ~0x07)
r14118r14119
24622465            }
24632466            else
24642467            {
2465               if((newCS & ~0x07) > cpustate->gdtr.limit)
2468               if((newCS & ~0x07) >= cpustate->gdtr.limit)
24662469               {
24672470                  logerror("IRET: Return CS selector is past GDT limit.\n");
24682471                  FAULT(FAULT_GP,newCS & ~0x07)
r14118r14119
25332536            i386_load_protected_mode_segment(cpustate,&stack);
25342537            if(operand32 == 0)
25352538            {
2536               if(REG16(SP)+10 > cpustate->sreg[SS].limit+1)
2539               if(i386_limit_check(cpustate,SS,REG16(SP)+9) != 0)
25372540               {
25382541                  logerror("IRET: SP is past SS limit.\n");
2539                  FAULT(FAULT_SS,0)               }
2542                  FAULT(FAULT_SS,0)
2543               }
25402544            }
25412545            else
25422546            {
2543               if(REG32(ESP)+20 > cpustate->sreg[SS].limit+1)
2547               if(i386_limit_check(cpustate,SS,REG32(ESP)+19) != 0)
25442548               {
25452549                  logerror("IRET: ESP is past SS limit.\n");
25462550                  FAULT(FAULT_SS,0)
25472551               }
25482552            }
25492553            /* Check CS selector and descriptor */
2550            if((newCS & ~0x07) == 0)
2554            if((newCS & ~0x03) == 0)
25512555            {
25522556               logerror("IRET: Return CS selector is null.\n");
25532557               FAULT(FAULT_GP,0)
25542558            }
25552559            if(newCS & 0x04)
25562560            {
2557               if((newCS & ~0x07) > cpustate->ldtr.limit)
2561               if((newCS & ~0x07) >= cpustate->ldtr.limit)
25582562               {
25592563                  logerror("IRET: Return CS selector is past LDT limit.\n");
25602564                  FAULT(FAULT_GP,newCS & ~0x07);
r14118r14119
25622566            }
25632567            else
25642568            {
2565               if((newCS & ~0x07) > cpustate->gdtr.limit)
2569               if((newCS & ~0x07) >= cpustate->gdtr.limit)
25662570               {
25672571                  logerror("IRET: Return CS selector is past GDT limit.\n");
25682572                  FAULT(FAULT_GP,newCS & ~0x07);
r14118r14119
25972601
25982602            /* Check SS selector and descriptor */
25992603            DPL = (stack.flags >> 5) & 0x03;
2600            if((newSS & ~0x07) == 0)
2604            if((newSS & ~0x03) == 0)
26012605            {
26022606               logerror("IRET: Return SS selector is null.\n");
26032607               FAULT(FAULT_GP,0)
26042608            }
26052609            if(newSS & 0x04)
26062610            {
2607               if((newSS & ~0x07) > cpustate->ldtr.limit)
2611               if((newSS & ~0x07) >= cpustate->ldtr.limit)
26082612               {
26092613                  logerror("IRET: Return SS selector is past LDT limit.\n");
26102614                  FAULT(FAULT_GP,newSS & ~0x07);
r14118r14119
26122616            }
26132617            else
26142618            {
2615               if((newSS & ~0x07) > cpustate->gdtr.limit)
2619               if((newSS & ~0x07) >= cpustate->gdtr.limit)
26162620               {
26172621                  logerror("IRET: Return SS selector is past GDT limit.\n");
26182622                  FAULT(FAULT_GP,newSS & ~0x07);
src/emu/cpu/i386/i386ops.c
r14118r14119
17011701      UINT32 ea = GetEA(cpustate,modrm);
17021702      UINT8 src = READ8(cpustate,ea);
17031703      UINT8 dst = LOAD_REG8(modrm);
1704      WRITE8(cpustate,ea, dst);
17041705      STORE_REG8(modrm, src);
1705      WRITE8(cpustate,ea, dst);
17061706      CYCLES(cpustate,CYCLES_XCHG_REG_MEM);
17071707   }
17081708}
r14118r14119
20712071                     cpustate->CF = 1;
20722072               }
20732073            } else {
2074               /* TODO: Divide by zero */
2074               cpustate->ext = 0;
2075               i386_trap(cpustate, 0, 0, 0);
2076               cpustate->ext = 1;
20752077            }
20762078         }
20772079         break;
r14118r14119
21032105                     cpustate->CF = 1;
21042106               }
21052107            } else {
2106               /* TODO: Divide by zero */
2108               cpustate->ext = 0;
2109               i386_trap(cpustate, 0, 0, 0);
2110               cpustate->ext = 1;
21072111            }
21082112         }
21092113         break;
src/emu/cpu/i386/i386op32.c
r14118r14119
13611361
13621362static void I386OP(pop_eax)(i386_state *cpustate)         // Opcode 0x58
13631363{
1364   REG32(EAX) = POP32(cpustate);
1364   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1365   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1366      REG32(EAX) = POP32(cpustate);
1367   else
1368      FAULT(FAULT_SS,0)
13651369   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
13661370}
13671371
13681372static void I386OP(pop_ecx)(i386_state *cpustate)         // Opcode 0x59
13691373{
1370   REG32(ECX) = POP32(cpustate);
1374   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1375   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1376      REG32(ECX) = POP32(cpustate);
1377   else
1378      FAULT(FAULT_SS,0)
13711379   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
13721380}
13731381
13741382static void I386OP(pop_edx)(i386_state *cpustate)         // Opcode 0x5a
13751383{
1376   REG32(EDX) = POP32(cpustate);
1384   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1385   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1386      REG32(EDX) = POP32(cpustate);
1387   else
1388      FAULT(FAULT_SS,0)
13771389   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
13781390}
13791391
13801392static void I386OP(pop_ebx)(i386_state *cpustate)         // Opcode 0x5b
13811393{
1382   REG32(EBX) = POP32(cpustate);
1394   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1395   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1396      REG32(EBX) = POP32(cpustate);
1397   else
1398      FAULT(FAULT_SS,0)
13831399   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
13841400}
13851401
13861402static void I386OP(pop_esp)(i386_state *cpustate)         // Opcode 0x5c
13871403{
1388   REG32(ESP) = POP32(cpustate);
1404   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1405   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1406      REG32(ESP) = POP32(cpustate);
1407   else
1408      FAULT(FAULT_SS,0)
13891409   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
13901410}
13911411
13921412static void I386OP(pop_ebp)(i386_state *cpustate)         // Opcode 0x5d
13931413{
1394   REG32(EBP) = POP32(cpustate);
1414   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1415   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1416      REG32(EBP) = POP32(cpustate);
1417   else
1418      FAULT(FAULT_SS,0)
13951419   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
13961420}
13971421
13981422static void I386OP(pop_esi)(i386_state *cpustate)         // Opcode 0x5e
13991423{
1400   REG32(ESI) = POP32(cpustate);
1424   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1425   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1426      REG32(ESI) = POP32(cpustate);
1427   else
1428      FAULT(FAULT_SS,0)
14011429   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
14021430}
14031431
14041432static void I386OP(pop_edi)(i386_state *cpustate)         // Opcode 0x5f
14051433{
1406   REG32(EDI) = POP32(cpustate);
1434   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1435   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1436      REG32(EDI) = POP32(cpustate);
1437   else
1438      FAULT(FAULT_SS,0)
14071439   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
14081440}
14091441
14101442static void I386OP(pop_ds32)(i386_state *cpustate)         // Opcode 0x1f
14111443{
1412   cpustate->sreg[DS].selector = POP32(cpustate);
1413   if( PROTECTED_MODE ) {
1414      i386_load_segment_descriptor(cpustate,DS);
1415   } else {
1416      i386_load_segment_descriptor(cpustate,DS);
1444   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1445   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1446   {
1447      cpustate->sreg[DS].selector = POP32(cpustate);
1448      if( PROTECTED_MODE ) {
1449         i386_load_segment_descriptor(cpustate,DS);
1450      } else {
1451         i386_load_segment_descriptor(cpustate,DS);
1452      }
14171453   }
1454   else
1455      FAULT(FAULT_SS,0)
14181456   CYCLES(cpustate,CYCLES_POP_SREG);
14191457}
14201458
14211459static void I386OP(pop_es32)(i386_state *cpustate)         // Opcode 0x07
14221460{
1423   cpustate->sreg[ES].selector = POP32(cpustate);
1424   if( PROTECTED_MODE ) {
1425      i386_load_segment_descriptor(cpustate,ES);
1426   } else {
1427      i386_load_segment_descriptor(cpustate,ES);
1461   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1462   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1463   {
1464      cpustate->sreg[ES].selector = POP32(cpustate);
1465      if( PROTECTED_MODE ) {
1466         i386_load_segment_descriptor(cpustate,ES);
1467      } else {
1468         i386_load_segment_descriptor(cpustate,ES);
1469      }
14281470   }
1471   else
1472      FAULT(FAULT_SS,0)
14291473   CYCLES(cpustate,CYCLES_POP_SREG);
14301474}
14311475
14321476static void I386OP(pop_fs32)(i386_state *cpustate)         // Opcode 0x0f a1
14331477{
1434   cpustate->sreg[FS].selector = POP32(cpustate);
1435   if( PROTECTED_MODE ) {
1436      i386_load_segment_descriptor(cpustate,FS);
1437   } else {
1438      i386_load_segment_descriptor(cpustate,FS);
1478   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1479   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1480   {
1481      cpustate->sreg[FS].selector = POP32(cpustate);
1482      if( PROTECTED_MODE ) {
1483         i386_load_segment_descriptor(cpustate,FS);
1484      } else {
1485         i386_load_segment_descriptor(cpustate,FS);
1486      }
14391487   }
1488   else
1489      FAULT(FAULT_SS,0)
14401490   CYCLES(cpustate,CYCLES_POP_SREG);
14411491}
14421492
14431493static void I386OP(pop_gs32)(i386_state *cpustate)         // Opcode 0x0f a9
14441494{
1445   cpustate->sreg[GS].selector = POP32(cpustate);
1446   if( PROTECTED_MODE ) {
1447      i386_load_segment_descriptor(cpustate,GS);
1448   } else {
1449      i386_load_segment_descriptor(cpustate,GS);
1495   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1496   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1497   {
1498      cpustate->sreg[GS].selector = POP32(cpustate);
1499      if( PROTECTED_MODE ) {
1500         i386_load_segment_descriptor(cpustate,GS);
1501      } else {
1502         i386_load_segment_descriptor(cpustate,GS);
1503      }
14501504   }
1505   else
1506      FAULT(FAULT_SS,0)
14511507   CYCLES(cpustate,CYCLES_POP_SREG);
14521508}
14531509
14541510static void I386OP(pop_ss32)(i386_state *cpustate)         // Opcode 0x17
14551511{
1512   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
14561513   if(cpustate->IF != 0) // if external interrupts are enabled
14571514   {
14581515      cpustate->IF = 0;  // reset IF for the next instruction
14591516      cpustate->delayed_interrupt_enable = 1;
14601517   }
14611518
1462   cpustate->sreg[SS].selector = POP32(cpustate);
1463   if( PROTECTED_MODE ) {
1464      i386_load_segment_descriptor(cpustate,SS);
1465   } else {
1466      i386_load_segment_descriptor(cpustate,SS);
1519   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1520   {
1521      cpustate->sreg[SS].selector = POP32(cpustate);
1522      if( PROTECTED_MODE ) {
1523         i386_load_segment_descriptor(cpustate,SS);
1524      } else {
1525         i386_load_segment_descriptor(cpustate,SS);
1526      }
14671527   }
1528   else
1529      FAULT(FAULT_SS,0)
14681530   CYCLES(cpustate,CYCLES_POP_SREG);
14691531}
14701532
r14118r14119
14721534{
14731535   UINT8 modrm = FETCH(cpustate);
14741536   UINT32 value;
1537   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1538   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1539   {
1540      value = POP32(cpustate);
14751541
1476   value = POP32(cpustate);
1477
1478   if( modrm >= 0xc0 ) {
1479      STORE_RM32(modrm, value);
1480   } else {
1481      UINT32 ea = GetEA(cpustate,modrm);
1482      WRITE32(cpustate,ea, value);
1542      if( modrm >= 0xc0 ) {
1543         STORE_RM32(modrm, value);
1544      } else {
1545         UINT32 ea = GetEA(cpustate,modrm);
1546         WRITE32(cpustate,ea, value);
1547      }
14831548   }
1549   else
1550      FAULT(FAULT_SS,0)
14841551   CYCLES(cpustate,CYCLES_POP_RM);
14851552}
14861553
14871554static void I386OP(popad)(i386_state *cpustate)            // Opcode 0x61
14881555{
1489   REG32(EDI) = POP32(cpustate);
1490   REG32(ESI) = POP32(cpustate);
1491   REG32(EBP) = POP32(cpustate);
1492   REG32(ESP) += 4;
1493   REG32(EBX) = POP32(cpustate);
1494   REG32(EDX) = POP32(cpustate);
1495   REG32(ECX) = POP32(cpustate);
1496   REG32(EAX) = POP32(cpustate);
1556   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1557   if(i386_limit_check(cpustate,SS,offset+31) == 0)
1558   {
1559      REG32(EDI) = POP32(cpustate);
1560      REG32(ESI) = POP32(cpustate);
1561      REG32(EBP) = POP32(cpustate);
1562      REG32(ESP) += 4;
1563      REG32(EBX) = POP32(cpustate);
1564      REG32(EDX) = POP32(cpustate);
1565      REG32(ECX) = POP32(cpustate);
1566      REG32(EAX) = POP32(cpustate);
1567   }
1568   else
1569      FAULT(FAULT_SS,0)
14971570   CYCLES(cpustate,CYCLES_POPA);
14981571}
14991572
r14118r14119
15031576   UINT32 current = get_flags(cpustate);
15041577   UINT8 IOPL = (current >> 12) & 0x03;
15051578   UINT32 mask = 0x00257fd5;  // VM, VIP and VIF cannot be set by POPF/POPFD
1579   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
15061580
15071581   // IOPL can only change if CPL is 0
15081582   if(cpustate->CPL != 0)
r14118r14119
15221596      mask &= ~0x00003000;  // IOPL cannot be changed while in V8086 mode
15231597   }
15241598
1525   value = POP32(cpustate);
1526   value &= ~0x00010000;  // RF will always return zero
1527   set_flags(cpustate,(current & ~mask) | (value & mask));  // mask out reserved bits
1599   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1600   {
1601      value = POP32(cpustate);
1602      value &= ~0x00010000;  // RF will always return zero
1603      set_flags(cpustate,(current & ~mask) | (value & mask));  // mask out reserved bits
1604   }
1605   else
1606      FAULT(FAULT_SS,0)
15281607   CYCLES(cpustate,CYCLES_POPF);
15291608}
15301609
15311610static void I386OP(push_eax)(i386_state *cpustate)         // Opcode 0x50
15321611{
1533   PUSH32(cpustate, REG32(EAX) );
1612   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1613   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1614      PUSH32(cpustate, REG32(EAX) );
1615   else
1616      FAULT(FAULT_SS,0)
15341617   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
15351618}
15361619
15371620static void I386OP(push_ecx)(i386_state *cpustate)         // Opcode 0x51
15381621{
1539   PUSH32(cpustate, REG32(ECX) );
1622   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1623   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1624      PUSH32(cpustate, REG32(ECX) );
1625   else
1626      FAULT(FAULT_SS,0)
15401627   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
15411628}
15421629
15431630static void I386OP(push_edx)(i386_state *cpustate)         // Opcode 0x52
15441631{
1545   PUSH32(cpustate, REG32(EDX) );
1632   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1633   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1634      PUSH32(cpustate, REG32(EDX) );
1635   else
1636      FAULT(FAULT_SS,0)
15461637   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
15471638}
15481639
15491640static void I386OP(push_ebx)(i386_state *cpustate)         // Opcode 0x53
15501641{
1551   PUSH32(cpustate, REG32(EBX) );
1642   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1643   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1644      PUSH32(cpustate, REG32(EBX) );
1645   else
1646      FAULT(FAULT_SS,0)
15521647   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
15531648}
15541649
15551650static void I386OP(push_esp)(i386_state *cpustate)         // Opcode 0x54
15561651{
1557   PUSH32(cpustate, REG32(ESP) );
1652   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1653   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1654      PUSH32(cpustate, REG32(ESP) );
1655   else
1656      FAULT(FAULT_SS,0)
15581657   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
15591658}
15601659
15611660static void I386OP(push_ebp)(i386_state *cpustate)         // Opcode 0x55
15621661{
1563   PUSH32(cpustate, REG32(EBP) );
1662   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1663   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1664      PUSH32(cpustate, REG32(EBP) );
1665   else
1666      FAULT(FAULT_SS,0)
15641667   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
15651668}
15661669
15671670static void I386OP(push_esi)(i386_state *cpustate)         // Opcode 0x56
15681671{
1569   PUSH32(cpustate, REG32(ESI) );
1672   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1673   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1674      PUSH32(cpustate, REG32(ESI) );
1675   else
1676      FAULT(FAULT_SS,0)
15701677   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
15711678}
15721679
15731680static void I386OP(push_edi)(i386_state *cpustate)         // Opcode 0x57
15741681{
1575   PUSH32(cpustate, REG32(EDI) );
1682   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1683   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1684      PUSH32(cpustate, REG32(EDI) );
1685   else
1686      FAULT(FAULT_SS,0)
15761687   CYCLES(cpustate,CYCLES_PUSH_REG_SHORT);
15771688}
15781689
15791690static void I386OP(push_cs32)(i386_state *cpustate)         // Opcode 0x0e
15801691{
1581   PUSH32(cpustate, cpustate->sreg[CS].selector );
1692   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1693   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1694      PUSH32(cpustate, cpustate->sreg[CS].selector );
1695   else
1696      FAULT(FAULT_SS,0)
15821697   CYCLES(cpustate,CYCLES_PUSH_SREG);
15831698}
15841699
15851700static void I386OP(push_ds32)(i386_state *cpustate)         // Opcode 0x1e
15861701{
1587   PUSH32(cpustate, cpustate->sreg[DS].selector );
1702   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1703   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1704      PUSH32(cpustate, cpustate->sreg[DS].selector );
1705   else
1706      FAULT(FAULT_SS,0)
15881707   CYCLES(cpustate,CYCLES_PUSH_SREG);
15891708}
15901709
15911710static void I386OP(push_es32)(i386_state *cpustate)         // Opcode 0x06
15921711{
1593   PUSH32(cpustate, cpustate->sreg[ES].selector );
1712   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1713   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1714      PUSH32(cpustate, cpustate->sreg[ES].selector );
1715   else
1716      FAULT(FAULT_SS,0)
15941717   CYCLES(cpustate,CYCLES_PUSH_SREG);
15951718}
15961719
15971720static void I386OP(push_fs32)(i386_state *cpustate)         // Opcode 0x0f a0
15981721{
1599   PUSH32(cpustate, cpustate->sreg[FS].selector );
1722   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1723   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1724      PUSH32(cpustate, cpustate->sreg[FS].selector );
1725   else
1726      FAULT(FAULT_SS,0)
16001727   CYCLES(cpustate,CYCLES_PUSH_SREG);
16011728}
16021729
16031730static void I386OP(push_gs32)(i386_state *cpustate)         // Opcode 0x0f a8
16041731{
1605   PUSH32(cpustate, cpustate->sreg[GS].selector );
1732   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1733   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1734      PUSH32(cpustate, cpustate->sreg[GS].selector );
1735   else
1736      FAULT(FAULT_SS,0)
16061737   CYCLES(cpustate,CYCLES_PUSH_SREG);
16071738}
16081739
16091740static void I386OP(push_ss32)(i386_state *cpustate)         // Opcode 0x16
16101741{
1611   PUSH32(cpustate, cpustate->sreg[SS].selector );
1742   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1743   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1744      PUSH32(cpustate, cpustate->sreg[SS].selector );
1745   else
1746      FAULT(FAULT_SS,0)
16121747   CYCLES(cpustate,CYCLES_PUSH_SREG);
16131748}
16141749
16151750static void I386OP(push_i32)(i386_state *cpustate)         // Opcode 0x68
16161751{
16171752   UINT32 value = FETCH32(cpustate);
1618   PUSH32(cpustate,value);
1753   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1754   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1755      PUSH32(cpustate,value);
1756   else
1757      FAULT(FAULT_SS,0)
16191758   CYCLES(cpustate,CYCLES_PUSH_IMM);
16201759}
16211760
16221761static void I386OP(pushad)(i386_state *cpustate)         // Opcode 0x60
16231762{
16241763   UINT32 temp = REG32(ESP);
1625   PUSH32(cpustate, REG32(EAX) );
1626   PUSH32(cpustate, REG32(ECX) );
1627   PUSH32(cpustate, REG32(EDX) );
1628   PUSH32(cpustate, REG32(EBX) );
1629   PUSH32(cpustate, temp );
1630   PUSH32(cpustate, REG32(EBP) );
1631   PUSH32(cpustate, REG32(ESI) );
1632   PUSH32(cpustate, REG32(EDI) );
1764   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1765   if(i386_limit_check(cpustate,SS,offset-32) == 0)
1766   {
1767      PUSH32(cpustate, REG32(EAX) );
1768      PUSH32(cpustate, REG32(ECX) );
1769      PUSH32(cpustate, REG32(EDX) );
1770      PUSH32(cpustate, REG32(EBX) );
1771      PUSH32(cpustate, temp );
1772      PUSH32(cpustate, REG32(EBP) );
1773      PUSH32(cpustate, REG32(ESI) );
1774      PUSH32(cpustate, REG32(EDI) );
1775   }
1776   else
1777      FAULT(FAULT_SS,0)
16331778   CYCLES(cpustate,CYCLES_PUSHA);
16341779}
16351780
16361781static void I386OP(pushfd)(i386_state *cpustate)         // Opcode 0x9c
16371782{
1638   PUSH32(cpustate, get_flags(cpustate) & 0x00fcffff );
1783   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1784   if(i386_limit_check(cpustate,SS,offset-4) == 0)
1785      PUSH32(cpustate, get_flags(cpustate) & 0x00fcffff );
1786   else
1787      FAULT(FAULT_SS,0)
16391788   CYCLES(cpustate,CYCLES_PUSHF);
16401789}
16411790
r14118r14119
20212170      UINT32 ea = GetEA(cpustate,modrm);
20222171      UINT32 src = READ32(cpustate,ea);
20232172      UINT32 dst = LOAD_REG32(modrm);
2173      WRITE32(cpustate,ea, dst);
20242174      STORE_REG32(modrm, src);
2025      WRITE32(cpustate,ea, dst);
20262175      CYCLES(cpustate,CYCLES_XCHG_REG_MEM);
20272176   }
20282177}
r14118r14119
25262675                  REG32(EAX) = (UINT32)result;
25272676               }
25282677            } else {
2529               /* TODO: Divide by zero */
2678               cpustate->ext = 0;
2679               i386_trap(cpustate, 0, 0, 0);
2680               cpustate->ext = 1;
25302681            }
25312682         }
25322683         break;
r14118r14119
25542705                  REG32(EAX) = (UINT32)result;
25552706               }
25562707            } else {
2557               /* TODO: Divide by zero */
2708               cpustate->ext = 0;
2709               i386_trap(cpustate, 0, 0, 0);
2710               cpustate->ext = 1;
25582711            }
25592712         }
25602713         break;
r14118r14119
27532906      case 2:         /* LLDT */
27542907         if ( PROTECTED_MODE && !V8086_MODE )
27552908         {
2909            if(cpustate->CPL)
2910               FAULT(FAULT_GP,0)
27562911            if( modrm >= 0xc0 ) {
27572912               address = LOAD_RM32(modrm);
27582913               cpustate->ldtr.segment = address;
r14118r14119
27782933      case 3:         /* LTR */
27792934         if ( PROTECTED_MODE && !V8086_MODE )
27802935         {
2936            if(cpustate->CPL)
2937               FAULT(FAULT_GP,0)
27812938            if( modrm >= 0xc0 ) {
27822939               address = LOAD_RM32(modrm);
27832940               cpustate->task.segment = address;
r14118r14119
29653122         }
29663123      case 2:         /* LGDT */
29673124         {
3125            if(PROTECTED_MODE && cpustate->CPL)
3126               FAULT(FAULT_GP,0)
29683127            if( modrm >= 0xc0 ) {
29693128               address = LOAD_RM32(modrm);
29703129               ea = i386_translate(cpustate, CS, address );
r14118r14119
29783137         }
29793138      case 3:         /* LIDT */
29803139         {
3140            if(PROTECTED_MODE && cpustate->CPL)
3141               FAULT(FAULT_GP,0)
29813142            if( modrm >= 0xc0 ) {
29823143               address = LOAD_RM32(modrm);
29833144               ea = i386_translate(cpustate, CS, address );

Previous 15994 Revisions Next


© 1998-2018 The MESS Team