Advertisement
Leap_

fdsfsdfsd

Jul 27th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.26 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. JOYPAD_REGISTER equ $00 ; joypad
  6. PAD_PORT_DPAD equ %00100000 ; select d-pad buttons
  7. PAD_PORT_BUTTONS equ %00010000 ; select other buttons
  8. PAD_OUTPUT_MASK equ %00001111 ; mask for the output buttons
  9. DPAD_DOWN equ 7 ; down is 7, example: cp 7 is it the down button?
  10. DPAD_UP equ 6 ; up is 6
  11. DPAD_LEFT equ 5 ; left is 5
  12. DPAD_RIGHT equ 4 ; right is 4
  13. START_BUTTON equ 3
  14. SELECT_BUTTON equ 2
  15. B_BUTTON equ 1
  16. A_BUTTON equ 0 ; down to here
  17. DPAD_DOWN_MASK equ %10000000 ; masks for the buttons
  18. DPAD_UP_MASK equ %01000000
  19. DPAD_LEFT_MASK equ %00100000
  20. DPAD_RIGHT_MASK equ %00010000
  21. START_BUTTON_MASK equ %00001000
  22. SELECT_BUTTON_MASK equ %00000100
  23. B_BUTTON_MASK equ %00000010
  24. A_BUTTON_MASK equ %00000001
  25.  
  26. DIV_REGISTER equ $04 ; divide timer... read to get time, write to reset it to 0
  27. TIMA_REGISTER equ $05 ; main timer... freq is set in TAC reg, generates interupt when overflows
  28. TMA_REGISTER equ $06 ; Timer Modulo... main timer loaded with this value after it overflows
  29. TAC_REGISTER equ $07 ; Timer Control
  30. TIMER_STOP equ %00000100 ; timer halt flag... 0=stop, 1=run
  31. TIMER_FREQ_MASK equ %00000011 ; mask for timer frequency bits
  32. TIMER_FREQ_4KHz equ %00000000 ; main timer runs at 4.096 KHz
  33. TIMER_FREQ_262KHz equ %00000001 ; main timer runs at 262.144 KHz
  34. TIMER_FREQ_65KHZ equ %00000010 ; main timer runs at 65.536 KHz
  35. TIMER_FREQ_16KHz equ %00000011 ; main timer runs at 15.384 KHz
  36.  
  37. IRQ_FLAG_REGISTER equ $0F ; Interrupt Flag
  38. VBLANK_INT equ %00000001 ; bit 0 = vblank interrupt on/off
  39. LCDC_INT equ %00000010 ; bit 1 = LCDC interrupt on/off
  40. TIMER_INT equ %00000100 ; bit 2 = Timer Overflow interrupt on/off
  41. SERIAL_INT equ %00001000 ; bit 3 = Serial I/O Transfer Completion interrupt on/off
  42. CONTROLLER_INT equ %00010000 ; bit 4 = ??
  43.  
  44. LCDC_CONTROL equ $40 ; LCD (Graphics) Control
  45. BKG_DISP_FLAG equ %00000001 ; bit 0 = background tile map is on if set
  46. SPRITE_DISP_FLAG equ %00000010 ; bit 1 = sprites are on if set
  47. SPRITE_DISP_SIZE equ %00000100 ; bit 2 = sprite size (0=8x8 pixels, 1=16x8)
  48. BKG_MAP_LOC equ %00001000 ; bit 3 = background tile map location (0=$9800-$9bff, 1=$9c00-$9fff)
  49. TILES_LOC equ %00010000 ; bit 4 = tile data location (0=$8800-$97ff, 1=$8000-$8fff)
  50. WINDOW_DISP_FLAG equ %00100000 ; bit 5 = window tile map is on if set
  51. WINDOW_MAP_LOC equ %01000000 ; bit 6 = window tile map location (0=$9800-$9bff, 1=$9c00-9fff)
  52. DISPLAY_FLAG equ %10000000 ; bit 7 = LCD display on if set
  53.  
  54. LCDC_STATUS equ $41 ; LCDC Status
  55. DISP_CYCLE_MODE equ %00000011 ; mask for the display cycle mode bits
  56. VBLANK_MODE equ %00000000 ; system is in vertical blanking interval
  57. HBLANK_MODE equ %00000001 ; system is in a horizontal blanking interval
  58. SPRITE_MODE equ %00000010 ; system is reading sprite RAM
  59. LCD_TRANSFER equ %00000011 ; system is transfering data to the LCD driver
  60.  
  61. SCROLL_BKG_Y equ $42 ; vertical scroll position of background tile map
  62. SCROLL_BKG_X equ $43 ; horizontal scroll position of background tile map
  63.  
  64. LCDC_LY_COUNTER equ $44 ; increments every scan line (0..143 = display, 144-153 = vblank)
  65. LY_COMPARE equ $45 ; ??
  66.  
  67. DMA_REGISTER equ $46 ; DMA Transfer and Start Address
  68.  
  69. PALETTE_BKG equ $47 ; palette data for background tile map
  70. PALETTE_SPRITE_0 equ $48 ; sprite palette 0 data
  71. PALETTE_SPRITE_1 equ $49 ; sprite palette 1 data
  72.  
  73. POS_WINDOW_Y equ $4A ; window tile map Y position
  74. POS_WINDOW_X equ $4B ; window tile map X position
  75.  
  76. INTERRUPT_ENABLE equ $ff ; Interrupt Enable
  77.  
  78. ; $ff80 to $fffe is 128 bytes of internal RAM
  79. STACK_TOP equ $fff4 ; put the stack here
  80.  
  81. ; video ram display locations
  82. TILES_MEM_LOC_0 equ $8800 ; tile map tiles only
  83. TILES_MEM_LOC_1 equ $8000 ; tile maps and sprite tiles
  84.  
  85. MAP_MEM_LOC_0 equ $9800 ; background and window tile maps
  86. MAP_MEM_LOC_1 equ $9c00 ; (select which uses what mem loc in LCDC_CONTROL register)
  87.  
  88. SPRITE_ATTRIB_MEM_LOC equ $fe00 ; OAM memory (sprite attributes)
  89.  
  90. ; sprite attribute flags
  91. SPRITE_FLAGS_PAL equ %00010000 ; palette (0=sprite pal 0, 1=sprite pal 1)
  92. SPRITE_FLAGS_XFLIP equ %00100000 ; sprite is horizontal flipped
  93. SPRITE_FLAGS_YFLIP equ %01000000 ; sprite is vertical flipped
  94. SPRITE_FLAGS_PRIORITY equ %10000000 ; sprite display priority (0=on top bkg & win, 1=behind bkg & win)
  95.  
  96. BYTES_PER_TILE = 16 ; 16*5
  97.  
  98.  
  99.  
  100. ;-------------------------------------------------------------------------
  101. ; start of the game rom (address 0000)
  102. ;-------------------------------------------------------------------------
  103. SECTION "rst 00", ROM0 [$00]
  104. rst $38
  105. SECTION "rst 08", ROM0 [$08]
  106. rst $38
  107. SECTION "rst 10", ROM0 [$10]
  108. rst $38
  109. SECTION "rst 18", ROM0 [$18]
  110. rst $38
  111. SECTION "rst 20", ROM0 [$20]
  112. rst $38
  113. SECTION "rst 28", ROM0 [$28]
  114. rst $38
  115. SECTION "rst 30", ROM0 [$30]
  116. rst $38
  117. SECTION "rst 38", ROM0 [$38]
  118. rst $38
  119.  
  120. ; NOTE: the hardware requires the interrupt jumps to be at these addresses
  121.  
  122. SECTION "VBlank_IRQ_Jump",HOME[$0040]
  123. ; Vertical Blanking interrupt
  124. jp VBlankFunc
  125.  
  126. SECTION "LCDC_IRQ_Jump",HOME[$0048]
  127. ; LCDC Status interrupt (can be set for H-Blanking interrupt)
  128. rst $38
  129.  
  130. SECTION "Timer_Overflow_IRQ_Jump",HOME[$0050]
  131. ; Main Timer Overflow interrupt
  132. reti
  133.  
  134. SECTION "Serial_IRQ_Jump",HOME[$0058]
  135. ; Serial Transfer Completion interrupt
  136. reti
  137.  
  138. SECTION "Joypad_IRQ_Jump",HOME[$0060]
  139. ; Joypad Button Interrupt
  140. reti
  141.  
  142.  
  143. SECTION "Entry",HOME[$0100]
  144. nop ; one cpu cycle (four clock cycles)
  145. jp title_stuff ; jump to our code
  146.  
  147. SECTION "GameBoy_Header_Start",HOME[$0104]
  148. ds $150 - $104 ; rgbfix
  149.  
  150.  
  151.  
  152. SECTION "Game_Code_Start",HOME[$0150]
  153. ; begining of game code
  154. ; this routine sets up some registers and values, and starts the game
  155. title_stuff::
  156. ld sp, STACK_TOP ; put stack where it wants it
  157. ld a, VBLANK_INT ; get the vblank int
  158. ldh [INTERRUPT_ENABLE], a ; load it into the interrupt enable
  159. sub a ; default accumulator value is 1, apparently
  160. ldh [LCDC_STATUS], a ; 0
  161. ldh [LCDC_CONTROL], a ; 0
  162. ld [vblank_flag], a ; reset vblank flag
  163.  
  164. ; load the tiles
  165.  
  166. ld bc, TitleTiles ; black and white tiles for our title screen
  167. call LoadTitleTiles
  168.  
  169. ld bc, TitleScreenData ; our title screen map, is supported by our tile set which was loaded above.
  170. call LoadTitleScreen
  171.  
  172. call InitSprites ; clear sprites
  173.  
  174. call InitPalettes ; init the palettes
  175. ; setup the screen and other formats
  176. ld a, DISPLAY_FLAG | BKG_DISP_FLAG | SPRITE_DISP_FLAG | TILES_LOC | WINDOW_MAP_LOC ; load this
  177. ldh [LCDC_CONTROL], a ; into the LCD control
  178. call wait_for_key ; wait for a key to be pressed
  179.  
  180.  
  181.  
  182. ; this routine loads our title screen and shows it.
  183. LoadTitleScreen::
  184. ld hl, MAP_MEM_LOC_0 ; lcdc control must be 0
  185.  
  186. ld de, 4 * 16
  187. ld e, 5 ; number of tiles to load
  188.  
  189. .tile
  190. ld d, BYTES_PER_TILE
  191. .byte
  192. .wait
  193. ld a, [LCDC_STATUS]
  194. and SPRITE_MODE
  195. jr nz, .wait
  196. ld a, [bc]
  197. ld [hli], a
  198. inc bc
  199. dec d
  200. jr nz, .byte
  201. dec e
  202. jr nz, .tile
  203. ret
  204.  
  205.  
  206.  
  207.  
  208. ; this function waits for A to be pressed
  209. ; when A is pressed, it starts the game, loads the sprites, and the main stuff
  210.  
  211. wait_for_key::
  212. call ReadJoypad ; read the joypad
  213. ld a, [joypad_down] ; get the button pressed
  214. bit A_BUTTON, a ; did the player press the a button?
  215. jr z, .key_loop ; if not, keep looping
  216. jp start ; otherwise, if the a button was pressed, start the game
  217.  
  218. .key_loop
  219. jr wait_for_key ; loop
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. WaitVBL::
  227. ld a, [vblank_flag]
  228. cp 0 ; are we in vblank
  229. jr WaitVBL ; keep looping......
  230. ret
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. LoadTitleTiles::
  238. ld bc, TitleTiles
  239. ld hl, TILES_MEM_LOC_0
  240. ld e, 5
  241. .tile
  242. ld d, BYTES_PER_TILE
  243. .byte
  244. .wait
  245. ld a, [LCDC_STATUS]
  246. and SPRITE_MODE
  247. jr nz, .wait
  248. ld a, [bc]
  249. ld [hli], a
  250. inc bc
  251. dec d
  252. jr nz, .byte
  253. dec e
  254. jr nz, .tile
  255. ret
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262. start::
  263. ld a, 1
  264. ld [LCDC_CONTROL], a ; tiles/map must be loaded into "1" bank
  265. xor a
  266. ld a, 16
  267. ldh [SCROLL_BKG_X], a ; background map will start at 16,16
  268. ldh [SCROLL_BKG_Y], a
  269.  
  270. call WaitVBL
  271. ld bc, TileData ; our overworld tileset
  272. call LoadTiles
  273. ; load the background map
  274. ld bc, BkgMapData ; our world map
  275. call LoadMapToBkg
  276.  
  277.  
  278.  
  279.  
  280. ; init player sprite
  281. ld a, $40
  282. ld [spaceship_xpos], a
  283. ld [spaceship_ypos], a
  284. ld a, 2
  285. ld [spaceship_tile], a
  286. ld a, 0
  287. ld [spaceship_flags], a
  288.  
  289.  
  290. ; allow interrupts to start occuring
  291. ei
  292.  
  293. jp Game_Loop
  294.  
  295.  
  296.  
  297.  
  298. ; main game loop
  299. Game_Loop::
  300. ; don't do a frame update unless we have had a vblank
  301. ld a, [vblank_flag]
  302. cp 0
  303. jp z, .end_game_loop
  304.  
  305. ; get this frame's joypad info
  306. call ReadJoypad
  307.  
  308. ; adjust sprite due to d-pad presses
  309. call MoveSpaceship ; yeah I know, I'm dumb
  310.  
  311. ; reset vblank flag
  312. ld a, 0
  313. ld [vblank_flag], a
  314.  
  315. .end_game_loop
  316. ; time to loop!
  317. jp Game_Loop
  318.  
  319.  
  320.  
  321.  
  322. ;-----------------------------------------------------------------------
  323. ; copy a block of data
  324. ;
  325. ; in: de - destination ptr
  326. ; hl - source ptr
  327. ; b - number of bytes to copy
  328. ;-----------------------------------------------------------------------
  329. CopyBlock::
  330. push af ; save af
  331.  
  332. .copy_block_loop
  333. ld a, [hli] ; inc hl
  334. ld [de], a ; load it into de
  335. inc de ; inc that
  336. dec b ; dec that
  337. jr nz, .copy_block_loop ; then loop
  338.  
  339. pop af ; restore af and return
  340. ret ;
  341.  
  342.  
  343.  
  344.  
  345. ;------------------------------------------
  346. ; init the local copy of the sprites
  347. ;------------------------------------------
  348. InitSprites::
  349. ld hl, $c000 ; my sprites are at $c000
  350. ld b, 40*4 ; 40 sprites, 4 bytes per sprite
  351. ld a, $ff
  352. .init_sprites_loop
  353. ld [hli], a
  354. dec b
  355. jr nz, .init_sprites_loop
  356. ret
  357.  
  358.  
  359.  
  360. ;----------------------------------------------------
  361. ; load the tiles from ROM into the tile video memory
  362. ;
  363. ; IN: bc = address of tile data to load
  364. ;----------------------------------------------------
  365. LoadTiles::
  366. ld hl, TILES_MEM_LOC_1 ; load the tiles to tiles bank 1
  367.  
  368. ld de, 4 * 16
  369. ld e, 5
  370. .tile
  371. ld d, BYTES_PER_TILE
  372. .byte
  373. .wait
  374. ld a, [LCDC_STATUS]
  375. and SPRITE_MODE
  376. jr nz, .wait
  377. ld a, [bc]
  378. ld [hli], a
  379. inc bc
  380. dec d
  381. jr nz, .byte
  382. dec e
  383. jr nz, .tile
  384. ret
  385.  
  386.  
  387.  
  388.  
  389.  
  390. ;----------------------------------------------------
  391. ; load the tile map to the background
  392. ;
  393. ; IN: bc = address of map to load
  394. ;----------------------------------------------------
  395. LoadMapToBkg::
  396. ld hl, MAP_MEM_LOC_1 ; load the map to map bank 1
  397.  
  398. ld e, 5 ; number of tiles to load
  399. .tile
  400. ld d, BYTES_PER_TILE
  401. .byte
  402. .wait
  403. ld a, [LCDC_STATUS]
  404. and SPRITE_MODE
  405. jr nz, .wait
  406. ld a, [bc]
  407. ld [hli], a
  408. inc bc
  409. dec d
  410. jr nz, .byte
  411. dec e
  412. jr nz, .tile
  413. ret
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420. ;----------------------------------------------------
  421. ; init the palettes to basic
  422. ;----------------------------------------------------
  423. InitPalettes::
  424. ld a, %10010011 ; set palette colors
  425.  
  426. ; load it to all the palettes
  427. ldh [PALETTE_BKG], a
  428. ldh [PALETTE_SPRITE_0], a
  429. ldh [PALETTE_SPRITE_1], a
  430.  
  431. ret
  432.  
  433.  
  434.  
  435. ;-----------------------------------------------------------------------
  436. ; read the joypad
  437. ;
  438. ; output:
  439. ; This loads two variables:
  440. ; joypad_held - what buttons are currently held
  441. ; joypad_down - what buttons went down since last joypad read
  442. ;-----------------------------------------------------------------------
  443. ReadJoypad::
  444. ; get the d-pad buttons
  445. ld a, PAD_PORT_DPAD ; select d-pad
  446. ldh [JOYPAD_REGISTER], a ; send it to the joypad
  447. ldh a, [JOYPAD_REGISTER]
  448. ldh a, [JOYPAD_REGISTER]
  449. ldh a, [JOYPAD_REGISTER]
  450. ldh a, [JOYPAD_REGISTER]
  451. ldh a, [JOYPAD_REGISTER]
  452. ldh a, [JOYPAD_REGISTER] ; get the result back (takes a few cycles)
  453. cpl ; bit-flip the result
  454. ; ld b, a
  455. and PAD_OUTPUT_MASK ; mask out the output bits
  456. swap a ; put the d-pad button results to top nibble
  457. ld b, a ; and store it
  458.  
  459. ; get A / B / SELECT / START buttons
  460. ld a, PAD_PORT_BUTTONS ; select buttons
  461. ldh [JOYPAD_REGISTER], a ; send it to the joypad
  462. ldh a, [JOYPAD_REGISTER]
  463. ldh a, [JOYPAD_REGISTER]
  464. ldh a, [JOYPAD_REGISTER]
  465. ldh a, [JOYPAD_REGISTER]
  466. ldh a, [JOYPAD_REGISTER]
  467. ldh a, [JOYPAD_REGISTER] ; get the result back (takes even more cycles?)
  468. cpl ; bit-flip the result
  469. and PAD_OUTPUT_MASK ; mask out the output bits
  470. or b ; add it to the other button bits
  471. ld b, a ; put it back in c
  472.  
  473. ; calculate the buttons that went down since last joypad read
  474. ld a, [joypad_held] ; grab last button bits
  475. cpl ; invert them
  476. and b ; combine the bits with current bits
  477. ld [joypad_down], a ; store just-went-down button bits
  478.  
  479. ld a, b
  480. ld [joypad_held], a ; store the held down button bits
  481.  
  482. ld a, $30 ; reset joypad
  483. ldh [JOYPAD_REGISTER],A
  484.  
  485. ret ; done
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492. ;---------------------------------------------------
  493. ; my vblank routine - do all graphical changes here
  494. ; while the display is not drawing
  495. ;---------------------------------------------------
  496. VBlankFunc::
  497. di ; disable interrupts
  498. push af
  499.  
  500. ; increment my little timer
  501. ld a, [ScrollTimer] ; get the scroll timer
  502. inc a ; increment it
  503. ld [ScrollTimer], a
  504.  
  505.  
  506.  
  507. ; load the sprite attrib table to OAM memory
  508. .vblank_sprite_DMA
  509. ld a, $c0 ; dma from $c000 (where I have my local copy of the attrib table)
  510. ldh [DMA_REGISTER], a ; start the dma
  511.  
  512. ld a, $28 ; wait for 160 microsec (using a loop)
  513. .vblank_dma_wait
  514. dec a
  515. jr nz, .vblank_dma_wait
  516.  
  517. ld hl, SPRITE_ATTRIB_MEM_LOC
  518.  
  519. ; set the vblank occured flag
  520. ld a, 1
  521. ld [vblank_flag], a
  522.  
  523.  
  524. pop af
  525. ei ; enable interrupts
  526. reti ; and done
  527.  
  528.  
  529.  
  530.  
  531. MoveSpaceship::
  532. push af
  533.  
  534. ; check buttons for d-pad presses
  535. .check_for_up
  536. ld a, [joypad_held]
  537. bit DPAD_UP, a
  538. jp z, .check_for_down ; if button not pressed then done
  539.  
  540. ; up was held down
  541. .check_for_upright
  542. ; is right also held?
  543. ld a, [joypad_held]
  544. bit DPAD_RIGHT, a
  545. jp z, .check_for_upleft
  546.  
  547. ; up + right held, so sprite needs to be diagonal
  548. ld a, 1 ; diagonal sprite is tile 1
  549. ld [spaceship_tile], a
  550. ld a, 0 ; flags are no x or y flip
  551. ld [spaceship_flags], a
  552.  
  553. jp .adjust_up_pos
  554.  
  555. .check_for_upleft
  556. ; is left also held?
  557. ld a, [joypad_held]
  558. bit DPAD_LEFT, a
  559. jp z, .set_up_only
  560.  
  561. ; up + left held, so sprite needs to be diagonal
  562. ld a, 1 ; diagonal sprite is tile 1
  563. ld [spaceship_tile], a
  564. ld a, SPRITE_FLAGS_XFLIP ; sprite should be x flipped
  565. ld [spaceship_flags], a
  566.  
  567. jp .adjust_up_pos
  568.  
  569. .set_up_only
  570. ; only up was held, so sprite needs to be up
  571. ld a, 0 ; vertical sprite is tile 0
  572. ld [spaceship_tile], a
  573. ld a, 0
  574. ld [spaceship_flags], a
  575.  
  576. .adjust_up_pos
  577. ; adjust the sprite's position
  578. ld a, [ScrollTimer] ; only move sprite every 2nd vblank
  579. and %00000001
  580. jr nz, .check_for_left
  581.  
  582. ; move sprite up a pixel
  583. ld a, [spaceship_ypos]
  584. dec a
  585. ld [spaceship_ypos], a
  586.  
  587. ; don't check down, since up + down should never occur
  588. jp .check_for_left
  589.  
  590. .check_for_down
  591. ld a, [joypad_held]
  592. bit DPAD_DOWN, a
  593. jp z, .check_for_left ; if button not pressed then done
  594.  
  595. ; down was held down
  596. .check_for_downright
  597. ; is right also held?
  598. ld a, [joypad_held]
  599. bit DPAD_RIGHT, a
  600. jp z, .check_for_downleft
  601.  
  602. ; down + right held, so sprite needs to be diagonal
  603. ld a, 1 ; diagonal sprite is tile 1
  604. ld [spaceship_tile], a
  605. ld a, SPRITE_FLAGS_YFLIP ; y flip the sprite
  606. ld [spaceship_flags], a
  607.  
  608. jp .adjust_down_pos
  609.  
  610. .check_for_downleft
  611. ; is left also held?
  612. ld a, [joypad_held]
  613. bit DPAD_LEFT, a
  614. jp z, .set_down_only
  615.  
  616. ; down + left held, so sprite needs to be diagonal
  617. ld a, 1 ; diagonal sprite is tile 1
  618. ld [spaceship_tile], a
  619. ld a, SPRITE_FLAGS_XFLIP + SPRITE_FLAGS_YFLIP ; sprite should be x and y flipped
  620. ld [spaceship_flags], a
  621.  
  622. jp .adjust_down_pos
  623.  
  624. .set_down_only
  625. ; only down was held, so sprite needs to be down
  626. ld a, 0 ; vertical sprite is tile 0
  627. ld [spaceship_tile], a
  628. ld a, SPRITE_FLAGS_YFLIP
  629. ld [spaceship_flags], a
  630.  
  631. .adjust_down_pos
  632. ; adjust the sprite's position
  633. ld a, [ScrollTimer] ; only move sprite every 2nd vblank
  634. and %00000001
  635. jr nz, .check_for_left
  636.  
  637. ; move sprite up a pixel
  638. ld a, [spaceship_ypos]
  639. inc a
  640. ld [spaceship_ypos], a
  641.  
  642. .check_for_left
  643. ld a, [joypad_held]
  644. bit DPAD_LEFT, a
  645. jp z, .check_for_right ; if button not pressed then done
  646.  
  647. ; left was pressed
  648. .check_left_andUpOrDown
  649. ld a, [joypad_held]
  650. and DPAD_UP_MASK + DPAD_DOWN_MASK
  651. jp nz, .adjust_left_pos ; if up or down was pressed, then we already set the sprite attribs
  652.  
  653. ; sprite needs to be horizontal
  654. ld a, 2 ; horizontal sprite is tile 2
  655. ld [spaceship_tile], a
  656. ld a, SPRITE_FLAGS_XFLIP
  657. ld [spaceship_flags], a
  658.  
  659. .adjust_left_pos
  660. ld a, [ScrollTimer] ; only move sprite every 2nd vblank
  661. and %00000001
  662. jr nz, .done_checking_dpad
  663.  
  664. ; move sprite left one pixel
  665. ld a, [spaceship_xpos]
  666. dec a
  667. ld [spaceship_xpos], a
  668.  
  669. jp .done_checking_dpad ; if left was pressed, don't check right
  670.  
  671. .check_for_right
  672. ld a, [joypad_held]
  673. bit DPAD_RIGHT, a
  674. jp z, .done_checking_dpad ; if button not pressed then done
  675.  
  676. ; right was pressed
  677. .check_right_andUpOrDown
  678. ld a, [joypad_held]
  679. and DPAD_UP_MASK + DPAD_DOWN_MASK
  680. jp nz, .adjust_right_pos ; if up or down was pressed, then we already set the sprite attribs
  681.  
  682. ; sprite needs to be horizontal
  683. ld a, 2 ; horizontal sprite is tile 2
  684. ld [spaceship_tile], a
  685. ld a, 0
  686. ld [spaceship_flags], a
  687.  
  688. .adjust_right_pos
  689. ld a, [ScrollTimer] ; only move sprite every 2nd vblank
  690. and %00000001
  691. jr nz, .done_checking_dpad
  692.  
  693. ; move sprite left one pixel
  694. ld a, [spaceship_xpos]
  695. inc a
  696. ld [spaceship_xpos], a
  697.  
  698. jp .done_checking_dpad ; if left was pressed, don't check right
  699.  
  700. .done_checking_dpad
  701. ld a, [joypad_down]
  702. bit A_BUTTON, a
  703. jr z, .check_b_button
  704.  
  705.  
  706. .check_b_button
  707. ld a, [joypad_down]
  708. bit B_BUTTON, a
  709. jr z, .done_move_ship
  710.  
  711. ld a, [scrl_dir_flag]
  712. xor 1
  713. ld [scrl_dir_flag], a ; toggle the scroll direction flag
  714.  
  715. .done_move_ship
  716. pop af
  717. ret
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728. TitleTiles:
  729. INCLUDE "titleSet.dat" ; title screen tileset
  730.  
  731.  
  732. TitleScreenData:
  733. INCLUDE "titleScreen.dat" ; title screen map
  734.  
  735.  
  736. ; tiles are here
  737. TileData:
  738. INCLUDE "tgTiles.dat"
  739.  
  740. ; map is here
  741. WorldMap:
  742. BkgMapData:
  743. INCLUDE "tgMap.dat"
  744.  
  745.  
  746.  
  747. ;-------------------------------------------------------------------------
  748. ; Internal RAM... store dynamic data here
  749. ;-------------------------------------------------------------------------
  750. SECTION "RAM_Start_Sprites",BSS[$C000]
  751.  
  752. ; local version of sprite attrib table
  753. spaceship_ypos:
  754. ds 1
  755. spaceship_xpos:
  756. ds 1
  757. spaceship_tile:
  758. ds 1
  759. spaceship_flags:
  760. ds 1
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767. SECTION "RAM_Other_Variables",BSS[$C0A0]
  768. ; other variables
  769.  
  770. ; joypad values
  771. joypad_held:
  772. ds 1 ; what buttons are currently held
  773. joypad_down:
  774. ds 1 ; what buttons went down since last joypad read
  775.  
  776. ; scroll values
  777. world_x:
  778. ds 2
  779. world_y:
  780. ds 2
  781.  
  782. x_scrl_accum:
  783. ds 1
  784. y_scrl_accum:
  785. ds 1
  786.  
  787. ; bullets (16 of them, 2 bytes for each)
  788. ; 1st byte = orientation (3 bits) - if $ff, this bullet is unused
  789. ; 2nd byte = time left to live (in vblanks)
  790. bullet_data:
  791. ds 32
  792.  
  793. ; frame timing
  794. vblank_flag:
  795. ds 1 ; set if a vblank occured since last pass through game loop
  796.  
  797. ; scroll direction flag
  798. scrl_dir_flag:
  799. ds 1
  800.  
  801. ; temp variables
  802. ScrollTimer:
  803. ds 1 ; temp variable for slowing down scroll speed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement