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() \
88 #elif defined(WIZ_BUILD)
90 static u16 rot_buffer[240*4];
91 static u32 rot_lines_total = 4;
92 static u32 rot_line_count = 0;
93 static char rot_msg_buff[64];
95 static u32 screen_offset = 0;
96 static u16 *screen_pixels = NULL;
97 const u32 screen_pitch = 320;
99 #define get_screen_pixels() \
102 #define get_screen_pitch() \
108 #include "SDL_gp2x.h"
109 SDL_Surface *hw_screen;
112 const u32 video_scale = 1;
114 #define get_screen_pixels() \
115 ((u16 *)screen->pixels) \
117 #define get_screen_pitch() \
118 (screen->pitch / 2) \
122 void render_scanline_conditional_tile(u32 start, u32 end, u16 *scanline,
123 u32 enable_flags, u32 dispcnt, u32 bldcnt, tile_layer_render_struct
125 void render_scanline_conditional_bitmap(u32 start, u32 end, u16 *scanline,
126 u32 enable_flags, u32 dispcnt, u32 bldcnt, bitmap_layer_render_struct
131 // This old version is not necessary if the palette is either being converted
132 // transparently or the ABGR 1555 format is being used natively. The direct
133 // version (without conversion) is much faster.
135 #define tile_lookup_palette_full(palette, source) \
136 current_pixel = palette[source]; \
137 convert_palette(current_pixel) \
139 #define tile_lookup_palette(palette, source) \
140 current_pixel = palette[source]; \
143 #ifdef RENDER_COLOR16_NORMAL
145 #define tile_expand_base_normal(index) \
146 tile_expand_base_color16(index) \
150 #define tile_expand_base_normal(index) \
151 tile_lookup_palette(palette, current_pixel); \
152 dest_ptr[index] = current_pixel \
156 #define tile_expand_transparent_normal(index) \
157 tile_expand_base_normal(index) \
159 #define tile_expand_copy(index) \
160 dest_ptr[index] = copy_ptr[index] \
163 #define advance_dest_ptr_base(delta) \
166 #define advance_dest_ptr_transparent(delta) \
167 advance_dest_ptr_base(delta) \
169 #define advance_dest_ptr_copy(delta) \
170 advance_dest_ptr_base(delta); \
174 #define color_combine_mask_a(layer) \
175 ((io_registers[REG_BLDCNT] >> layer) & 0x01) \
177 // For color blending operations, will create a mask that has in bit
178 // 10 if the layer is target B, and bit 9 if the layer is target A.
180 #define color_combine_mask(layer) \
181 (color_combine_mask_a(layer) | \
182 ((io_registers[REG_BLDCNT] >> (layer + 7)) & 0x02)) << 9 \
184 // For alpha blending renderers, draw the palette index (9bpp) and
185 // layer bits rather than the raw RGB. For the base this should write to
186 // the 32bit location directly.
188 #define tile_expand_base_alpha(index) \
189 dest_ptr[index] = current_pixel | pixel_combine \
191 #define tile_expand_base_bg(index) \
192 dest_ptr[index] = bg_combine \
195 // For layered (transparent) writes this should shift the "stack" and write
196 // to the bottom. This will preserve the topmost pixel and the most recent
199 #define tile_expand_transparent_alpha(index) \
200 dest_ptr[index] = (dest_ptr[index] << 16) | current_pixel | pixel_combine \
203 // OBJ should only shift if the top isn't already OBJ
204 #define tile_expand_transparent_alpha_obj(index) \
205 dest = dest_ptr[index]; \
206 if(dest & 0x00000100) \
208 dest_ptr[index] = (dest & 0xFFFF0000) | current_pixel | pixel_combine; \
212 dest_ptr[index] = (dest << 16) | current_pixel | pixel_combine; \
216 // For color effects that don't need to preserve the previous layer.
217 // The color32 version should be used with 32bit wide dest_ptr so as to be
218 // compatible with alpha combine on top of it.
220 #define tile_expand_base_color16(index) \
221 dest_ptr[index] = current_pixel | pixel_combine \
223 #define tile_expand_transparent_color16(index) \
224 tile_expand_base_color16(index) \
226 #define tile_expand_base_color32(index) \
227 tile_expand_base_color16(index) \
229 #define tile_expand_transparent_color32(index) \
230 tile_expand_base_color16(index) \
233 // Operations for isolation 8bpp pixels within 32bpp pixel blocks.
235 #define tile_8bpp_pixel_op_mask(op_param) \
236 current_pixel = current_pixels & 0xFF \
238 #define tile_8bpp_pixel_op_shift_mask(shift) \
239 current_pixel = (current_pixels >> shift) & 0xFF \
241 #define tile_8bpp_pixel_op_shift(shift) \
242 current_pixel = current_pixels >> shift \
244 #define tile_8bpp_pixel_op_none(shift) \
246 // Base should always draw raw in 8bpp mode; color 0 will be drawn where
249 #define tile_8bpp_draw_base_normal(index) \
250 tile_expand_base_normal(index) \
252 #define tile_8bpp_draw_base_alpha(index) \
255 tile_expand_base_alpha(index); \
259 tile_expand_base_bg(index); \
263 #define tile_8bpp_draw_base_color16(index) \
264 tile_8bpp_draw_base_alpha(index) \
266 #define tile_8bpp_draw_base_color32(index) \
267 tile_8bpp_draw_base_alpha(index) \
270 #define tile_8bpp_draw_base(index, op, op_param, alpha_op) \
271 tile_8bpp_pixel_op_##op(op_param); \
272 tile_8bpp_draw_base_##alpha_op(index) \
274 // Transparent (layered) writes should only replace what is there if the
275 // pixel is not transparent (zero)
277 #define tile_8bpp_draw_transparent(index, op, op_param, alpha_op) \
278 tile_8bpp_pixel_op_##op(op_param); \
281 tile_expand_transparent_##alpha_op(index); \
284 #define tile_8bpp_draw_copy(index, op, op_param, alpha_op) \
285 tile_8bpp_pixel_op_##op(op_param); \
288 tile_expand_copy(index); \
291 // Get the current tile from the map in 8bpp mode
293 #define get_tile_8bpp() \
294 current_tile = *map_ptr; \
295 tile_ptr = tile_base + ((current_tile & 0x3FF) * 64) \
298 // Draw half of a tile in 8bpp mode, for base renderer
300 #define tile_8bpp_draw_four_noflip(index, combine_op, alpha_op) \
301 tile_8bpp_draw_##combine_op(index + 0, mask, 0, alpha_op); \
302 tile_8bpp_draw_##combine_op(index + 1, shift_mask, 8, alpha_op); \
303 tile_8bpp_draw_##combine_op(index + 2, shift_mask, 16, alpha_op); \
304 tile_8bpp_draw_##combine_op(index + 3, shift, 24, alpha_op) \
307 // Like the above, but draws the half-tile horizontally flipped
309 #define tile_8bpp_draw_four_flip(index, combine_op, alpha_op) \
310 tile_8bpp_draw_##combine_op(index + 3, mask, 0, alpha_op); \
311 tile_8bpp_draw_##combine_op(index + 2, shift_mask, 8, alpha_op); \
312 tile_8bpp_draw_##combine_op(index + 1, shift_mask, 16, alpha_op); \
313 tile_8bpp_draw_##combine_op(index + 0, shift, 24, alpha_op) \
315 #define tile_8bpp_draw_four_base(index, alpha_op, flip_op) \
316 tile_8bpp_draw_four_##flip_op(index, base, alpha_op) \
319 // Draw half of a tile in 8bpp mode, for transparent renderer; as an
320 // optimization the entire thing is checked against zero (in transparent
321 // capable renders it is more likely for the pixels to be transparent than
324 #define tile_8bpp_draw_four_transparent(index, alpha_op, flip_op) \
325 if(current_pixels != 0) \
327 tile_8bpp_draw_four_##flip_op(index, transparent, alpha_op); \
330 #define tile_8bpp_draw_four_copy(index, alpha_op, flip_op) \
331 if(current_pixels != 0) \
333 tile_8bpp_draw_four_##flip_op(index, copy, alpha_op); \
336 // Helper macro for drawing 8bpp tiles clipped against the edge of the screen
338 #define partial_tile_8bpp(combine_op, alpha_op) \
339 for(i = 0; i < partial_tile_run; i++) \
341 tile_8bpp_draw_##combine_op(0, mask, 0, alpha_op); \
342 current_pixels >>= 8; \
343 advance_dest_ptr_##combine_op(1); \
347 // Draws 8bpp tiles clipped against the left side of the screen,
348 // partial_tile_offset indicates how much clipped in it is, partial_tile_run
349 // indicates how much it should draw.
351 #define partial_tile_right_noflip_8bpp(combine_op, alpha_op) \
352 if(partial_tile_offset >= 4) \
354 current_pixels = *((u32 *)(tile_ptr + 4)) >> \
355 ((partial_tile_offset - 4) * 8); \
356 partial_tile_8bpp(combine_op, alpha_op); \
360 partial_tile_run -= 4; \
361 current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 8); \
362 partial_tile_8bpp(combine_op, alpha_op); \
363 current_pixels = *((u32 *)(tile_ptr + 4)); \
364 tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
365 advance_dest_ptr_##combine_op(4); \
369 // Draws 8bpp tiles clipped against both the left and right side of the
370 // screen, IE, runs of less than 8 - partial_tile_offset.
372 #define partial_tile_mid_noflip_8bpp(combine_op, alpha_op) \
373 if(partial_tile_offset >= 4) \
375 current_pixels = *((u32 *)(tile_ptr + 4)) >> \
376 ((partial_tile_offset - 4) * 8); \
377 partial_tile_8bpp(combine_op, alpha_op); \
381 current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 8); \
382 if((partial_tile_offset + partial_tile_run) > 4) \
384 u32 old_run = partial_tile_run; \
385 partial_tile_run = 4 - partial_tile_offset; \
386 partial_tile_8bpp(combine_op, alpha_op); \
387 partial_tile_run = old_run - partial_tile_run; \
388 current_pixels = *((u32 *)(tile_ptr + 4)); \
389 partial_tile_8bpp(combine_op, alpha_op); \
393 partial_tile_8bpp(combine_op, alpha_op); \
398 // Draws 8bpp tiles clipped against the right side of the screen,
399 // partial_tile_run indicates how much there is to draw.
401 #define partial_tile_left_noflip_8bpp(combine_op, alpha_op) \
402 if(partial_tile_run >= 4) \
404 current_pixels = *((u32 *)tile_ptr); \
405 tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
406 advance_dest_ptr_##combine_op(4); \
408 partial_tile_run -= 4; \
411 current_pixels = *((u32 *)(tile_ptr)); \
412 partial_tile_8bpp(combine_op, alpha_op) \
415 // Draws a non-clipped (complete) 8bpp tile.
417 #define tile_noflip_8bpp(combine_op, alpha_op) \
418 current_pixels = *((u32 *)tile_ptr); \
419 tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
420 current_pixels = *((u32 *)(tile_ptr + 4)); \
421 tile_8bpp_draw_four_##combine_op(4, alpha_op, noflip) \
424 // Like the above versions but draws flipped tiles.
426 #define partial_tile_flip_8bpp(combine_op, alpha_op) \
427 for(i = 0; i < partial_tile_run; i++) \
429 tile_8bpp_draw_##combine_op(0, shift, 24, alpha_op); \
430 current_pixels <<= 8; \
431 advance_dest_ptr_##combine_op(1); \
434 #define partial_tile_right_flip_8bpp(combine_op, alpha_op) \
435 if(partial_tile_offset >= 4) \
437 current_pixels = *((u32 *)tile_ptr) << ((partial_tile_offset - 4) * 8); \
438 partial_tile_flip_8bpp(combine_op, alpha_op); \
442 partial_tile_run -= 4; \
443 current_pixels = *((u32 *)(tile_ptr + 4)) << \
444 ((partial_tile_offset - 4) * 8); \
445 partial_tile_flip_8bpp(combine_op, alpha_op); \
446 current_pixels = *((u32 *)tile_ptr); \
447 tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
448 advance_dest_ptr_##combine_op(4); \
451 #define partial_tile_mid_flip_8bpp(combine_op, alpha_op) \
452 if(partial_tile_offset >= 4) \
454 current_pixels = *((u32 *)tile_ptr) << ((partial_tile_offset - 4) * 8); \
455 partial_tile_flip_8bpp(combine_op, alpha_op); \
459 current_pixels = *((u32 *)(tile_ptr + 4)) << \
460 ((partial_tile_offset - 4) * 8); \
462 if((partial_tile_offset + partial_tile_run) > 4) \
464 u32 old_run = partial_tile_run; \
465 partial_tile_run = 4 - partial_tile_offset; \
466 partial_tile_flip_8bpp(combine_op, alpha_op); \
467 partial_tile_run = old_run - partial_tile_run; \
468 current_pixels = *((u32 *)(tile_ptr)); \
469 partial_tile_flip_8bpp(combine_op, alpha_op); \
473 partial_tile_flip_8bpp(combine_op, alpha_op); \
477 #define partial_tile_left_flip_8bpp(combine_op, alpha_op) \
478 if(partial_tile_run >= 4) \
480 current_pixels = *((u32 *)(tile_ptr + 4)); \
481 tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
482 advance_dest_ptr_##combine_op(4); \
484 partial_tile_run -= 4; \
487 current_pixels = *((u32 *)(tile_ptr + 4)); \
488 partial_tile_flip_8bpp(combine_op, alpha_op) \
490 #define tile_flip_8bpp(combine_op, alpha_op) \
491 current_pixels = *((u32 *)(tile_ptr + 4)); \
492 tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
493 current_pixels = *((u32 *)tile_ptr); \
494 tile_8bpp_draw_four_##combine_op(4, alpha_op, flip) \
497 // Operations for isolating 4bpp tiles in a 32bit block
499 #define tile_4bpp_pixel_op_mask(op_param) \
500 current_pixel = current_pixels & 0x0F \
502 #define tile_4bpp_pixel_op_shift_mask(shift) \
503 current_pixel = (current_pixels >> shift) & 0x0F \
505 #define tile_4bpp_pixel_op_shift(shift) \
506 current_pixel = current_pixels >> shift \
508 #define tile_4bpp_pixel_op_none(op_param) \
510 // Draws a single 4bpp pixel as base, normal renderer; checks to see if the
511 // pixel is zero because if so the current palette should not be applied.
512 // These ifs can be replaced with a lookup table, may or may not be superior
513 // this way, should be benchmarked. The lookup table would be from 0-255
514 // identity map except for multiples of 16, which would map to 0.
516 #define tile_4bpp_draw_base_normal(index) \
519 current_pixel |= current_palette; \
520 tile_expand_base_normal(index); \
524 tile_expand_base_normal(index); \
528 #define tile_4bpp_draw_base_alpha(index) \
531 current_pixel |= current_palette; \
532 tile_expand_base_alpha(index); \
536 tile_expand_base_bg(index); \
539 #define tile_4bpp_draw_base_color16(index) \
540 tile_4bpp_draw_base_alpha(index) \
542 #define tile_4bpp_draw_base_color32(index) \
543 tile_4bpp_draw_base_alpha(index) \
546 #define tile_4bpp_draw_base(index, op, op_param, alpha_op) \
547 tile_4bpp_pixel_op_##op(op_param); \
548 tile_4bpp_draw_base_##alpha_op(index) \
551 // Draws a single 4bpp pixel as layered, if not transparent.
553 #define tile_4bpp_draw_transparent(index, op, op_param, alpha_op) \
554 tile_4bpp_pixel_op_##op(op_param); \
557 current_pixel |= current_palette; \
558 tile_expand_transparent_##alpha_op(index); \
561 #define tile_4bpp_draw_copy(index, op, op_param, alpha_op) \
562 tile_4bpp_pixel_op_##op(op_param); \
565 current_pixel |= current_palette; \
566 tile_expand_copy(index); \
570 // Draws eight background pixels in transparent mode, for alpha or normal
573 #define tile_4bpp_draw_eight_base_zero(value) \
574 dest_ptr[0] = value; \
575 dest_ptr[1] = value; \
576 dest_ptr[2] = value; \
577 dest_ptr[3] = value; \
578 dest_ptr[4] = value; \
579 dest_ptr[5] = value; \
580 dest_ptr[6] = value; \
581 dest_ptr[7] = value \
584 // Draws eight background pixels for the alpha renderer, basically color zero
585 // with the background flag high.
587 #define tile_4bpp_draw_eight_base_zero_alpha() \
588 tile_4bpp_draw_eight_base_zero(bg_combine) \
590 #define tile_4bpp_draw_eight_base_zero_color16() \
591 tile_4bpp_draw_eight_base_zero_alpha() \
593 #define tile_4bpp_draw_eight_base_zero_color32() \
594 tile_4bpp_draw_eight_base_zero_alpha() \
597 // Draws eight background pixels for the normal renderer, just a bunch of
600 #ifdef RENDER_COLOR16_NORMAL
602 #define tile_4bpp_draw_eight_base_zero_normal() \
604 tile_4bpp_draw_eight_base_zero(current_pixel) \
608 #define tile_4bpp_draw_eight_base_zero_normal() \
609 current_pixel = palette[0]; \
610 tile_4bpp_draw_eight_base_zero(current_pixel) \
615 // Draws eight 4bpp pixels.
617 #define tile_4bpp_draw_eight_noflip(combine_op, alpha_op) \
618 tile_4bpp_draw_##combine_op(0, mask, 0, alpha_op); \
619 tile_4bpp_draw_##combine_op(1, shift_mask, 4, alpha_op); \
620 tile_4bpp_draw_##combine_op(2, shift_mask, 8, alpha_op); \
621 tile_4bpp_draw_##combine_op(3, shift_mask, 12, alpha_op); \
622 tile_4bpp_draw_##combine_op(4, shift_mask, 16, alpha_op); \
623 tile_4bpp_draw_##combine_op(5, shift_mask, 20, alpha_op); \
624 tile_4bpp_draw_##combine_op(6, shift_mask, 24, alpha_op); \
625 tile_4bpp_draw_##combine_op(7, shift, 28, alpha_op) \
628 // Draws eight 4bpp pixels in reverse order (for hflip).
630 #define tile_4bpp_draw_eight_flip(combine_op, alpha_op) \
631 tile_4bpp_draw_##combine_op(7, mask, 0, alpha_op); \
632 tile_4bpp_draw_##combine_op(6, shift_mask, 4, alpha_op); \
633 tile_4bpp_draw_##combine_op(5, shift_mask, 8, alpha_op); \
634 tile_4bpp_draw_##combine_op(4, shift_mask, 12, alpha_op); \
635 tile_4bpp_draw_##combine_op(3, shift_mask, 16, alpha_op); \
636 tile_4bpp_draw_##combine_op(2, shift_mask, 20, alpha_op); \
637 tile_4bpp_draw_##combine_op(1, shift_mask, 24, alpha_op); \
638 tile_4bpp_draw_##combine_op(0, shift, 28, alpha_op) \
641 // Draws eight 4bpp pixels in base mode, checks if all are zero, if so draws
642 // the appropriate background pixels.
644 #define tile_4bpp_draw_eight_base(alpha_op, flip_op) \
645 if(current_pixels != 0) \
647 tile_4bpp_draw_eight_##flip_op(base, alpha_op); \
651 tile_4bpp_draw_eight_base_zero_##alpha_op(); \
655 // Draws eight 4bpp pixels in transparent (layered) mode, checks if all are
656 // zero and if so draws nothing.
658 #define tile_4bpp_draw_eight_transparent(alpha_op, flip_op) \
659 if(current_pixels != 0) \
661 tile_4bpp_draw_eight_##flip_op(transparent, alpha_op); \
665 #define tile_4bpp_draw_eight_copy(alpha_op, flip_op) \
666 if(current_pixels != 0) \
668 tile_4bpp_draw_eight_##flip_op(copy, alpha_op); \
671 // Gets the current tile in 4bpp mode, also getting the current palette and
674 #define get_tile_4bpp() \
675 current_tile = *map_ptr; \
676 current_palette = (current_tile >> 12) << 4; \
677 tile_ptr = tile_base + ((current_tile & 0x3FF) * 32); \
680 // Helper macro for drawing clipped 4bpp tiles.
682 #define partial_tile_4bpp(combine_op, alpha_op) \
683 for(i = 0; i < partial_tile_run; i++) \
685 tile_4bpp_draw_##combine_op(0, mask, 0, alpha_op); \
686 current_pixels >>= 4; \
687 advance_dest_ptr_##combine_op(1); \
691 // Draws a 4bpp tile clipped against the left edge of the screen.
692 // partial_tile_offset is how far in it's clipped, partial_tile_run is
695 #define partial_tile_right_noflip_4bpp(combine_op, alpha_op) \
696 current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 4); \
697 partial_tile_4bpp(combine_op, alpha_op) \
700 // Draws a 4bpp tile clipped against both edges of the screen, same as right.
702 #define partial_tile_mid_noflip_4bpp(combine_op, alpha_op) \
703 partial_tile_right_noflip_4bpp(combine_op, alpha_op) \
706 // Draws a 4bpp tile clipped against the right edge of the screen.
707 // partial_tile_offset is how many to draw.
709 #define partial_tile_left_noflip_4bpp(combine_op, alpha_op) \
710 current_pixels = *((u32 *)tile_ptr); \
711 partial_tile_4bpp(combine_op, alpha_op) \
714 // Draws a complete 4bpp tile row (not clipped)
715 #define tile_noflip_4bpp(combine_op, alpha_op) \
716 current_pixels = *((u32 *)tile_ptr); \
717 tile_4bpp_draw_eight_##combine_op(alpha_op, noflip) \
720 // Like the above, but draws flipped tiles.
722 #define partial_tile_flip_4bpp(combine_op, alpha_op) \
723 for(i = 0; i < partial_tile_run; i++) \
725 tile_4bpp_draw_##combine_op(0, shift, 28, alpha_op); \
726 current_pixels <<= 4; \
727 advance_dest_ptr_##combine_op(1); \
730 #define partial_tile_right_flip_4bpp(combine_op, alpha_op) \
731 current_pixels = *((u32 *)tile_ptr) << (partial_tile_offset * 4); \
732 partial_tile_flip_4bpp(combine_op, alpha_op) \
734 #define partial_tile_mid_flip_4bpp(combine_op, alpha_op) \
735 partial_tile_right_flip_4bpp(combine_op, alpha_op) \
737 #define partial_tile_left_flip_4bpp(combine_op, alpha_op) \
738 current_pixels = *((u32 *)tile_ptr); \
739 partial_tile_flip_4bpp(combine_op, alpha_op) \
741 #define tile_flip_4bpp(combine_op, alpha_op) \
742 current_pixels = *((u32 *)tile_ptr); \
743 tile_4bpp_draw_eight_##combine_op(alpha_op, flip) \
746 // Draws a single (partial or complete) tile from the tilemap, flipping
749 #define single_tile_map(tile_type, combine_op, color_depth, alpha_op) \
750 get_tile_##color_depth(); \
751 if(current_tile & 0x800) \
752 tile_ptr += vertical_pixel_flip; \
754 if(current_tile & 0x400) \
756 tile_type##_flip_##color_depth(combine_op, alpha_op); \
760 tile_type##_noflip_##color_depth(combine_op, alpha_op); \
764 // Draws multiple sequential tiles from the tilemap, hflips and vflips as
767 #define multiple_tile_map(combine_op, color_depth, alpha_op) \
768 for(i = 0; i < tile_run; i++) \
770 single_tile_map(tile, combine_op, color_depth, alpha_op); \
771 advance_dest_ptr_##combine_op(8); \
775 // Draws a partial tile from a tilemap clipped against the left edge of the
778 #define partial_tile_right_map(combine_op, color_depth, alpha_op) \
779 single_tile_map(partial_tile_right, combine_op, color_depth, alpha_op); \
782 // Draws a partial tile from a tilemap clipped against both edges of the
785 #define partial_tile_mid_map(combine_op, color_depth, alpha_op) \
786 single_tile_map(partial_tile_mid, combine_op, color_depth, alpha_op) \
788 // Draws a partial tile from a tilemap clipped against the right edge of the
791 #define partial_tile_left_map(combine_op, color_depth, alpha_op) \
792 single_tile_map(partial_tile_left, combine_op, color_depth, alpha_op) \
795 // Advances a non-flipped 4bpp obj to the next tile.
797 #define obj_advance_noflip_4bpp() \
801 // Advances a non-flipped 8bpp obj to the next tile.
803 #define obj_advance_noflip_8bpp() \
807 // Advances a flipped 4bpp obj to the next tile.
809 #define obj_advance_flip_4bpp() \
813 // Advances a flipped 8bpp obj to the next tile.
815 #define obj_advance_flip_8bpp() \
820 // Draws multiple sequential tiles from an obj, flip_op determines if it should
821 // be flipped or not (set to flip or noflip)
823 #define multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op) \
824 for(i = 0; i < tile_run; i++) \
826 tile_##flip_op##_##color_depth(combine_op, alpha_op); \
827 obj_advance_##flip_op##_##color_depth(); \
828 advance_dest_ptr_##combine_op(8); \
832 // Draws an obj's tile clipped against the left side of the screen
834 #define partial_tile_right_obj(combine_op, color_depth, alpha_op, flip_op) \
835 partial_tile_right_##flip_op##_##color_depth(combine_op, alpha_op); \
836 obj_advance_##flip_op##_##color_depth() \
838 // Draws an obj's tile clipped against both sides of the screen
840 #define partial_tile_mid_obj(combine_op, color_depth, alpha_op, flip_op) \
841 partial_tile_mid_##flip_op##_##color_depth(combine_op, alpha_op) \
843 // Draws an obj's tile clipped against the right side of the screen
845 #define partial_tile_left_obj(combine_op, color_depth, alpha_op, flip_op) \
846 partial_tile_left_##flip_op##_##color_depth(combine_op, alpha_op) \
849 // Extra variables specific for 8bpp/4bpp tile renderers.
851 #define tile_extra_variables_8bpp() \
853 #define tile_extra_variables_4bpp() \
854 u32 current_palette \
857 // Byte lengths of complete tiles and tile rows in 4bpp and 8bpp.
859 #define tile_width_4bpp 4
860 #define tile_size_4bpp 32
861 #define tile_width_8bpp 8
862 #define tile_size_8bpp 64
865 // Render a single scanline of text tiles
867 #define tile_render(color_depth, combine_op, alpha_op) \
869 u32 vertical_pixel_offset = (vertical_offset % 8) * \
870 tile_width_##color_depth; \
871 u32 vertical_pixel_flip = \
872 ((tile_size_##color_depth - tile_width_##color_depth) - \
873 vertical_pixel_offset) - vertical_pixel_offset; \
874 tile_extra_variables_##color_depth(); \
875 u8 *tile_base = vram + (((bg_control >> 2) & 0x03) * (1024 * 16)) + \
876 vertical_pixel_offset; \
877 u32 pixel_run = 256 - (horizontal_offset % 256); \
880 map_base += ((vertical_offset % 256) / 8) * 32; \
881 partial_tile_offset = (horizontal_offset % 8); \
883 if(pixel_run >= end) \
885 if(partial_tile_offset) \
887 partial_tile_run = 8 - partial_tile_offset; \
888 if(end < partial_tile_run) \
890 partial_tile_run = end; \
891 partial_tile_mid_map(combine_op, color_depth, alpha_op); \
896 end -= partial_tile_run; \
897 partial_tile_right_map(combine_op, color_depth, alpha_op); \
901 tile_run = end / 8; \
902 multiple_tile_map(combine_op, color_depth, alpha_op); \
904 partial_tile_run = end % 8; \
906 if(partial_tile_run) \
908 partial_tile_left_map(combine_op, color_depth, alpha_op); \
913 if(partial_tile_offset) \
915 partial_tile_run = 8 - partial_tile_offset; \
916 partial_tile_right_map(combine_op, color_depth, alpha_op); \
919 tile_run = (pixel_run - partial_tile_run) / 8; \
920 multiple_tile_map(combine_op, color_depth, alpha_op); \
921 map_ptr = second_ptr; \
923 tile_run = end / 8; \
924 multiple_tile_map(combine_op, color_depth, alpha_op); \
926 partial_tile_run = end % 8; \
927 if(partial_tile_run) \
929 partial_tile_left_map(combine_op, color_depth, alpha_op); \
934 #define render_scanline_dest_normal u16
935 #define render_scanline_dest_alpha u32
936 #define render_scanline_dest_alpha_obj u32
937 #define render_scanline_dest_color16 u16
938 #define render_scanline_dest_color32 u32
939 #define render_scanline_dest_partial_alpha u32
940 #define render_scanline_dest_copy_tile u16
941 #define render_scanline_dest_copy_bitmap u16
944 // If rendering a scanline that is not a target A then there's no point in
945 // keeping what's underneath it because it can't blend with it.
947 #define render_scanline_skip_alpha(bg_type, combine_op) \
948 if((pixel_combine & 0x00000200) == 0) \
950 render_scanline_##bg_type##_##combine_op##_color32(layer, \
951 start, end, scanline); \
956 #ifdef RENDER_COLOR16_NORMAL
958 #define render_scanline_extra_variables_base_normal(bg_type) \
959 const u32 pixel_combine = 0 \
963 #define render_scanline_extra_variables_base_normal(bg_type) \
964 u16 *palette = palette_ram_converted \
969 #define render_scanline_extra_variables_base_alpha(bg_type) \
970 u32 bg_combine = color_combine_mask(5); \
971 u32 pixel_combine = color_combine_mask(layer) | (bg_combine << 16); \
972 render_scanline_skip_alpha(bg_type, base) \
974 #define render_scanline_extra_variables_base_color() \
975 u32 bg_combine = color_combine_mask(5); \
976 u32 pixel_combine = color_combine_mask(layer) \
978 #define render_scanline_extra_variables_base_color16(bg_type) \
979 render_scanline_extra_variables_base_color() \
981 #define render_scanline_extra_variables_base_color32(bg_type) \
982 render_scanline_extra_variables_base_color() \
985 #define render_scanline_extra_variables_transparent_normal(bg_type) \
986 render_scanline_extra_variables_base_normal(bg_type) \
988 #define render_scanline_extra_variables_transparent_alpha(bg_type) \
989 u32 pixel_combine = color_combine_mask(layer); \
990 render_scanline_skip_alpha(bg_type, transparent) \
992 #define render_scanline_extra_variables_transparent_color() \
993 u32 pixel_combine = color_combine_mask(layer) \
995 #define render_scanline_extra_variables_transparent_color16(bg_type) \
996 render_scanline_extra_variables_transparent_color() \
998 #define render_scanline_extra_variables_transparent_color32(bg_type) \
999 render_scanline_extra_variables_transparent_color() \
1005 // Map widths and heights
1007 u32 map_widths[] = { 256, 512, 256, 512 };
1008 u32 map_heights[] = { 256, 256, 512, 512 };
1010 // Build text scanline rendering functions.
1012 #define render_scanline_text_builder(combine_op, alpha_op) \
1013 void render_scanline_text_##combine_op##_##alpha_op(u32 layer, \
1014 u32 start, u32 end, void *scanline) \
1016 render_scanline_extra_variables_##combine_op##_##alpha_op(text); \
1017 u32 bg_control = io_registers[REG_BG0CNT + layer]; \
1018 u32 map_size = (bg_control >> 14) & 0x03; \
1019 u32 map_width = map_widths[map_size]; \
1020 u32 map_height = map_heights[map_size]; \
1021 u32 horizontal_offset = \
1022 (io_registers[REG_BG0HOFS + (layer * 2)] + start) % 512; \
1023 u32 vertical_offset = (io_registers[REG_VCOUNT] + \
1024 io_registers[REG_BG0VOFS + (layer * 2)]) % 512; \
1025 u32 current_pixel; \
1026 u32 current_pixels; \
1027 u32 partial_tile_run = 0; \
1028 u32 partial_tile_offset; \
1031 render_scanline_dest_##alpha_op *dest_ptr = \
1032 ((render_scanline_dest_##alpha_op *)scanline) + start; \
1034 u16 *map_base = (u16 *)(vram + ((bg_control >> 8) & 0x1F) * (1024 * 2)); \
1035 u16 *map_ptr, *second_ptr; \
1040 if((map_size & 0x02) && (vertical_offset >= 256)) \
1042 map_base += ((map_width / 8) * 32) + \
1043 (((vertical_offset - 256) / 8) * 32); \
1047 map_base += (((vertical_offset % 256) / 8) * 32); \
1050 if(map_size & 0x01) \
1052 if(horizontal_offset >= 256) \
1054 horizontal_offset -= 256; \
1055 map_ptr = map_base + (32 * 32) + (horizontal_offset / 8); \
1056 second_ptr = map_base; \
1060 map_ptr = map_base + (horizontal_offset / 8); \
1061 second_ptr = map_base + (32 * 32); \
1066 horizontal_offset %= 256; \
1067 map_ptr = map_base + (horizontal_offset / 8); \
1068 second_ptr = map_base; \
1071 if(bg_control & 0x80) \
1073 tile_render(8bpp, combine_op, alpha_op); \
1077 tile_render(4bpp, combine_op, alpha_op); \
1081 render_scanline_text_builder(base, normal);
1082 render_scanline_text_builder(transparent, normal);
1083 render_scanline_text_builder(base, color16);
1084 render_scanline_text_builder(transparent, color16);
1085 render_scanline_text_builder(base, color32);
1086 render_scanline_text_builder(transparent, color32);
1087 render_scanline_text_builder(base, alpha);
1088 render_scanline_text_builder(transparent, alpha);
1091 s32 affine_reference_x[2];
1092 s32 affine_reference_y[2];
1094 #define affine_render_bg_pixel_normal() \
1095 current_pixel = palette_ram_converted[0] \
1097 #define affine_render_bg_pixel_alpha() \
1098 current_pixel = bg_combine \
1100 #define affine_render_bg_pixel_color16() \
1101 affine_render_bg_pixel_alpha() \
1103 #define affine_render_bg_pixel_color32() \
1104 affine_render_bg_pixel_alpha() \
1106 #define affine_render_bg_pixel_base(alpha_op) \
1107 affine_render_bg_pixel_##alpha_op() \
1109 #define affine_render_bg_pixel_transparent(alpha_op) \
1111 #define affine_render_bg_pixel_copy(alpha_op) \
1113 #define affine_render_bg_base(alpha_op) \
1114 dest_ptr[0] = current_pixel
1116 #define affine_render_bg_transparent(alpha_op) \
1118 #define affine_render_bg_copy(alpha_op) \
1120 #define affine_render_bg_remainder_base(alpha_op) \
1121 affine_render_bg_pixel_##alpha_op(); \
1122 for(; i < end; i++) \
1124 affine_render_bg_base(alpha_op); \
1125 advance_dest_ptr_base(1); \
1128 #define affine_render_bg_remainder_transparent(alpha_op) \
1130 #define affine_render_bg_remainder_copy(alpha_op) \
1132 #define affine_render_next(combine_op) \
1135 advance_dest_ptr_##combine_op(1) \
1137 #define affine_render_scale_offset() \
1138 tile_base += ((pixel_y % 8) * 8); \
1139 map_base += (pixel_y / 8) << map_pitch \
1141 #define affine_render_scale_pixel(combine_op, alpha_op) \
1142 map_offset = (pixel_x / 8); \
1143 if(map_offset != last_map_offset) \
1145 tile_ptr = tile_base + (map_base[map_offset] * 64); \
1146 last_map_offset = map_offset; \
1148 tile_ptr = tile_base + (map_base[(pixel_x / 8)] * 64); \
1149 current_pixel = tile_ptr[(pixel_x % 8)]; \
1150 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op); \
1151 affine_render_next(combine_op) \
1153 #define affine_render_scale(combine_op, alpha_op) \
1155 pixel_y = source_y >> 8; \
1157 affine_render_bg_pixel_##combine_op(alpha_op); \
1158 if((u32)pixel_y < (u32)width_height) \
1160 affine_render_scale_offset(); \
1161 for(; i < end; i++) \
1163 pixel_x = source_x >> 8; \
1165 if((u32)pixel_x < (u32)width_height) \
1170 affine_render_bg_##combine_op(alpha_op); \
1171 affine_render_next(combine_op); \
1174 for(; i < end; i++) \
1176 pixel_x = source_x >> 8; \
1178 if((u32)pixel_x >= (u32)width_height) \
1181 affine_render_scale_pixel(combine_op, alpha_op); \
1184 affine_render_bg_remainder_##combine_op(alpha_op); \
1187 #define affine_render_scale_wrap(combine_op, alpha_op) \
1189 u32 wrap_mask = width_height - 1; \
1190 pixel_y = (source_y >> 8) & wrap_mask; \
1191 if((u32)pixel_y < (u32)width_height) \
1193 affine_render_scale_offset(); \
1194 for(i = 0; i < end; i++) \
1196 pixel_x = (source_x >> 8) & wrap_mask; \
1197 affine_render_scale_pixel(combine_op, alpha_op); \
1203 #define affine_render_rotate_pixel(combine_op, alpha_op) \
1204 map_offset = (pixel_x / 8) + ((pixel_y / 8) << map_pitch); \
1205 if(map_offset != last_map_offset) \
1207 tile_ptr = tile_base + (map_base[map_offset] * 64); \
1208 last_map_offset = map_offset; \
1211 current_pixel = tile_ptr[(pixel_x % 8) + ((pixel_y % 8) * 8)]; \
1212 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op); \
1213 affine_render_next(combine_op) \
1215 #define affine_render_rotate(combine_op, alpha_op) \
1217 affine_render_bg_pixel_##combine_op(alpha_op); \
1218 for(i = 0; i < end; i++) \
1220 pixel_x = source_x >> 8; \
1221 pixel_y = source_y >> 8; \
1223 if(((u32)pixel_x < (u32)width_height) && \
1224 ((u32)pixel_y < (u32)width_height)) \
1228 affine_render_bg_##combine_op(alpha_op); \
1229 affine_render_next(combine_op); \
1232 for(; i < end; i++) \
1234 pixel_x = source_x >> 8; \
1235 pixel_y = source_y >> 8; \
1237 if(((u32)pixel_x >= (u32)width_height) || \
1238 ((u32)pixel_y >= (u32)width_height)) \
1240 affine_render_bg_remainder_##combine_op(alpha_op); \
1244 affine_render_rotate_pixel(combine_op, alpha_op); \
1248 #define affine_render_rotate_wrap(combine_op, alpha_op) \
1250 u32 wrap_mask = width_height - 1; \
1251 for(i = 0; i < end; i++) \
1253 pixel_x = (source_x >> 8) & wrap_mask; \
1254 pixel_y = (source_y >> 8) & wrap_mask; \
1256 affine_render_rotate_pixel(combine_op, alpha_op); \
1261 // Build affine background renderers.
1263 #define render_scanline_affine_builder(combine_op, alpha_op) \
1264 void render_scanline_affine_##combine_op##_##alpha_op(u32 layer, \
1265 u32 start, u32 end, void *scanline) \
1267 render_scanline_extra_variables_##combine_op##_##alpha_op(affine); \
1268 u32 bg_control = io_registers[REG_BG0CNT + layer]; \
1269 u32 current_pixel; \
1270 s32 source_x, source_y; \
1271 u32 vcount = io_registers[REG_VCOUNT]; \
1272 u32 pixel_x, pixel_y; \
1273 u32 layer_offset = (layer - 2) * 8; \
1275 u32 map_size = (bg_control >> 14) & 0x03; \
1276 u32 width_height = 1 << (7 + map_size); \
1277 u32 map_pitch = map_size + 4; \
1278 u8 *map_base = vram + (((bg_control >> 8) & 0x1F) * (1024 * 2)); \
1279 u8 *tile_base = vram + (((bg_control >> 2) & 0x03) * (1024 * 16)); \
1281 u32 map_offset, last_map_offset = (u32)-1; \
1283 render_scanline_dest_##alpha_op *dest_ptr = \
1284 ((render_scanline_dest_##alpha_op *)scanline) + start; \
1286 dx = (s16)io_registers[REG_BG2PA + layer_offset]; \
1287 dy = (s16)io_registers[REG_BG2PC + layer_offset]; \
1288 source_x = affine_reference_x[layer - 2] + (start * dx); \
1289 source_y = affine_reference_y[layer - 2] + (start * dy); \
1293 switch(((bg_control >> 12) & 0x02) | (dy != 0)) \
1296 affine_render_scale(combine_op, alpha_op); \
1300 affine_render_rotate(combine_op, alpha_op); \
1304 affine_render_scale_wrap(combine_op, alpha_op); \
1308 affine_render_rotate_wrap(combine_op, alpha_op); \
1313 render_scanline_affine_builder(base, normal);
1314 render_scanline_affine_builder(transparent, normal);
1315 render_scanline_affine_builder(base, color16);
1316 render_scanline_affine_builder(transparent, color16);
1317 render_scanline_affine_builder(base, color32);
1318 render_scanline_affine_builder(transparent, color32);
1319 render_scanline_affine_builder(base, alpha);
1320 render_scanline_affine_builder(transparent, alpha);
1323 #define bitmap_render_pixel_mode3(alpha_op) \
1324 convert_palette(current_pixel); \
1325 *dest_ptr = current_pixel \
1327 #define bitmap_render_pixel_mode4(alpha_op) \
1328 tile_expand_base_##alpha_op(0) \
1330 #define bitmap_render_pixel_mode5(alpha_op) \
1331 bitmap_render_pixel_mode3(alpha_op) \
1334 #define bitmap_render_scale(type, alpha_op, width, height) \
1335 pixel_y = (source_y >> 8); \
1336 if((u32)pixel_y < (u32)height) \
1338 pixel_x = (source_x >> 8); \
1339 src_ptr += (pixel_y * width); \
1345 dest_ptr -= pixel_x; \
1352 src_ptr += pixel_x; \
1355 if((pixel_x + end) >= width) \
1356 end = (width - pixel_x); \
1358 for(i = 0; (s32)i < (s32)end; i++) \
1360 current_pixel = *src_ptr; \
1361 bitmap_render_pixel_##type(alpha_op); \
1368 if((u32)(source_y >> 8) < (u32)height) \
1370 for(i = 0; i < end; i++) \
1372 pixel_x = (source_x >> 8); \
1374 if((u32)pixel_x < (u32)width) \
1381 for(; i < end; i++) \
1383 pixel_x = (source_x >> 8); \
1385 if((u32)pixel_x >= (u32)width) \
1388 current_pixel = src_ptr[pixel_x]; \
1389 bitmap_render_pixel_##type(alpha_op); \
1398 #define bitmap_render_rotate(type, alpha_op, width, height) \
1399 for(i = 0; i < end; i++) \
1401 pixel_x = source_x >> 8; \
1402 pixel_y = source_y >> 8; \
1404 if(((u32)pixel_x < (u32)width) && ((u32)pixel_y < (u32)height)) \
1412 for(; i < end; i++) \
1414 pixel_x = (source_x >> 8); \
1415 pixel_y = (source_y >> 8); \
1417 if(((u32)pixel_x >= (u32)width) || ((u32)pixel_y >= (u32)height)) \
1420 current_pixel = src_ptr[pixel_x + (pixel_y * width)]; \
1421 bitmap_render_pixel_##type(alpha_op); \
1429 #define render_scanline_vram_setup_mode3() \
1430 u16 *src_ptr = (u16 *)vram \
1432 #define render_scanline_vram_setup_mode5() \
1434 if(io_registers[REG_DISPCNT] & 0x10) \
1435 src_ptr = (u16 *)(vram + 0xA000); \
1437 src_ptr = (u16 *)vram \
1440 #ifdef RENDER_COLOR16_NORMAL
1442 #define render_scanline_vram_setup_mode4() \
1443 const u32 pixel_combine = 0; \
1445 if(io_registers[REG_DISPCNT] & 0x10) \
1446 src_ptr = vram + 0xA000; \
1453 #define render_scanline_vram_setup_mode4() \
1454 u16 *palette = palette_ram_converted; \
1456 if(io_registers[REG_DISPCNT] & 0x10) \
1457 src_ptr = vram + 0xA000; \
1465 // Build bitmap scanline rendering functions.
1467 #define render_scanline_bitmap_builder(type, alpha_op, width, height) \
1468 void render_scanline_bitmap_##type##_##alpha_op(u32 start, u32 end, \
1471 u32 bg_control = io_registers[REG_BG2CNT]; \
1472 u32 current_pixel; \
1473 s32 source_x, source_y; \
1474 u32 vcount = io_registers[REG_VCOUNT]; \
1475 s32 pixel_x, pixel_y; \
1477 s32 dx = (s16)io_registers[REG_BG2PA]; \
1478 s32 dy = (s16)io_registers[REG_BG2PC]; \
1482 render_scanline_dest_##alpha_op *dest_ptr = \
1483 ((render_scanline_dest_##alpha_op *)scanline) + start; \
1484 render_scanline_vram_setup_##type(); \
1488 source_x = affine_reference_x[0] + (start * dx); \
1489 source_y = affine_reference_y[0] + (start * dy); \
1493 bitmap_render_scale(type, alpha_op, width, height); \
1497 bitmap_render_rotate(type, alpha_op, width, height); \
1501 render_scanline_bitmap_builder(mode3, normal, 240, 160);
1502 render_scanline_bitmap_builder(mode4, normal, 240, 160);
1503 render_scanline_bitmap_builder(mode5, normal, 160, 128);
1506 // Fill in the renderers for a layer based on the mode type,
1508 #define tile_layer_render_functions(type) \
1510 render_scanline_##type##_base_normal, \
1511 render_scanline_##type##_transparent_normal, \
1512 render_scanline_##type##_base_alpha, \
1513 render_scanline_##type##_transparent_alpha, \
1514 render_scanline_##type##_base_color16, \
1515 render_scanline_##type##_transparent_color16, \
1516 render_scanline_##type##_base_color32, \
1517 render_scanline_##type##_transparent_color32 \
1521 // Use if a layer is unsupported for that mode.
1523 #define tile_layer_render_null() \
1525 NULL, NULL, NULL, NULL \
1528 #define bitmap_layer_render_functions(type) \
1530 render_scanline_bitmap_##type##_normal \
1533 // Structs containing functions to render the layers for each mode, for
1534 // each render type.
1535 tile_layer_render_struct tile_mode_renderers[3][4] =
1538 tile_layer_render_functions(text), tile_layer_render_functions(text),
1539 tile_layer_render_functions(text), tile_layer_render_functions(text)
1542 tile_layer_render_functions(text), tile_layer_render_functions(text),
1543 tile_layer_render_functions(affine), tile_layer_render_functions(text)
1546 tile_layer_render_functions(text), tile_layer_render_functions(text),
1547 tile_layer_render_functions(affine), tile_layer_render_functions(affine)
1551 bitmap_layer_render_struct bitmap_mode_renderers[3] =
1553 bitmap_layer_render_functions(mode3),
1554 bitmap_layer_render_functions(mode4),
1555 bitmap_layer_render_functions(mode5)
1559 #define render_scanline_layer_functions_tile() \
1560 tile_layer_render_struct *layer_renderers = \
1561 tile_mode_renderers[dispcnt & 0x07] \
1563 #define render_scanline_layer_functions_bitmap() \
1564 bitmap_layer_render_struct *layer_renderers = \
1565 bitmap_mode_renderers + ((dispcnt & 0x07) - 3) \
1568 // Adjust a flipped obj's starting position
1570 #define obj_tile_offset_noflip(color_depth) \
1572 #define obj_tile_offset_flip(color_depth) \
1573 + (tile_size_##color_depth * ((obj_width - 8) / 8)) \
1576 // Adjust the obj's starting point if it goes too far off the left edge of \
1579 #define obj_tile_right_offset_noflip(color_depth) \
1580 tile_ptr += (partial_tile_offset / 8) * tile_size_##color_depth \
1582 #define obj_tile_right_offset_flip(color_depth) \
1583 tile_ptr -= (partial_tile_offset / 8) * tile_size_##color_depth \
1585 // Get the current row offset into an obj in 1D map space
1587 #define obj_tile_offset_1D(color_depth, flip_op) \
1588 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1589 + ((vertical_offset / 8) * (obj_width / 8) * tile_size_##color_depth) \
1590 + ((vertical_offset % 8) * tile_width_##color_depth) \
1591 obj_tile_offset_##flip_op(color_depth) \
1593 // Get the current row offset into an obj in 2D map space
1595 #define obj_tile_offset_2D(color_depth, flip_op) \
1596 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1597 + ((vertical_offset / 8) * 1024) \
1598 + ((vertical_offset % 8) * tile_width_##color_depth) \
1599 obj_tile_offset_##flip_op(color_depth) \
1602 // Get the palette for 4bpp obj.
1604 #define obj_get_palette_4bpp() \
1605 current_palette = (obj_attribute_2 >> 8) & 0xF0 \
1607 #define obj_get_palette_8bpp() \
1610 // Render the current row of an obj.
1612 #define obj_render(combine_op, color_depth, alpha_op, map_space, flip_op) \
1614 obj_get_palette_##color_depth(); \
1615 obj_tile_offset_##map_space(color_depth, flip_op); \
1617 if(obj_x < (s32)start) \
1619 dest_ptr = scanline + start; \
1620 pixel_run = obj_width - (start - obj_x); \
1621 if((s32)pixel_run > 0) \
1623 if((obj_x + obj_width) >= end) \
1625 pixel_run = end - start; \
1626 partial_tile_offset = start - obj_x; \
1627 obj_tile_right_offset_##flip_op(color_depth); \
1628 partial_tile_offset %= 8; \
1630 if(partial_tile_offset) \
1632 partial_tile_run = 8 - partial_tile_offset; \
1633 if((s32)pixel_run < (s32)partial_tile_run) \
1635 if((s32)pixel_run > 0) \
1637 partial_tile_run = pixel_run; \
1638 partial_tile_mid_obj(combine_op, color_depth, alpha_op, \
1645 pixel_run -= partial_tile_run; \
1646 partial_tile_right_obj(combine_op, color_depth, alpha_op, \
1650 tile_run = pixel_run / 8; \
1651 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1652 partial_tile_run = pixel_run % 8; \
1653 if(partial_tile_run) \
1655 partial_tile_left_obj(combine_op, color_depth, alpha_op, \
1661 partial_tile_offset = start - obj_x; \
1662 obj_tile_right_offset_##flip_op(color_depth); \
1663 partial_tile_offset %= 8; \
1664 if(partial_tile_offset) \
1666 partial_tile_run = 8 - partial_tile_offset; \
1667 partial_tile_right_obj(combine_op, color_depth, alpha_op, \
1670 tile_run = pixel_run / 8; \
1671 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1677 if((obj_x + obj_width) >= end) \
1679 pixel_run = end - obj_x; \
1680 if((s32)pixel_run > 0) \
1682 dest_ptr = scanline + obj_x; \
1683 tile_run = pixel_run / 8; \
1684 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1685 partial_tile_run = pixel_run % 8; \
1686 if(partial_tile_run) \
1688 partial_tile_left_obj(combine_op, color_depth, alpha_op, flip_op); \
1694 dest_ptr = scanline + obj_x; \
1695 tile_run = obj_width / 8; \
1696 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1700 #define obj_scale_offset_1D(color_depth) \
1701 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1702 + ((vertical_offset / 8) * (max_x / 8) * tile_size_##color_depth) \
1703 + ((vertical_offset % 8) * tile_width_##color_depth) \
1705 // Get the current row offset into an obj in 2D map space
1707 #define obj_scale_offset_2D(color_depth) \
1708 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1709 + ((vertical_offset / 8) * 1024) \
1710 + ((vertical_offset % 8) * tile_width_##color_depth) \
1712 #define obj_render_scale_pixel_4bpp(combine_op, alpha_op) \
1715 current_pixel = tile_ptr[tile_map_offset + ((tile_x >> 1) & 0x03)] >> 4; \
1720 tile_ptr[tile_map_offset + ((tile_x >> 1) & 0x03)] & 0x0F; \
1723 tile_4bpp_draw_##combine_op(0, none, 0, alpha_op) \
1726 #define obj_render_scale_pixel_8bpp(combine_op, alpha_op) \
1727 current_pixel = tile_ptr[tile_map_offset + (tile_x & 0x07)]; \
1728 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op); \
1730 #define obj_render_scale(combine_op, color_depth, alpha_op, map_space) \
1732 u32 vertical_offset; \
1733 source_y += (y_delta * dmy); \
1734 vertical_offset = (source_y >> 8); \
1735 if((u32)vertical_offset < (u32)max_y) \
1737 obj_scale_offset_##map_space(color_depth); \
1738 source_x += (y_delta * dmx) - (middle_x * dx); \
1740 for(i = 0; i < obj_width; i++) \
1742 tile_x = (source_x >> 8); \
1744 if((u32)tile_x < (u32)max_x) \
1748 advance_dest_ptr_##combine_op(1); \
1751 for(; i < obj_width; i++) \
1753 tile_x = (source_x >> 8); \
1755 if((u32)tile_x >= (u32)max_x) \
1758 tile_map_offset = (tile_x >> 3) * tile_size_##color_depth; \
1759 obj_render_scale_pixel_##color_depth(combine_op, alpha_op); \
1762 advance_dest_ptr_##combine_op(1); \
1768 #define obj_rotate_offset_1D(color_depth) \
1769 obj_tile_pitch = (max_x / 8) * tile_size_##color_depth \
1771 #define obj_rotate_offset_2D(color_depth) \
1772 obj_tile_pitch = 1024 \
1774 #define obj_render_rotate_pixel_4bpp(combine_op, alpha_op) \
1777 current_pixel = tile_ptr[tile_map_offset + \
1778 ((tile_x >> 1) & 0x03) + ((tile_y & 0x07) * obj_pitch)] >> 4; \
1782 current_pixel = tile_ptr[tile_map_offset + \
1783 ((tile_x >> 1) & 0x03) + ((tile_y & 0x07) * obj_pitch)] & 0x0F; \
1786 tile_4bpp_draw_##combine_op(0, none, 0, alpha_op) \
1788 #define obj_render_rotate_pixel_8bpp(combine_op, alpha_op) \
1789 current_pixel = tile_ptr[tile_map_offset + \
1790 (tile_x & 0x07) + ((tile_y & 0x07) * obj_pitch)]; \
1792 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op) \
1794 #define obj_render_rotate(combine_op, color_depth, alpha_op, map_space) \
1796 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32); \
1797 obj_rotate_offset_##map_space(color_depth); \
1799 source_x += (y_delta * dmx) - (middle_x * dx); \
1800 source_y += (y_delta * dmy) - (middle_x * dy); \
1802 for(i = 0; i < obj_width; i++) \
1804 tile_x = (source_x >> 8); \
1805 tile_y = (source_y >> 8); \
1807 if(((u32)tile_x < (u32)max_x) && ((u32)tile_y < (u32)max_y)) \
1812 advance_dest_ptr_##combine_op(1); \
1815 for(; i < obj_width; i++) \
1817 tile_x = (source_x >> 8); \
1818 tile_y = (source_y >> 8); \
1820 if(((u32)tile_x >= (u32)max_x) || ((u32)tile_y >= (u32)max_y)) \
1823 tile_map_offset = ((tile_x >> 3) * tile_size_##color_depth) + \
1824 ((tile_y >> 3) * obj_tile_pitch); \
1825 obj_render_rotate_pixel_##color_depth(combine_op, alpha_op); \
1829 advance_dest_ptr_##combine_op(1); \
1833 // Render the current row of an affine transformed OBJ.
1835 #define obj_render_affine(combine_op, color_depth, alpha_op, map_space) \
1837 s16 *params = oam_ram + (((obj_attribute_1 >> 9) & 0x1F) * 16); \
1838 s32 dx = params[3]; \
1839 s32 dmx = params[7]; \
1840 s32 dy = params[11]; \
1841 s32 dmy = params[15]; \
1842 s32 source_x, source_y; \
1843 s32 tile_x, tile_y; \
1845 u32 tile_map_offset; \
1848 s32 max_x = obj_width; \
1849 s32 max_y = obj_height; \
1851 u32 obj_pitch = tile_width_##color_depth; \
1852 u32 obj_tile_pitch; \
1854 middle_x = (obj_width / 2); \
1855 middle_y = (obj_height / 2); \
1857 source_x = (middle_x << 8); \
1858 source_y = (middle_y << 8); \
1861 if(obj_attribute_0 & 0x200) \
1869 if((s32)obj_x < (s32)start) \
1871 u32 x_delta = start - obj_x; \
1872 middle_x -= x_delta; \
1873 obj_width -= x_delta; \
1876 if((s32)obj_width <= 0) \
1880 if((s32)(obj_x + obj_width) >= (s32)end) \
1882 obj_width = end - obj_x; \
1884 if((s32)obj_width <= 0) \
1887 dest_ptr = scanline + obj_x; \
1889 y_delta = vcount - (obj_y + middle_y); \
1891 obj_get_palette_##color_depth(); \
1895 obj_render_scale(combine_op, color_depth, alpha_op, map_space); \
1899 obj_render_rotate(combine_op, color_depth, alpha_op, map_space); \
1903 u32 obj_width_table[] = { 8, 16, 32, 64, 16, 32, 32, 64, 8, 8, 16, 32 };
1904 u32 obj_height_table[] = { 8, 16, 32, 64, 8, 8, 16, 32, 16, 32, 32, 64 };
1906 u8 obj_priority_list[5][160][128];
1907 u32 obj_priority_count[5][160];
1908 u32 obj_alpha_count[160];
1911 // Build obj rendering functions
1913 #ifdef RENDER_COLOR16_NORMAL
1915 #define render_scanline_obj_extra_variables_normal(bg_type) \
1916 const u32 pixel_combine = (1 << 8) \
1920 #define render_scanline_obj_extra_variables_normal(bg_type) \
1921 u16 *palette = palette_ram_converted + 256 \
1926 #define render_scanline_obj_extra_variables_color() \
1928 u32 pixel_combine = color_combine_mask(4) | (1 << 8) \
1930 #define render_scanline_obj_extra_variables_alpha_obj(map_space) \
1931 render_scanline_obj_extra_variables_color(); \
1932 if((pixel_combine & 0x00000200) == 0) \
1934 render_scanline_obj_color32_##map_space(priority, start, end, scanline); \
1938 #define render_scanline_obj_extra_variables_color16(map_space) \
1939 render_scanline_obj_extra_variables_color() \
1941 #define render_scanline_obj_extra_variables_color32(map_space) \
1942 render_scanline_obj_extra_variables_color() \
1944 #define render_scanline_obj_extra_variables_partial_alpha(map_space) \
1945 render_scanline_obj_extra_variables_color(); \
1946 u32 base_pixel_combine = pixel_combine \
1948 #define render_scanline_obj_extra_variables_copy(type) \
1949 u32 bldcnt = io_registers[REG_BLDCNT]; \
1950 u32 dispcnt = io_registers[REG_DISPCNT]; \
1951 u32 obj_enable = io_registers[REG_WINOUT] >> 8; \
1952 render_scanline_layer_functions_##type(); \
1953 u32 copy_start, copy_end; \
1954 u16 copy_buffer[240]; \
1957 #define render_scanline_obj_extra_variables_copy_tile(map_space) \
1958 render_scanline_obj_extra_variables_copy(tile) \
1960 #define render_scanline_obj_extra_variables_copy_bitmap(map_space) \
1961 render_scanline_obj_extra_variables_copy(bitmap) \
1964 #define render_scanline_obj_main(combine_op, alpha_op, map_space) \
1965 if(obj_attribute_0 & 0x100) \
1967 if((obj_attribute_0 >> 13) & 0x01) \
1969 obj_render_affine(combine_op, 8bpp, alpha_op, map_space); \
1973 obj_render_affine(combine_op, 4bpp, alpha_op, map_space); \
1978 vertical_offset = vcount - obj_y; \
1980 if((obj_attribute_1 >> 13) & 0x01) \
1981 vertical_offset = obj_height - vertical_offset - 1; \
1983 switch(((obj_attribute_0 >> 12) & 0x02) | \
1984 ((obj_attribute_1 >> 12) & 0x01)) \
1987 obj_render(combine_op, 4bpp, alpha_op, map_space, noflip); \
1991 obj_render(combine_op, 4bpp, alpha_op, map_space, flip); \
1995 obj_render(combine_op, 8bpp, alpha_op, map_space, noflip); \
1999 obj_render(combine_op, 8bpp, alpha_op, map_space, flip); \
2004 #define render_scanline_obj_no_partial_alpha(combine_op, alpha_op, map_space) \
2005 render_scanline_obj_main(combine_op, alpha_op, map_space) \
2007 #define render_scanline_obj_partial_alpha(combine_op, alpha_op, map_space) \
2008 if((obj_attribute_0 >> 10) & 0x03) \
2010 pixel_combine = 0x00000300; \
2011 render_scanline_obj_main(combine_op, alpha_obj, map_space); \
2015 pixel_combine = base_pixel_combine; \
2016 render_scanline_obj_main(combine_op, color32, map_space); \
2019 #define render_scanline_obj_prologue_transparent(alpha_op) \
2021 #define render_scanline_obj_prologue_copy_body(type) \
2022 copy_start = obj_x; \
2023 if(obj_attribute_0 & 0x200) \
2024 copy_end = obj_x + (obj_width * 2); \
2026 copy_end = obj_x + obj_width; \
2028 if(copy_start < start) \
2029 copy_start = start; \
2030 if(copy_end > end) \
2033 if((copy_start < end) && (copy_end > start)) \
2035 render_scanline_conditional_##type(copy_start, copy_end, copy_buffer, \
2036 obj_enable, dispcnt, bldcnt, layer_renderers); \
2037 copy_ptr = copy_buffer + copy_start; \
2044 #define render_scanline_obj_prologue_copy_tile() \
2045 render_scanline_obj_prologue_copy_body(tile) \
2047 #define render_scanline_obj_prologue_copy_bitmap() \
2048 render_scanline_obj_prologue_copy_body(bitmap) \
2050 #define render_scanline_obj_prologue_copy(alpha_op) \
2051 render_scanline_obj_prologue_##alpha_op() \
2054 #define render_scanline_obj_builder(combine_op, alpha_op, map_space, \
2056 void render_scanline_obj_##alpha_op##_##map_space(u32 priority, \
2057 u32 start, u32 end, render_scanline_dest_##alpha_op *scanline) \
2059 render_scanline_obj_extra_variables_##alpha_op(map_space); \
2063 s32 obj_width, obj_height; \
2064 u32 obj_attribute_0, obj_attribute_1, obj_attribute_2; \
2065 s32 vcount = io_registers[REG_VCOUNT]; \
2067 u32 current_pixels; \
2068 u32 current_pixel; \
2069 u32 current_palette; \
2070 u32 vertical_offset; \
2071 u32 partial_tile_run, partial_tile_offset; \
2074 render_scanline_dest_##alpha_op *dest_ptr; \
2075 u8 *tile_base = vram + 0x10000; \
2077 u32 obj_count = obj_priority_count[priority][vcount]; \
2078 u8 *obj_list = obj_priority_list[priority][vcount]; \
2080 for(obj_num = 0; obj_num < obj_count; obj_num++) \
2082 oam_ptr = oam_ram + (obj_list[obj_num] * 4); \
2083 obj_attribute_0 = oam_ptr[0]; \
2084 obj_attribute_1 = oam_ptr[1]; \
2085 obj_attribute_2 = oam_ptr[2]; \
2086 obj_size = ((obj_attribute_0 >> 12) & 0x0C) | (obj_attribute_1 >> 14); \
2088 obj_x = (s32)(obj_attribute_1 << 23) >> 23; \
2089 obj_width = obj_width_table[obj_size]; \
2091 render_scanline_obj_prologue_##combine_op(alpha_op); \
2093 obj_y = obj_attribute_0 & 0xFF; \
2098 obj_height = obj_height_table[obj_size]; \
2099 render_scanline_obj_##partial_alpha_op(combine_op, alpha_op, map_space); \
2103 render_scanline_obj_builder(transparent, normal, 1D, no_partial_alpha);
2104 render_scanline_obj_builder(transparent, normal, 2D, no_partial_alpha);
2105 render_scanline_obj_builder(transparent, color16, 1D, no_partial_alpha);
2106 render_scanline_obj_builder(transparent, color16, 2D, no_partial_alpha);
2107 render_scanline_obj_builder(transparent, color32, 1D, no_partial_alpha);
2108 render_scanline_obj_builder(transparent, color32, 2D, no_partial_alpha);
2109 render_scanline_obj_builder(transparent, alpha_obj, 1D, no_partial_alpha);
2110 render_scanline_obj_builder(transparent, alpha_obj, 2D, no_partial_alpha);
2111 render_scanline_obj_builder(transparent, partial_alpha, 1D, partial_alpha);
2112 render_scanline_obj_builder(transparent, partial_alpha, 2D, partial_alpha);
2113 render_scanline_obj_builder(copy, copy_tile, 1D, no_partial_alpha);
2114 render_scanline_obj_builder(copy, copy_tile, 2D, no_partial_alpha);
2115 render_scanline_obj_builder(copy, copy_bitmap, 1D, no_partial_alpha);
2116 render_scanline_obj_builder(copy, copy_bitmap, 2D, no_partial_alpha);
2120 void order_obj(u32 video_mode)
2122 s32 obj_num, priority, row;
2124 s32 obj_size, obj_mode;
2125 s32 obj_width, obj_height;
2127 u32 obj_attribute_0, obj_attribute_1, obj_attribute_2;
2128 s32 vcount = io_registers[REG_VCOUNT];
2129 u32 partial_tile_run, partial_tile_offset;
2132 u16 *oam_ptr = oam_ram + 508;
2134 u8 *tile_base = vram + 0x10000;
2137 for(priority = 0; priority < 5; priority++)
2139 for(row = 0; row < 160; row++)
2141 obj_priority_count[priority][row] = 0;
2145 for(row = 0; row < 160; row++)
2147 obj_alpha_count[row] = 0;
2150 for(obj_num = 127; obj_num >= 0; obj_num--, oam_ptr -= 4)
2152 obj_attribute_0 = oam_ptr[0];
2153 obj_attribute_2 = oam_ptr[2];
2154 obj_size = obj_attribute_0 & 0xC000;
2155 obj_priority = (obj_attribute_2 >> 10) & 0x03;
2156 obj_mode = (obj_attribute_0 >> 10) & 0x03;
2158 if(((obj_attribute_0 & 0x0300) != 0x0200) && (obj_size != 0xC000) &&
2159 (obj_mode != 3) && ((video_mode < 3) ||
2160 ((obj_attribute_2 & 0x3FF) >= 512)))
2162 obj_y = obj_attribute_0 & 0xFF;
2166 obj_attribute_1 = oam_ptr[1];
2167 obj_size = ((obj_size >> 12) & 0x0C) | (obj_attribute_1 >> 14);
2168 obj_height = obj_height_table[obj_size];
2169 obj_width = obj_width_table[obj_size];
2171 if(obj_attribute_0 & 0x200)
2177 if(((obj_y + obj_height) > 0) && (obj_y < 160))
2179 obj_x = (s32)(obj_attribute_1 << 23) >> 23;
2181 if(((obj_x + obj_width) > 0) && (obj_x < 240))
2185 obj_height += obj_y;
2189 if((obj_y + obj_height) >= 160)
2191 obj_height = 160 - obj_y;
2196 for(row = obj_y; row < obj_y + obj_height; row++)
2198 current_count = obj_priority_count[obj_priority][row];
2199 obj_priority_list[obj_priority][row][current_count] = obj_num;
2200 obj_priority_count[obj_priority][row] = current_count + 1;
2201 obj_alpha_count[row]++;
2211 for(row = obj_y; row < obj_y + obj_height; row++)
2213 current_count = obj_priority_count[obj_priority][row];
2214 obj_priority_list[obj_priority][row][current_count] = obj_num;
2215 obj_priority_count[obj_priority][row] = current_count + 1;
2224 u32 layer_order[16];
2227 u32 order_layers(u32 layer_flags)
2229 s32 priority, layer_number;
2232 for(priority = 3; priority >= 0; priority--)
2234 for(layer_number = 3; layer_number >= 0; layer_number--)
2236 if(((layer_flags >> layer_number) & 1) &&
2237 ((io_registers[REG_BG0CNT + layer_number] & 0x03) == priority))
2239 layer_order[layer_count] = layer_number;
2244 if((obj_priority_count[priority][io_registers[REG_VCOUNT]] > 0)
2245 && (layer_flags & 0x10))
2247 layer_order[layer_count] = priority | 0x04;
2253 #define fill_line(_start, _end) \
2256 for(i = _start; i < _end; i++) \
2258 dest_ptr[i] = color; \
2262 #define fill_line_color_normal() \
2263 color = palette_ram_converted[color] \
2265 #define fill_line_color_alpha() \
2267 #define fill_line_color_color16() \
2269 #define fill_line_color_color32() \
2271 #define fill_line_builder(type) \
2272 void fill_line_##type(u16 color, render_scanline_dest_##type *dest_ptr, \
2273 u32 start, u32 end) \
2275 fill_line_color_##type(); \
2276 fill_line(start, end); \
2279 fill_line_builder(normal);
2280 fill_line_builder(alpha);
2281 fill_line_builder(color16);
2282 fill_line_builder(color32);
2285 // Alpha blend two pixels (pixel_top and pixel_bottom).
2287 #define blend_pixel() \
2288 pixel_bottom = palette_ram_converted[(pixel_pair >> 16) & 0x1FF]; \
2289 pixel_bottom = (pixel_bottom | (pixel_bottom << 16)) & 0x07E0F81F; \
2290 pixel_top = ((pixel_top * blend_a) + (pixel_bottom * blend_b)) >> 4 \
2293 // Alpha blend two pixels, allowing for saturation (individual channels > 31).
2294 // The operation is optimized towards saturation not occuring.
2296 #define blend_saturate_pixel() \
2297 pixel_bottom = palette_ram_converted[(pixel_pair >> 16) & 0x1FF]; \
2298 pixel_bottom = (pixel_bottom | (pixel_bottom << 16)) & 0x07E0F81F; \
2299 pixel_top = ((pixel_top * blend_a) + (pixel_bottom * blend_b)) >> 4; \
2300 if(pixel_top & 0x08010020) \
2302 if(pixel_top & 0x08000000) \
2303 pixel_top |= 0x07E00000; \
2305 if(pixel_top & 0x00010000) \
2306 pixel_top |= 0x0000F800; \
2308 if(pixel_top & 0x00000020) \
2309 pixel_top |= 0x0000001F; \
2312 #define brighten_pixel() \
2313 pixel_top = upper + ((pixel_top * blend) >> 4); \
2315 #define darken_pixel() \
2316 pixel_top = (pixel_top * blend) >> 4; \
2318 #define effect_condition_alpha \
2319 ((pixel_pair & 0x04000200) == 0x04000200) \
2321 #define effect_condition_fade(pixel_source) \
2322 ((pixel_source & 0x00000200) == 0x00000200) \
2324 #define expand_pixel_no_dest(expand_type, pixel_source) \
2325 pixel_top = (pixel_top | (pixel_top << 16)) & 0x07E0F81F; \
2326 expand_type##_pixel(); \
2327 pixel_top &= 0x07E0F81F; \
2328 pixel_top = (pixel_top >> 16) | pixel_top \
2330 #define expand_pixel(expand_type, pixel_source) \
2331 pixel_top = palette_ram_converted[pixel_source & 0x1FF]; \
2332 expand_pixel_no_dest(expand_type, pixel_source); \
2333 *screen_dest_ptr = pixel_top \
2335 #define expand_loop(expand_type, effect_condition, pixel_source) \
2336 screen_src_ptr += start; \
2337 screen_dest_ptr += start; \
2341 for(i = 0; i < end; i++) \
2343 pixel_source = *screen_src_ptr; \
2344 if(effect_condition) \
2346 expand_pixel(expand_type, pixel_source); \
2350 *screen_dest_ptr = \
2351 palette_ram_converted[pixel_source & 0x1FF]; \
2355 screen_dest_ptr++; \
2359 #define expand_loop_partial_alpha(alpha_expand, expand_type) \
2360 screen_src_ptr += start; \
2361 screen_dest_ptr += start; \
2365 for(i = 0; i < end; i++) \
2367 pixel_pair = *screen_src_ptr; \
2368 if(effect_condition_fade(pixel_pair)) \
2370 if(effect_condition_alpha) \
2372 expand_pixel(alpha_expand, pixel_pair); \
2376 expand_pixel(expand_type, pixel_pair); \
2381 *screen_dest_ptr = \
2382 palette_ram_converted[pixel_pair & 0x1FF]; \
2386 screen_dest_ptr++; \
2390 #define expand_partial_alpha(expand_type) \
2391 if((blend_a + blend_b) > 16) \
2393 expand_loop_partial_alpha(blend_saturate, expand_type); \
2397 expand_loop_partial_alpha(blend, expand_type); \
2402 // Blend top two pixels of scanline with each other.
2404 #ifdef RENDER_COLOR16_NORMAL
2408 void expand_normal(u16 *screen_ptr, u32 start, u32 end)
2410 u32 i, pixel_source;
2411 screen_ptr += start;
2417 for(i = 0; i < end; i++)
2419 pixel_source = *screen_ptr;
2420 *screen_ptr = palette_ram_converted[pixel_source];
2430 #define expand_normal(screen_ptr, start, end)
2437 void expand_blend(u32 *screen_src_ptr, u16 *screen_dest_ptr,
2441 u32 pixel_top, pixel_bottom;
2442 u32 bldalpha = io_registers[REG_BLDALPHA];
2443 u32 blend_a = bldalpha & 0x1F;
2444 u32 blend_b = (bldalpha >> 8) & 0x1F;
2453 // The individual colors can saturate over 31, this should be taken
2454 // care of in an alternate pass as it incurs a huge additional speedhit.
2455 if((blend_a + blend_b) > 16)
2457 expand_loop(blend_saturate, effect_condition_alpha, pixel_pair);
2461 expand_loop(blend, effect_condition_alpha, pixel_pair);
2467 // Blend scanline with white.
2469 void expand_darken(u16 *screen_src_ptr, u16 *screen_dest_ptr,
2473 s32 blend = 16 - (io_registers[REG_BLDY] & 0x1F);
2479 expand_loop(darken, effect_condition_fade(pixel_top), pixel_top);
2483 // Blend scanline with black.
2485 void expand_brighten(u16 *screen_src_ptr, u16 *screen_dest_ptr,
2489 u32 blend = io_registers[REG_BLDY] & 0x1F;
2496 upper = ((0x07E0F81F * blend) >> 4) & 0x07E0F81F;
2499 expand_loop(brighten, effect_condition_fade(pixel_top), pixel_top);
2504 // Expand scanline such that if both top and bottom pass it's alpha,
2505 // if only top passes it's as specified, and if neither pass it's normal.
2507 void expand_darken_partial_alpha(u32 *screen_src_ptr, u16 *screen_dest_ptr,
2510 s32 blend = 16 - (io_registers[REG_BLDY] & 0x1F);
2512 u32 pixel_top, pixel_bottom;
2513 u32 bldalpha = io_registers[REG_BLDALPHA];
2514 u32 blend_a = bldalpha & 0x1F;
2515 u32 blend_b = (bldalpha >> 8) & 0x1F;
2527 expand_partial_alpha(darken);
2531 void expand_brighten_partial_alpha(u32 *screen_src_ptr, u16 *screen_dest_ptr,
2534 s32 blend = io_registers[REG_BLDY] & 0x1F;
2536 u32 pixel_top, pixel_bottom;
2537 u32 bldalpha = io_registers[REG_BLDALPHA];
2538 u32 blend_a = bldalpha & 0x1F;
2539 u32 blend_b = (bldalpha >> 8) & 0x1F;
2546 upper = ((0x07E0F81F * blend) >> 4) & 0x07E0F81F;
2555 expand_partial_alpha(brighten);
2559 // Render an OBJ layer from start to end, depending on the type (1D or 2D)
2560 // stored in dispcnt.
2562 #define render_obj_layer(type, dest, _start, _end) \
2563 current_layer &= ~0x04; \
2564 if(dispcnt & 0x40) \
2565 render_scanline_obj_##type##_1D(current_layer, _start, _end, dest); \
2567 render_scanline_obj_##type##_2D(current_layer, _start, _end, dest) \
2570 // Render a target all the way with the background color as taken from the
2573 #define fill_line_bg(type, dest, _start, _end) \
2574 fill_line_##type(0, dest, _start, _end) \
2577 // Render all layers as they appear in the layer order.
2579 #define render_layers(tile_alpha, obj_alpha, dest) \
2581 current_layer = layer_order[0]; \
2582 if(current_layer & 0x04) \
2584 /* If the first one is OBJ render the background then render it. */ \
2585 fill_line_bg(tile_alpha, dest, 0, 240); \
2586 render_obj_layer(obj_alpha, dest, 0, 240); \
2590 /* Otherwise render a base layer. */ \
2591 layer_renderers[current_layer].tile_alpha##_render_base(current_layer, \
2595 /* Render the rest of the layers. */ \
2596 for(layer_order_pos = 1; layer_order_pos < layer_count; layer_order_pos++) \
2598 current_layer = layer_order[layer_order_pos]; \
2599 if(current_layer & 0x04) \
2601 render_obj_layer(obj_alpha, dest, 0, 240); \
2605 layer_renderers[current_layer]. \
2606 tile_alpha##_render_transparent(current_layer, 0, 240, dest); \
2611 #define render_condition_alpha \
2612 (((io_registers[REG_BLDALPHA] & 0x1F1F) != 0x001F) && \
2613 ((io_registers[REG_BLDCNT] & 0x3F) != 0) && \
2614 ((io_registers[REG_BLDCNT] & 0x3F00) != 0)) \
2616 #define render_condition_fade \
2617 (((io_registers[REG_BLDY] & 0x1F) != 0) && \
2618 ((io_registers[REG_BLDCNT] & 0x3F) != 0)) \
2620 #define render_layers_color_effect(renderer, layer_condition, \
2621 alpha_condition, fade_condition, _start, _end) \
2623 if(layer_condition) \
2625 if(obj_alpha_count[io_registers[REG_VCOUNT]] > 0) \
2627 /* Render based on special effects mode. */ \
2628 u32 screen_buffer[240]; \
2629 switch((bldcnt >> 6) & 0x03) \
2634 if(alpha_condition) \
2636 renderer(alpha, alpha_obj, screen_buffer); \
2637 expand_blend(screen_buffer, scanline, _start, _end); \
2643 /* Fade to white */ \
2646 if(fade_condition) \
2648 renderer(color32, partial_alpha, screen_buffer); \
2649 expand_brighten_partial_alpha(screen_buffer, scanline, \
2656 /* Fade to black */ \
2659 if(fade_condition) \
2661 renderer(color32, partial_alpha, screen_buffer); \
2662 expand_darken_partial_alpha(screen_buffer, scanline, \
2670 renderer(color32, partial_alpha, screen_buffer); \
2671 expand_blend(screen_buffer, scanline, _start, _end); \
2675 /* Render based on special effects mode. */ \
2676 switch((bldcnt >> 6) & 0x03) \
2681 if(alpha_condition) \
2683 u32 screen_buffer[240]; \
2684 renderer(alpha, alpha_obj, screen_buffer); \
2685 expand_blend(screen_buffer, scanline, _start, _end); \
2691 /* Fade to white */ \
2694 if(fade_condition) \
2696 renderer(color16, color16, scanline); \
2697 expand_brighten(scanline, scanline, _start, _end); \
2703 /* Fade to black */ \
2706 if(fade_condition) \
2708 renderer(color16, color16, scanline); \
2709 expand_darken(scanline, scanline, _start, _end); \
2716 renderer(normal, normal, scanline); \
2717 expand_normal(scanline, _start, _end); \
2722 u32 pixel_top = palette_ram_converted[0]; \
2723 switch((bldcnt >> 6) & 0x03) \
2725 /* Fade to white */ \
2728 if(color_combine_mask_a(5)) \
2730 u32 blend = io_registers[REG_BLDY] & 0x1F; \
2736 upper = ((0x07E0F81F * blend) >> 4) & 0x07E0F81F; \
2737 blend = 16 - blend; \
2739 expand_pixel_no_dest(brighten, pixel_top); \
2744 /* Fade to black */ \
2747 if(color_combine_mask_a(5)) \
2749 s32 blend = 16 - (io_registers[REG_BLDY] & 0x1F); \
2754 expand_pixel_no_dest(darken, pixel_top); \
2759 fill_line_color16(pixel_top, scanline, _start, _end); \
2764 // Renders an entire scanline from 0 to 240, based on current color mode.
2766 void render_scanline_tile(u16 *scanline, u32 dispcnt)
2769 u32 layer_order_pos;
2770 u32 bldcnt = io_registers[REG_BLDCNT];
2771 render_scanline_layer_functions_tile();
2773 render_layers_color_effect(render_layers, layer_count,
2774 render_condition_alpha, render_condition_fade, 0, 240);
2777 void render_scanline_bitmap(u16 *scanline, u32 dispcnt)
2779 u32 bldcnt = io_registers[REG_BLDCNT];
2780 render_scanline_layer_functions_bitmap();
2782 u32 layer_order_pos;
2784 fill_line_bg(normal, scanline, 0, 240);
2786 for(layer_order_pos = 0; layer_order_pos < layer_count; layer_order_pos++)
2788 current_layer = layer_order[layer_order_pos];
2789 if(current_layer & 0x04)
2791 render_obj_layer(normal, scanline, 0, 240);
2795 layer_renderers->normal_render(0, 240, scanline);
2800 // Render layers from start to end based on if they're allowed in the
2803 #define render_layers_conditional(tile_alpha, obj_alpha, dest) \
2806 current_layer = layer_order[layer_order_pos]; \
2807 /* If OBJ aren't enabled skip to the first non-OBJ layer */ \
2808 if(!(enable_flags & 0x10)) \
2810 while((current_layer & 0x04) || !((1 << current_layer) & enable_flags)) \
2812 layer_order_pos++; \
2813 current_layer = layer_order[layer_order_pos]; \
2815 /* Oops, ran out of layers, render the background. */ \
2816 if(layer_order_pos == layer_count) \
2818 fill_line_bg(tile_alpha, dest, start, end); \
2823 /* Render the first valid layer */ \
2824 layer_renderers[current_layer].tile_alpha##_render_base(current_layer, \
2825 start, end, dest); \
2827 layer_order_pos++; \
2829 /* Render the rest of the layers if active, skipping OBJ ones. */ \
2830 for(; layer_order_pos < layer_count; layer_order_pos++) \
2832 current_layer = layer_order[layer_order_pos]; \
2833 if(!(current_layer & 0x04) && ((1 << current_layer) & enable_flags)) \
2835 layer_renderers[current_layer]. \
2836 tile_alpha##_render_transparent(current_layer, start, end, dest); \
2842 /* Find the first active layer, skip all of the inactive ones */ \
2843 while(!((current_layer & 0x04) || ((1 << current_layer) & enable_flags))) \
2845 layer_order_pos++; \
2846 current_layer = layer_order[layer_order_pos]; \
2848 /* Oops, ran out of layers, render the background. */ \
2849 if(layer_order_pos == layer_count) \
2851 fill_line_bg(tile_alpha, dest, start, end); \
2856 if(current_layer & 0x04) \
2858 /* If the first one is OBJ render the background then render it. */ \
2859 fill_line_bg(tile_alpha, dest, start, end); \
2860 render_obj_layer(obj_alpha, dest, start, end); \
2864 /* Otherwise render a base layer. */ \
2865 layer_renderers[current_layer]. \
2866 tile_alpha##_render_base(current_layer, start, end, dest); \
2869 layer_order_pos++; \
2871 /* Render the rest of the layers. */ \
2872 for(; layer_order_pos < layer_count; layer_order_pos++) \
2874 current_layer = layer_order[layer_order_pos]; \
2875 if(current_layer & 0x04) \
2877 render_obj_layer(obj_alpha, dest, start, end); \
2881 if(enable_flags & (1 << current_layer)) \
2883 layer_renderers[current_layer]. \
2884 tile_alpha##_render_transparent(current_layer, start, end, dest); \
2895 // Render all of the BG and OBJ in a tiled scanline from start to end ONLY if
2896 // enable_flag allows that layer/OBJ. Also conditionally render color effects.
2898 void render_scanline_conditional_tile(u32 start, u32 end, u16 *scanline,
2899 u32 enable_flags, u32 dispcnt, u32 bldcnt, tile_layer_render_struct
2903 u32 layer_order_pos = 0;
2905 render_layers_color_effect(render_layers_conditional,
2906 (layer_count && (enable_flags & 0x1F)),
2907 ((enable_flags & 0x20) && render_condition_alpha),
2908 ((enable_flags & 0x20) && render_condition_fade), start, end);
2912 // Render the BG and OBJ in a bitmap scanline from start to end ONLY if
2913 // enable_flag allows that layer/OBJ. Also conditionally render color effects.
2915 void render_scanline_conditional_bitmap(u32 start, u32 end, u16 *scanline,
2916 u32 enable_flags, u32 dispcnt, u32 bldcnt, bitmap_layer_render_struct
2920 u32 layer_order_pos;
2922 fill_line_bg(normal, scanline, start, end);
2924 for(layer_order_pos = 0; layer_order_pos < layer_count; layer_order_pos++)
2926 current_layer = layer_order[layer_order_pos];
2927 if(current_layer & 0x04)
2929 if(enable_flags & 0x10)
2931 render_obj_layer(normal, scanline, start, end);
2936 if(enable_flags & 0x04)
2937 layer_renderers->normal_render(start, end, scanline);
2943 #define window_x_coords(window_number) \
2944 window_##window_number##_x1 = \
2945 io_registers[REG_WIN##window_number##H] >> 8; \
2946 window_##window_number##_x2 = \
2947 io_registers[REG_WIN##window_number##H] & 0xFF; \
2948 window_##window_number##_enable = \
2949 (winin >> (window_number * 8)) & 0x3F; \
2951 if(window_##window_number##_x1 > 240) \
2952 window_##window_number##_x1 = 240; \
2954 if(window_##window_number##_x2 > 240) \
2955 window_##window_number##_x2 = 240 \
2957 #define window_coords(window_number) \
2958 u32 window_##window_number##_x1, window_##window_number##_x2; \
2959 u32 window_##window_number##_y1, window_##window_number##_y2; \
2960 u32 window_##window_number##_enable; \
2961 window_##window_number##_y1 = \
2962 io_registers[REG_WIN##window_number##V] >> 8; \
2963 window_##window_number##_y2 = \
2964 io_registers[REG_WIN##window_number##V] & 0xFF; \
2966 if(window_##window_number##_y1 > window_##window_number##_y2) \
2968 if((((vcount <= window_##window_number##_y2) || \
2969 (vcount > window_##window_number##_y1)) || \
2970 (window_##window_number##_y2 > 227)) && \
2971 (window_##window_number##_y1 <= 227)) \
2973 window_x_coords(window_number); \
2977 window_##window_number##_x1 = 240; \
2978 window_##window_number##_x2 = 240; \
2983 if((((vcount >= window_##window_number##_y1) && \
2984 (vcount < window_##window_number##_y2)) || \
2985 (window_##window_number##_y2 > 227)) && \
2986 (window_##window_number##_y1 <= 227)) \
2988 window_x_coords(window_number); \
2992 window_##window_number##_x1 = 240; \
2993 window_##window_number##_x2 = 240; \
2997 #define render_window_segment(type, start, end, window_type) \
3000 render_scanline_conditional_##type(start, end, scanline, \
3001 window_##window_type##_enable, dispcnt, bldcnt, layer_renderers); \
3004 #define render_window_segment_unequal(type, start, end, window_type) \
3005 render_scanline_conditional_##type(start, end, scanline, \
3006 window_##window_type##_enable, dispcnt, bldcnt, layer_renderers) \
3008 #define render_window_segment_clip(type, clip_start, clip_end, start, end, \
3013 if(start < clip_start) \
3015 if(end > clip_start) \
3017 if(end > clip_end) \
3019 render_window_segment_unequal(type, clip_start, clip_end, \
3024 render_window_segment_unequal(type, clip_start, end, window_type); \
3030 if(end > clip_end) \
3032 if(start < clip_end) \
3033 render_window_segment_unequal(type, start, clip_end, window_type); \
3037 render_window_segment_unequal(type, start, end, window_type); \
3042 #define render_window_clip_1(type, start, end) \
3043 if(window_1_x1 != 240) \
3045 if(window_1_x1 > window_1_x2) \
3047 render_window_segment_clip(type, start, end, 0, window_1_x2, 1); \
3048 render_window_segment_clip(type, start, end, window_1_x2, window_1_x1, \
3050 render_window_segment_clip(type, start, end, window_1_x1, 240, 1); \
3054 render_window_segment_clip(type, start, end, 0, window_1_x1, out); \
3055 render_window_segment_clip(type, start, end, window_1_x1, window_1_x2, \
3057 render_window_segment_clip(type, start, end, window_1_x2, 240, out); \
3062 render_window_segment(type, start, end, out); \
3065 #define render_window_clip_obj(type, start, end); \
3066 render_window_segment(type, start, end, out); \
3067 if(dispcnt & 0x40) \
3068 render_scanline_obj_copy_##type##_1D(4, start, end, scanline); \
3070 render_scanline_obj_copy_##type##_2D(4, start, end, scanline) \
3073 #define render_window_segment_clip_obj(type, clip_start, clip_end, start, \
3078 if(start < clip_start) \
3080 if(end > clip_start) \
3082 if(end > clip_end) \
3084 render_window_clip_obj(type, clip_start, clip_end); \
3088 render_window_clip_obj(type, clip_start, end); \
3094 if(end > clip_end) \
3096 if(start < clip_end) \
3098 render_window_clip_obj(type, start, clip_end); \
3103 render_window_clip_obj(type, start, end); \
3109 #define render_window_clip_1_obj(type, start, end) \
3110 if(window_1_x1 != 240) \
3112 if(window_1_x1 > window_1_x2) \
3114 render_window_segment_clip(type, start, end, 0, window_1_x2, 1); \
3115 render_window_segment_clip_obj(type, start, end, window_1_x2, \
3117 render_window_segment_clip(type, start, end, window_1_x1, 240, 1); \
3121 render_window_segment_clip_obj(type, start, end, 0, window_1_x1); \
3122 render_window_segment_clip(type, start, end, window_1_x1, window_1_x2, \
3124 render_window_segment_clip_obj(type, start, end, window_1_x2, 240); \
3129 render_window_clip_obj(type, start, end); \
3134 #define render_window_single(type, window_number) \
3135 u32 winin = io_registers[REG_WININ]; \
3136 window_coords(window_number); \
3137 if(window_##window_number##_x1 > window_##window_number##_x2) \
3139 render_window_segment(type, 0, window_##window_number##_x2, \
3141 render_window_segment(type, window_##window_number##_x2, \
3142 window_##window_number##_x1, out); \
3143 render_window_segment(type, window_##window_number##_x1, 240, \
3148 render_window_segment(type, 0, window_##window_number##_x1, out); \
3149 render_window_segment(type, window_##window_number##_x1, \
3150 window_##window_number##_x2, window_number); \
3151 render_window_segment(type, window_##window_number##_x2, 240, out); \
3154 #define render_window_multi(type, front, back) \
3155 if(window_##front##_x1 > window_##front##_x2) \
3157 render_window_segment(type, 0, window_##front##_x2, front); \
3158 render_window_clip_##back(type, window_##front##_x2, \
3159 window_##front##_x1); \
3160 render_window_segment(type, window_##front##_x1, 240, front); \
3164 render_window_clip_##back(type, 0, window_##front##_x1); \
3165 render_window_segment(type, window_##front##_x1, window_##front##_x2, \
3167 render_window_clip_##back(type, window_##front##_x2, 240); \
3170 #define render_scanline_window_builder(type) \
3171 void render_scanline_window_##type(u16 *scanline, u32 dispcnt) \
3173 u32 vcount = io_registers[REG_VCOUNT]; \
3174 u32 winout = io_registers[REG_WINOUT]; \
3175 u32 bldcnt = io_registers[REG_BLDCNT]; \
3176 u32 window_out_enable = winout & 0x3F; \
3178 render_scanline_layer_functions_##type(); \
3180 switch(dispcnt >> 13) \
3182 /* Just window 0 */ \
3185 render_window_single(type, 0); \
3189 /* Just window 1 */ \
3192 render_window_single(type, 1); \
3196 /* Windows 1 and 2 */ \
3199 u32 winin = io_registers[REG_WININ]; \
3202 render_window_multi(type, 0, 1); \
3206 /* Just OBJ windows */ \
3209 u32 window_obj_enable = winout >> 8; \
3210 render_window_clip_obj(type, 0, 240); \
3214 /* Window 0 and OBJ window */ \
3217 u32 window_obj_enable = winout >> 8; \
3218 u32 winin = io_registers[REG_WININ]; \
3220 render_window_multi(type, 0, obj); \
3224 /* Window 1 and OBJ window */ \
3227 u32 window_obj_enable = winout >> 8; \
3228 u32 winin = io_registers[REG_WININ]; \
3230 render_window_multi(type, 1, obj); \
3234 /* Window 0, 1, and OBJ window */ \
3237 u32 window_obj_enable = winout >> 8; \
3238 u32 winin = io_registers[REG_WININ]; \
3241 render_window_multi(type, 0, 1_obj); \
3247 render_scanline_window_builder(tile);
3248 render_scanline_window_builder(bitmap);
3250 u32 active_layers[6] = { 0x1F, 0x17, 0x1C, 0x14, 0x14, 0x14 };
3252 u32 small_resolution_width = 240;
3253 u32 small_resolution_height = 160;
3254 u32 resolution_width, resolution_height;
3256 void update_scanline()
3258 u32 pitch = get_screen_pitch();
3259 u32 dispcnt = io_registers[REG_DISPCNT];
3260 u32 display_flags = (dispcnt >> 8) & 0x1F;
3261 u32 vcount = io_registers[REG_VCOUNT];
3262 u16 *screen_offset = get_screen_pixels() + (vcount * pitch);
3263 u32 video_mode = dispcnt & 0x07;
3266 // If OAM has been modified since the last scanline has been updated then
3267 // reorder and reprofile the OBJ lists.
3270 order_obj(video_mode);
3274 order_layers((dispcnt >> 8) & active_layers[video_mode]);
3280 if (screen_scale == unscaled_rot || screen_scale == scaled_aspect_rot)
3282 if (rot_line_count == rot_lines_total)
3285 if (vcount - rot_lines_total < FONT_HEIGHT && rot_msg_buff[0])
3287 print_string_ext(rot_msg_buff, 0xFFFF, 0x0000, 0, 0,
3288 rot_buffer, 240, 0, vcount - rot_lines_total, rot_lines_total);
3289 if (vcount >= FONT_HEIGHT)
3290 rot_msg_buff[0] = 0;
3292 if (screen_scale == unscaled_rot)
3293 do_rotated_blit(gpsp_gp2x_screen, rot_buffer, vcount);
3295 upscale_aspect_row(gpsp_gp2x_screen, rot_buffer, vcount/3);
3297 screen_offset = &rot_buffer[rot_line_count++ * 240];
3301 // If the screen is in in forced blank draw pure white.
3304 fill_line_color16(0xFFFF, screen_offset, 0, 240);
3312 render_scanline_window_tile(screen_offset, dispcnt);
3316 render_scanline_tile(screen_offset, dispcnt);
3322 render_scanline_window_bitmap(screen_offset, dispcnt);
3324 render_scanline_bitmap(screen_offset, dispcnt);
3328 affine_reference_x[0] += (s16)io_registers[REG_BG2PB];
3329 affine_reference_y[0] += (s16)io_registers[REG_BG2PD];
3330 affine_reference_x[1] += (s16)io_registers[REG_BG3PB];
3331 affine_reference_y[1] += (s16)io_registers[REG_BG3PD];
3336 u32 screen_flip = 0;
3340 if(video_direct == 0)
3342 u32 *old_ge_cmd_ptr = ge_cmd_ptr;
3343 sceKernelDcacheWritebackAll();
3345 // Render the current screen
3346 ge_cmd_ptr = ge_cmd + 2;
3347 GE_CMD(TBP0, ((u32)screen_pixels & 0x00FFFFFF));
3348 GE_CMD(TBW0, (((u32)screen_pixels & 0xFF000000) >> 8) |
3350 ge_cmd_ptr = old_ge_cmd_ptr;
3352 sceGeListEnQueue(ge_cmd, ge_cmd_ptr, gecbid, NULL);
3354 // Flip to the next screen
3358 screen_pixels = screen_texture + (240 * 160 * 2);
3360 screen_pixels = screen_texture;
3364 #elif defined(WIZ_BUILD)
3368 if((resolution_width == small_resolution_width) &&
3369 (resolution_height == small_resolution_height))
3371 switch(screen_scale)
3374 upscale_aspect(gpsp_gp2x_screen, screen_pixels);
3377 do_rotated_blit(gpsp_gp2x_screen, rot_buffer, 160);
3380 case scaled_aspect_rot:
3385 warm_cache_op_all(WOP_D_CLEAN);
3388 pollux_video_flip();
3389 screen_pixels = (u16 *)gpsp_gp2x_screen + screen_offset;
3394 #define integer_scale_copy_2() \
3395 current_scanline_ptr[x2] = current_pixel; \
3396 current_scanline_ptr[x2 - 1] = current_pixel; \
3399 #define integer_scale_copy_3() \
3400 current_scanline_ptr[x2] = current_pixel; \
3401 current_scanline_ptr[x2 - 1] = current_pixel; \
3402 current_scanline_ptr[x2 - 2] = current_pixel; \
3405 #define integer_scale_copy_4() \
3406 current_scanline_ptr[x2] = current_pixel; \
3407 current_scanline_ptr[x2 - 1] = current_pixel; \
3408 current_scanline_ptr[x2 - 2] = current_pixel; \
3409 current_scanline_ptr[x2 - 3] = current_pixel; \
3412 #define integer_scale_horizontal(scale_factor) \
3413 for(y = 0; y < 160; y++) \
3415 for(x = 239, x2 = (240 * video_scale) - 1; x >= 0; x--) \
3417 current_pixel = current_scanline_ptr[x]; \
3418 integer_scale_copy_##scale_factor(); \
3419 current_scanline_ptr[x2] = current_scanline_ptr[x]; \
3420 current_scanline_ptr[x2 - 1] = current_scanline_ptr[x]; \
3421 current_scanline_ptr[x2 - 2] = current_scanline_ptr[x]; \
3423 current_scanline_ptr += pitch; \
3428 if((video_scale != 1) && (current_scale != unscaled))
3432 u16 *screen_ptr = get_screen_pixels();
3433 u16 *current_scanline_ptr = screen_ptr;
3434 u32 pitch = get_screen_pitch();
3441 integer_scale_horizontal(2);
3445 integer_scale_horizontal(3);
3450 integer_scale_horizontal(4);
3455 for(y = 159, y2 = (160 * video_scale) - 1; y >= 0; y--)
3457 for(i = 0; i < video_scale; i++)
3459 memcpy(screen_ptr + (y2 * pitch),
3460 screen_ptr + (y * pitch), 480 * video_scale);
3467 if((screen_scale == unscaled) &&
3468 (resolution_width == small_resolution_width) &&
3469 (resolution_height == small_resolution_height))
3471 SDL_Rect srect = {0, 0, 240, 160};
3472 SDL_Rect drect = {40, 40, 240, 160};
3473 SDL_BlitSurface(screen, &srect, hw_screen, &drect);
3475 else if((screen_scale == scaled_aspect) &&
3476 (resolution_width == small_resolution_width) &&
3477 (resolution_height == small_resolution_height))
3479 SDL_Rect drect = {0, 10, 0, 0};
3480 SDL_BlitSurface(screen, NULL, hw_screen, &drect);
3484 SDL_BlitSurface(screen, NULL, hw_screen, NULL);
3486 warm_cache_op_all(WOP_D_CLEAN);
3495 u32 frame_to_render;
3497 void update_screen()
3499 if(!skip_next_frame)
3507 sceDisplaySetMode(0, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
3509 sceDisplayWaitVblankStart();
3510 sceDisplaySetFrameBuf((void*)psp_gu_vram_base, PSP_LINE_SIZE,
3511 PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
3515 sceGuStart(GU_DIRECT, display_list);
3516 sceGuDrawBuffer(GU_PSM_5650, (void*)0, PSP_LINE_SIZE);
3517 sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3518 (void*)0, PSP_LINE_SIZE);
3519 sceGuClear(GU_COLOR_BUFFER_BIT);
3521 sceGuOffset(2048 - (PSP_SCREEN_WIDTH / 2), 2048 - (PSP_SCREEN_HEIGHT / 2));
3522 sceGuViewport(2048, 2048, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
3524 sceGuScissor(0, 0, PSP_SCREEN_WIDTH + 1, PSP_SCREEN_HEIGHT + 1);
3525 sceGuEnable(GU_SCISSOR_TEST);
3526 sceGuTexMode(GU_PSM_5650, 0, 0, GU_FALSE);
3527 sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
3528 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
3529 sceGuEnable(GU_TEXTURE_2D);
3531 sceGuFrontFace(GU_CW);
3532 sceGuDisable(GU_BLEND);
3537 sceDisplayWaitVblankStart();
3538 sceGuDisplay(GU_TRUE);
3540 PspGeCallbackData gecb;
3541 gecb.signal_func = NULL;
3542 gecb.signal_arg = NULL;
3543 gecb.finish_func = Ge_Finish_Callback;
3544 gecb.finish_arg = NULL;
3545 gecbid = sceGeSetCallback(&gecb);
3547 screen_vertex[0] = 0 + 0.5;
3548 screen_vertex[1] = 0 + 0.5;
3549 screen_vertex[2] = 0 + 0.5;
3550 screen_vertex[3] = 0 + 0.5;
3551 screen_vertex[4] = 0;
3552 screen_vertex[5] = GBA_SCREEN_WIDTH - 0.5;
3553 screen_vertex[6] = GBA_SCREEN_HEIGHT - 0.5;
3554 screen_vertex[7] = PSP_SCREEN_WIDTH - 0.5;
3555 screen_vertex[8] = PSP_SCREEN_HEIGHT - 0.5;
3556 screen_vertex[9] = 0;
3558 // Set framebuffer to PSP VRAM
3559 GE_CMD(FBP, ((u32)psp_gu_vram_base & 0x00FFFFFF));
3560 GE_CMD(FBW, (((u32)psp_gu_vram_base & 0xFF000000) >> 8) | PSP_LINE_SIZE);
3561 // Set texture 0 to the screen texture
3562 GE_CMD(TBP0, ((u32)screen_texture & 0x00FFFFFF));
3563 GE_CMD(TBW0, (((u32)screen_texture & 0xFF000000) >> 8) | GBA_SCREEN_WIDTH);
3564 // Set the texture size to 256 by 256 (2^8 by 2^8)
3565 GE_CMD(TSIZE0, (8 << 8) | 8);
3566 // Flush the texture cache
3568 // Use 2D coordinates, no indeces, no weights, 32bit float positions,
3569 // 32bit float texture coordinates
3570 GE_CMD(VTYPE, (1 << 23) | (0 << 11) | (0 << 9) |
3571 (3 << 7) | (0 << 5) | (0 << 2) | 3);
3572 // Set the base of the index list pointer to 0
3574 // Set the rest of index list pointer to 0 (not being used)
3576 // Set the base of the screen vertex list pointer
3577 GE_CMD(BASE, ((u32)screen_vertex & 0xFF000000) >> 8);
3578 // Set the rest of the screen vertex list pointer
3579 GE_CMD(VADDR, ((u32)screen_vertex & 0x00FFFFFF));
3580 // Primitive kick: render sprite (primitive 6), 2 vertices
3581 GE_CMD(PRIM, (6 << 16) | 2);
3582 // Done with commands
3584 // Raise signal interrupt
3590 #elif defined(WIZ_BUILD)
3600 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE);
3603 SDL_GP2X_AllowGfxMemory(NULL, 0);
3605 hw_screen = SDL_SetVideoMode(320 * video_scale, 240 * video_scale,
3608 screen = SDL_CreateRGBSurface(SDL_HWSURFACE, 240 * video_scale,
3609 160 * video_scale, 16, 0xFFFF, 0xFFFF, 0xFFFF, 0);
3611 warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
3613 screen = SDL_SetVideoMode(240 * video_scale, 160 * video_scale, 16, 0);
3620 video_scale_type screen_scale = scaled_aspect;
3621 video_scale_type current_scale = scaled_aspect;
3622 video_filter_type screen_filter = filter_bilinear;
3627 void video_resolution_large()
3629 if(video_direct != 1)
3632 screen_pixels = psp_gu_vram_base;
3634 sceGuStart(GU_DIRECT, display_list);
3635 sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3636 (void*)0, PSP_LINE_SIZE);
3641 void set_gba_resolution(video_scale_type scale)
3643 u32 filter_linear = 0;
3644 screen_scale = scale;
3648 screen_vertex[2] = 120 + 0.5;
3649 screen_vertex[3] = 56 + 0.5;
3650 screen_vertex[7] = GBA_SCREEN_WIDTH + 120 - 0.5;
3651 screen_vertex[8] = GBA_SCREEN_HEIGHT + 56 - 0.5;
3655 screen_vertex[2] = 36 + 0.5;
3656 screen_vertex[3] = 0 + 0.5;
3657 screen_vertex[7] = 408 + 36 - 0.5;
3658 screen_vertex[8] = PSP_SCREEN_HEIGHT - 0.5;
3662 screen_vertex[2] = 0;
3663 screen_vertex[3] = 0;
3664 screen_vertex[7] = PSP_SCREEN_WIDTH;
3665 screen_vertex[8] = PSP_SCREEN_HEIGHT;
3669 sceGuStart(GU_DIRECT, display_list);
3670 if(screen_filter == filter_bilinear)
3671 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
3673 sceGuTexFilter(GU_NEAREST, GU_NEAREST);
3678 clear_screen(0x0000);
3681 void video_resolution_small()
3683 if(video_direct != 0)
3685 set_gba_resolution(screen_scale);
3687 screen_pixels = screen_texture;
3690 sceGuStart(GU_DIRECT, display_list);
3691 sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3692 (void*)0, PSP_LINE_SIZE);
3697 void clear_screen(u16 color)
3700 u16 *src_ptr = get_screen_pixels();
3704 for(i = 0; i < (512 * 272); i++, src_ptr++)
3709 // I don't know why this doesn't work.
3710 /* color = (((color & 0x1F) * 255 / 31) << 0) |
3711 ((((color >> 5) & 0x3F) * 255 / 63) << 8) |
3712 ((((color >> 11) & 0x1F) * 255 / 31) << 16) | (0xFF << 24);
3714 sceGuStart(GU_DIRECT, display_list);
3715 sceGuDrawBuffer(GU_PSM_5650, (void*)0, PSP_LINE_SIZE);
3716 //sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3717 // (void*)0, PSP_LINE_SIZE);
3718 sceGuClearColor(color);
3719 sceGuClear(GU_COLOR_BUFFER_BIT);
3724 #elif defined(WIZ_BUILD)
3726 void video_resolution_large()
3729 resolution_width = 320;
3730 resolution_height = 240;
3735 wiz_lcd_set_portrait(0);
3738 void video_resolution_small()
3742 switch (screen_scale)
3745 screen_offset = 320*40 + 40;
3746 wiz_lcd_set_portrait(0);
3749 screen_offset = 320*(80 - 14) + 80;
3750 wiz_lcd_set_portrait(0);
3753 wiz_lcd_set_portrait(1);
3754 rot_lines_total = 4;
3757 case scaled_aspect_rot:
3758 wiz_lcd_set_portrait(1);
3759 rot_lines_total = 3;
3767 resolution_width = 240;
3768 resolution_height = 160;
3771 void set_gba_resolution(video_scale_type scale)
3773 screen_scale = scale;
3776 void clear_screen(u16 color)
3778 u32 col = ((u32)color << 16) | color;
3779 u32 *p = gpsp_gp2x_screen;
3787 void video_resolution_large()
3789 current_scale = unscaled;
3792 SDL_FreeSurface(screen);
3793 SDL_GP2X_AllowGfxMemory(NULL, 0);
3794 hw_screen = SDL_SetVideoMode(320, 240, 16, SDL_HWSURFACE);
3795 screen = SDL_CreateRGBSurface(SDL_HWSURFACE, 320, 240, 16, 0xFFFF,
3797 resolution_width = 320;
3798 resolution_height = 240;
3801 warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
3803 screen = SDL_SetVideoMode(480, 272, 16, 0);
3804 resolution_width = 480;
3805 resolution_height = 272;
3809 void video_resolution_small()
3811 current_scale = screen_scale;
3815 SDL_FreeSurface(screen);
3816 SDL_GP2X_AllowGfxMemory(NULL, 0);
3819 if (screen_scale != unscaled)
3821 w = small_resolution_width * video_scale;
3822 h = small_resolution_height * video_scale;
3824 if (screen_scale == scaled_aspect) h += 20;
3825 hw_screen = SDL_SetVideoMode(w, h, 16, SDL_HWSURFACE);
3827 screen = SDL_CreateRGBSurface(SDL_HWSURFACE,
3828 small_resolution_width * video_scale, small_resolution_height *
3829 video_scale, 16, 0xFFFF, 0xFFFF, 0xFFFF, 0);
3833 warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
3835 screen = SDL_SetVideoMode(small_resolution_width * video_scale,
3836 small_resolution_height * video_scale, 16, 0);
3838 resolution_width = small_resolution_width;
3839 resolution_height = small_resolution_height;
3842 void set_gba_resolution(video_scale_type scale)
3844 if(screen_scale != scale)
3846 screen_scale = scale;
3852 small_resolution_width = 240 * video_scale;
3853 small_resolution_height = 160 * video_scale;
3859 void clear_screen(u16 color)
3861 u16 *dest_ptr = get_screen_pixels();
3862 u32 line_skip = get_screen_pitch() - screen->w;
3865 for(y = 0; y < screen->h; y++)
3867 for(x = 0; x < screen->w; x++, dest_ptr++)
3871 dest_ptr += line_skip;
3879 u16 *copy = malloc(240 * 160 * 2);
3880 memcpy(copy, get_screen_pixels(), 240 * 160 * 2);
3884 void blit_to_screen(u16 *src, u32 w, u32 h, u32 dest_x, u32 dest_y)
3886 u32 pitch = get_screen_pitch();
3887 u16 *dest_ptr = get_screen_pixels() + dest_x + (dest_y * pitch);
3890 u32 line_skip = pitch - w;
3893 for(y = 0; y < h; y++)
3895 for(x = 0; x < w; x++, src_ptr++, dest_ptr++)
3897 *dest_ptr = *src_ptr;
3899 dest_ptr += line_skip;
3903 void print_string_ext(const char *str, u16 fg_color, u16 bg_color,
3904 u32 x, u32 y, void *_dest_ptr, u32 pitch, u32 pad, u32 h_offset, u32 height)
3906 u16 *dest_ptr = (u16 *)_dest_ptr + (y * pitch) + x;
3907 u8 current_char = str[0];
3910 u32 i = 0, i2, i3, h;
3914 if(y + height > resolution_height)
3919 if(current_char == '\n')
3923 dest_ptr = get_screen_pixels() + (y * pitch) + x;
3927 glyph_offset = _font_offset[current_char];
3928 current_x += FONT_WIDTH;
3929 glyph_offset += h_offset;
3930 for(i2 = h_offset, h = 0; i2 < FONT_HEIGHT && h < height; i2++, h++, glyph_offset++)
3932 current_row = _font_bits[glyph_offset];
3933 for(i3 = 0; i3 < FONT_WIDTH; i3++)
3935 if((current_row >> (15 - i3)) & 0x01)
3936 *dest_ptr = fg_color;
3938 *dest_ptr = bg_color;
3941 dest_ptr += (pitch - FONT_WIDTH);
3943 dest_ptr = dest_ptr - (pitch * h) + FONT_WIDTH;
3948 current_char = str[str_index];
3950 if((i < pad) && (current_char == 0))
3959 if(current_x + FONT_WIDTH > resolution_width /* EDIT */)
3961 while (current_char && current_char != '\n')
3963 current_char = str[str_index++];
3969 void print_string(const char *str, u16 fg_color, u16 bg_color,
3973 if ((screen_scale == unscaled_rot || screen_scale == scaled_aspect_rot) &&
3974 (resolution_width == small_resolution_width) &&
3975 (resolution_height == small_resolution_height))
3977 snprintf(rot_msg_buff, sizeof(rot_msg_buff), "%s", str);
3981 print_string_ext(str, fg_color, bg_color, x, y, get_screen_pixels(),
3982 get_screen_pitch(), 0, 0, FONT_HEIGHT);
3985 void print_string_pad(const char *str, u16 fg_color, u16 bg_color,
3986 u32 x, u32 y, u32 pad)
3988 print_string_ext(str, fg_color, bg_color, x, y, get_screen_pixels(),
3989 get_screen_pitch(), pad, 0, FONT_HEIGHT);
3992 u32 debug_cursor_x = 0;
3993 u32 debug_cursor_y = 0;
3997 void debug_screen_clear()
4001 void debug_screen_start()
4005 void debug_screen_end()
4009 void debug_screen_update()
4013 void debug_screen_printf(const char *format, ...)
4017 va_start(ap, format);
4018 vprintf(format, ap);
4022 void debug_screen_newline(u32 count)
4030 void debug_screen_clear()
4034 clear_screen(0x0000);
4037 void debug_screen_start()
4039 video_resolution_large();
4040 debug_screen_clear();
4043 void debug_screen_end()
4045 video_resolution_small();
4048 void debug_screen_update()
4053 void debug_screen_printf(const char *format, ...)
4055 char str_buffer[512];
4056 u32 str_buffer_length;
4059 va_start(ap, format);
4060 str_buffer_length = vsnprintf(str_buffer, 512, format, ap);
4063 printf("printing debug string %s at %d %d\n", str_buffer,
4064 debug_cursor_x, debug_cursor_y);
4066 print_string(str_buffer, 0xFFFF, 0x0000, debug_cursor_x, debug_cursor_y);
4067 debug_cursor_x += FONT_WIDTH * str_buffer_length;
4070 void debug_screen_newline(u32 count)
4073 debug_cursor_y += FONT_HEIGHT * count;
4078 void debug_screen_printl(const char *format, ...)
4082 va_start(ap, format);
4083 debug_screen_printf(format, ap);
4084 debug_screen_newline(1);
4085 // debug_screen_printf("\n");
4090 #define video_savestate_builder(type) \
4091 void video_##type##_savestate(file_tag_type savestate_file) \
4093 file_##type##_array(savestate_file, affine_reference_x); \
4094 file_##type##_array(savestate_file, affine_reference_y); \
4097 video_savestate_builder(read);
4098 video_savestate_builder(write_mem);