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) \
206 vertex_struct vertexes[4] __attribute__((aligned(32)));
208 u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_command)
210 u32 current_command = 0, command_length;
212 u32 *list_start = list;
213 u32 *list_end = list + (size / 4);
215 for(; list < list_end; list += 1 + command_length)
217 s16 *list_s16 = (void *)list;
218 current_command = *list >> 24;
219 command_length = command_lengths[current_command];
220 if (list + 1 + command_length > list_end) {
221 current_command = (u32)-1;
225 switch(current_command)
232 u32 x = list_s16[2] & 0x3FF;
233 u32 y = list_s16[3] & 0x1FF;
234 u32 width = list_s16[4] & 0x3FF;
235 u32 height = list_s16[5] & 0x1FF;
236 u32 color = list[0] & 0xFFFFFF;
239 width = ((width + 0xF) & ~0xF);
241 if((x + width) > 1024)
243 u32 width_a = 1024 - x;
244 u32 width_b = width - width_a;
246 if((y + height) > 512)
248 u32 height_a = 512 - y;
249 u32 height_b = height - height_a;
251 render_block_fill(psx_gpu, color, x, y, width_a, height_a);
252 render_block_fill(psx_gpu, color, 0, y, width_b, height_a);
253 render_block_fill(psx_gpu, color, x, 0, width_a, height_b);
254 render_block_fill(psx_gpu, color, 0, 0, width_b, height_b);
258 render_block_fill(psx_gpu, color, x, y, width_a, height);
259 render_block_fill(psx_gpu, color, 0, y, width_b, height);
264 if((y + height) > 512)
266 u32 height_a = 512 - y;
267 u32 height_b = height - height_a;
269 render_block_fill(psx_gpu, color, x, y, width, height_a);
270 render_block_fill(psx_gpu, color, x, 0, width, height_b);
274 render_block_fill(psx_gpu, color, x, y, width, height);
282 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
284 get_vertex_data_xy(0, 2);
285 get_vertex_data_xy(1, 4);
286 get_vertex_data_xy(2, 6);
288 render_triangle(psx_gpu, vertexes, current_command);
294 set_clut(psx_gpu, list_s16[5]);
295 set_texture(psx_gpu, list_s16[9]);
296 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
298 get_vertex_data_xy_uv(0, 2);
299 get_vertex_data_xy_uv(1, 6);
300 get_vertex_data_xy_uv(2, 10);
302 render_triangle(psx_gpu, vertexes, current_command);
308 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
310 get_vertex_data_xy(0, 2);
311 get_vertex_data_xy(1, 4);
312 get_vertex_data_xy(2, 6);
313 get_vertex_data_xy(3, 8);
315 render_triangle(psx_gpu, vertexes, current_command);
316 render_triangle(psx_gpu, &(vertexes[1]), current_command);
322 set_clut(psx_gpu, list_s16[5]);
323 set_texture(psx_gpu, list_s16[9]);
324 set_triangle_color(psx_gpu, list[0] & 0xFFFFFF);
326 get_vertex_data_xy_uv(0, 2);
327 get_vertex_data_xy_uv(1, 6);
328 get_vertex_data_xy_uv(2, 10);
329 get_vertex_data_xy_uv(3, 14);
331 render_triangle(psx_gpu, vertexes, current_command);
332 render_triangle(psx_gpu, &(vertexes[1]), current_command);
338 get_vertex_data_xy_rgb(0, 0);
339 get_vertex_data_xy_rgb(1, 4);
340 get_vertex_data_xy_rgb(2, 8);
342 render_triangle(psx_gpu, vertexes, current_command);
351 set_clut(psx_gpu, list_s16[5]);
352 set_texture(psx_gpu, list_s16[11]);
354 get_vertex_data_xy_uv_rgb(0, 0);
355 get_vertex_data_xy_uv_rgb(1, 6);
356 get_vertex_data_xy_uv_rgb(2, 12);
358 render_triangle(psx_gpu, vertexes, current_command);
367 get_vertex_data_xy_rgb(0, 0);
368 get_vertex_data_xy_rgb(1, 4);
369 get_vertex_data_xy_rgb(2, 8);
370 get_vertex_data_xy_rgb(3, 12);
372 render_triangle(psx_gpu, vertexes, current_command);
373 render_triangle(psx_gpu, &(vertexes[1]), current_command);
382 set_clut(psx_gpu, list_s16[5]);
383 set_texture(psx_gpu, list_s16[11]);
385 get_vertex_data_xy_uv_rgb(0, 0);
386 get_vertex_data_xy_uv_rgb(1, 6);
387 get_vertex_data_xy_uv_rgb(2, 12);
388 get_vertex_data_xy_uv_rgb(3, 18);
390 render_triangle(psx_gpu, vertexes, current_command);
391 render_triangle(psx_gpu, &(vertexes[1]), current_command);
397 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
398 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
399 vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
400 vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
402 render_line(psx_gpu, vertexes, current_command, list[0]);
408 u32 num_vertexes = 1;
409 u32 *list_position = &(list[2]);
412 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
413 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
418 vertexes[0] = vertexes[1];
420 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
421 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
423 render_line(psx_gpu, vertexes, current_command, list[0]);
428 if(list_position >= list_end)
432 if((xy & 0xF000F000) == 0x50005000)
436 command_length += (num_vertexes - 2);
442 vertexes[0].r = list[0] & 0xFF;
443 vertexes[0].g = (list[0] >> 8) & 0xFF;
444 vertexes[0].b = (list[0] >> 16) & 0xFF;
445 vertexes[0].x = list_s16[2] + psx_gpu->offset_x;
446 vertexes[0].y = list_s16[3] + psx_gpu->offset_y;
448 vertexes[1].r = list[2] & 0xFF;
449 vertexes[1].g = (list[2] >> 8) & 0xFF;
450 vertexes[1].b = (list[2] >> 16) & 0xFF;
451 vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
452 vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
454 render_line(psx_gpu, vertexes, current_command, 0);
460 u32 num_vertexes = 1;
461 u32 *list_position = &(list[2]);
465 vertexes[1].r = color & 0xFF;
466 vertexes[1].g = (color >> 8) & 0xFF;
467 vertexes[1].b = (color >> 16) & 0xFF;
468 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
469 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
471 color = list_position[0];
474 xy = list_position[1];
476 vertexes[0] = vertexes[1];
478 vertexes[1].r = color & 0xFF;
479 vertexes[1].g = (color >> 8) & 0xFF;
480 vertexes[1].b = (color >> 16) & 0xFF;
481 vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
482 vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
484 render_line(psx_gpu, vertexes, current_command, 0);
489 if(list_position >= list_end)
492 color = list_position[0];
493 if((color & 0xF000F000) == 0x50005000)
497 command_length += ((num_vertexes - 2) * 2);
503 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
504 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
505 u32 width = list_s16[4] & 0x3FF;
506 u32 height = list_s16[5] & 0x1FF;
508 render_sprite(psx_gpu, x, y, 0, 0, width, height, current_command, list[0]);
514 u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
515 u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
516 u32 uv = list_s16[4];
517 u32 width = list_s16[6] & 0x3FF;
518 u32 height = list_s16[7] & 0x1FF;
520 set_clut(psx_gpu, list_s16[5]);
522 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, width, height,
523 current_command, list[0]);
532 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
533 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
535 render_sprite(psx_gpu, x, y, 0, 0, 1, 1, current_command, list[0]);
544 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
545 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
547 render_sprite(psx_gpu, x, y, 0, 0, 8, 8, current_command, list[0]);
556 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
557 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
558 u32 uv = list_s16[4];
560 set_clut(psx_gpu, list_s16[5]);
562 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 8, 8,
563 current_command, list[0]);
572 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
573 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
575 render_sprite(psx_gpu, x, y, 0, 0, 16, 16, current_command, list[0]);
584 s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
585 s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
586 u32 uv = list_s16[4];
588 set_clut(psx_gpu, list_s16[5]);
590 render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 16, 16,
591 current_command, list[0]);
595 case 0x80: // vid -> vid
596 render_block_move(psx_gpu, list_s16[2] & 0x3FF, list_s16[3] & 0x1FF,
597 list_s16[4] & 0x3FF, list_s16[5] & 0x1FF,
598 ((list_s16[6] - 1) & 0x3FF) + 1, ((list_s16[7] - 1) & 0x1FF) + 1);
602 case 0xA0: // sys -> vid
603 case 0xC0: // vid -> sys
606 case 0xA0: // sys -> vid
608 u32 load_x = list_s16[2] & 0x3FF;
609 u32 load_y = list_s16[3] & 0x1FF;
610 u32 load_width = list_s16[4] & 0x3FF;
611 u32 load_height = list_s16[5] & 0x1FF;
612 u32 load_size = load_width * load_height;
614 command_length += load_size / 2;
619 render_block_copy(psx_gpu, (u16 *)&(list_s16[6]), load_x, load_y,
620 load_width, load_height, load_width);
624 case 0xC0: // vid -> sys
629 set_texture(psx_gpu, list[0] & 0x1FF);
631 if(list[0] & (1 << 9))
632 psx_gpu->render_state_base |= RENDER_STATE_DITHER;
634 psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
636 psx_gpu->display_area_draw_enable = (list[0] >> 10) & 0x1;
643 u32 texture_window_settings = list[0];
646 if(texture_window_settings != psx_gpu->texture_window_settings)
648 tmp = (texture_window_settings & 0x1F) | 0x20;
649 for(w = 8; (tmp & 1) == 0; tmp >>= 1, w <<= 1);
651 tmp = ((texture_window_settings >> 5) & 0x1f) | 0x20;
652 for (h = 8; (tmp & 1) == 0; tmp >>= 1, h <<= 1);
655 x = ((texture_window_settings >> 10) & tmp) << 3;
658 y = ((texture_window_settings >> 15) & tmp) << 3;
660 flush_render_block_buffer(psx_gpu);
662 psx_gpu->texture_window_settings = texture_window_settings;
663 psx_gpu->texture_window_x = x;
664 psx_gpu->texture_window_y = y;
665 psx_gpu->texture_mask_width = w - 1;
666 psx_gpu->texture_mask_height = h - 1;
668 update_texture_ptr(psx_gpu);
675 psx_gpu->viewport_start_x = list[0] & 0x3FF;
676 psx_gpu->viewport_start_y = (list[0] >> 10) & 0x1FF;
678 #ifdef TEXTURE_CACHE_4BPP
679 psx_gpu->viewport_mask =
680 texture_region_mask(psx_gpu->viewport_start_x,
681 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
682 psx_gpu->viewport_end_y);
688 psx_gpu->viewport_end_x = list[0] & 0x3FF;
689 psx_gpu->viewport_end_y = (list[0] >> 10) & 0x1FF;
691 #ifdef TEXTURE_CACHE_4BPP
692 psx_gpu->viewport_mask =
693 texture_region_mask(psx_gpu->viewport_start_x,
694 psx_gpu->viewport_start_y, psx_gpu->viewport_end_x,
695 psx_gpu->viewport_end_y);
702 s32 offset_x = list[0] << 21;
703 s32 offset_y = list[0] << 10;
704 psx_gpu->offset_x = offset_x >> 21;
705 psx_gpu->offset_y = offset_y >> 21;
713 u32 mask_settings = list[0];
714 u16 mask_msb = mask_settings << 15;
717 psx_gpu->render_state_base |= RENDER_STATE_MASK_EVALUATE;
719 psx_gpu->render_state_base &= ~RENDER_STATE_MASK_EVALUATE;
721 if(mask_msb != psx_gpu->mask_msb)
723 flush_render_block_buffer(psx_gpu);
724 psx_gpu->mask_msb = mask_msb;
739 if (last_command != NULL)
740 *last_command = current_command;
741 return list - list_start;