Class: RubyHDL::High::Siter

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

Overview

Describes a SW implementation of an iterator statement.

Instance Method Summary collapse

Constructor Details

#initialize(sequencer, *commands, &ruby_block) ⇒ Siter

Create a new iteration statement in sequencer +sequencer+ for chain of commands +commands+ to interate while executing +ruby_block+.



3900
3901
3902
3903
3904
3905
3906
3907
3908
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3900

def initialize(sequencer,*commands, &ruby_block)
  @sequencer = sequencer
  @commands = commands
  if ruby_block then
    # The iterator is finalized.
    @blk = Sblock.new(sequencer,&ruby_block)
  end
  # puts "New iterator with blk=#{@blk} commands=#{@commands}"
end

Instance Method Details

#each_command(&ruby_block) ⇒ Object Also known as: each

Iterate on the commands.



3911
3912
3913
3914
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3911

def each_command(&ruby_block)
  return to_enum(:each_command) unless ruby_block
  @commands.each(&ruby_block)
end

#each_statement(&ruby_block) ⇒ Object

Iterate on the statements.



3918
3919
3920
3921
3922
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3918

def each_statement(&ruby_block)
  return to_enum(:each_statement) unless ruby_block
  # Apply ruby_block on the block.
  @blk.each_statement(&ruby_block)
end

#each_statement_deep(&ruby_block) ⇒ Object

Iterate deeply on the statements.



3925
3926
3927
3928
3929
3930
3931
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3925

def each_statement_deep(&ruby_block)
  return to_enum(:each_statement_deep) unless ruby_block
  # Recurse on the yes block.
  @blk.each_statement_deep
  # And apply ruby_block on self.
  ruby_block.call(self)
end

#make_iterator(meth, *args, &ruby_block) ⇒ Object

Create an iterator for a given method +meth+.



3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3976

def make_iterator(meth,*args,&ruby_block)
  # if ruby_block then
  #   blk = Sblock.new(@sequencer,&ruby_block)
  #   command = RubyHDL::High::Ruby.new do
  #     "#{meth}(#{args.map{|arg| arg.to_ruby}.join(",")}) { #{blk.to_ruby} }"
  #   end
  # else
  #   command = RubyHDL::High::Ruby.new do
  #     "#{meth}(#{args.map{|arg| arg.to_ruby}.join(",")})"
  #   end
  # end
  command = "#{meth}"
  if args.any? then
    command += "(*#{RubyHDL::High::Ruby.new {
    "#{args.map{|arg| arg.to_ruby}.join(",")}"}})"
  end
  return Siter.new(@sequencer,*@commands,command,&ruby_block)
end

#sall?(arg = nil, &ruby_block) ⇒ Boolean

Tell if all the elements respect a given criterion given either as +arg+ or as block.

Returns:

  • (Boolean)


4009
4010
4011
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4009

def sall?(arg = nil,&ruby_block)
  return self.make_iterator("all?",arg,&ruby_block)
end

#sany?(arg = nil, &ruby_block) ⇒ Boolean

Tell if any of the elements respects a given criterion given either as +arg+ or as block.

Returns:

  • (Boolean)


4015
4016
4017
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4015

def sany?(arg = nil,&ruby_block)
  return self.make_iterator("any?",arg,&ruby_block)
end

#schain(arg) ⇒ Object

Returns an SEnumerator generated from current enumerable and +arg+



4020
4021
4022
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4020

def schain(arg)
  return self.make_iterator("chain",arg)
end

#schunk(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby chunk. NOTE: to do, or may be not.



4026
4027
4028
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4026

def schunk(*args,&ruby_block)
  raise "schunk is not supported yet."
end

#schunk_while(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby chunk_while. NOTE: to do, or may be not.



4032
4033
4034
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4032

def schunk_while(*args,&ruby_block)
  raise "schunk_while is not supported yet."
end

#scompactObject

HW implementation of the Ruby compact, but remove 0 values instead on nil (since nil that does not have any meaning in HW).



4053
4054
4055
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4053

def scompact
  return self.make_iterator("compact",&ruby_block)
end

#scount(obj = nil, &ruby_block) ⇒ Object

WH implementation of the Ruby count.



4059
4060
4061
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4059

def scount(obj = nil, &ruby_block)
  return self.make_iterator("count",obj,&ruby_block)
end

#scycle(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby cycle.



4064
4065
4066
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4064

def scycle(n = nil,&ruby_block)
  return self.make_iterator("cycle",n,&ruby_block)
end

#sdrop(n) ⇒ Object

HW implementation of the Ruby drop.



4076
4077
4078
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4076

def sdrop(n)
  return self.make_iterator("drop",n)
end

#sdrop_while(&ruby_block) ⇒ Object

HW implementation of the Ruby drop_while.



4081
4082
4083
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4081

def sdrop_while(&ruby_block)
  return self.make_iterator("drop_while",&ruby_block)
end

#seach_cons(n, &ruby_block) ⇒ Object

HW implementation of the Ruby each_cons



4086
4087
4088
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4086

def seach_cons(n,&ruby_block)
  return self.make_iterator("each_cons",n,&ruby_block)
end

#seach_entry(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby each_entry. NOTE: to do, or may be not.



4092
4093
4094
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4092

def seach_entry(*args,&ruby_block)
  raise "seach_entry is not supported yet."
end

#seach_nexts(num, &ruby_block) ⇒ Object

Iterator on the +num+ next elements. NOTE:

  • Stop iteration when the end of the range is reached or when there are no elements left
  • This is not a method from Ruby but one specific for hardware where creating a array is very expensive.


4308
4309
4310
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4308

def seach_nexts(num,&ruby_block)
  return self.seach.snexts(num,&ruby_block)
end

#seach_range(rng, &ruby_block) ⇒ Object

Iterator on each of the elements in range +rng+. NOTE:

  • Stop iteration when the end of the range is reached or when there are no elements left
  • This is not a method from Ruby but one specific for hardware where creating a array is very expensive.


4003
4004
4005
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4003

def seach_range(rng,&ruby_block)
  return self.make_iterator("each_range",rng,&ruby_block)
end

#seach_slice(n, &ruby_block) ⇒ Object

HW implementation of the Ruby each_slice



4097
4098
4099
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4097

def seach_slice(n,&ruby_block)
  return self.make_iterator("each_slice",n,&ruby_block)
end

#seach_with_index(*args, &ruby_block) ⇒ Object Also known as: with_index

HW implementation of the Ruby each_with_index.



4102
4103
4104
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4102

def seach_with_index(*args,&ruby_block)
  return self.make_iterator("each_with_index",*args,&ruby_block)
end

#seach_with_object(obj, &ruby_block) ⇒ Object

HW implementation of the Ruby each_with_object.



4108
4109
4110
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4108

def seach_with_object(obj,&ruby_block)
  return self.make_iterator("each_with_object",obj,&ruby_block)
end

#sfind(if_none_proc, &ruby_block) ⇒ Object

HW implementation of the Ruby find. NOTE: contrary to Ruby, if_none_proc is mandatory since there is no nil in HW. Moreover, the argument can also be a value.



4071
4072
4073
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4071

def sfind(if_none_proc, &ruby_block)
  return self.make_iterator("find",if_none_proc,&ruby_block)
end

#sfind_index(obj = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby find_index.



4123
4124
4125
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4123

def sfind_index(obj = nil, &ruby_block)
  return self.make_iterator("find_index",obj,&ruby_block)
end

#sfirst(n = 1) ⇒ Object

HW implementation of the Ruby first.



4128
4129
4130
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4128

def sfirst(n=1)
  return self.make_iterator("first",n)
end

#sflat_map(&ruby_block) ⇒ Object

HW implementation of the Ruby flat_map. NOTE: actually due to the way HDLRuby handles vectors, should work like smap



4047
4048
4049
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4047

def sflat_map(&ruby_block)
  return self.make_iterator("flat_map",&ruby_block)
end

#sgrep(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby grep. NOTE: to do, or may be not.



4134
4135
4136
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4134

def sgrep(*args,&ruby_block)
  raise "sgrep is not supported yet."
end

#sgrep_v(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby grep_v. NOTE: to do, or may be not.



4140
4141
4142
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4140

def sgrep_v(*args,&ruby_block)
  raise "sgrep_v is not supported yet."
end

#sgroup_by(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby group_by. NOTE: to do, or may be not.



4146
4147
4148
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4146

def sgroup_by(*args,&ruby_block)
  raise "sgroup_by is not supported yet."
end

#sinclude?(obj) ⇒ Boolean

HW implementation of the Ruby include?

Returns:

  • (Boolean)


4151
4152
4153
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4151

def sinclude?(obj)
  return self.make_iterator("include?",obj)
end

#sinject(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby inject.



4156
4157
4158
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4156

def sinject(*args,&ruby_block)
  return self.make_iterator("inject",*args,&ruby_block)
end

#slazy(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby lazy. NOTE: to do, or may be not.



4168
4169
4170
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4168

def slazy(*args,&ruby_block)
  raise "slazy is not supported yet."
end

#smap(&ruby_block) ⇒ Object

Returns a vector containing the execution result of the given block on each element. If no block is given, return an SEnumerator. NOTE: be carful that the resulting vector can become huge if there are many element.



4040
4041
4042
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4040

def smap(&ruby_block)
  return self.make_iterator("map",&ruby_block)
end

#smax(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby max.



4173
4174
4175
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4173

def smax(n = nil, &ruby_block)
  return self.make_iterator("max",n,&ruby_block)
end

#smax_by(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby max_by.



4178
4179
4180
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4178

def smax_by(n = nil, &ruby_block)
  return self.make_iterator("max_by",n,&ruby_block)
end

#smin(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby min.



4183
4184
4185
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4183

def smin(n = nil, &ruby_block)
  return self.make_iterator("min",n,&ruby_block)
end

#smin_by(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby min_by.



4188
4189
4190
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4188

def smin_by(n = nil, &ruby_block)
  return self.make_iterator("min_by",n,&ruby_block)
end

#sminmax(&ruby_block) ⇒ Object

HW implementation of the Ruby minmax.



4193
4194
4195
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4193

def sminmax(&ruby_block)
  return self.make_iterator("minmax",&ruby_block)
end

#sminmax_by(&ruby_block) ⇒ Object

HW implementation of the Ruby minmax_by.



4198
4199
4200
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4198

def sminmax_by(&ruby_block)
  return self.make_iterator("minmax_by",&ruby_block)
end

#snone?(arg = nil, &ruby_block) ⇒ Boolean

Tell if none of the elements respects a given criterion given either as +arg+ or as block.

Returns:

  • (Boolean)


4204
4205
4206
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4204

def snone?(arg = nil,&ruby_block)
  return self.make_iterator("none?",arg,&ruby_block)
end

#sone?(arg = nil, &ruby_block) ⇒ Boolean

Tell if one and only one of the elements respects a given criterion given either as +arg+ or as block.

Returns:

  • (Boolean)


4210
4211
4212
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4210

def sone?(arg = nil,&ruby_block)
  return self.make_iterator("one?",arg,&ruby_block)
end

#spartition(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby partition. NOTE: to do, or may be not.



4216
4217
4218
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4216

def spartition(*args,&ruby_block)
  raise "spartition is not supported yet."
end

#sreduceObject

HW implementation of the Ruby reduce.



4161
4162
4163
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4161

def sreduce
  return self.make_iterator("reduce",*args,&ruby_block)
end

#sreject(&ruby_block) ⇒ Object

HW implementatiob of the Ruby reject.



4221
4222
4223
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4221

def sreject(&ruby_block)
  return self.make_iterator("reject",&ruby_block)
end

#sreverse_each(*args, &ruby_block) ⇒ Object

HW implementatiob of the Ruby reverse_each.



4226
4227
4228
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4226

def sreverse_each(*args,&ruby_block)
  return self.make_iterator("reverse_each",*args,&ruby_block)
end

#sselect(&ruby_block) ⇒ Object

HW implementation of the Ruby select.



4118
4119
4120
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4118

def sselect(&ruby_block)
  return self.make_iterator("select",&ruby_block)
end

#sslice_after(pattern = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby slice_after. NOTE: to do, or may be not.



4232
4233
4234
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4232

def sslice_after(pattern = nil,&ruby_block)
  raise "sslice_after is not supported yet."
end

#sslice_before(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby slice_before. NOTE: to do, or may be not.



4238
4239
4240
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4238

def sslice_before(*args,&ruby_block)
  raise "sslice_before is not supported yet."
end

#sslice_when(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby slice_when. NOTE: to do, or may be not.



4244
4245
4246
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4244

def sslice_when(*args,&ruby_block)
  raise "sslice_before is not supported yet."
end

#ssort(&ruby_block) ⇒ Object

HW implementation of the Ruby sort.



4254
4255
4256
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4254

def ssort(&ruby_block)
  return self.make_iterator("sort",&ruby_block)
end

#ssort_by(&ruby_block) ⇒ Object

HW implementation of the Ruby sort.



4259
4260
4261
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4259

def ssort_by(&ruby_block)
  return self.make_iterator("sort_by",&ruby_block)
end

#ssort_merge(arI, arO, first, middle, last, &ruby_block) ⇒ Object

Merge two arrays in order, for ssort only.



4249
4250
4251
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4249

def ssort_merge(arI, arO, first, middle, last, &ruby_block)
  return self.make_iterator("sort_merge",arI,arO,first,middle,last,&ruby_block)
end

#ssum(initial_value = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby sum.



4264
4265
4266
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4264

def ssum(initial_value = nil,&ruby_block)
  return self.make_iterator("sum",initial_value,&ruby_block)
end

#stake(n) ⇒ Object

The HW implementation of the Ruby take.



4269
4270
4271
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4269

def stake(n)
  return self.make_iterator("take",n)
end

#stake_while(&ruby_block) ⇒ Object

The HW implementation of the Ruby take_while.



4274
4275
4276
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4274

def stake_while(&ruby_block)
  return self.make_iterator("take_while",&ruby_block)
end

#stally(h = nil) ⇒ Object

HW implementation of the Ruby tally. NOTE: to do, or may be not.



4280
4281
4282
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4280

def stally(h = nil)
  raise "stally is not supported yet."
end

#sto_aObject

HW implementation of the Ruby to_a.



4113
4114
4115
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4113

def sto_a
  return self.make_iterator("to_a")
end

#sto_h(h = nil) ⇒ Object

HW implementation of the Ruby to_h. NOTE: to do, or may be not.



4286
4287
4288
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4286

def sto_h(h = nil)
  raise "sto_h is not supported yet."
end

#suniq(&ruby_block) ⇒ Object

HW implementation of the Ruby uniq.



4291
4292
4293
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4291

def suniq(&ruby_block)
  return self.make_iterator("uniq",&ruby_block)
end

#szip(obj, &ruby_block) ⇒ Object

HW implementation of the Ruby zip. NOTE: for now szip is deactivated untile tuples are properly handled by HDLRuby.



4298
4299
4300
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4298

def szip(obj,&ruby_block)
  return self.make_iterator("zip",obj,&ruby_block)
end

#to_cObject

Convert to C code.



3945
3946
3947
3948
3949
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3945

def to_c
  res = @sequencer.clk_up_c + "\n" +
    @commands.map { |command| command.to_c }.join("_")
  return res + "(#{@blk.to_c})"
end

#to_python(l = "") ⇒ Object

Convert to Python code.



3952
3953
3954
3955
3956
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3952

def to_python(l = "")
  res = @sequencer.clk_up_python(l) + "\n" +
    @commands.map { |command| command.to_python }.join("_")
  return res + "(#{@blk.to_python})"
end

#to_rubyObject

Convert to Ruby code.



3934
3935
3936
3937
3938
3939
3940
3941
3942
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3934

def to_ruby
  # puts "to_ruby with blk=#{@blk} commands=#{@commands}"
  res = @sequencer.clk_up + "\n" + 
    @commands.map { |command| command.to_ruby }.join(".") 
  return res + " do" + 
    (@blk.each_arg.any? ? 
     "|#{@blk.each_arg.map(&:to_ruby).join(",")}|" : "") + 
    "\n#{@blk.to_ruby}\n#{@sequencer.clk_up}\nend"
end

#to_tf(l = "") ⇒ Object

Convert to TensorFlow code.



3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 3959

def to_tf(l = "")
  if @commands[1] == "times" then
    # Simple times loop case.
    if @blk.each_arg.none? then
      raise "Require an index for loop conversion to TensorFlow."
    end
    arg = @blk.each_arg.to_a[0]
    val = @commands[0]
    return "#{l}#{arg.to_tf} = tf.range(0.0,#{val.to_tf},1.0)\n" +
           @blk.to_tf(l)
  else
    res =  @commands.map { |command| command.to_tf }.join("_")
    return res + "(#{@blk.to_tf})"
  end
end