Advertisement
Leap_

fdsfsdfds

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