29#if defined(__linux__) || defined(__linux) || defined(linux) || defined(__unix__) || defined(__APPLE__)
30 #define __US_LINUX_COMPILE
31#elif defined(_WIN32) || defined(_WIN64) || defined(__MINGW32__) || defined(__MINGW64__)
32 #define __US_WINDOWS_COMPILE
40#if defined(__US_WINDOWS_COMPILE)
42 #if defined(__aarch64__) || defined(_M_ARM64) || defined(__arm__) || defined(_M_ARM) || defined(_ARM_)
51 #define WINAPI __stdcall
54 #define LIBUSB_CALL WINAPI
64#pragma GCC diagnostic push
65#pragma GCC diagnostic ignored "-Wunused-variable"
82 #include <stdatomic.h>
90#if defined(__clang__) && defined(__MINGW32__)
91 #if defined(__x86_64__) || defined(__aarch64__)
115 #define USBDBG(...) fprintf(__VA_ARGS__)
117 #define USBDBG(...) ((void)0)
119#define USBERR(...) fprintf(__VA_ARGS__)
124struct libusb_context;
125struct libusb_transfer;
142 #ifdef DEBUG_USBSID_MEMORY
169 static int run_thread;
172 #ifdef DEBUG_USBSID_MEMORY
173 static uint8_t sid_memory[0x20];
174 static uint8_t sid_memory_changed[0x20];
175 static uint16_t sid_memory_cycles[0x20];
179 static struct libusb_device_handle *devh = NULL;
180 static struct libusb_transfer *transfer_out = NULL;
181 static struct libusb_transfer *transfer_in = NULL;
182 static libusb_context *ctx = NULL;
183 static bool in_buffer_dma =
false;
184 static bool out_buffer_dma =
false;
186 static bool threaded =
false;
187 static bool withcycles =
false;
188 static int rc, read_completed, write_completed;
191 static uint8_t * __restrict__ in_buffer;
192 static uint8_t * __restrict__ out_buffer;
193 static uint8_t * __restrict__ thread_buffer;
194 static uint8_t * __restrict__ write_buffer;
195 #ifdef DEBUG_USBSID_MEMORY
196 static uint8_t * __restrict__ temp_buffer;
198 static uint8_t * __restrict__ result;
199 static int len_out_buffer;
200 static int buffer_pos = 1;
201 static int flush_buffer = 0;
208 uint8_t * __restrict__ ringbuffer;
210 static ring_buffer_t us_ringbuffer;
211 const int min_diff_size = 16;
212 const int min_ring_size = 256;
213 const int default_diff_size = 64;
214 const int default_ring_size = 8192;
270 static const enum clock_speeds clockSpeed[] = { DEFAULT, PAL, NTSC, DREAN, NTSC2 };
271 static const enum refresh_rates refreshRate[] = { HZ_DEFAULT, HZ_EU, HZ_US, HZ_US, HZ_US };
272 static const enum raster_rates rasterRate[] = { R_DEFAULT, R_EU, R_US, R_US, R_US };
273 static long cycles_per_sec = DEFAULT;
274 static long cycles_per_frame = HZ_DEFAULT;
275 static long cycles_per_raster = R_DEFAULT;
276 static int clk_retrieved = 0;
277 static long us_clkrate = 0;
278 static int numsids = 0;
279 static int fmoplsid = -1;
280 static int pcbversion = -1;
281 static int socketconfig = -1;
284 static int us_Found = 0;
285 static int instance = -1;
288 typedef std::nano ratio_t;
289 typedef std::chrono::steady_clock::time_point timestamp_t;
290 typedef std::chrono::nanoseconds duration_t;
293 static std::atomic_int us_thread(0);
295 static _Atomic
int us_thread = 0;
297 static pthread_mutex_t us_mutex;
302 static bool us_Initialised;
303 static bool us_Available;
304 static bool us_PortIsOpen;
308 static double us_CPUcycleDuration;
309 static double us_InvCPUcycleDurationNanoSeconds;
310 static timestamp_t m_StartTime;
311 static timestamp_t m_LastTime;
314 static int diff_size;
315 static int ring_size;
318 int LIBUSB_Setup(
bool start_threaded,
bool with_cycles);
319 int LIBUSB_Exit(
void);
320 int LIBUSB_Available(uint16_t vendor_id, uint16_t product_id);
321 void LIBUSB_StopTransfers(
void);
322 int LIBUSB_OpenDevice(
void);
323 void LIBUSB_CloseDevice(
void);
324 int LIBUSB_DetachKernelDriver(
void);
325 int LIBUSB_ConfigureDevice(
void);
326 void LIBUSB_InitOutBuffer(
void);
327 void LIBUSB_FreeOutBuffer(
void);
328 void LIBUSB_InitInBuffer(
void);
329 void LIBUSB_FreeInBuffer(
void);
330 static void LIBUSB_CALL usb_out(
struct libusb_transfer *transfer);
331 static void LIBUSB_CALL usb_in(
struct libusb_transfer *transfer);
334 unsigned char encoding[7] = { 0x40, 0x54, 0x89, 0x00, 0x00, 0x00, 0x08 };
337 void* USBSID_Thread(
void);
338 int USBSID_InitThread(
void);
339 void USBSID_StopThread(
void);
340 int USBSID_IsRunning(
void);
344 void USBSID_ResetRingBuffer(
void);
345 void USBSID_InitRingBuffer(
int buffer_size,
int differ_size);
346 void USBSID_InitRingBuffer(
void);
347 void USBSID_DeInitRingBuffer(
void);
348 bool USBSID_IsHigher(
void);
349 int USBSID_RingDiff(
void);
350 void USBSID_RingPut(uint8_t item);
351 uint8_t USBSID_RingGet(
void);
352 void USBSID_FlushBuffer(
void);
355 void USBSID_RingPopCycled(
void);
356 void USBSID_RingPop(
void);
364 int USBSID_Init(
bool start_threaded,
bool with_cycles);
365 int USBSID_Close(
void);
366 int USBSID_GetInstanceID(
void){
return us_InstanceID; };
367 bool USBSID_isInitialised(
void){
return us_Initialised; };
368 bool USBSID_isAvailable(
void){
return us_Available; };
369 bool USBSID_isOpen(
void){
return us_PortIsOpen; };
372 void USBSID_Pause(
void);
373 void USBSID_Reset(
void);
374 void USBSID_ResetAllRegisters(
void);
375 void USBSID_Mute(
void);
376 void USBSID_UnMute(
void);
377 void USBSID_DisableSID(
void);
378 void USBSID_EnableSID(
void);
379 void USBSID_ClearBus(
void);
380 void USBSID_SetClockRate(
long clockrate_cycles,
382 long USBSID_GetClockRate(
void);
383 long USBSID_GetRefreshRate(
void);
384 long USBSID_GetRasterRate(
void);
385 uint8_t* USBSID_GetSocketConfig(uint8_t socket_config[]);
386 int USBSID_GetSocketNumSIDS(
int socket, uint8_t socket_config[]);
387 int USBSID_GetSocketChipType(
int socket, uint8_t socket_config[]);
388 int USBSID_GetSocketSIDType1(
int socket, uint8_t socket_config[]);
389 int USBSID_GetSocketSIDType2(
int socket, uint8_t socket_config[]);
390 int USBSID_GetNumSIDs(
void);
391 int USBSID_GetFMOplSID(
void);
392 int USBSID_GetPCBVersion(
void);
393 void USBSID_SetStereo(
int state);
394 void USBSID_ToggleStereo(
void);
397 void USBSID_SingleWrite(
unsigned char *buff,
int len);
398 unsigned char USBSID_SingleRead(uint8_t reg);
399 unsigned char USBSID_SingleReadConfig(
unsigned char *buff,
int len);
402 void USBSID_Write(
unsigned char *buff,
size_t len);
403 void USBSID_Write(uint8_t reg, uint8_t val);
404 void USBSID_Write(
unsigned char *buff,
size_t len, uint16_t cycles);
405 void USBSID_Write(uint8_t reg, uint8_t val, uint16_t cycles);
406 void USBSID_WriteCycled(uint8_t reg, uint8_t val, uint16_t cycles);
407 unsigned char USBSID_Read(uint8_t reg);
408 unsigned char USBSID_Read(
unsigned char *writebuff);
409 unsigned char USBSID_Read(
unsigned char *writebuff, uint16_t cycles);
412 void USBSID_WriteRing(uint8_t reg, uint8_t val);
413 void USBSID_WriteRingCycled(uint8_t reg, uint8_t val, uint16_t cycles);
416 void USBSID_EnableThread(
void);
417 void USBSID_DisableThread(
void);
420 void USBSID_SetFlush(
void);
421 void USBSID_Flush(
void);
422 void USBSID_SetBufferSize(
int size);
423 void USBSID_SetDiffSize(
int size);
424 void USBSID_RestartRingBuffer(
void);
427 void USBSID_RestartThread(
bool with_cycles);
428 static void *_USBSID_Thread(
void *context)
430 return ((USBSID_Class *)context)->USBSID_Thread();
434 uint_fast64_t USBSID_WaitForCycle(uint_fast16_t cycles);
435 uint_fast64_t USBSID_WaitForCycle_(uint_fast16_t cycles);
436 void USBSID_SyncTime(
void);
440 uint8_t USBSID_Address(uint16_t addr);
447#pragma GCC diagnostic pop
448#pragma GCC pop_options