Advertisement
atm959

Gameboy Transfer SGB Packets

Jun 5th, 2018
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;I don't know if this is the best or most optimized way to transfer SGB packets; it just works
  2.  
  3. sendSGBPacket:
  4.     ld b, 16            ;Number of bytes to send
  5.     ld c, 1             ;Number to AND with
  6.     ld hl, PAL          ;Address of the packet
  7.     ld de, $FF00        ;Address of the joypad register
  8.     ld a, $00           ;Load zero into A
  9.     ld [de], a          ;Set P14 and P15 to low
  10.     ld a, $30           ;Load 0x30 into A
  11.     ld [de], a          ;Set P14 and P15 to high
  12. .sendPacketLoop:
  13.     ld a, [hl]          ;Load a byte into A
  14.     and c               ;AND it with C
  15.     jp nz, .sendZero    ;If the bit is zero, send a zero
  16. .sendOne:
  17.     ld a, $20           ;Load 0x20 into A
  18.     ld [de], a          ;Set P15 to high
  19.     jp .afterBit        ;Don't write a zero
  20. .sendZero:
  21.     ld a, $10           ;Load 0x10 into A
  22.     ld [de], a          ;Set P14 to high
  23. .afterBit:
  24.     ld a, c             ;Load C into A
  25.     cp $80              ;Compare it with 0x80
  26.     jp z, .resetAND     ;Reset the AND value if it is true
  27.     sla c               ;If not, shift C left by one
  28.     jp .skipResetAND    ;Don't reset the AND value
  29. .resetAND:
  30.     ld c, $01           ;Load 0x01 into C
  31.     inc hl              ;Point to the next packet byte
  32.     dec b               ;Decrement the loop counter
  33.     ld a, b             ;Load B into A
  34.     cp $00              ;Compare it with 0x00
  35.     jp z, .doneSending  ;B is zero, all bytes are transferred, done transferring
  36. .skipResetAND:
  37.     ld a, $30           ;Load 0x30 into A
  38.     ld [de], a          ;Set P14 and P15 to high
  39.     ld a, [hl]          ;Load a packet byte into A
  40.     jp .sendPacketLoop  ;Loop to transfer
  41. .doneSending:
  42.     ld a, $30           ;Load 0x30 into A
  43.     ld [de], a          ;Set P14 and P15 to high
  44.     ld a, $20           ;Load 0x20 into A
  45.     ld [de], a          ;Set P15 to high
  46.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement