Class: RubyHDL::High::TypeTuple
- Defined in:
- lib/HDLRuby/std/sequencer_sw.rb
Overview
Describes a tuple type.
Instance Attribute Summary
Attributes inherited from Type
Instance Method Summary collapse
-
#add_type(type) ⇒ Object
Adds a sub +type+.
-
#base ⇒ Object
Gets the base type.
-
#base? ⇒ Boolean
Tells if the type has a base.
-
#direction ⇒ Object
Get the direction of the type, little or big endian.
-
#each(&ruby_block) ⇒ Object
Iterates over the sub name/type pair.
-
#each_type(&ruby_block) ⇒ Object
Iterates over the sub types.
-
#each_type_deep(&ruby_block) ⇒ Object
(also: #each_deep)
Iterates over the types deeply if any.
-
#eql?(obj) ⇒ Boolean
Comparison for hash: structural comparison.
-
#equivalent?(type) ⇒ Boolean
Tell if +type+ is equivalent to current type.
-
#get_all_types ⇒ Object
Gets an array containing all the syb types.
-
#get_type(index) ⇒ Object
Gets a sub type by +index+.
-
#hash ⇒ Object
Hash function.
-
#initialize(name, direction, *content) ⇒ TypeTuple
constructor
Creates a new tuple type named +name+ width +direction+ and whose sub types are given by +content+.
-
#range ⇒ Object
Gets the range of the type.
-
#regular? ⇒ Boolean
Tell if the tuple is regular, i.e., all its sub types are equivalent.
-
#types? ⇒ Boolean
Tells if the type has sub types.
-
#width ⇒ Object
Gets the bitwidth.
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_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+.
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1400 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+.
1455 1456 1457 1458 1459 1460 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1455 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)
1541 1542 1543 1544 1545 1546 1547 1548 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1541 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)
1533 1534 1535 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1533 def base? return regular? end |
#direction ⇒ Object
Get the direction of the type, little or big endian.
1512 1513 1514 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1512 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.
1465 1466 1467 1468 1469 1470 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1465 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.
1475 1476 1477 1478 1479 1480 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1475 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.
1483 1484 1485 1486 1487 1488 1489 1490 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1483 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.
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1420 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.
1554 1555 1556 1557 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1554 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.
1445 1446 1447 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1445 def get_all_types return @types.clone end |
#get_type(index) ⇒ Object
Gets a sub type by +index+.
1450 1451 1452 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1450 def get_type(index) return @types[index.to_i] end |
#hash ⇒ Object
Hash function.
1435 1436 1437 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1435 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)
1520 1521 1522 1523 1524 1525 1526 1527 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1520 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.
1497 1498 1499 1500 1501 1502 1503 1504 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1497 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.
1440 1441 1442 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1440 def types? return true end |
#width ⇒ Object
Gets the bitwidth.
1507 1508 1509 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1507 def width return @types.reduce(0) { |sum,type| sum + type.width } end |