Module: HDLRuby::Low::Low2C

Defined in:
lib/HDLRuby/hruby_low2c.rb

Overview

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

Constant Summary collapse

@@hdrobj2c =

Generates a uniq name for an object.

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

{}

Class Method Summary collapse

Class Method Details

.behavior_access(obj) ⇒ Object

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



150
151
152
153
154
155
# File 'lib/HDLRuby/hruby_low2c.rb', line 150

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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/HDLRuby/hruby_low2c.rb', line 54

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_string(str) ⇒ Object

Converts string +str+ to a C-compatible string.



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

def self.c_string(str)
    str = str.gsub(/\n/,"\\n")
    str.gsub!(/\t/,"\\t")
    return str
end

.code_name(obj) ⇒ Object

Generates the name of a executable function for an object.



104
105
106
# File 'lib/HDLRuby/hruby_low2c.rb', line 104

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
28
# File 'lib/HDLRuby/hruby_low2c.rb', line 21

def self.includes(*names)
    res =  '#include <stdlib.h>' + "\n" + 
           '#include <string.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.



31
32
33
34
# File 'lib/HDLRuby/hruby_low2c.rb', line 31

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+



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/HDLRuby/hruby_low2c.rb', line 132

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"
    # Enable it.
    res << "   set_enable_system(top_system,1);\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.



99
100
101
# File 'lib/HDLRuby/hruby_low2c.rb', line 99

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

.obj_name(obj) ⇒ Object

Generates a uniq name for an object.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/HDLRuby/hruby_low2c.rb', line 86

def self.obj_name(obj)
    id = obj.hierarchy.map! {|obj| obj.object_id}
    oname = @@hdrobj2c[id]
    unless oname then
        # name = obj.respond_to?(:name) ? "_#{self.c_name(obj.name)}" : ""
        # oname = "_c#{@@hdrobj2c.size}#{name}"
        oname = "_" << @@hdrobj2c.size.to_s(36)
        @@hdrobj2c[id] = oname
    end
    return oname
end

.prototype(name) ⇒ Object

Generates a prototype from a function +name+.



119
120
121
# File 'lib/HDLRuby/hruby_low2c.rb', line 119

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.



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

def self.thread(name)

end

.type_name(obj) ⇒ Object

Generates the name of a type.



109
110
111
# File 'lib/HDLRuby/hruby_low2c.rb', line 109

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

.unit_name(obj) ⇒ Object

Generates the name of a unit.



114
115
116
# File 'lib/HDLRuby/hruby_low2c.rb', line 114

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.



160
161
162
163
164
# File 'lib/HDLRuby/hruby_low2c.rb', line 160

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