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

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker/] [soc_def.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:      soc_def.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 a System-on-chip and derives
37
#     the functionallity from Component.
38
#     The two important fields are
39
#       - @cores: holds all core-instances
40
#       - cons:   holds all connections
41
#     In addition, the field @static is used to store
42
#     static parameters, which are set for cores used in this SOC.
43
#
44
###############################################################
45
 
46
module SOCMaker
47
class SOCDef < Component
48
  include ERR
49
  include YAML_EXT
50
 
51
  attr_accessor :cores
52
  attr_accessor :cons
53
  attr_accessor :static
54
  def initialize( name, version, toplevel, options = {} )
55
 
56
    init_with( { 'name'     => name,
57
                 'version'  => version,
58
                 'toplevel' => toplevel }.merge( options ) )
59
 
60
  end
61
 
62
  def encode_with( coder )
63
    super coder
64
    %w[ cores cons static ].
65
      each { |v| coder[ v ] = instance_variable_get "@#{v}" }
66
  end
67
 
68
  def init_with( coder )
69
    super coder
70
    @cores  = coder[ 'cores'  ] || {}
71
    @static = coder[ 'static' ] || {}
72
    @cons   = coder[ 'cons'   ] || {}
73
  end
74
 
75
  def verify
76
    super( true )
77
 
78
    @cores.each do |core_name, core_inst|
79
      core_inst.verify
80
    end
81
 
82
  end
83
 
84
 
85
 
86
 
87
 
88
 
89
  # SOCMaker::logger.error( "instantiation #{inst_name} is already in use" )
90
  def inst_in_use?( inst_name )
91
       @cores[ inst_name.to_sym ] != nil or
92
       @cons[ inst_name.to_sym ]  != nil
93
  end
94
 
95
  def rm( inst_name )
96
 
97
    if @cores[ inst_name.to_sym ] != nil
98
      # TODO: remove also all related connections
99
      @cores.delete( inst_name.to_sym )
100
    elsif @cons[ inst_name.to_sym ]  != nil
101
      @cons.delete( inst_name.to_sym )
102
    else
103
      return false
104
    end
105
    return true
106
  end
107
 
108
 
109
  def add_core( name, version, inst_name )
110
 
111
    return false if inst_in_use?( inst_name )
112
 
113
    # check, if the core exits in our library
114
    #  if not: an error will be raised
115
    SOCMaker::lib.get_core( name, version )
116
 
117
    @cores[ inst_name.to_sym ] = SOCMaker::CoreInst.new( name+version )
118
  end
119
 
120
 
121
  def ifc_in_use?( inst_name, ifc_name )
122
 
123
    # go through all connections and check,
124
    # that non of the interfaces we want to connect is used
125
    @cons.each do |_con_name, con_def|
126
      return true if con_def[ :mapping ][ 0 ][ inst_name.to_sym] == ifc_name.to_sym
127
      return true if con_def[ :mapping ][ 1 ][ inst_name.to_sym] == ifc_name.to_sym
128
    end
129
    return false
130
 
131
  end
132
 
133
 
134 5 feddischso
 
135
  def get_core_def( inst )
136
      if @cores[ inst.to_sym ] != nil
137
        return @cores[ inst.to_sym ].defn
138
      elsif inst == @name
139
        return self
140
      else
141
        perr_if( true,
142
          "Instance '#{inst}' does not exist in SOC '#{self.name}'" )
143
      end
144
  end
145
 
146
 
147
 
148 3 feddischso
  def add_connection( inst1, ifc1_name, inst2, ifc2_name, con_name )
149
 
150
    return nil if inst_in_use?( con_name )
151
 
152
 
153
   [ [ inst1, ifc1_name ],
154
     [ inst2, ifc2_name ] ].each do |sub_arr|
155
      perr_if( ifc_in_use?( sub_arr[ 0 ], sub_arr[ 1 ] ),
156
          "Interface #{sub_arr[ 1 ]} of instance '#{sub_arr[ 0 ]}' is already in use " )
157
    end
158
 
159
 
160 5 feddischso
    core_spec_1 = get_core_def( inst1 )
161
    core_spec_2 = get_core_def( inst2 )
162 3 feddischso
 
163 5 feddischso
 
164 3 feddischso
    [ [ core_spec_1, ifc1_name ],
165
      [ core_spec_2, ifc2_name ] ].each do |sub_arr|
166
        perr_if( sub_arr[ 0 ].interfaces[ sub_arr[ 1 ].to_sym ] == nil,
167
          "Interface '#{sub_arr[ 1 ]}' dosn't exist in core '#{sub_arr[0].name}' " )
168
    end
169
 
170
    # check name and version of the ifcs which will be connected
171
    perr_if( core_spec_1.interfaces[ ifc1_name.to_sym ].name     !=
172
             core_spec_2.interfaces[ ifc2_name.to_sym ].name     ||
173
             core_spec_1.interfaces[ ifc1_name.to_sym ].version  !=
174
             core_spec_2.interfaces[ ifc2_name.to_sym ].version,
175
          "Can't connect #{
176
            core_spec_1.interfaces[ ifc1_name.to_sym ].name } - #{
177
            core_spec_2.interfaces[ ifc2_name.to_sym ].name } : #{
178
            core_spec_1.interfaces[ ifc1_name.to_sym ].version } - #{
179
            core_spec_2.interfaces[ ifc2_name.to_sym ].version }" )
180
 
181
 
182
    @cons[ con_name.to_sym ] = {
183
          :rule    => "or",
184
          :mapping => [ { inst1.to_sym => ifc1_name.to_sym },
185
                        { inst2.to_sym => ifc2_name.to_sym } ] }
186 5 feddischso
    return false
187 3 feddischso
  end
188
 
189
  def set_param( instance, param, value )
190
 
191
    # get instance
192
    core_inst = @cores[ instance.to_sym ]
193
    perr_if( core_inst == nil,
194
      "Can't find '#{instance}' in SOC" )
195
 
196
    # get the core-definition
197
    core_def = SOCMaker::lib.get_core( core_inst.type  )
198
 
199
    # check if parameter exist
200
    if core_def.inst_parameters[ param.to_sym ] != nil
201
      core_inst.params[ param.to_sym ] = value
202
    else
203
      perr_if( true,
204
        "Parameter '#{param}' not found in '#{core_def.name}'" )
205
    end
206
 
207
  end
208
 
209
  def get_param( instance, param )
210
 
211
    # get instance
212
    core_inst = @cores[ instance.to_sym ]
213
    perr_if( core_inst == nil,
214
      "Can't find '#{instance}' in SOC" )
215
    param_val = core_inst.params[ param.to_sym ]
216
    perr_if( param_val == nil,
217
      "Can't find parameter '#{param}' in '#{instance}'" )
218
    return param_val
219
  end
220
 
221
 
222
  def set_sparam( core, param, value )
223
 
224
    #get instance
225
 
226
    # check, if we are instantiating this core
227
    perr_if( @cores.select{ |name,inst| inst.type == core }.size == 0,
228
      "Core '#{core}' is not instantiated in this SOC" )
229
 
230
    # get the core-definition
231
    core_def = SOCMaker::lib.get_core( core )
232
 
233
    # check if parameter exist
234
    perr_if( core_def.static_parameters.select{ |f,p| p.parameters[ param.to_sym ] != nil }.size == 0,
235
        "Parameter '#{param}' not found in '#{core_def.name}'" )
236
 
237
    @static[ core.to_sym ] ||= {}
238
    @static[ core.to_sym ][ param.to_sym ] = value
239
  end
240
 
241
  def get_sparam( core, param )
242
    perr_if( @static[ core.to_sym ] == nil,
243
      "Core '#{core}' does not exist in this SOC" )
244
 
245
    perr_if( @static[ core.to_sym ][ param.to_sym ] == nil,
246
      "Parameter '#{param}' does not exist for core '#{core}'" )
247
 
248
    return @static[ core.to_sym ][ param.to_sym ]
249
  end
250
 
251
 
252
 
253
  def gen_toplevel( coder = VHDLCoder.new() )
254
 
255 5 feddischso
    file_name = @name.dup
256 3 feddischso
 
257
    if coder.is_a?( VHDLCoder )
258
      file_name << ".vhd"
259
    elsif coder.is_a?( VerilogCoder )
260
      file_name << ".v"
261
    else
262
      perr_if( true,
263
        "No valid coder" )
264
    end
265
 
266
 
267
    SOCMaker::logger.proc( "START of creating top-level '" + file_name + "'" )
268
 
269
 
270
    #
271
    # Create a unique list of cores and
272
    # add every core to your implementation.
273
    # Even if there are multiple instances of a core,
274
    # we need to decalre it only once
275
    #
276
    @cores.values.uniq{|x| x.type }.each do |inst; spec|
277
 
278
      spec = SOCMaker::lib.get_core( inst.type  )
279
      SOCMaker::lib.check_nil( spec, "Can't find #{ inst.type } in SOC library" )
280
 
281
      coder.add_core_declaration( inst.type, spec )
282
    end
283
 
284
   #core_instances = {}
285
   #@cores.each do |inst_name, _core_inst|
286
   #  # the corresponding core from SOC lib
287
   #  core_def = SOCMaker::lib.get_core( _core_inst[ :type ] )
288
   #  core_instances[ inst_name ] = SOCMaker::CoreInst.new( core_def, _core_inst[ :params ] )
289
   #end
290
 
291
 
292
    @cores.each do |inst_name, inst|
293
      coder.add_core_inst( inst_name.to_s, inst )
294
    end
295
 
296
 
297
 
298
    # Iterate over all connections:
299
    #  - create signal instances
300
    #  - add assignments
301
    #
302
    @cons.each do |con_name, con_def|
303
      gen_toplevel_con(   con_name.to_s,
304
                          con_def[ :rule ],
305
                          con_def[ :mapping ][0],
306
                          con_def[ :mapping ][1],
307
      coder  )
308
 
309
    end
310
 
311
    SOCMaker::logger.proc( "writing top-level" )
312
 
313
    file_dir  = File.join( SOCMaker::conf[ :build_dir ],
314
                           SOCMaker::conf[ :hdl_dir   ] )
315
    ::FileUtils.mkdir_p file_dir
316
    file_path = File.join( file_dir, file_name )
317
    File.open( file_path, 'w' ) do |f|
318
      f.write( coder.get_entity( self, "test" ) )
319
    end
320
    SOCMaker::logger.proc( "END of creating top-level" )
321
 
322
  end
323
 
324
 
325
  def gen_toplevel_con( name, rule, src, dst, coder )
326
 
327
    src_inst            = {};
328
    dst_inst            = {};
329
 
330
    # fetch somehow the spec
331
    ifc_spec = SOCMaker::lib.get_ifc(
332 5 feddischso
      get_core_def( src.keys.first.to_s ).interfaces[ src.values.first ].name,
333
      get_core_def( src.keys.first.to_s ).interfaces[ src.values.first ].version )
334 3 feddischso
 
335 5 feddischso
    port_used_tmp = {};
336 3 feddischso
    ifc_spec.ports.keys.each do |_name|
337 5 feddischso
      # TODO: bis dahin konnte angenommen werden, dass self auch eine Art instanz ist,
338
      #       aber hier geht das nicht mehr, weil get_len nur für CoreInst definiert ist.
339
      #
340
 
341 3 feddischso
 
342
      # create a length table
343 5 feddischso
      port_used_tmp[ _name ] = false
344 3 feddischso
      dst.each do |inst_name, ifc_name|
345 5 feddischso
        port_used_tmp[ _name ] ||= get_core_def( inst_name ).implements_port?( ifc_name, _name )
346 3 feddischso
      end
347
      src.each do |inst_name, ifc_name|
348 5 feddischso
        port_used_tmp[ _name ] ||= get_core_def( inst_name ).implements_port?( ifc_name, _name )
349 3 feddischso
      end
350
    end
351
 
352
    # getting the maximum length for each signal
353 5 feddischso
#    max_length = Hash[ length_tmp.map{ |key, arr| [ key, arr.max ] } ]
354 3 feddischso
 
355 5 feddischso
#    coder.ifc_declaration( ifc_spec, name, max_length )
356 3 feddischso
 
357
 
358
 
359
    src.keys.each do |inst_name|
360
      src_inst[ inst_name ]            = @cores[ inst_name ]
361
    end
362
    dst.keys.each do |inst_name|
363
      dst_inst[ inst_name ]            = @cores[ inst_name ]
364
    end
365
 
366
 
367
 
368 5 feddischso
#    coder.ifc_assignment( ifc_spec, name, max_length, src_inst, dst_inst, src, dst )
369 3 feddischso
 
370
  end
371
 
372
  def copy_files
373
 
374
    SOCMaker::logger.proc( "START of copying all HDL files" )
375
 
376
    #
377
    # Create a unique list of cores and
378
    # for every core, create a directory and copy files
379
    #
380
    @cores.values.uniq{|x| x.type }.each do |core_inst; core_def, dst_dir|
381
 
382
      core_def = SOCMaker::lib.get_core( core_inst.type )
383
 
384
      # create destination directory name and ensure, that it is exist
385
      dst_dir  = get_and_ensure_dst_dir!( core_def.name )
386
 
387
      # copy each file into destination dir
388
      core_def.hdlfiles.each do |file, val|
389
        file_path = File.join( core_def.dir, val.path )
390 5 feddischso
        dst_path = File.join( dst_dir, val.path )
391 3 feddischso
        SOCMaker::logger.proc( "copy #{file_path} to #{ dst_path} " )
392 5 feddischso
        FileUtils.mkdir_p(File.dirname(dst_path))
393 3 feddischso
        FileUtils.cp( file_path, dst_path )
394
      end
395
 
396
 
397
 
398
      #
399
      # handle the static parameters
400
      #   (search and replace in pakckage/include files)
401
      core_def.static_parameters.each do |file, param|
402
 
403
        token_val_map = {}
404
        param.parameters.each do |n,p|
405
 
406
          if  @static[ core_inst.type.to_sym ]      != nil and
407
              @static[ core_inst.type.to_sym ][ n ] != nil
408
 
409
            # use value defined in soc-spec
410
            token_val_map[ p.token ] = @static[ core_inst.type.to_sym ][ n ]
411
          else
412
            # use default value from core-spec
413
            token_val_map[ p.token ] =  p.default
414
          end
415
 
416
        end
417
 
418
        # create file paths
419
        src_path = File.join( core_def.dir, param.path )
420
        dst_dir  = get_and_ensure_dst_dir!( core_def.name )
421
        dst_path = File.join( dst_dir, param.file_dst )
422
 
423
 
424
        # process each line of input file
425
        # and replace tokens by value via
426
        # regular expression
427
        File.open( dst_path, 'w' ) do |dst_f|
428
          File.open( src_path ) do |src_f|
429
            SOCMaker::logger.proc( "create #{dst_path} from #{ src_path} " )
430
            while line = src_f.gets
431
              token_val_map.each { |token, val| line = line.sub( Regexp.new( token.to_s ), val.to_s ) }
432
              dst_f.puts line
433
            end
434
          end
435
        end
436
 
437
 
438
 
439
      end
440
 
441
    end
442
 
443
 
444
    SOCMaker::logger.proc( "END of copying all HDL files" )
445
  end
446
 
447
 
448
  def ==(o)
449
    o.class   == self.class   &&
450
    o.cores   == self.cores   &&
451
    o.cons    == self.cons    &&
452
    o.static  == self.static  &&
453
    super( o )
454
  end
455
 
456
 
457 5 feddischso
  def to_s
458
 
459
    tmp = "_________ SOC #{@name}: _______\n"     +
460
          super                                   +
461
          "\n__connections__\n"
462
 
463
    @cons.each do |_con_name, con_def|
464
      tmp += "#{_con_name}: #{con_def}\n"
465
    end
466
 
467
    tmp += "\n__cores__\n"
468
    @cores.each do |inst_name, inst|
469
      tmp += "#{inst_name}:\n#{inst}\n"
470
    end
471
    tmp += "'''''''''''''''''''''''''''''''''''\n"
472
    return tmp
473
  end
474
 
475
 
476 3 feddischso
end # class SOCSpec
477
end # module SOCMaker
478
 
479
 
480
# vim: noai:ts=2:sw=2

powered by: WebSVN 2.1.0

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