forked from FunKey-Project/gpsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.c
4647 lines (3911 loc) · 226 KB
/
video.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* gameplaySP
*
* Copyright (C) 2006 Exophase <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <SDL/SDL_ttf.h>
#include "common.h"
#define WANT_FONT_BITS
#include "font.h"
#include "menu.h"
#include "configfile_fk.h"
#ifdef CHIP_BUILD
#include "arm/neon_scaler.h"
#endif
#define AVERAGE(z, x) ((((z) & 0xF7DEF7DE) >> 1) + (((x) & 0xF7DEF7DE) >> 1))
#define AVERAGEHI(AB) ((((AB) & 0xF7DE0000) >> 1) + (((AB) & 0xF7DE) << 15))
#define AVERAGELO(CD) ((((CD) & 0xF7DE) >> 1) + (((CD) & 0xF7DE0000) >> 17))
// Support math
#define Half(A) (((A) >> 1) & 0x7BEF)
#define Quarter(A) (((A) >> 2) & 0x39E7)
// Error correction expressions to piece back the lower bits together
#define RestHalf(A) ((A) & 0x0821)
#define RestQuarter(A) ((A) & 0x1863)
// Error correction expressions for quarters of pixels
#define Corr1_3(A, B) Quarter(RestQuarter(A) + (RestHalf(B) << 1) + RestQuarter(B))
#define Corr3_1(A, B) Quarter((RestHalf(A) << 1) + RestQuarter(A) + RestQuarter(B))
// Error correction expressions for halves
#define Corr1_1(A, B) ((A) & (B) & 0x0821)
// Quarters
#define Weight1_3(A, B) (Quarter(A) + Half(B) + Quarter(B) + Corr1_3(A, B))
#define Weight3_1(A, B) (Half(A) + Quarter(A) + Quarter(B) + Corr3_1(A, B))
// Halves
#define Weight1_1(A, B) (Half(A) + Half(B) + Corr1_1(A, B))
#if 0
#ifdef PSP_BUILD
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspgu.h>
#include <psppower.h>
#include <psprtc.h>
static float *screen_vertex = (float *)0x441FC100;
static u32 *ge_cmd = (u32 *)0x441FC000;
static u16 *psp_gu_vram_base = (u16 *)(0x44000000);
static u32 *ge_cmd_ptr = (u32 *)0x441FC000;
static u32 gecbid;
static u32 video_direct = 0;
static u32 __attribute__((aligned(16))) display_list[32];
#define GBA_SCREEN_WIDTH 240
#define GBA_SCREEN_HEIGHT 160
#define RES_HW_SCREEN_HORIZONTAL 240
#define RES_HW_SCREEN_VERTICAL 240
#define PSP_SCREEN_WIDTH 480
#define PSP_SCREEN_HEIGHT 272
#define PSP_LINE_SIZE 512
#define PSP_ALL_BUTTON_MASK 0xFFFF
#define GE_CMD_FBP 0x9C
#define GE_CMD_FBW 0x9D
#define GE_CMD_TBP0 0xA0
#define GE_CMD_TBW0 0xA8
#define GE_CMD_TSIZE0 0xB8
#define GE_CMD_TFLUSH 0xCB
#define GE_CMD_CLEAR 0xD3
#define GE_CMD_VTYPE 0x12
#define GE_CMD_BASE 0x10
#define GE_CMD_VADDR 0x01
#define GE_CMD_IADDR 0x02
#define GE_CMD_PRIM 0x04
#define GE_CMD_FINISH 0x0F
#define GE_CMD_SIGNAL 0x0C
#define GE_CMD_NOP 0x00
#define GE_CMD(cmd, operand) \
*ge_cmd_ptr = (((GE_CMD_##cmd) << 24) | (operand)); \
ge_cmd_ptr++ \
static u16 *screen_texture = (u16 *)(0x4000000 + (512 * 272 * 2));
static u16 *current_screen_texture = (u16 *)(0x4000000 + (512 * 272 * 2));
static u16 *screen_pixels = (u16 *)(0x4000000 + (512 * 272 * 2));
static u32 screen_pitch = 240;
static void Ge_Finish_Callback(int id, void *arg)
{
}
#define get_screen_pixels() \
screen_pixels \
#define get_screen_pitch() \
screen_pitch \
#elif defined(POLLUX_BUILD)
static u16 rot_buffer[240*4];
static u32 rot_lines_total = 4;
static u32 rot_line_count = 0;
#ifdef WIZ_BUILD
static char rot_msg_buff[64];
#endif
static u32 screen_offset = 0;
static u16 *screen_pixels = NULL;
const u32 screen_pitch = 320;
#define get_screen_pixels() \
screen_pixels \
#define get_screen_pitch() \
screen_pitch \
#elif defined(PND_BUILD) || defined(RPI_BUILD)
static u16 *screen_pixels = NULL;
#define get_screen_pixels() \
screen_pixels \
#define get_screen_pitch() \
resolution_width \
#else
#endif
#ifdef GP2X_BUILD
#include "SDL_gp2x.h"
SDL_Surface *hw_screen;
#endif
#endif
#ifdef CHIP_BUILD
SDL_Surface *hw_screen = NULL;
#define GBA_SCREEN_WIDTH 240
#define GBA_SCREEN_HEIGHT 160
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define ABS(x) (((x) < 0) ? (-x) : (x))
#endif
SDL_Surface *screen = NULL;
SDL_Surface *virtual_hw_screen = NULL;
SDL_Rect screenRect, gbaRect;
const u32 video_scale = 1;
#define get_screen_pixels() \
((u16 *)screen->pixels) \
#define get_screen_pitch() \
(screen->pitch >> 1) \
int need_screen_cleared;
char hud_msg[HUD_MSG_MAX_SIZE];
int hud_msg_secs_on_screen = 0;
static int hud_ms_at_launch;
static void render_scanline_conditional_tile(u32 start, u32 end, u16 *scanline,
u32 enable_flags, u32 dispcnt, u32 bldcnt, const tile_layer_render_struct
*layer_renderers);
static void render_scanline_conditional_bitmap(u32 start, u32 end, u16 *scanline,
u32 enable_flags, u32 dispcnt, u32 bldcnt, const bitmap_layer_render_struct
*layer_renderers);
#define no_op \
// This old version is not necessary if the palette is either being converted
// transparently or the ABGR 1555 format is being used natively. The direct
// version (without conversion) is much faster.
#define tile_lookup_palette_full(palette, source) \
current_pixel = palette[source]; \
convert_palette(current_pixel) \
#define tile_lookup_palette(palette, source) \
current_pixel = palette[source]; \
#ifdef RENDER_COLOR16_NORMAL
#define tile_expand_base_normal(index) \
tile_expand_base_color16(index) \
#else
#define tile_expand_base_normal(index) \
tile_lookup_palette(palette, current_pixel); \
dest_ptr[index] = current_pixel \
#endif
#define tile_expand_transparent_normal(index) \
tile_expand_base_normal(index) \
#define tile_expand_copy(index) \
dest_ptr[index] = copy_ptr[index] \
#define advance_dest_ptr_base(delta) \
dest_ptr += delta \
#define advance_dest_ptr_transparent(delta) \
advance_dest_ptr_base(delta) \
#define advance_dest_ptr_copy(delta) \
advance_dest_ptr_base(delta); \
copy_ptr += delta \
#define color_combine_mask_a(layer) \
((io_registers[REG_BLDCNT] >> layer) & 0x01) \
// For color blending operations, will create a mask that has in bit
// 10 if the layer is target B, and bit 9 if the layer is target A.
#define color_combine_mask(layer) \
(color_combine_mask_a(layer) | \
((io_registers[REG_BLDCNT] >> (layer + 7)) & 0x02)) << 9 \
// For alpha blending renderers, draw the palette index (9bpp) and
// layer bits rather than the raw RGB. For the base this should write to
// the 32bit location directly.
#define tile_expand_base_alpha(index) \
dest_ptr[index] = current_pixel | pixel_combine \
#define tile_expand_base_bg(index) \
dest_ptr[index] = bg_combine \
// For layered (transparent) writes this should shift the "stack" and write
// to the bottom. This will preserve the topmost pixel and the most recent
// one.
#define tile_expand_transparent_alpha(index) \
dest_ptr[index] = (dest_ptr[index] << 16) | current_pixel | pixel_combine \
// OBJ should only shift if the top isn't already OBJ
#define tile_expand_transparent_alpha_obj(index) \
dest = dest_ptr[index]; \
if(dest & 0x00000100) \
{ \
dest_ptr[index] = (dest & 0xFFFF0000) | current_pixel | pixel_combine; \
} \
else \
{ \
dest_ptr[index] = (dest << 16) | current_pixel | pixel_combine; \
} \
// For color effects that don't need to preserve the previous layer.
// The color32 version should be used with 32bit wide dest_ptr so as to be
// compatible with alpha combine on top of it.
#define tile_expand_base_color16(index) \
dest_ptr[index] = current_pixel | pixel_combine \
#define tile_expand_transparent_color16(index) \
tile_expand_base_color16(index) \
#define tile_expand_base_color32(index) \
tile_expand_base_color16(index) \
#define tile_expand_transparent_color32(index) \
tile_expand_base_color16(index) \
// Operations for isolation 8bpp pixels within 32bpp pixel blocks.
#define tile_8bpp_pixel_op_mask(op_param) \
current_pixel = current_pixels & 0xFF \
#define tile_8bpp_pixel_op_shift_mask(shift) \
current_pixel = (current_pixels >> shift) & 0xFF \
#define tile_8bpp_pixel_op_shift(shift) \
current_pixel = current_pixels >> shift \
#define tile_8bpp_pixel_op_none(shift) \
// Base should always draw raw in 8bpp mode; color 0 will be drawn where
// color 0 is.
#define tile_8bpp_draw_base_normal(index) \
tile_expand_base_normal(index) \
#define tile_8bpp_draw_base_alpha(index) \
if(current_pixel) \
{ \
tile_expand_base_alpha(index); \
} \
else \
{ \
tile_expand_base_bg(index); \
} \
#define tile_8bpp_draw_base_color16(index) \
tile_8bpp_draw_base_alpha(index) \
#define tile_8bpp_draw_base_color32(index) \
tile_8bpp_draw_base_alpha(index) \
#define tile_8bpp_draw_base(index, op, op_param, alpha_op) \
tile_8bpp_pixel_op_##op(op_param); \
tile_8bpp_draw_base_##alpha_op(index) \
// Transparent (layered) writes should only replace what is there if the
// pixel is not transparent (zero)
#define tile_8bpp_draw_transparent(index, op, op_param, alpha_op) \
tile_8bpp_pixel_op_##op(op_param); \
if(current_pixel) \
{ \
tile_expand_transparent_##alpha_op(index); \
} \
#define tile_8bpp_draw_copy(index, op, op_param, alpha_op) \
tile_8bpp_pixel_op_##op(op_param); \
if(current_pixel) \
{ \
tile_expand_copy(index); \
} \
// Get the current tile from the map in 8bpp mode
#define get_tile_8bpp() \
current_tile = *map_ptr; \
tile_ptr = tile_base + ((current_tile & 0x3FF) * 64) \
// Draw half of a tile in 8bpp mode, for base renderer
#define tile_8bpp_draw_four_noflip(index, combine_op, alpha_op) \
tile_8bpp_draw_##combine_op(index + 0, mask, 0, alpha_op); \
tile_8bpp_draw_##combine_op(index + 1, shift_mask, 8, alpha_op); \
tile_8bpp_draw_##combine_op(index + 2, shift_mask, 16, alpha_op); \
tile_8bpp_draw_##combine_op(index + 3, shift, 24, alpha_op) \
// Like the above, but draws the half-tile horizontally flipped
#define tile_8bpp_draw_four_flip(index, combine_op, alpha_op) \
tile_8bpp_draw_##combine_op(index + 3, mask, 0, alpha_op); \
tile_8bpp_draw_##combine_op(index + 2, shift_mask, 8, alpha_op); \
tile_8bpp_draw_##combine_op(index + 1, shift_mask, 16, alpha_op); \
tile_8bpp_draw_##combine_op(index + 0, shift, 24, alpha_op) \
#define tile_8bpp_draw_four_base(index, alpha_op, flip_op) \
tile_8bpp_draw_four_##flip_op(index, base, alpha_op) \
// Draw half of a tile in 8bpp mode, for transparent renderer; as an
// optimization the entire thing is checked against zero (in transparent
// capable renders it is more likely for the pixels to be transparent than
// opaque)
#define tile_8bpp_draw_four_transparent(index, alpha_op, flip_op) \
if(current_pixels != 0) \
{ \
tile_8bpp_draw_four_##flip_op(index, transparent, alpha_op); \
} \
#define tile_8bpp_draw_four_copy(index, alpha_op, flip_op) \
if(current_pixels != 0) \
{ \
tile_8bpp_draw_four_##flip_op(index, copy, alpha_op); \
} \
// Helper macro for drawing 8bpp tiles clipped against the edge of the screen
#define partial_tile_8bpp(combine_op, alpha_op) \
for(i = 0; i < partial_tile_run; i++) \
{ \
tile_8bpp_draw_##combine_op(0, mask, 0, alpha_op); \
current_pixels >>= 8; \
advance_dest_ptr_##combine_op(1); \
} \
// Draws 8bpp tiles clipped against the left side of the screen,
// partial_tile_offset indicates how much clipped in it is, partial_tile_run
// indicates how much it should draw.
#define partial_tile_right_noflip_8bpp(combine_op, alpha_op) \
if(partial_tile_offset >= 4) \
{ \
current_pixels = *((u32 *)(tile_ptr + 4)) >> \
((partial_tile_offset - 4) * 8); \
partial_tile_8bpp(combine_op, alpha_op); \
} \
else \
{ \
partial_tile_run -= 4; \
current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 8); \
partial_tile_8bpp(combine_op, alpha_op); \
current_pixels = *((u32 *)(tile_ptr + 4)); \
tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
advance_dest_ptr_##combine_op(4); \
} \
// Draws 8bpp tiles clipped against both the left and right side of the
// screen, IE, runs of less than 8 - partial_tile_offset.
#define partial_tile_mid_noflip_8bpp(combine_op, alpha_op) \
if(partial_tile_offset >= 4) \
{ \
current_pixels = *((u32 *)(tile_ptr + 4)) >> \
((partial_tile_offset - 4) * 8); \
partial_tile_8bpp(combine_op, alpha_op); \
} \
else \
{ \
current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 8); \
if((partial_tile_offset + partial_tile_run) > 4) \
{ \
u32 old_run = partial_tile_run; \
partial_tile_run = 4 - partial_tile_offset; \
partial_tile_8bpp(combine_op, alpha_op); \
partial_tile_run = old_run - partial_tile_run; \
current_pixels = *((u32 *)(tile_ptr + 4)); \
partial_tile_8bpp(combine_op, alpha_op); \
} \
else \
{ \
partial_tile_8bpp(combine_op, alpha_op); \
} \
} \
// Draws 8bpp tiles clipped against the right side of the screen,
// partial_tile_run indicates how much there is to draw.
#define partial_tile_left_noflip_8bpp(combine_op, alpha_op) \
if(partial_tile_run >= 4) \
{ \
current_pixels = *((u32 *)tile_ptr); \
tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
advance_dest_ptr_##combine_op(4); \
tile_ptr += 4; \
partial_tile_run -= 4; \
} \
\
current_pixels = *((u32 *)(tile_ptr)); \
partial_tile_8bpp(combine_op, alpha_op) \
// Draws a non-clipped (complete) 8bpp tile.
#define tile_noflip_8bpp(combine_op, alpha_op) \
current_pixels = *((u32 *)tile_ptr); \
tile_8bpp_draw_four_##combine_op(0, alpha_op, noflip); \
current_pixels = *((u32 *)(tile_ptr + 4)); \
tile_8bpp_draw_four_##combine_op(4, alpha_op, noflip) \
// Like the above versions but draws flipped tiles.
#define partial_tile_flip_8bpp(combine_op, alpha_op) \
for(i = 0; i < partial_tile_run; i++) \
{ \
tile_8bpp_draw_##combine_op(0, shift, 24, alpha_op); \
current_pixels <<= 8; \
advance_dest_ptr_##combine_op(1); \
} \
#define partial_tile_right_flip_8bpp(combine_op, alpha_op) \
if(partial_tile_offset >= 4) \
{ \
current_pixels = *((u32 *)tile_ptr) << ((partial_tile_offset - 4) * 8); \
partial_tile_flip_8bpp(combine_op, alpha_op); \
} \
else \
{ \
partial_tile_run -= 4; \
current_pixels = *((u32 *)(tile_ptr + 4)) << \
((partial_tile_offset - 4) * 8); \
partial_tile_flip_8bpp(combine_op, alpha_op); \
current_pixels = *((u32 *)tile_ptr); \
tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
advance_dest_ptr_##combine_op(4); \
} \
#define partial_tile_mid_flip_8bpp(combine_op, alpha_op) \
if(partial_tile_offset >= 4) \
{ \
current_pixels = *((u32 *)tile_ptr) << ((partial_tile_offset - 4) * 8); \
partial_tile_flip_8bpp(combine_op, alpha_op); \
} \
else \
{ \
current_pixels = *((u32 *)(tile_ptr + 4)) << \
((partial_tile_offset - 4) * 8); \
\
if((partial_tile_offset + partial_tile_run) > 4) \
{ \
u32 old_run = partial_tile_run; \
partial_tile_run = 4 - partial_tile_offset; \
partial_tile_flip_8bpp(combine_op, alpha_op); \
partial_tile_run = old_run - partial_tile_run; \
current_pixels = *((u32 *)(tile_ptr)); \
partial_tile_flip_8bpp(combine_op, alpha_op); \
} \
else \
{ \
partial_tile_flip_8bpp(combine_op, alpha_op); \
} \
} \
#define partial_tile_left_flip_8bpp(combine_op, alpha_op) \
if(partial_tile_run >= 4) \
{ \
current_pixels = *((u32 *)(tile_ptr + 4)); \
tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
advance_dest_ptr_##combine_op(4); \
tile_ptr -= 4; \
partial_tile_run -= 4; \
} \
\
current_pixels = *((u32 *)(tile_ptr + 4)); \
partial_tile_flip_8bpp(combine_op, alpha_op) \
#define tile_flip_8bpp(combine_op, alpha_op) \
current_pixels = *((u32 *)(tile_ptr + 4)); \
tile_8bpp_draw_four_##combine_op(0, alpha_op, flip); \
current_pixels = *((u32 *)tile_ptr); \
tile_8bpp_draw_four_##combine_op(4, alpha_op, flip) \
// Operations for isolating 4bpp tiles in a 32bit block
#define tile_4bpp_pixel_op_mask(op_param) \
current_pixel = current_pixels & 0x0F \
#define tile_4bpp_pixel_op_shift_mask(shift) \
current_pixel = (current_pixels >> shift) & 0x0F \
#define tile_4bpp_pixel_op_shift(shift) \
current_pixel = current_pixels >> shift \
#define tile_4bpp_pixel_op_none(op_param) \
// Draws a single 4bpp pixel as base, normal renderer; checks to see if the
// pixel is zero because if so the current palette should not be applied.
// These ifs can be replaced with a lookup table, may or may not be superior
// this way, should be benchmarked. The lookup table would be from 0-255
// identity map except for multiples of 16, which would map to 0.
#define tile_4bpp_draw_base_normal(index) \
if(current_pixel) \
{ \
current_pixel |= current_palette; \
tile_expand_base_normal(index); \
} \
else \
{ \
tile_expand_base_normal(index); \
} \
#define tile_4bpp_draw_base_alpha(index) \
if(current_pixel) \
{ \
current_pixel |= current_palette; \
tile_expand_base_alpha(index); \
} \
else \
{ \
tile_expand_base_bg(index); \
} \
#define tile_4bpp_draw_base_color16(index) \
tile_4bpp_draw_base_alpha(index) \
#define tile_4bpp_draw_base_color32(index) \
tile_4bpp_draw_base_alpha(index) \
#define tile_4bpp_draw_base(index, op, op_param, alpha_op) \
tile_4bpp_pixel_op_##op(op_param); \
tile_4bpp_draw_base_##alpha_op(index) \
// Draws a single 4bpp pixel as layered, if not transparent.
#define tile_4bpp_draw_transparent(index, op, op_param, alpha_op) \
tile_4bpp_pixel_op_##op(op_param); \
if(current_pixel) \
{ \
current_pixel |= current_palette; \
tile_expand_transparent_##alpha_op(index); \
} \
#define tile_4bpp_draw_copy(index, op, op_param, alpha_op) \
tile_4bpp_pixel_op_##op(op_param); \
if(current_pixel) \
{ \
current_pixel |= current_palette; \
tile_expand_copy(index); \
} \
// Draws eight background pixels in transparent mode, for alpha or normal
// renderers.
#define tile_4bpp_draw_eight_base_zero(value) \
dest_ptr[0] = value; \
dest_ptr[1] = value; \
dest_ptr[2] = value; \
dest_ptr[3] = value; \
dest_ptr[4] = value; \
dest_ptr[5] = value; \
dest_ptr[6] = value; \
dest_ptr[7] = value \
// Draws eight background pixels for the alpha renderer, basically color zero
// with the background flag high.
#define tile_4bpp_draw_eight_base_zero_alpha() \
tile_4bpp_draw_eight_base_zero(bg_combine) \
#define tile_4bpp_draw_eight_base_zero_color16() \
tile_4bpp_draw_eight_base_zero_alpha() \
#define tile_4bpp_draw_eight_base_zero_color32() \
tile_4bpp_draw_eight_base_zero_alpha() \
// Draws eight background pixels for the normal renderer, just a bunch of
// zeros.
#ifdef RENDER_COLOR16_NORMAL
#define tile_4bpp_draw_eight_base_zero_normal() \
current_pixel = 0; \
tile_4bpp_draw_eight_base_zero(current_pixel) \
#else
#define tile_4bpp_draw_eight_base_zero_normal() \
current_pixel = palette[0]; \
tile_4bpp_draw_eight_base_zero(current_pixel) \
#endif
// Draws eight 4bpp pixels.
#define tile_4bpp_draw_eight_noflip(combine_op, alpha_op) \
tile_4bpp_draw_##combine_op(0, mask, 0, alpha_op); \
tile_4bpp_draw_##combine_op(1, shift_mask, 4, alpha_op); \
tile_4bpp_draw_##combine_op(2, shift_mask, 8, alpha_op); \
tile_4bpp_draw_##combine_op(3, shift_mask, 12, alpha_op); \
tile_4bpp_draw_##combine_op(4, shift_mask, 16, alpha_op); \
tile_4bpp_draw_##combine_op(5, shift_mask, 20, alpha_op); \
tile_4bpp_draw_##combine_op(6, shift_mask, 24, alpha_op); \
tile_4bpp_draw_##combine_op(7, shift, 28, alpha_op) \
// Draws eight 4bpp pixels in reverse order (for hflip).
#define tile_4bpp_draw_eight_flip(combine_op, alpha_op) \
tile_4bpp_draw_##combine_op(7, mask, 0, alpha_op); \
tile_4bpp_draw_##combine_op(6, shift_mask, 4, alpha_op); \
tile_4bpp_draw_##combine_op(5, shift_mask, 8, alpha_op); \
tile_4bpp_draw_##combine_op(4, shift_mask, 12, alpha_op); \
tile_4bpp_draw_##combine_op(3, shift_mask, 16, alpha_op); \
tile_4bpp_draw_##combine_op(2, shift_mask, 20, alpha_op); \
tile_4bpp_draw_##combine_op(1, shift_mask, 24, alpha_op); \
tile_4bpp_draw_##combine_op(0, shift, 28, alpha_op) \
// Draws eight 4bpp pixels in base mode, checks if all are zero, if so draws
// the appropriate background pixels.
#define tile_4bpp_draw_eight_base(alpha_op, flip_op) \
if(current_pixels != 0) \
{ \
tile_4bpp_draw_eight_##flip_op(base, alpha_op); \
} \
else \
{ \
tile_4bpp_draw_eight_base_zero_##alpha_op(); \
} \
// Draws eight 4bpp pixels in transparent (layered) mode, checks if all are
// zero and if so draws nothing.
#define tile_4bpp_draw_eight_transparent(alpha_op, flip_op) \
if(current_pixels != 0) \
{ \
tile_4bpp_draw_eight_##flip_op(transparent, alpha_op); \
} \
#define tile_4bpp_draw_eight_copy(alpha_op, flip_op) \
if(current_pixels != 0) \
{ \
tile_4bpp_draw_eight_##flip_op(copy, alpha_op); \
} \
// Gets the current tile in 4bpp mode, also getting the current palette and
// the pixel block.
#define get_tile_4bpp() \
current_tile = *map_ptr; \
current_palette = (current_tile >> 12) << 4; \
tile_ptr = tile_base + ((current_tile & 0x3FF) * 32); \
// Helper macro for drawing clipped 4bpp tiles.
#define partial_tile_4bpp(combine_op, alpha_op) \
for(i = 0; i < partial_tile_run; i++) \
{ \
tile_4bpp_draw_##combine_op(0, mask, 0, alpha_op); \
current_pixels >>= 4; \
advance_dest_ptr_##combine_op(1); \
} \
// Draws a 4bpp tile clipped against the left edge of the screen.
// partial_tile_offset is how far in it's clipped, partial_tile_run is
// how many to draw.
#define partial_tile_right_noflip_4bpp(combine_op, alpha_op) \
current_pixels = *((u32 *)tile_ptr) >> (partial_tile_offset * 4); \
partial_tile_4bpp(combine_op, alpha_op) \
// Draws a 4bpp tile clipped against both edges of the screen, same as right.
#define partial_tile_mid_noflip_4bpp(combine_op, alpha_op) \
partial_tile_right_noflip_4bpp(combine_op, alpha_op) \
// Draws a 4bpp tile clipped against the right edge of the screen.
// partial_tile_offset is how many to draw.
#define partial_tile_left_noflip_4bpp(combine_op, alpha_op) \
current_pixels = *((u32 *)tile_ptr); \
partial_tile_4bpp(combine_op, alpha_op) \
// Draws a complete 4bpp tile row (not clipped)
#define tile_noflip_4bpp(combine_op, alpha_op) \
current_pixels = *((u32 *)tile_ptr); \
tile_4bpp_draw_eight_##combine_op(alpha_op, noflip) \
// Like the above, but draws flipped tiles.
#define partial_tile_flip_4bpp(combine_op, alpha_op) \
for(i = 0; i < partial_tile_run; i++) \
{ \
tile_4bpp_draw_##combine_op(0, shift, 28, alpha_op); \
current_pixels <<= 4; \
advance_dest_ptr_##combine_op(1); \
} \
#define partial_tile_right_flip_4bpp(combine_op, alpha_op) \
current_pixels = *((u32 *)tile_ptr) << (partial_tile_offset * 4); \
partial_tile_flip_4bpp(combine_op, alpha_op) \
#define partial_tile_mid_flip_4bpp(combine_op, alpha_op) \
partial_tile_right_flip_4bpp(combine_op, alpha_op) \
#define partial_tile_left_flip_4bpp(combine_op, alpha_op) \
current_pixels = *((u32 *)tile_ptr); \
partial_tile_flip_4bpp(combine_op, alpha_op) \
#define tile_flip_4bpp(combine_op, alpha_op) \
current_pixels = *((u32 *)tile_ptr); \
tile_4bpp_draw_eight_##combine_op(alpha_op, flip) \
// Draws a single (partial or complete) tile from the tilemap, flipping
// as necessary.
#define single_tile_map(tile_type, combine_op, color_depth, alpha_op) \
get_tile_##color_depth(); \
if(current_tile & 0x800) \
tile_ptr += vertical_pixel_flip; \
\
if(current_tile & 0x400) \
{ \
tile_type##_flip_##color_depth(combine_op, alpha_op); \
} \
else \
{ \
tile_type##_noflip_##color_depth(combine_op, alpha_op); \
} \
// Draws multiple sequential tiles from the tilemap, hflips and vflips as
// necessary.
#define multiple_tile_map(combine_op, color_depth, alpha_op) \
for(i = 0; i < tile_run; i++) \
{ \
single_tile_map(tile, combine_op, color_depth, alpha_op); \
advance_dest_ptr_##combine_op(8); \
map_ptr++; \
} \
// Draws a partial tile from a tilemap clipped against the left edge of the
// screen.
#define partial_tile_right_map(combine_op, color_depth, alpha_op) \
single_tile_map(partial_tile_right, combine_op, color_depth, alpha_op); \
map_ptr++ \
// Draws a partial tile from a tilemap clipped against both edges of the
// screen.
#define partial_tile_mid_map(combine_op, color_depth, alpha_op) \
single_tile_map(partial_tile_mid, combine_op, color_depth, alpha_op) \
// Draws a partial tile from a tilemap clipped against the right edge of the
// screen.
#define partial_tile_left_map(combine_op, color_depth, alpha_op) \
single_tile_map(partial_tile_left, combine_op, color_depth, alpha_op) \
// Advances a non-flipped 4bpp obj to the next tile.
#define obj_advance_noflip_4bpp() \
tile_ptr += 32 \
// Advances a non-flipped 8bpp obj to the next tile.
#define obj_advance_noflip_8bpp() \
tile_ptr += 64 \
// Advances a flipped 4bpp obj to the next tile.
#define obj_advance_flip_4bpp() \
tile_ptr -= 32 \
// Advances a flipped 8bpp obj to the next tile.
#define obj_advance_flip_8bpp() \
tile_ptr -= 64 \
// Draws multiple sequential tiles from an obj, flip_op determines if it should
// be flipped or not (set to flip or noflip)
#define multiple_tile_obj(combine_op, color_depth, alpha_op, flip_op) \
for(i = 0; i < tile_run; i++) \
{ \
tile_##flip_op##_##color_depth(combine_op, alpha_op); \
obj_advance_##flip_op##_##color_depth(); \
advance_dest_ptr_##combine_op(8); \
} \
// Draws an obj's tile clipped against the left side of the screen
#define partial_tile_right_obj(combine_op, color_depth, alpha_op, flip_op) \
partial_tile_right_##flip_op##_##color_depth(combine_op, alpha_op); \
obj_advance_##flip_op##_##color_depth() \
// Draws an obj's tile clipped against both sides of the screen
#define partial_tile_mid_obj(combine_op, color_depth, alpha_op, flip_op) \
partial_tile_mid_##flip_op##_##color_depth(combine_op, alpha_op) \
// Draws an obj's tile clipped against the right side of the screen
#define partial_tile_left_obj(combine_op, color_depth, alpha_op, flip_op) \
partial_tile_left_##flip_op##_##color_depth(combine_op, alpha_op) \
// Extra variables specific for 8bpp/4bpp tile renderers.
#define tile_extra_variables_8bpp() \
#define tile_extra_variables_4bpp() \
u32 current_palette \
// Byte lengths of complete tiles and tile rows in 4bpp and 8bpp.
#define tile_width_4bpp 4
#define tile_size_4bpp 32
#define tile_width_8bpp 8
#define tile_size_8bpp 64
// Render a single scanline of text tiles
#define tile_render(color_depth, combine_op, alpha_op) \
{ \
u32 vertical_pixel_offset = (vertical_offset % 8) * \
tile_width_##color_depth; \
u32 vertical_pixel_flip = \
((tile_size_##color_depth - tile_width_##color_depth) - \
vertical_pixel_offset) - vertical_pixel_offset; \
tile_extra_variables_##color_depth(); \
u8 *tile_base = vram + (((bg_control >> 2) & 0x03) * (1024 * 16)) + \
vertical_pixel_offset; \
u32 pixel_run = 256 - (horizontal_offset % 256); \
u32 current_tile; \
\
map_base += ((vertical_offset % 256) / 8) * 32; \
partial_tile_offset = (horizontal_offset % 8); \
\
if(pixel_run >= end) \
{ \
if(partial_tile_offset) \
{ \
partial_tile_run = 8 - partial_tile_offset; \
if(end < partial_tile_run) \
{ \
partial_tile_run = end; \
partial_tile_mid_map(combine_op, color_depth, alpha_op); \
return; \
} \
else \
{ \
end -= partial_tile_run; \
partial_tile_right_map(combine_op, color_depth, alpha_op); \
} \
} \
\
tile_run = end / 8; \
multiple_tile_map(combine_op, color_depth, alpha_op); \
\
partial_tile_run = end % 8; \
\
if(partial_tile_run) \
{ \
partial_tile_left_map(combine_op, color_depth, alpha_op); \
} \
} \
else \
{ \
if(partial_tile_offset) \
{ \
partial_tile_run = 8 - partial_tile_offset; \
partial_tile_right_map(combine_op, color_depth, alpha_op); \
} \
\
tile_run = (pixel_run - partial_tile_run) / 8; \
multiple_tile_map(combine_op, color_depth, alpha_op); \
map_ptr = second_ptr; \
end -= pixel_run; \
tile_run = end / 8; \
multiple_tile_map(combine_op, color_depth, alpha_op); \
\
partial_tile_run = end % 8; \
if(partial_tile_run) \
{ \
partial_tile_left_map(combine_op, color_depth, alpha_op); \