1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
; Diffusion Limited Aggregation
; B. Watson's asm rewrite of ChrisTOS's Atari 8-bit version.
; Original lives here: https://github.com/ctzio/DLA/
; This version uses ANTIC narrow playfield mode, since the original
; uses fewer than 256 columns of a GR.8 screen. This gives a slight
; speed boost for 2 reasons: less DMA from the ANTIC chip, and we get
; to use 1 byte for the X coordinate.
.include "dlaver.inc"
.include "atari.inc"
.include "xex.inc"
.include "nextpage.inc"
loadaddr = $2000
lowcode = $0600 ; memcheck and io.s
screen = $4000 ; must be on a x000 (4K) boundary
screen2 = screen + $1000 ; rest of screen RAM after 4K boundary
pixarray = screen + $20
linelen = $20 ; aka 32 bytes, antic F (GR.8) in narrow mode.
maxlines = $aa ; 170 lines of display
screenbytes = maxlines * linelen
dl_len = 202 ; remember to update this if you modify the display list!
dlist = screen - dl_len
DMA_ON = $21
DEFAULTPART = 1000
linebuf = $0580
textbuf = $0590
.bss
.org $80
maxparticles: .res 2 ; user's response to "How many particles?"
seedtype: .res 1 ; user's response to seed type prompt (minus one; 0-3)
pixptr: .res 2 ; used by plot and drunkwalk
pixmask: .res 1 ; used by render
cursor_x: .res 1 ; cursor x/y are args to plot and drunkwalk
cursor_y: .res 1
pixptr2: .res 2 ; used by drunkwalk
screenptr = pixptr2 ; used by render
circlesize: .res 1 ; 0 to 3
particles: .res 2
spawn_x: .res 2
spawn_y: .res 2
old_dma: .res 1 ; these 3 are for restoring GR.0 mode
old_dl: .res 2
old_savmsc: .res 2
cloksav: .res 3 ; hold RTCLOK here while we convert to MM:SS.CC
fptmp: .res 6 ; used in mmss.s
.code
;;; init xex segment: check that there's enough memory.
xex_org lowcode
.include "io.s" ; printchrx and getchrx
.include "printint.s"
memcheck:
lda RAMTOP
cmp #$c0 ; 48K
bcs mem_ok
lda #<memmsg
ldx #>memmsg
jsr printmsg
lda RAMTOP ; free RAM in pages, divide by 4 to get K
lsr
lsr
jsr printdecb
lda #<memmsg2
ldx #>memmsg2
jsr printmsg
jsr getchr ; wait for user to press a key
jmp WARMSV ; get outta here (simulate Reset keypress)
mem_ok:
rts
memmsg: .byte "Need 48K, only have ",0
memmsg2: .byte "K.",$9b,"Disable BASIC, remove cartridge?",$9b
.byte "Press any key...",0
xex_init memcheck
;;; main xex segment: the actual program. the functions in io.s
;;; and printint.s are still in low memory.
xex_org loadaddr
.include "mmss.s"
; init stuff gets done once, at startup
init:
lda #0
sta seedtype ; default seed type is the single pixel
sta LMARGN
lda SDMCTL
sta old_dma
lda SDLSTL
sta old_dl
lda SDLSTH
sta old_dl+1
lda SAVMSC
sta old_savmsc
lda SAVMSC+1
sta old_savmsc+1
; set default particles (if user just hits return)
lda #<DEFAULTPART
sta maxparticles
lda #>DEFAULTPART
sta maxparticles+1
; end of init stuff.
; "New" option jumps here, restore GR.0 screen
getargs:
jsr restore_gr0
; print banner and prompt.
printbanner:
lda #<banner
ldx #>banner
jsr printmsg
lda maxparticles
ldx maxparticles+1
jsr printdecw
lda #<partprompt
ldx #>partprompt
jsr printmsg
; use CIO to read input, so user can use backspace/etc.
jsr readline
lda linebuf
cmp #$9b
beq usedefault ; if user hit Return
; use floating point ROM to convert input to an integer.
lda #0
sta CIX
jsr AFP ; ASCII to floating point, result in FR0
bcs printbanner ; C set means error (negative or >65535)
jsr FPI ; convert FR0 to integer (result in FR0)
bcs printbanner
lda FR0
tax
ora FR0+1 ; we don't accept 0 for an answer!
beq printbanner
lda FR0+1
stx maxparticles
sta maxparticles+1
usedefault:
; print seed type prompt
lda #<seedprompt
ldx #>seedprompt
jsr printmsg
ldx seedtype
inx
txa
jsr printdecb
lda #<seedprompt2
ldx #>seedprompt2
jsr printmsg
readseed:
jsr getchrx
cmp #$9b
beq dfltseed ; use default if user pressed return
cmp #$31
bcc readseed
cmp #$35
bcs readseed
and #$0f
tax
dex
stx seedtype
dfltseed:
lda #$9b
jsr printchr
generate: ;;; start of generate()
jsr initscreen
; wait for shadow regs to get updated...
lda RTCLOK+2
wl:
cmp RTCLOK+2
beq wl
lda #1 ; ...turn off shadow reg updates (tiny speed boost)
sta CRITIC
lda #0
sta DMACTL
sta particles
sta particles+1
sta RTCLOK
sta RTCLOK+1
sta RTCLOK+2
sta circlesize
jsr set_limits
lda #<points_x
sta spawn_x
lda #>points_x
sta spawn_x+1
lda #<points_y
sta spawn_y
lda #>points_y
sta spawn_y+1
jsr drawseed
next_particle:
jsr drunkwalk ; spawn, walk around, plot (all in one)
inc particles
bne ph_ok
inc particles+1
ph_ok:
; increase circlesize at appropriate particle counts
; if(particles == 100 || particles == 300 || particles == 600) goto next_size;
lda particles
ldx particles+1
bne not_100
cmp #100
beq next_size
not_100:
cpx #>300
bne not_300
cmp #<300
beq next_size
not_300:
cpx #>600
bne checkmaxparts
cmp #<600
beq next_size
bne checkmaxparts
next_size:
inc circlesize
jsr set_limits
inc spawn_x+1
inc spawn_y+1
checkmaxparts:
; if(particles != maxparticles) goto next_particle;
lda particles
cmp maxparticles
bne next_particle
lda particles+1
cmp maxparticles+1
bne next_particle
main_done:
jsr render
lda #0
sta CRITIC
sta COLOR2
sta ATRACT
lda #DMA_ON
sta SDMCTL
lda RTCLOK
sta cloksav
lda RTCLOK+1
sta cloksav+1
lda RTCLOK+2
sta cloksav+2
; print elapsed time. see mmss.s for gory details.
jsr print_mmss
; print menu, wait for keystroke.
lda #<menumsg
ldx #>menumsg
jsr printmsg
; user might have hit some random key during plotting; ignore it.
ldx #$ff
stx CH
waitkey:
jsr getchr
; see what key was hit
and #$5f ; ignore case
cmp #'R' ; Redo
bne notredo
jmp generate
notredo:
cmp #'S' ; Save
beq saveimage
cmp #'N' ; New
bne waitkey ; ignore any other keystroke
jmp getargs
;;; End of generate()
;;; Subroutine: saveimage
;;; Does exactly what it says on the tin: saves the image.
;;; User is prompted for a filename. In case of disk error,
;;; there's a "Retry [Y/n]?" prompt.
saveimage:
jsr restore_gr0 ; back to GR.0, so we can...
lda #<saveprompt
ldx #>saveprompt
jsr printmsg ; ...prompt for, and...
jsr readline ; ...let the user type a filename.
jsr close1 ; make sure the IOCB is closed before we open it!
; prepend D: to the filename, if there's no device given.
lda linebuf+1
cmp #':'
beq open_output
lda linebuf+2
cmp #':'
beq open_output
ldy ICBLL
dloop:
lda linebuf,y
sta linebuf+2,y ; make for for D:
dey
bpl dloop
; insert D: and add 2 to the buffer length.
lda #'D'
sta linebuf
lda #':'
sta linebuf+1
inc ICBLL
inc ICBLL
open_output:
; CIO is nice, but it's kind of a PITA to use...
; OPEN #1,8,0,<filename>
ldx #$10
lda #3 ; OPEN
sta ICCOM,x
lda #<linebuf
sta ICBAL,x
lda #>linebuf
sta ICBAH,x
ldy ICBLL ; length returned by CIO when we called readline...
dey ; ...but one byte too long (due to the EOL).
tya
sta ICBLL,x
lda #0
sta ICBLH,x
sta ICAX2,x
lda #8
sta ICAX1,x
jsr CIOV
cpy #1 ; CIO returns with Y=1 for success.
bne save_error
; write data to the channel
ldx #$10
lda #$0b ; write binary record
sta ICCOM,x
lda #<screen
sta ICBAL,x
lda #>screen
sta ICBAH,x
lda #<screenbytes
sta ICBLL,x
lda #>screenbytes
sta ICBLH,x
jsr CIOV
cpy #1
bne save_error
; close the channel
jsr close1
cpy #1
bne save_error
; let the user know the save worked.
lda #<saveokmsg
ldx #>saveokmsg
jsr printmsg
jsr getchr ; any key
jmp getargs
; print error message, prompt for retry.
save_error:
tya
pha
lda #<diskerrmsg
ldx #>diskerrmsg
jsr printmsg
pla
jsr printdecb
lda #<retrymsg
ldx #>retrymsg
jsr printmsg
get_retry_key:
jsr getchr
cmp #$9b ; return = yes
beq jsaveimage
and #$5f ; ignore case
cmp #'Y'
beq jsaveimage
cmp #'N'
bne get_retry_key
jmp getargs
jsaveimage:
lda #$9b
jsr printchr
jmp saveimage
;;; Subroutine: set_limits
;;; Sets the X/Y min/max limits based on circlesize.
;;; Preserves Y register, trashes everything else.
; The selfmod_* addresses are operands to compare instructions,
; found in drunkwalk.s.
set_limits:
ldx circlesize
lda xmin,x
sta selfmod_xmin
lda ymin,x
sta selfmod_ymin
lda xmax,x
sta selfmod_xmax
lda ymax,x
sta selfmod_ymax
rts
;;; Subroutine: initscreen
;;; Clear screen and pixarray memory, point ANTIC to our display list.
;;; no arguments. trashes all registers.
; Take 18 jiffies (0.3 sec). Probably not worth optimizing.
; Uncomment next line to see how long it takes (check locations
; $0600-$0603 in atari800 debugger).
; INITSCR_PROFILE = 1
initscreen:
.ifdef INITSCR_PROFILE
lda RTCLOK+1
sta $0600
lda RTCLOK+2
sta $0601
.endif
lda #<screen
sta pixptr
lda #>screen
sta pixptr+1
; first, clear linebuf and textbuf
lda #0
tay
isloop0:
sta linebuf,y
iny
cpy #$40
bne isloop0
; next, clear screen memory
ldx #$72 ; clear this many pages
tay ; 0 again
isloop:
sta (pixptr),y
iny
bne isloop
inc pixptr+1
dex
bne isloop
lda #DMA_ON ; set ANTIC narrow playfield mode
sta SDMCTL
lda #<dlist ; use our display list
sta SDLSTL
lda #>dlist
sta SDLSTH
; sneaky: tell the E: driver that screen RAM starts in our buffer,
; so we can use printchr and printchrx to print to the bottom text
; line in graphics mode.
lda #<textbuf
sta SAVMSC
lda #>textbuf
sta SAVMSC+1
lda #0
sta ROWCRS
sta COLCRS
sta COLCRS+1
.ifdef INITSCR_PROFILE
lda RTCLOK+1
sta $0602
lda RTCLOK+2
sta $0603
.endif
rts
.include "render.s"
;;; Subroutine: plot
;;; Turns on the pixel at (cursor_x, cursor_y), in pixarray.
;;; Trashes all registers (and pixptr).
plot:
ldx cursor_y
lda lineaddrs_l,x
sta pixptr
lda lineaddrs_h,x
sta pixptr+1
ldy cursor_x
lda #1
sta (pixptr),y
rts
; drunkwalk is less than a page long. nextpage macro starts it
; on a page boundary, so branches don't cost an extra cycle.
nextpage
.include "drunkwalk.s"
;;; Subroutine: drawseed
;;; dispatch to appropriate seed subroutine
drawseed:
ldx seedtype
lda seeds_h,x
pha
lda seeds_l,x
pha
rts
;;; Subroutine: seed_point
;;; draw initial point in center
seed_point:
lda #center_x
sta cursor_x
lda #center_y
sta cursor_y
jmp plot
;;; Subroutine: seed_long
;;; horizontal line, the width of the screen
seed_long:
lda #$1
sta cursor_x
lda #center_y
sta cursor_y
slnoop:
jsr plot
inc cursor_x
lda cursor_x
cmp #$aa
bne slnoop
rts
;;; Subroutine: seed_plus
;;; plus share, made of two 20px lines intersecting in the center
seed_plus:
lda #center_x
sta cursor_x
lda #center_y-10
sta cursor_y
sploop:
jsr plot
inc cursor_y
lda cursor_y
cmp #center_y+10
bne sploop
lda #center_x-10
sta cursor_x
lda #center_y
sta cursor_y
slloop:
jsr plot
inc cursor_x
lda cursor_x
cmp #center_x+10
bne slloop
rts
;;; Subroutine: seed_4pt
;;; four points, the corners of a 20px square
seed_4pt:
lda #center_x-10
sta cursor_x
lda #center_y-10
sta cursor_y
jsr plot
lda #center_y+10
sta cursor_y
jsr plot
lda #center_x+10
sta cursor_x
jsr plot
lda #center_y-10
sta cursor_y
jmp plot
;;; Subroutine: readline
;;; Read a line from E:, store result in linebuf.
;;; The terminating EOL ($9b) gets stored at the end.
;;; linebuf happens to be right where the FP ROM needs it for AFP.
readline:
lda #<linebuf
sta INBUFF
sta ICBAL
lda #>linebuf
sta INBUFF+1
sta ICBAH
lda #0
sta ICBLH
lda #32 ; buffer length; longer than we need
sta ICBLL
lda #5
sta ICCOM
ldx #0
jmp CIOV
;;; Subroutine: restore_gr0
;;; Restore the OS's GRAPHICS 0 display, so we can use E:
restore_gr0:
lda #$90
sta COLOR2
lda #$0e
sta COLOR1
lda #$ff
sta CH
lda old_dl
sta SDLSTL
lda old_dl+1
sta SDLSTH
lda old_dma
sta SDMCTL
lda old_savmsc
sta SAVMSC
lda old_savmsc+1
sta SAVMSC+1
rts
;;; Subroutine: close1
;;; Closes IOCB 1, same as CLOSE #1 in BASIC.
close1:
ldx #$10
lda #$0c ; close
sta ICCOM,x
jmp CIOV
;;;;; end of executable code, data tables from here on out.
; prompts.
; banner and saveprompt must start with a clear-screen code.
banner:
.byte $7d, "Diffusion Limited Aggregate",$9b
.byte "Urchlay's ASM version ",VERSION,$9b,$9b
.byte "Particle count range: 1 to 65535",$9b
.byte "How many particles [",$0
partprompt:
.byte "]? ",$0
seedprompt:
.byte $9b,"Seed Type: ",$9b,"1=Dot 2=Plus 3=4Dots 4=Line [",$0
seedprompt2:
.byte "]? ",$0
saveprompt:
.byte $7d, "Save: Enter filename:",$9b,"> ",$0
saveokmsg:
.byte "Saved OK, press any key...",$0
diskerrmsg:
.byte "I/O Error ",$0
retrymsg:
.byte " Retry [Y/n]? ",$0
; this prompt has to be <22 bytes long, and can NOT contain any
; screen control codes, because when it's printed we're only
; printing to a 32-byte buffer (which we told the OS was the
; start of a GR.0 screen, but that was designated "a lie").
menumsg:
.byte " "
.byte ' ','S'|$80,"ave"
.byte ' ','R'|$80,"edo"
.byte ' ','N'|$80,"ew? "
.byte 0
; jump table for seed functions
seeds_l: .byte <(seed_point-1),<(seed_plus-1),<(seed_4pt-1),<(seed_long-1)
seeds_h: .byte >(seed_point-1),>(seed_plus-1),>(seed_4pt-1),>(seed_long-1)
; dlatbl.s is generated by perl script, mkdlatbl.pl
.include "dlatbl.s"
; table of addresses, for each line on the screen.
; these 2 tables (low and high bytes) are less than a page each.
; nextpage macro guarantees they don't cross a page boundary (so
; indexed addressing doesn't cost an extra cycle).
nextpage
lineaddrs_l:
laddr .set pixarray
.repeat 170
.byte <laddr
laddr .set laddr + 170
.endrep
nextpage
lineaddrs_h:
laddr .set pixarray
.repeat 170
.byte >laddr
laddr .set laddr + 170
.endrep
;;; display list
; ANTIC opcodes
blank8 = $70
blank3 = $20
gr8 = $0f
gr0 = $02
lms = $40
jvb = $41
xex_org dlist
.byte blank8, blank8, blank8
.byte blank8, blank3
.byte gr8 | lms
.word screen
.repeat 127
.byte gr8
.endrep
.byte gr8 | lms
.word screen2
.repeat maxlines - 129
.byte gr8
.endrep
.byte blank3
.byte gr0 | lms
.word textbuf
.byte jvb
.word dlist
xex_run init
|