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

Subversion Repositories potato

[/] [potato/] [trunk/] [src/] [pp_utilities.vhd] - Blame information for rev 45

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 skordal
-- The Potato Processor - A simple processor for FPGAs
2
-- (c) Kristian Klomsten Skordal 2014 <kristian.skordal@wafflemail.net>
3 3 skordal
-- Report bugs and issues on <http://opencores.org/project,potato,bugtracker>
4 2 skordal
 
5
library ieee;
6
use ieee.std_logic_1164.all;
7
 
8
use work.pp_types.all;
9
use work.pp_constants.all;
10
 
11
package pp_utilities is
12
 
13
        --! Converts a boolean to an std_logic.
14
        function to_std_logic(input : in boolean) return std_logic;
15
 
16
        -- Checks if a number is 2^n:
17
        function is_pow2(input : in natural) return boolean;
18
 
19
        --! Calculates log2 with integers.
20
        function log2(input : in natural) return natural;
21
 
22 45 skordal
        -- Gets the value of the sel signals to the wishbone interconnect for the specified
23
        -- operand size and address.
24
        function wb_get_data_sel(size : in std_logic_vector(1 downto 0); address : in std_logic_vector)
25
                return std_logic_vector;
26
 
27 2 skordal
end package pp_utilities;
28
 
29
package body pp_utilities is
30
 
31
        function to_std_logic(input : in boolean) return std_logic is
32
        begin
33
                if input then
34
                        return '1';
35
                else
36
                        return '0';
37
                end if;
38
        end function to_std_logic;
39
 
40
        function is_pow2(input : in natural) return boolean is
41
                variable c : natural := 1;
42
        begin
43 45 skordal
                for i in 0 to 30 loop -- FIXME: Simulator complains about 2^31 being out of range!
44 2 skordal
                        if input = i then
45
                                return true;
46
                        end if;
47
 
48
                        c := c * 2;
49
                end loop;
50
 
51
                return false;
52
        end function is_pow2;
53
 
54
        function log2(input : in natural) return natural is
55
                variable retval : natural := 0;
56
                variable temp   : natural := input;
57
        begin
58
                while temp > 1 loop
59
                        retval := retval + 1;
60
                        temp := temp / 2;
61
                end loop;
62
 
63
                return retval;
64
        end function log2;
65
 
66 45 skordal
        function wb_get_data_sel(size : in std_logic_vector(1 downto 0); address : in std_logic_vector)
67
                return std_logic_vector is
68
        begin
69
                case size is
70
                        when b"01" =>
71
                                case address(1 downto 0) is
72
                                        when b"00" =>
73
                                                return b"0001";
74
                                        when b"01" =>
75
                                                return b"0010";
76
                                        when b"10" =>
77
                                                return b"0100";
78
                                        when b"11" =>
79
                                                return b"1000";
80
                                        when others =>
81
                                                return b"0001";
82
                                end case;
83
                        when b"10" =>
84
                                if address(1) = '0' then
85
                                        return b"0011";
86
                                else
87
                                        return b"1100";
88
                                end if;
89
                        when others =>
90
                                return b"1111";
91
                end case;
92
        end function wb_get_data_sel;
93
 
94 2 skordal
end package body pp_utilities;

powered by: WebSVN 2.1.0

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