_stdbuf equ 1536
_arr equ 128
_putchr_proc_adr equ 1619
_std_print_out___5 equ _stdbuf
_std_print_out___6 equ _putchr_proc_adr
key__Q equ 47
key__none equ 255
CH equ 764
RTCLOCK equ 20
k equ 130
j equ 132
time equ 134
_s2__i equ 135
   org $2e0
   dta a($2000)
   org $2000
;### esieve.atl(16) "Computing primes..."
   jsr _std_print_out
   dta b(19),c'Computing primes...'
   dta b(128)
   dta b(0)
;### esieve.atl(18) for k 
   ldy #0
   sty k-0+1
_lbl5:
;### esieve.atl(19) 	sieve(k) = 1
   lda #<sieve
   sta _arr-0
   lda #>sieve
   clc
   adc k+1
   sta _arr-0+1
   lda #1
   sta (_arr),y
   iny
   jne _lbl17
   inc k+1
_lbl17:
   cpy #0
   jne _lbl5
   lda k-0+1
   cmp #32
   jne _lbl5
;### esieve.atl(21) RTCLOCK = 0
   lda #0
   sta RTCLOCK
;### esieve.atl(23) for i:2..SQRT_COUNT where sieve(i) = 1
   lda #2
   sta _s2__i
_lbl10:
   lda _s2__i
   ;
   tax
   lda sieve-0,x
   cmp #0
   jeq _lbl8
;### esieve.atl(24) 		j = i * 2
   lda _s2__i
   asl
   sta j-0
   lda #0
   rol
   sta j-0+1
;### esieve.atl(28) 			j = j + i
   jmp _lbl7
_lbl9:
;### esieve.atl(26) 			sieve(j) = 0
   lda #<sieve
   sta _arr-0
   lda #>sieve
   clc
   adc j+1
   sta _arr-0+1
   ldy j-0+0
   lda #0
   sta (_arr),y
;### esieve.atl(27) 			j = j + i
   lda j-0
   clc
   adc _s2__i
   sta j-0
   jcc _lbl7
   inc j+1
_lbl7:
;### esieve.atl(25) 		while j<=8191
   lda j-0+1
   cmp #31
   jeq _lbl19
   jcs _lbl8
_lbl19:
   lda j-0
   cmp #255
   jeq _lbl9
   jcc _lbl9
_lbl8:
   inc _s2__i
   lda _s2__i
   cmp #92
   jne _lbl10
;### esieve.atl(29) time = RTCLOCK
   lda RTCLOCK
   sta time
;### esieve.atl(31) "Time used: [time] ticks"
   jsr _std_print_out
   dta b(11),c'Time used: '
   dta b(129),a(time)
   dta b(6),c' ticks'
   dta b(128)
   dta b(0)
;### esieve.atl(32) "Press Q to quit, any other key for list"
   jsr _std_print_out
   dta b(39),c'Press Q to quit, any other key for list'
   dta b(128)
   dta b(0)
;### esieve.atl(34) CH = none
   lda #key__none
   sta CH
;### esieve.atl(37) for k where sieve(k) = 1 until CH = Q
_lbl13:
;### esieve.atl(35) until CH <> none
   lda CH
   cmp #key__none
   jeq _lbl13
_lbl12:
   lda #0
   sta k-0
   sta k-0+1
;### esieve.atl(40) CH = none
   jmp _lbl14
_lbl16:
   lda #<sieve
   sta _arr-0
   lda #>sieve
   clc
   adc k+1
   sta _arr-0+1
   ldy k-0+0
   lda (_arr),y
   cmp #0
   jeq _lbl14
;### esieve.atl(38) 	 "[k]"
   jsr _std_print_out
   dta b(130),a(k)
   dta b(128)
   dta b(0)
_lbl14:
   lda CH
   cmp #key__Q
   jeq _lbl15
   inc k+0
   jne _lbl20
   inc k+1
_lbl20:
   lda k-0
   cmp #0
   jne _lbl16
   lda k-0+1
   cmp #32
   jne _lbl16
_lbl15:
   lda #key__none
   sta CH
_lbl21:
   jmp _lbl21
   icl 'C:\nes\atalan\codebase\atalan\platform\atari\atari.asm'
   icl 'C:\nes\atalan\codebase\atalan\processor\m6502\m6502.asm'

sieve:
   .ds 8192
