Module: Origen::SubBlocks

Includes:
Domains, Parent, Path, RegBaseAddress
Defined in:
lib/origen/sub_blocks.rb

Defined Under Namespace

Modules: Domains, Parent, Path, RegBaseAddress

Instance Method Summary collapse

Methods included from Path

#abs_path, #abs_path=, #path, #path=, #path_var

Methods included from RegBaseAddress

#base_address, #reg_base_address, #reg_base_address_for_domain

Methods included from Parent

#owner=, #parent

Methods included from Domains

#domain, #domain_specified?, #domains

Instance Method Details

#all_sub_blocksObject

Returns an array containing all descendant child objects of the given sub-block, i.e. this returns an array containing children’s children as well

Note that this returns an array instead of a hash since there could be naming collisions in the hash keys



230
231
232
233
234
# File 'lib/origen/sub_blocks.rb', line 230

def all_sub_blocks
  @all_sub_blocks ||= begin
    (sub_blocks_array + sub_blocks_array.map(&:all_sub_blocks)).flatten
  end
end

#delete_sub_blocksObject

Delete all sub_blocks by emptyig the Hash



216
217
218
# File 'lib/origen/sub_blocks.rb', line 216

def delete_sub_blocks
  @sub_blocks = {}
end

#init_sub_blocks(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This will be called whenever an object that includes this module is instantiated



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/origen/sub_blocks.rb', line 7

def init_sub_blocks(*args)
  options = args.find { |a| a.is_a?(Hash) }
  if options
    # Using reg_base_address for storage to avoid class with the original Origen base
    # address API, but will accept any of these
    @reg_base_address = options.delete(:reg_base_address) || options.delete(:reg_base_address) ||
                        options.delete(:base_address) || options.delete(:base) || 0
    if options[:_instance]
      if @reg_base_address.is_a?(Array)
        @reg_base_address = @reg_base_address[options[:_instance]]
      elsif options[:base_address_step]
        @reg_base_address = @reg_base_address + (options[:_instance] * options[:base_address_step])
      end
    end
    @domain_names = [options.delete(:domain) || options.delete(:domains)].flatten.compact
    @domain_specified = !@domain_names.empty?
    @path = options.delete(:path)
    @abs_path = options.delete(:abs_path) || options.delete(:absolute_path)
  end
  if is_a?(SubBlock)
    options.each do |k, v|
      send("#{k}=", v)
    end
  end
end

#namespaceObject



305
306
307
# File 'lib/origen/sub_blocks.rb', line 305

def namespace
  self.class.to_s.sub(/::[^:]*$/, '')
end

#owns_registers?Boolean

Returns true if the given sub block owns at least one register

Returns:

  • (Boolean)


237
238
239
240
241
242
243
# File 'lib/origen/sub_blocks.rb', line 237

def owns_registers?
  if regs
    regs.is_a?(Origen::Registers::RegCollection) && !regs.empty?
  else
    false
  end
end

#sub_block(name, options = {}) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/origen/sub_blocks.rb', line 245

def sub_block(name, options = {})
  if i = options.delete(:instances)
    a = []
    options[:_instance] = i
    i.times do |j|
      o = options.dup
      o[:_instance] = j
      a << sub_block("#{name}#{j}", o)
    end
    define_singleton_method "#{name}s" do
      a
    end
    a
  else
    class_name = options.delete(:class_name)
    if class_name
      begin
        klass = eval("::#{namespace}::#{class_name}")
      rescue
        begin
          klass = eval(class_name)
        rescue
          begin
            klass = eval("#{self.class}::#{class_name}")
          rescue
            puts "Could not find class: #{class_name}"
            raise 'Unknown sub block class!'
          end
        end
      end
    else
      klass = Origen::SubBlock
    end
    unless klass.respond_to?(:includes_origen_model)
      puts 'Any class which is to be instantiated as a sub_block must include Origen::Model,'
      puts "add this to #{klass}:"
      puts ''
      puts '  include Origen::Model'
      puts ''
      fail 'Sub block does not include Origen::Model!'
    end
    block = klass.new(options.merge(parent: self, name: name))
    if sub_blocks[name]
      fail "You have already defined a sub-block named #{name} within class #{self.class}"
    else
      sub_blocks[name] = block
      if respond_to?(name)
        # puts "Tried to create a sub-block named #{name} in #{self.class}, but it already has a method with this name!"
        # puts "To avoid confusion rename one of them and try again!"
        # raise "Non-unique sub-block name!"
      else
        define_singleton_method name do
          sub_blocks[name]
        end
      end
    end
    block
  end
end

#sub_blocks(*args) ⇒ Object Also known as: children

Returns a hash containing all immediate children of the given sub-block



206
207
208
209
210
211
212
# File 'lib/origen/sub_blocks.rb', line 206

def sub_blocks(*args)
  if args.empty?
    @sub_blocks ||= {}.with_indifferent_access
  else
    sub_block(*args)
  end
end

#sub_blocks_arrayObject Also known as: children_array



220
221
222
# File 'lib/origen/sub_blocks.rb', line 220

def sub_blocks_array
  sub_blocks.map { |_name, sub_block| sub_block }
end