Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. include "tileset.asm"
  2.  
  3. SECTION "Start", ROM0[$100]
  4. EntryPoint:
  5.    jp init
  6.  
  7. SECTION "VBLANK", ROM0[$040]
  8. VBLANK:
  9.    ;update oam
  10.    call $FF80
  11.  
  12.    ;check collision
  13.    call checkCollision
  14.  
  15.    ;check Input
  16.    call checkInput
  17.    reti
  18.  
  19. SECTION "Game", ROM0[$150]
  20.  
  21. checkCollision:
  22.    ld a, [Ball_PosY]
  23.    cp a, 144 + 9
  24.    jp z, .end
  25.    inc a
  26.    ld [Ball_PosY], a
  27. .end
  28.    ret
  29.  
  30. checkInput:
  31.    ld a, %00100000
  32.    ld [$FF00], a
  33.    ld a, [$FF00]
  34.    cpl
  35.    AND %00000001
  36.    cp 0
  37.    call z, moveLeft
  38.  
  39.    ld a, [$FF00]
  40.    cpl
  41.    AND %00000010
  42.    cp 0
  43.    call z, moveRight
  44.    ret
  45.  
  46. moveRight:
  47.    ld a, [Ball_PosX]
  48.    inc a
  49.    ld [Ball_PosX], a
  50.    ret
  51.  
  52. moveLeft:
  53.    ld a, [Ball_PosX]
  54.    dec a
  55.    ld [Ball_PosX], a
  56.    ret
  57.  
  58. dma_copy:
  59.     push af                ; store the old a and status reg (f) on the stack so we can use them for our own purposes
  60.     ld a, $C0            ; OamData variable
  61.     ldh [$FF46], a            ; once we put this address into the DMA register, the transfer will begin ($A0 bytes from $C000)
  62.  
  63.     ; now we want to delay for 160us while the data gets copied
  64.     ; this uses a basic countdown-jump back loop
  65.     ld a, $28            ; this is our countdown value ($28 to 0)
  66. dma_copy_wait:
  67.     dec a
  68.     jr nz, dma_copy_wait        ; if a is not 0, jump back to the wait loop
  69.  
  70.     ; once we are here, the dma is all done
  71.     pop af                ; restore af to its old value
  72.     ret                ; and go back to what we were doing
  73. dma_copy_end:
  74.  
  75. ;hl = destination
  76. ;bc = ByteCount
  77. ;de = Source
  78. memcopy:
  79.   ld a, [de]
  80.   ldi [hl], a
  81.   dec bc
  82.   inc de
  83.   ld a, b
  84.   or c
  85.   jr nz, memcopy
  86.   ret
  87.  
  88. ;hl = destination
  89. ;bc = Source
  90. ;a = value
  91. memset:
  92.    push af
  93. .repeat
  94.    pop af
  95.    ldi [hl], a
  96.    dec bc
  97.    push af
  98.    ld a, b
  99.    or c
  100.    jr nz, .repeat
  101.    pop af
  102.    ret
  103.  
  104. WaitForVBLANK:
  105.    ld a, [$FF44]
  106.    cp a, 144
  107.    jp nz, WaitForVBLANK
  108.    ret
  109.  
  110. init:
  111.    ;disable interrupts
  112.    di
  113.  
  114.    ;disable screen
  115.    call WaitForVBLANK
  116.    ld a, 0
  117.    ld [$FF40], a
  118.  
  119.    ;remoe Nintendo Logo from VRAM
  120.    ld hl, $8000
  121.    ld bc, $81A0 - $8000
  122.    ld a, 0
  123.    call memset
  124.  
  125.    ld hl, $9904
  126.    ld bc, $9930 - $9904
  127.    ld a, 0
  128.    call memset
  129.  
  130.    ;clear OAM
  131.    ld hl, $C000
  132.    ld bc, $A0
  133.    ld a, 0
  134.    call memset
  135.  
  136.    ;draw background
  137.    ld hl, $9000
  138.    ld bc, Background_END - Background
  139.    ld de, Background
  140.    call memcopy
  141.  
  142.    ;draw Ball
  143.    ld hl, $8000
  144.    ld bc, Ball_END - Ball
  145.    ld de, Ball
  146.    call memcopy
  147.  
  148.    ;save ball values in WRAM
  149.    ;set x coordinate
  150.    ld hl, Ball_PosY
  151.    ld a, 8
  152.    ldi [hl], a
  153.    ;set x coordinate
  154.    ld a, 20
  155.    ldi [hl], a
  156.  
  157.    ld a, 0
  158.    ;set pattern
  159.    ldi [hl], a
  160.  
  161.    ;set flags
  162.    ldi [hl], a
  163.  
  164.    ; copy DMA function to HRAM
  165.    ld hl, $FF80
  166.    ld bc, dma_copy_end - dma_copy
  167.    ld de, dma_copy
  168.    call memcopy
  169.  
  170.    ;set color palette
  171.    ld a, %11100100
  172.    ld [$FF47], a
  173.    ld [$FF48], a
  174.  
  175.    ;enable screen
  176.    ld a, %10000011
  177.    ld [$FF40], a
  178.  
  179.    ;enable interrupts
  180.    ld a, %00000001
  181.    ld [$FFFF], a
  182.    ei
  183.  
  184. game:
  185.    halt
  186.    jp game
  187.  
  188. SECTION "OAM", WRAM0[$C000]
  189. Ball_PosY: DS 1
  190. Ball_PosX: DS 1
  191. Ball_PatternNumber: DS 1
  192. Ball_Flags: DS 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement