src/emu/cpu/i386/i386op16.c
| r14118 | r14119 | |
| 1189 | 1189 | static void I386OP(loop16)(i386_state *cpustate) // Opcode 0xe2 |
| 1190 | 1190 | { |
| 1191 | 1191 | 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 ) { |
| 1194 | 1194 | if (cpustate->sreg[CS].d) |
| 1195 | 1195 | { |
| 1196 | 1196 | cpustate->eip += disp; |
| r14118 | r14119 | |
| 1207 | 1207 | static void I386OP(loopne16)(i386_state *cpustate) // Opcode 0xe0 |
| 1208 | 1208 | { |
| 1209 | 1209 | 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 ) { |
| 1212 | 1212 | if (cpustate->sreg[CS].d) |
| 1213 | 1213 | { |
| 1214 | 1214 | cpustate->eip += disp; |
| r14118 | r14119 | |
| 1225 | 1225 | static void I386OP(loopz16)(i386_state *cpustate) // Opcode 0xe1 |
| 1226 | 1226 | { |
| 1227 | 1227 | 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 ) { |
| 1230 | 1230 | if (cpustate->sreg[CS].d) |
| 1231 | 1231 | { |
| 1232 | 1232 | cpustate->eip += disp; |
| r14118 | r14119 | |
| 1486 | 1486 | |
| 1487 | 1487 | static void I386OP(pop_ax)(i386_state *cpustate) // Opcode 0x58 |
| 1488 | 1488 | { |
| 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) |
| 1490 | 1494 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1491 | 1495 | } |
| 1492 | 1496 | |
| 1493 | 1497 | static void I386OP(pop_cx)(i386_state *cpustate) // Opcode 0x59 |
| 1494 | 1498 | { |
| 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) |
| 1496 | 1504 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1497 | 1505 | } |
| 1498 | 1506 | |
| 1499 | 1507 | static void I386OP(pop_dx)(i386_state *cpustate) // Opcode 0x5a |
| 1500 | 1508 | { |
| 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) |
| 1502 | 1514 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1503 | 1515 | } |
| 1504 | 1516 | |
| 1505 | 1517 | static void I386OP(pop_bx)(i386_state *cpustate) // Opcode 0x5b |
| 1506 | 1518 | { |
| 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) |
| 1508 | 1524 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1509 | 1525 | } |
| 1510 | 1526 | |
| 1511 | 1527 | static void I386OP(pop_sp)(i386_state *cpustate) // Opcode 0x5c |
| 1512 | 1528 | { |
| 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) |
| 1514 | 1534 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1515 | 1535 | } |
| 1516 | 1536 | |
| 1517 | 1537 | static void I386OP(pop_bp)(i386_state *cpustate) // Opcode 0x5d |
| 1518 | 1538 | { |
| 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) |
| 1520 | 1544 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1521 | 1545 | } |
| 1522 | 1546 | |
| 1523 | 1547 | static void I386OP(pop_si)(i386_state *cpustate) // Opcode 0x5e |
| 1524 | 1548 | { |
| 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) |
| 1526 | 1554 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1527 | 1555 | } |
| 1528 | 1556 | |
| 1529 | 1557 | static void I386OP(pop_di)(i386_state *cpustate) // Opcode 0x5f |
| 1530 | 1558 | { |
| 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) |
| 1532 | 1564 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1533 | 1565 | } |
| 1534 | 1566 | |
| 1535 | 1567 | static void I386OP(pop_ds16)(i386_state *cpustate) // Opcode 0x1f |
| 1536 | 1568 | { |
| 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 | } |
| 1542 | 1578 | } |
| 1579 | else |
| 1580 | FAULT(FAULT_SS,0) |
| 1543 | 1581 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1544 | 1582 | } |
| 1545 | 1583 | |
| 1546 | 1584 | static void I386OP(pop_es16)(i386_state *cpustate) // Opcode 0x07 |
| 1547 | 1585 | { |
| 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 | } |
| 1553 | 1595 | } |
| 1596 | else |
| 1597 | FAULT(FAULT_SS,0) |
| 1554 | 1598 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1555 | 1599 | } |
| 1556 | 1600 | |
| 1557 | 1601 | static void I386OP(pop_fs16)(i386_state *cpustate) // Opcode 0x0f a1 |
| 1558 | 1602 | { |
| 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 | } |
| 1564 | 1612 | } |
| 1613 | else |
| 1614 | FAULT(FAULT_SS,0) |
| 1565 | 1615 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1566 | 1616 | } |
| 1567 | 1617 | |
| 1568 | 1618 | static void I386OP(pop_gs16)(i386_state *cpustate) // Opcode 0x0f a9 |
| 1569 | 1619 | { |
| 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 | } |
| 1575 | 1629 | } |
| 1630 | else |
| 1631 | FAULT(FAULT_SS,0) |
| 1576 | 1632 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1577 | 1633 | } |
| 1578 | 1634 | |
| 1579 | 1635 | static void I386OP(pop_ss16)(i386_state *cpustate) // Opcode 0x17 |
| 1580 | 1636 | { |
| 1637 | UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP)); |
| 1581 | 1638 | if(cpustate->IF != 0) // if external interrupts are enabled |
| 1582 | 1639 | { |
| 1583 | 1640 | cpustate->IF = 0; // reset IF for the next instruction |
| 1584 | 1641 | cpustate->delayed_interrupt_enable = 1; |
| 1585 | 1642 | } |
| 1586 | 1643 | |
| 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 | } |
| 1592 | 1652 | } |
| 1653 | else |
| 1654 | FAULT(FAULT_SS,0) |
| 1593 | 1655 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1594 | 1656 | } |
| 1595 | 1657 | |
| r14118 | r14119 | |
| 1597 | 1659 | { |
| 1598 | 1660 | UINT8 modrm = FETCH(cpustate); |
| 1599 | 1661 | UINT16 value; |
| 1662 | UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP)); |
| 1600 | 1663 | |
| 1601 | | value = POP16(cpustate); |
| 1664 | if(i386_limit_check(cpustate,SS,offset+1) == 0) |
| 1665 | { |
| 1666 | value = POP16(cpustate); |
| 1602 | 1667 | |
| 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 | } |
| 1608 | 1674 | } |
| 1675 | else |
| 1676 | FAULT(FAULT_SS,0) |
| 1609 | 1677 | CYCLES(cpustate,CYCLES_POP_RM); |
| 1610 | 1678 | } |
| 1611 | 1679 | |
| 1612 | 1680 | static void I386OP(popa)(i386_state *cpustate) // Opcode 0x61 |
| 1613 | 1681 | { |
| 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) |
| 1622 | 1697 | CYCLES(cpustate,CYCLES_POPA); |
| 1623 | 1698 | } |
| 1624 | 1699 | |
| r14118 | r14119 | |
| 1628 | 1703 | UINT32 current = get_flags(cpustate); |
| 1629 | 1704 | UINT8 IOPL = (current >> 12) & 0x03; |
| 1630 | 1705 | UINT32 mask = 0x7fd5; |
| 1706 | UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP)); |
| 1631 | 1707 | |
| 1632 | 1708 | // IOPL can only change if CPL is 0 |
| 1633 | 1709 | if(cpustate->CPL != 0) |
| r14118 | r14119 | |
| 1647 | 1723 | mask &= ~0x00003000; // IOPL cannot be changed while in V8086 mode |
| 1648 | 1724 | } |
| 1649 | 1725 | |
| 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) |
| 1652 | 1733 | CYCLES(cpustate,CYCLES_POPF); |
| 1653 | 1734 | } |
| 1654 | 1735 | |
| 1655 | 1736 | static void I386OP(push_ax)(i386_state *cpustate) // Opcode 0x50 |
| 1656 | 1737 | { |
| 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) |
| 1658 | 1743 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1659 | 1744 | } |
| 1660 | 1745 | |
| 1661 | 1746 | static void I386OP(push_cx)(i386_state *cpustate) // Opcode 0x51 |
| 1662 | 1747 | { |
| 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) |
| 1664 | 1753 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1665 | 1754 | } |
| 1666 | 1755 | |
| 1667 | 1756 | static void I386OP(push_dx)(i386_state *cpustate) // Opcode 0x52 |
| 1668 | 1757 | { |
| 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) |
| 1670 | 1763 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1671 | 1764 | } |
| 1672 | 1765 | |
| 1673 | 1766 | static void I386OP(push_bx)(i386_state *cpustate) // Opcode 0x53 |
| 1674 | 1767 | { |
| 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) |
| 1676 | 1773 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1677 | 1774 | } |
| 1678 | 1775 | |
| 1679 | 1776 | static void I386OP(push_sp)(i386_state *cpustate) // Opcode 0x54 |
| 1680 | 1777 | { |
| 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) |
| 1682 | 1783 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1683 | 1784 | } |
| 1684 | 1785 | |
| 1685 | 1786 | static void I386OP(push_bp)(i386_state *cpustate) // Opcode 0x55 |
| 1686 | 1787 | { |
| 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) |
| 1688 | 1793 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1689 | 1794 | } |
| 1690 | 1795 | |
| 1691 | 1796 | static void I386OP(push_si)(i386_state *cpustate) // Opcode 0x56 |
| 1692 | 1797 | { |
| 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) |
| 1694 | 1803 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1695 | 1804 | } |
| 1696 | 1805 | |
| 1697 | 1806 | static void I386OP(push_di)(i386_state *cpustate) // Opcode 0x57 |
| 1698 | 1807 | { |
| 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) |
| 1700 | 1813 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1701 | 1814 | } |
| 1702 | 1815 | |
| 1703 | 1816 | static void I386OP(push_cs16)(i386_state *cpustate) // Opcode 0x0e |
| 1704 | 1817 | { |
| 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) |
| 1706 | 1823 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1707 | 1824 | } |
| 1708 | 1825 | |
| 1709 | 1826 | static void I386OP(push_ds16)(i386_state *cpustate) // Opcode 0x1e |
| 1710 | 1827 | { |
| 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) |
| 1712 | 1833 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1713 | 1834 | } |
| 1714 | 1835 | |
| 1715 | 1836 | static void I386OP(push_es16)(i386_state *cpustate) // Opcode 0x06 |
| 1716 | 1837 | { |
| 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) |
| 1718 | 1843 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1719 | 1844 | } |
| 1720 | 1845 | |
| 1721 | 1846 | static void I386OP(push_fs16)(i386_state *cpustate) // Opcode 0x0f a0 |
| 1722 | 1847 | { |
| 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) |
| 1724 | 1853 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1725 | 1854 | } |
| 1726 | 1855 | |
| 1727 | 1856 | static void I386OP(push_gs16)(i386_state *cpustate) // Opcode 0x0f a8 |
| 1728 | 1857 | { |
| 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) |
| 1730 | 1863 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1731 | 1864 | } |
| 1732 | 1865 | |
| 1733 | 1866 | static void I386OP(push_ss16)(i386_state *cpustate) // Opcode 0x16 |
| 1734 | 1867 | { |
| 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) |
| 1736 | 1873 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1737 | 1874 | } |
| 1738 | 1875 | |
| 1739 | 1876 | static void I386OP(push_i16)(i386_state *cpustate) // Opcode 0x68 |
| 1740 | 1877 | { |
| 1741 | 1878 | 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) |
| 1743 | 1884 | CYCLES(cpustate,CYCLES_PUSH_IMM); |
| 1744 | 1885 | } |
| 1745 | 1886 | |
| 1746 | 1887 | static void I386OP(pusha)(i386_state *cpustate) // Opcode 0x60 |
| 1747 | 1888 | { |
| 1748 | 1889 | 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) |
| 1757 | 1904 | CYCLES(cpustate,CYCLES_PUSHA); |
| 1758 | 1905 | } |
| 1759 | 1906 | |
| 1760 | 1907 | static void I386OP(pushf)(i386_state *cpustate) // Opcode 0x9c |
| 1761 | 1908 | { |
| 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) |
| 1763 | 1914 | CYCLES(cpustate,CYCLES_PUSHF); |
| 1764 | 1915 | } |
| 1765 | 1916 | |
| r14118 | r14119 | |
| 2672 | 2823 | cpustate->CF = 1; |
| 2673 | 2824 | } |
| 2674 | 2825 | } else { |
| 2675 | | /* TODO: Divide by zero */ |
| 2826 | cpustate->ext = 0; |
| 2827 | i386_trap(cpustate, 0, 0, 0); |
| 2828 | cpustate->ext = 1; |
| 2676 | 2829 | } |
| 2677 | 2830 | } |
| 2678 | 2831 | break; |
| r14118 | r14119 | |
| 2704 | 2857 | cpustate->CF = 1; |
| 2705 | 2858 | } |
| 2706 | 2859 | } else { |
| 2707 | | /* TODO: Divide by zero */ |
| 2860 | cpustate->ext = 0; |
| 2861 | i386_trap(cpustate, 0, 0, 0); |
| 2862 | cpustate->ext = 1; |
| 2708 | 2863 | } |
| 2709 | 2864 | } |
| 2710 | 2865 | break; |
| r14118 | r14119 | |
| 2904 | 3059 | case 2: /* LLDT */ |
| 2905 | 3060 | if ( PROTECTED_MODE && !V8086_MODE ) |
| 2906 | 3061 | { |
| 3062 | if(cpustate->CPL) |
| 3063 | FAULT(FAULT_GP,0) |
| 2907 | 3064 | if( modrm >= 0xc0 ) { |
| 2908 | 3065 | address = LOAD_RM16(modrm); |
| 2909 | 3066 | cpustate->ldtr.segment = address; |
| r14118 | r14119 | |
| 2929 | 3086 | case 3: /* LTR */ |
| 2930 | 3087 | if ( PROTECTED_MODE && !V8086_MODE ) |
| 2931 | 3088 | { |
| 3089 | if(cpustate->CPL) |
| 3090 | FAULT(FAULT_GP,0) |
| 2932 | 3091 | if( modrm >= 0xc0 ) { |
| 2933 | 3092 | address = LOAD_RM16(modrm); |
| 2934 | 3093 | cpustate->task.segment = address; |
| r14118 | r14119 | |
| 3117 | 3276 | } |
| 3118 | 3277 | case 2: /* LGDT */ |
| 3119 | 3278 | { |
| 3279 | if(PROTECTED_MODE && cpustate->CPL) |
| 3280 | FAULT(FAULT_GP,0) |
| 3120 | 3281 | if( modrm >= 0xc0 ) { |
| 3121 | 3282 | address = LOAD_RM16(modrm); |
| 3122 | 3283 | ea = i386_translate(cpustate, CS, address ); |
| r14118 | r14119 | |
| 3130 | 3291 | } |
| 3131 | 3292 | case 3: /* LIDT */ |
| 3132 | 3293 | { |
| 3294 | if(PROTECTED_MODE && cpustate->CPL) |
| 3295 | FAULT(FAULT_GP,0) |
| 3133 | 3296 | if( modrm >= 0xc0 ) { |
| 3134 | 3297 | address = LOAD_RM16(modrm); |
| 3135 | 3298 | ea = i386_translate(cpustate, CS, address ); |
| r14118 | r14119 | |
| 3155 | 3318 | } |
| 3156 | 3319 | case 6: /* LMSW */ |
| 3157 | 3320 | { |
| 3158 | | // TODO: Check for protection fault |
| 3321 | if(PROTECTED_MODE && cpustate->CPL) |
| 3322 | FAULT(FAULT_GP,0) |
| 3159 | 3323 | UINT16 b; |
| 3160 | 3324 | if( modrm >= 0xc0 ) { |
| 3161 | 3325 | b = LOAD_RM16(modrm); |
src/emu/cpu/i386/i386.c
| r14118 | r14119 | |
| 363 | 363 | /* Must be within the relevant descriptor table limits */ |
| 364 | 364 | if(selector & 0x04) |
| 365 | 365 | { |
| 366 | | if((selector & ~0x07) > cpustate->ldtr.limit) |
| 366 | if((selector & ~0x07) >= cpustate->ldtr.limit) |
| 367 | 367 | invalid = 1; |
| 368 | 368 | } |
| 369 | 369 | else |
| 370 | 370 | { |
| 371 | | if((selector & ~0x07) > cpustate->gdtr.limit) |
| 371 | if((selector & ~0x07) >= cpustate->gdtr.limit) |
| 372 | 372 | invalid = 1; |
| 373 | 373 | } |
| 374 | 374 | |
| r14118 | r14119 | |
| 393 | 393 | } |
| 394 | 394 | } |
| 395 | 395 | |
| 396 | | #if 0 |
| 397 | | // this will be more useful once expand-down segments are supported (the FM-Towns uses these for the stack) |
| 398 | | static void i386_stack_check(i386_state *cpustate, INT16 offset) |
| 396 | static int i386_limit_check(i386_state *cpustate, int seg, UINT32 offset) |
| 399 | 397 | { |
| 400 | 398 | if(PROTECTED_MODE && !V8086_MODE) |
| 401 | 399 | { |
| 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 |
| 404 | 401 | { |
| 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 | } |
| 407 | 407 | } |
| 408 | | if(REG32(ESP) + offset > cpustate->sreg[SS].limit) |
| 408 | else |
| 409 | 409 | { |
| 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 | } |
| 412 | 415 | } |
| 413 | 416 | } |
| 417 | return 0; |
| 414 | 418 | } |
| 415 | | #endif |
| 416 | 419 | |
| 417 | 420 | static void i386_protected_mode_sreg_load(i386_state *cpustate, UINT16 selector, UINT8 reg) |
| 418 | 421 | { |
| r14118 | r14119 | |
| 431 | 434 | i386_load_protected_mode_segment(cpustate,&stack); |
| 432 | 435 | DPL = (stack.flags >> 5) & 0x03; |
| 433 | 436 | |
| 434 | | if((selector & ~0x0007) == 0) |
| 437 | if((selector & ~0x0003) == 0) |
| 435 | 438 | { |
| 436 | 439 | logerror("SReg Load (%08x): Selector is null.\n",cpustate->pc); |
| 437 | 440 | FAULT(FAULT_GP,0) |
| 438 | 441 | } |
| 439 | 442 | if(selector & 0x0004) // LDT |
| 440 | 443 | { |
| 441 | | if((selector & ~0x0007) > cpustate->ldtr.limit) |
| 444 | if((selector & ~0x0007) >= cpustate->ldtr.limit) |
| 442 | 445 | { |
| 443 | 446 | logerror("SReg Load (%08x): Selector is out of LDT bounds.\n",cpustate->pc); |
| 444 | 447 | FAULT(FAULT_GP,selector & ~0x03) |
| r14118 | r14119 | |
| 446 | 449 | } |
| 447 | 450 | else // GDT |
| 448 | 451 | { |
| 449 | | if((selector & ~0x0007) > cpustate->gdtr.limit) |
| 452 | if((selector & ~0x0007) >= cpustate->gdtr.limit) |
| 450 | 453 | { |
| 451 | 454 | logerror("SReg Load (%08x): Selector is out of GDT bounds.\n",cpustate->pc); |
| 452 | 455 | FAULT(FAULT_GP,selector & ~0x03) |
| r14118 | r14119 | |
| 477 | 480 | { |
| 478 | 481 | I386_SREG desc; |
| 479 | 482 | |
| 480 | | if((selector & ~0x0007) == 0) |
| 483 | if((selector & ~0x0003) == 0) |
| 481 | 484 | { |
| 482 | 485 | cpustate->sreg[reg].selector = selector; |
| 483 | 486 | i386_load_segment_descriptor(cpustate, reg ); |
| r14118 | r14119 | |
| 492 | 495 | |
| 493 | 496 | if(selector & 0x0004) // LDT |
| 494 | 497 | { |
| 495 | | if((selector & ~0x0007) > cpustate->ldtr.limit) |
| 498 | if((selector & ~0x0007) >= cpustate->ldtr.limit) |
| 496 | 499 | { |
| 497 | 500 | logerror("SReg Load (%08x): Selector is out of LDT bounds.\n",cpustate->pc); |
| 498 | 501 | FAULT(FAULT_GP,selector & ~0x03) |
| r14118 | r14119 | |
| 500 | 503 | } |
| 501 | 504 | else // GDT |
| 502 | 505 | { |
| 503 | | if((selector & ~0x0007) > cpustate->gdtr.limit) |
| 506 | if((selector & ~0x0007) >= cpustate->gdtr.limit) |
| 504 | 507 | { |
| 505 | 508 | logerror("SReg Load (%08x): Selector is out of GDT bounds.\n",cpustate->pc); |
| 506 | 509 | FAULT(FAULT_GP,selector & ~0x03) |
| r14118 | r14119 | |
| 607 | 610 | } |
| 608 | 611 | |
| 609 | 612 | /* segment privilege checks */ |
| 610 | | if(entry > cpustate->idtr.limit) |
| 613 | if(entry >= cpustate->idtr.limit) |
| 611 | 614 | { |
| 612 | 615 | logerror("IRQ (%08x): Vector %02xh is past IDT limit.\n",cpustate->pc,entry); |
| 613 | 616 | FAULT_EXP(FAULT_GP,entry+2) |
| r14118 | r14119 | |
| 700 | 703 | } |
| 701 | 704 | else |
| 702 | 705 | { |
| 703 | | if(segment > cpustate->gdtr.limit) |
| 706 | if(segment >= cpustate->gdtr.limit) |
| 704 | 707 | { |
| 705 | 708 | logerror("IRQ: Task gate: TSS is past GDT limit.\n"); |
| 706 | 709 | FAULT_EXP(FAULT_TS,segment & ~0x07); |
| r14118 | r14119 | |
| 739 | 742 | } |
| 740 | 743 | if(segment & 0x04) |
| 741 | 744 | { |
| 742 | | if((segment & ~0x07) > cpustate->ldtr.limit) |
| 745 | if((segment & ~0x07) >= cpustate->ldtr.limit) |
| 743 | 746 | { |
| 744 | 747 | logerror("IRQ: Gate segment is past LDT limit.\n"); |
| 745 | 748 | FAULT_EXP(FAULT_GP,(segment & 0x07)+cpustate->ext) |
| r14118 | r14119 | |
| 747 | 750 | } |
| 748 | 751 | else |
| 749 | 752 | { |
| 750 | | if((segment & ~0x07) > cpustate->gdtr.limit) |
| 753 | if((segment & ~0x07) >= cpustate->gdtr.limit) |
| 751 | 754 | { |
| 752 | 755 | logerror("IRQ: Gate segment is past GDT limit.\n"); |
| 753 | 756 | FAULT_EXP(FAULT_GP,(segment & 0x07)+cpustate->ext) |
| r14118 | r14119 | |
| 788 | 791 | } |
| 789 | 792 | if(stack.selector & 0x04) |
| 790 | 793 | { |
| 791 | | if((stack.selector & ~0x07) > cpustate->ldtr.base) |
| 794 | if((stack.selector & ~0x07) >= cpustate->ldtr.base) |
| 792 | 795 | { |
| 793 | 796 | logerror("IRQ: New stack selector is past LDT limit.\n"); |
| 794 | 797 | FAULT_EXP(FAULT_TS,(stack.selector & ~0x07)+cpustate->ext) |
| r14118 | r14119 | |
| 796 | 799 | } |
| 797 | 800 | else |
| 798 | 801 | { |
| 799 | | if((stack.selector & ~0x07) > cpustate->gdtr.base) |
| 802 | if((stack.selector & ~0x07) >= cpustate->gdtr.base) |
| 800 | 803 | { |
| 801 | 804 | logerror("IRQ: New stack selector is past GDT limit.\n"); |
| 802 | 805 | FAULT_EXP(FAULT_TS,(stack.selector & ~0x07)+cpustate->ext) |
| r14118 | r14119 | |
| 1198 | 1201 | UINT32 offset = off; |
| 1199 | 1202 | |
| 1200 | 1203 | /* Check selector is not null */ |
| 1201 | | if((segment & ~0x07) == 0) |
| 1204 | if((segment & ~0x03) == 0) |
| 1202 | 1205 | { |
| 1203 | 1206 | logerror("JMP: Segment is null.\n"); |
| 1204 | 1207 | FAULT(FAULT_GP,0) |
| r14118 | r14119 | |
| 1207 | 1210 | if((segment & 0x04) == 0) |
| 1208 | 1211 | { |
| 1209 | 1212 | /* check GDT limit */ |
| 1210 | | if((segment & ~0x07) > (cpustate->gdtr.limit)) |
| 1213 | if((segment & ~0x07) >= (cpustate->gdtr.limit)) |
| 1211 | 1214 | { |
| 1212 | 1215 | logerror("JMP: Segment is past GDT limit.\n"); |
| 1213 | 1216 | FAULT(FAULT_GP,segment & 0xfffc) |
| r14118 | r14119 | |
| 1216 | 1219 | else |
| 1217 | 1220 | { |
| 1218 | 1221 | /* check LDT limit */ |
| 1219 | | if((segment & ~0x07) > (cpustate->ldtr.limit)) |
| 1222 | if((segment & ~0x07) >= (cpustate->ldtr.limit)) |
| 1220 | 1223 | { |
| 1221 | 1224 | logerror("JMP: Segment is past LDT limit.\n"); |
| 1222 | 1225 | FAULT(FAULT_GP,segment & 0xfffc) |
| r14118 | r14119 | |
| 1474 | 1477 | UINT32 offset = off; |
| 1475 | 1478 | int x; |
| 1476 | 1479 | |
| 1477 | | if((selector & ~0x07) == 0) |
| 1480 | if((selector & ~0x03) == 0) |
| 1478 | 1481 | { |
| 1479 | 1482 | logerror("CALL (%08x): Selector is null.\n",cpustate->pc); |
| 1480 | 1483 | FAULT(FAULT_GP,0) // #GP(0) |
| 1481 | 1484 | } |
| 1482 | 1485 | if(selector & 0x04) |
| 1483 | 1486 | { |
| 1484 | | if((selector & ~0x07) > cpustate->ldtr.limit) |
| 1487 | if((selector & ~0x07) >= cpustate->ldtr.limit) |
| 1485 | 1488 | { |
| 1486 | 1489 | logerror("CALL: Selector is past LDT limit.\n"); |
| 1487 | 1490 | FAULT(FAULT_GP,selector & ~0x03) // #GP(selector) |
| r14118 | r14119 | |
| 1489 | 1492 | } |
| 1490 | 1493 | else |
| 1491 | 1494 | { |
| 1492 | | if((selector & ~0x07) > cpustate->gdtr.limit) |
| 1495 | if((selector & ~0x07) >= cpustate->gdtr.limit) |
| 1493 | 1496 | { |
| 1494 | 1497 | logerror("CALL: Selector is past GDT limit.\n"); |
| 1495 | 1498 | FAULT(FAULT_GP,selector & ~0x03) // #GP(selector) |
| r14118 | r14119 | |
| 1942 | 1945 | if(RPL == CPL) |
| 1943 | 1946 | { |
| 1944 | 1947 | /* same privilege level */ |
| 1945 | | if((newCS & ~0x07) == 0) |
| 1948 | if((newCS & ~0x03) == 0) |
| 1946 | 1949 | { |
| 1947 | 1950 | logerror("RETF: Return segment is null.\n"); |
| 1948 | 1951 | FAULT(FAULT_GP,0) |
| 1949 | 1952 | } |
| 1950 | 1953 | if(newCS & 0x04) |
| 1951 | 1954 | { |
| 1952 | | if((newCS & ~0x07) > cpustate->ldtr.limit) |
| 1955 | if((newCS & ~0x07) >= cpustate->ldtr.limit) |
| 1953 | 1956 | { |
| 1954 | 1957 | logerror("RETF: Return segment is past LDT limit.\n"); |
| 1955 | 1958 | FAULT(FAULT_GP,newCS & ~0x03) |
| r14118 | r14119 | |
| 1957 | 1960 | } |
| 1958 | 1961 | else |
| 1959 | 1962 | { |
| 1960 | | if((newCS & ~0x07) > cpustate->gdtr.limit) |
| 1963 | if((newCS & ~0x07) >= cpustate->gdtr.limit) |
| 1961 | 1964 | { |
| 1962 | 1965 | logerror("RETF: Return segment is past GDT limit.\n"); |
| 1963 | 1966 | FAULT(FAULT_GP,newCS & ~0x03) |
| r14118 | r14119 | |
| 1996 | 1999 | } |
| 1997 | 2000 | if(operand32 == 0) |
| 1998 | 2001 | { |
| 1999 | | if(REG16(SP) > (cpustate->sreg[SS].limit & 0xffff)+1) |
| 2002 | if(i386_limit_check(cpustate,SS,REG16(SP)+count+3) != 0) |
| 2000 | 2003 | { |
| 2001 | 2004 | logerror("RETF (%08x): SP is past stack segment limit.\n",cpustate->pc); |
| 2002 | 2005 | FAULT(FAULT_SS,0) |
| r14118 | r14119 | |
| 2004 | 2007 | } |
| 2005 | 2008 | else |
| 2006 | 2009 | { |
| 2007 | | if(REG32(ESP) > cpustate->sreg[SS].limit+1) |
| 2010 | if(i386_limit_check(cpustate,SS,REG32(ESP)+count+7) != 0) |
| 2008 | 2011 | { |
| 2009 | 2012 | logerror("RETF: ESP is past stack segment limit.\n"); |
| 2010 | 2013 | FAULT(FAULT_SS,0) |
| r14118 | r14119 | |
| 2020 | 2023 | /* outer privilege level */ |
| 2021 | 2024 | if(operand32 == 0) |
| 2022 | 2025 | { |
| 2023 | | if(REG16(SP)+8+count > cpustate->sreg[SS].limit+1) |
| 2026 | if(i386_limit_check(cpustate,SS,REG16(SP)+count+7) != 0) |
| 2024 | 2027 | { |
| 2025 | 2028 | logerror("RETF (%08x): SP is past stack segment limit.\n",cpustate->pc); |
| 2026 | 2029 | FAULT(FAULT_SS,0) |
| r14118 | r14119 | |
| 2028 | 2031 | } |
| 2029 | 2032 | else |
| 2030 | 2033 | { |
| 2031 | | if(REG32(ESP)+16+count > cpustate->sreg[SS].limit+1) |
| 2034 | if(i386_limit_check(cpustate,SS,REG32(SP)+count+15) != 0) |
| 2032 | 2035 | { |
| 2033 | 2036 | logerror("RETF: ESP is past stack segment limit.\n"); |
| 2034 | 2037 | FAULT(FAULT_SS,0) |
| r14118 | r14119 | |
| 2036 | 2039 | } |
| 2037 | 2040 | |
| 2038 | 2041 | /* Check CS selector and descriptor */ |
| 2039 | | if((newCS & ~0x07) == 0) |
| 2042 | if((newCS & ~0x03) == 0) |
| 2040 | 2043 | { |
| 2041 | 2044 | logerror("RETF: CS segment is null.\n"); |
| 2042 | 2045 | FAULT(FAULT_GP,0) |
| 2043 | 2046 | } |
| 2044 | 2047 | if(newCS & 0x04) |
| 2045 | 2048 | { |
| 2046 | | if((newCS & ~0x07) > cpustate->ldtr.limit) |
| 2049 | if((newCS & ~0x07) >= cpustate->ldtr.limit) |
| 2047 | 2050 | { |
| 2048 | 2051 | logerror("RETF: CS segment selector is past LDT limit.\n"); |
| 2049 | 2052 | FAULT(FAULT_GP,newCS & ~0x07) |
| r14118 | r14119 | |
| 2051 | 2054 | } |
| 2052 | 2055 | else |
| 2053 | 2056 | { |
| 2054 | | if((newCS & ~0x07) > cpustate->gdtr.limit) |
| 2057 | if((newCS & ~0x07) >= cpustate->gdtr.limit) |
| 2055 | 2058 | { |
| 2056 | 2059 | logerror("RETF: CS segment selector is past GDT limit.\n"); |
| 2057 | 2060 | FAULT(FAULT_GP,newCS & ~0x07) |
| r14118 | r14119 | |
| 2232 | 2235 | logerror("IRET: Task return: Back-linked TSS is not in GDT.\n"); |
| 2233 | 2236 | FAULT(FAULT_TS,task & ~0x07) |
| 2234 | 2237 | } |
| 2235 | | if((task & ~0x07) > cpustate->gdtr.limit) |
| 2238 | if((task & ~0x07) >= cpustate->gdtr.limit) |
| 2236 | 2239 | { |
| 2237 | 2240 | logerror("IRET: Task return: Back-linked TSS is not in GDT.\n"); |
| 2238 | 2241 | FAULT(FAULT_TS,task & ~0x07) |
| r14118 | r14119 | |
| 2408 | 2411 | { |
| 2409 | 2412 | if(operand32 == 0) |
| 2410 | 2413 | { |
| 2411 | | if(REG16(SP)+4 > cpustate->sreg[SS].limit+1) |
| 2414 | if(i386_limit_check(cpustate,SS,REG16(SP)+3) != 0) |
| 2412 | 2415 | { |
| 2413 | 2416 | logerror("IRET: Data on stack is past SS limit.\n"); |
| 2414 | 2417 | FAULT(FAULT_SS,0) |
| r14118 | r14119 | |
| 2416 | 2419 | } |
| 2417 | 2420 | else |
| 2418 | 2421 | { |
| 2419 | | if(REG32(ESP)+6 > cpustate->sreg[SS].limit+1) |
| 2422 | if(i386_limit_check(cpustate,SS,REG32(ESP)+7) != 0) |
| 2420 | 2423 | { |
| 2421 | 2424 | logerror("IRET: Data on stack is past SS limit.\n"); |
| 2422 | 2425 | FAULT(FAULT_SS,0) |
| r14118 | r14119 | |
| 2433 | 2436 | /* return to same privilege level */ |
| 2434 | 2437 | if(operand32 == 0) |
| 2435 | 2438 | { |
| 2436 | | if(REG16(SP)+6 > cpustate->sreg[SS].limit+1) |
| 2439 | if(i386_limit_check(cpustate,SS,REG16(SP)+5) != 0) |
| 2437 | 2440 | { |
| 2438 | 2441 | logerror("IRET (%08x): Data on stack is past SS limit.\n",cpustate->pc); |
| 2439 | 2442 | FAULT(FAULT_SS,0) |
| r14118 | r14119 | |
| 2441 | 2444 | } |
| 2442 | 2445 | else |
| 2443 | 2446 | { |
| 2444 | | if(REG32(ESP)+12 > cpustate->sreg[SS].limit+1) |
| 2447 | if(i386_limit_check(cpustate,SS,REG32(ESP)+11) != 0) |
| 2445 | 2448 | { |
| 2446 | 2449 | logerror("IRET (%08x): Data on stack is past SS limit.\n",cpustate->pc); |
| 2447 | 2450 | FAULT(FAULT_SS,0) |
| 2448 | 2451 | } |
| 2449 | 2452 | } |
| 2450 | | if((newCS & ~0x07) == 0) |
| 2453 | if((newCS & ~0x03) == 0) |
| 2451 | 2454 | { |
| 2452 | 2455 | logerror("IRET: Return CS selector is null.\n"); |
| 2453 | 2456 | FAULT(FAULT_GP,0) |
| 2454 | 2457 | } |
| 2455 | 2458 | if(newCS & 0x04) |
| 2456 | 2459 | { |
| 2457 | | if((newCS & ~0x07) > cpustate->ldtr.limit) |
| 2460 | if((newCS & ~0x07) >= cpustate->ldtr.limit) |
| 2458 | 2461 | { |
| 2459 | 2462 | logerror("IRET: Return CS selector (%04x) is past LDT limit.\n",newCS); |
| 2460 | 2463 | FAULT(FAULT_GP,newCS & ~0x07) |
| r14118 | r14119 | |
| 2462 | 2465 | } |
| 2463 | 2466 | else |
| 2464 | 2467 | { |
| 2465 | | if((newCS & ~0x07) > cpustate->gdtr.limit) |
| 2468 | if((newCS & ~0x07) >= cpustate->gdtr.limit) |
| 2466 | 2469 | { |
| 2467 | 2470 | logerror("IRET: Return CS selector is past GDT limit.\n"); |
| 2468 | 2471 | FAULT(FAULT_GP,newCS & ~0x07) |
| r14118 | r14119 | |
| 2533 | 2536 | i386_load_protected_mode_segment(cpustate,&stack); |
| 2534 | 2537 | if(operand32 == 0) |
| 2535 | 2538 | { |
| 2536 | | if(REG16(SP)+10 > cpustate->sreg[SS].limit+1) |
| 2539 | if(i386_limit_check(cpustate,SS,REG16(SP)+9) != 0) |
| 2537 | 2540 | { |
| 2538 | 2541 | logerror("IRET: SP is past SS limit.\n"); |
| 2539 | | FAULT(FAULT_SS,0) } |
| 2542 | FAULT(FAULT_SS,0) |
| 2543 | } |
| 2540 | 2544 | } |
| 2541 | 2545 | else |
| 2542 | 2546 | { |
| 2543 | | if(REG32(ESP)+20 > cpustate->sreg[SS].limit+1) |
| 2547 | if(i386_limit_check(cpustate,SS,REG32(ESP)+19) != 0) |
| 2544 | 2548 | { |
| 2545 | 2549 | logerror("IRET: ESP is past SS limit.\n"); |
| 2546 | 2550 | FAULT(FAULT_SS,0) |
| 2547 | 2551 | } |
| 2548 | 2552 | } |
| 2549 | 2553 | /* Check CS selector and descriptor */ |
| 2550 | | if((newCS & ~0x07) == 0) |
| 2554 | if((newCS & ~0x03) == 0) |
| 2551 | 2555 | { |
| 2552 | 2556 | logerror("IRET: Return CS selector is null.\n"); |
| 2553 | 2557 | FAULT(FAULT_GP,0) |
| 2554 | 2558 | } |
| 2555 | 2559 | if(newCS & 0x04) |
| 2556 | 2560 | { |
| 2557 | | if((newCS & ~0x07) > cpustate->ldtr.limit) |
| 2561 | if((newCS & ~0x07) >= cpustate->ldtr.limit) |
| 2558 | 2562 | { |
| 2559 | 2563 | logerror("IRET: Return CS selector is past LDT limit.\n"); |
| 2560 | 2564 | FAULT(FAULT_GP,newCS & ~0x07); |
| r14118 | r14119 | |
| 2562 | 2566 | } |
| 2563 | 2567 | else |
| 2564 | 2568 | { |
| 2565 | | if((newCS & ~0x07) > cpustate->gdtr.limit) |
| 2569 | if((newCS & ~0x07) >= cpustate->gdtr.limit) |
| 2566 | 2570 | { |
| 2567 | 2571 | logerror("IRET: Return CS selector is past GDT limit.\n"); |
| 2568 | 2572 | FAULT(FAULT_GP,newCS & ~0x07); |
| r14118 | r14119 | |
| 2597 | 2601 | |
| 2598 | 2602 | /* Check SS selector and descriptor */ |
| 2599 | 2603 | DPL = (stack.flags >> 5) & 0x03; |
| 2600 | | if((newSS & ~0x07) == 0) |
| 2604 | if((newSS & ~0x03) == 0) |
| 2601 | 2605 | { |
| 2602 | 2606 | logerror("IRET: Return SS selector is null.\n"); |
| 2603 | 2607 | FAULT(FAULT_GP,0) |
| 2604 | 2608 | } |
| 2605 | 2609 | if(newSS & 0x04) |
| 2606 | 2610 | { |
| 2607 | | if((newSS & ~0x07) > cpustate->ldtr.limit) |
| 2611 | if((newSS & ~0x07) >= cpustate->ldtr.limit) |
| 2608 | 2612 | { |
| 2609 | 2613 | logerror("IRET: Return SS selector is past LDT limit.\n"); |
| 2610 | 2614 | FAULT(FAULT_GP,newSS & ~0x07); |
| r14118 | r14119 | |
| 2612 | 2616 | } |
| 2613 | 2617 | else |
| 2614 | 2618 | { |
| 2615 | | if((newSS & ~0x07) > cpustate->gdtr.limit) |
| 2619 | if((newSS & ~0x07) >= cpustate->gdtr.limit) |
| 2616 | 2620 | { |
| 2617 | 2621 | logerror("IRET: Return SS selector is past GDT limit.\n"); |
| 2618 | 2622 | FAULT(FAULT_GP,newSS & ~0x07); |
src/emu/cpu/i386/i386op32.c
| r14118 | r14119 | |
| 1361 | 1361 | |
| 1362 | 1362 | static void I386OP(pop_eax)(i386_state *cpustate) // Opcode 0x58 |
| 1363 | 1363 | { |
| 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) |
| 1365 | 1369 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1366 | 1370 | } |
| 1367 | 1371 | |
| 1368 | 1372 | static void I386OP(pop_ecx)(i386_state *cpustate) // Opcode 0x59 |
| 1369 | 1373 | { |
| 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) |
| 1371 | 1379 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1372 | 1380 | } |
| 1373 | 1381 | |
| 1374 | 1382 | static void I386OP(pop_edx)(i386_state *cpustate) // Opcode 0x5a |
| 1375 | 1383 | { |
| 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) |
| 1377 | 1389 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1378 | 1390 | } |
| 1379 | 1391 | |
| 1380 | 1392 | static void I386OP(pop_ebx)(i386_state *cpustate) // Opcode 0x5b |
| 1381 | 1393 | { |
| 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) |
| 1383 | 1399 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1384 | 1400 | } |
| 1385 | 1401 | |
| 1386 | 1402 | static void I386OP(pop_esp)(i386_state *cpustate) // Opcode 0x5c |
| 1387 | 1403 | { |
| 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) |
| 1389 | 1409 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1390 | 1410 | } |
| 1391 | 1411 | |
| 1392 | 1412 | static void I386OP(pop_ebp)(i386_state *cpustate) // Opcode 0x5d |
| 1393 | 1413 | { |
| 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) |
| 1395 | 1419 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1396 | 1420 | } |
| 1397 | 1421 | |
| 1398 | 1422 | static void I386OP(pop_esi)(i386_state *cpustate) // Opcode 0x5e |
| 1399 | 1423 | { |
| 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) |
| 1401 | 1429 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1402 | 1430 | } |
| 1403 | 1431 | |
| 1404 | 1432 | static void I386OP(pop_edi)(i386_state *cpustate) // Opcode 0x5f |
| 1405 | 1433 | { |
| 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) |
| 1407 | 1439 | CYCLES(cpustate,CYCLES_POP_REG_SHORT); |
| 1408 | 1440 | } |
| 1409 | 1441 | |
| 1410 | 1442 | static void I386OP(pop_ds32)(i386_state *cpustate) // Opcode 0x1f |
| 1411 | 1443 | { |
| 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 | } |
| 1417 | 1453 | } |
| 1454 | else |
| 1455 | FAULT(FAULT_SS,0) |
| 1418 | 1456 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1419 | 1457 | } |
| 1420 | 1458 | |
| 1421 | 1459 | static void I386OP(pop_es32)(i386_state *cpustate) // Opcode 0x07 |
| 1422 | 1460 | { |
| 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 | } |
| 1428 | 1470 | } |
| 1471 | else |
| 1472 | FAULT(FAULT_SS,0) |
| 1429 | 1473 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1430 | 1474 | } |
| 1431 | 1475 | |
| 1432 | 1476 | static void I386OP(pop_fs32)(i386_state *cpustate) // Opcode 0x0f a1 |
| 1433 | 1477 | { |
| 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 | } |
| 1439 | 1487 | } |
| 1488 | else |
| 1489 | FAULT(FAULT_SS,0) |
| 1440 | 1490 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1441 | 1491 | } |
| 1442 | 1492 | |
| 1443 | 1493 | static void I386OP(pop_gs32)(i386_state *cpustate) // Opcode 0x0f a9 |
| 1444 | 1494 | { |
| 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 | } |
| 1450 | 1504 | } |
| 1505 | else |
| 1506 | FAULT(FAULT_SS,0) |
| 1451 | 1507 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1452 | 1508 | } |
| 1453 | 1509 | |
| 1454 | 1510 | static void I386OP(pop_ss32)(i386_state *cpustate) // Opcode 0x17 |
| 1455 | 1511 | { |
| 1512 | UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP)); |
| 1456 | 1513 | if(cpustate->IF != 0) // if external interrupts are enabled |
| 1457 | 1514 | { |
| 1458 | 1515 | cpustate->IF = 0; // reset IF for the next instruction |
| 1459 | 1516 | cpustate->delayed_interrupt_enable = 1; |
| 1460 | 1517 | } |
| 1461 | 1518 | |
| 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 | } |
| 1467 | 1527 | } |
| 1528 | else |
| 1529 | FAULT(FAULT_SS,0) |
| 1468 | 1530 | CYCLES(cpustate,CYCLES_POP_SREG); |
| 1469 | 1531 | } |
| 1470 | 1532 | |
| r14118 | r14119 | |
| 1472 | 1534 | { |
| 1473 | 1535 | UINT8 modrm = FETCH(cpustate); |
| 1474 | 1536 | 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); |
| 1475 | 1541 | |
| 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 | } |
| 1483 | 1548 | } |
| 1549 | else |
| 1550 | FAULT(FAULT_SS,0) |
| 1484 | 1551 | CYCLES(cpustate,CYCLES_POP_RM); |
| 1485 | 1552 | } |
| 1486 | 1553 | |
| 1487 | 1554 | static void I386OP(popad)(i386_state *cpustate) // Opcode 0x61 |
| 1488 | 1555 | { |
| 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) |
| 1497 | 1570 | CYCLES(cpustate,CYCLES_POPA); |
| 1498 | 1571 | } |
| 1499 | 1572 | |
| r14118 | r14119 | |
| 1503 | 1576 | UINT32 current = get_flags(cpustate); |
| 1504 | 1577 | UINT8 IOPL = (current >> 12) & 0x03; |
| 1505 | 1578 | UINT32 mask = 0x00257fd5; // VM, VIP and VIF cannot be set by POPF/POPFD |
| 1579 | UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP)); |
| 1506 | 1580 | |
| 1507 | 1581 | // IOPL can only change if CPL is 0 |
| 1508 | 1582 | if(cpustate->CPL != 0) |
| r14118 | r14119 | |
| 1522 | 1596 | mask &= ~0x00003000; // IOPL cannot be changed while in V8086 mode |
| 1523 | 1597 | } |
| 1524 | 1598 | |
| 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) |
| 1528 | 1607 | CYCLES(cpustate,CYCLES_POPF); |
| 1529 | 1608 | } |
| 1530 | 1609 | |
| 1531 | 1610 | static void I386OP(push_eax)(i386_state *cpustate) // Opcode 0x50 |
| 1532 | 1611 | { |
| 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) |
| 1534 | 1617 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1535 | 1618 | } |
| 1536 | 1619 | |
| 1537 | 1620 | static void I386OP(push_ecx)(i386_state *cpustate) // Opcode 0x51 |
| 1538 | 1621 | { |
| 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) |
| 1540 | 1627 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1541 | 1628 | } |
| 1542 | 1629 | |
| 1543 | 1630 | static void I386OP(push_edx)(i386_state *cpustate) // Opcode 0x52 |
| 1544 | 1631 | { |
| 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) |
| 1546 | 1637 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1547 | 1638 | } |
| 1548 | 1639 | |
| 1549 | 1640 | static void I386OP(push_ebx)(i386_state *cpustate) // Opcode 0x53 |
| 1550 | 1641 | { |
| 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) |
| 1552 | 1647 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1553 | 1648 | } |
| 1554 | 1649 | |
| 1555 | 1650 | static void I386OP(push_esp)(i386_state *cpustate) // Opcode 0x54 |
| 1556 | 1651 | { |
| 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) |
| 1558 | 1657 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1559 | 1658 | } |
| 1560 | 1659 | |
| 1561 | 1660 | static void I386OP(push_ebp)(i386_state *cpustate) // Opcode 0x55 |
| 1562 | 1661 | { |
| 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) |
| 1564 | 1667 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1565 | 1668 | } |
| 1566 | 1669 | |
| 1567 | 1670 | static void I386OP(push_esi)(i386_state *cpustate) // Opcode 0x56 |
| 1568 | 1671 | { |
| 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) |
| 1570 | 1677 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1571 | 1678 | } |
| 1572 | 1679 | |
| 1573 | 1680 | static void I386OP(push_edi)(i386_state *cpustate) // Opcode 0x57 |
| 1574 | 1681 | { |
| 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) |
| 1576 | 1687 | CYCLES(cpustate,CYCLES_PUSH_REG_SHORT); |
| 1577 | 1688 | } |
| 1578 | 1689 | |
| 1579 | 1690 | static void I386OP(push_cs32)(i386_state *cpustate) // Opcode 0x0e |
| 1580 | 1691 | { |
| 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) |
| 1582 | 1697 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1583 | 1698 | } |
| 1584 | 1699 | |
| 1585 | 1700 | static void I386OP(push_ds32)(i386_state *cpustate) // Opcode 0x1e |
| 1586 | 1701 | { |
| 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) |
| 1588 | 1707 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1589 | 1708 | } |
| 1590 | 1709 | |
| 1591 | 1710 | static void I386OP(push_es32)(i386_state *cpustate) // Opcode 0x06 |
| 1592 | 1711 | { |
| 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) |
| 1594 | 1717 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1595 | 1718 | } |
| 1596 | 1719 | |
| 1597 | 1720 | static void I386OP(push_fs32)(i386_state *cpustate) // Opcode 0x0f a0 |
| 1598 | 1721 | { |
| 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) |
| 1600 | 1727 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1601 | 1728 | } |
| 1602 | 1729 | |
| 1603 | 1730 | static void I386OP(push_gs32)(i386_state *cpustate) // Opcode 0x0f a8 |
| 1604 | 1731 | { |
| 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) |
| 1606 | 1737 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1607 | 1738 | } |
| 1608 | 1739 | |
| 1609 | 1740 | static void I386OP(push_ss32)(i386_state *cpustate) // Opcode 0x16 |
| 1610 | 1741 | { |
| 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) |
| 1612 | 1747 | CYCLES(cpustate,CYCLES_PUSH_SREG); |
| 1613 | 1748 | } |
| 1614 | 1749 | |
| 1615 | 1750 | static void I386OP(push_i32)(i386_state *cpustate) // Opcode 0x68 |
| 1616 | 1751 | { |
| 1617 | 1752 | 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) |
| 1619 | 1758 | CYCLES(cpustate,CYCLES_PUSH_IMM); |
| 1620 | 1759 | } |
| 1621 | 1760 | |
| 1622 | 1761 | static void I386OP(pushad)(i386_state *cpustate) // Opcode 0x60 |
| 1623 | 1762 | { |
| 1624 | 1763 | 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) |
| 1633 | 1778 | CYCLES(cpustate,CYCLES_PUSHA); |
| 1634 | 1779 | } |
| 1635 | 1780 | |
| 1636 | 1781 | static void I386OP(pushfd)(i386_state *cpustate) // Opcode 0x9c |
| 1637 | 1782 | { |
| 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) |
| 1639 | 1788 | CYCLES(cpustate,CYCLES_PUSHF); |
| 1640 | 1789 | } |
| 1641 | 1790 | |
| r14118 | r14119 | |
| 2021 | 2170 | UINT32 ea = GetEA(cpustate,modrm); |
| 2022 | 2171 | UINT32 src = READ32(cpustate,ea); |
| 2023 | 2172 | UINT32 dst = LOAD_REG32(modrm); |
| 2173 | WRITE32(cpustate,ea, dst); |
| 2024 | 2174 | STORE_REG32(modrm, src); |
| 2025 | | WRITE32(cpustate,ea, dst); |
| 2026 | 2175 | CYCLES(cpustate,CYCLES_XCHG_REG_MEM); |
| 2027 | 2176 | } |
| 2028 | 2177 | } |
| r14118 | r14119 | |
| 2526 | 2675 | REG32(EAX) = (UINT32)result; |
| 2527 | 2676 | } |
| 2528 | 2677 | } else { |
| 2529 | | /* TODO: Divide by zero */ |
| 2678 | cpustate->ext = 0; |
| 2679 | i386_trap(cpustate, 0, 0, 0); |
| 2680 | cpustate->ext = 1; |
| 2530 | 2681 | } |
| 2531 | 2682 | } |
| 2532 | 2683 | break; |
| r14118 | r14119 | |
| 2554 | 2705 | REG32(EAX) = (UINT32)result; |
| 2555 | 2706 | } |
| 2556 | 2707 | } else { |
| 2557 | | /* TODO: Divide by zero */ |
| 2708 | cpustate->ext = 0; |
| 2709 | i386_trap(cpustate, 0, 0, 0); |
| 2710 | cpustate->ext = 1; |
| 2558 | 2711 | } |
| 2559 | 2712 | } |
| 2560 | 2713 | break; |
| r14118 | r14119 | |
| 2753 | 2906 | case 2: /* LLDT */ |
| 2754 | 2907 | if ( PROTECTED_MODE && !V8086_MODE ) |
| 2755 | 2908 | { |
| 2909 | if(cpustate->CPL) |
| 2910 | FAULT(FAULT_GP,0) |
| 2756 | 2911 | if( modrm >= 0xc0 ) { |
| 2757 | 2912 | address = LOAD_RM32(modrm); |
| 2758 | 2913 | cpustate->ldtr.segment = address; |
| r14118 | r14119 | |
| 2778 | 2933 | case 3: /* LTR */ |
| 2779 | 2934 | if ( PROTECTED_MODE && !V8086_MODE ) |
| 2780 | 2935 | { |
| 2936 | if(cpustate->CPL) |
| 2937 | FAULT(FAULT_GP,0) |
| 2781 | 2938 | if( modrm >= 0xc0 ) { |
| 2782 | 2939 | address = LOAD_RM32(modrm); |
| 2783 | 2940 | cpustate->task.segment = address; |
| r14118 | r14119 | |
| 2965 | 3122 | } |
| 2966 | 3123 | case 2: /* LGDT */ |
| 2967 | 3124 | { |
| 3125 | if(PROTECTED_MODE && cpustate->CPL) |
| 3126 | FAULT(FAULT_GP,0) |
| 2968 | 3127 | if( modrm >= 0xc0 ) { |
| 2969 | 3128 | address = LOAD_RM32(modrm); |
| 2970 | 3129 | ea = i386_translate(cpustate, CS, address ); |
| r14118 | r14119 | |
| 2978 | 3137 | } |
| 2979 | 3138 | case 3: /* LIDT */ |
| 2980 | 3139 | { |
| 3140 | if(PROTECTED_MODE && cpustate->CPL) |
| 3141 | FAULT(FAULT_GP,0) |
| 2981 | 3142 | if( modrm >= 0xc0 ) { |
| 2982 | 3143 | address = LOAD_RM32(modrm); |
| 2983 | 3144 | ea = i386_translate(cpustate, CS, address ); |