/* * cbl_open — включить CBL, зарегистрировать fill()-callback и один * из ISR-насосов. * * Управляющее слово: bit7 (CBL on) + bit4 (int enable) + fmt (bit6 * stereo, bit5 16-бит) + freq_code. Размер блока/буфер тишины * зависят от fmt и underrun_mode (см. cbl.h). cbl_close вешается на * atexit. * * ЗАПРЕТ CBL_PUMP_OTIR + 16-бит (подтверждено чтением исходника MAME * sprinter.cpp): порт данных 0x4F ВСЕГДА кладёт байт как есть в один * слот, не собирая пару байт в 16-бит значение и не сверяясь с * cbl_mode16() вообще — это умеет только акселераторный путь (спец- * страница 0xFD, do_accel_block). Комбинация принципиально не может * звучать правильно, поэтому запрещена (EINVAL). Для 16-бит — * CBL_PUMP_ACCEL. */ #include #include #include #include #include "_cbl.h" #include "../irq/_irq.h" int cbl_open(uint8_t freq_code, uint8_t fmt, uint8_t pump_mode, uint8_t underrun_mode, cbl_fill_fn fill) { if (_irq_cbl_hook) { errno = EBUSY; return -1; } if (freq_code < CBL_FREQ_7K8 || freq_code > CBL_FREQ_109K) { errno = EINVAL; return -1; } if (fmt & (uint8_t)~(CBL_FMT_MONO16 | CBL_FMT_STEREO8)) { errno = EINVAL; /* биты вне 5/6 — неизвестный формат */ return -1; } if (pump_mode != CBL_PUMP_OTIR && pump_mode != CBL_PUMP_ACCEL) { errno = EINVAL; return -1; } if (pump_mode == CBL_PUMP_OTIR && (fmt & CBL_FMT_MONO16)) { errno = EINVAL; /* OTIR не может собрать 16-бит сэмпл — только ACCEL */ return -1; } if (underrun_mode != CBL_UNDERRUN_APP && underrun_mode != CBL_UNDERRUN_SILENCE) { errno = EINVAL; return -1; } uint16_t block = (fmt & CBL_FMT_MONO16) ? 256u : 128u; uint8_t *silence = 0; if (underrun_mode == CBL_UNDERRUN_SILENCE) { silence = malloc(block); if (!silence) { errno = ENOMEM; return -1; } memset(silence, (fmt & CBL_FMT_MONO16) ? 0x00 : 0x80, block); } if (_irq_table_ref() != 0) { free(silence); return -1; } _cbl_block = block; _cbl_reqs = 0; _cbl_undr = 0; _cbl_fill = fill; _cbl_underrun_mode = underrun_mode; _cbl_silence = silence; static uint8_t atexit_armed; if (!atexit_armed) { atexit_armed = 1; atexit(cbl_close); /* CBL обязан умолкнуть до шелла */ } IRQ_DISABLE(); _irq_cbl_hook = (pump_mode == CBL_PUMP_ACCEL) ? _cbl_pump_accel : _cbl_pump_otir; _cbl_ctrl((uint8_t)(0x90 | fmt | freq_code)); /* on + int + формат + частота */ IRQ_ENABLE(); return 0; }