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

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker/] [core_inst.rb] - Blame information for rev 10

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 feddischso
###############################################################
2
#
3
#  File:      core_inst.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
 
36
module SOCMaker
37 9 feddischso
 
38
######
39
#
40
# This class represents a core instantiation within
41
# a SOC. It contains a parameter-hash (@params),
42
# which is used to define, which parameters are set to which values.
43
# The type field is used to identify the SOCMaker::CoreDef
44
# and the field @defn is initialized as reference to
45
# the corresponding CoreDef instance.
46 3 feddischso
class CoreInst
47
  include ERR
48
 
49
 
50
 
51
  attr_accessor :defn
52
  attr_accessor :type
53
  attr_accessor :params
54
 
55 10 feddischso
  #
56
  # Constructor
57
  # There is one mandatory attributes and an optional one.
58
  #
59
  # *type*::    The id of the core-definition, which is instanciated
60
  # *params*::  Instanciation parameters
61
  #
62 3 feddischso
  def initialize(  type, params = {} )
63
    init_with(  'type'   => type,
64
                'params' => params  )
65 7 feddischso
 
66 3 feddischso
  end
67 9 feddischso
 
68
  #
69
  # Encoder function (to yaml)
70
  #
71
  # +coder+:: An instance of the Psych::Coder to encode this class to a YAML file
72
  #
73 3 feddischso
  def encode_with( coder )
74
    %w[ type params ].
75
      each { |v| coder[ v ] = instance_variable_get "@#{v}" }
76
  end
77
 
78 9 feddischso
  #
79
  # Initialization function (from yaml)
80
  #
81
  # +coder+:: An instance of the Psych::Coder to init this class from a YAML file
82
  #
83
  #
84 3 feddischso
  def init_with( coder )
85
 
86
    serr_if( coder[ 'type' ] == nil,
87
      "no type is provided for a core instance",
88
      field: "type" )
89
 
90
    @type = coder[ 'type' ]
91
 
92
    @params = coder[ 'params' ] || {}
93
    serr_if( !@params.is_a?( Hash ), 'Parameters are not given as hash',
94
      field: 'params' )
95
 
96
  end
97
 
98
 
99
 
100
 
101 7 feddischso
  #
102 9 feddischso
  # Runs a consistency check and creates an internal
103
  # hash, which contains all evaluated ports.
104
  # Because the core-definition may contain variable port sizes,
105
  # which depend on the instance, these sizes need to be evaluated.
106
  # This is also done here and the result is stored in @_ifcs_evaluated.
107 8 feddischso
  #
108
  #
109 7 feddischso
  def consistency_check
110
 
111 3 feddischso
    @defn = SOCMaker::lib.get_core( @type )
112
 
113
 
114
    # check, if the instance parameters in the core definition
115
    @params.each do |param_name, value|
116
      verr_if(  @defn.inst_parameters[ param_name ] == nil,
117
                "Parameter not found: " + param_name.to_s,
118
        field: 'params' )
119
    end
120
 
121
    ## auto-complete parameters with default values
122
    @defn.inst_parameters.each do |param_name, param|
123
 
124
      # auto-complete to default values
125
      @params[ param_name ] ||= param.default
126
    end
127
 
128 9 feddischso
    @_ifcs_evaluated ||= {}
129 8 feddischso
    @defn.interfaces.keys.each do |ifc_name|
130 9 feddischso
      @_ifcs_evaluated[ ifc_name ] = {}
131
      @defn.ports( ifc_name.to_s ) do |port_name, port_dir, port_len, default, is_last |
132 8 feddischso
        if port_len.is_a?( String )
133
          param_match = SOCMaker::conf[ :length_regex ].match( port_len )
134
 
135
          if param_match and @params[ port_len.to_sym ] != nil
136
            tmp =@params[ port_len.to_sym ]
137
            tmp = tmp.to_i if tmp.is_a?( String )
138 9 feddischso
            @_ifcs_evaluated[ ifc_name ][ port_name.to_sym ] = { len: tmp, dir: port_dir, default: default }
139 8 feddischso
          else
140
            SOCMaker::logger.error( "Failed to evaluate #{port_len} for port #{port_name}" )
141
          end
142
        else
143 9 feddischso
          @_ifcs_evaluated[ ifc_name ][ port_name.to_sym ] = { len: port_len, dir: port_dir, default: default }
144 8 feddischso
        end
145
      end
146
    end
147 9 feddischso
 
148 7 feddischso
    @defn.consistency_check
149 3 feddischso
  end
150
 
151
 
152 6 feddischso
 
153 7 feddischso
 
154
  #
155
  # Generate toplevel hdl file for this instance.
156
  # This assumes, that this instance represents a SOC with
157
  # further instances.
158
  #
159
  #
160 9 feddischso
  # +coder+:: An instance of the SOCMaker::HDLCoder, which is used to
161
  #           create the auto-generated HDL (optional).
162
  #           If no coder is given, a SOCMaker::VHDLCoder is used.
163 7 feddischso
  #
164
  #
165
  def gen_toplevel( coder = VHDLCoder.new )
166 6 feddischso
 
167
 
168
    #
169 7 feddischso
    # Get filename
170 6 feddischso
    #
171 10 feddischso
    file_name = coder.filename( @defn.dir_name )
172 6 feddischso
 
173
    SOCMaker::logger.proc( "START of creating top-level '" + file_name + "'" )
174
 
175
 
176
    #
177
    # Create a unique list of cores and
178 7 feddischso
    # add for each core a component statement (vhdl only).
179 6 feddischso
    # Even if there are multiple instances of a core,
180
    # we need to decalre it only once
181
    #
182
    @defn.cores.values.uniq{|x| x.type }.each do |inst; spec|
183
 
184
      spec = SOCMaker::lib.get_core( inst.type  )
185
      SOCMaker::lib.check_nil( spec, "Can't find #{ inst.type } in SOC library" )
186
 
187 7 feddischso
      coder.add_core_component( inst.type, spec )
188 6 feddischso
    end
189
 
190 7 feddischso
    #
191
    # Instanciate each core
192
    #
193 6 feddischso
    @defn.cores.each do |inst_name, inst|
194 7 feddischso
      coder.add_core_instance( inst_name.to_s, inst )
195 6 feddischso
    end
196
 
197
 
198
    # Iterate over all connections:
199
    #  - create signal instances
200
    #  - add assignments
201
    #
202
    @defn.cons.each do |con_name, con_def|
203
      gen_toplevel_con(   con_name.to_s,
204
                          con_def[ :rule ],
205
                          con_def[ :mapping ][0],
206
                          con_def[ :mapping ][1],
207 7 feddischso
                          coder  )
208 6 feddischso
 
209
    end
210 8 feddischso
 
211
    assign_unused_to_default( coder )
212 7 feddischso
 
213
    #
214
    # Write content to the file
215
    #
216 6 feddischso
    SOCMaker::logger.proc( "writing top-level" )
217
    file_dir  = File.join( SOCMaker::conf[ :build_dir ],
218
                           SOCMaker::conf[ :hdl_dir   ] )
219
    ::FileUtils.mkdir_p file_dir
220 7 feddischso
    File.open( File.join( file_dir, file_name ), 'w' ) do |f|
221 10 feddischso
      f.write( coder.get_hdl_code( self, @defn.toplevel ) )
222 6 feddischso
    end
223 7 feddischso
    SOCMaker::logger.proc( "END of creating top-level hdl code for #{@defn.name}" )
224 6 feddischso
 
225
  end
226
 
227
 
228 7 feddischso
 
229 9 feddischso
  #
230
  # Iterate over all ports and call the block with
231
  #   - port-name
232
  #   - port length
233
  #   - default value
234
  #   - is-last value
235
  #
236
  #  If no argument is given, all ports of this instance are processed.
237
  #  If arguments are given, each argument is supposed to the name of
238
  #  a interface. All specified interfaces with all ports are processed.
239
  #
240
  # +args+::  Optional list of interface names
241
  #
242
  #
243
  def ports( *args )
244 7 feddischso
 
245 9 feddischso
    if args.size == 0
246
      ifc_sel = @_ifcs_evaluated
247
    else
248
      ifc_sel = @_ifcs_evaluated.select{ |k,v| args.include?( k.to_s ) }
249
    end
250
 
251 10 feddischso
    ifc_sel.values.each_with_index do |ifc, i_ifc|
252 9 feddischso
      ifc.each_with_index do |(port_name, port_def), i_port|
253
        yield(  port_name.to_s,
254
                port_def[ :dir ],
255
                port_def[ :len ],
256
                port_def[ :default ],
257
                i_port==ifc.size-1 && i_ifc == ifc_sel.size-1 )
258
      end
259
    end
260
  end
261
 
262 7 feddischso
  #
263 9 feddischso
  # Iterate over all generics and call the block with
264
  #   - generic-name
265
  #   - generic-type
266
  #   - the value
267
  #   - is-last information
268
  #
269
  def generics
270
    @defn.generics do |name, type, default_value, is_last|
271
      value = @params[ name.to_sym ];
272
      value = value
273
      value = default_value if value == nil
274
      yield( name.to_s, type, value, is_last )
275
    end
276
  end
277
 
278
 
279
 
280
 
281
 
282
 
283
 
284
  #
285
  # Returns a port, identified by the interface and port name
286
  #
287
  #  +ifc_name+::       name of the interface
288
  #  +port_spec_name+:: name of the port
289
  #
290
  def port( ifc_name, port_spec_name )
291
    tmp = @defn.interfaces[ ifc_name.to_sym ].
292
        ports.select{ |key,hash| hash.defn == port_spec_name.to_s }.
293
        keys.first.to_s
294
    return [ tmp, @_ifcs_evaluated[ ifc_name.to_sym ][ tmp.to_sym ] ]
295
  end
296
 
297
  #
298
  # Returns the length of a port within an interface.
299
  # If no instance is given, we know that it is
300
  # a toplevel interface.
301
  # Otherwise we check for this and we do a recursive call.
302
  # If this port is within a interface of a core, we
303
  # pass the call to the core-definition of this instance, which
304
  # knows all cores.
305
  #
306
  # +ifc_name+::        name of the interface
307
  # +port_spec_name+::  name of the port
308
  # +inst+::            name of the instance (optional), default is nil
309
  #
310
  def port_length( ifc_name, port_spec_name, inst = nil )
311
    if inst == nil
312
      tmp = @defn.interfaces[ ifc_name.to_sym ].
313
          ports.select{ |key,hash| hash.defn == port_spec_name.to_s }.
314
          keys.first.to_s
315
      return tmp.size == 0 ? 0 : @_ifcs_evaluated[ ifc_name.to_sym ][ tmp.to_sym ][ :len ]
316
    else
317 10 feddischso
      if inst == @defn.toplevel.to_sym
318 9 feddischso
        return port_length( ifc_name, port_spec_name )
319
      else
320
        return @defn.port_length( ifc_name, port_spec_name, inst )
321
      end
322
    end
323
  end
324
 
325
  #
326
  # Returns the core definition for an instance (identified by its name)
327
  #
328
  # +inst+:: name of the instance
329
  #
330
  def core_definition( inst )
331
      if inst == @defn.name
332
        return @defn
333
      else
334
        tmp = @defn.core_definition( inst )
335
        perr_if( tmp == nil, "#Processing error: {inst} not found by core_definition" )
336
        return tmp
337
      end
338
  end
339
 
340
  #
341
  # Returns a core instance, identified by its name.
342
  # If it is not a sub-core, we return our self
343
  #
344
  # +inst+::  name of the instance
345
  #
346
  def core_instance( inst )
347
    if @defn.cores[ inst ] != nil
348
      return @defn.cores[ inst ]
349
    else
350
      return self
351
    end
352
  end
353
 
354
 
355
 
356
 
357
 
358
 
359
  #
360
  # Returns a string describing this instance
361
  #
362
  def to_s
363
    "type:     #{type}\n"   +
364
    "params:   #{params}\n"
365
  end
366
 
367
  #
368
  # Equality operator
369
  #
370
  def ==(o)
371
    o.class     == self.class   &&
372
    o.type      == self.type    &&
373
    o.params    == self.params
374
  end
375
 
376
 
377
 
378
 
379
 
380
 
381
  #
382 8 feddischso
  # Assign default values for unused interfaces.
383
  # This is just a helper function and is used by gen_toplevel
384
  #
385
  # +coder+:: A HDL coder, which is used to create the auto-generated HDL.
386
  #
387
  def assign_unused_to_default( coder )
388
 
389
 
390
 
391
    # iterate over all instances
392
    # and check all interfaces
393
    @defn.cores.each do |inst_name, inst|
394
 
395
      inst.defn.interfaces.each do |ifc_name, ifc|
396
 
397
        #
398
        # Get the interface specification by using the 1st source entry
399
        # and searching for the core-definition.
400
        #
401
        if !@defn.ifc_in_use?( inst_name, ifc_name )
402 10 feddischso
          coder.add_ifc_default_assignment(  inst, inst_name, ifc_name )
403 8 feddischso
        end
404
      end
405
    end
406
  end
407
 
408
  #
409 7 feddischso
  # This function is called during the toplevel generation
410
  # for each connection.
411
  #
412
  # +name+::   The name of the connection
413
  # +rule+::   The combination rule (obsolete/unused)
414
  # +src+::    Source hash with instance name as key and interface name as value
415
  # +dst+::    Destination hash with instance name as key and interface name as value
416
  # +coder+::  The HDL coder which is used
417
  #
418 6 feddischso
  def gen_toplevel_con( name, rule, src, dst, coder )
419
 
420
    src_inst            = {};
421
    dst_inst            = {};
422
 
423 7 feddischso
    #
424
    # Get the interface specification by using the 1st source entry
425
    # and searching for the core-definition.
426
    #
427 6 feddischso
    ifc_spec = SOCMaker::lib.get_ifc(
428 10 feddischso
      core_definition( src.keys.first.to_s ).interfaces[ src.values.first ].id )
429 7 feddischso
 
430
 
431
    #
432
    # Get the maximum required signal length
433
    #
434
    # For each signal in the interface specification,
435
    # we create a list. The list has an entry for each source
436
    # and destination signal, which defines the length.
437
    #
438
    # In the second step, the maximum in each list is extracted.
439
    #
440 6 feddischso
    length_tmp  = {};
441
    ifc_spec.ports.keys.each do |_name|
442
      length_tmp[ _name ] = []
443
      dst.each do |inst_name, ifc_name|
444 9 feddischso
        length_tmp[ _name ] << port_length( ifc_name, _name, inst_name )
445 6 feddischso
      end
446
      src.each do |inst_name, ifc_name|
447 9 feddischso
        length_tmp[ _name ] << port_length( ifc_name, _name, inst_name )
448 6 feddischso
      end
449
    end
450
    max_length = Hash[ length_tmp.map{ |key, arr| [ key, arr.max ] } ]
451
 
452
 
453 7 feddischso
    #
454
    # Prepare a hash for all sources and destinations, where
455
    # the instance name is the key and the core-instance is
456
    # the value.
457
    #
458 6 feddischso
    src.keys.each do |inst_name|
459 9 feddischso
      src_inst[ inst_name ] = core_instance( inst_name )
460 6 feddischso
    end
461
    dst.keys.each do |inst_name|
462 9 feddischso
      dst_inst[ inst_name ] = core_instance( inst_name )
463 6 feddischso
    end
464
 
465 7 feddischso
    #
466
    # create the declaraion and assignments
467
    #
468
    coder.add_ifc_connection( ifc_spec, name, max_length, src_inst, dst_inst, src, dst )
469 6 feddischso
 
470
  end
471
 
472
 
473 3 feddischso
 
474
 
475
 
476 9 feddischso
 
477
 
478
 
479
 
480
 
481
  private :assign_unused_to_default, :gen_toplevel_con
482
 
483
 
484
 
485 3 feddischso
end # CoreInst
486
end # SOCMaker
487
 
488
 
489
 
490
# 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.