From 71b6ea2431c54f9678170ef09cd1953e49e16db9 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Mon, 6 May 2024 03:22:44 +0300 Subject: [PATCH 01/13] Serial: Begin the work on making non-Qt serial support (this is a requirement to make it work under libADLMIDI) --- src/opl/chips/opl_serial_port.cpp | 145 ++++++++++++++++++++++++++++-- src/opl/chips/opl_serial_port.h | 3 +- 2 files changed, 140 insertions(+), 8 deletions(-) diff --git a/src/opl/chips/opl_serial_port.cpp b/src/opl/chips/opl_serial_port.cpp index 0ee4374..64262ab 100644 --- a/src/opl/chips/opl_serial_port.cpp +++ b/src/opl/chips/opl_serial_port.cpp @@ -22,6 +22,134 @@ #include +#ifdef __linux__ +#include +#include +#include +#include +#endif + + +class ChipSerialPort +{ + std::string m_portName; + +#ifdef __unix__ + int m_port; + struct termios m_portSetup; + + static unsigned int baud2enum(unsigned int baud) + { + if(baud == 0) + return B0; + else if(baud <= 50) + return B50; + else if(baud <= 75) + return B75; + else if(baud <= 110) + return B110; + else if(baud <= 134) + return B134; + else if(baud <= 150) + return B150; + else if(baud <= 200) + return B200; + else if(baud <= 300) + return B300; + else if(baud <= 600) + return B600; + else if(baud <= 1200) + return B1200; + else if(baud <= 1800) + return B1800; + else if(baud <= 2400) + return B2400; + else if(baud <= 4800) + return B4800; + else if(baud <= 9600) + return B9600; + else if(baud <= 19200) + return B19200; + else if(baud <= 38400) + return B38400; + else if(baud <= 57600) + return B57600; + else if(baud <= 115200) + return B115200; + else + return B230400; + } +#endif + +public: + ChipSerialPort() + { +#ifdef __linux__ + m_port = 0; + memset(&m_portSetup, 0, sizeof(struct termios)); +#endif + } + + ~ChipSerialPort() + { + close(); + } + + bool isOpen() + { + return m_port != 0; + } + + void close() + { + if(m_port) + ::close(m_port); + + m_port = 0; + } + + bool open(const std::string &portName, unsigned baudRate) + { + if(m_port) + this->close(); + + std::string portPath = "/dev/" + portName; + m_port = ::open(portPath.c_str(), O_WRONLY); + + if(m_port < 0) + { + m_port = 0; + return false; + } + + if(tcgetattr(m_port, &m_portSetup) != 0) + { + close(); + return false; + } + + cfsetospeed(&m_portSetup, baud2enum(baudRate)); + + if(tcsetattr(m_port, TCSANOW, &m_portSetup) != 0) + { + close(); + return false; + } + + return true; + } + + int write(uint8_t *data, size_t size) + { + if(!m_port) + return 0; + + return ::write(m_port, data, size); + } +}; + + + 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; @@ -83,9 +211,12 @@ bool OPL_SerialPort::connectPort(const QString &name, unsigned baudRate, unsigne // so chipType() will be correct after the port is live m_protocol.storeRelease(protocol); - QSerialPort *port = m_port = new QSerialPort(name); - port->setBaudRate(baudRate); - return port->open(QSerialPort::WriteOnly); + // QSerialPort *port = m_port = new QSerialPort(name); + // port->setBaudRate(baudRate); + // return port->open(QSerialPort::WriteOnly); + + m_port = new ChipSerialPort(); + return m_port->open(name.toStdString(), baudRate); } void OPL_SerialPort::writeReg(uint16_t addr, uint8_t data) @@ -96,7 +227,7 @@ void OPL_SerialPort::writeReg(uint16_t addr, uint8_t data) void OPL_SerialPort::sendSerial(uint addr, uint data) { - QSerialPort *port = m_port; + ChipSerialPort *port = m_port; if(!port || !port->isOpen()) return; @@ -118,7 +249,7 @@ void OPL_SerialPort::sendSerial(uint addr, uint data) break; sendBuffer[0] = (uint8_t)addr; sendBuffer[1] = (uint8_t)data; - port->write((char *)sendBuffer, 2); + port->write(sendBuffer, 2); break; } case ProtocolNukeYktOPL3: @@ -126,7 +257,7 @@ void OPL_SerialPort::sendSerial(uint addr, uint data) sendBuffer[0] = (addr >> 6) | 0x80; sendBuffer[1] = ((addr & 0x3f) << 1) | (data >> 7); sendBuffer[2] = (data & 0x7f); - port->write((char *)sendBuffer, 3); + port->write(sendBuffer, 3); break; } case ProtocolRetroWaveOPL3: @@ -140,7 +271,7 @@ void OPL_SerialPort::sendSerial(uint addr, uint data) 0xfb, static_cast(data) }; size_t packed_len = retrowave_protocol_serial_pack(buf, sizeof(buf), sendBuffer); - port->write((char *)sendBuffer, (qint64)packed_len); + port->write(sendBuffer, packed_len); break; } } diff --git a/src/opl/chips/opl_serial_port.h b/src/opl/chips/opl_serial_port.h index 288b2c5..64676f8 100644 --- a/src/opl/chips/opl_serial_port.h +++ b/src/opl/chips/opl_serial_port.h @@ -27,6 +27,7 @@ #include class QSerialPort; +class ChipSerialPort; /// class OPL_SerialPort : public QObject, public OPLChipBaseT @@ -61,7 +62,7 @@ private slots: void sendSerial(uint addr, uint data); private: - QSerialPort *m_port; + ChipSerialPort *m_port; QAtomicInt m_protocol; }; From 71e85f2b41115c2866be2faea67728e46a84c69e Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Mon, 6 May 2024 03:34:26 +0300 Subject: [PATCH 02/13] Serial: Small organizing Put the serial interface class into the separated head --- src/opl/chips/chipset.cmake | 1 + src/opl/chips/opl_serial_misc.h | 171 ++++++++++++++++++++++++++++++ src/opl/chips/opl_serial_port.cpp | 152 +++----------------------- src/opl/chips/opl_serial_port.h | 25 +++-- src/opl/chips/opl_serial_port.pri | 2 +- 5 files changed, 201 insertions(+), 150 deletions(-) create mode 100644 src/opl/chips/opl_serial_misc.h diff --git a/src/opl/chips/chipset.cmake b/src/opl/chips/chipset.cmake index 06b69e5..d4e4f1e 100644 --- a/src/opl/chips/chipset.cmake +++ b/src/opl/chips/chipset.cmake @@ -58,5 +58,6 @@ if(ENABLE_SERIAL_PORT) list(APPEND CHIPS_SOURCES "src/opl/chips/opl_serial_port.cpp" "src/opl/chips/opl_serial_port.h" + "src/opl/chips/opl_serial_misc.h" ) endif() diff --git a/src/opl/chips/opl_serial_misc.h b/src/opl/chips/opl_serial_misc.h new file mode 100644 index 0000000..0d74d85 --- /dev/null +++ b/src/opl/chips/opl_serial_misc.h @@ -0,0 +1,171 @@ +/* + * Interfaces over Yamaha OPL3 (YMF262) chip emulators + * + * Copyright (c) 2017-2023 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 __unix__ +#include +#include +#include +#include +#endif + + +class ChipSerialPortBase +{ +protected: + std::string m_portName; + +public: + ChipSerialPortBase() {} + virtual ~ChipSerialPortBase() {} + + bool isOpen() + { + return false; + } + + void close() {} + + bool open(const std::string &/*portName*/, unsigned /*baudRate*/) + { + return false; + } + + int write(uint8_t */*data*/, size_t /*size*/) + { + return 0; + } +}; + + + +#ifdef __unix__ +class ChipSerialPort : public ChipSerialPortBase +{ + int m_port; + struct termios m_portSetup; + + static unsigned int baud2enum(unsigned int baud) + { + if(baud == 0) + return B0; + else if(baud <= 50) + return B50; + else if(baud <= 75) + return B75; + else if(baud <= 110) + return B110; + else if(baud <= 134) + return B134; + else if(baud <= 150) + return B150; + else if(baud <= 200) + return B200; + else if(baud <= 300) + return B300; + else if(baud <= 600) + return B600; + else if(baud <= 1200) + return B1200; + else if(baud <= 1800) + return B1800; + else if(baud <= 2400) + return B2400; + else if(baud <= 4800) + return B4800; + else if(baud <= 9600) + return B9600; + else if(baud <= 19200) + return B19200; + else if(baud <= 38400) + return B38400; + else if(baud <= 57600) + return B57600; + else if(baud <= 115200) + return B115200; + else + return B230400; + } + +public: + ChipSerialPort() : ChipSerialPortBase() + { + m_port = 0; + memset(&m_portSetup, 0, sizeof(struct termios)); + } + + virtual ~ChipSerialPort() + { + close(); + } + + bool isOpen() + { + return m_port != 0; + } + + void close() + { + if(m_port) + ::close(m_port); + + m_port = 0; + } + + bool open(const std::string &portName, unsigned baudRate) + { + if(m_port) + this->close(); + + std::string portPath = "/dev/" + portName; + m_port = ::open(portPath.c_str(), O_WRONLY); + + if(m_port < 0) + { + m_port = 0; + return false; + } + + if(tcgetattr(m_port, &m_portSetup) != 0) + { + close(); + return false; + } + + cfsetospeed(&m_portSetup, baud2enum(baudRate)); + + if(tcsetattr(m_port, TCSANOW, &m_portSetup) != 0) + { + close(); + return false; + } + + return true; + } + + int write(uint8_t *data, size_t size) + { + if(!m_port) + return 0; + + return ::write(m_port, data, size); + } +}; + +#endif // __unix__ diff --git a/src/opl/chips/opl_serial_port.cpp b/src/opl/chips/opl_serial_port.cpp index 64262ab..44469c1 100644 --- a/src/opl/chips/opl_serial_port.cpp +++ b/src/opl/chips/opl_serial_port.cpp @@ -1,19 +1,21 @@ /* - * OPL Bank Editor by Wohlstand, a free tool for music bank editing - * Copyright (c) 2016-2023 Vitaly Novichkov + * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * 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 3 of the License, or - * any later version. + * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) * - * This program is distributed in the hope that it will be useful, + * 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 General Public License for more details. + * 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 General Public License - * along with this program. If not, see . + * 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 "opl_serial_port.h" @@ -21,133 +23,7 @@ #ifdef ENABLE_HW_OPL_SERIAL_PORT #include - -#ifdef __linux__ -#include -#include -#include -#include -#endif - - -class ChipSerialPort -{ - std::string m_portName; - -#ifdef __unix__ - int m_port; - struct termios m_portSetup; - - static unsigned int baud2enum(unsigned int baud) - { - if(baud == 0) - return B0; - else if(baud <= 50) - return B50; - else if(baud <= 75) - return B75; - else if(baud <= 110) - return B110; - else if(baud <= 134) - return B134; - else if(baud <= 150) - return B150; - else if(baud <= 200) - return B200; - else if(baud <= 300) - return B300; - else if(baud <= 600) - return B600; - else if(baud <= 1200) - return B1200; - else if(baud <= 1800) - return B1800; - else if(baud <= 2400) - return B2400; - else if(baud <= 4800) - return B4800; - else if(baud <= 9600) - return B9600; - else if(baud <= 19200) - return B19200; - else if(baud <= 38400) - return B38400; - else if(baud <= 57600) - return B57600; - else if(baud <= 115200) - return B115200; - else - return B230400; - } -#endif - -public: - ChipSerialPort() - { -#ifdef __linux__ - m_port = 0; - memset(&m_portSetup, 0, sizeof(struct termios)); -#endif - } - - ~ChipSerialPort() - { - close(); - } - - bool isOpen() - { - return m_port != 0; - } - - void close() - { - if(m_port) - ::close(m_port); - - m_port = 0; - } - - bool open(const std::string &portName, unsigned baudRate) - { - if(m_port) - this->close(); - - std::string portPath = "/dev/" + portName; - m_port = ::open(portPath.c_str(), O_WRONLY); - - if(m_port < 0) - { - m_port = 0; - return false; - } - - if(tcgetattr(m_port, &m_portSetup) != 0) - { - close(); - return false; - } - - cfsetospeed(&m_portSetup, baud2enum(baudRate)); - - if(tcsetattr(m_port, TCSANOW, &m_portSetup) != 0) - { - close(); - return false; - } - - return true; - } - - int write(uint8_t *data, size_t size) - { - if(!m_port) - return 0; - - return ::write(m_port, data, size); - } -}; - +#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) diff --git a/src/opl/chips/opl_serial_port.h b/src/opl/chips/opl_serial_port.h index 64676f8..3b00fcf 100644 --- a/src/opl/chips/opl_serial_port.h +++ b/src/opl/chips/opl_serial_port.h @@ -1,21 +1,24 @@ /* - * OPL Bank Editor by Wohlstand, a free tool for music bank editing - * Copyright (c) 2016-2023 Vitaly Novichkov + * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * 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 3 of the License, or - * any later version. + * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) * - * This program is distributed in the hope that it will be useful, + * 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 General Public License for more details. + * 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 General Public License - * along with this program. If not, see . + * 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 */ + #ifndef OPL_SERIAL_PORT_H #define OPL_SERIAL_PORT_H diff --git a/src/opl/chips/opl_serial_port.pri b/src/opl/chips/opl_serial_port.pri index a1dea69..652796e 100644 --- a/src/opl/chips/opl_serial_port.pri +++ b/src/opl/chips/opl_serial_port.pri @@ -1,4 +1,4 @@ QT += serialport -HEADERS += $$PWD/opl_serial_port.h +HEADERS += $$PWD/opl_serial_port.h $$PWD/opl_serial_misc.h SOURCES += $$PWD/opl_serial_port.cpp From 6e4c1ebeca7db2443bb661f615eb60e6f4844bc7 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Mon, 6 May 2024 05:22:29 +0300 Subject: [PATCH 03/13] Serial: Implemented Windows code However, not yet tested (because of unrelated local driver problems) --- src/opl/chips/opl_serial_misc.h | 127 +++++++++++++++++++++++++++++++- 1 file changed, 124 insertions(+), 3 deletions(-) diff --git a/src/opl/chips/opl_serial_misc.h b/src/opl/chips/opl_serial_misc.h index 0d74d85..550cdca 100644 --- a/src/opl/chips/opl_serial_misc.h +++ b/src/opl/chips/opl_serial_misc.h @@ -25,12 +25,13 @@ #include #endif +#ifdef _WIN32 +#include +#endif + class ChipSerialPortBase { -protected: - std::string m_portName; - public: ChipSerialPortBase() {} virtual ~ChipSerialPortBase() {} @@ -169,3 +170,123 @@ class ChipSerialPort : public ChipSerialPortBase }; #endif // __unix__ + + + + +#ifdef _WIN32 + +class ChipSerialPort : public ChipSerialPortBase +{ + HANDLE m_port; + + static unsigned int baud2enum(unsigned int baud) + { + if(baud <= 110) + return CBR_110; + else if(baud <= 300) + return CBR_300; + else if(baud <= 600) + return CBR_600; + else if(baud <= 1200) + return CBR_1200; + else if(baud <= 2400) + return CBR_2400; + else if(baud <= 4800) + return CBR_4800; + else if(baud <= 9600) + return CBR_9600; + else if(baud <= 19200) + return CBR_19200; + else if(baud <= 38400) + return CBR_38400; + else if(baud <= 57600) + return CBR_57600; + else if(baud <= 115200) + return CBR_115200; + else + return CBR_115200; + } + +public: + ChipSerialPort() : ChipSerialPortBase() + { + m_port = NULL; + } + + virtual ~ChipSerialPort() + { + close(); + } + + bool isOpen() + { + return m_port != NULL; + } + + void close() + { + if(m_port) + CloseHandle(m_port); + + m_port = NULL; + } + + bool open(const std::string &portName, unsigned baudRate) + { + if(m_port) + this->close(); + + std::string portPath = "\\\\.\\" + portName; + m_port = CreateFileA(portPath.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); + + if(m_port == INVALID_HANDLE_VALUE) + { + m_port = NULL; + return false; + } + + DCB dcb; + BOOL succ; + + SecureZeroMemory(&dcb, sizeof(DCB)); + dcb.DCBlength = sizeof(DCB); + + succ = GetCommState(m_port, &dcb); + if(!succ) + { + this->close(); + return false; + } + + dcb.BaudRate = baud2enum(baudRate); + dcb.ByteSize = 8; + dcb.Parity = NOPARITY; + dcb.StopBits = ONESTOPBIT; + + succ = SetCommState(m_port, &dcb); + + if(!succ) + { + this->close(); + return false; + } + + return true; + } + + int write(uint8_t *data, size_t size) + { + if(!m_port) + return 0; + + DWORD written = 0; + + if(!WriteFile(m_port, data, size, &written, 0)) + return 0; + + return written; + } +}; + +#endif // _WIN32 From 1f033a51b910c98a540f61e6e3f2f586406d1b20 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Mon, 6 May 2024 23:40:50 +0300 Subject: [PATCH 04/13] Fixed build for ARM64 --- src/ins_names.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ins_names.h b/src/ins_names.h index 9a97f16..5e00080 100644 --- a/src/ins_names.h +++ b/src/ins_names.h @@ -25,7 +25,13 @@ #include #include -#pragma pack(push, 1) + +#if !defined(_MSC_VER) && !defined(__aarch64__) && !defined(__3DS__) +#define ATTRIB_PACKED __attribute__((__packed__)) +#else +#define ATTRIB_PACKED +#endif + struct MidiProgram { //! Kind of instrument. 'M' melodic 'P' percussive @@ -40,8 +46,7 @@ struct MidiProgram const char *bankName; //! Patch name const char *patchName; -}; -#pragma pack(pop) +} ATTRIB_PACKED; enum MidiSpec { From e8c0869cabe369ca0f06331702482cf8e2baa65d Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Tue, 7 May 2024 00:14:46 +0300 Subject: [PATCH 05/13] Serial: Attempt to fix the macOS work --- src/opl/chips/opl_serial_misc.h | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/opl/chips/opl_serial_misc.h b/src/opl/chips/opl_serial_misc.h index 550cdca..17a33c4 100644 --- a/src/opl/chips/opl_serial_misc.h +++ b/src/opl/chips/opl_serial_misc.h @@ -18,9 +18,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifdef __unix__ +#if defined( __unix__) || defined(__APPLE__) #include -#include #include #include #endif @@ -29,6 +28,8 @@ #include #endif +#include + class ChipSerialPortBase { @@ -56,7 +57,7 @@ class ChipSerialPortBase -#ifdef __unix__ +#if defined( __unix__) || defined(__APPLE__) class ChipSerialPort : public ChipSerialPortBase { int m_port; @@ -139,20 +140,36 @@ class ChipSerialPort : public ChipSerialPortBase if(m_port < 0) { + fprintf(stderr, "-- OPL Serial ERROR: failed to open tty device `%s': %s\n", portPath.c_str(), strerror(errno)); + fflush(stderr); m_port = 0; return false; } if(tcgetattr(m_port, &m_portSetup) != 0) { + fprintf(stderr, "-- OPL Serial ERROR: failed to retrieve setup `%s': %s\n", portPath.c_str(), strerror(errno)); + fflush(stderr); close(); return false; } + m_portSetup.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); + m_portSetup.c_oflag &= ~OPOST; + m_portSetup.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + m_portSetup.c_cflag &= ~(CSIZE | PARENB); + m_portSetup.c_cflag |= CS8; + +#if defined (__linux__) || defined (__CYGWIN__) + m_portSetup.c_cflag &= ~CBAUD; +#endif + cfsetospeed(&m_portSetup, baud2enum(baudRate)); if(tcsetattr(m_port, TCSANOW, &m_portSetup) != 0) { + fprintf(stderr, "-- OPL Serial ERROR: failed to apply setup `%s': %s\n", portPath.c_str(), strerror(errno)); + fflush(stderr); close(); return false; } From 6c054b918adab38f75fea4531d1569d12fcec579 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Thu, 9 May 2024 07:16:45 +0300 Subject: [PATCH 06/13] Serial: Make separated Qt and generic parts of the module --- src/bank_editor.cpp | 6 +-- src/bank_editor.h | 2 +- src/opl/chips/chipset.cmake | 2 + src/opl/chips/opl_serial_misc.h | 21 ++++++---- src/opl/chips/opl_serial_port.cpp | 29 +++---------- src/opl/chips/opl_serial_port.h | 19 +++------ src/opl/chips/opl_serial_port.pri | 4 +- src/opl/chips/opl_serial_port_qt.cpp | 62 ++++++++++++++++++++++++++++ src/opl/chips/opl_serial_port_qt.h | 54 ++++++++++++++++++++++++ src/opl/generator.cpp | 6 +-- src/opl/generator.h | 4 +- 11 files changed, 154 insertions(+), 55 deletions(-) create mode 100644 src/opl/chips/opl_serial_port_qt.cpp create mode 100644 src/opl/chips/opl_serial_port_qt.h diff --git a/src/bank_editor.cpp b/src/bank_editor.cpp index c684e43..1954209 100644 --- a/src/bank_editor.cpp +++ b/src/bank_editor.cpp @@ -44,7 +44,7 @@ #include "opl/chips/win9x_opl_proxy.h" #endif #ifdef ENABLE_HW_OPL_SERIAL_PORT // to set port and rate -#include "opl/chips/opl_serial_port.h" +#include "opl/chips/opl_serial_port_qt.h" #endif #include "FileFormats/ffmt_factory.h" @@ -255,7 +255,7 @@ void BankEditor::loadSettings() m_serialPortName = setup.value("hw-opl-serial-port", QString()).toString(); m_serialPortBaudRate = setup.value("hw-opl-serial-baud-rate", 115200).toUInt(); m_serialPortProtocol = setup.value("hw-opl-serial-protocol", 0u).toUInt(); - OPL_SerialPort &serial = *m_serialPortOpl; + OPL_SerialPortQt &serial = *m_serialPortOpl; serial.connectPort(m_serialPortName, m_serialPortBaudRate, m_serialPortProtocol); #endif @@ -1477,7 +1477,7 @@ void BankEditor::on_actionHardware_OPL_triggered() #endif #if defined(ENABLE_HW_OPL_SERIAL_PORT) - OPL_SerialPort &serial = *m_serialPortOpl; + OPL_SerialPortQt &serial = *m_serialPortOpl; dlg->setSerialPortName(m_serialPortName); dlg->setSerialBaudRate(m_serialPortBaudRate); dlg->setSerialProtocol(m_serialPortProtocol); diff --git a/src/bank_editor.h b/src/bank_editor.h index 983caaa..cf06a52 100644 --- a/src/bank_editor.h +++ b/src/bank_editor.h @@ -135,7 +135,7 @@ class BankEditor : public QMainWindow #endif #ifdef ENABLE_HW_OPL_SERIAL_PORT - OPL_SerialPort *m_serialPortOpl = nullptr; + OPL_SerialPortQt *m_serialPortOpl = nullptr; QString m_serialPortName; unsigned m_serialPortBaudRate = 115200; unsigned m_serialPortProtocol = 0; diff --git a/src/opl/chips/chipset.cmake b/src/opl/chips/chipset.cmake index d4e4f1e..9527e45 100644 --- a/src/opl/chips/chipset.cmake +++ b/src/opl/chips/chipset.cmake @@ -57,7 +57,9 @@ endif() if(ENABLE_SERIAL_PORT) list(APPEND CHIPS_SOURCES "src/opl/chips/opl_serial_port.cpp" + "src/opl/chips/opl_serial_port_qt.cpp" "src/opl/chips/opl_serial_port.h" + "src/opl/chips/opl_serial_port_qt.h" "src/opl/chips/opl_serial_misc.h" ) endif() diff --git a/src/opl/chips/opl_serial_misc.h b/src/opl/chips/opl_serial_misc.h index 17a33c4..2c0ff9f 100644 --- a/src/opl/chips/opl_serial_misc.h +++ b/src/opl/chips/opl_serial_misc.h @@ -18,10 +18,15 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifndef OPL_SERIAL_MISC_H +#define OPL_SERIAL_MISC_H + #if defined( __unix__) || defined(__APPLE__) #include #include #include +#include +#include #endif #ifdef _WIN32 @@ -109,7 +114,7 @@ class ChipSerialPort : public ChipSerialPortBase ChipSerialPort() : ChipSerialPortBase() { m_port = 0; - memset(&m_portSetup, 0, sizeof(struct termios)); + std::memset(&m_portSetup, 0, sizeof(struct termios)); } virtual ~ChipSerialPort() @@ -140,16 +145,16 @@ class ChipSerialPort : public ChipSerialPortBase if(m_port < 0) { - fprintf(stderr, "-- OPL Serial ERROR: failed to open tty device `%s': %s\n", portPath.c_str(), strerror(errno)); - fflush(stderr); + std::fprintf(stderr, "-- OPL Serial ERROR: failed to open tty device `%s': %s\n", portPath.c_str(), strerror(errno)); + std::fflush(stderr); m_port = 0; return false; } if(tcgetattr(m_port, &m_portSetup) != 0) { - fprintf(stderr, "-- OPL Serial ERROR: failed to retrieve setup `%s': %s\n", portPath.c_str(), strerror(errno)); - fflush(stderr); + std::fprintf(stderr, "-- OPL Serial ERROR: failed to retrieve setup `%s': %s\n", portPath.c_str(), strerror(errno)); + std::fflush(stderr); close(); return false; } @@ -168,8 +173,8 @@ class ChipSerialPort : public ChipSerialPortBase if(tcsetattr(m_port, TCSANOW, &m_portSetup) != 0) { - fprintf(stderr, "-- OPL Serial ERROR: failed to apply setup `%s': %s\n", portPath.c_str(), strerror(errno)); - fflush(stderr); + std::fprintf(stderr, "-- OPL Serial ERROR: failed to apply setup `%s': %s\n", portPath.c_str(), strerror(errno)); + std::fflush(stderr); close(); return false; } @@ -307,3 +312,5 @@ class ChipSerialPort : public ChipSerialPortBase }; #endif // _WIN32 + +#endif // OPL_SERIAL_MISC_H diff --git a/src/opl/chips/opl_serial_port.cpp b/src/opl/chips/opl_serial_port.cpp index 44469c1..d151a52 100644 --- a/src/opl/chips/opl_serial_port.cpp +++ b/src/opl/chips/opl_serial_port.cpp @@ -22,7 +22,6 @@ #ifdef ENABLE_HW_OPL_SERIAL_PORT -#include #include "opl_serial_misc.h" @@ -78,45 +77,32 @@ OPL_SerialPort::~OPL_SerialPort() m_port = nullptr; } -bool OPL_SerialPort::connectPort(const QString &name, unsigned baudRate, unsigned protocol) +bool OPL_SerialPort::connectPort(const std::string& name, unsigned baudRate, unsigned protocol) { delete m_port; m_port = nullptr; // ensure audio thread reads protocol atomically and in order, // so chipType() will be correct after the port is live - m_protocol.storeRelease(protocol); + 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.toStdString(), baudRate); + return m_port->open(name, baudRate); } void OPL_SerialPort::writeReg(uint16_t addr, uint8_t data) { - QMetaObject::invokeMethod(this, "sendSerial", - Qt::QueuedConnection, Q_ARG(uint, addr), Q_ARG(uint, data)); -} - -void OPL_SerialPort::sendSerial(uint addr, uint data) -{ + uint8_t sendBuffer[16]; ChipSerialPort *port = m_port; if(!port || !port->isOpen()) return; -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - unsigned protocol = m_protocol.loadRelaxed(); -#else - unsigned protocol = m_protocol.load(); -#endif - - uint8_t sendBuffer[16]; - - switch(protocol) + switch(m_protocol) { default: case ProtocolArduinoOPL2: @@ -166,9 +152,7 @@ const char *OPL_SerialPort::emulatorName() OPLChipBase::ChipType OPL_SerialPort::chipType() { - unsigned protocol = m_protocol.loadAcquire(); - - switch(protocol) + switch(m_protocol) { default: case ProtocolArduinoOPL2: @@ -177,7 +161,6 @@ OPLChipBase::ChipType OPL_SerialPort::chipType() case ProtocolRetroWaveOPL3: return OPLChipBase::CHIPTYPE_OPL3; } - } #endif // ENABLE_HW_OPL_SERIAL_PORT diff --git a/src/opl/chips/opl_serial_port.h b/src/opl/chips/opl_serial_port.h index 3b00fcf..a449e32 100644 --- a/src/opl/chips/opl_serial_port.h +++ b/src/opl/chips/opl_serial_port.h @@ -24,22 +24,16 @@ #ifdef ENABLE_HW_OPL_SERIAL_PORT +#include #include "opl_chip_base.h" -#include -#include -#include -class QSerialPort; class ChipSerialPort; -/// -class OPL_SerialPort : public QObject, public OPLChipBaseT +class OPL_SerialPort : public OPLChipBaseT { - Q_OBJECT - public: OPL_SerialPort(); - ~OPL_SerialPort() override; + virtual ~OPL_SerialPort() override; enum Protocol { @@ -49,7 +43,7 @@ class OPL_SerialPort : public QObject, public OPLChipBaseT ProtocolRetroWaveOPL3, }; - bool connectPort(const QString &name, unsigned baudRate, unsigned protocol); + bool connectPort(const std::string &name, unsigned baudRate, unsigned protocol); bool canRunAtPcmRate() const override { return false; } void setRate(uint32_t /*rate*/) override {} @@ -61,12 +55,9 @@ class OPL_SerialPort : public QObject, public OPLChipBaseT const char *emulatorName() override; ChipType chipType() override; -private slots: - void sendSerial(uint addr, uint data); - private: ChipSerialPort *m_port; - QAtomicInt m_protocol; + int m_protocol; }; #endif // ENABLE_HW_OPL_SERIAL_PORT diff --git a/src/opl/chips/opl_serial_port.pri b/src/opl/chips/opl_serial_port.pri index 652796e..dca15b1 100644 --- a/src/opl/chips/opl_serial_port.pri +++ b/src/opl/chips/opl_serial_port.pri @@ -1,4 +1,4 @@ QT += serialport -HEADERS += $$PWD/opl_serial_port.h $$PWD/opl_serial_misc.h -SOURCES += $$PWD/opl_serial_port.cpp +HEADERS += $$PWD/opl_serial_port.h $$PWD/opl_serial_port_qt.h $$PWD/opl_serial_misc.h +SOURCES += $$PWD/opl_serial_port.cpp $$PWD/opl_serial_port_qt.cpp diff --git a/src/opl/chips/opl_serial_port_qt.cpp b/src/opl/chips/opl_serial_port_qt.cpp new file mode 100644 index 0000000..2cdb056 --- /dev/null +++ b/src/opl/chips/opl_serial_port_qt.cpp @@ -0,0 +1,62 @@ +/* + * Interfaces over Yamaha OPL3 (YMF262) chip emulators + * + * Copyright (c) 2017-2023 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_qt.h" + + +OPL_SerialPortQt::OPL_SerialPortQt() : + QObject(), + OPL_SerialPort() +{} + +OPL_SerialPortQt::~OPL_SerialPortQt() +{} + +bool OPL_SerialPortQt::connectPort(const QString& name, unsigned int baudRate, unsigned int protocol) +{ + bool ret; + m_protoMutex.lock(); + ret = OPL_SerialPort::connectPort(name.toStdString(), baudRate, protocol); + m_protoMutex.unlock(); + return ret; +} + +void OPL_SerialPortQt::writeReg(uint16_t addr, uint8_t data) +{ + QMetaObject::invokeMethod(this, "sendSerial", + Qt::QueuedConnection, Q_ARG(uint, addr), Q_ARG(uint, data)); +} + +OPLChipBase::ChipType OPL_SerialPortQt::chipType() +{ + m_protoMutex.lock(); + OPLChipBase::ChipType ret = OPL_SerialPort::chipType(); + m_protoMutex.unlock(); + return ret; +} + +void OPL_SerialPortQt::sendSerial(uint addr, uint data) +{ + OPL_SerialPort::writeReg(addr, data); +} + +#endif // ENABLE_HW_OPL_SERIAL_PORT diff --git a/src/opl/chips/opl_serial_port_qt.h b/src/opl/chips/opl_serial_port_qt.h new file mode 100644 index 0000000..88cd0d1 --- /dev/null +++ b/src/opl/chips/opl_serial_port_qt.h @@ -0,0 +1,54 @@ +/* + * Interfaces over Yamaha OPL3 (YMF262) chip emulators + * + * Copyright (c) 2017-2023 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 + */ + + +#ifndef OPL_SERIAL_PORT_QT_H +#define OPL_SERIAL_PORT_QT_H + +#ifdef ENABLE_HW_OPL_SERIAL_PORT + +#include "opl_serial_port.h" +#include +#include + +class OPL_SerialPortQt : public QObject, public OPL_SerialPort +{ + Q_OBJECT + +public: + OPL_SerialPortQt(); + virtual ~OPL_SerialPortQt() override; + + bool connectPort(const QString &name, unsigned baudRate, unsigned protocol); + + void writeReg(uint16_t addr, uint8_t data) override; + + ChipType chipType() override; + +private slots: + void sendSerial(uint addr, uint data); + +private: + QMutex m_protoMutex; +}; + +#endif // ENABLE_HW_OPL_SERIAL_PORT + +#endif // OPL_SERIAL_PORT_QT_H diff --git a/src/opl/generator.cpp b/src/opl/generator.cpp index 42958f7..7b00819 100644 --- a/src/opl/generator.cpp +++ b/src/opl/generator.cpp @@ -36,7 +36,7 @@ #endif #ifdef ENABLE_HW_OPL_SERIAL_PORT -#include "chips/opl_serial_port.h" +#include "chips/opl_serial_port_qt.h" #endif #define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c" @@ -913,9 +913,9 @@ Win9x_OPL_Proxy &Generator::oplProxy() #endif #ifdef ENABLE_HW_OPL_SERIAL_PORT -OPL_SerialPort &Generator::serialPortOpl() +OPL_SerialPortQt& Generator::serialPortOpl() { - static OPL_SerialPort serial; + static OPL_SerialPortQt serial; return serial; } #endif diff --git a/src/opl/generator.h b/src/opl/generator.h index 42cadde..aa8a3fc 100644 --- a/src/opl/generator.h +++ b/src/opl/generator.h @@ -32,7 +32,7 @@ class Win9x_OPL_Proxy; #endif #ifdef ENABLE_HW_OPL_SERIAL_PORT -class OPL_SerialPort; +class OPL_SerialPortQt; #endif #define NUM_OF_CHANNELS 23 @@ -181,7 +181,7 @@ class Generator #endif #ifdef ENABLE_HW_OPL_SERIAL_PORT - static OPL_SerialPort &serialPortOpl(); + static OPL_SerialPortQt &serialPortOpl(); #endif private: From 6c7946ad49148027a7d74824b2ea873b46485a5d Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Thu, 9 May 2024 09:47:10 +0300 Subject: [PATCH 07/13] Whoops Forgot to apply copyright year here too --- src/opl/chips/common/mutex.hpp | 2 +- src/opl/chips/common/ptr.hpp | 2 +- src/opl/chips/dosbox_opl3.cpp | 2 +- src/opl/chips/dosbox_opl3.h | 2 +- src/opl/chips/java_opl3.cpp | 2 +- src/opl/chips/java_opl3.h | 2 +- src/opl/chips/nuked_opl3.cpp | 2 +- src/opl/chips/nuked_opl3.h | 2 +- src/opl/chips/nuked_opl3_v174.cpp | 2 +- src/opl/chips/nuked_opl3_v174.h | 2 +- src/opl/chips/opal_opl3.cpp | 2 +- src/opl/chips/opal_opl3.h | 2 +- src/opl/chips/opl_chip_base.h | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/opl/chips/common/mutex.hpp b/src/opl/chips/common/mutex.hpp index 3500653..ced49df 100644 --- a/src/opl/chips/common/mutex.hpp +++ b/src/opl/chips/common/mutex.hpp @@ -2,7 +2,7 @@ * libADLMIDI is a free Software MIDI synthesizer library with OPL3 emulation * * Original ADLMIDI code: Copyright (c) 2010-2014 Joel Yliluoma - * ADLMIDI Library API: Copyright (c) 2015-2023 Vitaly Novichkov + * ADLMIDI Library API: Copyright (c) 2015-2024 Vitaly Novichkov * * Library is based on the ADLMIDI, a MIDI player for Linux and Windows with OPL3 emulation: * http://iki.fi/bisqwit/source/adlmidi.html diff --git a/src/opl/chips/common/ptr.hpp b/src/opl/chips/common/ptr.hpp index 4a37760..f80ffd6 100644 --- a/src/opl/chips/common/ptr.hpp +++ b/src/opl/chips/common/ptr.hpp @@ -2,7 +2,7 @@ * libADLMIDI is a free Software MIDI synthesizer library with OPL3 emulation * * Original ADLMIDI code: Copyright (c) 2010-2014 Joel Yliluoma - * ADLMIDI Library API: Copyright (c) 2015-2023 Vitaly Novichkov + * ADLMIDI Library API: Copyright (c) 2015-2024 Vitaly Novichkov * * Library is based on the ADLMIDI, a MIDI player for Linux and Windows with OPL3 emulation: * http://iki.fi/bisqwit/source/adlmidi.html diff --git a/src/opl/chips/dosbox_opl3.cpp b/src/opl/chips/dosbox_opl3.cpp index b516d15..32890ce 100644 --- a/src/opl/chips/dosbox_opl3.cpp +++ b/src/opl/chips/dosbox_opl3.cpp @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/dosbox_opl3.h b/src/opl/chips/dosbox_opl3.h index 580fdbc..5ac876e 100644 --- a/src/opl/chips/dosbox_opl3.h +++ b/src/opl/chips/dosbox_opl3.h @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/java_opl3.cpp b/src/opl/chips/java_opl3.cpp index 0979366..855d88d 100644 --- a/src/opl/chips/java_opl3.cpp +++ b/src/opl/chips/java_opl3.cpp @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/java_opl3.h b/src/opl/chips/java_opl3.h index 1dbfae0..c7b3806 100644 --- a/src/opl/chips/java_opl3.h +++ b/src/opl/chips/java_opl3.h @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/nuked_opl3.cpp b/src/opl/chips/nuked_opl3.cpp index 0ac6cdc..8d28303 100644 --- a/src/opl/chips/nuked_opl3.cpp +++ b/src/opl/chips/nuked_opl3.cpp @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/nuked_opl3.h b/src/opl/chips/nuked_opl3.h index 7de73b5..f0f03ed 100644 --- a/src/opl/chips/nuked_opl3.h +++ b/src/opl/chips/nuked_opl3.h @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/nuked_opl3_v174.cpp b/src/opl/chips/nuked_opl3_v174.cpp index cba2502..4f1f57e 100644 --- a/src/opl/chips/nuked_opl3_v174.cpp +++ b/src/opl/chips/nuked_opl3_v174.cpp @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/nuked_opl3_v174.h b/src/opl/chips/nuked_opl3_v174.h index 5ac3a82..582a4d0 100644 --- a/src/opl/chips/nuked_opl3_v174.h +++ b/src/opl/chips/nuked_opl3_v174.h @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/opal_opl3.cpp b/src/opl/chips/opal_opl3.cpp index 3ea2bde..a8ae7d7 100644 --- a/src/opl/chips/opal_opl3.cpp +++ b/src/opl/chips/opal_opl3.cpp @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/opal_opl3.h b/src/opl/chips/opal_opl3.h index 880c820..d580729 100644 --- a/src/opl/chips/opal_opl3.h +++ b/src/opl/chips/opal_opl3.h @@ -1,7 +1,7 @@ /* * Interfaces over Yamaha OPL3 (YMF262) chip emulators * - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 diff --git a/src/opl/chips/opl_chip_base.h b/src/opl/chips/opl_chip_base.h index 35823c2..5fc58ba 100644 --- a/src/opl/chips/opl_chip_base.h +++ b/src/opl/chips/opl_chip_base.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand) + * Copyright (c) 2017-2024 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 From 3c5652eaf761f03102d35443502921204f24eb9d Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Thu, 9 May 2024 09:53:16 +0300 Subject: [PATCH 08/13] Ouch! --- src/opl/chips/opl_serial_misc.h | 1 + src/opl/chips/opl_serial_port.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/opl/chips/opl_serial_misc.h b/src/opl/chips/opl_serial_misc.h index 2c0ff9f..84711c3 100644 --- a/src/opl/chips/opl_serial_misc.h +++ b/src/opl/chips/opl_serial_misc.h @@ -27,6 +27,7 @@ #include #include #include +#include #endif #ifdef _WIN32 diff --git a/src/opl/chips/opl_serial_port.cpp b/src/opl/chips/opl_serial_port.cpp index d151a52..593a251 100644 --- a/src/opl/chips/opl_serial_port.cpp +++ b/src/opl/chips/opl_serial_port.cpp @@ -18,10 +18,10 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "opl_serial_port.h" #ifdef ENABLE_HW_OPL_SERIAL_PORT +#include "opl_serial_port.h" #include "opl_serial_misc.h" @@ -68,19 +68,19 @@ static size_t retrowave_protocol_serial_pack(const uint8_t *buf_in, size_t len_i } OPL_SerialPort::OPL_SerialPort() - : m_port(nullptr), m_protocol(ProtocolUnknown) + : m_port(NULL), m_protocol(ProtocolUnknown) {} OPL_SerialPort::~OPL_SerialPort() { delete m_port; - m_port = nullptr; + m_port = NULL; } bool OPL_SerialPort::connectPort(const std::string& name, unsigned baudRate, unsigned protocol) { delete m_port; - m_port = nullptr; + m_port = NULL; // ensure audio thread reads protocol atomically and in order, // so chipType() will be correct after the port is live From 8e76071a1005b2f0aa940c2ffe59c6b11383befe Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Tue, 21 May 2024 13:51:46 +0300 Subject: [PATCH 09/13] Serial: Don't set speed on macOS It's need to set this differently. --- src/opl/chips/opl_serial_misc.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/opl/chips/opl_serial_misc.h b/src/opl/chips/opl_serial_misc.h index 84711c3..038955c 100644 --- a/src/opl/chips/opl_serial_misc.h +++ b/src/opl/chips/opl_serial_misc.h @@ -170,7 +170,12 @@ class ChipSerialPort : public ChipSerialPortBase m_portSetup.c_cflag &= ~CBAUD; #endif +#if defined (BSD) || defined(__FreeBSD__) + cfsetispeed(&m_portSetup, baudRate); + cfsetospeed(&m_portSetup, baudRate); +#elif !defined(__APPLE__) cfsetospeed(&m_portSetup, baud2enum(baudRate)); +#endif if(tcsetattr(m_port, TCSANOW, &m_portSetup) != 0) { From 370a22cc394e0be101f5ef9b68c966e852c5cb23 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Tue, 21 May 2024 16:57:35 +0300 Subject: [PATCH 10/13] QMake: Fixed the "C" letter in the test message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It was actually Cyrillic "С" (called 'Es') --- FMBankEdit.pro | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FMBankEdit.pro b/FMBankEdit.pro index 8b7d4ad..3e30f1c 100644 --- a/FMBankEdit.pro +++ b/FMBankEdit.pro @@ -30,10 +30,10 @@ greaterThan(QT_MAJOR_VERSION, 4):{ if(qtCompileTest(cpp14)) { CONFIG += c++14 CONFIG += enable_ymfm - message("TEST: С++14 support presented, YMFM will be ENABLED!") + message("TEST: C++14 support presented, YMFM will be ENABLED!") } else { CONFIG += c++11 - message("TEST: С++14 was not found, YMFM will be DISABLED!") + message("TEST: C++14 was not found, YMFM will be DISABLED!") } } else { QMAKE_CXXFLAGS += -std=c++11 From a2a097969415a8b459e5b21eead8a58980add2a5 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Thu, 23 May 2024 01:13:57 +0300 Subject: [PATCH 11/13] CMake: Don't link the chips library twice --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e692239..cb5848a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -174,7 +174,7 @@ set(MEASURER_SOURCES "src/opl/measurer.cpp") add_library(Measurer STATIC ${MEASURER_SOURCES}) target_include_directories(Measurer PUBLIC "src") -target_link_libraries(Measurer PUBLIC Chips Common Qt5::Concurrent) +target_link_libraries(Measurer PUBLIC Common Qt5::Concurrent) if(NOT MSVC AND NOT APPLE) target_compile_options(Measurer PRIVATE "-fopenmp") endif() @@ -455,5 +455,5 @@ endif() add_executable(measurer_tool "utils/measurer/measurer-tool.cpp") set_target_properties(measurer_tool PROPERTIES OUTPUT_NAME "measurer") -target_link_libraries(measurer_tool PRIVATE FileFormats Measurer) +target_link_libraries(measurer_tool PRIVATE FileFormats Measurer Chips) pge_set_nopie(measurer_tool) From 7f02a77f5381b6dfbaf4cbd8de7f17826f5f8bcb Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Thu, 23 May 2024 01:14:23 +0300 Subject: [PATCH 12/13] Serial: Do properly set the baud speed on macOS --- src/opl/chips/opl_serial_misc.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/opl/chips/opl_serial_misc.h b/src/opl/chips/opl_serial_misc.h index 038955c..7d46b76 100644 --- a/src/opl/chips/opl_serial_misc.h +++ b/src/opl/chips/opl_serial_misc.h @@ -28,6 +28,11 @@ #include #include #include +#include +#endif + +#ifdef __APPLE__ +#include #endif #ifdef _WIN32 @@ -185,6 +190,16 @@ class ChipSerialPort : public ChipSerialPortBase return false; } +#ifdef __APPLE__ + if(ioctl(m_port, IOSSIOSPEED, &baudRate) == -1) + { + std::fprintf(stderr, "-- OPL Serial ERROR: Failed to set MacOS specific tty attributes for `%s': %s", portPath.c_str(), strerror(errno)); + std::fflush(stderr); + close(); + return false; + } +#endif + return true; } From 333a7a941f437e9e8ec3e516dad2e3c07d6331c8 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Thu, 23 May 2024 01:43:29 +0300 Subject: [PATCH 13/13] Serial: Apply fixes done on libADLMIDI side --- src/opl/chips/opl_serial_misc.h | 11 ++++++----- src/opl/chips/opl_serial_port.cpp | 4 ++-- src/opl/chips/opl_serial_port.h | 6 +++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/opl/chips/opl_serial_misc.h b/src/opl/chips/opl_serial_misc.h index 7d46b76..2583a4b 100644 --- a/src/opl/chips/opl_serial_misc.h +++ b/src/opl/chips/opl_serial_misc.h @@ -48,19 +48,19 @@ class ChipSerialPortBase ChipSerialPortBase() {} virtual ~ChipSerialPortBase() {} - bool isOpen() + virtual bool isOpen() { return false; } - void close() {} + virtual void close() {} - bool open(const std::string &/*portName*/, unsigned /*baudRate*/) + virtual bool open(const std::string &/*portName*/, unsigned /*baudRate*/) { return false; } - int write(uint8_t */*data*/, size_t /*size*/) + virtual int write(uint8_t * /*data*/, size_t /*size*/) { return 0; } @@ -117,7 +117,8 @@ class ChipSerialPort : public ChipSerialPortBase } public: - ChipSerialPort() : ChipSerialPortBase() + ChipSerialPort() : + ChipSerialPortBase() { m_port = 0; std::memset(&m_portSetup, 0, sizeof(struct termios)); diff --git a/src/opl/chips/opl_serial_port.cpp b/src/opl/chips/opl_serial_port.cpp index 593a251..1607d8c 100644 --- a/src/opl/chips/opl_serial_port.cpp +++ b/src/opl/chips/opl_serial_port.cpp @@ -90,14 +90,14 @@ bool OPL_SerialPort::connectPort(const std::string& name, unsigned baudRate, uns // port->setBaudRate(baudRate); // return port->open(QSerialPort::WriteOnly); - m_port = new ChipSerialPort(); + m_port = new ChipSerialPort; return m_port->open(name, baudRate); } void OPL_SerialPort::writeReg(uint16_t addr, uint8_t data) { uint8_t sendBuffer[16]; - ChipSerialPort *port = m_port; + ChipSerialPortBase *port = m_port; if(!port || !port->isOpen()) return; diff --git a/src/opl/chips/opl_serial_port.h b/src/opl/chips/opl_serial_port.h index a449e32..346c68e 100644 --- a/src/opl/chips/opl_serial_port.h +++ b/src/opl/chips/opl_serial_port.h @@ -27,7 +27,7 @@ #include #include "opl_chip_base.h" -class ChipSerialPort; +class ChipSerialPortBase; class OPL_SerialPort : public OPLChipBaseT { @@ -40,7 +40,7 @@ class OPL_SerialPort : public OPLChipBaseT ProtocolUnknown, ProtocolArduinoOPL2, ProtocolNukeYktOPL3, - ProtocolRetroWaveOPL3, + ProtocolRetroWaveOPL3 }; bool connectPort(const std::string &name, unsigned baudRate, unsigned protocol); @@ -56,7 +56,7 @@ class OPL_SerialPort : public OPLChipBaseT ChipType chipType() override; private: - ChipSerialPort *m_port; + ChipSerialPortBase *m_port; int m_protocol; };