Settings.pm 27.9 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
#-------------------------------------------------------------------------------
#  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:              Settings.pm
#  Created on:        16.02.2015
#  $LastChangedBy$
#  $LastChangedDate$
#
#  Description:
#
# Contains helper functions to deal with FIJI Settings files.
#-------------------------------------------------------------------------------

Christian Fibich's avatar
Christian Fibich committed
21
22
## @file Settings.pm
# @brief Contains class \ref FIJI::Settings
23

24
## @class FIJI::Settings
25
#
Christian Fibich's avatar
Christian Fibich committed
26
# @brief Contains helper functions to deal with FIJI Settings files.
27
28
package FIJI::Settings;

29
30
31
use strict;
use warnings;

32
use Scalar::Util 'blessed';
33
34
35
use Log::Log4perl qw(get_logger);
use Scalar::Util "looks_like_number";
use Config::Simple;
36
37
38
use Data::Dumper;
use POSIX qw(ceil);
use List::Util qw(max);
39
use File::Spec;
40

41
42
use FIJI qw(:all);

Christian Fibich's avatar
Christian Fibich committed
43
## @var @base_resources stores the resource count for default config
Christian Fibich's avatar
Christian Fibich committed
44
45
my @base_resources;

46
## @function public new ($phase, $fiji_ini_file, $existing_settings)
Stefan Tauner's avatar
Stefan Tauner committed
47
48
# Create a new settings instance.
#
Christian Fibich's avatar
Christian Fibich committed
49
50
51
# \param phase Tool flow phase the settings need to be compatible with.
# \param fiji_ini_file (optional) Path to the configuration file to read.
# \param existing_settings (optional) A reference to reuse and return
52
#        with values read from file. Any contained data will be cleared.
53
54
# \returns The new settings instance or a string describing the reason
#          why it could not be created.
55
sub new ($;$$) {
56
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
57
    my ($class, $phase, $fiji_ini_file, $existing_settings) = @_;
58
59
60
61

    my $fiji_settings_ref;

    # if there is no existing settings instance yet, create one
Christian Fibich's avatar
Christian Fibich committed
62
    if (!defined($existing_settings)) {
63
        $fiji_settings_ref = {};
Christian Fibich's avatar
Christian Fibich committed
64
        $fiji_settings_ref = bless($fiji_settings_ref, $class);
65
    } else {
66
67
        $fiji_settings_ref = $existing_settings;
    }
Christian Fibich's avatar
Christian Fibich committed
68
    if (!blessed($fiji_settings_ref) || !$fiji_settings_ref->isa("FIJI::Settings")) {
69
        my $msg;
Christian Fibich's avatar
Christian Fibich committed
70
        if (!defined($existing_settings)) {
71
72
73
74
75
76
77
78
79
            $msg = "Could not create FIJI::Settings instance.";
        } else {
            $msg = "Given settings are not of type FIJI::Settings.";
        }
        $logger->error($msg);
        return $msg;
    }

    # If there is a file given, try to read it. Else just create a default instance.
Christian Fibich's avatar
Christian Fibich committed
80
81
82
    if (defined($fiji_ini_file)) {
        $fiji_settings_ref = read_settingsfile($phase, $fiji_ini_file, $fiji_settings_ref);
        if (!ref($fiji_settings_ref)) {
83
84
            return $fiji_settings_ref;    # actually an error message
        }
85
        $fiji_settings_ref->{'filename'} = $fiji_ini_file;
86
87
    } else {
        $fiji_settings_ref->{'design'} = {};
Christian Fibich's avatar
Christian Fibich committed
88
        _set_defaults(DESIGNMAP, $fiji_settings_ref->{'design'}, $phase);
89
        $fiji_settings_ref->{'fius'}     = [];
90
        $fiji_settings_ref->{'filename'} = File::Spec->curdir();
91
    }
Stefan Tauner's avatar
Stefan Tauner committed
92

Christian Fibich's avatar
Christian Fibich committed
93
    @base_resources = _est_resources(DESIGNMAP->{'FREQUENCY'}->{'default'}, DESIGNMAP->{'BAUDRATE'}->{'default'}, DESIGNMAP->{'TIMER_WIDTH'}->{'default'}, DESIGNMAP->{'RESET_DUT_IN_DURATION'}->{'default'}, DESIGNMAP->{'LFSR_WIDTH'}->{'default'}, 0, "logarithmic");
94
95
96

    return $fiji_settings_ref;
}
Stefan Tauner's avatar
Stefan Tauner committed
97
98

sub _export_value {
99
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
100
    my ($map_ref, $k, $v_ref) = @_;
101

Christian Fibich's avatar
Christian Fibich committed
102
    if (defined($map_ref->{$k}->{'type'})) {
103
        my $orig = ${$v_ref};
Christian Fibich's avatar
Christian Fibich committed
104
105
        if ($map_ref->{$k}->{'type'} eq 'hexadecimal') {
            ${$v_ref} = sprintf("0x%x", $orig);
106
107
108

            # } elsif ($map_ref->{$k}->{'type'} eq 'natural') {
            # } elsif ($map_ref->{$k}->{'type'} eq 'boolean') {
Christian Fibich's avatar
Christian Fibich committed
109
            $logger->trace("Converted value of $k (\"$orig\") to \"${$v_ref}\".") if ($orig ne ${$v_ref});
110
111
112
113
        }

        # } elsif (defined($map_ref->{$k}->{'values'})) {
    }
114
115
}

116
## @method public save ($fiji_ini_file)
117
118
# @brief Store contained FIJI Settings to file.
#
Christian Fibich's avatar
Christian Fibich committed
119
# @attention Will happily overwrite existing files!
120
#
121
# @param fiji_ini_file The file name to write the FIJI Settings to.
Stefan Tauner's avatar
Stefan Tauner committed
122
sub save ($) {
123
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
124
    my ($self, $fiji_ini_file) = @_;
125
126
    return "No file name given" if !defined($fiji_ini_file);

Christian Fibich's avatar
Christian Fibich committed
127
    my $fiji_ini = new Config::Simple(syntax => 'ini');
128
129
    my $design_ref;
    my $fiu_cnt = 0;
Christian Fibich's avatar
Christian Fibich committed
130
    foreach my $key (keys %{$self}) {
131
        my $val = $self->{$key};
Christian Fibich's avatar
Christian Fibich committed
132
133
134
        if (ref(\$val) eq "REF") {
            if (ref($val) eq "HASH") {
                if ($key eq "design") {
135
136
137
                    $design_ref = $val;
                    next;
                }
Christian Fibich's avatar
Christian Fibich committed
138
139
140
            } elsif (ref($val) eq "ARRAY") {
                if ($key eq "fius") {
                    foreach my $fiu (@{$val}) {
141
142
                        my $ini_fiu;

Christian Fibich's avatar
Christian Fibich committed
143
                        foreach my $k (keys(%{$fiu})) {
144
                            my $ini_name = FIUMAP->{$k}->{'ini_name'};
Christian Fibich's avatar
Christian Fibich committed
145
                            if (!defined($fiu->{$k})) {
146
147
148
149
150
151
152
153
                                $logger->debug("Skip saving undefined value of FIU constant with key $ini_name.");
                                next;
                            }

                            # Copy value to new hash with external naming.
                            $ini_fiu->{$ini_name} = $fiu->{$k};

                            # Convert value to external representation
Christian Fibich's avatar
Christian Fibich committed
154
155
                            _export_value(FIUMAP, $k, \$ini_fiu->{$ini_name});
                            $logger->trace(sprintf("Exporting FIU%d setting %s -> %s (%s -> %s).", $fiu_cnt, $k, $ini_name, defined($fiu->{$k}) ? $fiu->{$k} : "undef", defined($ini_fiu->{$ini_name}) ? $ini_fiu->{$ini_name} : "undef"),);
156
                        }
Christian Fibich's avatar
Christian Fibich committed
157
                        $fiji_ini->set_block("FIU" . $fiu_cnt++, $ini_fiu);
158
159
160
                    }
                    next;
                }
161
            }
162
163
        } elsif ($key eq "filename") {
            next;
164
        }
165
166

        my $err = "Unknown element found in FIJI Settings: key: \"$key\" val: \"$val\"";
167
168
169
170
171
        $logger->error($err);
        return $err;
    }
    $design_ref->{'FIU_NUM'} = $fiu_cnt;
    my $ini_design;
Christian Fibich's avatar
Christian Fibich committed
172
    foreach my $k (keys(%{$design_ref})) {
173
        my $ini_name = DESIGNMAP->{$k}->{'ini_name'};
Christian Fibich's avatar
Christian Fibich committed
174
        if (!defined($design_ref->{$k})) {
175
176
177
178
179
180
            $logger->debug("Skip saving undefined value of design constant with key $ini_name.");
            next;
        }

        # Copy value to new hash with external naming.
        $ini_design->{$ini_name} = $design_ref->{$k};
181

182
        # Convert value to external representation
Christian Fibich's avatar
Christian Fibich committed
183
184
        _export_value(DESIGNMAP, $k, \$ini_design->{$ini_name});
        $logger->trace(sprintf("Exporting Design setting %s -> %s (%s -> %s).", $k, $ini_name, defined($design_ref->{$k}) ? $design_ref->{$k} : "undef", defined($ini_design->{$ini_name}) ? $ini_design->{$ini_name} : "undef"),);
185
    }
Christian Fibich's avatar
Christian Fibich committed
186
    $fiji_ini->set_block("CONSTS", $ini_design);
187

Christian Fibich's avatar
Christian Fibich committed
188
    if (!defined($fiji_ini->write($fiji_ini_file))) {
189
190
191
192
        my $err = Config::Simple->error();
        $logger->error($err);
        return $err;
    }
193
    $self->{'filename'} = $fiji_ini_file;
194
195
    return undef;
}
196

197
## @function public read_settingsfile ($phase, $fiji_ini_file)
198
# @brief Load the FIJI Settings file containing design and FIU constants.
199
#
200
# @param phase  Tool flow phase the settings stored in the given file
Stefan Tauner's avatar
Stefan Tauner committed
201
#               need to be compatible with.
202
# @param fiji_ini_file The name of an .ini file with FIJI Settings:
203
#         - a 'consts' block containing the constants specified by
Stefan Tauner's avatar
Stefan Tauner committed
204
#           \ref _sanitize_design.
205
206
207
#         - at least one FIU block named "FIU<number>" where "<number>"
#           is a strictly increasing integer starting with 0 containing
#           the constants for the respective FIU, see \ref _sanitize_fiu
208
#
209
# @returns a reference to the hash containing the read constants.
210
sub read_settingsfile ($$$) {
211
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
212
    my ($phase, $fiji_ini_file, $existing_settings) = @_;
213
214
    my $fiji_ini;
    eval { $fiji_ini = new Config::Simple($fiji_ini_file) };    # pesky library tries to die on syntax errors
Christian Fibich's avatar
Christian Fibich committed
215
    if (!defined($fiji_ini)) {
216
        my $submsg = defined($@) ? $@ : Config::Simple->error();
Christian Fibich's avatar
Christian Fibich committed
217
        if (length($submsg) == 0) {
218
219
220
221
222
223
224
225
            $submsg = "Empty file?";
        }
        my $msg = "Could not read config file \"$fiji_ini_file\": $submsg";
        $logger->error($msg);
        return $msg;
    }

    my $design_ref = $fiji_ini->get_block("CONSTS");
Christian Fibich's avatar
Christian Fibich committed
226
    if (!(%$design_ref)) {
227
228
229
230
        my $msg = "Could not fetch CONSTS block from config file \"$fiji_ini_file\"";
        $logger->error($msg);
        return $msg;
    }
Christian Fibich's avatar
Christian Fibich committed
231
    _set_defaults(DESIGNMAP, $design_ref, $phase);
232

Christian Fibich's avatar
Christian Fibich committed
233
234
    $design_ref = _rename_import(DESIGNMAP, $design_ref);
    if (!defined($design_ref)) {
235
236
237
238
239
240
241
        my $msg = "Design constants do not match the FIJI Settings naming scheme.";
        $logger->error($msg);
        return $msg;
    }

    # Create a new instance or reuse the shallow hull of the existing one
    my $fiji_settings_ref;
Christian Fibich's avatar
Christian Fibich committed
242
    if (!defined($existing_settings)) {
243
        $fiji_settings_ref = {};
Christian Fibich's avatar
Christian Fibich committed
244
        bless($fiji_settings_ref, "FIJI::Settings");
Stefan Tauner's avatar
Stefan Tauner committed
245
    } else {
246
247
248
        $fiji_settings_ref = $existing_settings;

        # Clear the hash
Christian Fibich's avatar
Christian Fibich committed
249
        for (keys %$fiji_settings_ref) {
250
251
252
            delete $fiji_settings_ref->{$_};
        }
    }
Christian Fibich's avatar
Christian Fibich committed
253
    if (!blessed($fiji_settings_ref) || !$fiji_settings_ref->isa("FIJI::Settings")) {
254
        my $msg;
Christian Fibich's avatar
Christian Fibich committed
255
        if (!defined($existing_settings)) {
256
257
258
259
260
261
262
263
264
265
266
            $msg = "Could not create FIJI::Settings instance.";
        } else {
            $msg = "Given settings are not of type FIJI::Settings.";
        }
        $logger->error($msg);
        return $msg;
    }
    $fiji_settings_ref->{'design'} = $design_ref;
    $fiji_settings_ref->{'fius'}   = [];

    # sanitize and validate read design constants
Christian Fibich's avatar
Christian Fibich committed
267
268
    $design_ref = _sanitize_design($design_ref, $phase);
    if (!ref($design_ref)) {
269
270
271
272
273
274
275
276
277
278
        $logger->error($design_ref);
        return $design_ref;
    }

    my $fiu_num = 0;

    # loop over all read fius
    while (1) {
        my $fiu_name = "FIU" . $fiu_num;
        my $fiu_ref  = $fiji_ini->get_block($fiu_name);
Christian Fibich's avatar
Christian Fibich committed
279
        if (!(%$fiu_ref)) {
280
281
            last;
        }
Christian Fibich's avatar
Christian Fibich committed
282
283
        $fiu_ref = _rename_import(FIUMAP, $fiu_ref);
        if (!defined($design_ref)) {
284
285
286
287
288
289
            my $msg = "FIU constants of $fiu_name do not match the FIJI Settings naming scheme.";
            $logger->error($msg);
            return $msg;
        }

        my $tmp_fiu = {};
Christian Fibich's avatar
Christian Fibich committed
290
        _set_defaults(FIUMAP, $tmp_fiu, $phase);
291
292

        # overwrite default entries
Christian Fibich's avatar
Christian Fibich committed
293
        foreach my $k (keys(%{$fiu_ref})) {
294
295
296
297
            $tmp_fiu->{$k} = $fiu_ref->{$k};
        }
        $fiu_ref = $tmp_fiu;

Christian Fibich's avatar
Christian Fibich committed
298
299
        $fiu_ref = _sanitize_fiu($fiu_ref, $phase);
        if (!ref($fiu_ref)) {
300
301
302
303
304
305
306
307
308
            my $msg = "(Some) constants for $fiu_name in FIJI Settings are invalid.";
            $logger->error($msg);
            return $msg;
        }
        $fiji_settings_ref->{'fius'}->[$fiu_num] = $fiu_ref;
        $fiu_num++;
        $logger->trace("Read in $fiu_name from FIJI Settings file successfully.");
    }

Christian Fibich's avatar
Christian Fibich committed
309
    if ($fiu_num == 0) {
310
311
312
313
314
        $logger->debug("Could not fetch any FIU blocks from config file \"$fiji_ini_file\"");
    }

    # FIU_NUM is optional in the Settings file... if it was set check that
    # it corresponds to the number of FIU<number> blocks.
Christian Fibich's avatar
Christian Fibich committed
315
    if (defined($design_ref->{'FIU_NUM'}) && $design_ref->{'FIU_NUM'} != $fiu_num) {
316
317
318
319
320
321
322
        my $msg = FIU_NUM->{'ini_name'} . " does not match the numbers of FIU blocks found.";
        $logger->error($msg);
        return $msg;
    } else {
        $design_ref->{'FIU_NUM'} = $fiu_num;    # assume the best if FIU_NUM constant is not given
    }

Christian Fibich's avatar
Christian Fibich committed
323
    splice(@{$fiji_settings_ref->{'fius'}}, $fiu_num);
324
325
    $logger->info("Successfully read in design constants and $fiu_num FIU definitions from FIJI Settings file \"$fiji_ini_file\".");
    return $fiji_settings_ref;
326
327
}

328
## @method public set_fiu_defaults (%$fiu_ref)
329
330
331
# @brief Overwrite existing fields (if defined) with defaults defined in FIJI.pm.
#
sub set_fiu_defaults ($) {
Christian Fibich's avatar
Christian Fibich committed
332
333
    my ($self, $consts_ref) = @_;
    return _set_defaults(FIUMAP, $consts_ref);
Stefan Tauner's avatar
Stefan Tauner committed
334
335
336
337
338
}

## @function _set_defaults (%$map_ref, %$consts_ref)
# @brief Set defaults according to FIJI.pm.
sub _set_defaults {
339
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
340
341
342
    my ($map_ref, $consts_ref, $phase) = @_;
    foreach my $k (keys(%{$map_ref})) {
        if (exists($map_ref->{$k}->{'default'})) {
343

344
            # Set default only if it is not mandatory in the given phase.
Christian Fibich's avatar
Christian Fibich committed
345
            if (defined($phase) && scalar(grep { $_ eq $phase } @{$map_ref->{$k}->{'phases_opt'}}) == 0) {
346
347
348
349
                next;
            }

            # Also, do not overwrite existing values
Christian Fibich's avatar
Christian Fibich committed
350
            if (defined($consts_ref->{$k})) {
351
352
353
                next;
            }
            $consts_ref->{$k} = $map_ref->{$k}->{default};
Christian Fibich's avatar
Christian Fibich committed
354
            $logger->trace(sprintf("Adding default constant: %s (%s) = %s.", $k, $map_ref->{$k}->{'ini_name'}, $map_ref->{$k}->{default}));
355
356
357
        }
    }
}
358

Stefan Tauner's avatar
Stefan Tauner committed
359
sub validate_design_value {
Christian Fibich's avatar
Christian Fibich committed
360
361
    my ($k, $v_ref) = @_;
    return validate_value(DESIGNMAP, $k, $v_ref);
Stefan Tauner's avatar
Stefan Tauner committed
362
363
364
}

sub validate_fiu_value {
Christian Fibich's avatar
Christian Fibich committed
365
366
    my ($k, $v_ref) = @_;
    return validate_value(FIUMAP, $k, $v_ref);
Stefan Tauner's avatar
Stefan Tauner committed
367
368
}

369
370
371
372
373
374
375
376
377
378
## @function validate_value (%$map_ref, $k, $$v_ref, $old, $log_func)
#   Do validation (and conversation from external->internal representation)
#
#   \param map_ref  reference to FIJI Settings mappings
#   \param k        key identifying the respective setting
#   \param v_ref    scalar reference to the proposed value (that may be modified)
#   \param old      (optional) previously valid value
#   \param log_func (optional) the (log4perl) log function to use
#                   (defaul is \&Log::Log4perl::Logger::trace)
sub validate_value ($$$;$$) {
379
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
380
    my ($map_ref, $k, $v_ref, $old, $log_func) = @_;
381
    $log_func = \&Log::Log4perl::Logger::trace if !defined($log_func);
Christian Fibich's avatar
Christian Fibich committed
382
    if (defined($map_ref->{$k}->{'type'})) {
383
384
        my $orig = ${$v_ref};

Christian Fibich's avatar
Christian Fibich committed
385
        # @FIXME: check if the constant is depending on another constant, that needs to be enabled.
386
387
388
389
        # my $dependency = @{$map_ref->{$k}->{'depends_on'}};
        # if (defined($dependency) && defined(@{$map_ref->{$dependency}->{'value'}}
        # }

Christian Fibich's avatar
Christian Fibich committed
390
        if ($map_ref->{$k}->{'type'} eq 'net') {
391

Christian Fibich's avatar
Christian Fibich committed
392
            # @TODO: check syntax? there is nothing more to do in here unless we supply allowed nets...
393
            # which is actually possible because the input netlist is stored in the settings as well
Christian Fibich's avatar
Christian Fibich committed
394
395
396
        } elsif ($map_ref->{$k}->{'type'} eq 'hexadecimal') {
            if ($orig !~ /^(0|(0[xX][[:xdigit:]]+))$/) {
                $log_func->($logger, "$k: $orig does not look like a natural hexadecimal number.");
397
398
399
400
401
                return 0;
            }
            ${$v_ref} = hex($orig);

            # Check for natural value range. Should never trigger due to the regex above.
Christian Fibich's avatar
Christian Fibich committed
402
403
            if (${$v_ref} < 0) {
                $log_func->($logger, "$k: $orig is negative.");
404
405
                return 0;
            }
Christian Fibich's avatar
Christian Fibich committed
406
        } elsif ($map_ref->{$k}->{'type'} eq 'natural') {
407
408

            # Match hexadecimal, binary, octal and decimal numbers (the latter also in scientific notation)
Christian Fibich's avatar
Christian Fibich committed
409
410
            if ($orig !~ /^(0|(0(x[0-9a-fA-F]+|[0-7]+))|(0b[01]+)|([1-9][0-9]*(e(-[0-9])?[0-9]+)?))$/) {
                $log_func->($logger, "$k: $orig does not look like a number.");
411
412
413
414
415
                return 0;
            }

            # convert non-decimal (hexadecimal, binary, octal) values to decimal
            ${$v_ref} = oct($orig) if $orig =~ /^0/;
Christian Fibich's avatar
Christian Fibich committed
416
417
            if (!looks_like_number(${$v_ref})) {
                $log_func->($logger, "$k: $orig does not look like a number.");
418
419
                return 0;
            }
Stefan Tauner's avatar
Stefan Tauner committed
420

421
            # Check for natural value range. Should never trigger due to the regex above.
Christian Fibich's avatar
Christian Fibich committed
422
423
            if (${$v_ref} < 0) {
                $log_func->($logger, "$k: $orig is negative.");
424
425
                return 0;
            }
Christian Fibich's avatar
Christian Fibich committed
426
        } elsif ($map_ref->{$k}->{'type'} eq 'boolean') {
427
428

            # convert strings to binary if need be
Christian Fibich's avatar
Christian Fibich committed
429
430
            if (!defined($orig)) {
                $log_func->($logger, "$k: \"undef\" is not a boolean value.");
431
                return 0;
Christian Fibich's avatar
Christian Fibich committed
432
            } elsif (lc($orig) eq 'true') {
433
                $orig = 1;
Christian Fibich's avatar
Christian Fibich committed
434
            } elsif (lc($orig) eq 'false') {
435
436
                $orig = 0;
            }
Christian Fibich's avatar
Christian Fibich committed
437
438
            if (($orig ne '0') && ($orig ne '1')) {
                $log_func->($logger, "$k: \"$orig\" does not look like a boolean value.");
439
440
441
442
                return 0;
            }

            # ensure proper boolean value, i.e. 0 or 1
Christian Fibich's avatar
Christian Fibich committed
443
            ${$v_ref} = (!!$orig) ? 1 : 0;
444
        }
Christian Fibich's avatar
Christian Fibich committed
445
        $logger->trace("Converted value of $k (\"$orig\") to \"${$v_ref}\".") if (defined($orig) && $orig ne ${$v_ref});
446
447
    }

Christian Fibich's avatar
Christian Fibich committed
448
    if (defined($map_ref->{$k}->{'values'})) {
449
        my $values_ref = $map_ref->{$k}->{'values'};
Christian Fibich's avatar
Christian Fibich committed
450
        if (ref($values_ref) eq 'ARRAY') {
451
452

            # Look for given value in allowed values
Christian Fibich's avatar
Christian Fibich committed
453
454
            if (scalar(grep { $_ eq ${$v_ref} } @{$values_ref}) == 0) {
                $log_func->($logger, "$k: ${$v_ref} is not allowed. Allowed values are: " . join(", ", @{$map_ref->{$k}->{'values'}}));
455
456
                return 0;
            }
Christian Fibich's avatar
Christian Fibich committed
457
458
459
        } elsif (ref($values_ref) eq 'CODE') {
            if (!$values_ref->(${$v_ref}, $old)) {
                $log_func->($logger, "$k: ${$v_ref} is not allowed.");
460
461
462
463
464
465
                return 0;
            }
        }
    }
    return 1;
}
Stefan Tauner's avatar
Stefan Tauner committed
466
467
468
469
470
471
472
473
474
475
476

# @function _rename_import (%$map_ref, %$consts_ref)
# Rename and convert entries in consts_ref according to map_ref.
#
# This function takes a hash of FIJI Settings and converts its contents
# to the respective internal representation. This allows to use different
# names and value representations in the external file than within the
# implementation.
#
# \returns $consts_ref, or undef on errors
sub _rename_import {
477
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
478
479
    my ($map_ref, $consts_ref) = @_;
    if (ref($consts_ref) ne 'HASH') {
480
481
482
        $logger->error("Parameter is not a reference to a hash (containing design constants).");
        return undef;
    }
483

484
485
    # Iterating over respective hash from FIJI.pm and rename the entries
    # to match our internal naming scheme.
Christian Fibich's avatar
Christian Fibich committed
486
    foreach my $k (keys(%{$map_ref})) {
487
        my $ini_name = $map_ref->{$k}->{'ini_name'};
Christian Fibich's avatar
Christian Fibich committed
488
        if (exists($consts_ref->{$ini_name}) && $ini_name ne $k) {
489
            $consts_ref->{$k} = $consts_ref->{$ini_name};
Christian Fibich's avatar
Christian Fibich committed
490
            $logger->trace(sprintf("Renaming Design setting %s -> %s (=%s).", $ini_name, $k, defined($consts_ref->{$ini_name}) ? $consts_ref->{$ini_name} : "undef"),);
491
492
493
            delete $consts_ref->{$ini_name};
        }
    }
Christian Fibich's avatar
Christian Fibich committed
494
495
496
    foreach my $entry_key (keys(%{$consts_ref})) {
        if (!exists($map_ref->{$entry_key})) {
            $logger->debug("Deleting unknown setting %s = %s.", $entry_key, $consts_ref->{$entry_key});
497
498
499
500
501
502
            delete $consts_ref->{$entry_key};
        }
    }

    return $consts_ref;
}
Stefan Tauner's avatar
Stefan Tauner committed
503

504
## @function _sanitize_fiu (%$fiu_ref, $phase)
505
506
# @brief Convert and sanity check FIJI Settings.
#
507
508
# \param fiu_ref a reference to a hash containing FIJI Settings for a
#                single FIU.
Christian Fibich's avatar
Christian Fibich committed
509
# \param phase   the current design phase
510
511
512
#
# \returns A new hash with all constants required in the FIU settings
#          in sanitized form, or undef on errors.
513
sub _sanitize_fiu {
514
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
515
516
    my ($fiu_ref, $phase) = @_;
    if (ref($fiu_ref) ne 'HASH') {
517
518
519
520
        my $msg = "Parameter is not a reference to a hash (containing FIU constants).";
        $logger->error($msg);
        return $msg;
    }
521

Christian Fibich's avatar
Christian Fibich committed
522
523
    $fiu_ref = _validate_hashmap(FIUMAP, $fiu_ref, $phase);
    if (!ref($fiu_ref)) {
524
525
526
        $logger->error("Could not validate Design Constants.");
        return $fiu_ref;
    }
Stefan Tauner's avatar
Stefan Tauner committed
527

528
    return $fiu_ref;
529
530
}

531
sub _disabled_via_dependency {
Christian Fibich's avatar
Christian Fibich committed
532
    my ($map_ref, $consts_ref, $k) = @_;
533
534
535
    my $dependency = $map_ref->{$k}->{'depends_on'};
    return defined($dependency) && !$consts_ref->{$dependency};
}
536

Christian Fibich's avatar
Christian Fibich committed
537
sub _validate_hashmap {
538
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
539
540
541
    my ($map_ref, $consts_ref, $phase) = @_;
    my @map_keys = keys(%{$map_ref});
    foreach my $entry_key (keys(%{$consts_ref})) {
542
543
544
        my $forbidden_by = $map_ref->{$entry_key}->{'forbidden_by'};
        my $ini_name     = $map_ref->{$entry_key}->{'ini_name'};

Christian Fibich's avatar
Christian Fibich committed
545
546
        if (!exists($map_ref->{$entry_key})) {
            $logger->debug("Deleting unknown setting %s = %s.", $entry_key, $consts_ref->{$entry_key});
547
548
            next;
        }
Stefan Tauner's avatar
Stefan Tauner committed
549

550
        @map_keys = grep { $_ ne $entry_key } @map_keys;    # mark constant key as done
Christian Fibich's avatar
Christian Fibich committed
551
552
553
554
        if (_disabled_via_dependency($map_ref, $consts_ref, $entry_key)) {
            $logger->debug(sprintf("Key %s is disabled via %s. Skipping validation.", $entry_key, $map_ref->{$entry_key}->{'depends_on'}));
        } elsif (!validate_value($map_ref, $entry_key, \$consts_ref->{$entry_key})) {
            my $msg = sprintf("%s = %s is invalid.", $entry_key, !defined($consts_ref->{$entry_key}) ? "<undef>" : $consts_ref->{$entry_key});
555
556
557
558
            $logger->error($msg);
            return $msg;
        }

Christian Fibich's avatar
Christian Fibich committed
559
        if (defined($forbidden_by) && $consts_ref->{$entry_key} && $consts_ref->{$forbidden_by}) {
560
561
562
563
            my $msg = "$entry_key is forbidden when $forbidden_by is enabled";
            $logger->error($msg);
            return $msg;
        }
Stefan Tauner's avatar
Stefan Tauner committed
564
    }
565

Christian Fibich's avatar
Christian Fibich committed
566
    if (!defined($phase)) {
567

568
569
570
571
572
        # If there is no phase we are creating an "empty" hash that has
        # all possible defaults set earlier already and the there is
        # nothing we can or should do here.
        return $consts_ref;
    }
573

574
575
576
577
578
    # Iterate over the constants defined in FIJI.pm that apparently are
    # not contained in $consts_ref.
    foreach my $k (@map_keys) {
        my $dependency = $map_ref->{$k}->{'depends_on'};
        if (
Christian Fibich's avatar
Christian Fibich committed
579
580
581
            scalar(grep { $_ eq $phase } @{$map_ref->{$k}->{'phases_opt'}}) == 0 &&    # mandatory in current phase
            !_disabled_via_dependency($map_ref, $consts_ref, $k)                       # no dependency or dependency is enabled
           )
582
583
584
585
586
587
588
        {
            my $msg = "$k is mandatory in phase $phase.";
            $logger->error($msg);
            return $msg;
        }

    }
Stefan Tauner's avatar
Stefan Tauner committed
589

Christian Fibich's avatar
Christian Fibich committed
590
    # @TODO: implement the same mechanism with forbidden_by
591
592
593

    return $consts_ref;
}
Stefan Tauner's avatar
Stefan Tauner committed
594

595
## @function _sanitize_design (%$consts_ref, $phase)
Stefan Tauner's avatar
Stefan Tauner committed
596
# @brief Sanity check FIJI Design Settings.
597
#
Stefan Tauner's avatar
Stefan Tauner committed
598
# The function deals with sanity checks for the values
599
# themselves. It checks for the following conditions:
600
601
602
#
#   - FIU_NUM: > 0
#   - FIU_CFG_BITS: > 0
603
#   - TIMER_WIDTH: > 0, <= 16
604
605
606
#   - ID: > 0, < 2^15-1
#   - BAUDRATE: > 0
#
607
# \param consts_ref a reference to a hash containing some design settings
Stefan Tauner's avatar
Stefan Tauner committed
608
# \param phase the tool flow phase that defines the rules to check against
609
#
Stefan Tauner's avatar
Stefan Tauner committed
610
# \returns The given hash with all constants required in the design
611
#          settings in sanitized form, or an error message.
Stefan Tauner's avatar
Stefan Tauner committed
612
sub _sanitize_design {
613
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
614
615
    my ($consts_ref, $phase) = @_;
    if (ref($consts_ref) ne 'HASH') {
616
617
618
619
620
621
        my $msg = "Parameter is not a reference to a hash (containing design constants).";
        $logger->error($msg);
        return $msg;
    }

    # check for sane values
Christian Fibich's avatar
Christian Fibich committed
622
623
    $consts_ref = _validate_hashmap(DESIGNMAP, $consts_ref, $phase);
    if (!ref($consts_ref)) {
624
625
626
627
628
        my $msg = "Could not validate Design Constants.";
        $logger->error($msg);
        return "$msg $consts_ref";
    }

Christian Fibich's avatar
Christian Fibich committed
629
    if (($consts_ref->{'FIU_CFG_BITS'} <= 0)) {
630
631
632
633
        my $msg = "FIU_CFG_BITS is <= 0.";
        $logger->error($msg);
        return $msg;
    }
Christian Fibich's avatar
Christian Fibich committed
634
    if (($consts_ref->{'TIMER_WIDTH'} <= 0) || ($consts_ref->{'TIMER_WIDTH'} > 16)) {
635
636
637
638
        my $msg = "TIMER_WIDTH is invalid ($consts_ref->{'TIMER_WIDTH'}).";
        $logger->error($msg);
        return $msg;
    }
Christian Fibich's avatar
Christian Fibich committed
639
    if (defined($consts_ref->{'ID'}) && ($consts_ref->{ID} < 0 || $consts_ref->{ID} > (2**16 - 1))) {
640
641
642
643
        my $msg = "ID is invalid ($consts_ref->{ID}).";
        $logger->error($msg);
        return $msg;
    }
Christian Fibich's avatar
Christian Fibich committed
644
    if (($consts_ref->{'BAUDRATE'} <= 0)) {
645
646
647
648
649
650
        my $msg = "BAUDRATE missing is <= 0.";
        $logger->error($msg);
        return $msg;
    }

    return $consts_ref;
651
652
}

653
654
sub _log2 {
    my $val = shift;
Christian Fibich's avatar
Christian Fibich committed
655
    return ($val > 0) ? (log($val) / log(2)) : 0;
656
657
}

658
659
## @function private _est_resources ($FREQUENCY, $BAUD, $TIMER_WIDTH, $RESET_CYCLES, $LFSR_WIDTH, $FIU_NUM, $algo)
# @brief estimates the number of Registers and LUTs to implement FIJI logic with the given settings
Christian Fibich's avatar
Christian Fibich committed
660
#
661
662
663
664
665
# The function ant its parameters were determined by experiment & fitted by scipy.optimize.curve_fit
#
# @param FREQUENCY      the clock frequency the FIJI logic will run at in Hz
# @param BAUD           the baud rate for DUT <-> FIJI communication
# @param TIMER_WIDTH    the width of the injection timer (durations) in Bytes
Christian Fibich's avatar
Christian Fibich committed
666
# @param RESET_CYCLES   the number of cycles to apply a FIJI-to-DUT reset
667
# @param LFSR_WIDTH     width of the LFSR used for stuck-open emulation
Christian Fibich's avatar
Christian Fibich committed
668
# @param FIU_NUM        the number of FIUs in the configuration
669
670
671
#
# @returns ($registers, $lutsum)

Christian Fibich's avatar
Christian Fibich committed
672
sub _est_resources {
673
    my $logger = get_logger("");
674
    my ($FREQUENCY, $BAUD, $TIMER_WIDTH, $RESET_CYCLES, $LFSR_WIDTH, $FIU_NUM) = @_;
Christian Fibich's avatar
Christian Fibich committed
675

Christian Fibich's avatar
Christian Fibich committed
676
    # @FIXME where do we put these values? they are likely to change if the VHDL
677
678
    # source is changed...

Christian Fibich's avatar
Christian Fibich committed
679
680
681
    my $registers;
    my $lut6;

Christian Fibich's avatar
Christian Fibich committed
682
683
684
685
686
687
    $logger->debug("FREQUENCY $FREQUENCY out of range (1000000 - 500000000)") if ($FREQUENCY < 1000000 || $FREQUENCY > 500000000);
    $logger->debug("BAUD $BAUD out of range (9600 - 3000000)")                if ($BAUD < 9600         || $BAUD > 3000000);
    $logger->debug("TIMER_WIDTH $TIMER_WIDTH out of range (1 - 8)")           if ($TIMER_WIDTH < 1     || $TIMER_WIDTH > 8);
    $logger->debug("RESET_CYCLES $RESET_CYCLES out of range (1 - 16)")        if ($RESET_CYCLES < 1    || $RESET_CYCLES > 16);
    $logger->debug("LFSR_WIDTH $LFSR_WIDTH out of range (16 - 64)")           if ($LFSR_WIDTH < 16     || $LFSR_WIDTH > 64);
    $logger->debug("FIU_NUM $FIU_NUM out of range (1 - 64)")                  if ($FIU_NUM < 1         || $FIU_NUM > 64);
688

689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
    $registers = 8769408455.04;
    $registers += (3.16525787254e-08)*($FREQUENCY) + (-8063276676.79)*(2**(-1.60957863771e-17 * $FREQUENCY));
    $registers += (0.000329011902135)*($BAUD) + (-705457194.107)*(2**(7.38032560255e-13 * $BAUD));
    $registers += (86.8258719199)*($TIMER_WIDTH) + (-424084.540754)*(2**(0.000218517717305 * $TIMER_WIDTH));
    $registers += (5.02154556118)*($RESET_CYCLES) + (51167.4447102)*(2**(-0.000135580209031 * $RESET_CYCLES));
    $registers += (-5.30893252274)*($LFSR_WIDTH) + (-301970.077109)*(2**(-2.99010866996e-05 * $LFSR_WIDTH));
    $registers += (13.746880792)*($FIU_NUM) + (437.305954808)*(2**(-0.00583539664462 * $FIU_NUM));

    $lut6 = -1591399138.51;
    $lut6 += (5.65349933334e-06)*($FREQUENCY) + (-245548261.879)*(2**(3.26830064347e-14 * $FREQUENCY));
    $lut6 += (-0.0602788840594)*($BAUD) + (1868596655.23)*(2**(4.65257746085e-11 * $BAUD));
    $lut6 += (9.04974779295)*($TIMER_WIDTH) + (-123738.805411)*(2**(7.77721022021e-07 * $TIMER_WIDTH));
    $lut6 += (0.156780025973)*($RESET_CYCLES) + (-176988.018256)*(2**(1e-08 * $RESET_CYCLES));
    $lut6 += (-1.88257966999)*($LFSR_WIDTH) + (-31351130.663)*(2**(-8.63995764468e-08 * $LFSR_WIDTH));
    $lut6 += (3.95693187928)*($FIU_NUM) + (2718.76465806)*(2**(-0.000895867386597 * $FIU_NUM));

    return ($registers, $lut6);
Christian Fibich's avatar
Christian Fibich committed
706
707
}

708
709
## @function public estimate_resources ($settings_ref)
# @brief estimates the resources needed to implement the given settings
Christian Fibich's avatar
Christian Fibich committed
710
#
711
712
# @returns a hash with the members 'regs' and 'lut_calc'
#
713
sub estimate_resources {
714
    my $logger         = get_logger("");
715
    my ($settings_ref) = @_;
716
717
718
    my $consts_ref     = $settings_ref->{'design'};
    my $fiu_ref        = $settings_ref->{'fius'};
    my $fiu_num        = @{$fiu_ref};
Christian Fibich's avatar
Christian Fibich committed
719

Christian Fibich's avatar
Christian Fibich committed
720
    my @calcrv = _est_resources($consts_ref->{'FREQUENCY'}, $consts_ref->{'BAUDRATE'}, $consts_ref->{'TIMER_WIDTH'}, $consts_ref->{'RESET_DUT_IN_DURATION'}, $consts_ref->{'LFSR_WIDTH'}, $fiu_num, "logarithmic");
Christian Fibich's avatar
Christian Fibich committed
721

722
    my $rv = {
Christian Fibich's avatar
Christian Fibich committed
723
724
        regs     => sprintf("%.2f", $calcrv[0] / $base_resources[0]),
        lut_calc => sprintf("%.2f", $calcrv[1] / $base_resources[1]),
725
    };
Christian Fibich's avatar
Christian Fibich committed
726

727
    $logger->debug("ESTIMATE: current config will need Base*$rv->{'regs'} registers and Base*$rv->{'lut_calc'} combinational resources");
728
729
730
    return $rv;
}

731
1;