3 * Adlib OPL2/OPL3 FM synthesizer chipset test program.
4 * Play MIDI file using the OPLx synthesizer (well, poorly anyway)
5 * (C) 2010-2012 Jonathan Campbell.
6 * Hackipedia DOS library.
8 * This code is licensed under the LGPL.
9 * <insert LGPL legal text here>
11 * Compiles for intended target environments:
12 * - MS-DOS [pure DOS mode, or Windows or OS/2 DOS Box]
15 #include "src/lib/16_head.h"
16 #include "src/lib/16_tail.h"
17 #include "src/lib/16_pm.h"
18 #include "src/lib/16_ca.h"
19 #include "src/lib/16_mm.h"
20 #include "src/lib/16_hc.h"
21 #include "src/lib/16_dbg.h"
23 // #include <conio.h> /* this is where Open Watcom hides the outp() etc. functions */
24 // #include <stdlib.h>
25 // #include <string.h>
26 // #include <unistd.h>
27 // #include <malloc.h>
33 #include <hw/vga/vga.h>
34 #include <hw/dos/dos.h>
35 #include <hw/8254/8254.h> /* 8254 timer */
36 #include <hw/8259/8259.h>
37 #include <hw/adlib/adlib.h>
39 static void (interrupt *old_irq0)();
40 static volatile unsigned long irq0_ticks=0;
41 static volatile unsigned int irq0_cnt=0,irq0_add=0,irq0_max=0;
50 static struct imf_entry* imf_music=NULL;
51 static struct imf_entry* imf_play_ptr=NULL;
52 static struct imf_entry* imf_music_end=NULL;
53 static uint16_t imf_delay_countdown=0;
56 printf("-------------------------------------------------------------------------------\n");\
58 printf("bigbuffer %Fp\t", gvar->ca.audiosegs[0]);\
59 printf("&%Fp\n", MEMPTR gvar->ca.audiosegs[0]);\
60 printf("imf_music %Fp\t", imf_music);\
61 printf("&%Fp\n", imf_music);\
62 printf("imf_play_ptr %Fp\t", imf_play_ptr);\
63 printf("&%Fp\n", imf_play_ptr);\
64 printf("imf_music_end %Fp\t", imf_music_end);\
65 printf("&%Fp\n", imf_music_end);\
66 printf("-------------------------------------------------------------------------------\n");\
69 void imf_free_music(global_game_variables_t *gvar) {
70 if (imf_music) free(imf_music);
71 MM_FreePtr(MEMPTR gvar->ca.audiosegs[0], gvar);
72 imf_music = imf_play_ptr = imf_music_end = NULL;
73 imf_delay_countdown = 0;
76 int imf_load_music(const char *path, global_game_variables_t *gvar) {
83 fd = open(path,O_RDONLY|O_BINARY);
86 len = lseek(fd,0,SEEK_END);
89 if (buf[0] != 0 || buf[1] != 0) // type 1 IMF
90 len = *((uint16_t*)buf);
94 if (len == 0 || len > 65535UL) {
100 // imf_music = malloc(len);
101 MM_GetPtr(MEMPTR gvar->ca.audiosegs[0],len, gvar);
102 imf_music = (struct imf_entry *)gvar->ca.audiosegs[0];
103 if (imf_music == NULL) {
107 read(fd,imf_music,len);
110 imf_play_ptr = imf_music;
111 imf_music_end = imf_music + (len >> 2UL);
116 /* WARNING: subroutine call in interrupt handler. make sure you compile with -zu flag for large/compact memory models */
117 void interrupt irq0() {
119 if ((irq0_cnt += irq0_add) >= irq0_max) {
120 irq0_cnt -= irq0_max;
124 p8259_OCW2(0,P8259_OCW2_NON_SPECIFIC_EOI);
129 if (imf_delay_countdown == 0) {
131 adlib_write(imf_play_ptr->reg,imf_play_ptr->data);
132 imf_delay_countdown = imf_play_ptr->delay;
134 if (imf_play_ptr == imf_music_end)
137 imf_play_ptr = imf_music;
139 } while (imf_delay_countdown == 0);
142 imf_delay_countdown--;
146 void adlib_shut_up() {
149 memset(adlib_fm,0,sizeof(adlib_fm));
150 memset(&adlib_reg_bd,0,sizeof(adlib_reg_bd));
151 for (i=0;i < adlib_fm_voices;i++) {
152 struct adlib_fm_operator *f;
153 f = &adlib_fm[i].mod;
154 f->ch_a = f->ch_b = f->ch_c = f->ch_d = 1;
155 f = &adlib_fm[i].car;
156 f->ch_a = f->ch_b = f->ch_c = f->ch_d = 1;
159 for (i=0;i < adlib_fm_voices;i++) {
160 struct adlib_fm_operator *f;
162 f = &adlib_fm[i].mod;
164 f->total_level = 63 - 16;
167 f->sustain_level = 0;
174 f = &adlib_fm[i].car;
176 f->total_level = 63 - 16;
179 f->sustain_level = 0;
190 void main(int argc,char **argv) {
191 static global_game_variables_t gvar;
192 unsigned long tickrate = 700;
202 PM_Startup(&gvar); PM_CheckMainMem(&gvar); PM_UnlockMainMem(&gvar);
204 printf("ADLIB FM test program IMFPLAY\n");
206 printf("You must specify an IMF file to play\n");
211 printf("Cannot init library\n");
214 if (!probe_8254()) { /* we need the timer to keep time with the music */
215 printf("8254 timer not found\n");
219 if (!imf_load_music(argv[1], &gvar)) {
220 printf("Failed to load IMF Music\n");
224 write_8254_system_timer(T8254_REF_CLOCK_HZ / tickrate);
227 irq0_max = 1000; /* about 18.2Hz */
228 old_irq0 = _dos_getvect(8);/*IRQ0*/
229 _dos_setvect(8,irq0);
232 shutdown_adlib_opl3(); // NTS: Apparently the music won't play otherwise
234 irq0_ticks = ptick = 0;
241 adv = irq0_ticks - ptick;
242 if (adv >= 100UL) adv = 100UL;
253 if (c == 0) c = getch() << 8;
260 // printf("contents of the imf_music\n[\n%s\n]\n", imf_music);
262 imf_free_music(&gvar);
265 _dos_setvect(8,old_irq0);
266 write_8254_system_timer(0);/* back to normal 18.2Hz */