*
* Bibliotheque des appels SecondSight
*
* (c) 1995, Brutal Deluxe
*

         lst   off

*----------------------------------------

ssCOMMAND =    $E0C0B0
ssWRITEDATA =  $E0C0B1
ssREADDATA =   $E0C0B2
ssHANDSHAKE =  $E0C0B8

*----------------------------------------

_xGetStatus tsc
         sec
         sbc   #$0006
         tcs
         phd
         tcd
         php
         sei
         lda   $0A
         sta   $03
         lda   $0C
         sta   $05
         lda   #0
         sta   $01
         sep   #$20
         stal  ssCOMMAND
         ldx   #-1
]lp      ldal  ssHANDSHAKE
         cmp   #$01
         beq   _xGetStatus1
         dex
         bne   ]lp
         rep   #$20
         lda   #-1
         sta   $01
         bra   _xGetStatus8

         mx    %10

_xGetStatus1 ldx #5
         ldal  ssREADDATA
]lp      ldal  ssREADDATA
         sta   [$0A]
         inc   $0A
         bne   _xGetStatus2
         inc   $0B
_xGetStatus2 dex
         bne   ]lp
         lda   [$03]
         cmp   #'G'
         bne   _xGetStatus3
         ldy   #$0001
         lda   [$03],Y
         cmp   #'S'
         bne   _xGetStatus3
         iny
         lda   [$03],Y
         cmp   #'V'
         bne   _xGetStatus3
         iny
         lda   [$03],Y
         cmp   #'G'
         bne   _xGetStatus3
         iny
         lda   [$03],Y
         cmp   #'A'
         beq   _xGetStatus4
_xGetStatus3 lda #$FF
         sta   $01
         sta   $02
         bra   _xGetStatus7
_xGetStatus4 lda #$00
         xba
         ldal  ssREADDATA
         tax
         sta   [$0A]
         inc   $0A
         bne   _xGetStatus5
         inc   $0B
_xGetStatus5 ldal ssREADDATA
         sta   [$0A]
         inc   $0A
         bne   _xGetStatus6
         inc   $0B
_xGetStatus6 dex
         bne   _xGetStatus5
_xGetStatus7 rep #$20
_xGetStatus8 plp
         ldy   $01
         lda   $08
         sta   $0C
         lda   $07
         sta   $0B
         pld
         tsc
         clc
         adc   #$000A
         tcs
         tya
         rtl

*----------------------------------------

_xSetMode tsc
         phd
         tcd
         php
         sei
         lda   #1
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $06        ; $04
         jsr   _WriteData
         lda   $04        ; $06
         jsr   _WriteData
         jsr   WaitHSOff
         jsr   WaitHSDone
         plp
         lda   $02
         sta   $06
         lda   $01
         sta   $05
         pld
         tsc
         clc
         adc   #$0004
         tcs
         rtl

*----------------------------------------

_xUploadData tsc
         sec
         sbc   #$0006
         tcs
         phd
         tcd
         stz   $05
         php
         sei
         lda   #2
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $16        ; $0A
         jsr   _WriteData
         lda   $12        ; $0C
         jsr   _WriteData
         lda   $13        ; $0D
         jsr   _WriteData
         lda   $14        ; $0E
         jsr   _WriteData
         lda   $0E        ; $10
         jsr   _WriteData
         lda   $0F        ; $11
         jsr   _WriteData
         lda   $10        ; $12
         jsr   _WriteData
         lda   $0A        ; $14
         jsr   _WriteData
         lda   $0B        ; $15
         jsr   _WriteData
         lda   $0C        ; $16
         jsr   _WriteData
         jsr   WaitHSOff
         lda   $10        ; $12
         and   #$00FF
         sta   $01
         beq   _xUploadData2
_xUploadData1 jsr WaitHSOn
         ldy   #256
]lp      ldx   $0A        ; $14
         lda   $0C        ; $16
         jsr   WritePage
         inc   $0B        ; $15
         dey
         bne   ]lp
         jsr   WaitHSOff
         dec   $01
         bne   _xUploadData1
_xUploadData2 jsr WaitHSOn
         lda   $0E        ; $10
         and   #$0001
         sta   $05
         lda   $0E        ; $10
         and   #$FFFE
         sta   $0E        ; $10
         stz   $10        ; $12
         lda   $0F        ; $11
         beq   _xUploadData3
         tay
]lp      ldx   $0A        ; $14
         lda   $0C        ; $16
         jsr   WritePage
         inc   $0B        ; $15
         dey
         bne   ]lp
         stz   $0F        ; $11
_xUploadData3 lda $0E     ; $10
         beq   _xUploadData4
         ldy   #0
]lp      lda   [$0A],y    ; $14
         stal  ssWRITEDATA
         iny
         iny
         cpy   $0E        ; $10
         bne   ]lp
_xUploadData4 lda $05
         beq   _xUploadData5
         sep   #$20
         lda   [$0A],y    ; $14
         stal  ssWRITEDATA
         rep   #$20
_xUploadData5 jsr WaitHSOff
         plp
         lda   $08
         sta   $16
         lda   $07
         sta   $15
         pld
         tsc
         clc
         adc   #$0014
         tcs
         rtl

*----------------------------------------

_xScrollScreen tsc
         phd
         tcd
         php
         sei
         lda   #3
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $0C        ; $04
         jsr   _WriteData
         lda   $0D        ; $05
         jsr   _WriteData
         lda   $0E        ; $06
         jsr   _WriteData
         lda   $08        ; $08
         jsr   _WriteData
         lda   $09        ; $09
         jsr   _WriteData
         lda   $0A        ; $0A
         jsr   _WriteData
         lda   $04        ; $0C
         jsr   _WriteData
         lda   $05        ; $0D
         jsr   _WriteData
         lda   $06        ; $0E
         jsr   _WriteData
         plp
         jsr   WaitHSOff
         jsr   WaitHSDone
         lda   $02
         sta   $0E
         lda   $01
         sta   $0D
         pld
         tsc
         clc
         adc   #$000C
         tcs
         rtl

*----------------------------------------

_xScreenOff php
         sei
         lda   #4
         jsr   _WriteCmd
         plp
         rtl

*----------------------------------------

_xScreenOn php
         sei
         lda   #5
         jsr   _WriteCmd
         plp
         rtl

*----------------------------------------

_xSetPalette tsc
         phd
         tcd
         php
         sei
         lda   #6
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $04
         jsr   _WriteCmd
         lda   $05
         jsr   _WriteCmd
         lda   $06
         jsr   _WriteCmd
         jsr   WaitHSOff
         jsr   WaitHSOn
         ldy   #0
]lp      lda   [$04],y
         jsr   _WriteData
         iny
         cpy   #768
         bne   ]lp
         jsr   WaitHSOff
         plp
         lda   $02
         sta   $06
         lda   $01
         sta   $05
         pld
         tsc
         clc
         adc   #$0004
         tcs
         rtl

*----------------------------------------

_xSetPaletteEntry tsc
         phd
         tcd
         php
         sei
         lda   #7
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $08        ; $04
         jsr   _WriteData
         lda   $04        ; $06
         jsr   _WriteData
         lda   $05        ; $07
         jsr   _WriteData
         lda   $06        ; $08
         jsr   _WriteData
         jsr   WaitHSOff
         plp
         lda   $02
         sta   $08
         lda   $01
         sta   $07
         pld
         tsc
         clc
         adc   #$0006
         tcs
         rtl

*----------------------------------------

_xSetBorder tsc
         phd
         tcd
         php
         sei
         lda   #8
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $04
         jsr   _WriteCmd
         jsr   WaitHSOff
         plp
         lda   $02
         sta   $04
         lda   $01
         sta   $03
         pld
         tsc
         clc
         adc   #$0002
         tcs
         rtl

*----------------------------------------

_xRunCode tsc
         phd
         tcd
         php
         sei
         lda   #9
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $0A        ; $04
         jsr   _WriteData
         lda   $08        ; $06
         jsr   _WriteData
         lda   $06        ; $08
         jsr   _WriteData
         lda   $04        ; $0A
         jsr   _WriteData
         lda   $05        ; $0B
         jsr   _WriteData
         jsr   WaitHSOff
         jsr   WaitHSDone
         plp
         lda   $02
         sta   $0A
         lda   $01
         sta   $09
         pld
         tsc
         clc
         adc   #$0008
         tcs
         rtl

*----------------------------------------

_xClearScreen tsc
         phd
         tcd
         php
         sei
         lda   #10
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $0C        ; $04
         jsr   _WriteData
         lda   $08        ; $06
         jsr   _WriteData
         lda   $09        ; $07
         jsr   _WriteData
         lda   $0A        ; $08
         jsr   _WriteData
         lda   $04        ; $0A
         jsr   _WriteData
         lda   $05        ; $0B
         jsr   _WriteData
         lda   $06        ; $0C
         jsr   _WriteData
         jsr   WaitHSOff
         jsr   WaitHSDone
         plp
         lda   $02
         sta   $0C
         lda   $01
         sta   $0B
         pld
         tsc
         clc
         adc   #$000A
         tcs
         rtl

*----------------------------------------

_xSetShadow tsc
         phd
         tcd
         php
         sei
         lda   #11
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $04
         jsr   _WriteData
         jsr   WaitHSOff
         plp
         lda   $02
         sta   $04
         lda   $01
         sta   $03
         pld
         tsc
         clc
         adc   #$0002
         tcs
         rtl

*----------------------------------------

_xSetVGAReg tsc
         phd
         tcd
         php
         sei
         lda   #12
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $0A        ; $04
         jsr   _WriteCmd
         lda   $0B        ; $05
         jsr   _WriteCmd
         lda   $08        ; $06
         jsr   _WriteCmd
         lda   $06        ; $08
         jsr   _WriteCmd
         lda   $07        ; $09
         jsr   _WriteCmd
         lda   $04        ; $0A
         jsr   _WriteCmd
         jsr   WaitHSOff
         plp
         lda   $02
         sta   $0A
         lda   $01
         sta   $09
         pld
         tsc
         clc
         adc   #$0008
         tcs
         rtl

*----------------------------------------

_xGetVGAReg tsc
         sec
         sbc   #$0002
         tcs
         phd
         tcd
         php
         sei
         lda   #13
         jsr   _WriteCmd
         jsr   WaitHSOn
         lda   $0A        ; $06
         jsr   _WriteCmd
         lda   $0B        ; $07
         jsr   _WriteCmd
         lda   $08
         jsr   _WriteCmd
         lda   $06        ; $0A
         jsr   _WriteCmd
         lda   $07        ; $0B
         jsr   _WriteCmd
         jsr   WaitHSOff
         jsr   WaitHSOn
         ldal  ssREADDATA
         ldal  ssREADDATA
         sta   $01
         jsr   WaitHSOff
         plp
         ldy   $01
         lda   $04
         sta   $0A
         lda   $03
         sta   $09
         pld
         tsc
         clc
         adc   #$0008
         tcs
         tya
         rtl

*----------------------------------------

_xSetUserMode tsc
         phd
         tcd
         php
         sei
         lda   #14
         jsr   _WriteCmd
         jsr   WaitHSOn
         ldy   #0
]lp      lda   [$04],y
         stal  ssWRITEDATA
         iny
         iny
         cpy   #$0054
         bcc   ]lp
         jsr   WaitHSOff
         plp
         lda   $02
         sta   $06
         lda   $01
         sta   $05
         pld
         tsc
         clc
         adc   #$0004
         tcs
         rtl

*
* Routines bas niveau de la SecondSight
*

FalseRead sep  #$30
         ldal  ssCOMMAND
         rep   #$30
         rts

*----------------------------------------

LongDelay ldx  #256
]lp      dex
         bne   ]lp
         rts

*----------------------------------------

_WriteByte tax
         tax
         sep   #$30
         tay
         txa
         stal  ssWRITEDATA
         tya
]lp      cmpl  ssHANDSHAKE
         beq   ]lp
         rep   #$30
         rts

*----------------------------------------

_WriteCmd sep  #$20
         stal  ssCOMMAND
         rep   #$20
         rts

*----------------------------------------

_WriteData sep #$20
         stal  ssWRITEDATA
         rep   #$20
         rts

*----------------------------------------

WaitHSOn lda   #1
         sep   #$20
]lp      cmpl  ssHANDSHAKE
         bne   ]lp
         rep   #$20
         rts

*----------------------------------------

WaitHSOff lda  #0
         sep   #$20
]lp      cmpl  ssHANDSHAKE
         bne   ]lp
         rep   #$20
         rts

*----------------------------------------

WaitHSDone sep #$20
]lp      ldal  ssHANDSHAKE
         cmp   #$A5
         beq   WaitHSDone1
         cmp   #$A6
         bne   ]lp
WaitHSDone1 rep #$20
         rts

*----------------------------------------

WritePage phb
         sep   #$20
         pha
         plb
         rep   #$20
         phd
         lda   #$C000
         tcd

]LP      =     $0
         lup   128
         lda|  ]LP,X
         sta   $B1
]LP      =     ]LP+2
         --^

         pld
         plb
         rts
