Advertisement
AneMou

Part II - Lesson 17: Input - main.asm

Oct 19th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Z80 Assembler 8.71 KB | Source Code | 0 0
  1. INCLUDE "hardware.inc"
  2.  
  3. SECTION "Header", ROM0[$100]
  4.  
  5.     jp EntryPoint
  6.  
  7.     ds $150 - @, 0 ; Make room for the header
  8.  
  9. EntryPoint:
  10.     ; Do not turn the LCD off outside of VBlank
  11. WaitVBlank:
  12.     ld a, [rLY]
  13.     cp 144
  14.     jp c, WaitVBlank
  15.  
  16.     ; Turn the LCD off
  17.     ld a, 0
  18.     ld [rLCDC], a
  19.  
  20.     ; Copy the tile data
  21.     ld de, Tiles
  22.     ld hl, $9000
  23.     ld bc, TilesEnd - Tiles
  24.     call Memcopy
  25.  
  26.     ; Copy the tilemap
  27.     ld de, Tilemap
  28.     ld hl, $9800
  29.     ld bc, TilemapEnd - Tilemap
  30.     call Memcopy
  31.  
  32.     ; Copy the paddle tile
  33.     ld de, Paddle
  34.     ld hl, $8000
  35.     ld bc, PaddleEnd - Paddle
  36.     call Memcopy
  37.  
  38.     ld a, 0
  39.     ld b, 160
  40.     ld hl, _OAMRAM
  41. ClearOam:
  42.     ld [hli], a
  43.     dec b
  44.     jp nz, ClearOam
  45.  
  46.     ld hl, _OAMRAM
  47.     ld a, 128 + 16
  48.     ld [hli], a
  49.     ld a, 16 + 8
  50.     ld [hli], a
  51.     ld a, 0
  52.     ld [hli], a
  53.     ld [hli], a
  54.  
  55.     ; Turn the LCD on
  56.     ld a, LCDCF_ON | LCDCF_BGON | LCDCF_OBJON
  57.     ld [rLCDC], a
  58.  
  59.     ; During the first (blank) frame, initialize display registers
  60.     ld a, %11100100
  61.     ld [rBGP], a
  62.     ld a, %11100100
  63.     ld [rOBP0], a
  64.  
  65.     ; Initialize global variables
  66.     ld a, 0
  67.     ld [wFrameCounter], a
  68.     ld [wCurKeys], a
  69.     ld [wNewKeys], a
  70.  
  71. Main:
  72.     ld a, [rLY]
  73.     cp 144
  74.     jp nc, Main
  75. WaitVBlank2:
  76.     ld a, [rLY]
  77.     cp 144
  78.     jp c, WaitVBlank2
  79.  
  80.     ; Check the current keys every frame and move left or right.
  81.     call UpdateKeys
  82.  
  83.     ; First, check if the left button is pressed.
  84. CheckLeft: 
  85.     ld a, [wCurKeys]
  86.     and a, PADF_LEFT
  87.     jp z, CheckRight
  88. Left:
  89.     ; Move the paddle one pixel to the left.
  90.     ld a, [_OAMRAM + 1]
  91.     dec a
  92.     ; If we've already hit the edge of the playfield, don't move.
  93.     cp a, 15
  94.     jp z, Main
  95.     ld [_OAMRAM + 1], a
  96.     jp Main
  97.  
  98. ; Then check the right button.
  99. CheckRight:
  100.     ld a, [wCurKeys]
  101.     and a, PADF_RIGHT
  102.     jp z, Main
  103. Right:
  104.     ; Move the paddle one pixel to the right.
  105.     ld a, [_OAMRAM + 1]
  106.     inc a
  107.     ; If we've already hit the edge of the playfield, don't move.
  108.     cp a, 105
  109.     jp z, Main
  110.     ld [_OAMRAM + 1], a
  111.     jp Main
  112.  
  113. UpdateKeys:
  114.   ; Poll half the controller
  115.   ld a, P1F_GET_BTN
  116.   call .onenibble
  117.   ld b, a ; B7-4 = 1; B3-0 = unpressed buttons
  118.  
  119.   ; Poll the other half
  120.   ld a, P1F_GET_DPAD
  121.   call .onenibble
  122.   swap a ; A3-0 = unpressed directions; A7-4 = 1
  123.   xor a, b ; A = pressed buttons + directions
  124.   ld b, a ; B = pressed buttons + directions
  125.  
  126.   ; And release the controller
  127.   ld a, P1F_GET_NONE
  128.   ldh [rP1], a
  129.  
  130.   ; Combine with previous wCurKeys to make wNewKeys
  131.   ld a, [wCurKeys]
  132.   xor a, b ; A = keys that changed state
  133.   and a, b ; A = keys that changed to pressed
  134.   ld [wNewKeys], a
  135.   ld a, b
  136.   ld [wCurKeys], a
  137.   ret
  138.  
  139. .onenibble
  140.   ldh [rP1], a ; switch the key matrix
  141.   call .knownret ; burn 10 cycles calling a known ret
  142.   ldh a, [rP1] ; ignore value while waiting for the key matrix to settle
  143.   ldh a, [rP1]
  144.   ldh a, [rP1] ; this read counts
  145.   or a, $F0 ; A7-4 = 1; A3-0 = unpressed keys
  146. .knownret
  147.   ret
  148.  
  149. ; Copy bytes from one area to another.
  150. ; @param de: Source
  151. ; @param hl: Destination
  152. ; @param bc: Length
  153. Memcopy:
  154.     ld a, [de]
  155.     ld [hli], a
  156.     inc de
  157.     dec bc
  158.     ld a, b
  159.     or a, c
  160.     jp nz, Memcopy
  161.     ret
  162.  
  163. Tiles:
  164.     dw `33333333
  165.     dw `33333333
  166.     dw `33333333
  167.     dw `33322222
  168.     dw `33322222
  169.     dw `33322222
  170.     dw `33322211
  171.     dw `33322211
  172.     dw `33333333
  173.     dw `33333333
  174.     dw `33333333
  175.     dw `22222222
  176.     dw `22222222
  177.     dw `22222222
  178.     dw `11111111
  179.     dw `11111111
  180.     dw `33333333
  181.     dw `33333333
  182.     dw `33333333
  183.     dw `22222333
  184.     dw `22222333
  185.     dw `22222333
  186.     dw `11222333
  187.     dw `11222333
  188.     dw `33333333
  189.     dw `33333333
  190.     dw `33333333
  191.     dw `33333333
  192.     dw `33333333
  193.     dw `33333333
  194.     dw `33333333
  195.     dw `33333333
  196.     dw `33322211
  197.     dw `33322211
  198.     dw `33322211
  199.     dw `33322211
  200.     dw `33322211
  201.     dw `33322211
  202.     dw `33322211
  203.     dw `33322211
  204.     dw `22222222
  205.     dw `20000000
  206.     dw `20111111
  207.     dw `20111111
  208.     dw `20111111
  209.     dw `20111111
  210.     dw `22222222
  211.     dw `33333333
  212.     dw `22222223
  213.     dw `00000023
  214.     dw `11111123
  215.     dw `11111123
  216.     dw `11111123
  217.     dw `11111123
  218.     dw `22222223
  219.     dw `33333333
  220.     dw `11222333
  221.     dw `11222333
  222.     dw `11222333
  223.     dw `11222333
  224.     dw `11222333
  225.     dw `11222333
  226.     dw `11222333
  227.     dw `11222333
  228.     dw `00000000
  229.     dw `00000000
  230.     dw `00000000
  231.     dw `00000000
  232.     dw `00000000
  233.     dw `00000000
  234.     dw `00000000
  235.     dw `00000000
  236.     dw `11001100
  237.     dw `11111111
  238.     dw `11111111
  239.     dw `21212121
  240.     dw `22222222
  241.     dw `22322232
  242.     dw `23232323
  243.     dw `33333333
  244.     ; Paste your logo here:
  245.     dw `33000000
  246.     dw `33000000
  247.     dw `33000000
  248.     dw `33000000
  249.     dw `33111100
  250.     dw `33111100
  251.     dw `33111111
  252.     dw `33111111
  253.     dw `33331111
  254.     dw `00331111
  255.     dw `00331111
  256.     dw `00331111
  257.     dw `00331111
  258.     dw `00331111
  259.     dw `11331111
  260.     dw `11331111
  261.     dw `11333300
  262.     dw `11113300
  263.     dw `11113300
  264.     dw `11113300
  265.     dw `11113311
  266.     dw `11113311
  267.     dw `11113311
  268.     dw `11113311
  269.     dw `00003333
  270.     dw `00000033
  271.     dw `00000033
  272.     dw `00000033
  273.     dw `11000033
  274.     dw `11000033
  275.     dw `11111133
  276.     dw `11111133
  277.     dw `33111111
  278.     dw `33111111
  279.     dw `33111111
  280.     dw `33111111
  281.     dw `33111111
  282.     dw `33111111
  283.     dw `33111111
  284.     dw `33111111
  285.     dw `11331111
  286.     dw `11331111
  287.     dw `11331111
  288.     dw `11331111
  289.     dw `11331111
  290.     dw `11331111
  291.     dw `11331111
  292.     dw `11331111
  293.     dw `11113311
  294.     dw `11113311
  295.     dw `11113311
  296.     dw `11113311
  297.     dw `11113311
  298.     dw `11113311
  299.     dw `11113311
  300.     dw `11113311
  301.     dw `11111133
  302.     dw `11111133
  303.     dw `11111133
  304.     dw `11111133
  305.     dw `11111133
  306.     dw `11111133
  307.     dw `11111133
  308.     dw `11111133
  309.     dw `33111111
  310.     dw `33111111
  311.     dw `33111111
  312.     dw `33111111
  313.     dw `33111111
  314.     dw `33111111
  315.     dw `33111111
  316.     dw `33111111
  317.     dw `11331111
  318.     dw `11331111
  319.     dw `11331111
  320.     dw `11331111
  321.     dw `11331111
  322.     dw `11331111
  323.     dw `11331111
  324.     dw `11331111
  325.     dw `11113311
  326.     dw `11113311
  327.     dw `11113311
  328.     dw `11113311
  329.     dw `11113311
  330.     dw `11113311
  331.     dw `11113311
  332.     dw `11113311
  333.     dw `11111133
  334.     dw `11111133
  335.     dw `11111133
  336.     dw `11111133
  337.     dw `11111133
  338.     dw `11111133
  339.     dw `11111133
  340.     dw `11111133
  341.     dw `33111111
  342.     dw `33111111
  343.     dw `33111111
  344.     dw `33111111
  345.     dw `33111111
  346.     dw `33111111
  347.     dw `33111111
  348.     dw `33111111
  349.     dw `11331111
  350.     dw `11331111
  351.     dw `11331111
  352.     dw `11331111
  353.     dw `11330000
  354.     dw `11330000
  355.     dw `11330000
  356.     dw `33330000
  357.     dw `11113311
  358.     dw `11113311
  359.     dw `00003311
  360.     dw `00003311
  361.     dw `00003311
  362.     dw `00003311
  363.     dw `00003311
  364.     dw `00333311
  365.     dw `11111133
  366.     dw `11111133
  367.     dw `11111133
  368.     dw `11111133
  369.     dw `11111133
  370.     dw `11111133
  371.     dw `11111133
  372.     dw `11113333
  373. TilesEnd:
  374.  
  375. Tilemap:
  376.     db $00, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $02, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  377.     db $04, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  378.     db $04, $08, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  379.     db $04, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  380.     db $04, $08, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  381.     db $04, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  382.     db $04, $08, $05, $06, $05, $06, $05, $06, $05, $06, $05, $06, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  383.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  384.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  385.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  386.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  387.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  388.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  389.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $0A, $0B, $0C, $0D, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  390.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $0E, $0F, $10, $11, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  391.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $12, $13, $14, $15, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  392.     db $04, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $07, $03, $16, $17, $18, $19, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  393.     db $04, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $07, $03, $03, $03, $03, $03, $03, 0,0,0,0,0,0,0,0,0,0,0,0
  394. TilemapEnd:
  395.  
  396. Paddle:
  397.     dw `13333331
  398.     dw `30000003
  399.     dw `13333331
  400.     dw `00000000
  401.     dw `00000000
  402.     dw `00000000
  403.     dw `00000000
  404.     dw `00000000
  405. PaddleEnd:
  406.  
  407. SECTION "Counter", WRAM0
  408. wFrameCounter: db
  409.  
  410. SECTION "Input Variables", WRAM0
  411. wCurKeys: db
  412. wNewKeys: db
  413.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement