1 // license:BSD-3-Clause
2 // copyright-holders:smf
3 /*
4  * PlayStation Geometry Transformation Engine emulator
5  *
6  * Copyright 2003-2013 smf
7  *
8  */
9 
10 #ifndef MAME_CPU_PSX_GTE_H
11 #define MAME_CPU_PSX_GTE_H
12 
13 #pragma once
14 
15 
16 #define GTE_SF( op ) ( ( op >> 19 ) & 1 )
17 #define GTE_MX( op ) ( ( op >> 17 ) & 3 )
18 #define GTE_V( op ) ( ( op >> 15 ) & 3 )
19 #define GTE_CV( op ) ( ( op >> 13 ) & 3 )
20 #define GTE_LM( op ) ( ( op >> 10 ) & 1 )
21 #define GTE_FUNCT( op ) ( op & 63 )
22 
23 class gte
24 {
25 public:
gte()26 	gte() : m_sf(0), m_mac0(0), m_mac1(0), m_mac2(0), m_mac3(0)
27 	{
28 	}
29 
30 	PAIR m_cp2cr[ 32 ];
31 	PAIR m_cp2dr[ 32 ];
32 
33 	uint32_t getcp2dr( uint32_t pc, int reg );
34 	void setcp2dr( uint32_t pc, int reg, uint32_t value );
35 	uint32_t getcp2cr( uint32_t pc, int reg );
36 	void setcp2cr( uint32_t pc, int reg, uint32_t value );
37 	int docop2( uint32_t pc, int gteop );
38 
39 protected:
40 	class int44
41 	{
42 	public:
int44(int64_t value)43 		int44( int64_t value ) :
44 			m_value( value ),
45 			m_positive_overflow( value > 0x7ffffffffff ),
46 			m_negative_overflow( value < -0x80000000000 )
47 		{
48 		}
49 
int44(int64_t value,bool positive_overflow,bool negative_overflow)50 		int44( int64_t value, bool positive_overflow, bool negative_overflow ) :
51 			m_value( value ),
52 			m_positive_overflow( positive_overflow ),
53 			m_negative_overflow( negative_overflow )
54 		{
55 		}
56 
57 		int44 operator+( int64_t add )
58 		{
59 			int64_t value = ( ( m_value + add ) << 20 ) >> 20;
60 
61 			return int44( value,
62 				m_positive_overflow || ( value < 0 && m_value >= 0 && add >= 0 ),
63 				m_negative_overflow || ( value >= 0 && m_value < 0 && add < 0 ) );
64 		}
65 
positive_overflow()66 		bool positive_overflow()
67 		{
68 			return m_positive_overflow;
69 		}
70 
negative_overflow()71 		bool negative_overflow()
72 		{
73 			return m_negative_overflow;
74 		}
75 
value()76 		int64_t value()
77 		{
78 			return m_value;
79 		}
80 
81 	private:
82 		int64_t m_value;
83 		bool m_positive_overflow;
84 		bool m_negative_overflow;
85 	};
86 
87 	int32_t LIM( int32_t value, int32_t max, int32_t min, uint32_t flag );
88 	int32_t BOUNDS( int44 a, int max_flag, int min_flag );
89 	int32_t A1( int44 a );
90 	int32_t A2( int44 a );
91 	int32_t A3( int44 a );
92 	int32_t Lm_B1( int32_t a, int lm );
93 	int32_t Lm_B2( int32_t a, int lm );
94 	int32_t Lm_B3( int32_t a, int lm );
95 	int32_t Lm_B3_sf( int64_t value, int sf, int lm );
96 	int32_t Lm_C1( int32_t a );
97 	int32_t Lm_C2( int32_t a );
98 	int32_t Lm_C3( int32_t a );
99 	int32_t Lm_D( int64_t a, int sf );
100 	uint32_t Lm_E( uint32_t result );
101 	int64_t F( int64_t a );
102 	int32_t Lm_G1( int64_t a );
103 	int32_t Lm_G2( int64_t a );
104 	int32_t Lm_H( int64_t value, int sf );
105 
106 	int m_sf;
107 	int64_t m_mac0;
108 	int64_t m_mac1;
109 	int64_t m_mac2;
110 	int64_t m_mac3;
111 };
112 
113 #endif // MAME_CPU_PSX_GTE_H
114