找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 7841|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

我用VHDL語言實(shí)現(xiàn)的簡(jiǎn)單CPU設(shè)計(jì)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
使用VHDL語言編寫的一個(gè)課程設(shè)計(jì),寫了一個(gè)簡(jiǎn)單CPU,包含通用寄存器,PC寄存器,ALU等等,供大家參考



單片機(jī)源程序如下:
  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;
復(fù)制代碼

所有資料51hei提供下載:
9.ALU.rar (5.49 MB, 下載次數(shù): 55)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:258507 發(fā)表于 2019-2-28 17:54 | 只看該作者
bucuo,支持一下。正好學(xué)習(xí)了VHDL,在做畢設(shè)
回復(fù)

使用道具 舉報(bào)

板凳
ID:517723 發(fā)表于 2019-4-21 13:17 | 只看該作者
我最近有個(gè)大作業(yè)也在做這個(gè),但是管腳映射除了奇怪的問題。
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是頂層文件的臨時(shí)變量,F(xiàn)在仿真以后,spo和spo_ram都成功地讀到了ram里面第一行的數(shù)據(jù),但是from_mpmery就是沒有這個(gè)變量,還是一個(gè)空值,使得后續(xù)所有的變量全部沒用了。這到底是什么問題?
回復(fù)

使用道具 舉報(bào)

地板
ID:392858 發(fā)表于 2019-12-25 16:48 | 只看該作者
支持下
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表