From 230efab504263a5ce84170ce51eca7767f5af0cd Mon Sep 17 00:00:00 2001 From: Bakisha Date: Sat, 8 Aug 2020 11:51:09 +0200 Subject: [PATCH] Autoconfigure RAM for emulator Autoconfigure RAM for emulator based on MCU's free memory --- .../02_ram.ino | 16 +- .../03_6502.ino | 34 +- .../30_tuneChecks.ino | 2 +- .../40_IRQ.ino | 1404 ++++++++--------- .../50_autoconfig.ino | 2 +- .../80_buttons.ino | 2 +- .../90_debug.ino | 8 +- .../STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1.ino | 21 +- .../xx_RAM.h | 17 +- .../z_SetupLoop.ino | 4 +- .../02_ram.ino | 10 + .../10_SD.ino | 2 +- .../40_IRQ.ino | 18 +- .../50_autoconfig.ino | 28 +- .../60_sounds.ino | 2 +- .../90_debug.ino | 8 - .../STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3.ino | 15 +- .../xx_RAM.h | 11 +- .../z_SetupLoop.ino | 1 + 19 files changed, 811 insertions(+), 794 deletions(-) diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/02_ram.ino b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/02_ram.ino index e5c293d..033987c 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/02_ram.ino +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/02_ram.ino @@ -1,12 +1,22 @@ -#if defined(SPI_RAM) -// TODO -#elif defined(PARALLEL_SRAM) +#if defined(SPI_RAM) +// TODO +#elif defined(PARALLEL_SRAM) // TODO #else // internal RAM +void AllocateRAM() { + if ((FreeBytes()) > 0xffff ) { + RAM_SIZE = 0xffff; + } + else { + RAM_SIZE = FreeBytes() - 2048; + } + RAM = (uint8_t*) calloc(RAM_SIZE, sizeof(uint8_t)); // allocate memory +} + inline void POKE (uint16_t addr , uint8_t bytE ) { RAM[addr] = bytE; diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/03_6502.ino b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/03_6502.ino index 83eca6d..8546961 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/03_6502.ino +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/03_6502.ino @@ -14,7 +14,7 @@ inline uint8_t read6502(uint16_t address) { JSR1003 = 1; // if it's loading <20-03-10> (example: JSR $1003) value from sid_play routine, it is time for fake VIC-II irq signal } if ((address >= 0xD400) && (address < 0xD420)) { - + STAD4XX = 1; // sid read return_value = SID[address - 0xD400]; // TODO: make it unreadable // SID } @@ -39,19 +39,19 @@ inline uint8_t read6502(uint16_t address) { if ( address < (0x400) ) return_value = PEEK (address) ; // zero page, stack, player, screen RAM -//CIA timer + //CIA timer if ( address == 0xdc04 ) { - return_value = CIA_DC04; -} -if ( address == 0xdc05 ) { - return_value = CIA_DC05; -} + return_value = CIA_DC04; + } + if ( address == 0xdc05 ) { + return_value = CIA_DC05; + } -return return_value; + return return_value; } @@ -292,16 +292,16 @@ inline void write6502(uint16_t address, uint8_t value) { } // LOW_RAM - //CIA timer + //CIA timer if ( address == 0xdc04 ) { - CIA_DC04=value; -} -if ( address == 0xdc05 ) { - CIA_DC05=value; -} -if (CIA_DC05>0) { // set song speed only when Hi value of CIA timer is greater then 0 - set_tune_speed (); -} + CIA_DC04 = value; + } + if ( address == 0xdc05 ) { + CIA_DC05 = value; + } + if (CIA_DC05 > 0) { // set song speed only when Hi value of CIA timer is greater then 0 + set_tune_speed (); + } } diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/30_tuneChecks.ino b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/30_tuneChecks.ino index 8ff7179..6cd01c3 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/30_tuneChecks.ino +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/30_tuneChecks.ino @@ -93,7 +93,7 @@ inline bool Compatibility_check() { if (SID_load_start == 0) { SID_load_start = PEEK (0x7c + 0x380) + (PEEK (0x7d + 0x380) * 256); } - if ( SID_load_start >= 0x07E8) { + if ( SID_load_start >= 0x0400) { // >= 0x07E8) { LOAD_ADDRESS = SID_load_start; } diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/40_IRQ.ino b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/40_IRQ.ino index 23e59a6..c44948f 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/40_IRQ.ino +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/40_IRQ.ino @@ -1,807 +1,807 @@ void irq_handler(void) { // - SetAUDIO(); // in 20_hardware.ino + SetAUDIO(); // in 20_hardware.ino - /* - skip_counter--; // 1Mhz to 985250HZ difference. Is it needed? - if (skip_counter == 0) { - skip_counter = skip_counter_max; - return; - } - */ - - VIC_irq = VIC_irq + multiplier; - if (VIC_irq >= SID_speed) { - VIC_irq_request = 1; - VIC_irq = 0; + /* + skip_counter--; // 1Mhz to 985250HZ difference. Is it needed? + if (skip_counter == 0) { + skip_counter = skip_counter_max; + return; } + */ + VIC_irq = VIC_irq + multiplier; + if (VIC_irq >= SID_speed) { + VIC_irq_request = 1; + VIC_irq = 0; + } - tune_play_counter = tune_play_counter + multiplier; - if (tune_play_counter >= tune_end_counter) { - play_next_tune = true; - } - SID_emulator(); + tune_play_counter = tune_play_counter + multiplier; + if (tune_play_counter >= tune_end_counter) { + play_next_tune = true; + } + SID_emulator(); - STAD4XX = 0; - } - inline void SID_emulator() { + STAD4XX = 0; +} - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // - // Magic - // - /////////////////////////////////// +inline void SID_emulator() { + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // + // Magic + // + /////////////////////////////////// - OSC_MSB_Previous_1 = OSC_MSB_1; - OSC_MSB_Previous_2 = OSC_MSB_2; - OSC_MSB_Previous_3 = OSC_MSB_3; + OSC_MSB_Previous_1 = OSC_MSB_1; + OSC_MSB_Previous_2 = OSC_MSB_2; + OSC_MSB_Previous_3 = OSC_MSB_3; - OSC_1 = ((!test_bit_voice_1) & 1) * ((OSC_1 + ( multiplier * OSC_1_HiLo)) ) & 0xffffff; - OSC_2 = ((!test_bit_voice_2) & 1) * ((OSC_2 + ( multiplier * OSC_2_HiLo)) ) & 0xffffff; - OSC_3 = ((!test_bit_voice_3) & 1) * ((OSC_3 + ( multiplier * OSC_3_HiLo)) ) & 0xffffff; + OSC_1 = ((!test_bit_voice_1) & 1) * ((OSC_1 + ( multiplier * OSC_1_HiLo)) ) & 0xffffff; + OSC_2 = ((!test_bit_voice_2) & 1) * ((OSC_2 + ( multiplier * OSC_2_HiLo)) ) & 0xffffff; + OSC_3 = ((!test_bit_voice_3) & 1) * ((OSC_3 + ( multiplier * OSC_3_HiLo)) ) & 0xffffff; - // noise_1 - OSC_noise_1 = OSC_noise_1 + multiplier * OSC_1_HiLo; - OSC_bit19_1 = OSC_noise_1 >> 19 ; - for (i = 0; i < OSC_bit19_1; i++) { - bit_0_1 = (( bitRead(pseudorandom_1, 22) ) ^ ((bitRead(pseudorandom_1, 17 ) ) ) ) & 0x1; - pseudorandom_1 = pseudorandom_1 << 1; - //pseudorandom_1 = pseudorandom_1 & 0x7fffff; - pseudorandom_1 = bit_0_1 | pseudorandom_1; - } - OSC_noise_1 = OSC_noise_1 - (OSC_bit19_1 << 19); + // noise_1 + OSC_noise_1 = OSC_noise_1 + multiplier * OSC_1_HiLo; + OSC_bit19_1 = OSC_noise_1 >> 19 ; + for (i = 0; i < OSC_bit19_1; i++) { + bit_0_1 = (( bitRead(pseudorandom_1, 22) ) ^ ((bitRead(pseudorandom_1, 17 ) ) ) ) & 0x1; + pseudorandom_1 = pseudorandom_1 << 1; + //pseudorandom_1 = pseudorandom_1 & 0x7fffff; + pseudorandom_1 = bit_0_1 | pseudorandom_1; + } + OSC_noise_1 = OSC_noise_1 - (OSC_bit19_1 << 19); - // noise_2 - OSC_noise_2 = OSC_noise_2 + multiplier * OSC_2_HiLo; - OSC_bit19_2 = OSC_noise_2 >> 19 ; - for (i = 0; i < OSC_bit19_2; i++) { - bit_0_2 = (( bitRead(pseudorandom_2, 22) ) ^ ((bitRead(pseudorandom_2, 17 ) ) ) ) & 0x1; - pseudorandom_2 = pseudorandom_2 << 1; - //pseudorandom_2 = pseudorandom_2 & 0x7fffff; - pseudorandom_2 = bit_0_2 | pseudorandom_2; - } - OSC_noise_2 = OSC_noise_2 - (OSC_bit19_2 << 19) ; - - // noise_3 - OSC_noise_3 = OSC_noise_3 + multiplier * OSC_3_HiLo; - OSC_bit19_3 = OSC_noise_3 >> 19 ; - for (i = 0; i < OSC_bit19_3; i++) { - bit_0_3 = (( bitRead(pseudorandom_3, 22) ) ^ ((bitRead(pseudorandom_3, 17 ) ) ) ) & 0x1; - pseudorandom_3 = pseudorandom_3 << 1; - //pseudorandom_3 = pseudorandom_3 & 0x7fffff; - pseudorandom_3 = bit_0_3 | pseudorandom_3; - } - OSC_noise_3 = OSC_noise_3 - (OSC_bit19_3 << 19 ); - - if (OSC_1 >= 0x800000) OSC_MSB_1 = 1; else OSC_MSB_1 = 0; - if ( (!OSC_MSB_Previous_1) & (OSC_MSB_1)) MSB_Rising_1 = 1; else MSB_Rising_1 = 0; - - if (OSC_2 >= 0x800000) OSC_MSB_2 = 1; else OSC_MSB_2 = 0; - if ( (!OSC_MSB_Previous_2) & (OSC_MSB_2)) MSB_Rising_2 = 1; else MSB_Rising_2 = 0; - - if (OSC_3 >= 0x800000) OSC_MSB_3 = 1; else OSC_MSB_3 = 0; - if ( (!OSC_MSB_Previous_3) & (OSC_MSB_3)) MSB_Rising_3 = 1; else MSB_Rising_3 = 0; - - - - if (SYNC_bit_voice_1 & MSB_Rising_3) OSC_1 = OSC_1 & 0x7fffff; - if (SYNC_bit_voice_2 & MSB_Rising_1) OSC_2 = OSC_2 & 0x7fffff; - if (SYNC_bit_voice_3 & MSB_Rising_2) OSC_3 = OSC_3 & 0x7fffff; - - if ( (triangle_bit_voice_1) & (ring_bit_voice_1) ) OSC_MSB_1 = OSC_MSB_1 ^ OSC_MSB_3; - if ( (triangle_bit_voice_2) & (ring_bit_voice_2) ) OSC_MSB_2 = OSC_MSB_2 ^ OSC_MSB_1; - if ( (triangle_bit_voice_3) & (ring_bit_voice_3) ) OSC_MSB_3 = OSC_MSB_3 ^ OSC_MSB_2; - - waveform_switch_1 = (noise_bit_voice_1 << 3) | (pulse_bit_voice_1 << 2) | (sawtooth_bit_voice_1 << 1) | (triangle_bit_voice_1); - waveform_switch_2 = (noise_bit_voice_2 << 3) | (pulse_bit_voice_2 << 2) | (sawtooth_bit_voice_2 << 1) | (triangle_bit_voice_2); - waveform_switch_3 = (noise_bit_voice_3 << 3) | (pulse_bit_voice_3 << 2) | (sawtooth_bit_voice_3 << 1) | (triangle_bit_voice_3); - - temp11 = (OSC_1 >> 12); - - switch (waveform_switch_1) { - case 0: - WaveformDA_1 = 0; - break; - case 1: - WaveformDA_triangle_1 = (( (OSC_MSB_1 * B2047) ^ (temp11 & B2047)) << 1) ; - WaveformDA_1 = WaveformDA_triangle_1; - break; - case 2: - WaveformDA_sawtooth_1 = temp11; - WaveformDA_1 = WaveformDA_sawtooth_1; - break; - case 3: - WaveformDA_triangle_1 = (( (OSC_MSB_1 * B2047) ^ (temp11 & B2047)) << 1) ; - WaveformDA_sawtooth_1 = temp11; - WaveformDA_1 = AND_mask[(WaveformDA_triangle_1 & WaveformDA_sawtooth_1)] << 4; - break; - case 4: - if (temp11 >= PW_HiLo_voice_1 ) WaveformDA_pulse_1 = B4095; else WaveformDA_pulse_1 = 0; - WaveformDA_1 = WaveformDA_pulse_1; - break; - case 5: - WaveformDA_triangle_1 = (( (OSC_MSB_1 * B2047) ^ (temp11 & B2047)) << 1) ; - if (temp11 >= PW_HiLo_voice_1 ) WaveformDA_pulse_1 = B4095; else WaveformDA_pulse_1 = 0; - WaveformDA_1 = AND_mask[WaveformDA_triangle_1 & WaveformDA_pulse_1] << 4; - break; - case 6: - WaveformDA_sawtooth_1 = temp11; // same as upper 12 bits of OSC - if (temp11 >= PW_HiLo_voice_1 ) WaveformDA_pulse_1 = B4095; else WaveformDA_pulse_1 = 0; - WaveformDA_1 = AND_mask[WaveformDA_sawtooth_1 & WaveformDA_pulse_1] << 4; - break; - case 7: - WaveformDA_triangle_1 = (( (OSC_MSB_1 * B2047) ^ (temp11 & B2047)) << 1) ; - WaveformDA_sawtooth_1 = temp11; - if (temp11 >= PW_HiLo_voice_1 ) WaveformDA_pulse_1 = B4095; else WaveformDA_pulse_1 = 0; - WaveformDA_1 = AND_mask[WaveformDA_pulse_1 & WaveformDA_sawtooth_1 & WaveformDA_triangle_1] << 4; - break; - case 8: - WaveformDA_noise_1 = B4095 & (pseudorandom_1 >> 11); - - WaveformDA_1 = WaveformDA_noise_1; - break; - case 9: - WaveformDA_1 = 0; - break; - case 10: - WaveformDA_1 = 0; - break; - case 11: - WaveformDA_1 = 0; - break; - case 12: - WaveformDA_1 = 0; - break; - case 13: - WaveformDA_1 = 0; - break; - case 14: - WaveformDA_1 = 0; - break; - case 15: - WaveformDA_1 = 0; - break; - } + // noise_2 + OSC_noise_2 = OSC_noise_2 + multiplier * OSC_2_HiLo; + OSC_bit19_2 = OSC_noise_2 >> 19 ; + for (i = 0; i < OSC_bit19_2; i++) { + bit_0_2 = (( bitRead(pseudorandom_2, 22) ) ^ ((bitRead(pseudorandom_2, 17 ) ) ) ) & 0x1; + pseudorandom_2 = pseudorandom_2 << 1; + //pseudorandom_2 = pseudorandom_2 & 0x7fffff; + pseudorandom_2 = bit_0_2 | pseudorandom_2; + } + OSC_noise_2 = OSC_noise_2 - (OSC_bit19_2 << 19) ; + + // noise_3 + OSC_noise_3 = OSC_noise_3 + multiplier * OSC_3_HiLo; + OSC_bit19_3 = OSC_noise_3 >> 19 ; + for (i = 0; i < OSC_bit19_3; i++) { + bit_0_3 = (( bitRead(pseudorandom_3, 22) ) ^ ((bitRead(pseudorandom_3, 17 ) ) ) ) & 0x1; + pseudorandom_3 = pseudorandom_3 << 1; + //pseudorandom_3 = pseudorandom_3 & 0x7fffff; + pseudorandom_3 = bit_0_3 | pseudorandom_3; + } + OSC_noise_3 = OSC_noise_3 - (OSC_bit19_3 << 19 ); + + if (OSC_1 >= 0x800000) OSC_MSB_1 = 1; else OSC_MSB_1 = 0; + if ( (!OSC_MSB_Previous_1) & (OSC_MSB_1)) MSB_Rising_1 = 1; else MSB_Rising_1 = 0; + + if (OSC_2 >= 0x800000) OSC_MSB_2 = 1; else OSC_MSB_2 = 0; + if ( (!OSC_MSB_Previous_2) & (OSC_MSB_2)) MSB_Rising_2 = 1; else MSB_Rising_2 = 0; + + if (OSC_3 >= 0x800000) OSC_MSB_3 = 1; else OSC_MSB_3 = 0; + if ( (!OSC_MSB_Previous_3) & (OSC_MSB_3)) MSB_Rising_3 = 1; else MSB_Rising_3 = 0; + + + + if (SYNC_bit_voice_1 & MSB_Rising_3) OSC_1 = OSC_1 & 0x7fffff; + if (SYNC_bit_voice_2 & MSB_Rising_1) OSC_2 = OSC_2 & 0x7fffff; + if (SYNC_bit_voice_3 & MSB_Rising_2) OSC_3 = OSC_3 & 0x7fffff; + + if ( (triangle_bit_voice_1) & (ring_bit_voice_1) ) OSC_MSB_1 = OSC_MSB_1 ^ OSC_MSB_3; + if ( (triangle_bit_voice_2) & (ring_bit_voice_2) ) OSC_MSB_2 = OSC_MSB_2 ^ OSC_MSB_1; + if ( (triangle_bit_voice_3) & (ring_bit_voice_3) ) OSC_MSB_3 = OSC_MSB_3 ^ OSC_MSB_2; + + waveform_switch_1 = (noise_bit_voice_1 << 3) | (pulse_bit_voice_1 << 2) | (sawtooth_bit_voice_1 << 1) | (triangle_bit_voice_1); + waveform_switch_2 = (noise_bit_voice_2 << 3) | (pulse_bit_voice_2 << 2) | (sawtooth_bit_voice_2 << 1) | (triangle_bit_voice_2); + waveform_switch_3 = (noise_bit_voice_3 << 3) | (pulse_bit_voice_3 << 2) | (sawtooth_bit_voice_3 << 1) | (triangle_bit_voice_3); + + temp11 = (OSC_1 >> 12); + + switch (waveform_switch_1) { + case 0: + WaveformDA_1 = 0; + break; + case 1: + WaveformDA_triangle_1 = (( (OSC_MSB_1 * B2047) ^ (temp11 & B2047)) << 1) ; + WaveformDA_1 = WaveformDA_triangle_1; + break; + case 2: + WaveformDA_sawtooth_1 = temp11; + WaveformDA_1 = WaveformDA_sawtooth_1; + break; + case 3: + WaveformDA_triangle_1 = (( (OSC_MSB_1 * B2047) ^ (temp11 & B2047)) << 1) ; + WaveformDA_sawtooth_1 = temp11; + WaveformDA_1 = AND_mask[(WaveformDA_triangle_1 & WaveformDA_sawtooth_1)] << 4; + break; + case 4: + if (temp11 >= PW_HiLo_voice_1 ) WaveformDA_pulse_1 = B4095; else WaveformDA_pulse_1 = 0; + WaveformDA_1 = WaveformDA_pulse_1; + break; + case 5: + WaveformDA_triangle_1 = (( (OSC_MSB_1 * B2047) ^ (temp11 & B2047)) << 1) ; + if (temp11 >= PW_HiLo_voice_1 ) WaveformDA_pulse_1 = B4095; else WaveformDA_pulse_1 = 0; + WaveformDA_1 = AND_mask[WaveformDA_triangle_1 & WaveformDA_pulse_1] << 4; + break; + case 6: + WaveformDA_sawtooth_1 = temp11; // same as upper 12 bits of OSC + if (temp11 >= PW_HiLo_voice_1 ) WaveformDA_pulse_1 = B4095; else WaveformDA_pulse_1 = 0; + WaveformDA_1 = AND_mask[WaveformDA_sawtooth_1 & WaveformDA_pulse_1] << 4; + break; + case 7: + WaveformDA_triangle_1 = (( (OSC_MSB_1 * B2047) ^ (temp11 & B2047)) << 1) ; + WaveformDA_sawtooth_1 = temp11; + if (temp11 >= PW_HiLo_voice_1 ) WaveformDA_pulse_1 = B4095; else WaveformDA_pulse_1 = 0; + WaveformDA_1 = AND_mask[WaveformDA_pulse_1 & WaveformDA_sawtooth_1 & WaveformDA_triangle_1] << 4; + break; + case 8: + WaveformDA_noise_1 = B4095 & (pseudorandom_1 >> 11); + + WaveformDA_1 = WaveformDA_noise_1; + break; + case 9: + WaveformDA_1 = 0; + break; + case 10: + WaveformDA_1 = 0; + break; + case 11: + WaveformDA_1 = 0; + break; + case 12: + WaveformDA_1 = 0; + break; + case 13: + WaveformDA_1 = 0; + break; + case 14: + WaveformDA_1 = 0; + break; + case 15: + WaveformDA_1 = 0; + break; + + } - // voice 2 - - temp12 = (OSC_2 >> 12); - - switch (waveform_switch_2) { - case 0: - WaveformDA_2 = 0; - break; - case 1: - WaveformDA_triangle_2 = (( (OSC_MSB_2 * B2047) ^ (temp12 & B2047)) << 1) ; - WaveformDA_2 = WaveformDA_triangle_2; - break; - case 2: - WaveformDA_sawtooth_2 = temp12; - WaveformDA_2 = WaveformDA_sawtooth_2; - break; - case 3: - WaveformDA_triangle_2 = (( (OSC_MSB_2 * B2047) ^ (temp12 & B2047)) << 1) ; - WaveformDA_sawtooth_2 = temp12; - WaveformDA_2 = AND_mask[(WaveformDA_triangle_2 & WaveformDA_sawtooth_2)] << 4; - break; - case 4: - if (temp12 >= PW_HiLo_voice_2 ) WaveformDA_pulse_2 = B4095; else WaveformDA_pulse_2 = 0; - WaveformDA_2 = WaveformDA_pulse_2; - break; - case 5: - WaveformDA_triangle_2 = (( (OSC_MSB_2 * B2047) ^ (temp12 & B2047)) << 1) ; - if (temp12 >= PW_HiLo_voice_2 ) WaveformDA_pulse_2 = B4095; else WaveformDA_pulse_2 = 0; - WaveformDA_2 = AND_mask[WaveformDA_triangle_2 & WaveformDA_pulse_2] << 4; - break; - case 6: - WaveformDA_sawtooth_2 = temp12; - if (temp12 >= PW_HiLo_voice_2 ) WaveformDA_pulse_2 = B4095; else WaveformDA_pulse_2 = 0; - WaveformDA_2 = AND_mask[WaveformDA_sawtooth_2 & WaveformDA_pulse_2] << 4; - break; - case 7: - WaveformDA_triangle_2 = (( (OSC_MSB_2 * B2047) ^ (temp12 & B2047)) << 1) ; - WaveformDA_sawtooth_2 = temp12; - if (temp12 >= PW_HiLo_voice_2 ) WaveformDA_pulse_2 = B4095; else WaveformDA_pulse_2 = 0; - WaveformDA_2 = AND_mask[WaveformDA_pulse_2 & WaveformDA_sawtooth_2 & WaveformDA_triangle_2] << 4; - break; - case 8: - WaveformDA_noise_2 = B4095 & (pseudorandom_2 >> 11); - WaveformDA_2 = WaveformDA_noise_2; - break; - case 9: - WaveformDA_2 = 0; - break; - case 10: - WaveformDA_2 = 0; - break; - case 11: - WaveformDA_2 = 0; - break; - case 12: - WaveformDA_2 = 0; - break; - case 13: - WaveformDA_2 = 0; - break; - case 14: - WaveformDA_2 = 0; - break; - case 15: - WaveformDA_2 = 0; - break; + // voice 2 + + temp12 = (OSC_2 >> 12); + + switch (waveform_switch_2) { + case 0: + WaveformDA_2 = 0; + break; + case 1: + WaveformDA_triangle_2 = (( (OSC_MSB_2 * B2047) ^ (temp12 & B2047)) << 1) ; + WaveformDA_2 = WaveformDA_triangle_2; + break; + case 2: + WaveformDA_sawtooth_2 = temp12; + WaveformDA_2 = WaveformDA_sawtooth_2; + break; + case 3: + WaveformDA_triangle_2 = (( (OSC_MSB_2 * B2047) ^ (temp12 & B2047)) << 1) ; + WaveformDA_sawtooth_2 = temp12; + WaveformDA_2 = AND_mask[(WaveformDA_triangle_2 & WaveformDA_sawtooth_2)] << 4; + break; + case 4: + if (temp12 >= PW_HiLo_voice_2 ) WaveformDA_pulse_2 = B4095; else WaveformDA_pulse_2 = 0; + WaveformDA_2 = WaveformDA_pulse_2; + break; + case 5: + WaveformDA_triangle_2 = (( (OSC_MSB_2 * B2047) ^ (temp12 & B2047)) << 1) ; + if (temp12 >= PW_HiLo_voice_2 ) WaveformDA_pulse_2 = B4095; else WaveformDA_pulse_2 = 0; + WaveformDA_2 = AND_mask[WaveformDA_triangle_2 & WaveformDA_pulse_2] << 4; + break; + case 6: + WaveformDA_sawtooth_2 = temp12; + if (temp12 >= PW_HiLo_voice_2 ) WaveformDA_pulse_2 = B4095; else WaveformDA_pulse_2 = 0; + WaveformDA_2 = AND_mask[WaveformDA_sawtooth_2 & WaveformDA_pulse_2] << 4; + break; + case 7: + WaveformDA_triangle_2 = (( (OSC_MSB_2 * B2047) ^ (temp12 & B2047)) << 1) ; + WaveformDA_sawtooth_2 = temp12; + if (temp12 >= PW_HiLo_voice_2 ) WaveformDA_pulse_2 = B4095; else WaveformDA_pulse_2 = 0; + WaveformDA_2 = AND_mask[WaveformDA_pulse_2 & WaveformDA_sawtooth_2 & WaveformDA_triangle_2] << 4; + break; + case 8: + WaveformDA_noise_2 = B4095 & (pseudorandom_2 >> 11); + WaveformDA_2 = WaveformDA_noise_2; + break; + case 9: + WaveformDA_2 = 0; + break; + case 10: + WaveformDA_2 = 0; + break; + case 11: + WaveformDA_2 = 0; + break; + case 12: + WaveformDA_2 = 0; + break; + case 13: + WaveformDA_2 = 0; + break; + case 14: + WaveformDA_2 = 0; + break; + case 15: + WaveformDA_2 = 0; + break; - } + } - // voice 3 - - temp13 = (OSC_3 >> 12); - - switch (waveform_switch_3) { - case 0: - WaveformDA_3 = 0; - break; - case 1: - WaveformDA_triangle_3 = (( (OSC_MSB_3 * B2047) ^ (temp13 & B2047)) << 1) ; - WaveformDA_3 = WaveformDA_triangle_3; - break; - case 2: - WaveformDA_sawtooth_3 = temp13; - WaveformDA_3 = WaveformDA_sawtooth_3; - break; - case 3: - WaveformDA_triangle_3 = (( (OSC_MSB_3 * B2047) ^ (temp13 & B2047)) << 1) ; - WaveformDA_sawtooth_3 = temp13; - WaveformDA_3 = AND_mask[(WaveformDA_triangle_3 & WaveformDA_sawtooth_3)] << 4; - break; - case 4: - if (temp13 >= PW_HiLo_voice_3 ) WaveformDA_pulse_3 = B4095; else WaveformDA_pulse_3 = 0; - WaveformDA_3 = WaveformDA_pulse_3; - break; - case 5: - WaveformDA_triangle_3 = (( (OSC_MSB_3 * B2047) ^ (temp13 & B2047)) << 1) ; - if (temp13 >= PW_HiLo_voice_3 ) WaveformDA_pulse_3 = B4095; else WaveformDA_pulse_3 = 0; - WaveformDA_3 = AND_mask[WaveformDA_triangle_3 & WaveformDA_pulse_3] << 4; - break; - case 6: - WaveformDA_sawtooth_3 = temp13; - if (temp13 >= PW_HiLo_voice_3 ) WaveformDA_pulse_3 = B4095; else WaveformDA_pulse_3 = 0; - WaveformDA_3 = AND_mask[WaveformDA_sawtooth_3 & WaveformDA_pulse_3] << 4; - break; - case 7: - WaveformDA_triangle_3 = (( (OSC_MSB_3 * B2047) ^ (temp13 & B2047)) << 1) ; - WaveformDA_sawtooth_3 = temp13; - if (temp13 >= PW_HiLo_voice_3 ) WaveformDA_pulse_3 = B4095; else WaveformDA_pulse_3 = 0; - WaveformDA_3 = AND_mask[WaveformDA_pulse_3 & WaveformDA_sawtooth_3 & WaveformDA_triangle_3] << 4; - break; - case 8: - WaveformDA_noise_3 = B4095 & (pseudorandom_3 >> 11); - WaveformDA_3 = WaveformDA_noise_3; - break; - case 9: - WaveformDA_3 = 0; - break; - case 10: - WaveformDA_3 = 0; - break; - case 11: - WaveformDA_3 = 0; - break; - case 12: - WaveformDA_3 = 0; - break; - case 13: - WaveformDA_3 = 0; - break; - case 14: - WaveformDA_3 = 0; - break; - case 15: - WaveformDA_3 = 0; - break; + // voice 3 + + temp13 = (OSC_3 >> 12); + + switch (waveform_switch_3) { + case 0: + WaveformDA_3 = 0; + break; + case 1: + WaveformDA_triangle_3 = (( (OSC_MSB_3 * B2047) ^ (temp13 & B2047)) << 1) ; + WaveformDA_3 = WaveformDA_triangle_3; + break; + case 2: + WaveformDA_sawtooth_3 = temp13; + WaveformDA_3 = WaveformDA_sawtooth_3; + break; + case 3: + WaveformDA_triangle_3 = (( (OSC_MSB_3 * B2047) ^ (temp13 & B2047)) << 1) ; + WaveformDA_sawtooth_3 = temp13; + WaveformDA_3 = AND_mask[(WaveformDA_triangle_3 & WaveformDA_sawtooth_3)] << 4; + break; + case 4: + if (temp13 >= PW_HiLo_voice_3 ) WaveformDA_pulse_3 = B4095; else WaveformDA_pulse_3 = 0; + WaveformDA_3 = WaveformDA_pulse_3; + break; + case 5: + WaveformDA_triangle_3 = (( (OSC_MSB_3 * B2047) ^ (temp13 & B2047)) << 1) ; + if (temp13 >= PW_HiLo_voice_3 ) WaveformDA_pulse_3 = B4095; else WaveformDA_pulse_3 = 0; + WaveformDA_3 = AND_mask[WaveformDA_triangle_3 & WaveformDA_pulse_3] << 4; + break; + case 6: + WaveformDA_sawtooth_3 = temp13; + if (temp13 >= PW_HiLo_voice_3 ) WaveformDA_pulse_3 = B4095; else WaveformDA_pulse_3 = 0; + WaveformDA_3 = AND_mask[WaveformDA_sawtooth_3 & WaveformDA_pulse_3] << 4; + break; + case 7: + WaveformDA_triangle_3 = (( (OSC_MSB_3 * B2047) ^ (temp13 & B2047)) << 1) ; + WaveformDA_sawtooth_3 = temp13; + if (temp13 >= PW_HiLo_voice_3 ) WaveformDA_pulse_3 = B4095; else WaveformDA_pulse_3 = 0; + WaveformDA_3 = AND_mask[WaveformDA_pulse_3 & WaveformDA_sawtooth_3 & WaveformDA_triangle_3] << 4; + break; + case 8: + WaveformDA_noise_3 = B4095 & (pseudorandom_3 >> 11); + WaveformDA_3 = WaveformDA_noise_3; + break; + case 9: + WaveformDA_3 = 0; + break; + case 10: + WaveformDA_3 = 0; + break; + case 11: + WaveformDA_3 = 0; + break; + case 12: + WaveformDA_3 = 0; + break; + case 13: + WaveformDA_3 = 0; + break; + case 14: + WaveformDA_3 = 0; + break; + case 15: + WaveformDA_3 = 0; + break; - } + } - //ADSR1 + //ADSR1 - // gate change check + // gate change check - switch (Gate_bit_1) { - case 0: + switch (Gate_bit_1) { + case 0: - if (Gate_previous_1 == 1) { + if (Gate_previous_1 == 1) { - ADSR_stage_1 = 4; - LFSR15_1 = 0; - LFSR5_1 = 0; - LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Release_1]; - Gate_previous_1 = 0; - } - break; - case 1: - if (Gate_previous_1 == 0) { - - ADSR_stage_1 = 1; // - LFSR15_1 = 0; - LFSR5_1 = 0; - Gate_previous_1 = 1; - hold_zero_1 = false; - LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Attack_1]; - } - break; - } + ADSR_stage_1 = 4; + LFSR15_1 = 0; + LFSR5_1 = 0; + LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Release_1]; + Gate_previous_1 = 0; + } + break; + case 1: + if (Gate_previous_1 == 0) { + + ADSR_stage_1 = 1; // + LFSR15_1 = 0; + LFSR5_1 = 0; + Gate_previous_1 = 1; + hold_zero_1 = false; + LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Attack_1]; + } + break; + } - // Increase LFSR15 counter for ADSR (scaled to match) + // Increase LFSR15 counter for ADSR (scaled to match) - LFSR15_1 = LFSR15_1 + multiplier;; - if ( ((LFSR15_1 >= LFSR15_comparator_value_1 ) ) ) { + LFSR15_1 = LFSR15_1 + multiplier;; + if ( ((LFSR15_1 >= LFSR15_comparator_value_1 ) ) ) { + + Divided_LFSR15_1 = ((LFSR15_1 ) / LFSR15_comparator_value_1); + LFSR15_1 = LFSR15_1 - Divided_LFSR15_1 * LFSR15_comparator_value_1; + LFSR5_1 = LFSR5_1 + Divided_LFSR15_1 ; + if ((ADSR_stage_1 == 1) | (LFSR5_1 >= LFSR5_comparator_value_1) ) { + Divided_LFSR5_1 = (LFSR5_1 ) / LFSR5_comparator_value_1; + if (Divided_LFSR5_1 >= 1) { + LFSR5_1 = 0; + } + else { + LFSR5_1 = LFSR5_1 - Divided_LFSR5_1 * LFSR5_comparator_value_1; + } + + if (hold_zero_1 == false) { + switch (ADSR_stage_1) { + case 0: + break; + case 1: + ADSR_volume_1 = (ADSR_volume_1 + Divided_LFSR15_1) ; + if (ADSR_volume_1 >= 0xff) { + ADSR_volume_1 = 0xff - (ADSR_volume_1 - 0xff); + ADSR_stage_1 = 2; + hold_zero_1 = false; + LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Decay_1 ]; + } + + break; + case 2: + if (ADSR_volume_1 >= Divided_LFSR5_1) { + ADSR_volume_1 = ADSR_volume_1 - Divided_LFSR5_1; + } + else { + ADSR_volume_1 = 0; + } + + if (ADSR_volume_1 <= (( ADSR_Sustain_1 << 4) + ADSR_Sustain_1)) { + ADSR_volume_1 = (( ADSR_Sustain_1 << 4) + ADSR_Sustain_1); + LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Release_1 ]; + ADSR_stage_1 = 3; + } + break; + case 3: + if (ADSR_volume_1 > (( ADSR_Sustain_1 << 4) + ADSR_Sustain_1)) { + ADSR_stage_1 = 2; + LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Decay_1 ]; + } + break; + case 4: + if (ADSR_volume_1 >= Divided_LFSR5_1) { + ADSR_volume_1 = ADSR_volume_1 - Divided_LFSR5_1; + } + else { + ADSR_volume_1 = 0; + } + break; - Divided_LFSR15_1 = ((LFSR15_1 ) / LFSR15_comparator_value_1); - LFSR15_1 = LFSR15_1 - Divided_LFSR15_1 * LFSR15_comparator_value_1; - LFSR5_1 = LFSR5_1 + Divided_LFSR15_1 ; - if ((ADSR_stage_1 == 1) | (LFSR5_1 >= LFSR5_comparator_value_1) ) { - Divided_LFSR5_1 = (LFSR5_1 ) / LFSR5_comparator_value_1; - if (Divided_LFSR5_1 >= 1) { - LFSR5_1 = 0; - } - else { - LFSR5_1 = LFSR5_1 - Divided_LFSR5_1 * LFSR5_comparator_value_1; } - if (hold_zero_1 == false) { - switch (ADSR_stage_1) { - case 0: - break; - case 1: - ADSR_volume_1 = (ADSR_volume_1 + Divided_LFSR15_1) ; - if (ADSR_volume_1 >= 0xff) { - ADSR_volume_1 = 0xff - (ADSR_volume_1 - 0xff); - ADSR_stage_1 = 2; - hold_zero_1 = false; - LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Decay_1 ]; - } - - break; - case 2: - if (ADSR_volume_1 >= Divided_LFSR5_1) { - ADSR_volume_1 = ADSR_volume_1 - Divided_LFSR5_1; - } - else { - ADSR_volume_1 = 0; - } - - if (ADSR_volume_1 <= (( ADSR_Sustain_1 << 4) + ADSR_Sustain_1)) { - ADSR_volume_1 = (( ADSR_Sustain_1 << 4) + ADSR_Sustain_1); - LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Release_1 ]; - ADSR_stage_1 = 3; - } - break; - case 3: - if (ADSR_volume_1 > (( ADSR_Sustain_1 << 4) + ADSR_Sustain_1)) { - ADSR_stage_1 = 2; - LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Decay_1 ]; - } - break; - case 4: - if (ADSR_volume_1 >= Divided_LFSR5_1) { - ADSR_volume_1 = ADSR_volume_1 - Divided_LFSR5_1; - } - else { - ADSR_volume_1 = 0; - } - break; - - } - - LFSR5_comparator_value_1 = ADSR_Volume2LFSR5[ADSR_volume_1]; - - - if (ADSR_volume_1 == 0) { - hold_zero_1 = true; - } + LFSR5_comparator_value_1 = ADSR_Volume2LFSR5[ADSR_volume_1]; + + if (ADSR_volume_1 == 0) { + hold_zero_1 = true; } + } + } - } + } - //ADSR2 + //ADSR2 - // gate change check + // gate change check - switch (Gate_bit_2) { - case 0: + switch (Gate_bit_2) { + case 0: - if (Gate_previous_2 == 1) { - ADSR_stage_2 = 4; - LFSR15_2 = 0; - LFSR5_2 = 0; - LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Release_2]; - Gate_previous_2 = 0; + if (Gate_previous_2 == 1) { + ADSR_stage_2 = 4; + LFSR15_2 = 0; + LFSR5_2 = 0; + LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Release_2]; + Gate_previous_2 = 0; - } + } - break; - case 1: - if (Gate_previous_2 == 0) { - ADSR_stage_2 = 1; - LFSR15_2 = 0; - LFSR5_2 = 0; - Gate_previous_2 = 1; - hold_zero_2 = false; - LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Attack_2]; - } - break; - } + break; + case 1: + if (Gate_previous_2 == 0) { + ADSR_stage_2 = 1; + LFSR15_2 = 0; + LFSR5_2 = 0; + Gate_previous_2 = 1; + hold_zero_2 = false; + LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Attack_2]; + } + break; + } - // Increase LFSR15 counter for ADSR (scaled to match) - - LFSR15_2 = LFSR15_2 + multiplier;; - if ( ((LFSR15_2 >= LFSR15_comparator_value_2 ) ) ) { - Divided_LFSR15_2 = ((LFSR15_2 ) / LFSR15_comparator_value_2); - LFSR15_2 = LFSR15_2 - Divided_LFSR15_2 * LFSR15_comparator_value_2; - LFSR5_2 = LFSR5_2 + Divided_LFSR15_2 ; - if ((ADSR_stage_2 == 1) | (LFSR5_2 >= LFSR5_comparator_value_2) ) { - Divided_LFSR5_2 = (LFSR5_2 ) / LFSR5_comparator_value_2; - if (Divided_LFSR5_2 >= 1) { - LFSR5_2 = 0; + // Increase LFSR15 counter for ADSR (scaled to match) + + LFSR15_2 = LFSR15_2 + multiplier;; + if ( ((LFSR15_2 >= LFSR15_comparator_value_2 ) ) ) { + Divided_LFSR15_2 = ((LFSR15_2 ) / LFSR15_comparator_value_2); + LFSR15_2 = LFSR15_2 - Divided_LFSR15_2 * LFSR15_comparator_value_2; + LFSR5_2 = LFSR5_2 + Divided_LFSR15_2 ; + if ((ADSR_stage_2 == 1) | (LFSR5_2 >= LFSR5_comparator_value_2) ) { + Divided_LFSR5_2 = (LFSR5_2 ) / LFSR5_comparator_value_2; + if (Divided_LFSR5_2 >= 1) { + LFSR5_2 = 0; + } + if (hold_zero_2 == false) { + switch (ADSR_stage_2) { + + case 0: + + break; + case 1: + + ADSR_volume_2 = (ADSR_volume_2 + Divided_LFSR15_2) ; + + if (ADSR_volume_2 >= 0xff) { + ADSR_volume_2 = 0xff - (ADSR_volume_2 - 0xff); + ADSR_stage_2 = 2; + hold_zero_2 = false; + LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Decay_2 ]; + } + break; + case 2: + if (ADSR_volume_2 >= Divided_LFSR5_2) { + ADSR_volume_2 = ADSR_volume_2 - Divided_LFSR5_2; + } + else { + ADSR_volume_2 = 0; + } + if (ADSR_volume_2 <= (( ADSR_Sustain_2 << 4) + ADSR_Sustain_2)) { + ADSR_volume_2 = (( ADSR_Sustain_2 << 4) + ADSR_Sustain_2); + LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Release_2 ]; + ADSR_stage_2 = 3; + } + break; + case 3: + if (ADSR_volume_2 > (( ADSR_Sustain_2 << 4) + ADSR_Sustain_2)) { + ADSR_stage_2 = 2; + LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Decay_2 ]; + } + break; + case 4: + if (ADSR_volume_2 >= Divided_LFSR5_2) { + ADSR_volume_2 = ADSR_volume_2 - Divided_LFSR5_2; + } + else { + ADSR_volume_2 = 0; + } + break; } - if (hold_zero_2 == false) { - switch (ADSR_stage_2) { - - case 0: - - break; - case 1: - - ADSR_volume_2 = (ADSR_volume_2 + Divided_LFSR15_2) ; - - if (ADSR_volume_2 >= 0xff) { - ADSR_volume_2 = 0xff - (ADSR_volume_2 - 0xff); - ADSR_stage_2 = 2; - hold_zero_2 = false; - LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Decay_2 ]; - } - break; - case 2: - if (ADSR_volume_2 >= Divided_LFSR5_2) { - ADSR_volume_2 = ADSR_volume_2 - Divided_LFSR5_2; - } - else { - ADSR_volume_2 = 0; - } - if (ADSR_volume_2 <= (( ADSR_Sustain_2 << 4) + ADSR_Sustain_2)) { - ADSR_volume_2 = (( ADSR_Sustain_2 << 4) + ADSR_Sustain_2); - LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Release_2 ]; - ADSR_stage_2 = 3; - } - break; - case 3: - if (ADSR_volume_2 > (( ADSR_Sustain_2 << 4) + ADSR_Sustain_2)) { - ADSR_stage_2 = 2; - LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Decay_2 ]; - } - break; - case 4: - if (ADSR_volume_2 >= Divided_LFSR5_2) { - ADSR_volume_2 = ADSR_volume_2 - Divided_LFSR5_2; - } - else { - ADSR_volume_2 = 0; - } - break; - } - - LFSR5_comparator_value_2 = ADSR_Volume2LFSR5[ADSR_volume_2]; - - if (ADSR_volume_2 == 0) { - hold_zero_2 = true; - } + + LFSR5_comparator_value_2 = ADSR_Volume2LFSR5[ADSR_volume_2]; + + if (ADSR_volume_2 == 0) { + hold_zero_2 = true; } } } + } - //ADSR3 + //ADSR3 - // gate change check + // gate change check - switch (Gate_bit_3) { - case 0: + switch (Gate_bit_3) { + case 0: - if (Gate_previous_3 == 1) { - ADSR_stage_3 = 4; - LFSR15_3 = 0; - LFSR5_3 = 0; - LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Release_3]; - Gate_previous_3 = 0; - } - break; - case 1: - if (Gate_previous_3 == 0) { - ADSR_stage_3 = 1; // - LFSR15_3 = 0; - LFSR5_3 = 0; - Gate_previous_3 = 1; - hold_zero_3 = false; - LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Attack_3]; - } - break; + if (Gate_previous_3 == 1) { + ADSR_stage_3 = 4; + LFSR15_3 = 0; + LFSR5_3 = 0; + LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Release_3]; + Gate_previous_3 = 0; + } + break; + case 1: + if (Gate_previous_3 == 0) { + ADSR_stage_3 = 1; // + LFSR15_3 = 0; + LFSR5_3 = 0; + Gate_previous_3 = 1; + hold_zero_3 = false; + LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Attack_3]; + } + break; - } + } - // Increase LFSR15 counter for ADSR (scaled to match) + // Increase LFSR15 counter for ADSR (scaled to match) - LFSR15_3 = LFSR15_3 + multiplier;; - if ( ((LFSR15_3 >= LFSR15_comparator_value_3 ) ) ) { + LFSR15_3 = LFSR15_3 + multiplier;; + if ( ((LFSR15_3 >= LFSR15_comparator_value_3 ) ) ) { - Divided_LFSR15_3 = ((LFSR15_3 ) / LFSR15_comparator_value_3); - LFSR15_3 = LFSR15_3 - Divided_LFSR15_3 * LFSR15_comparator_value_3; - LFSR5_3 = LFSR5_3 + Divided_LFSR15_3 ; + Divided_LFSR15_3 = ((LFSR15_3 ) / LFSR15_comparator_value_3); + LFSR15_3 = LFSR15_3 - Divided_LFSR15_3 * LFSR15_comparator_value_3; + LFSR5_3 = LFSR5_3 + Divided_LFSR15_3 ; - if ((ADSR_stage_3 == 1) | (LFSR5_3 >= LFSR5_comparator_value_3) ) { - Divided_LFSR5_3 = (LFSR5_3 ) / LFSR5_comparator_value_3; - if (Divided_LFSR5_3 >= 1) { - LFSR5_3 = 0; - } - else { - LFSR5_3 = LFSR5_3 - Divided_LFSR5_3 * LFSR5_comparator_value_3; + if ((ADSR_stage_3 == 1) | (LFSR5_3 >= LFSR5_comparator_value_3) ) { + Divided_LFSR5_3 = (LFSR5_3 ) / LFSR5_comparator_value_3; + if (Divided_LFSR5_3 >= 1) { + LFSR5_3 = 0; + } + else { + LFSR5_3 = LFSR5_3 - Divided_LFSR5_3 * LFSR5_comparator_value_3; + } + if (hold_zero_3 == false) { + + switch (ADSR_stage_3) { + case 0: + break; + case 1: + ADSR_volume_3 = (ADSR_volume_3 + Divided_LFSR15_3) ; + if (ADSR_volume_3 >= 0xff) { + ADSR_volume_3 = 0xff - (ADSR_volume_3 - 0xff); + ADSR_stage_3 = 2; + hold_zero_3 = false; + LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Decay_3 ]; + } + break; + case 2: + if (ADSR_volume_3 >= Divided_LFSR5_3) { + ADSR_volume_3 = ADSR_volume_3 - Divided_LFSR5_3; + } + else { + ADSR_volume_3 = 0; + } + if (ADSR_volume_3 <= (( ADSR_Sustain_3 << 4) + ADSR_Sustain_3)) { + ADSR_volume_3 = (( ADSR_Sustain_3 << 4) + ADSR_Sustain_3); + LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Release_3 ]; + ADSR_stage_3 = 3; + } + break; + case 3: + if (ADSR_volume_3 > (( ADSR_Sustain_3 << 4) + ADSR_Sustain_3)) { + ADSR_stage_3 = 2; + LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Decay_3 ]; + } + break; + case 4: + if (ADSR_volume_3 >= Divided_LFSR5_3) { + ADSR_volume_3 = ADSR_volume_3 - Divided_LFSR5_3; + } + else { + ADSR_volume_3 = 0; + } + break; } - if (hold_zero_3 == false) { - - switch (ADSR_stage_3) { - case 0: - break; - case 1: - ADSR_volume_3 = (ADSR_volume_3 + Divided_LFSR15_3) ; - if (ADSR_volume_3 >= 0xff) { - ADSR_volume_3 = 0xff - (ADSR_volume_3 - 0xff); - ADSR_stage_3 = 2; - hold_zero_3 = false; - LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Decay_3 ]; - } - break; - case 2: - if (ADSR_volume_3 >= Divided_LFSR5_3) { - ADSR_volume_3 = ADSR_volume_3 - Divided_LFSR5_3; - } - else { - ADSR_volume_3 = 0; - } - if (ADSR_volume_3 <= (( ADSR_Sustain_3 << 4) + ADSR_Sustain_3)) { - ADSR_volume_3 = (( ADSR_Sustain_3 << 4) + ADSR_Sustain_3); - LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Release_3 ]; - ADSR_stage_3 = 3; - } - break; - case 3: - if (ADSR_volume_3 > (( ADSR_Sustain_3 << 4) + ADSR_Sustain_3)) { - ADSR_stage_3 = 2; - LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Decay_3 ]; - } - break; - case 4: - if (ADSR_volume_3 >= Divided_LFSR5_3) { - ADSR_volume_3 = ADSR_volume_3 - Divided_LFSR5_3; - } - else { - ADSR_volume_3 = 0; - } - break; - } - - LFSR5_comparator_value_3 = ADSR_Volume2LFSR5[ADSR_volume_3]; - - if (ADSR_volume_3 == 0) { - hold_zero_3 = true; - } + LFSR5_comparator_value_3 = ADSR_Volume2LFSR5[ADSR_volume_3]; + + if (ADSR_volume_3 == 0) { + hold_zero_3 = true; } + } } + } - // FILTERS: + // FILTERS: #ifndef USE_FILTERS - if (FILTER_Enable_1) { - if (FILTER_LP) { - WaveformDA_1 = 0xfff - WaveformDA_1 ; - } - if (FILTER_HP) { - WaveformDA_1 = 0xfff - WaveformDA_1 ; - } + if (FILTER_Enable_1) { + if (FILTER_LP) { + WaveformDA_1 = 0xfff - WaveformDA_1 ; } - if (FILTER_Enable_2) { - if (FILTER_LP) { - WaveformDA_2 = 0xfff - WaveformDA_2 ; - } - if (FILTER_HP) { - WaveformDA_2 = 0xfff - WaveformDA_2 ; - } + if (FILTER_HP) { + WaveformDA_1 = 0xfff - WaveformDA_1 ; } - if (FILTER_Enable_3) { - if (FILTER_LP) { - WaveformDA_3 = 0xfff - WaveformDA_3 ; - } - if (FILTER_HP) { - WaveformDA_3 = 0xfff - WaveformDA_3 ; - } + } + if (FILTER_Enable_2) { + if (FILTER_LP) { + WaveformDA_2 = 0xfff - WaveformDA_2 ; + } + if (FILTER_HP) { + WaveformDA_2 = 0xfff - WaveformDA_2 ; } + } + if (FILTER_Enable_3) { + if (FILTER_LP) { + WaveformDA_3 = 0xfff - WaveformDA_3 ; + } + if (FILTER_HP) { + WaveformDA_3 = 0xfff - WaveformDA_3 ; + } + } #endif #ifndef USE_CHANNEL_1 - WaveformDA_1 = 0; + WaveformDA_1 = 0; #endif #ifndef USE_CHANNEL_2 - WaveformDA_2 = 0; + WaveformDA_2 = 0; #endif #ifndef USE_CHANNEL_3 - WaveformDA_3 = 0; + WaveformDA_3 = 0; #endif - /////////////////// ANALOG /////////////////////////// + /////////////////// ANALOG /////////////////////////// - Volume_1 = int32_t(WaveformDA_1 * ADSR_volume_1) - 0x80000 ; - Volume_2 = int32_t(WaveformDA_2 * ADSR_volume_2) - 0x80000 ; - Volume_3 = int32_t(WaveformDA_3 * ADSR_volume_3) - 0x80000 ; + Volume_1 = int32_t(WaveformDA_1 * ADSR_volume_1) - 0x80000 ; + Volume_2 = int32_t(WaveformDA_2 * ADSR_volume_2) - 0x80000 ; + Volume_3 = int32_t(WaveformDA_3 * ADSR_volume_3) - 0x80000 ; - // FILTERS redirect to filtered or unfiltered output + // FILTERS redirect to filtered or unfiltered output - switch (FILTER_Enable_switch) { - default: - case 0x0: - Volume_filtered = 0; - if (OFF3 ) - { - Volume_unfiltered = Volume_1 + Volume_2; - } - else { - Volume_unfiltered = Volume_1 + Volume_2 + Volume_3 ; - } - break; - case 0x1: - Volume_filtered = Volume_1; - if (OFF3 ) - { - Volume_unfiltered = Volume_2; - } - else - { - Volume_unfiltered = Volume_2 + Volume_3 ; - } - break; - case 0x2: - Volume_filtered = Volume_2; - if (OFF3 ) - { - Volume_unfiltered = Volume_1 ; - } - else - { - Volume_unfiltered = Volume_1 + Volume_3 ; - } - break; - case 0x3: - Volume_filtered = Volume_1 + Volume_2; - if (OFF3 ) - { - Volume_unfiltered = 0 ; - } - else - { - Volume_unfiltered = Volume_3 ; - } - break; - case 0x4: - Volume_filtered = Volume_3; - Volume_unfiltered = Volume_1 + Volume_2 ; - break; - case 0x5: - Volume_filtered = Volume_1 + Volume_3; - Volume_unfiltered = Volume_2 ; - break; - case 0x6: - Volume_filtered = Volume_2 + Volume_3; - Volume_unfiltered = Volume_1 ; - break; - case 0x7: - Volume_filtered = Volume_1 + Volume_2 + Volume_3; - Volume_unfiltered = 0; - break; - } + switch (FILTER_Enable_switch) { + default: + case 0x0: + Volume_filtered = 0; + if (OFF3 ) + { + Volume_unfiltered = Volume_1 + Volume_2; + } + else { + Volume_unfiltered = Volume_1 + Volume_2 + Volume_3 ; + } + break; + case 0x1: + Volume_filtered = Volume_1; + if (OFF3 ) + { + Volume_unfiltered = Volume_2; + } + else + { + Volume_unfiltered = Volume_2 + Volume_3 ; + } + break; + case 0x2: + Volume_filtered = Volume_2; + if (OFF3 ) + { + Volume_unfiltered = Volume_1 ; + } + else + { + Volume_unfiltered = Volume_1 + Volume_3 ; + } + break; + case 0x3: + Volume_filtered = Volume_1 + Volume_2; + if (OFF3 ) + { + Volume_unfiltered = 0 ; + } + else + { + Volume_unfiltered = Volume_3 ; + } + break; + case 0x4: + Volume_filtered = Volume_3; + Volume_unfiltered = Volume_1 + Volume_2 ; + break; + case 0x5: + Volume_filtered = Volume_1 + Volume_3; + Volume_unfiltered = Volume_2 ; + break; + case 0x6: + Volume_filtered = Volume_2 + Volume_3; + Volume_unfiltered = Volume_1 ; + break; + case 0x7: + Volume_filtered = Volume_1 + Volume_2 + Volume_3; + Volume_unfiltered = 0; + break; + } - Volume_filter_input = Volume_filtered; - Volume_filter_output = Volume_filtered; + Volume_filter_input = Volume_filtered; + Volume_filter_output = Volume_filtered; #ifdef USE_FILTERS - Volume_filter_input = int32_t(Volume_filter_input) >> 7; + Volume_filter_input = int32_t(Volume_filter_input) >> 7; - delta_t = multiplier; - delta_t_flt = FILTER_SENSITIVITY; + delta_t = multiplier; + delta_t_flt = FILTER_SENSITIVITY; - while (delta_t) { - if (delta_t < delta_t_flt) { - delta_t_flt = delta_t; - } - - w0_delta_t = (int32_t(w0_ceil_dt * delta_t_flt) >> 6); - dVbp = (int32_t(w0_delta_t*Vhp) >> 14); - dVlp = (int32_t(w0_delta_t*Vbp) >> 14); - Vbp -= dVbp; - Vlp -= dVlp; - Vhp = (int32_t(Vbp * (Q_1024_div)) >> 10) - Vlp - Volume_filter_input; - delta_t -= delta_t_flt; + while (delta_t) { + if (delta_t < delta_t_flt) { + delta_t_flt = delta_t; } - Volume_filter_output = 0; - if (FILTER_LP) { - Volume_filter_output = Volume_filter_output + Vlp; - } - if (FILTER_HP) { - Volume_filter_output = Volume_filter_output + Vhp; - } - if (FILTER_BP) { - Volume_filter_output = Volume_filter_output + Vbp; - } - // - - Volume_filter_output = (int32_t(Volume_filter_output) << 7); - -#endif + w0_delta_t = (int32_t(w0_ceil_dt * delta_t_flt) >> 6); + dVbp = (int32_t(w0_delta_t*Vhp) >> 14); + dVlp = (int32_t(w0_delta_t*Vbp) >> 14); + Vbp -= dVbp; + Vlp -= dVlp; + Vhp = (int32_t(Vbp * (Q_1024_div)) >> 10) - Vlp - Volume_filter_input; + delta_t -= delta_t_flt; + } - Volume = ((Volume_filter_output + Volume_unfiltered) >> 2 ) + 0x80000; - - if (Volume < 0) Volume = 0; - if (Volume > 0xfffff) Volume = 0xfffff; - - // main_volume_32bit = ( magic_number * period * ((Volume)&0xfffff) * MASTER_VOLUME) >> 24; - main_volume_32bit = (Volume ) ; - main_volume_32bit = (main_volume_32bit * magic_number); - main_volume_32bit = (main_volume_32bit) >> 12; - main_volume_32bit = (main_volume_32bit * MASTER_VOLUME); - main_volume_32bit = (main_volume_32bit * period) ; - main_volume_32bit = (main_volume_32bit ) >> 12; - main_volume = main_volume_32bit + 1; - - OSC3 = (OSC_3 >> 16) & 0xff; // - /* - OSC3 = (((OSC_3 & 0x400000) >> 11) | // OSC3 output for SID register - ((OSC_3 & 0x100000) >> 10) | - ((OSC_3 & 0x010000) >> 7) | - ((OSC_3 & 0x002000) >> 5) | - ((OSC_3 & 0x000800) >> 4) | - ((OSC_3 & 0x000080) >> 1) | - ((OSC_3 & 0x000010) << 1) | - ((OSC_3 & 0x000004) << 2) )&0xff; - */ - // - ENV3 = (ADSR_volume_3) & 0xff; ; // ((Volume_3 + 0x80000) >> 12) & 0xff; - // - SID[25] = POTX; - SID[26] = POTY; - SID[27] = OSC3; - SID[28] = ENV3; + Volume_filter_output = 0; + if (FILTER_LP) { + Volume_filter_output = Volume_filter_output + Vlp; + } + if (FILTER_HP) { + Volume_filter_output = Volume_filter_output + Vhp; + } + if (FILTER_BP) { + Volume_filter_output = Volume_filter_output + Vbp; + } + // + Volume_filter_output = (int32_t(Volume_filter_output) << 7); +#endif - } + Volume = ((Volume_filter_output + Volume_unfiltered) >> 2 ) + 0x80000; + + if (Volume < 0) Volume = 0; + if (Volume > 0xfffff) Volume = 0xfffff; + + // main_volume_32bit = ( magic_number * period * ((Volume)&0xfffff) * MASTER_VOLUME) >> 24; + main_volume_32bit = (Volume ) ; + main_volume_32bit = (main_volume_32bit * magic_number); + main_volume_32bit = (main_volume_32bit) >> 12; + main_volume_32bit = (main_volume_32bit * MASTER_VOLUME); + main_volume_32bit = (main_volume_32bit * period) ; + main_volume_32bit = (main_volume_32bit ) >> 12; + main_volume = main_volume_32bit + 1; + + OSC3 = (OSC_3 >> 16) & 0xff; // + /* + OSC3 = (((OSC_3 & 0x400000) >> 11) | // OSC3 output for SID register + ((OSC_3 & 0x100000) >> 10) | + ((OSC_3 & 0x010000) >> 7) | + ((OSC_3 & 0x002000) >> 5) | + ((OSC_3 & 0x000800) >> 4) | + ((OSC_3 & 0x000080) >> 1) | + ((OSC_3 & 0x000010) << 1) | + ((OSC_3 & 0x000004) << 2) )&0xff; + */ + // + ENV3 = (ADSR_volume_3) & 0xff; ; // ((Volume_3 + 0x80000) >> 12) & 0xff; + // + SID[25] = POTX; + SID[26] = POTY; + SID[27] = OSC3; + SID[28] = ENV3; + + + +} diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/50_autoconfig.ino b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/50_autoconfig.ino index bf25ce4..2966932 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/50_autoconfig.ino +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/50_autoconfig.ino @@ -151,5 +151,5 @@ inline void FRAMEtest () { debugPrintTXT ("Emulated frame speed: "); debugPrintNUMBER (uint32_t((100 * Emu_uS) / Real_uS )); debugPrintTXTln ("%"); Emu_uS = 0; Real_uS = 0; - + } diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/80_buttons.ino b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/80_buttons.ino index 13ad17e..2e980b4 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/80_buttons.ino +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/80_buttons.ino @@ -130,7 +130,7 @@ void checkButton1() { #ifdef NUMBER_OF_ALL_FOLDERS // if HVSC folder is included favorites_finished = !favorites_finished ; #endif - current_folder = 0; + current_folder = 0; play_next_folder = true; load_next_file = true; play_next_tune = false; diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/90_debug.ino b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/90_debug.ino index 5d144c7..57ec31a 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/90_debug.ino +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/90_debug.ino @@ -1,10 +1,4 @@ -// from SDfat library -extern "C" char* sbrk(int incr); -// free RAM (actually, free stack -inline uint32_t FreeBytes() { - char top = 't'; - return &top - reinterpret_cast(sbrk(0)); -} + // set serial inline void debugInit () { diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1.ino b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1.ino index c73f654..057380a 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1.ino +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1.ino @@ -1,27 +1,19 @@ // Choose your board and upload method from menu -// Choose available RAM for emulator (depending of microcontroller) (currently set for BluePill - STM32F103C8, ROGER's or STM32Duino core ) + // it's strongly recommended to set optimatization on FASTEST -O3 (from tool menu of Arduino IDE). // STM32-SID-Player: https://github.com/Bakisha/STM32-SID-PLAYER // HVSC database: https://www.hvsc.c64.org/downloads (download and unpack to SD Card) -//////////////////////////////////////////////////////////////////////////////////////////// -// -// emulator settings -// -////////////////////////////////////////////////////////////////////////////////////////////// - -#define RAM_SIZE 0x3000 // ---> IMPORTANT! <--- Set this value based on microcontroller used. maximum is 65535 bytes ( 0xFFFF HEX ) or available microcontoller's RAM (leave at least 2000 bytes free ) -#define TUNE_PLAY_TIME 215 // Can't implement songlenghts, manual values are needed (in seconds)// TODO: try to determine silence in output, and skip to next tune - - ///////////////////////////////////////////////////////////////////////////////////////////// // // Player configuration: // ////////////////////////////////////////////////////////////////////////////////////////////// +#define TUNE_PLAY_TIME 215 // Can't implement songlenghts, manual values are needed (in seconds)// TODO: try to determine silence in output, and skip to next tune + const char * const HVSC = "HVSC" ; // ---> IMPORTANT! <--- name of HVSC SID Collection folder on your SD Card ("HVSC", "C64Music", "/" or "" for root, etc" //#include "01_HVSC.h" // disabled for Bluepill. Can be enabled, but maximum RAM_SIZE is 0x1400 (5120 bytes) and O0 optimatization @@ -105,12 +97,13 @@ bool RANDOM_FOLDERS = false; // - play folders playlist in #define USE_FILTERS // uncomment for testing, irq is faster in calculations (so multiplier will be smaller, and samplerate will be higher) -#define FILTER_SENSITIVITY 4 // Lower is better. Best is 1. It will consume irq time , but FILTER_FREQUENCY can be set much higher -#define FILTER_FREQUENCY 12500 // You'll know it if it's very high (it will totaly ruin sound) +#define FILTER_SENSITIVITY 4 // Lower is better. Best is 1. It will consume irq time (smaller samplerate) , but FILTER_FREQUENCY can be set much higher +#define FILTER_FREQUENCY 12500 // You'll know it if it's very high (it will totaly ruin sound) // Play with these two values to determine what is "SID-like" sound // NOTE: Filters are linear in all frequencies range. Don't ask for true filters emulation, i'm suprised i managed to emulate it at all :-) -// -------------------------------------------- +// NOTE2: lower FILTER_FREQUENCY is similar to 6581 SID chip, higher value (12500 and above) is more like 8581 SID chip. +// .--------------------------------------------. // | | maximum | // | FILTER_SENSITIVITY | FILTER_FREQUENCY | // ---------------------------------------------- diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/xx_RAM.h b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/xx_RAM.h index 4e794b4..866c7e3 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/xx_RAM.h +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/xx_RAM.h @@ -1,10 +1,19 @@ +// from SDfat library +extern "C" char* sbrk(int incr); +// free RAM (actually, free stack +inline uint32_t FreeBytes() { + char top = 't'; + return &top - reinterpret_cast(sbrk(0)); +} -#if defined(SPI_RAM) -// TODO -#elif defined(PARALLEL_SRAM) + +#if defined(SPI_RAM) +// TODO +#elif defined(PARALLEL_SRAM) // TODO #else - uint8_t RAM[RAM_SIZE]; +uint16_t RAM_SIZE = 0; +uint8_t * RAM = NULL; #endif diff --git a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/z_SetupLoop.ino b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/z_SetupLoop.ino index f4faa4c..2354b04 100644 --- a/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/z_SetupLoop.ino +++ b/SD_CARD/STM32SID_FOLDER-PLAYER_SDCARD_BUTTON1/z_SetupLoop.ino @@ -12,6 +12,8 @@ void setup() { pinMode(LED_BUILTIN, OUTPUT); randomSeed(millis()); + AllocateRAM(); // in 02_ram.ino // allocate available memory (max 65535), leave 2048 bytes for locals + debugInit(); // in 90_debug.ino autoconfigFinished = false; // do the autoconfig only once @@ -154,7 +156,7 @@ void loop() { play_next_tune = false; } else { - + reset_SID(); set_tune_speed (); infoSID(); diff --git a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/02_ram.ino b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/02_ram.ino index de4c9e2..033987c 100644 --- a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/02_ram.ino +++ b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/02_ram.ino @@ -7,6 +7,16 @@ #else // internal RAM +void AllocateRAM() { + if ((FreeBytes()) > 0xffff ) { + RAM_SIZE = 0xffff; + } + else { + RAM_SIZE = FreeBytes() - 2048; + } + RAM = (uint8_t*) calloc(RAM_SIZE, sizeof(uint8_t)); // allocate memory +} + inline void POKE (uint16_t addr , uint8_t bytE ) { RAM[addr] = bytE; diff --git a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/10_SD.ino b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/10_SD.ino index ea35907..7f70010 100644 --- a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/10_SD.ino +++ b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/10_SD.ino @@ -311,7 +311,7 @@ bool SD_LOAD () { for (uint32_t i = 0; i < SID_data_size - 0x7e; i++) { // data start at $7e offset if ( LOW_RAM == true) { // LOW_RAM from Compatibility_check - if (i < (RAM_SIZE - 0x0400) ) { + if (i < uint16_t(RAM_SIZE - 0x0400) ) { POKE (0x400 + i, sidfile.read()) ; // load sid data to 0x400 } } diff --git a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/40_IRQ.ino b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/40_IRQ.ino index c4d540e..9e928a2 100644 --- a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/40_IRQ.ino +++ b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/40_IRQ.ino @@ -24,9 +24,9 @@ void irq_handler(void) { // change_tune = true; player = false; } - - SID_emulator(); - + if ( player == true ) { // not when loading + SID_emulator(); + } STAD4XX = 0; } @@ -382,14 +382,14 @@ inline void SID_emulator() { ADSR_volume_1 = 0; } - if (ADSR_volume_1 <= (( ADSR_Sustain_1 << 4) + ADSR_Sustain_1)) { + if (ADSR_volume_1 <= uint16_t(( ADSR_Sustain_1 << 4) + ADSR_Sustain_1)) { ADSR_volume_1 = (( ADSR_Sustain_1 << 4) + ADSR_Sustain_1); LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Release_1 ]; ADSR_stage_1 = 3; } break; case 3: - if (ADSR_volume_1 > (( ADSR_Sustain_1 << 4) + ADSR_Sustain_1)) { + if (ADSR_volume_1 > uint16_t(( ADSR_Sustain_1 << 4) + ADSR_Sustain_1)) { ADSR_stage_1 = 2; LFSR15_comparator_value_1 = ADSR_LFSR15[ADSR_Decay_1 ]; } @@ -488,14 +488,14 @@ inline void SID_emulator() { else { ADSR_volume_2 = 0; } - if (ADSR_volume_2 <= (( ADSR_Sustain_2 << 4) + ADSR_Sustain_2)) { + if (ADSR_volume_2 <= uint16_t(( ADSR_Sustain_2 << 4) + ADSR_Sustain_2)) { ADSR_volume_2 = (( ADSR_Sustain_2 << 4) + ADSR_Sustain_2); LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Release_2 ]; ADSR_stage_2 = 3; } break; case 3: - if (ADSR_volume_2 > (( ADSR_Sustain_2 << 4) + ADSR_Sustain_2)) { + if (ADSR_volume_2 > uint16_t(( ADSR_Sustain_2 << 4) + ADSR_Sustain_2)) { ADSR_stage_2 = 2; LFSR15_comparator_value_2 = ADSR_LFSR15[ADSR_Decay_2 ]; } @@ -585,14 +585,14 @@ inline void SID_emulator() { else { ADSR_volume_3 = 0; } - if (ADSR_volume_3 <= (( ADSR_Sustain_3 << 4) + ADSR_Sustain_3)) { + if (ADSR_volume_3 <= uint16_t(( ADSR_Sustain_3 << 4) + ADSR_Sustain_3)) { ADSR_volume_3 = (( ADSR_Sustain_3 << 4) + ADSR_Sustain_3); LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Release_3 ]; ADSR_stage_3 = 3; } break; case 3: - if (ADSR_volume_3 > (( ADSR_Sustain_3 << 4) + ADSR_Sustain_3)) { + if (ADSR_volume_3 > uint16_t(( ADSR_Sustain_3 << 4) + ADSR_Sustain_3)) { ADSR_stage_3 = 2; LFSR15_comparator_value_3 = ADSR_LFSR15[ADSR_Decay_3 ]; } diff --git a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/50_autoconfig.ino b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/50_autoconfig.ino index 2966932..03e5568 100644 --- a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/50_autoconfig.ino +++ b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/50_autoconfig.ino @@ -84,20 +84,20 @@ inline void autoconfigMultiplier () { best_multiplier = multiplier ; } // enable this if raw values are needed during calculations (in case to manualy find perfect value ) - /* - debugPrintTXTln("Testing SID Emulator"); - debugPrintTXT("SID emulation: "); - debugPrintNUMBER(Real_uS); - debugPrintTXT(" uS passed. "); - debugPrintTXT("Around: "); - debugPrintNUMBER(SID_uS); - debugPrintTXT(" uS per irq, "); - debugPrintTXT("at multiplier: "); - debugPrintNUMBER(multiplier); - debugPrintTXT(". Estimated emulated IRQ time: "); - debugPrintNUMBER(estimated_frame_time); - debugPrintTXTln("uS."); - */ + +// debugPrintTXTln("Testing SID Emulator"); +// debugPrintTXT("SID emulation: "); +// debugPrintNUMBER(Real_uS); +// debugPrintTXT(" uS passed. "); +// debugPrintTXT("Around: "); +// debugPrintNUMBER(SID_uS); +// debugPrintTXT(" uS per irq, "); +// debugPrintTXT("at multiplier: "); +// debugPrintNUMBER(multiplier); +// debugPrintTXT(". Estimated emulated IRQ time: "); +// debugPrintNUMBER(estimated_frame_time); +// debugPrintTXTln("uS."); + multiplier++; if (multiplier > 248) { multiplier = 248; diff --git a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/60_sounds.ino b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/60_sounds.ino index 2ea1e21..2ee0172 100644 --- a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/60_sounds.ino +++ b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/60_sounds.ino @@ -59,7 +59,7 @@ inline void error_open_file() { //sawtooth_bit_voice_1=1; // triangle_bit_voice_1 = 1; //pulse_bit_voice_1 = 1; - noise_bit_voice_1; + noise_bit_voice_1=1; Gate_bit_1 = 1; delay(480); OSC_1_HiLo = 0xc800; diff --git a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/90_debug.ino b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/90_debug.ino index f940bba..e63bcab 100644 --- a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/90_debug.ino +++ b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/90_debug.ino @@ -1,11 +1,3 @@ -// from SDfat library -extern "C" char* sbrk(int incr); -// free RAM (actually, free stack -inline uint32_t FreeBytes() { - char top = 't'; - return &top - reinterpret_cast(sbrk(0)); -} - // set serial diff --git a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3.ino b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3.ino index 7fe2b25..70c55b2 100644 --- a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3.ino +++ b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3.ino @@ -1,19 +1,11 @@ // Choose your board and upload method from menu -// Choose available RAM for emulator (depending of microcontroller) (currently set for BluePill - STM32F103C8, ROGER's or STM32Duino core ) + // it's strongly recommended to set optimatization on FASTEST -O3 (from tool menu of Arduino IDE). // STM32-SID-Player: https://github.com/Bakisha/STM32-SID-PLAYER // HVSC database: https://www.hvsc.c64.org/downloads (download and unpack to SD Card) -//////////////////////////////////////////////////////////////////////////////////////////// -// -// emulator settings -// -////////////////////////////////////////////////////////////////////////////////////////////// - -#define RAM_SIZE 0x3000 // ---> IMPORTANT! <--- Set this value based on microcontroller used. maximum is 65535 bytes ( 0xFFFF HEX ) or available microcontoller's RAM (leave at least 2000 bytes free ) - ///////////////////////////////////////////////////////////////////////////////////////////// // // Player configuration: @@ -231,6 +223,11 @@ const char * const FOLDER_PLAYLIST // set favorite direct | | ------ SD_CLK /SW / SW /SW 1 ||<----------------| OUDIO OUT | ---------- CS_SDARD | | | | -- .-----------------------+------+------+------------------+-----------------|------------------| GND + R1 = 100-500 Ohm + C1 = 10-100 nF + C2 = 10 uF + P1 = 10KOhm potentiometer + diff --git a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/xx_RAM.h b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/xx_RAM.h index 7477ae3..866c7e3 100644 --- a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/xx_RAM.h +++ b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/xx_RAM.h @@ -1,3 +1,11 @@ +// from SDfat library +extern "C" char* sbrk(int incr); +// free RAM (actually, free stack +inline uint32_t FreeBytes() { + char top = 't'; + return &top - reinterpret_cast(sbrk(0)); +} + @@ -6,5 +14,6 @@ #elif defined(PARALLEL_SRAM) // TODO #else -uint8_t RAM[RAM_SIZE]; +uint16_t RAM_SIZE = 0; +uint8_t * RAM = NULL; #endif diff --git a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/z_SetupLoop.ino b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/z_SetupLoop.ino index 7292528..8dd933f 100644 --- a/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/z_SetupLoop.ino +++ b/SD_CARD/STM32SID_FOLDER_PLAYER_SDCARD_BUTTON3/z_SetupLoop.ino @@ -11,6 +11,7 @@ void setup() { // put your setup code here, to run once: + AllocateRAM(); // in 02_ram.ino // allocate available memory (max 65535), leave 2048 bytes for locals debugInit (); // in 90_debug.ino initSD (); // in 10_SD.ino