(defun sim () ;; strictly time based (let ((state '(1 1 0 1)) ;R S Q /Q (old '(x x x x)) (ti 0)) (labels ((show () (apply #'format t "~& ~4D: ~D ~D ~D ~D~%" ti state)) (run () (loop (setq state (multiple-value-list (apply #'flip-flop state))) (when (equal old state) (return)) (setq old state) (show) (incf ti)))) ;; (run) (setf ti 5 (car state) 0) ;R = 0 (run) (setf ti 10 (car state) 1) ;R = 1, again (run) (setf ti 15 (cadr state) 0) ;S = 0 (run) (setf ti 15 (cadr state) 1) ;S = 1, again (run)))) (defun flip-flop (r s q /q) (psetq q (nand r /q) /q (nand s q)) (values r s q /q)) (defun nand (x y) (logand 1 (lognand x y))) ;; 0: 1 1 0 1 ;; 5: 0 1 1 1 ;; 6: 0 1 1 0 ;; 10: 1 1 1 0 ;; 15: 1 0 1 1 ;; 16: 1 0 0 1 ;; 15: 1 1 0 1