fiji_instrument.pl 15.6 KB
Newer Older
Christian Fibich's avatar
Christian Fibich committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#-------------------------------------------------------------------------------
#  University of Applied Sciences Technikum Wien
#
#  Department of Embedded Systems
#  http://embsys.technikum-wien.at
#
#  Josef Ressel Center for Verification of Embedded Computing Systems
#  http://vecs.technikum-wien.at
#
#-------------------------------------------------------------------------------
#  File:              fiji_instrument.pl
#  Created on:        29.04.2015
#  $LastChangedBy$
#  $LastChangedDate$
#
#  Description:
#
#  FIJI instrument script
#
#-------------------------------------------------------------------------------

22
## @file
Christian Fibich's avatar
Christian Fibich committed
23
24
25
# @brief FIJI instrument script
## @file
# Instruments a given netlist with the given settings
26
27
28
29

use strict;
use warnings;

30
31
use Scalar::Util 'blessed';

32
33
34
use FindBin;
use lib "$FindBin::Bin";

35
36
use File::Basename qw(basename);

37
use Log::Log4perl qw(get_logger);
38
use Clone qw(clone);
39
use Digest::CRC qw(crcccitt);
40

41
42
use FIJI qw(:all);

43
44
use FIJI::Settings;
use FIJI::Netlist;
45

46
use FIJI::VHDL;
47
48
use FIJI::Constraints;

Christian Fibich's avatar
Christian Fibich committed
49
use File::Spec;
50
use File::Path qw(make_path);
51

52
use Data::Dumper;
53
54
use Getopt::Long;

Christian Fibich's avatar
Christian Fibich committed
55
56
57
58
59
60
use constant NETLIST_SUFFIX => {
    ALTERA_QUARTUS => 'vqm',
    XILINX_VIVADO  => 'vm',
    OTHER          => 'v',
};

61
62
63
64
65
66
67
68
69
70
my @argv;

sub log_start {
    my $name = basename($0);
    $name =~ s/\.p[lm]//;
    my $logger = get_logger("");
    $logger->debug("=== Starting new execution of $name ===");
    $logger->debug(sprintf("%d argument(s)%s", scalar(@argv), scalar(@argv) > 0 ? ": @argv" : ""));
}

71
sub main {
72
    my $logger = get_logger("");
73
74
75
76
77
78
    my ($options) = @_;

    my $msg;
    my $rv;

    my %hash;
Christian Fibich's avatar
Christian Fibich committed
79
    my $self = bless(\%hash);
80
81
    my $output_dir;

82
    my ($settings_ref, $msg) = FIJI::Settings->new("setup", $options->{'fiji_settings_file'});
83
84
    my $netlist_filename = $options->{'netlist_file'};

85
86
87
88
    if (defined $msg) {
        $logger->error($msg);
    }

Christian Fibich's avatar
Christian Fibich committed
89
    if (!ref($settings_ref)) {
90
91
92
93
        return 1;
    }
    $self->{'settings'} = $settings_ref;

94
    # Retrieve directory meta data of the settings file
95
    my ($basevolume, $basedirs, $file) = File::Spec->splitpath($self->{'settings'}->{'filename'});
Christian Fibich's avatar
Christian Fibich committed
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    # Set/Create output directory
    if (defined $options->{'output_dir'}) {
        # Command line parameter has highest precedence
        $output_dir = $options->{'output_dir'};
    } else {
        # If parameter is not given default is to use FIJI::Settings value
        # (which is either given in the settings file or by the default in FIJI.pm)
        $output_dir = File::Spec->rel2abs($self->{'settings'}->{'design'}->{'OUTPUT_DIR'}, File::Spec->catpath($basevolume, $basedirs, ""));
        $logger->info("Using output directory from FIJI settings: \"$output_dir\".");
    }

    if (!(-d $output_dir)) {
        make_path($output_dir, {error => \my $err});

        if (@$err) {
            my $msg = "Could not create output directory: $!";
            $logger->fatal($msg);
            return 1;
        }
    }

118
    # Configure logging
Christian Fibich's avatar
Christian Fibich committed
119
    if (defined $self->{'settings'}->{'design'}->{'INSTRUMENTATION_LOG'}) {
120
121
122
123
124
125
126
127
128
129
        my $logfile_path;
        # If an output dir is given put log file relatively to that
        if (defined($options->{'output_dir'})) {
            $logfile_path = $options->{'output_dir'};
        } else {
            # ... else put the log file relatively to the directory used for the settings file
            $logfile_path = File::Spec->catpath($basevolume, $basedirs, "");
        }
        $logfile_path = File::Spec->rel2abs($self->{'settings'}->{'design'}->{'INSTRUMENTATION_LOG'}, $logfile_path);

Christian Fibich's avatar
Christian Fibich committed
130
        my $file_appender        = $Log::Log4perl::Logger::APPENDER_BY_NAME{'logfile'};
131
        my $custom_file_appender = Log::Log4perl::Appender->new(
Christian Fibich's avatar
Christian Fibich committed
132
133
134
            "Log::Log4perl::Appender::File",
            name      => "custom_file_log",
            Threshold => "TRACE",
135
            filename  => $logfile_path,
Christian Fibich's avatar
Christian Fibich committed
136
        );
137
138
        my $layout = $file_appender->{'layout'};
        $custom_file_appender->layout(clone($layout));
139
140
141
142
143
144
145
146
147
148
149

        my $ign = new Log::Log4perl::Filter("ignore_all_filter", sub { 0 });
        foreach my $a (values %{Log::Log4perl->appenders()}) {
            if ($a->name eq "logfile") {
                $logger->info("Switching from initial log file to custom log file " . $custom_file_appender->filename);
                unlink($a->filename);
                Log::Log4perl->eradicate_appender($a->name);
                next;
            }
            $a->filter($ign);
        }
150
        $logger->add_appender($custom_file_appender);
151
152
153
154
        log_start();
        foreach my $a (values %{Log::Log4perl->appenders()}) {
            $a->{filter} = undef;
        }
155
156
    }

Christian Fibich's avatar
Christian Fibich committed
157
158
159
160
161
162
163
164
165
166
167
168
169
    my $nl = new FIJI::Netlist();

    if ($nl->read_file($netlist_filename) != 0) {
        my $msg = "Netlist could not be loaded correctly from \"$netlist_filename\".";
        $logger->fatal($msg);
        return 1;
    }

    #
    # Retrieve toplevel module
    #

    my $toplevel_module = $nl->get_toplevel_module();
Christian Fibich's avatar
Christian Fibich committed
170
171

    if (ref($toplevel_module) ne "Verilog::Netlist::Module") {
Christian Fibich's avatar
Christian Fibich committed
172
173
174
175
176
177
178
        $logger->fatal($toplevel_module);
        return 1;
    }

    if (!defined $options->{'file_prefix'}) {
        $options->{'file_prefix'} = $toplevel_module->name;
    }
Christian Fibich's avatar
Christian Fibich committed
179
180
181
182
    my $nsh = NETLIST_SUFFIX;
    my $netlist_suffix = (defined $nsh->{$settings_ref->{'design'}->{'IMPLEMENTATION_TOOL'}}) ?
                          $nsh->{$settings_ref->{'design'}->{'IMPLEMENTATION_TOOL'}} :
                          $nsh->{'OTHERS'};
Christian Fibich's avatar
Christian Fibich committed
183

Christian Fibich's avatar
Christian Fibich committed
184
    my $export_nl_filename                    = File::Spec->catfile($output_dir,  $options->{'file_prefix'} . "_instrumented.".$netlist_suffix);
Christian Fibich's avatar
Christian Fibich committed
185
186
    my $export_cfg_filename                   = File::Spec->catfile($output_dir,  $options->{'file_prefix'} . "_download.cfg");
    my $export_wrapper_filename               = File::Spec->catfile($output_dir,  $options->{'file_prefix'} . "_wrapper.vhd");
Christian Fibich's avatar
Christian Fibich committed
187
188
    my $export_pkg_filename                   = File::Spec->catfile($output_dir,  $options->{'file_prefix'} . "_config_pkg.vhd");
    my $export_constraints_filename           = File::Spec->catfile($output_dir,  $options->{'file_prefix'} . "_constraints");
Christian Fibich's avatar
Christian Fibich committed
189

190
191
192
    #
    # start FIU instrumentation
    #
193

194
    # FIU part
Christian Fibich's avatar
Christian Fibich committed
195
    for (my $fiu_idx = 0 ; $fiu_idx < @{$settings_ref->{'fius'}} ; $fiu_idx++) {
196

Christian Fibich's avatar
Christian Fibich committed
197
        my $fiu = @{$settings_ref->{'fius'}}[$fiu_idx];
198

199
        my $ret = $nl->instrument_net($fiu->{'FIU_NET_NAME'}, $fiu_idx, $fiu->{'FIU_DRIVER_PATH'}, $fiu->{'FIU_DRIVER_TYPE'});
200
201
202
203
204

        if ($ret) {
            $logger->error($ret);
            return 1;
        }
205
    }
206

207
208
    # Clock net
    $logger->info("Adding CLOCK net");
209
210
211
    my $clk_net = $nl->get_net_from_path($settings_ref->{'design'}->{'CLOCK_NET'});
    if (!blessed($clk_net) || !$clk_net->isa("Verilog::Netlist::Net")) {
        $logger->error($clk_net);
212
213
214
        return 1;
    }

215
    my $ret = $nl->net_add_function($clk_net, FIJI::VHDL->FIJI_PORTTYPE_CLOCK, "clock_from_dut_o");
216
    if ($ret) {
217
        $logger->error($ret);
218
        return 1;
219
    }
220
221

    # Instrument reset from DUT if enabled
Christian Fibich's avatar
Christian Fibich committed
222
    if ($settings_ref->{'design'}->{'RESET_DUT_OUT_EN'} == 1) {
223
224
        $logger->info("Adding RESET_DUT_OUT net");

225
226
227
        my $rst_in_net = $nl->get_net_from_path($settings_ref->{'design'}->{'RESET_DUT_OUT_NAME'});
        if (!blessed($rst_in_net) || !$rst_in_net->isa("Verilog::Netlist::Net")) {
            $logger->error($rst_in_net);
228
229
230
            return 1;
        }

231
        my $ret = $nl->net_add_function($rst_in_net, FIJI::VHDL->FIJI_PORTTYPE_RESET_FROM_DUT, "reset_from_dut_o");
232
233
234
235
        if ($ret) {
            $logger->error($ret);
            return 1;
        }
236
237
    }

238
    # Instrument reset to DUT if enabled
Christian Fibich's avatar
Christian Fibich committed
239
    if ($settings_ref->{'design'}->{'RESET_DUT_IN_EN'} == 1) {
240
241
        $logger->info("Adding RESET_DUT_IN net");

Christian Fibich's avatar
Christian Fibich committed
242
        my $port = $toplevel_module->find_port($settings_ref->{'design'}->{'RESET_DUT_IN_NAME'});
243

Christian Fibich's avatar
Christian Fibich committed
244
        if (!defined $port) {
Christian Fibich's avatar
Christian Fibich committed
245
            $msg = "Could not find port '" . $toplevel_module->name . "|" . $settings_ref->{'design'}->{'RESET_DUT_IN_NAME'} . "'\n";
246
247
248
249
            $logger->error($msg);
            return 1;
        }

Christian Fibich's avatar
Christian Fibich committed
250
        $port->userdata(FIJI::VHDL->FIJI_USERDATA_PORTTYPE, FIJI::VHDL->FIJI_PORTTYPE_RESET_TO_DUT);
Christian Fibich's avatar
Christian Fibich committed
251
        $toplevel_module->link;
252
    }
253

254
    # Trigger from DUT if enabled
255

Christian Fibich's avatar
Christian Fibich committed
256
    if ($settings_ref->{'design'}->{'TRIGGER_DUT_EN'} == 1) {
257
        $logger->info("Adding TRIGGER_DUT net");
258
259
260
        my $rst_out_net = $nl->get_net_from_path($settings_ref->{'design'}->{'TRIGGER_DUT_NAME'});
        if (!blessed($rst_out_net) || !$rst_out_net->isa("Verilog::Netlist::Net")) {
            $logger->error($rst_out_net);
261
262
263
            return 1;
        }

264
        my $ret = $nl->net_add_function($rst_out_net, FIJI::VHDL->FIJI_PORTTYPE_TRIGGER_FROM_DUT, "trigger_from_dut_o");
265
266
267
268
        if ($ret) {
            $logger->error($ret);
            return 1;
        }
269
270
    }

271
    # instrument fault detect signal 1 if enabled
Christian Fibich's avatar
Christian Fibich committed
272
    if ($settings_ref->{'design'}->{'FAULT_DETECT_1_EN'} == 1) {
273
        $logger->info("Adding FAULT_DETECT_1 net");
274

275
276
277
        my $fd1_net = $nl->get_net_from_path($settings_ref->{'design'}->{'FAULT_DETECT_1_NAME'});
        if (!blessed($fd1_net) || !$fd1_net->isa("Verilog::Netlist::Net")) {
            $logger->error($fd1_net);
278
279
280
            return 1;
        }

281
        my $ret = $nl->net_add_function($fd1_net, FIJI::VHDL->FIJI_PORTTYPE_FAULT_DETECTION, "fault_detect_1_o", 0);
282
283
284
285
        if ($ret) {
            $logger->error($ret);
            return 1;
        }
286
287
    }

288
    # instrument fault detect signal 2 if enabled
Christian Fibich's avatar
Christian Fibich committed
289
    if ($settings_ref->{'design'}->{'FAULT_DETECT_2_EN'} == 1) {
290
        $logger->info("Adding FAULT_DETECT_2 net");
291

292
293
294
        my $fd2_net = $nl->get_net_from_path($settings_ref->{'design'}->{'FAULT_DETECT_2_NAME'});
        if (!blessed($fd2_net) || !$fd2_net->isa("Verilog::Netlist::Net")) {
            $logger->error($fd2_net);
295
296
            return 1;
        }
297

298
        my $ret = $nl->net_add_function($fd2_net, FIJI::VHDL->FIJI_PORTTYPE_FAULT_DETECTION, "fault_detect_2_o", 0);
299
300
301
302
303
        if ($ret) {
            $logger->error($ret);
            return 1;
        }
    }
304

305
306
307
308
309
310
311
    # calculate design ID
    # Design ID = CRC-CCITT of all input data:
    # - input netlist (file content)
    # - input settings file (file content)
    # - toplevel module name
    # - file prefix
    #
Christian Fibich's avatar
Christian Fibich committed
312
    # @FIXME: OK generating the ID like this?
313
    #
Christian Fibich's avatar
Christian Fibich committed
314
    my $ctx = Digest::CRC->new(type => "crcccitt");
315

Christian Fibich's avatar
Christian Fibich committed
316
    open(my $fh, "<", $netlist_filename);
317

Christian Fibich's avatar
Christian Fibich committed
318
    if (!defined $fh) {
319
320
        $logger->error($!);
        return 1;
321
322
    }

323
324
325
326
    $ctx->addfile($fh);

    close($fh);

Christian Fibich's avatar
Christian Fibich committed
327
    open($fh, "<", $options->{'fiji_settings_file'});
328

Christian Fibich's avatar
Christian Fibich committed
329
    if (!defined $fh) {
330
331
        $logger->error($!);
        return 1;
332
333
    }

334
335
336
337
    $ctx->addfile($fh);

    close($fh);

Christian Fibich's avatar
Christian Fibich committed
338
    $ctx->add($toplevel_module->name);
Christian Fibich's avatar
Christian Fibich committed
339
    $ctx->add($options->{'file_prefix'});
340

341
342
    my $id = hex $ctx->hexdigest;

Christian Fibich's avatar
Christian Fibich committed
343
    $logger->info(sprintf("Design ID chosen to be 0x%04X.", $id));
344
345
    #
    # Generate download cfg file
Christian Fibich's avatar
Christian Fibich committed
346
    # @FIXME should we overwrite the original file instead?
347
348
349
350
351
352
353
354
    #
    $settings_ref->{'design'}->{'ID'} = $id;
    $settings_ref->save($export_cfg_filename);

    #
    # output netlist
    #

Christian Fibich's avatar
Christian Fibich committed
355
    $rv = $nl->export($export_nl_filename, $id);
356

Christian Fibich's avatar
Christian Fibich committed
357
    if (defined $rv) {
358
359
        $logger->error($rv);
        return 1;
360
361
    }

362
363
364
365
366
    #
    # Make wrapper
    #
    my $wrapper_config = {};
    $wrapper_config->{'netlist'}                  = $nl->{'nl'};
Christian Fibich's avatar
Christian Fibich committed
367
    $wrapper_config->{'dut_toplevel_module_name'} = $toplevel_module->name;
368
369
370
371
372
    $wrapper_config->{'fiji_settings_filename'}   = $export_cfg_filename;
    $wrapper_config->{'vhdl_filename'}            = $export_wrapper_filename;

    $rv = FIJI::VHDL->generate_wrapper_module($wrapper_config);

Christian Fibich's avatar
Christian Fibich committed
373
    if (defined $rv) {
374
375
        $logger->error($rv);
        return 1;
376
    }
377

378
379
380
    #
    # Generate cfg vhd
    #
Christian Fibich's avatar
Christian Fibich committed
381
    $rv = FIJI::VHDL->generate_config_package($export_cfg_filename, $export_pkg_filename);
382

Christian Fibich's avatar
Christian Fibich committed
383
    if (defined $rv) {
384
385
        $logger->error($rv);
        return 1;
386
387
    }

Christian Fibich's avatar
Christian Fibich committed
388
389
390
391
392
393
    my $dut_inst_name  = (defined $settings_ref->{'design'}->{'DUT_INST_NAME'})     ? $settings_ref->{'design'}->{'DUT_INST_NAME'}     : FIJI::VHDL->FIJI_DEFAULTS->{'DUT_INST_NAME'};
    my $fiji_inst_name = (defined $settings_ref->{'design'}->{'FIJI_INST_NAME'})    ? $settings_ref->{'design'}->{'FIJI_INST_NAME'}    : FIJI::VHDL->FIJI_DEFAULTS->{'FIJI_INST_NAME'};
    my $wrapper_name   = (defined $settings_ref->{'design'}->{'FIJI_WRAPPER_NAME'}) ? $settings_ref->{'design'}->{'FIJI_WRAPPER_NAME'} : FIJI::VHDL->FIJI_DEFAULTS->{'FIJI_WRAPPER_NAME'};
    my $gen_fault_injection_label   = FIJI::VHDL->FIJI_DEFAULTS->{'FIJI_FAULT_INJECTION_GEN_LABEL'};


394
395
396
    #
    # Generate placement constraints
    #
Christian Fibich's avatar
Christian Fibich committed
397
    $rv = FIJI::Constraints->generate_constraints(
398
        $settings_ref->{'design'}->{'SYNTHESIS_TOOL'},
Christian Fibich's avatar
Christian Fibich committed
399
        $settings_ref->{'design'}->{'IMPLEMENTATION_TOOL'}, {
400
            dut_module  => $wrapper_config->{'dut_toplevel_module_name'},
Christian Fibich's avatar
Christian Fibich committed
401
402
403
404
            fiji_module => $wrapper_name,
            dut_inst    => $dut_inst_name,
            fiji_inst   => $fiji_inst_name,
            fiji_gen    => $gen_fault_injection_label,
405
            mode        => $settings_ref->{'design'}->{'OPTIMIZATIONS'},
406
        },
Christian Fibich's avatar
Christian Fibich committed
407
        $export_constraints_filename
408
409
    );

Christian Fibich's avatar
Christian Fibich committed
410
411
    # FIXME add constraint export to other tools

Christian Fibich's avatar
Christian Fibich committed
412
    if (defined $rv) {
413
414
        $logger->error($rv);
        return 1;
415
416
    }

417
    $logger->debug("=== Stopping execution ===");
418
419
    return 0;
}
420

Christian Fibich's avatar
Christian Fibich committed
421
sub usage {
Christian Fibich's avatar
Christian Fibich committed
422
    my ($err) = @_;
423
    my $msg = <<USAGE;
Christian Fibich's avatar
Christian Fibich committed
424
425
426
427
Usage: $0 [PARAMETERS]

Required parameters:

428
-s, --fiji-settings-file=<filename>   the cfg file with FIJI settings
Christian Fibich's avatar
Christian Fibich committed
429

430
-n, --netlist-file=<filename>         the netlist file to instrument
Christian Fibich's avatar
Christian Fibich committed
431
432
433

Optional parameters:

434
-p, --file-prefix=<prefix>            prefix for all generated files
Christian Fibich's avatar
Christian Fibich committed
435
436
                                        otherwise: toplevel module name

437
-o, --output-dir=<path>               the directory where the generated files are put
Christian Fibich's avatar
Christian Fibich committed
438
439
440
441
                                        the directory will be created if necessary
                                        overrules the directory specified in
                                          the fiji settings file
                                        otherwise: current working directory
Christian Fibich's avatar
Christian Fibich committed
442
443
444
445

--help                          display this help and exit
USAGE

Christian Fibich's avatar
Christian Fibich committed
446
    if ($err == 0) {
Christian Fibich's avatar
Christian Fibich committed
447
448
449
450
451
452
453
454
        print STDOUT $msg;
        return 0;
    } else {
        print STDERR $msg;
        return -1;
    }
}

455
456
457
my $log_conf = File::Spec->catfile($FindBin::Bin, 'logger.conf');
Log::Log4perl::init_and_watch($log_conf, 'HUP');

458
my $logger = get_logger("");
459
my $break  = undef;
460
461
462
463

# ARGV gets modified by GetOptions below but we need it later in log_start (again)
@argv=@ARGV;
log_start();
464

Christian Fibich's avatar
Christian Fibich committed
465
# Defaults
466
467
468
469
470
471
my $options = {
    fiji_settings_file => undef,
    netlist_file       => undef,
    file_prefix        => undef,
    output_dir         => undef
};
472

473
GetOptions(
474
475
476
477
    "fiji-settings-file|s=s" => \$options->{'fiji_settings_file'},
    "netlist-file|n=s"       => \$options->{'netlist_file'},
    "file-prefix|p=s"        => \$options->{'file_prefix'},
    "output-dir|o=s"         => \$options->{'output_dir'},
Christian Fibich's avatar
Christian Fibich committed
478
    "help|h"                 => sub { exit(usage(0)); },
479
);
480

Christian Fibich's avatar
Christian Fibich committed
481
# Check if all required options present
Christian Fibich's avatar
Christian Fibich committed
482
483
for my $k (keys(%{$options})) {
    if (!defined $options->{$k}) {
Christian Fibich's avatar
Christian Fibich committed
484
        if ($k eq 'output_dir' || $k eq 'file_prefix') {
485
486
487
488
489
490
491
        } else {
            $break = 1;
            $logger->error("Option $k is required.");
        }
    }
}

Christian Fibich's avatar
Christian Fibich committed
492
exit(usage(1)) if (defined $break);
Christian Fibich's avatar
Christian Fibich committed
493
494

# clean up the passed paths
Christian Fibich's avatar
Christian Fibich committed
495
496
497
498
# @FIXME needed ?
$options->{'fiji_settings_file'} = File::Spec->canonpath($options->{'fiji_settings_file'});
$options->{'netlist_file'}       = File::Spec->canonpath($options->{'netlist_file'});
$options->{'output_dir'}         = File::Spec->canonpath($options->{'output_dir'}) if defined $options->{'output_dir'};
499
500

exit main($options);