Here is a new version of my CONWAY_DPF file for superForth, sightly modified to add a word "bench" to compare runtimes with or without plotting :
Code: Select all
: .< 0 <# #S #> TYPE ;
: >< ( n,a,b --- [a<=n<=b] ) >R OVER <= SWAP R> <= AND ;
CR CR .( Conway's life in a )
120 DUP .< .( x) DUP CONSTANT ww ( world width )
85 DUP . .( torus shaped world) DUP CONSTANT wh ( height )
* DUP 2/ CREATE wm ALLOT ( world map of nibbles 4bits/cell )
2* CREATE ws HERE >R ALLOT HERE R> CONSTANT da CONSTANT ma
VARIABLE ds VARIABLE ms ( DAD and MOM stack pointers ) HEX
CREATE ss F0F , F0F0 , 70 , 7 , 7000 , 700 , 7070 , 707 ,
CREATE ni A0A , 404 , 506 , 708 , 80A , B0C , D0D ,
CREATE nr 0 , 101 , 101 , 303 , 100 , 0 , 200 , CR DECIMAL
: sd ( color,x,y --- )
384 * SWAP 4 + 2 /MOD 2* ROT +
DUP >R 2 A@ OVER 2* ss + @ AND
ROT DUP
IF
2* ROT + 2* ss + @ OR
ELSE
DROP SWAP DROP
THEN
DUP R@ 2 A!
R> 128 + 2 A!
;
VARIABLE goal
: cs ( clear DAD and MOM stacks ) da ds ! ma ms ! ;
: >d ds @ ! 2 ds +! ; ( DAD push : any first added next-door )
: >m -2 ms +! ms @ ! ; ( MOM push : any born/surviving/killed )
: <d ds @ da = IF -1 ELSE -2 ds +! ds @ @ THEN ; ( DAD pop )
: <m ms @ ma = IF -1 ELSE ms @ @ 2 ms +! THEN ; ( MOM pop )
: xy> ( x,y --- m ) ww * + ;
: xy< ( m --- x,y ) ww /MOD ;
: n@ ( m --- n )
2 /MOD wm + C@ SWAP
IF
16 /
ELSE
15 AND
THEN ;
: n! ( n,m --- )
2 /MOD wm + DUP >R C@ SWAP
IF
15 AND SWAP 16 * OR
ELSE
240 AND OR
THEN
R> C!
;
: dad
BEGIN
<m ( pops MOM )
DUP -1 <>
WHILE
DUP n@ 2 8 ><
IF
2 -1 DO
2 -1 DO
DUP
xy<
I J ROT
+ wh MOD >R
+ ww MOD R>
xy>
DUP >R
n@ DUP 4 <
IF
R@ >d
THEN
ni + C@
R> n!
LOOP
LOOP
DROP
ELSE
DUP n@ 1 =
IF
9 OVER n! >d
ELSE
DROP
THEN
THEN
REPEAT
DROP
;
: mom
BEGIN
<d ( pops DAD )
DUP -1 <>
WHILE
DUP DUP n@
nr + C@
DUP ROT xy<
( sd )
goal @ IF sd ELSE DROP DROP DROP THEN
DUP
IF
OVER >m
THEN
SWAP n!
REPEAT
DROP
;
: LIFE
BEGIN
dad mom
1 KEYROW 64 =
UNTIL
;
: FLU
CLS
cs
ww wh * 2/ 0
DO
0 I wm + C!
LOOP
;
: LAY ( x,y --- )
xy> DUP >m
2 OVER n!
2 SWAP xy< sd
;
.( Creating incrementally complex organisms )
VARIABLE components
VARIABLE flag 0 flag !
VARIABLE A_C
: S1 SWAP ; : S7 NEGATE ; : S2 NEGATE S1 ; : S6 SWAP S7 ;
: S3 SWAP S2 ; : S5 NEGATE S6 ; : S4 NEGATE S3 ; : SO ;
CREATE S ' SO , ' S1 , ' S2 , ' S3 , ' S4 , ' S5 , ' S6 , ' S7 ,
CREATE g8
0 C, 1 C, 2 C, 3 C, 4 C, 5 C, 6 C, 7 C,
1 C, 0 C, 7 C, 6 C, 5 C, 4 C, 3 C, 2 C,
2 C, 3 C, 4 C, 5 C, 6 C, 7 C, 0 C, 1 C,
3 C, 2 C, 1 C, 0 C, 7 C, 6 C, 5 C, 4 C,
4 C, 5 C, 6 C, 7 C, 0 C, 1 C, 2 C, 3 C,
5 C, 4 C, 3 C, 2 C, 1 C, 0 C, 7 C, 6 C,
6 C, 7 C, 0 C, 1 C, 2 C, 3 C, 4 C, 5 C,
7 C, 6 C, 5 C, 4 C, 3 C, 2 C, 1 C, 0 C,
: *g8 3 PICK 8 * + g8 + C@ ;
: bio+ 2- , C, C, C, 5 components @ +! ;
: ;BIO 0 flag ! ;
: c [ HERE A_C ! ]
flag @
IF
A_C @ bio+
ELSE
DROP LAY
THEN
;
: BIO:
CREATE HERE components ! 0 , -1 flag !
DOES> flag @
IF
bio+
ELSE
DUP @ OVER + 2+ SWAP 2+
DO
I 4 + C@
I 3 + C@
I 2 + C@
*g8 >R
2 PICK 2* S + @ EXECUTE
3 PICK + SWAP
4 PICK + SWAP R> I @ EXECUTE
5 +LOOP
DROP DROP DROP
THEN
;
BIO: g 0 0 0 c 1 0 0 c 2 0 0 c 2 1 0 c 1 2 0 c ;BIO
BIO: r 0 0 0 c 1 0 0 c 1 1 0 c 2 1 0 c 0 2 0 c ;BIO
BIO: b 0 20 6 g 4 12 4 g 8 18 6 r 9 8 1 g 14 7 1 r
20 6 1 g 22 20 6 r 23 10 1 g 28 9 1 r 30 0 4 g 34 22 6 g
38 14 4 g 39 3 1 g ;BIO : demo FLU 70 30 2 b LIFE ;
BIO: l5 0 0 0 c 1 0 0 c 2 0 0 c 3 0 0 c 4 0 0 c ;BIO
BIO: 7l5 0 0 0 l5 6 0 0 l5 12 0 0 l5 18 0 0 l5 24 0 0 l5
30 0 0 l5 36 0 0 l5 ;BIO : joli FLU 37 42 0 7l5 LIFE ;
BIO: m1 0 0 0 c 0 1 0 c 1 2 0 c 2 2 0 c 3 2 0 c ;BIO
: li 20 0 DO I 6 * 0 0 l5 LOOP ;
BIO: lin li ;BIO
: line FLU 0 42 0 lin ;
: ligne line LIFE ;
: alien FLU 70 30 2 b ;
: bench
DUP goal ! TIME ROT 0
DO
dad mom
1 KEYROW 64 =
IF
I 1+ . CR LEAVE
THEN
LOOP
TIME D- D. CR
;
END_FILE
The word that does the plotting is the first after the table declarations : it is called "sd" and requires 3 parameters.
This word is only used once by the word "mom". I declared also a new variable called "goal" to be used with "bench".
Instead of executing "sd" unconditionally, "mom" tests "goal", and if zero, does not plot !
at the end, I added indeed the word "bench" and also two initial configuration of life called "line" and "alien" to measure with "bench" the runtimes with and without plotting. So, after starting superForth :
LOAD_FILE mdv2_conway_dpf (LOAD_FILE must be uppercase, superForth is case sensitive ; mdv2 just fo example)
Then first run without plotting :
line 0 bench
must be stopped after some time (try first a few seconds) with the space bar.
Apart the initial life nothing is plotted, but after stopping you will see the number <n> of generations passed and the time it took in seconds.
Then running again with <n> as parameter instead of zero :
line <n> bench
you just have to wait until it stops with the same number of generations passed WITH plotting. Ouput : the time it took. if you are impatient you may also stop it with the space bar.
Have fun !