(原創) 如何使用Nios II C2H compiler? (IC Design) (DE2) (Nios II) (Quartus II) (SOPC Builder) (C/C++) (C2H)

Abstract
由於嵌入式的CPU運算速度較差,一些演算法在軟體可能無法達到real time需求,這時會考慮用Verilog或VHDL來實現硬體加速。為了減少time to market,Altera提供了C2H compiler,讓你直接將用軟體C語言的程式碼變成硬體。

Introduction
使用環境:Quartus II 7.2 SP1 + MegaCore IP 7.2 SP1 + DE2(Cyclone II EP2C35F627C6)

什麼是C2H?它是(C to Hardware)的縮寫,能將你原本的軟體C語言程式碼變成硬體。更詳細的介紹請參考(轉貼) 快速可編程單晶片系統開發與ANSI C語言功能的硬體加速 (IC Design) (C/C++) (News) ,若你對它背後的原理感興趣,建議你參考這篇Altera原廠發表的paper:Automated Generation of Hardware Accelerators With Direct Memory Access From ANSI/ISO Standard C Functions。本文主要討論C2H應用上會遇到的問題。

建立Nios II硬體
Step 1:
建立Quartus II專案

複製DE2 CD中的\DE2_demonstrations\SOPC_Builder\Reference_Design\DE2_NIOS專案到硬碟(或從http://www.terasic.com/downloads/cd-rom/de2/ 下載),別忘了取消唯讀屬性,若你想自己從頭到尾自己由SOPC Builder建立,請參閱(原創) 如何自己用SOPC Builder建立一個能在DE2上跑μC/OS-II的Nios II系統? (IC Design) (DE2) (Quartus II) (Nios II) (SOPC Builder) (μC/OS-II)

這裡有一點須注意,目前友晶範例由Quartus II 6.0和7.1所開發,若你和我一樣使用更高的版本如Quartus II 7.2,請將DE2_NIOS專案打開後,馬上用SOPC Buider開啟,更新成7.2的新格式後存檔,因為C2H等一下會將C程式變成SOPC Builder的custom component,格式的差異會導致C2H compiler無法編譯成功

c2h_00.gif


建立Nios II軟體
Step 1:
建立Nios II專案

使用Hello World template建立一個專案,並在hello_world.c使用以下程式碼。

 1 /* 
 2 (C) OOMusou 2008 http://oomusou.cnblogs.com
 3 
 4 Filename    : hello_world.c
 5 Compiler    : Nios II gcc
 6 Description : Demo how to use C2H compiler
 7 Release     : 04/22/2008 1.0
 8 */
 9 #include <stdio.h>
10 
11 int sum_elements(int *list, int len) {
12   int i;
13   int sum = 0;
14   
15   for(i = 0; i < len; i++)
16     sum += *list++;
17     
18   return sum;
19 }
20 
21 int main() {
22   int ia[] = {12345};
23   int sum = sum_elements(ia, 5);
24   
25   printf("%d", sum);
26 }


這個程式很簡單,只想將array內每個元素做相加,內容我就不再多做解釋,等一下我們會辦法用C2H將sum_elemenet這個function用硬體加速。

目前為止,你可以先執行看看純Nios II的軟體是否可執行成功,若純軟體都無法執行,表示SOPC Builder那邊的配置有問題,之後硬體就更不可能成功了。

Step 2:
指定function變成硬體

現在我們想將sum_elements()這麼function用硬體執行,如下圖將sum_elements選擇Accelerate with the Nios II C2H Compiler。

c2h_01.gif



選了之後會在下方多出C2H的設定,選擇『Build Software and generate SOPC Builder system』和『Use hardware accelerator in place of software implementation. Flush data cach before each call』.

c2h_02.gif

最後重build整個project,這裡要有心理準備,會非常非常的久,完全看你PC硬體的配備了。到目前為止,新的SOPC Builder system已經建立好了。

Step 3:
重回Quartus II

重回Quartus II編譯,若你有完整的license就沒問題,像我並沒有完整的license,而是用破解的,就只會產生DE2_NIOS_time_limited.sof這個檔,最後再用programmer將這個sof燒進DE2,會出現以下提醒,可以按確定忽略。

c2h_03.gif

programmer燒入後,會出現以下畫面。

c2h_04.gif

Step 4:
回Nios II EDS執行

如同執行Nios II軟體一樣,這時候就會用硬體執行sum_elements()了。

Remark
有幾個地方特別提出來討論:
1.在以上step中,在C2H選項中,我是用『Build software and generate SOPC Builder system』,而不是選『Build software, generate SOPC Builder system, and run Quartus II compilation』,理由是若你有完整的license,可以build出DE2_NIOS.sof,那就沒問題,回到Quartus II很自然可以用programmer將DE2_NIOS.sof燒進DE2,但因為我們是破解版,所以build出來的是DE2_NIOS_time_limited.sof,這樣會導致原來的programmer燒進DE2的還是DE2_NIOS.sof,很多人就是因為這樣而以為因為沒有license無法使用C2H。

2.若日後想切回軟體執行,只要如下圖選擇『Use software implementation』即可,至於為什麼這麼神奇,只要這樣就可以切換軟硬體,在Automated Generation of Hardware Accelerators With Direct Memory Access From ANSI/ISO Standard C Functions 有詳細解釋他的原理。

c2h_05.gif

3.為了證明這是硬體,將SOPC Builder打開後,發現多了C2H所產生的accelerator_hello_world_0_sum_elements_managed_instance這個custom component,這是原本沒有的。

c2h_06.gif

4.或許你會懷疑,若function中還呼叫其他function,也能做硬體加速嗎?我將程式改成如下所示:

 1 /* 
 2 (C) OOMusou 2008 http://oomusou.cnblogs.com
 3 
 4 Filename    : hello_world.c
 5 Compiler    : Nios II gcc
 6 Description : Demo how to use C2H compiler
 7 Release     : 04/22/2008 1.0
 8 */
 9 #include <stdio.h>
10 
11 int func(int i) {
12   return i + 2;
13 }
14 
15 int sum_elements(int *list, int len) {
16   int i;
17   int sum = 0;
18   
19   for(i = 0; i < len; i++)
20     sum += func(*list++); 
21 
22   return sum;
23 }
24 
25 
26 int main() {
27   int ia[] = {12345};
28   int sum = sum_elements(ia, 5);
29   
30   printf("%d", sum);
31 }


在sum_elements()內再呼叫func(),而且這兩個function都能做硬體加速。

c2h_07.gif

c2h_08.gif


完整程式碼下載
DE2_NIOS_c2h.7z
DE2_NIOS_c2h_function_call.7z

Conclusion
透過C2H,能讓你在不改變軟體開發流程下,快速的使用硬體加速,這對縮短time to market幫助很大。也讓你在不需要了解硬體描述語言下,就能開發硬體。所以C語言只能開發軟體和韌體?透過C2H compiler,C語言也可以開發硬體喔!!

See Also
(轉貼) 快速可編程單晶片系統開發與ANSI C語言功能的硬體加速 (IC Design) (C/C++) (News)
(原創) 如何自己用SOPC Builder建立一個能在DE2上跑μC/OS-II的Nios II系統? (IC Design) (DE2) (Quartus II) (Nios II) (SOPC Builder) (μC/OS-II)
(原創) 如何破解Quartus II 7.2 SP1? (IC Design) (Quartus II) (Nios II)

Reference
Automated Generation of Hardware Accelerators With Direct Memory Access From ANSI/ISO Standard C Functions
Nios II Software Developer's Handbook

posted on 2008-04-22 20:52  真 OO无双  阅读(7671)  评论(21编辑  收藏  举报

导航