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 7

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
 
76 7 feddischso
  def consistency_check
77
    super
78
    @cores.values.each do |inst|
79
      inst.consistency_check
80 3 feddischso
    end
81
  end
82
 
83
 
84
 
85
 
86
  # SOCMaker::logger.error( "instantiation #{inst_name} is already in use" )
87
  def inst_in_use?( inst_name )
88
       @cores[ inst_name.to_sym ] != nil or
89
       @cons[ inst_name.to_sym ]  != nil
90
  end
91
 
92
  def rm( inst_name )
93
 
94
    if @cores[ inst_name.to_sym ] != nil
95
      # TODO: remove also all related connections
96
      @cores.delete( inst_name.to_sym )
97
    elsif @cons[ inst_name.to_sym ]  != nil
98
      @cons.delete( inst_name.to_sym )
99
    else
100
      return false
101
    end
102
    return true
103
  end
104
 
105
 
106
  def add_core( name, version, inst_name )
107
 
108
    return false if inst_in_use?( inst_name )
109
 
110
    # check, if the core exits in our library
111
    #  if not: an error will be raised
112
    SOCMaker::lib.get_core( name, version )
113
 
114
    @cores[ inst_name.to_sym ] = SOCMaker::CoreInst.new( name+version )
115
  end
116
 
117
 
118
  def ifc_in_use?( inst_name, ifc_name )
119
 
120
    # go through all connections and check,
121
    # that non of the interfaces we want to connect is used
122
    @cons.each do |_con_name, con_def|
123
      return true if con_def[ :mapping ][ 0 ][ inst_name.to_sym] == ifc_name.to_sym
124
      return true if con_def[ :mapping ][ 1 ][ inst_name.to_sym] == ifc_name.to_sym
125
    end
126
    return false
127
 
128
  end
129
 
130
 
131 7 feddischso
  def get_port_len( ifc_name, port_name, inst )
132 6 feddischso
    if @cores[ inst.to_sym ] != nil
133 7 feddischso
      return @cores[ inst ].get_port_len( ifc_name, port_name )
134 6 feddischso
    else
135
      return nil
136
    end
137
  end
138 5 feddischso
 
139
  def get_core_def( inst )
140
      if @cores[ inst.to_sym ] != nil
141
        return @cores[ inst.to_sym ].defn
142
      elsif inst == @name
143
        return self
144
      else
145 6 feddischso
        return nil
146 5 feddischso
      end
147
  end
148
 
149 7 feddischso
 
150 5 feddischso
 
151
 
152 7 feddischso
  def add_to_connection( inst1, ifc1_name, inst2, ifc2_name, con_name )
153
    perr_if( @cons[ con_name.to_sym ]  == nil, "Connection instance #{con_name} not found" )
154
    @cons[ con_name.to_sym ][:mapping][0][ inst1.to_sym ] = ifc1_name.to_sym
155
    @cons[ con_name.to_sym ][:mapping][1][ inst2.to_sym ] = ifc2_name.to_sym
156
  end
157
 
158
 
159
 
160 3 feddischso
  def add_connection( inst1, ifc1_name, inst2, ifc2_name, con_name )
161
 
162 7 feddischso
    if @cores[ con_name.to_sym ] != nil
163
      return nil
164
    elsif @cons[ con_name.to_sym ]  != nil
165
      return add_to_connection( inst1, ifc1_name, inst2, ifc2_name, con_name )
166
    end
167 3 feddischso
 
168
 
169 7 feddischso
    [ [ inst1, ifc1_name ],
170
      [ inst2, ifc2_name ] ].each do |sub_arr|
171
       perr_if( ifc_in_use?( sub_arr[ 0 ], sub_arr[ 1 ] ),
172
           "Interface #{sub_arr[ 1 ]} of instance '#{sub_arr[ 0 ]}' is already in use " )
173 3 feddischso
    end
174
 
175
 
176 7 feddischso
    core_def_1 = get_core_def( inst1 )
177
    core_def_2 = get_core_def( inst2 )
178
 
179
    perr_if( !core_def_1, "Can't find core #{inst1}" )
180
    perr_if( !core_def_2, "Can't find core #{inst2}" )
181 3 feddischso
 
182 5 feddischso
 
183 7 feddischso
    [ [ core_def_1, ifc1_name ],
184
      [ core_def_2, ifc2_name ] ].each do |sub_arr|
185 3 feddischso
        perr_if( sub_arr[ 0 ].interfaces[ sub_arr[ 1 ].to_sym ] == nil,
186 7 feddischso
          "Interface '#{sub_arr[ 1 ]}' dosn't exist in core '#{sub_arr[0].name}' \n" +
187
          "The following interfaces do exist:  '#{sub_arr[0].interfaces.keys}'"  )
188 3 feddischso
    end
189
 
190
    # check name and version of the ifcs which will be connected
191 7 feddischso
    perr_if( core_def_1.interfaces[ ifc1_name.to_sym ].name     !=
192
             core_def_2.interfaces[ ifc2_name.to_sym ].name     ||
193
             core_def_1.interfaces[ ifc1_name.to_sym ].version  !=
194
             core_def_2.interfaces[ ifc2_name.to_sym ].version,
195 3 feddischso
          "Can't connect #{
196 7 feddischso
            core_def_1.interfaces[ ifc1_name.to_sym ].name } - #{
197
            core_def_2.interfaces[ ifc2_name.to_sym ].name } : #{
198
            core_def_1.interfaces[ ifc1_name.to_sym ].version } - #{
199
            core_def_2.interfaces[ ifc2_name.to_sym ].version }" )
200 3 feddischso
 
201
 
202
    @cons[ con_name.to_sym ] = {
203
          :rule    => "or",
204
          :mapping => [ { inst1.to_sym => ifc1_name.to_sym },
205
                        { inst2.to_sym => ifc2_name.to_sym } ] }
206 5 feddischso
    return false
207 3 feddischso
  end
208
 
209
  def set_param( instance, param, value )
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
 
216
    # get the core-definition
217
    core_def = SOCMaker::lib.get_core( core_inst.type  )
218
 
219
    # check if parameter exist
220
    if core_def.inst_parameters[ param.to_sym ] != nil
221
      core_inst.params[ param.to_sym ] = value
222
    else
223
      perr_if( true,
224
        "Parameter '#{param}' not found in '#{core_def.name}'" )
225
    end
226
 
227
  end
228
 
229
  def get_param( instance, param )
230
 
231
    # get instance
232
    core_inst = @cores[ instance.to_sym ]
233
    perr_if( core_inst == nil,
234
      "Can't find '#{instance}' in SOC" )
235
    param_val = core_inst.params[ param.to_sym ]
236
    perr_if( param_val == nil,
237
      "Can't find parameter '#{param}' in '#{instance}'" )
238
    return param_val
239
  end
240
 
241
 
242
  def set_sparam( core, param, value )
243
 
244
    #get instance
245
 
246
    # check, if we are instantiating this core
247
    perr_if( @cores.select{ |name,inst| inst.type == core }.size == 0,
248
      "Core '#{core}' is not instantiated in this SOC" )
249
 
250
    # get the core-definition
251
    core_def = SOCMaker::lib.get_core( core )
252
 
253
    # check if parameter exist
254
    perr_if( core_def.static_parameters.select{ |f,p| p.parameters[ param.to_sym ] != nil }.size == 0,
255
        "Parameter '#{param}' not found in '#{core_def.name}'" )
256
 
257
    @static[ core.to_sym ] ||= {}
258
    @static[ core.to_sym ][ param.to_sym ] = value
259
  end
260
 
261
  def get_sparam( core, param )
262
    perr_if( @static[ core.to_sym ] == nil,
263
      "Core '#{core}' does not exist in this SOC" )
264
 
265
    perr_if( @static[ core.to_sym ][ param.to_sym ] == nil,
266
      "Parameter '#{param}' does not exist for core '#{core}'" )
267
 
268
    return @static[ core.to_sym ][ param.to_sym ]
269
  end
270
 
271
 
272
  def copy_files
273
 
274
    SOCMaker::logger.proc( "START of copying all HDL files" )
275
 
276
    #
277
    # Create a unique list of cores and
278
    # for every core, create a directory and copy files
279
    #
280
    @cores.values.uniq{|x| x.type }.each do |core_inst; core_def, dst_dir|
281
 
282
      core_def = SOCMaker::lib.get_core( core_inst.type )
283
 
284
      # create destination directory name and ensure, that it is exist
285
      dst_dir  = get_and_ensure_dst_dir!( core_def.name )
286
 
287
      # copy each file into destination dir
288
      core_def.hdlfiles.each do |file, val|
289
        file_path = File.join( core_def.dir, val.path )
290 5 feddischso
        dst_path = File.join( dst_dir, val.path )
291 3 feddischso
        SOCMaker::logger.proc( "copy #{file_path} to #{ dst_path} " )
292 5 feddischso
        FileUtils.mkdir_p(File.dirname(dst_path))
293 3 feddischso
        FileUtils.cp( file_path, dst_path )
294
      end
295
 
296
 
297
 
298
      #
299
      # handle the static parameters
300
      #   (search and replace in pakckage/include files)
301
      core_def.static_parameters.each do |file, param|
302
 
303
        token_val_map = {}
304
        param.parameters.each do |n,p|
305
 
306
          if  @static[ core_inst.type.to_sym ]      != nil and
307
              @static[ core_inst.type.to_sym ][ n ] != nil
308
 
309
            # use value defined in soc-spec
310
            token_val_map[ p.token ] = @static[ core_inst.type.to_sym ][ n ]
311
          else
312
            # use default value from core-spec
313
            token_val_map[ p.token ] =  p.default
314
          end
315
 
316
        end
317
 
318
        # create file paths
319
        src_path = File.join( core_def.dir, param.path )
320
        dst_dir  = get_and_ensure_dst_dir!( core_def.name )
321
        dst_path = File.join( dst_dir, param.file_dst )
322
 
323
 
324
        # process each line of input file
325
        # and replace tokens by value via
326
        # regular expression
327
        File.open( dst_path, 'w' ) do |dst_f|
328
          File.open( src_path ) do |src_f|
329
            SOCMaker::logger.proc( "create #{dst_path} from #{ src_path} " )
330
            while line = src_f.gets
331
              token_val_map.each { |token, val| line = line.sub( Regexp.new( token.to_s ), val.to_s ) }
332
              dst_f.puts line
333
            end
334
          end
335
        end
336
 
337
 
338
 
339
      end
340
 
341
    end
342
 
343
 
344
    SOCMaker::logger.proc( "END of copying all HDL files" )
345
  end
346
 
347
 
348 6 feddischso
 
349 3 feddischso
  def ==(o)
350
    o.class   == self.class   &&
351
    o.cores   == self.cores   &&
352
    o.cons    == self.cons    &&
353
    o.static  == self.static  &&
354
    super( o )
355
  end
356
 
357
 
358 5 feddischso
  def to_s
359
 
360
    tmp = "_________ SOC #{@name}: _______\n"     +
361
          super                                   +
362
          "\n__connections__\n"
363
 
364
    @cons.each do |_con_name, con_def|
365
      tmp += "#{_con_name}: #{con_def}\n"
366
    end
367
 
368
    tmp += "\n__cores__\n"
369
    @cores.each do |inst_name, inst|
370
      tmp += "#{inst_name}:\n#{inst}\n"
371
    end
372
    tmp += "'''''''''''''''''''''''''''''''''''\n"
373
    return tmp
374
  end
375
 
376
 
377 3 feddischso
end # class SOCSpec
378
end # module SOCMaker
379
 
380
 
381
# 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.