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_12bit(value) \
204 (((s32)((value) << 20)) >> 20) \
206 #define sign_extend_11bit(value) \
207 (((s32)((value) << 21)) >> 21) \
209 #define sign_extend_10bit(value) \
210 (((s32)((value) << 22)) >> 22) \
213 #define get_vertex_data_xy(vertex_number, offset16) \
214 vertexes[vertex_number].x = \
215 sign_extend_12bit(list_s16[offset16]) + psx_gpu->offset_x; \
216 vertexes[vertex_number].y = \
217 sign_extend_12bit(list_s16[(offset16) + 1]) + psx_gpu->offset_y; \
219 #define get_vertex_data_uv(vertex_number, offset16) \
220 vertexes[vertex_number].u = list_s16[offset16] & 0xFF; \
221 vertexes[vertex_number].v = (list_s16[offset16] >> 8) & 0xFF \
223 #define get_vertex_data_rgb(vertex_number, offset32) \
224 vertexes[vertex_number].r = list[offset32] & 0xFF; \
225 vertexes[vertex_number].g = (list[offset32] >> 8) & 0xFF; \
226 vertexes[vertex_number].b = (list[offset32] >> 16) & 0xFF \
228 #define get_vertex_data_xy_uv(vertex_number, offset16) \
229 get_vertex_data_xy(vertex_number, offset16); \
230 get_vertex_data_uv(vertex_number, (offset16) + 2) \
232 #define get_vertex_data_xy_rgb(vertex_number, offset16) \
233 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
234 get_vertex_data_xy(vertex_number, (offset16) + 2); \
236 #define get_vertex_data_xy_uv_rgb(vertex_number, offset16) \
237 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
238 get_vertex_data_xy(vertex_number, (offset16) + 2); \
239 get_vertex_data_uv(vertex_number, (offset16) + 4); \
241 #define set_vertex_color_constant(vertex_number, color) \
242 vertexes[vertex_number].r = color & 0xFF; \
243 vertexes[vertex_number].g = (color >> 8) & 0xFF; \
244 vertexes[vertex_number].b = (color >> 16) & 0xFF \
246 #define get_vertex_data_xy_rgb_constant(vertex_number, offset16, color) \
247 get_vertex_data_xy(vertex_number, offset16); \
248 set_vertex_color_constant(vertex_number, color) \
254 u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size,
255 s32 *cpu_cycles_sum_out, s32 *cpu_cycles_last, u32 *last_command)
257 vertex_struct vertexes[4] __attribute__((aligned(16))) = {};
258 u32 current_command = 0, command_length;
259 u32 cpu_cycles_sum = 0, cpu_cycles = *cpu_cycles_last;
261 u32 *list_start = list;
262 u32 *list_end = list + (size / 4);
264 for(; list < list_end; list += 1 + command_length)
266 s16 *list_s16 = (void *)list;
267 current_command = *list >> 24;
268 command_length = command_lengths[current_command];
269 if (list + 1 + command_length > list_end) {
270 current_command = (u32)-1;
274 switch(current_command)
281 u32 x = list_s16[2] & 0x3FF;
282 u32 y = list_s16[3] & 0x1FF;
283 u32 width = list_s16[4] & 0x3FF;
284 u32 height = list_s16[5] & 0x1FF;
285 u32 color = list[0] & 0xFFFFFF;
287 do_fill(psx_gpu, x, y, width, height, color);
288 gput_sum(cpu_cycles_sum, cpu_cycles, gput_fill(width, height));
294 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
296 get_vertex_data_xy(0, 2);
297 get_vertex_data_xy(1, 4);
298 get_vertex_data_xy(2, 6);
300 render_triangle(psx_gpu, vertexes, current_command);
301 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base());
307 set_clut(psx_gpu, list_s16[5]);
308 set_texture(psx_gpu, list_s16[9]);
309 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
311 get_vertex_data_xy_uv(0, 2);
312 get_vertex_data_xy_uv(1, 6);
313 get_vertex_data_xy_uv(2, 10);
315 render_triangle(psx_gpu, vertexes, current_command);
316 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_t());
322 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
324 get_vertex_data_xy(0, 2);
325 get_vertex_data_xy(1, 4);
326 get_vertex_data_xy(2, 6);
327 get_vertex_data_xy(3, 8);
329 render_triangle(psx_gpu, vertexes, current_command);
330 render_triangle(psx_gpu, &(vertexes[1]), current_command);
331 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base());
337 set_clut(psx_gpu, list_s16[5]);
338 set_texture(psx_gpu, list_s16[9]);
339 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
341 get_vertex_data_xy_uv(0, 2);
342 get_vertex_data_xy_uv(1, 6);
343 get_vertex_data_xy_uv(2, 10);
344 get_vertex_data_xy_uv(3, 14);
346 render_triangle(psx_gpu, vertexes, current_command);
347 render_triangle(psx_gpu, &(vertexes[1]), current_command);
348 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_t());
354 get_vertex_data_xy_rgb(0, 0);
355 get_vertex_data_xy_rgb(1, 4);
356 get_vertex_data_xy_rgb(2, 8);
358 render_triangle(psx_gpu, vertexes, current_command);
359 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_g());
365 set_clut(psx_gpu, list_s16[5]);
366 set_texture(psx_gpu, list_s16[11]);
368 get_vertex_data_xy_uv_rgb(0, 0);
369 get_vertex_data_xy_uv_rgb(1, 6);
370 get_vertex_data_xy_uv_rgb(2, 12);
372 render_triangle(psx_gpu, vertexes, current_command);
373 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_gt());
379 get_vertex_data_xy_rgb(0, 0);
380 get_vertex_data_xy_rgb(1, 4);
381 get_vertex_data_xy_rgb(2, 8);
382 get_vertex_data_xy_rgb(3, 12);
384 render_triangle(psx_gpu, vertexes, current_command);
385 render_triangle(psx_gpu, &(vertexes[1]), current_command);
386 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_g());
392 set_clut(psx_gpu, list_s16[5]);
393 set_texture(psx_gpu, list_s16[11]);
395 get_vertex_data_xy_uv_rgb(0, 0);
396 get_vertex_data_xy_uv_rgb(1, 6);
397 get_vertex_data_xy_uv_rgb(2, 12);
398 get_vertex_data_xy_uv_rgb(3, 18);
400 render_triangle(psx_gpu, vertexes, current_command);
401 render_triangle(psx_gpu, &(vertexes[1]), current_command);
402 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_gt());
408 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
409 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
410 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
411 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
413 render_line(psx_gpu, vertexes, current_command, list[0], 0);
414 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
420 u32 num_vertexes = 1;
421 u32 *list_position = &(list[2]);
424 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
425 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
430 vertexes[0] = vertexes[1];
432 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
433 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
435 render_line(psx_gpu, vertexes, current_command, list[0], 0);
436 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
441 if(list_position >= list_end)
443 current_command = (u32)-1;
448 if((xy & 0xF000F000) == 0x50005000)
452 command_length += (num_vertexes - 2);
458 vertexes[0].r = list[0] & 0xFF;
459 vertexes[0].g = (list[0] >> 8) & 0xFF;
460 vertexes[0].b = (list[0] >> 16) & 0xFF;
461 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
462 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
464 vertexes[1].r = list[2] & 0xFF;
465 vertexes[1].g = (list[2] >> 8) & 0xFF;
466 vertexes[1].b = (list[2] >> 16) & 0xFF;
467 vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
468 vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
470 render_line(psx_gpu, vertexes, current_command, 0, 0);
471 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
477 u32 num_vertexes = 1;
478 u32 *list_position = &(list[2]);
482 vertexes[1].r = color & 0xFF;
483 vertexes[1].g = (color >> 8) & 0xFF;
484 vertexes[1].b = (color >> 16) & 0xFF;
485 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
486 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
488 color = list_position[0];
491 xy = list_position[1];
493 vertexes[0] = vertexes[1];
495 vertexes[1].r = color & 0xFF;
496 vertexes[1].g = (color >> 8) & 0xFF;
497 vertexes[1].b = (color >> 16) & 0xFF;
498 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
499 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
501 render_line(psx_gpu, vertexes, current_command, 0, 0);
502 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
507 if(list_position >= list_end)
509 current_command = (u32)-1;
513 color = list_position[0];
514 if((color & 0xF000F000) == 0x50005000)
518 command_length += ((num_vertexes - 2) * 2);
524 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
525 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
526 s32 width = list_s16[4] & 0x3FF;
527 s32 height = list_s16[5] & 0x1FF;
529 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
530 current_command, list[0]);
531 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
537 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
538 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
539 u32 uv = list_s16[4];
540 s32 width = list_s16[6] & 0x3FF;
541 s32 height = list_s16[7] & 0x1FF;
543 set_clut(psx_gpu, list_s16[5]);
545 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF,
546 &width, &height, current_command, list[0]);
547 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
553 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
554 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
555 s32 width = 1, height = 1;
557 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
558 current_command, list[0]);
559 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(1, 1));
565 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
566 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
567 s32 width = 8, height = 8;
569 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
570 current_command, list[0]);
571 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
577 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
578 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
579 u32 uv = list_s16[4];
580 s32 width = 8, height = 8;
582 set_clut(psx_gpu, list_s16[5]);
584 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF,
585 &width, &height, current_command, list[0]);
586 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
592 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
593 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
594 s32 width = 16, height = 16;
596 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
597 current_command, list[0]);
598 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
604 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
605 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
606 u32 uv = list_s16[4];
607 s32 width = 16, height = 16;
609 set_clut(psx_gpu, list_s16[5]);
611 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF,
612 &width, &height, current_command, list[0]);
613 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
619 case 0x80 ... 0x9F: // vid -> vid
620 case 0xA0 ... 0xBF: // sys -> vid
621 case 0xC0 ... 0xDF: // vid -> sys
624 case 0x80 ... 0x9F: // vid -> vid
626 u32 sx = list_s16[2] & 0x3FF;
627 u32 sy = list_s16[3] & 0x1FF;
628 u32 dx = list_s16[4] & 0x3FF;
629 u32 dy = list_s16[5] & 0x1FF;
630 u32 w = ((list_s16[6] - 1) & 0x3FF) + 1;
631 u32 h = ((list_s16[7] - 1) & 0x1FF) + 1;
633 if (sx == dx && sy == dy && psx_gpu->mask_msb == 0)
636 render_block_move(psx_gpu, sx, sy, dx, dy, w, h);
640 case 0xA0 ... 0xBF: // sys -> vid
642 u32 load_x = list_s16[2] & 0x3FF;
643 u32 load_y = list_s16[3] & 0x1FF;
644 u32 load_width = list_s16[4] & 0x3FF;
645 u32 load_height = list_s16[5] & 0x1FF;
646 u32 load_size = load_width * load_height;
648 command_length += load_size / 2;
653 render_block_copy(psx_gpu, (u16 *)&(list_s16[6]), load_x, load_y,
654 load_width, load_height, load_width);
658 case 0xC0 ... 0xDF: // vid -> sys
663 set_texture(psx_gpu, list[0]);
665 if(list[0] & (1 << 9))
666 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
668 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
670 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
677 u32 texture_window_settings = list[0];
680 if(texture_window_settings != psx_gpu->texture_window_settings)
682 tmp = (texture_window_settings & 0x1F) | 0x20;
683 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
685 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
686 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
689 x = ((texture_window_settings >> 10) & tmp) << 3;
692 y = ((texture_window_settings >> 15) & tmp) << 3;
694 flush_render_block_buffer(psx_gpu);
696 psx_gpu->texture_window_settings = texture_window_settings;
697 psx_gpu->texture_window_x = x;
698 psx_gpu->texture_window_y = y;
699 psx_gpu->texture_mask_width = w - 1;
700 psx_gpu->texture_mask_height = h - 1;
702 update_texture_ptr(psx_gpu);
710 s16 viewport_start_x = list[0] & 0x3FF;
711 s16 viewport_start_y = (list[0] >> 10) & 0x1FF;
713 if(viewport_start_x == psx_gpu->viewport_start_x &&
714 viewport_start_y == psx_gpu->viewport_start_y)
719 psx_gpu->viewport_start_x = viewport_start_x;
720 psx_gpu->viewport_start_y = viewport_start_y;
722 #ifdef TEXTURE_CACHE_4BPP
723 psx_gpu->viewport_mask =
724 texture_region_mask(psx_gpu->viewport_start_x,
725 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
726 psx_gpu->viewport_end_y);
734 s16 viewport_end_x = list[0] & 0x3FF;
735 s16 viewport_end_y = (list[0] >> 10) & 0x1FF;
737 if(viewport_end_x == psx_gpu->viewport_end_x &&
738 viewport_end_y == psx_gpu->viewport_end_y)
743 psx_gpu->viewport_end_x = viewport_end_x;
744 psx_gpu->viewport_end_y = viewport_end_y;
746 #ifdef TEXTURE_CACHE_4BPP
747 psx_gpu->viewport_mask =
748 texture_region_mask(psx_gpu->viewport_start_x,
749 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
750 psx_gpu->viewport_end_y);
758 s32 offset_x = list[0] << 21;
759 s32 offset_y = list[0] << 10;
760 psx_gpu->offset_x = offset_x >> 21;
761 psx_gpu->offset_y = offset_y >> 21;
769 u32 mask_settings = list[0];
770 u16 mask_msb = mask_settings << 15;
773 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
775 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
777 if(mask_msb != psx_gpu->mask_msb)
779 flush_render_block_buffer(psx_gpu);
780 psx_gpu->mask_msb = mask_msb;
793 *cpu_cycles_sum_out += cpu_cycles_sum;
794 *cpu_cycles_last = cpu_cycles;
795 *last_command = current_command;
796 return list - list_start;
801 // this thing has become such a PITA, should just handle the 2048 width really
802 static void update_enhancement_buf_scanouts(psx_gpu_struct *psx_gpu,
803 int x, int y, int w, int h)
805 int max_bufs = ARRAY_SIZE(psx_gpu->enhancement_scanouts);
806 struct psx_gpu_scanout *s;
807 int i, sel, right, bottom;
808 u32 tol_x = 48, tol_y = 16;
811 //w = (w + 15) & ~15;
812 psx_gpu->saved_hres = w;
813 assert(!(max_bufs & (max_bufs - 1)));
814 for (i = 0; i < max_bufs; i++) {
815 s = &psx_gpu->enhancement_scanouts[i];
816 if (s->x == x && s->y == y && w - s->w <= tol_x && h - s->h <= tol_y)
820 // evict any scanout that intersects
823 for (i = 0, sel = -1; i < max_bufs; i++) {
824 s = &psx_gpu->enhancement_scanouts[i];
825 if (s->x >= right) continue;
826 if (s->x + s->w <= x) continue;
827 if (s->y >= bottom) continue;
828 if (s->y + s->h <= y) continue;
829 // ... but allow upto 16 pixels intersection that some games do
830 if ((intersection = s->x + s->w - x) - 1u <= tol_x) {
831 s->w -= intersection;
834 if ((intersection = s->y + s->h - y) - 1u <= tol_y) {
835 s->h -= intersection;
838 //printf("%4d%4d%4dx%d evicted\n", s->x, s->y, s->w, s->h);
844 // 2nd intersection check
845 for (i = 0; i < max_bufs; i++) {
846 s = &psx_gpu->enhancement_scanouts[i];
849 if ((intersection = right - s->x) - 1u <= tol_x) {
853 if ((intersection = bottom - s->y) - 1u <= tol_y) {
860 sel = psx_gpu->enhancement_scanout_eselect++;
861 psx_gpu->enhancement_scanout_eselect &= max_bufs - 1;
862 s = &psx_gpu->enhancement_scanouts[sel];
868 sync_enhancement_buffers(x, y, w, h);
870 printf("scanouts:\n");
871 for (i = 0; i < ARRAY_SIZE(psx_gpu->enhancement_scanouts); i++) {
872 s = &psx_gpu->enhancement_scanouts[i];
874 printf("%4d%4d%4dx%d\n", s->x, s->y, s->w, s->h);
879 static int select_enhancement_buf_index(psx_gpu_struct *psx_gpu, s32 x, s32 y)
882 for (i = 0; i < ARRAY_SIZE(psx_gpu->enhancement_scanouts); i++) {
883 const struct psx_gpu_scanout *s = &psx_gpu->enhancement_scanouts[i];
884 if (s->x <= x && x < s->x + s->w &&
885 s->y <= y && y < s->y + s->h)
891 #define select_enhancement_buf_by_index(psx_gpu_, i_) \
892 ((psx_gpu_)->enhancement_buf_ptr + ((i_) << 20))
894 static void *select_enhancement_buf_ptr(psx_gpu_struct *psx_gpu, s32 x, s32 y)
896 int i = select_enhancement_buf_index(psx_gpu, x, y);
897 return i >= 0 ? select_enhancement_buf_by_index(psx_gpu, i) : NULL;
900 static void select_enhancement_buf(psx_gpu_struct *psx_gpu)
902 s32 x = psx_gpu->saved_viewport_start_x + 16;
903 s32 y = psx_gpu->saved_viewport_start_y + 16;
904 psx_gpu->enhancement_current_buf_ptr = select_enhancement_buf_ptr(psx_gpu, x, y);
907 #define enhancement_disable() { \
908 psx_gpu->vram_out_ptr = psx_gpu->vram_ptr; \
909 psx_gpu->viewport_start_x = psx_gpu->saved_viewport_start_x; \
910 psx_gpu->viewport_start_y = psx_gpu->saved_viewport_start_y; \
911 psx_gpu->viewport_end_x = psx_gpu->saved_viewport_end_x; \
912 psx_gpu->viewport_end_y = psx_gpu->saved_viewport_end_y; \
913 psx_gpu->uvrgb_phase = 0x8000; \
916 static int enhancement_enable(psx_gpu_struct *psx_gpu)
918 if (!psx_gpu->enhancement_current_buf_ptr)
920 psx_gpu->vram_out_ptr = psx_gpu->enhancement_current_buf_ptr;
921 psx_gpu->viewport_start_x = psx_gpu->saved_viewport_start_x * 2;
922 psx_gpu->viewport_start_y = psx_gpu->saved_viewport_start_y * 2;
923 psx_gpu->viewport_end_x = psx_gpu->saved_viewport_end_x * 2 + 1;
924 psx_gpu->viewport_end_y = psx_gpu->saved_viewport_end_y * 2 + 1;
925 if (psx_gpu->viewport_end_x - psx_gpu->viewport_start_x + 1 > 1024)
926 psx_gpu->viewport_end_x = psx_gpu->viewport_start_x + 1023;
927 psx_gpu->uvrgb_phase = 0x7fff;
931 #define shift_vertices3(v) { \
940 #define unshift_vertices3(v) { \
949 #define shift_triangle_area() \
950 psx_gpu->triangle_area *= 4
953 void scale2x_tiles8(void *dst, const void *src, int w8, int h)
955 uint16_t* d = (uint16_t*)dst;
956 const uint16_t* s = (const uint16_t*)src;
960 uint16_t* d_save = d;
961 const uint16_t* s_save = s;
1016 s = s_save + 1024; /* or 512? */
1021 // simple check for a case where no clipping is used
1022 // - now handled by adjusting the viewport
1023 static int check_enhanced_range(psx_gpu_struct *psx_gpu, int x, int y)
1028 static int is_in_array(int val, int array[], int len)
1031 for (i = 0; i < len; i++)
1032 if (array[i] == val)
1037 static int make_members_unique(int array[], int len)
1040 for (i = j = 1; i < len; i++)
1041 if (!is_in_array(array[i], array, j))
1042 array[j++] = array[i];
1044 if (array[0] > array[1]) {
1045 i = array[0]; array[0] = array[1]; array[1] = i;
1050 static void patch_u(vertex_struct *vertex_ptrs, int count, int old, int new)
1053 for (i = 0; i < count; i++)
1054 if (vertex_ptrs[i].u == old)
1055 vertex_ptrs[i].u = new;
1058 static void patch_v(vertex_struct *vertex_ptrs, int count, int old, int new)
1061 for (i = 0; i < count; i++)
1062 if (vertex_ptrs[i].v == old)
1063 vertex_ptrs[i].v = new;
1066 // this sometimes does more harm than good, like in PE2
1067 static void uv_hack(vertex_struct *vertex_ptrs, int vertex_count)
1071 for (i = 0; i < vertex_count; i++) {
1072 u[i] = vertex_ptrs[i].u;
1073 v[i] = vertex_ptrs[i].v;
1075 if (make_members_unique(u, vertex_count) == 2 && u[1] - u[0] >= 8) {
1076 if ((u[0] & 7) == 7) {
1077 patch_u(vertex_ptrs, vertex_count, u[0], u[0] + 1);
1078 //printf("u hack: %3u-%3u -> %3u-%3u\n", u[0], u[1], u[0]+1, u[1]);
1080 else if ((u[1] & 7) == 0 || u[1] - u[0] > 128) {
1081 patch_u(vertex_ptrs, vertex_count, u[1], u[1] - 1);
1082 //printf("u hack: %3u-%3u -> %3u-%3u\n", u[0], u[1], u[0], u[1]-1);
1085 if (make_members_unique(v, vertex_count) == 2 && ((v[0] - v[1]) & 7) == 0) {
1086 if ((v[0] & 7) == 7) {
1087 patch_v(vertex_ptrs, vertex_count, v[0], v[0] + 1);
1088 //printf("v hack: %3u-%3u -> %3u-%3u\n", v[0], v[1], v[0]+1, v[1]);
1090 else if ((v[1] & 7) == 0) {
1091 patch_v(vertex_ptrs, vertex_count, v[1], v[1] - 1);
1092 //printf("v hack: %3u-%3u -> %3u-%3u\n", v[0], v[1], v[0], v[1]-1);
1097 static void do_triangle_enhanced(psx_gpu_struct *psx_gpu,
1098 vertex_struct *vertexes, u32 current_command)
1100 vertex_struct *vertex_ptrs[3];
1102 if (!prepare_triangle(psx_gpu, vertexes, vertex_ptrs))
1105 if (!psx_gpu->hack_disable_main)
1106 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
1108 if (!check_enhanced_range(psx_gpu, vertex_ptrs[0]->x, vertex_ptrs[2]->x))
1111 if (!enhancement_enable(psx_gpu))
1114 shift_vertices3(vertex_ptrs);
1115 shift_triangle_area();
1116 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
1117 unshift_vertices3(vertex_ptrs);
1120 static void do_quad_enhanced(psx_gpu_struct *psx_gpu, vertex_struct *vertexes,
1121 u32 current_command)
1123 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1124 enhancement_disable();
1125 do_triangle_enhanced(psx_gpu, &vertexes[1], current_command);
1130 #define fill_vertex(i, x_, y_, u_, v_, rgb_) \
1131 vertexes[i].x = x_; \
1132 vertexes[i].y = y_; \
1133 vertexes[i].u = u_; \
1134 vertexes[i].v = v_; \
1135 vertexes[i].r = rgb_; \
1136 vertexes[i].g = (rgb_) >> 8; \
1137 vertexes[i].b = (rgb_) >> 16
1139 static void do_sprite_enhanced(psx_gpu_struct *psx_gpu, int x, int y,
1140 u32 u, u32 v, u32 w, u32 h, u32 cmd_rgb)
1142 vertex_struct *vertex_ptrs[3];
1143 u32 flags = (cmd_rgb >> 24);
1144 u32 color = cmd_rgb & 0xffffff;
1145 u32 render_state_base_saved = psx_gpu->render_state_base;
1150 (RENDER_FLAGS_MODULATE_TEXELS | RENDER_FLAGS_BLEND |
1151 RENDER_FLAGS_TEXTURE_MAP);
1153 set_triangle_color(psx_gpu, color);
1154 if(color == 0x808080)
1155 flags |= RENDER_FLAGS_MODULATE_TEXELS;
1157 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
1158 enhancement_enable();
1165 if (u1 < u) u1 = 0xff;
1166 if (v1 < v) v1 = 0xff;
1171 fill_vertex(0, x, y, u, v, color);
1172 fill_vertex(1, x, y1, u, v1, color);
1173 fill_vertex(2, x1, y, u1, v, color);
1174 if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs)) {
1175 shift_vertices3(vertex_ptrs);
1176 shift_triangle_area();
1177 render_triangle_p(psx_gpu, vertex_ptrs, flags);
1183 fill_vertex(0, x1, y, u1, v, color);
1184 fill_vertex(1, x, y1, u, v1, color);
1185 fill_vertex(2, x1, y1, u1, v1, color);
1186 if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs)) {
1187 shift_vertices3(vertex_ptrs);
1188 shift_triangle_area();
1189 render_triangle_p(psx_gpu, vertex_ptrs, flags);
1192 psx_gpu->render_state_base = render_state_base_saved;
1195 static void do_sprite_enhanced(psx_gpu_struct *psx_gpu, int x, int y,
1196 u32 u, u32 v, u32 w, u32 h, u32 cmd_rgb)
1198 u32 flags = (cmd_rgb >> 24);
1199 u32 color = cmd_rgb & 0xffffff;
1201 render_sprite_4x(psx_gpu, x, y, u, v, w, h, flags, color);
1205 u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size,
1206 s32 *cpu_cycles_sum_out, s32 *cpu_cycles_last, u32 *last_command)
1208 vertex_struct vertexes[4] __attribute__((aligned(16))) = {};
1209 u32 current_command = 0, command_length;
1210 u32 cpu_cycles_sum = 0, cpu_cycles = *cpu_cycles_last;
1212 u32 *list_start = list;
1213 u32 *list_end = list + (size / 4);
1215 psx_gpu->saved_viewport_start_x = psx_gpu->viewport_start_x;
1216 psx_gpu->saved_viewport_start_y = psx_gpu->viewport_start_y;
1217 psx_gpu->saved_viewport_end_x = psx_gpu->viewport_end_x;
1218 psx_gpu->saved_viewport_end_y = psx_gpu->viewport_end_y;
1219 select_enhancement_buf(psx_gpu);
1221 for(; list < list_end; list += 1 + command_length)
1223 s16 *list_s16 = (void *)list;
1224 current_command = *list >> 24;
1225 command_length = command_lengths[current_command];
1226 if (list + 1 + command_length > list_end) {
1227 current_command = (u32)-1;
1231 enhancement_disable();
1233 switch(current_command)
1240 u32 x = list_s16[2] & 0x3FF;
1241 u32 y = list_s16[3] & 0x1FF;
1242 u32 width = list_s16[4] & 0x3FF;
1243 u32 height = list_s16[5] & 0x1FF;
1244 u32 color = list[0] & 0xFFFFFF;
1248 width = ((width + 0xF) & ~0xF);
1249 gput_sum(cpu_cycles_sum, cpu_cycles, gput_fill(width, height));
1250 if (width == 0 || height == 0)
1253 do_fill(psx_gpu, x, y, width, height, color);
1255 i1 = select_enhancement_buf_index(psx_gpu, x, y);
1256 i2 = select_enhancement_buf_index(psx_gpu, x + width - 1, y + height - 1);
1257 if (i1 < 0 || i1 != i2) {
1258 sync_enhancement_buffers(x, y, width, height);
1262 psx_gpu->vram_out_ptr = select_enhancement_buf_by_index(psx_gpu, i1);
1267 render_block_fill_enh(psx_gpu, color, x, y, width, height);
1273 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1275 get_vertex_data_xy(0, 2);
1276 get_vertex_data_xy(1, 4);
1277 get_vertex_data_xy(2, 6);
1279 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1280 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base());
1286 set_clut(psx_gpu, list_s16[5]);
1287 set_texture(psx_gpu, list_s16[9]);
1288 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1290 get_vertex_data_xy_uv(0, 2);
1291 get_vertex_data_xy_uv(1, 6);
1292 get_vertex_data_xy_uv(2, 10);
1294 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1295 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_t());
1301 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1303 get_vertex_data_xy(0, 2);
1304 get_vertex_data_xy(1, 4);
1305 get_vertex_data_xy(2, 6);
1306 get_vertex_data_xy(3, 8);
1308 do_quad_enhanced(psx_gpu, vertexes, current_command);
1309 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base());
1315 set_clut(psx_gpu, list_s16[5]);
1316 set_texture(psx_gpu, list_s16[9]);
1317 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1319 get_vertex_data_xy_uv(0, 2);
1320 get_vertex_data_xy_uv(1, 6);
1321 get_vertex_data_xy_uv(2, 10);
1322 get_vertex_data_xy_uv(3, 14);
1324 if (psx_gpu->hack_texture_adj)
1325 uv_hack(vertexes, 4);
1326 do_quad_enhanced(psx_gpu, vertexes, current_command);
1327 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_t());
1333 get_vertex_data_xy_rgb(0, 0);
1334 get_vertex_data_xy_rgb(1, 4);
1335 get_vertex_data_xy_rgb(2, 8);
1337 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1338 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_g());
1344 set_clut(psx_gpu, list_s16[5]);
1345 set_texture(psx_gpu, list_s16[11]);
1347 get_vertex_data_xy_uv_rgb(0, 0);
1348 get_vertex_data_xy_uv_rgb(1, 6);
1349 get_vertex_data_xy_uv_rgb(2, 12);
1351 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1352 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_gt());
1358 get_vertex_data_xy_rgb(0, 0);
1359 get_vertex_data_xy_rgb(1, 4);
1360 get_vertex_data_xy_rgb(2, 8);
1361 get_vertex_data_xy_rgb(3, 12);
1363 do_quad_enhanced(psx_gpu, vertexes, current_command);
1364 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_g());
1370 set_clut(psx_gpu, list_s16[5]);
1371 set_texture(psx_gpu, list_s16[11]);
1373 get_vertex_data_xy_uv_rgb(0, 0);
1374 get_vertex_data_xy_uv_rgb(1, 6);
1375 get_vertex_data_xy_uv_rgb(2, 12);
1376 get_vertex_data_xy_uv_rgb(3, 18);
1378 if (psx_gpu->hack_texture_adj)
1379 uv_hack(vertexes, 4);
1380 do_quad_enhanced(psx_gpu, vertexes, current_command);
1381 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_gt());
1387 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
1388 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
1389 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
1390 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
1392 render_line(psx_gpu, vertexes, current_command, list[0], 0);
1393 if (enhancement_enable(psx_gpu))
1394 render_line(psx_gpu, vertexes, current_command, list[0], 1);
1395 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
1401 u32 num_vertexes = 1;
1402 u32 *list_position = &(list[2]);
1405 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1406 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1408 xy = *list_position;
1411 vertexes[0] = vertexes[1];
1413 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1414 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1416 enhancement_disable();
1417 render_line(psx_gpu, vertexes, current_command, list[0], 0);
1418 if (enhancement_enable(psx_gpu))
1419 render_line(psx_gpu, vertexes, current_command, list[0], 1);
1420 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
1425 if(list_position >= list_end)
1427 current_command = (u32)-1;
1431 xy = *list_position;
1432 if((xy & 0xF000F000) == 0x50005000)
1436 command_length += (num_vertexes - 2);
1442 vertexes[0].r = list[0] & 0xFF;
1443 vertexes[0].g = (list[0] >> 8) & 0xFF;
1444 vertexes[0].b = (list[0] >> 16) & 0xFF;
1445 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
1446 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
1448 vertexes[1].r = list[2] & 0xFF;
1449 vertexes[1].g = (list[2] >> 8) & 0xFF;
1450 vertexes[1].b = (list[2] >> 16) & 0xFF;
1451 vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
1452 vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
1454 render_line(psx_gpu, vertexes, current_command, 0, 0);
1455 if (enhancement_enable(psx_gpu))
1456 render_line(psx_gpu, vertexes, current_command, 0, 1);
1457 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
1463 u32 num_vertexes = 1;
1464 u32 *list_position = &(list[2]);
1465 u32 color = list[0];
1468 vertexes[1].r = color & 0xFF;
1469 vertexes[1].g = (color >> 8) & 0xFF;
1470 vertexes[1].b = (color >> 16) & 0xFF;
1471 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1472 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1474 color = list_position[0];
1477 xy = list_position[1];
1479 vertexes[0] = vertexes[1];
1481 vertexes[1].r = color & 0xFF;
1482 vertexes[1].g = (color >> 8) & 0xFF;
1483 vertexes[1].b = (color >> 16) & 0xFF;
1484 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1485 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1487 enhancement_disable();
1488 render_line(psx_gpu, vertexes, current_command, 0, 0);
1489 if (enhancement_enable(psx_gpu))
1490 render_line(psx_gpu, vertexes, current_command, 0, 1);
1491 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
1496 if(list_position >= list_end)
1498 current_command = (u32)-1;
1502 color = list_position[0];
1503 if((color & 0xF000F000) == 0x50005000)
1507 command_length += ((num_vertexes - 2) * 2);
1513 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1514 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1515 s32 width = list_s16[4] & 0x3FF;
1516 s32 height = list_s16[5] & 0x1FF;
1518 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
1519 current_command, list[0]);
1520 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1522 if (check_enhanced_range(psx_gpu, x, x + width)) {
1523 width = list_s16[4] & 0x3FF;
1524 height = list_s16[5] & 0x1FF;
1525 do_sprite_enhanced(psx_gpu, x, y, 0, 0, width, height, list[0]);
1532 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1533 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1535 u8 v = list_s16[4] >> 8;
1536 s32 width = list_s16[6] & 0x3FF;
1537 s32 height = list_s16[7] & 0x1FF;
1539 set_clut(psx_gpu, list_s16[5]);
1541 render_sprite(psx_gpu, x, y, u, v,
1542 &width, &height, current_command, list[0]);
1543 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1545 if (check_enhanced_range(psx_gpu, x, x + width)) {
1546 width = list_s16[6] & 0x3FF;
1547 height = list_s16[7] & 0x1FF;
1548 do_sprite_enhanced(psx_gpu, x, y, u, v, width, height, list[0]);
1555 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1556 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1557 s32 width = 1, height = 1;
1559 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
1560 current_command, list[0]);
1561 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(1, 1));
1563 if (check_enhanced_range(psx_gpu, x, x + 1))
1564 do_sprite_enhanced(psx_gpu, x, y, 0, 0, 1, 1, list[0]);
1570 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1571 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1572 s32 width = 8, height = 8;
1574 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
1575 current_command, list[0]);
1576 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1578 if (check_enhanced_range(psx_gpu, x, x + 8))
1579 do_sprite_enhanced(psx_gpu, x, y, 0, 0, 8, 8, list[0]);
1585 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1586 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1588 u8 v = list_s16[4] >> 8;
1589 s32 width = 8, height = 8;
1591 set_clut(psx_gpu, list_s16[5]);
1593 render_sprite(psx_gpu, x, y, u, v,
1594 &width, &height, current_command, list[0]);
1595 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1597 if (check_enhanced_range(psx_gpu, x, x + 8))
1598 do_sprite_enhanced(psx_gpu, x, y, u, v, 8, 8, list[0]);
1604 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1605 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1606 s32 width = 16, height = 16;
1608 render_sprite(psx_gpu, x, y, 0, 0, &width, &height,
1609 current_command, list[0]);
1610 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1612 if (check_enhanced_range(psx_gpu, x, x + 16))
1613 do_sprite_enhanced(psx_gpu, x, y, 0, 0, 16, 16, list[0]);
1619 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1620 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1622 u8 v = list_s16[4] >> 8;
1623 s32 width = 16, height = 16;
1625 set_clut(psx_gpu, list_s16[5]);
1627 render_sprite(psx_gpu, x, y, u, v,
1628 &width, &height, current_command, list[0]);
1629 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(width, height));
1631 if (check_enhanced_range(psx_gpu, x, x + 16))
1632 do_sprite_enhanced(psx_gpu, x, y, u, v, 16, 16, list[0]);
1636 case 0x80 ... 0x9F: // vid -> vid
1637 case 0xA0 ... 0xBF: // sys -> vid
1638 case 0xC0 ... 0xDF: // vid -> sys
1642 set_texture(psx_gpu, list[0]);
1644 if(list[0] & (1 << 9))
1645 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
1647 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
1649 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
1656 u32 texture_window_settings = list[0];
1657 u32 tmp, x, y, w, h;
1659 if(texture_window_settings != psx_gpu->texture_window_settings)
1661 tmp = (texture_window_settings & 0x1F) | 0x20;
1662 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
1664 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
1665 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
1667 tmp = 32 - (w >> 3);
1668 x = ((texture_window_settings >> 10) & tmp) << 3;
1670 tmp = 32 - (h >> 3);
1671 y = ((texture_window_settings >> 15) & tmp) << 3;
1673 flush_render_block_buffer(psx_gpu);
1675 psx_gpu->texture_window_settings = texture_window_settings;
1676 psx_gpu->texture_window_x = x;
1677 psx_gpu->texture_window_y = y;
1678 psx_gpu->texture_mask_width = w - 1;
1679 psx_gpu->texture_mask_height = h - 1;
1681 update_texture_ptr(psx_gpu);
1689 s16 viewport_start_x = list[0] & 0x3FF;
1690 s16 viewport_start_y = (list[0] >> 10) & 0x1FF;
1692 if(viewport_start_x == psx_gpu->viewport_start_x &&
1693 viewport_start_y == psx_gpu->viewport_start_y)
1697 psx_gpu->viewport_start_x = viewport_start_x;
1698 psx_gpu->viewport_start_y = viewport_start_y;
1699 psx_gpu->saved_viewport_start_x = viewport_start_x;
1700 psx_gpu->saved_viewport_start_y = viewport_start_y;
1702 select_enhancement_buf(psx_gpu);
1704 #ifdef TEXTURE_CACHE_4BPP
1705 psx_gpu->viewport_mask =
1706 texture_region_mask(psx_gpu->viewport_start_x,
1707 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
1708 psx_gpu->viewport_end_y);
1716 s16 viewport_end_x = list[0] & 0x3FF;
1717 s16 viewport_end_y = (list[0] >> 10) & 0x1FF;
1719 if(viewport_end_x == psx_gpu->viewport_end_x &&
1720 viewport_end_y == psx_gpu->viewport_end_y)
1725 psx_gpu->viewport_end_x = viewport_end_x;
1726 psx_gpu->viewport_end_y = viewport_end_y;
1727 psx_gpu->saved_viewport_end_x = viewport_end_x;
1728 psx_gpu->saved_viewport_end_y = viewport_end_y;
1730 select_enhancement_buf(psx_gpu);
1732 if (!psx_gpu->enhancement_current_buf_ptr)
1733 log_anomaly("vp %3d,%3d %3d,%d - no buf\n",
1734 psx_gpu->viewport_start_x, psx_gpu->viewport_start_y,
1735 viewport_end_x, viewport_end_y);
1737 #ifdef TEXTURE_CACHE_4BPP
1738 psx_gpu->viewport_mask =
1739 texture_region_mask(psx_gpu->viewport_start_x,
1740 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
1741 psx_gpu->viewport_end_y);
1749 s32 offset_x = list[0] << 21;
1750 s32 offset_y = list[0] << 10;
1751 psx_gpu->offset_x = offset_x >> 21;
1752 psx_gpu->offset_y = offset_y >> 21;
1760 u32 mask_settings = list[0];
1761 u16 mask_msb = mask_settings << 15;
1764 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
1766 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
1768 if(mask_msb != psx_gpu->mask_msb)
1770 flush_render_block_buffer(psx_gpu);
1771 psx_gpu->mask_msb = mask_msb;
1783 enhancement_disable();
1786 *cpu_cycles_sum_out += cpu_cycles_sum;
1787 *cpu_cycles_last = cpu_cycles;
1788 *last_command = current_command;
1789 return list - list_start;
1794 // vim:ts=2:shiftwidth=2:expandtab