summaryrefslogtreecommitdiff
path: root/libpcsxcore/pgxp_gte.c
blob: 4b22c778c68d1a60254751ea363404c464cc21cb (plain) (blame)
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
/***************************************************************************
*   Copyright (C) 2016 by iCatButler                                      *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
***************************************************************************/

/**************************************************************************
*	pgxp_gte.c
*	PGXP - Parallel/Precision Geometry Xform Pipeline
*
*	Created on: 12 Mar 2016
*      Author: iCatButler
***************************************************************************/

#include "pgxp_gte.h"
#include "psxmem.h"

typedef struct 
{
	float			x;
	float			y;
	float			z;
	unsigned int	valid;
	unsigned int	count;
} precise_value;

typedef union
{
	struct
	{
		s16 x;
		s16 y;
	};
	u32 word;
} low_value;

#define tolerance 1.0f

precise_value GTE_reg[32];
precise_value CPU_reg[32];

precise_value Mem[2048 * 1024 / 4];		// mirror 2MB in 32-bit words
precise_value Scratch[2048 * 1024 / 4]; // mirror 2MB in 32-bit words

void PGXP_Init()
{
	memset(Mem, 0, sizeof(Mem));
}

char* PGXP_GetMem()
{
	return (char*)(Mem);
}

#define VRAM 0
#define SCRATCH 1

precise_value* ReadMem(u32 addr)
{
	u32 memType;
	uint32_t paddr = addr;
	int* ip = NULL;

	switch (paddr >> 20)
	{
	case 0x800:
	case 0x801:
	case 0xa00:
	case 0xa01:
	case 0x000:
	case 0x001:
		memType = VRAM;
		break;
	case 0x1f8:
		memType = SCRATCH;
		break;
	default:
		return NULL;
	}
	
#ifdef GTE_LOG
	//GTE_LOG("PGXP_Read %x [%x] |", addr, paddr);
#endif
	if (memType == VRAM)
	{
		paddr = (paddr & 0x1FFFFF) >> 2;
		return &Mem[paddr];
	}
	else if (memType == SCRATCH)
	{
		paddr = (paddr & 0x1FFFFF) >> 2;
		return &Scratch[paddr];
	}

	return NULL;
}

void WriteMem(precise_value value, u32 addr)
{
	u32 memType;
	uint32_t paddr = addr;
	int* ip = NULL;

	switch (paddr >> 20)
	{
	case 0x800:
	case 0x801:
	case 0xa00:
	case 0xa01:
	case 0x000:
	case 0x001:
		memType = VRAM;
		break;
	case 0x1f8:
		memType = SCRATCH;
		break;
	default:
		if (value.valid)
			*ip = 5;
		return;
	}

#ifdef GTE_LOG
	GTE_LOG("PGXP_Write %x [%x] |", addr, paddr);
#endif

	// Store to RAM
	if (memType == VRAM)
	{
		paddr = (paddr & 0x1FFFFF) >> 2;
		Mem[paddr] = value;
	}
	else if (memType == SCRATCH)
	{
		paddr = (paddr & 0x1FFFFF) >> 2;// (paddr & 0x3FFF) >> 2;
		Scratch[paddr] = value;
	}
}

#define SX0 (GTE_reg[ 12 ].x)
#define SY0 (GTE_reg[ 12 ].y)
#define SX1 (GTE_reg[ 13 ].x)
#define SY1 (GTE_reg[ 13 ].y)
#define SX2 (GTE_reg[ 14 ].x)
#define SY2 (GTE_reg[ 14 ].y)

#define SXY0 (GTE_reg[ 12 ])
#define SXY1 (GTE_reg[ 13 ])
#define SXY2 (GTE_reg[ 14 ])
#define SXYP (GTE_reg[ 15 ])

unsigned int PGXP_validate(float high, s16 low)
{
	if (fabs(high - (float)(low)) < tolerance)
		return 1;
	return 0;
}

// Check that value is still within tolerance of low precision value and invalidate if not
precise_value PGXP_validateXY(precise_value *high, u32 low)
{
	low_value temp;
	precise_value ret;

	ret.valid = 0;
	temp.word = low;

	if (!high)
		return ret;

	high->valid = (high->valid && PGXP_validate(high->x, temp.x) && PGXP_validate(high->y, temp.y));

	// Cheat
	//if (!high->valid)
	//{
	//	high->x = temp.x;
	//	high->y = temp.y;
	//	high->valid = 1;
	//}

	return *high;
}

u32 PGXP_compareXY(precise_value high, u32 low)
{
	low_value temp;
	temp.word = low;

	if (PGXP_validate(high.x, temp.x) && PGXP_validate(high.y, temp.y))
		return 1;
	return 0;
}

precise_value PGXP_copyXY(u32 low)
{
	low_value temp;
	precise_value ret;

	ret.valid = 0;
	temp.word = low;

	ret.x = temp.x;
	ret.y = temp.y;
	ret.count = 0;
	ret.valid = 1;

	return ret;
}

void PGXP_pushSXYZ2f(float _x, float _y, float _z)
{
	static unsigned int uCount = 0;
	// push values down FIFO
	SXY0 = SXY1;
	SXY1 = SXY2;
	
	SXY2.x		= _x;
	SXY2.y		= _y;
	SXY2.z		= _z;
	SXY2.valid	= 1;
	SXY2.count	= uCount++;

#ifdef GTE_LOG
	GTE_LOG("PGPR_PUSH (%f, %f) %u %u|", SXY2.x, SXY2.y, SXY2.valid, SXY2.count);
#endif
}

void PGXP_pushSXYZ2s(s64 _x, s64 _y, s64 _z)
{
	float fx = (float)(_x) / (float)(1 << 16);
	float fy = (float)(_y) / (float)(1 << 16);
	float fz = (float)(_z);

	PGXP_pushSXYZ2f(fx, fy, fz);
}

int PGXP_NLCIP_valid()
{
	if (SXY0.valid && SXY1.valid && SXY2.valid)
		return 1;
	return 0;
}

float PGXP_NCLIP()
{
	float nclip = ((SX0 * SY1) + (SX1 * SY2) + (SX2 * SY0) - (SX0 * SY2) - (SX1 * SY0) - (SX2 * SY1));

	// ensure fractional values are not incorrectly rounded to 0
	if (fabs(nclip) < 1.0f)
		nclip += (nclip < 0.f ? -2 : 2);

	return nclip;
}

static precise_value PGXP_MFC2_int(u32 reg)
{
	switch (reg) 
	{
	case 15:
		GTE_reg[reg] = SXYP = SXY2;
		break;
	}

	return GTE_reg[reg];
}


static void PGXP_MTC2_int(precise_value value, u32 reg)
{
	switch(reg)
	{
		case 15:
			// push FIFO
			SXY0 = SXY1;
			SXY1 = SXY2;
			SXY2 = value;
			SXYP = SXY2;
			break;

		case 31:
			return;
	}

	GTE_reg[reg] = value;
}

// copy GTE data reg to GPR reg (MFC2)
void PGXP_MFC2(u32 gpr, u32 gtr, u32 value)
{
	if (!gpr) return;
#ifdef GTE_LOG
	GTE_LOG("PGXP_MFC2 [%x] [%x] %x (%u %u)|", gpr, gtr, value, GTE_reg[gtr].valid, GTE_reg[gtr].count);
#endif

	CPU_reg[gpr] = PGXP_validateXY(&GTE_reg[gtr], value);
}

// copy GPR reg to GTE data reg (MTC2)
void PGXP_MTC2(u32 gpr, u32 gtr, u32 value)
{
#ifdef GTE_LOG
	GTE_LOG("PGXP_MTC2 [%x] [%x] %x (%u %u)|", gpr, gtr, value, CPU_reg[gtr].valid, CPU_reg[gtr].count);
#endif
	PGXP_MTC2_int(PGXP_validateXY(&CPU_reg[gpr], value), gtr);
}

// copy memory to GTE reg
void PGXP_LWC2(u32 addr, u32 gtr, u32 value)
{
#ifdef GTE_LOG
	precise_value* pp = ReadMem(addr);
	precise_value p;
	low_value temp;
	temp.word = value;

	p.x = p.y = p.valid = 0;

	if (pp)
		p = *pp;

	GTE_LOG("PGXP_LWC2 %x [%x] %x (%d, %d) (%f, %f) %u %u|", addr, gtr, value, temp.x, temp.y, p.x, p.y, p.valid, p.count);
#endif
	PGXP_MTC2_int(PGXP_validateXY(ReadMem(addr), value), gtr);
}

//copy GTE reg to memory
void PGXP_SWC2(u32 addr, u32 gtr, u32 value)
{
#ifdef GTE_LOG
	low_value temp;
	temp.word = value;

	if (PGXP_compareXY(GTE_reg[gtr], value))
		GTE_LOG("PGPR_SWC2 %x [%x] %x (%d, %d) (%f, %f) %u %u|", addr, gtr, value, temp.x, temp.y, GTE_reg[gtr].x, GTE_reg[gtr].y, GTE_reg[gtr].valid, GTE_reg[gtr].count);
#endif
	WriteMem(PGXP_validateXY(&GTE_reg[gtr], value), addr);
}

// ltore 32bit word
void PGPR_L32(u32 addr, u32 code, u32 value)
{
	u32 reg = ((code >> 16) & 0x1F); // The rt part of the instruction register 
	u32 op = ((code >> 26));
	precise_value p;

	low_value temp;
	temp.word = value;

	p.x = p.y = p.valid = p.count = 0;

	switch (op)
	{
	case 34:	//LWL
		CPU_reg[reg] = PGXP_validateXY(ReadMem(addr), value);
		break;
	case 35:	//LW
		CPU_reg[reg] = PGXP_validateXY(ReadMem(addr), value);
		break;
	case 37:	//LWR
		CPU_reg[reg] = PGXP_validateXY(ReadMem(addr), value);
		break;
	case 50:	//LWC2 (GTE vertex reads)
		GTE_reg[reg] = PGXP_validateXY(ReadMem(addr), value);
		break;
	default:
		// invalidate register
		//	WriteMem(p, addr);
		break;
	}

#ifdef GTE_LOG
	GTE_LOG("PGPR_L32 %u: %x %x[%x %x] (%d, %d) (%f, %f) %x %u|", op, addr, value, code, reg, temp.x, temp.y, CPU_reg[reg].x, CPU_reg[reg].y, CPU_reg[reg].valid, CPU_reg[reg].count);
#endif
}

// store 32bit word
void PGPR_S32(u32 addr, u32 code, u32 value)
{
	u32 reg = ((code >> 16) & 0x1F); // The rt part of the instruction register 
	u32 op = ((code >> 26));
	precise_value p;

	low_value temp;
	temp.word = value;

	p.x = p.y = p.valid = p.count = 0;

#ifdef GTE_LOG
	GTE_LOG("PGPR_S32 %u: %x %x[%x %x] (%d, %d) (%f, %f) %x %u|", op, addr, value, code, reg, temp.x, temp.y, CPU_reg[reg].x, CPU_reg[reg].y, CPU_reg[reg].valid, CPU_reg[reg].count);
#endif

	switch (op)
	{
	case 42:	//SWL
		WriteMem(PGXP_validateXY(&CPU_reg[reg], value), addr);
		break;
	case 43:	//SW
		WriteMem(PGXP_validateXY(&CPU_reg[reg], value), addr);
		break;
	case 46:	//SWR
		WriteMem(PGXP_validateXY(&CPU_reg[reg], value), addr);
		break;
	case 58:	//SWC2 (GTE vertex writes)
		WriteMem(PGXP_validateXY(&GTE_reg[reg], value), addr);
		break;
	default:
		// invalidate memory
	//	WriteMem(p, addr);
		break;
	}
}

u32 PGXP_psxMemRead32Trace(u32 mem, u32 code)
{
	u32 value = psxMemRead32(mem);
	PGPR_L32(mem, code, value);
	return value;
}

void PGXP_psxMemWrite32Trace(u32 mem, u32 value, u32 code)
{
	PGPR_S32(mem, code, value);
	psxMemWrite32(mem, value);
}