HDLRuby 2.11.11 → 2.11.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +55 -18
  3. data/ext/hruby_sim/hruby_rcsim_build.c +27 -0
  4. data/ext/hruby_sim/hruby_sim.h +3 -0
  5. data/ext/hruby_sim/hruby_sim_core.c +17 -5
  6. data/ext/hruby_sim/hruby_sim_stack_calc.c +1 -1
  7. data/ext/hruby_sim/hruby_sim_tree_calc.c +8 -1
  8. data/ext/hruby_sim/hruby_sim_vcd.c +24 -7
  9. data/ext/hruby_sim/hruby_sim_vizualize.c +9 -1
  10. data/lib/HDLRuby/hdr_samples/constant_in_function.rb +3 -1
  11. data/lib/HDLRuby/hdr_samples/counter_dff_bench.rb +3 -1
  12. data/lib/HDLRuby/hdr_samples/huge_rom.rb +1 -1
  13. data/lib/HDLRuby/hdr_samples/mei8.rb +11 -11
  14. data/lib/HDLRuby/hdr_samples/mei8_bench.rb +11 -11
  15. data/lib/HDLRuby/hdr_samples/neg_arith_bench.rb +4 -4
  16. data/lib/HDLRuby/hdr_samples/rom_nest.rb +1 -1
  17. data/lib/HDLRuby/hdr_samples/ruby_fir_hw.rb +4 -4
  18. data/lib/HDLRuby/hdr_samples/struct.rb +44 -10
  19. data/lib/HDLRuby/hdr_samples/with_bram.rb +45 -0
  20. data/lib/HDLRuby/hdr_samples/with_casts.rb +3 -3
  21. data/lib/HDLRuby/hdr_samples/with_concat.rb +6 -6
  22. data/lib/HDLRuby/hdr_samples/with_connector_memory.rb +2 -2
  23. data/lib/HDLRuby/hdr_samples/with_def.rb +10 -3
  24. data/lib/HDLRuby/hdr_samples/with_define_operator.rb +44 -0
  25. data/lib/HDLRuby/hdr_samples/with_fixpoint.rb +12 -12
  26. data/lib/HDLRuby/hdr_samples/with_init.rb +3 -3
  27. data/lib/HDLRuby/hdr_samples/with_leftright.rb +21 -0
  28. data/lib/HDLRuby/hdr_samples/with_reduce.rb +13 -13
  29. data/lib/HDLRuby/hdr_samples/with_ref_array.rb +6 -6
  30. data/lib/HDLRuby/hdr_samples/with_subsums.rb +3 -3
  31. data/lib/HDLRuby/hdr_samples/with_terminate.rb +3 -3
  32. data/lib/HDLRuby/hdr_samples/with_to_a.rb +10 -10
  33. data/lib/HDLRuby/hdr_samples/with_values.rb +3 -3
  34. data/lib/HDLRuby/hdrcc.rb +14 -1
  35. data/lib/HDLRuby/hruby_bstr.rb +10 -5
  36. data/lib/HDLRuby/hruby_high.rb +114 -27
  37. data/lib/HDLRuby/hruby_low.rb +187 -16
  38. data/lib/HDLRuby/hruby_low2c.rb +71 -11
  39. data/lib/HDLRuby/hruby_low2vhd.rb +2 -1
  40. data/lib/HDLRuby/hruby_low_fix_types.rb +1 -0
  41. data/lib/HDLRuby/hruby_low_mutable.rb +30 -1
  42. data/lib/HDLRuby/hruby_low_resolve.rb +15 -2
  43. data/lib/HDLRuby/hruby_low_without_concat.rb +28 -8
  44. data/lib/HDLRuby/hruby_low_without_parinseq.rb +14 -4
  45. data/lib/HDLRuby/hruby_low_without_select.rb +2 -2
  46. data/lib/HDLRuby/hruby_low_without_subsignals.rb +279 -0
  47. data/lib/HDLRuby/hruby_rcsim.rb +80 -71
  48. data/lib/HDLRuby/hruby_rsim.rb +132 -7
  49. data/lib/HDLRuby/hruby_rsim_vcd.rb +99 -27
  50. data/lib/HDLRuby/hruby_values.rb +35 -31
  51. data/lib/HDLRuby/std/bram.rb +22 -0
  52. data/lib/HDLRuby/std/fixpoint.rb +2 -2
  53. data/lib/HDLRuby/std/fsm.rb +20 -3
  54. data/lib/HDLRuby/std/function_generator.rb +2 -2
  55. data/lib/HDLRuby/version.rb +1 -1
  56. metadata +7 -3
  57. data/lib/HDLRuby/hdr_samples/sumprod.rb +0 -29
@@ -26,6 +26,14 @@ module HDLRuby::High
26
26
  return HDLRuby::Infinity
27
27
  end
28
28
 
29
+ # Reimplementation of the Proc's curry that transmit the context for
30
+ # execution.
31
+ def curry_with_context(*args,&ruby_block)
32
+ return proc do |cxt,*new_args|
33
+ cxt.instance_exec(*(args+new_args),&ruby_block)
34
+ end
35
+ end
36
+
29
37
 
30
38
 
31
39
  ##
@@ -38,7 +46,7 @@ module HDLRuby::High
38
46
  # puts "eigen_extend for #{self} class=#{self.class}"
39
47
  obj.singleton_methods.each do |name|
40
48
  next if name == :yaml_tag # Do not know why we need to skip
41
- puts "name=#{name}"
49
+ # puts "name=#{name}"
42
50
  self.define_singleton_method(name, &obj.singleton_method(name))
43
51
  end
44
52
  end
@@ -403,6 +411,11 @@ module HDLRuby::High
403
411
  return @scope
404
412
  end
405
413
 
414
+ # Converts to a new reference.
415
+ def to_ref
416
+ return RefObject.new(this,self)
417
+ end
418
+
406
419
  # Creates and adds a set of inputs typed +type+ from a list of +names+.
407
420
  #
408
421
  # NOTE: a name can also be a signal, is which case it is duplicated.
@@ -1397,6 +1410,7 @@ module HDLRuby::High
1397
1410
  # Merge the included systems interface in +systemT+
1398
1411
  # NOTE: incompatible with further to_low transformation.
1399
1412
  def merge_included(systemT)
1413
+ # puts "merge_included for scope=#{self.name} with behaviors=#{self.each_behavior.count}"
1400
1414
  # Recurse on the sub.
1401
1415
  self.each_scope {|scope| scope.merge_included(systemT) }
1402
1416
  # Include for current scope.
@@ -1437,6 +1451,8 @@ module HDLRuby::High
1437
1451
  end
1438
1452
  # Adds its subscopes.
1439
1453
  included.scope.each_scope do |scope|
1454
+ # Do not override scopes with same name since it is prioritary!
1455
+ next if !scope.name.empty? && systemT.scope.each_scope.find {|sc| sc.name == scope.name}
1440
1456
  scope.no_parent!
1441
1457
  systemT.scope.add_scope(scope)
1442
1458
  end
@@ -1709,10 +1725,27 @@ module HDLRuby::High
1709
1725
  # Set the new method for the operator.
1710
1726
  self.define_singleton_method(comp_operator(operator)) do |*args|
1711
1727
  # puts "Top user=#{HDLRuby::High.top_user}"
1712
- HDLRuby::High.top_user.instance_exec do
1713
- sub do
1714
- HDLRuby::High.top_user.instance_exec(*args,&ruby_block)
1715
- end
1728
+ HDLRuby::High.top_user.sub(HDLRuby.uniq_name) do
1729
+ ruby_block.call(*args)
1730
+ end
1731
+ end
1732
+ end
1733
+
1734
+ # Redefinition of +operator+ when requiring the context to be passed
1735
+ # as argument (normally only used internally).
1736
+ def define_operator_with_context(operator,&ruby_block)
1737
+ # Ensure there is a block.
1738
+ ruby_block = proc {} unless block_given?
1739
+ # Register the operator as overloaded.
1740
+ @overloads ||= {}
1741
+ @overloads[operator] = ruby_block
1742
+ # Set the new method for the operator.
1743
+ self.define_singleton_method(comp_operator(operator)) do |*args|
1744
+ # puts "Top user=#{HDLRuby::High.top_user}"
1745
+ HDLRuby::High.top_user.sub(HDLRuby.uniq_name) do
1746
+ # It is assumed that the first argument of the ruby_block
1747
+ # is the context in which it must be executed.
1748
+ ruby_block.call(self,*args)
1716
1749
  end
1717
1750
  end
1718
1751
  end
@@ -1945,7 +1978,8 @@ module HDLRuby::High
1945
1978
  gtype)
1946
1979
  # Adds the possible overloaded operators.
1947
1980
  self.each_overload do |op,ruby_block|
1948
- gtype.define_operator(op,&(ruby_block.curry[*args]))
1981
+ # gtype.define_operator(op,&(ruby_block.curry[*args]))
1982
+ gtype.define_operator_with_context(op,&(High.curry_with_context(*args,&ruby_block)))
1949
1983
  end
1950
1984
  # Returns the resulting type
1951
1985
  return gtype
@@ -2051,10 +2085,10 @@ module HDLRuby::High
2051
2085
 
2052
2086
  # Converts the type to HDLRuby::Low and set its +name+.
2053
2087
  def to_low(name = self.name)
2054
- # return HDLRuby::Low::TypeTuple.new(name,self.direction,
2055
- # *@types.map { |type| type.to_low } )
2056
- typeTupleL = HDLRuby::Low::TypeTuple.new(name,self.direction,
2057
- *@types.map { |type| type.to_low } )
2088
+ # typeTupleL = HDLRuby::Low::TypeTuple.new(name,self.direction,
2089
+ # *@types.map do |typ| typ.to_low )
2090
+ typeTupleL = HDLRuby::Low::TypeTuple.new(name,self.direction)
2091
+ @types.each { |typ| typeTupleL.add_type(typ.to_low) }
2058
2092
  # # For debugging: set the source high object
2059
2093
  # typeTupleL.properties[:low2high] = self.hdr_id
2060
2094
  # self.properties[:high2low] = typeTupleL
@@ -2116,6 +2150,7 @@ module HDLRuby::High
2116
2150
  gtype = type.generate(*args)
2117
2151
  # And add it as a local type of the system.
2118
2152
  HDLRuby::High.top_user.add_type(gtype)
2153
+ gtype
2119
2154
  end
2120
2155
  end
2121
2156
  else
@@ -3208,6 +3243,7 @@ module HDLRuby::High
3208
3243
  # expr.to_low
3209
3244
  # end
3210
3245
  # )
3246
+ i = 0
3211
3247
  concatL = HDLRuby::Low::Concat.new(self.type.to_low,
3212
3248
  self.each_expression.map do |expr|
3213
3249
  expr.to_low
@@ -3292,7 +3328,8 @@ module HDLRuby::High
3292
3328
 
3293
3329
  # Converts to a new event.
3294
3330
  def to_event
3295
- return Event.new(:change,self.to_ref)
3331
+ # return Event.new(:change,self.to_ref)
3332
+ return Event.new(:anyedge,self.to_ref)
3296
3333
  end
3297
3334
 
3298
3335
  # Iterate over the elements.
@@ -3370,8 +3407,19 @@ module HDLRuby::High
3370
3407
  def to_low
3371
3408
  # puts "to_low with base=#{@base} @object=#{@object}"
3372
3409
  # puts "@object.name=#{@object.name}"
3373
- refNameL = HDLRuby::Low::RefName.new(self.type.to_low,
3410
+ if @base.is_a?(RefThis) &&
3411
+ (@object.parent != High.top_user) &&
3412
+ (@object.parent != High.cur_system) &&
3413
+ (!@object.parent.name.empty?) then
3414
+ # Need to have a hierachical access.
3415
+ # puts "Indirect access for #{self.object.name}: #{self.object.parent.name}(#{self.object.parent.class}) != #{High.cur_system.name}(#{High.top_user.class})"
3416
+ refNameL = HDLRuby::Low::RefName.new(self.type.to_low,
3417
+ @object.parent.to_ref.to_low,@object.name)
3418
+ else
3419
+ # Direct access is enough.
3420
+ refNameL = HDLRuby::Low::RefName.new(self.type.to_low,
3374
3421
  @base.to_ref.to_low,@object.name)
3422
+ end
3375
3423
  # # For debugging: set the source high object
3376
3424
  # refNameL.properties[:low2high] = self.hdr_id
3377
3425
  # self.properties[:high2low] = refNameL
@@ -3801,11 +3849,15 @@ module HDLRuby::High
3801
3849
 
3802
3850
  # Hierarchical type allows access to sub references, so generate
3803
3851
  # the corresponding methods.
3852
+ # For that first get the real type.
3853
+ type = type.def while type.is_a?(TypeDef)
3854
+ # Now process it if it is a structured type.
3804
3855
  if type.struct? then
3805
3856
  type.each_name do |name|
3857
+ sig = SignalI.new(name,type.get_type(name),dir)
3858
+ self.add_signal(sig)
3806
3859
  self.define_singleton_method(name) do
3807
- RefObject.new(self.to_ref,
3808
- SignalI.new(name,type.get_type(name),dir))
3860
+ RefObject.new(self.to_ref,sig)
3809
3861
  end
3810
3862
  end
3811
3863
  end
@@ -3838,17 +3890,41 @@ module HDLRuby::High
3838
3890
 
3839
3891
  # Creates a positive edge event from the signal.
3840
3892
  def posedge
3841
- return Event.new(:posedge,self.to_ref)
3893
+ # return Event.new(:posedge,self.to_ref)
3894
+ # Is there any sub signals?
3895
+ if self.each_signal.any? then
3896
+ # Yes, make events with them instead.
3897
+ return self.each_signal.map { |sig| sig.posedge }
3898
+ else
3899
+ # No, create a single event.
3900
+ return Event.new(:posedge,self.to_ref)
3901
+ end
3842
3902
  end
3843
3903
 
3844
3904
  # Creates a negative edge event from the signal.
3845
3905
  def negedge
3846
- return Event.new(:negedge,self.to_ref)
3906
+ # return Event.new(:negedge,self.to_ref)
3907
+ # Is there any sub signals?
3908
+ if self.each_signal.any? then
3909
+ # Yes, make events with them instead.
3910
+ return self.each_signal.map { |sig| sig.negedge }
3911
+ else
3912
+ # No, create a single event.
3913
+ return Event.new(:negedge,self.to_ref)
3914
+ end
3847
3915
  end
3848
3916
 
3849
3917
  # Creates an edge event from the signal.
3850
- def edge
3851
- return Event.new(:edge,self.to_ref)
3918
+ def anyedge
3919
+ # return Event.new(:edge,self.to_ref)
3920
+ # Is there any sub signals?
3921
+ if self.each_signal.any? then
3922
+ # Yes, make events with them instead.
3923
+ return self.each_signal.map { |sig| sig.anyedge }
3924
+ else
3925
+ # No, create a single event.
3926
+ return Event.new(:anyedge,self.to_ref)
3927
+ end
3852
3928
  end
3853
3929
 
3854
3930
  # Converts to a new reference.
@@ -3871,6 +3947,10 @@ module HDLRuby::High
3871
3947
  # return HDLRuby::Low::SignalI.new(name,self.type.to_low)
3872
3948
  valueL = self.value ? self.value.to_low : nil
3873
3949
  signalIL = HDLRuby::Low::SignalI.new(name,self.type.to_low,valueL)
3950
+ # Recurse on the sub signals if any.
3951
+ self.each_signal do |sig|
3952
+ signalIL.add_signal(sig.to_low)
3953
+ end
3874
3954
  # # For debugging: set the source high object
3875
3955
  # signalIL.properties[:low2high] = self.hdr_id
3876
3956
  # self.properties[:high2low] = signalIL
@@ -3937,6 +4017,10 @@ module HDLRuby::High
3937
4017
  # self.value.to_low)
3938
4018
  signalCL = HDLRuby::Low::SignalC.new(name,self.type.to_low,
3939
4019
  self.value.to_low)
4020
+ # Recurse on the sub signals if any.
4021
+ self.each_signal do |sig|
4022
+ signalCL.add_signal(sig.to_low)
4023
+ end
3940
4024
  # # For debugging: set the source high object
3941
4025
  # signalCL.properties[:low2high] = self.hdr_id
3942
4026
  # self.properties[:high2low] = signalCL
@@ -4062,7 +4146,8 @@ module HDLRuby::High
4062
4146
  # +ruby_block+.
4063
4147
  #
4064
4148
  # NOTE: the else part is defined through the helse method.
4065
- def hif(condition, mode = nil, &ruby_block)
4149
+ # def hif(condition, mode = nil, &ruby_block)
4150
+ def hif(condition, mode = self.mode, &ruby_block)
4066
4151
  # Ensure there is a block.
4067
4152
  ruby_block = proc {} unless block_given?
4068
4153
  # Creates the if statement.
@@ -4327,8 +4412,8 @@ module HDLRuby::High
4327
4412
  # @location = caller_locations
4328
4413
  # Sets the current behavior
4329
4414
  @@cur_behavior = self
4330
- # Add the events.
4331
- events.each { |event| self.add_event(event) }
4415
+ # Add the events (they may be hierarchical to flatten)
4416
+ events.flatten.each { |event| self.add_event(event) }
4332
4417
  # Create and add the block.
4333
4418
  self.block = High.make_block(mode,&ruby_block)
4334
4419
  # Unset the current behavior
@@ -4982,19 +5067,21 @@ module HDLRuby::High
4982
5067
  def to_value
4983
5068
  str = self.to_s
4984
5069
  return nil if str[0] != "_" # Bit string are prefixed by "_"
4985
- # Remove the "_" not needed any longer.
4986
- str = str[1..-1]
4987
5070
  # Get and check the type
4988
- type = str[0]
4989
- if type == "0" or type == "1" or type == "z" or type == "Z" then
5071
+ # type = str[0]
5072
+ type = str[1]
5073
+ if ["0", "1", "z", "Z", "o", "d", "h"].include?(type) then
4990
5074
  # Default binary
4991
5075
  type = "b"
4992
5076
  else
4993
5077
  # Not a default type
4994
- str = str[1..-1]
5078
+ # str = str[1..-1]
5079
+ str = str[2..-1]
4995
5080
  end
4996
- return nil if str.empty?
4997
5081
  return nil unless ["b","u","s"].include?(type)
5082
+ # Remove the "_"
5083
+ str = str.delete("_")
5084
+ return nil if str.empty?
4998
5085
  # Get the width if any.
4999
5086
  if str[0].match(/[0-9]/) then
5000
5087
  width = str.scan(/[0-9]*/)[0]
@@ -850,6 +850,7 @@ module HDLRuby::Low
850
850
 
851
851
  # Adds inner signal +signal+.
852
852
  def add_inner(signal)
853
+ # puts "adding inner signal: #{signal.name}(#{signal})"
853
854
  # Check and add the signal.
854
855
  unless signal.is_a?(SignalI)
855
856
  raise AnyError,
@@ -1632,6 +1633,112 @@ module HDLRuby::Low
1632
1633
 
1633
1634
  alias_method :each_deep, :each_type_deep
1634
1635
 
1636
+ # Tells if the type signed.
1637
+ def signed?
1638
+ return @def.signed?
1639
+ end
1640
+
1641
+ # Tells if the type is unsigned.
1642
+ def unsigned?
1643
+ return @def.unsigned?
1644
+ end
1645
+
1646
+ # Tells if the type is fixed point.
1647
+ def fixed?
1648
+ return @def.fixed?
1649
+ end
1650
+
1651
+ # Tells if the type is floating point.
1652
+ def float?
1653
+ return @def.float?
1654
+ end
1655
+
1656
+ # Tells if the type is a leaf.
1657
+ def leaf?
1658
+ return @def.leaf?
1659
+ end
1660
+
1661
+ # Tells if the type of of vector kind.
1662
+ def vector?
1663
+ return @def.vector?
1664
+ end
1665
+
1666
+ # Gets the bitwidth of the type, by default 0.
1667
+ # Bit, signed, unsigned and Float base have a width of 1.
1668
+ def width
1669
+ return @def.width
1670
+ end
1671
+
1672
+ # Gets the type max value if any.
1673
+ # Default: not defined.
1674
+ def max
1675
+ return @def.max
1676
+ end
1677
+
1678
+ # Gets the type min value if any.
1679
+ # Default: not defined.
1680
+ def min
1681
+ return @def.min
1682
+ end
1683
+
1684
+ # Get the direction of the type, little or big endian.
1685
+ def direction
1686
+ return @def.direction
1687
+ end
1688
+
1689
+ # Tells if the type has a range.
1690
+ def range?
1691
+ return @def.range?
1692
+ end
1693
+
1694
+ # Gets the range of the type, by default range is not defined.
1695
+ def range
1696
+ return @def.range
1697
+ end
1698
+
1699
+ # Tells if the type has a base.
1700
+ def base?
1701
+ return @def.base?
1702
+ end
1703
+
1704
+ # Gets the base type, by default base type is not defined.
1705
+ def base
1706
+ return @def.base
1707
+ end
1708
+
1709
+ # Tells if the type has sub types.
1710
+ def types?
1711
+ return @def.types?
1712
+ end
1713
+
1714
+ # Tells if the type is regular (applies for tuples).
1715
+ def regular?
1716
+ return @def.regular?
1717
+ end
1718
+
1719
+ # Tells if the type has named sub types.
1720
+ def struct?
1721
+ return @def.struct?
1722
+ end
1723
+
1724
+ # Tells if the type is hierarchical.
1725
+ def hierarchical?
1726
+ return @def.hierarchical?
1727
+ end
1728
+
1729
+ # Tell if +type+ is equivalent to current type.
1730
+ #
1731
+ # NOTE: type can be compatible while not being equivalent, please
1732
+ # refer to `hruby_types.rb` for type compatibility.
1733
+ def equivalent?(type)
1734
+ return @def.equivalent?(type)
1735
+ end
1736
+
1737
+ # Converts to a bit vector.
1738
+ def to_vector
1739
+ return @def.to_vector
1740
+ end
1741
+
1635
1742
  end
1636
1743
 
1637
1744
 
@@ -1925,7 +2032,7 @@ module HDLRuby::Low
1925
2032
  def each(&ruby_block)
1926
2033
  # No ruby block? Return an enumerator.
1927
2034
  return to_enum(:each) unless ruby_block
1928
- # A ruby block? Apply it on each input signal instance.
2035
+ # A ruby block? Apply it on each sub name/type pair.
1929
2036
  @types.each(&ruby_block)
1930
2037
  end
1931
2038
 
@@ -1935,7 +2042,7 @@ module HDLRuby::Low
1935
2042
  def each_type(&ruby_block)
1936
2043
  # No ruby block? Return an enumerator.
1937
2044
  return to_enum(:each_type) unless ruby_block
1938
- # A ruby block? Apply it on each input signal instance.
2045
+ # A ruby block? Apply it on each sub type.
1939
2046
  @types.each(&ruby_block)
1940
2047
  end
1941
2048
 
@@ -2021,16 +2128,18 @@ module HDLRuby::Low
2021
2128
  ##
2022
2129
  # Describes a structure type.
2023
2130
  class TypeStruct < Type
2024
- # Creates a new structure type named +name+ with +direction+ and
2131
+ attr_reader :direction
2132
+
2133
+ # Creates a new structure type named +name+ with direction +dir+ and
2025
2134
  # whose hierachy is given by +content+.
2026
- def initialize(name,direction,content)
2135
+ def initialize(name,dir,content)
2027
2136
  # Initialize the type.
2028
2137
  super(name)
2029
2138
 
2030
2139
  # Set the direction.
2031
- @direction = direction.to_sym
2140
+ @direction = dir.to_sym
2032
2141
  unless [:little, :big].include?(@direction)
2033
- raise AnyError, "Invalid direction for a type: #{direction}"
2142
+ raise AnyError, "Invalid direction for a type: #{dir}"
2034
2143
  end
2035
2144
 
2036
2145
  # Check and set the content.
@@ -2046,7 +2155,8 @@ module HDLRuby::Low
2046
2155
  # Comparison for hash: structural comparison.
2047
2156
  def eql?(obj)
2048
2157
  # General type comparison.
2049
- return false unless super(obj)
2158
+ # return false unless super(obj)
2159
+ return false unless obj.is_a?(TypeStruct)
2050
2160
  # Specific comparison.
2051
2161
  idx = 0
2052
2162
  obj.each_key do |name|
@@ -2078,8 +2188,13 @@ module HDLRuby::Low
2078
2188
  end
2079
2189
 
2080
2190
  # Gets a sub type by +name+.
2191
+ # NOTE: +name+ can also be an index.
2081
2192
  def get_type(name)
2082
- return @types[name.to_sym]
2193
+ if name.respond_to?(:to_sym) then
2194
+ return @types[name.to_sym]
2195
+ else
2196
+ return @types.values[name.to_i]
2197
+ end
2083
2198
  end
2084
2199
 
2085
2200
  # Iterates over the sub name/type pair.
@@ -2088,7 +2203,7 @@ module HDLRuby::Low
2088
2203
  def each(&ruby_block)
2089
2204
  # No ruby block? Return an enumerator.
2090
2205
  return to_enum(:each) unless ruby_block
2091
- # A ruby block? Apply it on each input signal instance.
2206
+ # A ruby block? Apply it on each sub name/type pair.
2092
2207
  @types.each(&ruby_block)
2093
2208
  end
2094
2209
 
@@ -2098,17 +2213,27 @@ module HDLRuby::Low
2098
2213
  def each_type(&ruby_block)
2099
2214
  # No ruby block? Return an enumerator.
2100
2215
  return to_enum(:each_type) unless ruby_block
2101
- # A ruby block? Apply it on each input signal instance.
2216
+ # A ruby block? Apply it on each sub type.
2102
2217
  @types.each_value(&ruby_block)
2103
2218
  end
2104
2219
 
2220
+ # Iterates over the keys.
2221
+ #
2222
+ # Returns an enumerator if no ruby block is given.
2223
+ def each_key(&ruby_block)
2224
+ # No ruby block? Return an enumerator.
2225
+ return to_enum(:each_key) unless ruby_block
2226
+ # A ruby block? Apply it on each key.
2227
+ @types.each_key(&ruby_block)
2228
+ end
2229
+
2105
2230
  # Iterates over the sub type names.
2106
2231
  #
2107
2232
  # Returns an enumerator if no ruby block is given.
2108
2233
  def each_name(&ruby_block)
2109
2234
  # No ruby block? Return an enumerator.
2110
2235
  return to_enum(:each_name) unless ruby_block
2111
- # A ruby block? Apply it on each input signal instance.
2236
+ # A ruby block? Apply it on each name.
2112
2237
  @types.each_key(&ruby_block)
2113
2238
  end
2114
2239
 
@@ -2128,7 +2253,11 @@ module HDLRuby::Low
2128
2253
  #
2129
2254
  # NOTE: must be redefined for specific types.
2130
2255
  def width
2131
- return @types.reduce(0) {|sum,type| sum + type.width }
2256
+ if @types.is_a?(Array) then
2257
+ return @types.reduce(0) {|sum,type| sum + type.width }
2258
+ else
2259
+ return @types.each_value.reduce(0) {|sum,type| sum + type.width }
2260
+ end
2132
2261
  end
2133
2262
 
2134
2263
  # # Checks the compatibility with +type+
@@ -2506,8 +2635,9 @@ module HDLRuby::Low
2506
2635
  end
2507
2636
  @value = val
2508
2637
  val.parent = self
2509
- else
2510
- @value = nil
2638
+ # For memory optimization: no initialization if not used.
2639
+ # else
2640
+ # @value = nil
2511
2641
  end
2512
2642
  end
2513
2643
 
@@ -2517,6 +2647,40 @@ module HDLRuby::Low
2517
2647
  false
2518
2648
  end
2519
2649
 
2650
+ # Adds sub signal +sig+
2651
+ def add_signal(sig)
2652
+ # puts "add sub=#{sig.name} in signal=#{self}"
2653
+ # Sets the hash of sub signals if none.
2654
+ @signals = HashName.new unless @signals
2655
+ # Check and add the signal.
2656
+ unless sig.is_a?(SignalI)
2657
+ raise AnyError,
2658
+ "Invalid class for a signal instance: #{sig.class}"
2659
+ end
2660
+ # if @signals.include?(sig) then
2661
+ # raise AnyError, "SignalI #{sig.name} already present."
2662
+ # end
2663
+ # Set its parent.
2664
+ sig.parent = self
2665
+ # And add it
2666
+ @signals.add(sig)
2667
+ end
2668
+
2669
+ # Gets a sub signal by name.
2670
+ def get_signal(name)
2671
+ return @signals ? @signals[name] : nil
2672
+ end
2673
+
2674
+ # Iterates over the sub signals.
2675
+ #
2676
+ # Returns an enumerator if no ruby block is given.
2677
+ def each_signal(&ruby_block)
2678
+ # No ruby block? Return an enumerator.
2679
+ return to_enum(:each_signal) unless ruby_block
2680
+ # A ruby block? Apply it on each sub signal instance if any.
2681
+ @signals.each(&ruby_block) if @signals
2682
+ end
2683
+
2520
2684
  # # Add decorator capability (modifies intialize to put after).
2521
2685
  # include Hdecorator
2522
2686
 
@@ -4717,8 +4881,15 @@ module HDLRuby::Low
4717
4881
  content.is_a?(HDLRuby::BitString)
4718
4882
  # content = HDLRuby::BitString.new(content.to_s)
4719
4883
  content = content.to_s
4720
- if self.type.unsigned? && content[0] != 0 then
4721
- content = "0" + content.rjust(self.type.width,content[0])
4884
+ if self.type.unsigned? && content[0] != "0" then
4885
+ # content = "0" + content.rjust(self.type.width,content[0])
4886
+ if content[0] == "1" then
4887
+ # Do not extend the 1, but 0 instead.
4888
+ content = "0" + content.rjust(self.type.width,"0")
4889
+ else
4890
+ # But extend the other.
4891
+ content = "0" + content.rjust(self.type.width,content[0])
4892
+ end
4722
4893
  end
4723
4894
  content = HDLRuby::BitString.new(content)
4724
4895
  end