Class: RubyHDL::High::Type

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

Overview

Describes a high-level data type.

NOTE: by default a type is not specified.

Direct Known Subclasses

TypeDef, TypeGen, TypeStruct, TypeTuple, TypeVector

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HDLRuby::Tprocess

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

Constructor Details

#initialize(name = nil) ⇒ Type

Creates a new type named +name+.



750
751
752
753
754
755
756
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 750

def initialize(name = nil)
  if name then
    @name = name.to_sym 
    # Registers the name.
    self.register(name)
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



745
746
747
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 745

def name
  @name
end

Instance Method Details

#[](rng) ⇒ Object

Creates a new vector type of range +rng+ and with current type as base.



959
960
961
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 959

def [](rng)
  return TypeVector.new(:"",self,rng)
end

#baseObject

Gets the base type, by default base type is not defined.



846
847
848
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 846

def base
  raise "No base type for type #{self} (#{self.name})"
end

#base?Boolean

Tells if the type has a base.

Returns:

  • (Boolean)


841
842
843
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 841

def base?
  return false
end

#binary(operator, expr0, expr1) ⇒ Object

Performs binary operation +operator+ on expressions +expr0+ and +expr1+.



1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1018

def binary(operator, expr0, expr1)
  # Look for a specific computation method.
  comp = comp_operator(operator)
  if self.respond_to?(comp) then
    # Found, use it.
    self.send(comp,expr0,expr1)
  else
    # Not found, back to default computation.
    expr0.to_value.send(operator,expr1)
  end
end

#comp_operator(op) ⇒ Object

Gets the computation method for +operator+.



999
1000
1001
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 999

def comp_operator(op)
  return (op.to_s + ":C").to_sym
end

#constant(hsh) ⇒ Object

Declares high-level untyped constant signals by name and value given by +hsh+ of the current type.



992
993
994
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 992

def constant(hsh)
  RubyHDL::High.top_sblock.make_constants(self,hsh)
end

#define_operator(operator, &ruby_block) ⇒ Object

Redefinition of +operator+.



1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1031

def define_operator(operator,&ruby_block)
  # Ensure there is a block.
  ruby_block = proc {} unless block_given?
  # Register the operator as overloaded.
  @overloads ||= {}
  @overloads[operator] = ruby_block
  # Set the new method for the operator.
  self.define_singleton_method(comp_operator(operator)) do |*args|
    # puts "Top user=#{HDLRuby::High.top_user}"
    RubyHDL::High.top_sblock.sub(RubyHDL.uniq_name) do
      ruby_block.call(*args)
    end
  end
end

#define_operator_with_context(operator, &ruby_block) ⇒ Object

Redefinition of +operator+ when requiring the context to be passed as argument (normally only used internally).



1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1048

def define_operator_with_context(operator,&ruby_block)
  # Ensure there is a block.
  ruby_block = proc {} unless block_given?
  # Register the operator as overloaded.
  @overloads ||= {}
  @overloads[operator] = ruby_block
  # Set the new method for the operator.
  self.define_singleton_method(comp_operator(operator)) do |*args|
    # puts "Top user=#{HDLRuby::High.top_user}"
    RubyHDL::High.top_sblock.sub(RubyHDL.uniq_name) do
      # It is assumed that the first argument of the ruby_block
      # is the context in which it must be executed.
      ruby_block.call(self,*args)
    end
  end
end

#directionObject

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



825
826
827
828
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 825

def direction
  # By default, little endian.
  return :little
end

#each_overload(&ruby_block) ⇒ Object

Interates over the overloaded operators.



1066
1067
1068
1069
1070
1071
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1066

def each_overload(&ruby_block)
  # No ruby block? Return an enumerator.
  return to_enum(:each_overload) unless ruby_block
  # A block? Apply it on each overload if any.
  @overloads.each(&ruby_block) if @overloads
end

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

Iterates over the types deeply if any.



880
881
882
883
884
885
886
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 880

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 that's all by default.
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


761
762
763
764
765
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 761

def eql?(obj)
  return false unless obj.is_a?(Type)
  return false unless @name.eql?(obj.name)
  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)


874
875
876
877
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 874

def equivalent?(type)
  # By default, types are equivalent iff they have the same name.
  return (type.is_a?(Type) and self.name == type.name)
end

#fixed?Boolean

Tells if the type is fixed point.

Returns:

  • (Boolean)


783
784
785
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 783

def fixed?
  return false
end

#float?Boolean

Tells if the type is floating point.

Returns:

  • (Boolean)


788
789
790
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 788

def float?
  return false
end

#hashObject

Hash function.



768
769
770
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 768

def hash
  return [@name].hash
end

#hierarchical?Boolean

Tells if the type is hierarchical.

Returns:

  • (Boolean)


866
867
868
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 866

def hierarchical?
  return self.base? || self.types?
end

#htype?Boolean

Tells htype has been included.

Returns:

  • (Boolean)


895
896
897
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 895

def htype?
  return true
end

#inner(*names) ⇒ Object

Declares high-level untyped inner signals named +names+ of the current type.



986
987
988
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 986

def inner(*names)
  RubyHDL::High.top_sblock.make_inners(self,*names)
end

#input(*names) ⇒ Object

Declares high-level input signals named +names+ of the current type.



966
967
968
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 966

def input(*names)
  RubyHDL::High.top_sblock.make_inputs(self,*names)
end

#leaf?Boolean

Tells if the type is a leaf.

Returns:

  • (Boolean)


793
794
795
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 793

def leaf?
  return false
end

#leftObject

Gets the type as left value.

NOTE: used for asymetric types like TypeSystemI.



932
933
934
935
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 932

def left
  # By default self.
  self
end

#maxObject

Gets the type max value if any. Default: not defined.



814
815
816
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 814

def max
  raise "No max value for type #{self} (#{self.name})"
end

#minObject

Gets the type min value if any. Default: not defined.



820
821
822
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 820

def min
  raise "No min value for type #{self} (#{self.name})"
end

#output(*names) ⇒ Object

Declares high-level untyped output signals named +names+ of the current type.



972
973
974
975
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 972

def output(*names)
  # High.top_user.make_outputs(self.instantiate,*names)
  RubyHDL::High.top_sblock.make_outputs(self,*names)
end

#rangeObject

Gets the range of the type, by default range is not defined.



836
837
838
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 836

def range
  raise "No range for type #{self} (#{self.name})"
end

#range?Boolean

Tells if the type has a range.

Returns:

  • (Boolean)


831
832
833
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 831

def range?
  return false
end

#register(name) ⇒ Object

Register the +name+ of the type.



923
924
925
926
927
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 923

def register(name)
  # Sets the hdl-like access to the type.
  obj = self # For using the right self within the proc
  RubyHDL::High.top_sblock.register(name) { obj }
end

#regular?Boolean

Tells if the type is regular (applies for tuples).

Returns:

  • (Boolean)


856
857
858
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 856

def regular?
  return false
end

#rightObject

Gets the type as right value.

NOTE: used for asymetric types like TypeSystemI.



940
941
942
943
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 940

def right
  # By default self.
  self
end

#signed?Boolean

Tells if the type signed.

Returns:

  • (Boolean)


773
774
775
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 773

def signed?
  return false
end

#struct?Boolean

Tells if the type has named sub types.

Returns:

  • (Boolean)


861
862
863
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 861

def struct?
  return false
end

#to_cObject

Convert to C code.



1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1074

def to_c
  case @name
  when :void
    return "void"
  when :bit, :unsigned
    return "unsigned"
  when :signed
    return "signed"
  when :float
    return "double"
  when :string
    return "char*"
  else
    return @name.to_s
  end
end

#to_c_initObject

Convert to C initialization code.



1092
1093
1094
1095
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1092

def to_c_init
  # By default: 0
  return "0"
end

#to_python_initObject

Convert to Python initialization code.



1098
1099
1100
1101
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1098

def to_python_init
  # By default: 0
  return "0"
end

#to_typeObject

Converts to a type. Returns self since it is already a type.



901
902
903
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 901

def to_type
  return self
end

#to_vectorObject

Converts to a bit vector.



890
891
892
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 890

def to_vector
  return TypeVector.new(:"", Bit, self.width-1..0)
end

#typedef(name) ⇒ Object

Declares a new type definition with +name+ equivalent to current one.



948
949
950
951
952
953
954
955
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 948

def typedef(name)
  # Create the new type.
  typ = TypeDef.new(name,self)
  # Register it.
  High.top_sblock.register(name) { typ }
  # Return it.
  return typ
end

#types?Boolean

Tells if the type has sub types.

Returns:

  • (Boolean)


851
852
853
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 851

def types?
  return false
end

#unary(operator, expr) ⇒ Object

Performs unary operation +operator+ on expression +expr+.



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1004

def unary(operator,expr)
  # Look for a specific computation method.
  comp = comp_operator(operator)
  if self.respond_to?(comp) then
    # Found, use it.
    self.send(comp,expr)
  else
    # Not found, back to default computation.
    expr.to_value.send(operator)
  end
end

#unsigned?Boolean

Tells if the type is unsigned.

Returns:

  • (Boolean)


778
779
780
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 778

def unsigned?
  return false
end

#vector?Boolean

Tells if the type of of vector kind.

Returns:

  • (Boolean)


798
799
800
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 798

def vector?
  return false
end

#widthObject

Gets the bitwidth of the type, by default 0. Bit, signed, unsigned and Float base have a width of 1.



804
805
806
807
808
809
810
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 804

def width
  if [:bit, :signed, :unsigned, :float ].include?(@name) then
    return 1
  else
    return 0
  end
end