找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7069|回复: 3
收起左侧

我用VHDL语言实现的简单CPU设计

[复制链接]
ID:280277 发表于 2018-7-14 18:20 | 显示全部楼层 |阅读模式
使用VHDL语言编写的一个课程设计,写了一个简单CPU,包含通用寄存器,PC寄存器,ALU等等,供大家参考

0.png

单片机源程序如下:
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.std_logic_unsigned.all;
  4. package packexp is
  5.          function calculator(ch : std_logic_vector(2 downto 0);
  6.                             da,db :std_logic_vector(7 downto 0))  
  7.         return std_logic_vector;
  8. end;
  9. package body packexp is
  10.         function calculator(ch : std_logic_vector(2 downto 0); da,db : std_logic_vector(7 downto 0))  return std_logic_vector is
  11.                  begin
  12.                  case(ch) is
  13.                  when "001" =>
  14.                           return ('0' & (da and db));
  15.                  when "010" =>
  16.                           return ('0' & (da or db));
  17.                  when "011" =>
  18.                           return ('0' & (da xor db));          
  19.                  when "100" =>
  20.                           return ('0' & da) + ('0' & db);
  21.                  when "101" =>
  22.                           return ('0' & da(6 downto 0) & '0');
  23.                  when "110" =>
  24.                           return ('0' & '0' & da(7 downto 1));
  25.                  when "111" =>
  26.                           return ('0' & da(7) & da(7 downto 1));
  27.                  when others =>
  28.                           return ("000000000");
  29.                  end case;
  30.         end function calculator;
  31. end;
  32. library ieee;
  33. use ieee.std_logic_1164.all;
  34. use ieee.std_logic_unsigned.all;
  35. use work.packexp.all;
  36. entity ALU is
  37.    port(clk : in std_logic;
  38.              mode : in std_logic_vector(1 downto 0);
  39.              S   : in std_logic_vector(2 downto 0);
  40.                   Cin : in std_logic;
  41.                   D   : in std_logic_vector(7 downto 0);
  42.                   Cout : out std_logic;
  43.                   dataout : out std_logic_vector(7 downto 0));
  44. end entity ALU;                  
  45. architecture mainpart of ALU is
  46.     begin
  47.          process(clk)
  48.          variable A : std_logic_vector(7 downto 0) := (others => '0');
  49.     variable B : std_logic_vector(7 downto 0) := (others => '0');
  50.          variable dataa : std_logic_vector(7 downto 0) := (others => '0');
  51.          variable datab : std_logic_vector(7 downto 0) := (others => '0');
  52.          variable result : std_logic_vector(8 downto 0) := (others => '0');
  53.             begin
  54.                  if(clk'event and clk = '1')
  55.                  then
  56.                          if(S = "000")
  57.                          then
  58.                                   A := (others => '0');
  59.                                   B := (others => '0');
  60.                                   result := (others => '0');
  61.                                   dataout <= result(7 downto 0);
  62.                          else
  63.                                   case(mode) is
  64.                                   when "00" =>
  65.                                                 A := D;
  66.                                                 dataout <= A;
  67.                                   when "01" =>
  68.                                                 B := D;
  69.                                                 dataout <= B;
  70.                                   when "11" | "10" =>
  71.                                                 dataa := A;
  72.                                                 datab := B;
  73.                                                 result := calculator(S,dataa,datab);
  74.                                                 if(S = "100")
  75.                                                 then
  76.                                                          result := result + ("00000000" & Cin);
  77.                                                          Cout <= result(8);
  78.                                                 else
  79.                                                          Cout <= '0';
  80.                                                 end if;
  81.                                                 dataout <= result(7 downto 0);
  82.                                   when others =>
  83.                                                 null;
  84.                                   end case;
  85.                          end if;
  86.                   end if;
  87.          end process;
  88. end mainpart;
复制代码

所有资料51hei提供下载:
9.ALU.rar (5.49 MB, 下载次数: 55)
回复

使用道具 举报

ID:258507 发表于 2019-2-28 17:54 | 显示全部楼层
bucuo,支持一下。正好学习了VHDL,在做毕设
回复

使用道具 举报

ID:517723 发表于 2019-4-21 13:17 | 显示全部楼层
我最近有个大作业也在做这个,但是管脚映射除了奇怪的问题。
file3:MBR port map(CLK=>CLK,RST=>RST,control_signal=>control_signal,from_memory=>spo_ram,from_ACC=>ACC_L,to_memory=>to_memory,MBR_out=>MBR_out,wren=>wren);
file12:RAM1 port map(a=>address(4 downto 0),d=>MBR_out,clk=>CLK,we=>wren,spo=>spo_ram);
其中from_memory是in 变量,spo是out变量,spo_ram是顶层文件的临时变量。现在仿真以后,spo和spo_ram都成功地读到了ram里面第一行的数据,但是from_mpmery就是没有这个变量,还是一个空值,使得后续所有的变量全部没用了。这到底是什么问题?
回复

使用道具 举报

ID:392858 发表于 2019-12-25 16:48 | 显示全部楼层
支持下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表