home *** CD-ROM | disk | FTP | other *** search
/ ticalc.org / ticalc_org_rev_b.iso / archives / 83 / asm / source / sprdemo.z80 < prev    next >
Encoding:
Text File  |  2001-07-01  |  11.4 KB  |  378 lines

  1. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  2. ;│█████ Z80 ██████│  Sprite  demo  │█████████████████████│ movax │████████████│
  3. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  4.  
  5. ; Sprite example 2 v1.0
  6. ; Coded by Hannes Edfeldt in 1997
  7.  
  8. ; This little program demonstrates how to use the routines DRWSPR and CLRSPR.
  9.  
  10. ; Use the graphkeys to alternate between five diffrent speeds, press clear to
  11. ; quit.
  12.  
  13. ; Feel free to use these routines in your own productions as long as you give
  14. ; me some credit.
  15.  
  16. ; This file should of course be viewed in a DOS texteditor ;)
  17.  
  18. ; Hannes Edfeldt -+- movax@algonet.se -+- http://www.algonet.se/~movax
  19.  
  20.         .org     9327h
  21.  
  22. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  23. ;│█████ Z80 ██████│      CODE      │█████████████████████│ movax │████████████│
  24. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  25.  
  26. START:
  27.         call    RINDOFF
  28.         call    BUFCLR
  29.  
  30. MAIN:
  31.         ld      b,44            ; -- Draw sprites
  32.         ld      ix,SPRCORD
  33.         ld      hl,ALIEN        ; Get address to the alien sprite
  34. SPRLOP: ld      a,(ix+0)        ; Get x-coordinate
  35.         ld      e,(ix+1)        ; Get y-coordinate
  36.         push    bc
  37.         push    hl
  38.         push    ix
  39.         call    DRWSPR          ; Draw the sprite
  40.         pop     ix
  41.         pop     hl
  42.         pop     bc
  43.         inc     ix              ; Make ix point to next sprite's coordinates
  44.         inc     ix
  45.         djnz    SPRLOP
  46.  
  47.  
  48.         ld      a,(SPEED)
  49.         ld      b,a
  50. WAITL1:
  51.         push    bc
  52.         call    BUFCOPY         ; Copy graphbuf to LCD
  53.         pop     bc
  54.         djnz    WAITL1
  55.  
  56.  
  57.         ld      b,44            ; -- Clear sprites
  58.         ld      ix,SPRCORD
  59.         ld      hl,ALIEN        ; Get address to the alien sprite
  60. CLRLOP: ld      a,(ix+0)        ; Get x-coordinate
  61.         ld      e,(ix+1)        ; Get y-coordinate
  62.         push    bc
  63.         push    hl
  64.         push    ix
  65.         call    CLRSPR          ; Clear the sprite
  66.         pop     ix
  67.         pop     hl
  68.         pop     bc
  69.         inc     ix              ; Make ix point to next sprite's coordinates
  70.         inc     ix
  71.         djnz    CLRLOP
  72.  
  73.  
  74.         ld      b,44            ; -- Update sprite directions
  75.         ld      ix,SPRCORD
  76.         ld      hl,SPRDIR
  77.  
  78. CHGDIR: ld      a,(ix+0)
  79.         cp      0               ; Reached left edge?
  80.         jp      nz,SKP03
  81.         ld      a,1
  82.         ld      (hl),a
  83.  
  84. SKP03:  cp      88              ; Reached right edge?
  85.         jp      nz,SKP04
  86.         ld      a,-1
  87.         ld      (hl),a
  88.  
  89. SKP04:  inc     hl
  90.         ld      a,(ix+1)
  91.         cp      0               ; Reached top?
  92.         jp      nz,SKP05
  93.         ld      a,1
  94.         ld      (hl),a
  95.  
  96. SKP05:  cp      56              ; Reached bottom?
  97.         jp      nz,SKP06
  98.         ld      a,-1
  99.         ld      (hl),a
  100.  
  101. SKP06:  inc     ix
  102.         inc     ix
  103.         inc     hl
  104.         djnz    CHGDIR
  105.  
  106.  
  107.         ld      b,88            ; -- Sprite movement
  108.         ld      ix,SPRCORD
  109.         ld      hl,SPRDIR
  110.  
  111. SPRMOV: ld      a,(ix+0)
  112.         add     a,(hl)
  113.         ld      (ix+0),a
  114.         inc     ix
  115.         inc     hl
  116.         djnz    SPRMOV
  117.  
  118.  
  119.         call    READKEY         ; Look for speedchange
  120.         call    OP2TOP1
  121.         call    CONVOP1
  122.         sub     10
  123.         cp      6
  124.         jp      nc,SKP08
  125.         ld      (KEY),a
  126.  
  127. SKP08:  ld      a,(KEY)
  128.         ld      (SPEED),a
  129.  
  130.         ld      a,0ffh          ; Reset the keyport (kinda)
  131.         out     (1),a
  132.         ld      a,0fdh          ; Enable the row with CLEAR
  133.         out     (1),a
  134.         in      a,(1)
  135.         cp      191             ; Check for CLEAR
  136.         jp      nz,MAIN         ; Exit if clear is pressed
  137.  
  138.  
  139.         call    CLRTSHD         ; Clear textshadow
  140.         call    GOHOME          ; Leave graphscreen and go to homescreen
  141.         call    BUFCLR          ; Clear the graphbuf
  142.         call    READKEY         ; Catch the clear press
  143.         call    HOMEUP          ; Place cursor at home
  144.         ret                     ; Exit program
  145.  
  146. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  147. ;│█████ Z80 ██████│   PROCEDURES   │█████████████████████│ movax │████████████│
  148. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  149.  
  150. ;▄████████████▀ DRWSPR ▀███████████████████████████████████████████████████████
  151. ;┌────────────────────────────────────────────────────────────────────────────┐
  152. ;│ Draw 8x8 sprite ■ a=x, e=y, hl=sprite address                              │
  153. ;└────────────────────────────────────────────────────────────────────────────┘
  154. DRWSPR:
  155.  
  156.         push    hl              ; Save sprite address
  157.  
  158. ;████   Calculate the address in graphbuf   ████
  159.  
  160.         ld      hl,0            ; Do y*12
  161.         ld      d,0
  162.         add     hl,de
  163.         add     hl,de
  164.         add     hl,de
  165.         add     hl,hl
  166.         add     hl,hl
  167.  
  168.         ld      d,0             ; Do x/8
  169.         ld      e,a
  170.         srl     e
  171.         srl     e
  172.         srl     e
  173.         add     hl,de
  174.  
  175.         ld      de,8e29h
  176.         add     hl,de           ; Add address to graphbuf
  177.  
  178.         ld      b,00000111b     ; Get the remainder of x/8
  179.         and     b
  180.         cp      0               ; Is this sprite aligned to 8*n,y?
  181.         jp      z,ALIGN
  182.  
  183.  
  184. ;████   Non aligned sprite blit starts here   ████
  185.  
  186.         pop     ix              ; ix->sprite
  187.         ld      d,a             ; d=how many bits to shift each line
  188.  
  189.         ld      e,8             ; Line loop
  190. LILOP:  ld      b,(ix+0)        ; Get sprite data
  191.  
  192.         ld      c,0             ; Shift loop
  193.         push    de
  194. SHLOP:  srl     b
  195.         rr      c
  196.         dec     d
  197.         jp      nz,SHLOP
  198.         pop     de
  199.  
  200.         ld      a,b             ; Write line to graphbuf
  201.         or      (hl)
  202.         ld      (hl),a
  203.         inc     hl
  204.         ld      a,c
  205.         or      (hl)
  206.         ld      (hl),a
  207.  
  208.         ld      bc,11           ; Calculate next line address
  209.         add     hl,bc
  210.         inc     ix              ; Inc spritepointer
  211.  
  212.         dec     e
  213.         jp      nz,LILOP        ; Next line
  214.  
  215.         jp      DONE1
  216.  
  217.  
  218. ;████   Aligned sprite blit starts here   ████
  219.  
  220. ALIGN:                          ; Blit an aligned sprite to graphbuf
  221.         pop     de              ; de->sprite
  222.         ld      b,8
  223. ALOP1:  ld      a,(de)
  224.         or      (hl)            ; xor=erase/blit
  225.         ld      (hl),a
  226.         inc     de
  227.         push    bc
  228.         ld      bc,12
  229.         add     hl,bc
  230.         pop     bc
  231.         djnz    ALOP1
  232.  
  233. DONE1:
  234.         ret
  235. ;▄████████████▄ DRWSPR ▄███████████████████████████████████████████████████████
  236.  
  237.  
  238. ;▄████████████▀ CLRSPR ▀███████████████████████████████████████████████████████
  239. ;┌────────────────────────────────────────────────────────────────────────────┐
  240. ;│ Clear 8x8 sprite ■ a=x, e=y, hl=sprite address                             │
  241. ;└────────────────────────────────────────────────────────────────────────────┘
  242. CLRSPR:
  243.         push    hl              ; Save sprite address
  244.  
  245. ;████   Calculate the address in graphbuf   ████
  246.  
  247.         ld      hl,0            ; Do y*12
  248.         ld      d,0
  249.         add     hl,de
  250.         add     hl,de
  251.         add     hl,de
  252.         add     hl,hl
  253.         add     hl,hl
  254.  
  255.         ld      d,0             ; Do x/8
  256.         ld      e,a
  257.         srl     e
  258.         srl     e
  259.         srl     e
  260.         add     hl,de
  261.  
  262.         ld      de,8e29h
  263.         add     hl,de           ; Add address to graphbuf
  264.  
  265.         ld      b,00000111b     ; Get the remainder of x/8
  266.         and     b
  267.         cp      0               ; Is this sprite aligned to 8*n,y?
  268.         jp      z,ALIGN2
  269.  
  270.  
  271. ;████   Non aligned sprite erase starts here   ████
  272.  
  273.         pop     ix              ; ix->sprite
  274.         ld      d,a             ; d=how many bits to shift each line
  275.  
  276.         ld      e,8             ; Line loop
  277. LILOP2: ld      b,(ix+0)        ; Get sprite data
  278.  
  279.         ld      c,0             ; Shift loop
  280.         push    de
  281. SHLOP2: srl     b
  282.         rr      c
  283.         dec     d
  284.         jp      nz,SHLOP2
  285.         pop     de
  286.  
  287.         ld      a,b             ; Write line to graphbuf
  288.         cpl
  289.         and     (hl)
  290.         ld      (hl),a
  291.         inc     hl
  292.         ld      a,c
  293.         cpl
  294.         and     (hl)
  295.         ld      (hl),a
  296.  
  297.         ld      bc,11           ; Calculate next line address
  298.         add     hl,bc
  299.         inc     ix              ; Inc spritepointer
  300.  
  301.         dec     e
  302.         jp      nz,LILOP2       ; Next line
  303.  
  304.         jp      DONE5
  305.  
  306.  
  307. ;████   Aligned sprite erase starts here   ████
  308.  
  309. ALIGN2:                         ; Erase an aligned sprite in graphbuf
  310.         pop     de              ; de->sprite
  311.         ld      b,8
  312. ALOP2:  ld      a,(de)
  313.         cpl
  314.         and     (hl)
  315.         ld      (hl),a
  316.         inc     de
  317.         push    bc
  318.         ld      bc,12
  319.         add     hl,bc
  320.         pop     bc
  321.         djnz    ALOP2
  322.  
  323. DONE5:
  324.         ret
  325. ;▄████████████▄ CLRSPR ▄███████████████████████████████████████████████████████
  326.  
  327. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  328. ;│█████ Z80 ██████│     EQUALS     │█████████████████████│ movax │████████████│
  329. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  330.  
  331. WAITKEY .equ     4CFEh  ; Wait for a key and read
  332. BUFCLR    .equ     515Bh    ; Clear the graph backup
  333. BUFCOPY .equ     5164h    ; Copy the graph backup to the screen
  334. RINDOFF .equ     4795h    ; Turn off runindicator
  335. PRINTHL .equ     4709h    ; Print HL in dec. on the screen
  336. OP2TOP1 .equ     41C2h    ; Move OP2 to OP1
  337. CONVOP1 .equ     4EFCh    ; Convert fp value in OP1 to a 2 byte hex
  338. READKEY .equ     4A18h    ; Read key and place it in OP2 as a fp value
  339. GOHOME    .equ     47A1h    ; Go to home screen (finish gfx program)
  340. CLRTSHD .equ     4765h    ; Clear text shadow
  341. HOMEUP    .equ     4775h    ; Place cursor at home
  342. STRING    .equ     470Dh    ; Print 0 terminated string to screen (hl->string)
  343.  
  344. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  345. ;│█████ Z80 ██████│      DATA      │█████████████████████│ movax │████████████│
  346. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  347.  
  348. ALIEN   .db     00011000b
  349.         .db     01111110b
  350.         .db     11111111b
  351.         .db     10111101b
  352.         .db     10011001b
  353.         .db     11111111b
  354.         .db     01111110b
  355.         .db     00111100b
  356.  
  357. SPRCORD .db     40,0,48,0
  358.         .db     24,8,32,8,40,8,48,8,56,8,64,8
  359.         .db     16,16,24,16,32,16,40,16,48,16,56,16,64,16,72,16
  360.         .db     16,24,32,24,40,24,48,24,56,24,72,24
  361.         .db     16,32,40,32,48,32,72,32
  362.         .db     16,40,24,40,32,40,40,40,48,40,56,40,64,40,72,40
  363.         .db     24,48,32,48,40,48,48,48,56,48,64,48
  364.         .db     32,56,40,56,48,56,56,56
  365.  
  366. SPEED   .db     64
  367. KEY     .db     1
  368.  
  369. SPRDIR  .db     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  370.         .db     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  371.         .db     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  372.         .db     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  373.  
  374. .end
  375. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  376. ;│█████ Z80 ██████│  Sprite  demo  │█████████████████████│ movax │████████████│
  377. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  378.