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 u32 screen_offset = 0;
91 static u16 *screen_pixels = NULL;
92 const u32 screen_pitch = 320;
94 #define get_screen_pixels() \
97 #define get_screen_pitch() \
103 #include "SDL_gp2x.h"
104 SDL_Surface *hw_screen;
107 const u32 video_scale = 1;
109 #define get_screen_pixels() \
110 ((u16 *)screen->pixels) \
112 #define get_screen_pitch() \
113 (screen->pitch / 2) \
117 void render_scanline_conditional_tile(u32 start, u32 end, u16 *scanline,
118 u32 enable_flags, u32 dispcnt, u32 bldcnt, tile_layer_render_struct
120 void render_scanline_conditional_bitmap(u32 start, u32 end, u16 *scanline,
121 u32 enable_flags, u32 dispcnt, u32 bldcnt, bitmap_layer_render_struct
126 // This old version is not necessary if the palette is either being converted
127 // transparently or the ABGR 1555 format is being used natively. The direct
128 // version (without conversion) is much faster.
130 #define tile_lookup_palette_full(palette, source) \
131 current_pixel = palette[source]; \
132 convert_palette(current_pixel) \
134 #define tile_lookup_palette(palette, source) \
135 current_pixel = palette[source]; \
138 #ifdef RENDER_COLOR16_NORMAL
140 #define tile_expand_base_normal(index) \
141 tile_expand_base_color16(index) \
145 #define tile_expand_base_normal(index) \
146 tile_lookup_palette(palette, current_pixel); \
147 dest_ptr[index] = current_pixel \
151 #define tile_expand_transparent_normal(index) \
152 tile_expand_base_normal(index) \
154 #define tile_expand_copy(index) \
155 dest_ptr[index] = copy_ptr[index] \
158 #define advance_dest_ptr_base(delta) \
161 #define advance_dest_ptr_transparent(delta) \
162 advance_dest_ptr_base(delta) \
164 #define advance_dest_ptr_copy(delta) \
165 advance_dest_ptr_base(delta); \
169 #define color_combine_mask_a(layer) \
170 ((io_registers[REG_BLDCNT] >> layer) & 0x01) \
172 // For color blending operations, will create a mask that has in bit
173 // 10 if the layer is target B, and bit 9 if the layer is target A.
175 #define color_combine_mask(layer) \
176 (color_combine_mask_a(layer) | \
177 ((io_registers[REG_BLDCNT] >> (layer + 7)) & 0x02)) << 9 \
179 // For alpha blending renderers, draw the palette index (9bpp) and
180 // layer bits rather than the raw RGB. For the base this should write to
181 // the 32bit location directly.
183 #define tile_expand_base_alpha(index) \
184 dest_ptr[index] = current_pixel | pixel_combine \
186 #define tile_expand_base_bg(index) \
187 dest_ptr[index] = bg_combine \
190 // For layered (transparent) writes this should shift the "stack" and write
191 // to the bottom. This will preserve the topmost pixel and the most recent
194 #define tile_expand_transparent_alpha(index) \
195 dest_ptr[index] = (dest_ptr[index] << 16) | current_pixel | pixel_combine \
198 // OBJ should only shift if the top isn't already OBJ
199 #define tile_expand_transparent_alpha_obj(index) \
200 dest = dest_ptr[index]; \
201 if(dest & 0x00000100) \
203 dest_ptr[index] = (dest & 0xFFFF0000) | current_pixel | pixel_combine; \
207 dest_ptr[index] = (dest << 16) | current_pixel | pixel_combine; \
211 // For color effects that don't need to preserve the previous layer.
212 // The color32 version should be used with 32bit wide dest_ptr so as to be
213 // compatible with alpha combine on top of it.
215 #define tile_expand_base_color16(index) \
216 dest_ptr[index] = current_pixel | pixel_combine \
218 #define tile_expand_transparent_color16(index) \
219 tile_expand_base_color16(index) \
221 #define tile_expand_base_color32(index) \
222 tile_expand_base_color16(index) \
224 #define tile_expand_transparent_color32(index) \
225 tile_expand_base_color16(index) \
228 // Operations for isolation 8bpp pixels within 32bpp pixel blocks.
230 #define tile_8bpp_pixel_op_mask(op_param) \
231 current_pixel = current_pixels & 0xFF \
233 #define tile_8bpp_pixel_op_shift_mask(shift) \
234 current_pixel = (current_pixels >> shift) & 0xFF \
236 #define tile_8bpp_pixel_op_shift(shift) \
237 current_pixel = current_pixels >> shift \
239 #define tile_8bpp_pixel_op_none(shift) \
241 // Base should always draw raw in 8bpp mode; color 0 will be drawn where
244 #define tile_8bpp_draw_base_normal(index) \
245 tile_expand_base_normal(index) \
247 #define tile_8bpp_draw_base_alpha(index) \
250 tile_expand_base_alpha(index); \
254 tile_expand_base_bg(index); \
258 #define tile_8bpp_draw_base_color16(index) \
259 tile_8bpp_draw_base_alpha(index) \
261 #define tile_8bpp_draw_base_color32(index) \
262 tile_8bpp_draw_base_alpha(index) \
265 #define tile_8bpp_draw_base(index, op, op_param, alpha_op) \
266 tile_8bpp_pixel_op_##op(op_param); \
267 tile_8bpp_draw_base_##alpha_op(index) \
269 // Transparent (layered) writes should only replace what is there if the
270 // pixel is not transparent (zero)
272 #define tile_8bpp_draw_transparent(index, op, op_param, alpha_op) \
273 tile_8bpp_pixel_op_##op(op_param); \
276 tile_expand_transparent_##alpha_op(index); \
279 #define tile_8bpp_draw_copy(index, op, op_param, alpha_op) \
280 tile_8bpp_pixel_op_##op(op_param); \
283 tile_expand_copy(index); \
286 // Get the current tile from the map in 8bpp mode
288 #define get_tile_8bpp() \
289 current_tile = *map_ptr; \
290 tile_ptr = tile_base + ((current_tile & 0x3FF) * 64) \
293 // Draw half of a tile in 8bpp mode, for base renderer
295 #define tile_8bpp_draw_four_noflip(index, combine_op, alpha_op) \
296 tile_8bpp_draw_##combine_op(index + 0, mask, 0, alpha_op); \
297 tile_8bpp_draw_##combine_op(index + 1, shift_mask, 8, alpha_op); \
298 tile_8bpp_draw_##combine_op(index + 2, shift_mask, 16, alpha_op); \
299 tile_8bpp_draw_##combine_op(index + 3, shift, 24, alpha_op) \
302 // Like the above, but draws the half-tile horizontally flipped
304 #define tile_8bpp_draw_four_flip(index, combine_op, alpha_op) \
305 tile_8bpp_draw_##combine_op(index + 3, mask, 0, alpha_op); \
306 tile_8bpp_draw_##combine_op(index + 2, shift_mask, 8, alpha_op); \
307 tile_8bpp_draw_##combine_op(index + 1, shift_mask, 16, alpha_op); \
308 tile_8bpp_draw_##combine_op(index + 0, shift, 24, alpha_op) \
310 #define tile_8bpp_draw_four_base(index, alpha_op, flip_op) \
311 tile_8bpp_draw_four_##flip_op(index, base, alpha_op) \
314 // Draw half of a tile in 8bpp mode, for transparent renderer; as an
315 // optimization the entire thing is checked against zero (in transparent
316 // capable renders it is more likely for the pixels to be transparent than
319 #define tile_8bpp_draw_four_transparent(index, alpha_op, flip_op) \
320 if(current_pixels != 0) \
322 tile_8bpp_draw_four_##flip_op(index, transparent, alpha_op); \
325 #define tile_8bpp_draw_four_copy(index, alpha_op, flip_op) \
326 if(current_pixels != 0) \
328 tile_8bpp_draw_four_##flip_op(index, copy, alpha_op); \
331 // Helper macro for drawing 8bpp tiles clipped against the edge of the screen
333 #define partial_tile_8bpp(combine_op, alpha_op) \
334 for(i = 0; i < partial_tile_run; i++) \
336 tile_8bpp_draw_##combine_op(0, mask, 0, alpha_op); \
337 current_pixels >>= 8; \
338 advance_dest_ptr_##combine_op(1); \
342 // Draws 8bpp tiles clipped against the left side of the screen,
343 // partial_tile_offset indicates how much clipped in it is, partial_tile_run
344 // indicates how much it should draw.
346 #define partial_tile_right_noflip_8bpp(combine_op, alpha_op) \
347 if(partial_tile_offset >= 4) \
349 current_pixels = *((u32 *)(tile_ptr + 4)) >> \
350 ((partial_tile_offset - 4) * 8); \
351 partial_tile_8bpp(combine_op, alpha_op); \
355 partial_tile_run -= 4; \
356 current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 8); \
357 partial_tile_8bpp(combine_op, alpha_op); \
358 current_pixels = *((u32 *)(tile_ptr + 4)); \
359 tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
360 advance_dest_ptr_##combine_op(4); \
364 // Draws 8bpp tiles clipped against both the left and right side of the
365 // screen, IE, runs of less than 8 - partial_tile_offset.
367 #define partial_tile_mid_noflip_8bpp(combine_op, alpha_op) \
368 if(partial_tile_offset >= 4) \
370 current_pixels = *((u32 *)(tile_ptr + 4)) >> \
371 ((partial_tile_offset - 4) * 8); \
372 partial_tile_8bpp(combine_op, alpha_op); \
376 current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 8); \
377 if((partial_tile_offset + partial_tile_run) > 4) \
379 u32 old_run = partial_tile_run; \
380 partial_tile_run = 4 - partial_tile_offset; \
381 partial_tile_8bpp(combine_op, alpha_op); \
382 partial_tile_run = old_run - partial_tile_run; \
383 current_pixels = *((u32 *)(tile_ptr + 4)); \
384 partial_tile_8bpp(combine_op, alpha_op); \
388 partial_tile_8bpp(combine_op, alpha_op); \
393 // Draws 8bpp tiles clipped against the right side of the screen,
394 // partial_tile_run indicates how much there is to draw.
396 #define partial_tile_left_noflip_8bpp(combine_op, alpha_op) \
397 if(partial_tile_run >= 4) \
399 current_pixels = *((u32 *)tile_ptr); \
400 tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
401 advance_dest_ptr_##combine_op(4); \
403 partial_tile_run -= 4; \
406 current_pixels = *((u32 *)(tile_ptr)); \
407 partial_tile_8bpp(combine_op, alpha_op) \
410 // Draws a non-clipped (complete) 8bpp tile.
412 #define tile_noflip_8bpp(combine_op, alpha_op) \
413 current_pixels = *((u32 *)tile_ptr); \
414 tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
415 current_pixels = *((u32 *)(tile_ptr + 4)); \
416 tile_8bpp_draw_four_##combine_op(4, alpha_op, noflip) \
419 // Like the above versions but draws flipped tiles.
421 #define partial_tile_flip_8bpp(combine_op, alpha_op) \
422 for(i = 0; i < partial_tile_run; i++) \
424 tile_8bpp_draw_##combine_op(0, shift, 24, alpha_op); \
425 current_pixels <<= 8; \
426 advance_dest_ptr_##combine_op(1); \
429 #define partial_tile_right_flip_8bpp(combine_op, alpha_op) \
430 if(partial_tile_offset >= 4) \
432 current_pixels = *((u32 *)tile_ptr) << ((partial_tile_offset - 4) * 8); \
433 partial_tile_flip_8bpp(combine_op, alpha_op); \
437 partial_tile_run -= 4; \
438 current_pixels = *((u32 *)(tile_ptr + 4)) << \
439 ((partial_tile_offset - 4) * 8); \
440 partial_tile_flip_8bpp(combine_op, alpha_op); \
441 current_pixels = *((u32 *)tile_ptr); \
442 tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
443 advance_dest_ptr_##combine_op(4); \
446 #define partial_tile_mid_flip_8bpp(combine_op, alpha_op) \
447 if(partial_tile_offset >= 4) \
449 current_pixels = *((u32 *)tile_ptr) << ((partial_tile_offset - 4) * 8); \
450 partial_tile_flip_8bpp(combine_op, alpha_op); \
454 current_pixels = *((u32 *)(tile_ptr + 4)) << \
455 ((partial_tile_offset - 4) * 8); \
457 if((partial_tile_offset + partial_tile_run) > 4) \
459 u32 old_run = partial_tile_run; \
460 partial_tile_run = 4 - partial_tile_offset; \
461 partial_tile_flip_8bpp(combine_op, alpha_op); \
462 partial_tile_run = old_run - partial_tile_run; \
463 current_pixels = *((u32 *)(tile_ptr)); \
464 partial_tile_flip_8bpp(combine_op, alpha_op); \
468 partial_tile_flip_8bpp(combine_op, alpha_op); \
472 #define partial_tile_left_flip_8bpp(combine_op, alpha_op) \
473 if(partial_tile_run >= 4) \
475 current_pixels = *((u32 *)(tile_ptr + 4)); \
476 tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
477 advance_dest_ptr_##combine_op(4); \
479 partial_tile_run -= 4; \
482 current_pixels = *((u32 *)(tile_ptr + 4)); \
483 partial_tile_flip_8bpp(combine_op, alpha_op) \
485 #define tile_flip_8bpp(combine_op, alpha_op) \
486 current_pixels = *((u32 *)(tile_ptr + 4)); \
487 tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
488 current_pixels = *((u32 *)tile_ptr); \
489 tile_8bpp_draw_four_##combine_op(4, alpha_op, flip) \
492 // Operations for isolating 4bpp tiles in a 32bit block
494 #define tile_4bpp_pixel_op_mask(op_param) \
495 current_pixel = current_pixels & 0x0F \
497 #define tile_4bpp_pixel_op_shift_mask(shift) \
498 current_pixel = (current_pixels >> shift) & 0x0F \
500 #define tile_4bpp_pixel_op_shift(shift) \
501 current_pixel = current_pixels >> shift \
503 #define tile_4bpp_pixel_op_none(op_param) \
505 // Draws a single 4bpp pixel as base, normal renderer; checks to see if the
506 // pixel is zero because if so the current palette should not be applied.
507 // These ifs can be replaced with a lookup table, may or may not be superior
508 // this way, should be benchmarked. The lookup table would be from 0-255
509 // identity map except for multiples of 16, which would map to 0.
511 #define tile_4bpp_draw_base_normal(index) \
514 current_pixel |= current_palette; \
515 tile_expand_base_normal(index); \
519 tile_expand_base_normal(index); \
523 #define tile_4bpp_draw_base_alpha(index) \
526 current_pixel |= current_palette; \
527 tile_expand_base_alpha(index); \
531 tile_expand_base_bg(index); \
534 #define tile_4bpp_draw_base_color16(index) \
535 tile_4bpp_draw_base_alpha(index) \
537 #define tile_4bpp_draw_base_color32(index) \
538 tile_4bpp_draw_base_alpha(index) \
541 #define tile_4bpp_draw_base(index, op, op_param, alpha_op) \
542 tile_4bpp_pixel_op_##op(op_param); \
543 tile_4bpp_draw_base_##alpha_op(index) \
546 // Draws a single 4bpp pixel as layered, if not transparent.
548 #define tile_4bpp_draw_transparent(index, op, op_param, alpha_op) \
549 tile_4bpp_pixel_op_##op(op_param); \
552 current_pixel |= current_palette; \
553 tile_expand_transparent_##alpha_op(index); \
556 #define tile_4bpp_draw_copy(index, op, op_param, alpha_op) \
557 tile_4bpp_pixel_op_##op(op_param); \
560 current_pixel |= current_palette; \
561 tile_expand_copy(index); \
565 // Draws eight background pixels in transparent mode, for alpha or normal
568 #define tile_4bpp_draw_eight_base_zero(value) \
569 dest_ptr[0] = value; \
570 dest_ptr[1] = value; \
571 dest_ptr[2] = value; \
572 dest_ptr[3] = value; \
573 dest_ptr[4] = value; \
574 dest_ptr[5] = value; \
575 dest_ptr[6] = value; \
576 dest_ptr[7] = value \
579 // Draws eight background pixels for the alpha renderer, basically color zero
580 // with the background flag high.
582 #define tile_4bpp_draw_eight_base_zero_alpha() \
583 tile_4bpp_draw_eight_base_zero(bg_combine) \
585 #define tile_4bpp_draw_eight_base_zero_color16() \
586 tile_4bpp_draw_eight_base_zero_alpha() \
588 #define tile_4bpp_draw_eight_base_zero_color32() \
589 tile_4bpp_draw_eight_base_zero_alpha() \
592 // Draws eight background pixels for the normal renderer, just a bunch of
595 #ifdef RENDER_COLOR16_NORMAL
597 #define tile_4bpp_draw_eight_base_zero_normal() \
599 tile_4bpp_draw_eight_base_zero(current_pixel) \
603 #define tile_4bpp_draw_eight_base_zero_normal() \
604 current_pixel = palette[0]; \
605 tile_4bpp_draw_eight_base_zero(current_pixel) \
610 // Draws eight 4bpp pixels.
612 #define tile_4bpp_draw_eight_noflip(combine_op, alpha_op) \
613 tile_4bpp_draw_##combine_op(0, mask, 0, alpha_op); \
614 tile_4bpp_draw_##combine_op(1, shift_mask, 4, alpha_op); \
615 tile_4bpp_draw_##combine_op(2, shift_mask, 8, alpha_op); \
616 tile_4bpp_draw_##combine_op(3, shift_mask, 12, alpha_op); \
617 tile_4bpp_draw_##combine_op(4, shift_mask, 16, alpha_op); \
618 tile_4bpp_draw_##combine_op(5, shift_mask, 20, alpha_op); \
619 tile_4bpp_draw_##combine_op(6, shift_mask, 24, alpha_op); \
620 tile_4bpp_draw_##combine_op(7, shift, 28, alpha_op) \
623 // Draws eight 4bpp pixels in reverse order (for hflip).
625 #define tile_4bpp_draw_eight_flip(combine_op, alpha_op) \
626 tile_4bpp_draw_##combine_op(7, mask, 0, alpha_op); \
627 tile_4bpp_draw_##combine_op(6, shift_mask, 4, alpha_op); \
628 tile_4bpp_draw_##combine_op(5, shift_mask, 8, alpha_op); \
629 tile_4bpp_draw_##combine_op(4, shift_mask, 12, alpha_op); \
630 tile_4bpp_draw_##combine_op(3, shift_mask, 16, alpha_op); \
631 tile_4bpp_draw_##combine_op(2, shift_mask, 20, alpha_op); \
632 tile_4bpp_draw_##combine_op(1, shift_mask, 24, alpha_op); \
633 tile_4bpp_draw_##combine_op(0, shift, 28, alpha_op) \
636 // Draws eight 4bpp pixels in base mode, checks if all are zero, if so draws
637 // the appropriate background pixels.
639 #define tile_4bpp_draw_eight_base(alpha_op, flip_op) \
640 if(current_pixels != 0) \
642 tile_4bpp_draw_eight_##flip_op(base, alpha_op); \
646 tile_4bpp_draw_eight_base_zero_##alpha_op(); \
650 // Draws eight 4bpp pixels in transparent (layered) mode, checks if all are
651 // zero and if so draws nothing.
653 #define tile_4bpp_draw_eight_transparent(alpha_op, flip_op) \
654 if(current_pixels != 0) \
656 tile_4bpp_draw_eight_##flip_op(transparent, alpha_op); \
660 #define tile_4bpp_draw_eight_copy(alpha_op, flip_op) \
661 if(current_pixels != 0) \
663 tile_4bpp_draw_eight_##flip_op(copy, alpha_op); \
666 // Gets the current tile in 4bpp mode, also getting the current palette and
669 #define get_tile_4bpp() \
670 current_tile = *map_ptr; \
671 current_palette = (current_tile >> 12) << 4; \
672 tile_ptr = tile_base + ((current_tile & 0x3FF) * 32); \
675 // Helper macro for drawing clipped 4bpp tiles.
677 #define partial_tile_4bpp(combine_op, alpha_op) \
678 for(i = 0; i < partial_tile_run; i++) \
680 tile_4bpp_draw_##combine_op(0, mask, 0, alpha_op); \
681 current_pixels >>= 4; \
682 advance_dest_ptr_##combine_op(1); \
686 // Draws a 4bpp tile clipped against the left edge of the screen.
687 // partial_tile_offset is how far in it's clipped, partial_tile_run is
690 #define partial_tile_right_noflip_4bpp(combine_op, alpha_op) \
691 current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 4); \
692 partial_tile_4bpp(combine_op, alpha_op) \
695 // Draws a 4bpp tile clipped against both edges of the screen, same as right.
697 #define partial_tile_mid_noflip_4bpp(combine_op, alpha_op) \
698 partial_tile_right_noflip_4bpp(combine_op, alpha_op) \
701 // Draws a 4bpp tile clipped against the right edge of the screen.
702 // partial_tile_offset is how many to draw.
704 #define partial_tile_left_noflip_4bpp(combine_op, alpha_op) \
705 current_pixels = *((u32 *)tile_ptr); \
706 partial_tile_4bpp(combine_op, alpha_op) \
709 // Draws a complete 4bpp tile row (not clipped)
710 #define tile_noflip_4bpp(combine_op, alpha_op) \
711 current_pixels = *((u32 *)tile_ptr); \
712 tile_4bpp_draw_eight_##combine_op(alpha_op, noflip) \
715 // Like the above, but draws flipped tiles.
717 #define partial_tile_flip_4bpp(combine_op, alpha_op) \
718 for(i = 0; i < partial_tile_run; i++) \
720 tile_4bpp_draw_##combine_op(0, shift, 28, alpha_op); \
721 current_pixels <<= 4; \
722 advance_dest_ptr_##combine_op(1); \
725 #define partial_tile_right_flip_4bpp(combine_op, alpha_op) \
726 current_pixels = *((u32 *)tile_ptr) << (partial_tile_offset * 4); \
727 partial_tile_flip_4bpp(combine_op, alpha_op) \
729 #define partial_tile_mid_flip_4bpp(combine_op, alpha_op) \
730 partial_tile_right_flip_4bpp(combine_op, alpha_op) \
732 #define partial_tile_left_flip_4bpp(combine_op, alpha_op) \
733 current_pixels = *((u32 *)tile_ptr); \
734 partial_tile_flip_4bpp(combine_op, alpha_op) \
736 #define tile_flip_4bpp(combine_op, alpha_op) \
737 current_pixels = *((u32 *)tile_ptr); \
738 tile_4bpp_draw_eight_##combine_op(alpha_op, flip) \
741 // Draws a single (partial or complete) tile from the tilemap, flipping
744 #define single_tile_map(tile_type, combine_op, color_depth, alpha_op) \
745 get_tile_##color_depth(); \
746 if(current_tile & 0x800) \
747 tile_ptr += vertical_pixel_flip; \
749 if(current_tile & 0x400) \
751 tile_type##_flip_##color_depth(combine_op, alpha_op); \
755 tile_type##_noflip_##color_depth(combine_op, alpha_op); \
759 // Draws multiple sequential tiles from the tilemap, hflips and vflips as
762 #define multiple_tile_map(combine_op, color_depth, alpha_op) \
763 for(i = 0; i < tile_run; i++) \
765 single_tile_map(tile, combine_op, color_depth, alpha_op); \
766 advance_dest_ptr_##combine_op(8); \
770 // Draws a partial tile from a tilemap clipped against the left edge of the
773 #define partial_tile_right_map(combine_op, color_depth, alpha_op) \
774 single_tile_map(partial_tile_right, combine_op, color_depth, alpha_op); \
777 // Draws a partial tile from a tilemap clipped against both edges of the
780 #define partial_tile_mid_map(combine_op, color_depth, alpha_op) \
781 single_tile_map(partial_tile_mid, combine_op, color_depth, alpha_op) \
783 // Draws a partial tile from a tilemap clipped against the right edge of the
786 #define partial_tile_left_map(combine_op, color_depth, alpha_op) \
787 single_tile_map(partial_tile_left, combine_op, color_depth, alpha_op) \
790 // Advances a non-flipped 4bpp obj to the next tile.
792 #define obj_advance_noflip_4bpp() \
796 // Advances a non-flipped 8bpp obj to the next tile.
798 #define obj_advance_noflip_8bpp() \
802 // Advances a flipped 4bpp obj to the next tile.
804 #define obj_advance_flip_4bpp() \
808 // Advances a flipped 8bpp obj to the next tile.
810 #define obj_advance_flip_8bpp() \
815 // Draws multiple sequential tiles from an obj, flip_op determines if it should
816 // be flipped or not (set to flip or noflip)
818 #define multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op) \
819 for(i = 0; i < tile_run; i++) \
821 tile_##flip_op##_##color_depth(combine_op, alpha_op); \
822 obj_advance_##flip_op##_##color_depth(); \
823 advance_dest_ptr_##combine_op(8); \
827 // Draws an obj's tile clipped against the left side of the screen
829 #define partial_tile_right_obj(combine_op, color_depth, alpha_op, flip_op) \
830 partial_tile_right_##flip_op##_##color_depth(combine_op, alpha_op); \
831 obj_advance_##flip_op##_##color_depth() \
833 // Draws an obj's tile clipped against both sides of the screen
835 #define partial_tile_mid_obj(combine_op, color_depth, alpha_op, flip_op) \
836 partial_tile_mid_##flip_op##_##color_depth(combine_op, alpha_op) \
838 // Draws an obj's tile clipped against the right side of the screen
840 #define partial_tile_left_obj(combine_op, color_depth, alpha_op, flip_op) \
841 partial_tile_left_##flip_op##_##color_depth(combine_op, alpha_op) \
844 // Extra variables specific for 8bpp/4bpp tile renderers.
846 #define tile_extra_variables_8bpp() \
848 #define tile_extra_variables_4bpp() \
849 u32 current_palette \
852 // Byte lengths of complete tiles and tile rows in 4bpp and 8bpp.
854 #define tile_width_4bpp 4
855 #define tile_size_4bpp 32
856 #define tile_width_8bpp 8
857 #define tile_size_8bpp 64
860 // Render a single scanline of text tiles
862 #define tile_render(color_depth, combine_op, alpha_op) \
864 u32 vertical_pixel_offset = (vertical_offset % 8) * \
865 tile_width_##color_depth; \
866 u32 vertical_pixel_flip = \
867 ((tile_size_##color_depth - tile_width_##color_depth) - \
868 vertical_pixel_offset) - vertical_pixel_offset; \
869 tile_extra_variables_##color_depth(); \
870 u8 *tile_base = vram + (((bg_control >> 2) & 0x03) * (1024 * 16)) + \
871 vertical_pixel_offset; \
872 u32 pixel_run = 256 - (horizontal_offset % 256); \
875 map_base += ((vertical_offset % 256) / 8) * 32; \
876 partial_tile_offset = (horizontal_offset % 8); \
878 if(pixel_run >= end) \
880 if(partial_tile_offset) \
882 partial_tile_run = 8 - partial_tile_offset; \
883 if(end < partial_tile_run) \
885 partial_tile_run = end; \
886 partial_tile_mid_map(combine_op, color_depth, alpha_op); \
891 end -= partial_tile_run; \
892 partial_tile_right_map(combine_op, color_depth, alpha_op); \
896 tile_run = end / 8; \
897 multiple_tile_map(combine_op, color_depth, alpha_op); \
899 partial_tile_run = end % 8; \
901 if(partial_tile_run) \
903 partial_tile_left_map(combine_op, color_depth, alpha_op); \
908 if(partial_tile_offset) \
910 partial_tile_run = 8 - partial_tile_offset; \
911 partial_tile_right_map(combine_op, color_depth, alpha_op); \
914 tile_run = (pixel_run - partial_tile_run) / 8; \
915 multiple_tile_map(combine_op, color_depth, alpha_op); \
916 map_ptr = second_ptr; \
918 tile_run = end / 8; \
919 multiple_tile_map(combine_op, color_depth, alpha_op); \
921 partial_tile_run = end % 8; \
922 if(partial_tile_run) \
924 partial_tile_left_map(combine_op, color_depth, alpha_op); \
929 #define render_scanline_dest_normal u16
930 #define render_scanline_dest_alpha u32
931 #define render_scanline_dest_alpha_obj u32
932 #define render_scanline_dest_color16 u16
933 #define render_scanline_dest_color32 u32
934 #define render_scanline_dest_partial_alpha u32
935 #define render_scanline_dest_copy_tile u16
936 #define render_scanline_dest_copy_bitmap u16
939 // If rendering a scanline that is not a target A then there's no point in
940 // keeping what's underneath it because it can't blend with it.
942 #define render_scanline_skip_alpha(bg_type, combine_op) \
943 if((pixel_combine & 0x00000200) == 0) \
945 render_scanline_##bg_type##_##combine_op##_color32(layer, \
946 start, end, scanline); \
951 #ifdef RENDER_COLOR16_NORMAL
953 #define render_scanline_extra_variables_base_normal(bg_type) \
954 const u32 pixel_combine = 0 \
958 #define render_scanline_extra_variables_base_normal(bg_type) \
959 u16 *palette = palette_ram_converted \
964 #define render_scanline_extra_variables_base_alpha(bg_type) \
965 u32 bg_combine = color_combine_mask(5); \
966 u32 pixel_combine = color_combine_mask(layer) | (bg_combine << 16); \
967 render_scanline_skip_alpha(bg_type, base) \
969 #define render_scanline_extra_variables_base_color() \
970 u32 bg_combine = color_combine_mask(5); \
971 u32 pixel_combine = color_combine_mask(layer) \
973 #define render_scanline_extra_variables_base_color16(bg_type) \
974 render_scanline_extra_variables_base_color() \
976 #define render_scanline_extra_variables_base_color32(bg_type) \
977 render_scanline_extra_variables_base_color() \
980 #define render_scanline_extra_variables_transparent_normal(bg_type) \
981 render_scanline_extra_variables_base_normal(bg_type) \
983 #define render_scanline_extra_variables_transparent_alpha(bg_type) \
984 u32 pixel_combine = color_combine_mask(layer); \
985 render_scanline_skip_alpha(bg_type, transparent) \
987 #define render_scanline_extra_variables_transparent_color() \
988 u32 pixel_combine = color_combine_mask(layer) \
990 #define render_scanline_extra_variables_transparent_color16(bg_type) \
991 render_scanline_extra_variables_transparent_color() \
993 #define render_scanline_extra_variables_transparent_color32(bg_type) \
994 render_scanline_extra_variables_transparent_color() \
1000 // Map widths and heights
1002 u32 map_widths[] = { 256, 512, 256, 512 };
1003 u32 map_heights[] = { 256, 256, 512, 512 };
1005 // Build text scanline rendering functions.
1007 #define render_scanline_text_builder(combine_op, alpha_op) \
1008 void render_scanline_text_##combine_op##_##alpha_op(u32 layer, \
1009 u32 start, u32 end, void *scanline) \
1011 render_scanline_extra_variables_##combine_op##_##alpha_op(text); \
1012 u32 bg_control = io_registers[REG_BG0CNT + layer]; \
1013 u32 map_size = (bg_control >> 14) & 0x03; \
1014 u32 map_width = map_widths[map_size]; \
1015 u32 map_height = map_heights[map_size]; \
1016 u32 horizontal_offset = \
1017 (io_registers[REG_BG0HOFS + (layer * 2)] + start) % 512; \
1018 u32 vertical_offset = (io_registers[REG_VCOUNT] + \
1019 io_registers[REG_BG0VOFS + (layer * 2)]) % 512; \
1020 u32 current_pixel; \
1021 u32 current_pixels; \
1022 u32 partial_tile_run = 0; \
1023 u32 partial_tile_offset; \
1026 render_scanline_dest_##alpha_op *dest_ptr = \
1027 ((render_scanline_dest_##alpha_op *)scanline) + start; \
1029 u16 *map_base = (u16 *)(vram + ((bg_control >> 8) & 0x1F) * (1024 * 2)); \
1030 u16 *map_ptr, *second_ptr; \
1035 if((map_size & 0x02) && (vertical_offset >= 256)) \
1037 map_base += ((map_width / 8) * 32) + \
1038 (((vertical_offset - 256) / 8) * 32); \
1042 map_base += (((vertical_offset % 256) / 8) * 32); \
1045 if(map_size & 0x01) \
1047 if(horizontal_offset >= 256) \
1049 horizontal_offset -= 256; \
1050 map_ptr = map_base + (32 * 32) + (horizontal_offset / 8); \
1051 second_ptr = map_base; \
1055 map_ptr = map_base + (horizontal_offset / 8); \
1056 second_ptr = map_base + (32 * 32); \
1061 horizontal_offset %= 256; \
1062 map_ptr = map_base + (horizontal_offset / 8); \
1063 second_ptr = map_base; \
1066 if(bg_control & 0x80) \
1068 tile_render(8bpp, combine_op, alpha_op); \
1072 tile_render(4bpp, combine_op, alpha_op); \
1076 render_scanline_text_builder(base, normal);
1077 render_scanline_text_builder(transparent, normal);
1078 render_scanline_text_builder(base, color16);
1079 render_scanline_text_builder(transparent, color16);
1080 render_scanline_text_builder(base, color32);
1081 render_scanline_text_builder(transparent, color32);
1082 render_scanline_text_builder(base, alpha);
1083 render_scanline_text_builder(transparent, alpha);
1086 s32 affine_reference_x[2];
1087 s32 affine_reference_y[2];
1089 #define affine_render_bg_pixel_normal() \
1090 current_pixel = palette_ram_converted[0] \
1092 #define affine_render_bg_pixel_alpha() \
1093 current_pixel = bg_combine \
1095 #define affine_render_bg_pixel_color16() \
1096 affine_render_bg_pixel_alpha() \
1098 #define affine_render_bg_pixel_color32() \
1099 affine_render_bg_pixel_alpha() \
1101 #define affine_render_bg_pixel_base(alpha_op) \
1102 affine_render_bg_pixel_##alpha_op() \
1104 #define affine_render_bg_pixel_transparent(alpha_op) \
1106 #define affine_render_bg_pixel_copy(alpha_op) \
1108 #define affine_render_bg_base(alpha_op) \
1109 dest_ptr[0] = current_pixel
1111 #define affine_render_bg_transparent(alpha_op) \
1113 #define affine_render_bg_copy(alpha_op) \
1115 #define affine_render_bg_remainder_base(alpha_op) \
1116 affine_render_bg_pixel_##alpha_op(); \
1117 for(; i < end; i++) \
1119 affine_render_bg_base(alpha_op); \
1120 advance_dest_ptr_base(1); \
1123 #define affine_render_bg_remainder_transparent(alpha_op) \
1125 #define affine_render_bg_remainder_copy(alpha_op) \
1127 #define affine_render_next(combine_op) \
1130 advance_dest_ptr_##combine_op(1) \
1132 #define affine_render_scale_offset() \
1133 tile_base += ((pixel_y % 8) * 8); \
1134 map_base += (pixel_y / 8) << map_pitch \
1136 #define affine_render_scale_pixel(combine_op, alpha_op) \
1137 map_offset = (pixel_x / 8); \
1138 if(map_offset != last_map_offset) \
1140 tile_ptr = tile_base + (map_base[map_offset] * 64); \
1141 last_map_offset = map_offset; \
1143 tile_ptr = tile_base + (map_base[(pixel_x / 8)] * 64); \
1144 current_pixel = tile_ptr[(pixel_x % 8)]; \
1145 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op); \
1146 affine_render_next(combine_op) \
1148 #define affine_render_scale(combine_op, alpha_op) \
1150 pixel_y = source_y >> 8; \
1152 affine_render_bg_pixel_##combine_op(alpha_op); \
1153 if((u32)pixel_y < (u32)width_height) \
1155 affine_render_scale_offset(); \
1156 for(; i < end; i++) \
1158 pixel_x = source_x >> 8; \
1160 if((u32)pixel_x < (u32)width_height) \
1165 affine_render_bg_##combine_op(alpha_op); \
1166 affine_render_next(combine_op); \
1169 for(; i < end; i++) \
1171 pixel_x = source_x >> 8; \
1173 if((u32)pixel_x >= (u32)width_height) \
1176 affine_render_scale_pixel(combine_op, alpha_op); \
1179 affine_render_bg_remainder_##combine_op(alpha_op); \
1182 #define affine_render_scale_wrap(combine_op, alpha_op) \
1184 u32 wrap_mask = width_height - 1; \
1185 pixel_y = (source_y >> 8) & wrap_mask; \
1186 if((u32)pixel_y < (u32)width_height) \
1188 affine_render_scale_offset(); \
1189 for(i = 0; i < end; i++) \
1191 pixel_x = (source_x >> 8) & wrap_mask; \
1192 affine_render_scale_pixel(combine_op, alpha_op); \
1198 #define affine_render_rotate_pixel(combine_op, alpha_op) \
1199 map_offset = (pixel_x / 8) + ((pixel_y / 8) << map_pitch); \
1200 if(map_offset != last_map_offset) \
1202 tile_ptr = tile_base + (map_base[map_offset] * 64); \
1203 last_map_offset = map_offset; \
1206 current_pixel = tile_ptr[(pixel_x % 8) + ((pixel_y % 8) * 8)]; \
1207 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op); \
1208 affine_render_next(combine_op) \
1210 #define affine_render_rotate(combine_op, alpha_op) \
1212 affine_render_bg_pixel_##combine_op(alpha_op); \
1213 for(i = 0; i < end; i++) \
1215 pixel_x = source_x >> 8; \
1216 pixel_y = source_y >> 8; \
1218 if(((u32)pixel_x < (u32)width_height) && \
1219 ((u32)pixel_y < (u32)width_height)) \
1223 affine_render_bg_##combine_op(alpha_op); \
1224 affine_render_next(combine_op); \
1227 for(; i < end; i++) \
1229 pixel_x = source_x >> 8; \
1230 pixel_y = source_y >> 8; \
1232 if(((u32)pixel_x >= (u32)width_height) || \
1233 ((u32)pixel_y >= (u32)width_height)) \
1235 affine_render_bg_remainder_##combine_op(alpha_op); \
1239 affine_render_rotate_pixel(combine_op, alpha_op); \
1243 #define affine_render_rotate_wrap(combine_op, alpha_op) \
1245 u32 wrap_mask = width_height - 1; \
1246 for(i = 0; i < end; i++) \
1248 pixel_x = (source_x >> 8) & wrap_mask; \
1249 pixel_y = (source_y >> 8) & wrap_mask; \
1251 affine_render_rotate_pixel(combine_op, alpha_op); \
1256 // Build affine background renderers.
1258 #define render_scanline_affine_builder(combine_op, alpha_op) \
1259 void render_scanline_affine_##combine_op##_##alpha_op(u32 layer, \
1260 u32 start, u32 end, void *scanline) \
1262 render_scanline_extra_variables_##combine_op##_##alpha_op(affine); \
1263 u32 bg_control = io_registers[REG_BG0CNT + layer]; \
1264 u32 current_pixel; \
1265 s32 source_x, source_y; \
1266 u32 vcount = io_registers[REG_VCOUNT]; \
1267 u32 pixel_x, pixel_y; \
1268 u32 layer_offset = (layer - 2) * 8; \
1270 u32 map_size = (bg_control >> 14) & 0x03; \
1271 u32 width_height = 1 << (7 + map_size); \
1272 u32 map_pitch = map_size + 4; \
1273 u8 *map_base = vram + (((bg_control >> 8) & 0x1F) * (1024 * 2)); \
1274 u8 *tile_base = vram + (((bg_control >> 2) & 0x03) * (1024 * 16)); \
1276 u32 map_offset, last_map_offset = (u32)-1; \
1278 render_scanline_dest_##alpha_op *dest_ptr = \
1279 ((render_scanline_dest_##alpha_op *)scanline) + start; \
1281 dx = (s16)io_registers[REG_BG2PA + layer_offset]; \
1282 dy = (s16)io_registers[REG_BG2PC + layer_offset]; \
1283 source_x = affine_reference_x[layer - 2] + (start * dx); \
1284 source_y = affine_reference_y[layer - 2] + (start * dy); \
1288 switch(((bg_control >> 12) & 0x02) | (dy != 0)) \
1291 affine_render_scale(combine_op, alpha_op); \
1295 affine_render_rotate(combine_op, alpha_op); \
1299 affine_render_scale_wrap(combine_op, alpha_op); \
1303 affine_render_rotate_wrap(combine_op, alpha_op); \
1308 render_scanline_affine_builder(base, normal);
1309 render_scanline_affine_builder(transparent, normal);
1310 render_scanline_affine_builder(base, color16);
1311 render_scanline_affine_builder(transparent, color16);
1312 render_scanline_affine_builder(base, color32);
1313 render_scanline_affine_builder(transparent, color32);
1314 render_scanline_affine_builder(base, alpha);
1315 render_scanline_affine_builder(transparent, alpha);
1318 #define bitmap_render_pixel_mode3(alpha_op) \
1319 convert_palette(current_pixel); \
1320 *dest_ptr = current_pixel \
1322 #define bitmap_render_pixel_mode4(alpha_op) \
1323 tile_expand_base_##alpha_op(0) \
1325 #define bitmap_render_pixel_mode5(alpha_op) \
1326 bitmap_render_pixel_mode3(alpha_op) \
1329 #define bitmap_render_scale(type, alpha_op, width, height) \
1330 pixel_y = (source_y >> 8); \
1331 if((u32)pixel_y < (u32)height) \
1333 pixel_x = (source_x >> 8); \
1334 src_ptr += (pixel_y * width); \
1340 dest_ptr -= pixel_x; \
1347 src_ptr += pixel_x; \
1350 if((pixel_x + end) >= width) \
1351 end = (width - pixel_x); \
1353 for(i = 0; (s32)i < (s32)end; i++) \
1355 current_pixel = *src_ptr; \
1356 bitmap_render_pixel_##type(alpha_op); \
1363 if((u32)(source_y >> 8) < (u32)height) \
1365 for(i = 0; i < end; i++) \
1367 pixel_x = (source_x >> 8); \
1369 if((u32)pixel_x < (u32)width) \
1376 for(; i < end; i++) \
1378 pixel_x = (source_x >> 8); \
1380 if((u32)pixel_x >= (u32)width) \
1383 current_pixel = src_ptr[pixel_x]; \
1384 bitmap_render_pixel_##type(alpha_op); \
1393 #define bitmap_render_rotate(type, alpha_op, width, height) \
1394 for(i = 0; i < end; i++) \
1396 pixel_x = source_x >> 8; \
1397 pixel_y = source_y >> 8; \
1399 if(((u32)pixel_x < (u32)width) && ((u32)pixel_y < (u32)height)) \
1407 for(; i < end; i++) \
1409 pixel_x = (source_x >> 8); \
1410 pixel_y = (source_y >> 8); \
1412 if(((u32)pixel_x >= (u32)width) || ((u32)pixel_y >= (u32)height)) \
1415 current_pixel = src_ptr[pixel_x + (pixel_y * width)]; \
1416 bitmap_render_pixel_##type(alpha_op); \
1424 #define render_scanline_vram_setup_mode3() \
1425 u16 *src_ptr = (u16 *)vram \
1427 #define render_scanline_vram_setup_mode5() \
1429 if(io_registers[REG_DISPCNT] & 0x10) \
1430 src_ptr = (u16 *)(vram + 0xA000); \
1432 src_ptr = (u16 *)vram \
1435 #ifdef RENDER_COLOR16_NORMAL
1437 #define render_scanline_vram_setup_mode4() \
1438 const u32 pixel_combine = 0; \
1440 if(io_registers[REG_DISPCNT] & 0x10) \
1441 src_ptr = vram + 0xA000; \
1448 #define render_scanline_vram_setup_mode4() \
1449 u16 *palette = palette_ram_converted; \
1451 if(io_registers[REG_DISPCNT] & 0x10) \
1452 src_ptr = vram + 0xA000; \
1460 // Build bitmap scanline rendering functions.
1462 #define render_scanline_bitmap_builder(type, alpha_op, width, height) \
1463 void render_scanline_bitmap_##type##_##alpha_op(u32 start, u32 end, \
1466 u32 bg_control = io_registers[REG_BG2CNT]; \
1467 u32 current_pixel; \
1468 s32 source_x, source_y; \
1469 u32 vcount = io_registers[REG_VCOUNT]; \
1470 s32 pixel_x, pixel_y; \
1472 s32 dx = (s16)io_registers[REG_BG2PA]; \
1473 s32 dy = (s16)io_registers[REG_BG2PC]; \
1477 render_scanline_dest_##alpha_op *dest_ptr = \
1478 ((render_scanline_dest_##alpha_op *)scanline) + start; \
1479 render_scanline_vram_setup_##type(); \
1483 source_x = affine_reference_x[0] + (start * dx); \
1484 source_y = affine_reference_y[0] + (start * dy); \
1488 bitmap_render_scale(type, alpha_op, width, height); \
1492 bitmap_render_rotate(type, alpha_op, width, height); \
1496 render_scanline_bitmap_builder(mode3, normal, 240, 160);
1497 render_scanline_bitmap_builder(mode4, normal, 240, 160);
1498 render_scanline_bitmap_builder(mode5, normal, 160, 128);
1501 // Fill in the renderers for a layer based on the mode type,
1503 #define tile_layer_render_functions(type) \
1505 render_scanline_##type##_base_normal, \
1506 render_scanline_##type##_transparent_normal, \
1507 render_scanline_##type##_base_alpha, \
1508 render_scanline_##type##_transparent_alpha, \
1509 render_scanline_##type##_base_color16, \
1510 render_scanline_##type##_transparent_color16, \
1511 render_scanline_##type##_base_color32, \
1512 render_scanline_##type##_transparent_color32 \
1516 // Use if a layer is unsupported for that mode.
1518 #define tile_layer_render_null() \
1520 NULL, NULL, NULL, NULL \
1523 #define bitmap_layer_render_functions(type) \
1525 render_scanline_bitmap_##type##_normal \
1528 // Structs containing functions to render the layers for each mode, for
1529 // each render type.
1530 tile_layer_render_struct tile_mode_renderers[3][4] =
1533 tile_layer_render_functions(text), tile_layer_render_functions(text),
1534 tile_layer_render_functions(text), tile_layer_render_functions(text)
1537 tile_layer_render_functions(text), tile_layer_render_functions(text),
1538 tile_layer_render_functions(affine), tile_layer_render_functions(text)
1541 tile_layer_render_functions(text), tile_layer_render_functions(text),
1542 tile_layer_render_functions(affine), tile_layer_render_functions(affine)
1546 bitmap_layer_render_struct bitmap_mode_renderers[3] =
1548 bitmap_layer_render_functions(mode3),
1549 bitmap_layer_render_functions(mode4),
1550 bitmap_layer_render_functions(mode5)
1554 #define render_scanline_layer_functions_tile() \
1555 tile_layer_render_struct *layer_renderers = \
1556 tile_mode_renderers[dispcnt & 0x07] \
1558 #define render_scanline_layer_functions_bitmap() \
1559 bitmap_layer_render_struct *layer_renderers = \
1560 bitmap_mode_renderers + ((dispcnt & 0x07) - 3) \
1563 // Adjust a flipped obj's starting position
1565 #define obj_tile_offset_noflip(color_depth) \
1567 #define obj_tile_offset_flip(color_depth) \
1568 + (tile_size_##color_depth * ((obj_width - 8) / 8)) \
1571 // Adjust the obj's starting point if it goes too far off the left edge of \
1574 #define obj_tile_right_offset_noflip(color_depth) \
1575 tile_ptr += (partial_tile_offset / 8) * tile_size_##color_depth \
1577 #define obj_tile_right_offset_flip(color_depth) \
1578 tile_ptr -= (partial_tile_offset / 8) * tile_size_##color_depth \
1580 // Get the current row offset into an obj in 1D map space
1582 #define obj_tile_offset_1D(color_depth, flip_op) \
1583 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1584 + ((vertical_offset / 8) * (obj_width / 8) * tile_size_##color_depth) \
1585 + ((vertical_offset % 8) * tile_width_##color_depth) \
1586 obj_tile_offset_##flip_op(color_depth) \
1588 // Get the current row offset into an obj in 2D map space
1590 #define obj_tile_offset_2D(color_depth, flip_op) \
1591 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1592 + ((vertical_offset / 8) * 1024) \
1593 + ((vertical_offset % 8) * tile_width_##color_depth) \
1594 obj_tile_offset_##flip_op(color_depth) \
1597 // Get the palette for 4bpp obj.
1599 #define obj_get_palette_4bpp() \
1600 current_palette = (obj_attribute_2 >> 8) & 0xF0 \
1602 #define obj_get_palette_8bpp() \
1605 // Render the current row of an obj.
1607 #define obj_render(combine_op, color_depth, alpha_op, map_space, flip_op) \
1609 obj_get_palette_##color_depth(); \
1610 obj_tile_offset_##map_space(color_depth, flip_op); \
1612 if(obj_x < (s32)start) \
1614 dest_ptr = scanline + start; \
1615 pixel_run = obj_width - (start - obj_x); \
1616 if((s32)pixel_run > 0) \
1618 if((obj_x + obj_width) >= end) \
1620 pixel_run = end - start; \
1621 partial_tile_offset = start - obj_x; \
1622 obj_tile_right_offset_##flip_op(color_depth); \
1623 partial_tile_offset %= 8; \
1625 if(partial_tile_offset) \
1627 partial_tile_run = 8 - partial_tile_offset; \
1628 if((s32)pixel_run < (s32)partial_tile_run) \
1630 if((s32)pixel_run > 0) \
1632 partial_tile_run = pixel_run; \
1633 partial_tile_mid_obj(combine_op, color_depth, alpha_op, \
1640 pixel_run -= partial_tile_run; \
1641 partial_tile_right_obj(combine_op, color_depth, alpha_op, \
1645 tile_run = pixel_run / 8; \
1646 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1647 partial_tile_run = pixel_run % 8; \
1648 if(partial_tile_run) \
1650 partial_tile_left_obj(combine_op, color_depth, alpha_op, \
1656 partial_tile_offset = start - obj_x; \
1657 obj_tile_right_offset_##flip_op(color_depth); \
1658 partial_tile_offset %= 8; \
1659 if(partial_tile_offset) \
1661 partial_tile_run = 8 - partial_tile_offset; \
1662 partial_tile_right_obj(combine_op, color_depth, alpha_op, \
1665 tile_run = pixel_run / 8; \
1666 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1672 if((obj_x + obj_width) >= end) \
1674 pixel_run = end - obj_x; \
1675 if((s32)pixel_run > 0) \
1677 dest_ptr = scanline + obj_x; \
1678 tile_run = pixel_run / 8; \
1679 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1680 partial_tile_run = pixel_run % 8; \
1681 if(partial_tile_run) \
1683 partial_tile_left_obj(combine_op, color_depth, alpha_op, flip_op); \
1689 dest_ptr = scanline + obj_x; \
1690 tile_run = obj_width / 8; \
1691 multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op); \
1695 #define obj_scale_offset_1D(color_depth) \
1696 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1697 + ((vertical_offset / 8) * (max_x / 8) * tile_size_##color_depth) \
1698 + ((vertical_offset % 8) * tile_width_##color_depth) \
1700 // Get the current row offset into an obj in 2D map space
1702 #define obj_scale_offset_2D(color_depth) \
1703 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32) \
1704 + ((vertical_offset / 8) * 1024) \
1705 + ((vertical_offset % 8) * tile_width_##color_depth) \
1707 #define obj_render_scale_pixel_4bpp(combine_op, alpha_op) \
1710 current_pixel = tile_ptr[tile_map_offset + ((tile_x >> 1) & 0x03)] >> 4; \
1715 tile_ptr[tile_map_offset + ((tile_x >> 1) & 0x03)] & 0x0F; \
1718 tile_4bpp_draw_##combine_op(0, none, 0, alpha_op) \
1721 #define obj_render_scale_pixel_8bpp(combine_op, alpha_op) \
1722 current_pixel = tile_ptr[tile_map_offset + (tile_x & 0x07)]; \
1723 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op); \
1725 #define obj_render_scale(combine_op, color_depth, alpha_op, map_space) \
1727 u32 vertical_offset; \
1728 source_y += (y_delta * dmy); \
1729 vertical_offset = (source_y >> 8); \
1730 if((u32)vertical_offset < (u32)max_y) \
1732 obj_scale_offset_##map_space(color_depth); \
1733 source_x += (y_delta * dmx) - (middle_x * dx); \
1735 for(i = 0; i < obj_width; i++) \
1737 tile_x = (source_x >> 8); \
1739 if((u32)tile_x < (u32)max_x) \
1743 advance_dest_ptr_##combine_op(1); \
1746 for(; i < obj_width; i++) \
1748 tile_x = (source_x >> 8); \
1750 if((u32)tile_x >= (u32)max_x) \
1753 tile_map_offset = (tile_x >> 3) * tile_size_##color_depth; \
1754 obj_render_scale_pixel_##color_depth(combine_op, alpha_op); \
1757 advance_dest_ptr_##combine_op(1); \
1763 #define obj_rotate_offset_1D(color_depth) \
1764 obj_tile_pitch = (max_x / 8) * tile_size_##color_depth \
1766 #define obj_rotate_offset_2D(color_depth) \
1767 obj_tile_pitch = 1024 \
1769 #define obj_render_rotate_pixel_4bpp(combine_op, alpha_op) \
1772 current_pixel = tile_ptr[tile_map_offset + \
1773 ((tile_x >> 1) & 0x03) + ((tile_y & 0x07) * obj_pitch)] >> 4; \
1777 current_pixel = tile_ptr[tile_map_offset + \
1778 ((tile_x >> 1) & 0x03) + ((tile_y & 0x07) * obj_pitch)] & 0x0F; \
1781 tile_4bpp_draw_##combine_op(0, none, 0, alpha_op) \
1783 #define obj_render_rotate_pixel_8bpp(combine_op, alpha_op) \
1784 current_pixel = tile_ptr[tile_map_offset + \
1785 (tile_x & 0x07) + ((tile_y & 0x07) * obj_pitch)]; \
1787 tile_8bpp_draw_##combine_op(0, none, 0, alpha_op) \
1789 #define obj_render_rotate(combine_op, color_depth, alpha_op, map_space) \
1791 tile_ptr = tile_base + ((obj_attribute_2 & 0x3FF) * 32); \
1792 obj_rotate_offset_##map_space(color_depth); \
1794 source_x += (y_delta * dmx) - (middle_x * dx); \
1795 source_y += (y_delta * dmy) - (middle_x * dy); \
1797 for(i = 0; i < obj_width; i++) \
1799 tile_x = (source_x >> 8); \
1800 tile_y = (source_y >> 8); \
1802 if(((u32)tile_x < (u32)max_x) && ((u32)tile_y < (u32)max_y)) \
1807 advance_dest_ptr_##combine_op(1); \
1810 for(; i < obj_width; i++) \
1812 tile_x = (source_x >> 8); \
1813 tile_y = (source_y >> 8); \
1815 if(((u32)tile_x >= (u32)max_x) || ((u32)tile_y >= (u32)max_y)) \
1818 tile_map_offset = ((tile_x >> 3) * tile_size_##color_depth) + \
1819 ((tile_y >> 3) * obj_tile_pitch); \
1820 obj_render_rotate_pixel_##color_depth(combine_op, alpha_op); \
1824 advance_dest_ptr_##combine_op(1); \
1828 // Render the current row of an affine transformed OBJ.
1830 #define obj_render_affine(combine_op, color_depth, alpha_op, map_space) \
1832 s16 *params = oam_ram + (((obj_attribute_1 >> 9) & 0x1F) * 16); \
1833 s32 dx = params[3]; \
1834 s32 dmx = params[7]; \
1835 s32 dy = params[11]; \
1836 s32 dmy = params[15]; \
1837 s32 source_x, source_y; \
1838 s32 tile_x, tile_y; \
1840 u32 tile_map_offset; \
1843 s32 max_x = obj_width; \
1844 s32 max_y = obj_height; \
1846 u32 obj_pitch = tile_width_##color_depth; \
1847 u32 obj_tile_pitch; \
1849 middle_x = (obj_width / 2); \
1850 middle_y = (obj_height / 2); \
1852 source_x = (middle_x << 8); \
1853 source_y = (middle_y << 8); \
1856 if(obj_attribute_0 & 0x200) \
1864 if((s32)obj_x < (s32)start) \
1866 u32 x_delta = start - obj_x; \
1867 middle_x -= x_delta; \
1868 obj_width -= x_delta; \
1871 if((s32)obj_width <= 0) \
1875 if((s32)(obj_x + obj_width) >= (s32)end) \
1877 obj_width = end - obj_x; \
1879 if((s32)obj_width <= 0) \
1882 dest_ptr = scanline + obj_x; \
1884 y_delta = vcount - (obj_y + middle_y); \
1886 obj_get_palette_##color_depth(); \
1890 obj_render_scale(combine_op, color_depth, alpha_op, map_space); \
1894 obj_render_rotate(combine_op, color_depth, alpha_op, map_space); \
1898 u32 obj_width_table[] = { 8, 16, 32, 64, 16, 32, 32, 64, 8, 8, 16, 32 };
1899 u32 obj_height_table[] = { 8, 16, 32, 64, 8, 8, 16, 32, 16, 32, 32, 64 };
1901 u8 obj_priority_list[5][160][128];
1902 u32 obj_priority_count[5][160];
1903 u32 obj_alpha_count[160];
1906 // Build obj rendering functions
1908 #ifdef RENDER_COLOR16_NORMAL
1910 #define render_scanline_obj_extra_variables_normal(bg_type) \
1911 const u32 pixel_combine = (1 << 8) \
1915 #define render_scanline_obj_extra_variables_normal(bg_type) \
1916 u16 *palette = palette_ram_converted + 256 \
1921 #define render_scanline_obj_extra_variables_color() \
1923 u32 pixel_combine = color_combine_mask(4) | (1 << 8) \
1925 #define render_scanline_obj_extra_variables_alpha_obj(map_space) \
1926 render_scanline_obj_extra_variables_color(); \
1927 if((pixel_combine & 0x00000200) == 0) \
1929 render_scanline_obj_color32_##map_space(priority, start, end, scanline); \
1933 #define render_scanline_obj_extra_variables_color16(map_space) \
1934 render_scanline_obj_extra_variables_color() \
1936 #define render_scanline_obj_extra_variables_color32(map_space) \
1937 render_scanline_obj_extra_variables_color() \
1939 #define render_scanline_obj_extra_variables_partial_alpha(map_space) \
1940 render_scanline_obj_extra_variables_color(); \
1941 u32 base_pixel_combine = pixel_combine \
1943 #define render_scanline_obj_extra_variables_copy(type) \
1944 u32 bldcnt = io_registers[REG_BLDCNT]; \
1945 u32 dispcnt = io_registers[REG_DISPCNT]; \
1946 u32 obj_enable = io_registers[REG_WINOUT] >> 8; \
1947 render_scanline_layer_functions_##type(); \
1948 u32 copy_start, copy_end; \
1949 u16 copy_buffer[240]; \
1952 #define render_scanline_obj_extra_variables_copy_tile(map_space) \
1953 render_scanline_obj_extra_variables_copy(tile) \
1955 #define render_scanline_obj_extra_variables_copy_bitmap(map_space) \
1956 render_scanline_obj_extra_variables_copy(bitmap) \
1959 #define render_scanline_obj_main(combine_op, alpha_op, map_space) \
1960 if(obj_attribute_0 & 0x100) \
1962 if((obj_attribute_0 >> 13) & 0x01) \
1964 obj_render_affine(combine_op, 8bpp, alpha_op, map_space); \
1968 obj_render_affine(combine_op, 4bpp, alpha_op, map_space); \
1973 vertical_offset = vcount - obj_y; \
1975 if((obj_attribute_1 >> 13) & 0x01) \
1976 vertical_offset = obj_height - vertical_offset - 1; \
1978 switch(((obj_attribute_0 >> 12) & 0x02) | \
1979 ((obj_attribute_1 >> 12) & 0x01)) \
1982 obj_render(combine_op, 4bpp, alpha_op, map_space, noflip); \
1986 obj_render(combine_op, 4bpp, alpha_op, map_space, flip); \
1990 obj_render(combine_op, 8bpp, alpha_op, map_space, noflip); \
1994 obj_render(combine_op, 8bpp, alpha_op, map_space, flip); \
1999 #define render_scanline_obj_no_partial_alpha(combine_op, alpha_op, map_space) \
2000 render_scanline_obj_main(combine_op, alpha_op, map_space) \
2002 #define render_scanline_obj_partial_alpha(combine_op, alpha_op, map_space) \
2003 if((obj_attribute_0 >> 10) & 0x03) \
2005 pixel_combine = 0x00000300; \
2006 render_scanline_obj_main(combine_op, alpha_obj, map_space); \
2010 pixel_combine = base_pixel_combine; \
2011 render_scanline_obj_main(combine_op, color32, map_space); \
2014 #define render_scanline_obj_prologue_transparent(alpha_op) \
2016 #define render_scanline_obj_prologue_copy_body(type) \
2017 copy_start = obj_x; \
2018 if(obj_attribute_0 & 0x200) \
2019 copy_end = obj_x + (obj_width * 2); \
2021 copy_end = obj_x + obj_width; \
2023 if(copy_start < start) \
2024 copy_start = start; \
2025 if(copy_end > end) \
2028 if((copy_start < end) && (copy_end > start)) \
2030 render_scanline_conditional_##type(copy_start, copy_end, copy_buffer, \
2031 obj_enable, dispcnt, bldcnt, layer_renderers); \
2032 copy_ptr = copy_buffer + copy_start; \
2039 #define render_scanline_obj_prologue_copy_tile() \
2040 render_scanline_obj_prologue_copy_body(tile) \
2042 #define render_scanline_obj_prologue_copy_bitmap() \
2043 render_scanline_obj_prologue_copy_body(bitmap) \
2045 #define render_scanline_obj_prologue_copy(alpha_op) \
2046 render_scanline_obj_prologue_##alpha_op() \
2049 #define render_scanline_obj_builder(combine_op, alpha_op, map_space, \
2051 void render_scanline_obj_##alpha_op##_##map_space(u32 priority, \
2052 u32 start, u32 end, render_scanline_dest_##alpha_op *scanline) \
2054 render_scanline_obj_extra_variables_##alpha_op(map_space); \
2058 s32 obj_width, obj_height; \
2059 u32 obj_attribute_0, obj_attribute_1, obj_attribute_2; \
2060 s32 vcount = io_registers[REG_VCOUNT]; \
2062 u32 current_pixels; \
2063 u32 current_pixel; \
2064 u32 current_palette; \
2065 u32 vertical_offset; \
2066 u32 partial_tile_run, partial_tile_offset; \
2069 render_scanline_dest_##alpha_op *dest_ptr; \
2070 u8 *tile_base = vram + 0x10000; \
2072 u32 obj_count = obj_priority_count[priority][vcount]; \
2073 u8 *obj_list = obj_priority_list[priority][vcount]; \
2075 for(obj_num = 0; obj_num < obj_count; obj_num++) \
2077 oam_ptr = oam_ram + (obj_list[obj_num] * 4); \
2078 obj_attribute_0 = oam_ptr[0]; \
2079 obj_attribute_1 = oam_ptr[1]; \
2080 obj_attribute_2 = oam_ptr[2]; \
2081 obj_size = ((obj_attribute_0 >> 12) & 0x0C) | (obj_attribute_1 >> 14); \
2083 obj_x = (s32)(obj_attribute_1 << 23) >> 23; \
2084 obj_width = obj_width_table[obj_size]; \
2086 render_scanline_obj_prologue_##combine_op(alpha_op); \
2088 obj_y = obj_attribute_0 & 0xFF; \
2093 obj_height = obj_height_table[obj_size]; \
2094 render_scanline_obj_##partial_alpha_op(combine_op, alpha_op, map_space); \
2098 render_scanline_obj_builder(transparent, normal, 1D, no_partial_alpha);
2099 render_scanline_obj_builder(transparent, normal, 2D, no_partial_alpha);
2100 render_scanline_obj_builder(transparent, color16, 1D, no_partial_alpha);
2101 render_scanline_obj_builder(transparent, color16, 2D, no_partial_alpha);
2102 render_scanline_obj_builder(transparent, color32, 1D, no_partial_alpha);
2103 render_scanline_obj_builder(transparent, color32, 2D, no_partial_alpha);
2104 render_scanline_obj_builder(transparent, alpha_obj, 1D, no_partial_alpha);
2105 render_scanline_obj_builder(transparent, alpha_obj, 2D, no_partial_alpha);
2106 render_scanline_obj_builder(transparent, partial_alpha, 1D, partial_alpha);
2107 render_scanline_obj_builder(transparent, partial_alpha, 2D, partial_alpha);
2108 render_scanline_obj_builder(copy, copy_tile, 1D, no_partial_alpha);
2109 render_scanline_obj_builder(copy, copy_tile, 2D, no_partial_alpha);
2110 render_scanline_obj_builder(copy, copy_bitmap, 1D, no_partial_alpha);
2111 render_scanline_obj_builder(copy, copy_bitmap, 2D, no_partial_alpha);
2115 void order_obj(u32 video_mode)
2117 s32 obj_num, priority, row;
2119 s32 obj_size, obj_mode;
2120 s32 obj_width, obj_height;
2122 u32 obj_attribute_0, obj_attribute_1, obj_attribute_2;
2123 s32 vcount = io_registers[REG_VCOUNT];
2124 u32 partial_tile_run, partial_tile_offset;
2127 u16 *oam_ptr = oam_ram + 508;
2129 u8 *tile_base = vram + 0x10000;
2132 for(priority = 0; priority < 5; priority++)
2134 for(row = 0; row < 160; row++)
2136 obj_priority_count[priority][row] = 0;
2140 for(row = 0; row < 160; row++)
2142 obj_alpha_count[row] = 0;
2145 for(obj_num = 127; obj_num >= 0; obj_num--, oam_ptr -= 4)
2147 obj_attribute_0 = oam_ptr[0];
2148 obj_attribute_2 = oam_ptr[2];
2149 obj_size = obj_attribute_0 & 0xC000;
2150 obj_priority = (obj_attribute_2 >> 10) & 0x03;
2151 obj_mode = (obj_attribute_0 >> 10) & 0x03;
2153 if(((obj_attribute_0 & 0x0300) != 0x0200) && (obj_size != 0xC000) &&
2154 (obj_mode != 3) && ((video_mode < 3) ||
2155 ((obj_attribute_2 & 0x3FF) >= 512)))
2157 obj_y = obj_attribute_0 & 0xFF;
2161 obj_attribute_1 = oam_ptr[1];
2162 obj_size = ((obj_size >> 12) & 0x0C) | (obj_attribute_1 >> 14);
2163 obj_height = obj_height_table[obj_size];
2164 obj_width = obj_width_table[obj_size];
2166 if(obj_attribute_0 & 0x200)
2172 if(((obj_y + obj_height) > 0) && (obj_y < 160))
2174 obj_x = (s32)(obj_attribute_1 << 23) >> 23;
2176 if(((obj_x + obj_width) > 0) && (obj_x < 240))
2180 obj_height += obj_y;
2184 if((obj_y + obj_height) >= 160)
2186 obj_height = 160 - obj_y;
2191 for(row = obj_y; row < obj_y + obj_height; row++)
2193 current_count = obj_priority_count[obj_priority][row];
2194 obj_priority_list[obj_priority][row][current_count] = obj_num;
2195 obj_priority_count[obj_priority][row] = current_count + 1;
2196 obj_alpha_count[row]++;
2206 for(row = obj_y; row < obj_y + obj_height; row++)
2208 current_count = obj_priority_count[obj_priority][row];
2209 obj_priority_list[obj_priority][row][current_count] = obj_num;
2210 obj_priority_count[obj_priority][row] = current_count + 1;
2219 u32 layer_order[16];
2222 u32 order_layers(u32 layer_flags)
2224 s32 priority, layer_number;
2227 for(priority = 3; priority >= 0; priority--)
2229 for(layer_number = 3; layer_number >= 0; layer_number--)
2231 if(((layer_flags >> layer_number) & 1) &&
2232 ((io_registers[REG_BG0CNT + layer_number] & 0x03) == priority))
2234 layer_order[layer_count] = layer_number;
2239 if((obj_priority_count[priority][io_registers[REG_VCOUNT]] > 0)
2240 && (layer_flags & 0x10))
2242 layer_order[layer_count] = priority | 0x04;
2248 #define fill_line(_start, _end) \
2251 for(i = _start; i < _end; i++) \
2253 dest_ptr[i] = color; \
2257 #define fill_line_color_normal() \
2258 color = palette_ram_converted[color] \
2260 #define fill_line_color_alpha() \
2262 #define fill_line_color_color16() \
2264 #define fill_line_color_color32() \
2266 #define fill_line_builder(type) \
2267 void fill_line_##type(u16 color, render_scanline_dest_##type *dest_ptr, \
2268 u32 start, u32 end) \
2270 fill_line_color_##type(); \
2271 fill_line(start, end); \
2274 fill_line_builder(normal);
2275 fill_line_builder(alpha);
2276 fill_line_builder(color16);
2277 fill_line_builder(color32);
2280 // Alpha blend two pixels (pixel_top and pixel_bottom).
2282 #define blend_pixel() \
2283 pixel_bottom = palette_ram_converted[(pixel_pair >> 16) & 0x1FF]; \
2284 pixel_bottom = (pixel_bottom | (pixel_bottom << 16)) & 0x07E0F81F; \
2285 pixel_top = ((pixel_top * blend_a) + (pixel_bottom * blend_b)) >> 4 \
2288 // Alpha blend two pixels, allowing for saturation (individual channels > 31).
2289 // The operation is optimized towards saturation not occuring.
2291 #define blend_saturate_pixel() \
2292 pixel_bottom = palette_ram_converted[(pixel_pair >> 16) & 0x1FF]; \
2293 pixel_bottom = (pixel_bottom | (pixel_bottom << 16)) & 0x07E0F81F; \
2294 pixel_top = ((pixel_top * blend_a) + (pixel_bottom * blend_b)) >> 4; \
2295 if(pixel_top & 0x08010020) \
2297 if(pixel_top & 0x08000000) \
2298 pixel_top |= 0x07E00000; \
2300 if(pixel_top & 0x00010000) \
2301 pixel_top |= 0x0000F800; \
2303 if(pixel_top & 0x00000020) \
2304 pixel_top |= 0x0000001F; \
2307 #define brighten_pixel() \
2308 pixel_top = upper + ((pixel_top * blend) >> 4); \
2310 #define darken_pixel() \
2311 pixel_top = (pixel_top * blend) >> 4; \
2313 #define effect_condition_alpha \
2314 ((pixel_pair & 0x04000200) == 0x04000200) \
2316 #define effect_condition_fade(pixel_source) \
2317 ((pixel_source & 0x00000200) == 0x00000200) \
2319 #define expand_pixel_no_dest(expand_type, pixel_source) \
2320 pixel_top = (pixel_top | (pixel_top << 16)) & 0x07E0F81F; \
2321 expand_type##_pixel(); \
2322 pixel_top &= 0x07E0F81F; \
2323 pixel_top = (pixel_top >> 16) | pixel_top \
2325 #define expand_pixel(expand_type, pixel_source) \
2326 pixel_top = palette_ram_converted[pixel_source & 0x1FF]; \
2327 expand_pixel_no_dest(expand_type, pixel_source); \
2328 *screen_dest_ptr = pixel_top \
2330 #define expand_loop(expand_type, effect_condition, pixel_source) \
2331 screen_src_ptr += start; \
2332 screen_dest_ptr += start; \
2336 for(i = 0; i < end; i++) \
2338 pixel_source = *screen_src_ptr; \
2339 if(effect_condition) \
2341 expand_pixel(expand_type, pixel_source); \
2345 *screen_dest_ptr = \
2346 palette_ram_converted[pixel_source & 0x1FF]; \
2350 screen_dest_ptr++; \
2354 #define expand_loop_partial_alpha(alpha_expand, expand_type) \
2355 screen_src_ptr += start; \
2356 screen_dest_ptr += start; \
2360 for(i = 0; i < end; i++) \
2362 pixel_pair = *screen_src_ptr; \
2363 if(effect_condition_fade(pixel_pair)) \
2365 if(effect_condition_alpha) \
2367 expand_pixel(alpha_expand, pixel_pair); \
2371 expand_pixel(expand_type, pixel_pair); \
2376 *screen_dest_ptr = \
2377 palette_ram_converted[pixel_pair & 0x1FF]; \
2381 screen_dest_ptr++; \
2385 #define expand_partial_alpha(expand_type) \
2386 if((blend_a + blend_b) > 16) \
2388 expand_loop_partial_alpha(blend_saturate, expand_type); \
2392 expand_loop_partial_alpha(blend, expand_type); \
2397 // Blend top two pixels of scanline with each other.
2399 #ifdef RENDER_COLOR16_NORMAL
2403 void expand_normal(u16 *screen_ptr, u32 start, u32 end)
2405 u32 i, pixel_source;
2406 screen_ptr += start;
2412 for(i = 0; i < end; i++)
2414 pixel_source = *screen_ptr;
2415 *screen_ptr = palette_ram_converted[pixel_source];
2425 #define expand_normal(screen_ptr, start, end)
2432 void expand_blend(u32 *screen_src_ptr, u16 *screen_dest_ptr,
2436 u32 pixel_top, pixel_bottom;
2437 u32 bldalpha = io_registers[REG_BLDALPHA];
2438 u32 blend_a = bldalpha & 0x1F;
2439 u32 blend_b = (bldalpha >> 8) & 0x1F;
2448 // The individual colors can saturate over 31, this should be taken
2449 // care of in an alternate pass as it incurs a huge additional speedhit.
2450 if((blend_a + blend_b) > 16)
2452 expand_loop(blend_saturate, effect_condition_alpha, pixel_pair);
2456 expand_loop(blend, effect_condition_alpha, pixel_pair);
2462 // Blend scanline with white.
2464 void expand_darken(u16 *screen_src_ptr, u16 *screen_dest_ptr,
2468 s32 blend = 16 - (io_registers[REG_BLDY] & 0x1F);
2474 expand_loop(darken, effect_condition_fade(pixel_top), pixel_top);
2478 // Blend scanline with black.
2480 void expand_brighten(u16 *screen_src_ptr, u16 *screen_dest_ptr,
2484 u32 blend = io_registers[REG_BLDY] & 0x1F;
2491 upper = ((0x07E0F81F * blend) >> 4) & 0x07E0F81F;
2494 expand_loop(brighten, effect_condition_fade(pixel_top), pixel_top);
2499 // Expand scanline such that if both top and bottom pass it's alpha,
2500 // if only top passes it's as specified, and if neither pass it's normal.
2502 void expand_darken_partial_alpha(u32 *screen_src_ptr, u16 *screen_dest_ptr,
2505 s32 blend = 16 - (io_registers[REG_BLDY] & 0x1F);
2507 u32 pixel_top, pixel_bottom;
2508 u32 bldalpha = io_registers[REG_BLDALPHA];
2509 u32 blend_a = bldalpha & 0x1F;
2510 u32 blend_b = (bldalpha >> 8) & 0x1F;
2522 expand_partial_alpha(darken);
2526 void expand_brighten_partial_alpha(u32 *screen_src_ptr, u16 *screen_dest_ptr,
2529 s32 blend = io_registers[REG_BLDY] & 0x1F;
2531 u32 pixel_top, pixel_bottom;
2532 u32 bldalpha = io_registers[REG_BLDALPHA];
2533 u32 blend_a = bldalpha & 0x1F;
2534 u32 blend_b = (bldalpha >> 8) & 0x1F;
2541 upper = ((0x07E0F81F * blend) >> 4) & 0x07E0F81F;
2550 expand_partial_alpha(brighten);
2554 // Render an OBJ layer from start to end, depending on the type (1D or 2D)
2555 // stored in dispcnt.
2557 #define render_obj_layer(type, dest, _start, _end) \
2558 current_layer &= ~0x04; \
2559 if(dispcnt & 0x40) \
2560 render_scanline_obj_##type##_1D(current_layer, _start, _end, dest); \
2562 render_scanline_obj_##type##_2D(current_layer, _start, _end, dest) \
2565 // Render a target all the way with the background color as taken from the
2568 #define fill_line_bg(type, dest, _start, _end) \
2569 fill_line_##type(0, dest, _start, _end) \
2572 // Render all layers as they appear in the layer order.
2574 #define render_layers(tile_alpha, obj_alpha, dest) \
2576 current_layer = layer_order[0]; \
2577 if(current_layer & 0x04) \
2579 /* If the first one is OBJ render the background then render it. */ \
2580 fill_line_bg(tile_alpha, dest, 0, 240); \
2581 render_obj_layer(obj_alpha, dest, 0, 240); \
2585 /* Otherwise render a base layer. */ \
2586 layer_renderers[current_layer].tile_alpha##_render_base(current_layer, \
2590 /* Render the rest of the layers. */ \
2591 for(layer_order_pos = 1; layer_order_pos < layer_count; layer_order_pos++) \
2593 current_layer = layer_order[layer_order_pos]; \
2594 if(current_layer & 0x04) \
2596 render_obj_layer(obj_alpha, dest, 0, 240); \
2600 layer_renderers[current_layer]. \
2601 tile_alpha##_render_transparent(current_layer, 0, 240, dest); \
2606 #define render_condition_alpha \
2607 (((io_registers[REG_BLDALPHA] & 0x1F1F) != 0x001F) && \
2608 ((io_registers[REG_BLDCNT] & 0x3F) != 0) && \
2609 ((io_registers[REG_BLDCNT] & 0x3F00) != 0)) \
2611 #define render_condition_fade \
2612 (((io_registers[REG_BLDY] & 0x1F) != 0) && \
2613 ((io_registers[REG_BLDCNT] & 0x3F) != 0)) \
2615 #define render_layers_color_effect(renderer, layer_condition, \
2616 alpha_condition, fade_condition, _start, _end) \
2618 if(layer_condition) \
2620 if(obj_alpha_count[io_registers[REG_VCOUNT]] > 0) \
2622 /* Render based on special effects mode. */ \
2623 u32 screen_buffer[240]; \
2624 switch((bldcnt >> 6) & 0x03) \
2629 if(alpha_condition) \
2631 renderer(alpha, alpha_obj, screen_buffer); \
2632 expand_blend(screen_buffer, scanline, _start, _end); \
2638 /* Fade to white */ \
2641 if(fade_condition) \
2643 renderer(color32, partial_alpha, screen_buffer); \
2644 expand_brighten_partial_alpha(screen_buffer, scanline, \
2651 /* Fade to black */ \
2654 if(fade_condition) \
2656 renderer(color32, partial_alpha, screen_buffer); \
2657 expand_darken_partial_alpha(screen_buffer, scanline, \
2665 renderer(color32, partial_alpha, screen_buffer); \
2666 expand_blend(screen_buffer, scanline, _start, _end); \
2670 /* Render based on special effects mode. */ \
2671 switch((bldcnt >> 6) & 0x03) \
2676 if(alpha_condition) \
2678 u32 screen_buffer[240]; \
2679 renderer(alpha, alpha_obj, screen_buffer); \
2680 expand_blend(screen_buffer, scanline, _start, _end); \
2686 /* Fade to white */ \
2689 if(fade_condition) \
2691 renderer(color16, color16, scanline); \
2692 expand_brighten(scanline, scanline, _start, _end); \
2698 /* Fade to black */ \
2701 if(fade_condition) \
2703 renderer(color16, color16, scanline); \
2704 expand_darken(scanline, scanline, _start, _end); \
2711 renderer(normal, normal, scanline); \
2712 expand_normal(scanline, _start, _end); \
2717 u32 pixel_top = palette_ram_converted[0]; \
2718 switch((bldcnt >> 6) & 0x03) \
2720 /* Fade to white */ \
2723 if(color_combine_mask_a(5)) \
2725 u32 blend = io_registers[REG_BLDY] & 0x1F; \
2731 upper = ((0x07E0F81F * blend) >> 4) & 0x07E0F81F; \
2732 blend = 16 - blend; \
2734 expand_pixel_no_dest(brighten, pixel_top); \
2739 /* Fade to black */ \
2742 if(color_combine_mask_a(5)) \
2744 s32 blend = 16 - (io_registers[REG_BLDY] & 0x1F); \
2749 expand_pixel_no_dest(darken, pixel_top); \
2754 fill_line_color16(pixel_top, scanline, _start, _end); \
2759 // Renders an entire scanline from 0 to 240, based on current color mode.
2761 void render_scanline_tile(u16 *scanline, u32 dispcnt)
2764 u32 layer_order_pos;
2765 u32 bldcnt = io_registers[REG_BLDCNT];
2766 render_scanline_layer_functions_tile();
2768 render_layers_color_effect(render_layers, layer_count,
2769 render_condition_alpha, render_condition_fade, 0, 240);
2772 void render_scanline_bitmap(u16 *scanline, u32 dispcnt)
2774 u32 bldcnt = io_registers[REG_BLDCNT];
2775 render_scanline_layer_functions_bitmap();
2777 u32 layer_order_pos;
2779 fill_line_bg(normal, scanline, 0, 240);
2781 for(layer_order_pos = 0; layer_order_pos < layer_count; layer_order_pos++)
2783 current_layer = layer_order[layer_order_pos];
2784 if(current_layer & 0x04)
2786 render_obj_layer(normal, scanline, 0, 240);
2790 layer_renderers->normal_render(0, 240, scanline);
2795 // Render layers from start to end based on if they're allowed in the
2798 #define render_layers_conditional(tile_alpha, obj_alpha, dest) \
2801 current_layer = layer_order[layer_order_pos]; \
2802 /* If OBJ aren't enabled skip to the first non-OBJ layer */ \
2803 if(!(enable_flags & 0x10)) \
2805 while((current_layer & 0x04) || !((1 << current_layer) & enable_flags)) \
2807 layer_order_pos++; \
2808 current_layer = layer_order[layer_order_pos]; \
2810 /* Oops, ran out of layers, render the background. */ \
2811 if(layer_order_pos == layer_count) \
2813 fill_line_bg(tile_alpha, dest, start, end); \
2818 /* Render the first valid layer */ \
2819 layer_renderers[current_layer].tile_alpha##_render_base(current_layer, \
2820 start, end, dest); \
2822 layer_order_pos++; \
2824 /* Render the rest of the layers if active, skipping OBJ ones. */ \
2825 for(; layer_order_pos < layer_count; layer_order_pos++) \
2827 current_layer = layer_order[layer_order_pos]; \
2828 if(!(current_layer & 0x04) && ((1 << current_layer) & enable_flags)) \
2830 layer_renderers[current_layer]. \
2831 tile_alpha##_render_transparent(current_layer, start, end, dest); \
2837 /* Find the first active layer, skip all of the inactive ones */ \
2838 while(!((current_layer & 0x04) || ((1 << current_layer) & enable_flags))) \
2840 layer_order_pos++; \
2841 current_layer = layer_order[layer_order_pos]; \
2843 /* Oops, ran out of layers, render the background. */ \
2844 if(layer_order_pos == layer_count) \
2846 fill_line_bg(tile_alpha, dest, start, end); \
2851 if(current_layer & 0x04) \
2853 /* If the first one is OBJ render the background then render it. */ \
2854 fill_line_bg(tile_alpha, dest, start, end); \
2855 render_obj_layer(obj_alpha, dest, start, end); \
2859 /* Otherwise render a base layer. */ \
2860 layer_renderers[current_layer]. \
2861 tile_alpha##_render_base(current_layer, start, end, dest); \
2864 layer_order_pos++; \
2866 /* Render the rest of the layers. */ \
2867 for(; layer_order_pos < layer_count; layer_order_pos++) \
2869 current_layer = layer_order[layer_order_pos]; \
2870 if(current_layer & 0x04) \
2872 render_obj_layer(obj_alpha, dest, start, end); \
2876 if(enable_flags & (1 << current_layer)) \
2878 layer_renderers[current_layer]. \
2879 tile_alpha##_render_transparent(current_layer, start, end, dest); \
2890 // Render all of the BG and OBJ in a tiled scanline from start to end ONLY if
2891 // enable_flag allows that layer/OBJ. Also conditionally render color effects.
2893 void render_scanline_conditional_tile(u32 start, u32 end, u16 *scanline,
2894 u32 enable_flags, u32 dispcnt, u32 bldcnt, tile_layer_render_struct
2898 u32 layer_order_pos = 0;
2900 render_layers_color_effect(render_layers_conditional,
2901 (layer_count && (enable_flags & 0x1F)),
2902 ((enable_flags & 0x20) && render_condition_alpha),
2903 ((enable_flags & 0x20) && render_condition_fade), start, end);
2907 // Render the BG and OBJ in a bitmap scanline from start to end ONLY if
2908 // enable_flag allows that layer/OBJ. Also conditionally render color effects.
2910 void render_scanline_conditional_bitmap(u32 start, u32 end, u16 *scanline,
2911 u32 enable_flags, u32 dispcnt, u32 bldcnt, bitmap_layer_render_struct
2915 u32 layer_order_pos;
2917 fill_line_bg(normal, scanline, start, end);
2919 for(layer_order_pos = 0; layer_order_pos < layer_count; layer_order_pos++)
2921 current_layer = layer_order[layer_order_pos];
2922 if(current_layer & 0x04)
2924 if(enable_flags & 0x10)
2926 render_obj_layer(normal, scanline, start, end);
2931 if(enable_flags & 0x04)
2932 layer_renderers->normal_render(start, end, scanline);
2938 #define window_x_coords(window_number) \
2939 window_##window_number##_x1 = \
2940 io_registers[REG_WIN##window_number##H] >> 8; \
2941 window_##window_number##_x2 = \
2942 io_registers[REG_WIN##window_number##H] & 0xFF; \
2943 window_##window_number##_enable = \
2944 (winin >> (window_number * 8)) & 0x3F; \
2946 if(window_##window_number##_x1 > 240) \
2947 window_##window_number##_x1 = 240; \
2949 if(window_##window_number##_x2 > 240) \
2950 window_##window_number##_x2 = 240 \
2952 #define window_coords(window_number) \
2953 u32 window_##window_number##_x1, window_##window_number##_x2; \
2954 u32 window_##window_number##_y1, window_##window_number##_y2; \
2955 u32 window_##window_number##_enable; \
2956 window_##window_number##_y1 = \
2957 io_registers[REG_WIN##window_number##V] >> 8; \
2958 window_##window_number##_y2 = \
2959 io_registers[REG_WIN##window_number##V] & 0xFF; \
2961 if(window_##window_number##_y1 > window_##window_number##_y2) \
2963 if((((vcount <= window_##window_number##_y2) || \
2964 (vcount > window_##window_number##_y1)) || \
2965 (window_##window_number##_y2 > 227)) && \
2966 (window_##window_number##_y1 <= 227)) \
2968 window_x_coords(window_number); \
2972 window_##window_number##_x1 = 240; \
2973 window_##window_number##_x2 = 240; \
2978 if((((vcount >= window_##window_number##_y1) && \
2979 (vcount < window_##window_number##_y2)) || \
2980 (window_##window_number##_y2 > 227)) && \
2981 (window_##window_number##_y1 <= 227)) \
2983 window_x_coords(window_number); \
2987 window_##window_number##_x1 = 240; \
2988 window_##window_number##_x2 = 240; \
2992 #define render_window_segment(type, start, end, window_type) \
2995 render_scanline_conditional_##type(start, end, scanline, \
2996 window_##window_type##_enable, dispcnt, bldcnt, layer_renderers); \
2999 #define render_window_segment_unequal(type, start, end, window_type) \
3000 render_scanline_conditional_##type(start, end, scanline, \
3001 window_##window_type##_enable, dispcnt, bldcnt, layer_renderers) \
3003 #define render_window_segment_clip(type, clip_start, clip_end, start, end, \
3008 if(start < clip_start) \
3010 if(end > clip_start) \
3012 if(end > clip_end) \
3014 render_window_segment_unequal(type, clip_start, clip_end, \
3019 render_window_segment_unequal(type, clip_start, end, window_type); \
3025 if(end > clip_end) \
3027 if(start < clip_end) \
3028 render_window_segment_unequal(type, start, clip_end, window_type); \
3032 render_window_segment_unequal(type, start, end, window_type); \
3037 #define render_window_clip_1(type, start, end) \
3038 if(window_1_x1 != 240) \
3040 if(window_1_x1 > window_1_x2) \
3042 render_window_segment_clip(type, start, end, 0, window_1_x2, 1); \
3043 render_window_segment_clip(type, start, end, window_1_x2, window_1_x1, \
3045 render_window_segment_clip(type, start, end, window_1_x1, 240, 1); \
3049 render_window_segment_clip(type, start, end, 0, window_1_x1, out); \
3050 render_window_segment_clip(type, start, end, window_1_x1, window_1_x2, \
3052 render_window_segment_clip(type, start, end, window_1_x2, 240, out); \
3057 render_window_segment(type, start, end, out); \
3060 #define render_window_clip_obj(type, start, end); \
3061 render_window_segment(type, start, end, out); \
3062 if(dispcnt & 0x40) \
3063 render_scanline_obj_copy_##type##_1D(4, start, end, scanline); \
3065 render_scanline_obj_copy_##type##_2D(4, start, end, scanline) \
3068 #define render_window_segment_clip_obj(type, clip_start, clip_end, start, \
3073 if(start < clip_start) \
3075 if(end > clip_start) \
3077 if(end > clip_end) \
3079 render_window_clip_obj(type, clip_start, clip_end); \
3083 render_window_clip_obj(type, clip_start, end); \
3089 if(end > clip_end) \
3091 if(start < clip_end) \
3093 render_window_clip_obj(type, start, clip_end); \
3098 render_window_clip_obj(type, start, end); \
3104 #define render_window_clip_1_obj(type, start, end) \
3105 if(window_1_x1 != 240) \
3107 if(window_1_x1 > window_1_x2) \
3109 render_window_segment_clip(type, start, end, 0, window_1_x2, 1); \
3110 render_window_segment_clip_obj(type, start, end, window_1_x2, \
3112 render_window_segment_clip(type, start, end, window_1_x1, 240, 1); \
3116 render_window_segment_clip_obj(type, start, end, 0, window_1_x1); \
3117 render_window_segment_clip(type, start, end, window_1_x1, window_1_x2, \
3119 render_window_segment_clip_obj(type, start, end, window_1_x2, 240); \
3124 render_window_clip_obj(type, start, end); \
3129 #define render_window_single(type, window_number) \
3130 u32 winin = io_registers[REG_WININ]; \
3131 window_coords(window_number); \
3132 if(window_##window_number##_x1 > window_##window_number##_x2) \
3134 render_window_segment(type, 0, window_##window_number##_x2, \
3136 render_window_segment(type, window_##window_number##_x2, \
3137 window_##window_number##_x1, out); \
3138 render_window_segment(type, window_##window_number##_x1, 240, \
3143 render_window_segment(type, 0, window_##window_number##_x1, out); \
3144 render_window_segment(type, window_##window_number##_x1, \
3145 window_##window_number##_x2, window_number); \
3146 render_window_segment(type, window_##window_number##_x2, 240, out); \
3149 #define render_window_multi(type, front, back) \
3150 if(window_##front##_x1 > window_##front##_x2) \
3152 render_window_segment(type, 0, window_##front##_x2, front); \
3153 render_window_clip_##back(type, window_##front##_x2, \
3154 window_##front##_x1); \
3155 render_window_segment(type, window_##front##_x1, 240, front); \
3159 render_window_clip_##back(type, 0, window_##front##_x1); \
3160 render_window_segment(type, window_##front##_x1, window_##front##_x2, \
3162 render_window_clip_##back(type, window_##front##_x2, 240); \
3165 #define render_scanline_window_builder(type) \
3166 void render_scanline_window_##type(u16 *scanline, u32 dispcnt) \
3168 u32 vcount = io_registers[REG_VCOUNT]; \
3169 u32 winout = io_registers[REG_WINOUT]; \
3170 u32 bldcnt = io_registers[REG_BLDCNT]; \
3171 u32 window_out_enable = winout & 0x3F; \
3173 render_scanline_layer_functions_##type(); \
3175 switch(dispcnt >> 13) \
3177 /* Just window 0 */ \
3180 render_window_single(type, 0); \
3184 /* Just window 1 */ \
3187 render_window_single(type, 1); \
3191 /* Windows 1 and 2 */ \
3194 u32 winin = io_registers[REG_WININ]; \
3197 render_window_multi(type, 0, 1); \
3201 /* Just OBJ windows */ \
3204 u32 window_obj_enable = winout >> 8; \
3205 render_window_clip_obj(type, 0, 240); \
3209 /* Window 0 and OBJ window */ \
3212 u32 window_obj_enable = winout >> 8; \
3213 u32 winin = io_registers[REG_WININ]; \
3215 render_window_multi(type, 0, obj); \
3219 /* Window 1 and OBJ window */ \
3222 u32 window_obj_enable = winout >> 8; \
3223 u32 winin = io_registers[REG_WININ]; \
3225 render_window_multi(type, 1, obj); \
3229 /* Window 0, 1, and OBJ window */ \
3232 u32 window_obj_enable = winout >> 8; \
3233 u32 winin = io_registers[REG_WININ]; \
3236 render_window_multi(type, 0, 1_obj); \
3242 render_scanline_window_builder(tile);
3243 render_scanline_window_builder(bitmap);
3245 u32 active_layers[6] = { 0x1F, 0x17, 0x1C, 0x14, 0x14, 0x14 };
3247 u32 small_resolution_width = 240;
3248 u32 small_resolution_height = 160;
3249 u32 resolution_width, resolution_height;
3251 void update_scanline()
3253 u32 pitch = get_screen_pitch();
3254 u32 dispcnt = io_registers[REG_DISPCNT];
3255 u32 display_flags = (dispcnt >> 8) & 0x1F;
3256 u32 vcount = io_registers[REG_VCOUNT];
3257 u16 *screen_offset = get_screen_pixels() + (vcount * pitch);
3258 u32 video_mode = dispcnt & 0x07;
3261 // If OAM has been modified since the last scanline has been updated then
3262 // reorder and reprofile the OBJ lists.
3265 order_obj(video_mode);
3269 order_layers((dispcnt >> 8) & active_layers[video_mode]);
3274 // If the screen is in in forced blank draw pure white.
3277 fill_line_color16(0xFFFF, screen_offset, 0, 240);
3285 render_scanline_window_tile(screen_offset, dispcnt);
3289 render_scanline_tile(screen_offset, dispcnt);
3295 render_scanline_window_bitmap(screen_offset, dispcnt);
3297 render_scanline_bitmap(screen_offset, dispcnt);
3301 affine_reference_x[0] += (s16)io_registers[REG_BG2PB];
3302 affine_reference_y[0] += (s16)io_registers[REG_BG2PD];
3303 affine_reference_x[1] += (s16)io_registers[REG_BG3PB];
3304 affine_reference_y[1] += (s16)io_registers[REG_BG3PD];
3309 u32 screen_flip = 0;
3313 if(video_direct == 0)
3315 u32 *old_ge_cmd_ptr = ge_cmd_ptr;
3316 sceKernelDcacheWritebackAll();
3318 // Render the current screen
3319 ge_cmd_ptr = ge_cmd + 2;
3320 GE_CMD(TBP0, ((u32)screen_pixels & 0x00FFFFFF));
3321 GE_CMD(TBW0, (((u32)screen_pixels & 0xFF000000) >> 8) |
3323 ge_cmd_ptr = old_ge_cmd_ptr;
3325 sceGeListEnQueue(ge_cmd, ge_cmd_ptr, gecbid, NULL);
3327 // Flip to the next screen
3331 screen_pixels = screen_texture + (240 * 160 * 2);
3333 screen_pixels = screen_texture;
3337 #elif defined(WIZ_BUILD)
3341 if((screen_scale == scaled_aspect) &&
3342 (resolution_width == small_resolution_width) &&
3343 (resolution_height == small_resolution_height))
3345 upscale_aspect(gpsp_gp2x_screen, screen_pixels);
3347 pollux_video_flip();
3348 screen_pixels = (u16 *)gpsp_gp2x_screen + screen_offset;
3353 #define integer_scale_copy_2() \
3354 current_scanline_ptr[x2] = current_pixel; \
3355 current_scanline_ptr[x2 - 1] = current_pixel; \
3358 #define integer_scale_copy_3() \
3359 current_scanline_ptr[x2] = current_pixel; \
3360 current_scanline_ptr[x2 - 1] = current_pixel; \
3361 current_scanline_ptr[x2 - 2] = current_pixel; \
3364 #define integer_scale_copy_4() \
3365 current_scanline_ptr[x2] = current_pixel; \
3366 current_scanline_ptr[x2 - 1] = current_pixel; \
3367 current_scanline_ptr[x2 - 2] = current_pixel; \
3368 current_scanline_ptr[x2 - 3] = current_pixel; \
3371 #define integer_scale_horizontal(scale_factor) \
3372 for(y = 0; y < 160; y++) \
3374 for(x = 239, x2 = (240 * video_scale) - 1; x >= 0; x--) \
3376 current_pixel = current_scanline_ptr[x]; \
3377 integer_scale_copy_##scale_factor(); \
3378 current_scanline_ptr[x2] = current_scanline_ptr[x]; \
3379 current_scanline_ptr[x2 - 1] = current_scanline_ptr[x]; \
3380 current_scanline_ptr[x2 - 2] = current_scanline_ptr[x]; \
3382 current_scanline_ptr += pitch; \
3387 if((video_scale != 1) && (current_scale != unscaled))
3391 u16 *screen_ptr = get_screen_pixels();
3392 u16 *current_scanline_ptr = screen_ptr;
3393 u32 pitch = get_screen_pitch();
3400 integer_scale_horizontal(2);
3404 integer_scale_horizontal(3);
3409 integer_scale_horizontal(4);
3414 for(y = 159, y2 = (160 * video_scale) - 1; y >= 0; y--)
3416 for(i = 0; i < video_scale; i++)
3418 memcpy(screen_ptr + (y2 * pitch),
3419 screen_ptr + (y * pitch), 480 * video_scale);
3426 if((screen_scale == unscaled) &&
3427 (resolution_width == small_resolution_width) &&
3428 (resolution_height == small_resolution_height))
3430 SDL_Rect srect = {0, 0, 240, 160};
3431 SDL_Rect drect = {40, 40, 240, 160};
3432 SDL_BlitSurface(screen, &srect, hw_screen, &drect);
3434 else if((screen_scale == scaled_aspect) &&
3435 (resolution_width == small_resolution_width) &&
3436 (resolution_height == small_resolution_height))
3438 SDL_Rect drect = {0, 10, 0, 0};
3439 SDL_BlitSurface(screen, NULL, hw_screen, &drect);
3443 SDL_BlitSurface(screen, NULL, hw_screen, NULL);
3445 warm_cache_op_all(WOP_D_CLEAN);
3454 u32 frame_to_render;
3456 void update_screen()
3458 if(!skip_next_frame)
3466 sceDisplaySetMode(0, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
3468 sceDisplayWaitVblankStart();
3469 sceDisplaySetFrameBuf((void*)psp_gu_vram_base, PSP_LINE_SIZE,
3470 PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
3474 sceGuStart(GU_DIRECT, display_list);
3475 sceGuDrawBuffer(GU_PSM_5650, (void*)0, PSP_LINE_SIZE);
3476 sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3477 (void*)0, PSP_LINE_SIZE);
3478 sceGuClear(GU_COLOR_BUFFER_BIT);
3480 sceGuOffset(2048 - (PSP_SCREEN_WIDTH / 2), 2048 - (PSP_SCREEN_HEIGHT / 2));
3481 sceGuViewport(2048, 2048, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
3483 sceGuScissor(0, 0, PSP_SCREEN_WIDTH + 1, PSP_SCREEN_HEIGHT + 1);
3484 sceGuEnable(GU_SCISSOR_TEST);
3485 sceGuTexMode(GU_PSM_5650, 0, 0, GU_FALSE);
3486 sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
3487 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
3488 sceGuEnable(GU_TEXTURE_2D);
3490 sceGuFrontFace(GU_CW);
3491 sceGuDisable(GU_BLEND);
3496 sceDisplayWaitVblankStart();
3497 sceGuDisplay(GU_TRUE);
3499 PspGeCallbackData gecb;
3500 gecb.signal_func = NULL;
3501 gecb.signal_arg = NULL;
3502 gecb.finish_func = Ge_Finish_Callback;
3503 gecb.finish_arg = NULL;
3504 gecbid = sceGeSetCallback(&gecb);
3506 screen_vertex[0] = 0 + 0.5;
3507 screen_vertex[1] = 0 + 0.5;
3508 screen_vertex[2] = 0 + 0.5;
3509 screen_vertex[3] = 0 + 0.5;
3510 screen_vertex[4] = 0;
3511 screen_vertex[5] = GBA_SCREEN_WIDTH - 0.5;
3512 screen_vertex[6] = GBA_SCREEN_HEIGHT - 0.5;
3513 screen_vertex[7] = PSP_SCREEN_WIDTH - 0.5;
3514 screen_vertex[8] = PSP_SCREEN_HEIGHT - 0.5;
3515 screen_vertex[9] = 0;
3517 // Set framebuffer to PSP VRAM
3518 GE_CMD(FBP, ((u32)psp_gu_vram_base & 0x00FFFFFF));
3519 GE_CMD(FBW, (((u32)psp_gu_vram_base & 0xFF000000) >> 8) | PSP_LINE_SIZE);
3520 // Set texture 0 to the screen texture
3521 GE_CMD(TBP0, ((u32)screen_texture & 0x00FFFFFF));
3522 GE_CMD(TBW0, (((u32)screen_texture & 0xFF000000) >> 8) | GBA_SCREEN_WIDTH);
3523 // Set the texture size to 256 by 256 (2^8 by 2^8)
3524 GE_CMD(TSIZE0, (8 << 8) | 8);
3525 // Flush the texture cache
3527 // Use 2D coordinates, no indeces, no weights, 32bit float positions,
3528 // 32bit float texture coordinates
3529 GE_CMD(VTYPE, (1 << 23) | (0 << 11) | (0 << 9) |
3530 (3 << 7) | (0 << 5) | (0 << 2) | 3);
3531 // Set the base of the index list pointer to 0
3533 // Set the rest of index list pointer to 0 (not being used)
3535 // Set the base of the screen vertex list pointer
3536 GE_CMD(BASE, ((u32)screen_vertex & 0xFF000000) >> 8);
3537 // Set the rest of the screen vertex list pointer
3538 GE_CMD(VADDR, ((u32)screen_vertex & 0x00FFFFFF));
3539 // Primitive kick: render sprite (primitive 6), 2 vertices
3540 GE_CMD(PRIM, (6 << 16) | 2);
3541 // Done with commands
3543 // Raise signal interrupt
3549 #elif defined(WIZ_BUILD)
3559 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE);
3562 SDL_GP2X_AllowGfxMemory(NULL, 0);
3564 hw_screen = SDL_SetVideoMode(320 * video_scale, 240 * video_scale,
3567 screen = SDL_CreateRGBSurface(SDL_HWSURFACE, 240 * video_scale,
3568 160 * video_scale, 16, 0xFFFF, 0xFFFF, 0xFFFF, 0);
3570 warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
3572 screen = SDL_SetVideoMode(240 * video_scale, 160 * video_scale, 16, 0);
3579 video_scale_type screen_scale = scaled_aspect;
3580 video_scale_type current_scale = scaled_aspect;
3581 video_filter_type screen_filter = filter_bilinear;
3586 void video_resolution_large()
3588 if(video_direct != 1)
3591 screen_pixels = psp_gu_vram_base;
3593 sceGuStart(GU_DIRECT, display_list);
3594 sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3595 (void*)0, PSP_LINE_SIZE);
3600 void set_gba_resolution(video_scale_type scale)
3602 u32 filter_linear = 0;
3603 screen_scale = scale;
3607 screen_vertex[2] = 120 + 0.5;
3608 screen_vertex[3] = 56 + 0.5;
3609 screen_vertex[7] = GBA_SCREEN_WIDTH + 120 - 0.5;
3610 screen_vertex[8] = GBA_SCREEN_HEIGHT + 56 - 0.5;
3614 screen_vertex[2] = 36 + 0.5;
3615 screen_vertex[3] = 0 + 0.5;
3616 screen_vertex[7] = 408 + 36 - 0.5;
3617 screen_vertex[8] = PSP_SCREEN_HEIGHT - 0.5;
3621 screen_vertex[2] = 0;
3622 screen_vertex[3] = 0;
3623 screen_vertex[7] = PSP_SCREEN_WIDTH;
3624 screen_vertex[8] = PSP_SCREEN_HEIGHT;
3628 sceGuStart(GU_DIRECT, display_list);
3629 if(screen_filter == filter_bilinear)
3630 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
3632 sceGuTexFilter(GU_NEAREST, GU_NEAREST);
3637 clear_screen(0x0000);
3640 void video_resolution_small()
3642 if(video_direct != 0)
3644 set_gba_resolution(screen_scale);
3646 screen_pixels = screen_texture;
3649 sceGuStart(GU_DIRECT, display_list);
3650 sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3651 (void*)0, PSP_LINE_SIZE);
3656 void clear_screen(u16 color)
3659 u16 *src_ptr = get_screen_pixels();
3663 for(i = 0; i < (512 * 272); i++, src_ptr++)
3668 // I don't know why this doesn't work.
3669 /* color = (((color & 0x1F) * 255 / 31) << 0) |
3670 ((((color >> 5) & 0x3F) * 255 / 63) << 8) |
3671 ((((color >> 11) & 0x1F) * 255 / 31) << 16) | (0xFF << 24);
3673 sceGuStart(GU_DIRECT, display_list);
3674 sceGuDrawBuffer(GU_PSM_5650, (void*)0, PSP_LINE_SIZE);
3675 //sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT,
3676 // (void*)0, PSP_LINE_SIZE);
3677 sceGuClearColor(color);
3678 sceGuClear(GU_COLOR_BUFFER_BIT);
3683 #elif defined(WIZ_BUILD)
3685 void video_resolution_large()
3688 resolution_width = 320;
3689 resolution_height = 240;
3696 void video_resolution_small()
3698 if(screen_scale == scaled_aspect)
3699 screen_offset = 320*(80 - 14) + 80;
3701 screen_offset = 320*40 + 40;
3702 resolution_width = 240;
3703 resolution_height = 160;
3710 void set_gba_resolution(video_scale_type scale)
3712 screen_scale = scale;
3715 void clear_screen(u16 color)
3717 u32 col = ((u32)color << 16) | color;
3718 u32 *p = gpsp_gp2x_screen;
3726 void video_resolution_large()
3728 current_scale = unscaled;
3731 SDL_FreeSurface(screen);
3732 SDL_GP2X_AllowGfxMemory(NULL, 0);
3733 hw_screen = SDL_SetVideoMode(320, 240, 16, SDL_HWSURFACE);
3734 screen = SDL_CreateRGBSurface(SDL_HWSURFACE, 320, 240, 16, 0xFFFF,
3736 resolution_width = 320;
3737 resolution_height = 240;
3740 warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
3742 screen = SDL_SetVideoMode(480, 272, 16, 0);
3743 resolution_width = 480;
3744 resolution_height = 272;
3748 void video_resolution_small()
3750 current_scale = screen_scale;
3754 SDL_FreeSurface(screen);
3755 SDL_GP2X_AllowGfxMemory(NULL, 0);
3758 if (screen_scale != unscaled)
3760 w = small_resolution_width * video_scale;
3761 h = small_resolution_height * video_scale;
3763 if (screen_scale == scaled_aspect) h += 20;
3764 hw_screen = SDL_SetVideoMode(w, h, 16, SDL_HWSURFACE);
3766 screen = SDL_CreateRGBSurface(SDL_HWSURFACE,
3767 small_resolution_width * video_scale, small_resolution_height *
3768 video_scale, 16, 0xFFFF, 0xFFFF, 0xFFFF, 0);
3772 warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
3774 screen = SDL_SetVideoMode(small_resolution_width * video_scale,
3775 small_resolution_height * video_scale, 16, 0);
3777 resolution_width = small_resolution_width;
3778 resolution_height = small_resolution_height;
3781 void set_gba_resolution(video_scale_type scale)
3783 if(screen_scale != scale)
3785 screen_scale = scale;
3791 small_resolution_width = 240 * video_scale;
3792 small_resolution_height = 160 * video_scale;
3798 void clear_screen(u16 color)
3800 u16 *dest_ptr = get_screen_pixels();
3801 u32 line_skip = get_screen_pitch() - screen->w;
3804 for(y = 0; y < screen->h; y++)
3806 for(x = 0; x < screen->w; x++, dest_ptr++)
3810 dest_ptr += line_skip;
3818 u16 *copy = malloc(240 * 160 * 2);
3819 memcpy(copy, get_screen_pixels(), 240 * 160 * 2);
3823 void blit_to_screen(u16 *src, u32 w, u32 h, u32 dest_x, u32 dest_y)
3825 u32 pitch = get_screen_pitch();
3826 u16 *dest_ptr = get_screen_pixels() + dest_x + (dest_y * pitch);
3829 u32 line_skip = pitch - w;
3832 for(y = 0; y < h; y++)
3834 for(x = 0; x < w; x++, src_ptr++, dest_ptr++)
3836 *dest_ptr = *src_ptr;
3838 dest_ptr += line_skip;
3842 void print_string_ext(const char *str, u16 fg_color, u16 bg_color,
3843 u32 x, u32 y, void *_dest_ptr, u32 pitch, u32 pad)
3845 u16 *dest_ptr = (u16 *)_dest_ptr + (y * pitch) + x;
3846 u8 current_char = str[0];
3855 if(y + FONT_HEIGHT > resolution_height)
3860 if(current_char == '\n')
3864 dest_ptr = get_screen_pixels() + (y * pitch) + x;
3868 glyph_offset = _font_offset[current_char];
3869 current_x += FONT_WIDTH;
3870 for(i2 = 0; i2 < FONT_HEIGHT; i2++, glyph_offset++)
3872 current_row = _font_bits[glyph_offset];
3873 for(i3 = 0; i3 < FONT_WIDTH; i3++)
3875 if((current_row >> (15 - i3)) & 0x01)
3876 *dest_ptr = fg_color;
3878 *dest_ptr = bg_color;
3881 dest_ptr += (pitch - FONT_WIDTH);
3883 dest_ptr = dest_ptr - (pitch * FONT_HEIGHT) + FONT_WIDTH;
3888 current_char = str[str_index];
3890 if((i < pad) && (current_char == 0))
3899 if(current_x + FONT_WIDTH > resolution_width /* EDIT */)
3901 while (current_char && current_char != '\n')
3903 current_char = str[str_index++];
3909 void print_string(const char *str, u16 fg_color, u16 bg_color,
3912 print_string_ext(str, fg_color, bg_color, x, y, get_screen_pixels(),
3913 get_screen_pitch(), 0);
3916 void print_string_pad(const char *str, u16 fg_color, u16 bg_color,
3917 u32 x, u32 y, u32 pad)
3919 print_string_ext(str, fg_color, bg_color, x, y, get_screen_pixels(),
3920 get_screen_pitch(), pad);
3923 u32 debug_cursor_x = 0;
3924 u32 debug_cursor_y = 0;
3928 void debug_screen_clear()
3932 void debug_screen_start()
3936 void debug_screen_end()
3940 void debug_screen_update()
3944 void debug_screen_printf(const char *format, ...)
3948 va_start(ap, format);
3949 vprintf(format, ap);
3953 void debug_screen_newline(u32 count)
3961 void debug_screen_clear()
3965 clear_screen(0x0000);
3968 void debug_screen_start()
3970 video_resolution_large();
3971 debug_screen_clear();
3974 void debug_screen_end()
3976 video_resolution_small();
3979 void debug_screen_update()
3984 void debug_screen_printf(const char *format, ...)
3986 char str_buffer[512];
3987 u32 str_buffer_length;
3990 va_start(ap, format);
3991 str_buffer_length = vsnprintf(str_buffer, 512, format, ap);
3994 printf("printing debug string %s at %d %d\n", str_buffer,
3995 debug_cursor_x, debug_cursor_y);
3997 print_string(str_buffer, 0xFFFF, 0x0000, debug_cursor_x, debug_cursor_y);
3998 debug_cursor_x += FONT_WIDTH * str_buffer_length;
4001 void debug_screen_newline(u32 count)
4004 debug_cursor_y += FONT_HEIGHT * count;
4009 void debug_screen_printl(const char *format, ...)
4013 va_start(ap, format);
4014 debug_screen_printf(format, ap);
4015 debug_screen_newline(1);
4016 // debug_screen_printf("\n");
4021 #define video_savestate_builder(type) \
4022 void video_##type##_savestate(file_tag_type savestate_file) \
4024 file_##type##_array(savestate_file, affine_reference_x); \
4025 file_##type##_array(savestate_file, affine_reference_y); \
4028 video_savestate_builder(read);
4029 video_savestate_builder(write_mem);