Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;I don't know if this is the best or most optimized way to transfer SGB packets; it just works
- sendSGBPacket:
- ld b, 16 ;Number of bytes to send
- ld c, 1 ;Number to AND with
- ld hl, PAL ;Address of the packet
- ld de, $FF00 ;Address of the joypad register
- ld a, $00 ;Load zero into A
- ld [de], a ;Set P14 and P15 to low
- ld a, $30 ;Load 0x30 into A
- ld [de], a ;Set P14 and P15 to high
- .sendPacketLoop:
- ld a, [hl] ;Load a byte into A
- and c ;AND it with C
- jp nz, .sendZero ;If the bit is zero, send a zero
- .sendOne:
- ld a, $20 ;Load 0x20 into A
- ld [de], a ;Set P15 to high
- jp .afterBit ;Don't write a zero
- .sendZero:
- ld a, $10 ;Load 0x10 into A
- ld [de], a ;Set P14 to high
- .afterBit:
- ld a, c ;Load C into A
- cp $80 ;Compare it with 0x80
- jp z, .resetAND ;Reset the AND value if it is true
- sla c ;If not, shift C left by one
- jp .skipResetAND ;Don't reset the AND value
- .resetAND:
- ld c, $01 ;Load 0x01 into C
- inc hl ;Point to the next packet byte
- dec b ;Decrement the loop counter
- ld a, b ;Load B into A
- cp $00 ;Compare it with 0x00
- jp z, .doneSending ;B is zero, all bytes are transferred, done transferring
- .skipResetAND:
- ld a, $30 ;Load 0x30 into A
- ld [de], a ;Set P14 and P15 to high
- ld a, [hl] ;Load a packet byte into A
- jp .sendPacketLoop ;Loop to transfer
- .doneSending:
- ld a, $30 ;Load 0x30 into A
- ld [de], a ;Set P14 and P15 to high
- ld a, $20 ;Load 0x20 into A
- ld [de], a ;Set P15 to high
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement