Advertisement
Guest User

blinken.z80

a guest
Nov 4th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.             .CR     z80
  2.             .ORG    0x8000
  3.             .TF      blinken.hex,ins
  4. SEED:       .DW      0xadde
  5. SEED2:      .DW      0xefbe
  6. SEED3:      .DW      0x3331
  7. SEED4:      .DW      0x5f7a
  8. PORT        .EQU     0
  9. CYCLES:     .DW      0xA000 ; Default delay loop length
  10. LAST:       .DB      0x00
  11. CURR:       .DB      0x00
  12. MODE:       .DB      0x02
  13. ; MODE: Selected with buttons (when implemented)
  14. ; 0x01: Fast random flashes
  15. ; 0x02: Random flashes, random delays
  16. ; 0x04: Cylon
  17. ; 0x08: Counter
  18. ; 0x10:
  19. ; 0x20:
  20. ; 0x40:
  21. ; 0x80: Exit to monitor
  22.  
  23. DIRECTION:      .DB      0x00
  24. ;  Current direction of chase (0=L, 1=R)
  25.  
  26.             .ORG    0x8100
  27.  
  28. BEGIN:
  29.             CALL    modeselect
  30.             JP      begin
  31.  
  32. EXIT:               ; return to SCM
  33.             LD      a,port
  34.             LD      c,a
  35.             LD      a,0x00
  36.             OUT     (c),a
  37.             LD      a,0x01
  38.             LD      c,0x00
  39.             RST     0x0030
  40.             RET
  41.  
  42. MODESELECT:
  43.             LD      a,port
  44.             LD      c,a
  45.             IN      a,(c)
  46.             CALL    nz,setmode
  47.             LD      a,(mode)
  48.             BIT     0,a
  49.             JP      nz,fastflash
  50.             BIT     1,a
  51.             JP      nz,standardflash
  52.             BIT     2,a
  53.             JP      nz,cylon
  54.             BIT     3,a
  55.             JP      nz,counter
  56.             BIT     7,a
  57.             JP      nz,exit
  58.             ; fall through if the selected mode is invalid
  59.             JP      nz,standardflash
  60.             RET
  61.  
  62. SETMODE:    LD      (mode),a
  63.             ; some mode specific setup
  64.             BIT     2,a ; cylon
  65.             JP      nz,cylonsetup
  66.             BIT     3,a ; counter
  67.             JP      nz,countsetup
  68.             RET
  69.  
  70. COUNTER:
  71.             LD      a,(last)
  72.             CALL    write
  73.             INC     a
  74.             LD      (last),a
  75.             LD      bc,(cycles)
  76.             CALL    delay
  77.             RET
  78.  
  79. COUNTSETUP: LD      a,1
  80.             LD      (last),a
  81.             LD      hl,0x1000
  82.             LD      (cycles),hl
  83.             RET
  84.  
  85. CYLONSETUP: LD      a,0
  86.             LD      (direction),a
  87.             LD      a,0x1
  88.             LD      (last),a
  89.             LD      hl,0x1000
  90.             LD      (cycles),hl
  91.             RET
  92.  
  93.  
  94. CYLON:      LD      a,(direction)
  95.             LD      d,0x10 ; counter for PCM loop
  96.             LD      b, 1
  97.             AND     b
  98.             JP      nz,cylonright
  99. CYLONLEFT:  LD      a,(last)
  100.             RLA
  101.             LD      (curr),a
  102.             JP      cylonpcm
  103. CYLONRIGHT: LD      a,(last)
  104.             RRA
  105.             LD      (curr),a
  106.             JP      cylonpcm
  107. CYLONPCM:   LD      a,(curr)
  108.             LD      b,a
  109.             LD      a,(last)
  110.             OR      b
  111.             CALL    write
  112.             LD      bc,0x0080
  113.             CALL    delay
  114.             LD      a,(curr)
  115.             CALL    write
  116.             LD      bc,0x0400
  117.             CALL    delay
  118.             DEC     d
  119.             JP      nz,cylonpcm
  120.  
  121. ; finally set the direction for next time
  122.             LD      a,(curr)
  123.             OR      a ; clear carry flag
  124.             PUSH    af
  125.             RLA
  126.             JP      c,nextright
  127.             POP     af
  128.             RRA
  129.             JP      c,nextleft
  130.             JP      cylondone
  131. NEXTRIGHT:  LD      a,1
  132.             LD      (direction),a
  133.             JP      cylondone
  134. NEXTLEFT:   LD      a,0
  135.             LD      (direction),a
  136. CYLONDONE:  LD      a,(curr)
  137.             LD      (last),a
  138.             RET
  139.  
  140. STANDARDFLASH:
  141.             CALL    RandLFSR
  142.             CALL    write
  143.             LD      bc,(cycles)
  144.             CALL    delay
  145.             CALL    randomisedelay
  146.             RET
  147.  
  148. FASTFLASH:
  149.             CALL    RandLFSR
  150.             LD      (last),a
  151.             CALL    write
  152.             LD      bc,0x1000
  153.             CALL    delay
  154.             RET
  155.  
  156. WRITE:      PUSH    af
  157.             PUSH    bc
  158.             LD      b,a ;writes value of A to LEDs
  159.             LD      a,port
  160.             LD      c,a
  161.             OUT     (c),b
  162.             POP     bc
  163.             POP     af
  164.             RET
  165.  
  166. RANDOMISEDELAY:
  167.             CALL    RandLFSR
  168.             LD      (cycles),a
  169.             CALL    RandLFSR
  170.             LD      b,a
  171.             SRL     b ; keep things speedy
  172.             LD      a,b
  173.             LD      (cycles+1),a
  174.             RET
  175.  
  176. DELAY:               ;preload BC with desired number of cycles
  177.             BIT     0,a ; the most expensive instruction we could find :)
  178.             BIT     0,a
  179.             AND     0xff
  180.             DEC     bc
  181.             LD      a,c
  182.             OR      b
  183.             JP      nz,delay
  184.             RET
  185.  
  186. RANDLFSR:
  187.             LD      hl,seed+4
  188.             LD      e,(hl)
  189.             INC     hl
  190.             LD      d,(hl)
  191.             INC     hl
  192.             LD      c,(hl)
  193.             INC     hl
  194.             LD      a,(hl)
  195.             LD      b,a
  196.             RL      e
  197.             RL      d
  198.             RL      c
  199.             RLA
  200.             RL      e
  201.             RL      d
  202.             RL      c
  203.             RLA
  204.             RL      e
  205.             RL      d
  206.             RL      c
  207.             RLA
  208.             LD      h,a
  209.             RL      e
  210.             RL      d
  211.             RL      c
  212.             RLA
  213.             XOR     b
  214.             RL      e
  215.             RL      d
  216.             XOR     h
  217.             XOR     c
  218.             XOR     d
  219.             LD      hl,seed+6
  220.             LD      de,seed+7
  221.             LD      bc,7
  222.             LDDR
  223.             LD      (de),a
  224.             RET
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement