In article <43F5BEDC.4000505 / capitain.de>,
Pit Capitain  <pit / capitain.de> wrote:
>Phil Tomson schrieb:
>> ...
>> 
>>   class Counter < RHDL
>>     inputs    :clk, :rst
>>     outputs   :count_out
>>     variables :count=>0
>> 
>>     define_behavior { 
>>       process(clk) {
>>         if clk.event and clk == 1
>>           if rst == 1
>>             count = 0
>>           else
>>             count += 1
>>           end
>>           count_out << count
>>         end
>>       }
>>     }
>>   end
>> 
>> ...
>> 
>> However, notice the 'variables' method.  The idea there is that there could be 
>> some variables used in the define_behavior block (and this was the case with 
>> the previous incarnation of RHDL, so I didn't want to lose that functionality).
>> Now by 'variable' here I mean that I want a variable that also has scope 
>> within the define_behavior block.  The define_behavior block will be called 
>> many, many times during a simulation and I don't want the variable to be 
>> re-initialzed on each call.
>> 
>> ...
>
>Phil, I don't understand the usage of variables. When should they be 
>initialized? What should be their scope (class, instance, thread, 
>other)? Can't you use instance variables with accessor methods?
>

Well, it's a bit strange in the example above.  Really what I want is for it to 
be method-level, I suppose.  Recall that I showed an alternative where the user 
actually defines the initialize method explicitly:



   #....
   def initialize
      count = 0
      define_behvior {
        #...do something with count ...
      }
   end


In that scenario count is a variable whose scope is within the initialize 
method.  It can also be referred to within the scope of the block passed to 
define_behavior (define_behavior in that case just assigns the block to a 
@behavior instance variable.)


In the scenario of the code snippet you quote above the idea was to somehow 
have the behavior block 'recontextualized' within the context of a generated 
method:
  def get_behavior
    count = 0
    #define_behavior block stored in @behavior:
    #the following line is not real Ruby:
    recontextualized_proc = Proc.new &@behavior, binding
  end

then later on the proc can be called.

Of course, if we pass a string to define_behavior and store it, then we can use 
the way shown by Jim Freeze to acheive the same thing.  The count variable then 
gets initialized to a value outside of the behavior block and is available 
within the behavior block.

Phil