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 flush_render_block_buffer(psx_gpu);
101 render_state_base &= ~(0xF << 6);
102 render_state_base |= ((texture_settings >> 5) & 0xF) << 6;
104 psx_gpu->render_state_base = render_state_base;
106 psx_gpu->current_texture_mask = 0x1 << new_texture_page;
108 if(texture_mode == TEXTURE_MODE_8BPP)
110 // In 8bpp mode 256x256 takes up two pages. If it's on the right edge it
111 // wraps back around to the left edge.
112 u32 adjacent_texture_page = ((texture_settings + 1) & 0xF) | (texture_settings & 0x10);
113 psx_gpu->current_texture_mask |= 0x1 << adjacent_texture_page;
115 if((psx_gpu->last_8bpp_texture_page ^ new_texture_page) & 0x1)
117 u32 dirty_textures_8bpp_alternate_mask =
118 psx_gpu->dirty_textures_8bpp_alternate_mask;
119 psx_gpu->dirty_textures_8bpp_alternate_mask =
120 psx_gpu->dirty_textures_8bpp_mask;
121 psx_gpu->dirty_textures_8bpp_mask = dirty_textures_8bpp_alternate_mask;
124 psx_gpu->last_8bpp_texture_page = new_texture_page;
127 psx_gpu->current_texture_page = new_texture_page;
128 psx_gpu->texture_settings = texture_settings;
130 update_texture_ptr(psx_gpu);
134 void set_clut(psx_gpu_struct *psx_gpu, u32 clut_settings)
136 if(psx_gpu->clut_settings != clut_settings)
138 flush_render_block_buffer(psx_gpu);
139 psx_gpu->clut_settings = clut_settings;
140 psx_gpu->clut_ptr = psx_gpu->vram_ptr + ((clut_settings & 0x7FFF) * 16);
144 void set_triangle_color(psx_gpu_struct *psx_gpu, u32 triangle_color)
146 if(psx_gpu->triangle_color != triangle_color)
148 flush_render_block_buffer(psx_gpu);
149 psx_gpu->triangle_color = triangle_color;
153 #define sign_extend_12bit(value) \
154 (((s32)((value) << 20)) >> 20) \
156 #define sign_extend_11bit(value) \
157 (((s32)((value) << 21)) >> 21) \
159 #define sign_extend_10bit(value) \
160 (((s32)((value) << 22)) >> 22) \
163 #define get_vertex_data_xy(vertex_number, offset16) \
164 vertexes[vertex_number].x = \
165 sign_extend_12bit(list_s16[offset16]) + psx_gpu->offset_x; \
166 vertexes[vertex_number].y = \
167 sign_extend_12bit(list_s16[(offset16) + 1]) + psx_gpu->offset_y; \
169 #define get_vertex_data_uv(vertex_number, offset16) \
170 vertexes[vertex_number].u = list_s16[offset16] & 0xFF; \
171 vertexes[vertex_number].v = (list_s16[offset16] >> 8) & 0xFF \
173 #define get_vertex_data_rgb(vertex_number, offset32) \
174 vertexes[vertex_number].r = list[offset32] & 0xFF; \
175 vertexes[vertex_number].g = (list[offset32] >> 8) & 0xFF; \
176 vertexes[vertex_number].b = (list[offset32] >> 16) & 0xFF \
178 #define get_vertex_data_xy_uv(vertex_number, offset16) \
179 get_vertex_data_xy(vertex_number, offset16); \
180 get_vertex_data_uv(vertex_number, (offset16) + 2) \
182 #define get_vertex_data_xy_rgb(vertex_number, offset16) \
183 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
184 get_vertex_data_xy(vertex_number, (offset16) + 2); \
186 #define get_vertex_data_xy_uv_rgb(vertex_number, offset16) \
187 get_vertex_data_rgb(vertex_number, (offset16) / 2); \
188 get_vertex_data_xy(vertex_number, (offset16) + 2); \
189 get_vertex_data_uv(vertex_number, (offset16) + 4); \
191 #define set_vertex_color_constant(vertex_number, color) \
192 vertexes[vertex_number].r = color & 0xFF; \
193 vertexes[vertex_number].g = (color >> 8) & 0xFF; \
194 vertexes[vertex_number].b = (color >> 16) & 0xFF \
196 #define get_vertex_data_xy_rgb_constant(vertex_number, offset16, color) \
197 get_vertex_data_xy(vertex_number, offset16); \
198 set_vertex_color_constant(vertex_number, color) \
200 vertex_struct vertexes[4] __attribute__((aligned(32)));
202 void gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size)
204 u32 current_command, command_length;
206 u32 *list_end = list + (size / 4);
208 for(; list < list_end; list += 1 + command_length)
210 s16 *list_s16 = (void *)list;
211 current_command = *list >> 24;
212 command_length = command_lengths[current_command];
214 switch(current_command)
220 render_block_fill(psx_gpu, list[0] & 0xFFFFFF, list_s16[2] & 0x3FF,
221 list_s16[3] & 0x1FF, list_s16[4] & 0x3FF, list_s16[5] & 0x1FF);
226 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
228 get_vertex_data_xy(0, 2);
229 get_vertex_data_xy(1, 4);
230 get_vertex_data_xy(2, 6);
232 render_triangle(psx_gpu, vertexes, current_command);
238 set_clut(psx_gpu, list_s16[5]);
239 set_texture(psx_gpu, list_s16[9]);
240 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
242 get_vertex_data_xy_uv(0, 2);
243 get_vertex_data_xy_uv(1, 6);
244 get_vertex_data_xy_uv(2, 10);
246 render_triangle(psx_gpu, vertexes, current_command);
252 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
254 get_vertex_data_xy(0, 2);
255 get_vertex_data_xy(1, 4);
256 get_vertex_data_xy(2, 6);
257 get_vertex_data_xy(3, 8);
259 render_triangle(psx_gpu, vertexes, current_command);
260 render_triangle(psx_gpu, &(vertexes[1]), current_command);
266 set_clut(psx_gpu, list_s16[5]);
267 set_texture(psx_gpu, list_s16[9]);
268 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
270 get_vertex_data_xy_uv(0, 2);
271 get_vertex_data_xy_uv(1, 6);
272 get_vertex_data_xy_uv(2, 10);
273 get_vertex_data_xy_uv(3, 14);
275 render_triangle(psx_gpu, vertexes, current_command);
276 render_triangle(psx_gpu, &(vertexes[1]), current_command);
282 get_vertex_data_xy_rgb(0, 0);
283 get_vertex_data_xy_rgb(1, 4);
284 get_vertex_data_xy_rgb(2, 8);
286 render_triangle(psx_gpu, vertexes, current_command);
295 set_clut(psx_gpu, list_s16[5]);
296 set_texture(psx_gpu, list_s16[11]);
298 get_vertex_data_xy_uv_rgb(0, 0);
299 get_vertex_data_xy_uv_rgb(1, 6);
300 get_vertex_data_xy_uv_rgb(2, 12);
302 render_triangle(psx_gpu, vertexes, current_command);
311 get_vertex_data_xy_rgb(0, 0);
312 get_vertex_data_xy_rgb(1, 4);
313 get_vertex_data_xy_rgb(2, 8);
314 get_vertex_data_xy_rgb(3, 12);
316 render_triangle(psx_gpu, vertexes, current_command);
317 render_triangle(psx_gpu, &(vertexes[1]), current_command);
326 set_clut(psx_gpu, list_s16[5]);
327 set_texture(psx_gpu, list_s16[11]);
329 get_vertex_data_xy_uv_rgb(0, 0);
330 get_vertex_data_xy_uv_rgb(1, 6);
331 get_vertex_data_xy_uv_rgb(2, 12);
332 get_vertex_data_xy_uv_rgb(3, 18);
334 render_triangle(psx_gpu, vertexes, current_command);
335 render_triangle(psx_gpu, &(vertexes[1]), current_command);
341 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
342 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
343 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
344 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
346 render_line(psx_gpu, vertexes, current_command, list[0]);
352 u32 num_vertexes = 1;
353 u32 *list_position = &(list[2]);
356 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
357 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
363 if((xy & 0xF000F000) == 0x50005000)
366 vertexes[0] = vertexes[1];
368 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
369 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
374 if(list_position > list_end)
377 render_line(psx_gpu, vertexes, current_command, list[0]);
381 command_length += (num_vertexes - 2);
388 vertexes[0].r = list[0] & 0xFF;
389 vertexes[0].g = (list[0] >> 8) & 0xFF;
390 vertexes[0].b = (list[0] >> 16) & 0xFF;
391 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
392 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
394 vertexes[1].r = list[2] & 0xFF;
395 vertexes[1].g = (list[2] >> 8) & 0xFF;
396 vertexes[1].b = (list[2] >> 16) & 0xFF;
397 vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
398 vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
400 render_line(psx_gpu, vertexes, current_command, 0);
406 u32 num_vertexes = 1;
407 u32 *list_position = &(list[2]);
411 vertexes[1].r = color & 0xFF;
412 vertexes[1].g = (color >> 8) & 0xFF;
413 vertexes[1].b = (color >> 16) & 0xFF;
414 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
415 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
419 color = list_position[0];
420 if((color & 0xF000F000) == 0x50005000)
423 xy = list_position[1];
425 vertexes[0] = vertexes[1];
427 vertexes[1].r = color & 0xFF;
428 vertexes[1].g = (color >> 8) & 0xFF;
429 vertexes[1].b = (color >> 16) & 0xFF;
430 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
431 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
436 if(list_position > list_end)
439 render_line(psx_gpu, vertexes, current_command, 0);
443 command_length += ((num_vertexes * 2) - 2);
450 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
451 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
452 u32 width = list_s16[4] & 0x3FF;
453 u32 height = list_s16[5] & 0x1FF;
455 render_sprite(psx_gpu, x, y, 0, 0, width, height, current_command, list[0]);
461 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
462 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
463 u32 uv = list_s16[4];
464 u32 width = list_s16[6] & 0x3FF;
465 u32 height = list_s16[7] & 0x1FF;
467 set_clut(psx_gpu, list_s16[5]);
469 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, width, height,
470 current_command, list[0]);
479 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
480 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
482 render_sprite(psx_gpu, x, y, 0, 0, 1, 1, current_command, list[0]);
491 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
492 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
494 render_sprite(psx_gpu, x, y, 0, 0, 8, 8, current_command, list[0]);
503 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
504 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
505 u32 uv = list_s16[4];
507 set_clut(psx_gpu, list_s16[5]);
509 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 8, 8,
510 current_command, list[0]);
519 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
520 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
522 render_sprite(psx_gpu, x, y, 0, 0, 16, 16, current_command, list[0]);
531 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
532 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
533 u32 uv = list_s16[4];
535 set_clut(psx_gpu, list_s16[5]);
537 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 16, 16,
538 current_command, list[0]);
542 case 0x80: // vid -> vid
543 render_block_move(psx_gpu, list_s16[2] & 0x3FF, list_s16[3] & 0x1FF,
544 list_s16[4] & 0x3FF, list_s16[5] & 0x1FF, list_s16[6], list_s16[7]);
547 case 0xA0: // sys -> vid
549 u32 load_x = list_s16[2] & 0x3FF;
550 u32 load_y = list_s16[3] & 0x1FF;
551 u32 load_width = list_s16[4] & 0x3FF;
552 u32 load_height = list_s16[5] & 0x1FF;
553 u32 load_size = load_width * load_height;
555 command_length += load_size / 2;
557 render_block_copy(psx_gpu, (u16 *)&(list_s16[6]), load_x, load_y,
558 load_width, load_height, load_width);
562 case 0xC0: // vid -> sys
566 set_texture(psx_gpu, list[0] & 0x1FF);
567 if(list[0] & (1 << 9))
568 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
570 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
572 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
578 u32 texture_window_settings = list[0];
581 if(texture_window_settings != psx_gpu->texture_window_settings)
583 tmp = (texture_window_settings & 0x1F) | 0x20;
584 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
586 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
587 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
590 x = ((texture_window_settings >> 10) & tmp) << 3;
593 y = ((texture_window_settings >> 15) & tmp) << 3;
595 flush_render_block_buffer(psx_gpu);
597 psx_gpu->texture_window_settings = texture_window_settings;
598 psx_gpu->texture_window_x = x;
599 psx_gpu->texture_window_y = y;
600 psx_gpu->texture_mask_width = w - 1;
601 psx_gpu->texture_mask_height = h - 1;
603 update_texture_ptr(psx_gpu);
609 psx_gpu->viewport_start_x = list[0] & 0x3FF;
610 psx_gpu->viewport_start_y = (list[0] >> 10) & 0x1FF;
612 #ifdef TEXTURE_CACHE_4BPP
613 psx_gpu->viewport_mask =
614 texture_region_mask(psx_gpu->viewport_start_x,
615 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
616 psx_gpu->viewport_end_y);
621 psx_gpu->viewport_end_x = list[0] & 0x3FF;
622 psx_gpu->viewport_end_y = (list[0] >> 10) & 0x1FF;
624 #ifdef TEXTURE_CACHE_4BPP
625 psx_gpu->viewport_mask =
626 texture_region_mask(psx_gpu->viewport_start_x,
627 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
628 psx_gpu->viewport_end_y);
634 s32 offset_x = list[0] << 21;
635 s32 offset_y = list[0] << 10;
636 psx_gpu->offset_x = offset_x >> 21;
637 psx_gpu->offset_y = offset_y >> 21;
644 u32 mask_settings = list[0];
645 u16 mask_msb = mask_settings << 15;
648 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
650 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
652 if(mask_msb != psx_gpu->mask_msb)
654 flush_render_block_buffer(psx_gpu);
655 psx_gpu->mask_msb = mask_msb;