Class: AutoGenTdl

Inherits:
Object
  • Object
show all
Defined in:
lib/tdl/auto_script/autogentdl_a2.rb,
lib/tdl/auto_script/autogentdl_a2.rb

Overview

add auto_path

Constant Summary collapse

@@auto_path =
"./"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = "", out_file_path = "", info = true) ⇒ AutoGenTdl

Returns a new instance of AutoGenTdl.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 6

def initialize(filename="",out_file_path="",info=true)
    @expand_path = File.expand_path(filename)
    sf = File.open(filename,"r")
    fstr = sf.read.force_encoding("utf-8")
    sf.close

    fstr.gsub!(/\/\/\s*\(\*\s*show\s*=\s*"false"\s*\*\)/,"(* show = \"false\" *)")
    fstr.gsub!(/\/\*.*?\*\//m,"")
    fstr.gsub!(/\/\/.*/,"")

    if fstr =~ AxiStream::Synth_REP  # axi_stream class
        @targer_class = "AxiStream"
    elsif fstr =~ Axi4::Synth_REP # axi4 class
        @targer_class = "Axi4"
    elsif fstr =~ VideoInf::Synth_REP  # videoinf class
        @targer_class = "VideoInf"
    elsif fstr =~ DataInf::Synth_REP # datainf class
        @targer_class = "DataInf"
    elsif fstr =~ DataInf_C::Synth_REP# datainf_c class
        @targer_class = "DataInf_C"
    elsif fstr =~ AxiLite::Synth_REP# datainf_c class
        @targer_class = "AxiLite"
    elsif fstr =~ Logic::Synth_REP # only logic
        @targer_class = "Logic"
    else
        puts ">>>#{filename} <<< File's head must mark (* ??? = \"true\" *)\n" if info
        return nil
    end

    @autof_name = File.join(out_file_path,File::basename(filename,".*")+"_auto.rb")
    @autof = File.open(@autof_name,"w")

    regexp_module = /module\s+(?<name>\w+)\s*(#\((?<parameter>.*?)\))?\s*\((?<port>.*?)\)\s*;/m

    mth =  regexp_module.match(fstr)
    @module_name = mth["name"].downcase
    @origin_module_name = mth["name"]
    @parameter_str = mth["parameter"]
    @port_str = mth["port"]
    if @port_str
        @port_str = @port_str.split(",")
    else
        @port_str = []
    end

    @up_stream = nil
    @down_stream = nil

    @inf_array = []
    @collect_array = []
    @format_args_twins = []
    @actial_args_twins = []
    @tmp_actial_args_twins = []
    @alone_args = []
    @ex_params = []
    @ex_ports = []
    @test_obj = []
    @tdl_param_design_hash = []
    @tdl_inst_design_hash = []

    @type_port_args = []

    @inf_port_left_len = 0
    @inf_port_right_len = 0
    @para_port_len = 0


    Parameter.parse_params(@parameter_str) do |h|
        @format_args_twins  << "#{h[:name]}:#{h[:value]}"
        @actial_args_twins  << "#{h[:name]}:#{h[:name]}"
        @tmp_actial_args_twins  << "#{h[:name]}: tmp_#{h[:name]}"
        @alone_args << h[:name]
        @ex_params << h[:inst_ex_port]
        @para_port_len = h[:port_len] if h[:port_len] > @para_port_len
        @test_obj << "#{h[:name]} = Parameter.new(name:\"#{h[:name]}\",value:#{h[:value]})"
        @tdl_param_design_hash << h
    end

    @port_str = SignalElm.parse_ports(@port_str) do |h|
        @format_args_twins  << "#{h[:name]}:\"#{h[:name]}\""
        @actial_args_twins  << "#{h[:name]}:#{h[:name]}"
        @tmp_actial_args_twins  << "#{h[:name]}: tmp_#{h[:name]}"
        @alone_args << h[:name]

        @ex_ports << h[:inst_ex_port]
        @inf_port_left_len =  h[:port_left_len]   if h[:port_left_len]  > @inf_port_left_len
        @inf_port_right_len = h[:port_right_len]  if h[:port_right_len] > @inf_port_right_len
        @test_obj << "#{h[:name]} = Logic.new(name:\"#{h[:name]}\")"
        @tdl_inst_design_hash << h

        @type_port_args << h[:name] if(@targer_class.eql?(h[:type].to_s) && h[:vector].nil? )
    end

    inf_proc = Proc.new { |h|
        @format_args_twins  << "#{h[:name]}:\"#{h[:name]}\""
        @actial_args_twins  << "#{h[:name]}:#{h[:name]}"
        @tmp_actial_args_twins  << "#{h[:name]}: tmp_#{h[:name]}"
        @inf_array  << h[:name] if h[:vector]
        @collect_array << {name:h[:name],type:h[:type],way:h[:way]}
        @alone_args << h[:name]
        if h[:up_down] == "up_stream"
            @up_stream = h[:name]
        elsif h[:up_down] == "down_stream"
            @down_stream = h[:name]
        end
        @ex_ports << h[:inst_ex_port]
        @inf_port_left_len =  h[:port_left_len]   if h[:port_left_len]  > @inf_port_left_len
        @inf_port_right_len = h[:port_right_len]  if h[:port_right_len] > @inf_port_right_len

        @test_obj   << "#{h[:name]} = #{h[:type].to_s}.new(name:\"#{h[:origin_name]}\",clock:c0,reset:r0)"
        @tdl_inst_design_hash << h

        @type_port_args << h[:name] if @targer_class.eql?(h[:type].to_s)
    }

    @port_str = DataInf.parse_ports(@port_str,&inf_proc)
    @port_str = DataInf_C.parse_ports(@port_str,&inf_proc)
    @port_str = VideoInf.parse_ports(@port_str,&inf_proc)
    @port_str = AxiLite.parse_ports(@port_str,&inf_proc)
    @port_str = AxiStream.parse_ports(@port_str,&inf_proc)
    @port_str = Axi4.parse_ports(@port_str,&inf_proc)

    @@inf_parse_methods.each do |m|
        @port_str = m.call(@port_str,&inf_proc)
    end

end

Instance Attribute Details

#axi4_portsObject (readonly)

Returns the value of attribute axi4_ports.



5
6
7
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 5

def axi4_ports
  @axi4_ports
end

#axis_portsObject (readonly)

Returns the value of attribute axis_ports.



5
6
7
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 5

def axis_ports
  @axis_ports
end

#data_c_portsObject (readonly)

Returns the value of attribute data_c_ports.



5
6
7
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 5

def data_c_ports
  @data_c_ports
end

#data_portsObject (readonly)

Returns the value of attribute data_ports.



5
6
7
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 5

def data_ports
  @data_ports
end

#inf_portsObject (readonly)

Returns the value of attribute inf_ports.



5
6
7
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 5

def inf_ports
  @inf_ports
end

#lite_portsObject (readonly)

Returns the value of attribute lite_ports.



5
6
7
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 5

def lite_ports
  @lite_ports
end

#normal_portsObject (readonly)

Returns the value of attribute normal_ports.



5
6
7
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 5

def normal_ports
  @normal_ports
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 5

def params
  @params
end

#video_portsObject (readonly)

Returns the value of attribute video_ports.



5
6
7
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 5

def video_ports
  @video_ports
end

Class Method Details

.add_inf_parse(parse_method) ⇒ Object



135
136
137
138
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 135

def self.add_inf_parse(parse_method)
    @@inf_parse_methods ||= []
    @@inf_parse_methods << parse_method
end

.auto_pathObject



428
429
430
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 428

def self.auto_path
    @@auto_path
end

.auto_path=(path) ⇒ Object



432
433
434
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 432

def self.auto_path=(path)
    @@auto_path=path
end

Instance Method Details

#add_LastModuleInstNameObject



304
305
306
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 304

def add_LastModuleInstName()
    "GlobalParam.LastModuleInstName = instance_name "
end

#auto_rb(ex_methods_str: "") ⇒ Object



414
415
416
417
418
419
420
421
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 414

def auto_rb(ex_methods_str:"")
    if @module_name
        # @autof.puts gen_auto_class(gen_methods(ex_str:ex_methods_str))+gen_tdl_inst_module
        @autof.puts gen_auto_class(gen_methods(ex_str:ex_methods_str))
        @autof.close
        require_relative @autof_name
    end
end

#check_inf_typeObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 194

def check_inf_type
    str = ""
    @collect_array.each do |e|
        str +=
"
if #{e[:name]}.is_a? Array
#{e[:name]}.each do |d|
    raise TdlError.new(\"\#{d.to_s} must a #{e[:type]}\") unless #{e[:name]}.is_a? #{e[:type]}
end
else
raise TdlError.new(\"#{e[:name]} must a #{e[:type]}\") unless #{e[:name]}.is_a? #{e[:type]}
end
"       end
    return str
end

#ex_up_down_argsObject



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 158

def ex_up_down_args
    if(@alone_args.include? "up_stream") && (@alone_args.include? "down_stream")
        ["up_stream:nil","down_stream:nil"]
    elsif @alone_args.include? "up_stream"
        ["up_stream:nil"]
    elsif @alone_args.include? "down_stream"
        ["down_stream:nil"]
    else
        []
    end
end

#ex_up_down_args_aloneObject



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 170

def ex_up_down_args_alone
    if(@alone_args.include? "up_stream") && (@alone_args.include? "down_stream")
        ["up_stream","down_stream"]
    elsif @alone_args.include? "up_stream"
        ["up_stream"]
    elsif @alone_args.include? "down_stream"
        ["down_stream"]
    else
        []
    end
end

#gen_auto_class(method_str) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 182

def gen_auto_class(method_str)
"
##{Time.now}
#require_relative \".././tdl\"

class #{@targer_class}

#{method_str}

end\n\n"
end

#gen_class_method(method_name, ex_up_down_args, ex_up_down_same_args) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 308

def gen_class_method(method_name,ex_up_down_args,ex_up_down_same_args)


    "
public

def self.#{@module_name}(
    #{(@format_args_twins+ex_up_down_args+['belong_to_module:nil']).join(",\n        ")}
    )
    return_stream = nil
    #{
        if @type_port_args.any?
            "belong_to_module = [#{(@type_port_args+ex_up_down_args_alone).join(',')}].first.belong_to_module unless belong_to_module"
        end
    }
    #{
        if @down_stream && @up_stream
"
    if down_stream.nil? && #{@down_stream}.eql?(\"#{@down_stream}\")
        if up_stream.is_a? #{@targer_class}
            down_stream = up_stream.copy
        else
            down_stream = #{@up_stream}.copy
        end
        return_stream = down_stream
    end
"           end
    }
    #{
        if @down_stream && @up_stream
"
    if up_stream.nil? && #{@up_stream}.eql?(\"#{@up_stream}\")
        if down_stream.is_a? #{@targer_class}
            up_stream = down_stream.copy
        else
            up_stream = #{@down_stream}.copy
        end
        return_stream = up_stream
    end
"           end
    }
    #{
        if @down_stream
            "
    if down_stream.is_a? #{@targer_class}
        down_stream.#{method_name}(
            #{(@actial_args_twins+ex_up_down_same_args).join(",\n                ")})
    elsif #{@down_stream}.is_a? #{@targer_class}
        #{@down_stream}.#{method_name}(
            #{(@actial_args_twins+ex_up_down_same_args).join(",\n                ")})
    else
        belong_to_module.#{@targer_class}_NC.#{method_name}(
            #{(@actial_args_twins+ex_up_down_same_args).join(",\n                ")})
    end"
        else
    "belong_to_module.#{@targer_class}_NC.#{method_name}(
        #{(@actial_args_twins+ex_up_down_same_args).join(",\n            ")})"
        end
    }
    return return_stream
end
    "
end

#gen_methods(ex_str: "") ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 221

def gen_methods(ex_str:"")

    if @down_stream
        method_name = @module_name
    else
        method_name = "_"+@module_name
    end

    ex_up_down_args = []
    ex_up_down_args << "up_stream:nil" if @up_stream
    ex_up_down_args << "down_stream:nil" if @down_stream

    ex_up_down_same_args = []
    ex_up_down_same_args << "up_stream:up_stream" if @up_stream
    ex_up_down_same_args << "down_stream:down_stream" if @down_stream

    str =
"
def #{method_name}(
    #{(@format_args_twins+ex_up_down_args).join(",\n        ")}
)
#{ex_str}
    Tdl.add_to_all_file_paths('#{@module_name}','#{@expand_path}')
    # GlobalParam.CurrTdlModule.add_to_all_file_paths(['#{@module_name}','#{@expand_path}'])
    return_stream = self
    #{proc_array_inf}
    #{
        if @down_stream && @up_stream
"
    if up_stream.nil? && #{@up_stream}.eql?(\"#{@up_stream}\") && (!(#{@down_stream}.eql?(\"#{@down_stream}\")) || !down_stream.nil?)
        # up_stream = self.copy(name:\"#{@up_stream}\")
        # return_stream = up_stream
        #{
            if @down_stream
                "#{@down_stream} = down_stream if down_stream"
            end
        }
        return down_stream.#{method_name}(#{@up_stream}:self)
    end
"           end
    }
    #{
        if @up_stream
            "#{@up_stream} = up_stream if up_stream"
        end
    }
    #{
        if @down_stream
            "#{@down_stream} = self unless self==belong_to_module.#{@targer_class}_NC"
    "unless self.eql? belong_to_module.#{@targer_class}_NC
        #{@down_stream} = self
    else
        if down_stream
            #{@down_stream} = down_stream
        end
    end"

        end
    }


    belong_to_module.#{@targer_class}_draw << #{method_name}_draw(
        #{(@actial_args_twins+ex_up_down_same_args).join(",\n            ")})
    return return_stream
end

private

def #{method_name}_draw(
    #{(@format_args_twins+ex_up_down_args).join(",\n        ")}
)

    large_name_len(
        #{@alone_args.join(",\n            ")}
    )
    instance_name = \"#{@origin_module_name}_\#{signal}_inst\"
\"
// FilePath:::#{@expand_path}
#{module_str}\"
end
" + gen_class_method(method_name,ex_up_down_args,ex_up_down_same_args)
end

#gen_tdl_inst_moduleObject



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 372

def gen_tdl_inst_module

"
class Tdl

def Tdl.inst_#{@module_name}(
    #{@format_args_twins.join(",\n        ")})
    hash = TdlHash.new
    #{
        (@tdl_param_design_hash+@tdl_inst_design_hash).map { |e|
            "
    unless #{e[:name]}.is_a? Hash
        hash.case_record(:#{e[:name]},#{e[:name]})
    else
        # hash.new_index(:#{e[:name]})= lambda { a = #{e[:type]}.new(#{e[:name]});a.name = \"#{e[:name]}\";return a }
        # hash[:#{e[:name]}] = lambda { a = #{e[:type]}.new(#{e[:name]});a.name = \"#{e[:name]}\";return a }
        raise TdlError.new('#{@module_name} #{e[:type]} #{e[:name]} TdlHash cant include Proc') if #{e[:name]}.select{ |k,v| v.is_a? Proc }.any?
        lam = lambda {
            a = #{e[:type]}.new(#{e[:name]})
            unless #{e[:name]}[:name]
                a.name = \"#{e[:name]}\"
            end
            return a }
        hash.[]=(:#{e[:name]},lam,false)
    end
            "
        }.join("\n")
    }

    if block_given?
        yield hash
    end

    hash.push_to_module_stack(#{@targer_class},:#{@module_name})
    hash.open_error = true
    return hash
end
end
"
end

#module_strObject



152
153
154
155
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 152

def module_str
    # @origin_module_name+parameter_str+" #{@origin_module_name}_\#{signal}_inst"+port_str
    @origin_module_name+parameter_str+" \#{instance_name}"+port_str
end

#parameter_strObject



140
141
142
143
144
145
146
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 140

def parameter_str
    if @parameter_str
        "#(\n"+@tdl_param_design_hash.map { |e| e[:inst_ex_port].call(@para_port_len) }.join(",\n")+"\n)"
    else
        ""
    end
end

#port_strObject



148
149
150
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 148

def port_str
    "(\n"+@tdl_inst_design_hash.map {|e| e[:inst_ex_port].call(@inf_port_left_len,@inf_port_right_len) }.join(",\n")+"\n);\n"
end

#proc_array_infObject



210
211
212
213
214
215
216
217
218
219
# File 'lib/tdl/auto_script/autogentdl_a2.rb', line 210

def proc_array_inf
    str_map = @collect_array.map do |e|
        if (@inf_array.include? e[:name]) && e[:way]
"        #{e[:name]} = #{e[:type]}.same_name_socket(:#{e[:way].to_s},mix=false,#{e[:name]},nil,belong_to_module) unless #{e[:name]}.is_a? String"
        else
"        #{e[:name]} = #{e[:type]}.same_name_socket(:#{e[:way].to_s},mix=true,#{e[:name]},nil,belong_to_module) unless #{e[:name]}.is_a? String"
        end
    end
    return "\n"+str_map.join("\n")
end