	JMP init
	JMP	clavier
	DSW positiononehot	; 0000 0001 0000 0000
	DSW positionx	; 7 (index parmi les 16 colonnes)
	DSW	tir		; y a t'il un tir en cours ?
	DSW	tirpositionx	; position x du tir en onehot 0000 0001 0000 0000
	DSW	tirpositiony	; position y en index parmi les 16 lignes possibles
  DSW monsterx ; position du monstre en x
  DSW monstery ; position du monstre en y
  DSW monsterdir ; direction de deplacement : 0 gauche, 1 droite
	DSW	idxdbuff

init:	LDSPi	@stack@
	STI
	JMP	splash

splash:	DECSP ; écran de démarrage
	LDAi	0		;i = 0
	POKEA	1	
lsplu:	PEEKA	1		; A:= i début de la progression de ligne ascendante
	LDBi	10		; B := 10
	SUBB			; B := i - 10 ?
	JZB 	elsplu		; then goto end while (elspl)
	CALL	cls
	PEEKA	1		; A := i
	PUSHA
	LDBi	FFFF
	PUSHB
	CALL 	disl		; on affiche
	INCSP			; on enlève les opérandes
	INCSP
	CALL	swapbuf
	PEEKA	1
	LDBi	1
	ADDA
	POKEA	1		; i = i + 1
	JMP 	lsplu
elsplu:	LDAi	0		;i = 0
	POKEA	1	
lspld:	PEEKA	1		; A:= i début de la progression de ligne descendante
	LDBi	10		; B := 10
	SUBB			; B := i - 10 ?
	JZB 	elspld		; then goto end while (elspld)
	CALL	cls
	PEEKB	1		; A := i
	LDAi	F
	SUBA
	PUSHA
	LDBi	FFFF
	PUSHB
	CALL 	disl		; on affiche
	INCSP			; on enlève les opérandes
	INCSP
	CALL	swapbuf
	PEEKA	1
	LDBi	1
	ADDA
	POKEA	1		; i = i + 1
	JMP 	lspld
elspld:	LDAi	0
	PUSHA
	PUSHA
	CALL	disl
	INCSP
	INCSP			; Fin du splash screen
	JMP	main			

main:	CALL initplayer ; programme principal
  CALL initmonster
mainl:	CALL	steptir ; boucle principale : physique puis affichage
  DECSP ; on fait de la place pour stocker le resultat de fin de partie
  CALL testkill
  POPA ; on recupère dans A si on a gagné, perdu ou si on continue 0: rien / 1: gagné / 2: perdu
  JZA continue
  LDBi 1
  SUBB
  JZB victory
  JMP lost
continue:  CALL stepmonster
	CALL	affiche
	JMP	mainl

victory: END
lost: CALL cls
  CALL swapbuf
  LDAi 07E0
  STA 2001
  LDAi 1818
  STA 2002
  LDAi 2004
  STA 2003
  LDAi 2004
  STA 2004
  LDAi 4002
  STA 2005
  LDAi 4002
  STA 2006
  LDAi 9E79
  STA 2007
  LDAi BE7D
  STA 2008
  LDAi BE7D
  STA 2009
  LDAi BE7D
  STA 200A
  LDAi 5C3A
  STA 200B
  LDAi 2184
  STA 200C
  LDAi 1188
  STA 200D
  LDAi 1008
  STA 200E
  LDAi 1248
  STA 200F
  LDAi 0FF0
  STA 2010
  CALL swapbuf
  END

initplayer: LDAi	0 ; initialisation de la plateforme du joueur
	STA	tir
	STA	tirpositionx
	STA	tirpositiony
	LDAi	100		; One hot encoding !
	STA	positiononehot
	LDAi	7
	STA	positionx
  RET

initmonster: LDAi 8
  STA monsterx
  LDAi f
  STA monstery
  LDAi 0
  STA monsterdir
  RET

	;; If any, move the shot upward until getting out of the screen
steptir:LDAd	tir
	JZA	etir 		; un tir ? sinon on a terminé
	LDAd	tirpositiony
	LDBi	1
	ADDA
	STA	tirpositiony
	LDBi	10		; est ce qu'on est sorti de l'écran ?
	SUBB
	JZB	outtir
	JMP	etir
outtir:	LDAi	0
	STA	tir
	STA	tirpositiony
	STA	tirpositionx
etir:	RET

stepmonster: LDBd monsterdir
  JZB stepmonsterleft
stepmonsterright: LDAd monsterx
  DIVA
  STA monsterx
  JZA stepmonsterdownleft
  RET
stepmonsterdownleft: LDAi 1
  STA monsterx
  LDBi 1
  LDAd monstery
  SUBA
  STA monstery
  LDAi 0
  STA monsterdir
  RET
stepmonsterleft: LDAd monsterx
  LDBi 2
  MULA
  STA monsterx
  LDBi 8000
  SUBB
  JZB stepmonsterdownright
  RET
stepmonsterdownright: LDBi 1
  LDAd monstery
  SUBA
  STA monstery
  LDAi 1
  STA monsterdir
  RET

;; Test the winning conditions: 
;; Returns :  0: nothing / 1: win / 2: lost
testkill: LDAd monstery
  LDBi 1
  SUBB
  JZB testkillfailure; the player lost !
  LDAd monsterx
  LDBd tirpositionx
  SUBB
  JZB testkilly
testkillnothing: LDAi 0
  POKEA 2
  RET
testkillfailure: LDAi 2
  POKEA 2
  RET
testkilly: LDAd monstery
  LDBd tirpositiony
  SUBB
  JZB testkillsuccess
  JMP testkillnothing
testkillsuccess: LDAi 1
  POKEA 2
  RET

;; Display the game
affiche:CALL	cls
	LDAd	tirpositiony	; Affichage du tir
	LDBi	2001
	ADDB
	LDAd	tirpositionx	;
	STAB			;
	LDBd	positiononehot	; Affichage de la base du joueur
	LDAi	2
	MULA
	ADDB
	LDAd	positiononehot
	DIVA
	ADDB
	LDAi	2001
	STBA
	LDAd	positiononehot	; Petit canon
	LDBi	2002
	STAB
  ;; Monster
  LDAi 2001
  LDBd monstery
  ADDB
  LDAd monsterx
  STAB
	CALL	swapbuf
	RET

;; Clean the screen
cls:	DECSP
	LDAi	0
	POKEA	1
lcls:	LDAi	10
	PEEKB	1	
	SUBB
	JZB	elcls		; i = 16 ?
	PEEKA	1
	LDBi	2001
	ADDB
	LDAi	0
	STAB
	PEEKA	1
	LDBi	1
	ADDA
	POKEA 	1
	JMP lcls
elcls:	INCSP
	RET
	;; disl(line_number, value)
disl:	PEEKA	3
	LDBi	2001
	ADDB			; B contient l'adresse de la ligne
	PEEKA	2		; A contient la valeur à afficher
	STAB			; B -> RAM[A]
	RET
	
clavier:LDAi 0
  STA 6000
  LDAi 1
  STA 6000
  LDAd	6000
	LDBi	71
	SUBB
	JZB	qkey
	LDBi	64
	SUBB
	JZB	dkey
	LDBi	6C
	SUBB
	JZB	lkey
	JMP	eclavier
qkey:	LDAd	positionx	;Move left the plateform
	JZA	eqkey		; already at max left
	LDBi	1
	SUBA
	STA	positionx
	LDAd	positiononehot
	LDBi	2
	MULA
	STA	positiononehot
eqkey:	JMP	eclavier
dkey:	LDAd	positionx	;Move right the plateform
	LDBi	F
	SUBB
	JZB	edkey		; plateform already at max right
	LDBi	1
	ADDA
	STA	positionx
	LDAd	positiononehot
	DIVA
	STA 	positiononehot
edkey:	JMP 	eclavier
lkey:	LDAi	1
	STA	tir
	LDAd	positiononehot
	STA	tirpositionx
	LDAi	1
	STA	tirpositiony
elkey:	JMP	eclavier
eclavier:RTI

tdbuff:	LDAi	0
	STA	2000
	CALL	cls
	LDAi	1
	STA	2000
	CALL	cls
	DECSP
	LDAi	0
	POKEA	1
ltdbuf: PEEKA	1		; A:= i
	LDBi	10		; B := 10
	SUBB			; B := i - 10 ?
	JZB 	eltdbuf		; then goto end while (elspl)
	PEEKA	1
	LDBi	2001
	ADDB
	LDAi	FFFF
	STAB
	PEEKA	1
	LDBi	1
	ADDA
	POKEA	1
	JMP 	ltdbuf
eltdbuf:LDAi	0
	STA	2010
	END
swapbuf:LDAi	1
	LDBd	idxdbuff
	SUBA
	STA	idxdbuff
	STA	2000
	RET
