lib/origen/pins.rb in origen-0.53.0 vs lib/origen/pins.rb in origen-0.53.1

- old
+ new

@@ -691,31 +691,82 @@ Origen.pin_bank.virtual_pin_groups(options) end end alias_method :virtual_pin_group, :virtual_pin_groups + def all_pin_ids(type: nil, **options) + case type + when :power_pin, :power_pins + dut.pins(power_pin: true).map { |n, p| [n, *p.aliases.keys] }.flatten.map { |n| [n, dut.power_pin(n)] }.to_h + when :ground_pin, :ground_pins + dut.pins(ground_pin: true).map { |n, p| [n, *p.aliases.keys] }.flatten.map { |n| [n, dut.ground_pin(n)] }.to_h + when :virtual_pin, :virtual_pins + dut.pins(virtual_pin: true).map { |n, p| [n, *p.aliases.keys] }.flatten.map { |n| [n, dut.virtual_pin(n)] }.to_h + when :other_pin, :other_pins + dut.pins(other_pin: true).map { |n, p| [n, *p.aliases.keys] }.flatten.map { |n| [n, dut.other_pin(n)] }.to_h + else + # Maintain the legacy lookup of power_pin: true, ground_pin: true, etc. + if options[:power_pin] + all_pin_ids(type: :power_pin) + elsif options[:ground_pin] + all_pin_ids(type: :ground_pin) + elsif options[:virtual_pin] + all_pin_ids(type: :virtual_pin) + elsif options[:other_pin] + all_pin_ids(type: :other_pin) + else + dut.pins.map { |n, p| [n, *p.aliases.keys] }.flatten.map { |n| [n, dut.pin(n)] }.to_h + end + end + end + # Permits access via object.pin(x), returns a hash of all pins if no id # is specified. # ==== Examples # $top.pin(:done) # $soc.pin(:port_a1) # pin(:fail) # Access directly from within the module - def pins(id = nil, options = {}, &_block) - id, options = nil, id if id.is_a?(Hash) - if id - pin = Origen.pin_bank.find(id, options) - unless pin - puts <<-END + def pins(*ids, &_block) + options = (ids.pop if ids.last.is_a?(Hash)) || {} + + # Methods may give an ID of nil, which would previously gloss over + # the pin lookup. To maintain backwards compability, reject any nils in the input, + # then proceed as normal. + ids.reject!(&:nil?) + if !ids.empty? || block_given? + pins = [] + + if block_given? + pins += all_pin_ids(options).select { |n, p| yield(n, p) }.values.uniq + end + + ids.each do |id| + if id.is_a?(Regexp) + pins += all_pin_ids(options).select { |n, p| n.to_s =~ id }.values.uniq + else + pin = Origen.pin_bank.find(id, options) + unless pin + puts <<-END You have tried to reference pin :#{id} within #{self.class} but it does not exist, this could be because the pin has not been defined yet or it is an alias that is not available in the current context. If you meant to define the pin then use the add_pin method instead. - END - fail 'Pin not found' + END + fail 'Pin not found' + end + pins << pin + end end - pin + + # Maintain return value of a single pin object if only given a single identifier + if ids.size == 1 && !ids.first.is_a?(Regexp) + pins.first + else + options[:keep_duplicates] ? pins : pins.uniq! + PinCollection.new(self, *pins, options) + end else if options[:power_pin] Origen.pin_bank.power_pins elsif options[:ground_pin] Origen.pin_bank.ground_pins @@ -739,15 +790,16 @@ pins(id, options, &block) end alias_method :power_pin, :power_pins # Equivalent to the pins method but considers ground pins rather than regular pins - def ground_pins(id = nil, options = {}, &block) - id, options = nil, id if id.is_a?(Hash) + def ground_pins(*ids, &block) + options = (ids.pop if ids.last.is_a?(Hash)) || {} + options = { ground_pin: true }.merge(options) - pins(id, options, &block) + pins(*ids, options, &block) end alias_method :ground_pin, :ground_pins # Equivalent to the pins method but considers other pins rather than regular pins def other_pins(id = nil, options = {}, &block)