Class: Rhdl::ModuleGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rhdl/module_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_name, component) ⇒ ModuleGenerator

Returns a new instance of ModuleGenerator.



5
6
7
8
# File 'lib/rhdl/module_generator.rb', line 5

def initialize(module_name, component)
  @module_name = module_name
  @component = component
end

Instance Attribute Details

#componentObject (readonly)

Returns the value of attribute component.



3
4
5
# File 'lib/rhdl/module_generator.rb', line 3

def component
  @component
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



3
4
5
# File 'lib/rhdl/module_generator.rb', line 3

def module_name
  @module_name
end

Instance Method Details

#comb_blockObject



56
57
58
59
60
61
62
# File 'lib/rhdl/module_generator.rb', line 56

def comb_block
  @comb_block ||= component.
    comb_block_dsl.
    statements.
    map{|x| x.generate}.flatten.map{|x| "  #{x}"}.
    join("\n")
end

#generateObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rhdl/module_generator.rb', line 71

def generate
  result = "    module \#{module_name}\n    (\n    \#{params_stmt}\n    );\n    \#{wire_list}\n    always @(*) begin\n    \#{comb_block}\n    end\n  \n    endmodule\n  \n  CONTENT\n  \n  result\nend\n"

#maxlensObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rhdl/module_generator.rb', line 10

def maxlens
  @maxlens ||= begin
    res = {}
    params.each do |paraminfo|
      paraminfo.each do |key, val|
        res[key] ||= 0
        len = (val&.length || 0)
        if res[key] < len
          res[key] = len
        end
      end
    end
  
    res
  end
end

#paramsObject



27
28
29
30
31
# File 'lib/rhdl/module_generator.rb', line 27

def params
  @params ||= [] +
    component.inputs.map{|x| x.generate_terms} +
    component.outputs.map{|x| x.generate_terms}
end

#params_arrObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rhdl/module_generator.rb', line 33

def params_arr
  @params_arr ||= begin
    res = []
    params.each do |paraminfo|
      arr = []
      paraminfo.each do |key, val|
        if val.nil?
          arr << "".ljust(maxlens[key])
          next
        end
  
        arr << val.ljust(maxlens[key])
      end
      res << "  #{arr.join(' ')}"
    end
    res
  end
end

#params_stmtObject



52
53
54
# File 'lib/rhdl/module_generator.rb', line 52

def params_stmt
  @params_stmt ||= params_arr.join(",\n")
end

#wire_listObject



64
65
66
67
68
69
# File 'lib/rhdl/module_generator.rb', line 64

def wire_list
  component.wires.map do |x|
    terms = x.generate_terms
    "reg #{terms[:bitterm]} #{terms[:name]}; // wire"
  end.join("\n")
end