diff --git a/src/include/86box/snd_opl_nuked.h b/src/include/86box/snd_opl_nuked.h index e53f860f1..0b203fe31 100644 --- a/src/include/86box/snd_opl_nuked.h +++ b/src/include/86box/snd_opl_nuked.h @@ -20,4 +20,181 @@ #ifndef SOUND_OPL_NUKED_H #define SOUND_OPL_NUKED_H +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#ifndef OPL_ENABLE_STEREOEXT +#define OPL_ENABLE_STEREOEXT 0 +#endif + +#define OPL_WRITEBUF_SIZE 1024 +#define OPL_WRITEBUF_DELAY 2 + +typedef struct _opl3_slot opl3_slot; +typedef struct _opl3_channel opl3_channel; +typedef struct _opl3_chip opl3_chip; + +struct _opl3_slot { + opl3_channel *channel; + opl3_chip *chip; + int16_t out; + int16_t fbmod; + int16_t *mod; + int16_t prout; + uint16_t eg_rout; + uint16_t eg_out; + uint8_t eg_inc; + uint8_t eg_gen; + uint8_t eg_rate; + uint8_t eg_ksl; + uint8_t *trem; + uint8_t reg_vib; + uint8_t reg_type; + uint8_t reg_ksr; + uint8_t reg_mult; + uint8_t reg_ksl; + uint8_t reg_tl; + uint8_t reg_ar; + uint8_t reg_dr; + uint8_t reg_sl; + uint8_t reg_rr; + uint8_t reg_wf; + uint8_t key; + uint32_t pg_reset; + uint32_t pg_phase; + uint16_t pg_phase_out; + uint8_t slot_num; +}; + +struct _opl3_channel { + opl3_slot *slotz[2]; // Don't use "slots" keyword to avoid conflict with Qt applications + opl3_channel *pair; + opl3_chip *chip; + int16_t *out[4]; + +#if OPL_ENABLE_STEREOEXT + int32_t leftpan; + int32_t rightpan; +#endif + + uint8_t chtype; + uint16_t f_num; + uint8_t block; + uint8_t fb; + uint8_t con; + uint8_t alg; + uint8_t ksv; + uint16_t cha; + uint16_t chb; + uint16_t chc; + uint16_t chd; + uint8_t ch_num; +}; + +typedef struct _opl3_writebuf { + uint64_t time; + uint16_t reg; + uint8_t data; +} opl3_writebuf; + +struct _opl3_chip { + opl3_channel channel[18]; + opl3_slot slot[36]; + uint16_t timer; + uint64_t eg_timer; + uint8_t eg_timerrem; + uint8_t eg_state; + uint8_t eg_add; + uint8_t eg_timer_lo; + uint8_t newm; + uint8_t nts; + uint8_t rhy; + uint8_t vibpos; + uint8_t vibshift; + uint8_t tremolo; + uint8_t tremolopos; + uint8_t tremoloshift; + uint32_t noise; + int16_t zeromod; + int32_t mixbuff[4]; + uint8_t rm_hh_bit2; + uint8_t rm_hh_bit3; + uint8_t rm_hh_bit7; + uint8_t rm_hh_bit8; + uint8_t rm_tc_bit3; + uint8_t rm_tc_bit5; + +#if OPL_ENABLE_STEREOEXT + uint8_t stereoext; +#endif + + // OPL3L + int32_t rateratio; + int32_t samplecnt; + int32_t oldsamples[4]; + int32_t samples[4]; + + uint64_t writebuf_samplecnt; + uint32_t writebuf_cur; + uint32_t writebuf_last; + uint64_t writebuf_lasttime; + opl3_writebuf writebuf[OPL_WRITEBUF_SIZE]; +}; + +typedef struct { + opl3_chip opl; + int8_t flags; + int8_t pad; + + uint16_t port; + uint8_t status; + uint8_t timer_ctrl; + uint16_t timer_count[2]; + uint16_t timer_cur_count[2]; + + pc_timer_t timers[2]; + + int pos; + int32_t buffer[MUSICBUFLEN * 2]; +} nuked_drv_t; + +enum { + FLAG_CYCLES = 0x02, + FLAG_OPL3 = 0x01 +}; + +enum { + STAT_TMR_OVER = 0x60, + STAT_TMR1_OVER = 0x40, + STAT_TMR2_OVER = 0x20, + STAT_TMR_ANY = 0x80 +}; + +enum { + CTRL_RESET = 0x80, + CTRL_TMR_MASK = 0x60, + CTRL_TMR1_MASK = 0x40, + CTRL_TMR2_MASK = 0x20, + CTRL_TMR2_START = 0x02, + CTRL_TMR1_START = 0x01 +}; + +void OPL3_Generate(opl3_chip *chip, int32_t *buf); +void OPL3_GenerateResampled(opl3_chip *chip, int32_t *buf); +void OPL3_Reset(opl3_chip *chip, uint32_t samplerate); +void OPL3_WriteReg(void *priv, uint16_t reg, uint8_t val); +void OPL3_WriteRegBuffered(void *priv, uint16_t reg, uint8_t val); +void OPL3_GenerateStream(opl3_chip *chip, int32_t *sndptr, uint32_t numsamples); + +static void OPL3_Generate4Ch(void *priv, int32_t *buf4); +void OPL3_Generate4Ch_Resampled(opl3_chip *chip, int32_t *buf4); +void OPL3_Generate4Ch_Stream(opl3_chip *chip, int32_t *sndptr1, int32_t *sndptr2, uint32_t numsamples); + +#ifdef __cplusplus +} +#endif + #endif /*SOUND_OPL_NUKED_H*/ diff --git a/src/include/slirp/libslirp-version.h b/src/include/slirp/libslirp-version.h deleted file mode 100644 index b68906957..000000000 --- a/src/include/slirp/libslirp-version.h +++ /dev/null @@ -1,24 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause */ -#ifndef LIBSLIRP_VERSION_H_ -#define LIBSLIRP_VERSION_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#define SLIRP_MAJOR_VERSION 4 -#define SLIRP_MINOR_VERSION 7 -#define SLIRP_MICRO_VERSION 0 -#define SLIRP_VERSION_STRING "4.7.0-86Box" - -#define SLIRP_CHECK_VERSION(major,minor,micro) \ - (SLIRP_MAJOR_VERSION > (major) || \ - (SLIRP_MAJOR_VERSION == (major) && SLIRP_MINOR_VERSION > (minor)) || \ - (SLIRP_MAJOR_VERSION == (major) && SLIRP_MINOR_VERSION == (minor) && \ - SLIRP_MICRO_VERSION >= (micro))) - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* LIBSLIRP_VERSION_H_ */ diff --git a/src/include/slirp/libslirp.h b/src/include/slirp/libslirp.h deleted file mode 100644 index 7a6c9a4da..000000000 --- a/src/include/slirp/libslirp.h +++ /dev/null @@ -1,273 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause */ -#ifndef LIBSLIRP_H -#define LIBSLIRP_H - -#include -#include -#include - -#ifdef _WIN32 -#include -#include -#include -#else -#include -#include -#endif - -#include "libslirp-version.h" - -/* Windows does not define ssize_t, so we need to define it here. */ -#ifndef _SSIZE_T_DEFINED -# define _SSIZE_T_DEFINED -# undef ssize_t -# ifdef _WIN64 -# define ssize_t int64_t -# else -# define ssize_t int32_t -# endif -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* Opaque structure containing the slirp state */ -typedef struct Slirp Slirp; - -/* Flags passed to SlirpAddPollCb and to be returned by SlirpGetREventsCb. */ -enum { - SLIRP_POLL_IN = 1 << 0, - SLIRP_POLL_OUT = 1 << 1, - SLIRP_POLL_PRI = 1 << 2, - SLIRP_POLL_ERR = 1 << 3, - SLIRP_POLL_HUP = 1 << 4, -}; - -typedef ssize_t (*SlirpReadCb)(void *buf, size_t len, void *opaque); -typedef ssize_t (*SlirpWriteCb)(const void *buf, size_t len, void *opaque); -typedef void (*SlirpTimerCb)(void *opaque); -typedef int (*SlirpAddPollCb)(int fd, int events, void *opaque); -typedef int (*SlirpGetREventsCb)(int idx, void *opaque); - -typedef enum SlirpTimerId { - SLIRP_TIMER_RA, - SLIRP_TIMER_NUM, -} SlirpTimerId; - -/* - * Callbacks from slirp, to be set by the application. - * - * The opaque parameter is set to the opaque pointer given in the slirp_new / - * slirp_init call. - */ -typedef struct SlirpCb { - /* - * Send an ethernet frame to the guest network. The opaque parameter is the - * one given to slirp_init(). If the guest is not ready to receive a frame, - * the function can just drop the data. TCP will then handle retransmissions - * at a lower pace. - * <0 reports an IO error. - */ - SlirpWriteCb send_packet; - /* Print a message for an error due to guest misbehavior. */ - void (*guest_error)(const char *msg, void *opaque); - /* Return the virtual clock value in nanoseconds */ - int64_t (*clock_get_ns)(void *opaque); - /* Create a new timer with the given callback and opaque data. Not - * needed if timer_new_opaque is provided. */ - void *(*timer_new)(SlirpTimerCb cb, void *cb_opaque, void *opaque); - /* Remove and free a timer */ - void (*timer_free)(void *timer, void *opaque); - /* Modify a timer to expire at @expire_time (ms) */ - void (*timer_mod)(void *timer, int64_t expire_time, void *opaque); - /* Register a fd for future polling */ - void (*register_poll_fd)(int fd, void *opaque); - /* Unregister a fd */ - void (*unregister_poll_fd)(int fd, void *opaque); - /* Kick the io-thread, to signal that new events may be processed because some TCP buffer - * can now receive more data, i.e. slirp_socket_can_recv will return 1. */ - void (*notify)(void *opaque); - - /* - * Fields introduced in SlirpConfig version 4 begin - */ - - /* Initialization has completed and a Slirp* has been created. */ - void (*init_completed)(Slirp *slirp, void *opaque); - /* Create a new timer. When the timer fires, the application passes - * the SlirpTimerId and cb_opaque to slirp_handle_timer. */ - void *(*timer_new_opaque)(SlirpTimerId id, void *cb_opaque, void *opaque); -} SlirpCb; - -#define SLIRP_CONFIG_VERSION_MIN 1 -#define SLIRP_CONFIG_VERSION_MAX 4 - -typedef struct SlirpConfig { - /* Version must be provided */ - uint32_t version; - /* - * Fields introduced in SlirpConfig version 1 begin - */ - int restricted; - bool in_enabled; - struct in_addr vnetwork; - struct in_addr vnetmask; - struct in_addr vhost; - bool in6_enabled; - struct in6_addr vprefix_addr6; - uint8_t vprefix_len; - struct in6_addr vhost6; - const char *vhostname; - const char *tftp_server_name; - const char *tftp_path; - const char *bootfile; - struct in_addr vdhcp_start; - struct in_addr vnameserver; - struct in6_addr vnameserver6; - const char **vdnssearch; - const char *vdomainname; - /* Default: IF_MTU_DEFAULT */ - size_t if_mtu; - /* Default: IF_MRU_DEFAULT */ - size_t if_mru; - /* Prohibit connecting to 127.0.0.1:* */ - bool disable_host_loopback; - /* - * Enable emulation code (*warning*: this code isn't safe, it is not - * recommended to enable it) - */ - bool enable_emu; - /* - * Fields introduced in SlirpConfig version 2 begin - */ - struct sockaddr_in *outbound_addr; - struct sockaddr_in6 *outbound_addr6; - /* - * Fields introduced in SlirpConfig version 3 begin - */ - bool disable_dns; /* slirp will not redirect/serve any DNS packet */ - /* - * Fields introduced in SlirpConfig version 4 begin - */ - bool disable_dhcp; /* slirp will not reply to any DHCP requests */ -} SlirpConfig; - -/* Create a new instance of a slirp stack */ -Slirp *slirp_new(const SlirpConfig *cfg, const SlirpCb *callbacks, - void *opaque); -/* slirp_init is deprecated in favor of slirp_new */ -Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork, - struct in_addr vnetmask, struct in_addr vhost, - bool in6_enabled, struct in6_addr vprefix_addr6, - uint8_t vprefix_len, struct in6_addr vhost6, - const char *vhostname, const char *tftp_server_name, - const char *tftp_path, const char *bootfile, - struct in_addr vdhcp_start, struct in_addr vnameserver, - struct in6_addr vnameserver6, const char **vdnssearch, - const char *vdomainname, const SlirpCb *callbacks, - void *opaque); -/* Shut down an instance of a slirp stack */ -void slirp_cleanup(Slirp *slirp); - -/* This is called by the application when it is about to sleep through poll(). - * *timeout is set to the amount of virtual time (in ms) that the application intends to - * wait (UINT32_MAX if infinite). slirp_pollfds_fill updates it according to - * e.g. TCP timers, so the application knows it should sleep a smaller amount of - * time. slirp_pollfds_fill calls add_poll for each file descriptor - * that should be monitored along the sleep. The opaque pointer is passed as - * such to add_poll, and add_poll returns an index. */ -void slirp_pollfds_fill(Slirp *slirp, uint32_t *timeout, - SlirpAddPollCb add_poll, void *opaque); - -/* This is called by the application after sleeping, to report which file - * descriptors are available. slirp_pollfds_poll calls get_revents on each file - * descriptor, giving it the index that add_poll returned during the - * slirp_pollfds_fill call, to know whether the descriptor is available for - * read/write/etc. (SLIRP_POLL_*) - * select_error should be passed 1 if poll() returned an error. */ -void slirp_pollfds_poll(Slirp *slirp, int select_error, - SlirpGetREventsCb get_revents, void *opaque); - -/* This is called by the application when the guest emits a packet on the - * guest network, to be interpreted by slirp. */ -void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len); - -/* This is called by the application when a timer expires, if it provides - * the timer_new_opaque callback. It is not needed if the application only - * uses timer_new. */ -void slirp_handle_timer(Slirp *slirp, SlirpTimerId id, void *cb_opaque); - -/* These set up / remove port forwarding between a host port in the real world - * and the guest network. */ -int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, - int host_port, struct in_addr guest_addr, int guest_port); -int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, - int host_port); - -#define SLIRP_HOSTFWD_UDP 1 -#define SLIRP_HOSTFWD_V6ONLY 2 -int slirp_add_hostxfwd(Slirp *slirp, - const struct sockaddr *haddr, socklen_t haddrlen, - const struct sockaddr *gaddr, socklen_t gaddrlen, - int flags); -int slirp_remove_hostxfwd(Slirp *slirp, - const struct sockaddr *haddr, socklen_t haddrlen, - int flags); - -/* Set up port forwarding between a port in the guest network and a - * command running on the host */ -int slirp_add_exec(Slirp *slirp, const char *cmdline, - struct in_addr *guest_addr, int guest_port); -/* Set up port forwarding between a port in the guest network and a - * Unix port on the host */ -int slirp_add_unix(Slirp *slirp, const char *unixsock, - struct in_addr *guest_addr, int guest_port); -/* Set up port forwarding between a port in the guest network and a - * callback that will receive the data coming from the port */ -int slirp_add_guestfwd(Slirp *slirp, SlirpWriteCb write_cb, void *opaque, - struct in_addr *guest_addr, int guest_port); - -/* TODO: rather identify a guestfwd through an opaque pointer instead of through - * the guest_addr */ - -/* This is called by the application for a guestfwd, to determine how much data - * can be received by the forwarded port through a call to slirp_socket_recv. */ -size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr, - int guest_port); -/* This is called by the application for a guestfwd, to provide the data to be - * sent on the forwarded port */ -void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port, - const uint8_t *buf, int size); - -/* Remove entries added by slirp_add_exec, slirp_add_unix or slirp_add_guestfwd */ -int slirp_remove_guestfwd(Slirp *slirp, struct in_addr guest_addr, - int guest_port); - -/* Return a human-readable state of the slirp stack */ -char *slirp_connection_info(Slirp *slirp); - -/* Return a human-readable state of the NDP/ARP tables */ -char *slirp_neighbor_info(Slirp *slirp); - -/* Save the slirp state through the write_cb. The opaque pointer is passed as - * such to the write_cb. */ -void slirp_state_save(Slirp *s, SlirpWriteCb write_cb, void *opaque); - -/* Returns the version of the slirp state, to be saved along the state */ -int slirp_state_version(void); - -/* Load the slirp state through the read_cb. The opaque pointer is passed as - * such to the read_cb. The version should be given as it was obtained from - * slirp_state_version when slirp_state_save was called. */ -int slirp_state_load(Slirp *s, int version_id, SlirpReadCb read_cb, - void *opaque); - -/* Return the version of the slirp implementation */ -const char *slirp_version_string(void); - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* LIBSLIRP_H */ diff --git a/src/network/CMakeLists.txt b/src/network/CMakeLists.txt index e407d4364..0783e4b4c 100644 --- a/src/network/CMakeLists.txt +++ b/src/network/CMakeLists.txt @@ -23,6 +23,9 @@ target_link_libraries(86Box PkgConfig::SLIRP) if(WIN32) target_link_libraries(PkgConfig::SLIRP INTERFACE wsock32 ws2_32 iphlpapi iconv) + if(STATIC_BUILD) + add_compile_definitions(LIBSLIRP_STATIC) + endif() endif() if (HAIKU) diff --git a/src/network/net_slirp.c b/src/network/net_slirp.c index 599ee896d..92434973d 100644 --- a/src/network/net_slirp.c +++ b/src/network/net_slirp.c @@ -171,7 +171,11 @@ net_slirp_notify(void *opaque) (void) opaque; } +#if SLIRP_CHECK_VERSION(4, 8, 0) +slirp_ssize_t +#else ssize_t +#endif net_slirp_send_packet(const void *qp, size_t pkt_len, void *opaque) { net_slirp_t *slirp = (net_slirp_t *) opaque; diff --git a/src/sound/CMakeLists.txt b/src/sound/CMakeLists.txt index a05481228..2d57a42e0 100644 --- a/src/sound/CMakeLists.txt +++ b/src/sound/CMakeLists.txt @@ -90,8 +90,12 @@ if(FLUIDSYNTH) target_link_libraries(86Box PkgConfig::FLUIDSYNTH) if(STATIC_BUILD) target_link_libraries(86Box -static ${FLUIDSYNTH_STATIC_LIBRARIES} -fopenmp) - if(WIN32 AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64") - target_link_libraries(86Box psapi) + if(WIN32) + add_compile_definitions(FLUIDSYNTH_NOT_A_DLL) + + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64") + target_link_libraries(86Box psapi) + endif() endif() endif() diff --git a/src/sound/midi_fluidsynth.c b/src/sound/midi_fluidsynth.c index c1b9956d0..f8c7964f7 100644 --- a/src/sound/midi_fluidsynth.c +++ b/src/sound/midi_fluidsynth.c @@ -7,7 +7,6 @@ #ifdef __unix__ # include #endif -#define FLUIDSYNTH_NOT_A_DLL #include #include <86box/86box.h> diff --git a/src/sound/resid-fp/WaveformGenerator.cpp b/src/sound/resid-fp/WaveformGenerator.cpp index 207431c94..4c7a55b3d 100644 --- a/src/sound/resid-fp/WaveformGenerator.cpp +++ b/src/sound/resid-fp/WaveformGenerator.cpp @@ -43,7 +43,7 @@ namespace reSIDfp const unsigned int FLOATING_OUTPUT_TTL_6581R3 = 54000; const unsigned int FLOATING_OUTPUT_FADE_6581R3 = 1400; // ~1s -const unsigned int FLOATING_OUTPUT_TTL_6581R4 = 1000000; +//const unsigned int FLOATING_OUTPUT_TTL_6581R4 = 1000000; // ~1s const unsigned int FLOATING_OUTPUT_TTL_8580R5 = 800000; const unsigned int FLOATING_OUTPUT_FADE_8580R5 = 50000; @@ -61,7 +61,7 @@ const unsigned int FLOATING_OUTPUT_FADE_8580R5 = 50000; const unsigned int SHIFT_REGISTER_RESET_6581R3 = 50000; const unsigned int SHIFT_REGISTER_FADE_6581R3 = 15000; // ~2.15s -const unsigned int SHIFT_REGISTER_RESET_6581R4 = 2150000; +//const unsigned int SHIFT_REGISTER_RESET_6581R4 = 2150000; // ~2.8s const unsigned int SHIFT_REGISTER_RESET_8580R5 = 986000; const unsigned int SHIFT_REGISTER_FADE_8580R5 = 314300; diff --git a/src/sound/snd_cmi8x38.c b/src/sound/snd_cmi8x38.c index b1f56f775..d548763ce 100644 --- a/src/sound/snd_cmi8x38.c +++ b/src/sound/snd_cmi8x38.c @@ -860,7 +860,7 @@ cmi8x38_write(uint16_t addr, uint8_t val, void *priv) case 0x1b: if (dev->type == CMEDIA_CMI8338) - val &= 0xf0; + val &= 0xf4; /* bit 2 reserved, mpxplay driver expects writable */ else val &= 0xd7; break; @@ -909,6 +909,24 @@ cmi8x38_write(uint16_t addr, uint8_t val, void *priv) dev->sb->opl.write(addr, val, dev->sb->opl.priv); return; + case 0x80 ... 0x83: + case 0x88 ... 0x8b: + dev->io_regs[addr] = val; + dev->dma[(addr & 0x78) >> 3].sample_ptr = *((uint32_t *) &dev->io_regs[addr & 0xfc]); + return; + + case 0x84 ... 0x85: + case 0x8c ... 0x8d: + dev->io_regs[addr] = val; + dev->dma[(addr & 0x78) >> 3].frame_count_dma = dev->dma[(addr & 0x78) >> 3].sample_count_out = *((uint16_t *) &dev->io_regs[addr & 0xfe]) + 1; + return; + + case 0x86 ... 0x87: + case 0x8e ... 0x8f: + dev->io_regs[addr] = val; + dev->dma[(addr & 0x78) >> 3].frame_count_fragment = *((uint16_t *) &dev->io_regs[addr & 0xfe]) + 1; + return; + case 0x92: if (dev->type == CMEDIA_CMI8338) return; @@ -927,7 +945,6 @@ cmi8x38_write(uint16_t addr, uint8_t val, void *priv) case 0x26: case 0x70: case 0x71: - case 0x80 ... 0x8f: break; default: diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 95b61638e..f1132867b 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -18,7 +18,7 @@ * siliconpr0n.org(John McMaster, digshadow): * YMF262 and VRC VII decaps and die shots. * - * Version: 1.8.0 + * Version: 1.8 * * Translation from C++ into C done by Miran Grca. * @@ -35,7 +35,7 @@ * * Copyright 2017-2020 Fred N. van Kempen. * Copyright 2016-2020 Miran Grca. - * Copyright 2013-2018 Alexey Khokholov (Nuke.YKT) + * Copyright 2013-2020 Alexey Khokholov (Nuke.YKT) */ #include #include @@ -45,14 +45,27 @@ #define HAVE_STDARG_H #include <86box/86box.h> -#include <86box/snd_opl_nuked.h> #include <86box/sound.h> #include <86box/timer.h> #include <86box/device.h> #include <86box/snd_opl.h> +#include <86box/snd_opl_nuked.h> + + +#if OPL_ENABLE_STEREOEXT && !defined OPL_SIN +#ifndef _USE_MATH_DEFINES +#define _USE_MATH_DEFINES 1 +#endif +#include +// input: [0, 256), output: [0, 65536] +#define OPL_SIN(x) ((int32_t)(sin((x) * M_PI / 512.0) * 65536.0)) +#endif + +/* Quirk: Some FM channels are output one sample later on the left side than the right. */ +#ifndef OPL_QUIRK_CHANNELSAMPLEDELAY +#define OPL_QUIRK_CHANNELSAMPLEDELAY (!OPL_ENABLE_STEREOEXT) +#endif -#define WRBUF_SIZE 1024 -#define WRBUF_DELAY 1 #define RSM_FRAC 10 // #define OPL_FREQ FREQ_48000 @@ -72,148 +85,6 @@ enum { egk_drum = 0x02 }; -enum envelope_gen_num { - envelope_gen_num_attack = 0, - envelope_gen_num_decay = 1, - envelope_gen_num_sustain = 2, - envelope_gen_num_release = 3 -}; - -struct chan; -struct chip; - -typedef struct slot { - struct chan *chan; - struct chip *dev; - int16_t out; - int16_t fbmod; - int16_t *mod; - int16_t prout; - int16_t eg_rout; - int16_t eg_out; - uint8_t eg_inc; - uint8_t eg_gen; - uint8_t eg_rate; - uint8_t eg_ksl; - uint8_t *trem; - uint8_t reg_vib; - uint8_t reg_type; - uint8_t reg_ksr; - uint8_t reg_mult; - uint8_t reg_ksl; - uint8_t reg_tl; - uint8_t reg_ar; - uint8_t reg_dr; - uint8_t reg_sl; - uint8_t reg_rr; - uint8_t reg_wf; - uint8_t key; - uint32_t pg_reset; - uint32_t pg_phase; - uint16_t pg_phase_out; - uint8_t slot_num; -} slot_t; - -typedef struct chan { - slot_t *slots[2]; - struct chan *pair; - struct chip *dev; - int16_t *out[4]; - uint8_t chtype; - uint16_t f_num; - uint8_t block; - uint8_t fb; - uint8_t con; - uint8_t alg; - uint8_t ksv; - uint16_t cha; - uint16_t chb; - uint8_t ch_num; -} chan_t; - -typedef struct wrbuf { - uint64_t time; - uint16_t reg; - uint8_t data; -} wrbuf_t; - -typedef struct chip { - chan_t chan[18]; - slot_t slot[36]; - uint16_t timer; - uint64_t eg_timer; - uint8_t eg_timerrem; - uint8_t eg_state; - uint8_t eg_add; - uint8_t newm; - uint8_t nts; - uint8_t rhy; - uint8_t vibpos; - uint8_t vibshift; - uint8_t tremolo; - uint8_t tremolopos; - uint8_t tremoloshift; - uint32_t noise; - int16_t zeromod; - int32_t mixbuff[2]; - uint8_t rm_hh_bit2; - uint8_t rm_hh_bit3; - uint8_t rm_hh_bit7; - uint8_t rm_hh_bit8; - uint8_t rm_tc_bit3; - uint8_t rm_tc_bit5; - - // OPL3L - int32_t rateratio; - int32_t samplecnt; - int32_t oldsamples[2]; - int32_t samples[2]; - - uint64_t wrbuf_samplecnt; - uint32_t wrbuf_cur; - uint32_t wrbuf_last; - uint64_t wrbuf_lasttime; - wrbuf_t wrbuf[WRBUF_SIZE]; -} nuked_t; - -typedef struct { - nuked_t opl; - int8_t flags; - int8_t pad; - - uint16_t port; - uint8_t status; - uint8_t timer_ctrl; - uint16_t timer_count[2]; - uint16_t timer_cur_count[2]; - - pc_timer_t timers[2]; - - int pos; - int32_t buffer[MUSICBUFLEN * 2]; -} nuked_drv_t; - -enum { - FLAG_CYCLES = 0x02, - FLAG_OPL3 = 0x01 -}; - -enum { - STAT_TMR_OVER = 0x60, - STAT_TMR1_OVER = 0x40, - STAT_TMR2_OVER = 0x20, - STAT_TMR_ANY = 0x80 -}; - -enum { - CTRL_RESET = 0x80, - CTRL_TMR_MASK = 0x60, - CTRL_TMR1_MASK = 0x40, - CTRL_TMR2_MASK = 0x20, - CTRL_TMR2_START = 0x02, - CTRL_TMR1_START = 0x01 -}; - #ifdef ENABLE_OPL_LOG int nuked_do_log = ENABLE_OPL_LOG; @@ -322,10 +193,10 @@ static const uint8_t kslshift[4] = { // envelope generator constants static const uint8_t eg_incstep[4][4] = { - {0, 0, 0, 0}, - { 1, 0, 0, 0}, - { 1, 0, 1, 0}, - { 1, 1, 1, 0} + { 0, 0, 0, 0 }, + { 1, 0, 0, 0 }, + { 1, 0, 1, 0 }, + { 1, 1, 1, 0 } }; // address decoding @@ -338,21 +209,30 @@ static const uint8_t ch_slot[18] = { 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32 }; +#if OPL_ENABLE_STEREOEXT +/* + stereo extension panning table +*/ + +static int32_t panpot_lut[256]; +static uint8_t panpot_lut_build = 0; +#endif + // Envelope generator -typedef int16_t (*env_sinfunc)(uint16_t phase, uint16_t envelope); -typedef void (*env_genfunc)(slot_t *slot); +typedef int16_t (*envelope_sinfunc)(uint16_t phase, uint16_t envelope); +typedef void (*envelope_genfunc)(opl3_slot *slot); static int16_t -env_calc_exp(uint32_t level) +OPL3_EnvelopeCalcExp(uint32_t level) { if (level > 0x1fff) level = 0x1fff; - return ((exprom[level & 0xff] << 1) >> (level >> 8)); + return ((exprom[level & 0xffu] << 1) >> (level >> 8)); } static int16_t -env_calc_sin0(uint16_t phase, uint16_t env) +OPL3_EnvelopeCalcSin0(uint16_t phase, uint16_t envelope) { uint16_t out = 0; uint16_t neg = 0; @@ -363,15 +243,15 @@ env_calc_sin0(uint16_t phase, uint16_t env) neg = 0xffff; if (phase & 0x0100) - out = logsinrom[(phase & 0xff) ^ 0xff]; + out = logsinrom[(phase & 0xffu) ^ 0xffu]; else - out = logsinrom[phase & 0xff]; + out = logsinrom[phase & 0xffu]; - return (env_calc_exp(out + (env << 3)) ^ neg); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg); } static int16_t -env_calc_sin1(uint16_t phase, uint16_t env) +OPL3_EnvelopeCalcSin1(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -380,30 +260,30 @@ env_calc_sin1(uint16_t phase, uint16_t env) if (phase & 0x0200) out = 0x1000; else if (phase & 0x0100) - out = logsinrom[(phase & 0xff) ^ 0xff]; + out = logsinrom[(phase & 0xffu) ^ 0xffu]; else - out = logsinrom[phase & 0xff]; + out = logsinrom[phase & 0xffu]; - return (env_calc_exp(out + (env << 3))); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3))); } static int16_t -env_calc_sin2(uint16_t phase, uint16_t env) +OPL3_EnvelopeCalcSin2(uint16_t phase, uint16_t envelope) { uint16_t out = 0; phase &= 0x03ff; if (phase & 0x0100) - out = logsinrom[(phase & 0xff) ^ 0xff]; + out = logsinrom[(phase & 0xffu) ^ 0xffu]; else - out = logsinrom[phase & 0xff]; + out = logsinrom[phase & 0xffu]; - return (env_calc_exp(out + (env << 3))); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3))); } static int16_t -env_calc_sin3(uint16_t phase, uint16_t env) +OPL3_EnvelopeCalcSin3(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -412,13 +292,13 @@ env_calc_sin3(uint16_t phase, uint16_t env) if (phase & 0x0100) out = 0x1000; else - out = logsinrom[phase & 0xff]; + out = logsinrom[phase & 0xffu]; - return (env_calc_exp(out + (env << 3))); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3))); } static int16_t -env_calc_sin4(uint16_t phase, uint16_t env) +OPL3_EnvelopeCalcSin4(uint16_t phase, uint16_t envelope) { uint16_t out = 0; uint16_t neg = 0; @@ -431,15 +311,15 @@ env_calc_sin4(uint16_t phase, uint16_t env) if (phase & 0x0200) out = 0x1000; else if (phase & 0x80) - out = logsinrom[((phase ^ 0xff) << 1) & 0xff]; + out = logsinrom[((phase ^ 0xffu) << 1u) & 0xffu]; else - out = logsinrom[(phase << 1) & 0xff]; + out = logsinrom[(phase << 1u) & 0xffu]; - return (env_calc_exp(out + (env << 3)) ^ neg); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg); } static int16_t -env_calc_sin5(uint16_t phase, uint16_t env) +OPL3_EnvelopeCalcSin5(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -448,15 +328,15 @@ env_calc_sin5(uint16_t phase, uint16_t env) if (phase & 0x0200) out = 0x1000; else if (phase & 0x80) - out = logsinrom[((phase ^ 0xff) << 1) & 0xff]; + out = logsinrom[((phase ^ 0xffu) << 1u) & 0xffu]; else - out = logsinrom[(phase << 1) & 0xff]; + out = logsinrom[(phase << 1u) & 0xffu]; - return (env_calc_exp(out + (env << 3))); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3))); } static int16_t -env_calc_sin6(uint16_t phase, uint16_t env) +OPL3_EnvelopeCalcSin6(uint16_t phase, uint16_t envelope) { uint16_t neg = 0; @@ -465,11 +345,11 @@ env_calc_sin6(uint16_t phase, uint16_t env) if (phase & 0x0200) neg = 0xffff; - return (env_calc_exp(env << 3) ^ neg); + return (OPL3_EnvelopeCalcExp(envelope << 3) ^ neg); } static int16_t -env_calc_sin7(uint16_t phase, uint16_t env) +OPL3_EnvelopeCalcSin7(uint16_t phase, uint16_t envelope) { uint16_t out = 0; uint16_t neg = 0; @@ -483,24 +363,32 @@ env_calc_sin7(uint16_t phase, uint16_t env) out = phase << 3; - return (env_calc_exp(out + (env << 3)) ^ neg); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg); } -static const env_sinfunc env_sin[8] = { - env_calc_sin0, - env_calc_sin1, - env_calc_sin2, - env_calc_sin3, - env_calc_sin4, - env_calc_sin5, - env_calc_sin6, - env_calc_sin7 +static const envelope_sinfunc envelope_sin[8] = { + OPL3_EnvelopeCalcSin0, + OPL3_EnvelopeCalcSin1, + OPL3_EnvelopeCalcSin2, + OPL3_EnvelopeCalcSin3, + OPL3_EnvelopeCalcSin4, + OPL3_EnvelopeCalcSin5, + OPL3_EnvelopeCalcSin6, + OPL3_EnvelopeCalcSin7 +}; + +enum envelope_gen_num { + envelope_gen_num_attack = 0, + envelope_gen_num_decay = 1, + envelope_gen_num_sustain = 2, + envelope_gen_num_release = 3 }; static void -env_update_ksl(slot_t *slot) +OPL3_EnvelopeUpdateKSL(opl3_slot *slot) { - int16_t ksl = (kslrom[slot->chan->f_num >> 6] << 2) - ((0x08 - slot->chan->block) << 5); + int16_t ksl = (kslrom[slot->channel->f_num >> 6u] << 2) + - ((0x08 - slot->channel->block) << 5); if (ksl < 0) ksl = 0; @@ -509,7 +397,7 @@ env_update_ksl(slot_t *slot) } static void -env_calc(slot_t *slot) +OPL3_EnvelopeCalc(opl3_slot *slot) { uint8_t nonzero; uint8_t rate; @@ -524,7 +412,8 @@ env_calc(slot_t *slot) uint8_t eg_off; uint8_t reset = 0; - slot->eg_out = slot->eg_rout + (slot->reg_tl << 2) + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem; + slot->eg_out = slot->eg_rout + (slot->reg_tl << 2) + + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem; if (slot->key && slot->eg_gen == envelope_gen_num_release) { reset = 1; reg_rate = slot->reg_ar; @@ -552,19 +441,21 @@ env_calc(slot_t *slot) } slot->pg_reset = reset; - ks = slot->chan->ksv >> ((slot->reg_ksr ^ 1) << 1); + ks = slot->channel->ksv >> ((slot->reg_ksr ^ 1) << 1); nonzero = (reg_rate != 0); rate = ks + (reg_rate << 2); rate_hi = rate >> 2; rate_lo = rate & 0x03; + if (rate_hi & 0x10) rate_hi = 0x0f; - eg_shift = rate_hi + slot->dev->eg_add; + + eg_shift = rate_hi + slot->chip->eg_add; shift = 0; if (nonzero) { if (rate_hi < 12) { - if (slot->dev->eg_state) + if (slot->chip->eg_state) switch (eg_shift) { case 12: shift = 1; @@ -582,11 +473,11 @@ env_calc(slot_t *slot) break; } } else { - shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->dev->timer & 0x03]; + shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->chip->eg_timer_lo]; if (shift & 0x04) shift = 0x03; if (!shift) - shift = slot->dev->eg_state; + shift = slot->chip->eg_state; } } @@ -610,7 +501,7 @@ env_calc(slot_t *slot) if (!slot->eg_rout) slot->eg_gen = envelope_gen_num_decay; else if (slot->key && shift > 0 && rate_hi != 0x0f) - eg_inc = ((~slot->eg_rout) << shift) >> 4; + eg_inc = ~slot->eg_rout >> (4 - shift); break; case envelope_gen_num_decay: @@ -640,48 +531,50 @@ env_calc(slot_t *slot) } static void -env_key_on(slot_t *slot, uint8_t type) +OPL3_EnvelopeKeyOn(opl3_slot *slot, uint8_t type) { slot->key |= type; } static void -env_key_off(slot_t *slot, uint8_t type) +OPL3_EnvelopeKeyOff(opl3_slot *slot, uint8_t type) { slot->key &= ~type; } +// Phase Generator static void -phase_generate(slot_t *slot) +OPL3_PhaseGenerate(opl3_slot *slot) { - uint16_t f_num; - uint32_t basefreq; - uint8_t rm_xor; - uint8_t n_bit; - uint32_t noise; - uint16_t phase; - int8_t range; - uint8_t vibpos; - nuked_t *dev; + opl3_chip *chip; + uint16_t f_num; + uint32_t basefreq; + uint8_t rm_xor; + uint8_t n_bit; + uint32_t noise; + uint16_t phase; - dev = slot->dev; - f_num = slot->chan->f_num; + chip = slot->chip; + f_num = slot->channel->f_num; if (slot->reg_vib) { + int8_t range; + uint8_t vibpos; + range = (f_num >> 7) & 7; - vibpos = dev->vibpos; + vibpos = chip->vibpos; if (!(vibpos & 3)) range = 0; else if (vibpos & 1) range >>= 1; - range >>= dev->vibshift; + range >>= chip->vibshift; if (vibpos & 4) range = -range; f_num += range; } - basefreq = (f_num << slot->chan->block) >> 1; + basefreq = (f_num << slot->channel->block) >> 1; phase = (uint16_t) (slot->pg_phase >> 9); if (slot->pg_reset) @@ -689,20 +582,22 @@ phase_generate(slot_t *slot) slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1; // Rhythm mode - noise = dev->noise; + noise = chip->noise; slot->pg_phase_out = phase; if (slot->slot_num == 13) { // hh - dev->rm_hh_bit2 = (phase >> 2) & 1; - dev->rm_hh_bit3 = (phase >> 3) & 1; - dev->rm_hh_bit7 = (phase >> 7) & 1; - dev->rm_hh_bit8 = (phase >> 8) & 1; + chip->rm_hh_bit2 = (phase >> 2) & 1; + chip->rm_hh_bit3 = (phase >> 3) & 1; + chip->rm_hh_bit7 = (phase >> 7) & 1; + chip->rm_hh_bit8 = (phase >> 8) & 1; } - if (slot->slot_num == 17 && (dev->rhy & 0x20)) { // tc - dev->rm_tc_bit3 = (phase >> 3) & 1; - dev->rm_tc_bit5 = (phase >> 5) & 1; + if (slot->slot_num == 17 && (chip->rhy & 0x20)) { // tc + chip->rm_tc_bit3 = (phase >> 3) & 1; + chip->rm_tc_bit5 = (phase >> 5) & 1; } - if (dev->rhy & 0x20) { - rm_xor = (dev->rm_hh_bit2 ^ dev->rm_hh_bit7) | (dev->rm_hh_bit3 ^ dev->rm_tc_bit5) | (dev->rm_tc_bit3 ^ dev->rm_tc_bit5); + if (chip->rhy & 0x20) { + rm_xor = (chip->rm_hh_bit2 ^ chip->rm_hh_bit7) + | (chip->rm_hh_bit3 ^ chip->rm_tc_bit5) + | (chip->rm_tc_bit3 ^ chip->rm_tc_bit5); switch (slot->slot_num) { case 13: // hh @@ -714,7 +609,8 @@ phase_generate(slot_t *slot) break; case 16: // sd - slot->pg_phase_out = (dev->rm_hh_bit8 << 9) | ((dev->rm_hh_bit8 ^ (noise & 1)) << 8); + slot->pg_phase_out = (chip->rm_hh_bit8 << 9) + | ((chip->rm_hh_bit8 ^ (noise & 1)) << 8); break; case 17: // tc @@ -728,16 +624,17 @@ phase_generate(slot_t *slot) n_bit = ((noise >> 14) ^ noise) & 0x01; - dev->noise = (noise >> 1) | (n_bit << 22); + chip->noise = (noise >> 1) | (n_bit << 22); } +// Slot static void -slot_write_20(slot_t *slot, uint8_t data) +OPL3_SlotWrite20(opl3_slot *slot, uint8_t data) { if ((data >> 7) & 0x01) - slot->trem = &slot->dev->tremolo; + slot->trem = &slot->chip->tremolo; else - slot->trem = (uint8_t *) &slot->dev->zeromod; + slot->trem = (uint8_t *) &slot->chip->zeromod; slot->reg_vib = (data >> 6) & 0x01; slot->reg_type = (data >> 5) & 0x01; @@ -746,23 +643,23 @@ slot_write_20(slot_t *slot, uint8_t data) } static void -slot_write_40(slot_t *slot, uint8_t data) +OPL3_SlotWrite40(opl3_slot *slot, uint8_t data) { slot->reg_ksl = (data >> 6) & 0x03; slot->reg_tl = data & 0x3f; - env_update_ksl(slot); + OPL3_EnvelopeUpdateKSL(slot); } static void -slot_write_60(slot_t *slot, uint8_t data) +OPL3_SlotWrite60(opl3_slot *slot, uint8_t data) { slot->reg_ar = (data >> 4) & 0x0f; slot->reg_dr = data & 0x0f; } static void -slot_write_80(slot_t *slot, uint8_t data) +OPL3_SlotWrite80(opl3_slot *slot, uint8_t data) { slot->reg_sl = (data >> 4) & 0x0f; @@ -773,329 +670,363 @@ slot_write_80(slot_t *slot, uint8_t data) } static void -slot_write_e0(slot_t *slot, uint8_t data) +OPL3_SlotWriteE0(opl3_slot *slot, uint8_t data) { slot->reg_wf = data & 0x07; - if (slot->dev->newm == 0x00) + if (slot->chip->newm == 0x00) slot->reg_wf &= 0x03; } static void -slot_generate(slot_t *slot) +OPL3_SlotGenerate(opl3_slot *slot) { - slot->out = env_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, - slot->eg_out); + slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, slot->eg_out); } static void -slot_calc_fb(slot_t *slot) +OPL3_SlotCalcFB(opl3_slot *slot) { - if (slot->chan->fb != 0x00) - slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->chan->fb); + if (slot->channel->fb != 0x00) + slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb); else slot->fbmod = 0; slot->prout = slot->out; } +// Channel static void -channel_setup_alg(chan_t *ch) -{ - if (ch->chtype == ch_drum) { - if (ch->ch_num == 7 || ch->ch_num == 8) { - ch->slots[0]->mod = &ch->dev->zeromod; - ch->slots[1]->mod = &ch->dev->zeromod; - return; - } - - switch (ch->alg & 0x01) { - case 0x00: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->slots[0]->out; - break; - - case 0x01: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->dev->zeromod; - break; - - default: - break; - } - return; - } - - if (ch->alg & 0x08) - return; - - if (ch->alg & 0x04) { - ch->pair->out[0] = &ch->dev->zeromod; - ch->pair->out[1] = &ch->dev->zeromod; - ch->pair->out[2] = &ch->dev->zeromod; - ch->pair->out[3] = &ch->dev->zeromod; - - switch (ch->alg & 0x03) { - case 0x00: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->pair->slots[0]->out; - ch->slots[0]->mod = &ch->pair->slots[1]->out; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->slots[1]->out; - ch->out[1] = &ch->dev->zeromod; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - case 0x01: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->pair->slots[0]->out; - ch->slots[0]->mod = &ch->dev->zeromod; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->pair->slots[1]->out; - ch->out[1] = &ch->slots[1]->out; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - case 0x02: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->dev->zeromod; - ch->slots[0]->mod = &ch->pair->slots[1]->out; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->pair->slots[0]->out; - ch->out[1] = &ch->slots[1]->out; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - case 0x03: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->dev->zeromod; - ch->slots[0]->mod = &ch->pair->slots[1]->out; - ch->slots[1]->mod = &ch->dev->zeromod; - ch->out[0] = &ch->pair->slots[0]->out; - ch->out[1] = &ch->slots[0]->out; - ch->out[2] = &ch->slots[1]->out; - ch->out[3] = &ch->dev->zeromod; - break; - - default: - break; - } - } else - switch (ch->alg & 0x01) { - case 0x00: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->slots[1]->out; - ch->out[1] = &ch->dev->zeromod; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - case 0x01: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->dev->zeromod; - ch->out[0] = &ch->slots[0]->out; - ch->out[1] = &ch->slots[1]->out; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - default: - break; - } -} +OPL3_ChannelSetupAlg(opl3_channel *channel); static void -channel_update_rhythm(nuked_t *dev, uint8_t data) +OPL3_ChannelUpdateRhythm(opl3_chip *chip, uint8_t data) { - chan_t *ch6; - chan_t *ch7; - chan_t *ch8; + opl3_channel *channel6; + opl3_channel *channel7; + opl3_channel *channel8; uint8_t chnum; - dev->rhy = data & 0x3f; - if (dev->rhy & 0x20) { - ch6 = &dev->chan[6]; - ch7 = &dev->chan[7]; - ch8 = &dev->chan[8]; - ch6->out[0] = &ch6->slots[1]->out; - ch6->out[1] = &ch6->slots[1]->out; - ch6->out[2] = &dev->zeromod; - ch6->out[3] = &dev->zeromod; - ch7->out[0] = &ch7->slots[0]->out; - ch7->out[1] = &ch7->slots[0]->out; - ch7->out[2] = &ch7->slots[1]->out; - ch7->out[3] = &ch7->slots[1]->out; - ch8->out[0] = &ch8->slots[0]->out; - ch8->out[1] = &ch8->slots[0]->out; - ch8->out[2] = &ch8->slots[1]->out; - ch8->out[3] = &ch8->slots[1]->out; + chip->rhy = data & 0x3f; + if (chip->rhy & 0x20) { + channel6 = &chip->channel[6]; + channel7 = &chip->channel[7]; + channel8 = &chip->channel[8]; + channel6->out[0] = &channel6->slotz[1]->out; + channel6->out[1] = &channel6->slotz[1]->out; + channel6->out[2] = &chip->zeromod; + channel6->out[3] = &chip->zeromod; + channel7->out[0] = &channel7->slotz[0]->out; + channel7->out[1] = &channel7->slotz[0]->out; + channel7->out[2] = &channel7->slotz[1]->out; + channel7->out[3] = &channel7->slotz[1]->out; + channel8->out[0] = &channel8->slotz[0]->out; + channel8->out[1] = &channel8->slotz[0]->out; + channel8->out[2] = &channel8->slotz[1]->out; + channel8->out[3] = &channel8->slotz[1]->out; for (chnum = 6; chnum < 9; chnum++) - dev->chan[chnum].chtype = ch_drum; + chip->channel[chnum].chtype = ch_drum; - channel_setup_alg(ch6); - channel_setup_alg(ch7); - channel_setup_alg(ch8); + OPL3_ChannelSetupAlg(channel6); + OPL3_ChannelSetupAlg(channel7); + OPL3_ChannelSetupAlg(channel8); // hh - if (dev->rhy & 0x01) - env_key_on(ch7->slots[0], egk_drum); + if (chip->rhy & 0x01) + OPL3_EnvelopeKeyOn(channel7->slotz[0], egk_drum); else - env_key_off(ch7->slots[0], egk_drum); + OPL3_EnvelopeKeyOff(channel7->slotz[0], egk_drum); // tc - if (dev->rhy & 0x02) - env_key_on(ch8->slots[1], egk_drum); + if (chip->rhy & 0x02) + OPL3_EnvelopeKeyOn(channel8->slotz[1], egk_drum); else - env_key_off(ch8->slots[1], egk_drum); + OPL3_EnvelopeKeyOff(channel8->slotz[1], egk_drum); // tom - if (dev->rhy & 0x04) - env_key_on(ch8->slots[0], egk_drum); + if (chip->rhy & 0x04) + OPL3_EnvelopeKeyOn(channel8->slotz[0], egk_drum); else - env_key_off(ch8->slots[0], egk_drum); + OPL3_EnvelopeKeyOff(channel8->slotz[0], egk_drum); // sd - if (dev->rhy & 0x08) - env_key_on(ch7->slots[1], egk_drum); + if (chip->rhy & 0x08) + OPL3_EnvelopeKeyOn(channel7->slotz[1], egk_drum); else - env_key_off(ch7->slots[1], egk_drum); + OPL3_EnvelopeKeyOff(channel7->slotz[1], egk_drum); // bd - if (dev->rhy & 0x10) { - env_key_on(ch6->slots[0], egk_drum); - env_key_on(ch6->slots[1], egk_drum); + if (chip->rhy & 0x10) { + OPL3_EnvelopeKeyOn(channel6->slotz[0], egk_drum); + OPL3_EnvelopeKeyOn(channel6->slotz[1], egk_drum); } else { - env_key_off(ch6->slots[0], egk_drum); - env_key_off(ch6->slots[1], egk_drum); + OPL3_EnvelopeKeyOff(channel6->slotz[0], egk_drum); + OPL3_EnvelopeKeyOff(channel6->slotz[1], egk_drum); } } else { for (chnum = 6; chnum < 9; chnum++) { - dev->chan[chnum].chtype = ch_2op; + chip->channel[chnum].chtype = ch_2op; - channel_setup_alg(&dev->chan[chnum]); - env_key_off(dev->chan[chnum].slots[0], egk_drum); - env_key_off(dev->chan[chnum].slots[1], egk_drum); + OPL3_ChannelSetupAlg(&chip->channel[chnum]); + OPL3_EnvelopeKeyOff(chip->channel[chnum].slotz[0], egk_drum); + OPL3_EnvelopeKeyOff(chip->channel[chnum].slotz[1], egk_drum); } } } static void -channel_write_a0(chan_t *ch, uint8_t data) +OPL3_ChannelWriteA0(opl3_channel *channel, uint8_t data) { - if (ch->dev->newm && ch->chtype == ch_4op2) + if (channel->chip->newm && channel->chtype == ch_4op2) return; - ch->f_num = (ch->f_num & 0x300) | data; - ch->ksv = (ch->block << 1) | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); + channel->f_num = (channel->f_num & 0x300) | data; + channel->ksv = (channel->block << 1) + | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01); - env_update_ksl(ch->slots[0]); - env_update_ksl(ch->slots[1]); + OPL3_EnvelopeUpdateKSL(channel->slotz[0]); + OPL3_EnvelopeUpdateKSL(channel->slotz[1]); - if (ch->dev->newm && ch->chtype == ch_4op) { - ch->pair->f_num = ch->f_num; - ch->pair->ksv = ch->ksv; + if (channel->chip->newm && channel->chtype == ch_4op) { + channel->pair->f_num = channel->f_num; + channel->pair->ksv = channel->ksv; - env_update_ksl(ch->pair->slots[0]); - env_update_ksl(ch->pair->slots[1]); + OPL3_EnvelopeUpdateKSL(channel->pair->slotz[0]); + OPL3_EnvelopeUpdateKSL(channel->pair->slotz[1]); } } static void -channel_write_b0(chan_t *ch, uint8_t data) +OPL3_ChannelWriteB0(opl3_channel *channel, uint8_t data) { - if (ch->dev->newm && ch->chtype == ch_4op2) + if (channel->chip->newm && channel->chtype == ch_4op2) return; - ch->f_num = (ch->f_num & 0xff) | ((data & 0x03) << 8); - ch->block = (data >> 2) & 0x07; - ch->ksv = (ch->block << 1) | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); + channel->f_num = (channel->f_num & 0xff) | ((data & 0x03) << 8); + channel->block = (data >> 2) & 0x07; + channel->ksv = (channel->block << 1) + | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01); - env_update_ksl(ch->slots[0]); - env_update_ksl(ch->slots[1]); + OPL3_EnvelopeUpdateKSL(channel->slotz[0]); + OPL3_EnvelopeUpdateKSL(channel->slotz[1]); - if (ch->dev->newm && ch->chtype == ch_4op) { - ch->pair->f_num = ch->f_num; - ch->pair->block = ch->block; - ch->pair->ksv = ch->ksv; + if (channel->chip->newm && channel->chtype == ch_4op) { + channel->pair->f_num = channel->f_num; + channel->pair->block = channel->block; + channel->pair->ksv = channel->ksv; - env_update_ksl(ch->pair->slots[0]); - env_update_ksl(ch->pair->slots[1]); + OPL3_EnvelopeUpdateKSL(channel->pair->slotz[0]); + OPL3_EnvelopeUpdateKSL(channel->pair->slotz[1]); } } static void -channel_write_c0(chan_t *ch, uint8_t data) +OPL3_ChannelSetupAlg(opl3_channel *channel) { - ch->fb = (data & 0x0e) >> 1; - ch->con = data & 0x01; - ch->alg = ch->con; + if (channel->chtype == ch_drum) { + if (channel->ch_num == 7 || channel->ch_num == 8) { + channel->slotz[0]->mod = &channel->chip->zeromod; + channel->slotz[1]->mod = &channel->chip->zeromod; + return; + } - if (ch->dev->newm) { - if (ch->chtype == ch_4op) { - ch->pair->alg = 0x04 | (ch->con << 1) | ch->pair->con; - ch->alg = 0x08; - channel_setup_alg(ch->pair); - } else if (ch->chtype == ch_4op2) { - ch->alg = 0x04 | (ch->pair->con << 1) | ch->con; - ch->pair->alg = 0x08; - channel_setup_alg(ch); + switch (channel->alg & 0x01) { + case 0x00: + channel->slotz[0]->mod = &channel->slotz[0]->fbmod; + channel->slotz[1]->mod = &channel->slotz[0]->out; + break; + + case 0x01: + channel->slotz[0]->mod = &channel->slotz[0]->fbmod; + channel->slotz[1]->mod = &channel->chip->zeromod; + break; + + default: + break; + } + return; + } + + if (channel->alg & 0x08) + return; + + if (channel->alg & 0x04) { + channel->pair->out[0] = &channel->chip->zeromod; + channel->pair->out[1] = &channel->chip->zeromod; + channel->pair->out[2] = &channel->chip->zeromod; + channel->pair->out[3] = &channel->chip->zeromod; + + switch (channel->alg & 0x03) { + case 0x00: + channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod; + channel->pair->slotz[1]->mod = &channel->pair->slotz[0]->out; + channel->slotz[0]->mod = &channel->pair->slotz[1]->out; + channel->slotz[1]->mod = &channel->slotz[0]->out; + channel->out[0] = &channel->slotz[1]->out; + channel->out[1] = &channel->chip->zeromod; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; + break; + + case 0x01: + channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod; + channel->pair->slotz[1]->mod = &channel->pair->slotz[0]->out; + channel->slotz[0]->mod = &channel->chip->zeromod; + channel->slotz[1]->mod = &channel->slotz[0]->out; + channel->out[0] = &channel->pair->slotz[1]->out; + channel->out[1] = &channel->slotz[1]->out; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; + break; + + case 0x02: + channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod; + channel->pair->slotz[1]->mod = &channel->chip->zeromod; + channel->slotz[0]->mod = &channel->pair->slotz[1]->out; + channel->slotz[1]->mod = &channel->slotz[0]->out; + channel->out[0] = &channel->pair->slotz[0]->out; + channel->out[1] = &channel->slotz[1]->out; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; + break; + + case 0x03: + channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod; + channel->pair->slotz[1]->mod = &channel->chip->zeromod; + channel->slotz[0]->mod = &channel->pair->slotz[1]->out; + channel->slotz[1]->mod = &channel->chip->zeromod; + channel->out[0] = &channel->pair->slotz[0]->out; + channel->out[1] = &channel->slotz[0]->out; + channel->out[2] = &channel->slotz[1]->out; + channel->out[3] = &channel->chip->zeromod; + break; + + default: + break; + } + } else + switch (channel->alg & 0x01) { + case 0x00: + channel->slotz[0]->mod = &channel->slotz[0]->fbmod; + channel->slotz[1]->mod = &channel->slotz[0]->out; + channel->out[0] = &channel->slotz[1]->out; + channel->out[1] = &channel->chip->zeromod; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; + break; + + case 0x01: + channel->slotz[0]->mod = &channel->slotz[0]->fbmod; + channel->slotz[1]->mod = &channel->chip->zeromod; + channel->out[0] = &channel->slotz[0]->out; + channel->out[1] = &channel->slotz[1]->out; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; + break; + + default: + break; + } +} + +static void +OPL3_ChannelUpdateAlg(opl3_channel *channel) +{ + channel->alg = channel->con; + + if (channel->chip->newm) { + if (channel->chtype == ch_4op) { + channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con); + channel->alg = 0x08; + OPL3_ChannelSetupAlg(channel->pair); + } else if (channel->chtype == ch_4op2) { + channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con); + channel->pair->alg = 0x08; + OPL3_ChannelSetupAlg(channel); } else - channel_setup_alg(ch); + OPL3_ChannelSetupAlg(channel); } else - channel_setup_alg(ch); - - if (ch->dev->newm) { - ch->cha = ((data >> 4) & 0x01) ? ~0 : 0; - ch->chb = ((data >> 5) & 0x01) ? ~0 : 0; - } else - ch->cha = ch->chb = (uint16_t) ~0; + OPL3_ChannelSetupAlg(channel); } static void -channel_key_on(chan_t *ch) +OPL3_ChannelWriteC0(opl3_channel *channel, uint8_t data) { - if (ch->dev->newm) { - if (ch->chtype == ch_4op) { - env_key_on(ch->slots[0], egk_norm); - env_key_on(ch->slots[1], egk_norm); - env_key_on(ch->pair->slots[0], egk_norm); - env_key_on(ch->pair->slots[1], egk_norm); - } else if (ch->chtype == ch_2op || ch->chtype == ch_drum) { - env_key_on(ch->slots[0], egk_norm); - env_key_on(ch->slots[1], egk_norm); + channel->fb = (data & 0x0e) >> 1; + channel->con = data & 0x01; + OPL3_ChannelUpdateAlg(channel); + + if (channel->chip->newm) { + channel->cha = ((data >> 4) & 0x01) ? ~0 : 0; + channel->chb = ((data >> 5) & 0x01) ? ~0 : 0; + channel->chc = ((data >> 6) & 0x01) ? ~0 : 0; + channel->chd = ((data >> 7) & 0x01) ? ~0 : 0; + } else { + channel->cha = channel->chb = (uint16_t) ~0; + // TODO: Verify on real chip if DAC2 output is disabled in compat mode + channel->chc = channel->chd = 0; + } + +#if OPL_ENABLE_STEREOEXT + if (!channel->chip->stereoext) { + channel->leftpan = channel->cha << 16; + channel->rightpan = channel->chb << 16; + } +#endif +} + +#if OPL_ENABLE_STEREOEXT +static void +OPL3_ChannelWriteD0(opl3_channel *channel, uint8_t data) +{ + if (channel->chip->stereoext) { + channel->leftpan = panpot_lut[data ^ 0xffu]; + channel->rightpan = panpot_lut[data]; + } +} +#endif + +static void +OPL3_ChannelKeyOn(opl3_channel *channel) +{ + if (channel->chip->newm) { + if (channel->chtype == ch_4op) { + OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm); + OPL3_EnvelopeKeyOn(channel->pair->slotz[0], egk_norm); + OPL3_EnvelopeKeyOn(channel->pair->slotz[1], egk_norm); + } else if (channel->chtype == ch_2op || channel->chtype == ch_drum) { + OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm); } } else { - env_key_on(ch->slots[0], egk_norm); - env_key_on(ch->slots[1], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm); } } static void -channel_key_off(chan_t *ch) +OPL3_ChannelKeyOff(opl3_channel *channel) { - if (ch->dev->newm) { - if (ch->chtype == ch_4op) { - env_key_off(ch->slots[0], egk_norm); - env_key_off(ch->slots[1], egk_norm); - env_key_off(ch->pair->slots[0], egk_norm); - env_key_off(ch->pair->slots[1], egk_norm); - } else if (ch->chtype == ch_2op || ch->chtype == ch_drum) { - env_key_off(ch->slots[0], egk_norm); - env_key_off(ch->slots[1], egk_norm); + if (channel->chip->newm) { + if (channel->chtype == ch_4op) { + OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm); + OPL3_EnvelopeKeyOff(channel->pair->slotz[0], egk_norm); + OPL3_EnvelopeKeyOff(channel->pair->slotz[1], egk_norm); + } else if (channel->chtype == ch_2op || channel->chtype == ch_drum) { + OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm); } } else { - env_key_off(ch->slots[0], egk_norm); - env_key_off(ch->slots[1], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm); } } static void -channel_set_4op(nuked_t *dev, uint8_t data) +OPL3_ChannelSet4Op(opl3_chip *chip, uint8_t data) { uint8_t chnum; @@ -1106,45 +1037,291 @@ channel_set_4op(nuked_t *dev, uint8_t data) chnum += 9 - 3; if ((data >> bit) & 0x01) { - dev->chan[chnum].chtype = ch_4op; - dev->chan[chnum + 3].chtype = ch_4op2; + chip->channel[chnum].chtype = ch_4op; + chip->channel[chnum + 3u].chtype = ch_4op2; + OPL3_ChannelUpdateAlg(&chip->channel[chnum]); } else { - dev->chan[chnum].chtype = ch_2op; - dev->chan[chnum + 3].chtype = ch_2op; + chip->channel[chnum].chtype = ch_2op; + chip->channel[chnum + 3u].chtype = ch_2op; + OPL3_ChannelUpdateAlg(&chip->channel[chnum]); + OPL3_ChannelUpdateAlg(&chip->channel[chnum + 3u]); } } } +static void +OPL3_ProcessSlot(opl3_slot *slot) +{ + OPL3_SlotCalcFB(slot); + OPL3_EnvelopeCalc(slot); + OPL3_PhaseGenerate(slot); + OPL3_SlotGenerate(slot); +} + +static inline void +OPL3_Generate4Ch(void *priv, int32_t *buf4) +{ + opl3_chip *chip = (opl3_chip *) priv; + opl3_channel *channel; + opl3_writebuf *writebuf; + int16_t **out; + int32_t mix[2]; + uint8_t i; + int16_t accm; + uint8_t shift = 0; + + buf4[1] = chip->mixbuff[1]; + buf4[3] = chip->mixbuff[3]; + +#if OPL_QUIRK_CHANNELSAMPLEDELAY + for (i = 0; i < 15; i++) +#else + for (i = 0; i < 36; i++) +#endif + OPL3_ProcessSlot(&chip->slot[i]); + + mix[0] = mix[1] = 0; + + for (i = 0; i < 18; i++) { + channel = &chip->channel[i]; + out = channel->out; + accm = *out[0] + *out[1] + *out[2] + *out[3]; +#if OPL_ENABLE_STEREOEXT + mix[0] += (int16_t) ((accm * channel->leftpan) >> 16); +#else + mix[0] += (int16_t) (accm & channel->cha); +#endif + mix[1] += (int16_t) (accm & channel->chc); + } + + chip->mixbuff[0] = mix[0]; + chip->mixbuff[2] = mix[1]; + +#if OPL_QUIRK_CHANNELSAMPLEDELAY + for (i = 15; i < 18; i++) + OPL3_ProcessSlot(&chip->slot[i]); +#endif + + buf4[0] = chip->mixbuff[0]; + buf4[2] = chip->mixbuff[2]; + +#if OPL_QUIRK_CHANNELSAMPLEDELAY + for (i = 18; i < 33; i++) + OPL3_ProcessSlot(&chip->slot[i]); +#endif + + mix[0] = mix[1] = 0; + + for (i = 0; i < 18; i++) { + channel = &chip->channel[i]; + out = channel->out; + accm = *out[0] + *out[1] + *out[2] + *out[3]; +#if OPL_ENABLE_STEREOEXT + mix[0] += (int16_t) ((accm * channel->rightpan) >> 16); +#else + mix[0] += (int16_t) (accm & channel->chb); +#endif + mix[1] += (int16_t) (accm & channel->chd); + } + + chip->mixbuff[1] = mix[0]; + chip->mixbuff[3] = mix[1]; + +#if OPL_QUIRK_CHANNELSAMPLEDELAY + for (i = 33; i < 36; i++) + OPL3_ProcessSlot(&chip->slot[i]); +#endif + + if ((chip->timer & 0x3f) == 0x3f) + chip->tremolopos = (chip->tremolopos + 1) % 210; + + if (chip->tremolopos < 105) + chip->tremolo = chip->tremolopos >> chip->tremoloshift; + else + chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift; + + if ((chip->timer & 0x03ff) == 0x03ff) + chip->vibpos = (chip->vibpos + 1) & 7; + + chip->timer++; + + if (chip->eg_state) { + while (shift < 13 && ((chip->eg_timer >> shift) & 1) == 0) + shift++; + + if (shift > 12) + chip->eg_add = 0; + else + chip->eg_add = shift + 1; + + chip->eg_timer_lo = (uint8_t) (chip->eg_timer & 0x3u); + } + + if (chip->eg_timerrem || chip->eg_state) { + if (chip->eg_timer == UINT64_C(0xfffffffff)) { + chip->eg_timer = 0; + chip->eg_timerrem = 1; + } else { + chip->eg_timer++; + chip->eg_timerrem = 0; + } + } + + chip->eg_state ^= 1; + + while ((writebuf = &chip->writebuf[chip->writebuf_cur]), writebuf->time <= chip->writebuf_samplecnt) { + if (!(writebuf->reg & 0x200)) + break; + + writebuf->reg &= 0x01ff; + + OPL3_WriteReg(chip, writebuf->reg, writebuf->data); + + chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE; + } + + chip->writebuf_samplecnt++; +} + +void +OPL3_Generate(opl3_chip *chip, int32_t *buf) +{ + int32_t samples[4]; + OPL3_Generate4Ch(chip, samples); + buf[0] = samples[0]; + buf[1] = samples[1]; +} + +void +OPL3_Generate4ChResampled(opl3_chip *chip, int32_t *buf4) +{ + while (chip->samplecnt >= chip->rateratio) { + chip->oldsamples[0] = chip->samples[0]; + chip->oldsamples[1] = chip->samples[1]; + chip->oldsamples[2] = chip->samples[2]; + chip->oldsamples[3] = chip->samples[3]; + OPL3_Generate4Ch(chip, chip->samples); + chip->samplecnt -= chip->rateratio; + } + + buf4[0] = (int32_t) ((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt) + + chip->samples[0] * chip->samplecnt) / chip->rateratio); + buf4[1] = (int32_t) ((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt) + + chip->samples[1] * chip->samplecnt) / chip->rateratio); + buf4[2] = (int32_t) ((chip->oldsamples[2] * (chip->rateratio - chip->samplecnt) + + chip->samples[2] * chip->samplecnt) / chip->rateratio); + buf4[3] = (int32_t) ((chip->oldsamples[3] * (chip->rateratio - chip->samplecnt) + + chip->samples[3] * chip->samplecnt) / chip->rateratio); + + chip->samplecnt += 1 << RSM_FRAC; +} + +void +OPL3_GenerateResampled(opl3_chip *chip, int32_t *buf) +{ + int32_t samples[4]; + OPL3_Generate4ChResampled(chip, samples); + buf[0] = samples[0]; + buf[1] = samples[1]; +} + +void +OPL3_Reset(opl3_chip *chip, uint32_t samplerate) +{ + opl3_slot *slot; + opl3_channel *channel; + uint8_t local_ch_slot; + + memset(chip, 0x00, sizeof(opl3_chip)); + + for (uint8_t slotnum = 0; slotnum < 36; slotnum++) { + slot = &chip->slot[slotnum]; + slot->chip = chip; + slot->mod = &chip->zeromod; + slot->eg_rout = 0x01ff; + slot->eg_out = 0x01ff; + slot->eg_gen = envelope_gen_num_release; + slot->trem = (uint8_t *) &chip->zeromod; + slot->slot_num = slotnum; + } + + for (uint8_t channum = 0; channum < 18; channum++) { + channel = &chip->channel[channum]; + local_ch_slot = ch_slot[channum]; + channel->slotz[0] = &chip->slot[local_ch_slot]; + channel->slotz[1] = &chip->slot[local_ch_slot + 3u]; + chip->slot[local_ch_slot].channel = channel; + chip->slot[local_ch_slot + 3u].channel = channel; + + if ((channum % 9) < 3) + channel->pair = &chip->channel[channum + 3u]; + else if ((channum % 9) < 6) + channel->pair = &chip->channel[channum - 3u]; + + channel->chip = chip; + channel->out[0] = &chip->zeromod; + channel->out[1] = &chip->zeromod; + channel->out[2] = &chip->zeromod; + channel->out[3] = &chip->zeromod; + channel->chtype = ch_2op; + channel->cha = 0xffff; + channel->chb = 0xffff; +#if OPL_ENABLE_STEREOEXT + channel->leftpan = 0x10000; + channel->rightpan = 0x10000; +#endif + channel->ch_num = channum; + + OPL3_ChannelSetupAlg(channel); + } + + chip->noise = 1; + chip->rateratio = (samplerate << RSM_FRAC) / 49716; + chip->tremoloshift = 4; + chip->vibshift = 1; + +#if OPL_ENABLE_STEREOEXT + if (!panpot_lut_build) { + for (int32_t i = 0; i < 256; i++) + panpot_lut[i] = OPL_SIN(i); + panpot_lut_build = 1; + } +#endif +} + uint16_t nuked_write_addr(void *priv, uint16_t port, uint8_t val) { - const nuked_t *dev = (nuked_t *) priv; + const opl3_chip *chip = (opl3_chip *) priv; uint16_t addr; addr = val; - if ((port & 0x0002) && ((addr == 0x0005) || dev->newm)) + if ((port & 0x0002) && ((addr == 0x0005) || chip->newm)) addr |= 0x0100; return addr; } void -nuked_write_reg(void *priv, uint16_t reg, uint8_t val) +OPL3_WriteReg(void *priv, uint16_t reg, uint8_t val) { - nuked_t *dev = (nuked_t *) priv; - uint8_t high = (reg >> 8) & 0x01; - uint8_t regm = reg & 0xff; + opl3_chip *chip = (opl3_chip *) priv; + uint8_t high = (reg >> 8) & 0x01; + uint8_t regm = reg & 0xff; switch (regm & 0xf0) { case 0x00: if (high) switch (regm & 0x0f) { case 0x04: - channel_set_4op(dev, val); + OPL3_ChannelSet4Op(chip, val); break; case 0x05: - dev->newm = val & 0x01; + chip->newm = val & 0x01; +#if OPL_ENABLE_STEREOEXT + chip->stereoext = (val >> 1) & 0x01; +#endif break; default: @@ -1153,7 +1330,7 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) else switch (regm & 0x0f) { case 0x08: - dev->nts = (val >> 6) & 0x01; + chip->nts = (val >> 6) & 0x01; break; default: @@ -1163,58 +1340,65 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) case 0x20: case 0x30: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_20(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); + if (ad_slot[regm & 0x1fu] >= 0) + OPL3_SlotWrite20(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x40: case 0x50: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_40(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); + if (ad_slot[regm & 0x1fu] >= 0) + OPL3_SlotWrite40(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x60: case 0x70: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_60(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); + if (ad_slot[regm & 0x1fu] >= 0) + OPL3_SlotWrite60(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x80: case 0x90: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_80(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); + if (ad_slot[regm & 0x1fu] >= 0) + OPL3_SlotWrite80(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); + break; + + case 0xe0: + case 0xf0: + if (ad_slot[regm & 0x1fu] >= 0) + OPL3_SlotWriteE0(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0xa0: if ((regm & 0x0f) < 9) - channel_write_a0(&dev->chan[9 * high + (regm & 0x0f)], val); + OPL3_ChannelWriteA0(&chip->channel[9u * high + (regm & 0x0fu)], val); break; case 0xb0: if (regm == 0xbd && !high) { - dev->tremoloshift = (((val >> 7) ^ 1) << 1) + 2; - dev->vibshift = ((val >> 6) & 0x01) ^ 1; - channel_update_rhythm(dev, val); + chip->tremoloshift = (((val >> 7) ^ 1) << 1) + 2; + chip->vibshift = ((val >> 6) & 0x01) ^ 1; + OPL3_ChannelUpdateRhythm(chip, val); } else if ((regm & 0x0f) < 9) { - channel_write_b0(&dev->chan[9 * high + (regm & 0x0f)], val); + OPL3_ChannelWriteB0(&chip->channel[9u * high + (regm & 0x0fu)], val); if (val & 0x20) - channel_key_on(&dev->chan[9 * high + (regm & 0x0f)]); + OPL3_ChannelKeyOn(&chip->channel[9u * high + (regm & 0x0fu)]); else - channel_key_off(&dev->chan[9 * high + (regm & 0x0f)]); + OPL3_ChannelKeyOff(&chip->channel[9u * high + (regm & 0x0fu)]); } break; case 0xc0: if ((regm & 0x0f) < 9) - channel_write_c0(&dev->chan[9 * high + (regm & 0x0f)], val); + OPL3_ChannelWriteC0(&chip->channel[9u * high + (regm & 0x0fu)], val); break; - case 0xe0: - case 0xf0: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_e0(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); +#if OPL_ENABLE_STEREOEXT + case 0xd0: + if ((regm & 0x0f) < 9) + OPL3_ChannelWriteD0(&chip->channel[9u * high + (regm & 0x0fu)], val); break; +#endif default: break; @@ -1222,231 +1406,62 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) } void -nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) +OPL3_WriteRegBuffered(void *priv, uint16_t reg, uint8_t val) { - nuked_t *dev = (nuked_t *) priv; - uint64_t time1; - uint64_t time2; + opl3_chip *chip = (opl3_chip *) priv; + uint64_t time1; + uint64_t time2; + opl3_writebuf *writebuf; + uint32_t writebuf_last; - if (dev->wrbuf[dev->wrbuf_last].reg & 0x0200) { - nuked_write_reg(dev, dev->wrbuf[dev->wrbuf_last].reg & 0x01ff, - dev->wrbuf[dev->wrbuf_last].data); + writebuf_last = chip->writebuf_last; + writebuf = &chip->writebuf[writebuf_last]; - dev->wrbuf_cur = (dev->wrbuf_last + 1) % WRBUF_SIZE; - dev->wrbuf_samplecnt = dev->wrbuf[dev->wrbuf_last].time; + if (writebuf->reg & 0x0200) { + OPL3_WriteReg(chip, writebuf->reg & 0x01ff, writebuf->data); + + chip->writebuf_cur = (writebuf_last + 1) % OPL_WRITEBUF_SIZE; + chip->writebuf_samplecnt = writebuf->time; } - dev->wrbuf[dev->wrbuf_last].reg = reg | 0x0200; - dev->wrbuf[dev->wrbuf_last].data = val; - time1 = dev->wrbuf_lasttime + WRBUF_DELAY; - time2 = dev->wrbuf_samplecnt; + writebuf->reg = reg | 0x0200; + writebuf->data = val; + time1 = chip->writebuf_lasttime + OPL_WRITEBUF_DELAY; + time2 = chip->writebuf_samplecnt; if (time1 < time2) time1 = time2; - dev->wrbuf[dev->wrbuf_last].time = time1; - dev->wrbuf_lasttime = time1; - dev->wrbuf_last = (dev->wrbuf_last + 1) % WRBUF_SIZE; + writebuf->time = time1; + chip->writebuf_lasttime = time1; + chip->writebuf_last = (writebuf_last + 1) % OPL_WRITEBUF_SIZE; } void -nuked_generate(void *priv, int32_t *bufp) +OPL3_Generate4ChStream(opl3_chip *chip, int32_t *sndptr1, int32_t *sndptr2, uint32_t numsamples) { - nuked_t *dev = (nuked_t *) priv; - int16_t accm; - int16_t shift = 0; - uint8_t i; - uint8_t j; + int32_t samples[4]; - bufp[1] = dev->mixbuff[1]; - - for (i = 0; i < 15; i++) { - slot_calc_fb(&dev->slot[i]); - env_calc(&dev->slot[i]); - phase_generate(&dev->slot[i]); - slot_generate(&dev->slot[i]); + for (uint_fast32_t i = 0; i < numsamples; i++) { + OPL3_Generate4Ch(chip, samples); + sndptr1[0] = samples[0]; + sndptr1[1] = samples[1]; + sndptr2[0] = samples[2]; + sndptr2[1] = samples[3]; + sndptr1 += 2; + sndptr2 += 2; } - - dev->mixbuff[0] = 0; - - for (i = 0; i < 18; i++) { - accm = 0; - - for (j = 0; j < 4; j++) - accm += *dev->chan[i].out[j]; - - dev->mixbuff[0] += (int16_t) (accm & dev->chan[i].cha); - } - for (i = 15; i < 18; i++) { - slot_calc_fb(&dev->slot[i]); - env_calc(&dev->slot[i]); - phase_generate(&dev->slot[i]); - slot_generate(&dev->slot[i]); - } - - bufp[0] = dev->mixbuff[0]; - - for (i = 18; i < 33; i++) { - slot_calc_fb(&dev->slot[i]); - env_calc(&dev->slot[i]); - phase_generate(&dev->slot[i]); - slot_generate(&dev->slot[i]); - } - - dev->mixbuff[1] = 0; - - for (i = 0; i < 18; i++) { - accm = 0; - - for (j = 0; j < 4; j++) - accm += *dev->chan[i].out[j]; - - dev->mixbuff[1] += (int16_t) (accm & dev->chan[i].chb); - } - - for (i = 33; i < 36; i++) { - slot_calc_fb(&dev->slot[i]); - env_calc(&dev->slot[i]); - phase_generate(&dev->slot[i]); - slot_generate(&dev->slot[i]); - } - - if ((dev->timer & 0x3f) == 0x3f) - dev->tremolopos = (dev->tremolopos + 1) % 210; - - if (dev->tremolopos < 105) - dev->tremolo = dev->tremolopos >> dev->tremoloshift; - else - dev->tremolo = (210 - dev->tremolopos) >> dev->tremoloshift; - - if ((dev->timer & 0x03ff) == 0x03ff) - dev->vibpos = (dev->vibpos + 1) & 7; - - dev->timer++; - dev->eg_add = 0; - - if (dev->eg_timer) { - while (shift < 36 && ((dev->eg_timer >> shift) & 1) == 0) - shift++; - - if (shift > 12) - dev->eg_add = 0; - else - dev->eg_add = shift + 1; - } - - if (dev->eg_timerrem || dev->eg_state) { - if (dev->eg_timer == 0xfffffffff) { - dev->eg_timer = 0; - dev->eg_timerrem = 1; - } else { - dev->eg_timer++; - dev->eg_timerrem = 0; - } - } - - dev->eg_state ^= 1; - - while (dev->wrbuf[dev->wrbuf_cur].time <= dev->wrbuf_samplecnt) { - if (!(dev->wrbuf[dev->wrbuf_cur].reg & 0x200)) - break; - - dev->wrbuf[dev->wrbuf_cur].reg &= 0x01ff; - - nuked_write_reg(dev, dev->wrbuf[dev->wrbuf_cur].reg, - dev->wrbuf[dev->wrbuf_cur].data); - - dev->wrbuf_cur = (dev->wrbuf_cur + 1) % WRBUF_SIZE; - } - - dev->wrbuf_samplecnt++; } void -nuked_generate_resampled(nuked_t *dev, int32_t *bufp) +OPL3_GenerateStream(opl3_chip *chip, int32_t *sndptr, uint32_t numsamples) { - while (dev->samplecnt >= dev->rateratio) { - dev->oldsamples[0] = dev->samples[0]; - dev->oldsamples[1] = dev->samples[1]; - nuked_generate(dev, dev->samples); - dev->samplecnt -= dev->rateratio; - } - - bufp[0] = (int32_t) ((dev->oldsamples[0] * (dev->rateratio - dev->samplecnt) - + dev->samples[0] * dev->samplecnt) - / dev->rateratio); - bufp[1] = (int32_t) ((dev->oldsamples[1] * (dev->rateratio - dev->samplecnt) - + dev->samples[1] * dev->samplecnt) - / dev->rateratio); - - dev->samplecnt += 1 << RSM_FRAC; -} - -void -nuked_generate_raw(nuked_t *dev, int32_t *bufp) -{ - nuked_generate(dev, dev->samples); - - bufp[0] = (int32_t) dev->samples[0]; - bufp[1] = (int32_t) dev->samples[1]; -} - -void -nuked_generate_stream(nuked_t *dev, int32_t *sndptr, uint32_t num) -{ - for (uint32_t i = 0; i < num; i++) { - nuked_generate_raw(dev, sndptr); + for (uint_fast32_t i = 0; i < numsamples; i++) { + OPL3_Generate(chip, sndptr); sndptr += 2; } } -void -nuked_init(nuked_t *dev, uint32_t samplerate) -{ - uint8_t i; - - memset(dev, 0x00, sizeof(nuked_t)); - - for (i = 0; i < 36; i++) { - dev->slot[i].dev = dev; - dev->slot[i].mod = &dev->zeromod; - dev->slot[i].eg_rout = 0x01ff; - dev->slot[i].eg_out = 0x01ff; - dev->slot[i].eg_gen = envelope_gen_num_release; - dev->slot[i].trem = (uint8_t *) &dev->zeromod; - dev->slot[i].slot_num = i; - } - - for (i = 0; i < 18; i++) { - dev->chan[i].slots[0] = &dev->slot[ch_slot[i]]; - dev->chan[i].slots[1] = &dev->slot[ch_slot[i] + 3]; - dev->slot[ch_slot[i]].chan = &dev->chan[i]; - dev->slot[ch_slot[i] + 3].chan = &dev->chan[i]; - - if ((i % 9) < 3) - dev->chan[i].pair = &dev->chan[i + 3]; - else if ((i % 9) < 6) - dev->chan[i].pair = &dev->chan[i - 3]; - - dev->chan[i].dev = dev; - dev->chan[i].out[0] = &dev->zeromod; - dev->chan[i].out[1] = &dev->zeromod; - dev->chan[i].out[2] = &dev->zeromod; - dev->chan[i].out[3] = &dev->zeromod; - dev->chan[i].chtype = ch_2op; - dev->chan[i].cha = 0xffff; - dev->chan[i].chb = 0xffff; - dev->chan[i].ch_num = i; - - channel_setup_alg(&dev->chan[i]); - } - - dev->noise = 1; - dev->rateratio = (samplerate << RSM_FRAC) / 49716; - dev->tremoloshift = 4; - dev->vibshift = 1; -} - static void nuked_timer_tick(nuked_drv_t *dev, int tmr) { @@ -1523,7 +1538,7 @@ nuked_drv_init(const device_t *info) dev->status = 0x06; /* Initialize the NukedOPL object. */ - nuked_init(&dev->opl, OPL_FREQ); + OPL3_Reset(&dev->opl, OPL_FREQ); timer_add(&dev->timers[0], nuked_timer_1, dev, 0); timer_add(&dev->timers[1], nuked_timer_2, dev, 0); @@ -1546,7 +1561,7 @@ nuked_drv_update(void *priv) if (dev->pos >= music_pos_global) return dev->buffer; - nuked_generate_stream(&dev->opl, + OPL3_GenerateStream(&dev->opl, &dev->buffer[dev->pos * 2], music_pos_global - dev->pos); @@ -1588,7 +1603,7 @@ nuked_drv_write(uint16_t port, uint8_t val, void *priv) nuked_drv_update(dev); if ((port & 0x0001) == 0x0001) { - nuked_write_reg_buffered(&dev->opl, dev->port, val); + OPL3_WriteRegBuffered(&dev->opl, dev->port, val); switch (dev->port) { case 0x002: /* Timer 1 */ @@ -1672,4 +1687,4 @@ const fm_drv_t nuked_opl_drv = { &nuked_drv_set_do_cycles, NULL, NULL, -}; \ No newline at end of file +}; diff --git a/src/sound/sound.c b/src/sound/sound.c index f941c8817..794e21359 100644 --- a/src/sound/sound.c +++ b/src/sound/sound.c @@ -137,6 +137,11 @@ static const SOUND_CARD sound_cards[] = { { &cms_device }, { &cs4235_device }, { &cs4236b_device }, + { &ess_688_device }, + { &ess_ess0100_pnp_device }, + { &ess_1688_device }, + { &ess_ess0102_pnp_device }, + { &ess_ess0968_pnp_device }, { &gus_device }, { &sb_1_device }, { &sb_15_device }, @@ -176,11 +181,6 @@ static const SOUND_CARD sound_cards[] = { { &ct5880_device }, { &ad1881_device }, { &cs4297a_device }, - { &ess_688_device }, - { &ess_ess0100_pnp_device }, - { &ess_1688_device }, - { &ess_ess0102_pnp_device }, - { &ess_ess0968_pnp_device }, { NULL } // clang-format on }; diff --git a/src/video/vid_chips_69000.c b/src/video/vid_chips_69000.c index 2caefd413..fd4ff5c8e 100644 --- a/src/video/vid_chips_69000.c +++ b/src/video/vid_chips_69000.c @@ -2477,7 +2477,7 @@ chips_69000_init(const device_t *info) timer_add(&chips->decrement_timer, chips_69000_decrement_timer, chips, 0); timer_on_auto(&chips->decrement_timer, 1000000. / 2000.); - chips->i2c = i2c_gpio_init("chips_69000_ddc"); + chips->i2c = i2c_gpio_init("ddc_chips_69000"); chips->ddc = ddc_init(i2c_gpio_get_bus(chips->i2c)); chips->flat_panel_regs[0x01] = 1; diff --git a/src/video/vid_paradise.c b/src/video/vid_paradise.c index dbc4c2ba3..70f90d6d8 100644 --- a/src/video/vid_paradise.c +++ b/src/video/vid_paradise.c @@ -45,6 +45,7 @@ typedef struct paradise_t { } type; uint32_t vram_mask; + uint32_t memory; uint32_t read_bank[4], write_bank[4]; @@ -167,12 +168,10 @@ paradise_out(uint16_t addr, uint8_t val, void *priv) return; } - old = svga->gdcreg[svga->gdcaddr]; switch (svga->gdcaddr) { case 6: - if ((val & 0xc) != (old & 0xc)) { - svga->gdcreg[6] = val; - switch (svga->gdcreg[6] & 0xc) { + if ((svga->gdcreg[6] & 0x0c) != (val & 0xc)) { + switch (val & 0x0c) { case 0x0: /*128k at A0000*/ mem_mapping_set_addr(&svga->mapping, 0xa0000, 0x20000); svga->banked_mask = 0xffff; @@ -193,8 +192,9 @@ paradise_out(uint16_t addr, uint8_t val, void *priv) default: break; } - paradise_remap(paradise); } + svga->gdcreg[6] = val; + paradise_remap(paradise); return; case 9: @@ -296,6 +296,8 @@ paradise_recalctimings(svga_t *svga) { const paradise_t *paradise = (paradise_t *) svga->priv; + svga->lowres = !(svga->gdcreg[0x0e] & 0x01); + if (paradise->type == WD90C30) { if (svga->crtc[0x3e] & 0x01) svga->vtotal |= 0x400; @@ -309,22 +311,18 @@ paradise_recalctimings(svga_t *svga) svga->split |= 0x400; svga->interlace = !!(svga->crtc[0x2d] & 0x20); - - if (!svga->interlace && !(svga->gdcreg[0x0e] & 0x01) && (svga->hdisp >= 1024) && ((svga->gdcreg[5] & 0x60) == 0) && (svga->miscout >= 0x27) && (svga->miscout <= 0x2f) && ((svga->gdcreg[6] & 1) || (svga->attrregs[0x10] & 1))) { /*Horrible tweak to re-enable the interlace after returning to - a windowed DOS box in Win3.x*/ - svga->interlace = 1; - } } if (paradise->type < WD90C30) { if ((svga->gdcreg[6] & 1) || (svga->attrregs[0x10] & 1)) { - if ((svga->bpp >= 8) && (svga->gdcreg[0x0e] & 0x01)) { + if ((svga->bpp >= 8) && !svga->lowres) { svga->render = svga_render_8bpp_highres; + svga->vram_display_mask = (svga->crtc[0x2f] & 0x02) ? 0x3ffff : paradise->vram_mask; } } } else { if ((svga->gdcreg[6] & 1) || (svga->attrregs[0x10] & 1)) { - if ((svga->bpp >= 8) && (svga->gdcreg[0x0e] & 0x01)) { + if ((svga->bpp >= 8) && !svga->lowres) { if (svga->bpp == 16) { svga->render = svga_render_16bpp_highres; svga->hdisp >>= 1; @@ -339,13 +337,16 @@ paradise_recalctimings(svga_t *svga) svga->hdisp += 12; if (svga->hdisp == 800) svga->ma_latch -= 3; - } else { + } else svga->render = svga_render_8bpp_highres; - } - } + + svga->vram_display_mask = (svga->crtc[0x2f] & 0x02) ? 0x3ffff : paradise->vram_mask; + } else if ((svga->bpp <= 8) && svga->lowres && !svga->interlace && (svga->hdisp >= 1024) && + (svga->miscout >= 0x27) && (svga->miscout <= 0x2f)) + svga->interlace = 1; /*Horrible tweak to re-enable the interlace after returning to + a windowed DOS box in Win3.x*/ } } - svga->vram_display_mask = (svga->crtc[0x2f] & 0x02) ? 0x3ffff : paradise->vram_mask; } static void @@ -356,15 +357,10 @@ paradise_write(uint32_t addr, uint8_t val, void *priv) uint32_t prev_addr; uint32_t prev_addr2; - if (!(svga->gdcreg[5] & 0x40)) { - svga_write(addr, val, svga); - return; - } - addr = (addr & 0x7fff) + paradise->write_bank[(addr >> 15) & 3]; /*Could be done in a better way but it works.*/ - if ((svga->gdcreg[0x0e] & 0x01) || (svga->gdcreg[5] & 0x40)) { + if (!svga->lowres || (svga->attrregs[0x10] & 0x40)) { if (((svga->gdcreg[6] & 0x0c) == 0x04) && (svga->crtc[0x14] & 0x40) && ((svga->gdcreg[0x0b] & 0xc0) == 0xc0) && !svga->chain4) { prev_addr = addr & 3; prev_addr2 = addr & 0xfffc; @@ -385,6 +381,7 @@ paradise_write(uint32_t addr, uint8_t val, void *priv) } svga_write_linear(addr, val, svga); } + static void paradise_writew(uint32_t addr, uint16_t val, void *priv) { @@ -393,15 +390,10 @@ paradise_writew(uint32_t addr, uint16_t val, void *priv) uint32_t prev_addr; uint32_t prev_addr2; - if (!(svga->gdcreg[5] & 0x40)) { - svga_writew(addr, val, svga); - return; - } - addr = (addr & 0x7fff) + paradise->write_bank[(addr >> 15) & 3]; /*Could be done in a better way but it works.*/ - if ((svga->gdcreg[0x0e] & 0x01) || (svga->gdcreg[5] & 0x40)) { + if (!svga->lowres || (svga->attrregs[0x10] & 0x40)) { if (((svga->gdcreg[6] & 0x0c) == 0x04) && (svga->crtc[0x14] & 0x40) && ((svga->gdcreg[0x0b] & 0xc0) == 0xc0) && !svga->chain4) { prev_addr = addr & 3; prev_addr2 = addr & 0xfffc; @@ -431,13 +423,10 @@ paradise_read(uint32_t addr, void *priv) uint32_t prev_addr; uint32_t prev_addr2; - if (!(svga->gdcreg[5] & 0x40)) - return svga_read(addr, svga); - addr = (addr & 0x7fff) + paradise->read_bank[(addr >> 15) & 3]; /*Could be done in a better way but it works.*/ - if ((svga->gdcreg[0x0e] & 0x01) || (svga->gdcreg[5] & 0x40)) { + if (!svga->lowres || (svga->attrregs[0x10] & 0x40)) { if (((svga->gdcreg[6] & 0x0c) == 0x04) && (svga->crtc[0x14] & 0x40) && ((svga->gdcreg[0x0b] & 0xc0) == 0xc0) && !svga->chain4) { prev_addr = addr & 3; prev_addr2 = addr & 0xfffc; @@ -458,6 +447,7 @@ paradise_read(uint32_t addr, void *priv) } return svga_read_linear(addr, svga); } + static uint16_t paradise_readw(uint32_t addr, void *priv) { @@ -466,13 +456,10 @@ paradise_readw(uint32_t addr, void *priv) uint32_t prev_addr; uint32_t prev_addr2; - if (!(svga->gdcreg[5] & 0x40)) - return svga_readw(addr, svga); - addr = (addr & 0x7fff) + paradise->read_bank[(addr >> 15) & 3]; /*Could be done in a better way but it works.*/ - if ((svga->gdcreg[0x0e] & 0x01) || (svga->gdcreg[5] & 0x40)) { + if (!svga->lowres || (svga->attrregs[0x10] & 0x40)) { if (((svga->gdcreg[6] & 0x0c) == 0x04) && (svga->crtc[0x14] & 0x40) && ((svga->gdcreg[0x0b] & 0xc0) == 0xc0) && !svga->chain4) { prev_addr = addr & 3; prev_addr2 = addr & 0xfffc; @@ -506,6 +493,8 @@ paradise_init(const device_t *info, uint32_t memsize) else video_inform(VIDEO_FLAG_TYPE_SPECIAL, &timing_paradise_wd90c); + paradise->memory = memsize >> 10; + switch (info->local) { case PVGA1A: svga_init(info, svga, paradise, memsize, /*256kb*/