Verilog实现移位寄存器「建议收藏」

Verilog实现移位寄存器「建议收藏」移位寄存器

大家好,又见面了,我是你们的朋友全栈君。

Verilog实现8位环形移位寄存器

左移: 环形就是首尾相连

module shift_regist (
    input  wire clk,
    input wire rstn,
    input wire [7:0]D,
    output reg [7:0]Q
);
always @(posedge  clk  or  negedge  rstn) begin
    if(!rstn)
        Q<=8'b000000;
    else
         Q<={ 
   D[6:0],D[7]} ;    
end
endmodule //shift_regist 

右移:

module shift_regist (
    input  wire clk,
    input wire [7:0]D,
    input wire rstn,
    output reg [7:0]Q
);
always @(posedge  clk ) begin
        if(!rstn)
        Q<=8'b000000;
    else
         Q<={ 
   D[0],D[7:1]} ;    
end
endmodule //shift_regist  

普通的移位寄存器用for语句实现:

module shift_regist2(Q,D,rst,clk);
    output [7:0] Q;
    input D,rst,clk;
    reg [7:0] Q;
    integer i;
always @(posedge clk)
    if (!rst) 
        Q<=8'b000000;
    else
      	for (i=7;i>0;i=i-1)
        begin
            Q[i]<=Q[i-1];  
                Q[0]<=D;
            end
endmodule 

普通左移:

//8 bit shift register
module shift_regist(
  input d,
  input rstn,
  input clk,
  output reg [7:0]q
);
  always@(posedge clk or negedge rstn)begin
    if(!rstn)
      q <=8'b0;
    else
      q <={ 
   q[6:0],d};
  end
endmodule

tb测试:

module tb;
  reg d,rstn,clk;
  wire [7:0]q;

  shift_regist u_shift(d,rstn,clk,q);

  initial begin
    rstn=0;
    clk=0;
    #5
    rstn=1;
  end

  always #5 clk=~clk;

  initial begin
    d=0;
    #10 d=0; //00
    #10 d=1; //001
    #10 d=1; //0011
    #10 d=0; //00110
    #10 d=0;
    #10 d=1;
    #10 d=1;
    #10 d=0;
    #10 d=1;
    #10 $finish;
  end
endmodule

图形分析:
在这里插入图片描述

双向shift:就是加个判断

always@(posedge clk)begin
	if(dir==0)
		sf<={ 
   sf[2:0],din};
	else
		sf<={ 
   din,sf[3:1]};
end
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160236.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月16日 下午2:46
下一篇 2022年7月16日 下午3:00


相关推荐

  • MySQL修改root密码的4种方法

    MySQL修改root密码的4种方法方法1:用SETPASSWORD命令首先登录MySQL。格式:mysql>setpasswordfor用户名@localhost=password(‘新密码’);例子:mysql>setpasswordforroot@localhost=password(‘123’);方法2:用mysqladmin格式:mysqladmin-u用户名-p旧密

    2022年6月29日
    55
  • Linux 查看环境变量_linux修改环境变量顺序

    Linux 查看环境变量_linux修改环境变量顺序一、Linux的变量种类     按变量的生存周期来划分,Linux变量可分为两类:     1、永久的:需要修改配置文件,变量永久生效。     2、临时的:使用export命令声明即可,变量在关闭shell时失效。 二、设置变量的三种方法1、在/etc/profile文件中添加变量【对所有用户生效(永久的)】     用VI在文件/etc/profile文件

    2026年4月20日
    4
  • 18.网页尺寸scrollHeight

    18.网页尺寸scrollHeight

    2021年9月4日
    53
  • 腾讯云消息队列CMQ

    腾讯云消息队列CMQ消息队列 CMQ 版 TDMQforCMQ 简称 TDMQCMQ 版 是一款分布式高可用的消息队列服务 它能够提供可靠的 基于消息的异步通信机制 能够将分布式部署的不同应用 或同一应用的不同组件 中的信息传递 存储在可靠有效的 CMQ 队列中 防止消息丢失 TDMQCMQ 版支持多进程同时读写 收发互不干扰 无需各应用或组件始终处于运行状态 相比传统开源 MQ 应用腾讯云消息队列 CMQ 优势有哪些 高性能 兼顾性能与可靠性 单 TDMQCMQ 版实例 QPS 达到 5000 高扩展性 1 队列数量及队

    2026年3月17日
    1
  • 豆包:是类AI Agents智能助手,还是未来Agentic AI?

    豆包:是类AI Agents智能助手,还是未来Agentic AI?

    2026年3月15日
    1
  • 文献阅读 – Deep Contextualized Word Representations

    文献阅读 – Deep Contextualized Word RepresentationsDeepContextu E Peters M Neumann M Iyyer M Gardner etal DeepContextu NAACL 2018 摘要深度上下文词表示 deepcontextu

    2026年3月18日
    103

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号