VHDL學習----多埠通用暫存器堆RF
阿新 • • 發佈:2021-01-12
暫存器堆(register file)是CPU中多個暫存器組成的陣列,通常由快速的靜態隨機讀寫儲存器(SRAM)實現。這種RAM具有專門的讀埠與寫埠,可以多路併發訪問不同的暫存器。
本人所寫的RF暫存器堆,其中包含4個8位暫存器(R0、R1、R2、R3),有三個控制埠。其中兩個埠控制讀操作,一個埠控制寫操作,三個埠可同時操作。RD1、RD0選擇從A埠讀出的暫存器,RS1、RS0選擇從B埠讀出的暫存器,WR1、WR0選擇被寫入的暫存器。WRD 控制寫操作。當WRD = 0時,禁止寫操作;當WRD = 1 時,將來自ER暫存器的資料寫入由WR1、WR0 選中的暫存器。
程式碼如下:
library ieee; use ieee.std_logic_1164.all; entity RF is port( RS:in std_logic_vector(1 downto 0); RD:in std_logic_vector(1 downto 0); WR:in std_logic_vector(1 downto 0); WRD:in std_logic; D_ER_i:in std_logic_vector(7 downto 0); D_RA_o:out std_logic_vector(7 downto 0); D_RB_o:out std_logic_vector(7 downto 0) ); end RF; architecture behave of Rf is signal R0: std_logic_vector(7 downto 0); signal R1: std_logic_vector(7 downto 0); signal R2: std_logic_vector(7 downto 0); signal R3: std_logic_vector(7 downto 0); begin process(WR) --write begin if(WRD='1') then case WR is when "00"=> R0<=D_ER_i; when "01"=> R1<=D_ER_i; when "10"=> R2<=D_ER_i; when "11"=> R3<=D_ER_i; when others=>null; end case; end if; end process; process(RD) ---read A begin case RD is when "00"=> D_RA_o<=R0; when "01"=> D_RA_o<=R1; when "10"=> D_RA_o<=R2; when "11"=> D_RA_o<=R3; when others=>null; end case; end process; process(RS) ---read B begin case RS is when "00"=> D_RB_o<=R0; when "01"=> D_RB_o<=R1; when "10"=> D_RB_o<=R2; when "11"=> D_RB_o<=R3; when others=>null; end case; end process; end behave;
隨後進行模擬,驗證讀寫功能,波形圖如下:
此時讀寫控制訊號WRD為1,表明可以進行寫操作。WR為01,選中R1,向暫存器R1寫入10101010。RD、RS同時為01,選中R1暫存器,從R1中讀取資料。可以看出,D_RA_o與D_RB_o讀出的資料相同,均為10101010,既驗證了D_ER_i中的資料成功寫入R1中,也驗證了A、B兩埠成功讀出R1中資料。
再次進行對比模擬,將RS改為11,即B埠從R3讀取資料。由於並未向R3中寫入資料,因此B埠讀不出資料,而A埠仍然正常讀出資料。說明讀寫功能均正確:)