#------------------------------------------------------------------------------- # 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. #------------------------------------------------------------------------------- ## @file Settings.pm # @brief Contains class \ref FIJI::Settings ## @class FIJI::Settings # # @brief Contains helper functions to deal with FIJI Settings files. package FIJI::Settings; use strict; use warnings; use Scalar::Util 'blessed'; use Log::Log4perl qw(get_logger); use Scalar::Util "looks_like_number"; use Data::Dumper; use POSIX qw(ceil); use List::Util qw(max); use File::Spec; use FIJI::ConfigSorted; use FIJI qw(:all); ## @var @base_resources stores the resource count for default config my @base_resources; ## @function public new ($phase, $fiji_ini_file, $existing_settings) # Create a new settings instance. # # \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 # with values read from file. Any contained data will be cleared. # \returns The new settings instance (or undef in the case of an error) # and a diagnostic string describing the reason why it could not be created (undef if successful) sub new ($;$$) { my $logger = get_logger(""); my ($class, $phase, $fiji_ini_file, $existing_settings) = @_; my $fiji_settings_ref; # if there is no existing settings instance yet, create one if (!defined($existing_settings)) { $fiji_settings_ref = {}; $fiji_settings_ref = bless($fiji_settings_ref, $class); } else { $fiji_settings_ref = $existing_settings; } if (!blessed($fiji_settings_ref) || !$fiji_settings_ref->isa("FIJI::Settings")) { my $msg; if (!defined($existing_settings)) { $msg = "Could not create FIJI::Settings instance."; } else { $msg = "Given settings are not of type FIJI::Settings."; } $logger->error($msg); return (undef, $msg); } my $warn; # If there is a file given, try to read it. Else just create a default instance. if (defined($fiji_ini_file)) { ($fiji_settings_ref, $warn) = read_settingsfile($phase, $fiji_ini_file, $fiji_settings_ref); if (!defined($fiji_settings_ref)) { return ($fiji_settings_ref, $warn); } $fiji_settings_ref->{'filename'} = $fiji_ini_file; } else { $fiji_settings_ref->{'design'} = {}; _set_defaults(DESIGNMAP, $fiji_settings_ref->{'design'}, $phase); $fiji_settings_ref->{'fius'} = []; $fiji_settings_ref->{'filename'} = File::Spec->curdir(); } @base_resources = _est_resources(DESIGNMAP->{'FREQUENCY'}->{'default'}, DESIGNMAP->{'BAUDRATE'}->{'default'}, DESIGNMAP->{'TIMER_WIDTH'}->{'default'}, DESIGNMAP->{'RESET_DUT_IN_DURATION'}->{'default'}, DESIGNMAP->{'LFSR_WIDTH'}->{'default'}, 1, "logarithmic"); return ($fiji_settings_ref, $warn); } sub _export_value { my $logger = get_logger(""); my ($map_ref, $k, $v_ref) = @_; if (defined($map_ref->{$k}->{'type'})) { my $orig = ${$v_ref}; if ($map_ref->{$k}->{'type'} eq 'hexadecimal' || $map_ref->{$k}->{'type'} eq 'lfsrpoly') { ${$v_ref} = sprintf("0x%x", $orig); # } elsif ($map_ref->{$k}->{'type'} eq 'natural') { # } elsif ($map_ref->{$k}->{'type'} eq 'boolean') { $logger->trace("Converted value of $k (\"$orig\") to \"${$v_ref}\".") if ($orig ne ${$v_ref}); } elsif ($map_ref->{$k}->{'type'} eq 'net') { # Due to an annoying behavior of Config::Simple we have to enclose # escaped identifiers with quotes. These are necessary to preserve spaces. if (substr($orig, -1) eq ' ') { ${$v_ref} = "\"$orig\""; } } } } ## @method public save ($fiji_ini_file) # @brief Store contained FIJI Settings to file. # # @attention Will happily overwrite existing files! # # @param fiji_ini_file The file name to write the FIJI Settings to. sub save ($) { my $logger = get_logger(""); my ($self, $fiji_ini_file) = @_; return "No file name given" if !defined($fiji_ini_file); my $fiji_ini = new FIJI::ConfigSorted(syntax => 'ini'); my $design_ref; my $fiu_cnt = 0; foreach my $key (keys %{$self}) { my $val = $self->{$key}; if (ref(\$val) eq "REF") { if (ref($val) eq "HASH") { if ($key eq "design") { $design_ref = $val; next; } } elsif (ref($val) eq "ARRAY") { if ($key eq "fius") { foreach my $fiu (@{$val}) { my $ini_fiu; foreach my $k (keys(%{$fiu})) { my $ini_name = FIUMAP->{$k}->{'ini_name'}; if (!defined($fiu->{$k})) { $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 _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"),); } $fiji_ini->set_block("FIU" . $fiu_cnt++, $ini_fiu); } next; } } } elsif ($key eq "filename") { next; } my $err = "Unknown element found in FIJI Settings: key: \"$key\" val: \"$val\""; $logger->error($err); return $err; } $design_ref->{'FIU_NUM'} = $fiu_cnt; my $ini_design; foreach my $k (keys(%{$design_ref})) { my $ini_name = DESIGNMAP->{$k}->{'ini_name'}; if (!defined($design_ref->{$k})) { $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}; # Convert value to external representation _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"),); } $fiji_ini->set_block("CONSTS", $ini_design); if (!defined($fiji_ini->write($fiji_ini_file))) { my $err = FIJI::ConfigSorted->error(); $logger->error($err); return $err; } $self->{'filename'} = $fiji_ini_file; return undef; } ## @function public read_settingsfile ($phase, $fiji_ini_file) # @brief Load the FIJI Settings file containing design and FIU constants. # # @param phase Tool flow phase the settings stored in the given file # need to be compatible with. # @param fiji_ini_file The name of an .ini file with FIJI Settings: # - a 'consts' block containing the constants specified by # \ref _sanitize_design. # - at least one FIU block named "FIU" where "" # is a strictly increasing integer starting with 0 containing # the constants for the respective FIU, see \ref _sanitize_fiu # # @returns a reference to the hash containing the read constants (or undef in the case of an error) # and a diagnostic string describing the reason why it could not be created (undef if successful) sub read_settingsfile ($$$) { my $logger = get_logger(""); my ($phase, $fiji_ini_file, $existing_settings) = @_; my $fiji_ini; eval { $fiji_ini = new FIJI::ConfigSorted($fiji_ini_file) }; # pesky library tries to die on syntax errors if (!defined($fiji_ini)) { my $submsg = defined($@) ? $@ : FIJI::ConfigSorted->error(); if (length($submsg) == 0) { $submsg = "Empty file?"; } my $msg = "Could not read config file \"$fiji_ini_file\": $submsg"; $logger->error($msg); return (undef, $msg); } my $design_ref = $fiji_ini->get_block("CONSTS"); if (!(%$design_ref)) { my $msg = "Could not fetch CONSTS block from config file \"$fiji_ini_file\""; $logger->error($msg); return (undef, $msg); } _set_defaults(DESIGNMAP, $design_ref, $phase); $design_ref = _rename_import(DESIGNMAP, $design_ref); if (!defined($design_ref)) { my $msg = "Design constants do not match the FIJI Settings naming scheme."; $logger->error($msg); return (undef, $msg); } # Create a new instance or reuse the shallow hull of the existing one my $fiji_settings_ref; if (!defined($existing_settings)) { $fiji_settings_ref = {}; bless($fiji_settings_ref, "FIJI::Settings"); } else { $fiji_settings_ref = $existing_settings; # Clear the hash for (keys %$fiji_settings_ref) { delete $fiji_settings_ref->{$_}; } } if (!blessed($fiji_settings_ref) || !$fiji_settings_ref->isa("FIJI::Settings")) { my $msg; if (!defined($existing_settings)) { $msg = "Could not create FIJI::Settings instance."; } else { $msg = "Given settings are not of type FIJI::Settings."; } $logger->error($msg); return (undef, $msg); } $fiji_settings_ref->{'design'} = $design_ref; $fiji_settings_ref->{'fius'} = []; # sanitize and validate read design constants $design_ref = _sanitize_design($design_ref, $phase); if (!ref($design_ref)) { $logger->error($design_ref); return (undef, $design_ref); } my $fiu_num = 0; my $dup = {}; my $warn; # loop over all read fius while (1) { my $fiu_name = "FIU" . $fiu_num; my $fiu_ref = $fiji_ini->get_block($fiu_name); if (!(%$fiu_ref)) { last; } $fiu_ref = _rename_import(FIUMAP, $fiu_ref); if (!defined($design_ref)) { my $msg = "FIU constants of $fiu_name do not match the FIJI Settings naming scheme."; $logger->error($msg); return (undef, $msg); } my $tmp_fiu = {}; _set_defaults(FIUMAP, $tmp_fiu, $phase); # overwrite default entries foreach my $k (keys(%{$fiu_ref})) { $tmp_fiu->{$k} = $fiu_ref->{$k}; } $fiu_ref = $tmp_fiu; $fiu_ref = _sanitize_fiu($fiu_ref, $phase); if (!ref($fiu_ref)) { my $msg = "(Some) constants for $fiu_name in FIJI Settings are invalid."; $logger->error($msg); return (undef, $msg); } if (defined $dup->{$fiu_ref->{'FIU_NET_NAME'}}) { my $msg = "More than one FIU attached to ".$fiu_ref->{'FIU_NET_NAME'}; $logger->error($msg); $warn .= $msg; } else { $dup->{$fiu_ref->{'FIU_NET_NAME'}} = 1; } $fiji_settings_ref->{'fius'}->[$fiu_num] = $fiu_ref; $fiu_num++; $logger->trace("Read in $fiu_name from FIJI Settings file successfully."); } if ($fiu_num == 0) { $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 blocks. if (defined($design_ref->{'FIU_NUM'}) && $design_ref->{'FIU_NUM'} != $fiu_num) { my $msg = FIU_NUM->{'ini_name'} . " does not match the numbers of FIU blocks found."; $logger->error($msg); return (undef, $msg); } else { $design_ref->{'FIU_NUM'} = $fiu_num; # assume the best if FIU_NUM constant is not given } splice(@{$fiji_settings_ref->{'fius'}}, $fiu_num); $logger->info("Successfully read in design constants and $fiu_num FIU definitions from FIJI Settings file \"$fiji_ini_file\"."); return ($fiji_settings_ref, $warn); } ## @method public set_fiu_defaults (%$fiu_ref) # @brief Overwrite existing fields (if defined) with defaults defined in FIJI.pm. # sub set_fiu_defaults ($) { my ($self, $consts_ref) = @_; return _set_defaults(FIUMAP, $consts_ref); } ## @function _set_defaults (%$map_ref, %$consts_ref) # @brief Set defaults according to FIJI.pm. sub _set_defaults { my $logger = get_logger(""); my ($map_ref, $consts_ref, $phase) = @_; foreach my $k (keys(%{$map_ref})) { if (exists($map_ref->{$k}->{'default'})) { # Set default only if it is not mandatory in the given phase. if (defined($phase) && scalar(grep { $_ eq $phase } @{$map_ref->{$k}->{'phases_opt'}}) == 0) { next; } # Also, do not overwrite existing values if (defined($consts_ref->{$k})) { next; } $consts_ref->{$k} = $map_ref->{$k}->{default}; $logger->trace(sprintf("Adding default constant: %s (%s) = %s.", $k, $map_ref->{$k}->{'ini_name'}, $map_ref->{$k}->{default})); } } } sub validate_design_value { my ($k, $v_ref) = @_; return validate_value(DESIGNMAP, $k, $v_ref); } sub validate_fiu_value { my ($k, $v_ref) = @_; return validate_value(FIUMAP, $k, $v_ref); } ## @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 dep_ref scalar reference to a value the proposed value depends on # \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 { my $logger = get_logger(""); my ($map_ref, $k, $v_ref, $dep_ref, $old, $log_func) = @_; $log_func = \&Log::Log4perl::Logger::trace if !defined($log_func); if (defined($map_ref->{$k}->{'type'})) { my $orig = ${$v_ref}; # @FIXME: check if the constant is depending on another constant, that needs to be enabled. # my $dependency = @{$map_ref->{$k}->{'depends_on'}}; # if (defined($dependency) && defined(@{$map_ref->{$dependency}->{'value'}} # } if ($map_ref->{$k}->{'type'} eq 'net') { # @TODO: check syntax? there is nothing more to do in here unless we supply allowed nets... # which is actually possible because the input netlist is stored in the settings as well } elsif ($map_ref->{$k}->{'type'} eq 'hexadecimal' || $map_ref->{$k}->{'type'} eq 'lfsrpoly') { if ($orig !~ /^(0|(0[xX][[:xdigit:]]+))$/) { $log_func->($logger, "$k: $orig does not look like a natural hexadecimal number."); return 0; } ${$v_ref} = hex($orig); # Check for natural value range. Should never trigger due to the regex above. if (${$v_ref} < 0) { $log_func->($logger, "$k: $orig is negative."); return 0; } } elsif ($map_ref->{$k}->{'type'} eq 'natural') { # Match hexadecimal, binary, octal and decimal numbers (the latter also in scientific notation) 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."); return 0; } # convert non-decimal (hexadecimal, binary, octal) values to decimal ${$v_ref} = oct($orig) if $orig =~ /^0/; if (!looks_like_number(${$v_ref})) { $log_func->($logger, "$k: $orig does not look like a number."); return 0; } # Check for natural value range. Should never trigger due to the regex above. if (${$v_ref} < 0) { $log_func->($logger, "$k: $orig is negative."); return 0; } } elsif ($map_ref->{$k}->{'type'} eq 'boolean') { # convert strings to binary if need be if (!defined($orig)) { $log_func->($logger, "$k: \"undef\" is not a boolean value."); return 0; } elsif (lc($orig) eq 'true') { $orig = 1; } elsif (lc($orig) eq 'false') { $orig = 0; } if (($orig ne '0') && ($orig ne '1')) { $log_func->($logger, "$k: \"$orig\" does not look like a boolean value."); return 0; } # ensure proper boolean value, i.e. 0 or 1 ${$v_ref} = (!!$orig) ? 1 : 0; } $logger->trace("Converted value of $k (\"$orig\") to \"${$v_ref}\".") if (defined($orig) && $orig ne ${$v_ref}); } if (defined($map_ref->{$k}->{'values'})) { my $values_ref = $map_ref->{$k}->{'values'}; my $dep_val; if (ref $dep_ref eq 'SCALAR') { $dep_val = $dep_ref; } if (ref($values_ref) eq 'ARRAY') { # Look for given value in allowed values 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'}})); return 0; } } elsif (ref($values_ref) eq 'CODE') { if (!$values_ref->(${$v_ref}, $old, $$dep_val)) { $log_func->($logger, "$k: ${$v_ref} is not allowed."); return 0; } } } return 1; } # @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 keys # to the respective internal representation. This allows us to use different # name representations in the external file than within the implementation. # # # \returns $consts_ref, or undef on errors sub _rename_import { my $logger = get_logger(""); my ($map_ref, $consts_ref) = @_; if (ref($consts_ref) ne 'HASH') { $logger->error("Parameter is not a reference to a hash (containing design constants)."); return undef; } # Iterating over respective hash from FIJI.pm and rename the entries # to match our internal naming scheme. foreach my $k (keys(%{$map_ref})) { my $ini_name = $map_ref->{$k}->{'ini_name'}; if (exists($consts_ref->{$ini_name}) && $ini_name ne $k) { $consts_ref->{$k} = $consts_ref->{$ini_name}; $logger->trace(sprintf("Renaming Design setting %s -> %s (=%s).", $ini_name, $k, defined($consts_ref->{$ini_name}) ? $consts_ref->{$ini_name} : "undef"),); delete $consts_ref->{$ini_name}; } } 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}); delete $consts_ref->{$entry_key}; } } return $consts_ref; } ## @function _sanitize_fiu (%$fiu_ref, $phase) # @brief Convert and sanity check FIJI Settings. # # \param fiu_ref a reference to a hash containing FIJI Settings for a # single FIU. # \param phase the current design phase # # \returns A new hash with all constants required in the FIU settings # in sanitized form, or undef on errors. sub _sanitize_fiu { my $logger = get_logger(""); my ($fiu_ref, $phase) = @_; if (ref($fiu_ref) ne 'HASH') { my $msg = "Parameter is not a reference to a hash (containing FIU constants)."; $logger->error($msg); return $msg; } $fiu_ref = _validate_hashmap(FIUMAP, $fiu_ref, $phase); if (!ref($fiu_ref)) { $logger->error("Could not validate Design Constants."); return $fiu_ref; } return $fiu_ref; } sub _disabled_via_dependency { my ($map_ref, $consts_ref, $k) = @_; my $dependency = $map_ref->{$k}->{'depends_on'}; return defined($dependency) && !$consts_ref->{$dependency}; } sub _validate_hashmap { my $logger = get_logger(""); my ($map_ref, $consts_ref, $phase) = @_; my @map_keys = keys(%{$map_ref}); foreach my $entry_key (keys(%{$consts_ref})) { my $forbidden_by = $map_ref->{$entry_key}->{'forbidden_by'}; my $ini_name = $map_ref->{$entry_key}->{'ini_name'}; if (!exists($map_ref->{$entry_key})) { $logger->debug("Deleting unknown setting %s = %s.", $entry_key, $consts_ref->{$entry_key}); next; } @map_keys = grep { $_ ne $entry_key } @map_keys; # mark constant key as done 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}) ? "" : $consts_ref->{$entry_key}); $logger->error($msg); return $msg; } if (defined($forbidden_by) && $consts_ref->{$entry_key} && $consts_ref->{$forbidden_by}) { my $msg = "$entry_key is forbidden when $forbidden_by is enabled"; $logger->error($msg); return $msg; } } if (!defined($phase)) { # 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; } # 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 ( 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 ) { my $msg = "$k is mandatory in phase $phase."; $logger->error($msg); return $msg; } } # @TODO: implement the same mechanism with forbidden_by return $consts_ref; } ## @function _sanitize_design (%$consts_ref, $phase) # @brief Sanity check FIJI Design Settings. # # The function deals with sanity checks for the values # themselves. It checks for the following conditions: # # - FIU_NUM: > 0 # - FIU_CFG_BITS: > 0 # - TIMER_WIDTH: > 0, <= 16 # - ID: > 0, < 2^15-1 # - BAUDRATE: > 0 # # \param consts_ref a reference to a hash containing some design settings # \param phase the tool flow phase that defines the rules to check against # # \returns The given hash with all constants required in the design # settings in sanitized form, or an error message. sub _sanitize_design { my $logger = get_logger(""); my ($consts_ref, $phase) = @_; if (ref($consts_ref) ne 'HASH') { my $msg = "Parameter is not a reference to a hash (containing design constants)."; $logger->error($msg); return $msg; } # check for sane values $consts_ref = _validate_hashmap(DESIGNMAP, $consts_ref, $phase); if (!ref($consts_ref)) { my $msg = "Could not validate Design Constants."; $logger->error($msg); return "$msg $consts_ref"; } if (($consts_ref->{'FIU_CFG_BITS'} <= 0)) { my $msg = "FIU_CFG_BITS is <= 0."; $logger->error($msg); return $msg; } if (($consts_ref->{'TIMER_WIDTH'} <= 0) || ($consts_ref->{'TIMER_WIDTH'} > 16)) { my $msg = "TIMER_WIDTH is invalid ($consts_ref->{'TIMER_WIDTH'})."; $logger->error($msg); return $msg; } if (defined($consts_ref->{'ID'}) && ($consts_ref->{ID} < 0 || $consts_ref->{ID} > (2**16 - 1))) { my $msg = "ID is invalid ($consts_ref->{ID})."; $logger->error($msg); return $msg; } if (($consts_ref->{'BAUDRATE'} <= 0)) { my $msg = "BAUDRATE missing is <= 0."; $logger->error($msg); return $msg; } return $consts_ref; } sub _log2 { my $val = shift; return ($val > 0) ? (log($val) / log(2)) : 0; } ## @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 # # 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 # @param RESET_CYCLES the number of cycles to apply a FIJI-to-DUT reset # @param LFSR_WIDTH width of the LFSR used for stuck-open emulation # @param FIU_NUM the number of FIUs in the configuration # # @returns ($registers, $lutsum) sub _est_resources { my $logger = get_logger(""); my ($FREQUENCY, $BAUD, $TIMER_WIDTH, $RESET_CYCLES, $LFSR_WIDTH, $FIU_NUM) = @_; # @FIXME where do we put these values? they are likely to change if the VHDL # source is changed... my $registers; my $lut6; my $out_of_range = []; if ($FREQUENCY < 1000000 || $FREQUENCY > 500000000) { $logger->debug("FREQUENCY $FREQUENCY out of range for correct resource estimation (1000000 - 500000000)"); push @{$out_of_range}, "FREQUENCY"; } if ($BAUD < 9600 || $BAUD > 3000000) { $logger->debug("BAUD $BAUD out of range for correct resource estimation (9600 - 3000000)"); push @{$out_of_range}, "BAUD"; } if ($TIMER_WIDTH < 1 || $TIMER_WIDTH > 8) { $logger->debug("TIMER_WIDTH $TIMER_WIDTH out of range for correct resource estimation (1 - 8)"); push @{$out_of_range}, "TIMER_WIDTH"; } if ($RESET_CYCLES < 1 || $RESET_CYCLES > 16) { $logger->debug("RESET_CYCLES $RESET_CYCLES out of range for correct resource estimation (1 - 16)"); push @{$out_of_range}, "RESET_CYCLES"; } if ($LFSR_WIDTH < 16 || $LFSR_WIDTH > 64) { $logger->debug("LFSR_WIDTH $LFSR_WIDTH out of range for correct resource estimation (16 - 64)"); push @{$out_of_range}, "RESET_CYCLES"; } if ($FIU_NUM < 1 || $FIU_NUM > 64) { $logger->debug("FIU_NUM $FIU_NUM out of range for correct resource estimation (1 - 64)"); push @{$out_of_range}, "FIU_NUM"; } $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, $out_of_range); } ## @function public estimate_resources ($settings_ref) # @brief estimates the resources needed to implement the given settings # # @returns a hash with the members 'regs' and 'lut_calc' # sub estimate_resources { my $logger = get_logger(""); my ($settings_ref) = @_; my $consts_ref = $settings_ref->{'design'}; my $fiu_ref = $settings_ref->{'fius'}; my $fiu_num = @{$fiu_ref}; 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"); my $rv = { regs => sprintf("%.2f", $calcrv[0] / $base_resources[0]), lut_calc => sprintf("%.2f", $calcrv[1] / $base_resources[1]), out_of_range => $calcrv[2], }; $logger->debug("ESTIMATE: current config will need Base*$rv->{'regs'} registers and Base*$rv->{'lut_calc'} combinational resources"); return $rv; } 1;