vhdl 四输入表决器 二位二进制乘法器 一位二进制全减器等源代码及仿真波形
VHDL的乘法器设计——数字电路课程设计

cout=>c4,s=>s4(1));
------------------------------last u12:full_adder port map (a=>d4(1),b=>d4(2),cin=>d4(3),
【第二章】:设计思路及方案 算法结构(无符号)
由上图可见,乘法的运算最终是加法的运算,两个 4BIT 输入,输出为 7BIT。 模块一、半加器:单比特输入相加,
模块二、全加器:由两个半加器组成,有一个进位输入,
模块三、进位保留加法器:
3
最终程序结构图
流水设计的原理:在前向割集上加入四级流水
图一 图二
elsif clk'event and clk='1' then
--------------------------------------first d1(1)<= x(1); d1(2)<= x(2); d1(3)<= x(3);
7
d1(4)<= x(4); d1(5)<= x(5); d1(6)<= x(6); d1(7)<=c1(1) ; d1(8)<=s1(1); d1(9)<= x(9); d1(10)<=x(10); d1(11)<=c1(2); d1(12)<=s1(2); d1(13)<=x(13); d1(14)<=c1(3) ; d1(15)<=s1(3) ; d1(16)<=x(16);
【连载】FPGAVerilogHDL系列实例--------4位二进制加减法计数器

【连载】FPGAVerilogHDL系列实例--------4位⼆进制加减法计数器Verilog HDL 之 4位⼆进制加减法计数器⼀、原理 计数器是数字系统中⽤的较多的基本逻辑器件。
它不仅能记录输⼊时钟脉冲的个数,还可以实现分频、定时等功能。
计数器的种类很多。
按脉冲⽅式可以分为同步计数器和异步计数器;按进制可以分为⼆进制计数器和⾮⼆进制计数器;按计数过程数字的增减,可分为加计数器、减计数器和可逆计数器。
本实验就是设计⼀个4位⼆进制加减法计数器,该计数器可以通过⼀个控制信号决定计数器时加计数还是减计数,另外,该寄存器还有⼀个清零输⼊,低电平有效。
还有⼀个load装载数据的信号输⼊,⽤于预置数据;还有⼀个C的输出,⽤于计数器的级联。
其功能表如表1.1所⽰; 表1.1 4位⼆进制加减法计数器功能表⼆、实现在设计⽂件中输⼊Verilog代码1/****************************** 分频模块 *************************************/23 `timescale 1 ns / 1 ps4 module qu_dou ( clk ,rst , a ,b );56 input clk ;7 wire clk ;8 input rst ;9 input a ;10 wire a ;1112 output b ;13 reg b ;1415 reg [31:0] cnt ;16 reg clkout ;17 always @ ( posedge clk or negedge rst )18 begin19if ( rst == 1'b0 )20 cnt <= 0 ;21else begin if ( a==1'b1 ) begin22if ( cnt >= 32'd3000000 )23 b <= 1 ;24else25 cnt <= cnt + 1'b1 ;2627 end28else begin b <= 1'b0 ;29 cnt <= 0 ;30 end31 end32 end333435 endmodule功能实现1 `timescale 1 ns / 1 ps23 module counter4 ( load ,clr ,c ,DOUT ,clk, up_down ,DIN ,sysclk , rst );45 input load ;6 input clk;7 wire load ;8 input clr ;9 wire clr ;10 input up_down ;11 wire up_down ;12 input [3:0] DIN ;13 wire [3:0] DIN ;14 input sysclk ;15 input rst ;1617 output c ;18 reg c ;19 output [3:0] DOUT ;20 wire [3:0] DOUT ;21 reg [3:0] data_r;2223/***************** 例化去抖模块 *************************************/24 wire clk_r ;25 qu_dou qu_dou (26 .clk (sysclk) ,27 .rst (rst) ,28 .a (clk),29 .b (clk_r));3031//********************************************************************* 323334 assign DOUT = data_r;35 always @ ( posedge clk_r or posedge clr or posedge load)36 begin37if ( clr == 1) //同步清零38 data_r <= 0;39else if ( load == 1) //同步预置40 data_r <= DIN;41else begin if ( up_down ==1)42 begin43if ( data_r == 4'b1111) begin //加计数44 data_r <= 4'b0000;45 c = 1;46 end47else begin //减计数48 data_r <= data_r +1;49 c = 0 ;50 end51 end52else53 begin54if ( data_r == 4'b0000) begin //加计数55 data_r <= 4'b1111;56 c = 1;57 end58else begin //减计数59 data_r <= data_r -1;60 c = 0 ;61 end62 end63 end64 end65 endmodule。
用VHDL实现二位二进制乘法应用译码器

用V H D L实现二位二进制乘法应用译码器文件管理序列号:[K8UY-K9IO69-O6M243-OL889-F88688]------------------------------------------------------------------------------------ Company:-- Engineer:---- Create Date: 18:25:21 04/15/2013-- Design Name:-- Module Name: FOUR - Behavioral-- Project Name:-- Target Devices:-- Tool versions:-- Description:---- Dependencies:---- Revision:-- Revision 0.01 - File Created-- Additional Comments:------------------------------------------------------------------------------------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;entity FOUR isPort ( a : in STD_LOGIC;b : in STD_LOGIC;c : in STD_LOGIC;d : in STD_LOGIC;DOUT : out std_logic_vector(3 downto 0));end FOUR;architecture Behavioral of FOUR issignal DIN :STD_LOGIC_VECTOR(3 DOWNTO 0);signal y : std_logic_vector(16 downto 0);beginDIN <= A & B & C & D;process (A,B,C,D)begincase DIN iswhen "0000" => y(0) <= '0';when "0001" => y(1) <= '0';when "0010" => y(2) <= '0';when "0011" => y(3) <= '0';when "0100" => y(4) <= '0';when "0101" => y(5) <= '0';when "0110" => y(6) <= '0';when "0111" => y(7) <= '0';when "1000" => y(8) <= '0';when "1001" => y(9) <= '0';when "1010" => y(10) <= '0';when "1011" => y(11) <= '0';when "1100" => y(12) <= '0';when "1101" => y(13) <= '0';when "1110" => y(14) <= '0';when "1111" => y(15) <= '0';when others => y(16) <= '0';end case ;end process;DIN(3)<=not(y(15));DIN(2)<=not(y(10) and y(11) and y(14));DIN(1)<=not(y(6) and y(7) and y(9)and y(11) and y(13) and y(14)); DIN(0)<=not(y(5)and y(7) and y(13) and y(15));end Behavioral;。
【实验报告】北邮 - 电子线路设计与仿真 - 实验二 - 一位8421全加器设计(VHDL输入)

实验名称:一位8421全加器设计(VHDL输入法)一、实验目的(1)学习用VHDL语言对计数器设计、仿真和硬件测试,进一步熟悉VHDL设计技术。
设计程序独立完成全加器的仿真。
全加器由两个半加器组合而成,原理类似。
半加器不考虑低位进位,但有高位进位;全加器要考虑低位的进位且该进位和求和的二进制相加,可能获得更高的进位。
(2)VHDL编程输入的设计步骤,设计方法等。
二、实验内容和原理1、系统构成·八段数码管显示模块·八段数码管扫描模块·BCD转换及加法计算模块·键盘输入数据读取模块·时钟分频模块2、矩阵键盘工作原理按键设置在行列线交叉点,行列线分别连接到按键开关的两端。
列线通过上拉电阻接5V电压,即列线的输出被钳位到高电平状态。
判断键盘中有无按键按下是通过行线送入扫描信号然后从列线读取状态得到的。
其方法是依次给行线送低电平,检查列线的输入。
如果列线全是高电平,则代表低电平信号所在的行中无按键按下;如果列线有输入为低电平,则代表低电平信号所在的行和出现低电平的列的交点处有按键按下无按键按下时,col0~col3输出分别为“1111”当输入扫描时,扫描第一行,即IN1<=’0’,当按下Button 1,那么输出col输出信号将发生变化,Out1变为’0’,则col0~col3输出分别为“1110”。
可通过行扫描码和列输出码来获得分时扫描的键盘按压信号。
只要扫描时间适当,就可得到按键的按压情况。
3、键盘输入一个完整的键盘控制程序应解决以下任务:(1)检测有无按键按下(2)有键按下,在无硬件去抖得情况下,应有软件延时除去抖动影响(3)键扫描程序(4)将键编码转换成相应键值整个设计程序包括三个模块:时钟分频、键盘扫描和键译码转换。
为了显示,还必须在顶层添加显示部分。
由于使用的外部时钟频率为50MHz,这个频率对扫描来说太高,所以这里需要一个分频器来分得适合键盘扫描使用的频率。
EDA实验报告实验一:一位二进制全减器的设计

实验一、一位二进制全减器的设计一、实验目的:(1)掌握Quartus II 的VHDL 文本设计的全过程;(2)熟练和掌握EDA设计流程;熟悉简单组合电路的设计,掌握系统仿真,学会分析硬件测试结果。
(3)学习PH-1V型实验装置上发光二极管和按键的使用方法。
二、实验内容与要求:(1)用文本方法实现半减器,再利用半减器完成全减器的设计,熟悉层次设计概念;(2)给出此项设计的仿真波形;(3)选择实验电路NO.1验证, 用发光管指示显示结果。
三、设计原理:(1)半减器真值表:xx yy Diff1 S_out10 0 0 00 1 0 11 0 0 01 1 0 0(表中Diff1表示本位向高位的借位,S_out1表示本位)(2)全减器真值表:x y Sub_in diffr Sub_out0 0 0 0 00 0 1 1 10 1 0 1 10 1 1 0 11 0 0 1 01 0 1 0 01 1 0 0 01 1 1 1 1(表中Sub_in表示低位向本位的借位,diffr表示本位向高位的借位,Sub_out表示本位)四、实验程序:(1)对半减器进行描述:(2)对全减器进行原理图编辑:五、实验步骤:1.建立工作库文件夹和编辑设计文件(1)在D盘下建立一个文件夹保存工程文件;(2)打开QuartusII,建立新的VHDL文件,再打开的页面下输入程序。
2.编译过程(1)输入完程序之后逐个编译(2)逐个编译无错之后进行全程编译3.系统仿真(1)建立新的波形激励文件(2)在波形编辑器窗口添加节点(3)通过Edit->End Time 来设定仿真结束时间(4)点击save保存(5)通过Tools下的Simulator Tools项进行仿真,然后观察输出波形。
4.引脚锁定(1)通过Assignment->Assignment Editor->Pin查找到所有的引脚(2)选择各个输入输出信号来锁定到不同引脚,进行全编译。
采用VHDL层次化文件设计一个四位全减器

采用VHDL层次化文件设计一个四位全减器一、实训目的1.巩固VHDL层次化文件设计方法。
2.培养应用VHDL层次化文件设计法的技能。
二、实训器材计算机与Quartus Ⅱ工具软件。
三、实训指导(一)实训原理4位二进制减法器由4个全减器构成,而全减器又由一个半减器和一个或门构成,半减器的真值表如表4-1所示:表4-1 半减器的真值表输入输出a1b1s1c10000011110101100半减器的逻辑表达式为:s1=NOT(a1 XOR(NOT b1))c1=(NOT a1) AND b1一位全减器的真值表如表4-2所示:表4-2 一位全减器的真值表c_in i1i2fs c_out0000000111010100110010011101011100011111(二)实训步骤1.电路模块划分根据算法分析,4位二进制减法器可由4个全减器构成,画出其原理方框图。
全减器的原理方框图如图4-1所示。
而每个全减器又可划分为一个半减器和一个或门这两个更小的模块,画出其原理方框图。
4位二进制减法器的原理方框图如图4-2所示。
图4-1一位全减器原理方框图图4-2 4位二进制减法器原理框图2.设计底层设计文件(1)设计半减器文件halfsub.vhd 。
(2)设计或门电路文件orgate.vhd 。
(3)设计全减器电路文件fullsub.vhd ,其中把半减器和或门电路文件作为元件调用。
3.设计顶层设计文件设计顶层设计文件sub4.vhd ,其中把全减器文件作为元件调用。
VHDL 代码如下:halfsub.vhd文件代码如下:ENTITY halfsub ISPORT(a1,b1:IN BIT;s1,c1:OUT BIT);END halfsub;ARCHITECTURE a OF halfsub ISBEGINPROCESS(a1,b1)BEGINs1<=NOT(a1 XOR(NOT b1)) AFTER 10ns; c1<=(NOT a1) AND b1 AFTER 10 ns;END PROCESS;END a;orgate.vhd文件代码如下:ENTITY orgate ISPORT(a,b:IN BIT;o:OUT BIT);END orgate;ARCHITECTURE a OF orgate ISBEGINo<=a OR b;END a;fullsub.vhd文件代码如下:ENTITY fullsub ISPORT(i1,i2,c_in:IN BIT;fs,c_out:OUT BIT);END fullsub;ARCHITECTURE a OF fullsub ISSIGNAL temp_s,temp_c1,temp_c2:BIT; COMPONENT halfsubPORT(a1,b1:IN BIT;s1,c1:OUT BIT);END COMPONENT;COMPONENT orgatePORT(a,b:IN BIT;o:OUT BIT);END COMPONENT;BEGINU0:halfsub PORT MAP(i1,i2,temp_s,temp_c1);U1:halfsub PORT MAP(temp_s,c_in,fs,temp_c2);U2:orgate PORT MAP(temp_c1,temp_c2,c_out);END a;sub4.vhd文件代码如下:ENTITY sub4 ISPORT(a,b:IN BIT_VECTOR(3 DOWNTO 0);cin:IN BIT;fs:OUT BIT_VECTOR(3 DOWNTO 0);cout:OUT BIT);END sub4;ARCHITECTURE a OF sub4 ISSIGNAL temp_co0,temp_co1,temp_co2:BIT;COMPONENT fullsub ISPORT(i1,i2,c_in:IN BIT;fs,c_out:OUT BIT);END COMPONENT;BEGINU0:fullsub PORT MAP(a(0),b(0),cin,fs(0),temp_co0);U1:fullsub PORT MAP(a(1),b(1),temp_co0,fs(1),temp_co1); U2:fullsub PORT MAP(a(2),b(2),temp_co1,fs(2),temp_co2); U3:fullsub PORT MAP(a(3),b(3),temp_co2,fs(3),cout); END a;1.编译顶层设计文件把以上各个模块的VHDL设计文件放入同一个文件夹中,以顶层文件建立工程,直接编译顶层文件同时也就编译各个底层模块文件。
vhdl编程实例

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)。
用VHDL实现二位二进制乘法(应用4-16译码器)

------------------------------------------------------------------------------------ Company:-- Engineer:---- Create Date: 18:25:21 04/15/2013-- Design Name:-- Module Name: FOUR - Behavioral-- Project Name:-- Target Devices:-- Tool versions:-- Description:---- Dependencies:---- Revision:-- Revision 0.01 - File Created-- Additional Comments:------------------------------------------------------------------------------------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 FOUR isPort ( a : in STD_LOGIC;b : in STD_LOGIC;c : in STD_LOGIC;d : in STD_LOGIC;DOUT : out std_logic_vector(3 downto 0));end FOUR;architecture Behavioral of FOUR issignal DIN :STD_LOGIC_VECTOR(3 DOWNTO 0);signal y : std_logic_vector(16 downto 0);beginDIN <= A & B & C & D;process (A,B,C,D)begincase DIN iswhen "0000" => y(0) <= '0';when "0001" => y(1) <= '0';when "0010" => y(2) <= '0';when "0011" => y(3) <= '0';when "0100" => y(4) <= '0';when "0101" => y(5) <= '0';when "0110" => y(6) <= '0';when "0111" => y(7) <= '0';when "1000" => y(8) <= '0';when "1001" => y(9) <= '0';when "1010" => y(10) <= '0';when "1011" => y(11) <= '0';when "1100" => y(12) <= '0';when "1101" => y(13) <= '0';when "1110" => y(14) <= '0';when "1111" => y(15) <= '0';when others => y(16) <= '0';end case ;end process;DIN(3)<=not(y(15));DIN(2)<=not(y(10) and y(11) and y(14));DIN(1)<=not(y(6) and y(7) and y(9)and y(11) and y(13) and y(14)); DIN(0)<=not(y(5)and y(7) and y(13) and y(15));end Behavioral;。
vhdl 四位流水线乘法器

实验二四位流水线乘法器一、实验目的1.了解四位并行乘法器的原理。
2.了解四位并行乘法器的设计思想和流水线的实现方法。
3.掌握用VHDL 语言实现基本二进制运算的方法。
二、实验内容与要求通过开关和键盘输入两组4BIT的二进制数据,按照二进制加法器原理进行加和,求出和及进位,并通过LED显示灯输出显示,完成编译、综合、适配、仿真、实验箱上的硬件测试。
三、实验原理流水线结构的并行乘法器的最大有点就是速度快,尤其实在连续输入的乘法器中,可以达到近乎单周期的运算速度。
流水线乘法器是组合逻辑电路实现无符号数乘法的方法上发展而来的。
其关键是在组合逻辑电路的基础上插入寄存器。
假如有被乘数A 和乘数B,首先用A 与B 的最低位相乘得到S1,然后再把A 左移1 位与B 的第2 位相乘得到S2,再将A 左移3 位与B 的第三位相乘得到S3,依此类推,直到把B 的所有位都乘完为止,然后再把乘得的结果S1、S2、S3……相加即得到相乘的结果。
需要注意的是,具体实现乘法器是,并不是真正的去乘,而是利用简单的判断去实现,举个简单的例子。
假如A 左移n 位后与B 的第n 位相乘,如果B 的这位为‘1’,那么相乘的中间结果就是A 左移n 位后的结果,否则如果B 的这位为‘0’,那么就直接让相乘的中间结果为0 即可。
带B 的所有位相乘结束后,把所有的中间结果相加即得到A 与B 相乘的结果。
在此基础上插入寄存器即可实现流水线乘法器。
四、实验平台(1)硬件:计算机、GX-SOC/SOPC-DEV-LABCycloneII EP2C35F672C8核心板(2)软件:Quartus II软件PIN_AF8 DATAOUT[4] LED4PIN_AE7 DATAOUT[5] LED5PIN_AF7 DATAOUT[6] LED6PIN_AA11 DATAOUT[7] LED7PIN_AE21 BCD[0] 数码管DP4BPIN_AB20 BCD[1]PIN_AC20 BCD[2]PIN_AF20 BCD[3]PIN_AE20 BCD[4] 数码管DP5BPIN_AD19 BCD[5]PIN_AC19 BCD[6]PIN_AA17 BCD[7]PIN_AA18 BCD[8] 数码管DP6BPIN_W17 BCD[9]PIN_V17 BCD[10]PIN_AB18 BCD[11]六、仿真截图七、硬件实现八、程序代码1---clkgen.vhdlibrary IEEE;-- 1HZuse IEEE.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity clkgen isport (CLK : in std_logic;CLK1HZ: out std_logic);end entity;architecture clk_arch of clkgen issignal COUNT : integer range 0 to 50000000; --50MHZ -->1hz begin -- 50M/1=50000000 PROCESS(CLK)BEGINif clk'event and clk='1' thenIF COUNT= 50000000 thenCOUNT<=0;ELSE COUNT<=COUNT+1;END IF;END IF;END PROCESS;PROCESS(COUNT)BEGINIF COUNT= 5000000 THEN -- 1HZCLK1HZ<='1';ELSE CLK1HZ<='0';END IF;END PROCESS;end architecture;2—BCD-- 输出控制模块,把乘法器的输出转换成BCD码在数码管上显示、-- SCKZ.VHDlibrary IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;entity BIN2BCD isport ( DIN: in std_logic_vector(7 downto 0); ---The input 8bit binaryBCDOUT: out std_logic_vector(11 downto 0)--输出显示, 已转换成BCD码);end entity;architecture arch of BIN2BCD issignal data2,data3,data4 :std_logic_vector(9 downto 0);-- 输出数据缓存signal hundred,ten,unit:std_logic_vector(3 downto 0);--signal bcdbuffer:std_logic_vector(11 downto 0);---2'1111_1001_11=999beginBCDOUT<= bcdbuffer;bcdbuffer(11 downto 8)<=hundred;bcdbuffer(7 downto 4)<=ten;bcdbuffer(3 downto 0)<=unit;get_hundred_value:process(data2)beginDA TA2<="00"&DIN;---get hundred valueif data2>=900 thenhundred<="1001";--9data3<=data2-900;elsif data2>=800 thenhundred<="1000";--8data3<=data2-500;elsif data2>=700 thenhundred<="0111";--7data3<=data2-700;elsif data2>=600 thenhundred<="0110";--6data3<=data2-600;elsif data2>=500 thenhundred<="0101";--5data3<=data2-500;elsif data2>=400 thenhundred<="0100";--4data3<=data2-400;elsif data2>=300 thenhundred<="0011";--3data3<=data2-300;elsif data2>=200 thenhundred<="0010";--2data3<=data2-200;elsif data2>=100 thenhundred<="0001";--1data3<=data2-100;else data3<=data2;hundred<="0000";end if;end process; ---get_thousand_valueget_tens_value:process(data3) begin---get tens placeif data3>=90 thenten<="1001";--9data4<=data3-90;elsif data3>=80 thenten<="1000";--8data4<=data3-50;elsif data3>=70 thenten<="0111";--7data4<=data3-70;elsif data3>=60 thenten<="0110";--6data4<=data3-60;elsif data3>=50 thenten<="0101";--5data4<=data3-50;elsif data3>=40 thenten<="0100";--4data4<=data3-40;elsif data3>=30 thenten<="0011";--3data4<=data3-30;elsif data3>=20 thenten<="0010";--2data4<=data3-20;elsif data3>=10 thenten<="0001";--1data4<=data3-10;else data4<=data3;ten<="0000";end if;end process; ---get_ten_valueget_unit_value:process(data4)begin--unit's orderif (data4>0) thenunit<=data4(3 downto 0);else unit<="0000";end if;end process;end arch;3 multi4b --------------------------------------------------------------------------------/ -- DESCRIPTION : Signed mulitplier:-- AIN (A) input width : 4-- BIN (B) input width : 4-- Q (data_out) output width : 8-- 并行流水乘法器--------------------------------------------------------------------------------/--10 × 9 = 90-- 1 0 1 0-- 1 0 0 1 =-- --------------- 1 0 1 0-- 0 0 0 0 --partial products-- 0 0 0 0-- 1 0 1 0-- -------------------- 1 0 1 1 0 1 0--parallel : process all the inputs at the same time--pipeline : use several stages with registers to implement it----关键思想,插入寄存器library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;entity multi4b isport ( CLK: in STD_LOGIC; ---system clockAIN: in STD_LOGIC_VECTOR (3 downto 0); ---one inputBIN: in STD_LOGIC_VECTOR (3 downto 0);-- the other inputdata_out: out STD_LOGIC_VECTOR (7 downto 0)---the result ---make sure the biggest value ,i,e. 1111x1111=1110_0001 can be held in the register );end multi4b;architecture multi_arch of multi4b issignal A,B :std_logic_vector(3 downto 0); --input register---registers to hold the result of the first processing---registers added to make use of pipeline, the 1st stagesignal A_MULT_B0: STD_LOGIC_VECTOR (3 downto 0);signal A_MULT_B1: STD_LOGIC_VECTOR (3 downto 0);signal A_MULT_B2: STD_LOGIC_VECTOR (3 downto 0);signal A_MULT_B3: STD_LOGIC_VECTOR (3 downto 0);---register to hold the result of the multipliersignal C_TEMP : STD_LOGIC_VECTOR (7 downto 0);beginPROCESS(CLK,AIN,BIN)beginif CLK'EVENT AND CLK='1' THEN-- multiplier operand inputs are registeredA<= AIN;B<= BIN;-----------------Fist stage of the multiplier------------------here we get the axb(0),axb(1),axb(2),axb(3),i.e.partial products---put them into the responding registersA_MULT_B0(0) <= A (0) and B (0);----- multi 1 , get the a(0) and b(0), & put it into the register A_MULT_B0(0)A_MULT_B0(1) <= A (1) and B (0);A_MULT_B0(2) <= A (2) and B (0);A_MULT_B0(3) <= A (3) and B (0);--10 × 9 = 90-- 1 0 1 0-- 1 0 0 1 =-- --------------- 0 0 0 0 1 0 1 0-- 0 0 0 0 0 0 0 0 --partial products-- 0 0 0 0-- 1 0 1 0-- -------------------- 1 0 1 1 0 1 0A_MULT_B1(0) <= A (0) and B (1);A_MULT_B1(1) <= A (1) and B (1);A_MULT_B1(2) <= A (2) and B (1);A_MULT_B1(3) <= A (3) and B (1);A_MULT_B2(0) <= A (0) and B (2);A_MULT_B2(1) <= A (1) and B (2);A_MULT_B2(2) <= A (2) and B (2);A_MULT_B2(3) <= A (3) and B (2);A_MULT_B3(0) <= A (0) and B (3);A_MULT_B3(1) <= A (1) and B (3);A_MULT_B3(2) <= A (2) and B (3);A_MULT_B3(3) <= A (3) and B (3);end if;end process;--------------------Second stage of the multiplier---------------add the all the partial products ,then get the result of the multiplier C_TEMP<=( "0000" & A_MULT_B0 )+( "000"& A_MULT_B1 &'0' )+( "00" & A_MULT_B2 & "00" )+( '0'&A_MULT_B3 & "000" );--build a signal register output---输出寄存,利于实现流水data_out <= C_TEMP; --output registerend multi_arch;九、实验总结。
VHDL硬件描述语言四位加法器实验报告

题目:硬件描述语言实验四:四位加法器姓名:***** 学号: ******地点:主楼402 时间: 3月21日一、实验目的:进一步练习VHDL语言设计工程的建立与仿真的步骤和方法、熟悉VHDL语言基本设计实体的编写方法。
二、实验环境:PC个人计算机、Windows XP操作系统、Quartus II集成开发环境软件。
三、设计要求:采用三种方式设计一个四位加法器,实体名称分别为“adder4”、“adder4_2”、和“adder_3”,四位加法器的引脚与功能如下表。
端口模式端口名数据类型说明in (输入)astd_logic_vector(3 downto 0)加数b 加数ci std_logic 低位进位out (输出)s std_logic_vector(3 downto 0) 和co std_logic 高位进位四、实验步骤:1、采用寄存器传输的描述方式:首先新建一个工程,命名为“adder4”,然后编辑代码。
注意在模块内部(构造体说明部分)需要定义三个连接线,定义语句为:signal c0,c1,c2 : std_logic代码如下:library ieee;use ieee.std_logic_1164.all;entity adder4 isport(a,b : in std_logic_vector (3 downto 0);ci : in std_logic;s : out std_logic_vector (3 downto 0);co : out std_logic);end entity;architecture rtl of adder4 issignal c0,c1,c2 : std_logic;begins(0) <= a(0) xor b(0) xor ci;c0<= (a(0) and b(0)) or (a(0) and ci) or (b(0) and ci);s(1)<= a(1) xor b(1) xor c0;c1<= (a(1) and b(1)) or (a(1) and c0) or (b(1) and c0);s(2)<= a(2) xor b(2) xor c1;c2<= (a(2) and b(2)) or (a(2) and c1) or (b(2) and c1);s(3)<= a(3) xor b(3) xor c2;co<= (a(3) and b(3)) or (a(3) and c2) or (b(3) and c2); end rtl;仿真波形图如下:仿真电路图如下:s~1c0~0c0~1c0~3c0s~3c1~0c1~1c1~3c1s~5c2~0c2~1c2~3c2s~7co~0co~1co~3co~4cicoa[3..0]b[3..0]s[3..0]由上图可知设计是正确的。
利用VHDL的实现通用计算器的源程序

源程序:4位二进制并行进位加法器的源程序ADDER4B.VHD 如下LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY ADDER4B IS --四位二进制并行加法器PORT(ci:IN STD_LOGIC; --低位进位a:IN STD_LOGIC_VECTOR3 DOWNTO 0); --4位加数b:IN STD_LOGIC_VECTOR(3 DOWNTO 0); --4位被加数s:OUT STD_LOGIC_VECTOR(3 DOWNTO 0); --4位和co:OUT STD_LOGIC --进位输出);END ADDER4B;ARCHITECTURE behave OF ADDER4B ISSIGNAL SINT:STD_LOGIC_VECTOR(4 DOWNTO 0); --部定义的一个数据SIGNAL aa,bb:STD_LOGIC_VECTOR(4 DOWNTO 0);BEGINaa<=’0’&a; --将4位加数矢量扩为5位,为进位提供空间bb<=’0’&b; --将4位被加数矢量扩为5位,为进位提供空间INT<=aa+bb+ci; -- 相加s<=SINT(3 DOWNTO 0);co<=SINT(4); --最高位为输出进位END behave;顶层模块:8位二进制并行进位加法器的部分程序ADDER8B.VHD如下:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY ADDER8B ISPORT(ci:IN STD_LOGIC;a:IN STD_LOGIC_VECTOR(7 DOWNTO 0);b:IN STD_LOGIC_VECTOR(7 DOWNTO 0);s:OUT STD_LOGIC_VECTOR(7 DOWNTO 0);co:OUT STD_LOGIC);END ADDER8B;ARCHITECTURE a OF ADDER8B ISComponent adder4B --引用4位二进制并行进位加法器PORT(ci:IN STD_LOGIC;a:IN STD_LOGIC_VECTOR3 DOWNTO 0);b:IN STD_LOGIC_VECTOR(3 DOWNTO 0);s:OUT STD_LOGIC_VECTOR(3 DOWNTO 0);co:OUT STD_LOGIC);END COMPONENT;SIGNAL CARRY_OUT:STD_LOGIC; --4位加法器的进位标志BEGINU1:ADDER4B --安装一个4位二进制加法器U1PORT MAP(ci=>ci,a=>a(3 DOWNTO 0),b=>b(3 DWONTO 0),s=>(3 DOWNTO0),co=>CARRY_OUT);U2:ADDER4B --安装一个4位二进制加法器U2PORT MAP(ci=>CARRY_OUT,a=>a(7 DOWNTO 4),b=>b(7 DWONTO 4),s=>(7 DOWNTO 4),co=>co);END behave;加法器VHDL程序如下LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY adder ISport(a:in std_logic; --被加数ab:in std_logic; --加数bci:in std_logic; --输入进位s:out std_logic; --结果输出co:out std_logic --输出进位);end adder;architecture behave of adder issignal tem: std_logic; --暂存signal stem: std_logic;begintem<=a xor b; --中间变量stem<=tem xor ci; --结果co<=(tem and ci) or (a and b); --进位输出s<=stem; --输出end behave;4位二进制并行进位减法器的源程序suber.VHD如下:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY sub4 ISPORT(a:IN STD_LOGIC_VECTOR(3 DOWNTO 0); --4位被减数b:IN STD_LOGIC_VECTOR(3 DOWNTO 0); --4位减数ci:IN STD_LOGIC; --输入进位s:OUT STD_LOGIC_VECTOR(3 DOWNTO 0); --结果输出co:OUT STD_LOGIC --输出进位);end suber;architecture behave of suber iscomponent adder is --引用加法器的模块port(a:in std_logic;b:in std_logic;ci:in std_logic;s:out std_logic;co:out std_logic);end component;signal btem:std_logic_vector(3 downto 0); --减数寄存signal ctem:std_logic_vector(4 downto 0); -- 进位寄存signal stem:std_logic_vector(3 downto 0); -- 结果寄存beginbtem(3 downto 0)<=not b(3 downto 0); --先把减数求反ctem(0)<=not ci; --输入的进位也求反,从而对减数求补码g1:for I in 0 to 3 generate --连用4位全加器add:adder port map (a(i),btem(i),ctem(i),stem(i),ctem(i+1));end generate;s(3 downto 0)<=stem(3 downto 0); --结果输出co<=not ctem(4); --求反输出进位end behave;乘法器的源程序:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;Entity mul isPort(a:in std_logic_vector(3 downto 0); --4位被乘数b:in std_logic_vector(3 downto 0); --4位乘数y:out std_logic_vector(7 downto 0) --乘积);end mul;architecture arch of mul isbeginy(7 downto 0)<=a(3 downto 0)*b(3 downto 0);end arch;除法器的源程序:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;entity diver isPORT(a:IN STD_LOGIC_VECTOR(7 DOWNTO 0); --8位被除数输入b:IN STD_LOGIC_VECTOR(3 DOWNTO 0); --4位除数输入clk:IN STD_LOGIC; --时钟str:IN STD_LOGIC; --启动信号s:OUT STD_LOGIC_VECTOR(3 DOWNTO 0); --4位商输出y:OUT STD_LOGIC_VECTOR(3 DOWNTO 0) --4位余数输出);End;Architecture behave of diver isComponent suber is --引用减法器PORT(a:IN STD_LOGIC_VECTOR(3 DOWNTO 0);b:IN STD_LOGIC_VECTOR(3 DOWNTO 0);ci:IN STD_LOGIC;s:OUT STD_LOGIC_VECTOR(3 DOWNTO 0);co:OUT STD_LOGIC);End component;type state_type is(start,one,two,three,eror); --状态定义signal state:state_type; --定义状态变量signal ain: std_logic_vector(7 downto 0); --被除数寄存signal bin: std_logic_vector(3 downto 0); --除数寄存signal atem: std_logic_vector(3 downto 0); --减法器被减数输入signal btem: std_logic_vector(3 downto 0); --减法器减数输入signal stem: std_logic_vector(3 downto 0); --结果寄存signal citem: std_logic; --减法器借位输入signal cotem:std_logic; --减法器借位输出beginp2:process(clk)variable n: integer range 0 to 3; --移位次数计数值beginif clk’event and clk=’1’ thencase state iswhen atart=> --开始状态if str=’1’ then --收到启动信号state<=one; --转到状态oneatem(3 downto 0)<=a(7 downto 4); --把高4位放到减法器被减数端btem(3 downto 0)<=b(3 downto 0); --把除数放到减法器减数端ain(7 downto 0)<=a(7 downto 0); --寄存被除数bin(3 downto 0)<=b(3 downto 0); --寄存除数end if;when one=> --第一次移位if cotem=’0’ then --被除数高4位小于除数,溢出!state<=eror; --转到出错状态else --不溢出ain(3 downto 1)<=ain(2 downto 0); --被除数做移位ain(0)<=not cotem; --在最低位接收该位商值atem(3 downto 0)<=ain(6 downto 3); --把除数寄存器高4位输到减法器,作为减法器被减数state<=two; --转到下一状态end if;when two=> --再做3此移位if n=2 then --第四次移位state<=three; --是,则跳转到下一状态n:=0; --移位计数器清零else --否则state<=two; --还回到这个状态n:=n+1; --移位计数器加1end if;if cotem=’0’ then --不够减,有借位atem(3 downto 1)<=stem(2 downto 0); --减法器结果移位作为下一次的输入else --够减,没有借位atem(3 downto 1)<=atem(2 downto 0); --结果输出移位作为下一次的输入end if;ain(3 downto 1)<=ain(2 downto 0); --结果寄存器左移一位ain(0)<=not cotem; --这次运算借位输出,输入到寄存器ain最后一位atem(0)<=ain(3); --寄存器ain的最高位作为减法器输入被减数的低位when three=> --正常运算结果输出s(3 downto 1)<=ain(2 downto 0); --寄存器ain低3位作为输出结果高3位s(0)<=not cotem; --最后一次减法运算的借位输出求反作为结果输出最低位if cotem=’0’ then --最后一次减法运算,够减(无借位)y(3 downto 0)<=atem(3 downto 0); --则减法器输出结果为整个除法的余数else --否则,不够减y(3 downto 0)<=atem(3 downto 0); --则最后一次减法运算的被减数为整个除法的余数end if;atem(3 downto 0)<= "0"; --寄存器清零btem(3 downto 0)<= "0"; --寄存器清零state<=start; --回到开始状态when eror=> --溢出状态state<=start; --回到开始状态atem(3 downto 0)<= "0"; --寄存器清零btem(3 downto 0)<= "0"; --寄存器清零end case;end if;end process p2;citem<=’0’; --4位减法器借位输入接地U1:suber port map(atem,btem,citem,stem,cotem);end behave;数字按键译码电路VHDL语言描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;entity numdecoder isport(reset:in std_logic;inclk:std_logic;innum:std_logic_vetctor(9 downto 0);outnum:buffer std_logic_vector(3 woento 0);outflag:out std_logic);end;architecture behave of numdecoer is]beginif reser=’1’thenoutnum<=”0000”;elsif inclk’event and inclk=’1’thencase innum iswhen”0000000001”=>outnum<=”0000”;outflag<=’1’; --按下第一个键表示输入0when”0000000010”=>outnum<=”0001”;outflag<=’1’; --按下第二个键表示输入1when”0000000100”=>outnum<=”0010”;outflag<=’1’; --按下第三个键表示输入2when”0000001000”=>outnum<=”0011”;outflag<=’1’; --按下第四个键表示输入3when”0000010000”=>outnum<=”0100”;outflag<=’1’; --按下第五个键表示输入4when”0000100000”=>outnum<=”0101”;outflag<=’1’; --按下第六个键表示输入5when”0001000000”=>outnum<=”0110”;outflag<=’1’; --按下第七个键表示输入6when”0010000000”=>outnum<=”0111”;outflag<=’1’; --按下第八个键表示输入7when”010*******”=>outnum<=”1000”;outflag<=’1’; --按下第九个键表示输入8when”1000000000”=>outnum<=”1001”;outflag<=’1’; --按下第十个键表示输入9when others=>outnum<=outnum;outflag<=’0’; --不按键时保持end case;end if;end process;end behave;7段译码器的vhdl语言描述LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL; entity vdecode isport(indata:in std_logic_vector(3 downto 0);outdata:out std_logic_vector(0 to 6));End;Atchitecture behave of vdecode isBeginWith indata selectOutdata<=”1111110”when”0000”,”0110000”when”0000”,”1111001”when”0000”,”0110011”when”0000”,”1011011”when”0000”,”1011111”when”0000”,”1110000”when”0000”,”1111111”when”0000”,”1111110”when”0000”,”1111110”when”0000”,”1111011”when”0000”,”0000000”when others;End behave;8位二进制数转换成个位、十位、百位的进程:Ctrview:process(c,clk)BeginIf c=’1’thenview1<=”0000”;view2<=”0000”;view<=”0000”;viewstep<=takenum;elsif clk’event and clk=’1’thencase virestep iswhen takenum=>ktemp<=keep;viewstep<=hundred=>if ktemp>=”11001000”thenview1<=”0010”;ktemp<=ktemp-“11001000;elsif ktemp>=”01100100”thenview1<=”0001”;ktemp<=ktemp-“01100100”;elsif view1<=”0000”;end if;viewstep<=ten;when ten=>if ktemp>=”01011010”thenview2<=”1001”;ktemp<=ktemp-“01011010”;elsif ktemp>=”01010000”thenview2<=”1000”;ktemp<=ktemp-“01010000”; elsif ktemp>=”01000110”thenview2<=”0111”;ktemp<=ktemp-“01000110”; elsif ktemp>=”00111100”thenview2<=”0110”;ktemp<=ktemp-“00111100”; elsif ktemp>=”00110010”thenview2<=”0101”;ktemp<=ktemp-“00110010”; elsif ktemp>=”00101000”thenview2<=”0100”;ktemp<=ktemp-“00101000”; elsif ktemp>=”00011110”thenview2<=”0011”;ktemp<=ktemp-“00011110”; elsif ktemp>=”00010100”thenview2<=”0010”;ktemp<=ktemp-“00010100”; elsif ktemp>=”00001010”thenview2<=”0001”;ktemp<=ktemp-“00001010”; elsif view2<=”0000”;end if;viewstep<=onewhen one=>view3<=ktemp(3 downto 0);viewstep<=takenum;when others=>NULL;end case;end if;end process ctrview;计算器的VHDL语言LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;Entity cal isPort(inclk:in std_logic;num:in std_logic_vector(9 downto 0);plus: in std_logic;subt: in std_logic;mult: in std_logic;mdiv: in std_logic;equal: in std_logic;c: in std_logic;onum1,onum2,onum3:out std_logic_vector(0 to0));end cal;architecture behave of cal istype state is(takenum,hundred,ten,one);signal viewstep: state;signal ktemp: std_logic_vector(7 downto 0);signal flag: std_logic;signal fl: std_logic;signal acc: std_logic_vector(7 downto 0);signalreg: std_logic_vector(7 downto 0);signal keep: std_logic_vector(7 downto 0);signal ans:std_logic_vector(7 downto 0);signal dans: std_logic_vector(3 downto 0);signal numbuff: std_logic_vector(3 downto 0);signal vf: std_logic;signal strdiv: std_logic;signal numclk: std_logic;signal clear:std_logic;signal inplus: std_logic;signal insubt: std_logic;signal inmult: std_logic;signal inmdiv: std_logic;signal inequal: std_logic;signal view1,view2,view3:std_logic_vector(3 downto 0); signal cou: std_logic_vector(1 downto 0);signal clk_gg: std_logic_vector(11 downto 0);signal clk: std_logic;signal clk: std_logic;component numbercoder isport(reset: in std_logic;inclk: in std_logic;innum: in std_logic_vector(9 downto 0);outnum: buffer std_logic_vector(3 downto 0); outflag: out std_logic);end component;component vdecode isport(indata: in std_logic_vector(3 downto 0); outdata: out srd_logic_vector(0 to 6));end component;component diver isport(a: in std_logic_vector(7 downto 0);b: in std_logic_vector(3 downto 0);clk: in std_logic;str: in std_logic;s: in std_logic_vector(3 downto 0);y: in std_logic_vector(3 downto 0););end component;begininum1: numdecoder port map (c,clk,num,numbuff,numclk); clock: process(inclk,c)beginif c=’1’ thenclk_gg(11 downto 0)<=”0”;elsif inclk’event and inclk=’1’ thenclk_gg(11 downto 0)<=clk_gg(11 downto 0)+1;end if;end process clock;clk<=clk_gg(11);pacecal: process(c,clk)beginif c=’1’theninplus<=’0’;insubt<=’0’;inmult<=’0’;inmdiv<=’0’;elsif clk’event and clk=’1’thenif plus=’1’ theninplus<=’1’;insubt<=’0’;inmult<=’0’; inmdiv<=’0’;elsif subt=’1’ theninplus<=’0’;insubt<=’1’;inmult<=’0’; inmdiv<=’0’;elsif mult=’1’ theninplus<=’0’;insubt<=’0’;inmult<=’1’; inmdiv<=’0’;elsif mdiv=’1’ theninplus<=’0’;insubt<=’0’;inmult<=’0’; inmdiv<=’1’;end if;end if;end process pacecal;ctrflag: process(c,clk)beginif c=’1’ thenflag<=’0’;elsif clk’event and clk=’1’ thenif inplus=’1’ or insubt=’1’ or inmult==’1’ or inmdiv=’1’ then flag<=’1’;else flag<=’0’;end if;ctrfirstnum: process(c,clk)beginif c=’1’ thenacc<=”00000000”;elsif numclk’event and numclk=’0’ thenif flag=’0’ thenacc<=acc*”1010”+numbuff;end if;enf if;end process ctrfirstnum;ctrsecondnum: process(c,clk)beginif c=’1’ or clear=’1’ thenreg<=”00000000”;fl<=’0’elsif numclk’event and numclk=’0’ thenif flag=’1’ thenfl<=’1’;reg<=reg*”1010”+numbuff;end if;end if;end process ctrsecondnum;ctrclear: process(c,clk)beginif c=’1’ thenclear<=’0’;elsif clk’event and clk=’1’ thenif plus=’1’ or subt=’1’ thenclear<=’1’;else clear<=’0’;end if;end if;end process ctrclear;ctrinequal: process(c,clk)beginif c=’1’ theninequal<=’0’;elsif clk’event and clk=’1’ thenif plus=’1’ or subt=’1’ or mult=’1’ or mdiv=’1’ or equal=’1’ then inequal<=’1’;else inequal<=’0’;end if;end if;end process ctrinequal;ctrcou: process(c,inequal)beginif c=’1’ thencou<=”00”;elsif inequal’event and inequal=’1’ thenif cou=”10” thencou<=cou;else cou<=cou+1;end if;end if;end process ctrcou;ctrcal: process(c,inequal)beginif c=’1’ thenans<=”00000000”;strdiv<=’0’;elsif inequal’event and inequal=’1’ thenif flag=’1’ thenif inplus=’1’ thenif cou=”10” thenans<=ans+reg;else ans<=acc+reg;end if;elsif insubt=’1’ thenif cou=”10” thenans<=ans-reg;else ans<=acc-reg;end if;elsif inmult=’1’ thenif acc<=00001111” and reg<=”00001111” thenans<=acc(3 downto 0)*reg(3 downto 0);else ans<=”00000000”;end if;elsif inmdiv=’1’ thenstrdiv<=’1’;end if;else strdiv<=’0’;end if;end if;end process ctrcal;d1: div port map (acc,reg(3 downto 0),clk,strdiv,dans); ctrvf: process(c,equal)beginif c=’1’ thenvf<=’0’;elsif equal;event and equal=’1’ thenvf<=’1’;end if;end process ctrvf;ctrkeep: process(c,clk)beginif c=’1’ thenkeep<=”00000000”;elsif clk’event and clk=’0’ thenif flag=’0’ thenkeep<=acc;elsif flag=’1’ and fl=’1’ and vf=’0’ thenkeep<=reg;elsif flag=’1’ and fl=’0’ and vf=’0’ and cou=”10” then keep<=ans;elsif flag=’1’ and vf=’1’ thenif inmdiv=’0’ thenkeep<=ans;elsekeep(3 downto 0)<=dans;end if;end if;end if;end process ctrkeep;ctrview: process(c,clk)beginif c=’1’ thenview1<=”0000”;view2<=”0000”;view3<=”0000”; viewstep<=takenum;elsif clk’event and clk=’1’ thencase viewstep iswhen takenum =>ktemp<=keep;viewstep<=hundred;when hundred =>if ktemp>=”11001000” thenview1<=”0010”; ktemp<=ktemp-“11001000”;elsif ktemp>=”01100100” thenview1<=”0001”; ktemp<=ktemp-“01100100”;else view1<=”0000”;end if;viewstep<=ten;when ten =>if ktemp>=”01011010” thenview2<=”1001”; ktemp<=ktemp-“01011010”;elsif ktemp>=”01010000” thenview2<=”1000”; ktemp<=ktemp-“01010000”;elsif ktemp>=”01000110” thenview2<=”0111”; ktemp<=ktemp-“01000110”;elsif ktemp>=”00111100” thenview2<=”0110”; ktemp<=ktemp-“00111100”;elsif ktemp>=”00110010” thenview2<=”0101”; ktemp<=ktemp-“00110010”;elsif ktemp>=”00101000” thenview2<=”0100”; ktemp<=ktemp-“00101000”;elsif ktemp>=”00011110” thenview2<=”0011”; ktemp<=ktemp-“00011110”;elsif ktemp>=”00010100” thenview2<=”0010”; ktemp<=ktemp-“00010100”;elsif ktemp>=”00001010” thenview2<=”0001”; ktemp<=ktemp-“00001010”; else view2<=”0000”;end if;viewstep<=one;when one =>view3<=ktemp(3 downto 0);viewstep<=takenum;when others =>null;end case;end if;end process ctrview;v1: vdecode port map (view1,onum1);v2: vdecode port map (view2,onum2);v3: vdecode port map (view3,onum3);end c;。
北邮数电实验VHDL源代码完整2013年汇总

北邮数电实验VHDL源代码完整版注:北邮信通院数电实验,大二下共四次实验,以下为四次实验的完整代码,仅供参考,希望学弟学妹在抄代码的时候了解每一行代码的含义。
知识是自己的。
别忘了,北邮的未来靠你们。
注意事项:1学校部分电脑打不开07版word文件(后缀docx),建议大家准备一份TXT以防万一2运行出错时可能是你输入有误,比如中文和英文符号弄错了3数电实验很简单,但要心细,一定要按老师说的做4数电实验报告千万不要抄袭,老师判断力很强实验一:半加器老师会给出,全加器是画图,怎么画书上有,不用源代码。
实验二:(1)3位二进制数比较器LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY comp3 ISPORT(A:IN STD_LOGIC_VECTOR(2 DOWNTO 0);B:IN STD_LOGIC_VECTOR(2 DOWNTO 0);YA,YB,YC:OUT STD_LOGIC);END comp3;ARCHITECTURE behave OF comp3 ISBEGINPROCESS(A,B)BEGINIF(A>B)THENY A<='1';YB<='0';YC<='0';ELSIF(A<B)THENY A<='0';YB<='1';YC<='0';ELSEY A<='0';YB<='0';YC<='1';END IF;END PROCESS;END behave;(2)4选1数据选择器LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY mux4 ISPORT(A:IN STD_LOGIC_VECTOR(1 DOWNTO 0);D0,D1,D2,D3:IN STD_LOGIC;Y,YB:OUT STD_LOGIC);END mux4;ARCHITECTURE behave OF mux4 ISBEGINPROCESS(A,D0,D1,D2,D3)BEGINCASE A ISWHEN"00"=> Y<=D0;YB <= NOT D0;WHEN"01"=> Y<=D1;YB <= NOT D1;WHEN"10"=> Y<=D2;YB <= NOT D2;WHEN"11"=> Y<=D3;YB <= NOT D3;WHEN OTHERS=> Y<='Z';YB<='Z';END CASE;END PROCESS;END behave;(3)8421码转换为格雷码LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY trans1 ISPORT(A:IN STD_LOGIC_VECTOR(3 DOWNTO 0);B:OUT STD_LOGIC_VECTOR(3 DOWNTO 0)); END trans1;ARCHITECTURE trans_gray OF trans1 ISBEGINB(0)<=A(0)XOR A(1);B(1)<=A(1)XOR A(2);B(2)<=A(2)XOR A(3);B(3)<=A(3);END trans_gray;(4)8421码转换为余三码LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY sunyu_trans2 ISPORT(A:IN STD_LOGIC_VECTOR(3 DOWNTO 0);B:OUT STD_LOGIC_VECTOR(3 DOWNTO 0)); END sunyu_trans2;ARCHITECTURE trans_ex3 OF sunyu_trans2 IS BEGINPROCESS(A)BEGINCASE A ISWHEN"0000"=> B<="0011";WHEN"0001"=> B<="0100";WHEN"0010"=> B<="0101";WHEN"0011"=> B<="0110";WHEN"0100"=> B<="0111";WHEN"0101"=> B<="1000";WHEN"0110"=> B<="1001";WHEN"0111"=> B<="1010";WHEN"1000"=> B<="1011";WHEN"1001"=> B<="1100";WHEN OTHERS=> B<="ZZZZ";END CASE;END PROCESS;END trans_ex3;(5)数码管译码器LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY sunyu_encoder ISPORT(A:IN STD_LOGIC_VECTOR(3 DOWNTO 0);B:OUT STD_LOGIC_VECTOR(6 DOWNTO 0);C:OUT STD_LOGIC_VECTOR(5 DOWNTO 0)); END sunyu_encoder;ARCHITECTURE encoder_arch OF sunyu_encoder IS BEGINPROCESS(A)BEGINC<="011111";CASE A ISWHEN"0000"=> B<="1111110";--0WHEN"0001"=> B<="0110000";--1WHEN"0010"=> B<="1101101";--2WHEN"0011"=> B<="1111001";--3WHEN"0100"=> B<="0110011";--4WHEN"0101"=> B<="1011011";--5WHEN"0110"=> B<="1011111";--6WHEN"0111"=> B<="1110000";--7WHEN"1000"=> B<="1111111";--8WHEN"1001"=> B<="1111011";--9WHEN OTHERS=> B<="ZZZZZZZ";END CASE;END PROCESS;END encoder_arch;实验三:注:以下的AAA(1)(2)(3)(4)为课前做好的,但课上老师要求有了些变化,实际上机的代码在下面BBB中AAA(1)带异步复位的四位二进制减计数器LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY count_1 ISPORT(clk,reset:IN STD_LOGIC;q:OUT STD_LOGIC_VECTOR(3 DOWNTO 0));END count_1;ARCHITECTURE a OF count_1 ISSIGNAL q_temp:STD_LOGIC_VECTOR(3 DOWNTO 0);BEGINPROCESS(clk,reset)BEGINIF reset='0' THENq_temp <="1111";ELSIF clk'EVENT AND clk='1' THENq_temp <=q_temp-1;END IF;END PROCESS;q<= q_temp;END a;(2)带异步复位的8421码十进制计数器LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY count_BCD ISPORT(clk,reset:IN STD_LOGIC;q:OUT STD_LOGIC_VECTOR(3 DOWNTO 0));END count_BCD;ARCHITECTURE a OF count_BCD ISSIGNAL q_temp:STD_LOGIC_VECTOR(3 DOWNTO 0); BEGINPROCESS(clk,reset)BEGINIF reset='0' THENq_temp <="0000";ELSIF clk'EVENT AND clk='1' THENIF q_temp="1001" THENq_temp <="0000";ELSE q_temp <=q_temp+1;END IF;END IF;END PROCESS;q<= q_temp;END a;(3)分频器LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY div_12 ISPORT(clk:IN STD_LOGIC;clear:IN STD_LOGIC;clk_out:OUT STD_LOGIC);END div_12;ARCHITECTURE a OF div_12 ISSIGNAL temp:INTEGER RANGE 0 TO 11;BEGINp1:PROCESS(clear,clk)BEGINIF clear='0'THENtemp<=0;ELSIF clk'EVENT AND clk='1' THENIF temp=11 THENtemp<=0;ELSE temp<=temp+1;END IF;END IF;END PROCESS p1;p2:PROCESS(temp)BEGINIF temp<6 THENclk_out<='0';ELSE clk_out<='1';END IF;END PROCESS p2;END a;(4)带异步复位的四位环形计数器LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY ring ISPORT(clk,reset:IN STD_LOGIC;countout:OUT STD_LOGIC_VECTOR(3 DOWNTO 0)); END ring;ARCHITECTURE behave OF ring ISSIGNAL nextcount:STD_LOGIC_VECTOR(3 DOWNTO 0); BEGINPROCESS(clk,reset) --0001-0010-0100-1000-0001 BEGINIF reset='0' THEN nextcount<="0001";ELSIF clk'EVENT AND clk='1' THENCASE nextcount ISWHEN"0001"=> nextcount<="0010"; WHEN"0010"=> nextcount<="0100"; WHEN"0100"=> nextcount<="1000";WHEN OTHERS=> nextcount<="0001";END CASE;END IF;END PROCESS;countout<=nextcount;END behave;BBBLIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY count_BCD ISPORT(clk,reset:IN STD_LOGIC;q:OUT STD_LOGIC_VECTOR(3 DOWNTO 0));END count_BCD;ARCHITECTURE a OF count_BCD ISSIGNAL q_temp:STD_LOGIC_VECTOR(3 DOWNTO 0); BEGINPROCESS(clk,reset)BEGINIF reset='1' THENq_temp <="0000";ELSIF clk'EVENT AND clk='1' THENIF q_temp="1001" THENq_temp <="0000";ELSE q_temp <=q_temp+1;END IF;END IF;END PROCESS;q<= q_temp;END a;LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY ring ISPORT(clk,reset:IN STD_LOGIC;--clk_out:out STD_LOGIC;countout:OUT STD_LOGIC_VECTOR(3 DOWNTO 0)); END ring;ARCHITECTURE behave OF ring ISSIGNAL nextcount:STD_LOGIC_VECTOR(3 DOWNTO 0); SIGNAL temp:STD_LOGIC;BEGINp1:PROCESS(clk)V ARIABLE count:integer range 0 to 25000000;BEGINIF( clk'EVENT AND clk='1' )THENIF (count=25000000) THENcount:=0;temp<=not temp;ELSE count:=count+1;END IF;END IF;END PROCESS p1;--clk_out<=temp;p2:PROCESS(temp,reset) --0001-0010-0100-1000-0001 BEGINIF reset='1' THEN nextcount<="0001";ELSIF temp'EVENT AND temp='1' THENCASE nextcount ISWHEN"0001"=> nextcount<="0010"; WHEN"0010"=> nextcount<="0100"; WHEN"0100"=> nextcount<="1000";WHEN OTHERS=> nextcount<="0001";END CASE;END IF;END PROCESS p2;countout<=nextcount;END behave;LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY div_12new ISPORT(clk:IN STD_LOGIC;clear:IN STD_LOGIC;clk_out:OUT STD_LOGIC);END div_12new;ARCHITECTURE a OF div_12new ISSIGNAL temp:STD_LOGIC;BEGINPROCESS(clear,clk)V ARIABLE count:integer range 0 to 5;BEGINif (clear='1') thencount:=0;ELSIF( clk'EVENT AND clk='1' )THENIF (count=5) THENcount:=0;temp<=not temp;ELSE count:=count+1;END IF;END IF;END PROCESS;clk_out<=temp;END a;实验四:这个稍有难度,而且书上没有多少参考代码,仔细研究哦~(1)数码管显示012345library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity nixietube1 isport(clk: in std_logic;partout:out std_logic_vector(6 downto 0);catout: out std_logic_vector(5 downto 0));end nixietube1;architecture a of nixietube1 issignal part: std_logic_vector(6 downto 0);signal cat: std_logic_vector(5 downto 0);signal tempclk: std_logic;signal count: integer range 0 to 50000;beginp1:process(clk)beginif(clk'event and clk='1')thenif count=50000 thencount<=0;tempclk<= not tempclk;elsecount<=count+1;end if;end if;end process p1;p2:process(tempclk)beginif(tempclk'event and tempclk='1')thencase cat iswhen"111110"=> cat<="011111";part<="1111110"; --0 when"011111"=> cat<="101111";part<="0110000"; --1 when"101111"=> cat<="110111";part<="1101101"; --2 when"110111"=> cat<="111011";part<="1111001"; --3 when"111011"=> cat<="111101";part<="0110011"; --4 when"111101"=> cat<="111110";part<="1011011"; --5 when others => cat<="011111";part<="1111110"; --0 end case;end if;end process p2;catout<=cat;partout<=part;end a;(2)数码管滚动显示012345library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity shiyan12new2 isport(clk: in std_logic;partout:out std_logic_vector(6 downto 0);catout: out std_logic_vector(5 downto 0));end shiyan12new2;architecture a of shiyan12new2 issignal part: std_logic_vector(6 downto 0);signal cat: std_logic_vector(5 downto 0);signal number: std_logic_vector(5 downto 0); signal tempclk: std_logic;--a clk(div 1)signal move: std_logic;--a clk(div 2)beginp1:process(clk)--div 1 (cat 0-5)variable count:integer range 0 to 50000:=0;beginif(clk'event and clk='1')thenif(count=50000)thencount:=0;tempclk<= not tempclk;elsecount:=count+1;end if;end if;end process p1;p2:process(tempclk)beginif tempclk'event and tempclk='1' thencase cat iswhen"011111"=>cat<="101111"; when"101111"=>cat<="110111"; when"110111"=>cat<="111011"; when"111011"=>cat<="111101"; when"111101"=>cat<="111110";when others =>cat<="011111";end case;end if;end process p2;catout<=cat;p3:process(clk)--div 2 (one cat and change) about 1Hz variable count:integer range 0 to 25000000:=0; beginif (clk'event and clk='1') thenif (count=25000000) thencount:=0;move<=not move;elsecount:=count+1;end if;end if;end process p3;p4:process(tempclk,move)--make numbersvariable judge1:integer range 0 to 1:=0;-- 1 when "move" come variable judge2:integer range 0 to 1:=0;beginif (move'event and move='1') thenjudge1:=1;end if;if (tempclk'event and tempclk='1') thenif (judge1=0) then--when move donnot comecase number iswhen"011111"=>number<="101111"; when"101111"=>number<="110111"; when"110111"=>number<="111011"; when"111011"=>number<="111101"; when"111101"=>number<="111110";when others =>number<="011111";end case;judge2:=0;elsecase number iswhen"011111"=>number<="110111"; when"101111"=>number<="111011"; when"110111"=>number<="111101"; when"111011"=>number<="111110"; when"111110"=>number<="101111";when others =>number<="011111";end case;judge2:=1;end if;end if;if judge2=1 thenjudge1:=0;end if;end process p4;p5:process(number)begincase number iswhen"011111"=>part<="1111110";when"101111"=>part<="0110000"; when"110111"=>part<="1101101";when"111011"=>part<="1111001";when"111101"=>part<="0110011";when"111110"=>part<="1011011";when others =>part<="1111110";end case;end process p5;partout<=part;end a;(3)数码管滚动显示012345,且用全灭的数码管填充右边,直至全灭library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity shiyan12new3 isport(clk: in std_logic;partout:out std_logic_vector(6 downto 0);catout: out std_logic_vector(5 downto 0));end shiyan12new3;architecture a of shiyan12new3 issignal part: std_logic_vector(6 downto 0);signal cat: std_logic_vector(5 downto 0);signal number: std_logic_vector(5 downto 0);signal tempclk: std_logic;--a clk(div 1)signal move: std_logic;--a clk(div 2)beginp1:process(clk)--div 1 (cat 0-5)variable count:integer range 0 to 50000 :=0;beginif(clk'event and clk='1')thenif(count=50000)thencount:=0;tempclk<= not tempclk;elsecount:=count+1;end if;end if;end process p1;p2:process(tempclk)variable count: integer range 0 to 11;variable temp:std_logic_vector(5 downto 0);beginif (move'event and move='1')thenif count=11 thencount:=0;elsecount:=count+1;end if;end if;if tempclk'event and tempclk='1' thencase temp iswhen"011111"=>temp:="101111"; when"101111"=>temp:="110111"; when"110111"=>temp:="111011"; when"111011"=>temp:="111101"; when"111101"=>temp:="111110";when others =>temp:="011111";end case;end if;case count iswhen 0 =>cat<=(temp or "000000");--cat is active low when 1 =>cat<=(temp or "000001");when 2 =>cat<=(temp or "000011");when 3 =>cat<=(temp or "000111");when 4 =>cat<=(temp or "001111");when 5 =>cat<=(temp or "011111");when 6 =>cat<=(temp or "111111");when 7 =>cat<=(temp or "111110");when 8 =>cat<=(temp or "111100");when 9 =>cat<=(temp or "111000");when 10=>cat<=(temp or "110000");when 11=>cat<=(temp or "100000");end case;catout<=cat;end process p2;p3:process(clk) --div 2 (one cat and change)about 1Hzvariable count:integer range 0 to 25000000:=0;beginif (clk'event and clk='1') thenif (count=25000000) thencount:=0;move<=not move;elsecount:=count+1;end if;end if;end process p3;p4:process(tempclk,move)--make numbersvariable judge1:integer range 0 to 1:=0;-- 1 when "move" come variable judge2:integer range 0 to 1:=0;beginif (move'event and move='1') thenjudge1:=1;end if;if (tempclk'event and tempclk='1') thenif (judge1=0) then--when move donnot comecase number iswhen"011111"=>number<="101111"; when"101111"=>number<="110111"; when"110111"=>number<="111011"; when"111011"=>number<="111101"; when"111101"=>number<="111110";when others =>number<="011111";end case;judge2:=0;elsecase number iswhen"011111"=>number<="110111"; when"101111"=>number<="111011"; when"110111"=>number<="111101"; when"111011"=>number<="111110"; when"111110"=>number<="101111";when others =>number<="011111";end case;judge2:=1;end if;end if;if judge2=1 thenjudge1:=0;end if;end process p4;p5:process(number)begincase number is when"011111"=>part<="1111110"; when"101111"=>part<="0110000"; when"110111"=>part<="1101101"; when"111011"=>part<="1111001"; when"111101"=>part<="0110011"; when"111110"=>part<="1011011"; when others =>part<="1111110"; end case;end process p5;partout<=part;end a;。
二位全减器VHDL设计

根据一位二进制全减器的工作原理, 可得其真值表为 (如下: cin 表示低位向本位借位。 cout 表示本位向高位借位) ain 0 0 0 0 1 1 1 1 bin 0 0 1 1 0 0 1 1 cin 0 1 0 1 0 1 0 1 cout 0 1 1 1 0 0 0 1 sum 0 1 1 0 1 0 0 1
u2 : h_suber PORT MAP(a=>e, b=>cin, co=>f,so=>sum); u3 : or2a PORT MAP(a=>d, b=>f,c=>cout); END ARCHITECTURE fd1 ; ///////////////////////////////////////////////////////////////////////////////// LIBRARY IEEE; ——二位全减器 USE IEEE.STD_LOGIC_1164.ALL; ENTITY f_2_suber IS PORT ( A,B: IN STD_LOGIC_VECTOR(1 DOWNTO 0); C0:IN STD_LOGIC; S: OUT STD_LOGIC_ VECTOR(1 DOWNTO 0); C:OUT STD_LOGIC ); END ENTITY f_2_suber; ARCHITECTURE one OF f_2_suber IS COMPONENT f_suber ——调用一位全减器声明语句 PORT (ain,bin,cin : IN STD_LOGIC; cout,sum : OUT STD_LOGIC ); END COMPONENT ; SIGNAL S1,C1,S2: STD_LOGIC; BEGIN U1: f_suber PORT MAP ( ain=>A(0),bin=>B(0),cin=>C0,cout=>C1,sum=>S1 ); U2: f_suber PORT MAP ( ain=>A(1),bin=>B(1),cin=>C1,cout=>C,sum=>S2 ); S<=S2&S1; END ARCHITECTURE one ;
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 isport( a,b,c,d : in std_logic;s0,s1,s2,s3 : in std_logic; y : outstd_logic); end entity mux41a;architecture one of mux41a issignal s0_3 : std_logic_vector(3 downto 0); begins0_3=s0s1s2s3;y=a when s0_3=0111 else b when s0_3=1011 else c whens0_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 cyinb图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 isport( x,y: in std_logic;diff,s_out: out std_logic); end entityh_suber;architecture hs1 of h_suber is begindiff = x xor (not y);s_out = (not x) and y;end architecture hs1;--解(1.2):采用例化实现图4-20的1位全减器library ieee; --1位二进制全减器顺层设计描述 useieee.std_logic_1164.all; entity f_suber isport(xin,yin,sub_in: in std_logic;sub_out,diff_out: outstd_logic); end entity f_suber;architecture fs1 of f_suber iscomponent h_suber --调用半减器声明语句port(x, y: instd_logic; diff,s_out: out std_logic); end component;signal a,b,c: std_logic; --定义1个信号作为内部的连接线。
vhdl 四位流水线乘法器

实验二四位流水线乘法器一、实验目的1.了解四位并行乘法器的原理。
2.了解四位并行乘法器的设计思想和流水线的实现方法。
3.掌握用VHDL 语言实现基本二进制运算的方法。
二、实验内容与要求通过开关和键盘输入两组4BIT的二进制数据,按照二进制加法器原理进行加和,求出和及进位,并通过LED显示灯输出显示,完成编译、综合、适配、仿真、实验箱上的硬件测试。
三、实验原理流水线结构的并行乘法器的最大有点就是速度快,尤其实在连续输入的乘法器中,可以达到近乎单周期的运算速度。
流水线乘法器是组合逻辑电路实现无符号数乘法的方法上发展而来的。
其关键是在组合逻辑电路的基础上插入寄存器。
假如有被乘数A 和乘数B,首先用A 与B 的最低位相乘得到S1,然后再把A 左移1 位与B 的第2 位相乘得到S2,再将A 左移3 位与B 的第三位相乘得到S3,依此类推,直到把B 的所有位都乘完为止,然后再把乘得的结果S1、S2、S3……相加即得到相乘的结果。
需要注意的是,具体实现乘法器是,并不是真正的去乘,而是利用简单的判断去实现,举个简单的例子。
假如A 左移n 位后与B 的第n 位相乘,如果B 的这位为‘1’,那么相乘的中间结果就是A 左移n 位后的结果,否则如果B 的这位为‘0’,那么就直接让相乘的中间结果为0 即可。
带B 的所有位相乘结束后,把所有的中间结果相加即得到A 与B 相乘的结果。
在此基础上插入寄存器即可实现流水线乘法器。
四、实验平台(1)硬件:计算机、GX-SOC/SOPC-DEV-LABCycloneII EP2C35F672C8核心板(2)软件:Quartus II软件PIN_AF8 DATAOUT[4] LED4PIN_AE7 DATAOUT[5] LED5PIN_AF7 DATAOUT[6] LED6PIN_AA11 DATAOUT[7] LED7PIN_AE21 BCD[0] 数码管DP4BPIN_AB20 BCD[1]PIN_AC20 BCD[2]PIN_AF20 BCD[3]PIN_AE20 BCD[4] 数码管DP5BPIN_AD19 BCD[5]PIN_AC19 BCD[6]PIN_AA17 BCD[7]PIN_AA18 BCD[8] 数码管DP6BPIN_W17 BCD[9]PIN_V17 BCD[10]PIN_AB18 BCD[11]六、仿真截图七、硬件实现八、程序代码1---clkgen.vhdlibrary IEEE;-- 1HZuse IEEE.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity clkgen isport (CLK : in std_logic;CLK1HZ: out std_logic);end entity;architecture clk_arch of clkgen issignal COUNT : integer range 0 to 50000000; --50MHZ -->1hz begin -- 50M/1=50000000 PROCESS(CLK)BEGINif clk'event and clk='1' thenIF COUNT= 50000000 thenCOUNT<=0;ELSE COUNT<=COUNT+1;END IF;END IF;END PROCESS;PROCESS(COUNT)BEGINIF COUNT= 5000000 THEN -- 1HZCLK1HZ<='1';ELSE CLK1HZ<='0';END IF;END PROCESS;end architecture;2—BCD-- 输出控制模块,把乘法器的输出转换成BCD码在数码管上显示、-- SCKZ.VHDlibrary IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;entity BIN2BCD isport ( DIN: in std_logic_vector(7 downto 0); ---The input 8bit binaryBCDOUT: out std_logic_vector(11 downto 0)--输出显示, 已转换成BCD码);end entity;architecture arch of BIN2BCD issignal data2,data3,data4 :std_logic_vector(9 downto 0);-- 输出数据缓存signal hundred,ten,unit:std_logic_vector(3 downto 0);--signal bcdbuffer:std_logic_vector(11 downto 0);---2'1111_1001_11=999beginBCDOUT<= bcdbuffer;bcdbuffer(11 downto 8)<=hundred;bcdbuffer(7 downto 4)<=ten;bcdbuffer(3 downto 0)<=unit;get_hundred_value:process(data2)beginDA TA2<="00"&DIN;---get hundred valueif data2>=900 thenhundred<="1001";--9data3<=data2-900;elsif data2>=800 thenhundred<="1000";--8data3<=data2-500;elsif data2>=700 thenhundred<="0111";--7data3<=data2-700;elsif data2>=600 thenhundred<="0110";--6data3<=data2-600;elsif data2>=500 thenhundred<="0101";--5data3<=data2-500;elsif data2>=400 thenhundred<="0100";--4data3<=data2-400;elsif data2>=300 thenhundred<="0011";--3data3<=data2-300;elsif data2>=200 thenhundred<="0010";--2data3<=data2-200;elsif data2>=100 thenhundred<="0001";--1data3<=data2-100;else data3<=data2;hundred<="0000";end if;end process; ---get_thousand_valueget_tens_value:process(data3) begin---get tens placeif data3>=90 thenten<="1001";--9data4<=data3-90;elsif data3>=80 thenten<="1000";--8data4<=data3-50;elsif data3>=70 thenten<="0111";--7data4<=data3-70;elsif data3>=60 thenten<="0110";--6data4<=data3-60;elsif data3>=50 thenten<="0101";--5data4<=data3-50;elsif data3>=40 thenten<="0100";--4data4<=data3-40;elsif data3>=30 thenten<="0011";--3data4<=data3-30;elsif data3>=20 thenten<="0010";--2data4<=data3-20;elsif data3>=10 thenten<="0001";--1data4<=data3-10;else data4<=data3;ten<="0000";end if;end process; ---get_ten_valueget_unit_value:process(data4)begin--unit's orderif (data4>0) thenunit<=data4(3 downto 0);else unit<="0000";end if;end process;end arch;3 multi4b --------------------------------------------------------------------------------/ -- DESCRIPTION : Signed mulitplier:-- AIN (A) input width : 4-- BIN (B) input width : 4-- Q (data_out) output width : 8-- 并行流水乘法器--------------------------------------------------------------------------------/--10 × 9 = 90-- 1 0 1 0-- 1 0 0 1 =-- --------------- 1 0 1 0-- 0 0 0 0 --partial products-- 0 0 0 0-- 1 0 1 0-- -------------------- 1 0 1 1 0 1 0--parallel : process all the inputs at the same time--pipeline : use several stages with registers to implement it----关键思想,插入寄存器library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;entity multi4b isport ( CLK: in STD_LOGIC; ---system clockAIN: in STD_LOGIC_VECTOR (3 downto 0); ---one inputBIN: in STD_LOGIC_VECTOR (3 downto 0);-- the other inputdata_out: out STD_LOGIC_VECTOR (7 downto 0)---the result ---make sure the biggest value ,i,e. 1111x1111=1110_0001 can be held in the register );end multi4b;architecture multi_arch of multi4b issignal A,B :std_logic_vector(3 downto 0); --input register---registers to hold the result of the first processing---registers added to make use of pipeline, the 1st stagesignal A_MULT_B0: STD_LOGIC_VECTOR (3 downto 0);signal A_MULT_B1: STD_LOGIC_VECTOR (3 downto 0);signal A_MULT_B2: STD_LOGIC_VECTOR (3 downto 0);signal A_MULT_B3: STD_LOGIC_VECTOR (3 downto 0);---register to hold the result of the multipliersignal C_TEMP : STD_LOGIC_VECTOR (7 downto 0);beginPROCESS(CLK,AIN,BIN)beginif CLK'EVENT AND CLK='1' THEN-- multiplier operand inputs are registeredA<= AIN;B<= BIN;-----------------Fist stage of the multiplier------------------here we get the axb(0),axb(1),axb(2),axb(3),i.e.partial products---put them into the responding registersA_MULT_B0(0) <= A (0) and B (0);----- multi 1 , get the a(0) and b(0), & put it into the register A_MULT_B0(0)A_MULT_B0(1) <= A (1) and B (0);A_MULT_B0(2) <= A (2) and B (0);A_MULT_B0(3) <= A (3) and B (0);--10 × 9 = 90-- 1 0 1 0-- 1 0 0 1 =-- --------------- 0 0 0 0 1 0 1 0-- 0 0 0 0 0 0 0 0 --partial products-- 0 0 0 0-- 1 0 1 0-- -------------------- 1 0 1 1 0 1 0A_MULT_B1(0) <= A (0) and B (1);A_MULT_B1(1) <= A (1) and B (1);A_MULT_B1(2) <= A (2) and B (1);A_MULT_B1(3) <= A (3) and B (1);A_MULT_B2(0) <= A (0) and B (2);A_MULT_B2(1) <= A (1) and B (2);A_MULT_B2(2) <= A (2) and B (2);A_MULT_B2(3) <= A (3) and B (2);A_MULT_B3(0) <= A (0) and B (3);A_MULT_B3(1) <= A (1) and B (3);A_MULT_B3(2) <= A (2) and B (3);A_MULT_B3(3) <= A (3) and B (3);end if;end process;--------------------Second stage of the multiplier---------------add the all the partial products ,then get the result of the multiplier C_TEMP<=( "0000" & A_MULT_B0 )+( "000"& A_MULT_B1 &'0' )+( "00" & A_MULT_B2 & "00" )+( '0'&A_MULT_B3 & "000" );--build a signal register output---输出寄存,利于实现流水data_out <= C_TEMP; --output registerend multi_arch;九、实验总结。
采用VHDL层次化文件设计一个四位全加器

采用VHDL层次化文件设计一个四位全加器一、实训目的1.巩固VHDL层次化文件设计方法。
2.培养应用VHDL层次化文件设计法的技能。
二、实训器材计算机与Quartus Ⅱ工具软件。
三、实训指导(一)实训原理4位二进制加法器由4个全加器构成,而全加器又由一个半加器和一个或门构成,半加器的真值表如表5-1所示:表5-1 半加器的真值表半加器的逻辑表达式为:so=NOT(a XOR(NOT b))co=a AND b一位全加器的真值表如表5-2所示:表5-2 一位全加器的真值表(二)实训步骤1.电路模块划分根据算法分析,4位二进制加法器可由4个全加器构成,画出其原理方框图。
全加器原理方框图如图5-1所示。
而每个全加器又可划分为一个半加器和一个或门这两个更小的模块,画出其原理方框图。
4位二进制加法器原理方框图如图5-2所示。
图5-1 一位全加器原理方框图图5-2 4位二进制加法器原理框图2.设计底层设计文件(1)设计半减器文件halfadd.vhd。
(2)设计或门电路文件orgate.vhd。
(3)设计全加器电路文件fulladd.vhd,其中把半加器和或门电路文件作为元件调用。
3.设计顶层设计文件设计顶层设计文件add4.vhd,其中把全加器文件作为元件调用。
VHDL代码如下:halfadd.vhd文件代码如下:ENTITY halfadd ISPORT(a,b:IN BIT;so,co:OUT BIT);END halfadd;ARCHITECTURE a OF halfadd ISBEGINPROCESS(a,b)BEGINso<=NOT(a XOR(NOT b)) AFTER 10ns; co<=a AND b AFTER 10 ns;END PROCESS;END a;orgate.vhd文件代码如下:ENTITY orgate ISPORT(a1,b1:IN BIT;o:OUT BIT);END orgate;ARCHITECTURE a OF orgate ISBEGINo<=a1 OR b1;END a;fulladd.vhd文件代码如下:ENTITY fulladd ISPORT(i1,i2,c_in:IN BIT;fs,c_out:OUT BIT);END fulladd;ARCHITECTURE a OF fulladd ISSIGNAL temp_s,temp_c1,temp_c2:BIT; COMPONENT halfaddPORT(a,b:IN BIT;so,co:OUT BIT);END COMPONENT;COMPONENT orgatePORT(a1,b1:IN BIT;o:OUT BIT);END COMPONENT;BEGINU0:halfadd PORT MAP(i1,i2,temp_s,temp_c1);U1:halfadd PORT MAP(temp_s,c_in,fs,temp_c2);U2:orgate PORT MAP(temp_c1,temp_c2,c_out);END a;add4.vhd文件代码如下:ENTITY add4 ISPORT(a,b:IN BIT_VECTOR(3 DOWNTO 0);cin:IN BIT;so:OUT BIT_VECTOR(3 DOWNTO 0);co:OUT BIT);END add4;ARCHITECTURE a OF add4 ISSIGNAL temp_co0,temp_co1,temp_co2:BIT;COMPONENT fulladd ISPORT(i1,i2,c_in:IN BIT;fs,c_out:OUT BIT);END COMPONENT;BEGINU0:fulladd PORT MAP(a(0),b(0),cin,so(0),temp_co0);U1:fulladd PORT MAP(a(1),b(1),temp_co0,so(1),temp_co1); U2:fulladd PORT MAP(a(2),b(2),temp_co1,so(2),temp_co2); U3:fulladd PORT MAP(a(3),b(3),temp_co2,so(3),co);END a;1.编译顶层设计文件把以上各个模块的VHDL设计文件放入同一个文件夹中,以顶层文件建立工程,直接编译顶层文件同时也就编译各个底层模块文件。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
将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;仿真图形:。