計算機組成原理實驗報告硬設2016_第1頁
計算機組成原理實驗報告硬設2016_第2頁
計算機組成原理實驗報告硬設2016_第3頁
已閱讀5頁,還剩51頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

《計算機組成原理》課程設計報告年級專業(yè)班級:完成日期:小組成員、組內(nèi)分工及各成員成績學號姓名分工工作量比例成績引言實驗目的在掌握傳統(tǒng)的、順序執(zhí)行的 CPU工作原理基礎上,理解掌握流水CPU的工作原理;掌握流水線的設計思路和方法,尤其是對結(jié)構(gòu)相關(guān)、數(shù)據(jù)相關(guān)和控制相關(guān)的解決思路;理解硬件設計流程,熟悉掌握指令系統(tǒng)的設計方法,并設計簡單的指令系統(tǒng);理解和掌握基于VHDL語言和TEC-CAM件平臺設計模型機的方法,并藉此掌握工程設計的思路和方法。實驗要求基本設計要求參考《16位5級流水無cache實驗CPU課程設計實驗要求》文檔及其VHDL代碼,在理解其思想和方法的基礎上,將其改造成8位的5級流水無cache的實驗CPU包括對指令系統(tǒng)、數(shù)據(jù)通路、各流水段模塊、內(nèi)存模塊等方面的改造。利用 VHDL語言編程實現(xiàn),并在TEC-CA平臺上進行仿真測試。為方便起見,后續(xù)16位5級流水無cache實驗CPU簡記為ExpCPU-16而8位的則記為ExpCPU-8。對于內(nèi)存模塊的改造,參考《計算機組成原理》課程綜合實驗的方法,獨立設計一塊8位的RAM。要求測試減法的溢出標志位,如測試127-(-96)或者-127-96的溢出標志位。額外設計要求時間允許的情況下,進行一些額外的、探索性的改造,可用于加分。例如,利用TEC-CA平臺上的16位RAM來存放8位的指令和數(shù)據(jù);實現(xiàn)一條JRS指令,以便在符號標志位S=1時跳轉(zhuǎn)。需要改寫ID段的控制信息,并改寫IF段;實現(xiàn)一條CMPJDR,SR,offset指令,當比較的兩個數(shù)相等時,跳轉(zhuǎn)到目標地址PC+1+offset;(4)可以探索從外部輸入指令,而不是初始化時將指令“寫死”在RAM中;(5)此5段流水模塊之間,并沒有明顯地加上流水寄存器,可以考慮在不同模塊間加上流水寄存器;(6)探索5段流水帶cache的CPU的設計。各組亦可根據(jù)實際情況來做一些創(chuàng)新性的探索,酌情加分。實驗設備本課程綜合設計中,需要用到的實驗設備如下:PC兩臺;TEC-CA硬件實驗箱一個;QuartusII軟件平臺一個;DebugController軟件一個;JTAG連接線。主要成果實現(xiàn)5級流水無cache的8位實驗CPU,16條簡單的命令執(zhí)行,輸出確認無誤。8位無Cache的5段流水CPI總體設計2.1指令系統(tǒng)的設計(注:要給出指令功能、指令格式、指令列表)constantADD:std_logic_vector(3downto0):="0000";//加法constantSUBB:std_logic_vector(3downto0):="0001";//減法constantANDins:std_logic_vector(3downto0):="0011";//及constantMOV:std_logic_vector(3downto0):="0010";//賦值constantLOAD:std_logic_vector(3downto0):="1000";//load指令constantSTORE:std_logic_vector(3downto0):="1010";//store指令constantLOADH:std_logic_vector(3downto0):="1011"; //高位load指令constantLOADL:std_logic_vector(3downto0):="1001";//低位load指令constantJR:std_logic_vector(3downto0):="0100";//跳轉(zhuǎn)constantJRZ:std_logic_vector(3downto0):="0111";//Z==1跳轉(zhuǎn)constantNOP:std_logic_vector(3downto0):="1100";//空操作constantMOVI:std_logic_vector(3downto0):="1101";//constantNOPIns:std_logic_vector(7downto0):="11000000";2.2 數(shù)據(jù)通路的設計(注:可參考16位CPU然后指出變?yōu)?位時,作了哪些修改)一、數(shù)據(jù)通路圖

五級流水CPU吉構(gòu)圖圖1無cache五級流水線數(shù)據(jù)通路示意圖整個CPU由下邊幾個模塊組成:控制器:在每個時鐘周期給出cpu其它部件的控制信號。運算器:本運算器包括寄存器堆。BusMUX通過控制信號BusSel數(shù)據(jù)的來源,通過Wr來控制送往外部數(shù)據(jù)總線上的數(shù)據(jù)內(nèi)容。AR(地址寄存器):驅(qū)動地址總線。IR(指令寄存器):存放根據(jù)當前PC值取得的指令2.3控制信號的設計(注:要分析控制信號有哪些,最后總結(jié)一個控制信號列表,可參考上個學期綜合實驗參考文檔中控制信號的列表方法)信號含義w_memTofeg?‘1':內(nèi)存數(shù)據(jù)到寄存器‘0':ALU輸出到寄存器wRegEn‘1':允許寫寄存器信號含義:禁止寫寄存器w_memToReg{‘0':內(nèi)存數(shù)據(jù)到寄存器‘1':ALU輸出到寄存器wRegEn‘1':允許寫寄存器‘0':禁止寫寄存器wrMemALUSrcwRegEn&MemToRegALUSrcRS000A0001AB010A10110B100AFFFF101A立即數(shù)ALUOprALUOpr功能0000R+S0001S-R0010RANDS0011RORS0100RXORS0101SHLS0110SHRS0111SARS1000LOADHR1001LOADLR1010MOVIRsetFlag(3位:###)信號含義000Flag_hold001Flag_update010Flag_innerDB011Flag_C0100Flag_C1101Flag_clear其它未定義結(jié)構(gòu)相關(guān)的處理由于我們的設計中不包含緩存, 因此會有取指和訪存的沖突,即結(jié)構(gòu)相關(guān)(見下圖)。1 執(zhí)行 u訪存1取指丄一譯碼一取指 M 譯碼當沖突發(fā)生時,必須先“訪存”,將“取指”延后一個時鐘周期,這樣才能保證指令的正確執(zhí)行。處理結(jié)構(gòu)相關(guān)需要做兩項工作:1)沖突檢測2)取指延后。1)沖突檢測只有執(zhí)行訪存指令(LOAD/STORE時,才會出現(xiàn)沖突。因此,我們在譯碼時產(chǎn)生一個標志是否訪存的信號 wrMem含義如下:wMem意義00寫 內(nèi) 存(STORE01讀內(nèi)存(LOAD1X不占用內(nèi)存通過檢查“訪存階段”的m_wrMer就可確定是否沖突。2)取指延后在每次取指時,若有沖突,則往 IR中寫入空指令(NOP),并保持PC不變,使取指延后一個節(jié)拍。數(shù)據(jù)相關(guān)的處理數(shù)據(jù)相關(guān)是指在執(zhí)行本條指令的過程中,如果用到的操作數(shù)是前面指令的執(zhí)行結(jié)果,則必須等待前面的指令執(zhí)行完成, 并把結(jié)果寫回寄存器或主存之后,本條指令才能繼續(xù)執(zhí)行 [3]。我們米用設置專用數(shù)據(jù)通路(即傍路技術(shù))來解決數(shù)據(jù)相關(guān)問題。但旁路技術(shù)并非一勞永逸。若前一指令為LOAD而后一指令和它數(shù)據(jù)相關(guān),如下圖所示,當下一指令的執(zhí)行階段需要數(shù)據(jù)時,上一指令尚未給出,這種情況是無法用旁路技術(shù)來解決的。

IM圖五級流水LOADIM圖五級流水LOAD數(shù)據(jù)相關(guān)示意圖針對上述情況的一般處理方法是通過插入 bubble使LOA[之后的兩條指令拖后一個節(jié)拍執(zhí)行[4]。但我們感覺這種處理過于繁瑣。我們發(fā)現(xiàn),如果在LOAD譯碼時,后面自動加一條空指令,即增加冗余,就可以避免上述情況的出現(xiàn)。在這種簡化處理后,我們將數(shù)據(jù)相關(guān)劃分為以下三種情況。1?相鄰指令數(shù)據(jù)相關(guān)舉例:INCR0INCR0在此情形下,將上一指令的ALU輸出經(jīng)緩存后直接送回,作為ALU多路選擇器的入口之一,如下圖所示 4——?—1 ? *AU'——1―1IMRegMem彳Reg

具體代碼片段為(forwardingentity.vhd):ifm_wRegEn='1'andm_SA=e_SAthenforwardA<="10";其中m_SA為當前處于訪問內(nèi)存(MEM沖寄存器輸出結(jié)果的索引,e_SA為處于當前執(zhí)行階段中 ALU的輸入寄存器A的索引,倘若兩個索引值相等,則產(chǎn)生相鄰指令數(shù)據(jù)相關(guān)2.中間隔1條指令的兩指令數(shù)據(jù)相關(guān)舉例:INCR0INCR1INCR0在此情形下,將第1條指令的回寫數(shù)據(jù)送回作為ALU多路選擇器的入口之一供第3條指令使用,如下圖所示。IIMLReg—1_1~1AIMLReg 9IMIIMLReg—1_1~1AIMLReg 9IMMem-A>L亠URegReg上Reg—1emMemAReg圖五級流水相隔1條指令的兩指令數(shù)據(jù)相關(guān)示意圖具體代碼片段如下(forwardingentity.vhd)elsifw_wRegEn='1'andw_SA=e_SAthenforwardA<="01";其中w_SA為當前處于回寫階段(WB)中寄存器輸出結(jié)果的索引,e_SA為處于當前執(zhí)行階段中ALU的輸入寄存器A的索引,倘若兩個索引值相等,則產(chǎn)生中間隔1條指令的兩指令數(shù)據(jù)相關(guān)3.中間隔2條指令的兩指令數(shù)據(jù)相關(guān)舉例:INCR0INCR1INCR2INCR0第1和第4條指令數(shù)據(jù)相關(guān),有兩種處理方法。方法1:在第4條指令的譯碼階段作旁路處理,將第1條指令的回寫數(shù)據(jù)作為運算器的輸入送入下一級,如下圖所示。方法2:考慮到讀寫寄存器的時間較短, 因此將寫寄存器的時機改在時鐘下降沿。這樣1、4指令就沒有數(shù)據(jù)相關(guān)了。

IM>LUReg—?―1—*—*—1IM>LUReg—?―1—*—*—1—1Mem—1RegMemIM亡RegRegAAIM—上Reg—1——1—?——*>U|—1—1MemIM 1上RegJ—?Mem圖五級流水相隔2條指令的兩指令數(shù)據(jù)相關(guān)示意圖由于方法2處理較簡潔,因此我們在實現(xiàn)時使用了后者。具體部分代碼如下(ifentity.vhd)process(reset,clk,PCStall)beginifreset='O'thenPC<=x"00";elsifFALLING_EDGE(clk)and(PCStall='0')thenPC<=PCnext;endif;endprocess;可以看到更新PC的時機為FALLING_EDGE控制相關(guān)的處理控制相關(guān)是指因為程序的執(zhí)行方向可能改變而引起的相關(guān)。 當執(zhí)行跳轉(zhuǎn)指令時,就會發(fā)生這種情況。除 JR外,JRZ等條件跳轉(zhuǎn)需要根據(jù)當前狀態(tài)位來決定是否跳轉(zhuǎn),而當前狀態(tài)位是由前面最近的會影響狀態(tài)位的指令(如算術(shù)指令)決定。常規(guī)的也是比較簡單的做法是在碰到JRX之類的跳轉(zhuǎn)指令時,延遲后邊流水線的進入。但我們通過分析,認為可以一點都不必延遲,通過旁路處理把控制相關(guān)轉(zhuǎn)為數(shù)據(jù)相關(guān)來處理。這樣處理,不必延遲,可以提高流水線的性能。按我們的方式解決控制相關(guān)需要做兩項工作:1) 通過旁路,提供狀態(tài)寄存器的值和臨時狀態(tài)位的值,為判斷是否跳轉(zhuǎn)作準備;2) 選擇PC更新的時機3.結(jié)構(gòu)級設計指關(guān)鍵模塊的功能、輸入輸出信號的框圖示意,并說明主要信號的功能及取值??梢苑譃槿缦聨讉€關(guān)鍵模塊: IF段、ID段、EX段、Mer段、W段、沖突檢測模塊、定向處理模塊等??梢詤⒖贾敖o出的《實踐報告》文檔的寫法,但要注意修改其中信號的位數(shù)。

總體邏輯結(jié)構(gòu)計五級流水CPU吉構(gòu)圖PCIRFIushHazadrDetectUnitJPCFlushtFlagFlagOperatorPCnextUXID_EXRegIFIDRegRNOPRegArray1SAMSB4w_wRegEn0wSB二WritedataRegSelforwardBUXMUXUXOuterDB指令寄存器限AoutBoute_ALUSrc,e_ALUOpr,e_SetFlagEX_MEMRegMEM_WBRegmWrMemwwrMemALUaInw_MemToReg>AUaluOmALUOutOuterDBwMemOut/1mRBdatam_wRegEnUXA0MALUbInforwardARE

GISTERSREGSPCIRFIushHazadrDetectUnitJPCFlushtFlagFlagOperatorPCnextUXID_EXRegIFIDRegRNOPRegArray1SAMSB4w_wRegEn0wSB二WritedataRegSelforwardBUXMUXUXOuterDB指令寄存器限AoutBoute_ALUSrc,e_ALUOpr,e_SetFlagEX_MEMRegMEM_WBRegmWrMemwwrMemALUaInw_MemToReg>AUaluOmALUOutOuterDBwMemOut/1mRBdatam_wRegEnUXA0MALUbInforwardARE

GISTERSREGSTERSw_ALUOutw_wRegEnForwardingUniteSAPe_SBw_SAPCm_ALUOutw_wrMemPCm_ALUOutw_wrMemMemOuterDBReadOuterDBdataWritedata wr Mem_addr圖五級流水整體結(jié)構(gòu)圖整個CPU由以下幾個模塊組成:取指模塊(IF):給出內(nèi)存地址,讀取指令并送入指令寄存器,為下一級準備數(shù)據(jù)。由于PC控制模塊處于取指模塊中,因此控制相關(guān)的檢測也置于取指模塊。譯碼模塊(ID):讀取寄存器值和指令譯碼。我們采取一次譯碼,逐級傳遞的方式,譯出后幾級流水所需的控制信號和數(shù)據(jù)(如立即數(shù)等),在每次時鐘上升沿到來時送入下一級。實際上,結(jié)構(gòu)相關(guān)、控制相關(guān)、數(shù)據(jù)相關(guān)的檢測都可歸入譯碼部分??紤]到“相關(guān)檢測”涉及到的信號分屬不同階段以及整體結(jié)構(gòu)的清晰性,我們將“相關(guān)檢測”獨立出來。執(zhí)行模塊(Ex):完成算術(shù)邏輯運算、計算有效地址和提供數(shù)據(jù)通道。訪存模塊(Ma:選擇地址線的數(shù)據(jù)來源和數(shù)據(jù)線的流向。訪存和取指在功能上是獨立的,但CPU對外只有一條地址線和數(shù)據(jù)線的事實決定了訪存和取指是相互聯(lián)系的。當執(zhí)行LOAD/STOR指令時,地址線由ALU送入“訪存段”的值提供;取指時,則由PC提供。當寫內(nèi)存時,CPU內(nèi)部數(shù)據(jù)送數(shù)據(jù)線;當需要讀內(nèi)存時,CPU往數(shù)據(jù)線送高阻?;貙懩K(Wb:選擇回寫數(shù)據(jù)源和根據(jù)寫使能信號 wRegEn將數(shù)據(jù)回寫到寄存器堆;HazardDetectEntity 模塊:檢測結(jié)構(gòu)相關(guān);ForwardingEntity 模塊:檢測數(shù)據(jù)相關(guān)。關(guān)鍵分模塊結(jié)構(gòu)設計執(zhí)行模塊(Ex)執(zhí)行模塊的主要工作是完成算術(shù)邏輯運算、計算有效地址和提供數(shù)據(jù)通道。執(zhí)行模塊的主體部分是運算器,其總體結(jié)構(gòu)和兩級流水是相似的。主要不同之處在于增加了旁路模塊,這是由于

五級流水存在數(shù)據(jù)相關(guān)需要旁路處理。五級流水CPU運算器結(jié)構(gòu)圖圖五級執(zhí)行模塊結(jié)構(gòu)圖HazardDetectEntity模塊該模塊通過檢查“訪存階段”的m_wrMer來確定是否發(fā)生結(jié)構(gòu)相關(guān)。具體請參閱上文中處理結(jié)構(gòu)相關(guān)的部分。結(jié)構(gòu)圖:d_IRm_wrMemw_wrMemd_IRm_wrMemw_wrMem圖五級HazardDetectEntity模塊結(jié)構(gòu)圖信號說明端口信號含義信號含義m_wrMewrMem處于訪存w_wrMemwrMem處于回寫輸m階段的值階段的值入d」R指令寄存器輸PCStal‘1':保持PCIFFIush‘1':NOP寫出l不變?nèi)隝R‘0':PC更新‘0':內(nèi)存輸出寫入IR表HazardDetectEntity 信號說明回寫模塊(WB回寫是一條指令執(zhí)行過程中的最后一步。它的工作是選擇回寫數(shù)據(jù)源和根據(jù)寫使能信號wRegEn將數(shù)據(jù)回寫到寄存器堆。需要選擇的數(shù)據(jù)源共有兩項:運算器輸出和內(nèi)存輸出。在執(zhí)行算術(shù)邏輯、移位等指令時選擇運算器輸出;執(zhí)行 LOAD指令時選擇內(nèi)存輸出。結(jié)構(gòu)圖:w_WBdataWritedata寫使能―)wRegEnw_WBdataWritedata寫使能―)wRegEn目的寄存器號RegArray―*destRegw_memToRegCLK圖五級回寫模塊結(jié)構(gòu)圖信號含義w_memToReg信號含義w_memToReg?‘1':內(nèi)存數(shù)據(jù)到寄存器‘0':ALU輸出到寄存器wRegEn‘1':允許寫寄存器‘0':禁止寫寄存器信號說明:表五級回寫模塊信號說明224ForwaringEntity 模塊在“時序設計”部分,我們已對“如何檢測數(shù)據(jù)相關(guān)”作了詳細敘述。具體實現(xiàn)由ForwardingEntity 模塊完成。結(jié)構(gòu)圖e_SAe_SBm_SAw_SAm_wRegEnw_wRegEnForwardingUnitforwardA forwardB圖五級ForwardinEntity 模塊結(jié)構(gòu)圖

信號說明端口信號含義信號含義輸入e_SA寄存器A口選擇信號處于執(zhí)行階段的值e_SB寄存器B口選擇信號處于執(zhí)行階段的值m_SA寄存器A口選擇信號處于訪存階段的值w_SA寄存器A口選擇信號處于回寫階段的值m_wRegEn寄存器寫使能處于訪存階段的值w_wRegEr寄存器寫使能處于回寫階段的值輸出forwradA選擇ALU的A口數(shù)據(jù)forwradB選擇ALU的B口數(shù)據(jù)表五級FowardingEntity模塊信號說明4.VHDL源代碼用黑色外的其它明顯的顏色標注對16位參考代碼所作的修改,這個作為評分時的一個主要參考因素。 此外,需有適當?shù)淖⑨專f明關(guān)鍵代碼的功能及作用,注釋亦用黑色外的其它顏色進行標注。-UnitPack.vhd-常量定義文件--2004-09-04libraryieee;useieee.std_logic_1164.all;PACKAGEunitPackISconstantZERO8:STD_LOGIC_VECTOR(7DOWNTO0):="00000000";constantZERO9:STD_LOGIC_VECTOR(8DOWNTO0):="000000000";constantZ8:STD_LOGIC_VECTOR(7DOWNTO0):="ZZZZZZZZ";--ALU:selectdatasourceconstantselA0"000";constantselAB"001";constantselA1"010";constantsel0B"011";constantselAF"100";:std_logic_vector(2downto0):=:std_logic_vector(2downto0):=:std_logic_vector(2downto0):=:std_logic_vector(2downto0):=constantselAD:std_logic_vector(2downto0):=constantselAD101";--ALU:SelectoperationconstantaluAdd:std_logic_vector(3downto0):"0000";constantaluSub:std_logic_vector(3downto0):"0001";constantaluAnd:std_logic_vector(3downto0):"0010";constantaluOr:std_logic_vector(3downto0):"0011";constantaluXor:std_logic_vector(3downto0):"0100";constantaluShl:std_logic_vector(3downto0):"0101";constantaluShr:std_logic_vector(3downto0):"0110";constantaluSar:std_logic_vector(3downto0):constantaluLOADH:std_logic_vector(3downto0):="1000";"1001";constantaluMOVI:std_logic_vector(3downto0):="1010";--狀態(tài)位的控制信號constantflag_hold:std_logic_vector(2downto0):="000";constantflag_update:std_logic_vector(2downto0):="001";constantflag_InnerDB:std_logic_vector(2downto0):="010";constantflag_C0:std_logic_vector(2downto0):="011";constantflag_C1:std_logic_vector(2downto0):="100";constantflag_clear :std_logic_vector(2downto0):="101";--指令操作碼constantADD :std_logic_vector(3downto0):="0000";constantSUBB:std_logic_vector(3downto0):="0001";

0):="0011";constantMOV:std_logic_vector(3downto0):="0010";constantLOAD:std_logic_vector(3downto0):="1000";constantSTORE:std_logic_vector(3downto0):="1010";constantLOADH:std_logic_vector(3downto0):="1011";constantLOADL:std_logic_vector(3downto0):="1001";constantJR:std_logic_vector(3downto0):="0100";constantJRZ:std_logic_vector(3downto0):="0111";constantNOP:std_logic_vector(3downto0):="1100";constantMOVI:std_logic_vector(3downto0):="1101";constantNOPIns:std_logic_vector(7downto0):="11000000";0):="0000"&"10"&"0"&"1"&"011"&"0000"&"000";subtypeINDEXTYPEisINTEGERrange0to3;subtypeWORDisstd_logic_vector(15downto0);subtypeBYTEisstd_logic_vector(7downto0);typeREGISTERARRAYisarray(0to3)ofBYTE;ENDunitPack;--MemAccessEntity.vhd- 訪存模塊libraryIEEE;useIEEE.std_logic_1164.all;useIEEE.std_logic_unsigned.all;usework.unitpack.all;entityMemAccessEntityisport(reset,clk:instd_logic;m_wrMem:instd_logic_vector(1downto0);w_wrMem:outstd_logic_vector(1downto0);m_ALUOu:tinbyte;m_RBdata:inbyte;wr:outstd_logic;OuterDB:inoutbyte;w_ALUOut:outbyte;w_MemOu:toutbyte;m_flag:instd_logic_vector(3downto0);w_flag:outstd_logic_vector(3downto0);PC:inbyte;addr:outbyte;m_SA:instd_logic_vector(1downto0);w_SA:outstd_logic_vector(1downto0);m_wRegEn:instd_logicm_destReg:instd_logic_vector(1downto0);m_memToReg:instd_logic;w_wRegEn:outstd_logic;w_destReg:outstd_logic_vector(1downto0);w_memToReg:outstd_logic);endentity;architectureMemAccessArchofMemAccessEntityisbeginprocess(clk,m_wrMem,m_ALUOut,m_RBdata,PC)begincasem_wrMemiswhen"00"=>addr<=m_ALUout;wr<='0';--writeMemoryOuterDB<=m_RBdata;when"01"=>wr<='1';addr<=m_ALUout;OuterDB<=Z8;whenothers=> wr<='1';addr<=PC;OuterDB<=Z8;endcase;endprocess;process(reset,clk)beginifreset='0'thenw_wRegEn<='0';w_wrMem<="10";elsifRISING_EDGE(clk)thenw_flag<=m_flag;w_ALUOut<=m_ALUOut;w_SA<=m_SA;w_wRegEn<=m_wRegEn;

w_destReg<=m_destReg;w_memToReg<=m_memToReg;w_memOut<=OuterDB;w_wrMem<=m_wrMem;endif;endprocess;endarchitecture;--IFEntity.vhd- 取指模塊libraryIEEE;useIEEE.std_logic_1164.all;useIEEE.std_logic_unsigned.all;usework.unitpack.all;entityIFEntityis

port(entityIFEntityis

port(reset

clk

Z,C:inSTD_LOGIC;:inSTD_LOGIC;:instd_logic;tempZ,tempC:instd_logic;e_setFlag:instd_logic_vector(2downto0);PCPlusOffset:inbyte;PCStall:instd_logic;IFFlush:instd_logicOuterDB:inbyte;PC_addrd_PCInc1d_IR:outbyte;:outbyte;:outbyte--指令寄存器輸出);endIFEntity;architectureIFArchofIFEntityissignalPC,IR:std_logic_vector(7downto0);signalPCIncSel:std_logic;signals_PCInc1,PCnext:std_logic_vector(7downto0);signalop:std_logic_vector(3downto0);signals_flag:std_logic_vector(3downto0);signals_selZ,s_selC:std_logic;signalZZ,CC:std_logic;beginop<=IR(7downto4);withe_setFlagselectZZ<=Z whenflag_hold,tempZwhenothers;withe_setFlagselectCC<=Cwhenflag_hold,tempCwhenothers;s_selZ<='1'WHEN(op=JRZANDZZ='1')ORop=JRelse'0';s_selC<='1'WHEN(op=JRCANDCC='1')else'0';PCIncSel<='1'WHENs_selZ='1'ors_selC='1'ELSE'0';s_PCInc1<=PC+x"01";WITHPCIncSelSELECTPCnext<=s_PCInc1 WHEN'0',PCPlusOffsetWHEN'1',s_PCInc1whenothers;process(reset,clk,PCStall)beginifreset='0'thenPC<=x"00";elsifFALLING_EDGE(clk)and(PCStall='0')thenPC<=PCnext;endif;endprocess;PC_addr<=PC;--PC作為內(nèi)存地址d_IR<=IR;process(reset,clk,OuterDB,IFFlush)beginifreset='0'thenIR<=NopIns;--NULLoperationelsifRISING_EDGE(clk)thencaseIFFlushiswhen'0' =>IR<=OuterDB;whenothers=>IR<=NOPIns;endcase;d_PCInc1<=s_PCInc1;endif;endprocess;endIFArch;--IDEntity.vhd- 譯碼模塊libraryIEEE;useIEEE.std_logic_1164.all;useIEEE.std_logic_unsigned.all;usework.unitpack.all;entityIDEntityisport(reset:instd_logic;clk:instd_logic;d_IR:inbyte;d_PCInc1:inbyte;w_WBData:inbyte;w_destReg:instd_logic_vector(1downto0);w_wRegEn:instd_logic;e_SA,e_SB:outstd_logic_vector(1downto0);i_PCPlusOffset :outbyte;e_RAOut,e_RBOut:outbyte;e_IMM:outbyte;e_ALUSrc:outstd_logic_vector(2downto0);e_ALUOpr:outstd_logic_vector(3downto0);e_SetFlag:outstd_logic_vector(2downto0);e_wrMem:outstd_logic_vector(1downto0);e_wRegEn:outstd_logic;e_destReg:outstd_logic_vector(1downto0);e_MemToReg:outstd_logic;

RegSel:instd_logic_vector(1downto0);RegOut:outbyte);endentity;architectureIDArchofIDEntityisSIGNALRegArray:REGISTERARRAY;signalwRegIndex,ia,ib:INDEXTYPE;signalSA,SB:std_logic_vector(1downto0);signalwrMem:std_logic_vector(1downto0);signalwRegEn:std_logic;signalmemToReg:std_logic;signaloffset:byte;signalRA,RB:byte;signalALUSrc:std_logic_vector(2downto0);signalALUOpr:std_logic_vector(3downto0);signalSetFlag:std_logic_vector(2downto0);signalimm:byte;beginia<=conv_integer(SA);ib<=conv_integer(SB);RA<=RegArray(ia);RB<=RegArray(ib);wRegIndex<=conv_integer(w_destReg);WriteBack:process(reset,clk)beginifreset='0'thenRegArray(0)<=x"00"; --初始化寄存器RegArray(1)<=x"00";RegArray(2)<=x"00"; --初始化寄存器RegArray(3)<=x"00";elsifFALLING_EDGE(clk)andw_wRegEn='1'thenRegArray(wRegIndex)<=w_WBData;endif;endprocess;Decode_Pro:process(d_IR)variableop:std_logic_vector(3downto0);variablectrl:std_logic_vector(17downto0);beginop:=d_IR(7downto4);-caseopiswhenADD=>ctrl:=d_IR(3downto0)&"10"&"1"&"1"&"001"&"0000"&"001";whenSUBB=>ctrl:=d_IR(3downto0)&"10"&"1"&"1"&"001"&"0001"&"001";whenANDins=>ctrl:=d_IR(3downto0)&"10"&"1"&"1"&"001"&"0010"&"001";whenMOV=>ctrl:=d_IR(3downto0)&"10"&"1"&"1"&"011"&"0000"&"000";whenLOADH=>ctrl:="1111"&"10"&"1"&"1"&"101"&"1000"&"000";imm<="0000"&d_IR(3downto0);whenLOADL=>ctrl:="1111"&"10"&"1"&"1"&"101"&"1001"&"000";imm<="0000"&d_IR(3downto0);whenLOAD=>ctrl:=d_IR(3downto0)&"01"&"1"&"0"&"011"&"0000"&"000";whenSTORE=>ctrl:=d_IR(3downto0)&"00"&"0"&"1"&"000"&"0000"&"000";whenJR|JRZ|JRC=>ctrl:=DoNothing;offset<=d_IR(3)&d_IR(3)&d_IR(3)&d_IR(3)&d_IR(3downto0);whenMOVI=>ctrl:=d_IR(3downto0)&"10"&"1"&"1"&"101"&"1010"&"000";imm<="000000"&d_IR(1downto0);whenORins=>ctrl:=d_IR(3downto0)&"10"&"1"&"1"&"001"&"0011"&"001";whenSHLIns=>ctrl:=d_IR(3downto0)&"10"&"1"&"1"&"000"&"0101"&"001";whenSHRIns=>ctrl:=d_IR(3downto0)&"10"&"1"&"1"&"000"&"0110"&"001";whenothers=>ctrl:=DoNothing;endcase;SA<=ctrl(17downto16);SB<=ctrl(15downto14);WrMem<=ctrl(13downto12);wRegEn<=ctrl(11);MemToReg<=ctrl(10);ALUSrc<=ctrl(9downto7);ALUOpr<=ctrl(6downto3);SetFlag<=ctrl(2downto0);endprocess;i_PCPlusOffset<=d_PCInc1+offset;process(reset,clk)beginifreset='0'thene_wrMem<="10";e_wRegEn<='0';elsifRISING_EDGE(clk)thene_SA<=SA;e_SB<=SB;e_RAOut<=RA;e_RBOut<=RB;e_IMM<=imm;e_ALUSrc<=ALUSrc;e_ALUOpr<=ALUOpr;e_SetFlag<=SetFlag;e_wrMem<=wrMem;e_wRegEn<=wRegEn;e_destReg<=SA;e_MemToReg<=memToReg;endif;endprocess;RegOut<=RegArray(conv_integer(RegSel));endarchitecture;--HazardDetectEntity.vhd- 結(jié)構(gòu)相關(guān)處理模塊libraryIEEE;useIEEE.std_logic_1164.all;useIEEE.std_logic_unsigned.all;usework.unitpack.all;entityHazardDetectEntityisport(m_wrMem:instd_logic_vector(1downto0);w_wrMem:instd_logic_vector(1downto0);d_IR:inbyte;IFFlush:outstd_logic;PCStall:outstd_logic);endentity;architectureHazardDetectArchofHazardDetectEntityisbeginprocess(m_wrMem,d_IR)variabled_op:std_logic_vector(3downto0);begind_op:=d_IR(7downto4);casem_wrmemiswhen"00"|"01"=>IFFlush<='1';ifw_wrMem="00"orw_wrMem="01"thenPCStall<='1';else

PCStall<='0';endif;whenothers=>cased_opiswhenLOAD=>IFFlush<='1';PCStall<='0';whenNOP=>PCStall<='1';IFFlush<='0';whenothers=>IFFlush<='0';PCStall<='0';endcase;傍路處理模塊endcase;endprocess;endarchitecture;傍路處理模塊--ForwardingEntity.vhd-libraryIEEE;useIEEE.std_logic_1164.all;entityForwardingEntityisport(m_wRegEn:instd_logic;w_wRegEn:w_wRegEn:instd_logicm_SA:instd_logic_vector(1downtom_SA:instd_logic_vector(1downto0);w_SA:instd_logic_vector(1downto0);e_SA:instd_logic_vector(1downto0);e_SB:w_SA:instd_logic_vector(1downto0);e_SA:instd_logic_vector(1downto0);e_SB:instd_logic_vector(1downto0);forwardA:outstd_logic_vector(1downto0);forwardB:outstd_logic_vector(1downto0));endentity;architectureForwardingArchofForwardingEntityisbeginprocess(m_wRegEn,w_wRegEn,m_SA,w_sA,e_SA,e_SB)beginifm_wRegEn='1'andm_SA=e_SAthenforwardA<="10";elsifw_wRegEn='1'andw_SA=e_SAthenforwardA<="01";elseforwardA<="00";endif;ifm_wRegEn='1'andm_SA=e_SBthenforwardB<="10";elsifw_wRegEn='1'andw_SA=e_SBthenforwardB<="01";elseforwardB<="00";endif;endprocess;endarchitecture;--ExEntity.vhd- 執(zhí)行模塊libraryieee;useieee.std_logic_1164.all;useieee.std_logic_unsigned.all;usework.unitPack.all;entityExEntityisport(reset,clk:instd_logic;e_RAOut,e_RBOut:inbyte;e_ALUSrc:instd_logic_vector(2downto0);e_ALUOpr:instd_logic_vector(3downto0);e_SetFlag:instd_logic_vector(2downto0);e_IMM:inbyte;forwardA,forwardB:instd_logic_vector(1downto0);e_SA:instd_logic_vector(1downto0);

m_SAoutstd_logic_vector(1downto0);m_SAe_ALUOut:inbyte;w_WBdata:inbyte;m_ALUOut:outbyte;m_RBdata:outbyte;i_tempZ:outstd_logic;i_tempC:outstd_logic;m_flag:outstd_logic_vector(3downto0);e_wRegEn:INstd_logic;m_wRegEn:outstd_logic;e_memToReg:instd_logic;m_memToReg:outstd_logic;e_destReg:INstd_logic_vector(1downto0);m_destReg:outstd_logic_vector(1downto0);e_wrMem:INstd_logic_vector(1downto0);m_wrMem:outstd_logic_vector(1downto0));endentity;architectureExArchofExEntityissignalALUaIn,ALUbIn:byte;signaldout:byte;signalC,Z,V,S:STD_LOGIC;signaltempFlag:std_logic_vector(3downto0);beginwithforwardAselectALUaIn<=e_RAOutwhen"00",e_ALUOutwhen"10",w_WBdatawhen"01",e_RAOutwhenothers;withforwardBselectALUbIn<=e_RBOutwhen"00",e_ALUOutwhen"10",w_WBdatawhen"01",e_RBOutwhenothers;ALUActivity:process(ALUaIn,ALUbIn,e_ALUSrc,e_ALUOpr,e_setFlag,e_IMM,tempFlag)variableALUResult,opR,opS:std_logic_vector(8downto0);variablecx,tempC,tempZ,tempV,tempS:std_logic;

begincasee_ALUSrcis-- 選擇ALU勺兩入口數(shù)據(jù)whenselA0=>opR:='0'&ALUaIn;opS:=ZERO9;whenselAB=>opR:='0'&ALUaIn;opS:='0'&ALUbIn;'0'&(X"01");whensel0B'0'&ALUbIn;whenselA1=>opR:='0'&(X"01");whensel0B'0'&ALUbIn;=>opR:=ZERO9;opS:=whenselAF=>opR:='0'&ALUaIn;opS:='1'&(X"FF");whenothers=>opR:=ZERO9;whenselAD=>whenothers=>opR:=ZERO9;'0'&e_IMM;opS:=ZERO9;endcase;7casee_ALUOpriswhenaluAdd=>ALUResult:=opR+opS;tempV:=((notopR(7))and(notopS(7))andALUResult(7))or(opR(7)andopS(7)and(notALUResult(7)));whenaluSub=>ALUResult:=opR-opS;tempV:=(opS(7)and(notopR(7))and(notALUResult(7)))or((NOTopS(7))andopR(7)and

ALUResult(7));whenaluAnd=>ALUResult:=opRandopS;whenaluOr=>ALUResult:=opRoropS;whenaluShl=>ALUResult(7downto1):=opR(6downto0);ALUResult(0):='0'; cx:=opR(7);whenaluShr=>ALUResult(6downto0):=opR(7downto1);ALUResult(7):='0'; cx:=opR(0);whenaluLOADH=>ALUResult:=opS(4downto0)&opR(3downto0);whenaluLOADL=>ALUResult:=

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論