<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RiscV &#8211; richliu&#039;s blog</title>
	<atom:link href="https://blog.richliu.com/category/computer/hardware/riscv/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.richliu.com</link>
	<description>Linux, 工作, 生活, 家人</description>
	<lastBuildDate>Sat, 11 Oct 2025 15:42:09 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>
	<item>
		<title>Run verilog Program on Lattice ECP5 Versa with OpenSource Tools</title>
		<link>https://blog.richliu.com/2025/10/11/6424/run-verilog-program-on-lattice-ecp5-versa-with-opensource-tools/</link>
					<comments>https://blog.richliu.com/2025/10/11/6424/run-verilog-program-on-lattice-ecp5-versa-with-opensource-tools/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Sat, 11 Oct 2025 15:37:56 +0000</pubDate>
				<category><![CDATA[IC Design]]></category>
		<category><![CDATA[RiscV]]></category>
		<category><![CDATA[ECP5]]></category>
		<category><![CDATA[FPGA]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=6424</guid>

					<description><![CDATA[<p>其實也不是我願意這樣做，Lattice Diamond 明明支援我手上的 FPGA ，實際上就是找不到我這一塊 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/10/11/6424/run-verilog-program-on-lattice-ecp5-versa-with-opensource-tools/">Run verilog Program on Lattice ECP5 Versa with OpenSource Tools</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>其實也不是我願意這樣做，Lattice Diamond 明明支援我手上的 FPGA ，實際上就是找不到我這一塊 ECP5 Versa Board ，IC 型是 LFE5UM-45F ，而 Lattice Diamond 只支援 LEF5U-45F 。</p>



<p>所以只能用之前的 Open Source 開發環境編 Verilog 程式。首先請參照<a href="https://blog.richliu.com/2025/08/27/6391/build-risc-v-on-ubuntu-linux/">Build Risc-V on Ubuntu Linux</a>設定開發環境</p>



<span id="more-6424"></span>



<p>最難的部份是找到 FPGA 的 clock input pin ，每一家 LLM 給的值都是錯的。實際上就是 User Manual 以下這段，不過我找到是從 litex-board 抓出來才知道原來是這一段，給個 table 啊，這文件不行，其它的 LED dip switch 都給了，clock 不給.</p>



<pre class="wp-block-preformatted">An on-board 100 MHz LVDS oscillator is provided for general purpose use. This clock source is connected to differential inputs P3 and P4 and must be used as LVDS inputs to the FPGA. This pin pair also provides optimal interface to the FPGA PLL for customized use.</pre>



<p>P3 and P4 pin 利用 LVDS 當 input. 這樣就可以寫 lpf 檔案，只能能抓出 clk ，而且知道FPGA ball number 要跟 verilog 對應。這樣就可以將LED 也抓進來了<br />而且看起來目前 FPGA 主要就是靠這兩根 pin 做 clk  輸入，100Mhz，如果是當 PCIe device 時也可以從 PCIe 那邊接 PCIe 的 clock 進來</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1636" height="466" src="https://blog.richliu.com/wp-content/uploads/2025/10/image.png" alt="" class="wp-image-6425" srcset="https://blog.richliu.com/wp-content/uploads/2025/10/image.png 1636w, https://blog.richliu.com/wp-content/uploads/2025/10/image-600x171.png 600w, https://blog.richliu.com/wp-content/uploads/2025/10/image-768x219.png 768w, https://blog.richliu.com/wp-content/uploads/2025/10/image-1536x438.png 1536w, https://blog.richliu.com/wp-content/uploads/2025/10/image-816x232.png 816w" sizes="(max-width: 1636px) 100vw, 1636px" /></figure>



<p>接下來我們就可以編 mapping file ，像是 ecp5evn.lpf 內容如下</p>



<pre class="wp-block-preformatted">LOCATE COMP "clk100" SITE "P3";<br />IOBUF PORT "clk100" IO_TYPE=LVDS;<br /><br /><br />LOCATE COMP "rst" SITE "H2";<br />IOBUF PORT "rst" IO_TYPE=LVCMOS33 PULLMODE=DOWN;<br /><br /># 將 Verilog 中的 "led" port 定位到 FPGA 的 LED output<br />LOCATE COMP "led[0]" SITE "F16";<br />IOBUF PORT "led[0]" IO_TYPE=LVCMOS33;<br />LOCATE COMP "led[1]" SITE "E17";<br />IOBUF PORT "led[1]" IO_TYPE=LVCMOS33;<br />LOCATE COMP "led[2]" SITE "F18";<br />IOBUF PORT "led[2]" IO_TYPE=LVCMOS33;<br />LOCATE COMP "led[3]" SITE "F17";<br />IOBUF PORT "led[3]" IO_TYPE=LVCMOS33;<br />LOCATE COMP "led[4]" SITE "E18";<br />IOBUF PORT "led[4]" IO_TYPE=LVCMOS33;<br />LOCATE COMP "led[5]" SITE "D18";<br />IOBUF PORT "led[5]" IO_TYPE=LVCMOS33;<br />LOCATE COMP "led[6]" SITE "D17";<br />IOBUF PORT "led[6]" IO_TYPE=LVCMOS33;<br />LOCATE COMP "led[7]" SITE "E16";<br />IOBUF PORT "led[7]" IO_TYPE=LVCMOS33;<br /></pre>



<p>然後寫一個簡單的 verilog 程式驗證我們的 code 是不是對的，像是一顆簡單的 CPU，然後寫了一個簡單的程式計算 1+10 </p>



<pre class="wp-block-code"><code>module aCPU (
    input  clk,   
    input  rst,
    output reg &#91;7:0] led,    // LED output
    output reg &#91;7:0] debug_led    // LED output
);
    // command
    parameter OP_NOP = 4'b0000   ;
    parameter OP_LI  = 4'b0001   ;
    parameter OP_ADD = 4'b0010   ;
    parameter OP_ADDI= 4'b0011   ; // copy s_reg + imm &#91;1:0] reg to d_reg
    parameter OP_BNER0 = 4'b0100   ;
    parameter OP_OUT = 4'b0101   ;
    parameter OP_HALT = 4'b0110   ;
    reg &#91;3:0] pc;
    reg &#91;7:0] register &#91;0:3];
    reg &#91;15:0] ir;   // ir = 指令暫存

    wire &#91;3:0] opcode /* verilator public */ = ir&#91;15:12];
    wire &#91;1:0] s_reg  = ir&#91;11:10];
    wire &#91;1:0] d_reg  = ir&#91;9:8];
    wire &#91;7:0] value  = ir&#91;7:0];

    reg &#91;15:0] program_memory &#91;0:15];


    // 初始化程式記憶體
    initial begin
        // 程式: 計算 1 + 10 並輸出
        program_memory&#91;0] = {OP_LI, 2'b00, 2'b00, 8'd1};    // LI only load to d_REG. s_reg no work
        program_memory&#91;1] = {OP_LI, 2'b01, 2'b01, 8'd11};   //
        program_memory&#91;2] = {OP_ADDI, 2'b10, 2'b10, 8'b0000}; // R3 = R2 + IMM
        program_memory&#91;3] = {OP_ADD, 2'b00, 2'b00, 8'b0001}; // R2 = R0 + IMM => R0
        program_memory&#91;4] = {OP_BNER0, 2'b00, 2'b01,8'b00000010}; // R0 跟 R1 比若不相同跳到 2
        program_memory&#91;5] = {OP_OUT,  2'b10, 2'b00, 8'b00000000}; // 輸出 s_reg to serial
        program_memory&#91;6] = {OP_HALT, 2'b10, 2'b01, 8'b00000000};     // 停止
    end
    always @(posedge clk or posedge rst) begin
        if (rst) begin
            pc &lt;= 0;
            register&#91;0] &lt;= 0;
            register&#91;1] &lt;= 0;
            register&#91;2] &lt;= 0;
            register&#91;3] &lt;= 0;
            led &lt;= 8'b11111111;
            debug_led &lt;= 8'b11111111;
        end
        else begin
            pc &lt;= pc + 1;
            ir &lt;= program_memory&#91;pc];
            // $display("pc&#91;%d] ir:%x reg&#91;0]:%d reg&#91;1]:%d reg&#91;2]:%d value: %x \n", pc, ir, register&#91;0], register&#91;1], register&#91;2], value);
            case (opcode)
                 OP_NOP: begin
                 end
                 OP_LI: begin
                    register&#91;s_reg] &lt;= value;
                 end
                 OP_ADD: begin
                    register&#91;d_reg] &lt;= register&#91;s_reg] + value;
                 end
                 OP_ADDI: begin
                    register&#91;d_reg] &lt;= register&#91;s_reg] + register&#91;value&#91;1:0]];
                 end
                 OP_BNER0: begin
                    if (register&#91;s_reg] != register&#91;d_reg]) begin
                        pc &lt;= value&#91;3:0];
                    end
                 end
                 OP_OUT: begin
                    led &lt;= register&#91;s_reg];
                 end
                 OP_HALT: begin
                    pc &lt;= pc - 1;
                 end
                 default: begin
                    // $display("Error No this command OP_CODE: %x ir: %x ", opcode, ir);
                 end
            endcase
        end
    end
endmodule</code></pre>



<p>當然為了這個程式就需要寫一個 test bench 檔案，就叫 ${PRJNAME}_tb.cpp <br />這不僅僅是當 test bench ，後來在弄的過程才知道可以靠 $display 顯示電路內的狀態，比幻想簡單太多了</p>



<pre class="wp-block-code"><code>#include &lt;stdio.h>
#include &lt;stdlib.h>
#include &lt;assert.h>
#include "VsCPU.h"
#include "verilated.h"

#include "verilated_vcd_c.h"

int main (int argc, char** argv, char** env){

    VerilatedContext* contextp = new VerilatedContext;
    contextp->commandArgs(argc, argv);
    VsCPU* top = new VsCPU{contextp};

    VerilatedVcdC* tfp = new VerilatedVcdC;
    contextp->traceEverOn(true); // open trace function
    top->trace(tfp, 99);
    tfp->open("wave.vcd");
    int clk = 0;

    int cycles = 0;
    while (!contextp->gotFinish() &amp;&amp; (cycles &lt; 100) ) {
      clk = !clk;
      top->clk100 = clk;
      top->eval();
      tfp->dump(contextp->time()); // dump wave
      contextp->timeInc(1); // move time to next clock

      if (cycles > 50) {
          // 檢查 LED 輸出是否為預期值 (1 + 10 = 11)
          if (top->led == (u_int8_t) ~0xc8) {
              printf("SUCCESS: 1 + 10 = %d\n", top->led);
              break;
          }
      }
      cycles++;
      if (cycles >= 99) {
          printf("TIMEOUT: Expected result not reached\n");
      }

    }
    //
    if (tfp) {
        tfp->close();
        delete tfp;
    }
    delete top;
    return 0;
}
</code></pre>



<p>然後撰寫一個 Makefile ，可以直接 Compile Code 變 bitstream 而且可以上傳到 FPGA 上.</p>



<pre class="wp-block-preformatted">DEVICE = um-45k<br />PACKAGE = CABGA381<br /><br />PRJNAME=aCPU<br /><br />all: ${PRJNAME}.bit<br /><br />${PRJNAME}.json: ${PRJNAME}.v<br />        yosys -p "synth_ecp5 -json ${PRJNAME}.json" ${PRJNAME}.v<br /><br />${PRJNAME}_out.config: ${PRJNAME}.json<br />        nextpnr-ecp5 --$(DEVICE) --package $(PACKAGE) --json ${PRJNAME}.json \<br />        --lpf ecp5evn.lpf --textcfg ${PRJNAME}_out.config<br /><br />${PRJNAME}.bit: ${PRJNAME}_out.config<br />        ecppack --svf ${PRJNAME}.svf ${PRJNAME}_out.config ${PRJNAME}.bit<br /><br />sim:<br />        verilator -Wall --cc --exe --build --trace ${PRJNAME}.v ${PRJNAME}_tb.cpp<br /><br />program:<br />        openFPGALoader -b ecp5_evn ${PRJNAME}.bit<br /><br />clean:<br />        rm -f *.json *.config *.bit *.svf</pre>



<p>make sim 之後執行 ./obj_dir/VaCPU 就可以看到程式輸出的結果，果然還是印出來比較簡單 debug</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1174" height="769" src="https://blog.richliu.com/wp-content/uploads/2025/10/image-1.png" alt="" class="wp-image-6426" srcset="https://blog.richliu.com/wp-content/uploads/2025/10/image-1.png 1174w, https://blog.richliu.com/wp-content/uploads/2025/10/image-1-600x393.png 600w, https://blog.richliu.com/wp-content/uploads/2025/10/image-1-768x503.png 768w, https://blog.richliu.com/wp-content/uploads/2025/10/image-1-816x535.png 816w" sizes="(max-width: 1174px) 100vw, 1174px" /></figure>



<p>同時也會產生 wave.vcd  這時可以用 gtkwave 看波形</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1236" height="834" src="https://blog.richliu.com/wp-content/uploads/2025/10/image-2.png" alt="" class="wp-image-6427" srcset="https://blog.richliu.com/wp-content/uploads/2025/10/image-2.png 1236w, https://blog.richliu.com/wp-content/uploads/2025/10/image-2-600x405.png 600w, https://blog.richliu.com/wp-content/uploads/2025/10/image-2-768x518.png 768w, https://blog.richliu.com/wp-content/uploads/2025/10/image-2-816x551.png 816w" sizes="(max-width: 1236px) 100vw, 1236px" /></figure>



<p>make program 之後就可以燒到 FPGA 板子上啦，輸出的燈號是反向的</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1280" height="960" src="https://blog.richliu.com/wp-content/uploads/2025/10/image-3.png" alt="" class="wp-image-6428" srcset="https://blog.richliu.com/wp-content/uploads/2025/10/image-3.png 1280w, https://blog.richliu.com/wp-content/uploads/2025/10/image-3-600x450.png 600w, https://blog.richliu.com/wp-content/uploads/2025/10/image-3-768x576.png 768w, https://blog.richliu.com/wp-content/uploads/2025/10/image-3-816x612.png 816w" sizes="(max-width: 1280px) 100vw, 1280px" /></figure>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/10/11/6424/run-verilog-program-on-lattice-ecp5-versa-with-opensource-tools/">Run verilog Program on Lattice ECP5 Versa with OpenSource Tools</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2025/10/11/6424/run-verilog-program-on-lattice-ecp5-versa-with-opensource-tools/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Running Risc-V Linux on Lattice Versa ECP5 EVB</title>
		<link>https://blog.richliu.com/2025/09/21/6411/running-risc-v-linux-on-lattice-versa-ecp5-evb/</link>
					<comments>https://blog.richliu.com/2025/09/21/6411/running-risc-v-linux-on-lattice-versa-ecp5-evb/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Sun, 21 Sep 2025 06:50:27 +0000</pubDate>
				<category><![CDATA[Embedded]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[IC Design]]></category>
		<category><![CDATA[RiscV]]></category>
		<category><![CDATA[ECP5]]></category>
		<category><![CDATA[FPGA]]></category>
		<category><![CDATA[Linux-on-lite-vexriscv]]></category>
		<category><![CDATA[LiteX]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=6411</guid>

					<description><![CDATA[<p>大部份的環境都要用上一篇(Build Risc-V on Ubuntu Linux)相同的環境，但是其實還有更 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/09/21/6411/running-risc-v-linux-on-lattice-versa-ecp5-evb/">Running Risc-V Linux on Lattice Versa ECP5 EVB</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>大部份的環境都要用上一篇(<a href="https://blog.richliu.com/2025/08/27/6391/build-risc-v-on-ubuntu-linux/">Build Risc-V on Ubuntu Linux</a>)相同的環境，但是其實還有更多的問題，中間也踩了不少地雷，還好都不算難解的問題</p>



<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#lattice-versa-evb-board-晶片型號不同">Lattice Versa EVB Board 晶片型號不同</a></li><li><a href="#連接上-evb-board">連接上 EVB Board</a></li><li><a href="#燒錄-bitstream-到板子上">燒錄 bitstream 到板子上</a></li><li><a href="#設定-udev">設定 udev</a></li></ul></nav></div>



<span id="more-6411"></span>



<h3 class="wp-block-heading" id="lattice-versa-evb-board-晶片型號不同">Lattice Versa EVB Board 晶片型號不同</h3>



<p>這個是最主要的問題，因為文件並不會提到這件事，都會用 versa_ecp5 帶過，但是實際上 versa_ecp5 是使用 LFE5UM5G-85F 的晶片，晶片比較新，邏輯閘數也比較多。一般市面上賣的 versa_ecp5 是 LFE5UM5G-45F，一片約在一萬元左右。<br />我從淘寶看到一塊比較便宜的 LFE5UM5G-45F ，結果買回來後是 LFE5UM-45F ，沒有 5G。查詢網路上的資料，大概只差在 SEDERS 最高頻可以到 5G ，還有速度比較快。</p>



<p>但是 LiteX default versa_ecp5 是預設用 LFE5UM5G-85F ，在燒錄時就會失敗，ID 不一樣</p>



<p>此時我們就要重編一次 Linux-on-litex-vexriscv ，加上 device 的參數，這樣就可以了</p>



<pre class="wp-block-code"><code>./make.py --device LFE5UM --board=versa_ecp5 --cpu-count=1 --build</code></pre>



<p>編出來的 bitstream 和 fpga hardware 不同的錯誤訊息，可以看到 hardware id 不一樣</p>



<pre class="wp-block-preformatted">$ openFPGALoader -b ecp5_evn versa_ecp5.bit<br />empty<br />Jtag frequency : requested 6.00MHz    -&gt; real 6.00MHz<br />Open file: DONE<br />b3bdffff<br />Parse file: DONE<br />mismatch between target's idcode and bitstream idcode<br />        bitstream has <strong>0x81112043 </strong>hardware requires <strong>0x01112043</strong><br />Error: Failed to program FPGA: std::exception</pre>



<h3 class="wp-block-heading" id="連接上-evb-board">連接上 EVB Board </h3>



<p>EVB Board 應該支援 ethernet load ，但是我還沒試出來，就當 TBD 吧，看設定檔內 ethernet 是可以用的</p>



<p>EVB Board 上的 FT2232H 支援 JTAG 和 Serial Port ，Linux kernel 的 image 可以從 serial port load，不過速度好慢啊</p>



<p>我之前用 Diamond programmer 要燒錄編出來的 bitstream file ，但是因為 id mismatch 的關係所以燒不上去，所以我最後是找了一台乾淨的 Linux 直接接 EVB Board 的 USB （要注意，這塊版子的 USB Port 是 mini-USB ，如果沒附線要有支援 mini-USB 的線），就這樣找到最後才發現根本型號不一樣，所以這篇就不會提到 Diamond programmer</p>



<p>記得接上之前要將 FTDI 的J50跳線改成下圖這樣，要跳過 iSPclock </p>



<p>（原文 <strong>Note</strong>: If you are using a Versa board, you will need to change J50 to bypass the iSPclock. Re-arrange the jumpers to connect pins 1-2 and 3-5 (leaving one jumper spare). See p19 of the Versa Board user guide.）</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="699" height="627" src="https://blog.richliu.com/wp-content/uploads/2025/09/image.png" alt="" class="wp-image-6412" srcset="https://blog.richliu.com/wp-content/uploads/2025/09/image.png 699w, https://blog.richliu.com/wp-content/uploads/2025/09/image-600x538.png 600w" sizes="(max-width: 699px) 100vw, 699px" /></figure>



<p>接上去之後，上電之後，Linux 下會出現 /dev/ttyUSB0 是 JTAG 的介面，/dev/ttyUSB1 是 serial port 的介面</p>



<p>在另一個視窗先執行 litex_term（記得在 venv 環境下執行）</p>



<pre class="wp-block-code"><code>litex_term --images=images/boot.json /dev/ttyUSB1</code></pre>



<h3 class="wp-block-heading" id="燒錄-bitstream-到板子上">燒錄 bitstream 到板子上</h3>



<p>最後一步就是燒錄上去了，我是用 OpenFPGAloader ，以下是安裝步驟</p>



<pre class="wp-block-code"><code>sudo apt install -y  libftdi1-dev

cd /nvme/cpu
git clone https://github.com/trabucayre/openFPGALoader
cd openFPGALoader
mkdir -p build
cd build
cmake ../
make -j `nproc`
sudo make install </code></pre>



<p>然後到 linux-on-litex-vexriscv 下用 OpenFPGAloader 暫時 load bitstream 上去，這速度很快，這樣就不用燒上去了</p>



<pre class="wp-block-code"><code>cd /nvme/cpu/linux-on-litex-vexriscv/build/versa_ecp5/gateware
$ openFPGALoader -b ecp5_evn versa_ecp5.bit
empty
Jtag frequency : requested 6.00MHz    -&gt; real 6.00MHz
Open file: DONE
b3bdffff
Parse file: DONE
Enable configuration: DONE
SRAM erase: DONE
Loading: &#91;==================================================] 100.00%
Done
Disable configuration: DONE</code></pre>



<p>如果順利，可以在剛剛執行 litex_term 上看到 load image 訊息<br />如果用的是上一篇文章內提到的先下載好 RiscV 的 Linux image 和 rootfs ，就可以看到 Linux boot 了</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="943" height="927" src="https://blog.richliu.com/wp-content/uploads/2025/09/image-1.png" alt="" class="wp-image-6413" srcset="https://blog.richliu.com/wp-content/uploads/2025/09/image-1.png 943w, https://blog.richliu.com/wp-content/uploads/2025/09/image-1-600x590.png 600w, https://blog.richliu.com/wp-content/uploads/2025/09/image-1-768x755.png 768w, https://blog.richliu.com/wp-content/uploads/2025/09/image-1-88x88.png 88w, https://blog.richliu.com/wp-content/uploads/2025/09/image-1-816x802.png 816w" sizes="(max-width: 943px) 100vw, 943px" /></figure>



<p>這年代可以自己編一顆 CPU 跑起來也真的是很有趣</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1353" height="927" src="https://blog.richliu.com/wp-content/uploads/2025/09/image-2.png" alt="" class="wp-image-6414" srcset="https://blog.richliu.com/wp-content/uploads/2025/09/image-2.png 1353w, https://blog.richliu.com/wp-content/uploads/2025/09/image-2-600x411.png 600w, https://blog.richliu.com/wp-content/uploads/2025/09/image-2-768x526.png 768w, https://blog.richliu.com/wp-content/uploads/2025/09/image-2-816x559.png 816w" sizes="(max-width: 1353px) 100vw, 1353px" /></figure>



<p>以下未完成，待續</p>



<h3 class="wp-block-heading" id="設定-udev">設定 udev</h3>



<p>這塊版子看起來要設定過 udev 才能被 LiteX 抓到，首先新增檔案 /etc/udev/rules.d/99-ftdi.rules<br />內容如下，記得 GROUP=換成 Linux login 帳號</p>



<pre class="wp-block-preformatted">SUBSYSTEMS=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", MODE="0660", GROUP="使用者名稱"</pre>



<p>然後執行以下命令之後再重新上電即可</p>



<pre class="wp-block-code"><code>sudo udevadm control --reload-rules
sudo udevadm trigger</code></pre>



<p>這個命令也可以像 OpenFPGAloader 一樣將 bitstream load 到 FPGA 上</p>



<pre class="wp-block-code"><code>./make.py --device LFE5UM --board=versa_ecp5 --cpu-count=1 --load </code></pre>



<p>45F Gate count 夠放二顆 CPU ，所以可以跑 SMP</p>



<pre class="wp-block-code"><code>./make.py --device LFE5UM --board=versa_ecp5 --cpu-count=2 --build
./make.py --device LFE5UM --board=versa_ecp5 --cpu-count=2 --load </code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1299" height="1033" src="https://blog.richliu.com/wp-content/uploads/2025/09/image-3.png" alt="" class="wp-image-6418" srcset="https://blog.richliu.com/wp-content/uploads/2025/09/image-3.png 1299w, https://blog.richliu.com/wp-content/uploads/2025/09/image-3-600x477.png 600w, https://blog.richliu.com/wp-content/uploads/2025/09/image-3-768x611.png 768w, https://blog.richliu.com/wp-content/uploads/2025/09/image-3-816x649.png 816w" sizes="(max-width: 1299px) 100vw, 1299px" /></figure>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/09/21/6411/running-risc-v-linux-on-lattice-versa-ecp5-evb/">Running Risc-V Linux on Lattice Versa ECP5 EVB</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2025/09/21/6411/running-risc-v-linux-on-lattice-versa-ecp5-evb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Build Risc-V on Ubuntu Linux</title>
		<link>https://blog.richliu.com/2025/08/27/6391/build-risc-v-on-ubuntu-linux/</link>
					<comments>https://blog.richliu.com/2025/08/27/6391/build-risc-v-on-ubuntu-linux/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Wed, 27 Aug 2025 13:04:18 +0000</pubDate>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[IC Design]]></category>
		<category><![CDATA[RiscV]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=6391</guid>

					<description><![CDATA[<p>打算弄個 Risc-V 做個自己的小 Project ，摸了一下大概才知道怎麼入門這篇是筆記，適合初學者 OS [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/08/27/6391/build-risc-v-on-ubuntu-linux/">Build Risc-V on Ubuntu Linux</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>打算弄個 Risc-V 做個自己的小 Project ，摸了一下大概才知道怎麼入門<br />這篇是筆記，適合初學者</p>



<p>OS: ubuntu 22.04 / 24.04 </p>



<span id="more-6391"></span>



<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#下載和-compile-source-code">下載和 compile source code</a><ul><li><a href="#install-ubuntu-packages">Install Ubuntu Packages</a></li><li><a href="#install-sbt">Install SBT</a></li><li><a href="#install-open-jdk">Install OpenJDK</a></li><li><a href="#重編-cmake">重編 cmake</a></li><li><a href="#下載-linux-on-litex-vexriscv">下載 linux-on-litex-vexriscv</a></li><li><a href="#編-buildroot-編出-risc-v-的-cross-compiler">編 Buildroot 編出 Risc-V 的 Cross Compiler</a></li><li><a href="#iverilog-版本太舊，重編-iverilog">iverilog 版本太舊，重編 iverilog</a></li><li><a href="#下載-yosys-prjtrellis-nextpnr">下載 yosys/prjtrellis/nextpnr</a></li><li><a href="#下載和編-litex">下載和編 litex</a></li></ul></li><li><a href="#跑-simulation">跑 Simulation</a><ul><li><a href="#跑-simulation-1">跑 simulation</a></li><li><a href="#build-fpga-bitstream-image">Build FPGA Bitstream Image</a></li></ul></li><li><a href="#error-messages">Error Messages</a><ul><li><a href="#ram-1-w-1-rs-generic-v-2-8-timescale-missing-on-this-module-as-other-modules-have-it">Ram_1w_1rs_Generic.v:2:8: Timescale missing on this module as other modules have it</a></li><li><a href="#error-module-eclkbridgecs-referenced-in-module-orange-crab-in-cell-eclkbridgecs-is-not-part-of-the-design">ERROR: Module `\ECLKBRIDGECS&#8217; referenced in module `\orange_crab&#8217; in cell `\ECLKBRIDGECS&#8217; is not part of the design</a></li></ul></li><li><a href="#reference">Reference</a></li></ul></nav></div>



<h2 class="wp-block-heading" id="下載和-compile-source-code">下載和 compile source code </h2>



<p>理論上用 linux-on-litex-vexriscv 照著作就可以了，不過中間還是有很多邊邊角角的小問題要處理 ，如果對系統不夠熟的人會在這邊卡很久。</p>



<h3 class="wp-block-heading" id="install-ubuntu-packages">Install Ubuntu Packages </h3>



<pre class="wp-block-code"><code>sudo apt-get install -y build-essential device-tree-compiler wget git python3-setuptools python3-dev
sudo apt-get install -y libssl-dev libncurses-dev libeigen3-dev
sudo apt-get install -y libevent-dev libjson-c-dev libboost-iostreams-dev
sudo apt-get install -y libreadline-dev tcl-dev libffi-dev bison flex gawk gtkwave

sudo apt-get install -y file bc unzip meson 
sudo apt-get install -y libboost-dev libboost-filesystem-dev libboost-thread-dev libboost-program-options-dev libboost-python-dev libboost-all-dev


# 24.04
sudo apt-get install -y python3-venv </code></pre>



<h3 class="wp-block-heading" id="install-sbt">Install SBT</h3>



<pre class="wp-block-code"><code>sudo apt-get update
sudo apt-get install apt-transport-https curl gnupg -yqq
echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | sudo tee /etc/apt/sources.list.d/sbt.list
echo "deb https://repo.scala-sbt.org/scalasbt/debian /" | sudo tee /etc/apt/sources.list.d/sbt_old.list
curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&amp;search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | sudo -H gpg --no-default-keyring --keyring gnupg-ring:/etc/apt/trusted.gpg.d/scalasbt-release.gpg --import
sudo chmod 644 /etc/apt/trusted.gpg.d/scalasbt-release.gpg
sudo apt-get update
sudo apt-get -y install sbt</code></pre>



<h3 class="wp-block-heading" id="install-open-jdk">Install OpenJDK</h3>



<p>這邊我們用 OpenJDK 17 </p>



<pre class="wp-block-code"><code>sudo apt-get install -y openjdk-17-jdk openjdk-17-jre</code></pre>



<h3 class="wp-block-heading" id="重編-cmake">重編 cmake </h3>



<p>內建的 cmake 看起來版本太低不能用，所以要下載 cmake 手動編譯</p>



<pre class="wp-block-code"><code>wget https://github.com/Kitware/CMake/releases/download/v4.0.3/cmake-4.0.3.tar.gz
tar xvf cmake-4.0.3.tar.gz
cd cmake-4.0.3
./configure
make -j `nproc`
sudo make install</code></pre>



<h3 class="wp-block-heading" id="下載-linux-on-litex-vexriscv">下載 linux-on-litex-vexriscv</h3>



<p>編  buildroot 要指定 linux-on-litex-vexriscv ，所以先下載</p>



<pre class="wp-block-code"><code>git clone https://github.com/litex-hub/linux-on-litex-vexriscv</code></pre>



<h3 class="wp-block-heading" id="編-buildroot-編出-risc-v-的-cross-compiler">編 Buildroot 編出 Risc-V 的 Cross Compiler </h3>



<p>我因為是在其他 CPU 的平台上跑，所以只能自己編 buildroot ，還好 buildroot 已經很成熟了，只是要花時間編出來而已，第一次有錯誤訊息沒關係，確認 output/host/bin/ 下有&#8221;riscv32-&#8221; 開頭的 toolchain 就可以了<br />等到編完 litex 之後再回來重編一次 buildroot 即可</p>



<pre class="wp-block-code"><code>git clone http://github.com/buildroot/buildroot
cd buildroot
make BR2_EXTERNAL=../linux-on-litex-vexriscv/buildroot/ litex_vexriscv_defconfig
make</code></pre>



<h3 class="wp-block-heading" id="iverilog-版本太舊，重編-iverilog">iverilog 版本太舊，重編 iverilog</h3>



<p>iverilog 和 yosys 都要注意不能用到 ubuntu 系統內建的版本，它們都太舊了<br />都要手動重裝新版的</p>



<pre class="wp-block-code"><code>sudo apt-get install -y autoconf gperf make gcc g++ bison flex
git clone https://github.com/steveicarus/iverilog.git
cd iverilog
autoconf
./configure 
sudo make -j `nproc` install </code></pre>



<h3 class="wp-block-heading" id="下載-yosys-prjtrellis-nextpnr">下載 yosys/prjtrellis/nextpnr </h3>



<p>主要是參照這篇 <a href="https://ithelp.ithome.com.tw/articles/10266953" target="_blank" rel="noopener">LiteX/VexRiscv 簡介與使用 (二) 始有晝夜</a> ，但是因為已經是幾年前了，有些東西有點小差異，不過不影響，目錄就用我的工作目錄</p>



<p>這邊要小心一點，最好是先清空相關的程式碼，像是 /nvme/cpu/sysroot/* 和 /usr/local/bin 下的某些檔案，我就吃了虧卡關了很久</p>



<p>如果是 Ubuntu 24.04 ，要先照下面一節的方式先切到 venv 的環境再安裝</p>



<pre class="wp-block-code"><code># 如果還沒做就先將路徑加上去
# 編 buildroot 時不能加入 buildroot 的 path，要編 litex 時再加入即可export PATH=$PATH:/usr/local/bin:/nvme/cpu/buildroot/output/host/bin/:/nvme/cpu/sysroot/bin/:~/.local/bin:

mkdir -p /nvme/cpu/sysroot
cd /nvme/cpu
git clone --recursive https://github.com/YosysHQ/yosys
git clone --recursive https://github.com/YosysHQ/prjtrellis
git clone --recursive https://github.com/YosysHQ/nextpnr
cd yosys
make PREFIX="/nvme/cpu/sysroot" CONFIG=gcc ENABLE_TCL=1 ENABLE_ABC=1 ENABLE_GLOB=1 ENABLE_PLUGINS=1 ENABLE_LIBYOSYS=1 ENABLE_PROTOBUF=1 ENABLE_ZLIB=1 ENABLE_PYOSYS=1 test all install -j `nproc`
cd ..
cd prjtrellis/libtrellis
cmake -DCMAKE_INSTALL_PREFIX=/nvme/cpu/sysroot .
make install -j `nproc`
cd ../../
cd nextpnr
cmake . -DARCH=ecp5 -DTRELLIS_INSTALL_PREFIX=/nvme/cpu/sysroot -B build
cd build
sudo make install -j `nproc`
</code></pre>



<h3 class="wp-block-heading" id="下載和編-litex">下載和編 litex </h3>



<p>ubuntu 22.04 可以用 &#8211;user ，但是 24.04 就強制要用 venv　要先有環境 </p>



<pre class="wp-block-code"><code>cd /nvme/cpu
python3 -m venv litex-venv
source litex-venv/bin/activate</code></pre>



<p>Ubuntu 22.04</p>



<pre class="wp-block-code"><code>cd /nvme/cpu
wget https://raw.githubusercontent.com/enjoy-digital/litex/master/litex_setup.py
chmod +x litex_setup.py
./litex_setup.py --init --install --user</code></pre>



<p>Ubuntu 24.04</p>



<pre class="wp-block-code"><code>./litex_setup.py --init --install</code></pre>



<p>Compile Lite-X</p>



<pre class="wp-block-code"><code>cd /nvme/cpu/linux-on-litex-vexriscv
./make.py --board=versa_ecp5 --cpu-count=1 --build</code></pre>



<h2 class="wp-block-heading" id="跑-simulation">跑 Simulation </h2>



<p>順利的話這時環境應該 OK 了，可以跑 simulation 了</p>



<p>先下載 Linux images 到 images</p>



<pre class="wp-block-code"><code>cd /nvme/cpu/linux-on-litex-vexriscv/images
wget https://github.com/litex-hub/linux-on-litex-vexriscv/files/8331338/linux_2022_03_23.zip
unzip linux_2022_03_23.zip</code></pre>



<h3 class="wp-block-heading" id="跑-simulation-1">跑 simulation </h3>



<p>這時就可以跑 simulation 了，不過我跑了 64 Cores 好像更慢，看起來給愈多 code 會跑的更大，像是變 SMP 或是跑到 100Mhz （一核用 1Mhz）<br />可能要花時間設參數，&#8211;jobs 到是可以設大一點</p>



<pre class="wp-block-preformatted">./sim.py<br /><br /># 或是<br />./sim.py <br /><br /># 或是<br />./sim.py  --jobs `nproc` --threads `nproc`<br /></pre>



<p><img loading="lazy" decoding="async" src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXc0cqn58FsJ3udHeeWXAoUNAOw8i-6amS6mlR9KI50IFMgPzvjH9nNR6LJI5xnEjv2Mu2Q2hnnhtVhFYTacZbEV2y7NPgI1tiukpiPoS1W04yON2UWJC0LW8XGwRR2a3Z8xnq0apw?key=08Ji1rDFx5yo_VwHwn3Aoj4y" width="497" height="600"/></p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="929" height="728" src="https://blog.richliu.com/wp-content/uploads/2025/08/image-5.png" alt="" class="wp-image-6393" style="width:528px;height:auto" srcset="https://blog.richliu.com/wp-content/uploads/2025/08/image-5.png 929w, https://blog.richliu.com/wp-content/uploads/2025/08/image-5-600x470.png 600w, https://blog.richliu.com/wp-content/uploads/2025/08/image-5-768x602.png 768w, https://blog.richliu.com/wp-content/uploads/2025/08/image-5-816x639.png 816w" sizes="(max-width: 929px) 100vw, 929px" /></figure>



<p></p>



<h3 class="wp-block-heading" id="build-fpga-bitstream-image">Build FPGA Bitstream Image </h3>



<p>以下就是編  orange_crab FPGA 板子的 command </p>



<pre class="wp-block-code"><code>./make.py --board=orange_crab --cpu-count=1 --build</code></pre>



<p>不過我編出來會 .dts failed <br />可以改編 versa_ecp5 ，這是 ecp5 的公板，目前應該只有 ecp5 可以靠Trellis/Yosys/nextpnr 這三個 Open Source 的軟體編出 FPGA 所需的 bitstream image，orange_crab 也是基於 ecp5 的 FPGA. </p>



<pre class="wp-block-preformatted">./make.py --board=versa_ecp5 --cpu-count=1 --build</pre>



<p>不過我寫這篇的時候還沒買 FPGA ，所以燒到板子上測試要等下一篇了</p>



<h2 class="wp-block-heading" id="error-messages">Error Messages </h2>



<h3 class="wp-block-heading" id="ram-1-w-1-rs-generic-v-2-8-timescale-missing-on-this-module-as-other-modules-have-it">Ram_1w_1rs_Generic.v:2:8: Timescale missing on this module as other modules have it</h3>



<p>原始錯誤訊息</p>



<pre class="wp-block-preformatted">%Error-TIMESCALEMOD: /nvme/cpu/pythondata-cpu-vexriscv-smp/pythondata_cpu_vexriscv_smp/verilog/Ram_1w_1rs_Generic.v:2:8: Timescale missing on this module as other modules have it (IEEE 1800-2017 3.14.2.2)<br />                     /nvme/cpu/pythondata-cpu-vexriscv-smp/pythondata_cpu_vexriscv_smp/verilog/VexRiscvLitexSmpCluster_Cc1_Iw32Is4096Iy1_Dw32Ds4096Dy1_ITs4DTs4_Ldw32_Ood_Hb1.v:8:8: ... Location of module with timescale<br />    8 | module VexRiscvLitexSmpCluster_Cc1_Iw32Is4096Iy1_Dw32Ds4096Dy1_ITs4DTs4_Ldw32_Ood_Hb1 (<br />      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />%Error: Exiting due to 1 error(s)<br />        ... See the manual and https://verilator.org for more assistance.<br />make: *** [/nvme/cpu/litex/litex/build/sim/core/Makefile:44: sim] Error 1<br />make: Leaving directory '/nvme/cpu/linux-on-litex-vexriscv/build/sim/gateware'<br />Testing expectations for wide_thru_priority.v ..Test: code_verilog_tutorial_fsm_full -&gt; ok<br /><br />Test: code_verilog_tutorial_parity -&gt; ok</pre>



<p>修改檔案 /nvme/cpu/pythondata-cpu-vexriscv-smp/pythondata_cpu_vexriscv_smp/verilog/Ram_1w_1rs_Generic.v 加上一行</p>



<pre class="wp-block-code"><code>diff --git a/pythondata_cpu_vexriscv_smp/verilog/Ram_1w_1rs_Generic.v b/pythondata_cpu_vexriscv_smp/verilog/Ram_1w_1rs_Generic.v
index c6e6a96..6a57a2e 100644
--- a/pythondata_cpu_vexriscv_smp/verilog/Ram_1w_1rs_Generic.v
+++ b/pythondata_cpu_vexriscv_smp/verilog/Ram_1w_1rs_Generic.v
@@ -1,3 +1,5 @@
+`timescale 1ns/1ps
+

 module Ram_1w_1rs #(
         parameter integer wordCount = 0,</code></pre>



<h3 class="wp-block-heading" id="error-module-eclkbridgecs-referenced-in-module-orange-crab-in-cell-eclkbridgecs-is-not-part-of-the-design">ERROR: Module `\ECLKBRIDGECS&#8217; referenced in module `\orange_crab&#8217; in cell `\ECLKBRIDGECS&#8217; is not part of the design</h3>



<pre class="wp-block-preformatted">Generating RTLIL representation for module `\VexRiscvLitexSmpCluster_Cc1_Iw32Is4096Iy1_Dw32Ds4096Dy1_ITs4DTs4_Ldw128_Ood_Hb1'.      ERROR: Module `\ECLKBRIDGECS' referenced in module `\orange_crab' in cell `\ECLKBRIDGECS' is not part of the design.   </pre>



<p>yosys 太舊，我是裝到系統的 yosys ，移除掉系統的 yosys 再自己編 yosys 就可以了</p>



<p></p>



<h2 class="wp-block-heading" id="reference">Reference</h2>



<p><a href="https://ithelp.ithome.com.tw/articles/10266953" target="_blank" rel="noopener">LiteX/VexRiscv 簡介與使用 (二) 始有晝夜</a> <br /><a href="https://github.com/enjoy-digital/litex" target="_blank" rel="noopener">LiteX</a><br /><a href="https://github.com/litex-hub/linux-on-litex-vexriscv" target="_blank" rel="noopener">linux-on-litex-vexriscv</a><br /><a href="https://github.com/litex-hub/linux-on-litex-vexriscv/issues/164" target="_blank" rel="noopener">Prebuilt Bitstreams and Linux/OpenSBI images</a> (抓 Linux Kernel 用)<br /><a href="https://orangecrab-fpga.github.io/orangecrab-hardware/docs/getting-started/" target="_blank" rel="noopener">Orange Crab 主網站</a><br /><a href="https://github.com/YosysHQ/oss-cad-suite-build" target="_blank" rel="noopener">OSS CAD Suite</a><br /><a href="https://gitlab.com/buildroot.org/buildroot/" target="_blank" rel="noopener">Buildroot</a></p>



<p></p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/08/27/6391/build-risc-v-on-ubuntu-linux/">Build Risc-V on Ubuntu Linux</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2025/08/27/6391/build-risc-v-on-ubuntu-linux/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
