forked from dolphin-emu/fifoplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
937 lines (799 loc) · 24.4 KB
/
main.cpp
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
#define ENABLE_CONSOLE 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <map>
#include <vector>
#include <stdint.h>
#include <iostream>
#include <machine/endian.h>
#include <malloc.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <unistd.h>
#include <fat.h>
#include <dirent.h>
#include <network.h>
#include "protocol.h"
#include "BPMemory.h"
#include "DffFile.h"
#include "FifoDataFile.h"
#include "OpcodeDecoding.h"
#include "FifoAnalyzer.h"
#include "VideoInterface.h"
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint8_t u8;
static u32 efbcopy_target = 0;
#define DEF_ALIGN 32
class aligned_buf
{
public:
aligned_buf() : buf(NULL), size(0), alignment(DEF_ALIGN) {}
aligned_buf(int alignment) : buf(NULL), size(0), alignment(alignment) {}
~aligned_buf()
{
free(buf);
}
aligned_buf(const aligned_buf& oth)
{
if (oth.buf)
{
buf = (u8*)memalign(oth.alignment, oth.size);
printf("copied to %p (%x) \n", buf, MEM_VIRTUAL_TO_PHYSICAL(buf));
memcpy(buf, oth.buf, oth.size);
}
else buf = NULL;
size = oth.size;
alignment = oth.alignment;
}
void resize(int new_size)
{
if (!buf)
{
buf = (u8*)memalign(alignment, new_size);
printf("allocated to %p (%x) - size %x \n", buf, MEM_VIRTUAL_TO_PHYSICAL(buf), new_size);
}
else
{
u8* old_buf = buf;
buf = (u8*)memalign(alignment, new_size);
memcpy(buf, old_buf, std::min(new_size, size));
printf("reallocated to %p (%x)\n", buf, MEM_VIRTUAL_TO_PHYSICAL(buf));
free(old_buf);
}
size = new_size;
}
u8* buf;
int size;
private:
int alignment;
};
std::map<u32, aligned_buf > memory_map; // map of memory chunks (indexed by starting address)
bool IntersectsMemoryRange(u32 start1, u32 size1, u32 start2, u32 size2)
{
return size1 && size2 && ((start1 >= start2 && start1 < start2 + size2) ||
(start2 >= start1 && start2 < start1 + size1));
}
// TODO: Needs to take care of alignment, too!
// Returns true if memory layout changed
bool PrepareMemoryLoad(u32 start_addr, u32 size)
{
bool ret = false;
// Make sure alignment of data inside the memory block is preserved
u32 off = start_addr % DEF_ALIGN;
start_addr = start_addr - off;
size += off;
std::vector<u32> affected_elements;
u32 new_start_addr = start_addr;
u32 new_end_addr = start_addr + size - 1;
// Find overlaps with existing memory chunks
for (auto it = memory_map.begin(); it != memory_map.end(); ++it)
{
if (IntersectsMemoryRange(it->first, it->second.size, start_addr, size))
{
affected_elements.push_back(it->first);
if (it->first < new_start_addr)
new_start_addr = it->first;
if (it->first + it->second.size > new_end_addr + 1)
new_end_addr = it->first + it->second.size - 1;
}
}
aligned_buf& new_memchunk(memory_map[new_start_addr]); // creates a new vector or uses the existing one
u32 new_size = new_end_addr - new_start_addr + 1;
// if the new memory range is inside an existing chunk, there's nothing to do
if (new_memchunk.size == new_size)
return false;
// resize chunk to required size, move old content to it, replace old arrays with new one
// NOTE: can't do reserve here because not the whole memory might be covered by existing memory chunks
new_memchunk.resize(new_size);
while (!affected_elements.empty())
{
u32 addr = affected_elements.back();
// first chunk is already in new_memchunk
if (addr != new_start_addr)
{
aligned_buf& src = memory_map[addr];
memcpy(&new_memchunk.buf[addr - new_start_addr], &src.buf[0], src.size);
memory_map.erase(addr);
ret = true;
}
affected_elements.pop_back();
}
// TODO: Handle critical case where memory allocation fails!
return ret;
}
// Must have been reserved via PrepareMemoryLoad first
u8* GetPointer(u32 addr)
{
for (auto it = memory_map.begin(); it != memory_map.end(); ++it)
if (addr >= it->first && addr < it->first + it->second.size)
return &it->second.buf[addr - it->first];
return NULL;
}
static u32 tex_addr[8] = {0};
static vu16* const _viReg = (u16*)0xCC002000;
using namespace VideoInterface;
void ApplyInitialState(const FifoData& fifo_data, u32* tex_addr, CPMemory& target_cpmem)
{
const std::vector<u32>& bpmem = fifo_data.bpmem;
const std::vector<u32>& cpmem = fifo_data.cpmem;
const std::vector<u32>& xfmem = fifo_data.xfmem;
const std::vector<u32>& xfregs = fifo_data.xfregs;
const std::vector<u16>& vimem = fifo_data.vimem;
for (unsigned int i = 0; i < fifo_data.bpmem.size(); ++i)
{
if ((i == BPMEM_TRIGGER_EFB_COPY
|| i == BPMEM_CLEARBBOX1
|| i == BPMEM_CLEARBBOX2
|| i == BPMEM_SETDRAWDONE
|| i == BPMEM_PE_TOKEN_ID // TODO: Sure that we want to skip this one?
|| i == BPMEM_PE_TOKEN_INT_ID
|| i == BPMEM_LOADTLUT0
|| i == BPMEM_LOADTLUT1
|| i == BPMEM_TEXINVALIDATE
|| i == BPMEM_PRELOAD_MODE
|| i == BPMEM_CLEAR_PIXEL_PERF))
continue;
u32 new_value = bpmem[i];
// Patch texture addresses
if ((i >= BPMEM_TX_SETIMAGE3 && i < BPMEM_TX_SETIMAGE3+4) ||
(i >= BPMEM_TX_SETIMAGE3_4 && i < BPMEM_TX_SETIMAGE3_4+4))
{
u32 tempval = le32toh(new_value);
TexImage3* img = (TexImage3*)&tempval;
u32 addr = img->image_base << 5;
u32 new_addr = MEM_VIRTUAL_TO_PHYSICAL(GetPointer(addr));
img->image_base = new_addr >> 5;
new_value = h32tole(tempval);
if (tex_addr)
{
if (i >= BPMEM_TX_SETIMAGE3 && i < BPMEM_TX_SETIMAGE3+4)
tex_addr[i - BPMEM_TX_SETIMAGE3] = new_addr;
else
tex_addr[4 + i - BPMEM_TX_SETIMAGE3_4] = new_addr;
}
}
#if ENABLE_CONSOLE!=1
wgPipe->U8 = 0x61;
wgPipe->U32 = (i<<24)|(le32toh(new_value)&0xffffff);
#endif
}
for (unsigned int i = 0; i < fifo_data.vimem.size(); ++i)
{
u16 new_value = vimem[i];
// Patch texture addresses
if ((2*i >= VI_FB_LEFT_TOP_HI && 2*i < VI_FB_LEFT_TOP_HI+4) ||
(2*i >= VI_FB_LEFT_BOTTOM_HI && 2*i < VI_FB_LEFT_BOTTOM_HI+4))
{
u32 tempval;
if (2*i == VI_FB_LEFT_TOP_HI)
{
// also swapping the two u16 values
tempval = ((u32)le16toh(vimem[VI_FB_LEFT_TOP_HI/2])) | ((u32)le16toh(vimem[VI_FB_LEFT_TOP_LO/2]) << 16);
}
else if (2*i == VI_FB_LEFT_BOTTOM_HI)
{
// also swapping the two u16 values
tempval = ((u32)le16toh(vimem[VI_FB_LEFT_BOTTOM_HI/2])) | ((u32)le16toh(vimem[VI_FB_LEFT_BOTTOM_LO/2]) << 16);
}
UVIFBInfoRegister* reg = (UVIFBInfoRegister*)&tempval;
u32 addr = (reg->POFF) ? (reg->FBB << 5) : reg->FBB;
u32 new_addr = MEM_VIRTUAL_TO_PHYSICAL(GetPointer(addr));
reg->FBB = (reg->POFF) ? (new_addr >> 5) : new_addr;
printf("XFB %s at %x (redirected to %x)\n", (2*i==VI_FB_LEFT_TOP_HI) ? "top" : "bottom", addr, new_addr);
u16 new_value_hi = h16tole(tempval >> 16);
u16 new_value_lo = h16tole(tempval & 0xFFFF);
#if ENABLE_CONSOLE!=1
// "raw" register poking broken for some reason, using the easy method for now...
/*u32 level;
_CPU_ISR_Disable(level);
_viReg[i] = new_value_hi;
_viReg[i+1] = new_value_lo;
_CPU_ISR_Restore(level);*/
// VIDEO_SetNextFramebuffer(GetPointer(new_addr));
VIDEO_SetNextFramebuffer(GetPointer(efbcopy_target)); // and there go our haxx..
#endif
++i; // increase i by 2
continue;
}
#if ENABLE_CONSOLE!=1
// TODO: Is this correct?
// _viReg[i] = new_value;
#endif
}
#if ENABLE_CONSOLE!=1
#define MLoadCPReg(addr, val) { wgPipe->U8 = 0x08; wgPipe->U8 = addr; wgPipe->U32 = val; LoadCPReg(addr, le32toh(cpmem[addr]), target_cpmem); }
MLoadCPReg(0x30, le32toh(cpmem[0x30]));
MLoadCPReg(0x40, le32toh(cpmem[0x40]));
MLoadCPReg(0x50, le32toh(cpmem[0x50]));
MLoadCPReg(0x60, le32toh(cpmem[0x60]));
for (int i = 0; i < 8; ++i)
{
MLoadCPReg(0x70 + i, le32toh(cpmem[0x70 + i]));
MLoadCPReg(0x80 + i, le32toh(cpmem[0x80 + i]));
MLoadCPReg(0x90 + i, le32toh(cpmem[0x90 + i]));
}
for (int i = 0; i < 16; ++i)
{
MLoadCPReg(0xa0 + i, le32toh(cpmem[0xa0 + i]));
MLoadCPReg(0xb0 + i, le32toh(cpmem[0xb0 + i]));
}
#undef MLoadCPReg
for (unsigned int i = 0; i < xfmem.size(); i += 16)
{
wgPipe->U8 = 0x10;
wgPipe->U32 = 0xf0000 | (i&0xffff); // load 16*4 bytes
for (int k = 0; k < 16; ++k)
wgPipe->U32 = le32toh(xfmem[i + k]);
}
for (unsigned int i = 0; i < xfregs.size(); ++i)
{
wgPipe->U8 = 0x10;
wgPipe->U32 = 0x1000 | (i&0x0fff);
wgPipe->U32 = le32toh(xfregs[i]);
}
// Flush WGP
for (int i = 0; i < 7; ++i)
wgPipe->U32 = 0;
wgPipe->U16 = 0;
wgPipe->U8 = 0;
#endif
}
#define DFF_FILENAME "sd:/dff/test.dff"
#define DEFAULT_FIFO_SIZE (256*1024)
static void *frameBuffer[2] = { NULL, NULL};
GXRModeObj *rmode;
u32 fb = 0;
u32 first_frame = 1;
void Init()
{
VIDEO_Init();
rmode = VIDEO_GetPreferredMode(NULL);
first_frame = 1;
fb = 0;
#if ENABLE_CONSOLE!=1
frameBuffer[0] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); // TODO: Shouldn't require manual framebuffer management!
frameBuffer[1] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
VIDEO_Configure(rmode);
VIDEO_SetNextFramebuffer(frameBuffer[fb]);
VIDEO_SetBlack(FALSE);
VIDEO_Flush();
VIDEO_WaitVSync();
if(rmode->viTVMode & VI_NON_INTERLACE)
VIDEO_WaitVSync();
#endif
fb ^= 1;
void *gp_fifo = NULL;
gp_fifo = memalign(32,DEFAULT_FIFO_SIZE);
memset(gp_fifo,0,DEFAULT_FIFO_SIZE);
GX_Init(gp_fifo,DEFAULT_FIFO_SIZE);
#if ENABLE_CONSOLE==1
console_init(frameBuffer[0],20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
#endif
WPAD_Init();
if(!fatInitDefault())
{
printf("fatInitDefault failed!\n");
}
net_init();
}
#include "mygx.h"
int ReadHandshake(int socket)
{
char data[4];
net_recv(socket, data, sizeof(data), 0);
uint32_t received_handshake = ntohl(*(uint32_t*)&data[0]);
if (received_handshake != handshake)
return RET_FAIL;
return RET_SUCCESS;
}
bool CheckIfHomePressed()
{
/* VIDEO_WaitVSync();
fb ^= 1;
*/
WPAD_ScanPads();
if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME)
{
return true;
}
return false;
}
void ReadStreamedDff(int socket)
{
int32_t n_size;
net_recv(socket, &n_size, 4, 0);
int32_t size = ntohl(n_size);
printf("About to read %d bytes of dff data!", size);
mkdir("sd:/dff", 0777);
FILE* file = fopen("sd:/dff/test.dff", "wb"); // TODO: Change!
if (file == NULL)
{
printf("Failed to open output file!\n");
}
for (; size > 0; )
{
char data[dff_stream_chunk_size];
ssize_t num_received = net_recv(socket, data, std::min(size,dff_stream_chunk_size), 0);
if (num_received == -1)
{
printf("Error in recv!\n");
}
else if (num_received > 0)
{
fwrite(data, num_received, 1, file);
size -= num_received;
}
// printf("%d bytes left to be read!\n", size);
CheckIfHomePressed();
}
printf ("Done reading :)\n");
fclose(file);
}
int WaitForConnection(int& server_socket)
{
int addrlen;
struct sockaddr_in my_name, peer_name;
int status;
server_socket = net_socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1)
{
printf("Failed to create server socket\n");
}
int yes = 1;
net_setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
memset(&my_name, 0, sizeof(my_name));
my_name.sin_family = AF_INET;
my_name.sin_port = htons(DFF_CONN_PORT);
my_name.sin_addr.s_addr = htonl(INADDR_ANY);
status = net_bind(server_socket, (struct sockaddr*)&my_name, sizeof(my_name));
if (status == -1)
{
printf("Failed to bind server socket\n");
}
status = net_listen(server_socket, 5); // TODO: Change second parameter..
if (status == -1)
{
printf("Failed to listen on server socket\n");
}
printf("Listening now!\n");
int client_socket = -1;
struct sockaddr_in client_info;
socklen_t ssize = sizeof(client_info);
int new_socket = net_accept(server_socket, (struct sockaddr*)&client_info, &ssize);
if (new_socket < 0)
{
printf("accept failed!\n");
}
else
{
client_socket = new_socket;
printf("accept succeeded and returned %d\n", client_socket);
}
return client_socket;
}
#define MSG_PEEK 0x02
void ReadCommandEnable(int socket, std::vector<AnalyzedFrameInfo>& analyzed_frames, bool enable)
{
char cmd;
u32 frame_idx;
u32 object;
u32 offset;
char data[12];
ssize_t numread = 0;
while (numread != sizeof(data))
numread += net_recv(socket, data+numread, sizeof(data)-numread, 0);
frame_idx = ntohl(*(u32*)&data[0]);
object = ntohl(*(u32*)&data[4]);
offset = ntohl(*(u32*)&data[8]);
printf("%s command %d in frame %d;\n", (enable)?"Enabled":"Disabled", offset, frame_idx);
AnalyzedFrameInfo& frame = analyzed_frames[frame_idx];
AnalyzedObject& obj = frame.objects[object];
for (int i = 0; i < obj.cmd_starts.size(); ++i)
{
if (obj.cmd_starts[i] == offset)
{
obj.cmd_enabled[i] = enable;
printf("%s command %d in frame %d, %d\n", (enable)?"Enabled":"Disabled", i, frame_idx, obj.cmd_enabled.size());
break;
}
}
}
void CheckForNetworkEvents(int server_socket, int client_socket, std::vector<AnalyzedFrameInfo>& analyzed_frames)
{
#if 0
fd_set readset;
FD_ZERO(&readset);
// FD_SET(server_socket, &readset);
// if (client_socket != -1)
FD_SET(client_socket, &readset);
// int maxfd = std::max(client_socket, server_socket);
int maxfd = client_socket;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
char data[12];
int ret = net_select(maxfd+1, &readset, NULL, NULL, &timeout); // TODO: Is this compatible with winsocks?
if (ret <= 0)
{
if (ret < 0)
printf("select returned %d\n", ret);
return;
}
/* if (FD_ISSET(server_socket, &readset))
{
int new_socket = net_accept(server_socket, NULL, NULL);
if (new_socket < 0)
{
qDebug() << "accept failed";
}
else client_socket = new_socket;
}*/
#endif
struct pollsd fds[2];
memset(fds, 0, sizeof(fds));
// fds[0].socket = server_socket;
fds[0].socket = client_socket;
fds[0].events = POLLIN;
int nfds = 1;
int timeout = 1; // TODO: Set to zero
int ret;
do {
ret = net_poll(fds, nfds, timeout);
if (ret < 0)
{
printf("poll returned error %d\n", ret);
return;
}
if (ret == 0)
{
printf("timeout :(\n");
// timeout
return;
}
char cmd;
ssize_t numread = net_recv(client_socket, &cmd, 1, 0);
printf("Peeked command %d\n", cmd);
switch (cmd)
{
case CMD_HANDSHAKE:
if (RET_SUCCESS == ReadHandshake(client_socket))
printf("Successfully exchanged handshake token!\n");
else
printf("Failed to exchange handshake token!\n");
// TODO: should probably write a handshake in return, but ... I'm lazy
break;
case CMD_STREAM_DFF:
//ReadStreamedDff(client_socket);
break;
case CMD_ENABLE_COMMAND:
case CMD_DISABLE_COMMAND:
ReadCommandEnable(client_socket, analyzed_frames, (cmd == CMD_ENABLE_COMMAND) ? true : false);
break;
default:
printf("Received unknown command: %d\n", cmd);
break;
}
printf("Looping again\n");
timeout = 100;
} while (ret > 0);
}
int main()
{
Init();
printf("Init done!\n");
int server_socket;
int client_socket = WaitForConnection(server_socket);
u8 dummy;
net_recv(client_socket, &dummy, 1, 0);
if (RET_SUCCESS == ReadHandshake(client_socket))
printf("Successfully exchanged handshake token!\n");
else
printf("Failed to exchanged handshake token!\n");
net_recv(client_socket, &dummy, 1, 0);
ReadStreamedDff(client_socket);
FifoData fifo_data;
LoadDffData(DFF_FILENAME, fifo_data);
printf("Loaded dff data\n");
FifoDataAnalyzer analyzer;
std::vector<AnalyzedFrameInfo> analyzed_frames;
analyzer.AnalyzeFrames(fifo_data, analyzed_frames);
printf("Analyzed dff data\n");
CPMemory cpmem; // TODO: Should be removed...
bool processing = true;
int first_frame = 0;
int last_frame = first_frame + fifo_data.frames.size()-1;
int cur_frame = first_frame;
while (processing)
{
CheckForNetworkEvents(server_socket, client_socket, analyzed_frames);
FifoFrameData& cur_frame_data = fifo_data.frames[cur_frame];
AnalyzedFrameInfo& cur_analyzed_frame = analyzed_frames[cur_frame];
if (cur_frame == 0) // TODO: Check for first_frame instead and apply previous state changes
{
for (unsigned int frameNum = 0; frameNum < fifo_data.frames.size(); ++frameNum)
{
const FifoFrameData &frame = fifo_data.frames[frameNum];
for (unsigned int i = 0; i < frame.memoryUpdates.size(); ++i)
{
PrepareMemoryLoad(frame.memoryUpdates[i].address, frame.memoryUpdates[i].dataSize);
//if (early_mem_updates)
// memcpy(GetPointer(frame.memoryUpdates[i].address), &frame.memoryUpdates[i].data[0], frame.memoryUpdates[i].data.size());
//DCFlushRange(GetPointer(frame.memoryUpdates[i].address), frame.memoryUpdates[i].dataSize);
}
}
ApplyInitialState(fifo_data, tex_addr, cpmem);
}
u32 last_pos = 0;
for (std::vector<AnalyzedObject>::iterator cur_object = cur_analyzed_frame.objects.begin();
cur_object != cur_analyzed_frame.objects.end(); ++cur_object)
{
for (std::vector<u32>::iterator cur_command = cur_object->cmd_starts.begin();
cur_command != cur_object->cmd_starts.end(); ++cur_command)
{
const int cmd_index = cur_command-cur_object->cmd_starts.begin();
u8* cmd_data = &cur_frame_data.fifoData[*cur_command];
const FifoFrameData &frame = fifo_data.frames[cur_frame];
for (unsigned int update = 0; update < frame.memoryUpdates.size(); ++update)
{
if ((!last_pos || frame.memoryUpdates[update].fifoPosition > last_pos) && frame.memoryUpdates[update].fifoPosition <= *cur_command)
{
// PrepareMemoryLoad(frame.memoryUpdates[update].address, frame.memoryUpdates[update].dataSize);
fseek(fifo_data.file, frame.memoryUpdates[update].dataOffset, SEEK_SET);
fread(GetPointer(frame.memoryUpdates[update].address), frame.memoryUpdates[update].dataSize, 1, fifo_data.file);
// DCFlushRange expects aligned addresses
u32 off = frame.memoryUpdates[update].address % DEF_ALIGN;
DCFlushRange(GetPointer(frame.memoryUpdates[update].address - off), frame.memoryUpdates[update].dataSize+off);
}
}
last_pos = *cur_command;
if (!cur_object->cmd_enabled[cmd_index])
continue;
if (cmd_data[0] == GX_LOAD_BP_REG)
{
// Patch texture addresses
if ((cmd_data[1] >= BPMEM_TX_SETIMAGE3 && cmd_data[1] < BPMEM_TX_SETIMAGE3+4) ||
(cmd_data[1] >= BPMEM_TX_SETIMAGE3_4 && cmd_data[1] < BPMEM_TX_SETIMAGE3_4+4))
{
u32 tempval = /*be32toh*/(*(u32*)&cmd_data[1]);
TexImage3* img = (TexImage3*)&tempval;
u32 addr = img->image_base << 5; // TODO: Proper mask?
u32 new_addr = MEM_VIRTUAL_TO_PHYSICAL(GetPointer(addr));
img->image_base = new_addr >> 5;
u32 new_value = /*h32tobe*/(tempval);
#if ENABLE_CONSOLE!=1
wgPipe->U8 = 0x61;
wgPipe->U32 = ((u32)cmd_data[1]<<24)|(/*be32toh*/(new_value)&0xffffff);
#endif
if (cmd_data[1] >= BPMEM_TX_SETIMAGE3 &&
cmd_data[1] < BPMEM_TX_SETIMAGE3+4)
tex_addr[cmd_data[1] - BPMEM_TX_SETIMAGE3] = new_addr;
else
tex_addr[4 + cmd_data[1] - BPMEM_TX_SETIMAGE3_4] = new_addr;
}
else if (cmd_data[1] == BPMEM_PRELOAD_ADDR)
{
// TODO
#if ENABLE_CONSOLE!=1
wgPipe->U8 = 0x61;
wgPipe->U8 = cmd_data[1];
wgPipe->U8 = cmd_data[2];
wgPipe->U8 = cmd_data[3];
wgPipe->U8 = cmd_data[4];
#endif
}
else if (cmd_data[1] == BPMEM_LOADTLUT0)
{
#if 1
// TODO: Untested
u32 tempval = /*be32toh*/(*(u32*)&cmd_data[1]);
u32 addr = tempval << 5; // TODO: Proper mask?
u32 new_addr = MEM_VIRTUAL_TO_PHYSICAL(GetPointer(addr));
u32 new_value = new_addr >> 5;
#if ENABLE_CONSOLE!=1
wgPipe->U8 = cmd_data[0];
wgPipe->U32 = (BPMEM_LOADTLUT0<<24)|(/*be32toh*/(new_value)&0xffffff);
#endif
#endif
}
else if (cmd_data[1] == BPMEM_EFB_ADDR)
{
u32 tempval = /*be32toh*/(*(u32*)&cmd_data[1]);
u32 addr = (tempval & 0xFFFFFF) << 5; // TODO
efbcopy_target = addr;
#if ENABLE_CONSOLE!=1
wgPipe->U8 = 0x61;
wgPipe->U8 = cmd_data[1];
wgPipe->U8 = cmd_data[2];
wgPipe->U8 = cmd_data[3];
wgPipe->U8 = cmd_data[4];
#endif
}
else if (cmd_data[1] == BPMEM_TRIGGER_EFB_COPY)
{
u32 tempval = /*be32toh*/(*(u32*)&cmd_data[1]);
UPE_Copy* copy = (UPE_Copy*)&tempval;
// Version 1 did not support EFB->XFB copies
if (fifo_data.version >= 2 || !copy->copy_to_xfb)
{
bool update_textures = PrepareMemoryLoad(efbcopy_target, 640*480*4); // TODO: Size!!
u32 new_addr = MEM_VIRTUAL_TO_PHYSICAL(GetPointer(efbcopy_target));
u32 new_value = /*h32tobe*/((BPMEM_EFB_ADDR<<24) | (new_addr >> 5));
#if ENABLE_CONSOLE!=1
// Update target address
wgPipe->U8 = 0x61;
wgPipe->U32 = (BPMEM_EFB_ADDR<<24)|(/*be32toh*/(new_value)&0xffffff);
#endif
// Gotta fix texture offsets if memory map layout changed
if (update_textures)
{
for (int k = 0; k < 8; ++k)
{
u32 new_addr = MEM_VIRTUAL_TO_PHYSICAL(GetPointer(tex_addr[k]));
u32 new_value = /*h32tobe*/(new_addr>>5);
#if ENABLE_CONSOLE!=1
wgPipe->U8 = 0x61;
u32 reg = (k < 4) ? (BPMEM_TX_SETIMAGE3+k) : (BPMEM_TX_SETIMAGE3_4+(k-4));
wgPipe->U32 = (reg<<24)|(/*be32toh*/(new_value)&0xffffff);
#endif
}
}
}
#if ENABLE_CONSOLE!=1
wgPipe->U8 = 0x61;
wgPipe->U8 = cmd_data[1];
wgPipe->U8 = cmd_data[2];
wgPipe->U8 = cmd_data[3];
wgPipe->U8 = cmd_data[4];
#endif
}
else
{
#if ENABLE_CONSOLE!=1
wgPipe->U8 = 0x61;
wgPipe->U8 = cmd_data[1];
wgPipe->U8 = cmd_data[2];
wgPipe->U8 = cmd_data[3];
wgPipe->U8 = cmd_data[4];
#endif
}
}
else if (cmd_data[0] == GX_LOAD_CP_REG)
{
u8 cmd2 = cmd_data[1];
u32 value = *(u32*)&cmd_data[2]; // TODO: Endiannes (only works on Wii)
if ((cmd2 & 0xF0) == 0xA0) // TODO: readability!
value = MEM_VIRTUAL_TO_PHYSICAL(GetPointer(value));
#if ENABLE_CONSOLE!=1
wgPipe->U8 = GX_LOAD_CP_REG;
wgPipe->U8 = cmd_data[1];
wgPipe->U32 = value;
#endif
LoadCPReg(cmd2, value, cpmem);
}
else if (cmd_data[0] == GX_LOAD_XF_REG)
{
// Load data directly instead of going through the loop again for no reason
u32 cmd2 = *(u32*)&cmd_data[1]; // TODO: Endianness (only works on Wii)
u8 streamSize = ((cmd2 >> 16) & 15) + 1;
#if ENABLE_CONSOLE!=1
wgPipe->U8 = GX_LOAD_XF_REG;
wgPipe->U32 = cmd2;
for (int byte = 0; byte < streamSize * 4; ++byte)
wgPipe->U8 = cmd_data[5+byte];
#endif
}
else if(cmd_data[0] == GX_LOAD_INDX_A ||
cmd_data[0] == GX_LOAD_INDX_B ||
cmd_data[0] == GX_LOAD_INDX_C ||
cmd_data[0] == GX_LOAD_INDX_D)
{
#if ENABLE_CONSOLE!=1
wgPipe->U8 = cmd_data[0];
wgPipe->U8 = cmd_data[1];
wgPipe->U8 = cmd_data[2];
wgPipe->U8 = cmd_data[3];
wgPipe->U8 = cmd_data[4];
#endif
}
else if (cmd_data[0] & 0x80)
{
u32 vtxAttrGroup = cmd_data[0] & GX_VAT_MASK;
int vertexSize = CalculateVertexSize(vtxAttrGroup, cpmem);
u16 streamSize = *(u16*)&cmd_data[1]; // TODO: Endianness (only works on Wii)
#if ENABLE_CONSOLE!=1
wgPipe->U8 = cmd_data[0];
wgPipe->U16 = streamSize;
for (int byte = 0; byte < streamSize * vertexSize; ++byte)
wgPipe->U8 = cmd_data[3+byte];
#endif
}
else
{
u32 size = cur_object->last_cmd_byte - *cur_command + 1;
if (cur_command+1 != cur_object->cmd_starts.end() && size > *(cur_command+1)-*cur_command)
size = *(cur_command+1)-*cur_command;
#if ENABLE_CONSOLE!=1
for (u32 addr = 0; addr < size; ++addr) {
// TODO: Push u32s instead
wgPipe->U8 = cmd_data[addr];
}
#endif
}
}
}
// TODO: Flush WGPipe
#if ENABLE_CONSOLE!=1
if (fifo_data.version < 2)
{
// finish frame for legacy dff files
//
// Note that GX_CopyDisp(frameBuffer[fb],GX_TRUE) uses an internal state
// which is out of sync with the dff_data, so we're manually writing
// to the EFB copy registers instead.
wgPipe->U8 = GX_LOAD_BP_REG;
wgPipe->U32 = (BPMEM_EFB_ADDR << 24) | ((MEM_VIRTUAL_TO_PHYSICAL(frameBuffer[fb]) >> 5) & 0xFFFFFF);
UPE_Copy copy;
copy.Hex = 0;
copy.clear = 1;
copy.copy_to_xfb = 1;
wgPipe->U8 = GX_LOAD_BP_REG;
wgPipe->U32 = (BPMEM_TRIGGER_EFB_COPY << 24) | copy.Hex;
VIDEO_SetNextFramebuffer(frameBuffer[fb]);
if (first_frame)
{
VIDEO_SetBlack(FALSE);
first_frame = 0;
}
}
#endif
VIDEO_Flush();
VIDEO_WaitVSync();
fb ^= 1;
// TODO: Menu stuff
// reset GX state
// draw menu
// restore GX state
// input checking
// A = select menu point
// B = menu back
// plus = pause
// minus = hide menu
// home = stop
WPAD_ScanPads();
// if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME)
// processing = false;
if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME)
{
printf("\n");
fclose(fifo_data.file);
exit(0);
}
++cur_frame;
cur_frame = first_frame + ((cur_frame-first_frame) % (last_frame-first_frame+1));
}
fclose(fifo_data.file);
return 0;
}