module HDLRuby::Low::Low2C

Provides tools for converting HDLRuby::Low objects to C.

Public Class Methods

behavior_access(obj) click to toggle source

Gets the structure of the behavior containing object obj.

# File lib/HDLRuby/hruby_low2c.rb, line 132
def self.behavior_access(obj)
    until obj.is_a?(Behavior)
        obj = obj.parent
    end
    return Low2C.obj_name(obj)
end
c_name(name) click to toggle source

Converts a name to a C-compatible name.

# File lib/HDLRuby/hruby_low2c.rb, line 53
def self.c_name(name)
    name = name.to_s
    # Convert special characters.
    name = name.each_char.map do |c|
        if c=~ /[a-z0-9]/ then
            c
        elsif c == "_" then
            "__"
        else
            "_" + c.ord.to_s
        end
    end.join
    # First character: only letter is possible.
    unless name[0] =~ /[a-z_]/ then
        name = "_" + name
    end
    return name
end
c_name?(name) click to toggle source

Tells if a name is C-compatible. To ensure compatibile, assume all the character must have the same case.

# File lib/HDLRuby/hruby_low2c.rb, line 45
def self.c_name?(name)
    name = name.to_s
    # First: character check.
    return false unless name =~ /^[a-zA-Z]|([a-zA-Z][a-zA-Z_0-9]*[a-zA-Z0-9])$/
    return true
end
c_string(str) click to toggle source

Converts string str to a C-compatible string.

# File lib/HDLRuby/hruby_low2c.rb, line 36
def self.c_string(str)
    str = str.gsub(/\n/,"\\n")
    str.gsub!(/\t/,"\\t")
    return str
end
code_name(obj) click to toggle source

Generates the name of a executable function for an object.

# File lib/HDLRuby/hruby_low2c.rb, line 88
def self.code_name(obj)
    return "code#{Low2C.obj_name(obj)}"
end
includes(*names) click to toggle source

Generates the includes for a C file, with names for extra h files.

# File lib/HDLRuby/hruby_low2c.rb, line 21
def self.includes(*names)
    res =  '#include <stdlib.h>' + "\n" + 
           '#include "hruby_sim.h"' + "\n"
    names.each { |name| res << "#include \"#{name}\"\n" }
    res << "\n"
    return res
end
int_width() click to toggle source

Gives the width of an int in the current computer.

# File lib/HDLRuby/hruby_low2c.rb, line 30
def self.int_width
    # puts "int_width=#{[1.to_i].pack("i").size*8}"
    return [1.to_i].pack("i").size*8
end
main(name,init_visualizer,top,objs,hnames) click to toggle source

Generates the main for making the objects of objs and for starting the simulation and including the files from hnames

# File lib/HDLRuby/hruby_low2c.rb, line 116
def self.main(name,init_visualizer,top,objs,hnames)
    res = Low2C.includes(*hnames)
    res << "int main(int argc, char* argv[]) {\n"
    # Build the objects.
    objs.each { |obj| res << "   #{Low2C.make_name(obj)}();\n" }
    # Sets the top systemT.
    res << "   top_system = #{Low2C.obj_name(top)};\n"
    # Starts the simulation.
    res<< "   hruby_sim_core(\"#{name}\",#{init_visualizer},-1);\n"
    # Close the main.
    res << "}\n"
    return res
end
make_name(obj) click to toggle source

Generates the name of a makeer for an object.

# File lib/HDLRuby/hruby_low2c.rb, line 83
def self.make_name(obj)
    return "make#{Low2C.obj_name(obj)}"
end
obj_name(obj) click to toggle source

Generates a uniq name for an object.

# File lib/HDLRuby/hruby_low2c.rb, line 73
def self.obj_name(obj)
    if obj.respond_to?(:name) then
        return Low2C.c_name(obj.name.to_s) +
               Low2C.c_name(obj.object_id.to_s)
    else
        return "_" + Low2C.c_name(obj.object_id.to_s)
    end
end
prototype(name) click to toggle source

Generates a prototype from a function name.

# File lib/HDLRuby/hruby_low2c.rb, line 103
def self.prototype(name)
    return "void #{name}();\n"
end
thread(name) click to toggle source

Generates the code of a thread calling name function and register it to the simulator.

# File lib/HDLRuby/hruby_low2c.rb, line 109
def self.thread(name)

end
type_name(obj) click to toggle source

Generates the name of a type.

# File lib/HDLRuby/hruby_low2c.rb, line 93
def self.type_name(obj)
    return "type#{Low2C.obj_name(obj)}"
end
unit_name(obj) click to toggle source

Generates the name of a unit.

# File lib/HDLRuby/hruby_low2c.rb, line 98
def self.unit_name(obj)
    return "#{obj.to_s.upcase}"
end
wait(obj,level) click to toggle source

Generates the code for a wait for time object obj with level identation.

# File lib/HDLRuby/hruby_low2c.rb, line 142
def self.wait(obj,level)
    return "hw_wait(#{obj.delay.to_c(level+1)}," + 
           "#{Low2C.behavior_access(obj)});\n" 
end