实验内容: 1 多路选择器(习题2.1) 2 ROM(习题3.4) 3 简易加法器(习题3.5) 4 通用译码器(习题4.4) 5 第五章习题5.1、5.5、5.6、5.7、5.8 | ||
实验要求: 1.依次完成各电路功能的VHDL代码编写 2.完成相应电路仿真,并对仿真结果截图,截图中要求尽可能多的体现不同输入信号对应的输入结果 3.完成实验报告,并按时提交至Blackboard,实验报告见实验报告模板,要求按模板各项内容完成。 4.特别提示:实验报告按模板内容逐项填写,要求有完整的VHDL代码、仿真测试文件(VHDL test bench)、仿真结果截图、仿真结果分析、实验结论(或对实验的总结、心得体会)等内容。 | ||
实验过程及内容: 2.1 多路选择器 多路选择器的顶层电路如图P2.1所示。根据真值表,如果输入l=“01”或者l=“10”,那么输出将等于对应的某一输入(c=a或c=b).然而如果输入l=“00”或者l=“11”,那么输出将分别为‘0’和‘Z’(高阻)。 (a)填写表格,完成下面的代码。 (b)是对你的解答给出相关的注释。 (c)将代码编译后进行仿真,验证其正确性。 实验完整VHDL代码: library IEEE; u IEEE.STD_LOGIC_1164.ALL; entity mux is Port ( a : in STD_LOGIC_VECTOR(7 DOWNTO 0); b : in STD_LOGIC_VECTOR(7 DOWNTO 0); l : in STD_LOGIC_VECTOR(1 DOWNTO 0); c : out STD_LOGIC_VECTOR(7 DOWNTO 0)); end mux; architecture example of mux is begin PROCESS (a,b,l) begin IF (l="00") THEN c <= "00000000"; ELSIF (l="01") THEN c <= a; ELSIF (l="10") THEN c <= b; ELSE c <= (OTHERS => 'U'); END IF; END PROCESS; end EXAMPLE; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY Test_Mux IS END Test_Mux; ARCHITECTURE behavior OF Test_Mux IS COMPONENT mux PORT( a : IN std_logic_vector(7 downto 0); b : IN std_logic_vector(7 downto 0); l : IN std_logic_vector(1 downto 0); c : OUT std_logic_vector(7 downto 0) ); END COMPONENT; --Inputs signal a : std_logic_vector(7 downto 0) := (others => '0'); signal b : std_logic_vector(7 downto 0) := (others => '0'); signal l : std_logic_vector(1 downto 0) := (others => '0'); --Outputs signal c : std_logic_vector(7 downto 0); -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: mux PORT MAP ( a => a, b => b, l => l, c => c ); -- Stimulus process stim_proc: process begin -- hold ret state for 100 ns. a<="10101010"; b<="11110000"; l <="00"; wait for 100 ns; l <="01"; wait for 100 ns; l <="10"; wait for 100 ns; l <="11"; wait for 100 ns; -- inrt stimulus here wait; end process; END; 仿真结果: 如图,当输入信号l为“00”时,输出信号c为“00000000”;当输入信号l为“01”时,输出信号c等于a即为“10101010”;当输入信号l为“10”时,输出信号c等于b即为“11110000”;当输入信号l为其他情况时,输出信号c等于自己设定的值,在此处即为“U”。 习题3.4 ROM 试用1*1维常数来实现只读存储器ROM(read-only memory),假设一个ROM由许多深度为8,位宽为4的块组成。提示:首先建立一个名为rom的数组,然后定义一个rom类型的信号来实现ROM,用常数值填充到ROM块中:CONSTANT my_rom:rom:=(values);。 实验完整VHDL代码: library IEEE; u IEEE.STD_LOGIC_1164.ALL; entity ROM is Port ( addr : in integer range 0 to 7; data : out STD_LOGIC_vector(3 downto 0)); end ROM; architecture Behavioral of ROM is TYPE ROM IS ARRAY (0 TO 7) OF STD_LOGIC_VECTOR(3 DOWNTO 0); CONSTANT my_rom:ROM:= ("0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111"); begin data <=my_rom(addr); end Behavioral; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY rom1 IS END rom1; ARCHITECTURE behavior OF rom1 IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT ROM PORT( addr : IN integer range 0 to 7 ; data : OUT std_logic_vector(3 downto 0) ); END COMPONENT; --Inputs signal addr : integer range 0 to 7; --Outputs signal data : std_logic_vector(3 downto 0); -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: ROM PORT MAP ( addr => addr, data => data ); -- Stimulus process stim_proc: process begin -- hold ret state for 100 ns. addr<=0; wait for 100 ns; addr<=2; wait for 100 ns; addr<=3; wait for 100 ns; addr<=5; wait for 100 ns; addr<=7; wait for 100 ns; addr<=1; wait for 100 ns; wait; end process; END; 仿真结果: 如图。当输入信号addr为“0”时,输出信号data为“0000”;当输入信号addr为“2”时,输出信号data为“0010”;当输入信号addr为“3”时,输出信号data为“0011”;当输入信号addr为“5”时,输出信号data为“0101”;当输入信号addr为“7”时,输出信号data为“0111”;当输入信号addr为“1”时,输出信号data为“0001”。 习题3.5 简易加法器 重新编写一段代码,实现例3.3所示的加法器,要求所有输入/输出信号的类型均为STD_LOGIC_VECTOR(提示:回顾3.8节所学的内容)。 实验完整VHDL代码: 西岐山library IEEE; u IEEE.STD_LOGIC_1164.ALL; u IEEE.STD_LOGIC_arith.all; u IEEE.STD_LOGIC_unsigned.all; entity adder1 is Port ( a : in STD_LOGIC_VECTOR (3 DOWNTO 0); b : in STD_LOGIC_VECTOR (3 DOWNTO 0); sum : out STD_LOGIC_VECTOR (4 DOWNTO 0)); end adder1; architecture Behavioral of adder1 is begin sum <= ('0'&a)+('0'&b); end Behavioral; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY adder2 IS END adder2; ARCHITECTURE behavior OF adder2 IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT adder1 PORT( a : IN std_logic_vector(3 downto 0); b : IN std_logic_vector(3 downto 0); sum : OUT std_logic_vector(4 downto 0) ); END COMPONENT; --Inputs signal a : std_logic_vector(3 downto 0) := (others => '0'); signal b : std_logic_vector(3 downto 0) := (others => '0'); --Outputs signal sum : std_logic_vector(4 downto 0); -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: adder1 PORT MAP ( a => a, b => b, sum => sum ); -- Stimulus process 社会主义现代化 stim_proc: process 虾的画法 begin -- hold ret state for 100 ns. a <="0000"; b <="0001"; wait for 100 ns; a <="0010"; b <="0011"; wait for 100 ns; a <="0100"; b <="0101"; wait for 100 ns; a <="0110"; b <="0111"; wait for 100 ns; a <="1000"; b <="1001"; wait for 100 ns; a <="1010"; b <="1011"; wait for 100 ns; a <="1100"; b <="1101"; wait for 100 ns; a <="1110"; b <="1111"; wait for 100 ns; wait; end process; END; 仿真结果: 如图。当输入信号a为“0000”,b为“0001”时,输出信号sum为“00001”;当输入信号a为“0010”,b为“0011”时,输出信号sum为“00101”;当输入信号a为“0100”,b为“0101”时,输出信号sum为“01001”;当输入信号a为“0110”,b为“0111”时,输出信号sum为“01101”;当输入信号a为“1000”,b为“1001”时,输出信号sum为“10001”;当输入信号a为“1010”,b为“1011”时,输出信号sum为“10101”;当输入信号a为“1100”,b为“1101”时,输出信号sum为“11001”;当输入信号a为“1110”,b为“1111”时,输出信号sum为“11101”。 习题4.4 通用译码器 下面这个习题和例4.1中的译码器电路有关。 (1)在例4.1给出的电路中,如果矢量的位宽发生变化,那么程序中的信号l(第7行)和(第8行)的位宽也要相应的改变。如果想要把例4.1中的设计修改为一个通用译码器。为此必须在ENTITY中使用GENERIC语句指定l的位宽(假设n=3),然后用n的函数来替代l和x的位宽上界。综合后,对电路进行仿真,验证其正确性。 实验完整VHDL代码: library IEEE; u IEEE.STD_LOGIC_1164.ALL; entity decoder1 is GENERIC (n: INTEGER :=3); Port ( ena : in STD_LOGIC; l : in STD_LOGIC_VECTOR(n-1 DOWNTO 0); x : out STD_LOGIC_VECTOR((2**n)-1 DOWNTO 0)); end decoder1; architecture Behavioral of decoder1 is begin PROCESS (ena, l) VARIABLE temp1: STD_LOGIC_VECTOR(x'HIGH DOWNTO 0); VARIABLE temp2: INTEGER RANGE 0 TO x'HIGH; BEGIN temp1 := (OTHERS => '1'); temp2 := 0; IF (ena= '1') THEN FOR i IN l'RANGE LOOP IF (l(i) ='1') THEN temp2 :=2*temp2+1; ELSE temp2 :=2*temp2; END IF; END LOOP; temp1(temp2) :='0'; END IF; x <= temp1; END PROCESS; END Behavioral; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY decoder2 IS END decoder2; ARCHITECTURE behavior OF decoder2 IS COMPONENT decoder1 Port ( ena : in STD_LOGIC; l : in STD_LOGIC_VECTOR(2 DOWNTO 0); x : out STD_LOGIC_VECTOR(7 DOWNTO 0)); END COMPONENT; --Inputs signal ena : std_logic := '0'; signal l : std_logic_vector(2 downto 0) := (others => '0'); --Outputs signal x : std_logic_vector(7 downto 0); -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: decoder1 PORT MAP ( ena => ena, l => l, x => x ); -- Stimulus process stim_proc: process begin -- hold ret state for 100 ns. ena<='1'; l<="010"; wait for 100 ns; ena<='1'; l<="100"; wait for 100 ns; ena<='1'; l<="110"; wait for 100 ns; ena<='1'; l<="100"; wait for 100 ns; ena<='1'; l<="101"; wait for 100 ns; ena<='1'; l<="111"; wait for 100 ns; ena<='1'; l<="101"; wait for 100 ns; -- inrt stimulus here wait; end process; END; 仿真结果: 如图。让使能端始终为“1”。当输入信号l为“010”时,输出信号x为“11111011”;当输入信号l为“100”时,输出信号x为“11101111”;当输入信号l为“110”时,输出信号x为“10111111白发多是什么原因”;当输入信号l为“100”时,输出信号x为“11101111”;当输入信号l为“101”时,输出信号x为“11011111”;当输入信号l为“111”时,输出信号x为“01111111”;当输入信号l为“101”时,输出信号x为“11011111”。 (2)在例4.1的设计中引入了一个二进制整数到整数的转换函数(第20行第26行)。如果把l声明为整数,就不需要使用这个转换函数。要求读者修改代码,将信号l声明为整数类型。当信号l的位宽用n来指定时,代码才是通用的。综合代码并进行仿真。 实验完整VHDL代码: library IEEE; u IEEE.STD_LOGIC_1164.ALL; entity decoder3 is GENERIC (n: INTEGER :=3); Port ( ena : in STD_LOGIC; l : in INTEGER RANGE 0 TO (2**n)-1; x : out STD_LOGIC_VECTOR((2**n)-1 DOWNTO 0)); end decoder3; architecture Behavioral of decoder3 is begin PROCESS (ena, l) VARIABLE temp1: STD_LOGIC_VECTOR(x'HIGH DOWNTO 0); BEGIN temp1 := (OTHERS => '1'); IF (ena= '1') THEN temp1(l) :='0'; END IF; x <= temp1; END PROCESS; END Behavioral; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY decorder4 IS END decorder4; ARCHITECTURE behavior OF decorder4 IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT decoder3 PORT( ena : IN std_logic; l : IN integer range 0 to 7; x : OUT std_logic_vector(7 downto 0) ); END COMPONENT; --Inputs signal ena : std_logic := '0'; signal l : integer:= 0; --Outputs signal x : std_logic_vector(7 downto 0); -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: decoder3 PORT MAP ( ena => ena, l => l, x => x ); -- Stimulus process stim_proc: process begin -- hold ret state for 100 ns. ena<='1'; l<=1; wait for 100 ns; ena<='1'; l<=0; wait for 100 ns; ena<='1'; l<=1; wait for 100 ns; ena<='1'; l<=0; wait for 100 ns; ena<='1'; l<=2; wait for 100 ns; ena<='1'; l<=1; wait for 100 ns; ena<='1'; l<=2; wait for 100 ns; -- inrt stimulus here wait; end process; END; 仿真结果: 如图。如图。让使能端始终为“1”。当输入信号l为“1”时,输出信号x为“11111101”;当输入信号l为“0”时,输出信号x为“11111110”;当输入信号l为“1”时,输出信号x为“11111101”;当输入信号l为“0”时,输出信号x为“11111110”;当输入信号l为“10”时,输出信号x为“11111011”;当输入信号l为“1”时,输出信号x为“11111101”;当输入信号l为“10”时,输出信号x为“11111011”。 习题5.1 通用多路复用器 在例5.1和例5.2给出的多路复用器中,输入矢量的个数和每个输入矢量矢量,m代表每个输入矢量的位宽。如图所示,电路有2n个输入(注意这里的n和m没有依赖关系)。试用GENERIC语句来指定n的值,并假设m=8.实现这个电路。 实验完整VHDL代码: library IEEE; u IEEE.STD_LOGIC_1164.ALL; entity mux1 is GENERIC (n: INTEGER :=2;m: INTEGER :=7); Port ( a : in STD_LOGIC_VECTOR(m DOWNTO 0); b : in STD_LOGIC_VECTOR(m DOWNTO 0); c : in STD_LOGIC_VECTOR(m DOWNTO 0); d : in STD_LOGIC_VECTOR(m DOWNTO 0); e : in STD_LOGIC_VECTOR(m DOWNTO 0); f : in STD_LOGIC_VECTOR(m DOWNTO 0); g : in STD_LOGIC_VECTOR(m DOWNTO 0); h : in STD_LOGIC_VECTOR(m DOWNTO 0); l : in STD_LOGIC_VECTOR(n DOWNTO 0); Y : out STD_LOGIC_VECTOR(m DOWNTO 0)); end mux1; architecture Behavioral of mux1 is begin Y <= a WHEN l="000" ELSE b WHEN l="001" ELSE c WHEN l="010" ELSE d WHEN l="011" ELSE e WHEN l="100" ELSE f WHEN l="101" ELSE g WHEN l="110" ELSE h;02 end architecture; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY mux2 IS END mux2; ARCHITECTURE behavior OF mux2 IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT mux1 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); e : IN std_logic_vector(7 downto 0); f : IN std_logic_vector(7 downto 0); g : IN std_logic_vector(7 downto 0); h : IN std_logic_vector(7 downto 0); l : IN std_logic_vector(2 downto 0); Y : OUT std_logic_vector(7 downto 0) ); END COMPONENT; --Inputs signal a : std_logic_vector(7 downto 0) := (others => '0'); signal b : std_logic_vector(7 downto 0) := (others => '0'); signal c : std_logic_vector(7 downto 0) := (others => '0'); signal d : std_logic_vector(7 downto 0) := (others => '0'); signal e : std_logic_vector(7 downto 0) := (others => '0'); signal f : std_logic_vector(7 downto 0) := (others => '0'); signal g : std_logic_vector(7 downto 0) := (others => '0'); signal h : std_logic_vector(7 downto 0) := (others => '0'); signal l : std_logic_vector(2 downto 0) := (others => '0'); --Outputs signal Y : std_logic_vector(7 downto 0); -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: mux1 PORT MAP ( a => a, b => b, c => c, d => d, e => e, f => f, g => g, h => h, l => l, Y => Y ); -- Stimulus process stim_proc: process begin -- hold ret state for 100 ns. l<="000"; a<="00000000"; b<="00110001"; c<="10101000"; d<="01010111"; e<="11110000"; f<="01110011"; g<="00110011"; h<="11111111"; wait for 100 ns; l<="001"; a<="00000000"; b<="00110001"; c<="10101000"; d<="01010111"; e<="11110000"; f<="01110011"; g<="00110011"; h<="11111111"; wait for 100 ns; l<="100"; a<="00000000"; b<="00110001"; c<="10101000"; d<="01010111"; e<="11110000"; f<="01110011"; g<="00110011"; h<="11111111"; wait for 100 ns; l<="010"; a<="00000000"; b<="00110001"; c<="10101000"; d<="01010111"; e<="11110000"; f<="01110011"; g<="00110011"; h<="11111111"; wait for 100 ns; l<="011"; a<="00000000"; b<="00110001"; c<="10101000"; d<="01010111"; e<="11110000"; f<="01110011"; g<="00110011"; h<="11111111"; wait for 100 ns; l<="111"; a<="00000000"; b<="00110001"; c<="10101000"; d<="01010111"; e<="11110000"; f<="01110011"; g<="00110011"; h<="11111111"; wait for 100 ns; l<="101"; a<="00000000"; b<="00110001"; c<="10101000"; d<="01010111"; e<="11110000"; f<="01110011"; g<="00110011"; h<="11111111"; wait for 100 ns; -- inrt stimulus here wait; end process; END; 仿真结果: 如图。假设输入的各种信号为a<="00000000",b<="00110001", c<="10101000",d<="01010111",e<="11110000",f<="01110011", g<="00110011",h<="11111111"。当输入信号l为“000”时,输出信号Y等于a信号等于“00000000”;当输入信号l为“001”时,输出信号Y等于b信号等于“00110001”;当输入信号l为“100”时,输出信号Y等于e信号等于“11110000”;当输入信号l为“010”时,输出信号Y等于c信号等于“10101000”;当输入信号l为“011”时,输出信号Y等于d信号等于“01010111”;当输入信号l为“111”时,输出信号Y等于h信号等于“11111111”;当输入信号l为“101”时,输出信号Y等于f信号等于“01110011”。 习题5.5 有符号数/无符号数加法器和减法器 与习题5.4相比,图P5.5所示的电路增加了一个两位的输入信号(l),这样电路就可以有选择的执行有符号数/无符号数加法运算或减法运算(见真值表)。试编写VHDL代码实现这个电路。 实验完整VHDL代码: library IEEE; u ieee.std_logic_1164.all; u ieee.std_logic_arith.all; u ieee.std_logic_unsigned.all; u ieee.std_logic_signed.all; entity adderminus is port(a,b:in unsigned(7 downto 0); l:in bit_vector(1 downto 0); sum:out std_logic_vector(8 downto 0)); end; architecture bhv of adderminus is signal temp1,temp2:unsigned (8 downto 0); signal temp3,temp4:signed(8 downto 0); --signal an,as,sn,ss:std_logic_vector(8 downto 0); signal a0,b0:signed (7 downto 0); signal cin0:std_logic_vector(7 downto 0); begin a0<=conv_signed(a,8); b0<=conv_signed(b,8); temp1<=conv_unsigned((a+b),9); temp2<=conv_unsigned((a-b),9); temp3<=conv_signed((a0+b0),9); temp4<=conv_signed((a0-b0),9); sum<=conv_std_logic_vector(temp1,9)when l="00" el conv_std_logic_vector(temp3,9)when l="01" el conv_std_logic_vector(temp2,9)when l="10" el conv_std_logic_vector(temp4,9); end; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; u ieee.std_logic_arith.all; u ieee.std_logic_unsigned.all; u ieee.std_logic_signed.all; ENTITY Test_adderminus IS END Test_adderminus; ARCHITECTURE behavior OF Test_adderminus IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT adderminus PORT( a : IN unsigned(7 downto 0); b : IN unsigned(7 downto 0); l : in bit_vector(1 downto 0); sum : out std_logic_vector(8 downto 0) ); END COMPONENT; --Inputs signal a : unsigned(7 downto 0); signal b : unsigned(7 downto 0); signal l : bit_vector(1 downto 0); --Outputs signal sum : std_logic_vector(8 downto 0); -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: adderminus PORT MAP ( a => a, b => b, l => l, sum => sum ); -- Stimulus process stim_proc: process begin -- hold ret state for 100 ns. a<="00100000"; b<="00001100"; l<="00"; wait for 100 ns; a<="00100100"; b<="01000110"; l<="01"; wait for 100 ns; a<="11001100"; b<="00110101"; l<="10"; wait for 100 ns; a<="00110010"; b<="00100000"; l<="11"; wait for 100 ns; -- inrt stimulus here wait; end process; END; 仿真结果: 如图。当输入信号a为“00100000”,输入信号b为“00001100”,输入信号l为“00”,输出信号sum为“000101100”;当输入信号a为“00100100”,输入信号b为“01000110”,输入信号l为“01”,输出信号sum为“001101010”;当输入信号a为“11001100”,输入信号b为“00110101”,输入信号l为“10”,输出信号sum为“010010111”;当输入信号a为“00110010”,输入信号b为“00100000”,输入信号l为“11”,输出信号sum为“000010010”。 习题5.6 二进制码-格雷码转化器 在数字系统中,我们用得最多的是二进制码。其最低位的权重是20,权重按其尾数以2的指数归律递增,最高位的权重是2n-1(n是位宽)。另一方面,格雷码的相邻码字具有最小汉明距离。也就是说,相邻码字只有一位不同。当n=4时,我们在表P5.6中列出了4位二进制码和格雷码的所有码字。试编写VHDL代码,实现从二进制码到格雷码的准换功能(位宽n为GENERIC参数,以增加电路的通用性)。 实验完整VHDL代码: library IEEE; u IEEE.STD_LOGIC_1164.ALL; entity grey is Port ( a : in STD_LOGIC_VECTOR(3 DOWNTO 0); c : out STD_LOGIC_VECTOR(3 DOWNTO 0)); end grey; architecture Behavioral of grey is begin c<="0000" WHEN a<="0000" ELSE "0001" WHEN a<="0001" ELSE "0011" WHEN a<="0010" ELSE "0010" WHEN a<="0011" ELSE "0110" WHEN a<="0100" ELSE "0111" WHEN a<="0101" ELSE "0101" WHEN a<="0110" ELSE "0100" WHEN a<="0111" ELSE 质量宣传标语 "1100" WHEN a<="1000" ELSE "1101" WHEN a<="1001" ELSE "1111" WHEN a<="1010" ELSE "1110" WHEN a<="1011" ELSE 准噶尔部 "1010" WHEN a<="1100" ELSE "1011" WHEN a<="1101" ELSE "1001" WHEN a<="1110" ELSE "1000"; end Behavioral; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY Test_grey IS END Test_grey; ARCHITECTURE behavior OF Test_grey IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT grey PORT( a : IN std_logic_vector(3 downto 0); c : OUT std_logic_vector(3 downto 0) ); END COMPONENT; --Inputs signal a : std_logic_vector(3 downto 0) := (others => '0'); --Outputs signal c : std_logic_vector(3 downto 0); -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: grey PORT MAP ( a => a, c => c ); -- Stimulus process stim_proc: process begin -- hold ret state for 100 ns. a<="0000"; wait for 100 ns; a<="0001"; wait for 100 ns; a<="0000"; wait for 100 ns; a<="1100"; wait for 100 ns; a<="0110"; wait for 100 ns; a<="1000"; wait for 100 ns; a<="0011"; wait for 100 ns; -- inrt stimulus here wait; end process; END; 仿真结果: 如图。当输入二进制数为“0000”时,输出格雷码为“0000”;当输入二进制数为“0001”时,输出格雷码为“0001”;当输入二进制数为“0000”时,输出格雷码为“0000”;当输入二进制数为“1100”时,输出格雷码为“1010”;当输入二进制数为“0110”时,输出格雷码为“0101”;当输入二进制数为“1000”时,输出格雷码为“1100”;当输入二进制数为“0011”时,输出格雷码为“0010”。 习题5.7 简易桶形移位寄存器 图P5.7给出了一种简易的桶形移位寄存器的电路图。在这个电路中,输出或者是输入矢量(8位)左移一位,或者是等于输入。当矢量左移时,最低位填充‘0’(见电路图的左下角),outp(0)=’0’;outp(i)=inp(i-1),1≤i≤7。当矢量不移位时,输出就等于输入。试用并发代码来实现这个电路。 实验完整VHDL代码: library IEEE; u IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_ARITH.ALL; entity shifter is Port ( inp : in bit_vector (7 downto 0); shift : in INTEGER RANGE 0 TO 1; outp : out bit_vector (7 downto 0)); end shifter; architecture Behavioral of shifter is begin outp <= inp WHEN shift = 0 ELSE inp sll 1; end Behavioral; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY Test_shifter IS END Test_shifter; ARCHITECTURE behavior OF Test_shifter IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT shifter PORT( inp : in bit_vector (7 downto 0); shift : in INTEGER RANGE 0 TO 1; outp : out bit_vector (7 downto 0) ); END COMPONENT; --Inputs signal inp : bit_vector(7 downto 0) := (others => '0'); signal shift : integer range 0 to 1 ; --Outputs signal outp : bit_vector(7 downto 0); -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: shifter PORT MAP ( inp => inp, shift => shift, outp => outp ); -- Stimulus process stim_proc: process begin -- hold ret state for 100 ns. inp<="01001101"; shift<=1; wait for 100 ns; inp<="01001101"; shift<=0; wait for 100 ns; inp<="01100001"; shift<=1; wait for 100 ns; inp<="01100001"; shift<=0; wait for 100 ns; -- inrt stimulus here wait; end process; END; 仿真结果: 如图。当输入信号inp为“01001101”,shift=1时(移位),输出信号outp为“10011010”;当输入信号inp为“01001101”,shift=0时(不移位),输出信号outp为“01001101”;当输入信号inp为“01100001”,shift=1时(移位),输出信号outp为“01100001”;当输入信号inp为“01100001”,shift=0时(不移位),输出信号outp为“01100001”。 习题5.8 比较器 试写VHDL代码实现对输入的两个8位矢量(a和b)进行比较操作的电路。选择信号l决定是对无符号数比较(l=’0’)还是对有符号数(l=’1’)比较。电路有3个输出信号x1,x2和x3,分别代表a>b,a=b和a<b(见图P5.8)。 实验完整VHDL代码: library IEEE; u IEEE.STD_LOGIC_1164.ALL; u IEEE.STD_LOGIC_ARITH.ALL; u IEEE.STD_LOGIC_SIGNED.ALL; entity comparator is Port ( a : in STD_LOGIC_VECTOR(7 DOWNTO 0); b : in STD_LOGIC_VECTOR(7 DOWNTO 0); l : in STD_LOGIC; x1 : out STD_LOGIC; x2 : out STD_LOGIC; x3 : out STD_LOGIC); end comparator; architecture Behavioral of comparator is signal temp: std_logic_vector (1 to 4); begin temp(1)<='1' when (('0'&a)-('0'&b)>0) el '0'; temp(2)<='1' when (('0'&a)-('0'&b)=0) el '0'; temp(3)<='1' when ((a(7)&a)-(b(7)&b)>0) el '0'; temp(4)<='1' when ((a(7)&a)-(b(7)&b)=0) el '0'; x1<=temp(1) when l='0' el temp(3); x2<=temp(2) when l='0' el temp(4); x3<=not(temp(1) or temp(2)) when l='0' el not(temp(3) or temp(4)); end; 仿真测试文件代码: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY Text_comparator IS END Text_comparator; ARCHITECTURE behavior OF Text_comparator IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT comparator PORT( a : IN std_logic_vector(7 downto 0); b : IN std_logic_vector(7 downto 0); l : IN std_logic; x1 : OUT std_logic; x2 : OUT std_logic; 求积函数 x3 : OUT std_logic ); END COMPONENT; --Inputs signal a : std_logic_vector(7 downto 0) := (others => '0'); signal b : std_logic_vector(7 downto 0) := (others => '0'); signal l : std_logic := '0'; --Outputs signal x1 : std_logic; signal x2 : std_logic; signal x3 : std_logic; -- No clocks detected in port list. Replace <clock> below with -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: comparator PORT MAP ( a => a, b => b, l => l, x1 => x1, x2 => x2, x3 => x3 ); -- Stimulus process stim_proc: process begin -- hold ret state for 100 ns. a<="01011001"; b<="01010101"; l<='0'; wait for 100 ns; a<="11011101"; b<="01011101"; l<='1'; wait for 100 ns; a<="01011000"; b<="01010000"; l<='0'; wait for 100 ns; a<="01000011"; b<="11000101"; l<='1'; wait for 100 ns; a<="11111001"; b<="00000000"; l<='0'; wait for 100 ns; a<="01010001"; b<="11010110"; l<='1'; wait for 100 ns; -- inrt stimulus here wait; end process; END; 仿真结果: 如图。当输入信号a为“01011001”,b为“01010101”,l为“0”(对无符号数比较),输出信号x1信号为“1”,x2信号为“0”,x3信号为“0”;当输入信号a为“11011101”,b为“01011101”,l为“1”(对有符号数比较),输出信号x1信号为“0”,x2信号为“0”,x3信号为“1”;当输入信号a为“01011000”,b为“01010000”,l为“0”(对无符号数比较),输出信号x1信号为“1”,x2信号为“0”,x3信号为“0”;当输入信号a为“01000011”,b为“11000101”,l为“1”(对有符号数比较),输出信号x1信号为“1”,x2信号为“0”,x3信号为“0”;当输入信号a为“11111001”,b为“00000000”,l为“0”(对无符号数比较),输出信号x1信号为“1”,x2信号为“0”,x3信号为“0”;当输入信号a为“01010001”,b为“11010110”,l为“1”(对有符号数比较),输出信号x1信号为“1”,x2信号为“0”,x3信号为“0”。 | ||
实验结论: 通过本次实验,我掌握了ISE的使用方法,并初步掌握了VHDL语言的编程思想和方法。希望能通过接下来的试验,掌握更多VHDL编程语言的方法。 | ||
指导教师批阅意见: 成绩评定: 指导教师签字: 年 月 日 | ||
备注: | ||
本文发布于:2023-05-23 03:32:14,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/fan/82/740106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |