# File lib/ruby-vpi.rb, line 31
 31:   def RubyVpi.init_bench aDesignId, aSpecFormat, &aSimulationCycle
 32:     raise ArgumentError, "block must be given" unless block_given?
 33: 
 34:     if caller.find {|s| s =~ /^(.*?)_bench.rb:/}
 35:       testName = $1
 36:     else
 37:       raise 'Unable to determine name of test.'
 38:     end
 39: 
 40:     useDebugger = !(ENV['DEBUG'] || '').empty?
 41:     useCoverage = !(ENV['COVERAGE'] || '').empty?
 42:     usePrototype = !(ENV['PROTOTYPE'] || '').empty?
 43: 
 44:     # set up code coverage analysis
 45:       # XXX: this is loaded *before* RCov to prevent coverage statistics about it
 46:       require 'ruby-vpi/vpi'
 47: 
 48:       if useCoverage
 49:         require 'ruby-vpi/rcov'
 50: 
 51:         RubyVpi.with_coverage_analysis do |a|
 52:           a.dump_coverage_info [
 53:             Rcov::TextReport.new,
 54:             Rcov::HTMLCoverage.new(:destdir => "#{testName}_coverage")
 55:           ]
 56:         end
 57: 
 58:         Vpi::vpi_printf "#{Config::PROJECT_NAME}: coverage analysis is enabled for test #{testName.inspect}\n"
 59:       end
 60: 
 61:     # set up the specification library
 62:       case aSpecFormat
 63:         when :xUnit
 64:           require 'test/unit'
 65: 
 66:         when :rSpec
 67:           ARGV.concat %w[-f s]
 68:           require 'spec'
 69: 
 70:         when :tSpec
 71:           ARGV << '-rs'
 72:           require 'test/spec'
 73:       end
 74: 
 75:     # set up the interactive debugger
 76:       if useDebugger
 77:         require 'ruby-debug'
 78: 
 79:         Debugger.start
 80:         Debugger.post_mortem
 81: 
 82:         Vpi::vpi_printf "#{Config::PROJECT_NAME}: debugger is enabled for test #{testName.inspect}\n"
 83:       end
 84: 
 85:       # suppress undefined method errors when debugger is not enabled
 86:         unless Kernel.respond_to? :debugger
 87:           Kernel.class_eval do
 88:             define_method :debugger do
 89:               # this is a dummy method!
 90:             end
 91:           end
 92:         end
 93: 
 94:     # set up the VPI utility layer
 95:       Object.class_eval do
 96:         include Vpi
 97:       end
 98: 
 99:     # load the design under test
100:       unless design = vpi_handle_by_name("#{testName}_bench", nil)
101:         raise "Verilog bench for test #{testName.inspect} is inaccessible."
102:       end
103: 
104:       Kernel.const_set(aDesignId, design)
105:       require "#{testName}_design.rb"
106: 
107:     # load the design's prototype
108:       if usePrototype
109:         require "#{testName}_proto.rb"
110: 
111:         Vpi.module_eval do
112:           define_method :simulate do
113:             design.simulate!
114:           end
115: 
116:           define_method :vpi_register_cb do
117:             warn "vpi_register_cb: callbacks not allowed when prototyping"
118:           end
119:         end
120: 
121:         Vpi::vpi_printf "#{Config::PROJECT_NAME}: prototype is enabled for test #{testName.inspect}\n"
122: 
123:       else
124:         Vpi.module_eval do
125:           define_method :simulate, &aSimulationCycle
126:         end
127: 
128:         # XXX: this completes the handshake, by calling relay_verilog, with pthread_mutex_lock() in relay_main() in the C extension
129:         advance_time
130:       end
131: 
132:     # load the design's specification
133:       require "#{testName}_spec.rb"
134:   end