home *** CD-ROM | disk | FTP | other *** search
/ ticalc.org / ticalc_org_rev_b.iso / archives / 82 / asm / source / ash / life.asm < prev    next >
Encoding:
Assembly Source File  |  2001-07-01  |  6.0 KB  |  505 lines

  1. #include ti82.h
  2. #include graph.h
  3.  
  4.  
  5. neighbors = APD_BUF
  6. mode = TEXT_MEM
  7. onflag = TEXT_MEM+1
  8. pointerx = TEXT_MEM+2
  9. pointery = TEXT_MEM+3
  10. flash = TEXT_MEM+4
  11. realflash = TEXT_MEM+5
  12. toggling = TEXT_MEM+6
  13. realpointers = TEXT_MEM+7
  14. realpointers2 = TEXT_MEM+8
  15.     .org START_ADDR
  16.     .db "Life 1.1 - Lite",0
  17.     ld a,1
  18.     ld (mode),a
  19.     ld a,$8c
  20.     out (2),a
  21.     
  22. starteverything:
  23.     ld hl,pointerx
  24.     ld (hl),7
  25.     inc hl
  26.     ld (hl),7
  27.     ld hl,realpointers    
  28.     ld (hl),45
  29.     inc hl
  30.     ld (hl),29
  31.     ld a,255
  32.     ld hl,GRAPH_MEM
  33.     ld (hl),a
  34.     ld de,GRAPH_MEM+1
  35.     ld bc,$2ff
  36.     ldir
  37.     call cleararray
  38.     jp gotocursor
  39.  
  40.  
  41. doitagain:
  42.     call makeitappear
  43.     ld hl,mode
  44.     ld a,(hl)
  45.     or a
  46.     jp nz,steploop    
  47.     call GET_KEY
  48.     cp $37
  49.     ret z
  50.     cp $30
  51.     jp z,gotostep        
  52.     cp $38
  53.     jp z, gotocursor    
  54.     cp $28
  55.     call z,cleararray
  56.     
  57.     
  58.  
  59. update: ld e,0
  60.     ld hl,neighbors
  61. examineloop:
  62.     ld a,(hl)
  63.     or a
  64.     jp m,alivenow
  65. deadnow:
  66.     ld b,7
  67.     and b
  68.     cp 3
  69.     jr z,birth
  70.     jr die
  71.     
  72. birth:
  73.     set 6,(hl)
  74.     jr endofexamine
  75. alivenow:
  76.     ld b,7
  77.     and b
  78.     cp 3
  79.     jr z,birth
  80.     cp 2
  81.     jr z,birth
  82. die:    res 6,(hl)
  83.     
  84. endofexamine:
  85.     inc hl
  86.     inc e
  87.     ld a,e
  88.     or a
  89.     jr nz,examineloop
  90.     
  91.     
  92.  
  93. neighborly:
  94.     ld e,0
  95.     ld ix,neighbors
  96. neighborloop1:
  97.     ld a,(ix)
  98.     ld b,%11000000
  99.     and b
  100.     cp 64
  101.     jr z,realbirth
  102.     cp 128
  103.     jr z,realdeath
  104.     inc e
  105.     jp z,doitagain
  106.     inc ix
  107.     jr neighborloop1
  108.     
  109. realbirth:
  110.     set 7,(ix)
  111.     ld c,1
  112.     jr updateneighbors
  113. realdeath:
  114.     res 7,(ix)
  115.     ld c,255
  116. updateneighbors:
  117.     ld b,e
  118.     ld a,e
  119.     and %11110000
  120.     srl a
  121.     srl a
  122.     srl a
  123.     srl a
  124.     ld d,a    
  125.     ld a,e
  126.     and %00001111
  127.     ld e,a
  128.  
  129. letsgo:
  130.     
  131.     
  132.     ld a,d
  133.     or a
  134.     jr z,midleft
  135.     
  136.     ld a,e
  137.     or a
  138.     jr z,topmid 
  139.     
  140.     ld a,(ix-17)
  141.     add a,c
  142.     ld (ix-17),a
  143.     
  144. topmid:    
  145.     ld a,(ix-16)
  146.     add a,c
  147.     ld (ix-16),a
  148.     
  149.     ld a,e
  150.     cp 15
  151.     jr z,midleft
  152.         
  153.     ld a,(ix-15)
  154.     add a,c
  155.     ld (ix-15),a
  156. midleft:
  157.     ld a,e
  158.     or a
  159.     jr z,midright
  160.     
  161.     ld a,(ix-1)
  162.     add a,c
  163.     ld (ix-1),a
  164. midright:
  165.     
  166.     ld a,e
  167.     cp 15
  168.     jr z,botleft
  169.  
  170.     ld a,(ix+1)
  171.     add a,c
  172.     ld (ix+1),a
  173. botleft:
  174.     ld a,d
  175.     cp 15
  176.     jr z,doneupdating
  177.     
  178.     ld a,e
  179.     or a
  180.     jr z,botmid
  181.     
  182.     ld a,(ix+15)
  183.     add a,c
  184.     ld (ix+15),a
  185.     
  186. botmid:
  187.     ld a,(ix+16)
  188.     add a,c
  189.     ld (ix+16),a
  190.     
  191.     ld a,e
  192.     cp 15
  193.     jr z,doneupdating
  194.     
  195.     ld a,(ix+17)
  196.     add a,c
  197.     ld (ix+17),a
  198.     
  199.  
  200. doneupdating:
  201.     ld a,(toggling)
  202.     or a
  203.     jr nz,returnfromtoggling    
  204.     ld e,b
  205.     inc ix
  206.     inc e
  207.     jp z,doitagain
  208.     jp neighborloop1
  209. returnfromtoggling:
  210.     xor a
  211.     ld (toggling),a    
  212.     pop bc
  213.     jp updatescreen
  214.  
  215.  
  216.  
  217.  
  218.  
  219.     
  220.  
  221. step:    
  222. steploop:
  223.     call GET_KEY
  224.     cp $36
  225.     jp z,update
  226.     cp $37
  227.     ret z
  228.     cp $30
  229.     jr z,gotorun    
  230.     cp $38
  231.     jr z,gotocursor
  232.     cp $28
  233.     call z,cleararray
  234.     jr steploop
  235.  
  236. gotostep:
  237.     ld hl,mode
  238.     ld a,1
  239.     ld (hl),a
  240.     jr step
  241. gotorun:
  242.     ld hl,mode
  243.     xor a
  244.     ld (hl),a
  245.     jp doitagain
  246.  
  247.  
  248.  
  249. gotocursor:
  250.     xor a
  251.     ld (flash),a
  252.     ld (realflash),a
  253.     inc a
  254.     ld (onflag),a
  255.     ld (mode),a
  256.     ld hl,realpointers    
  257.     ld b,(hl)
  258.     inc hl
  259.     ld c,(hl)
  260.  
  261.     
  262.     
  263. updatecursor:
  264.     push bc
  265.     call disppix
  266.     ROM_CALL(DISP_GRAPH)
  267.     pop bc
  268. ewait:
  269.     ld hl,flash    
  270.     inc (hl)
  271.     jr nz,noflip
  272. flip:
  273.     ld hl,realflash
  274.     inc (hl)
  275.     ld a,(hl)
  276.     cp 30
  277.     jr nz,noflip    
  278.     
  279.     xor a
  280.     ld (hl),a
  281.     ld hl,onflag
  282.     ld a,1
  283.     xor (hl)
  284.     ld (hl),a
  285.     jr updatecursor
  286. noflip:    
  287.     call GET_KEY
  288.     cp $01
  289.     jr z,down
  290.     cp $02
  291.     jr z,left
  292.     cp $03
  293.     jr z,right
  294.     cp $04
  295.     jr z,up
  296.     cp $36
  297.     jr z,toggle
  298.     cp $38
  299.     call z, pushpointer
  300.     jp z,doitagain    
  301.     cp $28
  302.     call z,cleararray
  303.     cp $30 
  304.     call z,pushpointer
  305.     jp z,gotorun    
  306.     cp $37
  307.     ret z
  308.     jr ewait    
  309. up:    ld hl,pointery
  310.     ld d,(hl)
  311.     ld a,14
  312.     cp d
  313.     jr c,ewait
  314.     inc (hl)
  315.     inc c
  316.     inc c
  317.     inc c
  318.     jr updatescreen
  319. left:    ld hl,pointerx
  320.     ld a,(hl)
  321.     or a
  322.     jr z,ewait
  323.     dec (hl)
  324.     dec b
  325.     dec b
  326.     dec b
  327.     jr updatescreen
  328. right: ld hl,pointerx
  329.     ld d,(hl)
  330.     ld a,14
  331.     cp d
  332.     jr c,ewait
  333.     inc (hl)
  334.     inc b
  335.     inc b
  336.     inc b
  337.     jr updatescreen
  338.     
  339. down:    ld hl,pointery
  340.     ld a,(hl)
  341.     or a
  342.     jr z,ewait
  343.     dec (hl)
  344.     dec c
  345.     dec c
  346.     dec c
  347. updatescreen:
  348.     push bc
  349.     call makeitappear
  350.     pop bc
  351.     jp ewait
  352. toggle:
  353.     push bc
  354.         ld hl,pointerx
  355.         ld b,(hl)
  356.         inc hl
  357.         ld c,(hl)
  358.     ld e,b
  359.     ld d,c
  360.     ld a,c
  361.     sla a
  362.     sla a
  363.     sla a
  364.     sla a
  365.     add a,b
  366.     ld c,a
  367.     ld b,0
  368.     ld ix,neighbors
  369.     add ix,bc
  370.     ld hl,toggling
  371.     inc (hl)
  372.     ld a,(ix)
  373.     or a
  374.         jp m,toggledeath 
  375.     ld c,1
  376.     set 6,(ix)
  377.     set 7,(ix)
  378.     jp letsgo
  379. toggledeath:
  380.     ld c,255
  381.     res 6,(ix)
  382.     res 7,(ix)
  383.     jp letsgo
  384.  
  385.  
  386.  
  387. ;**************************subroutines
  388.  
  389. point:
  390.     ld a,(onflag)
  391.     or a
  392.     jr z,Point_off
  393. Point_on:
  394.     push hl
  395.     push bc                ;save bc
  396.     push de                ;save de
  397.     call FIND_PIXEL            ;get offset of pixel
  398.     ld de, $88B8            ;add with offset
  399.     add hl, de            ;for the graph mem
  400.     or (hl)                ;set bit
  401.     ld (hl), a            ;load back
  402.     pop de                ;restore de
  403.     pop bc                ;restore bc
  404.     pop hl
  405.     ret
  406. Point_off:
  407.     push hl
  408.     push bc
  409.     push de
  410.     call FIND_PIXEL
  411.     ld de,$88B8
  412.     add hl,de
  413.     ld b,255
  414.     xor b
  415.     and (hl)
  416.     ld (hl),a
  417.     pop de
  418.     pop bc
  419.     pop hl
  420.     ret
  421.  
  422.  
  423. disppix:
  424.     
  425.     inc c    
  426.     inc c
  427.     call point    
  428.     dec c
  429.     call point
  430.     dec c
  431.     call point
  432.     inc b
  433.     call point    
  434.     inc c
  435.     call point    
  436.     inc c
  437.     call point
  438.     inc b
  439.     call point
  440.     dec c
  441.     call point
  442.     dec c
  443.     call point
  444.     inc b
  445.     ret    
  446.  
  447.  
  448. makeitappear:
  449.     ld h,16        ;h=ycounter
  450.     ld l,16        ;l=xcounter
  451.     ld b,24        ;xcoordinate
  452.     ld c,8        ;ycoordinate
  453.     ld de,neighbors-1    ;address to start with
  454. xloop:            ;move x coordinate
  455.     inc de        ;move to next spot in array
  456.     ld a,(de)    ;load alive at x,y
  457.     or a        ;check flags
  458.     jp m,onpix    ;if not alive,off
  459.     xor a
  460.     ld (onflag),a
  461.     call disppix
  462.     jr nodraw
  463. onpix:
  464.     ld a,1
  465.     ld (onflag),a
  466.     call disppix
  467.     
  468.  
  469. nodraw:    dec l        ;decrement xcounter
  470.     ld a,l        ;
  471.     or a        ;check flags
  472.     jr nz,xloop    ;not zero - do again
  473.     ld l,16        ;reload with 16
  474.     inc c
  475.     inc c        ;move ycoordinate
  476.     inc c
  477.     ld b,24        ;move xcoordinate
  478.     dec h        ;decrement ycounter
  479.     ld a,h        ;check flags
  480.     or a        
  481.     jr nz,xloop    ;again
  482.     ROM_CALL(DISP_GRAPH)
  483.     ret
  484.  
  485.  
  486. cleararray:
  487.     push bc
  488.     ld hl,neighbors
  489.     ld (hl),0
  490.     ld de,neighbors+1
  491.     ld bc,255    
  492.     ldir            ;clear everything
  493.     call makeitappear
  494.     pop bc
  495.     ret
  496. pushpointer:
  497.     push af
  498.     ld hl,realpointers
  499.     ld (hl),b
  500.     inc hl
  501.     ld (hl),c
  502.     pop af
  503.     ret
  504. .end 
  505.