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 if(psx_gpu->texture_settings != texture_settings)
97 u32 new_texture_page = texture_settings & 0x1F;
98 u32 texture_mode = (texture_settings >> 7) & 0x3;
99 u32 render_state_base = psx_gpu->render_state_base;
101 flush_render_block_buffer(psx_gpu);
103 render_state_base &= ~(0xF << 6);
104 render_state_base |= ((texture_settings >> 5) & 0xF) << 6;
106 psx_gpu->render_state_base = render_state_base;
108 psx_gpu->current_texture_mask = 0x1 << new_texture_page;
110 if(texture_mode == TEXTURE_MODE_8BPP)
112 // In 8bpp mode 256x256 takes up two pages. If it's on the right edge it
113 // wraps back around to the left edge.
114 u32 adjacent_texture_page = ((texture_settings + 1) & 0xF) | (texture_settings & 0x10);
115 psx_gpu->current_texture_mask |= 0x1 << adjacent_texture_page;
117 if((psx_gpu->last_8bpp_texture_page ^ new_texture_page) & 0x1)
119 u32 dirty_textures_8bpp_alternate_mask =
120 psx_gpu->dirty_textures_8bpp_alternate_mask;
121 psx_gpu->dirty_textures_8bpp_alternate_mask =
122 psx_gpu->dirty_textures_8bpp_mask;
123 psx_gpu->dirty_textures_8bpp_mask = dirty_textures_8bpp_alternate_mask;
126 psx_gpu->last_8bpp_texture_page = new_texture_page;
129 psx_gpu->current_texture_page = new_texture_page;
130 psx_gpu->texture_settings = texture_settings;
132 update_texture_ptr(psx_gpu);
136 void set_clut(psx_gpu_struct *psx_gpu, u32 clut_settings)
138 if(psx_gpu->clut_settings != clut_settings)
140 flush_render_block_buffer(psx_gpu);
141 psx_gpu->clut_settings = clut_settings;
142 psx_gpu->clut_ptr = psx_gpu->vram_ptr + ((clut_settings & 0x7FFF) * 16);
146 void set_triangle_color(psx_gpu_struct *psx_gpu, u32 triangle_color)
148 if(psx_gpu->triangle_color != triangle_color)
150 flush_render_block_buffer(psx_gpu);
151 psx_gpu->triangle_color = triangle_color;
155 #define sign_extend_12bit(value) \
156 (((s32)((value) << 20)) >> 20) \
158 #define sign_extend_11bit(value) \
159 (((s32)((value) << 21)) >> 21) \
161 #define sign_extend_10bit(value) \
162 (((s32)((value) << 22)) >> 22) \
165 #define get_vertex_data_xy(vertex_number, offset16) \
166 vertexes[vertex_number].x = \
167 sign_extend_12bit(list_s16[offset16]) + psx_gpu->offset_x; \
168 vertexes[vertex_number].y = \
169 sign_extend_12bit(list_s16[(offset16) + 1]) + psx_gpu->offset_y; \
171 #define get_vertex_data_uv(vertex_number, offset16) \
172 vertexes[vertex_number].u = list_s16[offset16] & 0xFF; \
173 vertexes[vertex_number].v = (list_s16[offset16] >> 8) & 0xFF \
175 #define get_vertex_data_rgb(vertex_number, offset32) \
176 vertexes[vertex_number].r = list[offset32] & 0xFF; \
177 vertexes[vertex_number].g = (list[offset32] >> 8) & 0xFF; \
178 vertexes[vertex_number].b = (list[offset32] >> 16) & 0xFF \
180 #define get_vertex_data_xy_uv(vertex_number, offset16) \
181 get_vertex_data_xy(vertex_number, offset16); \
182 get_vertex_data_uv(vertex_number, (offset16) + 2) \
184 #define get_vertex_data_xy_rgb(vertex_number, offset16) \
185 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
186 get_vertex_data_xy(vertex_number, (offset16) + 2); \
188 #define get_vertex_data_xy_uv_rgb(vertex_number, offset16) \
189 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
190 get_vertex_data_xy(vertex_number, (offset16) + 2); \
191 get_vertex_data_uv(vertex_number, (offset16) + 4); \
193 #define set_vertex_color_constant(vertex_number, color) \
194 vertexes[vertex_number].r = color & 0xFF; \
195 vertexes[vertex_number].g = (color >> 8) & 0xFF; \
196 vertexes[vertex_number].b = (color >> 16) & 0xFF \
198 #define get_vertex_data_xy_rgb_constant(vertex_number, offset16, color) \
199 get_vertex_data_xy(vertex_number, offset16); \
200 set_vertex_color_constant(vertex_number, color) \
202 vertex_struct vertexes[4] __attribute__((aligned(32)));
204 void gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size)
206 u32 current_command, command_length;
208 u32 *list_end = list + (size / 4);
210 for(; list < list_end; list += 1 + command_length)
212 s16 *list_s16 = (void *)list;
213 current_command = *list >> 24;
214 command_length = command_lengths[current_command];
216 switch(current_command)
223 u32 x = list_s16[2] & 0x3FF;
224 u32 y = list_s16[3] & 0x1FF;
225 u32 width = list_s16[4] & 0x3FF;
226 u32 height = list_s16[5] & 0x1FF;
227 u32 color = list[0] & 0xFFFFFF;
230 width = ((width + 0xF) & ~0xF);
232 if((x + width) > 1024)
234 u32 width_a = 1024 - x;
235 u32 width_b = width - width_a;
237 if((y + height) > 512)
239 u32 height_a = 512 - y;
240 u32 height_b = height - height_a;
242 render_block_fill(psx_gpu, color, x, y, width_a, height_a);
243 render_block_fill(psx_gpu, color, 0, y, width_b, height_a);
244 render_block_fill(psx_gpu, color, x, 0, width_a, height_b);
245 render_block_fill(psx_gpu, color, 0, 0, width_b, height_b);
249 render_block_fill(psx_gpu, color, x, y, width_a, height);
250 render_block_fill(psx_gpu, color, 0, y, width_b, height);
255 if((y + height) > 512)
257 u32 height_a = 512 - y;
258 u32 height_b = height - height_a;
260 render_block_fill(psx_gpu, color, x, y, width, height_a);
261 render_block_fill(psx_gpu, color, x, 0, width, height_b);
265 render_block_fill(psx_gpu, color, x, y, width, height);
273 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
275 get_vertex_data_xy(0, 2);
276 get_vertex_data_xy(1, 4);
277 get_vertex_data_xy(2, 6);
279 render_triangle(psx_gpu, vertexes, current_command);
285 set_clut(psx_gpu, list_s16[5]);
286 set_texture(psx_gpu, list_s16[9]);
287 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
289 get_vertex_data_xy_uv(0, 2);
290 get_vertex_data_xy_uv(1, 6);
291 get_vertex_data_xy_uv(2, 10);
293 render_triangle(psx_gpu, vertexes, current_command);
299 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
301 get_vertex_data_xy(0, 2);
302 get_vertex_data_xy(1, 4);
303 get_vertex_data_xy(2, 6);
304 get_vertex_data_xy(3, 8);
306 render_triangle(psx_gpu, vertexes, current_command);
307 render_triangle(psx_gpu, &(vertexes[1]), current_command);
313 set_clut(psx_gpu, list_s16[5]);
314 set_texture(psx_gpu, list_s16[9]);
315 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
317 get_vertex_data_xy_uv(0, 2);
318 get_vertex_data_xy_uv(1, 6);
319 get_vertex_data_xy_uv(2, 10);
320 get_vertex_data_xy_uv(3, 14);
322 render_triangle(psx_gpu, vertexes, current_command);
323 render_triangle(psx_gpu, &(vertexes[1]), current_command);
329 get_vertex_data_xy_rgb(0, 0);
330 get_vertex_data_xy_rgb(1, 4);
331 get_vertex_data_xy_rgb(2, 8);
333 render_triangle(psx_gpu, vertexes, current_command);
342 set_clut(psx_gpu, list_s16[5]);
343 set_texture(psx_gpu, list_s16[11]);
345 get_vertex_data_xy_uv_rgb(0, 0);
346 get_vertex_data_xy_uv_rgb(1, 6);
347 get_vertex_data_xy_uv_rgb(2, 12);
349 render_triangle(psx_gpu, vertexes, current_command);
358 get_vertex_data_xy_rgb(0, 0);
359 get_vertex_data_xy_rgb(1, 4);
360 get_vertex_data_xy_rgb(2, 8);
361 get_vertex_data_xy_rgb(3, 12);
363 render_triangle(psx_gpu, vertexes, current_command);
364 render_triangle(psx_gpu, &(vertexes[1]), current_command);
373 set_clut(psx_gpu, list_s16[5]);
374 set_texture(psx_gpu, list_s16[11]);
376 get_vertex_data_xy_uv_rgb(0, 0);
377 get_vertex_data_xy_uv_rgb(1, 6);
378 get_vertex_data_xy_uv_rgb(2, 12);
379 get_vertex_data_xy_uv_rgb(3, 18);
381 render_triangle(psx_gpu, vertexes, current_command);
382 render_triangle(psx_gpu, &(vertexes[1]), current_command);
388 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
389 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
390 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
391 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
393 render_line(psx_gpu, vertexes, current_command, list[0]);
399 u32 num_vertexes = 1;
400 u32 *list_position = &(list[2]);
403 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
404 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
410 if((xy & 0xF000F000) == 0x50005000)
413 vertexes[0] = vertexes[1];
415 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
416 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
421 if(list_position > list_end)
424 render_line(psx_gpu, vertexes, current_command, list[0]);
428 command_length += (num_vertexes - 2);
435 vertexes[0].r = list[0] & 0xFF;
436 vertexes[0].g = (list[0] >> 8) & 0xFF;
437 vertexes[0].b = (list[0] >> 16) & 0xFF;
438 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
439 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
441 vertexes[1].r = list[2] & 0xFF;
442 vertexes[1].g = (list[2] >> 8) & 0xFF;
443 vertexes[1].b = (list[2] >> 16) & 0xFF;
444 vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
445 vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
447 render_line(psx_gpu, vertexes, current_command, 0);
453 u32 num_vertexes = 1;
454 u32 *list_position = &(list[2]);
458 vertexes[1].r = color & 0xFF;
459 vertexes[1].g = (color >> 8) & 0xFF;
460 vertexes[1].b = (color >> 16) & 0xFF;
461 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
462 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
466 color = list_position[0];
467 if((color & 0xF000F000) == 0x50005000)
470 xy = list_position[1];
472 vertexes[0] = vertexes[1];
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;
483 if(list_position > list_end)
486 render_line(psx_gpu, vertexes, current_command, 0);
490 command_length += ((num_vertexes * 2) - 2);
497 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
498 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
499 u32 width = list_s16[4] & 0x3FF;
500 u32 height = list_s16[5] & 0x1FF;
502 render_sprite(psx_gpu, x, y, 0, 0, width, height, current_command, list[0]);
508 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
509 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
510 u32 uv = list_s16[4];
511 u32 width = list_s16[6] & 0x3FF;
512 u32 height = list_s16[7] & 0x1FF;
514 set_clut(psx_gpu, list_s16[5]);
516 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, width, height,
517 current_command, list[0]);
526 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
527 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
529 render_sprite(psx_gpu, x, y, 0, 0, 1, 1, current_command, list[0]);
538 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
539 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
541 render_sprite(psx_gpu, x, y, 0, 0, 8, 8, current_command, list[0]);
550 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
551 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
552 u32 uv = list_s16[4];
554 set_clut(psx_gpu, list_s16[5]);
556 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 8, 8,
557 current_command, list[0]);
566 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
567 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
569 render_sprite(psx_gpu, x, y, 0, 0, 16, 16, current_command, list[0]);
578 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
579 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
580 u32 uv = list_s16[4];
582 set_clut(psx_gpu, list_s16[5]);
584 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 16, 16,
585 current_command, list[0]);
589 case 0x80: // vid -> vid
590 render_block_move(psx_gpu, list_s16[2] & 0x3FF, list_s16[3] & 0x1FF,
591 list_s16[4] & 0x3FF, list_s16[5] & 0x1FF, list_s16[6], list_s16[7]);
594 case 0xA0: // sys -> vid
596 u32 load_x = list_s16[2] & 0x3FF;
597 u32 load_y = list_s16[3] & 0x1FF;
598 u32 load_width = list_s16[4] & 0x3FF;
599 u32 load_height = list_s16[5] & 0x1FF;
600 u32 load_size = load_width * load_height;
602 command_length += load_size / 2;
607 render_block_copy(psx_gpu, (u16 *)&(list_s16[6]), load_x, load_y,
608 load_width, load_height, load_width);
612 case 0xC0: // vid -> sys
616 set_texture(psx_gpu, list[0] & 0x1FF);
618 if(list[0] & (1 << 9))
619 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
621 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
623 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
629 u32 texture_window_settings = list[0];
632 if(texture_window_settings != psx_gpu->texture_window_settings)
634 tmp = (texture_window_settings & 0x1F) | 0x20;
635 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
637 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
638 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
641 x = ((texture_window_settings >> 10) & tmp) << 3;
644 y = ((texture_window_settings >> 15) & tmp) << 3;
646 flush_render_block_buffer(psx_gpu);
648 psx_gpu->texture_window_settings = texture_window_settings;
649 psx_gpu->texture_window_x = x;
650 psx_gpu->texture_window_y = y;
651 psx_gpu->texture_mask_width = w - 1;
652 psx_gpu->texture_mask_height = h - 1;
654 update_texture_ptr(psx_gpu);
660 psx_gpu->viewport_start_x = list[0] & 0x3FF;
661 psx_gpu->viewport_start_y = (list[0] >> 10) & 0x1FF;
663 #ifdef TEXTURE_CACHE_4BPP
664 psx_gpu->viewport_mask =
665 texture_region_mask(psx_gpu->viewport_start_x,
666 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
667 psx_gpu->viewport_end_y);
672 psx_gpu->viewport_end_x = list[0] & 0x3FF;
673 psx_gpu->viewport_end_y = (list[0] >> 10) & 0x1FF;
675 #ifdef TEXTURE_CACHE_4BPP
676 psx_gpu->viewport_mask =
677 texture_region_mask(psx_gpu->viewport_start_x,
678 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
679 psx_gpu->viewport_end_y);
685 s32 offset_x = list[0] << 21;
686 s32 offset_y = list[0] << 10;
687 psx_gpu->offset_x = offset_x >> 21;
688 psx_gpu->offset_y = offset_y >> 21;
695 u32 mask_settings = list[0];
696 u16 mask_msb = mask_settings << 15;
699 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
701 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
703 if(mask_msb != psx_gpu->mask_msb)
705 flush_render_block_buffer(psx_gpu);
706 psx_gpu->mask_msb = mask_msb;