]> rtime.felk.cvut.cz Git - fpga/pwm.git/blob - pwm_min.vhd
Wave_table initialization data format modified.
[fpga/pwm.git] / pwm_min.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 --------------------------------------------------------------------------------
7
8 entity pwm_min is
9   generic (
10     IRF_ADR_W  : integer := 5;
11     PWM_W      : integer := 10;
12     BASE       : integer := 0;
13     PWMMIN_OFF : integer := 6;
14     P_BASE     : integer := 16;
15     P_SIZE     : integer := 4;
16     PWM_OFF    : integer := 1);
17   port (
18     -- Primary Slave interface
19     ACK_O     : out std_logic;
20     CLK_I     : in  std_logic;
21     RST_I     : in  std_logic;
22     STB_I     : in  std_logic;
23     -- Shared dual-port memory
24     IRF_ACK_I : in  std_logic;
25     IRF_ADR_O : out std_logic_vector (IRF_ADR_W-1 downto 0);
26     IRF_DAT_I : in  std_logic_vector (15 downto 0);
27     IRF_DAT_O : out std_logic_vector (15 downto 0);
28     IRF_STB_O : out std_logic;
29     IRF_WE_O  : out std_logic);
30 end pwm_min;
31
32 --------------------------------------------------------------------------------
33
34 architecture behavioral of pwm_min is
35
36   type state_t is (ready, phase1, phase2, phase3, done);
37   subtype irf_adr_t is std_logic_vector (IRF_ADR_W-1 downto 0);
38
39   constant PWMMIN : irf_adr_t := conv_std_logic_vector(BASE+PWMMIN_OFF, IRF_ADR_W);
40   constant PWM1   : irf_adr_t := conv_std_logic_vector(P_BASE+PWM_OFF+0*P_SIZE, IRF_ADR_W);
41   constant PWM2   : irf_adr_t := conv_std_logic_vector(P_BASE+PWM_OFF+1*P_SIZE, IRF_ADR_W);
42   constant PWM3   : irf_adr_t := conv_std_logic_vector(P_BASE+PWM_OFF+2*P_SIZE, IRF_ADR_W);
43
44   signal state : state_t := ready;
45
46   signal pwm_adr     : std_logic_vector (IRF_ADR_W-1 downto 0);
47   --signal pwm_compare : std_logic_vector (PWM_W-1 downto 0);
48   signal pwm_compare : std_logic_vector (PWM_W-1 downto 0);
49   signal pwm_less    : std_logic;
50   signal write_min   : std_logic;
51   
52   signal ack     : std_logic := '0';
53   signal irf_stb : std_logic := '0';
54   signal irf_we  : std_logic;
55
56 --------------------------------------------------------------------------------
57   
58 begin
59
60   ACK_O     <= ack and STB_I;
61
62   IRF_ADR_O <= PWMMIN when write_min = '1' else pwm_adr;
63   IRF_DAT_O <= IRF_DAT_I;
64   IRF_STB_O <= irf_stb and (not write_min or irf_we);
65   IRF_WE_O  <= irf_we;
66
67   pwm_less <= '1' when IRF_DAT_I(pwm_compare'RANGE) < pwm_compare else '0';
68   irf_we <= '1' when (pwm_less = '1' or state = phase1) and write_min = '1' else '0';
69   
70   process (CLK_I) is
71   begin
72     if rising_edge(CLK_I) then
73       if irf_we = '1' then
74         pwm_compare <= IRF_DAT_I(pwm_compare'RANGE);
75       end if;
76     end if;
77   end process;
78   
79   
80   FSM : process (CLK_I) is
81   begin
82     if rising_edge(CLK_I) then
83       if RST_I = '1' then
84         state   <= ready;
85         ack     <= '0';
86         irf_stb <= '0';
87         
88       else
89         case state is
90           when ready =>
91             if STB_I = '1' then
92               state     <= phase1;
93               pwm_adr   <= PWM1;
94               irf_stb   <= '1';
95               write_min <= '0';
96             end if;
97
98           when phase1 =>
99             write_min <= '1';
100             if write_min = '1' then
101               state     <= phase2;
102               pwm_adr   <= PWM2;
103               write_min <= '0';
104             end if;
105
106           when phase2 =>
107             write_min <= '1';
108             if write_min = '1' then
109               state     <= phase3;
110               pwm_adr   <= PWM3;
111               write_min <= '0';
112             end if;
113
114           when phase3 =>
115             write_min <= '1';
116             if write_min = '1' then
117               state     <= done;
118               ack       <= '1';
119               irf_stb   <= '0';
120               write_min <= '0';
121             end if;
122
123           when done =>
124             if STB_I = '0' then
125               state <= ready;
126               ack   <= '0';
127             end if;
128         end case;
129         
130       end if;
131     end if;
132   end process;
133
134 end behavioral;