1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the AUTHORS
5  * file distributed with this source distribution.
6  *
7  * Additional copyright for this file:
8  * Copyright (C) 1999-2000 Revolution Software Ltd.
9  * This code is based on source code created by Revolution Software,
10  * used with permission.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25  *
26  */
27 
28 #ifndef ICB_PSX_SCRN_H
29 #define ICB_PSX_SCRN_H
30 
31 #include "engines/icb/gfx/psx_pcgpu.h"
32 #include "engines/icb/gfx/psx_pcdefines.h"
33 #include "engines/icb/gfx/psx_ot.h"
34 
35 #include "common/util.h"
36 
37 namespace ICB {
38 
39 typedef struct DoubleBuffer {
40 	DRAWENV draw; /* drawing environment */
41 	DISPENV disp; /* display environment */
42 } DoubleBuffer;
43 
44 #define SCREEN_W 640
45 #define SCREEN_H 480
46 
47 #define MIN_SCREEN_X (0)
48 #define MAX_SCREEN_X (SCREEN_W - 1)
49 #define MIN_SCREEN_Y (0)
50 #define MAX_SCREEN_Y (SCREEN_H - 1)
51 #define MSG_X 20
52 #define MSG_Y (SCREEN_H - 40)
53 
54 #define MSG_W SCREEN_W
55 #define MSG_H 10
56 
57 // Length of the OT
58 #define OT_LENGTH 16 // bit length of OT
59 
60 #define OT_SIZE (1 << OT_LENGTH)
61 
62 // The PSX Data-cache which is 1KB big
63 #define D_CACHE getScratchAddr(0)
64 #define D_CACHE_SIZE 1024
65 
66 // Number of DR_LOAD primitives to allocate
67 // #define MAX_DRLOADS 2048
68 
69 // Screen position for 0,0
70 extern int32 scrn_ox;
71 extern int32 scrn_oy;
72 
73 // Global double buffer and drawing functions
74 void SwapDoubleBuffer(void);
75 void SwapBufferIndex(void);
76 void DrawDisplayList(int32 buf);
77 void Init_display(int32 w, int32 h, int32 x1, int32 y1, int32 x2, int32 y2, int32 ox, int32 oy, int32 clear);
78 void RenderCycle(void);
79 
80 // Global double buffer and drawing variables
81 
82 extern OT_tag *otarray[2];
83 extern OT_tag *drawot;
84 
85 extern int32 drawBuf;
86 extern int32 dont_set_dispbuf;
87 extern DoubleBuffer db[2];
88 extern DoubleBuffer *pdb;
89 extern uint32 reloadFont;
90 extern int32 global_use_otlist;
91 
92 // A global array place in which to create GPU packets
93 typedef uint8 GPUPACKET;
94 extern GPUPACKET *drawpacket;
95 extern GPUPACKET *drawpacketStart;
96 extern GPUPACKET *drawpacketEnd;
97 
98 // For tracking the maximum number of packets used
99 extern int32 globalPacketMax;
100 extern int32 packetsUsed;
101 
102 // How much to shift & then offset the z values from gte to
103 // put them into the otlist
104 extern int32 g_otz_shift;
105 extern int32 g_otz_offset;
106 
107 // Enable/disable updating of the auto-sliding & scaling min,max z position
108 extern int32 update_minmaxzpos;
109 
110 // Global graphics options for z-clipping and camera scalings
111 extern int32 minZOTpos;
112 extern int32 maxZOTpos;
113 extern int32 minUsedZpos;
114 extern int32 maxUsedZpos;
115 extern int32 nearClip;
116 
117 #if (_PSX_ON_PC == 0) && (_PSX == 1)
118 
119 // Number of GPU packets to reserve
120 #define PACKETMAX 1500
121 // the size of a POLY_GT3 packet
122 #define PACKETSIZE sizeof(POLY_GT3)
123 // a full-up DR_LOAD is 17 int32s (68 bytes) and is the biggest type of packet
124 #define PACKET_MAX_SIZE sizeof(DR_LOAD)
125 #define PACKETMEM (PACKETMAX * PACKETSIZE)
126 
127 // this is the nick pelling speed-up bit
128 
129 #define set3(_r0, _r1) __asm__ volatile("swl %1, 2( %0 )" : : "r"(_r0), "r"(_r1) : "memory")
130 
get3(void * _r0)131 static inline uint32 get3(void *_r0) {
132 	register uint32 t;
133 	__asm__ volatile("lwl %0, 2( %1 )" : "=r"(t) : "r"(_r0) : "memory");
134 	return t;
135 }
136 
137 #ifdef setaddr
138 #undef setaddr
139 #undef getaddr
140 #undef addPrim
141 #undef addPrims
142 #endif
143 
144 #define setaddr(_p0, _p1) set3((_p0), ((uint32)(_p1)) << 8)
145 #define getaddr(_p) (get3(_p) >> 8)
146 
addPrim(uint32 * ot,void * p)147 static inline void addPrim(uint32 *ot, void *p) {
148 	uint32 tmp = get3(ot); // lwl
149 	setaddr(ot, p);        // sll, swl
150 	set3(p, tmp);          // swl
151 }
152 
addPrims(uint32 * ot,void * p0,void * p1)153 static inline void addPrims(uint32 *ot, void *p0, void *p1) {
154 	uint32 tmp = get3(ot); // lwl
155 	setaddr(ot, p0);       // sll, swl
156 	set3(p1, tmp);         // swl
157 }
158 
159 #endif // #if (_PSX_ON_PC == 0) && (_PSX==1)
160 
161 // Cheers to Nick Pelling for this excellent mySetDrawLoad
162 // This one is a straight forward inline version of SetDrawLoad
163 // and adds the DR_FLUSH onto the end
mySetDrawLoad(DR_LOAD * p,RECT16 * r,int32 length)164 static inline void mySetDrawLoad(DR_LOAD *p, RECT16 *r, int32 length) {
165 	p->code[0] = (uint32)(0xA0 << 24); // 0xA0000000
166 	p->code[1] = *((uint32 *)r);
167 	p->code[2] = *((uint32 *)r + 1);
168 	setlen(p, (1 + 2 + 1 + length)); // code=1 RECT=2 DR_FLUSH=1 data=length
169 	p->p[length] = 0x01 << 24;       // DR_FLUSH
170 }
171 
172 // This one is just like SetDrawLoad but does not add the DR_FLUSH on the end
mySetDrawLoadNoFlush(DR_LOAD * p,RECT16 * r,int32 length)173 static inline void mySetDrawLoadNoFlush(DR_LOAD *p, RECT16 *r, int32 length) {
174 	p->code[0] = (uint32)(0xA0 << 24); // 0xA0000000
175 	p->code[1] = *((uint32 *)r);
176 	p->code[2] = *((uint32 *)r + 1);
177 	setlen(p, (1 + 2 + length)); // code=1 RECT=2 data=length
178 	p->p[length] = 0x01 << 24;   // DR_FLUSH
179 }
180 
181 // Handy function for using the packets global array
myAddPacket(int32 len)182 static inline void myAddPacket(int32 len) {
183 	// Advance to next spot :
184 	// can still fail e.g. add a small packet then try to add a large packet
185 	drawpacket += len;
186 	if (drawpacket >= drawpacketEnd) {
187 		drawpacket = drawpacketStart;
188 	}
189 }
190 
191 // Handy function for putting a DR_LOAD into the packets global array
myAddDRLOAD(RECT16 * r,uint32 * pot,int32 length)192 static inline void myAddDRLOAD(RECT16 *r, uint32 *pot, int32 length) {
193 	mySetDrawLoad((DR_LOAD *)drawpacket, r, length);
194 	addPrim(pot, drawpacket);
195 	myAddPacket(((5 + length) << 2));
196 }
197 
198 // Handy function for putting a DR_LOAD into the packets global array
199 // but without adding a DR_FLUSH onto the end
myAddDRLOADNoFlush(RECT16 * r,uint32 * pot,int32 length)200 static inline void myAddDRLOADNoFlush(RECT16 *r, uint32 *pot, int32 length) {
201 	mySetDrawLoadNoFlush((DR_LOAD *)drawpacket, r, length);
202 	addPrim(pot, drawpacket);
203 	myAddPacket(((4 + length) << 2));
204 }
205 
206 // Little to convert a z-value into an OT position
myMakeOTPosition(int32 z0)207 static inline int32 myMakeOTPosition(int32 z0) {
208 	int32 z1 = (z0 >> g_otz_shift) - g_otz_offset;
209 
210 	minUsedZpos = MIN(z0, minUsedZpos);
211 	maxUsedZpos = MAX(z0, maxUsedZpos);
212 
213 	z1 = MAX(minZOTpos, z1);
214 	z1 = MIN(maxZOTpos, z1);
215 
216 	return z1;
217 }
218 
219 // My own add prim function which automatically computes the correct
220 // index in the OT and also performs simple near,far z clipping
myAddPrimClip(int32 z0,void * primitive)221 static inline int32 myAddPrimClip(int32 z0, void *primitive) {
222 	int32 otpos = myMakeOTPosition(z0);
223 	if (otpos == -1)
224 		return -1; // ignore out of clipping range
225 
226 #if (_PSX_ON_PC == 1) || (_PSX == 0)
227 	z0 = z0 >> 2; // Divide z by 4 so it matches background units
228 	addPrimZUsr(drawot + otpos, primitive, z0, OTusrData);
229 #else
230 	addPrim(drawot + otpos, primitive);
231 #endif
232 	return otpos;
233 }
234 
235 } // End of namespace ICB
236 
237 #endif // #ifndef PSX_SCRN_H
238