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 6

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
#     This class represents an abstract component.
37
#     It is one of the central classes and holds data,
38
#     which is used to describe and instanciate a core or SOC.
39
#
40
#     The following fields are defined and used
41
#     by CoreDef and SOCDef
42
#         - name               : name of the core  (mandatory)
43
#         - version            : version of the core  (mandatory)
44
#         - toplevel           : toplevel name (mandatory)
45
#         - description        : description of this core
46
#         - date               : creation date
47
#         - license            : license of this core
48
#         - licensefile        : location of the license file
49
#         - author             : author of this core
50
#         - authormail         : author-mail of this core
51
#         - vccmd              : a version control command, which is used
52
#                                to download the files
53
#         - interfaces         : interfaces which are implemented
54
#                                see SOCMaker::IfcSpc
55
#         - inst_parameters    : hash of instantiation parameters
56
#                                see SOCMaker::Parameter
57
#         - static_parameters  : hash of static parameters
58
#                                see SOCMaker::SParameter
59
########
60
#
61
# TODO
62
#
63
#
64
###############################################################
65
 
66
 
67
module SOCMaker
68
class Component
69
  include ERR
70
  include YAML_EXT
71
 
72
  attr_accessor :name
73
  attr_accessor :version
74
  attr_accessor :toplevel
75
  attr_accessor :description
76
  attr_accessor :date
77
  attr_accessor :license
78
  attr_accessor :licensefile
79
  attr_accessor :author
80
  attr_accessor :authormail
81
  attr_accessor :vccmd
82
  attr_accessor :interfaces
83
  attr_accessor :functions
84
  attr_accessor :inst_parameters
85
  attr_accessor :static_parameters
86
  def initialize( name, version, toplevel, options = {} )
87
    init_with( { 'name'      => name,
88
                 'version'   => version,
89
                 'toplevel'  => toplevel }.merge( options ) )
90
  end
91
  def encode_with( coder )
92
    %w[ name version description date license licensefile
93
        author authormail vccmd toplevel interfaces
94
        functions inst_parameters static_parameters ].
95
          each { |v| coder[ v ] = instance_variable_get "@#{v}" }
96
  end
97
  def init_with( coder )
98
 
99
    serr_if( coder[ 'name' ] == nil,
100
      'Name not defined',
101
      field:    'name'    )
102
    @name = coder[ 'name' ]
103
    verr_if( !@name.is_a?( String ),
104
      'The name must be of type string',
105
      field:    'name'         )
106
    serr_if( @name.size  == 0,
107
      'Name not defined (size == 0)',
108
      field:    'name'    )
109
    verr_if( !!SOCMaker::conf[ :name_regex ].match( @name ) == false,
110
         'The core name is invalid',
111
        instance: @name,
112
        field:    'name' )
113
 
114
    serr_if( coder[ 'version' ] == nil,
115
      'Version not defined',
116
      instance: @name,
117
      field:    'version' )
118
    @version = coder[ 'version' ]
119
    serr_if( @version.size == 0,
120
      'Version not defined (size == 0)',
121
      instance: @name,
122
      field:    'version' )
123
 
124
    # cast from numeric to string, if not given as string
125
    @version = @version.to_s if @version.is_a? ( Numeric )
126
    verr_if( !@version.is_a?( String ),
127
      'The name must be of type string or numeric',
128
      field:    'name'         )
129
 
130
 
131
 
132
    serr_if( coder[ 'toplevel' ] == nil,
133
      'Toplevel not defined',
134
      instance: @name,
135
      field:    'toplevel' )
136
    @toplevel = coder[ 'toplevel' ]
137
    verr_if( !@toplevel.is_a?( String ),
138
      "toplevel must be of type string",
139
      instance: @name,
140
      field:    "toplevel" )
141
    serr_if( @toplevel.size  == 0,
142
      'Toplevel not defined (size == 0 )',
143
      instance: @name,
144
      field:    'toplevel' )
145
 
146
 
147
 
148
 
149
    # set non-nil values
150
    #  -> we don't need to check for nil in the rest of the
151
    #     processing
152
    @description       = coder[ 'description'       ] || ""
153
    @date              = coder[ 'date'              ] || ""
154
    @license           = coder[ 'license'           ] || ""
155
    @licensefile       = coder[ 'licensefile'       ] || ""
156
    @author            = coder[ 'author'            ] || ""
157
    @authormail        = coder[ 'authormail'        ] || ""
158
    @vccmd             = coder[ 'vccmd'             ] || ""
159
    @interfaces        = coder[ 'interfaces'        ] || {}
160
    @functions         = coder[ 'functions'         ] || {}
161
    @inst_parameters   = coder[ 'inst_parameters'   ] || {}
162
    @static_parameters = coder[ 'static_parameters' ] || {}
163
 
164
 
165
    # ensure, that these fields are of type String
166
    %w[ description date license licensefile
167
        author authormail vccmd ].each do |n|
168
      verr_if( !instance_variable_get( '@'+n ).is_a?( String ),
169
        "#{n} must be of type String",
170
        instance: @name,
171
        field:    n )
172
    end
173
 
174
    # ensure, that these fields are of type Hash
175
    %w[ interfaces inst_parameters
176
        functions  static_parameters ].each do |n|
177
      verr_if( !instance_variable_get( '@'+n ).is_a?( Hash ),
178
        "#{n} must be of type Hash",
179
        instance: @name,
180
        field:    n )
181
    end
182
 
183
 
184
 
185
    # check interfaces
186
    @interfaces.each do |ifc_name, ifc|
187
      serr_if( ifc == nil,
188
            'Interface not defined',
189
            instance:   @name+":"+ifc_name.to_s )
190
 
191
      serr_if( !ifc.is_a?( SOCMaker::IfcDef ),
192
            'Interface definition is not SOCMaker::IfcDef (use SOCM_IFC)',
193
            instance: @name+":"+ifc_name.to_s )
194
    end
195
 
196
    # check instance parameters
197
    @inst_parameters.each do |name, param |
198
      serr_if( param == nil,
199
            'Instance parameter not defined',
200
            instance:   @name+":"+name.to_s )
201
 
202
      serr_if( !param.is_a?( SOCMaker::Parameter ),
203
            'Instance parameter not SOCMaker::Parameter (use SOCM_PARAM)',
204
            instance: @name+":"+name.to_s )
205
    end
206
 
207
    # check instance parameters
208
    @static_parameters.each do |name, sparam |
209
      serr_if( sparam == nil,
210
            'Static parameter not defined',
211
            instance:   @name+":"+name.to_s )
212
 
213
      serr_if( !sparam.is_a?( SOCMaker::SParameter ),
214
            'Static parameter not SOCMaker::Parameter (use SOCM_SPARAM)',
215
            instance: @name+":"+name.to_s )
216
    end
217
 
218
 
219
 
220
 
221
  end
222
 
223
 
224
  def get_files
225 5 feddischso
    unless self.vccmd.nil? or @vccmd.size == 0
226 6 feddischso
      #puts"cd #{@dir} && #{@vccmd}"
227 5 feddischso
      system( "cd #{@dir} && #{vccmd} " )
228 3 feddischso
    end
229
  end
230
 
231
 
232
 
233
 
234
  def generics
235
    @inst_parameters.each_with_index do |(name, val), i|
236
      yield( name.to_s, val.type, val.default, i == @inst_parameters.size-1 )
237
    end
238
  end
239
 
240 5 feddischso
  def get_and_ensure_dst_dir!( core_name )
241
      dst_dir =  File.expand_path(
242
            File.join(
243
              SOCMaker::conf[ :build_dir ],
244
              SOCMaker::conf[ :hdl_dir   ],
245
              core_name ) )
246
      FileUtils.mkdir_p dst_dir
247
      return dst_dir
248
  end
249 3 feddischso
 
250
  #
251
  # Iterates over interface list.
252
  # For each interface, all ports are processed.
253
  # For each port within each interface, we lookup the port defn
254
  # and yield the call block with
255
  #   - port-name
256
  #   - port-definition
257
  #   - info if last
258
  # as argument
259
  #
260
  #
261
  #
262
  def ports( *args )
263
 
264
    if args.size == 0
265
      @interfaces.values.each_with_index do | ifc, i_ifc; ifc_def|
266
 
267
        # get interface definition
268
        ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
269
 
270
        # loop over ports in this interface
271
        ifc.ports.each_with_index do |(port_name, port_def), i_port; port_dir|
272
 
273
          # the reference to the port in the definition
274
          defn_ref = port_def.defn
275
          port_dir = ifc_def.ports[ defn_ref.to_sym ]
276
          perr_if( port_dir == nil,
277
                "Can't find #{defn_ref} in interface " +
278
                "definition #{ifc_def.name} version "  +
279
                ifc_def.version + "==>>" + ifc_def.to_yaml )
280
 
281
          # An xor mechanism between port_dir and ifc=>dir is used
282
          # to determine the direction of a port, for example:
283
          #  If the interface is declared as input (1) and a port is declared as input (1)
284
          #  the resulting direction will be an output 1^1 = 0.
285
          #  But if the overall interface direction is an output (0) and a port is declared
286
          #  as input, the resulting direction will an input 0^1 = 1.
287
          # This allows to define a port-direction in the interface definition,
288
          # and toggle the directions on core-definition level.
289
 
290
          #  (name, direction, length, is_last)
291
          yield(  port_name.to_s,
292
                  port_dir ^ ifc.dir,
293
                  port_def.len,
294
                  ( (i_port == ifc.ports.size-1 ) and (i_ifc == @interfaces.size-1 ) ) )
295
        end
296
      end
297
 
298
    elsif args.size == 1
299
 
300
      # get interface (input is the name as string  )
301
      ifc = @interfaces[ args.first.to_sym  ]
302
 
303
      ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
304
      ifc.ports.each do |port_name, port_def; port_dir|
305
        port_def = port_def.defn
306
        port_dir = ifc_def.ports[ port_def.to_sym ]
307
        if port_dir == nil
308
          perr_if( port_dir==nil,
309
              "Can't find #{port_def} in" +
310
              "interface definition #{ifc_def.name} " +
311
              "version #{ifc_def.version}" )
312
        end
313
        yield(  port_def.to_s,
314
                port_name.to_s,
315
                port_dir ^ ifc.dir )
316
      end
317
 
318
    else
319
 
320
    end
321
 
322
  end
323
 
324 5 feddischso
  def implements_port?( ifc_name, port_spec_name )
325
    tmp = @interfaces[ ifc_name.to_sym ].
326
        ports.select{ |key,hash| hash.defn == port_spec_name.to_s }.keys
327 3 feddischso
 
328 5 feddischso
    perr_if( tmp.size > 1,
329
      "The port #{port_spec_name} of interface #{ifc_name} is implemented
330
      multiple times" )
331
 
332
    return tmp.size == 1
333
  end
334 3 feddischso
 
335
  def param_ok?( param_name, param_value )
336
    param = inst_parameters[ param_name.to_sym ]
337
    param = static_parameters[ param_name.to_sym ] if param == nil
338
    return false if param == nil
339
  end
340
 
341
 
342
 
343
  def verify( is_soc = false )
344
 
345
    # check interfaces
346
    @interfaces.each do |ifc_name, ifc|
347
      ifc.verify
348
    end
349
 
350
    # check instance parameters
351
    @inst_parameters.each do |name, param |
352
      param.verify
353
    end
354
 
355
    # check instance parameters
356
    @static_parameters.each do |name, sparam |
357
      sparam.verify
358
    end
359
 
360
  end
361
 
362
  def ==(o)
363
 
364
    tmp    = ( o.class   == self.class )
365
    return tmp if !tmp
366
 
367
    %w[ name version description date license licensefile
368
        author authormail vccmd toplevel interfaces
369
        functions inst_parameters static_parameters ].
370
          each do |v|
371
      return false if instance_variable_get( "@#{v}" ) != o.instance_variable_get( "@#{v}" )
372
    end
373
    return true
374
  end
375
 
376 5 feddischso
  def to_s
377
    "version:           #{@version}\n"              +
378
    "toplevel:          #{@toplevel}\n"             +
379
    "description:       #{@description}\n"          +
380
    "date:              #{@date}\n"                 +
381
    "license:           #{@license}\n"              +
382
    "licensefile:       #{@licensefile}\n"          +
383
    "author:            #{@author}\n"               +
384
    "authormail:        #{@authormail}\n"           +
385
    "vccmd:             #{@vccmd}\n"                +
386
    "interfaces:        #{@interfaces}\n"           +
387
    "functions:         #{@functions}\n"            +
388
    "inst_parameters:   #{@inst_parameters}\n"      +
389
    "static_parameters: #{@static_parameters}\n"
390
  end
391
 
392
 
393 3 feddischso
end # class CoreDef
394
end # module SOCMaker
395
 
396
 
397
# vim: noai:ts=2:sw=2
398
 

powered by: WebSVN 2.1.0

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