OpenCores
URL https://opencores.org/ocsvn/soc_maker/soc_maker/trunk

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker/] [component.rb] - Blame information for rev 9

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 feddischso
###############################################################
2
#
3
#  File:      component.rb
4
#
5
#  Author:    Christian Hättich
6
#
7
#  Project:   System-On-Chip Maker
8
#
9
#  Target:    Linux / Windows / Mac
10
#
11
#  Language:  ruby
12
#
13
#
14
###############################################################
15
#
16
#
17
#   Copyright (C) 2014  Christian Hättich  - feddischson [ at ] opencores.org
18
#
19
#   This program is free software: you can redistribute it and/or modify
20
#   it under the terms of the GNU General Public License as published by
21
#   the Free Software Foundation, either version 3 of the License, or
22
#   (at your option) any later version.
23
#
24
#   This program is distributed in the hope that it will be useful,
25
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
#   GNU General Public License for more details.
28
#
29
#   You should have received a copy of the GNU General Public License
30
#   along with this program.  If not, see .
31
#
32
#
33
###############################################################
34
#
35
#   Description:
36
########
37
#
38
# TODO
39
#
40
#
41
###############################################################
42
 
43
 
44 9 feddischso
 
45 3 feddischso
module SOCMaker
46 9 feddischso
 
47
 
48
 
49
######
50
#
51
# This class represents an abstract component.
52
# It is one of the central classes and holds data,
53
# which is used to describe a core or System-On-Chip (SOC).
54
#
55
#
56 3 feddischso
class Component
57
  include ERR
58
  include YAML_EXT
59
 
60 9 feddischso
 
61
  # name of the core  (mandatory)
62 3 feddischso
  attr_accessor :name
63 9 feddischso
 
64
  # version of the core  (mandatory)
65 3 feddischso
  attr_accessor :version
66 9 feddischso
 
67
  # toplevel name (mandatory)
68 3 feddischso
  attr_accessor :toplevel
69 9 feddischso
 
70
  # description of this core
71 3 feddischso
  attr_accessor :description
72 9 feddischso
 
73
  # creation date
74 3 feddischso
  attr_accessor :date
75 9 feddischso
 
76
  # license of this core
77 3 feddischso
  attr_accessor :license
78 9 feddischso
 
79
  # location of the license file
80 3 feddischso
  attr_accessor :licensefile
81 9 feddischso
 
82
  # author of this core
83 3 feddischso
  attr_accessor :author
84 9 feddischso
 
85
  # author-mail of this core
86 3 feddischso
  attr_accessor :authormail
87 9 feddischso
 
88
  # a version control command, which is used to download the files
89 3 feddischso
  attr_accessor :vccmd
90 9 feddischso
 
91
  # interfaces which are implemented see SOCMaker::IfcSpc
92 3 feddischso
  attr_accessor :interfaces
93 9 feddischso
 
94 3 feddischso
  attr_accessor :functions
95 9 feddischso
 
96
  # hash of instantiation parameters see SOCMaker::Parameter
97 3 feddischso
  attr_accessor :inst_parameters
98 9 feddischso
 
99
  # hash of static parameters see SOCMaker::SParameter
100 3 feddischso
  attr_accessor :static_parameters
101 9 feddischso
 
102
 
103
  #
104
  # Constructor
105
  # The three attributes are required, and all other attributes
106
  # can be given as a optinal hash
107
  #
108
  # *name*:: Name of this component
109
  # *version*:: Version of this component
110
  # *toplevel*:: Toplevel name of this component
111
  # *optional*:: Non-mandatory values, which can be set during initialization.
112
  #
113
  #
114
  def initialize( name, version, toplevel, optional = {} )
115 3 feddischso
    init_with( { 'name'      => name,
116
                 'version'   => version,
117 9 feddischso
                 'toplevel'  => toplevel }.merge( optional ) )
118 3 feddischso
  end
119 9 feddischso
 
120
  #
121
  # Encoder function (to yaml)
122
  #
123
  # +coder+:: An instance of the Psych::Coder to encode this class to a YAML file
124
  #
125 3 feddischso
  def encode_with( coder )
126
    %w[ name version description date license licensefile
127
        author authormail vccmd toplevel interfaces
128
        functions inst_parameters static_parameters ].
129
          each { |v| coder[ v ] = instance_variable_get "@#{v}" }
130
  end
131 9 feddischso
 
132
  #
133
  # Initialization function (from yaml)
134
  #
135
  # +coder+:: An instance of the Psych::Coder to init this class from a YAML file
136
  #
137
  #
138 3 feddischso
  def init_with( coder )
139
 
140
    serr_if( coder[ 'name' ] == nil,
141
      'Name not defined',
142
      field:    'name'    )
143
    @name = coder[ 'name' ]
144
    verr_if( !@name.is_a?( String ),
145
      'The name must be of type string',
146
      field:    'name'         )
147
    serr_if( @name.size  == 0,
148
      'Name not defined (size == 0)',
149
      field:    'name'    )
150
    verr_if( !!SOCMaker::conf[ :name_regex ].match( @name ) == false,
151
         'The core name is invalid',
152
        instance: @name,
153
        field:    'name' )
154
 
155
    serr_if( coder[ 'version' ] == nil,
156
      'Version not defined',
157
      instance: @name,
158
      field:    'version' )
159
    @version = coder[ 'version' ]
160
    serr_if( @version.size == 0,
161
      'Version not defined (size == 0)',
162
      instance: @name,
163
      field:    'version' )
164
 
165
    # cast from numeric to string, if not given as string
166
    @version = @version.to_s if @version.is_a? ( Numeric )
167
    verr_if( !@version.is_a?( String ),
168
      'The name must be of type string or numeric',
169
      field:    'name'         )
170
 
171
 
172
 
173
    serr_if( coder[ 'toplevel' ] == nil,
174
      'Toplevel not defined',
175
      instance: @name,
176
      field:    'toplevel' )
177
    @toplevel = coder[ 'toplevel' ]
178
    verr_if( !@toplevel.is_a?( String ),
179
      "toplevel must be of type string",
180
      instance: @name,
181
      field:    "toplevel" )
182
    serr_if( @toplevel.size  == 0,
183
      'Toplevel not defined (size == 0 )',
184
      instance: @name,
185
      field:    'toplevel' )
186
 
187
 
188
 
189
 
190
    # set non-nil values
191
    #  -> we don't need to check for nil in the rest of the
192
    #     processing
193
    @description       = coder[ 'description'       ] || ""
194
    @date              = coder[ 'date'              ] || ""
195
    @license           = coder[ 'license'           ] || ""
196
    @licensefile       = coder[ 'licensefile'       ] || ""
197
    @author            = coder[ 'author'            ] || ""
198
    @authormail        = coder[ 'authormail'        ] || ""
199
    @vccmd             = coder[ 'vccmd'             ] || ""
200
    @interfaces        = coder[ 'interfaces'        ] || {}
201
    @functions         = coder[ 'functions'         ] || {}
202
    @inst_parameters   = coder[ 'inst_parameters'   ] || {}
203
    @static_parameters = coder[ 'static_parameters' ] || {}
204
 
205
 
206
    # ensure, that these fields are of type String
207
    %w[ description date license licensefile
208
        author authormail vccmd ].each do |n|
209
      verr_if( !instance_variable_get( '@'+n ).is_a?( String ),
210
        "#{n} must be of type String",
211
        instance: @name,
212
        field:    n )
213
    end
214
 
215
    # ensure, that these fields are of type Hash
216
    %w[ interfaces inst_parameters
217
        functions  static_parameters ].each do |n|
218
      verr_if( !instance_variable_get( '@'+n ).is_a?( Hash ),
219
        "#{n} must be of type Hash",
220
        instance: @name,
221
        field:    n )
222
    end
223
 
224
 
225
 
226
    # check interfaces
227
    @interfaces.each do |ifc_name, ifc|
228
      serr_if( ifc == nil,
229
            'Interface not defined',
230
            instance:   @name+":"+ifc_name.to_s )
231
 
232
      serr_if( !ifc.is_a?( SOCMaker::IfcDef ),
233 7 feddischso
            'Interface definition is not SOCMaker::IfcDef (please use SOCM_IFC)',
234 3 feddischso
            instance: @name+":"+ifc_name.to_s )
235
    end
236
 
237
    # check instance parameters
238
    @inst_parameters.each do |name, param |
239
      serr_if( param == nil,
240
            'Instance parameter not defined',
241
            instance:   @name+":"+name.to_s )
242
 
243
      serr_if( !param.is_a?( SOCMaker::Parameter ),
244 7 feddischso
            'Instance parameter not SOCMaker::Parameter (please use SOCM_PARAM)',
245 3 feddischso
            instance: @name+":"+name.to_s )
246
    end
247
 
248
    # check instance parameters
249
    @static_parameters.each do |name, sparam |
250
      serr_if( sparam == nil,
251
            'Static parameter not defined',
252
            instance:   @name+":"+name.to_s )
253
 
254
      serr_if( !sparam.is_a?( SOCMaker::SParameter ),
255 7 feddischso
            'Static parameter not SOCMaker::Parameter (please use SOCM_SPARAM)',
256 3 feddischso
            instance: @name+":"+name.to_s )
257
    end
258
 
259 7 feddischso
  end
260 3 feddischso
 
261
 
262 9 feddischso
  #
263
  # Runs a consistency check:
264
  # Iterate over all interfaces and check, if the interface is
265
  # in the SOCMaker::Lib.
266
  # The function also checks also, if the ports defined by this
267
  # core is also defined in the interface.
268
  #
269 7 feddischso
  def consistency_check
270 8 feddischso
    @interfaces.values.each_with_index do | ifc, i_ifc; ifc_def|
271
 
272
      # get interface definition
273
      ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
274 3 feddischso
 
275 8 feddischso
 
276
      # check, if all mandatory ports are implemented by this interface
277
      ifc_def.ports.each do | port_name, port |
278
        perr_if( port[ :mandatory ] == true &&
279
                  ifc.ports.select{ |key,port_def| port_def.defn.to_sym == port_name }.size == 0,
280
          "Mandatory port #{port_name} is not implemented in interface #{ifc.name}" )
281
      end
282
    end
283
 
284 3 feddischso
  end
285
 
286
 
287 9 feddischso
  #
288
  # Runs the Version Control System command via system(....)
289
  #
290
  def update_vcs
291 5 feddischso
    unless self.vccmd.nil? or @vccmd.size == 0
292 6 feddischso
      #puts"cd #{@dir} && #{@vccmd}"
293 5 feddischso
      system( "cd #{@dir} && #{vccmd} " )
294 3 feddischso
    end
295
  end
296
 
297
 
298
 
299 9 feddischso
 
300
 
301
  # Iterates over all generic values of this component
302
  # and yield the call block with
303
  # - generic name
304
  # - generic type
305
  # - generic default
306
  # - is-last value
307 3 feddischso
  def generics
308
    @inst_parameters.each_with_index do |(name, val), i|
309 9 feddischso
 
310
      _generic_name     = name.to_s
311
      _generic_type     = val.type
312
      _generic_default  = val.default
313
      _is_last          = i == @inst_parameters.size-1
314
      yield( _generic_name   ,
315
             _generic_type   ,
316
             _generic_default,
317
             _is_last         )
318
 
319 3 feddischso
    end
320
  end
321
 
322
 
323
  #
324 9 feddischso
  # Iterates over interface list (if no argument is given)
325
  # or all specified interfaces.
326 3 feddischso
  # For each interface, all ports are processed.
327
  # For each port within each interface, we lookup the port defn
328
  # and yield the call block with
329 9 feddischso
  # - port-name
330
  # - port length
331
  # - default value
332
  # - is-last value
333 3 feddischso
  # as argument
334
  #
335 8 feddischso
  # An xor mechanism between port_dir and ifc=>dir is used
336
  # to determine the direction of a port, for example:
337 9 feddischso
  # If the interface is declared as input (1) and a port is declared as input (1)
338
  # the resulting direction will be an output 1^1 = 0.
339
  # But if the overall interface direction is an output (0) and a port is declared
340
  # as input, the resulting direction will an input 0^1 = 1.
341 8 feddischso
  # This allows to define a port-direction in the interface definition,
342
  # and toggle the directions on core-definition level.
343 3 feddischso
  #
344
  #
345 9 feddischso
  # *args*:: An optional list of interface names
346 3 feddischso
  def ports( *args )
347
 
348
    if args.size == 0
349 9 feddischso
      @ifc_sel = @interfaces
350
    else
351
      @ifc_sel = @interfaces.select{ |k,v| args.include?( k.to_s ) }
352
    end
353 3 feddischso
 
354 9 feddischso
    @ifc_sel.values.each_with_index do | ifc, i_ifc; ifc_def|
355
 
356
      # get interface definition
357
      ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
358 3 feddischso
 
359 9 feddischso
      # loop over ports in this interface
360
      ifc.ports.each_with_index do |(port_name, port_def), i_port |
361 3 feddischso
 
362 9 feddischso
        # the reference to the port in the definition
363 8 feddischso
        defn_ref      = port_def.defn.to_sym
364
        perr_if( !ifc_def.ports.has_key?( defn_ref ),
365 9 feddischso
            "Can't find #{port_def} in" +
366 8 feddischso
            "interface definition #{ifc_def.name} " +
367
            "version #{ifc_def.version}" )
368 9 feddischso
 
369
        _port_name    = port_name.to_s
370
        _port_dir     = ifc_def.ports[ defn_ref ][:dir] ^ ifc.dir
371
        _port_length  = port_def.len
372
        _port_default = ifc_def.ports[ defn_ref ][ :default  ]
373
        _is_last      = ( (i_port == ifc.ports.size-1 ) and (i_ifc == @ifc_sel.size-1 ) )
374
        yield(  _port_name, _port_dir, _port_length, _port_default, _is_last )
375 3 feddischso
      end
376
    end
377
  end
378
 
379
 
380 7 feddischso
 
381 9 feddischso
  #
382
  # Equality operator
383
  #
384 3 feddischso
  def ==(o)
385
 
386
    tmp    = ( o.class   == self.class )
387
    return tmp if !tmp
388
 
389
    %w[ name version description date license licensefile
390
        author authormail vccmd toplevel interfaces
391
        functions inst_parameters static_parameters ].
392
          each do |v|
393
      return false if instance_variable_get( "@#{v}" ) != o.instance_variable_get( "@#{v}" )
394
    end
395
    return true
396
  end
397
 
398 9 feddischso
  #
399
  # Returns a string describing this instance
400
  #
401 5 feddischso
  def to_s
402
    "version:           #{@version}\n"              +
403
    "toplevel:          #{@toplevel}\n"             +
404
    "description:       #{@description}\n"          +
405
    "date:              #{@date}\n"                 +
406
    "license:           #{@license}\n"              +
407
    "licensefile:       #{@licensefile}\n"          +
408
    "author:            #{@author}\n"               +
409
    "authormail:        #{@authormail}\n"           +
410
    "vccmd:             #{@vccmd}\n"                +
411
    "interfaces:        #{@interfaces}\n"           +
412
    "functions:         #{@functions}\n"            +
413
    "inst_parameters:   #{@inst_parameters}\n"      +
414
    "static_parameters: #{@static_parameters}\n"
415
  end
416
 
417
 
418 9 feddischso
 
419
 
420
 
421
  #
422
  # Creates a core directory, if it doesn't exist.
423
  # The path of the target directoy depends
424
  # on SOCMaker::conf[ :build_dir ] and
425
  # on SOCMaker::conf[ :hdl_dir ].
426
  # The resulting path is
427
  # ./#{SOCMaker::conf[ :build_dir ]}/#{SOCMaker::conf[ :hdl_dir ]}/dir_name
428
  #
429
  # *dir_name*:: Name of the target directory
430
  #
431
  def self.get_and_ensure_dst_dir!( dir_name )
432
      dst_dir =  File.expand_path(
433
            File.join(
434
              SOCMaker::conf[ :build_dir ],
435
              SOCMaker::conf[ :hdl_dir   ],
436
              dir_name ) )
437
      FileUtils.mkdir_p dst_dir
438
      return dst_dir
439
  end
440
 
441
 
442
 
443
 
444
 
445
 
446
 
447 3 feddischso
end # class CoreDef
448
end # module SOCMaker
449
 
450
 
451
# vim: noai:ts=2:sw=2
452
 
453 8 feddischso
# vim: noai:ts=2:sw=2

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.