library ieee;
use ieee.std_logic_1164.all;
entity hdb3bianyi is-------top程序
port(codein:in std_logic;
clk:in std_logic;
clr:in std_logic;
codeoutut std_logic_vector(1 downto 0);
fb,zb:in std_logic;
decode,v2,v3ut std_logic);
end entity hdb3bianyi;
architecture gh of hdb3bianyi is component hdb3
port(codein:in std_logic;
clk:in std_logic;
clr:in std_logic;
codeoutut std_logic_vector(1 downto 0));
end component hdb3;
component hdb3by
port(fb,zb,clk:in std_logic;
decode,v2,v3:out std_logic);
end component hdb3by;
begin
U1:HDB3
PORT MAP ( CODEIN=>CODEIN,CLK=>CLK,CLR=>CLR,CODEOUT=>CODEOUT );
U2:HDB3by
PORT MAP (fb=>fb,zb=>zb,clk=>clk,decode=>decode,v2=>v2,v3=>v3); end architecture gh;
-----------------------------编码
library ieee;
use ieee.std_logic_1164.all;
entity hdb3 is
port(codein:in std_logic;
clk:in std_logic;
clr:in std_logic;
codeout:out std_logic_vector(1 downto 0));
end hdb3;
architecture behave of hdb3 is
signal cnt0:integer:=0;
signal flag0:integer range 1 downto 0 :=0;
signal flag1:integer range 1 downto 0 :=0;
signal flag2:integer range 1 downto 0 :=1;
signal flag3:integer range 1 downto 0 :=0;
signal firstv:integer range 1 downto 0 :=0;
signal codeoutv : std_logic_vector(2 downto 0);
signal s0:std_logic_vector(4 downto 0) :="00000";
signal codeoutb:std_logic_vector(2 downto 0);
signal s1:std_logic_vector(4 downto 0) :="00000";
signal clkb:std_logic;
signal clkv:std_logic;
signal clkout:std_logic;
signal s2:std_logic_vector(4 downto 0) :="00000";
signal s3 :std_logic_vector(2 downto 0);
component dff
port(d :in std_logic;
clk :in std_logic;
q :out std_logic);
end component;
----------------------插V
begin
vclk:clkv<=clk after 10 ns;
add_v:process(clk,clr)
begin
if clk'event and clk='1'then
if clr='1'then
codeoutv<="000";
cnt0<=0;
else
case codein is
when '1'=> --始终成立
cnt0<=0;
if(flag0=0) then
codeoutv<="110";
flag0<=1;
codeoutv<="010";
flag0<=0;
end if;
when '0'=>
if cnt0=3 then
if firstv=0 then
if flag0=0 then
codeoutv<="011";
flag1<=0;
else
codeoutv<="111";
flag1<=1;
end if;
firstv<=1;
else
if flag1=0 then
codeoutv<="111";
flag1<=1;
flag0<=1;
else
codeoutv<="011";
flag1<=0;
flag0<=0;
end if;
end if;
cnt0<=0;
else
cnt0<=cnt0+1;
codeoutv<="000";
end if;
when others=> ----在其他所有的情况
codeoutv<="000";
cnt0<=cnt0;
end case;
end if;
end if;
end process add_v;
s0(0)<=codeoutv(0);
s1(0)<=codeoutv(1);
s2(0)<=codeoutv(2); ds21:dff port map(s2(0),clk,s2(1));
ds11:dff port map(s1(0),clk,s1(1));
ds01:dff port map(s0(0),clk,s0(1));
ds22:dff port map(s2(1),clk,s2(2));
ds12:dff port map(s1(1),clk,s1(2));
ds02:dff port map(s0(1),clk,s0(2));
ds23:dff port map(s2(2),clk,s2(3));
ds13:dff port map(s1(2),clk,s1(3));
ds03:dff port map(s0(2),clk,s0(3));
---------插B
bclk:clkb<=not clk;
add_b:process(clkb)
begin
if clkb'event and clkb='1'then
case codeoutv is
when "110"=>
flag3<=1;
s2(4)<=s2(3);
s1(4)<=s1(3);
s0(4)<=s0(3);
when "010"=>
flag3<=0;
s2(4)<=s2(3);
s1(4)<=s1(3);
s0(4)<=s0(3);
when "111"=>
if flag3<=0 then
s2(4)<='1';
s1(4)<='0';
s0(4)<='1';
flag3<=1;
else
s2(4)<=s2(3);
s1(4)<=s1(3);
s0(4)<=s0(3);
end if;
flag2<=1;
when "011"=>
if flag3<=0 then
s2(4)<=s2(3);
s1(4)<=s1(3);
s0(4)<=s0(3);
else
s2(4)<='0';
s1(4)<='0';
s0(4)<='1';
flag3<=0;
end if;
flag2<=0;
when others=>
s2(4)<=s2(3);
s1(4)<=s1(3);
s0(4)<=s0(3);
end case;
codeoutb<=s2(4)&s1(4)&s0(4);
end if;
end process add_b;
-------------------------输出
outclk:clkout<=clk after 5 ns;
output: process(clkout)
begin
if clkout'event and clkout='1'then
if codeoutb="000"then
codeout<="00";
elsif codeoutb="001"or codeoutb="010" or codeoutb="011"then
codeout<="01";
else
codeout<="10";
end if;
end if;
end process output;
end behave;
--------------------译码
library ieee;
use ieee.std_logic_1164.all;
entity hdb3by is
port(fb,zb,clk:in std_logic;
decode,v2,v3:out std_logic);
end entity hdb3by;
architecture hh of hdb3by is component kvb
port(clk:in std_logic;
v,datain:in std_logic;
decode:out std_logic);
end component kvb; component or1
port(a,b:in std_logic;c:out std_logic);
end component or1; component fv
port(fb,zb:in std_logic;
fvout:out std_logic);
end component fv; component zv
port(fb,zb:in std_logic;
zvout:out std_logic);
end component zv; component v1
port(a,b:in std_logic;
v2,v3:out std_logic);
end component v1; signal m,x,y,z:std_logic;
begin
t1:zv port map(fb=>fb,zb=>zb,zvout=>x);
t2:fv port map(fb=>fb,zb=>zb,fvout=>y);
t3:or1 port map(a=>y,b=>x,c=>z);
t4:or1 port map(a=>fb,b=>zb,c=>m);
t5:kvb port map(clk=>clk,v=>z,datain=>m,decode=>decode);
t6:v1 port map(a=>x,b=>y,v2=>v2,v3=>v3);
end architecture hh; library ieee;
use ieee.std_logic_1164.all;
entity v1 is
port(a,b:in std_logic;
v2,v3:out std_logic);
end v1;
architecture one of v1 is
begin
v2<=a;
v3<=b;
end one;
---------+v模块
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity zv is
port(fb,zb:in std_logic;
zvout:out std_logic);
end zv;
architecture bh of zv is
signal M:std_logic_vector(2 downto 0);
begin
process(zb,fb)
begin
if fb='1'then M<="000";
elsif zb'event and zb='1'then
if M<2 then
M<=M+1;
end if;
end if;
end process; process(FB,M)
begin
if FB='0'then
if M<2 then
zvout<='0' ;
else
zvout<=zb;
end if;
else
zvout<='0';
end if;
end process;
end bh;
-------- -V模块
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity fv is
port(fb,zb:in std_logic;
fvout:out std_logic);
end fv;
architecture hh of fv is
signal N:std_logic_vector(2 downto 0);
begin
process(zb,fb)
begin
if zb='1'then N<="000";
elsif fb'event and fb='1'then
if N<2 then
N<=N+1;
end if ;
end if;
end process; process(zb,N)
begin
if zb='0'then
if N<2 then
fvout<='0';
else
fvout<=fb;
end if;
else
fvout<='0';
end if;
end process;
end hh;
----------加法器 library ieee;
use ieee.std_logic_1164.all;
entity or1 is
port(a,b:in std_logic;c:out std_logic);
end entity or1;
architecture one of or1 is
begin
c<=a or b;
end architecture one; -----------扣V扣B
library ieee;
use ieee.std_logic_1164.all;
entity kvb is
port(clk:in std_logic;
v,datain:in std_logic;
decode:out std_logic);
end kvb; architecture behav of kvb is
signal A0,A1,A2,A3:std_logic;
begin
process(clk,v)
begin
if clk'event and clk='1'then
if(v='1')then
A0<='0';
A1<='0';
A2<='0';
A3<='0';
decode<=A0;
elsif(v='0')then
A3<=datain;
A2<=A3;
A1<=A2;
A0<=A1;
decode<=A0;
end if;
end if;
end process;
end behav; |