************************************************************************
*                                                                      *
*                             GAME SYSTEM                              *
*                             -----------                              *
*                                                                      *
* (c) Atreid Concept SA                      Version 0.5      22/12/90 *
************************************************************************



************************************************************************
GSInit         anop                     ;Temp2= ptr image font

               jsr Nombre16Init
               jsr Random
;               jsr SoundInit
               rts

GSTemp1        ds 4
GSTemp2        ds 4
GSTemp3        ds 4
GSTemp4        ds 4
GSTemp5        ds 4


;--------------------------
;init de 'Nombre16'

Nombre16Init   anop

               ldx #0                   ;on construit une routine pour
               txy                      ;faire la copie de l'image
               stz Temp                 ;source vers l'ecran
Nb16CopyLoop   anop
               lda #$bf                 ; LDA >$bbnnnn,x
               sta Nb16doCopy,x
               lda Temp2+1              ; bb
               sta Nb16doCopy+2,x
               lda Temp2                ; nnnn
               clc
               adc Temp
               sta Nb16doCopy+1,x
               inx
               inx
               inx
               inx
               lda #$99                 ; STA nnnn,y
               sta Nb16doCopy,x
               lda #$2000               ; nnnn
               clc
               adc Temp
               sta Nb16doCopy+1,x
               inx
               inx
               inx
               inc Temp
               inc Temp
               iny
               cpy #4
               bne Nb16CopyLoop
               ldy #0
               lda Temp
               clc
               adc #160-8
               sta Temp
               cmp #2560
               bne Nb16CopyLoop

               rts


;--------------------------
;init de 'Sound'

SoundInit      anop
               sei
               sep #$20
               longa off

               lda >$e100ca             ;acces aux registres
               and #$0f
               sta >$e0c03c

               lda #$e1                 ;registre $e1
               sta >$e0c03e             ;SoundAdrL
               lda #$40                 ;32 oscillateurs * 2
               sta >$e0c03d             ;SoundData

               rep #$30
               longa on

               cli
               rts





************************************************************************
PaintRect      anop                     ;GSTemp1  = coor. x
               anop                     ;GSTemp1+2= coor. y
               anop                     ;GSTemp2  = dep relatif x
               anop                     ;GSTemp2+2= dep relatif y
               anop                     ;GSTemp3  = couleur

               lda GSTemp2
               lsr a                    ;un octect = 2 pts
               sta GSTemp2
               lda GSTemp1
               lsr a                    ;un octect = 2 pts
               ldy GSTemp1+2
PRLoop         anop
               cpy #0
               beq PR2
               dey
               clc
               adc #160
               bra PRLoop

PR2            anop
               clc
               adc #$2000
               sta PR4+1
               ldx #0
               txy
PR3            anop
               lda GSTemp3
PR4            sta >$e12000,x
               inx
               inx
               cpx GSTemp2
               blt PR3
               lda PR4+1
               clc
               adc #160
               sta PR4+1
               ldx #0
               iny
               cpy GSTemp2+2
               blt PR3
               rts




************************************************************************
ErrTexte8      anop                     ;a= C string ptr
               anop                     ;x= position x
               anop                     ;y= position y

               stz GSTemp1              ;Invert ($1e)
               stz GSTemp2              ;Underline ($1f)
               pha
               txa
               clc
               lsr a
ErrT8L1        anop                     ;calcul de l'offset ecran
               cpy #0
               beq ErrT8L2
               clc
               adc #160
               dey
               bra ErrT8L1

ErrT8L2        anop
               sta GSTemp3              ;offset
               sta GSTemp4
               phd
               tsc
               tcd

               ldy #0
ErrT8L3        anop
               lda (3),y
               and #$ff
               cmp #0
               beq ErrT8L4
               phy
               jsr ErrT8Aff
               ply
               iny
               bra ErrT8L3

ErrT8L4        anop
               pld
               pla
               rts

ErrT8Aff       anop
               cmp #$20
               bge ErrT8Aff0
               brl ErrT8Spe
ErrT8Aff0      anop
               sec
               sbc #$20                 ;premier caractere est 'espace'
               stz GSTemp5
ErrT8Aff1      anop
               sec
               sbc #40                  ;40 caracteres par lignes
               bcc ErrT8Aff2
               pha
               lda GSTemp5
               clc
               adc #8*160               ;ligne suivante
               sta GSTemp5
               pla
               bra ErrT8Aff1

ErrT8Aff2      anop
               adc #40                  ;40 caracteres par lignes
               asl a
               asl a                    ;4 octets de large par caractere
               clc
               adc GSTemp5
               tay                      ;offset source

               ldx GSTemp3
               lda #0
ErrT8Aff3      anop
               pha
               lda ErrFont8,y
               eor GSTemp1
               sta >$e12000,x
               lda ErrFont8+2,y
               eor GSTemp1
               sta >$e12000+2,x
               tya
               clc
               adc #160
               tay
               txa
               clc
               adc #160
               tax
               pla
               inc a
               cmp #8
               bne ErrT8Aff3
               lda GSTemp2
               beq ErrT8Aff4            ;souligner ?

               lda GSTemp3
               clc
               adc #7*160
               lda GSTemp1
               eor #$ffff
               sta >$e12000,x
               sta >$e12000+2,x

ErrT8Aff4      anop
               lda GSTemp3
               clc
               adc #4
               sta GSTemp3
               rts


ErrT8Spe       anop
               cmp #$08                 ;fleche gauche
               beq ErrT8Left
               cmp #$09                 ;tab
               beq ErrT8Tab
               cmp #$0a                 ;fleche bas
               beq ErrT8Down
               cmp #$0b                 ;fleche haut
               beq ErrT8Up
               cmp #$0d                 ;return
               beq ErrT8CR
               cmp #$15                 ;fleche droite
               beq ErrT8Right
               cmp #$1f                 ;toggle underline
               beq ErrT8Under
               cmp #$1e                 ;toggle invert
               beq ErrT8Invert
               rts

ErrT8Left      anop
               lda GSTemp3
               sec
               sbc #4
               sta GSTemp3
               rts

ErrT8Tab       anop
               lda GSTemp3
               clc
               adc #40                  ;+10 caracteres
               sta GSTemp3
               rts

ErrT8Down      anop
               lda GSTemp3
               clc
               adc #8*160
               sta GSTemp3
               rts

ErrT8Up        anop
               lda GSTemp3
               sec
               sbc #8*160
               sta GSTemp3
               rts

ErrT8CR        anop
               lda GSTemp4
               clc
               adc #8*160
               sta GSTemp3
               sta GSTemp4
               rts

ErrT8Right     anop
               lda GSTemp3
               clc
               adc #4
               sta GSTemp3
               rts

ErrT8Under     anop
               lda GSTemp2
               eor #$8000
               sta GSTemp2
               rts

ErrT8Invert    anop
               lda GSTemp1
               eor #$ffff
               sta GSTemp1
               rts

               copy GSFont.Src





************************************************************************
TransTSB16     anop                     ;GSTemp1= adresse dest

               PushLong GSTemp1

               phd
               tsc
               tcd

               stz GSTemp2
               stz GSTemp3
               ldx #0
               ldy #0

TSBRoutLoop    anop
               lda #$04                 ;TSB nn
               sta [3],y
               iny
               lda GSTemp3              ;nn
               inc GSTemp3
               inc GSTemp3
               sta [3],y
               iny

NextTSB        anop
               lda GSTemp3
               cmp #166                 ;Ligne + nb oct/ligne
               beq NextT1
               cmp #6                   ;nb octets/ligne
               bne NextT2
               lda GSTemp3
               clc
               adc #160-6               ;Ligne - nb oct/ligne
               sta GSTemp3
               bra NextT2
NextT1         anop
               stz GSTemp3
               lda #$7b                 ;TDC
               sta [3],y
               iny
               lda #$69                 ;ADC #nnnn
               sta [3],y
               iny
               lda #320                 ;nnnn A 2 Lignes
               sta [3],y
               iny
               iny
               lda #$5b                 ;TCD
               sta [3],y
               iny
               lda #$98                 ;TYA
               sta [3],y
               iny
NextT2         anop
               inx
               inx
               cpx #66                  ;Nb octetsA 6octets*11lignes
               bne TSBRoutLoop
RoutLoop2      lda #$6b                 ;RTL
               sta [3],y
               iny

               pld
               pla
               pla
               rts





************************************************************************
Linear16       anop                     ;GSTemp1= adresse source
               anop                     ;GSTemp2= adresse dest

               PushLong GSTemp1
               PushLong GSTemp2

               phd
               tsc
               tcd

               ldy #0
               ldx #0
Linear16Loop   anop
               lda [7],y
               phy
               txy
               sta [3],y
               ply
               iny
               iny
               inx
               inx
               lda [7],y
               phy
               txy
               sta [3],y
               ply
               iny
               iny
               inx
               inx
               lda [7],y
               phy
               txy
               sta [3],y
               ply
               iny
               iny
               inx
               inx
               lda [7],y
               phy
               txy
               sta [3],y
               ply
               inx
               inx
               tya
               clc
               adc #160-6               ;16 points par ligne
               tay
               cpx #16*8                ;16 lignes * 8 octets
               blt Linear16Loop

               pld
               pla
               pla
               pla
               pla
               rts





************************************************************************
ZPBank         anop                     ;a=0/1 zp-s bank $0/1

               plx
               cmp #0
               beq ZPBank0

               tsc
               sta ZPBankStack

               sep #$30
               longa off
               lda >$e0C068
               sta ZPBankStat
               ora #$34
               sta >$e0C068
               rep #$30
               longa on

               lda #$9df0
               tcs                      ;fixe la stack apres les SCBs
               phx
               rts

ZPBank0        anop
               longa off
               sep #$30
               lda ZPBankStat
               sta >$e0C068
               rep #$30
               longa on

               lda ZPBankStack
               tcs
               phx
               rts

ZPBankStat     ds 1
ZPBankStack    ds 2





************************************************************************
CursorCSet16   anop                     ;a= adresse data curseur (16*16)
               anop                     ;y=$0101/$e1e1 bank dest
               anop                     ;x= adresse memoire destination

               sty GSTemp5
               sta CursorRoutLoop+1
               phx

               phd
               tsc
               tcd

               jsr CursorLoop0

               pld
               pla
               rts

CursorLoop0    anop
               ldy #0
               ldx #0
               stz GSTemp4
               stz GSTemp3
               stz GSTemp1
               lda #$8b                 ;PHB
               sta (3),y
               iny
               lda #$a9                 ; LDA #nnnn
               sta (3),y
               iny
               lda GSTemp5              ;nnnn
               sta (3),y
               iny
               iny
               lda #$48                 ;PHA
               sta (3),y
               iny
               lda #$ab                 ;PLB
               sta (3),y
               iny
               lda #$ab                 ;PLB
               sta (3),y
               iny

CursorRoutLoop lda NextWord2,x
               sta GSTemp2
               jsr TestTrans
               bcs doTrans20
               bra doCopy2

NextWord2      anop
               inc GSTemp1
               lda GSTemp1
               cmp #4                   ;Nb word/ligne
               bne CursorNW2
               stz GSTemp1
               lda GSTemp3
               clc
               adc #152                 ;160 - 8 (nb oct/ligne)
               sta GSTemp3
CursorNW2      anop
               inx
               inx
               cpx #128                 ;Nb octetsA 8octets*16lignes
               bne CursorRoutLoop
               brl TransLoop22

doCopy2        anop
               lda GSTemp2
               cmp GSTemp4
               beq SameSTY2
               lda #$a9                 ;LDA #nnnn
               sta (3),y
               iny
               lda GSTemp2              ;nnnn
               sta (3),y
               iny
               iny
               sta GSTemp4
SameSTY2       anop
               lda #$9d                 ;STA nnnn,x
               sta (3),y
               iny
               lda GSTemp3
               inc GSTemp3
               inc GSTemp3
               sta (3),y                ;nnnn
               iny
               iny
               brl NextWord2

doTrans20      anop
               phx
               stz GSTemp4
               lda #$000f
               tax
               and GSTemp2
               jsr ModTemp4
               lda #$00f0
               tax
               and GSTemp2
               jsr ModTemp4
               lda #$0f00
               tax
               and GSTemp2
               jsr ModTemp4
               lda #$f000
               tax
               and GSTemp2
               jsr ModTemp4
               lda GSTemp4
               bne doTrans22
               inc GSTemp3
               inc GSTemp3
               plx
               brl NextWord2
doTrans22      anop
               lda #$bd                 ;LDA nnnn,x
               sta (3),y
               iny
               lda GSTemp3
               sta (3),y
               iny
               iny
               lda #$29                 ;AND #nnnn
               sta (3),y
               iny
               lda GSTemp4              ;nnnn
               eor #$ffff
               sta (3),y
               iny
               iny
               lda #$09                 ;ORA #nnnn
               sta (3),y
               iny
               lda GSTemp2              ;nnnn
               sta (3),y
               iny
               iny
               lda #$9d                 ;STA nnnn,x
               sta (3),y
               iny
               lda GSTemp3              ;nnnn
               inc GSTemp3
               inc GSTemp3
               sta (3),y
               iny
               iny
               stz GSTemp4
               plx
               brl NextWord2

TransLoop22    anop
               lda #$ab                 ;PLB
               sta (3),y
               iny
               lda #$6b                 ;RTL
               sta (3),y
               iny
               rts

ModTemp4       anop
               cmp #0
               beq MT4End
               txa
               ora GSTemp4
               sta GSTemp4
MT4End         rts

TestTrans      anop
               and #$000f
               beq TransOk
               lda GSTemp2
               and #$00f0
               beq TransOk
               lda GSTemp2
               and #$0f00
               beq TransOk
               lda GSTemp2
               and #$f000
               beq TransOk
               clc
               rts
TransOk        sec
               rts





************************************************************************
WaitUser       anop                     ;--> a= caractere tape
               anop                     ;       0 si clic ou fire

               sep #$30
               longa off
               longi off

               lda #0
               lda >$e0c024
               sta >$e0c010             ;echantillonne clavier
WULoop         anop
               rep #$30
               longa on
               longi on
               lda #150
               jsr WaitSpot
               jsr WaitVBL
               longa off
               longi off
               sep #$30
               lda >$e0C000             ;teste touche
               bmi WUDone1
               lda >$e0C061             ;teste fire 0
               bmi WUDone
               lda >$e0C062             ;teste fire 1
               bmi WUDone
               lda >$e0C027             ;teste clic souris
               bpl WULoop
               and #%00000010
               bne WULoop2
               lda >$e0C024
               lda >$e0C024
               bmi WULoop
               bra WUDone
WULoop2        anop
               lda >$e0C024
               bra WULoop


WUDone         anop
               lda #0
WUDone1        anop
               rep #$30
               longa on
               longi on
               and #$7f
               rts




***********************************************************************
Fade           anop                     ;a=1 fade in     a=0 fade out
               anop                     ;Source bank $01, dest bank $e1
               cmp #0
               bne FadeIn
               brl FadeOut

FadeIn         anop
               ldx #0
Shadow1        anop
               lda >$012000,x
               sta >$e12000,x
               inx
               inx
               cpx #32256               ;copie image + SCBs
               bne Shadow1

               ldy #16
               ldx #0
FadeInLoop     anop
               lda >$019e00,x
               sta Temp4+2
               lda >$e19e00,x
               sta Temp4
               cmp Temp4+2
               beq FILNoChange
               lda Temp4
               and #$0f00
               sta Temp
               lda Temp4
               and #$00f0
               sta Temp2
               lda Temp4
               and #$000f
               sta Temp3
               lda Temp4+2
               and #$0f00
               cmp Temp
               beq FIL1
               lda Temp
               clc
               adc #$0100
               sta Temp
FIL1           anop
               lda Temp4+2
               and #$00f0
               cmp Temp2
               beq FIL2
               lda Temp2
               clc
               adc #$0010
               sta Temp2
FIL2           anop
               lda Temp4+2
               and #$000f
               cmp Temp3
               beq FIL3
               lda Temp3
               clc
               adc #$0001
               sta Temp3
FIL3           anop
               lda Temp
               ora Temp2
               ora Temp3
               sta >$e19e00,x
FILNoChange    anop
               dey
               bne FILNextVBL
               jsr NextVBL
FILNextVBL     anop
               inx
               inx
               cpx #64                  ;fade sur 2 palettes
               beq FIL4
               brl FadeInLoop
FIL4           anop
               ldx #0
               dey
               beq FIL5
               brl FadeInLoop
FIL5           anop
               rts

NextVBL        anop
               phx
               lda #150
               jsr WaitSpot
               jsr WaitVBL
               plx
               ldy #16
               rts

FadeOut        anop
               ldy #16
               ldx #0
FadeOutLoop    anop
               lda >$e19e00,x
               cmp #0
               beq FOLNoChange
               lda >$e19e00,x
               and #$0f00
               sta Temp
               lda >$e19e00,x
               and #$00f0
               sta Temp2
               lda >$e19e00,x
               and #$000f
               sta Temp3
               lda Temp
               beq FOL1
               lda Temp
               sec
               sbc #$0100
               sta Temp
FOL1           anop
               lda Temp2
               beq FOL2
               lda Temp2
               sec
               sbc #$0010
               sta Temp2
FOL2           anop
               lda Temp3
               beq FOL3
               lda Temp3
               sec
               sbc #$0001
               sta Temp3
FOL3           anop
               lda Temp
               ora Temp2
               ora Temp3
               sta >$e19e00,x
FOLNoChange    anop
               dey
               bne FOLNextVBL
               jsr NextVBL
FOLNextVBL     anop
               inx
               inx
               cpx #64                 ;fade sur 2 palettes
               bne FadeOutLoop
               ldx #0
               dey
               bne FadeOutLoop
               rts



************************************************************************
Decompacte     anop                     ;Temp= handle
               anop                     ;Temp2= ptr dest

               PushLong Temp
               phd                      ;on dereference le handle
               tsc
               tcd
               ldy #0
               lda [3],y
               sta Temp3
               ldy #2
               lda [3],y
               sta Temp3+2
               pld
               pla
               pla
 
               PushLong #0
               PushLong Temp
               _GetHandleSize           ;Taille du handle ?
               PullLong Temp4

               lda #32768
               sta UnPackAreaSz

               PushWord #0
               PushLong Temp3
               PushWord Temp4
               PushLong #Temp2
               PushLong #UnpackAreaSz
               _UnPackBytes
               pla
               rts

UnPackAreaSz   dc i2'32768'




************************************************************************
WaitVBL        anop
               lda >$e0C019
               and #%0000000010000000
               beq WaitVBL
               rts




************************************************************************
Texte16        anop                     ;a= ptr str
               anop                     ;x= coordonnee x
               anop                     ;y= coordonnee y
               anop                     ;CarPtr= ptr image source

               pha
               txa
               lsr a
               sta Temp2
               lda CarPtr
               sta Temp
               lda CarPtr+2
               sta Temp+2
               lda #0
MemY           anop
               clc
               adc #160
               dey
               bne MemY
               clc
               adc Temp2
               sta Temp2                ;offset sur l'ecran

               pla
               sta T16length+1
               inc a
               sta T16Read+1
T16length      lda T16length
               and #$ff
               sta Temp3
               bne T16NotZero
               rts                      ;si len$(str)=0 on s'en va

T16NotZero     anop
               ldx #0
T16Read        lda T16Read,x
               and #$ff
               cmp #128
               bge T16Aff0
               sta Temp4
               ldy #0
T16Trans       anop
               lda T16Car,y
               and #$ff
               cmp Temp4
               beq T16Aff
               iny
               cpy T16NbCar
               bne T16Trans
               bra T16Aff

T16Aff0        anop
               and #$7f
               tay
T16Aff         anop
               phx
               tya
               ldx #0
               sec
T16Aff2        anop
               sbc #20
               inx
               bcs T16Aff2

               adc #20
               asl a
               asl a
               asl a

T16Aff3        anop
               dex
               cpx #0
               beq T16Aff4
               clc
               adc #16*160
               bra T16Aff3

T16Aff4        anop
               jsr Sprite16

               lda Temp2
               clc
               adc #8
               sta Temp2
               plx
               inx
               cpx Temp3
               bne T16Read

               rts

T16Car         dc c'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
               dc c'.:!?#0123456789 '
T16CarEnd      anop
T16NbCar       dc i2'T16CarEnd-T16Car'




************************************************************************
Nombre16       anop                     ;a= ptr str
               anop                     ;x= coordonnee x
               anop                     ;y= coordonnee y
               anop                     ;CarPtr= ptr image source

               pha
               txa
               lsr a
               sta Temp2
               lda #0
Nb16MemY       anop
               clc
               adc #160
               dey
               bne Nb16MemY
               clc
               adc Temp2
               sta Temp2                ;offset sur l'ecran

               pla
               sta Nb16Length+1
               inc a
               sta Nb16Read+1
               stz Temp4
Nb16Length     lda Nb16Length
               and #$ff
               sta Temp3
               dec Temp3
               ldx #0
Nb16Read       lda Nb16Read,x
               and #$ff
               cmp #32                  ;espace
               bne Nb16a
               lda #48                  ;chiffre 0
Nb16a          anop
               cmp #76
               bge Nb16b
               clc
               adc #66-48               ;0 dans image - 0 ascii
Nb16b          anop
               phx
               jsr Nb16affiche
               plx
               lda Temp4                ;espace pour caractere suivant
               clc
               adc #8
               sta Temp4
               inx
               cpx Temp3
               blt Nb16Read

               rts

Nb16Affiche    anop
               ldx #0
               sec
Nb16Aff2       anop
               sbc #20
               inx
               bcs Nb16Aff2

               adc #20
               asl a
               asl a
               asl a

Nb16Aff3       anop
               dex
               cpx #0
               beq Nb16Aff4
               clc
               adc #16*160
               bra Nb16Aff3

Nb16Aff4       anop
               tax
               lda Temp2                ;offset ecran source
               clc
               adc Temp4
               tay                      ;offset ecran dest

Nb16Copy       anop                     ;routine de copie des objets
               lda #$0101
               pha
               plb
               plb
Nb16doCopy     ds 4*(4+3)*16
               phk
               plb
               rts




************************************************************************
HexStr         anop                     ;Temp= valeur 32 bits
               anop                     ;Temp2= ptr de string
               anop                     ;a= nb caractere de la str

               ldx Temp2
               sta Temp3
               inc Temp3
               inx
               stx HexSClr+1
               tax
               lda #48                  ;chiffre 0 en ascii
HexSClr        sta HexSClr,x
               dex
               bne HexSClr

               PushLong Temp
               PushLong Temp2
               PushWord Temp3
               PushWord #0
               _Long2Dec

               lda ScoreStr
               and #$ff00
               ora Temp3
               sta ScoreStr

               rts




************************************************************************
Sprite16       anop                     ;Temp= ptr image source
               anop                     ;Temp2= offset ecran dest
               anop                     ;a= offset ecran source

               ldx Temp+1
               stx Sp16Read+2
               clc
               adc Temp
               sta Sp16Read+1
               lda #$2000
               clc
               adc Temp2
               sta SP16Write+1
               sep #$20
               longa off
               lda #$e1                 ;page video
               pha
               plb
               ldx #0
               txy
Sp16Read       lda >Sp16Read,x
               beq Sp16Next
Sp16Write      sta Sp16Write,x
Sp16Next       inx
               iny
               cpy #8
               bne Sp16Read
               ldy #0
               rep #$30
               longa on
               txa
               clc
               adc #160-8
               tax
               sep #$20
               longa off
               cpx #16*160
               bne Sp16Read

               rep #$30
               longa on
               phk
               plb
               rts




************************************************************************
CopyZone       anop                     ;Temp   = coor. x
               anop                     ;Temp+2 = coor. y
               anop                     ;Temp2  = dep relatif x
               anop                     ;Temp2+2= dep relatif y

               lda Temp2
               lsr a                    ;un octect = 2 pts
               sta Temp2
               lda Temp
               lsr a                    ;un octect = 2 pts
               ldy Temp+2
CZLoop         anop
               cpy #0
               beq CZ2
               dey
               clc
               adc #160
               bra CZLoop

CZ2            anop
               clc
               adc #$2000
               sta CZ3+1
               sta CZ4+1
               ldx #0
               txy
CZ3            lda >$012000,x
CZ4            sta >$e12000,x
               inx
               inx
               cpx Temp2
               blt CZ3
               lda CZ3+1
               clc
               adc #160
               sta CZ3+1
               sta CZ4+1
               ldx #0
               iny
               cpy Temp2+2
               blt CZ3
               rts




************************************************************************
WaitSpot       anop                     ;a= ligne

               lsr a
               longa off
               sep #$30
               sta WS1+1
WaitS          anop
               lda >$e0C02e
               and #$7f
WS1            cmp #0
               blt WaitS
               cmp #100
               bge WaitS
               longa on
               rep #$30

               rts




************************************************************************
Random         anop                     ;--> a= rnd(256)

               lda >$e0c02e
               xba
               clc
               adc VBLCounter0
               sta VBLCounter0
               and #$ff
               rts




************************************************************************
Pause          anop                     ;a = temps de pause (1/60 sec)
               pha
               jsr WaitVBL              ;on force l'attente a 1/60s
PauseVBL       anop
               lda >$e0C019
               and #%0000000010000000
               bne PauseVBL
               pla
               dec a
               bne Pause
               rts




************************************************************************
ReadMouse      anop                     ;a --> 0 A pas d'info souris
               anop                     ;a --> $8000
               anop                     ;==> Mx--> coor X   My--> coor Y
               anop                     ;    MBut--> bouton

               lda Mx
               sta Mx2
               lda My
               sta My2
               lda MBut
               sta MBut2

               longa off
               longi off
               sep #$30
               lda >$e0C027
               bpl RMDonebis
               and #2
               beq DoXY
               lda >$e0C024
RMDonebis      lda #0                   ;pas d'info souris
               brl RMDone
DoXY           lda >$e0C024
               tax
               lda >$e0c024
               sta Temp
               longa on
               longi on
               rep #$30
               txa
               and #$ff
               tax
               and #%01000000
               bne XPos
               txa
               and #%00111111
               clc
               adc Mx
               sta Mx
               bra NowDoY
XPos           anop
               txa
               ldx Mx
               and #%00111111
               eor #%00111111
               inc a
               sta Mx
               txa
               sec
               sbc Mx
               sta Mx
NowDoY         lda Temp
               and #$FF
               tax
               and #%01000000
               bne YPos
               txa
               and #%00111111
               clc
               adc My
               sta My
               bra XYDone
YPos           anop
               txa
               and #%00111111
               eor #%00111111
               inc a
               ldx My
               sta My
               txa
               sec
               sbc My
               sta My
XYDone         anop
               stz MBut
               lda Temp
               and #$80
               bne RMDoUp
               lda #$8000
               sta MBut

RMDoUp         anop
               lda Mx
               cmp #$f000
               bge doXMin
               cmp #309                 ;max x
               bge doXMax
               bra ClampY
doXMin         stz Mx
               bra ClampY
doXMax         lda #308                 ;clamp x
               sta Mx
ClampY         anop
               lda My
               cmp #$f000
               bge doYMin
               cmp #128                 ;max y
               bge doYMax
               bra RMDone2
doYMin         stz My
               bra RMDone2
doYMax         lda #127                 ;clamp y
               sta My

RMDone2        anop
               lda Mx
               cmp Mx2
               bne RMDone1
               lda My
               cmp My2
               bne RMDone1
               lda MBut
               cmp MBut2
               bne RMDone1
               lda #0
               bra RMDone
RMDone1        lda #$8000
RMDone         rep #$30
               rts

Mx             ds 2
Mx2            ds 2
My             ds 2
My2            ds 2
MBut           ds 2
MBut2          ds 2





************************************************************************
Shadow         anop                     ;a = 1 A shadow on
               anop                     ;a = 0 A shadow off

               eor #1
               asl a
               asl a
               asl a
               ora #%00010001 
               sep #$30
               sta >$e0C035             ;shadowing video du bank $01
               rep #$30
               rts





************************************************************************
Sound          anop                     ;a = frequence du son
               anop                     ;x = Ptr bloc info son
               anop                     ;==> Bloc info son (words)A
               anop                     ; _ Volume ($0 - $ff)
               anop                     ; _ Page adresse dans DOC ($0 - $ff)
               anop                     ; _ Longueur du son
               anop                     ; _ Controle du son

               sei

               sta GSTemp2
               phx
               phd                      ;on lit les infos son
               tsc
               tcd

               sep #$20
               longa off

               lda >$e100ca             ;volume systeme
               and #$0f                 ;acces aux registres du DOC
               sta >$e0c03c

               lda SoundCh              ;Reg frequence (bas)
               sta >$e0c03e
               lda GSTemp2              ;Frequence
               sta >$e0c03d
               lda SoundCh              ;Reg frequence (haut)
               clc
               adc #$20
               sta >$e0c03e
               lda GSTemp2+1            ;Frequence
               sta >$e0c03d

               lda SoundCh
               clc
               adc #$40                 ;Reg Volume
               sta >$e0c03e
               ldy #0
               lda (3),y                ;Lecture Volume
               sta >$e0c03d

               lda SoundCh
               clc
               adc #$80                 ;Reg adresse Ram DOC
               sta >$e0c03e
               ldy #2
               lda (3),y                ;Lecture adresse
               sta >$e0c03d

               lda SoundCh
               clc
               adc #$C0                 ;Reg taille du son
               sta >$e0c03e
               ldy #4
               lda (3),y                ;Lecture taille du son
               sta >$e0c03d

               lda SoundCh
               clc
               adc #$A0                 ;Reg controle du son
               sta >$e0c03e
               ldy #6
               lda (3),y                ;Lecture controle
               sta >$e0c03d             ;Le son est joue !

               lda SoundCh
               inc a
               and #$f                 ;16 oscillos !
               sta SoundCh

               rep #$30
               longa on

               pld
               pla

               cli
               rts

SoundCh        ds 2





************************************************************************
ClearDOC       anop

               sei
               sep #$20
               longa off

               lda >$e100ca             ;volume systeme
               and #$0f                 ;acces a la ram DOC
               ora #%01100000           ;auto inc des pointeurs
               sta >$e0c03c

               lda #0
               sta >$e0c03e             ;adresse basse
               sta >$e0c03f             ;adresse haute dans le DOC

               ldy #0
               lda #0
ClearDOCLoop   anop
               sta >$e0c03d             ;sound doc
               iny
               bne ClearDOCLoop

               rep #$30
               longa on

               cli
               rts




************************************************************************
SoundLoad      anop                     ;Temp = handle du son
               anop                     ;Temp2 = ptr bloc info son

               sei

               PushLong #0
               PushLong Temp
               _GetHandleSize           ;taille du son en octets
               PullLong Temp3

               PushLong Temp
               PushLong Temp2

               phd
               tsc
               tcd

               ldy #0
               lda [7],y                ;dereference de Temp
               tax
               ldy #2
               lda [7],y
               stx <7
               sta <9

               sep #$20
               longa off

               ldy #0
               lda #$ff                 ;on fixe le volume a 255
               sta [3],y

               ldx #7
               lda Temp3+1              ;nombre de pages mem du son
               bmi SoundDOCok0          ;size = 32 ko   si   son > 32 ko
               ldx #0
SoundDOCLoop0  anop
               cmp SoundSzTable,x       ;table des tailles possibles
               blt SoundDOCok0
               inx
               bra SoundDOCLoop0
SoundDOCok0    anop
               stx Temp
               txa                      ;taille en pages (puissance de 2)
               asl a
               asl a
               asl a
               ora Temp                 ;resolution
               ldy #4
               sta [3],y

               lda >$e100ca             ;volume systeme
               and #$0f                 ;acces a la ram DOC
               ora #%01000000
               sta >$e0c03c

               stz Temp2
FindDOCadr     anop
               lda Temp2
               sta >$e0c03f             ;adr haute
               lda #0
               sta >$e0c03e             ;adr basse
               lda >$e0c03d             ;une lecture pour rien
               ldy #0
FindDOCadr2    anop
               lda >$e0c03d             ;lecture du DOC
               bne FindDOCadr3
               rep #$30
               lda >$e0c03e
               inc a                    ;incrementation manuelle !
               sta >$e0c03e
               sep #$20
               iny
               cpy Temp3                ;longueur du son (octets)
               blt FindDOCadr2
               bra FindDOCadr5
FindDOCadr3    anop
               ldx Temp
               lda SoundSzTable,x
               clc
               adc Temp2
               sta Temp2
               bcc FindDOCadr           ;si le DOC est pleinA page 0!

FindDOCadr5    anop
               lda Temp2                ;adresse du son dans le DOC
               ldy #2
               sta [3],y

               ldy #6
               lda #%00000010           ;Controle de l'oscillo
               sta [3],y                ;--> droite, pas int, one-shot

               lda >$e100ca             ;volume systeme
               and #$0f                 ;acces a la ram DOC
               ora #%01100000           ;auto inc des pointeurs
               sta >$e0c03c

               lda #0
               sta >$e0c03e             ;adresse basse
               ldy #2
               lda [3],y                ;lecture page mem
               sta >$e0c03f             ;adresse haute dans le DOC
               ldy #0
LoadDOCLoop    anop
               lda [7],y
               bne LoadDOCL2
               lda #1
LoadDOCL2      anop
               sta >$e0c03d             ;sound doc
               iny
               cpy Temp3                ;nb octets du son
               bne LoadDOCLoop
               lda #0
               sta >$e0c03d             ;sound doc
               sta >$e0c03d
               sta >$e0c03d
               sta >$e0c03d
               sta >$e0c03d
               sta >$e0c03d
               sta >$e0c03d
               sta >$e0c03d

               rep #$30
               longa on

               pld
               pla                      ;on enleve les ptrs !
               pla
               pla
               pla

               cli
               rts


SoundSzTable   dc i1'1'
               dc i1'2'
               dc i1'4'
               dc i1'8'
               dc i1'16'
               dc i1'32'
               dc i1'64'
               dc i1'128'




************************************************************************
ReadJoy        anop                     ;mx / my
               anop                     ;==> a=0 : pas de deplacement

               lda Mx
               sta Mx2
               lda My
               sta My2
               sei

               stz Paddle0
               stz Paddle1
               stz Paddle2
               stz Paddle3

               sep #$30
               longa off
               longi off

               ldy #$30                 ;Sampling value

               lda >$e0c036             ;Force GS Speed to 1Mhz
               pha
               and #$7f
               sta >$e0c036

               lda >$e0c070             ;Prepare to read paddles
               nop

APaddle0       lda >$e0c064             ;paddle 0
               bmi AIncPaddle0
               bra APaddle1
AIncPaddle0    inc Paddle0

APaddle1       lda >$e0c065             ;paddle 1
               bmi AIncPaddle1
               bra APaddle2
AIncPaddle1    inc Paddle1

APaddle2       lda >$e0c066             ;paddle 2
               bmi AIncPaddle2
               bra APaddle3
AIncPaddle2    inc Paddle2

APaddle3       lda >$e0c067             ;paddle 3
               bmi AIncPaddle3
               bra AContinue
AIncPaddle3    inc Paddle3

AContinue      bra Adey
Adey           dey

               bne APaddle0

               pla
               sta >$e0c036             ;Restore GS speed
               cli

               rep #$30
               longa on
               longi on

               lda Paddle0
               and #$ff                 ;resultat
               cmp #$18
               bge AdcX
               sta Temp
               lda #$18
               sec
               sbc Temp
               clc
               lsr a
               lsr a
               lsr a
               sta Temp
               lda Mx
               sec
               sbc Temp
               cmp #$f000
               blt ReadXEnd0
               lda #0
               bra ReadXEnd0
AdcX           anop
               sec
               sbc #$18
               clc
               lsr a
               lsr a
               lsr a
               clc
               adc Mx
               cmp #309
               blt ReadXEnd0
               lda #308

ReadXEnd0      anop
               sta Mx

ReadXEnd       anop
               lda Paddle1
               and #$ff                 ;resultat
               cmp #$18
               bge AdcY
               sta Temp
               lda #$18
               sec
               sbc Temp
               clc
               lsr a
               lsr a
               lsr a
               sta Temp
               lda My
               sec
               sbc Temp
               cmp #$f000
               blt ReadYEnd0
               lda #0
               bra ReadYEnd0
AdcY           anop
               sec
               sbc #$18
               clc
               lsr a
               lsr a
               lsr a
               adc My
               cmp #128
               blt ReadYEnd0
               lda #127

ReadYEnd0      anop
               sta My
ReadYEnd       anop
               lda >$e0c061
               and #%0000000010000000
               beq RNotFire
               lda #1
               sta MBut
               bra RDepTest
RNotFire       anop
               stz MBut

RDepTest       anop
               lda Mx
               cmp Mx2
               bne RDepTestF
               lda My
               cmp My2
               bne RDepTestF
               lda #0
               rts
RDepTestF      anop
               lda #$80
               rts

Paddle0        ds 2
Paddle1        ds 2
Paddle2        ds 2
Paddle3        ds 2

