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 const u8 command_lengths[256] =
21 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00
22 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10
23 3, 3, 3, 3, 6, 6, 6, 6, 4, 4, 4, 4, 8, 8, 8, 8, // 20
24 5, 5, 5, 5, 8, 8, 8, 8, 7, 7, 7, 7, 11, 11, 11, 11, // 30
25 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 40
26 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, // 50
27 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, // 60
28 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, // 70
29 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90
31 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // a0
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // b0
33 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // c0
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // d0
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // e0
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // f0
39 void update_texture_ptr(psx_gpu_struct *psx_gpu)
44 switch((psx_gpu->render_state_base >> 8) & 0x3)
47 case TEXTURE_MODE_4BPP:
48 texture_base = psx_gpu->texture_4bpp_cache[psx_gpu->current_texture_page];
50 texture_ptr = texture_base;
51 texture_ptr += psx_gpu->texture_window_x & 0xF;
52 texture_ptr += (psx_gpu->texture_window_y & 0xF) << 4;
53 texture_ptr += (psx_gpu->texture_window_x >> 4) << 8;
54 texture_ptr += (psx_gpu->texture_window_y >> 4) << 12;
57 case TEXTURE_MODE_8BPP:
58 if(psx_gpu->current_texture_page & 0x1)
61 psx_gpu->texture_8bpp_odd_cache[psx_gpu->current_texture_page >> 1];
66 psx_gpu->texture_8bpp_even_cache[psx_gpu->current_texture_page >> 1];
69 texture_ptr = texture_base;
70 texture_ptr += psx_gpu->texture_window_x & 0xF;
71 texture_ptr += (psx_gpu->texture_window_y & 0xF) << 4;
72 texture_ptr += (psx_gpu->texture_window_x >> 4) << 8;
73 texture_ptr += (psx_gpu->texture_window_y >> 4) << 12;
76 case TEXTURE_MODE_16BPP:
77 texture_base = (u8 *)(psx_gpu->vram_ptr);
78 texture_base += (psx_gpu->current_texture_page & 0xF) * 128;
79 texture_base += ((psx_gpu->current_texture_page >> 4) * 256) * 2048;
81 texture_ptr = texture_base;
82 texture_ptr += psx_gpu->texture_window_x * 2;
83 texture_ptr += (psx_gpu->texture_window_y) * 2048;
87 psx_gpu->texture_page_base = texture_base;
88 psx_gpu->texture_page_ptr = texture_ptr;
91 void set_texture(psx_gpu_struct *psx_gpu, u32 texture_settings)
93 if(psx_gpu->texture_settings != texture_settings)
95 u32 new_texture_page = texture_settings & 0x1F;
96 u32 texture_mode = (texture_settings >> 7) & 0x3;
97 u32 render_state_base = psx_gpu->render_state_base;
99 if(psx_gpu->current_texture_page != new_texture_page)
100 flush_render_block_buffer(psx_gpu);
102 render_state_base &= ~(0xF << 6);
103 render_state_base |= ((texture_settings >> 5) & 0xF) << 6;
105 psx_gpu->render_state_base = render_state_base;
107 psx_gpu->current_texture_mask = 0x1 << new_texture_page;
109 if(texture_mode == TEXTURE_MODE_8BPP)
111 // In 8bpp mode 256x256 takes up two pages. If it's on the right edge it
112 // wraps back around to the left edge.
113 u32 adjacent_texture_page = ((texture_settings + 1) & 0xF) | (texture_settings & 0x10);
114 psx_gpu->current_texture_mask |= 0x1 << adjacent_texture_page;
116 if((psx_gpu->last_8bpp_texture_page ^ new_texture_page) & 0x1)
118 u32 dirty_textures_8bpp_alternate_mask =
119 psx_gpu->dirty_textures_8bpp_alternate_mask;
120 psx_gpu->dirty_textures_8bpp_alternate_mask =
121 psx_gpu->dirty_textures_8bpp_mask;
122 psx_gpu->dirty_textures_8bpp_mask = dirty_textures_8bpp_alternate_mask;
125 psx_gpu->last_8bpp_texture_page = new_texture_page;
128 psx_gpu->current_texture_page = new_texture_page;
129 psx_gpu->texture_settings = texture_settings;
131 update_texture_ptr(psx_gpu);
135 void set_clut(psx_gpu_struct *psx_gpu, u32 clut_settings)
137 if(psx_gpu->clut_settings != clut_settings)
139 flush_render_block_buffer(psx_gpu);
140 psx_gpu->clut_settings = clut_settings;
141 psx_gpu->clut_ptr = psx_gpu->vram_ptr + ((clut_settings & 0x7FFF) * 16);
145 void set_triangle_color(psx_gpu_struct *psx_gpu, u32 triangle_color)
147 if(psx_gpu->triangle_color != triangle_color)
149 flush_render_block_buffer(psx_gpu);
150 psx_gpu->triangle_color = triangle_color;
154 #define sign_extend_12bit(value) \
155 (((s32)((value) << 20)) >> 20) \
157 #define get_vertex_data_xy(vertex_number, offset16) \
158 vertexes[vertex_number].x = \
159 sign_extend_12bit(list_s16[offset16]) + psx_gpu->offset_x; \
160 vertexes[vertex_number].y = \
161 sign_extend_12bit(list_s16[(offset16) + 1]) + psx_gpu->offset_y; \
163 #define get_vertex_data_uv(vertex_number, offset16) \
164 vertexes[vertex_number].u = list_s16[offset16] & 0xFF; \
165 vertexes[vertex_number].v = (list_s16[offset16] >> 8) & 0xFF \
167 #define get_vertex_data_rgb(vertex_number, offset32) \
168 vertexes[vertex_number].r = list[offset32] & 0xFF; \
169 vertexes[vertex_number].g = (list[offset32] >> 8) & 0xFF; \
170 vertexes[vertex_number].b = (list[offset32] >> 16) & 0xFF \
172 #define get_vertex_data_xy_uv(vertex_number, offset16) \
173 get_vertex_data_xy(vertex_number, offset16); \
174 get_vertex_data_uv(vertex_number, (offset16) + 2) \
176 #define get_vertex_data_xy_rgb(vertex_number, offset16) \
177 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
178 get_vertex_data_xy(vertex_number, (offset16) + 2); \
180 #define get_vertex_data_xy_uv_rgb(vertex_number, offset16) \
181 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
182 get_vertex_data_xy(vertex_number, (offset16) + 2); \
183 get_vertex_data_uv(vertex_number, (offset16) + 4); \
185 #define set_vertex_color_constant(vertex_number, color) \
186 vertexes[vertex_number].r = color & 0xFF; \
187 vertexes[vertex_number].g = (color >> 8) & 0xFF; \
188 vertexes[vertex_number].b = (color >> 16) & 0xFF \
190 #define get_vertex_data_xy_rgb_constant(vertex_number, offset16, color) \
191 get_vertex_data_xy(vertex_number, offset16); \
192 set_vertex_color_constant(vertex_number, color) \
194 vertex_struct vertexes[4] __attribute__((aligned(32)));
196 void gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size)
198 u32 current_command, command_length;
200 u32 *list_end = list + (size / 4);
202 for(; list < list_end; list += 1 + command_length)
204 s16 *list_s16 = (void *)list;
205 current_command = *list >> 24;
206 command_length = command_lengths[current_command];
208 switch(current_command)
214 render_block_fill(psx_gpu, list[0] & 0xFFFFFF, list_s16[2], list_s16[3],
215 list_s16[4] & 0x3FF, list_s16[5] & 0x3FF);
220 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
222 get_vertex_data_xy(0, 2);
223 get_vertex_data_xy(1, 4);
224 get_vertex_data_xy(2, 6);
226 render_triangle(psx_gpu, vertexes, current_command);
232 set_clut(psx_gpu, list_s16[5]);
233 set_texture(psx_gpu, list_s16[9]);
234 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
236 get_vertex_data_xy_uv(0, 2);
237 get_vertex_data_xy_uv(1, 6);
238 get_vertex_data_xy_uv(2, 10);
240 render_triangle(psx_gpu, vertexes, current_command);
246 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
248 get_vertex_data_xy(0, 2);
249 get_vertex_data_xy(1, 4);
250 get_vertex_data_xy(2, 6);
251 get_vertex_data_xy(3, 8);
253 render_triangle(psx_gpu, vertexes, current_command);
254 render_triangle(psx_gpu, &(vertexes[1]), current_command);
260 set_clut(psx_gpu, list_s16[5]);
261 set_texture(psx_gpu, list_s16[9]);
262 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
264 get_vertex_data_xy_uv(0, 2);
265 get_vertex_data_xy_uv(1, 6);
266 get_vertex_data_xy_uv(2, 10);
267 get_vertex_data_xy_uv(3, 14);
269 render_triangle(psx_gpu, vertexes, current_command);
270 render_triangle(psx_gpu, &(vertexes[1]), current_command);
276 get_vertex_data_xy_rgb(0, 0);
277 get_vertex_data_xy_rgb(1, 4);
278 get_vertex_data_xy_rgb(2, 8);
280 render_triangle(psx_gpu, vertexes, current_command);
289 set_clut(psx_gpu, list_s16[5]);
290 set_texture(psx_gpu, list_s16[11]);
292 get_vertex_data_xy_uv_rgb(0, 0);
293 get_vertex_data_xy_uv_rgb(1, 6);
294 get_vertex_data_xy_uv_rgb(2, 12);
296 render_triangle(psx_gpu, vertexes, current_command);
305 get_vertex_data_xy_rgb(0, 0);
306 get_vertex_data_xy_rgb(1, 4);
307 get_vertex_data_xy_rgb(2, 8);
308 get_vertex_data_xy_rgb(3, 12);
310 render_triangle(psx_gpu, vertexes, current_command);
311 render_triangle(psx_gpu, &(vertexes[1]), current_command);
320 set_clut(psx_gpu, list_s16[5]);
321 set_texture(psx_gpu, list_s16[11]);
323 get_vertex_data_xy_uv_rgb(0, 0);
324 get_vertex_data_xy_uv_rgb(1, 6);
325 get_vertex_data_xy_uv_rgb(2, 12);
326 get_vertex_data_xy_uv_rgb(3, 18);
328 render_triangle(psx_gpu, vertexes, current_command);
329 render_triangle(psx_gpu, &(vertexes[1]), current_command);
335 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
336 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
337 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
338 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
340 render_line(psx_gpu, vertexes, current_command, list[0]);
346 u32 num_vertexes = 1;
347 u32 *list_position = &(list[2]);
350 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
351 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
359 vertexes[0] = vertexes[1];
361 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
362 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
367 render_line(psx_gpu, vertexes, current_command, list[0]);
371 command_length += (num_vertexes - 2);
378 vertexes[0].r = list[0] & 0xFF;
379 vertexes[0].g = (list[0] >> 8) & 0xFF;
380 vertexes[0].b = (list[0] >> 16) & 0xFF;
381 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
382 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
384 vertexes[1].r = list[2] & 0xFF;
385 vertexes[1].g = (list[2] >> 8) & 0xFF;
386 vertexes[1].b = (list[2] >> 16) & 0xFF;
387 vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
388 vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
390 render_line(psx_gpu, vertexes, current_command, 0);
396 u32 num_vertexes = 1;
397 u32 *list_position = &(list[2]);
401 vertexes[1].r = color & 0xFF;
402 vertexes[1].g = (color >> 8) & 0xFF;
403 vertexes[1].b = (color >> 16) & 0xFF;
404 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
405 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
409 color = list_position[0];
410 if(color == 0x55555555)
413 xy = list_position[1];
415 vertexes[0] = vertexes[1];
417 vertexes[1].r = color & 0xFF;
418 vertexes[1].g = (color >> 8) & 0xFF;
419 vertexes[1].b = (color >> 16) & 0xFF;
420 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
421 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
426 render_line(psx_gpu, vertexes, current_command, 0);
430 command_length += ((num_vertexes * 2) - 2);
437 u32 x = list_s16[2] + psx_gpu->offset_x;
438 u32 y = list_s16[3] + psx_gpu->offset_y;
439 u32 width = list_s16[4] & 0x3FF;
440 u32 height = list_s16[5] & 0x1FF;
442 render_sprite(psx_gpu, x, y, 0, 0, width, height, current_command, list[0]);
448 u32 x = list_s16[2] + psx_gpu->offset_x;
449 u32 y = list_s16[3] + psx_gpu->offset_y;
450 u32 uv = list_s16[4];
451 u32 width = list_s16[6] & 0x3FF;
452 u32 height = list_s16[7] & 0x1FF;
454 set_clut(psx_gpu, list_s16[5]);
456 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, width, height,
457 current_command, list[0]);
466 s32 x = list_s16[2] + psx_gpu->offset_x;
467 s32 y = list_s16[3] + psx_gpu->offset_y;
469 render_sprite(psx_gpu, x, y, 0, 0, 1, 1, current_command, list[0]);
478 s32 x = list_s16[2] + psx_gpu->offset_x;
479 s32 y = list_s16[3] + psx_gpu->offset_y;
481 render_sprite(psx_gpu, x, y, 0, 0, 8, 8, current_command, list[0]);
490 s32 x = list_s16[2] + psx_gpu->offset_x;
491 s32 y = list_s16[3] + psx_gpu->offset_y;
492 u32 uv = list_s16[4];
494 set_clut(psx_gpu, list_s16[5]);
496 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 8, 8,
497 current_command, list[0]);
506 s32 x = list_s16[2] + psx_gpu->offset_x;
507 s32 y = list_s16[3] + psx_gpu->offset_y;
509 render_sprite(psx_gpu, x, y, 0, 0, 16, 16, current_command, list[0]);
518 s32 x = list_s16[2] + psx_gpu->offset_x;
519 s32 y = list_s16[3] + psx_gpu->offset_y;
520 u32 uv = list_s16[4];
522 set_clut(psx_gpu, list_s16[5]);
524 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 16, 16,
525 current_command, list[0]);
529 case 0x80: // vid -> vid
530 render_block_move(psx_gpu, list_s16[2] & 0x3FF, list_s16[3] & 0x1FF,
531 list_s16[4] & 0x3FF, list_s16[5] & 0x1FF, list_s16[6], list_s16[7]);
534 case 0xA0: // sys -> vid
536 u32 load_x = list_s16[2];
537 u32 load_y = list_s16[3];
538 u32 load_width = list_s16[4];
539 u32 load_height = list_s16[5];
540 u32 load_size = load_width * load_height;
542 command_length += load_size / 2;
544 render_block_copy(psx_gpu, (u16 *)&(list_s16[6]), load_x, load_y,
545 load_width, load_height, load_width);
549 case 0xC0: // vid -> sys
553 set_texture(psx_gpu, list[0] & 0x1FF);
554 if(list[0] & (1 << 9))
555 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
557 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
559 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
565 u32 texture_window_settings = list[0];
568 if(texture_window_settings != psx_gpu->texture_window_settings)
570 tmp = (texture_window_settings & 0x1F) | 0x20;
571 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
573 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
574 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
577 x = ((texture_window_settings >> 10) & tmp) << 3;
580 y = ((texture_window_settings >> 15) & tmp) << 3;
582 flush_render_block_buffer(psx_gpu);
584 psx_gpu->texture_window_settings = texture_window_settings;
585 psx_gpu->texture_window_x = x;
586 psx_gpu->texture_window_y = y;
587 psx_gpu->texture_mask_width = w - 1;
588 psx_gpu->texture_mask_height = h - 1;
590 update_texture_ptr(psx_gpu);
596 psx_gpu->viewport_start_x = list[0] & 0x3FF;
597 psx_gpu->viewport_start_y = (list[0] >> 10) & 0x1FF;
599 #ifdef TEXTURE_CACHE_4BPP
600 psx_gpu->viewport_mask =
601 texture_region_mask(psx_gpu->viewport_start_x,
602 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
603 psx_gpu->viewport_end_y);
608 psx_gpu->viewport_end_x = list[0] & 0x3FF;
609 psx_gpu->viewport_end_y = (list[0] >> 10) & 0x1FF;
611 #ifdef TEXTURE_CACHE_4BPP
612 psx_gpu->viewport_mask =
613 texture_region_mask(psx_gpu->viewport_start_x,
614 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
615 psx_gpu->viewport_end_y);
621 s32 offset_x = list[0] << 21;
622 s32 offset_y = list[0] << 10;
623 psx_gpu->offset_x = offset_x >> 21;
624 psx_gpu->offset_y = offset_y >> 21;
631 u32 mask_settings = list[0];
632 u16 mask_msb = mask_settings << 15;
635 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
637 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
639 if(mask_msb != psx_gpu->mask_msb)
641 flush_render_block_buffer(psx_gpu);
642 psx_gpu->mask_msb = mask_msb;