;..................
(define Bsetcommon
     (?Rand
        (dot zl1 zm1 off 30000)
        (arrow aa6)
        (s-h-b bb1)(Delay bb2)(Block bb3)
        (J-n bb4)(M-dn bb5)(M-df bb6)(ph-n bb7)
        (trace-1 ztrace)

        (S cc1)(D cc2)(B cc3)(J cc4)(M cc5)(F cc6)(P cc7)
        (arrow-e zarrow-e)(arrow-s zarrow-s)(arrow-n zarrow-n)(arrow-w zarrow-w)
     )
)

;======================= setup  ===============================
(define B-count1 ; binary counter - expanded
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn c15a g17a e13a i11a k15a m9a o13a q1a s11a u9a
              c15b g17b e13b i11b k15b m9b o13b q1b s11b u9b
        )
        (M-up c17a u1a
              c17b u1b)
        (M-uf e15a i13a m11a q9a
              e15b i13b m11b q9b)
        (J-e  g15a k13a o11a s9a
              g15b k13b o11b s9b)

        (ph-n q3a)   
      )
      (Bsetcommon)
   )
)

(define B-count2 ; binary counter -   compact
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn m12a n11a o10a o13a p9a p12a q11a r10a 
              m12b n11b o10b o13b p9b p12b q11b r10b
        )
        (M-up m13a
              m13b)
        (M-uf n12a o11a p10a q9a
              n12b o11b p10b q9b)
        (J-e  o12a p11a q10a r9a
              o12b p11b q10b r9b)

        (ph-n q1a q3a q5a q7a)   

      )
      (Bsetcommon)
   )
)

(define B-clocks; clocks and blocks
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn b22a b18a b14a b10a d19a f11a t13a u23a u15a
              b22b b18b b14b b10b d19b f11b t13b u23b u15b)
        (M-up b23a b19a b15a b11a u22a t23a u13a t15a
              b23b b19b b15b b11b u22b t23b u13b t15b)
        (J-s  c23a c15a
              c23b c15b)
        (J-n  c22a c14a d18a f10a t22a
              c22b c14b d18b f10b t22b)
        (J-w  t14a   
              t14b)
        (Block t18a
               t18b)

        (ph-e d11a c19a)
        (ph-w c18a d10a d15a  d23a  f23a)
      )
      (Bsetcommon)
   )
)

(define B-f2; freq doubler
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn b14a p13a w15a
              b14b p13b w15b)
        (M-up b15a p15a q13a q14a r14a
              b15b p15b q13b q14b r14b)
        (J-s  c15a
              c15b)
        (J-n  c14a
              c14b)
        (J-e  r15a
              r15b)
        (J-w  p14a   
              p14b)

        (ph-w d15a)
      )
      (Bsetcommon)
   )
)


(define B-refl ; reflectors
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn l20a k14a h9a m15a  u11a t10a u19a
              l20b k14b h9b m15b  u11b t10b u19b)
        (M-up h20a l9a k15a m14a  u18a t19a u10a
              h20b l9b k15b m14b  u18b t19b u10b)
        (M-uf l14a
              l14b)
        (J-s  t18a
              t18b)
        (J-n  t11a
              t11b)
        (Block s18a s11a
               s18b s11b)
        (ph-n l11a t15a)
      )
      (Bsetcommon)
   )
)

(define B-bulb ;ON/OFF bulb
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn l18a k14a h8a m15a
              l18b k14b h8b m15b)
        (M-up h18a l8a k15a m14a
              h18b l8b k15b m14b)
        (M-uf l14a
              l14b)
        (J-n  l15a
              l15b)

        (ph-n l11a)
      )
      (Bsetcommon)
   )
)

(define B-delay   ; 2-unit delay
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (Delay n19a
               n19b)
        (M-dn b22a b19a b16a n16a n17a
              b22b b19b b16b n16b n17b)
        (M-up b23a b20a b17a m16a m17a
              b23b b20b b17b m16b m17b)
        (J-s  c23a c20a c17a 
              c23b c20b c17b)
        (J-n  c22a c19a c16a
              c22b c19b c16b)

        (ph-w d17a d20a d23a)
      )

      (Bsetcommon)
   )
)

(define B-cross  ; crossing of data streams
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn f15a m22a
              f15b m22b)
        (M-up f16a l22a
              f16b l22b)
        (J-s  g16a 
              g16b)
        (J-n  g15a 
              g15b)
        (J-e  l21a
              l21b)
        (J-w  m21a
              m21b)

        (ph-w h16a)(ph-n m20a)
      )

      (Bsetcommon)
   )
)


;------------------------------ gates -----------

(define B-diode ; diode
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn m20a
              m20b)
        (M-up l20a
              l20b)
        (J-e  l19a h14a p14a
              l19b h14b p14b)
        (J-w  m19a 
              m19b)
        (J-n  l14a
              l14b)
        (Block h13a h15a p13a p15a
               h13b h15b p13b p15b)

        (ph-n m18a)
      )
      (Bsetcommon)
   )
)

(define B-OR ; OR gate
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn b22a b14a j22a
              b22b b14b j22b)
        (M-up b23a b15a
              b23b b15b)
        (J-s  c23a c15a j14a
              c23b c15b j14b)
        (J-n  c22a c14a
              c22b c14b)

        (ph-w d15a  d23a  f23a)
      )
      (Bsetcommon)
   )
)


(define B-NOT   ; NOT gate
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn e22a b14a
              e22b b14b)
        (M-up e23a b15a j14a
              e23b b15b j14b)
        (J-s  f23a c15a
              f23b c15b)
        (J-n  f22a c14a
              f22b c14b)
        (Block j23a
               j23b)

        (ph-w d15a  g23a i23a)
      )
      (Bsetcommon)
   )
)

(define B-ANDNOT; ANDNOT gate
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn b22a b14a j22a
              b22b b14b j22b)
        (M-up b23a b15a
              b23b b15b)
        (J-s  c23a c15a 
              c23b c15b)
        (J-n  c22a c14a 
              c22b c14b)
        (Block k14a
               k14b)

        (ph-w d15a  d23a  f23a)
      )
      (Bsetcommon)
   )
)

(define B-AND   ; AND gate
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn b22a b19a l18a n23a
              b22b b19b l18b n23b)
        (M-up b23a b20a l23a m18a m19a
              b23b b20b l23b m18b m19b)
        (J-s  c23a c20a
              c23b c20b)
        (J-n  c22a c19a
              c22b c19b)
        (J-w  l22a
              l22b)
        (Block m20a o19a
               m20b o19b)

        (ph-w d19a  d23a  f23a)
      )
      (Bsetcommon)
   )
)

(define B-NAND   ; NAND gate
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn a18a a14a k16a m13a o19a
              a18b a14b k16b m13b o19b)
        (M-up a19a a15a k17a m19a o13a
              a19b a15b k17b m19b o13b)
        (J-s  b15a b19a l17a 
              b15b b19b l17b)
        (J-n  b14a b18a l16a 
              b14b b18b l16b)
        (J-w  m16a
              m16b)
        (J-e  o16a
              o16b)
        (Block n18a n14a
               n18b n14b)

        (ph-w c19a c15a m17a)
      )
      (Bsetcommon)
   )
)

(define B-XOR    ; XOR  gate
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn h13a i23a l12a m23a m13a
              h13b i23b l12b m23b m13b)
        (M-up h23a l23a
              h23b l23b)
        (J-s  m12a
              m12b)
        (J-w  i18a m20a
              i18b m20b)
        (J-e  h18a l20a
              h18b l20b)

        (ph-n l22a)(ph-s i21a)
      )
      (Bsetcommon)
   )
)


;----------------------- flipflops ---------------------
(define B-dualflip3   ; 3-cycle dual-input flip-flop
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn h9a i10a k10a l13a m14a
              h9b i10b k10b l13b m14b)
        (M-up h13a i14a l9a m8a
              h13b i14b l9b m8b)
        (J-e  j10a j8a
              j10b j8b)
        (J-w  k8a
              k8b)
        (J-n  j13a l14a
              j13b l14b)
        (Delay i11a i12a l10a l8a
               i11b i12b l10b l8b)

        (ph-s j23a l17a)
      )
      (Bsetcommon)
   )
)

(define B-dualflip4   ; 4-cycle dual-input flip-flop
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn f13a f8a g13a h25a j13a k15a k13a l11a
              f13b f8b g13b h25b j13b k15b k13b l11b)
        (M-up f14a f11a g14a g15a j14a k14a l8a
              f14b f11b g14b g15b j14b k14b l8b)
        (J-s  g11a k11a j8a
              g11b k11b j8b)
        (J-n  g8a k8a h10a j15a
              g8b k8b h10b j15b)
        (Block e10a m10a
               e10b m10b)       
        (ph-e f25a)(ph-s j19a)
      )
      (Bsetcommon)
   )
)

(define B-dualflip5   ; 5-cycle dual-input flip-flop
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn h8a i10a i12a j25a k10a k12a l13a m14a
              h8b i10b i12b j25b k10b k12b l13b m14b)
        (M-up h13a i14a i11a l8a m6a k11a
              h13b i14b i11b l8b m6b k11b)
        (J-n  j13a l14a
              j13b l14b)
        (J-e  j10a j6a
              j10b j6b)
        (J-w  k6a
              k6b)
        (Delay l6a l10a
               l6b l10b)

        (ph-s l17a)(ph-e h25a)
      )
      (Bsetcommon)
   )
)

(define B-dualflip6   ; 6-cycle dual-input flip-flop
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn m19a
              m19b)
        (M-up o19a
              o19b)
        (J-w  o20a
              o20b)
        (J-e  m20a
              m20b)
        (ph-n n1a)(ph-e k20a)
      )
      (Bsetcommon)
   )
)

(define B-flip3   ; 3-cycle flip-flop
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn h9a i10a k10a l13a l15a m14a
              h9b i10b k10b l13b l15b m14b)
        (M-up h13a i14a j15a l9a m8a k25a
              h13b i14b j15b l9b m8b k25b)
        (M-df k15a
              k15b)
        (J-e  j10a j8a
              j10b j8b)
        (J-w  k8a
              k8b)
        (J-n  j13a l14a
              j13b l14b)
        (Delay i11a i12a l10a l8a
               i11b i12b l10b l8b)

        (ph-w l25a)(ph-s k17a k23a)
      )
      (Bsetcommon)
   )
)

(define B-flip4   ; 4-cycle flip-flop
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn f13a f8a g13a j13a j16a k15a k13a l11a
              f13b f8b g13b j13b j16b k15b k13b l11b)
        (M-up f14a f11a g14a g15a h16a i25a j14a k14a l8a
              f14b f11b g14b g15b h16b i25b j14b k14b l8b)
        (M-df i16a
              i16b)
        (J-s  g11a k11a j8a
              g11b k11b j8b)
        (J-n  g8a k8a h10a j15a
              g8b k8b h10b j15b)  
        (ph-w k25a)(ph-s i23a i19a)
      )
      (Bsetcommon)
   )
)


(define B-flip5   ; 5-cycle flip-flop
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn h8a i10a i12a k10a k12a l13a l15a m14a
              h8b i10b i12b k10b k12b l13b l15b m14b)
        (M-up h13a i14a j15a i11a l8a m6a k11a k25a
              h13b i14b j15b i11b l8b m6b k11b k25b)
        (M-df k15a
              k15b)
        (J-n  j13a l14a
              j13b l14b)
        (J-e  j10a j6a
              j10b j6b)
        (J-w  k6a
              k6b)
        (Delay l6a l10a
               l6b l10b)

        (ph-s k17a)(ph-w m25a r25a)
      )
      (Bsetcommon)
   )
)

(define B-flip6   ; 6-cycle flip-flop
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn l14a m15a
              l14b m15b)
        (M-up b15a l16a n14a o15a
              b15b l16b n14b o15b)
        (M-uf l15a
              l15b)
        (J-s  m16a o16a
              m16b o16b)

        (ph-n b10a)(ph-e c15a i15a)
      )
      (Bsetcommon)
   )
)

(define B-flip6-2   ; 6-cycle flip-flop without moving mirror
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn i14a k13a m12a o13a
              i14b k13b m12b o13b)
        (M-up d15a i16a j15a j14a n14a o12a
              d15b i16b j15b j14b n14b o12b)
        (J-s  m16a m13a n16a
              m16b m13b n16b)
        (J-n  m14a
              m14b)
        (J-w  i15a
              i15b)
       (Block k17a
              k17b)

        (ph-n d6a d12a)(ph-e g15a)(ph-w n12a)
      )
      (Bsetcommon)
   )
)

(define B-flip6-3  ; 6-cycle flip-flop with J-tiles only
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (J-s  k18a m16a m13a n16a o13a
              k18b m16b m13b n16b o13b)
        (J-n  j14a k13a l17a m12a m14a n14a
              j14b k13b l17b m12b m14b n14b)
        (J-w  i15a l18a o12a
              i15b l18b o12b)
        (J-e  d15a i14a i16a j15a k17a
              d15b i14b i16b j15b k17b)

        (ph-n d6a d12a)(ph-e g15a)(ph-w n12a)
      )
      (Bsetcommon)
   )
)



(define B-flip8   ; 8-cycle flip-flop
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn l13a m14a
              l13b m14b)
        (M-up b15a l16a n13a o14a
              b15b l16b n13b o14b)
        (M-uf l15a
              l15b)
        (J-s  m16a o16a
              m16b o16b)

        (ph-n b7a)(ph-e i15a)
      )
      (Bsetcommon)
   )
)

(define B-flip8-2   ; 8-cycle flip-flop without moving mirror
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn i14a k13a m12a p13a
              i14b k13b m12b p13b)
        (M-up d15a i16a j15a j14a l14a o14a p12a
              d15b i16b j15b j14b l14b o14b p12b)
        (J-s  m16a m13a o16a 
              m16b m13b o16b)
        (J-n  m14a
              m14b)
        (J-w  i15a 
              i15b)
        (Block k17a
               k17b)

        (ph-n d2a d10a)(ph-e g15a)(ph-w n12a)
      )
      (Bsetcommon)
   )
)

(define B-flip8-3   ; 8-cycle flip-flop built from J-elements only
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (J-s  k18a l14a m16a m13a o16a p13a
              k18b l14b m16b m13b o16b p13b)
        (J-n  j14a k13a l17a m12a m14a o14a
              j14b k13b l17b m12b m14b o14b)
        (J-w  i15a l18a p12a 
              i15b l18b p12b)
        (J-e  d15a i14a i16a j15a k17a
              d15b i14b i16b j15b k17b)

        (ph-n d2a d10a)(ph-e g15a)(ph-w n12a)
      )
      (Bsetcommon)
   )
)


;--------------------- other applications -----------------
(define B-even-odd; even/odd detector
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn b19a b9a i20a i10a n18a o19a o10a
              b19b b9b i20b i10b n18b o19b o10b)
        (M-up b10a b20a o18a n10a o9a
              b10b b20b o18b n10b o9b)
        (J-n  i19a i9a
              i19b i9b)
        (ph-e c20a g20a c10a g10a)(ph-w d9a)
      )
      (Bsetcommon)
   )
)

(define B-pos-edge; positive edge detector
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn b22a b14a h23a o21a o12a
              b22b b14b h23b o21b o12b)
        (M-up b23a b15a o23a o15a p21a p12a
              b23b b15b o23b o15b p21b p12b)
        (J-s  c15a
              c15b)
        (J-n  h22a c14a 
              h22b c14b)
        (J-w  o22a o14a
              o22b o14b)
        (Block p16a p24a
               p16b p24b)

        (ph-w d15a)(ph-e c23a e23a g23a)
      )
      (Bsetcommon)
   )
)

(define B-neg-edge; negative edge detector
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn b16a b9a i10a h17a n15a n8a o11a
              b16b b9b i10b h17b n15b n8b o11b)
        (M-up b10a b17a n17a n11a o15a
              b10b b17b n17b n11b o15b)
        (J-n  h16a i9a
              h16b i9b)
        (J-w  n16a n9a
              n16b n9b)
        (Block p17a p8a
               p17b p8b) 

        (ph-e c17a e17a g17a c10a g10a)
      )
      (Bsetcommon)
   )
)

(define B-edge; (double) edge detector
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn h11a h13a i13a j10a k14a l13a m12a k25a
              h11b h13b i13b j10b k14b l13b m12b k25b)
        (M-up h12a h14a j13a j25a k18a m10a
              h12b h14b j13b j25b k18b m10b)
        (J-n  i12a j14a k13a
              i12b j14b k13b)
        (J-s  l10a
              l10b)
        (J-e  j18a
              j18b)
        (Block l11a
               l11b) 

        (ph-n j24a)(ph-s k23a k19a)
      )
      (Bsetcommon)
   )
)

(define B-fmir6sim ; simulation of a flip-flop mirror
   (board-setup 
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn o15a o12a p15a q20a q14a r15a n16a o16a n18a o18a n7a o7a n9a o9a
              o15b o12b p15b q20b q14b r15b n16b o16b n18b o18b n7b n9b o7b o9b
              s19a s15a s12a t10a t6a u20a u8a
              s19b s15b s12b t10b t6b u20b u8b)
        (M-up o14a o11a p11a q12a q6a r11a n17a o17a n19a o19a n8a o8a n10a o10a
              o14b o11b p11b q12b q6b r11b n17b o17b n19b o19b n8b o8b n10b o10b
              s14a s11a s7a t20a t16a u18a u6a
              s14b s11b s7b t20b t16b u18b u6b)
        (J-s  r14a
              r14b)
        (J-n  r12a
              r12b)
        (J-e  p13a q19a q18a q8a q7a t18a t8a
              p13b q19b q18b q8b q7b t18b t8b)
        (J-w  o13a p14a p12a r13a s18a s8a
              o13b p14b p12b r13b s18b s8b)
        (Block n6a n20a p16a p10a
               n6b n20b p16b p10b)

        (ph-e a13a g13a m13a)(ph-s u19a q9a)(ph-n u7a)
      )
      (Bsetcommon)
   )
)

(define B-fmir8sim ; simulation of a flip-flop mirror
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn o15a o12a o9a p15a p9a q21a q14a r15a
              o15b o12b o9b p15b p9b q21b q14b r15b
              s20a s17a s15a s12a t10a t5a u21a u8a
              s20b s17b s15b s12b t10b t5b u21b u8b)
        (M-up o17a o14a o11a p17a p11a q12a q5a r11a
              o17b o14b o11b p17b p11b q12b q5b r11b
              s14a s11a s9a s6a t21a t16a u18a u5a
              s14b s11b s9b s6b t21b t16b u18b u5b)
        (J-s  r14a
              r14b)
        (J-n  r12a
              r12b)  
        (J-e  p13a q20a q18a q8a q6a t18a t8a
              p13b q20b q18b q8b q6b t18b t8b)
        (J-w  o13a p14a p12a r13a s18a s8a
              o13b p14b p12b r13b s18b s8b)
        (Block o5a o21a p16a p10a
               o5b o21b p16b p10b)

        (ph-e c13a k13a r8a)(ph-s u19a)(ph-n u7a)
      )
      (Bsetcommon)
   )
)

(define B-P2S ; par to serial converter
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn c11a c16a c21a f13a h18a i16a l18a l23a m21a
              c11b c16b c21b f13b h18b i16b l18b l23b m21b
              r23a s21a v23a
              r23b s21b v23b)
        (M-up c13a c18a c23a f11a h16a i18a l16a l21a m23a
              c13b c18b c23b f11b h16b i18b l16b l21b m23b
              r21a s23a v21a x2a
              r21b s23b v21b x2b)
        (J-s  x7a x12a x17a x22a
              x7b x12b x17b x22b)
        (J-e  f12a h17a l17a l22a r22a v22a
              f12b h17b l17b l22b r22b v22b)
        (J-w  c12a c17a c22a i17a m22a s22a
              c12b c17b c22b i17b m22b s22b)
        (Block y7a y12a y17a y22a
               y7b y12b y17b y22b)
        (Delay d11a d16a d21a e11a e16a e21a f16a f21a g16a g21a
               d11b d16b d21b e11b e16b e21b f16b f21b g16b g21b
               h21a i21a j16a j21a k16a k21a n21a o21a p21a q21a
               h21b i21b j16b j21b k16b k21b n21b o21b p21b q21b
               t21a u21a x11a x16a x21a
               t21b u21b x11b x16b x21b)
        (ph-e a7a a12a a17a a22a)
      )
      (Bsetcommon)
   )
)

(define B-dimp ; dual-input multiplexer
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn j12a l13a o14a q15a m25a  m1a o2a
              j12b l13b o14b q15b m25b  m1b o2b)
        (M-up j14a k15a n13a p12a p14a o25a q14a o1a
              j14b k15b n13b p12b p14b o25b q14b o1b)
        (J-s  j13a m11a p13a
              j13b m11b p13b)
        (J-e  n12a m2a
              n12b m2b)
        (J-w  l12a
              l12b)
        (J-n  m14a o15a
              m14b o15b)
        (Block l10a n10a  k10a o10a
               l10b n10b  k10b o10b)
        (ph-w n2a s25a)(ph-e o13a)(ph-s m23a)
      )
      (Bsetcommon)
   )
)


(define B-mimp ; mono-input multiplexer
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn j12a l13a o14a q15a  m1a o2a o16a
              j12b l13b o14b q15b  m1b o2b o16b)
        (M-up j14a k15a n13a p12a p14a m16a n25a q14a o1a
              j14b k15b n13b p12b p14b m16b n25b q14b o1b)
        (J-s  j13a m11a p13a
              j13b m11b p13b)
        (J-e  n12a m2a
              n12b m2b)
        (J-w  l12a
              l12b)
        (J-n  m14a o15a
              m14b o15b)
        (M-uf n16a
              n16b)
        (Block l10a n10a
               l10b n10b)
        (ph-w n2a q25a)(ph-e o13a)(ph-s n22a)
      )
      (Bsetcommon)
   )
)

(define B-mimp2 ; mono-input multiplexer  II
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn b23a h9a i25a i14a l15a m24a
              b23b h9b i25b i14b l15b m24b)
        (M-up b25a k24a j15a m23a l14a l9a
              b25b k24b j15b m23b l14b l9b)
        (J-s  j14a j11a
              j14b j11b)
        (J-e  i11a k23a
              i11b k23b)
        (J-w  i23a k11a
              i23b k11b)
        (J-n  i9a j9a k9a k15a
              i9b j9b k9b k15b)
        (M-uf j12a
              j12b)
        (Block h13a i13a k13a l13a
               h13b i13b k13b l13b)
        (ph-e l24a)(ph-s b24a)(ph-n j10a)
      )
      (Bsetcommon)
   )
)


(define B-didmp ; dual-input demultiplexer
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn j12a l13a o14a q15a m25a  f9a r9a t10a
              j12b l13b o14b q15b m25b  f9b r9b t10b)
        (M-up j14a k15a n13a p12a p14a o25a q14a f10a h9a t9a
              j14b k15b n13b p12b p14b o25b q14b f10b h9b t9b)
        (J-s  j13a m10a p13a  h10a r10a
              j13b m10b p13b  h10b r10b)
        (J-e  n12a
              n12b)
        (J-w  l12a
              l12b)
        (J-n  m14a o15a
              m14b o15b)
        (Block l9a n9a k11a o11a
               l9b n9b k11b o11b)
        (ph-w s9a s25a)(ph-e g9a o13a)(ph-s m23a)
      )
      (Bsetcommon)
   )
)


(define B-midmp ; mono-input demultiplexer
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn j12a l13a o14a q15a o16a  a10a w10a y11a
              j12b l13b o14b q15b o16b  a10b w10b y11b)
        (M-up j14a k15a n13a p12a p14a m16a n25a q14a  a11a c10a y10a
              j14b k15b n13b p12b p14b m16b n25b q14b  a11b c10b y10b)
        (J-s  j13a m11a p13a  c11a w11a
              j13b m11b p13b  c11b w11b)
        (J-e  n12a
              n12b)
        (J-w  l12a
              l12b)
        (J-n  m14a o15a
              m14b o15b)
        (M-uf n16a
              n16b)
        (Block l10a n10a
               l10b n10b)
        (ph-w x10a q25a)(ph-e b10a o13a)(ph-s n22a)
      )
      (Bsetcommon)
   )
)

(define B-trainset ; train set
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn a2a c10a c19a d20a e6a i10a j12a l13a m1a o2a o16a o14a p24a q15a r17a
              a2b c10b c19b d20b e6b i10b j12b l13b m1b o2b o16b o14b p24b q15b r17b)
        (M-up a6a e2a c11a d10a d19a i16a j14a k15a l24a m17a n13a o1a p14a p12a q14a r10a
              a6b e2b c11b d10b d19b i16b j14b k15b l24b m17b n13b o1b p14b p12b q14b r10b)
        (J-s  d14a c23a j13a m10a p13a
              d14b c23b j13b m10b p13b)
        (J-e  c14a n12a
              c14b n12b)
        (J-w  d23a l12a
              d23b l12b)
        (J-n  c1a d11a c20a l16a m2a m14a o15a p17a
              c1b d11b c20b l16b m2b m14b o15b p17b)
        (M-uf c6a
              c6b)
        (Delay i12a i13a p10a q10a
               i12b i13b p10b q10b)
        (Block c13a c15a e11a b20a d22a d24a k11a l9a l2a n9a o11a
               c13b c15b e11b b20b d22b d24b k11b l9b l2b n9b o11b
               a8a b8a c8a d8a e8a f8a a17a b17a c17a d17a e17a f17a
               g1a g2a g3a g4a g5a g6a g7a g8a g9a g10a 
               g11a g12a g13a g14a g15a g16a g17a g18a g19a g20a
               g21a g22a g23a g24a g25a)
        (ph-w f23a)(ph-e a14a o13a)(ph-s l20a)(ph-n c4a)
      )
      (Bsetcommon)
   )
)

(define B-binadd ; binary adder
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        (M-dn g5a i25a h7a i14a j18a j8a k3a m21a m20a m9a o13a n13a n24a o22a o11a o10a p11a p15a q21a q2a r3a r14a s3a s4a v25a
              g5b i25b h7b i14b j18b j8b k3b m21b m20b m9b o13b n13b n24b o22b o11b o10b p11b p15b q21b q2b r3b r14b s3b s4b v25b)
        (M-up a25a g15a h9a h24a i18a i10a j16a k21a l8a l22a m12a n7a n9a n15a o9a o20a p25a p12a p5a q4a x3a y2a
              a25b g15b h9b h24b i18b i10b j16b k21b l8b l22b m12b n7b n9b n15b o9b o20b p25b p12b p5b q4b x3b y2b)
        (J-s  h8a k5a n8a
              h8b k5b n8b)
        (J-e  i16a l7a o21a r2a
              i16b l7b o21b r2b)
        (J-w  j7a n14a
              j7b n14b)
        (J-n  h15a i24a k9a m10a p21a q14a r4a
              h15b i24b k9b m10b p21b q14b r4b)
       ;(M-uf c6a
       ;      c6b)
        (Delay g6a g7a g8a g9a g10a g11a g12a g13a h16a h17a j24a
               k17a k18a k19a k20a k24a l24a m24a p22a p23a p24a
               q15a q16a q17a q18a q19a q20a
               r6a r7a r8a r9a r10a r11a r12a r13a t3a u3a v3a w3a x4a
               g6b g7b g8b g9b g10b g11b g12b g13b h16b h17b j24b
               k17b k18b k19b k20b k24b l24b m24b p22b p23b p24b
               q15b q16b q17b q18b q19b q20b
               r6b r7b r8b r9b r10b r11b r12b r13b t3b u3b v3b w3b x4b)
        (Block b2a b8a b14a b20a b24a i6a j4a j15a l4a m6a n21a o14a s24a
               w4a w10a w16a w22a)
        (ph-w s25a)(ph-e b25a h25a m8a)(ph-s j17a)(ph-n v16a v22a)
      )
      (Bsetcommon)
   )
)

(define B-binadd2 ; binary adder II
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(del-2 dd6)(clear-2 dd7) ;drawing options
         (dot zfromto off 30000)

        ;special
        (ph-w y10a)
        (M-dn a17a a17b)(M-up a20a a20b)(M-df a18a a18b)

        ;part 1
        (M-dn b20a b19a c17a c15a e14a e10a f14a f15a h19a i18a i17a
              b20b b19b c17b c15b e14b e10b f14b f15b h19b i18b i17b)
        (M-up d10a d12a d14a d16a e12a e18a f11a g16a g20a h15a
              d10b d12b d14b d16b e12b e18b f11b g16b g20b h15b)
        (J-s  b14a b13a b11a
              b14b b13b b11b)
        (J-w  b17a f16a
              b17b f16b)
        (J-n  e15a f13a
              e15b f13b)
        (M-df b10a e11a e16a
              b10b e11b e16b)
        (M-uf e13a
              e13b)
        (Block b18a f17a g14a)

        ;part 2
        (M-dn j20a j19a k17a k15a m14a m10a n14a n15a p19a q18a q17a
              j20b j19b k17b k15b m14b m10b n14b n15b p19b q18b q17b)
        (M-up l10a l12a l14a l16a m12a m18a n11a o16a o20a p15a
              l10b l12b l14b l16b m12b m18b n11b o16b o20b p15b)
        (J-s  j14a j13a j11a
              j14b j13b j11b)
        (J-w  j17a n16a
              j17b n16b)
        (J-n  m15a n13a
              m15b n13b)
        (M-df j10a m13a m16a
              j10b m13b m16b)
        (M-uf m11a
              m11b)
        (Block j18a n17a o14a)

        ;part 3
        (M-dn r20a r19a s17a s15a u14a u10a v14a v15a x19a y18a y17a
              r20b r19b s17b s15b u14b u10b v14b v15b x19b y18b y17b)
        (M-up t10a t12a t14a t16a u12a u18a v11a w16a w20a x15a
              t10b t12b t14b t16b u12b u18b v11b w16b w20b x15b)
        (J-s  r14a r13a r11a
              r14b r13b r11b)
        (J-w  r17a v16a
              r17b v16b)
        (J-n  u15a v13a
              u15b v13b)
        (M-df r10a u16a
              r10b u16b)
        (M-uf u11a u13a
              u11b u13b)
        (Block r18a v17a w14a)

      )
      (Bsetcommon)
   )
)

;-------------------------    fun   -----------------------------
(define B-mainspring; fun spiral by greg schmidt
   (board-setup
      (Self
         (run1-2 dd1)(run6-2 dd2)(run-2 dd3)(draw-1 dd4)(shift-2 dd5)(clear-2 dd6)
         (dot zfromto off 30000)

        (M-dn b1a c2a c3a d4a e5a f6a g7a h8a i9a j10a k11a l12a n1a o2a
              b1b c2b c3b d4b e5b f6b g7b h8b i9b j10b k11b l12b n1b o2b
              n14a o15a p16a q17a r18a s19a t20a u21a v22a w23a x24a
              n14b o15b p16b q17b r18b s19b t20b u21b v22b w23b x24b)
        (M-up b24a c23a d22a e21a f20a g19a h18a i17a j16a k15a l14a o1a
              b24b c23b d22b e21b f20b g19b h18b i17b j16b k15b l14b o1b
              o12a p11a q10a r9a s8a t7a u6a v5a w4a x3a c1a
              o12b p11b q10b r9b s8b t7b u6b v5b w4b x3b c1b)
        (J-n  b2a n2a
              b2b n2b)
        (Block a2a m2a
               a2b m2b)

        (ph-n b3a)
      )
      (Bsetcommon)
   )
)


;========================== variants ===================================
;(variant (title "-"))
(variant (title "Compact binary counter")
   (B-count2)
)

(variant (title "Clocks and blocks")
   (description
      "Hit the run button to see the photon guns (=clocks) in action.
      \From top to bottom they shoot at frequency 2, 3, 4 and 5.   
      \
      \Note that the photons always have be at least 2 units apart;
      \otherwise the program might not process them properly.
      \
      \Three types of blocks are shown to the right.
      \Blocks catch photons.  
      \We see that the black square as block is actually redundant;
      \a block can be built from three mirrors and one junction (top)
      \or from four mirrors and a junction (bottom). 
      \However, we will use the black square block in our variants 
      \because it keeps our constructions simple and saves space. 
      "
   )
   (B-clocks)
)

(variant (title "Frequency doubler")
   (description
      "Hit the run button to see the 4-cycle photon gun (=clock) in action.
      \
      \The clock shoots at a gagdet which serves as a frequency doubler:
      \for each photon, a twin is created which trails the photon by 2 units.
      "
   )
   (B-f2)
)


(variant (title "Reflectors")
   (description
      "Hit the run button.
      \The double reflector shown on the left side works for photons
      \coming in from either north or south direction.
      \The reflector delays the photons by 6 ticks.
      \
      \The setup on the right shows reflectors (designed by Greg Schmidt) 
      \which only work for photons coming in from one direction.
      \These reflectors delay the photon by 4 ticks.
      "
   )
   (B-refl)
)

(variant (title "On/Off bulb")
   (description
      "Hit the run button.
      \The 'bulb' (the red flipflop-mirror) toggles between red and yellow
      \when a photon passes through.
      "
   )
   (B-bulb)
)

(variant (title "Delays")
   (description
      "Click the 'run 6' button three times. 
      \We see that three idenatical data streams are
      \created by the three clocks at left.
      \The top stream travel undisturbed.
      \The centre stream is delayed by one tick
      \The botton stream is delayed by two ticks.
      \In this case a set of four fixed mirrors creates a 2-tick delay.
      "
   )
   (B-delay)
)

(variant (title "Crossing")
   (description
      "Hit the run button. 
      \We see that the two data streams cross each other without
      \interference because they are out of step.
      \This demonstrates that in Photonworld no special gadgetry
      \is necessary for crossings as long as the data streams
      \are kept out of step.
      \This greatly helps to keep our constructions simple.
      "
   )
   (B-cross)
)

;------------------------------- gates ----------------
(variant (title "-"))
(variant (title "Logical gates: Diode (on-way gate)")
   (description
      "Hit the run button.
      \A junction in combination together with two blocks works as a diode.
      "
   )
   (B-diode)
)

(variant (title "Logical gates: OR")
   (description
      "Hit the run button.
      \The junctions work as OR gates;
      \Note that these OR gates can have up to three input lines.
      "
   )
   (B-OR)
)

(variant (title "Logical gates: NOT")
   (description
      "Hit the run button.
      \When two photons collide in free space they will be eliminated.
      \This serves us as to build a simple NOT(A) gate.
      \Here data stream A is the one coming from the  bottom,
      \A clock of frequency two is used at the top.
      \
      \The data stream exiting to the right represents NOT(A).
      \This means that it contains a photon if and only if there is no photon coming
      \from A.
      "
   )
   (B-NOT)
)

(variant (title "Logical gates: ANDNOT")
   (description
      "Hit the run button.
      \When two photons collide in free space they will be eliminated.
      \This serves us to  build as a simple ANDNOT(A,B) gate.
      \Here data stream A is the one entering from the top,
      \B the one entering from the left.
      \The data stream finally heading south is ANDNOT(A,B).
      \This means that it contains a photon whenever there is a photon coming
      \from A, but not from B.
      "
   )
   (B-ANDNOT)
)

(variant (title "Logical gates: AND")
   (description
      "Hit the run button.
      \
      \The AND gate outputs a photon only if
      \there is a photon coming from both inputs simultaneously.
      \
      \Here we used two ANDNOT gates to build the AND gate:
      \AND(A,B) = ANDNOT(A,(ANDNOT(A,B)).
      \Data stream A is the one on the top.
      "
   )
   (B-AND)
)

(variant (title "Logical gates: NAND")
   (description
      "Hit the run button.
      \We could build a NAND gate from a NOT anmd and AND gate NAND(A,B) = NOT(AND(A,B).
      \but the construction shown here is much simpler.
      \It uses the fact that NAND(A,B) = OR (NOT(A), NOT(B)).
      "
   )
   (B-NAND)
)

(variant (title "Logical gates: XOR")
   (description
      "Hit the run button.
      \
      \The XOR gate outputs a photon if and only if
      \there is a photon coming both from either input line, but not from both
      \simultaneously.
      \
      \Note the extreme simplicity of the gate:
      \it uses only three elements: two mirrors and a junction.
      "
   )
   (B-XOR)
)


;---------- dual-input flip-flops ------------
(variant (title "-"))
(variant (title "3-cycle dual-input flip-flop")
   (description
      "In dual-input flip-flops the start and stop signal arrive on
      \separate tracks.
      \
      \Hit the run button.
      \Watch how the 6-cycle clock containing two photons is switched on
      \and off by the incoming photons.
      \Note that the photons must be sychronized;
      \they must have a delay between them of a multiple of 3.
      \
      \Restriction: In this setup, a stop signal may only be sent when
      \the flip-flop is ON.
      "
   )
   (B-dualflip3)
)

(variant (title "4-cycle dual-input flip-flop")
   (description
      "Dual-input flip-flops are the most simple flip-flops
      \in Photonworld. The start and stop signals arrive on
      \separate tracks.
      \
      \Hit the run button.
      \Watch how the two 4-cycle clocks are switched on
      \and off by the incoming photons.
      \Note that the incoming photons must be sychronized;
      \they must have a delay between them of a multiple of 4.
      "
   )
   (B-dualflip4)
)

(variant (title "5-cycle dual-input flip-flop")
   (description
      "In dual-input flip-flops the start and stop signal arrive on
      \separate tracks.
      \
      \Hit the run button.
      \Watch how the 10-cycle clock containing two photons is switched on
      \and off by the incoming photons.
      \Note that the photons must be sychronized;
      \they must have a delay between them of a multiple of 5.
      \
      \Restriction: In this setup, a stop signal may only be sent when
      \the flip-flop is ON.
      "
   )
   (B-dualflip5)
)

(variant (title "6-cycle dual-input flip-flop")
   (description
      "Dual-input flip-flops are the most simple flip-flops
      \in Photonworld. The start and stop signals arrive on
      \separate tracks.
      \
      \Hit the run button.
      \Watch how the 6-cycle clock is switched on and off
      \by the incoming photons.
      \Note that the photons must be sychronized;
      \they must have a delay between them of a multiple of 6.
      "
   )
   (B-dualflip6)
)

;.................. other flip-flops...........................
(variant (title "-"))
(variant (title "3-cycle flip-flop")
   (description
      "Hit the run button.
      \Watch how the 3-cycle flip-flop is switched on and off
      \by the incoming photons.
      \
      \The flip-flop uses a 6-cycle photon gun which contains
      \two photons. The photons in this gun  circuit have a delay
      \of 3 ticks between them.
      \The two photons are simultaneously created by one
      \incoming phton and simultaneaously deleted by the next incoming
      \photon.
      \
      \This basic principle can be used for many flip-flops.
      \However, in general it does not produce the smallest
      \flip-flops.
      "
   )
   (B-flip3)
)

(variant (title "4-cycle flip-flop")
   (description
      "Hit the run button.
      \Watch how the 4-cycle flip-flop is switched on and off
      \by the incoming photons.
      \
      \The flip-flop is constructed from two 8-cycle flip-flops,
      \which have a 4-cycle delay between them.
      "
   )
   (B-flip4)
)

(variant (title "5-cycle flip-flop")
   (description
      "Hit the run button.
      \Watch how the 5-cycle flip-flop is switched on and off
      \by the incoming photons.
      \
      \The flip-flop uses a 10-cycle photon gun which contains
      \two photons. The photons in this gun circuit have a delay
      \of 5 ticks between them.
      \The two photons are simultaneously created by one
      \incoming phton and simultaneaously deleted by the next incoming
      \photon.
      \
      \This basic principle can be used for many flip-flops.
      \However, in general it does not produce the smallest
      \flip-flops.
      "
   )
   (B-flip5)
)
(variant (title "6-cycle flip-flop")
   (description
      "Hit the run button.
      \Watch how the 6-cycle clock is switched on and off
      \by the incoming photons.
      "
   )
   (B-flip6)
)

(variant (title "6-cycle flip-flop without moving mirror") 
   (description
      "Hit the run button.
      \Watch how the 8-cycle clock is switched on and off
      \by the incoming photons.
      \
      \Note how much more complicated this construction is
      \(because we avoided using a moving mirror) 
      \compared to the flip-flop in the previous variant.  
      \
      \And here is how it works in detail:
      \The incoming photon is split into two parallel ones.
      \The top photon triggers the start of a 6-cycle flip-flop,
      \the one beneath stops the clock by colliding with the
      \circulating photon.
      \When the flip-flop is not active, the second photon is
      \stopped from eliminating its twin; this is done by a photon
      \emitted by a permanent 6-cycle clock, seen on the lower right.
      \
      \Once the flip-flop is up and running, it not only emits photons
      \to the right, but also emits one to the bottom.
      \This second photon then collides with 
      \the flip-flop photons, thereby rendering the temporary clock inactive
      \and thus allowing the incoming twin to stop the flip-flop cycle. 
      \Hence we have three data streams stopping each other:
      \Clock output stops twin, twin stops flip-flop cycle, flip-flop
      \output stops clock output. 
      "
   )
   (B-flip6-2)
)

(variant (title "6-cycle flip-flop built from J-elements only") 
   (description
      "Hit the run button.
      \Watch how the 8-cycle clock is switched on and off
      \by the incoming photons.
      \
      \This flip-flop is basically the same as in the previous variant.
      \However, we have replaced all mirrors and blocks with J-elements.   
      \This proves that the junction/splitter is the only type of element we 
      \really need to build a flip-flop, and hence the only one we need
      \to build all elements of a computer.
      \
      \We will see in another variant further down how we can
      \simulate a flip-flop mirror using only J-elements.
      "
   )
   (B-flip6-3)
)


(variant (title "8-cycle flip-flop") 
   (description
      "Hit the run button.
      \Watch how the 8-cycle clock is switched on and off
      \by the incoming photons.
      "
   )
   (B-flip8)
)

(variant (title "8-cycle flip-flop without moving mirror") 
   (description
      "Hit the run button.
      \Watch how the 8-cycle clock is switched on and off
      \by the incoming photons.
      \
      \Note how much more complicated this construction is
      \(because we avoided using a moving mirror)
      \compared to the flip-flop in the previous variant.
      \
      \And here is how it works in detail:
      \The incoming photon is split into two parallel ones.
      \The top photon triggers the start of the 8-cycle flip-flop,
      \the one beneath stops the flip-flop by colliding with the
      \circulating photon.
      \When the flip-flop is not active, the second photon is
      \stopped from eliminating its twin; this is done by a photon 
      \emitted by the 8-cycle clock, seen on the lower right.
      \
      \Once the flip-flop is up and running, it not only emits photons
      \to the right, but also emits one to the bottom.
      \This second photon then collides with
      \the clock photons, thereby rendering the clock inactive
      \and thus allowing the incoming twin to stop the flip-flop
      \cycle. 
      \Hence we have three data streams stopping each other:
      \Clock output stops twin, twin stops flip-flop cycle, flip-flop
      \output stops clock output. 
      "
   )
   (B-flip8-2)
)

(variant (title "8-cycle flip-flop built from J-elements only") 
   (description
      "Hit the run button.
      \Watch how the 8-cycle clock is switched on and off
      \by the incoming photons.
      \
      \This flip-flop is basically the same as in the previous variant.
      \However, we have replaced all mirrors and blocks with J-elements.   
      \This proves that the junction/splitter is the only type of element we 
      \really need to build a flip-flop, and hence the only one we need
      \to build all elements of a computer.
      \
      \We will see in another variant further down how we can  
      \simulate a flip-flop mirror using only J-elements.
      "
   )
   (B-flip8-3)
)


;----------------- others applications --------------------
(variant (title "-"))
(variant (title "Even/Odd detector")
   (description
      "Hit the run button. 
      \ 
      \The top loop emits serieses of two photons,  
      \the bottom one serieses of three photons.
      \We see that the even/odd detector to the right
      \releases a photon (namely the last photon of the series) 
      \if and only if the data stream contained an even number of photons.
      "
   )
   (B-even-odd)
)

(variant (title "Positive edge detector")
   (description
      "Hit the run button. 
      \A negative edge detector emitsa photon
      \whenever the last photon of a series of photons arrives.
      \
      \A delayed signal interferes with the original data stream
      \to create the desired result.
      "
   )
   (B-pos-edge)
)

(variant (title "Negative edge detector")
   (description
      "Hit the run button. 
      \The top clock creates a 2-cycle data stream 11100001110000...,
      \the bottom one a 4-cycle data stream 11001100...        
      \The negative edge detectors at the right emit a photon
      \whenever the last photon of a series of photons arrives.
      \Note that this signal is delayed by 2 (respectively 4) ticks.  
      \The original signal interferes with a delayed twin
      \to create the desired result.
      "
   )
   (B-neg-edge)
)

(variant (title "Double edge detector")
   (description
      "Hit the run button.
      \A (double) edge detector emits a photon
      \either when the first or after the last photon of a series of photons arrives.
      \Note that the second signal is delayed.  
      \
      \We have built the edge detector by XORing the datastream with
      \a delayed copy of itself.
      \Note that the construction incorporates a position where two beams
      \cross each other without interference.
      "
   )
   (B-edge)
)

(variant (title "Flip-flop mirror (6-cycle) built from other elements")
   (description
      "Hit the run button.
      \Watch how the (6-cycle) input data are redirected to the
      \top and to the bottom alternatingly. 
      \The technical term for such an element is a 'multiplexer'.
      \
      \This shows that we can simulate a flip-flop mirror
      \from the other building elements.
      \The only restriction is that the incoming photons have
      \a distance of each other which is a multiple of 6. 
      \
      \Of course we can create a similar setup which uses J-elements
      \only, omitting the block and mirror elements.
      \I leave this as a task to the reader.      
      "
   )
   (B-fmir6sim)
)


(variant (title "Flip-flop mirror (8-cycle) built from other elements")
   (description
      "Hit the run button.
      \Watch how the (8-cycle) input data are redirected to the
      \top and to the bottom alternatingly.
      \The technical term for such an element is a 'multiplexer'.
      \
      \This shows that we can simulate a flip-flop mirror
      \from the other building elements.
      \The only restriction is that the incoming photons have
      \a distance of each other which is a multiple of 8.
      \
      \Of course we can create a similar setup which uses J-elements
      \only, omitting the block and mirror elements.
      \I leave this as a task to the reader.
      "
   )
   (B-fmir8sim)
)

(variant (title "Parallel to serial converter")
   (description
      "Hit the run button.
      \
      \Parallel input wires carry photons which represent
      \1/2/4/8 bits of data.
      \Design by Greg Schmidt.
     "
   )
   (B-P2S)
)

(variant (title "Dual-input multiplexer")
   (description
      "A multiplexer directs the incoming data stream to one of two output lines.
      \Signals separate from the data stream control
      \which of the output lines the data stream is directed to.
      \The chosen direction stays the same until the next control signal arrives.
      \In the dual-input version of a multiplexer these control signals
      \arrive on separate lines.
      \
      \Hit the run button.
      \Here we present a multiplexer for 6-cycle data.
      \The data stream is entering from the bottom.
      \First it is directed to the left,
      \later to the right and finally it keeps going to the left.
      \These actions are triggered by control signals entering
      \the multiplexer on separate input lines.
      \Note: it is allowed that one of these control lines carries several
      \signals in a row.
      \
      \Technical description:
      \The circuit contains two 6-cycle flip-flops,
      \only one of which is active at any time.
      \One flip-flop blocks the output to the left,
      \the other the output to the right.
      \The incoming signal starts one flip-flop and
      \- via a beam splitter - simultaneously stops the other flip-flop.
     "
   )
   (B-dimp)
)

(variant (title "Mono-input multiplexer I")
   (description
      "A multiplexer directs the incoming data stream to one of two output lines
      \(here left and right).
      \Signals separate from the data stream control which of the output lines
      \the data stream is directed to.
      \The chosen direction stays the same until the next control signal arrives.
      \In the mono-input version of a multiplexer these control signals
      \arrive on the same line.
      \
      \Here we present a multiplexer for 6-cycle data.
      \Hit the run button.
      \The data stream is entering from the bottom.
      \First it is directed to the left,
      \later to the right and finally it keeps going to the left.
      \These actions are triggered by control signals entering
      \the multiplexer from above.
      \Note that each incoming signal changes the
      \direction of the outgoing data stream.
      \
      \Technical description:
      \The circuit contains two 6-cycle flip-flops,
      \only one of which is active at any time.
      \One flip-flop blocks the output to the left,
      \the other the output to the right.
      \The incoming signal starts one flip-flop and
      \- via a beam splitter - simultaneously stops the other flip-flop.
      \An alternating mirror takes care that the next signal
      \starts and stops the opposite flip-flops.
     "
   )
   (B-mimp)
)
(variant (title "Mono-input multiplexer II")
   (description
      "A multiplexer directs the incoming data stream to one of two output lines
      \(here left and right).
      \Signals separate from the data stream control which of the output lines
      \the data stream is directed to.
      \The chosen direction stays the same until the next control signal arrives.
      \In the mono-input version of a multiplexer these control signals
      \arrive on the same line.
      \
      \Hit the run button.
      \Here we present a multiplexer for 6-cycle data.
      \(Note its small area of only 5x7 !)
      \The data stream is entering from the top right.
      \The control data enters from the top left.
      \Note how it changes the direction of the outgoing data stream.
      \
      \Technical description:
      \The circuit uses an alternating mirror,
      \which directs the uncoming data to the left and right output.
      \Each incoming photon gets a twin attched to it (2 ticks behind it)
      \which resets the alternating mirror.
      \Hence the data bits actually all go into one direction.
      \The twins are eliminated by a 6-cycle clock when they leave the multiplexer.
      \
      \The control signal (top left) starts flips the alternating mirror
      \and thus flips the direction of the outgoing data.
      \Note that the control signal must be timed 4 ticks after the
      \6-cycle clocking.
      \The control signal is eliminated when it leaves the multiplexer.
     "
   )
   (B-mimp2)
)

(variant (title "Dual-input demultiplexer")
   (description
      "A demultiplexer directs one of two incoming data streams to one output line.
      \Signals separate from the data stream control
      \which of the input data streams is chosen.
      \The chosen direction stays the same until the next control signal arrives.
      \In the dual-input version of a multiplexer these control signals
      \for 'left' and 'right' arrive on separate lines.
      \This has the advantage that several 'left' commands can be sent
      \without a 'right' signal in between (application: binary adder).
      \
      \Here we present a demultiplexer for 6-cycle data.
      \Note that the demultiplexer is very similar to one of the 
      \multiplexers we presented before (see other variant).
     "
   )
   (B-didmp)
)

(variant (title "Mono-input demultiplexer")
   (description
      "A demultiplexer directs one of two incoming data streams to one output line.
      \Signals separate from the data stream control
      \which of the input data streams is chosen.
      \The chosen direction stays the same until the next control signal arrives.
      \In the mono-input version of a multiplexer these control signals
      \for 'left' and 'right' arrive on the same line.
      \
      \Here we present a demultiplexer for 6-cycle data.
      \Note that the demultiplexer is identical to the multiplexer (see other variant).
     "
   )
   (B-midmp)
)

(variant (title "Train set")
   (description
      "It is possible to build a computer using a train , straight tracks and shunts (also called 'points').
      \Adam Chalcraft and Michael Greene discovered that three types of shunts are sufficient
      \Such a set of shunts is called a 'train set'.
      \
      \Let us start with the so-called lazy point.
      \It's a Y-shaped piece of track. A train entering the Y from below runs up
      \the upright and out of whichever arm of the Y the points are set for,
      \leaving them unchanged.
      \But a train that enters from one of the arms will--if necessary--reset the points
      \so that they connect that arm to the upright and then exit via the upright.
      \Lazy points have two states, depending on which arm is connected to the upright:
      \call them left and right.
      \A Photonworld version of a lazy point is shown on the right.
      \It has been built using dual-input multiplexer (see associated variant).
      \A single photon plays the role of the train.
      \Data entering from below exit on the top left until some photon enters from the top right.
      \
      \The next type of point is a sprung one.
      \It's like a lazy point except that any train entering along the upright of the Y
      \always leaves by the same arm. Hence there is a left-sprung type and a right-sprung type.
      \In Photonworld we can build these points from a splitter and a diode (see top left).
      \
      \The third kind of point is a flip-flop.
      \With a flip-flop shunt, trains always enter along the upright of the Y
      \and exit through the left and right arms alternately.
      \In Photonworld we can use a flip-flop mirror for this (see bottom left).
      "
   )
   (B-trainset)
)

(variant (title "Binary Adder")
   (description
      "Of course a binary adder is possible in Photonworld.
      \Hit the run button.
      \At the top left the number A = '11' (=3) enters the adder,
      \with the lowest digit first.
      \Similarly, at the top right the number B = '111' (= 7) enters the adder.
      \We will see the number C = '1010' leaving the adder, with the lowest digit first.
      \In order to see where the number C starts, we have A running prarallel
      \to it at the right border when the result comes out.
      \Black squares have been added to indicate th positions of the digits.
      \
      \The setup in principle can take numbers of any size
      \(you would have to extend the incoming lines for this).
      \
      \Techinal description:
      \Bit by bit, the input data are XORed : Ci = XOR(Ai,Bi).
      \When a bit is found in both data streams at the same time (AND(Ai),(Bi) = true),
      \the system switches to Ci = XOR(NOT(Ai),Bi) from the next bit onwards.
      \It stays in this mode until a zero is found in both A and B
      \(we check AND(NOT(Ai),NOT(Bi)) = true).
      \Then the system switches back to XOR(Ai, Bi) from the next bit onwards.
      \
      \The switch between the two XOR modes is executed via a
      \so-called double-input demultiplexer (see associated variant).
     "
   )
   (B-binadd)
)

(variant (title "Binary Adder II")
   (description
      "Here the data are represented by mirrors,
      \and a single photon does all the processing.
      \(Compare the binary adder in the Zillions game 'Train Set'!)
      \
      \The mirrors representing the data are neatlky place on top iof each other.
      \At the bottom (yellow, red, red) we see input 011 = 3,
      \the middle shows 101 (red, yellow, red) = 5.
      \We could also use fixed mirrors for these data, but the flip-flop mirrors
      \stand out more.
      \The top line will show 1000 (red, yellow, yellow, yellow) at the end,
      \with the highest digit in the top left corner.
      \(the two input numbers are changed in the process, but that can be avoided 
      \by using fixed mirrors, as mentioned above.)
      \
      \In order to take larger input numbers, this setup has to be enlarged
      \by the same area for each additional digit.
      \Remember that the previous adder does not have thios requirement.
      \
      \Techinal description:
      \Bit by bit, the input data are checked: for each 1, the mirror at the top
      \is flipped. If it is set to 'left', then the photon makes an additional
      \travel to the result bit to the left and flips this mirror as well.
      \
      \The biggest design problem here is that the resulting bits
      \must be able to be written from two different circuits.
      \Note how that is done by having parts of the circuit shared,
      \but the photon is running in opposite direction in the second loop,
      \which allows using separate entrances and exits for the two cases.
      \The same trick has been used in the binary adder of the 'Train Set' game.
     "
   )
   (B-binadd2)
)


;----------------- just fun --------------------
(variant (title "-"))
(variant (title "Fun: Mainspring")
   (description
      "Hit the run button.
      \
      \A single photon spirals its way towards the centre and then
      \back the same way.
      \A little fun thing by Greg Schmidt.
     "
   )
   (B-mainspring)
)
