shithub: pokered

Download patch

ref: d9c77261a1a637515e6cfa3445326591cf011ebb
parent: 95a2c2a6dacb62adf04aa81fd1d7a79da68cb7ea
author: sawakita <sawakitanoodles@gmail.com>
date: Thu Nov 1 14:56:12 EDT 2012

Add routine that sends SGB packets

The delaying routine Wait7000, called after each single packet sending
is disassembled too.

--- a/main.asm
+++ b/main.asm
@@ -70196,7 +70196,92 @@
 	ld a, [hl]
 	ret
 
-INCBIN "baserom.gbc",$71FB6,$725C8-$71FB6
+INCBIN "baserom.gbc",$71FB6,$71FEB-$71FB6
+
+SendSGBPacket: ;$5feb
+;check number of packets
+    ld a,[hl]
+    and a,$07
+    ret z
+; store number of packets in B
+    ld b,a
+.loop2\@
+; save B for later use
+    push bc
+; load a non-zero value in $fff9 to disable the routine that checks actual
+; joypad input (said routine, located at $15f, does nothing if $fff9 is not
+; zero)
+    ld a,$01
+    ld [$fff9],a
+; send RESET signal (P14=LOW, P15=LOW)
+    xor a
+    ld [$ff00],a
+; set P14=HIGH, P15=HIGH
+    ld a,$30
+    ld [$ff00],a
+;load length of packets (16 bytes)
+    ld b,$10
+.nextByte\@
+;set bit counter (8 bits per byte)
+    ld e,$08
+; get next byte in the packet
+    ld a,[hli]
+    ld d,a
+.nextBit0\@
+    bit 0,d
+; if 0th bit is not zero set P14=HIGH,P15=LOW (send bit 1)
+    ld a,$10
+    jr nz,.next0\@
+; else (if 0th bit is zero) set P14=LOW,P15=HIGH (send bit 0)
+    ld a,$20
+.next0\@
+    ld [$ff00],a
+; must set P14=HIGH,P15=HIGH between each "pulse"
+    ld a,$30
+    ld [$ff00],a
+; rotation will put next bit in 0th position (so  we can always use command
+; "bit 0,d" to fetch the bit that has to be sent)
+    rr d
+; decrease bit counter so we know when we have sent all 8 bits of current byte
+    dec e
+    jr nz,.nextBit0\@
+    dec b
+    jr nz,.nextByte\@
+; send bit 1 as a "stop bit" (end of parameter data)
+    ld a,$20
+    ld [$ff00],a
+; set P14=HIGH,P15=HIGH
+    ld a,$30
+    ld [$ff00],a
+    xor a
+    ld [$fff9],a
+; wait for about 70000 cycles
+    call Wait7000
+; restore (previously pushed) number of packets
+    pop bc
+    dec b
+; return if there are no more packets
+    ret z
+; else send 16 more bytes
+    jr .loop2\@
+
+INCBIN "baserom.gbc",$7202B,$7214A-$7202B
+
+Wait7000: ;$614a
+; each loop takes about 10 cycles so this routine actually loops through 70000
+; cycles.
+    ld de,$1b58    ; = 7000
+.loop\@
+    nop
+    nop
+    nop
+    dec de
+    ld a,d
+    or e
+    jr nz,.loop\@
+    ret
+
+INCBIN "baserom.gbc",$72156,$725C8-$72156
 
 MonsterPalettes: ; 65C8
 	db PAL_MEWMON    ; MISSINGNO