aboutsummaryrefslogtreecommitdiff
path: root/src/chips/opl_serial_port.cpp
blob: d491d696dd721b83b38ad125c5c5a299bcb7653a (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
/*
 * 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
 */


#ifdef ENABLE_HW_OPL_SERIAL_PORT

#include "opl_serial_port.h"
#include "opl_serial_misc.h"


static size_t retrowave_protocol_serial_pack(const uint8_t *buf_in, size_t len_in, uint8_t *buf_out)
{
    size_t in_cursor = 0;
    size_t out_cursor = 0;

    buf_out[out_cursor] = 0x00;
    out_cursor += 1;

    uint8_t shift_count = 0;

    while(in_cursor < len_in)
    {
        uint8_t cur_byte_out = buf_in[in_cursor] >> shift_count;
        if(in_cursor > 0)
            cur_byte_out |= (buf_in[in_cursor - 1] << (8 - shift_count));

        cur_byte_out |= 0x01;
        buf_out[out_cursor] = cur_byte_out;

        shift_count += 1;
        in_cursor += 1;
        out_cursor += 1;
        if(shift_count > 7)
        {
            shift_count = 0;
            in_cursor -= 1;
        }
    }

    if(shift_count)
    {
        buf_out[out_cursor] = buf_in[in_cursor - 1] << (8 - shift_count);
        buf_out[out_cursor] |= 0x01;
        out_cursor += 1;
    }

    buf_out[out_cursor] = 0x02;
    out_cursor += 1;

    return out_cursor;
}

OPL_SerialPort::OPL_SerialPort()
    : m_port(NULL), m_protocol(ProtocolUnknown)
{}

OPL_SerialPort::~OPL_SerialPort()
{
    delete m_port;
    m_port = NULL;
}

bool OPL_SerialPort::connectPort(const std::string& name, unsigned baudRate, unsigned protocol)
{
    delete m_port;
    m_port = NULL;

    // ensure audio thread reads protocol atomically and in order,
    // so chipType() will be correct after the port is live
    m_protocol = protocol;

    // QSerialPort *port = m_port = new QSerialPort(name);
    // port->setBaudRate(baudRate);
    // return port->open(QSerialPort::WriteOnly);

    m_port = new ChipSerialPort;
    return m_port->open(name, baudRate);
}

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

void OPL_SerialPort::writeReg(uint16_t addr, uint8_t data)
{
    uint8_t sendBuffer[16];
    ChipSerialPortBase *port = m_port;

    if(!port || !port->isOpen())
        return;

    switch(m_protocol)
    {
    default:
    case ProtocolArduinoOPL2:
    {
        if(addr >= 0x100)
            break;
        sendBuffer[0] = (uint8_t)addr;
        sendBuffer[1] = (uint8_t)data;
        port->write(sendBuffer, 2);
        break;
    }
    case ProtocolNukeYktOPL3:
    {
        sendBuffer[0] = (addr >> 6) | 0x80;
        sendBuffer[1] = ((addr & 0x3f) << 1) | (data >> 7);
        sendBuffer[2] = (data & 0x7f);
        port->write(sendBuffer, 3);
        break;
    }
    case ProtocolRetroWaveOPL3:
    {
        bool port1 = (addr & 0x100) != 0;
        uint8_t buf[8] =
        {
            static_cast<uint8_t>(0x21 << 1), 0x12,
            static_cast<uint8_t>(port1 ? 0xe5 : 0xe1), static_cast<uint8_t>(addr & 0xff),
            static_cast<uint8_t>(port1 ? 0xe7 : 0xe3), static_cast<uint8_t>(data),
            0xfb, static_cast<uint8_t>(data)
        };
        size_t packed_len = retrowave_protocol_serial_pack(buf, sizeof(buf), sendBuffer);
        port->write(sendBuffer, packed_len);
        break;
    }
    }
}

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

const char *OPL_SerialPort::emulatorName()
{
    return "OPL Serial Port Driver";
}

OPLChipBase::ChipType OPL_SerialPort::chipType()
{
    switch(m_protocol)
    {
    default:
    case ProtocolArduinoOPL2:
        return OPLChipBase::CHIPTYPE_OPL2;
    case ProtocolNukeYktOPL3:
    case ProtocolRetroWaveOPL3:
        return OPLChipBase::CHIPTYPE_OPL3;
    }
}

#endif // ENABLE_HW_OPL_SERIAL_PORT