Class: OrigenSim::Simulator

Inherits:
Object
  • Object
show all
Includes:
Origen::PersistentCallbacks
Defined in:
lib/origen_sim/simulator.rb

Overview

Responsible for managing and communicating with the simulator process, a single instance of this class is instantiated as OrigenSim.simulator

Constant Summary collapse

VENDORS =
[:icarus, :cadence, :synopsys, :generic]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSimulator



19
20
21
22
# File 'lib/origen_sim/simulator.rb', line 19

def initialize
  @simulations = []
  @simulation_open = false
end

Instance Attribute Details

#configurationObject (readonly) Also known as: config

Returns the value of attribute configuration.



11
12
13
# File 'lib/origen_sim/simulator.rb', line 11

def configuration
  @configuration
end

#simulationObject (readonly)

The instance of OrigenSim::Simulation for the current simulation



14
15
16
# File 'lib/origen_sim/simulator.rb', line 14

def simulation
  @simulation
end

#simulationsObject (readonly)

Returns an array containing all instances of OrigenSim::Simulation that were created in the order that they were created



17
18
19
# File 'lib/origen_sim/simulator.rb', line 17

def simulations
  @simulations
end

Instance Method Details

#before_pattern(name) ⇒ Object

Called before every pattern is generated, but we only use it the first time it is called to kick off the simulator process if the current tester is an OrigenSim::Tester



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/origen_sim/simulator.rb', line 498

def before_pattern(name)
  if simulation_tester?
    if OrigenSim.flow || !simulation
      # When running patterns back-to-back, only want to launch the simulator the first time
      start unless simulation
    else
      stop
      start
    end
    # Set the current pattern name in the simulation
    put("a^#{name.sub(/\..*/, '')}")
    @pattern_count ||= 0
    # If running a flow, give the user some feedback about pass/fail status after
    # each individual pattern has completed
    if @pattern_count > 0 && OrigenSim.flow
      simulation.log_results(true)
    end
    @pattern_count += 1
  end
end

#commit_simulation_objects(options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/origen_sim/simulator.rb', line 90

def commit_simulation_objects(options = {})
  sid = options[:id] || id
  ldir = "#{Origen.root}/simulation/#{sid}"
  tmp_dir = "#{Origen.root}/tmp/origen_sim/tmp"
  unless File.exist?(ldir)
    fail "The simulation directory to check in does not exist: #{ldir}"
  end
  Dir.chdir "#{Origen.root}/simulation/" do
    system "tar -cvzf #{sid}.tar.gz #{sid}"
  end

  FileUtils.rm_rf(tmp_dir) if File.exist?(tmp_dir)
  FileUtils.mkdir_p(tmp_dir)
  FileUtils.cp "#{ldir}.tar.gz", tmp_dir

  rc = Origen::RevisionControl.new remote: config[:rc_dir_url], local: tmp_dir
  rc.checkin "#{sid}.tar.gz", unmanaged: true, force: true, comment: 'Checked in via sim:rc command'
ensure
  FileUtils.rm_f "#{ldir}.tar.gz" if File.exist?("#{ldir}.tar.gz")
  FileUtils.rm_rf tmp_dir if File.exist?(tmp_dir)
end

#compiled_dirObject

Returns the directory where the compiled simulation object lives, this should be checked into your Origen app’s repository



138
139
140
141
142
143
144
# File 'lib/origen_sim/simulator.rb', line 138

def compiled_dir
  @compiled_dir ||= begin
    d = "#{Origen.root}/simulation/#{id}/#{config[:vendor]}"
    FileUtils.mkdir_p(d)
    d
  end
end

#config_changed?Boolean

Returns true if the config has been changed since the last time we called save_config_signature



818
819
820
# File 'lib/origen_sim/simulator.rb', line 818

def config_changed?
  Origen.app.session.origen_sim["#{id}_config"] != config
end

#configure(options, &block) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/origen_sim/simulator.rb', line 112

def configure(options, &block)
  fail 'A vendor must be supplied, e.g. OrigenSim::Tester.new(vendor: :icarus)' unless options[:vendor]
  unless VENDORS.include?(options[:vendor])
    fail "Unknown vendor #{options[:vendor]}, valid values are: #{VENDORS.map { |v| ':' + v.to_s }.join(', ')}"
  end
  @configuration = options
  @tmp_dir = nil
end

#cycle(number_of_cycles) ⇒ Object



600
601
602
603
# File 'lib/origen_sim/simulator.rb', line 600

def cycle(number_of_cycles)
  put("3^#{number_of_cycles}")
  simulation.read_sim_output
end

#define_pinsObject

Tells the simulator about the pins in the current device so that it can set up internal handles to efficiently access them



556
557
558
559
560
561
562
563
564
# File 'lib/origen_sim/simulator.rb', line 556

def define_pins
  dut.rtl_pins.each_with_index do |(name, pin), i|
    pin.simulation_index = i
    put("0^#{pin.rtl_name}^#{i}^#{pin.drive_wave.index}^#{pin.compare_wave.index}")
  end
  dut.rtl_pins.each do |name, pin|
    pin.apply_force
  end
end

#define_wavesObject



582
583
584
585
586
587
588
589
# File 'lib/origen_sim/simulator.rb', line 582

def define_waves
  dut.timeset.drive_waves.each_with_index do |wave, i|
    put("6^#{i}^0^#{wave_to_str(wave)}")
  end
  dut.timeset.compare_waves.each_with_index do |wave, i|
    put("6^#{i}^1^#{wave_to_str(wave)}")
  end
end

#dut_versionObject

Returns the version of Origen Sim that the current DUT object was compiled with



829
830
831
832
833
834
# File 'lib/origen_sim/simulator.rb', line 829

def dut_version
  @dut_version ||= begin
    put('i^')
    Origen::VersionString.new(get.strip)
  end
end

#end_simulationObject



591
592
593
# File 'lib/origen_sim/simulator.rb', line 591

def end_simulation
  put('8^')
end

#error(message) ⇒ Object



616
617
618
619
# File 'lib/origen_sim/simulator.rb', line 616

def error(message)
  simulation.logged_errors = true
  Origen.log.error message
end

#error_countObject

Returns the current simulation error count



622
623
624
# File 'lib/origen_sim/simulator.rb', line 622

def error_count
  peek("#{testbench_top}.debug.errors").to_i
end

#fetch_simulation_objects(options = {}) ⇒ Object



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
# File 'lib/origen_sim/simulator.rb', line 51

def fetch_simulation_objects(options = {})
  sid = options[:id] || id
  ldir = "#{Origen.root}/simulation/#{sid}"
  tmp_dir = "#{Origen.root}/tmp/origen_sim/tmp"
  if config[:rc_dir_url]
    unless config[:rc_version]
      puts "You must supply an :rc_version option when using :rc_dir_url (you can set this to something like 'Trunk' or 'master' if you want)"
      exit 1
    end
    if !File.exist?(compiled_dir) ||
       (File.exist?(compiled_dir) && Dir.entries(compiled_dir).size <= 2) ||
       (Origen.app.session.origen_sim[sid] != config[:rc_version]) ||
       options[:force]
      Origen.log.info "Fetching the simulation object for #{sid}..."
      Origen.app.session.origen_sim[sid] = nil # Clear this up front, if the checkout fails we won't know what we have
      FileUtils.rm_rf(tmp_dir) if File.exist?(tmp_dir)
      FileUtils.mkdir_p(tmp_dir)
      FileUtils.mkdir_p("#{Origen.root}/simulation")
      rc = Origen::RevisionControl.new remote: config[:rc_dir_url], local: tmp_dir
      rc.checkout "#{sid}.tar.gz", force: true, version: config[:rc_version]
      FileUtils.mv "#{tmp_dir}/#{sid}.tar.gz", "#{Origen.root}/simulation"
      FileUtils.rm_rf(ldir) if File.exist?(ldir)
      Dir.chdir "#{Origen.root}/simulation/" do
        system "tar -xvf #{sid}.tar.gz"
      end
      Origen.app.session.origen_sim[sid] = config[:rc_version]
    end
  else
    if !File.exist?(compiled_dir) ||
       (File.exist?(compiled_dir) && Dir.entries(compiled_dir).size <= 2)
      puts "There is no previously compiled simulation object in: #{compiled_dir}"
      exit 1
    end
  end
ensure
  FileUtils.rm_f "#{ldir}.tar.gz" if File.exist?("#{ldir}.tar.gz")
  FileUtils.rm_rf tmp_dir if File.exist?(tmp_dir)
end

#generic_run_cmdObject



43
44
45
# File 'lib/origen_sim/simulator.rb', line 43

def generic_run_cmd
  config[:generic_run_cmd]
end

#getObject

Get a message from the simulator, will block until one is received



475
476
477
# File 'lib/origen_sim/simulator.rb', line 475

def get
  simulation.socket.readline
end

#idObject

The ID assigned to the current simulation target, falls back to to the Origen target name if an :id option is not supplied when instantiating the tester



124
125
126
# File 'lib/origen_sim/simulator.rb', line 124

def id
  config[:id] || Origen.target.name
end

#interactive_shutdownObject



713
714
715
# File 'lib/origen_sim/simulator.rb', line 713

def interactive_shutdown
  @interactive_mode = true
end

#log_messages=(val) ⇒ Object

When set to true the simulator will log all messages it receives, note that this must be run in conjunction with -d supplied to the Origen command to actually see the messages



27
28
29
30
31
32
33
# File 'lib/origen_sim/simulator.rb', line 27

def log_messages=(val)
  if val
    put('d^1')
  else
    put('d^0')
  end
end

#on_flow_end(options) ⇒ Object

At the end of a test program flow generation/simulation



489
490
491
492
493
# File 'lib/origen_sim/simulator.rb', line 489

def on_flow_end(options)
  if simulation_tester? && options[:top_level]
    stop
  end
end

#on_flow_start(options) ⇒ Object

At the start of a test program flow generation/simulation



480
481
482
483
484
485
486
# File 'lib/origen_sim/simulator.rb', line 480

def on_flow_start(options)
  if simulation_tester? && options[:top_level]
    OrigenSim.flow = Origen.interface.flow.name
    start
    @pattern_count = 0
  end
end

#on_origen_shutdownObject



732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/origen_sim/simulator.rb', line 732

def on_origen_shutdown
  unless simulations.empty?
    failed = false
    # Stop the current simulation, this is done with the rescue wrapper so that the rest
    # of the shutdown continues if we got in here via a CTRL-C, in which case the simulator
    # is probably already dead
    begin
      stop if simulation_open?
    rescue
      simulation.completed_cleanly = false
    end
    unless @interactive_mode
      if simulations.size == 1
        failed = simulation.failed?
      else
        failed_simulation_count = simulations.count(&:failed?)
        if failed_simulation_count > 0
          Origen.log.error "#{failed_simulation_count} of #{simulations.size} simulations failed!"
          failed = true
        end
      end
      if failed
        Origen.app.stats.report_fail
      else
        Origen.app.stats.report_pass
      end
    end
    puts
    if simulations.size == 1
      puts 'To view the simulation run the following command:'
      puts
      puts "  #{simulation.view_wave_command}"
    else
      puts 'To view the simulations run the following commands:'
      puts
      simulations.each do |simulation|
        if simulation.failed?
          puts "  #{simulation.view_wave_command}".red
        else
          puts "  #{simulation.view_wave_command}"
        end
      end
    end
    puts
    unless @interactive_mode
      failed ? exit(1) : exit(0)
    end
  end
end

#on_timeset_changedObject

This will be called automatically whenever tester.set_timeset has been called



543
544
545
546
547
548
549
550
551
552
# File 'lib/origen_sim/simulator.rb', line 543

def on_timeset_changed
  # Important that this is done first, since it is used to clear the pin
  # and wave definitions in the bridge
  set_period(dut.current_timeset_period)
  # Clear pins and waves
  define_pins
  define_waves
  # Apply the pin reset values / re-apply the existing states
  put_all_pin_states
end

#pattern_generated(path) ⇒ Object

This will be called at the end of every pattern, make sure the simulator is not running behind before potentially moving onto another pattern



522
523
524
# File 'lib/origen_sim/simulator.rb', line 522

def pattern_generated(path)
  sync_up if simulation_tester?
end

#peek(net) ⇒ Object

Returns the current value of the given net, or nil if the given path does not resolve to a valid node

The value is returned as an instance of Origen::Value



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/origen_sim/simulator.rb', line 630

def peek(net)
  # The Verilog spec does not specify that underlying VPI put method should
  # handle a part select, so some simulators do not handle it. Therefore we
  # deal with it here to ensure cross simulator compatibility.

  # http://rubular.com/r/eTVGzrYmXQ
  if net =~ /(.*)\[(\d+):?(\.\.)?(\d*)\]$/
    net = Regexp.last_match(1)
    msb = Regexp.last_match(2).to_i
    lsb = Regexp.last_match(4)
    lsb = lsb.empty? ? nil : lsb.to_i
  end

  sync_up
  put("9^#{clean(net)}")
  m = get.strip

  if m == 'FAIL'
    return nil
  else
    if msb
      # Setting a range of bits
      if lsb
        Origen::Value.new('b' + m[(m.size - 1 - msb)..(m.size - 1 - lsb)])
      else
        Origen::Value.new('b' + m[m.size - 1 - msb])
      end
    else
      Origen::Value.new('b' + m)
    end
  end
end

#poke(net, value) ⇒ Object

Forces the given value to the given net. Note that no error checking is done and no error will be communicated if an illegal net is supplied. The user should follow up with a peek if they want to verify that the poke was applied.



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/origen_sim/simulator.rb', line 667

def poke(net, value)
  # The Verilog spec does not specify that underlying VPI put method should
  # handle a part select, so some simulators do not handle it. Therefore we
  # deal with it here to ensure cross simulator compatibility.

  # http://rubular.com/r/eTVGzrYmXQ
  if !config[:vendor] == :synopsys && net =~ /(.*)\[(\d+):?(\.\.)?(\d*)\]$/
    path = Regexp.last_match(1)
    msb = Regexp.last_match(2).to_i
    lsb = Regexp.last_match(4)
    lsb = lsb.empty? ? nil : lsb.to_i

    v = peek(path)
    return nil unless v
    # Setting a range of bits
    if lsb
      upper = v >> (msb + 1)
      # Make sure value does not overflow
      value = value[(msb - lsb)..0]
      if lsb == 0
        value = (upper << (msb + 1)) | value
      else
        lower = v[(lsb - 1)..0]
        value = (upper << (msb + 1)) |
                (value << lsb) | lower
      end

    # Setting a single bit
    else
      if msb == 0
        upper = v >> 1
        value = (upper << 1) | value[0]
      else
        lower = v[(msb - 1)..0]
        upper = v >> (msb + 1)
        value = (upper << (msb + 1)) |
                (value[0] << msb) | lower
      end
    end
    net = path
  end

  sync_up
  put("b^#{clean(net)}^#{value}")
end

#post_process_run_cmdObject



47
48
49
# File 'lib/origen_sim/simulator.rb', line 47

def post_process_run_cmd
  config[:post_process_run_cmd]
end

#put(msg) ⇒ Object

Send the given message string to the simulator



469
470
471
# File 'lib/origen_sim/simulator.rb', line 469

def put(msg)
  simulation.socket.write(msg + "\n")
end

#put_all_pin_statesObject

Applies the current state of all pins to the simulation



534
535
536
537
538
539
# File 'lib/origen_sim/simulator.rb', line 534

def put_all_pin_states
  dut.rtl_pins.each do |name, pin|
    pin.reset_simulator_state
    pin.update_simulation
  end
end

#rtl_topObject



39
40
41
# File 'lib/origen_sim/simulator.rb', line 39

def rtl_top
  config[:rtl_top] || 'dut'
end

#run_cmdObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
# File 'lib/origen_sim/simulator.rb', line 197

def run_cmd
  case config[:vendor]
  when :icarus
    cmd = configuration[:vvp] || 'vvp'
    cmd += " -M#{compiled_dir} -morigen #{compiled_dir}/origen.vvp +socket+#{socket_id}"

  when :cadence
    input_file = "#{tmp_dir}/#{wave_file_basename}.tcl"
    if !File.exist?(input_file) || config_changed?
      Origen.app.runner.launch action:            :compile,
                               files:             "#{Origen.root!}/templates/probe.tcl.erb",
                               output:            tmp_dir,
                               check_for_changes: false,
                               quiet:             true,
                               options:           { dir: wave_dir, wave_file: wave_file_basename, force: config[:force], setup: config[:setup], depth: :all },
                               output_file_name:  "#{wave_file_basename}.tcl"
    end
    input_file_fast = "#{tmp_dir}/#{wave_file_basename}_fast.tcl"
    if !File.exist?(input_file_fast) || config_changed?
      fast_probe_depth = config[:fast_probe_depth] || 1
      Origen.app.runner.launch action:            :compile,
                               files:             "#{Origen.root!}/templates/probe.tcl.erb",
                               output:            tmp_dir,
                               check_for_changes: false,
                               quiet:             true,
                               options:           { dir: wave_dir, wave_file: wave_file_basename, force: config[:force], setup: config[:setup], depth: fast_probe_depth },
                               output_file_name:  "#{wave_file_basename}_fast.tcl"
    end
    save_config_signature
    wave_dir  # Ensure this exists since it won't be referenced above if the input file is already generated

    cmd = configuration[:irun] || 'irun'
    cmd += " -r origen -snapshot origen +socket+#{socket_id}"
    cmd += $use_fast_probe_depth ? " -input #{input_file_fast}" : " -input #{input_file}"
    cmd += " -nclibdirpath #{compiled_dir}"

  when :synopsys
    cmd = "#{compiled_dir}/simv +socket+#{socket_id} -vpd_file #{wave_file_basename}.vpd"

  when :generic
    # Generic tester requires that a generic_run_command option/block be provided.
    # This should either be a string, an array (which will be joined here), or a block that needs to return either
    # a string or array. In the event of a block, the block will be given the simulator.
    if generic_run_cmd
      cmd = generic_run_cmd
      if cmd.is_a?(Proc)
        cmd = cmd.call(self)
      end

      if cmd.is_a?(Array)
        # We'll join this together with the '; ' string. This means that each array element will be run
        # sequentially.
        cmd = cmd.join(' && ')
      elsif !cmd.is_a?(String)
        # If its Proc, it was already run, and if its a Array if would have gone into the other case.
        # So, this is either another proc, not an array and not a string, so not sure what to do with this.
        # Complain about the cmd.
        fail "OrigenSim :generic_run_cmd is of class #{generic_run_cmd.class}. It must be either an Array, String, or a Proc that returns an Array or String."
      end
    else
      fail 'OrigenSim Generic Toolchain/Vendor requires a :generic_run_cmd option/block to be provided. No options/block provided!'
    end

  else
    fail "Run cmd not defined yet for simulator #{config[:vendor]}"

  end

  # Allow the user to post-process the command. This should be a block which will be given two parameters:
  # 1. the command, and 2. the simulation object (self).
  # In the event of a generic tester, this *could* replace the launch command, but that's not the real intention,
  # since a simulator could be made that inherits from a generic simulator setup and still post process the command.
  cmd = post_process_run_cmd.call(cmd, self) if post_process_run_cmd
  fail "OrigenSim: :post_process_run_cmd returned object of class #{cmd.class}. Must return a String." unless cmd.is_a?(String)

  cmd
end

#run_dirObject



336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/origen_sim/simulator.rb', line 336

def run_dir
  case config[:vendor]
  when :icarus
    d = File.join(wave_dir, wave_file_basename)
    FileUtils.mkdir_p(d)
    d
  when :synopsys
    wave_dir
  else
    tmp_dir
  end
end

#save_config_signatureObject

Locally saves a signature for the current config, this will cause config_changed? to return false until its contents change



824
825
826
# File 'lib/origen_sim/simulator.rb', line 824

def save_config_signature
  Origen.app.session.origen_sim["#{id}_config"] = config
end

#set_period(period_in_ns) ⇒ Object



595
596
597
598
# File 'lib/origen_sim/simulator.rb', line 595

def set_period(period_in_ns)
  period_in_ps = period_in_ns * time_conversion_factor * (config[:time_factor] || 1)
  put("1^#{period_in_ps}")
end

#simulation_open?Boolean



349
350
351
# File 'lib/origen_sim/simulator.rb', line 349

def simulation_open?
  @simulation_open
end

#simulation_tester?Boolean



786
787
788
# File 'lib/origen_sim/simulator.rb', line 786

def simulation_tester?
  (tester && tester.is_a?(OrigenSim::Tester))
end

#socket_idObject



782
783
784
# File 'lib/origen_sim/simulator.rb', line 782

def socket_id
  simulation.socket_id
end

#startObject

Starts up the simulator process



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/origen_sim/simulator.rb', line 354

def start
  @simulation_open = true
  @simulation = Simulation.new(wave_file_basename, view_wave_command)
  simulations << @simulation

  fetch_simulation_objects

  cmd = run_cmd + ' & echo \$!'

  launch_simulator = %(
    require 'open3'
    require 'socket'
    require 'io/wait'
    require 'origen'

    pid = nil

    def kill_simulation(pid)
      begin
        # If the process already finished, then we will see an Errno exception.
        # It does not harm anything, but looks ugly, so catch it here and ignore.
        Process.kill('KILL', pid)
      rescue Errno::ESRCH => e
      end
      exit!
    end

    stdout_socket = UNIXSocket.new('#{simulation.socket_id(:stdout)}')
    stderr_socket = UNIXSocket.new('#{simulation.socket_id(:stderr)}')
    heartbeat = UNIXSocket.new('#{simulation.socket_id(:heartbeat)}')

    begin

      Dir.chdir '#{run_dir}' do
        Open3.popen3('#{cmd}') do |stdin, stdout, stderr, thread|
          pid = stdout.gets.strip.to_i
          heartbeat.puts(pid.to_s)

          # Listen for a heartbeat from the main Origen process every 5 seconds, kill the
          # simulator after two missed heartbeats
          Thread.new do
            missed_heartbeats = 0
            loop do
              sleep 5

              # If the socket read hangs, count that as a reason to shutdown
              socket_read = false
              Thread.new do
                sleep 1
                kill_simulation(pid) unless socket_read
              end

              if heartbeat.ready?
                while heartbeat.ready? do
                  heartbeat.gets
                end
                missed_heartbeats = 0
              else
                missed_heartbeats += 1
              end
              socket_read = true
              if missed_heartbeats > 1
                kill_simulation(pid)
              end
            end
          end

          threads = []
          threads << Thread.new do
            until (line = stdout.gets).nil?
              stdout_socket.puts line
            end
          end
          threads << Thread.new do
            until (line = stderr.gets).nil?
              stderr_socket.puts line
            end
          end
          threads.each(&:join)
        end
      end

    ensure
      # Make sure this process never finishes and leaves the simulator running
      kill_simulation(pid) if pid
    end
  )

  simulator_parent_process = spawn("ruby -e \"#{launch_simulator}\"")
  Process.detach(simulator_parent_process)

  timeout_connection(config[:startup_timeout] || 60) do
    simulation.open # This will block until the simulation process responds

    @connection_established = true # Cancels timeout_connection
    if @connection_timed_out
      simulation.failed_to_start = true
      exit  # Assume it is not worth trying another pattern in this case, some kind of environment/config issue
    end
  end
  data = get
  unless data.strip == 'READY!'
    simulation.failed_to_start = true
    exit  # Assume it is not worth trying another pattern in this case, some kind of environment/config issue
  end
  # Tick the simulation on, this seems to be required since any VPI puts operations before
  # the simulation has started are not applied.
  # Note that this is not setting a tester timeset, so the application will still have to
  # do that before generating any vectors.
  set_period(100)
  cycle(1)
  Origen.listeners_for(:simulation_startup).each(&:simulation_startup)
end

#stopObject

Stop the simulator



718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/origen_sim/simulator.rb', line 718

def stop
  @simulation_open = false
  simulation.read_sim_output
  simulation.error_count = error_count
  Origen.listeners_for(:simulation_shutdown).each(&:simulation_shutdown)
  ended = Time.now
  end_simulation
  # Give the simulator time to shut down
  sleep 0.1 while simulation.running?
  simulation.close
  simulation.completed_cleanly = true
  simulation.log_results unless Origen.current_command == 'interactive'
end

#syncObject



805
806
807
808
809
810
811
# File 'lib/origen_sim/simulator.rb', line 805

def sync
  put('f')
  @sync_active = true
  yield
  put('g')
  @sync_active = false
end

#sync_active?Boolean



813
814
815
# File 'lib/origen_sim/simulator.rb', line 813

def sync_active?
  @sync_active
end

#sync_upObject

Blocks the Origen process until the simulator indicates that it has processed all operations up to this point



607
608
609
610
611
612
613
614
# File 'lib/origen_sim/simulator.rb', line 607

def sync_up
  put('7^')
  data = get
  unless data.strip == 'OK!'
    fail 'Origen and the simulator are out of sync!'
  end
  simulation.read_sim_output
end

#testbench_topObject



35
36
37
# File 'lib/origen_sim/simulator.rb', line 35

def testbench_top
  config[:testbench_top] || 'origen'
end

#timeout_connection(wait_in_s) ⇒ Object



790
791
792
793
794
795
796
797
798
799
800
801
802
803
# File 'lib/origen_sim/simulator.rb', line 790

def timeout_connection(wait_in_s)
  @connection_timed_out = false
  @connection_established = false
  t = Thread.new do
    sleep wait_in_s
    # If the Verilog process has not established a connection yet, then make one to
    # release our process and then exit
    unless @connection_established
      @connection_timed_out = true
      UNIXSocket.new(socket_id).puts("Time out\n")
    end
  end
  yield
end

#tmp_dirObject



128
129
130
131
132
133
134
# File 'lib/origen_sim/simulator.rb', line 128

def tmp_dir
  @tmp_dir ||= begin
    d = "#{Origen.root}/tmp/origen_sim/#{id}/#{config[:vendor]}"
    FileUtils.mkdir_p(d)
    d
  end
end

#view_wave_commandObject



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
# File 'lib/origen_sim/simulator.rb', line 287

def view_wave_command
  cmd = nil
  case config[:vendor]
  when :icarus
    edir = Pathname.new(wave_config_dir).relative_path_from(Pathname.pwd)
    cmd = "cd #{edir} && "
    cmd += configuration[:gtkwave] || 'gtkwave'
    dir = Pathname.new(wave_dir).relative_path_from(edir.expand_path)
    cmd += " #{dir}/#{wave_file_basename}/dump.vcd "
    f = Pathname.new(wave_config_file).relative_path_from(edir.expand_path)
    cmd += " --save #{f} &"

  when :cadence
    edir = Pathname.new(wave_config_dir).relative_path_from(Pathname.pwd)
    cmd = "cd #{edir} && "
    cmd += configuration[:simvision] || 'simvision'
    dir = Pathname.new(wave_dir).relative_path_from(edir.expand_path)
    cmd += " #{dir}/#{wave_file_basename}/#{wave_file_basename}.dsn #{dir}/#{wave_file_basename}/#{wave_file_basename}.trn"
    f = Pathname.new(wave_config_file).relative_path_from(edir.expand_path)
    cmd += " -input #{f} &"

  when :synopsys
    edir = Pathname.new(wave_config_dir).relative_path_from(Pathname.pwd)
    cmd = "cd #{edir} && "
    cmd += configuration[:dve] || 'dve'
    dir = Pathname.new(wave_dir).relative_path_from(edir.expand_path)
    cmd += " -vpd #{dir}/#{wave_file_basename}.vpd"
    f = Pathname.new(wave_config_file).relative_path_from(edir.expand_path)
    cmd += " -session #{f}"
    cmd += ' &'

  when :generic
    # Since this could be anything, the simulator will need to set this up. But, once it is, we can print it here.
    if config[:view_waveform_cmd]
      cmd = config[:view_waveform_cmd]
    else
      Origen.log.warn 'OrigenSim cannot provide a view-waveform command for a :generic vendor.'
      Origen.log.warn 'Please supply a view-waveform command though the :view_waveform_cmd option during the OrigenSim::Generic instantiation.'
    end

  else
    # Print a warning stating an unknown vendor was reached here.
    # This shouldn't happen, but just in case.
    Origen.log.warn "OrigenSim does not know the command to view waveforms for vendor :#{config[:vendor]}!"

  end
  cmd
end

#wave_config_dirObject



154
155
156
157
158
159
160
# File 'lib/origen_sim/simulator.rb', line 154

def wave_config_dir
  @wave_config_dir ||= begin
    d = "#{Origen.root}/config/waves/#{id}"
    FileUtils.mkdir_p(d)
    d
  end
end

#wave_config_extObject



186
187
188
189
190
191
192
193
194
195
# File 'lib/origen_sim/simulator.rb', line 186

def wave_config_ext
  case config[:vendor]
  when :icarus
    'gtkw'
  when :cadence
    'svcf'
  when :synopsys
    'tcl'
  end
end

#wave_config_fileObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/origen_sim/simulator.rb', line 162

def wave_config_file
  @wave_config_file ||= begin
    f = "#{wave_config_dir}/#{User.current.id}.#{wave_config_ext}"
    unless File.exist?(f)
      # Take a default wave if one has been set up
      d = "#{wave_config_dir}/default.#{wave_config_ext}"
      if File.exist?(d)
        FileUtils.cp(d, f)
      else
        # Otherwise seed it with the latest existing setup by someone else
        d = Dir.glob("#{wave_config_dir}/*.#{wave_config_ext}").max { |a, b| File.ctime(a) <=> File.ctime(b) }
        if d
          FileUtils.cp(d, f)
        else
          # We tried our best, start from scratch
          d = "#{Origen.root!}/templates/empty.#{wave_config_ext}"
          FileUtils.cp(d, f) if File.exist?(d)
        end
      end
    end
    f
  end
end

#wave_dir(subdir = nil) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/origen_sim/simulator.rb', line 146

def wave_dir(subdir = nil)
  @wave_dir ||= begin
    d = "#{Origen.root}/waves/#{id}"
    FileUtils.mkdir_p(d)
    d
  end
end

#wave_file_basenameObject



275
276
277
278
279
280
281
282
283
284
285
# File 'lib/origen_sim/simulator.rb', line 275

def wave_file_basename
  if OrigenSim.flow
    OrigenSim.flow.to_s
  else
    if Origen.app.current_job
      @last_wafe_file_basename = Pathname.new(Origen.app.current_job.output_file).basename('.*').to_s
    else
      @last_wafe_file_basename
    end
  end
end

#wave_to_str(wave) ⇒ Object



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/origen_sim/simulator.rb', line 566

def wave_to_str(wave)
  wave.evaluated_events.map do |time, data|
    time = time * time_conversion_factor * (config[:time_factor] || 1)
    if data == :x
      data = 'X'
    elsif data == :data
      data = wave.drive? ? 'D' : 'C'
    end
    if data == 'C'
      "#{time}_#{data}_#{time + (time_conversion_factor * (config[:time_factor] || 1))}_X"
    else
      "#{time}_#{data}"
    end
  end.join('_')
end

#write_comment(comment) ⇒ Object



526
527
528
529
530
531
# File 'lib/origen_sim/simulator.rb', line 526

def write_comment(comment)
  # Not sure what the limiting factor here is, the comment memory in the test bench should
  # be able to handle 1024 / 8 length strings, but any bigger than this hangs the simulation
  comment = comment[0..96]
  put("c^#{comment}")
end