~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Source/Core/VideoBackends/Software/Src/OpcodeDecoder.cpp

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Dolphin Emulator Project
 
2
// Licensed under GPLv2
 
3
// Refer to the license.txt file included.
 
4
 
 
5
#include "Common.h"
 
6
 
 
7
#include "DataReader.h"
 
8
 
 
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"
 
20
 
 
21
typedef void (*DecodingFunction)(u32);
 
22
 
 
23
namespace OpcodeDecoder
 
24
{
 
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;
 
33
 
 
34
 
 
35
void DoState(PointerWrap &p)
 
36
{
 
37
        p.Do(minCommandSize);
 
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);
 
40
        p.Do(readOpcode);
 
41
        p.Do(inObjectStream);
 
42
        p.Do(lastPrimCmd);
 
43
        p.Do(streamSize);
 
44
        p.Do(streamAddress);
 
45
        if (p.GetMode() == PointerWrap::MODE_READ)
 
46
                  ResetDecoding();
 
47
}
 
48
 
 
49
void DecodePrimitiveStream(u32 iBufferSize)
 
50
{
 
51
        u32 vertexSize = vertexLoader.GetVertexSize();
 
52
 
 
53
        bool skipPrimitives = g_bSkipCurrentFrame ||
 
54
                 swstats.thisFrame.numDrawnObjects < g_SWVideoConfig.drawStart ||
 
55
                 swstats.thisFrame.numDrawnObjects >= g_SWVideoConfig.drawEnd;
 
56
 
 
57
        if (skipPrimitives)
 
58
        {
 
59
                while (streamSize > 0 && iBufferSize >= vertexSize)
 
60
                {
 
61
                        g_pVideoData += vertexSize;
 
62
                        iBufferSize -= vertexSize;
 
63
                        streamSize--;
 
64
                }
 
65
        }
 
66
        else
 
67
        {
 
68
                while (streamSize > 0 && iBufferSize >= vertexSize)
 
69
                {
 
70
                        vertexLoader.LoadVertex();
 
71
                        iBufferSize -= vertexSize;
 
72
                        streamSize--;
 
73
                }
 
74
        }
 
75
 
 
76
        if (streamSize == 0)
 
77
        {
 
78
                // return to normal command processing
 
79
                ResetDecoding();
 
80
        }
 
81
}
 
82
 
 
83
void ReadXFData(u32 iBufferSize)
 
84
{
 
85
        _assert_msg_(VIDEO, iBufferSize >= (u32)(streamSize * 4), "Underflow during standard opcode decoding"); 
 
86
 
 
87
        u32 pData[16];
 
88
        for (int i = 0; i < streamSize; i++)
 
89
                pData[i] = DataReadU32();
 
90
        SWLoadXFReg(streamSize, streamAddress, pData);
 
91
 
 
92
        // return to normal command processing
 
93
        ResetDecoding();
 
94
}
 
95
 
 
96
void ExecuteDisplayList(u32 addr, u32 count)
 
97
{
 
98
        u8 *videoDataSave = g_pVideoData;
 
99
 
 
100
        u8 *dlStart = Memory::GetPointer(addr);
 
101
 
 
102
        g_pVideoData = dlStart;
 
103
 
 
104
        while (OpcodeDecoder::CommandRunnable(count))
 
105
        {
 
106
                OpcodeDecoder::Run(count);
 
107
 
 
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;
 
111
 
 
112
                _assert_msg_(VIDEO, count >= readCount, "Display list underrun"); 
 
113
 
 
114
                count -= readCount;
 
115
        }
 
116
 
 
117
        g_pVideoData = videoDataSave;
 
118
}
 
119
 
 
120
void DecodeStandard(u32 bufferSize)
 
121
{
 
122
        _assert_msg_(VIDEO, CommandRunnable(bufferSize), "Underflow during standard opcode decoding"); 
 
123
 
 
124
        int Cmd = DataReadU8();
 
125
 
 
126
        if (Cmd == GX_NOP)
 
127
                return;
 
128
        // Causes a SIGBUS error on Android
 
129
        // XXX: Investigate
 
130
#ifndef ANDROID
 
131
        // check if switching in or out of an object
 
132
        // only used for debugging
 
133
        if (inObjectStream && (Cmd & 0x87) != lastPrimCmd)
 
134
        {
 
135
                inObjectStream = false;
 
136
                DebugUtil::OnObjectEnd();
 
137
        }
 
138
        if (Cmd & 0x80 && !inObjectStream)
 
139
        {
 
140
                inObjectStream = true;
 
141
                lastPrimCmd = Cmd & 0x87;
 
142
                DebugUtil::OnObjectBegin();
 
143
        }
 
144
#endif
 
145
        switch(Cmd)
 
146
        {
 
147
        case GX_NOP:
 
148
                break;
 
149
 
 
150
        case GX_LOAD_CP_REG: //0x08
 
151
                {
 
152
                        u32 SubCmd = DataReadU8();
 
153
                        u32 Value = DataReadU32();
 
154
                        SWLoadCPReg(SubCmd, Value);
 
155
                }
 
156
                break;
 
157
 
 
158
        case GX_LOAD_XF_REG:
 
159
                {
 
160
                        u32 Cmd2 = DataReadU32();
 
161
                        streamSize = ((Cmd2 >> 16) & 15) + 1;
 
162
                        streamAddress = Cmd2 & 0xFFFF;
 
163
                        currentFunction = ReadXFData;
 
164
                        minCommandSize = streamSize * 4;
 
165
                        readOpcode = false;
 
166
                }
 
167
                break;
 
168
 
 
169
        case GX_LOAD_INDX_A: //used for position matrices
 
170
                SWLoadIndexedXF(DataReadU32(), 0xC);
 
171
                break;
 
172
        case GX_LOAD_INDX_B: //used for normal matrices
 
173
                SWLoadIndexedXF(DataReadU32(), 0xD);
 
174
                break;
 
175
        case GX_LOAD_INDX_C: //used for postmatrices
 
176
                SWLoadIndexedXF(DataReadU32(), 0xE);
 
177
                break;
 
178
        case GX_LOAD_INDX_D: //used for lights
 
179
                SWLoadIndexedXF(DataReadU32(), 0xF);
 
180
                break;
 
181
 
 
182
        case GX_CMD_CALL_DL:
 
183
                {
 
184
                        u32 dwAddr  = DataReadU32();
 
185
                        u32 dwCount = DataReadU32();
 
186
                        ExecuteDisplayList(dwAddr, dwCount);
 
187
                }
 
188
                break;
 
189
 
 
190
        case 0x44:
 
191
                // zelda 4 swords calls it and checks the metrics registers after that
 
192
                break;
 
193
 
 
194
        case GX_CMD_INVL_VC:// Invalidate       (vertex cache?) 
 
195
                DEBUG_LOG(VIDEO, "Invalidate    (vertex cache?)");
 
196
                break;
 
197
 
 
198
        case GX_LOAD_BP_REG: //0x61
 
199
                {
 
200
                        u32 cmd = DataReadU32();
 
201
                        SWLoadBPReg(cmd);
 
202
                }
 
203
                break;
 
204
 
 
205
        // draw primitives 
 
206
        default:
 
207
                if (Cmd & 0x80)
 
208
                {
 
209
                        u8 vatIndex = Cmd & GX_VAT_MASK;
 
210
                        u8 primitiveType = (Cmd & GX_PRIMITIVE_MASK) >> GX_PRIMITIVE_SHIFT;
 
211
                        vertexLoader.SetFormat(vatIndex, primitiveType);
 
212
 
 
213
                        // switch to primitive processing
 
214
                        streamSize = DataReadU16();
 
215
                        currentFunction = DecodePrimitiveStream;
 
216
                        minCommandSize = vertexLoader.GetVertexSize();
 
217
                        readOpcode = false;
 
218
 
 
219
                        INCSTAT(swstats.thisFrame.numPrimatives);
 
220
                        DEBUG_LOG(VIDEO, "Draw begin");
 
221
                }
 
222
                else
 
223
                {
 
224
                        PanicAlert("GFX: Unknown Opcode (0x%x).\n", Cmd);
 
225
                        break;
 
226
                }
 
227
                break;
 
228
        }
 
229
}
 
230
 
 
231
 
 
232
void Init()
 
233
{
 
234
        inObjectStream = false;
 
235
        lastPrimCmd = 0;
 
236
        ResetDecoding();
 
237
}
 
238
 
 
239
void ResetDecoding()
 
240
{
 
241
        currentFunction = DecodeStandard;
 
242
        minCommandSize = 1;
 
243
        readOpcode = true;
 
244
}
 
245
 
 
246
bool CommandRunnable(u32 iBufferSize)
 
247
{
 
248
        if (iBufferSize < minCommandSize)
 
249
                return false;
 
250
 
 
251
        if (readOpcode)
 
252
        {
 
253
                u8 Cmd = DataPeek8(0);
 
254
                u32 minSize = 1;
 
255
 
 
256
                switch(Cmd)
 
257
                {
 
258
                case GX_LOAD_CP_REG: //0x08
 
259
                        minSize = 6;
 
260
                        break;
 
261
 
 
262
                case GX_LOAD_XF_REG:
 
263
                        minSize = 5;
 
264
                        break;
 
265
 
 
266
                case GX_LOAD_INDX_A: //used for position matrices
 
267
                        minSize = 5;
 
268
                        break;
 
269
                case GX_LOAD_INDX_B: //used for normal matrices
 
270
                        minSize = 5;
 
271
                        break;
 
272
                case GX_LOAD_INDX_C: //used for postmatrices
 
273
                        minSize = 5;
 
274
                        break;
 
275
                case GX_LOAD_INDX_D: //used for lights
 
276
                        minSize = 5;
 
277
                        break;
 
278
 
 
279
                case GX_CMD_CALL_DL:
 
280
                        minSize = 9;
 
281
                        break;
 
282
 
 
283
                case GX_LOAD_BP_REG: //0x61
 
284
                        minSize = 5;
 
285
                        break;
 
286
 
 
287
                // draw primitives 
 
288
                default:
 
289
                        if (Cmd & 0x80)
 
290
                                minSize = 3;
 
291
                        break;
 
292
                }
 
293
 
 
294
                return (iBufferSize >= minSize);
 
295
        }
 
296
 
 
297
        return true;
 
298
}
 
299
 
 
300
void Run(u32 iBufferSize)
 
301
{
 
302
        currentFunction(iBufferSize);
 
303
}
 
304
 
 
305
}