1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-12-01 13:50:30 +00:00
8bitworkshop/presets/coleco/musicplayer.c

116 lines
18 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <string.h>
#include <cv.h>
#include <cvu.h>
#define PATTERN ((const cv_vmemp)0x0000)
#define COLOR ((const cv_vmemp)0x2000)
#define IMAGE ((const cv_vmemp)0x1c00)
uintptr_t __at(0x6a) font_bitmap_a;
uintptr_t __at(0x6c) font_bitmap_0;
typedef unsigned char byte;
typedef unsigned short word;
volatile bool vint;
volatile uint_fast8_t vint_counter;
void vint_handler(void) {
vint = true;
vint_counter++;
}
void wait_vsync() {
vint = false;
while (!vint) ;
}
void delay(byte i) {
while (i--) {
wait_vsync();
}
}
//
// MUSIC ROUTINES
//
// Namespace(bias=0, freq=1789772.5, length=64, maxbits=15.0, upper=49)
// 437.0 459.515928524 49
const int note_table[64] = {
17356, 32764, 30926, 29190, 27551, 26005, 24546, 23168, 21868, 20640, 19482, 18388, 17356, 32765, 30926, 29190, 27552, 26005, 24546, 23168, 21868, 20640, 19482, 18389, 17357, 16382, 15463, 14595, 13776, 13003, 12273, 11584, 10934, 10320, 9741, 9194, 8678, 8191, 7731, 7298, 6888, 6501, 6136, 5792, 5467, 5160, 4871, 4597, 4339, 4096, 3866, 3649, 3444, 3251, 3068, 2896, 2733, 2580, 2435, 2299, 2170, 2048, 1933, 1824, };
struct {
byte volume;
} voice[3];
byte music_index = 0;
byte cur_duration = 0;
const byte music1[] = {
0x35,0x41,0x8a,0x37,0x43,0x8a,0x33,0x3f,0x8a,0x30,0x3c,0x94,0x3e,0x32,0x8a,0x3a,0x2e,0x94,0x35,0x29,0x8a,0x37,0x2b,0x8a,0x33,0x27,0x8a,0x30,0x24,0x94,0x32,0x26,0x8a,0x2e,0x22,0x94,0x29,0x1d,0x8a,0x2b,0x1f,0x8a,0x27,0x1b,0x8a,0x24,0x18,0x94,0x1a,0x26,0x8a,0x18,0x24,0x8a,0x17,0x23,0x8a,0x16,0x22,0xa8,0x3a,0x35,0x32,0x94,0x29,0x26,0x22,0x8a,0x2a,0x8a,0x2b,0x1b,0x8a,0x33,0x8a,0x22,0x1f,0x27,0x8a,0x2b,0x8a,0x33,0x22,0x16,0x94,0x2b,0x27,0x25,0x8a,0x33,0x8a,0x20,0x14,0x94,0x27,0x24,0x94,0x1f,0x13,0x8a,0x3f,0x37,0x33,0x8a,0x38,0x35,0x41,0x8a,0x42,0x39,0x36,0x8a,0x43,0x3a,0x37,0x8a,0x3f,0x37,0x33,0x8a,0x35,0x41,0x38,0x8a,0x3a,0x37,0x43,0x8a,0x16,0x8a,0x3e,0x35,0x32,0x8a,0x41,0x38,0x35,0x94,0x3f,0x33,0x37,0x94,0x1f,0x22,0x27,0x94,0x27,0x22,0x1f,0x94,0x29,0x26,0x22,0x8a,0x2a,0x8a,0x2b,0x1b,0x8a,0x33,0x8a,0x1f,0x22,0x27,0x8a,0x2b,0x8a,0x33,0x22,0x16,0x94,0x2b,0x27,0x25,0x8a,0x33,0x8a,0x20,0x14,0x94,0x24,0x27,0x94,0x1f,0x13,0x94,0x3c,0x33,0x30,0x8a,0x3a,0x33,0x2e,0x8a,0x39,0x33,0x2d,0x8a,0x3c,0x30,0x8a,0x3f,0x33,0x1d,0x8a,0x43,0x37,0x8a,0x1d,0x8a,0x41,0x35,0x8a,0x3f,0x33,0x27,0x8a,0x3c,0x30,0x8a,0x41,0x35,0x38,0x94,0x22,0x16,0x94,0x24,0x18,0x94,0x29,0x26,0x1a,0x8a,0x2a,0x8a,0x2b,0x1b,0x8a,0x33,0x8a,0x27,0x22,0x1f,0x8a,0x2b,0x8a,0x33,0x16,0x22,0x94,0x2b,0x27,0x25,0x8a,0x33,0x8a,0x20,0x14,0x94,0x27,0x24,0x94,0x13,0x1f,0x8a,0x3f,0x33,0x37,0x8a,0x41,0x38,0x35,0x8a,0x42,0x39,0x36,0x8a,0x37,0x3a,0x43,0x8a,0x3f,0x37,0x33,0x8a,0x41,0x38,0x35,0x8a,0x43,0x3a,0x37,0x8a,0x16,0x8a,0x3e,0x35,0x32,0x8a,0x41,0x38,0x35,0x94,0x3f,0x37,0x33,0x94,0x27,0x22,0x1f,0x94,0x2b,0x27,0x22,0x94,0x3f,0x33,0x8a,0x41,0x35,0x8a,0x43,0x37,0x27,0x8a,0x3f,0x33,0x8a,0x41,0x35,0x2b,0x8a,0x43,0x37,0x8a,0x25,0x19,0x8a,0x3f,0x33,0x8a,0x41,0x35,0x2b,0x8a,0x3f,0x33,0x8a,0x43,0x37,0x24,0x8a,0x3f,0x33,0x8a,0x41,0x35,0x2c,0x8a,0x43,0x37,0x8a,0x23,0x17,0x8a,0x3f,0x33,0x8a,0x41,0x35,0x2c,0x8a,0x3f,0x33,0x8a,0x3a,0x37,0x43,0x8a,0x3f,0x37,0x33,0x8a,0x41,0x38,0x35,0x8a,0x43,0x3a,0x37,0x8a,0x16,0x8a,0x3e,0x32,0x35,0x8a,0x41,0x35,0x38,0x94,0x3f,0x33,0x37,0x94,0x22,0x16,0x94,0x24,0x18,0x94,0x29,0x26,0x1a,0x8a,0x2a,0x8a,0x2b,0x1b,0x8a,0x33,0x8a,0x27,0x22,0x1f,0x8a,0x2b,0x8a,0x33,0x22,0x16,0x94,0x2b,0x25,0x22,0x8a,0x33,0x8a,0x20,0x14,0x94,0x27,0x24,0x94,0x1f,0x13,0x8a,0x3f,0x37,0x33,0x8a,0x35,0x41,0x38,0x8a,0x42,0x39,0x36,0x8a,0x43,0x3a,0x37,0x8a,0x3f,0x37,0x33,0x8a,0x38,0x35,0x41,0x8a,0x43,0x3a,0x37,0x8a,0x16,0x8a,0x3e,0x35,0x32,0x8a,0x35,0x41,0x38,0x94,0x37,0x33,0x3f,0x94,0x27,0x22,0x1f,0x94,0x27,0x22,0x1f,0x94,0x29,0x26,0x22,0x8a,0x2a,0x8a,0x2b,0x1b,0x8a,0x33,0x8a,0x22,0x1f,0x27,0x8a,0x2b,0x8a,0x33,0x22,0x16,0x94,0x2b,0x27,0x25,0x8a,0x33,0x8a,0x20,0x14,0x94,0x27,0x24,0x94,0x1f,0x13,0x94,0x3c,0x33,0x30,0x8a,0x3a,0x33,0x2e,0x8a,0x2d,0x33,0x39,0x8a,0x3c,0x30,0x8a,0x3f,0x33,0x27,0x8a,0x43,0x37,0x8a,0x1d,0x8a,0x41,0x35,0x8a,0x33,0x3f,0x27,0x8a,0x3c,0x30,0x8a,0x41,0x38,0x35,0x94,0x22,0x16,0x94,0x24,0x18,0x94,0x29,0x26,0x1a,0x8a,0x2a,0x8a,0x2b,0x1b,0x8a,0x33,0x8a,0x22,0x27,0x1f,0x8a,0x2b,0x8a,0x33,0x22,0x16,0x94,0x2b,0x27,0x25,0x8a,0x33,0x8a,0x20,0x14,0x94,0x24,0x27,0x94,0x1f,0x13,0x8a,0x3f,0x37,0x33,0x8a,0x41,0x38,0x35,0x8a,0x36,0x39,0x42,0x8a,0x43,0x3a,0x37,0x8a,0x3f,0x37,0x33,0x8a,0x41,0x38,0x35,0x8a,0x37,0x43,0x3a,0x8a,0x16,0x8a,0x3e,0x35,0x32,0x8a,0x41,0x38,0x35,0x94,0x33,0x37,0x3f,0x94,0x27,0x22,0x1f,0x94,0x22,0x27,0x2b,0x94,0x3f,0x33,0x8a,0x41,0x35,0x8a,0x43,0x37,0x27,0x8a,0x3f,0x33,0x8a,0x41,0x35,0x2b,0x8a,0x43,0x37,0x8a,0x25,0x19,0x8a,0x3f,0x33,0x8a,0x41,0x35,0x2b,0x8a,0x3f,0x33,0x8a,0x43,0x37,0x24,0x8a,0x33,0x3f,0x8a,0x41,0x35,0x2c,0x8a,0x43,0x37,0x8a,0x23,0x17,0x8a,0x3f,0x33,0x8a,0x41,0x35,0x2c,0x8a,0x3f,0x33,0x8a,0x43,0x3a,0x37,0x8a,0x3f,0x37,0x33,0x8a,0x41,0x38,0x35,0x8a,0x37,0x3a,0x43,0x8a,0x16,0x8a,0x3e,0x35,0x32,0x8a,0x41,0x38,0x35,0x94,0x33,0x37,0x3f,0x94,0x22,0x16,0x94,0x1b,0x0f,0x8a,0x37,0x2b,0x33,0x8a,0x38,0x2c,0x35,0x8a,0x39,0x36,0x2d,0x8a,0x3a,0x37,0x2e,0x94,0x3c,0x37,0x30,0x8a,0x3a,0x2e,0x37,0x8a,0x16,0x8a,0x37,0x33,0x2b,0x8a,0x2c,0x38,0x35,0x8a,0x39,0x36,0x2d,0x8a,0x3a,0x37,0x2e,0x94,0x3c,0x37,0x30,0x8a,0x2e,0x37,0x3a,0x8a,0x16,0x8a,0x37,0x8a,0x33,0x27,0x2b,0x8a,0x2e,
};
static const byte* music_ptr = NULL;
inline byte next_music_byte() {
return *music_ptr++;
}
void play_music() {
byte ch;
byte freech = 0;
for (ch=0; ch<3; ch++) {
if (voice[ch].volume) {
cv_set_attenuation(CV_SOUNDCHANNEL_0+ch*2, 32-voice[ch].volume--);
} else {
freech = ch;
}
}
if (music_ptr) {
ch = freech;
while (cur_duration == 0) {
byte note = next_music_byte();
if ((note & 0x80) == 0) {
int period = note_table[note & 63];
cv_set_attenuation(CV_SOUNDCHANNEL_0+ch*2, 32);
cv_set_frequency(CV_SOUNDCHANNEL_0+ch*2, period);
voice[ch].volume = 31;
ch = ch ? ch-1 : 2;
} else {
if (note == 0xff)
music_ptr = NULL;
cur_duration = note & 63;
}
}
cur_duration--;
}
}
void start_music(const byte* music) {
music_ptr = music;
cur_duration == 0;
}
void setup_40_column_font() {
cv_set_image_table(IMAGE);
cvu_memtovmemcpy(0x1800, (void *)(font_bitmap_0 - 0x30*8), 2048);
cv_set_character_pattern_t(0x1800);
cv_set_screen_mode(CV_SCREENMODE_TEXT);
}
void main(void) {
setup_40_column_font();
cv_set_colors(CV_COLOR_LIGHT_GREEN, CV_COLOR_BLACK);
cvu_vmemset(IMAGE, '.', 960);
cv_set_vint_handler(vint_handler);
cv_set_screen_active(true);
while (1) {
if (!music_ptr) start_music(music1);
play_music();
delay(1);
}
}