*---------------------------------------------------------------------
*
* Sieve of Eratosthenes benchmark test published in the September 1981
*   issue of BYTE magazine. Below is source for the test in C. 
*
*   This file contains a 6800 assembly language version of this same
*   Sieve test. The 6800 code is written as if an average C compiler
*   generated the code.
*
*   This test runs in 28.25 seconds on a SWTPC 6800, 1 MHz
*
*   Mike Douglas, Jan 2023
*
*---------------------------------------------------------------------
*
* Byte Sieve in C
*
* #define true 1
* #define false 0
* #define size 8190
* #define sizepl 8191
* char flags[sizepl];
*
* main() {
*     int i, prime, k, count, iter;
*     printf("10 iterations\n");
*     for (iter = 0; iter < 10; iter ++) {
*         count=0;
*         for (i = 0; i < sizep1; i++)
*             flags[i] = true;
*         for (i = 0; i < sizep1; i++) {
*             if (flags[i]) {
*                 prime = i + i + 3;
*                 k = i + prime;
*                 while (k < sizep1) {
*                     flags[k] = false;
*                     k += prime;
*                 }
*                 count = count + 1;
*             }
*         }
*     }
*     printf("\n%d primes", count);
* }
*
*-----------------------------------------------------------------------

PDATA1	equ	$E07E			SWTBUG print string
OUT4HS	equ	$E0C8			SWTBUG 16 bit hex display
CONTRL	equ	$E0E3			SWTBUG re-entry point

SIZE	equ	8190+1
ITERS	equ	10

	org	$100

* printf("10 iterations\n");

	ldx	#mIters
	jsr	PDATA1

* for (iter = 0; iter < 10; iter ++) {

	ldx	#0
	stx	iter

loop1	ldab	iter+1
	ldaa	iter
	subb	#ITERS and $FF
	sbca	#ITERS/256
	bcs	fwd1
	jmp	lp1Exit

* count=0;

fwd1	ldx	#0
	stx	count
	
* for (i = 0; i < size; i++)

	ldx	#0
	stx	i

loop2	ldab	i+1
	ldaa	i
	subb	#SIZE and $FF
	sbca	#SIZE/256
	bcc	lp2Exit

* flags[i] = true;

	ldab	#flags and $FF
	ldaa	#flags/256
	addb	i+1
	adca	i
	stab	accum16+1
	staa	accum16
	ldx	accum16
	ldaa	#1
	staa	0,x

lp2Inc	ldx	i
	inx
	stx	i
	bra	loop2

* for (i = 0; i < size; i++) {

lp2Exit	ldx	#0
	stx	i

loop3	ldab	i+1
	ldaa	i
	subb	#SIZE and $FF
	sbca	#SIZE/256
	bcs	fwd2
	jmp	lp3Exit

* if (flags[i]) {

fwd2	ldab	#flags and $FF
	ldaa	#flags/256
	addb	i+1
	adca	i
	stab	accum16+1
	staa	accum16
	ldx	accum16
	ldaa	0,x
	bne	fwd3
	jmp	lp3Inc

* prime = i + i + 3;

fwd3	ldab	i+1
	ldaa	i
	addb	i+1
	adca	i
	addb	#3
	adca	#0
	stab	prime+1
	staa	prime
	
* k = i + prime;

	addb	i+1
	adca	i
	stab	k+1
	staa	k

* while (k < size) {

loop4	ldab	k+1
	ldaa	k
	subb	#SIZE and $FF
	sbca	#SIZE/256
	bcc	lp4Exit

* flags[k] = false;

	ldab	#flags and $FF
	ldaa	#flags/256
	addb	k+1
	adca	k
	stab	accum16+1
	staa	accum16
	ldx	accum16
	clr	0,x

* k += prime;

	ldab	k+1
	ldaa	k
	addb	prime+1
	adca	prime
	stab	k+1
	staa	k
	bra	loop4

* count = count + 1;

lp4Exit	ldx	count
	inx
	stx	count

* closing brace of 2nd "for (i = 0; i < sizep1; i++)" loop

lp3Inc	ldx	i
	inx
	stx	i
	jmp	loop3

lp3Exit	equ	*

* closing brace of "for (iter = 0; iter < 10; iter ++)" loop

lp1Inc	ldx	iter
	inx
	stx	iter
	jmp	loop1

* printf("\n%d primes", count);

lp1Exit	ldx	#mPrimes
	jsr	PDATA1
	ldx	#count
	jsr	OUT4HS
	jmp	CONTRL

*-----------------------------------------------------------
* Data area
*-----------------------------------------------------------
mIters	fcb	13,10
	fcc	"10 Iterations"
	fcb	13,10,10,4

mPrimes	fcc	"Primes found (in hex): "
	fcb	4

accum16	rmb	2
i	rmb	2
prime	rmb	2
k	rmb	2
count	rmb	2
iter	rmb	2
flags	equ	*

	end
