Class: RubyHDL::High::TypeStruct

Inherits:
Type
  • Object
show all
Defined in:
lib/HDLRuby/std/sequencer_sw.rb

Overview

Describes a struct type.

Instance Attribute Summary collapse

Attributes inherited from Type

#name

Instance Method Summary collapse

Methods inherited from Type

#[], #base, #base?, #binary, #comp_operator, #constant, #define_operator, #define_operator_with_context, #each_overload, #fixed?, #float?, #hierarchical?, #htype?, #inner, #input, #leaf?, #left, #max, #min, #output, #range, #range?, #register, #regular?, #right, #signed?, #to_c, #to_c_init, #to_type, #to_vector, #typedef, #unary, #unsigned?, #vector?

Methods included from HDLRuby::Tprocess

#&, #*, #+, #+@, #-@, #/, #<<, #==, #abs, #lr, #make, #resolve, #slice, #~

Constructor Details

#initialize(name, dir, content) ⇒ TypeStruct

Creates a new structure type named +name+ with direction +dir+ and whose hierachy is given by +content+.



1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1553

def initialize(name,dir,content)
  # Initialize the type.
  super(name)

  # Set the direction.
  @direction = dir.to_sym
  unless [:little, :big].include?(@direction)
    raise "Invalid direction for a type: #{dir}"
  end

  # Check and set the content.
  content = Hash[content]
  @types = content.map do |k,v|
    unless v.is_a?(Type) then
      raise "Invalid class for a type: #{v.class}"
    end
    [ k.to_sym, v ]
  end.to_h
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



1549
1550
1551
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1549

def direction
  @direction
end

Instance Method Details

#each(&ruby_block) ⇒ Object

Iterates over the sub name/type pair.

Returns an enumerator if no ruby block is given.



1621
1622
1623
1624
1625
1626
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1621

def each(&ruby_block)
  # No ruby block? Return an enumerator.
  return to_enum(:each) unless ruby_block
  # A ruby block? Apply it on each sub name/type pair.
  @types.each(&ruby_block)
end

#each_key(&ruby_block) ⇒ Object

Iterates over the keys.

Returns an enumerator if no ruby block is given.



1641
1642
1643
1644
1645
1646
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1641

def each_key(&ruby_block)
  # No ruby block? Return an enumerator.
  return to_enum(:each_key) unless ruby_block
  # A ruby block? Apply it on each key.
  @types.each_key(&ruby_block)
end

#each_name(&ruby_block) ⇒ Object

Iterates over the sub type names.

Returns an enumerator if no ruby block is given.



1651
1652
1653
1654
1655
1656
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1651

def each_name(&ruby_block)
  # No ruby block? Return an enumerator.
  return to_enum(:each_name) unless ruby_block
  # A ruby block? Apply it on each name.
  @types.each_key(&ruby_block)
end

#each_type(&ruby_block) ⇒ Object

Iterates over the sub types.

Returns an enumerator if no ruby block is given.



1631
1632
1633
1634
1635
1636
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1631

def each_type(&ruby_block)
  # No ruby block? Return an enumerator.
  return to_enum(:each_type) unless ruby_block
  # A ruby block? Apply it on each sub type.
  @types.each_value(&ruby_block)
end

#each_type_deep(&ruby_block) ⇒ Object Also known as: each_deep

Iterates over the types deeply if any.



1659
1660
1661
1662
1663
1664
1665
1666
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1659

def each_type_deep(&ruby_block)
  # No ruby block? Return an enumerator.
  return to_enum(:each_type_deep) unless ruby_block
  # A ruby block? First apply it to current.
  ruby_block.call(self)
  # And recurse on the sub types.
  @types.each_value { |type| type.each_type_deep(&ruby_block) }
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1574

def eql?(obj)
  # General type comparison.
  # return false unless super(obj)
  return false unless obj.is_a?(TypeStruct)
  # Specific comparison.
  idx = 0
  obj.each_key do |name|
    return false unless @types[name].eql?(obj.get_type(name))
    idx += 1
  end
  return false unless idx == @types.size
  return true
end

#equivalent?(type) ⇒ Boolean

Tell if +type+ is equivalent to current type.

NOTE: type can be compatible while not being equivalent, please refer to hruby_types.rb for type compatibility.

Returns:

  • (Boolean)


1685
1686
1687
1688
1689
1690
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1685

def equivalent?(type)
  return (type.is_a?(TypeStruct) and
          !@types.to_a.zip(type.types.to_a).index do |t0,t1|
            t0[0] != t1[0] or !t0[1].equivalent?(t1[1])
          end)
end

#get_all_typesObject

Gets an array containing all the syb types.



1604
1605
1606
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1604

def get_all_types
  return @types.values
end

#get_type(name) ⇒ Object

Gets a sub type by +name+. NOTE: +name+ can also be an index.



1610
1611
1612
1613
1614
1615
1616
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1610

def get_type(name)
  if name.respond_to?(:to_sym) then
    return @types[name.to_sym]
  else
    return @types.values[name.to_i]
  end
end

#hashObject

Hash function.



1589
1590
1591
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1589

def hash
  return [super,@types].hash
end

#struct?Boolean

Tells if the type has named sub types.

Returns:

  • (Boolean)


1594
1595
1596
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1594

def struct?
  return true
end

#types?Boolean

Tells if the type has sub types.

Returns:

  • (Boolean)


1599
1600
1601
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1599

def types?
  return true
end

#widthObject

Gets the bitwidth of the type, nil for undefined.

NOTE: must be redefined for specific types.



1673
1674
1675
1676
1677
1678
1679
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1673

def width
  if @types.is_a?(Array) then
    return @types.reduce(0) {|sum,type| sum + type.width }
  else
    return @types.each_value.reduce(0) {|sum,type| sum + type.width }
  end
end