Advertisement
Leap_

newcode.asm

Jul 28th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; to do:
  2. ; main game loop
  3. ; sprite movement
  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. SECTION "rst 00", ROM0 [$00]
  97.         rst $38
  98. SECTION "rst 08", ROM0 [$08]
  99.         rst $38
  100. SECTION "rst 10", ROM0 [$10]
  101.         rst $38
  102. SECTION "rst 18", ROM0 [$18]
  103.         rst $38
  104. SECTION "rst 28", ROM0 [$20]
  105.         rst $38
  106. SECTION "rst 30", ROM0 [$30]
  107.         rst $38
  108. SECTION "rst 38", ROM0 [$38]
  109.         rst $38
  110.        
  111. ; hardware interrupts
  112.  
  113. SECTION "vblank", ROM0 [$40]
  114.         jp VBlank
  115. SECTION "hblank", ROM0 [$48]
  116.         rst $38
  117. SECTION "timer", ROM0 [$50]
  118.         reti
  119. SECTION "serial", ROM0 [$58]
  120.         reti
  121. SECTION "joypad", ROM0 [$60]
  122.         reti
  123.        
  124.  
  125. SECTION "Entry", ROM0 [$100]
  126.        
  127.         nop ; one cpu cycle, four clock cycles
  128.         jp Start ; enter the program
  129.        
  130. SECTION "Header", ROM0 [$104]
  131.        
  132.        
  133.         ds $150 - $104 ; rgbfix
  134.        
  135. SECTION "Main", ROM0 [$150]
  136. Start::
  137.         ld  sp, STACK_TOP ; top of sp
  138.         ld  a, VBLANK_INT
  139.         ldh [INTERRUPT_ENABLE], a
  140.         sub a
  141.         ldh [LCDC_STATUS], a ; 0
  142.         ldh [LCDC_CONTROL], a ; 0
  143.         ld  [vblank_flag], a ; reset the vblank flag
  144.        
  145.         ; load our tiles
  146.         ld  bc, TitleTiles
  147.         call    LoadTitleTiles
  148.        
  149.         ; load title screen data
  150.         ld  bc, TitleScreenData
  151.         call    LoadTitleScreen
  152.        
  153.         call    InitSprites ; clear sprites
  154.         call    InitPalettes ; set palettes
  155.        
  156.         ld  a, DISPLAY_FLAG | BKG_DISP_FLAG | SPRITE_DISP_FLAG | TILES_LOC | WINDOW_MAP_LOC
  157.         ldh [LCDC_CONTROL], a
  158.        
  159.         jp  titleKeyLoop ; wait for key to be pressed
  160.      
  161. ; ----------------------------------
  162. ; START OF ACTUAL GAME
  163. ; GAME LOOP INCLUDED
  164. ; ----------------------------------
  165.  
  166. gameStart::
  167.  
  168.        
  169.        
  170.        
  171. GameLoop::
  172.        
  173.        
  174.  
  175. ; ----------------------
  176. ; SECTION FOR LOADING TILES/TILESETS
  177. ; ----------------------
  178. ; routine to load tiles into vram
  179. BYTES_PER_TILE  =   16
  180.  
  181. LoadTitleTiles::
  182.     ld bc, TitleTiles
  183.     ld hl, TILES_MEM_LOC_0
  184.     ld e, 5
  185. .tile
  186.     ld d, BYTES_PER_TILE
  187. .byte
  188. .wait
  189.     ld a, [LCDC_STATUS]
  190.     and SPRITE_MODE
  191.     jr nz, .wait
  192.     ld a, [bc]
  193.     ld [hli], a
  194.     inc bc
  195.     dec d
  196.     jr nz, .byte
  197.     dec e
  198.     jr nz, .tile
  199.     ret
  200.  
  201.  
  202. ; routine to show title screen    
  203. LoadTitleScreen::
  204.     ld      hl, MAP_MEM_LOC_0   ; load the map to map bank 0
  205.     ld      e, $04  ; 4 blocks (32x32 tiles, 1024 bytes)
  206. .tile
  207.     ld  d, $00 ; 256 bytes per "block"
  208. .wait
  209.     ; only write during
  210.     ldh     a, [LCDC_STATUS]    ; get the status
  211.     and     SPRITE_MODE         ; don't write during sprite and transfer modes
  212.     jr      nz, .wait
  213.  
  214.     ld      a, [bc]     ; get the next value from the source
  215.     ld      [hli], a    ; load the value to the destination, incrementing dest. ptr
  216.     inc     bc          ; increment the source ptr
  217.  
  218.     ; now loop de times
  219.     dec     d
  220.     jp      nz, .byte
  221.     dec     e
  222.     jp      nz, .tile
  223.  
  224.     ret
  225.  
  226.  
  227.        
  228. ; --------------------------
  229. ; END
  230. ; --------------------------      
  231.  
  232. ; hide the sprites        
  233. HideSprites::
  234.         xor a
  235.         ld  hl, SPRITE_ATTRIB_MEM_LOC
  236.         ld  b, 40 * 4
  237. .loop
  238.         ld  [hli], a
  239.         dec b
  240.         jr  nz, .loop
  241.         ret
  242. ; this routine waits for a key to be pressed
  243. ; when A is pressed, it jumps to our actual game start
  244. titleKeyLoop::
  245.             call Joypad ; read joypad
  246.             ld  a, [joypad_down] ; get the last button down
  247.             bit A_BUTTON, a ; was it the a button?
  248.             jr  z, .key_loop ; if not, keep looping
  249.             jp  gameStart
  250.            
  251.            
  252. .key_loop
  253.             jr  titleKeyLoop ; loop it
  254.  
  255.  
  256.  
  257. ; function  to read joypad info            
  258. Joypad::
  259.         ld  a, PAD_PORT_DPAD ; select dpad
  260.         ldh [JOYPAD_REGISTER], a ; send to joypad
  261.         ldh  a, [JOYPAD_REGISTER]
  262.         ldh  a, [JOYPAD_REGISTER]
  263.         ldh  a, [JOYPAD_REGISTER]
  264.         ldh  a, [JOYPAD_REGISTER]
  265.         ldh  a, [JOYPAD_REGISTER]
  266.         ldh  a, [JOYPAD_REGISTER] ; get result back
  267.         cpl ; bit flip it
  268.         ld  b, a
  269.         and PAD_OUTPUT_MASK ; mask out output bits
  270.         swap    a
  271.         ld  b, a
  272.        
  273.        
  274.         ; get a, b, select, and start buttons
  275.         ld  a, PAD_PORT_BUTTONS ; buttons
  276.         ldh [JOYPAD_REGISTER], a ; send to joypad
  277.         ldh  a, [JOYPAD_REGISTER]
  278.         ldh  a, [JOYPAD_REGISTER]
  279.         ldh  a, [JOYPAD_REGISTER]
  280.         ldh  a, [JOYPAD_REGISTER]
  281.         ldh  a, [JOYPAD_REGISTER]
  282.         ldh  a, [JOYPAD_REGISTER] ; get result back
  283.         cpl ; bit flip result
  284.         and PAD_OUTPUT_MASK ; mask out output bits
  285.         or  b
  286.         ld  b, a
  287.        
  288.         ; calculate buttons that went down since last joypad read
  289.         ld  a, [joypad_held] ; get last button bits
  290.         cpl ; invert
  291.         and b
  292.         ld  [joypad_down], a
  293.         ld  a, b
  294.         ld  [joypad_held], a
  295.         ld  a, $30 ; reset joypad
  296.         ldh [JOYPAD_REGISTER], a ; send back
  297.        
  298.         ret ; done
  299.        
  300.      
  301.        
  302.        
  303.        
  304. ; -----------------
  305. ; Init functions, these routines are called at the beginning of the code.
  306. ; -----------------
  307.  
  308. InitPalettes::
  309.         ld  a, %10010011 ; set palette colors
  310.        
  311.         ; load to all the palettes
  312.         ldh [PALETTE_BKG], a
  313.         ldh [PALETTE_SPRITE_0], a
  314.         ldh [PALETTE_SPRITE_1], a
  315.        
  316. InitSprites::
  317.         ld  hl, $c000
  318.         ld  b, 40*4
  319.         ld  a, $ff
  320. .init_sprites_loop
  321.         ld  [hli], a
  322.         dec b
  323.         jr  init_sprites_loop
  324.         ret
  325. ; ----------------------
  326. ; copy block of data
  327. ; de = destination
  328. ; hl = source
  329. ; b = number of bytes to copy
  330. ; -----------------------
  331.        
  332. CopyData::
  333.         push af
  334. .copy_block_loop
  335.         ld  a, [hli]
  336.         ld  [de], a
  337.         inc de
  338.         dec b
  339.         jr  nz, copy_block_loop
  340.         pop af
  341.         ret
  342.  
  343. ; ---------------
  344. ; vblank function
  345. ; ---------------    
  346. VBlank::
  347.         di
  348.         push af
  349.         ld  a, $c0 ; dma from $c000
  350.         ldh [DMA_REGISTER], a ; start DMA
  351.         ld  a, $28 ; wait for 160 microseconds
  352. .vblank_dma_wait
  353.         dec a
  354.         jr  nz, .vblank_dma_wait
  355.         ld  hl, $fe00 ; sprite memo
  356.         ld  a, 1
  357.         ld  [vblank_flag], a
  358.         pop af
  359.         ei
  360.         reti
  361.  
  362.  
  363.  
  364. TitleTiles::
  365. INCLUDE "titleTiles.z80" ; title screen tileset
  366.  
  367. TitleScreenData::
  368. INCLUDE "titleScreen.z80" ; title screen
  369.  
  370.  
  371. SECTION "RAMSprites",BSS[$C000]
  372.  
  373. playerx:
  374. ds  1
  375. playery:
  376. ds  1
  377. playerTile:
  378. ds  1
  379. playerFlags:
  380. ds  1
  381.  
  382.  
  383. SECTION "RAM",BSS[$C0A0]
  384.  
  385. joypad_held:
  386. ds  1
  387. joypad_down:
  388. ds  1
  389. vblank_flag:
  390. ds  1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement