1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
|
//
// /home/ms/source/sidplay/libsidplay/emu/RCS/6510_.cpp,v
//
// --------------------------------------------------------------------------
// Copyright (c) 1994-1997 Michael Schwendt. All rights reserved.
//
// Redistribution and use in source and binary forms, either unchanged or
// modified, are permitted provided that the following conditions are met:
//
// (1) Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// (2) Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// --------------------------------------------------------------------------
//
// MOS-6510 Interpreter. Known bugs, missing features, incompatibilities:
//
// - No support for code execution in Basic-ROM/Kernal-ROM.
// Only execution of code in RAM is allowed.
// - Probably inconsistent emulation of illegal instructions part 3.
// - No detection of deadlocks.
// - Faked RTI (= RTS).
// - Anybody knows, whether it is ``Kernel'' instead of ``Kernal'' ?
// Perhaps it is a proper name invented by CBM ?
// It is spelled ``Kernal'' in nearly every C64 documentation !
//
#include "6510_.h"
#include "myendian.h"
#include "emucfg.h"
ubyte* c64mem1 = 0; // 64KB C64-RAM
ubyte* c64mem2 = 0; // Basic-ROM, VIC, SID, I/O, Kernal-ROM
bool sidKeysOff[32]; // key_off detection
bool sidKeysOn[32]; // key_on detection
ubyte sidLastValue = 0; // last value written to the SID
ubyte optr3readWave = 0; // D41B
ubyte optr3readEnve = 0; // D41C
// --------------------------------------------------------------------------
static ubyte AC, XR, YR; // 6510 processor registers
static uword PC, SP; // program-counter, stack-pointer
// PC is only used temporarily !
// The current program-counter is pPC-pPCbase
static ubyte* pPCbase; // pointer to RAM/ROM buffer base
static ubyte* pPCend; // pointer to RAM/ROM buffer end
static ubyte* pPC; // pointer to PC location
// ---------------------------------------------------- Memory de-/allocation
static ubyte* c64ramBuf = 0;
static ubyte* c64romBuf = 0;
bool c64memFree()
{
if ( c64romBuf != 0 )
{
delete[] c64romBuf;
c64romBuf = (c64mem2 = 0);
}
if ( c64ramBuf != 0 )
{
delete[] c64ramBuf;
c64ramBuf = (c64mem1 = 0);
}
return true;
}
bool c64memAlloc()
{
c64memFree();
bool wasSuccess = true;
if (( c64ramBuf = new ubyte[65536+256] ) == 0 )
{
wasSuccess = false;
}
if (( c64romBuf = new ubyte[65536+256] ) == 0 )
{
wasSuccess = false;
}
if (!wasSuccess)
{
c64memFree();
}
else
{
// Make the memory buffers accessible to the whole emulator engine.
c64mem1 = c64ramBuf;
c64mem2 = c64romBuf;
}
return wasSuccess;
}
// ------------------------------------------------------ (S)tatus (R)egister
// MOS-6510 SR: NV-BDIZC
// 76543210
//
struct statusRegister
{
unsigned Carry : 1;
unsigned Zero : 1;
unsigned Interrupt : 1;
unsigned Decimal : 1;
unsigned Break : 1;
unsigned NotUsed : 1;
unsigned oVerflow : 1;
unsigned Negative : 1;
};
static statusRegister SR;
// Some handy defines to ease SR access.
#define CF SR.Carry
#define ZF SR.Zero
#define IF SR.Interrupt
#define DF SR.Decimal
#define BF SR.Break
#define NU SR.NotUsed
#define VF SR.oVerflow
#define NF SR.Negative
inline void affectNZ(ubyte reg)
{
ZF = (reg == 0);
NF = ((reg & 0x80) != 0);
}
inline void resetSR()
{
// Explicit paranthesis looks great.
CF = (ZF = (IF = (DF = (BF = (VF = (NF = 0))))));
NU = 1;
}
inline ubyte codeSR()
{
register ubyte tempSR = CF;
tempSR |= (ZF<<1);
tempSR |= (IF<<2);
tempSR |= (DF<<3);
tempSR |= (BF<<4);
tempSR |= (NU<<5);
tempSR |= (VF<<6);
tempSR |= (NF<<7);
return tempSR;
}
inline void decodeSR(ubyte stackByte)
{
CF = (stackByte & 1);
ZF = ((stackByte & 2) !=0 );
IF = ((stackByte & 4) !=0 );
DF = ((stackByte & 8) !=0 );
BF = ((stackByte & 16) !=0 );
NU = 1; // if used or writable, ((stackByte & 32) !=0 );
VF = ((stackByte & 64) !=0 );
NF = ((stackByte & 128) !=0 );
}
// --------------------------------------------------------------------------
// Handling conditional branches.
inline void branchIfClear(ubyte flag)
{
if (flag == 0)
{
PC = pPC-pPCbase; // calculate 16-bit PC
PC += (sbyte)*pPC; // add offset, keep it 16-bit (uword)
pPC = pPCbase+PC; // calc new pointer-PC
}
pPC++;
}
inline void branchIfSet(ubyte flag)
{
if (flag != 0)
{
PC = pPC-pPCbase; // calculate 16-bit PC
PC += (sbyte)*pPC; // add offset, keep it 16-bit (uword)
pPC = pPCbase+PC; // calc new pointer-PC
}
pPC++;
}
// --------------------------------------------------------------------------
// Addressing modes:
// Calculating 8/16-bit effective addresses out of data operands.
inline uword abso()
{
return readLEword(pPC);
}
inline uword absx()
{
return readLEword(pPC)+XR;
}
inline uword absy()
{
return readLEword(pPC)+YR;
}
inline ubyte imm()
{
return *pPC;
}
inline uword indx()
{
return readEndian(c64mem1[(*pPC+1+XR)&0xFF],c64mem1[(*pPC+XR)&0xFF]);
}
inline uword indy()
{
return YR+readEndian(c64mem1[(*pPC+1)&0xFF],c64mem1[*pPC]);
}
inline ubyte zp()
{
return *pPC;
}
inline ubyte zpx()
{
return *pPC+XR;
}
inline ubyte zpy()
{
return *pPC+YR;
}
// --------------------------------------------------------------------------
// LIFO-Stack:
//
// |xxxxxx|
// |xxxxxx|
// |______|<- SP <= (hi)-return-address
// |______| <= (lo)
// |______|
//
static bool stackIsOkay = true;
inline void resetSP()
{
SP = 0x1ff; // SP to top of stack
stackIsOkay = true;
}
inline void checkSP()
{
stackIsOkay = ((SP>0xff)&&(SP<=0x1ff)); // check boundaries
}
inline void RTS_()
{
SP++;
PC =readEndian( c64mem1[SP +1], c64mem1[SP] ) +1;
pPC = pPCbase+PC;
SP++;
checkSP();
}
// --------------------------------------------------------------------------
// Relevant configurable memory banks:
//
// $A000 to $BFFF = RAM, Basic-ROM
// $C000 to $CFFF = RAM
// $D000 to $DFFF = RAM, I/O, Char-ROM
// $E000 to $FFFF = RAM, Kernal-ROM
//
// Bank-Select Register $01:
//
// Bits
// 210 $A000-$BFFF $D000-$DFFF $E000-$FFFF
// ------------------------------------------------
// 000 RAM RAM RAM
// 001 RAM Char-ROM RAM
// 010 RAM Char-ROM Kernal-ROM
// 011 Basic-ROM Char-ROM Kernal-ROM
// 100 RAM RAM RAM
// 101 RAM I/O RAM
// 110 RAM I/O Kernal-ROM
// 111 Basic-ROM I/O Kernal-ROM
//
// "Transparent ROM" mode:
//
// Basic-ROM and Kernal-ROM are considered transparent to read/write access.
// Basic-ROM is also considered transparent to branches (JMP, BCC, ...).
// I/O and Kernal-ROM are togglable via bank-select register $01.
static bool isBasic; // these flags are used to not have to repeatedly
static bool isIO; // evaluate the bank-select register for each
static bool isKernal; // address operand
static ubyte* bankSelReg; // pointer to RAM[1], bank-select register
static ubyte fakeReadTimer;
inline void evalBankSelect()
{
// Determine new memory configuration.
isBasic = ((*bankSelReg & 3) == 3);
isIO = ((*bankSelReg & 7) > 4);
isKernal = ((*bankSelReg & 2) != 0);
}
// Upon JMP/JSR prevent code execution in Basic-ROM/Kernal-ROM.
inline void evalBankJump()
{
if (PC < 0xA000)
{
;
}
else
{
// Get high-nibble of address.
switch (PC >> 12)
{
case 0xa:
case 0xb:
{
if (isBasic)
{
RTS_();
}
break;
}
case 0xc:
{
break;
}
case 0xd:
{
if (isIO)
{
RTS_();
}
break;
}
case 0xe:
case 0xf:
default: // <-- just to please the compiler
{
if (isKernal)
{
RTS_();
}
break;
}
}
}
}
// Functions to retrieve data.
static ubyte readData_bs(uword addr)
{
if (addr < 0xA000)
{
return c64mem1[addr];
}
else
{
// Get high-nibble of address.
switch (addr >> 12)
{
case 0xa:
case 0xb:
{
if (isBasic)
return c64mem2[addr];
else
return c64mem1[addr];
}
case 0xc:
{
return c64mem1[addr];
}
case 0xd:
{
if (isIO)
{
uword tempAddr = (addr & 0xfc1f);
// Not SID ?
if (( tempAddr & 0xff00 ) != 0xd400 )
{
switch (addr)
{
case 0xd011:
case 0xd012:
case 0xdc04:
case 0xdc05:
{
return (fakeReadTimer = c64mem2[addr]+fakeReadTimer*13+1);
}
default:
{
return c64mem2[addr];
}
}
}
else
{
// $D41D/1E/1F, $D43D/, ... SID not mirrored
if (( tempAddr & 0x00ff ) >= 0x001d )
return(c64mem2[addr]);
// (Mirrored) SID.
else
{
switch (tempAddr)
{
case 0xd41b:
{
return optr3readWave;
}
case 0xd41c:
{
return optr3readEnve;
}
default:
{
return sidLastValue;
}
}
}
}
}
else
return c64mem1[addr];
}
case 0xe:
case 0xf:
default: // <-- just to please the compiler
{
if (isKernal)
return c64mem2[addr];
else
return c64mem1[addr];
}
}
}
}
static ubyte readData_transp(uword addr)
{
if (addr < 0xD000)
{
return c64mem1[addr];
}
else
{
// Get high-nibble of address.
switch (addr >> 12)
{
case 0xd:
{
if (isIO)
{
uword tempAddr = (addr & 0xfc1f);
// Not SID ?
if (( tempAddr & 0xff00 ) != 0xd400 )
{
switch (addr)
{
case 0xd011:
case 0xd012:
case 0xdc04:
case 0xdc05:
{
return (fakeReadTimer = c64mem2[addr]+fakeReadTimer*13+1);
}
default:
{
return c64mem2[addr];
}
}
}
else
{
// $D41D/1E/1F, $D43D/, ... SID not mirrored
if (( tempAddr & 0x00ff ) >= 0x001d )
return(c64mem2[addr]);
// (Mirrored) SID.
else
{
switch (tempAddr)
{
case 0xd41b:
{
return optr3readWave;
}
case 0xd41c:
{
return optr3readEnve;
}
default:
{
return sidLastValue;
}
}
}
}
}
else
return c64mem1[addr];
}
case 0xe:
case 0xf:
default: // <-- just to please the compiler
{
return c64mem1[addr];
}
}
}
}
static ubyte readData_plain(uword addr)
{
return c64mem1[addr];
}
inline ubyte readData_zp(uword addr)
{
return c64mem1[addr];
}
// Functions to store data.
static void writeData_bs(uword addr, ubyte data)
{
if ((addr < 0xd000) || (addr >= 0xe000))
{
c64mem1[addr] = data;
if (addr == 0x01) // write to Bank-Select Register ?
{
evalBankSelect();
}
}
else
{
if (isIO)
{
// Check whether real SID or mirrored SID.
uword tempAddr = (addr & 0xfc1f);
// Not SID ?
if (( tempAddr & 0xff00 ) != 0xd400 )
{
c64mem2[addr] = data;
}
// $D41D/1E/1F, $D43D/3E/3F, ...
// Map to real address to support PlaySID
// Extended SID Chip Registers.
else if (( tempAddr & 0x00ff ) >= 0x001d )
{
// Mirrored SID.
c64mem2[addr] = (sidLastValue = data);
}
else
{
// SID.
c64mem2[tempAddr] = (sidLastValue = data);
// Handle key_ons.
sidKeysOn[tempAddr&0x001f] = sidKeysOn[tempAddr&0x001f] || ((data&1)!=0);
// Handle key_offs.
sidKeysOff[tempAddr&0x001f] = sidKeysOff[tempAddr&0x001f] || ((data&1)==0);
}
}
else
{
c64mem1[addr] = data;
}
}
}
static void writeData_plain(uword addr, ubyte data)
{
// Check whether real SID or mirrored SID.
uword tempAddr = (addr & 0xfc1f);
// Not SID ?
if (( tempAddr & 0xff00 ) != 0xd400 )
{
c64mem1[addr] = data;
}
// $D41D/1E/1F, $D43D/3E/3F, ...
// Map to real address to support PlaySID
// Extended SID Chip Registers.
else if (( tempAddr & 0x00ff ) >= 0x001d )
{
// Mirrored SID.
c64mem1[addr] = (sidLastValue = data);
}
else
{
// SID.
c64mem2[tempAddr] = (sidLastValue = data);
// Handle key_ons.
sidKeysOn[tempAddr&0x001f] = sidKeysOn[tempAddr&0x001f] || ((data&1)!=0);
// Handle key_offs.
sidKeysOff[tempAddr&0x001f] = sidKeysOff[tempAddr&0x001f] || ((data&1)==0);
}
}
inline void writeData_zp(uword addr, ubyte data)
{
c64mem1[addr] = data;
if (addr == 0x01) // write to Bank-Select Register ?
{
evalBankSelect();
}
}
// Use pointers to allow plain-memory modifications.
static ubyte (*readData)(uword) = &readData_bs;
static void (*writeData)(uword,ubyte) = &writeData_bs;
// --------------------------------------------------------------------------
// Legal instructions in alphabetical order.
//
inline void ADC_m(ubyte x)
{
if ( DF == 1 )
{
uword AC2 = AC +x +CF;
ZF = ( AC2 == 0 );
if ((( AC & 15 ) + ( x & 15 ) + CF ) > 9 )
{
AC2 += 6;
}
VF = ((( AC ^ x ^ AC2 ) & 0x80 ) != 0 ) ^ CF;
NF = (( AC2 & 128 ) != 0 );
if ( AC2 > 0x99 )
{
AC2 += 96;
}
CF = ( AC2 > 0x99 );
AC = ( AC2 & 255 );
}
else
{
uword AC2 = AC +x +CF;
CF = ( AC2 > 255 );
VF = ((( AC ^ x ^ AC2 ) & 0x80 ) != 0 ) ^ CF;
affectNZ( AC = ( AC2 & 255 ));
}
}
static void ADC_imm() { ADC_m(imm()); pPC++; }
static void ADC_abso() { ADC_m( readData(abso()) ); pPC += 2; }
static void ADC_absx() { ADC_m( readData(absx()) ); pPC += 2; }
static void ADC_absy() { ADC_m( readData(absy()) ); pPC += 2; }
static void ADC_indx() { ADC_m( readData(indx()) ); pPC++; }
static void ADC_indy() { ADC_m( readData(indy()) ); pPC++; }
static void ADC_zp() { ADC_m( readData_zp(zp()) ); pPC++; }
static void ADC_zpx() { ADC_m( readData_zp(zpx()) ); pPC++; }
inline void AND_m(ubyte x)
{
affectNZ( AC &= x );
}
static void AND_imm() { AND_m(imm()); pPC++; }
static void AND_abso() { AND_m( readData(abso()) ); pPC += 2; }
static void AND_absx() { AND_m( readData(absx()) ); pPC += 2; }
static void AND_absy() { AND_m( readData(absy()) ); pPC += 2; }
static void AND_indx() { AND_m( readData(indx()) ); pPC++; }
static void AND_indy() { AND_m( readData(indy()) ); pPC++; }
static void AND_zp() { AND_m( readData_zp(zp()) ); pPC++; }
static void AND_zpx() { AND_m( readData_zp(zpx()) ); pPC++; }
inline ubyte ASL_m(ubyte x)
{
CF = (( x & 128 ) != 0 );
affectNZ( x <<= 1);
return x;
}
static void ASL_AC()
{
AC = ASL_m(AC);
}
static void ASL_abso()
{
uword tempAddr = abso();
pPC += 2;
writeData( tempAddr, ASL_m( readData(tempAddr)) );
}
static void ASL_absx()
{
uword tempAddr = absx();
pPC += 2;
writeData( tempAddr, ASL_m( readData(tempAddr)) );
}
static void ASL_zp()
{
uword tempAddr = zp();
pPC++;
writeData_zp( tempAddr, ASL_m( readData_zp(tempAddr)) );
}
static void ASL_zpx()
{
uword tempAddr = zpx();
pPC++;
writeData_zp( tempAddr, ASL_m( readData_zp(tempAddr)) );
}
static void BCC_() { branchIfClear(CF); }
static void BCS_() { branchIfSet(CF); }
static void BEQ_() { branchIfSet(ZF); }
inline void BIT_m(ubyte x)
{
ZF = (( AC & x ) == 0 );
VF = (( x & 64 ) != 0 );
NF = (( x & 128 ) != 0 );
}
static void BIT_abso() { BIT_m( readData(abso()) ); pPC += 2; }
static void BIT_zp() { BIT_m( readData_zp(zp()) ); pPC++; }
static void BMI_() { branchIfSet(NF); }
static void BNE_() { branchIfClear(ZF); }
static void BPL_() { branchIfClear(NF); }
static void BRK_()
{
BF = (IF = 1);
#if !defined(NO_RTS_UPON_BRK)
RTS_();
#endif
}
static void BVC_() { branchIfClear(VF); }
static void BVS_() { branchIfSet(VF); }
static void CLC_() { CF = 0; }
static void CLD_() { DF = 0; }
static void CLI_() { IF = 0; }
static void CLV_() { VF = 0; }
inline void CMP_m(ubyte x)
{
ZF = ( AC == x );
CF = ( AC >= x );
NF = ( (sbyte)( AC - x ) < 0 );
}
static void CMP_abso() { CMP_m( readData(abso()) ); pPC += 2; }
static void CMP_absx() { CMP_m( readData(absx()) ); pPC += 2; }
static void CMP_absy() { CMP_m( readData(absy()) ); pPC += 2; }
static void CMP_imm() { CMP_m(imm()); pPC++; }
static void CMP_indx() { CMP_m( readData(indx()) ); pPC++; }
static void CMP_indy() { CMP_m( readData(indy()) ); pPC++; }
static void CMP_zp() { CMP_m( readData_zp(zp()) ); pPC++; }
static void CMP_zpx() { CMP_m( readData_zp(zpx()) ); pPC++; }
inline void CPX_m(ubyte x)
{
ZF = ( XR == x );
CF = ( XR >= x );
NF = ( (sbyte)( XR - x ) < 0 );
}
static void CPX_abso() { CPX_m( readData(abso()) ); pPC += 2; }
static void CPX_imm() { CPX_m(imm()); pPC++; }
static void CPX_zp() { CPX_m( readData_zp(zp()) ); pPC++; }
inline void CPY_m(ubyte x)
{
ZF = ( YR == x );
CF = ( YR >= x );
NF = ( (sbyte)( YR - x ) < 0 );
}
static void CPY_abso() { CPY_m( readData(abso()) ); pPC += 2; }
static void CPY_imm() { CPY_m(imm()); pPC++; }
static void CPY_zp() { CPY_m( readData_zp(zp()) ); pPC++; }
inline void DEC_m(uword addr)
{
ubyte x = readData(addr);
affectNZ(--x);
writeData(addr, x);
}
inline void DEC_m_zp(uword addr)
{
ubyte x = readData_zp(addr);
affectNZ(--x);
writeData_zp(addr, x);
}
static void DEC_abso() { DEC_m( abso() ); pPC += 2; }
static void DEC_absx() { DEC_m( absx() ); pPC += 2; }
static void DEC_zp() { DEC_m_zp( zp() ); pPC++; }
static void DEC_zpx() { DEC_m_zp( zpx() ); pPC++; }
static void DEX_() { affectNZ(--XR); }
static void DEY_() { affectNZ(--YR); }
inline void EOR_m(ubyte x)
{
AC ^= x;
affectNZ(AC);
}
static void EOR_abso() { EOR_m( readData(abso()) ); pPC += 2; }
static void EOR_absx() { EOR_m( readData(absx()) ); pPC += 2; }
static void EOR_absy() { EOR_m( readData(absy()) ); pPC += 2; }
static void EOR_imm() { EOR_m(imm()); pPC++; }
static void EOR_indx() { EOR_m( readData(indx()) ); pPC++; }
static void EOR_indy() { EOR_m( readData(indy()) ); pPC++; }
static void EOR_zp() { EOR_m( readData_zp(zp()) ); pPC++; }
static void EOR_zpx() { EOR_m( readData_zp(zpx()) ); pPC++; }
inline void INC_m(uword addr)
{
ubyte x = readData(addr);
affectNZ(++x);
writeData(addr, x);
}
inline void INC_m_zp(uword addr)
{
ubyte x = readData_zp(addr);
affectNZ(++x);
writeData_zp(addr, x);
}
static void INC_abso() { INC_m( abso() ); pPC += 2; }
static void INC_absx() { INC_m( absx() ); pPC += 2; }
static void INC_zp() { INC_m_zp( zp() ); pPC++; }
static void INC_zpx() { INC_m_zp( zpx() ); pPC++; }
static void INX_() { affectNZ(++XR); }
static void INY_() { affectNZ(++YR); }
static void JMP_()
{
PC = abso();
pPC = pPCbase+PC;
evalBankJump();
}
static void JMP_transp()
{
PC = abso();
if ( (PC>=0xd000) && isKernal )
{
RTS_(); // will set pPC
}
else
{
pPC = pPCbase+PC;
}
}
static void JMP_plain()
{
PC = abso();
pPC = pPCbase+PC;
}
static void JMP_vec()
{
uword tempAddrLo = abso();
uword tempAddrHi = (tempAddrLo&0xFF00) | ((tempAddrLo+1)&0x00FF);
PC = readEndian(readData(tempAddrHi),readData(tempAddrLo));
pPC = pPCbase+PC;
evalBankJump();
}
static void JMP_vec_transp()
{
uword tempAddrLo = abso();
uword tempAddrHi = (tempAddrLo&0xFF00) | ((tempAddrLo+1)&0x00FF);
PC = readEndian(readData(tempAddrHi),readData(tempAddrLo));
if ( (PC>=0xd000) && isKernal )
{
RTS_(); // will set pPC
}
else
{
pPC = pPCbase+PC;
}
}
static void JMP_vec_plain()
{
uword tempAddrLo = abso();
uword tempAddrHi = (tempAddrLo&0xFF00) | ((tempAddrLo+1)&0x00FF);
PC = readEndian(readData(tempAddrHi),readData(tempAddrLo));
pPC = pPCbase+PC;
}
inline void JSR_main()
{
uword tempPC = abso();
pPC += 2;
PC = pPC-pPCbase;
PC--;
SP--;
writeLEword(c64mem1+SP,PC);
SP--;
checkSP();
PC = tempPC;
}
static void JSR_()
{
JSR_main();
pPC = pPCbase+PC;
evalBankJump();
}
static void JSR_transp()
{
JSR_main();
if ( (PC>=0xd000) && isKernal )
{
RTS_(); // will set pPC
}
else
{
pPC = pPCbase+PC;
}
}
static void JSR_plain()
{
JSR_main();
pPC = pPCbase+PC;
}
static void LDA_abso() { affectNZ( AC = readData(abso()) ); pPC += 2; }
static void LDA_absx() { affectNZ( AC = readData( absx() )); pPC += 2; }
static void LDA_absy() { affectNZ( AC = readData( absy() ) ); pPC += 2; }
static void LDA_imm() { affectNZ( AC = imm() ); pPC++; }
static void LDA_indx() { affectNZ( AC = readData( indx() ) ); pPC++; }
static void LDA_indy() { affectNZ( AC = readData( indy() ) ); pPC++; }
static void LDA_zp() { affectNZ( AC = readData_zp( zp() ) ); pPC++; }
static void LDA_zpx() { affectNZ( AC = readData_zp( zpx() ) ); pPC++; }
static void LDX_abso() { affectNZ(XR=readData(abso())); pPC += 2; }
static void LDX_absy() { affectNZ(XR=readData(absy())); pPC += 2; }
static void LDX_imm() { affectNZ(XR=imm()); pPC++; }
static void LDX_zp() { affectNZ(XR=readData_zp(zp())); pPC++; }
static void LDX_zpy() { affectNZ(XR=readData_zp(zpy())); pPC++; }
static void LDY_abso() { affectNZ(YR=readData(abso())); pPC += 2; }
static void LDY_absx() { affectNZ(YR=readData(absx())); pPC += 2; }
static void LDY_imm() { affectNZ(YR=imm()); pPC++; }
static void LDY_zp() { affectNZ(YR=readData_zp(zp())); pPC++; }
static void LDY_zpx() { affectNZ(YR=readData_zp(zpx())); pPC++; }
inline ubyte LSR_m(ubyte x)
{
CF = x & 1;
x >>= 1;
NF = 0;
ZF = (x == 0);
return x;
}
static void LSR_AC()
{
AC = LSR_m(AC);
}
static void LSR_abso()
{
uword tempAddr = abso();
pPC += 2;
writeData( tempAddr, (LSR_m( readData(tempAddr))) );
}
static void LSR_absx()
{
uword tempAddr = absx();
pPC += 2;
writeData( tempAddr, (LSR_m( readData(tempAddr))) );
}
static void LSR_zp()
{
uword tempAddr = zp();
pPC++;
writeData_zp( tempAddr, (LSR_m( readData_zp(tempAddr))) );
}
static void LSR_zpx()
{
uword tempAddr = zpx();
pPC++;
writeData_zp( tempAddr, (LSR_m( readData_zp(tempAddr))) );
}
inline void ORA_m(ubyte x)
{
affectNZ( AC |= x );
}
static void ORA_abso() { ORA_m( readData(abso()) ); pPC += 2; }
static void ORA_absx() { ORA_m( readData(absx()) ); pPC += 2; }
static void ORA_absy() { ORA_m( readData(absy()) ); pPC += 2; }
static void ORA_imm() { ORA_m(imm()); pPC++; }
static void ORA_indx() { ORA_m( readData(indx()) ); pPC++; }
static void ORA_indy() { ORA_m( readData(indy()) ); pPC++; }
static void ORA_zp() { ORA_m( readData_zp(zp()) ); pPC++; }
static void ORA_zpx() { ORA_m( readData_zp(zpx()) ); pPC++; }
static void NOP_() { }
static void PHA_() { c64mem1[SP--] = AC; }
static void PHP_()
{
c64mem1[SP--] = codeSR();
}
static void PLA_()
{
affectNZ(AC=c64mem1[++SP]);
}
static void PLP_()
{
decodeSR(c64mem1[++SP]);
}
inline ubyte ROL_m(ubyte x)
{
ubyte y = ( x << 1 ) + CF;
CF = (( x & 0x80 ) != 0 );
affectNZ(y);
return y;
}
static void ROL_AC() { AC=ROL_m(AC); }
static void ROL_abso()
{
uword tempAddr = abso();
pPC += 2;
writeData( tempAddr, ROL_m( readData(tempAddr)) );
}
static void ROL_absx()
{
uword tempAddr = absx();
pPC += 2;
writeData( tempAddr, ROL_m( readData(tempAddr)) );
}
static void ROL_zp()
{
uword tempAddr = zp();
pPC++;
writeData_zp( tempAddr, ROL_m( readData_zp(tempAddr)) );
}
static void ROL_zpx()
{
uword tempAddr = zpx();
pPC++;
writeData_zp( tempAddr, ROL_m( readData_zp(tempAddr)) );
}
inline ubyte ROR_m(ubyte x)
{
ubyte y = ( x >> 1 ) | ( CF << 7 );
CF = ( x & 1 );
affectNZ(y);
return y;
}
static void ROR_AC()
{
AC = ROR_m(AC);
}
static void ROR_abso()
{
uword tempAddr = abso();
pPC += 2;
writeData( tempAddr, ROR_m( readData(tempAddr)) );
}
static void ROR_absx()
{
uword tempAddr = absx();
pPC += 2;
writeData( tempAddr, ROR_m( readData(tempAddr)) );
}
static void ROR_zp()
{
uword tempAddr = zp();
pPC++;
writeData_zp( tempAddr, ROR_m( readData_zp(tempAddr)) );
}
static void ROR_zpx()
{
uword tempAddr = zpx();
pPC++;
writeData_zp( tempAddr, ROR_m( readData_zp(tempAddr)) );
}
static void RTI_()
{
// equal to RTS_();
SP++;
PC =readEndian( c64mem1[SP +1], c64mem1[SP] ) +1;
pPC = pPCbase+PC;
SP++;
checkSP();
}
// RTS_() is inline.
inline void SBC_m(ubyte s)
{
s = (~s) & 255;
if ( DF == 1 )
{
uword AC2 = AC +s +CF;
ZF = ( AC2 == 0 );
if ((( AC & 15 ) + ( s & 15 ) + CF ) > 9 )
{
AC2 += 6;
}
VF = ((( AC ^ s ^ AC2 ) & 0x80 ) != 0 ) ^ CF;
NF = (( AC2 & 128 ) != 0 );
if ( AC2 > 0x99 )
{
AC2 += 96;
}
CF = ( AC2 > 0x99 );
AC = ( AC2 & 255 );
}
else
{
uword AC2 = AC + s + CF;
CF = ( AC2 > 255 );
VF = ((( AC ^ s ^ AC2 ) & 0x80 ) != 0 ) ^ CF;
affectNZ( AC = ( AC2 & 255 ));
}
}
static void SBC_abso() { SBC_m( readData(abso()) ); pPC += 2; }
static void SBC_absx() { SBC_m( readData(absx()) ); pPC += 2; }
static void SBC_absy() { SBC_m( readData(absy()) ); pPC += 2; }
static void SBC_imm() { SBC_m(imm()); pPC++; }
static void SBC_indx() { SBC_m( readData( indx()) ); pPC++; }
static void SBC_indy() { SBC_m( readData(indy()) ); pPC++; }
static void SBC_zp() { SBC_m( readData_zp(zp()) ); pPC++; }
static void SBC_zpx() { SBC_m( readData_zp(zpx()) ); pPC++; }
static void SEC_() { CF=1; }
static void SED_() { DF=1; }
static void SEI_() { IF=1; }
static void STA_abso() { writeData( abso(), AC ); pPC += 2; }
static void STA_absx() { writeData( absx(), AC ); pPC += 2; }
static void STA_absy() { writeData( absy(), AC ); pPC += 2; }
static void STA_indx() { writeData( indx(), AC ); pPC++; }
static void STA_indy() { writeData( indy(), AC ); pPC++; }
static void STA_zp() { writeData_zp( zp(), AC ); pPC++; }
static void STA_zpx() { writeData_zp( zpx(), AC ); pPC++; }
static void STX_abso() { writeData( abso(), XR ); pPC += 2; }
static void STX_zp() { writeData_zp( zp(), XR ); pPC++; }
static void STX_zpy() { writeData_zp( zpy(), XR ); pPC++; }
static void STY_abso() { writeData( abso(), YR ); pPC += 2; }
static void STY_zp() { writeData_zp( zp(), YR ); pPC++; }
static void STY_zpx() { writeData_zp( zpx(), YR ); pPC++; }
static void TAX_() { affectNZ(XR=AC); }
static void TAY_() { affectNZ(YR=AC); }
static void TSX_()
{
XR = SP & 255;
affectNZ(XR);
}
static void TXA_() { affectNZ(AC=XR); }
static void TXS_() { SP = XR | 0x100; checkSP(); }
static void TYA_() { affectNZ(AC=YR); }
// --------------------------------------------------------------------------
// Illegal codes/instructions part (1).
static void ILL_TILT() { }
static void ILL_1NOP() { }
static void ILL_2NOP() { pPC++; }
static void ILL_3NOP() { pPC += 2; }
// --------------------------------------------------------------------------
// Illegal codes/instructions part (2).
inline void ASLORA_m(uword addr)
{
ubyte x = ASL_m(readData(addr));
writeData(addr,x);
ORA_m(x);
}
inline void ASLORA_m_zp(uword addr)
{
ubyte x = ASL_m(readData_zp(addr));
writeData_zp(addr,x);
ORA_m(x);
}
static void ASLORA_abso()
{
ASLORA_m(abso());
pPC += 2;
}
static void ASLORA_absx()
{
ASLORA_m(absx());
pPC += 2;
}
static void ASLORA_absy()
{
ASLORA_m(absy());
pPC += 2;
}
static void ASLORA_indx()
{
ASLORA_m(indx());
pPC++;
}
static void ASLORA_indy()
{
ASLORA_m(indy());
pPC++;
}
static void ASLORA_zp()
{
ASLORA_m_zp(zp());
pPC++;
}
static void ASLORA_zpx()
{
ASLORA_m_zp(zpx());
pPC++;
}
inline void ROLAND_m(uword addr)
{
uword x = ROL_m(readData(addr));
writeData(addr,x);
AND_m(x);
}
inline void ROLAND_m_zp(uword addr)
{
uword x = ROL_m(readData_zp(addr));
writeData_zp(addr,x);
AND_m(x);
}
static void ROLAND_abso()
{
ROLAND_m(abso());
pPC += 2;
}
static void ROLAND_absx()
{
ROLAND_m(absx());
pPC += 2;
}
static void ROLAND_absy()
{
ROLAND_m(absy());
pPC += 2;
}
static void ROLAND_indx()
{
ROLAND_m(indx());
pPC++;
}
static void ROLAND_indy()
{
ROLAND_m(indy());
pPC++;
}
static void ROLAND_zp()
{
ROLAND_m_zp(zp());
pPC++;
}
static void ROLAND_zpx()
{
ROLAND_m_zp(zpx());
pPC++;
}
inline void LSREOR_m(uword addr)
{
uword x = LSR_m(readData(addr));
writeData(addr,x);
EOR_m(x);
}
inline void LSREOR_m_zp(uword addr)
{
uword x = LSR_m(readData_zp(addr));
writeData_zp(addr,x);
EOR_m(x);
}
static void LSREOR_abso()
{
LSREOR_m(abso());
pPC += 2;
}
static void LSREOR_absx()
{
LSREOR_m(absx());
pPC += 2;
}
static void LSREOR_absy()
{
LSREOR_m(absy());
pPC += 2;
}
static void LSREOR_indx()
{
LSREOR_m(indx());
pPC++;
}
static void LSREOR_indy()
{
LSREOR_m(indy());
pPC++;
}
static void LSREOR_zp()
{
LSREOR_m_zp(zp());
pPC++;
}
static void LSREOR_zpx()
{
LSREOR_m_zp(zpx());
pPC++;
}
inline void RORADC_m(uword addr)
{
ubyte x = ROR_m(readData(addr));
writeData(addr,x);
ADC_m(x);
}
inline void RORADC_m_zp(uword addr)
{
ubyte x = ROR_m(readData_zp(addr));
writeData_zp(addr,x);
ADC_m(x);
}
static void RORADC_abso()
{
RORADC_m(abso());
pPC += 2;
}
static void RORADC_absx()
{
RORADC_m(absx());
pPC += 2;
}
static void RORADC_absy()
{
RORADC_m(absy());
pPC += 2;
}
static void RORADC_indx()
{
RORADC_m(indx());
pPC++;
}
static void RORADC_indy()
{
RORADC_m(indy());
pPC++;
}
static void RORADC_zp()
{
RORADC_m_zp(zp());
pPC++;
}
static void RORADC_zpx()
{
RORADC_m_zp(zpx());
pPC++;
}
inline void DECCMP_m(uword addr)
{
ubyte x = readData(addr);
writeData(addr,(--x));
CMP_m(x);
}
inline void DECCMP_m_zp(uword addr)
{
ubyte x = readData_zp(addr);
writeData_zp(addr,(--x));
CMP_m(x);
}
static void DECCMP_abso()
{
DECCMP_m(abso());
pPC += 2;
}
static void DECCMP_absx()
{
DECCMP_m(absx());
pPC += 2;
}
static void DECCMP_absy()
{
DECCMP_m(absy());
pPC += 2;
}
static void DECCMP_indx()
{
DECCMP_m(indx());
pPC++;
}
static void DECCMP_indy()
{
DECCMP_m(indy());
pPC++;
}
static void DECCMP_zp()
{
DECCMP_m_zp(zp());
pPC++;
}
static void DECCMP_zpx()
{
DECCMP_m_zp(zpx());
pPC++;
}
inline void INCSBC_m(uword addr)
{
ubyte x = readData(addr);
writeData(addr,(++x));
SBC_m(x);
}
inline void INCSBC_m_zp(uword addr)
{
ubyte x = readData_zp(addr);
writeData_zp(addr,(++x));
SBC_m(x);
}
static void INCSBC_abso()
{
INCSBC_m(abso());
pPC += 2;
}
static void INCSBC_absx()
{
INCSBC_m(absx());
pPC += 2;
}
static void INCSBC_absy()
{
INCSBC_m(absy());
pPC += 2;
}
static void INCSBC_indx()
{
INCSBC_m(indx());
pPC++;
}
static void INCSBC_indy()
{
INCSBC_m(indy());
pPC++;
}
static void INCSBC_zp()
{
INCSBC_m_zp(zp());
pPC++;
}
static void INCSBC_zpx()
{
INCSBC_m_zp(zpx());
pPC++;
}
// --------------------------------------------------------------------------
// Illegal codes/instructions part (3). This implementation is considered to
// be only partially working due to inconsistencies in the available
// documentation.
// Note: In some of the functions emulated, defined instructions are used and
// already increment the PC ! Take care, and do not increment further !
// Double-setting of processor flags can occur, too.
static void ILL_0B() // equal to 2B
{
AND_imm();
CF = NF;
}
static void ILL_4B()
{
AND_imm();
LSR_AC();
}
static void ILL_6B()
{
if (DF == 0)
{
AND_imm();
ROR_AC();
CF = (AC & 1);
VF = (AC >> 5) ^ (AC >> 6);
}
}
static void ILL_83()
{
writeData(indx(),AC & XR);
pPC++;
}
static void ILL_87()
{
writeData_zp(zp(),AC & XR);
pPC++;
}
static void ILL_8B()
{
TXA_();
AND_imm();
}
static void ILL_8F()
{
writeData(abso(),AC & XR);
pPC += 2;
}
static void ILL_93()
{
writeData(indy(), AC & XR & (1+(readData((*pPC)+1) & 0xFF)));
pPC++;
}
static void ILL_97()
{
writeData_zp(zpx(),AC & XR);
pPC++;
}
static void ILL_9B()
{
SP = 0x100 | (AC & XR);
writeData(absy(),(SP & ((*pPC+1)+1)));
pPC += 2;
checkSP();
}
static void ILL_9C()
{
writeData(absx(),(YR & ((*pPC+1)+1)));
pPC += 2;
}
static void ILL_9E()
{
writeData(absy(),(XR & ((*pPC+1)+1)));
pPC += 2;
}
static void ILL_9F()
{
writeData(absy(),(AC & XR & ((*pPC+1)+1)));
pPC += 2;
}
static void ILL_A3()
{
LDA_indx();
TAX_();
}
static void ILL_A7()
{
LDA_zp();
TAX_();
}
static void ILL_AF()
{
LDA_abso();
TAX_();
}
static void ILL_B3()
{
LDA_indy();
TAX_();
}
static void ILL_B7()
{
affectNZ(AC = readData_zp(zpy())); // would be LDA_zpy()
TAX_();
pPC++;
}
static void ILL_BB()
{
XR = (SP & absy());
pPC += 2;
TXS_();
TXA_();
}
static void ILL_BF()
{
LDA_absy();
TAX_();
}
static void ILL_CB()
{
uword tmp = XR & AC;
tmp -= imm();
CF = (tmp > 255);
affectNZ(XR=(tmp&255));
}
static void ILL_EB()
{
SBC_imm();
}
// --------------------------------------------------------------------------
static ptr2func instrList[] =
{
&BRK_, &ORA_indx, &ILL_TILT, &ASLORA_indx, &ILL_2NOP, &ORA_zp, &ASL_zp, &ASLORA_zp,
&PHP_, &ORA_imm, &ASL_AC, &ILL_0B, &ILL_3NOP, &ORA_abso, &ASL_abso, &ASLORA_abso,
&BPL_, &ORA_indy, &ILL_TILT, &ASLORA_indy, &ILL_2NOP, &ORA_zpx, &ASL_zpx, &ASLORA_zpx,
&CLC_, &ORA_absy, &ILL_1NOP, &ASLORA_absy, &ILL_3NOP, &ORA_absx, &ASL_absx, &ASLORA_absx,
&JSR_, &AND_indx, &ILL_TILT, &ROLAND_indx, &BIT_zp, &AND_zp, &ROL_zp, &ROLAND_zp,
&PLP_, &AND_imm, &ROL_AC, &ILL_0B, &BIT_abso, &AND_abso, &ROL_abso, &ROLAND_abso,
&BMI_, &AND_indy, &ILL_TILT, &ROLAND_indy, &ILL_2NOP, &AND_zpx, &ROL_zpx, &ROLAND_zpx,
&SEC_, &AND_absy, &ILL_1NOP, &ROLAND_absy, &ILL_3NOP, &AND_absx, &ROL_absx, &ROLAND_absx,
// 0x40
&RTI_, &EOR_indx, &ILL_TILT, &LSREOR_indx, &ILL_2NOP, &EOR_zp, &LSR_zp, &LSREOR_zp,
&PHA_, &EOR_imm, &LSR_AC, &ILL_4B, &JMP_, &EOR_abso, &LSR_abso, &LSREOR_abso,
&BVC_, &EOR_indy, &ILL_TILT, &LSREOR_indy, &ILL_2NOP, &EOR_zpx, &LSR_zpx, &LSREOR_zpx,
&CLI_, &EOR_absy, &ILL_1NOP, &LSREOR_absy, &ILL_3NOP, &EOR_absx, &LSR_absx, &LSREOR_absx,
&RTS_, &ADC_indx, &ILL_TILT, &RORADC_indx, &ILL_2NOP, &ADC_zp, &ROR_zp, &RORADC_zp,
&PLA_, &ADC_imm, &ROR_AC, &ILL_6B, &JMP_vec, &ADC_abso, &ROR_abso, &RORADC_abso,
&BVS_, &ADC_indy, &ILL_TILT, &RORADC_indy, &ILL_2NOP, &ADC_zpx, &ROR_zpx, &RORADC_zpx,
&SEI_, &ADC_absy, &ILL_1NOP, &RORADC_absy, &ILL_3NOP, &ADC_absx, &ROR_absx, &RORADC_absx,
// 0x80
&ILL_2NOP, &STA_indx, &ILL_2NOP, &ILL_83, &STY_zp, &STA_zp, &STX_zp, &ILL_87,
&DEY_, &ILL_2NOP, &TXA_, &ILL_8B, &STY_abso, &STA_abso, &STX_abso, &ILL_8F,
&BCC_, &STA_indy, &ILL_TILT, &ILL_93, &STY_zpx, &STA_zpx, &STX_zpy, &ILL_97,
&TYA_, &STA_absy, &TXS_, &ILL_9B, &ILL_9C, &STA_absx, &ILL_9E, &ILL_9F,
&LDY_imm, &LDA_indx, &LDX_imm, &ILL_A3, &LDY_zp, &LDA_zp, &LDX_zp, &ILL_A7,
&TAY_, &LDA_imm, &TAX_, &ILL_1NOP, &LDY_abso, &LDA_abso, &LDX_abso, &ILL_AF,
&BCS_, &LDA_indy, &ILL_TILT, &ILL_B3, &LDY_zpx, &LDA_zpx, &LDX_zpy, &ILL_B7,
&CLV_, &LDA_absy, &TSX_, &ILL_BB, &LDY_absx, &LDA_absx, &LDX_absy, &ILL_BF,
// 0xC0
&CPY_imm, &CMP_indx, &ILL_2NOP, &DECCMP_indx, &CPY_zp, &CMP_zp, &DEC_zp, &DECCMP_zp,
&INY_, &CMP_imm, &DEX_, &ILL_CB, &CPY_abso, &CMP_abso, &DEC_abso, &DECCMP_abso,
&BNE_, &CMP_indy, &ILL_TILT, &DECCMP_indy, &ILL_2NOP, &CMP_zpx, &DEC_zpx, &DECCMP_zpx,
&CLD_, &CMP_absy, &ILL_1NOP, &DECCMP_absy, &ILL_3NOP, &CMP_absx, &DEC_absx, &DECCMP_absx,
&CPX_imm, &SBC_indx, &ILL_2NOP, &INCSBC_indx, &CPX_zp, &SBC_zp, &INC_zp, &INCSBC_zp,
&INX_, &SBC_imm, &NOP_, &ILL_EB, &CPX_abso, &SBC_abso, &INC_abso, &INCSBC_abso,
&BEQ_, &SBC_indy, &ILL_TILT, &INCSBC_indy, &ILL_2NOP, &SBC_zpx, &INC_zpx, &INCSBC_zpx,
&SED_, &SBC_absy, &ILL_1NOP, &INCSBC_absy, &ILL_3NOP, &SBC_absx, &INC_absx, &INCSBC_absx
};
static int memoryMode = MPU_TRANSPARENT_ROM; // the default
void initInterpreter(int inMemoryMode)
{
memoryMode = inMemoryMode;
if (memoryMode == MPU_TRANSPARENT_ROM)
{
readData = &readData_transp;
writeData = &writeData_bs;
instrList[0x20] = &JSR_transp;
instrList[0x4C] = &JMP_transp;
instrList[0x6C] = &JMP_vec_transp;
// Make the memory buffers accessible to the whole emulator engine.
// Use two distinct 64KB memory areas.
c64mem1 = c64ramBuf;
c64mem2 = c64romBuf;
}
else if (memoryMode == MPU_PLAYSID_ENVIRONMENT)
{
readData = &readData_plain;
writeData = &writeData_plain;
instrList[0x20] = &JSR_plain;
instrList[0x4C] = &JMP_plain;
instrList[0x6C] = &JMP_vec_plain;
// Make the memory buffers accessible to the whole emulator engine.
// Use a single 64KB memory area.
c64mem2 = (c64mem1 = c64ramBuf);
}
else // if (memoryMode == MPU_BANK_SWITCHING)
{
readData = &readData_bs;
writeData = &writeData_bs;
instrList[0x20] = &JSR_;
instrList[0x4C] = &JMP_;
instrList[0x6C] = &JMP_vec;
// Make the memory buffers accessible to the whole emulator engine.
// Use two distinct 64KB memory areas.
c64mem1 = c64ramBuf;
c64mem2 = c64romBuf;
}
bankSelReg = c64ramBuf+1; // extra pointer
// Set code execution segment to RAM.
pPCbase = c64ramBuf;
pPCend = c64ramBuf+65536;
}
bool interpreter(uword p, ubyte ramrom, ubyte a, ubyte x, ubyte y)
{
if (memoryMode == MPU_PLAYSID_ENVIRONMENT)
{
AC = a;
XR = 0;
YR = 0;
}
else
{
*bankSelReg = ramrom;
evalBankSelect();
AC = a;
XR = x;
YR = y;
}
// Set program-counter (pointer instead of raw PC).
pPC = pPCbase+p;
resetSP();
resetSR();
sidKeysOff[4] = (sidKeysOff[4+7] = (sidKeysOff[4+14] = false));
sidKeysOn[4] = (sidKeysOn[4+7] = (sidKeysOn[4+14] = false));
do
{
(*instrList[*(pPC++)])();
}
while (stackIsOkay&&(pPC<pPCend));
return true;
}
void c64memReset(int clockSpeed, ubyte randomSeed)
{
static ubyte fakeRndSeed = fakeRndSeed*13+randomSeed;
fakeReadTimer = fakeRndSeed;
if ((c64mem1 != 0) && (c64mem2 != 0))
{
c64mem1[0] = 0x2F;
// defaults: Basic-ROM on, Kernal-ROM on, I/O on
c64mem1[1] = 0x07;
evalBankSelect();
// CIA-Timer A $DC04/5 = $4025 PAL, $4295 NTSC
if (clockSpeed == SIDTUNE_CLOCK_NTSC)
{
c64mem1[0x02a6] = 0; // NTSC
c64mem2[0xdc04] = 0x95;
c64mem2[0xdc05] = 0x42;
}
else // if (clockSpeed == SIDTUNE_CLOCK_PAL)
{
c64mem1[0x02a6] = 1; // PAL
c64mem2[0xdc04] = 0x25;
c64mem2[0xdc05] = 0x40;
}
// fake VBI-interrupts that do $D019, BMI ...
c64mem2[0xd019] = 0xff;
// software vectors
// IRQ to $EA31
c64mem1[0x0314] = 0x31;
c64mem1[0x0315] = 0xea;
// BRK to $FE66
c64mem1[0x0316] = 0x66;
c64mem1[0x0317] = 0xfe;
// NMI to $FE47
c64mem1[0x0318] = 0x47;
c64mem1[0x0319] = 0xfe;
// hardware vectors
if (memoryMode == MPU_PLAYSID_ENVIRONMENT)
{
c64mem1[0xff48] = 0x6c;
c64mem1[0xff49] = 0x14;
c64mem1[0xff4a] = 0x03;
c64mem1[0xfffa] = 0xf8;
c64mem1[0xfffb] = 0xff;
c64mem1[0xfffe] = 0x48;
c64mem1[0xffff] = 0xff;
}
else
{
// NMI to $FE43
c64mem1[0xfffa] = 0x43;
c64mem1[0xfffb] = 0xfe;
// RESET to $FCE2
c64mem1[0xfffc] = 0xe2;
c64mem1[0xfffd] = 0xfc;
// IRQ to $FF48
c64mem1[0xfffe] = 0x48;
c64mem1[0xffff] = 0xff;
}
// clear SID
for ( int i = 0; i < 0x1d; i++ )
{
c64mem2[0xd400 +i] = 0;
}
// default Mastervolume, no filter
c64mem2[0xd418] = (sidLastValue = 0x0f);
}
}
void c64memClear()
{
// Clear entire RAM and ROM.
for ( udword i = 0; i < 0x10000; i++ )
{
c64mem1[i] = 0;
if (memoryMode != MPU_PLAYSID_ENVIRONMENT)
{
c64mem2[i] = 0;
}
sidLastValue = 0;
}
if (memoryMode == MPU_PLAYSID_ENVIRONMENT)
{
// Fill Kernal-ROM address space with RTI instructions.
for ( udword j = 0xE000; j < 0x10000; j++ )
{
c64mem1[j] = 0x40;
}
}
else
{
// Fill Basic-ROM address space with RTS instructions.
for ( udword j1 = 0xA000; j1 < 0xC000; j1++ )
{
c64mem2[j1] = 0x60;
}
// Fill Kernal-ROM address space with RTI instructions.
for ( udword j2 = 0xE000; j2 < 0x10000; j2++ )
{
c64mem2[j2] = 0x40;
}
}
}
// Input: A 16-bit effective address
// Output: A default bank-select value for $01.
ubyte c64memRamRom( uword address )
{
if (memoryMode == MPU_PLAYSID_ENVIRONMENT)
{
return 4; // RAM only, but special I/O mode
}
else
{
if ( address < 0xa000 )
{
return 7; // Basic-ROM, Kernal-ROM, I/O
}
else if ( address < 0xd000 )
{
return 6; // Kernal-ROM, I/O
}
else if ( address >= 0xe000 )
{
return 5; // I/O only
}
else
{
return 4; // RAM only
}
}
}
|