Advertisement
swimingduck

Z80 Tetris

Mar 16th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .nolist
  2.  #include "ti83plus.inc"
  3.  #define gbuf plotsscreen
  4.  
  5.  #define fallingBlocks AppBackUpScreen
  6.  #define setBlocks AppBackUpScreen+8
  7. .list
  8.  
  9. ; coords go x, y, x, y in fallingBlocks
  10. ; h = y coordinate, l = x coordinate
  11. ; coords go (0, 0), (1, 0), ..., (9, 0), (0, 1), (1, 1), ...,
  12. ; (9, 1), (0, 2), (1, 2), ... in setBlocks
  13.  
  14. .org $9D93
  15. .db $BB, $6D
  16. programStart:
  17.  ld hl, $FC0B
  18.  ld (fallingBlocks), hl
  19.  ld hl, $FF0B
  20.  ld (fallingBlocks+2), hl
  21.  ld hl, $FF0E
  22.  ld (fallingBlocks+4), hl
  23.  ld hl, $FF11
  24.  ld (fallingBlocks+6), hl
  25.  
  26.  ld a, (fallTime)
  27.  ld (fallTimer), a
  28.  ld a, (lockTime)
  29.  ld (lockTimer), a
  30. gameLoop:
  31.  ld a, 0
  32.  ld (isBlocked), a
  33.  ld a, (fallTimer)
  34.  dec a
  35.  jp nz, writeFallTimer
  36.  call dropFallingBlocks
  37.  ld a, (fallTime)
  38. writeFallTimer:
  39.  ld (fallTimer), a
  40. keyInput:
  41.  bcall(_GetCSC)
  42.  cp skleft
  43.  jp z, keyLeft
  44.  cp skright
  45.  jp z,keyRight
  46. softDrop:
  47.  cp skdown
  48.  jp z, keyDown
  49. rotation:
  50.  ; cp skup
  51.  ; jp z, keyUp
  52. locking:
  53.  ld a, (isBlocked)
  54.  cp 0               ; z flag is only set after cp 0
  55.  jp z, notBlocked
  56.  ld a, (lockTimer)
  57.  dec a
  58.  ld (lockTimer), a
  59.  jp z, drawBlocks
  60.  ret
  61. ; ld b, 4            ; the block has been fully locked
  62. ; ld hl, fallingBlocks + 7
  63. ;settingBlocks:
  64. ; ld a, (hl)
  65. ; call divideBy3
  66. ; sla a              ; multiplying by 10
  67. ; ld c, a
  68. ; sla a
  69. ; sla a
  70. ; add a, c
  71. ; ld c, a
  72. ; dec hl
  73. ; ld a, (hl)
  74. ; call divideBy3
  75. ; add a, c
  76. ; push hl
  77. ; ld hl, setBlocks
  78. ; add a, l           ; this will not overflow
  79. ; ld l, a
  80. ; ld (hl), 1
  81. ; pop hl
  82. ; dec hl
  83. ; djnz settingBlocks
  84. ; call drawBlocks
  85. ; call fastCopy
  86. ; jp programStart
  87. notBlocked:
  88.  ld a, (lockTime)
  89.  ld (lockTimer), a
  90. drawBlocks:
  91.  call drawFallingBlocks
  92.  call fastCopy
  93.  call drawFallingBlocks ; erase the blocks through XOR
  94.  jp gameLoop
  95.  ret
  96.  
  97. keyDown:
  98.  ld a, 1
  99.  ld (fallTimer), a
  100.  jp rotation
  101.  
  102. keyLeft:
  103.  ld b, 4
  104.  ld hl, fallingBlocks + 6
  105. leftWallCheckLoop:
  106.  ld a, (hl)
  107.  cp 2
  108.  jp nz, leftNotBlocked
  109.  jp softdrop
  110. leftNotBlocked:
  111.  dec l
  112.  dec l
  113.  djnz leftWallCheckLoop
  114.  ld b, 4
  115.  ld hl, fallingBlocks + 6
  116. keyLeftLoop:
  117.  ld a, (hl)
  118.  sub 3
  119.  ld (hl), a
  120.  dec l
  121.  dec l
  122.  djnz keyLeftLoop
  123.  jp softDrop
  124.  
  125. keyRight:
  126.  ld b, 4
  127.  ld hl, fallingBlocks + 6
  128. rightWallCheckLoop:
  129.  ld a, (hl)
  130.  cp 29
  131.  jp nz, rightNotBlocked
  132.  jp softdrop
  133. rightNotBlocked:
  134.  dec l
  135.  dec l
  136.  djnz rightWallCheckLoop
  137.  ld b, 4
  138.  ld hl, fallingBlocks + 6
  139. keyRightLoop:
  140.  ld a, (hl)
  141.  add a, 3
  142.  ld (hl), a
  143.  dec l
  144.  dec l
  145.  djnz keyRightLoop
  146.  jp softDrop
  147.  
  148. ; drops all 4 falling blocks by one block height
  149. ; input: none
  150. ; output:
  151. ; drops each falling block by one block height
  152. ; destroys: a b hl
  153. dropFallingBlocks:
  154.  ld b, 4
  155.  ld hl, fallingBlocks + 7
  156. fallingBlocksFloorCheckLoop:
  157.  ld a, (hl)
  158.  cp 59
  159.  jp nz, fallNotBlocked
  160.  ld a, 1                ; at least one block is obstructed
  161.  ld (isBlocked), a
  162.  ret
  163. fallNotBlocked:
  164.  dec l
  165.  dec l
  166.  djnz fallingBlocksFloorCheckLoop
  167.  ld b, 4
  168.  ld hl, fallingBlocks + 7
  169. dropFallingBlocksLoop:
  170.  ld a, (hl)
  171.  add a, 3
  172.  ld (hl), a
  173.  dec l
  174.  dec l
  175.  djnz dropFallingBlocksLoop
  176.  ret
  177.  
  178. ; draws the 4 falling blocks
  179. ; input: none
  180. ; output: draws the 4 blocks to the screen
  181. ; destroys: af bc de hl ix
  182. drawFallingBlocks:
  183.  ld b, 4
  184.  ld de, fallingBlocks
  185.  ld a, e
  186.  add a, b
  187.  add a, b
  188.  ld e, a
  189. drawFallingBlocksLoop:
  190.  dec e
  191.  ld a, (de)
  192.  ld l, a
  193.  dec e
  194.  ld a, (de)
  195.  push bc
  196.  push de
  197.  ld ix, block
  198.  ld b, 3
  199.  call putSprite
  200.  pop de
  201.  pop bc
  202.  djnz drawFallingBlocksLoop
  203.  ret
  204.  
  205. ; copies sprite to gbuf
  206. ; destroys: af bc de hl ix
  207. putSprite:
  208.  ld    e,l
  209.  ld    h,$00
  210.  ld    d,h
  211.  add    hl,de
  212.  add    hl,de
  213.  add    hl,hl
  214.  add    hl,hl               ;Find the Y displacement offset
  215.  ld    e,a
  216.  and    $07               ;Find the bit number
  217.  ld    c,a
  218.  srl    e
  219.  srl    e
  220.  srl    e
  221.  add    hl,de             ;Find the X displacement offset
  222.  ld    de,gbuf
  223.  add    hl,de
  224. putSpriteLoop1:
  225. sl1:    ld    d,(ix)             ;loads image byte into D
  226.  ld    e,$00
  227.  ld    a,c
  228.  or    a
  229.  jr    z,putSpriteSkip1
  230. putSpriteLoop2:
  231.  srl    d                  ;rotation to give out smooth moving
  232.  rr    e
  233.  dec    a
  234.  jr    nz,putSpriteLoop2
  235. putSpriteSkip1:
  236.  ld    a,(hl)
  237.  xor    d
  238.  ld    (hl),a
  239.  inc    hl
  240.  ld    a,(hl)
  241.  xor    e
  242.  ld    (hl),a              ;copy to buffer using XOR logic
  243.  ld    de,$0B
  244.  add    hl,de
  245.  inc    ix                   ;Set for next byte of image
  246.  djnz    putSpriteLoop1
  247.  ret
  248.  
  249. ; copies gbuf to the LCD
  250. ; Input: nothing
  251. ; Output: graph buffer is copied to the screen and subsequently cleared
  252. ; destroys: af bc de hl
  253. fastCopy:
  254.  di
  255.  ld a,$80
  256.  out ($10),a
  257.  ld hl,gbuf-12-(-(12*64)+1)
  258.  ld a,$20
  259.  ld c,a
  260.  inc hl  ;filler
  261.  dec hl ;filler
  262. fastCopyAgain:
  263.  ld b,64
  264.  inc c
  265.  ld de,-(12*64)+1
  266.  out ($10),a
  267.  add hl,de
  268.  ld de,10
  269. fastCopyLoop:
  270.  add hl,de
  271.  inc hl  ;filler
  272.  inc hl  ;filler
  273.  inc de ;filler
  274.  ld a,(hl)
  275.  out ($11),a
  276.  dec de ;filler
  277.  djnz fastCopyLoop
  278.  ld a,c
  279.  cp $2B+1
  280.  jr nz,fastCopyAgain
  281.  ret
  282.  
  283. ; sutracts 2 from the a register, divides by 3,
  284. ; and puts the result back into a
  285. ; only works if 0 < a < 62 and if (a - 2) | 3
  286. ; destroys: de
  287. divideBy3:
  288.  cp 20
  289.  jp nc, greaterThan20
  290.  ld de, divisionBy3LookUpTable - 2
  291.  jp division
  292. greaterThan20:
  293.  cp 41
  294.  jp nc, greaterThan41
  295.  ld de, divisionBy3LookUpTable - 22
  296.  jp division
  297. greaterThan41:
  298.  ld de, divisionBy3LookUpTable - 42
  299. division:
  300.  add a, e
  301.  ld e, a
  302.  ld a, (de)
  303.  ret
  304. divisionBy3LookUpTable:
  305.  .db 0
  306.  .db 7
  307.  .db 14
  308.  .db 1
  309.  .db 8
  310.  .db 15
  311.  .db 2
  312.  .db 9
  313.  .db 16
  314.  .db 3
  315.  .db 10
  316.  .db 17
  317.  .db 4
  318.  .db 11
  319.  .db 18
  320.  .db 5
  321.  .db 12
  322.  .db 19
  323.  .db 6
  324.  .db 13
  325.  .db 20
  326.  
  327. ; variables
  328. fallTimer:
  329.  .db %00000000
  330. fallTime:
  331.  .db %01111111
  332. lockTimer:
  333.  .db %00000000
  334. lockTime:
  335.  .db %00111111
  336. isBlocked:
  337.  .db %00000000
  338.  
  339. ; sprites
  340. block:
  341.  .db %11000000
  342.  .db %11000000
  343.  .db %00000000
  344. .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement