在設(shè)計(jì)中,我們不斷的給目錄、源代碼、文件、函數(shù)、變量、參數(shù)、類、封包進(jìn)行命名與定義。當(dāng)一件工作需要進(jìn)行的次數(shù)非常之多,足以證明它是不可或缺的基本工作。我們一定要知道一點(diǎn),基礎(chǔ)工作是整個(gè)項(xiàng)目的基石。忽視抑或是輕視基礎(chǔ)工作是一件非常錯(cuò)誤的工作理念。我們需要用最嚴(yán)謹(jǐn)認(rèn)真的態(tài)度去對(duì)待,同時(shí)作為回報(bào),它將令你的作品顯得專業(yè)而優(yōu)雅。 我們以信號(hào)的定義為例來(lái)說(shuō)明這個(gè)問(wèn)題。先來(lái)看這么一組代碼: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 4344 45 464748 always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cnt <=>=> end else if(add_cnt)begin if(end_cnt) cnt <=>=> else cnt <= cnt="" +="">=> end end assign add_cnt = flag1||flag2 ; assign end_cnt = add_cnt && cnt==x-1 ; always @(posedge clk or negedge rst_n)begin if(rst_n==1'b0)begin flag1 <=>=> end else if(en1)begin flag1 <=>=> end else if(end_cnt)begin flag1 <=>=> end end always @(posedge clk or negedge rst_n)begin if(rst_n==1'b0)begin flag2 <=>=> end else if(en2)begin flag2 <=>=> end else if(end_cnt)begin flag2 <=>=> end end always @(*)begin if(flag1) x = 5; else if(flag2) x = 7; else begin x = 0; end end 這組代碼的功能是當(dāng)en1時(shí)計(jì)數(shù)5下;en2計(jì)數(shù)7下。在這組代碼中,en1時(shí)flag1拉高;end-cnt時(shí)flag1變低;en2時(shí)flag2拉高;end-cnt時(shí)flag2變低;也就是在flag1或者flag2時(shí)加一,然后用flag1和flag2分別區(qū)分計(jì)數(shù)5下和7下。 盡管能夠?qū)崿F(xiàn)功能,但是在這組代碼中,存在信號(hào)定義不明確得現(xiàn)象。flag1和flag2到底是什么意思?是表示flag1(flag2)時(shí)en1產(chǎn)生,還是en1(en2)時(shí)的計(jì)數(shù)狀態(tài)?為說(shuō)明這一點(diǎn)就得用到XXXXX (寫(xiě)加一條件時(shí)需要用到add_cnt = flag1||flag2)語(yǔ)句。 這里重申一下我們很重要的那條簡(jiǎn)單原則,一個(gè)代碼(信號(hào))只做一件事且做好這件事!按照這個(gè)規(guī)則,思路就是這樣了:用一個(gè)信號(hào)flag1來(lái)表示計(jì)數(shù)狀態(tài),另外一個(gè)信號(hào)flag2表示是由en1還是en2所產(chǎn)生。那么,加一與否的條件非常簡(jiǎn)單,就是是否處于工作狀態(tài)(flag1);同理,計(jì)數(shù)5或者7下只需要使用flag2一個(gè)信號(hào)。那么代碼就會(huì)是這樣: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 3839 404142 43 44 45 always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cnt <=>=> end else if(add_cnt)begin if(end_cnt) cnt <=>=> else cnt <= cnt="" +="">=> end end assign add_cnt = flag1 ; assign end_cnt = add_cnt && cnt==x-1 ; always @(posedge clk or negedge rst_n)begin if(rst_n==1'b0)begin flag1 <=>=> end else if(en1||en2 )begin flag1 <=>=> end else if(end_cnt)begin flag1 <=>=> end end always @(posedge clk or negedge rst_n)begin if(rst_n==1'b0)begin flag2 <=>=> end else if(en1)begin flag2 <=>=> end else if(en2)begin flag2 <=>=> end end always @(*)begin if(flag==0) x = 5; else x = 7; end 看到這里,也許有些朋友會(huì)覺(jué)得:好像區(qū)別沒(méi)那么大?。縪k,我們假設(shè)一下,如果程序中不僅是是en1,en2,而是有en3,en4……enX,又或者將來(lái)需要維護(hù)和優(yōu)化,這兩者的區(qū)別將會(huì)天壤之別。 關(guān)于信號(hào)定義方面,《至簡(jiǎn)設(shè)計(jì)法》的作者潘文明給出了一個(gè)近乎完美的答案。例如在計(jì)數(shù)器代碼設(shè)計(jì)中的“架構(gòu)八步法”,第一步就是明確定義信號(hào),用具體、清晰且無(wú)疑異的語(yǔ)句,定義每個(gè)信號(hào)所要實(shí)現(xiàn)的功能,以及重點(diǎn)描述信號(hào)的變化情況。如下圖中的信號(hào)列表。 信號(hào)列表。(4)(用文字版) 信號(hào)名 I/O 位寬 說(shuō)明 clk I 1 系統(tǒng)工作時(shí)鐘 rst_n I 1 系統(tǒng)復(fù)位信號(hào) Din_sop I 1 當(dāng)vld=1時(shí)才有效,輸入報(bào)文頭指示信號(hào) Din_eop I 1 當(dāng)vld=1時(shí)才有效,輸入報(bào)文尾指示信號(hào) Din_vld I 1 輸入數(shù)據(jù)有效標(biāo)志,高電平有效 Din_err I 1 輸入報(bào)文錯(cuò)誤標(biāo)志,在eop有效時(shí)才有效 din I 8 輸入數(shù)據(jù)總線 Dout_sop O 1 當(dāng)vld=1時(shí)才有效,輸出報(bào)文頭指示信號(hào) Dout_eop O 1 當(dāng)vld=1時(shí)才有效,輸出報(bào)文尾指示信號(hào) Dout_vld O 1 輸出數(shù)據(jù)有效標(biāo)志,高電平有效 dout O 8 輸出數(shù)據(jù)總線 Dout_err O 1 輸出報(bào)文錯(cuò)誤標(biāo)志,在eop有效時(shí)才有效 從中可以看出,優(yōu)秀的FPGA設(shè)計(jì)師一開(kāi)始就從頂層結(jié)構(gòu)明確定義信號(hào),將可能出現(xiàn)的混亂從根源上解決。這樣的思路和方法實(shí)在非常值得我們每一位從業(yè)者學(xué)習(xí)和借鑒。 |
|