_stdbuf equ 1536
_arr equ 128
_putchr_proc_adr equ 1619
_TEMPW2 equ 52
_std_print_out___5 equ _stdbuf
_std_print_out___6 equ _putchr_proc_adr
rtclock1 equ 20
rtclock2 equ 19
count equ 130
_s1__i equ 132
_s2__i equ 134
_s2___21 equ 135
_s2___22 equ 136
_s3__k equ 137
_s3___25 equ 139
_s3___27 equ 141
_s3___28 equ 143
_s4__k equ 144
_s4___30 equ 146
t equ 148
_35 equ 150
_36 equ 151
_37 equ 152
   org $2e0
   dta a($2000)
   org $2000
;### bit_sieve.atl(24) rtclock1 = 0
   ldy #0
   sty rtclock1
;### bit_sieve.atl(25) rtclock2 = 0
   sty rtclock2
;### bit_sieve.atl(27) for i:0..bmax flags(i)=$aa
   sty _s1__i-0+1
_lbl5:
   lda #<flags
   sta _arr-0
   lda #>flags
   clc
   adc _s1__i+1
   sta _arr-0+1
   lda #170
   sta (_arr),y
   iny
   jne _lbl12
   inc _s1__i+1
_lbl12:
   cpy #0
   jne _lbl5
   lda _s1__i-0+1
   cmp #32
   jne _lbl5
;### bit_sieve.atl(29) for i:3..sqrt max_prime step 2 where (flags(i/8) bitand mask(i mod 8) <> 0)
   lda #3
   sta _s2__i
_lbl9:
   lda _s2__i
   lsr
   lsr
   lsr
   sta _s2___21
   lda _s2__i
   and #7
   sta _s2___22
   lda _s2___21
   ;
   tax
   lda flags-0,x
   sta _35
   ldx _s2___22
   lda mask-0,x
   and _35
   cmp #0
   jeq _lbl6
;### bit_sieve.atl(30) 	for k:i*i..max_prime step 2*i
   lda _s2__i
   ldx _s2__i
   jsr _sys_mul8
   lda _s2__i
   asl
   sta _s3___25-0
   lda #0
   rol
   sta _s3___25-0+1
   lda _TEMPW2-0
   sta _s3__k-0
   lda _TEMPW2-0+1
   sta _s3__k-0+1
_lbl8:
;### bit_sieve.atl(31) 		flags(k/8) = flags(k/8) bitand maskx(k mod 8)
   lda _s3__k-0
   sta _s3___27-0
   lda _s3__k-0+1
   lsr
   ror _s3___27-0+0
   lsr
   ror _s3___27-0+0
   lsr
   ror _s3___27-0+0
   sta _s3___27-0+1
   lda _s3__k-0
   and #7
   sta _s3___28
   lda #<flags
   sta _arr-0
   lda #>flags
   clc
   adc _s3___27+1
   sta _arr-0+1
   ldy _s3___27-0+0
   lda (_arr),y
   sta _36
   lda _s3___28
   ;
   tax
   lda _36
   and maskx,x
   clc
   sta (_arr),y
   lda _s3__k-0
   clc
   adc _s3___25+0
   sta _s3__k-0
   lda _s3__k-0+1
   adc _s3___25+1
   sta _s3__k-0+1
   jcc _lbl8
_lbl6:
   lda _s2__i
   clc
   adc #2
   sta _s2__i
   cmp #1
   jne _lbl9
;### bit_sieve.atl(33) count = 1
   lda #1
   sta count-0
   lda #0
   sta count-0+1
;### bit_sieve.atl(34) for k:3..max_prime step 2 where (flags(k/8) bitand mask(k mod 8) <> 0) 
   lda #3
   sta _s4__k-0
   lda #0
   sta _s4__k-0+1
_lbl11:
   lda _s4__k-0
   sta _s4___30-0
   lda _s4__k-0+1
   lsr
   ror _s4___30-0+0
   lsr
   ror _s4___30-0+0
   lsr
   ror _s4___30-0+0
   sta _s4___30-0+1
   lda _s4__k-0
   and #7
   tax
   lda #<flags
   sta _arr-0
   lda #>flags
   clc
   adc _s4___30+1
   sta _arr-0+1
   ldy _s4___30-0+0
   lda (_arr),y
   sta _37
   lda mask-0,x
   and _37
   cmp #0
   jeq _lbl10
;### bit_sieve.atl(35) 	inc count
   inc count+0
   jne _lbl10
   inc count+1
_lbl10:
   lda _s4__k-0
   clc
   adc #2
   sta _s4__k-0
   jcc _lbl14
   inc _s4__k+1
_lbl14:
   lda _s4__k-0
   cmp #1
   jne _lbl11
   lda _s4__k-0+1
   cmp #0
   jne _lbl11
;### bit_sieve.atl(37) t = rtclock2 * 256 + rtclock1
   lda #0
   clc
   adc rtclock1
   sta t-0
   lda rtclock2
   adc #0
   sta t-0+1
;### bit_sieve.atl(39) "[count] prime numbers in [t] ticks"
   jsr _std_print_out
   dta b(130),a(count)
   dta b(18),c' prime numbers in '
   dta b(130),a(t)
   dta b(6),c' ticks'
   dta b(128)
   dta b(0)
_lbl15:
   jmp _lbl15
mask:
;### bit_sieve.atl(19) 	mask:array(0..7) = 1,2,4,8,16,32,64,128
   dta b(1)
   dta b(2)
   dta b(4)
   dta b(8)
   dta b(16)
   dta b(32)
   dta b(64)
   dta b(128)
maskx:
;### bit_sieve.atl(20) 	maskx:array(0..7) = %1111'1110,%1111'1101,%1111'1011,%1111'0111,%1110'1111,%1101'1111,%1011'1111,%0111'1111
   dta b(254)
   dta b(253)
   dta b(251)
   dta b(247)
   dta b(239)
   dta b(223)
   dta b(191)
   dta b(127)
   rts
   icl 'C:\nes\atalan\codebase\atalan\platform\atari\atari.asm'
   icl 'C:\nes\atalan\codebase\atalan\processor\m6502\m6502.asm'

flags:
   .ds 8192
