diff --git a/A2Mac.xcodeproj/project.pbxproj b/A2Mac.xcodeproj/project.pbxproj index 31857ac..2b16306 100644 --- a/A2Mac.xcodeproj/project.pbxproj +++ b/A2Mac.xcodeproj/project.pbxproj @@ -427,8 +427,8 @@ 32A9F7472467B5DF004902A1 /* audio */ = { isa = PBXGroup; children = ( - 32A9F7492467B60B004902A1 /* speaker.c */, 32A9F7482467B60B004902A1 /* speaker.h */, + 32A9F7492467B60B004902A1 /* speaker.c */, ); path = audio; sourceTree = ""; diff --git a/src/cpu/6502.c b/src/cpu/6502.c index 47c17fe..00d6cbf 100644 --- a/src/cpu/6502.c +++ b/src/cpu/6502.c @@ -713,10 +713,6 @@ void softReset() { void m6502_Run() { - // clear speaker buffer, so we can fill it up by new data - memset(spkr_samples, 127, spkr_buf_size); - - // init time //#ifdef CLK_WAIT // unsigned long long elpased = (unsigned long long)-1LL; @@ -778,7 +774,11 @@ void m6502_Run() { } spkr_play(); + // clear speaker buffer, so we can fill it up by new data + memset(spkr_samples, spkr_level, spkr_buf_size); + + } void read_rom( const char * bundlePath, const char * filename, uint8_t * rom, const uint16_t addr ) { diff --git a/src/dev/audio/speaker.c b/src/dev/audio/speaker.c index a78b434..51378b5 100644 --- a/src/dev/audio/speaker.c +++ b/src/dev/audio/speaker.c @@ -48,6 +48,10 @@ ALCcontext *ctx = NULL; ALuint spkr_buf = 0; ALuint spkr_src = 0; +// we start with the max, because otherwise the speaker clicks +uint8_t spkr_level = SPKR_LEVEL_MAX; + + const int spkr_seconds = 1; const unsigned spkr_sample_rate = 44100; const unsigned spkr_buf_size = spkr_seconds * spkr_sample_rate; @@ -64,7 +68,7 @@ void spkr_init() { alcMakeContextCurrent(ctx); // Fill buffer with zeros - memset( spkr_samples, 127, spkr_buf_size ); + memset( spkr_samples, spkr_level, spkr_buf_size ); } @@ -112,4 +116,6 @@ void spkr_play() { alSourcei(spkr_src, AL_LOOPING, 0); alSourcePlay(spkr_src); + + memset(spkr_samples, spkr_level, spkr_buf_size); } diff --git a/src/dev/audio/speaker.h b/src/dev/audio/speaker.h index c7ef43a..62e82ca 100644 --- a/src/dev/audio/speaker.h +++ b/src/dev/audio/speaker.h @@ -11,6 +11,8 @@ #include +#define SPKR_LEVEL_MIN 0 +#define SPKR_LEVEL_MAX 127 extern void spkr_init(void); extern void spkr_exit(void); @@ -19,5 +21,8 @@ extern void spkr_play(void); extern const unsigned spkr_buf_size; extern char spkr_samples []; +extern uint8_t spkr_level; + + #endif /* speaker_h */ diff --git a/src/dev/mem/mmio.h b/src/dev/mem/mmio.h index b4fab5e..1a8ee0b 100644 --- a/src/dev/mem/mmio.h +++ b/src/dev/mem/mmio.h @@ -48,7 +48,6 @@ uint8_t * const RDHIMEM = Apple2_64K_MEM; // Pointer to the Shadow Memory uint8_t * const WRHIMEM = Apple2_Dummy_RAM; // Pointer to the Shadow Memory Map so we can use this from Swift - #define DEF_RAM_PAGE(mem,pg) \ (mem) + ((pg) << 8) @@ -401,6 +400,35 @@ void auxMemorySelect() { } +INLINE void spkr_switch() { + // TODO: This is very slow! + // printf("io_KBDSTRB\n"); + + // push a click into the speaker buffer + // (we will play the entire buffer at the end of the frame) + int sample_idx = clkfrm / 22; + if ( spkr_level > SPKR_LEVEL_MIN ) { + spkr_level = SPKR_LEVEL_MIN; + } + else { + spkr_level = SPKR_LEVEL_MAX; + } + //spkr_samples[sample_idx] = spkr_level; + memset(spkr_samples + sample_idx, spkr_level, spkr_buf_size - sample_idx); + + //ViewController_spk_up_play(); + + + // case io_VID_CLR80VID: + // videoMode.col80 = 0; + // break; + // + // case io_VID_SET80VID: + // videoMode.col80 = 1; + // break; + // +} + INLINE uint8_t ioRead( uint16_t addr ) { // if (outdev) fprintf(outdev, "ioRead:%04X\n", addr); @@ -419,28 +447,10 @@ INLINE uint8_t ioRead( uint16_t addr ) { Apple2_64K_RAM[io_KBD] &= 0x7F; return Apple2_64K_RAM[io_KBDSTRB]; - case (uint8_t)io_SPKR: { - // TODO: This is very slow! -// printf("io_KBDSTRB\n"); - - // push a click into the speaker buffer - // (we will play the entire buffer at the end of the frame) - int sample_idx = clkfrm / 22; - spkr_samples[sample_idx] = 0; - - //ViewController_spk_up_play(); - + case (uint8_t)io_SPKR: + spkr_switch(); return Apple2_64K_RAM[io_SPKR]; -// case io_VID_CLR80VID: -// videoMode.col80 = 0; -// break; -// -// case io_VID_SET80VID: -// videoMode.col80 = 1; -// break; -// - } case (uint8_t)io_VID_RDTEXT: return videoMode.text << 7; @@ -699,6 +709,10 @@ INLINE void ioWrite( uint16_t addr, uint8_t val ) { Apple2_64K_RAM[io_KBD] &= 0x7F; break; + case io_SPKR: + spkr_switch(); + break; + case io_RDMAINRAM: // printf("io_RDMAINRAM\n"); MEMcfg.RD_AUX_MEM = 0;