VHDL十大经典基础功能设计实例代码合集

简介: 本合集精选VHDL十大经典基础设计实例(与门、加法器、D触发器、计数器、分频器、移位寄存器、译码器、MUX、奇偶校验、三段式FSM),覆盖组合/时序逻辑核心场景,每例含可综合、可仿真完整代码,适配Quartus/Vivado,助力硬件新手快速入门实战。(239字)

VHDL十大经典基础功能设计实例代码合集

一、文档概述

VHDL是硬件描述语言核心标准,广泛应用于FPGA、ASIC开发,入门阶段掌握基础逻辑、时序、计数器、状态机等经典模块是硬件开发必备能力。本文整理10个高频经典VHDL工程案例,覆盖组合逻辑、时序逻辑、分频、移位寄存器、状态机等核心场景,每个案例附带可直接综合、仿真的完整代码,适配Quartus、Vivado主流开发工具,适合硬件初学者快速上手实操。

二、十大经典VHDL案例代码演示

案例1:二输入与门(组合逻辑基础)

最简组合逻辑,理解实体、结构体基础语法

library ieee;
use ieee.std_logic_1164.all;

entity and2 is
port(
    a,b : in std_logic;
    y   : out std_logic
);
end entity and2;

architecture behav of and2 is
begin
    y <= a and b;
end architecture behav;

案例2:4位二进制加法器

多位运算基础,掌握矢量信号使用

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity add4 is
port(
    num1,num2 : in std_logic_vector(3 downto 0);
    sum_out   : out std_logic_vector(3 downto 0);
    carry     : out std_logic
);
end add4;

architecture behav of add4 is
signal temp : std_logic_vector(4 downto 0);
begin
    temp <= ('0' & num1) + num2;
    sum_out <= temp(3 downto 0);
    carry <= temp(4);
end behav;

案例3:D触发器(基础时序单元)

时序逻辑入门,时钟、复位信号标准写法

library ieee;
use ieee.std_logic_1164.all;

entity dff is
port(
    clk,rst,d : in std_logic;
    q         : out std_logic
);
end dff;

architecture behav of dff is
begin
process(clk,rst)
begin
    if rst = '1' then
        q <= '0';
    elsif rising_edge(clk) then
        q <= d;
    end if;
end process;
end behav;

案例4:8位同步计数器

掌握时序进程、循环计数逻辑

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity cnt8 is
port(
    clk,rst : in std_logic;
    cnt_out : out std_logic_vector(7 downto 0)
);
end cnt8;

architecture behav of cnt8 is
signal cnt : std_logic_vector(7 downto 0);
begin
process(clk,rst)
begin
    if rst='1' then
        cnt <= (others=>'0');
    elsif rising_edge(clk) then
        cnt <= cnt + 1;
    end if;
end process;
cnt_out <= cnt;
end behav;

案例5:二分频电路

时钟分频最常用基础模块

library ieee;
use ieee.std_logic_1164.all;

entity div2 is
port(
    clk_in,rst : in std_logic;
    clk_out    : out std_logic
);
end div2;

architecture behav of div2 is
signal temp : std_logic;
begin
process(clk_in,rst)
begin
    if rst='1' then
        temp <= '0';
    elsif rising_edge(clk_in) then
        temp <= not temp;
    end if;
end process;
clk_out <= temp;
end behav;

案例6:4位移位寄存器

串行数据传输基础模块

library ieee;
use ieee.std_logic_1164.all;

entity shift4 is
port(
    clk,rst,sin : in std_logic;
    q           : out std_logic_vector(3 downto 0)
);
end shift4;

architecture behav of shift4 is
signal reg : std_logic_vector(3 downto 0);
begin
process(clk,rst)
begin
    if rst='1' then
        reg <= "0000";
    elsif rising_edge(clk) then
        reg <= reg(2 downto 0) & sin;
    end if;
end process;
q <= reg;
end behav;

案例7:3-8译码器

地址译码、外设选通核心电路

library ieee;
use ieee.std_logic_1164.all;

entity decoder38 is
port(
    din : in std_logic_vector(2 downto 0);
    dout: out std_logic_vector(7 downto 0)
);
end decoder38;

architecture behav of decoder38 is
begin
with din select
dout <= "00000001" when "000",
        "00000010" when "001",
        "00000100" when "010",
        "00001000" when "011",
        "00010000" when "100",
        "00100000" when "101",
        "01000000" when "110",
        "10000000" when others;
end behav;

案例8:多路选择器MUX8to1

数据通路切换通用模块

library ieee;
use ieee.std_logic_1164.all;

entity mux8_1 is
port(
    sel : in std_logic_vector(2 downto 0);
    datain : in std_logic_vector(7 downto 0);
    dataout : out std_logic
);
end mux8_1;

architecture behav of mux8_1 is
begin
with sel select
dataout <= datain(0) when "000",
           datain(1) when "001",
           datain(2) when "010",
           datain(3) when "011",
           datain(4) when "100",
           datain(5) when "101",
           datain(6) when "110",
           datain(7) when others;
end behav;

案例9:奇偶校验电路

通信数据校验基础设计

library ieee;
use ieee.std_logic_1164.all;

entity parity is
port(
    din : in std_logic_vector(7 downto 0);
    parity_bit : out std_logic
);
end parity;

architecture behav of parity is
begin
parity_bit <= din(0) xor din(1) xor din(2) xor din(3)
            xor din(4) xor din(5) xor din(6) xor din(7);
end behav;

案例10:三段式有限状态机FSM

复杂时序逻辑标准开发范式

library ieee;
use ieee.std_logic_1164.all;

entity fsm3seg is
type state_type is (s0,s1,s2,s3);
port(
    clk,rst,start : in std_logic;
    state_out     : out std_logic_vector(1 downto 0)
);
end fsm3seg;

architecture behav of fsm3seg is
signal curr_state,next_state : state_type;
begin
--第一段:时序逻辑 状态寄存
process(clk,rst)
begin
    if rst='1' then
        curr_state <= s0;
    elsif rising_edge(clk) then
        curr_state <= next_state;
    end if;
end process;

--第二段:组合逻辑 状态跳转
process(curr_state,start)
begin
case curr_state is
when s0 => if start='1' then next_state<=s1; else next_state<=s0; end if;
when s1 => next_state <= s2;
when s2 => next_state <= s3;
when s3 => next_state <= s0;
end case;
end process;

--第三段:组合逻辑 输出逻辑
with curr_state select
state_out <= "00" when s0,
             "01" when s1,
             "10" when s2,
             "11" when s3;
end behav;

三、使用说明

  1. 编译环境:代码兼容Vivado 2018+、Quartus II 13.0及以上版本;
  2. 仿真操作:新建工程,复制对应模块代码,添加testbench即可完成波形仿真;
  3. 综合注意:时序模块务必同步复位/异步复位规范书写,避免亚稳态;
  4. 拓展方向:可基于计数器、分频器组合设计计时器、流水灯等综合项目。

海量精选技术文档和实战案例持续更新,敬请关注【风骏时光少年】

相关文章
|
2天前
|
云安全 人工智能 运维
阿里云SecOps Agent,全新安全跨产品执行体验
自然语言驱动 云安全中心/WAF/CFW/ 等多款安全产品联动
1577 1
|
1天前
|
机器学习/深度学习 人工智能 调度
🐴 HappyHorse 1.1 现已上线阿里云百炼!快来查收模型使用指南,现在调用享 6 折~
HappyHorse 1.1 是新一代视频生成大模型,全面升级动态表现力、角色一致性、指令遵循、视觉质感与音画协同能力。支持I2V/T2V/R2V三类生成,适配短剧、电商广告、品牌营销等场景,提供高质、流畅、可控的AI视频生产力。
427 2
🐴 HappyHorse 1.1 现已上线阿里云百炼!快来查收模型使用指南,现在调用享 6 折~
|
12天前
|
缓存 测试技术 API
Qwen 3.7 Plus 与 Max 实测:性价比与多模态能力差异解析(2026)
2026 年 6 月 1 日,阿里悄无声息地发布了 Qwen 3.7 Plus,距 Qwen 3.7 Max 上线刚好 11 天。同样的 1M 上下文,同样的 35 小时自治上限。但价格才是头条:Plus 是 0.40/M输入,Max是 2.50/M——便宜约 6 倍——并且还能看图、看视频。Vision Arena 上 Plus 已经排到 #16。所以这周真正值得讨论的问题不是”要不要为视觉能力买单”,而是”Max 凭什么用 6 倍价格换来 2 个百分点的 benchmark 领先”。
|
13天前
|
JavaScript 定位技术 API
CodeGraph 爆火:编程 Agent 需要的不是更多上下文,而是一张提前画好的代码地图
CodeGraph 是一款爆火的本地代码智能工具,通过 tree-sitter 解析 AST 构建结构化知识图谱(存于 SQLite),为编程 Agent 提前生成“代码地图”。它显著降低 Agent 在中大型项目中的探索成本——实测工具调用减少71%、Token 降57%、速度提升46%,支持19+语言及主流框架路由识别,完全离线、无需 API Key。
865 11
CodeGraph 爆火:编程 Agent 需要的不是更多上下文,而是一张提前画好的代码地图
|
1天前
|
数据采集 人工智能 搜索推荐
企业智能体的下半场,如何让智能体越用越聪明?
AgentLoop 正在邀测期,点击申请邀测资格。
184 124
|
13天前
|
人工智能 运维 JavaScript
阿里云Qoder CN(原通义灵码)全解析 产品形态、版本划分与技术适配说明
在AI辅助开发与智能办公工具持续普及的当下,阿里云旗下原通义灵码正式更名为Qoder CN,同时延伸出QoderWork CN、Qoder CN CLI、Qoder CN Mobile等多款配套产品,形成覆盖代码开发、日常办公、终端交互、移动端使用的完整工具矩阵。Qoder CN核心定位为AI智能编码助手,深度适配主流代码编辑器、集成开发环境以及终端场景;QoderWork CN则偏向桌面端综合办公辅助,二者面向不同使用场景,划分了多个版本档位,搭配差异化资源配额、功能权限与计费规则,同时兼容多款主流大模型。
917 8
|
9天前
|
人工智能 自然语言处理 算法
阿里云百炼Qwen 3.7 Plus与Max实测全解:性价比与多模态能力、成本深度对比
2026年,阿里云百炼平台推出的Qwen 3.7系列成为企业与开发者落地AI应用的核心选择,其中Qwen 3.7 Max与Plus作为两大旗舰版本,定位差异显著:Max是纯文本推理旗舰,专注高强度智能体与复杂逻辑任务;Plus则是多模态全能版,在保留强大文本能力的同时,补齐图像、视频理解能力,且价格大幅降低。本文基于2026年最新实测数据,从核心参数、文本能力、多模态能力、智能体表现、性价比与场景选型六大维度,全面解析两款模型的差异,为用户提供精准选型参考。
453 0
|
13天前
|
JSON 缓存 安全
通过 CC Switch 本地路由让 Codex CLI 接入 DeepSeek 等第三方模型
CC Switch 通过本地路由(`127.0.0.1:15721`)实现协议转换:将 Codex 的 Responses API 请求自动映射为 DeepSeek 等厂商的 Chat Completions 接口,兼容流式响应与工具调用,无需修改 Codex 源码,安全隔离 API Key。(239字)
2499 7
通过 CC Switch 本地路由让 Codex CLI 接入 DeepSeek 等第三方模型