3 * Copyright (C) 2006 Exophase <exophase@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <pspkernel.h>
29 #include <pspdisplay.h>
35 static float *screen_vertex = (float *)0x441FC100;
36 static u32 *ge_cmd = (u32 *)0x441FC000;
37 static u16 *psp_gu_vram_base = (u16 *)(0x44000000);
38 static u32 *ge_cmd_ptr = (u32 *)0x441FC000;
40 static u32 video_direct = 0;
42 static u32 __attribute__((aligned(16))) display_list[32];
44 #define GBA_SCREEN_WIDTH 240
45 #define GBA_SCREEN_HEIGHT 160
47 #define PSP_SCREEN_WIDTH 480
48 #define PSP_SCREEN_HEIGHT 272
49 #define PSP_LINE_SIZE 512
51 #define PSP_ALL_BUTTON_MASK 0xFFFF
53 #define GE_CMD_FBP 0x9C
54 #define GE_CMD_FBW 0x9D
55 #define GE_CMD_TBP0 0xA0
56 #define GE_CMD_TBW0 0xA8
57 #define GE_CMD_TSIZE0 0xB8
58 #define GE_CMD_TFLUSH 0xCB
59 #define GE_CMD_CLEAR 0xD3
60 #define GE_CMD_VTYPE 0x12
61 #define GE_CMD_BASE 0x10
62 #define GE_CMD_VADDR 0x01
63 #define GE_CMD_IADDR 0x02
64 #define GE_CMD_PRIM 0x04
65 #define GE_CMD_FINISH 0x0F
66 #define GE_CMD_SIGNAL 0x0C
67 #define GE_CMD_NOP 0x00
69 #define GE_CMD(cmd, operand) \
70 *ge_cmd_ptr = (((GE_CMD_##cmd) << 24) | (operand)); \
73 static u16 *screen_texture = (u16 *)(0x4000000 + (512 * 272 * 2));
74 static u16 *current_screen_texture = (u16 *)(0x4000000 + (512 * 272 * 2));
75 static u16 *screen_pixels = (u16 *)(0x4000000 + (512 * 272 * 2));
76 static u32 screen_pitch = 240;
78 static void Ge_Finish_Callback(int id, void *arg)
82 #define get_screen_pixels() \
85 #define get_screen_pitch() \
92 SDL_Surface *hw_screen;
95 const u32 video_scale = 1;
96 extern void gp2x_flush_cache(void *beginning_addr, void *end_addr, int flags);
98 #define get_screen_pixels() \
99 ((u16 *)screen->pixels) \
101 #define get_screen_pitch() \
102 (screen->pitch / 2) \
106 void render_scanline_conditional_tile(u32 start, u32 end, u16 *scanline,
107 u32 enable_flags, u32 dispcnt, u32 bldcnt, tile_layer_render_struct
109 void render_scanline_conditional_bitmap(u32 start, u32 end, u16 *scanline,
110 u32 enable_flags, u32 dispcnt, u32 bldcnt, bitmap_layer_render_struct
115 // This old version is not necessary if the palette is either being converted
116 // transparently or the ABGR 1555 format is being used natively. The direct
117 // version (without conversion) is much faster.
119 #define tile_lookup_palette_full(palette, source) \
120 current_pixel = palette[source]; \
121 convert_palette(current_pixel) \
123 #define tile_lookup_palette(palette, source) \
124 current_pixel = palette[source]; \
127 #ifdef RENDER_COLOR16_NORMAL
129 #define tile_expand_base_normal(index) \
130 tile_expand_base_color16(index) \
134 #define tile_expand_base_normal(index) \
135 tile_lookup_palette(palette, current_pixel); \
136 dest_ptr[index] = current_pixel \
140 #define tile_expand_transparent_normal(index) \
141 tile_expand_base_normal(index) \
143 #define tile_expand_copy(index) \
144 dest_ptr[index] = copy_ptr[index] \
147 #define advance_dest_ptr_base(delta) \
150 #define advance_dest_ptr_transparent(delta) \
151 advance_dest_ptr_base(delta) \
153 #define advance_dest_ptr_copy(delta) \
154 advance_dest_ptr_base(delta); \
158 #define color_combine_mask_a(layer) \
159 ((io_registers[REG_BLDCNT] >> layer) & 0x01) \
161 // For color blending operations, will create a mask that has in bit
162 // 10 if the layer is target B, and bit 9 if the layer is target A.
164 #define color_combine_mask(layer) \
165 (color_combine_mask_a(layer) | \
166 ((io_registers[REG_BLDCNT] >> (layer + 7)) & 0x02)) << 9 \
168 // For alpha blending renderers, draw the palette index (9bpp) and
169 // layer bits rather than the raw RGB. For the base this should write to
170 // the 32bit location directly.
172 #define tile_expand_base_alpha(index) \
173 dest_ptr[index] = current_pixel | pixel_combine \
175 #define tile_expand_base_bg(index) \
176 dest_ptr[index] = bg_combine \
179 // For layered (transparent) writes this should shift the "stack" and write
180 // to the bottom. This will preserve the topmost pixel and the most recent
183 #define tile_expand_transparent_alpha(index) \
184 dest_ptr[index] = (dest_ptr[index] << 16) | current_pixel | pixel_combine \
187 // OBJ should only shift if the top isn't already OBJ
188 #define tile_expand_transparent_alpha_obj(index) \
189 dest = dest_ptr[index]; \
190 if(dest & 0x00000100) \
192 dest_ptr[index] = (dest & 0xFFFF0000) | current_pixel | pixel_combine; \
196 dest_ptr[index] = (dest << 16) | current_pixel | pixel_combine; \
200 // For color effects that don't need to preserve the previous layer.
201 // The color32 version should be used with 32bit wide dest_ptr so as to be
202 // compatible with alpha combine on top of it.
204 #define tile_expand_base_color16(index) \
205 dest_ptr[index] = current_pixel | pixel_combine \
207 #define tile_expand_transparent_color16(index) \
208 tile_expand_base_color16(index) \
210 #define tile_expand_base_color32(index) \
211 tile_expand_base_color16(index) \
213 #define tile_expand_transparent_color32(index) \
214 tile_expand_base_color16(index) \
217 // Operations for isolation 8bpp pixels within 32bpp pixel blocks.
219 #define tile_8bpp_pixel_op_mask(op_param) \
220 current_pixel = current_pixels & 0xFF \
222 #define tile_8bpp_pixel_op_shift_mask(shift) \
223 current_pixel = (current_pixels >> shift) & 0xFF \
225 #define tile_8bpp_pixel_op_shift(shift) \
226 current_pixel = current_pixels >> shift \
228 #define tile_8bpp_pixel_op_none(shift) \
230 // Base should always draw raw in 8bpp mode; color 0 will be drawn where
233 #define tile_8bpp_draw_base_normal(index) \
234 tile_expand_base_normal(index) \
236 #define tile_8bpp_draw_base_alpha(index) \
239 tile_expand_base_alpha(index); \
243 tile_expand_base_bg(index); \
247 #define tile_8bpp_draw_base_color16(index) \
248 tile_8bpp_draw_base_alpha(index) \
250 #define tile_8bpp_draw_base_color32(index) \
251 tile_8bpp_draw_base_alpha(index) \
254 #define tile_8bpp_draw_base(index, op, op_param, alpha_op) \
255 tile_8bpp_pixel_op_##op(op_param); \
256 tile_8bpp_draw_base_##alpha_op(index) \
258 // Transparent (layered) writes should only replace what is there if the
259 // pixel is not transparent (zero)
261 #define tile_8bpp_draw_transparent(index, op, op_param, alpha_op) \
262 tile_8bpp_pixel_op_##op(op_param); \
265 tile_expand_transparent_##alpha_op(index); \
268 #define tile_8bpp_draw_copy(index, op, op_param, alpha_op) \
269 tile_8bpp_pixel_op_##op(op_param); \
272 tile_expand_copy(index); \
275 // Get the current tile from the map in 8bpp mode
277 #define get_tile_8bpp() \
278 current_tile = *map_ptr; \
279 tile_ptr = tile_base + ((current_tile & 0x3FF) * 64) \
282 // Draw half of a tile in 8bpp mode, for base renderer
284 #define tile_8bpp_draw_four_noflip(index, combine_op, alpha_op) \
285 tile_8bpp_draw_##combine_op(index + 0, mask, 0, alpha_op); \
286 tile_8bpp_draw_##combine_op(index + 1, shift_mask, 8, alpha_op); \
287 tile_8bpp_draw_##combine_op(index + 2, shift_mask, 16, alpha_op); \
288 tile_8bpp_draw_##combine_op(index + 3, shift, 24, alpha_op) \
291 // Like the above, but draws the half-tile horizontally flipped
293 #define tile_8bpp_draw_four_flip(index, combine_op, alpha_op) \
294 tile_8bpp_draw_##combine_op(index + 3, mask, 0, alpha_op); \
295 tile_8bpp_draw_##combine_op(index + 2, shift_mask, 8, alpha_op); \
296 tile_8bpp_draw_##combine_op(index + 1, shift_mask, 16, alpha_op); \
297 tile_8bpp_draw_##combine_op(index + 0, shift, 24, alpha_op) \
299 #define tile_8bpp_draw_four_base(index, alpha_op, flip_op) \
300 tile_8bpp_draw_four_##flip_op(index, base, alpha_op) \
303 // Draw half of a tile in 8bpp mode, for transparent renderer; as an
304 // optimization the entire thing is checked against zero (in transparent
305 // capable renders it is more likely for the pixels to be transparent than
308 #define tile_8bpp_draw_four_transparent(index, alpha_op, flip_op) \
309 if(current_pixels != 0) \
311 tile_8bpp_draw_four_##flip_op(index, transparent, alpha_op); \
314 #define tile_8bpp_draw_four_copy(index, alpha_op, flip_op) \
315 if(current_pixels != 0) \
317 tile_8bpp_draw_four_##flip_op(index, copy, alpha_op); \
320 // Helper macro for drawing 8bpp tiles clipped against the edge of the screen
322 #define partial_tile_8bpp(combine_op, alpha_op) \
323 for(i = 0; i < partial_tile_run; i++) \
325 tile_8bpp_draw_##combine_op(0, mask, 0, alpha_op); \
326 current_pixels >>= 8; \
327 advance_dest_ptr_##combine_op(1); \
331 // Draws 8bpp tiles clipped against the left side of the screen,
332 // partial_tile_offset indicates how much clipped in it is, partial_tile_run
333 // indicates how much it should draw.
335 #define partial_tile_right_noflip_8bpp(combine_op, alpha_op) \
336 if(partial_tile_offset >= 4) \
338 current_pixels = *((u32 *)(tile_ptr + 4)) >> \
339 ((partial_tile_offset - 4) * 8); \
340 partial_tile_8bpp(combine_op, alpha_op); \
344 partial_tile_run -= 4; \
345 current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 8); \
346 partial_tile_8bpp(combine_op, alpha_op); \
347 current_pixels = *((u32 *)(tile_ptr + 4)); \
348 tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
349 advance_dest_ptr_##combine_op(4); \
353 // Draws 8bpp tiles clipped against both the left and right side of the
354 // screen, IE, runs of less than 8 - partial_tile_offset.
356 #define partial_tile_mid_noflip_8bpp(combine_op, alpha_op) \
357 if(partial_tile_offset >= 4) \
359 current_pixels = *((u32 *)(tile_ptr + 4)) >> \
360 ((partial_tile_offset - 4) * 8); \
361 partial_tile_8bpp(combine_op, alpha_op); \
365 current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 8); \
366 if((partial_tile_offset + partial_tile_run) > 4) \
368 u32 old_run = partial_tile_run; \
369 partial_tile_run = 4 - partial_tile_offset; \
370 partial_tile_8bpp(combine_op, alpha_op); \
371 partial_tile_run = old_run - partial_tile_run; \
372 current_pixels = *((u32 *)(tile_ptr + 4)); \
373 partial_tile_8bpp(combine_op, alpha_op); \
377 partial_tile_8bpp(combine_op, alpha_op); \
382 // Draws 8bpp tiles clipped against the right side of the screen,
383 // partial_tile_run indicates how much there is to draw.
385 #define partial_tile_left_noflip_8bpp(combine_op, alpha_op) \
386 if(partial_tile_run >= 4) \
388 current_pixels = *((u32 *)tile_ptr); \
389 tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
390 advance_dest_ptr_##combine_op(4); \
392 partial_tile_run -= 4; \
395 current_pixels = *((u32 *)(tile_ptr)); \
396 partial_tile_8bpp(combine_op, alpha_op) \
399 // Draws a non-clipped (complete) 8bpp tile.
401 #define tile_noflip_8bpp(combine_op, alpha_op) \
402 current_pixels = *((u32 *)tile_ptr); \
403 tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
404 current_pixels = *((u32 *)(tile_ptr + 4)); \
405 tile_8bpp_draw_four_##combine_op(4, alpha_op, noflip) \
408 // Like the above versions but draws flipped tiles.
410 #define partial_tile_flip_8bpp(combine_op, alpha_op) \
411 for(i = 0; i < partial_tile_run; i++) \
413 tile_8bpp_draw_##combine_op(0, shift, 24, alpha_op); \
414 current_pixels <<= 8; \
415 advance_dest_ptr_##combine_op(1); \
418 #define partial_tile_right_flip_8bpp(combine_op, alpha_op) \
419 if(partial_tile_offset >= 4) \
421 current_pixels = *((u32 *)tile_ptr) << ((partial_tile_offset - 4) * 8); \
422 partial_tile_flip_8bpp(combine_op, alpha_op); \
426 partial_tile_run -= 4; \
427 current_pixels = *((u32 *)(tile_ptr + 4)) << \
428 ((partial_tile_offset - 4) * 8); \
429 partial_tile_flip_8bpp(combine_op, alpha_op); \
430 current_pixels = *((u32 *)tile_ptr); \
431 tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
432 advance_dest_ptr_##combine_op(4); \
435 #define partial_tile_mid_flip_8bpp(combine_op, alpha_op) \
436 if(partial_tile_offset >= 4) \
438 current_pixels = *((u32 *)tile_ptr) << ((partial_tile_offset - 4) * 8); \
439 partial_tile_flip_8bpp(combine_op, alpha_op); \
443 current_pixels = *((u32 *)(tile_ptr + 4)) << \
444 ((partial_tile_offset - 4) * 8); \
446 if((partial_tile_offset + partial_tile_run) > 4) \
448 u32 old_run = partial_tile_run; \
449 partial_tile_run = 4 - partial_tile_offset; \
450 partial_tile_flip_8bpp(combine_op, alpha_op); \
451 partial_tile_run = old_run - partial_tile_run; \
452 current_pixels = *((u32 *)(tile_ptr)); \
453 partial_tile_flip_8bpp(combine_op, alpha_op); \
457 partial_tile_flip_8bpp(combine_op, alpha_op); \
461 #define partial_tile_left_flip_8bpp(combine_op, alpha_op) \
462 if(partial_tile_run >= 4) \
464 current_pixels = *((u32 *)(tile_ptr + 4)); \
465 tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
466 advance_dest_ptr_##combine_op(4); \
468 partial_tile_run -= 4; \
471 current_pixels = *((u32 *)(tile_ptr + 4)); \
472 partial_tile_flip_8bpp(combine_op, alpha_op) \
474 #define tile_flip_8bpp(combine_op, alpha_op) \
475 current_pixels = *((u32 *)(tile_ptr + 4)); \
476 tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
477 current_pixels = *((u32 *)tile_ptr); \
478 tile_8bpp_draw_four_##combine_op(4, alpha_op, flip) \
481 // Operations for isolating 4bpp tiles in a 32bit block
483 #define tile_4bpp_pixel_op_mask(op_param) \
484 current_pixel = current_pixels & 0x0F \
486 #define tile_4bpp_pixel_op_shift_mask(shift) \
487 current_pixel = (current_pixels >> shift) & 0x0F \
489 #define tile_4bpp_pixel_op_shift(shift) \
490 current_pixel = current_pixels >> shift \
492 #define tile_4bpp_pixel_op_none(op_param) \
494 // Draws a single 4bpp pixel as base, normal renderer; checks to see if the
495 // pixel is zero because if so the current palette should not be applied.
496 // These ifs can be replaced with a lookup table, may or may not be superior
497 // this way, should be benchmarked. The lookup table would be from 0-255
498 // identity map except for multiples of 16, which would map to 0.
500 #define tile_4bpp_draw_base_normal(index) \
503 current_pixel |= current_palette; \
504 tile_expand_base_normal(index); \
508 tile_expand_base_normal(index); \
512 #define tile_4bpp_draw_base_alpha(index) \
515 current_pixel |= current_palette; \
516 tile_expand_base_alpha(index); \
520 tile_expand_base_bg(index); \
523 #define tile_4bpp_draw_base_color16(index) \
524 tile_4bpp_draw_base_alpha(index) \
526 #define tile_4bpp_draw_base_color32(index) \
527 tile_4bpp_draw_base_alpha(index) \
530 #define tile_4bpp_draw_base(index, op, op_param, alpha_op) \
531 tile_4bpp_pixel_op_##op(op_param); \
532 tile_4bpp_draw_base_##alpha_op(index) \
535 // Draws a single 4bpp pixel as layered, if not transparent.
537 #define tile_4bpp_draw_transparent(index, op, op_param, alpha_op) \
538 tile_4bpp_pixel_op_##op(op_param); \
541 current_pixel |= current_palette; \
542 tile_expand_transparent_##alpha_op(index); \
545 #define tile_4bpp_draw_copy(index, op, op_param, alpha_op) \
546 tile_4bpp_pixel_op_##op(op_param); \
549 current_pixel |= current_palette; \
550 tile_expand_copy(index); \
554 // Draws eight background pixels in transparent mode, for alpha or normal
557 #define tile_4bpp_draw_eight_base_zero(value) \
558 dest_ptr[0] = value; \
559 dest_ptr[1] = value; \
560 dest_ptr[2] = value; \
561 dest_ptr[3] = value; \
562 dest_ptr[4] = value; \
563 dest_ptr[5] = value; \
564 dest_ptr[6] = value; \
565 dest_ptr[7] = value \
568 // Draws eight background pixels for the alpha renderer, basically color zero
569 // with the background flag high.
571 #define tile_4bpp_draw_eight_base_zero_alpha() \
572 tile_4bpp_draw_eight_base_zero(bg_combine) \
574 #define tile_4bpp_draw_eight_base_zero_color16() \
575 tile_4bpp_draw_eight_base_zero_alpha() \
577 #define tile_4bpp_draw_eight_base_zero_color32() \
578 tile_4bpp_draw_eight_base_zero_alpha() \
581 // Draws eight background pixels for the normal renderer, just a bunch of
584 #ifdef RENDER_COLOR16_NORMAL
586 #define tile_4bpp_draw_eight_base_zero_normal() \
588 tile_4bpp_draw_eight_base_zero(current_pixel) \
592 #define tile_4bpp_draw_eight_base_zero_normal() \
593 current_pixel = palette[0]; \
594 tile_4bpp_draw_eight_base_zero(current_pixel) \
599 // Draws eight 4bpp pixels.
601 #define tile_4bpp_draw_eight_noflip(combine_op, alpha_op) \
602 tile_4bpp_draw_##combine_op(0, mask, 0, alpha_op); \
603 tile_4bpp_draw_##combine_op(1, shift_mask, 4, alpha_op); \
604 tile_4bpp_draw_##combine_op(2, shift_mask, 8, alpha_op); \
605 tile_4bpp_draw_##combine_op(3, shift_mask, 12, alpha_op); \
606 tile_4bpp_draw_##combine_op(4, shift_mask, 16, alpha_op); \
607 tile_4bpp_draw_##combine_op(5, shift_mask, 20, alpha_op); \
608 tile_4bpp_draw_##combine_op(6, shift_mask, 24, alpha_op); \
609 tile_4bpp_draw_##combine_op(7, shift, 28, alpha_op) \
612 // Draws eight 4bpp pixels in reverse order (for hflip).
614 #define tile_4bpp_draw_eight_flip(combine_op, alpha_op) \
615 tile_4bpp_draw_##combine_op(7, mask, 0, alpha_op); \
616 tile_4bpp_draw_##combine_op(6, shift_mask, 4, alpha_op); \
617 tile_4bpp_draw_##combine_op(5, shift_mask, 8, alpha_op); \
618 tile_4bpp_draw_##combine_op(4, shift_mask, 12, alpha_op); \
619 tile_4bpp_draw_##combine_op(3, shift_mask, 16, alpha_op); \
620 tile_4bpp_draw_##combine_op(2, shift_mask, 20, alpha_op); \
621 tile_4bpp_draw_##combine_op(1, shift_mask, 24, alpha_op); \
622 tile_4bpp_draw_##combine_op(0, shift, 28, alpha_op) \
625 // Draws eight 4bpp pixels in base mode, checks if all are zero, if so draws
626 // the appropriate background pixels.
628 #define tile_4bpp_draw_eight_base(alpha_op, flip_op) \
629 if(current_pixels != 0) \
631 tile_4bpp_draw_eight_##flip_op(base, alpha_op); \
635 tile_4bpp_draw_eight_base_zero_##alpha_op(); \
639 // Draws eight 4bpp pixels in transparent (layered) mode, checks if all are
640 // zero and if so draws nothing.
642 #define tile_4bpp_draw_eight_transparent(alpha_op, flip_op) \
643 if(current_pixels != 0) \
645 tile_4bpp_draw_eight_##flip_op(transparent, alpha_op); \
649 #define tile_4bpp_draw_eight_copy(alpha_op, flip_op) \
650 if(current_pixels != 0) \
652 tile_4bpp_draw_eight_##flip_op(copy, alpha_op); \
655 // Gets the current tile in 4bpp mode, also getting the current palette and
658 #define get_tile_4bpp() \
659 current_tile = *map_ptr; \
660 current_palette = (current_tile >> 12) << 4; \
661 tile_ptr = tile_base + ((current_tile & 0x3FF) * 32); \
664 // Helper macro for drawing clipped 4bpp tiles.
666 #define partial_tile_4bpp(combine_op, alpha_op) \
667 for(i = 0; i < partial_tile_run; i++) \
669 tile_4bpp_draw_##combine_op(0, mask, 0, alpha_op); \
670 current_pixels >>= 4; \
671 advance_dest_ptr_##combine_op(1); \
675 // Draws a 4bpp tile clipped against the left edge of the screen.
676 // partial_tile_offset is how far in it's clipped, partial_tile_run is
679 #define partial_tile_right_noflip_4bpp(combine_op, alpha_op) \
680 current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 4); \
681 partial_tile_4bpp(combine_op, alpha_op) \
684 // Draws a 4bpp tile clipped against both edges of the screen, same as right.
686 #define partial_tile_mid_noflip_4bpp(combine_op, alpha_op) \
687 partial_tile_right_noflip_4bpp(combine_op, alpha_op) \
690 // Draws a 4bpp tile clipped against the right edge of the screen.
691 // partial_tile_offset is how many to draw.
693 #define partial_tile_left_noflip_4bpp(combine_op, alpha_op) \
694 current_pixels = *((u32 *)tile_ptr); \
695 partial_tile_4bpp(combine_op, alpha_op) \
698 // Draws a complete 4bpp tile row (not clipped)
699 #define tile_noflip_4bpp(combine_op, alpha_op) \
700 current_pixels = *((u32 *)tile_ptr); \
701 tile_4bpp_draw_eight_##combine_op(alpha_op, noflip) \
704 // Like the above, but draws flipped tiles.
706 #define partial_tile_flip_4bpp(combine_op, alpha_op) \
707 for(i = 0; i < partial_tile_run; i++) \
709 tile_4bpp_draw_##combine_op(0, shift, 28, alpha_op); \
710 current_pixels <<= 4; \
711 advance_dest_ptr_##combine_op(1); \
714 #define partial_tile_right_flip_4bpp(combine_op, alpha_op) \
715 current_pixels = *((u32 *)tile_ptr) << (partial_tile_offset * 4); \
716 partial_tile_flip_4bpp(combine_op, alpha_op) \
718 #define partial_tile_mid_flip_4bpp(combine_op, alpha_op) \
719 partial_tile_right_flip_4bpp(combine_op, alpha_op) \
721 #define partial_tile_left_flip_4bpp(combine_op, alpha_op) \
722 current_pixels = *((u32 *)tile_ptr); \
723 partial_tile_flip_4bpp(combine_op, alpha_op) \
725 #define tile_flip_4bpp(combine_op, alpha_op) \
726 current_pixels = *((u32 *)tile_ptr); \
727 tile_4bpp_draw_eight_##combine_op(alpha_op, flip) \
730 // Draws a single (partial or complete) tile from the tilemap, flipping
733 #define single_tile_map(tile_type, combine_op, color_depth, alpha_op) \
734 get_tile_##color_depth(); \
735 if(current_tile & 0x800) \
736 tile_ptr += vertical_pixel_flip; \
738 if(current_tile & 0x400) \
740 tile_type##_flip_##color_depth(combine_op, alpha_op); \
744 tile_type##_noflip_##color_depth(combine_op, alpha_op); \
748 // Draws multiple sequential tiles from the tilemap, hflips and vflips as
751 #define multiple_tile_map(combine_op, color_depth, alpha_op) \
752 for(i = 0; i < tile_run; i++) \
754 single_tile_map(tile, combine_op, color_depth, alpha_op); \
755 advance_dest_ptr_##combine_op(8); \
759 // Draws a partial tile from a tilemap clipped against the left edge of the
762 #define partial_tile_right_map(combine_op, color_depth, alpha_op) \
763 single_tile_map(partial_tile_right, combine_op, color_depth, alpha_op); \
766 // Draws a partial tile from a tilemap clipped against both edges of the
769 #define partial_tile_mid_map(combine_op, color_depth, alpha_op) \
770 single_tile_map(partial_tile_mid, combine_op, color_depth, alpha_op) \
772 // Draws a partial tile from a tilemap clipped against the right edge of the
775 #define partial_tile_left_map(combine_op, color_depth, alpha_op) \
776 single_tile_map(partial_tile_left, combine_op, color_depth, alpha_op) \
779 // Advances a non-flipped 4bpp obj to the next tile.
781 #define obj_advance_noflip_4bpp() \
785 // Advances a non-flipped 8bpp obj to the next tile.
787 #define obj_advance_noflip_8bpp() \
791 // Advances a flipped 4bpp obj to the next tile.
793 #define obj_advance_flip_4bpp() \
797 // Advances a flipped 8bpp obj to the next tile.
799 #define obj_advance_flip_8bpp() \
804 // Draws multiple sequential tiles from an obj, flip_op determines if it should
805 // be flipped or not (set to flip or noflip)
807 #define multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op) \
808 for(i = 0; i < tile_run; i++) \
810 tile_##flip_op##_##color_depth(combine_op, alpha_op); \
811 obj_advance_##flip_op##_##color_depth(); \
812 advance_dest_ptr_##combine_op(8); \
816 // Draws an obj's tile clipped against the left side of the screen
818 #define partial_tile_right_obj(combine_op, color_depth, alpha_op, flip_op) \
819 partial_tile_right_##flip_op##_##color_depth(combine_op, alpha_op); \
820 obj_advance_##flip_op##_##color_depth() \
822 // Draws an obj's tile clipped against both sides of the screen
824 #define partial_tile_mid_obj(combine_op, color_depth, alpha_op, flip_op) \
825 partial_tile_mid_##flip_op##_##color_depth(combine_op, alpha_op) \
827 // Draws an obj's tile clipped against the right side of the screen
829 #define partial_tile_left_obj(combine_op, color_depth, alpha_op, flip_op) \
830 partial_tile_left_##flip_op##_##color_depth(combine_op, alpha_op) \
833 // Extra variables specific for 8bpp/4bpp tile renderers.
835 #define tile_extra_variables_8bpp() \
837 #define tile_extra_variables_4bpp() \
838 u32 current_palette \
841 // Byte lengths of complete tiles and tile rows in 4bpp and 8bpp.
843 #define tile_width_4bpp 4
844 #define tile_size_4bpp 32
845 #define tile_width_8bpp 8
846 #define tile_size_8bpp 64
849 // Render a single scanline of text tiles
851 #define tile_render(color_depth, combine_op, alpha_op) \
853 u32 vertical_pixel_offset = (vertical_offset % 8) * \
854 tile_width_##color_depth; \
855 u32 vertical_pixel_flip = \
856 ((tile_size_##color_depth - tile_width_##color_depth) - \
857 vertical_pixel_offset) - vertical_pixel_offset; \
858 tile_extra_variables_##color_depth(); \
859 u8 *tile_base = vram + (((bg_control >> 2) & 0x03) * (1024 * 16)) + \
860 vertical_pixel_offset; \
861 u32 pixel_run = 256 - (horizontal_offset % 256); \
864 map_base += ((vertical_offset % 256) / 8) * 32; \
865 partial_tile_offset = (horizontal_offset % 8); \
867 if(pixel_run >= end) \
869 if(partial_tile_offset) \
871 partial_tile_run = 8 - partial_tile_offset; \
872 if(end < partial_tile_run) \
874 partial_tile_run = end; \
875 partial_tile_mid_map(combine_op, color_depth, alpha_op); \
880 end -= partial_tile_run; \
881 partial_tile_right_map(combine_op, color_depth, alpha_op); \
885 tile_run = end / 8; \
886 multiple_tile_map(combine_op, color_depth, alpha_op); \
888 partial_tile_run = end % 8; \
890 if(partial_tile_run) \
892 partial_tile_left_map(combine_op, color_depth, alpha_op); \
897 if(partial_tile_offset) \
899 partial_tile_run = 8 - partial_tile_offset; \
900 partial_tile_right_map(combine_op, color_depth, alpha_op); \
903 tile_run = (pixel_run - partial_tile_run) / 8; \
904 multiple_tile_map(combine_op, color_depth, alpha_op); \
905 map_ptr = second_ptr; \
907 tile_run = end / 8; \
908 multiple_tile_map(combine_op, color_depth, alpha_op); \
910 partial_tile_run = end % 8; \
911 if(partial_tile_run) \
913 partial_tile_left_map(combine_op, color_depth, alpha_op); \
918 #define render_scanline_dest_normal u16
919 #define render_scanline_dest_alpha u32
920 #define render_scanline_dest_alpha_obj u32
921 #define render_scanline_dest_color16 u16
922 #define render_scanline_dest_color32 u32
923 #define render_scanline_dest_partial_alpha u32
924 #define render_scanline_dest_copy_tile u16
925 #define render_scanline_dest_copy_bitmap u16
928 // If rendering a scanline that is not a target A then there's no point in
929 // keeping what's underneath it because it can't blend with it.
931 #define render_scanline_skip_alpha(bg_type, combine_op) \
932 if((pixel_combine & 0x00000200) == 0) \
934 render_scanline_##bg_type##_##combine_op##_color32(layer, \
935 start, end, scanline); \
940 #ifdef RENDER_COLOR16_NORMAL
942 #define render_scanline_extra_variables_base_normal(bg_type) \
943 const u32 pixel_combine = 0 \
947 #define render_scanline_extra_variables_base_normal(bg_type) \
948 u16 *palette = palette_ram_converted \
953 #define render_scanline_extra_variables_base_alpha(bg_type) \
954 u32 bg_combine = color_combine_mask(5); \
955 u32 pixel_combine = color_combine_mask(layer) | (bg_combine << 16); \
956 render_scanline_skip_alpha(bg_type, base) \
958 #define render_scanline_extra_variables_base_color() \
959 u32 bg_combine = color_combine_mask(5); \
960 u32 pixel_combine = color_combine_mask(layer) \
962 #define render_scanline_extra_variables_base_color16(bg_type) \
963 render_scanline_extra_variables_base_color() \
965 #define render_scanline_extra_variables_base_color32(bg_type) \
966 render_scanline_extra_variables_base_color() \
969 #define render_scanline_extra_variables_transparent_normal(bg_type) \
970 render_scanline_extra_variables_base_normal(bg_type) \
972 #define render_scanline_extra_variables_transparent_alpha(bg_type) \
973 u32 pixel_combine = color_combine_mask(layer); \
974 render_scanline_skip_alpha(bg_type, transparent) \
976 #define render_scanline_extra_variables_transparent_color() \
977 u32 pixel_combine = color_combine_mask(layer) \
979 #define render_scanline_extra_variables_transparent_color16(bg_type) \
980 render_scanline_extra_variables_transparent_color() \
982 #define render_scanline_extra_variables_transparent_color32(bg_type) \
983 render_scanline_extra_variables_transparent_color() \
989 // Map widths and heights
991 u32 map_widths[] = { 256, 512, 256, 512 };
992 u32 map_heights[] = { 256, 256, 512, 512 };
994 // Build text scanline rendering functions.
996 #define render_scanline_text_builder(combine_op, alpha_op) \
997 void render_scanline_text_##combine_op##_##alpha_op(u32 layer, \
998 u32 start, u32 end, void *scanline) \
1000 render_scanline_extra_variables_##combine_op##_##alpha_op(text); \
1001 u32 bg_control = io_registers[REG_BG0CNT + layer]; \
1002 u32 map_size = (bg_control >> 14) & 0x03; \
1003 u32 map_width = map_widths[map_size]; \
1004 u32 map_height = map_heights[map_size]; \
1005 u32 horizontal_offset = \
1006 (io_registers[REG_BG0HOFS + (layer * 2)] + start) % 512; \
1007 u32 vertical_offset = (io_registers[REG_VCOUNT] + \
1008 io_registers[REG_BG0VOFS + (layer * 2)]) % 512; \
1009 u32 current_pixel; \
1010 u32 current_pixels; \
1011 u32 partial_tile_run = 0; \
1012 u32 partial_tile_offset; \
1015 render_scanline_dest_##alpha_op *dest_ptr = \
1016 ((render_scanline_dest_##alpha_op *)scanline) + start; \
1018 u16 *map_base = (u16 *)(vram + ((bg_control >> 8) & 0x1F) * (1024 * 2)); \
1019 u16 *map_ptr, *second_ptr; \
1024 if((map_size & 0x02) && (vertical_offset >= 256)) \
1026 map_base += ((map_width / 8) * 32) + \
1027 (((vertical_offset - 256) / 8) * 32); \
1031 map_base += (((vertical_offset % 256) / 8) * 32); \
1034 if(map_size & 0x01) \
1036 if(horizontal_offset >= 256) \
1038 horizontal_offset -= 256; \
1039 map_ptr = map_base + (32 * 32) + (horizontal_offset / 8); \
1040 second_ptr = map_base; \
1044 map_ptr = map_base + (horizontal_offset / 8); \
1045 second_ptr = map_base + (32 * 32); \
1050 horizontal_offset %= 256; \
1051 map_ptr = map_base + (horizontal_offset / 8); \
1052 second_ptr = map_base; \
1055 if(bg_control & 0x80) \
1057 tile_render(8bpp, combine_op, alpha_op); \
1061 tile_render(4bpp, combine_op, alpha_op); \
1065 render_scanline_text_builder(base, normal);
1066 render_scanline_text_builder(transparent, normal);
1067 render_scanline_text_builder(base, color16);
1068 render_scanline_text_builder(transparent, color16);
1069 render_scanline_text_builder(base, color32);
1070 render_scanline_text_builder(transparent, color32);
1071 render_scanline_text_builder(base, alpha);
1072 render_scanline_text_builder(transparent, alpha);
1075 s32 affine_reference_x[2];
1076 s32 affine_reference_y[2];
1078 #define affine_render_bg_pixel_normal() \
1079 current_pixel = palette_ram_converted[0] \
1081 #define affine_render_bg_pixel_alpha() \
1082 current_pixel = bg_combine \
1084 #define affine_render_bg_pixel_color16() \
1085 affine_render_bg_pixel_alpha() \
1087 #define affine_render_bg_pixel_color32() \
1088 affine_render_bg_pixel_alpha() \
1090 #define affine_render_bg_pixel_base(alpha_op) \
1091 affine_render_bg_pixel_##alpha_op() \
1093 #define affine_render_bg_pixel_transparent(alpha_op) \
1095 #define affine_render_bg_pixel_copy(alpha_op) \
1097 #define affine_render_bg_base(alpha_op) \
1098 dest_ptr[0] = current_pixel
1100 #define affine_render_bg_transparent(alpha_op) \
1102 #define affine_render_bg_copy(alpha_op) \
1104 #define affine_render_bg_remainder_base(alpha_op) \
1105 affine_render_bg_pixel_##alpha_op(); \
1106 for(; i < end; i++) \
1108 affine_render_bg_base(alpha_op); \
1109 advance_dest_ptr_base(1); \
1112 #define affine_render_bg_remainder_transparent(alpha_op) \
1114 #define affine_render_bg_remainder_copy(alpha_op) \
1116 #define affine_render_next(combine_op) \
1119 advance_dest_ptr_##combine_op(1) \
1121 #define affine_render_scale_offset() \
1122 tile_base += ((pixel_y % 8) * 8); \
1123 map_base += (pixel_y / 8) << map_pitch \
1125 #define affine_render_scale_pixel(combine_op, alpha_op) \
1126 map_offset = (pixel_x / 8); \
1127 if(map_offset != last_map_offset) \
1129 tile_ptr = tile_base + (map_base[map_offset] * 64); \
1130 last_map_offset = map_offset; \
1132 tile_ptr = tile_base + (map_base[(pixel_x / 8)] * 64); \
1133 current_pixel = tile_ptr[(pixel_x % 8)]; \
1134 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op); \
1135 affine_render_next(combine_op) \
1137 #define affine_render_scale(combine_op, alpha_op) \
1139 pixel_y = source_y >> 8; \
1141 affine_render_bg_pixel_##combine_op(alpha_op); \
1142 if((u32)pixel_y < (u32)width_height) \
1144 affine_render_scale_offset(); \
1145 for(; i < end; i++) \
1147 pixel_x = source_x >> 8; \
1149 if((u32)pixel_x < (u32)width_height) \
1154 affine_render_bg_##combine_op(alpha_op); \
1155 affine_render_next(combine_op); \
1158 for(; i < end; i++) \
1160 pixel_x = source_x >> 8; \
1162 if((u32)pixel_x >= (u32)width_height) \
1165 affine_render_scale_pixel(combine_op, alpha_op); \
1168 affine_render_bg_remainder_##combine_op(alpha_op); \
1171 #define affine_render_scale_wrap(combine_op, alpha_op) \
1173 u32 wrap_mask = width_height - 1; \
1174 pixel_y = (source_y >> 8) & wrap_mask; \
1175 if((u32)pixel_y < (u32)width_height) \
1177 affine_render_scale_offset(); \
1178 for(i = 0; i < end; i++) \
1180 pixel_x = (source_x >> 8) & wrap_mask; \
1181 affine_render_scale_pixel(combine_op, alpha_op); \
1187 #define affine_render_rotate_pixel(combine_op, alpha_op) \
1188 map_offset = (pixel_x / 8) + ((pixel_y / 8) << map_pitch); \
1189 if(map_offset != last_map_offset) \
1191 tile_ptr = tile_base + (map_base[map_offset] * 64); \
1192 last_map_offset = map_offset; \
1195 current_pixel = tile_ptr[(pixel_x % 8) + ((pixel_y % 8) * 8)]; \
1196 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op); \
1197 affine_render_next(combine_op) \
1199 #define affine_render_rotate(combine_op, alpha_op) \
1201 affine_render_bg_pixel_##combine_op(alpha_op); \
1202 for(i = 0; i < end; i++) \
1204 pixel_x = source_x >> 8; \
1205 pixel_y = source_y >> 8; \
1207 if(((u32)pixel_x < (u32)width_height) && \
1208 ((u32)pixel_y < (u32)width_height)) \
1212 affine_render_bg_##combine_op(alpha_op); \
1213 affine_render_next(combine_op); \
1216 for(; i < end; i++) \
1218 pixel_x = source_x >> 8; \
1219 pixel_y = source_y >> 8; \
1221 if(((u32)pixel_x >= (u32)width_height) || \
1222 ((u32)pixel_y >= (u32)width_height)) \
1224 affine_render_bg_remainder_##combine_op(alpha_op); \
1228 affine_render_rotate_pixel(combine_op, alpha_op); \
1232 #define affine_render_rotate_wrap(combine_op, alpha_op) \
1234 u32 wrap_mask = width_height - 1; \
1235 for(i = 0; i < end; i++) \
1237 pixel_x = (source_x >> 8) & wrap_mask; \
1238 pixel_y = (source_y >> 8) & wrap_mask; \
1240 affine_render_rotate_pixel(combine_op, alpha_op); \
1245 // Build affine background renderers.
1247 #define render_scanline_affine_builder(combine_op, alpha_op) \
1248 void render_scanline_affine_##combine_op##_##alpha_op(u32 layer, \
1249 u32 start, u32 end, void *scanline) \
1251 render_scanline_extra_variables_##combine_op##_##alpha_op(affine); \
1252 u32 bg_control = io_registers[REG_BG0CNT + layer]; \
1253 u32 current_pixel; \
1254 s32 source_x, source_y; \
1255 u32 vcount = io_registers[REG_VCOUNT]; \
1256 u32 pixel_x, pixel_y; \
1257 u32 layer_offset = (layer - 2) * 8; \
1259 u32 map_size = (bg_control >> 14) & 0x03; \
1260 u32 width_height = 1 << (7 + map_size); \
1261 u32 map_pitch = map_size + 4; \
1262 u8 *map_base = vram + (((bg_control >> 8) & 0x1F) * (1024 * 2)); \
1263 u8 *tile_base = vram + (((bg_control >> 2) & 0x03) * (1024 * 16)); \
1265 u32 map_offset, last_map_offset = (u32)-1; \
1267 render_scanline_dest_##alpha_op *dest_ptr = \
1268 ((render_scanline_dest_##alpha_op *)scanline) + start; \
1270 dx = (s16)io_registers[REG_BG2PA + layer_offset]; \
1271 dy = (s16)io_registers[REG_BG2PC + layer_offset]; \
1272 source_x = affine_reference_x[layer - 2] + (start * dx); \
1273 source_y = affine_reference_y[layer - 2] + (start * dy); \
1277 switch(((bg_control >> 12) & 0x02) | (dy != 0)) \
1280 affine_render_scale(combine_op, alpha_op); \
1284 affine_render_rotate(combine_op, alpha_op); \
1288 affine_render_scale_wrap(combine_op, alpha_op); \
1292 affine_render_rotate_wrap(combine_op, alpha_op); \
1297 render_scanline_affine_builder(base, normal);
1298 render_scanline_affine_builder(transparent, normal);
1299 render_scanline_affine_builder(base, color16);
1300 render_scanline_affine_builder(transparent, color16);
1301 render_scanline_affine_builder(base, color32);
1302 render_scanline_affine_builder(transparent, color32);
1303 render_scanline_affine_builder(base, alpha);
1304 render_scanline_affine_builder(transparent, alpha);
1307 #define bitmap_render_pixel_mode3(alpha_op) \
1308 convert_palette(current_pixel); \
1309 *dest_ptr = current_pixel \
1311 #define bitmap_render_pixel_mode4(alpha_op) \
1312 tile_expand_base_##alpha_op(0) \
1314 #define bitmap_render_pixel_mode5(alpha_op) \
1315 bitmap_render_pixel_mode3(alpha_op) \
1318 #define bitmap_render_scale(type, alpha_op, width, height) \
1319 pixel_y = (source_y >> 8); \
1320 if((u32)pixel_y < (u32)height) \
1322 pixel_x = (source_x >> 8); \
1323 src_ptr += (pixel_y * width); \
1329 dest_ptr -= pixel_x; \
1336 src_ptr += pixel_x; \
1339 if((pixel_x + end) >= width) \
1340 end = (width - pixel_x); \
1342 for(i = 0; (s32)i < (s32)end; i++) \
1344 current_pixel = *src_ptr; \
1345 bitmap_render_pixel_##type(alpha_op); \
1352 if((u32)(source_y >> 8) < (u32)height) \
1354 for(i = 0; i < end; i++) \
1356 pixel_x = (source_x >> 8); \
1358 if((u32)pixel_x < (u32)width) \
1365 for(; i < end; i++) \
1367 pixel_x = (source_x >> 8); \
1369 if((u32)pixel_x >= (u32)width) \
1372 current_pixel = src_ptr[pixel_x]; \
1373 bitmap_render_pixel_##type(alpha_op); \
1382 #define bitmap_render_rotate(type, alpha_op, width, height) \
1383 for(i = 0; i < end; i++) \
1385 pixel_x = source_x >> 8; \
1386 pixel_y = source_y >> 8; \
1388 if(((u32)pixel_x < (u32)width) && ((u32)pixel_y < (u32)height)) \
1396 for(; i < end; i++) \
1398 pixel_x = (source_x >> 8); \
1399 pixel_y = (source_y >> 8); \
1401 if(((u32)pixel_x >= (u32)width) || ((u32)pixel_y >= (u32)height)) \
1404 current_pixel = src_ptr[pixel_x + (pixel_y * width)]; \
1405 bitmap_render_pixel_##type(alpha_op); \
1413 #define render_scanline_vram_setup_mode3() \
1414 u16 *src_ptr = (u16 *)vram \
1416 #define render_scanline_vram_setup_mode5() \
1418 if(io_registers[REG_DISPCNT] & 0x10) \
1419 src_ptr = (u16 *)(vram + 0xA000); \
1421 src_ptr = (u16 *)vram \
1424 #ifdef RENDER_COLOR16_NORMAL
1426 #define render_scanline_vram_setup_mode4() \
1427 const u32 pixel_combine = 0; \
1429 if(io_registers[REG_DISPCNT] & 0x10) \
1430 src_ptr = vram + 0xA000; \
1437 #define render_scanline_vram_setup_mode4() \
1438 u16 *palette = palette_ram_converted; \
1440 if(io_registers[REG_DISPCNT] & 0x10) \
1441 src_ptr = vram + 0xA000; \
1449 // Build bitmap scanline rendering functions.
1451 #define render_scanline_bitmap_builder(type, alpha_op, width, height) \
1452 void render_scanline_bitmap_##type##_##alpha_op(u32 start, u32 end, \
1455 u32 bg_control = io_registers[REG_BG2CNT]; \
1456 u32 current_pixel; \
1457 s32 source_x, source_y; \
1458 u32 vcount = io_registers[REG_VCOUNT]; \
1459 s32 pixel_x, pixel_y; \
1461 s32 dx = (s16)io_registers[REG_BG2PA]; \
1462 s32 dy = (s16)io_registers[REG_BG2PC]; \
1466 render_scanline_dest_##alpha_op *dest_ptr = \
1467 ((render_scanline_dest_##alpha_op *)scanline) + start; \
1468 render_scanline_vram_setup_##type(); \
1472 source_x = affine_reference_x[0] + (start * dx); \
1473 source_y = affine_reference_y[0] + (start * dy); \
1477 bitmap_render_scale(type, alpha_op, width, height); \
1481 bitmap_render_rotate(type, alpha_op, width, height); \
1485 render_scanline_bitmap_builder(mode3, normal, 240, 160);
1486 render_scanline_bitmap_builder(mode4, normal, 240, 160);
1487 render_scanline_bitmap_builder(mode5, normal, 160, 128);
1490 // Fill in the renderers for a layer based on the mode type,
1492 #define tile_layer_render_functions(type) \
1494 render_scanline_##type##_base_normal, \
1495 render_scanline_##type##_transparent_normal, \
1496 render_scanline_##type##_base_alpha, \
1497 render_scanline_##type##_transparent_alpha, \
1498 render_scanline_##type##_base_color16, \
1499 render_scanline_##type##_transparent_color16, \
1500 render_scanline_##type##_base_color32, \
1501 render_scanline_##type##_transparent_color32 \
1505 // Use if a layer is unsupported for that mode.
1507 #define tile_layer_render_null() \
1509 NULL, NULL, NULL, NULL \
1512 #define bitmap_layer_render_functions(type) \
1514 render_scanline_bitmap_##type##_normal \
1517 // Structs containing functions to render the layers for each mode, for
1518 // each render type.
1519 tile_layer_render_struct tile_mode_renderers[3][4] =
1522 tile_layer_render_functions(text), tile_layer_render_functions(text),
1523 tile_layer_render_functions(text), tile_layer_render_functions(text)
1526 tile_layer_render_functions(text), tile_layer_render_functions(text),
1527 tile_layer_render_functions(affine), tile_layer_render_functions(text)
1530 tile_layer_render_functions(text), tile_layer_render_functions(text),
1531 tile_layer_render_functions(affine), tile_layer_render_functions(affine)
1535 bitmap_layer_render_struct bitmap_mode_renderers[3] =
1537 bitmap_layer_render_functions(mode3),
1538 bitmap_layer_render_functions(mode4),
1539 bitmap_layer_render_functions(mode5)
1543 #define render_scanline_layer_functions_tile() \
1544 tile_layer_render_struct *layer_renderers = \
1545 tile_mode_renderers[dispcnt & 0x07] \
1547 #define render_scanline_layer_functions_bitmap() \
1548 bitmap_layer_render_struct *layer_renderers = \
1549 bitmap_mode_renderers + ((dispcnt & 0x07) - 3) \
1552 // Adjust a flipped obj's starting position
1554 #define obj_tile_offset_noflip(color_depth) \
1556 #define obj_tile_offset_flip(color_depth) \
1557 + (tile_size_##color_depth * ((obj_width - 8) / 8)) \
1560 // Adjust the obj's starting point if it goes too far off the left edge of \
1563 #define obj_tile_right_offset_noflip(color_depth) \
1564 tile_ptr += (partial_tile_offset / 8) * tile_size_##color_depth \
1566 #define obj_tile_right_offset_flip(color_depth) \
1567 tile_ptr -= (partial_tile_offset / 8) * tile_size_##color_depth \
1569 // Get the current row offset into an obj in 1D map space
1571 #define obj_tile_offset_1D(color_depth, flip_op) \
1572 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1573 + ((vertical_offset / 8) * (obj_width / 8) * tile_size_##color_depth) \
1574 + ((vertical_offset % 8) * tile_width_##color_depth) \
1575 obj_tile_offset_##flip_op(color_depth) \
1577 // Get the current row offset into an obj in 2D map space
1579 #define obj_tile_offset_2D(color_depth, flip_op) \
1580 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1581 + ((vertical_offset / 8) * 1024) \
1582 + ((vertical_offset % 8) * tile_width_##color_depth) \
1583 obj_tile_offset_##flip_op(color_depth) \
1586 // Get the palette for 4bpp obj.
1588 #define obj_get_palette_4bpp() \
1589 current_palette = (obj_attribute_2 >> 8) & 0xF0 \
1591 #define obj_get_palette_8bpp() \
1594 // Render the current row of an obj.
1596 #define obj_render(combine_op, color_depth, alpha_op, map_space, flip_op) \
1598 obj_get_palette_##color_depth(); \
1599 obj_tile_offset_##map_space(color_depth, flip_op); \
1601 if(obj_x < (s32)start) \
1603 dest_ptr = scanline + start; \
1604 pixel_run = obj_width - (start - obj_x); \
1605 if((s32)pixel_run > 0) \
1607 if((obj_x + obj_width) >= end) \
1609 pixel_run = end - start; \
1610 partial_tile_offset = start - obj_x; \
1611 obj_tile_right_offset_##flip_op(color_depth); \
1612 partial_tile_offset %= 8; \
1614 if(partial_tile_offset) \
1616 partial_tile_run = 8 - partial_tile_offset; \
1617 if((s32)pixel_run < (s32)partial_tile_run) \
1619 if((s32)pixel_run > 0) \
1621 partial_tile_run = pixel_run; \
1622 partial_tile_mid_obj(combine_op, color_depth, alpha_op, \
1629 pixel_run -= partial_tile_run; \
1630 partial_tile_right_obj(combine_op, color_depth, alpha_op, \
1634 tile_run = pixel_run / 8; \
1635 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1636 partial_tile_run = pixel_run % 8; \
1637 if(partial_tile_run) \
1639 partial_tile_left_obj(combine_op, color_depth, alpha_op, \
1645 partial_tile_offset = start - obj_x; \
1646 obj_tile_right_offset_##flip_op(color_depth); \
1647 partial_tile_offset %= 8; \
1648 if(partial_tile_offset) \
1650 partial_tile_run = 8 - partial_tile_offset; \
1651 partial_tile_right_obj(combine_op, color_depth, alpha_op, \
1654 tile_run = pixel_run / 8; \
1655 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1661 if((obj_x + obj_width) >= end) \
1663 pixel_run = end - obj_x; \
1664 if((s32)pixel_run > 0) \
1666 dest_ptr = scanline + obj_x; \
1667 tile_run = pixel_run / 8; \
1668 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1669 partial_tile_run = pixel_run % 8; \
1670 if(partial_tile_run) \
1672 partial_tile_left_obj(combine_op, color_depth, alpha_op, flip_op); \
1678 dest_ptr = scanline + obj_x; \
1679 tile_run = obj_width / 8; \
1680 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1684 #define obj_scale_offset_1D(color_depth) \
1685 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1686 + ((vertical_offset / 8) * (max_x / 8) * tile_size_##color_depth) \
1687 + ((vertical_offset % 8) * tile_width_##color_depth) \
1689 // Get the current row offset into an obj in 2D map space
1691 #define obj_scale_offset_2D(color_depth) \
1692 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1693 + ((vertical_offset / 8) * 1024) \
1694 + ((vertical_offset % 8) * tile_width_##color_depth) \
1696 #define obj_render_scale_pixel_4bpp(combine_op, alpha_op) \
1699 current_pixel = tile_ptr[tile_map_offset + ((tile_x >> 1) & 0x03)] >> 4; \
1704 tile_ptr[tile_map_offset + ((tile_x >> 1) & 0x03)] & 0x0F; \
1707 tile_4bpp_draw_##combine_op(0, none, 0, alpha_op) \
1710 #define obj_render_scale_pixel_8bpp(combine_op, alpha_op) \
1711 current_pixel = tile_ptr[tile_map_offset + (tile_x & 0x07)]; \
1712 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op); \
1714 #define obj_render_scale(combine_op, color_depth, alpha_op, map_space) \
1716 u32 vertical_offset; \
1717 source_y += (y_delta * dmy); \
1718 vertical_offset = (source_y >> 8); \
1719 if((u32)vertical_offset < (u32)max_y) \
1721 obj_scale_offset_##map_space(color_depth); \
1722 source_x += (y_delta * dmx) - (middle_x * dx); \
1724 for(i = 0; i < obj_width; i++) \
1726 tile_x = (source_x >> 8); \
1728 if((u32)tile_x < (u32)max_x) \
1732 advance_dest_ptr_##combine_op(1); \
1735 for(; i < obj_width; i++) \
1737 tile_x = (source_x >> 8); \
1739 if((u32)tile_x >= (u32)max_x) \
1742 tile_map_offset = (tile_x >> 3) * tile_size_##color_depth; \
1743 obj_render_scale_pixel_##color_depth(combine_op, alpha_op); \
1746 advance_dest_ptr_##combine_op(1); \
1752 #define obj_rotate_offset_1D(color_depth) \
1753 obj_tile_pitch = (max_x / 8) * tile_size_##color_depth \
1755 #define obj_rotate_offset_2D(color_depth) \
1756 obj_tile_pitch = 1024 \
1758 #define obj_render_rotate_pixel_4bpp(combine_op, alpha_op) \
1761 current_pixel = tile_ptr[tile_map_offset + \
1762 ((tile_x >> 1) & 0x03) + ((tile_y & 0x07) * obj_pitch)] >> 4; \
1766 current_pixel = tile_ptr[tile_map_offset + \
1767 ((tile_x >> 1) & 0x03) + ((tile_y & 0x07) * obj_pitch)] & 0x0F; \
1770 tile_4bpp_draw_##combine_op(0, none, 0, alpha_op) \
1772 #define obj_render_rotate_pixel_8bpp(combine_op, alpha_op) \
1773 current_pixel = tile_ptr[tile_map_offset + \
1774 (tile_x & 0x07) + ((tile_y & 0x07) * obj_pitch)]; \
1776 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op) \
1778 #define obj_render_rotate(combine_op, color_depth, alpha_op, map_space) \
1780 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32); \
1781 obj_rotate_offset_##map_space(color_depth); \
1783 source_x += (y_delta * dmx) - (middle_x * dx); \
1784 source_y += (y_delta * dmy) - (middle_x * dy); \
1786 for(i = 0; i < obj_width; i++) \
1788 tile_x = (source_x >> 8); \
1789 tile_y = (source_y >> 8); \
1791 if(((u32)tile_x < (u32)max_x) && ((u32)tile_y < (u32)max_y)) \
1796 advance_dest_ptr_##combine_op(1); \
1799 for(; i < obj_width; i++) \
1801 tile_x = (source_x >> 8); \
1802 tile_y = (source_y >> 8); \
1804 if(((u32)tile_x >= (u32)max_x) || ((u32)tile_y >= (u32)max_y)) \
1807 tile_map_offset = ((tile_x >> 3) * tile_size_##color_depth) + \
1808 ((tile_y >> 3) * obj_tile_pitch); \
1809 obj_render_rotate_pixel_##color_depth(combine_op, alpha_op); \
1813 advance_dest_ptr_##combine_op(1); \
1817 // Render the current row of an affine transformed OBJ.
1819 #define obj_render_affine(combine_op, color_depth, alpha_op, map_space) \
1821 s16 *params = oam_ram + (((obj_attribute_1 >> 9) & 0x1F) * 16); \
1822 s32 dx = params[3]; \
1823 s32 dmx = params[7]; \
1824 s32 dy = params[11]; \
1825 s32 dmy = params[15]; \
1826 s32 source_x, source_y; \
1827 s32 tile_x, tile_y; \
1829 u32 tile_map_offset; \
1832 s32 max_x = obj_width; \
1833 s32 max_y = obj_height; \
1835 u32 obj_pitch = tile_width_##color_depth; \
1836 u32 obj_tile_pitch; \
1838 middle_x = (obj_width / 2); \
1839 middle_y = (obj_height / 2); \
1841 source_x = (middle_x << 8); \
1842 source_y = (middle_y << 8); \
1845 if(obj_attribute_0 & 0x200) \
1853 if((s32)obj_x < (s32)start) \
1855 u32 x_delta = start - obj_x; \
1856 middle_x -= x_delta; \
1857 obj_width -= x_delta; \
1860 if((s32)obj_width <= 0) \
1864 if((s32)(obj_x + obj_width) >= (s32)end) \
1866 obj_width = end - obj_x; \
1868 if((s32)obj_width <= 0) \
1871 dest_ptr = scanline + obj_x; \
1873 y_delta = vcount - (obj_y + middle_y); \
1875 obj_get_palette_##color_depth(); \
1879 obj_render_scale(combine_op, color_depth, alpha_op, map_space); \
1883 obj_render_rotate(combine_op, color_depth, alpha_op, map_space); \
1887 u32 obj_width_table[] = { 8, 16, 32, 64, 16, 32, 32, 64, 8, 8, 16, 32 };
1888 u32 obj_height_table[] = { 8, 16, 32, 64, 8, 8, 16, 32, 16, 32, 32, 64 };
1890 u8 obj_priority_list[5][160][128];
1891 u32 obj_priority_count[5][160];
1892 u32 obj_alpha_count[160];
1895 // Build obj rendering functions
1897 #ifdef RENDER_COLOR16_NORMAL
1899 #define render_scanline_obj_extra_variables_normal(bg_type) \
1900 const u32 pixel_combine = (1 << 8) \
1904 #define render_scanline_obj_extra_variables_normal(bg_type) \
1905 u16 *palette = palette_ram_converted + 256 \
1910 #define render_scanline_obj_extra_variables_color() \
1912 u32 pixel_combine = color_combine_mask(4) | (1 << 8) \
1914 #define render_scanline_obj_extra_variables_alpha_obj(map_space) \
1915 render_scanline_obj_extra_variables_color(); \
1916 if((pixel_combine & 0x00000200) == 0) \
1918 render_scanline_obj_color32_##map_space(priority, start, end, scanline); \
1922 #define render_scanline_obj_extra_variables_color16(map_space) \
1923 render_scanline_obj_extra_variables_color() \
1925 #define render_scanline_obj_extra_variables_color32(map_space) \
1926 render_scanline_obj_extra_variables_color() \
1928 #define render_scanline_obj_extra_variables_partial_alpha(map_space) \
1929 render_scanline_obj_extra_variables_color(); \
1930 u32 base_pixel_combine = pixel_combine \
1932 #define render_scanline_obj_extra_variables_copy(type) \
1933 u32 bldcnt = io_registers[REG_BLDCNT]; \
1934 u32 dispcnt = io_registers[REG_DISPCNT]; \
1935 u32 obj_enable = io_registers[REG_WINOUT] >> 8; \
1936 render_scanline_layer_functions_##type(); \
1937 u32 copy_start, copy_end; \
1938 u16 copy_buffer[240]; \
1941 #define render_scanline_obj_extra_variables_copy_tile(map_space) \
1942 render_scanline_obj_extra_variables_copy(tile) \
1944 #define render_scanline_obj_extra_variables_copy_bitmap(map_space) \
1945 render_scanline_obj_extra_variables_copy(bitmap) \
1948 #define render_scanline_obj_main(combine_op, alpha_op, map_space) \
1949 if(obj_attribute_0 & 0x100) \
1951 if((obj_attribute_0 >> 13) & 0x01) \
1953 obj_render_affine(combine_op, 8bpp, alpha_op, map_space); \
1957 obj_render_affine(combine_op, 4bpp, alpha_op, map_space); \
1962 vertical_offset = vcount - obj_y; \
1964 if((obj_attribute_1 >> 13) & 0x01) \
1965 vertical_offset = obj_height - vertical_offset - 1; \
1967 switch(((obj_attribute_0 >> 12) & 0x02) | \
1968 ((obj_attribute_1 >> 12) & 0x01)) \
1971 obj_render(combine_op, 4bpp, alpha_op, map_space, noflip); \
1975 obj_render(combine_op, 4bpp, alpha_op, map_space, flip); \
1979 obj_render(combine_op, 8bpp, alpha_op, map_space, noflip); \
1983 obj_render(combine_op, 8bpp, alpha_op, map_space, flip); \
1988 #define render_scanline_obj_no_partial_alpha(combine_op, alpha_op, map_space) \
1989 render_scanline_obj_main(combine_op, alpha_op, map_space) \
1991 #define render_scanline_obj_partial_alpha(combine_op, alpha_op, map_space) \
1992 if((obj_attribute_0 >> 10) & 0x03) \
1994 pixel_combine = 0x00000300; \
1995 render_scanline_obj_main(combine_op, alpha_obj, map_space); \
1999 pixel_combine = base_pixel_combine; \
2000 render_scanline_obj_main(combine_op, color32, map_space); \
2003 #define render_scanline_obj_prologue_transparent(alpha_op) \
2005 #define render_scanline_obj_prologue_copy_body(type) \
2006 copy_start = obj_x; \
2007 if(obj_attribute_0 & 0x200) \
2008 copy_end = obj_x + (obj_width * 2); \
2010 copy_end = obj_x + obj_width; \
2012 if(copy_start < start) \
2013 copy_start = start; \
2014 if(copy_end > end) \
2017 if((copy_start < end) && (copy_end > start)) \
2019 render_scanline_conditional_##type(copy_start, copy_end, copy_buffer, \
2020 obj_enable, dispcnt, bldcnt, layer_renderers); \
2021 copy_ptr = copy_buffer + copy_start; \
2028 #define render_scanline_obj_prologue_copy_tile() \
2029 render_scanline_obj_prologue_copy_body(tile) \
2031 #define render_scanline_obj_prologue_copy_bitmap() \
2032 render_scanline_obj_prologue_copy_body(bitmap) \
2034 #define render_scanline_obj_prologue_copy(alpha_op) \
2035 render_scanline_obj_prologue_##alpha_op() \
2038 #define render_scanline_obj_builder(combine_op, alpha_op, map_space, \
2040 void render_scanline_obj_##alpha_op##_##map_space(u32 priority, \
2041 u32 start, u32 end, render_scanline_dest_##alpha_op *scanline) \
2043 render_scanline_obj_extra_variables_##alpha_op(map_space); \
2047 s32 obj_width, obj_height; \
2048 u32 obj_attribute_0, obj_attribute_1, obj_attribute_2; \
2049 s32 vcount = io_registers[REG_VCOUNT]; \
2051 u32 current_pixels; \
2052 u32 current_pixel; \
2053 u32 current_palette; \
2054 u32 vertical_offset; \
2055 u32 partial_tile_run, partial_tile_offset; \
2058 render_scanline_dest_##alpha_op *dest_ptr; \
2059 u8 *tile_base = vram + 0x10000; \
2061 u32 obj_count = obj_priority_count[priority][vcount]; \
2062 u8 *obj_list = obj_priority_list[priority][vcount]; \
2064 for(obj_num = 0; obj_num < obj_count; obj_num++) \
2066 oam_ptr = oam_ram + (obj_list[obj_num] * 4); \
2067 obj_attribute_0 = oam_ptr[0]; \
2068 obj_attribute_1 = oam_ptr[1]; \
2069 obj_attribute_2 = oam_ptr[2]; \
2070 obj_size = ((obj_attribute_0 >> 12) & 0x0C) | (obj_attribute_1 >> 14); \
2072 obj_x = (s32)(obj_attribute_1 << 23) >> 23; \
2073 obj_width = obj_width_table[obj_size]; \
2075 render_scanline_obj_prologue_##combine_op(alpha_op); \
2077 obj_y = obj_attribute_0 & 0xFF; \
2082 obj_height = obj_height_table[obj_size]; \
2083 render_scanline_obj_##partial_alpha_op(combine_op, alpha_op, map_space); \
2087 render_scanline_obj_builder(transparent, normal, 1D, no_partial_alpha);
2088 render_scanline_obj_builder(transparent, normal, 2D, no_partial_alpha);
2089 render_scanline_obj_builder(transparent, color16, 1D, no_partial_alpha);
2090 render_scanline_obj_builder(transparent, color16, 2D, no_partial_alpha);
2091 render_scanline_obj_builder(transparent, color32, 1D, no_partial_alpha);
2092 render_scanline_obj_builder(transparent, color32, 2D, no_partial_alpha);
2093 render_scanline_obj_builder(transparent, alpha_obj, 1D, no_partial_alpha);
2094 render_scanline_obj_builder(transparent, alpha_obj, 2D, no_partial_alpha);
2095 render_scanline_obj_builder(transparent, partial_alpha, 1D, partial_alpha);
2096 render_scanline_obj_builder(transparent, partial_alpha, 2D, partial_alpha);
2097 render_scanline_obj_builder(copy, copy_tile, 1D, no_partial_alpha);
2098 render_scanline_obj_builder(copy, copy_tile, 2D, no_partial_alpha);
2099 render_scanline_obj_builder(copy, copy_bitmap, 1D, no_partial_alpha);
2100 render_scanline_obj_builder(copy, copy_bitmap, 2D, no_partial_alpha);
2104 void order_obj(u32 video_mode)
2106 s32 obj_num, priority, row;
2108 s32 obj_size, obj_mode;
2109 s32 obj_width, obj_height;
2111 u32 obj_attribute_0, obj_attribute_1, obj_attribute_2;
2112 s32 vcount = io_registers[REG_VCOUNT];
2113 u32 partial_tile_run, partial_tile_offset;
2116 u16 *oam_ptr = oam_ram + 508;
2118 u8 *tile_base = vram + 0x10000;
2121 for(priority = 0; priority < 5; priority++)
2123 for(row = 0; row < 160; row++)
2125 obj_priority_count[priority][row] = 0;
2129 for(row = 0; row < 160; row++)
2131 obj_alpha_count[row] = 0;
2134 for(obj_num = 127; obj_num >= 0; obj_num--, oam_ptr -= 4)
2136 obj_attribute_0 = oam_ptr[0];
2137 obj_attribute_2 = oam_ptr[2];
2138 obj_size = obj_attribute_0 & 0xC000;
2139 obj_priority = (obj_attribute_2 >> 10) & 0x03;
2140 obj_mode = (obj_attribute_0 >> 10) & 0x03;
2142 if(((obj_attribute_0 & 0x0300) != 0x0200) && (obj_size != 0xC000) &&
2143 (obj_mode != 3) && ((video_mode < 3) ||
2144 ((obj_attribute_2 & 0x3FF) >= 512)))
2146 obj_y = obj_attribute_0 & 0xFF;
2150 obj_attribute_1 = oam_ptr[1];
2151 obj_size = ((obj_size >> 12) & 0x0C) | (obj_attribute_1 >> 14);
2152 obj_height = obj_height_table[obj_size];
2153 obj_width = obj_width_table[obj_size];
2155 if(obj_attribute_0 & 0x200)
2161 if(((obj_y + obj_height) > 0) && (obj_y < 160))
2163 obj_x = (s32)(obj_attribute_1 << 23) >> 23;
2165 if(((obj_x + obj_width) > 0) && (obj_x < 240))
2169 obj_height += obj_y;
2173 if((obj_y + obj_height) >= 160)
2175 obj_height = 160 - obj_y;
2180 for(row = obj_y; row < obj_y + obj_height; row++)
2182 current_count = obj_priority_count[obj_priority][row];
2183 obj_priority_list[obj_priority][row][current_count] = obj_num;
2184 obj_priority_count[obj_priority][row] = current_count + 1;
2185 obj_alpha_count[row]++;
2195 for(row = obj_y; row < obj_y + obj_height; row++)
2197 current_count = obj_priority_count[obj_priority][row];
2198 obj_priority_list[obj_priority][row][current_count] = obj_num;
2199 obj_priority_count[obj_priority][row] = current_count + 1;
2208 u32 layer_order[16];
2211 u32 order_layers(u32 layer_flags)
2213 s32 priority, layer_number;
2216 for(priority = 3; priority >= 0; priority--)
2218 for(layer_number = 3; layer_number >= 0; layer_number--)
2220 if(((layer_flags >> layer_number) & 1) &&
2221 ((io_registers[REG_BG0CNT + layer_number] & 0x03) == priority))
2223 layer_order[layer_count] = layer_number;
2228 if((obj_priority_count[priority][io_registers[REG_VCOUNT]] > 0)
2229 && (layer_flags & 0x10))
2231 layer_order[layer_count] = priority | 0x04;
2237 #define fill_line(_start, _end) \
2240 for(i = _start; i < _end; i++) \
2242 dest_ptr[i] = color; \
2246 #define fill_line_color_normal() \
2247 color = palette_ram_converted[color] \
2249 #define fill_line_color_alpha() \
2251 #define fill_line_color_color16() \
2253 #define fill_line_color_color32() \
2255 #define fill_line_builder(type) \
2256 void fill_line_##type(u16 color, render_scanline_dest_##type *dest_ptr, \
2257 u32 start, u32 end) \
2259 fill_line_color_##type(); \
2260 fill_line(start, end); \
2263 fill_line_builder(normal);
2264 fill_line_builder(alpha);
2265 fill_line_builder(color16);
2266 fill_line_builder(color32);
2269 // Alpha blend two pixels (pixel_top and pixel_bottom).
2271 #define blend_pixel() \
2272 pixel_bottom = palette_ram_converted[(pixel_pair >> 16) & 0x1FF]; \
2273 pixel_bottom = (pixel_bottom | (pixel_bottom << 16)) & 0x07E0F81F; \
2274 pixel_top = ((pixel_top * blend_a) + (pixel_bottom * blend_b)) >> 4 \
2277 // Alpha blend two pixels, allowing for saturation (individual channels > 31).
2278 // The operation is optimized towards saturation not occuring.
2280 #define blend_saturate_pixel() \
2281 pixel_bottom = palette_ram_converted[(pixel_pair >> 16) & 0x1FF]; \
2282 pixel_bottom = (pixel_bottom | (pixel_bottom << 16)) & 0x07E0F81F; \
2283 pixel_top = ((pixel_top * blend_a) + (pixel_bottom * blend_b)) >> 4; \
2284 if(pixel_top & 0x08010020) \
2286 if(pixel_top & 0x08000000) \
2287 pixel_top |= 0x07E00000; \
2289 if(pixel_top & 0x00010000) \
2290 pixel_top |= 0x0000F800; \
2292 if(pixel_top & 0x00000020) \
2293 pixel_top |= 0x0000001F; \
2296 #define brighten_pixel() \
2297 pixel_top = upper + ((pixel_top * blend) >> 4); \
2299 #define darken_pixel() \
2300 pixel_top = (pixel_top * blend) >> 4; \
2302 #define effect_condition_alpha \
2303 ((pixel_pair & 0x04000200) == 0x04000200) \
2305 #define effect_condition_fade(pixel_source) \
2306 ((pixel_source & 0x00000200) == 0x00000200) \
2308 #define expand_pixel_no_dest(expand_type, pixel_source) \
2309 pixel_top = (pixel_top | (pixel_top << 16)) & 0x07E0F81F; \
2310 expand_type##_pixel(); \
2311 pixel_top &= 0x07E0F81F; \
2312 pixel_top = (pixel_top >> 16) | pixel_top \
2314 #define expand_pixel(expand_type, pixel_source) \
2315 pixel_top = palette_ram_converted[pixel_source & 0x1FF]; \
2316 expand_pixel_no_dest(expand_type, pixel_source); \
2317 *screen_dest_ptr = pixel_top \
2319 #define expand_loop(expand_type, effect_condition, pixel_source) \
2320 screen_src_ptr += start; \
2321 screen_dest_ptr += start; \
2325 for(i = 0; i < end; i++) \
2327 pixel_source = *screen_src_ptr; \
2328 if(effect_condition) \
2330 expand_pixel(expand_type, pixel_source); \
2334 *screen_dest_ptr = \
2335 palette_ram_converted[pixel_source & 0x1FF]; \
2339 screen_dest_ptr++; \
2343 #define expand_loop_partial_alpha(alpha_expand, expand_type) \
2344 screen_src_ptr += start; \
2345 screen_dest_ptr += start; \
2349 for(i = 0; i < end; i++) \
2351 pixel_pair = *screen_src_ptr; \
2352 if(effect_condition_fade(pixel_pair)) \
2354 if(effect_condition_alpha) \
2356 expand_pixel(alpha_expand, pixel_pair); \
2360 expand_pixel(expand_type, pixel_pair); \
2365 *screen_dest_ptr = \
2366 palette_ram_converted[pixel_pair & 0x1FF]; \
2370 screen_dest_ptr++; \
2374 #define expand_partial_alpha(expand_type) \
2375 if((blend_a + blend_b) > 16) \
2377 expand_loop_partial_alpha(blend_saturate, expand_type); \
2381 expand_loop_partial_alpha(blend, expand_type); \
2386 // Blend top two pixels of scanline with each other.
2388 #ifdef RENDER_COLOR16_NORMAL
2392 void expand_normal(u16 *screen_ptr, u32 start, u32 end)
2394 u32 i, pixel_source;
2395 screen_ptr += start;
2401 for(i = 0; i < end; i++)
2403 pixel_source = *screen_ptr;
2404 *screen_ptr = palette_ram_converted[pixel_source];
2414 #define expand_normal(screen_ptr, start, end)
2421 void expand_blend(u32 *screen_src_ptr, u16 *screen_dest_ptr,
2425 u32 pixel_top, pixel_bottom;
2426 u32 bldalpha = io_registers[REG_BLDALPHA];
2427 u32 blend_a = bldalpha & 0x1F;
2428 u32 blend_b = (bldalpha >> 8) & 0x1F;
2437 // The individual colors can saturate over 31, this should be taken
2438 // care of in an alternate pass as it incurs a huge additional speedhit.
2439 if((blend_a + blend_b) > 16)
2441 expand_loop(blend_saturate, effect_condition_alpha, pixel_pair);
2445 expand_loop(blend, effect_condition_alpha, pixel_pair);
2451 // Blend scanline with white.
2453 void expand_darken(u16 *screen_src_ptr, u16 *screen_dest_ptr,
2457 s32 blend = 16 - (io_registers[REG_BLDY] & 0x1F);
2463 expand_loop(darken, effect_condition_fade(pixel_top), pixel_top);
2467 // Blend scanline with black.
2469 void expand_brighten(u16 *screen_src_ptr, u16 *screen_dest_ptr,
2473 u32 blend = io_registers[REG_BLDY] & 0x1F;
2480 upper = ((0x07E0F81F * blend) >> 4) & 0x07E0F81F;
2483 expand_loop(brighten, effect_condition_fade(pixel_top), pixel_top);
2488 // Expand scanline such that if both top and bottom pass it's alpha,
2489 // if only top passes it's as specified, and if neither pass it's normal.
2491 void expand_darken_partial_alpha(u32 *screen_src_ptr, u16 *screen_dest_ptr,
2494 s32 blend = 16 - (io_registers[REG_BLDY] & 0x1F);
2496 u32 pixel_top, pixel_bottom;
2497 u32 bldalpha = io_registers[REG_BLDALPHA];
2498 u32 blend_a = bldalpha & 0x1F;
2499 u32 blend_b = (bldalpha >> 8) & 0x1F;
2511 expand_partial_alpha(darken);
2515 void expand_brighten_partial_alpha(u32 *screen_src_ptr, u16 *screen_dest_ptr,
2518 s32 blend = io_registers[REG_BLDY] & 0x1F;
2520 u32 pixel_top, pixel_bottom;
2521 u32 bldalpha = io_registers[REG_BLDALPHA];
2522 u32 blend_a = bldalpha & 0x1F;
2523 u32 blend_b = (bldalpha >> 8) & 0x1F;
2530 upper = ((0x07E0F81F * blend) >> 4) & 0x07E0F81F;
2539 expand_partial_alpha(brighten);
2543 // Render an OBJ layer from start to end, depending on the type (1D or 2D)
2544 // stored in dispcnt.
2546 #define render_obj_layer(type, dest, _start, _end) \
2547 current_layer &= ~0x04; \
2548 if(dispcnt & 0x40) \
2549 render_scanline_obj_##type##_1D(current_layer, _start, _end, dest); \
2551 render_scanline_obj_##type##_2D(current_layer, _start, _end, dest) \
2554 // Render a target all the way with the background color as taken from the
2557 #define fill_line_bg(type, dest, _start, _end) \
2558 fill_line_##type(0, dest, _start, _end) \
2561 // Render all layers as they appear in the layer order.
2563 #define render_layers(tile_alpha, obj_alpha, dest) \
2565 current_layer = layer_order[0]; \
2566 if(current_layer & 0x04) \
2568 /* If the first one is OBJ render the background then render it. */ \
2569 fill_line_bg(tile_alpha, dest, 0, 240); \
2570 render_obj_layer(obj_alpha, dest, 0, 240); \
2574 /* Otherwise render a base layer. */ \
2575 layer_renderers[current_layer].tile_alpha##_render_base(current_layer, \
2579 /* Render the rest of the layers. */ \
2580 for(layer_order_pos = 1; layer_order_pos < layer_count; layer_order_pos++) \
2582 current_layer = layer_order[layer_order_pos]; \
2583 if(current_layer & 0x04) \
2585 render_obj_layer(obj_alpha, dest, 0, 240); \
2589 layer_renderers[current_layer]. \
2590 tile_alpha##_render_transparent(current_layer, 0, 240, dest); \
2595 #define render_condition_alpha \
2596 (((io_registers[REG_BLDALPHA] & 0x1F1F) != 0x001F) && \
2597 ((io_registers[REG_BLDCNT] & 0x3F) != 0) && \
2598 ((io_registers[REG_BLDCNT] & 0x3F00) != 0)) \
2600 #define render_condition_fade \
2601 (((io_registers[REG_BLDY] & 0x1F) != 0) && \
2602 ((io_registers[REG_BLDCNT] & 0x3F) != 0)) \
2604 #define render_layers_color_effect(renderer, layer_condition, \
2605 alpha_condition, fade_condition, _start, _end) \
2607 if(layer_condition) \
2609 if(obj_alpha_count[io_registers[REG_VCOUNT]] > 0) \
2611 /* Render based on special effects mode. */ \
2612 u32 screen_buffer[240]; \
2613 switch((bldcnt >> 6) & 0x03) \
2618 if(alpha_condition) \
2620 renderer(alpha, alpha_obj, screen_buffer); \
2621 expand_blend(screen_buffer, scanline, _start, _end); \
2627 /* Fade to white */ \
2630 if(fade_condition) \
2632 renderer(color32, partial_alpha, screen_buffer); \
2633 expand_brighten_partial_alpha(screen_buffer, scanline, \
2640 /* Fade to black */ \
2643 if(fade_condition) \
2645 renderer(color32, partial_alpha, screen_buffer); \
2646 expand_darken_partial_alpha(screen_buffer, scanline, \
2654 renderer(color32, partial_alpha, screen_buffer); \
2655 expand_blend(screen_buffer, scanline, _start, _end); \
2659 /* Render based on special effects mode. */ \
2660 switch((bldcnt >> 6) & 0x03) \
2665 if(alpha_condition) \
2667 u32 screen_buffer[240]; \
2668 renderer(alpha, alpha_obj, screen_buffer); \
2669 expand_blend(screen_buffer, scanline, _start, _end); \
2675 /* Fade to white */ \
2678 if(fade_condition) \
2680 renderer(color16, color16, scanline); \
2681 expand_brighten(scanline, scanline, _start, _end); \
2687 /* Fade to black */ \
2690 if(fade_condition) \
2692 renderer(color16, color16, scanline); \
2693 expand_darken(scanline, scanline, _start, _end); \
2700 renderer(normal, normal, scanline); \
2701 expand_normal(scanline, _start, _end); \
2706 u32 pixel_top = palette_ram_converted[0]; \
2707 switch((bldcnt >> 6) & 0x03) \
2709 /* Fade to white */ \
2712 if(color_combine_mask_a(5)) \
2714 u32 blend = io_registers[REG_BLDY] & 0x1F; \
2720 upper = ((0x07E0F81F * blend) >> 4) & 0x07E0F81F; \
2721 blend = 16 - blend; \
2723 expand_pixel_no_dest(brighten, pixel_top); \
2728 /* Fade to black */ \
2731 if(color_combine_mask_a(5)) \
2733 s32 blend = 16 - (io_registers[REG_BLDY] & 0x1F); \
2738 expand_pixel_no_dest(darken, pixel_top); \
2743 fill_line_color16(pixel_top, scanline, _start, _end); \
2748 // Renders an entire scanline from 0 to 240, based on current color mode.
2750 void render_scanline_tile(u16 *scanline, u32 dispcnt)
2753 u32 layer_order_pos;
2754 u32 bldcnt = io_registers[REG_BLDCNT];
2755 render_scanline_layer_functions_tile();
2757 render_layers_color_effect(render_layers, layer_count,
2758 render_condition_alpha, render_condition_fade, 0, 240);
2761 void render_scanline_bitmap(u16 *scanline, u32 dispcnt)
2763 u32 bldcnt = io_registers[REG_BLDCNT];
2764 render_scanline_layer_functions_bitmap();
2766 u32 layer_order_pos;
2768 fill_line_bg(normal, scanline, 0, 240);
2770 for(layer_order_pos = 0; layer_order_pos < layer_count; layer_order_pos++)
2772 current_layer = layer_order[layer_order_pos];
2773 if(current_layer & 0x04)
2775 render_obj_layer(normal, scanline, 0, 240);
2779 layer_renderers->normal_render(0, 240, scanline);
2784 // Render layers from start to end based on if they're allowed in the
2787 #define render_layers_conditional(tile_alpha, obj_alpha, dest) \
2790 current_layer = layer_order[layer_order_pos]; \
2791 /* If OBJ aren't enabled skip to the first non-OBJ layer */ \
2792 if(!(enable_flags & 0x10)) \
2794 while((current_layer & 0x04) || !((1 << current_layer) & enable_flags)) \
2796 layer_order_pos++; \
2797 current_layer = layer_order[layer_order_pos]; \
2799 /* Oops, ran out of layers, render the background. */ \
2800 if(layer_order_pos == layer_count) \
2802 fill_line_bg(tile_alpha, dest, start, end); \
2807 /* Render the first valid layer */ \
2808 layer_renderers[current_layer].tile_alpha##_render_base(current_layer, \
2809 start, end, dest); \
2811 layer_order_pos++; \
2813 /* Render the rest of the layers if active, skipping OBJ ones. */ \
2814 for(; layer_order_pos < layer_count; layer_order_pos++) \
2816 current_layer = layer_order[layer_order_pos]; \
2817 if(!(current_layer & 0x04) && ((1 << current_layer) & enable_flags)) \
2819 layer_renderers[current_layer]. \
2820 tile_alpha##_render_transparent(current_layer, start, end, dest); \
2826 /* Find the first active layer, skip all of the inactive ones */ \
2827 while(!((current_layer & 0x04) || ((1 << current_layer) & enable_flags))) \
2829 layer_order_pos++; \
2830 current_layer = layer_order[layer_order_pos]; \
2832 /* Oops, ran out of layers, render the background. */ \
2833 if(layer_order_pos == layer_count) \
2835 fill_line_bg(tile_alpha, dest, start, end); \
2840 if(current_layer & 0x04) \
2842 /* If the first one is OBJ render the background then render it. */ \
2843 fill_line_bg(tile_alpha, dest, start, end); \
2844 render_obj_layer(obj_alpha, dest, start, end); \
2848 /* Otherwise render a base layer. */ \
2849 layer_renderers[current_layer]. \
2850 tile_alpha##_render_base(current_layer, start, end, dest); \
2853 layer_order_pos++; \
2855 /* Render the rest of the layers. */ \
2856 for(; layer_order_pos < layer_count; layer_order_pos++) \
2858 current_layer = layer_order[layer_order_pos]; \
2859 if(current_layer & 0x04) \
2861 render_obj_layer(obj_alpha, dest, start, end); \
2865 if(enable_flags & (1 << current_layer)) \
2867 layer_renderers[current_layer]. \
2868 tile_alpha##_render_transparent(current_layer, start, end, dest); \
2879 // Render all of the BG and OBJ in a tiled scanline from start to end ONLY if
2880 // enable_flag allows that layer/OBJ. Also conditionally render color effects.
2882 void render_scanline_conditional_tile(u32 start, u32 end, u16 *scanline,
2883 u32 enable_flags, u32 dispcnt, u32 bldcnt, tile_layer_render_struct
2887 u32 layer_order_pos = 0;
2889 render_layers_color_effect(render_layers_conditional,
2890 (layer_count && (enable_flags & 0x1F)),
2891 ((enable_flags & 0x20) && render_condition_alpha),
2892 ((enable_flags & 0x20) && render_condition_fade), start, end);
2896 // Render the BG and OBJ in a bitmap scanline from start to end ONLY if
2897 // enable_flag allows that layer/OBJ. Also conditionally render color effects.
2899 void render_scanline_conditional_bitmap(u32 start, u32 end, u16 *scanline,
2900 u32 enable_flags, u32 dispcnt, u32 bldcnt, bitmap_layer_render_struct
2904 u32 layer_order_pos;
2906 fill_line_bg(normal, scanline, start, end);
2908 for(layer_order_pos = 0; layer_order_pos < layer_count; layer_order_pos++)
2910 current_layer = layer_order[layer_order_pos];
2911 if(current_layer & 0x04)
2913 if(enable_flags & 0x10)
2915 render_obj_layer(normal, scanline, start, end);
2920 if(enable_flags & 0x04)
2921 layer_renderers->normal_render(start, end, scanline);
2927 #define window_x_coords(window_number) \
2928 window_##window_number##_x1 = \
2929 io_registers[REG_WIN##window_number##H] >> 8; \
2930 window_##window_number##_x2 = \
2931 io_registers[REG_WIN##window_number##H] & 0xFF; \
2932 window_##window_number##_enable = \
2933 (winin >> (window_number * 8)) & 0x3F; \
2935 if(window_##window_number##_x1 > 240) \
2936 window_##window_number##_x1 = 240; \
2938 if(window_##window_number##_x2 > 240) \
2939 window_##window_number##_x2 = 240 \
2941 #define window_coords(window_number) \
2942 u32 window_##window_number##_x1, window_##window_number##_x2; \
2943 u32 window_##window_number##_y1, window_##window_number##_y2; \
2944 u32 window_##window_number##_enable; \
2945 window_##window_number##_y1 = \
2946 io_registers[REG_WIN##window_number##V] >> 8; \
2947 window_##window_number##_y2 = \
2948 io_registers[REG_WIN##window_number##V] & 0xFF; \
2950 if(window_##window_number##_y1 > window_##window_number##_y2) \
2952 if((((vcount <= window_##window_number##_y2) || \
2953 (vcount > window_##window_number##_y1)) || \
2954 (window_##window_number##_y2 > 227)) && \
2955 (window_##window_number##_y1 <= 227)) \
2957 window_x_coords(window_number); \
2961 window_##window_number##_x1 = 240; \
2962 window_##window_number##_x2 = 240; \
2967 if((((vcount >= window_##window_number##_y1) && \
2968 (vcount < window_##window_number##_y2)) || \
2969 (window_##window_number##_y2 > 227)) && \
2970 (window_##window_number##_y1 <= 227)) \
2972 window_x_coords(window_number); \
2976 window_##window_number##_x1 = 240; \
2977 window_##window_number##_x2 = 240; \
2981 #define render_window_segment(type, start, end, window_type) \
2984 render_scanline_conditional_##type(start, end, scanline, \
2985 window_##window_type##_enable, dispcnt, bldcnt, layer_renderers); \
2988 #define render_window_segment_unequal(type, start, end, window_type) \
2989 render_scanline_conditional_##type(start, end, scanline, \
2990 window_##window_type##_enable, dispcnt, bldcnt, layer_renderers) \
2992 #define render_window_segment_clip(type, clip_start, clip_end, start, end, \
2997 if(start < clip_start) \
2999 if(end > clip_start) \
3001 if(end > clip_end) \
3003 render_window_segment_unequal(type, clip_start, clip_end, \
3008 render_window_segment_unequal(type, clip_start, end, window_type); \
3014 if(end > clip_end) \
3016 if(start < clip_end) \
3017 render_window_segment_unequal(type, start, clip_end, window_type); \
3021 render_window_segment_unequal(type, start, end, window_type); \
3026 #define render_window_clip_1(type, start, end) \
3027 if(window_1_x1 != 240) \
3029 if(window_1_x1 > window_1_x2) \
3031 render_window_segment_clip(type, start, end, 0, window_1_x2, 1); \
3032 render_window_segment_clip(type, start, end, window_1_x2, window_1_x1, \
3034 render_window_segment_clip(type, start, end, window_1_x1, 240, 1); \
3038 render_window_segment_clip(type, start, end, 0, window_1_x1, out); \
3039 render_window_segment_clip(type, start, end, window_1_x1, window_1_x2, \
3041 render_window_segment_clip(type, start, end, window_1_x2, 240, out); \
3046 render_window_segment(type, start, end, out); \
3049 #define render_window_clip_obj(type, start, end); \
3050 render_window_segment(type, start, end, out); \
3051 if(dispcnt & 0x40) \
3052 render_scanline_obj_copy_##type##_1D(4, start, end, scanline); \
3054 render_scanline_obj_copy_##type##_2D(4, start, end, scanline) \
3057 #define render_window_segment_clip_obj(type, clip_start, clip_end, start, \
3062 if(start < clip_start) \
3064 if(end > clip_start) \
3066 if(end > clip_end) \
3068 render_window_clip_obj(type, clip_start, clip_end); \
3072 render_window_clip_obj(type, clip_start, end); \
3078 if(end > clip_end) \
3080 if(start < clip_end) \
3082 render_window_clip_obj(type, start, clip_end); \
3087 render_window_clip_obj(type, start, end); \
3093 #define render_window_clip_1_obj(type, start, end) \
3094 if(window_1_x1 != 240) \
3096 if(window_1_x1 > window_1_x2) \
3098 render_window_segment_clip(type, start, end, 0, window_1_x2, 1); \
3099 render_window_segment_clip_obj(type, start, end, window_1_x2, \
3101 render_window_segment_clip(type, start, end, window_1_x1, 240, 1); \
3105 render_window_segment_clip_obj(type, start, end, 0, window_1_x1); \
3106 render_window_segment_clip(type, start, end, window_1_x1, window_1_x2, \
3108 render_window_segment_clip_obj(type, start, end, window_1_x2, 240); \
3113 render_window_clip_obj(type, start, end); \
3118 #define render_window_single(type, window_number) \
3119 u32 winin = io_registers[REG_WININ]; \
3120 window_coords(window_number); \
3121 if(window_##window_number##_x1 > window_##window_number##_x2) \
3123 render_window_segment(type, 0, window_##window_number##_x2, \
3125 render_window_segment(type, window_##window_number##_x2, \
3126 window_##window_number##_x1, out); \
3127 render_window_segment(type, window_##window_number##_x1, 240, \
3132 render_window_segment(type, 0, window_##window_number##_x1, out); \
3133 render_window_segment(type, window_##window_number##_x1, \
3134 window_##window_number##_x2, window_number); \
3135 render_window_segment(type, window_##window_number##_x2, 240, out); \
3138 #define render_window_multi(type, front, back) \
3139 if(window_##front##_x1 > window_##front##_x2) \
3141 render_window_segment(type, 0, window_##front##_x2, front); \
3142 render_window_clip_##back(type, window_##front##_x2, \
3143 window_##front##_x1); \
3144 render_window_segment(type, window_##front##_x1, 240, front); \
3148 render_window_clip_##back(type, 0, window_##front##_x1); \
3149 render_window_segment(type, window_##front##_x1, window_##front##_x2, \
3151 render_window_clip_##back(type, window_##front##_x2, 240); \
3154 #define render_scanline_window_builder(type) \
3155 void render_scanline_window_##type(u16 *scanline, u32 dispcnt) \
3157 u32 vcount = io_registers[REG_VCOUNT]; \
3158 u32 winout = io_registers[REG_WINOUT]; \
3159 u32 bldcnt = io_registers[REG_BLDCNT]; \
3160 u32 window_out_enable = winout & 0x3F; \
3162 render_scanline_layer_functions_##type(); \
3164 switch(dispcnt >> 13) \
3166 /* Just window 0 */ \
3169 render_window_single(type, 0); \
3173 /* Just window 1 */ \
3176 render_window_single(type, 1); \
3180 /* Windows 1 and 2 */ \
3183 u32 winin = io_registers[REG_WININ]; \
3186 render_window_multi(type, 0, 1); \
3190 /* Just OBJ windows */ \
3193 u32 window_obj_enable = winout >> 8; \
3194 render_window_clip_obj(type, 0, 240); \
3198 /* Window 0 and OBJ window */ \
3201 u32 window_obj_enable = winout >> 8; \
3202 u32 winin = io_registers[REG_WININ]; \
3204 render_window_multi(type, 0, obj); \
3208 /* Window 1 and OBJ window */ \
3211 u32 window_obj_enable = winout >> 8; \
3212 u32 winin = io_registers[REG_WININ]; \
3214 render_window_multi(type, 1, obj); \
3218 /* Window 0, 1, and OBJ window */ \
3221 u32 window_obj_enable = winout >> 8; \
3222 u32 winin = io_registers[REG_WININ]; \
3225 render_window_multi(type, 0, 1_obj); \
3231 render_scanline_window_builder(tile);
3232 render_scanline_window_builder(bitmap);
3234 u32 active_layers[6] = { 0x1F, 0x17, 0x1C, 0x14, 0x14, 0x14 };
3236 u32 small_resolution_width = 240;
3237 u32 small_resolution_height = 160;
3238 u32 resolution_width, resolution_height;
3240 void update_scanline()
3242 u32 pitch = get_screen_pitch();
3243 u32 dispcnt = io_registers[REG_DISPCNT];
3244 u32 display_flags = (dispcnt >> 8) & 0x1F;
3245 u32 vcount = io_registers[REG_VCOUNT];
3246 u16 *screen_offset = get_screen_pixels() + (vcount * pitch);
3247 u32 video_mode = dispcnt & 0x07;
3250 // If OAM has been modified since the last scanline has been updated then
3251 // reorder and reprofile the OBJ lists.
3254 order_obj(video_mode);
3258 order_layers((dispcnt >> 8) & active_layers[video_mode]);
3263 // If the screen is in in forced blank draw pure white.
3266 fill_line_color16(0xFFFF, screen_offset, 0, 240);
3274 render_scanline_window_tile(screen_offset, dispcnt);
3278 render_scanline_tile(screen_offset, dispcnt);
3284 render_scanline_window_bitmap(screen_offset, dispcnt);
3286 render_scanline_bitmap(screen_offset, dispcnt);
3290 affine_reference_x[0] += (s16)io_registers[REG_BG2PB];
3291 affine_reference_y[0] += (s16)io_registers[REG_BG2PD];
3292 affine_reference_x[1] += (s16)io_registers[REG_BG3PB];
3293 affine_reference_y[1] += (s16)io_registers[REG_BG3PD];
3298 u32 screen_flip = 0;
3302 if(video_direct == 0)
3304 u32 *old_ge_cmd_ptr = ge_cmd_ptr;
3305 sceKernelDcacheWritebackAll();
3307 // Render the current screen
3308 ge_cmd_ptr = ge_cmd + 2;
3309 GE_CMD(TBP0, ((u32)screen_pixels & 0x00FFFFFF));
3310 GE_CMD(TBW0, (((u32)screen_pixels & 0xFF000000) >> 8) |
3312 ge_cmd_ptr = old_ge_cmd_ptr;
3314 sceGeListEnQueue(ge_cmd, ge_cmd_ptr, gecbid, NULL);
3316 // Flip to the next screen
3320 screen_pixels = screen_texture + (240 * 160 * 2);
3322 screen_pixels = screen_texture;
3328 #define integer_scale_copy_2() \
3329 current_scanline_ptr[x2] = current_pixel; \
3330 current_scanline_ptr[x2 - 1] = current_pixel; \
3333 #define integer_scale_copy_3() \
3334 current_scanline_ptr[x2] = current_pixel; \
3335 current_scanline_ptr[x2 - 1] = current_pixel; \
3336 current_scanline_ptr[x2 - 2] = current_pixel; \
3339 #define integer_scale_copy_4() \
3340 current_scanline_ptr[x2] = current_pixel; \
3341 current_scanline_ptr[x2 - 1] = current_pixel; \
3342 current_scanline_ptr[x2 - 2] = current_pixel; \
3343 current_scanline_ptr[x2 - 3] = current_pixel; \
3346 #define integer_scale_horizontal(scale_factor) \
3347 for(y = 0; y < 160; y++) \
3349 for(x = 239, x2 = (240 * video_scale) - 1; x >= 0; x--) \
3351 current_pixel = current_scanline_ptr[x]; \
3352 integer_scale_copy_##scale_factor(); \
3353 current_scanline_ptr[x2] = current_scanline_ptr[x]; \
3354 current_scanline_ptr[x2 - 1] = current_scanline_ptr[x]; \
3355 current_scanline_ptr[x2 - 2] = current_scanline_ptr[x]; \
3357 current_scanline_ptr += pitch; \
3362 if((video_scale != 1) && (current_scale != unscaled))
3366 u16 *screen_ptr = get_screen_pixels();
3367 u16 *current_scanline_ptr = screen_ptr;
3368 u32 pitch = get_screen_pitch();
3375 integer_scale_horizontal(2);
3379 integer_scale_horizontal(3);
3384 integer_scale_horizontal(4);
3389 for(y = 159, y2 = (160 * video_scale) - 1; y >= 0; y--)
3391 for(i = 0; i < video_scale; i++)
3393 memcpy(screen_ptr + (y2 * pitch),
3394 screen_ptr + (y * pitch), 480 * video_scale);
3401 if((screen_scale == unscaled) &&
3402 (resolution_width == small_resolution_width) &&
3403 (resolution_height == small_resolution_height))
3405 SDL_Rect srect = {0, 0, 240, 160};
3406 SDL_Rect drect = {40, 40, 240, 160};
3407 SDL_BlitSurface(screen, &srect, hw_screen, &drect);
3409 else if((screen_scale == scaled_aspect) &&
3410 (resolution_width == small_resolution_width) &&
3411 (resolution_height == small_resolution_height))
3413 SDL_Rect drect = {0, 10, 0, 0};
3414 SDL_BlitSurface(screen, NULL, hw_screen, &drect);
3418 SDL_BlitSurface(screen, NULL, hw_screen, NULL);
3420 /* it is unclear if this syscall takes virtual or physical addresses,
3421 * but using virtual seems to work for me. */
3422 gp2x_flush_cache(hw_screen->pixels, hw_screen->pixels + 320*240, 0);
3431 u32 frame_to_render;
3433 void update_screen()
3435 if(!skip_next_frame)
3443 sceDisplaySetMode(0, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
3445 sceDisplayWaitVblankStart();
3446 sceDisplaySetFrameBuf((void*)psp_gu_vram_base, PSP_LINE_SIZE,
3447 PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
3451 sceGuStart(GU_DIRECT, display_list);
3452 sceGuDrawBuffer(GU_PSM_5650, (void*)0, PSP_LINE_SIZE);
3453 sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3454 (void*)0, PSP_LINE_SIZE);
3455 sceGuClear(GU_COLOR_BUFFER_BIT);
3457 sceGuOffset(2048 - (PSP_SCREEN_WIDTH / 2), 2048 - (PSP_SCREEN_HEIGHT / 2));
3458 sceGuViewport(2048, 2048, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
3460 sceGuScissor(0, 0, PSP_SCREEN_WIDTH + 1, PSP_SCREEN_HEIGHT + 1);
3461 sceGuEnable(GU_SCISSOR_TEST);
3462 sceGuTexMode(GU_PSM_5650, 0, 0, GU_FALSE);
3463 sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
3464 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
3465 sceGuEnable(GU_TEXTURE_2D);
3467 sceGuFrontFace(GU_CW);
3468 sceGuDisable(GU_BLEND);
3473 sceDisplayWaitVblankStart();
3474 sceGuDisplay(GU_TRUE);
3476 PspGeCallbackData gecb;
3477 gecb.signal_func = NULL;
3478 gecb.signal_arg = NULL;
3479 gecb.finish_func = Ge_Finish_Callback;
3480 gecb.finish_arg = NULL;
3481 gecbid = sceGeSetCallback(&gecb);
3483 screen_vertex[0] = 0 + 0.5;
3484 screen_vertex[1] = 0 + 0.5;
3485 screen_vertex[2] = 0 + 0.5;
3486 screen_vertex[3] = 0 + 0.5;
3487 screen_vertex[4] = 0;
3488 screen_vertex[5] = GBA_SCREEN_WIDTH - 0.5;
3489 screen_vertex[6] = GBA_SCREEN_HEIGHT - 0.5;
3490 screen_vertex[7] = PSP_SCREEN_WIDTH - 0.5;
3491 screen_vertex[8] = PSP_SCREEN_HEIGHT - 0.5;
3492 screen_vertex[9] = 0;
3494 // Set framebuffer to PSP VRAM
3495 GE_CMD(FBP, ((u32)psp_gu_vram_base & 0x00FFFFFF));
3496 GE_CMD(FBW, (((u32)psp_gu_vram_base & 0xFF000000) >> 8) | PSP_LINE_SIZE);
3497 // Set texture 0 to the screen texture
3498 GE_CMD(TBP0, ((u32)screen_texture & 0x00FFFFFF));
3499 GE_CMD(TBW0, (((u32)screen_texture & 0xFF000000) >> 8) | GBA_SCREEN_WIDTH);
3500 // Set the texture size to 256 by 256 (2^8 by 2^8)
3501 GE_CMD(TSIZE0, (8 << 8) | 8);
3502 // Flush the texture cache
3504 // Use 2D coordinates, no indeces, no weights, 32bit float positions,
3505 // 32bit float texture coordinates
3506 GE_CMD(VTYPE, (1 << 23) | (0 << 11) | (0 << 9) |
3507 (3 << 7) | (0 << 5) | (0 << 2) | 3);
3508 // Set the base of the index list pointer to 0
3510 // Set the rest of index list pointer to 0 (not being used)
3512 // Set the base of the screen vertex list pointer
3513 GE_CMD(BASE, ((u32)screen_vertex & 0xFF000000) >> 8);
3514 // Set the rest of the screen vertex list pointer
3515 GE_CMD(VADDR, ((u32)screen_vertex & 0x00FFFFFF));
3516 // Primitive kick: render sprite (primitive 6), 2 vertices
3517 GE_CMD(PRIM, (6 << 16) | 2);
3518 // Done with commands
3520 // Raise signal interrupt
3530 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE);
3533 SDL_GP2X_AllowGfxMemory(NULL, 0);
3535 hw_screen = SDL_SetVideoMode(320 * video_scale, 240 * video_scale,
3538 screen = SDL_CreateRGBSurface(SDL_HWSURFACE, 240 * video_scale,
3539 160 * video_scale, 16, 0xFFFF, 0xFFFF, 0xFFFF, 0);
3541 gp2x_load_mmuhack();
3543 screen = SDL_SetVideoMode(240 * video_scale, 160 * video_scale, 16, 0);
3550 video_scale_type screen_scale = scaled_aspect;
3551 video_scale_type current_scale = scaled_aspect;
3552 video_filter_type screen_filter = filter_bilinear;
3557 void video_resolution_large()
3559 if(video_direct != 1)
3562 screen_pixels = psp_gu_vram_base;
3564 sceGuStart(GU_DIRECT, display_list);
3565 sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3566 (void*)0, PSP_LINE_SIZE);
3571 void set_gba_resolution(video_scale_type scale)
3573 u32 filter_linear = 0;
3574 screen_scale = scale;
3578 screen_vertex[2] = 120 + 0.5;
3579 screen_vertex[3] = 56 + 0.5;
3580 screen_vertex[7] = GBA_SCREEN_WIDTH + 120 - 0.5;
3581 screen_vertex[8] = GBA_SCREEN_HEIGHT + 56 - 0.5;
3585 screen_vertex[2] = 36 + 0.5;
3586 screen_vertex[3] = 0 + 0.5;
3587 screen_vertex[7] = 408 + 36 - 0.5;
3588 screen_vertex[8] = PSP_SCREEN_HEIGHT - 0.5;
3592 screen_vertex[2] = 0;
3593 screen_vertex[3] = 0;
3594 screen_vertex[7] = PSP_SCREEN_WIDTH;
3595 screen_vertex[8] = PSP_SCREEN_HEIGHT;
3599 sceGuStart(GU_DIRECT, display_list);
3600 if(screen_filter == filter_bilinear)
3601 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
3603 sceGuTexFilter(GU_NEAREST, GU_NEAREST);
3608 clear_screen(0x0000);
3611 void video_resolution_small()
3613 if(video_direct != 0)
3615 set_gba_resolution(screen_scale);
3617 screen_pixels = screen_texture;
3620 sceGuStart(GU_DIRECT, display_list);
3621 sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3622 (void*)0, PSP_LINE_SIZE);
3627 void clear_screen(u16 color)
3630 u16 *src_ptr = get_screen_pixels();
3634 for(i = 0; i < (512 * 272); i++, src_ptr++)
3639 // I don't know why this doesn't work.
3640 /* color = (((color & 0x1F) * 255 / 31) << 0) |
3641 ((((color >> 5) & 0x3F) * 255 / 63) << 8) |
3642 ((((color >> 11) & 0x1F) * 255 / 31) << 16) | (0xFF << 24);
3644 sceGuStart(GU_DIRECT, display_list);
3645 sceGuDrawBuffer(GU_PSM_5650, (void*)0, PSP_LINE_SIZE);
3646 //sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3647 // (void*)0, PSP_LINE_SIZE);
3648 sceGuClearColor(color);
3649 sceGuClear(GU_COLOR_BUFFER_BIT);
3656 void video_resolution_large()
3658 current_scale = unscaled;
3661 SDL_FreeSurface(screen);
3662 SDL_GP2X_AllowGfxMemory(NULL, 0);
3663 hw_screen = SDL_SetVideoMode(320, 240, 16, SDL_HWSURFACE);
3664 screen = SDL_CreateRGBSurface(SDL_HWSURFACE, 320, 240, 16, 0xFFFF,
3666 resolution_width = 320;
3667 resolution_height = 240;
3670 gp2x_load_mmuhack();
3672 screen = SDL_SetVideoMode(480, 272, 16, 0);
3673 resolution_width = 480;
3674 resolution_height = 272;
3678 void video_resolution_small()
3680 current_scale = screen_scale;
3684 SDL_FreeSurface(screen);
3685 SDL_GP2X_AllowGfxMemory(NULL, 0);
3688 if (screen_scale != unscaled)
3690 w = small_resolution_width * video_scale;
3691 h = small_resolution_height * video_scale;
3693 if (screen_scale == scaled_aspect) h += 20;
3694 hw_screen = SDL_SetVideoMode(w, h, 16, SDL_HWSURFACE);
3696 screen = SDL_CreateRGBSurface(SDL_HWSURFACE,
3697 small_resolution_width * video_scale, small_resolution_height *
3698 video_scale, 16, 0xFFFF, 0xFFFF, 0xFFFF, 0);
3702 gp2x_load_mmuhack();
3704 screen = SDL_SetVideoMode(small_resolution_width * video_scale,
3705 small_resolution_height * video_scale, 16, 0);
3707 resolution_width = small_resolution_width;
3708 resolution_height = small_resolution_height;
3711 void set_gba_resolution(video_scale_type scale)
3713 if(screen_scale != scale)
3715 screen_scale = scale;
3721 small_resolution_width = 240 * video_scale;
3722 small_resolution_height = 160 * video_scale;
3728 void clear_screen(u16 color)
3730 u16 *dest_ptr = get_screen_pixels();
3731 u32 line_skip = get_screen_pitch() - screen->w;
3734 for(y = 0; y < screen->h; y++)
3736 for(x = 0; x < screen->w; x++, dest_ptr++)
3740 dest_ptr += line_skip;
3748 u16 *copy = malloc(240 * 160 * 2);
3749 memcpy(copy, get_screen_pixels(), 240 * 160 * 2);
3753 void blit_to_screen(u16 *src, u32 w, u32 h, u32 dest_x, u32 dest_y)
3755 u32 pitch = get_screen_pitch();
3756 u16 *dest_ptr = get_screen_pixels() + dest_x + (dest_y * pitch);
3759 u32 line_skip = pitch - w;
3762 for(y = 0; y < h; y++)
3764 for(x = 0; x < w; x++, src_ptr++, dest_ptr++)
3766 *dest_ptr = *src_ptr;
3768 dest_ptr += line_skip;
3772 void print_string_ext(const char *str, u16 fg_color, u16 bg_color,
3773 u32 x, u32 y, void *_dest_ptr, u32 pitch, u32 pad)
3775 u16 *dest_ptr = (u16 *)_dest_ptr + (y * pitch) + x;
3776 u8 current_char = str[0];
3785 if(y + FONT_HEIGHT > resolution_height)
3790 if(current_char == '\n')
3794 dest_ptr = get_screen_pixels() + (y * pitch) + x;
3798 glyph_offset = _font_offset[current_char];
3799 current_x += FONT_WIDTH;
3800 for(i2 = 0; i2 < FONT_HEIGHT; i2++, glyph_offset++)
3802 current_row = _font_bits[glyph_offset];
3803 for(i3 = 0; i3 < FONT_WIDTH; i3++)
3805 if((current_row >> (15 - i3)) & 0x01)
3806 *dest_ptr = fg_color;
3808 *dest_ptr = bg_color;
3811 dest_ptr += (pitch - FONT_WIDTH);
3813 dest_ptr = dest_ptr - (pitch * FONT_HEIGHT) + FONT_WIDTH;
3818 current_char = str[str_index];
3820 if((i < pad) && (current_char == 0))
3829 if(current_x + FONT_WIDTH > resolution_width /* EDIT */)
3831 while (current_char && current_char != '\n')
3833 current_char = str[str_index++];
3839 void print_string(const char *str, u16 fg_color, u16 bg_color,
3842 print_string_ext(str, fg_color, bg_color, x, y, get_screen_pixels(),
3843 get_screen_pitch(), 0);
3846 void print_string_pad(const char *str, u16 fg_color, u16 bg_color,
3847 u32 x, u32 y, u32 pad)
3849 print_string_ext(str, fg_color, bg_color, x, y, get_screen_pixels(),
3850 get_screen_pitch(), pad);
3853 u32 debug_cursor_x = 0;
3854 u32 debug_cursor_y = 0;
3858 void debug_screen_clear()
3862 void debug_screen_start()
3866 void debug_screen_end()
3870 void debug_screen_update()
3874 void debug_screen_printf(const char *format, ...)
3878 va_start(ap, format);
3879 vprintf(format, ap);
3883 void debug_screen_newline(u32 count)
3891 void debug_screen_clear()
3895 clear_screen(0x0000);
3898 void debug_screen_start()
3900 video_resolution_large();
3901 debug_screen_clear();
3904 void debug_screen_end()
3906 video_resolution_small();
3909 void debug_screen_update()
3914 void debug_screen_printf(const char *format, ...)
3916 char str_buffer[512];
3917 u32 str_buffer_length;
3920 va_start(ap, format);
3921 str_buffer_length = vsnprintf(str_buffer, 512, format, ap);
3924 printf("printing debug string %s at %d %d\n", str_buffer,
3925 debug_cursor_x, debug_cursor_y);
3927 print_string(str_buffer, 0xFFFF, 0x0000, debug_cursor_x, debug_cursor_y);
3928 debug_cursor_x += FONT_WIDTH * str_buffer_length;
3931 void debug_screen_newline(u32 count)
3934 debug_cursor_y += FONT_HEIGHT * count;
3939 void debug_screen_printl(const char *format, ...)
3943 va_start(ap, format);
3944 debug_screen_printf(format, ap);
3945 debug_screen_newline(1);
3946 // debug_screen_printf("\n");
3951 #define video_savestate_builder(type) \
3952 void video_##type##_savestate(file_tag_type savestate_file) \
3954 file_##type##_array(savestate_file, affine_reference_x); \
3955 file_##type##_array(savestate_file, affine_reference_y); \
3958 video_savestate_builder(read);
3959 video_savestate_builder(write_mem);