1 // license:BSD-3-Clause
2 // copyright-holders:smf
3 /*
4  * PlayStation GPU emulator
5  *
6  * Copyright 2003-2014 smf
7  *
8  */
9 
10 #ifndef MAME_VIDEO_PSX_H
11 #define MAME_VIDEO_PSX_H
12 
13 #pragma once
14 
15 #define PSXGPU_DEBUG_VIEWER ( 0 )
16 
17 DECLARE_DEVICE_TYPE(CXD8514Q,  cxd8514q_device)
18 DECLARE_DEVICE_TYPE(CXD8538Q,  cxd8538q_device)
19 DECLARE_DEVICE_TYPE(CXD8561Q,  cxd8561q_device)
20 DECLARE_DEVICE_TYPE(CXD8561BQ, cxd8561bq_device)
21 DECLARE_DEVICE_TYPE(CXD8561CQ, cxd8561cq_device)
22 DECLARE_DEVICE_TYPE(CXD8654Q,  cxd8654q_device)
23 
24 class psxcpu_device;
25 
26 class psxgpu_device : public device_t, public device_video_interface, public device_palette_interface
27 {
28 public:
29 	// configuration helpers
vblank_callback()30 	auto vblank_callback() { return m_vblank_handler.bind(); }
set_vram_size(int size)31 	void set_vram_size(int size) { vramSize = size; }
32 
33 	void write(offs_t offset, uint32_t data, uint32_t mem_mask = ~0);
34 	uint32_t read(offs_t offset, uint32_t mem_mask = ~0);
35 	void dma_read( uint32_t *ram, uint32_t n_address, int32_t n_size );
36 	void dma_write( uint32_t *ram, uint32_t n_address, int32_t n_size );
37 	void lightgun_set( int, int );
38 
imperfect_features()39 	static constexpr feature_type imperfect_features() { return feature::GRAPHICS; }
40 
41 protected:
42 	static constexpr unsigned MAX_LEVEL = 32;
43 	static constexpr unsigned MID_LEVEL = (MAX_LEVEL / 2) << 8;
44 	static constexpr unsigned MAX_SHADE = 0x100;
45 	static constexpr unsigned MID_SHADE = 0x80;
46 
47 	// construction/destruction
48 	psxgpu_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock, uint32_t vram_size, psxcpu_device *cpu_tag);
49 	psxgpu_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock);
50 
51 	virtual void device_start() override;
52 	virtual void device_post_load() override;
53 	virtual void device_reset() override;
54 	virtual void device_config_complete() override;
55 
56 	// device_palette_interface overrides
palette_entries()57 	virtual uint32_t palette_entries() const override { return 32*32*32*2; }
58 
59 	int vramSize;
60 
61 private:
62 	static constexpr unsigned DEBUG_COORDS = 10;
63 
64 	struct psx_gpu_debug
65 	{
66 		std::unique_ptr<bitmap_ind16> mesh;
67 		int b_clear;
68 		int b_mesh;
69 		int n_skip;
70 		int b_texture;
71 		int n_interleave;
72 		int n_coord;
73 		int n_coordx[ DEBUG_COORDS ];
74 		int n_coordy[ DEBUG_COORDS ];
75 	};
76 
77 	struct FLATVERTEX
78 	{
79 		PAIR n_coord;
80 	};
81 
82 	struct GOURAUDVERTEX
83 	{
84 		PAIR n_bgr;
85 		PAIR n_coord;
86 	};
87 
88 	struct FLATTEXTUREDVERTEX
89 	{
90 		PAIR n_coord;
91 		PAIR n_texture;
92 	};
93 
94 	struct GOURAUDTEXTUREDVERTEX
95 	{
96 		PAIR n_bgr;
97 		PAIR n_coord;
98 		PAIR n_texture;
99 	};
100 
101 	union PACKET
102 	{
103 		uint32_t n_entry[ 16 ];
104 
105 		struct
106 		{
107 			PAIR n_cmd;
108 			struct FLATVERTEX vertex[ 2 ];
109 			PAIR n_size;
110 		} MoveImage;
111 
112 		struct
113 		{
114 			PAIR n_bgr;
115 			PAIR n_coord;
116 			PAIR n_size;
117 		} FlatRectangle;
118 
119 		struct
120 		{
121 			PAIR n_bgr;
122 			PAIR n_coord;
123 		} FlatRectangle8x8;
124 
125 		struct
126 		{
127 			PAIR n_bgr;
128 			PAIR n_coord;
129 		} FlatRectangle16x16;
130 
131 		struct
132 		{
133 			PAIR n_bgr;
134 			PAIR n_coord;
135 			PAIR n_texture;
136 		} Sprite8x8;
137 
138 		struct
139 		{
140 			PAIR n_bgr;
141 			PAIR n_coord;
142 			PAIR n_texture;
143 		} Sprite16x16;
144 
145 		struct
146 		{
147 			PAIR n_bgr;
148 			PAIR n_coord;
149 			PAIR n_texture;
150 			PAIR n_size;
151 		} FlatTexturedRectangle;
152 
153 		struct
154 		{
155 			PAIR n_bgr;
156 			struct FLATVERTEX vertex[ 4 ];
157 		} FlatPolygon;
158 
159 		struct
160 		{
161 			struct GOURAUDVERTEX vertex[ 4 ];
162 		} GouraudPolygon;
163 
164 		struct
165 		{
166 			PAIR n_bgr;
167 			struct FLATVERTEX vertex[ 2 ];
168 		} MonochromeLine;
169 
170 		struct
171 		{
172 			struct GOURAUDVERTEX vertex[ 2 ];
173 		} GouraudLine;
174 
175 		struct
176 		{
177 			PAIR n_bgr;
178 			struct FLATTEXTUREDVERTEX vertex[ 4 ];
179 		} FlatTexturedPolygon;
180 
181 		struct
182 		{
183 			struct GOURAUDTEXTUREDVERTEX vertex[ 4 ];
184 		} GouraudTexturedPolygon;
185 
186 		struct
187 		{
188 			PAIR n_bgr;
189 			struct FLATVERTEX vertex;
190 		} Dot;
191 
192 		struct
193 		{
194 			PAIR n_bgr;
195 			struct FLATTEXTUREDVERTEX vertex;
196 		} TexturedDot;
197 	};
198 
199 	void updatevisiblearea();
200 	void decode_tpage( uint32_t tpage );
201 	void FlatPolygon( int n_points );
202 	void FlatTexturedPolygon( int n_points );
203 	void GouraudPolygon( int n_points );
204 	void GouraudTexturedPolygon( int n_points );
205 	void MonochromeLine();
206 	void GouraudLine();
207 	void FrameBufferRectangleDraw();
208 	void FlatRectangle();
209 	void FlatRectangle8x8();
210 	void FlatRectangle16x16();
211 	void FlatTexturedRectangle();
212 	void Sprite8x8();
213 	void Sprite16x16();
214 	void Dot();
215 	void TexturedDot();
216 	void MoveImage();
217 	void psx_gpu_init( int n_gputype );
218 	void gpu_reset();
219 	void gpu_read( uint32_t *p_ram, int32_t n_size );
220 	void gpu_write( uint32_t *p_ram, int32_t n_size );
221 
222 	int32_t m_n_tx;
223 	int32_t m_n_ty;
224 	int32_t n_abr;
225 	int32_t n_tp;
226 	int32_t n_ix;
227 	int32_t n_iy;
228 	int32_t n_ti;
229 
230 	std::unique_ptr<uint16_t[]> p_vram;
231 	uint32_t n_vramx;
232 	uint32_t n_vramy;
233 	uint32_t n_twy;
234 	uint32_t n_twx;
235 	uint32_t n_twh;
236 	uint32_t n_tww;
237 	uint32_t n_drawarea_x1;
238 	uint32_t n_drawarea_y1;
239 	uint32_t n_drawarea_x2;
240 	uint32_t n_drawarea_y2;
241 	uint32_t n_horiz_disstart;
242 	uint32_t n_horiz_disend;
243 	uint32_t n_vert_disstart;
244 	uint32_t n_vert_disend;
245 	uint32_t b_reverseflag;
246 	int32_t n_drawoffset_x;
247 	int32_t n_drawoffset_y;
248 	uint32_t m_n_displaystartx;
249 	uint32_t n_displaystarty;
250 	int m_n_gputype;
251 	uint32_t n_gpustatus;
252 	uint32_t n_gpuinfo;
253 	uint32_t n_gpu_buffer_offset;
254 	uint32_t n_lightgun_x;
255 	uint32_t n_lightgun_y;
256 	uint32_t n_screenwidth;
257 	uint32_t n_screenheight;
258 	bool m_draw_stp;
259 	bool m_check_stp;
260 
261 	PACKET m_packet;
262 
263 	uint16_t *p_p_vram[ 1024 ];
264 
265 	uint16_t p_n_redshade[ MAX_LEVEL * MAX_SHADE ];
266 	uint16_t p_n_greenshade[ MAX_LEVEL * MAX_SHADE ];
267 	uint16_t p_n_blueshade[ MAX_LEVEL * MAX_SHADE ];
268 	uint16_t p_n_redlevel[ 0x10000 ];
269 	uint16_t p_n_greenlevel[ 0x10000 ];
270 	uint16_t p_n_bluelevel[ 0x10000 ];
271 
272 	uint16_t p_n_f025[ MAX_LEVEL * MAX_SHADE ];
273 	uint16_t p_n_f05[ MAX_LEVEL * MAX_SHADE ];
274 	uint16_t p_n_f1[ MAX_LEVEL * MAX_SHADE ];
275 	uint16_t p_n_redb05[ 0x10000 ];
276 	uint16_t p_n_greenb05[ 0x10000 ];
277 	uint16_t p_n_blueb05[ 0x10000 ];
278 	uint16_t p_n_redb1[ 0x10000 ];
279 	uint16_t p_n_greenb1[ 0x10000 ];
280 	uint16_t p_n_blueb1[ 0x10000 ];
281 	uint16_t p_n_redaddtrans[ MAX_LEVEL * MAX_LEVEL ];
282 	uint16_t p_n_greenaddtrans[ MAX_LEVEL * MAX_LEVEL ];
283 	uint16_t p_n_blueaddtrans[ MAX_LEVEL * MAX_LEVEL ];
284 	uint16_t p_n_redsubtrans[ MAX_LEVEL * MAX_LEVEL ];
285 	uint16_t p_n_greensubtrans[ MAX_LEVEL * MAX_LEVEL ];
286 	uint16_t p_n_bluesubtrans[ MAX_LEVEL * MAX_LEVEL ];
287 
288 	uint32_t p_n_g0r0[ 0x10000 ];
289 	uint32_t p_n_b0[ 0x10000 ];
290 	uint32_t p_n_r1[ 0x10000 ];
291 	uint32_t p_n_b1g1[ 0x10000 ];
292 
293 	devcb_write_line m_vblank_handler;
294 
295 	void vblank(screen_device &screen, bool vblank_state);
296 	uint32_t update_screen(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
297 
298 #if defined(PSXGPU_DEBUG_VIEWER) && PSXGPU_DEBUG_VIEWER
299 	void DebugMeshInit();
300 	void DebugMesh( int n_coordx, int n_coordy );
301 	void DebugMeshEnd();
302 	void DebugCheckKeys();
303 	int DebugMeshDisplay( bitmap_rgb32 &bitmap, const rectangle &cliprect );
304 	int DebugTextureDisplay( bitmap_rgb32 &bitmap );
305 
306 	psx_gpu_debug m_debug;
307 #endif
308 };
309 
310 class cxd8514q_device : public psxgpu_device
311 {
312 public:
313 	// construction/destruction
314 	cxd8514q_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, uint32_t vram_size, psxcpu_device *cpu);
315 	cxd8514q_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
316 };
317 
318 class cxd8538q_device : public psxgpu_device
319 {
320 public:
321 	// construction/destruction
322 	cxd8538q_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, uint32_t vram_size, psxcpu_device *cpu);
323 	cxd8538q_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
324 };
325 
326 class cxd8561q_device : public psxgpu_device
327 {
328 public:
329 	// construction/destruction
330 	cxd8561q_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, uint32_t vram_size, psxcpu_device *cpu);
331 	cxd8561q_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
332 };
333 
334 class cxd8561bq_device : public psxgpu_device
335 {
336 public:
337 	// construction/destruction
338 	cxd8561bq_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, uint32_t vram_size, psxcpu_device *cpu);
339 	cxd8561bq_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
340 };
341 
342 class cxd8561cq_device : public psxgpu_device
343 {
344 public:
345 	// construction/destruction
346 	cxd8561cq_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, uint32_t vram_size, psxcpu_device *cpu);
347 	cxd8561cq_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
348 };
349 
350 class cxd8654q_device : public psxgpu_device
351 {
352 public:
353 	// construction/destruction
354 	cxd8654q_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, uint32_t vram_size, psxcpu_device *cpu);
355 	cxd8654q_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
356 };
357 
358 #endif // MAME_VIDEO_PSX_H
359