-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbyte_sieve_6809.core_size.asm
207 lines (177 loc) · 6.26 KB
/
byte_sieve_6809.core_size.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
; [TURBO9_HEADER_START]
; ////////////////////////////////////////////////////////////////////////////
; Turbo9 Microprocessor IP
; ////////////////////////////////////////////////////////////////////////////
; Website: www.turbo9.org
; Contact: team[at]turbo9[dot]org
; ////////////////////////////////////////////////////////////////////////////
; [TURBO9_LICENSE_START]
; BSD-1-Clause
;
; Copyright (c) 2020-2023
; Kevin Phillipson
; Michael Rywalt
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;
; 1. Redistributions of source code must retain the above copyright notice,
; this list of conditions and the following disclaimer.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.
; [TURBO9_LICENSE_END]
; ////////////////////////////////////////////////////////////////////////////
; Engineer: Kevin Phillipson
; Description: Only the core algorithm for size
; BYTE Sieve Benchmark (Language: 6809 ASM)
; Originally published in BYTE magazine September 1981
; 6809 Assembly port by Kevin Phillipson
; (Only the core algorithm for size)
;
; ////////////////////////////////////////////////////////////////////////////
; History:
; 07.14.2023 - Kevin Phillipson
; File header added
;
; ////////////////////////////////////////////////////////////////////////////
; [TURBO9_HEADER_END]
; ////////////////////////////////////////////////////////////////////////////
; Turbo9 / 6809 BYTE sieve benchmark program
; ////////////////////////////////////////////////////////////////////////////
IFDEF _COCO
prog_start equ $4000
clk_cnt equ $0112
ELSE
prog_start equ $8000
clk_cnt equ $0004
output_port equ $0000
ENDC
data_size equ 8190
iter equ 10
LF equ $0a
org prog_start ; 32k of memory at the top
IFDEF _COCO
section code
ENDC
; ////////////////////////////////////////////////////////////////////////////
; Code Under Test
; ////////////////////////////////////////////////////////////////////////////
code_under_test:
leas -1,s
; ////////////////////////////////////////////////////////////////////////////
; BYTE sieve function
; ////////////////////////////////////////////////////////////////////////////
; #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 = 1; iter <= 10; iter ++) { // interation_loop
; count=0 ;
; for (i = 0; i <= size; i++) // set_flags_loop
; flags[i] = true; // set_flags_loop_done
; for (i = 0; i <= size; i++) { // find_primes_loop
; if (flags[i]) {
; prime = i + i + 3;
; k = i + prime;
; while (k <= size) { // clr_flags_loop
; flags[k] = false;
; k += prime;
; } // clr_flags_loop_done
; count = count + 1;
; }
; } // find_primes_loop_done
; } // interation_loop_done
; printf("\n%d primes", count);
; }
; Variables:
;
; i: generic loop counter (16bit) (X used)
; prime: 16bit (D used)
; k: 16bit (Y used)
; count: 16bit (U used)
; iter: 8bit (0,s)
lda #iter
sta ,s
interation_loop
ldx #((data_size+1)&$0007)
ldu #flags
ldd #$0101
;
set_flags_loop_1
sta ,u+
leax -1,x
bne set_flags_loop_1
;
ldx #((data_size+1)&$FFF8)
beq set_flags_done
tfr d,y ; y = $0101
tfr d,x ; x = $0101
tfr a,dp ; dp = $01
ldu #(flags+data_size+1)
pshs cc
set_flags_loop_8
tfr a,cc ; cc = $01
pshu y,x,dp,b,a,cc
cmpu #(flags+((data_size+1)&$0007))
bne set_flags_loop_8 ; Could unroll this loop...
puls cc
set_flags_done
ldu #0 ; U = count = 0
ldd #3 ; D = i + i + 3 (prime)
ldx #flags ; X = i + flags
find_primes_loop
tst ,x+ ; need to account for this +1
bne if_flag_set ; flag[i] = 1?
addd #2
cmpx #(flags+data_size)
bls find_primes_loop ; Could unroll this loop...
find_primes_loop_done_0
dec ,s
bne interation_loop
interation_loop_done_0
bra main_done
if_flag_set
leay d,x ; Y = k = i + prime + flags +1
cmpy #(data_size+flags+1) ; while (k <= size) (+1 correction for tst ,x+)
bhi clr_flags_loop_done
clr_flags_loop
clr -1,y ; flags[k] = false; (-1 correction for tst ,x+)
leay d,y ; X = k += prime
cmpy #(data_size+flags+1) ; while (k <= size) (+1 correction for tst ,x+)
bls clr_flags_loop ; Could unroll this loop...
clr_flags_loop_done
leau 1,u
addd #2
cmpx #(flags+data_size)
bls find_primes_loop
find_primes_loop_done_1
dec ,s
bne interation_loop
interation_loop_done_1
; ////////////////////////////////////////////////////////////////////////////
main_done:
bra main_done
; ////////////////////////////////////////////////////////////////////////////
; Data Block
; ////////////////////////////////////////////////////////////////////////////
data_addr:
flags: .blkb (data_size+1)
data_end:
IFDEF _COCO
endsect
ENDC