-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsdl.cc
744 lines (566 loc) · 18.9 KB
/
sdl.cc
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
#include <SDL.h>
//#ifdef GP2X
# include <sys/mman.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <sys/ioctl.h>
# include <sys/soundcard.h>
# include <sys/time.h>
# include <sys/resource.h>
# include <sys/types.h>
# include <unistd.h>
# include <string.h>
//#endif
#include <stdlib.h>
#include "beebconfig.h"
#include "beebconfig_data.h"
#include "video.h"
#ifdef USE_DMA
#include "dma.h"
#endif
#ifdef GP2X
/* GP2x MMSP2 registers:
*/
//volatile unsigned long *MMSP2_16;
//volatile unsigned short *MMSP2_32;
volatile Uint8 *gp2x_sound_buffer_p=NULL;
static volatile Uint16 *gp2x_memregs=NULL;
static volatile Uint32 *gp2x_memregl=NULL;
static int gp2x_dev_mem=NULL;
static void GP2x_InitializeSoundBuffer(void);
#endif
/* Joystick for GP2X:
*/
#ifdef GP2X
SDL_Joystick *joystick_p = NULL;
#endif
/* Frame buffer:
*/
SDL_Surface *frame_buffer_p=NULL;
SDL_Surface *rgb_surface=NULL;
/* Sound support:
*/
#define SOUND_BUFFER_SIZE (1024*64)
Uint8 SDLSoundBuffer[SOUND_BUFFER_SIZE];
unsigned long SDLSoundBufferOffset_IN;
unsigned long SDLSoundBufferOffset_OUT;
unsigned long SDLSoundBufferBytesHave;
/* This is a hack for now, the GP2x will say
* that 512 samples is OK, then take 1024 via the
* callback.
*
* I'll investigate this later.
*/
//#define REQUESTED_NUMBER_OF_SAMPLES 8192
//#define REQUESTED_NUMBER_OF_SAMPLES 4096
//#define REQUESTED_NUMBER_OF_SAMPLES 2048
//#define REQUESTED_NUMBER_OF_SAMPLES 256
//#define REQUESTED_NUMBER_OF_SAMPLES 128
#ifndef GP2x
# define REQUESTED_NUMBER_OF_SAMPLES 1024
//# define REQUESTED_NUMBER_OF_SAMPLES 512
//# define REQUESTED_NUMBER_OF_SAMPLES 256
#else
# define REQUESTED_NUMBER_OF_SAMPLES 512
#endif
int samples;
SDL_AudioSpec wanted;
void InitializeSoundBuffer(void)
{
SDLSoundBufferOffset_IN =0;
SDLSoundBufferOffset_OUT = 0;
SDLSoundBufferBytesHave = 0;
int i;
for(i=0; i< SOUND_BUFFER_SIZE; i++)
SDLSoundBuffer[i] = (Uint8) 0;
}
void FlushSoundBuffer(void)
{
InitializeSoundBuffer();
}
/*
void AddBytesToSDLSoundBuffer(void *p, int len)
{
//REQUESTED_NUMBER_OF_SAMPLES
int i;
Uint8 *pp = (Uint8*) p;
for(i=0; i<len; i++)
SDLSoundBuffer[i] = *(pp++);
}
*/
static int buffer_sound=0;
/*
void AddBytesToSDLSoundBuffer(void *p, int len)
{
int i;
Uint8 *pp;
pp = (Uint8*) p;
// printf("ADDED %d BYTES\n %d %d %d", len, (int) pp[0], (int) pp[1], (int) pp[2]);
for(i=0; i<len; i++){
SDLSoundBuffer[SDLSoundBufferOffset_IN] = *(pp++);
SDLSoundBufferOffset_IN++;
if (SDLSoundBufferOffset_IN >= SOUND_BUFFER_SIZE)
SDLSoundBufferOffset_IN = (unsigned long) 0;
}
SDLSoundBufferBytesHave+=len;
if (SDLSoundBufferBytesHave > SOUND_BUFFER_SIZE) {
printf("*** SOUND BUFFER OVERRUN ***\n");
if (SDLSoundBufferOffset_IN >= REQUESTED_NUMBER_OF_SAMPLES*10)
SDLSoundBufferOffset_OUT = SDLSoundBufferOffset_IN - REQUESTED_NUMBER_OF_SAMPLES*10;
else
SDLSoundBufferOffset_OUT = SOUND_BUFFER_SIZE-1 - REQUESTED_NUMBER_OF_SAMPLES*10 + SDLSoundBufferOffset_IN;
SDLSoundBufferBytesHave = REQUESTED_NUMBER_OF_SAMPLES*10;
}
// Only start sound once we have enough samples buffered.
if (SDLSoundBufferBytesHave >= (REQUESTED_NUMBER_OF_SAMPLES*3) && buffer_sound == 0) {
printf("Now have %d, starting sound\n", SDLSoundBufferBytesHave);
SDL_PauseAudio(0);
buffer_sound=1;
}
}
*/
void AddBytesToSDLSoundBuffer(void *p, int len)
{
int i;
Uint8 *pp;
pp = (Uint8*) p;
// printf("ADDED %d BYTES\n %d %d %d", len, (int) pp[0], (int) pp[1], (int) pp[2]);
for(i=0; i<len; i++){
SDLSoundBuffer[SDLSoundBufferOffset_IN] = *(pp++);
SDLSoundBufferOffset_IN++;
if (SDLSoundBufferOffset_IN >= SOUND_BUFFER_SIZE)
SDLSoundBufferOffset_IN = (unsigned long) 0;
}
SDLSoundBufferBytesHave+=len;
if (SDLSoundBufferBytesHave > SOUND_BUFFER_SIZE) {
//printf("*** SOUND BUFFER OVERRUN ***\n");
if (SDLSoundBufferOffset_IN >= REQUESTED_NUMBER_OF_SAMPLES*10)
SDLSoundBufferOffset_OUT = SDLSoundBufferOffset_IN - REQUESTED_NUMBER_OF_SAMPLES*10;
else
SDLSoundBufferOffset_OUT = SOUND_BUFFER_SIZE-1 - REQUESTED_NUMBER_OF_SAMPLES*10 + SDLSoundBufferOffset_IN;
SDLSoundBufferBytesHave = REQUESTED_NUMBER_OF_SAMPLES*10;
}
// Only start sound once we have enough samples buffered.
// if (SDLSoundBufferBytesHave >= (REQUESTED_NUMBER_OF_SAMPLES*3) && buffer_sound == 0) {
if (SDLSoundBufferBytesHave >= (REQUESTED_NUMBER_OF_SAMPLES*2) && buffer_sound == 0) {
//printf("Now have %d, starting sound\n", SDLSoundBufferBytesHave);
SDL_PauseAudio(0);
buffer_sound=1;
}
}
volatile Uint8 SequentialSDLSoundBuffer[10*1024]; // Shouldn't get this big!
volatile Uint8* GetSoundBufferPtr(void)
{
return SequentialSDLSoundBuffer;
//return gp2x_sound_buffer_p;
}
/*
static int CatchupSound(void)
{
int i=0;
// was
// while ( SDLSoundBufferBytesHave > ( REQUESTED_NUMBER_OF_SAMPLES * 1)){
// while ( SDLSoundBufferBytesHave > ( REQUESTED_NUMBER_OF_SAMPLES + (REQUESTED_NUMBER_OF_SAMPLES>>1) )) {
while ( SDLSoundBufferBytesHave > ( REQUESTED_NUMBER_OF_SAMPLES * 2)){
// Dump two out of the buffer per time.
// 1
SDLSoundBuffer[SDLSoundBufferOffset_OUT] = 0;
i++;
SDLSoundBufferOffset_OUT++;
if (SDLSoundBufferOffset_OUT >= SOUND_BUFFER_SIZE)
SDLSoundBufferOffset_OUT = (unsigned long) 0;
SDLSoundBufferBytesHave--;
// 2
SDLSoundBuffer[SDLSoundBufferOffset_OUT] = 0;
i++;
SDLSoundBufferOffset_OUT++;
if (SDLSoundBufferOffset_OUT >= SOUND_BUFFER_SIZE)
SDLSoundBufferOffset_OUT = (unsigned long) 0;
SDLSoundBufferBytesHave--;
}
return i;
}
*/
void CatchupSound(void)
{
while (SDLSoundBufferBytesHave > ( REQUESTED_NUMBER_OF_SAMPLES * 2)){
SDLSoundBuffer[SDLSoundBufferOffset_OUT] = 0;
SDLSoundBufferOffset_OUT++;
if (SDLSoundBufferOffset_OUT >= SOUND_BUFFER_SIZE)
SDLSoundBufferOffset_OUT = (unsigned long) 0;
SDLSoundBufferBytesHave--;
}
}
/*
int GetBytesFromSDLSoundBuffer(int len)
{
static unsigned int uiCatchedUpTimes = 0;
int i;
volatile Uint8 *p;
// if ( (unsigned long) len > SDLSoundBufferBytesHave)
// len = SDLSoundBufferBytesHave;
//p = SequentialSDLSoundBuffer;
p = GetSoundBufferPtr();
if (p == NULL) {printf("sound buffer is NULL\n"); return 0;}
for(i=0; i<len; i++){
*(p++) = SDLSoundBuffer[SDLSoundBufferOffset_OUT];
SDLSoundBuffer[SDLSoundBufferOffset_OUT] = 0;
SDLSoundBufferOffset_OUT++;
if (SDLSoundBufferOffset_OUT >= SOUND_BUFFER_SIZE)
SDLSoundBufferOffset_OUT = (unsigned long) 0;
}
SDLSoundBufferBytesHave-=len;
#ifdef GP2X
if (SDLSoundBufferBytesHave > REQUESTED_NUMBER_OF_SAMPLES * 2){
#else
if (SDLSoundBufferBytesHave > REQUESTED_NUMBER_OF_SAMPLES * 3){
#endif
i=CatchupSound();
fprintf(stderr, "Dumped %d sound samples, catched up %u times so far..\n"
, i // SDLSoundBufferBytesHave - ( REQUESTED_NUMBER_OF_SAMPLES * 2)
, ++uiCatchedUpTimes);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// i=0;
// while ( SDLSoundBufferBytesHave > REQUESTED_NUMBER_OF_SAMPLES) {
// SDLSoundBuffer[SDLSoundBufferOffset_OUT] = 0;
// SDLSoundBufferOffset_OUT++;
// if (SDLSoundBufferOffset_OUT >= SOUND_BUFFER_SIZE)
// SDLSoundBufferOffset_OUT = (unsigned long) 0;
//
// SDLSoundBufferBytesHave--;
// i++;
// }
// if (i>0) printf("Dumped %d sound samples.\n", i);
///////////////////////////////////////////////////////////////////////////////////////////////////
return len;
}
*/
int GetBytesFromSDLSoundBuffer(int len)
{
static unsigned int uiCatchedUpTimes = 0;
int i;
volatile Uint8 *p;
// if ( (unsigned long) len > SDLSoundBufferBytesHave)
// len = SDLSoundBufferBytesHave;
//p = SequentialSDLSoundBuffer;
p = GetSoundBufferPtr();
if (p == NULL) {printf("sound buffer is NULL\n"); return 0;}
for(i=0; i<len; i++){
*(p++) = SDLSoundBuffer[SDLSoundBufferOffset_OUT];
//SDLSoundBuffer[SDLSoundBufferOffset_OUT] = 0;
SDLSoundBufferOffset_OUT++;
if (SDLSoundBufferOffset_OUT >= SOUND_BUFFER_SIZE)
SDLSoundBufferOffset_OUT = (unsigned long) 0;
}
SDLSoundBufferBytesHave-=len;
// if (SDLSoundBufferBytesHave > REQUESTED_NUMBER_OF_SAMPLES * 5){
if (SDLSoundBufferBytesHave > REQUESTED_NUMBER_OF_SAMPLES * 4){
//fprintf(stderr, "Dumping %d sound samples, catched up %u times so far..\n"
// , SDLSoundBufferBytesHave - ( REQUESTED_NUMBER_OF_SAMPLES * 3), ++uiCatchedUpTimes);
CatchupSound();
}
return len;
}
unsigned long HowManyBytesLeftInSDLSoundBuffer(void)
{
return SDLSoundBufferBytesHave;
}
/*
void fill_audio(void *udata, Uint8 *stream, int len)
{
SDL_MixAudio(stream, SDLSoundBuffer , len, SDL_MIX_MAXVOLUME);
}
*/
/*
void fill_audio(void *udata, Uint8 *stream, int len)
{
volatile Uint8 *p;
void *tmp_udata;
tmp_udata = udata;
if (HowManyBytesLeftInSDLSoundBuffer() == 0)
return;
if ( HowManyBytesLeftInSDLSoundBuffer() < (unsigned int) len) { // (REQUESTED_NUMBER_OF_SAMPLES) ) {
printf("not enough data have %d want %d.\n", SDLSoundBufferBytesHave, len);
buffer_sound=0;
return;
}
// if (len > (int) HowManyBytesLeftInSDLSoundBuffer() ) {
// len = HowManyBytesLeftInSDLSoundBuffer();
// printf("not enough data.\n");
// return;
// }
p = GetSoundBufferPtr();
if (p == NULL) {printf("sound buffer is NULL\n"); return;}
len = GetBytesFromSDLSoundBuffer(len);
SDL_MixAudio(stream, (const Uint8*) p, len, SDL_MIX_MAXVOLUME);
}
*/
void fill_audio(void *udata, Uint8 *stream, int len)
{
volatile Uint8 *p;
void *tmp_udata;
tmp_udata = udata;
if (HowManyBytesLeftInSDLSoundBuffer() == 0)
return;
if ( HowManyBytesLeftInSDLSoundBuffer() < (unsigned int) len) { // (REQUESTED_NUMBER_OF_SAMPLES) ) {
//printf("not enough data have %d want %d.\n", SDLSoundBufferBytesHave, len);
buffer_sound=0;
return;
}
// if (len > (int) HowManyBytesLeftInSDLSoundBuffer() ) {
// len = HowManyBytesLeftInSDLSoundBuffer();
// printf("not enough data.\n");
// return;
// }
p = GetSoundBufferPtr();
if (p == NULL) {printf("sound buffer is NULL\n"); return;}
len = GetBytesFromSDLSoundBuffer(len);
SDL_MixAudio(stream, (const Uint8*) p, len, SDL_MIX_MAXVOLUME);
}
/* In main.cc
*/
void at_exit(void);
int InitializeSDL(int,char*[])
{
int flags, ret;
flags = SDL_INIT_VIDEO /* | SDL_INIT_AUDIO */ ;
#ifdef GP2X
flags |= SDL_INIT_JOYSTICK;
#endif
if (SDL_Init(flags) <0) {
fprintf(stderr, "Unable to initialise SDL: %s\n"
, SDL_GetError());
return 0;
}
/* atexit doesn't seem to work on the GP2X...
*/
#ifndef GP2X
atexit(at_exit);
#endif
/* Setup frame buffer:
*/
flags = SDL_HWSURFACE | SDL_HWPALETTE;
#ifndef GP2X
//if ( (frame_buffer_p=SDL_SetVideoMode(640, 480, 8, flags)) == NULL ) {
if ( (rgb_surface=SDL_SetVideoMode(320, 240, 16, flags)) == NULL ) {
#else
if ( (frame_buffer_p=SDL_SetVideoMode(320, 240, 8, flags)) == NULL ) {
#endif
fprintf(stderr, "Unable to set video mode: %s\n"
, SDL_GetError());
return 0;
}
frame_buffer_p = SDL_CreateRGBSurface(SDL_SWSURFACE,
320, 240, 8, 0, 0, 0, 0);
if (frame_buffer_p == NULL)
{
fprintf(stderr, "Unable to create surface: %s\n"
, SDL_GetError());
return 0;
}
#ifdef GP2X
SDL_ShowCursor(SDL_DISABLE);
#endif
#ifdef GP2X
if ( (joystick_p=SDL_JoystickOpen(0) ) == NULL) {
fprintf(stderr, "Couldn't open the joystick: %s\n"
, SDL_GetError());
return 0;
}
#endif
#ifndef GP2X
{
/* Fill screen with some boxes to denote boundries.
*/
SDL_FillRect(frame_buffer_p, NULL, SDL_MapRGB(frame_buffer_p->format
, 0xff, 0x00, 0x00));
SDL_Rect rect= {0, 0, 322, 258};
Uint32 color = SDL_MapRGB(frame_buffer_p->format, 0xff, 0xff, 0x00);
SDL_FillRect(frame_buffer_p, &rect, color);
SDL_UpdateRect(frame_buffer_p, 0, 0, frame_buffer_p->w
, frame_buffer_p->h);
rect= (SDL_Rect) {0, 0, 322, 242};
color = SDL_MapRGB(frame_buffer_p->format, 0x00, 0xff, 0xff);
SDL_FillRect(frame_buffer_p, &rect, color);
SDL_UpdateRect(frame_buffer_p, 0,0,frame_buffer_p->w,frame_buffer_p->h);
}
#endif
#ifdef USE_DMA
if(!init_dma())
return 0;
#endif
/* Successful:
*/
return 1;
}
int InitializeSDLSound(int soundfrequency)
{
SDL_AudioSpec *obtained_p;
samples = REQUESTED_NUMBER_OF_SAMPLES;
obtained_p = (SDL_AudioSpec*) malloc(sizeof(SDL_AudioSpec));
SDLSoundBufferOffset_IN = 0;
SDLSoundBufferOffset_OUT = 0;
SDLSoundBufferBytesHave = 0;
InitializeSoundBuffer();
#ifdef GP2X
GP2x_InitializeSoundBuffer();
#endif
// This seems to be a big in Paeryn's SDL: AUDIO_U8 wants twice as much
// data as it should. So I've just set BeebEm to calc. sound at 44.1Khz
// and SDL can play it at 22.05Khz.
#ifdef GP2X
wanted.freq = soundfrequency/2;
#else
wanted.freq = soundfrequency;
#endif
wanted.format = AUDIO_U8;
wanted.channels = 1;
wanted.samples = samples; //1024;
wanted.callback = fill_audio;
wanted.userdata = NULL;
if ( SDL_OpenAudio(&wanted, obtained_p) < 0 ) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
//== return(0);
}
printf("Sound:\n");
printf("freq=%d\n", (int) obtained_p->freq);
printf("format=%d\n", (int) obtained_p->format);
printf("channels=%d\n", (int) obtained_p->channels);
printf("silence=%d\n", (int) obtained_p->silence);
printf("samples=%d\n", (int) obtained_p->samples);
printf("size=%d\n------------\n\n", (int) obtained_p->samples);
// SDL_PauseAudio(0);
// SDL_Delay(250);
return(1);
}
void FreeSDLSound(void)
{
SDL_CloseAudio();
}
/* Sleeping:
*/
#define MIN_OS_WAIT_TIME 4
static void BusyWait(Uint32 u32TimeShouldWait, Uint32 u32StartTickCount)
{
Uint32 u32AjustedTime;
do{
u32AjustedTime = SDL_GetTicks();
/* Handle wrap around after ~47 days of continued execution
*/
if (u32AjustedTime < u32StartTickCount)
u32AjustedTime = u32AjustedTime
+ ( ((Uint32) 0xffffffff) - u32StartTickCount);
else
u32AjustedTime = u32AjustedTime - u32StartTickCount;
}while(u32AjustedTime < u32TimeShouldWait);
}
void SDL_Sleep(unsigned int ticks)
{
/* Do nothing if BeebEm asked to wait 0 ms.
*/
if (ticks<1) return;
// Only sleep if we are sure the OS can honnor it, busywait otherwise:
// if(ticks >= MIN_OS_WAIT_TIME){
//#ifndef GP2X
// SDL_Delay(ticks);
// }else{
//#else
BusyWait(ticks, SDL_GetTicks());
//#endif
// }
}
#ifdef GP2X
/* GP2X:
*/
#define SYS_CLK_FREQ 7372800
//MMSP2_16
/* Volume, a value between 0 and 99:
*/
int volume = 0;
/* OSS Mixer file:
*/
int oss_mixer = -1;
void OSS_Mixer_Init(void) {
oss_mixer=open("/dev/mixer", O_RDWR);
if (oss_mixer == -1) {
printf("Can't open mixer.\n");
}
}
void OSS_Mixer_Quit(void) {
if (oss_mixer != -1)
close(oss_mixer);
}
int OSS_Mixer_GetVolume(void) {
if (oss_mixer == -1)
return -1;
else
return volume;
}
void OSS_Mixer_SetVolume(int new_volume) {
static int L;
if (volume == new_volume) return;
if (oss_mixer != -1) {
L=(((new_volume*0x50)/100)<<8)|((new_volume*0x50)/100);
ioctl(oss_mixer, SOUND_MIXER_WRITE_PCM, &L);
volume = new_volume;
printf("Volume changed to %d.\n", new_volume);
config.settings.volume = new_volume;
}
}
void GP2x_Initialize(void)
{
if (gp2x_dev_mem == NULL) {
gp2x_dev_mem = open("/dev/mem", O_RDWR);
gp2x_memregl=(Uint32 *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev_mem, 0xc0000000);
gp2x_memregs=(Uint16*)gp2x_memregl;
}
}
void GP2x_FreeResources(void)
{
if (gp2x_dev_mem != NULL)
close(gp2x_dev_mem);
}
static void GP2x_InitializeSoundBuffer(void)
{
if (gp2x_sound_buffer_p == NULL) {
if (gp2x_dev_mem == NULL) GP2x_Initialize();
/* OK, now I'm writing over the secondary frame buffer.
*/
//gp2x_sound_buffer_p=(Uint8 *)mmap(0, 0x19000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev_mem, 0x02100000);
// Secondary frame buffer
gp2x_sound_buffer_p=(Uint8 *)mmap(0, 0x19000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev_mem, 0x03381000);
// Primary frame buffer
//gp2x_sound_buffer_p=(Uint8 *)mmap(0, 0x19000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev_mem, 0x03101000);
}
}
// Value will change at 25HZ, same as the Beebem simulated CRT.
unsigned long GP2X_timer(void)
{
if (gp2x_dev_mem == NULL) GP2x_Initialize();
return gp2x_memregl[0x0A00>>2]; // /( (SYS_CLK_FREQ-1000)/25);
}
void GP2x_SetCPUSpeed(unsigned int MHZ)
{
static unsigned int old_mhz = 0;
unsigned v;
unsigned mdiv,pdiv=3,scale=0;
if (MHZ<100) MHZ=100;
if (MHZ>260) MHZ=260;
if (MHZ == old_mhz) return;
if (gp2x_dev_mem == NULL) GP2x_Initialize();
if (gp2x_memregs != NULL) {
config.settings.speed = MHZ;
old_mhz = MHZ;
MHZ*=1000000;
mdiv=(MHZ*pdiv)/SYS_CLK_FREQ;
mdiv=((mdiv-8)<<8) & 0xff00;
pdiv=((pdiv-2)<<2) & 0xfc;
scale&=3;
v=mdiv | pdiv | scale;
gp2x_memregs[0x910>>1]=v;
} else {
printf("Unable to set GP2x 920T CPU speed - could not access MMSP2 registers.\n");
}
}
#endif