Menu

Slow Conway Life

2017-06-30
2017-06-30
  • stan cartwright

    stan cartwright - 2017-06-30

    It's life Jim but not as we know it. ( ran faster on a zx81 :) )
    https://youtu.be/geaXNmXHyd4

    ;life...the slow version :)
    #chip mega328p,16
    #option explicit
    #include <glcd.h>
    ;
        #define GLCD_TYPE GLCD_TYPE_ILI9341
        'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
        #define GLCD_DC   portb.2 ;    DIGITAL_10         'HW Data command line DC
        #define GLCD_CS   portd.7 ;    DIGITAL_7          ' Chip select line CS
        #define GLCD_RESET   portd.4 ;      DIGITAL_4         ' Reset line Tie high..not needed
        #define GLCD_DO   portb.3 ;   DIGITAL 11          'HW Data out | MOSI SDI
        #define GLCD_SCK   portb.5 ;   DIGITAL_13          'HW Clock Line SCK
        #define ILI9341_HardwareSPI    ' remove/comment out if you want to use software SPI.still slow
        'GLCD selected extension font set. ASCII characters 31-254, the extended font uses 1358 bytes of program memory
        #define GLCD_EXTENDEDFONTSET1
        GLCDfntDefaultsize = 2
        GLCDRotate (landscape)
    ;
    dim ii ,tmp , pointer , cycle  as word
    dim d1 , rnd , readchar , printline as byte
    cycle = 0
    DIM w1(300) as byte ;15*20
    DIM n1(300) as byte
    dim p1(8) as byte
    for tmp = 1 to 300 ;fill arrays with spaces
      w1(tmp) = asc(" ")
      n1(tmp) = asc(" ")
    next  tmp
    ;
    for tmp = 1 to 50 ; 50 random cells to start
      rnd=random : rnd += 30; in 300 cells
      w1(rnd)=asc("O")
    next tmp
    ;
    glcdcls ILI9341_BLUE
    GLCDPrint (232,0,"CYCLE",ILI9341_SILVER)
    do
      FOR ii=22 TO (300-22)
        d1=0
        ; get 8 surrounding cells into p1 string
        p1(1)=w1(ii-1) ;1st cell
        p1(2)=w1(ii+1)
        p1(3)=w1(ii-21)
        p1(4)=w1(ii-20)
        p1(5)=w1(ii-19)
        p1(6)=w1(ii+19)
        p1(7)=w1(ii+20)
        p1(8)=w1(ii+21) ;p1 filled with surrounding cell values
        n1(ii)=w1(ii)
        FOR tmp = 1 TO 8 ;p1 is test occupied space around cell
          IF p1(tmp) = asc("O") THEN
          d1 ++
          end if
        NEXT tmp
    ;test cell state
        IF (w1(ii)=32) AND (d1=3) THEN
          n1(ii)=asc("O"): GOTO done
        end if
        IF (w1(ii)=asc("O")) AND (d1<2) THEN
          n1(ii)=32: GOTO done
        end if
        IF (w1(ii)=asc("O")) AND (d1>3) THEN
          n1(ii)=32
        end if
    done:
        NEXT ii ;tests done
    ;; print new generation
        pointer=1
        for tmp = 0 to 223 step 16 ;print 15 lines 0 to 223 ypos down screen :chars 16 pixels high
          for rnd = 1 to 189 step 10 ;print 20 w1(x) chars across screen
            GLCDPrint (rnd,tmp,chr(w1(pointer)),ILI9341_YELLOW)
            pointer ++ ;next char in w1(pointer)
          next rnd
        next tmp
    ;
        GLCDPrint (232,16,str(cycle),ILI9341_WHITE)
        cycle ++
    ;
        for tmp = 1 to 300 ;refresh w1()
          w1(tmp)=n1(tmp)
        next tmp
    loop
    
     
  • Anobium

    Anobium - 2017-06-30

    Note. The code in the posting may required a version of Great Cow BASIC greater than v.0.97.01. The array handler may need adapation in earlier versions of Great Cow BASIC, and early version of Great Cow BASIC did not support large arrays.

    Recommend v0.98.00 or greater.

     
  • Anobium

    Anobium - 2017-06-30

    Consider these changes.

    • asc("O") = this equates to 0x4F in ASCII code. Replacing all method calls to asc() with a constant assignment.
    • GLCDPrint is a string print. Use GLCDDrawChar as this will be faster for single characters.
    • Create a buffer array as a buffer and only print changes in the output buffer.
    • The IF - THEN tests could be optimised.

    :-)

     
  • stan cartwright

    stan cartwright - 2017-06-30

    It is a straight conversion of a ZX Spectrum program https://rosettacode.org/wiki/Conway%27s_Game_of_Life Unclear Basic is "unique". You can say Dim w$(300):Dim n$(300):w$=n$. It was easier to convert to GCB than say bbc basic.
    I've changed it so it wraps around one end of the array to the other ie top and bottom of screen wrap around.
    I'll try to speed the display up. Life ran at a few fps full screen on a zx81 in machine code.
    Compiler Version: 0.97.<<>> 2017-04-09 Program Memory: 8016 bytes RAM: 805/2048 bytes (39.31%) Chip: MEGA328P

     

    Last edit: stan cartwright 2017-06-30
  • stan cartwright

    stan cartwright - 2017-06-30

    Anobium, "Create a buffer array as a buffer and only print changes in the output buffer."
    Good idea. So I tried

    ; print new generation
        pointer=1
        for tmp = 0 to 223 step 16 ;print 111 lines 0 to 223 ypos down screen :chars 16 pixels high
          for rnd = 1 to 189 step 10 ;print 20 w1(x) chars across screen
            if w1(pointer) <> n1(pointer) then
                GLCDDrawChar (rnd,tmp,w1(pointer),ILI9341_YELLOW)
            end if
            pointer ++ ;next char in w1(pointer)
          next rnd
        next tmp
    

    but it didn't work, paper and pen says it should..only print cell in w1(xx) , old generation , if it's different to corresponding cell in n1(xx) , the new generation. The screen fills with "o"s after a while.
    There's a 1024 dim array in include ssd1306 glcd so large arrays must be ok in this .97 version.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.