dsforth: moved 8x8 printer to separate include (and made it configurable); added...
[urasm.git] / samples / lanton_ant.zas
blobdfeb6675b96202dac92f74daad67de8eee79ce76
1 ; http://www.retroprogramming.com/2016/05/langtons-ant-for-zx-spectrum.html
2 ; Langton's Ant is an automata which creates a complex pattern by following
3 ; a couple of simple rules:
5 ; If the ant is on an empty pixel, turn 90 right, set the pixel then move forward
6 ; If the ant is on a set pixel, turn 90 left, reset the pixel then move forward
8 ; The ant's path appears chaotic at first before falling into a repetitive "highway"
9 ; pattern, moving 2 pixels diagonally every 104 cycles.
11   org #8000
13   ld    a,6
14   include <cls_ei.zas>
16   call  MAIN
17   include <waitkey.zas>
18   ret
20 MAIN:
21   ld de,128*256+96
23 ANT:
24   ; make it visually better ;-)
25   halt
27   ld    a,c      ; check direction
28   and   3
29   rrca
30   add   a,a
31   dec   a
32   jr    nc,XMOVE
34   add   a,e     ; adjust y position +/-1
35   ld    e,a
36   cp    192
37   ret   nc
38   xor   a
40 XMOVE:
41   add   a,d     ; adjust x position +/-1
42   ld    d,a
44 ; ----------
45   and   7       ; calculate screen address
46   ld    b,a
47   inc   b
48   ld    a,e
49   rra
50   scf
51   rra
52   or    a
53   rra
54   ld    l,a
55   xor   e
56   and   248
57   xor   e
58   ld    h,a
59   ld    a,d
60   xor   l
61   and   7
62   xor   d
63   rrca
64   rrca
65   rrca
66   ld    l,a
67   ld    a,1
68 PLOTBIT:
69   rrca
70   djnz PLOTBIT
71 ; ----------
73   ld    b,a      ; test pixel
74   and   (hl)
76   jr    nz,LEFT  ; turn left/right
77   inc   c
78   inc   c
79 LEFT:
80   dec   c
82   ld    a,b      ; flip pixel
83   xor   (hl)
84   ld    (hl),a
85   jr    ANT