HDLBits学习笔记——移位寄存器

这篇具有很好参考价值的文章主要介绍了HDLBits学习笔记——移位寄存器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

为了方便做笔记,从移位寄存器(Shift Registers)这章开始按章节做记录。

1.   4-bit Shift Registers

题目:

Build a 4-bit shift register (right shift), with asynchronous reset, synchronous load, and enable.

  • areset: Resets shift register to zero.
  • load: Loads shift register with data[3:0] instead of shifting.
  • ena: Shift right (q[3] becomes zero, q[0] is shifted out and disappears).
  • q: The contents of the shift register.

If both the load and ena inputs are asserted (1), the load input has higher priority.

思路:

设计一个4位逻辑移位寄存器(右移),寄存器具有异步复位,同步加载和使能。

复位:寄存器置为0。

加载:加载data[3:0]到寄存器中。

使能:寄存器右移。

q:移位寄存器。

需要注意的是,题目要求当加载和使能同时为高电平时,优先对数据进行加载。

代码:文章来源地址https://www.toymoban.com/news/detail-403654.html

module top_module(
    input clk,
    input areset,  // async active-high reset to zero
    input load,
    input ena,
    input [3:0] data,
    output reg [3:0] q); 
    always@(posedge clk, posedge areset) begin
        if(areset)
            q<=0;
        else if(load)
            q <= data;
        else if(ena) begin
            q[0]<=q[1];
            q[1]<=q[2];
            q[2]<=q[3];
            q[3]<=0;
// q <=q[3:1];
        end
    end
endmodule

2.Left/right rotator

题目:

Build a 100-bit left/right rotator, with synchronous load and left/right enable. A rotator shifts-in the shifted-out bit from the other end of the register, unlike a shifter that discards the shifted-out bit and shifts in a zero. If enabled, a rotator rotates the bits around and does not modify/discard them.

  • load: Loads shift register with data[99:0] instead of rotating.
  • ena[1:0]: Chooses whether and which direction to rotate.
    • 2'b01 rotates right by one bit
    • 2'b10 rotates left by one bit
    • 2'b00 and 2'b11 do not rotate.
  • q: The contents of the rotator.

思路: 

顾名思义是一个可控制左移/右移的循环移位寄存器,同步加载。与题1不同的是,移位后的数据将会翻折到另一端而不是置0。

题中使能端ena[1:0]控制寄存器是否左移/右移。

代码:

module top_module(
    input clk,
    input load,
    input [1:0] ena,
    input [99:0] data,
    output reg [99:0] q); 
    always@(posedge clk) begin
        if(load)
            q <= data;
        else begin
            case(ena)
                2'b01: q <= {q[0],q[99:1]};
                2'b10: q <= {q[98:0],q[99]};
                default: q <= q;
            endcase
        end
    end
endmodule

3.Left/right arithmetic shift by 1 0r 8

题目:

Build a 64-bit arithmetic shift register, with synchronous load. The shifter can shift both left and right, and by 1 or 8 bit positions, selected by amount.

An arithmetic right shift shifts in the sign bit of the number in the shift register (q[63] in this case) instead of zero as done by a logical right shift. Another way of thinking about an arithmetic right shift is that it assumes the number being shifted is signed and preserves the sign, so that arithmetic right shift divides a signed number by a power of two.

There is no difference between logical and arithmetic left shifts.

  • load: Loads shift register with data[63:0] instead of shifting.
  • ena: Chooses whether to shift.
  • amount: Chooses which direction and how much to shift.
    • 2'b00: shift left by 1 bit.
    • 2'b01: shift left by 8 bits.
    • 2'b10: shift right by 1 bit.
    • 2'b11: shift right by 8 bits.
  • q: The contents of the shifter.

思路: 

本题要求设计一个可实现左移/右移功能的算数移位寄存器,与逻辑移位寄存器不同的地方在于,有符号数需要考虑算数右移

对于无符号数,算数移位寄存器与逻辑移位寄存器实现方法一致。

对于有符号数,算数左移与逻辑左移一致,移位后补0;算数右移与逻辑右移不同,算数右移左侧需补充符号位而非0。

代码:

module top_module(
    input clk,
    input load,
    input ena,
    input [1:0] amount,
    input [63:0] data,
    output reg [63:0] q); 
    always@(posedge clk) begin
        if(load)
            q <= data;
        else if(ena) 
        begin
            case(amount)
                2'b00: q <= {q[62:0],1'b0};
                2'b01: q <= {q[55:0],{8{1'b0}}};
                2'b10: q <= {q[63],q[63:1]};
                2'b11: q <= {{8{q[63]}},q[63:8]};
            endcase
        end
    end
endmodule

4.    5bits LFSR

题目:

A linear feedback shift register is a shift register usually with a few XOR gates to produce the next state of the shift register. A Galois LFSR is one particular arrangement where bit positions with a "tap" are XORed with the output bit to produce its next value, while bit positions without a tap shift. If the taps positions are carefully chosen, the LFSR can be made to be "maximum-length". A maximum-length LFSR of n bits cycles through 2n-1 states before repeating (the all-zero state is never reached).

The following diagram shows a 5-bit maximal-length Galois LFSR with taps at bit positions 5 and 3. (Tap positions are usually numbered starting from 1). Note that I drew the XOR gate at position 5 for consistency, but one of the XOR gate inputs is 0.

HDLBits学习笔记——移位寄存器

思路:

题目提供了一个线性反馈移位寄存器的电路图,这一个带有若干个XOR门的移位寄存器,将带有抽头的位置与输出位进行异或运算来产生下一个值。

代码:

module top_module(
    input clk,
    input reset,    // Active-high synchronous reset to 5'h1
    output [4:0] q
); 
    always@(posedge clk) begin
        if(reset)
            q <= 5'h1;
        else begin
            q[4] <= q[0]^0;
            q[3] <= q[4];
            q[2] <= q[3]^q[0];
            q[1] <= q[2];
            q[0] <= q[1];
        end
    end
endmodule

官方解法:

module top_module(
	input clk,
	input reset,
	output reg [4:0] q);
	
	reg [4:0] q_next;		// q_next is not a register

	// Convenience: Create a combinational block of logic that computes
	// what the next value should be. For shorter code, I first shift
	// all of the values and then override the two bit positions that have taps.
	// A logic synthesizer creates a circuit that behaves as if the code were
	// executed sequentially, so later assignments override earlier ones.
	// Combinational always block: Use blocking assignments.
	always @(*) begin
		q_next = q[4:1];	// Shift all the bits. This is incorrect for q_next[4] and q_next[2]
		q_next[4] = q[0];	// Give q_next[4] and q_next[2] their correct assignments
		q_next[2] = q[3] ^ q[0];
	end
	
	
	// This is just a set of DFFs. I chose to compute the connections between the
	// DFFs above in its own combinational always block, but you can combine them if you wish.
	// You'll get the same circuit either way.
	// Edge-triggered always block: Use non-blocking assignments.
	always @(posedge clk) begin
		if (reset)
			q <= 5'h1;
		else
			q <= q_next;
	end
	
endmodule

5.     3-bit LFSR

题目:

Write the Verilog code for this sequential circuit (Submodules are ok, but the top-level must be named top_module). Assume that you are going to implement the circuit on the DE1-SoC board. Connect the R inputs to the SW switches, connect Clock to KEY[0], and L to KEY[1]. Connect the Q outputs to the red lights LEDR.

HDLBits学习笔记——移位寄存器

 为题目所给的时序逻辑电路写一段代码。其中输入R为SW,时钟信号为KEY[0],选择器判别信号为KEY[1],输出Q为LEDR。

思路:

将时序和组合分别写在两个模块内,一个实现触发器功能,一个实现选择器功能。

代码:

module top_module (
	input [2:0] SW,      // R
	input [1:0] KEY,     // L and clk
	output [2:0] LEDR);  // Q
    reg [2:0] temp;
    always@(*) begin
        temp[0] = KEY[1] ? SW[0] : LEDR[2];
        temp[1] = KEY[1] ? SW[1] : LEDR[0];
        temp[2] = KEY[1] ? SW[2] : (LEDR[2]^LEDR[1]);
    end
    always@(posedge KEY[0]) begin
        LEDR[2] <= temp[2];
        LEDR[1] <= temp[1];
        LEDR[0] <= temp[0];
    end
endmodule

6. 32-bit LFSR

题目:

Build a 32-bit Galois LFSR with taps at bit positions 32, 22, 2, and 1.

This is long enough that you'd want to use vectors, not 32 instantiations of DFFs.

思路:

设计一个32位的LFSR,抽头位置在32,22,2,1。与LFSR5思路一致,只是寄存器位数较大,因此题目提示我们不要实例化32个触发器。

可以直接对Q(即qnext)进行移位运算,再对抽头位置逐一进行修正。

代码:

module top_module(
    input clk,
    input reset,    // Active-high synchronous reset to 32'h1
    output [31:0] q
); 
    reg [31:0] Q;
    always@(*) begin
        Q = {q[0],q[31:1]};
        Q[21] = q[22]^q[0];
        Q[1] = q[2]^q[0];
        Q[0] = q[1]^q[0];
    end
    always@(posedge clk) begin
        if(reset)
            q <= 32'h1;
    	else 
            q <= Q;
    end
endmodule

7.Shift register(Exams/m2014 q4k)

题目:

Implement the following circuit:

HDLBits学习笔记——移位寄存器

 思路:

实现上述时序电路,包含4个DFF,1个输入in,1个输入out,所有触发器具有相同输入的时钟信号以及同步复位信号resetn(低电平有效)。

考虑创建一个寄存器Q来存储触发器的输出值。

代码:

module top_module (
    input clk,
    input resetn,   // synchronous reset
    input in,
    output out);
    reg[3:1] Q;
    always@(posedge clk) begin
        if(!resetn) begin
            Q <= 0;
        	out <=0;
        end
        else begin
            Q[1] <= in;
            Q[2] <= Q[1];
            Q[3] <= Q[2];
            out <= Q[3];
        end
    end
endmodule

官方解法:

module top_module (
	input clk,
	input resetn,
	input in,
	output out
);

	reg [3:0] sr;
	
	// Create a shift register named sr. It shifts in "in".
	always @(posedge clk) begin
		if (~resetn)		// Synchronous active-low reset
			sr <= 0;
		else 
			sr <= {sr[2:0], in};
	end
	
	assign out = sr[3];		// Output the final bit (sr[3])

endmodule

8.Shift register(Exams/2014 q4b)

题目:

Consider the n-bit shift register circuit shown below:

HDLBits学习笔记——移位寄存器

Write a top-level Verilog module (named top_module) for the shift register, assuming that n = 4. Instantiate four copies of your MUXDFF subcircuit in your top-level module. Assume that you are going to implement the circuit on the DE2 board.

  • Connect the R inputs to the SW switches,
  • clk to KEY[0],
  • E to KEY[1],
  • L to KEY[2], and
  • w to KEY[3].
  • Connect the outputs to the red lights LEDR[3:0].

(Reuse your MUXDFF from exams/2014_q4a.)

思路:

题目给出了一个n-bit移位寄存器电路,要求我们按照该电路的模式设计一个4-bit移位寄存器。

题目要求实例化4个由“两个选择器+单个触发器“”组成的MUXDFF来完成电路设计。

代码:

module top_module (
    input [3:0] SW,
    input [3:0] KEY,
    output [3:0] LEDR
); //
    MUXDFF u4(
        .clk(KEY[0]),
        .E	(KEY[1]),
        .R	(SW[3]),
        .L	(KEY[2]),
        .w	(KEY[3]),
        .Q	(LEDR[3])
    );
	MUXDFF u3(
        .clk(KEY[0]),
        .E	(KEY[1]),
        .R	(SW[2]),
        .L	(KEY[2]),
        .w	(LEDR[3]),
        .Q	(LEDR[2])
    );
    MUXDFF u2(
        .clk(KEY[0]),
        .E	(KEY[1]),
        .R	(SW[1]),
        .L	(KEY[2]),
        .w	(LEDR[2]),
        .Q	(LEDR[1])
    );
    MUXDFF u1(
        .clk(KEY[0]),
        .E	(KEY[1]),
        .R	(SW[0]),
        .L	(KEY[2]),
        .w	(LEDR[1]),
        .Q	(LEDR[0])
    );
endmodule

module MUXDFF (
    input clk,
    input E,
    input R,
    input L,
    input w,
    output Q
);
    wire t1,t2;
    always@(*) begin
        t1 = E ? w:Q;
        t2 = L ? R:t1;
    end
    always@(posedge clk) begin
        Q <= t2;
    end
endmodule

9.     3-input LUT(Exams/ece241 2013 q12)

题目:

In this question, you will design a circuit for an 8x1 memory, where writing to the memory is accomplished by shifting-in bits, and reading is "random access", as in a typical RAM. You will then use the circuit to realize a 3-input logic function.

First, create an 8-bit shift register with 8 D-type flip-flops. Label the flip-flop outputs from Q[0]...Q[7]. The shift register input should be called S, which feeds the input of Q[0] (MSB is shifted in first). The enable input controls whether to shift. Then, extend the circuit to have 3 additional inputs A,B,C and an output Z. The circuit's behaviour should be as follows: when ABC is 000, Z=Q[0], when ABC is 001, Z=Q[1], and so on. Your circuit should contain ONLY the 8-bit shift register, and multiplexers. (Aside: this circuit is called a 3-input look-up-table (LUT)).

思路:

用8个触发器和1个3输入单输出的选择器设计一个3输入的查找表。

触发器输出Q[7:0]可以用向量完成,这样就不需要实例化8个D触发器。

查找表部分,因为只是一个3-input的查找表,直接用case枚举了所有可能,实际上在设计一个n-input的查找表时,代码的第一个always块部分应该直接用Z=Q[{A,B,C}]代替。

代码:

module top_module (
    input clk,
    input enable,
    input S,
    input A, B, C,
    output Z ); 
    reg [7:0] Q;
    always@(*) begin
            case({A,B,C})
                3'b000: Z = Q[0];
                3'b001: Z = Q[1];
                3'b010: Z = Q[2];
                3'b011: Z = Q[3];
                3'b100: Z = Q[4];
                3'b101: Z = Q[5];
                3'b110: Z = Q[6];
                3'b111: Z = Q[7];
            endcase
    end
    always@(posedge clk) begin
        if(enable)
            Q <= {Q[6:0],S};
        else
            Q <= Q;
    end
endmodule

到了这里,关于HDLBits学习笔记——移位寄存器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • verilog——移位寄存器

    在Verilog中,你可以使用移位寄存器来实现数据的移位操作。移位寄存器是一种常用的数字电路,用于将数据向左或向右移动一个或多个位置。这在数字信号处理、通信系统和其他应用中非常有用。以下是一个使用Verilog实现的简单移位寄存器的示例: module ShiftRegister (   inpu

    2024年02月05日
    浏览(38)
  • FPGA之移位寄存器

            SLICEM中的LUT可以配置为32位移位寄存器,而无需使用slice中可用的触发器。以这种方式使用,每个LUT 可以将串 行数据延迟 1 到 32 个时钟周期。移入D (DI1 LUT 引脚)和移出 Q31(MC31 LUT 引脚)线路将LUT级联,以形成更大的移位寄存器。因此,SLICEM 中的四个 LUT 被级联以

    2024年02月19日
    浏览(43)
  • Verilog实现移位寄存器

    Verilog实现8位环形移位寄存器 左移: 环形就是首尾相连 右移: 普通的移位寄存器用for语句实现: 普通左移: tb测试: 图形分析: 双向shift:就是加个判断

    2024年02月11日
    浏览(47)
  • LABVIEW的移位寄存器

    移位寄存器是数据的容器,可以包含任何数据类型。 添加移位寄存器后,在循环结构左右两侧的平行位置将各增加一个包含三角形的方框。左侧的方框代表上一次循环的运行结果,而右侧的代表本次循环要输入的结果。  最终得到5次循环后的结果。 接下来我们做一个通过移

    2024年02月11日
    浏览(40)
  • 线性反馈移位寄存器(LSFR)

    流密码的流密钥产生器可以通过线性驱动和非线性组合两部分来实现。而线性驱动部分可以由线性反馈移位寄存器(LFSR)来实现。 线性反馈移位寄存器(LFSR):通常由移位寄存器和异或门逻辑组成。其主要应用在:伪随机数,伪噪声序列,计数器,BIST,数据的加密和CRC校验等

    2024年02月17日
    浏览(40)
  • 【FGPA】Verilog:移位寄存器 | 环形计数器 | 4bit移位寄存器的实现 | 4bit环形计数器的实现

      目录 Ⅰ. 理论部分 0x00 移位寄存器(Shift Register) 0x01 环形计数器(Ring Counter)

    2024年02月05日
    浏览(34)
  • 4.3 移位寄存器的实现和应用

    在数字电路中,移位寄存器(英语:shift register)是一种在若干相同时间脉冲下工作的以触发器为基础的器件,数据以并行或串行的方式输入到该器件中,然后每个时间脉冲依次向左或右移动一个比特,在输出端进行输出。这种移位寄存器是一维的,事实上还有多维的移位寄存

    2024年02月10日
    浏览(39)
  • 使用FPGA实现桶形移位寄存器

    我给大家介绍的是逻辑/算术左移移位寄存器。实现的功能是根据输入信号shift将输入信号进行移位,高位移除,低位补0。我建立的工程是由3个独立的桶形移位寄存器组成的。 library ieee; use ieee.std_logic_1164.all; entity barrel is      port( inp : in std_logic_vector(7 downto 0);          

    2024年04月29日
    浏览(38)
  • Verilog基础之十一、移位寄存器实现

    目录 一、前言 二、工程设计 ​2.1 工程代码 2.2 综合结果 2.3 仿真结果     移位寄存器SRL在工程中属于使用频率较高个模块,可用于存储数据,实现串并转换;根据数据移动方向可分为左移寄存器,右移寄存器,左移是向数据高位移动,右移是向数据低位移动。  工程中包

    2024年02月11日
    浏览(36)
  • 设计一个8位双向循环移位寄存器vhdl

    设计一个8位双向循环移位寄存器vhdl 状态表如下: CLK RESET LOAD M 工作状态 × 0 × × 复位 ↑ 1 1 × 置数 ↑ 1 0 1 左移 ↑ 1 0 0 右移 (4)不考虑串行输出,移动不能用移位操作符。

    2024年02月11日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包