]> rtime.felk.cvut.cz Git - fpga/pwm.git/blob - mcc_master.vhd
Corrected mcc_master.
[fpga/pwm.git] / mcc_master.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
5
6 library std;
7 use std.textio.all;
8
9 --------------------------------------------------------------------------------
10
11 entity mcc_master is
12   generic (
13     MCC_W     : integer := 5;
14     MUX_W     : integer := 2;
15     IRF_ADR_W : integer := 5);
16   port (
17     -- Primary slave intefrace
18     ACK_O        : out std_logic;
19     CLK_I        : in  std_logic;
20     RST_I        : in  std_logic;
21     STB_I        : in  std_logic;
22     -- Motion Control Chain
23     MCC_STB_O    : out std_logic_vector (MCC_W-1 downto 0);
24     MCC_ACK_I    : in  std_logic_vector (MCC_W-1 downto 0);
25     MCC_MUX_CODE : out std_logic_vector (MUX_W-1 downto 0);
26     MCC_MUX_EN   : out std_logic;
27     -- Shared dual-port memory
28     IRF_ACK_I    : in  std_logic;
29     IRF_ADR_O    : out std_logic_vector (IRF_ADR_W-1 downto 0);
30     IRF_DAT_I    : in  std_logic_vector (15 downto 0);
31     IRF_DAT_O    : out std_logic_vector (15 downto 0);
32     IRF_STB_O    : out std_logic;
33     IRF_WE_O     : out std_logic);
34 end entity mcc_master;
35
36 --------------------------------------------------------------------------------
37
38 architecture behavioral of mcc_master is
39
40   type state_t is (ready, read_mask, do_mcc, done);
41
42   signal state : state_t;
43
44   signal mcc_mask       : std_logic_vector (MCC_W-1 downto 0);
45   signal mcc_ack_inner  : std_logic_vector (MCC_W downto 0);
46   signal mcc_stb_inner  : std_logic_vector (MCC_W-1 downto 0);
47   signal mux_code_inner : std_logic_vector (MUX_W-1 downto 0);
48   signal mcc_exec       : std_logic;
49   
50 --------------------------------------------------------------------------------
51
52 begin
53
54   IRF_ADR_O <= conv_std_logic_vector(0, IRF_ADR_W);
55   IRF_DAT_O <= (others => '-');
56   IRF_WE_O  <= '0';
57
58
59   priority_encoder_1: entity work.priority_encoder
60     generic map (
61       SEL_W  => MCC_W,
62       CODE_W => MUX_W)
63     port map (
64       sel  => mcc_stb_inner,
65       code => mux_code_inner);
66   
67
68   MCC_EXEC_LOGIC : process (RST_I, CLK_I) is
69   begin
70     if RST_I = '1' then
71       mcc_ack_inner <= (others => '0');
72       mcc_stb_inner <= (others => '0');
73
74     elsif rising_edge(CLK_I) then
75       if mcc_exec = '0' then
76         mcc_ack_inner <= (others => '0');
77         mcc_stb_inner <= (others => '0');
78         
79       else
80         mcc_ack_inner (0) <= mcc_exec;
81         
82         for i in 0 to MCC_W-1 loop
83           if mcc_mask (i) = '1' then
84             mcc_ack_inner (i+1) <= MCC_ACK_I (i);
85             mcc_stb_inner (i)   <= mcc_ack_inner (i);
86           else
87             mcc_ack_inner (i+1) <= mcc_ack_inner (i);
88             mcc_stb_inner (i)   <= '0';
89           end if;
90         end loop;
91       end if;
92     end if;
93   end process;
94
95   
96   LATCHES : process (RST_I, CLK_I) is
97   begin
98     if RST_I = '1' then
99       MCC_STB_O    <= (others => '0');
100       MCC_MUX_CODE <= (others => '0');
101
102     elsif rising_edge(CLK_I) then
103       MCC_STB_O    <= mcc_stb_inner;
104       MCC_MUX_CODE <= mux_code_inner;
105     end if;
106   end process;
107
108   
109   FSM : process (CLK_I, RST_I) is
110   begin
111     if RST_I = '1' then
112       state      <= ready;
113       ACK_O      <= '0';
114       mcc_exec   <= '0';
115       MCC_MUX_EN <= '0';
116       IRF_STB_O  <= '0';
117       
118     elsif rising_edge(CLK_I) then
119       case state is
120         when ready =>
121           if STB_I = '1' then
122             state     <= read_mask;
123             IRF_STB_O <= '1';
124           end if;
125
126         when read_mask =>
127           if IRF_ACK_I = '1' then
128             state      <= do_mcc;
129             mcc_mask   <= IRF_DAT_I (mcc_mask'RANGE);
130             MCC_MUX_EN <= '1';
131             mcc_exec   <= '1';
132           end if;
133
134         when do_mcc =>
135           if mcc_ack_inner (MCC_W) = '1' then
136             state      <= done;
137             ACK_O      <= '1';
138             IRF_STB_O  <= '0';
139             MCC_MUX_EN <= '0';
140             mcc_exec   <= '0';
141           end if;
142
143         when done =>
144           if STB_I = '0' then
145             state <= ready;
146             ACK_O <= '0';
147           end if;
148       end case;      
149     end if;
150   end process;
151   
152 end architecture behavioral;
153