URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
Compare Revisions
- This comparison shows the changes necessary to convert path
/openrisc/tags/or1ksim/or1ksim-0.3.0/peripheral/channels
- from Rev 19 to Rev 21
- ↔ Reverse comparison
Rev 19 → Rev 21
/fd.h
0,0 → 1,50
/* fd.h -- Declaration of functions for peripheral to |
* communicate with host through file descriptors |
|
Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca> |
Copyright (C) 2008 Embecosm Limited |
|
Contributor Jeremy Bennett <jeremy.bennett@embecosm.com> |
|
This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator. |
|
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 3 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, see <http://www.gnu.org/licenses/>. */ |
|
/* This program is commented throughout in a fashion suitable for processing |
with Doxygen. */ |
|
|
#ifndef FD__H |
#define FD__H |
|
/*! Data structure to represent a channel through file descriptors */ |
struct fd_channel |
{ |
int fdin; |
int fdout; |
}; |
|
/* Global data structures */ |
extern struct channel_ops fd_channel_ops; |
|
/* Function prototypes for external use */ |
extern int fd_read (void *data, |
char *buffer, |
int size); |
extern int fd_write (void *data, |
const char *buffer, |
int size); |
|
#endif /* FD__H */ |
|
fd.h
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: xterm.c
===================================================================
--- xterm.c (nonexistent)
+++ xterm.c (revision 21)
@@ -0,0 +1,285 @@
+/* xterm.c -- Definition of functions and structures for
+ peripheral to communicate with host through an xterm.
+ Inspired from SWI-Prolog by Jan Wielemaker (GPL too)
+ even if there is really few in common.
+
+ Copyright (C) 2002 Richard Prescott
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+/* Autoconf and/or portability configuration */
+#include "config.h"
+#include "port.h"
+
+/* System includes */
+#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) && defined(HAVE_PTSNAME)
+#endif
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#if HAVE_SYS_STROPTS_H
+#include /* I_PUSH ioctl */
+#endif
+
+#if HAVE_BASENAME
+#include /* basename() */
+#endif
+
+/* Package includes */
+#include "channel.h"
+#include "generic.h"
+#include "fd.h"
+
+/*! Data structure to represent the connection to the xterm */
+struct xterm_channel
+{
+ struct fd_channel fds;
+ int pid;
+ char **argv;
+};
+
+/* Forward declaration of static functions */
+static void xterm_close (void *data);
+static void *xterm_init (const char *input);
+static int xterm_open (void *data);
+
+/*! Global data structure with the xterm interface functions */
+struct channel_ops xterm_channel_ops = {
+ .init = xterm_init,
+ .open = xterm_open,
+ .close = xterm_close,
+ .read = fd_read,
+ .write = fd_write,
+ .free = generic_free,
+};
+
+
+#if !(HAVE_BASENAME)
+static char *
+basename (const char *filename)
+{
+ char *p = strrchr (filename, '/');
+
+ return p ? p + 1 : (char *) filename;
+}
+#endif /* HAVE_BASENAME */
+
+static void
+xterm_close (void *data)
+{
+ struct xterm_channel *xt = data;
+
+ if (!xt)
+ return;
+
+ if (xt->fds.fdin != -1)
+ close (xt->fds.fdin);
+
+ if (xt->pid != -1)
+ {
+ kill (xt->pid, SIGKILL);
+ waitpid (xt->pid, NULL, 0);
+ }
+
+ if (xt->argv)
+ free (xt->argv);
+
+ xt->fds.fdin = -1;
+ xt->fds.fdout = -1;
+ xt->pid = -1;
+ xt->argv = NULL;
+
+}
+
+#if defined(HAVE_ON_EXIT)
+static void
+xterm_exit (int i, void *data)
+{
+ xterm_close (data);
+}
+#endif
+
+#define MAX_XTERM_ARGS 100
+static void *
+xterm_init (const char *input)
+{
+ struct xterm_channel *retval = malloc (sizeof (struct xterm_channel));
+ if (retval)
+ {
+ int i;
+ char *arglist;
+
+ retval->fds.fdin = -1;
+ retval->fds.fdout = -1;
+ retval->pid = -1;
+
+#if defined(HAVE_ON_EXIT)
+ /* reset cause exit(1), leaving an xterm opened */
+ on_exit (xterm_exit, retval);
+#endif
+
+ i = 2;
+ arglist = (char *) input;
+ retval->argv = malloc (sizeof (char *) * MAX_XTERM_ARGS);
+ if (!retval->argv)
+ {
+ free (retval);
+ return NULL;
+ }
+ /* Assume xterm arguments are separated by whitespace */
+ while ((retval->argv[i++] = strtok (arglist, " \t\n")))
+ {
+ arglist = NULL;
+ if (i == MAX_XTERM_ARGS - 1)
+ {
+ free (retval);
+ return NULL;
+ }
+ }
+
+ }
+ return (void *) retval;
+}
+
+
+
+static int
+xterm_open (void *data)
+{
+#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) && defined(HAVE_PTSNAME)
+ struct xterm_channel *xt = data;
+ int master, retval;
+ char *slavename;
+ struct termios termio;
+ char arg[64], *fin;
+
+ if (!data)
+ {
+ errno = ENODEV;
+ return -1;
+ }
+
+ master = open ("/dev/ptmx", O_RDWR);
+
+ if (master < 0)
+ return -1;
+
+ grantpt (master);
+ unlockpt (master);
+ slavename = (char *) ptsname (master);
+
+ if (!slavename)
+ {
+ errno = ENOTTY;
+ goto closemastererror;
+ }
+
+ xt->fds.fdout = xt->fds.fdin = open (slavename, O_RDWR);
+ if (xt->fds.fdout < 0)
+ goto closemastererror;
+
+#if HAVE_DECL_I_PUSH
+ /* These modules must be pushed onto the stream for some non-Linux and
+ non-Cygwin operating systems. */
+ retval = ioctl (xt->fds.fdin, I_PUSH, "ptem");
+ if (retval < 0)
+ goto closeslaveerror;
+
+ retval = ioctl (xt->fds.fdin, I_PUSH, "ldterm");
+ if (retval < 0)
+ goto closeslaveerror;
+#endif
+
+ retval = tcgetattr (xt->fds.fdin, &termio);
+ if (retval < 0)
+ goto closeslaveerror;
+ termio.c_lflag &= ~ECHO;
+ retval = tcsetattr (xt->fds.fdin, TCSADRAIN, &termio);
+ if (retval < 0)
+ goto closeslaveerror;
+
+ xt->pid = fork ();
+
+ if (xt->pid == -1)
+ goto closeslaveerror;
+
+ if (xt->pid == 0)
+ {
+ /* Ctrl-C on sim still kill the xterm, grrr */
+ signal (SIGINT, SIG_IGN);
+
+ fin = slavename + strlen (slavename) - 2;
+ if (strchr (fin, '/'))
+ {
+ sprintf (arg, "-S%s/%d", basename (slavename), master);
+ }
+ else
+ {
+ sprintf (arg, "-S%c%c%d", fin[0], fin[1], master);
+ }
+ xt->argv[0] = "xterm";
+ xt->argv[1] = arg;
+ execvp ("xterm", xt->argv);
+ write (master, "\n", 1);
+ exit (1);
+ }
+
+ do
+ retval = read (xt->fds.fdin, &arg, 1);
+ while (retval >= 0 && arg[0] != '\n');
+ if (retval < 0)
+ goto closeslaveerror;
+
+ termio.c_lflag |= ECHO;
+ retval = tcsetattr (xt->fds.fdin, TCSADRAIN, &termio);
+
+ if (retval < 0)
+ goto closeslaveerror;
+
+ return 0;
+
+closeslaveerror:
+ close (xt->fds.fdin);
+
+closemastererror:
+ close (master);
+ xt->pid = xt->fds.fdin = xt->fds.fdout = -1;
+ return -1;
+
+#else
+ /* I don't see how this stuff should be working on a system that doesn't know
+ grantpt(), unlockpt(), ptsname(). Mac OS X also does not have /dev/ptmx.
+ -hpanther
+ */
+ return -1;
+#endif
+}
xterm.c
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: xterm.h
===================================================================
--- xterm.h (nonexistent)
+++ xterm.h (revision 21)
@@ -0,0 +1,31 @@
+/* xterm.h -- peripheral communication with host through an xterm header
+
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+#ifndef XTERM__H
+#define XTERM__H
+
+extern struct channel_ops xterm_channel_ops;
+
+#endif /* XTERM__H */
xterm.h
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: channel.c
===================================================================
--- channel.c (nonexistent)
+++ channel.c (revision 21)
@@ -0,0 +1,194 @@
+/* channel.c -- Definition of types and structures for
+ peripherals to communicate with host. Adapted from UML.
+
+ Copyright (C) 2002 Richard Prescott
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+/* Autoconf and/or portability configuration */
+#include "config.h"
+#include "port.h"
+
+/* System includes */
+#include
+#include
+#include
+
+/* Package includes */
+#include "channel.h"
+#include "fd.h"
+#include "file.h"
+#include "tcp.h"
+#include "tty.h"
+#include "xterm.h"
+
+struct channel_factory
+{
+ const char *name;
+ const struct channel_ops *ops;
+ struct channel_factory *next;
+};
+
+static struct channel_factory preloaded[] = {
+ {"fd", &fd_channel_ops, &preloaded[1]},
+ {"file", &file_channel_ops, &preloaded[2]},
+ {"xterm", &xterm_channel_ops, &preloaded[3]},
+ {"tcp", &tcp_channel_ops, &preloaded[4]},
+ {"tty", &tty_channel_ops, NULL}
+};
+
+static struct channel_factory *head = &preloaded[0];
+
+/* Forward declaration of static functions */
+static struct channel_factory *find_channel_factory (const char *name);
+
+struct channel *
+channel_init (const char *descriptor)
+{
+ struct channel *retval;
+ struct channel_factory *current;
+ char *args, *name;
+ int count;
+
+ if (!descriptor)
+ {
+ return NULL;
+ }
+
+ retval = (struct channel *) calloc (1, sizeof (struct channel));
+
+ if (!retval)
+ {
+ perror (descriptor);
+ exit (1);
+ }
+
+ args = strchr (descriptor, ':');
+
+ if (args)
+ {
+ count = args - descriptor;
+ args++;
+ }
+ else
+ {
+ count = strlen (descriptor);
+ }
+
+ name = (char *) strndup (descriptor, count);
+
+ if (!name)
+ {
+ perror (name);
+ exit (1);
+ }
+
+ current = find_channel_factory (name);
+
+ if (!current)
+ {
+ errno = ENODEV;
+ perror (descriptor);
+ exit (1);
+ }
+
+ retval->ops = current->ops;
+
+ free (name);
+
+ if (!retval->ops)
+ {
+ errno = ENODEV;
+ perror (descriptor);
+ exit (1);
+ }
+
+ if (retval->ops->init)
+ {
+ retval->data = (retval->ops->init) (args);
+
+ if (!retval->data)
+ {
+ perror (descriptor);
+ exit (1);
+ }
+ }
+
+ return retval;
+}
+
+int
+channel_open (struct channel *channel)
+{
+ if (channel && channel->ops && channel->ops->open)
+ {
+ return (channel->ops->open) (channel->data);
+ }
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+channel_read (struct channel *channel, char *buffer, int size)
+{
+ if (channel && channel->ops && channel->ops->read)
+ {
+ return (channel->ops->read) (channel->data, buffer, size);
+ }
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+channel_write (struct channel *channel, const char *buffer, int size)
+{
+ if (channel && channel->ops && channel->ops->write)
+ {
+ return (channel->ops->write) (channel->data, buffer, size);
+ }
+ errno = ENOSYS;
+ return -1;
+}
+
+void
+channel_close (struct channel *channel)
+{
+ if (channel && channel->ops && channel->ops->close)
+ {
+ (channel->ops->close) (channel->data);
+ }
+}
+
+static struct channel_factory *
+find_channel_factory (const char *name)
+{
+ struct channel_factory *current = head;
+
+ current = head;
+ while (current && strcmp (current->name, name))
+ {
+ current = current->next;
+ }
+
+ return current;
+}
channel.c
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: generic.c
===================================================================
--- generic.c (nonexistent)
+++ generic.c (revision 21)
@@ -0,0 +1,62 @@
+/* generic.c -- Definition of generic functions for peripheral to
+ * communicate with host
+
+ Copyright (C) 2002 Richard Prescott
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+/* Autoconf and/or portability configuration */
+#include "config.h"
+
+/* System includes */
+#include
+#include
+
+
+int
+generic_open (void *data)
+{
+ if (data)
+ {
+ return 0;
+ }
+ errno = ENODEV;
+ return -1;
+}
+
+
+void
+generic_close (void *data)
+{
+ return;
+}
+
+
+void
+generic_free (void *data)
+{
+ if (data)
+ {
+ free (data);
+ }
+}
generic.c
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: channel.h
===================================================================
--- channel.h (nonexistent)
+++ channel.h (revision 21)
@@ -0,0 +1,63 @@
+/* channel.h -- Definition of types and structures for
+ peripheral to communicate with host. Addapted from UML.
+
+ Copyright (C) 2002 Richard Prescott
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+#ifndef CHANNEL__H
+#define CHANNEL__H
+
+/*! A data structure representing all the functions required on a channel */
+struct channel_ops
+{
+ void *(*init) (const char *);
+ int (*open) (void *);
+ void (*close) (void *);
+ int (*read) (void *, char *, int);
+ int (*write) (void *, const char *, int);
+ void (*free) (void *);
+ int (*isok) (void *);
+ char *(*status) (void *);
+};
+
+/*! A data structure representing a channel. Its operations and data */
+struct channel
+{
+ const struct channel_ops *ops;
+ void *data;
+};
+
+
+/* Function prototypes for external use */
+extern struct channel *channel_init (const char *descriptor);
+extern int channel_open (struct channel *channel);
+extern int channel_read (struct channel *channel,
+ char *buffer,
+ int size);
+extern int channel_write (struct channel *channel,
+ const char *buffer,
+ int size);
+extern void channel_close (struct channel *channel);
+
+#endif /* CHANNEL__H */
channel.h
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: file.c
===================================================================
--- file.c (nonexistent)
+++ file.c (revision 21)
@@ -0,0 +1,172 @@
+/* file.c -- Definition of functions and structures for
+ peripheral to communicate with host through files
+
+ Copyright (C) 2002 Richard Prescott
+ Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+/* Autoconf and/or portability configuration */
+#include "config.h"
+#include "port.h"
+
+/* System includes */
+#include
+#include
+#include
+#include
+
+/* Package includes */
+#include "channel.h"
+#include "fd.h"
+
+/*! Data structure representing a channel to/from a file */
+struct file_channel
+{
+ struct fd_channel fds;
+ char *namein;
+ char *nameout;
+};
+
+
+/* Forward declarations of static routines */
+static void *file_init (const char *args);
+static int file_open (void *data);
+static void file_close (void *data);
+static void file_free (void *data);
+
+/*! Data structure with all the operations for communicating with a file
+ channel */
+struct channel_ops file_channel_ops = {
+ .init = file_init,
+ .open = file_open,
+ .close = file_close,
+ .read = fd_read,
+ .write = fd_write,
+ .free = file_free,
+};
+
+
+static void *
+file_init (const char *args)
+{
+ struct file_channel *retval;
+ char *nameout;
+
+ if (!args)
+ {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ retval = (struct file_channel *) calloc (1, sizeof (struct file_channel));
+
+ if (!retval)
+ {
+ return NULL;
+ }
+
+ retval->fds.fdin = -1;
+ retval->fds.fdout = -1;
+
+ nameout = strchr (args, ',');
+
+ if (nameout)
+ {
+ retval->namein = strndup (args, nameout - args);
+ retval->nameout = strdup (nameout + 1);
+ }
+ else
+ {
+ retval->nameout = retval->namein = strdup (args);
+ }
+
+ return (void *) retval;
+}
+
+static int
+file_open (void *data)
+{
+ struct file_channel *files = (struct file_channel *) data;
+
+ if (!files)
+ {
+ errno = ENODEV;
+ return -1;
+ }
+
+ if (files->namein == files->nameout)
+ {
+ /* if we have the same name in and out
+ * it cannot (logically) be a regular files.
+ * so we wont create one
+ */
+ files->fds.fdin = files->fds.fdout = open (files->namein, O_RDWR);
+
+ return files->fds.fdin < 0 ? -1 : 0;
+ }
+
+
+ files->fds.fdin = open (files->namein, O_RDONLY | O_CREAT, 0664);
+
+ if (files->fds.fdin < 0)
+ return -1;
+
+ files->fds.fdout = open (files->nameout, O_WRONLY | O_CREAT, 0664);
+
+ if (files->fds.fdout < 0)
+ {
+ close (files->fds.fdout);
+ files->fds.fdout = -1;
+ return -1;
+ }
+
+ return 0;
+}
+
+static void
+file_close (void *data)
+{
+ struct file_channel *files = (struct file_channel *) data;
+
+ if (files->fds.fdin != files->fds.fdout)
+ close (files->fds.fdin);
+
+ close (files->fds.fdout);
+
+ files->fds.fdin = -1;
+ files->fds.fdout = -1;
+}
+
+static void
+file_free (void *data)
+{
+ struct file_channel *files = (struct file_channel *) data;
+
+ if (files->namein != files->nameout)
+ free (files->namein);
+
+ free (files->nameout);
+
+ free (files);
+}
file.c
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: tty.c
===================================================================
--- tty.c (nonexistent)
+++ tty.c (revision 21)
@@ -0,0 +1,222 @@
+/* tty.c -- Definition of functions for peripheral to
+ * communicate with host via a tty.
+
+ Copyright (C) 2002 Richard Prescott
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+/* Autoconf and/or portability configuration */
+#include "config.h"
+#include "port.h"
+
+/* System includes */
+#include
+#include
+#include
+#include
+#include
+
+/* Package includes */
+#include "channel.h"
+#include "generic.h"
+#include "fd.h"
+
+/* Default parameters if not specified in config file */
+#define DEFAULT_BAUD B19200
+#define DEFAULT_TTY_DEVICE "/dev/ttyS0"
+
+/*! Data structure representing a TTY channel */
+struct tty_channel
+{
+ struct fd_channel fds;
+};
+
+/*! Table of Baud rates */
+static const struct
+{
+ char *name;
+ int value;
+} baud_table[] =
+{
+ {
+ "50", B50},
+ {
+ "2400", B2400},
+ {
+ "4800", B4800},
+ {
+ "9600", B9600},
+ {
+ "19200", B19200},
+ {
+ "38400", B38400},
+ {
+ "115200", B115200},
+ {
+ "230400", B230400},
+ {
+ 0, 0}
+};
+
+/* Forward declaration of static functions */
+static void *tty_init (const char *input);
+static int tty_open (void *data);
+
+/*! Global data structure representing the operations on a TTY channel */
+struct channel_ops tty_channel_ops = {
+ .init = tty_init,
+ .open = tty_open,
+ .close = generic_close,
+ .read = fd_read,
+ .write = fd_write,
+ .free = generic_free,
+};
+
+
+/* Convert baud rate string to termio baud rate constant */
+static int
+parse_baud (char *baud_string)
+{
+ int i;
+ for (i = 0; baud_table[i].name; i++)
+ {
+ if (!strcmp (baud_table[i].name, baud_string))
+ return baud_table[i].value;
+ }
+
+ fprintf (stderr, "Error: unknown baud rate: %s\n", baud_string);
+ fprintf (stderr, " Known baud rates: ");
+
+ for (i = 0; baud_table[i].name; i++)
+ {
+ fprintf (stderr, "%s%s", baud_table[i].name,
+ baud_table[i + 1].name ? ", " : "\n");
+ }
+ return B0;
+}
+
+static void *
+tty_init (const char *input)
+{
+ int fd = 0, baud;
+ char *param_name, *param_value, *device;
+ struct termios options;
+ struct tty_channel *channel;
+
+ channel = (struct tty_channel *) malloc (sizeof (struct tty_channel));
+ if (!channel)
+ return NULL;
+
+ /* Make a copy of config string, because we're about to mutate it */
+ input = strdup (input);
+ if (!input)
+ goto error;
+
+ baud = DEFAULT_BAUD;
+ device = DEFAULT_TTY_DEVICE;
+
+ /* Parse command-line parameters
+ Command line looks like name1=value1,name2,name3=value3,... */
+ while ((param_name = strtok ((char *) input, ",")))
+ {
+
+ input = NULL;
+
+ /* Parse a parameter's name and value */
+ param_value = strchr (param_name, '=');
+ if (param_value != NULL)
+ {
+ *param_value = '\0';
+ param_value++; /* Advance past '=' character */
+ }
+
+ if (!strcmp (param_name, "baud") && param_value)
+ {
+ baud = parse_baud (param_value);
+ if (baud == B0)
+ {
+ goto error;
+ }
+ }
+ else if (!strcmp (param_name, "device"))
+ {
+ device = param_value;
+ }
+ else
+ {
+ fprintf (stderr, "error: unknown tty channel parameter \"%s\"\n",
+ param_name);
+ goto error;
+ }
+ }
+
+ fd = open (device, O_RDWR);
+ if (fd < 0)
+ goto error;
+
+ /* Get the current options for the port... */
+ if (tcgetattr (fd, &options) < 0)
+ goto error;
+
+ /* Set the serial baud rate */
+ cfsetispeed (&options, baud);
+ cfsetospeed (&options, baud);
+
+ /* Enable the receiver and set local mode... */
+
+ /* cfmakeraw(&options);
+ *
+ * cygwin lacks cfmakeraw(), just do it explicitly
+ */
+ options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
+ | INLCR | IGNCR | ICRNL | IXON);
+ options.c_oflag &= ~OPOST;
+ options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
+ options.c_cflag &= ~(CSIZE | PARENB);
+ options.c_cflag |= CS8;
+
+ options.c_cflag |= (CLOCAL | CREAD);
+
+
+ /* Set the new options for the port... */
+ if (tcsetattr (fd, TCSANOW, &options) < 0)
+ goto error;
+
+ channel->fds.fdin = channel->fds.fdout = fd;
+ free ((void *) input);
+ return channel;
+
+error:
+ if (fd > 0)
+ close (fd);
+ free (channel);
+ if (input)
+ free ((void *) input);
+ return NULL;
+}
+
+static int
+tty_open (void *data)
+{
+ return 0;
+}
tty.c
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: generic.h
===================================================================
--- generic.h (nonexistent)
+++ generic.h (revision 21)
@@ -0,0 +1,35 @@
+/* generic.h -- Declaration of generic functions for peripheral to
+ communicate with host
+
+ Copyright (C) 2002 Richard Prescott
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+#ifndef GENERIC__H
+#define GENERIC__H
+
+/* Function prototypes for external use */
+extern int generic_open (void *data);
+extern void generic_close (void *data);
+extern void generic_free (void *data);
+
+#endif /* GENERIC__H */
generic.h
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: file.h
===================================================================
--- file.h (nonexistent)
+++ file.h (revision 21)
@@ -0,0 +1,32 @@
+/* file.h -- Header for functions and structures for
+ peripheral to communicate with host through files
+
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+#ifndef FILE__H
+#define FILE__H
+
+/* Global data structures for external use */
+extern struct channel_ops file_channel_ops;
+
+#endif /* FILE__H */
file.h
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: tty.h
===================================================================
--- tty.h (nonexistent)
+++ tty.h (revision 21)
@@ -0,0 +1,33 @@
+/* tty.h -- Header for functions for peripheral to
+ * communicate with host via a tty.
+
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+#ifndef TTY__H
+#define TTY__H
+
+/* Global data structures for external use */
+extern struct channel_ops tty_channel_ops;
+
+#endif /* TTY__H */
tty.h
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: tcp.c
===================================================================
--- tcp.c (nonexistent)
+++ tcp.c (revision 21)
@@ -0,0 +1,211 @@
+/* tcp.c -- Definition of functions for peripheral to
+ * communicate with host via a tcp socket.
+
+ Copyright (C) 2002 Richard Prescott
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+/* Autoconf and/or portability configuration */
+#include "config.h"
+#include "port.h"
+
+/* System includes */
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+/* Package includes */
+#include "channel.h"
+#include "generic.h"
+#include "fd.h"
+
+/*! Structure to represent a TCP/IP channel */
+struct tcp_channel
+{
+ struct fd_channel fds;
+ int socket_fd; /* Socket to listen to */
+ int port_number; /* TCP port number */
+ int connected; /* If 0, no remote endpoint yet */
+ int nonblocking; /* If 0, read/write will block until
+ remote client connects */
+};
+
+/* Forward declarations of static functions */
+static void *tcp_init (const char *input);
+static int tcp_open (void *data);
+static int tcp_read (void *data,
+ char *buffer,
+ int size);
+static int tcp_write (void *data,
+ const char *buffer,
+ int size);
+
+/*! Data structure holding all the operations for a TCP/IP channel */
+struct channel_ops tcp_channel_ops = {
+ .init = tcp_init,
+ .open = tcp_open,
+ .close = generic_close,
+ .read = tcp_read,
+ .write = tcp_write,
+ .free = generic_free,
+};
+
+static void *
+tcp_init (const char *input)
+{
+ int port_number, fd, flags;
+ struct sockaddr_in local_ip;
+ struct tcp_channel *channel =
+ (struct tcp_channel *) malloc (sizeof (struct tcp_channel));
+ if (!channel)
+ return NULL;
+
+ fd = 0;
+ channel->nonblocking = 1;
+ channel->fds.fdin = -1;
+ channel->fds.fdout = -1;
+ channel->socket_fd = -1;
+ channel->port_number = -1;
+
+ port_number = atoi (input);
+ if (port_number == 0)
+ goto error;
+
+ fd = socket (AF_INET, SOCK_STREAM, 0);
+ if (fd < 0)
+ goto error;
+
+ flags = 1;
+ if (setsockopt
+ (fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &flags, sizeof (int)) < 0)
+ {
+ perror ("Can not set SO_REUSEADDR option on channel socket");
+ goto error;
+ }
+
+ memset (&local_ip, 0, sizeof (local_ip));
+ local_ip.sin_family = AF_INET;
+ local_ip.sin_addr.s_addr = htonl (INADDR_ANY);
+ local_ip.sin_port = htons (port_number);
+ if (bind (fd, (struct sockaddr *) &local_ip, sizeof (local_ip)) < 0)
+ {
+ perror ("Can't bind local address");
+ goto error;
+ }
+
+ if (channel->nonblocking)
+ {
+ if (fcntl (fd, F_SETFL, O_NONBLOCK) < 0)
+ {
+ perror ("Can not make channel socket non-blocking");
+ goto error;
+ }
+ }
+
+ if (listen (fd, 1) < 0)
+ goto error;
+
+ channel->socket_fd = fd;
+ channel->port_number = port_number;
+ channel->connected = 0;
+ return (void *) channel;
+
+error:
+ if (fd)
+ close (fd);
+ free (channel);
+ return NULL;
+}
+
+static int
+tcp_open (void *data)
+{
+ /* Socket is opened lazily, upon first read or write, so do nothing here */
+ return 0;
+}
+
+
+static int
+wait_for_tcp_connect (struct tcp_channel *channel)
+{
+ int fd;
+ socklen_t sizeof_remote_ip;
+ struct sockaddr_in remote_ip;
+
+ sizeof_remote_ip = sizeof (remote_ip);
+ fd =
+ accept (channel->socket_fd, (struct sockaddr *) &remote_ip,
+ &sizeof_remote_ip);
+ if (fd < 0)
+ {
+ if (channel->nonblocking)
+ {
+ /* Not an error if there is not yet a remote connection - try again later */
+ if (errno == EAGAIN)
+ return 0;
+ }
+ perror ("Couldn't accept connection");
+ return -1;
+ }
+
+ channel->fds.fdin = channel->fds.fdout = fd;
+ close (channel->socket_fd);
+ channel->socket_fd = -1;
+ channel->connected = 1;
+ return 1;
+}
+
+static int
+tcp_read (void *data, char *buffer, int size)
+{
+ struct tcp_channel *channel = data;
+
+ /* Lazily connect to tcp partner on read/write */
+ if (!channel->connected)
+ {
+ int retval = wait_for_tcp_connect (data);
+ if (retval <= 0)
+ return retval;
+ }
+ return fd_read (data, buffer, size);
+}
+
+static int
+tcp_write (void *data, const char *buffer, int size)
+{
+ struct tcp_channel *channel = data;
+
+ /* Lazily connect to tcp partner on read/write */
+ if (!channel->connected)
+ {
+ int retval = wait_for_tcp_connect (data);
+ if (retval < 0)
+ return retval;
+ }
+ return fd_write (data, buffer, size);
+}
tcp.c
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: Makefile.am
===================================================================
--- Makefile.am (nonexistent)
+++ Makefile.am (revision 21)
@@ -0,0 +1,38 @@
+# Makefile -- Makefile for peripherals channels to host
+#
+# Copyright (C) 2002 Richard Prescott
+# Copyright (C) 2008 Embecosm Limited
+#
+# Contributor Jeremy Bennett
+#
+# This file is part of OpenRISC 1000 Architectural Simulator.
+#
+# 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 3 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, see .
+
+
+noinst_LTLIBRARIES = libchannels.la
+libchannels_la_SOURCES = channel.c \
+ fd.c \
+ file.c \
+ generic.c \
+ tcp.c \
+ tty.c \
+ xterm.c \
+ channel.h \
+ fd.h \
+ file.h \
+ generic.h \
+ tcp.h \
+ tty.h \
+ xterm.h
Makefile.am
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: fd.c
===================================================================
--- fd.c (nonexistent)
+++ fd.c (revision 21)
@@ -0,0 +1,187 @@
+/* fd.c -- Definition of functions and structures for
+ peripheral to communicate with host through file descriptors
+
+ Copyright (C) 2002 Richard Prescott
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+/* Autoconf and/or portability configuration */
+#include "config.h"
+#include "port.h"
+
+/* System includes */
+#include
+#include
+#include
+#include
+#include
+#include
+
+/* Package includes */
+#include "fd.h"
+#include "channel.h"
+#include "generic.h"
+
+
+/* Forward declarations of static functions */
+static void *fd_init (const char *args);
+static int fd_isok (void *data);
+static char *fd_status (void *data);
+
+/*! Global data structure representing the operations for communicating
+ through a file descriptor channel */
+struct channel_ops fd_channel_ops = {
+ .init = fd_init,
+ .open = generic_open,
+ .close = generic_close,
+ .read = fd_read,
+ .write = fd_write,
+ .free = generic_free,
+ .isok = fd_isok,
+ .status = fd_status,
+};
+
+
+static void *
+fd_init (const char *args)
+{
+ struct fd_channel *retval;
+
+ retval = (struct fd_channel *) calloc (1, sizeof (struct fd_channel));
+
+ if (!retval)
+ {
+ return NULL;
+ }
+
+
+ retval->fdin = atoi (args); /* so 0 if garbage */
+ /* TODO: strtoul */
+
+ args = strchr (args, ',');
+
+ if (args)
+ {
+ retval->fdout = atoi (args + 1);
+ }
+ else
+ {
+ retval->fdout = retval->fdin;
+ }
+
+ return (void *) retval;
+}
+
+int
+fd_read (void *data, char *buffer, int size)
+{
+ struct fd_channel *fds = (struct fd_channel *) data;
+ struct timeval timeout = { 0, 0 };
+ fd_set rfds;
+ int retval;
+
+ if (!fds)
+ {
+ errno = ENODEV;
+ return -1;
+ }
+
+ /* Loop, to allow for select being interrupted */
+ do
+ {
+ FD_ZERO (&rfds);
+ FD_SET (fds->fdin, &rfds);
+
+ retval = select (fds->fdin + 1, &rfds, NULL, NULL, &timeout);
+ }
+ while ((retval < 0) && (EINTR == errno));
+
+ if (retval <= 0)
+ return retval;
+
+ /* Loop, to allow for select being interrupted */
+ do
+ {
+ retval = read (fds->fdin, buffer, size);
+ }
+ while ((retval < 0) && (EINTR == errno));
+
+ return retval;
+}
+
+int
+fd_write (void *data, const char *buffer, int size)
+{
+ struct fd_channel *fds = (struct fd_channel *) data;
+ if (fds)
+ {
+ return write (fds->fdout, buffer, size);
+ }
+ errno = ENODEV;
+ return -1;
+}
+
+static int
+fd_isok (void *data)
+{
+ struct fd_channel *fds = (struct fd_channel *) data;
+ if (fds)
+ {
+ return fds->fdout != -1 && fds->fdin != -1;
+ }
+ return 0;
+}
+
+static int
+fd_status_fd (int fd, char *str, int size)
+{
+ if (fd == -1)
+ return snprintf (str, size, "closed");
+
+ return snprintf (str, size, "opened(fd=%d)", fd);
+}
+
+static char *
+fd_status (void *data)
+{
+ static char retval[256];
+ int index = 0;
+
+ struct fd_channel *fds = (struct fd_channel *) data;
+ if (fds)
+ {
+ index += snprintf (retval + index, sizeof (retval) - index, "in ");
+ index +=
+ fd_status_fd (fds->fdin, retval + index, sizeof (retval) - index);
+
+ index += snprintf (retval + index, sizeof (retval) - index, "out ");
+ index +=
+ fd_status_fd (fds->fdout, retval + index, sizeof (retval) - index);
+ }
+ else
+ {
+ snprintf (retval, sizeof (retval), "(null)");
+ }
+ return retval;
+}
+
fd.c
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: tcp.h
===================================================================
--- tcp.h (nonexistent)
+++ tcp.h (revision 21)
@@ -0,0 +1,33 @@
+/* tcp.h -- Header for functions for peripheral to
+ communicate with host via a TCP/IP socket.
+
+ Copyright (C) 2008 Embecosm Limited
+
+ Contributor Jeremy Bennett
+
+ This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
+
+ 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 3 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, see . */
+
+/* This program is commented throughout in a fashion suitable for processing
+ with Doxygen. */
+
+
+#ifndef TCP__H
+#define TCP__H
+
+/* Global data structures for external use */
+extern struct channel_ops tcp_channel_ops;
+
+#endif /* TCP__H */
tcp.h
Property changes :
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: .cvsignore
===================================================================
--- .cvsignore (nonexistent)
+++ .cvsignore (revision 21)
@@ -0,0 +1,2 @@
+Makefile
+.deps