;---------------------------------------
; WireworldV2 - Electronic simulatior

; See Scientific American, Jan 1990

; Conductior         =
; Electron head      #
; Electron tail      -

; Create using the normal screen editor
; & press return to start. Press the
; CBMkey to exit

; Created 13/1/1991 Chris Baird
;---------------------------------------

chrin    = $ffcf
store    = $c200
mem      = $c600
tran     = $ca00
indr1    = $fb
indr2    = $fd
count    = $ff
pass     = 671

;---------------------------------------
         *= $c000

         jsr $e544
         ldx #128     ;Enable key repeat
         stx 650

         ldy #0
loop1    lda store,y   ;Move last design
         sta $0400,y          ;to screen
         lda store+256,y
         sta $0500,y
         lda store+512,y
         sta $0600,y
         lda store+744,y
         sta $06e8,y
         iny
         bne loop1

         lda pass   ;Clear screen if 1st
         bne loop2  ;time exicuted
         stx pass
         jsr $e544

loop2    jsr $ffcf      ;Get design
         cmp #13
         bne loop2


         ldy #0
loop3    lda $0400,y      ;Save design &
         sta store,y      ;clr work area
         sta tran,y
         lda $0500,y
         sta store+256,y
         sta tran+256,y
         lda $0600,y
         sta store+512,y
         sta tran+512,y
         lda $06e8,y
         sta store+744,y
         sta tran+744,y
         iny
         bne loop3


main     jsr trans   ;Move screen to ref
         jsr calcmem ;Calc new from ref
         jsr memscrn ;Move new to screen

         lda $dc01   ;Check for exit
         cmp #223
         bne main

         jsr $e544
         lda #0      ;Return to shell
         sta 650
         clc
         rts

;---------------------------------------

trans    lda #<mem    ;Convert design to
         sta indr1      ;internal format
         lda #>mem
         sta indr1+1
         ldy #0
         sty indr2
         lda #4
         sta indr2+1
         ldx #4
         stx count

tran1    lda (indr2),y
         ldx #0

         cmp #45     ; "-" = 1
         bne tran2
         ldx #1
         bne trana

tran2    cmp #61     ; "=" = 2
         bne tran3
         ldx #2
         bne trana

tran3    cmp #35     ; "#" = 16
         bne trana
         ldx #16

trana    txa
         sta (indr1),y
         iny
         bne tran1
         inc indr1+1
         inc indr2+1
         dec count
         bne tran1
         rts

;---------------------------------------

calcmem  lda #<mem+41
         sta indr1
         lda #>mem+41
         sta indr1+1
         lda #<tran+41
         sta indr2
         lda #>tran+41
         sta indr2+1
         ldy #41


calc1    lda indr1
         cmp #<mem+917
         bne calc2
         lda indr1+1
         cmp #>mem+917
         bne calc2
         rts

  ;Calc next state of a cell
  ;CA rules:  -  =>  =
           ;  #  =>  -
           ;  =  => # if 1 or 2 #'s near
           ;else => =


calc2    ldx #32
         lda (indr1),y
         beq calc3

         cmp #1     ; = => =
         bne *+6
         ldx #61
         bne calc3

         cmp #16    ; # => -
         bne *+6
         ldx #45
         bne calc3

         ldx #61    ;Add neigbours
         clc           ;background=0
         lda #0        ;conductior=1
         tay           ;tail=2
         adc (indr1),y ;head=16
         iny
         adc (indr1),y
         iny
         adc (indr1),y
         ldy #40
         adc (indr1),y
         ldy #42
         adc (indr1),y
         ldy #80
         adc (indr1),y
         iny
         adc (indr1),y
         iny
         adc (indr1),y
         ldy #41

         cmp #16        ;if 15<total<48
         bcc calc3      ;becomes a head
         cmp #48        ;else conductior
         bcs calc3
         ldx #35

calc3    txa
         sta (indr2),y
         inc indr2
         inc indr1
         bne calc1
         inc indr1+1
         inc indr2+1
         bne calc1

;---------------------------------------

memscrn  ldy #0
mem1     lda tran,y
         sta $0400,y
         lda tran+256,y
         sta $0500,y
         lda tran+512,y
         sta $0600,y
         lda tran+744,y
         sta $06e8,y
         iny
         bne mem1
         rts

;---------------------------------------


