[Ruby]VerilogトップRTLからテストベンチテンプレート生成

不完全だが、ざっと雛形作ってくれるRuby。
blogへソース埋め込むHTML記述練習。

#----------------------------------------------------------------------
# file : mk_top2tb.rb
# func : make testbench template from top module
# command: ruby mk_top2tb.rb
#----------------------------------------------------------------------
## Input file reads.
ifile_name, ofile_name = ARGV
ifile = File::open(ifile_name, "r")
ofile = File::open(ofile_name, "a")

in_a = []
out_a = []
bid_a = []
port_a = []

@module_name == ""
@tb_name == ""
@bus_ext == ""

print "Input file = " + ifile_name + "\n"
print "Output file = " + ofile_name + "\n"

#---------------------------------------------------
# check rtl
#---------------------------------------------------
ifile.each do |line|
line.force_encoding('UTF-8')
line = line.encode("UTF-16BE", "UTF-8", :invalid => :replace, :undef => :replace, :replace => '?').encode("UTF-8")

line = line.sub(/\/\/.*/,"").sub(/; */,"").sub(/^ */,"") ; # delete comment, head space, end space

if /`include */ =~ line
ofile.print line
end

if /module *[a-zA-Z0-9_]* *\(/ =~ line
temp_a = line.split(/module|\s/)
@module_name = temp_a[2]
@tb_name = "tb_" + temp_a[2]
end

if /^\s*input[^a-zA-Z0-9_]/ =~ line
temp_a = line.split(/input *|,|;|\s/)
temp_a.delete("")
in_a << temp_a
port_a = port_a + temp_a
end

if /^\s*output[^a-zA-Z0-9_]/ =~ line
temp_a = line.split(/output *|,|;|\s/)
temp_a.delete("")
out_a << temp_a
port_a = port_a + temp_a
end

if /^\s*inout[^a-zA-Z0-9_]/ =~ line
temp_a = line.split(/inout *|,|;|\s/)
temp_a.delete("")
bid_a << temp_a
port_a = port_a + temp_a
end
end

#---------------------------------------------------
# write to testbench file
#---------------------------------------------------
ofile.print "module " + @tb_name + ";\n\n"

ofile.print " parameter HCYCLE = 1000 ;\n\n"

ofile.print " // input port ----------------------\n"
in_a.each do |i|
if /\[/ =~ i[0]
@bus_ext = i[0]
i.delete_at(0)
else
@bus_ext = ""
end
i.each do |hoge|
ofile.print " reg " + @bus_ext + " " + hoge + ";\n"
end
end

ofile.print "\n"
ofile.print " // output port ----------------------\n"
out_a.each do |i|
if /\[/ =~ i[0]
@bus_ext = i[0]
i.delete_at(0)
else
@bus_ext = ""
end
i.each do |hoge|
ofile.print " wire " + @bus_ext + " " + hoge + ";\n"
end
end

ofile.print "\n"
ofile.print " // bidire port ----------------------\n"
bid_a.each do |i|
p "--------------------------------------------------"
p i
if /\[/ =~ i[0]
@bus_ext = i[0]
i.delete_at(0)
else
@bus_ext = ""
end
i.each do |hoge|
ofile.print " reg " + @bus_ext + " " + hoge + "_in;\n"
ofile.print " wire " + @bus_ext + " " + hoge + " = " + hoge + "_in;\n"
end
end

ofile.print "\n"
ofile.print " // uut ----------------------\n"
ofile.print " " + @module_name + " uut (\n"
cnt = 0

port_a.delete_if{|item| item =~ /\[/ }
port_a.each do |i|
if cnt == 0
ofile.print " ." + i + "(" + i + ")\n"
else
ofile.print " , ." + i + "(" + i + ")\n"
end
cnt = cnt + 1
end
ofile.print " );\n"

ofile.print "\n"
ofile.print " // -----------------------------------\n"
ofile.print " // test pattern\n"
ofile.print " // -----------------------------------\n"
ofile.print " initial begin\n"
ofile.print " // initialize ---------------\n"
in_a.each do |i|
i.delete_if{|item| item =~ /\[/ }
i.each do |hoge|
ofile.print " " + hoge + " = 0;\n"
end
end
bid_a.each do |i|
i.delete_if{|item| item =~ /\[/ }
i.each do |hoge|
ofile.print " " + hoge + "_in = 0;\n"
end
end
ofile.print "\n"
ofile.print " @(negedge clk_i) ;\n"
ofile.print " @(negedge clk_i) ;\n"
ofile.print " @(negedge clk_i) ;\n"
ofile.print "\n"
ofile.print " end\n"

ofile.print "\n"
ofile.print " // -----------------------------------\n"
ofile.print " // clock\n"
ofile.print " // -----------------------------------\n"
ofile.print " always begin\n"
ofile.print " clk_i = 0;\n"
ofile.print " #HCYCLE;\n"
ofile.print " clk_i = 1;\n"
ofile.print " #HCYCLE;\n"
ofile.print " end\n"

ofile.print "\n"
ofile.print "endmodule\n"

ofile.close


スポンサーサイト

[Ruby]10進数⇔2進数と10進数⇔16進数変換

http://www2u.biglobe.ne.jp/~MAS/ruby/32BFCA3130BFCABFF4CAD1B4B9.html

<DEC2BIN(Integer#to_s使用>
a = 255
puts a.to_s(2) # 11111111

<BIN2DEC(String#oct使用)>
a = '10101111'
puts ("0b" + a).oct # 175


<DEC2HEX(Integer#to_s使用)>
a = 255
puts a.to_s(16) # ff

<HEX2DEC(String#hex)>
a = 'a0'
puts a.hex # 160

カレンダー
06 | 2018/07 | 08
1 2 3 4 5 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 - - - -
累積訪問者
現在の訪問者
現在の閲覧者数:
最新記事
最新トラックバック
最新コメント
月別アーカイブ
カテゴリ
プロフィール

bobgosso

Author:bobgosso
FPGAのブログへようこそ!

検索フォーム
RSSリンクの表示
リンク
ブロとも申請フォーム

この人とブロともになる

QRコード
QRコード