Skip to content

Commit cad1ca0

Browse files
authored
Feature/added execution guess gs psmt (#104)
* fix: CRITICAL fix on code gen on generating BEQ translation, I added a small yeld because goto could spin forever and monopolize guest execution * feat: add scratchpad alias base and improve scratchpad address handling * feat: added debug logging on GifArbiter for submit and drain operations * feat: add interrupt and thread management syscall implementations fix: change some IDs calls to match ps2sdk * feat: added vif1 logs * feat: added logs on gs gpu feat: added performLocalToLocalTransfer to GS emulation path for TRXDIR = 2. (emulates the PS2 GS “copy this rectangle from one place in VRAM to another”) * feat: added PSMT8 and refactor PSMT4 * feat: some identation on vu1 feat: added some logs on vu1 * feat: added GuestExecutionScope to temporarily stop owning guest execution, then restore it exactly as it was. feat: added vsync wizardry feat: added some regression test * fix: fix gs logger * feat: remove extra logs I think they will help no one feat: move join all threads to prevent the app to get stuck on close, but now it random crash on closing feat: one more small test on psmt4 to try fix ghosting on re code veronica * feat: added a small case for exporter from ghidra for metal slug 3 * feat: added ugly code to pass on test
1 parent 7ca6a86 commit cad1ca0

32 files changed

+3933
-676
lines changed

ps2xRecomp/src/lib/code_generator.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,19 @@ namespace ps2recomp
227227
const uint32_t branchPc = branchInst.address;
228228
const uint32_t delayPc = branchInst.address + 4u;
229229
const uint32_t fallthroughPc = branchInst.address + 8u;
230+
auto emitInternalTarget = [&](uint32_t target, uint32_t sourcePc, std::string_view indent)
231+
{
232+
ss << fmt::format("{}ctx->pc = 0x{:X}u;\n", indent, target);
233+
if (target <= sourcePc)
234+
{
235+
ss << fmt::format("{}runtime->cooperativeGuestYield();\n", indent);
236+
ss << fmt::format("{}goto label_{:x};\n", indent, target);
237+
}
238+
else
239+
{
240+
ss << fmt::format("{}goto label_{:x};\n", indent, target);
241+
}
242+
};
230243

231244
std::vector<uint32_t> sortedInternalTargets;
232245
if (branchInst.opcode == OPCODE_SPECIAL &&
@@ -293,8 +306,7 @@ namespace ps2recomp
293306

294307
if (internalTargets.contains(target))
295308
{
296-
ss << fmt::format(" ctx->pc = 0x{:X}u;\n", target);
297-
ss << fmt::format(" goto label_{:x};\n", target);
309+
emitInternalTarget(target, branchPc, " ");
298310
}
299311
else
300312
{
@@ -579,8 +591,7 @@ namespace ps2recomp
579591

580592
if (internalTargets.contains(target))
581593
{
582-
ss << fmt::format(" ctx->pc = 0x{:X}u;\n", target);
583-
ss << fmt::format(" goto label_{:x};\n", target);
594+
emitInternalTarget(target, branchPc, " ");
584595
}
585596
else
586597
{
@@ -606,8 +617,7 @@ namespace ps2recomp
606617
ss << " if (" << branchTakenVar << ") {\n";
607618
if (internalTargets.contains(target))
608619
{
609-
ss << fmt::format(" ctx->pc = 0x{:X}u;\n", target);
610-
ss << fmt::format(" goto label_{:x};\n", target);
620+
emitInternalTarget(target, branchPc, " ");
611621
}
612622
else
613623
{

ps2xRecomp/tools/ghidra/ExportPS2Functions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ private boolean hasCallableLabelReference(Address address) {
376376

377377
MemoryBlock fromBlock = currentProgram.getMemory().getBlock(from);
378378
if (fromBlock == null || !fromBlock.isExecute()) {
379-
return true;
379+
continue; // lets ignore DATA/non-code refs
380380
}
381381
}
382382

ps2xRuntime/include/ps2_gs_gpu.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ class GS
216216
const uint8_t *lockDisplaySnapshot(uint32_t &outSize);
217217
void unlockDisplaySnapshot();
218218
uint32_t getLastDisplayBaseBytes() const;
219+
const GSFrameReg &getContextFrame(int index) const
220+
{
221+
return m_ctx[(index != 0) ? 1 : 0].frame;
222+
}
219223

220224
uint32_t consumeLocalToHostBytes(uint8_t *dst, uint32_t maxBytes);
221225

@@ -227,6 +231,7 @@ class GS
227231
void vertexKick(bool drawing);
228232

229233
void processImageData(const uint8_t *data, uint32_t sizeBytes);
234+
void performLocalToLocalTransfer();
230235
void performLocalToHostToBuffer();
231236

232237
GSContext &activeContext();

ps2xRuntime/include/ps2_gs_psmt4.h

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,47 @@
66
namespace GSPSMT4
77
{
88

9-
static const uint8_t blockTable4[8][4] = {
10-
{ 0, 2, 8, 10 },
11-
{ 1, 3, 9, 11 },
12-
{ 4, 6, 12, 14 },
13-
{ 5, 7, 13, 15 },
14-
{ 16, 18, 24, 26 },
15-
{ 17, 19, 25, 27 },
16-
{ 20, 22, 28, 30 },
17-
{ 21, 23, 29, 31 },
18-
};
19-
20-
static const uint16_t columnTable4[16][32] = {
21-
{ 0, 8, 32, 40, 64, 72, 96, 104, 2, 10, 34, 42, 66, 74, 98, 106, 4, 12, 36, 44, 68, 76, 100, 108, 6, 14, 38, 46, 70, 78, 102, 110 },
22-
{ 16, 24, 48, 56, 80, 88, 112, 120, 18, 26, 50, 58, 82, 90, 114, 122, 20, 28, 52, 60, 84, 92, 116, 124, 22, 30, 54, 62, 86, 94, 118, 126 },
23-
{ 65, 73, 97, 105, 1, 9, 33, 41, 67, 75, 99, 107, 3, 11, 35, 43, 69, 77, 101, 109, 5, 13, 37, 45, 71, 79, 103, 111, 7, 15, 39, 47 },
24-
{ 81, 89, 113, 121, 17, 25, 49, 57, 83, 91, 115, 123, 19, 27, 51, 59, 85, 93, 117, 125, 21, 29, 53, 61, 87, 95, 119, 127, 23, 31, 55, 63 },
25-
{ 192, 200, 224, 232, 128, 136, 160, 168, 194, 202, 226, 234, 130, 138, 162, 170, 196, 204, 228, 236, 132, 140, 164, 172, 198, 206, 230, 238, 134, 142, 166, 174 },
26-
{ 208, 216, 240, 248, 144, 152, 176, 184, 210, 218, 242, 250, 146, 154, 178, 186, 212, 220, 244, 252, 148, 156, 180, 188, 214, 222, 246, 254, 150, 158, 182, 190 },
27-
{ 129, 137, 161, 169, 193, 201, 225, 233, 131, 139, 163, 171, 195, 203, 227, 235, 133, 141, 165, 173, 197, 205, 229, 237, 135, 143, 167, 175, 199, 207, 231, 239 },
28-
{ 145, 153, 177, 185, 209, 217, 241, 249, 147, 155, 179, 187, 211, 219, 243, 251, 149, 157, 181, 189, 213, 221, 245, 253, 151, 159, 183, 191, 215, 223, 247, 255 },
29-
{ 256, 264, 288, 296, 320, 328, 352, 360, 258, 266, 290, 298, 322, 330, 354, 362, 260, 268, 292, 300, 324, 332, 356, 364, 262, 270, 294, 302, 326, 334, 358, 366 },
30-
{ 272, 280, 304, 312, 336, 344, 368, 376, 274, 282, 306, 314, 338, 346, 370, 378, 276, 284, 308, 316, 340, 348, 372, 380, 278, 286, 310, 318, 342, 350, 374, 382 },
31-
{ 321, 329, 353, 361, 257, 265, 289, 297, 323, 331, 355, 363, 259, 267, 291, 299, 325, 333, 357, 365, 261, 269, 293, 301, 327, 335, 359, 367, 263, 271, 295, 303 },
32-
{ 337, 345, 369, 377, 273, 281, 305, 313, 339, 347, 371, 379, 275, 283, 307, 315, 341, 349, 373, 381, 277, 285, 309, 317, 343, 351, 375, 383, 279, 287, 311, 319 },
33-
{ 448, 456, 480, 488, 384, 392, 416, 424, 450, 458, 482, 490, 386, 394, 418, 426, 452, 460, 484, 492, 388, 396, 420, 428, 454, 462, 486, 494, 390, 398, 422, 430 },
34-
{ 464, 472, 496, 504, 400, 408, 432, 440, 466, 474, 498, 506, 402, 410, 434, 442, 468, 476, 500, 508, 404, 412, 436, 444, 470, 478, 502, 510, 406, 414, 438, 446 },
35-
{ 385, 393, 417, 425, 449, 457, 481, 489, 387, 395, 419, 427, 451, 459, 483, 491, 389, 397, 421, 429, 453, 461, 485, 493, 391, 399, 423, 431, 455, 463, 487, 495 },
36-
{ 401, 409, 433, 441, 465, 473, 497, 505, 403, 411, 435, 443, 467, 475, 499, 507, 405, 413, 437, 445, 469, 477, 501, 509, 407, 415, 439, 447, 471, 479, 503, 511 },
37-
};
38-
39-
inline uint32_t blockIdPSMT4(uint32_t block, uint32_t width, uint32_t x, uint32_t y)
40-
{
41-
return block + ((y >> 2) & ~0x1Fu) * (width >> 7) + ((x >> 2) & ~0x1Fu)
42-
+ blockTable4[(y >> 4) & 7][(x >> 5) & 3];
43-
}
44-
45-
inline uint32_t addrPSMT4(uint32_t block, uint32_t width, uint32_t x, uint32_t y)
46-
{
47-
uint32_t page = (block >> 5) + (y >> 7) * (width >> 1) + (x >> 7);
48-
uint32_t blk = block & 0x1Fu;
49-
uint32_t yy = y & 0x7Fu;
50-
uint32_t xx = x & 0x7Fu;
51-
uint32_t blockId = blk + blockTable4[(yy >> 4) & 7][(xx >> 5) & 3];
52-
uint32_t column = columnTable4[yy & 15u][xx & 31u];
53-
uint32_t offset = (blockId << 9) + column;
54-
return (page << 14) + offset;
55-
}
9+
static const uint16_t columnTable4[16][32] = {
10+
{0, 8, 16, 24, 32, 40, 48, 56, 2, 10, 18, 26, 34, 42, 50, 58, 4, 12, 20, 28, 36, 44, 52, 60, 6, 14, 22, 30, 38, 46, 54, 62},
11+
{512, 520, 528, 536, 544, 552, 560, 568, 514, 522, 530, 538, 546, 554, 562, 570, 516, 524, 532, 540, 548, 556, 564, 572, 518, 526, 534, 542, 550, 558, 566, 574},
12+
{33, 41, 49, 57, 1, 9, 17, 25, 35, 43, 51, 59, 3, 11, 19, 27, 37, 45, 53, 61, 5, 13, 21, 29, 39, 47, 55, 63, 7, 15, 23, 31},
13+
{545, 553, 561, 569, 513, 521, 529, 537, 547, 555, 563, 571, 515, 523, 531, 539, 549, 557, 565, 573, 517, 525, 533, 541, 551, 559, 567, 575, 519, 527, 535, 543},
14+
{1056, 1064, 1072, 1080, 1024, 1032, 1040, 1048, 1058, 1066, 1074, 1082, 1026, 1034, 1042, 1050, 1060, 1068, 1076, 1084, 1028, 1036, 1044, 1052, 1062, 1070, 1078, 1086, 1030, 1038, 1046, 1054},
15+
{1568, 1576, 1584, 1592, 1536, 1544, 1552, 1560, 1570, 1578, 1586, 1594, 1538, 1546, 1554, 1562, 1572, 1580, 1588, 1596, 1540, 1548, 1556, 1564, 1574, 1582, 1590, 1598, 1542, 1550, 1558, 1566},
16+
{1025, 1033, 1041, 1049, 1057, 1065, 1073, 1081, 1027, 1035, 1043, 1051, 1059, 1067, 1075, 1083, 1029, 1037, 1045, 1053, 1061, 1069, 1077, 1085, 1031, 1039, 1047, 1055, 1063, 1071, 1079, 1087},
17+
{1537, 1545, 1553, 1561, 1569, 1577, 1585, 1593, 1539, 1547, 1555, 1563, 1571, 1579, 1587, 1595, 1541, 1549, 1557, 1565, 1573, 1581, 1589, 1597, 1543, 1551, 1559, 1567, 1575, 1583, 1591, 1599},
18+
{2048, 2056, 2064, 2072, 2080, 2088, 2096, 2104, 2050, 2058, 2066, 2074, 2082, 2090, 2098, 2106, 2052, 2060, 2068, 2076, 2084, 2092, 2100, 2108, 2054, 2062, 2070, 2078, 2086, 2094, 2102, 2110},
19+
{2560, 2568, 2576, 2584, 2592, 2600, 2608, 2616, 2562, 2570, 2578, 2586, 2594, 2602, 2610, 2618, 2564, 2572, 2580, 2588, 2596, 2604, 2612, 2620, 2566, 2574, 2582, 2590, 2598, 2606, 2614, 2622},
20+
{2081, 2089, 2097, 2105, 2049, 2057, 2065, 2073, 2083, 2091, 2099, 2107, 2051, 2059, 2067, 2075, 2085, 2093, 2101, 2109, 2053, 2061, 2069, 2077, 2087, 2095, 2103, 2111, 2055, 2063, 2071, 2079},
21+
{2593, 2601, 2609, 2617, 2561, 2569, 2577, 2585, 2595, 2603, 2611, 2619, 2563, 2571, 2579, 2587, 2597, 2605, 2613, 2621, 2565, 2573, 2581, 2589, 2599, 2607, 2615, 2623, 2567, 2575, 2583, 2591},
22+
{3104, 3112, 3120, 3128, 3072, 3080, 3088, 3096, 3106, 3114, 3122, 3130, 3074, 3082, 3090, 3098, 3108, 3116, 3124, 3132, 3076, 3084, 3092, 3100, 3110, 3118, 3126, 3134, 3078, 3086, 3094, 3102},
23+
{3616, 3624, 3632, 3640, 3584, 3592, 3600, 3608, 3618, 3626, 3634, 3642, 3586, 3594, 3602, 3610, 3620, 3628, 3636, 3644, 3588, 3596, 3604, 3612, 3622, 3630, 3638, 3646, 3590, 3598, 3606, 3614},
24+
{3073, 3081, 3089, 3097, 3105, 3113, 3121, 3129, 3075, 3083, 3091, 3099, 3107, 3115, 3123, 3131, 3077, 3085, 3093, 3101, 3109, 3117, 3125, 3133, 3079, 3087, 3095, 3103, 3111, 3119, 3127, 3135},
25+
{3585, 3593, 3601, 3609, 3617, 3625, 3633, 3641, 3587, 3595, 3603, 3611, 3619, 3627, 3635, 3643, 3589, 3597, 3605, 3613, 3621, 3629, 3637, 3645, 3591, 3599, 3607, 3615, 3623, 3631, 3639, 3647},
26+
};
27+
28+
inline uint32_t pageLocalNibbleOffset(uint32_t x, uint32_t y)
29+
{
30+
uint32_t yy = y & 0x7Fu;
31+
uint32_t xx = x & 0x7Fu;
32+
uint32_t blockBase = (((xx >> 5) & 3u) << 12) +
33+
(((yy >> 4) & 7u) << 6);
34+
return blockBase + columnTable4[yy & 15u][xx & 31u];
35+
}
36+
37+
inline uint32_t addrPSMT4(uint32_t block, uint32_t width, uint32_t x, uint32_t y)
38+
{
39+
const uint32_t pagesPerRow = ((width >> 1u) != 0u) ? (width >> 1u) : 1u;
40+
const uint32_t localNibble = pageLocalNibbleOffset(x, y);
41+
const uint32_t localByte = localNibble >> 1u;
42+
const uint32_t localRow = localByte >> 8u;
43+
const uint32_t localColumnByte = localByte & 0xFFu;
44+
const uint32_t globalByte =
45+
block * 256u +
46+
(((y >> 7u) * 32u + localRow) * (pagesPerRow * 256u)) +
47+
((x >> 7u) * 256u + localColumnByte);
48+
return (globalByte << 1u) | (localNibble & 1u);
49+
}
5650

5751
}
5852

0 commit comments

Comments
 (0)