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

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker/] [lib.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:      spc_lib.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 the library, which holds all
37
#       - cores (core-definitions)
38
#       - interfaces (interface-specifications)
39
#
40
#
41
####
42
#
43
#
44
#
45
###############################################################
46
 
47
module SOCMaker
48
class Lib
49 7 feddischso
  include ERR
50 3 feddischso
 
51
  def initialize
52
 
53
    # will store all cores
54
    @cores_lib      = {}
55
 
56
    # will store the versions of all cores { name => { ver1, ver2, ver3 } }
57
    @cores_ver  = {}
58
 
59
    # will store all interfaces
60
    @ifc_lib      = {}
61
 
62
    # will store the versions of all interfaces { name => { ver1, ver2, ver3 } }
63
    @ifc_ver  = {}
64
 
65
 
66
    # we remember paths, which we've already processed
67
    @path_lut = []
68
 
69
  end
70
 
71
 
72
  def clear
73
    @cores_lib.clear
74
    @cores_ver.clear
75
    @ifc_lib.clear
76
    @ifc_ver.clear
77
    @path_lut.clear
78
  end
79
 
80
 
81
  # refreshes the core library:
82
  # it useses the global configuration entry cores_search_path,
83
  # which defines, where to search for inc_fname (defined in soc_maker_conf.rb) files.
84
  # For each directory, we call process_include
85
  def refresh( paths = nil )
86
 
87
    paths = [ paths ] if paths.is_a?( String )
88
 
89
 
90
    SOCMaker::logger.info  "START REFRESHING CORE LIBRARY"
91
 
92
    # clear the libs
93
    clear
94
 
95
    # use argument if given, otherwise config paths
96
    paths ||= SOCMaker::conf[ :cores_search_path ]
97
 
98
 
99
    paths.each do |dir|
100
      process_include dir
101
    end
102
    SOCMaker::logger.info  "DONE REFRESHING CORE LIBRARY"
103
 
104
  end
105
 
106
 
107
 
108
  def process_include( dir )
109
 
110
    #
111
    # this prevents the revursive call
112
    # from an infinite call
113
    #
114
    folder_sym = File.expand_path( dir ).to_sym
115 7 feddischso
    lerr_if( @path_lut.include?( folder_sym ),
116
        "double-include: infinite resursive search?" )
117
    @path_lut << folder_sym
118 3 feddischso
 
119
    # get all yaml files in the directory
120
    SOCMaker::logger.info  "search for include in: " + dir
121
 
122
 
123
    SOCMaker::from_s( get_all_yaml_in_str( dir ) ) do |o|
124
      o.dir = dir
125
      case o
126
      when SOCMaker::LibInc
127
        add_include( o, dir )
128
      when SOCMaker::CoreDef
129
        add_core( o )
130
      when SOCMaker::SOCDef
131
        add_core( o )
132
      when SOCMaker::IfcSpc
133
        add_ifc( o )
134
      else
135
        #TODO add error
136
      end
137
    end
138
 
139
  end
140
 
141
 
142
 
143
  def get_all_yaml_in_str( dir )
144
    yaml_str = ""
145
    Dir[ File.join( dir, "*.yaml" ) ].sort.each do |yaml_file|
146
      SOCMaker::logger.info "reading:" + yaml_file
147
      yaml_str << File.read( yaml_file )
148
    end
149
    return yaml_str
150
  end
151
 
152
 
153
 
154
  # gets an SOCMaker::LibInc object and iterates
155
  # over all folders.
156
  # Note: this is moved from process_include to this extra function
157
  # to support test capability
158
  def add_include( soc_inc_object, dir )
159
    soc_inc_object.dirs.each { |d| process_include( File.expand_path( File.join( dir, d ) ) ) }
160
  end
161
 
162
  def add_core( core )
163
    # generate key-string from name and vesion
164
    core_key = core.name + core.version
165
 
166
    # save core
167
    @cores_lib[ core_key ] = core
168
    @cores_ver[ core.name.to_sym ] = [] unless @cores_ver.has_key?(core.name.to_sym)
169
    @cores_ver[ core.name.to_sym ] << core.version
170
 
171
    SOCMaker::logger.info  "loaded "     +
172
                            core.name     +
173
                            " version "   +
174
                            core.version
175
  end
176
  def get_core( name, version = "" )
177
    core_key = name + version
178
    tmp = @cores_lib[ core_key ]
179
    check_nil( tmp, "Core '#{name}' version '#{version}' does not exist" )
180
    return tmp
181
  end
182
  def rm_core( core )
183
    core_key = core.name + core.version
184
    @cores_lib.delete( core_key )
185
  end
186
 
187
 
188
  def add_ifc( ifc )
189
    ifc_key = ifc.name + ifc.version
190
    @ifc_lib[ ifc_key         ] = ifc
191
    @ifc_ver[ ifc.name.to_sym ] = [] unless @ifc_ver.has_key?(ifc.name.to_sym)
192
    @ifc_ver[ ifc.name.to_sym ] << ifc.version
193
  end
194
  def get_ifc( name, version = "" )
195
    ifc_key = name + version
196
    tmp = @ifc_lib[ ifc_key ]
197
    check_nil( tmp, "Interface '#{name}' version '#{version}' does not exist" )
198
    return tmp
199
  end
200
  def rm_ifc( ifc )
201
    ifc_key = ifc.name + ifc.version
202
    @ifc_lib.delete( ifc_key )
203
  end
204
 
205
  def to_s
206
      "IP-Core - lib: \n"             +
207
      @cores_lib.keys.to_s            +
208
      "\n\nIP-Core - versions: \n"    +
209
      @cores_ver.to_s                 +
210
      "\n\nInterface - lib: \n"       +
211
      @ifc_lib.keys.to_s              +
212
      "\n\nInterface - versions: \n"  +
213
      @ifc_ver.to_s  + "\n"
214
  end
215
 
216
 
217
 
218
  def check_nil( var, error_msg = "")
219
    if var == nil
220
      SOCMaker::logger.error error_msg
221
      raise SOCMaker::ERR::LibError.new( "", error_msg )
222
    end
223
  end
224
 
225
 
226
 
227
  #
228
  # get all interfaces in a list
229
  #
230
  # TODO untested: do we need this?
231
  def get_ifcs( core )
232
    ifc_list = [];
233
    core.interfaces.values.each do |ifc; ifc_tmp|
234
      ifc_tmp = get_ifc( ifc[ :name ], ifc[ :version ] )
235
 
236
      # error handling
237
      if ifc_tmp == nil
238
        SOCMaker::logger.error  "Can't find #{ifc[ :name ]} version #{ifc[ :version ]} in SOC library"
239
        raise NameError, "Can't find #{ifc[ :name ]} version #{ifc[ :version ]} in SOC library"
240
      end
241
 
242
      # add interface to list
243
      ifc_list << ifc_tmp
244
    end
245
    return ifc_list
246
  end
247
 
248 5 feddischso
 
249
  #
250
  # TODO add test code
251
  #
252
  def cores
253
    @cores_lib.each do |nameversion,core|
254
      yield( nameversion.to_s, core )
255
    end
256
  end
257 3 feddischso
 
258
 
259
end #class Lib
260
end #Module SOCMaker
261
 
262
#
263
# 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.