OpenCores
URL https://opencores.org/ocsvn/hive/hive/trunk

Subversion Repositories hive

[/] [hive/] [trunk/] [v04.05/] [functions.h] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 ericw
/*
2
--------------------------------------------------------------------------------
3
--
4
-- Module : functions.h
5
--
6
--------------------------------------------------------------------------------
7
--
8
-- Function:
9
-- - A bunch of functions for a stack processor.
10
--
11
-- Instantiates:
12
-- - Nothing.
13
--
14
--------------------------------------------------------------------------------
15
*/
16
 
17
 
18
        /*
19
        ---------------
20
        -- functions --
21
        ---------------
22
        */
23
 
24
 
25
        // returns safe ceiling value of log2(n)
26
        // (for vector sizing we never want less than 1)
27
        // examples: 
28
        // clog2(0 thru 2)  = 1,
29
        // clog2(3 & 4)     = 2,
30
        // clog2(5 thru 8)  = 3,
31
        // clog2(9 thru 16) = 4, etc.
32
        function integer clog2;
33
        input integer in;
34
                begin
35
                        clog2 = 1;
36
                        while ( in > 2 ) begin
37
                                in = ( in + 1 ) / 2;
38
                                clog2 = clog2 + 1;
39
                        end
40
                end
41
        endfunction
42
 
43
 
44
        // flip 32 bit value
45
        function [31:0] flip_32;
46
        input [31:0] in;
47
        integer i;
48
                begin
49
                        for ( i=0; i<32; i=i+1 ) begin
50
                                flip_32[i] = in[31-i];
51
                        end
52
                end
53
        endfunction
54
 
55
 
56
        // return leading zero count of 32 bit value
57
        // examples:
58
        // lzc_32(32'b000...000) = 32
59
        // lzc_32(32'b000...001) = 31
60
        // lzc_32(32'b000...01x) = 30
61
        // ...
62
        // lzc_32(32'b001...xxx) = 2
63
        // lzc_32(32'b01x...xxx) = 1
64
        // lzc_32(32'b1xx...xxx) = 0
65
        function [5:0] lzc_32;
66
        input [31:0] in;
67
        integer j, hi_1, all_0;
68
                begin
69
                        hi_1 = 31;
70
                        all_0 = 1;
71
                        // priority encoder (find MSB 1 position)
72
                        for ( j = 0; j < 32; j = j + 1 ) begin
73
                                if ( in[j] ) begin
74
                                        hi_1 = j;
75
                                        all_0 = 0;
76
                                end
77
                        end
78
                end
79
                // invert & concat to get zero count
80
                lzc_32 = { all_0[0], ~hi_1[4:0] };
81
        endfunction
82
 
83
 
84
        // returns the max of 2 integers
85
        function integer max_of_2;
86
        input integer a, b;
87
                begin
88
                        if ( a > b ) begin
89
                                max_of_2 = a;
90
                        end else begin
91
                                max_of_2 = b;
92
                        end
93
                end
94
        endfunction
95
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.