<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[hong]]></title><description><![CDATA[Obsidian digital garden]]></description><link>http://github.com/dylang/node-rss</link><image><url>site-lib/media/favicon.png</url><title>hong</title><link/></image><generator>Webpage HTML Export plugin for Obsidian</generator><lastBuildDate>Fri, 16 Oct 2026 01:33:43 GMT</lastBuildDate><atom:link href="site-lib/rss.xml" rel="self" type="application/rss+xml"/><pubDate>Fri, 16 Oct 2026 01:33:32 GMT</pubDate><ttl>60</ttl><dc:creator/><item><title><![CDATA[chapter-sp]]></title><description><![CDATA[push button และ slide switch เมื่อกดหรือปล่อย หน้าสัมผัส (contact) จะสั่นกระเด้งเปิด-ปิดหลายครั้งก่อนจะนิ่ง ทำให้สัญญาณมี edge เกิดขึ้นหลายครั้ง เราเรียกปรากฏการณ์นี้ว่า bounce ถ้านำสัญญาณนี้เข้าวงจรโดยตรง FPGA ที่ทำงานระดับ MHz จะอ่านทุก edge เหล่านั้นเป็น "การกดหลายครั้ง" ส่งผลให้เกิดการทำงานผิดพลาดโดยไม่ได้ตั้งใจ เช่น counter นับเกิน หรือ FSM เปลี่ยน state ผิด<img alt="img/switch-bounce.png" src="coursecontent/img/switch-bounce.png" target="_self" style="width: 687px; max-width: 100%;">ปัญหานี้แก้ได้ทาง hardware ด้วยการใส่วงจร RC กรองสัญญาณ แต่การเพิ่ม component เป็นการเพิ่ม cost และยังเพิ่มพื้นที่บน board ด้วย<br><img alt="img/switch-debounce.jpg" src="coursecontent/img/switch-debounce.jpg" target="_self" style="width: 398px; max-width: 100%;">เราแก้ปัญหานี้ได้ด้วย logic ใน FPGA โดยไม่ต้องเพิ่มอุปกรณ์ หลักการคือ sampling สัญญาณทุก ๆ 1 ms แล้วตีความว่า "กดจริง" ก็ต่อเมื่อสัญญาณใหม่นิ่งต่อเนื่องครบ N sample เท่านั้น กล่าวคือ ถ้าอ่านได้ 1 ต่อเนื่องครบ N sample จึงตีความว่าเป็น 1 และถ้าอ่านได้ 0 ต่อเนื่องครบ N sample จึงตีความว่าเป็น 0 ส่วนถ้ายังไม่ครบ ให้คง output เดิมไว้หมายเหตุ: คาบ sampling 1 ms และค่า N เป็นเพียงตัวอย่างที่นิยม ไม่ใช่มาตรฐาน ให้เลือกตามสวิตช์ที่ใช้ เช่น N = 10 ทำให้ output หน่วงประมาณ 10 ms ซึ่งน้อยกว่าที่ผู้ใช้จะรู้สึกได้ (ดูตารางด้านล่าง)ภาพด้านล่างแสดงตัวอย่างการ sampling ทุก 1 ms เมื่อ N = 5<br><img alt="img/debounce-sampling.svg" src="coursecontent/img/debounce-sampling.svg" target="_self" style="width: 1000px; max-width: 100%;">การเลือกค่า N ต้องรู้ก่อนว่าสวิตช์ bounce นานเท่าไร ตารางด้านล่างสรุปค่าทั่วไปและผลของการเลือกเวลาที่สั้นหรือนานเกินไปหลักการข้างต้นแปลงเป็น logic ใน FPGA ได้ดังนี้<br><img alt="img/d-flip-flop-latch.png" src="coursecontent/img/d-flip-flop-latch.png" target="_self" style="width: 524px; max-width: 100%;">คำว่า "Latch 1 clk" หรือ "หน่วง 1 clk" ที่คนในวงการ FPGA พูดกันเวลาเห็นโค้ดนี้ เป็นการใช้คำแสลงหรือภาษาพูดที่อาจทำให้สับสน
ในทางฮาร์ดแวร์จริงๆ ตัววงจรไม่ใช่ D-Latch แต่สิ่งที่พวกเขาหมายถึงคือ "พฤติกรรมการกักข้อมูลและส่งออกในสัญญาณนาฬิกาถัดไป"
โดยมีความหมายในแง่การทำงาน 2 ประเด็นหลักๆ ดังนี้: คำว่า "Latch" ในที่นี้เป็นคำกริยา แปลว่า "ล็อกค่า" (Latch the data)
เวลาคนพูดว่า "โค้ดนี้จะ Latch ข้อมูลไว้ 1 clk" เขาไม่ได้หมายถึงตัวอุปกรณ์ D-Latch ครับ แต่เขาใช้คำว่า Latch เป็นคำกริยา (Verb) ที่แปลว่า "การล็อก/จับ/สแนปเอาค่าข้อมูล ณ วินาทีนั้นไว้" สัญญาณอินพุต d จะถูก D Flip-Flop ล็อกค่าเอาไว้ตอนขอบขาขึ้น แล้วส่งออกไปที่ q ผลลัพธ์ทำให้เกิดการ "หน่วงเวลาไป 1 คล็อก" (1 Clock Cycle Delay)
เนื่องจาก D Flip-Flop จะทำงานเฉพาะจังหวะขอบขาขึ้นเท่านั้น ทำให้ข้อมูลที่เอาต์พุต q จะเปลี่ยนแปลงช้ากว่าอินพุต d ไป 1 สัญญาณนาฬิกาเสมอ คนทำงานสายนี้จึงมักเรียกพฤติกรรมนี้ว่าการ "Latch/Hold ค่าไว้ 1 clk" หรือ "Register ไว้ 1 clk" เพื่อใช้ในการซิงค์ข้อมูลหรือทำ Pipeline ครับ เปรียบเทียบภาษาพูด VS ภาษาฮาร์ดแวร์
ภาษาฮาร์ดแวร์ (ถูกต้องตามทฤษฎี): โค้ดนี้สร้าง D Flip-Flop (ไม่ใช่ D-Latch)
ภาษาพูดหน้างาน (ภาษาแสลง): "โค้ดนี้เอาไว้ Latch ค่า d ไว้ 1 clk แล้วค่อยส่งออก q" (หมายถึง การใช้ Flip-flop เก็บค่าและหน่วงเวลาไป 1 คล็อก)
ใน CLB FPGA มี D flip flop (ไม่ใช่ D-latch)<br><img alt="img/xilinx-CLB.png" src="coursecontent/img/xilinx-clb.png" target="_self" style="width: 480px; max-width: 100%;"><br><a rel="noopener nofollow" class="external-link is-unresolved" href="https://www.digikey.co.th/en/maker/projects/introduction-to-fpga-part-10-metastability-and-fifo/74884ed134474e008a1e444ea9dacb0f" target="_self">https://www.digikey.co.th/en/maker/projects/introduction-to-fpga-part-10-metastability-and-fifo/74884ed134474e008a1e444ea9dacb0f</a><br>
<a rel="noopener nofollow" class="external-link is-unresolved" href="https://www.macnica.co.jp/business/semiconductor/articles/altera/120821/" target="_self">https://www.macnica.co.jp/business/semiconductor/articles/altera/120821/</a><br>
<a rel="noopener nofollow" class="external-link is-unresolved" href="https://www.macnica.co.jp/business/semiconductor/articles/altera/2130/" target="_self">https://www.macnica.co.jp/business/semiconductor/articles/altera/2130/</a>Metastability (สภาวะกึ่งเสถียร) คือสภาวะที่ output ของ flip-flop อยู่ที่ระดับแรงดันกึ่งกลางระหว่าง 0 กับ 1 หรือสั่นไปมา เป็นเวลาหนึ่งก่อนจะ settle เป็น 0 หรือ 1 อย่างใดอย่างหนึ่ง โดยเกิดเมื่อ input ของ FF เปลี่ยนค่า ในช่วงที่ละเมิดข้อกำหนด setup time / hold time
ข้อกำหนด : D flip-flop ต้องการให้ input นิ่งก่อนขอบ clock อย่างน้อย tSU และนิ่งต่อหลังขอบ clock อย่างน้อย tH ถ้า input เปลี่ยนในช่วงนี้ FF จะ sample ไม่สมบูรณ์ ผลคือ output อาจกลายเป็น metastable<br><img alt="img/ff-symbol-setup-hold.svg" src="coursecontent/img/ff-symbol-setup-hold.svg" target="_self" style="width: 806px; max-width: 100%;"><br><img alt="img/ff-output-normal-vs-metastable.svg" src="coursecontent/img/ff-output-normal-vs-metastable.svg" target="_self" style="width: 783px; max-width: 100%;">ตัวอย่างกรณีไม่ผ่านข้อกำหนด
Setup time violation: D เปลี่ยนก่อนขอบ clock น้อยกว่า tSU
<br><img alt="img/ff-setup-violation.svg" src="coursecontent/img/ff-setup-violation.svg" target="_self" style="width: 700px; max-width: 100%;">
Hold time violation: D เปลี่ยนหลังขอบ clock เร็วกว่า tH
<br><img alt="img/ff-hold-violation.svg" src="coursecontent/img/ff-hold-violation.svg" target="_self" style="width: 697px; max-width: 100%;">]]></description><link>coursecontent/chapter-sp.html</link><guid isPermaLink="false">CourseContent/chapter-sp.md</guid><pubDate>Fri, 16 Oct 2026 01:32:43 GMT</pubDate><enclosure url="coursecontent/img/switch-bounce.png" length="0" type="image/png"/><content:encoded>&lt;figure&gt;&lt;img src="coursecontent/img/switch-bounce.png"&gt;&lt;/figure&gt;</content:encoded></item><item><title><![CDATA[chapter5-basic-simulation]]></title><description><![CDATA[
<a data-tooltip-position="top" aria-label="1 Simulation tool" data-href="#1 Simulation tool" href="coursecontent/chapter5-basic-simulation.html#1_Simulation_tool_0" class="internal-link" target="_self" rel="noopener nofollow">1 Simulation tool</a><br>
- <a data-tooltip-position="top" aria-label="1.1.1 ตารางสรุป FPGA Simulation Tools" data-href="#1.1.1 ตารางสรุป FPGA Simulation Tools" href="coursecontent/chapter5-basic-simulation.html#1.1.1_ตารางสรุป_FPGA_Simulation_Tools_0" class="internal-link" target="_self" rel="noopener nofollow">1.1.1 ตารางสรุป FPGA Simulation Tools</a><br>
- <a data-tooltip-position="top" aria-label="1.1.2 หมวดหมู่เครื่องมือจำลอง (Simulator Categories)" data-href="#1.1.2 หมวดหมู่เครื่องมือจำลอง (Simulator Categories)" href="coursecontent/chapter5-basic-simulation.html#1.1.2_หมวดหมู่เครื่องมือจำลอง_(Simulator_Categories)_0" class="internal-link" target="_self" rel="noopener nofollow">1.1.2 หมวดหมู่เครื่องมือจำลอง (Simulator Categories)</a>
<br><a data-tooltip-position="top" aria-label="2 เปรียบเทียบ Vivado XSim vs ModelSim/Questa" data-href="#2 เปรียบเทียบ Vivado XSim vs ModelSim/Questa" href="coursecontent/chapter5-basic-simulation.html#2_เปรียบเทียบ_Vivado_XSim_vs_ModelSim/Questa_0" class="internal-link" target="_self" rel="noopener nofollow">2 เปรียบเทียบ Vivado XSim vs ModelSim/Questa</a><br>
- <a data-tooltip-position="top" aria-label="2.1.1 ตารางเปรียบเทียบ" data-href="#2.1.1 ตารางเปรียบเทียบ" href="coursecontent/chapter5-basic-simulation.html#2.1.1_ตารางเปรียบเทียบ_0" class="internal-link" target="_self" rel="noopener nofollow">2.1.1 ตารางเปรียบเทียบ</a><br>
- <a data-tooltip-position="top" aria-label="2.1.2 AMD Vivado XSim" data-href="#2.1.2 AMD Vivado XSim" href="coursecontent/chapter5-basic-simulation.html#2.1.2_AMD_Vivado_XSim_0" class="internal-link" target="_self" rel="noopener nofollow">2.1.2 AMD Vivado XSim</a><br>
- <a data-tooltip-position="top" aria-label="2.1.3 ModelSim Altera Edition สำหรับจำลองชิป AMD/Xilinx FPGA" data-href="#2.1.3 ModelSim Altera Edition สำหรับจำลองชิป AMD/Xilinx FPGA" href="coursecontent/chapter5-basic-simulation.html#2.1.3_ModelSim_Altera_Edition_สำหรับจำลองชิป_AMD/Xilinx_FPGA_0" class="internal-link" target="_self" rel="noopener nofollow">2.1.3 ModelSim Altera Edition สำหรับจำลองชิป AMD/Xilinx FPGA</a>
<br><a data-tooltip-position="top" aria-label="3 Basic Simulation with ModelSim" data-href="#3 Basic Simulation with ModelSim" href="coursecontent/chapter5-basic-simulation.html#3_Basic_Simulation_with_ModelSim_0" class="internal-link" target="_self" rel="noopener nofollow">3 Basic Simulation with ModelSim</a><br>
- <a data-tooltip-position="top" aria-label="3.1.1 โครงสร้างพื้นฐานของ ModelSim Workflow" data-href="#3.1.1 โครงสร้างพื้นฐานของ ModelSim Workflow" href="coursecontent/chapter5-basic-simulation.html#3.1.1_โครงสร้างพื้นฐานของ_ModelSim_Workflow_0" class="internal-link" target="_self" rel="noopener nofollow">3.1.1 โครงสร้างพื้นฐานของ ModelSim Workflow</a><br>
- <a data-tooltip-position="top" aria-label="3.1.2 การจำลองผ่าน ModelSim GUI" data-href="#3.1.2 การจำลองผ่าน ModelSim GUI" href="coursecontent/chapter5-basic-simulation.html#3.1.2_การจำลองผ่าน_ModelSim_GUI_0" class="internal-link" target="_self" rel="noopener nofollow">3.1.2 การจำลองผ่าน ModelSim GUI</a><br>
- <a data-tooltip-position="top" aria-label="3.1.3 การจำลองผ่าน Command Line / Transcript และไฟล์ `.do` Script" data-href="#3.1.3 การจำลองผ่าน Command Line / Transcript และไฟล์ `.do` Script" href="coursecontent/chapter5-basic-simulation.html#3.1.3_การจำลองผ่าน_Command_Line_/_Transcript_และไฟล์_`.do`_Script_0" class="internal-link" target="_self" rel="noopener nofollow">3.1.3 การจำลองผ่าน Command Line / Transcript และไฟล์ .do Script</a>
ปัจจุบันมี Simulation Tools ให้เลือกใช้หลายตัว ทั้งแบบเสียเงินและฟรี แต่ละตัวมีจุดเด่นและข้อจำกัดต่างกันไป โดยสรุปรายละเอียดของแต่ละ Tool ไว้ในตารางด้านล่างนี้เครื่องมือที่ผู้พัฒนาต้นทาง (เช่น Siemens EDA) อนุญาตให้ค่าย FPGA (Intel, AMD/Xilinx, Lattice, Microchip) นำไป customize และ bundle แถมมากับ IDE ของตัวเอง โดยผูกสิทธิ์การใช้งานไว้กับซอฟต์แวร์ค่ายนั้น
ตัวอย่าง: Vivado XSim (AMD/Xilinx) — ฟรีเมื่อดาวน์โหลด Vivado ML Standard Edition ไม่ต้องใช้ License file สำหรับชิปรุ่นทั่วไป
ModelSim / Questa Intel FPGA Edition — ฟรีแต่ต้องลงทะเบียนขอ License File จาก Intel (Starter Edition จำกัดจำนวนบรรทัด code หรือความเร็วในการจำลอง)
ModelSim / Active-HDL Lattice Edition, ModelSim ME (Microsemi Edition) ข้อดี: มีการ compile simulation library ของชิปค่ายนั้นๆ มาให้เรียบร้อยแล้ว พร้อมใช้งานทันที
ข้อจำกัด: ถูกล็อกให้จำลองการทำงานได้เฉพาะชิปของ vendor ค่ายนั้นๆ เท่านั้น (เช่น ModelSim Intel FPGA Edition จะนำไปใช้จำลองชิป Xilinx ไม่ได้) และมักจำกัด feature หรือขนาด design ใน Starter Edition
เหมาะกับ: งานเรียน, โปรเจกต์เล็ก-กลางที่ใช้ FPGA ค่ายเดียวตลอด
เครื่องมือ open-source ที่พัฒนาโดยชุมชนหรือผู้พัฒนาอิสระ ไม่สังกัดค่าย FPGA ใด ใช้ข้าม toolchain ได้อย่างอิสระ
ตัวอย่าง: GHDL, NVC (VHDL) / Icarus Verilog, Verilator (Verilog/SystemVerilog)
จุดเด่น: ไม่มีข้อจำกัดเรื่อง license, รันบน CI/CD ได้ง่าย, community พัฒนาต่อเนื่อง, เหมาะกับ workflow ร่วมกับ VUnit/OSVVM/cocotb และ GTKWave สำหรับดู waveform
ข้อควรระวัง: การรองรับมาตรฐานภาษาใหม่ๆ อาจตามหลัง commercial tool และแต่ละตัวมีจุดแข็งต่างกัน (เช่น GHDL เสถียรและนิยมสูงสุด แต่ NVC เร็วกว่าและรองรับ VHDL-2019 ดีกว่า) นอกจากนี้ถ้า design มีการเรียกใช้ primitive ของ vendor (เช่น IP core จาก Vivado IP Catalog) การ setting library ของ vendor ให้ใช้งานกับ GHDL/NVC ได้ ก็อาจมีความยุ่งยาก
เหมาะกับ: งานที่ไม่ต้องการผูกกับ vendor และไม่อยากมีปัญหาเรื่อง license (ทำ automated testing/CI ได้โดยไม่ติดข้อจำกัดเรื่อง license seat แต่ทุกกลุ่ม รวมถึง Commercial ก็ใช้ทำ CI/regression ได้เช่นกัน)
ซอฟต์แวร์ simulator เต็มรูปแบบที่ผลิตและขายโดยผู้พัฒนาโดยตรง (ไม่ผ่าน OEM ค่ายใด) มี feature ครบสำหรับงาน verification ระดับอุตสาหกรรม
ตัวอย่าง: ModelSim PE/SE, Questa Base/Prime/Ultra (Siemens EDA), Active-HDL, Riviera-PRO (Aldec)
จุดเด่น: รองรับชิปและเทคโนโลยีของ FPGA ทุกค่าย (Xilinx, Intel, Lattice, Microchip) รวมถึงงาน ASIC, performance สูง, debug tool ครบ, รองรับ mixed-language และ UVM เต็มรูปแบบ ไม่มีขีดจำกัดด้านขนาดหรือความเร็ว
ข้อจำกัด: ราคาสูงมาก (commercial license) และผู้ใช้ต้อง compile simulation library ของแต่ละค่าย FPGA เองก่อนใช้งาน
เหมาะกับ: งานอุตสาหกรรม, ASIC/FPGA ขนาดใหญ่ที่ต้องการ verification methodology มาตรฐาน
ตารางสรุปเปรียบเทียบในงานจริง XSim ยังไม่ได้มาแทนที่ ModelSim/Questa ทั้งหมด — ตัวเลือกที่เหมาะสมขึ้นอยู่กับ scope และระดับของงานVivado Simulator (XSim) เป็น simulator ที่ฝังมากับ Vivado โดยตรง ไม่ต้องตั้งค่า path หรือ compile library เพิ่มเติมสำหรับชิป Xilinx/AMD จึงเป็นตัวเลือกแรกที่เหมาะกับการเริ่มต้นจำลอง behavioral simulation
เตรียม Testbench — เขียนไฟล์ testbench (VHDL/Verilog) ที่ instantiate DUT (Design Under Test) แล้ว add เข้าโปรเจกต์ผ่าน Add Sources → Add or create simulation sources
กำหนด Top Module ของ Simulation — ในหน้าต่าง Sources คลิกขวาที่ testbench แล้วเลือก Set as Top (เฉพาะใน simulation set เช่น sim_1 ไม่กระทบ synthesis top)
รัน Simulation — คลิก Flow Navigator → SIMULATION → Run Simulation → Run Behavioral Simulation Vivado จะ elaborate design แล้วเปิดหน้าต่าง XSim Waveform ให้อัตโนมัติ ควบคุมการจำลอง — ใช้ปุ่มใน waveform toolbar: Run All — รันจนจบ testbench หรือจน stop/finish
Run For — กำหนดเวลาที่ต้องการรัน (เช่น 1000 ns) แล้วหยุด
Restart — reset การจำลองกลับไปเวลา 0 โดยไม่ต้อง re-elaborate ใหม่ทั้งหมด เพิ่มสัญญาณเข้า Waveform — ลาก signal จากหน้าต่าง Scope/Objects เข้า waveform window หรือคลิกขวา → Add to Wave Window
Note : Vivado รองรับการรัน simulation ได้หลายระดับ ขึ้นอยู่กับว่า design ผ่านขั้นตอนไหนมาแล้ว (RTL → Synthesis → Implementation) โดยหลักๆ ที่ใช้งานบ่อยที่สุดมี 3 แบบ: หมายเหตุ: นอกจาก 3 แบบข้างต้น Vivado ยังมี Run Post-Synthesis Functional Simulation และ Run Post-Implementation Functional Simulation ให้เลือกด้วย ซึ่งจำลองจาก netlist หลัง synthesis/implementation เช่นกัน แต่ไม่ annotate ค่า delay (ไม่ต้องใช้ไฟล์ SDF) เหมาะกับการตรวจสอบว่า logic ยังทำงานถูกต้องหลัง synthesis/implementation โดยไม่สนใจเรื่อง timing
<br><img src="coursecontent/img/xsim1.png" target="_self"><br><img alt="img/xsim2.png" src="coursecontent/img/xsim2.png" target="_self" style="width: 857px; max-width: 100%;"><br><img src="coursecontent/img/xsim3.png" target="_self">ModelSim-Intel FPGA Edition (หรือชื่อเดิม ModelSim Altera Edition) เป็น OEM simulator ที่ผูกมากับ Quartus Prime โดยค่าเริ่มต้นจะมี simulation library ของ Intel/Altera ให้พร้อมใช้งาน(ไม่มี library ของ Xilinx ติดตั้งมาให้) เราสามารถนำมาใช้ simulate กับดีไซน์ที่มาจาก Vivado (ชิป AMD/Xilinx) แต่ว่าจะต้อง compile Xilinx simulation library เพิ่มเองก่อนโดยสรุปมีขั้นตอนดังนี้<br>
(ดูรายละเอียดได้จาก: <a rel="noopener nofollow" class="external-link is-unresolved" href="https://wichayen.github.io/fpgathailand/pynq-z2-modelsim_th.html" target="_self">https://wichayen.github.io/fpgathailand/pynq-z2-modelsim_th.html</a>)
Compile Xilinx Simulation Library ด้วย Modelsim
Export Hardware ของ vivado projct
แก้ไข compile.do ให้ครบสมบูรณ์
ModelSim Altera Edition (do compile.do → do simulate.do)
ข้อควรทราบ: โดยปกติแล้ว AMD/Xilinx ไม่ได้ bundle ModelSim มาให้หัวข้อนี้เป็นพื้นฐานการใช้งาน ModelSim ตั้งแต่สร้าง library, compile source, ไปจนถึงรัน simulation และดู waveform — ใช้ได้ทั้ง ModelSim Altera/Intel Edition และ ModelSim/Questa Standaloneการจำลองด้วย ModelSim มีขั้นตอนหลัก 4 ส่วนเสมอ ไม่ว่าจะรันผ่าน GUI หรือ command line:
สร้าง Working Library — พื้นที่เก็บไฟล์ที่ compile แล้ว (เทียบเท่า xil_defaultlib ใน Vivado)
Compile Source — แปลงไฟล์ VHDL/Verilog เข้า library
Load Design (vsim) — โหลด design unit ที่ต้องการจำลอง (ปกติคือ testbench)
Run &amp; View Waveform — สั่งรันและดูผลลัพธ์ เปิด ModelSim แล้วสร้างโปรเจกต์ใหม่ผ่าน File → New → Project ตั้งชื่อและเลือก working directory
<br><img src="coursecontent/img/modelsim-1.png" target="_self"><br><img src="coursecontent/img/modelsim-2.png" target="_self">
Add Existing File — เพิ่มไฟล์ source (.vhd/.v) และไฟล์ testbench เข้าโปรเจกต์
<br><img src="coursecontent/img/modelsim-3.png" target="_self">
ไปที่เมนู Compile → Compile All
(ถ้า compile ผ่าน จะเห็นเครื่องหมายถูกสีเขียวหน้าไฟล์ใน Project window)
<br><img src="coursecontent/img/modelsim-4.png" target="_self">
ไปที่เมนู Simulate → Start Simulation จากนั้นเลือกไปที่ work.tb_glitch_demo (testbench ของเรา) แล้วกด OK
<br><img src="coursecontent/img/modelsim-5.png" target="_self"><br><img src="coursecontent/img/modelsim-6.png" target="_self">
เพิ่มสัญญาณเข้า waveform: ลาก signal จากหน้าต่าง Objects เข้า Wave window หรือคลิกขวา → Add to → Wave → Selected Signals
สั่งรันการจำลองที่ Transcript หรือปุ่ม toolbar: run 1us — รันตามเวลาที่กำหนด <br><img alt="img/modelsim-8.png" src="coursecontent/img/modelsim-8.png" target="_self" style="width: 994px; max-width: 100%;">ModelSim มี Tcl (Tool Command Language) เป็นภาษา script หลักสำหรับควบคุมการทำงานทั้งหมด ไม่ว่าจะเป็นการสร้าง library, compile, โหลด design, หรือสั่งรัน simulation เป็นภาษา scripting ทั่วไปที่ EDA tool หลายเจ้านิยมใช้ (รวมถึง Vivado ด้วย) เพราะเขียนสั้น กระชับ และเรียกใช้คำสั่งของ tool ได้โดยตรง วิธีนี้เหมาะสำหรับการเขียน script อัตโนมัติหรือรันแบบ batch โดยไม่ต้องเปิด GUI
คำสั่ง Tcl ของ ModelSim ถ้าต้องการดูคำสั่งทั้งหมดแบบละเอียด แนะนำให้ไปดูใน Reference Manual ของ ModelSim/Questa โดยตรง (เปิดได้จากเมนู Help → Tcl Man Pages ในโปรแกรม หรือค้นจาก command เฉพาะที่ต้องการในเอกสาร)ด้านล่างเป็นตัวอย่างในการสร้าง simulation project:# ==============================================================================
# ModelSim / QuestaSim Simulation Script
# Compares Ideal (Zero-Delay) vs Real Delayed (Glitch) behavior simultaneously.
# How to run:
# In ModelSim transcript console: do sim.do
# ============================================================================== # 1. Quit any running simulation
quit -sim # 2. Clean and create work library
if [file exists work] { vdel -lib work -all
}
vlib work
vmap work work # 3. Compile VHDL source files
vcom -2008 -work work glitch_demo.vhd
vcom -2008 -work work tb_glitch_demo.vhd # 4. Start simulation with 1ps timing precision
vsim -t 1ps -novopt work.tb_glitch_demo # 5. Configure Waveform Viewer
delete wave * add wave -divider "CLOCK &amp; RESET"
add wave -color "Yellow" -itemcolor "Yellow" /tb_glitch_demo/clk
add wave -color "White" -itemcolor "White" /tb_glitch_demo/rst_n add wave -divider "INPUT STIMULUS"
add wave -color "Cyan" -itemcolor "Cyan" /tb_glitch_demo/d_in add wave -divider "INTERNAL DELAYED SIGNALS (PHYSICAL MODEL)"
add wave -color "Gray" -itemcolor "Gray" /tb_glitch_demo/dut_delay/inv1
add wave -color "Gray" -itemcolor "Gray" /tb_glitch_demo/dut_delay/inv2
add wave -color "Orange" -itemcolor "Orange" /tb_glitch_demo/dut_delay/d_not add wave -divider "IDEAL RTL OUTPUTS (ZERO DELAY)"
add wave -color "Cyan" -itemcolor "Cyan" /tb_glitch_demo/y_glitch
add wave -color "Green" -itemcolor "Green" /tb_glitch_demo/y_clean add wave -divider "REAL FPGA OUTPUTS (WITH DELAY &amp; GLITCH)"
add wave -color "Red" -itemcolor "Red" /tb_glitch_demo/y_glitch_delay
add wave -color "Green" -itemcolor "Green" /tb_glitch_demo/y_clean_delay # 6. Run simulation
run 350 ns # 7. Fit waveform into window
wave zoomfull
คำอธิบาย
quit -sim — ปิด simulation เดิมที่ค้างอยู่ก่อน (ถ้ามี) กันไม่ให้ error ตอนสั่ง vsim ใหม่ทับของเดิม
if [file exists work] { vdel -lib work -all } — เช็คว่ามี library work เก่าอยู่ไหม ถ้ามีให้ลบทิ้งก่อน เพื่อ compile ใหม่แบบ clean ทุกครั้ง
vlib / vmap — สร้าง library ใหม่ชื่อ work แล้ว map ให้ใช้งานได้ (ต้องทำก่อนเสมอถ้ายังไม่มี work library)
vcom -2008 -work work — compile ไฟล์ VHDL เข้า library work โดยบังคับใช้มาตรฐาน VHDL-2008
vsim -t 1ps -novopt — โหลดและ elaborate design พร้อมเปิด simulation kernel โดย
-t 1ps กำหนดความละเอียดเวลา (time resolution) ของ simulator เป็น 1 ps ส่วน
-novopt คือปิดการทำ optimization ตอน elaborate เพื่อไม่ให้สัญญาณภายใน (เช่น /dut_delay/inv1, /dut_delay/inv2) ถูกตัดทิ้งไปจนดูไม่ได้
delete wave * — เคลียร์ waveform window ให้ว่างก่อน เผื่อมี signal เก่าค้างจากการรันครั้งก่อน
add wave -divider "ชื่อกลุ่ม" — ใส่เส้นแบ่งกลุ่มสัญญาณใน waveform ให้อ่านง่ายขึ้น (เช่น แยกกลุ่ม Clock/Reset, Input, Internal Signal, Output)
add wave -color "..." -itemcolor "..." &lt;path/to/signal&gt; — เพิ่มสัญญาณที่ต้องการดูแบบเจาะจงทีละเส้น พร้อมกำหนดสีให้ต่างกันเพื่อแยกแยะง่าย
run 350 ns — รันจำลองเป็นเวลา 350 นาโนวินาที
wave zoomfull — ปรับ waveform window ให้ fit พอดีกับช่วงเวลาที่รันไปทั้งหมดโดยอัตโนมัติ ไม่ต้อง zoom เอง
วิธีรัน Scriptคำสั่งทั้งหมดข้างต้นเก็บไว้ในไฟล์ sim.do :
เปิด ModelSim ขึ้นมา (ยังไม่ต้องเปิดโปรเจกต์ก็ได้)
ที่ Transcript window ให้ cd ไปยัง path ที่เก็บไฟล์ sim.do และไฟล์ source ทั้งหมดไว้ก่อน เช่น:
ModelSim&gt; cd D:/github/FPGACourse/workshop/ws_glitch สั่งรัน script ด้วยคำสั่ง do ตามด้วยชื่อไฟล์:
ModelSim&gt; do sim.do ModelSim จะรันคำสั่งทั้งหมดในไฟล์ตามลำดับให้อัตโนมัติ
<br><img alt="img/modelsim-9.png" src="coursecontent/img/modelsim-9.png" target="_self" style="width: 818px; max-width: 100%;"><br><img alt="img/modelsim-10.png" src="coursecontent/img/modelsim-10.png" target="_self" style="width: 920px; max-width: 100%;">Batch Mode (ไม่เปิด GUI)
นอกจากรันผ่าน Transcript window ใน GUI แล้ว เรายังสามารถรันแบบ batch mode คือไม่เปิด GUI เลยได้ (เหมาะกับ CI/CD หรือ regression test) โดยเปิด Command Prompt หรือ PowerShell ขึ้นมา แล้ว cd ไปยัง path ที่เก็บไฟล์ sim.do และไฟล์ source ทั้งหมด (เช่นเดียวกับตอนรันใน Transcript) จากนั้นสั่ง:
vsim -c -do "sim.do; quit -f" -c — รันแบบ console-only ไม่เปิด GUI
quit -f — ปิดโปรแกรมทันทีหลัง simulation เสร็จ (จำเป็นสำหรับ batch mode เพื่อไม่ให้ค้างรอ) ]]></description><link>coursecontent/chapter5-basic-simulation.html</link><guid isPermaLink="false">CourseContent/chapter5-basic-simulation.md</guid><pubDate>Mon, 05 Oct 2026 04:31:48 GMT</pubDate><enclosure url="coursecontent/img/xsim1.png" length="0" type="image/png"/><content:encoded>&lt;figure&gt;&lt;img src="coursecontent/img/xsim1.png"&gt;&lt;/figure&gt;</content:encoded></item><item><title><![CDATA[chapter3-basic-vhdl]]></title><description><![CDATA[
<a data-tooltip-position="top" aria-label="1 โครงสร้างภาษา VHDL: Entity &amp; Architecture" data-href="#1 โครงสร้างภาษา VHDL: Entity &amp; Architecture" href="coursecontent/chapter3-basic-vhdl.html#1_โครงสร้างภาษา_VHDL_Entity_&amp;_Architecture_0" class="internal-link" target="_self" rel="noopener nofollow">1 โครงสร้างภาษา VHDL: Entity &amp; Architecture</a>
<br><a data-tooltip-position="top" aria-label="2 Combinational Logic vs Sequential Logic" data-href="#2 Combinational Logic vs Sequential Logic" href="coursecontent/chapter3-basic-vhdl.html#2_Combinational_Logic_vs_Sequential_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">2 Combinational Logic vs Sequential Logic</a> <br><a data-tooltip-position="top" aria-label="2.1 ตัวอย่างโค้ด VHDL: Combinational Process vs Clocked Sequential Process" data-href="#2.1 ตัวอย่างโค้ด VHDL: Combinational Process vs Clocked Sequential Process" href="coursecontent/chapter3-basic-vhdl.html#2.1_ตัวอย่างโค้ด_VHDL_Combinational_Process_vs_Clocked_Sequential_Process_0" class="internal-link" target="_self" rel="noopener nofollow">2.1 ตัวอย่างโค้ด VHDL: Combinational Process vs Clocked Sequential Process</a>
<br><a data-tooltip-position="top" aria-label="2.2 เจาะลึก Sensitivity List: การทำงานและข้อควรระวัง" data-href="#2.2 เจาะลึก Sensitivity List: การทำงานและข้อควรระวัง" href="coursecontent/chapter3-basic-vhdl.html#2.2_เจาะลึก_Sensitivity_List_การทำงานและข้อควรระวัง_0" class="internal-link" target="_self" rel="noopener nofollow">2.2 เจาะลึก Sensitivity List: การทำงานและข้อควรระวัง</a> <br><a data-tooltip-position="top" aria-label="2.2.1 Sensitivity List ใน Combinational Process" data-href="#2.2.1 Sensitivity List ใน Combinational Process" href="coursecontent/chapter3-basic-vhdl.html#2.2.1_Sensitivity_List_ใน_Combinational_Process_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.1 Sensitivity List ใน Combinational Process</a>
<br><a data-tooltip-position="top" aria-label="2.2.2 Sensitivity List ใน Clocked Sequential Process" data-href="#2.2.2 Sensitivity List ใน Clocked Sequential Process" href="coursecontent/chapter3-basic-vhdl.html#2.2.2_Sensitivity_List_ใน_Clocked_Sequential_Process_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.2 Sensitivity List ใน Clocked Sequential Process</a>
<br><a data-tooltip-position="top" aria-label="2.2.3 VHDL-2008 Standard Option: `process(all)`" data-href="#2.2.3 VHDL-2008 Standard Option: `process(all)`" href="coursecontent/chapter3-basic-vhdl.html#2.2.3_VHDL-2008_Standard_Option_`process(all)`_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.3 VHDL-2008 Standard Option: process(all)</a> <br><a data-tooltip-position="top" aria-label="3 ชนิดข้อมูลและแอตทริบิวต์ (Data Types &amp; Attributes)" data-href="#3 ชนิดข้อมูลและแอตทริบิวต์ (Data Types &amp; Attributes)" href="coursecontent/chapter3-basic-vhdl.html#3_ชนิดข้อมูลและแอตทริบิวต์_(Data_Types_&amp;_Attributes)_0" class="internal-link" target="_self" rel="noopener nofollow">3 ชนิดข้อมูลและแอตทริบิวต์ (Data Types &amp; Attributes)</a> <br><a data-tooltip-position="top" aria-label="3.1 ชนิดข้อมูลหลักใน VHDL (`std_logic_1164`):" data-href="#3.1 ชนิดข้อมูลหลักใน VHDL (`std_logic_1164`):" href="coursecontent/chapter3-basic-vhdl.html#3.1_ชนิดข้อมูลหลักใน_VHDL_(`std_logic_1164`)_0" class="internal-link" target="_self" rel="noopener nofollow">3.1 ชนิดข้อมูลหลักใน VHDL (std_logic_1164):</a>
<br><a data-tooltip-position="top" aria-label="3.2 แอตทริบิวต์ที่ใช้บ่อย (VHDL Attributes):" data-href="#3.2 แอตทริบิวต์ที่ใช้บ่อย (VHDL Attributes):" href="coursecontent/chapter3-basic-vhdl.html#3.2_แอตทริบิวต์ที่ใช้บ่อย_(VHDL_Attributes)_0" class="internal-link" target="_self" rel="noopener nofollow">3.2 แอตทริบิวต์ที่ใช้บ่อย (VHDL Attributes):</a> <br><a data-tooltip-position="top" aria-label="4 โครงสร้างควบคุมการทำงาน (Control Flow &amp; Hardware Generation)" data-href="#4 โครงสร้างควบคุมการทำงาน (Control Flow &amp; Hardware Generation)" href="coursecontent/chapter3-basic-vhdl.html#4_โครงสร้างควบคุมการทำงาน_(Control_Flow_&amp;_Hardware_Generation)_0" class="internal-link" target="_self" rel="noopener nofollow">4 โครงสร้างควบคุมการทำงาน (Control Flow &amp; Hardware Generation)</a> <br><a data-tooltip-position="top" aria-label="4.1 คำสั่งควบคุมลำดับภายใน Process (Sequential Control Statements)" data-href="#4.1 คำสั่งควบคุมลำดับภายใน Process (Sequential Control Statements)" href="coursecontent/chapter3-basic-vhdl.html#4.1_คำสั่งควบคุมลำดับภายใน_Process_(Sequential_Control_Statements)_0" class="internal-link" target="_self" rel="noopener nofollow">4.1 คำสั่งควบคุมลำดับภายใน Process (Sequential Control Statements)</a>
<br><a data-tooltip-position="top" aria-label="4.2 คำสั่งสร้างฮาร์ดแวร์ขนานภายนอก Process (Concurrent Generate Statement)" data-href="#4.2 คำสั่งสร้างฮาร์ดแวร์ขนานภายนอก Process (Concurrent Generate Statement)" href="coursecontent/chapter3-basic-vhdl.html#4.2_คำสั่งสร้างฮาร์ดแวร์ขนานภายนอก_Process_(Concurrent_Generate_Statement)_0" class="internal-link" target="_self" rel="noopener nofollow">4.2 คำสั่งสร้างฮาร์ดแวร์ขนานภายนอก Process (Concurrent Generate Statement)</a> <br><a data-tooltip-position="top" aria-label="5 การออกแบบเชิงโมดูลขั้นสูง (Package, Functions &amp; Procedures)" data-href="#5 การออกแบบเชิงโมดูลขั้นสูง (Package, Functions &amp; Procedures)" href="coursecontent/chapter3-basic-vhdl.html#5_การออกแบบเชิงโมดูลขั้นสูง_(Package,_Functions_&amp;_Procedures)_0" class="internal-link" target="_self" rel="noopener nofollow">5 การออกแบบเชิงโมดูลขั้นสูง (Package, Functions &amp; Procedures)</a> <br><a data-tooltip-position="top" aria-label="5.1 Package (`*_pkg.vhd`)" data-href="#5.1 Package (`*_pkg.vhd`)" href="coursecontent/chapter3-basic-vhdl.html#5.1_Package_(`*_pkg.vhd`)_0" class="internal-link" target="_self" rel="noopener nofollow">5.1 Package (_pkg.vhd)</a>
<br><a data-tooltip-position="top" aria-label="5.2 Functions (`function`)" data-href="#5.2 Functions (`function`)" href="coursecontent/chapter3-basic-vhdl.html#5.2_Functions_(`function`)_0" class="internal-link" target="_self" rel="noopener nofollow">5.2 Functions (function)</a>
<br><a data-tooltip-position="top" aria-label="5.3 Procedures (`procedure`)" data-href="#5.3 Procedures (`procedure`)" href="coursecontent/chapter3-basic-vhdl.html#5.3_Procedures_(`procedure`)_0" class="internal-link" target="_self" rel="noopener nofollow">5.3 Procedures (procedure)</a> <br><a data-tooltip-position="top" aria-label="6 Signal vs Variable" data-href="#6 Signal vs Variable" href="coursecontent/chapter3-basic-vhdl.html#6_Signal_vs_Variable_0" class="internal-link" target="_self" rel="noopener nofollow">6 Signal vs Variable</a>
<br><a data-tooltip-position="top" aria-label="7 Signal Naming example" data-href="#7 Signal Naming example" href="coursecontent/chapter3-basic-vhdl.html#7_Signal_Naming_example_0" class="internal-link" target="_self" rel="noopener nofollow">7 Signal Naming example</a> <br><a data-tooltip-position="top" aria-label="7.1 ตัวอย่าง Convention หลัก" data-href="#7.1 ตัวอย่าง Convention หลัก" href="coursecontent/chapter3-basic-vhdl.html#7.1_ตัวอย่าง_Convention_หลัก_0" class="internal-link" target="_self" rel="noopener nofollow">7.1 ตัวอย่าง Convention หลัก</a>
<br><a data-tooltip-position="top" aria-label="7.2 Convention อื่นๆ ที่พบได้บ่อยในอุตสาหกรรม (สำหรับอ้างอิง)" data-href="#7.2 Convention อื่นๆ ที่พบได้บ่อยในอุตสาหกรรม (สำหรับอ้างอิง)" href="coursecontent/chapter3-basic-vhdl.html#7.2_Convention_อื่นๆ_ที่พบได้บ่อยในอุตสาหกรรม_(สำหรับอ้างอิง)_0" class="internal-link" target="_self" rel="noopener nofollow">7.2 Convention อื่นๆ ที่พบได้บ่อยในอุตสาหกรรม (สำหรับอ้างอิง)</a> <br><a data-tooltip-position="top" aria-label="8 Synthesizable vs Non-synthesizable (Testbench-only) VHDL Syntax" data-href="#8 Synthesizable vs Non-synthesizable (Testbench-only) VHDL Syntax" href="coursecontent/chapter3-basic-vhdl.html#8_Synthesizable_vs_Non-synthesizable_(Testbench-only)_VHDL_Syntax_0" class="internal-link" target="_self" rel="noopener nofollow">8 Synthesizable vs Non-synthesizable (Testbench-only) VHDL Syntax</a> <br><a data-tooltip-position="top" aria-label="8.1 ตารางเปรียบเทียบ Syntax ที่พบบ่อย" data-href="#8.1 ตารางเปรียบเทียบ Syntax ที่พบบ่อย" href="coursecontent/chapter3-basic-vhdl.html#8.1_ตารางเปรียบเทียบ_Syntax_ที่พบบ่อย_0" class="internal-link" target="_self" rel="noopener nofollow">8.1 ตารางเปรียบเทียบ Syntax ที่พบบ่อย</a>
<br><a data-tooltip-position="top" aria-label="8.2 ตัวอย่างโค้ด: รูปแบบ Non-synthesizable (Testbench-only) ที่พบบ่อย" data-href="#8.2 ตัวอย่างโค้ด: รูปแบบ Non-synthesizable (Testbench-only) ที่พบบ่อย" href="coursecontent/chapter3-basic-vhdl.html#8.2_ตัวอย่างโค้ด_รูปแบบ_Non-synthesizable_(Testbench-only)_ที่พบบ่อย_0" class="internal-link" target="_self" rel="noopener nofollow">8.2 ตัวอย่างโค้ด: รูปแบบ Non-synthesizable (Testbench-only) ที่พบบ่อย</a>
<br><a data-tooltip-position="top" aria-label="8.3 วิธีเช็คว่าโค้ดของเรา Synthesizable หรือ Non-synthesizable" data-href="#8.3 วิธีเช็คว่าโค้ดของเรา Synthesizable หรือ Non-synthesizable" href="coursecontent/chapter3-basic-vhdl.html#8.3_วิธีเช็คว่าโค้ดของเรา_Synthesizable_หรือ_Non-synthesizable_0" class="internal-link" target="_self" rel="noopener nofollow">8.3 วิธีเช็คว่าโค้ดของเรา Synthesizable หรือ Non-synthesizable</a> เนื้อหานี้มุ่งเน้นการทำความเข้าใจสถาปัตยกรรมภาษา VHDL (Hardware Description Language) ตั้งแต่พื้นฐานโครงสร้าง พฤติกรรมฮาร์ดแวร์ กฎการตั้งชื่อสัญญาณมาตรฐานอุตสาหกรรม จนถึงการออกแบบโมดูลขั้นสูงโครงสร้างหลักของไฟล์ VHDL แบ่งออกเป็น 3 ส่วนหลักตามลำดับดังแสดงในรูป:<br><img alt="img/vhdl_structure.png" src="coursecontent/img/vhdl_structure.png" target="_self" style="width: 553px; max-width: 100%;"> Library Declaration (ส่วนประกาศไลบรารี): ทำหน้าที่ระบุไลบรารีและแพ็กเกจที่จะดึงมาใช้งาน เช่น library IEEE; use IEEE.STD_LOGIC_1164.ALL; เพื่อใช้งานชนิดข้อมูลมาตรฐานฮาร์ดแวร์ Entity Declaration (ส่วนประกาศเอนทิตี): เปรียบเสมือน "กรอบกล่องฮาร์ดแวร์ภายนอก" ที่กำหนดชื่อโมดูล พารามิเตอร์ปรับขนาดวงจร (Generic) และสัญญาณขาเข้า/ขาออก (Port) Architecture Definition (ส่วนนิยามสถาปัตยกรรม): เปรียบเสมือน "วงจรภายในกล่อง" ที่บรรยายการทำงานของฮาร์ดแวร์ โดยแบ่งโครงสร้างภายในออกเป็น 2 โซนสำคัญ: Declarative Region (โซนประกาศก่อนคำสั่ง begin): Component Declaration: ประกาศโมดูลย่อย (Sub-modules) ที่ต้องการดึงมาต่อร่วมในวงจร
Signal Declaration: ประกาศสายสัญญาณภายใน (Internal Nodes) และสลักข้อมูล/เรจิสเตอร์ (Registers) Statement Region (โซนบรรยายวงจรหลังคำสั่ง begin): เขียนบรรยายพฤติกรรมวงจร (Circuit Description) ด้วยคำสั่งขนาน (Concurrent Statements), การต่อสายโมดูลย่อย (Component Instantiation), หรือบล็อกการทำงานตามลำดับ (process) library IEEE;
use IEEE.STD_LOGIC_1164.ALL; -- 1. Library &amp; Entity Declaration (กรอบกล่องภายนอก)
entity my_module is Generic ( G_DATA_WIDTH : integer := 8 -- Generic Parameter ); Port ( clk : in std_logic; rst_n : in std_logic; i_data: in std_logic_vector(G_DATA_WIDTH-1 downto 0); o_data: out std_logic_vector(G_DATA_WIDTH-1 downto 0) );
end entity my_module; -- 2. Architecture Definition (วงจรภายในกล่อง)
architecture Behavioral of my_module is -- [Declarative Region] ประกาศ Signal / Component ภายใน signal s_data_reg : std_logic_vector(G_DATA_WIDTH-1 downto 0);
begin -- [Statement Region] บรรยายวงจรฮาร์ดแวร์หลังคำสั่ง begin process(clk, rst_n) begin if rst_n = '0' then s_data_reg &lt;= (others =&gt; '0'); elsif rising_edge(clk) then s_data_reg &lt;= i_data; end if; end process; o_data &lt;= s_data_reg;
end architecture Behavioral;
ข้อควรระวัง: การกำหนดค่าเริ่มต้นที่ Signal Declaration
การเขียน signal s_data_reg : std_logic_vector(...) := (others =&gt; '0'); เป็นการกำหนดค่า Initial Value สำหรับ Simulation เท่านั้น (ค่า ณ เวลา 0 ns) ไม่ใช่วงจร Reset จริง — บน FPGA จริง ค่านี้จะถูกโหลดผ่านกลไก Configuration/Bitstream ของแต่ละเวนเดอร์เท่านั้น (ใช้ได้กับบาง Flow) และไม่สามารถพึ่งพาแทนวงจร Reset (เช่น rst_n) ได้ หากต้องการ Reset ที่ทำงานได้จริงหลัง Power-up หรือระหว่างการทำงาน ต้องออกแบบผ่าน process(clk, rst_n) ตามตัวอย่างข้างต้นเสมอ
ในภาษา VHDL วงจรฮาร์ดแวร์และการประมวลผลคำสั่งแบ่งออกเป็น 2 ประเภทหลัก: Combinational Logic (วงจรรวมเชิงผสม — นอก Process หรือใน Combinational Process): โค้ดที่อยู่นอก process (เช่น สัญญาณกำหนดค่าธรรมดา, when-else, with-select) รวมถึง Combinational Process จะถูกประมวลผล ขนานกันตลอดเวลา (Parallel Execution) ไม่ว่าเราจะสลับบรรทัดขึ้นลงอย่างไร วงจรฮาร์ดแวร์ที่สังเคราะห์ได้จะมีผลลัพธ์เหมือนกันทุกประการ Sequential Logic (วงจรเชิงลำดับ — ใน Clocked Process): โค้ดที่อยู่ภายในบล็อก process(...) ที่ทำงานตามขอบสัญญาณนาฬิกา จะถูกประมวลผล เรียงตามลำดับบรรทัดจากบนลงล่าง (Sequential Execution) เมื่อสัญญาณใน Sensitivity List มีการเปลี่ยนแปลงค่า 💡 ข้อสังเกต Combinational Logic: ถึงแม้โค้ดใน process จะอ่านและประมวลผลเรียงบรรทัด แต่เมื่อนำไปสังเคราะห์เป็นวงจร Combinational Logic (ที่ใส่ Sensitivity List ครบถ้วน) ผลลัพธ์ฮาร์ดแวร์ที่ได้ในชิปจริงก็คือ การทำงานขนานกันตลอดเวลา (Parallel Hardware) ไม่ต่างจากการเขียน Combinational Logic นอก Process
<br><img alt="img/logic_type.png" src="coursecontent/img/logic_type.png" target="_self" style="width: 966px; max-width: 100%;">ในทางปฏิบัติ การใช้งาน Process แบ่งออกเป็น 2 ประเภทหลัก:
Combinational Process (ไม่มี Clock): ทำงานเมื่ออินพุตเปลี่ยนค่า (ต้องใส่สัญญาณอินพุตทุกตัวใน Sensitivity List หรือใช้ process(all) เพื่อให้ได้วงจร Combinational Logic ขนานที่สมบูรณ์และป้องกัน Latch)
Clocked Sequential Process (มี Clock — ⭐ วิธีมาตรฐานที่แนะนำสำหรับ FPGA): ทำงานเฉพาะที่ขอบสัญญาณนาฬิกา (rising_edge(clk)) สังเคราะห์ได้ Flip-Flops (Sequential Logic) ที่มีไทม์มิ่งเสถียร ป้องกันการเกิด Glitch และ Latch โดยสมบูรณ์
library IEEE;
use IEEE.STD_LOGIC_1164.ALL; entity process_types_demo is Port ( clk : in std_logic; rst_n : in std_logic; i_a : in std_logic; i_b : in std_logic; o_comb_y : out std_logic; -- ผลลัพธ์จาก Combinational Process o_clocked_q: out std_logic -- ผลลัพธ์จาก Clocked Sequential Process (แนะนำ) );
end entity process_types_demo; architecture Behavioral of process_types_demo is signal s_comb_node : std_logic;
begin ------------------------------------------------------------------ -- 1. Combinational Process (ประมวลผลทันทีที่อินพุตเปลี่ยน) -- ⚠️ ต้องใส่ "ทุกสัญญาณที่ถูกอ่านค่าใน Process" เข้า Sensitivity List ให้ครบ ------------------------------------------------------------------ process(i_a, i_b, s_comb_node) begin s_comb_node &lt;= i_a and i_b; o_comb_y &lt;= s_comb_node or i_a; end process; ------------------------------------------------------------------ -- 2. Clocked Sequential Process (⭐ แนะนำที่สุดสำหรับการออกแบบ FPGA) -- ทำงานเรียงลำดับเฉพาะจังหวะขอบขาขึ้นของ clk (และ Async Reset) ------------------------------------------------------------------ process(clk, rst_n) begin if rst_n = '0' then o_clocked_q &lt;= '0'; elsif rising_edge(clk) then -- สังเคราะห์กลายเป็น D Flip-Flop บันทึกค่าตามจังหวะ Clock o_clocked_q &lt;= i_a and i_b; end if; end process; end architecture Behavioral;
ข้อสังเกตฮาร์ดแวร์: Combinational Logic: เหมาะสำหรับเขียนสมการ Combinational สั้นๆ (เช่น o_y &lt;= i_a and i_b;, when-else, with-select)
Sequential Logic: จำเป็นต้องใช้เมื่อสร้าง Sequential Logic (Flip-Flops / Clock Edge) หรือต้องการใช้โครงสร้างเงื่อนไขที่ซับซ้อน เช่น if-then-else และ case-when Sensitivity List คือรายการสัญญาณที่ระบุไว้ในวงเล็บหลังคำสั่ง process(...) เพื่อบอก Simulator และ Synthesis Tool ว่า "ให้บล็อก Process นี้ประมวลผลใหม่ทุกครั้งที่มีสัญญาณตัวใดตัวหนึ่งในรายการเปลี่ยนค่า (Signal Event)"
กฎเหล็ก: ต้องใส่ สัญญาณอินพุตทุกตัว ที่ถูกอ่านค่าภายใน Process เข้าไปใน Sensitivity List ให้ครบถ้วน
หากใส่ไม่ครบ: จะเกิดปัญหา Simulation vs Synthesis Mismatch (ผลการจำลองใน Simulation ไม่ตรงกับฮาร์ดแวร์จริง) และ Synthesis Tool จะสร้าง Unintentional Latch ขึ้นมาเก็บค่าสัญญาณที่ไม่ได้ถูกอัปเดต กฎเหล็ก: ใส่เฉพาะสัญญาณ Clock (และ Asynchronous Reset ถ้ามี) เท่านั้น!
เหตุผล: วงจร Register / Flip-Flop จะตอบสนองและเปลี่ยนสถานะเฉพาะที่ขอบสัญญาณนาฬิกา (rising_edge(clk)) สัญญาณ Data Input อื่นๆ ไม่จำเป็นต้องใส่ใน Sensitivity List เพราะค่าจะถูกจับบันทึก (Sample) ตามจังหวะ Clock เท่านั้น
ในมาตรฐาน VHDL-2008 เพิ่มฟีเจอร์ process(all) สำหรับ Combinational Logic ช่วยให้ Compiler รวบรวมสัญญาณอินพุตทั้งหมดที่อ่านใน Process เข้า Sensitivity List ให้อัตโนมัติ ช่วยลดข้อผิดพลาดในการลืมใส่สัญญาณให้ครบถ้วน std_logic: สัญญาณ 1 บิต มี 9 สถานะ (สถานะหลักคือ '0', '1', 'Z' (High Impedance), 'X' (Unknown))<br>
<img alt="img/std_logic.png" src="coursecontent/img/std_logic.png" target="_self" style="width: 672px; max-width: 100%;"> std_logic_vector: อาเรย์ชุดสัญญาณบิต เช่น std_logic_vector(7 downto 0) array: การจำลองหน่วยความจำ เช่น อาเรย์ของ vector:
type ram_type is array (0 to 255) of std_logic_vector(7 downto 0);
signal s_ram : ram_type; record: กลุ่มข้อมูลรวมหลายประเภทไว้ในชื่อเดียว (คล้าย struct ในภาษา C):
type bus_header_t is record addr : std_logic_vector(15 downto 0); data : std_logic_vector(31 downto 0); valid: std_logic;
end record; 'high / 'low: คืนค่าดัชนีสูงสุด / ต่ำสุดของเวกเตอร์
'range / 'reverse_range: คืนค่าช่วงดัชนี เช่น (7 downto 0)
'length: คืนค่าจำนวนบิตทั้งหมดของเวกเตอร์ if-then-else:
ใช้สร้างเงื่อนไขตามลำดับความสำคัญ (Priority Logic) ซึ่งหากไม่ระบุเงื่อนไข else ครบถ้วนในวงจร Combinational Logic จะทำให้เกิด Unintentional Latch ได้
process(i_enable, i_data)
begin if i_enable = '1' then o_out &lt;= i_data; else o_out &lt;= (others =&gt; '0'); end if;
end process; case-when:
ใช้สร้าง Multiplexer หรือ FSM State Transition โดยบังคับให้ระบุเงื่อนไขให้ครอบคลุมทุกค่าอินพุตที่เป็นไปได้ด้วย when others
process(s_state)
begin case s_state is when "00" =&gt; o_led &lt;= "0001"; when "01" =&gt; o_led &lt;= "0010"; when "10" =&gt; o_led &lt;= "0100"; when others =&gt; o_led &lt;= "1000"; -- ป้องกัน Latch end case;
end process; for-loop:
ใช้คลี่สร้างโครงสร้างลอจิกขนานตามจำนวนรอบแบบตายตัว (Unrolling Loop) ในการสังเคราะห์ฮาร์ดแวร์ เช่น การสลับกลับบิตข้อมูล (Bit Reversal):
process(i_data)
begin for i in 0 to 7 loop o_reversed(i) &lt;= i_data(7 - i); end loop;
end process; for-generate: ถอดแบบโมดูลหรือวงจรขนานตามจำนวนที่กำหนด เช่น สร้าง Buffer 4 ช่อง:
g_buf_array: for i in 0 to 3 generate u_buf : entity work.buffer_cell port map ( i_in =&gt; i_data(i), o_out =&gt; o_data(i) );
end generate g_buf_array; if-generate: เลือกสร้างโครงสร้างฮาร์ดแวร์ตามเงื่อนไข Generic (Conditional Instantiation) เช่น เลือกสร้างวงจร Pipe-lined Register หรือต่อตรงตามค่า Generic G_INCLUDE_REG:
-- สร้างวงจรเก็บข้อมูลเมื่อ Generic G_INCLUDE_REG = true
g_reg_gen: if G_INCLUDE_REG generate u_reg : entity work.data_register port map ( clk =&gt; clk, i_d =&gt; i_data, o_q =&gt; o_data );
end generate g_reg_gen; -- สร้างวงจรต่อสายตรงเมื่อ Generic G_INCLUDE_REG = false
g_pass_gen: if not G_INCLUDE_REG generate o_data &lt;= i_data;
end generate g_pass_gen; ใช้เก็บประกาศประเภทข้อมูลชนิดพิเศษ (type), ค่าคงที่ (constant), และฟังก์ชันส่วนกลาง เพื่อให้ทุกโมดูลในโปรเจกต์อ้างอิงและดึงไปใช้งานร่วมกันได้:library IEEE;
use IEEE.STD_LOGIC_1164.ALL; package sys_config_pkg is constant C_CLK_FREQ : integer := 100_000_000; -- 100 MHz type T_BUS_RECORD is record addr : std_logic_vector(15 downto 0); data : std_logic_vector(31 downto 0); valid : std_logic; end record;
end package sys_config_pkg;
บล็อกการคำนวณแบบ Pure Combinational Logic ที่คืนค่ากลับมา 1 ค่าเสมอ (ไม่มี Propagation Delay ในการจำลอง และไม่สร้าง Clock Edge) นิยมใช้ทำ Math Calculation หรือหาขนาด Bit Width:function calc_parity (val : std_logic_vector) return std_logic is variable v_parity : std_logic := '0';
begin for i in val'range loop v_parity := v_parity xor val(i); end loop; return v_parity;
end function calc_parity;
บล็อกโค้ดย่อยที่สามารถรับและส่งค่าผ่านพอร์ต in, out, inout ได้หลายค่าพร้อมกัน นิยมนำมาใช้สร้าง Helper Subprograms สำหรับไดรฟ์สัญญาณในไฟล์ Testbench ให้เขียนได้สั้นและอ่านง่ายขึ้น (สามารถใช้คำสั่ง wait for หน่วงเวลาภายใน Procedure ได้เมื่อใช้ใน Testbench)-- 1. ประกาศและนิยาม Procedure สำหรับส่งข้อมูลผ่าน Bus ใน Testbench
procedure write_bus ( constant addr : in std_logic_vector(7 downto 0); constant data : in std_logic_vector(7 downto 0); signal o_addr : out std_logic_vector(7 downto 0); signal o_data : out std_logic_vector(7 downto 0); signal o_valid: out std_logic
) is
begin o_addr &lt;= addr; o_data &lt;= data; o_valid &lt;= '1'; wait for 10 ns; -- หน่วงเวลา 1 Clock Cycle o_valid &lt;= '0'; wait for 10 ns;
end procedure write_bus; -- 2. การเรียกใช้งานภายใน Stimulus Process ของ Testbench
p_stimulus : process
begin -- ส่งข้อมูลผ่าน Procedure ได้สั้นกระชับ ไม่ต้องเขียนโค้ดซ้ำหลายบรรทัด write_bus(X"0A", X"FF", s_tb_addr, s_tb_data, s_tb_valid); write_bus(X"0B", X"A5", s_tb_addr, s_tb_data, s_tb_valid); wait;
end process p_stimulus;
สิ่งสำคัญที่สุดในการเขียน VHDL คือความเข้าใจเรื่อง การอัปเดตค่าของ Signal และ Variable:ข้อควรระวัง Signal vs Variable:
การใช้ Variable กำหนดค่าหลายครั้งใน Process เดียวกัน จะสร้าง Combinational Logic ที่ต่อซ้อนกันเป็นทอดๆ (Cascaded Logic) ซึ่งอาจส่งผลเสียต่อ Data Path Delay และเกิด Timing Violation ได้ หากไม่จำเป็นควรเน้นใช้ signal เป็นหลัก
เพื่อเพิ่มความอ่านง่าย (Readability) และลดความผิดพลาดในการเชื่อมต่อโมดูลความซับซ้อนสูง การออกแบบ FPGA มักมีข้อตกลงในการเติม Prefix/Suffix ให้กับสัญญาณข้อควรทราบก่อนอ่านตาราง
แต่ละบริษัท/ทีมออกแบบมี Naming Convention ของตัวเอง ซึ่งไม่มีสูตรตายตัวว่าแบบไหนถูกหรือผิด สิ่งที่สำคัญที่สุดคือ เลือกใช้ Convention เดียวให้สม่ำเสมอตลอดทั้งโปรเจกต์ ห้ามผสมหลาย Convention ปนกัน (เช่น ใช้ทั้ง _n และ _l แทนความหมาย Active-Low ในโปรเจกต์เดียวกัน) เพราะจะทำให้ทีมสับสนและอ่านโค้ดยากขึ้น
หัวข้อ 8.1 เป็นตัวอย่าง Convention หลักที่แนะนำ ส่วน 8.2 เป็น Convention อื่นๆ ที่พบได้บ่อยในอุตสาหกรรม ไว้สำหรับอ้างอิงเวลาไปอ่านโค้ดของทีม/บริษัทอื่น
⚠️ คำเตือน
ตารางด้านล่างนี้รวบรวมมาจากหลายแหล่ง/หลายบริษัท มีบาง Pattern ที่ ขัดแย้งกับตาราง 8.1 โดยตรง (เช่น Active-Low ใช้ _n ใน 8.1 แต่บางแหล่งใช้ _l, Internal Signal ใช้ s_* ใน 8.1 แต่บางแหล่งใช้ w_*) — อ่านไว้เพื่อ "รู้จัก" เวลาไปเจอโค้ดของทีมอื่น
Convention แบบ Xilinx/AMD (Active-Low ใช้ Suffix _l แทน _n)Convention แบบ EmLogic (Internal Signal แยก Prefix w_*/r_* ตาม Combinational/Registered)อ่านเพิ่มเติมได้ที่:
<br><a rel="noopener nofollow" class="external-link is-unresolved" href="https://emlogic.no/vhdl-conventions/" target="_self">https://emlogic.no/vhdl-conventions/</a>
<br><a rel="noopener nofollow" class="external-link is-unresolved" href="https://wiki.electroniciens.cnrs.fr/images/Xilinx_HDL_Coding_style.pdf" target="_self">https://wiki.electroniciens.cnrs.fr/images/Xilinx_HDL_Coding_style.pdf</a>
<br><a rel="noopener nofollow" class="external-link is-unresolved" href="https://nandland.com/coding-style-guidelines-for-vhdl-verilog/" target="_self">https://nandland.com/coding-style-guidelines-for-vhdl-verilog/</a>
ภาษา VHDL ถูกออกแบบมาให้ใช้ได้ทั้ง สร้างฮาร์ดแวร์จริง (Synthesis) และ จำลองพฤติกรรม (Simulation) แต่ไม่ใช่ทุกคำสั่งใน VHDL จะสามารถ "สังเคราะห์" ออกมาเป็นวงจรจริงบนชิปได้ บาง Syntax มีไว้สำหรับ Testbench เท่านั้น (Simulation-only) เพราะมันอธิบายพฤติกรรมของ "เวลา" หรือ "สภาพแวดล้อมการทดสอบ" ซึ่งไม่มีตัวตนอยู่จริงในฮาร์ดแวร์💡 หลักการ Synthesizable Code (RTL) = โค้ดที่ Synthesis Tool (Quartus, Vivado) แปลงเป็น Gate/Flip-Flop/Wire จริงได้
Non-synthesizable Code (Testbench-only) = โค้ดที่ใช้ได้เฉพาะตอน Simulate (ModelSim, QuestaSim, GHDL) เพื่อสร้าง Stimulus หรือตรวจสอบผลลัพธ์ ถ้านำไปสังเคราะห์ Synthesis Tool จะ Error หรือเพิกเฉย (Ignore) ทันที ผู้เริ่มต้นศึกษา FPGA ส่วนใหญ่มักเจอปัญหานี้เป็นด่านแรกๆ คือ แยกไม่ออกว่า Syntax ไหน "Synthesizable" (สังเคราะห์เป็นวงจรจริงได้) และ Syntax ไหน "Un-synthesizable" (ใช้ได้แค่ตอน Simulate) เพราะ Compiler ของ Simulator (เช่น ModelSim) จะ Compile ผ่านหมดทุกคำสั่ง โดยไม่มี Error ใดๆ ทำให้เข้าใจผิดว่าโค้ดที่รันผ่านใน Simulation จะสังเคราะห์เป็นฮาร์ดแวร์ได้เสมอ ทั้งที่จริงแล้ว VHDL เป็น Superset ที่ใหญ่กว่า Hardware ที่สังเคราะห์ได้จริงมาก ความสับสนนี้เป็นสาเหตุอันดับต้นๆ ที่ทำให้ Code รันบน Simulator ได้ปกติ แต่พอนำไป Synthesis กลับ Error หรือได้วงจรที่ทำงานผิดจากที่ตั้งใจ ตารางด้านล่างนี้จึงสรุป Keyword/Syntax ที่พบบ่อยไว้ให้library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use STD.TEXTIO.ALL; entity tb_my_module is
end entity tb_my_module; -- Testbench ไม่มี Port architecture sim of tb_my_module is constant C_CLK_PERIOD : time := 10 ns; -- 'time' type: Non-synthesizable signal clk : std_logic := '0'; signal rst_n : std_logic := '0'; signal i_data : std_logic_vector(7 downto 0) := (others =&gt; '0'); signal o_data : std_logic_vector(7 downto 0);
begin -- Instance ของวงจรจริง (DUT: Design Under Test) u_dut : entity work.my_module port map ( clk =&gt; clk, rst_n =&gt; rst_n, i_data =&gt; i_data, o_data =&gt; o_data ); ------------------------------------------------------------------ -- Clock Generator: ใช้ wait for สร้างสัญญาณนาฬิกา (Non-synthesizable) ------------------------------------------------------------------ p_clk_gen : process begin clk &lt;= '0'; wait for C_CLK_PERIOD / 2; clk &lt;= '1'; wait for C_CLK_PERIOD / 2; end process p_clk_gen; ------------------------------------------------------------------ -- Stimulus Process: สั่งค่าอินพุตตามลำดับเวลา แล้วปิดท้ายด้วย wait; ------------------------------------------------------------------ p_stimulus : process begin rst_n &lt;= '0'; wait for 20 ns; -- absolute delay: Non-synthesizable rst_n &lt;= '1'; wait until rising_edge(clk); -- รอ Edge: Non-synthesizable i_data &lt;= X"AA"; wait for 40 ns; assert (o_data = X"AA") -- assert/report: Non-synthesizable report "o_data mismatch!" severity error; wait; -- หยุด Process ค้างตลอดไป จบการทดสอบ end process p_stimulus; end architecture sim;
Syntax เหล่านี้ไม่สามารถสังเคราะห์เป็นวงจรจริงได้ wait for, wait until, assert/report, std.textio, ชนิดข้อมูล time/real
Clock Generator (p_clk_gen แบบข้างต้น) วิธีที่ง่ายและแม่นยำที่สุดคือ ลอง Compile/รัน Synthesis จริงบน Tool ที่ใช้งานอยู่ (เช่น Quartus, Vivado) หากโค้ดส่วนใดเป็น Non-synthesizable Tool จะแจ้ง Error หรือ Warning ให้ทราบทันที]]></description><link>coursecontent/chapter3-basic-vhdl.html</link><guid isPermaLink="false">CourseContent/chapter3-basic-vhdl.md</guid><pubDate>Mon, 05 Oct 2026 04:31:48 GMT</pubDate><enclosure url="coursecontent/img/vhdl_structure.png" length="0" type="image/png"/><content:encoded>&lt;figure&gt;&lt;img src="coursecontent/img/vhdl_structure.png"&gt;&lt;/figure&gt;</content:encoded></item><item><title><![CDATA[chapter2-fundamental-of-digital-logic]]></title><description><![CDATA[
<a data-tooltip-position="top" aria-label="1 ระบบเลขฐานและการแทนค่าข้อมูล (Number Systems &amp; Data Representation)" data-href="#1 ระบบเลขฐานและการแทนค่าข้อมูล (Number Systems &amp; Data Representation)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1_ระบบเลขฐานและการแทนค่าข้อมูล_(Number_Systems_&amp;_Data_Representation)_0" class="internal-link" target="_self" rel="noopener nofollow">1 ระบบเลขฐานและการแทนค่าข้อมูล (Number Systems &amp; Data Representation)</a> <br><a data-tooltip-position="top" aria-label="1.1 เลขฐานสองและเลขฐานสิบหก (Binary &amp; Hexadecimal)" data-href="#1.1 เลขฐานสองและเลขฐานสิบหก (Binary &amp; Hexadecimal)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.1_เลขฐานสองและเลขฐานสิบหก_(Binary_&amp;_Hexadecimal)_0" class="internal-link" target="_self" rel="noopener nofollow">1.1 เลขฐานสองและเลขฐานสิบหก (Binary &amp; Hexadecimal)</a>
<br><a data-tooltip-position="top" aria-label="1.2 การแทนค่าเลขติดลบ (Signed Number Representations)" data-href="#1.2 การแทนค่าเลขติดลบ (Signed Number Representations)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.2_การแทนค่าเลขติดลบ_(Signed_Number_Representations)_0" class="internal-link" target="_self" rel="noopener nofollow">1.2 การแทนค่าเลขติดลบ (Signed Number Representations)</a> <br><a data-tooltip-position="top" aria-label="1.2.1 วิธีการแทนค่าเลขติดลบ 3 รูปแบบหลัก" data-href="#1.2.1 วิธีการแทนค่าเลขติดลบ 3 รูปแบบหลัก" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.2.1_วิธีการแทนค่าเลขติดลบ_3_รูปแบบหลัก_0" class="internal-link" target="_self" rel="noopener nofollow">1.2.1 วิธีการแทนค่าเลขติดลบ 3 รูปแบบหลัก</a>
<br><a data-tooltip-position="top" aria-label="1.2.2 ตารางเปรียบเทียบการแปลงรหัสไบนารี 8-bit ในแต่ละระบบ (8-bit Signed Representation Comparison)" data-href="#1.2.2 ตารางเปรียบเทียบการแปลงรหัสไบนารี 8-bit ในแต่ละระบบ (8-bit Signed Representation Comparison)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.2.2_ตารางเปรียบเทียบการแปลงรหัสไบนารี_8-bit_ในแต่ละระบบ_(8-bit_Signed_Representation_Comparison)_0" class="internal-link" target="_self" rel="noopener nofollow">1.2.2 ตารางเปรียบเทียบการแปลงรหัสไบนารี 8-bit ในแต่ละระบบ (8-bit Signed Representation Comparison)</a> <br><a data-tooltip-position="top" aria-label="1.3 การบวกและการลบเลขฐานสอง (Binary Addition &amp; Subtraction)" data-href="#1.3 การบวกและการลบเลขฐานสอง (Binary Addition &amp; Subtraction)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.3_การบวกและการลบเลขฐานสอง_(Binary_Addition_&amp;_Subtraction)_0" class="internal-link" target="_self" rel="noopener nofollow">1.3 การบวกและการลบเลขฐานสอง (Binary Addition &amp; Subtraction)</a> <br><a data-tooltip-position="top" aria-label="1.3.1 หลักการบวกเลขฐานสอง (Binary Addition Rule)" data-href="#1.3.1 หลักการบวกเลขฐานสอง (Binary Addition Rule)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.3.1_หลักการบวกเลขฐานสอง_(Binary_Addition_Rule)_0" class="internal-link" target="_self" rel="noopener nofollow">1.3.1 หลักการบวกเลขฐานสอง (Binary Addition Rule)</a>
<br><a data-tooltip-position="top" aria-label="1.3.2 การลบเลขฐานสองด้วย 2's Complement (Subtraction via Addition)" data-href="#1.3.2 การลบเลขฐานสองด้วย 2's Complement (Subtraction via Addition)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.3.2_การลบเลขฐานสองด้วย_2's_Complement_(Subtraction_via_Addition)_0" class="internal-link" target="_self" rel="noopener nofollow">1.3.2 การลบเลขฐานสองด้วย 2's Complement (Subtraction via Addition)</a>
<br><a data-tooltip-position="top" aria-label="1.3.3 การตรวจจับ Overflow ในการบวกเลข Signed (Signed Overflow Detection)" data-href="#1.3.3 การตรวจจับ Overflow ในการบวกเลข Signed (Signed Overflow Detection)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.3.3_การตรวจจับ_Overflow_ในการบวกเลข_Signed_(Signed_Overflow_Detection)_0" class="internal-link" target="_self" rel="noopener nofollow">1.3.3 การตรวจจับ Overflow ในการบวกเลข Signed (Signed Overflow Detection)</a> <br><a data-tooltip-position="top" aria-label="1.4 ระบบทศนิยม: Fixed-Point vs Floating-Point" data-href="#1.4 ระบบทศนิยม: Fixed-Point vs Floating-Point" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.4_ระบบทศนิยม_Fixed-Point_vs_Floating-Point_0" class="internal-link" target="_self" rel="noopener nofollow">1.4 ระบบทศนิยม: Fixed-Point vs Floating-Point</a> <br><a data-tooltip-position="top" aria-label="1.4.1 ตัวอย่าง: การแปลงค่าเป็น Fixed-Point (Q4.4)" data-href="#1.4.1 ตัวอย่าง: การแปลงค่าเป็น Fixed-Point (Q4.4)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.4.1_ตัวอย่าง_การแปลงค่าเป็น_Fixed-Point_(Q4.4)_0" class="internal-link" target="_self" rel="noopener nofollow">1.4.1 ตัวอย่าง: การแปลงค่าเป็น Fixed-Point (Q4.4)</a>
<br><a data-tooltip-position="top" aria-label="1.4.2 ตัวอย่าง: การแปลงค่าเป็น Floating-Point (IEEE 754 Single Precision)" data-href="#1.4.2 ตัวอย่าง: การแปลงค่าเป็น Floating-Point (IEEE 754 Single Precision)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#1.4.2_ตัวอย่าง_การแปลงค่าเป็น_Floating-Point_(IEEE_754_Single_Precision)_0" class="internal-link" target="_self" rel="noopener nofollow">1.4.2 ตัวอย่าง: การแปลงค่าเป็น Floating-Point (IEEE 754 Single Precision)</a> <br><a data-tooltip-position="top" aria-label="2 พื้นฐานลอจิกเกตและการทำงานบน FPGA (Basic Logic Gates &amp; FPGA LUT Implementation)" data-href="#2 พื้นฐานลอจิกเกตและการทำงานบน FPGA (Basic Logic Gates &amp; FPGA LUT Implementation)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#2_พื้นฐานลอจิกเกตและการทำงานบน_FPGA_(Basic_Logic_Gates_&amp;_FPGA_LUT_Implementation)_0" class="internal-link" target="_self" rel="noopener nofollow">2 พื้นฐานลอจิกเกตและการทำงานบน FPGA (Basic Logic Gates &amp; FPGA LUT Implementation)</a> <br><a data-tooltip-position="top" aria-label="2.1 ลอจิกเกตพื้นฐาน (Basic Logic Gates)" data-href="#2.1 ลอจิกเกตพื้นฐาน (Basic Logic Gates)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#2.1_ลอจิกเกตพื้นฐาน_(Basic_Logic_Gates)_0" class="internal-link" target="_self" rel="noopener nofollow">2.1 ลอจิกเกตพื้นฐาน (Basic Logic Gates)</a> <br><a data-tooltip-position="top" aria-label="2.1.1 ตารางสรุปลอจิกเกตพื้นฐาน (Logic Gates Summary Table)" data-href="#2.1.1 ตารางสรุปลอจิกเกตพื้นฐาน (Logic Gates Summary Table)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#2.1.1_ตารางสรุปลอจิกเกตพื้นฐาน_(Logic_Gates_Summary_Table)_0" class="internal-link" target="_self" rel="noopener nofollow">2.1.1 ตารางสรุปลอจิกเกตพื้นฐาน (Logic Gates Summary Table)</a>
<br><a data-tooltip-position="top" aria-label="2.1.2 สัญลักษณ์ทางฮาร์ดแวร์และตารางความจริงของแต่ละเกต (Logic Gate Diagrams &amp; Truth Tables)" data-href="#2.1.2 สัญลักษณ์ทางฮาร์ดแวร์และตารางความจริงของแต่ละเกต (Logic Gate Diagrams &amp; Truth Tables)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#2.1.2_สัญลักษณ์ทางฮาร์ดแวร์และตารางความจริงของแต่ละเกต_(Logic_Gate_Diagrams_&amp;_Truth_Tables)_0" class="internal-link" target="_self" rel="noopener nofollow">2.1.2 สัญลักษณ์ทางฮาร์ดแวร์และตารางความจริงของแต่ละเกต (Logic Gate Diagrams &amp; Truth Tables)</a> <br><a data-tooltip-position="top" aria-label="2.2 การจัดเก็บลอจิกเกตใน FPGA ผ่าน Look-Up Table (LUT Mapping)" data-href="#2.2 การจัดเก็บลอจิกเกตใน FPGA ผ่าน Look-Up Table (LUT Mapping)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#2.2_การจัดเก็บลอจิกเกตใน_FPGA_ผ่าน_Look-Up_Table_(LUT_Mapping)_0" class="internal-link" target="_self" rel="noopener nofollow">2.2 การจัดเก็บลอจิกเกตใน FPGA ผ่าน Look-Up Table (LUT Mapping)</a> <br><a data-tooltip-position="top" aria-label="3 สถาปัตยกรรมและประเภทของวงจรดิจิทัล (Digital Logic Architectures: Combinational vs Sequential Logic)" data-href="#3 สถาปัตยกรรมและประเภทของวงจรดิจิทัล (Digital Logic Architectures: Combinational vs Sequential Logic)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#3_สถาปัตยกรรมและประเภทของวงจรดิจิทัล_(Digital_Logic_Architectures_Combinational_vs_Sequential_Logic)_0" class="internal-link" target="_self" rel="noopener nofollow">3 สถาปัตยกรรมและประเภทของวงจรดิจิทัล (Digital Logic Architectures: Combinational vs Sequential Logic)</a> <br><a data-tooltip-position="top" aria-label="3.2 ตารางเปรียบเทียบ Combinational vs Sequential Logic" data-href="#3.2 ตารางเปรียบเทียบ Combinational vs Sequential Logic" href="coursecontent/chapter2-fundamental-of-digital-logic.html#3.2_ตารางเปรียบเทียบ_Combinational_vs_Sequential_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">3.2 ตารางเปรียบเทียบ Combinational vs Sequential Logic</a>
<br><a data-tooltip-position="top" aria-label="3.3 Combinational Logic" data-href="#3.3 Combinational Logic" href="coursecontent/chapter2-fundamental-of-digital-logic.html#3.3_Combinational_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">3.3 Combinational Logic</a> <br><a data-tooltip-position="top" aria-label="3.3.2 ตัวอย่างวงจร Combinational ที่สำคัญใน FPGA" data-href="#3.3.2 ตัวอย่างวงจร Combinational ที่สำคัญใน FPGA" href="coursecontent/chapter2-fundamental-of-digital-logic.html#3.3.2_ตัวอย่างวงจร_Combinational_ที่สำคัญใน_FPGA_0" class="internal-link" target="_self" rel="noopener nofollow">3.3.2 ตัวอย่างวงจร Combinational ที่สำคัญใน FPGA</a> <br><a data-tooltip-position="top" aria-label="3.4 Sequential Logic" data-href="#3.4 Sequential Logic" href="coursecontent/chapter2-fundamental-of-digital-logic.html#3.4_Sequential_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">3.4 Sequential Logic</a> <br><a data-tooltip-position="top" aria-label="3.4.1 ส่วนประกอบหลักของ Sequential Logic" data-href="#3.4.1 ส่วนประกอบหลักของ Sequential Logic" href="coursecontent/chapter2-fundamental-of-digital-logic.html#3.4.1_ส่วนประกอบหลักของ_Sequential_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">3.4.1 ส่วนประกอบหลักของ Sequential Logic</a>
<br><a data-tooltip-position="top" aria-label="3.4.2 วงจร Sequential ที่พบบ่อยใน FPGA (สร้างจาก D Flip-Flop)" data-href="#3.4.2 วงจร Sequential ที่พบบ่อยใน FPGA (สร้างจาก D Flip-Flop)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#3.4.2_วงจร_Sequential_ที่พบบ่อยใน_FPGA_(สร้างจาก_D_Flip-Flop)_0" class="internal-link" target="_self" rel="noopener nofollow">3.4.2 วงจร Sequential ที่พบบ่อยใน FPGA (สร้างจาก D Flip-Flop)</a>
<br><a data-tooltip-position="top" aria-label="3.4.3 ข้อควรระวัง: การเผลอสร้าง Latch (Unintentional Latch Inference)" data-href="#3.4.3 ข้อควรระวัง: การเผลอสร้าง Latch (Unintentional Latch Inference)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#3.4.3_ข้อควรระวัง_การเผลอสร้าง_Latch_(Unintentional_Latch_Inference)_0" class="internal-link" target="_self" rel="noopener nofollow">3.4.3 ข้อควรระวัง: การเผลอสร้าง Latch (Unintentional Latch Inference)</a> <br><a data-tooltip-position="top" aria-label="4 การออกแบบสัญญาณนาฬิกาและรีเซ็ต (Clock &amp; Reset Design)" data-href="#4 การออกแบบสัญญาณนาฬิกาและรีเซ็ต (Clock &amp; Reset Design)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#4_การออกแบบสัญญาณนาฬิกาและรีเซ็ต_(Clock_&amp;_Reset_Design)_0" class="internal-link" target="_self" rel="noopener nofollow">4 การออกแบบสัญญาณนาฬิกาและรีเซ็ต (Clock &amp; Reset Design)</a> <br><a data-tooltip-position="top" aria-label="4.1 สัญญาณนาฬิกาและ Synchronous Design Standard" data-href="#4.1 สัญญาณนาฬิกาและ Synchronous Design Standard" href="coursecontent/chapter2-fundamental-of-digital-logic.html#4.1_สัญญาณนาฬิกาและ_Synchronous_Design_Standard_0" class="internal-link" target="_self" rel="noopener nofollow">4.1 สัญญาณนาฬิกาและ Synchronous Design Standard</a> <br><a data-tooltip-position="top" aria-label="4.1.2 วงจร Synchronizer สำหรับสัญญาณ 1 บิต (2-Flip-Flop Synchronizer)" data-href="#4.1.2 วงจร Synchronizer สำหรับสัญญาณ 1 บิต (2-Flip-Flop Synchronizer)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#4.1.2_วงจร_Synchronizer_สำหรับสัญญาณ_1_บิต_(2-Flip-Flop_Synchronizer)_0" class="internal-link" target="_self" rel="noopener nofollow">4.1.2 วงจร Synchronizer สำหรับสัญญาณ 1 บิต (2-Flip-Flop Synchronizer)</a> <br><a data-tooltip-position="top" aria-label="4.2 สัญญาณรีเซ็ต (Reset Signal Design)" data-href="#4.2 สัญญาณรีเซ็ต (Reset Signal Design)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#4.2_สัญญาณรีเซ็ต_(Reset_Signal_Design)_0" class="internal-link" target="_self" rel="noopener nofollow">4.2 สัญญาณรีเซ็ต (Reset Signal Design)</a> <br><a data-tooltip-position="top" aria-label="4.2.2 ตัวอย่างโค้ด VHDL เปรียบเทียบ Synchronous Reset vs Asynchronous Reset" data-href="#4.2.2 ตัวอย่างโค้ด VHDL เปรียบเทียบ Synchronous Reset vs Asynchronous Reset" href="coursecontent/chapter2-fundamental-of-digital-logic.html#4.2.2_ตัวอย่างโค้ด_VHDL_เปรียบเทียบ_Synchronous_Reset_vs_Asynchronous_Reset_0" class="internal-link" target="_self" rel="noopener nofollow">4.2.2 ตัวอย่างโค้ด VHDL เปรียบเทียบ Synchronous Reset vs Asynchronous Reset</a> <br><a data-tooltip-position="top" aria-label="5 Finite State Machines - FSM" data-href="#5 Finite State Machines - FSM" href="coursecontent/chapter2-fundamental-of-digital-logic.html#5_Finite_State_Machines_-_FSM_0" class="internal-link" target="_self" rel="noopener nofollow">5 Finite State Machines - FSM</a> <br><a data-tooltip-position="top" aria-label="5.1 ประเภทของ FSM: Moore Machine vs Mealy Machine" data-href="#5.1 ประเภทของ FSM: Moore Machine vs Mealy Machine" href="coursecontent/chapter2-fundamental-of-digital-logic.html#5.1_ประเภทของ_FSM_Moore_Machine_vs_Mealy_Machine_0" class="internal-link" target="_self" rel="noopener nofollow">5.1 ประเภทของ FSM: Moore Machine vs Mealy Machine</a> <br><a data-tooltip-position="top" aria-label="5.1.2 ตัวอย่างโค้ด: Moore FSM ควบคุมไฟจราจรอย่างง่าย (Traffic Light)" data-href="#5.1.2 ตัวอย่างโค้ด: Moore FSM ควบคุมไฟจราจรอย่างง่าย (Traffic Light)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#5.1.2_ตัวอย่างโค้ด_Moore_FSM_ควบคุมไฟจราจรอย่างง่าย_(Traffic_Light)_0" class="internal-link" target="_self" rel="noopener nofollow">5.1.2 ตัวอย่างโค้ด: Moore FSM ควบคุมไฟจราจรอย่างง่าย (Traffic Light)</a> <br><a data-tooltip-position="top" aria-label="6 การวิเคราะห์เวลาและสัญญาณรูปคลื่น (Timing Analysis &amp; Waveform Analysis)" data-href="#6 การวิเคราะห์เวลาและสัญญาณรูปคลื่น (Timing Analysis &amp; Waveform Analysis)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#6_การวิเคราะห์เวลาและสัญญาณรูปคลื่น_(Timing_Analysis_&amp;_Waveform_Analysis)_0" class="internal-link" target="_self" rel="noopener nofollow">6 การวิเคราะห์เวลาและสัญญาณรูปคลื่น (Timing Analysis &amp; Waveform Analysis)</a> <br><a data-tooltip-position="top" aria-label="6.1 การอ่าน รูปคลื่นสัญญาณ (Timing Diagram Reading)" data-href="#6.1 การอ่าน รูปคลื่นสัญญาณ (Timing Diagram Reading)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#6.1_การอ่าน_รูปคลื่นสัญญาณ_(Timing_Diagram_Reading)_0" class="internal-link" target="_self" rel="noopener nofollow">6.1 การอ่าน รูปคลื่นสัญญาณ (Timing Diagram Reading)</a> <br><a data-tooltip-position="top" aria-label="6.1.1 รูปคลื่นสัญญาณพื้นฐาน (Basic Shift Register Waveform)" data-href="#6.1.1 รูปคลื่นสัญญาณพื้นฐาน (Basic Shift Register Waveform)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#6.1.1_รูปคลื่นสัญญาณพื้นฐาน_(Basic_Shift_Register_Waveform)_0" class="internal-link" target="_self" rel="noopener nofollow">6.1.1 รูปคลื่นสัญญาณพื้นฐาน (Basic Shift Register Waveform)</a>
<br><a data-tooltip-position="top" aria-label="6.1.2 ตัวอย่างรูปคลื่นการทำงานจริง: AXI4-Lite Protocol Waveform" data-href="#6.1.2 ตัวอย่างรูปคลื่นการทำงานจริง: AXI4-Lite Protocol Waveform" href="coursecontent/chapter2-fundamental-of-digital-logic.html#6.1.2_ตัวอย่างรูปคลื่นการทำงานจริง_AXI4-Lite_Protocol_Waveform_0" class="internal-link" target="_self" rel="noopener nofollow">6.1.2 ตัวอย่างรูปคลื่นการทำงานจริง: AXI4-Lite Protocol Waveform</a> <br><a data-tooltip-position="top" aria-label="6.2 ข้อกำหนดเวลา Setup Time และ Hold Time" data-href="#6.2 ข้อกำหนดเวลา Setup Time และ Hold Time" href="coursecontent/chapter2-fundamental-of-digital-logic.html#6.2_ข้อกำหนดเวลา_Setup_Time_และ_Hold_Time_0" class="internal-link" target="_self" rel="noopener nofollow">6.2 ข้อกำหนดเวลา Setup Time และ Hold Time</a>
<br><a data-tooltip-position="top" aria-label="6.3 Timing Slack &amp; Maximum Frequency ($F_{max}$)" data-href="#6.3 Timing Slack &amp; Maximum Frequency ($F_{max}$)" href="coursecontent/chapter2-fundamental-of-digital-logic.html#6.3_Timing_Slack_&amp;_Maximum_Frequency_($F_{max}$)_0" class="internal-link" target="_self" rel="noopener nofollow">6.3 Timing Slack &amp; Maximum Frequency ($F_{max}$)</a> เนื้อหาบทนี้มุ่งเน้นการปูพื้นฐาน Digital Logic Fundamentals ที่จำเป็นสำหรับการเขียนภาษาบรรยายฮาร์ดแวร์ (VHDL/Verilog) บน FPGA ตั้งแต่ระบบเลขฐาน วงจรตรรกะ Combinational &amp; Sequential ไปจนถึงสถาปัตยกรรม Synchronous Design และการวิเคราะห์ Timing Closureในระบบดิจิทัล สัญญาณไฟฟ้ามีเพียง 2 สถานะคือ HIGH (1) และ LOW (0) การเข้าใจรูปแบบการแทนค่าข้อมูลจึงเป็นจุดเริ่มต้นที่สำคัญในการออกแบบฮาร์ดแวร์:
Binary System (Base 2): ประกอบด้วยตัวเลข 0 และ 1 Bit (Binary Digit): หน่วยที่เล็กที่สุด (0 หรือ 1)
Byte: 8 bits (เช่น 1101_0010)
Word: ขึ้นอยู่กับสถาปัตยกรรม (16 bits, 32 bits หรือ 64 bits) Hexadecimal System (Base 16): ใช้ตัวเลข 0-9 และ A-F แทนค่า 10-15 โดย 1 Hex Digit จะแทนเลขฐานสองได้ 4 bits พอดี ช่วยให้มนุษย์อ่านบัสข้อมูลขนาดยาวได้ง่ายขึ้น
ในการออกแบบระบบดิจิทัลและการคำนวณใน FPGA การแทนค่าตัวเลขติดลบ (Signed Numbers) สามารถทำได้ 3 วิธีหลัก: Sign and Magnitude (บิตเครื่องหมาย + ขนาด): ใช้บิตซ้ายสุด (MSB) เป็น Sign Bit () และบิตที่เหลือเก็บค่าขนาดตัวเลข (Magnitude)
ตัวอย่าง (4 บิต): 0101, 1101
ข้อเสีย: มีค่า ศูนย์ 2 ค่า (+0 = 0000, -0 = 1000) และสร้างวงจรบวก/ลบทางฮาร์ดแวร์ได้ซับซ้อน 1's Complement (คอมพลีเมนต์หนึ่ง): สลับบิตทั้งหมดจาก 0 เป็น 1 และ 1 เป็น 0 เพื่อแทนค่าติดลบ
ตัวอย่าง (4 บิต): 0101, 1010
ข้อเสีย: ยังคงมีค่า ศูนย์ 2 ค่า (+0 = 0000, -0 = 1111) และต้องใช้วงจร End-around Carry ในการบวกเลข 2's Complement (คอมพลีเมนต์สอง) — ⭐ วิธีมาตรฐานที่นิยมใช้แพร่หลายที่สุด: นำค่า 1's Complement มา บวกเพิ่มด้วย 1
ข้อดีสำคัญ: มีค่า ศูนย์เพียงค่าเดียว (0000) สามารถใช้ วงจรบวกฮาร์ดแวร์ (Adder Circuit) ชุดเดียวกัน คำนวณได้ทั้งการบวกและการลบเลข โดยไม่ต้องสร้างวงจรลบแยกต่างหาก ช่วยประหยัดพื้นที่ LUTs ใน FPGA ได้อย่างมาก <br><img alt="img/complement.png" src="coursecontent/img/complement.png" target="_self" style="width: 420px; max-width: 100%;">Cyclic depiction of unsigned (white ring),
ones'-complement (orange),
and two's-complement (blue) values of 4-bit integersสรุปข้อสังเกตจากตาราง 8-bit: 1111_1011: Sign &amp; Magnitude: MSB=1 (ลบ), บิตที่เหลือ 111 1011 = 123 ค่าคือ 1's Complement: MSB=1 (ลบ), สลับบิตกลับได้ 0000 0100 = 4 ค่าคือ 2's Complement: MSB=1 (ลบ), สลับบิตกลับแล้ว +1 ได้ 0000 0101 = 5 ค่าคือ 2's Complement กำจัดปัญหา / ทำให้ขอบเขตเก็บค่าได้กว้างกว่า ( ถึง ) ในขณะที่วิธีอื่นเก็บได้เพียง ถึง การบวกเลขฐานสองมีกฎพื้นฐาน 4 กรณี (เหมือนวงจร Half Adder):เมื่อบวกเลขหลายบิต แต่ละหลักต้องนำ Carry จากหลักก่อนหน้า มาบวกร่วมด้วย (วงจร Full Adder ที่รับอินพุต 3 ตัว คือ A, B, และ Carry-in)ตัวอย่าง: บวกเลขฐานสอง 4 บิตแบบ Unsigned Carry: 0 1 1 0 0 1 0 1 (5) + 0 0 1 1 (3) ----------- 1 0 0 0 (8)
ไล่ทีละบิตจาก LSB (ขวาสุด): (Sum=0, Carry=1) → (Sum=0, Carry=1) → (Sum=0, Carry=1) → (Sum=1, Carry=0) → ผลลัพธ์ 1000 ✅จุดเด่นสำคัญของ 2's Complement (ตามหัวข้อ 2.2) คือทำให้ใช้ วงจรบวก (Adder) ตัวเดียวกัน คำนวณการลบได้ โดยใช้หลักการ:
ตัวอย่าง: ลบเลขฐานสอง 4 บิตแบบ Signed ผลลัพธ์ ✅ ตรงกับการลบเลขปกติ — นี่คือเหตุผลที่ Hardware ไม่จำเป็นต้องมีวงจรลบ (Subtractor) แยกต่างหากจากวงจรบวกเลยใน FPGAเนื่องจากเลข Signed ถูกจำกัดขอบเขตค่าตามจำนวนบิต (เช่น 4 บิต Signed เก็บได้แค่ ถึง ตามหัวข้อ 2.2) การบวกเลขสองจำนวนที่ มีเครื่องหมายเดียวกัน อาจได้ผลลัพธ์ที่เกินขอบเขตนั้นได้ เรียกว่า Overflowกฎการตรวจจับ: (ใช้ XOR)ตัวอย่าง: Overflow เมื่อบวก ด้วย Signed 4 บิต Carry: 0 1 1 0 0 1 0 1 (+5) + 0 1 0 0 (+4) ----------- 1 0 0 1 (ผลลัพธ์ดิบ) Carry-in เข้า MSB (บิตที่ 3) , Carry-out จาก MSB → เกิด Overflow!
ตรวจสอบ: ผลลัพธ์ 1001 เมื่ออ่านแบบ Signed 4 บิต (2's Complement) จะได้ ซึ่งผิดอย่างชัดเจน (ค่าจริงคือ ซึ่งเกินขอบเขต สูงสุดของ Signed 4 บิต) จึงต้องอาศัย Overflow Flag เป็นตัวเตือนว่าผลลัพธ์ที่ได้ใช้งานไม่ได้
ในทางปฏิบัติ ไม่ต้องคำนวณ Overflow ด้วยมือแบบนี้ — เครื่องมือ Synthesis และไลบรารี ieee.numeric_std ใน VHDL จัดการเรื่อง Carry/Overflow ให้อัตโนมัติเมื่อใช้ signed/unsigned type บวกลบกันตามปกติ (ดูตัวอย่างโค้ดจริงในหัวข้อ 4.3.1.1.3) หน้าที่ของผู้ออกแบบคือ เลือกจำนวนบิตของ Signal ให้กว้างเพียงพอ ไม่ให้ค่าที่คำนวณได้เกินขอบเขตที่ Signal เก็บได้ตั้งแต่แรก
กรณีใช้ไลบรารีรุ่นเก่า (Legacy Library): std_logic_unsigned
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all; โค้ดรุ่นเก่าหรือโปรเจกต์เก่าบางส่วนยังใช้ std_logic_unsigned แทน ieee.numeric_std เพื่อบวก/ลบ std_logic_vector ได้โดยตรง (ไม่ต้องแปลงเป็น signed/unsigned type ก่อน) ซึ่งมีข้อควรระวังสำคัญ: ไม่ใช่มาตรฐาน IEEE จริงๆ: แม้จะอยู่ภายใต้ library ieee แต่ std_logic_unsigned เป็นไลบรารีที่ Synopsys สร้างขึ้นเอง (Vendor-specific/Non-standard) ไม่ได้รับรองโดย IEEE อย่างเป็นทางการ ต่างจาก numeric_std ที่เป็นมาตรฐาน IEEE 1076.3 แท้จริง
ตีความเป็น Unsigned เสมอ: std_logic_unsigned Overload Operator (+, -, &gt;, &lt;, =) ให้ทำงานกับ std_logic_vector ได้โดยตรงเลย โดยตีความเป็น Unsigned เท่านั้น ไม่มีทางเลือกตีความแบบ Signed ในตัวมันเอง หากต้องการ Signed ต้องพึ่ง std_logic_arith เพิ่ม ซึ่งอาจชนกับ std_logic_unsigned ได้หากใช้ปนกัน (Ambiguous Operator Error)
Overflow ยังคงต้องระวังเหมือนเดิม: ไม่ว่าจะใช้ไลบรารีไหน หลักการ Overflow ยังเป็นไปตามกฎ Carry-in XOR Carry-out ของ MSB เหมือนเดิมทุกประการ — std_logic_unsigned ไม่ได้มีกลไกป้องกัน Overflow ที่ดีไปกว่า numeric_std แต่อย่างใด คำแนะนำ: สำหรับโค้ดใหม่ ควรใช้ ieee.numeric_std เป็นมาตรฐาน (ตามที่ใช้ในตัวอย่างทั้งหมดของเอกสารนี้) เพราะเป็นมาตรฐานสากลที่ Tool สมัยใหม่ทุกตัวรองรับ และแยก Type signed/unsigned ชัดเจนกว่า ส่วน std_logic_unsigned มีไว้เพื่อความเข้าใจเมื่อต้องอ่านหรือ Maintain โค้ดเก่าเท่านั้น Fixed-Point (จุดตรึง): กำหนดตำแหน่งจุดทศนิยมตายตัว (เช่น Q4.4 = 4-bit Integer + 4-bit Fractional) ข้อดี: สังเคราะห์เป็นฮาร์ดแวร์ได้ง่าย ประหยัด LUTs และใช้ DSP Block ทำงานได้เร็วมาก (นิยมสุดใน FPGA) Floating-Point (จุดลอย): ตามมาตรฐาน IEEE 754 (Single Precision 32-bit / Double 64-bit) ข้อดี: ขอบเขตช่วงตัวเลขกว้างมาก
ข้อเสีย: วงจรซับซ้อน ใช้ทรัพยากร FPGA สูง และเกิด Latency มากขึ้น รูปแบบ Q4.4 หมายถึง 8 บิตทั้งหมด แบ่งเป็น 4 บิตส่วนจำนวนเต็ม (Integer) และ 4 บิตส่วนทศนิยม (Fractional) โดยค่าทศนิยมจะถูกคูณด้วย (Scale Factor) แล้วปัดเป็นจำนวนเต็มก่อนเก็บลงรีจิสเตอร์ตัวอย่างที่ 1: แปลงเลข เป็น Q4.4สรุปการแบ่งบิต ในรูปแบบ Q4.4:ตรวจสอบย้อนกลับ (Decode): 0101_1100 → Integer = 0101 = 5, Fractional = 1100 = 12 → ค่า ✅ ตรงกับค่าตั้งต้นตัวอย่างที่ 2: Signed Fixed-Point ค่าติดลบ (2's Complement, เพิ่ม Sign bit เป็น 9 บิต)Fixed-Point ที่เป็นเลขติดลบ ยังคงใช้ 2's Complement เช่นเดียวกับเลขจำนวนเต็ม Signed ทั่วไป (ตามหัวข้อ 2.2) เพียงแค่ "จินตนาการ" ตำแหน่งจุดทศนิยมแทรกเข้าไปในบิตแพทเทิร์นเดิมเท่านั้น ตัว Hardware Adder/Subtractor ไม่รู้และไม่สนใจตำแหน่งจุดทศนิยม จึงบวกลบบิตแบบ Binary ปกติเหมือนเดิมทุกประการในตัวอย่างนี้จะเพิ่ม Sign bit อีก 1 บิต นำหน้า Q4.4 เดิม รวมเป็น 9 บิต (เรียกได้ว่าเป็น Signed Q4.4 หรือ 1.4.4 format) เพื่อให้เห็นตำแหน่ง Sign bit แยกออกมาชัดเจนจาก Integer/Fractionตัวอย่าง: แปลง เป็น Signed Q4.4 (9 บิต)ตรวจสอบย้อนกลับ: มองทั้ง 9 บิต 1_1010_0100 เป็นเลข Signed 2's Complement (ไม่สนใจจุดทศนิยมก่อน) → MSB=1 → เป็นค่าลบ → คำนวณ Magnitude โดย Invert + บวก 1 กลับ: 1_1010_0100 → invert → 0_0101_1011 → +1 → 0_0101_1100 → หารด้วย Scale Factor → ✅ ตรงกับค่าตั้งต้นทำไมเพิ่ม Sign bit เป็น 9 บิต? การเพิ่ม Sign bit แยกต่างหาก (Sign-Extend) ทำให้เห็นตำแหน่งเครื่องหมายชัดเจนขึ้นในเชิงการอธิบาย แต่ในทางปฏิบัติเวลาออกแบบวงจรจริง มักไม่แยก Sign bit ออกมาต่างหาก — ระบบ Q4.4 แบบ Signed 8 บิตเดิม (ที่ MSB ของ Integer field ทำหน้าที่เป็น Sign bit ไปในตัวอยู่แล้วตามคุณสมบัติของ 2's Complement) ก็เพียงพอและประหยัดบิตกว่า การเพิ่มเป็น 9 บิตจึงเทียบเท่ากับการทำ Sign Extension ของเลข 8 บิตเดิมเท่านั้น
รูปแบบ IEEE 754 Single Precision (32 บิต) แบ่งเป็น 3 ส่วน: Sign (1 บิต) + Exponent (8 บิต, Bias = 127) + Mantissa (23 บิต)ตัวอย่าง: แปลงเลข เป็น IEEE 754 Single Precisionผลลัพธ์รวม 32 บิต: 0 10000001 01110000000000000000000 ตัวอย่าง: ค่าติดลบ ใน IEEE 754ต่างจาก Fixed-Point ตรงที่ Floating-Point ไม่ใช้ 2's Complement กับเลขติดลบ — ใช้วิธีที่ง่ายกว่ามาก คือ Sign-Magnitude โดยแค่ สลับ Sign bit จาก 0 เป็น 1 เท่านั้น ส่วน Exponent และ Mantissa เหมือนเดิมทุกบิต (เพราะ Magnitude ของ ไม่เปลี่ยน):ผลลัพธ์รวม 32 บิต: 1 10000001 01110000000000000000000 สังเกตความแตกต่าง: Fixed-Point ( → 8 บิต 0x5C) ใช้พื้นที่น้อยกว่ามาก แต่ครอบคลุมช่วงค่าจำกัด (Q4.4 ครอบคลุมได้แค่ ถึง ) ในขณะที่ Floating-Point ( → 32 บิต 0x40B80000) ใช้พื้นที่มากกว่า 4 เท่า แต่ครอบคลุมช่วงค่าได้กว้างกว่ามหาศาล (ประมาณ ) — นี่คือ Trade-off หลักที่ต้องพิจารณาเวลาเลือกใช้งานบน FPGA
ลอจิกเกตถือเป็น อิฐบล็อกพื้นฐานที่สุดของวงจรดิจิทัล ทั้งหมด ไม่ว่าจะเป็นวงจรประเภท Combinational Logic (ที่ใช้ Gate ประกอบเป็น MUX/Decoder/Adder) หรือ Sequential Logic (ที่ใช้ Gate ในการคำนวณ Next State Logic หน้า Flip-Flop) ข้อแตกต่างระหว่าง ASIC และ FPGA:
ในวงจรรวมแบบสั่งทำพิเศษ (ASIC) ลอจิกเกตจะถูกสร้างจากทรานซิสเตอร์จริงบนซิลิกอน แต่ใน FPGA ตรรกะเกตทั้งหมดจะถูกสังเคราะห์และจัดเก็บเอาต์พุตไว้ใน Look-Up Tables (LUTs)
<br><img alt="img/gate_and.png" src="coursecontent/img/gate_and.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="img/gate_or.png" src="coursecontent/img/gate_or.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="img/gate_not.png" src="coursecontent/img/gate_not.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="img/gate_nand.png" src="coursecontent/img/gate_nand.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="img/gate_nor.png" src="coursecontent/img/gate_nor.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="img/gate_xor.png" src="coursecontent/img/gate_xor.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="img/gate_xnor.png" src="coursecontent/img/gate_xnor.png" target="_self" style="width: 520px; max-width: 100%;">ในการเขียนโค้ด VHDL หรือ Verilog เครื่องมือ Synthesis Tool (เช่น Xilinx Vivado หรือ Intel Quartus) จะไม่ได้ต่อเกตจริงทีละตัว แต่จะทำการ แมปตารางความจริง (Truth Table) ของเกตหรือฟังก์ชันตรรกะทั้งหมด ลงในหน่วยความจำ SRAM cells ของ Look-Up Table (LUT) Edit
LUT4: สามารถรองรับฟังก์ชันตรรกะของอินพุต 4 บิตใดๆ ก็ได้ (จัดเก็บผลลัพธ์ 16 บิตใน SRAM)
LUT6: รองรับฟังก์ชันตรรกะของอินพุต 6 บิตใดๆ ก็ได้ (จัดเก็บผลลัพธ์ 64 บิตใน SRAM)
ข้อดีของ FPGA: ไม่ว่าสมการจะซับซ้อนแค่ไหน (เช่น AND, OR, XOR ต่อรวมกันหลายชั้น) ขอเพียงมีอินพุตไม่เกินจำนวนพินของ LUT ตัว FPGA จะประมวลผลผ่าน LUT เพียง 1 propagation delay เท่ากันเสมอ
การออกแบบวงจรดิจิทัลบน FPGA แบ่งประเภทสถาปัตยกรรมวงจรออกเป็น 2 ประเภทหลัก ได้แก่ Combinational Logic (ไร้หน่วยความจำ) และ Sequential Logic (มีหน่วยความจำและใช้นาฬิกา)Editตัวชี้วัดที่แท้จริงของ Sequential Logic คือ "หน่วยความจำ/สถานะ" ไม่ใช่ "Clock"
ตาราง Table 3-1 ข้างบนสรุปแบบทั่วไปสำหรับกรณีที่พบบ่อยที่สุดใน FPGA (Flip-Flop แบบ Edge-Triggered) แต่ในทางนิยามที่แท้จริง นิยามของ Sequential Logic คือ "Output ขึ้นอยู่กับทั้ง Input ปัจจุบัน และสถานะ (State) ที่จำไว้จากอดีต" — Clock เป็นเพียง กลไกหนึ่ง ที่ใช้ควบคุมจังหวะการจำค่าใหม่เท่านั้น ไม่ใช่เงื่อนไขบังคับ <br><img src="coursecontent/img/logic_type.png" target="_self">Combinational Logic คือวงจรที่เอาต์พุตขึ้นอยู่กับ ค่าอินพุตในขณะนั้นทันที (ไม่มีสภาวะความจำ/ไม่ต้องใช้ Clock)Edit<br><img src="coursecontent/img/combinational_logic.png" target="_self">
Multiplexer (MUX): สวิตช์เลือกสัญญาณอินพุต 1 จากหลายทางมาออกเอาต์พุต 1 ทาง ตามพิน Select
Demultiplexer (DEMUX): วงจรตรงข้าม MUX กระจายอินพุต 1 ช่องไปยังเอาต์พุตหลายช่อง
Decoder: แปลงรหัสอินพุตขนาด บิตเป็นเอาต์พุตขนาด บิต (เช่น 2-to-4 Line Decoder)
Encoder &amp; Priority Encoder: แปลงสัญญาณหลายช่องให้เป็นรหัสขนาด บิต โดย Priority Encoder จะให้ความสำคัญกับช่องสัญญาณสูงสุดก่อนเสมอ
Binary Adder &amp; Subtractor: วงจรบวกและลบเลขฐานสอง (Half Adder, Full Adder, Ripple Carry Adder, Carry Lookahead Adder)
-- MUX 4 ออก 1 ใช้รูปแบบ Conditional Signal Assignment (when-else)
o_y &lt;= i_a when i_sel = "00" else i_b when i_sel = "01" else i_c when i_sel = "10" else i_d; -- กรณีอื่นๆ (i_sel = "11")
-- แปลงรหัส 2 บิตเป็นสัญญาณ Active-High 4 ช่อง
process(i_sel)
begin case i_sel is when "00" =&gt; o_dec &lt;= "0001"; when "01" =&gt; o_dec &lt;= "0010"; when "10" =&gt; o_dec &lt;= "0100"; when others =&gt; o_dec &lt;= "1000"; -- "11" end case;
end process;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL; -- วงจรบวกเลขฐานสองขนาด 4 บิต
o_sum &lt;= std_logic_vector(unsigned(i_a) + unsigned(i_b));
Sequential Logic คือวงจรที่มี สถานะความจำ (Memory State) โดยเอาต์พุตจะขึ้นอยู่กับทั้ง อินพุตปัจจุบัน และ สถานะเดิมในอดีต ผ่านจังหวะสัญญาณนาฬิกา (Clock)ตามหลักทฤษฎี Digital Logic Design ส่วนประกอบพื้นฐานที่แท้จริง (Primitive Building Block) ของ Sequential Logic คือหน่วยความจำ 1 บิตที่เล็กที่สุด ได้แก่ D Flip-Flop (Data Flip-Flop) — เป็น Flip-Flop หลักที่ใช้ใน FPGA โดยข้อมูลอินพุต จะถูกส่งผ่านไปยังเอาต์พุต เฉพาะที่ขอบขาขึ้น (Rising Edge) ของ Clock เท่านั้น (Edge-Triggered)วงจรกลุ่มนี้ล้วนเป็นการนำ D Flip-Flop (จากหัวข้อ 3.4.1) หลายตัวมาต่อกันในรูปแบบต่างๆ:
Registers: กลุ่มของ Flip-Flop หลายๆ ตัวที่นำมาต่อขนานกันเพื่อเก็บบันทึกชุดข้อมูลหลายบิต (เช่น 8-bit Register, 32-bit Register)
Shift Registers: นำ D Flip-Flop มาต่ออนุกรมกันเพื่อเลื่อนบิตข้อมูลไปทางซ้ายหรือขวาตามรอบ Clock (SISO, SIPO, PISO, PIPO)
Counters: วงจรนับค่า (Binary Counter และ Gray Code Counter สำหรับ Async FIFO)
process(clk, rst_n)
begin if rst_n = '0' then s_shift_reg &lt;= (others =&gt; '0'); elsif rising_edge(clk) then s_shift_reg &lt;= s_shift_reg(6 downto 0) &amp; i_din; end if;
end process; o_parallel_data &lt;= s_shift_reg;
process(clk, rst_n)
begin if rst_n = '0' then s_cnt &lt;= (others =&gt; '0'); elsif rising_edge(clk) then if i_up_down = '1' then s_cnt &lt;= s_cnt + 1; -- นับขึ้น else s_cnt &lt;= s_cnt - 1; -- นับลง end if; end if;
end process; o_count &lt;= std_logic_vector(s_cnt);
Latch เป็นวงจร Sequential ที่ Transparent ตามระดับสัญญาณ (Level-Sensitive) ต่างจาก Flip-Flop ที่เป็น Edge-Sensitive FPGA Design ควรหลีกเลี่ยงการใช้ Latch โดยเด็ดขาด เพราะ:
FPGA ไม่มี Latch ฮาร์ดแวร์สำเร็จรูป (มีแต่ FF ฝังมาให้ฟรีในทุก Slice/CLB) เมื่อโค้ดเผลอสั่งให้เกิด Latch เครื่องมือ Synthesis ต้อง "ปลอมสร้าง" ขึ้นเองจาก LUT + Feedback Loop ผ่าน Fabric Routing ทั่วไป ทำให้กินทรัพยากรเกินจำเป็น
Combinational Loop แบบนี้ Static Timing Analysis (STA) วิเคราะห์ได้ยาก เสี่ยงเกิด Glitch/Race Condition
Tool มักแจ้ง Warning: Latch inferred ซึ่งเกือบทุกครั้งคือสัญญาณว่าโค้ดเขียนผิดพลาด ไม่ใช่ตั้งใจ
สาเหตุที่พบบ่อยที่สุด: เขียน process แบบ Combinational (ไม่มี Clock ใน Sensitivity List) แต่ กำหนดค่า Output ไม่ครบทุกเงื่อนไข (Incomplete Assignment) ทำให้ Synth ต้อง "จำ" ค่าเดิมไว้ ซึ่งพฤติกรรมนี้คือคุณสมบัติของ Latch<br><img alt="img/slice-clb-3paths.svg" src="coursecontent/img/slice-clb-3paths.svg" target="_self" style="width: 811px; max-width: 100%;">กฎจำง่ายๆ: Combinational Process (ไม่มี Clock) ต้องกำหนดค่า Output ให้ครบทุกเงื่อนไขเสมอ (Path B) — ถ้าลืมจะกลายเป็น Path C (Latch) ทันที✅ (Path A) — มี Clock Edge ชัดเจน ใช้ FF จริง:process(clk)
begin if rising_edge(clk) then y &lt;= a; end if;
end process;
✅ (Path B) — เลือกวิธีใดวิธีหนึ่ง ให้ผลลัพธ์วงจรเหมือนกัน:-- วิธีที่ 1: Default assignment ก่อนเสมอ
process(sel, a, b)
begin y &lt;= b; if sel = '1' then y &lt;= a; end if;
end process;
-- วิธีที่ 2: else ปิดให้ครบทุกเงื่อนไข
process(sel, a, b)
begin if sel = '1' then y &lt;= a; else y &lt;= b; end if;
end process;
❌ (Path C) — เผลอสร้าง Latch ไม่ควรทำ-- ผิด (Path C — เผลอสร้าง Latch)
process(sel, a, b)
begin if sel = '1' then y &lt;= a; end if; -- ไม่มี else กรณี sel = '0' -&gt; Synth ต้องจำค่า y เดิมไว้ -&gt; Infer Latch
end process;
สัญญาณนาฬิกาและรีเซ็ตถือเป็น หัวใจหลักของการควบคุมระบบ Synchronous System ทั้งหมดใน FPGA
Clock Signal: สัญญาณนาฬิกากำหนดจังหวะการทำงานของระบบ โดยใน FPGA ต้องส่งผ่าน Global Clock Trees เพื่อให้สัญญาณกระจายถึง Flip-Flop ทุกตัวด้วยค่า Clock Skew ต่ำที่สุด
Clock Skew: ความเหลื่อมเวลาของสัญญาณนาฬิกาที่เดินทางไปถึง Flip-Flop แต่ละตัว<br>
<img src="coursecontent/img/clock-skew.png" target="_self"> Clock Domain: ขอบเขตสัญญาณนาฬิกา สัญญาณที่เดินทางข้าม Clock Domain ต่างความถี่ เรียกว่า CDC (Clock Domain Crossing) ต้องใช้ Synchronizer หรือ FIFO กั้นเพื่อป้องกันข้อมูลผิดพลาด
เมื่อสัญญาณ 1 บิต (เช่น Flag หรือ Status bit) ต้องเดินทางข้าม Clock Domain การป้อนสัญญาณเข้า Flip-Flop ปลายทางตรงๆ มีความเสี่ยงสูงที่จะทำให้เกิด Metastability (ตามที่กล่าวในหัวข้อ 7.2) เนื่องจากสัญญาณอาจเปลี่ยนค่าในช่วง Setup/Hold Window ของ Flip-Flop ปลายทางพอดีวิธีแก้ที่นิยมและง่ายที่สุดคือ 2-Flip-Flop Synchronizer: นำสัญญาณผ่าน Flip-Flop 2 ตัวเรียงต่อกันในโดเมน Clock ปลายทาง เพื่อให้เวลา (1 Clock Period) สำหรับสถานะ Metastable ที่อาจเกิดขึ้นที่ FF ตัวแรกได้ "สงบ" ลงก่อนที่จะถูกอ่านค่าโดย FF ตัวที่สอง-- 2-FF Synchronizer: ซิงค์สัญญาณ 1 บิต (i_async_signal) จาก Clock Domain อื่น
-- เข้าสู่ Clock Domain ปลายทางที่ขับด้วย clk_dst
process(clk_dst, rst_n)
begin if rst_n = '0' then s_sync_ff1 &lt;= '0'; s_sync_ff2 &lt;= '0'; elsif rising_edge(clk_dst) then s_sync_ff1 &lt;= i_async_signal; -- FF ตัวแรก: จุดเสี่ยง Metastability s_sync_ff2 &lt;= s_sync_ff1; -- FF ตัวที่สอง: ให้เวลา Settle ก่อนใช้งานจริง end if;
end process; o_sync_signal &lt;= s_sync_ff2; -- นำสัญญาณที่ Sync แล้วไปใช้งานต่อในโดเมนนี้
ข้อจำกัดสำคัญ: 2-FF Synchronizer ใช้ได้เฉพาะสัญญาณ 1 บิต หรือสัญญาณที่เปลี่ยนค่าไม่พร้อมกันได้ (เช่น Single-bit Pulse/Flag) เท่านั้น ห้ามใช้กับบัสข้อมูลหลายบิต (เช่น Counter value หรือ Bus data) โดยตรง เพราะแต่ละบิตอาจ Synchronize เสร็จไม่พร้อมกัน ทำให้ค่าที่อ่านได้ผิดเพี้ยน (Bit Skew) — กรณีข้อมูลหลายบิตข้าม Clock Domain ต้องใช้ Async FIFO หรือเทคนิค Handshake แทน
ใช้สำหรับกำหนดสถานะเริ่มต้น (Initial State) ให้กับ Flip-Flop ตอนเปิดเครื่องหรือรีสตาร์ทระบบ:
Active High (rst): ทำงานเมื่อสัญญาณเป็น '1'
Active Low (rst_n): ทำงานเมื่อสัญญาณเป็น '0' (นิยมใช้เป็นมาตรฐานอุตสาหกรรม)
process(clk)
begin if rising_edge(clk) then if rst_n = '0' then -- เช็ค Reset ตามจังหวะ Clock q_out &lt;= (others =&gt; '0'); else q_out &lt;= d_in; end if; end if;
end process;
process(clk, rst_n) -- ใส่ rst_n เข้า Sensitivity List
begin if rst_n = '0' then -- รีเซ็ตทันทีเมื่อ rst_n เป็น '0' q_out &lt;= (others =&gt; '0'); elsif rising_edge(clk) then q_out &lt;= d_in; end if;
end process;
FSM คือรูปแบบการออกแบบ Sequential Logic ที่ได้รับความนิยมสูงสุดใน FPGA สำหรับควบคุมลำดับการทำงานและสถิติต่างๆ (Control Logic &amp; Sequencer)<br><img src="coursecontent/img/moore.png" target="_self"><br><img src="coursecontent/img/mealy.png" target="_self">library IEEE;
use IEEE.STD_LOGIC_1164.ALL; entity traffic_light_fsm is port ( clk : in std_logic; rst_n : in std_logic; o_red : out std_logic; o_yellow : out std_logic; o_green : out std_logic );
end entity; architecture rtl of traffic_light_fsm is -- นิยาม State ทั้งหมดด้วย Enumerated Type (Synthesis Tool จะเลือก State Encoding ให้อัตโนมัติ) type state_type is (S_RED, S_GREEN, S_YELLOW); signal s_current_state, s_next_state : state_type;
begin -- Process 1: State Register (Sequential) - เก็บ State ตามจังหวะ Clock p_state_reg : process(clk, rst_n) begin if rst_n = '0' then s_current_state &lt;= S_RED; elsif rising_edge(clk) then s_current_state &lt;= s_next_state; end if; end process; -- Process 2: Next-State Logic (Combinational) - คำนวณ State ถัดไป p_next_state : process(s_current_state) begin case s_current_state is when S_RED =&gt; s_next_state &lt;= S_GREEN; when S_GREEN =&gt; s_next_state &lt;= S_YELLOW; when S_YELLOW =&gt; s_next_state &lt;= S_RED; when others =&gt; s_next_state &lt;= S_RED; -- Safe default กัน Latch/Unknown State end case; end process; -- Process 3: Output Logic (Combinational, Moore-style) - ขึ้นกับ State ปัจจุบันเท่านั้น p_output_logic : process(s_current_state) begin o_red &lt;= '0'; o_yellow &lt;= '0'; o_green &lt;= '0'; case s_current_state is when S_RED =&gt; o_red &lt;= '1'; when S_GREEN =&gt; o_green &lt;= '1'; when S_YELLOW =&gt; o_yellow &lt;= '1'; end case; end process; end architecture;
สิ่งสำคัญที่สุดในการออกแบบฮาร์ดแวร์ความเร็วสูงคือ Timing Closure (สัญญาณเดินทางทันรอบนาฬิกา) และการอ่าน Timing Diagram เพื่อวิเคราะห์พฤติกรรมวงจรTiming Diagram ใช้แสดงความสัมพันธ์ของสัญญาณไฟฟ้าระหว่างอินพุต เอาต์พุต และ Clock ตามแกนเวลา (Time Axis)ClockData_InShift_Reg0x000x010x020x040x08Valid_Out
Clock Period (): ระยะเวลา 1 รอบของสัญญาณนาฬิกา
Propagation Delay (): ความหน่วงเวลารอคอยตั้งแต่เปลี่ยนอินพุตจนเอาต์พุตตอบสนอง
Setup Window: ช่วงเวลาก่อนขอบ Clock ที่อินพุตต้องนิ่ง
ในการออกแบบระบบบน FPGA ที่เชื่อมต่อกับ Microprocessor (เช่น ARM Cortex ใน Zynq) มาตรฐาน AXI4-Lite ถือเป็นบัสโปรโตคอลมาตรฐานที่นิยมใช้แพร่หลายที่สุด การส่งข้อมูลใน AXI4-Lite อาศัย หลักการ Handshake (VALID / READY)AXI4-Lite Write Transaction Waveform0123456789ACLKAWADDR0x4000_0000AWVALIDAWREADYWDATA0xDEADBEEFWVALIDWREADYBVALIDBREADYWrite Address (AW)Write Data (W)Write Response (B)กฎทองของการ Handshake ใน AXI Protocol:
"การรับส่งข้อมูลจะเกิดขึ้นสำเร็จ (Transfer Done) เฉพาะในรอบขอบขาขึ้นของ ACLK ที่สัญญาณ VALID = 1 และ READY = 1 พร้อมกันเท่านั้น" VALID (สร้างโดยฝั่งส่ง): บ่งบอกว่าข้อมูล/ที่อยู่บนบัสมีความถูกต้องและพร้อมส่งแล้ว
READY (สร้างโดยฝั่งรับ): บ่งบอกว่าปลายทางพร้อมรับข้อมูลเข้าสู่ภายในแล้ว
หาก VALID = 1 แต่ READY = 0 ฝั่งส่งจะต้อง ค้างค่าข้อมูลไว้บนบัส (Wait State) จนกว่าฝั่งรับจะยก READY = 1 ตอบกลับมา Write Address Channel (AW): ส่งตำแหน่ง Address ที่ต้องการเขียน (AWADDR, AWVALID, AWREADY)
Write Data Channel (W): ส่งข้อมูล Data ที่ต้องการเขียน (WDATA, WSTRB, WVALID, WREADY)
Write Response Channel (B): รายงานสถานะการเขียนกลับไปยัง Master (BRESP, BVALID, BREADY)
Read Address Channel (AR): ส่งตำแหน่ง Address ที่ต้องการอ่าน (ARADDR, ARVALID, ARREADY)
Read Data Channel (R): ส่งข้อมูลที่อ่านได้กลับไปยัง Master (RDATA, RRESP, RVALID, RREADY)
Clock : __/‾‾\__/‾‾\__/‾‾\__/‾‾\__
Data Input: ====&lt; Data Valid &gt;==== |&lt;-t_su-&gt;|&lt;-t_h-&gt;| Setup Time (): ระยะเวลาขั้นต่ำที่ข้อมูลอินพุตจะต้อง คงที่และนิ่ง ก่อนที่ขอบสัญญาณนาฬิกา (Clock Edge) จะมาถึง
Hold Time (): ระยะเวลาขั้นต่ำที่ข้อมูลอินพุตจะต้อง คงค้างไว้ หลังจากขอบสัญญาณนาฬิกาผ่านไปแล้ว
Metastability (สภาวะไม่เสถียร): หากข้อมูลเปลี่ยนในช่วงเวลา หรือ เอาต์พุตของ Flip-Flop จะตกอยู่ในสถานะกึ่งกลางระหว่าง 0 และ 1 (Uncertain state) ส่งผลให้ระบบล่มหรือทำงานผิดพลาดได้ Timing Constraints File (.sdc / .xdc): ไฟล์บอก EDA Tool ว่า Clock ในระบบมีความถี่เท่าไร (เช่น 100 MHz = 10 ns Period)
Data Path Delay (): เวลาที่สัญญาณเดินทางจาก Flip-Flop ต้นทาง ผ่าน LUTs ไปยัง Flip-Flop ปลายทาง
Slack (ระยะเวลาคงเหลือ): Positive Slack (): สัญญาณเดินทางทันรอบเวลา (Timing PASSED)
Negative Slack (): สัญญาณเดินทางไม่ทัน (Timing VIOLATED) ระบบจะทำงานผิดพลาดที่ความแน่นั้น ต้องลดความถี่ Clock หรือปรับแก้โค้ด ]]></description><link>coursecontent/chapter2-fundamental-of-digital-logic.html</link><guid isPermaLink="false">CourseContent/chapter2-fundamental-of-digital-logic.md</guid><pubDate>Mon, 05 Oct 2026 04:31:48 GMT</pubDate><enclosure url="coursecontent/img/complement.png" length="0" type="image/png"/><content:encoded>&lt;figure&gt;&lt;img src="coursecontent/img/complement.png"&gt;&lt;/figure&gt;</content:encoded></item><item><title><![CDATA[chapter1-introduction-to-fpga]]></title><description><![CDATA[
<a data-tooltip-position="top" aria-label="1 Basic FPGA Overview &amp; Concepts" data-href="#1 Basic FPGA Overview &amp; Concepts" href="coursecontent/chapter1-introduction-to-fpga.html#1_Basic_FPGA_Overview_&amp;_Concepts_0" class="internal-link" target="_self" rel="noopener nofollow">1 Basic FPGA Overview &amp; Concepts</a> <br><a data-tooltip-position="top" aria-label="1.1 FPGA คืออะไร? (What is an FPGA?)" data-href="#1.1 FPGA คืออะไร? (What is an FPGA?)" href="coursecontent/chapter1-introduction-to-fpga.html#1.1_FPGA_คืออะไร?_(What_is_an_FPGA" class="internal-link" target="_self" rel="noopener nofollow">1.1 FPGA คืออะไร? (What is an FPGA?)</a> <br><a data-tooltip-position="top" aria-label="1.1.1 คำศัพท์:" data-href="#1.1.1 คำศัพท์:" href="coursecontent/chapter1-introduction-to-fpga.html#1.1.1_คำศัพท์_0" class="internal-link" target="_self" rel="noopener nofollow">1.1.1 คำศัพท์:</a> <br><a data-tooltip-position="top" aria-label="1.2 FPGA เหมาะกับงานอะไร? (FPGA Use Cases &amp; Comparison)" data-href="#1.2 FPGA เหมาะกับงานอะไร? (FPGA Use Cases &amp; Comparison)" href="coursecontent/chapter1-introduction-to-fpga.html#1.2_FPGA_เหมาะกับงานอะไร?_(FPGA_Use_Cases_&amp;_Comparison)_0" class="internal-link" target="_self" rel="noopener nofollow">1.2 FPGA เหมาะกับงานอะไร? (FPGA Use Cases &amp; Comparison)</a> <br><a data-tooltip-position="top" aria-label="1.2.1 ตารางเปรียบเทียบ FPGA vs MCU vs CPU/GPU vs ASIC" data-href="#1.2.1 ตารางเปรียบเทียบ FPGA vs MCU vs CPU/GPU vs ASIC" href="coursecontent/chapter1-introduction-to-fpga.html#1.2.1_ตารางเปรียบเทียบ_FPGA_vs_MCU_vs_CPU/GPU_vs_ASIC_0" class="internal-link" target="_self" rel="noopener nofollow">1.2.1 ตารางเปรียบเทียบ FPGA vs MCU vs CPU/GPU vs ASIC</a>
<br><a data-tooltip-position="top" aria-label="1.2.2 สรุปจุดเด่นและการเลือกใช้งาน:" data-href="#1.2.2 สรุปจุดเด่นและการเลือกใช้งาน:" href="coursecontent/chapter1-introduction-to-fpga.html#1.2.2_สรุปจุดเด่นและการเลือกใช้งาน_0" class="internal-link" target="_self" rel="noopener nofollow">1.2.2 สรุปจุดเด่นและการเลือกใช้งาน:</a> <br><a data-tooltip-position="top" aria-label="1.3 เทคโนโลยีของ FPGA (FPGA Technologies &amp; Architectures)" data-href="#1.3 เทคโนโลยีของ FPGA (FPGA Technologies &amp; Architectures)" href="coursecontent/chapter1-introduction-to-fpga.html#1.3_เทคโนโลยีของ_FPGA_(FPGA_Technologies_&amp;_Architectures)_0" class="internal-link" target="_self" rel="noopener nofollow">1.3 เทคโนโลยีของ FPGA (FPGA Technologies &amp; Architectures)</a>
<br><a data-tooltip-position="top" aria-label="1.4 การแบ่งระดับและตระกูลของ FPGA ในปัจจุบัน (FPGA Product Tiering)" data-href="#1.4 การแบ่งระดับและตระกูลของ FPGA ในปัจจุบัน (FPGA Product Tiering)" href="coursecontent/chapter1-introduction-to-fpga.html#1.4_การแบ่งระดับและตระกูลของ_FPGA_ในปัจจุบัน_(FPGA_Product_Tiering)_0" class="internal-link" target="_self" rel="noopener nofollow">1.4 การแบ่งระดับและตระกูลของ FPGA ในปัจจุบัน (FPGA Product Tiering)</a> <br><a data-tooltip-position="top" aria-label="1.4.1 FPGA (Only) Series" data-href="#1.4.1 FPGA (Only) Series" href="coursecontent/chapter1-introduction-to-fpga.html#1.4.1_FPGA_(Only)_Series_0" class="internal-link" target="_self" rel="noopener nofollow">1.4.1 FPGA (Only) Series</a>
<br><a data-tooltip-position="top" aria-label="1.4.2 SoC FPGA Series (FPGA ที่รวม CPU Hard Processor System ไว้ในชิปเดียว)" data-href="#1.4.2 SoC FPGA Series (FPGA ที่รวม CPU Hard Processor System ไว้ในชิปเดียว)" href="coursecontent/chapter1-introduction-to-fpga.html#1.4.2_SoC_FPGA_Series_(FPGA_ที่รวม_CPU_Hard_Processor_System_ไว้ในชิปเดียว)_0" class="internal-link" target="_self" rel="noopener nofollow">1.4.2 SoC FPGA Series (FPGA ที่รวม CPU Hard Processor System ไว้ในชิปเดียว)</a> <br><a data-tooltip-position="top" aria-label="2 FPGA Internal Architecture &amp; Resources" data-href="#2 FPGA Internal Architecture &amp; Resources" href="coursecontent/chapter1-introduction-to-fpga.html#2_FPGA_Internal_Architecture_&amp;_Resources_0" class="internal-link" target="_self" rel="noopener nofollow">2 FPGA Internal Architecture &amp; Resources</a> <br><a data-tooltip-position="top" aria-label="2.2 องค์ประกอบพื้นฐาน: LUT &amp; Flip-Flop" data-href="#2.2 องค์ประกอบพื้นฐาน: LUT &amp; Flip-Flop" href="coursecontent/chapter1-introduction-to-fpga.html#2.2_องค์ประกอบพื้นฐาน_LUT_&amp;_Flip-Flop_0" class="internal-link" target="_self" rel="noopener nofollow">2.2 องค์ประกอบพื้นฐาน: LUT &amp; Flip-Flop</a> <br><a data-tooltip-position="top" aria-label="2.2.1 Look-Up Table (LUT)" data-href="#2.2.1 Look-Up Table (LUT)" href="coursecontent/chapter1-introduction-to-fpga.html#2.2.1_Look-Up_Table_(LUT)_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.1 Look-Up Table (LUT)</a>
<br><a data-tooltip-position="top" aria-label="2.2.2 Flip-Flop (FF) / Registers" data-href="#2.2.2 Flip-Flop (FF) / Registers" href="coursecontent/chapter1-introduction-to-fpga.html#2.2.2_Flip-Flop_(FF)_/_Registers_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.2 Flip-Flop (FF) / Registers</a> <br><a data-tooltip-position="top" aria-label="2.3 หน่วยความจำภายใน (Block RAM)" data-href="#2.3 หน่วยความจำภายใน (Block RAM)" href="coursecontent/chapter1-introduction-to-fpga.html#2.3_หน่วยความจำภายใน_(Block_RAM)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3 หน่วยความจำภายใน (Block RAM)</a> <br><a data-tooltip-position="top" aria-label="2.3.2 ตารางเปรียบเทียบขนาด BRAM ใน FPGA แต่ละรุ่น (โดยประมาณ)" data-href="#2.3.2 ตารางเปรียบเทียบขนาด BRAM ใน FPGA แต่ละรุ่น (โดยประมาณ)" href="coursecontent/chapter1-introduction-to-fpga.html#2.3.2_ตารางเปรียบเทียบขนาด_BRAM_ใน_FPGA_แต่ละรุ่น_(โดยประมาณ)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3.2 ตารางเปรียบเทียบขนาด BRAM ใน FPGA แต่ละรุ่น (โดยประมาณ)</a>
<br><a data-tooltip-position="top" aria-label="2.3.3 การเลือกใช้งาน BRAM และ DDR (Application Use-cases)" data-href="#2.3.3 การเลือกใช้งาน BRAM และ DDR (Application Use-cases)" href="coursecontent/chapter1-introduction-to-fpga.html#2.3.3_การเลือกใช้งาน_BRAM_และ_DDR_(Application_Use-cases)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3.3 การเลือกใช้งาน BRAM และ DDR (Application Use-cases)</a> <br><a data-tooltip-position="top" aria-label="2.4 บล็อกประมวลผลคณิตศาสตร์ (DSP Blocks)" data-href="#2.4 บล็อกประมวลผลคณิตศาสตร์ (DSP Blocks)" href="coursecontent/chapter1-introduction-to-fpga.html#2.4_บล็อกประมวลผลคณิตศาสตร์_(DSP_Blocks)_0" class="internal-link" target="_self" rel="noopener nofollow">2.4 บล็อกประมวลผลคณิตศาสตร์ (DSP Blocks)</a>
<br><a data-tooltip-position="top" aria-label="2.5 ระบบสัญญาณนาฬิกา (PLL &amp; Clock Network)" data-href="#2.5 ระบบสัญญาณนาฬิกา (PLL &amp; Clock Network)" href="coursecontent/chapter1-introduction-to-fpga.html#2.5_ระบบสัญญาณนาฬิกา_(PLL_&amp;_Clock_Network)_0" class="internal-link" target="_self" rel="noopener nofollow">2.5 ระบบสัญญาณนาฬิกา (PLL &amp; Clock Network)</a>
<br><a data-tooltip-position="top" aria-label="2.6 บล็อกอินพุตและเอาต์พุต (I/O Banks &amp; IO Voltage Standards)" data-href="#2.6 บล็อกอินพุตและเอาต์พุต (I/O Banks &amp; IO Voltage Standards)" href="coursecontent/chapter1-introduction-to-fpga.html#2.6_บล็อกอินพุตและเอาต์พุต_(I/O_Banks_&amp;_IO_Voltage_Standards)_0" class="internal-link" target="_self" rel="noopener nofollow">2.6 บล็อกอินพุตและเอาต์พุต (I/O Banks &amp; IO Voltage Standards)</a> <br><a data-tooltip-position="top" aria-label="2.6.2 มาตรฐานแรงดัน I/O ทั่วไปและการประยุกต์ใช้งาน (I/O Standards &amp; Target Applications)" data-href="#2.6.2 มาตรฐานแรงดัน I/O ทั่วไปและการประยุกต์ใช้งาน (I/O Standards &amp; Target Applications)" href="coursecontent/chapter1-introduction-to-fpga.html#2.6.2_มาตรฐานแรงดัน_I/O_ทั่วไปและการประยุกต์ใช้งาน_(I/O_Standards_&amp;_Target_Applications)_0" class="internal-link" target="_self" rel="noopener nofollow">2.6.2 มาตรฐานแรงดัน I/O ทั่วไปและการประยุกต์ใช้งาน (I/O Standards &amp; Target Applications)</a>
<br><a data-tooltip-position="top" aria-label="2.6.3 รายละเอียดสเปก I/O Banks และแอปพลิเคชันแยกตามตระกูลชิปยอดนิยม" data-href="#2.6.3 รายละเอียดสเปก I/O Banks และแอปพลิเคชันแยกตามตระกูลชิปยอดนิยม" href="coursecontent/chapter1-introduction-to-fpga.html#2.6.3_รายละเอียดสเปก_I/O_Banks_และแอปพลิเคชันแยกตามตระกูลชิปยอดนิยม_0" class="internal-link" target="_self" rel="noopener nofollow">2.6.3 รายละเอียดสเปก I/O Banks และแอปพลิเคชันแยกตามตระกูลชิปยอดนิยม</a>
<br><a data-tooltip-position="top" aria-label="2.6.4 พินฟังก์ชันพิเศษเฉพาะทางบน FPGA (Special Function &amp; Dedicated Pins)" data-href="#2.6.4 พินฟังก์ชันพิเศษเฉพาะทางบน FPGA (Special Function &amp; Dedicated Pins)" href="coursecontent/chapter1-introduction-to-fpga.html#2.6.4_พินฟังก์ชันพิเศษเฉพาะทางบน_FPGA_(Special_Function_&amp;_Dedicated_Pins)_0" class="internal-link" target="_self" rel="noopener nofollow">2.6.4 พินฟังก์ชันพิเศษเฉพาะทางบน FPGA (Special Function &amp; Dedicated Pins)</a>
<br><a data-tooltip-position="top" aria-label="2.6.5 สรุปข้อควรระวังสำคัญในการออกแบบฮาร์ดแวร์ (Hardware Design Pitfalls)" data-href="#2.6.5 สรุปข้อควรระวังสำคัญในการออกแบบฮาร์ดแวร์ (Hardware Design Pitfalls)" href="coursecontent/chapter1-introduction-to-fpga.html#2.6.5_สรุปข้อควรระวังสำคัญในการออกแบบฮาร์ดแวร์_(Hardware_Design_Pitfalls)_0" class="internal-link" target="_self" rel="noopener nofollow">2.6.5 สรุปข้อควรระวังสำคัญในการออกแบบฮาร์ดแวร์ (Hardware Design Pitfalls)</a> <br><a data-tooltip-position="top" aria-label="2.7 วงจรคอนฟิกูเรชัน (Configuration Circuits &amp; Modes)" data-href="#2.7 วงจรคอนฟิกูเรชัน (Configuration Circuits &amp; Modes)" href="coursecontent/chapter1-introduction-to-fpga.html#2.7_วงจรคอนฟิกูเรชัน_(Configuration_Circuits_&amp;_Modes)_0" class="internal-link" target="_self" rel="noopener nofollow">2.7 วงจรคอนฟิกูเรชัน (Configuration Circuits &amp; Modes)</a>
<br><a data-tooltip-position="top" aria-label="2.8 ทรัพยากรฟังก์ชันพิเศษฮาร์ดแวร์ (Special Hard IP Functions)" data-href="#2.8 ทรัพยากรฟังก์ชันพิเศษฮาร์ดแวร์ (Special Hard IP Functions)" href="coursecontent/chapter1-introduction-to-fpga.html#2.8_ทรัพยากรฟังก์ชันพิเศษฮาร์ดแวร์_(Special_Hard_IP_Functions)_0" class="internal-link" target="_self" rel="noopener nofollow">2.8 ทรัพยากรฟังก์ชันพิเศษฮาร์ดแวร์ (Special Hard IP Functions)</a> <br><a data-tooltip-position="top" aria-label="3 FPGA Design Flow" data-href="#3 FPGA Design Flow" href="coursecontent/chapter1-introduction-to-fpga.html#3_FPGA_Design_Flow_0" class="internal-link" target="_self" rel="noopener nofollow">3 FPGA Design Flow</a><br>
- <a data-tooltip-position="top" aria-label="3.1.2 รายละเอียดแต่ละขั้นตอน:" data-href="#3.1.2 รายละเอียดแต่ละขั้นตอน:" href="coursecontent/chapter1-introduction-to-fpga.html#3.1.2_รายละเอียดแต่ละขั้นตอน_0" class="internal-link" target="_self" rel="noopener nofollow">3.1.2 รายละเอียดแต่ละขั้นตอน:</a>
<br><a data-tooltip-position="top" aria-label="4 EDA Tools Landscape &amp; Ecosystem" data-href="#4 EDA Tools Landscape &amp; Ecosystem" href="coursecontent/chapter1-introduction-to-fpga.html#4_EDA_Tools_Landscape_&amp;_Ecosystem_0" class="internal-link" target="_self" rel="noopener nofollow">4 EDA Tools Landscape &amp; Ecosystem</a>
FPGA ย่อมาจาก Field Programmable Gate Array คือ ชิปวงจรรวม (Integrated Circuit: IC) ชนิดหนึ่งที่โครงสร้างฮาร์ดแวร์ภายในสามารถถูกโปรแกรมหรือกำหนดค่าใหม่ (Reconfigure) ได้โดยผู้ใช้งานหลังจากผลิตมาจากโรงงานแล้ว ("In the Field") ต่างจาก ASIC (Application-Specific Integrated Circuit) หรือชิปเฉพาะทาง (เช่น ชิปประมวลผลผลใน iPhone หรือการ์ดจอ) ที่โครงสร้างวงจรถูกสร้างตายตัวมาจากโรงงาน ไม่สามารถแก้ไขได้อีก
Field Programmable: สามารถโปรแกรมแก้ไขโครงสร้างฮาร์ดแวร์ได้หลังจากผลิตมาแล้ว
Logic Blocks: บล็อกประมวลผลตรรกะพื้นฐานภายใน FPGA ที่สามารถตั้งค่าให้ทำงานตามตรรกะใดๆ ก็ได้
Interconnects: สายสัญญาณเชื่อมต่อภายในแบบโปรแกรมเส้นทางได้ (Programmable Routing) สำหรับเชื่อมต่อ Logic Blocks แต่ละตัวเข้าด้วยกัน
<br><img alt="img/fpga.png" src="coursecontent/img/fpga.png" target="_self" style="width: 534px; max-width: 100%;">ในการเลือกใช้งานชิปประมวลผลสำหรับระบบฝังตัว (Embedded Systems) หรือระบบประมวลผลประสิทธิภาพสูง เรามักจะเปรียบเทียบระหว่าง MCU (Microcontroller Unit), CPU / GPU, FPGA และ ASIC
MCU: เหมาะกับงานควบคุมทั่วไป (Control Logic), งานที่ต้องการราคาถูก, พัฒนาไว เช่น อ่านเซนเซอร์เปิด-ปิดไฟ
CPU / GPU: เหมาะกับการประมวลผลข้อมูลลำดับเชิงซ้อน (Sequential Execution) หรืองาน Graphic / AI model ใหญ่ๆ ที่ประมวลผลบนซอฟต์แวร์
FPGA: เหมาะกับงานที่ต้องการ True Parallel Processing (การประมวลผลขนานแท้จริงระดับฮาร์ดแวร์), งานที่ต้องการตอบสนองระดับ Microsecond / Nanosecond (Deterministic Real-time), งานประมวลผลสัญญาณดิจิทัล (DSP), Video Processing, High-Frequency Trading, Communication Protocols (5G/SDR) และการสร้าง Prototype สำหรับ ASIC
ASIC: เหมาะกับผลิตภัณฑ์ที่มีปริมาณการผลิตระดับหลายล้านชิ้น (Mass Production) ที่ต้องการประสิทธิภาพสูงสุดและประหยัดพลังงานที่สุด
จำแนกตามเทคโนโลยีการจัดเก็บข้อมูลการคอนฟิก (Configuration Technology):ข้อสังเกตเพิ่มเติมเกี่ยวกับตระกูลชิป: Intel MAX 10: สถาปัตยกรรมภายในเป็น SRAM FPGA แต่มี Internal Flash สำหรับโหลด Configuration อยู่ในชิปตัวเดียวกัน ทำให้มีคุณสมบัติ Instant-on เหมือน Flash FPGA
Lattice iCE40: เป็น SRAM-based FPGA (มักโหลด Configuration จาก SPI Flash ภายนอก) ในขณะที่ MachXO เป็น Flash-based FPGA SoC FPGA ผสานรวม Hard IP Core ของ Processor (เช่น ARM Cortex หรือ RISC-V) เข้ากับ FPGA Logic Fabric ช่วยให้เขียนซอฟต์แวร์ระบบปฏิบัติการ (Linux/RTOS) ควบคู่กับการเร่งความเร็วด้วยฮาร์ดแวร์ (Hardware Acceleration) ได้ในชิปเดียวโครงสร้างทรัพยากรภายในของ FPGA ประกอบไปด้วยองค์ประกอบหลักที่สำคัญ ดังนี้:
Logic Array Block (LUT + FF)
BRAM (Block RAM)
DSP
PLL
Hard IP(PCIe, DDR Ctrl, Transceivers, etc.)<br>
<img src="coursecontent/img/fpga-2.png" target="_self"> LUT คือวงจรความทรงจำขนาดเล็ก (SRAM-based Truth Table) ที่ใช้จำลองวงจรตรรกะแบบ Combinational Logic (เช่น AND, OR, XOR, MUX หรือสมการลอจิกซับซ้อน)
ใน FPGA ยุคปัจจุบัน มักใช้ 4-input LUT หรือ 6-input LUT
LUT 6-input สามารถทำป้อนอินพุตได้ 6 สัญญาณ และสร้างเอาต์พุตตาม Truth Table ที่โปรแกรมไว้ได้ทุกรูปแบบ ใช้สำหรับเก็บบันทึกสถานะข้อมูล (Sequential Logic) ขนานคู่ไปกับ LUT
ทำงานตามจังหวะสัญญาณนาฬิกา (Clock-driven)
หนึ่งบล็อกประมวลผลพื้นฐาน (เรียกว่า Slice ใน Xilinx หรือ ALM ใน Intel) มักประกอบด้วย LUT และ Flip-Flop ทำงานร่วมกัน
Configurable Logic Block : <br><img alt="img/xilinx-CLB.png" src="coursecontent/img/xilinx-clb.png" target="_self" style="width: 480px; max-width: 100%;">Example : zynq-7000 CLB specification<br>
<img alt="img/zynq-7000-CLB.png" src="coursecontent/img/zynq-7000-clb.png" target="_self" style="width: 833px; max-width: 100%;">FPGA มีเตรียมหน่วยความจำในภายในไว้ให้ แต่ขนาดไม่ได้ใหญ่มาก กรณีที่ต้องการใช้ขนาดใหญ่จำเป็นต้องต่อ external memory เอาDSP Block (Digital Signal Processing Block) หรือบางค่ายเรียกว่า MULT / MAC Block
เป็นวงจรฮาร์ดแวร์คูณและบวกสำเร็จรูป (Hardened Multiplier &amp; Accumulator: MAC)
ทำไมต้องมี DSP Block?: การใช้ LUT แบบธรรมดามาสร้างวงจรคูณเลข (Multiplier) จะสิ้นเปลือง LUT จำนวนมากและทำงานได้ช้า ดังนั้น FPGA จึงใส่ชิปคูณฮาร์ดแวร์เฉพาะทางนี้มาให้
งานที่เหมาะกับ DSP: การทำ FIR/IIR Filter, Fast Fourier Transform (FFT), Matrix Multiplication, การประมวลผลภาพ และการคำนวณเวกเตอร์ใน AI/ML Global Clock Network: เป็นสายสัญญาณพิเศษภายใน FPGA ที่ออกแบบมาให้กระจายสัญญาณนาฬิกาไปยัง Flip-Flop ทุกตัวทั่วทั้งชิปด้วยความหน่วง (Skew) ที่ต่ำที่สุด ป้องกันปัญหา Timing Violation<br>
<img src="coursecontent/img/global-clock.png" target="_self"> PLL (Phase-Locked Loop) / MMCM (Mixed-Mode Clock Manager): ทำหน้าที่ปรับเพิ่ม/ลดความถี่สัญญาณนาฬิกา (Frequency Synthesis/Multiplication/Division)
ปรับเปลี่ยนเฟสสัญญาณ (Phase Shift) เพื่อเช็ค Timing และจัดการกับ Clock Jitter
สิ่งสำคัญคือ PLL ไม่ได้เป็น Global Clock Network แต่ เอาต์พุตของ PLL สามารถป้อนเข้า Global Clock Network ได้ พิน I/O ของ FPGA จะถูกจัดกลุ่มแบ่งออกเป็น I/O Banks โดยในแต่ละ Bank สามารถตั้งค่าแรงดันไฟฟ้าจ่ายพอร์ต (VCCIO) แยกอิสระจากกันได้ เพื่อให้อินเทอร์เฟซเชื่อมต่อกับอุปกรณ์ภายนอกต่างแรงดันได้อย่างยืดหยุ่น<br><img alt="img/io-bank.png" src="coursecontent/img/io-bank.png" target="_self" style="width: 609px; max-width: 100%;">มาตรฐานสัญญาณไฟฟ้าแต่ละประเภทถูกออกแบบมาเพื่อรองรับงานเฉพาะทางที่แตกต่างกัน ทั้งด้านความเร็ว การประหยัดพลังงาน และการทนต่อสัญญาณรบกวน:note : เวลาต่อวงจรต้องคำนึงค่าพวกนี้ (Vih, Vil, Voh, Vol) ถ้าไม่ match กันก็จะสื่อสารกันไม่ได้(ค่ามาตรฐานทั่วไปตาม JEDEC LVTTL/LVCMOS Interface Standard — JESD8C.01 (3.3V), JESD8-5A (2.5V), JESD8-7A (1.8V), JESD8-11A (1.5V), JESD8-12A (1.2V) ตัวเลขจริงอาจต่างกันเล็กน้อยในแต่ละผู้ผลิตชิป ให้ตรวจสอบ Datasheet ของอุปกรณ์นั้นๆ ประกอบด้วย)ปัญหาคลาสสิก: TTL Output (5V) → Standard CMOS Input (5V)
แม้ทั้งสองฝั่งใช้ VCC = 5V เท่ากัน แต่ สเปก VOH ของ TTL ต่ำกว่า VIH ที่ Standard CMOS ต้องการ: TTL รับประกัน VOH (min) เพียง 2.4V เท่านั้น
แต่ Standard CMOS (เช่น 4000B series, 74HC) ต้องการ VIH (min) สูงถึง ≈3.5V (≈0.7×VCC) ถึงจะยอมรับว่าเป็น Logic '1'
ผลคือช่วงแรงดัน 2.4V–3.5V เป็นโซนไม่แน่นอน (Undefined Region) ที่ CMOS อาจอ่านค่าผิดเป็น Logic '0' ได้ ทำให้วงจรทำงานผิดพลาด หมายเหตุ: ปัญหานี้เป็นเรื่องของ TTL/CMOS Logic Family แบบดั้งเดิม (5V) ไม่เกี่ยวข้องกับ LVCMOS ของ FPGA สมัยใหม่ ซึ่งออกแบบ VOH/VIH ให้ใกล้ Rail มากกว่าและไม่มีตระกูล TTL แท้ๆ หลงเหลืออยู่ใน FPGA I/O ปัจจุบัน
<br><img alt="img/standart-logic.png" src="coursecontent/img/standart-logic.png" target="_self" style="width: 906px; max-width: 100%;">ตารางเปรียบเทียบประเภท I/O Bank, ช่วงแรงดัน VCCIO, ข้อกำหนดเฉพาะ และตัวอย่างการนำไปใช้ในงานจริง:นอกจากพิน User I/O ทั่วไปที่ใช้เชื่อมต่อสัญญาณตามต้องการแล้ว FPGA ยังมี Special Dedicated Pins ซึ่งทำหน้าที่เฉพาะทางระดับฮาร์ดแวร์ที่ไม่สามารถเปลี่ยนหน้าที่ผ่านซอฟต์แวร์ได้ ดังนี้:ข้อควรระวังในการจ่ายไฟ VCCIO และต่อพิน FPGA: ความต่างของ HR vs HP Banks ใน Xilinx UltraScale+:
หากจ่ายไฟ หรือป้อนสัญญาณ 3.3V เข้า HP Bank (High Performance) โดยตรง ชิปอาจ พังเสียหายถาวร (Over-voltage Damage)
LVDS Termination Resistor:
การใช้งาน LVDS อินพุต จะต้องตรวจสอบว่าชิปมีตัวต้านทาน ภายในชิป (เช่น DIFF_TERM = TRUE ใน Xilinx หรือ Internal OCT ใน Intel) หรือไม่ หากไม่มี ต้องวางตัวต้านทาน Differential Resistor ที่ฝั่ง Receiver บนแผ่น PCB
VREF Voltage Pins:
ในการใช้งานพอร์ตร่วมกับหน่วยความจำ DDR (เช่น SSTL15/SSTL18) จะต้องต่อพิน ซึ่งมีแรงดันเป็นครึ่งหนึ่งของ (เช่น สำหรับ DDR3 1.5V) เข้ากับ I/O Bank นั้นๆ ด้วย เนื่องจาก SRAM FPGA จะสูญเสียข้อมูลเมื่อปิดไฟ จึงต้องมีวงจรและโหมดในการโหลด Bitstream (ไฟล์คอนฟิก .bit / .sof) เมื่อจ่ายไฟเปิดเครื่อง:
JTAG Mode: ใช้สำหรับการปรับแต่งและ Debug ในขั้นตอนการพัฒนาผ่านสายโปรแกรม (เช่น Xilinx Platform Cable, USB-Blaster)
Master SPI Flash Mode: FPGA ทำหน้าที่เป็น Master อ่านไฟล์ Bitstream จาก External SPI Flash เข้ามาใส่ตัวเองตอน Power-up
Slave Serial / Parallel Mode: มี Processor ภายนอกหรือ Microcontroller เป็นตัวส่งข้อมูล Bitstream มาให้ FPGA
A : program configuration data to SPI flash
B : when power on, fpga will read configuration data from SPI flash<br><img alt="img/configuration.png" src="coursecontent/img/configuration.png" target="_self" style="width: 660px; max-width: 100%;">นอกเหนือจาก Logic Fabric ทั่วไป ชิป FPGA ระดับกลางถึงสูงมักฝัง Hard IP Core (วงจรฮาร์ดแวร์จริงที่ไม่ต้องเสีย Logic LUT) เพื่อประสิทธิภาพการทำงานสูงสุด โดยมีตัวอย่าง Hard IP ที่สำคัญและใช้งานแพร่หลายดังนี้:<br><img alt="img/special-function.png" src="coursecontent/img/special-function.png" target="_self" style="width: 531px; max-width: 100%;">สรุปเพิ่มเติมเกี่ยวกับ DSP Slice ในฐานะ Special Hard IP:
DSP Slice / DSP Block ถือเป็นหนึ่งใน Special Hard IP ที่สำคัญที่สุดของ FPGA เนื่องจากถูกสร้างเป็น Dedicated Hardened ASIC Circuit เพื่อทำหน้าที่คูณและบวก (MAC: Multiply-Accumulate) โดยในแต่ละตระกูล/ซีรีส์ จะมีขนาดบิตของ Multiplier และสถาปัตยกรรมภายในแตกต่างกันอย่างชัดเจน เช่น: Xilinx Spartan-6: DSP48A1 (ขนาดตัวคูณ bit)
Xilinx 7-Series / Zynq-7000: DSP48E1 (ขนาดตัวคูณ bit + Accumulator 48 bit)
Xilinx UltraScale / UltraScale+: DSP48E2 (ขนาดตัวคูณ bit + ALU/Wide XOR)
AMD Versal ACAP: DSP58 (ขนาดตัวคูณ bit + รองรับ Native INT8 / FP32 Vector Math)
Intel Stratix 10 / Agilex: Variable Precision DSP ( bit หรือ Single-Precision IEEE-754 Floating Point) กระบวนการเปลี่ยนไฟล์โค้ดภาษา HDL ไปเป็นฮาร์ดแวร์จริงบนชิป FPGA ประกอบด้วยขั้นตอนต่างๆ ดังนี้:Edit
RTL Specification &amp; Coding: เขียนอธิบายวงจรด้วยภาษา Verilog, VHDL หรือ SystemVerilog
RTL Simulation: จำลองการทำงานของโค้ดฮาร์ดแวร์ร่วมกับไฟล์ Testbench โดยยังไม่ต้องใช้ชิปจริง เพื่อเช็คความถูกต้องของตรรกะ
Synthesis: EDA Tool ทำการสังเคราะห์แปลงโค้ดเชิงตรรกะ ให้กลายเป็นวงจรตรรกะระดับเกต (Gate-Level Netlist) ที่ประกอบไปด้วย LUTs, Flip-Flops และ MUXs
Place and Route (P&amp;R): Place: กำหนดตำแหน่งที่จะวาง LUT/FF แต่ละตัวลงในพิกัดจริงบนชิป FPGA
Route: เชื่อมต่อสายสัญญาณ (Interconnects) ระหว่าง บล็อกต่างๆ Static Timing Analysis (STA): ตรวจสอบว่าสัญญาณเดินทางทันตามรอบเวลา Clock ที่กำหนดหรือไม่
Bitstream Generation: แปลงข้อมูลการจัดวางวงจรให้เป็นไฟล์ไบนารี Bitstream (.bit สำหรับ Xilinx หรือ .sof สำหรับ Intel)
Program Device: อัปโหลดไฟล์ Bitstream เข้าไปทดสอบในบอร์ดจริงผ่านสายโปรแกรม JTAG
ซอฟต์แวร์เครื่องมือสำหรับพัฒนา FPGA แบ่งออกเป็นค่ายหลักและ Open-Source Toolchain:]]></description><link>coursecontent/chapter1-introduction-to-fpga.html</link><guid isPermaLink="false">CourseContent/chapter1-introduction-to-fpga.md</guid><pubDate>Mon, 05 Oct 2026 04:31:48 GMT</pubDate><enclosure url="coursecontent/img/fpga.png" length="0" type="image/png"/><content:encoded>&lt;figure&gt;&lt;img src="coursecontent/img/fpga.png"&gt;&lt;/figure&gt;</content:encoded></item><item><title><![CDATA[chapter4-coding-tools]]></title><description><![CDATA[
<a data-tooltip-position="top" aria-label="1 Traditional &amp; Lightweight Text Editors" data-href="#1 Traditional &amp; Lightweight Text Editors" href="coursecontent/chapter4-coding-tools.html#1_Traditional_&amp;_Lightweight_Text_Editors_0" class="internal-link" target="_self" rel="noopener nofollow">1 Traditional &amp; Lightweight Text Editors</a> <br><a data-tooltip-position="top" aria-label="1.1 Notepad++" data-href="#1.1 Notepad++" href="coursecontent/chapter4-coding-tools.html#1.1_Notepad++_0" class="internal-link" target="_self" rel="noopener nofollow">1.1 Notepad++</a> <br><a data-tooltip-position="top" aria-label="2 Modern Code Editors (VS Code &amp; Extensions)" data-href="#2 Modern Code Editors (VS Code &amp; Extensions)" href="coursecontent/chapter4-coding-tools.html#2_Modern_Code_Editors_(VS_Code_&amp;_Extensions)_0" class="internal-link" target="_self" rel="noopener nofollow">2 Modern Code Editors (VS Code &amp; Extensions)</a> <br><a data-tooltip-position="top" aria-label="2.1 Better TODO Tree / Todo Tree" data-href="#2.1 Better TODO Tree / Todo Tree" href="coursecontent/chapter4-coding-tools.html#2.1_Better_TODO_Tree_/_Todo_Tree_0" class="internal-link" target="_self" rel="noopener nofollow">2.1 Better TODO Tree / Todo Tree</a>
<br><a data-tooltip-position="top" aria-label="2.2 Sigasi Visual HDL (SVH) &amp; Linting Tools" data-href="#2.2 Sigasi Visual HDL (SVH) &amp; Linting Tools" href="coursecontent/chapter4-coding-tools.html#2.2_Sigasi_Visual_HDL_(SVH)_&amp;_Linting_Tools_0" class="internal-link" target="_self" rel="noopener nofollow">2.2 Sigasi Visual HDL (SVH) &amp; Linting Tools</a> <br><a data-tooltip-position="top" aria-label="2.2.1 Real-time Syntax &amp; Semantic Checking (Linting)" data-href="#2.2.1 Real-time Syntax &amp; Semantic Checking (Linting)" href="coursecontent/chapter4-coding-tools.html#2.2.1_Real-time_Syntax_&amp;_Semantic_Checking_(Linting)_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.1 Real-time Syntax &amp; Semantic Checking (Linting)</a>
<br><a data-tooltip-position="top" aria-label="2.2.2 Naming Conventions Enforcement" data-href="#2.2.2 Naming Conventions Enforcement" href="coursecontent/chapter4-coding-tools.html#2.2.2_Naming_Conventions_Enforcement_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.2 Naming Conventions Enforcement</a> <br><a data-tooltip-position="top" aria-label="2.3 TerosHDL (ทางเลือกฟรี 100%)" data-href="#2.3 TerosHDL (ทางเลือกฟรี 100%)" href="coursecontent/chapter4-coding-tools.html#2.3_TerosHDL_(ทางเลือกฟรี_100)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3 TerosHDL (ทางเลือกฟรี 100%)</a> <br><a data-tooltip-position="top" aria-label="3 AI Coding Assistants &amp; Agents in FPGA Development" data-href="#3 AI Coding Assistants &amp; Agents in FPGA Development" href="coursecontent/chapter4-coding-tools.html#3_AI_Coding_Assistants_&amp;_Agents_in_FPGA_Development_0" class="internal-link" target="_self" rel="noopener nofollow">3 AI Coding Assistants &amp; Agents in FPGA Development</a> <br><a data-tooltip-position="top" aria-label="3.1 AI Agent Tools ที่มี Free Tier ให้ทดลองใช้" data-href="#3.1 AI Agent Tools ที่มี Free Tier ให้ทดลองใช้" href="coursecontent/chapter4-coding-tools.html#3.1_AI_Agent_Tools_ที่มี_Free_Tier_ให้ทดลองใช้_0" class="internal-link" target="_self" rel="noopener nofollow">3.1 AI Agent Tools ที่มี Free Tier ให้ทดลองใช้</a> ในบทนี้จะพาไปดู IDE, Text Editor และเครื่องมือช่วยเขียนโค้ด (Coding Tools &amp; Assistants) ที่ผู้เขียนใช้งานจริงในการพัฒนา FPGA/VHDL ตั้งแต่เครื่องมือพื้นฐานไปจนถึง AI Coding AgentText Editor น้ำหนักเบา ยอดนิยมสำหรับการเปิดดูและแก้ไขไฟล์อย่างรวดเร็ว
จุดเด่น: โหลดเร็ว ไม่กินทรัพยากรเครื่อง
มี Built-in Syntax Highlighting สำหรับ VHDL และ Verilog
รองรับ Plugin ต่างๆ เช่น Compare Plugin สำหรับเปรียบเทียบไฟล์ VHDL สองเวอร์ชัน ข้อจำกัด: ไม่มีระบบ Code Intelligence (เช่น Auto-complete ลึกๆ หรือ Linting แบบ Real-time)
Visual Studio Code (VS Code) เป็น Code Editor มาตรฐานยุคใหม่ที่นิยมใช้กันอย่างแพร่หลาย มี Ecosystem ของ Extension ที่ครอบคลุมการพัฒนา FPGA โดยส่วนตัวมี Extension หลักๆ ที่ติดไว้ใช้งานประจำดังนี้Extension สำหรับปักหมุด Comment ชั่วคราวในโค้ด เช่น -- TODO: Add reset logic หรือ -- FIXME: Check setup time แล้ว Extension จะรวบรวมทุกจุดที่ Comment ไว้มาแสดงเป็นรายการเดียวใน Sidebar ให้ไล่เช็คได้ครบในที่เดียว ไม่ต้องมานั่งไล่หาทีละไฟล์ ช่วยให้ไม่พลาดจุดที่ยังเขียนไม่เสร็จก่อนนำโค้ดไป Synthesis<br>Sigasi เป็นเครื่องมือระดับมืออาชีพสำหรับการวิเคราะห์และตรวจสอบคุณภาพโค้ด HDL (VHDL/SystemVerilog) ดูตัวอย่างการใช้งานคร่าวๆ ได้ที่ <a data-tooltip-position="top" aria-label="https://wichayen.github.io/fpgathailand/Sigasi.html" rel="noopener nofollow" class="external-link is-unresolved" href="https://wichayen.github.io/fpgathailand/Sigasi.html" target="_self">Sigasi: VS Code Extension สุดเทพสำหรับคนเขียน VHDL/Verilog</a>
ตรวจจับข้อผิดพลาดทันทีขณะพิมพ์ (เช่น การลืมใส่ Signal ใน Sensitivity List, การเกิด Unintended Latch, หรือ Type Mismatch)
ช่วยลดเวลาจากการต้องรอคอมไพล์ใน Tool ใหญ่ๆ อย่าง Vivado หรือ Quartus
<br>อ่านรายละเอียดเพิ่มเติม: <a data-tooltip-position="top" aria-label="https://www.sigasi.com/manual/creating-editing-exploring-code/linting/vhdl/" rel="noopener nofollow" class="external-link is-unresolved" href="https://www.sigasi.com/manual/creating-editing-exploring-code/linting/vhdl/" target="_self">Sigasi VHDL Linting</a> กำหนดและบังคับใช้มาตรฐานการตั้งชื่อ (Naming Rules) เพื่อให้ทีมพัฒนาอ่านโค้ดเข้าใจตรงกัน เช่น: สัญญาณ Input: i_clock, i_reset_n
สัญญาณ Output: o_data_valid
สัญญาณ Internal: s_cnt_reg, r_state
Constants: C_DATA_WIDTH <br>อ่านรายละเอียดเพิ่มเติม: <a data-tooltip-position="top" aria-label="https://www.sigasi.com/manual/creating-editing-exploring-code/linting/vhdl/naming-conventions/" rel="noopener nofollow" class="external-link is-unresolved" href="https://www.sigasi.com/manual/creating-editing-exploring-code/linting/vhdl/naming-conventions/" target="_self">Sigasi Naming Conventions</a>
note: เวอร์ชันที่ใช้งานได้ฟรีคือ Community Edition เท่านั้น ถ้าจะนำไปใช้งานในบริษัท/งานเชิงพาณิชย์ ต้องเช็ค License ให้ดีก่อนว่าเข้าเงื่อนไข Free หรือไม่
TerosHDL เป็น VS Code Extension แบบ Open-source เฉพาะทาง HDL โดยตรง (VHDL/Verilog/SystemVerilog) รวมเครื่องมือที่จำเป็นไว้ในตัวเดียว:
State Machine Viewer: วาด State Diagram จากโค้ด VHDL ให้อัตโนมัติ ช่วยตรวจสอบ Logic ของ FSM ได้ง่ายขึ้น
Hierarchy / Dependency Viewer: แสดงโครงสร้าง Entity/Module และความสัมพันธ์ระหว่างไฟล์ในโปรเจกต์
Template Generator: สร้างโครง Entity, Process, Testbench ให้อัตโนมัติ
Simulator Integration: เชื่อมต่อกับ Simulator ได้หลายตัว เช่น GHDL, ModelSim, Vivado, Quartus
จุดต่างหลักจาก Sigasi คือ TerosHDL ฟรีแบบไม่มีเงื่อนไข ไม่ว่าจะใช้ส่วนตัวหรือในบริษัทก็ไม่ต้องกังวลเรื่อง License แต่ความสามารถด้าน Real-time Linting/Semantic Checking จะไม่ลึกเท่า Sigasi ซึ่งเป็นเครื่องมือระดับ Commercial Antigravity Agentic IDE จาก Google (ต่อยอดจาก Gemini) มี Editor ในตัวพร้อม AI Agent ที่แก้ไขไฟล์, รันคำสั่ง Terminal และวางแผนงานหลายขั้นตอนได้เอง เหมาะกับงานที่ต้องแก้ไฟล์หลายไฟล์พร้อมกัน เช่น ปรับ VHDL หลาย Module ให้ตรงตาม Naming Convention เดียวกัน
⚠️ ปัจจุบันฟรีเฉพาะช่วง Public Preview เท่านั้น และ Quota ของ Free Tier ถูกปรับลดมาแล้วหลายรอบตั้งแต่เปิดตัว ยังไม่มีการยืนยันราคาถาวรในระยะยาว Codex AI Coding Agent จาก OpenAI ใช้งานผ่าน CLI หรือ IDE Extension ได้ รับคำสั่งเป็นภาษาธรรมชาติแล้วลงมือแก้โค้ด/รันเทสต์ให้อัตโนมัติ เหมาะกับงาน Refactor หรือสร้าง Boilerplate Code (เช่น Testbench เปล่าจาก Entity)
⚠️ Free Tier มาจาก ChatGPT Free Plan ซึ่งมี Quota ค่อนข้างจำกัด เหมาะสำหรับลองใช้งานสั้นๆ มากกว่าใช้ทำงานประจำวัน ถ้าต้องการใช้จริงจังต้องอัปเกรดเป็นแพลนที่มีค่าใช้จ่าย Opencode เครื่องมือ Agentic Coding แบบ Open-source รันผ่าน Terminal จุดเด่นคือสลับ Model Provider ได้อิสระ (เชื่อมต่อได้ทั้ง Cloud API เช่น Gemini/Claude หรือ Local LLM ที่รันบนเครื่องตัวเอง) เหมาะสำหรับผู้ที่ต้องการควบคุมค่าใช้จ่ายและความเป็นส่วนตัวของโค้ด
ตัวโปรแกรมเองฟรีแบบไม่มีเงื่อนไข (Open-source, ไม่บังคับ Subscription) ต่างจาก Antigravity และ Codex ตรงที่ค่าใช้จ่ายจะไปอยู่ที่ Provider ที่เลือกใช้แทน (ถ้าใช้ Cloud API) หรือไม่มีค่าใช้จ่ายเลยถ้าใช้ Local LLM GitHub Copilot AI Coding Assistant จาก GitHub ใช้งานเป็น Extension ใน VS Code (และ IDE อื่นๆ) ได้โดยตรง จุดเด่นคือ Auto-complete แบบ Inline ขณะพิมพ์โค้ด บวกกับโหมด Chat/Agent ให้สั่งงานเป็นภาษาธรรมชาติ และรองรับ MCP Server ได้เช่นกัน ผูกกับ Ecosystem ของ GitHub แน่นหนา (PR, Code Review) เหมาะกับคนที่ใช้ GitHub เป็นหลักอยู่แล้ว
Free Tier เป็นแบบถาวร ไม่ใช่แค่ Preview ให้ Auto-complete 2,000 ครั้ง/เดือน และ Chat/Agent ประมาณ 50 ครั้ง/เดือน เพียงพอสำหรับลองใช้งานเบื้องต้น ถ้าต้องการ Auto-complete/Agent แบบไม่จำกัด ต้องอัปเกรดเป็น Pro ($10/เดือน) note: การใช้งาน AI tools เหล่านี้ต้องระวังเรื่องการส่งข้อมูลออกไปข้างนอก เพราะบางบริษัทเค้ามี ข้อกำหนด ห้ามเปิดเผยข้อมูล (Source Code, Register Map, IP ภายใน) ออกสู่ Cloud AI
]]></description><link>coursecontent/chapter4-coding-tools.html</link><guid isPermaLink="false">CourseContent/chapter4-coding-tools.md</guid><pubDate>Mon, 07 Sep 2026 00:28:26 GMT</pubDate></item><item><title><![CDATA[chapter2-fundamental-of-digital-logic-marp]]></title><description><![CDATA[ระบบเลขฐาน → ลอจิกเกต → Combinational &amp; Sequential → Clock/Reset → FSM → Timing Analysis
ระบบเลขฐาน และการแทนค่าข้อมูล — Binary/Hex, Signed Numbers, Fixed/Floating-Point
ลอจิกเกตพื้นฐาน และการทำงานบน FPGA ผ่าน Look-Up Table (LUT)
Combinational vs Sequential Logic พร้อมตัวอย่างโค้ด VHDL
Clock &amp; Reset Design — Synchronous Design, Synchronizer, Reset Styles
Finite State Machine (FSM) — Moore vs Mealy พร้อมตัวอย่าง Traffic Light
Timing Analysis — Setup/Hold Time, Slack, ในระบบดิจิทัล สัญญาณไฟฟ้ามีเพียง 2 สถานะ: HIGH (1) และ LOW (0)
การเข้าใจรูปแบบการแทนค่าข้อมูลเป็น จุดเริ่มต้นที่สำคัญ ในการออกแบบฮาร์ดแวร์
เนื้อหา: เลขฐานสอง/ฐานสิบหก → เลขติดลบ → การบวก/ลบ → Fixed-Point vs Floating-Point
Binary System (Base 2) — ประกอบด้วยตัวเลข 0 และ 1
Bit (Binary Digit): หน่วยที่เล็กที่สุด (0 หรือ 1)
Byte: 8 bits (เช่น 1101_0010)
Word: ขึ้นอยู่กับสถาปัตยกรรม (16 bits, 32 bits หรือ 64 bits)
Hexadecimal System (Base 16) — ใช้ตัวเลข 0-9 และ A-F แทนค่า 10-15
1 Hex Digit = 4 bits พอดี → ช่วยให้มนุษย์อ่านบัสข้อมูลขนาดยาวได้ง่ายขึ้น
ในการออกแบบระบบดิจิทัลและการคำนวณใน FPGA การแทนค่าตัวเลขติดลบทำได้ 3 วิธีหลัก:
Sign and Magnitude — บิตเครื่องหมาย + ขนาด
1's Complement — คอมพลีเมนต์หนึ่ง
2's Complement — คอมพลีเมนต์สอง ⭐ วิธีมาตรฐานที่นิยมใช้แพร่หลายที่สุด ใช้บิตซ้ายสุด (MSB) เป็น Sign Bit: บิตที่เหลือเก็บ ขนาดตัวเลข (Magnitude)
ตัวอย่าง (4 บิต): 0101, 1101
ข้อเสีย
มีค่า ศูนย์ 2 ค่า: 0000, 1000
สร้างวงจรบวก/ลบทางฮาร์ดแวร์ได้ซับซ้อน
]]></description><link>coursecontent/marp/chapter2-fundamental-of-digital-logic-marp.html</link><guid isPermaLink="false">CourseContent/marp/chapter2-fundamental-of-digital-logic-marp.md</guid><pubDate>Fri, 28 Aug 2026 06:34:24 GMT</pubDate></item><item><title><![CDATA[chapter2-fundamental-of-digital-logic-marp]]></title><description><![CDATA[พื้นฐานลอจิกดิจิทัลสำหรับการเขียน VHDL/Verilog บน FPGAเนื้อหาบทนี้ปูพื้นฐานที่จำเป็นสำหรับการเขียนภาษาบรรยายฮาร์ดแวร์บน FPGA ตั้งแต่ระบบเลขฐาน วงจรตรรกะ Combinational &amp; Sequential ไปจนถึง Synchronous Design และการวิเคราะห์ Timing Closure
เข้าใจ ระบบเลขฐาน และการแทนค่าข้อมูล (Binary, Hex, Signed, Fixed/Floating-Point)
เข้าใจ ลอจิกเกตพื้นฐาน และการทำงานบน FPGA ผ่าน Look-Up Table (LUT)
แยกแยะ Combinational Logic กับ Sequential Logic และเขียน VHDL ได้
ออกแบบ Clock &amp; Reset และ Finite State Machine (FSM)
วิเคราะห์ Timing (Setup/Hold, Slack, ) เพื่อให้ได้ Timing Closure ระบบเลขฐานและการแทนค่าข้อมูล (Number Systems)
พื้นฐานลอจิกเกตและ LUT บน FPGA
สถาปัตยกรรมวงจรดิจิทัล — Combinational vs Sequential
การออกแบบสัญญาณนาฬิกาและรีเซ็ต (Clock &amp; Reset)
Finite State Machines (FSM)
การวิเคราะห์เวลาและสัญญาณรูปคลื่น (Timing &amp; Waveform)
ในระบบดิจิทัล สัญญาณไฟฟ้ามีเพียง 2 สถานะ คือ HIGH (1) และ LOW (0)
Binary System (Base 2): ประกอบด้วยตัวเลข 0 และ 1 Bit (Binary Digit): หน่วยที่เล็กที่สุด (0 หรือ 1)
Byte: 8 bits (เช่น 1101_0010)
Word: ขึ้นอยู่กับสถาปัตยกรรม (16, 32 หรือ 64 bits) Hexadecimal System (Base 16): ใช้ 0-9 และ A-F แทนค่า 10-15 1 Hex Digit แทนเลขฐานสองได้ 4 bits พอดี → อ่านบัสข้อมูลยาวได้ง่ายขึ้น ในการออกแบบระบบดิจิทัลและการคำนวณใน FPGA การแทนค่าตัวเลขติดลบ (Signed) ทำได้ 3 วิธี:
Sign and Magnitude (บิตเครื่องหมาย + ขนาด)
1's Complement (คอมพลีเมนต์หนึ่ง)
2's Complement (คอมพลีเมนต์สอง) — ⭐ วิธีมาตรฐานที่นิยมใช้แพร่หลายที่สุด ใช้บิตซ้ายสุด (MSB) เป็น Sign Bit () และบิตที่เหลือเก็บ Magnitude (ขนาดตัวเลข)
ตัวอย่าง (4 บิต): 0101, 1101
ข้อเสีย: มีค่า ศูนย์ 2 ค่า (+0 = 0000, -0 = 1000)
สร้างวงจรบวก/ลบทางฮาร์ดแวร์ได้ซับซ้อน ]]></description><link>coursecontent/chapter2-fundamental-of-digital-logic-marp.html</link><guid isPermaLink="false">CourseContent/chapter2-fundamental-of-digital-logic-marp.md</guid><pubDate>Thu, 27 Aug 2026 12:15:04 GMT</pubDate></item><item><title><![CDATA[chapter3-basic-vhdl]]></title><description><![CDATA[
<a data-tooltip-position="top" aria-label="1 ภาษาบรรยายฮาร์ดแวร์ VHDL &amp; Signal Naming Conventions" data-href="#1 ภาษาบรรยายฮาร์ดแวร์ VHDL &amp; Signal Naming Conventions" href="chapter3-basic-vhdl.html#1_ภาษาบรรยายฮาร์ดแวร์_VHDL_&amp;_Signal_Naming_Conventions_0" class="internal-link" target="_self" rel="noopener nofollow">1 ภาษาบรรยายฮาร์ดแวร์ VHDL &amp; Signal Naming Conventions</a>
<br><a data-tooltip-position="top" aria-label="2 โครงสร้างภาษา VHDL: Entity &amp; Architecture" data-href="#2 โครงสร้างภาษา VHDL: Entity &amp; Architecture" href="chapter3-basic-vhdl.html#2_โครงสร้างภาษา_VHDL_Entity_&amp;_Architecture_0" class="internal-link" target="_self" rel="noopener nofollow">2 โครงสร้างภาษา VHDL: Entity &amp; Architecture</a>
<br><a data-tooltip-position="top" aria-label="3 Combinational Logic vs Sequential Logic" data-href="#3 Combinational Logic vs Sequential Logic" href="chapter3-basic-vhdl.html#3_Combinational_Logic_vs_Sequential_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">3 Combinational Logic vs Sequential Logic</a> <br><a data-tooltip-position="top" aria-label="3.1 ตัวอย่างโค้ด VHDL: Combinational Process vs Clocked Sequential Process" data-href="#3.1 ตัวอย่างโค้ด VHDL: Combinational Process vs Clocked Sequential Process" href="chapter3-basic-vhdl.html#3.1_ตัวอย่างโค้ด_VHDL_Combinational_Process_vs_Clocked_Sequential_Process_0" class="internal-link" target="_self" rel="noopener nofollow">3.1 ตัวอย่างโค้ด VHDL: Combinational Process vs Clocked Sequential Process</a>
<br><a data-tooltip-position="top" aria-label="3.2 เจาะลึก Sensitivity List: การทำงานและข้อควรระวัง" data-href="#3.2 เจาะลึก Sensitivity List: การทำงานและข้อควรระวัง" href="chapter3-basic-vhdl.html#3.2_เจาะลึก_Sensitivity_List_การทำงานและข้อควรระวัง_0" class="internal-link" target="_self" rel="noopener nofollow">3.2 เจาะลึก Sensitivity List: การทำงานและข้อควรระวัง</a> <br><a data-tooltip-position="top" aria-label="3.2.1 Sensitivity List ใน Combinational Process" data-href="#3.2.1 Sensitivity List ใน Combinational Process" href="chapter3-basic-vhdl.html#3.2.1_Sensitivity_List_ใน_Combinational_Process_0" class="internal-link" target="_self" rel="noopener nofollow">3.2.1 Sensitivity List ใน Combinational Process</a>
<br><a data-tooltip-position="top" aria-label="3.2.2 Sensitivity List ใน Clocked Sequential Process" data-href="#3.2.2 Sensitivity List ใน Clocked Sequential Process" href="chapter3-basic-vhdl.html#3.2.2_Sensitivity_List_ใน_Clocked_Sequential_Process_0" class="internal-link" target="_self" rel="noopener nofollow">3.2.2 Sensitivity List ใน Clocked Sequential Process</a>
<br><a data-tooltip-position="top" aria-label="3.2.3 VHDL-2008 Standard Option: `process(all)`" data-href="#3.2.3 VHDL-2008 Standard Option: `process(all)`" href="chapter3-basic-vhdl.html#3.2.3_VHDL-2008_Standard_Option_`process(all)`_0" class="internal-link" target="_self" rel="noopener nofollow">3.2.3 VHDL-2008 Standard Option: process(all)</a> <br><a data-tooltip-position="top" aria-label="4 ชนิดข้อมูลและแอตทริบิวต์ (Data Types &amp; Attributes)" data-href="#4 ชนิดข้อมูลและแอตทริบิวต์ (Data Types &amp; Attributes)" href="chapter3-basic-vhdl.html#4_ชนิดข้อมูลและแอตทริบิวต์_(Data_Types_&amp;_Attributes)_0" class="internal-link" target="_self" rel="noopener nofollow">4 ชนิดข้อมูลและแอตทริบิวต์ (Data Types &amp; Attributes)</a> <br><a data-tooltip-position="top" aria-label="4.1 ชนิดข้อมูลหลักใน VHDL (`std_logic_1164`):" data-href="#4.1 ชนิดข้อมูลหลักใน VHDL (`std_logic_1164`):" href="chapter3-basic-vhdl.html#4.1_ชนิดข้อมูลหลักใน_VHDL_(`std_logic_1164`)_0" class="internal-link" target="_self" rel="noopener nofollow">4.1 ชนิดข้อมูลหลักใน VHDL (std_logic_1164):</a>
<br><a data-tooltip-position="top" aria-label="4.2 แอตทริบิวต์ที่ใช้บ่อย (VHDL Attributes):" data-href="#4.2 แอตทริบิวต์ที่ใช้บ่อย (VHDL Attributes):" href="chapter3-basic-vhdl.html#4.2_แอตทริบิวต์ที่ใช้บ่อย_(VHDL_Attributes)_0" class="internal-link" target="_self" rel="noopener nofollow">4.2 แอตทริบิวต์ที่ใช้บ่อย (VHDL Attributes):</a> <br><a data-tooltip-position="top" aria-label="5 โครงสร้างควบคุมการทำงาน (Control Flow &amp; Hardware Generation)" data-href="#5 โครงสร้างควบคุมการทำงาน (Control Flow &amp; Hardware Generation)" href="chapter3-basic-vhdl.html#5_โครงสร้างควบคุมการทำงาน_(Control_Flow_&amp;_Hardware_Generation)_0" class="internal-link" target="_self" rel="noopener nofollow">5 โครงสร้างควบคุมการทำงาน (Control Flow &amp; Hardware Generation)</a> <br><a data-tooltip-position="top" aria-label="5.1 คำสั่งควบคุมลำดับภายใน Process (Sequential Control Statements)" data-href="#5.1 คำสั่งควบคุมลำดับภายใน Process (Sequential Control Statements)" href="chapter3-basic-vhdl.html#5.1_คำสั่งควบคุมลำดับภายใน_Process_(Sequential_Control_Statements)_0" class="internal-link" target="_self" rel="noopener nofollow">5.1 คำสั่งควบคุมลำดับภายใน Process (Sequential Control Statements)</a>
<br><a data-tooltip-position="top" aria-label="5.2 คำสั่งสร้างฮาร์ดแวร์ขนานภายนอก Process (Concurrent Generate Statement)" data-href="#5.2 คำสั่งสร้างฮาร์ดแวร์ขนานภายนอก Process (Concurrent Generate Statement)" href="chapter3-basic-vhdl.html#5.2_คำสั่งสร้างฮาร์ดแวร์ขนานภายนอก_Process_(Concurrent_Generate_Statement)_0" class="internal-link" target="_self" rel="noopener nofollow">5.2 คำสั่งสร้างฮาร์ดแวร์ขนานภายนอก Process (Concurrent Generate Statement)</a> <br><a data-tooltip-position="top" aria-label="6 การออกแบบเชิงโมดูลขั้นสูง (Package, Functions &amp; Procedures)" data-href="#6 การออกแบบเชิงโมดูลขั้นสูง (Package, Functions &amp; Procedures)" href="chapter3-basic-vhdl.html#6_การออกแบบเชิงโมดูลขั้นสูง_(Package,_Functions_&amp;_Procedures)_0" class="internal-link" target="_self" rel="noopener nofollow">6 การออกแบบเชิงโมดูลขั้นสูง (Package, Functions &amp; Procedures)</a> <br><a data-tooltip-position="top" aria-label="6.1 Package (`*_pkg.vhd`)" data-href="#6.1 Package (`*_pkg.vhd`)" href="chapter3-basic-vhdl.html#6.1_Package_(`*_pkg.vhd`)_0" class="internal-link" target="_self" rel="noopener nofollow">6.1 Package (_pkg.vhd)</a>
<br><a data-tooltip-position="top" aria-label="6.2 Functions (`function`)" data-href="#6.2 Functions (`function`)" href="chapter3-basic-vhdl.html#6.2_Functions_(`function`)_0" class="internal-link" target="_self" rel="noopener nofollow">6.2 Functions (function)</a>
<br><a data-tooltip-position="top" aria-label="6.3 Procedures (`procedure`)" data-href="#6.3 Procedures (`procedure`)" href="chapter3-basic-vhdl.html#6.3_Procedures_(`procedure`)_0" class="internal-link" target="_self" rel="noopener nofollow">6.3 Procedures (procedure)</a> <br><a data-tooltip-position="top" aria-label="7 Signal vs Variable" data-href="#7 Signal vs Variable" href="chapter3-basic-vhdl.html#7_Signal_vs_Variable_0" class="internal-link" target="_self" rel="noopener nofollow">7 Signal vs Variable</a>
<br><a data-tooltip-position="top" aria-label="8 Signal Naming example" data-href="#8 Signal Naming example" href="chapter3-basic-vhdl.html#8_Signal_Naming_example_0" class="internal-link" target="_self" rel="noopener nofollow">8 Signal Naming example</a> <br><a data-tooltip-position="top" aria-label="8.1 ตัวอย่าง Convention หลัก" data-href="#8.1 ตัวอย่าง Convention หลัก" href="chapter3-basic-vhdl.html#8.1_ตัวอย่าง_Convention_หลัก_0" class="internal-link" target="_self" rel="noopener nofollow">8.1 ตัวอย่าง Convention หลัก</a>
<br><a data-tooltip-position="top" aria-label="8.2 Convention อื่นๆ ที่พบได้บ่อยในอุตสาหกรรม (สำหรับอ้างอิง)" data-href="#8.2 Convention อื่นๆ ที่พบได้บ่อยในอุตสาหกรรม (สำหรับอ้างอิง)" href="chapter3-basic-vhdl.html#8.2_Convention_อื่นๆ_ที่พบได้บ่อยในอุตสาหกรรม_(สำหรับอ้างอิง)_0" class="internal-link" target="_self" rel="noopener nofollow">8.2 Convention อื่นๆ ที่พบได้บ่อยในอุตสาหกรรม (สำหรับอ้างอิง)</a> <br><a data-tooltip-position="top" aria-label="9 Synthesizable vs Non-synthesizable (Testbench-only) VHDL Syntax" data-href="#9 Synthesizable vs Non-synthesizable (Testbench-only) VHDL Syntax" href="chapter3-basic-vhdl.html#9_Synthesizable_vs_Non-synthesizable_(Testbench-only)_VHDL_Syntax_0" class="internal-link" target="_self" rel="noopener nofollow">9 Synthesizable vs Non-synthesizable (Testbench-only) VHDL Syntax</a> <br><a data-tooltip-position="top" aria-label="9.1 ตารางเปรียบเทียบ Syntax ที่พบบ่อย" data-href="#9.1 ตารางเปรียบเทียบ Syntax ที่พบบ่อย" href="chapter3-basic-vhdl.html#9.1_ตารางเปรียบเทียบ_Syntax_ที่พบบ่อย_0" class="internal-link" target="_self" rel="noopener nofollow">9.1 ตารางเปรียบเทียบ Syntax ที่พบบ่อย</a>
<br><a data-tooltip-position="top" aria-label="9.2 ตัวอย่างโค้ด: รูปแบบ Non-synthesizable (Testbench-only) ที่พบบ่อย" data-href="#9.2 ตัวอย่างโค้ด: รูปแบบ Non-synthesizable (Testbench-only) ที่พบบ่อย" href="chapter3-basic-vhdl.html#9.2_ตัวอย่างโค้ด_รูปแบบ_Non-synthesizable_(Testbench-only)_ที่พบบ่อย_0" class="internal-link" target="_self" rel="noopener nofollow">9.2 ตัวอย่างโค้ด: รูปแบบ Non-synthesizable (Testbench-only) ที่พบบ่อย</a>
<br><a data-tooltip-position="top" aria-label="9.3 วิธีเช็คว่าโค้ดของเรา Synthesizable หรือ Non-synthesizable" data-href="#9.3 วิธีเช็คว่าโค้ดของเรา Synthesizable หรือ Non-synthesizable" href="chapter3-basic-vhdl.html#9.3_วิธีเช็คว่าโค้ดของเรา_Synthesizable_หรือ_Non-synthesizable_0" class="internal-link" target="_self" rel="noopener nofollow">9.3 วิธีเช็คว่าโค้ดของเรา Synthesizable หรือ Non-synthesizable</a> เนื้อหานี้มุ่งเน้นการทำความเข้าใจสถาปัตยกรรมภาษา VHDL (Hardware Description Language) ตั้งแต่พื้นฐานโครงสร้าง พฤติกรรมฮาร์ดแวร์ กฎการตั้งชื่อสัญญาณมาตรฐานอุตสาหกรรม จนถึงการออกแบบโมดูลขั้นสูงโครงสร้างหลักของไฟล์ VHDL แบ่งออกเป็น 3 ส่วนหลักตามลำดับดังแสดงในรูป:<br><img alt="vhdl_structure.png" src="img/vhdl_structure.png" target="_self" style="width: 553px; max-width: 100%;"> Library Declaration (ส่วนประกาศไลบรารี): ทำหน้าที่ระบุไลบรารีและแพ็กเกจที่จะดึงมาใช้งาน เช่น library IEEE; use IEEE.STD_LOGIC_1164.ALL; เพื่อใช้งานชนิดข้อมูลมาตรฐานฮาร์ดแวร์ Entity Declaration (ส่วนประกาศเอนทิตี): เปรียบเสมือน "กรอบกล่องฮาร์ดแวร์ภายนอก" ที่กำหนดชื่อโมดูล พารามิเตอร์ปรับขนาดวงจร (Generic) และสัญญาณขาเข้า/ขาออก (Port) Architecture Definition (ส่วนนิยามสถาปัตยกรรม): เปรียบเสมือน "วงจรภายในกล่อง" ที่บรรยายการทำงานของฮาร์ดแวร์ โดยแบ่งโครงสร้างภายในออกเป็น 2 โซนสำคัญ: Declarative Region (โซนประกาศก่อนคำสั่ง begin): Component Declaration: ประกาศโมดูลย่อย (Sub-modules) ที่ต้องการดึงมาต่อร่วมในวงจร
Signal Declaration: ประกาศสายสัญญาณภายใน (Internal Nodes) และสลักข้อมูล/เรจิสเตอร์ (Registers) Statement Region (โซนบรรยายวงจรหลังคำสั่ง begin): เขียนบรรยายพฤติกรรมวงจร (Circuit Description) ด้วยคำสั่งขนาน (Concurrent Statements), การต่อสายโมดูลย่อย (Component Instantiation), หรือบล็อกการทำงานตามลำดับ (process) library IEEE;
use IEEE.STD_LOGIC_1164.ALL; -- 1. Library &amp; Entity Declaration (กรอบกล่องภายนอก)
entity my_module is Generic ( G_DATA_WIDTH : integer := 8 -- Generic Parameter ); Port ( clk : in std_logic; rst_n : in std_logic; i_data: in std_logic_vector(G_DATA_WIDTH-1 downto 0); o_data: out std_logic_vector(G_DATA_WIDTH-1 downto 0) );
end entity my_module; -- 2. Architecture Definition (วงจรภายในกล่อง)
architecture Behavioral of my_module is -- [Declarative Region] ประกาศ Signal / Component ภายใน signal s_data_reg : std_logic_vector(G_DATA_WIDTH-1 downto 0);
begin -- [Statement Region] บรรยายวงจรฮาร์ดแวร์หลังคำสั่ง begin process(clk, rst_n) begin if rst_n = '0' then s_data_reg &lt;= (others =&gt; '0'); elsif rising_edge(clk) then s_data_reg &lt;= i_data; end if; end process; o_data &lt;= s_data_reg;
end architecture Behavioral;
ข้อควรระวัง: การกำหนดค่าเริ่มต้นที่ Signal Declaration
การเขียน signal s_data_reg : std_logic_vector(...) := (others =&gt; '0'); เป็นการกำหนดค่า Initial Value สำหรับ Simulation เท่านั้น (ค่า ณ เวลา 0 ns) ไม่ใช่วงจร Reset จริง — บน FPGA จริง ค่านี้จะถูกโหลดผ่านกลไก Configuration/Bitstream ของแต่ละเวนเดอร์เท่านั้น (ใช้ได้กับบาง Flow) และไม่สามารถพึ่งพาแทนวงจร Reset (เช่น rst_n) ได้ หากต้องการ Reset ที่ทำงานได้จริงหลัง Power-up หรือระหว่างการทำงาน ต้องออกแบบผ่าน process(clk, rst_n) ตามตัวอย่างข้างต้นเสมอ
ในภาษา VHDL วงจรฮาร์ดแวร์และการประมวลผลคำสั่งแบ่งออกเป็น 2 ประเภทหลัก: Combinational Logic (วงจรรวมเชิงผสม — นอก Process หรือใน Combinational Process): โค้ดที่อยู่นอก process (เช่น สัญญาณกำหนดค่าธรรมดา, when-else, with-select) รวมถึง Combinational Process จะถูกประมวลผล ขนานกันตลอดเวลา (Parallel Execution) ไม่ว่าเราจะสลับบรรทัดขึ้นลงอย่างไร วงจรฮาร์ดแวร์ที่สังเคราะห์ได้จะมีผลลัพธ์เหมือนกันทุกประการ Sequential Logic (วงจรเชิงลำดับ — ใน Clocked Process): โค้ดที่อยู่ภายในบล็อก process(...) ที่ทำงานตามขอบสัญญาณนาฬิกา จะถูกประมวลผล เรียงตามลำดับบรรทัดจากบนลงล่าง (Sequential Execution) เมื่อสัญญาณใน Sensitivity List มีการเปลี่ยนแปลงค่า 💡 ข้อสังเกต Combinational Logic: ถึงแม้โค้ดใน process จะอ่านและประมวลผลเรียงบรรทัด แต่เมื่อนำไปสังเคราะห์เป็นวงจร Combinational Logic (ที่ใส่ Sensitivity List ครบถ้วน) ผลลัพธ์ฮาร์ดแวร์ที่ได้ในชิปจริงก็คือ การทำงานขนานกันตลอดเวลา (Parallel Hardware) ไม่ต่างจากการเขียน Combinational Logic นอก Process
<br><img alt="logic_type.png" src="img/logic_type.png" target="_self" style="width: 966px; max-width: 100%;">ในทางปฏิบัติ การใช้งาน Process แบ่งออกเป็น 2 ประเภทหลัก:
Combinational Process (ไม่มี Clock): ทำงานเมื่ออินพุตเปลี่ยนค่า (ต้องใส่สัญญาณอินพุตทุกตัวใน Sensitivity List หรือใช้ process(all) เพื่อให้ได้วงจร Combinational Logic ขนานที่สมบูรณ์และป้องกัน Latch)
Clocked Sequential Process (มี Clock — ⭐ วิธีมาตรฐานที่แนะนำสำหรับ FPGA): ทำงานเฉพาะที่ขอบสัญญาณนาฬิกา (rising_edge(clk)) สังเคราะห์ได้ Flip-Flops (Sequential Logic) ที่มีไทม์มิ่งเสถียร ป้องกันการเกิด Glitch และ Latch โดยสมบูรณ์
library IEEE;
use IEEE.STD_LOGIC_1164.ALL; entity process_types_demo is Port ( clk : in std_logic; rst_n : in std_logic; i_a : in std_logic; i_b : in std_logic; o_comb_y : out std_logic; -- ผลลัพธ์จาก Combinational Process o_clocked_q: out std_logic -- ผลลัพธ์จาก Clocked Sequential Process (แนะนำ) );
end entity process_types_demo; architecture Behavioral of process_types_demo is signal s_comb_node : std_logic;
begin ------------------------------------------------------------------ -- 1. Combinational Process (ประมวลผลทันทีที่อินพุตเปลี่ยน) -- ⚠️ ต้องใส่ "ทุกสัญญาณที่ถูกอ่านค่าใน Process" เข้า Sensitivity List ให้ครบ ------------------------------------------------------------------ process(i_a, i_b, s_comb_node) begin s_comb_node &lt;= i_a and i_b; o_comb_y &lt;= s_comb_node or i_a; end process; ------------------------------------------------------------------ -- 2. Clocked Sequential Process (⭐ แนะนำที่สุดสำหรับการออกแบบ FPGA) -- ทำงานเรียงลำดับเฉพาะจังหวะขอบขาขึ้นของ clk (และ Async Reset) ------------------------------------------------------------------ process(clk, rst_n) begin if rst_n = '0' then o_clocked_q &lt;= '0'; elsif rising_edge(clk) then -- สังเคราะห์กลายเป็น D Flip-Flop บันทึกค่าตามจังหวะ Clock o_clocked_q &lt;= i_a and i_b; end if; end process; end architecture Behavioral;
ข้อสังเกตฮาร์ดแวร์: Combinational Logic: เหมาะสำหรับเขียนสมการ Combinational สั้นๆ (เช่น o_y &lt;= i_a and i_b;, when-else, with-select)
Sequential Logic: จำเป็นต้องใช้เมื่อสร้าง Sequential Logic (Flip-Flops / Clock Edge) หรือต้องการใช้โครงสร้างเงื่อนไขที่ซับซ้อน เช่น if-then-else และ case-when Sensitivity List คือรายการสัญญาณที่ระบุไว้ในวงเล็บหลังคำสั่ง process(...) เพื่อบอก Simulator และ Synthesis Tool ว่า "ให้บล็อก Process นี้ประมวลผลใหม่ทุกครั้งที่มีสัญญาณตัวใดตัวหนึ่งในรายการเปลี่ยนค่า (Signal Event)"
กฎเหล็ก: ต้องใส่ สัญญาณอินพุตทุกตัว ที่ถูกอ่านค่าภายใน Process เข้าไปใน Sensitivity List ให้ครบถ้วน
หากใส่ไม่ครบ: จะเกิดปัญหา Simulation vs Synthesis Mismatch (ผลการจำลองใน Simulation ไม่ตรงกับฮาร์ดแวร์จริง) และ Synthesis Tool จะสร้าง Unintentional Latch ขึ้นมาเก็บค่าสัญญาณที่ไม่ได้ถูกอัปเดต กฎเหล็ก: ใส่เฉพาะสัญญาณ Clock (และ Asynchronous Reset ถ้ามี) เท่านั้น!
เหตุผล: วงจร Register / Flip-Flop จะตอบสนองและเปลี่ยนสถานะเฉพาะที่ขอบสัญญาณนาฬิกา (rising_edge(clk)) สัญญาณ Data Input อื่นๆ ไม่จำเป็นต้องใส่ใน Sensitivity List เพราะค่าจะถูกจับบันทึก (Sample) ตามจังหวะ Clock เท่านั้น
ในมาตรฐาน VHDL-2008 เพิ่มฟีเจอร์ process(all) สำหรับ Combinational Logic ช่วยให้ Compiler รวบรวมสัญญาณอินพุตทั้งหมดที่อ่านใน Process เข้า Sensitivity List ให้อัตโนมัติ ช่วยลดข้อผิดพลาดในการลืมใส่สัญญาณให้ครบถ้วน std_logic: สัญญาณ 1 บิต มี 9 สถานะ (สถานะหลักคือ '0', '1', 'Z' (High Impedance), 'X' (Unknown))<br>
<img alt="std_logic.png" src="img/std_logic.png" target="_self" style="width: 672px; max-width: 100%;"> std_logic_vector: อาเรย์ชุดสัญญาณบิต เช่น std_logic_vector(7 downto 0) array: การจำลองหน่วยความจำ เช่น อาเรย์ของ vector:
type ram_type is array (0 to 255) of std_logic_vector(7 downto 0);
signal s_ram : ram_type; record: กลุ่มข้อมูลรวมหลายประเภทไว้ในชื่อเดียว (คล้าย struct ในภาษา C):
type bus_header_t is record addr : std_logic_vector(15 downto 0); data : std_logic_vector(31 downto 0); valid: std_logic;
end record; 'high / 'low: คืนค่าดัชนีสูงสุด / ต่ำสุดของเวกเตอร์
'range / 'reverse_range: คืนค่าช่วงดัชนี เช่น (7 downto 0)
'length: คืนค่าจำนวนบิตทั้งหมดของเวกเตอร์ if-then-else:
ใช้สร้างเงื่อนไขตามลำดับความสำคัญ (Priority Logic) ซึ่งหากไม่ระบุเงื่อนไข else ครบถ้วนในวงจร Combinational Logic จะทำให้เกิด Unintentional Latch ได้
process(i_enable, i_data)
begin if i_enable = '1' then o_out &lt;= i_data; else o_out &lt;= (others =&gt; '0'); end if;
end process; case-when:
ใช้สร้าง Multiplexer หรือ FSM State Transition โดยบังคับให้ระบุเงื่อนไขให้ครอบคลุมทุกค่าอินพุตที่เป็นไปได้ด้วย when others
process(s_state)
begin case s_state is when "00" =&gt; o_led &lt;= "0001"; when "01" =&gt; o_led &lt;= "0010"; when "10" =&gt; o_led &lt;= "0100"; when others =&gt; o_led &lt;= "1000"; -- ป้องกัน Latch end case;
end process; for-loop:
ใช้คลี่สร้างโครงสร้างลอจิกขนานตามจำนวนรอบแบบตายตัว (Unrolling Loop) ในการสังเคราะห์ฮาร์ดแวร์ เช่น การสลับกลับบิตข้อมูล (Bit Reversal):
process(i_data)
begin for i in 0 to 7 loop o_reversed(i) &lt;= i_data(7 - i); end loop;
end process; for-generate: ถอดแบบโมดูลหรือวงจรขนานตามจำนวนที่กำหนด เช่น สร้าง Buffer 4 ช่อง:
g_buf_array: for i in 0 to 3 generate u_buf : entity work.buffer_cell port map ( i_in =&gt; i_data(i), o_out =&gt; o_data(i) );
end generate g_buf_array; if-generate: เลือกสร้างโครงสร้างฮาร์ดแวร์ตามเงื่อนไข Generic (Conditional Instantiation) เช่น เลือกสร้างวงจร Pipe-lined Register หรือต่อตรงตามค่า Generic G_INCLUDE_REG:
-- สร้างวงจรเก็บข้อมูลเมื่อ Generic G_INCLUDE_REG = true
g_reg_gen: if G_INCLUDE_REG generate u_reg : entity work.data_register port map ( clk =&gt; clk, i_d =&gt; i_data, o_q =&gt; o_data );
end generate g_reg_gen; -- สร้างวงจรต่อสายตรงเมื่อ Generic G_INCLUDE_REG = false
g_pass_gen: if not G_INCLUDE_REG generate o_data &lt;= i_data;
end generate g_pass_gen; ใช้เก็บประกาศประเภทข้อมูลชนิดพิเศษ (type), ค่าคงที่ (constant), และฟังก์ชันส่วนกลาง เพื่อให้ทุกโมดูลในโปรเจกต์อ้างอิงและดึงไปใช้งานร่วมกันได้:library IEEE;
use IEEE.STD_LOGIC_1164.ALL; package sys_config_pkg is constant C_CLK_FREQ : integer := 100_000_000; -- 100 MHz type T_BUS_RECORD is record addr : std_logic_vector(15 downto 0); data : std_logic_vector(31 downto 0); valid : std_logic; end record;
end package sys_config_pkg;
บล็อกการคำนวณแบบ Pure Combinational Logic ที่คืนค่ากลับมา 1 ค่าเสมอ (ไม่มี Propagation Delay ในการจำลอง และไม่สร้าง Clock Edge) นิยมใช้ทำ Math Calculation หรือหาขนาด Bit Width:function calc_parity (val : std_logic_vector) return std_logic is variable v_parity : std_logic := '0';
begin for i in val'range loop v_parity := v_parity xor val(i); end loop; return v_parity;
end function calc_parity;
บล็อกโค้ดย่อยที่สามารถรับและส่งค่าผ่านพอร์ต in, out, inout ได้หลายค่าพร้อมกัน นิยมนำมาใช้สร้าง Helper Subprograms สำหรับไดรฟ์สัญญาณในไฟล์ Testbench ให้เขียนได้สั้นและอ่านง่ายขึ้น (สามารถใช้คำสั่ง wait for หน่วงเวลาภายใน Procedure ได้เมื่อใช้ใน Testbench)-- 1. ประกาศและนิยาม Procedure สำหรับส่งข้อมูลผ่าน Bus ใน Testbench
procedure write_bus ( constant addr : in std_logic_vector(7 downto 0); constant data : in std_logic_vector(7 downto 0); signal o_addr : out std_logic_vector(7 downto 0); signal o_data : out std_logic_vector(7 downto 0); signal o_valid: out std_logic
) is
begin o_addr &lt;= addr; o_data &lt;= data; o_valid &lt;= '1'; wait for 10 ns; -- หน่วงเวลา 1 Clock Cycle o_valid &lt;= '0'; wait for 10 ns;
end procedure write_bus; -- 2. การเรียกใช้งานภายใน Stimulus Process ของ Testbench
p_stimulus : process
begin -- ส่งข้อมูลผ่าน Procedure ได้สั้นกระชับ ไม่ต้องเขียนโค้ดซ้ำหลายบรรทัด write_bus(X"0A", X"FF", s_tb_addr, s_tb_data, s_tb_valid); write_bus(X"0B", X"A5", s_tb_addr, s_tb_data, s_tb_valid); wait;
end process p_stimulus;
สิ่งสำคัญที่สุดในการเขียน VHDL คือความเข้าใจเรื่อง การอัปเดตค่าของ Signal และ Variable:ข้อควรระวัง Signal vs Variable:
การใช้ Variable กำหนดค่าหลายครั้งใน Process เดียวกัน จะสร้าง Combinational Logic ที่ต่อซ้อนกันเป็นทอดๆ (Cascaded Logic) ซึ่งอาจส่งผลเสียต่อ Data Path Delay และเกิด Timing Violation ได้ หากไม่จำเป็นควรเน้นใช้ signal เป็นหลัก
เพื่อเพิ่มความอ่านง่าย (Readability) และลดความผิดพลาดในการเชื่อมต่อโมดูลความซับซ้อนสูง การออกแบบ FPGA มักมีข้อตกลงในการเติม Prefix/Suffix ให้กับสัญญาณข้อควรทราบก่อนอ่านตาราง
แต่ละบริษัท/ทีมออกแบบมี Naming Convention ของตัวเอง ซึ่งไม่มีสูตรตายตัวว่าแบบไหนถูกหรือผิด สิ่งที่สำคัญที่สุดคือ เลือกใช้ Convention เดียวให้สม่ำเสมอตลอดทั้งโปรเจกต์ ห้ามผสมหลาย Convention ปนกัน (เช่น ใช้ทั้ง _n และ _l แทนความหมาย Active-Low ในโปรเจกต์เดียวกัน) เพราะจะทำให้ทีมสับสนและอ่านโค้ดยากขึ้น
หัวข้อ 8.1 เป็นตัวอย่าง Convention หลักที่แนะนำ ส่วน 8.2 เป็น Convention อื่นๆ ที่พบได้บ่อยในอุตสาหกรรม ไว้สำหรับอ้างอิงเวลาไปอ่านโค้ดของทีม/บริษัทอื่น
⚠️ คำเตือน
ตารางด้านล่างนี้รวบรวมมาจากหลายแหล่ง/หลายบริษัท มีบาง Pattern ที่ ขัดแย้งกับตาราง 8.1 โดยตรง (เช่น Active-Low ใช้ _n ใน 8.1 แต่บางแหล่งใช้ _l, Internal Signal ใช้ s_* ใน 8.1 แต่บางแหล่งใช้ w_*) — อ่านไว้เพื่อ "รู้จัก" เวลาไปเจอโค้ดของทีมอื่น
Convention แบบ Xilinx/AMD (Active-Low ใช้ Suffix _l แทน _n)Convention แบบ EmLogic (Internal Signal แยก Prefix w_*/r_* ตาม Combinational/Registered)อ่านเพิ่มเติมได้ที่:
<br><a rel="noopener nofollow" class="external-link is-unresolved" href="https://emlogic.no/vhdl-conventions/" target="_self">https://emlogic.no/vhdl-conventions/</a>
<br><a rel="noopener nofollow" class="external-link is-unresolved" href="https://wiki.electroniciens.cnrs.fr/images/Xilinx_HDL_Coding_style.pdf" target="_self">https://wiki.electroniciens.cnrs.fr/images/Xilinx_HDL_Coding_style.pdf</a>
ภาษา VHDL ถูกออกแบบมาให้ใช้ได้ทั้ง สร้างฮาร์ดแวร์จริง (Synthesis) และ จำลองพฤติกรรม (Simulation) แต่ไม่ใช่ทุกคำสั่งใน VHDL จะสามารถ "สังเคราะห์" ออกมาเป็นวงจรจริงบนชิปได้ บาง Syntax มีไว้สำหรับ Testbench เท่านั้น (Simulation-only) เพราะมันอธิบายพฤติกรรมของ "เวลา" หรือ "สภาพแวดล้อมการทดสอบ" ซึ่งไม่มีตัวตนอยู่จริงในฮาร์ดแวร์💡 หลักการ Synthesizable Code (RTL) = โค้ดที่ Synthesis Tool (Quartus, Vivado) แปลงเป็น Gate/Flip-Flop/Wire จริงได้
Non-synthesizable Code (Testbench-only) = โค้ดที่ใช้ได้เฉพาะตอน Simulate (ModelSim, QuestaSim, GHDL) เพื่อสร้าง Stimulus หรือตรวจสอบผลลัพธ์ ถ้านำไปสังเคราะห์ Synthesis Tool จะ Error หรือเพิกเฉย (Ignore) ทันที ผู้เริ่มต้นศึกษา FPGA ส่วนใหญ่มักเจอปัญหานี้เป็นด่านแรกๆ คือ แยกไม่ออกว่า Syntax ไหน "Synthesizable" (สังเคราะห์เป็นวงจรจริงได้) และ Syntax ไหน "Un-synthesizable" (ใช้ได้แค่ตอน Simulate) เพราะ Compiler ของ Simulator (เช่น ModelSim) จะ Compile ผ่านหมดทุกคำสั่ง โดยไม่มี Error ใดๆ ทำให้เข้าใจผิดว่าโค้ดที่รันผ่านใน Simulation จะสังเคราะห์เป็นฮาร์ดแวร์ได้เสมอ ทั้งที่จริงแล้ว VHDL เป็น Superset ที่ใหญ่กว่า Hardware ที่สังเคราะห์ได้จริงมาก ความสับสนนี้เป็นสาเหตุอันดับต้นๆ ที่ทำให้ Code รันบน Simulator ได้ปกติ แต่พอนำไป Synthesis กลับ Error หรือได้วงจรที่ทำงานผิดจากที่ตั้งใจ ตารางด้านล่างนี้จึงสรุป Keyword/Syntax ที่พบบ่อยไว้ให้library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use STD.TEXTIO.ALL; entity tb_my_module is
end entity tb_my_module; -- Testbench ไม่มี Port architecture sim of tb_my_module is constant C_CLK_PERIOD : time := 10 ns; -- 'time' type: Non-synthesizable signal clk : std_logic := '0'; signal rst_n : std_logic := '0'; signal i_data : std_logic_vector(7 downto 0) := (others =&gt; '0'); signal o_data : std_logic_vector(7 downto 0);
begin -- Instance ของวงจรจริง (DUT: Design Under Test) u_dut : entity work.my_module port map ( clk =&gt; clk, rst_n =&gt; rst_n, i_data =&gt; i_data, o_data =&gt; o_data ); ------------------------------------------------------------------ -- Clock Generator: ใช้ wait for สร้างสัญญาณนาฬิกา (Non-synthesizable) ------------------------------------------------------------------ p_clk_gen : process begin clk &lt;= '0'; wait for C_CLK_PERIOD / 2; clk &lt;= '1'; wait for C_CLK_PERIOD / 2; end process p_clk_gen; ------------------------------------------------------------------ -- Stimulus Process: สั่งค่าอินพุตตามลำดับเวลา แล้วปิดท้ายด้วย wait; ------------------------------------------------------------------ p_stimulus : process begin rst_n &lt;= '0'; wait for 20 ns; -- absolute delay: Non-synthesizable rst_n &lt;= '1'; wait until rising_edge(clk); -- รอ Edge: Non-synthesizable i_data &lt;= X"AA"; wait for 40 ns; assert (o_data = X"AA") -- assert/report: Non-synthesizable report "o_data mismatch!" severity error; wait; -- หยุด Process ค้างตลอดไป จบการทดสอบ end process p_stimulus; end architecture sim;
Syntax เหล่านี้ไม่สามารถสังเคราะห์เป็นวงจรจริงได้ wait for, wait until, assert/report, std.textio, ชนิดข้อมูล time/real
Clock Generator (p_clk_gen แบบข้างต้น) วิธีที่ง่ายและแม่นยำที่สุดคือ ลอง Compile/รัน Synthesis จริงบน Tool ที่ใช้งานอยู่ (เช่น Quartus, Vivado) หากโค้ดส่วนใดเป็น Non-synthesizable Tool จะแจ้ง Error หรือ Warning ให้ทราบทันที]]></description><link>chapter3-basic-vhdl.html</link><guid isPermaLink="false">chapter3-basic-vhdl.md</guid><pubDate>Tue, 25 Aug 2026 09:01:18 GMT</pubDate><enclosure url="img/vhdl_structure.png" length="0" type="image/png"/><content:encoded>&lt;figure&gt;&lt;img src="img/vhdl_structure.png"&gt;&lt;/figure&gt;</content:encoded></item><item><title><![CDATA[chapter1-introduction-to-fpga]]></title><description><![CDATA[
<a data-tooltip-position="top" aria-label="1 Basic FPGA Overview &amp; Concepts" data-href="#1 Basic FPGA Overview &amp; Concepts" href="chapter1-introduction-to-fpga.html#1_Basic_FPGA_Overview_&amp;_Concepts_0" class="internal-link" target="_self" rel="noopener nofollow">1 Basic FPGA Overview &amp; Concepts</a> <br><a data-tooltip-position="top" aria-label="1.1 FPGA คืออะไร? (What is an FPGA?)" data-href="#1.1 FPGA คืออะไร? (What is an FPGA?)" href="chapter1-introduction-to-fpga.html#1.1_FPGA_คืออะไร?_(What_is_an_FPGA" class="internal-link" target="_self" rel="noopener nofollow">1.1 FPGA คืออะไร? (What is an FPGA?)</a> <br><a data-tooltip-position="top" aria-label="1.1.1 คำศัพท์:" data-href="#1.1.1 คำศัพท์:" href="chapter1-introduction-to-fpga.html#1.1.1_คำศัพท์_0" class="internal-link" target="_self" rel="noopener nofollow">1.1.1 คำศัพท์:</a> <br><a data-tooltip-position="top" aria-label="1.2 FPGA เหมาะกับงานอะไร? (FPGA Use Cases &amp; Comparison)" data-href="#1.2 FPGA เหมาะกับงานอะไร? (FPGA Use Cases &amp; Comparison)" href="chapter1-introduction-to-fpga.html#1.2_FPGA_เหมาะกับงานอะไร?_(FPGA_Use_Cases_&amp;_Comparison)_0" class="internal-link" target="_self" rel="noopener nofollow">1.2 FPGA เหมาะกับงานอะไร? (FPGA Use Cases &amp; Comparison)</a> <br><a data-tooltip-position="top" aria-label="1.2.1 ตารางเปรียบเทียบ FPGA vs MCU vs CPU/GPU vs ASIC" data-href="#1.2.1 ตารางเปรียบเทียบ FPGA vs MCU vs CPU/GPU vs ASIC" href="chapter1-introduction-to-fpga.html#1.2.1_ตารางเปรียบเทียบ_FPGA_vs_MCU_vs_CPU/GPU_vs_ASIC_0" class="internal-link" target="_self" rel="noopener nofollow">1.2.1 ตารางเปรียบเทียบ FPGA vs MCU vs CPU/GPU vs ASIC</a>
<br><a data-tooltip-position="top" aria-label="1.2.2 สรุปจุดเด่นและการเลือกใช้งาน:" data-href="#1.2.2 สรุปจุดเด่นและการเลือกใช้งาน:" href="chapter1-introduction-to-fpga.html#1.2.2_สรุปจุดเด่นและการเลือกใช้งาน_0" class="internal-link" target="_self" rel="noopener nofollow">1.2.2 สรุปจุดเด่นและการเลือกใช้งาน:</a> <br><a data-tooltip-position="top" aria-label="1.3 เทคโนโลยีของ FPGA (FPGA Technologies &amp; Architectures)" data-href="#1.3 เทคโนโลยีของ FPGA (FPGA Technologies &amp; Architectures)" href="chapter1-introduction-to-fpga.html#1.3_เทคโนโลยีของ_FPGA_(FPGA_Technologies_&amp;_Architectures)_0" class="internal-link" target="_self" rel="noopener nofollow">1.3 เทคโนโลยีของ FPGA (FPGA Technologies &amp; Architectures)</a>
<br><a data-tooltip-position="top" aria-label="1.4 การแบ่งระดับและตระกูลของ FPGA ในปัจจุบัน (FPGA Product Tiering)" data-href="#1.4 การแบ่งระดับและตระกูลของ FPGA ในปัจจุบัน (FPGA Product Tiering)" href="chapter1-introduction-to-fpga.html#1.4_การแบ่งระดับและตระกูลของ_FPGA_ในปัจจุบัน_(FPGA_Product_Tiering)_0" class="internal-link" target="_self" rel="noopener nofollow">1.4 การแบ่งระดับและตระกูลของ FPGA ในปัจจุบัน (FPGA Product Tiering)</a> <br><a data-tooltip-position="top" aria-label="1.4.1 FPGA (Only) Series" data-href="#1.4.1 FPGA (Only) Series" href="chapter1-introduction-to-fpga.html#1.4.1_FPGA_(Only)_Series_0" class="internal-link" target="_self" rel="noopener nofollow">1.4.1 FPGA (Only) Series</a>
<br><a data-tooltip-position="top" aria-label="1.4.2 SoC FPGA Series (FPGA ที่รวม CPU Hard Processor System ไว้ในชิปเดียว)" data-href="#1.4.2 SoC FPGA Series (FPGA ที่รวม CPU Hard Processor System ไว้ในชิปเดียว)" href="chapter1-introduction-to-fpga.html#1.4.2_SoC_FPGA_Series_(FPGA_ที่รวม_CPU_Hard_Processor_System_ไว้ในชิปเดียว)_0" class="internal-link" target="_self" rel="noopener nofollow">1.4.2 SoC FPGA Series (FPGA ที่รวม CPU Hard Processor System ไว้ในชิปเดียว)</a> <br><a data-tooltip-position="top" aria-label="2 FPGA Internal Architecture &amp; Resources" data-href="#2 FPGA Internal Architecture &amp; Resources" href="chapter1-introduction-to-fpga.html#2_FPGA_Internal_Architecture_&amp;_Resources_0" class="internal-link" target="_self" rel="noopener nofollow">2 FPGA Internal Architecture &amp; Resources</a> <br><a data-tooltip-position="top" aria-label="2.2 องค์ประกอบพื้นฐาน: LUT &amp; Flip-Flop" data-href="#2.2 องค์ประกอบพื้นฐาน: LUT &amp; Flip-Flop" href="chapter1-introduction-to-fpga.html#2.2_องค์ประกอบพื้นฐาน_LUT_&amp;_Flip-Flop_0" class="internal-link" target="_self" rel="noopener nofollow">2.2 องค์ประกอบพื้นฐาน: LUT &amp; Flip-Flop</a> <br><a data-tooltip-position="top" aria-label="2.2.1 Look-Up Table (LUT)" data-href="#2.2.1 Look-Up Table (LUT)" href="chapter1-introduction-to-fpga.html#2.2.1_Look-Up_Table_(LUT)_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.1 Look-Up Table (LUT)</a>
<br><a data-tooltip-position="top" aria-label="2.2.2 Flip-Flop (FF) / Registers" data-href="#2.2.2 Flip-Flop (FF) / Registers" href="chapter1-introduction-to-fpga.html#2.2.2_Flip-Flop_(FF)_/_Registers_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.2 Flip-Flop (FF) / Registers</a> <br><a data-tooltip-position="top" aria-label="2.3 หน่วยความจำภายใน (Block RAM)" data-href="#2.3 หน่วยความจำภายใน (Block RAM)" href="chapter1-introduction-to-fpga.html#2.3_หน่วยความจำภายใน_(Block_RAM)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3 หน่วยความจำภายใน (Block RAM)</a> <br><a data-tooltip-position="top" aria-label="2.3.2 ตารางเปรียบเทียบขนาด BRAM ใน FPGA แต่ละรุ่น (โดยประมาณ)" data-href="#2.3.2 ตารางเปรียบเทียบขนาด BRAM ใน FPGA แต่ละรุ่น (โดยประมาณ)" href="chapter1-introduction-to-fpga.html#2.3.2_ตารางเปรียบเทียบขนาด_BRAM_ใน_FPGA_แต่ละรุ่น_(โดยประมาณ)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3.2 ตารางเปรียบเทียบขนาด BRAM ใน FPGA แต่ละรุ่น (โดยประมาณ)</a>
<br><a data-tooltip-position="top" aria-label="2.3.3 การเลือกใช้งาน BRAM และ DDR (Application Use-cases)" data-href="#2.3.3 การเลือกใช้งาน BRAM และ DDR (Application Use-cases)" href="chapter1-introduction-to-fpga.html#2.3.3_การเลือกใช้งาน_BRAM_และ_DDR_(Application_Use-cases)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3.3 การเลือกใช้งาน BRAM และ DDR (Application Use-cases)</a> <br><a data-tooltip-position="top" aria-label="2.4 บล็อกประมวลผลคณิตศาสตร์ (DSP Blocks)" data-href="#2.4 บล็อกประมวลผลคณิตศาสตร์ (DSP Blocks)" href="chapter1-introduction-to-fpga.html#2.4_บล็อกประมวลผลคณิตศาสตร์_(DSP_Blocks)_0" class="internal-link" target="_self" rel="noopener nofollow">2.4 บล็อกประมวลผลคณิตศาสตร์ (DSP Blocks)</a>
<br><a data-tooltip-position="top" aria-label="2.5 ระบบสัญญาณนาฬิกา (PLL &amp; Clock Network)" data-href="#2.5 ระบบสัญญาณนาฬิกา (PLL &amp; Clock Network)" href="chapter1-introduction-to-fpga.html#2.5_ระบบสัญญาณนาฬิกา_(PLL_&amp;_Clock_Network)_0" class="internal-link" target="_self" rel="noopener nofollow">2.5 ระบบสัญญาณนาฬิกา (PLL &amp; Clock Network)</a>
<br><a data-tooltip-position="top" aria-label="2.6 บล็อกอินพุตและเอาต์พุต (I/O Banks &amp; IO Voltage Standards)" data-href="#2.6 บล็อกอินพุตและเอาต์พุต (I/O Banks &amp; IO Voltage Standards)" href="chapter1-introduction-to-fpga.html#2.6_บล็อกอินพุตและเอาต์พุต_(I/O_Banks_&amp;_IO_Voltage_Standards)_0" class="internal-link" target="_self" rel="noopener nofollow">2.6 บล็อกอินพุตและเอาต์พุต (I/O Banks &amp; IO Voltage Standards)</a> <br><a data-tooltip-position="top" aria-label="2.6.2 มาตรฐานแรงดัน I/O ทั่วไปและการประยุกต์ใช้งาน (I/O Standards &amp; Target Applications)" data-href="#2.6.2 มาตรฐานแรงดัน I/O ทั่วไปและการประยุกต์ใช้งาน (I/O Standards &amp; Target Applications)" href="chapter1-introduction-to-fpga.html#2.6.2_มาตรฐานแรงดัน_I/O_ทั่วไปและการประยุกต์ใช้งาน_(I/O_Standards_&amp;_Target_Applications)_0" class="internal-link" target="_self" rel="noopener nofollow">2.6.2 มาตรฐานแรงดัน I/O ทั่วไปและการประยุกต์ใช้งาน (I/O Standards &amp; Target Applications)</a>
<br><a data-tooltip-position="top" aria-label="2.6.3 รายละเอียดสเปก I/O Banks และแอปพลิเคชันแยกตามตระกูลชิปยอดนิยม" data-href="#2.6.3 รายละเอียดสเปก I/O Banks และแอปพลิเคชันแยกตามตระกูลชิปยอดนิยม" href="chapter1-introduction-to-fpga.html#2.6.3_รายละเอียดสเปก_I/O_Banks_และแอปพลิเคชันแยกตามตระกูลชิปยอดนิยม_0" class="internal-link" target="_self" rel="noopener nofollow">2.6.3 รายละเอียดสเปก I/O Banks และแอปพลิเคชันแยกตามตระกูลชิปยอดนิยม</a>
<br><a data-tooltip-position="top" aria-label="2.6.4 พินฟังก์ชันพิเศษเฉพาะทางบน FPGA (Special Function &amp; Dedicated Pins)" data-href="#2.6.4 พินฟังก์ชันพิเศษเฉพาะทางบน FPGA (Special Function &amp; Dedicated Pins)" href="chapter1-introduction-to-fpga.html#2.6.4_พินฟังก์ชันพิเศษเฉพาะทางบน_FPGA_(Special_Function_&amp;_Dedicated_Pins)_0" class="internal-link" target="_self" rel="noopener nofollow">2.6.4 พินฟังก์ชันพิเศษเฉพาะทางบน FPGA (Special Function &amp; Dedicated Pins)</a>
<br><a data-tooltip-position="top" aria-label="2.6.5 สรุปข้อควรระวังสำคัญในการออกแบบฮาร์ดแวร์ (Hardware Design Pitfalls)" data-href="#2.6.5 สรุปข้อควรระวังสำคัญในการออกแบบฮาร์ดแวร์ (Hardware Design Pitfalls)" href="chapter1-introduction-to-fpga.html#2.6.5_สรุปข้อควรระวังสำคัญในการออกแบบฮาร์ดแวร์_(Hardware_Design_Pitfalls)_0" class="internal-link" target="_self" rel="noopener nofollow">2.6.5 สรุปข้อควรระวังสำคัญในการออกแบบฮาร์ดแวร์ (Hardware Design Pitfalls)</a> <br><a data-tooltip-position="top" aria-label="2.7 วงจรคอนฟิกูเรชัน (Configuration Circuits &amp; Modes)" data-href="#2.7 วงจรคอนฟิกูเรชัน (Configuration Circuits &amp; Modes)" href="chapter1-introduction-to-fpga.html#2.7_วงจรคอนฟิกูเรชัน_(Configuration_Circuits_&amp;_Modes)_0" class="internal-link" target="_self" rel="noopener nofollow">2.7 วงจรคอนฟิกูเรชัน (Configuration Circuits &amp; Modes)</a>
<br><a data-tooltip-position="top" aria-label="2.8 ทรัพยากรฟังก์ชันพิเศษฮาร์ดแวร์ (Special Hard IP Functions)" data-href="#2.8 ทรัพยากรฟังก์ชันพิเศษฮาร์ดแวร์ (Special Hard IP Functions)" href="chapter1-introduction-to-fpga.html#2.8_ทรัพยากรฟังก์ชันพิเศษฮาร์ดแวร์_(Special_Hard_IP_Functions)_0" class="internal-link" target="_self" rel="noopener nofollow">2.8 ทรัพยากรฟังก์ชันพิเศษฮาร์ดแวร์ (Special Hard IP Functions)</a> <br><a data-tooltip-position="top" aria-label="3 FPGA Design Flow" data-href="#3 FPGA Design Flow" href="chapter1-introduction-to-fpga.html#3_FPGA_Design_Flow_0" class="internal-link" target="_self" rel="noopener nofollow">3 FPGA Design Flow</a><br>
- <a data-tooltip-position="top" aria-label="3.1.2 รายละเอียดแต่ละขั้นตอน:" data-href="#3.1.2 รายละเอียดแต่ละขั้นตอน:" href="chapter1-introduction-to-fpga.html#3.1.2_รายละเอียดแต่ละขั้นตอน_0" class="internal-link" target="_self" rel="noopener nofollow">3.1.2 รายละเอียดแต่ละขั้นตอน:</a>
<br><a data-tooltip-position="top" aria-label="4 EDA Tools Landscape &amp; Ecosystem" data-href="#4 EDA Tools Landscape &amp; Ecosystem" href="chapter1-introduction-to-fpga.html#4_EDA_Tools_Landscape_&amp;_Ecosystem_0" class="internal-link" target="_self" rel="noopener nofollow">4 EDA Tools Landscape &amp; Ecosystem</a>
FPGA ย่อมาจาก Field Programmable Gate Array คือ ชิปวงจรรวม (Integrated Circuit: IC) ชนิดหนึ่งที่โครงสร้างฮาร์ดแวร์ภายในสามารถถูกโปรแกรมหรือกำหนดค่าใหม่ (Reconfigure) ได้โดยผู้ใช้งานหลังจากผลิตมาจากโรงงานแล้ว ("In the Field") ต่างจาก ASIC (Application-Specific Integrated Circuit) หรือชิปเฉพาะทาง (เช่น ชิปประมวลผลผลใน iPhone หรือการ์ดจอ) ที่โครงสร้างวงจรถูกสร้างตายตัวมาจากโรงงาน ไม่สามารถแก้ไขได้อีก
Field Programmable: สามารถโปรแกรมแก้ไขโครงสร้างฮาร์ดแวร์ได้หลังจากผลิตมาแล้ว
Logic Blocks: บล็อกประมวลผลตรรกะพื้นฐานภายใน FPGA ที่สามารถตั้งค่าให้ทำงานตามตรรกะใดๆ ก็ได้
Interconnects: สายสัญญาณเชื่อมต่อภายในแบบโปรแกรมเส้นทางได้ (Programmable Routing) สำหรับเชื่อมต่อ Logic Blocks แต่ละตัวเข้าด้วยกัน
<br><img alt="fpga.png" src="img/fpga.png" target="_self" style="width: 534px; max-width: 100%;">ในการเลือกใช้งานชิปประมวลผลสำหรับระบบฝังตัว (Embedded Systems) หรือระบบประมวลผลประสิทธิภาพสูง เรามักจะเปรียบเทียบระหว่าง MCU (Microcontroller Unit), CPU / GPU, FPGA และ ASIC
MCU: เหมาะกับงานควบคุมทั่วไป (Control Logic), งานที่ต้องการราคาถูก, พัฒนาไว เช่น อ่านเซนเซอร์เปิด-ปิดไฟ
CPU / GPU: เหมาะกับการประมวลผลข้อมูลลำดับเชิงซ้อน (Sequential Execution) หรืองาน Graphic / AI model ใหญ่ๆ ที่ประมวลผลบนซอฟต์แวร์
FPGA: เหมาะกับงานที่ต้องการ True Parallel Processing (การประมวลผลขนานแท้จริงระดับฮาร์ดแวร์), งานที่ต้องการตอบสนองระดับ Microsecond / Nanosecond (Deterministic Real-time), งานประมวลผลสัญญาณดิจิทัล (DSP), Video Processing, High-Frequency Trading, Communication Protocols (5G/SDR) และการสร้าง Prototype สำหรับ ASIC
ASIC: เหมาะกับผลิตภัณฑ์ที่มีปริมาณการผลิตระดับหลายล้านชิ้น (Mass Production) ที่ต้องการประสิทธิภาพสูงสุดและประหยัดพลังงานที่สุด
จำแนกตามเทคโนโลยีการจัดเก็บข้อมูลการคอนฟิก (Configuration Technology):ข้อสังเกตเพิ่มเติมเกี่ยวกับตระกูลชิป: Intel MAX 10: สถาปัตยกรรมภายในเป็น SRAM FPGA แต่มี Internal Flash สำหรับโหลด Configuration อยู่ในชิปตัวเดียวกัน ทำให้มีคุณสมบัติ Instant-on เหมือน Flash FPGA
Lattice iCE40: เป็น SRAM-based FPGA (มักโหลด Configuration จาก SPI Flash ภายนอก) ในขณะที่ MachXO เป็น Flash-based FPGA SoC FPGA ผสานรวม Hard IP Core ของ Processor (เช่น ARM Cortex หรือ RISC-V) เข้ากับ FPGA Logic Fabric ช่วยให้เขียนซอฟต์แวร์ระบบปฏิบัติการ (Linux/RTOS) ควบคู่กับการเร่งความเร็วด้วยฮาร์ดแวร์ (Hardware Acceleration) ได้ในชิปเดียวโครงสร้างทรัพยากรภายในของ FPGA ประกอบไปด้วยองค์ประกอบหลักที่สำคัญ ดังนี้:
Logic Array Block (LUT + FF)
BRAM (Block RAM)
DSP
PLL
Hard IP(PCIe, DDR Ctrl, Transceivers, etc.)<br>
<img alt="fpga-2.png" src="img/fpga-2.png" target="_self"> LUT คือวงจรความทรงจำขนาดเล็ก (SRAM-based Truth Table) ที่ใช้จำลองวงจรตรรกะแบบ Combinational Logic (เช่น AND, OR, XOR, MUX หรือสมการลอจิกซับซ้อน)
ใน FPGA ยุคปัจจุบัน มักใช้ 4-input LUT หรือ 6-input LUT
LUT 6-input สามารถทำป้อนอินพุตได้ 6 สัญญาณ และสร้างเอาต์พุตตาม Truth Table ที่โปรแกรมไว้ได้ทุกรูปแบบ ใช้สำหรับเก็บบันทึกสถานะข้อมูล (Sequential Logic) ขนานคู่ไปกับ LUT
ทำงานตามจังหวะสัญญาณนาฬิกา (Clock-driven)
หนึ่งบล็อกประมวลผลพื้นฐาน (เรียกว่า Slice ใน Xilinx หรือ ALM ใน Intel) มักประกอบด้วย LUT และ Flip-Flop ทำงานร่วมกัน
Configurable Logic Block : <br><img alt="xilinx-CLB.png" src="img/xilinx-clb.png" target="_self" style="width: 480px; max-width: 100%;">Example : zynq-7000 CLB specification<br>
<img alt="zynq-7000-CLB.png" src="img/zynq-7000-clb.png" target="_self" style="width: 833px; max-width: 100%;">FPGA มีเตรียมหน่วยความจำในภายในไว้ให้ แต่ขนาดไม่ได้ใหญ่มาก กรณีที่ต้องการใช้ขนาดใหญ่จำเป็นต้องต่อ external memory เอาDSP Block (Digital Signal Processing Block) หรือบางค่ายเรียกว่า MULT / MAC Block
เป็นวงจรฮาร์ดแวร์คูณและบวกสำเร็จรูป (Hardened Multiplier &amp; Accumulator: MAC)
ทำไมต้องมี DSP Block?: การใช้ LUT แบบธรรมดามาสร้างวงจรคูณเลข (Multiplier) จะสิ้นเปลือง LUT จำนวนมากและทำงานได้ช้า ดังนั้น FPGA จึงใส่ชิปคูณฮาร์ดแวร์เฉพาะทางนี้มาให้
งานที่เหมาะกับ DSP: การทำ FIR/IIR Filter, Fast Fourier Transform (FFT), Matrix Multiplication, การประมวลผลภาพ และการคำนวณเวกเตอร์ใน AI/ML Global Clock Network: เป็นสายสัญญาณพิเศษภายใน FPGA ที่ออกแบบมาให้กระจายสัญญาณนาฬิกาไปยัง Flip-Flop ทุกตัวทั่วทั้งชิปด้วยความหน่วง (Skew) ที่ต่ำที่สุด ป้องกันปัญหา Timing Violation<br>
<img alt="global-clock.png" src="img/global-clock.png" target="_self"> PLL (Phase-Locked Loop) / MMCM (Mixed-Mode Clock Manager): ทำหน้าที่ปรับเพิ่ม/ลดความถี่สัญญาณนาฬิกา (Frequency Synthesis/Multiplication/Division)
ปรับเปลี่ยนเฟสสัญญาณ (Phase Shift) เพื่อเช็ค Timing และจัดการกับ Clock Jitter
สิ่งสำคัญคือ PLL ไม่ได้เป็น Global Clock Network แต่ เอาต์พุตของ PLL สามารถป้อนเข้า Global Clock Network ได้ พิน I/O ของ FPGA จะถูกจัดกลุ่มแบ่งออกเป็น I/O Banks โดยในแต่ละ Bank สามารถตั้งค่าแรงดันไฟฟ้าจ่ายพอร์ต (VCCIO) แยกอิสระจากกันได้ เพื่อให้อินเทอร์เฟซเชื่อมต่อกับอุปกรณ์ภายนอกต่างแรงดันได้อย่างยืดหยุ่น<br><img alt="io-bank.png" src="img/io-bank.png" target="_self" style="width: 609px; max-width: 100%;">มาตรฐานสัญญาณไฟฟ้าแต่ละประเภทถูกออกแบบมาเพื่อรองรับงานเฉพาะทางที่แตกต่างกัน ทั้งด้านความเร็ว การประหยัดพลังงาน และการทนต่อสัญญาณรบกวน:note : เวลาต่อวงจรต้องคำนึงค่าพวกนี้ (Vih, Vil, Voh, Vol) ถ้าไม่ match กันก็จะสื่อสารกันไม่ได้(ค่ามาตรฐานทั่วไปตาม JEDEC LVTTL/LVCMOS Interface Standard — JESD8C.01 (3.3V), JESD8-5A (2.5V), JESD8-7A (1.8V), JESD8-11A (1.5V), JESD8-12A (1.2V) ตัวเลขจริงอาจต่างกันเล็กน้อยในแต่ละผู้ผลิตชิป ให้ตรวจสอบ Datasheet ของอุปกรณ์นั้นๆ ประกอบด้วย)ปัญหาคลาสสิก: TTL Output (5V) → Standard CMOS Input (5V)
แม้ทั้งสองฝั่งใช้ VCC = 5V เท่ากัน แต่ สเปก VOH ของ TTL ต่ำกว่า VIH ที่ Standard CMOS ต้องการ: TTL รับประกัน VOH (min) เพียง 2.4V เท่านั้น
แต่ Standard CMOS (เช่น 4000B series, 74HC) ต้องการ VIH (min) สูงถึง ≈3.5V (≈0.7×VCC) ถึงจะยอมรับว่าเป็น Logic '1'
ผลคือช่วงแรงดัน 2.4V–3.5V เป็นโซนไม่แน่นอน (Undefined Region) ที่ CMOS อาจอ่านค่าผิดเป็น Logic '0' ได้ ทำให้วงจรทำงานผิดพลาด หมายเหตุ: ปัญหานี้เป็นเรื่องของ TTL/CMOS Logic Family แบบดั้งเดิม (5V) ไม่เกี่ยวข้องกับ LVCMOS ของ FPGA สมัยใหม่ ซึ่งออกแบบ VOH/VIH ให้ใกล้ Rail มากกว่าและไม่มีตระกูล TTL แท้ๆ หลงเหลืออยู่ใน FPGA I/O ปัจจุบัน
<br><img alt="standart-logic.png" src="img/standart-logic.png" target="_self" style="width: 906px; max-width: 100%;">ตารางเปรียบเทียบประเภท I/O Bank, ช่วงแรงดัน VCCIO, ข้อกำหนดเฉพาะ และตัวอย่างการนำไปใช้ในงานจริง:นอกจากพิน User I/O ทั่วไปที่ใช้เชื่อมต่อสัญญาณตามต้องการแล้ว FPGA ยังมี Special Dedicated Pins ซึ่งทำหน้าที่เฉพาะทางระดับฮาร์ดแวร์ที่ไม่สามารถเปลี่ยนหน้าที่ผ่านซอฟต์แวร์ได้ ดังนี้:ข้อควรระวังในการจ่ายไฟ VCCIO และต่อพิน FPGA: ความต่างของ HR vs HP Banks ใน Xilinx UltraScale+:
หากจ่ายไฟ หรือป้อนสัญญาณ 3.3V เข้า HP Bank (High Performance) โดยตรง ชิปอาจ พังเสียหายถาวร (Over-voltage Damage)
LVDS Termination Resistor:
การใช้งาน LVDS อินพุต จะต้องตรวจสอบว่าชิปมีตัวต้านทาน ภายในชิป (เช่น DIFF_TERM = TRUE ใน Xilinx หรือ Internal OCT ใน Intel) หรือไม่ หากไม่มี ต้องวางตัวต้านทาน Differential Resistor ที่ฝั่ง Receiver บนแผ่น PCB
VREF Voltage Pins:
ในการใช้งานพอร์ตร่วมกับหน่วยความจำ DDR (เช่น SSTL15/SSTL18) จะต้องต่อพิน ซึ่งมีแรงดันเป็นครึ่งหนึ่งของ (เช่น สำหรับ DDR3 1.5V) เข้ากับ I/O Bank นั้นๆ ด้วย เนื่องจาก SRAM FPGA จะสูญเสียข้อมูลเมื่อปิดไฟ จึงต้องมีวงจรและโหมดในการโหลด Bitstream (ไฟล์คอนฟิก .bit / .sof) เมื่อจ่ายไฟเปิดเครื่อง:
JTAG Mode: ใช้สำหรับการปรับแต่งและ Debug ในขั้นตอนการพัฒนาผ่านสายโปรแกรม (เช่น Xilinx Platform Cable, USB-Blaster)
Master SPI Flash Mode: FPGA ทำหน้าที่เป็น Master อ่านไฟล์ Bitstream จาก External SPI Flash เข้ามาใส่ตัวเองตอน Power-up
Slave Serial / Parallel Mode: มี Processor ภายนอกหรือ Microcontroller เป็นตัวส่งข้อมูล Bitstream มาให้ FPGA
A : program configuration data to SPI flash
B : when power on, fpga will read configuration data from SPI flash<br><img alt="configuration.png" src="img/configuration.png" target="_self" style="width: 660px; max-width: 100%;">นอกเหนือจาก Logic Fabric ทั่วไป ชิป FPGA ระดับกลางถึงสูงมักฝัง Hard IP Core (วงจรฮาร์ดแวร์จริงที่ไม่ต้องเสีย Logic LUT) เพื่อประสิทธิภาพการทำงานสูงสุด โดยมีตัวอย่าง Hard IP ที่สำคัญและใช้งานแพร่หลายดังนี้:<br><img alt="special-function.png" src="img/special-function.png" target="_self" style="width: 531px; max-width: 100%;">สรุปเพิ่มเติมเกี่ยวกับ DSP Slice ในฐานะ Special Hard IP:
DSP Slice / DSP Block ถือเป็นหนึ่งใน Special Hard IP ที่สำคัญที่สุดของ FPGA เนื่องจากถูกสร้างเป็น Dedicated Hardened ASIC Circuit เพื่อทำหน้าที่คูณและบวก (MAC: Multiply-Accumulate) โดยในแต่ละตระกูล/ซีรีส์ จะมีขนาดบิตของ Multiplier และสถาปัตยกรรมภายในแตกต่างกันอย่างชัดเจน เช่น: Xilinx Spartan-6: DSP48A1 (ขนาดตัวคูณ bit)
Xilinx 7-Series / Zynq-7000: DSP48E1 (ขนาดตัวคูณ bit + Accumulator 48 bit)
Xilinx UltraScale / UltraScale+: DSP48E2 (ขนาดตัวคูณ bit + ALU/Wide XOR)
AMD Versal ACAP: DSP58 (ขนาดตัวคูณ bit + รองรับ Native INT8 / FP32 Vector Math)
Intel Stratix 10 / Agilex: Variable Precision DSP ( bit หรือ Single-Precision IEEE-754 Floating Point) กระบวนการเปลี่ยนไฟล์โค้ดภาษา HDL ไปเป็นฮาร์ดแวร์จริงบนชิป FPGA ประกอบด้วยขั้นตอนต่างๆ ดังนี้:
RTL Specification &amp; Coding: เขียนอธิบายวงจรด้วยภาษา Verilog, VHDL หรือ SystemVerilog
RTL Simulation: จำลองการทำงานของโค้ดฮาร์ดแวร์ร่วมกับไฟล์ Testbench โดยยังไม่ต้องใช้ชิปจริง เพื่อเช็คความถูกต้องของตรรกะ
Synthesis: EDA Tool ทำการสังเคราะห์แปลงโค้ดเชิงตรรกะ ให้กลายเป็นวงจรตรรกะระดับเกต (Gate-Level Netlist) ที่ประกอบไปด้วย LUTs, Flip-Flops และ MUXs
Place and Route (P&amp;R): Place: กำหนดตำแหน่งที่จะวาง LUT/FF แต่ละตัวลงในพิกัดจริงบนชิป FPGA
Route: เชื่อมต่อสายสัญญาณ (Interconnects) ระหว่าง บล็อกต่างๆ Static Timing Analysis (STA): ตรวจสอบว่าสัญญาณเดินทางทันตามรอบเวลา Clock ที่กำหนดหรือไม่
Bitstream Generation: แปลงข้อมูลการจัดวางวงจรให้เป็นไฟล์ไบนารี Bitstream (.bit สำหรับ Xilinx หรือ .sof สำหรับ Intel)
Program Device: อัปโหลดไฟล์ Bitstream เข้าไปทดสอบในบอร์ดจริงผ่านสายโปรแกรม JTAG
ซอฟต์แวร์เครื่องมือสำหรับพัฒนา FPGA แบ่งออกเป็นค่ายหลักและ Open-Source Toolchain:]]></description><link>chapter1-introduction-to-fpga.html</link><guid isPermaLink="false">chapter1-introduction-to-fpga.md</guid><pubDate>Mon, 24 Aug 2026 00:02:15 GMT</pubDate><enclosure url="img/fpga.png" length="0" type="image/png"/><content:encoded>&lt;figure&gt;&lt;img src="img/fpga.png"&gt;&lt;/figure&gt;</content:encoded></item><item><title><![CDATA[chapter2-fundamental-of-digital-logic]]></title><description><![CDATA[
<a data-tooltip-position="top" aria-label="1 พื้นฐานวงจรดิจิทัล สัญญาณนาฬิกา และการวิเคราะห์ไทม์มิ่ง (Digital Logic Fundamentals &amp; Timing Analysis)" data-href="#1 พื้นฐานวงจรดิจิทัล สัญญาณนาฬิกา และการวิเคราะห์ไทม์มิ่ง (Digital Logic Fundamentals &amp; Timing Analysis)" href="chapter2-fundamental-of-digital-logic.html#1_พื้นฐานวงจรดิจิทัล_สัญญาณนาฬิกา_และการวิเคราะห์ไทม์มิ่ง_(Digital_Logic_Fundamentals_&amp;_Timing_Analysis)_0" class="internal-link" target="_self" rel="noopener nofollow">1 พื้นฐานวงจรดิจิทัล สัญญาณนาฬิกา และการวิเคราะห์ไทม์มิ่ง (Digital Logic Fundamentals &amp; Timing Analysis)</a>
<br><a data-tooltip-position="top" aria-label="2 ระบบเลขฐานและการแทนค่าข้อมูล (Number Systems &amp; Data Representation)" data-href="#2 ระบบเลขฐานและการแทนค่าข้อมูล (Number Systems &amp; Data Representation)" href="chapter2-fundamental-of-digital-logic.html#2_ระบบเลขฐานและการแทนค่าข้อมูล_(Number_Systems_&amp;_Data_Representation)_0" class="internal-link" target="_self" rel="noopener nofollow">2 ระบบเลขฐานและการแทนค่าข้อมูล (Number Systems &amp; Data Representation)</a> <br><a data-tooltip-position="top" aria-label="2.1 เลขฐานสองและเลขฐานสิบหก (Binary &amp; Hexadecimal)" data-href="#2.1 เลขฐานสองและเลขฐานสิบหก (Binary &amp; Hexadecimal)" href="chapter2-fundamental-of-digital-logic.html#2.1_เลขฐานสองและเลขฐานสิบหก_(Binary_&amp;_Hexadecimal)_0" class="internal-link" target="_self" rel="noopener nofollow">2.1 เลขฐานสองและเลขฐานสิบหก (Binary &amp; Hexadecimal)</a>
<br><a data-tooltip-position="top" aria-label="2.2 การแทนค่าเลขติดลบ (Signed Number Representations)" data-href="#2.2 การแทนค่าเลขติดลบ (Signed Number Representations)" href="chapter2-fundamental-of-digital-logic.html#2.2_การแทนค่าเลขติดลบ_(Signed_Number_Representations)_0" class="internal-link" target="_self" rel="noopener nofollow">2.2 การแทนค่าเลขติดลบ (Signed Number Representations)</a> <br><a data-tooltip-position="top" aria-label="2.2.1 วิธีการแทนค่าเลขติดลบ 3 รูปแบบหลัก" data-href="#2.2.1 วิธีการแทนค่าเลขติดลบ 3 รูปแบบหลัก" href="chapter2-fundamental-of-digital-logic.html#2.2.1_วิธีการแทนค่าเลขติดลบ_3_รูปแบบหลัก_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.1 วิธีการแทนค่าเลขติดลบ 3 รูปแบบหลัก</a>
<br><a data-tooltip-position="top" aria-label="2.2.2 ตารางเปรียบเทียบการแปลงรหัสไบนารี 8-bit ในแต่ละระบบ (8-bit Signed Representation Comparison)" data-href="#2.2.2 ตารางเปรียบเทียบการแปลงรหัสไบนารี 8-bit ในแต่ละระบบ (8-bit Signed Representation Comparison)" href="chapter2-fundamental-of-digital-logic.html#2.2.2_ตารางเปรียบเทียบการแปลงรหัสไบนารี_8-bit_ในแต่ละระบบ_(8-bit_Signed_Representation_Comparison)_0" class="internal-link" target="_self" rel="noopener nofollow">2.2.2 ตารางเปรียบเทียบการแปลงรหัสไบนารี 8-bit ในแต่ละระบบ (8-bit Signed Representation Comparison)</a> <br><a data-tooltip-position="top" aria-label="2.3 การบวกและการลบเลขฐานสอง (Binary Addition &amp; Subtraction)" data-href="#2.3 การบวกและการลบเลขฐานสอง (Binary Addition &amp; Subtraction)" href="chapter2-fundamental-of-digital-logic.html#2.3_การบวกและการลบเลขฐานสอง_(Binary_Addition_&amp;_Subtraction)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3 การบวกและการลบเลขฐานสอง (Binary Addition &amp; Subtraction)</a> <br><a data-tooltip-position="top" aria-label="2.3.1 หลักการบวกเลขฐานสอง (Binary Addition Rule)" data-href="#2.3.1 หลักการบวกเลขฐานสอง (Binary Addition Rule)" href="chapter2-fundamental-of-digital-logic.html#2.3.1_หลักการบวกเลขฐานสอง_(Binary_Addition_Rule)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3.1 หลักการบวกเลขฐานสอง (Binary Addition Rule)</a>
<br><a data-tooltip-position="top" aria-label="2.3.2 การลบเลขฐานสองด้วย 2's Complement (Subtraction via Addition)" data-href="#2.3.2 การลบเลขฐานสองด้วย 2's Complement (Subtraction via Addition)" href="chapter2-fundamental-of-digital-logic.html#2.3.2_การลบเลขฐานสองด้วย_2's_Complement_(Subtraction_via_Addition)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3.2 การลบเลขฐานสองด้วย 2's Complement (Subtraction via Addition)</a>
<br><a data-tooltip-position="top" aria-label="2.3.3 การตรวจจับ Overflow ในการบวกเลข Signed (Signed Overflow Detection)" data-href="#2.3.3 การตรวจจับ Overflow ในการบวกเลข Signed (Signed Overflow Detection)" href="chapter2-fundamental-of-digital-logic.html#2.3.3_การตรวจจับ_Overflow_ในการบวกเลข_Signed_(Signed_Overflow_Detection)_0" class="internal-link" target="_self" rel="noopener nofollow">2.3.3 การตรวจจับ Overflow ในการบวกเลข Signed (Signed Overflow Detection)</a> <br><a data-tooltip-position="top" aria-label="2.4 ระบบทศนิยม: Fixed-Point vs Floating-Point" data-href="#2.4 ระบบทศนิยม: Fixed-Point vs Floating-Point" href="chapter2-fundamental-of-digital-logic.html#2.4_ระบบทศนิยม_Fixed-Point_vs_Floating-Point_0" class="internal-link" target="_self" rel="noopener nofollow">2.4 ระบบทศนิยม: Fixed-Point vs Floating-Point</a> <br><a data-tooltip-position="top" aria-label="2.4.1 ตัวอย่าง: การแปลงค่าเป็น Fixed-Point (Q4.4)" data-href="#2.4.1 ตัวอย่าง: การแปลงค่าเป็น Fixed-Point (Q4.4)" href="chapter2-fundamental-of-digital-logic.html#2.4.1_ตัวอย่าง_การแปลงค่าเป็น_Fixed-Point_(Q4.4)_0" class="internal-link" target="_self" rel="noopener nofollow">2.4.1 ตัวอย่าง: การแปลงค่าเป็น Fixed-Point (Q4.4)</a>
<br><a data-tooltip-position="top" aria-label="2.4.2 ตัวอย่าง: การแปลงค่าเป็น Floating-Point (IEEE 754 Single Precision)" data-href="#2.4.2 ตัวอย่าง: การแปลงค่าเป็น Floating-Point (IEEE 754 Single Precision)" href="chapter2-fundamental-of-digital-logic.html#2.4.2_ตัวอย่าง_การแปลงค่าเป็น_Floating-Point_(IEEE_754_Single_Precision)_0" class="internal-link" target="_self" rel="noopener nofollow">2.4.2 ตัวอย่าง: การแปลงค่าเป็น Floating-Point (IEEE 754 Single Precision)</a> <br><a data-tooltip-position="top" aria-label="3 พื้นฐานลอจิกเกตและการทำงานบน FPGA (Basic Logic Gates &amp; FPGA LUT Implementation)" data-href="#3 พื้นฐานลอจิกเกตและการทำงานบน FPGA (Basic Logic Gates &amp; FPGA LUT Implementation)" href="chapter2-fundamental-of-digital-logic.html#3_พื้นฐานลอจิกเกตและการทำงานบน_FPGA_(Basic_Logic_Gates_&amp;_FPGA_LUT_Implementation)_0" class="internal-link" target="_self" rel="noopener nofollow">3 พื้นฐานลอจิกเกตและการทำงานบน FPGA (Basic Logic Gates &amp; FPGA LUT Implementation)</a> <br><a data-tooltip-position="top" aria-label="3.1 ลอจิกเกตพื้นฐาน (Basic Logic Gates)" data-href="#3.1 ลอจิกเกตพื้นฐาน (Basic Logic Gates)" href="chapter2-fundamental-of-digital-logic.html#3.1_ลอจิกเกตพื้นฐาน_(Basic_Logic_Gates)_0" class="internal-link" target="_self" rel="noopener nofollow">3.1 ลอจิกเกตพื้นฐาน (Basic Logic Gates)</a> <br><a data-tooltip-position="top" aria-label="3.1.1 ตารางสรุปลอจิกเกตพื้นฐาน (Logic Gates Summary Table)" data-href="#3.1.1 ตารางสรุปลอจิกเกตพื้นฐาน (Logic Gates Summary Table)" href="chapter2-fundamental-of-digital-logic.html#3.1.1_ตารางสรุปลอจิกเกตพื้นฐาน_(Logic_Gates_Summary_Table)_0" class="internal-link" target="_self" rel="noopener nofollow">3.1.1 ตารางสรุปลอจิกเกตพื้นฐาน (Logic Gates Summary Table)</a>
<br><a data-tooltip-position="top" aria-label="3.1.2 สัญลักษณ์ทางฮาร์ดแวร์และตารางความจริงของแต่ละเกต (Logic Gate Diagrams &amp; Truth Tables)" data-href="#3.1.2 สัญลักษณ์ทางฮาร์ดแวร์และตารางความจริงของแต่ละเกต (Logic Gate Diagrams &amp; Truth Tables)" href="chapter2-fundamental-of-digital-logic.html#3.1.2_สัญลักษณ์ทางฮาร์ดแวร์และตารางความจริงของแต่ละเกต_(Logic_Gate_Diagrams_&amp;_Truth_Tables)_0" class="internal-link" target="_self" rel="noopener nofollow">3.1.2 สัญลักษณ์ทางฮาร์ดแวร์และตารางความจริงของแต่ละเกต (Logic Gate Diagrams &amp; Truth Tables)</a> <br><a data-tooltip-position="top" aria-label="3.2 การจัดเก็บลอจิกเกตใน FPGA ผ่าน Look-Up Table (LUT Mapping)" data-href="#3.2 การจัดเก็บลอจิกเกตใน FPGA ผ่าน Look-Up Table (LUT Mapping)" href="chapter2-fundamental-of-digital-logic.html#3.2_การจัดเก็บลอจิกเกตใน_FPGA_ผ่าน_Look-Up_Table_(LUT_Mapping)_0" class="internal-link" target="_self" rel="noopener nofollow">3.2 การจัดเก็บลอจิกเกตใน FPGA ผ่าน Look-Up Table (LUT Mapping)</a> <br><a data-tooltip-position="top" aria-label="4 สถาปัตยกรรมและประเภทของวงจรดิจิทัล (Digital Logic Architectures: Combinational vs Sequential Logic)" data-href="#4 สถาปัตยกรรมและประเภทของวงจรดิจิทัล (Digital Logic Architectures: Combinational vs Sequential Logic)" href="chapter2-fundamental-of-digital-logic.html#4_สถาปัตยกรรมและประเภทของวงจรดิจิทัล_(Digital_Logic_Architectures_Combinational_vs_Sequential_Logic)_0" class="internal-link" target="_self" rel="noopener nofollow">4 สถาปัตยกรรมและประเภทของวงจรดิจิทัล (Digital Logic Architectures: Combinational vs Sequential Logic)</a> <br><a data-tooltip-position="top" aria-label="4.2 ตารางเปรียบเทียบ Combinational vs Sequential Logic" data-href="#4.2 ตารางเปรียบเทียบ Combinational vs Sequential Logic" href="chapter2-fundamental-of-digital-logic.html#4.2_ตารางเปรียบเทียบ_Combinational_vs_Sequential_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">4.2 ตารางเปรียบเทียบ Combinational vs Sequential Logic</a>
<br><a data-tooltip-position="top" aria-label="4.3 Combinational Logic" data-href="#4.3 Combinational Logic" href="chapter2-fundamental-of-digital-logic.html#4.3_Combinational_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">4.3 Combinational Logic</a> <br><a data-tooltip-position="top" aria-label="4.3.2 ตัวอย่างวงจร Combinational ที่สำคัญใน FPGA" data-href="#4.3.2 ตัวอย่างวงจร Combinational ที่สำคัญใน FPGA" href="chapter2-fundamental-of-digital-logic.html#4.3.2_ตัวอย่างวงจร_Combinational_ที่สำคัญใน_FPGA_0" class="internal-link" target="_self" rel="noopener nofollow">4.3.2 ตัวอย่างวงจร Combinational ที่สำคัญใน FPGA</a> <br><a data-tooltip-position="top" aria-label="4.4 Sequential Logic" data-href="#4.4 Sequential Logic" href="chapter2-fundamental-of-digital-logic.html#4.4_Sequential_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">4.4 Sequential Logic</a> <br><a data-tooltip-position="top" aria-label="4.4.1 ส่วนประกอบหลักของ Sequential Logic" data-href="#4.4.1 ส่วนประกอบหลักของ Sequential Logic" href="chapter2-fundamental-of-digital-logic.html#4.4.1_ส่วนประกอบหลักของ_Sequential_Logic_0" class="internal-link" target="_self" rel="noopener nofollow">4.4.1 ส่วนประกอบหลักของ Sequential Logic</a>
<br><a data-tooltip-position="top" aria-label="4.4.2 ข้อควรระวัง: การเผลอสร้าง Latch (Unintentional Latch Inference)" data-href="#4.4.2 ข้อควรระวัง: การเผลอสร้าง Latch (Unintentional Latch Inference)" href="chapter2-fundamental-of-digital-logic.html#4.4.2_ข้อควรระวัง_การเผลอสร้าง_Latch_(Unintentional_Latch_Inference)_0" class="internal-link" target="_self" rel="noopener nofollow">4.4.2 ข้อควรระวัง: การเผลอสร้าง Latch (Unintentional Latch Inference)</a> <br><a data-tooltip-position="top" aria-label="5 การออกแบบสัญญาณนาฬิกาและรีเซ็ต (Clock &amp; Reset Design)" data-href="#5 การออกแบบสัญญาณนาฬิกาและรีเซ็ต (Clock &amp; Reset Design)" href="chapter2-fundamental-of-digital-logic.html#5_การออกแบบสัญญาณนาฬิกาและรีเซ็ต_(Clock_&amp;_Reset_Design)_0" class="internal-link" target="_self" rel="noopener nofollow">5 การออกแบบสัญญาณนาฬิกาและรีเซ็ต (Clock &amp; Reset Design)</a> <br><a data-tooltip-position="top" aria-label="5.1 สัญญาณนาฬิกาและ Synchronous Design Standard" data-href="#5.1 สัญญาณนาฬิกาและ Synchronous Design Standard" href="chapter2-fundamental-of-digital-logic.html#5.1_สัญญาณนาฬิกาและ_Synchronous_Design_Standard_0" class="internal-link" target="_self" rel="noopener nofollow">5.1 สัญญาณนาฬิกาและ Synchronous Design Standard</a> <br><a data-tooltip-position="top" aria-label="5.1.2 วงจร Synchronizer สำหรับสัญญาณ 1 บิต (2-Flip-Flop Synchronizer)" data-href="#5.1.2 วงจร Synchronizer สำหรับสัญญาณ 1 บิต (2-Flip-Flop Synchronizer)" href="chapter2-fundamental-of-digital-logic.html#5.1.2_วงจร_Synchronizer_สำหรับสัญญาณ_1_บิต_(2-Flip-Flop_Synchronizer)_0" class="internal-link" target="_self" rel="noopener nofollow">5.1.2 วงจร Synchronizer สำหรับสัญญาณ 1 บิต (2-Flip-Flop Synchronizer)</a> <br><a data-tooltip-position="top" aria-label="5.2 สัญญาณรีเซ็ต (Reset Signal Design)" data-href="#5.2 สัญญาณรีเซ็ต (Reset Signal Design)" href="chapter2-fundamental-of-digital-logic.html#5.2_สัญญาณรีเซ็ต_(Reset_Signal_Design)_0" class="internal-link" target="_self" rel="noopener nofollow">5.2 สัญญาณรีเซ็ต (Reset Signal Design)</a> <br><a data-tooltip-position="top" aria-label="5.2.2 ตัวอย่างโค้ด VHDL เปรียบเทียบ Synchronous Reset vs Asynchronous Reset" data-href="#5.2.2 ตัวอย่างโค้ด VHDL เปรียบเทียบ Synchronous Reset vs Asynchronous Reset" href="chapter2-fundamental-of-digital-logic.html#5.2.2_ตัวอย่างโค้ด_VHDL_เปรียบเทียบ_Synchronous_Reset_vs_Asynchronous_Reset_0" class="internal-link" target="_self" rel="noopener nofollow">5.2.2 ตัวอย่างโค้ด VHDL เปรียบเทียบ Synchronous Reset vs Asynchronous Reset</a> <br><a data-tooltip-position="top" aria-label="6 Finite State Machines - FSM" data-href="#6 Finite State Machines - FSM" href="chapter2-fundamental-of-digital-logic.html#6_Finite_State_Machines_-_FSM_0" class="internal-link" target="_self" rel="noopener nofollow">6 Finite State Machines - FSM</a> <br><a data-tooltip-position="top" aria-label="6.1 ประเภทของ FSM: Moore Machine vs Mealy Machine" data-href="#6.1 ประเภทของ FSM: Moore Machine vs Mealy Machine" href="chapter2-fundamental-of-digital-logic.html#6.1_ประเภทของ_FSM_Moore_Machine_vs_Mealy_Machine_0" class="internal-link" target="_self" rel="noopener nofollow">6.1 ประเภทของ FSM: Moore Machine vs Mealy Machine</a> <br><a data-tooltip-position="top" aria-label="6.1.2 ตัวอย่างโค้ด: Moore FSM ควบคุมไฟจราจรอย่างง่าย (Traffic Light)" data-href="#6.1.2 ตัวอย่างโค้ด: Moore FSM ควบคุมไฟจราจรอย่างง่าย (Traffic Light)" href="chapter2-fundamental-of-digital-logic.html#6.1.2_ตัวอย่างโค้ด_Moore_FSM_ควบคุมไฟจราจรอย่างง่าย_(Traffic_Light)_0" class="internal-link" target="_self" rel="noopener nofollow">6.1.2 ตัวอย่างโค้ด: Moore FSM ควบคุมไฟจราจรอย่างง่าย (Traffic Light)</a> <br><a data-tooltip-position="top" aria-label="7 การวิเคราะห์เวลาและสัญญาณรูปคลื่น (Timing Analysis &amp; Waveform Analysis)" data-href="#7 การวิเคราะห์เวลาและสัญญาณรูปคลื่น (Timing Analysis &amp; Waveform Analysis)" href="chapter2-fundamental-of-digital-logic.html#7_การวิเคราะห์เวลาและสัญญาณรูปคลื่น_(Timing_Analysis_&amp;_Waveform_Analysis)_0" class="internal-link" target="_self" rel="noopener nofollow">7 การวิเคราะห์เวลาและสัญญาณรูปคลื่น (Timing Analysis &amp; Waveform Analysis)</a> <br><a data-tooltip-position="top" aria-label="7.1 การอ่าน รูปคลื่นสัญญาณ (Timing Diagram Reading)" data-href="#7.1 การอ่าน รูปคลื่นสัญญาณ (Timing Diagram Reading)" href="chapter2-fundamental-of-digital-logic.html#7.1_การอ่าน_รูปคลื่นสัญญาณ_(Timing_Diagram_Reading)_0" class="internal-link" target="_self" rel="noopener nofollow">7.1 การอ่าน รูปคลื่นสัญญาณ (Timing Diagram Reading)</a> <br><a data-tooltip-position="top" aria-label="7.1.1 รูปคลื่นสัญญาณพื้นฐาน (Basic Shift Register Waveform)" data-href="#7.1.1 รูปคลื่นสัญญาณพื้นฐาน (Basic Shift Register Waveform)" href="chapter2-fundamental-of-digital-logic.html#7.1.1_รูปคลื่นสัญญาณพื้นฐาน_(Basic_Shift_Register_Waveform)_0" class="internal-link" target="_self" rel="noopener nofollow">7.1.1 รูปคลื่นสัญญาณพื้นฐาน (Basic Shift Register Waveform)</a>
<br><a data-tooltip-position="top" aria-label="7.1.2 ตัวอย่างรูปคลื่นการทำงานจริง: AXI4-Lite Protocol Waveform" data-href="#7.1.2 ตัวอย่างรูปคลื่นการทำงานจริง: AXI4-Lite Protocol Waveform" href="chapter2-fundamental-of-digital-logic.html#7.1.2_ตัวอย่างรูปคลื่นการทำงานจริง_AXI4-Lite_Protocol_Waveform_0" class="internal-link" target="_self" rel="noopener nofollow">7.1.2 ตัวอย่างรูปคลื่นการทำงานจริง: AXI4-Lite Protocol Waveform</a> <br><a data-tooltip-position="top" aria-label="7.2 ข้อกำหนดเวลา Setup Time และ Hold Time" data-href="#7.2 ข้อกำหนดเวลา Setup Time และ Hold Time" href="chapter2-fundamental-of-digital-logic.html#7.2_ข้อกำหนดเวลา_Setup_Time_และ_Hold_Time_0" class="internal-link" target="_self" rel="noopener nofollow">7.2 ข้อกำหนดเวลา Setup Time และ Hold Time</a>
<br><a data-tooltip-position="top" aria-label="7.3 Timing Slack &amp; Maximum Frequency ($F_{max}$)" data-href="#7.3 Timing Slack &amp; Maximum Frequency ($F_{max}$)" href="chapter2-fundamental-of-digital-logic.html#7.3_Timing_Slack_&amp;_Maximum_Frequency_($F_{max}$)_0" class="internal-link" target="_self" rel="noopener nofollow">7.3 Timing Slack &amp; Maximum Frequency ($F_{max}$)</a> เนื้อหาบทนี้มุ่งเน้นการปูพื้นฐาน Digital Logic Fundamentals ที่จำเป็นสำหรับการเขียนภาษาบรรยายฮาร์ดแวร์ (VHDL/Verilog) บน FPGA ตั้งแต่ระบบเลขฐาน วงจรตรรกะ Combinational &amp; Sequential ไปจนถึงสถาปัตยกรรม Synchronous Design และการวิเคราะห์ Timing Closureในระบบดิจิทัล สัญญาณไฟฟ้ามีเพียง 2 สถานะคือ HIGH (1) และ LOW (0) การเข้าใจรูปแบบการแทนค่าข้อมูลจึงเป็นจุดเริ่มต้นที่สำคัญในการออกแบบฮาร์ดแวร์:
Binary System (Base 2): ประกอบด้วยตัวเลข 0 และ 1 Bit (Binary Digit): หน่วยที่เล็กที่สุด (0 หรือ 1)
Byte: 8 bits (เช่น 1101_0010)
Word: ขึ้นอยู่กับสถาปัตยกรรม (16 bits, 32 bits หรือ 64 bits) Hexadecimal System (Base 16): ใช้ตัวเลข 0-9 และ A-F แทนค่า 10-15 โดย 1 Hex Digit จะแทนเลขฐานสองได้ 4 bits พอดี ช่วยให้มนุษย์อ่านบัสข้อมูลขนาดยาวได้ง่ายขึ้น
ในการออกแบบระบบดิจิทัลและการคำนวณใน FPGA การแทนค่าตัวเลขติดลบ (Signed Numbers) สามารถทำได้ 3 วิธีหลัก: Sign and Magnitude (บิตเครื่องหมาย + ขนาด): ใช้บิตซ้ายสุด (MSB) เป็น Sign Bit () และบิตที่เหลือเก็บค่าขนาดตัวเลข (Magnitude)
ตัวอย่าง (4 บิต): 0101, 1101
ข้อเสีย: มีค่า ศูนย์ 2 ค่า (+0 = 0000, -0 = 1000) และสร้างวงจรบวก/ลบทางฮาร์ดแวร์ได้ซับซ้อน 1's Complement (คอมพลีเมนต์หนึ่ง): สลับบิตทั้งหมดจาก 0 เป็น 1 และ 1 เป็น 0 เพื่อแทนค่าติดลบ
ตัวอย่าง (4 บิต): 0101, 1010
ข้อเสีย: ยังคงมีค่า ศูนย์ 2 ค่า (+0 = 0000, -0 = 1111) และต้องใช้วงจร End-around Carry ในการบวกเลข 2's Complement (คอมพลีเมนต์สอง) — ⭐ วิธีมาตรฐานที่นิยมใช้แพร่หลายที่สุด: นำค่า 1's Complement มา บวกเพิ่มด้วย 1
ข้อดีสำคัญ: มีค่า ศูนย์เพียงค่าเดียว (0000) สามารถใช้ วงจรบวกฮาร์ดแวร์ (Adder Circuit) ชุดเดียวกัน คำนวณได้ทั้งการบวกและการลบเลข โดยไม่ต้องสร้างวงจรลบแยกต่างหาก ช่วยประหยัดพื้นที่ LUTs ใน FPGA ได้อย่างมาก <br><img alt="complement.png" src="img/complement.png" target="_self" style="width: 420px; max-width: 100%;">Cyclic depiction of unsigned (white ring),
ones'-complement (orange),
and two's-complement (blue) values of 4-bit integersสรุปข้อสังเกตจากตาราง 8-bit: 1111_1011: Sign &amp; Magnitude: MSB=1 (ลบ), บิตที่เหลือ 111 1011 = 123 ค่าคือ 1's Complement: MSB=1 (ลบ), สลับบิตกลับได้ 0000 0100 = 4 ค่าคือ 2's Complement: MSB=1 (ลบ), สลับบิตกลับแล้ว +1 ได้ 0000 0101 = 5 ค่าคือ 2's Complement กำจัดปัญหา / ทำให้ขอบเขตเก็บค่าได้กว้างกว่า ( ถึง ) ในขณะที่วิธีอื่นเก็บได้เพียง ถึง การบวกเลขฐานสองมีกฎพื้นฐาน 4 กรณี (เหมือนวงจร Half Adder):เมื่อบวกเลขหลายบิต แต่ละหลักต้องนำ Carry จากหลักก่อนหน้า มาบวกร่วมด้วย (วงจร Full Adder ที่รับอินพุต 3 ตัว คือ A, B, และ Carry-in)ตัวอย่าง: บวกเลขฐานสอง 4 บิตแบบ Unsigned Carry: 0 1 1 0 0 1 0 1 (5) + 0 0 1 1 (3) ----------- 1 0 0 0 (8)
ไล่ทีละบิตจาก LSB (ขวาสุด): (Sum=0, Carry=1) → (Sum=0, Carry=1) → (Sum=0, Carry=1) → (Sum=1, Carry=0) → ผลลัพธ์ 1000 ✅จุดเด่นสำคัญของ 2's Complement (ตามหัวข้อ 2.2) คือทำให้ใช้ วงจรบวก (Adder) ตัวเดียวกัน คำนวณการลบได้ โดยใช้หลักการ:
ตัวอย่าง: ลบเลขฐานสอง 4 บิตแบบ Signed ผลลัพธ์ ✅ ตรงกับการลบเลขปกติ — นี่คือเหตุผลที่ Hardware ไม่จำเป็นต้องมีวงจรลบ (Subtractor) แยกต่างหากจากวงจรบวกเลยใน FPGAเนื่องจากเลข Signed ถูกจำกัดขอบเขตค่าตามจำนวนบิต (เช่น 4 บิต Signed เก็บได้แค่ ถึง ตามหัวข้อ 2.2) การบวกเลขสองจำนวนที่ มีเครื่องหมายเดียวกัน อาจได้ผลลัพธ์ที่เกินขอบเขตนั้นได้ เรียกว่า Overflowกฎการตรวจจับ: (ใช้ XOR)ตัวอย่าง: Overflow เมื่อบวก ด้วย Signed 4 บิต Carry: 0 1 1 0 0 1 0 1 (+5) + 0 1 0 0 (+4) ----------- 1 0 0 1 (ผลลัพธ์ดิบ) Carry-in เข้า MSB (บิตที่ 3) , Carry-out จาก MSB → เกิด Overflow!
ตรวจสอบ: ผลลัพธ์ 1001 เมื่ออ่านแบบ Signed 4 บิต (2's Complement) จะได้ ซึ่งผิดอย่างชัดเจน (ค่าจริงคือ ซึ่งเกินขอบเขต สูงสุดของ Signed 4 บิต) จึงต้องอาศัย Overflow Flag เป็นตัวเตือนว่าผลลัพธ์ที่ได้ใช้งานไม่ได้
ในทางปฏิบัติ ไม่ต้องคำนวณ Overflow ด้วยมือแบบนี้ — เครื่องมือ Synthesis และไลบรารี ieee.numeric_std ใน VHDL จัดการเรื่อง Carry/Overflow ให้อัตโนมัติเมื่อใช้ signed/unsigned type บวกลบกันตามปกติ (ดูตัวอย่างโค้ดจริงในหัวข้อ 4.3.1.1.3) หน้าที่ของผู้ออกแบบคือ เลือกจำนวนบิตของ Signal ให้กว้างเพียงพอ ไม่ให้ค่าที่คำนวณได้เกินขอบเขตที่ Signal เก็บได้ตั้งแต่แรก
กรณีใช้ไลบรารีรุ่นเก่า (Legacy Library): std_logic_unsigned
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all; โค้ดรุ่นเก่าหรือโปรเจกต์เก่าบางส่วนยังใช้ std_logic_unsigned แทน ieee.numeric_std เพื่อบวก/ลบ std_logic_vector ได้โดยตรง (ไม่ต้องแปลงเป็น signed/unsigned type ก่อน) ซึ่งมีข้อควรระวังสำคัญ: ไม่ใช่มาตรฐาน IEEE จริงๆ: แม้จะอยู่ภายใต้ library ieee แต่ std_logic_unsigned เป็นไลบรารีที่ Synopsys สร้างขึ้นเอง (Vendor-specific/Non-standard) ไม่ได้รับรองโดย IEEE อย่างเป็นทางการ ต่างจาก numeric_std ที่เป็นมาตรฐาน IEEE 1076.3 แท้จริง
ตีความเป็น Unsigned เสมอ: std_logic_unsigned Overload Operator (+, -, &gt;, &lt;, =) ให้ทำงานกับ std_logic_vector ได้โดยตรงเลย โดยตีความเป็น Unsigned เท่านั้น ไม่มีทางเลือกตีความแบบ Signed ในตัวมันเอง หากต้องการ Signed ต้องพึ่ง std_logic_arith เพิ่ม ซึ่งอาจชนกับ std_logic_unsigned ได้หากใช้ปนกัน (Ambiguous Operator Error)
Overflow ยังคงต้องระวังเหมือนเดิม: ไม่ว่าจะใช้ไลบรารีไหน หลักการ Overflow ยังเป็นไปตามกฎ Carry-in XOR Carry-out ของ MSB เหมือนเดิมทุกประการ — std_logic_unsigned ไม่ได้มีกลไกป้องกัน Overflow ที่ดีไปกว่า numeric_std แต่อย่างใด คำแนะนำ: สำหรับโค้ดใหม่ ควรใช้ ieee.numeric_std เป็นมาตรฐาน (ตามที่ใช้ในตัวอย่างทั้งหมดของเอกสารนี้) เพราะเป็นมาตรฐานสากลที่ Tool สมัยใหม่ทุกตัวรองรับ และแยก Type signed/unsigned ชัดเจนกว่า ส่วน std_logic_unsigned มีไว้เพื่อความเข้าใจเมื่อต้องอ่านหรือ Maintain โค้ดเก่าเท่านั้น Fixed-Point (จุดตรึง): กำหนดตำแหน่งจุดทศนิยมตายตัว (เช่น Q4.4 = 4-bit Integer + 4-bit Fractional) ข้อดี: สังเคราะห์เป็นฮาร์ดแวร์ได้ง่าย ประหยัด LUTs และใช้ DSP Block ทำงานได้เร็วมาก (นิยมสุดใน FPGA) Floating-Point (จุดลอย): ตามมาตรฐาน IEEE 754 (Single Precision 32-bit / Double 64-bit) ข้อดี: ขอบเขตช่วงตัวเลขกว้างมาก
ข้อเสีย: วงจรซับซ้อน ใช้ทรัพยากร FPGA สูง และเกิด Latency มากขึ้น รูปแบบ Q4.4 หมายถึง 8 บิตทั้งหมด แบ่งเป็น 4 บิตส่วนจำนวนเต็ม (Integer) และ 4 บิตส่วนทศนิยม (Fractional) โดยค่าทศนิยมจะถูกคูณด้วย (Scale Factor) แล้วปัดเป็นจำนวนเต็มก่อนเก็บลงรีจิสเตอร์ตัวอย่างที่ 1: แปลงเลข เป็น Q4.4สรุปการแบ่งบิต ในรูปแบบ Q4.4:ตรวจสอบย้อนกลับ (Decode): 0101_1100 → Integer = 0101 = 5, Fractional = 1100 = 12 → ค่า ✅ ตรงกับค่าตั้งต้นตัวอย่างที่ 2: Signed Fixed-Point ค่าติดลบ (2's Complement, เพิ่ม Sign bit เป็น 9 บิต)Fixed-Point ที่เป็นเลขติดลบ ยังคงใช้ 2's Complement เช่นเดียวกับเลขจำนวนเต็ม Signed ทั่วไป (ตามหัวข้อ 2.2) เพียงแค่ "จินตนาการ" ตำแหน่งจุดทศนิยมแทรกเข้าไปในบิตแพทเทิร์นเดิมเท่านั้น ตัว Hardware Adder/Subtractor ไม่รู้และไม่สนใจตำแหน่งจุดทศนิยม จึงบวกลบบิตแบบ Binary ปกติเหมือนเดิมทุกประการในตัวอย่างนี้จะเพิ่ม Sign bit อีก 1 บิต นำหน้า Q4.4 เดิม รวมเป็น 9 บิต (เรียกได้ว่าเป็น Signed Q4.4 หรือ 1.4.4 format) เพื่อให้เห็นตำแหน่ง Sign bit แยกออกมาชัดเจนจาก Integer/Fractionตัวอย่าง: แปลง เป็น Signed Q4.4 (9 บิต)ตรวจสอบย้อนกลับ: มองทั้ง 9 บิต 1_1010_0100 เป็นเลข Signed 2's Complement (ไม่สนใจจุดทศนิยมก่อน) → MSB=1 → เป็นค่าลบ → คำนวณ Magnitude โดย Invert + บวก 1 กลับ: 1_1010_0100 → invert → 0_0101_1011 → +1 → 0_0101_1100 → หารด้วย Scale Factor → ✅ ตรงกับค่าตั้งต้นทำไมเพิ่ม Sign bit เป็น 9 บิต? การเพิ่ม Sign bit แยกต่างหาก (Sign-Extend) ทำให้เห็นตำแหน่งเครื่องหมายชัดเจนขึ้นในเชิงการอธิบาย แต่ในทางปฏิบัติเวลาออกแบบวงจรจริง มักไม่แยก Sign bit ออกมาต่างหาก — ระบบ Q4.4 แบบ Signed 8 บิตเดิม (ที่ MSB ของ Integer field ทำหน้าที่เป็น Sign bit ไปในตัวอยู่แล้วตามคุณสมบัติของ 2's Complement) ก็เพียงพอและประหยัดบิตกว่า การเพิ่มเป็น 9 บิตจึงเทียบเท่ากับการทำ Sign Extension ของเลข 8 บิตเดิมเท่านั้น
รูปแบบ IEEE 754 Single Precision (32 บิต) แบ่งเป็น 3 ส่วน: Sign (1 บิต) + Exponent (8 บิต, Bias = 127) + Mantissa (23 บิต)ตัวอย่าง: แปลงเลข เป็น IEEE 754 Single Precisionผลลัพธ์รวม 32 บิต: 0 10000001 01110000000000000000000 ตัวอย่าง: ค่าติดลบ ใน IEEE 754ต่างจาก Fixed-Point ตรงที่ Floating-Point ไม่ใช้ 2's Complement กับเลขติดลบ — ใช้วิธีที่ง่ายกว่ามาก คือ Sign-Magnitude โดยแค่ สลับ Sign bit จาก 0 เป็น 1 เท่านั้น ส่วน Exponent และ Mantissa เหมือนเดิมทุกบิต (เพราะ Magnitude ของ ไม่เปลี่ยน):ผลลัพธ์รวม 32 บิต: 1 10000001 01110000000000000000000 สังเกตความแตกต่าง: Fixed-Point ( → 8 บิต 0x5C) ใช้พื้นที่น้อยกว่ามาก แต่ครอบคลุมช่วงค่าจำกัด (Q4.4 ครอบคลุมได้แค่ ถึง ) ในขณะที่ Floating-Point ( → 32 บิต 0x40B80000) ใช้พื้นที่มากกว่า 4 เท่า แต่ครอบคลุมช่วงค่าได้กว้างกว่ามหาศาล (ประมาณ ) — นี่คือ Trade-off หลักที่ต้องพิจารณาเวลาเลือกใช้งานบน FPGA
ลอจิกเกตถือเป็น อิฐบล็อกพื้นฐานที่สุดของวงจรดิจิทัล ทั้งหมด ไม่ว่าจะเป็นวงจรประเภท Combinational Logic (ที่ใช้ Gate ประกอบเป็น MUX/Decoder/Adder) หรือ Sequential Logic (ที่ใช้ Gate ในการคำนวณ Next State Logic หน้า Flip-Flop) ข้อแตกต่างระหว่าง ASIC และ FPGA:
ในวงจรรวมแบบสั่งทำพิเศษ (ASIC) ลอจิกเกตจะถูกสร้างจากทรานซิสเตอร์จริงบนซิลิกอน แต่ใน FPGA ตรรกะเกตทั้งหมดจะถูกสังเคราะห์และจัดเก็บเอาต์พุตไว้ใน Look-Up Tables (LUTs)
<br><img alt="gate_and.png" src="img/gate_and.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="gate_or.png" src="img/gate_or.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="gate_not.png" src="img/gate_not.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="gate_nand.png" src="img/gate_nand.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="gate_nor.png" src="img/gate_nor.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="gate_xor.png" src="img/gate_xor.png" target="_self" style="width: 520px; max-width: 100%;"><br><img alt="gate_xnor.png" src="img/gate_xnor.png" target="_self" style="width: 520px; max-width: 100%;">ในการเขียนโค้ด VHDL หรือ Verilog เครื่องมือ Synthesis Tool (เช่น Xilinx Vivado หรือ Intel Quartus) จะไม่ได้ต่อเกตจริงทีละตัว แต่จะทำการ แมปตารางความจริง (Truth Table) ของเกตหรือฟังก์ชันตรรกะทั้งหมด ลงในหน่วยความจำ SRAM cells ของ Look-Up Table (LUT) LUT4: สามารถรองรับฟังก์ชันตรรกะของอินพุต 4 บิตใดๆ ก็ได้ (จัดเก็บผลลัพธ์ 16 บิตใน SRAM)
LUT6: รองรับฟังก์ชันตรรกะของอินพุต 6 บิตใดๆ ก็ได้ (จัดเก็บผลลัพธ์ 64 บิตใน SRAM)
ข้อดีของ FPGA: ไม่ว่าสมการจะซับซ้อนแค่ไหน (เช่น AND, OR, XOR ต่อรวมกันหลายชั้น) ขอเพียงมีอินพุตไม่เกินจำนวนพินของ LUT ตัว FPGA จะประมวลผลผ่าน LUT เพียง 1 propagation delay เท่ากันเสมอ
การออกแบบวงจรดิจิทัลบน FPGA แบ่งประเภทสถาปัตยกรรมวงจรออกเป็น 2 ประเภทหลัก ได้แก่ Combinational Logic (ไร้หน่วยความจำ) และ Sequential Logic (มีหน่วยความจำและใช้นาฬิกา)<br><img alt="logic_type.png" src="img/logic_type.png" target="_self">Combinational Logic คือวงจรที่เอาต์พุตขึ้นอยู่กับ ค่าอินพุตในขณะนั้นทันที (ไม่มีสภาวะความจำ/ไม่ต้องใช้ Clock)<br><img alt="combinational_logic.png" src="img/combinational_logic.png" target="_self">
Multiplexer (MUX): สวิตช์เลือกสัญญาณอินพุต 1 จากหลายทางมาออกเอาต์พุต 1 ทาง ตามพิน Select
Demultiplexer (DEMUX): วงจรตรงข้าม MUX กระจายอินพุต 1 ช่องไปยังเอาต์พุตหลายช่อง
Decoder: แปลงรหัสอินพุตขนาด บิตเป็นเอาต์พุตขนาด บิต (เช่น 2-to-4 Line Decoder)
Encoder &amp; Priority Encoder: แปลงสัญญาณหลายช่องให้เป็นรหัสขนาด บิต โดย Priority Encoder จะให้ความสำคัญกับช่องสัญญาณสูงสุดก่อนเสมอ
Binary Adder &amp; Subtractor: วงจรบวกและลบเลขฐานสอง (Half Adder, Full Adder, Ripple Carry Adder, Carry Lookahead Adder)
-- MUX 4 ออก 1 ใช้รูปแบบ Conditional Signal Assignment (when-else)
o_y &lt;= i_a when i_sel = "00" else i_b when i_sel = "01" else i_c when i_sel = "10" else i_d; -- กรณีอื่นๆ (i_sel = "11")
-- แปลงรหัส 2 บิตเป็นสัญญาณ Active-High 4 ช่อง
process(i_sel)
begin case i_sel is when "00" =&gt; o_dec &lt;= "0001"; when "01" =&gt; o_dec &lt;= "0010"; when "10" =&gt; o_dec &lt;= "0100"; when others =&gt; o_dec &lt;= "1000"; -- "11" end case;
end process;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL; -- วงจรบวกเลขฐานสองขนาด 4 บิต
o_sum &lt;= std_logic_vector(unsigned(i_a) + unsigned(i_b));
Sequential Logic คือวงจรที่มี สถานะความจำ (Memory State) โดยเอาต์พุตจะขึ้นอยู่กับทั้ง อินพุตปัจจุบัน และ สถานะเดิมในอดีต ผ่านจังหวะสัญญาณนาฬิกา (Clock)
D Flip-Flop (Data Flip-Flop): เป็น Flip-Flop หลักใน FPGA ข้อมูลอินพุต จะถูกส่งผ่านไปยังเอาต์พุต เฉพาะที่ขอบขาขึ้น (Rising Edge) ของ Clock
Registers: กลุ่มของ Flip-Flop หลายๆ ตัวที่นำมาต่อขนานกันเพื่อเก็บบันทึกชุดข้อมูลหลายบิต (เช่น 8-bit Register, 32-bit Register)
Shift Registers: นำ D Flip-Flop มาต่ออนุกรมกันเพื่อเลื่อนบิตข้อมูลไปทางซ้ายหรือขวาตามรอบ Clock (SISO, SIPO, PISO, PIPO)
Counters: วงจรนับค่า (Binary Counter และ Gray Code Counter สำหรับ Async FIFO)
process(clk, rst_n)
begin if rst_n = '0' then q &lt;= '0'; elsif rising_edge(clk) then if en = '1' then q &lt;= d; end if; end if;
end process;
process(clk, rst_n)
begin if rst_n = '0' then s_shift_reg &lt;= (others =&gt; '0'); elsif rising_edge(clk) then s_shift_reg &lt;= s_shift_reg(6 downto 0) &amp; i_din; end if;
end process; o_parallel_data &lt;= s_shift_reg;
process(clk, rst_n)
begin if rst_n = '0' then s_cnt &lt;= (others =&gt; '0'); elsif rising_edge(clk) then if i_up_down = '1' then s_cnt &lt;= s_cnt + 1; -- นับขึ้น else s_cnt &lt;= s_cnt - 1; -- นับลง end if; end if;
end process; o_count &lt;= std_logic_vector(s_cnt);
Latch เป็นวงจร Sequential อีกชนิดหนึ่งที่ต่างจาก Flip-Flop ตรงที่ Transparent ตามระดับสัญญาณ (Level-Sensitive) ไม่ใช่ที่ขอบสัญญาณนาฬิกา (Edge-Sensitive) แบบ Flip-Flop โดยทั่วไป FPGA Design ควรหลีกเลี่ยงการใช้ Latch โดยเด็ดขาด เพราะ:
LUT/FF Cell ใน FPGA ถูกออกแบบมาให้ทำงานเป็น Edge-Triggered Flip-Flop เป็นหลัก การ Infer Latch มักทำให้ต้องใช้ทรัพยากรพิเศษหรือทำงานได้ไม่เต็มประสิทธิภาพ
Latch วิเคราะห์ Timing (Static Timing Analysis) ได้ยากกว่า Flip-Flop มาก เสี่ยงเกิด Glitch/Race Condition
เครื่องมือ Synthesis (Vivado/Quartus) มักแจ้ง Warning: Latch inferred ซึ่งเกือบทุกครั้งเป็นสัญญาณว่าโค้ดเขียนผิดพลาด ไม่ใช่ตั้งใจ
สาเหตุที่พบบ่อยที่สุด: เขียน process แบบ Combinational (ไม่มี Clock ใน Sensitivity List) แต่ กำหนดค่า Output ไม่ครบทุกเงื่อนไข (Incomplete Assignment) ทำให้ Synthesis Tool ต้อง "จำ" ค่าเดิมไว้เมื่อไม่เข้าเงื่อนไขใดเลย ซึ่งพฤติกรรมการจำค่านี้คือคุณสมบัติของ Latch-- ตัวอย่างโค้ดที่ผิด (เผลอสร้าง Latch)
process(sel, a, b)
begin if sel = '1' then y &lt;= a; end if; -- ผิด! ไม่มี else กรณี sel = '0' ทำให้ Synth ต้องจำค่า y เดิมไว้ -&gt; Infer Latch
end process;
-- ตัวอย่างโค้ดที่ถูกต้อง วิธีที่ 1 (แก้โดยกำหนดค่า Default ก่อนเสมอ)
process(sel, a, b)
begin y &lt;= b; -- Default assignment กันไว้ก่อนเสมอ if sel = '1' then y &lt;= a; end if;
end process;
-- ตัวอย่างโค้ดที่ถูกต้อง วิธีที่ 2 (แก้โดยใช้ else ปิดให้ครบทุกเงื่อนไข)
process(sel, a, b)
begin if sel = '1' then y &lt;= a; else y &lt;= b; end if;
end process;
สัญญาณนาฬิกาและรีเซ็ตถือเป็น หัวใจหลักของการควบคุมระบบ Synchronous System ทั้งหมดใน FPGA
Clock Signal: สัญญาณนาฬิกากำหนดจังหวะการทำงานของระบบ โดยใน FPGA ต้องส่งผ่าน Global Clock Trees เพื่อให้สัญญาณกระจายถึง Flip-Flop ทุกตัวด้วยค่า Clock Skew ต่ำที่สุด
Clock Skew: ความเหลื่อมเวลาของสัญญาณนาฬิกาที่เดินทางไปถึง Flip-Flop แต่ละตัว<br>
<img alt="clock-skew.png" src="img/clock-skew.png" target="_self"> Clock Domain: ขอบเขตสัญญาณนาฬิกา สัญญาณที่เดินทางข้าม Clock Domain ต่างความถี่ เรียกว่า CDC (Clock Domain Crossing) ต้องใช้ Synchronizer หรือ FIFO กั้นเพื่อป้องกันข้อมูลผิดพลาด
เมื่อสัญญาณ 1 บิต (เช่น Flag หรือ Status bit) ต้องเดินทางข้าม Clock Domain การป้อนสัญญาณเข้า Flip-Flop ปลายทางตรงๆ มีความเสี่ยงสูงที่จะทำให้เกิด Metastability (ตามที่กล่าวในหัวข้อ 7.2) เนื่องจากสัญญาณอาจเปลี่ยนค่าในช่วง Setup/Hold Window ของ Flip-Flop ปลายทางพอดีวิธีแก้ที่นิยมและง่ายที่สุดคือ 2-Flip-Flop Synchronizer: นำสัญญาณผ่าน Flip-Flop 2 ตัวเรียงต่อกันในโดเมน Clock ปลายทาง เพื่อให้เวลา (1 Clock Period) สำหรับสถานะ Metastable ที่อาจเกิดขึ้นที่ FF ตัวแรกได้ "สงบ" ลงก่อนที่จะถูกอ่านค่าโดย FF ตัวที่สอง-- 2-FF Synchronizer: ซิงค์สัญญาณ 1 บิต (i_async_signal) จาก Clock Domain อื่น
-- เข้าสู่ Clock Domain ปลายทางที่ขับด้วย clk_dst
process(clk_dst, rst_n)
begin if rst_n = '0' then s_sync_ff1 &lt;= '0'; s_sync_ff2 &lt;= '0'; elsif rising_edge(clk_dst) then s_sync_ff1 &lt;= i_async_signal; -- FF ตัวแรก: จุดเสี่ยง Metastability s_sync_ff2 &lt;= s_sync_ff1; -- FF ตัวที่สอง: ให้เวลา Settle ก่อนใช้งานจริง end if;
end process; o_sync_signal &lt;= s_sync_ff2; -- นำสัญญาณที่ Sync แล้วไปใช้งานต่อในโดเมนนี้
ข้อจำกัดสำคัญ: 2-FF Synchronizer ใช้ได้เฉพาะสัญญาณ 1 บิต หรือสัญญาณที่เปลี่ยนค่าไม่พร้อมกันได้ (เช่น Single-bit Pulse/Flag) เท่านั้น ห้ามใช้กับบัสข้อมูลหลายบิต (เช่น Counter value หรือ Bus data) โดยตรง เพราะแต่ละบิตอาจ Synchronize เสร็จไม่พร้อมกัน ทำให้ค่าที่อ่านได้ผิดเพี้ยน (Bit Skew) — กรณีข้อมูลหลายบิตข้าม Clock Domain ต้องใช้ Async FIFO หรือเทคนิค Handshake แทน
ใช้สำหรับกำหนดสถานะเริ่มต้น (Initial State) ให้กับ Flip-Flop ตอนเปิดเครื่องหรือรีสตาร์ทระบบ:
Active High (rst): ทำงานเมื่อสัญญาณเป็น '1'
Active Low (rst_n): ทำงานเมื่อสัญญาณเป็น '0' (นิยมใช้เป็นมาตรฐานอุตสาหกรรม)
process(clk)
begin if rising_edge(clk) then if rst_n = '0' then -- เช็ค Reset ตามจังหวะ Clock q_out &lt;= (others =&gt; '0'); else q_out &lt;= d_in; end if; end if;
end process;
process(clk, rst_n) -- ใส่ rst_n เข้า Sensitivity List
begin if rst_n = '0' then -- รีเซ็ตทันทีเมื่อ rst_n เป็น '0' q_out &lt;= (others =&gt; '0'); elsif rising_edge(clk) then q_out &lt;= d_in; end if;
end process;
FSM คือรูปแบบการออกแบบ Sequential Logic ที่ได้รับความนิยมสูงสุดใน FPGA สำหรับควบคุมลำดับการทำงานและสถิติต่างๆ (Control Logic &amp; Sequencer)<br><img alt="moore.png" src="img/moore.png" target="_self"><br><img alt="Mealy.png" src="img/mealy.png" target="_self">library IEEE;
use IEEE.STD_LOGIC_1164.ALL; entity traffic_light_fsm is port ( clk : in std_logic; rst_n : in std_logic; o_red : out std_logic; o_yellow : out std_logic; o_green : out std_logic );
end entity; architecture rtl of traffic_light_fsm is -- นิยาม State ทั้งหมดด้วย Enumerated Type (Synthesis Tool จะเลือก State Encoding ให้อัตโนมัติ) type state_type is (S_RED, S_GREEN, S_YELLOW); signal s_current_state, s_next_state : state_type;
begin -- Process 1: State Register (Sequential) - เก็บ State ตามจังหวะ Clock p_state_reg : process(clk, rst_n) begin if rst_n = '0' then s_current_state &lt;= S_RED; elsif rising_edge(clk) then s_current_state &lt;= s_next_state; end if; end process; -- Process 2: Next-State Logic (Combinational) - คำนวณ State ถัดไป p_next_state : process(s_current_state) begin case s_current_state is when S_RED =&gt; s_next_state &lt;= S_GREEN; when S_GREEN =&gt; s_next_state &lt;= S_YELLOW; when S_YELLOW =&gt; s_next_state &lt;= S_RED; when others =&gt; s_next_state &lt;= S_RED; -- Safe default กัน Latch/Unknown State end case; end process; -- Process 3: Output Logic (Combinational, Moore-style) - ขึ้นกับ State ปัจจุบันเท่านั้น p_output_logic : process(s_current_state) begin o_red &lt;= '0'; o_yellow &lt;= '0'; o_green &lt;= '0'; case s_current_state is when S_RED =&gt; o_red &lt;= '1'; when S_GREEN =&gt; o_green &lt;= '1'; when S_YELLOW =&gt; o_yellow &lt;= '1'; end case; end process; end architecture;
สิ่งสำคัญที่สุดในการออกแบบฮาร์ดแวร์ความเร็วสูงคือ Timing Closure (สัญญาณเดินทางทันรอบนาฬิกา) และการอ่าน Timing Diagram เพื่อวิเคราะห์พฤติกรรมวงจรTiming Diagram ใช้แสดงความสัมพันธ์ของสัญญาณไฟฟ้าระหว่างอินพุต เอาต์พุต และ Clock ตามแกนเวลา (Time Axis)ClockData_InShift_Reg0x000x010x020x040x08Valid_Out
Clock Period (): ระยะเวลา 1 รอบของสัญญาณนาฬิกา
Propagation Delay (): ความหน่วงเวลารอคอยตั้งแต่เปลี่ยนอินพุตจนเอาต์พุตตอบสนอง
Setup Window: ช่วงเวลาก่อนขอบ Clock ที่อินพุตต้องนิ่ง
ในการออกแบบระบบบน FPGA ที่เชื่อมต่อกับ Microprocessor (เช่น ARM Cortex ใน Zynq) มาตรฐาน AXI4-Lite ถือเป็นบัสโปรโตคอลมาตรฐานที่นิยมใช้แพร่หลายที่สุด การส่งข้อมูลใน AXI4-Lite อาศัย หลักการ Handshake (VALID / READY)AXI4-Lite Write Transaction Waveform0123456789ACLKAWADDR0x4000_0000AWVALIDAWREADYWDATA0xDEADBEEFWVALIDWREADYBVALIDBREADYWrite Address (AW)Write Data (W)Write Response (B)กฎทองของการ Handshake ใน AXI Protocol:
"การรับส่งข้อมูลจะเกิดขึ้นสำเร็จ (Transfer Done) เฉพาะในรอบขอบขาขึ้นของ ACLK ที่สัญญาณ VALID = 1 และ READY = 1 พร้อมกันเท่านั้น" VALID (สร้างโดยฝั่งส่ง): บ่งบอกว่าข้อมูล/ที่อยู่บนบัสมีความถูกต้องและพร้อมส่งแล้ว
READY (สร้างโดยฝั่งรับ): บ่งบอกว่าปลายทางพร้อมรับข้อมูลเข้าสู่ภายในแล้ว
หาก VALID = 1 แต่ READY = 0 ฝั่งส่งจะต้อง ค้างค่าข้อมูลไว้บนบัส (Wait State) จนกว่าฝั่งรับจะยก READY = 1 ตอบกลับมา Write Address Channel (AW): ส่งตำแหน่ง Address ที่ต้องการเขียน (AWADDR, AWVALID, AWREADY)
Write Data Channel (W): ส่งข้อมูล Data ที่ต้องการเขียน (WDATA, WSTRB, WVALID, WREADY)
Write Response Channel (B): รายงานสถานะการเขียนกลับไปยัง Master (BRESP, BVALID, BREADY)
Read Address Channel (AR): ส่งตำแหน่ง Address ที่ต้องการอ่าน (ARADDR, ARVALID, ARREADY)
Read Data Channel (R): ส่งข้อมูลที่อ่านได้กลับไปยัง Master (RDATA, RRESP, RVALID, RREADY)
Clock : __/‾‾\__/‾‾\__/‾‾\__/‾‾\__
Data Input: ====&lt; Data Valid &gt;==== |&lt;-t_su-&gt;|&lt;-t_h-&gt;| Setup Time (): ระยะเวลาขั้นต่ำที่ข้อมูลอินพุตจะต้อง คงที่และนิ่ง ก่อนที่ขอบสัญญาณนาฬิกา (Clock Edge) จะมาถึง
Hold Time (): ระยะเวลาขั้นต่ำที่ข้อมูลอินพุตจะต้อง คงค้างไว้ หลังจากขอบสัญญาณนาฬิกาผ่านไปแล้ว
Metastability (สภาวะไม่เสถียร): หากข้อมูลเปลี่ยนในช่วงเวลา หรือ เอาต์พุตของ Flip-Flop จะตกอยู่ในสถานะกึ่งกลางระหว่าง 0 และ 1 (Uncertain state) ส่งผลให้ระบบล่มหรือทำงานผิดพลาดได้ Timing Constraints File (.sdc / .xdc): ไฟล์บอก EDA Tool ว่า Clock ในระบบมีความถี่เท่าไร (เช่น 100 MHz = 10 ns Period)
Data Path Delay (): เวลาที่สัญญาณเดินทางจาก Flip-Flop ต้นทาง ผ่าน LUTs ไปยัง Flip-Flop ปลายทาง
Slack (ระยะเวลาคงเหลือ): Positive Slack (): สัญญาณเดินทางทันรอบเวลา (Timing PASSED)
Negative Slack (): สัญญาณเดินทางไม่ทัน (Timing VIOLATED) ระบบจะทำงานผิดพลาดที่ความแน่นั้น ต้องลดความถี่ Clock หรือปรับแก้โค้ด ]]></description><link>chapter2-fundamental-of-digital-logic.html</link><guid isPermaLink="false">chapter2-fundamental-of-digital-logic.md</guid><pubDate>Mon, 24 Aug 2026 00:02:15 GMT</pubDate><enclosure url="img/complement.png" length="0" type="image/png"/><content:encoded>&lt;figure&gt;&lt;img src="img/complement.png"&gt;&lt;/figure&gt;</content:encoded></item></channel></rss>