I've written a small parrot program which implements a simple deterministic
cellular automata of the kind described in Stephen Wolfram's new book,
A New Kind of Science.

Code is attached.  You can also find it along with examples of output
at http://andywardley.com/parrot/automata.html

A


#------------------------------------------------------------------------
# 
# automata.pasm
#
# Parrot assembly program to implement a cellular automata of the form
# described in Stephen Wolfram's book "A New Kind of Science", Wolfram 
# Media Inc., 2002, ISBN 1-57955-008-8, http:://www.wolframscience.com/
#
# Written by Andy Wardley <[EMAIL PROTECTED]>
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as Perl and/or Parrot.
#
#------------------------------------------------------------------------

        # -- configurable options --

        # I0 specifies the automata rule as an integer from 0 to 255.
        # Some interesting rules are: 45, 57, 75, 86, 105, 109, 110, 
        # 129, 131, 150, 154, 182, 225
        set I0, 154

        # I1 specifies the line width (x)
        # I2 the number of iterations (y)
        set I1, 78
        set I2, 35

        # for wide-terminal users
        set I1, 150
        set I2, 60

        # I3 defines the seed cell position which is, by default, the 
        # middle of the row (I1 / 2).  You might prefer to set it 
        # manually, e.g. to 77 (the last cell, or I1 - 1) for rules
        # like 110 that only expand to the left
        div I3, I1, 2

        # -- end of configurable options --

        bsr init
loop:   bsr display
        bsr iterate
        dec I2
        if I2, loop
        end


#------------------------------------------------------------------------
# init
#
# Initialise P0 to be hold cells, P1 to define evolution rules.
#------------------------------------------------------------------------

init:
        # define new array of line width
        new P0, .Array
        set P0, I1

        # iterate over array to initialise
        set I10, 0
initloop:
        # at seed point?
        eq I10, I3, initon
initoff:
        set P0, I10, 0
        branch initnext
initon: 
        set P0, I10, 1
initnext:
        inc I10
        ne I10, I1, initloop

        # For the 3 parent cells that we examine, there are 8 possible
        # states: 000, 001, 010, 011, 100, 101, 110, 111.  Each bit of
        # the automata code number set in I0 indicates the state of the
        # next-generation cell for each of those states.  We extract
        # these into the array P1
        new P1, .Array
        set P1, 8
        set I10, 0      # loop iterator
        set I11, 1      # bitmap

maploop:
        and I12, I0, I11
        set P1, I10, I12
        inc I10
        mul I11, I11, 2
        lt I10, 8, maploop

        ret


#------------------------------------------------------------------------
# iterate
#
# Perform an iteration of the cellular automata
#------------------------------------------------------------------------

iterate:
        set I10, 0        # index of previous item
        set I11, 1        # index of current item
        set I12, 2        # index of next item
        set I20, P0, 1    # left parent, dup cell 1 first time round
        set I21, P0, 1    # above parent

iterloop:
        set I22, P0, I12  # right parent
        # duplicate penultimate cell for last cell
        set I13, I12
        inc I13
        ne I13, I1, iteron
        set I13, I12
        dec I13
        set I22, P0, I13
iteron:
        # calculate function lookup number from parents
        # is there an easier way to do this?
        set I13, 0
bit1:   unless I22, bit2
        set I13, 1
bit2:   unless I21, bit3
        add I13, I13, 2
bit3:   unless I20, bitend
        add I13, I13, 4
bitend: set I14, P1, I13
        if I14, iterblack
        branch, iterwhite
iterblack:
        set I14, 1
        branch itermain
iterwhite:
        set I14, 0
itermain:
        set I20, I21
        set I21, I22
        set P0, I11, I14
        set I10, I11
        inc I11
        inc I12
        lt I12, I1, iterloop
        ret


#------------------------------------------------------------------------
# display
#
# Display the current state of the cellular automata.
#------------------------------------------------------------------------

display: 
        set I10, 0
disploop:
        set I11, P0, I10
        if I11, dispon
dispoff:
        print " "
        branch dispnext
dispon:
        print "+"
dispnext:
        inc I10
        ne I10, I1, disploop
        print "\n"
        ret     



Reply via email to