22: def RubyVpi.init_bench aDesignId, aSpecFormat, &aSimulationCycle
23: raise ArgumentError, "block must be given" unless block_given?
24:
25: if caller.find {|s| s =~ /^(.*?)_bench.rb:/}
26: testName = $1
27: else
28: raise 'Unable to determine name of test.'
29: end
30:
31: useDebugger = !(ENV['DEBUG'] || '').empty?
32: useCoverage = !(ENV['COVERAGE'] || '').empty?
33: usePrototype = !(ENV['PROTOTYPE'] || '').empty?
34:
35:
36:
37:
38: require 'ruby-vpi/vpi'
39:
40: if useCoverage
41: require 'ruby-vpi/rcov'
42:
43: RubyVpi.with_coverage_analysis do |a|
44: a.dump_coverage_info [
45: Rcov::TextReport.new,
46: Rcov::HTMLCoverage.new(:destdir => "#{testName}_coverage")
47: ]
48: end
49:
50: Vpi::vpi_printf "#{Config::PROJECT_NAME}: coverage analysis is enabled for test #{testName.inspect}\n"
51: end
52:
53:
54: case aSpecFormat
55: when :xUnit
56: require 'test/unit'
57:
58: when :rSpec
59: ARGV.concat %w[-f s]
60: require 'spec'
61:
62: when :tSpec
63: ARGV << '-rs'
64: require 'test/spec'
65: end
66:
67:
68: if useDebugger
69: require 'ruby-debug'
70:
71: Debugger.start
72: Debugger.post_mortem
73:
74: Vpi::vpi_printf "#{Config::PROJECT_NAME}: debugger is enabled for test #{testName.inspect}\n"
75: end
76:
77:
78: unless Kernel.respond_to? :debugger
79: Kernel.class_eval do
80: define_method :debugger do
81:
82: end
83: end
84: end
85:
86:
87: Object.class_eval do
88: include Vpi
89: end
90:
91: Vpi.module_eval do
92: define_method :simulate, &aSimulationCycle
93: end
94:
95:
96: unless design = vpi_handle_by_name("#{testName}_bench", nil)
97: raise "Verilog bench for test #{testName.inspect} is inaccessible."
98: end
99:
100: Kernel.const_set(aDesignId, design)
101: require "#{testName}_design.rb"
102:
103:
104: if usePrototype
105: require "#{testName}_proto.rb"
106:
107: Vpi.module_eval do
108: define_method :advance_time do |*args|
109: Integer(args.first || 1).times { design.simulate! }
110: end
111:
112: define_method :vpi_register_cb do
113: warn "vpi_register_cb: callbacks are ignored when prototype is enabled"
114: end
115: end
116:
117: Vpi::vpi_printf "#{Config::PROJECT_NAME}: prototype is enabled for test #{testName.inspect}\n"
118:
119: else
120:
121:
122: advance_time
123: end
124:
125:
126: require "#{testName}_spec.rb"
127: end