libsidplayfp  1.1.0
hardsid-emu.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2013 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  * Copyright 2001-2002 by Jarno Paananen
7  * Copyright 2000-2002 Simon White
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 #ifndef HARDSID_EMU_H
25 #define HARDSID_EMU_H
26 
27 #include <string>
28 
29 #include "sidplayfp/event.h"
30 #include "sidplayfp/sidemu.h"
31 #include "sidplayfp/EventScheduler.h"
32 #include "sidplayfp/siddefs.h"
33 
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37 
38 #ifdef _WIN32
39 
40 #include <windows.h>
41 
42 #define HSID_VERSION_MIN (WORD) 0x0200
43 #define HSID_VERSION_204 (WORD) 0x0204
44 #define HSID_VERSION_207 (WORD) 0x0207
45 
46 //**************************************************************************
47 // Version 2 Interface
48 typedef void (CALLBACK* HsidDLL2_Delay_t) (BYTE deviceID, WORD cycles);
49 typedef BYTE (CALLBACK* HsidDLL2_Devices_t) ();
50 typedef void (CALLBACK* HsidDLL2_Filter_t) (BYTE deviceID, BOOL filter);
51 typedef void (CALLBACK* HsidDLL2_Flush_t) (BYTE deviceID);
52 typedef void (CALLBACK* HsidDLL2_Mute_t) (BYTE deviceID, BYTE channel, BOOL mute);
53 typedef void (CALLBACK* HsidDLL2_MuteAll_t) (BYTE deviceID, BOOL mute);
54 typedef void (CALLBACK* HsidDLL2_Reset_t) (BYTE deviceID);
55 typedef BYTE (CALLBACK* HsidDLL2_Read_t) (BYTE deviceID, WORD cycles, BYTE SID_reg);
56 typedef void (CALLBACK* HsidDLL2_Sync_t) (BYTE deviceID);
57 typedef void (CALLBACK* HsidDLL2_Write_t) (BYTE deviceID, WORD cycles, BYTE SID_reg, BYTE data);
58 typedef WORD (CALLBACK* HsidDLL2_Version_t) ();
59 
60 // Version 2.04 Extensions
61 typedef BOOL (CALLBACK* HsidDLL2_Lock_t) (BYTE deviceID);
62 typedef void (CALLBACK* HsidDLL2_Unlock_t) (BYTE deviceID);
63 typedef void (CALLBACK* HsidDLL2_Reset2_t) (BYTE deviceID, BYTE volume);
64 
65 // Version 2.07 Extensions
66 typedef void (CALLBACK* HsidDLL2_Mute2_t) (BYTE deviceID, BYTE channel, BOOL mute, BOOL manual);
67 
68 struct HsidDLL2
69 {
70  HINSTANCE Instance;
71  HsidDLL2_Delay_t Delay;
72  HsidDLL2_Devices_t Devices;
73  HsidDLL2_Filter_t Filter;
74  HsidDLL2_Flush_t Flush;
75  HsidDLL2_Lock_t Lock;
76  HsidDLL2_Unlock_t Unlock;
77  HsidDLL2_Mute_t Mute;
78  HsidDLL2_Mute2_t Mute2;
79  HsidDLL2_MuteAll_t MuteAll;
80  HsidDLL2_Reset_t Reset;
81  HsidDLL2_Reset2_t Reset2;
82  HsidDLL2_Read_t Read;
83  HsidDLL2_Sync_t Sync;
84  HsidDLL2_Write_t Write;
85  WORD Version;
86 };
87 
88 #endif // _WIN32
89 
90 #define HARDSID_VOICES 3
91 // Approx 60ms
92 #define HARDSID_DELAY_CYCLES 60000
93 
94 /***************************************************************************
95  * HardSID SID Specialisation
96  ***************************************************************************/
97 class HardSID : public sidemu, private Event
98 {
99 private:
100  friend class HardSIDBuilder;
101 
102  // HardSID specific data
103 #ifndef _WIN32
104  static bool m_sidFree[16];
105  int m_handle;
106 #endif
107 
108  static const unsigned int voices;
109  static unsigned int sid;
110 
111  static std::string m_credit;
112 
113 
114  // Generic variables
115  EventContext *m_eventContext;
116  event_clock_t m_accessClk;
117  std::string m_errorBuffer;
118 
119  // Must stay in this order
120  bool muted[HARDSID_VOICES];
121  unsigned int m_instance;
122  bool m_status;
123  bool m_locked;
124 
125 public:
126  static const char* getCredits();
127 
128 public:
129  HardSID(sidbuilder *builder);
130  ~HardSID();
131 
132  // Standard component functions
133  const char *credits () const { return getCredits(); }
134 
135  void reset() { sidemu::reset (); }
136  void reset(uint8_t volume);
137 
138  uint8_t read(uint_least8_t addr);
139  void write(uint_least8_t addr, uint8_t data);
140 
141  void clock();
142  const char *error() const { return m_errorBuffer.c_str(); }
143  bool getStatus() const { return m_status; }
144 
145  // Standard SID functions
146  void filter(bool enable);
147  void model(SidConfig::sid_model_t model SID_UNUSED) {;}
148  void voice(unsigned int num, bool mute);
149  // HardSID specific
150  void flush();
151 
152  // Must lock the SID before using the standard functions.
153  bool lock(EventContext *env);
154  void unlock();
155 
156 private:
157  // Fixed interval timer delay to prevent sidplay2
158  // shoot to 100% CPU usage when song nolonger
159  // writes to SID.
160  void event();
161 };
162 
163 #endif // HARDSID_EMU_H