Forest Fire

The Forest-fire model is described on wikipedia.
The following is an implementation of Forest Fire for the 6502.

Load this 574 byte program: forestfire.prg (576 bytes)

To run it on the Commodore 64:
SYS 17239
To run it on the Apple II+:
CALL 17239

or paste the following into your Apple II emulator.

FP
CALL -151
4357:
:20 A6 44 85 2C A9 4C 85 28 A2 0 8A 20 28 0 A5 26 9D 0 50 A5 27
:9D 19 50 A0 0 98 91 26 C8 C0 28 D0 F9 E8 E4 2C D0 E3 20 52 44 20
:5B 44 A2 60 86 2F 86 31 A0 0 84 30 98 91 30 C8 D0 FB E8 86 31 E0
:80 D0 F4 A2 0 86 30 A9 29 85 32 85 36 A9 53 85 34 A5 2F 85 31 85
:33 85 35 49 10 85 37 85 2F 8A 86 2C 4A A8 A9 F 90 2 69 E0 85 2D
:B9 0 50 85 26 B9 19 50 85 27 A0 1 84 2B B1 32 91 36 F0 46 10 53
:A9 0 A4 2B 91 36 88 51 26 25 2D 51 26 91 26 A4 2B C8 C0 29 D0 E0
:A5 32 85 30 A5 33 85 31 A5 34 85 32 85 36 18 69 2A 85 34 A5 35 49
:10 85 37 49 10 85 33 69 0 85 35 A6 2C E8 E0 30 D0 9F 20 85 44 4C
:9C 43 C6 28 D0 C7 20 5B 44 A9 44 D0 B3 A9 99 D0 AF C6 29 D0 9 C6
:2A D0 5 20 52 44 D0 EF B1 30 11 34 88 11 30 11 32 11 34 C8 C8 11
:30 11 32 11 34 30 DA 10 98 A9 17 85 29 A9 27 85 2A 60 A9 65 85 28
:60 A9 6B 85 29 A9 44 85 2A A9 19 60 A0 0 84 27 A A A 85 26 A
:26 27 A 26 27 65 26 85 26 A5 27 69 4 85 27 60 AD 0 E0 C9 4C D0
:44 2C 0 C0 10 14 2C 10 C0 2C 51 C0 68 68 A2 26 BD 0 51 95 0 E8
:E0 38 D0 F6 60 A2 26 B5 0 9D 0 51 E8 E0 38 D0 F6 AD 0 E0 C9 4C
:D0 32 2C 56 C0 2C 52 C0 2C 54 C0 2C 50 C0 A9 47 85 29 A9 F8 85 2A
:A9 18 60 C9 85 D0 D1 A5 C6 F0 CD A9 0 85 C6 AD 16 D0 29 EF 8D 16
:D0 A9 15 8D 18 D0 D0 AC C9 85 F0 3 4C 60 44 A9 0 8D 20 D0 A9 0
:8D 21 D0 A9 5 8D 22 D0 A9 8 8D 23 D0 AD 16 D0 9 10 8D 16 D0 A9
:30 85 37 4A 4A 85 2D AD 18 D0 29 F0 5 2D 8D 18 D0 A9 0 85 36 AA
:20 83 45 A9 20 85 36 A9 55 20 83 45 A9 48 85 36 A9 AA 20 83 45 A5
:37 18 69 2 85 37 A2 0 86 36 A9 0 A2 55 20 83 45 A9 20 85 36 8A
:20 83 45 A9 48 85 36 A9 AA 20 83 45 A5 37 18 69 2 85 37 A9 80 85
:36 A9 0 A2 AA 20 83 45 A9 A0 85 36 A9 55 20 83 45 A9 C8 85 36 8A
:20 83 45 4C 60 44 A0 0 91 36 C8 C0 4 D0 F9 8A 91 36 C8 C0 8 D0
:F9 60
4357G

forestfire.s 6502 Assembly of Forest Fire.
	ORG	$4357
; SYS 17239 or CALL 17239

EMPTY2	=	$00
TREE2	=	$44
FIRE2	=	$99

; common available zero page


GBASL	=	$26
GBASH	=	$27

SEED2	=	$28
SEED0	=	$29
SEED1	=	$2A

H2	=	$2B
V2	=	$2C
PLOTC	=	$2D
COLOR	=	$2E
PAGE	=	$2F
TOPL	=	$30
TOPH	=	$31
MIDL	=	$32
MIDH	=	$33
BTML	=	$34
BTMH	=	$35
PLOTL	=	$36
PLOTH	=	$37
lastzp	=	$38

tablelo	=	$5000
tablehi = tablelo+25

	JSR START
	STA V2
	LDA #$4C ; JMP instruction
	STA SEED2 ; temporary JMP
	LDX #$00 ; y coord
table:
	TXA
	JSR SEED2 ; temporary JMP GBASCALC
	LDA GBASL
	STA tablelo,X
	LDA GBASH
	STA tablehi,X
	LDY #$00
	TYA
clrline:
	STA (GBASL),Y
	INY
	CPY #40
	BNE clrline

	INX
	CPX V2
	BNE table

	JSR sseed0
	JSR sseed2

	LDX #$60
	STX PAGE
	STX TOPH
	LDY #$00
	STY TOPL
	TYA
zero:	STA (TOPL),Y
	INY
	BNE zero
	INX
	STX TOPH
	CPX #$80
	BNE zero

loop3:
	LDX #0
	STX TOPL
	LDA #41
	STA MIDL
	STA PLOTL
	LDA #83
	STA BTML
	LDA PAGE
	STA TOPH
	STA MIDH
	STA BTMH
	EOR #$10
	STA PLOTH
	STA PAGE
loop2:
	TXA
	STX V2
	LSR       ; F800 PLOT-like...
	; PHP       ; F801
	TAY	 ; save A in Y without touching C
	LDA #$0F
	BCC over2
	ADC #$E0
over2:	STA PLOTC   ; PLOT...
	LDA tablelo,Y ; lookup instead of GBASCALC
	STA GBASL
	LDA tablehi,Y
	STA GBASH
	; PLP     ; continue PLOT
	LDY #$01 ; x coord
loop1:
	STY H2
	LDA (MIDL),Y
	STA (PLOTL),Y
	BEQ empty
	BPL tree
	LDA #EMPTY2
doplot:	LDY H2
	STA (PLOTL),Y
	DEY
	EOR (GBASL),Y
	AND PLOTC
	EOR (GBASL),Y
	STA (GBASL),Y
noplot: 
	LDY H2
	INY
	CPY #41
	BNE loop1
	LDA MIDL
	STA TOPL
	LDA MIDH
	STA TOPH
	LDA BTML
	STA MIDL
	STA PLOTL
	CLC
	ADC #42
	STA BTML
	LDA BTMH
	EOR #$10
	STA PLOTH
	EOR #$10		
	STA MIDH
	ADC #$00
	STA BTMH
	LDX V2
	INX
	CPX #48
	BNE loop2
	JSR QUIT
	JMP loop3
empty:
	DEC SEED2
	BNE noplot
	JSR sseed2 ; probability f
	LDA #TREE2
	BNE doplot
ignite:
	LDA #FIRE2
	BNE doplot
tree:
	DEC SEED0
	BNE check
	DEC SEED1
	BNE check
	JSR sseed0 ; probability p
	BNE ignite
check:
	LDA (TOPL),Y ; n
	ORA (BTML),Y ; s
	DEY
	ORA (TOPL),Y ; nw
	ORA (MIDL),Y ; w
	ORA (BTML),Y ; sw
	INY
	INY
	ORA (TOPL),Y ; ne
	ORA (MIDL),Y ; e
	ORA (BTML),Y ; se
	BMI ignite
	BPL noplot

sseed0:
	LDA #$17 ; 1 in 10007 (prime)
	STA SEED0
	LDA #$27
	STA SEED1
	RTS
sseed2:
	LDA #$65  ; 1 in 101 (prime)
	STA SEED2
	RTS

default:
	LDA #<GBASCALC ; setup GBASCALC
	STA SEED0
	LDA #>GBASCALC
	STA SEED1
	LDA #25 ; screen rows
	RTS
GBASCALC:
	LDY #$00
	STY GBASH		
	ASL
	ASL
	ASL
	STA GBASL
	ASL
	ROL GBASH
	ASL
	ROL GBASH
	ADC GBASL
	STA GBASL
	LDA GBASH
	ADC #$04
	STA GBASH
	RTS

QUIT:
	LDA $E000

; APPLE II

	CMP #$4C
	BNE c64quit

	BIT $C000 ; apple ii keypress?
	BPL CONTINUE ; no keypressed then continue
	BIT $C010 ; clear keyboard strobe
	BIT $C051 ; text mode

; end APPLE II specific

ABORT:
	PLA
	PLA

	LDX #GBASL
restorzp:
	LDA $5100,X
	STA $00,X
	INX
	CPX #lastzp
	BNE restorzp

CONTINUE:
	RTS

START:
	LDX #GBASL
savezp:
	LDA $00,X
	STA $5100,X
	INX
	CPX #lastzp
	BNE savezp

; machine ???

	LDA $E000 ; terribly unreliable, oh well

; APPLE II

	CMP #$4C ; apple ii?
	BNE c64start ; nope, try another

	BIT $C056 ; low resolution
	BIT $C052 ; full screen
	BIT $C054 ; page one
	BIT $C050 ; graphics
; GBASCALC = $F847
	LDA #$47
	STA SEED0
	LDA #$F8
	STA SEED1
	LDA #24 ; screen rows
	RTS

; end APPLE II specific

; COMMODORE 64 specific

c64quit:

; COMMODORE 64

	CMP #$85 ; commodore 64?
	BNE CONTINUE ; nope, default to no keypress

	LDA $C6 ; commodore keyboard buffer length
	BEQ CONTINUE ; no keypressed then continue

	LDA #$00
	STA $C6
	LDA $D016 ; Screen control register #2
	AND #$EF  ; Bit #4: 0 = Multicolor mode off. 
	STA $D016
	LDA #21 ; default character set
	STA $D018
	BNE ABORT

c64start:

	CMP #$85 ; commodore 64?
	BEQ c64yes ; yes
	JMP default ; no, default to boringness
c64yes:
	LDA #$00  ; black
	STA $D020 ; border
	LDA #$00  ; black
	STA $D021 ; background
	LDA #$05  ; dark green
	STA $D022 ; Extra background color #1
	LDA #$08  ; orange
	STA $D023 ; Extra background color #2
	LDA $D016 ; Screen control register #2
	ORA #$10  ; Bit #4: 1 = Multicolor mode on. 
	STA $D016

	LDA #$30  ; 0011 0000 $3000 charset page
	STA PLOTH
	LSR
	LSR
	STA PLOTC ; 0000 1100 #$0C
; 53272 $D018
; POKE 53272,(PEEK(53272)AND240)+12: REM SET CHAR POINTER TO MEM. 12288
;     Bits #1-#3: In text mode, pointer to character memory
;    (bits #11-#13), relative to VIC bank, memory address $DD00
;     %110, 6: $3000-$37FF, 12288-14335.
	LDA $D018
	AND #$F0
	ORA PLOTC
	STA $D018
; setup nine characters

; 00- 00 00
	LDA #$00 ; chr(0) * 8
	STA PLOTL
	; --- LDA #$00 ; already zero
	TAX ; LDX #$00
	JSR charset

; 04- 00 55
	LDA #32 ; chr(4) * 8
	STA PLOTL
	LDA #$55
	; LDX #$00 ; already zero
	JSR charset

; 09- 00 AA
	LDA #72 ; chr(9) * 8
	STA PLOTL
	LDA #$AA
	; LDX #$00 ; already zero
	JSR charset

; 40- 55 00
	LDA PLOTH ; 512 = chr(64) * 8
	CLC
	ADC #$02
	STA PLOTH
	LDX #$00
	STX PLOTL
	LDA #$00
	LDX #$55
	JSR charset

; 44- 55 55
	LDA #32 ; chr(68) * 8
	STA PLOTL
	TXA ; LDA #$55
	; LDX #$55 ; already 55
	JSR charset

; 49- 55 AA
	LDA #72 ; chr(73) * 8
	STA PLOTL
	LDA #$AA
	; LDX #$55 ; already 55
	JSR charset

; 90- AA 00
	LDA PLOTH ; chr(144) * 8
	CLC
	ADC #$02
	STA PLOTH
	LDA #128
	STA PLOTL
	LDA #$00
	LDX #$AA
	JSR charset

; 94- AA 55
	LDA #160 ; chr(148) * 8
	STA PLOTL
	LDA #$55
	; LDX #$AA ; already AA
	JSR charset

; 99- AA AA
	LDA #200 ; chr(153) * 8
	STA PLOTL
	TXA ; LDA #$AA
	; LDX #$AA ; already AA
	JSR charset
	JMP default
charset:
	LDY #$00
chartop:
	STA (PLOTL),Y
	INY
	CPY #$04
	BNE chartop
	TXA
charbtm:
	STA (PLOTL),Y
	INY
	CPY #$08
	BNE charbtm
	RTS

; end COMMODORE 64 specific

usenet comp.sys.apple2.programmer (macgui.com)
    Re: Book: Apple II-6502 Assembly Language Tutor