 org $2000
 ; CIO
ICHID = $0340 ;IOCB 0 S:
ICCOM = $0342 ;IOCB Command
ICBAL = $0344 ;Xfer Buffer Adr
ICBAH = $0345
ICPTL = $0346 ;PutByte Adr
ICPTH = $0347
ICBLL = $0348 ;Buffer Len
ICBLH = $0349
CIOV = $E456 ; CIO Vector
; Setup CIO Call
start
     	ldx #<text
	ldy #>text
	jsr $c642


	ldx #<empty_line
 	ldy #>empty_line
	jsr $c642

	lda #0
	sta $ff
lp	jsr wait	
	ldy $ff
	tya
	asl @
 	tay
 	lda users,y
 	sta lower+1
	iny
	lda users,y
	sta higher+1
lower	ldx #$0
higher  ldy #$0
	jsr $c642
	inc $ff
        lda $ff
        cmp #6
	bne lp

	ldx #<press
 	ldy #>press
	jsr $c642

	lda $2fc
        cmp $2fc
	beq *-3

	ldx #<running
 	ldy #>running
	jsr $c642
	
        lda #0
	sta $ff

	jsr wait
	lda $2fc
	cmp $2fc
	beq *-3

	ldx #<chosen
 	ldy #>chosen
	jsr $c642

lp2
	jsr wait
        jsr randomize
        jsr compare
        jsr print  
        inc $ff
        lda $ff
        cmp #5
        bne lp2      
   
	ldx #<cong
 	ldy #>cong
	jsr $c642
        	
     jmp *
wait lda 20
     clc
     adc #30
     cmp 20
     bne *-2
     rts
randomize 
	lda $d20a
	and #%00000001
	sta first+1
	lda $d20a
	and #%00000001
	sta second+1
	lda $d20a
	and #%00000011
	clc
first	adc #0
	clc
second  adc #0
	rts
compare ldy #0
	cmp numberz,y
	beq from_begin
	iny
	cpy #5
	bne compare+2
nxt
	ldy $ff
	sta numberz,y
	rts 
from_begin pla
	   pla
	   jmp lp2

print   
 	asl @
	tay
	lda users,y
	sta q1+1
	iny
	lda users,y
	sta q2+1
q1	ldx #$0
q2	ldy #$0
	jsr $c642
	rts
		

text dta c "Welcome into randomize program. There are 6 participians:",$9b
numer1 dta c "1.CharlieChaplin",$9b
numer2 dta c "2.Mgr.Inz.Rafal",$9b
numer3 dta c "3.Greg",$9b
numer4 dta c "4.Mariuszbox",$9b
numer5 dta c "5.Renton",$9b
numer6 dta c "6.Xeen",$9b

users dta a(numer1),a(numer2),a(numer3),a(numer4),a(numer5),a(numer6)

empty_line dta c "            ",$9b
press      dta c "Press any key to run random generator.",$9b
running      dta c "Random generator started.Press any key to stop.",$9b
chosen       dta c "5 chosen people:",$9b
cong         dta c "Congratulations! Please contact me at gorgh@o2.pl to finalize the deal.",$9b
numberz    .byte 10,10,10,10,10,10
 run start
