\r
#include "plat_gp2x.h"\r
#include "soc.h"\r
+#include "soc_pollux.h"\r
#include "../common/plat.h"\r
#include "../common/menu.h"\r
#include "../common/arm_utils.h"\r
if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
{\r
int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
- int target_fps = Pico.m.pal ? 50 : 60;\r
- int frame_samples, snd_excess_add;\r
int snd_rate_oss = PsndRate;\r
gp2x_soc_t soc;\r
\r
+ memset(sndBuffer, 0, sizeof(sndBuffer));\r
+ PsndOut = sndBuffer;\r
+ PicoWriteSound = updateSound;\r
+ plat_update_volume(0, 0);\r
+\r
+ printf("starting audio: %i len: %i stereo: %i, pal: %i\n",\r
+ PsndRate, PsndLen, is_stereo, Pico.m.pal);\r
+ sndout_oss_start(snd_rate_oss, is_stereo, 1);\r
+ sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
+\r
soc = soc_detect();\r
- if (soc == SOCID_POLLUX) {\r
- /* POLLUX pain: DPLL1 / mclk_div / bitclk_div / 4 */\r
- switch (PsndRate) {\r
- case 44100: PsndRate = 44171; break; // 44170.673077\r
- case 22050: PsndRate = 22086; break; // 22085.336538\r
- case 11025: PsndRate = 11043; break; // 11042.668269\r
- default: break;\r
- }\r
- }\r
+ if (soc == SOCID_POLLUX)\r
+ PsndRate = pollux_get_real_snd_rate(PsndRate);\r
\r
#define SOUND_RERATE_FLAGS (POPT_EN_FM|POPT_EN_PSG|POPT_EN_STEREO|POPT_EXT_FM|POPT_EN_MCD_CDDA)\r
if (PsndRate != PsndRate_old || Pico.m.pal != pal_old || ((PicoOpt & POPT_EXT_FM) && crashed_940) ||\r
PsndRerate(Pico.m.frame_count ? 1 : 0);\r
}\r
\r
- memset(sndBuffer, 0, sizeof(sndBuffer));\r
- PsndOut = sndBuffer;\r
- PicoWriteSound = updateSound;\r
PsndRate_old = PsndRate;\r
PicoOpt_old = PicoOpt;\r
pal_old = Pico.m.pal;\r
- plat_update_volume(0, 0);\r
-\r
- frame_samples = PsndLen;\r
- snd_excess_add = ((PsndRate - PsndLen * target_fps)<<16) / target_fps;\r
- if (snd_excess_add != 0)\r
- frame_samples++;\r
- if (soc == SOCID_POLLUX)\r
- frame_samples *= 2; /* force larger buffer */\r
-\r
- printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
- PsndRate, PsndLen, snd_excess_add, is_stereo, Pico.m.pal);\r
- sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
- sndout_oss_start(snd_rate_oss, frame_samples, is_stereo);\r
-\r
- /* Wiz's sound hardware needs more prebuffer */\r
- if (soc == SOCID_POLLUX)\r
- updateSound(frame_samples);\r
}\r
}\r
\r
+static const int sound_rates[] = { 44100, 32000, 22050, 16000, 11025, 8000 };\r
+\r
void pemu_sound_stop(void)\r
{\r
- /* get back from Wiz pain */\r
- switch (PsndRate) {\r
- case 44171: PsndRate = 44100; break;\r
- case 22086: PsndRate = 22050; break;\r
- case 11043: PsndRate = 11025; break;\r
- default: break;\r
+ int i;\r
+\r
+ /* get back from Pollux pain */\r
+ PsndRate += 1000;\r
+ for (i = 0; i < ARRAY_SIZE(sound_rates); i++) {\r
+ if (PsndRate >= sound_rates[i]) {\r
+ PsndRate = sound_rates[i];\r
+ break;\r
+ }\r
}\r
}\r
\r
{\r
int po_old = PicoOpt;\r
\r
- doing_bg_frame = 1;\r
PicoOpt &= ~POPT_ALT_RENDERER;\r
PicoOpt |= POPT_ACC_SPRITES;\r
if (!no_scale)\r
PicoDrawSetCallbacks(NULL, NULL);\r
Pico.m.dirtyPal = 1;\r
\r
+ doing_bg_frame = 1;\r
if (do_emu)\r
PicoFrame();\r
else\r
PicoFrameDrawOnly();\r
+ doing_bg_frame = 0;\r
\r
g_menubg_src_ptr = g_screen_ptr;\r
- doing_bg_frame = 0;\r
PicoOpt = po_old;\r
}\r
\r
static char cpuclk_was_changed = 0;
static unsigned short memtimex_old[2];
-static unsigned int pllsetreg0;
+static unsigned int pllsetreg0_old;
+static unsigned int timer_drift; // count per real second
static int last_pal_setting = 0;
#define TIMER_BASE3 0x1980
#define TIMER_REG(x) memregl[(TIMER_BASE3 + x) >> 2]
-unsigned int gp2x_get_ticks_us_(void)
+static unsigned int gp2x_get_ticks_us_(void)
{
TIMER_REG(0x08) = 0x4b; /* run timer, latch value */
return TIMER_REG(0);
}
-unsigned int gp2x_get_ticks_ms_(void)
+static unsigned int gp2x_get_ticks_ms_(void)
{
/* approximate /= 1000 */
unsigned long long v64;
return v;
}
+int pollux_get_real_snd_rate(int req_rate)
+{
+ int clk0_src, clk1_src, rate, div;
+
+ clk0_src = (memregl[0xdbc4>>2] >> 1) & 7;
+ clk1_src = (memregl[0xdbc8>>2] >> 1) & 7;
+ if (clk0_src > 1 || clk1_src != 7) {
+ fprintf(stderr, "get_real_snd_rate: bad clk sources: %d %d\n", clk0_src, clk1_src);
+ return req_rate;
+ }
+
+ rate = decode_pll(clk0_src ? memregl[0xf008>>2] : memregl[0xf004>>2]);
+
+ // apply divisors
+ div = ((memregl[0xdbc4>>2] >> 4) & 0x1f) + 1;
+ rate /= div;
+ div = ((memregl[0xdbc8>>2] >> 4) & 0x1f) + 1;
+ rate /= div;
+ rate /= 64;
+
+ //printf("rate %d\n", rate);
+ rate -= rate * timer_drift / 1000000;
+ printf("adjusted rate: %d\n", rate);
+
+ if (rate < 8000-1000 || rate > 44100+1000) {
+ fprintf(stderr, "get_real_snd_rate: got bad rate: %d\n", rate);
+ return req_rate;
+ }
+
+ return rate;
+}
+
void pollux_init(void)
{
struct fb_fix_screeninfo fbfix;
- int i, ret, rate;
+ int i, ret, rate, timer_div;
memdev = open("/dev/mem", O_RDWR);
if (memdev == -1) {
/* find what PLL1 runs at, for the timer */
rate = decode_pll(memregl[0xf008>>2]);
printf("PLL1 @ %dHz\n", rate);
- rate /= 1000000;
/* setup timer */
- if (1 <= rate && rate <= 256) {
+ timer_div = (rate + 500000) / 1000000;
+ if (1 <= timer_div && timer_div <= 256) {
+ timer_drift = (rate - (timer_div * 1000000)) / timer_div;
+
if (TIMER_REG(0x08) & 8) {
fprintf(stderr, "warning: timer in use, overriding!\n");
timer_cleanup();
}
- TIMER_REG(0x44) = ((rate - 1) << 4) | 2; /* using PLL1, divide by it's rate */
+ TIMER_REG(0x44) = ((timer_div - 1) << 4) | 2; /* using PLL1, divide by it's rate */
TIMER_REG(0x40) = 0x0c; /* clocks on */
TIMER_REG(0x08) = 0x6b; /* run timer, clear irq, latch value */
gp2x_get_ticks_us = plat_get_ticks_us_good;
}
- pllsetreg0 = memregl[0xf004>>2];
+ pllsetreg0_old = memregl[0xf004>>2];
memtimex_old[0] = memregs[0x14802>>1];
memtimex_old[1] = memregs[0x14804>>1];
gp2x_video_changemode_ll_(16);
unset_ram_timings_();
if (cpuclk_was_changed) {
- memregl[0xf004>>2] = pllsetreg0;
+ memregl[0xf004>>2] = pllsetreg0_old;
memregl[0xf07c>>2] |= 0x8000;
}
timer_cleanup();
--- /dev/null
+
+int pollux_get_real_snd_rate(int req_rate);
\r
if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
{\r
- int snd_excess_add, frame_samples;\r
int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
\r
PsndRerate(Pico.m.frame_count ? 1 : 0);\r
\r
- frame_samples = PsndLen;\r
- snd_excess_add = ((PsndRate - PsndLen * target_fps)<<16) / target_fps;\r
- if (snd_excess_add != 0)\r
- frame_samples++;\r
-\r
- printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
- PsndRate, PsndLen, snd_excess_add, is_stereo, Pico.m.pal);\r
- sndout_oss_start(PsndRate, frame_samples, is_stereo);\r
+ printf("starting audio: %i len: %i stereo: %i, pal: %i\n",\r
+ PsndRate, PsndLen, is_stereo, Pico.m.pal);\r
+ sndout_oss_start(PsndRate, is_stereo, 1);\r
sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
PicoWriteSound = updateSound;\r
plat_update_volume(0, 0);\r
static int sounddev = -1, mixerdev = -1;
static int can_write_safe;
+#define FRAG_COUNT 4
int sndout_oss_init(void)
{
sounddev = -1;
}
-int sndout_oss_start(int rate, int frame_samples, int stereo)
+int sndout_oss_start(int rate, int stereo, int frames_in_frag)
{
- static int s_oldrate = 0, s_old_fsamples = 0, s_oldstereo = 0;
+ static int s_oldrate = 0, s_oldstereo = 0;
int frag, bsize, bits, ret;
// GP2X: if no settings change, we don't need to do anything,
// since audio is never stopped there
- if (sounddev >= 0 && rate == s_oldrate && s_old_fsamples == frame_samples && s_oldstereo == stereo)
+ if (sounddev >= 0 && rate == s_oldrate && s_oldstereo == stereo)
return 0;
sndout_oss_stop();
}
}
- // calculate buffer size. We want to fit 1 frame worth of sound data.
- // Also ignore mono because both GP2X and Wiz mixes mono to stereo anyway.
- bsize = frame_samples << 2;
+ // try to fit frames_in_frag frames worth of data in fragment
+ // ignore mono because it's unlikely to be used and
+ // both GP2X and Wiz mixes mono to stereo anyway.
+ bsize = (frames_in_frag * rate / 50) * 4;
for (frag = 0; bsize; bsize >>= 1, frag++)
;
- frag |= 16 << 16; // fragment count
+ frag |= FRAG_COUNT << 16; // fragment count
ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
if (ret < 0)
perror("SNDCTL_DSP_SETFRAGMENT failed");
printf("sndout_oss_start: %d/%dbit/%s, %d buffers of %i bytes\n",
rate, bits, stereo ? "stereo" : "mono", frag >> 16, 1 << (frag & 0xffff));
- s_oldrate = rate; s_old_fsamples = frame_samples; s_oldstereo = stereo;
+ s_oldrate = rate; s_oldstereo = stereo;
can_write_safe = 0;
return 0;
}
if (ret < 0)
return 1;
- // have enough bytes to write + 4 extra frags
- return bi.bytes - bi.fragsize * 4 >= bytes ? 1 : 0;
+ // have enough bytes to write + 1 frag
+ return bi.bytes - bi.fragsize >= bytes ? 1 : 0;
}
void sndout_oss_sync(void)
int sndout_oss_init(void);
-int sndout_oss_start(int rate, int frame_samples, int stereo);
+int sndout_oss_start(int rate, int stereo, int frames_in_frag);
void sndout_oss_stop(void);
int sndout_oss_write(const void *buff, int len);
int sndout_oss_can_write(int bytes);