OpenRisc-2-C to Verilog

来源:互联网 发布:网络小额贷款运营模式 编辑:程序博客网 时间:2024/06/01 14:22

引言

如何将C语言代码转换成verilog HDL或者VHDL呢?
 

2.1 在线转换:

http://c-to-verilog.com/online.html

C-to-Verilog.com是海法(Haifa)大学高层次综合领域的一个学术研究而产生的一个网站。
这个网站所用的编译器是SystemRacer综合系统的一个修改版本。这个编译器的源码可用于研究目的,并且已经发给了很多编译器的研究组织。
此外还有一些文章可供参考,这些文章介绍了这个综合器的实现原理。

 

2.2 下载源码,然后安装,再使用。

 

源码,我已经上传,GPL3许可;还有介绍这个综合器实现原理的文章,文章我也已经上传(这些文章都是发表在顶级期刊或会议的优秀paper,需要付费才能下载的),和源码放在一起:

http://download.csdn.net/detail/rill_zhen/4797683

 

2.3 注意

需要LLVM 2.5的支持。LLVM是构架编译器(compiler)的框架系统,
以C++编写而成,用于优化以任意程序语言编写的程序的编译时间(compile-time)、链接时间(link-time)、运行时间(run-time)以及空闲时间(idle-time),
对开发者保持开放,并兼容已有脚本。
LLVM计划启动于2000年,最初由University of Illinois at Urbana-Champaign的Chris Lattner主持开展。
2006年Chris Lattner加盟Apple Inc.并致力于LLVM在Apple开发体系中的应用。
Apple也是LLVM计划的主要资助者。

 

2.4 测试:

先贴一个截屏吧:

 

C代码:

[html] view plaincopyprint?
  1. void rill_main(void)   
  2. {   
  3. int i = 0;  
  4. int j = 0;  
  5.   
  6.     for (int i=0; i<2; i++)   
  7.         j = i;   
  8. }  


点击“综合”,生成verilogHDL代码:包含模块代码和testbench代码,呵呵,连测试代码都自动生成了,想的真周到。

verilog 模块代码:

[html] view plaincopyprint?
  1. /*       This module was generated by c-to-verilog.com  
  2.  * THIS SOFTWARE IS PROVIDED BY www.c-to-verilog.com ''AS IS'' AND ANY  
  3.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
  4.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE  
  5.  * DISCLAIMED. IN NO EVENT SHALL c-to-verilog.com BE LIABLE FOR ANY  
  6.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  
  7.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES)  
  8.  *   
  9.  * Found a bug? email info@c-to-verilog.com   
  10.  */  
  11.   
  12.   
  13. module rill_main  (clk, reset, rdy,// control   
  14.     return_value); // params   
  15.  input wire clk;  
  16.  input wire reset;  
  17.  output rdy;  
  18.  reg rdy;  
  19.  output return_value;  
  20.  reg return_value;  
  21.   
  22.   
  23.   
  24.  // Number of states:1  
  25.  reg [0:0] eip;  
  26.  parameter entry0 = 1'd0;  
  27.   
  28. // Assign part (0)  
  29.   
  30.   
  31. always @(posedge clk)  
  32.  begin  
  33.   if (reset)  
  34.    begin  
  35.     $display("@hard reset");  
  36.     eip<=0;  
  37.     rdy<=0;  
  38.    end  
  39.   
  40.   
  41. // Datapath   
  42.   
  43.   
  44. // Control   
  45. case (eip)  
  46. entry0:  
  47. begin  
  48.      rdy <= 1;  
  49.  return_value <= 0;  
  50.  $finish();  
  51. end  
  52.  endcase //eip  
  53. end //always @(..)  
  54.   
  55. endmodule  
  56.   
  57.   
  58.   
  59. // -- Library components --    
  60.   
  61. module mul (clk, a, b, p);  
  62. output reg [31:0] p;  
  63. input [31:0] a;  
  64. input [31:0] b;  
  65. input clk;reg [31:0] t0;  
  66. reg [31:0] t1;  
  67. reg [31:0] t2;  
  68. reg [31:0] t3;  
  69. always @(posedge clk)begin  
  70. t0 <= a * b;  
  71. t1 <= t0;  
  72. t2 <= t1;  
  73. t3 <= t2;  
  74. <=t3;  
  75. end  
  76. endmodule  
  77.   
  78.   
  79. module div (clk, a, b, p);  
  80. output reg [31:0] p;  
  81. input [31:0] a;  
  82. input [31:0] b;  
  83. input clk;reg [31:0] t0;  
  84. reg [31:0] t1;  
  85. reg [31:0] t2;  
  86. reg [31:0] t3;  
  87. always @(posedge clk)begin  
  88. t0 <= a / b;  
  89. t1 <= t0;  
  90. t2 <= t1;  
  91. t3 <= t2;  
  92. <=t3;  
  93. end  
  94. endmodule  
  95.   
  96.   
  97. module shl (clk, a, b, p);  
  98. output reg [31:0] p;  
  99. input [31:0] a;  
  100. input [31:0] b;  
  101. input clk;reg [31:0] t0;  
  102. reg [31:0] t1;  
  103. reg [31:0] t2;  
  104. reg [31:0] t3;  
  105. always @(posedge clk)begin  
  106. t0 <= a << b;  
  107. t1 <= t0;  
  108. t2 <= t1;  
  109. t3 <= t2;  
  110. <=t3;  
  111. end  
  112. endmodule  
  113.   
  114. // Dual port memory block  
  115. module xram (out0, din0, addr0, we0, clk0,  
  116.            out1, din1, addr1, we1, clk1);  
  117.   parameter ADDRESS_WIDTH = 16;  
  118.   parameter WORD_WIDTH = 32;  
  119.   output [WORD_WIDTH-1:0] out0;  
  120.   input [WORD_WIDTH-1:0] din0;  
  121.   input [ADDRESS_WIDTH-1:0] addr0;  
  122.   input we0;  
  123.   input clk0;  
  124.   output [WORD_WIDTH-1:0] out1;  
  125.   input [WORD_WIDTH-1:0] din1;  
  126.   input [ADDRESS_WIDTH-1:0] addr1;  
  127.   input we1;  
  128.   input clk1;  
  129.   reg [WORD_WIDTH-1:0] mem[1<<ADDRESS_WIDTH-1:0];  
  130.    integer i;  
  131.    initial begin  
  132.        for (i = 0; i < (1<<(ADDRESS_WIDTH-1)); i = i + 1) begin  
  133.        mem[i] <= i;  
  134.      end  
  135.    end  
  136.   assign out0 = mem[addr0];  
  137.   assign out1 = mem[addr1];  
  138.   always @(posedge clk0)begin  
  139.       if (we0) begin  
  140.           mem[addr0] = din0;  
  141.           $display($time,"w mem[%d] == %d; in=%d",addr0, mem[addr0],din0);  
  142.       end  
  143.   end  
  144.   always @(posedge clk1)begin  
  145.       if (we1) begin  
  146.           mem[addr1] = din1;  
  147.           $display($time,"w mem[%d] == %d; in=%d",addr0, mem[addr0],din0);  
  148.       end   
  149.   end  
  150. endmodule  


 

此外还会生成testbench代码(和上面的代码在同一个文件中):

[html] view plaincopyprint?
  1.  // Test Bench   
  2.   
  3.   
  4. module rill_main_test;  
  5.  wire rdy;  
  6.  reg reset, clk;  
  7.  always #5 clk = ~clk;  
  8.  wire return_value;  
  9. rill_main instance1 (clk, reset, rdy,// control   
  10.     return_value); // params   
  11. initial begin  
  12.  clk = 0;  
  13.  $monitor("return = %b, 0x%x", rdy,  return_value);  
  14.   
  15.  // Configure the values below to test the module  
  16.  #5 reset = 1; #5 reset = 0;  
  17. end  
  18.   
  19. endmodule //main_test  


 

2.5 C to VHDL

此外还可以将C代码转换成VHDL,请参考:

http://tce.cs.tut.fi/

简介:

TCE is a toolset for designing application-specific processors (ASP) based on    the Transport triggered architecture (TTA). The toolset provides a complete    co-design flow from C programs down to synthesizable VHDL and parallel program    binaries. Processor customization points include the register files, function    units, supported operations, and the interconnection network.

    TCE has been developed internally in the Tampere University of Technology    since the early 2003. The current source code base consists of roughly 400 000    lines of C++ code.

原创粉丝点击