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.



2420
2421
2422
2423
2424
2425
2426
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2420

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.



2439
2440
2441
2442
2443
2444
2445
2446
2447
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2439

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.



2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2450

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.



2434
2435
2436
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2434

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

#selsif(cond, &ruby_block) ⇒ Object

Add a selsif case.



2429
2430
2431
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2429

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

#to_cObject

Convert to C code.



2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2475

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.



2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2487

def to_python(l = "")
  res = @sequencer.clk_up_python(l) + 
    "\n#{l}if (#{@condition.to_ruby}) != 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.



2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2463

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