当前位置: 首页 > news >正文

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
http://www.jsqmd.com/news/105606/

相关文章:

  • 大专会计就业规划:上岗必考7大证书盘点与企业刚需解析
  • Unitree机器人Python SDK终极指南:从零开始实现机器人控制
  • hasattr()函数和getattr()函数
  • EmotiVoice语音合成配置热更新机制实现
  • dart特性之 --- mixin
  • EmotiVoice在生日祝福语音中的欢快演绎
  • EmotiVoice语音呼吸感模拟技术增加真实度
  • 企业级语音项目首选:EmotiVoice高性能TTS引擎
  • EmotiVoice能否用于远程医疗语音通知系统?HIPAA合规考量
  • 高职大数据与审计专业:7大高含金量证书
  • ScriptHookV模组开发实战:从入门到精通的完整指南
  • 8个AI论文工具,MBA轻松搞定毕业论文!
  • XVim终极使用指南:掌握Xcode中的Vim编辑技巧
  • 如何快速构建跨平台移动应用:yudao-cloud + UniApp 终极实践指南
  • Windows程序资源编辑神器rcedit:告别繁琐的图形界面操作
  • Qwen3-Omni多模态AI模型实战指南:从零构建智能语音交互应用
  • 单词倒排 和 字符串P型编码
  • 捷丰家俱×中扬立库:4360货位智能立库,赋能宜家核心供应商
  • 结合LLM使用EmotiVoice:大模型+情感语音的完整闭环方案
  • 技术赋能、生态联动与价值重塑:从iBox跨界看数字文化产业的新路径探索
  • JVET-AL0106
  • EmotiVoice语音合成日志记录规范:便于调试与审计
  • AI代理框架实战指南:从概念验证到企业级部署
  • React 的位掩码标记系统
  • 服务器运行easyocr报错Could not initialize NNPACK! Reason: Unsupported hardware.——解决方法
  • Doris集群搭建
  • 终极ASMR音频资源快速下载完整指南
  • React Native Vision Camera实战:从零构建高性能AR拍摄应用
  • JAVA基于多线程机制的理解
  • 网通领域发光二极管(LED)应用全解析:从基础认知到选型要点