   j
                 Μ   Μ                     Μ  ΐ ΐ         ΐ ΐ         Μ   Μ  Μ Μ                                              Μ   Μ                                                                                                                                                                                                                                                                                                                                                                                                                    Μ                               Μ                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ΜΜ       ΐ ΐ                       ΐ   ΐ       Μ ΐ                   Μ Μ  ΐ                  Μ                        ΐ  ΐ                  ΐΜ       Μ ΐ                                                                                                                                                                                                                                                                                                                                      ΐ ΐ     ΜΜ                ΐ Μΐ ΐ   Μ   ΐ          ΐ   ΐΜ    ΐΜΜ    ΜΜΜΜ   ΐΜΜ ΐ  ΜΜ     ΜΜΜ     ΜΜΜΜ   ΜΜΜ       ΐΐ  Μ ΐΐ  Μ                Μ ΐ         ΐΜ                                                                                                                                                                                                                                                                                                                                                                                                      ΐ    ΜΜ      Μ         ΐΜ  ΐΜ  ΐ ΜΜ     Μ        ΐΜ  ΐΜ                                                                                                                                                                                                                                                                                                                                                                                                                                                                    Μ  Μ  ΐ                     ΐ  Μ  ΐ	>                                                                ΐ? 3333333333033 3  0  033333333333333 03 333333333333333333333333333333333333333333333333333333333333333333333333333333 0 3 3 0 0 33 33303333 03 0 0 33  333333333333333333333333333333333333333333333333333333333333333333333333333333 03 0 00033  3  33 33333 0 0333 03 3333333333333333333333333333333333333333333333333333333333333333333333333333333 0 03 0300 0 303003 33   30 033 333333333333333333333333333333333333333333333333333333333333333333333333333333  3 3 3 033 3  3 333333 3 0033   033333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333330333 03333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CeECeE3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333TE333333333333333T PS P433333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CP333333333333333`c433333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333dP33333333333333CUd`c`TE33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 E33333333333333C@c`c`C@33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333p433333333333333CDc`c`CD33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333pD3333333333333333SPSP3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333dp@333333333333333CuVSvE333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CE333333333333333C @C @3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333D4333333333333333Cyv	G33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CU4TE33333333333333C   F33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333S 5P33333333333333d`43333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333S 5P3333333333333C     E3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CeffUeV43333333333d`43333333333TfE333333333333333333333333333333333333333333333333333333333333333333333333333333333S PS  E333333333C       E333333333C P333333333333333333333333333333333333333333333333333333333333333333333333333333333S Pcf@3D4333333d`4333333D4C``333333333333333333333333333333333333333333333333333333333333333333333333333333333CFS`TUTE43333C         E33333DVTUdP3333333333333333333333333333333333333333333333333333333333333333333333333333333333D4CDPPE3333d`4333CU U@E3333333333333333333333333333333333333333333333333333333333333333333333333333333333333DDdpEdP333C           E333S`Du`DD4333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c`3c`333d`433c`3c`3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333DDdpEd`U4C             E3Te`Du`DD4333333333333333333333333333333333333333333333333333333333333333333333333333333333D43CDPPv 4d`4pU U@E33333333333333333333333333333333333333333333333333333333333333333333333333333333T43S`TUTE5               4 EVTUdP33333333333333333333333333333333333333333333333333333333333333333333333333333333p53cf@3D4d gg`4D4C``33333333333333333333333333333333333333333333333333333333333333333333333333333333d 43S  E333C                   F333C P33333333333333333333333333333333333333333333333333333333333333333333333333333333CUDeUeV43333d`43333TfE3333333333333333333333333333333333333333333333333333333333333333333333333333333333T P4333333CT               VD33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CfE333333335333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333S`4dP33333333D             F4333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333SPDTP33333333353333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CUUE333333333D           F433333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333w433333333335333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333T P43333333333D         F4333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CF333333333333533333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333D4333333333333D       F433333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333TE33333333333335333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333P3333333333333D     F4333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333P33333333333333	53333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333TE33333333333333T    P433333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333SW333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333C `d @333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CuPSpE3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333dgeg43333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333  5333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333C 	 F333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333S   P333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333S `e P333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333CFCF3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333D43D4333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333ρ ;; title:   Conway's Game of Life
;; author:  Sakura
;; desc:
;; site:
;; license: MIT License (change this to your license of choice)
;; version: 0.1
;; script:  scheme
(define-macro (p2 n) `(expt 2 ,n))
(define width 240)
(define height 136)
(define-macro (wrx x) `(modulo ,x width))
(define-macro (wry y) `(modulo ,y height))
(define-macro (v@ tbl i) `(vector-ref ,tbl ,i))
(define (make-tbl)
	(do ((v (make-vector height))
      (i 0 (+ i 1)))
   ((= height i) v)
  (vector-set! v i
  	 (make-vector (floor (/ width 16)) 0))))
(define (clear-line line)
 (do ((i 0 (+ i 1)))
   ((= i (length line)))
  (vector-set! line i 0)))
(define (clear-n-tbl)
 (do ((i 0 (+ i 1)))
   ((= i (length n-tbl)))
  (clear-line (v@ n-tbl i))))
(define n-tbl (make-tbl))
(define a-tbl (make-tbl))
(define l-tbl (make-tbl))
(define r-tbl (make-tbl))
(define 1mask
 (vector (p2 0)(p2 4)(p2 8)(p2 12)
      (p2 16)(p2 20)(p2 24)(p2 28)
      (p2 32)(p2 36)(p2 40)(p2 44)
      (p2 48)(p2 52)(p2 56)(p2 60)))
(define 0mask
 (do ((v (make-vector (length 1mask)))
      (i 0 (+ i 1)))
   ((= i (length v)) v)
  (vector-set! v i
   (logxor -1
    (v@ 1mask i)))))
(define nmask
 (do ((v (make-vector (length 1mask)))
      (i 0 (+ i 1)))
   ((= i (length v)) v)
  (vector-set! v i
   (* 15 (v@ 1mask i)))))
	
(define (getn x y)
 (let* ((line (v@ n-tbl y))
        (pos (floor (/ x 16)))
        (v (v@ line pos))
        (bitpos (modulo x 16))
        (denom (v@ 1mask bitpos))
        (m (v@ nmask bitpos))
        )
  (/ (logand m v) denom)))
	
(define (get-tbl tbl x y)
 (let* ((line (v@ tbl y))
        (pos (floor (/ x 16)))
        (v (v@ line pos))
        (m (v@ 1mask
            (modulo x 16))))
 	(not (= 0 (logand v m)))))
	
(define (set1 tbl x y)
 (let* ((line (v@ tbl y))
        (pos (floor (/ x 16)))
        (v (v@ line pos))
        (m (v@ 1mask
            (modulo x 16))))
 	(vector-set! line pos (logior v m))))
	
(define (set0 tbl x y)  
 (let* ((line (v@ tbl y))
        (pos (floor (/ x 16)))
        (v (v@ line pos))
        (m (v@ 0mask
            (modulo x 16))))
  (vector-set! line pos (logand v m))))
	
(define (add-to-line line1 line2)
 (do ((i 0 (+ i 1)))
   ((= i (length line1)))
  (let ((v2 (v@ line2 i)))
   (if (not (= 0 v2))
  (vector-set! line1 i
   (+(v@ line1 i)
     v2))))))

(define (make-nc)
 (clear-n-tbl)
 (do ((y 0 (+ y 1)))
	  ((= y (length n-tbl)))
  (let ((nline (v@ n-tbl y)))
   (add-to-line nline(v@ l-tbl (wry (- y 1))))
   (add-to-line nline(v@ a-tbl (wry (- y 1))))
   (add-to-line nline(v@ r-tbl (wry (- y 1))))
   (add-to-line nline(v@ l-tbl y))
   (add-to-line nline(v@ r-tbl y))
   (add-to-line nline(v@ l-tbl (wry (+ y 1))))
   (add-to-line nline(v@ a-tbl (wry (+ y 1))))
   (add-to-line nline(v@ r-tbl (wry (+ y 1))))
   )))
	
(define (update)
	(make-nc)
	(do ((y 0 (+ y 1)))
   ((= y height))
		(do ((x 0 (+ x 1)))
		  ((= x width))
			(let ((n (getn x y)
       			)
							  (v (get-tbl a-tbl x y)))
				(t80::pix x y
				 (if v 0	(modulo (+ n seed) 16))) 
				(if v
				 (if (or (< n 2) (> n 3))
					  (begin
								(set0 a-tbl x y)
								(set0 l-tbl (wrx (- x 1)) y)
								(set0 r-tbl (wrx (+ x 1)) y)))
					(if (= n 3)
					  (begin
							 (set-cell x y)))
				)
   )
  )
 )
)

(define (set-cell x y)					
	(set1 a-tbl x y)
	(set1 l-tbl (wrx (- x 1)) y)
	(set1 r-tbl (wrx (+ x 1)) y))

(define (init)
 (do ((y 0 (+ y 1)))
    ((= y height))
   (do ((x 0 (+ x 1)))
      ((= x width))
     (if (not (= 0 (t80::pix x y)))
      (set-cell x y)))))
(define seed1 (modulo (t80::tstamp) 1000))
(define seed seed1)
(define (rnd n)
 (set! seed
  (modulo (+ 12345 (* seed 1103515245))
          2147483647))
  (modulo seed n))
          
(t80::cls)
(t80::spr 0
 (rnd width) (rnd height)
 0 1
 (rnd 4) (rnd 4)
 5 2)
(t80::spr 32
 (- (/ width 2) 24) (- (/ height 2) 16)
 0 1
 0 0
 7 4)

(init)
(define frames 0)
(define (TIC)
 (update)
	(set! frames (+ frames 1))
	(t80::print (string-append
	  "seed=" (number->string seed1)
			", gen=" (number->string frames))
			0 0 0)
	;; (t80::print (number->string (/ frames (t80::time))))
			
)
