Class: RubyHDL::High::Sif

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

Overview

Describes a SW implementation of a if statement.

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.



2283
2284
2285
2286
2287
2288
2289
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2283

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.



2302
2303
2304
2305
2306
2307
2308
2309
2310
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2302

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.



2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2313

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.



2297
2298
2299
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2297

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

#selsif(cond, &ruby_block) ⇒ Object

Add a selsif case.



2292
2293
2294
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2292

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

#to_cObject

Convert to C code.



2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2338

def to_c
  res = @sequencer.clk_up + "\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_rubyObject

Convert to Ruby code.



2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2326

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