summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/cpu/ChangeLog8
-rw-r--r--tools/cpu/configure.ac1
-rw-r--r--tools/cpu/nios2/.cvsignore14
-rw-r--r--tools/cpu/nios2/Makefile.am23
-rw-r--r--tools/cpu/nios2/README19
-rw-r--r--tools/cpu/nios2/bridges.c120
-rw-r--r--tools/cpu/nios2/bridges.h31
-rw-r--r--tools/cpu/nios2/clocks.c78
-rw-r--r--tools/cpu/nios2/clocks.h28
-rw-r--r--tools/cpu/nios2/configure.ac23
-rw-r--r--tools/cpu/nios2/devices.c132
-rw-r--r--tools/cpu/nios2/devices.h32
-rw-r--r--tools/cpu/nios2/nios2gen.c231
-rw-r--r--tools/cpu/nios2/output.c195
-rw-r--r--tools/cpu/nios2/output.h26
-rw-r--r--tools/cpu/nios2/ptf.c807
-rw-r--r--tools/cpu/nios2/ptf.h69
17 files changed, 1837 insertions, 0 deletions
diff --git a/tools/cpu/ChangeLog b/tools/cpu/ChangeLog
index 9f57f5659a..2e041d6612 100644
--- a/tools/cpu/ChangeLog
+++ b/tools/cpu/ChangeLog
@@ -1,3 +1,11 @@
+2006-08-09 Kolja Waschk <waschk@telos.de>
+
+ * configure.ac: New port to Altera NIOS II.
+ * nios2/.cvsignore, nios2/Makefile.am, nios2/README, nios2/bridges.c,
+ nios2/bridges.h, nios2/clocks.c, nios2/clocks.h, nios2/configure.ac,
+ nios2/devices.c, nios2/devices.h, nios2/nios2gen.c, nios2/output.c,
+ nios2/output.h, nios2/ptf.c, nios2/ptf.h: New files.
+
2004-09-24 Ralf Corsepius <ralf_corsepius@rtems.org>
* configure.ac: Require automake > 1.9.
diff --git a/tools/cpu/configure.ac b/tools/cpu/configure.ac
index 683ab6d266..cdbe9a0813 100644
--- a/tools/cpu/configure.ac
+++ b/tools/cpu/configure.ac
@@ -20,6 +20,7 @@ AC_SUBST(program_prefix)
AC_CONFIG_SUBDIRS(generic)
case "$RTEMS_CPU" in
sh ) AC_CONFIG_SUBDIRS(sh);;
+nios2 ) AC_CONFIG_SUBDIRS(nios2);;
esac
# Explicitly list all Makefiles here
diff --git a/tools/cpu/nios2/.cvsignore b/tools/cpu/nios2/.cvsignore
new file mode 100644
index 0000000000..bfdfd995be
--- /dev/null
+++ b/tools/cpu/nios2/.cvsignore
@@ -0,0 +1,14 @@
+aclocal.m4
+autom4te*.cache
+config.cache
+config.guess
+config.log
+config.status
+config.sub
+configure
+depcomp
+install-sh
+Makefile
+Makefile.in
+missing
+mkinstalldirs
diff --git a/tools/cpu/nios2/Makefile.am b/tools/cpu/nios2/Makefile.am
new file mode 100644
index 0000000000..b71477af4b
--- /dev/null
+++ b/tools/cpu/nios2/Makefile.am
@@ -0,0 +1,23 @@
+##
+## $Id$
+##
+
+ACLOCAL_AMFLAGS = -I ../../../aclocal
+
+noinst_PROGRAMS = nios2gen
+
+nios2gen_SOURCES = nios2gen.c \
+ bridges.c bridges.h \
+ devices.c devices.h \
+ clocks.c clocks.h \
+ output.c output.h \
+ ptf.h ptf.c
+
+if HELP2MAN
+man_MANS = nios2gen.1
+
+nios2gen.1: nios2gen$(EXEEXT)
+ $(HELP2MAN) -N ./nios2gen >$@
+endif
+
+include $(top_srcdir)/../../../automake/host.am
diff --git a/tools/cpu/nios2/README b/tools/cpu/nios2/README
new file mode 100644
index 0000000000..06c59fe3fc
--- /dev/null
+++ b/tools/cpu/nios2/README
@@ -0,0 +1,19 @@
+$Id$
+
+As an output from SOPC Builder you get a file with extension ".ptf" that fully
+describes the SOPC, including all CPUs, memory and integrated peripherals.
+
+It is structured as
+
+
+
+
+nios2gen:
+ Tool to generate BSP data for boards utilizing NIOS2 soft core processor.
+
+Specify the PTF and name of the CPU (if there are more than one).
+
+
+
+
+
diff --git a/tools/cpu/nios2/bridges.c b/tools/cpu/nios2/bridges.c
new file mode 100644
index 0000000000..f792d5e1aa
--- /dev/null
+++ b/tools/cpu/nios2/bridges.c
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+/********************************************************/
+/* Find bus bridges */
+
+/* This part of the program builds a list with pairs of bus
+ master port names (each is "device name/master port name").
+ It is then possible to find if a given master is actually
+ available under a different master port name through bridges.
+ */
+
+/* Typical example with external SRAM that is slave of
+ tristate_bridge_0/tristate_master, and
+ tristate_bridge_0 itself is slave of cpu0/data_master, the
+ bridge information would be stored as this bus_bridge_pair:
+ mastered_by = "cpu0/data_master" and
+ bridges_to = "tristate_bridge_0/tristate_master".
+ That allows to deduce that SRAM is actually mastered by
+ cpu0/data_master. If there were any address or bus width
+ translations, it should be noted in the bridges list... For
+ now we simply assume that bridges never translate anything.
+ */
+
+#include "ptf.h"
+#include "bridges.h"
+
+int is_bridged(
+ char *cpu_master,
+ char *dev_master,
+ bus_bridge_pair *bridges)
+{
+ char *curr_master;
+ bus_bridge_pair *bbp;
+
+ curr_master = dev_master;
+ while(curr_master != NULL)
+ {
+ /* Does cpu_master master curr_master? */
+ if(strcmp(cpu_master, curr_master) == 0) return 1; /* yes, cpu_masters cm */
+
+ /* No, cm is attached to a bridge? */
+ bbp = bridges;
+ while(bbp != NULL)
+ {
+ if(strcmp(bbp->bridges_to, curr_master) == 0)
+ {
+ curr_master = bbp->mastered_by;
+ break;
+ };
+ bbp = bbp->next;
+ };
+ if(bbp == NULL) curr_master = NULL;
+ };
+
+ return 0;
+}
+
+void add_bridge_master(struct ptf_item *pi, void *arg)
+{
+ struct { char *bt; bus_bridge_pair **bridges; } *binfo = arg;
+ bus_bridge_pair *new_pair;
+
+ if(binfo->bridges == 0) return;
+
+ new_pair = (bus_bridge_pair *)malloc(sizeof(bus_bridge_pair));
+ if(new_pair == NULL) return;
+
+ new_pair->bridges_to = binfo->bt;
+ new_pair->mastered_by = pi->item[pi->level]->value;
+ new_pair->next = *(binfo->bridges);
+ *(binfo->bridges) = new_pair;
+}
+
+void add_bridge_dest(struct ptf_item *pi, void *arg)
+{
+ struct ptf maby_section = { section, "MASTERED_BY", 0, 0, 0 };
+ struct ptf_item maby = { 1, &maby_section };
+
+ char *bridge_name = pi->item[1]->value;
+ char *bridge_dest = pi->item[pi->level]->value;
+ struct { char *bt; bus_bridge_pair **bridges; } binfo;
+
+ binfo.bridges = arg;
+ binfo.bt = (char*)malloc(strlen(bridge_name)+strlen(bridge_dest) + 2);
+ strcpy(binfo.bt, bridge_name);
+ strcat(binfo.bt, "/");
+ strcat(binfo.bt, bridge_dest);
+
+ ptf_match(pi->item[pi->level-1]->sub, &maby, add_bridge_master, &binfo);
+
+ /* binfo.bt is NOT freed here */
+}
+
+bus_bridge_pair *find_bridges(struct ptf *p)
+{
+ bus_bridge_pair *bridges = 0;
+
+ struct ptf system = { section, "SYSTEM", 0, 0, 0 };
+ struct ptf module = { section, "MODULE", 0, 0, 0 };
+ struct ptf slave = { section, "SLAVE", 0, 0, 0 };
+ struct ptf syb = { section, "SYSTEM_BUILDER_INFO", 0, 0, 0 };
+ struct ptf to = { item, "Bridges_To", 0, 0, 0 };
+ struct ptf_item brdg = { 5, &system, &module, &slave, &syb, &to };
+
+ ptf_match(p, &brdg, add_bridge_dest, &bridges);
+
+ return bridges;
+}
+
+
+
+
diff --git a/tools/cpu/nios2/bridges.h b/tools/cpu/nios2/bridges.h
new file mode 100644
index 0000000000..1d348dc2f2
--- /dev/null
+++ b/tools/cpu/nios2/bridges.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#ifndef __NIOS2GEN_BRIDGES_H
+#define __NIOS2GEN_BRIDGES_H 1
+
+typedef struct bus_bridge_pair
+{
+ char *mastered_by;
+ char *bridges_to;
+ struct bus_bridge_pair *next;
+}
+bus_bridge_pair;
+
+bus_bridge_pair *find_bridges(struct ptf *p);
+
+int is_bridged(
+ char *cpu_master,
+ char *dev_master,
+ bus_bridge_pair *bridges);
+
+#endif
+
+
diff --git a/tools/cpu/nios2/clocks.c b/tools/cpu/nios2/clocks.c
new file mode 100644
index 0000000000..ac0f85cb4d
--- /dev/null
+++ b/tools/cpu/nios2/clocks.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#include "ptf.h"
+#include "clocks.h"
+
+#include <stdlib.h>
+
+void add_clock_spec(struct ptf_item *pi, void *arg)
+{
+ clock_desc **clocks = arg;
+ clock_desc *new_clock;
+ unsigned long freq;
+
+ new_clock = (clock_desc*)malloc(sizeof(clock_desc));
+ if(new_clock == NULL) return;
+
+ new_clock->freq = strtoul(pi->item[pi->level]->value, 0, 0);
+ printf("freq = %lu (%s?)\n", new_clock->freq, pi->item[pi->level]->value);
+
+ new_clock->cfgname = NULL;
+ new_clock->name = pi->item[pi->level-1]->value;
+ new_clock->next = *clocks;
+
+
+ *clocks = new_clock;
+}
+
+void set_clock_cfgname(struct ptf_item *pi, void *arg)
+{
+ clock_desc *clock = arg;
+ clock->cfgname = pi->item[pi->level]->name;
+}
+
+clock_desc *find_clocks( struct ptf *sopc, struct ptf *cfg )
+{
+ clock_desc *clocks, *reverse;
+
+ struct ptf system = { section, "SYSTEM", 0, 0, 0 };
+ struct ptf wizargs = { section, "WIZARD_SCRIPT_ARGUMENTS", 0, 0, 0 };
+ struct ptf all = { section, "CLOCKS", 0, 0, 0 };
+ struct ptf clock = { section, "CLOCK", 0, 0, 0 };
+ struct ptf freq = { item, "frequency", 0, 0, 0 };
+ struct ptf_item clk_spec = { 5, &system, &wizargs, &all, &clock, &freq };
+
+ struct ptf named = { item, 0, 0, 0, 0 };
+ struct ptf_item clk_cfg = { 2, &all, &named };
+
+ clocks = NULL;
+ ptf_match(sopc, &clk_spec, add_clock_spec, &clocks);
+
+ /* Reverse the linked list and look for configured names */
+
+ reverse = NULL;
+ while(clocks)
+ {
+ clock_desc *tmp = clocks;
+ clocks = clocks->next;
+ tmp->next = reverse;
+ reverse = tmp;
+
+ named.value = tmp->name;
+ ptf_match(cfg, &clk_cfg, set_clock_cfgname, tmp);
+ if(tmp->cfgname == NULL) tmp->cfgname = ptf_defused_name(tmp->name);
+ };
+
+ return reverse;
+}
+
+
+
diff --git a/tools/cpu/nios2/clocks.h b/tools/cpu/nios2/clocks.h
new file mode 100644
index 0000000000..ef8395d39c
--- /dev/null
+++ b/tools/cpu/nios2/clocks.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#ifndef __NIOS2GEN_CLOCKS_H
+#define __NIOS2GEN_CLOCKS_H 1
+
+#include "ptf.h"
+
+typedef struct clockdsc
+{
+ char *name;
+ char *cfgname;
+ unsigned long freq;
+ struct clockdsc *next;
+}
+clock_desc;
+
+clock_desc *find_clocks( struct ptf *ptf, struct ptf *cfg );
+
+#endif
+
diff --git a/tools/cpu/nios2/configure.ac b/tools/cpu/nios2/configure.ac
new file mode 100644
index 0000000000..3388357c3c
--- /dev/null
+++ b/tools/cpu/nios2/configure.ac
@@ -0,0 +1,23 @@
+## Process this file with autoconf to produce a configure script.
+##
+## $Id$
+
+AC_PREREQ(2.59)
+AC_INIT([rtems-tools-cpu-nios2],[_RTEMS_VERSION],[rtems-bugs@rtems.com])
+AC_CONFIG_SRCDIR([nios2gen.c])
+RTEMS_TOP(../../..)
+
+RTEMS_CANONICAL_TARGET_CPU
+
+AM_INIT_AUTOMAKE([foreign 1.9])
+AM_MAINTAINER_MODE
+
+AC_PROG_CC
+AC_CHECK_PROGS(HELP2MAN,help2man)
+AM_CONDITIONAL(HELP2MAN,test -n "$HELP2MAN" )
+
+RTEMS_TOOLPATHS
+
+# Explicitly list all Makefiles here
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
diff --git a/tools/cpu/nios2/devices.c b/tools/cpu/nios2/devices.c
new file mode 100644
index 0000000000..112191f487
--- /dev/null
+++ b/tools/cpu/nios2/devices.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ptf.h"
+#include "bridges.h"
+#include "devices.h"
+
+void add_device(device_desc **dl, struct ptf *dev)
+{
+ device_desc *eds;
+
+ for(eds = *dl; eds; eds = eds->next)
+ {
+ if(eds->ptf == dev)
+ {
+ eds->slaves++;
+ return;
+ };
+ };
+
+ eds = (device_desc *)malloc(sizeof(device_desc));
+ eds->slaves = 1;
+ eds->ptf = dev;
+ eds->next = *dl;
+ *dl = eds;
+}
+
+void check_and_add_device(struct ptf_item *pi, void *arg)
+{
+ struct ptf *module = pi->item[pi->level-3];
+ struct ptf *sysinfo = pi->item[pi->level-2];
+ char *master_name = pi->item[pi->level]->value;
+
+ struct { char *dm; char *im; device_desc **dl; bus_bridge_pair *bridges; } *dinfo = arg;
+
+ if(is_bridged(dinfo->dm, master_name, dinfo->bridges) ||
+ is_bridged(dinfo->im, master_name, dinfo->bridges))
+ {
+ struct ptf *ni = ptf_alloc_item(item, "N2G_Selected", "1");
+ if(ni != NULL)
+ {
+ ni->next = sysinfo->sub;
+ sysinfo->sub = ni;
+ };
+ add_device(dinfo->dl, module);
+ };
+}
+
+void set_dev_cfgname(struct ptf_item *pi, void *arg)
+{
+ device_desc *dev = arg;
+ dev->cfgname = pi->item[pi->level]->name;
+}
+
+
+device_desc *find_devices(
+ struct ptf *ptf,
+ struct ptf *cfg,
+ struct ptf *cpu,
+ bus_bridge_pair *bridges)
+{
+ struct ptf system = { section, "SYSTEM", 0, 0, 0 };
+ struct ptf module = { section, "MODULE", 0, 0, 0 };
+ struct ptf slave = { section, "SLAVE", 0, 0, 0 };
+ struct ptf syb = { section, "SYSTEM_BUILDER_INFO", 0, 0, 0 };
+ struct ptf maby = { section, "MASTERED_BY", 0, 0, 0 };
+ struct ptf_item brdg = { 5, &system, &module, &slave, &syb, &maby };
+
+ struct ptf modules = { section, "MODULES", 0, 0, 0 };
+ struct ptf named = { item, 0, 0, 0, 0};
+ struct ptf_item devcf = { 2, &modules, &named };
+
+ struct { char *dm; char *im; device_desc **dl; bus_bridge_pair *bridges; } dinfo;
+
+ device_desc *found, *reverse;
+
+ found = NULL;
+
+ add_device(&found, cpu); /* The CPU is "self-connected", add it */
+
+ dinfo.dl = &found;
+ dinfo.bridges = bridges;
+ dinfo.dm = (char *)malloc(strlen(cpu->value)+13);
+ dinfo.im = (char *)malloc(strlen(cpu->value)+20);
+
+ strcpy(dinfo.im, cpu->value);
+ strcat(dinfo.im, "/");
+ strcpy(dinfo.dm, dinfo.im);
+ strcat(dinfo.dm, "data_master");
+ strcat(dinfo.im, "instruction_master");
+
+ /* "Available" is any MODULE with a SLAVE section that is MASTERED_BY
+ either instr_master or data_master of selected CPU, either directly
+ or through a bridge. See code above for more info about bridges.
+ */
+
+ ptf_match(ptf, &brdg, check_and_add_device, &dinfo);
+
+ free(dinfo.dm);
+ free(dinfo.im);
+
+ /* Reverse the linked list */
+
+ reverse = NULL;
+ while(found)
+ {
+ device_desc *tmp = found;
+ found = found->next;
+
+ tmp->next = reverse;
+ reverse = tmp;
+
+ named.value = tmp->ptf->value;
+ tmp->cfgname = NULL;
+ ptf_match(cfg, &devcf, set_dev_cfgname, tmp);
+ if(tmp->cfgname == NULL) tmp->cfgname = ptf_defused_name(tmp->ptf->value);
+ };
+
+ return reverse;
+}
+
diff --git a/tools/cpu/nios2/devices.h b/tools/cpu/nios2/devices.h
new file mode 100644
index 0000000000..11164c5245
--- /dev/null
+++ b/tools/cpu/nios2/devices.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#ifndef __NIOS2GEN_DEVICES_H
+#define __NIOS2GEN_DEVICES_H 1
+
+#include "ptf.h"
+#include "bridges.h"
+
+typedef struct dev_descr
+{
+ int slaves;
+ char *cfgname;
+ struct ptf *ptf;
+ struct dev_descr *next;
+}
+device_desc;
+
+device_desc *find_devices(
+ struct ptf *ptf,
+ struct ptf *cfg,
+ struct ptf *cpu,
+ bus_bridge_pair *bridges);
+
+#endif
diff --git a/tools/cpu/nios2/nios2gen.c b/tools/cpu/nios2/nios2gen.c
new file mode 100644
index 0000000000..ad9631f5a8
--- /dev/null
+++ b/tools/cpu/nios2/nios2gen.c
@@ -0,0 +1,231 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ptf.h"
+#include "bridges.h"
+#include "clocks.h"
+#include "devices.h"
+#include "output.h"
+
+/********************************************************/
+
+void store_ptf_parent(struct ptf_item *pi, void *arg)
+{
+ struct ptf *p = pi->item[pi->level-1];
+ *(struct ptf **)arg = p;
+}
+
+/********************************************************/
+
+void store_ptf_ptr(struct ptf_item *pi, void *arg)
+{
+ struct ptf *p = pi->item[pi->level];
+ *(struct ptf **)arg = p;
+}
+
+/********************************************************/
+
+void printf_ptf_value(struct ptf_item *pi, void *arg)
+{
+ struct ptf *p = pi->item[pi->level];
+ printf(*(char **)arg, p->value);
+}
+
+/********************************************************/
+
+void read_include_file(struct ptf_item *pi, void *arg)
+{
+ struct ptf *inc, *next;
+ struct ptf *p = pi->item[pi->level];
+
+ inc = ptf_parse_file(p->value);
+
+ if(inc == NULL)
+ {
+ fprintf(stderr, "Warning: couldn't parse included '%s'.\n", p->value);
+ return;
+ };
+
+ printf("Successfully read included PTF file %s.\n", p->value);
+
+ next = p->next;
+ for(p->next = inc; p->next != NULL; p = p->next);
+ p->next = next;
+}
+
+void usage()
+{
+ fprintf(stderr,
+"Please specify the name of a nios2gen PTF file that describes where to\n"
+"find the system description PTF from SOPC Builder on the command line.\n");
+}
+
+/********************************************************/
+
+int main(int argc, char *argv[])
+{
+ struct ptf *sopc, *cfg, *p, *cpu;
+ struct ptf_item pi;
+ device_desc *devices;
+ bus_bridge_pair *bridges;
+ clock_desc *clocks;
+
+ if (argc<2)
+ {
+ usage();
+ return -1;
+ };
+
+ cfg = ptf_parse_file(argv[1]);
+ if(cfg == NULL)
+ {
+ fprintf(stderr, "Couldn't parse '%s'.\n", argv[1]);
+ return -1;
+ };
+
+ printf("Successfully read config PTF file %s.\n", argv[1]);
+
+ /********************************************************/
+
+ {
+ struct ptf include_item = { item, "INCLUDE", 0, 0, 0 };
+ struct ptf_item inc_file_spec = { 1, &include_item };
+ ptf_match(cfg, &inc_file_spec, read_include_file, NULL);
+ }
+
+ /********************************************************/
+
+ {
+ struct ptf *p;
+ struct ptf sopc_ptf_item = { item, "SOPC_PTF", 0, 0, 0 };
+ struct ptf_item sopc_ptf_spec = { 1, &sopc_ptf_item };
+ ptf_match(cfg, &sopc_ptf_spec, store_ptf_ptr, &p);
+
+ if(p == NULL)
+ {
+ fprintf(stderr, "Missing 'SOPC_PTF' filename in %s!\n", argv[1]);
+ return -1;
+ };
+
+ sopc = ptf_parse_file(p->value);
+ if(sopc == NULL)
+ {
+ fprintf(stderr, "Could not parse SOPC_PTF '%s'.\n", p->value);
+ return -1;
+ };
+
+ printf("Successfully read SOPC PTF file %s.\n", p->value);
+ };
+
+ /********************************************************/
+ /* Find CPU */
+
+ printf("Looking for usable CPUs...\n");
+
+ {
+ struct ptf modules = { section, "MODULES", 0, 0, 0 };
+ struct ptf cpu_def = { item, "CPU", 0, 0, 0 };
+ struct ptf_item cpu_spec = { 2, &modules, &cpu_def };
+
+ ptf_match(cfg, &cpu_spec, store_ptf_ptr, &cpu);
+ };
+
+ {
+ int cpu_count;
+ struct ptf system = { section, "SYSTEM", 0, 0, 0 };
+ struct ptf module = { section, "MODULE", 0, 0, 0 };
+ struct ptf nios2_cpu_class = { item, "class", "altera_nios2", 0, 0 };
+ struct ptf_item class_spec = { 3, &system, &module, &nios2_cpu_class };
+
+ if(cpu) if(cpu->value) class_spec.item[1]->value = cpu->value;
+
+ cpu_count = ptf_match(sopc, &class_spec, store_ptf_parent, &cpu);
+
+ if(cpu_count > 1)
+ {
+ printf("There is more than one CPU. Please specify the one\n");
+ printf("you want to use with this BSP in your config file.\n");
+ printf("The available CPUs are named as follows:\n");
+ ptf_match(sopc, &class_spec, printf_ptf_value, " %s\n");
+ return -1;
+ };
+
+ if(cpu_count == 0)
+ {
+ printf("There is no NIOS2 cpu in the system.\n");
+ return -1;
+ }
+ };
+
+ printf("Using NIOS II CPU '%s'.\n", cpu->value);
+ printf("Only modules mastered by this CPU are considered now.\n");
+
+ /********************************************************/
+ /* Find clocks */
+
+ printf("Looking for clock definitions...\n");
+
+ clocks = find_clocks(sopc, cfg);
+
+ if(clocks)
+ {
+ clock_desc *cs;
+ for(cs = clocks; cs; cs = cs->next)
+ {
+ printf("Found clock: %s (%lu Hz)\n", cs->name, cs->freq);
+ };
+ }
+ else
+ {
+ printf("No clocks present.\n");
+ };
+
+ /********************************************************/
+ /* Find Bridges */
+
+ printf("Looking for bus bridges...\n");
+
+ bridges = find_bridges(sopc);
+
+ if(bridges)
+ {
+ bus_bridge_pair *bbp;
+ for(bbp = bridges; bbp; bbp=bbp->next)
+ {
+ printf("Found bridge: %s\n", bbp->mastered_by);
+ printf(" \\_%s\n", bbp->bridges_to);
+ };
+ }
+ else
+ {
+ printf("No bridges present.\n");
+ };
+
+ /********************************************************/
+ /* Find other devices available to the selected CPU */
+
+ devices = find_devices(sopc, cfg, cpu, bridges);
+
+ fwrite_header_file(stdout, cfg, devices, clocks);
+
+ // ptf_printf(stdout, ptf, "");
+ // ptf_printf(stdout, cfg, "");
+
+ return 0;
+}
+
+/* vi:ts=4:
+ */
+
+
diff --git a/tools/cpu/nios2/output.c b/tools/cpu/nios2/output.c
new file mode 100644
index 0000000000..e957e430a7
--- /dev/null
+++ b/tools/cpu/nios2/output.c
@@ -0,0 +1,195 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ptf.h"
+#include "devices.h"
+#include "output.h"
+
+typedef struct
+{
+ FILE *file;
+ device_desc *dev;
+ char *def_name;
+ char *orig_value;
+ clock_desc *clocks;
+ device_desc *devices;
+}
+out_desc;
+
+int is_not_connected(struct ptf_item *pi)
+{
+ if(pi->item[0] == NULL) return 0;
+ if(pi->item[0]->name == NULL) return 0;
+
+ if(strcmp(pi->item[0]->name, "SLAVE") == 0)
+ {
+ struct ptf *t;
+ struct ptf_item ti;
+ t = ptf_find(pi->item[0]->sub, &ti, item, "N2G_Selected", "1");
+
+ if(t == NULL) return 1;
+ };
+
+ return 0;
+}
+
+void fwrite_devhead_def(struct ptf_item *pi, void *arg)
+{
+ out_desc *dinfo = arg;
+
+ if(pi != NULL) if(is_not_connected(pi)) return;
+
+ fprintf(dinfo->file, "#define %s_%s %s\n",
+ dinfo->dev->cfgname, dinfo->def_name, dinfo->orig_value);
+}
+
+void fwrite_devhead_line(struct ptf_item *pi, void *arg)
+{
+ out_desc *dinfo = arg;
+
+ if(is_not_connected(pi)) return;
+
+ if(strncmp(dinfo->orig_value, "N2G_", 4)==0)
+ {
+ if(strncmp(dinfo->orig_value, "N2G_CLOCKREF_", 13)==0)
+ {
+ clock_desc *c;
+ for(c = dinfo->clocks; c; c=c->next)
+ {
+ if(strcmp(c->name, pi->item[pi->level]->value) == 0)
+ {
+ fprintf(dinfo->file, "#define %s_%s %s\n",
+ dinfo->dev->cfgname, dinfo->orig_value + 13, c->cfgname);
+ break;
+ };
+ };
+ }
+ else if(strncmp(dinfo->orig_value, "N2G_DEVICEREF_", 14)==0)
+ {
+ device_desc *d;
+ for(d = dinfo->devices; d; d=d->next)
+ {
+ if(strcmp(d->ptf->value, pi->item[pi->level]->value) == 0)
+ {
+ fprintf(dinfo->file, "#define %s_%s %s\n",
+ dinfo->dev->cfgname, dinfo->orig_value + 14, d->cfgname);
+ break;
+ };
+ };
+ }
+ }
+ else
+ {
+ fprintf(dinfo->file, "#define %s_%s %s\n",
+ dinfo->dev->cfgname, dinfo->orig_value,
+ pi->item[pi->level]->value);
+ };
+}
+
+void fwrite_device_header(struct ptf_item *pi, void *arg)
+{
+ struct ptf *f;
+ struct ptf_item fi;
+ out_desc *dinfo = arg;
+
+ /* This is called for every matching CLASS section in the
+ configuration. The following loop iterates through all
+ items in the CLASS section regardless of their nesting level */
+
+ f = ptf_find(pi->item[pi->level]->sub, &fi, item, 0, 0);
+
+ while(f != NULL)
+ {
+ dinfo->orig_value = f->value;
+ if(f->name && strncmp(f->name, "N2G_DEFINE_", 11)==0)
+ {
+ dinfo->def_name = f->name + 11;
+ if(fi.level >= 2)
+ {
+ fi.level--; /* match only the enclosing section */
+ ptf_match(dinfo->dev->ptf->sub, &fi, fwrite_devhead_def, dinfo);
+ fi.level++;
+ }
+ else
+ {
+ fwrite_devhead_def( 0, dinfo );
+ };
+ }
+ else
+ {
+ f->value = 0; /* Match ANY value */
+ ptf_match(dinfo->dev->ptf->sub, &fi, fwrite_devhead_line, dinfo);
+ f->value = dinfo->orig_value;
+ };
+ f = ptf_next(&fi, item, 0, 0);
+ };
+}
+
+void fwrite_value(struct ptf_item *pi, void *arg)
+{
+ FILE *file = arg;
+ fputs(pi->item[pi->level]->value, file);
+}
+
+void fwrite_header_file( FILE *file, struct ptf *cfg, device_desc *devices, clock_desc *clocks)
+{
+ struct ptf *p;
+ struct ptf_item pi;
+
+ struct ptf aclass = { section, "CLASS", 0, 0, 0 };
+ struct ptf_item matchaclass = { 1, &aclass };
+
+ struct ptf header = { item, "HEADER", 0, 0, 0 };
+ struct ptf_item matchheader = { 1, &header };
+
+ struct ptf epilog = { item, "EPILOG", 0, 0, 0 };
+ struct ptf_item matchepilog = { 1, &epilog };
+
+ out_desc dinfo;
+
+ dinfo.file = file;
+ dinfo.clocks = clocks;
+ dinfo.devices = devices;
+
+ ptf_match(cfg, &matchheader, fwrite_value, file);
+
+ if(clocks)
+ {
+ clock_desc *cs;
+ for(cs = clocks; cs; cs = cs->next)
+ {
+ fprintf(file, "#define %s_FREQ %luu\n", cs->cfgname, cs->freq);
+ };
+ };
+
+ if(devices)
+ {
+ for(dinfo.dev = devices; dinfo.dev; dinfo.dev=dinfo.dev->next)
+ {
+ fprintf(file, "\n");
+
+ p = ptf_find(dinfo.dev->ptf, &pi, item, "class", 0);
+ if(p)
+ {
+ aclass.value = p->value;
+ ptf_match(cfg, &matchaclass, fwrite_device_header, &dinfo);
+ };
+ };
+ };
+
+ ptf_match(cfg, &matchepilog, fwrite_value, file);
+}
+
+
+
diff --git a/tools/cpu/nios2/output.h b/tools/cpu/nios2/output.h
new file mode 100644
index 0000000000..32aa0f7940
--- /dev/null
+++ b/tools/cpu/nios2/output.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#ifndef __OUTPUT_H
+#define __OUTPUT_H 1
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ptf.h"
+#include "clocks.h"
+#include "devices.h"
+
+void fwrite_header_file(FILE *file, struct ptf *cfg, device_desc *devices, clock_desc *clocks);
+
+#endif
+
+
diff --git a/tools/cpu/nios2/ptf.c b/tools/cpu/nios2/ptf.c
new file mode 100644
index 0000000000..43a38ea522
--- /dev/null
+++ b/tools/cpu/nios2/ptf.c
@@ -0,0 +1,807 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include "ptf.h"
+
+#define PTFPARSER_MAXDEPTH 20
+#define PTFPARSER_NAMEBUFSIZE 1024
+#define PTFPARSER_VALUEBUFSIZE 4096
+
+#define DEBUG_EXPECTATIONS 1
+#define DEBUG_READVALUES 2
+#define DEBUG_FINDER 4
+#define DEBUG 0
+
+struct ptf_parser_state
+{
+ struct ptf *tree;
+ struct ptf *current_in[PTFPARSER_MAXDEPTH];
+ struct ptf *current_item;
+
+ long line; /* starts with 1, increments whenever a LF (ASCII 10) passes */
+ int section_level; /* starts at 0, incremented at {, decremented at } */
+ char *filename;
+ char name_buffer[PTFPARSER_NAMEBUFSIZE];
+ int name_length;
+ char value_buffer[PTFPARSER_VALUEBUFSIZE];
+ int value_length;
+ struct
+ {
+ unsigned error:1;
+ unsigned escaped:1;
+ unsigned single_quoted:1;
+ unsigned double_quoted:1;
+ } flag;
+
+ enum
+ {
+ section_or_item_specification,
+ more_section_type_or_item_name_chars,
+ whitespace_after_section_specification,
+ whitespace_after_section_or_item_name,
+ whitespace_before_item_value,
+ more_section_name_chars,
+ more_item_value_chars,
+ } expectation;
+};
+
+/***************************************************************************/
+struct ptf *ptf_alloc_item(ptf_item_type t, char *name, char *value)
+{
+ struct ptf *new_item;
+ new_item = (struct ptf *)malloc(sizeof(struct ptf));
+ if(!new_item) return NULL;
+
+ new_item->type = t;
+ new_item->sub = NULL;
+ new_item->next = NULL;
+ new_item->name = NULL;
+ new_item->value = NULL;
+
+ if(name != NULL)
+ {
+ int n = strlen(name);
+ if(n > 0)
+ {
+ new_item->name = (char *)malloc(n + 1);
+ if(new_item->name == NULL)
+ {
+ free(new_item);
+ return NULL;
+ };
+ strcpy(new_item->name, name);
+ }
+ };
+
+ if(value != NULL)
+ {
+ int n = strlen(value);
+ if(n > 0)
+ {
+ new_item->value = (char *)malloc(n + 1);
+ if(new_item->value == NULL)
+ {
+ if(name != NULL) free(new_item->name);
+ free(new_item);
+ return NULL;
+ };
+ strcpy(new_item->value, value);
+ };
+ };
+
+ return new_item;
+}
+
+/***************************************************************************/
+void add_ptf_item(struct ptf_parser_state *state, struct ptf *item)
+{
+ if(state->current_item == NULL)
+ {
+ if(state->section_level > 0)
+ state->current_in[state->section_level-1]->sub = item;
+ else
+ state->tree = item;
+ }
+ else
+ state->current_item->next = item;
+}
+
+
+/***************************************************************************/
+void parse_error(struct ptf_parser_state *state, char *errmsg)
+{
+ fprintf(stderr, "Error while parsing %s (line %lu): %s\n",
+ state->filename, state->line, errmsg);
+
+ state->flag.error = 1;
+}
+
+/***************************************************************************/
+void init_parser(struct ptf_parser_state *state, char *filename)
+{
+ int i;
+
+ state->line = 1;
+ state->flag.error = 0;
+ state->flag.escaped = 0;
+ state->flag.double_quoted = 0;
+ state->flag.single_quoted = 0;
+ state->section_level = 0;
+
+ state->filename = (char *)malloc(strlen(filename)+1);
+ if(state->filename != NULL) strcpy(state->filename, filename);
+
+ state->expectation = section_or_item_specification;
+
+ state->tree = NULL;
+ state->current_item = NULL;
+ for(i=1; i<PTFPARSER_MAXDEPTH; i++) state->current_in[i] = NULL;
+}
+
+/***************************************************************************/
+void ptf_free(struct ptf *ptf)
+{
+ struct ptf *this, *next;
+ for(this = ptf; this != NULL; this = next)
+ {
+ next = this->next;
+ if(this->value != NULL) free(this->value);
+ if(this->name != NULL) free(this->name);
+ if(this->type == section) ptf_free(this->sub);
+ free(this);
+ };
+}
+
+/***************************************************************************/
+void abort_parsing(struct ptf_parser_state *state)
+{
+ if(state->filename != NULL) free(state->filename);
+ ptf_free(state->tree);
+}
+
+/***************************************************************************/
+int add_char_to_buffer(int *len, char *buf, int maxlen, char c)
+{
+ if(*len >= maxlen) return 0;
+
+ buf[(*len)++] = c;
+
+ return 1;
+}
+
+/***************************************************************************/
+void parse_char(struct ptf_parser_state *state, int c)
+{
+ int is_not_quoted;
+ int is_no_space;
+ enum { item_parsed, section_opened, section_closed, none } parser_event;
+
+ switch(c)
+ {
+ case '\\':
+ {
+ if(state->flag.escaped == 0)
+ {
+ state->flag.escaped = 1;
+ return;
+ };
+ break;
+ };
+ case '"':
+ {
+ if(!state->flag.escaped && !state->flag.single_quoted)
+ {
+ state->flag.double_quoted = 1 - state->flag.double_quoted;
+ return;
+ }
+ break;
+ };
+ case '\'':
+ {
+ if(!state->flag.escaped && !state->flag.double_quoted)
+ {
+ state->flag.single_quoted = 1 - state->flag.single_quoted;
+ return;
+ }
+ break;
+ };
+ case '\n':
+ {
+ state->line++;
+ break;
+ };
+ default:
+ break;
+ };
+
+ parser_event = none;
+
+ is_not_quoted = !(state->flag.escaped ||
+ state->flag.single_quoted || state->flag.double_quoted);
+ is_no_space = (!is_not_quoted || !isspace(c));
+ state->flag.escaped = 0;
+
+ switch(state->expectation)
+ {
+ case section_or_item_specification:
+ {
+#if DEBUG&DEBUG_EXPECTATIONS
+ printf("Expectation: section_or_item_specification\n");
+#endif
+
+ if(is_not_quoted && c == '}')
+ {
+ parser_event = section_closed;
+ }
+ else if(is_no_space)
+ {
+ state->name_length = 1;
+ state->name_buffer[0] = c;
+ state->expectation = more_section_type_or_item_name_chars;
+ };
+ break;
+ };
+
+ case more_section_type_or_item_name_chars:
+ {
+#if DEBUG&DEBUG_EXPECTATIONS
+ printf("Expectation: more_section_type_or_item_name_chars\n");
+#endif
+
+ /* Item name is stored in name_buffer */
+ /* Section type is stored in name_buffer */
+ if(is_no_space)
+ {
+ if(!add_char_to_buffer(&state->name_length, state->name_buffer, PTFPARSER_NAMEBUFSIZE, c))
+ parse_error(state, "First word is too long; I expected a shorter section type or item name");
+ }
+ else
+ {
+ state->expectation = whitespace_after_section_or_item_name;
+ }
+ break;
+ };
+
+ case whitespace_after_section_specification:
+ {
+#if DEBUG&DEBUG_EXPECTATIONS
+ printf("Expectation: whitespace_after_section_specification\n");
+#endif
+
+ if(c == '{')
+ parser_event = section_opened;
+ else if(is_no_space)
+ parse_error(state, "Expected section content within brackets {...}");
+ break;
+ };
+
+ case whitespace_after_section_or_item_name:
+ {
+#if DEBUG&DEBUG_EXPECTATIONS
+ printf("Expectation: whitespace_after_section_or_item_name\n");
+#endif
+
+ if(c == '{')
+ {
+ state->value_length = 0;
+ parser_event = section_opened;
+ }
+ else if(c == '=')
+ state->expectation = whitespace_before_item_value;
+ else if(is_no_space)
+ {
+ state->value_length = 1;
+ state->value_buffer[0] = c;
+ state->expectation = more_section_name_chars;
+ };
+ break;
+ };
+
+ case more_section_name_chars:
+ {
+#if DEBUG&DEBUG_EXPECTATIONS
+ printf("Expectation: more_section_name_chars\n");
+#endif
+
+ /* Section name is stored in value_buffer */
+ if(is_no_space)
+ {
+ if(!add_char_to_buffer(&state->value_length, state->value_buffer, PTFPARSER_VALUEBUFSIZE, c))
+ parse_error(state, "Section name is too long");
+ }
+ else
+ state->expectation = whitespace_after_section_specification;
+ break;
+ }
+
+ case whitespace_before_item_value:
+ {
+#if DEBUG&DEBUG_EXPECTATIONS
+ printf("Expectation: whitespace_before_item_value\n");
+#endif
+
+ if(is_not_quoted && c == ';')
+ {
+ state->value_length = 0;
+ parser_event = item_parsed;
+ }
+ else if(is_no_space)
+ {
+ state->value_length = 1;
+ state->value_buffer[0] = c;
+ state->expectation = more_item_value_chars;
+ };
+ break;
+ };
+
+ case more_item_value_chars:
+ {
+#if DEBUG&DEBUG_EXPECTATIONS
+ printf("Expectation: more_item_value_chars\n");
+#endif
+
+ /* Item value is stored in value_buffer */
+ if(is_not_quoted && c == ';')
+ parser_event = item_parsed;
+ else if(is_no_space)
+ {
+ if(!add_char_to_buffer(&state->value_length, state->value_buffer, PTFPARSER_VALUEBUFSIZE, c))
+ parse_error(state, "Item value is too long");
+ }
+ else
+ parser_event = item_parsed;
+ break;
+ }
+
+ default:
+#if DEBUG&DEBUG_EXPECTATIONS
+ printf("Expectation: %d (???)\n", state->expectation);
+#endif
+
+ parse_error(state, "Internal error: Unhandled state of expectation");
+ };
+
+ switch(parser_event)
+ {
+ /* TODO: pointer tuff */
+
+ case item_parsed:
+ {
+ struct ptf *new_item;
+ state->name_buffer[state->name_length] = 0;
+ state->value_buffer[state->value_length] = 0;
+#if DEBUG&DEBUG_READVALUES
+ printf("== Item %s is '%s' ==\n", state->name_buffer, state->value_buffer);
+#endif
+
+ new_item = ptf_alloc_item(item, state->name_buffer, state->value_buffer);
+ if(new_item == NULL)
+ {
+ parse_error(state, "Internal error: "
+ "Could not allocate memory for new item");
+ return;
+ };
+
+ add_ptf_item(state, new_item);
+ state->current_item = new_item;
+ state->expectation = section_or_item_specification;
+
+ break;
+ };
+ case section_opened:
+ {
+ struct ptf *new_section;
+ state->name_buffer[state->name_length] = 0;
+ state->value_buffer[state->value_length] = 0;
+#if DEBUG&DEBUG_READVALUES
+ printf("== New %s section '%s' opened ==\n", state->name_buffer, state->value_buffer);
+#endif
+
+ if(state->section_level >= PTFPARSER_MAXDEPTH-1)
+ {
+ parse_error(state, "Internal error: "
+ "cannot handle sections nested as deep as here.");
+ return;
+ };
+
+ new_section = ptf_alloc_item(section, state->name_buffer, state->value_buffer);
+ if(new_section == NULL)
+ {
+ parse_error(state, "Internal error: "
+ "Could not allocate memory for new section");
+ return;
+ };
+
+ add_ptf_item(state, new_section);
+ state->current_item = NULL;
+ state->current_in[state->section_level] = new_section;
+ state->section_level++;
+
+ state->expectation = section_or_item_specification;
+ break;
+ };
+ case section_closed:
+ {
+ if(state->section_level < 1)
+ {
+ parse_error(state, "Found closing '}' without opening '{' before");
+ return;
+ };
+
+ state->section_level--;
+ state->current_item = state->current_in[state->section_level];
+ state->expectation = section_or_item_specification;
+#if DEBUG&DEBUG_READVALUES
+ printf("-- Closed section --\n");
+#endif
+ break;
+ };
+ default:
+ break;
+ };
+}
+
+/***************************************************************************/
+struct ptf *ptf_parse_file(char *filename)
+{
+ FILE *f;
+ char buffer[1024];
+
+ struct ptf *root;
+ struct ptf_parser_state state;
+
+ if(filename == NULL)
+ {
+ fprintf(stderr, "Internal error: "
+ "No filename was given to ptf_read()\n");
+ return NULL;
+ };
+
+ f = fopen(filename, "r");
+ if(f == NULL)
+ {
+ perror(filename);
+ return NULL;
+ };
+
+ init_parser(&state, filename);
+
+ while(!feof(f))
+ {
+ size_t r, n;
+
+ if(ferror(f))
+ {
+ perror(filename);
+ abort_parsing(&state);
+ fclose(f);
+ return NULL;
+ };
+
+ n = fread(buffer, 1, 1024, f);
+ for(r=0; r<n && (state.flag.error==0); r++) parse_char(&state, buffer[r]);
+ };
+
+ fclose(f);
+
+ if(state.section_level != 0)
+ {
+ parse_error(&state, "Input file seems to be incomplete, "
+ "one or more sections are not closed with '}'\n");
+ };
+
+ if(state.flag.error)
+ {
+ abort_parsing(&state);
+ return NULL;
+ };
+
+ return state.tree;
+}
+
+/***************************************************************************/
+
+void ptf_printf(FILE *s, struct ptf *tree, char *prefix)
+{
+ struct ptf *leaf;
+
+ for(leaf = tree; leaf != NULL; leaf = leaf->next)
+ {
+ switch(leaf->type)
+ {
+ case section:
+ {
+ char *new_prefix;
+ int new_prefix_len;
+ new_prefix_len = strlen(prefix) + strlen(leaf->name) + 2;
+ if(leaf->value != NULL && leaf->value[0] != 0)
+ {
+ new_prefix_len += strlen(leaf->value) + 1;
+ };
+ new_prefix = (char *)malloc(new_prefix_len);
+ strcpy(new_prefix, prefix);
+ strcat(new_prefix, leaf->name);
+ if(leaf->value != NULL && leaf->value[0] != 0)
+ {
+ strcat(new_prefix, "_");
+ strcat(new_prefix, leaf->value);
+ };
+ strcat(new_prefix, "/");
+ fputs(new_prefix, s);
+ fputs("\r\n", s);
+ ptf_printf(s, leaf->sub, new_prefix);
+ break;
+ };
+
+ case item:
+ {
+ char *c;
+ fputs(prefix, s);
+ fputs(leaf->name, s);
+ fputs(" = \"", s);
+ for(c=leaf->value; *c; c++)
+ {
+ if(*c=='\\' || *c=='"') putc('\\', s);
+ putc(*c, s);
+ };
+ fprintf(s, "\"\r\n");
+ break;
+ };
+
+ default:
+ break;
+ };
+ };
+}
+
+/***************************************************************************/
+
+int ptf_advance_one(struct ptf_item *item)
+{
+ int d;
+ struct ptf *leaf;
+
+ d = item->level;
+ leaf = item->item[d];
+
+ if(leaf != NULL)
+ {
+ if(leaf->type == section && leaf->sub != NULL)
+ {
+ if(item->level >= MAX_SECTION_NESTING-1)
+ {
+ /* raise an error? hm, for now we silently ignore the subtree */
+ }
+ else
+ {
+ d++;
+ item->item[d] = leaf->sub;
+ item->level = d;
+ return 0;
+ }
+ }
+ item->item[item->level] = leaf->next;
+ };
+
+ while(item->item[d] == NULL)
+ {
+ if(d == 0)
+ {
+ item->level = 0;
+ item->item[0] = NULL;
+ errno = ENOENT;
+ return -1;
+ }
+ d --;
+ leaf = item->item[d];
+ if(leaf != NULL) item->item[d] = leaf->next;
+ };
+
+ item->level = d;
+ return 0;
+}
+
+/***************************************************************************/
+
+int ptf_advance_until(
+ struct ptf_item *item,
+ ptf_item_type ttype,
+ char *name,
+ char *value)
+{
+ int r;
+ struct ptf *leaf;
+
+ do
+ {
+ leaf = item->item[item->level];
+#if DEBUG&DEBUG_FINDER
+ printf(" Does %s/%s match %s/%s?\n", leaf->name, leaf->value, name, value);
+#endif
+
+ if(leaf->type == ttype)
+ {
+ if(name == NULL)
+ {
+ if(value == NULL)
+ {
+ return 0; /* got it (any value) */
+ }
+ else if (leaf->value != NULL)
+ {
+ if(strcmp(leaf->value, value) == 0) return 0; /* got it */
+ }
+ }
+ else if(leaf->name != NULL)
+ {
+ if(strcmp(leaf->name, name) == 0)
+ {
+ if(value == NULL)
+ {
+ return 0; /* got it (any value) */
+ }
+ else if(leaf->value != NULL)
+ {
+ if(strcmp(leaf->value, value) == 0) return 0; /* got it */
+ }
+ }
+ }
+ };
+ r = ptf_advance_one(item);
+
+ } while(r == 0);
+
+ return r;
+}
+
+/***************************************************************************/
+
+struct ptf *ptf_find(
+ struct ptf *tree,
+ struct ptf_item *item,
+ ptf_item_type ttype,
+ char *name,
+ char *value)
+{
+ int r;
+
+ if(item == NULL) { errno = EINVAL; return NULL; };
+ if(tree == NULL) { errno = ENOENT; return NULL; };
+
+ item->level = 0;
+ item->item[0] = tree;
+
+ if(ptf_advance_until(item, ttype, name, value) != 0) return NULL;
+
+ r = item->level;
+ item->level++; /* To match ptf_match */
+ return item->item[r];
+}
+
+/***************************************************************************/
+
+struct ptf *ptf_next(
+ struct ptf_item *item,
+ ptf_item_type ttype,
+ char *name,
+ char *value)
+{
+ int r;
+ struct ptf *leaf;
+
+ if(item == NULL) { errno = EINVAL; return NULL; };
+
+ if(item->level < 1) return NULL;
+ item->level--; /* To match ptf_match */
+
+ r = ptf_advance_one(item);
+
+ if(r == 0) r = ptf_advance_until(item, ttype, name, value);
+
+ if(r != 0) return NULL;
+
+ r = item->level;
+ item->level++; /* To match ptf_match */
+ return item->item[r];
+}
+
+/***************************************************************************/
+
+int ptf_match(
+ struct ptf *const ptf,
+ struct ptf_item *const match,
+ const ptf_match_action action,
+ void *arg)
+{
+ int count;
+ struct ptf *p;
+ struct ptf_item pi;
+
+ p = ptf;
+ count = 0;
+ pi.level = 0;
+
+ while(p != NULL)
+ {
+ ptf_item_type mtype = match->item[pi.level]->type;
+ char *mname = match->item[pi.level]->name;
+ char *mvalue = match->item[pi.level]->value;
+
+#if DEBUG&DEBUG_FINDER
+ printf("Looking for %s/%s, checking %s/%s\n",
+ mname, mvalue, p->name, p->value);
+#endif
+
+ if(mtype == p->type &&
+ (mname==NULL || p->name==NULL || strcmp(mname, p->name)==0) &&
+ (mvalue==NULL || p->value==NULL || strcmp(mvalue, p->value)==0))
+ {
+ pi.item[pi.level] = p;
+
+ if(pi.level == match->level - 1)
+ {
+ if(action != NULL) action(&pi, arg);
+ p = p->next;
+ count++;
+ }
+ else
+ {
+ if(p->sub != NULL && pi.level < MAX_SECTION_NESTING-1)
+ {
+ pi.item[pi.level] = p;
+ pi.level++;
+ p = p->sub;
+ }
+ else
+ {
+ p = p->next;
+ };
+ };
+ }
+ else
+ {
+ p = p->next;
+ };
+
+ while(p == NULL && pi.level > 0)
+ {
+ pi.level--;
+ p = pi.item[pi.level]->next;
+ };
+ };
+ return count;
+}
+
+/***************************************************************************/
+
+char *ptf_defused_name(char *orig_name)
+{
+ int i,j;
+ char *s = (char *)malloc(1+strlen(orig_name));
+
+ if(!s) return NULL;
+
+ for(i=j=0;orig_name[i];i++)
+ {
+ if(!isalnum(orig_name[i]))
+ {
+ if(j>0) if(s[j-1]!='_') s[j++]='_';
+ }
+ else
+ {
+ s[j++] = toupper(orig_name[i]);
+ };
+ };
+ s[j] = 0;
+ return s;
+}
+
diff --git a/tools/cpu/nios2/ptf.h b/tools/cpu/nios2/ptf.h
new file mode 100644
index 0000000000..50955cb4a5
--- /dev/null
+++ b/tools/cpu/nios2/ptf.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#if !defined(__PTF_H)
+#define __PTF_H 1
+
+#include <stdio.h>
+
+#define MAX_SECTION_NESTING 20
+
+typedef enum
+{
+ item, section
+}
+ptf_item_type;
+
+struct ptf
+{
+ ptf_item_type type;
+ char *name;
+ char *value;
+ struct ptf *sub;
+ struct ptf *next;
+};
+
+struct ptf_item
+{
+ int level;
+ struct ptf *item[MAX_SECTION_NESTING];
+};
+
+
+typedef void (*ptf_match_action)(struct ptf_item *x, void *arg);
+
+struct ptf *ptf_parse_file(char *filename);
+struct ptf *ptf_alloc_item(ptf_item_type t, char *name, char *value);
+void ptf_printf(FILE *s, struct ptf *tree, char *prefix);
+
+struct ptf *ptf_find(
+ struct ptf *tree,
+ struct ptf_item *item,
+ ptf_item_type ttype,
+ char *name,
+ char *value);
+
+struct ptf *ptf_next(
+ struct ptf_item *item,
+ ptf_item_type ttype,
+ char *name,
+ char *value);
+
+int ptf_match(
+ struct ptf *const ptf,
+ struct ptf_item *const match,
+ const ptf_match_action action,
+ void *arg);
+
+char *ptf_defused_name(char *);
+
+
+#endif /* !defined(__PTF_H) */
+