Class: RubyHDL::High::Sif

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

Overview

Describes a SW implementation of a sif statement.

Direct Known Subclasses

Hif

Instance Method Summary collapse

Constructor Details

#initialize(sequencer, cond, &ruby_block) ⇒ Sif

Create a new if statement in sequencer +sequencer+ with +cond+ condition and +ruby_block+ for generating the block that is taken if the condition is met.



2919
2920
2921
2922
2923
2924
2925
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2919

def initialize(sequencer,cond, &ruby_block)
  @sequencer = sequencer
  @condition = cond.to_expr
  @yes_blk = Sblock.new(@sequencer,&ruby_block)
  @elsifs = []
  @else_blk = nil
end

Instance Method Details

#each_statement(&ruby_block) ⇒ Object

Iterate on the statements.



2938
2939
2940
2941
2942
2943
2944
2945
2946
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2938

def each_statement(&ruby_block)
  return to_enum(:each_statement) unless ruby_block
  # Apply ruby_block on the yes block.
  ruby_block.call(@yes_blk)
  # On the elsifs.
  @elsifs.each { |cond,statement| ruby_block.call(statement) }
  # On the else if any.
  ruby_block.call(@else_blk) if @else_blk
end

#each_statement_deep(&ruby_block) ⇒ Object

Iterate deeply on the statements.



2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2949

def each_statement_deep(&ruby_block)
  return to_enum(:each_statement_deep) unless ruby_block
  # Recurse on the yes block.
  @yes_blk.each_statement_deep
  # On the elsifs.
  @elsifs.each { |cond,statement| statement.each_statement_deep }
  # On the else if any.
  @else_blk.each_statement_deep if @else_blk
  # And apply ruby_block on self.
  ruby_block.call(self)
end

#selse(&ruby_block) ⇒ Object

Sets the else block.



2933
2934
2935
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2933

def selse(&ruby_block)
  @else_blk = Sblock.new(@sequencer,&ruby_block)
end

#selsif(cond, &ruby_block) ⇒ Object

Add a selsif case.



2928
2929
2930
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2928

def selsif(cond,&ruby_block)
  @elsifs << [cond,Sblock.new(@sequencer,&ruby_block)]
end

#to_cObject

Convert to C code.



2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2974

def to_c
  res = @sequencer.clk_up_c + "\nif(#{@condition.to_c}) {\n#{@yes_blk.to_c}\n}"
  @elsifs.each do |(cond,blk)|
    res << "\nelse if(#{cond.to_c}) {\n#{blk.to_c}\n}"
  end
  if @else_blk then
    res << "\nelse {\n#{@else_blk.to_c}\n}"
  end
  return res + @sequencer.clk_up_c
end

#to_python(l = "") ⇒ Object

Convert to Python code.



2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2986

def to_python(l = "")
  res = @sequencer.clk_up_python(l) + 
    "\n#{l}if (#{@condition.to_python}) != 0:\n" +
    "#{@yes_blk.to_python(l + "  ")}\n"
  @elsifs.each do |(cond,blk)|
    res << "#{l}elif (#{cond.to_python}) != 0:\n" +
    "#{blk.to_python(l + "  ")}\n"
  end
  if @else_blk then
    res << "#{l}else:\n#{@else_blk.to_python(l + "  ")}\n"
  end
  return res + @sequencer.clk_up_python(l)
end

#to_rubyObject

Convert to Ruby code.



2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2962

def to_ruby
  res = @sequencer.clk_up + "\nif((#{@condition.to_ruby}) != 0)\n#{@yes_blk.to_ruby}\n"
  @elsifs.each do |(cond,blk)|
    res << "elsif((#{cond.to_ruby}) != 0)\n#{blk.to_ruby}\n"
  end
  if @else_blk then
    res << "else\n#{@else_blk.to_ruby}\n"
  end
  return res + "end\n" + @sequencer.clk_up
end

#to_tf(l = "") ⇒ Object

Convert to TensorFlow code.



3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3001

def to_tf(l = "")
  res =  "\n#{l}tf.cond(#{@condition.to_tf},\n" +
         "\n#{l}        lambda: #{@yes_blk.to_f(l + "  ")},\n"
  @elseif.each do |(cond,blk)|
    res<<"\n#{l}        lambda: tf.cond(#{cond.to_tf}, lambda: #{blk}),\n" 
  end
  if @else_blk then
    res << "\n#{l}      lambda: #{else_blk.to_tf(l + "  ")})\n"
  else
    res << "\n#{l}      lambda: tf.constant0)\n"
  end
  return res
end