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
|
/*
Copyright 2013-2016 Michael Pavone
This file is part of BlastEm.
BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
*/
#include "genesis.h"
#include "blastem.h"
#include "nor.h"
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include "render.h"
#include "gst.h"
#include "util.h"
#include "debug.h"
#include "gdb_remote.h"
#include "saves.h"
#include "bindings.h"
#include "jcart.h"
#include "config.h"
#include "event_log.h"
#define MCLKS_NTSC 53693175
#define MCLKS_PAL 53203395
uint32_t MCLKS_PER_68K;
#define MCLKS_PER_YM 7
#define MCLKS_PER_Z80 15
#define MCLKS_PER_PSG (MCLKS_PER_Z80*16)
#define Z80_INT_PULSE_MCLKS 2573 //measured value is ~171.5 Z80 clocks
#define DEFAULT_SYNC_INTERVAL MCLKS_LINE
#define DEFAULT_LOWPASS_CUTOFF 3390
//TODO: Figure out the exact value for this
#define LINES_NTSC 262
#define LINES_PAL 313
#ifdef IS_LIB
#define MAX_SOUND_CYCLES (MCLKS_PER_YM*NUM_OPERATORS*6*4)
#else
#define MAX_SOUND_CYCLES 100000
#endif
#ifdef NEW_CORE
#define Z80_CYCLE cycles
#define Z80_OPTS opts
#define z80_handle_code_write(...)
#else
#define Z80_CYCLE current_cycle
#define Z80_OPTS options
#endif
void genesis_serialize(genesis_context *gen, serialize_buffer *buf, uint32_t m68k_pc, uint8_t all)
{
if (all) {
start_section(buf, SECTION_68000);
m68k_serialize(gen->m68k, m68k_pc, buf);
end_section(buf);
start_section(buf, SECTION_Z80);
z80_serialize(gen->z80, buf);
end_section(buf);
}
start_section(buf, SECTION_VDP);
vdp_serialize(gen->vdp, buf);
end_section(buf);
start_section(buf, SECTION_YM2612);
ym_serialize(gen->ym, buf);
end_section(buf);
start_section(buf, SECTION_PSG);
psg_serialize(gen->psg, buf);
end_section(buf);
if (all) {
start_section(buf, SECTION_GEN_BUS_ARBITER);
save_int8(buf, gen->z80->reset);
save_int8(buf, gen->z80->busreq);
save_int16(buf, gen->z80_bank_reg);
end_section(buf);
start_section(buf, SECTION_SEGA_IO_1);
io_serialize(gen->io.ports, buf);
end_section(buf);
start_section(buf, SECTION_SEGA_IO_2);
io_serialize(gen->io.ports + 1, buf);
end_section(buf);
start_section(buf, SECTION_SEGA_IO_EXT);
io_serialize(gen->io.ports + 2, buf);
end_section(buf);
start_section(buf, SECTION_MAIN_RAM);
save_int8(buf, RAM_WORDS * 2 / 1024);
save_buffer16(buf, gen->work_ram, RAM_WORDS);
end_section(buf);
start_section(buf, SECTION_SOUND_RAM);
save_int8(buf, Z80_RAM_BYTES / 1024);
save_buffer8(buf, gen->zram, Z80_RAM_BYTES);
end_section(buf);
cart_serialize(&gen->header, buf);
}
}
static uint8_t *serialize(system_header *sys, size_t *size_out)
{
genesis_context *gen = (genesis_context *)sys;
uint32_t address;
if (gen->m68k->resume_pc) {
gen->m68k->target_cycle = gen->m68k->current_cycle;
gen->header.save_state = SERIALIZE_SLOT+1;
resume_68k(gen->m68k);
if (size_out) {
*size_out = gen->serialize_size;
}
return gen->serialize_tmp;
} else {
serialize_buffer state;
init_serialize(&state);
uint32_t address = read_word(4, (void **)gen->m68k->mem_pointers, &gen->m68k->options->gen, gen->m68k) << 16;
address |= read_word(6, (void **)gen->m68k->mem_pointers, &gen->m68k->options->gen, gen->m68k);
genesis_serialize(gen, &state, address, 1);
if (size_out) {
*size_out = state.size;
}
return state.data;
}
}
static void ram_deserialize(deserialize_buffer *buf, void *vgen)
{
genesis_context *gen = vgen;
uint32_t ram_size = load_int8(buf) * 1024 / 2;
if (ram_size > RAM_WORDS) {
fatal_error("State has a RAM size of %d bytes", ram_size * 2);
}
load_buffer16(buf, gen->work_ram, ram_size);
m68k_invalidate_code_range(gen->m68k, 0xE00000, 0x1000000);
}
static void zram_deserialize(deserialize_buffer *buf, void *vgen)
{
genesis_context *gen = vgen;
uint32_t ram_size = load_int8(buf) * 1024;
if (ram_size > Z80_RAM_BYTES) {
fatal_error("State has a Z80 RAM size of %d bytes", ram_size);
}
load_buffer8(buf, gen->zram, ram_size);
z80_invalidate_code_range(gen->z80, 0, 0x4000);
}
static void update_z80_bank_pointer(genesis_context *gen)
{
if (gen->z80_bank_reg < 0x140) {
gen->z80->mem_pointers[1] = get_native_pointer(gen->z80_bank_reg << 15, (void **)gen->m68k->mem_pointers, &gen->m68k->options->gen);
} else {
gen->z80->mem_pointers[1] = NULL;
}
z80_invalidate_code_range(gen->z80, 0x8000, 0xFFFF);
}
static void bus_arbiter_deserialize(deserialize_buffer *buf, void *vgen)
{
genesis_context *gen = vgen;
gen->z80->reset = load_int8(buf);
gen->z80->busreq = load_int8(buf);
gen->z80_bank_reg = load_int16(buf) & 0x1FF;
}
static void adjust_int_cycle(m68k_context * context, vdp_context * v_context);
void genesis_deserialize(deserialize_buffer *buf, genesis_context *gen)
{
register_section_handler(buf, (section_handler){.fun = m68k_deserialize, .data = gen->m68k}, SECTION_68000);
register_section_handler(buf, (section_handler){.fun = z80_deserialize, .data = gen->z80}, SECTION_Z80);
register_section_handler(buf, (section_handler){.fun = vdp_deserialize, .data = gen->vdp}, SECTION_VDP);
register_section_handler(buf, (section_handler){.fun = ym_deserialize, .data = gen->ym}, SECTION_YM2612);
register_section_handler(buf, (section_handler){.fun = psg_deserialize, .data = gen->psg}, SECTION_PSG);
register_section_handler(buf, (section_handler){.fun = bus_arbiter_deserialize, .data = gen}, SECTION_GEN_BUS_ARBITER);
register_section_handler(buf, (section_handler){.fun = io_deserialize, .data = gen->io.ports}, SECTION_SEGA_IO_1);
register_section_handler(buf, (section_handler){.fun = io_deserialize, .data = gen->io.ports + 1}, SECTION_SEGA_IO_2);
register_section_handler(buf, (section_handler){.fun = io_deserialize, .data = gen->io.ports + 2}, SECTION_SEGA_IO_EXT);
register_section_handler(buf, (section_handler){.fun = ram_deserialize, .data = gen}, SECTION_MAIN_RAM);
register_section_handler(buf, (section_handler){.fun = zram_deserialize, .data = gen}, SECTION_SOUND_RAM);
register_section_handler(buf, (section_handler){.fun = cart_deserialize, .data = gen}, SECTION_MAPPER);
while (buf->cur_pos < buf->size)
{
load_section(buf);
}
update_z80_bank_pointer(gen);
adjust_int_cycle(gen->m68k, gen->vdp);
free(buf->handlers);
buf->handlers = NULL;
}
#include "m68k_internal.h" //needed for get_native_address_trans, should be eliminated once handling of PC is cleaned up
static void deserialize(system_header *sys, uint8_t *data, size_t size)
{
genesis_context *gen = (genesis_context *)sys;
deserialize_buffer buffer;
init_deserialize(&buffer, data, size);
genesis_deserialize(&buffer, gen);
//HACK: Fix this once PC/IR is represented in a better way in 68K core
gen->m68k->resume_pc = get_native_address_trans(gen->m68k, gen->m68k->last_prefetch_address);
}
uint16_t read_dma_value(uint32_t address)
{
genesis_context *genesis = (genesis_context *)current_system;
//TODO: Figure out what happens when you try to DMA from weird adresses like IO or banked Z80 area
if ((address >= 0xA00000 && address < 0xB00000) || (address >= 0xC00000 && address <= 0xE00000)) {
return 0;
}
//addresses here are word addresses (i.e. bit 0 corresponds to A1), so no need to do multiply by 2
return read_word(address * 2, (void **)genesis->m68k->mem_pointers, &genesis->m68k->options->gen, genesis->m68k);
}
static uint16_t get_open_bus_value(system_header *system)
{
genesis_context *genesis = (genesis_context *)system;
return read_dma_value(genesis->m68k->last_prefetch_address/2);
}
static void adjust_int_cycle(m68k_context * context, vdp_context * v_context)
{
//static int old_int_cycle = CYCLE_NEVER;
genesis_context *gen = context->system;
if (context->sync_cycle - context->current_cycle > gen->max_cycles) {
context->sync_cycle = context->current_cycle + gen->max_cycles;
}
context->int_cycle = CYCLE_NEVER;
if ((context->status & 0x7) < 6) {
uint32_t next_vint = vdp_next_vint(v_context);
if (next_vint != CYCLE_NEVER) {
context->int_cycle = next_vint;
context->int_num = 6;
}
if ((context->status & 0x7) < 4) {
uint32_t next_hint = vdp_next_hint(v_context);
if (next_hint != CYCLE_NEVER) {
next_hint = next_hint < context->current_cycle ? context->current_cycle : next_hint;
if (next_hint < context->int_cycle) {
context->int_cycle = next_hint;
context->int_num = 4;
}
}
}
}
if (context->int_cycle > context->current_cycle && context->int_pending == INT_PENDING_SR_CHANGE) {
context->int_pending = INT_PENDING_NONE;
}
/*if (context->int_cycle != old_int_cycle) {
printf("int cycle changed to: %d, level: %d @ %d(%d), frame: %d, vcounter: %d, hslot: %d, mask: %d, hint_counter: %d\n", context->int_cycle, context->int_num, v_context->cycles, context->current_cycle, v_context->frame, v_context->vcounter, v_context->hslot, context->status & 0x7, v_context->hint_counter);
old_int_cycle = context->int_cycle;
}*/
if (context->status & M68K_STATUS_TRACE || context->trace_pending) {
context->target_cycle = context->current_cycle;
return;
}
context->target_cycle = context->int_cycle < context->sync_cycle ? context->int_cycle : context->sync_cycle;
if (context->should_return || gen->header.enter_debugger) {
context->target_cycle = context->current_cycle;
} else if (context->target_cycle < context->current_cycle) {
//Changes to SR can result in an interrupt cycle that's in the past
//This can cause issues with the implementation of STOP though
context->target_cycle = context->current_cycle;
}
if (context->target_cycle == context->int_cycle) {
//Currently delays from Z80 access and refresh are applied only when we sync
//this can cause extra latency when it comes to interrupts
//to prevent this code forces some extra synchronization in the period immediately before an interrupt
if ((context->target_cycle - context->current_cycle) > gen->int_latency_prev1) {
context->target_cycle = context->sync_cycle = context->int_cycle - gen->int_latency_prev1;
} else if ((context->target_cycle - context->current_cycle) > gen->int_latency_prev2) {
context->target_cycle = context->sync_cycle = context->int_cycle - gen->int_latency_prev2;
} else {
context->target_cycle = context->sync_cycle = context->current_cycle;
}
}
/*printf("Cyc: %d, Trgt: %d, Int Cyc: %d, Int: %d, Mask: %X, V: %d, H: %d, HICount: %d, HReg: %d, Line: %d\n",
context->current_cycle, context->target_cycle, context->int_cycle, context->int_num, (context->status & 0x7),
v_context->regs[REG_MODE_2] & 0x20, v_context->regs[REG_MODE_1] & 0x10, v_context->hint_counter, v_context->regs[REG_HINT], v_context->cycles / MCLKS_LINE);*/
}
//#define DO_DEBUG_PRINT
#ifdef DO_DEBUG_PRINT
#define dprintf printf
#define dputs puts
#else
#define dprintf
#define dputs
#endif
static void z80_next_int_pulse(z80_context * z_context)
{
genesis_context * gen = z_context->system;
#ifdef NEW_CORE
z_context->int_cycle = vdp_next_vint_z80(gen->vdp);
z_context->int_end_cycle = z_context->int_cycle + Z80_INT_PULSE_MCLKS;
z_context->int_value = 0xFF;
z80_sync_cycle(z_context, z_context->sync_cycle);
#else
z_context->int_pulse_start = vdp_next_vint_z80(gen->vdp);
z_context->int_pulse_end = z_context->int_pulse_start + Z80_INT_PULSE_MCLKS;
z_context->im2_vector = 0xFF;
#endif
}
static void sync_z80(z80_context * z_context, uint32_t mclks)
{
#ifndef NO_Z80
if (z80_enabled) {
#ifdef NEW_CORE
if (z_context->int_cycle == 0xFFFFFFFFU) {
z80_next_int_pulse(z_context);
}
#endif
z80_run(z_context, mclks);
} else
#endif
{
z_context->Z80_CYCLE = mclks;
}
}
static void sync_sound(genesis_context * gen, uint32_t target)
{
//printf("YM | Cycle: %d, bpos: %d, PSG | Cycle: %d, bpos: %d\n", gen->ym->current_cycle, gen->ym->buffer_pos, gen->psg->cycles, gen->psg->buffer_pos * 2);
while (target > gen->psg->cycles && target - gen->psg->cycles > MAX_SOUND_CYCLES) {
uint32_t cur_target = gen->psg->cycles + MAX_SOUND_CYCLES;
//printf("Running PSG to cycle %d\n", cur_target);
psg_run(gen->psg, cur_target);
//printf("Running YM-2612 to cycle %d\n", cur_target);
ym_run(gen->ym, cur_target);
}
psg_run(gen->psg, target);
ym_run(gen->ym, target);
//printf("Target: %d, YM bufferpos: %d, PSG bufferpos: %d\n", target, gen->ym->buffer_pos, gen->psg->buffer_pos * 2);
}
//My refresh emulation isn't currently good enough and causes more problems than it solves
#define REFRESH_EMULATION
#ifdef REFRESH_EMULATION
#define REFRESH_INTERVAL 128
#define REFRESH_DELAY 2
uint32_t last_sync_cycle;
uint32_t refresh_counter;
#endif
#include <limits.h>
#define ADJUST_BUFFER (8*MCLKS_LINE*313)
#define MAX_NO_ADJUST (UINT_MAX-ADJUST_BUFFER)
m68k_context * sync_components(m68k_context * context, uint32_t address)
{
genesis_context * gen = context->system;
vdp_context * v_context = gen->vdp;
z80_context * z_context = gen->z80;
#ifdef REFRESH_EMULATION
//lame estimation of refresh cycle delay
refresh_counter += context->current_cycle - last_sync_cycle;
if (!gen->bus_busy) {
context->current_cycle += REFRESH_DELAY * MCLKS_PER_68K * (refresh_counter / (MCLKS_PER_68K * REFRESH_INTERVAL));
}
refresh_counter = refresh_counter % (MCLKS_PER_68K * REFRESH_INTERVAL);
#endif
uint32_t mclks = context->current_cycle;
sync_z80(z_context, mclks);
sync_sound(gen, mclks);
vdp_run_context(v_context, mclks);
if (mclks >= gen->reset_cycle) {
gen->reset_requested = 1;
context->should_return = 1;
gen->reset_cycle = CYCLE_NEVER;
}
if (v_context->frame != gen->last_frame) {
//printf("reached frame end %d | MCLK Cycles: %d, Target: %d, VDP cycles: %d, vcounter: %d, hslot: %d\n", gen->last_frame, mclks, gen->frame_end, v_context->cycles, v_context->vcounter, v_context->hslot);
gen->last_frame = v_context->frame;
event_flush(mclks);
gen->last_flush_cycle = mclks;
if(exit_after){
--exit_after;
if (!exit_after) {
exit(0);
}
}
if (context->current_cycle > MAX_NO_ADJUST) {
uint32_t deduction = mclks - ADJUST_BUFFER;
vdp_adjust_cycles(v_context, deduction);
io_adjust_cycles(gen->io.ports, context->current_cycle, deduction);
io_adjust_cycles(gen->io.ports+1, context->current_cycle, deduction);
io_adjust_cycles(gen->io.ports+2, context->current_cycle, deduction);
if (gen->mapper_type == MAPPER_JCART) {
jcart_adjust_cycles(gen, deduction);
}
context->current_cycle -= deduction;
z80_adjust_cycles(z_context, deduction);
ym_adjust_cycles(gen->ym, deduction);
if (gen->ym->vgm) {
vgm_adjust_cycles(gen->ym->vgm, deduction);
}
gen->psg->cycles -= deduction;
if (gen->reset_cycle != CYCLE_NEVER) {
gen->reset_cycle -= deduction;
}
event_cycle_adjust(mclks, deduction);
gen->last_flush_cycle -= deduction;
}
} else if (mclks - gen->last_flush_cycle > gen->soft_flush_cycles) {
event_soft_flush(mclks);
gen->last_flush_cycle = mclks;
}
gen->frame_end = vdp_cycles_to_frame_end(v_context);
context->sync_cycle = gen->frame_end;
//printf("Set sync cycle to: %d @ %d, vcounter: %d, hslot: %d\n", context->sync_cycle, context->current_cycle, v_context->vcounter, v_context->hslot);
if (context->int_ack) {
//printf("acknowledging %d @ %d:%d, vcounter: %d, hslot: %d\n", context->int_ack, context->current_cycle, v_context->cycles, v_context->vcounter, v_context->hslot);
vdp_int_ack(v_context);
context->int_ack = 0;
}
if (!address && (gen->header.enter_debugger || gen->header.save_state)) {
context->sync_cycle = context->current_cycle + 1;
}
adjust_int_cycle(context, v_context);
if (gen->reset_cycle < context->target_cycle) {
context->target_cycle = gen->reset_cycle;
}
if (address) {
if (gen->header.enter_debugger) {
gen->header.enter_debugger = 0;
debugger(context, address);
}
#ifdef NEW_CORE
if (gen->header.save_state) {
#else
if (gen->header.save_state && (z_context->pc || !z_context->native_pc || z_context->reset || !z_context->busreq)) {
#endif
uint8_t slot = gen->header.save_state - 1;
gen->header.save_state = 0;
#ifndef NEW_CORE
if (z_context->native_pc && !z_context->reset) {
//advance Z80 core to the start of an instruction
while (!z_context->pc)
{
sync_z80(z_context, z_context->current_cycle + MCLKS_PER_Z80);
}
}
#endif
char *save_path = slot >= SERIALIZE_SLOT ? NULL : get_slot_name(&gen->header, slot, use_native_states ? "state" : "gst");
if (use_native_states || slot >= SERIALIZE_SLOT) {
serialize_buffer state;
init_serialize(&state);
genesis_serialize(gen, &state, address, slot != EVENTLOG_SLOT);
if (slot == SERIALIZE_SLOT) {
gen->serialize_tmp = state.data;
gen->serialize_size = state.size;
context->sync_cycle = context->current_cycle;
context->should_return = 1;
} else if (slot == EVENTLOG_SLOT) {
event_state(context->current_cycle, &state);
} else {
save_to_file(&state, save_path);
free(state.data);
}
} else {
save_gst(gen, save_path, address);
}
if (slot != SERIALIZE_SLOT) {
debug_message("Saved state to %s\n", save_path);
}
free(save_path);
} else if(gen->header.save_state) {
context->sync_cycle = context->current_cycle + 1;
}
}
#ifdef REFRESH_EMULATION
last_sync_cycle = context->current_cycle;
#endif
return context;
}
static m68k_context * vdp_port_write(uint32_t vdp_port, m68k_context * context, uint16_t value)
{
if (vdp_port & 0x2700E0) {
fatal_error("machine freeze due to write to address %X\n", 0xC00000 | vdp_port);
}
vdp_port &= 0x1F;
//printf("vdp_port write: %X, value: %X, cycle: %d\n", vdp_port, value, context->current_cycle);
#ifdef REFRESH_EMULATION
//do refresh check here so we can avoid adding a penalty for a refresh that happens during a VDP access
refresh_counter += context->current_cycle - 4*MCLKS_PER_68K - last_sync_cycle;
context->current_cycle += REFRESH_DELAY * MCLKS_PER_68K * (refresh_counter / (MCLKS_PER_68K * REFRESH_INTERVAL));
refresh_counter = refresh_counter % (MCLKS_PER_68K * REFRESH_INTERVAL);
last_sync_cycle = context->current_cycle;
#endif
sync_components(context, 0);
genesis_context * gen = context->system;
vdp_context *v_context = gen->vdp;
uint32_t before_cycle = v_context->cycles;
if (vdp_port < 0x10) {
int blocked;
if (vdp_port < 4) {
while (vdp_data_port_write(v_context, value) < 0) {
while(v_context->flags & FLAG_DMA_RUN) {
vdp_run_dma_done(v_context, gen->frame_end);
if (v_context->cycles >= gen->frame_end) {
uint32_t cycle_diff = v_context->cycles - context->current_cycle;
uint32_t m68k_cycle_diff = (cycle_diff / MCLKS_PER_68K) * MCLKS_PER_68K;
if (m68k_cycle_diff < cycle_diff) {
m68k_cycle_diff += MCLKS_PER_68K;
}
context->current_cycle += m68k_cycle_diff;
gen->bus_busy = 1;
sync_components(context, 0);
gen->bus_busy = 0;
}
}
//context->current_cycle = v_context->cycles;
}
} else if(vdp_port < 8) {
vdp_run_context_full(v_context, context->current_cycle);
before_cycle = v_context->cycles;
blocked = vdp_control_port_write(v_context, value);
if (blocked) {
while (blocked) {
while(v_context->flags & FLAG_DMA_RUN) {
vdp_run_dma_done(v_context, gen->frame_end);
if (v_context->cycles >= gen->frame_end) {
uint32_t cycle_diff = v_context->cycles - context->current_cycle;
uint32_t m68k_cycle_diff = (cycle_diff / MCLKS_PER_68K) * MCLKS_PER_68K;
if (m68k_cycle_diff < cycle_diff) {
m68k_cycle_diff += MCLKS_PER_68K;
}
context->current_cycle += m68k_cycle_diff;
gen->bus_busy = 1;
sync_components(context, 0);
gen->bus_busy = 0;
}
}
if (blocked < 0) {
blocked = vdp_control_port_write(v_context, value);
} else {
blocked = 0;
}
}
} else {
context->sync_cycle = gen->frame_end = vdp_cycles_to_frame_end(v_context);
//printf("Set sync cycle to: %d @ %d, vcounter: %d, hslot: %d\n", context->sync_cycle, context->current_cycle, v_context->vcounter, v_context->hslot);
adjust_int_cycle(context, v_context);
}
} else {
fatal_error("Illegal write to HV Counter port %X\n", vdp_port);
}
if (v_context->cycles != before_cycle) {
//printf("68K paused for %d (%d) cycles at cycle %d (%d) for write\n", v_context->cycles - context->current_cycle, v_context->cycles - before_cycle, context->current_cycle, before_cycle);
uint32_t cycle_diff = v_context->cycles - context->current_cycle;
uint32_t m68k_cycle_diff = (cycle_diff / MCLKS_PER_68K) * MCLKS_PER_68K;
if (m68k_cycle_diff < cycle_diff) {
m68k_cycle_diff += MCLKS_PER_68K;
}
context->current_cycle += m68k_cycle_diff;
//Lock the Z80 out of the bus until the VDP access is complete
gen->bus_busy = 1;
sync_z80(gen->z80, v_context->cycles);
gen->bus_busy = 0;
}
} else if (vdp_port < 0x18) {
psg_write(gen->psg, value);
} else {
vdp_test_port_write(gen->vdp, value);
}
#ifdef REFRESH_EMULATION
last_sync_cycle -= 4;
//refresh may have happened while we were waiting on the VDP,
//so advance refresh_counter but don't add any delays
if (vdp_port >= 4 && vdp_port < 8 && v_context->cycles != before_cycle) {
refresh_counter = 0;
} else {
refresh_counter += (context->current_cycle - last_sync_cycle);
refresh_counter = refresh_counter % (MCLKS_PER_68K * REFRESH_INTERVAL);
}
last_sync_cycle = context->current_cycle;
#endif
return context;
}
static m68k_context * vdp_port_write_b(uint32_t vdp_port, m68k_context * context, uint8_t value)
{
return vdp_port_write(vdp_port, context, vdp_port < 0x10 ? value | value << 8 : ((vdp_port & 1) ? value : 0));
}
static void * z80_vdp_port_write(uint32_t vdp_port, void * vcontext, uint8_t value)
{
z80_context * context = vcontext;
genesis_context * gen = context->system;
vdp_port &= 0xFF;
if (vdp_port & 0xE0) {
fatal_error("machine freeze due to write to Z80 address %X\n", 0x7F00 | vdp_port);
}
if (vdp_port < 0x10) {
//These probably won't currently interact well with the 68K accessing the VDP
if (vdp_port < 4) {
vdp_run_context(gen->vdp, context->Z80_CYCLE);
vdp_data_port_write(gen->vdp, value << 8 | value);
} else if (vdp_port < 8) {
vdp_run_context_full(gen->vdp, context->Z80_CYCLE);
vdp_control_port_write(gen->vdp, value << 8 | value);
} else {
fatal_error("Illegal write to HV Counter port %X\n", vdp_port);
}
} else if (vdp_port < 0x18) {
sync_sound(gen, context->Z80_CYCLE);
psg_write(gen->psg, value);
} else {
vdp_test_port_write(gen->vdp, value);
}
return context;
}
static uint16_t vdp_port_read(uint32_t vdp_port, m68k_context * context)
{
if (vdp_port & 0x2700E0) {
fatal_error("machine freeze due to read from address %X\n", 0xC00000 | vdp_port);
}
vdp_port &= 0x1F;
uint16_t value;
#ifdef REFRESH_EMULATION
//do refresh check here so we can avoid adding a penalty for a refresh that happens during a VDP access
refresh_counter += context->current_cycle - 4*MCLKS_PER_68K - last_sync_cycle;
context->current_cycle += REFRESH_DELAY * MCLKS_PER_68K * (refresh_counter / (MCLKS_PER_68K * REFRESH_INTERVAL));
refresh_counter = refresh_counter % (MCLKS_PER_68K * REFRESH_INTERVAL);
last_sync_cycle = context->current_cycle;
#endif
genesis_context *gen = context->system;
vdp_context * v_context = gen->vdp;
if (vdp_port < 0x10) {
if (vdp_port < 4) {
sync_components(context, 0);
uint32_t before_cycle = v_context->cycles;
value = vdp_data_port_read(v_context);
if (v_context->cycles != before_cycle) {
//printf("68K paused for %d (%d) cycles at cycle %d (%d) for read\n", v_context->cycles - context->current_cycle, v_context->cycles - before_cycle, context->current_cycle, before_cycle);
context->current_cycle = v_context->cycles;
//Lock the Z80 out of the bus until the VDP access is complete
genesis_context *gen = context->system;
gen->bus_busy = 1;
sync_z80(gen->z80, v_context->cycles);
gen->bus_busy = 0;
}
} else if(vdp_port < 8) {
vdp_run_context(v_context, context->current_cycle);
value = vdp_control_port_read(v_context);
} else {
vdp_run_context(v_context, context->current_cycle);
value = vdp_hv_counter_read(v_context);
//printf("HV Counter: %X at cycle %d\n", value, v_context->cycles);
}
} else if (vdp_port < 0x18){
fatal_error("Illegal read from PSG port %X\n", vdp_port);
} else {
value = get_open_bus_value(&gen->header);
}
#ifdef REFRESH_EMULATION
last_sync_cycle -= 4;
//refresh may have happened while we were waiting on the VDP,
//so advance refresh_counter but don't add any delays
refresh_counter += (context->current_cycle - last_sync_cycle);
refresh_counter = refresh_counter % (MCLKS_PER_68K * REFRESH_INTERVAL);
last_sync_cycle = context->current_cycle;
#endif
return value;
}
static uint8_t vdp_port_read_b(uint32_t vdp_port, m68k_context * context)
{
uint16_t value = vdp_port_read(vdp_port, context);
if (vdp_port & 1) {
return value;
} else {
return value >> 8;
}
}
static uint8_t z80_vdp_port_read(uint32_t vdp_port, void * vcontext)
{
z80_context * context = vcontext;
if (vdp_port & 0xE0) {
fatal_error("machine freeze due to read from Z80 address %X\n", 0x7F00 | vdp_port);
}
genesis_context * gen = context->system;
//VDP access goes over the 68K bus like a bank area access
//typical delay from bus arbitration
context->Z80_CYCLE += 3 * MCLKS_PER_Z80;
//TODO: add cycle for an access right after a previous one
//TODO: Below cycle time is an estimate based on the time between 68K !BG goes low and Z80 !MREQ goes high
// Needs a new logic analyzer capture to get the actual delay on the 68K side
gen->m68k->current_cycle += 8 * MCLKS_PER_68K;
vdp_port &= 0x1F;
uint16_t ret;
if (vdp_port < 0x10) {
//These probably won't currently interact well with the 68K accessing the VDP
vdp_run_context(gen->vdp, context->Z80_CYCLE);
if (vdp_port < 4) {
ret = vdp_data_port_read(gen->vdp);
} else if (vdp_port < 8) {
ret = vdp_control_port_read(gen->vdp);
} else {
ret = vdp_hv_counter_read(gen->vdp);
}
} else {
//TODO: Figure out the correct value today
ret = 0xFFFF;
}
return vdp_port & 1 ? ret : ret >> 8;
}
//TODO: Move this inside the system context
static uint32_t zram_counter = 0;
static m68k_context * io_write(uint32_t location, m68k_context * context, uint8_t value)
{
genesis_context * gen = context->system;
if (location < 0x10000) {
//Access to Z80 memory incurs a one 68K cycle wait state
context->current_cycle += MCLKS_PER_68K;
if (!z80_enabled || z80_get_busack(gen->z80, context->current_cycle)) {
location &= 0x7FFF;
if (location < 0x4000) {
gen->zram[location & 0x1FFF] = value;
#ifndef NO_Z80
z80_handle_code_write(location & 0x1FFF, gen->z80);
#endif
} else if (location < 0x6000) {
sync_sound(gen, context->current_cycle);
if (location & 1) {
ym_data_write(gen->ym, value);
} else if(location & 2) {
ym_address_write_part2(gen->ym, value);
} else {
ym_address_write_part1(gen->ym, value);
}
} else if (location == 0x6000) {
gen->z80_bank_reg = (gen->z80_bank_reg >> 1 | value << 8) & 0x1FF;
if (gen->z80_bank_reg < 0x80) {
gen->z80->mem_pointers[1] = (gen->z80_bank_reg << 15) + ((char *)gen->z80->mem_pointers[2]);
} else {
gen->z80->mem_pointers[1] = NULL;
}
} else {
fatal_error("68K write to unhandled Z80 address %X\n", location);
}
}
} else {
if (location < 0x10100) {
switch(location >> 1 & 0xFF)
{
case 0x1:
io_data_write(gen->io.ports, value, context->current_cycle);
break;
case 0x2:
io_data_write(gen->io.ports+1, value, context->current_cycle);
break;
case 0x3:
io_data_write(gen->io.ports+2, value, context->current_cycle);
break;
case 0x4:
io_control_write(gen->io.ports, value, context->current_cycle);
break;
case 0x5:
io_control_write(gen->io.ports+1, value, context->current_cycle);
break;
case 0x6:
io_control_write(gen->io.ports+2, value, context->current_cycle);
break;
case 0x7:
gen->io.ports[0].serial_out = value;
break;
case 0x8:
case 0xB:
case 0xE:
//serial input port is not writeable
break;
case 0x9:
gen->io.ports[0].serial_ctrl = value;
break;
case 0xA:
gen->io.ports[1].serial_out = value;
break;
case 0xC:
gen->io.ports[1].serial_ctrl = value;
break;
case 0xD:
gen->io.ports[2].serial_out = value;
break;
case 0xF:
gen->io.ports[2].serial_ctrl = value;
break;
}
} else {
uint32_t masked = location & 0xFFF00;
if (masked == 0x11100) {
if (value & 1) {
dputs("bus requesting Z80");
if (z80_enabled) {
z80_assert_busreq(gen->z80, context->current_cycle);
} else {
gen->z80->busack = 1;
}
} else {
if (gen->z80->busreq) {
dputs("releasing z80 bus");
#ifdef DO_DEBUG_PRINT
char fname[20];
sprintf(fname, "zram-%d", zram_counter++);
FILE * f = fopen(fname, "wb");
fwrite(z80_ram, 1, sizeof(z80_ram), f);
fclose(f);
#endif
}
if (z80_enabled) {
z80_clear_busreq(gen->z80, context->current_cycle);
} else {
gen->z80->busack = 0;
}
}
} else if (masked == 0x11200) {
sync_z80(gen->z80, context->current_cycle);
if (value & 1) {
if (z80_enabled) {
z80_clear_reset(gen->z80, context->current_cycle);
} else {
gen->z80->reset = 0;
}
} else {
if (z80_enabled) {
z80_assert_reset(gen->z80, context->current_cycle);
} else {
gen->z80->reset = 1;
}
ym_reset(gen->ym);
}
} else if (masked != 0x11300 && masked != 0x11000) {
fatal_error("Machine freeze due to unmapped write to address %X\n", location | 0xA00000);
}
}
}
return context;
}
static m68k_context * io_write_w(uint32_t location, m68k_context * context, uint16_t value)
{
if (location < 0x10000 || (location & 0x1FFF) >= 0x100) {
return io_write(location, context, value >> 8);
} else {
return io_write(location, context, value);
}
}
#define FOREIGN 0x80
#define HZ50 0x40
#define USA FOREIGN
#define JAP 0x00
#define EUR (HZ50|FOREIGN)
#define NO_DISK 0x20
static uint8_t io_read(uint32_t location, m68k_context * context)
{
uint8_t value;
genesis_context *gen = context->system;
if (location < 0x10000) {
//Access to Z80 memory incurs a one 68K cycle wait state
context->current_cycle += MCLKS_PER_68K;
if (!z80_enabled || z80_get_busack(gen->z80, context->current_cycle)) {
location &= 0x7FFF;
if (location < 0x4000) {
value = gen->zram[location & 0x1FFF];
} else if (location < 0x6000) {
sync_sound(gen, context->current_cycle);
value = ym_read_status(gen->ym, context->current_cycle, location);
} else if (location < 0x7F00) {
value = 0xFF;
} else {
fatal_error("Machine freeze due to read of Z80 VDP memory window by 68K: %X\n", location | 0xA00000);
value = 0xFF;
}
} else {
uint16_t word = get_open_bus_value(&gen->header);
value = location & 1 ? word : word >> 8;
}
} else {
if (location < 0x10100) {
switch(location >> 1 & 0xFF)
{
case 0x0:
//version bits should be 0 for now since we're not emulating TMSS
value = gen->version_reg;
break;
case 0x1:
value = io_data_read(gen->io.ports, context->current_cycle);
break;
case 0x2:
value = io_data_read(gen->io.ports+1, context->current_cycle);
break;
case 0x3:
value = io_data_read(gen->io.ports+2, context->current_cycle);
break;
case 0x4:
value = gen->io.ports[0].control;
break;
case 0x5:
value = gen->io.ports[1].control;
break;
case 0x6:
value = gen->io.ports[2].control;
break;
case 0x7:
value = gen->io.ports[0].serial_out;
break;
case 0x8:
value = gen->io.ports[0].serial_in;
break;
case 0x9:
value = gen->io.ports[0].serial_ctrl;
break;
case 0xA:
value = gen->io.ports[1].serial_out;
break;
case 0xB:
value = gen->io.ports[1].serial_in;
break;
case 0xC:
value = gen->io.ports[1].serial_ctrl;
break;
case 0xD:
value = gen->io.ports[2].serial_out;
break;
case 0xE:
value = gen->io.ports[2].serial_in;
break;
case 0xF:
value = gen->io.ports[2].serial_ctrl;
break;
default:
value = get_open_bus_value(&gen->header) >> 8;
}
} else {
uint32_t masked = location & 0xFFF00;
if (masked == 0x11100) {
value = z80_enabled ? !z80_get_busack(gen->z80, context->current_cycle) : !gen->z80->busack;
value |= (get_open_bus_value(&gen->header) >> 8) & 0xFE;
dprintf("Byte read of BUSREQ returned %d @ %d (reset: %d)\n", value, context->current_cycle, gen->z80->reset);
} else if (masked == 0x11200) {
value = !gen->z80->reset;
} else if (masked == 0x11300 || masked == 0x11000) {
//A11300 is apparently completely unused
//A11000 is the memory control register which I am assuming is write only
value = get_open_bus_value(&gen->header) >> 8;
} else {
location |= 0xA00000;
fatal_error("Machine freeze due to read of unmapped IO location %X\n", location);
value = 0xFF;
}
}
}
return value;
}
static uint16_t io_read_w(uint32_t location, m68k_context * context)
{
genesis_context *gen = context->system;
uint16_t value = io_read(location, context);
if (location < 0x10000 || (location & 0x1FFF) < 0x100) {
value = value | (value << 8);
} else {
value <<= 8;
value |= get_open_bus_value(&gen->header) & 0xFF;
}
return value;
}
static void * z80_write_ym(uint32_t location, void * vcontext, uint8_t value)
{
z80_context * context = vcontext;
genesis_context * gen = context->system;
sync_sound(gen, context->Z80_CYCLE);
if (location & 1) {
ym_data_write(gen->ym, value);
} else if (location & 2) {
ym_address_write_part2(gen->ym, value);
} else {
ym_address_write_part1(gen->ym, value);
}
return context;
}
static uint8_t z80_read_ym(uint32_t location, void * vcontext)
{
z80_context * context = vcontext;
genesis_context * gen = context->system;
sync_sound(gen, context->Z80_CYCLE);
return ym_read_status(gen->ym, context->Z80_CYCLE, location);
}
static uint8_t z80_read_bank(uint32_t location, void * vcontext)
{
z80_context * context = vcontext;
genesis_context *gen = context->system;
if (gen->bus_busy) {
context->Z80_CYCLE = gen->m68k->current_cycle;
}
//typical delay from bus arbitration
context->Z80_CYCLE += 3 * MCLKS_PER_Z80;
//TODO: add cycle for an access right after a previous one
//TODO: Below cycle time is an estimate based on the time between 68K !BG goes low and Z80 !MREQ goes high
// Needs a new logic analyzer capture to get the actual delay on the 68K side
gen->m68k->current_cycle += 8 * MCLKS_PER_68K;
location &= 0x7FFF;
if (context->mem_pointers[1]) {
return context->mem_pointers[1][location ^ 1];
}
uint32_t address = gen->z80_bank_reg << 15 | location;
if (address >= 0xC00000 && address < 0xE00000) {
return z80_vdp_port_read(location & 0xFF, context);
} else if (address >= 0xA10000 && address <= 0xA10001) {
//Apparently version reg can be read through Z80 banked area
//TODO: Check rest of IO region addresses
return gen->version_reg;
} else {
fprintf(stderr, "Unhandled read by Z80 from address %X through banked memory area (%X)\n", address, gen->z80_bank_reg << 15);
}
return 0;
}
static void *z80_write_bank(uint32_t location, void * vcontext, uint8_t value)
{
z80_context * context = vcontext;
genesis_context *gen = context->system;
if (gen->bus_busy) {
context->Z80_CYCLE = gen->m68k->current_cycle;
}
//typical delay from bus arbitration
context->Z80_CYCLE += 3 * MCLKS_PER_Z80;
//TODO: add cycle for an access right after a previous one
//TODO: Below cycle time is an estimate based on the time between 68K !BG goes low and Z80 !MREQ goes high
// Needs a new logic analyzer capture to get the actual delay on the 68K side
gen->m68k->current_cycle += 8 * MCLKS_PER_68K;
location &= 0x7FFF;
uint32_t address = gen->z80_bank_reg << 15 | location;
if (address >= 0xE00000) {
address &= 0xFFFF;
((uint8_t *)gen->work_ram)[address ^ 1] = value;
} else if (address >= 0xC00000) {
z80_vdp_port_write(location & 0xFF, context, value);
} else {
fprintf(stderr, "Unhandled write by Z80 to address %X through banked memory area\n", address);
}
return context;
}
static void *z80_write_bank_reg(uint32_t location, void * vcontext, uint8_t value)
{
z80_context * context = vcontext;
genesis_context *gen = context->system;
gen->z80_bank_reg = (gen->z80_bank_reg >> 1 | value << 8) & 0x1FF;
update_z80_bank_pointer(context->system);
return context;
}
static uint16_t unused_read(uint32_t location, void *vcontext)
{
m68k_context *context = vcontext;
genesis_context *gen = context->system;
if (location < 0x800000 || (location >= 0xA13000 && location < 0xA13100) || (location >= 0xA12000 && location < 0xA12100)) {
//Only called if the cart/exp doesn't have a more specific handler for this region
return get_open_bus_value(&gen->header);
} else if (location == 0xA14000 || location == 0xA14002) {
if (gen->version_reg & 0xF) {
return gen->tmss_lock[location >> 1 & 1];
} else {
fatal_error("Machine freeze due to read from TMSS lock when TMSS is not present %X\n", location);
return 0xFFFF;
}
} else if (location == 0xA14100) {
if (gen->version_reg & 0xF) {
return get_open_bus_value(&gen->header);
} else {
fatal_error("Machine freeze due to read from TMSS control when TMSS is not present %X\n", location);
return 0xFFFF;
}
} else {
fatal_error("Machine freeze due to unmapped read from %X\n", location);
return 0xFFFF;
}
}
static uint8_t unused_read_b(uint32_t location, void *vcontext)
{
uint16_t v = unused_read(location & 0xFFFFFE, vcontext);
if (location & 1) {
return v;
} else {
return v >> 8;
}
}
static void *unused_write(uint32_t location, void *vcontext, uint16_t value)
{
m68k_context *context = vcontext;
genesis_context *gen = context->system;
uint8_t has_tmss = gen->version_reg & 0xF;
if (has_tmss && (location == 0xA14000 || location == 0xA14002)) {
gen->tmss_lock[location >> 1 & 1] = value;
} else if (has_tmss && location == 0xA14100) {
//TODO: implement TMSS control register
} else if (location < 0x800000 || (location >= 0xA13000 && location < 0xA13100) || (location >= 0xA12000 && location < 0xA12100)) {
//these writes are ignored when no relevant hardware is present
} else {
fatal_error("Machine freeze due to unmapped write to %X\n", location);
}
return vcontext;
}
static void *unused_write_b(uint32_t location, void *vcontext, uint8_t value)
{
m68k_context *context = vcontext;
genesis_context *gen = context->system;
uint8_t has_tmss = gen->version_reg & 0xF;
if (has_tmss && location >= 0xA14000 && location <= 0xA14003) {
uint32_t offset = location >> 1 & 1;
if (location & 1) {
gen->tmss_lock[offset] &= 0xFF00;
gen->tmss_lock[offset] |= value;
} else {
gen->tmss_lock[offset] &= 0xFF;
gen->tmss_lock[offset] |= value << 8;
}
} else if (has_tmss && (location == 0xA14100 || location == 0xA14101)) {
//TODO: implement TMSS control register
} else if (location < 0x800000 || (location >= 0xA13000 && location < 0xA13100) || (location >= 0xA12000 && location < 0xA12100)) {
//these writes are ignored when no relevant hardware is present
} else {
fatal_error("Machine freeze due to unmapped byte write to %X\n", location);
}
return vcontext;
}
static void set_speed_percent(system_header * system, uint32_t percent)
{
genesis_context *context = (genesis_context *)system;
uint32_t old_clock = context->master_clock;
context->master_clock = ((uint64_t)context->normal_clock * (uint64_t)percent) / 100;
while (context->ym->current_cycle != context->psg->cycles) {
sync_sound(context, context->psg->cycles + MCLKS_PER_PSG);
}
ym_adjust_master_clock(context->ym, context->master_clock);
psg_adjust_master_clock(context->psg, context->master_clock);
}
void set_region(genesis_context *gen, rom_info *info, uint8_t region)
{
if (!region) {
char * def_region = tern_find_path_default(config, "system\0default_region\0", (tern_val){.ptrval = "U"}, TVAL_PTR).ptrval;
if (!info->regions || (info->regions & translate_region_char(toupper(*def_region)))) {
region = translate_region_char(toupper(*def_region));
} else {
region = info->regions;
}
}
if (region & REGION_E) {
gen->version_reg = NO_DISK | EUR;
} else if (region & REGION_J) {
gen->version_reg = NO_DISK | JAP;
} else {
gen->version_reg = NO_DISK | USA;
}
if (region & HZ50) {
gen->normal_clock = MCLKS_PAL;
gen->soft_flush_cycles = MCLKS_LINE * 262 / 3 + 2;
} else {
gen->normal_clock = MCLKS_NTSC;
gen->soft_flush_cycles = MCLKS_LINE * 313 / 3 + 2;
}
gen->master_clock = gen->normal_clock;
}
static uint8_t load_state(system_header *system, uint8_t slot)
{
genesis_context *gen = (genesis_context *)system;
char *statepath = get_slot_name(system, slot, "state");
deserialize_buffer state;
uint32_t pc = 0;
uint8_t ret;
if (!gen->m68k->resume_pc) {
system->delayed_load_slot = slot + 1;
gen->m68k->should_return = 1;
ret = get_modification_time(statepath) != 0;
if (!ret) {
strcpy(statepath + strlen(statepath)-strlen("state"), "gst");
ret = get_modification_time(statepath) != 0;
}
goto done;
}
if (load_from_file(&state, statepath)) {
genesis_deserialize(&state, gen);
free(state.data);
//HACK
pc = gen->m68k->last_prefetch_address;
ret = 1;
} else {
strcpy(statepath + strlen(statepath)-strlen("state"), "gst");
pc = load_gst(gen, statepath);
ret = pc != 0;
}
if (ret) {
gen->m68k->resume_pc = get_native_address_trans(gen->m68k, pc);
}
done:
free(statepath);
return ret;
}
static void handle_reset_requests(genesis_context *gen)
{
while (gen->reset_requested || gen->header.delayed_load_slot)
{
if (gen->reset_requested) {
gen->reset_requested = 0;
gen->m68k->should_return = 0;
z80_assert_reset(gen->z80, gen->m68k->current_cycle);
z80_clear_busreq(gen->z80, gen->m68k->current_cycle);
ym_reset(gen->ym);
//Is there any sort of VDP reset?
m68k_reset(gen->m68k);
}
if (gen->header.delayed_load_slot) {
load_state(&gen->header, gen->header.delayed_load_slot - 1);
gen->header.delayed_load_slot = 0;
resume_68k(gen->m68k);
}
}
if (gen->header.force_release || render_should_release_on_exit()) {
bindings_release_capture();
vdp_release_framebuffer(gen->vdp);
render_pause_source(gen->ym->audio);
render_pause_source(gen->psg->audio);
}
}
static void start_genesis(system_header *system, char *statefile)
{
genesis_context *gen = (genesis_context *)system;
if (statefile) {
//first try loading as a native format savestate
deserialize_buffer state;
uint32_t pc;
if (load_from_file(&state, statefile)) {
genesis_deserialize(&state, gen);
free(state.data);
//HACK
pc = gen->m68k->last_prefetch_address;
} else {
pc = load_gst(gen, statefile);
if (!pc) {
fatal_error("Failed to load save state %s\n", statefile);
}
}
printf("Loaded %s\n", statefile);
if (gen->header.enter_debugger) {
gen->header.enter_debugger = 0;
insert_breakpoint(gen->m68k, pc, gen->header.debugger_type == DEBUGGER_NATIVE ? debugger : gdb_debug_enter);
}
adjust_int_cycle(gen->m68k, gen->vdp);
start_68k_context(gen->m68k, pc);
} else {
if (gen->header.enter_debugger) {
gen->header.enter_debugger = 0;
uint32_t address = gen->cart[2] << 16 | gen->cart[3];
insert_breakpoint(gen->m68k, address, gen->header.debugger_type == DEBUGGER_NATIVE ? debugger : gdb_debug_enter);
}
m68k_reset(gen->m68k);
}
handle_reset_requests(gen);
return;
}
static void resume_genesis(system_header *system)
{
genesis_context *gen = (genesis_context *)system;
if (gen->header.force_release || render_should_release_on_exit()) {
gen->header.force_release = 0;
render_set_video_standard((gen->version_reg & HZ50) ? VID_PAL : VID_NTSC);
bindings_reacquire_capture();
vdp_reacquire_framebuffer(gen->vdp);
render_resume_source(gen->ym->audio);
render_resume_source(gen->psg->audio);
}
resume_68k(gen->m68k);
handle_reset_requests(gen);
}
static void inc_debug_mode(system_header *system)
{
genesis_context *gen = (genesis_context *)system;
vdp_inc_debug_mode(gen->vdp);
}
static void request_exit(system_header *system)
{
genesis_context *gen = (genesis_context *)system;
gen->m68k->target_cycle = gen->m68k->current_cycle;
gen->m68k->should_return = 1;
}
static void persist_save(system_header *system)
{
genesis_context *gen = (genesis_context *)system;
if (gen->save_type == SAVE_NONE) {
return;
}
FILE * f = fopen(save_filename, "wb");
if (!f) {
fprintf(stderr, "Failed to open %s file %s for writing\n", save_type_name(gen->save_type), save_filename);
return;
}
if (gen->save_type == RAM_FLAG_BOTH) {
byteswap_rom(gen->save_size, (uint16_t *)gen->save_storage);
}
fwrite(gen->save_storage, 1, gen->save_size, f);
if (gen->save_type == RAM_FLAG_BOTH) {
byteswap_rom(gen->save_size, (uint16_t *)gen->save_storage);
}
fclose(f);
printf("Saved %s to %s\n", save_type_name(gen->save_type), save_filename);
}
static void load_save(system_header *system)
{
genesis_context *gen = (genesis_context *)system;
FILE * f = fopen(save_filename, "rb");
if (f) {
uint32_t read = fread(gen->save_storage, 1, gen->save_size, f);
fclose(f);
if (read > 0) {
if (gen->save_type == RAM_FLAG_BOTH) {
byteswap_rom(gen->save_size, (uint16_t *)gen->save_storage);
}
printf("Loaded %s from %s\n", save_type_name(gen->save_type), save_filename);
}
}
}
static void soft_reset(system_header *system)
{
genesis_context *gen = (genesis_context *)system;
if (gen->reset_cycle == CYCLE_NEVER) {
double random = (double)rand()/(double)RAND_MAX;
gen->reset_cycle = gen->m68k->current_cycle + random * MCLKS_LINE * (gen->version_reg & HZ50 ? LINES_PAL : LINES_NTSC);
if (gen->reset_cycle < gen->m68k->target_cycle) {
gen->m68k->target_cycle = gen->reset_cycle;
}
}
}
static void free_genesis(system_header *system)
{
genesis_context *gen = (genesis_context *)system;
vdp_free(gen->vdp);
memmap_chunk *map = (memmap_chunk *)gen->m68k->options->gen.memmap;
m68k_options_free(gen->m68k->options);
free(gen->cart);
free(gen->m68k);
free(gen->work_ram);
z80_options_free(gen->z80->Z80_OPTS);
free(gen->z80);
free(gen->zram);
ym_free(gen->ym);
psg_free(gen->psg);
free(gen->header.save_dir);
free_rom_info(&gen->header.info);
free(gen->lock_on);
free(gen);
}
static void gamepad_down(system_header *system, uint8_t gamepad_num, uint8_t button)
{
genesis_context *gen = (genesis_context *)system;
io_gamepad_down(&gen->io, gamepad_num, button);
if (gen->mapper_type == MAPPER_JCART) {
jcart_gamepad_down(gen, gamepad_num, button);
}
}
static void gamepad_up(system_header *system, uint8_t gamepad_num, uint8_t button)
{
genesis_context *gen = (genesis_context *)system;
io_gamepad_up(&gen->io, gamepad_num, button);
if (gen->mapper_type == MAPPER_JCART) {
jcart_gamepad_up(gen, gamepad_num, button);
}
}
static void mouse_down(system_header *system, uint8_t mouse_num, uint8_t button)
{
genesis_context *gen = (genesis_context *)system;
io_mouse_down(&gen->io, mouse_num, button);
}
static void mouse_up(system_header *system, uint8_t mouse_num, uint8_t button)
{
genesis_context *gen = (genesis_context *)system;
io_mouse_up(&gen->io, mouse_num, button);
}
static void mouse_motion_absolute(system_header *system, uint8_t mouse_num, uint16_t x, uint16_t y)
{
genesis_context *gen = (genesis_context *)system;
io_mouse_motion_absolute(&gen->io, mouse_num, x, y);
}
static void mouse_motion_relative(system_header *system, uint8_t mouse_num, int32_t x, int32_t y)
{
genesis_context *gen = (genesis_context *)system;
io_mouse_motion_relative(&gen->io, mouse_num, x, y);
}
static void keyboard_down(system_header *system, uint8_t scancode)
{
genesis_context *gen = (genesis_context *)system;
io_keyboard_down(&gen->io, scancode);
}
static void keyboard_up(system_header *system, uint8_t scancode)
{
genesis_context *gen = (genesis_context *)system;
io_keyboard_up(&gen->io, scancode);
}
static void set_audio_config(genesis_context *gen)
{
char *config_gain;
config_gain = tern_find_path(config, "audio\0psg_gain\0", TVAL_PTR).ptrval;
render_audio_source_gaindb(gen->psg->audio, config_gain ? atof(config_gain) : 0.0f);
config_gain = tern_find_path(config, "audio\0fm_gain\0", TVAL_PTR).ptrval;
render_audio_source_gaindb(gen->ym->audio, config_gain ? atof(config_gain) : 0.0f);
char *config_dac = tern_find_path_default(config, "audio\0fm_dac\0", (tern_val){.ptrval="zero_offset"}, TVAL_PTR).ptrval;
ym_enable_zero_offset(gen->ym, !strcmp(config_dac, "zero_offset"));
}
static void config_updated(system_header *system)
{
genesis_context *gen = (genesis_context *)system;
setup_io_devices(config, &system->info, &gen->io);
set_audio_config(gen);
}
static void start_vgm_log(system_header *system, char *filename)
{
genesis_context *gen = (genesis_context *)system;
vgm_writer *vgm = vgm_write_open(filename, gen->version_reg & HZ50 ? 50 : 60, gen->master_clock, gen->m68k->current_cycle);
if (vgm) {
printf("Started logging VGM to %s\n", filename);
sync_sound(gen, vgm->last_cycle);
ym_vgm_log(gen->ym, gen->master_clock, vgm);
psg_vgm_log(gen->psg, gen->master_clock, vgm);
gen->header.vgm_logging = 1;
} else {
printf("Failed to start logging to %s\n", filename);
}
}
static void stop_vgm_log(system_header *system)
{
puts("Stopped VGM log");
genesis_context *gen = (genesis_context *)system;
vgm_close(gen->ym->vgm);
gen->ym->vgm = gen->psg->vgm = NULL;
gen->header.vgm_logging = 0;
}
genesis_context *alloc_init_genesis(rom_info *rom, void *main_rom, void *lock_on, uint32_t system_opts, uint8_t force_region)
{
static memmap_chunk z80_map[] = {
{ 0x0000, 0x4000, 0x1FFF, 0, 0, MMAP_READ | MMAP_WRITE | MMAP_CODE, NULL, NULL, NULL, NULL, NULL },
{ 0x8000, 0x10000, 0x7FFF, 0, 0, 0, NULL, NULL, NULL, z80_read_bank, z80_write_bank},
{ 0x4000, 0x6000, 0x0003, 0, 0, 0, NULL, NULL, NULL, z80_read_ym, z80_write_ym},
{ 0x6000, 0x6100, 0xFFFF, 0, 0, 0, NULL, NULL, NULL, NULL, z80_write_bank_reg},
{ 0x7F00, 0x8000, 0x00FF, 0, 0, 0, NULL, NULL, NULL, z80_vdp_port_read, z80_vdp_port_write}
};
genesis_context *gen = calloc(1, sizeof(genesis_context));
gen->header.set_speed_percent = set_speed_percent;
gen->header.start_context = start_genesis;
gen->header.resume_context = resume_genesis;
gen->header.load_save = load_save;
gen->header.persist_save = persist_save;
gen->header.load_state = load_state;
gen->header.soft_reset = soft_reset;
gen->header.free_context = free_genesis;
gen->header.get_open_bus_value = get_open_bus_value;
gen->header.request_exit = request_exit;
gen->header.inc_debug_mode = inc_debug_mode;
gen->header.gamepad_down = gamepad_down;
gen->header.gamepad_up = gamepad_up;
gen->header.mouse_down = mouse_down;
gen->header.mouse_up = mouse_up;
gen->header.mouse_motion_absolute = mouse_motion_absolute;
gen->header.mouse_motion_relative = mouse_motion_relative;
gen->header.keyboard_down = keyboard_down;
gen->header.keyboard_up = keyboard_up;
gen->header.config_updated = config_updated;
gen->header.serialize = serialize;
gen->header.deserialize = deserialize;
gen->header.start_vgm_log = start_vgm_log;
gen->header.stop_vgm_log = stop_vgm_log;
gen->header.type = SYSTEM_GENESIS;
gen->header.info = *rom;
set_region(gen, rom, force_region);
tern_node *model = get_model(config, SYSTEM_GENESIS);
uint8_t tmss = !strcmp(tern_find_ptr_default(model, "tmss", "off"), "on");
if (tmss) {
gen->version_reg |= 1;
}
uint8_t max_vsram = !strcmp(tern_find_ptr_default(model, "vsram", "40"), "64");
gen->vdp = init_vdp_context(gen->version_reg & 0x40, max_vsram);
gen->vdp->system = &gen->header;
gen->frame_end = vdp_cycles_to_frame_end(gen->vdp);
char * config_cycles = tern_find_path(config, "clocks\0max_cycles\0", TVAL_PTR).ptrval;
gen->max_cycles = config_cycles ? atoi(config_cycles) : DEFAULT_SYNC_INTERVAL;
gen->int_latency_prev1 = MCLKS_PER_68K * 32;
gen->int_latency_prev2 = MCLKS_PER_68K * 16;
render_set_video_standard((gen->version_reg & HZ50) ? VID_PAL : VID_NTSC);
event_system_start(SYSTEM_GENESIS, (gen->version_reg & HZ50) ? VID_PAL : VID_NTSC, rom->name);
gen->ym = malloc(sizeof(ym2612_context));
char *fm = tern_find_ptr_default(model, "fm", "discrete 2612");
if (!strcmp(fm + strlen(fm) -4, "3834")) {
system_opts |= YM_OPT_3834;
}
ym_init(gen->ym, gen->master_clock, MCLKS_PER_YM, system_opts);
gen->psg = malloc(sizeof(psg_context));
psg_init(gen->psg, gen->master_clock, MCLKS_PER_PSG);
set_audio_config(gen);
z80_map[0].buffer = gen->zram = calloc(1, Z80_RAM_BYTES);
#ifndef NO_Z80
z80_options *z_opts = malloc(sizeof(z80_options));
init_z80_opts(z_opts, z80_map, 5, NULL, 0, MCLKS_PER_Z80, 0xFFFF);
gen->z80 = init_z80_context(z_opts);
#ifndef NEW_CORE
gen->z80->next_int_pulse = z80_next_int_pulse;
#endif
z80_assert_reset(gen->z80, 0);
#else
gen->z80 = calloc(1, sizeof(z80_context));
#endif
gen->z80->system = gen;
gen->z80->mem_pointers[0] = gen->zram;
gen->z80->mem_pointers[1] = gen->z80->mem_pointers[2] = (uint8_t *)main_rom;
gen->cart = main_rom;
gen->lock_on = lock_on;
gen->work_ram = calloc(2, RAM_WORDS);
if (!strcmp("random", tern_find_path_default(config, "system\0ram_init\0", (tern_val){.ptrval = "zero"}, TVAL_PTR).ptrval))
{
srand(time(NULL));
for (int i = 0; i < RAM_WORDS; i++)
{
gen->work_ram[i] = rand();
}
for (int i = 0; i < Z80_RAM_BYTES; i++)
{
gen->zram[i] = rand();
}
for (int i = 0; i < VRAM_SIZE; i++)
{
gen->vdp->vdpmem[i] = rand();
}
for (int i = 0; i < SAT_CACHE_SIZE; i++)
{
gen->vdp->sat_cache[i] = rand();
}
for (int i = 0; i < CRAM_SIZE; i++)
{
write_cram_internal(gen->vdp, i, rand());
}
for (int i = 0; i < gen->vdp->vsram_size; i++)
{
gen->vdp->vsram[i] = rand();
}
}
setup_io_devices(config, rom, &gen->io);
gen->header.has_keyboard = io_has_keyboard(&gen->io);
gen->mapper_type = rom->mapper_type;
gen->save_type = rom->save_type;
if (gen->save_type != SAVE_NONE) {
gen->save_ram_mask = rom->save_mask;
gen->save_size = rom->save_size;
gen->save_storage = rom->save_buffer;
gen->eeprom_map = rom->eeprom_map;
gen->num_eeprom = rom->num_eeprom;
if (gen->save_type == SAVE_I2C) {
eeprom_init(&gen->eeprom, gen->save_storage, gen->save_size);
} else if (gen->save_type == SAVE_NOR) {
memcpy(&gen->nor, rom->nor, sizeof(gen->nor));
//nor_flash_init(&gen->nor, gen->save_storage, gen->save_size, rom->save_page_size, rom->save_product_id, rom->save_bus);
}
} else {
gen->save_storage = NULL;
}
//This must happen before we generate memory access functions in init_m68k_opts
for (int i = 0; i < rom->map_chunks; i++)
{
if (rom->map[i].start == 0xE00000) {
rom->map[i].buffer = gen->work_ram;
break;
}
}
m68k_options *opts = malloc(sizeof(m68k_options));
init_m68k_opts(opts, rom->map, rom->map_chunks, MCLKS_PER_68K);
if (!strcmp(tern_find_ptr_default(model, "tas", "broken"), "broken")) {
opts->gen.flags |= M68K_OPT_BROKEN_READ_MODIFY;
}
gen->m68k = init_68k_context(opts, NULL);
gen->m68k->system = gen;
opts->address_log = (system_opts & OPT_ADDRESS_LOG) ? fopen("address.log", "w") : NULL;
//This must happen after the 68K context has been allocated
for (int i = 0; i < rom->map_chunks; i++)
{
if (rom->map[i].flags & MMAP_PTR_IDX) {
gen->m68k->mem_pointers[rom->map[i].ptr_index] = rom->map[i].buffer;
}
}
if (gen->mapper_type == MAPPER_SEGA) {
//initialize bank registers
for (int i = 1; i < sizeof(gen->bank_regs); i++)
{
gen->bank_regs[i] = i;
}
}
gen->reset_cycle = CYCLE_NEVER;
return gen;
}
genesis_context *alloc_config_genesis(void *rom, uint32_t rom_size, void *lock_on, uint32_t lock_on_size, uint32_t ym_opts, uint8_t force_region)
{
static memmap_chunk base_map[] = {
{0xE00000, 0x1000000, 0xFFFF, 0, 0, MMAP_READ | MMAP_WRITE | MMAP_CODE, NULL,
NULL, NULL, NULL, NULL},
{0xC00000, 0xE00000, 0x1FFFFF, 0, 0, 0, NULL,
(read_16_fun)vdp_port_read, (write_16_fun)vdp_port_write,
(read_8_fun)vdp_port_read_b, (write_8_fun)vdp_port_write_b},
{0xA00000, 0xA12000, 0x1FFFF, 0, 0, 0, NULL,
(read_16_fun)io_read_w, (write_16_fun)io_write_w,
(read_8_fun)io_read, (write_8_fun)io_write},
{0x000000, 0xFFFFFF, 0xFFFFFF, 0, 0, 0, NULL,
(read_16_fun)unused_read, (write_16_fun)unused_write,
(read_8_fun)unused_read_b, (write_8_fun)unused_write_b}
};
static tern_node *rom_db;
if (!rom_db) {
rom_db = load_rom_db();
}
rom_info info = configure_rom(rom_db, rom, rom_size, lock_on, lock_on_size, base_map, sizeof(base_map)/sizeof(base_map[0]));
rom = info.rom;
rom_size = info.rom_size;
#ifndef BLASTEM_BIG_ENDIAN
byteswap_rom(rom_size, rom);
if (lock_on) {
byteswap_rom(lock_on_size, lock_on);
}
#endif
char *m68k_divider = tern_find_path(config, "clocks\0m68k_divider\0", TVAL_PTR).ptrval;
if (!m68k_divider) {
m68k_divider = "7";
}
MCLKS_PER_68K = atoi(m68k_divider);
if (!MCLKS_PER_68K) {
MCLKS_PER_68K = 7;
}
return alloc_init_genesis(&info, rom, lock_on, ym_opts, force_region);
}
|