aboutsummaryrefslogtreecommitdiff
path: root/src/chips/ymf262_lle/nopl3.c
blob: 850cc1e3c6787f899af2293cb51472965f23390c (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
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (C) 2023 nukeykt
 *
 * This file is part of YMF262-LLE.
 *
 * 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.
 *
 *  YMF262 emulator
 *  Thanks:
 *      John McMaster (siliconpr0n.org):
 *          YMF262 decap and die shot
 *
 */

#include "nuked_fmopl3.h"
#include <stdint.h>
#include <stdlib.h>
#include "nopl3.h"

#define OPL_WRITEBUF_SIZE   2048
#define OPL_WRITEBUF_DELAY  1
#define RSM_FRAC 10

typedef struct _opl3_writebuf {
    uint64_t time;
    uint8_t reg;
    uint8_t data;
} opl3_writebuf;

typedef struct {
    fmopl3_t chip;

    int sample_a;
    int sample_b;
    int o_sy;
    int o_smpbd;
    int o_smpac;
    int shifter_ab;

    int32_t rateratio;
    int32_t samplecnt;
    int oldsample[2];

    uint64_t writebuf_samplecnt;
    uint32_t writebuf_cur;
    uint32_t writebuf_last;
    uint64_t writebuf_lasttime;
    opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
} nopl3_t;

void nopl3_cycle(nopl3_t *chip)
{
    int i;
    for (i = 0; i < 576/4; i++)
    {
        chip->chip.input.mclk = i & 1;
        FMOPL3_Clock(&chip->chip);

        if (chip->o_sy && !chip->chip.o_sy)
        {

            if (chip->o_smpac && !chip->chip.o_smpac)
            {
                chip->sample_a = chip->shifter_ab & 0xffff;
                if (chip->sample_a & 0x8000)
                {
                    chip->sample_a &= 0x7fff;
                }
                else
                {
                    chip->sample_a |= ~0x7fff;
                }
            }
            if (chip->o_smpbd && !chip->chip.o_smpbd)
            {
                chip->sample_b = chip->shifter_ab & 0xffff;
                if (chip->sample_b & 0x8000)
                {
                    chip->sample_b &= 0x7fff;
                }
                else
                {
                    chip->sample_b |= ~0x7fff;
                }
            }
            chip->shifter_ab = (chip->shifter_ab >> 1) | (chip->chip.o_doab << 15);

            chip->o_smpac = chip->chip.o_smpac;
            chip->o_smpbd = chip->chip.o_smpbd;
        }

        chip->o_sy = chip->chip.o_sy;
    }
}

void *nopl3_init(int clock, int samplerate)
{
    nopl3_t *chip = calloc(1, sizeof(nopl3_t));
    nopl3_set_rate(chip, clock, samplerate);
    return chip;
}

void nopl3_set_rate(void* chip_p, int clock, int samplerate)
{
    nopl3_t *chip = (nopl3_t*)chip_p;

    chip->chip.input.cs = 0;
    chip->chip.input.ic = 1;
    chip->chip.input.rd = 1;
    chip->chip.input.wr = 1;

    samplerate *= 1;

    chip->rateratio = ((samplerate << RSM_FRAC) * (int64_t)288) / clock;

    /* printf("%i %i\n", clock, samplerate); */

    nopl3_reset(chip);
}

void nopl3_shutdown(void *chip)
{
    free(chip);
}

void nopl3_reset(void *chip)
{
    nopl3_t* chip2 = chip;
    int i = 0;

    chip2->chip.input.ic = 0;
    for (i = 0; i < 100; i++)
    {
        nopl3_cycle(chip2);
    }
    chip2->chip.input.ic = 1;
    for (i = 0; i < 100; i++)
    {
        nopl3_cycle(chip2);
    }

}

void nopl3_write2(nopl3_t *chip, int port, int val)
{
    chip->chip.input.address = port;
    chip->chip.input.data_i = val;
    chip->chip.input.wr = 0;
    FMOPL3_Clock(&chip->chip); /* propagate */
    chip->chip.input.wr = 1;
    FMOPL3_Clock(&chip->chip); /* propagate */
}

void nopl3_getsample(void *chip, short *sndptr, int numsamples)
{
    nopl3_t* chip2 = chip;
    int i, buf0, buf1;
    short *p = sndptr;
    opl3_writebuf* writebuf;

    for (i = 0; i < numsamples; i++)
    {
        while (chip2->samplecnt >= chip2->rateratio)
        {
            chip2->oldsample[0] = chip2->sample_a;
            chip2->oldsample[1] = chip2->sample_b;
            nopl3_cycle(chip2);

            while ((writebuf = &chip2->writebuf[chip2->writebuf_cur]), writebuf->time <= chip2->writebuf_samplecnt)
            {
                if (!(writebuf->reg & 4))
                {
                    break;
                }
                writebuf->reg &= 3;
                nopl3_write2(chip2, writebuf->reg, writebuf->data);
                chip2->writebuf_cur = (chip2->writebuf_cur + 1) % OPL_WRITEBUF_SIZE;
            }
            chip2->writebuf_samplecnt++;
            chip2->samplecnt -= chip2->rateratio;
        }

        buf0 = (chip2->oldsample[0] * (chip2->rateratio - chip2->samplecnt)
            + chip2->sample_a * chip2->samplecnt) / chip2->rateratio;
        buf1 = (chip2->oldsample[1] * (chip2->rateratio - chip2->samplecnt)
            + chip2->sample_b * chip2->samplecnt) / chip2->rateratio;
        chip2->samplecnt += 1 << RSM_FRAC;
        *p++ = buf1;
        *p++ = buf0;
    }
}

void nopl3_getsample_one_native(void *chip, short *sndptr)
{
    nopl3_t* chip2 = chip;
    short *p = sndptr;
    opl3_writebuf* writebuf;

    chip2->oldsample[0] = chip2->sample_a;
    chip2->oldsample[1] = chip2->sample_b;
    nopl3_cycle(chip2);

    while ((writebuf = &chip2->writebuf[chip2->writebuf_cur]), writebuf->time <= chip2->writebuf_samplecnt)
    {
        if (!(writebuf->reg & 4))
            break;

        writebuf->reg &= 3;
        nopl3_write2(chip2, writebuf->reg, writebuf->data);
        chip2->writebuf_cur = (chip2->writebuf_cur + 1) % OPL_WRITEBUF_SIZE;
    }

    chip2->writebuf_samplecnt++;
    chip2->samplecnt -= chip2->rateratio;
    chip2->samplecnt += 1 << RSM_FRAC;

    *p++ = chip2->sample_b;
    *p++ = chip2->sample_a;
}

void nopl3_write(void *chip, int port, int val)
{
    nopl3_t* chip2 = chip;
    uint64_t time1, time2;
    opl3_writebuf *writebuf;
    uint32_t writebuf_last;

    writebuf_last = chip2->writebuf_last;
    writebuf = &chip2->writebuf[writebuf_last];

    if (writebuf->reg & 4)
    {
        nopl3_write2(chip2, writebuf->reg & 1, writebuf->data);
        nopl3_cycle(chip2);

        chip2->writebuf_cur = (writebuf_last + 1) % OPL_WRITEBUF_SIZE;
        chip2->writebuf_samplecnt = writebuf->time;
    }

    writebuf->reg = port | 4;
    writebuf->data = val;
    time1 = chip2->writebuf_lasttime + OPL_WRITEBUF_DELAY;
    time2 = chip2->writebuf_samplecnt;

    if (time1 < time2)
    {
        time1 = time2;
    }

    writebuf->time = time1;
    chip2->writebuf_lasttime = time1;
    chip2->writebuf_last = (writebuf_last + 1) % OPL_WRITEBUF_SIZE;
}