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+.



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

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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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_string(str) ⇒ Object

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



36
37
38
39
40
# 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) ⇒ Object

Generates the name of a executable function for an object.



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

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+



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

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.



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

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

.obj_name(obj) ⇒ Object

Generates a uniq name for an object.



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

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+.



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

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.



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

def self.thread(name)

end

.type_name(obj) ⇒ Object

Generates the name of a type.



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

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

.unit_name(obj) ⇒ Object

Generates the name of a unit.



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

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.



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

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