dol: fix shm_t contain the shm-buffers
[jump.git] / hdf2arch / hdf2arch.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use v5.010;
4
5 use XML::Simple qw(:strict);
6 use Data::Dumper;
7
8 my $infile  = shift;
9 my $outfile = shift;
10 my $data;
11
12 # supported chips
13 my $chips = {
14         'E16G301' => {
15                 'cores' => [4, 4],
16                 'sram'  => '0x8000',
17         },
18         'E64G401' => {
19                 'cores' => [8, 8],
20                 'sram'  => '0x8000',
21         },
22 };
23
24 # parse arguments
25 if(!$infile || !$outfile) {
26         say "Usage: hdf2arch <infile> <outfile>";
27         say "\tinfile:  Epiphany HDF file";
28         say "\toutfile: DOL XML file";
29         say "\tSpecify '-' to use standard input/output";
30         exit 1;
31 }
32
33 # parse HDF file
34 my $num_chips;          # number of chips in system
35 my @chiplist;           # information about current chip
36 my $chipnum = -1;       # current chip
37 my $num_emems;          # number of emems in system
38 my @ememlist;           # information about current emem
39 my $ememnum  = -1;      # current emem
40 open (FH, "<$infile") or die("Can't open $infile.");
41 while(<FH>) {
42         next if /\/\/.*/;       # skip comment lines
43
44         # globals - ignore them for now
45         #$data->{'version'} = $1 if(/^PLATFORM_VERSION\s+(\S+)\s*$/);
46         #$data->{'regbase'} = $1 if(/^ESYS_REGS_BASE\s+(\S+)\s*$/);
47
48
49         # number of chips
50         if(/^NUM_CHIPS\s+(\S+)\s*$/) {
51                 if(!$num_chips) {
52                         $num_chips = $1;
53                 } else {
54                         say STDERR "Warning: multiple 'NUM_CHIPS' entries!";
55                 }
56         }
57
58         # per-chip information
59         if(/^CHIP\s+(\S+)\s*$/) {
60                 my $id = $1;
61                 if($chips->{$id}) {
62                         $chipnum++;
63                         $chiplist[$chipnum]->{'id'} = $id;
64                         $chiplist[$chipnum]->{'rows'} =
65                                 $chips->{$id}->{'cores'}->[0];
66                         $chiplist[$chipnum]->{'cols'} =
67                                 $chips->{$id}->{'cores'}->[1];
68                         $chiplist[$chipnum]->{'sram'} = $chips->{$id}->{'sram'};
69                 } else {
70                         say STDERR "Warning: unknown CHIP '$id' - ignoring!";
71                         next;
72                 }
73         }
74         $chiplist[$chipnum]->{'row'} = $1 if(/^CHIP_ROW\s+(\S+)\s*$/);
75         $chiplist[$chipnum]->{'col'} = $1 if(/^CHIP_COL\s+(\S+)\s*$/);
76
77         # number of emems
78         if(/^NUM_EXT_MEMS\s+(\S+)\s*$/) {
79                 if(!$num_emems) {
80                         $num_emems = $1;
81                 } else {
82                         say STDERR "Warning: multiple 'NUM_EXT_EMEM' entries!";
83                 }
84         }
85
86
87         # per-emem information
88         if(/^EMEM\s+(\S+)\s*$/) {
89                 my $id = $1;
90                 if($id eq 'ext-DRAM') {
91                         $ememnum++;
92                         $ememlist[$ememnum]->{'id'} = $id;
93                 } else {
94                         say STDERR "Warning: unknown EMEM '$id' - ignoring!";
95                         next;
96                 }
97         }
98         $ememlist[$ememnum]->{'base'} = $1 if(/^EMEM_BASE_ADDRESS\s+(\S+)\s*$/);
99         $ememlist[$ememnum]->{'epi'}  = $1 if(/^EMEM_EPI_BASE\s+(\S+)\s*$/);
100         $ememlist[$ememnum]->{'size'} = $1 if(/^EMEM_SIZE\s+(\S+)\s*$/);
101         $ememlist[$ememnum]->{'type'} = $1 if(/^EMEM_TYPE\s+(\S+)\s*$/);
102 }
103
104 # some simple consistency checks
105 if($num_chips != scalar @chiplist) {
106         say STDERR "Warning: NUM_CHIPS does not match the number of CHIPs!";
107 }
108 if($num_emems != scalar @ememlist) {
109         say STDERR "Warning: NUM_EXT_MEMS does not match the number of EMEMs!";
110 }
111
112 # generate output data
113 $data->{'processor'} = [];
114 $data->{'memory'}    = [];
115
116 # host processor, epiphany cores and their memories
117 push $data->{'processor'}, {
118         'name' => 'arm_0',
119         'type' => 'RISC',
120         'configuration' => [
121                 { 'name'  => 'cores',
122                   'value' => '2' },
123         ],
124 };
125
126 for my $chip(@chiplist) {
127         for my $row (0..$chip->{'rows'}-1) {
128                 for my $col (0..$chip->{'cols'}-1) {
129                         my $r = $chip->{'row'} + $row;
130                         my $c = $chip->{'col'} + $col;
131                         push $data->{'processor'}, {
132                                 'name' => "core_${r}_${c}",
133                                 'type' => 'RISC',
134                                 'configuration' => [
135                                         { 'name'  => 'type',
136                                           'value' => $chip->{'id'}, },
137                                 ],
138                         };
139
140                         push $data->{'memory'}, {
141                                 'name' => "mem_${r}_${c}",
142                                 'type' => 'RAM',
143                                 'configuration' => [
144                                         { 'name'  => 'size',
145                                           'value' => $chip->{'sram'}, },
146                                 ],
147                         };
148                 }
149         }
150 }
151
152 # external memories
153 for my $idx(0..(scalar @ememlist-1)) {
154         push $data->{'memory'}, {
155                 'name' => "shm_$idx",
156                 'type' => "RAM",
157                 'configuration' => [
158                         { 'name'  => 'size',
159                           'value' => $ememlist[$idx]->{'size'} },
160                         { 'name'  => 'type',
161                           'value' => $ememlist[$idx]->{'type'} },
162                         { 'name'  => 'base',
163                           'value' => $ememlist[$idx]->{'base'} },
164                         { 'name'  => 'epibase',
165                           'value' => $ememlist[$idx]->{'epi'} },
166                 ],
167         };
168 }
169
170 #say Dumper $data;
171
172 # write data in xml format
173 $data->{'name'} = 'Adapteva Parallella Architecture';
174 $data->{'xmlns'} = 'http://www.tik.ee.ethz.ch/~shapes/schema/ARCHITECTURE';
175 $data->{'xmlns:xsi'} = 'http://www.w3.org/2001/XMLSchema-instance';
176 $data->{'xsi:schemaLocation'} = 'http://www.tik.ee.ethz.ch/~shapes/schema/ARCHITECTURE http://www.tik.ee.ethz.ch/~shapes/schema/architecture.xsd';
177 my $outxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".
178                 XMLout({'architecture' => $data},
179                         KeyAttr => [],
180                         KeepRoot => 1);
181
182 if($outfile eq '-') {
183         say $outxml;
184         exit 0;
185 } else {
186         open (FH, ">$outfile");
187         say FH $outxml;
188         close (FH);
189 }