GAME GAME GAME

GAME GAME GAME is what I called my first Nintendo Game Boy assembly project. The name comes from the childish assertion of its status as a game, since the project barely resembles a game at all.

The source code is really silly. I was following an old and mildly infamous tutorial, so I took a lot of liberties for my own amusement. I had the amazing idea of giving every label the most bizarre names I could come up with, leading to hits such as potato_salad.walmart and strawberry_jam.let_set. I also decided to write comments for as much code as I could, even when I had no idea what was being done. This was actually a good idea in hindsight: it made it explicit what I didn't understand (so I could learn about it in the future — not to be blindly accepted) and it helped reinforce the knowledge that I did gain. It's really funny how little I understood at this point, though.

I made another silly but good decision, which was to iterate on this and incorporate some of my own twists for a second version of GAME GAME GAME. This is a really good idea. I'd strongly encourage this when learning anything for the first time: practice what you just learned, and then extend it. Add something new. In this case, since the program involved moving a sprite around the screen, I switched the sprite to an arrow and I added functionality to make the arrow face the direction it moves (roughly — diagonal cases are not handled, with vertical movement given precedence).

The original version remained named "GAME GAME GAME v1", with the added functionality found in a separate copy named "GAME GAME GAME v2". (This might have been the first experience enticing me to start using version control, which began a little later!)

GAME GAME GAME v1.asm

file contents
INCLUDE "gbhw.inc"              ;include definitions because I'm a lazy bum
_SPR0_Y     EQU     _OAMRAM     ;constants for the sprite's OAM because I'm still lazy
_SPR0_X     EQU     _OAMRAM+1   ;more OAM constants
_SPR0_NUM   EQU     _OAMRAM+2   ;more
_SPR0_ATT   EQU     _OAMRAM+3   ;don't even need this one because I won't be working with it but why not
_PAD        EQU     _RAM        ;joypad variable goes in regular RAM
 
;program start
SECTION "i_like_cheese",HOME[$0100]
    nop                         ;NO OPERATION
    jp      cow_says_moo        ;go to the real program start

;boring ROM header stuff because I want the game to actually work
    ROM_HEADER  ROM_NOMBC, ROM_SIZE_32KBYTE, RAM_SIZE_0KBYTE

;the REAL program start
cow_says_moo:
    nop                         ;NOP
    di                          ;Disable Interrupts
    ld      sp, $FFFF           ;LoaD the top of RAM to the stack

;some more initial settings
potato_salad:
    ld      a, %11100100        ;use the normal palette because we want normal colours
    ld      [rBGP], a           ;and put it in the background palette register
    ld      [rOBP0], a          ;also the sprite (object, OBJ) palette register

    ld      a, 0                ;0, that's right captain feathersword,
    ld      [rSCX], a           ;we need to put 0 in the accumulator register for
    ld      [rSCY], a           ;the window scroll positions!

    call    strawberry_jam      ;for whatever reason I need to turn off the LCD

    ld      hl, Tiles           ;sprite(tm) (and background)
    ld      de, _VRAM           ;get the VRAM address in de
    ld      b, EndTiles-Tiles   ;b is the data counter, it counts how much data we need to transfer

.chair_cushion:
    ld      a, [hl]             ;data pointed to by hl goes into a (the tiles)
    ld      [de], a             ;put that data into address pointed to by de (the VRAM)
    dec     b                   ;one less byte of data to go
    jr      z, .walmart         ;if b is zero, go do the walmart subprocess
    ;if not:
    inc     hl                  ;go to the next byte of data
    inc     de                  ;go to the next byte in VRAM
    jr      .chair_cushion      ;loop
.walmart:
    ;fill the background map with tile 0 (the background tile)
    ld      hl, _SCRN0          ;background map
    ld      de, 32*32           ;background map is 32x32 tiles

.z_dash_niner_niner:
    ld      a, 0                ;tile 0
    ld      [hl], a             ;into _SCRN0
    dec     de                  ;counter - 1
    ;the 16-bit dec operation doesn't affect the flags, so I need to check manually
    ld      a, d                ;1st (technically 2nd) half of de
    or      e                   ;logical or with 2nd (1st) half
    jp      z, .sleeping_foot   ;if they're both zero (from the or) then it's done
    ;if not:
    inc     hl                  ;next!
    jp      .z_dash_niner_niner ;loop
.sleeping_foot:
    ld      hl, _OAMRAM         ;next is the sprite data, which goes into OAM RAM
    ld      de, 40*4            ;40 sprites x 4 bytes of OAM
.tiger_fur:
    ld      a, 0                ;0th
    ld      [hl], a             ;goes into OAM RAM
    dec     de                  ;-1
    ;check if zero
    ld      a, d
    or      e
    jp      z, .black_and_white
    inc     hl
    jp      .tiger_fur
.black_and_white:
    ;make the sprite
    ld      a, 84               ;y position
    ld      [_SPR0_Y], a
    ld      a, 84               ;x position
    ld      [_SPR0_X], a
    ld      a, 1                ;tile number
    ld      [_SPR0_NUM], a
    ld      a, 0                ;special attributes
    ld      [_SPR0_ATT], a

    ;DISPLAY ACTIVATE
    ld      a, LCDCF_ON|LCDCF_BG8000|LCDCF_BG9800|LCDCF_BGON|LCDCF_OBJ8|LCDCF_OBJON
    ld      [rLCDC], a

;main loop
fabric_softener:
    call    cloudy_mason_jar    ;read the joypad input
;wait for VBlank
.knalbv_rof_tiaw:
    ld      a, [rLY]            ;get the coord for LCDC
    cp      145                 ;is it 145? (the screen is 144 lines, so 145 is off it)
    jr      nz, .knalbv_rof_tiaw ;if it's not 145, then waste another CPU cycle (loop)
    ;oh look, it's 145
    ld      a, [_PAD]           ;check the joypad
    and     %00010000           ;is it the right button?
    call    nz, stranded_wire   ;if it is, then go deal with that

    ld      a, [_PAD]
    and     %00100000           ;left
    call    nz, water_bottle

    ld      a, [_PAD]
    and     %01000000           ;up
    call    nz, boston_red_sox

    ld      a, [_PAD]
    and     %10000000           ;down
    call    nz, ham_sandwich

    ld      a, [_PAD]
    and     %00000001           ;A button
    call    nz, dirty_dog

    call    orange_ink          ;delay
    jr      fabric_softener     ;loop

;what's that? oh you pressed a button?
;right
stranded_wire:
    ld      a, [_SPR0_X]        ;get the sprite's x position
    cp      160                 ;is it on the edge?
    ret     z                   ;if it is, then forget it

    inc     a                   ;if it's not on the edge, then x goes up 1
    ld      [_SPR0_X], a        ;store the new position in OAM

    ret                         ;return
;left
water_bottle:
    ld      a, [_SPR0_X]        ;get the sprite's x position
    cp      8                   ;is it on the edge?
    ret     z                   ;if it is, then forget it

    dec     a                   ;if it's not on the edge, then x goes down 1
    ld      [_SPR0_X], a        ;store the new position in OAM

    ret                         ;return
;up
boston_red_sox:
    ld      a, [_SPR0_Y]        ;get the sprite's y position
    cp      16                  ;is it on the edge?
    ret     z                   ;if it is, then forget it

    dec     a                   ;if it's not on the edge, then y goes down (up) 1
    ld      [_SPR0_Y], a        ;store the new position in OAM

    ret                         ;return
;down
ham_sandwich:
    ld      a, [_SPR0_Y]        ;get the sprite's y position
    cp      152                 ;is it on the edge?
    ret     z                   ;if it is, then forget it

    inc     a                   ;if it's not on the edge, then y goes up (down) 1
    ld      [_SPR0_Y], a        ;store the new position in OAM

    ret                         ;return
;A button
dirty_dog:
    ld      a, [_SPR0_NUM]      ;I want to change the sprite tile, so get the tile number
    cp      2                   ;is it already 2?
    jr      z, .dirty_cat       ;if it's already 2, then it should be changed to 1, right?

    ld      a, 2                ;new tile number
    ld      [_SPR0_NUM], a      ;store it in sprite data

    call    orange_ink          ;delay because otherwise the change would look too fast and weird
    ret                         ;return
.dirty_cat:
    ld      a, 1                ;new tile number
    ld      [_SPR0_NUM], a

    call    orange_ink          ;wait a bit
    ret                         ;return

;read the joypad input
cloudy_mason_jar:
    ld      a, %00100000        ;read d-pad (you can only do either d-pad or buttons at a time
                                ;for some weird reason
    ld      [rP1], a            ;tell the GameBoy that we want to read the d-pad
 
    ;do several readings to avoid "bouncing"
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]

    and     $0F                 ;the readings are stored in the bottom 4 bits so we only need those
    swap    a                   ;swap upper and lower bits
    ld      b, a                ;store it in b

    ld      a, %00010000        ;read buttons
    ld      [rP1], a

    ;no bouncing allowed
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]

    and     $0F                 ;top 4 bits are unused
    or      b                   ;I honestly don't know how this works because they're 8-bits
                                ;it probably works with each bit and I guess would combine the two
    cpl                         ;complement a
    ld      [_PAD], a           ;put it in the joypad variable
    ret                         ;return

;LCD shutdown?
strawberry_jam:
    ;not sure how this works or what is does
    ld      a, [rLCDC]
    rlca                        ;essentially set the high bit in the carry flag, not sure the purpose
    ret     nc                  ;not sure the purpose of this, but I think it goes back if the carry
                                ;is 0
.let_set:
    ld      a, [rLY]
    cp      145
    jr      nz, .let_set
    ;hey, it's 145
    ;not sure what this does either
    ld      a, [rLCDC]
    res     7, a
    ld      [rLCDC], a

    ret                         ;return
 
;delay
orange_ink:
    ld      de, 5000            ;the number of times to execute the loop
.yellow_ink:
    dec     de                  ;-1
    ld      a, d                ;is it zero?
    or      e
    jr      z, .ink_cartridge_empty
    nop
    jr      .yellow_ink         ;loop
.ink_cartridge_empty:
    ret

;tiles
Tiles:
    ;background tile
    DB  $00, $FF, $00, $FF, $00, $FF, $00, $FF
    DB  $00, $FF, $00, $FF, $00, $FF, $00, $FF
    ;sprite 1
    DB  $3C, $3C, $7E, $42, $FF, $A5, $FF, $81
    DB  $FF, $A5, $FF, $99, $7E, $42, $3C, $3C
    ;sprite 2
    DB  $3C, $3C, $7E, $42, $FF, $A5, $FF, $81
    DB  $FF, $81, $FF, $BD, $7E, $42, $3C, $3C
EndTiles:

GAME GAME GAME v2.asm

file contents
INCLUDE "gbhw.inc"              ;include definitions because I'm a lazy bum
_SPR0_Y     EQU     _OAMRAM     ;constants for the sprite's OAM because I'm still lazy
_SPR0_X     EQU     _OAMRAM+1   ;more OAM constants
_SPR0_NUM   EQU     _OAMRAM+2   ;more
_SPR0_ATT   EQU     _OAMRAM+3   ;don't even need this one because I won't be working with it but why not
_PAD        EQU     _RAM        ;joypad variable goes in regular RAM
 
;program start
SECTION "i_like_cheese",HOME[$0100]
    nop                         ;NO OPERATION
    jp      cow_says_moo        ;go to the real program start

;boring ROM header stuff because I want the game to actually work
    ROM_HEADER  ROM_NOMBC, ROM_SIZE_32KBYTE, RAM_SIZE_0KBYTE

;the REAL program start
cow_says_moo:
    nop                         ;NOP
    di                          ;Disable Interrupts
    ld      sp, $FFFF           ;LoaD the top of RAM to the stack

;some more initial settings
potato_salad:
    ld      a, %11100100        ;use the normal palette because we want normal colours
    ld      [rBGP], a           ;and put it in the background palette register
    ld      a, %11010010        ;use this palette for sprites because I want white in them
    ld      [rOBP0], a          ;store in the the sprite (object, OBJ) palette register

    ld      a, 0                ;0, that's right captain feathersword,
    ld      [rSCX], a           ;we need to put 0 in the accumulator register for
    ld      [rSCY], a           ;the window scroll positions!

    call    strawberry_jam      ;for whatever reason I need to turn off the LCD

    ld      hl, Tiles           ;sprite(tm) (and background)
    ld      de, _VRAM           ;get the VRAM address in de
    ld      b, EndTiles-Tiles   ;b is the data counter, it counts how much data we need to transfer

.chair_cushion:
    ld      a, [hl]             ;data pointed to by hl goes into a (the tiles)
    ld      [de], a             ;put that data into address pointed to by de (the VRAM)
    dec     b                   ;one less byte of data to go
    jr      z, .walmart         ;if b is zero, go do the walmart subprocess
    ;if not:
    inc     hl                  ;go to the next byte of data
    inc     de                  ;go to the next byte in VRAM
    jr      .chair_cushion      ;loop
.walmart:
    ;fill the background map with tile 0 (the background tile)
    ld      hl, _SCRN0          ;background map
    ld      de, 32*32           ;background map is 32x32 tiles

.z_dash_niner_niner:
    ld      a, 0                ;tile 0
    ld      [hl], a             ;into _SCRN0
    dec     de                  ;counter - 1
    ;the 16-bit dec operation doesn't affect the flags, so I need to check manually
    ld      a, d                ;1st (technically 2nd) half of de
    or      e                   ;logical or with 2nd (1st) half
    jp      z, .sleeping_foot   ;if they're both zero (from the or) then it's done
    ;if not:
    inc     hl                  ;next!
    jp      .z_dash_niner_niner ;loop
.sleeping_foot:
    ld      hl, _OAMRAM         ;next is the sprite data, which goes into OAM RAM
    ld      de, 40*4            ;40 sprites x 4 bytes of OAM
.tiger_fur:
    ld      a, 0                ;0th
    ld      [hl], a             ;goes into OAM RAM
    dec     de                  ;-1
    ;check if zero
    ld      a, d
    or      e
    jp      z, .black_and_white
    inc     hl
    jp      .tiger_fur
.black_and_white:
    ;make the sprite
    ld      a, 84               ;y position
    ld      [_SPR0_Y], a
    ld      a, 84               ;x position
    ld      [_SPR0_X], a
    ld      a, 1                ;tile number
    ld      [_SPR0_NUM], a
    ld      a, 0                ;special attributes
    ld      [_SPR0_ATT], a

    ;DISPLAY ACTIVATE
    ld      a, LCDCF_ON|LCDCF_BG8000|LCDCF_BG9800|LCDCF_BGON|LCDCF_OBJ8|LCDCF_OBJON
    ld      [rLCDC], a

;main loop
fabric_softener:
    call    cloudy_mason_jar    ;read the joypad input
;wait for VBlank
.knalbv_rof_tiaw:
    ld      a, [rLY]            ;get the coord for LCDC
    cp      145                 ;is it 145? (the screen is 144 lines, so 145 is off it)
    jr      nz, .knalbv_rof_tiaw ;if it's not 145, then waste another CPU cycle (loop)
    ;oh look, it's 145
    ld      a, [_PAD]           ;check the joypad
    and     %00010000           ;is it the right button?
    call    nz, stranded_wire   ;if it is, then go deal with that

    ld      a, [_PAD]
    and     %00100000           ;left
    call    nz, water_bottle

    ld      a, [_PAD]
    and     %01000000           ;up
    call    nz, boston_red_sox

    ld      a, [_PAD]
    and     %10000000           ;down
    call    nz, ham_sandwich

    ld      a, [_PAD]
    and     %00000001           ;A button
    call    nz, dirty_dog

    call    orange_ink          ;delay
    jr      fabric_softener     ;loop

;what's that? oh you pressed a button?
;right
stranded_wire:
    ld      a, 1                ;the left/right arrow sprite
    ld      [_SPR0_NUM], a
    ld      a, [_SPR0_ATT]
    res     5, a                ;no x flip, facing right
    ld      [_SPR0_ATT], a

    ld      a, [_SPR0_X]        ;get the sprite's x position
    cp      160                 ;is it on the edge?
    ret     z                   ;if it is, then forget it

    inc     a                   ;if it's not on the edge, then x goes up 1
    ld      [_SPR0_X], a        ;store the new position in OAM

    ret                         ;return
;left
water_bottle:
    ld      a, 1                ;the left/right arrow sprite
    ld      [_SPR0_NUM], a
    ld      a, [_SPR0_ATT]
    set     5, a                ;x flip, facing left
    ld      [_SPR0_ATT], a

    ld      a, [_SPR0_X]        ;get the sprite's x position
    cp      8                   ;is it on the edge?
    ret     z                   ;if it is, then forget it

    dec     a                   ;if it's not on the edge, then x goes down 1
    ld      [_SPR0_X], a        ;store the new position in OAM

    ret                         ;return
;up
boston_red_sox:
    ld      a, 2                ;the up/down arrow sprite
    ld      [_SPR0_NUM], a
    ld      a, [_SPR0_ATT]
    res     6, a                ;no y flip, facing up
    ld      [_SPR0_ATT], a

    ld      a, [_SPR0_Y]        ;get the sprite's y position
    cp      16                  ;is it on the edge?
    ret     z                   ;if it is, then forget it

    dec     a                   ;if it's not on the edge, then y goes down (up) 1
    ld      [_SPR0_Y], a        ;store the new position in OAM

    ret                         ;return
;down
ham_sandwich:
    ld      a, 2                ;the up/down arrow sprite
    ld      [_SPR0_NUM], a
    ld      a, [_SPR0_ATT]
    set     6, a                ;y flip, facing down
    ld      [_SPR0_ATT], a

    ld      a, [_SPR0_Y]        ;get the sprite's y position
    cp      152                 ;is it on the edge?
    ret     z                   ;if it is, then forget it

    inc     a                   ;if it's not on the edge, then y goes up (down) 1
    ld      [_SPR0_Y], a        ;store the new position in OAM

    ret                         ;return
;A button
dirty_dog:
    ld      a, [_SPR0_NUM]      ;I want to change the sprite tile, so get the tile number
    cp      3                   ;is it already 3?
    jr      z, .dirty_cat       ;if it's already 3, then it should be changed to 2, right?

    ld      a, 3                ;new tile number
    ld      [_SPR0_NUM], a      ;store it in sprite data

    call    orange_ink          ;delay because otherwise the change would look too fast and weird
    ret                         ;return
.dirty_cat:
    ld      a, 2                ;new tile number
    ld      [_SPR0_NUM], a

    call    orange_ink          ;wait a bit
    ret                         ;return

;read the joypad input
cloudy_mason_jar:
    ld      a, %00100000        ;read d-pad (you can only do either d-pad or buttons at a time
                                ;for some weird reason)
    ld      [rP1], a            ;tell the GameBoy that we want to read the d-pad
 
    ;do several readings to avoid "bouncing"
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]

    and     $0F                 ;the readings are stored in the bottom 4 bits so we only need those
    swap    a                   ;swap upper and lower bits
    ld      b, a                ;store it in b

    ld      a, %00010000        ;read buttons
    ld      [rP1], a

    ;no bouncing allowed
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]

    and     $0F                 ;top 4 bits are unused
    or      b                   ;I honestly don't know how this works because they're 8-bits
                                ;it probably works with each bit and I guess would combine the two
    cpl                         ;complement a
    ld      [_PAD], a           ;put it in the joypad variable
    ret                         ;return

;LCD shutdown?
strawberry_jam:
    ;not sure how this works or what is does
    ld      a, [rLCDC]
    rlca                        ;essentially set the high bit in the carry flag, not sure the purpose
    ret     nc                  ;not sure the purpose of this, but I think it goes back if the carry
                                ;is 0
.let_set:
    ld      a, [rLY]
    cp      145
    jr      nz, .let_set
    ;hey, it's 145
    ;not sure what this does either
    ld      a, [rLCDC]
    res     7, a
    ld      [rLCDC], a

    ret                         ;return
 
;delay
orange_ink:
    ld      de, 5000            ;the number of times to execute the loop
.yellow_ink:
    dec     de                  ;-1
    ld      a, d                ;is it zero?
    or      e
    jr      z, .ink_cartridge_empty
    nop
    jr      .yellow_ink         ;loop
.ink_cartridge_empty:
    ret

;tiles
Tiles:
    ;background tile
    DB  $00, $FF, $00, $FF, $00, $FF, $00, $FF
    DB  $00, $FF, $00, $FF, $00, $FF, $00, $FF
    ;sprite 1
    DB  $1C, $1C, $1E, $16, $FF, $F3, $FF, $81
    DB  $FD, $83, $FB, $F7, $16, $1E, $1C, $1C
    ;sprite 2
    DB  $3C, $3C, $76, $6E, $FB, $C7, $FD, $83
    DB  $FF, $E7, $3C, $24, $3C, $24, $3C, $3C
    ;sprite 3
    DB  $FF, $FF, $81, $FF, $BD, $C3, $BD, $C3
    DB  $BD, $C3, $BD, $C3, $81, $FF, $FF, $FF
EndTiles:

gbhw.inc

I did not write nor modify this file. I include it here only for the sake of preservation; it appears here as I had downloaded it from the aforementioned tutorial in 2019. The improved modern descendant of this file can be found on GitHub at gbdev/hardware.inc.

file contents
;*
;* GBHW.INC - Gameboy Hardware definitions
;*
; Modified for GBC by David Pello http://ladecadence.net

; If all of these are already defined, don't do it again.

        IF      !DEF(HARDWARE_INC)
HARDWARE_INC  SET  1

rev_Check_hardware_inc: MACRO
;NOTE: REVISION NUMBER CHANGES MUST BE ADDED
;TO SECOND PARAMETER IN FOLLOWING LINE.
        IF      \1 > 1.1      ;PUT REVISION NUMBER HERE
        WARN    "Version \1 or later of 'gbhw.inc' is required."
        ENDC
        ENDM

_HW	EQU $FF00

_VRAM         EQU  $8000 ; $8000->$A000
_SCRN0        EQU  $9800 ; $9800->$9BFF
_SCRN1        EQU  $9C00 ; $9C00->$9FFF
_RAM          EQU  $C000 ; $C000->$DFFF
_RAM_BNK1     EQU  $D000 ; $D000->$DFFF (GBC)
_HRAM         EQU  $F800 ; $F800->$FFFE
_OAMRAM       EQU  $FE00 ; $FE00->$FE9F
_AUD3WAVERAM  EQU  $FF30 ; $FF30->$FF3F


; --
; -- OAM flags
; --

OAMF_PRI    EQU  %10000000 ; Priority
OAMF_YFLIP  EQU  %01000000 ; Y flip
OAMF_XFLIP  EQU  %00100000 ; X flip
OAMF_PAL0   EQU  %00000000 ; Palette number; 0,1
OAMF_PAL1   EQU  %00010000 ; Palette number; 0,1


;***************************************************************************
;*
;* Custom registers
;*
;***************************************************************************

; --
; -- P1 ($FF00)
; -- Register for reading joy pad info.    (R/W)
; --
rP1 EQU $FF00

P1F_5   EQU  %00100000 ; P15 out port
P1F_4   EQU  %00010000 ; P14 out port
P1F_3   EQU  %00001000 ; P13 in port
P1F_2   EQU  %00000100 ; P12 in port
P1F_1   EQU  %00000010 ; P11 in port
P1F_0   EQU  %00000001 ; P10 in port


; --
; -- LCDC ($FF40)
; -- LCD Control (R/W)
; --
rLCDC EQU $FF40

LCDCF_OFF     EQU  %00000000 ; LCD Control Operation
LCDCF_ON      EQU  %10000000 ; LCD Control Operation
LCDCF_WIN9800 EQU  %00000000 ; Window Tile Map Display Select
LCDCF_WIN9C00 EQU  %01000000 ; Window Tile Map Display Select
LCDCF_WINOFF  EQU  %00000000 ; Window Display
LCDCF_WINON   EQU  %00100000 ; Window Display
LCDCF_BG8800  EQU  %00000000 ; BG & Window Tile Data Select
LCDCF_BG8000  EQU  %00010000 ; BG & Window Tile Data Select
LCDCF_BG9800  EQU  %00000000 ; BG Tile Map Display Select
LCDCF_BG9C00  EQU  %00001000 ; BG Tile Map Display Select
LCDCF_OBJ8    EQU  %00000000 ; OBJ Construction
LCDCF_OBJ16   EQU  %00000100 ; OBJ Construction
LCDCF_OBJOFF  EQU  %00000000 ; OBJ Display
LCDCF_OBJON   EQU  %00000010 ; OBJ Display
LCDCF_BGOFF   EQU  %00000000 ; BG Display
LCDCF_BGON    EQU  %00000001 ; BG Display
; "Window Character Data Select" follows BG


; --
; -- STAT ($FF41)
; -- LCDC Status   (R/W)
; --
rSTAT EQU $FF41

STATF_LYC     EQU  %01000000 ; LYCEQULY Coincidence (Selectable)
STATF_MODE10  EQU  %00100000 ; Mode 10
STATF_MODE01  EQU  %00010000 ; Mode 01 (V-Blank)
STATF_MODE00  EQU  %00001000 ; Mode 00 (H-Blank)
STATF_LYCF    EQU  %00000100 ; Coincidence Flag
STATF_HB      EQU  %00000000 ; H-Blank
STATF_VB      EQU  %00000001 ; V-Blank
STATF_OAM     EQU  %00000010 ; OAM-RAM is used by system
STATF_LCD     EQU  %00000011 ; Both OAM and VRAM used by system
STATF_BUSY    EQU  %00000010 ; When set, VRAM access is unsafe


; --
; -- SCY ($FF42)
; -- Scroll Y (R/W)
; --
rSCY  EQU $FF42


; --
; -- SCY ($FF43)
; -- Scroll X (R/W)
; --
rSCX  EQU $FF43


; --
; -- LY ($FF44)
; -- LCDC Y-Coordinate (R)
; --
; -- Values range from 0->153. 144->153 is the VBlank period.
; --
rLY EQU $FF44


; --
; -- LYC ($FF45)
; -- LY Compare (R/W)
; --
; -- When LYEQUEQULYC, STATF_LYCF will be set in STAT
; --
rLYC  EQU $FF45


; --
; -- DMA ($FF46)
; -- DMA Transfer and Start Address (W)
; --
rDMA  EQU $FF46


; --
; -- BGP ($FF47)
; -- BG Palette Data (W)
; --
; -- Bit 7-6 - Intensity for %11
; -- Bit 5-4 - Intensity for %10
; -- Bit 3-2 - Intensity for %01
; -- Bit 1-0 - Intensity for %00
; --
rBGP  EQU $FF47


; --
; -- OBP0 ($FF48)
; -- Object Palette 0 Data (W)
; --
; -- See BGP for info
; --
rOBP0 EQU $FF48


; --
; -- OBP1 ($FF49)
; -- Object Palette 1 Data (W)
; --
; -- See BGP for info
; --
rOBP1 EQU $FF49


; --
; -- SB ($FF01)
; -- Serial Transfer Data (R/W)
; --
rSB EQU $FF01

; --
; -- SC ($FF02)
; -- Serial I/O Control (R/W)
; --
rSC EQU $FF02

; --
; -- DIV ($FF04)
; -- Divider register (R/W)
; --
rDIV EQU $FF04


; --
; -- TIMA ($FF05)
; -- Timer counter (R/W)
; --
rTIMA EQU $FF05


; --
; -- TMA ($FF06)
; -- Timer modulo (R/W)
; --
rTMA EQU $FF06


; --
; -- TAC ($FF07)
; -- Timer control (R/W)
; --
rTAC EQU $FF07

TACF_START   EQU %00000100
TACF_STOP    EQU %00000000
TACF_4KHZ    EQU %00000000
TACF_16KHZ   EQU %00000011
TACF_65KHZ   EQU %00000010
TACF_262KHZ  EQU %00000001


; --
; -- IF ($FF0F)
; -- Interrupt Flag (R/W)
; --
; -- IE ($FFFF)
; -- Interrupt Enable (R/W)
; --
rIF EQU $FF0F
rIE EQU $FFFF

IEF_HILO    EQU  %00010000 ; Transition from High to Low of Pin number P10-P13
IEF_SERIAL  EQU  %00001000 ; Serial I/O transfer end
IEF_TIMER   EQU  %00000100 ; Timer Overflow
IEF_LCDC    EQU  %00000010 ; LCDC (see STAT)
IEF_VBLANK  EQU  %00000001 ; V-Blank


; --
; -- WY ($FF4A)
; -- Window Y Position (R/W)
; --
; -- 0 <EQU WY <EQU 143
; --
rWY EQU $FF4A


; --
; -- WX ($FF4B)
; -- Window X Position (R/W)
; --
; -- 7 <EQU WX <EQU 166
; --
rWX EQU $FF4B


;***************************************************************************
;*
;* Sound control registers
;*
;***************************************************************************

; --
; -- AUDVOL/NR50 ($FF24)
; -- Channel control / ON-OFF / Volume (R/W)
; --
; -- Bit 7   - Vin->SO2 ON/OFF (Vin??)
; -- Bit 6-4 - SO2 output level (volume) (# 0-7)
; -- Bit 3   - Vin->SO1 ON/OFF (Vin??)
; -- Bit 2-0 - SO1 output level (volume) (# 0-7)
; --
rNR50 EQU $FF24
rAUDVOL	EQU rNR50


; --
; -- AUDTERM/NR51 ($FF25)
; -- Selection of Sound output terminal (R/W)
; --
; -- Bit 7   - Output sound 4 to SO2 terminal
; -- Bit 6   - Output sound 3 to SO2 terminal
; -- Bit 5   - Output sound 2 to SO2 terminal
; -- Bit 4   - Output sound 1 to SO2 terminal
; -- Bit 3   - Output sound 4 to SO1 terminal
; -- Bit 2   - Output sound 3 to SO1 terminal
; -- Bit 1   - Output sound 2 to SO1 terminal
; -- Bit 0   - Output sound 0 to SO1 terminal
; --
rNR51 EQU $FF25
rAUDTERM	EQU rNR51


; --
; -- AUDENA/NR52 ($FF26)
; -- Sound on/off (R/W)
; --
; -- Bit 7   - All sound on/off (sets all audio regs to 0!)
; -- Bit 3   - Sound 4 ON flag (doesn't work!)
; -- Bit 2   - Sound 3 ON flag (doesn't work!)
; -- Bit 1   - Sound 2 ON flag (doesn't work!)
; -- Bit 0   - Sound 1 ON flag (doesn't work!)
; --
rNR52 EQU $FF26
rAUDENA	EQU rNR52


;***************************************************************************
;*
;* SoundChannel #1 registers
;*
;***************************************************************************

; --
; -- AUD1SWEEP/NR10 ($FF10)
; -- Sweep register (R/W)
; --
; -- Bit 6-4 - Sweep Time
; -- Bit 3   - Sweep Increase/Decrease
; --           0: Addition    (frequency increases???)
; --           1: Subtraction (frequency increases???)
; -- Bit 2-0 - Number of sweep shift (# 0-7)
; -- Sweep Time: (n*7.8ms)
; --
rNR10 EQU $FF10
rAUD1SWEEP	EQU rNR10


; --
; -- AUD1LEN/NR11 ($FF11)
; -- Sound length/Wave pattern duty (R/W)
; --
; -- Bit 7-6 - Wave Pattern Duty (00:12.5% 01:25% 10:50% 11:75%)
; -- Bit 5-0 - Sound length data (# 0-63)
; --
rNR11 EQU $FF11
rAUD1LEN	EQU rNR11


; --
; -- AUD1ENV/NR12 ($FF12)
; -- Envelope (R/W)
; --
; -- Bit 7-4 - Initial value of envelope
; -- Bit 3   - Envelope UP/DOWN
; --           0: Decrease
; --           1: Range of increase
; -- Bit 2-0 - Number of envelope sweep (# 0-7)
; --
rNR12 EQU $FF12
rAUD1ENV	EQU rNR12


; --
; -- AUD1LOW/NR13 ($FF13)
; -- Frequency lo (W)
; --
rNR13 EQU $FF13
rAUD1LOW	EQU rNR13


; --
; -- AUD1HIGH/NR14 ($FF14)
; -- Frequency hi (W)
; --
; -- Bit 7   - Initial (when set, sound restarts)
; -- Bit 6   - Counter/consecutive selection
; -- Bit 2-0 - Frequency's higher 3 bits
; --
rNR14 EQU $FF14
rAUD1HIGH	EQU rNR14


;***************************************************************************
;*
;* SoundChannel #2 registers
;*
;***************************************************************************

; --
; -- AUD2LEN/NR21 ($FF16)
; -- Sound Length; Wave Pattern Duty (R/W)
; --
; -- see AUD1LEN for info
; --
rNR21 EQU $FF16
rAUD2LEN	EQU rNR21


; --
; -- AUD2ENV/NR22 ($FF17)
; -- Envelope (R/W)
; --
; -- see AUD1ENV for info
; --
rNR22 EQU $FF17
rAUD2ENV	EQU rNR22


; --
; -- AUD2LOW/NR23 ($FF18)
; -- Frequency lo (W)
; --
rNR23 EQU $FF18
rAUD2LOW	EQU rNR23


; --
; -- AUD2HIGH/NR24 ($FF19)
; -- Frequency hi (W)
; --
; -- see AUD1HIGH for info
; --
rNR24 EQU $FF19
rAUD2HIGH	EQU rNR24


;***************************************************************************
;*
;* SoundChannel #3 registers
;*
;***************************************************************************

; --
; -- AUD3ENA/NR30 ($FF1A)
; -- Sound on/off (R/W)
; --
; -- Bit 7   - Sound ON/OFF (1EQUON,0EQUOFF)
; --
rNR30 EQU $FF1A
rAUD3ENA	EQU rNR30


; --
; -- AUD3LEN/NR31 ($FF1B)
; -- Sound length (R/W)
; --
; -- Bit 7-0 - Sound length
; --
rNR31 EQU $FF1B
rAUD3LEN	EQU rNR31


; --
; -- AUD3LEVEL/NR32 ($FF1C)
; -- Select output level
; --
; -- Bit 6-5 - Select output level
; --           00: 0/1 (mute)
; --           01: 1/1
; --           10: 1/2
; --           11: 1/4
; --
rNR32 EQU $FF1C
rAUD3LEVEL	EQU rNR32


; --
; -- AUD3LOW/NR33 ($FF1D)
; -- Frequency lo (W)
; --
; -- see AUD1LOW for info
; --
rNR33 EQU $FF1D
rAUD3LOW	EQU rNR33


; --
; -- AUD3HIGH/NR34 ($FF1E)
; -- Frequency hi (W)
; --
; -- see AUD1HIGH for info
; --
rNR34 EQU $FF1E
rAUD3HIGH	EQU rNR34


; --
; -- AUD4LEN/NR41 ($FF20)
; -- Sound length (R/W)
; --
; -- Bit 5-0 - Sound length data (# 0-63)
; --
rNR41 EQU $FF20
rAUD4LEN	EQU rNR41


; --
; -- AUD4ENV/NR42 ($FF21)
; -- Envelope (R/W)
; --
; -- see AUD1ENV for info
; --
rNR42 EQU $FF21
rAUD4ENV	EQU rNR42


; --
; -- AUD4POLY/NR42 ($FF22)
; -- Polynomial counter (R/W)
; --
; -- Bit 7-4 - Selection of the shift clock frequency of the (scf)
; --           polynomial counter (0000-1101)
; --           freqEQUdrf*1/2^scf (not sure)
; -- Bit 3 -   Selection of the polynomial counter's step
; --           0: 15 steps
; --           1: 7 steps
; -- Bit 2-0 - Selection of the dividing ratio of frequencies (drf)
; --           000: f/4   001: f/8   010: f/16  011: f/24
; --           100: f/32  101: f/40  110: f/48  111: f/56  (fEQU4.194304 Mhz)
; --
rNR42_2 EQU $FF22
rAUD4POLY	EQU rNR42_2


; --
; -- AUD4GO/NR43 ($FF23)
; -- (has wrong name and value (ff30) in Dr.Pan's doc!)
; --
; -- Bit 7 -   Inital
; -- Bit 6 -   Counter/consecutive selection
; --
rNR43 EQU $FF23
rAUD4GO	EQU rNR43	; silly name!

;***************************************************************************
;*
;* Cart related
;*
;***************************************************************************

ROM_NOMBC         EQU 0
ROM_MBC1          EQU 1
ROM_MBC1_RAM      EQU 2
ROM_MBC1_RAM_BAT  EQU 3
ROM_MBC2          EQU 5
ROM_MBC2_BAT      EQU 6
ROM_NOMBC_RAM     EQU 8
ROM_NOMBC_RAM_BAT EQU 9

ROM_SIZE_256KBIT  EQU 0
ROM_SIZE_512KBIT  EQU 1
ROM_SIZE_1M       EQU 2
ROM_SIZE_2M       EQU 3
ROM_SIZE_4M       EQU 4
ROM_SIZE_8M       EQU 5
ROM_SIZE_16M      EQU 6

ROM_SIZE_32KBYTE  EQU 0
ROM_SIZE_64KBYTE  EQU 1
ROM_SIZE_128KBYTE EQU 2
ROM_SIZE_256KBYTE EQU 3
ROM_SIZE_512KBYTE EQU 4
ROM_SIZE_1MBYTE   EQU 5
ROM_SIZE_2MBYTE   EQU 6

RAM_SIZE_0KBIT   EQU 0
RAM_SIZE_16KBIT  EQU 1
RAM_SIZE_64KBIT  EQU 2
RAM_SIZE_256KBIT EQU 3
RAM_SIZE_1MBIT   EQU 4

RAM_SIZE_0KBYTE   EQU 0
RAM_SIZE_2KBYTE   EQU 1
RAM_SIZE_8KBYTE   EQU 2
RAM_SIZE_32KBYTE  EQU 3
RAM_SIZE_128KBYTE EQU 4

;***************************************************************************
;*
;* Keypad related
;*
;***************************************************************************

PADF_DOWN       EQU     $80
PADF_UP         EQU     $40
PADF_LEFT       EQU     $20
PADF_RIGHT      EQU     $10
PADF_START      EQU     $08
PADF_SELECT     EQU     $04
PADF_B          EQU     $02
PADF_A          EQU     $01

PADB_DOWN       EQU     $7
PADB_UP         EQU     $6
PADB_LEFT       EQU     $5
PADB_RIGHT      EQU     $4
PADB_START      EQU     $3
PADB_SELECT     EQU     $2
PADB_B          EQU     $1
PADB_A          EQU     $0

;***************************************************************************
;*
;* Screen related
;*
;***************************************************************************

SCRN_X     EQU  160 ; Width of screen in pixels
SCRN_Y     EQU  144 ; Height of screen in pixels
SCRN_X_B   EQU  20  ; Width of screen in bytes
SCRN_Y_B   EQU  18  ; Height of screen in bytes

SCRN_VX    EQU  256 ; Virtual width of screen in pixels
SCRN_VY    EQU  256 ; Virtual height of screen in pixels
SCRN_VX_B  EQU  32  ; Virtual width of screen in bytes
SCRN_VY_B  EQU  32  ; Virtual height of screen in bytes

;***************************************************************************
;*
;* GameBoy Color registers
;*
;***************************************************************************

; GB Color detection
; Read A register after boot, a == $11 = GBC
; B register, bit 0 == 0, GBC, bit 0 == 1, GBA
REGA_GBC	EQU	$11
REGB_GBA	EQU	%00000001

; Gameboy Clock Speed
; Bit 7: Current Speed     (0=Normal, 1=Double) (Read Only)
; Bit 0: Prepare Speed Switch (0=No, 1=Prepare) (Read/Write)
;
; IF KEY1_BIT7 <> DESIRED_SPEED THEN
;   IE=00H       ;(FFFF)=00h
;   JOYP=30H     ;(FF00)=30h
;   KEY1=01H     ;(FF4D)=01h
;   STOP         ;STOP
; ENDIF


rKEY1			EQU	$FF4D		; Gameboy Clock Switch
GBC_IS_NORMAL_CLK	EQU	%00000000 	; Normal Clock ?
GBC_IS_DOUBLE_CLK	EQU	%10000000	; Double Clock ?
GBC_CHANGE_CLK		EQU	%00000001	; Prepare clock change

; RAM Banks
rSVBK	EQU	$FF70	; Set work ram bank


WRAM_BANK0	EQU	%00000000	; Bank 0
WRAM_BANK1	EQU	%00000001	; Bank 1
WRAM_BANK2	EQU	%00000010	; Bank 2
WRAM_BANK3	EQU	%00000011	; Bank 3
WRAM_BANK4	EQU	%00000100	; Bank 4
WRAM_BANK5	EQU	%00000101	; Bank 5
WRAM_BANK6	EQU	%00000110	; Bank 6
WRAM_BANK7	EQU	%00000111	; Bank 7


; Palettes
rBGPI	EQU	$FF68	; Background Palette Index
rBGPD	EQU	$FF69	; Backgorund Palette Data
rOBPI	EQU	$FF6A	; Sprite Palette Index
rOBPD	EQU	$FF6B	; Sprite Palette Data

; VRAM Banks
rVRBS	EQU	$FF4F	; VRAM Bank Select 
VRB0	EQU	$00	; VRAM Bank 0
VRB1	EQU	$01	; VRAM Bank 1

; GB Color Map attributes (VRAM Bank 1)
;
; Bit 0-2  Background Palette number  (BGP0-7)
; Bit 3    Tile VRAM Bank number      (0=Bank 0, 1=Bank 1)
; Bit 4    Not used
; Bit 5    Horizontal Flip            (0=Normal, 1=Mirror horizontally)
; Bit 6    Vertical Flip              (0=Normal, 1=Mirror vertically)
; Bit 7    BG-to-OAM Priority         (0=Use OAM priority bit, 1=BG Priority)

GBCMA_PAL0	EQU	%00000000	; Pallete 0
GBCMA_PAL1	EQU	%00000001	; Pallete 1
GBCMA_PAL2	EQU	%00000010	; Pallete 2
GBCMA_PAL3	EQU	%00000011	; Pallete 3
GBCMA_PAL4	EQU	%00000100	; Pallete 4
GBCMA_PAL5	EQU	%00000101	; Pallete 5
GBCMA_PAL6	EQU	%00000110	; Pallete 6
GBCMA_PAL7	EQU	%00000111	; Pallete 7
GBCMA_BNK0	EQU	%00000000	; Bank 0
GBCMA_BNK1	EQU	%00001000	; Bank 1
GBCMA_FLPH	EQU	%00100000	; Flip Horizontally
GBCMA_FLPV	EQU	%01000000	; Flip Vertically
GBCMA_BKGP	EQU	%10000000	; Background Priority	

; VRAM DMA
rDMA1		EQU	$FF51		; DMA Source High
rDMA2		EQU	$FF52		; DMA Source Low
rDMA3		EQU	$FF53		; DMA Destination High
rDMA4		EQU	$FF54		; DMA Destination Low
rDMA5		EQU	$FF55		; DMA Length-Mode-Start

GPDMA		EQU	%00000000	; rDMA5 General Purpose DMA
HBDMA		EQU	%10000000	; rDMA5 HBlank DMA
DMAAT		EQU	%00000000	; DMA Active (rDMA5 bit 7 = 0)

; IR Port
; Bit 0:   Write Data   (0=LED Off, 1=LED On)             (Read/Write)
; Bit 1:   Read Data    (0=Receiving IR Signal, 1=Normal) (Read Only)
; Bit 6-7: Data Read Enable (0=Disable, 3=Enable)         (Read/Write)

rRP		EQU	$FF56		; IR Port control


;; ROM HEADERS

NINTENDO_LOGO:   MACRO
;*
;* Nintendo scrolling logo
;* (Code won't work on a real GameBoy)
;* (if next six lines are altered.)
 DB $CE,$ED,$66,$66,$CC,$0D,$00,$0B,$03,$73,$00,$83,$00,$0C,$00,$0D
 DB $00,$08,$11,$1F,$88,$89,$00,$0E,$DC,$CC,$6E,$E6,$DD,$DD,$D9,$99
 DB $BB,$BB,$67,$63,$6E,$0E,$EC,$CC,$DD,$DC,$99,$9F,$BB,$B9,$33,$3E
        ENDM

ROM_HEADER:   MACRO
;*
;* Nintendo scrolling logo
;* (Code won't work on a real GameBoy)
;* (if next six lines are altered.)

;    0123456789ABCDEF
 DB $CE,$ED,$66,$66,$CC,$0D,$00,$0B,$03,$73,$00,$83,$00,$0C,$00,$0D
 DB $00,$08,$11,$1F,$88,$89,$00,$0E,$DC,$CC,$6E,$E6,$DD,$DD,$D9,$99
 DB $BB,$BB,$67,$63,$6E,$0E,$EC,$CC,$DD,$DC,$99,$9F,$BB,$B9,$33,$3E

 DB "GAME GAME GAME",0        ; Cart name - 15bytes
 DB 0                         ; $143
 DB 0,0                       ; $144 - Licensee code (not important)
 DB 0                         ; $146 - SGB Support indicator
 DB \1                        ; $147 - Cart type
 DB \2                        ; $148 - ROM Size
 DB \3                        ; $149 - RAM Size
 DB 1                         ; $14a - Destination code
 DB $33                       ; $14b - Old licensee code
 DB 0                         ; $14c - Mask ROM version
 DB 0                         ; $14d - Complement check (important)
 DW 0                         ; $14e - Checksum (not important)

        ENDM

ROM_HEADER_COLOR:   MACRO
;*
;* Nintendo scrolling logo
;* (Code won't work on a real GameBoy)
;* (if next six lines are altered.)

;    0123456789ABCDEF
 DB $CE,$ED,$66,$66,$CC,$0D,$00,$0B,$03,$73,$00,$83,$00,$0C,$00,$0D
 DB $00,$08,$11,$1F,$88,$89,$00,$0E,$DC,$CC,$6E,$E6,$DD,$DD,$D9,$99
 DB $BB,$BB,$67,$63,$6E,$0E,$EC,$CC,$DD,$DC,$99,$9F,$BB,$B9,$33,$3E

 DB "EXAMPLE",0,0,0,0,0,0,0,0 ; Cart name - 15bytes
 DB $80                       ; $143 - GB Color support
 DB 0,0                       ; $144 - Licensee code (not important)
 DB 0                         ; $146 - SGB Support indicator
 DB \1                        ; $147 - Cart type
 DB \2                        ; $148 - ROM Size
 DB \3                        ; $149 - RAM Size
 DB 1                         ; $14a - Destination code
 DB $33                       ; $14b - Old licensee code
 DB 0                         ; $14c - Mask ROM version
 DB 0                         ; $14d - Complement check (important)
 DW 0                         ; $14e - Checksum (not important)

        ENDM


        ENDC    ;HARDWARE_INC