source: vis_dev/vis-2.1/examples/rcnum/rcnum.v @ 11

Last change on this file since 11 was 11, checked in by cecile, 13 years ago

Add vis

File size: 1.8 KB
RevLine 
[11]1// This Verilog module describes a simple finite state machine for
2// the computation of the so-called rollercoaster numbers.
3// Given an initial number n[0] > 0, the rollercoaster number
4// sequence (n[i]) starting at n[0] is given by these rules:
5//   if n[i] is even, n[i+1] = n[i]/2;
6//   if n[i] is odd,  n[i+1] = n[i]*3+1.
7// The sequences for most (small) numbers eventually reach the
8// cycle (4,2,1).
9// This finite state machine has a state register storing n[i].
10// Since the register is finite, only a few positive integers can
11// be represented. Therefore 0 is used as trap state to indicate
12// overflow.
13//
14// This description intentionally avoids the use of '*' and '/'.
15//
16// Author: Fabio Somenzi <Fabio@Colorado.EDU>
17
18
19module rollercoasterNumbers(clock,numOut);
20    input           clock;
21    output [24:0] numOut;
22    reg [24:0]    numOut;
23
24    wire [24+2:0] tmp;
25
26    initial begin
27        numOut[0] = $ND(0,1);   
28        numOut[1] = $ND(0,1);   
29        numOut[2] = $ND(0,1);   
30        numOut[3] = $ND(0,1);   
31        numOut[4] = $ND(0,1);   
32        numOut[5] = $ND(0,1);   
33        numOut[6] = $ND(0,1);   
34        numOut[7] = $ND(0,1);   
35        numOut[8] = $ND(0,1);   
36        numOut[9] = $ND(0,1);   
37        numOut[10] = $ND(0,1); 
38        numOut[11] = $ND(0,1); 
39        numOut[12] = $ND(0,1); 
40        numOut[13] = $ND(0,1); 
41        numOut[14] = $ND(0,1); 
42        numOut[15] = $ND(0,1); 
43        numOut[16] = $ND(0,1); 
44        numOut[17] = $ND(0,1); 
45        numOut[18] = $ND(0,1); 
46        numOut[19] = $ND(0,1); 
47        numOut[20] = $ND(0,1); 
48        numOut[21] = $ND(0,1); 
49        numOut[22] = $ND(0,1); 
50        numOut[23] = $ND(0,1); 
51        numOut[24] = $ND(0,1); 
52    end
53
54    // Compute n[i] * 3 + 1.
55    assign tmp = {2'b0,numOut} + {1'b0,numOut,1'b1};
56
57    always @ (posedge clock)
58        if (numOut[0]) begin
59            // Check overflow.
60            numOut = (tmp[24+2] | tmp[24+1]) ? 0 : tmp[24:0];
61        end else begin
62            // Divide by 2.
63            numOut = {1'b0,numOut[24:1]};
64        end
65
66endmodule // rollercoasterNumbers
Note: See TracBrowser for help on using the repository browser.