数电代码总结
文章目录
decoder3-8
library ieee;
use ieee.std_logic_1164.all;
entity decoder38 is
port(
A2,A1,A0 : in std_logic;
yout : out std_logic_vector(7 downto 0)
);
end entity;
architecture decoder of decoder38 is
signal cinA : std_logic_vector(2 downto 0);
begin
cinA <= A2&A1&A0 ;
yout <="01111111" when cinA ="000" else
"10111111" when cinA ="001" else
"11011111" when cinA ="010" else
"11101111" when cinA ="011" else
"11110111" when cinA ="100" else
"11111011" when cinA ="101" else
"11111101" when cinA ="110" else
"11111110" when cinA ="111" else
"ZZZZZZZZ";
end decoder;
mux41bus
library ieee;
use ieee.std_logic_1164.all;
entity mux41bus is
port(
A : in std_logic_vector(7 downto 0) ;
B : in std_logic_vector(7 downto 0) ;
C : in std_logic_vector(7 downto 0) ;
D : in std_logic_vector(7 downto 0) ;
SEL1 : in std_logic;
SEL0 : in std_logic;
Yout : out std_logic_vector(7 downto 0)
);
end entity;
architecture mux41 of mux41bus is
signal cins : std_logic_vector(1 downto 0);
begin
cins <= SEL1&SEL0 ;
with cins select
Yout <= A when "00",
B when "01",
C when "10",
D when "11",
"ZZZZZZZZ" when others;
end mux41;
priencoder83
-----------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity priencoder83 is
port(
Indata : in std_logic_vector(7 downto 0) ;
Yout : out std_logic_vector(2 downto 0)
);
end entity;
-----------------------------------------------------
architecture rtl of priencoder83 is
begin
Yout <= "000" when Indata(0) = '0' else
"001" when Indata(1) = '0' else
"010" when Indata(2) = '0' else
"011" when Indata(3) = '0' else
"100" when Indata(4) = '0' else
"101" when Indata(5) = '0' else
"110" when Indata(6) = '0' else
"111" when Indata(7) = '0' else
"ZZZ";
end rtl;
-------------------------------------------------------
halfadd
-----------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity halfadd is
port(
a,b : in std_logic ;
s,c : out std_logic
);
end entity;
-----------------------------------------------------
architecture halfadd01 of halfadd is
begin
s <= a xor b;
c <= a and b;
end halfadd01;
-------------------------------------------------------
fulladd01
-----------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity fulladd01 is
port(
A,B : in std_logic ;
cin : in std_logic ;
sum,co : out std_logic
);
end entity;
-----------------------------------------------------
architecture rtl of fulladd01 is
component halfadd is
port (
a,b : in std_logic ;
s,c : out std_logic
);
end component halfadd;
signal temp_c: std_logic_vector(1 downto 0);
signal temp_s: std_logic;
begin
u0: halfadd port map(a=>A,b=>B,s=>temp_s,c=>temp_c(0));
u1: halfadd port map(a=>cin,b=>temp_s,s=>sum,c=>temp_c(1));
co<=temp_c(0) or temp_c(1);
end rtl;
-------------------------------------------------------
fulladd02
-----------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity fulladd02 is
port(
A,B : in std_logic ;
cin : in std_logic ;
sum,co : out std_logic
);
end entity;
-----------------------------------------------------
architecture rtl of fulladd02 is
begin
sum <= A xor B xor cin;
co <=( A and B ) or ((A xor B) and cin);
end rtl;
-------------------------------------------------------
add4bit_vhd
-----------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity add4bit_vhd is
port(
CINA : in std_logic_vector(3 downto 0);
CINB : in std_logic_vector(3 downto 0);
COUTS : out std_logic_vector(3 downto 0)
);
end entity;
-----------------------------------------------------
architecture rtl of add4bit_vhd is
component fulladd01 is
port(
A,B : in std_logic ;
cin : in std_logic ;
sum,co : out std_logic
);
end component fulladd01;
signal temp_co:std_logic_vector(3 downto 0);
begin
u0:fulladd01 port map (A=>CINA(0),B=>CINB(0),cin=>'0', sum=>COUTS(0),co=>temp_co(0));
u1:fulladd01 port map (A=>CINA(1),B=>CINB(1),cin=>temp_co(0),sum=>COUTS(1),co=>temp_co(1));
u2:fulladd01 port map (A=>CINA(2),B=>CINB(2),cin=>temp_co(1),sum=>COUTS(2),co=>temp_co(2));
u3:fulladd01 port map (A=>CINA(3),B=>CINB(3),cin=>temp_co(2),sum=>COUTS(3),co=>temp_co(3));
end rtl;
-------------------------------------------------------
decoder47
library ieee;
use ieee.std_logic_1164.all;
----------------------------------
entity decoder47 is
port (bcd: in std_logic_vector(3 downto 0);
led: out std_logic_vector(6 downto 0));
end;
----------------------------------
architecture bhv of decoder47 is
begin
process(bcd)
begin
case bcd is
when"0000"=>led<="0111111";
when"0001"=>led<="0000110";
when"0010"=>led<="1011011";
when"0011"=>led<="1001111";
when"0100"=>led<="1100110";
when"0101"=>led<="1101101";
when"0110"=>led<="1111101";
when"0111"=>led<="0000111";
when"1000"=>led<="1111111";
when"1001"=>led<="1100111";
when others=>led<="0000000";
end case;
end process;
end bhv;
NbitShift
N 位串行移位寄存器
library ieee; -- 注意:library
use ieee.std_logic_1164.all; --注意:_1164.all
entity NbitShift005 is
generic (N:integer := 4);
port(
clk : in std_logic ;
Din : in std_logic ;
Qout: out std_logic
);
end NbitShift005;
architecture RTL of NbitShift005 is
signal Dreg : std_logic_vector(N-1 downto 0 ) ;
begin
process(clk)
begin --注意:这里别忘了begin
if clk'event and clk='1' then
Dreg(0)<=Din;
Qout<=Dreg(N-1);
for i in 0 to N-2 loop
Dreg(i+1)<=Dreg(i);
end loop;
end if;
end process;
end RTL;
GenSignal
生成信号
library ieee;
use ieee.std_logic_1164.all;
entity GenSignal005 is
port(
clk : in std_logic;
Yout : out std_logic
);
end GenSignal005;
architecture RTL of GenSignal005 is
signal Dreg:std_logic_vector(3 downto 0);
begin
process(clk)
begin
if clk'event and clk='1' then
dreg(0)<=(dreg(2) xor dreg(3)) or ((not dreg(0))and(not dreg(1))and(not dreg(2))and(not dreg(3)));
dreg(1)<=dreg(0);
dreg(2)<=dreg(1);
dreg(3)<=dreg(2);
end if;
Yout<=dreg(3);
end process;
end RTL;
BiShift
双向移位寄存器
library ieee;
use ieee.std_logic_1164.all;
entity BiShift005 is
port(
clk,clr,load,dir : in std_logic;
Din : in std_logic_vector(7 downto 0);
Qout : out std_logic_vector(7 downto 0)
);
end BiShift005;
architecture RTL of BiShift005 is
signal Dreg:std_logic_vector(7 downto 0);
begin
Qout<=Dreg;
process(clk,clr)
begin
if clr='0' then Dreg<="00000000"; --异步清零
elsif clk'event and clk='1' then
if load='0' then --同步装载
Dreg<=Din;
elsif dir='0' then --循环左移
for i in 0 to 6 loop
Dreg(i+1)<=Dreg(i);
end loop;
Dreg(0)<=Dreg(7);
else --循环右移
for i in 0 to 6 loop
Dreg(i)<=Dreg(i+1);
end loop;
Dreg(7)<= Dreg(0);
end if ;
end if ;
end process;
end RTL;
BiCNT6Bit
6 为二进制计数器
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity BiCNT6Bit005 is ---005 改成学号
port(
CLR005,UPDN005,CLK005,EN005 :in std_logic; ---005 改成学号
Qf,Qe,Qd,Qc,Qb,Qa :out std_logic
);
end entity;
architecture Xiaozhuge of BiCNT6Bit005 is ---005 改成学号
signal CNT : std_logic_vector(5 downto 0) ;
begin
Qf<=CNT(5);Qe<=CNT(4);Qd<=CNT(3);Qc<=CNT(2);Qb<=CNT(1);Qa<=CNT(0);
process(CLK005,CLR005)
begin
if CLR005='1' then CNT<="000000";
elsif CLK005'event and CLK005='1' then
if EN005='0' then CNT<=CNT;
else
if UPDN005='1' then CNT<=CNT+'1';
else CNT<=CNT-'1';
end if;
end if;
end if;
end process;
end Xiaozhuge;
DivFreq
分频器,CLK 是时钟输入,f1 和 f2 是分频器的输出信号。
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity DivFreq005 is ---005 改成学号
port(
clk005 :in std_logic;
f1out005,f2out005 :out std_logic
);
end entity;
architecture Xiaozhuge of DivFreq005 is
signal cnt1 :integer range 0 to 7;
signal cnt2 :integer range 0 to 127;
begin
process(clk005) --计数
begin
if clk005'event and clk005='1' then
if cnt1=4 then cnt1<=0;
else cnt1<=cnt1+1;
end if;
end if;
if clk005'event and clk005='1' then
if cnt2=123 then cnt2<=0;
else cnt2<=cnt2+1;
end if;
end if;
end process;
process(cnt1)
begin
case cnt1 is -- case语句
when 0 to 3 => f1out005<='0';
when 4 to 4=> f1out005<='1'; --这里很有意思:4-4和4 有区别
when others => f1out005<='0';
end case;
end process;
process(cnt2)
begin
case cnt2 is -- case语句
when 0 to 108 => f2out005<='0';
when 109 to 123 => f2out005<='1';
when others => f2out005<='0';
end case;
end process;
end Xiaozhuge;
BCD60
满足时钟设计的秒信号和分钟信号的计数要求,采用 BCD 表
示个位和十位,计数周期为 60,从 00 计数到 59。
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity BCD60005 is
port(
CLK,EN,RST : in std_logic;
COUT005 : out std_logic;
BCDH005,BCDL005 : out std_logic_vector(3 downto 0)
);
end entity;
architecture Xiaozhuge of BCD60005 is
signal cnth,cntl : std_logic_vector(3 downto 0); --计数
signal colh : std_logic;
begin
BCDH005<=cnth;
BCDL005<=cntl;
----------------------------------------------------------
process(clk,RST)
begin
if RST='1' then cntl<="0000"; --异步清零 colh
elsif clk'event and clk='1' then
if EN='1' then
if cntl="1001" then cntl<="0000";--计数0-9
else cntl<=cntl+'1';
end if;
end if;
end if ;
end process;
--------------------------------------------------------------
colh<='1' when cntl="1001" else '0';
----------------------------------------------------------------
process(clk,RST)
begin
if RST='1' then cnth<="0000"; --异步清零 coth
elsif clk'event and clk='1' then
if colh='1' then
if cnth="0101" then cnth<="0000";--计数0-5
else cnth<=cnth+'1';
end if;
end if;
end if ;
end process;
COUT005<='1' when cnth="0101" and cntl="1001"
else '0';
end Xiaozhuge;
BCD24
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity BCD24005 is
port(
CLK,EN,RST : in std_logic;
COUT005 : out std_logic;
BCDH005,BCDL005 : out std_logic_vector(3 downto 0)
);
end entity;
architecture Xiaozhuge of BCD24005 is
signal sh,sl : std_logic_vector(3 downto 0); --计数
signal colh : std_logic;
begin
BCDH005<=sh;
BCDL005<=sl;
process(clk,RST)
begin
if RST='1' then sh<="0000";sl<="0000";
elsif clk'event and clk='1' then
if en='1' then
if sh="0000" or sh="0001" then
if sl="1001" then sl<="0000";sh<=sh+'1';--个位计数0-9
else sl<=sl+'1';
end if;
elsif sh="0010" then
if sl="0011" then sl<="0000";sh<="0000";--个位计数0-3 注意sh清零
else sl<=sl+'1';
end if;
end if;
end if;
end if;
end process;
COUT005<='1' when sh="0010" and sl="0011"
else '0';
end Xiaozhuge;
CheckSeq005
library ieee;
use ieee.std_logic_1164.all;
entity CheckSeq005 is
port(
CLK005 : in std_logic;
Xin005 : in std_logic;
nRST005 : in std_logic;
Zout : out std_logic
);
end entity;
architecture rtl of CheckSeq005 is
-- Build an enumerated type for the state machine
type state_type is (A,B,C,D,E,F);
-- Register to hold the current state
signal state : state_type;
begin
-- Logic to advance to the next state
process (CLK005, nRST005)
begin
if nRST005 = '1' then
state <= A;
elsif (rising_edge(CLK005)) then
case state is
when A=>
if Xin005 = '1' then
state <= B;
else
state <= A;
end if;
when B=>
if Xin005 = '1' then
state <= C;
else
state <= A;
end if;
when C=>
if Xin005 = '0' then
state <= D;
else
state <= C;
end if;
when D =>
if Xin005 = '1' then
state <= E;
else
state <= A;
end if;
when E =>
if Xin005 = '0' then
state <= F;
else
state <= C;
end if;
when F =>
if Xin005 = '0' then
state <= A;
else
state <= B;---注意F再次输入1 应到状态B
end if;
end case;
end if;
end process;
-- Output depends solely on the current state
process (state)
begin
case state is
when A =>
Zout <= '0';
when B =>
Zout <= '0';
when C =>
Zout <= '0';
when D =>
Zout <= '0';
when E =>
Zout <= '0';
when F =>
Zout <= '1';
end case;
end process;
end rtl;
GenSeq005
library ieee;
use ieee.std_logic_1164.all;
entity GenSeq005 is
port(
CLK005 : in std_logic;
nRST005 : in std_logic;
Yout : out std_logic
);
end entity;
architecture rtl of GenSeq005 is
-- Build an enumerated type for the state machine
type state_type is (A,B,C,D,E,F,G,H,I,J);
-- Register to hold the current state
signal state : state_type;
begin
-- Logic to advance to the next state
process (CLK005)
begin
if (rising_edge(CLK005)) then
case state is
when A=>
if nRST005 = '1' then state <= B;
else state <= J;
end if;
when B=>
if nRST005 = '1' then state <= C;
else state <= J;
end if;
when C=>
if nRST005 = '1' then state <= D;
else state <= J;
end if;
when D=>
if nRST005 = '1' then state <= E;
else state <= J;
end if;
when E=>
if nRST005 = '1' then state <= F;
else state <= J;
end if;
when F=>
if nRST005 = '1' then state <= G;
else state <= J;
end if;
when G=>
if nRST005 = '1' then state <= H;
else state <= J;
end if;
when H=>
if nRST005 = '1' then state <= I;
else state <= J;
end if;
when I=>
if nRST005 = '1' then state <= A;
else state <= J;
end if;
when J=>
if nRST005 = '1' then state <= A;
else state <= J;
end if;
end case;
end if;
end process;
-- Output depends solely on the current state
process (state)
begin
case state is
when A => Yout <= '0';
when B => Yout <= '1';
when C => Yout <= '0';
when D => Yout <= '1';
when E => Yout <= '1';
when F => Yout <= '0';
when G => Yout <= '1';
when H => Yout <= '1';
when I => Yout <= '1';
when J => Yout <= '0';
end case;
end process;
end rtl;
LEDLight005
library ieee;
use ieee.std_logic_1164.all;
entity LEDLight005 is
port(
CLK005,SEL1005,SEL0005: in std_logic ;
LED : out std_logic_vector(7 downto 0)
);
end entity;
architecture rtl of LEDLight005 is
signal temp_led :std_logic_vector(7 downto 0);
begin
LED<=temp_led;
process(CLK005,SEL1005,SEL0005)
begin
if CLK005'event and CLK005='1' then
if SEL0005='0'and SEL1005='0' then
temp_led<="00000000";
elsif SEL0005='1'and SEL1005='1' then
temp_led<="11111111";
elsif SEL0005='0'and SEL1005='1' then
if temp_led="11111111" or temp_led="00000000" then temp_led<= not temp_led;
else temp_led<="00000000";
end if;
elsif SEL0005='1'and SEL1005='0' then
if temp_led="11111111" or temp_led="00000000" then temp_led<= "10000000";
else temp_led<= temp_led(0) & temp_led(7 downto 1);
end if;
end if;
end if;
end process;
end rtl;