-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
2627 lines (2292 loc) · 72 KB
/
main.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
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h> /* for getcwd() */
/* posix includes for mkdir */
#include <sys/types.h>
#include <sys/stat.h>
#ifdef WIZ
#include <fcntl.h>
#include <sys/mman.h>
#endif
#include <SDL/SDL.h>
#ifdef OHBOY_USE_SDL_IMAGE
#include "SDL/SDL_image.h"
#endif /* OHBOY_USE_SDL_IMAGE */
#include "SFont.h"
#include "font8px.h" /* built in FPS font */
/*
#include "font14px.h"
*/
#include "font_pressstart8x.h"
#include "gnuboy.h"
#include "fb.h"
#include "input.h"
#include "rc.h"
#include "pcm.h"
#include "ubytegui/gui.h"
#include "hw.h"
#include "loader.h"
#include "gui_sdl.h"
#include "menu.h"
#ifdef __GNUC__
# define unlikely(x) __builtin_expect((x),0)
# define prefetch(x, y) __builtin_prefetch((x),(y))
#else
# define unlikely(x) (x)
# define prefetch(x, y)
#endif
void ohb_loadrom(char *rom);
/* fps */
/*static int sdl_showfps 0 = OFF, 1 = Text Only, 2 = Text in a box (white text on black background) */
static int fps_current_count = 0;
static int fps_last_count = 0;
static int fps_last_time = 0;
static int fps_current_time = 0;
static char fps_str[20] = {0};
static SDL_Surface *fps_font_bitmap_surface=NULL;
static SDL_Surface *menu_font_bitmap_surface=NULL;
static SDL_Surface *bgimage_bitmap_surface=NULL;
SFont_Font* fps_font=NULL;
SFont_Font* menu_font=NULL;
SDL_Surface *bordersf = NULL;
SDL_Rect myrect;
/* fps */
#define FONT_NAME "pressstart.ttf"
#define FONT_SIZE 8
#define SFONT_NAME "pressstart_font"
static font_t *font;
struct fb fb;
static int upscaler=0, frameskip=0, sdl_showfps=0, cpu_speed=0, bmpenabled=0, statesram=1, analog_input=1, systemmode=0, blendframes=0;
static char* romdir=0, pal=0;
char *border;
char *gbcborder;
static int osd_persist=0;
static int dvolume=0;
#ifdef DINGOO_BUILD
static int button_a=1, button_b=2, button_x=0, button_y=0, button_l=0, button_r=0;
#else
static int button_a=2, button_b=1, button_x=0, button_y=0, button_l=0, button_r=0;
#endif /* DINGOO_BUILD */
#ifdef GNUBOY_HARDWARE_VOLUME
static int sndlvl=10;
#endif /* GNBOY_HARDWARE_VOLUME */
#ifdef GCWZERO
static int alt_menu_combo=0;
#endif /* GCWZERO */
rcvar_t vid_exports[] =
{
RCV_INT("frameskip", &frameskip),
RCV_INT("showfps", &sdl_showfps),
RCV_INT("cpuspeed",&cpu_speed),
RCV_STRING("romdir",&romdir),
RCV_INT("statesram", &statesram),
RCV_INT("systemmode", &systemmode),
#ifdef GNUBOY_HARDWARE_VOLUME
RCV_INT("sndlvl", &sndlvl),
#endif /* GNBOY_HARDWARE_VOLUME */
RCV_STRING("palette",&pal),
RCV_INT("upscaler", &upscaler),
RCV_INT("bmpenabled", &bmpenabled),
RCV_STRING("border",&border),
RCV_STRING("gbcborder",&gbcborder),
RCV_INT("blendframes",&blendframes),
RCV_INT("button_a", &button_a),
RCV_INT("button_b", &button_b),
RCV_INT("button_x", &button_x),
RCV_INT("button_y", &button_y),
RCV_INT("button_l", &button_l),
RCV_INT("button_r", &button_r),
RCV_INT("analog_input", &analog_input),
#ifdef GCWZERO
RCV_INT("alt_menu_combo", &alt_menu_combo),
#endif /* GCWZERO */
RCV_END
};
struct fb currframe_fb;
struct fb lastframe_fb;
static un16 vid_currframe[160*144];
static un16 vid_lastframe[160*144];
struct fb vid_fb;
static un16 vid_frame[160*144];
SDL_Surface *screen;
int framecounter = 0;
#ifndef GNUBOY_DISABLE_SDL_SOUND
#ifndef OHBOY_DISABLE_SDL_SOUND
struct pcm pcm;
rcvar_t pcm_exports[] =
{
RCV_END
};
#endif /* OHBOY_DISABLE_SDL_SOUND */
#endif /* GNUBOY_DISABLE_SDL_SOUND */
#define PCM_SAMPLERATE 44100
#ifdef GCWZERO
#define PCM_BUFFER 2048
#define PCM_FRAME 1024
#define VOL_MULTIPLIER 3
#else
#define PCM_BUFFER 3072
#define PCM_FRAME 1024
#define VOL_MULTIPLIER 1
#endif /* GCWZERO */
#define UP_MASK 0x83
#define DOWN_MASK 0x38
#define LEFT_MASK 0x0E
#define RIGHT_MASK 0xE0
n16 *buf;
n16 pcm_buffer[PCM_BUFFER << 1];
byte pcm_frame[PCM_FRAME << 1];
volatile int pcm_buffered = 0;
n16 *pcm_head, *pcm_tail;
int pcm_soft_volume=0x80, pcm_bufferlen;
SDL_Joystick * sdl_joy;
static const int joy_commit_range = 3276;
static char Xstatus, Ystatus;
static int use_joy=0;
#ifdef CAANOO
enum
{
BTN_A = 0, // A / 1
BTN_X = 1, // X / 2
BTN_B = 2, // B / 3
BTN_Y = 3, // Y / 4
BTN_L = 4, // L / 5, L1
BTN_R = 5, // R / 6, L2
BTN_HOME = 6, // Home / 7, R1
BTN_HOLD = 7, // Hold / 8, R2
BTN_HELP1 = 8, // Help I / Select
BTN_HELP2 = 9, // Help II / Start
BTN_TACT = 10, // Tact / L Thumb Stick
BTN_UP = 11,
BTN_DOWN = 12,
BTN_LEFT = 13,
BTN_RIGHT = 14,
};
void Caanoo_PushAnalogEvent(int btn, int pressed){
SDL_Event event;
event.type = (pressed)?SDL_JOYBUTTONDOWN:SDL_JOYBUTTONUP;
event.jbutton.button = btn;
event.jbutton.state = (pressed)?SDL_PRESSED:SDL_RELEASED;
event.jbutton.which = 0;
SDL_PushEvent(&event);
}
void Caanoo_UpdateAnalog(void){
static int buttonsPrevLeft = 0;
static int buttonsPrevRight = 0;
static int buttonsPrevUp = 0;
static int buttonsPrevDown = 0;
int buttonsNowLeft = 0;
int buttonsNowRight = 0;
int buttonsNowUp = 0;
int buttonsNowDown = 0;
SDL_JoystickUpdate();
if (SDL_JoystickGetAxis( sdl_joy, 0 ) < -24000) buttonsNowLeft |= (1 << BTN_LEFT);
else if (SDL_JoystickGetAxis( sdl_joy, 0 ) > 24000) buttonsNowRight |= (1 << BTN_RIGHT);
else if (SDL_JoystickGetAxis( sdl_joy, 1 ) < -24000) buttonsNowUp |= (1 << BTN_UP);
else if (SDL_JoystickGetAxis( sdl_joy, 1 ) > 24000) buttonsNowDown |= (1 << BTN_DOWN);
if (buttonsNowLeft != buttonsPrevLeft)
{
if ((buttonsNowLeft & (1 << BTN_LEFT)) != (buttonsPrevLeft & (1 << BTN_LEFT)))
{
Caanoo_PushAnalogEvent(BTN_LEFT, (buttonsNowLeft & (1 << BTN_LEFT)));
}
}
buttonsPrevLeft = buttonsNowLeft;
if (buttonsNowRight != buttonsPrevRight)
{
if ((buttonsNowRight & (1 << BTN_RIGHT)) != (buttonsPrevRight & (1 << BTN_RIGHT)))
{
Caanoo_PushAnalogEvent(BTN_RIGHT, (buttonsNowRight & (1 << BTN_RIGHT)));
}
}
buttonsPrevRight = buttonsNowRight;
if (buttonsNowUp != buttonsPrevUp)
{
if ((buttonsNowUp & (1 << BTN_UP)) != (buttonsPrevUp & (1 << BTN_UP)))
{
Caanoo_PushAnalogEvent(BTN_UP, (buttonsNowUp & (1 << BTN_UP)));
}
}
buttonsPrevUp = buttonsNowUp;
if (buttonsNowDown != buttonsPrevDown)
{
if ((buttonsNowDown & (1 << BTN_DOWN)) != (buttonsPrevDown & (1 << BTN_DOWN)))
{
Caanoo_PushAnalogEvent(BTN_DOWN, (buttonsNowDown & (1 << BTN_DOWN)));
}
}
buttonsPrevDown = buttonsNowDown;
}
#endif
rcvar_t joy_exports[] =
{
RCV_BOOL("joy", &use_joy),
RCV_END
};
/* keymap - mappings of the form { scancode, localcode } - from sdl/keymap.c */
extern int keymap[][2];
void vid_init() {
SDL_LockSurface(screen);
vid_fb.w = screen->w;
vid_fb.h = screen->h;
vid_fb.pelsize = screen->format->BytesPerPixel;
vid_fb.pitch = screen->pitch;
vid_fb.indexed = fb.pelsize == 1;
vid_fb.ptr = screen->pixels;
vid_fb.cc[0].r = screen->format->Rloss;
vid_fb.cc[0].l = screen->format->Rshift;
vid_fb.cc[1].r = screen->format->Gloss;
vid_fb.cc[1].l = screen->format->Gshift;
vid_fb.cc[2].r = screen->format->Bloss;
vid_fb.cc[2].l = screen->format->Bshift;
SDL_UnlockSurface(screen);
fb.w = 160; /* Gameboy native res - width */
fb.h = 144; /* Gameboy native res - height */
fb.pelsize = 2;
fb.pitch = 320;
fb.indexed = 0;
fb.cc[0].r = screen->format->Rloss;
fb.cc[0].l = screen->format->Rshift;
fb.cc[1].r = screen->format->Gloss;
fb.cc[1].l = screen->format->Gshift;
fb.cc[2].r = screen->format->Bloss;
fb.cc[2].l = screen->format->Bshift;
fb.ptr = vid_frame;
fb.enabled = 1;
fb.dirty = 0;
lastframe_fb.w = 160; /* Gameboy native res - width */
lastframe_fb.h = 144; /* Gameboy native res - height */
lastframe_fb.pelsize = 2;
lastframe_fb.pitch = 320;
lastframe_fb.indexed = 0;
lastframe_fb.cc[0].r = screen->format->Rloss;
lastframe_fb.cc[0].l = screen->format->Rshift;
lastframe_fb.cc[1].r = screen->format->Gloss;
lastframe_fb.cc[1].l = screen->format->Gshift;
lastframe_fb.cc[2].r = screen->format->Bloss;
lastframe_fb.cc[2].l = screen->format->Bshift;
lastframe_fb.ptr = vid_lastframe;
lastframe_fb.enabled = 1;
lastframe_fb.dirty = 0;
currframe_fb.w = 160; /* Gameboy native res - width */
currframe_fb.h = 144; /* Gameboy native res - height */
currframe_fb.pelsize = 2;
currframe_fb.pitch = 320;
currframe_fb.indexed = 0;
currframe_fb.cc[0].r = screen->format->Rloss;
currframe_fb.cc[0].l = screen->format->Rshift;
currframe_fb.cc[1].r = screen->format->Gloss;
currframe_fb.cc[1].l = screen->format->Gshift;
currframe_fb.cc[2].r = screen->format->Bloss;
currframe_fb.cc[2].l = screen->format->Bshift;
currframe_fb.ptr = vid_currframe;
currframe_fb.enabled = 1;
currframe_fb.dirty = 0;
SDL_FreeSurface (bordersf);
vid_fb.first_paint = 1;
if (bmpenabled == 0){
#ifdef OHBOY_USE_SDL_IMAGE
bordersf = IMG_Load("etc"DIRSEP"black.png");}
#else
bordersf = SDL_LoadBMP("etc"DIRSEP"black.bmp");}
#endif /*OHBOY_USE_SDL_IMAGE*/
else if (bmpenabled == 1)
{
#ifdef OHBOY_USE_SDL_IMAGE
if (hw.cgb){bordersf = IMG_Load(gbcborder);}
if (!hw.cgb){bordersf = IMG_Load(border);}
#else
if (hw.cgb){bordersf = SDL_LoadBMP(gbcborder);}
if (!hw.cgb){bordersf = SDL_LoadBMP(border);}
#endif /*OHBOY_USE_SDL_IMAGE*/
if ((upscaler == 3) || (upscaler == 4) || (upscaler == 7)) {
#ifdef OHBOY_USE_SDL_IMAGE
bordersf = IMG_Load("etc"DIRSEP"black.png");}
#else
bordersf = SDL_LoadBMP("etc"DIRSEP"black.bmp");}
#endif /*OHBOY_USE_SDL_IMAGE*/
#if defined(DINGOO_OPENDINGUX)
if (bordersf == NULL){ /*Fix for flickering screen when borders are set to On but no border is loaded, and double buffer is used*/
#ifdef OHBOY_USE_SDL_IMAGE
bordersf = IMG_Load("etc"DIRSEP"black.png");}
#else
bordersf = SDL_LoadBMP("etc"DIRSEP"black.bmp");}
#endif /*OHBOY_USE_SDL_IMAGE*/
#endif /*DINGOO_OPENDINGUX*/
}
}
void paint_menu_bg() {
int x, y;
pixmap_t *pix;
#ifdef UBYTE_USE_LIBPNG
#ifdef DINGOO_SIM
/* do nothing */
#else
pix = pixmap_loadpng("etc"DIRSEP"launch.png");
if(pix){
SDL_LockSurface(screen);
x = (screen->w - pix->width)/2;
y = (screen->h - pix->height)/2;
osd_drawpixmap(pix,x,y,0);
pixmap_free(pix);
SDL_UnlockSurface(screen);
}
#endif /* DINGOO_SIM */
#else
/*
** Use SDL_LoadBMP() from base SDL
** TODO use IMG_Load() from SDL_image
*/
SDL_Surface *tmp_surface=NULL;
#ifdef DINGOO_SIM
/* do nothing */
#else
#ifdef OHBOY_USE_SDL_IMAGE
tmp_surface = IMG_Load("etc"DIRSEP"launch.png");
#else
tmp_surface = SDL_LoadBMP("etc"DIRSEP"launch.bmp");
#endif /*OHBOY_USE_SDL_IMAGE*/
if (tmp_surface)
{
bgimage_bitmap_surface = SDL_DisplayFormat(tmp_surface);
SDL_FreeSurface(tmp_surface);
if (bgimage_bitmap_surface)
{
/*
SDL_Rect src, dest;
src.x = 0;
src.y = 0;
src.w = bgimage_bitmap_surface->w;
src.h = bgimage_bitmap_surface->h;
dest.x = 0;
dest.y = 0;
dest.w = bgimage_bitmap_surface->w;
dest.h = bgimage_bitmap_surface->h;
*/
SDL_UnlockSurface(screen);
SDL_BlitSurface(bgimage_bitmap_surface, NULL, screen, NULL);
/*
SDL_BlitSurface(bgimage_bitmap_surface, &src, screen, &dest);
*/
SDL_Flip(screen); /*Fix for launch BMP not showing when using double buffer*/
SDL_BlitSurface(bgimage_bitmap_surface, NULL, screen, NULL); /*Paints the launch BMP two times*/
SDL_FreeSurface(bgimage_bitmap_surface);
SDL_Flip(screen);
/*
SDL_LockSurface(screen); // for some reason this prevents SFont from displaying.....
*/
}
}
#endif /* DINGOO_SIM */
#endif /* UBYTE_USE_LIBPNG */
}
void vid_setpal(int i, int r, int g, int b){
/*
** complete NOOP
** internal gameboy framebuffer fb.ptr/vid_frame[] is used instead
** and then used to blit to screen at vid_end()
*/
}
/*
** This appears to be the GPL algorithm described at http://scale2x.sourceforge.net/algorithm.html
*/
#define SCALE3X(l,r,t,b,E0,E1,E2,E3,E4,E5,E6,E7,E8)\
A = src[t+l], B = src[t], C = src[t+r];\
D = src[ l ], E = src[0], F = src[ r ];\
G = src[b+l], H = src[b], I = src[b+r];\
if (B != H && D != F) {\
E0 += D == B ? D : E;\
E1 += (D == B && E != C) || (B == F && E != A) ? B : E;\
E2 += B == F ? F : E;\
E3 += (D == B && E != G) || (D == H && E != A) ? D : E;\
E4 += E;\
E5 += (B == F && E != I) || (H == F && E != C) ? F : E;\
E6 += D == H ? D : E;\
E7 += (D == H && E != I) || (H == F && E != G) ? H : E;\
E8 += H == F ? F : E;\
} else {\
E0 += E;\
E1 += E;\
E2 += E;\
E3 += E;\
E4 += E;\
E5 += E;\
E6 += E;\
E7 += E;\
E8 += E;\
}
#ifdef WIZ
static un16 buffer[3][216];
void ohb_scale3x(){
un16 *dst = buffer[0];
un16 *src = (un16*)fb.ptr+159;
un16 *base = (un16*)vid_fb.ptr + 9612;
int x,y;
un16 A,B,C,D,E,F,G,H,I;
memset(buffer,0,432*3);
// Top-right
SCALE3X(-1,0,0,160,dst[216],dst[0],dst[0],dst[216],dst[0],dst[0],dst[217],dst[1],dst[1]);
dst++, src+=160;
// right
for(y=1; y<143; y+=2){
SCALE3X(-1,0,-160,160,dst[216],dst[0],dst[0],dst[217],dst[1],dst[1],dst[217],dst[1],dst[1]);
dst+=2, src+=160;
SCALE3X(-1,0,-160,160,dst[216],dst[0],dst[0],dst[216],dst[0],dst[0],dst[217],dst[1],dst[1]);
dst++, src+=160;
}
// bottom-Right
SCALE3X(-1,0,-160,0,dst[216],dst[0],dst[0],dst[217],dst[1],dst[1],dst[217],dst[1],dst[1]);
dst+=2, src+=160;
for(x=158; x>1; x-=2){
src = (un16*)fb.ptr + x;
// top
SCALE3X(-1,1,0,160,dst[216],dst[216],dst[0],dst[216],dst[216],dst[0],dst[217],dst[217],dst[1]);
dst++, src+=160;
// middle
for(y=1; y<143; y+=2){
SCALE3X(-1,1,-160,160,dst[216],dst[216],dst[0],dst[217],dst[217],dst[1],dst[217],dst[217],dst[1]);
dst+=2, src+=160;
SCALE3X(-1,1,-160,160,dst[216],dst[216],dst[0],dst[216],dst[216],dst[0],dst[217],dst[217],dst[1]);
dst++, src+=160;
}
// bottom
SCALE3X(-1,1,-160,0,dst[216],dst[216],dst[0],dst[217],dst[217],dst[1],dst[217],dst[217],dst[1]);
dst+=2, src+=160;
memcpy(base,buffer[0],432);
memcpy(base+240,buffer[1],432);
memcpy(base+480,buffer[2],432);
dst=buffer[0];
base += 720;
memset(buffer,0,432*3);
src = (un16*)fb.ptr + x-1;
// top
SCALE3X(-1,1,0,160,dst[216],dst[0],dst[0],dst[216],dst[0],dst[0],dst[217],dst[1],dst[1]);
dst++, src+=160;
// middle
for(y=1; y<143; y+=2){
SCALE3X(-1,1,-160,160,dst[216],dst[0],dst[0],dst[217],dst[1],dst[1],dst[217],dst[1],dst[1]);
dst+=2, src+=160;
SCALE3X(-1,1,-160,160,dst[216],dst[0],dst[0],dst[216],dst[0],dst[0],dst[217],dst[1],dst[1]);
dst++, src+=160;
}
// bottom
SCALE3X(-1,1,-160,0,dst[216],dst[0],dst[0],dst[217],dst[1],dst[1],dst[217],dst[1],dst[1]);
dst+=2, src+=160;
}
src = fb.ptr;
// top-left
SCALE3X(0,1,0,160,dst[216],dst[216],dst[0],dst[216],dst[216],dst[0],dst[217],dst[217],dst[1]);
dst++, src+=160;
// left
for(y=1; y<143; y+=2){
SCALE3X(0,1,-160,160,dst[216],dst[216],dst[0],dst[217],dst[217],dst[1],dst[217],dst[217],dst[1]);
dst+=2, src+=160;
SCALE3X(0,1,-160,160,dst[216],dst[216],dst[0],dst[216],dst[216],dst[0],dst[217],dst[217],dst[1]);
dst++, src+=160;
}
// bottom-right
SCALE3X(0,1,-160,0,dst[216],dst[216],dst[0],dst[217],dst[217],dst[1],dst[217],dst[217],dst[1]);
dst+=2, src+=160;
memcpy(base,buffer[0],432);
memcpy(base+240,buffer[1],432);
memcpy(base+480,buffer[2],432);
}
#else
static un16 buffer[3][240];
/*
** Appears to expect source/dest framebuffer pixels to be 16bpp
** But expects source to be in RGB343 format. Looks like it may expect dest to be RGB565.
** 1.5 scaler with "smoothing", i.e. 160x144 -> 239x215 BUT centered/centred in a 320x240 screen.
** It looks like it does a 2 pass conversion:
** 1) increase 3x
** 2) decrease by 2x
*/
void ohb_scale3x(){
un16 *dst = buffer[0];
un16 *src = (un16*)fb.ptr;
un16 *base = (un16*)vid_fb.ptr + 3880;
int x,y;
un16 A,B,C,D,E,F,G,H,I;
memset(buffer,0,480*3);
// Top-left
SCALE3X(0,1,0,160,dst[0],dst[0],dst[1],dst[0],dst[0],dst[1],dst[240],dst[240],dst[241]);
dst++, src++;
// Top
for(x=1; x<159; x+=2){
SCALE3X(-1,1,0,160,dst[0],dst[1],dst[1],dst[0],dst[1],dst[1],dst[240],dst[241],dst[241]);
dst+=2, src++;
SCALE3X(-1,1,0,160,dst[0],dst[0],dst[1],dst[0],dst[0],dst[1],dst[240],dst[240],dst[241]);
dst++, src++;
}
// Top-Right
SCALE3X(-1,0,0,160,dst[0],dst[1],dst[1],dst[0],dst[1],dst[1],dst[240],dst[241],dst[241]);
dst+=2, src++;
for(y=1; y<142; y+=2){
// left
SCALE3X(0,1,-160,160,dst[0],dst[0],dst[1],dst[240],dst[240],dst[241],dst[240],dst[240],dst[241]);
dst++, src++;
// middle
for(x=1; x<159; x+=2){
SCALE3X(-1,1,-160,160,dst[0],dst[1],dst[1],dst[240],dst[241],dst[241],dst[240],dst[241],dst[241]);
dst+=2, src++;
SCALE3X(-1,1,-160,160,dst[0],dst[0],dst[1],dst[240],dst[240],dst[241],dst[240],dst[240],dst[241]);
dst++, src++;
}
// right
SCALE3X(-1,0,-160,160,dst[0],dst[1],dst[1],dst[240],dst[241],dst[241],dst[240],dst[241],dst[241]);
dst+=2, src++;
memcpy(base,buffer[0],480);
memcpy(base+320,buffer[1],480);
memcpy(base+640,buffer[2],480);
dst=(un16*)buffer;
base += 960;
memset(buffer,0,480*3);
// left
SCALE3X(0,1,-160,160,dst[0],dst[0],dst[1],dst[0],dst[0],dst[1],dst[240],dst[240],dst[241]);
dst++, src++;
// middle
for(x=1; x<159; x+=2){
SCALE3X(-1,1,-160,160,dst[0],dst[1],dst[1],dst[0],dst[1],dst[1],dst[240],dst[241],dst[241]);
dst+=2, src++;
SCALE3X(-1,1,-160,160,dst[0],dst[0],dst[1],dst[0],dst[0],dst[1],dst[240],dst[240],dst[241]);
dst++, src++;
}
// right
SCALE3X(-1,0,-160,160,dst[0],dst[1],dst[1],dst[0],dst[1],dst[1],dst[240],dst[241],dst[241]);
dst+=2, src++;
}
// left
SCALE3X(0,1,-160,0,dst[0],dst[0],dst[1],dst[240],dst[240],dst[241],dst[240],dst[240],dst[241]);
dst++, src++;
// middle
for(x=1; x<159; x+=2){
SCALE3X(-1,1,-160,0,dst[0],dst[1],dst[1],dst[240],dst[241],dst[241],dst[240],dst[241],dst[241]);
dst+=2, src++;
SCALE3X(-1,1,-160,0,dst[0],dst[0],dst[1],dst[240],dst[240],dst[241],dst[240],dst[240],dst[241]);
dst++, src++;
}
// right
SCALE3X(-1,0,-160,0,dst[0],dst[1],dst[1],dst[240],dst[241],dst[241],dst[240],dst[241],dst[241]);
dst+=2, src++;
memcpy(base,buffer[0],480);
memcpy(base+320,buffer[1],480);
memcpy(base+640,buffer[2],480);
}
void blend_scale3x(){
un16 *dst = buffer[0];
un16 *src = (un16*)currframe_fb.ptr;
un16 *base = (un16*)vid_fb.ptr + 3880;
int x,y;
un16 A,B,C,D,E,F,G,H,I;
memset(buffer,0,480*3);
// Top-left
SCALE3X(0,1,0,160,dst[0],dst[0],dst[1],dst[0],dst[0],dst[1],dst[240],dst[240],dst[241]);
dst++, src++;
// Top
for(x=1; x<159; x+=2){
SCALE3X(-1,1,0,160,dst[0],dst[1],dst[1],dst[0],dst[1],dst[1],dst[240],dst[241],dst[241]);
dst+=2, src++;
SCALE3X(-1,1,0,160,dst[0],dst[0],dst[1],dst[0],dst[0],dst[1],dst[240],dst[240],dst[241]);
dst++, src++;
}
// Top-Right
SCALE3X(-1,0,0,160,dst[0],dst[1],dst[1],dst[0],dst[1],dst[1],dst[240],dst[241],dst[241]);
dst+=2, src++;
for(y=1; y<142; y+=2){
// left
SCALE3X(0,1,-160,160,dst[0],dst[0],dst[1],dst[240],dst[240],dst[241],dst[240],dst[240],dst[241]);
dst++, src++;
// middle
for(x=1; x<159; x+=2){
SCALE3X(-1,1,-160,160,dst[0],dst[1],dst[1],dst[240],dst[241],dst[241],dst[240],dst[241],dst[241]);
dst+=2, src++;
SCALE3X(-1,1,-160,160,dst[0],dst[0],dst[1],dst[240],dst[240],dst[241],dst[240],dst[240],dst[241]);
dst++, src++;
}
// right
SCALE3X(-1,0,-160,160,dst[0],dst[1],dst[1],dst[240],dst[241],dst[241],dst[240],dst[241],dst[241]);
dst+=2, src++;
memcpy(base,buffer[0],480);
memcpy(base+320,buffer[1],480);
memcpy(base+640,buffer[2],480);
dst=(un16*)buffer;
base += 960;
memset(buffer,0,480*3);
// left
SCALE3X(0,1,-160,160,dst[0],dst[0],dst[1],dst[0],dst[0],dst[1],dst[240],dst[240],dst[241]);
dst++, src++;
// middle
for(x=1; x<159; x+=2){
SCALE3X(-1,1,-160,160,dst[0],dst[1],dst[1],dst[0],dst[1],dst[1],dst[240],dst[241],dst[241]);
dst+=2, src++;
SCALE3X(-1,1,-160,160,dst[0],dst[0],dst[1],dst[0],dst[0],dst[1],dst[240],dst[240],dst[241]);
dst++, src++;
}
// right
SCALE3X(-1,0,-160,160,dst[0],dst[1],dst[1],dst[0],dst[1],dst[1],dst[240],dst[241],dst[241]);
dst+=2, src++;
}
// left
SCALE3X(0,1,-160,0,dst[0],dst[0],dst[1],dst[240],dst[240],dst[241],dst[240],dst[240],dst[241]);
dst++, src++;
// middle
for(x=1; x<159; x+=2){
SCALE3X(-1,1,-160,0,dst[0],dst[1],dst[1],dst[240],dst[241],dst[241],dst[240],dst[241],dst[241]);
dst+=2, src++;
SCALE3X(-1,1,-160,0,dst[0],dst[0],dst[1],dst[240],dst[240],dst[241],dst[240],dst[240],dst[241]);
dst++, src++;
}
// right
SCALE3X(-1,0,-160,0,dst[0],dst[1],dst[1],dst[240],dst[241],dst[241],dst[240],dst[241],dst[241]);
dst+=2, src++;
memcpy(base,buffer[0],480);
memcpy(base+320,buffer[1],480);
memcpy(base+640,buffer[2],480);
}
#endif
/****************************************************************/
/* Upscale from 160x144 to 320x240 */
void gb_upscale(uint32_t *to, uint32_t *from)
{
uint32_t reg1, reg2, reg3, reg4;
unsigned int x,y;
/* Before:
* a b
* c d
* e f
*
* After (parenthesis = average):
* a a b b
* (a,c) (a,c) (b,d) (b,d)
* c c d d
* (c,e) (c,e) (d,f) (d,f)
* e e f f
*/
for (y=0; y < 240/5; y++) {
for(x=0; x < 320/4; x++) {
prefetch(to+4, 1);
/* Read b-a */
reg2 = *from;
reg1 = reg2 & 0xffff0000;
reg1 |= reg1 >> 16;
/* Write b-b */
*(to+1) = reg1;
reg2 = reg2 & 0xffff;
reg2 |= reg2 << 16;
/* Write a-a */
*to = reg2;
/* Read d-c */
reg4 = *(from + 160/2);
reg3 = reg4 & 0xffff0000;
reg3 |= reg3 >> 16;
/* Write d-d */
*(to + 2*320/2 +1) = reg3;
reg4 = reg4 & 0xffff;
reg4 |= reg4 << 16;
/* Write c-c */
*(to + 2*320/2) = reg4;
/* Write (b,d)-(b,d) */
if (unlikely(reg1 != reg3))
reg1 = ((reg1 & 0xf7def7de) >> 1) + ((reg3 & 0xf7def7de) >> 1);
*(to + 320/2 +1) = reg1;
/* Write (a,c)-(a,c) */
if (unlikely(reg2 != reg4))
reg2 = ((reg2 & 0xf7def7de) >> 1) + ((reg4 & 0xf7def7de) >> 1);
*(to + 320/2) = reg2;
/* Read f-e */
reg2 = *(from++ + 2*160/2);
reg1 = reg2 & 0xffff0000;
reg1 |= reg1 >> 16;
/* Write f-f */
*(to + 4*320/2 +1) = reg1;
reg2 = reg2 & 0xffff;
reg2 |= reg2 << 16;
/* Write e-e */
*(to + 4*320/2) = reg2;
/* Write (d,f)-(d,f) */
if (unlikely(reg2 != reg4))
reg2 = ((reg2 & 0xf7def7de) >> 1) + ((reg4 & 0xf7def7de) >> 1);
*(to++ + 3*320/2) = reg2;
/* Write (c,e)-(c,e) */
if (unlikely(reg1 != reg3))
reg1 = ((reg1 & 0xf7def7de) >> 1) + ((reg3 & 0xf7def7de) >> 1);
*(to++ + 3*320/2) = reg1;
}
to += 4*320/2;
from += 2*160/2;
}
}
/* Upscale from 160x144 to 240x216 */
void ayla_scale15x(uint32_t *to, uint32_t *from)
{
/* Before:
* a b c d
* e f g h
*
* After (parenthesis = average):
* a (a,b) b c (c,d) d
* (a,e) (a,b,e,f) (b,f) (c,g) (c,d,g,h) (d,h)
* e (e,f) f g (g,h) h
*/
uint32_t reg1, reg2, reg3, reg4, reg5;
size_t x, y;
for (y=0; y<216/3; y++) {
for (x=0; x<240/6; x++) {
prefetch(to+4, 1);
/* Read b-a */
reg1 = *from;
reg5 = reg1 >> 16;
if (unlikely((reg1 & 0xffff) != reg5)) {
reg2 = (reg1 & 0xf7de0000) >> 1;
reg1 = (reg1 & 0xffff) + reg2 + ((reg1 & 0xf7de) << 15);
}
/* Write (a,b)-a */
*to = reg1;
/* Read f-e */
reg3 = *(from++ + 160/2);
reg2 = reg3 >> 16;
if (unlikely((reg3 & 0xffff) != reg2)) {
reg4 = (reg3 & 0xf7de0000) >> 1;
reg3 = (reg3 & 0xffff) + reg4 + ((reg3 & 0xf7de) << 15);
}
/* Write (e,f)-e */
*(to + 2*320/2) = reg3;
/* Write (a,b,e,f)-(a,e) */
if (unlikely(reg1 != reg3))
reg1 = ((reg1 & 0xf7def7de) >> 1) + ((reg3 & 0xf7def7de) >> 1);
*(to++ + 320/2) = reg1;
/* Read d-c */
reg1 = *from;
reg4 = reg1 << 16;
/* Write c-b */
reg5 |= reg4;
*to = reg5;
/* Read h-g */
reg3 = *(from++ + 160/2);
/* Write g-f */
reg2 |= (reg3 << 16);
*(to + 2*320/2) = reg2;
/* Write (c,g)-(b,f) */
if (unlikely(reg2 != reg5))
reg2 = ((reg5 & 0xf7def7de) >> 1) + ((reg2 & 0xf7def7de) >> 1);
*(to++ + 320/2) = reg2;
/* Write d-(c,d) */
if (unlikely((reg1 & 0xffff0000) != reg4)) {
reg2 = (reg1 & 0xf7def7de) >> 1;
reg1 = (reg1 & 0xffff0000) | ((reg2 + (reg2 >> 16)) & 0xffff);
}
*to = reg1;
/* Write h-(g,h) */
if (unlikely((reg3 & 0xffff) != reg3 >> 16)) {
reg2 = (reg3 & 0xf7def7de) >> 1;
reg3 = (reg3 & 0xffff0000) | ((reg2 + (reg2 >> 16)) & 0xffff);
}
*(to + 2*320/2) = reg3;
/* Write (d,h)-(c,d,g,h) */
if (unlikely(reg1 != reg3))
reg1 = ((reg1 & 0xf7def7de) >> 1) + ((reg3 & 0xf7def7de) >> 1);
*(to++ + 320/2) = reg1;
}
to += 2*360/2;
from += 160/2;
}
}
/*
* Approximately bilinear scaler, 160x144 to 280x240
*
* Copyright (C) 2014 hi-ban, Nebuleon <[email protected]>
*
* This function and all auxiliary functions are free software; you can
* redistribute them and/or modify them under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* These functions are distributed in the hope that they will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Support math
#define Half(A) (((A) >> 1) & 0x7BEF)
// Error correction expressions to piece back the lower bits together
#define RestHalf(A) ((A) & 0x0821)
// Error correction expressions for halves
#define Corr1_1(A, B) ((A) & (B) & 0x0821)
// Halves
#define Weight1_1(A, B) (Half(A) + Half(B) + Corr1_1(A, B))
/* Upscales a 160x144 image to 280x240 using an approximate bilinear
* resampling algorithm that only uses integer math.
*
* Input:
* src: A packed 160x144 pixel image. The pixel format of this image is
* RGB 565.
* Output:
* dst: A packed 280x240 pixel image. The pixel format of this image is
* RGB 565.
*/
void upscale_160x144_to_280x240_bilinearish(uint32_t* dst, uint32_t* src)
{
uint16_t* Src16 = (uint16_t*) src;
uint16_t* Dst16 = (uint16_t*) dst;
// There are 40 blocks of 4 pixels horizontally, and 48 of 3 vertically.
// Each block of 4x3 becomes 7x5.
uint32_t BlockX, BlockY;
uint16_t* BlockSrc;
uint16_t* BlockDst;
for (BlockY = 0; BlockY < 48; BlockY++)
{
BlockSrc = Src16 + BlockY * 160 * 3;
BlockDst = Dst16 + BlockY * 320 * 5;
for (BlockX = 0; BlockX < 40; BlockX++)
{
/* Horizontally:
* Before(4):
* (a)(b)(c)(d)
* After(7):
* (a)(ab)(b)(bc)(c)(cd)(d)
*
* Vertically:
* Before(3): After(5):
* (a) (a)
* (b) (ab)
* (c) (b)
* (bc)