summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/host
diff options
context:
space:
mode:
authorNicolas Schodet2007-10-07 22:16:03 +0200
committerNicolas Schodet2007-10-07 22:16:03 +0200
commit583f00e0b8efe2832f63efb478a51d3ad35e92ed (patch)
treee2cd42af768a4d248883d7fcebcb2124aad6fbe5 /digital/avr/modules/host
parente38de75f64149d148e66572e330b7ce808782b1f (diff)
Included SI2E avr modules.
Well, this need more works...
Diffstat (limited to 'digital/avr/modules/host')
-rw-r--r--digital/avr/modules/host/Makefile.module1
-rw-r--r--digital/avr/modules/host/README25
-rw-r--r--digital/avr/modules/host/host.h65
-rw-r--r--digital/avr/modules/host/host.host.c102
-rw-r--r--digital/avr/modules/host/test/Makefile12
-rw-r--r--digital/avr/modules/host/test/avrconfig.h28
-rw-r--r--digital/avr/modules/host/test/test_host.c61
7 files changed, 294 insertions, 0 deletions
diff --git a/digital/avr/modules/host/Makefile.module b/digital/avr/modules/host/Makefile.module
new file mode 100644
index 00000000..cf2cf1ee
--- /dev/null
+++ b/digital/avr/modules/host/Makefile.module
@@ -0,0 +1 @@
+host_SOURCES = host.host.c
diff --git a/digital/avr/modules/host/README b/digital/avr/modules/host/README
new file mode 100644
index 00000000..7b9e5075
--- /dev/null
+++ b/digital/avr/modules/host/README
@@ -0,0 +1,25 @@
+avr.host - Host fonctions modules.
+
+Host and simulation functions. See modules README for more details about AVR
+modules.
+
+
+Copyright (C) 2005 Nicolas Schodet
+
+Robot APB Team/Efrei 2006.
+ Web: http://assos.efrei.fr/robot/
+ Email: robot AT efrei DOT fr
+
+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.
diff --git a/digital/avr/modules/host/host.h b/digital/avr/modules/host/host.h
new file mode 100644
index 00000000..73cd4a58
--- /dev/null
+++ b/digital/avr/modules/host/host.h
@@ -0,0 +1,65 @@
+#ifndef host_h
+#define host_h
+/* host.h */
+/* avr.host - Host fonctions modules. {{{
+ *
+ * Copyright (C) 2005-2006 Nicolas Schodet
+ *
+ * Robot APB Team/Efrei 2006.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * 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.
+ *
+ * }}} */
+
+/* Discourage use of this header in non HOST compilation. */
+#ifndef HOST
+# error Use this header only for HOST only files !
+#endif
+
+/** Initialise host module. */
+void
+host_init (int argc, char **argv);
+
+/** Retrieve saved program arguments. Program name and used parameters are
+ * stripped. */
+void
+host_get_program_arguments (int *argc, char ***argv);
+
+/** Host variables are usefull on reset. They are passed in the environment.
+ * This is not optimised for performance. */
+
+/** Register a host integer. */
+void
+host_register_integer (const char *name, int val);
+
+/** Register a host string. */
+void
+host_register_string (const char *name, const char *val);
+
+/** Fetch a host integer, return -1 if non existant. */
+int
+host_fetch_integer (const char *name);
+
+/** Fetch a host string, return 0 if non existant. */
+const char *
+host_fetch_string (const char *name);
+
+/** Reset the program. */
+void
+host_reset (void) __attribute__ ((noreturn));
+
+#endif /* host_h */
diff --git a/digital/avr/modules/host/host.host.c b/digital/avr/modules/host/host.host.c
new file mode 100644
index 00000000..7967e9cd
--- /dev/null
+++ b/digital/avr/modules/host/host.host.c
@@ -0,0 +1,102 @@
+/* host.host.c */
+/* avr.host - Host fonctions modules. {{{
+ *
+ * Copyright (C) 2005-2006 Nicolas Schodet
+ *
+ * Robot APB Team/Efrei 2006.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * 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.
+ *
+ * }}} */
+#define _GNU_SOURCE
+#include "common.h"
+#include "host.h"
+
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+/* Saved arguments. Uninitialised global variables are set to 0 by the
+ * compiler. */
+static int host_saved_argc;
+static char **host_saved_argv;
+
+/** Initialise host module. */
+void
+host_init (int argc, char **argv)
+{
+ host_saved_argc = argc;
+ host_saved_argv = argv;
+}
+
+/** Retrieve saved program arguments. Program name and used parameters are
+ * stripped. */
+void
+host_get_program_arguments (int *argc, char ***argv)
+{
+ assert (host_saved_argc);
+ *argc = host_saved_argc - 1;
+ *argv = host_saved_argv + 1;
+}
+
+/** Register a host integer. */
+void
+host_register_integer (const char *name, int val)
+{
+ int r;
+ char sval[256];
+ /* Convert to string, and register the string. */
+ r = snprintf (sval, sizeof (sval), "%d", val);
+ assert (r < (int) sizeof (sval));
+ host_register_string (name, sval);
+}
+
+/** Register a host string. */
+void
+host_register_string (const char *name, const char *val)
+{
+ int r;
+ r = setenv (name, val, 1);
+ assert (r == 0);
+}
+
+/** Fetch a host integer, return -1 if non existant. */
+int
+host_fetch_integer (const char *name)
+{
+ const char *sval = host_fetch_string (name);
+ return sval ? atoi (sval) : -1;
+}
+
+/** Fetch a host string, return 0 if non existant. */
+const char *
+host_fetch_string (const char *name)
+{
+ return getenv (name);
+}
+
+/** Reset the program. */
+void
+host_reset (void)
+{
+ assert (host_saved_argv);
+ execv (program_invocation_name, host_saved_argv);
+ assert_perror (errno);
+ abort ();
+}
+
diff --git a/digital/avr/modules/host/test/Makefile b/digital/avr/modules/host/test/Makefile
new file mode 100644
index 00000000..5f8343c4
--- /dev/null
+++ b/digital/avr/modules/host/test/Makefile
@@ -0,0 +1,12 @@
+BASE = ../../..
+HOST_PROGS = test_host
+test_host_SOURCES = test_host.c
+MODULES = host
+CONFIGFILE = avrconfig.h
+# atmega8, atmega8535, atmega128...
+AVR_MCU = none
+# -O2 : speed
+# -Os : size
+OPTIMIZE = -O2
+
+include $(BASE)/make/Makefile.gen
diff --git a/digital/avr/modules/host/test/avrconfig.h b/digital/avr/modules/host/test/avrconfig.h
new file mode 100644
index 00000000..f3c23a9c
--- /dev/null
+++ b/digital/avr/modules/host/test/avrconfig.h
@@ -0,0 +1,28 @@
+#ifndef avrconfig_h
+#define avrconfig_h
+/* avrconfig.h - test_host config file. */
+/* host - Host fonctions modules. {{{
+ *
+ * Copyright (C) 2005 Nicolas Schodet
+ *
+ * Robot APB Team/Efrei 2006.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * 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.
+ *
+ * }}} */
+
+#endif /* avrconfig_h */
diff --git a/digital/avr/modules/host/test/test_host.c b/digital/avr/modules/host/test/test_host.c
new file mode 100644
index 00000000..8c98a94a
--- /dev/null
+++ b/digital/avr/modules/host/test/test_host.c
@@ -0,0 +1,61 @@
+/* test_host.c */
+/* avr.host - Host fonctions modules. {{{
+ *
+ * Copyright (C) 2005-2006 Nicolas Schodet
+ *
+ * Robot APB Team/Efrei 2006.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * 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.
+ *
+ * }}} */
+#include "common.h"
+#include "modules/host/host.h"
+
+#include <string.h>
+#include <stdio.h>
+
+int
+main (int argc, char **argv)
+{
+ int i;
+ const char *s;
+ int ac;
+ char **av;
+ printf ("reset\n");
+ host_init (argc, argv);
+ i = host_fetch_integer ("avr_integer");
+ s = host_fetch_string ("avr_string");
+ if (i != -1)
+ {
+ printf ("get\n");
+ host_get_program_arguments (&ac, &av);
+ assert_print (ac == 1 && strcmp (av[0], "ni") == 0,
+ "argument passing not working");
+ assert (i == 42);
+ assert (strcmp (s, "Ni!") == 0);
+ }
+ else
+ {
+ printf ("set\n");
+ assert_print (argc == 2 && strcmp (argv[1], "ni") == 0,
+ "please provide \"ni\" as the first argument");
+ host_register_integer ("avr_integer", 42);
+ host_register_string ("avr_string", "Ni!");
+ host_reset ();
+ }
+ return 0;
+}