ref: 84a05be9dbc9d1b181670f8fd7e036bbe691d856
parent: ca7f823d48bb78eda9048750909cdb315836125c
author: Simon Howard <[email protected]>
date: Tue Mar 10 17:55:55 EDT 2009
Add OPL lib plumbing and Linux native driver. Subversion-branch: /branches/opl-branch Subversion-revision: 1458
--- a/configure.in
+++ b/configure.in
@@ -54,7 +54,7 @@
])
AC_CHECK_HEADERS([linux/kd.h dev/isa/spkrio.h dev/speaker/speaker.h])
-AC_CHECK_FUNCS(mmap sched_setaffinity)
+AC_CHECK_FUNCS(mmap sched_setaffinity ioperm)
# DWF 2008-02-10: FIXME
AC_CHECK_LIB(samplerate, src_new)
--- a/opl/Makefile.am
+++ b/opl/Makefile.am
@@ -4,6 +4,8 @@
noinst_LIBRARIES=libopl.a
libopl_a_SOURCES = \
+ opl_internal.h \
opl.c opl.h \
- fmopl.c
+ opl_linux.c \
+ fmopl.c fmopl.h
--- a/opl/opl.c
+++ b/opl/opl.c
@@ -23,27 +23,71 @@
//
//-----------------------------------------------------------------------------
+#include "config.h"
+
+#include <stdlib.h>
+
#include "opl.h"
+#include "opl_internal.h"
+#ifdef HAVE_IOPERM
+extern opl_driver_t opl_linux_driver;
+#endif
+
+static opl_driver_t *drivers[] =
+{
+#ifdef HAVE_IOPERM
+ &opl_linux_driver,
+#endif
+ NULL
+};
+
+static opl_driver_t *driver = NULL;
+
int OPL_Init(unsigned int port_base)
{
- // TODO
- return 1;
+ int i;
+
+ // Try drivers until we find a working one:
+
+ for (i=0; drivers[i] != NULL; ++i)
+ {
+ if (drivers[i]->init_func(port_base))
+ {
+ driver = drivers[i];
+ return 1;
+ }
+ }
+
+ return 0;
}
void OPL_Shutdown(void)
{
- // TODO
+ if (driver != NULL)
+ {
+ driver->shutdown_func();
+ driver = NULL;
+ }
}
void OPL_WritePort(opl_port_t port, unsigned int value)
{
- // TODO
+ if (driver != NULL)
+ {
+ driver->write_port_func(port, value);
+ }
}
unsigned int OPL_ReadPort(opl_port_t port)
{
- // TODO
- return 0;
+ if (driver != NULL)
+ {
+ return driver->read_port_func(port);
+ }
+ else
+ {
+ return 0;
+ }
}
--- /dev/null
+++ b/opl/opl_internal.h
@@ -1,0 +1,96 @@
+// 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 internal interface.
+//
+//-----------------------------------------------------------------------------
+
+
+#ifndef OPL_INTERNAL_H
+#define OPL_INTERNAL_H
+
+#include "opl.h"
+
+typedef int (*opl_init_func)(unsigned int port_base);
+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 struct
+{
+ char *name;
+
+ opl_init_func init_func;
+ opl_shutdown_func shutdown_func;
+ opl_read_port_func read_port_func;
+ opl_write_port_func write_port_func;
+} opl_driver_t;
+
+#endif /* #ifndef OPL_INTERNAL_H */
+
+// 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 internal interface.
+//
+//-----------------------------------------------------------------------------
+
+
+#ifndef OPL_INTERNAL_H
+#define OPL_INTERNAL_H
+
+#include "opl.h"
+
+typedef int (*opl_init_func)(unsigned int port_base);
+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 struct
+{
+ char *name;
+
+ opl_init_func init_func;
+ opl_shutdown_func shutdown_func;
+ opl_read_port_func read_port_func;
+ opl_write_port_func write_port_func;
+} opl_driver_t;
+
+#endif /* #ifndef OPL_INTERNAL_H */
+
--- /dev/null
+++ b/opl/opl_linux.c
@@ -1,0 +1,79 @@
+// 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 Linux interface.
+//
+//-----------------------------------------------------------------------------
+
+#include "config.h"
+
+#ifdef HAVE_IOPERM
+
+#include <unistd.h>
+#include <sys/io.h>
+
+#include "opl.h"
+#include "opl_internal.h"
+
+static unsigned int opl_port_base;
+
+static void OPL_Linux_Init(unsigned int port_base)
+{
+ // Try to get permissions:
+
+ if (ioperm(port_base, 2, 1) < 0)
+ {
+ return 0;
+ }
+
+ opl_port_base = port_base;
+
+ return 1;
+}
+
+static void OPL_Linux_Shutdown(void)
+{
+ // Release permissions
+
+ ioperm(opl_port_base, 2, 0);
+}
+
+static unsigned int OPL_Linux_PortRead(opl_port_t port)
+{
+ return inb(opl_port_base + port);
+}
+
+static void OPL_Linux_PortWrite(opl_port_t port, unsigned int value)
+{
+ outb(opl_port_base + port, value);
+}
+
+opl_driver_t opl_linux_driver =
+{
+ "Linux",
+ OPL_Linux_Init,
+ OPL_Linux_Shutdown,
+ OPL_Linux_PortRead,
+ OPL_Linux_PortWrite
+};
+
+#endif /* #ifdef HAVE_IOPERM */
+