当前位置:首页 > eda技术实用教程verilog答案
eda技术实用教程verilog答案
【篇一:eda技术实用教程课后答案---潘松,黄继业】
端有四个输入:s0、s1、s2、s3。当且
仅当s0=0时:y=a;s1=0时:y=b;s2=0时:y=c;s3=0时:y=d。 --解:4选1多路选择器vhdl程序设计。 library ieee; use ieee.std_logic_1164.all; entity mux41a is
port( a,b,c,d : in std_logic;s0,s1,s2,s3 : in std_logic; y : out std_logic); end entity mux41a; architecture one of mux41a is
signal s0_3 : std_logic_vector(3 downto 0); begin s0_3=s0s1s2s3;
y=a when s0_3=0111 else b when s0_3=1011 else c when s0_3=1101 else d when s0_3=1110 else z; end architecture one;
3-4 给出1位全减器的vhdl描述;最终实现8位全减器。要求: 1)首先设计1位半减器,然后用例化语句将它们连接起来,图4-20中h_suber是半减器,diff是输出差
a xin (diff=x-y),s_out是借位输出(s_out=1,xy),sub_in是借位输入。 diff_out c yin b
图3-19 1位全加器
--解(1.1):实现1位半减器h_suber(diff=x-y;s_out=1,xy) library ieee;
use ieee.std_logic_1164.all; entity h_suber is
port( x,y: in std_logic;diff,s_out: out std_logic); end entity h_suber;
architecture hs1 of h_suber is begin diff = x xor (not y); s_out = (not x) and y; end architecture hs1;
--解(1.2):采用例化实现图4-20的1位全减器
library ieee; --1位二进制全减器顺层设计描述 use ieee.std_logic_1164.all; entity f_suber is
port(xin,yin,sub_in: in std_logic;sub_out,diff_out: out std_logic); end entity f_suber; architecture fs1 of f_suber is
component h_suber --调用半减器声明语句port(x, y: in std_logic; diff,s_out: out std_logic); end component;
signal a,b,c: std_logic; --定义1个信号作为内部的连接线。 begin
u1: h_suber port map(x=xin,y=yin,diff=a, s_out=b); u2: h_suber port map(x=a, y=sub_in, diff=diff_out,s_out=c); sub_out = c or b;
end architecture fs1;
(2)以1位全减器为基本硬件,构成串行借位的8位减法器,要求用例化语句来完成此项设计(减法运算是x-y-sun_in=difft)。 串行借位的8位减法器 diff1diff0 sout diff7
--解(2):采用例化方法,以1位全减器为基本硬件;实现串行借位的8位减法器(上图所示)。 library ieee;
use ieee.std_logic_1164.all; entity suber_8 is port(x0,x1,x2,x3,x4,x5,x6,x7: in std_logic; y0,y1,y2,y3,y4,y5,y6,y7,sin:in std_logic;diff0,diff1,diff2,diff3:out
std_logic;diff4,diff5,diff6,diff7,sout: out std_logic); end entity suber_8;
architecture s8 of suber_8 is
component f_suber --调用全减器声明语句port(xin,yin,sub_in: in std_logic; sub_out,diff_out: out std_logic); end component; signal a0,a1,a2,a3,a4,a5,a6: std_logic; --定义1个信号作为内部的连接线。 begin u0:f_suber port
map(xin=x0,yin=y0,diff_out=diff0,sub_in=sin,sub_out=a0); u1:f_suber port
map(xin=x1,yin=y1,diff_out=diff1,sub_in=a0,sub_out=a1); u2:f_suber port
map(xin=x2,yin=y2,diff_out=diff2,sub_in=a1,sub_out=a2); u3:f_suber port
map(xin=x3,yin=y3,diff_out=diff3,sub_in=a2,sub_out=a3); u4:f_suber port
map(xin=x4,yin=y4,diff_out=diff4,sub_in=a3,sub_out=a4); u5:f_suber port
map(xin=x5,yin=y5,diff_out=diff5,sub_in=a4,sub_out=a5); u6:f_suber port
map(xin=x6,yin=y6,diff_out=diff6,sub_in=a5,sub_out=a6); u7:f_suber port
map(xin=x7,yin=y7,diff_out=diff7,sub_in=a6,sub_out=sout); end architecture s8;
3-8 设计一个求补码的程序,输入数据是一个有符号的8位二进制(原码)数。
--解:5-9 设计一个求补码的程序,输入数据是一个有符号的8位二进制数。 library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all; entity org_patch is
port( org_data : in std_logic_vector(7 downto 0);--原码输入patch_data : out std_logic_vector(7 downto 0));--补码输出 end org_patch;
architecture bhv of org_patch is begin process(org_data) begin if(org_data(7)=0) then
patch_data=org_data; --org_data=0,补码=原码。else patch_data=org_data(7)(not org_data(6 downto 0))+1;--org_data0,补码=|原码|取反+1。 end if; end process; end bhv; 3—10
library ieee;
use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity add is
port(a:in std_logic_vector(7 downto 0);b:in std_logic_vector(7 downto 0);ci:in std_logic;co:out std_logic;
count:out std_logic_vector(7 downto 0)); end add; architecture bhv of add is begin process(a,b,ci)
variable data:std_logic_vector(1 downto 0); variable c:std_logic; begin c:=ci; for n in 0 to 7 loop
data:=(0a(n))+(0b(n))+(0c); count(n)=data(0); c:=data(1); end loop; co=c;
end process; end bhv;
3-14 用循环语句设计一个7人投票表决器,及一个4位4输入最大数值检测电路。
--解:5-7 用循环语句设计一个7人投票表决器,及一个4位4输出最大数值检测电路。 library ieee; use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all; entity vote_7 is
port( din: in std_logic_vector(6 downto 0);--7位表决输入(1:同意,0:不同意) g_4: out std_logic; --超过半数指示
cnth: out std_logic_vector(2 downto 0));--表决结果统计数 end vote_7;
architecture bhv of vote_7 is begin process(din)
variable q: std_logic_vector(2 downto 0); begin q:=000;
for n in 0 to 6 loop -- n是loop的循环变量 if(din(n)=1) then q:=q+1; end if;end loop;cnth=q;
if q=4 then g_4=1; else g_4=0; end if; end process; end bhv;
5-7 用vhdl设计一个功能类似74ls160的计数器。
--解:3-10 用vhdl设计一个功能类似74ls160(异步复位和同步使能加载、计数的十进制加法计数器)的计数器。 library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all; entity cnt10 is port(clk,rst,en,load : in std_logic;
data : in std_logic_vector(3 downto 0); --4位预置数 dout : out std_logic_vector(3 downto 0);--计数值输出 cout : out std_logic); --计数进位输出 end cnt10;
architecture behav of cnt10 is begin process(clk,rst,en,load)
variable q : std_logic_vector(3 downto 0); begin
if rst=0 then q:=(others =0);--计数器异步复位 elsif clkevent and clk=1 then --检测时钟上升沿
if en=1 then--检测是否允许计数或加载(同步使能)if load=0 then q:=data; --允许加载else
if q9 then q:=q+1; --允许计数,检测是否小于9
else q:=(others=0); --大于等于9时,计数值清零 end if;end if; end if; end if;
if q=9 then cout=1; --计数大于9,输出进位信号
共分享92篇相关文档