3 * Adlib OPL2/OPL3 FM synthesizer chipset test program.
4 * (C) 2010-2012 Jonathan Campbell.
5 * Hackipedia DOS library.
7 * This code is licensed under the LGPL.
8 * <insert LGPL legal text here>
10 * Compiles for intended target environments:
11 * - MS-DOS [pure DOS mode, or Windows or OS/2 DOS Box]
13 * This test program uses a "text user interface" to allow you to play
14 * with the OPL2/OPL3 chipset and it's parameters. Some "instruments"
15 * are preset for you if you want to make noise faster.
19 #include <conio.h> /* this is where Open Watcom hides the outp() etc. functions */
28 #include <hw/vga/vga.h>
29 #include <hw/dos/dos.h>
30 #include <hw/8254/8254.h> /* 8254 timer */
31 #include <hw/vga/vgagui.h>
32 #include <hw/vga/vgatty.h>
33 #include <hw/adlib/adlib.h>
35 static unsigned int musical_scale[18] = {
57 int main(int argc,char **argv) {
58 int i,loop,redraw,c,cc,selector=0,redrawln=0,hselect=0,selectsub=0;
62 printf("ADLIB FM test program\n");
65 printf("Cannot init VGA\n");
69 printf("Cannot init library\n");
75 /* for VGA: free up space if needed by going to 80x50 */
76 if (adlib_fm_voices > 9)
77 vga_bios_set_80x50_text();
79 memset(adlib_fm,0,sizeof(adlib_fm));
80 memset(&adlib_reg_bd,0,sizeof(adlib_reg_bd));
81 for (i=0;i < adlib_fm_voices;i++) {
82 struct adlib_fm_operator *f;
84 f->ch_a = f->ch_b = f->ch_c = f->ch_d = 1;
86 f->ch_a = f->ch_b = f->ch_c = f->ch_d = 1;
89 for (i=0;i < adlib_fm_voices;i++) {
90 struct adlib_fm_operator *f;
94 f->total_level = 63 - 16;
99 f->f_number = musical_scale[i%18];
103 f = &adlib_fm[i].car;
105 f->total_level = 63 - 16;
108 f->sustain_level = 7;
117 vga_write_color(0x07);
123 if (redraw || redrawln) {
125 for (vga=vga_alpha_ram,cc=0;cc < (80*vga_height);cc++) *vga++ = 0x1E00 | 177;
127 vga_write_color(0x1F);
128 sprintf(tmp,"Adlib FM, %u-voice %s. Use Z & X to adj F10=PRESET F1=QUIET ",adlib_fm_voices,
129 (adlib_flags & ADLIB_FM_OPL3) ? "OPL3" :
130 (adlib_flags & ADLIB_FM_DUAL_OPL2) ? "Dual OPL2" : "OPL2");
132 if (adlib_flags & ADLIB_FM_OPL3) vga_write("F2=OPL3 off ");
135 if (redrawln || redraw) {
136 struct adlib_reg_bd *bd = &adlib_reg_bd;
137 static const char *hsel_str[18] = {
138 "Amplitude modulatation",
142 "Modulator frequency multiple",
153 "Connection (operator 1 -> operator 2)",
155 "Channel mapping (OPL3)"
158 vga_write_color(0x1A);
161 sprintf(tmp,"AM=%u VB=%u RYTHM=%u BAS=%u SNA=%u TOM=%u CYM=%u HI=%u\n",
175 vga_write(hsel_str[hselect]);
178 vga_write_color(hselect == 0 ? 0x70 : 0x1E); vga_write("AM ");
179 vga_write_color(hselect == 1 ? 0x70 : 0x1E); vga_write("VB ");
180 vga_write_color(hselect == 2 ? 0x70 : 0x1E); vga_write("SUST ");
181 vga_write_color(hselect == 3 ? 0x70 : 0x1E); vga_write("KSR ");
182 vga_write_color(hselect == 4 ? 0x70 : 0x1E); vga_write("MMUL ");
183 vga_write_color(hselect == 5 ? 0x70 : 0x1E); vga_write("LKS ");
184 vga_write_color(hselect == 6 ? 0x70 : 0x1E); vga_write("TL ");
185 vga_write_color(hselect == 7 ? 0x70 : 0x1E); vga_write("AR ");
186 vga_write_color(hselect == 8 ? 0x70 : 0x1E); vga_write("DR ");
187 vga_write_color(hselect == 9 ? 0x70 : 0x1E); vga_write("SL ");
188 vga_write_color(hselect == 10 ? 0x70 : 0x1E); vga_write("RR ");
189 vga_write_color(hselect == 11 ? 0x70 : 0x1E); vga_write("KEY ");
190 vga_write_color(hselect == 12 ? 0x70 : 0x1E); vga_write("OCT ");
191 vga_write_color(hselect == 13 ? 0x70 : 0x1E); vga_write("FNUM ");
192 vga_write_color(hselect == 14 ? 0x70 : 0x1E); vga_write("FEED ");
193 vga_write_color(hselect == 15 ? 0x70 : 0x1E); vga_write("CON ");
194 vga_write_color(hselect == 16 ? 0x70 : 0x1E); vga_write("WV ");
195 vga_write_color(hselect == 17 ? 0x70 : 0x1E); vga_write("ABCD ");
197 for (i=0;i < adlib_fm_voices;i++) {
198 struct adlib_fm_operator *f;
201 f = &adlib_fm[i].mod;
203 freq = adlib_fm_op_to_freq(f);
204 vga_write_color(i == selector && selectsub == 0 ? 0x70 : 0x1E);
205 cc = sprintf(tmp,"%u %u %u %u %-2u %u %-2u %-2u %-2u %-2u %-2u %u %u %-4u %u %u %u %c%c%c%c %u %.1fHz ",
206 f->am, f->vibrato, f->sustain, f->key_scaling_rate,
207 f->mod_multiple, f->level_key_scale, f->total_level, f->attack_rate,
208 f->decay_rate, f->sustain_level, f->release_rate, f->key_on,
209 f->octave, f->f_number, f->feedback, f->connection,
210 f->waveform, f->ch_a?'*':'-', f->ch_b?'*':'-', f->ch_c?'*':'-',
211 f->ch_d?'*':'-', i+1, freq);
214 f = &adlib_fm[i].car;
215 vga_moveto(0,5+i*2+1);
216 vga_write_color(i == selector && selectsub == 1 ? 0x70 : 0x1E);
217 cc = sprintf(tmp,"%u %u %u %u %-2u %u %-2u %-2u %-2u %-2u %-2u %u CAR ",
218 f->am, f->vibrato, f->sustain, f->key_scaling_rate,
219 f->mod_multiple, f->level_key_scale, f->total_level, f->attack_rate,
220 f->decay_rate, f->sustain_level, f->release_rate, f->waveform);
231 if (c == 0) c = getch() << 8;
236 else if (c == 0x3B00) { /* F1 */
237 for (i=0;i < adlib_fm_voices;i++) {
238 adlib_fm[i].mod.key_on = 0;
239 adlib_fm[i].car.key_on = 0;
240 adlib_update_groupA0(i,&adlib_fm[i]);
244 else if (c == 0x3C00) { /* F2 */
245 if (adlib_flags & ADLIB_FM_OPL3) {
246 shutdown_adlib_opl3();
251 else if (c == 0x4400) { /* F10 */
252 unsigned short op = adlib_voice_to_op[selector];
254 vga_write_color(0x07);
258 vga_write("Choose an instrument to load into the channel:\n");
259 vga_write(" 1. Violin 2. Piano 3. Harpsichord 4. Horn 5. Deep bass drum\n");
260 vga_write(" 6. Small drum \n");
267 (adlib_flags & ADLIB_FM_OPL3 ?
268 adlib_fm_preset_violin_opl3 : adlib_fm_preset_violin_opl2);
270 adlib_fm[selector] = adlib_fm_preset_piano;
272 adlib_fm[selector] = adlib_fm_preset_harpsichord;
274 adlib_fm[selector] = adlib_fm_preset_horn;
276 adlib_fm[selector] = adlib_fm_preset_deep_bass_drum;
278 adlib_fm[selector] = adlib_fm_preset_small_drum;
280 adlib_update_groupA0(selector,&adlib_fm[selector]);
281 adlib_update_groupC0(selector,&adlib_fm[selector]);
282 adlib_update_operator(op,&adlib_fm[selector].mod);
283 adlib_update_operator(op+3,&adlib_fm[selector].car);
288 adlib_fm[selector].mod.key_on ^= 1;
289 adlib_update_groupA0(selector,&adlib_fm[selector]);
294 struct adlib_fm_operator *f = &adlib_fm[selector].mod; f->ch_a ^= 1;
295 adlib_update_groupC0(selector,&adlib_fm[selector]);
298 adlib_reg_bd.am_depth ^= 1;
299 adlib_update_bd(&adlib_reg_bd);
304 adlib_reg_bd.vibrato_depth ^= 1;
305 adlib_update_bd(&adlib_reg_bd);
309 adlib_reg_bd.rythm_enable ^= 1;
310 adlib_update_bd(&adlib_reg_bd);
315 struct adlib_fm_operator *f = &adlib_fm[selector].mod; f->ch_b ^= 1;
316 adlib_update_groupC0(selector,&adlib_fm[selector]);
319 adlib_reg_bd.bass_drum_on ^= 1;
320 adlib_update_bd(&adlib_reg_bd);
325 adlib_reg_bd.snare_drum_on ^= 1;
326 adlib_update_bd(&adlib_reg_bd);
330 adlib_reg_bd.tom_tom_on ^= 1;
331 adlib_update_bd(&adlib_reg_bd);
336 struct adlib_fm_operator *f = &adlib_fm[selector].mod; f->ch_c ^= 1;
337 adlib_update_groupC0(selector,&adlib_fm[selector]);
340 adlib_reg_bd.cymbal_on ^= 1;
341 adlib_update_bd(&adlib_reg_bd);
347 struct adlib_fm_operator *f = &adlib_fm[selector].mod; f->ch_d ^= 1;
348 adlib_update_groupC0(selector,&adlib_fm[selector]);
355 adlib_reg_bd.hi_hat_on ^= 1;
356 adlib_update_bd(&adlib_reg_bd);
359 else if (c == 'z' || c == 'Z' || c == 'x' || c == 'X') {
360 struct adlib_fm_operator *f;
361 int dec = tolower(c) == 'z';
362 unsigned short operator;
365 case 11:selectsub = 0;
369 if (selectsub) f = &adlib_fm[selector].car;
370 else f = &adlib_fm[selector].mod;
371 operator = adlib_voice_to_op[selector] + (selectsub*3);
374 case 0: f->am ^= 1; adlib_update_group20(operator,f); break;
375 case 11: f->key_on ^= 1; adlib_update_groupA0(selector,&adlib_fm[selector]); break;
376 case 1: f->vibrato ^= 1; adlib_update_group20(operator,f); break;
377 case 2: f->sustain ^= 1; adlib_update_group20(operator,f); break;
378 case 15: f->connection ^= 1; adlib_update_group20(operator,f); break;
379 case 3: f->key_scaling_rate ^= 1; adlib_update_group20(operator,f); break;
381 case 4: if (dec) f->mod_multiple--; else f->mod_multiple++;
382 adlib_update_group20(operator,f); break;
383 case 5: if (dec) f->level_key_scale--; else f->level_key_scale++;
384 adlib_update_group40(operator,f); break;
385 case 6: if (dec) f->total_level--; else f->total_level++;
386 adlib_update_group40(operator,f); break;
387 case 7: if (dec) f->attack_rate--; else f->attack_rate++;
388 adlib_update_group60(operator,f); break;
389 case 8: if (dec) f->decay_rate--; else f->decay_rate++;
390 adlib_update_group60(operator,f); break;
391 case 9: if (dec) f->sustain_level--; else f->sustain_level++;
392 adlib_update_group80(operator,f); break;
393 case 10: if (dec) f->release_rate--; else f->release_rate++;
394 adlib_update_group80(operator,f); break;
395 case 12: if (dec) f->octave--; else f->octave++;
396 adlib_update_groupA0(selector,&adlib_fm[selector]); break;
397 case 13: if (dec) f->f_number--; else f->f_number++;
398 adlib_update_groupA0(selector,&adlib_fm[selector]); break;
399 case 14: if (dec) f->feedback--; else f->feedback++;
400 adlib_update_groupC0(selector,&adlib_fm[selector]); break;
401 case 16: if (dec) f->waveform--; else f->waveform++;
402 adlib_update_groupE0(operator,f); break;
407 else if (c == 0x4800) {
408 if (selectsub && !(hselect >= 11 && hselect <= 15)) {
412 else if (selector > 0) {
413 selectsub = !(hselect >= 11 && hselect <= 15);
418 else if (c == 0x4B00) {
424 else if (c == 0x4D00) {
430 else if (c == 0x5000) {
431 if (selectsub == 0 && !(hselect >= 11 && hselect <= 15)) {
435 else if ((selector+1) < adlib_fm_voices) {