summaryrefslogtreecommitdiff
path: root/opl/examples/droplay.c
blob: d53a427bb907643d6473046f9f9e631bbe771e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// Copyright(C) 2009 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
//     Demonstration program for OPL library to play back DRO
//     format files.
//
//-----------------------------------------------------------------------------


#include <stdio.h>
#include <stdlib.h>

#include "SDL.h"

#include "opl.h"

#define HEADER_STRING "DBRAWOPL"
#define ADLIB_PORT 0x388

void WriteReg(unsigned int reg, unsigned int val)
{
    int i;

    // This was recorded from an OPL2, but we are probably playing
    // back on an OPL3, so we need to enable the original OPL2
    // channels.  Doom does this already, but other games don't.

    if ((reg & 0xf0) == OPL_REGS_FEEDBACK)
    {
        val |= 0x30;
    }

    OPL_WritePort(OPL_REGISTER_PORT, reg);

    for (i=0; i<6; ++i)
    {
        OPL_ReadPort(OPL_REGISTER_PORT);
    }

    OPL_WritePort(OPL_DATA_PORT, val);

    for (i=0; i<35; ++i)
    {
        OPL_ReadPort(OPL_REGISTER_PORT);
    }
}

void ClearAllRegs(void)
{
    int i;

    for (i=0; i<=0xff; ++i)
    {
	WriteReg(i, 0x00);
    }
}

// Detect an OPL chip.

int DetectOPL(void)
{
    int val1, val2;

    WriteReg(OPL_REG_TIMER_CTRL, 0x60);
    WriteReg(OPL_REG_TIMER_CTRL, 0x80);

    val1 = OPL_ReadPort(OPL_REGISTER_PORT) & 0xe0;

    WriteReg(OPL_REG_TIMER1, 0xff);
    WriteReg(OPL_REG_TIMER_CTRL, 0x21);

    OPL_Delay(1);

    val2 = OPL_ReadPort(OPL_REGISTER_PORT) & 0xe0;

    WriteReg(OPL_REG_TIMER_CTRL, 0x60);
    WriteReg(OPL_REG_TIMER_CTRL, 0x80);

    return val1 == 0 && val2 == 0xc0;
}

void Init(void)
{
    if (SDL_Init(SDL_INIT_TIMER) < 0)
    {
        fprintf(stderr, "Unable to initialise SDL timer\n");
        exit(-1);
    }

    if (!OPL_Init(ADLIB_PORT))
    {
        fprintf(stderr, "Unable to initialise OPL layer\n");
        exit(-1);
    }

    if (!DetectOPL())
    {
        fprintf(stderr, "Adlib not detected\n");
        exit(-1);
    }
}

void Shutdown(void)
{
    OPL_Shutdown();
}

struct timer_data
{
    int running;
    FILE *fstream;
};

void TimerCallback(void *data)
{
    struct timer_data *timer_data = data;
    int delay;

    if (!timer_data->running)
    {
        return;
    }

    // Read data until we must make a delay.

    for (;;)
    {
        int reg, val;

        // End of file?

        if (feof(timer_data->fstream))
        {
            timer_data->running = 0;
            return;
        }

        reg = fgetc(timer_data->fstream);
        val = fgetc(timer_data->fstream);

        // Register value of 0 or 1 indicates a delay.

        if (reg == 0x00)
        {
            delay = val;
            break;
        }
        else if (reg == 0x01)
        {
            val |= (fgetc(timer_data->fstream) << 8);
            delay = val;
            break;
        }
        else
        {
            WriteReg(reg, val);
        }
    }

    // Schedule the next timer callback.

    OPL_SetCallback(delay, TimerCallback, timer_data);
}

void PlayFile(char *filename)
{
    struct timer_data timer_data;
    int running;
    char buf[8];

    timer_data.fstream = fopen(filename, "rb");

    if (timer_data.fstream == NULL)
    {
        fprintf(stderr, "Failed to open %s\n", filename);
        exit(-1);
    }

    if (fread(buf, 1, 8, timer_data.fstream) < 8)
    {
        fprintf(stderr, "failed to read raw OPL header\n");
        exit(-1);
    }

    if (strncmp(buf, HEADER_STRING, 8) != 0)
    {
        fprintf(stderr, "Raw OPL header not found\n");
        exit(-1);
    }

    fseek(timer_data.fstream, 28, SEEK_SET);
    timer_data.running = 1;

    // Start callback loop sequence.

    OPL_SetCallback(0, TimerCallback, &timer_data);

    // Sleep until the playback finishes.

    do
    {
        OPL_Lock();
        running = timer_data.running;
        OPL_Unlock();

        SDL_Delay(100);
    } while (running);

    fclose(timer_data.fstream);
}

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        printf("Usage: %s <filename>\n", argv[0]);
        exit(-1);
    }

    Init();
    ClearAllRegs();

    PlayFile(argv[1]);

    ClearAllRegs();
    Shutdown();

    return 0;
}