FlipFlop.tcl



# Simula el Jk tipo D biestable sincronizado.
# Genera el fondo.
frame .f \
   -background green \
   -borderwidth 3

# _______ __ ________
# MUESTRA EL CIRCUITO
# ------- -- --------
canvas .f.cir -width 600 -height 300

# _____
# AND 1
.f.cir create arc  50  40 150 80 \
                  -start 270 -extent 180 -fill red
# _____
# AND 2
.f.cir create arc 50 100 150 140 \
                  -start 270 -extent 180 -fill red

# ______________________
# Conexiones iniciales 1
.f.cir create line 10  50 100  50 -fill red
.f.cir create line 60  70 100  70 -fill red
.f.cir create line 60 110 100 110 -fill red
.f.cir create line 30 130  40 130 -fill red
.f.cir create line 65 130 100 130 -fill red

# ________
.f.cir create line 40 115  40 145 -fill red
.f.cir create line 40 115  60 130 -fill red
.f.cir create line 40 145  60 130 -fill red
.f.cir create oval 58 127  65 134 -outline red

# ______________________
# Conexiones iniciales 2
.f.cir create line 60  70  60 110 -fill red
.f.cir create line 10  90  60  90 -fill red
.f.cir create line 30  50  30 130 -fill red
.f.cir create oval 58  87  65  94 -fill red

# _____
# NOR 1
.f.cir create arc 275  50 300 90 -outline red -style arc \
                   -start 270 -extent 180
.f.cir create arc  240  50 340 90 -outline red -style arc \
                   -start 270 -extent 180
.f.cir create oval 340 67 347 74 -outline red

# _____
# NOR 2
.f.cir create arc 275  110 300 150 -outline red -style arc \
                   -start 270 -extent 180
.f.cir create arc  240  110 340 150 -outline red -style arc \
                   -start 270 -extent 180
.f.cir create oval 340 127 347 134 -outline red

# ______________________
# Conexiones intermedias
.f.cir create line 150  60 300  60 -fill red
.f.cir create line 150 120 300 120 -fill red
# Conexiones intermedias 1
.f.cir create line 250  80 300  80 -fill red
.f.cir create line 250  80 250  95 -fill red
.f.cir create line 250  95 355  95 -fill red
.f.cir create line 355  95 355 130 -fill red
.f.cir create oval 352 126 359 133 -fill red
# Conexiones intermedias 2
.f.cir create line 250 140 300 140 -fill red
.f.cir create line 250 140 250 155 -fill red
.f.cir create line 250 155 370 155 -fill red
.f.cir create line 370 155 370  70 -fill red
.f.cir create oval 367  66 374  73 -fill red

# ____________________
# Conexiones de salida
.f.cir create line 348  70 400  70 -fill red
.f.cir create line 348 130 400 130 -fill red
.f.cir create text 450  55 -text "_"
.f.cir create text 450  70 -text "Q"
.f.cir create text 450 130 -text "Q"
.f.cir create text 280 270 -text "Biestable D sincronizado"

# __________________
# variables globales
# sal1            Salida negada
# sal2            Salida normal

set ent1 0
set ent2 0
set sal1 1
set sal2 0
set and1 0
set and2 0
set not1 1

button .f.in1 \
   -width 0 \
   -height 0 \
   -textvariable ent1 \
   -borderwidth 0 \
   -relief sunken \
   -bg green \
   -command { in .f.in1 }
button .f.in2 \
   -width 0 \
   -height 0 \
   -textvariable ent2 \
   -borderwidth 0 \
   -relief sunken \
   -bg green \
   -command { in .f.in2 }

button .f.sal1 \
   -width 0 \
   -height 0 \
   -textvariable sal1 \
   -borderwidth 0 \
   -relief sunken \
   -bg green
button .f.sal2 \
   -width 0 \
   -height 0 \
   -textvariable sal2 \
   -borderwidth 0 \
   -relief sunken \
   -bg green

button .f.and1 \
   -width 0 \
   -height 0 \
   -textvariable and1 \
   -borderwidth 0 \
   -relief sunken \
   -bg yellow
button .f.and2 \
   -width 0 \
   -height 0 \
   -textvariable and2 \
   -borderwidth 0 \
   -relief sunken \
   -bg yellow
button .f.not1 \
   -width 0 \
   -height 0 \
   -textvariable not1 \
   -borderwidth 0 \
   -relief sunken \
   -bg yellow

# _______
# Patitas
  set x(1)  30
  set y(1) 127
  set x(2)  90
  set y(2)  47
  set x(3)  90
  set y(3)  67
  set x(4)  90
  set y(4) 107
  set x(5)  90
  set y(5) 127
  set x(6) 286
  set y(6)  57
  set x(7) 286
  set y(7)  77
  set x(8) 286
  set y(8) 117
  set x(9) 286
  set y(9) 137
  for { set k 1 } { $k < 10 } { incr k } {
     button .f.p$k \
        -width 0 \
        -height 0 \
        -borderwidth 0 \
        -relief sunken \
        -bg red
     place configure .f.p$k  -x $x($k) -y $y($k)
     place configure .f.p$k  -width 10 -height 7
  }

# Sale del programa.
button .f.fin \
   -width 0 \
   -height 0 \
   -text "Salir" \
   -borderwidth 2 \
   -relief sunken \
   -bg yellow \
   -command { exit }

# Coloca los widget.
place configure .f.in1  -x   0 -y  40
place configure .f.in2  -x   0 -y  80
place configure .f.fin  -x 560 -y 270
place configure .f.sal1 -x 398 -y  60
place configure .f.sal2 -x 398 -y 120
place configure .f.and1 -x 150 -y  50
place configure .f.and2 -x 150 -y 110
place configure .f.not1 -x  65 -y 120
place configure .f.in1  -width 20 -height 20
place configure .f.in2  -width 20 -height 20
place configure .f.fin  -width 35 -height 25
place configure .f.and1 -width 20 -height 20
place configure .f.and2 -width 20 -height 20
place configure .f.sal1 -width 20 -height 20
place configure .f.sal2 -width 20 -height 20
place configure .f.not1 -width 20 -height 20

pack .f .f.cir

proc in { nombre } {
   global ent1 ent2 sal1 sal2 and1 and2 not1
   switch $nombre {
      .f.in1 { if { $ent1 == 0 } then {
                  set ent1 1
               } else {
                  set ent1 0
               }
             }
      .f.in2 { if { $ent2 == 0 } then {
                  set ent2 1
               } else {
                  set ent2 0
               }
             }
   }
   if { $ent1 == 0 && $ent2 == 0 } then {
      set sal1 $sal1
      set sal2 $sal2
      set and1 0
      set and2 0
      set not1 1
   }
   if { $ent1 == 0 && $ent2 == 1 } then {
      set sal1 1
      set sal2 0
      set and1 0
      set and2 1
      set not1 1
   }
   if { $ent1 == 1 && $ent2 == 0 } then {
      set sal1 $sal1
      set sal2 $sal2
      set and1 0
      set and2 0
      set not1 0
   }
   if { $ent1 == 1 && $ent2 == 1 } then {
      set sal1 0
      set sal2 1
      set and1 1
      set and2 0
      set not1 0
   }
}