Class: RubyHDL::High::TypeTuple

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

Overview

Describes a tuple type.

Instance Attribute Summary

Attributes inherited from Type

#name

Instance Method Summary collapse

Methods inherited from Type

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

Methods included from HDLRuby::Tprocess

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

Constructor Details

#initialize(name, direction, *content) ⇒ TypeTuple

Creates a new tuple type named name width direction and whose sub types are given by content.



1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1452

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

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

  # Check and set the content.
  content.each do |sub|
    unless sub.is_a?(Type) then
      raise "Invalid class for a type: #{sub.class}"
    end
  end
  @types = content
end

Instance Method Details

#add_type(type) ⇒ Object

Adds a sub type.



1507
1508
1509
1510
1511
1512
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1507

def add_type(type)
  unless type.is_a?(Type) then
    raise "Invalid class for a type: #{type.class} (#{type})"
  end
  @types << type
end

#base ⇒ Object

Gets the base type.

NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)



1593
1594
1595
1596
1597
1598
1599
1600
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1593

def base
  if regular? then
    # Regular tuple, return the type of its first element.
    return @types[0]
  else
    raise "No base type for type #{self}"
  end
end

#base? ⇒ Boolean

Tells if the type has a base.

NOTE: only if the tuple is regular (i.e., all its sub types are identical)

Returns:

  • (Boolean)


1585
1586
1587
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1585

def base?
  return regular?
end

#direction ⇒ Object

Get the direction of the type, little or big endian.



1564
1565
1566
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1564

def direction
  return @direction
end

#each(&ruby_block) ⇒ Object

Iterates over the sub name/type pair.

Returns an enumerator if no ruby block is given.



1517
1518
1519
1520
1521
1522
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1517

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_type(&ruby_block) ⇒ Object

Iterates over the sub types.

Returns an enumerator if no ruby block is given.



1527
1528
1529
1530
1531
1532
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1527

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(&ruby_block)
end

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

Iterates over the types deeply if any.



1535
1536
1537
1538
1539
1540
1541
1542
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1535

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 { |type| type.each_type_deep(&ruby_block) }
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1472

def eql?(obj)
  # # General type comparison.
  # return false unless super(obj)
  return false unless obj.is_a?(TypeTuple)
  # Specific comparison.
  idx = 0
  obj.each_type do |type|
    return false unless @types[idx].eql?(type)
    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)


1606
1607
1608
1609
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1606

def equivalent?(type)
  return (type.is_a?(TypeTuple) and
          !@types.zip(type.types).index {|t0,t1| !t0.equivalent?(t1) })
end

#get_all_types ⇒ Object

Gets an array containing all the syb types.



1497
1498
1499
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1497

def get_all_types
  return @types.clone
end

#get_type(index) ⇒ Object

Gets a sub type by index.



1502
1503
1504
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1502

def get_type(index)
  return @types[index.to_i]
end

#hash ⇒ Object

Hash function.



1487
1488
1489
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1487

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

#range ⇒ Object

Gets the range of the type.

NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)



1572
1573
1574
1575
1576
1577
1578
1579
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1572

def range
  if regular? then
    # Regular tuple, return its range as if it was an array.
    return 0..@types.size-1
  else
    raise "No range for type #{self}"
  end
end

#regular? ⇒ Boolean

Tell if the tuple is regular, i.e., all its sub types are equivalent.

NOTE: empty tuples are assumed not to be regular.

Returns:

  • (Boolean)


1549
1550
1551
1552
1553
1554
1555
1556
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1549

def regular?
  return false if @types.empty?
  t0 = @types[0]
  @types[1..-1].each do |type|
    return false unless t0.equivalent?(type)
  end
  return true
end

#types? ⇒ Boolean

Tells if the type has sub types.

Returns:

  • (Boolean)


1492
1493
1494
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1492

def types?
  return true
end

#width ⇒ Object

Gets the bitwidth.



1559
1560
1561
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1559

def width
  return @types.reduce(0) { |sum,type| sum + type.width }
end