2024-04-01 18:52:02 +03:00
library ieee ;
use ieee.std_logic_1164. all ;
use ieee.std_logic_arith. all ;
use ieee.std_logic_unsigned. all ;
2024-03-28 18:35:51 +03:00
entity RAM9X8_PWM is
generic (
2024-06-07 10:50:55 +03:00
PWM_MODE_CONTROL_LOWER : integer : = 14 ;
PWM_MODE_CONTROL_UPPER : integer : = 15 ;
PWM_MASK_2_LOWER : integer : = 16 ;
PWM_MASK_2_UPPER : integer : = 17 ;
PWM_MASK_1_LOWER : integer : = 18 ;
PWM_MASK_1_UPPER : integer : = 19 ;
PWM_DIRECT_CONTROL_2_LOWER : integer : = 20 ;
PWM_DIRECT_CONTROL_2_UPPER : integer : = 21 ;
PWM_DIRECT_CONTROL_1_LOWER : integer : = 22 ;
PWM_DIRECT_CONTROL_1_UPPER : integer : = 23 ;
PWM_PERIOD_LOWER : integer : = 24 ;
PWM_PERIOD_UPPER : integer : = 25 ;
PWM_DIRECTION_2_LOWER : integer : = 26 ;
PWM_DIRECTION_2_UPPER : integer : = 27 ;
PWM_DIRECTION_1_LOWER : integer : = 28 ;
PWM_DIRECTION_1_UPPER : integer : = 29 ;
PWM_CHANNEL_LOWER : integer : = 30 ;
PWM_CHANNEL_UPPER : integer : = 31 ;
PWM_TIMING_LOWER : integer : = 32 ;
PWM_TIMING_UPPER : integer : = 33 ;
PWM_CMD_LOWER : integer : = 34 ;
PWM_CMD_UPPER : integer : = 35 ;
PWM_CONTROL_LOWER : integer : = 36 ;
PWM_CONTROL_UPPER : integer : = 37 ;
2024-03-28 18:35:51 +03:00
DATA_BUS_WIDTH : integer : = 8 ;
ADDRESS_BUS_WIDTH : integer : = 9
) ;
port (
clk : in std_logic ;
data : inout std_logic_vector ( DATA_BUS_WIDTH - 1 downto 0 ) ;
address : in std_logic_vector ( ADDRESS_BUS_WIDTH - 1 downto 0 ) ;
we : in std_logic ;
oe : in std_logic ;
ce : in std_logic ;
tk : out std_logic_vector ( 31 downto 0 ) : = ( others = > '1' ) ;
interrupt : out std_logic : = '1' ;
2024-04-01 18:52:02 +03:00
pwm : in std_logic_vector ( 5 downto 0 ) ;
error : in std_logic
2024-03-28 18:35:51 +03:00
) ;
end entity ;
architecture behavorial of RAM9X8_PWM is
signal modeBuf : std_logic_vector ( 15 downto 0 ) : = ( others = > '0' ) ;
signal maskBuf : std_logic_vector ( 31 downto 0 ) : = ( others = > '1' ) ;
signal directControlBuf : std_logic_vector ( 31 downto 0 ) : = ( others = > '1' ) ;
signal periodBuf : std_logic_vector ( 15 downto 0 ) : = ( others = > '0' ) ;
2024-04-01 18:52:02 +03:00
signal directionBuf : std_logic_vector ( 31 downto 0 ) : = ( others = > '0' ) ;
2024-03-28 18:35:51 +03:00
signal channelBuf : std_logic_vector ( 15 downto 0 ) : = ( others = > '0' ) ;
signal cmdBuf : std_logic_vector ( 15 downto 0 ) : = ( others = > '0' ) ;
signal controlBuf : std_logic_vector ( 15 downto 0 ) : = ( others = > '0' ) ;
2024-04-01 18:52:02 +03:00
signal tkBuf : std_logic_vector ( 31 downto 0 ) : = ( others = > '1' ) ;
type mem is array ( 31 downto 0 ) of std_logic_vector ( 15 downto 0 ) ;
2024-03-28 18:35:51 +03:00
signal memory : mem ;
2024-04-01 18:52:02 +03:00
signal memoryBuf : mem ;
2024-03-28 18:35:51 +03:00
2024-04-01 18:52:02 +03:00
signal pwm3level : std_logic_vector ( 31 downto 0 ) : = ( others = > '1' ) ;
signal upDown : std_logic : = '0' ;
signal upDownPrev : std_logic : = '0' ;
signal interruptBuf : std_logic : = '0' ;
signal period : std_logic_vector ( 15 downto 0 ) : = ( others = > '0' ) ;
signal counter : std_logic_vector ( 15 downto 0 ) : = ( others = > '0' ) ;
signal enableWriteControlBuf : std_logic : = '1' ;
2024-03-28 18:35:51 +03:00
begin
process ( we , oe , ce )
variable addr : integer range 0 to 2 * * ADDRESS_BUS_WIDTH - 1 : = 0 ;
begin
if ( ce = '0' ) then -- Если микросхема выбрана
addr : = conv_integer ( address ) ;
2024-06-07 10:50:55 +03:00
if ( addr = PWM_MODE_CONTROL_UPPER or addr = PWM_MODE_CONTROL_LOWER
or addr = PWM_MASK_2_UPPER or addr = PWM_MASK_2_LOWER or addr = PWM_MASK_1_UPPER or addr = PWM_MASK_1_LOWER
or addr = PWM_DIRECT_CONTROL_2_UPPER or addr = PWM_DIRECT_CONTROL_2_LOWER or addr = PWM_DIRECT_CONTROL_1_UPPER or addr = PWM_DIRECT_CONTROL_1_LOWER
or addr = PWM_PERIOD_UPPER or addr = PWM_PERIOD_LOWER
or addr = PWM_DIRECTION_2_UPPER or addr = PWM_DIRECTION_2_LOWER or addr = PWM_DIRECTION_1_UPPER or addr = PWM_DIRECTION_1_LOWER
or addr = PWM_CHANNEL_UPPER or addr = PWM_CHANNEL_LOWER or addr = PWM_TIMING_UPPER or addr = PWM_TIMING_LOWER
or addr = PWM_CMD_UPPER or addr = PWM_CMD_LOWER or addr = PWM_CONTROL_UPPER or addr = PWM_CONTROL_LOWER ) then
2024-03-28 18:35:51 +03:00
if ( oe = '0' and we = '1' ) then -- Если сигнал чтения активен, а записи нет
case addr is
2024-06-07 10:50:55 +03:00
when PWM_MODE_CONTROL_UPPER = >
2024-03-28 18:35:51 +03:00
data < = modeBuf ( 15 downto 8 ) ;
2024-06-07 10:50:55 +03:00
when PWM_MODE_CONTROL_LOWER = >
2024-03-28 18:35:51 +03:00
data < = modeBuf ( 7 downto 0 ) ;
2024-06-07 10:50:55 +03:00
when PWM_MASK_2_UPPER = >
2024-03-28 18:35:51 +03:00
data < = maskBuf ( 31 downto 24 ) ;
2024-06-07 10:50:55 +03:00
when PWM_MASK_2_LOWER = >
2024-03-28 18:35:51 +03:00
data < = maskBuf ( 23 downto 16 ) ;
2024-06-07 10:50:55 +03:00
when PWM_MASK_1_UPPER = >
2024-03-28 18:35:51 +03:00
data < = maskBuf ( 15 downto 8 ) ;
2024-06-07 10:50:55 +03:00
when PWM_MASK_1_LOWER = >
2024-03-28 18:35:51 +03:00
data < = maskBuf ( 7 downto 0 ) ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECT_CONTROL_2_UPPER = >
2024-03-28 18:35:51 +03:00
data < = directControlBuf ( 31 downto 24 ) ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECT_CONTROL_2_LOWER = >
2024-03-28 18:35:51 +03:00
data < = directControlBuf ( 23 downto 16 ) ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECT_CONTROL_1_UPPER = >
2024-03-28 18:35:51 +03:00
data < = directControlBuf ( 15 downto 8 ) ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECT_CONTROL_1_LOWER = >
2024-03-28 18:35:51 +03:00
data < = directControlBuf ( 7 downto 0 ) ;
2024-06-07 10:50:55 +03:00
when PWM_PERIOD_UPPER = >
2024-03-28 18:35:51 +03:00
data < = periodBuf ( 15 downto 8 ) ;
2024-06-07 10:50:55 +03:00
when PWM_PERIOD_LOWER = >
2024-03-28 18:35:51 +03:00
data < = periodBuf ( 7 downto 0 ) ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECTION_2_UPPER = >
2024-04-01 18:52:02 +03:00
data < = directionBuf ( 31 downto 24 ) ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECTION_2_LOWER = >
2024-04-01 18:52:02 +03:00
data < = directionBuf ( 23 downto 16 ) ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECTION_1_UPPER = >
2024-03-28 18:35:51 +03:00
data < = directionBuf ( 15 downto 8 ) ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECTION_1_LOWER = >
2024-03-28 18:35:51 +03:00
data < = directionBuf ( 7 downto 0 ) ;
2024-06-07 10:50:55 +03:00
when PWM_CHANNEL_UPPER = >
2024-03-28 18:35:51 +03:00
data < = channelBuf ( 15 downto 8 ) ;
2024-06-07 10:50:55 +03:00
when PWM_CHANNEL_LOWER = >
2024-03-28 18:35:51 +03:00
data < = channelBuf ( 7 downto 0 ) ;
2024-06-07 10:50:55 +03:00
when PWM_TIMING_UPPER = >
2024-04-01 18:52:02 +03:00
data < = memory ( conv_integer ( channelBuf ) ) ( 15 downto 8 ) ;
2024-06-07 10:50:55 +03:00
when PWM_TIMING_LOWER = >
2024-04-01 18:52:02 +03:00
data < = memory ( conv_integer ( channelBuf ) ) ( 7 downto 0 ) ;
2024-06-07 10:50:55 +03:00
when PWM_CMD_UPPER = >
2024-03-28 18:35:51 +03:00
data < = cmdBuf ( 15 downto 8 ) ;
2024-06-07 10:50:55 +03:00
when PWM_CMD_LOWER = >
2024-03-28 18:35:51 +03:00
data < = cmdBuf ( 7 downto 0 ) ;
2024-06-07 10:50:55 +03:00
when PWM_CONTROL_UPPER = >
2024-04-08 12:29:11 +03:00
data < = controlBuf ( 15 downto 8 ) ;
2024-06-07 10:50:55 +03:00
when PWM_CONTROL_LOWER = >
2024-04-08 12:29:11 +03:00
data < = controlBuf ( 7 downto 0 ) ;
2024-03-28 18:35:51 +03:00
when others = >
data < = ( others = > 'Z' ) ; -- Запретить запись на шину
2024-04-01 18:52:02 +03:00
end case ;
2024-06-07 10:50:55 +03:00
if addr / = PWM_CONTROL_LOWER then
2024-04-01 18:52:02 +03:00
enableWriteControlBuf < = '1' ;
else
enableWriteControlBuf < = '0' ;
end if ;
2024-03-28 18:35:51 +03:00
elsif ( oe = '1' and we = '0' ) then -- Если сигнал записи активен, а чтения нет
case addr is
2024-06-07 10:50:55 +03:00
when PWM_MODE_CONTROL_UPPER = >
2024-03-28 18:35:51 +03:00
modeBuf ( 15 downto 8 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_MODE_CONTROL_LOWER = >
2024-03-28 18:35:51 +03:00
modeBuf ( 7 downto 0 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_MASK_2_UPPER = >
2024-03-28 18:35:51 +03:00
maskBuf ( 31 downto 24 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_MASK_2_LOWER = >
2024-03-28 18:35:51 +03:00
maskBuf ( 23 downto 16 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_MASK_1_UPPER = >
2024-03-28 18:35:51 +03:00
maskBuf ( 15 downto 8 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_MASK_1_LOWER = >
2024-03-28 18:35:51 +03:00
maskBuf ( 7 downto 0 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECT_CONTROL_2_UPPER = >
2024-03-28 18:35:51 +03:00
directControlBuf ( 31 downto 24 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECT_CONTROL_2_LOWER = >
2024-03-28 18:35:51 +03:00
directControlBuf ( 23 downto 16 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECT_CONTROL_1_UPPER = >
2024-03-28 18:35:51 +03:00
directControlBuf ( 15 downto 8 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECT_CONTROL_1_LOWER = >
2024-03-28 18:35:51 +03:00
directControlBuf ( 7 downto 0 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_PERIOD_UPPER = >
2024-03-28 18:35:51 +03:00
periodBuf ( 15 downto 8 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_PERIOD_LOWER = >
2024-03-28 18:35:51 +03:00
periodBuf ( 7 downto 0 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECTION_2_UPPER = >
2024-04-01 18:52:02 +03:00
directionBuf ( 31 downto 24 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECTION_2_LOWER = >
2024-04-01 18:52:02 +03:00
directionBuf ( 23 downto 16 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECTION_1_UPPER = >
2024-03-28 18:35:51 +03:00
directionBuf ( 15 downto 8 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_DIRECTION_1_LOWER = >
2024-03-28 18:35:51 +03:00
directionBuf ( 7 downto 0 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_CHANNEL_UPPER = >
2024-03-28 18:35:51 +03:00
channelBuf ( 15 downto 8 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_CHANNEL_LOWER = >
2024-03-28 18:35:51 +03:00
channelBuf ( 7 downto 0 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_TIMING_UPPER = >
2024-04-01 18:52:02 +03:00
memory ( conv_integer ( channelBuf ) ) ( 15 downto 8 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_TIMING_LOWER = >
2024-04-01 18:52:02 +03:00
memory ( conv_integer ( channelBuf ) ) ( 7 downto 0 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_CMD_UPPER = >
2024-03-28 18:35:51 +03:00
cmdBuf ( 15 downto 8 ) < = data ;
2024-06-07 10:50:55 +03:00
when PWM_CMD_LOWER = >
2024-03-28 18:35:51 +03:00
cmdBuf ( 7 downto 0 ) < = data ;
2024-06-07 10:50:55 +03:00
-- when PWM_CONTROL_UPPER =>
2024-04-01 18:52:02 +03:00
-- controlBuf(15 downto 8) <= data;
2024-06-07 10:50:55 +03:00
-- when PWM_CONTROL_LOWER =>
2024-04-01 18:52:02 +03:00
-- controlBuf(7 downto 0) <= data;
2024-03-28 18:35:51 +03:00
when others = >
data < = ( others = > 'Z' ) ; -- Запретить запись на шину
end case ;
else
data < = ( others = > 'Z' ) ; -- Запретить запись на шину
2024-04-01 18:52:02 +03:00
end if ;
2024-03-28 18:35:51 +03:00
else
data < = ( others = > 'Z' ) ; -- Запретить запись на шину
end if ;
else
data < = ( others = > 'Z' ) ; -- Запретить запись на шину
end if ;
end process ;
process ( clk ) is
begin
2024-04-01 18:52:02 +03:00
if rising_edge ( clk ) then
if upDown = '0' then
if conv_integer ( counter ) < conv_integer ( period ) then
counter < = counter + 1 ;
else
counter < = period ;
updown < = '1' ;
end if ;
else
if conv_integer ( counter ) > 0 then
counter < = counter - 1 ;
else
counter < = ( others = > '0' ) ;
upDown < = '0' ;
end if ;
end if ;
upDownPrev < = upDown ;
end if ;
end process ;
process ( clk ) is
variable count : integer range 0 to 7 : = 0 ;
begin
if rising_edge ( clk ) then
if upDown / = upDownPrev then
count : = 0 ;
if cmdBuf ( 14 ) = '0' then
interrupt < = '1' ;
2024-03-28 18:35:51 +03:00
else
2024-04-01 18:52:02 +03:00
if updown = '0' then
interrupt < = '1' ;
interruptBuf < = '1' ;
period < = periodBuf ; -- новый период грузится сразу во время начала подъема пилы
end if ;
2024-03-28 18:35:51 +03:00
end if ;
else
2024-04-01 18:52:02 +03:00
if count < 7 then
count : = count + 1 ;
2024-03-28 18:35:51 +03:00
else
2024-04-01 18:52:02 +03:00
interrupt < = '0' ;
interruptBuf < = '0' ;
2024-03-28 18:35:51 +03:00
end if ;
2024-04-01 18:52:02 +03:00
end if ;
end if ;
end process ;
process ( interruptBuf ) is
begin
if interruptBuf = '0' then
for i in 0 to 31 loop
memoryBuf ( i ) < = memory ( i ) ; -- а вот новые значения таймингов загрузяться только после счетчика. Н о зато могут грузиться дважды за период, если выбран соответствующий тип формирования прерывания
end loop ;
end if ;
end process ;
process ( clk ) is
begin
if rising_edge ( clk ) then
for i in 0 to 11 loop
if ( conv_integer ( memoryBuf ( i ) ) > counter ) then
if ( directionBuf ( i ) = '0' ) then
pwm3level ( i ) < = '0' ;
else
pwm3level ( i ) < = '1' ;
end if ;
else
if ( directionBuf ( i ) = '0' ) then
pwm3level ( i ) < = '1' ;
else
pwm3level ( i ) < = '0' ;
end if ;
end if ;
end loop ;
2024-03-28 18:35:51 +03:00
2024-04-01 18:52:02 +03:00
for i in 12 to 15 loop
if ( conv_integer ( memoryBuf ( i ) ) > counter ) then
pwm3level ( i ) < = '0' ;
else
pwm3level ( i ) < = '1' ;
end if ;
end loop ;
for i in 16 to 27 loop
if ( conv_integer ( memoryBuf ( i ) ) > counter ) then
if ( directionBuf ( i ) = '0' ) then
pwm3level ( i ) < = '0' ;
else
pwm3level ( i ) < = '1' ;
end if ;
else
if ( directionBuf ( i ) = '0' ) then
pwm3level ( i ) < = '1' ;
else
pwm3level ( i ) < = '0' ;
end if ;
end if ;
end loop ;
2024-03-28 18:35:51 +03:00
2024-04-01 18:52:02 +03:00
for i in 28 to 31 loop
if ( conv_integer ( memoryBuf ( i ) ) > counter ) then
pwm3level ( i ) < = '0' ;
else
pwm3level ( i ) < = '1' ;
end if ;
end loop ;
end if ;
end process ;
process ( clk ) is
begin
if rising_edge ( clk ) then
case modeBuf is
when x"0000" = >
tkBuf < = pwm3level ;
when x"0001" = >
tkBuf < = ( others = > '1' ) ;
tkBuf ( 5 downto 0 ) < = pwm ;
when x"0002" = >
tkBuf < = pwm3level ;
when x"0003" = >
tkBuf < = directControlBuf ;
when others = >
tkBuf < = ( others = > '1' ) ;
end case ;
end if ;
end process ;
process ( clk ) is
begin
if rising_edge ( clk ) then
if cmdBuf ( 15 ) = '1' and error = '1' then
tk < = tkBuf or maskBuf ;
else
tk < = ( others = > '1' ) ;
end if ;
end if ;
end process ;
process ( clk ) is
begin
if rising_edge ( clk ) then
if enableWriteControlBuf = '1' then
controlBuf < = counter ;
end if ;
end if ;
end process ;
2024-03-28 18:35:51 +03:00
end behavorial ;