Class: HDLRuby::High::Scope

Inherits:
Low::Scope show all
Includes:
HScope_missing, Hinner, Hmux, SingletonExtend
Defined in:
lib/HDLRuby/hruby_high.rb

Overview

Describes a scope for a system type

Constant Summary collapse

High =
HDLRuby::High

Constants included from Low::Low2Symbol

Low::Low2Symbol::Low2SymbolPrefix, Low::Low2Symbol::Low2SymbolTable, Low::Low2Symbol::Symbol2LowTable

Instance Attribute Summary collapse

Attributes included from Low::Hparent

#parent

Instance Method Summary collapse

Methods included from Hmux

#mux

Methods included from HScope_missing

#h_missing, #method_missing

Methods included from Hmissing

#method_missing

Methods included from SingletonExtend

#eigen_extend

Methods included from Hinner

included

Methods inherited from Low::Scope

#add_behavior, #add_code, #add_connection, #add_inner, #add_scope, #add_systemI, #add_systemT, #add_type, #blocks2seq!, #boolean_in_assign2select!, #break_concat_assigns!, #break_types!, #c_code_allocate, #cleanup!, #delete_behavior!, #delete_connection!, #delete_inner!, #delete_related!, #delete_scope!, #delete_systemI!, #delete_systemT!, #delete_type!, #delete_unless!, #each_behavior, #each_behavior_deep, #each_block_deep, #each_code, #each_connection, #each_connection_deep, #each_inner, #each_node_deep, #each_scope, #each_scope_deep, #each_signal, #each_signal_deep, #each_statement_deep, #each_systemI, #each_systemT, #each_type, #eql?, #explicit_types!, #extract_behaviors!, #extract_connections!, #extract_declares!, #extract_port_assign!, #get_all_inners, #get_by_name, #get_code, #get_inner, #get_signal, #get_systemI, #get_systemT, #get_type, #has_behavior?, #has_code?, #has_connection?, #has_inner?, #has_scope?, #has_signal?, #has_systemI?, #has_systemT?, #has_type?, #hash, #initial_concat_to_timed!, #instance_port?, #last_behavior, #make_portw, #map_behaviors!, #map_connections!, #map_inners!, #map_scopes!, #map_systemIs!, #map_systemTs!, #map_types!, #mixblocks2seq!, #parent_system, #port_assign?, #portw2ref, #portw_name2sym, #replace_names!, #replace_names_subs!, #reverse_each_behavior, #select2case!, #sym2portw_name, #to_c, #to_ch, #to_high, #to_upper_space!, #to_vhdl, #top_scope, #with_port!

Methods included from Low::ForceName

#extend_name!, #force_name!

Methods included from Low::Low2Symbol

#to_sym

Methods included from Low::Hparent

#scope

Constructor Details

#initialize(name = :"", systemT = nil, &ruby_block) ⇒ Scope

Creates a new scope with possible +name+. If the scope is a top scope of a system, this systemT is given by +systemT+.

The proc +ruby_block+ is executed for building the scope. If no block is provided, the scope is the top of a system and is filled by the instantiation procedure of the system.



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'lib/HDLRuby/hruby_high.rb', line 772

def initialize(name = :"", systemT = nil, &ruby_block)
    # Initialize the scope structure
    super(name)

    # Initialize the set of grouped system instances.
    @groupIs = {}

    # Creates the namespace.
    @namespace = Namespace.new(self)

    # Register the scope if it is not the top scope of a system
    # (in which case the system has already be registered with
    # the same name).
    unless name.empty? or systemT then
        # Named scope, set the hdl-like access to the scope.
        obj = self # For using the right self within the proc
        High.space_reg(name) { obj }
    end

    # Initialize the set of exported inner signals and instances
    @exports = {}
    # Initialize the set of included systems.
    @includes = {}

    # Builds the scope if a ruby block is provided
    # (which means the scope is not the top of a system).
    self.build(&ruby_block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HDLRuby::High::HScope_missing

Instance Attribute Details

#nameObject (readonly)

The name of the scope if any.



756
757
758
# File 'lib/HDLRuby/hruby_high.rb', line 756

def name
  @name
end

#namespaceObject (readonly)

The namespace



759
760
761
# File 'lib/HDLRuby/hruby_high.rb', line 759

def namespace
  @namespace
end

#return_valueObject (readonly)

The return value when building the scope.



762
763
764
# File 'lib/HDLRuby/hruby_high.rb', line 762

def return_value
  @return_value
end

Instance Method Details

#add_export(name) ⇒ Object

Adds a +name+ to export.

NOTE: if the name do not corresponds to any inner signal nor instance, raise an exception.

Raises:



843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
# File 'lib/HDLRuby/hruby_high.rb', line 843

def add_export(name)
    # Check the name.
    name = name.to_sym
    # Look for construct to make public.
    # Maybe it is an inner signals.
    inner = self.get_inner(name)
    if inner then
        # Yes set it as export.
        @exports[name] = inner
        return
    end
    # No, maybe it is an instance.
    instance = self.get_systemI(name)
    if instance then
        # Yes, set it as export.
        @exports[name] = instance
        return
    end
    # No, error.
    raise AnyError, "Invalid name for export: #{name}"
end

#add_groupI(name, *instances) ⇒ Object

Adds a group of system +instances+ named +name+.



808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/HDLRuby/hruby_high.rb', line 808

def add_groupI(name, *instances)
    # Ensure name is a symbol and is not already used for another
    # group.
    name = name.to_sym
    if @groupIs.key?(name)
        raise AnyError,
              "Group of system instances named #{name} already exist."
    end
    # Add the group.
    @groupIs[name.to_sym] = instances
    # Sets the parent of the instances.
    instances.each { |instance| instance.parent = self }
end

#add_inout(signal) ⇒ Object

Adds inout +signal+ in the current system.



945
946
947
# File 'lib/HDLRuby/hruby_high.rb', line 945

def add_inout(signal)
    self.parent.add_inout(signal)
end

#add_input(signal) ⇒ Object

Adds input +signal+ in the current system.



935
936
937
# File 'lib/HDLRuby/hruby_high.rb', line 935

def add_input(signal)
    self.parent.add_input(signal)
end

#add_output(signal) ⇒ Object

Adds output +signal+ in the current system.



940
941
942
# File 'lib/HDLRuby/hruby_high.rb', line 940

def add_output(signal)
    self.parent.add_output(signal)
end

#as(system) ⇒ Object

Casts as an included +system+.



1198
1199
1200
1201
1202
1203
# File 'lib/HDLRuby/hruby_high.rb', line 1198

def as(system)
    # puts "as with name: #{system.name}"
    system = system.name if system.respond_to?(:name)
    # puts "includes are: #{@includes.keys}"
    return @includes[system].namespace
end

#build(&ruby_block) ⇒ Object

Build the scope by executing +ruby_block+.

NOTE: used when the scope is not the top of a system.



904
905
906
907
908
909
910
911
# File 'lib/HDLRuby/hruby_high.rb', line 904

def build(&ruby_block)
    # Set the namespace for buidling the scope.
    High.space_push(@namespace)
    # Build the scope.
    @return_value = High.top_user.instance_eval(&ruby_block)
    High.space_pop
    @return_value
end

#build_top(base, *args) ⇒ Object

Builds the scope using +base+ as model scope with possible arguments +args+.

NOTE: Used by the instantiation procedure of a system.



918
919
920
921
922
923
924
925
926
927
928
929
# File 'lib/HDLRuby/hruby_high.rb', line 918

def build_top(base,*args)
    # Fills its namespace with the content of the base scope
    # (this latter may already contains access points if it has been
    #  opended for extension previously).
    @namespace.concat_namespace(base.namespace)
    High.space_push(@namespace)
    # Execute the instantiation block
    base.parent.each_instance_proc do |instance_proc|
        @return_value = High.top_user.instance_exec(*args,&instance_proc)
    end
    High.space_pop
end

#code(*content, &ruby_block) ⇒ Object

Declares a non-HDLRuby set of code chunks described by +content+ and completed from +ruby_block+ execution result. NOTE: content includes the events to activate the code on and a description of the code as a hash assotiating names to code text.



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/HDLRuby/hruby_high.rb', line 1006

def code(*content, &ruby_block)
    # Process the content.
    # Separate events from code chunks descriptions.
    events, chunks = content.partition {|elem| elem.is_a?(Event) }
    # Generates a large hash from the code.
    chunks = chunks.reduce(:merge)
    # Adds the result of the ruby block if any.
    if ruby_block then
        chunks.merge(HDLRuby::High.top_user.instance_eval(&ruby_block))
    end
    # Create the chunk objects.
    chunks = chunks.each.map do |name,content|
        content = [*content]
        # Process the lumps
        content.map! do |lump|
            lump.respond_to?(:to_expr) ? lump.to_expr : lump
        end
        Chunk.new(name,*content)
    end
    # Create the code object.
    res = Code.new
    # Adds the events.
    events.each(&res.method(:add_event))
    # Adds the chunks.
    chunks.each(&res.method(:add_chunk))
    # Adds the resulting code to the current scope.
    HDLRuby::High.top_user.add_code(res)
    # Return the resulting code
    return res
end

#cur_systemObject

Gets the current system.



1207
1208
1209
# File 'lib/HDLRuby/hruby_high.rb', line 1207

def cur_system
    return HDLRuby::High.cur_system
end

#each_export(&ruby_block) ⇒ Object

Iterates over the exported constructs.

Returns an enumerator if no ruby block is given.



868
869
870
871
872
873
874
875
# File 'lib/HDLRuby/hruby_high.rb', line 868

def each_export(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_export) unless ruby_block
    # A block? Apply it on each input signal instance.
    @exports.each_value(&ruby_block)
    # And apply on the sub scopes if any.
    @scopes.each {|scope| scope.each_export(&ruby_block) }
end

#each_groupI(&ruby_block) ⇒ Object

Iterates over the group of system instances.

Returns an enumerator if no ruby block is given.



832
833
834
835
836
837
# File 'lib/HDLRuby/hruby_high.rb', line 832

def each_groupI(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_groupI) unless ruby_block
    # A block? Apply it on each input signal instance.
    @groupIs.each(&ruby_block)
end

#each_included(&ruby_block) ⇒ Object

Iterates over the included systems.



878
879
880
881
882
883
884
885
# File 'lib/HDLRuby/hruby_high.rb', line 878

def each_included(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_included) unless ruby_block
    # A block? Apply it on each included system.
    @includes.each_value(&ruby_block)
    # And apply on the sub scopes if any.
    @scopes.each {|scope| scope.each_included(&ruby_block) }
end

#export(*names) ⇒ Object

Sets the constructs corresponding to +names+ as exports.



1153
1154
1155
# File 'lib/HDLRuby/hruby_high.rb', line 1153

def export(*names)
    names.each {|name| self.add_export(name) }
end

#fill_low(scopeLow) ⇒ Object

Fills a low level scope with self's contents.

NOTE: name conflicts are treated in the current NameStack state.



1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
# File 'lib/HDLRuby/hruby_high.rb', line 1216

def fill_low(scopeLow)
    # Adds the content of its included systems.
    @includes.each_value {|system| system.scope.fill_low(scopeLow) }
    # Adds the declared local system types.
    # NOTE: in the current version of HDLRuby::High, there should not
    # be any of them (only eigen systems are real system types).
    self.each_systemT { |systemT| scopeLow.add_systemT(systemT.to_low) }
    # Adds the local types.
    self.each_type { |type| scopeLow.add_type(type.to_low) }
    # Adds the inner scopes.
    self.each_scope { |scope| scopeLow.add_scope(scope.to_low) }
    # Adds the inner signals.
    self.each_inner { |inner| scopeLow.add_inner(inner.to_low) }
    # Adds the instances.
    # Single ones.
    self.each_systemI do |systemI|
        # puts "Filling with systemI=#{systemI.name}"
        systemI_low = scopeLow.add_systemI(systemI.to_low)
        # Also add the eigen system to the list of local systems.
        scopeLow.add_systemT(systemI_low.systemT)
    end
    # Grouped ones.
    self.each_groupI do |name,systemIs|
        systemIs.each.with_index { |systemI,i|
            # Sets the name of the system instance
            # (required for conversion of further accesses).
            # puts "systemI.respond_to?=#{systemI.respond_to?(:name=)}"
            systemI.name = name.to_s + "[#{i}]"
            # And convert it to low
            systemI_low = scopeLow.add_systemI(systemI.to_low())
            # Also add the eigen system to the list of local systems.
            scopeLow.add_systemT(systemI_low.systemT)
        }
    end
    # Adds the code chunks.
    self.each_code { |code| scopeLow.add_code(code.to_low) }
    # Adds the connections.
    self.each_connection { |connection|
        # puts "connection=#{connection}"
        scopeLow.add_connection(connection.to_low)
    }
    # Adds the behaviors.
    self.each_behavior { |behavior|
        scopeLow.add_behavior(behavior.to_low)
    }
end

#get_groupI(name) ⇒ Object

Access a group of system instances by +name+.

NOTE: the result is a copy of the group for avoiding side effects.



825
826
827
# File 'lib/HDLRuby/hruby_high.rb', line 825

def get_groupI(name)
    return @groupIs[name.to_sym].clone
end

#hcase(value) ⇒ Object

Creates a new case statement with a +value+ used for deciding which block to execute.

NOTE:

  • the when part is defined through the hwhen method.
  • a new behavior is created to enclose the hcase.


1130
1131
1132
1133
1134
# File 'lib/HDLRuby/hruby_high.rb', line 1130

def hcase(value)
    self.par do
        hcase(value)
    end
end

#helse(mode = nil, &ruby_block) ⇒ Object

Sets the block executed when the condition is not met to the block in +mode+ generated by the execution of +ruby_block+.

Can only be used once.

NOTE: added to the hif of the last behavior.



1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
# File 'lib/HDLRuby/hruby_high.rb', line 1099

def helse(mode = nil, &ruby_block)
    # There is a ruby_block: the helse is assumed to be with
    # the last statement of the last behavior.
    statement = self.last_behavior.last_statement
    # Completes the hif or the hcase statement.
    unless statement.is_a?(If) or statement.is_a?(Case) then
        raise AnyError, "Error: helse statement without hif nor hcase (#{statement.class})."
    end
    statement.helse(mode, &ruby_block)
end

#helsif(condition, mode = nil, &ruby_block) ⇒ Object

Sets the condition check when the condition is not met to the block, with a +condition+ that when met lead to the execution of the block in +mode+ generated by the +ruby_block+.



1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
# File 'lib/HDLRuby/hruby_high.rb', line 1113

def helsif(condition, mode = nil, &ruby_block)
    # There is a ruby_block: the helse is assumed to be with
    # the last statement of the last behavior.
    statement = self.last_behavior.last_statement
    # Completes the hif statement.
    unless statement.is_a?(If) then
        raise AnyError, "Error: helsif statement without hif (#{statement.class})."
    end
    statement.helsif(condition, mode, &ruby_block)
end

#hif(condition, mode = nil, &ruby_block) ⇒ Object

Creates a new if statement with a +condition+ that when met lead to the execution of the block in +mode+ generated by the +ruby_block+.

NOTE:

  • the else part is defined through the helse method.
  • a behavior is created to enclose the hif.


1087
1088
1089
1090
1091
# File 'lib/HDLRuby/hruby_high.rb', line 1087

def hif(condition, mode = nil, &ruby_block)
    self.par do
        hif(condition,mode,&ruby_block)
    end
end

#hwhen(match, mode = nil, &ruby_block) ⇒ Object

Sets the block of a case structure executed when the +match+ is met to the block in +mode+ generated by the execution of +ruby_block+.

Can only be used once.



1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'lib/HDLRuby/hruby_high.rb', line 1140

def hwhen(match, mode = nil, &ruby_block)
    # There is a ruby_block: the helse is assumed to be with
    # the last statement of the last behavior.
    statement = @behaviors.last.last_statement
    # Completes the hcase statement.
    unless statement.is_a?(Case) then
        raise AnyError, "Error: hwhen statement without hcase (#{statement.class})."
    end
    statement.hwhen(match, mode, &ruby_block)
end

#include(system, *args) ⇒ Object

Include a +system+ type with possible +args+ instanciation arguments.



1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
# File 'lib/HDLRuby/hruby_high.rb', line 1159

def include(system,*args)
    if @includes.key?(system.name) then
        raise AnyError, "Cannot include twice the same system."
    end
    # puts "Include system=#{system.name}"
    # Save the name of the included system, it will serve as key
    # for looking for the included expanded version.
    include_name = system.name
    # Expand the system to include
    system = system.expand(:"",*args)
    # Add the included system interface to the current one.
    if self.parent.is_a?(SystemT) then
        space = self.namespace
        # Interface signals
        # puts "i_name=#{i_name} @to_includes=#{@to_includes.size}"
        system.each_signal_with_included do |signal|
            # puts "signal=#{signal.name}"
            space.send(:define_singleton_method,signal.name) do
                signal
            end
        end
        # Exported objects
        system.each_export do |export|
            # puts "export=#{export.name}"
            space.send(:define_singleton_method,export.name) do
                export
            end
        end
        # Adds the task to execute on the instance.
        system.each_on_instance do |task|
            self.parent.on_instance(&task)
        end
    end
    # Adds it the list of includeds
    @includes[include_name] = system
    
end

#inout(*names) ⇒ Object

Declares high-level bit inout signals named +names+ in the current system.



997
998
999
# File 'lib/HDLRuby/hruby_high.rb', line 997

def inout(*names)
    self.parent.inout(*names)
end

#input(*names) ⇒ Object

Declares high-level bit input signals named +names+ in the current system.



985
986
987
# File 'lib/HDLRuby/hruby_high.rb', line 985

def input(*names)
    self.parent.input(*names)
end

#make_inouts(type, *names) ⇒ Object

Creates and adds a set of inouts typed +type+ from a list of +names+ in the current system.

NOTE: a name can also be a signal, is which case it is duplicated.



969
970
971
# File 'lib/HDLRuby/hruby_high.rb', line 969

def make_inouts(type, *names)
    self.parent.make_inouts(type,*names)
end

#make_inputs(type, *names) ⇒ Object

Creates and adds a set of inputs typed +type+ from a list of +names+ in the current system.

NOTE: a name can also be a signal, is which case it is duplicated.



953
954
955
# File 'lib/HDLRuby/hruby_high.rb', line 953

def make_inputs(type, *names)
    self.parent.make_inputs(type,*names)
end

#make_outputs(type, *names) ⇒ Object

Creates and adds a set of outputs typed +type+ from a list of +names+ in the current system.

NOTE: a name can also be a signal, is which case it is duplicated.



961
962
963
# File 'lib/HDLRuby/hruby_high.rb', line 961

def make_outputs(type, *names)
    self.parent.make_outputs(type,*names)
end

#open(&ruby_block) ⇒ Object

Opens for extension.

NOTE: actually executes +ruby_block+ in the context.



891
892
893
894
895
896
897
898
# File 'lib/HDLRuby/hruby_high.rb', line 891

def open(&ruby_block)
    High.space_push(@namespace)
    res = High.top_user.instance_eval(&ruby_block)
    High.space_pop
    # Return the result of the execution so that it can be used
    # as an expression
    res
end

#output(*names) ⇒ Object

Declares high-level bit output signals named +names+ in the current system.



991
992
993
# File 'lib/HDLRuby/hruby_high.rb', line 991

def output(*names)
    self.parent.output(*names)
end

#par(*events, &ruby_block) ⇒ Object

Declares a high-level parallel behavior activated on a list of +events+, and built by executing +ruby_block+.



1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/HDLRuby/hruby_high.rb', line 1063

def par(*events, &ruby_block)
    # Preprocess the events.
    events.map! do |event|
        event.respond_to?(:to_event) ? event.to_event : event
    end
    # Create and add the resulting behavior.
    self.add_behavior(Behavior.new(:par,*events,&ruby_block))
end

#seq(*events, &ruby_block) ⇒ Object

Declares a high-level sequential behavior activated on a list of +events+, and built by executing +ruby_block+.



1052
1053
1054
1055
1056
1057
1058
1059
# File 'lib/HDLRuby/hruby_high.rb', line 1052

def seq(*events, &ruby_block)
    # Preprocess the events.
    events.map! do |event|
        event.respond_to?(:to_event) ? event.to_event : event
    end
    # Create and add the resulting behavior.
    self.add_behavior(Behavior.new(:seq,*events,&ruby_block))
end

#sub(name = :"", &ruby_block) ⇒ Object

Declares a sub scope with possible +name+ and built from +ruby_block+.



1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/HDLRuby/hruby_high.rb', line 1038

def sub(name = :"", &ruby_block)
    # Creates the new scope.
    scope = Scope.new(name,&ruby_block)
    # puts "new scope=#{scope}"
    # Add it
    self.add_scope(scope)
    # puts "self=#{self}"
    # puts "self scopes=#{self.each_scope.to_a.join(",")}"
    # Use its return value
    return scope.return_value
end

#timed(&ruby_block) ⇒ Object

Declares a high-level timed behavior built by executing +ruby_block+. By default, timed behavior are sequential.



1074
1075
1076
1077
# File 'lib/HDLRuby/hruby_high.rb', line 1074

def timed(&ruby_block)
    # Create and add the resulting behavior.
    self.add_behavior(TimeBehavior.new(:seq,&ruby_block))
end

#to_lowObject

Converts the scope to HDLRuby::Low.



1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
# File 'lib/HDLRuby/hruby_high.rb', line 1264

def to_low()
    # Create the resulting low scope.
    # scopeLow = HDLRuby::Low::Scope.new()
    scopeLow = HDLRuby::Low::Scope.new(self.name)
    # Push the private namespace for the low generation.
    High.space_push(@namespace)
    # Pushes on the name stack for converting the internals of
    # the system.
    High.names_push
    # Adds the content of the actual system.
    self.fill_low(scopeLow)
    # Restores the name stack.
    High.names_pop
    # Restores the namespace stack.
    High.space_pop
    # Return theresulting system.
    return scopeLow
end

#to_refObject

Converts to a new reference.



974
975
976
# File 'lib/HDLRuby/hruby_high.rb', line 974

def to_ref
    return RefObject.new(this,self)
end

#to_userObject

Converts to a namespace user.



802
803
804
805
# File 'lib/HDLRuby/hruby_high.rb', line 802

def to_user
    # Already a user.
    return self
end