2 * Copyright (C) 2011 Gilead Kutnick "Exophase" <exophase@gmail.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
18 #include "../../gpulib/gpu_timing.h"
20 #ifndef command_lengths
21 const u8 command_lengths[256] =
23 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00
24 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10
25 3, 3, 3, 3, 6, 6, 6, 6, 4, 4, 4, 4, 8, 8, 8, 8, // 20
26 5, 5, 5, 5, 8, 8, 8, 8, 7, 7, 7, 7, 11, 11, 11, 11, // 30
27 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 40
28 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, // 50
29 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, // 60
30 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, // 70
31 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90
33 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // a0
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // b0
35 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // c0
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // d0
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // e0
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // f0
42 void update_texture_ptr(psx_gpu_struct *psx_gpu)
47 switch((psx_gpu->render_state_base >> 8) & 0x3)
49 case TEXTURE_MODE_4BPP:
50 texture_base = psx_gpu->texture_4bpp_cache[psx_gpu->current_texture_page];
52 texture_ptr = texture_base;
53 texture_ptr += psx_gpu->texture_window_x & 0xF;
54 texture_ptr += (psx_gpu->texture_window_y & 0xF) << 4;
55 texture_ptr += (psx_gpu->texture_window_x >> 4) << 8;
56 texture_ptr += (psx_gpu->texture_window_y >> 4) << 12;
59 case TEXTURE_MODE_8BPP:
60 if(psx_gpu->current_texture_page & 0x1)
63 psx_gpu->texture_8bpp_odd_cache[psx_gpu->current_texture_page >> 1];
68 psx_gpu->texture_8bpp_even_cache[psx_gpu->current_texture_page >> 1];
71 texture_ptr = texture_base;
72 texture_ptr += psx_gpu->texture_window_x & 0xF;
73 texture_ptr += (psx_gpu->texture_window_y & 0xF) << 4;
74 texture_ptr += (psx_gpu->texture_window_x >> 4) << 8;
75 texture_ptr += (psx_gpu->texture_window_y >> 4) << 12;
79 case TEXTURE_MODE_16BPP:
80 texture_base = (u8 *)(psx_gpu->vram_ptr);
81 texture_base += (psx_gpu->current_texture_page & 0xF) * 128;
82 texture_base += ((psx_gpu->current_texture_page >> 4) * 256) * 2048;
84 texture_ptr = texture_base;
85 texture_ptr += psx_gpu->texture_window_x * 2;
86 texture_ptr += (psx_gpu->texture_window_y) * 2048;
90 psx_gpu->texture_page_base = texture_base;
91 psx_gpu->texture_page_ptr = texture_ptr;
94 void set_texture(psx_gpu_struct *psx_gpu, u32 texture_settings)
96 texture_settings &= 0x1FF;
97 if(psx_gpu->texture_settings != texture_settings)
99 u32 new_texture_page = texture_settings & 0x1F;
100 u32 texture_mode = (texture_settings >> 7) & 0x3;
101 u32 render_state_base = psx_gpu->render_state_base;
103 flush_render_block_buffer(psx_gpu);
105 render_state_base &= ~(0xF << 6);
106 render_state_base |= ((texture_settings >> 5) & 0xF) << 6;
108 psx_gpu->render_state_base = render_state_base;
110 psx_gpu->current_texture_mask = 0x1 << new_texture_page;
112 if(texture_mode == TEXTURE_MODE_8BPP)
114 // In 8bpp mode 256x256 takes up two pages. If it's on the right edge it
115 // wraps back around to the left edge.
116 u32 adjacent_texture_page = ((texture_settings + 1) & 0xF) | (texture_settings & 0x10);
117 psx_gpu->current_texture_mask |= 0x1 << adjacent_texture_page;
119 if((psx_gpu->last_8bpp_texture_page ^ new_texture_page) & 0x1)
121 u32 dirty_textures_8bpp_alternate_mask =
122 psx_gpu->dirty_textures_8bpp_alternate_mask;
123 psx_gpu->dirty_textures_8bpp_alternate_mask =
124 psx_gpu->dirty_textures_8bpp_mask;
125 psx_gpu->dirty_textures_8bpp_mask = dirty_textures_8bpp_alternate_mask;
128 psx_gpu->last_8bpp_texture_page = new_texture_page;
131 psx_gpu->current_texture_page = new_texture_page;
132 psx_gpu->texture_settings = texture_settings;
134 update_texture_ptr(psx_gpu);
138 void set_clut(psx_gpu_struct *psx_gpu, u32 clut_settings)
140 if(psx_gpu->clut_settings != clut_settings)
142 flush_render_block_buffer(psx_gpu);
143 psx_gpu->clut_settings = clut_settings;
144 psx_gpu->clut_ptr = psx_gpu->vram_ptr + ((clut_settings & 0x7FFF) * 16);
148 void set_triangle_color(psx_gpu_struct *psx_gpu, u32 triangle_color)
150 if(psx_gpu->triangle_color != triangle_color)
152 flush_render_block_buffer(psx_gpu);
153 psx_gpu->triangle_color = triangle_color;
157 static void do_fill(psx_gpu_struct *psx_gpu, u32 x, u32 y,
158 u32 width, u32 height, u32 color)
161 width = ((width + 0xF) & ~0xF);
163 flush_render_block_buffer(psx_gpu);
165 if(unlikely((x + width) > 1024))
167 u32 width_a = 1024 - x;
168 u32 width_b = width - width_a;
170 if(unlikely((y + height) > 512))
172 u32 height_a = 512 - y;
173 u32 height_b = height - height_a;
175 render_block_fill(psx_gpu, color, x, y, width_a, height_a);
176 render_block_fill(psx_gpu, color, 0, y, width_b, height_a);
177 render_block_fill(psx_gpu, color, x, 0, width_a, height_b);
178 render_block_fill(psx_gpu, color, 0, 0, width_b, height_b);
182 render_block_fill(psx_gpu, color, x, y, width_a, height);
183 render_block_fill(psx_gpu, color, 0, y, width_b, height);
188 if(unlikely((y + height) > 512))
190 u32 height_a = 512 - y;
191 u32 height_b = height - height_a;
193 render_block_fill(psx_gpu, color, x, y, width, height_a);
194 render_block_fill(psx_gpu, color, x, 0, width, height_b);
198 render_block_fill(psx_gpu, color, x, y, width, height);
203 #define sign_extend_11bit(value) \
204 (((s32)((value) << 21)) >> 21) \
206 #define get_vertex_data_xy(vertex_number, offset16) \
207 vertexes[vertex_number].x = \
208 sign_extend_11bit(list_s16[offset16]) + psx_gpu->offset_x; \
209 vertexes[vertex_number].y = \
210 sign_extend_11bit(list_s16[(offset16) + 1]) + psx_gpu->offset_y; \
212 #define get_vertex_data_uv(vertex_number, offset16) \
213 vertexes[vertex_number].u = list_s16[offset16] & 0xFF; \
214 vertexes[vertex_number].v = (list_s16[offset16] >> 8) & 0xFF \
216 #define get_vertex_data_rgb(vertex_number, offset32) \
217 vertexes[vertex_number].r = list[offset32] & 0xFF; \
218 vertexes[vertex_number].g = (list[offset32] >> 8) & 0xFF; \
219 vertexes[vertex_number].b = (list[offset32] >> 16) & 0xFF \
221 #define get_vertex_data_xy_uv(vertex_number, offset16) \
222 get_vertex_data_xy(vertex_number, offset16); \
223 get_vertex_data_uv(vertex_number, (offset16) + 2) \
225 #define get_vertex_data_xy_rgb(vertex_number, offset16) \
226 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
227 get_vertex_data_xy(vertex_number, (offset16) + 2); \
229 #define get_vertex_data_xy_uv_rgb(vertex_number, offset16) \
230 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
231 get_vertex_data_xy(vertex_number, (offset16) + 2); \
232 get_vertex_data_uv(vertex_number, (offset16) + 4); \
234 #define set_vertex_color_constant(vertex_number, color) \
235 vertexes[vertex_number].r = color & 0xFF; \
236 vertexes[vertex_number].g = (color >> 8) & 0xFF; \
237 vertexes[vertex_number].b = (color >> 16) & 0xFF \
239 #define get_vertex_data_xy_rgb_constant(vertex_number, offset16, color) \
240 get_vertex_data_xy(vertex_number, offset16); \
241 set_vertex_color_constant(vertex_number, color) \
247 u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size,
248 s32 *cpu_cycles_sum_out, s32 *cpu_cycles_last, u32 *last_command)
250 vertex_struct vertexes[4] __attribute__((aligned(16))) = {};
251 u32 current_command = 0, command_length;
252 u32 cpu_cycles_sum = 0, cpu_cycles = *cpu_cycles_last;
254 u32 *list_start = list;
255 u32 *list_end = list + (size / 4);
257 for(; list < list_end; list += 1 + command_length)
259 s16 *list_s16 = (void *)list;
260 current_command = *list >> 24;
261 command_length = command_lengths[current_command];
262 if (list + 1 + command_length > list_end) {
263 current_command = (u32)-1;
267 switch(current_command)
274 u32 x = list_s16[2] & 0x3FF;
275 u32 y = list_s16[3] & 0x1FF;
276 u32 width = list_s16[4] & 0x3FF;
277 u32 height = list_s16[5] & 0x1FF;
278 u32 color = list[0] & 0xFFFFFF;
280 do_fill(psx_gpu, x, y, width, height, color);
281 gput_sum(cpu_cycles_sum, cpu_cycles, gput_fill(width, height));
287 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
289 get_vertex_data_xy(0, 2);
290 get_vertex_data_xy(1, 4);
291 get_vertex_data_xy(2, 6);
293 render_triangle(psx_gpu, vertexes, current_command);
294 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base());
300 set_clut(psx_gpu, list_s16[5]);
301 set_texture(psx_gpu, list_s16[9]);
302 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
304 get_vertex_data_xy_uv(0, 2);
305 get_vertex_data_xy_uv(1, 6);
306 get_vertex_data_xy_uv(2, 10);
308 render_triangle(psx_gpu, vertexes, current_command);
309 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_t());
315 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
317 get_vertex_data_xy(0, 2);
318 get_vertex_data_xy(1, 4);
319 get_vertex_data_xy(2, 6);
320 get_vertex_data_xy(3, 8);
322 render_triangle(psx_gpu, vertexes, current_command);
323 render_triangle(psx_gpu, &(vertexes[1]), current_command);
324 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base());
330 set_clut(psx_gpu, list_s16[5]);
331 set_texture(psx_gpu, list_s16[9]);
332 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
334 get_vertex_data_xy_uv(0, 2);
335 get_vertex_data_xy_uv(1, 6);
336 get_vertex_data_xy_uv(2, 10);
337 get_vertex_data_xy_uv(3, 14);
339 render_triangle(psx_gpu, vertexes, current_command);
340 render_triangle(psx_gpu, &(vertexes[1]), current_command);
341 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_t());
347 get_vertex_data_xy_rgb(0, 0);
348 get_vertex_data_xy_rgb(1, 4);
349 get_vertex_data_xy_rgb(2, 8);
351 render_triangle(psx_gpu, vertexes, current_command);
352 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_g());
358 set_clut(psx_gpu, list_s16[5]);
359 set_texture(psx_gpu, list_s16[11]);
361 get_vertex_data_xy_uv_rgb(0, 0);
362 get_vertex_data_xy_uv_rgb(1, 6);
363 get_vertex_data_xy_uv_rgb(2, 12);
365 render_triangle(psx_gpu, vertexes, current_command);
366 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_gt());
372 get_vertex_data_xy_rgb(0, 0);
373 get_vertex_data_xy_rgb(1, 4);
374 get_vertex_data_xy_rgb(2, 8);
375 get_vertex_data_xy_rgb(3, 12);
377 render_triangle(psx_gpu, vertexes, current_command);
378 render_triangle(psx_gpu, &(vertexes[1]), current_command);
379 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_g());
385 set_clut(psx_gpu, list_s16[5]);
386 set_texture(psx_gpu, list_s16[11]);
388 get_vertex_data_xy_uv_rgb(0, 0);
389 get_vertex_data_xy_uv_rgb(1, 6);
390 get_vertex_data_xy_uv_rgb(2, 12);
391 get_vertex_data_xy_uv_rgb(3, 18);
393 render_triangle(psx_gpu, vertexes, current_command);
394 render_triangle(psx_gpu, &(vertexes[1]), current_command);
395 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_gt());
401 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
402 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
403 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
404 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
406 render_line(psx_gpu, vertexes, current_command, list[0], 0);
407 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
413 u32 num_vertexes = 1;
414 u32 *list_position = &(list[2]);
417 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
418 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
423 vertexes[0] = vertexes[1];
425 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
426 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
428 render_line(psx_gpu, vertexes, current_command, list[0], 0);
429 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
434 if(list_position >= list_end)
436 current_command = (u32)-1;
441 if((xy & 0xF000F000) == 0x50005000)
445 command_length += (num_vertexes - 2);
451 vertexes[0].r = list[0] & 0xFF;
452 vertexes[0].g = (list[0] >> 8) & 0xFF;
453 vertexes[0].b = (list[0] >> 16) & 0xFF;
454 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
455 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
457 vertexes[1].r = list[2] & 0xFF;
458 vertexes[1].g = (list[2] >> 8) & 0xFF;
459 vertexes[1].b = (list[2] >> 16) & 0xFF;
460 vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
461 vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
463 render_line(psx_gpu, vertexes, current_command, 0, 0);
464 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
470 u32 num_vertexes = 1;
471 u32 *list_position = &(list[2]);
475 vertexes[1].r = color & 0xFF;
476 vertexes[1].g = (color >> 8) & 0xFF;
477 vertexes[1].b = (color >> 16) & 0xFF;
478 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
479 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
481 color = list_position[0];
484 xy = list_position[1];
486 vertexes[0] = vertexes[1];
488 vertexes[1].r = color & 0xFF;
489 vertexes[1].g = (color >> 8) & 0xFF;
490 vertexes[1].b = (color >> 16) & 0xFF;
491 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
492 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
494 render_line(psx_gpu, vertexes, current_command, 0, 0);
495 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
500 if(list_position >= list_end)
502 current_command = (u32)-1;
506 color = list_position[0];
507 if((color & 0xF000F000) == 0x50005000)
511 command_length += ((num_vertexes - 2) * 2);
517 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
518 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
519 s32 width = list_s16[4] & 0x3FF;
520 s32 height = list_s16[5] & 0x1FF;
522 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
523 current_command, list[0]);
524 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
530 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
531 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
532 u32 uv = list_s16[4];
533 s32 width = list_s16[6] & 0x3FF;
534 s32 height = list_s16[7] & 0x1FF;
536 set_clut(psx_gpu, list_s16[5]);
538 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF,
539 &width, &height, current_command, list[0]);
540 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
546 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
547 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
548 s32 width = 1, height = 1;
550 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
551 current_command, list[0]);
552 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(1, 1));
558 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
559 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
560 s32 width = 8, height = 8;
562 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
563 current_command, list[0]);
564 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
570 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
571 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
572 u32 uv = list_s16[4];
573 s32 width = 8, height = 8;
575 set_clut(psx_gpu, list_s16[5]);
577 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF,
578 &width, &height, current_command, list[0]);
579 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
585 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
586 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
587 s32 width = 16, height = 16;
589 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
590 current_command, list[0]);
591 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
597 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
598 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
599 u32 uv = list_s16[4];
600 s32 width = 16, height = 16;
602 set_clut(psx_gpu, list_s16[5]);
604 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF,
605 &width, &height, current_command, list[0]);
606 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
612 case 0x80 ... 0x9F: // vid -> vid
613 case 0xA0 ... 0xBF: // sys -> vid
614 case 0xC0 ... 0xDF: // vid -> sys
617 case 0x80 ... 0x9F: // vid -> vid
619 u32 sx = list_s16[2] & 0x3FF;
620 u32 sy = list_s16[3] & 0x1FF;
621 u32 dx = list_s16[4] & 0x3FF;
622 u32 dy = list_s16[5] & 0x1FF;
623 u32 w = ((list_s16[6] - 1) & 0x3FF) + 1;
624 u32 h = ((list_s16[7] - 1) & 0x1FF) + 1;
626 if (sx == dx && sy == dy && psx_gpu->mask_msb == 0)
629 render_block_move(psx_gpu, sx, sy, dx, dy, w, h);
633 case 0xA0 ... 0xBF: // sys -> vid
635 u32 load_x = list_s16[2] & 0x3FF;
636 u32 load_y = list_s16[3] & 0x1FF;
637 u32 load_width = list_s16[4] & 0x3FF;
638 u32 load_height = list_s16[5] & 0x1FF;
639 u32 load_size = load_width * load_height;
641 command_length += load_size / 2;
646 render_block_copy(psx_gpu, (u16 *)&(list_s16[6]), load_x, load_y,
647 load_width, load_height, load_width);
651 case 0xC0 ... 0xDF: // vid -> sys
656 set_texture(psx_gpu, list[0]);
658 if(list[0] & (1 << 9))
659 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
661 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
663 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
670 u32 texture_window_settings = list[0];
673 if(texture_window_settings != psx_gpu->texture_window_settings)
675 tmp = (texture_window_settings & 0x1F) | 0x20;
676 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
678 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
679 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
682 x = ((texture_window_settings >> 10) & tmp) << 3;
685 y = ((texture_window_settings >> 15) & tmp) << 3;
687 flush_render_block_buffer(psx_gpu);
689 psx_gpu->texture_window_settings = texture_window_settings;
690 psx_gpu->texture_window_x = x;
691 psx_gpu->texture_window_y = y;
692 psx_gpu->texture_mask_width = w - 1;
693 psx_gpu->texture_mask_height = h - 1;
695 update_texture_ptr(psx_gpu);
703 s16 viewport_start_x = list[0] & 0x3FF;
704 s16 viewport_start_y = (list[0] >> 10) & 0x1FF;
706 if(viewport_start_x == psx_gpu->viewport_start_x &&
707 viewport_start_y == psx_gpu->viewport_start_y)
712 psx_gpu->viewport_start_x = viewport_start_x;
713 psx_gpu->viewport_start_y = viewport_start_y;
715 #ifdef TEXTURE_CACHE_4BPP
716 psx_gpu->viewport_mask =
717 texture_region_mask(psx_gpu->viewport_start_x,
718 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
719 psx_gpu->viewport_end_y);
727 s16 viewport_end_x = list[0] & 0x3FF;
728 s16 viewport_end_y = (list[0] >> 10) & 0x1FF;
730 if(viewport_end_x == psx_gpu->viewport_end_x &&
731 viewport_end_y == psx_gpu->viewport_end_y)
736 psx_gpu->viewport_end_x = viewport_end_x;
737 psx_gpu->viewport_end_y = viewport_end_y;
739 #ifdef TEXTURE_CACHE_4BPP
740 psx_gpu->viewport_mask =
741 texture_region_mask(psx_gpu->viewport_start_x,
742 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
743 psx_gpu->viewport_end_y);
751 s32 offset_x = list[0] << 21;
752 s32 offset_y = list[0] << 10;
753 psx_gpu->offset_x = offset_x >> 21;
754 psx_gpu->offset_y = offset_y >> 21;
762 u32 mask_settings = list[0];
763 u16 mask_msb = mask_settings << 15;
766 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
768 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
770 if(mask_msb != psx_gpu->mask_msb)
772 flush_render_block_buffer(psx_gpu);
773 psx_gpu->mask_msb = mask_msb;
786 *cpu_cycles_sum_out += cpu_cycles_sum;
787 *cpu_cycles_last = cpu_cycles;
788 *last_command = current_command;
789 return list - list_start;
794 // this thing has become such a PITA, should just handle the 2048 width really
795 static void update_enhancement_buf_scanouts(psx_gpu_struct *psx_gpu,
796 int x, int y, int w, int h)
798 int max_bufs = ARRAY_SIZE(psx_gpu->enhancement_scanouts);
799 struct psx_gpu_scanout *s;
800 int i, sel, right, bottom;
801 u32 tol_x = 48, tol_y = 16;
804 //w = (w + 15) & ~15;
805 psx_gpu->saved_hres = w;
806 assert(!(max_bufs & (max_bufs - 1)));
807 for (i = 0; i < max_bufs; i++) {
808 s = &psx_gpu->enhancement_scanouts[i];
809 if (s->x == x && s->y == y && w - s->w <= tol_x && h - s->h <= tol_y)
813 // evict any scanout that intersects
816 for (i = 0, sel = -1; i < max_bufs; i++) {
817 s = &psx_gpu->enhancement_scanouts[i];
818 if (s->x >= right) continue;
819 if (s->x + s->w <= x) continue;
820 if (s->y >= bottom) continue;
821 if (s->y + s->h <= y) continue;
822 // ... but allow upto 16 pixels intersection that some games do
823 if ((intersection = s->x + s->w - x) - 1u <= tol_x) {
824 s->w -= intersection;
827 if ((intersection = s->y + s->h - y) - 1u <= tol_y) {
828 s->h -= intersection;
831 //printf("%4d%4d%4dx%d evicted\n", s->x, s->y, s->w, s->h);
837 // 2nd intersection check
838 for (i = 0; i < max_bufs; i++) {
839 s = &psx_gpu->enhancement_scanouts[i];
842 if ((intersection = right - s->x) - 1u <= tol_x) {
846 if ((intersection = bottom - s->y) - 1u <= tol_y) {
853 sel = psx_gpu->enhancement_scanout_eselect++;
854 psx_gpu->enhancement_scanout_eselect &= max_bufs - 1;
855 s = &psx_gpu->enhancement_scanouts[sel];
861 sync_enhancement_buffers(x, y, w, h);
863 printf("scanouts:\n");
864 for (i = 0; i < ARRAY_SIZE(psx_gpu->enhancement_scanouts); i++) {
865 s = &psx_gpu->enhancement_scanouts[i];
867 printf("%4d%4d%4dx%d\n", s->x, s->y, s->w, s->h);
872 static int select_enhancement_buf_index(psx_gpu_struct *psx_gpu, s32 x, s32 y)
875 for (i = 0; i < ARRAY_SIZE(psx_gpu->enhancement_scanouts); i++) {
876 const struct psx_gpu_scanout *s = &psx_gpu->enhancement_scanouts[i];
877 if (s->x <= x && x < s->x + s->w &&
878 s->y <= y && y < s->y + s->h)
884 #define select_enhancement_buf_by_index(psx_gpu_, i_) \
885 ((psx_gpu_)->enhancement_buf_ptr + ((i_) << 20))
887 static void *select_enhancement_buf_ptr(psx_gpu_struct *psx_gpu, s32 x, s32 y)
889 int i = select_enhancement_buf_index(psx_gpu, x, y);
890 return i >= 0 ? select_enhancement_buf_by_index(psx_gpu, i) : NULL;
893 static void select_enhancement_buf(psx_gpu_struct *psx_gpu)
895 s32 x = psx_gpu->saved_viewport_start_x + 16;
896 s32 y = psx_gpu->saved_viewport_start_y + 16;
897 psx_gpu->enhancement_current_buf_ptr = select_enhancement_buf_ptr(psx_gpu, x, y);
900 #define enhancement_disable() { \
901 psx_gpu->vram_out_ptr = psx_gpu->vram_ptr; \
902 psx_gpu->viewport_start_x = psx_gpu->saved_viewport_start_x; \
903 psx_gpu->viewport_start_y = psx_gpu->saved_viewport_start_y; \
904 psx_gpu->viewport_end_x = psx_gpu->saved_viewport_end_x; \
905 psx_gpu->viewport_end_y = psx_gpu->saved_viewport_end_y; \
906 psx_gpu->uvrgb_phase = 0x8000; \
909 static int enhancement_enable(psx_gpu_struct *psx_gpu)
911 if (!psx_gpu->enhancement_current_buf_ptr)
913 psx_gpu->vram_out_ptr = psx_gpu->enhancement_current_buf_ptr;
914 psx_gpu->viewport_start_x = psx_gpu->saved_viewport_start_x * 2;
915 psx_gpu->viewport_start_y = psx_gpu->saved_viewport_start_y * 2;
916 psx_gpu->viewport_end_x = psx_gpu->saved_viewport_end_x * 2 + 1;
917 psx_gpu->viewport_end_y = psx_gpu->saved_viewport_end_y * 2 + 1;
918 if (psx_gpu->viewport_end_x - psx_gpu->viewport_start_x + 1 > 1024)
919 psx_gpu->viewport_end_x = psx_gpu->viewport_start_x + 1023;
920 psx_gpu->uvrgb_phase = 0x7fff;
924 #define shift_vertices3(v) { \
933 #define unshift_vertices3(v) { \
942 #define shift_triangle_area() \
943 psx_gpu->triangle_area *= 4
946 void scale2x_tiles8(void *dst, const void *src, int w8, int h)
948 uint16_t* d = (uint16_t*)dst;
949 const uint16_t* s = (const uint16_t*)src;
953 uint16_t* d_save = d;
954 const uint16_t* s_save = s;
1009 s = s_save + 1024; /* or 512? */
1014 // simple check for a case where no clipping is used
1015 // - now handled by adjusting the viewport
1016 static int check_enhanced_range(psx_gpu_struct *psx_gpu, int x, int y)
1021 static int is_in_array(int val, int array[], int len)
1024 for (i = 0; i < len; i++)
1025 if (array[i] == val)
1030 static int make_members_unique(int array[], int len)
1033 for (i = j = 1; i < len; i++)
1034 if (!is_in_array(array[i], array, j))
1035 array[j++] = array[i];
1037 if (array[0] > array[1]) {
1038 i = array[0]; array[0] = array[1]; array[1] = i;
1043 static void patch_u(vertex_struct *vertex_ptrs, int count, int old, int new)
1046 for (i = 0; i < count; i++)
1047 if (vertex_ptrs[i].u == old)
1048 vertex_ptrs[i].u = new;
1051 static void patch_v(vertex_struct *vertex_ptrs, int count, int old, int new)
1054 for (i = 0; i < count; i++)
1055 if (vertex_ptrs[i].v == old)
1056 vertex_ptrs[i].v = new;
1059 // this sometimes does more harm than good, like in PE2
1060 static void uv_hack(vertex_struct *vertex_ptrs, int vertex_count)
1064 for (i = 0; i < vertex_count; i++) {
1065 u[i] = vertex_ptrs[i].u;
1066 v[i] = vertex_ptrs[i].v;
1068 if (make_members_unique(u, vertex_count) == 2 && u[1] - u[0] >= 8) {
1069 if ((u[0] & 7) == 7) {
1070 patch_u(vertex_ptrs, vertex_count, u[0], u[0] + 1);
1071 //printf("u hack: %3u-%3u -> %3u-%3u\n", u[0], u[1], u[0]+1, u[1]);
1073 else if ((u[1] & 7) == 0 || u[1] - u[0] > 128) {
1074 patch_u(vertex_ptrs, vertex_count, u[1], u[1] - 1);
1075 //printf("u hack: %3u-%3u -> %3u-%3u\n", u[0], u[1], u[0], u[1]-1);
1078 if (make_members_unique(v, vertex_count) == 2 && ((v[0] - v[1]) & 7) == 0) {
1079 if ((v[0] & 7) == 7) {
1080 patch_v(vertex_ptrs, vertex_count, v[0], v[0] + 1);
1081 //printf("v hack: %3u-%3u -> %3u-%3u\n", v[0], v[1], v[0]+1, v[1]);
1083 else if ((v[1] & 7) == 0) {
1084 patch_v(vertex_ptrs, vertex_count, v[1], v[1] - 1);
1085 //printf("v hack: %3u-%3u -> %3u-%3u\n", v[0], v[1], v[0], v[1]-1);
1090 static void do_triangle_enhanced(psx_gpu_struct *psx_gpu,
1091 vertex_struct *vertexes, u32 current_command)
1093 vertex_struct *vertex_ptrs[3];
1095 if (!prepare_triangle(psx_gpu, vertexes, vertex_ptrs))
1098 if (!psx_gpu->hack_disable_main)
1099 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
1101 if (!check_enhanced_range(psx_gpu, vertex_ptrs[0]->x, vertex_ptrs[2]->x))
1104 if (!enhancement_enable(psx_gpu))
1107 shift_vertices3(vertex_ptrs);
1108 shift_triangle_area();
1109 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
1110 unshift_vertices3(vertex_ptrs);
1113 static void do_quad_enhanced(psx_gpu_struct *psx_gpu, vertex_struct *vertexes,
1114 u32 current_command)
1116 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1117 enhancement_disable();
1118 do_triangle_enhanced(psx_gpu, &vertexes[1], current_command);
1123 #define fill_vertex(i, x_, y_, u_, v_, rgb_) \
1124 vertexes[i].x = x_; \
1125 vertexes[i].y = y_; \
1126 vertexes[i].u = u_; \
1127 vertexes[i].v = v_; \
1128 vertexes[i].r = rgb_; \
1129 vertexes[i].g = (rgb_) >> 8; \
1130 vertexes[i].b = (rgb_) >> 16
1132 static void do_sprite_enhanced(psx_gpu_struct *psx_gpu, int x, int y,
1133 u32 u, u32 v, u32 w, u32 h, u32 cmd_rgb)
1135 vertex_struct *vertex_ptrs[3];
1136 u32 flags = (cmd_rgb >> 24);
1137 u32 color = cmd_rgb & 0xffffff;
1138 u32 render_state_base_saved = psx_gpu->render_state_base;
1143 (RENDER_FLAGS_MODULATE_TEXELS | RENDER_FLAGS_BLEND |
1144 RENDER_FLAGS_TEXTURE_MAP);
1146 set_triangle_color(psx_gpu, color);
1147 if(color == 0x808080)
1148 flags |= RENDER_FLAGS_MODULATE_TEXELS;
1150 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
1151 enhancement_enable();
1158 if (u1 < u) u1 = 0xff;
1159 if (v1 < v) v1 = 0xff;
1164 fill_vertex(0, x, y, u, v, color);
1165 fill_vertex(1, x, y1, u, v1, color);
1166 fill_vertex(2, x1, y, u1, v, color);
1167 if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs)) {
1168 shift_vertices3(vertex_ptrs);
1169 shift_triangle_area();
1170 render_triangle_p(psx_gpu, vertex_ptrs, flags);
1176 fill_vertex(0, x1, y, u1, v, color);
1177 fill_vertex(1, x, y1, u, v1, color);
1178 fill_vertex(2, x1, y1, u1, v1, color);
1179 if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs)) {
1180 shift_vertices3(vertex_ptrs);
1181 shift_triangle_area();
1182 render_triangle_p(psx_gpu, vertex_ptrs, flags);
1185 psx_gpu->render_state_base = render_state_base_saved;
1188 static void do_sprite_enhanced(psx_gpu_struct *psx_gpu, int x, int y,
1189 u32 u, u32 v, u32 w, u32 h, u32 cmd_rgb)
1191 u32 flags = (cmd_rgb >> 24);
1192 u32 color = cmd_rgb & 0xffffff;
1194 render_sprite_4x(psx_gpu, x, y, u, v, w, h, flags, color);
1198 u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size,
1199 s32 *cpu_cycles_sum_out, s32 *cpu_cycles_last, u32 *last_command)
1201 vertex_struct vertexes[4] __attribute__((aligned(16))) = {};
1202 u32 current_command = 0, command_length;
1203 u32 cpu_cycles_sum = 0, cpu_cycles = *cpu_cycles_last;
1205 u32 *list_start = list;
1206 u32 *list_end = list + (size / 4);
1208 psx_gpu->saved_viewport_start_x = psx_gpu->viewport_start_x;
1209 psx_gpu->saved_viewport_start_y = psx_gpu->viewport_start_y;
1210 psx_gpu->saved_viewport_end_x = psx_gpu->viewport_end_x;
1211 psx_gpu->saved_viewport_end_y = psx_gpu->viewport_end_y;
1212 select_enhancement_buf(psx_gpu);
1214 for(; list < list_end; list += 1 + command_length)
1216 s16 *list_s16 = (void *)list;
1217 current_command = *list >> 24;
1218 command_length = command_lengths[current_command];
1219 if (list + 1 + command_length > list_end) {
1220 current_command = (u32)-1;
1224 enhancement_disable();
1226 switch(current_command)
1233 u32 x = list_s16[2] & 0x3FF;
1234 u32 y = list_s16[3] & 0x1FF;
1235 u32 width = list_s16[4] & 0x3FF;
1236 u32 height = list_s16[5] & 0x1FF;
1237 u32 color = list[0] & 0xFFFFFF;
1241 width = ((width + 0xF) & ~0xF);
1242 gput_sum(cpu_cycles_sum, cpu_cycles, gput_fill(width, height));
1243 if (width == 0 || height == 0)
1246 do_fill(psx_gpu, x, y, width, height, color);
1248 i1 = select_enhancement_buf_index(psx_gpu, x, y);
1249 i2 = select_enhancement_buf_index(psx_gpu, x + width - 1, y + height - 1);
1250 if (i1 < 0 || i1 != i2) {
1251 sync_enhancement_buffers(x, y, width, height);
1255 psx_gpu->vram_out_ptr = select_enhancement_buf_by_index(psx_gpu, i1);
1260 render_block_fill_enh(psx_gpu, color, x, y, width, height);
1266 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1268 get_vertex_data_xy(0, 2);
1269 get_vertex_data_xy(1, 4);
1270 get_vertex_data_xy(2, 6);
1272 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1273 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base());
1279 set_clut(psx_gpu, list_s16[5]);
1280 set_texture(psx_gpu, list_s16[9]);
1281 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1283 get_vertex_data_xy_uv(0, 2);
1284 get_vertex_data_xy_uv(1, 6);
1285 get_vertex_data_xy_uv(2, 10);
1287 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1288 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_t());
1294 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1296 get_vertex_data_xy(0, 2);
1297 get_vertex_data_xy(1, 4);
1298 get_vertex_data_xy(2, 6);
1299 get_vertex_data_xy(3, 8);
1301 do_quad_enhanced(psx_gpu, vertexes, current_command);
1302 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base());
1308 set_clut(psx_gpu, list_s16[5]);
1309 set_texture(psx_gpu, list_s16[9]);
1310 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1312 get_vertex_data_xy_uv(0, 2);
1313 get_vertex_data_xy_uv(1, 6);
1314 get_vertex_data_xy_uv(2, 10);
1315 get_vertex_data_xy_uv(3, 14);
1317 if (psx_gpu->hack_texture_adj)
1318 uv_hack(vertexes, 4);
1319 do_quad_enhanced(psx_gpu, vertexes, current_command);
1320 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_t());
1326 get_vertex_data_xy_rgb(0, 0);
1327 get_vertex_data_xy_rgb(1, 4);
1328 get_vertex_data_xy_rgb(2, 8);
1330 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1331 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_g());
1337 set_clut(psx_gpu, list_s16[5]);
1338 set_texture(psx_gpu, list_s16[11]);
1340 get_vertex_data_xy_uv_rgb(0, 0);
1341 get_vertex_data_xy_uv_rgb(1, 6);
1342 get_vertex_data_xy_uv_rgb(2, 12);
1344 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1345 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_gt());
1351 get_vertex_data_xy_rgb(0, 0);
1352 get_vertex_data_xy_rgb(1, 4);
1353 get_vertex_data_xy_rgb(2, 8);
1354 get_vertex_data_xy_rgb(3, 12);
1356 do_quad_enhanced(psx_gpu, vertexes, current_command);
1357 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_g());
1363 set_clut(psx_gpu, list_s16[5]);
1364 set_texture(psx_gpu, list_s16[11]);
1366 get_vertex_data_xy_uv_rgb(0, 0);
1367 get_vertex_data_xy_uv_rgb(1, 6);
1368 get_vertex_data_xy_uv_rgb(2, 12);
1369 get_vertex_data_xy_uv_rgb(3, 18);
1371 if (psx_gpu->hack_texture_adj)
1372 uv_hack(vertexes, 4);
1373 do_quad_enhanced(psx_gpu, vertexes, current_command);
1374 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_gt());
1380 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
1381 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
1382 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
1383 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
1385 render_line(psx_gpu, vertexes, current_command, list[0], 0);
1386 if (enhancement_enable(psx_gpu))
1387 render_line(psx_gpu, vertexes, current_command, list[0], 1);
1388 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
1394 u32 num_vertexes = 1;
1395 u32 *list_position = &(list[2]);
1398 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1399 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1401 xy = *list_position;
1404 vertexes[0] = vertexes[1];
1406 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1407 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1409 enhancement_disable();
1410 render_line(psx_gpu, vertexes, current_command, list[0], 0);
1411 if (enhancement_enable(psx_gpu))
1412 render_line(psx_gpu, vertexes, current_command, list[0], 1);
1413 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
1418 if(list_position >= list_end)
1420 current_command = (u32)-1;
1424 xy = *list_position;
1425 if((xy & 0xF000F000) == 0x50005000)
1429 command_length += (num_vertexes - 2);
1435 vertexes[0].r = list[0] & 0xFF;
1436 vertexes[0].g = (list[0] >> 8) & 0xFF;
1437 vertexes[0].b = (list[0] >> 16) & 0xFF;
1438 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
1439 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
1441 vertexes[1].r = list[2] & 0xFF;
1442 vertexes[1].g = (list[2] >> 8) & 0xFF;
1443 vertexes[1].b = (list[2] >> 16) & 0xFF;
1444 vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
1445 vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
1447 render_line(psx_gpu, vertexes, current_command, 0, 0);
1448 if (enhancement_enable(psx_gpu))
1449 render_line(psx_gpu, vertexes, current_command, 0, 1);
1450 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
1456 u32 num_vertexes = 1;
1457 u32 *list_position = &(list[2]);
1458 u32 color = list[0];
1461 vertexes[1].r = color & 0xFF;
1462 vertexes[1].g = (color >> 8) & 0xFF;
1463 vertexes[1].b = (color >> 16) & 0xFF;
1464 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1465 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1467 color = list_position[0];
1470 xy = list_position[1];
1472 vertexes[0] = vertexes[1];
1474 vertexes[1].r = color & 0xFF;
1475 vertexes[1].g = (color >> 8) & 0xFF;
1476 vertexes[1].b = (color >> 16) & 0xFF;
1477 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1478 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1480 enhancement_disable();
1481 render_line(psx_gpu, vertexes, current_command, 0, 0);
1482 if (enhancement_enable(psx_gpu))
1483 render_line(psx_gpu, vertexes, current_command, 0, 1);
1484 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
1489 if(list_position >= list_end)
1491 current_command = (u32)-1;
1495 color = list_position[0];
1496 if((color & 0xF000F000) == 0x50005000)
1500 command_length += ((num_vertexes - 2) * 2);
1506 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1507 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1508 s32 width = list_s16[4] & 0x3FF;
1509 s32 height = list_s16[5] & 0x1FF;
1511 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
1512 current_command, list[0]);
1513 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1515 if (check_enhanced_range(psx_gpu, x, x + width)) {
1516 width = list_s16[4] & 0x3FF;
1517 height = list_s16[5] & 0x1FF;
1518 do_sprite_enhanced(psx_gpu, x, y, 0, 0, width, height, list[0]);
1525 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1526 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1528 u8 v = list_s16[4] >> 8;
1529 s32 width = list_s16[6] & 0x3FF;
1530 s32 height = list_s16[7] & 0x1FF;
1532 set_clut(psx_gpu, list_s16[5]);
1534 render_sprite(psx_gpu, x, y, u, v,
1535 &width, &height, current_command, list[0]);
1536 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1538 if (check_enhanced_range(psx_gpu, x, x + width)) {
1539 width = list_s16[6] & 0x3FF;
1540 height = list_s16[7] & 0x1FF;
1541 do_sprite_enhanced(psx_gpu, x, y, u, v, width, height, list[0]);
1548 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1549 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1550 s32 width = 1, height = 1;
1552 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
1553 current_command, list[0]);
1554 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(1, 1));
1556 if (check_enhanced_range(psx_gpu, x, x + 1))
1557 do_sprite_enhanced(psx_gpu, x, y, 0, 0, 1, 1, list[0]);
1563 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1564 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1565 s32 width = 8, height = 8;
1567 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
1568 current_command, list[0]);
1569 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1571 if (check_enhanced_range(psx_gpu, x, x + 8))
1572 do_sprite_enhanced(psx_gpu, x, y, 0, 0, 8, 8, list[0]);
1578 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1579 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1581 u8 v = list_s16[4] >> 8;
1582 s32 width = 8, height = 8;
1584 set_clut(psx_gpu, list_s16[5]);
1586 render_sprite(psx_gpu, x, y, u, v,
1587 &width, &height, current_command, list[0]);
1588 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1590 if (check_enhanced_range(psx_gpu, x, x + 8))
1591 do_sprite_enhanced(psx_gpu, x, y, u, v, 8, 8, list[0]);
1597 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1598 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1599 s32 width = 16, height = 16;
1601 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
1602 current_command, list[0]);
1603 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1605 if (check_enhanced_range(psx_gpu, x, x + 16))
1606 do_sprite_enhanced(psx_gpu, x, y, 0, 0, 16, 16, list[0]);
1612 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1613 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1615 u8 v = list_s16[4] >> 8;
1616 s32 width = 16, height = 16;
1618 set_clut(psx_gpu, list_s16[5]);
1620 render_sprite(psx_gpu, x, y, u, v,
1621 &width, &height, current_command, list[0]);
1622 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1624 if (check_enhanced_range(psx_gpu, x, x + 16))
1625 do_sprite_enhanced(psx_gpu, x, y, u, v, 16, 16, list[0]);
1629 case 0x80 ... 0x9F: // vid -> vid
1630 case 0xA0 ... 0xBF: // sys -> vid
1631 case 0xC0 ... 0xDF: // vid -> sys
1635 set_texture(psx_gpu, list[0]);
1637 if(list[0] & (1 << 9))
1638 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
1640 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
1642 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
1649 u32 texture_window_settings = list[0];
1650 u32 tmp, x, y, w, h;
1652 if(texture_window_settings != psx_gpu->texture_window_settings)
1654 tmp = (texture_window_settings & 0x1F) | 0x20;
1655 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
1657 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
1658 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
1660 tmp = 32 - (w >> 3);
1661 x = ((texture_window_settings >> 10) & tmp) << 3;
1663 tmp = 32 - (h >> 3);
1664 y = ((texture_window_settings >> 15) & tmp) << 3;
1666 flush_render_block_buffer(psx_gpu);
1668 psx_gpu->texture_window_settings = texture_window_settings;
1669 psx_gpu->texture_window_x = x;
1670 psx_gpu->texture_window_y = y;
1671 psx_gpu->texture_mask_width = w - 1;
1672 psx_gpu->texture_mask_height = h - 1;
1674 update_texture_ptr(psx_gpu);
1682 s16 viewport_start_x = list[0] & 0x3FF;
1683 s16 viewport_start_y = (list[0] >> 10) & 0x1FF;
1685 if(viewport_start_x == psx_gpu->viewport_start_x &&
1686 viewport_start_y == psx_gpu->viewport_start_y)
1690 psx_gpu->viewport_start_x = viewport_start_x;
1691 psx_gpu->viewport_start_y = viewport_start_y;
1692 psx_gpu->saved_viewport_start_x = viewport_start_x;
1693 psx_gpu->saved_viewport_start_y = viewport_start_y;
1695 select_enhancement_buf(psx_gpu);
1697 #ifdef TEXTURE_CACHE_4BPP
1698 psx_gpu->viewport_mask =
1699 texture_region_mask(psx_gpu->viewport_start_x,
1700 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
1701 psx_gpu->viewport_end_y);
1709 s16 viewport_end_x = list[0] & 0x3FF;
1710 s16 viewport_end_y = (list[0] >> 10) & 0x1FF;
1712 if(viewport_end_x == psx_gpu->viewport_end_x &&
1713 viewport_end_y == psx_gpu->viewport_end_y)
1718 psx_gpu->viewport_end_x = viewport_end_x;
1719 psx_gpu->viewport_end_y = viewport_end_y;
1720 psx_gpu->saved_viewport_end_x = viewport_end_x;
1721 psx_gpu->saved_viewport_end_y = viewport_end_y;
1723 select_enhancement_buf(psx_gpu);
1725 if (!psx_gpu->enhancement_current_buf_ptr)
1726 log_anomaly("vp %3d,%3d %3d,%d - no buf\n",
1727 psx_gpu->viewport_start_x, psx_gpu->viewport_start_y,
1728 viewport_end_x, viewport_end_y);
1730 #ifdef TEXTURE_CACHE_4BPP
1731 psx_gpu->viewport_mask =
1732 texture_region_mask(psx_gpu->viewport_start_x,
1733 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
1734 psx_gpu->viewport_end_y);
1742 psx_gpu->offset_x = sign_extend_11bit(list[0]);
1743 psx_gpu->offset_y = sign_extend_11bit(list[0] >> 11);
1751 u32 mask_settings = list[0];
1752 u16 mask_msb = mask_settings << 15;
1755 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
1757 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
1759 if(mask_msb != psx_gpu->mask_msb)
1761 flush_render_block_buffer(psx_gpu);
1762 psx_gpu->mask_msb = mask_msb;
1774 enhancement_disable();
1777 *cpu_cycles_sum_out += cpu_cycles_sum;
1778 *cpu_cycles_last = cpu_cycles;
1779 *last_command = current_command;
1780 return list - list_start;
1785 // vim:ts=2:shiftwidth=2:expandtab