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 10

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 10 feddischso
  # ID of the core  (mandatory)
65
  attr_accessor :id
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 10 feddischso
  # *id*:: Id of this component
110 9 feddischso
  # *toplevel*:: Toplevel name of this component
111
  # *optional*:: Non-mandatory values, which can be set during initialization.
112
  #
113
  #
114 10 feddischso
  def initialize( name, id, toplevel, optional = {} )
115 3 feddischso
    init_with( { 'name'      => name,
116 10 feddischso
                 'id'        => id,
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 10 feddischso
    %w[ name id description date license licensefile
127 3 feddischso
        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
 
151 10 feddischso
    serr_if( coder[ 'id' ] == nil,
152
      'Id not defined',
153 3 feddischso
      instance: @name,
154 10 feddischso
      field:    'id' )
155
    @id = coder[ 'id' ]
156
    serr_if( @id.size == 0,
157
      'Id not defined (size == 0)',
158 3 feddischso
      instance: @name,
159 10 feddischso
      field:    'id' )
160 3 feddischso
 
161 10 feddischso
    verr_if( !@id.is_a?( String ),
162 3 feddischso
      'The name must be of type string or numeric',
163
      field:    'name'         )
164
 
165
 
166
 
167
    serr_if( coder[ 'toplevel' ] == nil,
168
      'Toplevel not defined',
169
      instance: @name,
170
      field:    'toplevel' )
171
    @toplevel = coder[ 'toplevel' ]
172
    verr_if( !@toplevel.is_a?( String ),
173
      "toplevel must be of type string",
174
      instance: @name,
175
      field:    "toplevel" )
176
    serr_if( @toplevel.size  == 0,
177
      'Toplevel not defined (size == 0 )',
178
      instance: @name,
179
      field:    'toplevel' )
180
 
181
 
182
 
183
 
184
    # set non-nil values
185
    #  -> we don't need to check for nil in the rest of the
186
    #     processing
187
    @description       = coder[ 'description'       ] || ""
188
    @date              = coder[ 'date'              ] || ""
189
    @license           = coder[ 'license'           ] || ""
190
    @licensefile       = coder[ 'licensefile'       ] || ""
191
    @author            = coder[ 'author'            ] || ""
192
    @authormail        = coder[ 'authormail'        ] || ""
193
    @vccmd             = coder[ 'vccmd'             ] || ""
194
    @interfaces        = coder[ 'interfaces'        ] || {}
195
    @functions         = coder[ 'functions'         ] || {}
196
    @inst_parameters   = coder[ 'inst_parameters'   ] || {}
197
    @static_parameters = coder[ 'static_parameters' ] || {}
198
 
199
 
200
    # ensure, that these fields are of type String
201
    %w[ description date license licensefile
202
        author authormail vccmd ].each do |n|
203
      verr_if( !instance_variable_get( '@'+n ).is_a?( String ),
204
        "#{n} must be of type String",
205
        instance: @name,
206
        field:    n )
207
    end
208
 
209
    # ensure, that these fields are of type Hash
210
    %w[ interfaces inst_parameters
211
        functions  static_parameters ].each do |n|
212
      verr_if( !instance_variable_get( '@'+n ).is_a?( Hash ),
213
        "#{n} must be of type Hash",
214
        instance: @name,
215
        field:    n )
216
    end
217
 
218
 
219
 
220
    # check interfaces
221
    @interfaces.each do |ifc_name, ifc|
222
      serr_if( ifc == nil,
223
            'Interface not defined',
224
            instance:   @name+":"+ifc_name.to_s )
225
 
226
      serr_if( !ifc.is_a?( SOCMaker::IfcDef ),
227 7 feddischso
            'Interface definition is not SOCMaker::IfcDef (please use SOCM_IFC)',
228 3 feddischso
            instance: @name+":"+ifc_name.to_s )
229
    end
230
 
231
    # check instance parameters
232
    @inst_parameters.each do |name, param |
233
      serr_if( param == nil,
234
            'Instance parameter not defined',
235
            instance:   @name+":"+name.to_s )
236
 
237
      serr_if( !param.is_a?( SOCMaker::Parameter ),
238 7 feddischso
            'Instance parameter not SOCMaker::Parameter (please use SOCM_PARAM)',
239 3 feddischso
            instance: @name+":"+name.to_s )
240
    end
241
 
242
    # check instance parameters
243
    @static_parameters.each do |name, sparam |
244
      serr_if( sparam == nil,
245
            'Static parameter not defined',
246
            instance:   @name+":"+name.to_s )
247
 
248
      serr_if( !sparam.is_a?( SOCMaker::SParameter ),
249 7 feddischso
            'Static parameter not SOCMaker::Parameter (please use SOCM_SPARAM)',
250 3 feddischso
            instance: @name+":"+name.to_s )
251
    end
252
 
253 7 feddischso
  end
254 3 feddischso
 
255 10 feddischso
  #
256
  # the directory name of this core
257
  #
258
  def dir_name
259
    @id.split(',').join("_")
260
  end
261 3 feddischso
 
262 10 feddischso
 
263
 
264 9 feddischso
  #
265
  # Runs a consistency check:
266
  # Iterate over all interfaces and check, if the interface is
267
  # in the SOCMaker::Lib.
268
  # The function also checks also, if the ports defined by this
269
  # core is also defined in the interface.
270
  #
271 7 feddischso
  def consistency_check
272 8 feddischso
    @interfaces.values.each_with_index do | ifc, i_ifc; ifc_def|
273
 
274
      # get interface definition
275 10 feddischso
      ifc_def = SOCMaker::lib.get_ifc( ifc.id )
276 3 feddischso
 
277 8 feddischso
 
278
      # check, if all mandatory ports are implemented by this interface
279
      ifc_def.ports.each do | port_name, port |
280
        perr_if( port[ :mandatory ] == true &&
281
                  ifc.ports.select{ |key,port_def| port_def.defn.to_sym == port_name }.size == 0,
282
          "Mandatory port #{port_name} is not implemented in interface #{ifc.name}" )
283
      end
284
    end
285
 
286 3 feddischso
  end
287
 
288
 
289 9 feddischso
  #
290
  # Runs the Version Control System command via system(....)
291
  #
292
  def update_vcs
293 5 feddischso
    unless self.vccmd.nil? or @vccmd.size == 0
294 6 feddischso
      #puts"cd #{@dir} && #{@vccmd}"
295 5 feddischso
      system( "cd #{@dir} && #{vccmd} " )
296 3 feddischso
    end
297
  end
298
 
299
 
300
 
301 9 feddischso
 
302
 
303
  # Iterates over all generic values of this component
304
  # and yield the call block with
305
  # - generic name
306
  # - generic type
307
  # - generic default
308
  # - is-last value
309 3 feddischso
  def generics
310
    @inst_parameters.each_with_index do |(name, val), i|
311 9 feddischso
 
312
      _generic_name     = name.to_s
313
      _generic_type     = val.type
314
      _generic_default  = val.default
315
      _is_last          = i == @inst_parameters.size-1
316
      yield( _generic_name   ,
317
             _generic_type   ,
318
             _generic_default,
319
             _is_last         )
320
 
321 3 feddischso
    end
322
  end
323
 
324
 
325
  #
326 9 feddischso
  # Iterates over interface list (if no argument is given)
327
  # or all specified interfaces.
328 3 feddischso
  # For each interface, all ports are processed.
329
  # For each port within each interface, we lookup the port defn
330
  # and yield the call block with
331 9 feddischso
  # - port-name
332
  # - port length
333
  # - default value
334
  # - is-last value
335 3 feddischso
  # as argument
336
  #
337 8 feddischso
  # An xor mechanism between port_dir and ifc=>dir is used
338
  # to determine the direction of a port, for example:
339 9 feddischso
  # If the interface is declared as input (1) and a port is declared as input (1)
340
  # the resulting direction will be an output 1^1 = 0.
341
  # But if the overall interface direction is an output (0) and a port is declared
342
  # as input, the resulting direction will an input 0^1 = 1.
343 8 feddischso
  # This allows to define a port-direction in the interface definition,
344
  # and toggle the directions on core-definition level.
345 3 feddischso
  #
346
  #
347 9 feddischso
  # *args*:: An optional list of interface names
348 3 feddischso
  def ports( *args )
349
 
350
    if args.size == 0
351 9 feddischso
      @ifc_sel = @interfaces
352
    else
353
      @ifc_sel = @interfaces.select{ |k,v| args.include?( k.to_s ) }
354
    end
355 3 feddischso
 
356 9 feddischso
    @ifc_sel.values.each_with_index do | ifc, i_ifc; ifc_def|
357
 
358
      # get interface definition
359 10 feddischso
      ifc_def = SOCMaker::lib.get_ifc( ifc.id )
360 3 feddischso
 
361 9 feddischso
      # loop over ports in this interface
362
      ifc.ports.each_with_index do |(port_name, port_def), i_port |
363 3 feddischso
 
364 9 feddischso
        # the reference to the port in the definition
365 8 feddischso
        defn_ref      = port_def.defn.to_sym
366
        perr_if( !ifc_def.ports.has_key?( defn_ref ),
367 9 feddischso
            "Can't find #{port_def} in" +
368 8 feddischso
            "interface definition #{ifc_def.name} " +
369 10 feddischso
            "id #{ifc_def.id}" )
370 9 feddischso
 
371
        _port_name    = port_name.to_s
372
        _port_dir     = ifc_def.ports[ defn_ref ][:dir] ^ ifc.dir
373
        _port_length  = port_def.len
374
        _port_default = ifc_def.ports[ defn_ref ][ :default  ]
375
        _is_last      = ( (i_port == ifc.ports.size-1 ) and (i_ifc == @ifc_sel.size-1 ) )
376
        yield(  _port_name, _port_dir, _port_length, _port_default, _is_last )
377 3 feddischso
      end
378
    end
379
  end
380
 
381
 
382 7 feddischso
 
383 9 feddischso
  #
384
  # Equality operator
385
  #
386 3 feddischso
  def ==(o)
387
 
388
    tmp    = ( o.class   == self.class )
389
    return tmp if !tmp
390
 
391 10 feddischso
    %w[ name id description date license licensefile
392 3 feddischso
        author authormail vccmd toplevel interfaces
393
        functions inst_parameters static_parameters ].
394
          each do |v|
395
      return false if instance_variable_get( "@#{v}" ) != o.instance_variable_get( "@#{v}" )
396
    end
397
    return true
398
  end
399
 
400 9 feddischso
  #
401
  # Returns a string describing this instance
402
  #
403 5 feddischso
  def to_s
404 10 feddischso
    "id:                #{@id}\n"              +
405 5 feddischso
    "toplevel:          #{@toplevel}\n"             +
406
    "description:       #{@description}\n"          +
407
    "date:              #{@date}\n"                 +
408
    "license:           #{@license}\n"              +
409
    "licensefile:       #{@licensefile}\n"          +
410
    "author:            #{@author}\n"               +
411
    "authormail:        #{@authormail}\n"           +
412
    "vccmd:             #{@vccmd}\n"                +
413
    "interfaces:        #{@interfaces}\n"           +
414
    "functions:         #{@functions}\n"            +
415
    "inst_parameters:   #{@inst_parameters}\n"      +
416
    "static_parameters: #{@static_parameters}\n"
417
  end
418
 
419
 
420 9 feddischso
 
421
 
422
 
423
  #
424
  # Creates a core directory, if it doesn't exist.
425
  # The path of the target directoy depends
426
  # on SOCMaker::conf[ :build_dir ] and
427
  # on SOCMaker::conf[ :hdl_dir ].
428
  # The resulting path is
429
  # ./#{SOCMaker::conf[ :build_dir ]}/#{SOCMaker::conf[ :hdl_dir ]}/dir_name
430
  #
431
  # *dir_name*:: Name of the target directory
432
  #
433
  def self.get_and_ensure_dst_dir!( dir_name )
434
      dst_dir =  File.expand_path(
435
            File.join(
436
              SOCMaker::conf[ :build_dir ],
437
              SOCMaker::conf[ :hdl_dir   ],
438
              dir_name ) )
439
      FileUtils.mkdir_p dst_dir
440
      return dst_dir
441
  end
442
 
443
 
444
 
445
 
446
 
447
 
448
 
449 3 feddischso
end # class CoreDef
450
end # module SOCMaker
451
 
452
 
453
# vim: noai:ts=2:sw=2
454
 
455 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.