quote:
smalSvolo4
library IEEE;
use IEEE.Std_Logic_1164.all;
entity Rs232Tx_0 is
generic(
gBaud: in Integer := 57600; -- Baud
gSystemClock: in Integer := 18000000; -- Hz
syncrst: in Integer := 0); -- Synchronous reset
port(
-- System interface
Reset_rise_N: in Std_ULogic; -- Synchronised reset
Clk: in Std_ULogic; -- System clock
-- Configuration interface
OddParity: in Std_ULogic; -- Odd parity bit
TwoStopBits: in Std_ULogic; -- Two stop bits
-- Asynchronous bit serial interface
RsOut: out Std_ULogic; -- Output data
RSOut_i_o: out Std_ULogic; -- âûõîäíûå èìïóëüñû îñíîâíîé
RSOut_i_r: out Std_ULogic; -- ðåçåðâíûé
RsBusy_N: in Std_ULogic; -- Reciever busy
RsReset: in Std_ULogic; -- Synchronous reset
-- Synchronous parallel interface
BreakReq: in Std_ULogic; -- Break request
BreakRdy: out Std_ULogic; -- Break ready
TransmitData: in Std_Logic_Vector(0 to 7); -- Data
TransmitReq: in Std_ULogic; -- Request
TransmitRdy: out Std_ULogic; -- Ready
canal: in boolean);
end entity Rs232Tx_0;
--============================== Architecture ================================--
architecture RTL of Rs232Tx_0 is
begin
----------
-- This process implements the functionality of the asynchronous transmitter.
----------
Tx232: process(Clk, Reset_rise_N)
type StateType is (Idle, Send); -- state machine
variable State: StateType;
variable Data: Std_Logic_Vector(0 to 13); -- parallel to serial
variable Parity: Std_ULogic; -- parity bit
variable BitCntr: Integer range 0 to 14; -- bit counter
subtype BaudRange is Integer range 0 to gSystemClock/gBaud;
constant BaudThreshold: BaudRange -- baud rate threshold
:= gSystemClock/gBaud;
variable BaudCntr: BaudRange; -- baud rate counter
variable BaudTick: Std_ULogic; -- baud rate tick
variable RsBusy2nd: Std_ULogic; -- synchronisation
variable RsBusy1st: Std_ULogic; -- synchronisation
variable Count0: integer range 0 to 100; -- äëèíà èìïóëüñà '0'
----------
-- This procedure is used to define all reset values for the asynchronous
-- or synchronous reset statements in this process. This is done to avoid
-- source code duplication.
----------
procedure Reset is
begin
State := Idle;
Parity := '0';
BitCntr := 0;
BaudCntr := 0;
BaudTick := '0';
RsOut <= '1';
RsOut_i_o <= '0';
RsOut_i_r <= '0';
RsBusy2nd := '0';
RsBusy1st := '0';
TransmitRdy <= '0';
BreakRdy <= '0';
Count0 := 0;
Data := (others => '1');
end Reset; ----------
begin
if syncrst=0 and Reset_rise_N = '0' then -- asynchronous reset
Reset;
elsif Rising_Edge(Clk) then -- clock edge
if RsReset='1' or -- functional reset
(syncrst/=0 and Reset_rise_N='0') then -- synchronous reset
Reset;
else -- no reset
-- transmitter
case State is
when Idle => -- reset and idle
if TransmitReq='1' and RsBusy2nd='0' then
-- transmission request
State := Send;
TransmitRdy <= '1';
BreakRdy <= '0';
RsOut <= '1';
RsOut_i_o <= '0';
RsOut_i_r <= '0';
BitCntr := 0;
-- parity calculation
Parity := not (TransmitData(0) xor
TransmitData(1) xor
TransmitData(2) xor
TransmitData(3) xor
TransmitData(4) xor
TransmitData(5) xor
TransmitData(6) xor
TransmitData(7));
-- forms the frame to be transmitted
if OddParity='1' and TwoStopBits='1' then
Data := "11" & Parity & TransmitData & '0' & "--";
BitCntr := 12;
elsif OddParity='1' and TwoStopBits='0' then
Data := '1' & Parity & TransmitData & '0' & "---";
BitCntr := 11;
elsif OddParity='0' and TwoStopBits='1' then
Data := "11" & TransmitData & '0' & "---";
BitCntr := 11;
else
Data := '1' & TransmitData & '0' & "----";
BitCntr := 10;
end if;
elsif BreakReq='1' and RsBusy2nd='0' then
-- BREAK request
State := Send;
TransmitRdy <= '0';
BreakRdy <= '1';
RsOut <= '1';
RsOut_i_o <= '0';
RsOut_i_r <= '0';
BitCntr := 14; -- extra zero bits
Data := "10000000000000";
Parity := '-';
else
State := Idle;
TransmitRdy <= '0'; -- ready to receive
BreakRdy <= '0';
RsOut <= '1';
RsOut_i_o <= '0';
RsOut_i_r <= '0';
end if;
BaudCntr := BaudThreshold;
BaudTick := '0';
Count0 := 4;
when Send => -- transferring byte
if BaudTick='1' then
if BitCntr <= 0 then
TransmitRdy <= '0'; -- byte completed
State := Idle;
else
State := Send;
BitCntr := BitCntr-1;
end if;
RSOut <= Data(BitCntr); -- bit to transmit
--RSOut_i <= Data(BitCntr);
case canal is
when true =>if Data(BitCntr)='0' then RSOut_i_o<='1'; else RSOut_i_o<='0';end if;
when false =>if Data(BitCntr)='0' then RSOut_i_r<='1'; else RSOut_i_r<='0';end if;
end case;
end if;
--TransmitRdy <= '0';
BreakRdy <= '0';
end case;
-- baud rate counter
if BaudCntr >= BaudThreshold then
BaudCntr := 0;
BaudTick := '1';
Count0 := 4;
else
BaudCntr := BaudCntr + 1;
BaudTick := '0';
if count0>0 then Count0:=Count0-1; else
case canal is
when true =>RSOut_i_o<='0';
when false =>RSOut_i_r<='0';
end case;
end if;
end if;
-- synchronisation
RsBusy2nd := RsBusy1st;
RsBusy1st := not RsBusy_N;
end if;
end if;
end process Tx232;
end architecture RTL; --======================================================--
осилишь данный язык?