303ebd0f8ba8a10bd68d2606550d5067bb18dba3
[mupen64plus-pandora.git] / source / mupen64plus-video-arachnoid / src / ucodes / UCode1.cpp
1 /******************************************************************************
2  * Arachnoid Graphics Plugin for Mupen64Plus
3  * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
4  *
5  * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *****************************************************************************/
21
22 #include "UCode1.h"
23 #include "UCode0.h"
24
25 //Forward declaration
26 #include "GBI.h"
27 #include "RSP.h"
28 #include "RDP.h"
29 #include "Memory.h"
30
31 #include "UCodeDefs.h"
32 #include "GBIDefs.h"
33 #include "Logger.h"
34
35 //-----------------------------------------------------------------------------
36 // Static Variables
37 //-----------------------------------------------------------------------------
38 RSP*    UCode1::m_rsp    = 0;   //!< Pointer to Reality Signal Processor 
39 RDP*    UCode1::m_rdp    = 0;   //!< Pointer to Reality Drawing Processor 
40 Memory* UCode1::m_memory = 0;
41 GBI*    UCode1::m_gbi    = 0;
42
43 //-----------------------------------------------------------------------------
44 //! Constructor
45 //-----------------------------------------------------------------------------
46 UCode1::UCode1()
47 {
48 }
49
50 //-----------------------------------------------------------------------------
51 //! Destructor
52 //-----------------------------------------------------------------------------
53 UCode1::~UCode1()
54 {
55 }
56
57 //-----------------------------------------------------------------------------
58 //! Initialize
59 //-----------------------------------------------------------------------------
60 void UCode1::initialize(GBI* gbi, RSP* rsp, RDP* rdp, Memory* memory)
61 {
62     m_gbi = gbi;
63     m_rsp = rsp;
64     m_rdp = rdp;
65     m_memory = memory;
66 }
67
68 //-----------------------------------------------------------------------------
69 //! Initialize GBI
70 //-----------------------------------------------------------------------------
71 void UCode1::initializeGBI()
72 {
73     // Set GeometryMode flags
74     GBI_InitFlags( F3DEX );
75
76     //          GBI Command              Command Value            //Target          Command Function
77     GBI_SetGBI( GBI::G_SPNOOP,           F3D_SPNOOP,              m_gbi->m_cmds,    UCode0::F3D_SPNoOp );
78     GBI_SetGBI( GBI::G_MTX,              F3D_MTX,                 m_gbi->m_cmds,    UCode0::F3D_Mtx );
79     GBI_SetGBI( GBI::G_RESERVED0,        F3D_RESERVED0,           m_gbi->m_cmds,    UCode0::F3D_Reserved0 );
80     GBI_SetGBI( GBI::G_MOVEMEM,          F3D_MOVEMEM,             m_gbi->m_cmds,    UCode0::F3D_MoveMem );
81     GBI_SetGBI( GBI::G_VTX,              F3D_VTX,                 m_gbi->m_cmds,    F3DEX_Vtx );
82     GBI_SetGBI( GBI::G_RESERVED1,        F3D_RESERVED1,           m_gbi->m_cmds,    UCode0::F3D_Reserved1 );
83     GBI_SetGBI( GBI::G_DL,               F3D_DL,                  m_gbi->m_cmds,    UCode0::F3D_DList );
84     GBI_SetGBI( GBI::G_RESERVED2,        F3D_RESERVED2,           m_gbi->m_cmds,    UCode0::F3D_Reserved2 );
85     GBI_SetGBI( GBI::G_RESERVED3,        F3D_RESERVED3,           m_gbi->m_cmds,    UCode0::F3D_Reserved3 );
86     GBI_SetGBI( GBI::G_SPRITE2D_BASE,    F3D_SPRITE2D_BASE,       m_gbi->m_cmds,    UCode0::F3D_Sprite2D_Base );
87     GBI_SetGBI( GBI::G_TRI1,             F3D_TRI1,                m_gbi->m_cmds,    F3DEX_Tri1 );
88     GBI_SetGBI( GBI::G_CULLDL,           F3D_CULLDL,              m_gbi->m_cmds,    F3DEX_CullDL );
89     GBI_SetGBI( GBI::G_POPMTX,           F3D_POPMTX,              m_gbi->m_cmds,    UCode0::F3D_PopMtx );
90     GBI_SetGBI( GBI::G_MOVEWORD,         F3D_MOVEWORD,            m_gbi->m_cmds,    UCode0::F3D_MoveWord );
91     GBI_SetGBI( GBI::G_TEXTURE,          F3D_TEXTURE,             m_gbi->m_cmds,    UCode0::F3D_Texture );
92     GBI_SetGBI( GBI::G_SETOTHERMODE_H,   F3D_SETOTHERMODE_H,      m_gbi->m_cmds,    UCode0::F3D_SetOtherMode_H );
93     GBI_SetGBI( GBI::G_SETOTHERMODE_L,   F3D_SETOTHERMODE_L,      m_gbi->m_cmds,    UCode0::F3D_SetOtherMode_L );
94     GBI_SetGBI( GBI::G_ENDDL,            F3D_ENDDL,               m_gbi->m_cmds,    UCode0::F3D_EndDL );
95     GBI_SetGBI( GBI::G_SETGEOMETRYMODE,  F3D_SETGEOMETRYMODE,     m_gbi->m_cmds,    UCode0::F3D_SetGeometryMode );
96     GBI_SetGBI( GBI::G_CLEARGEOMETRYMODE,F3D_CLEARGEOMETRYMODE,   m_gbi->m_cmds,    UCode0::F3D_ClearGeometryMode );
97     GBI_SetGBI( GBI::G_QUAD,             F3D_QUAD,                m_gbi->m_cmds,    F3DEX_Quad );
98     GBI_SetGBI( GBI::G_RDPHALF_1,        F3D_RDPHALF_1,           m_gbi->m_cmds,    UCode0::F3D_RDPHalf_1 );
99     GBI_SetGBI( GBI::G_RDPHALF_2,        F3D_RDPHALF_2,           m_gbi->m_cmds,    UCode0::F3D_RDPHalf_2 );
100     GBI_SetGBI( GBI::G_MODIFYVTX,        F3DEX_MODIFYVTX,         m_gbi->m_cmds,    F3DEX_ModifyVtx );
101     GBI_SetGBI( GBI::G_TRI2,             F3DEX_TRI2,              m_gbi->m_cmds,    F3DEX_Tri2 );
102     GBI_SetGBI( GBI::G_BRANCH_Z,         F3DEX_BRANCH_Z,          m_gbi->m_cmds,    F3DEX_Branch_Z );
103     GBI_SetGBI( GBI::G_LOAD_UCODE,       F3DEX_LOAD_UCODE,        m_gbi->m_cmds,    F3DEX_Load_uCode );
104 }
105
106 //-----------------------------------------------------------------------------
107 //! Load UCode
108 //-----------------------------------------------------------------------------
109 void UCode1::F3DEX_Load_uCode(MicrocodeArgument* ucode)
110 {
111     Logger::getSingleton().printMsg("F3DEX_Load_uCode - experimental", M64MSG_WARNING);
112     RSPUCodeLoadUCode* temp = (RSPUCodeLoadUCode*)ucode;
113
114     //unsigned int ucodeDataStart1 = m_memory->getRDRAMAddress( (*(unsigned int*)( m_memory->getRDRAM(pc-12))  );
115     unsigned int ucodeDataStart2 = m_rdp->getHalf1();
116
117     //if ( ucodeDataStart1 != ucodeDataStart2 )
118     //{
119     //    Logger::getSingleton().printMsg("Warning - UCode Data Start differs", M64MSG_INFO);
120     //}
121
122     //Select UCode
123     m_gbi->selectUCode( temp->ucodeStart,        //UCodeStart 
124                         ucodeDataStart2,         //UCodeDataStart
125                         temp->ucodeSize+1,       //UCodeSize
126                         8);                      //UCodeDataSize  //Always 8 ???                  
127 }
128
129 //-----------------------------------------------------------------------------
130 //! Add Vertices
131 //! @param ucode instruction from displaylist with input data
132 //-----------------------------------------------------------------------------
133 void UCode1::F3DEX_Vtx(MicrocodeArgument* ucode)
134 {
135     Logger::getSingleton().printMsg("F3DEX_Vtx", M64MSG_VERBOSE);
136     RSPUCodeAddVertices1* temp = (RSPUCodeAddVertices1*)ucode;
137
138     //Add Vertices
139     m_rsp->RSP_Vertex(temp->segmentAddress, temp->numVertices, temp->firstVertex);
140 }
141
142 //-----------------------------------------------------------------------------
143 //! Modify Vertex
144 //-----------------------------------------------------------------------------
145 void UCode1::F3DEX_ModifyVtx(MicrocodeArgument* ucode)
146 {
147     Logger::getSingleton().printMsg("F3DEX_ModifyVtx", M64MSG_VERBOSE);
148     RSPUCodeModifyVertex* temp = (RSPUCodeModifyVertex*)ucode;
149
150     switch ( temp->modifyType )
151     {
152         case G_MWO_POINT_RGBA:
153             m_rsp->RSP_SetVertexColor( temp->vertexIndex, 
154                                        temp->r/255.0f, 
155                                        temp->g/255.0f, 
156                                        temp->b/255.0f, 
157                                        temp->a/255.0f );
158             break;
159         case G_MWO_POINT_ST:
160             m_rsp->RSP_SetVertexTexCoord(temp->vertexIndex, 
161                                          temp->t / 32.0f, 
162                                          temp->s / 32.0f );
163             break;
164         case G_MWO_POINT_XYSCREEN:
165             break;
166         case G_MWO_POINT_ZSCREEN:
167             break;
168     };
169 }
170
171 //-----------------------------------------------------------------------------
172 //! Add 1 Triangle
173 //! @param ucode instruction from displaylist with input data
174 //-----------------------------------------------------------------------------
175 void UCode1::F3DEX_Tri1(MicrocodeArgument* ucode)
176 {
177     Logger::getSingleton().printMsg("F3DEX_Tri1", M64MSG_VERBOSE);
178     RSPUCodeAddOneTriangleF3DEX* temp = (RSPUCodeAddOneTriangleF3DEX*)ucode;
179
180     //Add one triangle 
181     m_rsp->RSP_1Triangle(temp->index0, temp->index1, temp->index2);
182 }
183
184 //-----------------------------------------------------------------------------
185 //! Add 2 Triangles
186 //! @param ucode instruction from displaylist with input data
187 //-----------------------------------------------------------------------------
188 void UCode1::F3DEX_Tri2(MicrocodeArgument* ucode)
189 {
190     Logger::getSingleton().printMsg("F3DEX_Tri2", M64MSG_VERBOSE);
191     RSPUCodeAddTwoTrianglesF3DEX* temp = (RSPUCodeAddTwoTrianglesF3DEX*)ucode;
192
193     //Add two triangles
194     m_rsp->RSP_2Triangles( temp->v0, temp->v1, temp->v2, 0,
195                            temp->v3, temp->v4, temp->v5, 0);
196 }
197
198 //-----------------------------------------------------------------------------
199 //! Add 1 Quadrangle
200 //! @param ucode instruction from displaylist with input data
201 //-----------------------------------------------------------------------------
202 void UCode1::F3DEX_Quad( MicrocodeArgument* ucode )
203 {
204     Logger::getSingleton().printMsg("F3DEX_Quad", M64MSG_VERBOSE);
205     RSPUCodeAddOneQuadF3DEX* temp = (RSPUCodeAddOneQuadF3DEX*)ucode;
206
207     //Add one Quad
208     m_rsp->RSP_1Quadrangle(temp->index0, temp->index1, temp->index2, temp->index3);
209 }
210
211 //-----------------------------------------------------------------------------
212 //! Cull Display List
213 //-----------------------------------------------------------------------------
214 void UCode1::F3DEX_CullDL(MicrocodeArgument* ucode)
215 {
216     static bool warned = false;
217     if ( !warned )
218     {
219         Logger::getSingleton().printMsg("F3DEX_CullDL - Unimplemented", M64MSG_WARNING);
220         warned = true;
221     }
222     RSPUCodeCullDisplayList* temp = (RSPUCodeCullDisplayList*)ucode;
223
224     //Cull display list?
225     m_rsp->RSP_CullDisplayList( temp->vertexIndex, temp->numVerticies  );
226 }
227
228 //-----------------------------------------------------------------------------
229 //! Branch Z
230 //-----------------------------------------------------------------------------
231 void UCode1::F3DEX_Branch_Z(MicrocodeArgument* ucode)
232 {
233     Logger::getSingleton().printMsg("F3DEX_Branch_Z", M64MSG_VERBOSE);
234     RSPUCodeBranchZF3DEX* temp = (RSPUCodeBranchZF3DEX*)ucode;
235
236     //Branch Display List?
237     m_rsp->RSP_BranchLessZ(m_rdp->getHalf1(), temp->vertex, (float)(int)temp->zvalue );
238 }