1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  prime_numgen.cpp --
23
24  Original Author: Martin Janssen, Synopsys, Inc., 2002-02-15
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38/******************************************************************************/
39/***			Prime Number Generator				    ***/
40/*** 		Implementation Filename:  prime_numgen.cc     		    ***/
41/******************************************************************************/
42
43#include "prime_numgen.h"
44
45void prime_numgen::entry()
46{
47
48  static unsigned int	p2 = 1,
49                	p3 = 0,
50			p5 = 0,
51			p7 = 0,
52			p11 = 0,
53			p13 = 0,
54			p17 = 0,
55			p19 = 0,
56			p23 = 0,
57			p29 = 0;
58
59  // HANDSHAKING
60  prime_ready.write(0);
61  wait();
62
63  // COMPUTE LOOP
64  while (true) {
65
66    // PRINT prime IF ALL OTHER VALUES ARE ZERO AND p2 IS NONZERO
67    if (p2 && !p3 && !p5 && !p7 && !p11 && !p13 && !p17 && !p19 && !p23 && !p29)
68    {
69      prime_ready.write(1);
70      prime.write(p2);		// Printing prime value p2
71      wait();
72
73      prime_ready.write(0);
74    }
75    wait();
76
77    // DETERMINE NEW PRIME NUMBER
78    if (p7 && p13) { p7--, p13--, p17++; }
79    else if (p5 && p17) { p5--, p17--, p2++, p3++, p13++; }
80    else if (p3 && p17) { p3--, p17--, p19++; }
81    else if (p2 && p19) { p2--, p19--, p23++; }
82    else if (p3 && p11) { p3--, p11--, p29++; }
83    else if (p29)       { p29--, p7++, p11++; }
84    else if (p23)       { p23--, p5++, p19++; }
85    else if (p19)       { p19--, p7++, p11++; }
86    else if (p17)       { p17 = 0; }
87    else if (p13)       { p13--, p11++; }
88    else if (p11)       { p11--, p13++; }
89    else if (p2 && p7)  { p2--, p7--, p3++, p5++; }
90    else if (p2)        { p2--, p3++, p5++; }
91    else 		{ p5++, p11++; }
92  }
93}
94