
 1000  *---------------------------------
 1010  * SIEVE PROGRAM:
 1020  * CALCULATES FIRST 1899 PRIMES IN 1.03 SECONDS!
 1030  *
 1040  * INSPIRED BY JIM GILBREATH
 1050  * SEE BYTE MAGAZINE, 9/81, PAGES 180-198.
 1060  *
 1070  * WRITTEN 9-3-81 BY:
 1080  * WILLIAM ROBERT SAVOIE
 1090  * 4405 DELASHMITT RD. APT 15
 1100  * HIXSON, TENN  37343
 1110  *
 1120  * EXTENSIVELY REVISED BY BOB SANDER-CEDERLOF
 1130  * TO SHAVE TIME FROM 1.39 SECONDS TO 1.03 SECONDS
 1140  *---------------------------------
 1150  * SIEVE PARAMETERS
 1160  *---------------------------------
 1170  BUFF   .EQ $3500    START OF BUFFER (#BUFF=0)
 1180  SIZE   .EQ 8192     SIZE OF FLAG ARRAY
 1190  *---------------------------------
 1200  * PAGE-ZERO VARIABLES
 1210  *---------------------------------
 1220  INDEX  .EQ $06,07   PAGE ZERO INDEX (LOCATION FOR I)
 1230  PRIME  .EQ $08,09   PRIME LOCATION
 1240  CVAR   .EQ $1B,1C   COUNT OF PRIME
 1250  ARRAY  .EQ $1D,1E   ARRAY POINTER
 1260  TIMES  .EQ $1F      COUNT LOOP
 1270  *---------------------------------
 1280  * APPLE ROM ROUTINES USED
 1290  *---------------------------------
 1300  PRINTN .EQ $F940    PRINT 2 BYTE NUMBER FROM MONITOR
 1310  HOME   .EQ $FC58    CLEAR VIDEO
 1320  CR     .EQ $FD8E    CARRIAGE RETURN
 1330  LINE   .EQ $FD9E    PRINT "-"
 1340  BELL   .EQ $FBE2    SOUND BELL WHEN DONE
 1350  *---------------------------------
 1360  * RUN PROGRAM 100 TIMES FOR ACCURATE TIME MEASUREMENTS!
 1370  *---------------------------------
 1380  START  JSR HOME     CLEAR SCREEN
 1390         LDA #100     LOOP 100 TIMES
 1400         STA TIMES    SET COUNTER
 1410  .1     JSR GO       RUN PRIME
 1420         LDA $400     TOGGLE SCREEN FO VISIBLE INDICATOR
 1430         EOR #$80     OF ACTION
 1440         STA $400
 1450         DEC TIMES
 1460         BNE .1       LOOP
 1470         JSR BELL     READ WATCH!
 1480         LDY CVAR+1   GET HI BYTE OF COUNT
 1490         LDX CVAR
 1500         JSR PRINTN   PRINT PRIMES FOUND
 1510         RTS
 1520  *---------------------------------
 1530  * RESET VARIABLES
 1540  *---------------------------------
 1550  GO     LDY #00      CLEAR INDEX
 1560         STY CVAR     CLEAR COUNT VARIABLE
 1570         STY CVAR+1   HI BYTE TOO
 1580         STY INDEX    CLEAR INDEX
 1590         STY INDEX+1  HI BYTE TOO
 1600         STY ARRAY    LOW BYTE OF ARRAY
 1610         LDA /BUFF    GET BUFFER LOCATION
 1620         STA ARRAY+1  SET ARRAY POINTER
 1630         LDA #$01     LOAD WITH ONE
 1640         LDX /SIZE      NUMBER OF PAGES TO STORE IN
 1650  *---------------------------------
 1660  * SET EACH ELEMENT IN ARRAY TO ONE,
 1670  *---------------------------------
 1680  SET    STA (ARRAY),Y  SET MEMORY
 1690         INY          NEXT LOCATION
 1700         BNE SET      GO 256 TIMES
 1710         INC ARRAY+1  MOVE ARRAY INDEX
 1720         DEX          TEST END
 1730         BNE SET      GO TELL END
 1740  *---------------------------------
 1750  * SCAN ENTIRE ARRAY, LOOKING FOR A PRIME
 1760  *---------------------------------
 1770  FORIN  LDA INDEX    GET INDEX TO ARRAY
 1780         CLC          READY ADD
 1790         STA ARRAY    SAVE LOW BYTE
 1800         LDA INDEX+1  GET HI BYTE
 1810         ADC /BUFF    ADD BUFFER LOCATION
 1820         STA ARRAY+1  SET POINTER
 1830         LDY #00      CLEAR Y REGISTER
 1840         LDA (ARRAY),Y  GET ARRAY VALUE 
 1850         BEQ .4       NOT PRIME, TRY NEXT ONE
 1860  *---------------------------------
 1870  * CALCULATE NEXT PRIME NUMBER WITH P=I+I+3
 1880  *---------------------------------
 1890         LDA INDEX    MAKE I+I IN X,Y
 1900         ASL
 1910         TAY
 1920         LDA INDEX+1
 1930         ROL
 1940         TAX
 1950         TYA          NOW ADD 3
 1960         ADC #3
 1970         STA PRIME
 1980         BCC .1
 1990         INX
 2000  .1     STX PRIME+1
 2010  *---------------------------------
 2020  * NOW CLEAR EVERY P-TH ENTRY AFTER P
 2030  *---------------------------------
 2040         LDY #0
 2050         CLC          BUMP ARRAY POINTER BY P
 2060  .2     LDA ARRAY
 2070         ADC PRIME
 2080         STA ARRAY
 2090         LDA ARRAY+1
 2100         ADC PRIME+1
 2110         STA ARRAY+1
 2120         CMP /BUFF+SIZE      SEE IF BEYOND END OF ARRAY
 2130         BCS .3       YES, FINISHED CLEARING
 2140         TYA          NO, CLEAR ENTRY IN ARRAY
 2150         STA (ARRAY),Y
 2160         BEQ .2       ...ALWAYS
 2170  *---------------------------------
 2180  * NOW COUNT PRIMES FOUND  (C=C+1)
 2190  *---------------------------------
 2200  .3
 2210  * --NOTE-- DELETE NEXT LINE TO TIME PROGRAM (JSR PRINTP)
 2220         JSR PRINTP   PRINT PRIME
 2230         INC CVAR     ADD ONE
 2240         BNE .04      GO IF NO OVERFLOW
 2250         INC CVAR+1   HI BYTE COUNTER
 2260  *---------------------------------
 2270  * INCREMENT INDEX AND TEST IF FINISHED
 2280  *---------------------------------
 2290  .4     INC INDEX
 2300         BNE .5
 2310         INC INDEX+1
 2320  .5     LDA INDEX+1
 2330         CMP /SIZE
 2340         BCC FORIN
 2350         RTS
 2360  *---------------------------------
 2370  * OPTIONAL PRINT PRIME SUBROUTINE
 2380  *---------------------------------
 2390  PRINTP LDY PRIME+1  HI BYTE 
 2400         LDX PRIME
 2410         JSR PRINTN   PRINT DECIMAL VAL
 2420         JSR LINE     VIDEO "-" OUT
 2430         RTS

