summaryrefslogtreecommitdiffstats
path: root/tools/cpu/nios2/nios2gen.c
blob: ad9631f5a8f9e3bff0cacb94dda6e3f32d362a89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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:
 */