Class: RubyHDL::High::SequencerT

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

Overview

Describes a SW implementation of a sequencer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clk = nil, start = nil, &ruby_block) ⇒ SequencerT

Create a new sequencer block, with clock counter clk and run control start. Note: if clk is not provided no clock counter will be generate.



3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3838

def initialize(clk = nil, start = nil, &ruby_block)
  # Sets the clock counter and start control.
  @clk = clk
  @start = start
  if @start then
    this = self
    # Make @start a controlling signal.
    @start.define_singleton_method(:<=,val) do
      this.resume if val.to_i == 1
    end
  end
  # Create a set of sfunction used in the sequencer.
  @sfunctions = {}
  # Create the main block.
  @blk = RubyHDL::High::Sblock.new(self,&ruby_block)
  # Build the Ruby code.
  @source = ""
  @code = nil
  self.build_ruby
end

Instance Attribute Details

#clk ⇒ Object (readonly)

The clock counter.



3833
3834
3835
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3833

def clk
  @clk
end

#source ⇒ Object (readonly)

The source code (in ruby).



3830
3831
3832
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3830

def source
  @source
end

Instance Method Details

#add_sfunction(name, sfunction) ⇒ Object

Add a sfunction.



3860
3861
3862
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3860

def add_sfunction(name,sfunction)
  @sfunctions[name.to_sym] = sfunction
end

#alive? ⇒ Boolean

Check is the sequencer can still be resumed.

Returns:

  • (Boolean)


3971
3972
3973
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3971

def alive?
  @code.alive?
end

#build_ruby ⇒ Object

Build the ruby code.



3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3875

def build_ruby
  this = self
  @source = <<-BUILD
#{RubyHDL::High.global_sblock.each_signal.map do |signal|
  # puts "for signal=#{signal.name} with type=#{signal.type}"
  case signal.dir
  when :input
    signal.to_ruby + " = RubyHDL.#{signal.name}"
  else
    signal.to_ruby + " ||= " + 
      (signal.array? ? "[*#{signal.value? ? signal.value : "nil"}]" : signal.value? ? signal.value.inspect : "0")
  end
end.join("\n")}

#{@sfunctions.map {|n,f| f.to_ruby }.join("\n\n")}

Fiber.new do
#{@blk.to_ruby}
end
BUILD
  # puts "building code_txt=" + @source
  self.reset!
end

#clk_up(count = 1) ⇒ Object

Generate a clock up of count cycles if any clock.



3938
3939
3940
3941
3942
3943
3944
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3938

def clk_up(count = 1)
  if @clk then
    return "#{clk.to_ruby} += #{count.to_i}"
  else
    return ""
  end
end

#clk_up_c(count = 1) ⇒ Object

Generate a clock up of count cycles if any clock for C code.



3947
3948
3949
3950
3951
3952
3953
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3947

def clk_up_c(count = 1)
  if @clk then
    return "#{clk.to_c} += #{count.to_i};"
  else
    return ""
  end
end

#reset! ⇒ Object

Resets the sequencer.



3966
3967
3968
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3966

def reset!
  @code = TOPLEVEL_BINDING.eval(@source)
end

#resume ⇒ Object Also known as: call

Executes the sequencer.



3959
3960
3961
3962
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3959

def resume
  # @code.call
  @code.resume
end

#sfunction(name) ⇒ Object

Get a sfunction by name.



3870
3871
3872
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3870

def sfunction(name)
  return @sfunctions[name.to_sym]
end

#sfunction?(name) ⇒ Boolean

Check if a sfunction is present.

Returns:

  • (Boolean)


3865
3866
3867
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3865

def sfunction?(name)
  return @sfunctions.key?(name.to_sym)
end

#to_c ⇒ Object

Convert to C code.



3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3905

def to_c
  typ = nil
  res = <<-BUILDC
#include <stdio.h>

#{RubyHDL::High.global_sblock.each_signal.map do |signal|
  typ = signal.type
  if signal.value? then
    if signal.array? then
      typ.base.to_c + " " + signal.to_c + "[#{typ.size}]" " = {" + 
        signal.value.inspect[1..-2] + "};"
    else
      typ.to_c + " " + signal.to_c + "=" + signal.value.inspect + ";"
    end
  else
    if signal.array? then
      typ.base.to_c + " " + signal.to_c + "[#{typ.size}]" + " =" + typ.to_c_init + ";"
    else
      typ.to_c + " " + signal.to_c + "=" + typ.to_c_init + ";"
    end
  end
end.join("\n")}

#{@sfunctions.map {|n,f| f.to_c }.join("\n\n")}

void sequencer() #{@blk.to_c}
BUILDC
  return res
end

#to_ruby ⇒ Object

Get the Ruby code.



3900
3901
3902
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3900

def to_ruby
  return @code
end