Module: HDLRuby::Low::Low2C

Defined in:
lib/HDLRuby/hruby_low2c.rb

Overview

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

Class Method Summary collapse

Class Method Details

.behavior_access(obj) ⇒ Object

Gets the structure of the behavior containing object +obj+.



125
126
127
128
129
130
# File 'lib/HDLRuby/hruby_low2c.rb', line 125

def self.behavior_access(obj)
    until obj.is_a?(Behavior)
        obj = obj.parent
    end
    return Low2C.obj_name(obj)
end

.c_name(name) ⇒ Object

Converts a +name+ to a C-compatible name.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/HDLRuby/hruby_low2c.rb', line 46

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) ⇒ Boolean

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

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/HDLRuby/hruby_low2c.rb', line 38

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

.code_name(obj) ⇒ Object

Generates the name of a executable function for an object.



81
82
83
# File 'lib/HDLRuby/hruby_low2c.rb', line 81

def self.code_name(obj)
    return "code#{Low2C.obj_name(obj)}"
end

.includes(*names) ⇒ Object

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



21
22
23
24
25
26
27
# 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_widthObject

Gives the width of an int in the current computer.



30
31
32
33
# 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) ⇒ Object

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



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/HDLRuby/hruby_low2c.rb', line 109

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) ⇒ Object

Generates the name of a makeer for an object.



76
77
78
# File 'lib/HDLRuby/hruby_low2c.rb', line 76

def self.make_name(obj)
    return "make#{Low2C.obj_name(obj)}"
end

.obj_name(obj) ⇒ Object

Generates a uniq name for an object.



66
67
68
69
70
71
72
73
# File 'lib/HDLRuby/hruby_low2c.rb', line 66

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) ⇒ Object

Generates a prototype from a function +name+.



96
97
98
# File 'lib/HDLRuby/hruby_low2c.rb', line 96

def self.prototype(name)
    return "void #{name}();\n"
end

.thread(name) ⇒ Object

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



102
103
104
# File 'lib/HDLRuby/hruby_low2c.rb', line 102

def self.thread(name)

end

.type_name(obj) ⇒ Object

Generates the name of a type.



86
87
88
# File 'lib/HDLRuby/hruby_low2c.rb', line 86

def self.type_name(obj)
    return "type#{Low2C.obj_name(obj)}"
end

.unit_name(obj) ⇒ Object

Generates the name of a unit.



91
92
93
# File 'lib/HDLRuby/hruby_low2c.rb', line 91

def self.unit_name(obj)
    return "#{obj.to_s.upcase}"
end

.wait(obj, level) ⇒ Object

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



135
136
137
138
# File 'lib/HDLRuby/hruby_low2c.rb', line 135

def self.wait(obj,level)
    return "hw_wait(#{obj.delay.to_c(level+1)}," + 
           "#{Low2C.behavior_access(obj)});\n" 
end