2014年7月31日 星期四

Gray Code

Gray code 是一種可靠性編碼方式, 連續兩個代碼間只有1 bit的差異,可以防止訊號傳輸錯誤進入亞穩態metastability,但是缺點是只能定義2^n的深度

用鏡射法產生gray code,上下對稱映射後,左邊上面補零,左邊下面補一



也可以用二進位跟graycode互相推算



如果需要的位元數不大的話可以用陽春的查表方式......
下例是以graycode的順序為主,再用graycode to binary 來查表
為了要使enable以後,graycode先從0000開始,所以在reset之後先把值設成1000






下面是以binary 為主, 再用binary to graycode公式來對應,但是從一開始





2014年7月29日 星期二

Sync Async Reset Study

Ref : Synchronous Resets? Asynchronous Resets? I am so confused! How will I ever know which to use? - Clifford E. Cummings

延伸閱讀 ! Asynchronous & Synchronous Reset
Design Techniques - Part Deux -Clifford E. Cummings
這篇文章是我讀得這篇的第二版,有多加一些東西,但是也把後面的multi ASIC reset拿掉,沒有時間看完,有空可以再看一次!


Synchronous Reset
clk同步的reset
directive // synopsys sync_set_reset "rst_n"
the sync_set_reset directive does not affect the logical behavior of a design, instead it only impacts the functional implementation of a design. A wise engineer would prefer to avoid re-synthesizing the design late in the project schedule and would add the sync_set_reset directive to all RTL code from the start of the project.

優點 :
  1. 沒有不穩定(metastability) 的問題,其保證整個電路都是同步
  2. 在有些設計裡面需要自己產生reset訊號,這時候適合用synchronous reset
缺點 :
1. 與非同步比需要比較多的邏輯,但是現在科技日新月異,die可以塞非常多邏輯,所以差別
不太大
2. 需要一直提供Clock, 雖然這clock可以當作filter來消除reset glitches, 但是如果這些
glitches發生在active clock edge,也會造成FF進入metastable,
  1. 速度較慢,且需要保證reset拉得夠長到active clock edge
  2. 在模擬的時候,reset可能會被設成X's,有些模擬器不支援這種情況, 所以可能會block out synchronous reset.
  3. 因為synchronous reset需要一個clk,在某些電路上可能會造成困擾,譬如在設計中有tristate bus,在這種電路中需要power on asynchronous reset

注意reset不在sensitivity list裡面
下面的例子是reset active high,如果reset active low,就要改為if ( reset = '0' )



Asynchronous Reset
clk不同步的reset
The approach to synthesizing asynchronous resets will depend on the designers approach to the reset buffer tree. If the reset is driven directly from an external pin, then usually doing a set_drive 0 on the reset pin and doing a set_dont_touch_network on the reset net will protect the net from being modified by synthesis. However, there is at least one ESNUG article that indicates this is not always the case[16].
One ESNUG contributor[15] indicates that sometimes set_resistance 0 on the reset net might also be needed. And our colleague, Steve Golson, has pointed out that you can set_resistance 0 on the net, or create a custom wireload model with resistance=0 and apply it to the reset input port with the command:
set_wire_load -port_list reset A recently updated SolvNet article also notes that starting with Synopsys release 2001.08 the definition of ideal nets has slightly changed[24] and that a set_ideal_net command can be used to create ideal nets and “get no timing
updates, get no delay optimization, and get no DRC fixing.”
Another colleague, Chris Kiegle, reported that doing a set_disable_timing on a net for pre-v2001.08 designs helped to clean up timing reports[2], which seems to be supported by two other SolvNet articles, one related to synthesis and another related to Physical Synthesis, that recommend usage of both a set_false_path and a set_disable_timing command[21][25].

優點 : 與同步比需要比較少的邏輯,不需要一直提供Clock,速度較快
The biggest advantage to using asynchronous resets is that, as long as the vendor library has
asynchronously resetable flip-flops, the data path is guaranteed to be clean.
缺點 : 最大的問題在於deassertion的時候,如果在active clock edge附近的話,有不穩定(metastability) 的問題,ASIC可能會失去reset state,另外跟同步相同的問題,會有因為glitch或是雜訊造成的偽reset(spurious reset)使系統進入metastable.



注意resetsensitivity list裡面
下面的例子是reset active high,如果reset active low,就要改為if ( reset = '0' )


Clifford E. Cummings paper中有建議的coding style
Synchronous reset flip-flops with non reset follower flip-flops









上面的第一種形式比較常被應用,第二種只是展示功能

Modeling Verilog flip-flops with asynchronous reset and asynchronous set

For those rare designs where reset and set are both permitted to be asserted simultaneously and then reset is removed
first, the fix to this simulation problem is to model the flip-flop using self-correcting code enclosed within the
translate_off/translate_on directives and force the output to the correct value for this one condition. The best
recommendation here is to avoid, as much as possible, the condition that requires a flip-flop that uses both
asynchronous set and asynchronous reset. The code in Example 6 shows the fix that will simulate correctly and
guarantee a match between pre- and post-synthesis simulations. This code uses the translate_off/translate_on
directives to force the correct output for the exception condition[4].

只有asynchronous 會有reset,set的情況, resetset同時assert, reset remove.


下面的例子是使用非同步,但是設計者不放resetdata path裡面(不太明白耶!!!)



Reset Synchronizer
結論! 每一個使用asynchronous resetASIC design一定要有一個Reset Synchronizer !

Reset Synchronizer具有同步與非同步的優點,先用非同步進來,用一個reg擋住,再用同步傳出去,下面的例子resetactive low




Reset distribution tree
reset distribution tree的負載跟clock distribution tree 一樣重
Unlike clock signals, skew between reset signals is not critical as long as the delay associated with any reset signal is short enough to allow propagation to all reset loads within a clock period and still meet recovery time of all destination registers and flip-flops
The safest way to clock a reset tree (synchronous or asynchronous reset) is to clock the internal-master-reset flip-flop from a leaf-clock of the clock tree as shown in Figure 9. If this approach will meet timing, life is good. In most cases, there is not enough time to have a clock pulse traverse the clock tree, clock the reset-driving flip-flop and then have the reset traverse the reset tree, all within one clock period.






Reset-glitch filtering
Cliff介紹一個消除reset glitch的方法, 利用delay來消除glitch,reset input必須要是schmitt trigger.

In order to add the delay, some vendors provide a delay hard macro that can be hand instantiated. If such a delay macro is not available, the designer could manually instantiate the delay into the synthesized design after optimization – remember not to optimize this block after the delay has been inserted or it will be removed. Of course the elements could have don’t touch attributes applied to prevent them from being removed. A second approach is to instantiated a slow buffer in a module and then instantiated that module multiple times to get the desired delay.


Multi-clock reset issues
之前討論resetdeassertion的時候可能會造成metastable,cliff舉出下列兩種方法來解決
  1. non-coordinated reset removal,reset只要在相近的時間點移除,不管是不是在一個clk之內

  1. sequenced coordination of reset removal. 要確定第一級reset移掉之後,要確定logic已經activate在另兩級移掉reset之前

文章後面都在介紹multi ASICreset,這部分如果以後工作需要的話再看吧!



2014年7月28日 星期一

VHDL textio example

汗顏阿! VHDL 寫了一陣子,沒寫過file IO(因為用不到阿阿阿!),之前Verilog寫的也都忘光了
來看一下怎麼寫,Debug的時候也可以用
先來測試寫入
注意! file inout and output cannot be synthesized
I/O operations do not refer to I/O pins of FPGA chips

textio std library裡面
在寫之前別忘了加入

資料型態如果是string則用 TEXT
如果是integer則用 INTR

寫資料
以下在ARCHITECTURE裡面

結果長這樣,做完synthesis之後結果就會出現,讓我不明白的是為甚麼第一句會出現兩次!!!!!

如果要看模擬,則要在程式裡面加wait ,然後用translaste on off包起來


讀資料

輸入的資料如下,如果要看到模擬波形,要把out1out2放在top layer



讀寫在一個process,把讀到的寫出來,但是還是一樣的老問題,為什麼第一個會重複阿!!!!





writefunction還可以多設兩個參數,設右邊開始讀,還有讀多少bit



2014年7月25日 星期五

Adjektive mit Präposition

1. Die deutsche Wirtschaft ist von Rohstoffimporten abhängig.
2. Entscheidend für den Erfolg war seine große Ausdauer.
3. Kaum ein Mensch ist frei von Vorurteilen.
4. Der Sänger ist bei jung und alt beliebt.
5. Als erster wurde Musiklehrer auf den begabten Jungen aufmerksam.
6. Ich bin immer gut zu Greta. Warum ist sie nur böse auf mich.
7. Die Regierung ist zu harten Maßnahmen entschlossen.
8. Bist du mit meinem Vorschlag einverstanden?
9. Prof. Rau wurde durch seine neuartige Krebstherapie weltweit bekannt.
bekannt sein für etwas 
bekannt durch etwas 
10. Viele Bürger sind um die Zukunft ihres Lands sehr besorgt.
11. Womit sind die Wissenschaftler beschäftigt?
12. Hundert Euro sind genug für Walter, mehr Geld bekommt er nicht.
genug haben von 
genug sein für
13. Wir sind an einem schnellen Abschluss der Arbeiten interessiert und für jede         Hilfe dankbar.
14. Nehmen Sie sich so viele Äpfel, wie Sie wollen, ich habe genug davon.
15. Der Reisende war froh über die Ankunft des Dolmetschers.
16. Manche Schülerin ist in ihren Lehrer verliebt.
17. Arbeitest du noch an dem Artikel? Nein, ich bin schon lange damit fertig.
18. Keiner der Prozessbeteiligten war glücklich über die Entscheidung des    
      Richters.
19. Diese Bergtour kann gefährlich sein, besonders für wenig Geübte.
20. Sie haben keine Ahnung, wozu dieser Mensch in seinem Zorn fähig ist.
21. Wir waren alle neugierig auf Barbaras Freund.
22. Zusätzlich zur Opernkarte bekam er auch ein Programm.
23. Dieses Klima ist für Herzkranke sehr ungesund.
24. Seien Sie nett zu ihren Mitmenschen!

2014年7月24日 星期四

FSM Coding Style

Referece : State Machine Coding Styles for Synthesis - Clifford E. Cummings

最近心血來潮想要把Clifford E. Cummings 寫過的文章都看過一次做個整理,正好看到FSM,最近工作上也寫到了兩個FSM,便先讀了OO無雙的FSM詳細介紹,原來我之前寫的FSM是不好的Coding style@@,後來看到altera官方出的VHDL MOORE&MEALY FSM,果然跟OO推薦的寫法一致,快點來做個練習矯正自己不好的Coding Style !!!!!!

斯斯有兩種! 一種叫摩爾,一種叫米粒(亂入XDDDD),常用的是Moore,
Moore的輸出只與現在的狀態有關
Mealy的輸出與現在的狀態與輸入有關
兩者的功能相同但是Mealy的輸出會比Moore早一個clk,因為Moore沒辦法根據輸入的訊號及時做出反應
最好的寫法是用兩個Process來寫FSM

常見的state編碼方式有下列幾種, binary sequence,gray-code,Johnson,one-hot,almost one-hot, one-hot with zero idle


ise中也可以選要用哪種encode來合成



Moore FSM


Mealy FSM

看一下合成Report已經被自動選為gray code,但也有可能會是binary,系統會自己選擇,因為我設定auto阿!!!


如果要用one hot也可以透過attribute來改,在程式裡面訊號宣告區加入
attribute syn_encoding : string;
attribute syn_encoding of state : signal is "onehot";


gray code在狀態轉換的時候可能會使輸出產生突波,one hot可以避免這種情形,但是one hot需要使用比較多的DFF,但是因為每次只變化2Bit,所以也比較省power


我以前在寫FSM的時候,都把next state logicoutput logic放在一起,OO無雙說,這樣會增加程式的複雜度,使維護困難,它說最好在Moore中的output logic在擋一級register使timing更好,但是我感覺這樣在模擬的時候,輸出與state差一個clk,會增加debug的難度



2014年7月21日 星期一

Verilog-VHDL Coding Style for synthesis

之前讀過一篇關於這方面的文章,後來整個忘了
但是大部分的規則都還記得,關於latch或是conbinational loop
今天剛好又看到這篇,趕快來複習一下兼做筆記
Reference : RTL Coding Styles That Yield Simulation and Synthesis Mismatches.

如果違反下列的design rule會造成pre-post synthesis mismatch

Sensitivity List
其不包括關鍵字rising_edge,falling_edge,如果在sensitivity list中有多餘的無關係訊號,只會使pre-synthesis跑得更久
  1. 不完全的sensitivity list
  2. 完全的sensitivity list但是錯誤的順序,因為在pre-synthesis是順序式的執行,assign的直要先放入sensitivity list

Function
function總是被當成conbinational logic來和成,有些工程師喜歡把combinational logic寫成function,但是如果在function裡面有個latch,合成的工具沒有辦法檢查到這個錯誤,pre-synthesis會去檢查functionality,post-synthesis會檢查combinational logic,這樣就會造成pre-post synthesis mismatch

Case Statement
Full Case – 只有Verilog,VHDL不需要,因為VHDLcase others,不會有不完全的case
如果我們使用 // synopsys full_case 這個指令, 這用來指示所有的Case狀態已經被指定,如果沒有被指定的Case則自動設為dont cares,如果在case中有寫default,系統就會自動忽略這個指令,很多程式設計師喜歡用這個指令因為它可以讓設計更小而且速度更快且可以修掉latch,但是事實上不一定,它可能會對設計完全不產生影響且會使設計變大或是變慢,或是造成pre-post synthesis mismatch,下面的例子有加了指令但是還是造成的Latch



但是如果在case前先把輸出值初始化就可以避免latch的產生



Parallel Case - 只有Verilog,因為VHDL不允許overlapp 或是不完全的case
如果case中會有重複的情況,表示這個設計不是parallel,如下例3'b011,3'b101,3'b110,3'b111都有兩個以上的case符合情況,它就會依照編碼的優先順序來輸出,稱為priority encoder,irq[2]>irq[1]>irq[0],synthesis的結果parallel會是no



在下例中,不會有case重複的情形,synthesis的結果parallel會是auto



如果使用者使用 parallel_case這個指令,synthesis的結果parallel會是user
如果在overlapp的電路上使用parallel_case,會造成pre-post synthesis mismatch
如果在已經平行的電路上使用parallel_case,系統會自動忽略,不會有差別





Casex- 只有Verilog,“?,z,x“皆為dont care,VHDL不允許

case中不要使用casex,它是用來表達其它不指定的狀態,但是容易使訊號進入未知的狀態,最好用casez


Casez- 只有Verilog,“?,z“皆為dont care,VHDL不允許
雖然casezcasex功能相似,但是如果設計有問題在驗證時容易被發現,casez常用在modeling address decoders and priority encoders.但是需要小心使用,它可能也會造成三態或是浮動的訊號


------------------------------------------------- 補充----------------------------------------------------------

---------------------------------------------------------------------------------------------------------------

初始化值不要設為X
Verilog,所有被設為x的值都會被合成優化為dont care,下面的例子如果為2'b11則會造成pre-post synthesis mismatch



模型初始化使用translate_off,translate_on
不應該在初始化的時候對synthesis隱藏初始值. 這可能會造成ASIC之後初始化出現問題



translate_off,translate_on的普遍使用方法
這個指令最好是用在display information,如果用來控制functionality則非常危險,唯一例外只有在使用非同步的D flip-flopresetset,我們會需要使用這個指令來測試不同狀況的組合輸出,(This exception requires the use of non-synthesizable constructs to provide the correct presynthesis model that accurately models and matches the post-synthesis model. This exception
condition is created as follows: assert reset, assert set, remove reset, leaving set still asserted. In
this case, the D flip-flop model needs a little assistance to correctly model the set condition
during pre-synthesis simulation.)因此在設計上最好避免使用reset/setD flip-flop,在下面的例子a在模擬的時候99%正確,但是會有個缺陷,c則是100%正確




xilinxconstraint中可以用三種關鍵字來表示
Translate Off and Translate On can be used with the following words:
• synthesis
• synopsys
• pragma


Translate Off and Translate On VHDL Syntax Example
In VHDL, write Translate Off and Translate On as follows:
-- synthesis translate_off
...code not synthesized...
-- synthesis translate_on



總結
但是如果使用者需要設計priority encoder,最好使用if-else-if
在設計中不要使用full_caseparallel_case,當它們有效的時候才是最危險的! 只有在onehot FSM中可以拿優化設計
如果要設計一個dont carecase,最好使用casez,而且最好使用? 不要使用z
檢查所有的case synthesis report




Timing Delays
如果在assign的左邊放入timing delay的值,因為輸出不會因為輸入變動及時改變,造成設計不精準,其會造成pre-post synthesis mismatch


延伸閱讀補充
http://www.nspark.org.tw/webfiles/verilog_coding_style-final.pdf
verilog,組合邏輯用blocking assignment(=),循序邏輯用nonblocking assignment(<=), 如果在循序邏輯中改用blocking assignment,和成後的結果相同,但是如果程式的順序不同,則造成在程式中指定的順序會影響和成出來的結果,不要在同一個always裡面同時使用blocking assignmentnonblocking assignment


ISE的設定中可以幫Case手動設定full or parallel