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.



4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4090

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

#clkObject (readonly)

The clock counter.



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

def clk
  @clk
end

#sourceObject (readonly)

The source code (in ruby).



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

def source
  @source
end

Instance Method Details

#add_sfunction(name, sfunction) ⇒ Object

Add a sfunction.



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

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

#alive?Boolean

Check is the sequencer can still be resumed.

Returns:

  • (Boolean)


4272
4273
4274
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4272

def alive?
  @code.alive?
end

#build_rubyObject

Build the ruby code.



4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4127

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.



4230
4231
4232
4233
4234
4235
4236
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4230

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.



4239
4240
4241
4242
4243
4244
4245
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4239

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

#clk_up_python(l = "", count = 1) ⇒ Object

Generate a clock up of +count+ cycles if any clock.



4248
4249
4250
4251
4252
4253
4254
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4248

def clk_up_python(l="", count = 1)
  if @clk then
    return "#{l}#{clk.to_python} += #{count.to_i}"
  else
    return ""
  end
end

#reset!Object

Resets the sequencer.



4267
4268
4269
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4267

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

#resumeObject Also known as: call

Executes the sequencer.



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

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

#sfunction(name) ⇒ Object

Get a sfunction by name.



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

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

#sfunction?(name) ⇒ Boolean

Check if a sfunction is present.

Returns:

  • (Boolean)


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

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

#to_cObject

Convert to C code.



4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4157

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_python(l = "") ⇒ Object

Convert to Python code.



4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 4188

def to_python(l = "")
  typ = nil
  res = <<-BUILDPYTHON
#{RubyHDL::High.global_sblock.each_signal.map do |signal|
  typ = signal.type
  if signal.value? then
    if signal.array? then
      res = signal.to_python + "= [0] * #{signal.type.range.size}"
      signal.value.each_with_index do |v,i|
        res += "\n" + signal.to_python + "[#{i}=#{v.to_python}]"
      end
      res
    else
      signal.to_python + "=" + signal.value.inspect
    end
  else
    if signal.array? then
      signal.to_python + " = " + typ.to_python_init
    else
      signal.to_python + "=" + typ.to_python_init
    end
  end
end.join("\n")}

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

def sequencer():
#{RubyHDL::High.global_sblock.each_signal.map do |signal|
   "  global #{signal.to_python}"
end.join("\n")}
#{@blk.to_python("  ")}
BUILDPYTHON
  return res
end

#to_rubyObject

Get the Ruby code.



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

def to_ruby
  return @code
end