Module: TdlSpace::VarElemenAttr

Included in:
TdlBaseInterface
Defined in:
lib/tdl/rebuild_ele/ele_base.rb

Instance Method Summary collapse

Instance Method Details

#_io_map(tdl_key, hdl_key, default_value, flag, other = nil) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 202

def _io_map(tdl_key,hdl_key,default_value,flag,other=nil)
    interface_io = get_class_var('origin_interface_io',{})
    interface_io[tdl_key.to_s] = [hdl_key.to_s,default_value,flag,other]
    set_class_var('origin_interface_io',interface_io)

    ## 定义实例方法
    self.class_exec(tdl_key) do |tdl_key|
        define_method(tdl_key) do 
            rel = self.instance_variable_get("@_#{tdl_key}_") || default_value
            if !dimension || dimension.empty?
                rel || TdlSpace::ArrayChain.create(obj:"#{self.inst_name}.#{hdl_key}", belong_to_module: belong_to_module)
            else 
                rel || TdlSpace::ArrayChain.create(obj:"#{self.inst_name}[0].#{hdl_key}", belong_to_module: belong_to_module)
            end
        end

        define_method("#{tdl_key}=") do |arg|
            self.instance_variable_set("@_#{tdl_key}_",arg)
        end

    end
end

#clock_io_map(tdl_key, hdl_key, default_freqM = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 152

def clock_io_map(tdl_key,hdl_key,default_freqM=nil)
    _io_map(tdl_key,hdl_key,nil,"clock",default_freqM)
     ## 定义实例方法
     self.class_exec(tdl_key) do |tdl_key|
        define_method('clock') do 
            rel = self.instance_variable_get("@_#{tdl_key}_")
            if !dimension || dimension.empty?
                rel || TdlSpace::ArrayChain.create(obj:"#{self.inst_name}.#{hdl_key}", belong_to_module: belong_to_module)
            else  
                rel || TdlSpace::ArrayChain.create(obj:"#{self.inst_name}[0].#{hdl_key}", belong_to_module: belong_to_module)
            end
        end

        define_method("clock=") do |arg|
            self.instance_variable_set("@_#{tdl_key}_",arg)
        end

    end
end

#comm_io_map(tdl_key, hdl_key, default_value = nil) ⇒ Object



192
193
194
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 192

def comm_io_map(tdl_key,hdl_key,default_value=nil)
    _io_map(tdl_key,hdl_key,default_value,nil)
end

#comm_io_maps_same(*tdl_keys) ⇒ Object



196
197
198
199
200
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 196

def comm_io_maps_same(*tdl_keys)
    tdl_keys.each do |e|
        comm_io_map(e,e,nil)
    end
end

#gen_sv_interface(path) ⇒ Object

生成 SV 文件



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
303
304
305
306
307
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
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 247

def gen_sv_interface(path)
    File.open(File.join(path,get_class_var('hdl_name')+".sv"),'w') do |f|

        inerface_params = get_class_var('origin_interface_params',{})

        par_str = []
        inerface_params.each do |k,v|
            par_str << "    parameter #{v[0]} = #{v[1]}"
        end

        interface_io = get_class_var('origin_interface_io',{})

        clock_reset_str = []
        sdata_str = []
        pdata_str = []
        interface_io.each do |k,v|
            ## 查找clock reset
            if v[2]=="clock" || v[2]=="reset"
                clock_reset_str << "    input #{v[0]}"
            end 

            ## 单信号
            if v[2] == "sdata"
                sdata_str << "logic #{v[0]};"
            end

            ## 多信号
            if v[2] == "pdata"
                dv = v[3]
                
                dv.map! do |e| 
                    if inerface_params.keys.include?(e.to_s )
                        inerface_params[e.to_s][0]
                    else 
                        e.to_s
                    end
                end 

                dv_str = dv.map do |e| 
                    "[#{e}-1:0]"
                end.join() 

                pdata_str << "logic #{dv_str} #{v[0]};"
            end

        end

        if par_str.any?
            par_str = "#(\n#{par_str.join(",\n")}\n)"
        else 
            par_str = ''
        end

        if clock_reset_str.any? 
            clock_reset_str = "\n"+clock_reset_str.join(",\n")+"\n"
        else  
            clock_reset_str = ''
        end

        if sdata_str.any? 
            sdata_str = "\n"+sdata_str.join("\n")+"\n"
        else 
            sdata_str = ""
        end

        if pdata_str.any? 
            pdata_str = "\n"+pdata_str.join("\n")+"\n"
        else  
            pdata_str = ''
        end

        interface_modports = get_class_var('interface_modports',[])

        sub_modport = []
        interface_modports.each do |e|
            interface_modport_signals_in  = get_class_var("#{e}_interface_modport_signals_input",[])
            interface_modport_signals_out = get_class_var("#{e}_interface_modport_signals_output",[])
            sub_modport << "modport #{e} (\n"

            xsub_modport = []
            xsub_modport += interface_modport_signals_in.map do |m| 
                if inerface_params.keys.include?(m.to_s )
                    inerface_params[m.to_s][0]
                else 
                    m.to_s
                end 
            end.map do |m| 
                "input #{m}"
            end
    
            xsub_modport += interface_modport_signals_out.map do |m| 
                if inerface_params.keys.include?(m.to_s )
                    inerface_params[m.to_s][0]
                else 
                    m.to_s
                end 
            end.map do |m| 
                "output #{m}"
            end


            sub_modport <<  xsub_modport.join(",\n")
            sub_modport << "\n);\n"
        end

        if sub_modport.any? 
            sub_modport = sub_modport.join('')
        else  
            sub_modport = ''
        end 
        
       
        f.puts "interface #{get_class_var('hdl_name')} #{par_str} (#{clock_reset_str});#{sdata_str}#{pdata_str}"
        f.puts sub_modport
        f.puts "endinterface:#{get_class_var('hdl_name')}"
    end 
end

#get_class_var(name, default = nil) ⇒ Object

hdl_name :axi_stream_inf modports :master,:slaver,:mirror,:mirror_out param_map :dsize,‘DSIZE’,8 ## <tdl_key><hdl_key><default_value> clock_io_map :aclk,:aclk,100 ## <tdl_key><hdl_key><default_freqM> reset_io_map :aresetn,:aresetn sdata_maps :axis_tvalid,:axis_tready,:axis_tuser

pdata_map   :axis_tdata,[:dsize]
pdata_map   :axis_tkeep,[]
pdata_map   :axis_tcnt


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 20

def get_class_var(name,default=nil)
    # begin
    #     instance_variable_get("@_#{name}_")
    # rescue 
    #     instance_variable_set("@_#{name}_",nil)
    # end

    unless instance_variable_get("@_#{name}_")
        instance_variable_set("@_#{name}_",default)
        container = instance_variable_get("@_#{name}_")
    else
        container = instance_variable_get("@_#{name}_")
    end
    
    return container
end

#hdl_name(name, *nicknames) ⇒ Object



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
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 43

def hdl_name(name,*nicknames)
    # @@__hdl_name__ = name.to_s 
    set_class_var('hdl_name',name.to_s)

    ## 给 sdlmodule 定义例化方法
    ## 例化名
    ## 比如 sv  接口 axi4,可以在tdl里面这样用 axi4() ; `name`=axi4
    self.class_exec(name) do |name|
        SdlModule.class_exec(name,self) do |name,ele_class|
            define_method(name) do |args={}|
                hash = args || {}
                hash[:belong_to_module] = self
                rel = TdlSpace::DefEleBaseArrayChain.new(hash)
                rel.tclass = ele_class
                return rel
            end

            nicknames.each do |nn|
                alias_method nn,name
            end
        end

        _self = self

        TdlSpace::DefPortArrayChain.class_exec(name,self) do |name,ele_class|
            define_method(name) do 
                rel = TdlSpace::DefPortEleBaseArrayChain.new(ele_class,belong_to_module)
                return rel
            end

            nicknames.each do |nn|
                alias_method nn,name
            end

        end
    end

    ## 给例化模块时的 SdlInstPortSugar 添加 方法
    SdlInstPortSugar.class_exec(name,nicknames)  do |name,nicknames|
        define_method(name) do 
            return self
        end

        nicknames.each do |ne|
            define_method(ne) do 
                return self 
            end
        end
    end
end

#modports(*args) ⇒ Object



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
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 94

def modports(*args)
    # @@__interface_modports__ ||= []
    # @@__interface_modports__ += args
    interface_modports = get_class_var('interface_modports',[])
    interface_modports += args 
    set_class_var('interface_modports',interface_modports)

    args.each do |e|
        # puts ("modport_#{e}_input")
        define_singleton_method("modport_#{e}_input") do |*xargs|
            interface_modport_signals = get_class_var("#{e}_interface_modport_signals_input",[])
            interface_modport_signals += xargs
            interface_modport_signals.uniq!
            set_class_var("#{e}_interface_modport_signals_input",interface_modport_signals)
        end

        define_singleton_method("modport_#{e}_output") do |*xargs|
            interface_modport_signals = get_class_var("#{e}_interface_modport_signals_output",[])
            interface_modport_signals += xargs
            interface_modport_signals.uniq!
            set_class_var("#{e}_interface_modport_signals_output",interface_modport_signals)
        end

    end

end

#param_map(tdl_key, hdl_key, default_value = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 121

def param_map(tdl_key,hdl_key,default_value=nil)
    interface_params = get_class_var('origin_interface_params',{})
    interface_params[tdl_key.to_s] = [hdl_key.to_s,default_value]
    set_class_var('origin_interface_params',interface_params)

    ## 定义实例方法
    self.class_exec(tdl_key,hdl_key) do |tdl_key,hdl_key|
        define_method(tdl_key) do 
            rel = self.instance_variable_get("@_#{tdl_key}_")
            unless rel 
                if !dimension || dimension.empty?
                    TdlSpace::ArrayChain.create(obj: "#{inst_name}.#{hdl_key}".to_nq, belong_to_module: belong_to_module)
                else 
                    TdlSpace::ArrayChain.create(obj: "#{inst_name}[0].#{hdl_key}".to_nq, belong_to_module: belong_to_module)
                end
            else 
                rel 
            end
        end

        define_method("#{tdl_key}=") do |arg|
            self.instance_variable_set("@_#{tdl_key}_",arg)
        end

        define_method(hdl_key) do 
            self.send(tdl_key)
        end

    end
end

#pdata_map(name, dimension = []) ⇒ Object



236
237
238
239
240
241
242
243
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 236

def pdata_map(name,dimension=[])
    _io_map(name,name,nil,'pdata',dimension)
    self.class_exec(name) do |e|
        define_method(e) do 
            TdlSpace::ArrayChain.create(obj:"#{self.inst_name}.#{e}", belong_to_module: belong_to_module)
        end
    end
end

#reset_io_map(tdl_key, hdl_key, active = 'low') ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 172

def reset_io_map(tdl_key,hdl_key,active='low')
    _io_map(tdl_key,hdl_key,nil,"reset",active.to_s.downcase)
     ## 定义实例方法
     self.class_exec(tdl_key) do |tdl_key|
        define_method('reset') do 
            rel = self.instance_variable_get("@_#{tdl_key}_")
            if !dimension || dimension.empty?
                rel || TdlSpace::ArrayChain.create(obj:"#{self.inst_name}.#{hdl_key}", belong_to_module: belong_to_module)
            else 
                rel || TdlSpace::ArrayChain.create(obj:"#{self.inst_name}[0].#{hdl_key}", belong_to_module: belong_to_module)
            end
        end

        define_method("reset=") do |arg|
            self.instance_variable_set("@_#{tdl_key}_",arg)
        end

    end
end

#sdata_maps(*args) ⇒ Object



225
226
227
228
229
230
231
232
233
234
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 225

def sdata_maps(*args)
    args.each do |e|
        _io_map(e,e,nil,'sdata',nil)
        self.class_exec(e) do |e|
            define_method(e) do 
                TdlSpace::ArrayChain.create(obj:"#{self.inst_name}.#{e}", belong_to_module: belong_to_module)
            end
        end
    end
end

#set_class_var(name, value) ⇒ Object



37
38
39
40
41
# File 'lib/tdl/rebuild_ele/ele_base.rb', line 37

def set_class_var(name,value)
    # rel = get_class_var(name)
    instance_variable_set("@_#{name}_",value)
    container = instance_variable_get("@_#{name}_")
end