1、精品简单CPU系统设计及实现简单CPU系统设计及实现数字电路综合实验报告简单CPU系统设计与实现 1设计课题的任务要求设计一个简单的CPU及其外部模块,能够完成机器代码的输入、存储、执行和结果显示。基本要求:指令要求至少有空指令,跳转指令,加法指令,存取数据指令。 利用自己设计的指令系统编写一段机器语言,可以完成求两个小于256的数的最大公约数。机器语言通过拨码开关和按键逐条输入,通过程序执行开关控制程序执行,最后在数码管或LED灯上显示结果。提高要求:设计并实现其它指令。 2系统设计2.1 设计思路设计的简易CPU系统主要由指令存储器(可以通过拨码和按键读入指令)、控制器、内部寄存器、内存以
2、及ALU构成。设计过程重点考虑如下事项:读/写寄存器,读/写存储器以及执行指令。通过执行读入的指令以及编写好的求两个数的最大公约数的程序来测试该系统的功能。2.2 总体框图2.2.1 系统总体框图由设计思路,可绘制如下的简单CPU系统总体框图:最终在quartusII中连接好的CPU系统图如下所示(其中的ACC属于ALU部件):2.2.2 系统状态转移图如上图,系统共有3个状态:S0、S1、S2。分别为S0:在这一状态,通过拨码和按键录入要执行的指令。S1:在这一状态,CPU执行程序,即S0录入的指令。S2:在这一状态,CPU执行完全部程序,等待再次执行或等待录入指令。2.2.3 求解最大公约
3、数的程序框图设a,b为给定的两个整数,用辗转相减法求解他们的最大公约数: 2.3 分块设计2.3.1 DIV(分频器) DIV将电路板所提供的时钟分频,产生CPU工作所需要的时钟以及数码管扫描所需要的时钟。 2.3.2 MCU (控制器) MCU 用来产生系统内部所有寄存器、运算单元所需的控制信号,以及执行各条指令所需要的微操作。它是整个系统设计的核心所在。 2.3.3 ALU(算术逻辑运算单元)ALU完成数据的算术和逻辑运算。ALU有5个输入端和2个输出端,其中一个操作数固定来自累加器acc(具体编程时可用变量或信号表示),另一个操作数来自端口mbr_in(通过数据总线接到寄存器MBR)。参
4、加运算的操作数在ALU中进行规定的操作运算,运算结束后,一方面将结果送至累加器,同时将操作结果的特征状态送标志寄存器。2.3.4 MAR (地址寄存器) MAR 存放着要被读取或写入的内存单元地址。其中,读操作时,CPU从内存读。而写操作时,CPU把数据写入内存。2.3.5 MBR (缓冲寄存器) MBR存放了将要存储到内存的数据或者从内存中读取的最新数据。MBR连接到系统总线中的地址总线。2.3.6 RAM(随机存取存储器) RAM 有着相对独立的输入输出管脚,在本系统设计中它作为内存使用。它不是CPU内部的寄存器,不属于CPU的一部分。但要测试所设计的CPU的性能,需要把RAM加进系统设计
5、中。 2.3.7 SMG (数码管显示 )SMG负责将运算结果转化为BCD码形式,然后用数码馆显示。 3 仿真波形及波形分析根据波形图可以看出状态机的运行,其中一个状态占两个时钟周期。该仿真相当于CPU执行指令的时序图,反映了程序运行情况。还包括了辗转相减法的仿真,和rst的清零仿真。可以看出该CPU系统的仿真结果正确,达到下载的要求。4 源程序(含注释)各个部件对应的源程序如下(最后的连接是用图形连接完成的):4.1 DIV(分频器) library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity
6、 DIV isport( clk_in:in std_logic; clk_out1:out std_logic; clk_out2:out std_logic);end DIV;architecture behave of DIV issignal tmp:std_logic_vector(15 downto 0);beginprocess(clk_in,tmp)begin if(clk_inevent and clk_in=1)then tmp=tmp+1; end if; clk_out1=tmp(0); clk_out2ctr=00010000000;status:=status+1;
7、 -MBRctr=00001000000;status:=status+1; -MAR -DECODING case opcode is when0001=status:=00100;ctr=00100000000; -Store -MBRstatus:=00101;ctr=00010000000; -Load -MBRstatus:=00111;ctr=00010000000; -Add -MBRstatus:=01001;ctr=00010000000; -Sub -MBRstatus:=11111;ctr=00000000011; -SHL -ACC-ACCstatus:=11111;c
8、tr=00000000100; -SHR -ACC1 when0111=status:=01011;ctr=00010000000; -And -MBRstatus:=01101;ctr=00010000000; -Or -MBRstatus:=11111;ctr=00000000111; -Not -ACCstatus:=00001;ctrif(cmp=0)then -JumpZ status:=00001;ctr=00000100000; else status:=00001;ctrstatus:=11111;ctrstatus:=00000;ctrctrctr=01000000000;s
9、tatus:=11111; -Store -RAMctr=00000011111;status:=status+1; -Load -BR-MBR;ACCctr=00000000001;status:=11111; -ACCctr=00000010000;status:=status+1; -Add -BRctr=00000000001;status:=11111; -ACCctr=00000010000;status:=status+1; -Sub -BRctr=00000000010;status:=11111; -ACCctr=00000010000;status:=status+1; -
10、And -BRctr=00000000101;status:=11111; -ACCctr=00000010000;status:=status+1; -Or -BRctr=00000000110;status:=11111; -ACCctr=00000010000;status:=status+1; -Mutiply-BRctr=00000001000;status:=11111; -ACCctr=00000100000;status:=00001; -PC-PC+1;MARctrctr=00000000000;status:=00001; end case; end if; end pro
11、cess;end behave;4.3 ALU(算术逻辑运算单元)library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity ALU isport( clk:in std_logic; rst:in std_logic; mbr_in:in std_logic_vector(11 downto 0); mbr_e:in std_logic; ctrl:in std_logic_vector(3 downto 0); overflow:out std_logic; acc:out std_logi
12、c_vector(11 downto 0) );end ALU;architecture behave of ALU isbeginprocess(clk,rst)variable br: std_logic_vector(11 downto 0);variable data_acc:std_logic_vector(11 downto 0);begin if(clkevent and clk=1)then if(rst=1)then accaccdata_acc:=data_acc+br; when0010=data_acc:=data_acc-br; when0011=data_acc:=
13、data_acc(10 downto 0)&0; when0100=data_acc:=0&data_acc(11 downto 1); when0101=data_acc:=data_acc and br; when0110=data_acc:=data_acc or br; when0111=data_acc:=not data_acc; when others=null; end case; end if; acc=data_acc; overflow=data_acc(11);end process;end behave;4.4 MAR (地址寄存器) library ieee;use
14、 ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity MAR isport( clk:in std_logic; rst:in std_logic; mbr_low:in std_logic_vector(7 downto 0); mbr_e:in std_logic; add:in std_logic; mar:out std_logic_vector(7 downto 0);end MAR;architecture behave of MAR isbeginprocess(clk,rst)variable pc:st
15、d_logic_vector(7 downto 0);variable mar_mid:std_logic_vector(7 downto 0);begin if(clkevent and clk=1)then if(rst=1)then pc:=00000000; mar_mid:=00000000; elsif(mbr_e=1)then pc:=mar_mid; mar_mid:=mbr_low; elsif(add=1)then pc:=pc+1; mar_mid:=pc; else null; end if; end if; mar=mar_mid;end process;end be
16、have;4.5 MBR (缓冲寄存器) library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity MBR isport( clk:in std_logic; ram_in:in std_logic_vector(11 downto 0); acc_in:in std_logic_vector(11 downto 0); ram_e:in std_logic; acc_e:in std_logic; rst:in std_logic; mbr:out std_logic_vector(11 d
17、ownto 0);end MBR;architecture behave of MBR isbeginprocess(clk,rst)begin if(clkevent and clk=1)then if(rst=1)then mbr=000000000000; elsif(acc_e=1)then mbr=acc_in; elsif(ram_e=1)then mbr=ram_in; else null; end if; end if;end process;end behave; 4.6 RAM(随机存取存储器) library ieee;use ieee.std_logic_1164.al
18、l;use ieee.std_logic_unsigned.all;entity RAM isport( clk_ram:in std_logic; w_r:in std_logic; code_e:in std_logic; mbr_in:in std_logic_vector(11 downto 0); mar_in:in std_logic_vector(7 downto 0); ram_in:in std_logic_vector(7 downto 0); ram_out:out std_logic_vector(11 downto 0); ram_outt:out std_logic
19、_vector(7 downto 0);end RAM;architecture behave of RAM isbeginprocess(clk_ram)type table is array(0 to 20)of std_logic_vector(11 downto 0);variable ram:table;variable i:integer range 0 to 31:=0;variable j:std_logic:=0;variable code_e1:std_logic;begin if(clk_ramevent and clk_ram=1)then if(code_e=1 an
20、d code_e1=0 and j=0)then ram(i)(11 downto 8):=ram_in(3 downto 0); ram_outt(7 downto 4)=1000; ram_outt(3 downto 0)=ram(i)(11 downto 8); j:=1; elsif(code_e=1 and code_e1=0 and j=1)then ram(i)(7 downto 0):=ram_in(7 downto 0); ram_outt=ram(i)(7 downto 0); j:=0; i:=i+1; elsif(w_r=1)then ram(conv_integer(
21、mar_in):=mbr_in; elsif(w_r=0)then ram_out=ram(conv_integer(mar_in); else null; end if; code_e1:=code_e; end if;end process;end behave; 4.7 SMG (数码管显示 ) library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;use ieee.std_logic_arith.all;entity SMG isport(clk:in std_logic;smg_en:in s
22、td_logic;n:in std_logic_vector(11 downto 0);count:out std_logic_vector(6 downto 0);count_en:out std_logic_vector(5 downto 0);end SMG;architecture behave of SMG issignal a_temp:std_logic_vector(3 downto 0);signal n1,n2,n3:std_logic_vector(3 downto 0);beginp1:process(n)variable a:integer range 0 to 127;variable b,c,d:integer range 0 to 9;begin a:=conv_integer(n); d:=a mod 10; c:=(a/10) mod 10; b:=a/100; n1=conv_std_logic_vector(b,4); n2=conv_std_logic_vector(c,4); n3count_en=011111;a_tempcount_en=101111;a_tempcount_en=110111;a_tempcount_en count count count count=1111001; w
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1