本文共 4082 字,大约阅读时间需要 13 分钟。
计算机组成原理实验项目要求之一,使用Quartus II的VHDL语言制作一个 4位加法器和4位乘法器,并烧到试验箱中进行测试。
关于我所使用的试验箱DICE-E213的部分介绍请参照
|
|-- and4a
|-- ls283
|-- mul4p
>>> 需要特别注意的是上述每一个都是一个工程,完成以后需要进行编译;在主程序中需要将上面两个模块引入,才能正常调用。
Library ieee; Use ieee.std_logic_1164.all;Use ieee.std_logic_unsigned.all;Entity and4a is Port(a:in std_logic_vector(3 downto 0); en:in std_logic; r:out std_logic_vector(3 downto 0));End and4a;Architecture behave of and4a isBegin Process(en,a(3 downto 0)) Begin If (en='1') then r<=a; Else r<="0000"; End if; End process;End behave;
Library ieee;Use ieee.std_logic_1164.all;Use ieee.std_logic_unsigned.all;Entity ls283 is Port (o1,o2:in std_logic_vector(3 downto 0); res:out std_logic_vector(4 downto 0));End ls283;Architecture behave of ls283 isBegin Process(o1,o2) Begin res<=('0'&o1)+('0'&o2); End process;End behave;
这部分是主程序
Library ieee; Use ieee.std_logic_1164.all;Use ieee.std_logic_unsigned.all;Entity mul4p is Port (op1,op2:in std_logic_vector(3 downto 0); result:out std_logic_vector(7 downto 0));End mul4p;Architecture count of mul4p is component and4a port (a:in std_logic_vector(3 downto 0); en:in std_logic; r:out std_logic_vector(3 downto 0));End component;Component ls283 port (o1,o2:in std_logic_vector(3 downto 0); res:out std_logic_vector(4 downto 0));End component;Signal sa:std_logic_vector(3 downto 0);Signal sb:std_logic_vector(4 downto 0);Signal sc:std_logic_vector(3 downto 0);Signal sd:std_logic_vector(4 downto 0);Signal se:std_logic_vector(3 downto 0);Signal sf:std_logic_vector(3 downto 0);Signal sg:std_logic_vector(3 downto 0);--signal tmp1:std_logic;Begin sg<=('0'&sf (3 downto 1)); --tmp1<=op1(1); u0:and4a port map(a=>op2,en=>op1(1),r=>se); U1:and4a port map(a=>op2,en=>op1(3),r=>sa); U2:ls283 port map(o1=>sb(4 downto 1),o2=>sa,res=>result(7 downto 3)); U3:and4a port map(a=>op2,en=>op1(2),r=>sc); U4:ls283 port map(o1=>sc,o2=>sd(4 downto 1),res=>sb); u5:ls283 port map(o1=>sg,o2=>se,res=>sd); u6:and4a port map(a=>op2,en=>op1(0),r=>sf); result(0)<=sf(0); result(1)<=sd(0); result(2)<=sb(0); --result(7 downto 0)<="00000000";End count;
将上面两个模块引入主程序工程的方法是:
如图所示将另外两个工程的文件夹引入主程序工程。
编译!
这个过程不在赘述,过程中如出现问题,可以参考
参照上图不难看出,我使用的引脚组是
输入1:JP3(2,4,5,6)
输入2:JP4(3,4,5,6)
输出:JP6(1,2,3,4,5,6,7,8)
测试之前说说明一下:对于本实验箱:按钮输入的一组:按下0弹出1,对于推动开关输入的一组:推上1推下0,输出灯泡:亮0灭1。
测试试验箱的流程在有 详细介绍。
此时上排按钮表示1111,下派开关输入量为0000,结果0000 0000 灯全亮。
此时上排1111,下排1111,结果应为225 即 11100001
此时上排1000,下排1100,结果应为3,即 0000 0011
当上排按钮全部按下,上排0000,无论下派为何值,结果都为0000
操作和乘法器一样,不再赘述。
只做一点简要说明:
结构:halfadd-->fulladd-->adder4
贴出代码
Library ieee;Use ieee.std_logic_1164.all;Entity halfadd isPort(a,b:in std_logic; S,c:out std_logic);end halfadd;Architecture add of halfadd isbeginS<=a xor b;c<=a and b;end;
Library ieee;Use ieee.std_logic_1164.all;Entity fulladd isPort(a,b,cin:in std_logic; S,c:out std_logic);end fulladd;Architecture add of fulladd issignal m,n,k:std_logic;component halfadd isPort(a,b:in std_logic; S,c:out std_logic);end component;beginU0:halfadd port map(a,b,m,n);U1:halfadd port map(m,cin,S,k);c<=n or k;end;
Library ieee;Use ieee.std_logic_1164.all;Entity adder4 isPort(a,b:in std_logic_vector(3 downto 0); cin:in std_logic; S:out std_logic_vector(3 downto 0); co:out std_logic);end adder4;Architecture add of adder4 issignal c:std_logic_vector(3 downto 0);component fulladd isPort(a,b,cin:in std_logic; S,c:out std_logic);end component;beginU0:fulladd port map(a(0),b(0),cin,S(0),c(0));U1:fulladd port map(a(1),b(1),c(0),S(1),c(1));U2:fulladd port map(a(2),b(2),c(1),S(2),c(2));U3:fulladd port map(a(3),b(3),c(2),S(3),co);end;
最后说一点:工程路径不能有中文。
VHDL 4位加法器 VHDL 4位乘法器 公共下载区
备用链接:
在试验过程中如有错误,欢迎留言,讨论,也欢迎指出我的错误。
转载地址:http://bnoen.baihongyu.com/