-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmandelbrot65.asm
More file actions
1438 lines (1239 loc) · 28.4 KB
/
mandelbrot65.asm
File metadata and controls
1438 lines (1239 loc) · 28.4 KB
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;-----------------------------------------------------------------------------
; MANDELBROT65 : A 3.8 FIXED POINT MATH MANDELBROT CALCULATOR FOR THE APPLE1
;-----------------------------------------------------------------------------
; FREDERIC STARK SEPTEMBER 2024
; https://www.github.com/fstark/mandelbrot65
; http://stark.fr/blog/mandelbrot65 (coming soon)
;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------
; Software starts at 0x0280
; It can be assembled anywhere but memory 0x1000-0x2000 must be available RAM
;-----------------------------------------------------------------------------
* = $0280
#define noNONRANDOM ; Define NONRANDOM to have a repeatable sequence
#define noDEBUG ; Define DEBUG to include some debugging support
;-----------------------------------------------------------------------------
; Apple1 ROM & Hardware constants
;-----------------------------------------------------------------------------
ECHO = $FFEF ; ECHO A CHARACTER
KBD = $D010 ; KEYBOARD
KBDCR = $D011 ; KEYBOARD CONTROL
;-----------------------------------------------------------------------------
; Zero page variables layout
;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------
; Coordinates of the current mandelbrot displayed,
; frequency for choice of next one
; iteration count for the current "pixel"
; and a few of internal variables (seed, abort, nan mask)
;-----------------------------------------------------------------------------
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
; 00 | X COORD | Y COORD | X DELTA | Y DELTA |ZOOM|FREQ| | |SEED|ABRT| IT |
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
X0 = $00 ; Top-left position in the set
Y0 = $02
DX = $04 ; Current delta
DY = $06
ZOOMLEVEL = $08 ; Current zoom level (0-4)
FREQ = $09 ; Used to randomly choose the next coordinates during the computation of the current one
SEED = $0D ; The random seed
ABORT = $0E ; If bit 7 is set, abort was requested by the user
; Use BIT ABORT + BMI to check
; It aborts the menu display or the current mandelbrot
IT = $0F ; Iteration counter
;-----------------------------------------------------------------------------
; Coordinates of the next mandelbrot to display (X,Y and X,Y deltas)
;-----------------------------------------------------------------------------
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
; 10 | NEXT X | NEXT Y | NX DELTA| NY DELTA|ZOOM| |
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
NEXTX = $10 ; Next position we will zoom in
NEXTY = $12
NEXTDX = $14 ; Next delta we will use after zooming in
NEXTDY = $16
NEXTZOOMLEVEL = $18 ; Next zoom level (0-4)
;-----------------------------------------------------------------------------
; Current "pixel" beging computed, in mandelbrot and screen space
;-----------------------------------------------------------------------------
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
; 20 | X CURR | Y CURR | X SCRN | Y SCRN | ZX | ZY | ZX^2 | ZY^ 2 |
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
X = $20 ; Current position in the set
Y = $22
SCRNX = $24 ; Counter of lines and columns left to draw
SCRNY = $26
ZX = $28 ; X in current iteration
ZY = $2A
ZX2 = $2C ; X^2 in current iteration
ZY2 = $2E
;-----------------------------------------------------------------------------
; Various temporaries
;-----------------------------------------------------------------------------
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
; 30 | 3 BYTES NUM | 3 BYTES INC | POINTER | |
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
NUM = $30 ; A 3 bytes number used to build the square table
INCR = $33 ; 3 bytes increment used to build the square table
PTR = $36 ; A generic pointer
#ifdef DEBUG
;-----------------------------------------------------------------------------
; Temporary variables used by debug code
;-----------------------------------------------------------------------------
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
; 40 | DBG TMP1| DBG TMP2| DBG TMP | |
; +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
TMP1 = $40
TMP2 = $42
TMP_DBG = $44 ; 3 Bytes needed
#endif
;-----------------------------------------------------------------------------
; Constants
;-----------------------------------------------------------------------------
; 16 bits number
; Format of numbers (16 bits, stored little endian -- reversed from this drawing)
; A X
; +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
; |i|i|i|i|i|i|i|f| |f|f|f|f|f|f|f|N|
; +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
;
; iiii = integer part (7 bits, 3 significant + sign, two complement)
; ffffffff = fractional part (8 bits)
; N is Nan:
; 0 for having even addresses in the square table
; 1 for illegal number
; The cannonical NaN is 00000000 00000001
; Square table is at 00010000 00000000
; to 00011111 11111111
; All numbers in the form of 00001iif fffffff0 have squares that overflow,
; (because they are larger than 4, so they square is larger than 8, the maxium)
; so there is opportunity to either squeeze more numbers in a rewrite (3.9 fixed point)
; or using less memory for the table
; Also, the least significant bits are not that important,
; so we could move to a 4.12 or 5.11 fixed point representation
; (with loss of precision in squares)
; Default Mandelbrot coordinates
INITALX = $FBD0 ; LEFT = 11111011 11010000 = -2.093
INITALY = $FDC0 ; TOP = 11111101 11000000 = -1.125
INITIALDX = $0026 ; DX = 00000000 00100110 = 0.073
INITIALDY = $0030 ; DY = 00000000 00110000 = 0.094
; Time to wait between two mandelbrot displays in "rought seconds"
WAITIME = 4 ; Approx number of seconds to wait before going
; to the next mandelbrot display
; Location of the square table (cannot be changed)
; It may be a good idea to support $E000-$EFFF for square table as well
; (for 4K+4K machines with RAM for BASIC)
SQUARETABLE = $1000 ; This table cannot be moved
SQUARETABLE_END = $2000 ; End of table
; Width and height of the screen
; Cannot be changed, but usefull to avoid magic numbers
SCREENWIDTH = 40
SCREENHEIGHT = 24
;-----------------------------------------------------------------------------
; Couple of macros easing the manipulation of 16 bits numbers
;-----------------------------------------------------------------------------
; Loads A number into AX
#define MLOADAX(NUM) LDX NUM: LDA NUM+1
; Stores AX into NUM
#define MSTOREAX(NUM) STX NUM: STA NUM+1
;-----------------------------------------------------------------------------
; Entry point
;-----------------------------------------------------------------------------
START = *
JMP MAIN
;-----------------------------------------------------------------------------
; This independent code is used to checksum the software
; Run it on a machine where it works and one where it doesn't
; and compare the checksums
;-----------------------------------------------------------------------------
.(
FROM = $A0
MID = $A2
TO = $A4
CRC = $A6 ; current value of CRC
TMP = $A7
CRC_FROM= $A8
CRC_TO = $AA
PRBYTE = $FFDC
WOZMON = $FF00
LDA #13
JSR ECHO
LDA #<START
STA FROM
LDA #>START
STA FROM+1
LDA #<END
STA TO
LDA #>END
STA TO+1
NEXTCRC:
JSR CALCMID
LDA FROM
STA CRC_FROM
LDA FROM+1
STA CRC_FROM+1
LDA MID
STA CRC_TO
LDA MID+1
STA CRC_TO+1
LDA #'1'
JSR ECHO
LDA #')'
JSR ECHO
JSR PRRANGE
JSR CRCRANGE
JSR PRCRC
LDA MID
STA CRC_FROM
LDA MID+1
STA CRC_FROM+1
LDA TO
STA CRC_TO
LDA TO+1
STA CRC_TO+1
LDA #'2'
JSR ECHO
LDA #')'
JSR ECHO
JSR PRRANGE
JSR CRCRANGE
JSR PRCRC
; Wait for 1 or 2
WAIT:
LDA KBDCR
BPL WAIT
LDA KBD
AND #$7F
CMP #'2'
BEQ SECOND
LDA MID
STA TO
LDA MID+1
STA TO+1
JMP NEXTCRC
SECOND:
LDA MID
STA FROM
LDA MID+1
STA FROM+1
JMP NEXTCRC
PRRANGE:
LDA CRC_FROM+1
JSR PRBYTE
LDA CRC_FROM
JSR PRBYTE
LDA #'-'
JSR ECHO
LDA CRC_TO+1
JSR PRBYTE
LDA CRC_TO
JSR PRBYTE
LDA #':'
JMP ECHO
PRCRC:
LDA CRC
JSR PRBYTE
LDA #13
JMP ECHO
CALCMID:
; MID = (FROM+TO)/2
; Clear the carry flag
CLC
; Add the low bytes of FROM and TO
LDA FROM
ADC TO
STA MID
; Add the high bytes of FROM and TO, including the carry from the previous addition
LDA FROM+1
ADC TO+1
STA MID+1
; Now divide by 2
LSR MID+1
ROR MID
RTS
; From http //www.6502.org/source/integers/crc-more.html
CRC8:
EOR CRC ; A contained the data
STA CRC ; XOR it with the byte
ASL ; current contents of A will become x^2 term
BCC UP1 ; if b7 = 1
EOR #$07 ; then apply polynomial with feedback
UP1 EOR CRC ; apply x^1
ASL ; C contains b7 ^ b6
BCC UP2
EOR #$07
UP2 EOR CRC ; apply unity term
STA CRC ; save result
RTS
; Computes CRC
CRCRANGE:
LDA #$00
STA CRC
LOOP LDA CRC_FROM
CMP CRC_TO
BNE CONT
LDA CRC_FROM+1
CMP CRC_TO+1
BNE CONT
RTS
CONT LDY #0
LDA (CRC_FROM),Y
JSR CRC8
INC CRC_FROM
BNE LOOP
INC CRC_FROM+1
JMP LOOP
.)
;-----------------------------------------------------------------------------
; The data that defines how this whole thing looks
; Placed at the beginning for easier editing
;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------
; One palette for each zoom level
;-----------------------------------------------------------------------------
PALETTE:
.byte "..,'~=+:;[/<*?&O0X# XXXXXXXXXXXXXXXXXXXX"
.byte "..,'~==+:;;[[/<*??&OO0X# XXXXXXXXXXXXXXX"
.byte "..,''~==++::;;[[/<<**??&OO0X# XXXXXXXXXX"
.byte "..,''~~==+++::;;[[/<<**??&&OO00XX# XXXXX"
.byte "..,''~~==+++:::;;;[[[//<<***??&&OO00XX# "
; Useful to debug
; .byte "01234567890abcdefghijklmnopqrstuvwxyz!@#"
; .byte "01234567890abcdefghijklmnopqrstuvwxyz!@#"
; .byte "01234567890abcdefghijklmnopqrstuvwxyz!@#"
; .byte "01234567890abcdefghijklmnopqrstuvwxyz!@#"
; .byte "01234567890abcdefghijklmnopqrstuvwxyz!@#"
;-----------------------------------------------------------------------------
; The offset of each palette
; (Not ideal as it limits the total palette to < 256 chars)
; (A double indirection would be better)
;-----------------------------------------------------------------------------
PALETTEDELTA:
.byte 0, 40, 80, 120, 160
;-----------------------------------------------------------------------------
; The number of iteration per zoom level
; This also defines each palette length
;-----------------------------------------------------------------------------
MAXITER:
.byte 19, 24, 29, 34, 39
;-----------------------------------------------------------------------------
; If iteration is lower than ZOOMTRIGGERMIN, the point cannot be chosen
; as next position (too far away from the set0)
;-----------------------------------------------------------------------------
ZOOMTRIGGERMIN:
.byte 15, 17, 18, 19, 20
;-----------------------------------------------------------------------------
; If iteration is larger or equal to ZOOMTRIGGERMAX, the poinr cannot be chosen
; as next position (too close to the set)
;-----------------------------------------------------------------------------
ZOOMTRIGGERMAX:
.byte 20, 20, 21, 22, 23
;-----------------------------------------------------------------------------
; Main loop
;-----------------------------------------------------------------------------
MAIN:
.(
CLD ; Clear decimal mode
; Initialize ZP constants
LDA #0
STA ABORT ; Will be $80 if user abort
; Display intro
JSR PRINTINLINE
.byte $d, $d
.byte " --== MANDELBROT 65 ==-- ", $d, $d
.byte "A 6502 MANDELBROT SET TIME WASTER", $d
.byte " FOR YOUR APPLE 1", $d, $d
.byte " V1.1 BY FRED STARK, 2024"
.byte " HTTP://STARK.FR/BLOG/MANDELBROT65",$d,$d
.byte " (PRESS ANY KEY TO START)", $d
.byte 0
; If user aborted, we skip waiting for keypress
; as we already collected entropy in the amount
; of characters displayed
BIT ABORT
BMI SKIP
; Wait for key and init SEED
LDA KBD
LOOP:
INC SEED
LDA KBDCR
BPL LOOP
LDA KBD
SKIP:
#ifdef NONRANDOM
; If you want repeatability
LDA #$1
STA SEED
#endif
; Clear any potential previous abort
LDA #0
STA ABORT
; Initialize Square table
JSR FILLSQUARES
; Goes in AUTO mode
JMP MANDELAUTO
.)
;-----------------------------------------------------------------------------
; Display and zooms in the mandelbrot set
;-----------------------------------------------------------------------------
MANDELAUTO:
.(
; Starts a full mandelbrot
JSR INITIALPLACE
LOOP:
; Load next place to go
JSR GOTOPLACE
; Make future next place standard mandelbrot
; as a default
JSR INITIALPLACE
; A couple of lines between drawings
LDA #$d
JSR ECHO
JSR ECHO
; Clear ABORT flag
LDA #0
STA ABORT
; Draw the mandelbrot set & pick a new spot
JSR DRAWSET
; Wait for a while (or key press)
JSR WAIT
; BNE MANDELAUTO ; If non-space pressed, restart full mandelbrot
JMP LOOP
.)
;-----------------------------------------------------------------------------
; Prints the string that is stored after the JSR instruction that sent us here
; Input:
; Data stored in the code after the jsr instruction
;-----------------------------------------------------------------------------
PRINTINLINE:
.(
PLA
STA PTR
PLA
STA PTR+1
JSR INCPTR
LDY #0
PRINTLOOP:
LDA (PTR),Y
BEQ PRINTDONE
; We skip actual slow printing if abort was requested
BIT ABORT
BMI SKIP
JSR ECHO
SKIP:
; See if a key was pressed
JSR KEYPRESSED
BCC SKIP2
LDA #$FF
STA ABORT
; Increment random seed with number of chars really displayed
INC SEED
; Incremement and next
SKIP2:
JSR INCPTR
JMP PRINTLOOP
PRINTDONE:
LDA PTR+1
PHA
LDA PTR
PHA
RTS
.)
;-----------------------------------------------------------------------------
; Fills ZP "next" variables with the initial values
; Output:
; ZP:NEXTX, ZP:NEXTY, ZP:NEXTDX, ZP:NEXTDY, ZP:NEXTZOOMLEVEL
;-----------------------------------------------------------------------------
INITIALPLACE:
.(
LDA #<INITALX
STA NEXTX
LDA #>INITALX
STA NEXTX+1
LDA #<INITALY
STA NEXTY
LDA #>INITALY
STA NEXTY+1
LDA #<INITIALDX
STA NEXTDX
LDA #>INITIALDX
STA NEXTDX+1
LDA #<INITIALDY
STA NEXTDY
LDA #>INITIALDY
STA NEXTDY+1
LDA #0
STA NEXTZOOMLEVEL
RTS
.)
;-----------------------------------------------------------------------------
; Copies NEXTX/NEXTY/NEXTDX/NEXTDY/NEXTZOOMLEVEL
; into X/0/Y0/DX/DY/ZOOMLEVEL
;-----------------------------------------------------------------------------
GOTOPLACE:
.(
LDA NEXTX
STA X0
LDA NEXTX+1
STA X0+1
LDA NEXTY
STA Y0
LDA NEXTY+1
STA Y0+1
LDA NEXTDX
STA DX
LDA NEXTDX+1
STA DX+1
LDA NEXTDY
STA DY
LDA NEXTDY+1
STA DY+1
LDA NEXTZOOMLEVEL
STA ZOOMLEVEL
RTS
.)
;-----------------------------------------------------------------------------
; Wait for some time at the end of the mandelbrot display
; (tuned for something like 4 seconds)
; Can be interrupted by pressing a key
; NZ if should abort
; Output:
; ZP:ABORT bit #7 sets if abort was requested
;-----------------------------------------------------------------------------
WAIT:
.(
; If ABORT was requested, we exit
BIT ABORT
BMI DONE
; Wait for around WAITIME seconds
LDX #WAITIME*2 ; Each double loop take around 1/2 second (0.72 seconds)
LOOP1:
TXA
PHA
LDY #0
LOOP2:
LDX #0
LOOP3:
DEX
BNE LOOP3
; Exit if key pressed
LDA KBDCR
BMI KEY
DEY
BNE LOOP2
PLA
TAX
DEX
BNE LOOP1
DONE:
RTS
KEY:
LDA #$80
STA ABORT
PLA
LDA KBD ; Clear key
CMP #' '
RTS
.)
;-----------------------------------------------------------------------------
; Has a key been pressed ?
; Output:
; C if key
; Z if key is ' '
;-----------------------------------------------------------------------------
KEYPRESSED:
.(
LDA KBDCR
BPL NOKEY
; Key pressed
LDA #$80
STA ABORT
; Read key
LDA KBD
CMP #' ' ; Z if space
SEC
RTS
NOKEY:
CLC ; No key, no Carry
RTS
.)
;-----------------------------------------------------------------------------
; Draw a single mandelbrot screen
; Selects the next place to zoom in when displaying, according to ZOOMTRIGGER*
;-----------------------------------------------------------------------------
DRAWSET:
.(
LDA #0
STA FREQ
LDA #SCREENHEIGHT
STA SCRNY
MLOADAX(Y0)
MSTOREAX(Y)
LOOP1:
LDA #SCREENWIDTH
STA SCRNX
MLOADAX(X0)
MSTOREAX(X)
LOOP2:
#ifdef DEBUG
MLOADAX(X)
JSR DBG_AX
.byte "X:", 0
MLOADAX(Y)
JSR DBG_AX
.byte " Y:", 0
LDA #' '
JSR ECHO
#endif
JSR ITER
#ifdef DEBUG
LDA #' '
JSR ECHO
#endif
; Get character
JSR CHARFROMIT
; To avoid scrolling when image is complete, we skip the last char
TAX
LDA SCRNY
CMP #1 ; Last line
BNE CONTINUE
LDA SCRNX
CMP #1 ; and last column
BEQ DONE ; we stop
CONTINUE:
; Handle keypress
JSR KEYPRESSED
BIT ABORT
BMI DONE
; Display character
TXA
JSR ECHO
; Maybe this could be the new zoom ?
LDA IT
LDY ZOOMLEVEL
CMP ZOOMTRIGGERMIN,Y
BMI SKIP
CMP ZOOMTRIGGERMAX,Y
BPL SKIP
JSR SELECTNEXT
SKIP:
; Move X to next position in set
LDA DX
CLC
ADC X
STA X
LDA DX+1
ADC X+1
STA X+1
DEC SCRNX
BNE LOOP2
; Move Y to next position in set
LDA DY
CLC
ADC Y
STA Y
LDA DY+1
ADC Y+1
STA Y+1
DEC SCRNY
BNE LOOP1
DONE:
RTS
.)
;-----------------------------------------------------------------------------
; Choose a zoomed in view centered on the current position
; (X-20*DX/2,Y-12*DY/2,DX/2,DY/2,ZOOMLEVEL+1) as the next position
; This choice is done 1/FREQ times, and FREQ is incremented
; This will choose a position uniformally
; without keeping all candidates in memory
; Only works for zooms where DX<128 (ie: all of them) because the /2 division
; is only made on the LSB
; Input:
; ZP:FREQ number of previous choices (probability that this one is the right one)
; ZP:X, ZP:Y, ZP:DX, ZP:DY, ZP:ZOOMLEVEL current position
; ZP:NEXTX, ZP:NEXTY, ZP:NEXTDX, ZP:NEXTDY, ZP:NEXTZOOMLEVEL next position
; Output:
; ZP:FREQ incremented
; ZP:NEXTX, ZP:NEXTY, ZP:NEXTDX, ZP:NEXTDY, ZP:NEXTZOOMLEVEL next position
;-----------------------------------------------------------------------------
SELECTNEXT:
.(
INC FREQ
LDA FREQ
JSR RNDCHOICE ; Sets Z 1/FREQ times
BNE SKIP
LDA DX
CMP #$80
ROR
CMP #1
BEQ SKIP ; Zoom is too high
AND #$FE
STA NEXTDX
LDA DX+1
STA NEXTDX+1
LDA DY
CMP #$80
ROR
CMP #1
BEQ SKIP ; Zoom is too high
AND #$FE
STA NEXTDY
LDA DY+1
STA NEXTDY+1
LDA X
STA NEXTX
LDA X+1
STA NEXTX+1
LDA Y
STA NEXTY
LDA Y+1
STA NEXTY+1
; Remove 20x NEXTDX to NEXTX
LDX #SCREENWIDTH/2
LOOP1:
LDA NEXTX
SEC
SBC NEXTDX
STA NEXTX
LDA NEXTX+1
SBC NEXTDX+1
STA NEXTX+1
DEX
BNE LOOP1
; Remove 12x NEXTDY to NEXTY
LDX #SCREENHEIGHT/2
LOOP2:
LDA NEXTY
SEC
SBC NEXTDY
STA NEXTY
LDA NEXTY+1
SBC NEXTDY+1
STA NEXTY+1
DEX
BNE LOOP2
LDA ZOOMLEVEL
STA NEXTZOOMLEVEL
INC NEXTZOOMLEVEL
; #### Should check with some MAXZOOMLEVEL
SKIP:
RTS
.)
;-----------------------------------------------------------------------------
; Sets Z flag 1/A times
; Fundamentally we compute (A%Z)==1, quite slowly
; Input:
; A: FREQ
; Output:
; Z flag: set 1/FREQ times
;-----------------------------------------------------------------------------
RNDCHOICE:
.(
TAX ; X = FREQ
JSR RANDOM
TAY ; Y = RANDOM
TXA ; A = FREQ
LOOP:
DEX
BNE SKIP ; Skip if not last
TAX ; Reset FREQ
SKIP:
DEY
BNE LOOP
TXA ; If X will be equal to 1 1/FREQ times
CMP #1 ; Test A%Z == 1
RTS
.)
;-----------------------------------------------------------------------------
; Return a random number in A
; Only an 8 bits seed, 255 different sequences
; Source: https://codebase64.org/doku.php?id=base:small_fast_8-bit_prng
; Input:
; ZP:SEED seed
; Output:
; A random number
; SEED updated
;-----------------------------------------------------------------------------
RANDOM:
.(
LDA SEED
ASL
BCC SKIP
EOR #$1d
SKIP:
STA SEED
RTS
.)
;-----------------------------------------------------------------------------
; Increments PTR
; Input:
; ZP:PTR, ZP:PTR+1
; Output:
; ZP:PTR, ZP:PTR+1 is incremented
;-----------------------------------------------------------------------------
INCPTR:
.(
CLC
INC PTR
BNE DONE
INC PTR+1
DONE:
RTS
.)
;-----------------------------------------------------------------------------
; Compute one set of mandelbrot iterations
; Input:
; ZP:X, ZP:Y contains the current position
; Output:
; ZP:IT iteration counter
;-----------------------------------------------------------------------------
ITER:
.(
; IT = 0
LDA #0
STA IT
; ZX = X
; ZX2 = ZX^2
MLOADAX(X)
MSTOREAX(ZX)
JSR SQUARE
BCS DONE
MSTOREAX(ZX2)
; ZY = Y
; ZY2 = ZY^2
MLOADAX(Y)
MSTOREAX(ZY)
JSR SQUARE
BCS DONE
MSTOREAX(ZY2)
LOOP:
; Compute one mandelbrot iteration
JSR MANDEL1
#ifdef DEBUG
PHP
MLOADAX(ZX)
JSR DBG_AX
.byte "ZX:", 0
MLOADAX(ZY)
JSR DBG_AX
.byte " ZY:", 0
LDA #' '
JSR ECHO
PLP
#endif
BCS DONE
; Increment iteration
INC IT
; Stop at 42
LDY ZOOMLEVEL
LDA MAXITER,Y
CMP IT
BNE LOOP
DONE:
RTS
.)
;-----------------------------------------------------------------------------
; Compute one mandelbrot iteration
; Inputs:
; ZP:ZX, ZP:ZY, ZP:ZX2, ZP:ZY2
; Outputs:
; ZP:ZX, ZP:ZY, ZP:ZX2, ZP:ZY2
; C if overflow or stop
;-----------------------------------------------------------------------------
MANDEL1:
.(
; COMPUTE ZY
; zy = 2zx.zy + y
; zy = zx2-(zx-zy)^2+zy2+y
; zy = -(-zy+zx)^2+zx2+zy2+y
; -zy
MLOADAX(ZY)
JSR NEG
; -zy+zx
CLC
PHA
TXA