1
// Copyright 2013 Dolphin Emulator Project
2
// Licensed under GPLv2
3
// Refer to the license.txt file included.
7
#include "DataReader.h"
9
#include "OpcodeDecoder.h"
10
#include "BPMemLoader.h"
11
#include "CPMemLoader.h"
12
#include "XFMemLoader.h"
13
#include "SWVertexLoader.h"
14
#include "SWStatistics.h"
15
#include "DebugUtil.h"
16
#include "SWCommandProcessor.h"
17
#include "CPMemLoader.h"
18
#include "SWVideoConfig.h"
19
#include "HW/Memmap.h"
21
typedef void (*DecodingFunction)(u32);
23
namespace OpcodeDecoder
25
static DecodingFunction currentFunction = NULL;
26
static u32 minCommandSize;
27
static u16 streamSize;
28
static u16 streamAddress;
29
static bool readOpcode;
30
static SWVertexLoader vertexLoader;
31
static bool inObjectStream;
32
static u8 lastPrimCmd;
35
void DoState(PointerWrap &p)
38
// Not sure what is wrong with this. Something(s) in here is causing dolphin to crash/hang when loading states saved from another run of dolphin. Doesn't seem too important anyway...
39
//vertexLoader.DoState(p);
45
if (p.GetMode() == PointerWrap::MODE_READ)
49
void DecodePrimitiveStream(u32 iBufferSize)
51
u32 vertexSize = vertexLoader.GetVertexSize();
53
bool skipPrimitives = g_bSkipCurrentFrame ||
54
swstats.thisFrame.numDrawnObjects < g_SWVideoConfig.drawStart ||
55
swstats.thisFrame.numDrawnObjects >= g_SWVideoConfig.drawEnd;
59
while (streamSize > 0 && iBufferSize >= vertexSize)
61
g_pVideoData += vertexSize;
62
iBufferSize -= vertexSize;
68
while (streamSize > 0 && iBufferSize >= vertexSize)
70
vertexLoader.LoadVertex();
71
iBufferSize -= vertexSize;
78
// return to normal command processing
83
void ReadXFData(u32 iBufferSize)
85
_assert_msg_(VIDEO, iBufferSize >= (u32)(streamSize * 4), "Underflow during standard opcode decoding");
88
for (int i = 0; i < streamSize; i++)
89
pData[i] = DataReadU32();
90
SWLoadXFReg(streamSize, streamAddress, pData);
92
// return to normal command processing
96
void ExecuteDisplayList(u32 addr, u32 count)
98
u8 *videoDataSave = g_pVideoData;
100
u8 *dlStart = Memory::GetPointer(addr);
102
g_pVideoData = dlStart;
104
while (OpcodeDecoder::CommandRunnable(count))
106
OpcodeDecoder::Run(count);
108
// if data was read by the opcode decoder then the video data pointer changed
109
u32 readCount = (u32)(g_pVideoData - dlStart);
110
dlStart = g_pVideoData;
112
_assert_msg_(VIDEO, count >= readCount, "Display list underrun");
117
g_pVideoData = videoDataSave;
120
void DecodeStandard(u32 bufferSize)
122
_assert_msg_(VIDEO, CommandRunnable(bufferSize), "Underflow during standard opcode decoding");
124
int Cmd = DataReadU8();
128
// Causes a SIGBUS error on Android
131
// check if switching in or out of an object
132
// only used for debugging
133
if (inObjectStream && (Cmd & 0x87) != lastPrimCmd)
135
inObjectStream = false;
136
DebugUtil::OnObjectEnd();
138
if (Cmd & 0x80 && !inObjectStream)
140
inObjectStream = true;
141
lastPrimCmd = Cmd & 0x87;
142
DebugUtil::OnObjectBegin();
150
case GX_LOAD_CP_REG: //0x08
152
u32 SubCmd = DataReadU8();
153
u32 Value = DataReadU32();
154
SWLoadCPReg(SubCmd, Value);
160
u32 Cmd2 = DataReadU32();
161
streamSize = ((Cmd2 >> 16) & 15) + 1;
162
streamAddress = Cmd2 & 0xFFFF;
163
currentFunction = ReadXFData;
164
minCommandSize = streamSize * 4;
169
case GX_LOAD_INDX_A: //used for position matrices
170
SWLoadIndexedXF(DataReadU32(), 0xC);
172
case GX_LOAD_INDX_B: //used for normal matrices
173
SWLoadIndexedXF(DataReadU32(), 0xD);
175
case GX_LOAD_INDX_C: //used for postmatrices
176
SWLoadIndexedXF(DataReadU32(), 0xE);
178
case GX_LOAD_INDX_D: //used for lights
179
SWLoadIndexedXF(DataReadU32(), 0xF);
184
u32 dwAddr = DataReadU32();
185
u32 dwCount = DataReadU32();
186
ExecuteDisplayList(dwAddr, dwCount);
191
// zelda 4 swords calls it and checks the metrics registers after that
194
case GX_CMD_INVL_VC:// Invalidate (vertex cache?)
195
DEBUG_LOG(VIDEO, "Invalidate (vertex cache?)");
198
case GX_LOAD_BP_REG: //0x61
200
u32 cmd = DataReadU32();
209
u8 vatIndex = Cmd & GX_VAT_MASK;
210
u8 primitiveType = (Cmd & GX_PRIMITIVE_MASK) >> GX_PRIMITIVE_SHIFT;
211
vertexLoader.SetFormat(vatIndex, primitiveType);
213
// switch to primitive processing
214
streamSize = DataReadU16();
215
currentFunction = DecodePrimitiveStream;
216
minCommandSize = vertexLoader.GetVertexSize();
219
INCSTAT(swstats.thisFrame.numPrimatives);
220
DEBUG_LOG(VIDEO, "Draw begin");
224
PanicAlert("GFX: Unknown Opcode (0x%x).\n", Cmd);
234
inObjectStream = false;
241
currentFunction = DecodeStandard;
246
bool CommandRunnable(u32 iBufferSize)
248
if (iBufferSize < minCommandSize)
253
u8 Cmd = DataPeek8(0);
258
case GX_LOAD_CP_REG: //0x08
266
case GX_LOAD_INDX_A: //used for position matrices
269
case GX_LOAD_INDX_B: //used for normal matrices
272
case GX_LOAD_INDX_C: //used for postmatrices
275
case GX_LOAD_INDX_D: //used for lights
283
case GX_LOAD_BP_REG: //0x61
294
return (iBufferSize >= minSize);
300
void Run(u32 iBufferSize)
302
currentFunction(iBufferSize);