news 2026/6/15 12:28:57

verilog简单入门day9-组合逻辑

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
verilog简单入门day9-组合逻辑

case1

已经给你一个BCD 的“1 位(1 个十进制数字)加法器”,名字叫bcd_fadd
它可以把两个 BCD 数字一个输入进位相加,并产生BCD 的和进位输出

module bcd_fadd ( input [3:0] a, input [3:0] b, input cin, output cout, output [3:0] sum );
module top_module( input [399:0] a, b, input cin, output cout, output [399:0] sum ); wire [99:0]c; bcd_fadd u_bcd_fadd_0( .a(a[3:0]), .b(b[3:0]), .cin(cin), .cout(c[0]), .sum(sum[3:0]) ) ; genvar i; generate for(i=1;i<100;i=i+1)begin:GEN bcd_fadd u_bcd_fadd_i( .a(a[4*i+3:4*i]), .b(b[4*i+3:4*i]), .cin(c[i-1]), .cout(c[i]), .sum(sum[4*i+3:4*i]) ) ; end endgenerate assign cout = c[99]; endmodule

case2真值表

真值表 = 把“所有可能的输入”和“对应的输出”全部列出来的表

如果输入是这样 → 输出应该是什么

想一个“最简单的开关例子”

场景:

有一个灯,两个开关AB

规则是:

只要有任意一个开关打开,灯就亮

那“真值表”在干嘛?

它只是把所有可能情况全部列出来:

两个开关,每个只有 0 / 1 两种状态

一共:

2 × 2 = 4 种情况

AB灯(out)
000
011
101
111

SOP(与或式)的规则是:
👉输出为 1 的“每一行” → 写一个 AND 项
👉把所有这些 AND 项用 OR 连起来

module top_module( input x3, input x2, input x1, // three inputs output f // one output ); assign f = (~x3&x2&~x1)| (~x3&x2&x1)| (x3&~x2&x1)| (x3&x2&x1); endmodule

case3看波形图写代码

相同为1,不同为0,同或

assign y=~(x^y);

case4

假设你正在设计一个电路,用来控制手机的铃声振动马达

当手机因为来电需要响铃时(输入ring = 1),
你的电路必须要么打开铃声,要么打开振动马达
但不能两个同时打开

如果手机处于振动模式vibrate_mode = 1),
就打开振动马达
否则(vibrate_mode = 0),
就打开铃声

尽量只使用assign语句
看看你能不能把这个“文字描述的问题”翻译成一组逻辑门。

module top_module ( input ring, input vibrate_mode, output ringer, // Make sound output motor // Vibrate ); assign ringer=(~vibrate_mode)&ring; assign motor =vibrate_mode&ring; endmodule

case5

一个冷热恒温器,用来控制加热器(冬天)空调(夏天)


Implement a circuit that will turn on and off the heater, air conditioning, and blower fan as appropriate.

实现一个电路,能正确地打开/关闭:

  • 加热器(heater)

  • 空调(air conditioner)

  • 风扇(blower fan)


The thermostat can be in one of two modes: heating (mode = 1) and cooling (mode = 0).

恒温器有两种模式:

  • 加热模式:mode = 1

  • 制冷模式:mode = 0


In heating mode, turn the heater on when it is too cold (too_cold = 1) but do not use the air conditioner.

加热模式下:

  • 如果太冷(too_cold = 1),打开加热器

  • 绝不能打开空调


In cooling mode, turn the air conditioner on when it is too hot (too_hot = 1), but do not turn on the heater.

制冷模式下:

  • 如果太热(too_hot = 1),打开空调

  • 绝不能打开加热器


When the heater or air conditioner are on, also turn on the fan to circulate the air.

只要加热器 或 空调打开了:

  • 风扇也要打开(用来送风)


In addition, the user can also request the fan to turn on (fan_on = 1), even if the heater and air conditioner are off.

另外:

  • 即使加热器和空调都关着

  • 用户也可以手动请求打开风扇(fan_on = 1


Try to use only assign statements…

尽量只用assign,把文字描述翻译成逻辑门。

module top_module ( input too_cold, input too_hot, input mode, input fan_on, output heater, output aircon, output fan ); assign heater=mode&too_cold; assign aircon=(~mode)&too_hot; assign fan = heater|aircon|fan_on; endmodule

case6

统计1

module top_module( input [2:0] in, output [1:0] out ); integer i; always@(*)begin out=0; for(i=0;i<3;i=i+1)begin if(in[i]) out = out+1; end end endmodule

case7

out_both的每一位表示:
当前输入位它左边的邻居(索引更大的那一位)是否都为 1

out_any的每一位表示:
当前输入位它右边的邻居(索引更小的那一位)是否至少有一个是 1

out_different的每一位表示:
当前输入位是否和左边的邻居不同。

module top_module( input [3:0] in, output [2:0] out_both, output [3:1] out_any, output [3:0] out_different ); integer i ; always@(*)begin for(i=0;i<3;i=i+1)begin out_both[i]=in[i]&in[i+1]; end end always@(*)begin for(i=1;i<4;i=i+1)begin out_any[i]=in[i]|in[i-1]; end end always@(*)begin for(i=0;i<4;i=i+1)begin out_different[i]=(i==3)?in[i]^in[0]:in[i]^in[i+1]; end end endmodule
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/11 11:59:32

Ant Design企业级组件库与设计工具无缝集成方案

Ant Design企业级组件库与设计工具无缝集成方案 【免费下载链接】ant-design An enterprise-class UI design language and React UI library 项目地址: https://gitcode.com/gh_mirrors/ant/ant-design 在当今快节奏的数字化产品开发环境中&#xff0c;设计与开发团队之…

作者头像 李华
网站建设 2026/6/15 10:39:09

零样本声音克隆技术突破!EmotiVoice带你实现秒级音色复制

零样本声音克隆技术突破&#xff01;EmotiVoice带你实现秒级音色复制 在虚拟主播直播中突然“变声”&#xff0c;游戏NPC因情绪变化而语调起伏&#xff0c;或是让语音助手用你亲人的声音温柔回应——这些曾属于科幻场景的体验&#xff0c;正随着零样本声音克隆技术的成熟逐步成…

作者头像 李华
网站建设 2026/6/14 19:12:37

如何评价EmotiVoice生成语音的质量?MOS评分实验结果公布

EmotiVoice语音质量实测&#xff1a;MOS评分揭示其真实表现力 在智能语音助手动辄“面无表情”地播报天气、有声书朗读听起来像机器人念稿的今天&#xff0c;用户早已不满足于“能听清”——他们想要的是有情绪、有温度、有个性的声音。这正是EmotiVoice这类高表现力TTS系统崛…

作者头像 李华
网站建设 2026/6/12 1:04:09

金仓数据库:不止于兼容,更以三重革新赋能企业数字化深水区

金仓数据库&#xff1a;不止于兼容&#xff0c;更以三重革新赋能企业数字化深水区 兼容是对企业历史投资的尊重是确保业务平稳过渡的基石然而这仅仅是故事的起点在数字化转型的深水区&#xff0c;企业对数据库的需求早已超越“语法兼容”的基础诉求。无论是核心业务系统的稳定运…

作者头像 李华