vhdl各种实验程序代码
- 格式:doc
- 大小:91.00 KB
- 文档页数:4
LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL; ENTITY dff_1 ISPORT (rst_n:IN STD_LOGIC;clk:IN STD_LOGIC;d:IN STD_LOGIC;q:OUT STD_LOGIC;q_n:OUT STD_LOGIC);END ENTITY dff_1; ARCHITECTURE rtl OF dff_1 IS BEGINPROCESS (rst_n,clk) ISBEGINIF rst_n='0' THENq<='0';q_n<=NOT d;END IF;END PROCESS;END ARCHITECTURE rtl;library ieee;use ieee.std_logic_1164.all;entity d_ff isport(rst_n,clk,d: in std_logic;q,q_n:out std_logic);end entity d_ff;architecture rtl of d_ff isbeginprocess (rst_n,clk) isbeginif rst_n='0' thenq<='0';q_n<='1';elsif clk'event and clk='1' thenq<=d;q_n<=not d;end if;end process;end architecture rtl;library ieee;use ieee.std_logic_1164.all;entity shift_reg8 isgeneric (N:integer:=8);port(rst_n,a,clk: in std_logic;b:out std_logic);end entity shift_reg8 ;architecture rtl of shift_reg8 iscomponent d_ff isport(rst_n,clk,d: in std_logic;q,q_n:out std_logic);end component d_ff;signal s:std_logic_vector(1 to N-1);beging1: for i in 0 to N-1 generateg2: if i=0 generatedffx: d_ff port map ( rst_n,clk,a,s(i+1));end generate;g3: if i=N-1 generatedffx: d_ff port map(rst_n,clk,s(i),b);end generate;g4: if (i/=0) and (i/=N-1) generatedffx: d_ff port map( rst_n,clk,s(i),s(i+1));end generate;end generate;end architecture rtl;library ieee;use ieee.std_logic_1164.all;use work.my_package.all;entity int_bin isport( a:in integer range 0 to 9;b: in std_logic_vector(3 downto 0);c: out std_logic_vector(3 downto 0);d: out integer range 0 to 9);end entity int_bin;architecture rtl of int_bin isbeginc<=int_to_bit (a);bit_to_int(b,d);end architecture rtl;library ieee;use ieee.std_logic_1164.all;package my_package isprocedure bit_to_int( a: in std_logic_vector( 3 downto 0);signal b:out integer range 15 downto 0); function int_to_bit(a: integer range 15 downto 0) return std_logic_vector;end package my_package;package body my_package isprocedure bit_to_int( a: in std_logic_vector( 3 downto 0);signal b:out integer range 15 downto 0) is begincase a iswhen "0000" => b<=0;when "0001" => b<=1;when "0010" => b<=2;when "0011" => b<=3;when "0100" => b<=4;when "0101" => b<=5;when "0110" => b<=6;when "0111" => b<=7;when "1000" => b<=8;when "1001" => b<=9;when "1010" => b<=10;when "1011" => b<=11;when "1100" => b<=12;when "1101" => b<=13;when "1110" => b<=14;when "1111" => b<=15;when others => null;end case;end procedure bit_to_int;function int_to_bit (a: integer range 15 downto 0) return std_logic_vector is variable b: std_logic_vector(3 downto 0);begincase a iswhen 0 => b:="0000";when 1 => b:="0001";when 2 => b:="0010";when 3 => b:="0011";when 4 => b:= "0100";when 5 => b:="0101";when 6 => b:="0110";when 7 => b:="0111";when 8 => b:="1000";when 9 => b:="1001";when 10 => b:="1010" ;when 11 => b:="1011" ;when 12 => b:="1100";when 13 => b:="1101";when 14 => b:="1110";when 15 => b:="1111";when others => null;end case;return b;end function;end package body my_package;library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity cnt6 isport( clk: in std_logic;data_o:out std_logic_vector(2 downto 0));end entity cnt6;architecture rtl of cnt6 is--signal cnt:std_logic_vector(2 downto 0):="000"; beginprocess(clk) isvariable cnt:std_logic_vector(2 downto 0):="000";beginif clk'event and clk='1' thenif cnt="101" then--cnt<="000";cnt:="000";else--cnt<=cnt+1;cnt:=cnt+1;end if;end if;data_o<=cnt;end process;--data_o<=cnt;end architecture rtl;library ieee;use ieee.std_logic_1164.all;entity full_adder isport( a:in std_logic;b:in std_logic;c:in std_logic;s:out std_logic;co:out std_logic);end entity full_adder;architecture rtl of full_adder issignal a_n,b_n,c_n:std_logic;signal m1,m2,m4,m7:std_logic;signal m3,m5:std_logic;begina_n<=not a;b_n<=not b;c_n<=not c;m1<=a_n and b_n and c;m2<=a_n and b and c_n;m4<=a and b_n and c_n;m7<=a and b and c;m3<=a_n and b and c;m5<=a and b_n and c;s<=m1 or m2 or m4 or m7;co<= (a and b) or m3 or m5;end architecture rtl;library ieee;use ieee.std_logic_1164.all;entity max_min isport(a,b,c,d: in std_logic_vector(7 downto 0);max,min:out std_logic_vector(7 downto 0)); end entity max_min;--architecture rtl of max_min is-- begin-- process(a,b,c,d) is-- begin-- if a>b then-- if a>c then-- if a>d then-- max<=a;-- else-- max<=d;-- end if;-- else --a<c,c>b-- if c>d then-- max<=c;-- else-- max<=d;-- end if;-- end if;-- else -- a<b-- if b>c then-- if b>d then-- max<=b;-- else-- max<=d;-- end if;-- else --b<c,b>a-- if c>d then-- max<=c;-- else-- max<=d;-- end if;-- end if;-- end if;-- end process;-- end architecture rtl;architecture rtl of max_min isbeginprocess(a,b,c,d) isvariable t1:std_logic_vector(7 downto 0);begint1:=a;if t1<b thent1:=b;end if;if t1<c thent1:=c;end if;if t1<d thent1:=d;end if;end process;process(a,b,c,d) isvariable t2:std_logic_vector(7 downto 0);begint2:=a;if t2>b thent2:=b;end if;if t2>c thent2:=c;end if;if t2>d thent2:=d;end if;min<=t2;end process;end architecture rtl;library ieee;use ieee.std_logic_1164.all;entity relation_op isgeneric(N:integer:=8);port( a:in std_logic_vector(N-1 downto 0);b:in std_logic_vector(N-1 downto 0);c:in std_logic_vector(N-1 downto 0);d:in std_logic_vector(N-1 downto 0);max,min:out std_logic_vector(N-1 downto 0));end entity relation_op;architecture rtl of relation_op isshared variable tmp1,tmp2,tmp3,tmp4:std_logic_vector(N-1 downto 0); --variable tmp2:std_logic_vector(N-1 downto 0);beginprocess(a,b,c,d)isbeginif a<b thentmp1:=b;tmp2:=a;elsetmp1:=a;end if;if c<d thentmp3:=d;tmp4:=c;elsetmp3:=c;tmp4:=d;end if;if tmp1<tmp3 thenmax<=tmp3;elsemax<=tmp1;end if;if tmp2<tmp4 thenmin<=tmp2;elsemin<=tmp4;end if;end process;end architecture rtl;library ieee;use ieee.std_logic_1164.all;entity sum isport( start: in std_logic;dout:out integer range 0 to 4950);end entity sum;architecture rtl of sum isbeginprocess (start) isvariable rt:integer range 0 to 4950:=0;beginrt:=0;for i in 0 to 99 looprt:=rt+i;end loop;if start='1' thendout<=rt;elsedout<=0;end if;end process;end architecture rtl;library ieee;use ieee.std_logic_1164.all;entity mux_8 isport( d0,d1,d2,d3,d4,d5,d6,d7:in std_logic_vector( 7 downto 0);sel: in std_logic_vector( 2 downto 0);data_o:out std_logic_vector( 7 downto 0));end entity mux_8;architecture rtl of mux_8 isbeginprocess(sel,d0,d1,d2,d3,d4,d5,d6,d7) isbegincase sel iswhen "000" =>data_o<=d0;when "001" =>data_o<=d1;when "010" =>data_o<=d2;when "011" =>data_o<=d3;when "100" =>data_o<=d4;when "101" =>data_o<=d5;when "110" =>data_o<=d6;when "111" =>data_o<=d7;when others =>null;end case;end process;end architecture rtl;library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity cnt6 isport( clk:in std_logic;rst_n:in std_logic;data_o:out std_logic_vector(2 downto 0)); end entity cnt6;architecture rtl of cnt6 issignal tmp:std_logic_vector(2 downto 0);beginprocess(rst_n,clk) isbeginif rst_n='0' thentmp<="000";elsif clk'event and clk='1' thenif tmp="101" thentmp<="000";elsetmp<=tmp+1;end if;end if;end process;data_o<=tmp;end architecture rtl;--architecture rtl of cnt6 is-- signal tmp:std_logic_vector(2 downto 0);---- begin-- process(rst_n,clk) is-- begin---- if clk'event and clk='1' then-- if rst_n='0' then-- tmp<="000";-- else-- if tmp="101" then-- tmp<="000";-- else-- tmp<=tmp+1;-- end if;-- end if;-- end if;-- end process;-- data_o<=tmp;-- end architecture rtl;library ieee;use ieee.std_logic_1164.all;entity cnt isport( rst_n: in std_logic;clk: in std_logic;data_o:out std_logic_vector(2 downto 0)); end entity cnt;--architecture rtl of cnt is-- signal q0,q1,q2:std_logic;-- begin-- process(rst_n,clk) is-- begin-- if rst_n='0' then-- q0<='0';-- q1<='0';-- q2<='0';-- elsif clk'event and clk='1' then-- q0<=not q0;-- q1<=q0 xor q1;-- q2<=(q0 and q1) xor q2;-- end if;-- end process;-- data_o<=q2&q1&q0;-- end architecture rtl;architecture rtl of cnt issignal q0,q1,q2:std_logic;beginprocess(rst_n,clk) isbeginif rst_n='0' thenq0<='0';elsif clk'event and clk='1' thenq0<=not q0;end if;end process;process(rst_n,q0) isbeginif rst_n='0' thenq1<='0';elsif q0'event and q0='0' thenq1<=not q1;end if;end process;process(rst_n,q1) isbeginif rst_n='0' thenq2<='0';elsif q1'event and q1='0' thenq2<=not q2;end if;end process;data_o<=q2&q1&q0;end architecture rtl;library ieee;use ieee.std_logic_1164.all;entity fre_div isport( rst_n:in std_logic;clk:in std_logic;clk_o:out std_logic);end entity fre_div;architecture rtl of fre_div istype state_t is (s_rst,s0,s1,s2,s3,s4,s5,s6,s7,s8,s9);signal state,next_state:state_t;signal clk_tmp:std_logic;beginprocess(rst_n,clk) isbeginif rst_n='0' thenstate<=s_rst;elsif clk'event and clk='1' thenstate<=next_state;end if;end process;process(state) isbegincase state iswhen s_rst => next_state<=s0;when s0 => next_state<=s1;when s1 => next_state<=s2;when s2 => next_state<=s3;when s3 => next_state<=s4;when s4 => next_state<=s5;when s5 => next_state<=s6;when s6 => next_state<=s7;when s7 => next_state<=s8;when s8 => next_state<=s9;when s9 => next_state<=s0;end case;end process;process(state) isbegincase state iswhen s_rst => clk_tmp<='0';when s0 => clk_tmp<='1';when s1 => clk_tmp<='1';when s2 => clk_tmp<='1';when s3 => clk_tmp<='1';when s4 => clk_tmp<='0';when s5 => clk_tmp<='0';when s6 => clk_tmp<='0';when s7 => clk_tmp<='0';when s8 => clk_tmp<='0';when s9 => clk_tmp<='0';end case;end process;process(clk) isbeginif clk'event and clk='1' thenclk_o<=clk_tmp;end if;end process;end architecture rtl;。
实验1—— EDA 工具使用与2选1多路选择器VHDL 描述实验2—— D 触发器VHDL 描述实验3—— 半加器VHDL 描述实验4—— 全加器VHDL 描述实验5——方波发生器VHDL 程序(简易函数发生器设计1)LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY square ISPORT(clk,reset : IN STD_LOGIC;Q : OUT STD_LOGIC);END square;ARCHITECTURE bhv OF square ISBEGINPROCESS(clk,reset)VARIABLE cnt : INTEGER RANGE 0 TO 63;BEGINIF reset='0' THENq<='0';ELSIF clk'EVENT AND clk='1' THENcnt:=cnt+1;IF cnt<31 THENq<='0';ELSEq<='1';END IF;END IF;END PROCESS;END bhv;实验6 —正弦波发生器VHDL程序(简易函数发生器设计2)LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY sin ISPORT(clk,reset:IN STD_LOGIC;q:OUT INTEGER RANGE 0 TO 255);END sin;ARCHITECTURE bhv OF sin ISBEGINPROCESS(clk,reset)VARIABLE tmp:INTEGER RANGE 0 TO 63;BEGINIF reset='0' THENq<=0;ELSIF clk'EVENT AND clk='1' THENIF tmp=63 THENtmp:=0;ELSEtmp:=tmp+1;END IF;CASE tmp ISWHEN 00=>q<=255;WHEN 01=>q<=254;WHEN 02=>q<=252;WHEN 03=>q<=249;WHEN 04=>q<=245;WHEN 05=>q<=239;WHEN 06=>q<=233;WHEN 07=>q<=225;WHEN 08=>q<=217;WHEN 09=>q<=207;WHEN 10=>q<=197;WHEN 11=>q<=186;WHEN 13=>q<=162; WHEN 14=>q<=150; WHEN 15=>q<=137; WHEN 16=>q<=124; WHEN 17=>q<=112; WHEN 18=>q<=99; WHEN 19=>q<=87; WHEN 20=>q<=75; WHEN 21=>q<=64; WHEN 22=>q<=53; WHEN 23=>q<=43; WHEN 24=>q<=34; WHEN 25=>q<=26; WHEN 26=>q<=19; WHEN 27=>q<=13; WHEN 28=>q<=8; WHEN 29=>q<=4; WHEN 30=>q<=1; WHEN 31=>q<=0; WHEN 32=>q<=0; WHEN 33=>q<=1; WHEN 34=>q<=4; WHEN 35=>q<=8; WHEN 36=>q<=13; WHEN 37=>q<=19; WHEN 38=>q<=26; WHEN 39=>q<=34; WHEN 40=>q<=43; WHEN 41=>q<=53; WHEN 42=>q<=64; WHEN 43=>q<=75; WHEN 44=>q<=87; WHEN 45=>q<=99; WHEN 46=>q<=112; WHEN 47=>q<=124; WHEN 48=>q<=137; WHEN 49=>q<=150;WHEN 51=>q<=174;WHEN 52=>q<=186;WHEN 53=>q<=197;WHEN 54=>q<=207;WHEN 55=>q<=217;WHEN 56=>q<=225;WHEN 57=>q<=233;WHEN 58=>q<=239;WHEN 59=>q<=245;WHEN 60=>q<=249;WHEN 61=>q<=252;WHEN 62=>q<=254;WHEN 63=>q<=255;WHEN OTHERS=>NULL;END CASE;END IF;END PROCESS;END bhv;实验7------- 加减法计数器VHDL程序加减法计数器:本例程为加减法计数器,主要实现的加减法计数的功能。
vhdl语言100例程序以下是100个关于VHDL语言的程序示例:1. 用VHDL编写一个计数器模块2. 用VHDL编写一个SR-Latch模块3. 用VHDL编写一个JK-Flip Flop模块4. 用VHDL编写一个D-Flip Flop模块5. 用VHDL编写一个T-Flip Flop模块6. 用VHDL编写一个复位计数器模块7. 用VHDL编写一个移位寄存器模块8. 用VHDL编写一个状态机模块9. 用VHDL编写一个MUX模块10. 用VHDL编写一个DeMUX模块11. 用VHDL编写一个加法器模块12. 用VHDL编写一个减法器模块13. 用VHDL编写一个乘法器模块14. 用VHDL编写一个除法器模块15. 用VHDL编写一个比较器模块16. 用VHDL编写一个位逻辑模块17. 用VHDL编写一个字逻辑模块18. 用VHDL编写一个数据选择器模块19. 用VHDL编写一个FIFO队列模块20. 用VHDL编写一个LIFO栈模块21. 用VHDL编写一个流水线模块22. 用VHDL编写一个中断控制器模块23. 用VHDL编写一个时钟分频器模块24. 用VHDL编写一个IO控制器模块25. 用VHDL编写一个SPI通信控制器模块26. 用VHDL编写一个I2C通信控制器模块27. 用VHDL编写一个UART通信控制器模块28. 用VHDL编写一个哈希函数模块29. 用VHDL编写一个随机数产生器模块30. 用VHDL编写一个CRC校验器模块31. 用VHDL编写一个AES加密算法模块32. 用VHDL编写一个DES加密算法模块33. 用VHDL编写一个SHA加密算法模块34. 用VHDL编写一个MD5加密算法模块35. 用VHDL编写一个RSA加密算法模块36. 用VHDL编写一个卷积滤波器模块37. 用VHDL编写一个峰值检测器模块38. 用VHDL编写一个平滑滤波器模块39. 用VHDL编写一个中值滤波器模块40. 用VHDL编写一个微处理器模块41. 用VHDL编写一个信号发生器模块42. 用VHDL编写一个信号采集器模块43. 用VHDL编写一个频率计算器模块44. 用VHDL编写一个相位计算器模块45. 用VHDL编写一个时序分析器模块46. 用VHDL编写一个正弦波产生器模块47. 用VHDL编写一个余弦波产生器模块48. 用VHDL编写一个数字滤波器模块49. 用VHDL编写一个数字信号处理器模块50. 用VHDL编写一个数字识别模块51. 用VHDL编写一个自动售货机模块52. 用VHDL编写一个二进制加法器模块53. 用VHDL编写一个二进制减法器模块54. 用VHDL编写一个二进制乘法器模块55. 用VHDL编写一个二进制除法器模块56. 用VHDL编写一个自然对数模块57. 用VHDL编写一个指数函数模块58. 用VHDL编写一个三角函数模块59. 用VHDL编写一个高斯滤波器模块60. 用VHDL编写一个激光传感器模块61. 用VHDL编写一个超声波传感器模块62. 用VHDL编写一个光电传感器模块63. 用VHDL编写一个温度传感器模块64. 用VHDL编写一个气压传感器模块65. 用VHDL编写一个陀螺仪模块67. 用VHDL编写一个电流传感器模块68. 用VHDL编写一个电容传感器模块69. 用VHDL编写一个磁场传感器模块70. 用VHDL编写一个通信电缆模块71. 用VHDL编写一个电源控制器模块72. 用VHDL编写一个电机控制器模块73. 用VHDL编写一个汽车控制器模块74. 用VHDL编写一个飞机控制器模块75. 用VHDL编写一个摄像头模块76. 用VHDL编写一个音频控制器模块77. 用VHDL编写一个扬声器控制器模块78. 用VHDL编写一个拨号器模块79. 用VHDL编写一个振动控制器模块80. 用VHDL编写一个压力控制器模块81. 用VHDL编写一个过滤器模块82. 用VHDL编写一个微波发射模块84. 用VHDL编写一个智能电表模块85. 用VHDL编写一个闹钟模块86. 用VHDL编写一个计时器模块87. 用VHDL编写一个时间戳模块88. 用VHDL编写一个脉冲宽度模块89. 用VHDL编写一个电路仿真模块90. 用VHDL编写一个电路控制模块91. 用VHDL编写一个电路测试模块92. 用VHDL编写一个电路优化模块93. 用VHDL编写一个电路布局模块94. 用VHDL编写一个电路验证模块95. 用VHDL编写一个数字信号发生器模块96. 用VHDL编写一个数字信号反演器模块97. 用VHDL编写一个数字信号滤波器模块98. 用VHDL编写一个数字信号加速器模块99. 用VHDL编写一个数字信号降噪器模块100. 用VHDL编写一个数字信号解调器模块VHDL语言是一种硬件描述语言,它用于描述数字电路和系统。
将8421BCD转换为余3码源代码:Library ieee;Use ieee.std_logic_1164.all;Entity bcd isPort(a:in std_logic_vector(3 downto 0);y:out std_logic_vector(3 downto 0));End;Architecture rtl of bcd isBeginProcess(a)BeginCase a isWhen"0000"=>y<="0011";When"0001"=>y<="0100";When"0010"=>y<="0101";When"0011"=>y<="0110";When"0100"=>y<="0111";When"0101"=>y<="1000";When"0110"=>y<="1001";When"0111"=>y<="1010";When"1000"=>y<="1011";When"1001"=>y<="1100";When others=>y<="ZZZZ";End case;End process;End;仿真图形:(仿真结果均有延时,大约20ns)四输入表决器源代码:Library ieee;Use ieee.std_logic_1164.all;Entity bjq isPort(i:in std_logic_vector(3 downto 0);f:out std_logic);End;Architecture nm2 of bjq isBeginProcess(i)Begincase i isWhen"0000"=>f<='0';When"0001"=>f<='0';When"0010"=>f<='0';When"0011"=>f<='0';When"0100"=>f<='0';When"0101"=>f<='0';When"0110"=>f<='0';When"0111"=>f<='1';When"1000"=>f<='0';When"1001"=>f<='0';When"1010"=>f<='0';When"1011"=>f<='1';When"1100"=>f<='0';When"1101"=>f<='1';When"1110"=>f<='1';When"1111"=>f<='1';When others=>f<='Z';End case;End process;End;仿真图形:2位二进制相乘电路源代码:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity multi isport(A,B:in std_logic_vector(1 downto 0);F:out std_logic_vector(3 downto 0));end;architecture bhv of multi isbeginprocess(A,B)beginif(A="01" and B="01" )thenF<="0001";elsif(A="01" and B="10")thenF<="0010";elsif(A="01" and B="11")thenF<="0011";elsif(A="10" and B="01")thenF<="0010";elsif(A="10" and B="10")thenF<="0100";elsif(A="10" and B="11")thenF<="0110";elsif(A="11" and B="01")thenF<="0011";elsif(A="11" and B="10")thenF<="0110";elsif(A="11" and B="11")thenF<="1001";elseF<="0000";end if;end process;end;仿真图形:一位二进制全减器源代码:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity subtracter isport(A,B,Ci:in std_logic;F,Co:out std_logic);end;architecture bhv of subtracter isbeginprocess(A,B,Ci)beginif(A='0' and B='0' and Ci='0')thenF<='0';Co<='0';elsif(A='0' and B='0' and Ci='1')thenF<='1';Co<='1';elsif(A='0' and B='1' and Ci='0')thenF<='1';Co<='1';elsif(A='0' and B='1' and Ci='1')thenF<='0';Co<='1';elsif(A='1' and B='0' and Ci='0')thenF<='1';Co<='0';elsif(A='1' and B='0' and Ci='1')thenF<='0';Co<='0';elsif(A='1' and B='1' and Ci='0')thenF<='0';Co<='0';elseF<='1';Co<='1';end if;end process;end;仿真图形:开关控制电路源代码:Library ieee;Use ieee.std_logic_1164.all;Entity switch_control isPort(a,b,c:in std_logic;y:out std_logic);End;Architecture nm5 of switch_control isBeginProcess(a,b,c);V ariable comb:std_logic_vector(2 downto 0);BeginComb:=a&b&c;Case comb isWhen"000"=>y<='0';When"001"=>y<='1';When"011"=>y<='0';When"010"=>y<='1';When"110"=>y<='0';When"111"=>y<='1';When"101"=>y<='0';When"100"=>y<='1';When others=>y<='X';End case;End process;End;仿真图形:4-16译码器library ieee;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY decode4-16 ISPORT(a,b,c,d:IN STD_LOGIC;q:BUFFER STD_LOGIC_VECTOR(15 DOWNTO 0)); END decode4-16 ;architecture behave of decode4-16 issignal indata:std_logic_vector(2 downto 0);beginindata<=c&b&a;process(indata)begincase indata iswhen “0000”=>y<=”1111111111111110”;when “0001”=>y<=”1111111111111101”;when “0010”=>y<=”1111111111111011”;w hen “0011”=>y<=”1111111111110111”;when “0100”=>y<=”1111111111101111”;when “0101”=>y<=”1111111111011111”;when “0110”=>y<=”1111111110111111”;when “0111”=>y<=”1111111101111111”;when “1000”=>y<=”1111111011111111”;when “1001”=>y<=”1111110111111111”;when “1010”=>y<=”1111101111111111”;when “1011”=>y<=”1111011111111111”;when “1100”=>y<=”1110111111111111”;when “1101”=>y<=”1101111111111111”;when “1110”=>y<=”1011111111111111”;when “1111”=>y<=”0111111111111111”;when others=>y<=”xxxxxxxxxxxxxxxx”;end case;end process;end behave;4-16译码器library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;---- Uncomment the following library declaration if instantiating ---- any Xilinx primitives in this code.--library UNISIM;--use UNISIM.VComponents.all;entity test isPort(a:in std_logic;b:in std_logic;c:in std_logic;d:in std_logic;Y:buffer std_logic_vector(15 downto 0);r0:out std_logic;r1:out std_logic;r2:out std_logic;r3:out std_logic);end test;architecture Behavioral of test issignal DIN:std_logic_vector(3 downto 0);beginDIN<=a&b&c&d;process(DIN)begincase DIN iswhen"0000"=>Y<="1111111111111110";when"0001"=>Y<="1111111111111101";when"0010"=>Y<="1111111111111011";when"0011"=>Y<="1111111111110111";when"0100"=>Y<="1111111111101111";when"0101"=>Y<="1111111111011111";when"0110"=>Y<="1111111110111111";when"0111"=>Y<="1111111101111111";when"1000"=>Y<="1111111011111111";when"1001"=>Y<="1111110111111111";when"1010"=>Y<="1111101111111111";when"1011"=>Y<="1111011111111111";when"1100"=>Y<="1110111111111111";when"1101"=>Y<="1101111111111111";when"1110"=>Y<="1011111111111111";when"1111"=>Y<="0111111111111111";when others=>Y<="0000000000000000";end case;end process;r0<=not(Y(5) and Y(7) and Y(13) and Y(15));r1<=not(Y(6) and Y(7) and Y(9) and Y(11) and Y(13) and Y(14));r2<=not(Y(10) and Y(11) and Y(14));r3<=not(Y(15) and Y(15));end Behavioral;NET "d" LOC="p94";NET "c" LOC="p95";NET "b" LOC="p96";NET "a" LOC="p97";NET "r0" LOC="p58";NET "r1" LOC="p59";NET "r2" LOC="p60";NET "r3" LOC="p61";1.设计60进计数器:1)设计思想:两个同步计数器,一个实现个位计数,一个实现十位计数,当个位计数到9时,十位的计数器加一,并个位计数器清零,继续自加,如此循环,直到十位到5,即计数到59,一端口输出高电平,十位和个位计数器清零,如此循环。
实验一. 分频器设计一.实验目的1.熟悉QUARTUSII 软件的使用2.熟悉PLD设计流程3. 学习分频器的设计二.实验内容设计一个最大分频为225的分频器,将50MHz时钟作为输入三.实验框图四.管脚设定CLOCK_50 PIN_N2LEDR[0] PIN_AE23五.实验代码LIBRARY ieee;USE ieee.std_logic_1164.all;USE ieee.std_logic_UNSIGNED.all;ENTITY clk1 ISPORT(clk:IN STD_LOGIC;DIGIT:OUT STD_LOGIC);END clk1;ARCHITECTURE clk1 OF clk1 ISBEGINCOUNT: PROCESS(clk)V ARIABLE temp:STD_LOGIC_VECTOR(25 DOWNTO 0);BEGINIF(clk'EVENT AND clk = '1')THENtemp := temp+1;IF(temp(25)='1') THENtemp:=(OTHERS=>'0');END IF;END IF;DIGIT <= temp(24);END PROCESS count;END clk1;六.心得体会通过这次实验,我初步掌握了QUARTUSII 软件的使用,为今后的实验打下基础。
实验二. VHDL描述风格比较一.实验目的1.深入体会VHDL三种描述风格的区别2. 学习3输入表决器,异或门的实现3.设计一个5输入表决器。
二.实验内容以3输入表决器,异或门,通用寄存器等代码为例,深入体会VHDL描述风格。
1.学习已给的3输入表决器代码,完成3输入表决器的三种描述方式的验证比较。
在QUARTUS II中对程序进行编译,下载,验证。
使用拔码开关SW0,SW1,SW2作为三个输入,输出在LEDR0表示,亮表示‘1’,不亮表示‘0’2.学习已给的异或门代码,完成异或门的三种描述方式的验证比较。
1.三与门library ieee;use ieee.std_logic_1164.all;entity yumen isport(a,b,c : in std_logic;f : out std_logic);end yumen;architecture and3_1 of yumen isbeginf<=a and b and c;end architecture and3_1;2.三八译码器library ieee;use ieee.std_logic_1164.all;entity jg isport(a,b,c,g1,g2a,g2b:in std_logic;y:out std_logic_vector(7 downto 0));end entity jg;architecture rt1 of jg issignal indata:std_logic_vector(2 downto 0); beginindata<=c&b&a;process(indata,g1,g2a,g2b)isbeginif(g1='1' and g2a='0' and g2b='0')then case indata iswhen"000"=>y<="11111110"; when"001"=>y<="11111101"; when"010"=>y<="11111011"; when"011"=>y<="11110111"; when"100"=>y<="11101111"; when"101"=>y<="11011111"; when"110"=>y<="10111111"; when"111"=>y<="01111111";when others=>y<="xxxxxxxx";end case;elsey<="11111111";end if;end process;end rt1; 3.同步复位/置位、下降沿触发的d触发器ibrary ieee;use ieee.std_logic_1164.all;entity adff isport(clk,d,r,s:in std_logic;q:out std_logic);end adff;architecture rtl of adff issignal q_temp,qb_temp:std_logic;beginprocess(clk,r,s)beginif(clk'event and clk='0')thenif(r='0' and s='1')thenq_temp<='1';if(r='1' and s='0')thenq_temp<='0';elseq_temp<=d;end if;end if;end if;end process;q<=q_temp;end rtl;4.异步复位/置位、上升沿触发的d发器ibrary ieee;use ieee.std_logic_1164.all;entity adff isport(clk,d,r,s:in std_logic;q:out std_logic);end adff;architecture rtl of adff issignal q_temp,qb_temp:std_logic;beginprocess(clk,r,s)beginif(r='0' and s='1')thenq_temp<='1';elsif(r='1' and s='0')thenq_temp<='0';elsif(clk'event and clk='1')thenq_temp<=d;end if;end process;q<=q_temp;end rtl;5.四分频器ibrary ieee;use ieee.std_logic_1164.all;entity one isport( clk1:in std_logic;clk4:out std_logic);end one;architecture one1 of one issignal data1:integer range 0 to 10;signal q1:std_logic;beginprocess(clk1)beginif rising_edge(clk1) thenif(data1=1) thendata1<=0;q1<=not q1;elsedata1<=data1+1;end if;end if;clk4<=q1;end process;end architecture one1;6.四选一library ieee;use ieee.std_logic_1164.all;entity mux4 isport(input:in std_logic_vector(3 downto 0); a,b:in std_logic;y : out std_logic);end mux4 ;architecture rtl of mux4 issignal sel:std_logic_vector(1 downto 0); beginsel<=b & a;process (input,sel)beginif (sel="00") theny <= input(0);elsif (sel="01") theny <= input(1);elsif (sel="10") theny <= input(2);elsey <= input(3);end if;end process;end rtl; 7.五分频器use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;use ieee.std_logic_arith.all;entity fenpin5 isport (rst,clkin :in std_logic;clkout:out std_logic);end fenpin5;architecture rtl of fenpin5 issignal count1,count2: std_logic_vector(7 downto 0);signal tmp,tmp1,tmp2: std_logic;begintmp<=tmp1 and tmp2;clkout<=tmp xor tmp1;process(clkin,rst)beginif rst ='1'thencount1 <= "00000000";tmp1<= '0';elsif clkin'event and clkin='1' thenif count1 = "00000100" thencount1 <= "00000000";elsecount1 <= count1 + 1;if count1 < "00000010" thentmp1<= '0';elsetmp1<= '1';end if;end if;end if;end process;end rtl;8.moore状态机library ieee;use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity moore isport (rst,clk,x:in std_logic; op:out std_logic);end moore;architecture a of moore is type state is (s0,s1,s2,s3); signal st: state;beginstate_comp: process(rst,clk) beginif rst='1' thenst <= s0;elsif rising_edge(clk) then case st iswhen s0 =>if x = '0' thenst <= s0;elsest <= s1;end if;op <= '1';when s1 =>if x = '0' thenst <= s3;elsest <= s2;end if;op <= '1';when s2=>if x = '0' thenst <= s2;elsest <= s3;end if;op <= '1';when s3=>if x = '0' thenst <= s3;elsest <= s0;end if;op <= '0';end case;end if; 9.mealy状态机library ieee;use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity mealy isport (rst,clk,x:in std_logic; op:out std_logic);end mealy;architecture a of mealy istype state is (s0,s1,s2,s3); signal st : state;beginstate_comp: process(rst,clk) beginif rst='1' thenst <= s0;elsif rising_edge(clk) then case st iswhen s0 =>if x = '0' thenst <= s0; op <= '0';elsest <= s1; op <= '1';end if;when s1 =>if x = '0' thenst <= s3; op <= '1';elsest <= s2; op <= '1';end if;when s2 =>if x = '0' thenst <= s2; op <= '0';elsest <= s3; op <= '1';end if;when s3 =>if x = '0' thenst <= s3; op <= '0';elsest <= s0; op <= '0';end if;end case;end if;end process state_comp;end a10.全加器library ieee;use ieee.std_logic_1164.all;entity full_adder isport (a,b,cin:in std_logic;s,co:out std_logic);end full_adder;architecture full1 of full_adder issignal tmp1,tmp2,tmp3:std_logic;begintmp1 <= a xor b;tmp2 <= a and b;tmp3 <= tmp1 and cin;s <= tmp1 xor cin;co <= tmp2 or tmp3;end full1;11.同步12进制计数器library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity count12en isport (clk,clr, en :in std_logic;qa,qb,qc,qd:out std_logic);end count12en;architecture rtl of count12en issignal count_4:std_logic_vector(3 downto 0); beginqa<=count_4(0);qb<=count_4(1);qc<=count_4(2);qd<=count_4(3);process (clr,clk)beginif(clr='1') thencount_4<="0000";elsif (clk'event and clk ='1') thenif(en='1') thenif (count_4="1011") thencount_4<="0000";elsecount_4<=count_4+'1';end if;end if;end if;end process;end rtl; 12.优先编码器library ieee;use ieee.std_logic_1164.all;entity priority_encoder isport(input:in std_logic_vector(7 downto 0); y : out std_logic_vector(2 downto 0));end priority_encoder;architecture rtl of priority_encoder is beginp1: process (input)beginif ( input(0) ='0') theny <= "111";elsif (input(1) ='0') theny <= "110";elsif (input(2) ='0') theny <= "101";elsif (input(3) ='0') theny <= "100";elsif (input(4) ='0') theny <= "011";elsif (input(5) ='0') theny <= "010";elsif (input(6) ='0') theny <= "001";elsey <= "000";end if;end process p1;end rtl;。
vhdl编程实例VHDL编程实例- 设计与实现一个4位的全加器在本篇文章中,我们将一步一步地回答如何设计和实现一个4位的全加器。
VHDL编程语言将是我们用于描述和模拟这个电路的工具。
第一步:理解全加器的原理在编写代码之前,我们首先需要理解全加器的原理。
全加器是一种用于对两个二进制数字进行相加的电路。
它接收三个输入信号:两个位的输入(A 和B)以及一个进位输入(C_in)。
全加器的输出结果为一个位的和(S)和一个进位输出(C_out)。
我们可以使用如下的真值表来描述全加器的输出结果:输入信号输出结果A B C_in S C_out0 0 0 0 00 0 1 1 00 1 0 1 00 1 1 0 11 0 0 1 01 0 1 0 11 1 0 0 11 1 1 1 1了解了全加器的工作原理后,我们可以开始编写代码了。
第二步:编写全加器的VHDL代码我们将使用VHDL语言来描述和模拟全加器。
下面是一个简单的4位全加器的VHDL代码实现:vhdlEntity声明entity full_adder isport (A, B : in std_logic_vector(3 downto 0);C_in : in std_logic;S : out std_logic_vector(3 downto 0);C_out : out std_logic);end full_adder;Architecture声明architecture Behavioral of full_adder isbeginprocess(A, B, C_in)variable carry : std_logic;begincarry := C_in;for i in 0 to 3 loopS(i) <= A(i) xor B(i) xor carry;carry := (A(i) and B(i)) or (carry and (A(i) xor B(i)));end loop;C_out <= carry;end process;end Behavioral;在此代码中,我们首先声明了一个实体(entity)和一个架构(architecture)。
实验一:半加器--底层的异或门library IEEE;use IEEE.std_logic_1164.all;entity xor_gate isport( a1,a2 : in std_logic;xor_out : out std_logic); end entity;architecture behav1 of xor_gate is beginxor_out <=a1 xor a2;end behav1;--底层的与门library IEEE;use IEEE.std_logic_1164.all;entity and_gate isport( a1,a2 : in std_logic;and_out : out std_logic); end entity;architecture behav1 of and_gate is beginand_out <=a1 and a2;end behav1;--顶层的设计实体library IEEE;use IEEE.std_logic_1164.all;entity half_adder isport( ai,bi : in std_logic;so,co : out std_logic); end entity;architecture struct2 of half_adder is component xor_gateport( a1,a2 : in std_logic;xor_out : out std_logic); end component;component and_gateport( a1,a2 : in std_logic;and_out : out std_logic); end component;beging1: xor_gate port map(a1=> ai, a2=> bi, xor_out=> so);--对异或门的例化g2: and_gate port map(a1=> ai, a2=> bi, and_out=> co);--对与门的例化end struct2;实验二:加法器--底层的异或门library IEEE;use IEEE.std_logic_1164.all;entity xor_gate isport( a1,a2 : in std_logic;xor_out : out std_logic);end entity;architecture behav1 of xor_gate isbeginxor_out <=a1 xor a2;end behav1;--底层的与门library IEEE;use IEEE.std_logic_1164.all;entity and_gate isport( a1,a2 : in std_logic;and_out : out std_logic);end entity;architecture behav1 of and_gate isbeginand_out <=a1 and a2;end behav1;--顶层的设计实体library IEEE;use IEEE.std_logic_1164.all;entity half_adder isport( ai,bi : in std_logic;so,co : out std_logic);end entity;architecture struct2 of half_adder iscomponent xor_gateport( a1,a2 : in std_logic;xor_out : out std_logic);end component;component and_gateport( a1,a2 : in std_logic;and_out : out std_logic);end component;beging1: xor_gate port map(a1=> ai, a2=> bi, xor_out=> so);--对异或门的例化g2: and_gate port map(a1=> ai, a2=> bi, and_out=> co);--对与门的例化end struct2;实验三:--底层的异或门library IEEE;use IEEE.std_logic_1164.all;entity xor_gate isport( a1,a2 : in std_logic;xor_out : out std_logic);end entity;architecture behav1 of xor_gate isbeginxor_out <=a1 xor a2;end behav1;--底层的与门library IEEE;use IEEE.std_logic_1164.all;entity and_gate isport( a1,a2 : in std_logic;and_out : out std_logic);end entity;architecture behav1 of and_gate isbeginand_out <=a1 and a2;end behav1;--顶层的设计实体library IEEE;use IEEE.std_logic_1164.all;entity half_adder isport( ai,bi : in std_logic;so,co : out std_logic);end entity;architecture struct2 of half_adder iscomponent xor_gateport( a1,a2 : in std_logic;xor_out : out std_logic);end component;component and_gateport( a1,a2 : in std_logic;and_out : out std_logic);end component;beging1: xor_gate port map(a1=> ai, a2=> bi, xor_out=> so);--对异或门的例化g2: and_gate port map(a1=> ai, a2=> bi, and_out=> co);--对与门的例化end struct2;实验四:自动售饮料机library ieee;use ieee.std_logic_1164.all;entity drinker isport(clk,rst:in std_logic;--clk时钟信号,rst复位x:in std_logic_vector(1 downto 0);--两个输入c:out std_logic_vector(1 downto 0)--两个输出);end drinker;architecture behave of drinker istype states is (s0,s1,s2);--自定义类型signal state:states;beginprocess (clk,rst)beginif rst='1' then state<=s0;--异步复位成初始状态s0c<="00";--初始输出为00elsif (clk' event and clk ='1') then --时钟上升沿case state iswhen s0=>--当状态为s0时if x="01" then --如果输入为01state<=s1;c<="00";--输出为00elsifx="10" then state<=s2;c<="00";elsestate<=s0; c<="00";--当输入为00或是约束的状态则保持原来的状态不变end if;when s1=>--当状态为s1时if x="01" thenstate<=s2;c<="00";elsifx="10" then --输入为10state<=s0;c<="10";--输出为10,状态变为s0elsestate<=s1; c<="00";--当输入为00或是约束的状态则保持原来的状态不变end if;when s2=>if x="01" thenstate<=s0;c<="10";elsifx="10" thenstate<=s0;c<="11";elsestate<=s2; c<="00";--当输入为00或是约束的状态则保持原来的状态不变end if;end case;end if;end process;end behave;。
计算机科学与技术学院实验报告(学年度第学期)课程名称EDA技术实验姓名学号专业计算机班级地点教师实验一:八位二进制补码一.实验目的1.熟悉Max+PlusII和GW48EDA开发系统的使用;2.掌握八位二进制补码的VHDL设计;3.元件例化语句的使用。
二.实验原理若原码为正,则补码等于原码;若原码为负,则补码为(2+原码)mod2。
三.八位二进制补码程序LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY JACKAN ISPORT(rst:IN STD_LOGIC;din:IN STD_LOGIC_VECTOR(7 DOWNTO 0);dout:OUT STD_LOGIC_VECTOR(7 DOWNTO 0));END ENTITY JACKAN;ARCHITECTURE HAIXIA OF JACKAN ISSIGNAL tmp:STD_LOGIC_VECTOR(6 DOWNTO 0);BEGINPROCESS(din,rst)BEGINIF rst='0' THENdout<=(OTHERS=>'0');ELSIF din(7) ='1' THENFOR i IN 0 TO 6 LOOPtmp(i)<=NOT din(i);END LOOP;dout(6 DOWNTO 0) <= tmp+1;dout(7) <= din(7);ELSEdout<= din;END IF;END PROCESS;END ARCHITECTURE HAIXIA;四.实验结果五.总结8位二进制补码:寄存器主要用来存储8位二进制数据。
高8位为符号位,不进行求反运算。
余下7位根据高8位的数据状态进行相应操作。
实验二.一位全减器的VHDL设计一. 实验目的1.熟悉Max+PlusII和GW48EDA开发系统的使用;2.掌握一位半减器的VHDL设计;3.掌握一位半减器构建一位全减器的方法;二.实验原理由两个半减器和一个或门构成一个全减器。
可实现功能:数码管显示,最右侧的一位数码管以1Hz变化由0-9循环往复分频+计数+显示整合之后(请注意改名后使用)100%成功:library ieee;use ieee.std_logic_1164.all;USE IEEE.STD_LOGIC_UNSIGNED.ALL;entity zhengti28 isport(clk:in std_logic;zhengti28:out std_logic;cp,clear:IN STD_LOGIC;q:OUT STD_LOGIC_VECTOR(3 DOWNTO 0);A:IN STD_LOGIC_VECTOR(3 DOWNTO 0);LED7S:OUT STD_LOGIC_VECTOR(6 DOWNTO 0);cat:OUT STD_LOGIC_VECTOR(5 DOWNTO 0));end;architecture one of zhengti28 issignal cnt:std_logic_vector(2 downto 0);signal clk_temp:std_logic;SIGNAL temp:STD_LOGIC_VECTOR(3 DOWNTO 0);SIGNAL tmp: INTEGER RANGE 0 TO 49999999;SIGNAL clk_out: STD_LOGIC;constant m:integer:=5;beginprocess(clk)beginif clk'event and clk='1' thenif cnt=m thenclk_temp<=not clk_temp;cnt<="000";elsecnt<=cnt+1;end if;end if;end process;zhengti28<=clk_temp;P1:PROCESS(cp)BEGINIF cp'event AND cp='1' THENIF tmp=49999999 THENtmp<=0;ELSEtmp<=tmp+1;END IF;IF tmp<=24999999 THENclk_out<='0';ELSEclk_out<='1';END IF;END IF;END PROCESS P1;P2:PROCESS(clear,clk_out)BEGINIF clear='1' THEN temp<="0000";ELSIF (clk_out'event and clk_out='1') THENIF temp="1001"THENtemp<="0000";ELSEtemp<=temp+1;END IF;END IF;END PROCESS P2;q<=temp;PROCESS(temp)BEGINCASE temp ISWHEN "0000"=>LED7S<="0111111";--X"3F"->0WHEN "0001"=>LED7S<="0000110";--X"06"->1WHEN "0010"=>LED7S<="1011011";--X"5B"->2WHEN "0011"=>LED7S<="1001111";--X"4F"->3WHEN "0100"=>LED7S<="1100110";--X"66"->4WHEN "0101"=>LED7S<="1101101";--X"6D"->5WHEN "0110"=>LED7S<="1111101";--X"7D"->6WHEN "0111"=>LED7S<="0000111";--X"07"->7WHEN "1000"=>LED7S<="1111111";--X"7F"->8WHEN "1001"=>LED7S<="1101111";--X"6F"->9WHEN OTHERS=>LED7S<="0000000";--X"6F"->null END CASE;cat<="111110";END PROCESS;END;其管脚设置:异步复位8421码十进制计数器:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY BCDcoder ISPORT(cp,clear:IN STD_LOGIC;q:OUT STD_LOGIC_VECTOR(3 DOWNTO 0));END BCDcoder;ARCHITECTURE a OF BCDcoder ISSIGNAL temp:STD_LOGIC_VECTOR(3 DOWNTO 0);SIGNAL tmp: INTEGER RANGE 0 TO 49999999; SIGNAL clk_out: STD_LOGIC;BEGINP1:PROCESS(cp)BEGINIF cp'event AND cp='1' THENIF tmp=49999999 THENtmp<=0;ELSEtmp<=tmp+1;END IF;IF tmp<=24999999 THENclk_out<='0';ELSEclk_out<='1';END IF;END IF;END PROCESS P1;P2:PROCESS(clear,clk_out)BEGINIF clear='1' THEN temp<="0000";ELSIF (clk_out'event and clk_out='1') THENIF temp="1001"THENtemp<="0000";ELSEtemp<=temp+1;END IF;END IF;END PROCESS P2;q<=temp;END a;分频器(上图为分频结果)library ieee;use ieee.std_logic_1164.all;USE IEEE.STD_LOGIC_UNSIGNED.ALL;entity fenpin28 isport(clk:in std_logic;fenpin28:out std_logic);end;architecture one of fenpin28 issignal cnt:std_logic_vector(2 downto 0);signal clk_temp:std_logic;constant m:integer:=5;beginprocess(clk)beginif clk'event and clk='1' thenif cnt=m thenclk_temp<=not clk_temp;cnt<="000";elsecnt<=cnt+1;end if;end if;end process;fenpin28<=clk_temp;end;计数器:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY jishu28 ISPORT(cp,clear:IN STD_LOGIC;q:OUT STD_LOGIC_VECTOR(3 DOWNTO 0));END jishu28;ARCHITECTURE a OF jishu28 ISSIGNAL temp:STD_LOGIC_VECTOR(3 DOWNTO 0);SIGNAL tmp: INTEGER RANGE 0 TO 49999999; SIGNAL clk_out: STD_LOGIC;BEGINP1:PROCESS(cp)BEGINIF cp'event AND cp='1' THENIF tmp=49999999 THENtmp<=0;ELSEtmp<=tmp+1;END IF;IF tmp<=24999999 THENclk_out<='0';ELSEclk_out<='1';END IF;END IF;END PROCESS P1;P2:PROCESS(clear,clk_out)BEGINIF clear='1' THEN temp<="0000";ELSIF (clk_out'event and clk_out='1') THENIF temp="1001"THENtemp<="0000";ELSEtemp<=temp+1;END IF;END IF;END PROCESS P2;q<=temp;END a;数码管:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY shuma28 ISPORT(A:IN STD_LOGIC_VECTOR(3 DOWNTO 0);LED7S:OUT STD_LOGIC_VECTOR(6 DOWNTO 0);cat:OUT STD_LOGIC_VECTOR(5 DOWNTO 0));END;ARCHITECTURE ONE OF shuma28 ISBEGINPROCESS(A)BEGINCASE A ISWHEN "0000"=>LED7S<="0111111";--X"3F"->0WHEN "0001"=>LED7S<="0000110";--X"06"->1WHEN "0010"=>LED7S<="1011011";--X"5B"->2WHEN "0011"=>LED7S<="1001111";--X"4F"->3WHEN "0100"=>LED7S<="1100110";--X"66"->4WHEN "0101"=>LED7S<="1101101";--X"6D"->5WHEN "0110"=>LED7S<="1111101";--X"7D"->6WHEN "0111"=>LED7S<="0000111";--X"07"->7WHEN "1000"=>LED7S<="1111111";--X"7F"->8WHEN "1001"=>LED7S<="1101111";--X"6F"->9WHEN OTHERS=>LED7S<="0000000";--X"6F"->null END CASE;cat<="111110";END PROCESS;END;一个有很多vhdl代码的网址:/download/shaoqso111/1901736。
静态数字显示:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY staticdigital ISPORT (clk,rst: IN STD_LOGIC; --rst复位,高有效figures: OUT STD_LOGIC_VECTOR(6 DOWNTO 0); --七段数码管cats: OUT STD_LOGIC_VECTOR(5 DOWNTO 0)); --控制端END staticdigital;ARCHITECTURE digital OF staticdigital ISSIGNAL address: STD_LOGIC_VECTOR(2 DOWNTO 0); --记录控制端信号中‘0’的位--置,亦即当前数码管的位置SIGNAL colti: INTEGER RANGE 1 TO 2500; --外部时钟,控制一次状态显示的时间BEGINPROCESS(clk,rst)BEGINIF(rst = '1') THEN --复位端有效address <= "111";ELSIF(clk'EVENT AND clk = '1') THEN --等待时钟上升沿的到来colti <= colti + 1;IF(address = "101") THENIF (colti = 2500) THEN --但是要考虑外部时钟是否停留了足够的时间address <= "000";END IF;ELSEIF (colti = 2500) THENaddress <= address + 1;END IF;END IF;END IF;END PROCESS;PROCESS(address)--一个结构体可以有多个进程,--进程之间是并行运行的。
library ieee;use ieee.std_logic_1164.all;entity traffic_control isport( clk,reset:in std_logic;light_h,light_v:out std_logic_vector(2 downto 0); count_vh,count_vl,count_hh,count_hl:out std_logic_vector(3 downto 0)); end entity traffic_control;architecture behave of traffic_control issignal temp,temp_h,temp_v:integer range 0 to 100:=0;beginP1:process(reset,clk)beginif(reset='1') thentemp<=0;elsif(reset='0') thenif(clk'event and clk='1') thenif(temp=100) thentemp<=0;else temp<=temp+1;end if;end if;end if;end process p1;p2:process(reset,clk,temp)variable y_h,y_v,x_h,x_v:integer range 0 to 9;beginif(temp<55) thentemp_h<=55-temp;temp_v<=60-temp;light_h<="100";light_v<="001";elsif(temp>=55 and temp<60)thentemp_h<=60-temp;temp_v<=60-temp;light_h<="010";light_v<="001";elsif(temp>=60 and temp<95)thentemp_h<=100-temp;temp_v<=95-temp;light_h<="001";light_v<="100";elsif(temp>=96) thentemp_h<=100-temp;temp_v<=100-temp;light_h<="100";light_v<="010";end if;y_h:=temp_h/10;x_h:=temp_h mod 10;y_v:=temp_v/10;x_v:=temp_v mod 10;c1:case y_h iswhen 0 => count_hh<="0000";when 1 => count_hh<="0001";when 2 => count_hh<="0010";when 3 => count_hh<="0011";when 4 => count_hh<="0100";when 5 => count_hh<="0101";when 6 => count_hh<="0110";when others => count_hh<="0000";end case c1;c2:case x_h iswhen 0 => count_hl<="0000";when 1 => count_hl<="0001";when 2 => count_hl<="0010";when 3 => count_hl<="0011";when 4 => count_hl<="0100";when 5 => count_hl<="0101";when 6 => count_hl<="0110";when 7 => count_hl<="0111";when 8 => count_hl<="1000";when 9 => count_hl<="1001";when others => count_hl<="0000"; end case c2;c3:case y_v iswhen 0 => count_vh<="0000";when 1 => count_vh<="0001";when 2 => count_vh<="0010";when 3 => count_vh<="0011";when 4 => count_vh<="0100";when 5 => count_vh<="0101";when 6 => count_vh<="0110";when others => count_vh<="0000"; end case c3;c4:case x_v iswhen 0 => count_vl<="0000";when 1 => count_vl<="0001";when 2 => count_vl<="0010";when 3 => count_vl<="0011";when 4 => count_vl<="0100";when 5 => count_vl<="0101";when 6 => count_vl<="0110";when 7 => count_vl<="0111";when 8 => count_vl<="1000";when 9 => count_vl<="1001";when others => count_vl<="0000"; end case c4;end process p2;end architecture behave;。
1、设计一个具有进位输入和进位输出的8位行波进位加法器。
其引脚名称如下表所示。
library ieee;use ieee.std_logic_1164.all;entity adder8 isport( a,b : instd_logic_vector(7 downto 0);Cin :in std_logic;cout : out std_logic;s :outstd_logic_vector(7 downto 0));end entity;architecture rtl of adder8 issignal temp:std_logic_vector(7 downto 0);begins(0)<=a(0) xor b(0) xor cin;temp(0)<=((a(0) and b(0) ) or (a(0) and cin) or (b(0) and cin)) ;s(1) <=a(1) xor b(1) xor temp(0); temp(1) <=((a(1) and b(1) ) or (a(1) and temp(0)) or (b(1) and temp(0)));s(2) <=a(2) xor b(2) xor temp(1); temp(2) <=((a(2) and b(2) ) or (a(2) and temp(1)) or (b(2) and temp(1))) ;s(3) <=a(3) xor b(3) xor temp(2); temp(3)<=((a(3) and b(3) ) or (a(3) and temp(2)) or (b(3) and temp(2)) );s(4)<=a(4) xor b(4) xor temp(3);temp(4)<=((a(4) and b(4) ) or (a(4) and temp(3)) or (b(4) and temp(3))) ;s(5) <=a(5) xor b(5) xor temp(4); temp(5) <=((a(5) and b(5) ) or (a(5) and temp(4)) or (b(5) and temp(4)));s(6) <=a(6) xor b(6) xor temp(5); temp(6) <=((a(6) and b(6) ) or (a(6) and temp(5)) or (b(6) and temp(5))) ;s(7) <=a(7) xor b(7) xor temp(6); temp(7)<=((a(7) and b(7) ) or (a(7) and temp(6)) or (b(7) and temp(6)) );cout<=temp(7);end rtl;2、设计一个具有同步清零和并行输出功能的10进制加法计数器。
八、源代码及注释--电子节拍器--分频器组模块dividers源代码--分频器组模块通过对50MHz的外部时钟进行分频,--产生其他模块所需的各种时钟频率并输出,--有60Hz、5000Hz、3000Hz、1500Hz、1000Hz,分别用5个进程实现。
library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity dividers is --定义实体dividersport(clk : in std_logic; --外部时钟输入clk_out60: out std_logic; --60Hz频率输出clk_out5000 : out std_logic; --5000Hz频率输出clk_out3000 : out std_logic; --3000Hz频率输出clk_out1500 : out std_logic; --1500Hz频率输出clk_out1000 : out std_logic); --1000Hz频率输出end dividers;architecture div of dividers issignal tmp5000 : integer range 0 to 4999; --分频得5000hz 的计数器大小signal tmp3000 : integer range 0 to 8332; --3000hz 3x10^9=50x10^6x60 use 3000hz signal tmp1500 : integer range 0 to 16665; --1500hzsignal tmp1000 : integer range 0 to 49999; --1000hzsignal tmp60 : integer range 0 to 416666; --60hzsignal clktmp5000 : std_logic; --分频得5000hz 的时钟信号signal clktmp3000 : std_logic;signal clktmp1500 : std_logic;signal clktmp1000 : std_logic;signal clktmp60 : std_logic;beginp5000: process(clk) --分频得5000hz 的分频器进程beginif clk'event and clk='1' then --对50MHz进行10000分频得5000hz 的频率if tmp5000=4999 then --达到5000hz 的计数器大小时归零tmp5000<=0;clktmp5000<=not clktmp5000; --5000hz 的时钟信号翻转一次elsetmp5000<=tmp5000+1; ----未达到5000hz 的计数器模值时计数器加一end if;end if;end process p5000; --分频得5000hz 的分频器进程结束p1500: process(clk) --以下各分频器原理同上beginif clk'event and clk='1' thenif tmp1500=16665 thentmp1500<=0;clktmp1500<=not clktmp1500;elsetmp1500<=tmp1500+1;end if;end if;end process p1500;p1000: process(clk)beginif clk'event and clk='1' thenif tmp1000=49999 thentmp1000<=0;clktmp1000<=not clktmp1000;elsetmp1000<=tmp1000+1;end if;end if;end process p1000;p3000: process(clk)beginif clk'event and clk='1' thenif tmp3000=8332 thentmp3000<=0;clktmp3000<=not clktmp3000;elsetmp3000<=tmp3000+1;end if;end if;end process p3000;p60: process(clk)beginif clk'event and clk='1' thenif tmp60=416666 thentmp60<=0;clktmp60<=not clktmp60;elsetmp60<=tmp60+1;end if;end if;end process p60;clk_out5000 <= clktmp5000; --输出分频得到的5000hz 频率clk_out3000 <= clktmp3000;clk_out1500 <= clktmp1500;clk_out1000 <= clktmp1000;clk_out60 <= clktmp60;end div; ———————————————————————————————————————--电子节拍器--速度设置模块set_speed源代码--速度设置模块实现速度在40至120内连续可调library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;entity set_speed isport(clk60 : in std_logic; --分频器60hz时钟输入vadd : in std_logic; --加按键输入vdec : in std_logic; --减按键输入clear : in std_logic; --置位键输入beat_v : out std_logic_vector(6 downto 0); --速度输出v_out0 : out std_logic_vector(3 downto 0); --速度个位二进制输出(对应0号数码管)v_out1 : out std_logic_vector(3 downto 0); --速度十位二进制输出(对应1号数码管)v_out2 : out std_logic_vector(3 downto 0)); --速度百位二进制输出(对应2号数码管)end set_speed;architecture set of set_speed issignal cn : integer range 40 to 120; --速度记录信号signal change_a : std_logic; --加按键输入按下记录信号signal change_d : std_logic; --减按键输入按下记录信号signal count_add : integer:=0; --加按键输入按下时长记录信号signal count_dec : integer:=0; --减按键输入按下时长记录信号signal a10 : integer range 0 to 99; --速度减去百位的数的记录信号signal a0 : integer range 0 to 9; --速度个位记录信号signal a1 : integer range 0 to 9; --速度十位记录信号signal a2 : integer range 0 to 1; --速度百位记录信号beginprocess(clk60,vadd,vdec)beginif clk60'event and clk60='1' thenif (vadd='1') then --若加键按下change_a<='1'; --加按键输入按下记录信号置1count_add<=count_add+1; --加按键输入按下时长记录信号加1if (count_add>60 and cn<=115) then --加按键输入按下时长记录信号大于60--即加按键按下时长大于1s(长按)--若速度小于115cn<=cn+5; --则速度连加5count_add<=count_add-60; --同时加按键输入按下时长记录信号减60 elsif (count_add>60 and cn>115) then --若速度大于115cn<=120; --速度只能增加到设置的上限120count_add<=0; --加按键输入按下时长记录信号置0change_a<='0'; --加按键输入按下记录信号置0end if;elsif (vdec='1') then --原理同加按键change_d<='1';count_dec<=count_dec+1;if (count_dec>60 and cn>=45) thencn<=cn-5;count_dec<=count_dec-60;elsif (count_dec>60 and cn<45) thencn<=40;count_dec<=0;change_d<='0';end if;elsif(vadd='0' and vdec='0') then --检测到无键按下时if (change_a='1' and count_add<60 and cn<120 ) then --若加键按下--且加按键输入按下时长记录信号小于60(短按)--且速度小于120cn<=cn+1; --则速度加1count_add<=0;change_a<='0';elsif (change_a='1' and count_add>=60 ) then --若加键长按count_add<=0;change_a<='0';if (cn<=115 ) then --原理同上cn<=cn+5;elsif (cn>115 ) thencn<=120;end if;elsif (change_d='1' and count_dec<60 and cn>40 ) then --原理同加按键cn<=cn-1;count_dec<=0;change_d<='0';elsif (change_d='1' and count_dec>=60 ) thencount_dec<=0;change_d<='0';if (cn>=45 ) thencn<=cn-5;elsif (cn<45 ) thencn<=40;end if;end if;end if;if clear ='1' then --实现置位功能cn<=80;count_add<=0;count_dec<=0;end if;if cn>=100 thena2<=1;a10<=cn-100;elsea2<=0;a10<=cn;end if;a1<= a10/10;a0<= a10-a1*10;end if;end process;v_out2<=conv_std_logic_vector(a2,4); --将速度的百位(十进制)转换为二进制后输出v_out1<=conv_std_logic_vector(a1,4);v_out0<=conv_std_logic_vector(a0,4);beat_v<=conv_std_logic_vector(cn,7); --将速度(十进制)转换为二进制后输出end set;--电子节拍器--节拍型选择模块set_rhythm源代码--节拍型选择模块可设置的节拍有1/4、2/4、3/4、4/4、3/8、6/8可选,--通过sel 按键选择,送到2个数码管显示,并把选好的节拍型输出到相关模块。
VHDL代码[例1.4.26] 2输入与门的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL; ENTITY and2 ISPORT(a, b : IN STD_LOGIC;y: OUT STD_LOGIC);END and2; ARCHITECTURE one OF and2 IS BEGINy<= a and b;END one;[例2.5.1] 2输入与非门的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL; ENTITY nand2 ISPORT(a, b : IN STD_LOGIC;y: OUT STD_LOGIC);END nand2; ARCHITECTURE one OF nand2 IS BEGINy<= a nand b;END one;[例2.5.2] 2输入或门的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL; ENTITY or2 ISPORT(a, b : IN STD_LOGIC;y: OUT STD_LOGIC);END or2;ARCHITECTURE one OF or2 IS BEGINy<= a or b;END one;[例2.5.3]非门的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL; ENTITY hnot ISPORT(a : IN STD_LOGIC;y: OUT STD_LOGIC);END hnot; ARCHITECTURE one OF hnot IS BEGINy<= not a;END one;[例2.5.4] 2输入异或门的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL; ENTITY xor2 ISPORT(a, b : IN STD_LOGIC;y: OUT STD_LOGIC);END xor2; ARCHITECTURE one OF xor2 IS BEGINy<= a xor b;END one;[例3.8.1] 3线-8线译码器的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;ENTITY decoder38 ISPORT(a : IN STD_LOGIC_VECTOR(2 DOWNTO 0); y: OUT STD_LOGIC_VECTOR(7 DOWNTO 0)); END decoder38;ARCHITECTURE one OF decoder38 ISBEGINPROCESS (a)BEGINCASE a ISWHEN "000" => y<= "00000001";WHEN "001" => y<= "00000010";WHEN "010" => y<= "00000100";WHEN "011" => y<= "00001000";WHEN "100" => y<= "00010000";WHEN "101" => y<= "00100000";WHEN "110" => y<= "01000000";WHEN "111" => y<= "10000000";WHEN OTHERS =>null ;END CASE;END PROCESS;END one;[例3.8.2] 8线-3线优先编码器的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;ENTITY encoder83 ISPORT( d : IN STD_LOGIC_VECTOR(7 DOWNTO 0); encode: OUT STD_LOGIC_VECTOR(2 DOWNTO 0)); END encoder83;ARCHITECTURE one OF encoder83 ISBEGINencode <= "111" when d(7) = '1' else "110" when d(6) = '1' else"101" when d(5) = '1' else"100" when d(4) = '1' else"011" when d(3) = '1' else"010" when d(2) = '1' else"001" when d(1) = '1' else"000" when d(0) = '1' ;END one;[例3.8.3] 4选1数据选择器的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;ENTITY mux41 isPORT (a,b,c,d : IN STD_LOGIC;s : IN STD_LOGIC_VECTOR(1 DOWNTO 0);z : OUT STD_LOGIC);END mux41;ARCHITECTURE one OF mux41 ISBEGINPROCESS (s ,a,b,c,d)BEGINCASE s ISWHEN "00" => z<= a;WHEN "01" => z<= b;WHEN "10" => z<= c;WHEN "11" => z<= d;WHEN OTHERS =>z<= 'x';END CASE;END PROCESS;END one;[例4.8.1] 同步复位D触发器的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL; ENTITY d_ff isPORT (d,clk,reset : IN STD_LOGIC;q : OUT STD_LOGIC);END d_ff;ARCHITECTURE one OF d_ff IS BEGINPROCESS (clk)BEGINIF clk'EVENT AND clk='1' THENIF reset='1' THENQ<='0';ELSE q<=d;END IF;END IF;END PROCESS;END one;[例4.8.2] 边沿JK 触发器的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL; ENTITY jk_ff isPORT (j,k,clk : IN STD_LOGIC;q, qn : OUT STD_LOGIC);END jk_ff;ARCHITECTURE one OF jk_ff IS SIGNAL q_s : STD_LOGIC; BEGINPROCESS (j,k,clk)BEGINIF clk'EVENT AND clk='1' THENIF J='0' AND k='0' THENq_s<= q_s;ELSIF J='0' AND k='1' THENq_s<='0';ELSIF J='1' AND k='0' THENq_s<='1';ELSIF J='1' AND k='1' THENq_s<=NOT q_s;END IF;END IF;END PROCESS;q<=q_s;qn<=not q_s;END one;[例5.6.1] 十进制计数器的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY count10 isPORT (cp : IN STD_LOGIC;q : OUT STD_LOGIC_VECTOR(3 DOWNTO 0) ); END count10;ARCHITECTURE one OF count10 ISSIGNAL count :STD_LOGIC_VECTOR(3 DOWNTO 0) ; BEGINPROCESS (cp)BEGINIF cp'EVENT AND cp='1' THENIF count <="1001" THENcount <="0000";ELSE count <= count +1;END IF;END IF;END PROCESS;q<= count;END one;[例5.6.2] 4位基本寄存器的VHDL描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;ENTITY registerb isPORT (cp,reset : IN STD_LOGIC;data : IN STD_LOGIC_VECTOR(3 DOWNTO 0);q: OUT STD_LOGIC_VECTOR(3 DOWNTO 0) ); END registerb;ARCHITECTURE one OF registerb ISBEGINPROCESS (cp)BEGINIF cp'EVENT AND cp='1' THEN IF reset='1' THENq<="0000";ELSEq<= data;END IF;END IF;END PROCESS;END one;。
序列检测器VHDL程序代码序列检测器图书馆;useieee.std_logic_1164.all;useieee.std_logic_unsigned.all;useieee.std_logic_ar ith.all;entitydetect110isport(clk,d_in:instd_logic;en:instd_logic;d_out:outstd_logic);endentity;架构行为检测110IStypestateis(s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11);signaln:state;signalp:state;beginprocess(clk)begin如果clk'event和clk='1'thenn<=p;endif;终末过程;过程(d_in,clk)开始如果(en='1')然后caseniswhens0=>如果(d_in='1')然后p<=s1;elsep<=s0;endif;d_uuout<='0';当1=>if(d_in='1')thenp<=s2;elsep<=s0;endif;d_out<='0';whens2=>if(d_in='1')thenp<=s2;elsep<=s3;endif;d_uuout<='0';whens3=>if(d_in='1')thenp<=s0;elsep<=s4;endif;d_uuout<='0';当4=>if(d_in='1')时,则P<=s5;elsep<=s0;endif;d_uuout<='0';当5=>if(d_in='1')时,则P<=s6;elsep<=s0;endif;d_uuout<='0';当6=>if(d_in='1')时,则p<=s7;elsep<=s3;endif;d_uuout<='0';当7=>if(d_in='0')时,则p<=s3;elsep<=s8;endif;d_out<='0';whens8=>if(d_in='1')thenp<=s2;elsep<=s9;endif;d_out<='0';whens9=>如果(d_in='1'),那么p<=s0;elsep<=s10;endif;d_uuout<='0';当10=>如果(d_in='1')那么p<=s5;elsep<=s11;endif;d_out<='0';whens11=>if(d_in='0')thenp<=s0;elsep<=s0;d_uuout<='1';endif;Wheothers=>null;尾声;elsed_uuuout<='0';endif;终末过程;恩德贝哈夫;交通灯libraryieee;使用IEEE。
1.三与门library ieee;use ieee.std_logic_1164.all;entity yumen isport(a,b,c : in std_logic;f : out std_logic);end yumen;architecture and3_1 of yumen isbeginf<=a and b and c;end architecture and3_1;2.三八译码器library ieee;use ieee.std_logic_1164.all;entity jg isport(a,b,c,g1,g2a,g2b:in std_logic;y:out std_logic_vector(7 downto 0));end entity jg;architecture rt1 of jg issignal indata:std_logic_vector(2 downto 0); beginindata<=c&b&a;process(indata,g1,g2a,g2b)isbeginif(g1='1' and g2a='0' and g2b='0')then case indata iswhen"000"=>y<="11111110"; when"001"=>y<="11111101"; when"010"=>y<="11111011"; when"011"=>y<="11110111"; when"100"=>y<="11101111"; when"101"=>y<="11011111"; when"110"=>y<="10111111"; when"111"=>y<="01111111";when others=>y<="xxxxxxxx";end case;elsey<="11111111";end if;end process;end rt1; 3.同步复位/置位、下降沿触发的d触发器ibrary ieee;use ieee.std_logic_1164.all;entity adff isport(clk,d,r,s:in std_logic;q:out std_logic);end adff;architecture rtl of adff issignal q_temp,qb_temp:std_logic;beginprocess(clk,r,s)beginif(clk'event and clk='0')thenif(r='0' and s='1')thenq_temp<='1';if(r='1' and s='0')thenq_temp<='0';elseq_temp<=d;end if;end if;end if;end process;q<=q_temp;end rtl;4.异步复位/置位、上升沿触发的d发器ibrary ieee;use ieee.std_logic_1164.all;entity adff isport(clk,d,r,s:in std_logic;q:out std_logic);end adff;architecture rtl of adff issignal q_temp,qb_temp:std_logic;beginprocess(clk,r,s)beginif(r='0' and s='1')thenq_temp<='1';elsif(r='1' and s='0')thenq_temp<='0';elsif(clk'event and clk='1')thenq_temp<=d;end if;end process;q<=q_temp;end rtl;5.四分频器ibrary ieee;use ieee.std_logic_1164.all;entity one isport( clk1:in std_logic;clk4:out std_logic);end one;architecture one1 of one issignal data1:integer range 0 to 10;signal q1:std_logic;beginprocess(clk1)beginif rising_edge(clk1) thenif(data1=1) thendata1<=0;q1<=not q1;elsedata1<=data1+1;end if;end if;clk4<=q1;end process;end architecture one1;6.四选一library ieee;use ieee.std_logic_1164.all;entity mux4 isport(input:in std_logic_vector(3 downto 0); a,b:in std_logic;y : out std_logic);end mux4 ;architecture rtl of mux4 issignal sel:std_logic_vector(1 downto 0); beginsel<=b & a;process (input,sel)beginif (sel="00") theny <= input(0);elsif (sel="01") theny <= input(1);elsif (sel="10") theny <= input(2);elsey <= input(3);end if;end process;end rtl; 7.五分频器use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;use ieee.std_logic_arith.all;entity fenpin5 isport (rst,clkin :in std_logic;clkout:out std_logic);end fenpin5;architecture rtl of fenpin5 issignal count1,count2: std_logic_vector(7 downto 0);signal tmp,tmp1,tmp2: std_logic;begintmp<=tmp1 and tmp2;clkout<=tmp xor tmp1;process(clkin,rst)beginif rst ='1'thencount1 <= "00000000";tmp1<= '0';elsif clkin'event and clkin='1' thenif count1 = "00000100" thencount1 <= "00000000";elsecount1 <= count1 + 1;if count1 < "00000010" thentmp1<= '0';elsetmp1<= '1';end if;end if;end if;end process;end rtl;8.moore状态机library ieee;use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity moore isport (rst,clk,x:in std_logic; op:out std_logic);end moore;architecture a of moore is type state is (s0,s1,s2,s3); signal st: state;beginstate_comp: process(rst,clk) beginif rst='1' thenst <= s0;elsif rising_edge(clk) then case st iswhen s0 =>if x = '0' thenst <= s0;elsest <= s1;end if;op <= '1';when s1 =>if x = '0' thenst <= s3;elsest <= s2;end if;op <= '1';when s2=>if x = '0' thenst <= s2;elsest <= s3;end if;op <= '1';when s3=>if x = '0' thenst <= s3;elsest <= s0;end if;op <= '0';end case;end if; 9.mealy状态机library ieee;use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity mealy isport (rst,clk,x:in std_logic; op:out std_logic);end mealy;architecture a of mealy istype state is (s0,s1,s2,s3); signal st : state;beginstate_comp: process(rst,clk) beginif rst='1' thenst <= s0;elsif rising_edge(clk) then case st iswhen s0 =>if x = '0' thenst <= s0; op <= '0';elsest <= s1; op <= '1';end if;when s1 =>if x = '0' thenst <= s3; op <= '1';elsest <= s2; op <= '1';end if;when s2 =>if x = '0' thenst <= s2; op <= '0';elsest <= s3; op <= '1';end if;when s3 =>if x = '0' thenst <= s3; op <= '0';elsest <= s0; op <= '0';end if;end case;end if;end process state_comp;end a10.全加器library ieee;use ieee.std_logic_1164.all;entity full_adder isport (a,b,cin:in std_logic;s,co:out std_logic);end full_adder;architecture full1 of full_adder issignal tmp1,tmp2,tmp3:std_logic;begintmp1 <= a xor b;tmp2 <= a and b;tmp3 <= tmp1 and cin;s <= tmp1 xor cin;co <= tmp2 or tmp3;end full1;11.同步12进制计数器library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity count12en isport (clk,clr, en :in std_logic;qa,qb,qc,qd:out std_logic);end count12en;architecture rtl of count12en issignal count_4:std_logic_vector(3 downto 0); beginqa<=count_4(0);qb<=count_4(1);qc<=count_4(2);qd<=count_4(3);process (clr,clk)beginif(clr='1') thencount_4<="0000";elsif (clk'event and clk ='1') thenif(en='1') thenif (count_4="1011") thencount_4<="0000";elsecount_4<=count_4+'1';end if;end if;end if;end process;end rtl; 12.优先编码器library ieee;use ieee.std_logic_1164.all;entity priority_encoder isport(input:in std_logic_vector(7 downto 0); y : out std_logic_vector(2 downto 0));end priority_encoder;architecture rtl of priority_encoder is beginp1: process (input)beginif ( input(0) ='0') theny <= "111";elsif (input(1) ='0') theny <= "110";elsif (input(2) ='0') theny <= "101";elsif (input(3) ='0') theny <= "100";elsif (input(4) ='0') theny <= "011";elsif (input(5) ='0') theny <= "010";elsif (input(6) ='0') theny <= "001";elsey <= "000";end if;end process p1;end rtl;。