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.
19 #ifndef command_lengths
20 const u8 command_lengths[256] =
22 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00
23 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10
24 3, 3, 3, 3, 6, 6, 6, 6, 4, 4, 4, 4, 8, 8, 8, 8, // 20
25 5, 5, 5, 5, 8, 8, 8, 8, 7, 7, 7, 7, 11, 11, 11, 11, // 30
26 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 40
27 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, // 50
28 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, // 60
29 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, // 70
30 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90
32 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // a0
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // b0
34 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // c0
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // d0
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // e0
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // f0
41 void update_texture_ptr(psx_gpu_struct *psx_gpu)
46 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;
78 case TEXTURE_MODE_16BPP:
79 texture_base = (u8 *)(psx_gpu->vram_ptr);
80 texture_base += (psx_gpu->current_texture_page & 0xF) * 128;
81 texture_base += ((psx_gpu->current_texture_page >> 4) * 256) * 2048;
83 texture_ptr = texture_base;
84 texture_ptr += psx_gpu->texture_window_x * 2;
85 texture_ptr += (psx_gpu->texture_window_y) * 2048;
89 psx_gpu->texture_page_base = texture_base;
90 psx_gpu->texture_page_ptr = texture_ptr;
93 void set_texture(psx_gpu_struct *psx_gpu, u32 texture_settings)
95 texture_settings &= 0x1FF;
96 if(psx_gpu->texture_settings != texture_settings)
98 u32 new_texture_page = texture_settings & 0x1F;
99 u32 texture_mode = (texture_settings >> 7) & 0x3;
100 u32 render_state_base = psx_gpu->render_state_base;
102 flush_render_block_buffer(psx_gpu);
104 render_state_base &= ~(0xF << 6);
105 render_state_base |= ((texture_settings >> 5) & 0xF) << 6;
107 psx_gpu->render_state_base = render_state_base;
109 psx_gpu->current_texture_mask = 0x1 << new_texture_page;
111 if(texture_mode == TEXTURE_MODE_8BPP)
113 // In 8bpp mode 256x256 takes up two pages. If it's on the right edge it
114 // wraps back around to the left edge.
115 u32 adjacent_texture_page = ((texture_settings + 1) & 0xF) | (texture_settings & 0x10);
116 psx_gpu->current_texture_mask |= 0x1 << adjacent_texture_page;
118 if((psx_gpu->last_8bpp_texture_page ^ new_texture_page) & 0x1)
120 u32 dirty_textures_8bpp_alternate_mask =
121 psx_gpu->dirty_textures_8bpp_alternate_mask;
122 psx_gpu->dirty_textures_8bpp_alternate_mask =
123 psx_gpu->dirty_textures_8bpp_mask;
124 psx_gpu->dirty_textures_8bpp_mask = dirty_textures_8bpp_alternate_mask;
127 psx_gpu->last_8bpp_texture_page = new_texture_page;
130 psx_gpu->current_texture_page = new_texture_page;
131 psx_gpu->texture_settings = texture_settings;
133 update_texture_ptr(psx_gpu);
137 void set_clut(psx_gpu_struct *psx_gpu, u32 clut_settings)
139 if(psx_gpu->clut_settings != clut_settings)
141 flush_render_block_buffer(psx_gpu);
142 psx_gpu->clut_settings = clut_settings;
143 psx_gpu->clut_ptr = psx_gpu->vram_ptr + ((clut_settings & 0x7FFF) * 16);
147 void set_triangle_color(psx_gpu_struct *psx_gpu, u32 triangle_color)
149 if(psx_gpu->triangle_color != triangle_color)
151 flush_render_block_buffer(psx_gpu);
152 psx_gpu->triangle_color = triangle_color;
156 static void do_fill(psx_gpu_struct *psx_gpu, u32 x, u32 y,
157 u32 width, u32 height, u32 color)
160 width = ((width + 0xF) & ~0xF);
162 flush_render_block_buffer(psx_gpu);
164 if(unlikely((x + width) > 1024))
166 u32 width_a = 1024 - x;
167 u32 width_b = width - width_a;
169 if(unlikely((y + height) > 512))
171 u32 height_a = 512 - y;
172 u32 height_b = height - height_a;
174 render_block_fill(psx_gpu, color, x, y, width_a, height_a);
175 render_block_fill(psx_gpu, color, 0, y, width_b, height_a);
176 render_block_fill(psx_gpu, color, x, 0, width_a, height_b);
177 render_block_fill(psx_gpu, color, 0, 0, width_b, height_b);
181 render_block_fill(psx_gpu, color, x, y, width_a, height);
182 render_block_fill(psx_gpu, color, 0, y, width_b, height);
187 if(unlikely((y + height) > 512))
189 u32 height_a = 512 - y;
190 u32 height_b = height - height_a;
192 render_block_fill(psx_gpu, color, x, y, width, height_a);
193 render_block_fill(psx_gpu, color, x, 0, width, height_b);
197 render_block_fill(psx_gpu, color, x, y, width, height);
202 #define sign_extend_12bit(value) \
203 (((s32)((value) << 20)) >> 20) \
205 #define sign_extend_11bit(value) \
206 (((s32)((value) << 21)) >> 21) \
208 #define sign_extend_10bit(value) \
209 (((s32)((value) << 22)) >> 22) \
212 #define get_vertex_data_xy(vertex_number, offset16) \
213 vertexes[vertex_number].x = \
214 sign_extend_12bit(list_s16[offset16]) + psx_gpu->offset_x; \
215 vertexes[vertex_number].y = \
216 sign_extend_12bit(list_s16[(offset16) + 1]) + psx_gpu->offset_y; \
218 #define get_vertex_data_uv(vertex_number, offset16) \
219 vertexes[vertex_number].u = list_s16[offset16] & 0xFF; \
220 vertexes[vertex_number].v = (list_s16[offset16] >> 8) & 0xFF \
222 #define get_vertex_data_rgb(vertex_number, offset32) \
223 vertexes[vertex_number].r = list[offset32] & 0xFF; \
224 vertexes[vertex_number].g = (list[offset32] >> 8) & 0xFF; \
225 vertexes[vertex_number].b = (list[offset32] >> 16) & 0xFF \
227 #define get_vertex_data_xy_uv(vertex_number, offset16) \
228 get_vertex_data_xy(vertex_number, offset16); \
229 get_vertex_data_uv(vertex_number, (offset16) + 2) \
231 #define get_vertex_data_xy_rgb(vertex_number, offset16) \
232 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
233 get_vertex_data_xy(vertex_number, (offset16) + 2); \
235 #define get_vertex_data_xy_uv_rgb(vertex_number, offset16) \
236 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
237 get_vertex_data_xy(vertex_number, (offset16) + 2); \
238 get_vertex_data_uv(vertex_number, (offset16) + 4); \
240 #define set_vertex_color_constant(vertex_number, color) \
241 vertexes[vertex_number].r = color & 0xFF; \
242 vertexes[vertex_number].g = (color >> 8) & 0xFF; \
243 vertexes[vertex_number].b = (color >> 16) & 0xFF \
245 #define get_vertex_data_xy_rgb_constant(vertex_number, offset16, color) \
246 get_vertex_data_xy(vertex_number, offset16); \
247 set_vertex_color_constant(vertex_number, color) \
253 vertex_struct vertexes[4] __attribute__((aligned(32)));
255 u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_command)
257 u32 current_command = 0, command_length;
259 u32 *list_start = list;
260 u32 *list_end = list + (size / 4);
262 for(; list < list_end; list += 1 + command_length)
264 s16 *list_s16 = (void *)list;
265 current_command = *list >> 24;
266 command_length = command_lengths[current_command];
267 if (list + 1 + command_length > list_end) {
268 current_command = (u32)-1;
272 switch(current_command)
279 u32 x = list_s16[2] & 0x3FF;
280 u32 y = list_s16[3] & 0x1FF;
281 u32 width = list_s16[4] & 0x3FF;
282 u32 height = list_s16[5] & 0x1FF;
283 u32 color = list[0] & 0xFFFFFF;
285 do_fill(psx_gpu, x, y, width, height, color);
291 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
293 get_vertex_data_xy(0, 2);
294 get_vertex_data_xy(1, 4);
295 get_vertex_data_xy(2, 6);
297 render_triangle(psx_gpu, vertexes, current_command);
303 set_clut(psx_gpu, list_s16[5]);
304 set_texture(psx_gpu, list_s16[9]);
305 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
307 get_vertex_data_xy_uv(0, 2);
308 get_vertex_data_xy_uv(1, 6);
309 get_vertex_data_xy_uv(2, 10);
311 render_triangle(psx_gpu, vertexes, current_command);
317 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
319 get_vertex_data_xy(0, 2);
320 get_vertex_data_xy(1, 4);
321 get_vertex_data_xy(2, 6);
322 get_vertex_data_xy(3, 8);
324 render_triangle(psx_gpu, vertexes, current_command);
325 render_triangle(psx_gpu, &(vertexes[1]), current_command);
331 set_clut(psx_gpu, list_s16[5]);
332 set_texture(psx_gpu, list_s16[9]);
333 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
335 get_vertex_data_xy_uv(0, 2);
336 get_vertex_data_xy_uv(1, 6);
337 get_vertex_data_xy_uv(2, 10);
338 get_vertex_data_xy_uv(3, 14);
340 render_triangle(psx_gpu, vertexes, current_command);
341 render_triangle(psx_gpu, &(vertexes[1]), current_command);
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);
360 set_clut(psx_gpu, list_s16[5]);
361 set_texture(psx_gpu, list_s16[11]);
363 get_vertex_data_xy_uv_rgb(0, 0);
364 get_vertex_data_xy_uv_rgb(1, 6);
365 get_vertex_data_xy_uv_rgb(2, 12);
367 render_triangle(psx_gpu, vertexes, current_command);
376 get_vertex_data_xy_rgb(0, 0);
377 get_vertex_data_xy_rgb(1, 4);
378 get_vertex_data_xy_rgb(2, 8);
379 get_vertex_data_xy_rgb(3, 12);
381 render_triangle(psx_gpu, vertexes, current_command);
382 render_triangle(psx_gpu, &(vertexes[1]), current_command);
391 set_clut(psx_gpu, list_s16[5]);
392 set_texture(psx_gpu, list_s16[11]);
394 get_vertex_data_xy_uv_rgb(0, 0);
395 get_vertex_data_xy_uv_rgb(1, 6);
396 get_vertex_data_xy_uv_rgb(2, 12);
397 get_vertex_data_xy_uv_rgb(3, 18);
399 render_triangle(psx_gpu, vertexes, current_command);
400 render_triangle(psx_gpu, &(vertexes[1]), current_command);
406 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
407 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
408 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
409 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
411 render_line(psx_gpu, vertexes, current_command, list[0], 0);
417 u32 num_vertexes = 1;
418 u32 *list_position = &(list[2]);
421 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
422 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
427 vertexes[0] = vertexes[1];
429 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
430 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
432 render_line(psx_gpu, vertexes, current_command, list[0], 0);
437 if(list_position >= list_end)
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);
469 u32 num_vertexes = 1;
470 u32 *list_position = &(list[2]);
474 vertexes[1].r = color & 0xFF;
475 vertexes[1].g = (color >> 8) & 0xFF;
476 vertexes[1].b = (color >> 16) & 0xFF;
477 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
478 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
480 color = list_position[0];
483 xy = list_position[1];
485 vertexes[0] = vertexes[1];
487 vertexes[1].r = color & 0xFF;
488 vertexes[1].g = (color >> 8) & 0xFF;
489 vertexes[1].b = (color >> 16) & 0xFF;
490 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
491 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
493 render_line(psx_gpu, vertexes, current_command, 0, 0);
498 if(list_position >= list_end)
501 color = list_position[0];
502 if((color & 0xF000F000) == 0x50005000)
506 command_length += ((num_vertexes - 2) * 2);
512 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
513 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
514 u32 width = list_s16[4] & 0x3FF;
515 u32 height = list_s16[5] & 0x1FF;
517 render_sprite(psx_gpu, x, y, 0, 0, width, height, current_command, list[0]);
523 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
524 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
525 u32 uv = list_s16[4];
526 u32 width = list_s16[6] & 0x3FF;
527 u32 height = list_s16[7] & 0x1FF;
529 set_clut(psx_gpu, list_s16[5]);
531 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, width, height,
532 current_command, list[0]);
541 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
542 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
544 render_sprite(psx_gpu, x, y, 0, 0, 1, 1, current_command, list[0]);
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);
556 render_sprite(psx_gpu, x, y, 0, 0, 8, 8, current_command, list[0]);
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 u32 uv = list_s16[4];
569 set_clut(psx_gpu, list_s16[5]);
571 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 8, 8,
572 current_command, list[0]);
581 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
582 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
584 render_sprite(psx_gpu, x, y, 0, 0, 16, 16, current_command, list[0]);
593 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
594 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
595 u32 uv = list_s16[4];
597 set_clut(psx_gpu, list_s16[5]);
599 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 16, 16,
600 current_command, list[0]);
604 case 0x80: // vid -> vid
606 u32 sx = list_s16[2] & 0x3FF;
607 u32 sy = list_s16[3] & 0x1FF;
608 u32 dx = list_s16[4] & 0x3FF;
609 u32 dy = list_s16[5] & 0x1FF;
610 u32 w = ((list_s16[6] - 1) & 0x3FF) + 1;
611 u32 h = ((list_s16[7] - 1) & 0x1FF) + 1;
613 if (sx == dx && sy == dy && psx_gpu->mask_msb == 0)
616 render_block_move(psx_gpu, sx, sy, dx, dy, w, h);
621 case 0xA0: // sys -> vid
622 case 0xC0: // vid -> sys
625 case 0xA0: // sys -> vid
627 u32 load_x = list_s16[2] & 0x3FF;
628 u32 load_y = list_s16[3] & 0x1FF;
629 u32 load_width = list_s16[4] & 0x3FF;
630 u32 load_height = list_s16[5] & 0x1FF;
631 u32 load_size = load_width * load_height;
633 command_length += load_size / 2;
638 render_block_copy(psx_gpu, (u16 *)&(list_s16[6]), load_x, load_y,
639 load_width, load_height, load_width);
643 case 0xC0: // vid -> sys
648 set_texture(psx_gpu, list[0]);
650 if(list[0] & (1 << 9))
651 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
653 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
655 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
662 u32 texture_window_settings = list[0];
665 if(texture_window_settings != psx_gpu->texture_window_settings)
667 tmp = (texture_window_settings & 0x1F) | 0x20;
668 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
670 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
671 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
674 x = ((texture_window_settings >> 10) & tmp) << 3;
677 y = ((texture_window_settings >> 15) & tmp) << 3;
679 flush_render_block_buffer(psx_gpu);
681 psx_gpu->texture_window_settings = texture_window_settings;
682 psx_gpu->texture_window_x = x;
683 psx_gpu->texture_window_y = y;
684 psx_gpu->texture_mask_width = w - 1;
685 psx_gpu->texture_mask_height = h - 1;
687 update_texture_ptr(psx_gpu);
694 psx_gpu->viewport_start_x = list[0] & 0x3FF;
695 psx_gpu->viewport_start_y = (list[0] >> 10) & 0x1FF;
697 #ifdef TEXTURE_CACHE_4BPP
698 psx_gpu->viewport_mask =
699 texture_region_mask(psx_gpu->viewport_start_x,
700 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
701 psx_gpu->viewport_end_y);
707 psx_gpu->viewport_end_x = list[0] & 0x3FF;
708 psx_gpu->viewport_end_y = (list[0] >> 10) & 0x1FF;
710 #ifdef TEXTURE_CACHE_4BPP
711 psx_gpu->viewport_mask =
712 texture_region_mask(psx_gpu->viewport_start_x,
713 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
714 psx_gpu->viewport_end_y);
721 s32 offset_x = list[0] << 21;
722 s32 offset_y = list[0] << 10;
723 psx_gpu->offset_x = offset_x >> 21;
724 psx_gpu->offset_y = offset_y >> 21;
732 u32 mask_settings = list[0];
733 u16 mask_msb = mask_settings << 15;
736 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
738 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
740 if(mask_msb != psx_gpu->mask_msb)
742 flush_render_block_buffer(psx_gpu);
743 psx_gpu->mask_msb = mask_msb;
758 if (last_command != NULL)
759 *last_command = current_command;
760 return list - list_start;
765 static void update_enhancement_buf_table(psx_gpu_struct *psx_gpu)
770 s = psx_gpu->enhancement_x_threshold;
771 for (x = 0; x < sizeof(psx_gpu->enhancement_buf_by_x16); x++)
773 if (x * 16 >= s - 15)
775 s += psx_gpu->enhancement_x_threshold;
778 psx_gpu->enhancement_buf_by_x16[x] = b;
782 #define select_enhancement_buf(psx_gpu) \
783 psx_gpu->enhancement_current_buf_ptr = \
784 select_enhancement_buf_ptr(psx_gpu, psx_gpu->saved_viewport_start_x)
786 #define enhancement_disable() { \
787 psx_gpu->vram_out_ptr = psx_gpu->vram_ptr; \
788 psx_gpu->viewport_start_x = psx_gpu->saved_viewport_start_x; \
789 psx_gpu->viewport_start_y = psx_gpu->saved_viewport_start_y; \
790 psx_gpu->viewport_end_x = psx_gpu->saved_viewport_end_x; \
791 psx_gpu->viewport_end_y = psx_gpu->saved_viewport_end_y; \
792 psx_gpu->uvrgb_phase = 0x8000; \
795 #define enhancement_enable() { \
796 psx_gpu->vram_out_ptr = psx_gpu->enhancement_current_buf_ptr; \
797 psx_gpu->viewport_start_x = psx_gpu->saved_viewport_start_x * 2; \
798 psx_gpu->viewport_start_y = psx_gpu->saved_viewport_start_y * 2; \
799 psx_gpu->viewport_end_x = psx_gpu->saved_viewport_end_x * 2 + 1; \
800 psx_gpu->viewport_end_y = psx_gpu->saved_viewport_end_y * 2 + 1; \
801 psx_gpu->uvrgb_phase = 0x1000; \
804 #define shift_vertices3(v) { \
813 #define unshift_vertices3(v) { \
822 #define shift_triangle_area() \
823 psx_gpu->triangle_area *= 4
825 extern void scale2x_tiles8(void *dst, const void *src, int w8, int h);
829 void scale2x_tiles8(void *dst, const void *src, int w8, int h) {}
832 static int disable_main_render;
834 static void do_triangle_enhanced(psx_gpu_struct *psx_gpu,
835 vertex_struct *vertexes, u32 current_command)
837 vertex_struct *vertex_ptrs[3];
839 if (!prepare_triangle(psx_gpu, vertexes, vertex_ptrs))
842 if (!disable_main_render)
843 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
845 enhancement_enable();
846 shift_vertices3(vertex_ptrs);
847 shift_triangle_area();
848 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
851 static void do_quad_enhanced(psx_gpu_struct *psx_gpu, vertex_struct *vertexes,
854 vertex_struct *vertex_ptrs[3];
856 if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs)) {
857 if (!disable_main_render)
858 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
860 enhancement_enable();
861 shift_vertices3(vertex_ptrs);
862 shift_triangle_area();
863 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
864 unshift_vertices3(vertex_ptrs);
866 enhancement_disable();
867 if (prepare_triangle(psx_gpu, &vertexes[1], vertex_ptrs)) {
868 if (!disable_main_render)
869 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
871 enhancement_enable();
872 shift_vertices3(vertex_ptrs);
873 shift_triangle_area();
874 render_triangle_p(psx_gpu, vertex_ptrs, current_command);
880 #define fill_vertex(i, x_, y_, u_, v_, rgb_) \
881 vertexes[i].x = x_; \
882 vertexes[i].y = y_; \
883 vertexes[i].u = u_; \
884 vertexes[i].v = v_; \
885 vertexes[i].r = rgb_; \
886 vertexes[i].g = (rgb_) >> 8; \
887 vertexes[i].b = (rgb_) >> 16
889 static void do_sprite_enhanced(psx_gpu_struct *psx_gpu, int x, int y,
890 u32 u, u32 v, u32 w, u32 h, u32 cmd_rgb)
892 vertex_struct *vertex_ptrs[3];
893 u32 flags = (cmd_rgb >> 24);
894 u32 color = cmd_rgb & 0xffffff;
895 u32 render_state_base_saved = psx_gpu->render_state_base;
900 (RENDER_FLAGS_MODULATE_TEXELS | RENDER_FLAGS_BLEND |
901 RENDER_FLAGS_TEXTURE_MAP);
903 set_triangle_color(psx_gpu, color);
904 if(color == 0x808080)
905 flags |= RENDER_FLAGS_MODULATE_TEXELS;
907 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
908 enhancement_enable();
915 if (u1 < u) u1 = 0xff;
916 if (v1 < v) v1 = 0xff;
921 fill_vertex(0, x, y, u, v, color);
922 fill_vertex(1, x, y1, u, v1, color);
923 fill_vertex(2, x1, y, u1, v, color);
924 if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs)) {
925 shift_vertices3(vertex_ptrs);
926 shift_triangle_area();
927 render_triangle_p(psx_gpu, vertex_ptrs, flags);
933 fill_vertex(0, x1, y, u1, v, color);
934 fill_vertex(1, x, y1, u, v1, color);
935 fill_vertex(2, x1, y1, u1, v1, color);
936 if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs)) {
937 shift_vertices3(vertex_ptrs);
938 shift_triangle_area();
939 render_triangle_p(psx_gpu, vertex_ptrs, flags);
942 psx_gpu->render_state_base = render_state_base_saved;
945 static void do_sprite_enhanced(psx_gpu_struct *psx_gpu, int x, int y,
946 u32 u, u32 v, u32 w, u32 h, u32 cmd_rgb)
948 u32 flags = (cmd_rgb >> 24);
949 u32 color = cmd_rgb & 0xffffff;
951 render_sprite_4x(psx_gpu, x, y, u, v, w, h, flags, color);
955 u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size,
958 u32 current_command = 0, command_length;
960 u32 *list_start = list;
961 u32 *list_end = list + (size / 4);
963 psx_gpu->saved_viewport_start_x = psx_gpu->viewport_start_x;
964 psx_gpu->saved_viewport_start_y = psx_gpu->viewport_start_y;
965 psx_gpu->saved_viewport_end_x = psx_gpu->viewport_end_x;
966 psx_gpu->saved_viewport_end_y = psx_gpu->viewport_end_y;
967 select_enhancement_buf(psx_gpu);
969 for(; list < list_end; list += 1 + command_length)
971 s16 *list_s16 = (void *)list;
972 current_command = *list >> 24;
973 command_length = command_lengths[current_command];
974 if (list + 1 + command_length > list_end) {
975 current_command = (u32)-1;
979 enhancement_disable();
981 switch(current_command)
988 u32 x = list_s16[2] & 0x3FF;
989 u32 y = list_s16[3] & 0x1FF;
990 u32 width = list_s16[4] & 0x3FF;
991 u32 height = list_s16[5] & 0x1FF;
992 u32 color = list[0] & 0xFFFFFF;
995 width = ((width + 0xF) & ~0xF);
997 do_fill(psx_gpu, x, y, width, height, color);
999 psx_gpu->vram_out_ptr = select_enhancement_buf_ptr(psx_gpu, x);
1004 render_block_fill_enh(psx_gpu, color, x, y, width, height);
1010 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1012 get_vertex_data_xy(0, 2);
1013 get_vertex_data_xy(1, 4);
1014 get_vertex_data_xy(2, 6);
1016 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1022 set_clut(psx_gpu, list_s16[5]);
1023 set_texture(psx_gpu, list_s16[9]);
1024 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1026 get_vertex_data_xy_uv(0, 2);
1027 get_vertex_data_xy_uv(1, 6);
1028 get_vertex_data_xy_uv(2, 10);
1030 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1036 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1038 get_vertex_data_xy(0, 2);
1039 get_vertex_data_xy(1, 4);
1040 get_vertex_data_xy(2, 6);
1041 get_vertex_data_xy(3, 8);
1043 do_quad_enhanced(psx_gpu, vertexes, current_command);
1049 set_clut(psx_gpu, list_s16[5]);
1050 set_texture(psx_gpu, list_s16[9]);
1051 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
1053 get_vertex_data_xy_uv(0, 2);
1054 get_vertex_data_xy_uv(1, 6);
1055 get_vertex_data_xy_uv(2, 10);
1056 get_vertex_data_xy_uv(3, 14);
1058 do_quad_enhanced(psx_gpu, vertexes, current_command);
1064 get_vertex_data_xy_rgb(0, 0);
1065 get_vertex_data_xy_rgb(1, 4);
1066 get_vertex_data_xy_rgb(2, 8);
1068 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1077 set_clut(psx_gpu, list_s16[5]);
1078 set_texture(psx_gpu, list_s16[11]);
1080 get_vertex_data_xy_uv_rgb(0, 0);
1081 get_vertex_data_xy_uv_rgb(1, 6);
1082 get_vertex_data_xy_uv_rgb(2, 12);
1084 do_triangle_enhanced(psx_gpu, vertexes, current_command);
1093 get_vertex_data_xy_rgb(0, 0);
1094 get_vertex_data_xy_rgb(1, 4);
1095 get_vertex_data_xy_rgb(2, 8);
1096 get_vertex_data_xy_rgb(3, 12);
1098 do_quad_enhanced(psx_gpu, vertexes, current_command);
1107 set_clut(psx_gpu, list_s16[5]);
1108 set_texture(psx_gpu, list_s16[11]);
1110 get_vertex_data_xy_uv_rgb(0, 0);
1111 get_vertex_data_xy_uv_rgb(1, 6);
1112 get_vertex_data_xy_uv_rgb(2, 12);
1113 get_vertex_data_xy_uv_rgb(3, 18);
1115 do_quad_enhanced(psx_gpu, vertexes, current_command);
1121 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
1122 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
1123 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
1124 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
1126 render_line(psx_gpu, vertexes, current_command, list[0], 0);
1127 enhancement_enable();
1128 render_line(psx_gpu, vertexes, current_command, list[0], 1);
1134 u32 num_vertexes = 1;
1135 u32 *list_position = &(list[2]);
1138 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1139 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1141 xy = *list_position;
1144 vertexes[0] = vertexes[1];
1146 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1147 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1149 enhancement_disable();
1150 render_line(psx_gpu, vertexes, current_command, list[0], 0);
1151 enhancement_enable();
1152 render_line(psx_gpu, vertexes, current_command, list[0], 1);
1157 if(list_position >= list_end)
1160 xy = *list_position;
1161 if((xy & 0xF000F000) == 0x50005000)
1165 command_length += (num_vertexes - 2);
1171 vertexes[0].r = list[0] & 0xFF;
1172 vertexes[0].g = (list[0] >> 8) & 0xFF;
1173 vertexes[0].b = (list[0] >> 16) & 0xFF;
1174 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
1175 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
1177 vertexes[1].r = list[2] & 0xFF;
1178 vertexes[1].g = (list[2] >> 8) & 0xFF;
1179 vertexes[1].b = (list[2] >> 16) & 0xFF;
1180 vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
1181 vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
1183 render_line(psx_gpu, vertexes, current_command, 0, 0);
1184 enhancement_enable();
1185 render_line(psx_gpu, vertexes, current_command, 0, 1);
1191 u32 num_vertexes = 1;
1192 u32 *list_position = &(list[2]);
1193 u32 color = list[0];
1196 vertexes[1].r = color & 0xFF;
1197 vertexes[1].g = (color >> 8) & 0xFF;
1198 vertexes[1].b = (color >> 16) & 0xFF;
1199 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1200 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1202 color = list_position[0];
1205 xy = list_position[1];
1207 vertexes[0] = vertexes[1];
1209 vertexes[1].r = color & 0xFF;
1210 vertexes[1].g = (color >> 8) & 0xFF;
1211 vertexes[1].b = (color >> 16) & 0xFF;
1212 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
1213 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
1215 enhancement_disable();
1216 render_line(psx_gpu, vertexes, current_command, 0, 0);
1217 enhancement_enable();
1218 render_line(psx_gpu, vertexes, current_command, 0, 1);
1223 if(list_position >= list_end)
1226 color = list_position[0];
1227 if((color & 0xF000F000) == 0x50005000)
1231 command_length += ((num_vertexes - 2) * 2);
1237 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1238 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1239 u32 width = list_s16[4] & 0x3FF;
1240 u32 height = list_s16[5] & 0x1FF;
1242 render_sprite(psx_gpu, x, y, 0, 0, width, height, current_command, list[0]);
1243 do_sprite_enhanced(psx_gpu, x, y, 0, 0, width, height, list[0]);
1249 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1250 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1252 u8 v = list_s16[4] >> 8;
1253 u32 width = list_s16[6] & 0x3FF;
1254 u32 height = list_s16[7] & 0x1FF;
1256 set_clut(psx_gpu, list_s16[5]);
1258 render_sprite(psx_gpu, x, y, u, v, width, height,
1259 current_command, list[0]);
1260 do_sprite_enhanced(psx_gpu, x, y, u, v, width, height, list[0]);
1269 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1270 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1272 render_sprite(psx_gpu, x, y, 0, 0, 1, 1, current_command, list[0]);
1273 do_sprite_enhanced(psx_gpu, x, y, 0, 0, 1, 1, list[0]);
1282 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1283 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1285 render_sprite(psx_gpu, x, y, 0, 0, 8, 8, current_command, list[0]);
1286 do_sprite_enhanced(psx_gpu, x, y, 0, 0, 8, 8, list[0]);
1295 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1296 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1298 u8 v = list_s16[4] >> 8;
1300 set_clut(psx_gpu, list_s16[5]);
1302 render_sprite(psx_gpu, x, y, u, v, 8, 8,
1303 current_command, list[0]);
1304 do_sprite_enhanced(psx_gpu, x, y, u, v, 8, 8, list[0]);
1313 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1314 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1316 render_sprite(psx_gpu, x, y, 0, 0, 16, 16, current_command, list[0]);
1317 do_sprite_enhanced(psx_gpu, x, y, 0, 0, 16, 16, list[0]);
1326 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
1327 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
1329 u8 v = list_s16[4] >> 8;
1331 set_clut(psx_gpu, list_s16[5]);
1333 render_sprite(psx_gpu, x, y, u, v, 16, 16, current_command, list[0]);
1334 do_sprite_enhanced(psx_gpu, x, y, u, v, 16, 16, list[0]);
1338 case 0x80: // vid -> vid
1340 u32 sx = list_s16[2] & 0x3FF;
1341 u32 sy = list_s16[3] & 0x1FF;
1342 u32 dx = list_s16[4] & 0x3FF;
1343 u32 dy = list_s16[5] & 0x1FF;
1344 u32 w = ((list_s16[6] - 1) & 0x3FF) + 1;
1345 u32 h = ((list_s16[7] - 1) & 0x1FF) + 1;
1348 if (sx == dx && sy == dy && psx_gpu->mask_msb == 0)
1351 render_block_move(psx_gpu, sx, sy, dx, dy, w, h);
1354 sx = sx & ~7; // FIXME?
1358 buf = select_enhancement_buf_ptr(psx_gpu, dx / 2);
1359 scale2x_tiles8(buf + dy * 1024 + dx,
1360 psx_gpu->vram_ptr + sy * 1024 + sx, w, h);
1364 case 0xA0: // sys -> vid
1365 case 0xC0: // vid -> sys
1369 set_texture(psx_gpu, list[0]);
1371 if(list[0] & (1 << 9))
1372 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
1374 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
1376 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
1383 u32 texture_window_settings = list[0];
1384 u32 tmp, x, y, w, h;
1386 if(texture_window_settings != psx_gpu->texture_window_settings)
1388 tmp = (texture_window_settings & 0x1F) | 0x20;
1389 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
1391 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
1392 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
1394 tmp = 32 - (w >> 3);
1395 x = ((texture_window_settings >> 10) & tmp) << 3;
1397 tmp = 32 - (h >> 3);
1398 y = ((texture_window_settings >> 15) & tmp) << 3;
1400 flush_render_block_buffer(psx_gpu);
1402 psx_gpu->texture_window_settings = texture_window_settings;
1403 psx_gpu->texture_window_x = x;
1404 psx_gpu->texture_window_y = y;
1405 psx_gpu->texture_mask_width = w - 1;
1406 psx_gpu->texture_mask_height = h - 1;
1408 update_texture_ptr(psx_gpu);
1417 psx_gpu->viewport_start_x = list[0] & 0x3FF;
1418 psx_gpu->viewport_start_y = (list[0] >> 10) & 0x1FF;
1419 psx_gpu->saved_viewport_start_x = psx_gpu->viewport_start_x;
1420 psx_gpu->saved_viewport_start_y = psx_gpu->viewport_start_y;
1422 d = psx_gpu->enhancement_x_threshold - psx_gpu->viewport_start_x;
1423 if(unlikely(0 < d && d <= 8))
1426 psx_gpu->enhancement_x_threshold = psx_gpu->viewport_start_x;
1427 update_enhancement_buf_table(psx_gpu);
1429 select_enhancement_buf(psx_gpu);
1431 #ifdef TEXTURE_CACHE_4BPP
1432 psx_gpu->viewport_mask =
1433 texture_region_mask(psx_gpu->viewport_start_x,
1434 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
1435 psx_gpu->viewport_end_y);
1442 psx_gpu->viewport_end_x = list[0] & 0x3FF;
1443 psx_gpu->viewport_end_y = (list[0] >> 10) & 0x1FF;
1444 psx_gpu->saved_viewport_end_x = psx_gpu->viewport_end_x;
1445 psx_gpu->saved_viewport_end_y = psx_gpu->viewport_end_y;
1447 #ifdef TEXTURE_CACHE_4BPP
1448 psx_gpu->viewport_mask =
1449 texture_region_mask(psx_gpu->viewport_start_x,
1450 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
1451 psx_gpu->viewport_end_y);
1458 s32 offset_x = list[0] << 21;
1459 s32 offset_y = list[0] << 10;
1460 psx_gpu->offset_x = offset_x >> 21;
1461 psx_gpu->offset_y = offset_y >> 21;
1469 u32 mask_settings = list[0];
1470 u16 mask_msb = mask_settings << 15;
1473 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
1475 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
1477 if(mask_msb != psx_gpu->mask_msb)
1479 flush_render_block_buffer(psx_gpu);
1480 psx_gpu->mask_msb = mask_msb;
1492 enhancement_disable();
1495 if (last_command != NULL)
1496 *last_command = current_command;
1497 return list - list_start;
1502 // vim:shiftwidth=2:expandtab