document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ; Super Game Boy packet transfer
  2. ; @entrada  HL: Packet address
  3. sgbpackettransfer:
  4.     ld      a[hl]
  5.     and     %00000111               ; The three lower bits indicate the number of packets to send
  6.     ret     z                       ; We return if there are no packets to send
  7.     ld      ba                   ; We store the number of packets to send
  8. .sgbpackettransfer_0:
  9.     push    bc
  10.     xor     a
  11.     ld      [rP1]a               ; Initial pulse (Start write). P14 = LOW and P15 = LOW
  12.     ld      a,  P1F_4 | P1F_5
  13.     ld      [rP1]a               ; P14 = HIGH and P15 = HIGH between pulses
  14.     ld      b16                  ; Number of bytes per packet
  15. .sgbpackettransfer_1:
  16.     ld      e8                   ; Bits per byte
  17.     ld      a[hli]
  18.     ld      da                   ; Next byte of the packet
  19. .sgbpackettransfer_2:
  20.     bit     0d
  21.     ld      a,  P1F_4               ; P14 = HIGH and P15 = LOW (Write 1)
  22.     jr      nz, .sgbpackettransfer_3
  23.     ld      a,  P1F_5               ; P14 = LOW and P15 = HIGH (Write 0)
  24. .sgbpackettransfer_3:
  25.     ld      [rP1]a               ; We send one bit
  26.     ld      a,  P1F_4 | P1F_5
  27.     ld      [rP1]a               ; P14 = HIGH and P15 = HIGH between pulses
  28.     rr      d                       ; We rotate the register so that the next bit goes to position 0
  29.     dec     e
  30.     jr      nz, .sgbpackettransfer_2; We jump while there are bits left to be sent
  31.     dec     b
  32.     jr      nz, .sgbpackettransfer_1; We jump while there are bytes left to be sent
  33.     ld      a,  P1F_5
  34.     ld      [rP1]a               ; Bit 129, stop bit (Write 0)
  35.     ld      a,  P1F_4 | P1F_5
  36.     ld      [rP1]a               ; P14 = HIGH and P15 = HIGH between pulses
  37.     call    sgbpackettransfer_wait  ; 280048 clock cycles are consumed (66.768646240234375 milliseconds) at 4.194304 mhz | 24 cycles
  38.     pop     bc
  39.     dec     b
  40.     ret     z
  41.     jr      .sgbpackettransfer_0    ; We jump while there are packets left to be sent
  42.  
  43. ; 280024 clock cycles are consumed
  44. sgbpackettransfer_wait:
  45.     ld      de, 7000                ; 12 cycles
  46. .sgbpackettransfer_wait_0:
  47.     nop                             ; 4 cycles
  48.     nop                             ; 4 cycles
  49.     nop                             ; 4 cycles
  50.     dec     de                      ; 8 cycles
  51.     ld      ad                   ; 4 cycles
  52.     or      e                       ; 4 cycles
  53.     jr      nz, .sgbpackettransfer_wait_0   ; 12 cycles if jumps, 8 if not
  54.     ret                             ; 16 cycles
');