shithub: choc

Download patch

ref: 98ee23f4268dbb1395aa0b2cbfad9f53d1092b33
parent: 6e4f6ab9626d81e4106d3ccc974a76d832fdff13
author: Simon Howard <[email protected]>
date: Sat May 30 19:24:11 EDT 2009

Add initial callback/timer API.

Subversion-branch: /branches/opl-branch
Subversion-revision: 1538

--- a/opl/Makefile.am
+++ b/opl/Makefile.am
@@ -10,5 +10,7 @@
         opl.c               opl.h                 \
         opl_linux.c                               \
         opl_sdl.c                                 \
+        opl_queue.c         opl_queue.h           \
+        opl_timer.c         opl_timer.h           \
         fmopl.c             fmopl.h
 
--- /dev/null
+++ b/opl/examples/.gitignore
@@ -1,0 +1,5 @@
+Makefile.in
+Makefile
+.deps
+droplay
+
--- a/opl/examples/droplay.c
+++ b/opl/examples/droplay.c
@@ -116,44 +116,51 @@
     OPL_Shutdown();
 }
 
-void PlayFile(char *filename)
+struct timer_data
 {
-    FILE *stream;
-    char buf[8];
+    int running;
+    FILE *fstream;
+};
 
-    stream = fopen(filename, "rb");
+void TimerCallback(void *data)
+{
+    struct timer_data *timer_data = data;
+    int delay;
 
-    if (fread(buf, 1, 8, stream) < 8)
+    if (!timer_data->running)
     {
-        fprintf(stderr, "failed to read raw OPL header\n");
-        exit(-1);
+        return;
     }
 
-    if (strncmp(buf, HEADER_STRING, 8) != 0)
-    {
-        fprintf(stderr, "Raw OPL header not found\n");
-        exit(-1);
-    }
+    // Read data until we must make a delay.
 
-    fseek(stream, 28, SEEK_SET);
-
-    while (!feof(stream))
+    for (;;)
     {
         int reg, val;
 
-        reg = fgetc(stream);
-        val = fgetc(stream);
+        // End of file?
 
-        // Delay?
+        if (feof(timer_data->fstream))
+        {
+            timer_data->running = 0;
+            return;
+        }
 
+        reg = fgetc(timer_data->fstream);
+        val = fgetc(timer_data->fstream);
+
+        // Register value of 0 or 1 indicates a delay.
+
         if (reg == 0x00)
         {
-            SDL_Delay(val);
+            delay = val;
+            break;
         }
         else if (reg == 0x01)
         {
-            val |= (fgetc(stream) << 8);
-            SDL_Delay(val);
+            val |= (fgetc(timer_data->fstream) << 8);
+            delay = val;
+            break;
         }
         else
         {
@@ -161,7 +168,50 @@
         }
     }
 
-    fclose(stream);
+    // Schedule the next timer callback.
+
+    OPL_SetCallback(delay, TimerCallback, timer_data);
+}
+
+void PlayFile(char *filename)
+{
+    struct timer_data timer_data;
+    int running;
+    char buf[8];
+
+    timer_data.fstream = fopen(filename, "rb");
+
+    if (fread(buf, 1, 8, timer_data.fstream) < 8)
+    {
+        fprintf(stderr, "failed to read raw OPL header\n");
+        exit(-1);
+    }
+
+    if (strncmp(buf, HEADER_STRING, 8) != 0)
+    {
+        fprintf(stderr, "Raw OPL header not found\n");
+        exit(-1);
+    }
+
+    fseek(timer_data.fstream, 28, SEEK_SET);
+    timer_data.running = 1;
+
+    // Start callback loop sequence.
+
+    OPL_SetCallback(0, TimerCallback, &timer_data);
+
+    // Sleep until the playback finishes.
+
+    do
+    {
+        OPL_Lock();
+        running = timer_data.running;
+        OPL_Unlock();
+
+        SDL_Delay(100);
+    } while (running);
+
+    fclose(timer_data.fstream);
 }
 
 int main(int argc, char *argv[])
--- a/opl/opl.c
+++ b/opl/opl.c
@@ -105,3 +105,27 @@
     }
 }
 
+void OPL_SetCallback(unsigned int ms, opl_callback_t callback, void *data)
+{
+    if (driver != NULL)
+    {
+        driver->set_callback_func(ms, callback, data);
+    }
+}
+
+void OPL_Lock(void)
+{
+    if (driver != NULL)
+    {
+        driver->lock_func();
+    }
+}
+
+void OPL_Unlock(void)
+{
+    if (driver != NULL)
+    {
+        driver->unlock_func();
+    }
+}
+
--- a/opl/opl.h
+++ b/opl/opl.h
@@ -27,6 +27,8 @@
 #ifndef OPL_OPL_H
 #define OPL_OPL_H
 
+typedef void (*opl_callback_t)(void *data);
+
 typedef enum
 {
     OPL_REGISTER_PORT = 0,
@@ -71,6 +73,20 @@
 // Read from one of the OPL I/O ports:
 
 unsigned int OPL_ReadPort(opl_port_t port);
+
+// Set a timer callback.  After the specified number of milliseconds
+// have elapsed, the callback will be invoked.
+
+void OPL_SetCallback(unsigned int ms, opl_callback_t callback, void *data);
+
+// Begin critical section, during which, OPL callbacks will not be
+// invoked.
+
+void OPL_Lock(void);
+
+// End critical section.
+
+void OPL_Unlock(void);
 
 #endif
 
--- a/opl/opl_internal.h
+++ b/opl/opl_internal.h
@@ -33,6 +33,11 @@
 typedef void (*opl_shutdown_func)(void);
 typedef unsigned int (*opl_read_port_func)(opl_port_t port);
 typedef void (*opl_write_port_func)(opl_port_t port, unsigned int value);
+typedef void (*opl_set_callback_func)(unsigned int ms,
+                                      opl_callback_t callback,
+                                      void *data);
+typedef void (*opl_lock_func)(void);
+typedef void (*opl_unlock_func)(void);
 
 typedef struct
 {
@@ -42,6 +47,9 @@
     opl_shutdown_func shutdown_func;
     opl_read_port_func read_port_func;
     opl_write_port_func write_port_func;
+    opl_set_callback_func set_callback_func;
+    opl_lock_func lock_func;
+    opl_unlock_func unlock_func;
 } opl_driver_t;
 
 #endif /* #ifndef OPL_INTERNAL_H */
--- a/opl/opl_linux.c
+++ b/opl/opl_linux.c
@@ -35,6 +35,7 @@
 
 #include "opl.h"
 #include "opl_internal.h"
+#include "opl_timer.h"
 
 static unsigned int opl_port_base;
 
@@ -51,11 +52,23 @@
 
     opl_port_base = port_base;
 
+    // Start callback thread
+
+    if (!OPL_Timer_StartThread())
+    {
+        ioperm(port_base, 2, 0);
+        return 0;
+    }
+
     return 1;
 }
 
 static void OPL_Linux_Shutdown(void)
 {
+    // Stop callback thread
+
+    OPL_Timer_StopThread();
+
     // Release permissions
 
     ioperm(opl_port_base, 2, 0);
@@ -77,7 +90,10 @@
     OPL_Linux_Init,
     OPL_Linux_Shutdown,
     OPL_Linux_PortRead,
-    OPL_Linux_PortWrite
+    OPL_Linux_PortWrite,
+    OPL_Timer_SetCallback,
+    OPL_Timer_Lock,
+    OPL_Timer_Unlock
 };
 
 #endif /* #ifdef HAVE_IOPERM */
--- /dev/null
+++ b/opl/opl_queue.c
@@ -1,0 +1,192 @@
+// Emacs style mode select   -*- C++ -*- 
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 Simon Howard
+//
+// 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 2
+// of the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
+//
+// DESCRIPTION:
+//     Queue of waiting callbacks, stored in a binary min heap, so that we
+//     can always get the first callback.
+//
+//-----------------------------------------------------------------------------
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "opl_queue.h"
+
+#define MAX_OPL_QUEUE 64
+
+typedef struct
+{
+    opl_callback_t callback;
+    void *data;
+    unsigned int time;
+} opl_queue_entry_t;
+
+struct opl_callback_queue_s
+{
+    opl_queue_entry_t entries[MAX_OPL_QUEUE];
+    unsigned int num_entries;
+};
+
+opl_callback_queue_t *OPL_Queue_Create(void)
+{
+    opl_callback_queue_t *queue;
+
+    queue = malloc(sizeof(opl_callback_queue_t));
+    queue->num_entries = 0;
+
+    return queue;
+}
+
+void OPL_Queue_Destroy(opl_callback_queue_t *queue)
+{
+    free(queue);
+}
+
+int OPL_Queue_IsEmpty(opl_callback_queue_t *queue)
+{
+    return queue->num_entries == 0;
+}
+
+void OPL_Queue_Push(opl_callback_queue_t *queue,
+                    opl_callback_t callback, void *data,
+                    unsigned int time)
+{
+    int entry_id;
+
+    if (queue->num_entries >= MAX_OPL_QUEUE)
+    {
+        fprintf(stderr, "OPL_Queue_Push: Exceeded maximum callbacks\n");
+        return;
+    }
+
+    // Add to last queue entry.
+
+    entry_id = queue->num_entries;
+    ++queue->num_entries;
+
+    // Shift existing entries down in the heap.
+
+    while (entry_id > 0 && time < queue->entries[entry_id / 2].time)
+    {
+        memcpy(&queue->entries[entry_id],
+               &queue->entries[entry_id / 2],
+               sizeof(opl_queue_entry_t));
+
+        entry_id /= 2;
+    }
+
+    // Insert new callback data.
+
+    queue->entries[entry_id].callback = callback;
+    queue->entries[entry_id].data = data;
+    queue->entries[entry_id].time = time;
+}
+
+int OPL_Queue_Pop(opl_callback_queue_t *queue,
+                  opl_callback_t *callback, void **data)
+{
+    opl_queue_entry_t *entry;
+    int child1, child2;
+    int i, next_i;
+
+    // Empty?
+
+    if (queue->num_entries <= 0)
+    {
+        return 0;
+    }
+
+    // Store the result:
+
+    *callback = queue->entries[0].callback;
+    *data = queue->entries[0].data;
+
+    // Decrease the heap size, and keep pointer to the last entry in
+    // the heap, which must now be percolated down from the top.
+
+    --queue->num_entries;
+    entry = &queue->entries[queue->num_entries];
+
+    // Percolate down.
+
+    i = 0;
+
+    for (;;)
+    {
+        child1 = i * 2 + 1;
+        child2 = i * 2 + 2;
+
+        if (child1 < queue->num_entries
+         && queue->entries[child1].time < entry->time)
+        {
+            // Left child is less than entry.
+            // Use the minimum of left and right children.
+
+            if (child2 < queue->num_entries
+             && queue->entries[child2].time < queue->entries[child1].time)
+            {
+                next_i = child2;
+            }
+            else
+            {
+                next_i = child1;
+            }
+        }
+        else if (child2 < queue->num_entries
+              && queue->entries[child2].time < entry->time)
+        {
+            // Right child is less than entry.  Go down the right side.
+
+            next_i = child2;
+        }
+        else
+        {
+            // Finished percolating.
+            break;
+        }
+
+        // Percolate the next value up and advance.
+
+        memcpy(&queue->entries[i],
+               &queue->entries[next_i],
+               sizeof(opl_queue_entry_t));
+        i = next_i;
+    }
+
+    // Store the old last-entry at its new position.
+
+    memcpy(&queue->entries[i], entry, sizeof(opl_queue_entry_t));
+
+    return 1;
+}
+
+unsigned int OPL_Queue_Peek(opl_callback_queue_t *queue)
+{
+    if (queue->num_entries > 0)
+    {
+        return queue->entries[0].time;
+    }
+    else
+    {
+        return 0;
+    }
+}
+
--- /dev/null
+++ b/opl/opl_queue.h
@@ -1,0 +1,44 @@
+// Emacs style mode select   -*- C++ -*- 
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 Simon Howard
+//
+// 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 2
+// of the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
+//
+// DESCRIPTION:
+//     OPL callback queue.
+//
+//-----------------------------------------------------------------------------
+
+#ifndef OPL_QUEUE_H
+#define OPL_QUEUE_H
+
+#include "opl.h"
+
+typedef struct opl_callback_queue_s opl_callback_queue_t;
+
+opl_callback_queue_t *OPL_Queue_Create(void);
+int OPL_Queue_IsEmpty(opl_callback_queue_t *queue);
+void OPL_Queue_Destroy(opl_callback_queue_t *queue);
+void OPL_Queue_Push(opl_callback_queue_t *queue,
+                    opl_callback_t callback, void *data,
+                    unsigned int time);
+int OPL_Queue_Pop(opl_callback_queue_t *queue,
+                  opl_callback_t *callback, void **data);
+unsigned int OPL_Queue_Peek(opl_callback_queue_t *queue);
+
+#endif /* #ifndef OPL_QUEUE_H */
+
--- a/opl/opl_sdl.c
+++ b/opl/opl_sdl.c
@@ -37,9 +37,12 @@
 #include "opl.h"
 #include "opl_internal.h"
 
+#include "opl_queue.h"
+
 // TODO:
 #define opl_sample_rate 22050
 
+static opl_callback_queue_t *callback_queue;
 static FM_OPL *opl_emulator = NULL;
 static int sdl_was_initialised = 0;
 static int mixing_freq, mixing_channels;
@@ -65,6 +68,7 @@
     {
         Mix_CloseAudio();
         SDL_QuitSubSystem(SDL_INIT_AUDIO);
+        OPL_Queue_Destroy(callback_queue);
         sdl_was_initialised = 0;
     }
 
@@ -82,6 +86,8 @@
 
     if (!SDLIsInitialised())
     {
+        callback_queue = OPL_Queue_Create();
+
         if (SDL_Init(SDL_INIT_AUDIO) < 0)
         {
             fprintf(stderr, "Unable to set up sound.\n");
@@ -161,6 +167,20 @@
     }
 }
 
+static void OPL_SDL_SetCallback(unsigned int ms,
+                                opl_callback_t callback,
+                                void *data)
+{
+}
+
+static void OPL_SDL_Lock(void)
+{
+}
+
+static void OPL_SDL_Unlock(void)
+{
+}
+
 opl_driver_t opl_sdl_driver =
 {
     "SDL",
@@ -167,6 +187,9 @@
     OPL_SDL_Init,
     OPL_SDL_Shutdown,
     OPL_SDL_PortRead,
-    OPL_SDL_PortWrite
+    OPL_SDL_PortWrite,
+    OPL_SDL_SetCallback,
+    OPL_SDL_Lock,
+    OPL_SDL_Unlock
 };
 
--- /dev/null
+++ b/opl/opl_timer.c
@@ -1,0 +1,197 @@
+// Emacs style mode select   -*- C++ -*- 
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 Simon Howard
+//
+// 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 2
+// of the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
+//
+// DESCRIPTION:
+//     OPL timer thread.
+//     Once started using OPL_Timer_StartThread, the thread sleeps,
+//     waking up to invoke callbacks set using OPL_Timer_SetCallback.
+//
+//-----------------------------------------------------------------------------
+
+#include "SDL.h"
+
+#include "opl_timer.h"
+#include "opl_queue.h"
+
+typedef enum
+{
+    THREAD_STATE_STOPPED,
+    THREAD_STATE_RUNNING,
+    THREAD_STATE_STOPPING,
+} thread_state_t;
+
+static SDL_Thread *timer_thread = NULL;
+static thread_state_t timer_thread_state;
+static int current_time;
+
+// Queue of callbacks waiting to be invoked.
+// The callback queue mutex is held while the callback queue structure
+// or current_time is being accessed.
+
+static opl_callback_queue_t *callback_queue;
+static SDL_mutex *callback_queue_mutex;
+
+// The timer mutex is held while timer callback functions are being
+// invoked, so that the calling code can prevent clashes.
+
+static SDL_mutex *timer_mutex;
+
+// Returns true if there is a callback at the head of the queue ready
+// to be invoked.  Otherwise, next_time is set to the time when the
+// timer thread must wake up again to check.
+
+static int CallbackWaiting(unsigned int *next_time)
+{
+    // If there are no queued callbacks, sleep for 50ms at a time
+    // until a callback is added.
+
+    if (OPL_Queue_IsEmpty(callback_queue))
+    {
+        *next_time = current_time + 50;
+        return 0;
+    }
+
+    // Read the time of the first callback in the queue.
+    // If the time for the callback has not yet arrived,
+    // we must sleep until the callback time.
+
+    *next_time = OPL_Queue_Peek(callback_queue);
+
+    return *next_time <= current_time;
+}
+
+static unsigned int GetNextTime(void)
+{
+    opl_callback_t callback;
+    void *callback_data;
+    unsigned int next_time;
+    int have_callback;
+
+    // Keep running through callbacks until there are none ready to
+    // run. When we run out of callbacks, next_time will be set.
+
+    do
+    {
+        SDL_LockMutex(callback_queue_mutex);
+
+        // Check if the callback at the head of the list is ready to
+        // be invoked.  If so, pop from the head of the queue.
+
+        have_callback = CallbackWaiting(&next_time);
+
+        if (have_callback)
+        {
+            OPL_Queue_Pop(callback_queue, &callback, &callback_data);
+        }
+
+        SDL_UnlockMutex(callback_queue_mutex);
+
+        // Now invoke the callback, if we have one.
+        // The timer mutex is held while the callback is invoked.
+
+        if (have_callback)
+        {
+            SDL_LockMutex(timer_mutex);
+            callback(callback_data);
+            SDL_UnlockMutex(timer_mutex);
+        }
+    } while (have_callback);
+
+    return next_time;
+}
+
+static int ThreadFunction(void *unused)
+{
+    unsigned int next_time;
+    unsigned int now;
+
+    // Keep running until OPL_Timer_StopThread is called.
+
+    while (timer_thread_state == THREAD_STATE_RUNNING)
+    {
+        // Get the next time that we must sleep until, and
+        // wait until that time.
+
+        next_time = GetNextTime();
+        now = SDL_GetTicks();
+
+        if (next_time > now)
+        {
+            SDL_Delay(next_time - now);
+        }
+
+        // Update the current time.
+
+        SDL_LockMutex(callback_queue_mutex);
+        current_time = next_time;
+        SDL_UnlockMutex(callback_queue_mutex);
+    }
+
+    timer_thread_state = THREAD_STATE_STOPPED;
+
+    return 0;
+}
+
+int OPL_Timer_StartThread(void)
+{
+    timer_thread_state = THREAD_STATE_RUNNING;
+    timer_thread = SDL_CreateThread(ThreadFunction, NULL);
+    timer_mutex = SDL_CreateMutex();
+
+    callback_queue = OPL_Queue_Create();
+    callback_queue_mutex = SDL_CreateMutex();
+    current_time = SDL_GetTicks();
+
+    if (timer_thread == NULL)
+    {
+        timer_thread_state = THREAD_STATE_STOPPED;
+        return 0;
+    }
+
+    return 1;
+}
+
+void OPL_Timer_StopThread(void)
+{
+    timer_thread_state = THREAD_STATE_STOPPING;
+
+    while (timer_thread_state != THREAD_STATE_STOPPED)
+    {
+        SDL_Delay(1);
+    }
+}
+
+void OPL_Timer_SetCallback(unsigned int ms, opl_callback_t callback, void *data)
+{
+    SDL_LockMutex(callback_queue_mutex);
+    OPL_Queue_Push(callback_queue, callback, data, current_time + ms);
+    SDL_UnlockMutex(callback_queue_mutex);
+}
+
+void OPL_Timer_Lock(void)
+{
+    SDL_LockMutex(timer_mutex);
+}
+
+void OPL_Timer_Unlock(void)
+{
+    SDL_UnlockMutex(timer_mutex);
+}
+
--- /dev/null
+++ b/opl/opl_timer.h
@@ -1,0 +1,40 @@
+// Emacs style mode select   -*- C++ -*- 
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 Simon Howard
+//
+// 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 2
+// of the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
+//
+// DESCRIPTION:
+//     OPL timer thread.
+//
+//-----------------------------------------------------------------------------
+
+#ifndef OPL_TIMER_H
+#define OPL_TIMER_H
+
+#include "opl.h"
+
+int OPL_Timer_StartThread(void);
+void OPL_Timer_StopThread(void);
+void OPL_Timer_SetCallback(unsigned int ms,
+                           opl_callback_t callback,
+                           void *data);
+void OPL_Timer_Lock(void);
+void OPL_Timer_Unlock(void);
+
+#endif /* #ifndef OPL_TIMER_H */
+