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 9

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 9 feddischso
  def initialize( name, version, toplevel, optional = {} )
55 3 feddischso
 
56
    init_with( { 'name'     => name,
57
                 'version'  => version,
58 9 feddischso
                 'toplevel' => toplevel }.merge( optional ) )
59 3 feddischso
 
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 9 feddischso
  def port_length( ifc_name, port_name, inst )
132 6 feddischso
    if @cores[ inst.to_sym ] != nil
133 9 feddischso
      return @cores[ inst ].port_length( ifc_name, port_name )
134 6 feddischso
    else
135
      return nil
136
    end
137
  end
138 5 feddischso
 
139 9 feddischso
  def core_definition( inst )
140 5 feddischso
      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 9 feddischso
    core_def_1 = core_definition( inst1 )
177
    core_def_2 = core_definition( inst2 )
178 7 feddischso
 
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 8 feddischso
 
277 3 feddischso
    #
278
    # Create a unique list of cores and
279
    # for every core, create a directory and copy files
280
    #
281
    @cores.values.uniq{|x| x.type }.each do |core_inst; core_def, dst_dir|
282
 
283
      core_def = SOCMaker::lib.get_core( core_inst.type )
284
 
285
      # create destination directory name and ensure, that it is exist
286 9 feddischso
      dst_dir  = Component.get_and_ensure_dst_dir!( core_def.name )
287 3 feddischso
 
288
      # copy each file into destination dir
289
      core_def.hdlfiles.each do |file, val|
290
        file_path = File.join( core_def.dir, val.path )
291 5 feddischso
        dst_path = File.join( dst_dir, val.path )
292 3 feddischso
        SOCMaker::logger.proc( "copy #{file_path} to #{ dst_path} " )
293 5 feddischso
        FileUtils.mkdir_p(File.dirname(dst_path))
294 3 feddischso
        FileUtils.cp( file_path, dst_path )
295
      end
296
 
297
 
298
      #
299
      # handle the static parameters
300
      #   (search and replace in pakckage/include files)
301 8 feddischso
      core_def.static_parameters.each do |file, sparam|
302 3 feddischso
 
303
        token_val_map = {}
304 8 feddischso
        sparam.parameters.each do |n,sparam_entry|
305 3 feddischso
 
306
          if  @static[ core_inst.type.to_sym ]      != nil and
307
              @static[ core_inst.type.to_sym ][ n ] != nil
308
            # use value defined in soc-spec
309 8 feddischso
            tmp = @static[ core_inst.type.to_sym ][ n ]
310 3 feddischso
          else
311
            # use default value from core-spec
312 8 feddischso
            tmp =  sparam_entry.default
313 3 feddischso
          end
314
 
315 8 feddischso
          if sparam_entry.type == "enum"
316
            token_val_map[ sparam_entry.token ] = sparam_entry.choice[ tmp ]
317
          elsif sparam_entry.type == "bool"
318
            if tmp == true
319
              token_val_map[ sparam_entry.token ] = sparam_entry.choice
320
            else
321
              token_val_map[ sparam_entry.token ] = ""
322
            end
323
          else
324
            token_val_map[ sparam_entry.token ] = tmp
325
          end
326
 
327
 
328 3 feddischso
        end
329 8 feddischso
 
330 3 feddischso
        # create file paths
331 8 feddischso
        src_path = File.join( core_def.dir, sparam.path )
332 9 feddischso
        dst_dir  = Component::get_and_ensure_dst_dir!( core_def.name )
333 8 feddischso
        dst_path = File.join( dst_dir, sparam.file_dst )
334 3 feddischso
 
335
 
336
        # process each line of input file
337
        # and replace tokens by value via
338
        # regular expression
339
        File.open( dst_path, 'w' ) do |dst_f|
340
          File.open( src_path ) do |src_f|
341
            SOCMaker::logger.proc( "create #{dst_path} from #{ src_path} " )
342
            while line = src_f.gets
343 8 feddischso
              token_val_map.each { |token, val| line = line.sub( Regexp.new( '\b' + token.to_s + '\b' ), val.to_s ) }
344 3 feddischso
              dst_f.puts line
345
            end
346
          end
347
        end
348
 
349
 
350
 
351
      end
352
 
353
    end
354
 
355
 
356
    SOCMaker::logger.proc( "END of copying all HDL files" )
357
  end
358
 
359
 
360 6 feddischso
 
361 3 feddischso
  def ==(o)
362
    o.class   == self.class   &&
363
    o.cores   == self.cores   &&
364
    o.cons    == self.cons    &&
365
    o.static  == self.static  &&
366
    super( o )
367
  end
368
 
369
 
370 5 feddischso
  def to_s
371
 
372
    tmp = "_________ SOC #{@name}: _______\n"     +
373
          super                                   +
374
          "\n__connections__\n"
375
 
376
    @cons.each do |_con_name, con_def|
377
      tmp += "#{_con_name}: #{con_def}\n"
378
    end
379
 
380
    tmp += "\n__cores__\n"
381
    @cores.each do |inst_name, inst|
382
      tmp += "#{inst_name}:\n#{inst}\n"
383
    end
384
    tmp += "'''''''''''''''''''''''''''''''''''\n"
385
    return tmp
386
  end
387
 
388
 
389 3 feddischso
end # class SOCSpec
390
end # module SOCMaker
391
 
392
 
393
# 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.