Class: SOCMaker::SParameter

Inherits:
Object
  • Object
show all
Includes:
ERR
Defined in:
lib/soc_maker/sparameter.rb

Overview

A small classes, used to group information and to verify, auto-correct and auto-complete this information: This class represents a static parameter, which is only defined once within a system. Usually, these static parameters are mapped into a vhdl package or verilog include file. The following fields are defined:

  • #path (of the file, which is used as input)

  • #file_dst (output file destination)

  • #parameters (hash of SParameterEntry values)

At the moment, the token within the value of the parameter-hash is used as regular expression to replace this token in the input file by the key of the parameter-hash, and write the result to the destination file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ERR

#consistence_error, #consistence_error_if, #init_error, #init_error_if, #processing_error, #processing_error_if

Constructor Details

#initialize(path, file_dst, optional = {}) ⇒ SParameter

This constructor expects the source file path, the destination file path. The paremters can be passed as optional arguments.

path

source file path

file_dst

destination file path



79
80
81
82
# File 'lib/soc_maker/sparameter.rb', line 79

def initialize( path, file_dst, optional = {} )
  init_with( { 'path' => path,
               'file_dst' => file_dst }.merge( optional ) )
end

Instance Attribute Details

#file_dstObject

Path of the destination file



67
68
69
# File 'lib/soc_maker/sparameter.rb', line 67

def file_dst
  @file_dst
end

#parametersObject

Hash of SParameterEntry, each entry represents a parameter



70
71
72
# File 'lib/soc_maker/sparameter.rb', line 70

def parameters
  @parameters
end

#pathObject

Path of the file, which is used as input



64
65
66
# File 'lib/soc_maker/sparameter.rb', line 64

def path
  @path
end

Instance Method Details

#==(o) ⇒ Object

Equality operator



210
211
212
213
214
215
# File 'lib/soc_maker/sparameter.rb', line 210

def ==(o)
  o.class == self.class               && 
    o.parameters == self.parameters   &&
    o.file_dst   == self.file_dst     &&
    o.path       == self.path
end

#deploy(param, core_dir, dst_dir) ⇒ Object

Deployment function. It takes the source file from core_dir, iterates over all parameters and checks, if the parameter is in avaliable in param. If not, the default value is taken. all parameters are added to the source file and the result is saved in dst_dir.

param

A parameter hash, like { :name => value }

core_dir:+ The core source directory, where the parameter source file is located

dst_dir

The destination directory, where the result is saved. This dir. must exist.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/soc_maker/sparameter.rb', line 156

def deploy( param, core_dir, dst_dir )

  token_val_map = {}
  @parameters.each do |param_name,sparam_entry|

    if  param               != nil and
        param[ param_name ] != nil
      # use value defined in soc-spec
      tmp = param[ param_name ]
    else
      # use default value from core-spec
      tmp =  sparam_entry.default
    end

    if sparam_entry.type == "enum"
      token_val_map[ sparam_entry.token ] = sparam_entry.choice[ tmp.to_i ]
    elsif sparam_entry.type == "bool"
      if tmp == true
        token_val_map[ sparam_entry.token ] = sparam_entry.choice
      else
        token_val_map[ sparam_entry.token ] = ""
      end
    else
      token_val_map[ sparam_entry.token ] = tmp
    end


  end

  # create file paths
  src_path = File.join( core_dir, @path )
  #dst_dir  = CoreDef::get_and_ensure_dst_dir!( dir_name )
  dst_path = File.join( dst_dir, @file_dst )


  # process each line of input file
  # and replace tokens by value via 
  # regular expression
  File.open( dst_path, 'w' ) do |dst_f|
    File.open( src_path ) do |src_f|
      SOCMaker::logger.proc( "create #{dst_path} from #{ src_path} " )
      while line = src_f.gets
        token_val_map.each { |token, val| line = line.sub( Regexp.new( token.to_s  ), val.to_s ) }
        dst_f.puts line
      end
    end
  end

end

#encode_with(coder) ⇒ Object

Encoder method (to yaml)

coder

An instance of the Psych::Coder to encode this class to a YAML file



89
90
91
92
93
94
# File 'lib/soc_maker/sparameter.rb', line 89

def encode_with( coder )
  init_error_if !coder.is_a?( Psych::Coder ), 
              'coder is not given as Psych::Coder'
  %w[ path file_dst parameters ].
        each { |v| coder[ v ] = instance_variable_get "@#{v}" }
end

#init_with(coder) ⇒ Object

Initialization method (from yaml)

coder

An instance of the Psych::Coder to init this class from a YAML file



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/soc_maker/sparameter.rb', line 102

def init_with( coder )
  init_error_if !( coder.is_a?( Hash ) || coder.is_a?( Psych::Coder ) ), 
              'coder is not given as Hash neither as Psych::Coder'

  # path
  init_error 'no file path specified for static parameter',
    field: 'path'  if coder[ 'path' ] == nil
  @path = coder[ 'path' ]

  init_error 'file path specified for static parameter is not of type string',
    field: 'path' if !@path.is_a?( String )

  init_error 'file path specified for static parameter has zero length',
    field: 'path'  if @path.size == 0


  # file_dst (file-destination)
  init_error 'no destination file directory given for static parameter',
    instance: @path,
    field:    'file_dst' if coder[ 'file_dst' ] == nil

  @file_dst = coder[ 'file_dst' ]
  init_error 'destination file directory given for static parameter is not of type string',
    instance: @path,
    field:    'file_dst' if !@file_dst.is_a?( String )

  init_error 'file path specified for static parameter has zero length',
    field: 'path'  if @file_dst.size == 0


  @parameters = coder[ 'parameters' ] || {}
  @parameters.each do |name, param|
    init_error 'Static parameter entry not defined',
          instance: name if param == nil

    init_error 'Static parameter entry not SOCMaker::SParameterEntry (use SOCM_SENTRY)', 
          instance: name if !param.is_a?( SOCMaker::SParameterEntry )
  end

end