This file is indexed.

/usr/share/tkgate-1.8.7/scripts/simulator.tcl is in tkgate-data 1.8.7-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 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
 93
 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
120
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#   Copyright (C) 1987-2007 by Jeffery P. Hansen
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Last edit by hansen on Fri Dec  5 14:24:53 2003
#

set simId 0
set simOn 0

set tkg_simDisplayedVal 0

proc tkg_disableEditModes {} {
  catch {
     toolbar:hideEdit

    .mbar.edit configure -state disabled
    .mbar.tool configure -state disabled
    .mbar.make configure -state disabled
    .mbar.gate configure -state disabled
  }
}

proc tkg_enableEditModes {} {
  catch {
    toolbar:showEdit
    .mbar.edit configure -state normal
    .mbar.tool configure -state normal
    .mbar.make configure -state normal
    .mbar.gate configure -state normal
  }
}

proc tkg_setupSimMenus {} {
  catch {
    .mbar.sim.menu entryconfigure 1 -label [m simend] -underline [ul 0] -command {gat_setMajorMode edit} -accelerator "Ctl-S e"

    .mbar.sim.menu entryconfigure 3 -state normal
    .mbar.sim.menu entryconfigure 4 -state normal
    .mbar.sim.menu entryconfigure 5 -state normal
    .mbar.sim.menu entryconfigure 6 -state normal
    .mbar.sim.menu entryconfigure 7 -state normal
    .mbar.sim.menu entryconfigure 8 -state normal
    .mbar.sim.menu entryconfigure 10 -state normal
    .mbar.sim.menu entryconfigure 11 -state normal
    .mbar.sim.menu entryconfigure 13 -state normal

    toolbar:showSim
  }
}

proc tkg_teardownSimMenus {} {
  catch {
    .mbar.sim.menu entryconfigure 1 -label [m simbegin] -underline [ul 0] -command { gat_setMajorMode simulate } -accelerator "Ctl-S b"

    .mbar.sim.menu entryconfigure 3 -state disabled
    .mbar.sim.menu entryconfigure 4 -state disabled
    .mbar.sim.menu entryconfigure 5 -state disabled
    .mbar.sim.menu entryconfigure 6 -state disabled
    .mbar.sim.menu entryconfigure 7 -state disabled
    .mbar.sim.menu entryconfigure 8 -state disabled
    .mbar.sim.menu entryconfigure 10 -state disabled
    .mbar.sim.menu entryconfigure 11 -state disabled
    .mbar.sim.menu entryconfigure 13 -state disabled

     toolbar:hideSim
  }
}

#############################################################################
#
# Verifiy that simulator is running
#
proc tkg_simCheck {} {
  global simOn

  if { $simOn == 0 } {
    errmsg "Command not valid when not in simulation mode."
    return 0
  }
  return 1
}

proc tkg_readSimInitScript {} {
  global sopts_simInitScript
  if { $sopts_simInitScript != "" } {
    gat_simScript $sopts_simInitScript
  }
}

########################################
#
# start up the simulator
#
proc tkg_startSim {fname} {
  global simId simOn mode simExec tkg_simDelayFile tkg_simDefaultDelayFile tkg_simCustomDelay tkg_currentFile tkg_simDebugInterface
  set basename ""

  if { $simOn } { return }

  set p [string last "/" $tkg_currentFile]
  if { $p > 0 } {
    set basename [string range $tkg_currentFile 0 $p]
    set basename "-B $basename"
  }

  if { $tkg_simCustomDelay } {
    set delayFile $tkg_simDelayFile
  } {
    set delayFile $tkg_simDefaultDelayFile
  }

  set delayFile $tkg_simDefaultDelayFile
  foreach f $tkg_simDelayFile {
    set delayFile "${delayFile}:$f"
  }

  if { $tkg_simDebugInterface } {
    puts "Exec: $simExec -D $delayFile $basename $fname"
  }

  if { [catch { set simId [open "|$simExec -D $delayFile $basename $fname" r+ ] } ] } {
    errmsg "Failed to start simultor '${simExec}'"
    return
  }

  fileevent $simId readable { tkg_readSim; update idletasks }
  set simOn 1
  set mode 1

  tkg_setupSimMenus

  tkg_resetLogo

  return $simId
}

########################################
#
# read a command from simulator
#
proc tkg_readSim {} {
  global simId simOn

  if { !$simOn } { return }

  if { [catch { set command [gets $simId] } ] } {
    gat_setMajorMode edit
    errmsg "Simulator has died (read error)."
    return
  }
  if { $command == "" && [eof $simId] } {
    gat_setMajorMode edit
    errmsg "Simulator has died (eof in read)."
    return
  }

  gat_scopeCommand $command
  return 0
}

proc tkg_simWrite {msg} {
  global simId tkg_simDebugInterface sopts_simLogFile

  if { $sopts_simLogFile != "" } {
    set log [open $sopts_simLogFile "a+"]
    puts $log $msg
    close $log
  }

  if { $tkg_simDebugInterface } {
    puts "->sim: $msg"
    flush stdout
  }

  if { [catch { puts $simId $msg }] } {
    gat_setMajorMode edit
    errmsg "Simulator has died (in write)."
    return
  }
  tkg_simFlush
}

proc tkg_simFlush {} {
  global simId
  if { [catch { flush $simId } ] } {
    gat_setMajorMode edit
    errmsg "Simulator has died (in flush)."
    return
  }
}

proc tkg_endSim {} {
  global simId simOn tkg_simDebugInterface 

  if { $tkg_simDebugInterface } {
    puts "tkg_endSim"
  }

  catch { fileevent $simId readable }
  catch { close $simId }
  set simOn 0
  tkg_teardownSimMenus
  gat_setMajorMode edit
  tkg_editLogo
  tkg_rmttys
}

#############################################################################
#
# breakpoint handling
#

proc tkg_BPdialog {str} {
  set w .ebp
  global ebp_str

  if { [catch { toplevel $w }] } {
    return
  }

  wm resizable $w 0 0
  wm title $w "TkGate: Edit Breakpoint"
  wm geometry $w [offsetgeometry . 50 50]
  wm transient $w .

  set ebp_str $str

  frame $w.f -relief raised -bd 2
  label $w.f.l -text "[m sim.breakpt]: "
  entry $w.f.e -width 40 -textvariable ebp_str
  pack $w.f.l $w.f.e -side left -padx 5 -pady 5

  okcancel $w.b { destroy .ebp } { global ebp_str; set ebp_str ""; destroy .ebp }

  bind $w.f.e <KeyPress-Return>  { destroy .ebp }


  pack $w.f -fill both -ipadx 10 -ipady 20
  pack $w.b -fill both

  helpon $w.f.l [m ho.sim.break]

  grab set $w
  tkwait window $w
  grab release $w

  return $ebp_str
}

proc tkg_editBP {} {
  set s [.bpedit.f.g.l curselection]  
  if { $s != "" } {
    set old_bp [.bpedit.f.g.l get $s]  
    set old_bp [string trim $old_bp "=> "]
    set new_bp [tkg_BPdialog $old_bp]
    if { $new_bp != "" } {
      if { [gat_breakpoint -replace $s $new_bp] >= 0 } {
        .bpedit.f.g.l delete $s
        .bpedit.f.g.l insert $s $new_bp
      }
    }
  } {
    errmsg "No breakpoint selected"
  }

}

#
# Add a new breakpoint.  If no arguments are specified, a dialog box is used
# to get the breakpoint text.  If an argument is specified, the breakpoint
# is taken from the command line.  The id number of the breakpoint is returned
# on success, or a -1 on failure.
#
proc tkg_addBP args {

  if { $args == "" } {
    set bp [tkg_BPdialog ""]
  } {
    set bp $args
  }

  set id -1
  catch {
    if {$bp != ""} {
      set id [gat_breakpoint -insert -1 $bp]
      if { $id >= 0 } {
        .bpedit.f.g.l insert end $bp
      }
    }
  }
  return $id
}

#
# Delete an existing breakpoint.  If no arguments are specified, the breakpoint
# is deleted based on the selected element in the breakpoint listbox, otherwise
# the specified breakpoint is deleted.
#
proc tkg_deleteBP args {
  if { $args == "" } {
    set s [.bpedit.f.g.l curselection]  
  } {
    set s $args
  }

  catch {
    if { $s != "" } {
      if { [gat_breakpoint -delete $s] >= 0 } {
        .bpedit.f.g.l delete $s
      }
    } {
      errmsg "No breakpoint selected"
    }
  }
}

proc tkg_editBreakpoints args {
  set w ".bpedit"

  if { ![tkg_simCheck] } return

  set parent "."
  parseargs $args {-parent}

#
# Do nothing if window is active.
#
  if { [catch { toplevel $w }] } {
    return
  }

  wm resizable $w 0 0
  wm title $w "TkGate: Breakpoints"
  wm geometry $w [offsetgeometry . 50 50]
  wm transient $w $parent

  frame $w.f -relief raised -bd 2
  frame $w.f.g
  frame $w.f.ops

  frame $w.cf -relief raised -bd 2

  listbox $w.f.g.l -width 40 -yscrollcommand "$w.f.g.vsb set" -xscrollcommand "$w.f.g.hsb set" -takefocus 0 -relief sunken -bd 2
  scrollbar $w.f.g.vsb -command "$w.f.g.l yview" -takefocus 0
  scrollbar $w.f.g.hsb -command "$w.f.g.l yview" -orient horizontal -takefocus 0
  grid $w.f.g.l -row 0 -column 0 -padx 3 -pady 3
  grid $w.f.g.vsb -row 0 -column 1 -padx 3 -pady 3 -sticky ns
  grid $w.f.g.hsb -row 1 -column 0 -padx 3 -pady 3 -sticky ew
  pack $w.f.g

  button $w.f.ops.add -text [m b.add] -command tkg_addBP
  button $w.f.ops.edit -text [m b.edit] -command tkg_editBP
  button $w.f.ops.del -text [m b.deleteb] -command tkg_deleteBP
  pack $w.f.ops.add $w.f.ops.edit $w.f.ops.del -side left -padx 5 -pady 5
  pack $w.f.ops

  button $w.cf.ok -text [m b.dismiss] -command { destroy .bpedit }
  pack $w.cf.ok -padx 5 -pady 5

  pack $w.f -ipadx 5 -ipady 5 -fill both
  pack $w.cf -ipadx 5 -ipady 5 -fill both

  bind $w <KeyPress-Delete> tkg_deleteBP 
  bind $w.f.g.l <Double-ButtonRelease-1> tkg_editBP 

  gat_breakpoint -load $w.f.g.l

#  grab set $w
#  tkwait window $w
#  grab release $w
}

#
# Delete all old breakpoint displays
#
proc tkg_clearBreaks {} {
  catch {
    set N [.bpedit.f.g.l size]
    for {set i 0} {$i < $N} {incr i} {
      set e [string trim [.bpedit.f.g.l get $i] "=> "]
      .bpedit.f.g.l delete $i
      .bpedit.f.g.l insert $i $e
    }
  }
}

proc tkg_seeBreaks args {
  if { [llength $args] <= 0 } {
    tkg_clearBreaks
    return
  }

  tkg_editBreakpoints
  tkg_clearBreaks

  catch {
    while { [llength $args] > 0 } {
      set i [lindex $args 0]
      set args [lrange $args 1 end]
      set e [.bpedit.f.g.l get $i]
      set e "==> $e"
      .bpedit.f.g.l delete $i
      .bpedit.f.g.l insert $i $e
    }
  }
}

#############################################################################
#
# Simulation script handling
#

proc tkg_doSimScript args {
  global simScript_filter simScript_filetypes

  if { ![tkg_simCheck] } return

  set parent "."
  parseargs $args {-parent}

  set fileName [tk_getOpenFile -defaultextension $simScript_filter -parent $parent -filetypes $simScript_filetypes ]
  if { $fileName != "" } {
    gat_simScript $fileName
  }
}

#############################################################################
#
# Load/Dump memory
#

proc tkg_simLoadMem args {
  global mem_filetypes mem_filter

  if { ![tkg_simCheck] } return

  set parent "."
  parseargs $args {-parent}

  set g [gat_simSelected ram rom]

  set load [tk_getOpenFile -defaultextension $mem_filter -parent $parent -filetypes $mem_filetypes ]
  if { $load != "" } {
#    tkg_simGateCmd $g load $load
    tkg_simWrite "memload $load $g"
  }
}

proc tkg_simDumpMem args {
  global mem_filetypes mem_filter

  if { ![tkg_simCheck] } return

  set parent "."
  parseargs $args {-parent}

  set g [gat_simSelected ram rom]
  if { $g == "" } {
    errmsg "RAM or ROM circuit element not selected."
    return
  }

  set dump [tk_getSaveFile -defaultextension $mem_filter -parent $parent -filetypes $mem_filetypes ]
  if { $dump != "" } {
    tkg_simGateCmd $g dump $dump
  }
}

#############################################################################
#
# basic commands
#

proc tkg_simNetSet {net val} {
  if { ![tkg_simCheck] } return

  tkg_simWrite "set $net $val"
}

proc tkg_simRun {} {
  global simOn

  if { $simOn == 0  } {
    gat_setMajorMode simulate
  } {
    gat_breakpoint -clear
    tkg_clearBreaks
    tkg_simWrite "go 1"
    tkg_runLogo
  }
}

proc tkg_simStop {} {
  global simOn

  if { $simOn == 0  } {
  } {
    tkg_simWrite "go 0"
  }

  tkg_pauseLogo
}

#
# args: +/- # after [clock]
#
#
proc tkg_simCycle args {
  global tkg_simClockOverStep tkg_simActClock tkg_simUseActClock tkg_simClockStepSize
  global simOn

  if { $simOn == 0  } return

  gat_breakpoint -clear
  tkg_clearBreaks

  if { $args == "" } {
    if { $tkg_simUseActClock != 0 && $tkg_simActClock != "" } {
      tkg_simWrite "clock + $tkg_simClockStepSize $tkg_simClockOverStep $tkg_simActClock"
    } else {
      tkg_simWrite "clock + $tkg_simClockStepSize $tkg_simClockOverStep"
    }
  } {
    tkg_simWrite "clock $args"
  }
}

proc tkg_simStep args {
  global tkg_simStepSize
  global simOn

  if { $simOn == 0  } return

  gat_breakpoint -clear
  tkg_clearBreaks

  if { $args == "" } {
    tkg_simWrite "step $tkg_simStepSize"
  } {
    tkg_simWrite "step $args"
  }
}

proc tkg_simGateCmd args {
   if { ![tkg_simCheck] } return
   tkg_simWrite "command $args"
}

#
# Create a window for entering a dip value.
#
proc tkg_dipEntry {g v x y} {
  global dip_set

  if { [catch { set e $dip_set($g) } ] } { set e "" }
  if { $e != "" } { 
    raise $e
    return
   }

  for {set i 0} { [catch { set w [toplevel .dipe$i] }] } { incr i } { }

  set dip_set($g) $w

  wm resizable $w 0 0
  wm title $w "TkGate: Dip Value"
  wm geometry $w [offsetgeometry . $x $y]
  wm transient $w .

  bind .scope <Destroy> "+ set dip_set($g) \"\"; destroy $w"

  label $w.t -text "$g:"
  entry $w.entr -width 10
  $w.entr insert 0 $v

  button $w.apply -text [m b.apply] -command " set q \[ gat_setDip $g \[ $w.entr get \] \]; $w.entr delete 0 end; $w.entr insert 0 \$q"
  button $w.close -text [m b.close] -command " set dip_set($g) \"\"; destroy $w"

  bindtags $w.entr [concat [list HexEntry] [bindtags $w.entr]]

  bind $w.entr <Return> "$w.apply configure -state active; after 500 \"$w.apply configure -state normal\"; $w.apply invoke"

  pack $w.t -side top -fill both
  pack $w.close -side bottom -fill both -pady 3
  pack $w.entr $w.apply -side left -pady 5 -padx 5 
}

proc tkg_simShowValue {n} {
  global tkg_simDisplayedVal tkg_showValueEv tkg_valuePopUpDelay

  set tkg_simDisplayedVal "-"

  tkg_simWrite "show $n"

  scan [winfo pointerxy .] "%d %d" x y

  set tkg_showValueEv  [after $tkg_valuePopUpDelay "tkg_postShowValue $x $y"]
}

proc tkg_postShowValue {x y} {
  global tkg_simDisplayedVal

  catch { destroy .showv }
  toplevel .showv -bg bisque

  wm geometry .showv +[expr $x + 5]+[expr $y - 30]
  wm transient .showv .
  wm overrideredirect .showv 1

  label .showv.l -textvariable tkg_simDisplayedVal -bg bisque
  pack .showv.l -padx 4 -pady 4
}

proc tkg_simHideValue {} {
  global tkg_showValueEv

  catch { after cancel $tkg_showValueEv }
  catch { destroy .showv }
}