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 5

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
    puts "PATH: "
226
    p File.join( @dir )
227
    unless self.vccmd.nil? or @vccmd.size == 0
228
      puts"cd #{@dir} && #{@vccmd}"
229
      system( "cd #{@dir} && #{vccmd} " )
230 3 feddischso
    end
231
  end
232
 
233
 
234
 
235
 
236
  def generics
237
    @inst_parameters.each_with_index do |(name, val), i|
238
      yield( name.to_s, val.type, val.default, i == @inst_parameters.size-1 )
239
    end
240
  end
241
 
242 5 feddischso
  def get_and_ensure_dst_dir!( core_name )
243
      dst_dir =  File.expand_path(
244
            File.join(
245
              SOCMaker::conf[ :build_dir ],
246
              SOCMaker::conf[ :hdl_dir   ],
247
              core_name ) )
248
      FileUtils.mkdir_p dst_dir
249
      return dst_dir
250
  end
251 3 feddischso
 
252
  #
253
  # Iterates over interface list.
254
  # For each interface, all ports are processed.
255
  # For each port within each interface, we lookup the port defn
256
  # and yield the call block with
257
  #   - port-name
258
  #   - port-definition
259
  #   - info if last
260
  # as argument
261
  #
262
  #
263
  #
264
  def ports( *args )
265
 
266
    if args.size == 0
267
      @interfaces.values.each_with_index do | ifc, i_ifc; ifc_def|
268
 
269
        # get interface definition
270
        ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
271
 
272
        # loop over ports in this interface
273
        ifc.ports.each_with_index do |(port_name, port_def), i_port; port_dir|
274
 
275
          # the reference to the port in the definition
276
          defn_ref = port_def.defn
277
          port_dir = ifc_def.ports[ defn_ref.to_sym ]
278
          perr_if( port_dir == nil,
279
                "Can't find #{defn_ref} in interface " +
280
                "definition #{ifc_def.name} version "  +
281
                ifc_def.version + "==>>" + ifc_def.to_yaml )
282
 
283
          # An xor mechanism between port_dir and ifc=>dir is used
284
          # to determine the direction of a port, for example:
285
          #  If the interface is declared as input (1) and a port is declared as input (1)
286
          #  the resulting direction will be an output 1^1 = 0.
287
          #  But if the overall interface direction is an output (0) and a port is declared
288
          #  as input, the resulting direction will an input 0^1 = 1.
289
          # This allows to define a port-direction in the interface definition,
290
          # and toggle the directions on core-definition level.
291
 
292
          #  (name, direction, length, is_last)
293
          yield(  port_name.to_s,
294
                  port_dir ^ ifc.dir,
295
                  port_def.len,
296
                  ( (i_port == ifc.ports.size-1 ) and (i_ifc == @interfaces.size-1 ) ) )
297
        end
298
      end
299
 
300
    elsif args.size == 1
301
 
302
      # get interface (input is the name as string  )
303
      ifc = @interfaces[ args.first.to_sym  ]
304
 
305
      ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
306
      ifc.ports.each do |port_name, port_def; port_dir|
307
        port_def = port_def.defn
308
        port_dir = ifc_def.ports[ port_def.to_sym ]
309
        if port_dir == nil
310
          perr_if( port_dir==nil,
311
              "Can't find #{port_def} in" +
312
              "interface definition #{ifc_def.name} " +
313
              "version #{ifc_def.version}" )
314
        end
315
        yield(  port_def.to_s,
316
                port_name.to_s,
317
                port_dir ^ ifc.dir )
318
      end
319
 
320
    else
321
 
322
    end
323
 
324
  end
325
 
326 5 feddischso
  def implements_port?( ifc_name, port_spec_name )
327
    tmp = @interfaces[ ifc_name.to_sym ].
328
        ports.select{ |key,hash| hash.defn == port_spec_name.to_s }.keys
329 3 feddischso
 
330 5 feddischso
    perr_if( tmp.size > 1,
331
      "The port #{port_spec_name} of interface #{ifc_name} is implemented
332
      multiple times" )
333
 
334
    return tmp.size == 1
335
  end
336 3 feddischso
 
337
  def param_ok?( param_name, param_value )
338
    param = inst_parameters[ param_name.to_sym ]
339
    param = static_parameters[ param_name.to_sym ] if param == nil
340
    return false if param == nil
341
  end
342
 
343
 
344
 
345
  def verify( is_soc = false )
346
 
347
    # check interfaces
348
    @interfaces.each do |ifc_name, ifc|
349
      ifc.verify
350
    end
351
 
352
    # check instance parameters
353
    @inst_parameters.each do |name, param |
354
      param.verify
355
    end
356
 
357
    # check instance parameters
358
    @static_parameters.each do |name, sparam |
359
      sparam.verify
360
    end
361
 
362
  end
363
 
364
  def ==(o)
365
 
366
    tmp    = ( o.class   == self.class )
367
    return tmp if !tmp
368
 
369
    %w[ name version description date license licensefile
370
        author authormail vccmd toplevel interfaces
371
        functions inst_parameters static_parameters ].
372
          each do |v|
373
      return false if instance_variable_get( "@#{v}" ) != o.instance_variable_get( "@#{v}" )
374
    end
375
    return true
376
  end
377
 
378 5 feddischso
  def to_s
379
    "version:           #{@version}\n"              +
380
    "toplevel:          #{@toplevel}\n"             +
381
    "description:       #{@description}\n"          +
382
    "date:              #{@date}\n"                 +
383
    "license:           #{@license}\n"              +
384
    "licensefile:       #{@licensefile}\n"          +
385
    "author:            #{@author}\n"               +
386
    "authormail:        #{@authormail}\n"           +
387
    "vccmd:             #{@vccmd}\n"                +
388
    "interfaces:        #{@interfaces}\n"           +
389
    "functions:         #{@functions}\n"            +
390
    "inst_parameters:   #{@inst_parameters}\n"      +
391
    "static_parameters: #{@static_parameters}\n"
392
  end
393
 
394
 
395 3 feddischso
end # class CoreDef
396
end # module SOCMaker
397
 
398
 
399
# vim: noai:ts=2:sw=2
400
 

powered by: WebSVN 2.1.0

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