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
|
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <assert.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#define WIIU_SAMPLE_RATE 48000
#ifdef _WIN32
#include <direct.h>
#include <windows.h>
#define snprintf _snprintf
#endif
#include <Core/gb.h>
#include "libretro.h"
#include "libretro_core_options.inc"
#ifdef _WIN32
static const char slash = '\\';
#else
static const char slash = '/';
#endif
#define MAX_VIDEO_WIDTH 256
#define MAX_VIDEO_HEIGHT 224
#define MAX_VIDEO_PIXELS (MAX_VIDEO_WIDTH * MAX_VIDEO_HEIGHT)
#define RETRO_MEMORY_GAMEBOY_1_SRAM ((1 << 8) | RETRO_MEMORY_SAVE_RAM)
#define RETRO_MEMORY_GAMEBOY_1_RTC ((2 << 8) | RETRO_MEMORY_RTC)
#define RETRO_MEMORY_GAMEBOY_2_SRAM ((3 << 8) | RETRO_MEMORY_SAVE_RAM)
#define RETRO_MEMORY_GAMEBOY_2_RTC ((3 << 8) | RETRO_MEMORY_RTC)
#define RETRO_GAME_TYPE_GAMEBOY_LINK_2P 0x101
enum rom_type {
ROM_TYPE_INVALID,
ROM_TYPE_DMG,
ROM_TYPE_SGB,
ROM_TYPE_CGB
};
#define GB_MODEL_AUTO (-1)
enum screen_layout {
LAYOUT_TOP_DOWN,
LAYOUT_LEFT_RIGHT
};
enum audio_out {
AUDIO_OUT_GB_1,
AUDIO_OUT_GB_2,
AUDIO_OUT_BOTH,
};
static GB_model_t model[2] = {
GB_MODEL_DMG_B,
GB_MODEL_DMG_B
};
static GB_model_t auto_model[2] = {
GB_MODEL_CGB_E,
GB_MODEL_CGB_E
};
static GB_model_t auto_sgb_model[2] = {
GB_MODEL_SGB_NTSC,
GB_MODEL_SGB_NTSC
};
static bool auto_sgb_enabled[2] = {
false,
false
};
static uint32_t *frame_buf = NULL;
static uint32_t *frame_buf_copy = NULL;
static uint32_t retained_frame_1[256 * 224];
static uint32_t retained_frame_2[256 * 224];
static struct retro_log_callback logging;
static retro_log_printf_t log_cb;
static retro_video_refresh_t video_cb;
static retro_audio_sample_batch_t audio_batch_cb;
static retro_input_poll_t input_poll_cb;
static retro_input_state_t input_state_cb;
static bool libretro_supports_bitmasks = false;
static unsigned emulated_devices = 1;
static bool initialized = false;
static unsigned screen_layout = 0;
static unsigned audio_out = 0;
static bool geometry_updated = false;
static bool link_cable_emulation = false;
/*static bool infrared_emulation = false;*/
static struct {
int16_t *data;
uint32_t sizes[2];
uint32_t capacity;
} output_audio_buffer = {NULL, 0, 0};
char retro_system_directory[4096];
GB_gameboy_t gameboy[2];
extern const unsigned char dmg_boot[], mgb_boot[], cgb0_boot[], cgb_boot[], agb_boot[], sgb_boot[], sgb2_boot[];
extern const unsigned dmg_boot_length, mgb_boot_length, cgb0_boot_length, cgb_boot_length, agb_boot_length, sgb_boot_length, sgb2_boot_length;
bool vblank1_occurred = false, vblank2_occurred = false;
static void fallback_log(enum retro_log_level level, const char *fmt, ...)
{
(void)level;
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
}
static struct retro_rumble_interface rumble;
static void GB_update_keys_status(GB_gameboy_t *gb, unsigned port)
{
uint16_t joypad_bits = 0;
input_poll_cb();
if (libretro_supports_bitmasks) {
joypad_bits = input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
}
else {
unsigned j;
for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3 + 1); j++) {
if (input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, j)) {
joypad_bits |= (1 << j);
}
}
}
GB_set_key_state_for_player(gb, GB_KEY_RIGHT, emulated_devices == 1 ? port : 0,
joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT));
GB_set_key_state_for_player(gb, GB_KEY_LEFT, emulated_devices == 1 ? port : 0,
joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT));
GB_set_key_state_for_player(gb, GB_KEY_UP, emulated_devices == 1 ? port : 0,
joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_UP));
GB_set_key_state_for_player(gb, GB_KEY_DOWN, emulated_devices == 1 ? port : 0,
joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN));
GB_set_key_state_for_player(gb, GB_KEY_A, emulated_devices == 1 ? port : 0,
joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_A));
GB_set_key_state_for_player(gb, GB_KEY_B, emulated_devices == 1 ? port : 0,
joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_B));
GB_set_key_state_for_player(gb, GB_KEY_SELECT, emulated_devices == 1 ? port : 0,
joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT));
GB_set_key_state_for_player(gb, GB_KEY_START, emulated_devices == 1 ? port : 0,
joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_START));
}
static void rumble_callback(GB_gameboy_t *gb, double amplitude)
{
if (!rumble.set_rumble_state) return;
if (gb == &gameboy[0]) {
rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 65535 * amplitude);
}
else if (gb == &gameboy[1]) {
rumble.set_rumble_state(1, RETRO_RUMBLE_STRONG, 65535 * amplitude);
}
}
static void ensure_output_audio_buffer_capacity(int32_t capacity)
{
if (capacity <= output_audio_buffer.capacity) {
return;
}
output_audio_buffer.data = realloc(
output_audio_buffer.data, capacity * sizeof(*output_audio_buffer.data));
output_audio_buffer.capacity = capacity;
log_cb(RETRO_LOG_DEBUG, "Output audio buffer capacity set to %d\n", capacity);
}
static void init_output_audio_buffer(int32_t capacity)
{
output_audio_buffer.data = NULL;
output_audio_buffer.sizes[0] = output_audio_buffer.sizes[1] = 0;
output_audio_buffer.capacity = 0;
ensure_output_audio_buffer_capacity(capacity);
}
static void free_output_audio_buffer()
{
free(output_audio_buffer.data);
output_audio_buffer.data = NULL;
output_audio_buffer.sizes[0] = output_audio_buffer.sizes[1] = 0;
output_audio_buffer.capacity = 0;
}
static void upload_output_audio_buffer()
{
uint32_t remaining_frames;
if (emulated_devices == 2) {
remaining_frames = MIN(output_audio_buffer.sizes[0], output_audio_buffer.sizes[1]) / 2;
output_audio_buffer.sizes[1] -= remaining_frames * 2;
}
else {
remaining_frames = output_audio_buffer.sizes[0] / 2;
}
output_audio_buffer.sizes[0] -= remaining_frames * 2;
int16_t *buf_pos = output_audio_buffer.data;
while (remaining_frames > 0) {
size_t uploaded_frames = audio_batch_cb(buf_pos, remaining_frames);
buf_pos += uploaded_frames * 2;
remaining_frames -= uploaded_frames;
}
if (emulated_devices == 2) {
memcpy(output_audio_buffer.data, buf_pos, MAX(output_audio_buffer.sizes[0], output_audio_buffer.sizes[1]));
}
}
static void audio_callback(GB_gameboy_t *gb, GB_sample_t *sample)
{
unsigned index = 0;
if (gb == &gameboy[1]) {
index = 1;
}
if (output_audio_buffer.capacity - MAX(output_audio_buffer.sizes[0], output_audio_buffer.sizes[1]) < 2) {
ensure_output_audio_buffer_capacity(output_audio_buffer.capacity * 1.5);
}
if ((index == 0 && audio_out == AUDIO_OUT_GB_1) ||
(index == 1 && audio_out == AUDIO_OUT_GB_2)) {
output_audio_buffer.data[output_audio_buffer.sizes[0]++] = sample->left;
output_audio_buffer.data[output_audio_buffer.sizes[0]++] = sample->right;
output_audio_buffer.sizes[1] = output_audio_buffer.sizes[0];
}
else if (audio_out == AUDIO_OUT_BOTH) {
if (output_audio_buffer.sizes[index] < output_audio_buffer.sizes[!index]) {
// We're the second instance to reach this sample, add and divide (To prevent overflow)
output_audio_buffer.data[output_audio_buffer.sizes[index]] =
(output_audio_buffer.data[output_audio_buffer.sizes[index]] + (signed)sample->left) / 2;
output_audio_buffer.sizes[index]++;
output_audio_buffer.data[output_audio_buffer.sizes[index]] =
(output_audio_buffer.data[output_audio_buffer.sizes[index]] + (signed)sample->right) / 2;
output_audio_buffer.sizes[index]++;
}
else {
// We're the first instance, set its contents
output_audio_buffer.data[output_audio_buffer.sizes[index]++] = sample->left;
output_audio_buffer.data[output_audio_buffer.sizes[index]++] = sample->right;
}
}
}
static void vblank1(GB_gameboy_t *gb, GB_vblank_type_t type)
{
if (type == GB_VBLANK_TYPE_REPEAT) {
memcpy(GB_get_pixels_output(gb),
retained_frame_1,
GB_get_screen_width(gb) * GB_get_screen_height(gb) * sizeof(uint32_t));
}
vblank1_occurred = true;
}
static void vblank2(GB_gameboy_t *gb, GB_vblank_type_t type)
{
if (type == GB_VBLANK_TYPE_REPEAT) {
memcpy(GB_get_pixels_output(gb),
retained_frame_2,
GB_get_screen_width(gb) * GB_get_screen_height(gb) * sizeof(uint32_t));
}
vblank2_occurred = true;
}
static void lcd_status_change_1(GB_gameboy_t *gb, bool on)
{
if (!on) {
memcpy(retained_frame_1,
GB_get_pixels_output(gb),
GB_get_screen_width(gb) * GB_get_screen_height(gb) * sizeof(uint32_t));
}
}
static void lcd_status_change_2(GB_gameboy_t *gb, bool on)
{
if (!on) {
memcpy(retained_frame_2,
GB_get_pixels_output(gb),
GB_get_screen_width(gb) * GB_get_screen_height(gb) * sizeof(uint32_t));
}
}
static bool bit_to_send1 = true, bit_to_send2 = true;
static void serial_start1(GB_gameboy_t *gb, bool bit_received)
{
bit_to_send1 = bit_received;
}
static bool serial_end1(GB_gameboy_t *gb)
{
bool ret = GB_serial_get_data_bit(&gameboy[1]);
GB_serial_set_data_bit(&gameboy[1], bit_to_send1);
return ret;
}
static void serial_start2(GB_gameboy_t *gb, bool bit_received)
{
bit_to_send2 = bit_received;
}
static bool serial_end2(GB_gameboy_t *gb)
{
bool ret = GB_serial_get_data_bit(&gameboy[0]);
GB_serial_set_data_bit(&gameboy[0], bit_to_send2);
return ret;
}
static void infrared_callback1(GB_gameboy_t *gb, bool output)
{
GB_set_infrared_input(&gameboy[1], output);
}
static void infrared_callback2(GB_gameboy_t *gb, bool output)
{
GB_set_infrared_input(&gameboy[0], output);
}
static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
{
return r <<16 | g <<8 | b;
}
static retro_environment_t environ_cb;
static void set_variable_visibility(void)
{
struct retro_core_option_display option_display_singlecart;
struct retro_core_option_display option_display_dualcart;
size_t i;
size_t num_options = 0;
// Show/hide options depending on the number of emulated devices
if (emulated_devices == 1) {
option_display_singlecart.visible = true;
option_display_dualcart.visible = false;
}
else if (emulated_devices == 2) {
option_display_singlecart.visible = false;
option_display_dualcart.visible = true;
}
// Determine number of options
while (true) {
if (!option_defs_us[num_options].key) break;
num_options++;
}
// Copy parameters from option_defs_us array
for (i = 0; i < num_options; i++) {
const char *key = option_defs_us[i].key;
if ((strcmp(key, "sameboy_model") == 0) ||
(strcmp(key, "sameboy_auto_sgb_model") == 0) ||
(strcmp(key, "sameboy_rtc") == 0) ||
(strcmp(key, "sameboy_scaling_filter") == 0) ||
(strcmp(key, "sameboy_mono_palette") == 0) ||
(strcmp(key, "sameboy_color_correction_mode") == 0) ||
(strcmp(key, "sameboy_light_temperature") == 0) ||
(strcmp(key, "sameboy_border") == 0) ||
(strcmp(key, "sameboy_high_pass_filter_mode") == 0) ||
(strcmp(key, "sameboy_audio_interference") == 0) ||
(strcmp(key, "sameboy_rumble") == 0)) {
option_display_singlecart.key = key;
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display_singlecart);
}
else if ((strcmp(key, "sameboy_link") == 0) ||
(strcmp(key, "sameboy_screen_layout") == 0) ||
(strcmp(key, "sameboy_audio_output") == 0) ||
(strcmp(key, "sameboy_model_1") == 0) ||
(strcmp(key, "sameboy_auto_sgb_model_1") == 0) ||
(strcmp(key, "sameboy_model_2") == 0) ||
(strcmp(key, "sameboy_auto_sgb_model_2") == 0) ||
(strcmp(key, "sameboy_mono_palette_1") == 0) ||
(strcmp(key, "sameboy_mono_palette_2") == 0) ||
(strcmp(key, "sameboy_color_correction_mode_1") == 0) ||
(strcmp(key, "sameboy_color_correction_mode_2") == 0) ||
(strcmp(key, "sameboy_light_temperature_1") == 0) ||
(strcmp(key, "sameboy_light_temperature_2") == 0) ||
(strcmp(key, "sameboy_high_pass_filter_mode_1") == 0) ||
(strcmp(key, "sameboy_high_pass_filter_mode_2") == 0) ||
(strcmp(key, "sameboy_audio_interference_1") == 0) ||
(strcmp(key, "sameboy_audio_interference_2") == 0) ||
(strcmp(key, "sameboy_rumble_1") == 0) ||
(strcmp(key, "sameboy_rumble_2") == 0)) {
option_display_dualcart.key = key;
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display_dualcart);
}
}
}
static const struct retro_subsystem_memory_info gb1_memory[] = {
{ "srm", RETRO_MEMORY_GAMEBOY_1_SRAM },
{ "rtc", RETRO_MEMORY_GAMEBOY_1_RTC },
};
static const struct retro_subsystem_memory_info gb2_memory[] = {
{ "srm", RETRO_MEMORY_GAMEBOY_2_SRAM },
{ "rtc", RETRO_MEMORY_GAMEBOY_2_RTC },
};
static const struct retro_subsystem_rom_info gb_roms[] = {
{ "GameBoy #1", "gb|gbc", false, false, true, gb1_memory, 1 },
{ "GameBoy #2", "gb|gbc", false, false, true, gb2_memory, 1 },
};
static const struct retro_subsystem_info subsystems[] = {
{ "2 Player Game Boy Link", "gb_link_2p", gb_roms, 2, RETRO_GAME_TYPE_GAMEBOY_LINK_2P },
{ NULL },
};
static const struct retro_controller_description controllers[] = {
{ "Nintendo Game Boy", RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0) },
};
static const struct retro_controller_description controllers_sgb[] = {
{ "SNES/SFC Gamepad", RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0) },
};
static struct retro_input_descriptor descriptors_1p[] = {
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 0 },
};
static struct retro_input_descriptor descriptors_2p[] = {
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 0 },
};
static struct retro_input_descriptor descriptors_4p[] = {
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
{ 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
{ 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 0 },
};
static void set_link_cable_state(bool state)
{
if (state && emulated_devices == 2) {
GB_set_serial_transfer_bit_start_callback(&gameboy[0], serial_start1);
GB_set_serial_transfer_bit_end_callback(&gameboy[0], serial_end1);
GB_set_serial_transfer_bit_start_callback(&gameboy[1], serial_start2);
GB_set_serial_transfer_bit_end_callback(&gameboy[1], serial_end2);
GB_set_infrared_callback(&gameboy[0], infrared_callback1);
GB_set_infrared_callback(&gameboy[1], infrared_callback2);
}
else if (!state) {
GB_set_serial_transfer_bit_start_callback(&gameboy[0], NULL);
GB_set_serial_transfer_bit_end_callback(&gameboy[0], NULL);
GB_set_serial_transfer_bit_start_callback(&gameboy[1], NULL);
GB_set_serial_transfer_bit_end_callback(&gameboy[1], NULL);
GB_set_infrared_callback(&gameboy[0], NULL);
GB_set_infrared_callback(&gameboy[1], NULL);
}
}
static void boot_rom_load(GB_gameboy_t *gb, GB_boot_rom_t type)
{
const char *model_name = (char *[]) {
[GB_BOOT_ROM_DMG_0] = "dmg0",
[GB_BOOT_ROM_DMG] = "dmg",
[GB_BOOT_ROM_MGB] = "mgb",
[GB_BOOT_ROM_SGB] = "sgb",
[GB_BOOT_ROM_SGB2] = "sgb2",
[GB_BOOT_ROM_CGB_0] = "cgb0",
[GB_BOOT_ROM_CGB] = "cgb",
[GB_BOOT_ROM_CGB_E] = "cgbE",
[GB_BOOT_ROM_AGB_0] = "agb0",
[GB_BOOT_ROM_AGB] = "agb",
}[type];
const uint8_t *boot_code = (const unsigned char *[]) {
[GB_BOOT_ROM_DMG_0] = dmg_boot, // DMG_0 not implemented yet
[GB_BOOT_ROM_DMG] = dmg_boot,
[GB_BOOT_ROM_MGB] = mgb_boot,
[GB_BOOT_ROM_SGB] = sgb_boot,
[GB_BOOT_ROM_SGB2] = sgb2_boot,
[GB_BOOT_ROM_CGB_0] = cgb0_boot,
[GB_BOOT_ROM_CGB] = cgb_boot,
[GB_BOOT_ROM_AGB] = agb_boot,
}[type];
unsigned boot_length = (unsigned []) {
[GB_BOOT_ROM_DMG_0] = dmg_boot_length, // DMG_0 not implemented yet
[GB_BOOT_ROM_DMG] = dmg_boot_length,
[GB_BOOT_ROM_MGB] = mgb_boot_length,
[GB_BOOT_ROM_SGB] = sgb_boot_length,
[GB_BOOT_ROM_SGB2] = sgb2_boot_length,
[GB_BOOT_ROM_CGB_0] = cgb0_boot_length,
[GB_BOOT_ROM_CGB] = cgb_boot_length,
[GB_BOOT_ROM_AGB] = agb_boot_length,
}[type];
char buf[4096 + 1 + 4 + 9 + 1];
snprintf(buf, sizeof(buf), "%s%c%s_boot.bin", retro_system_directory, slash, model_name);
log_cb(RETRO_LOG_INFO, "Initializing as model: %s\n", model_name);
log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf);
if (GB_load_boot_rom(gb, buf)) {
if (type == GB_BOOT_ROM_CGB_E) {
boot_rom_load(gb, GB_BOOT_ROM_CGB);
return;
}
if (type == GB_BOOT_ROM_AGB_0) {
boot_rom_load(gb, GB_BOOT_ROM_AGB);
return;
}
GB_load_boot_rom_from_buffer(gb, boot_code, boot_length);
}
}
static void retro_set_memory_maps(void)
{
struct retro_memory_descriptor descs[11];
size_t size;
uint16_t bank;
unsigned i;
/* todo: add netplay awareness for this so achievements can be granted on the respective client */
i = 0;
memset(descs, 0, sizeof(descs));
descs[0].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_IE, &size, &bank);
descs[0].start = 0xFFFF;
descs[0].len = 1;
descs[1].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_HRAM, &size, &bank);
descs[1].start = 0xFF80;
descs[1].len = 0x0080;
descs[2].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_RAM, &size, &bank);
descs[2].start = 0xC000;
descs[2].len = 0x1000;
descs[3].ptr = descs[2].ptr + 0x1000; /* GB RAM/GBC RAM bank 1 */
descs[3].start = 0xD000;
descs[3].len = 0x1000;
descs[4].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_CART_RAM, &size, &bank);
descs[4].start = 0xA000;
descs[4].len = 0x2000;
descs[5].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_VRAM, &size, &bank);
descs[5].start = 0x8000;
descs[5].len = 0x2000;
descs[6].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_ROM, &size, &bank);
descs[6].start = 0x0000;
descs[6].len = 0x4000;
descs[6].flags = RETRO_MEMDESC_CONST;
descs[7].ptr = descs[6].ptr + (bank * 0x4000);
descs[7].start = 0x4000;
descs[7].len = 0x4000;
descs[7].flags = RETRO_MEMDESC_CONST;
descs[8].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_OAM, &size, &bank);
descs[8].start = 0xFE00;
descs[8].len = 0x00A0;
descs[8].select = 0xFFFFFF00;
descs[9].ptr = descs[2].ptr + 0x2000; /* GBC RAM bank 2 */
descs[9].start = 0x10000;
descs[9].len = GB_is_cgb(&gameboy[i]) ? 0x6000 : 0; /* 0x1000 per bank (2-7), unmapped on GB */
descs[9].select = 0xFFFF0000;
descs[10].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_IO, &size, &bank);
descs[10].start = 0xFF00;
descs[10].len = 0x0080;
descs[10].select = 0xFFFFFF00;
struct retro_memory_map mmaps;
mmaps.descriptors = descs;
mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]);
environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps);
}
static void init_for_current_model(unsigned id)
{
unsigned i = id;
GB_model_t effective_model;
effective_model = model[i];
if (effective_model == GB_MODEL_AUTO) {
effective_model = auto_model[i];
}
if (GB_is_inited(&gameboy[i])) {
GB_switch_model_and_reset(&gameboy[i], effective_model);
retro_set_memory_maps();
}
else {
GB_init(&gameboy[i], effective_model);
}
geometry_updated = true;
GB_set_boot_rom_load_callback(&gameboy[i], boot_rom_load);
/* When running multiple devices they are assumed to use the same resolution */
GB_set_pixels_output(&gameboy[i],
(uint32_t *)(frame_buf + GB_get_screen_width(&gameboy[0]) * GB_get_screen_height(&gameboy[0]) * i));
GB_set_rgb_encode_callback(&gameboy[i], rgb_encode);
#ifdef WIIU
GB_set_sample_rate(&gameboy[i], WIIU_SAMPLE_RATE);
#else
GB_set_sample_rate(&gameboy[i], GB_get_clock_rate(&gameboy[i]) / 2);
#endif
GB_apu_set_sample_callback(&gameboy[i], audio_callback);
GB_set_rumble_callback(&gameboy[i], rumble_callback);
/* todo: attempt to make these more generic */
GB_set_vblank_callback(&gameboy[0], (GB_vblank_callback_t) vblank1);
GB_set_lcd_status_callback(&gameboy[0], lcd_status_change_1);
if (emulated_devices == 2) {
GB_set_vblank_callback(&gameboy[1], (GB_vblank_callback_t) vblank2);
GB_set_lcd_status_callback(&gameboy[1], lcd_status_change_2);
if (link_cable_emulation) {
set_link_cable_state(true);
}
}
/* Let's be extremely nitpicky about how devices and descriptors are set */
if (emulated_devices == 1 && (model[0] == GB_MODEL_SGB_PAL || model[0] == GB_MODEL_SGB_NTSC || model[0] == GB_MODEL_SGB2)) {
static const struct retro_controller_info ports[] = {
{ controllers_sgb, 1 },
{ controllers_sgb, 1 },
{ controllers_sgb, 1 },
{ controllers_sgb, 1 },
{ NULL, 0 },
};
environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, descriptors_4p);
}
else if (emulated_devices == 1) {
static const struct retro_controller_info ports[] = {
{ controllers, 1 },
{ NULL, 0 },
};
environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, descriptors_1p);
}
else {
static const struct retro_controller_info ports[] = {
{ controllers, 1 },
{ controllers, 1 },
{ NULL, 0 },
};
environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, descriptors_2p);
}
}
static GB_model_t string_to_model(const char *string)
{
static const struct {
const char *name;
GB_model_t model;
} models[] = {
{ "Game Boy", GB_MODEL_DMG_B},
{ "Game Boy Pocket", GB_MODEL_MGB},
{ "Game Boy Color 0", GB_MODEL_CGB_0},
{ "Game Boy Color A", GB_MODEL_CGB_A},
{ "Game Boy Color B", GB_MODEL_CGB_B},
{ "Game Boy Color C", GB_MODEL_CGB_C},
{ "Game Boy Color D", GB_MODEL_CGB_D},
{ "Game Boy Color", GB_MODEL_CGB_E},
{ "Game Boy Advance", GB_MODEL_AGB_A},
{ "Game Boy Player", GB_MODEL_GBP_A},
{ "Super Game Boy", GB_MODEL_SGB_NTSC},
{ "Super Game Boy PAL", GB_MODEL_SGB_PAL},
{ "Super Game Boy 2", GB_MODEL_SGB2},
};
for (unsigned i = 0; i < sizeof(models) / sizeof(models[0]); i++) {
if (strcmp(models[i].name, string) == 0) {
return models[i].model;
}
}
return GB_MODEL_AUTO;
}
static void check_variables()
{
struct retro_variable var = {0};
if (emulated_devices == 1) {
var.key = "sameboy_model";
var.value = NULL;
model[0] = GB_MODEL_AUTO;
auto_sgb_enabled[0] = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_model_t new_model = model[0];
new_model = string_to_model(var.value);
if (new_model == GB_MODEL_AUTO) {
if (strcmp(var.value, "Auto (SGB)") == 0) {
new_model = GB_MODEL_AUTO;
auto_sgb_enabled[0] = true;
}
}
model[0] = new_model;
}
var.key = "sameboy_auto_sgb_model";
var.value = NULL;
auto_sgb_model[0] = GB_MODEL_SGB_NTSC;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_model_t new_model = auto_sgb_model[0];
if (strcmp(var.value, "Super Game Boy PAL") == 0) {
new_model = GB_MODEL_SGB_PAL;
}
else if (strcmp(var.value, "Super Game Boy 2") == 0) {
new_model = GB_MODEL_SGB2;
}
else {
new_model = GB_MODEL_SGB_NTSC;
}
auto_sgb_model[0] = new_model;
}
var.key = "sameboy_rtc";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "sync to system clock") == 0) {
GB_set_rtc_mode(&gameboy[0], GB_RTC_MODE_SYNC_TO_HOST);
}
else if (strcmp(var.value, "accurate") == 0) {
GB_set_rtc_mode(&gameboy[0], GB_RTC_MODE_ACCURATE);
}
}
var.key = "sameboy_mono_palette";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "greyscale") == 0) {
GB_set_palette(&gameboy[0], &GB_PALETTE_GREY);
}
else if (strcmp(var.value, "lime") == 0) {
GB_set_palette(&gameboy[0], &GB_PALETTE_DMG);
}
else if (strcmp(var.value, "olive") == 0) {
GB_set_palette(&gameboy[0], &GB_PALETTE_MGB);
}
else if (strcmp(var.value, "teal") == 0) {
GB_set_palette(&gameboy[0], &GB_PALETTE_GBL);
}
}
var.key = "sameboy_color_correction_mode";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "off") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_DISABLED);
}
else if (strcmp(var.value, "correct curves") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_CORRECT_CURVES);
}
else if (strcmp(var.value, "emulate hardware") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_MODERN_BALANCED);
}
else if (strcmp(var.value, "preserve brightness") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_MODERN_BOOST_CONTRAST);
}
else if (strcmp(var.value, "reduce contrast") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_REDUCE_CONTRAST);
}
else if (strcmp(var.value, "harsh reality") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_LOW_CONTRAST);
}
else if (strcmp(var.value, "accurate") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_MODERN_ACCURATE);
}
}
var.key = "sameboy_light_temperature";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_set_light_temperature(&gameboy[0], atof(var.value));
}
var.key = "sameboy_border";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "never") == 0) {
GB_set_border_mode(&gameboy[0], GB_BORDER_NEVER);
}
else if (strcmp(var.value, "Super Game Boy only") == 0) {
GB_set_border_mode(&gameboy[0], GB_BORDER_SGB);
}
else if (strcmp(var.value, "always") == 0) {
GB_set_border_mode(&gameboy[0], GB_BORDER_ALWAYS);
}
geometry_updated = true;
}
var.key = "sameboy_high_pass_filter_mode";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "off") == 0) {
GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_OFF);
}
else if (strcmp(var.value, "accurate") == 0) {
GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_ACCURATE);
}
else if (strcmp(var.value, "remove dc offset") == 0) {
GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_REMOVE_DC_OFFSET);
}
}
var.key = "sameboy_audio_interference";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_set_interference_volume(&gameboy[0], atoi(var.value) / 100.0);
}
var.key = "sameboy_rumble";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "never") == 0) {
GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_DISABLED);
}
else if (strcmp(var.value, "rumble-enabled games") == 0) {
GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_CARTRIDGE_ONLY);
}
else if (strcmp(var.value, "all games") == 0) {
GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_ALL_GAMES);
}
}
}
else {
GB_set_border_mode(&gameboy[0], GB_BORDER_NEVER);
GB_set_border_mode(&gameboy[1], GB_BORDER_NEVER);
GB_set_rtc_mode(&gameboy[0], GB_RTC_MODE_ACCURATE);
GB_set_rtc_mode(&gameboy[1], GB_RTC_MODE_ACCURATE);
var.key = "sameboy_link";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
bool tmp = link_cable_emulation;
if (strcmp(var.value, "enabled") == 0) {
link_cable_emulation = true;
}
else {
link_cable_emulation = false;
}
if (link_cable_emulation && link_cable_emulation != tmp) {
set_link_cable_state(true);
}
else if (!link_cable_emulation && link_cable_emulation != tmp) {
set_link_cable_state(false);
}
}
var.key = "sameboy_screen_layout";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "top-down") == 0) {
screen_layout = LAYOUT_TOP_DOWN;
}
else {
screen_layout = LAYOUT_LEFT_RIGHT;
}
geometry_updated = true;
}
var.key = "sameboy_audio_output";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "Game Boy #1") == 0) {
audio_out = AUDIO_OUT_GB_1;
}
else if (strcmp(var.value, "Game Boy #2") == 0) {
audio_out = AUDIO_OUT_GB_2;
}
else {
audio_out = AUDIO_OUT_BOTH;
}
}
var.key = "sameboy_model_1";
var.value = NULL;
model[0] = GB_MODEL_AUTO;
auto_sgb_enabled[0] = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_model_t new_model = model[0];
new_model = string_to_model(var.value);
if (new_model == GB_MODEL_AUTO) {
if (strcmp(var.value, "Auto (SGB)") == 0) {
new_model = GB_MODEL_AUTO;
auto_sgb_enabled[0] = true;
}
}
model[0] = new_model;
}
var.key = "sameboy_auto_sgb_model_1";
var.value = NULL;
auto_sgb_model[0] = GB_MODEL_SGB_NTSC;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_model_t new_model = auto_sgb_model[0];
if (strcmp(var.value, "Super Game Boy PAL") == 0) {
new_model = GB_MODEL_SGB_PAL;
}
else if (strcmp(var.value, "Super Game Boy 2") == 0) {
new_model = GB_MODEL_SGB2;
}
else {
new_model = GB_MODEL_SGB_NTSC;
}
auto_sgb_model[0] = new_model;
}
var.key = "sameboy_model_2";
var.value = NULL;
model[1] = GB_MODEL_AUTO;
auto_sgb_enabled[1] = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_model_t new_model = model[1];
new_model = string_to_model(var.value);
if (new_model == GB_MODEL_AUTO) {
if (strcmp(var.value, "Auto (SGB)") == 0) {
new_model = GB_MODEL_AUTO;
auto_sgb_enabled[0] = true;
}
}
model[1] = new_model;
}
var.key = "sameboy_auto_sgb_model_2";
var.value = NULL;
auto_sgb_model[1] = GB_MODEL_SGB_NTSC;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_model_t new_model = auto_sgb_model[1];
if (strcmp(var.value, "Super Game Boy PAL") == 0) {
new_model = GB_MODEL_SGB_PAL;
}
else if (strcmp(var.value, "Super Game Boy 2") == 0) {
new_model = GB_MODEL_SGB2;
}
else {
new_model = GB_MODEL_SGB_NTSC;
}
auto_sgb_model[1] = new_model;
}
var.key = "sameboy_mono_palette_1";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "greyscale") == 0) {
GB_set_palette(&gameboy[0], &GB_PALETTE_GREY);
}
else if (strcmp(var.value, "lime") == 0) {
GB_set_palette(&gameboy[0], &GB_PALETTE_DMG);
}
else if (strcmp(var.value, "olive") == 0) {
GB_set_palette(&gameboy[0], &GB_PALETTE_MGB);
}
else if (strcmp(var.value, "teal") == 0) {
GB_set_palette(&gameboy[0], &GB_PALETTE_GBL);
}
}
var.key = "sameboy_mono_palette_2";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "greyscale") == 0) {
GB_set_palette(&gameboy[1], &GB_PALETTE_GREY);
}
else if (strcmp(var.value, "lime") == 0) {
GB_set_palette(&gameboy[1], &GB_PALETTE_DMG);
}
else if (strcmp(var.value, "olive") == 0) {
GB_set_palette(&gameboy[1], &GB_PALETTE_MGB);
}
else if (strcmp(var.value, "teal") == 0) {
GB_set_palette(&gameboy[1], &GB_PALETTE_GBL);
}
}
var.key = "sameboy_color_correction_mode_1";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "off") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_DISABLED);
}
else if (strcmp(var.value, "correct curves") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_CORRECT_CURVES);
}
else if (strcmp(var.value, "emulate hardware") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_MODERN_BALANCED);
}
else if (strcmp(var.value, "preserve brightness") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_MODERN_BOOST_CONTRAST);
}
else if (strcmp(var.value, "reduce contrast") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_REDUCE_CONTRAST);
}
else if (strcmp(var.value, "harsh reality") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_LOW_CONTRAST);
}
else if (strcmp(var.value, "accurate") == 0) {
GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_MODERN_ACCURATE);
}
}
var.key = "sameboy_color_correction_mode_2";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "off") == 0) {
GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_DISABLED);
}
else if (strcmp(var.value, "correct curves") == 0) {
GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_CORRECT_CURVES);
}
else if (strcmp(var.value, "emulate hardware") == 0) {
GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_MODERN_BALANCED);
}
else if (strcmp(var.value, "preserve brightness") == 0) {
GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_MODERN_BOOST_CONTRAST);
}
else if (strcmp(var.value, "reduce contrast") == 0) {
GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_REDUCE_CONTRAST);
}
else if (strcmp(var.value, "harsh reality") == 0) {
GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_LOW_CONTRAST);
}
else if (strcmp(var.value, "accurate") == 0) {
GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_MODERN_ACCURATE);
}
}
var.key = "sameboy_light_temperature_1";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_set_light_temperature(&gameboy[0], atof(var.value));
}
var.key = "sameboy_light_temperature_2";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_set_light_temperature(&gameboy[1], atof(var.value));
}
var.key = "sameboy_high_pass_filter_mode_1";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "off") == 0) {
GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_OFF);
}
else if (strcmp(var.value, "accurate") == 0) {
GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_ACCURATE);
}
else if (strcmp(var.value, "remove dc offset") == 0) {
GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_REMOVE_DC_OFFSET);
}
}
var.key = "sameboy_high_pass_filter_mode_2";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "off") == 0) {
GB_set_highpass_filter_mode(&gameboy[1], GB_HIGHPASS_OFF);
}
else if (strcmp(var.value, "accurate") == 0) {
GB_set_highpass_filter_mode(&gameboy[1], GB_HIGHPASS_ACCURATE);
}
else if (strcmp(var.value, "remove dc offset") == 0) {
GB_set_highpass_filter_mode(&gameboy[1], GB_HIGHPASS_REMOVE_DC_OFFSET);
}
}
var.key = "sameboy_audio_interference_1";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_set_interference_volume(&gameboy[0], atoi(var.value) / 100.0);
}
var.key = "sameboy_audio_interference_2";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
GB_set_interference_volume(&gameboy[1], atoi(var.value) / 100.0);
}
var.key = "sameboy_rumble_1";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "never") == 0) {
GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_DISABLED);
}
else if (strcmp(var.value, "rumble-enabled games") == 0) {
GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_CARTRIDGE_ONLY);
}
else if (strcmp(var.value, "all games") == 0) {
GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_ALL_GAMES);
}
}
var.key = "sameboy_rumble_2";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
if (strcmp(var.value, "never") == 0) {
GB_set_rumble_mode(&gameboy[1], GB_RUMBLE_DISABLED);
}
else if (strcmp(var.value, "rumble-enabled games") == 0) {
GB_set_rumble_mode(&gameboy[1], GB_RUMBLE_CARTRIDGE_ONLY);
}
else if (strcmp(var.value, "all games") == 0) {
GB_set_rumble_mode(&gameboy[1], GB_RUMBLE_ALL_GAMES);
}
}
}
set_variable_visibility();
}
void retro_init(void)
{
const char *dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", dir);
}
else {
snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", ".");
}
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging)) {
log_cb = logging.log;
}
else {
log_cb = fallback_log;
}
if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL)) {
libretro_supports_bitmasks = true;
}
init_output_audio_buffer(16384);
}
void retro_deinit(void)
{
free(frame_buf);
free(frame_buf_copy);
frame_buf = NULL;
frame_buf_copy = NULL;
free_output_audio_buffer();
libretro_supports_bitmasks = false;
}
unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
void retro_set_controller_port_device(unsigned port, unsigned device)
{
log_cb(RETRO_LOG_INFO, "Connecting device %u into port %u\n", device, port);
}
void retro_get_system_info(struct retro_system_info *info)
{
memset(info, 0, sizeof(*info));
info->library_name = "SameBoy";
#ifdef GIT_VERSION
info->library_version = GB_VERSION GIT_VERSION;
#else
info->library_version = GB_VERSION;
#endif
info->need_fullpath = false;
info->valid_extensions = "gb|gbc";
}
void retro_get_system_av_info(struct retro_system_av_info *info)
{
struct retro_game_geometry geom;
struct retro_system_timing timing = { GB_get_usual_frame_rate(&gameboy[0]), GB_get_sample_rate(&gameboy[audio_out == AUDIO_OUT_BOTH? 0 : audio_out])};
if (emulated_devices == 2) {
if (screen_layout == LAYOUT_TOP_DOWN) {
geom.base_width = GB_get_screen_width(&gameboy[0]);
geom.base_height = GB_get_screen_height(&gameboy[0]) * emulated_devices;
geom.aspect_ratio = (double)GB_get_screen_width(&gameboy[0]) / (emulated_devices * GB_get_screen_height(&gameboy[0]));
}
else if (screen_layout == LAYOUT_LEFT_RIGHT) {
geom.base_width = GB_get_screen_width(&gameboy[0]) * emulated_devices;
geom.base_height = GB_get_screen_height(&gameboy[0]);
geom.aspect_ratio = ((double)GB_get_screen_width(&gameboy[0]) * emulated_devices) / GB_get_screen_height(&gameboy[0]);
}
}
else {
geom.base_width = GB_get_screen_width(&gameboy[0]);
geom.base_height = GB_get_screen_height(&gameboy[0]);
geom.aspect_ratio = (double)GB_get_screen_width(&gameboy[0]) / GB_get_screen_height(&gameboy[0]);
}
geom.max_width = MAX_VIDEO_WIDTH * emulated_devices;
geom.max_height = MAX_VIDEO_HEIGHT * emulated_devices;
info->geometry = geom;
info->timing = timing;
}
void retro_set_environment(retro_environment_t cb)
{
bool categories_supported;
environ_cb = cb;
libretro_set_core_options(environ_cb, &categories_supported);
environ_cb(RETRO_ENVIRONMENT_SET_SUBSYSTEM_INFO, (void*)subsystems);
}
void retro_set_audio_sample(retro_audio_sample_t cb)
{
(void)cb;
}
void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
{
audio_batch_cb = cb;
}
void retro_set_input_poll(retro_input_poll_t cb)
{
input_poll_cb = cb;
}
void retro_set_input_state(retro_input_state_t cb)
{
input_state_cb = cb;
}
void retro_set_video_refresh(retro_video_refresh_t cb)
{
video_cb = cb;
}
void retro_reset(void)
{
check_variables();
for (int i = 0; i < emulated_devices; i++) {
init_for_current_model(i);
GB_reset(&gameboy[i]);
}
if (emulated_devices == 2) {
if (GB_get_unmultiplied_clock_rate(&gameboy[0]) != GB_get_unmultiplied_clock_rate(&gameboy[1])) {
audio_out = AUDIO_OUT_GB_1;
}
}
else {
audio_out = AUDIO_OUT_GB_1;
}
geometry_updated = true;
}
void retro_run(void)
{
bool updated = false;
if (!initialized) {
geometry_updated = false;
}
if (geometry_updated) {
struct retro_system_av_info info;
retro_get_system_av_info(&info);
environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &info.geometry);
geometry_updated = false;
}
if (!frame_buf) {
return;
}
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) {
check_variables();
}
if (emulated_devices == 2) {
GB_update_keys_status(&gameboy[0], 0);
GB_update_keys_status(&gameboy[1], 1);
}
else if (emulated_devices == 1 && (model[0] == GB_MODEL_SGB_PAL || model[0] == GB_MODEL_SGB_NTSC || model[0] == GB_MODEL_SGB2)) {
for (unsigned i = 0; i < 4; i++) {
GB_update_keys_status(&gameboy[0], i);
}
}
else {
GB_update_keys_status(&gameboy[0], 0);
}
vblank1_occurred = vblank2_occurred = false;
signed delta = 0;
if (emulated_devices == 2) {
while (!vblank1_occurred || !vblank2_occurred) {
if (delta >= 0) {
delta -= GB_run(&gameboy[0]);
}
else {
delta += GB_run(&gameboy[1]);
}
}
}
else {
GB_run_frame(&gameboy[0]);
}
if (emulated_devices == 2) {
if (screen_layout == LAYOUT_TOP_DOWN) {
video_cb(frame_buf,
GB_get_screen_width(&gameboy[0]),
GB_get_screen_height(&gameboy[0]) * emulated_devices,
GB_get_screen_width(&gameboy[0]) * sizeof(uint32_t));
}
else if (screen_layout == LAYOUT_LEFT_RIGHT) {
unsigned pitch = GB_get_screen_width(&gameboy[0]) * emulated_devices;
unsigned pixels_per_device = GB_get_screen_width(&gameboy[0]) * GB_get_screen_height(&gameboy[0]);
for (int y = 0; y < GB_get_screen_height(&gameboy[0]); y++) {
for (unsigned i = 0; i < emulated_devices; i++) {
memcpy(frame_buf_copy + y * pitch + GB_get_screen_width(&gameboy[0]) * i,
frame_buf + pixels_per_device * i + y * GB_get_screen_width(&gameboy[0]),
GB_get_screen_width(&gameboy[0]) * sizeof(uint32_t));
}
}
video_cb(frame_buf_copy, GB_get_screen_width(&gameboy[0]) * emulated_devices, GB_get_screen_height(&gameboy[0]), GB_get_screen_width(&gameboy[0]) * emulated_devices * sizeof(uint32_t));
}
}
else {
video_cb(frame_buf,
GB_get_screen_width(&gameboy[0]),
GB_get_screen_height(&gameboy[0]),
GB_get_screen_width(&gameboy[0]) * sizeof(uint32_t));
}
upload_output_audio_buffer();
initialized = true;
}
static enum rom_type check_rom_header(const uint8_t *data, size_t size)
{
enum rom_type type;
uint8_t cgb_flag;
uint8_t sgb_flag;
if (!data || (size < 0x146 + 1)) {
return ROM_TYPE_INVALID;
}
type = ROM_TYPE_DMG;
cgb_flag = data[0x143];
sgb_flag = data[0x146];
if ((cgb_flag == 0x80) || (cgb_flag == 0xC0)) {
type = ROM_TYPE_CGB;
}
if ((type == ROM_TYPE_DMG) && (sgb_flag == 0x03)) {
type = ROM_TYPE_SGB;
}
return type;
}
bool retro_load_game(const struct retro_game_info *info)
{
enum rom_type content_type = ROM_TYPE_INVALID;
const uint8_t *content_data = NULL;
size_t content_size;
if (info) {
content_data = (const uint8_t *)info->data;
content_size = info->size;
content_type = check_rom_header(content_data, content_size);
}
check_variables();
switch (content_type) {
case ROM_TYPE_DMG:
auto_model[0] = GB_MODEL_DMG_B;
auto_model[1] = GB_MODEL_DMG_B;
break;
case ROM_TYPE_SGB:
auto_model[0] = auto_sgb_enabled[0] ? auto_sgb_model[0] : GB_MODEL_DMG_B;
auto_model[1] = auto_sgb_enabled[1] ? auto_sgb_model[1] : GB_MODEL_DMG_B;
break;
case ROM_TYPE_CGB:
auto_model[0] = GB_MODEL_CGB_E;
auto_model[1] = GB_MODEL_CGB_E;
break;
case ROM_TYPE_INVALID:
default:
log_cb(RETRO_LOG_ERROR, "Invalid content\n");
return false;
}
frame_buf = (uint32_t *)malloc(MAX_VIDEO_PIXELS * emulated_devices * sizeof(uint32_t));
memset(frame_buf, 0, MAX_VIDEO_PIXELS * emulated_devices * sizeof(uint32_t));
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888;
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
log_cb(RETRO_LOG_ERROR, "XRGB8888 is not supported\n");
return false;
}
for (int i = 0; i < emulated_devices; i++) {
init_for_current_model(i);
GB_load_rom_from_buffer(&gameboy[i], content_data, content_size);
}
bool achievements = true;
environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &achievements);
if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble)) {
log_cb(RETRO_LOG_INFO, "Rumble environment supported\n");
}
else {
log_cb(RETRO_LOG_INFO, "Rumble environment not supported\n");
}
check_variables();
retro_set_memory_maps();
return true;
}
void retro_unload_game(void)
{
for (int i = 0; i < emulated_devices; i++) {
log_cb(RETRO_LOG_INFO, "Unloading GB: %d\n", emulated_devices);
GB_free(&gameboy[i]);
}
}
unsigned retro_get_region(void)
{
return RETRO_REGION_NTSC;
}
bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num_info)
{
if ((type == RETRO_GAME_TYPE_GAMEBOY_LINK_2P) && (num_info >= 2)) {
emulated_devices = 2;
}
else {
return false; /* all other types are unhandled for now */
}
check_variables();
frame_buf = (uint32_t*)malloc(emulated_devices * MAX_VIDEO_PIXELS * sizeof(uint32_t));
frame_buf_copy = (uint32_t*)malloc(emulated_devices * MAX_VIDEO_PIXELS * sizeof(uint32_t));
memset(frame_buf, 0, emulated_devices * MAX_VIDEO_PIXELS * sizeof(uint32_t));
memset(frame_buf_copy, 0, emulated_devices * MAX_VIDEO_PIXELS * sizeof(uint32_t));
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888;
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
log_cb(RETRO_LOG_ERROR, "XRGB8888 is not supported\n");
return false;
}
for (int i = 0; i < emulated_devices; i++) {
enum rom_type content_type = ROM_TYPE_INVALID;
const uint8_t *content_data = info[i].data;
size_t content_size = info[i].size;
content_type = check_rom_header(content_data, content_size);
switch (content_type) {
case ROM_TYPE_DMG:
auto_model[i] = GB_MODEL_DMG_B;
break;
case ROM_TYPE_SGB:
auto_model[i] = auto_sgb_enabled[i] ? auto_sgb_model[i] : GB_MODEL_DMG_B;
break;
case ROM_TYPE_CGB:
auto_model[i] = GB_MODEL_CGB_E;
break;
case ROM_TYPE_INVALID:
default:
log_cb(RETRO_LOG_ERROR, "Invalid content\n");
return false;
}
init_for_current_model(i);
GB_load_rom_from_buffer(&gameboy[i], content_data, content_size);
}
bool achievements = true;
environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &achievements);
if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble)) {
log_cb(RETRO_LOG_INFO, "Rumble environment supported\n");
}
else {
log_cb(RETRO_LOG_INFO, "Rumble environment not supported\n");
}
check_variables();
return true;
}
size_t retro_serialize_size(void)
{
static size_t maximum_save_size = 0;
if (maximum_save_size) {
return maximum_save_size * 2;
}
GB_gameboy_t temp;
GB_init(&temp, GB_MODEL_DMG_B);
maximum_save_size = GB_get_save_state_size(&temp);
GB_free(&temp);
GB_init(&temp, GB_MODEL_CGB_E);
maximum_save_size = MAX(maximum_save_size, GB_get_save_state_size(&temp));
GB_free(&temp);
GB_init(&temp, GB_MODEL_SGB2);
maximum_save_size = MAX(maximum_save_size, GB_get_save_state_size(&temp));
GB_free(&temp);
return maximum_save_size * 2;
}
bool retro_serialize(void *data, size_t size)
{
if (!initialized || !data) {
return false;
}
size_t offset = 0;
for (int i = 0; i < emulated_devices; i++) {
size_t state_size = GB_get_save_state_size(&gameboy[i]);
if (state_size > size) {
return false;
}
GB_save_state_to_buffer(&gameboy[i], ((uint8_t *) data) + offset);
offset += state_size;
size -= state_size;
}
return true;
}
bool retro_unserialize(const void *data, size_t size)
{
for (int i = 0; i < emulated_devices; i++) {
size_t state_size = GB_get_save_state_size(&gameboy[i]);
if (state_size > size) {
return false;
}
if (GB_load_state_from_buffer(&gameboy[i], data, state_size)) {
return false;
}
size -= state_size;
data = ((uint8_t *)data) + state_size;
}
return true;
}
void *retro_get_memory_data(unsigned type)
{
void *data = NULL;
if (emulated_devices == 1) {
switch (type) {
case RETRO_MEMORY_SYSTEM_RAM:
data = gameboy[0].ram;
break;
case RETRO_MEMORY_SAVE_RAM:
if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) {
data = gameboy[0].mbc_ram;
}
else {
data = NULL;
}
break;
case RETRO_MEMORY_VIDEO_RAM:
data = gameboy[0].vram;
break;
case RETRO_MEMORY_RTC:
if (gameboy[0].cartridge_type->has_battery) {
data = GB_GET_SECTION(&gameboy[0], rtc);
}
else {
data = NULL;
}
break;
default:
break;
}
}
else {
switch (type) {
case RETRO_MEMORY_GAMEBOY_1_SRAM:
if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) {
data = gameboy[0].mbc_ram;
}
else {
data = NULL;
}
break;
case RETRO_MEMORY_GAMEBOY_2_SRAM:
if (gameboy[1].cartridge_type->has_battery && gameboy[1].mbc_ram_size != 0) {
data = gameboy[1].mbc_ram;
}
else {
data = NULL;
}
break;
case RETRO_MEMORY_GAMEBOY_1_RTC:
if (gameboy[0].cartridge_type->has_battery) {
data = GB_GET_SECTION(&gameboy[0], rtc);
}
else {
data = NULL;
}
break;
case RETRO_MEMORY_GAMEBOY_2_RTC:
if (gameboy[1].cartridge_type->has_battery) {
data = GB_GET_SECTION(&gameboy[1], rtc);
}
else {
data = NULL;
}
break;
default:
break;
}
}
return data;
}
size_t retro_get_memory_size(unsigned type)
{
size_t size = 0;
if (emulated_devices == 1) {
switch (type) {
case RETRO_MEMORY_SYSTEM_RAM:
size = gameboy[0].ram_size;
break;
case RETRO_MEMORY_SAVE_RAM:
if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) {
size = gameboy[0].mbc_ram_size;
}
else {
size = 0;
}
break;
case RETRO_MEMORY_VIDEO_RAM:
size = gameboy[0].vram_size;
break;
case RETRO_MEMORY_RTC:
if (gameboy[0].cartridge_type->has_battery) {
size = GB_SECTION_SIZE(rtc);
}
else {
size = 0;
}
break;
default:
break;
}
}
else {
switch (type) {
case RETRO_MEMORY_GAMEBOY_1_SRAM:
if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) {
size = gameboy[0].mbc_ram_size;
}
else {
size = 0;
}
break;
case RETRO_MEMORY_GAMEBOY_2_SRAM:
if (gameboy[1].cartridge_type->has_battery && gameboy[1].mbc_ram_size != 0) {
size = gameboy[1].mbc_ram_size;
}
else {
size = 0;
}
break;
case RETRO_MEMORY_GAMEBOY_1_RTC:
if (gameboy[0].cartridge_type->has_battery) {
size = GB_SECTION_SIZE(rtc);
}
break;
case RETRO_MEMORY_GAMEBOY_2_RTC:
if (gameboy[1].cartridge_type->has_battery) {
size = GB_SECTION_SIZE(rtc);
}
break;
default:
break;
}
}
return size;
}
void retro_cheat_reset(void)
{}
void retro_cheat_set(unsigned index, bool enabled, const char *code)
{
(void)index;
(void)enabled;
(void)code;
}
|