Maquina.tcl



# Lenguaje de 8 bits
#===================================inicio

#===================================variables
set velocidad 1000
set instr 1
set pila 1
set x 0
set y 0
set z 0
set nz 1
#===================================global
# .f   genera el fondo
# .f.p muestra la pila
frame .f   -background green
frame .f.a -background green
frame .f.p -background green
frame .f.d -background green

label .f.cabeza -text "J. Rafel R. Ochoa" -width 14 -anchor w
pack .f.cabeza -side bottom

#===================================Nombre del archivo
frame .f.a.arch \
   -background green
label .f.a.arch.nombre \
   -text "Nombre de archivo: " \
   -width 18 -height 1 \
   -bg blue
entry .f.a.arch.arch -width 20 -textvariable archivo
pack .f.a.arch.nombre .f.a.arch.arch -side left

#===================================Video
frame .f.a.video \
   -background green
label .f.a.video.cabeza \
   -text "Rx \n\n Ry \n\n Rz \n\n\n NZ \n\n PC" \
   -width 5 -height 10 \
   -bg blue
button .f.a.video.fast \
   -width 2 -height 1 \
   -text "+" \
   -borderwidth 2 \
   -relief sunken \
   -bg green \
   -command { veloc mas }
button .f.a.video.slow \
   -width 2 -height 1 \
   -text "-" \
   -borderwidth 2 \
   -relief sunken \
   -bg green \
   -command { veloc menos }
pack .f.a.video.fast .f.a.video.slow -sid left
pack .f.a.video.cabeza -side left

button .f.a.video.x \
   -textvariable x \
   -width 10 -height 1 \
   -bg green
button .f.a.video.y \
   -textvariable y \
   -width 10 -height 1 \
   -bg green
button .f.a.video.z \
   -width 10 -height 1 \
   -textvariable z \
   -bg green
button .f.a.video.nz \
   -width 10 -height 1 \
   -textvariable nz \
   -bg green
button .f.a.video.pc \
   -width 10 -height 1 \
   -textvariable instr \
   -borderwidth 2 \
   -relief sunken \
   -bg green \
   -command { pc }
pack .f.a.video.x .f.a.video.y .f.a.video.z .f.a.video.nz .f.a.video.pc -side top

#===================================Control
frame .f.a.control
button .f.a.control.sig \
   -width 7 -height 1 \
   -text "Ejecuta" \
   -borderwidth 2 \
   -relief sunken \
   -bg red \
   -command { sig }
button .f.a.control.auto \
   -width 7 -height 1 \
   -text "Automatico" \
   -borderwidth 2 \
   -relief sunken \
   -bg red \
   -command { automatico }
button .f.a.control.fin \
   -width 7 -height 1 \
   -text "Salir" \
   -borderwidth 2 \
   -relief sunken \
   -bg red \
   -command { destroy .f }
button .f.a.control.cargar \
   -width 30 -height 1 \
   -text "CARGAR ARCHIVO" \
   -bg red \
   -borderwidth 0 \
   -command { arch }
pack .f.a.control.auto .f.a.control.sig .f.a.control.cargar .f.a.control.fin -side left

#===================================pila
frame .f.p.pila1
frame .f.p.pila2
label .f.p.cabeza -text "PILA" -width 5 -anchor w
pack .f.p.cabeza -side top
for { set k  1 } { $k < 11 } { incr k } {
   button .f.p.pila1.p$k \
      -width 5 -height 1 \
      -textvariable p$k \
      -bg green
   pack .f.p.pila1.p$k -side top
}
for { set k  11 } { $k < 21 } { incr k } {
   button .f.p.pila2.p$k \
      -width 5 -height 1 \
      -textvariable p$k \
      -bg green
   pack .f.p.pila2.p$k -side top
}

#===================================datos
frame .f.d.datos1
frame .f.d.datos2
label .f.d.cabeza -text "DATOS" \
    -width 5 -anchor w \
    -bg blue
pack .f.d.cabeza -side top
for { set k  1 } { $k < 11 } { incr k } {
   button .f.d.datos1.d$k \
      -width 5 -height 1 \
      -textvariable d$k \
      -bg green
   pack .f.d.datos1.d$k -side top
}
for { set k  11 } { $k < 21 } { incr k } {
   button .f.d.datos2.d$k \
      -width 5 -height 1 \
      -textvariable d$k \
      -bg green
   pack .f.d.datos2.d$k -side top
}

#===================================union
pack .f.a.arch .f.a.video .f.a.control -side top
pack .f.p.pila2 .f.p.pila1 -side right
pack .f.d.datos1 .f.d.datos2 -side left

pack .f.d .f.a .f.p -side left
pack .f -side left

#===================================procs
proc arch {} {
   global d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11
   global d12 d13 d14 d15 d16 d17 d18 d19 d20
   global p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11
   global p12 p13 p14 p15 p16 p17 p18 p19 p20
   global x y z nz pila instr archivo

   for { set k 1 } { $k < 21 } { incr k } {
      set d$k " "
   }
   for { set k 1 } { $k < 21 } { incr k } {
      set p$k " "
   }
   set nombre [open $archivo "r"]
   set k 1
   while {![eof $nombre]} {
      gets $nombre d$k
      incr k
   }
   close $nombre
   set instr 1
   set pila 1
   set nz 1
   set x 0
   set y 0
   set z 0
}

proc sig {} {
   global x y z nz instr pila
   global d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11
   global d12 d13 d14 d15 d16 d17 d18 d19 d20
   global p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11
   global p12 p13 p14 p15 p16 p17 p18 p19 p20

   switch $instr {
       1  { set linea $d1 }   2 { set linea $d2 }
       3  { set linea $d3 }   4 { set linea $d4 }
       5  { set linea $d5 }   6 { set linea $d6 }
       7  { set linea $d7 }   8 { set linea $d8 }
       9  { set linea $d9 }  10 { set linea $d10 }
       11 { set linea $d11 } 12 { set linea $d12 }
       13 { set linea $d13 } 14 { set linea $d14 }
       15 { set linea $d15 } 16 { set linea $d16 }
       17 { set linea $d17 } 18 { set linea $d18 }
       19 { set linea $d19 } 20 { set linea $d20 }
   }

   set comando [lindex $linea 0]
   set registro [lindex $linea 1]
   set valor [lindex $linea 2]

   if { $registro == "x" } { set a $x }
   if { $registro == "y" } { set a $y }
   if { $registro == "z" } { set a $z }

   if { $comando == "store" } {
        set $registro $valor
        incr instr
   }
   if { $comando == "mul" } {
        if { $valor == "x" } { set b $x }
        if { $valor == "y" } { set b $y }
        if { $valor == "z" } { set b $z }
        set $registro [expr $a*$b]
        incr instr
   }
   if { $comando == "dec" } {
        incr $registro -1
        incr instr
   }
   if { $comando == "cmp" } {
       if { $a == $valor } { set nz 0 }
       if { $a != $valor } { set nz 1 }
       incr instr
   }
   if { $comando == "jp" } {
        if { $nz != 0 } { set instr $valor }
        if { $nz == 0 } { incr instr }
   }
   if { $comando == "halt" } { }
   if { $comando == "push" } {
        set p$pila $a
        incr pila
        incr instr
   }
   if { $comando == "jsbr" } {
        set p$pila [expr $instr+1]
        incr pila
        set instr $registro
        
   }
   if { $comando == "rsbr" } {
        set a [expr $pila-1]
        switch $a {
           1  { set instr $p1 }   2 { set instr $p2 }
           3  { set instr $p3 }   4 { set instr $p4 }
           5  { set instr $p5 }   6 { set instr $p6 }
           7  { set instr $p7 }   8 { set instr $p8 }
           9  { set instr $p9 }  10 { set instr $p10 }
           11  { set instr $p11 }   12 { set instr $p12 }
           13  { set instr $p13 }   14 { set instr $p14 }
           15  { set instr $p15 }   16 { set instr $p16 }
           17  { set instr $p17 }   18 { set instr $p18 }
           19  { set instr $p19 }  20 { set instr $p20 }
       }
       set p$a " "
       incr pila -1
   }
   if { $comando == "pop" } {
       set a [expr $pila-1]
       switch $a {
           1  { set $registro $p1 }   2 { set $registro $p2 }
           3  { set $registro $p3 }   4 { set $registro $p4 }
           5  { set $registro $p5 }   6 { set $registro $p6 }
           7  { set $registro $p7 }   8 { set $registro $p8 }
           9  { set $registro $p9 }  10 { set $registro $p10 }
           11  { set instr $p11 }   12 { set instr $p12 }
           13  { set instr $p13 }   14 { set instr $p14 }
           15  { set instr $p15 }   16 { set instr $p16 }
           17  { set instr $p17 }   18 { set instr $p18 }
           19  { set instr $p19 }  20 { set instr $p20 }
       }
       set p$a " "
       incr pila -1
       incr instr
   }
}

proc pc {} {
   global instr
   incr instr
   if { $instr > 20 } { set instr 1 }
}

proc automatico {} {
   global instr
   while { $instr < 21 } {
     sig
     retardo
  }
}

proc retardo {} {
   global velocidad
   set limite [expr $velocidad + 1]
   while { $velocidad < $limite } {
      incr velocidad
   }
}

proc veloc { velo } {
   global velocidad
   switch $velo {
      "mas" { if { $velocidad < 3000 } {
           set velocidad [expr $velocidad + 100]
          }
        }
      "menos" { if { $velocidad != 0 } {
           set velocidad [expr $velocidad - 100]
          }
        }
   }
}