Advertisement
Guest User

A simple Gameboy program to count numbers

a guest
Sep 21st, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;    This program is free software: you can redistribute it and/or modify
  2. ;    it under the terms of the GNU General Public License as published by
  3. ;    the Free Software Foundation, either version 3 of the License, or
  4. ;    (at your option) any later version.
  5. ;
  6. ;    This program is distributed in the hope that it will be useful,
  7. ;    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. ;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. ;    GNU General Public License for more details.
  10. ;
  11. ;    You should have received a copy of the GNU General Public License
  12. ;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  13.  
  14. INCLUDE "gbhw.inc"          ; import file definitions
  15.  
  16. ;Variables
  17. _NUMBER EQU _RAM
  18.  
  19. ; Constants
  20. _POS_CRONOM         EQU     _SCRN0+32*4+6   ; screen position
  21.  
  22. ; VBlank interruption
  23. SECTION "Vblank",HOME[$0040]
  24.     call DrawingNumbers
  25.     reti
  26.    
  27. ; The program begins here:
  28. SECTION "start",HOME[$0100]
  29.     nop
  30.     jp      start
  31.    
  32.     ROM_HEADER  ROM_NOMBC, ROM_SIZE_32KBYTE, RAM_SIZE_0KBYTE
  33.    
  34. start:
  35.     nop
  36.     di                      ; disables interrupts
  37.     ld      sp, $ffff       ; We aim pile atop the ram initialization:
  38.  
  39. init:
  40.     ;set Variables
  41.     ld a, 0
  42.     ld [_NUMBER], a
  43.    
  44.     ld      a, %11100100    ; Pallet
  45.     ld      [rBGP], a       ; write background pallet
  46.     ld      [rOBP0], a      ; write sprite pallet
  47.     ld      a, 0
  48.     ld      [rSCX], a
  49.     ld      [rSCY], a
  50.    
  51.     call    Shutdown_LCD        ; turn off LCD
  52.    
  53.     ; Load Tiles into memory
  54.    
  55.     ld hl, Tiles
  56.     ld      de, _VRAM
  57.     ld      bc, FinTiles-Tiles  ; number of bytes to copy
  58.    
  59.     call CopyMemory
  60.    
  61.     ;clean map
  62.     ld      de, _SCRN0      ; map 0
  63.     ld      bc, 32*32
  64.     ld      l, 11           ; empty tile
  65.     call    FillMemory
  66.    
  67.     ; sprite attribute memory
  68.     ld      de, _OAMRAM     ; sprite attribute memory
  69.     ld      bc, 40*4        ;  40 sprites x 4 bytes each
  70.     ld      l, 0            ; we will start fresh, so the sprites
  71.     call    FillMemory  ; Unused remain off-screen
  72.    
  73.     call     DrawingNumbers ; Draw stopwatch
  74.    
  75.     ; setup and activate display
  76.     ld      a, LCDCF_ON|LCDCF_BG8000|LCDCF_BG9800|LCDCF_BGON|LCDCF_OBJ8
  77.     ld      [rLCDC], a
  78.    
  79. ; main loop
  80. main:
  81.  
  82. ld  bc, 15000
  83. call delay
  84.  
  85. call WaitVBlank
  86.  
  87. call DrawingNumbers
  88.  
  89. call IncrementNumber
  90.  
  91. cp 9
  92.  
  93. jr z, ResetNumber
  94.  
  95. jr main
  96.    
  97. IncrementNumber:
  98.  
  99.     ld a, [_NUMBER]
  100.     inc a
  101.     ld [_NUMBER], a
  102.    
  103.     ret
  104.    
  105. ResetNumber:
  106. ld a, 0
  107. ret
  108.    
  109. DrawingNumbers:
  110.    
  111.     ld a, [_NUMBER]
  112.     ld [_POS_CRONOM], a
  113.    
  114.     ret
  115.    
  116. ; LCD shutdown routine
  117. Shutdown_LCD:
  118.     ld      a,[rLCDC]
  119.     rlca                    ;It sets the high bit of LCDC in the carry flag
  120.     ret     nc              ; Display is already off, again.
  121.  
  122.     ; We VBlank hope to, because we can not turn off the screen
  123.     ; some other time
  124.     call    WaitVBlank
  125.  
  126.     ; we are in VBlank, we turn off the LCD
  127.     ld      a,[rLCDC]       ; in A, the contents of the LCDC
  128.     res     7,a             ; we zero bit 7 (on the LCD)
  129.     ld      [rLCDC],a       ; We wrote in the LCDC register content A
  130.  
  131.     ret                     ; return
  132.    
  133. WaitVBlank:
  134.     ld      a, [rLY]
  135.     cp      145
  136.     jr      nz, WaitVBlank
  137.     ret
  138.  
  139. delay:
  140. .delay:
  141.     dec     bc              ; decrement
  142.     ld      a, b            ; see if zero
  143.     or      c
  144.     jr      z, .fin_delay
  145.     nop
  146.     jr      .delay
  147. .fin_delay:
  148.     ret
  149.  
  150. ; memory copy routine
  151. ; copy a number of bytes from one direction to another
  152. ; expects the parameters:
  153. ; hl - copying data address
  154. ; of - destination address
  155. ; bc - amount of data to be copied
  156. ; destroys the contents of A
  157.  
  158. CopyMemory:
  159.     ld      a, [hl]     ; To load the data in A
  160.     ld      [de], a     ; copy the data to the destination
  161.     dec     bc          ; least one copy
  162.     ; We check if bc is zero
  163.     ld      a, c
  164.     or      b
  165.     ret     z           ; If zero, we return
  166.     ; if not, we still
  167.     inc     hl
  168.     inc     de
  169.     jr      CopyMemory
  170.  
  171. FillMemory:
  172.     ld      a, l
  173.     ld      [de], a     ; puts the data in the destination
  174.     dec     bc          ; least one fill
  175.  
  176.     ld      a, c
  177.     or      b           ; We check if bc is zero
  178.     ret     z           ; If zero return
  179.     inc     de          ; if not, we still
  180.     jr      FillMemory
  181.  
  182. Tiles:
  183. ; 0
  184. DW  `00000000
  185. DW  `00333300
  186. DW  `03000330
  187. DW  `03003030
  188. DW  `03030030
  189. DW  `03300030
  190. DW  `00333300
  191. DW  `00000000
  192. ; 1
  193. DW  `00000000
  194. DW  `00003000
  195. DW  `00033000
  196. DW  `00003000
  197. DW  `00003000
  198. DW  `00003000
  199. DW  `00333300
  200. DW  `00000000
  201. ; 2
  202. DW  `00000000
  203. DW  `00333300
  204. DW  `03000030
  205. DW  `00003300
  206. DW  `00030000
  207. DW  `00300000
  208. DW  `03333330
  209. DW  `00000000
  210. ; 3
  211. DW  `00000000
  212. DW  `00333300
  213. DW  `03000030
  214. DW  `00003300
  215. DW  `00000030
  216. DW  `03000030
  217. DW  `00333300
  218. DW  `00000000
  219. ; 4
  220. DW  `00000000
  221. DW  `00000300
  222. DW  `00003300
  223. DW  `00030300
  224. DW  `00333300
  225. DW  `00000300
  226. DW  `00000300
  227. DW  `00000000
  228. ; 5
  229. DW  `00000000
  230. DW  `03333330
  231. DW  `03000000
  232. DW  `00333300
  233. DW  `00000030
  234. DW  `03000030
  235. DW  `00333300
  236. DW  `00000000
  237. ; 6
  238. DW  `00000000
  239. DW  `00003000
  240. DW  `00030000
  241. DW  `00300000
  242. DW  `03333300
  243. DW  `03000030
  244. DW  `00333300
  245. DW  `00000000
  246. ; 7
  247. DW  `00000000
  248. DW  `03333330
  249. DW  `00000300
  250. DW  `00003000
  251. DW  `00030000
  252. DW  `00300000
  253. DW  `00300000
  254. DW  `00000000
  255. ; 8
  256. DW  `00000000
  257. DW  `00333300
  258. DW  `03000030
  259. DW  `00333300
  260. DW  `03000030
  261. DW  `03000030
  262. DW  `00333300
  263. DW  `00000000
  264.  
  265. ; 9
  266. DW  `00000000
  267. DW  `00333300
  268. DW  `03000030
  269. DW  `03000030
  270. DW  `00333300
  271. DW  `00003000
  272. DW  `00330000
  273. DW  `03000000
  274.  
  275. ; :
  276. DW  `00000000
  277. DW  `00033000
  278. DW  `00033000
  279. DW  `00000000
  280. DW  `00033000
  281. DW  `00033000
  282. DW  `00000000
  283. DW  `00000000
  284.  
  285. ; empty tile
  286. DW  `00000000
  287. DW  `00000000
  288. DW  `00000000
  289. DW  `00000000
  290. DW  `00000000
  291. DW  `00000000
  292. DW  `00000000
  293. DW  `00000000
  294. FinTiles:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement