📄 uart1.vhd
字号:
--Copyright (C) 1991-2004 Altera Corporation
--Any megafunction design, and related net list (encrypted or decrypted),
--support information, device programming or simulation file, and any other
--associated documentation or information provided by Altera or a partner
--under Altera's Megafunction Partnership Program may be used only to
--program PLD devices (but not masked PLD devices) from Altera. Any other
--use of such megafunction design, net list, support information, device
--programming or simulation file, or any other related documentation or
--information is prohibited for any other purpose, including, but not
--limited to modification, reverse engineering, de-compiling, or use with
--any other silicon devices, unless such use is explicitly licensed under
--a separate agreement with Altera or a megafunction partner. Title to
--the intellectual property, including patents, copyrights, trademarks,
--trade secrets, or maskworks, embodied in any such megafunction design,
--net list, support information, device programming or simulation file, or
--any other related documentation or information provided by Altera or a
--megafunction partner, remains with Altera, the megafunction partner, or
--their respective licensors. No other licenses, including any licenses
--needed under any third party's intellectual property, are provided herein.
--Copying or modifying any file, or portion thereof, to which this notice
--is attached violates this copyright.
library altera_vhdl_support;
use altera_vhdl_support.altera_vhdl_support_lib.all;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
library std;
use std.textio.all;
entity uart1_log_module is
port (
-- inputs:
signal clk : IN STD_LOGIC;
signal data : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
signal strobe : IN STD_LOGIC;
signal valid : IN STD_LOGIC
);
end entity uart1_log_module;
architecture europa of uart1_log_module is
file text_handle : TEXT ;
-- exemplar translate_off
-- purpose: convert 8 bit signal data to 8 bit string
FUNCTION bin_to_char(vec_to_convert : STD_LOGIC_VECTOR (7 downto 0))
RETURN CHARACTER IS
VARIABLE result: CHARACTER;
BEGIN
CASE vec_to_convert IS -- cover basic ascii printable characters...
when X"0a" => result := lf; -- \n, linefeed
when X"0d" => result := nul; -- \r, Ctrl-M
when X"09" => result := ht; -- \t, Ctrl-I, TAB
when X"20" => result := ' ' ;
when X"21" => result := '!' ;
when X"22" => result := '"' ;
when X"23" => result := '#' ;
when X"24" => result := '$' ;
when X"25" => result := '%' ;
when X"26" => result := '&' ;
when X"27" => result := ''' ; -- sync ' char for hilighting txt editors
when X"28" => result := '(' ;
when X"29" => result := ')' ;
when X"2a" => result := '*' ;
when X"2b" => result := '+' ;
when X"2c" => result := ',' ;
when X"2d" => result := '-' ;
when X"2e" => result := '.' ;
when X"2f" => result := '/' ;
when X"30" => result := '0' ;
when X"31" => result := '1' ;
when X"32" => result := '2' ;
when X"33" => result := '3' ;
when X"34" => result := '4' ;
when X"35" => result := '5' ;
when X"36" => result := '6' ;
when X"37" => result := '7' ;
when X"38" => result := '8' ;
when X"39" => result := '9' ;
when X"3a" => result := ':' ;
when X"3b" => result := ';' ;
when X"3c" => result := '<' ;
when X"3d" => result := '=' ;
when X"3e" => result := '>' ;
when X"3f" => result := '?' ;
when X"40" => result := '@' ;
when X"41" => result := 'A' ;
when X"42" => result := 'B' ;
when X"43" => result := 'C' ;
when X"44" => result := 'D' ;
when X"45" => result := 'E' ;
when X"46" => result := 'F' ;
when X"47" => result := 'G' ;
when X"48" => result := 'H' ;
when X"49" => result := 'I' ;
when X"4a" => result := 'J' ;
when X"4b" => result := 'K' ;
when X"4c" => result := 'L' ;
when X"4d" => result := 'M' ;
when X"4e" => result := 'N' ;
when X"4f" => result := 'O' ;
when X"50" => result := 'P' ;
when X"51" => result := 'Q' ;
when X"52" => result := 'R' ;
when X"53" => result := 'S' ;
when X"54" => result := 'T' ;
when X"55" => result := 'U' ;
when X"56" => result := 'V' ;
when X"57" => result := 'W' ;
when X"58" => result := 'X' ;
when X"59" => result := 'Y' ;
when X"5a" => result := 'Z' ;
when X"5b" => result := '[' ;
when X"5c" => result := '\' ;
when X"5d" => result := ']' ;
when X"5e" => result := '^' ;
when X"5f" => result := '_' ;
when X"60" => result := '`' ;
when X"61" => result := 'a' ;
when X"62" => result := 'b' ;
when X"63" => result := 'c' ;
when X"64" => result := 'd' ;
when X"65" => result := 'e' ;
when X"66" => result := 'f' ;
when X"67" => result := 'g' ;
when X"68" => result := 'h' ;
when X"69" => result := 'i' ;
when X"6a" => result := 'j' ;
when X"6b" => result := 'k' ;
when X"6c" => result := 'l' ;
when X"6d" => result := 'm' ;
when X"6e" => result := 'n' ;
when X"6f" => result := 'o' ;
when X"70" => result := 'p' ;
when X"71" => result := 'q' ;
when X"72" => result := 'r' ;
when X"73" => result := 's' ;
when X"74" => result := 't' ;
when X"75" => result := 'u' ;
when X"76" => result := 'v' ;
when X"77" => result := 'w' ;
when X"78" => result := 'x' ;
when X"79" => result := 'y' ;
when X"7a" => result := 'z' ;
when X"7b" => result := '{' ;
when X"7c" => result := '|' ;
when X"7d" => result := '}' ;
when X"7e" => result := '~' ;
when X"7f" => result := '_' ;
WHEN others =>
ASSERT False REPORT "data contains a non-printable character" SEVERITY Warning;
result := nul;
END case;
RETURN result;
end bin_to_char;
-- exemplar translate_on
begin
--exemplar translate_off
-- purpose: simulate verilog initial function to open file in write mode
-- type : combinational
-- inputs : initial
-- outputs: <none>
process is
variable initial : boolean := true; -- not initialized yet
variable status : file_open_status; -- status for fopen
begin -- process
if initial = true then
file_open (status, text_handle, "c:/pabst/test_scratch/test/full_featured/full_1c20_sim/uart1_log_module.txt", WRITE_MODE);
initial := false; -- done!
end if;
wait; -- wait forever
end process;
process (clk)
variable data_string : LINE; -- for line buffer to file
variable status : file_open_status; -- status for fopen
begin -- process clk
if clk'event and clk = '1' then -- sync ' chars for hilighting txt editors
if (valid and strobe) = '1' then
write (data_string,bin_to_char(data));
if data = X"0a" or data = X"0d" then -- \n or \r will flush line
writeline (text_handle,data_string);
file_close (text_handle); -- flush buffer
file_open (status, text_handle, "c:/pabst/test_scratch/test/full_featured/full_1c20_sim/uart1_log_module.txt", APPEND_MODE);
end if;
end if;
end if;
end process;
--exemplar translate_on
end europa;
library altera_vhdl_support;
use altera_vhdl_support.altera_vhdl_support_lib.all;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity uart1_tx is
port (
-- inputs:
signal baud_divisor : IN STD_LOGIC_VECTOR (8 DOWNTO 0);
signal begintransfer : IN STD_LOGIC;
signal clk : IN STD_LOGIC;
signal clk_en : IN STD_LOGIC;
signal do_force_break : IN STD_LOGIC;
signal reset_n : IN STD_LOGIC;
signal status_wr_strobe : IN STD_LOGIC;
signal tx_data : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
signal tx_wr_strobe : IN STD_LOGIC;
-- outputs:
signal tx_overrun : OUT STD_LOGIC;
signal tx_ready : OUT STD_LOGIC;
signal tx_shift_empty : OUT STD_LOGIC;
signal txd : OUT STD_LOGIC
);
end entity uart1_tx;
architecture europa of uart1_tx is
signal baud_clk_en : STD_LOGIC;
signal baud_rate_counter : STD_LOGIC_VECTOR (8 DOWNTO 0);
signal baud_rate_counter_is_zero : STD_LOGIC;
signal do_load_shifter : STD_LOGIC;
signal do_shift : STD_LOGIC;
signal internal_tx_ready : STD_LOGIC;
signal pre_txd : STD_LOGIC;
signal shift_done : STD_LOGIC;
signal tx_load_val : STD_LOGIC_VECTOR (9 DOWNTO 0);
signal tx_shift_reg_out : STD_LOGIC;
signal tx_shift_register_contents : STD_LOGIC_VECTOR (9 DOWNTO 0);
signal tx_wr_strobe_onset : STD_LOGIC;
signal unxshiftxtx_shift_register_contentsxtx_shift_reg_outxx5_in : STD_LOGIC_VECTOR (9 DOWNTO 0);
signal unxshiftxtx_shift_register_contentsxtx_shift_reg_outxx5_out : STD_LOGIC_VECTOR (9 DOWNTO 0);
begin
tx_wr_strobe_onset <= tx_wr_strobe AND begintransfer;
tx_load_val <= Std_Logic_Vector'(A_ToStdLogicVector(std_logic'('1')) & tx_data & A_ToStdLogicVector(std_logic'('0')));
shift_done <= NOT (or_reduce(tx_shift_register_contents));
process (clk, reset_n)
begin
if reset_n = '0' then
do_load_shifter <= std_logic'('0');
elsif clk'event and clk = '1' then
if std_logic'(clk_en) = '1' then
do_load_shifter <= (NOT internal_tx_ready) AND shift_done;
end if;
end if;
end process;
process (clk, reset_n)
begin
if reset_n = '0' then
internal_tx_ready <= std_logic'('1');
elsif clk'event and clk = '1' then
if std_logic'(clk_en) = '1' then
if std_logic'(tx_wr_strobe_onset) = '1' then
internal_tx_ready <= std_logic'('0');
elsif std_logic'(do_load_shifter) = '1' then
internal_tx_ready <= Vector_To_Std_Logic(-SIGNED(std_logic_vector'("00000000000000000000000000000001")));
end if;
end if;
end if;
end process;
process (clk, reset_n)
begin
if reset_n = '0' then
tx_overrun <= std_logic'('0');
elsif clk'event and clk = '1' then
if std_logic'(clk_en) = '1' then
if std_logic'(status_wr_strobe) = '1' then
tx_overrun <= std_logic'('0');
elsif std_logic'((NOT internal_tx_ready AND tx_wr_strobe_onset)) = '1' then
tx_overrun <= Vector_To_Std_Logic(-SIGNED(std_logic_vector'("00000000000000000000000000000001")));
end if;
end if;
end if;
end process;
process (clk, reset_n)
begin
if reset_n = '0' then
tx_shift_empty <= std_logic'('1');
elsif clk'event and clk = '1' then
if std_logic'(clk_en) = '1' then
tx_shift_empty <= internal_tx_ready AND shift_done;
end if;
end if;
end process;
process (clk, reset_n)
begin
if reset_n = '0' then
baud_rate_counter <= std_logic_vector'("000000000");
elsif clk'event and clk = '1' then
if std_logic'(clk_en) = '1' then
if std_logic'((baud_rate_counter_is_zero OR do_load_shifter)) = '1' then
baud_rate_counter <= baud_divisor;
else
baud_rate_counter <= A_EXT (((std_logic_vector'("000000000000000000000000") & (baud_rate_counter)) - std_logic_vector'("000000000000000000000000000000001")), 9);
end if;
end if;
end if;
end process;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -