forked from turbo9team/turbo9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyte_sieve_6809.asm
302 lines (252 loc) · 7.67 KB
/
byte_sieve_6809.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
; [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:
; BYTE Sieve Benchmark (Language: 6809 ASM)
; Originally published in BYTE magazine September 1981
; 6809 Assembly port by Kevin Phillipson
;
; ////////////////////////////////////////////////////////////////////////////
; 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
ldx #prog_title
jsr print_x
IFDEF _COCO
ldd #0
std clk_cnt
ELSE
lda >output_port
ora #$02
sta >output_port ; Set output_port[1] = 1, starting clk_counter
ENDC
; ////////////////////////////////////////////////////////////////////////////
; 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:
IFDEF _COCO
ELSE
; //////////////////////// Stop Clock Counter / Test Bench
lda >output_port
ora #$40
sta >output_port
ENDC
; //////////////////////// Print Primes Found
ldx #primes_found
jsr print_x
tfr u,d
jsr puthex_16bit
lda #LF
jsr putchar_a
; ////////; //////////////////////// Number of Iterations
ldx #num_iter
jsr print_x
lda #iter
jsr puthex_byte
lda #LF
jsr putchar_a
; //////////////// Print Clock Count
ldx #clk_cnt_output
jsr print_x
ldd >clk_cnt
jsr puthex_16bit
IFDEF _COCO
ldx #clk_cnt_unit
jsr print_x
ELSE
ldd >(clk_cnt+2)
jsr puthex_16bit
ENDC
lda #LF
jsr putchar_a
leas 1,s
jmp terminate_prog
; ////////////////////////////////////////////////////////////////////////////
; Data Block
; ////////////////////////////////////////////////////////////////////////////
data_addr:
flags: .blkb (data_size+1)
data_end:
clk_cnt_output
fcc "Clock count: $"
fcb $00
IFDEF _COCO
clk_cnt_unit
fcc " 16.67ms cycles"
fcb $00
ENDC
primes_found
fcc "Primes found: $"
fcb $00
prog_title:
fcb $0a
fcc "BYTE Sieve Benchmark (Language: 6809 ASM)"
fcb $0a
fcc "Originally published in BYTE magazine September 1981"
fcb $0a
fcc "6809 Assembly port by Kevin Phillipson"
fcb $0a
fcb $0a
fcb $00
num_iter:
fcc "Number of iterations: $"
fcb $00
IFDEF _COCO
include coco_sys_io.asm
ELSE
include turbo9_sys_io.asm
ENDC
include common_io.asm
IFDEF _COCO
endsect
ENDC