aboutsummaryrefslogtreecommitdiff
path: root/src/chips/dos_hw_opl.cpp
blob: a9f47652be32f25d25048ba12bd1c5c3cda7a7ac (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
/*
 * Interfaces over Yamaha OPL3 (YMF262) chip emulators
 *
 * Copyright (c) 2017-2025 Vitaly Novichkov (Wohlstand)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifdef __DJGPP__
#   include <pc.h>
#endif

#include "dos_hw_opl.h"

static uint16_t                 s_OPLBase[2]    = {0x388, 0x38A};
static char                     s_devName[80]   = {0};
static OPLChipBase::ChipType    s_type          = OPLChipBase::CHIPTYPE_OPL3;
static bool                     s_detected      = false;

static void s_updateDevName()
{
    const char *oplName = (s_type == OPLChipBase::CHIPTYPE_OPL3) ? "OPL3" : "OPL2";
    memset(s_devName, 0, sizeof(s_devName));
    snprintf(s_devName, 80, "%s at 0x%03X", oplName, s_OPLBase[0]);
}

static void s_detect()
{
    if(s_detected)
        return;

    const char *blaster = getenv("BLASTER");
    if(blaster)
    {
        size_t len = strlen(blaster);
        for(size_t i = 0; i < len - 1; ++i)
        {
            if(blaster[i] == 'T')
            {
                switch(blaster[i + 1])
                {
                case '1':
                case '2':
                    s_type = OPLChipBase::CHIPTYPE_OPL2;
                    break;
                case '3':
                case '4':
                case '6':
                    s_type = OPLChipBase::CHIPTYPE_OPL3;
                    break;
                default:
                    s_type = OPLChipBase::CHIPTYPE_OPL2;
                    break;
                }

                // printf("-- Detected BLASTER T%c\n", blaster[i + 1]);

                break;
            }
        }
    }
    else
        s_type = OPLChipBase::CHIPTYPE_OPL2;

    s_detected = true;
}

DOS_HW_OPL::DOS_HW_OPL()
{
    s_detect();
    s_updateDevName();
}

void DOS_HW_OPL::setOplAddress(uint16_t address)
{
    s_OPLBase[0] = address;
    s_OPLBase[1] = address + 2;
    s_updateDevName();
}

void DOS_HW_OPL::setChipType(ChipType type)
{
    s_type = type;
    s_detected = true; // Assignd manually, no need to detect
}

DOS_HW_OPL::~DOS_HW_OPL()
{
    DOS_HW_OPL::writeReg(0x0BD, 0);
    if(s_type == CHIPTYPE_OPL3)
    {
        DOS_HW_OPL::writeReg(0x104, 0);
        DOS_HW_OPL::writeReg(0x105, 0);
    }
}

void DOS_HW_OPL::writeReg(uint16_t addr, uint8_t data)
{
    assert(m_id <= 1);
    unsigned o = addr >> 8;
    unsigned port = s_OPLBase[m_id] + o * 2;

#   ifdef __DJGPP__
    outportb(port, addr);

    for(unsigned c = 0; c < 6; ++c)
        inportb(port);

    outportb(port + 1, data);

    for(unsigned c = 0; c < 35; ++c)
        inportb(port);
#   endif

#   ifdef __WATCOMC__
    outp(port, addr);

    for(uint16_t c = 0; c < 6; ++c)
        inp(port);

    outp(port + 1, data);

    for(uint16_t c = 0; c < 35; ++c)
        inp(port);
#   endif//__WATCOMC__
}

void DOS_HW_OPL::nativeGenerate(int16_t *frame)
{
    frame[0] = 0;
    frame[1] = 0;
}

const char *DOS_HW_OPL::emulatorName()
{
    return s_devName;
}

bool DOS_HW_OPL::hasFullPanning()
{
    return false;
}

OPLChipBase::ChipType DOS_HW_OPL::chipType()
{
    return s_type;
}