-;**** link routines ****
-;these are identical in concept to the routines used in ZTetris, ZPong, and probably
-;every other link game out there. However, these are commented :)
-losses: .dw 0
-Csend:
- ld b,32
-csendwait:
- nop
- dnz csendwait
- cal Qsend
- jr c,Csend
- ret
-Qsend:
-send:
-;inputs: c=byte to send, both wires must be high by default
-;outputs: b=8-number of bits sent, both wires high, goes to game over on certain conditions
-;destroys: af,bc,de
- in a,(7)
- and %11
- jr z,killlink ;if both lines low, get out of here (game over signal)
- ld b,8 ;sending 8 bits
-sendloop:
- ld de,$8000 ;error timer
- rl c ;force high bit into carry
- ld a,%11010100 ;lower the red wire by default (sending 0) ($d4)
- jr nc,selected ;if high bit was 0, use above value
- ld a,%11101000 ;if 1, lower the white wire instead ($e8)
-selected:
- out (7),a ;put it out the link port
-waitconfirm:
- call linktimer
- in a,(7) ;see what the wires are doing
- and %00000011 ;check lower 2 bits
- jr nz,waitconfirm ;if both bits 0, data was received ok (both wires low)
- ld a,%11000000
- out (7),a ;give the ok to raise both wires (one will be done automatically) ($c0)
-waitconfirm2:
- call linktimer
- in a,(7)
- and %00000011
- cp 3
- jr nz,waitconfirm2 ;wait for other calc to raise the other wire
- djnz sendloop ;continue sending
- xor a ;reset c; ld a,0
- ld (losses),a ;reset number of losses
- ret
-
-
-Crecv:
- cal receive
- jr c,Crecv
- ret
-Qrecv:
-receive:
-;inputs: both wires must be high by default
-;outputs: c=byte received, b=8-number of bits received, both wires high
-; goes to game over on certain conditions
-;destroys: af,bc,de
- in a,(7)
- and %00000011
- jr z,killlink ;game over signal applies to receiving too
- ld b,8 ;receiving 8 bits
-receiveloop:
- ld de,$8000 ;error timer
-waitreceive:
- call linktimer
- in a,(7)
- and %11
- cp 3 ;if bits 0 and 1 set, both wires are high
- jr z,waitreceive ;wait for one of the wires to go low
- rra ;check red wire status
- rl c ;if set, red is high (thus white is low), so put the correct bit into c
- rra ;now check white wire (since the flags are destroyed)
- ld a,%11010100 ;$d4
- jr nc,selected2 ;if white is low, lower red and vice versa
- ld a,%11101000 ;$e8
-selected2:
- out (7),a ;so now both wires are low
-waitreceive2:
- call linktimer
- in a,(7)
- and %11
- jr z,waitreceive2 ;wait for the wire we didn't lower to go high again (the other will remain low)
- ld a,%11000000 ;$c0
- out (7),a ;raise both wires since the other calc will have given the ok sign
- djnz receiveloop ;if not done, wait for next bit
- xor a ;reset c
- ld (losses),a ;reset number of losses
- ret
-
-linktimer: ;leave if we have to wait too long (and return an error)
- dec de
- ld a,d
- or e ;see if de is 0
- ret nz ;if not, keep going as usual
- ;otherwise we have to deal with a link failure
- ld a,%11000000 ;$c0
- out (7),a ;reset link status (both high)
- ld a,(losses) ;see how many consecutive losses we have
- inc a
- ld (losses),a ;add this loss to it
- pop de ;fix the stack to leave the link routine (de is ok to destroy)
- cp 20 ;see if this is the 20th consecutive failure
- ret c ;if not, keep playing
- ;otherwise, all is probably lost...
-
-killlink2:
- jr linkend
-killlink:
-linkend:
- jp z,Exit
-
-
-
-
-
-
-
-
-
-#ifdef 0