第六章- Verilog HDL 高级程序设计举例【Verilog】

news2024/9/20 10:14:31

第六章 Verilog HDL 高级程序设计举例【Verilog】

  • 前言
  • 推荐
  • 第六章 Verilog HDL 高级程序设计举例
    • 状态机
    • 用状态机设计1101序列检测器
      • 需要定义几个状态?
      • 代码设计——端口信号声明
      • 状态寄存器
      • 次态生成逻辑设计(C1模块)
      • 输出逻辑设计(C2模块)
      • 完整代码
      • 测试结果
    • 用状态机设计1101序列检测器
      • 不考虑重复序列的状态转移图???
      • 用米里状态机设计1101序列检测器
      • 完整代码
      • 测试结果
    • 交通灯
      • 输出
      • 交通灯的状态转移图
      • 代码设计?
      • 程序设计-----输入输出信号声明
      • 状态寄存器和状态转移逻辑设计
      • 输出逻辑设计
      • 完整代码
      • 测试结果
  • 最后

前言

2022/12/3 14:41

以下内容源自Verilog
仅供学习交流使用

推荐

Verilog

第六章 Verilog HDL 高级程序设计举例

状态机

2段式
在这里插入图片描述
3段式
在这里插入图片描述
Mealy状态机和Moore状态机
在这里插入图片描述

用状态机设计1101序列检测器

需要定义几个状态?

在这里插入图片描述

代码设计——端口信号声明

module seadeta_moore (
	input wire clk,
	input wire clr,
	input wire din,
	output reg dout
	);
	
	reg [2:0] present_state, next_state;
	parameter s0=3'b000,s1=3 'b001,s2=3 'b010,s3=3'b011,s4=3'b100;
endmodule

状态寄存器

  • 确定初始状态
  • 每一个clk的上升沿,修改状态寄存器的值
	always @(posedge clk or posedge clr)
		begin
			if(clr==1) present_state<=s0;
			else present_state<=next_state;
		end

次态生成逻辑设计(C1模块)

  • 根据输入和当前状态,决定转移的下一个状态
	always @(*)
		begin
			case (present_state)
				s0: if (din==1)next_state=s1;
					else next_state=s0;
				s1: if(din==1) next_state=s2;
					else next_state=s0;
				s2: if (din==0) next_state=s3;
					else next_state=s2;
				s3: if(din==1) next_state =s4;
					else next_state= s0;
				s4: if(din==0) next_state = s0;
					else next_state= s2;
				default next_state= s0;
			endcase
		end


输出逻辑设计(C2模块)

  • 根据当前的状态决定当前的输出
	always@(*)
		begin
			if(present_state==s4) dout=1;
			else dout =0;
		end

完整代码

//moore型
//考虑重复序列
module seadeta_moore (
	input wire clk,
	input wire clr,
	input wire din,
	output reg dout
	);
	//代码设计--端口信号声明
	reg [2:0] present_state, next_state;
	parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100;

	//状态寄存器
 	//确定初始状态
	//每一个clk的上升沿,修改状态寄存器的值
	always @(posedge clk or posedge clr)
		begin
			if(clr==1) present_state<=s0;
			else present_state<=next_state;
		end
	//次态生成逻辑设计(C1模块)
	//根据输入和当前状态,决定转移的下一个状态
	always @(*)
		begin
			case (present_state)
				s0: if (din==1)next_state=s1;
					else next_state=s0;
				s1: if(din==1) next_state=s2;
					else next_state=s0;
				s2: if (din==0) next_state=s3;
					else next_state=s2;
				s3: if(din==1) next_state =s4;
					else next_state= s0;
				s4: if(din==0) next_state = s0;
					else next_state= s2;
				default next_state= s0;
			endcase
		end

	//输出逻辑设计(C2模块)
	//根据当前的状态决定当前的输出
	always@(*)
		begin
			if(present_state==s4) dout=1;
			else dout =0;
		end
endmodule

测试结果

测试

module seadeta_tb;
	reg  clk;
	reg  clr;
	reg  din;
	wire dout;
	
	seadeta_moore m1(clk,clr,din,dout);


	always
		#5 clk=~clk;

	initial
		begin
			clk=0;
			clr=1;
			#10 clr=0;din=0;
			#10 din=1;
			#10 din=1;
			#10 din=0;
			#10 din=1;
			#10 din=1;
			#10 din=0;
			#10 din=1;
			#10 din=0;
			#10 din=1;
			#10 din=1;
			#10 din=0;
			#10 din=1;
		end
		



endmodule

结果
在这里插入图片描述

用状态机设计1101序列检测器

序列检测器就是将一个指定的序列从数字码流中检测出来。
当输入端出现序列1101时,输出为1,否则输出为0。在此不考虑重复序列,即出现指定序列后就重新开始序列检测,不再考虑以前的数据。

不考虑重复序列的状态转移图???

在这里插入图片描述

用米里状态机设计1101序列检测器

  • 需要定义4个状态。
  • 状态转移图

在这里插入图片描述

完整代码

//mealy型
//不考虑重复序列
module seadeta_mealy(
	input wire clk,
	input wire clr,
	input wire din,
	output reg dout
	);
	
	reg [2:0] present_state, next_state;
	parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011;

	//时序逻辑
	always @(posedge clk or posedge clr)
		begin
			if(clr==1) present_state<=s0;
			else present_state<=next_state;
		end
	
	//组合逻辑
	always @(*)
		begin
			case (present_state)
				s0: if (din==1)begin next_state=s1; dout=0; end
					else begin next_state=s0; dout=0; end
				s1: if(din==1) begin next_state=s2; dout=0; end
					else begin next_state=s0; dout=0; end
				s2: if (din==0) begin next_state=s3; dout=0; end
					else begin next_state=s2; dout=0; end
				s3: if(din==1) begin next_state =s0; dout=1; end
					else begin next_state= s0; dout=0; end
				
				default begin next_state= s0; dout=0; end
			endcase
		end

endmodule

测试结果

测试

module seadeta_tb;
	reg  clk;
	reg  clr;
	reg  din;
	wire dout;
	
	
	//seadeta_moore m1(clk,clr,din,dout);
	seadeta_mealy m2(clk,clr,din,dout);

	always
		#5 clk=~clk;

	initial
		begin
			clk=0;
			clr=1;
			#10 clr=0;din=0;
			#10 din=1;
			#10 din=1;
			#10 din=0;
			#10 din=1;
			#10 din=1;
			#10 din=0;
			#10 din=1;
			#10 din=0;
			#10 din=1;
			#10 din=1;
			#10 din=0;
			#10 din=1;
		end
		



endmodule

结果
在这里插入图片描述

交通灯

输出

在这里插入图片描述

交通灯的状态转移图

在这里插入图片描述

代码设计?

  • 用6个彩色LED等代表一组交通信号灯,为输出
  • 行为描述:根据当前的状态点亮熄灭对应的LED灯,计数结束就状态转移

在这里插入图片描述

程序设计-----输入输出信号声明

module traffic(
	input wire clk,input wire clr,
	output reg [5:0]lights
	);
	reg [2:0]state;
	reg [3:0]count;
	parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100,s5=3'b101;
	parameter sec5=5,sec1=1;
endmodule

状态寄存器和状态转移逻辑设计

	always @(posedge clk or posedge clr)
		begin
			if(clr==1)
			begin
				state<=s0;
				count<=0;
			end
		
			else
				case (state)
					s0:	if(count<sec5)
							begin
								state<=s0;
								count<=count+1;
							end
						else
							begin
								state<=s1;
								count<=0;
							end
					s1:	if(count<sec1)
							begin
								state<=s1;
								count<=count+1;
							end
						else
							begin
								state<=s2;
								count<=0;
							end
					s2:	if(count<sec1)
							begin
								state<=s2;
								count<=count+1;
							end
						else
							begin
								state<=s3;
								count<=0;
							end
					s3:	if(count<sec5)
							begin
								state<=s3;
								count<=count+1;
							end
						else
							begin
								state<=s4;
								count<=0;
							end
					s4:	if(count<sec1)
							begin
								state<=s4;
								count<=count+1;
							end
						else
							begin
								state<=s5;
								count<=0;
							end
					s5:	if (count<sec1)
							begin
								state<=s5;
								count<=count+1;
							end
						else
							begin
								state<=s0;
								count<=0;
							end
					default state<=s0;
				endcase
		end

输出逻辑设计

	always @(*)
		begin
			case (state)
			s0: lights=6'b100001;
			s1: lights=6'b100010;
			s2: lights=6'b100100;
			s3: lights=6'b001100;
			s4: lights=6'b010100;
			s5: lights=6'b100100;
			default lights=6'b100001;
			endcase
		end

在这里插入图片描述

完整代码

module traffic(
	input wire clk,input wire clr,
	output reg [5:0]lights
	);
	//程序设计-----输入输出信号声明
	reg [2:0]state;
	reg [3:0]count;//此位宽由sec5,sec1决定
	parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100,s5=3'b101;
	parameter sec5=5,sec1=1;//此单位为时钟周期

	//状态寄存器和状态转移逻辑设计
	always @(posedge clk or posedge clr)
		begin
			if(clr==1)
			begin
				state<=s0;
				count<=0;
			end
		
			else
				case(state)
					s0:	if(count<sec5)
							begin
								state<=s0;
								count<=count+1;
							end
						else
							begin
								state<=s1;
								count<=0;
							end
					s1:	if(count<sec1)
							begin
								state<=s1;
								count<=count+1;
							end
						else
							begin
								state<=s2;
								count<=0;
							end
					s2:	if(count<sec1)
							begin
								state<=s2;
								count<=count+1;
							end
						else
							begin
								state<=s3;
								count<=0;
							end
					s3:	if(count<sec5)
							begin
								state<=s3;
								count<=count+1;
							end
						else
							begin
								state<=s4;
								count<=0;
							end
					s4:	if(count<sec1)
							begin
								state<=s4;
								count<=count+1;
							end
						else
							begin
								state<=s5;
								count<=0;
							end
					s5:	if (count<sec1)
							begin
								state<=s5;
								count<=count+1;
							end
						else
							begin
								state<=s0;
								count<=0;
							end
					default state<=s0;
				endcase
		end
	
	//输出逻辑设计
	always @(*)
		begin
			case (state)
			s0: lights=6'b100001;
			s1: lights=6'b100010;
			s2: lights=6'b100100;
			s3: lights=6'b001100;
			s4: lights=6'b010100;
			s5: lights=6'b100100;
			default lights=6'b100001;
			endcase
		end
	
endmodule

测试结果

测试

module traffic_tb;
	
	reg clk;
	reg clr;
	wire [5:0]lights;


	traffic u1(clk,clr,lights);
	always 
		#5 clk=~clk;

	initial
		begin
			clk=0;
			clr=1;
			#10
			clr=0;
		end
	
	
	
endmodule

结果
在这里插入图片描述

最后

2022/12/3 16:21

这篇博客能写好的原因是:站在巨人的肩膀上

这篇博客要写好的目的是:做别人的肩膀

开源:为爱发电

学习:为我而行

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/58198.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vSphere-ESXi

VMware-vShpere 第一步&#xff1a; 在VMware虚拟机上安装一台ESXI 开启虚拟机 至此&#xff0c;虚拟机ESXI安装完毕 第二步&#xff1a;使用ESXI虚拟机IP上浏览器 登录后&#xff0c;上传一个映像文件至ISO文件 将VM虚拟机关机 手动添加一块硬盘 然后再使用虚拟机IP进入浏览器…

python入门——基础语法

python入门——基础语法 注释 单行注释&#xff1a;使用# 多行注释&#xff1a;使用 “”" “”" type()&#xff1a;查看数据的类型 int()&#xff1a;转化为整数 float()&#xff1a;转化为浮点数 str()&#xff1a;转化为字符串 标识符不可以使用这些 …

Spring WebFlux简单使用

官网&#xff1a;https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html 1.WebFlux介绍 Spring WebFlux 是 Spring Framework 5.0中引入的新的响应式web框架。与Spring MVC不同&#xff0c;它不需要Servlet API&#xff0c;是完全异步且非阻…

蓝桥杯嵌入式LCD屏幕

文章目录前言一、拷贝官方例程二、cubeMX配置三、LCD代码及函数分析使用四、sprintf函数总结前言 本篇文章将带大家学习LCD屏幕的操作&#xff0c;LCD的配置是非常复杂的&#xff0c;在比赛上去实现这些驱动程序基本上是不可能的&#xff0c;当然了比赛官方也是知道这一点的&a…

SpringMVC基础篇:MVC基础知识

第一章&#xff1a;SpringMVC引言 一&#xff1a;什么是SpringMVC 概念&#xff1a;SpringMVC是在Spring框架基础上衍生而来的一个MVC框架&#xff0c;主要解决了原有的MVC框架过程中控制器&#xff08;Controller&#xff09;的问题。 SpringMVC是Java开发当中最主流的web技…

了解软件测试

软件测试课程 1.1课程内容&#xff1a; 软件基础课程 ------设计测试用例方法 自动化课程&#xff08;web自动化&#xff09;------ 抢票功能 性能测试课程 ----项目性能测试 1.2 什么是测试 1.21 生活中测试的案例 坐地铁&#xff0c;做核酸&#xff0c;扫核酸码 比如当我…

[附源码]计算机毕业设计学生社团信息管理系统Springboot程序

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

零基础学习软件测试,掌握四点就够了

近年来越来越多的人转行到软件测试这一领域&#xff0c;对于很多外行的人来说&#xff0c;肯定对这一行业有很多不了解&#xff0c;对于这一职业的职责以及要求都会不清楚&#xff0c;那么我们今天就来梳理一下关于软件测试行业的信息。 一、软件测试的主要职责你知道吗&#x…

裸辞闭关60天,啃下这些软件测试笔记,有幸通过阿里测开岗P6面试

时代在发展&#xff0c;互联网之下&#xff0c;稍有一些落后可能就会被淘汰掉&#xff0c;因此我们需要不断去审视自己&#xff0c;通过学习来让自己得到相应的提升。 近段时间&#xff0c;我也了解到很多小伙伴不清楚作为测试工程师应该掌握什么样的核心知识&#xff1f;实际…

暴力算法 --- 莫队

文章目录莫队基础莫队带修改莫队树上莫队回滚莫队莫队 什么是莫队&#xff1f; 答&#xff1a;优雅的暴力&#xff01;&#xff01;&#xff01; 基础莫队 重复的数 题目描述&#xff1a;给出一个长度为NNN的序列&#xff0c;有若干查询&#xff0c;每次查询区间[li,ri][l_i,…

网页信息抓取-网页信息采集器

抓取整个网页&#xff0c;我们怎么抓取整个网页自己想要的文章内容&#xff0c;很多人一想到内容抓取。就想到要学习一门编程语言&#xff0c;最让大家熟知的就是python爬虫。如果完全靠自己自学&#xff0c;又是从零基础开始学习Python的情况下&#xff0c;我认为至少需要半年…

Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏

云收藏项目已经开源2年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时很新的技术,现在看来其实很多新技术是没有必要使用的,但做为学习案例来讲确实是一个绝佳的 Spring Boot 实践。 从开源到现在,写了一些教程给大家介绍如何部署云收藏,如何在IDE中运…

[附源码]计算机毕业设计ssm校园二手交易平台

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

解决JSP中Bean在页面显示不正确问题(scope关键字)

问题出现 有一天我在编写JSP的程序时&#xff0c;在Java后端写了跳转并且传输数据语句&#xff0c;但前端界面渲染出来的数据却是我在DAO中初始化的数据。 第一句语句将book对象注入request的Session中&#xff0c;第二句实现跳转到JSP页面&#xff0c;第三句将此时的request和…

Logistic回归——银行违约情况分析

文章目录一、Logistic回归原理1、Sigmoid函数作用2、用梯度下降法求解参数二、利用Logistic回归分类1、数据预处理2、利用梯度上升计算回归系数3、训练和验证总结一、Logistic回归原理 1、Sigmoid函数作用 Logistic回归的原理是用逻辑函数把线性回归的结果(-∞,∞)映射到(0,1…

【408篇】C语言笔记-第十章(线性表)

文章目录第一节&#xff1a;线性表的顺序表示线性表1. 定义2. 特点线性表的顺序表示1. 顺序表2. 优缺点比较3. 插入操作4. 删除操作5. 动态分配代码示例第二节&#xff1a;线性表的链式表示单链表优缺点对比插入操作删除操作代码示例第一节&#xff1a;线性表的顺序表示 线性表…

python中的序列(列表、元组、字符串)的切片操作

目录 一.序列 二. 序列常用操作——切片 注意 演示 一.序列 序列是指:内容连续、有序&#xff0c;可使用下标索引的一类数据容器 列表、元组、字符串&#xff0c;均可以可以视为序列。 二. 序列常用操作——切片 序列支持切片&#xff0c;即:列表、元组、字符串&#xf…

看卡塔尔世界杯,diff一下足球比赛与软件开发

diff一下足球比赛与软件开发吐槽世界杯E组&#xff01;类比软件开发与足球比赛教练与架构师的作用新技术——半自动越位技术世界杯冠军吐槽世界杯E组&#xff01; 最近博主看了多场世界杯比赛&#xff0c;看的我心力交瘁&#xff0c;欲哭无泪。 从日本与哥斯达黎加那场比赛开始…

算法刷题打卡第34天:有效的井字游戏

有效的井字游戏 难度&#xff1a;中等 给你一个字符串数组 boardboardboard 表示井字游戏的棋盘。当且仅当在井字游戏过程中&#xff0c;棋盘有可能达到 boardboardboard 所显示的状态时&#xff0c;才返回 truetruetrue 。 井字游戏的棋盘是一个 3 x 3 数组&#xff0c;由字…

MYSQL 中连接的使用

文章目录0 写在前面1 语法说明2 SQL准备3 举例说明3.1 内连接3.2 左连接3.3 右连接4 写在最后0 写在前面 实际业务中&#xff0c;查询数据库大多都是多表链接查询&#xff0c;所以MYSQL的连接的使用非常重要。 连接有三种: INNER JOIN&#xff08;内连接,或等值连接&#xff0…