Advertisement
Guest User

Untitled

a guest
Aug 4th, 2019
223
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.    ;apply gravity
  13.    call ApplyGravity
  14.  
  15.    ;check Input
  16.    call checkInput
  17.    reti
  18.  
  19. SECTION "Game", ROM0[$150]
  20.  
  21. ApplyGravity:
  22.    ; check for ground
  23.    ld a, [Player_PosY]
  24.    cp a, 144 + 9
  25.    ld b, a
  26.  
  27.    ;check for obstacles
  28.    ld a, [Player_PosY]
  29.    cp a + 9, [Obstacle1_PosY]
  30.    or b
  31.    ld b, a
  32.  
  33.    ld a, [Player_PosY]
  34.    cp a + 9, [Obstacle2_PosY]
  35.    or b
  36.    ld b, a
  37.  
  38.    ld a, [Player_PosY]
  39.    cp a + 9, [Obstacle3_PosY]
  40.    or b
  41.  
  42.    ;jump to .resetJump if no collision found
  43.    jp z, .resetJump
  44.    ; apply gravitation
  45.    inc a
  46.    ld [Player_PosY], a
  47. .end
  48.    ret
  49. .resetJump
  50.    ld a, 1
  51.    ld [canJump], a
  52.    jp .end
  53.  
  54. checkInput:
  55.    ld a, %00100000
  56.    ld [$FF00], a
  57.  
  58.    ld a, [$FF00]
  59.    cpl
  60.    AND %00000001
  61.    cp 1
  62.    call z, moveRight
  63.  
  64.    ld a, [$FF00]
  65.    cpl
  66.    AND %00000010
  67.    cp 2
  68.    call z, moveLeft
  69.  
  70.    ld a, [$FF00]
  71.    cpl
  72.    AND %00000100
  73.    cp 4
  74.    call z, jump
  75.    ret
  76.  
  77. ; move player to the right
  78. moveRight:
  79.    ld a, [Player_PosX]
  80.    inc a
  81.    push af
  82.    cp a, 144 + 18
  83.    jp nc, .reset
  84.    pop af
  85.    ld [Player_PosX], a
  86. .end
  87.    ret
  88. .reset
  89.    pop af
  90.    jp .end
  91.  
  92. ; move player to the left
  93. moveLeft:
  94.    ld a, [Player_PosX]
  95.    dec a
  96.    push af
  97.    cp a, 7
  98.    jp c, .reset
  99.    pop af
  100.    ld [Player_PosX], a
  101. .end
  102.    ret
  103. .reset
  104.    pop af
  105.    jp .end
  106.  
  107. ;jump with player
  108. jump:
  109.    ld a, [canJump]
  110.    and 1
  111.    jp z, .end
  112.    ld a, [Player_PosY]
  113.    sub 20
  114.    ld [Player_PosY], a
  115.  
  116.    ld a, 0
  117.    ld [canJump], a
  118. .end
  119.    ret
  120.  
  121. dma_copy:
  122.     push af                ; store the old a and status reg (f) on the stack so we can use them for our own purposes
  123.     ld a, $C0            ; OamData variable
  124.     ldh [$FF46], a            ; once we put this address into the DMA register, the transfer will begin ($A0 bytes from $C000)
  125.  
  126.     ; now we want to delay for 160us while the data gets copied
  127.     ; this uses a basic countdown-jump back loop
  128.     ld a, $28            ; this is our countdown value ($28 to 0)
  129. dma_copy_wait:
  130.     dec a
  131.     jr nz, dma_copy_wait        ; if a is not 0, jump back to the wait loop
  132.  
  133.     ; once we are here, the dma is all done
  134.     pop af                ; restore af to its old value
  135.     ret                ; and go back to what we were doing
  136. dma_copy_end:
  137.  
  138. ;hl = destination
  139. ;bc = ByteCount
  140. ;de = Source
  141. memcopy:
  142.   ld a, [de]
  143.   ldi [hl], a
  144.   dec bc
  145.   inc de
  146.   ld a, b
  147.   or c
  148.   jr nz, memcopy
  149.   ret
  150.  
  151. ;hl = destination
  152. ;bc = Source
  153. ;a = value
  154. memset:
  155.    push af
  156. .repeat
  157.    pop af
  158.    ldi [hl], a
  159.    dec bc
  160.    push af
  161.    ld a, b
  162.    or c
  163.    jr nz, .repeat
  164.    pop af
  165.    ret
  166.  
  167. ;return if vblank occurs
  168. WaitForVBLANK:
  169.    ld a, [$FF44]
  170.    cp a, 144
  171.    jp nz, WaitForVBLANK
  172.    ret
  173.  
  174. init:
  175.    ;disable interrupts
  176.    di
  177.  
  178.    ;disable screen
  179.    call WaitForVBLANK
  180.    ld a, 0
  181.    ld [$FF40], a
  182.  
  183.    ;remoe Nintendo Logo from VRAM
  184.    ld hl, $8000
  185.    ld bc, $81A0 - $8000
  186.    ld a, 0
  187.    call memset
  188.  
  189.    ld hl, $9904
  190.    ld bc, $9930 - $9904
  191.    ld a, 0
  192.    call memset
  193.  
  194.    ;clear OAM
  195.    ld hl, $C000
  196.    ld bc, $A0
  197.    ld a, 0
  198.    call memset
  199.  
  200.    ;draw background
  201.    ld hl, $9000
  202.    ld bc, Background_END - Background
  203.    ld de, Background
  204.    call memcopy
  205.  
  206.    ;load player sprite to VRAM
  207.    ld hl, $8000
  208.    ld bc, Player_END - Player
  209.    ld de, Player
  210.    call memcopy
  211.  
  212.    ;load obstacle sprite to VRAM
  213.    ld hl, $8010
  214.    ld bc, Obstacle_END - Obstacle
  215.    ld de, Obstacle
  216.    call memcopy
  217.  
  218.    ;save player values in WRAM
  219.    ;set y coordinate
  220.    ld hl, Player_PosY
  221.    ld a, 8
  222.    ldi [hl], a
  223.    ;set x coordinate
  224.    ld a, 20
  225.    ldi [hl], a
  226.    ;set pattern
  227.    ld a, 0
  228.    ldi [hl], a
  229.    ;set flags
  230.    ldi [hl], a
  231.  
  232.    ;save obstacle values in WRAM
  233.    ;set y coordinate of obstacle 1
  234.    ld hl, Obstacle1_PosY
  235.    ld a, 144 + 9
  236.    ldi [hl], a
  237.    ;set x coordinate of obstacle 1
  238.    ld a, 32
  239.    ldi [hl], a
  240.    ;set pattern of obstacle 1
  241.    ld a, 1
  242.    ldi [hl], a
  243.    ;set flags of obstacle 1
  244.    ld a, 0
  245.    ldi [hl], a
  246.  
  247.    ;set y coordinate of obstacle 2
  248.    ld a, 144 + 9
  249.    ldi [hl], a
  250.    ;set x coordinate of obstacle 2
  251.    ld a, 64
  252.    ldi [hl], a
  253.    ;set pattern of obstacle 2
  254.    ld a, 1
  255.    ldi [hl], a
  256.    ;set flags of obstacle 2
  257.    ld a, 0
  258.    ldi [hl], a
  259.  
  260.    ;set y coordinate of obstacle 3
  261.    ld a, 144 + 9
  262.    ldi [hl], a
  263.    ;set x coordinate of obstacle 3
  264.    ld a, 96
  265.    ldi [hl], a
  266.    ;set pattern of obstacle 3
  267.    ld a, 1
  268.    ldi [hl], a
  269.    ;set flags of obstacle 3
  270.    ld a, 0
  271.    ldi [hl], a
  272.  
  273.    ;init variables
  274.    ld a, 0
  275.    ld [canJump], a
  276.  
  277.    ; copy DMA function to HRAM
  278.    ld hl, $FF80
  279.    ld bc, dma_copy_end - dma_copy
  280.    ld de, dma_copy
  281.    call memcopy
  282.  
  283.    ;set color palette
  284.    ld a, %11100100
  285.    ld [$FF47], a
  286.    ld [$FF48], a
  287.  
  288.    ;enable screen
  289.    ld a, %10000011
  290.    ld [$FF40], a
  291.  
  292.    ;enable interrupts
  293.    ld a, %00000001
  294.    ld [$FFFF], a
  295.    ei
  296.  
  297. game:
  298.    halt
  299.    jp game
  300.  
  301. SECTION "OAM", WRAM0[$C000]
  302. ; player
  303. Player_PosY: DS 1
  304. Player_PosX: DS 1
  305. Player_PatternNumber: DS 1
  306. Player_Flags: DS 1
  307.  
  308. ; obstacles
  309. Obstacle1_PosY: DS 1
  310. Obstacle1_PosX: DS 1
  311. Obstacle1_PatternNumber: DS 1
  312. Obstacle1_Flags: DS 1
  313.  
  314. Obstacle2_PosY: DS 1
  315. Obstacle2_PosX: DS 1
  316. Obstacle2_PatternNumber: DS 1
  317. Obstacle2_Flags: DS 1
  318.  
  319. Obstacle3_PosY: DS 1
  320. Obstacle3_PosX: DS 1
  321. Obstacle3_PatternNumber: DS 1
  322. Obstacle3_Flags: DS 1
  323.  
  324. SECTION "Variables", WRAM0[$C100]
  325. canJump: DS 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement