目录

3.2 Sequential Logic

3.2.2 Counters

1. Four-bit binary counter​

2.Decade counter

3. Decade counter again

4. Slow decade counter

5. Counter 1-12

6. Counter 1000

7. 4-digit decimal counter

8. 12-hour clock

3.2.3 Shift Registers

1. 4-bit shift register

2. Left/right rotator

3. Left/right arithmetic shift by 1 or 8

4. 5-bit LFSR

5. 3-bit LFSR

6. 32-bit LFSR

7. Shift register

8. Shift register

9. 3-input LUT

3.2.4 More Circuits

​​​​1.Rule 90

2. Rule 110

3. Conway's Game of Life 16x16​​​​​​​

3.2 Sequential Logic

3.2.2 Counters

1. Four-bit binary counter

module top_module (input clk,input reset,      // Synchronous active-high resetoutput reg [3:0] q);always @(posedge clk)beginif(reset)beginq<='d0;endelse beginq<=q+1'b1;endendendmodule

2.Decade counter

module top_module (input clk,input reset,        // Synchronous active-high resetoutput [3:0] q);always @(posedge clk)beginif(reset) q<=4'd0;else if(q==4'd9) q<=4'd0;else q<=q+1'b1;end
endmodule

3. Decade counter again

Make a decade counter that counts 1 through 10, inclusive. The reset input is synchronous, and should reset the counter to 1.

module top_module (input clk,input reset,output [3:0] q); always @(posedge clk) beginif(reset) q<=4'd1;else if(q==10) q<=4'd1;else q<=q+1;endendmodule

4. Slow decade counter

Build a decade counter that counts from 0 through 9, inclusive, with a period of 10. The reset input is synchronous, and should reset the counter to 0. We want to be able to pause the counter rather than always incrementing every clock cycle, so the slowena input indicates when the counter should increment.

module top_module (input clk,input slowena,input reset,output reg [3:0] q);always @(posedge clk)beginif(reset) q<='d0;else if(slowena &&q==9) q<='d0;else if(slowena) q<=q+1'b1;endendmodule

5. Counter 1-12

Design a 1-12 counter with the following inputs and outputs:

  • Reset Synchronous active-high reset that forces the counter to 1

  • Enable Set high for the counter to run

  • Clk Positive edge-triggered clock input

  • Q[3:0] The output of the counter

  • c_enable, c_load, c_d[3:0] Control signals going to the provided 4-bit counter, so correct operation can be verified.

You have the following components available:

  • the 4-bit binary counter (count4) below, which has Enable and synchronous parallel-load inputs (load has higher priority than enable). The count4module is provided to you. Instantiate it in your circuit.

  • logic gates

module count4(
input clk,
input enable,
input load,
input [3:0] d,
output reg [3:0] Q
);
The c_enablec_load, and c_d outputs are the signals that go to the internal counter's enableload, and d inputs, respectively. Their purpose is to allow these signals to be checked for correctness.

module top_module (input clk,input reset,input enable,output [3:0] Q,output c_enable,output c_load,output [3:0] c_d
); //assign c_enable=enable;assign c_load=reset|(enable && Q==12);assign c_d=c_load?1:0;count4 the_counter (clk, c_enable,c_load, c_d,Q/*, ... */ );endmodule

6. Counter 1000

From a 1000 Hz clock, derive a 1 Hz signal, called OneHertz, that could be used to drive an Enable signal for a set of hour/minute/second counters to create a digital wall clock. Since we want the clock to count once per second, the OneHertz signal must be asserted for exactly one cycle each second. Build the frequency divider using modulo-10 (BCD) counters and as few other gates as possible. Also output the enable signals from each of the BCD counters you use (c_enable[0] for the fastest counter, c_enable[2] for the slowest).

The following BCD counter is provided for you. Enable must be high for the counter to run. Reset is synchronous and set high to force the counter to zero. All counters in your circuit must directly use the same 1000 Hz signal.

module bcdcount (
input clk,
input reset,
input enable,
output reg [3:0] Q
);

module top_module (input clk,input reset,output OneHertz,output [2:0] c_enable
); //wire [3:0] q1,q2,q3;assign OneHertz=(q1==4'd9)&&(q2==4'd9)&&(q3==4'd9);assign c_enable ={q1==4'd9&q2==4'd9,q1==4'd9,1'b1};bcdcount counter0 (clk, reset, c_enable[0],q1/*, ... */);bcdcount counter1 (clk, reset, c_enable[1],q2/*, ... */);bcdcount counter2 (clk, reset, c_enable[2],q3/*, ... */);endmodule

7. 4-digit decimal counter

Build a 4-digit BCD (binary-coded decimal) counter. Each decimal digit is encoded using 4 bits: q[3:0] is the ones digit, q[7:4] is the tens digit, etc. For digits [3:1], also output an enable signal indicating when each of the upper three digits should be incremented.

You may want to instantiate or modify some one-digit decade counters.

module top_module (input clk,input reset,   // Synchronous active-high resetoutput [3:1] ena,output [15:0] q);always @(posedge clk)beginif(reset)beginq<='d0;endelse beginif(q[3:0]!='d9)beginq[3:0]<=q[3:0]+1'b1;endelse beginq[3:0]<='d0;q[7:4]<=q[7:4]+1'b1;if(q[7:4]=='d9)beginq[7:4]<='d0;q[11:8]<=q[11:8]+1'b1;if(q[11:8]=='d9)beginq[11:8]<='d0;q[15:12]<=q[15:12]+1'b1;if(q[15:12] == 4'd9)beginq <= 0;endendendendendendassign ena[1]=(q[3:0] == 4'd9)?1:0;assign ena[2]=(q[7:4] == 4'd9 && q[3:0] == 4'd9)?1:0;assign ena[3]=(q[11:8] == 4'd9 && q[7:4] == 4'd9 && q[3:0] == 4'd9)?1:0;endmodule

8. 12-hour clock

Create a set of counters suitable for use as a 12-hour clock (with am/pm indicator). Your counters are clocked by a fast-running clk, with a pulse on ena whenever your clock should increment (i.e., once per second).

reset resets the clock to 12:00 AM. pm is 0 for AM and 1 for PM. hh, mm, and ss are two BCD (Binary-Coded Decimal) digits each for hours (01-12), minutes (00-59), and seconds (00-59). Reset has higher priority than enable, and can occur even when not enabled.

The following timing diagram shows the rollover behaviour from 11:59:59 AM to 12:00:00 PM and the synchronous reset and enable behaviour.

module top_module(input clk,input reset,input ena,output pm,output [7:0] hh,output [7:0] mm,output [7:0] ss); always @(posedge clk)beginif(reset)beginpm<=1'b0;hh[7:4]<=4'd1;hh[3:0]<=4'd2;mm<=8'h00;ss<=8'h00;endelse beginif(ena==1)beginif(ss[3:0]!='d9)beginss[3:0]<=ss[3:0]+1;endelse beginss[3:0]<=4'd0;ss[7:4]<=ss[7:4]+1;if(ss[7:4]=='d5)beginss[7:4]<='d0;mm[3:0]<=mm[3:0]+1;if(mm[3:0]=='d9)beginmm[3:0]<='d0;mm[7:4]<=mm[7:4]+1;if(mm[7:4]=='d5)beginmm[7:4]<='d0;hh[3:0]<=hh[3:0]+1;if(hh[3:0]=='d9)beginhh[3:0]<='d0;hh[7:4]<=hh[7:4]+1;endif(hh[7:4]=='d1&&hh[3:0]=='d1)beginpm<=~pm;hh[7:4]<='d1;hh[3:0]<='d2;endif(hh[7:4]=='d1&&hh[3:0]=='d2)beginhh[7:4]<='d0;hh[3:0]<='d1;endendendendendendendendendmodule

3.2.3 Shift Registers

1. 4-bit shift register

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.

module top_module(input clk,input areset,  // async active-high reset to zeroinput load,input ena,input [3:0] data,output reg [3:0] q); always @(posedge clk  or posedge areset)beginif(areset) q<=4'd0;else if(load)q<=data;else if(ena) q<={1'b0,q[3:1]};else  q<=q;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.
  • module top_module(input clk,input load,input [1:0] ena,input [99:0] data,output reg [99:0] q); always @(posedge clk)beginif(load)q<=data;else if(ena==2'b01)q<={q[0],q[99:1]};else if(ena==2'b10)q<={q[98:0],q[99]};else q<=q;endendmodule

3. Left/right arithmetic shift by 1 or 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.
  • 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)beginif(load)beginq<=data;endelse if(ena)begincase(amount)2'b00:q<={q[62:0],1'b0};2'b01:q<={q[55:0],8'b00000000};2'b10:q<={q[63],q[63:1]};2'b11:q<={{8{q[63]}},q[63:8]};default: ;endcaseendelse beginq<=q;endendendmodule

4. 5-bit LFSR

线性移位寄存器:

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.

module top_module(input clk,input reset,    // Active-high synchronous reset to 5'h1output [4:0] q
); always @(posedge clk)beginif(reset)beginq<='d1;endelse beginq[0]<=q[1];q[1]<=q[2];q[2]<=q[3]^q[0];q[3]<=q[4];q[4]<=1'b0^q[0];endendendmodule

5. 3-bit LFSR

module top_module (input [2:0] SW,      // Rinput [1:0] KEY,     // L and clkoutput [2:0] LEDR);  // Qalways @(posedge KEY[0])beginif(KEY[1])beginLEDR[2]<=SW[2];LEDR[1]<=SW[1];LEDR[0]<=SW[0];endelse beginLEDR[2]<=LEDR[1]^LEDR[2];LEDR[1]<=LEDR[0];LEDR[0]<=LEDR[2];endendendmodule

6. 32-bit LFSR

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

module top_module(input clk,input reset,    // Active-high synchronous reset to 32'h1output [31:0] q
); always @(posedge clk)beginif(reset)beginq<=32'h1;endelse beginq[0]<=q[0]^q[1];q[1]<=q[0]^q[2];q[2]<=q[3];q[3]<=q[4];q[4]<=q[5]; q[5]<=q[6];q[6]<=q[7];q[7]<=q[8];q[8]<=q[9];q[9]<=q[10];q[10]<=q[11];q[11]<=q[12];q[12]<=q[13];q[13]<=q[14];q[14]<=q[15];q[15]<=q[16];q[16]<=q[17];q[17]<=q[18];q[18]<=q[19];q[19]<=q[20];q[20]<=q[21];q[21]<=q[0]^q[22];q[22]<=q[23];q[23]<=q[24];q[24]<=q[25];q[25]<=q[26];q[26]<=q[27];q[27]<=q[28];q[28]<=q[29];q[29]<=q[30];q[30]<=q[31];q[31]<=q[0];endendendmodule

7. Shift register

module top_module (input clk,input resetn,   // synchronous resetinput in,output reg out);reg q1,q2,q3;always @(posedge clk)beginif(resetn==1'b0)beginout<=1'b0;endelse beginout<=q3;endendalways @(posedge clk)beginif(resetn==1'b0)beginq3<=1'b0;endelse beginq3<=q2;endendalways @(posedge clk)beginif(resetn==1'b0)beginq2<=1'b0;endelse beginq2<=q1;endendalways @(posedge clk)beginif(resetn==1'b0)beginq1<=1'b0;endelse beginq1<=in;endendendmodule

8. Shift register

module top_module (input [3:0] SW,input [3:0] KEY,output [3:0] LEDR
); //MUXDFF mux1(KEY[0],KEY[1],KEY[2],KEY[3],SW[3],LEDR[3]); MUXDFF mux2(KEY[0],KEY[1],KEY[2],LEDR[3],SW[2],LEDR[2]); MUXDFF mux3(KEY[0],KEY[1],KEY[2],LEDR[2],SW[1],LEDR[1]); MUXDFF mux4(KEY[0],KEY[1],KEY[2],LEDR[1],SW[0],LEDR[0]);endmodulemodule MUXDFF (input clk,input E,input L,input W,input R,output Q
);always @(posedge clk)beginif(L)beginQ<=R;endelse beginif(E)beginQ<=W;endelse beginQ<=Q;endendendendmodule

9. 3-input LUT

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)).

module top_module (input clk,input enable,input S,input A, B, C,output Z ); reg [7:0] Q;always@(posedge clk)beginif(enable) Q[7:0] <= {Q[6:0], S};endalways@(*)begincase({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];endcaseend
endmodule

3.2.4 More Circuits

​​​​1.Rule 90

Rule 90 is a one-dimensional cellular automaton with interesting properties.

The rules are simple. There is a one-dimensional array of cells (on or off). At each time step, the next state of each cell is the XOR of the cell's two current neighbours. A more verbose way of expressing this rule is the following table, where a cell's next state is a function of itself and its two neighbours:

Left Center Right Center's next state
1 1 1 0
1 1 0 1
1 0 1 0
1 0 0 1
0 1 1 1
0 1 0 0
0 0 1 1
0 0 0 0

(The name "Rule 90" comes from reading the "next state" column: 01011010 is decimal 90.)

In this circuit, create a 512-cell system (q[511:0]), and advance by one time step each clock cycle. The load input indicates the state of the system should be loaded with data[511:0]. Assume the boundaries (q[-1] and q[512]) are both zero (off).

module top_module(input clk,input load,input [511:0] data,output [511:0] q );always @(posedge clk)beginif(load)beginq<=data;endelse beginq<={1'b0,q[511:1]}^{q[510:0],1'b0};endend
endmodule

2. Rule 110

Rule 110 is a one-dimensional cellular automaton with interesting properties (such as being Turing-complete).

There is a one-dimensional array of cells (on or off). At each time step, the state of each cell changes. In Rule 110, the next state of each cell depends only on itself and its two neighbours, according to the following table:

Left Center Right Center's next state
1 1 1 0
1 1 0 1
1 0 1 1
1 0 0 0
0 1 1 1
0 1 0 1
0 0 1 1
0 0 0 0

(The name "Rule 110" comes from reading the "next state" column: 01101110 is decimal 110.)

In this circuit, create a 512-cell system (q[511:0]), and advance by one time step each clock cycle. The load input indicates the state of the system should be loaded with data[511:0]. Assume the boundaries (q[-1] and q[512]) are both zero (off).

module top_module(input clk,input load,input [511:0] data,output [511:0] q
); always @(posedge clk) beginif(load == 1'b1)beginq <= data;endelse beginq <= (q^{q[510:0],1'b0}) | (~{1'b0,q[511:1]}&{q[510:0],1'b0});  endend
endmodule

3. Conway's Game of Life 16x16

HDLBits 系列(7)——Sequential Logic(Counters、Shift Registers、More Circuits)相关推荐

  1. HDLBits 系列(8)——Sequential Logic(Finite State Machines(一))

    目录 3.2 Sequential Logic 3.2.5 Finite State Machines 1. Simple FSM 1 (asynchronous reset) 2. Simple F ...

  2. Verilog学习笔记HDLBits——Shift Registers

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.Shift Registers 1.4-bit shift register 2.Left/right rotat ...

  3. Verilog HDLBits 第十三期:3.2.3 Shift Registers

    目录 前言 3.2.3.1 4-bit Shift Register(shift4) Solution: 3.2.3.2 Left/right rotator(rotate100) Solution: ...

  4. HDLBits 系列(0)专题目录

    本篇博文是近来总结HDLBits系列的目录,点击蓝色字体即可进入查看具体内容. HDLBits 系列(1)从HDLBits中获取灵感,整顿自己,稳步前行 HDLBits 系列(2)如何避免生成锁存器? ...

  5. HDLBits 系列(29)PS/2 mouse protocol(PS/2 packet parser and datapath)

    目录 序言 原题传送 题目解释 我的设计 序言 上篇博客: HDLBits 系列(28)PS/2 mouse protocol(PS/2 packet parser) 只对PS/2 mouse pro ...

  6. HDLBits 系列(31)Serial Receiver and Datapath

    目录 序言 原题复现 我的设计 序言 上篇博文: HDLBits 系列(30)Serial Receiver 写了串行接收器如何接收8位串行数据,正确接收8位串行数据后给一个接收完毕标志信号,这篇博文 ...

  7. 时序分析基本概念介绍Sequential logic

    今天我们要介绍的时序分析基本概念是Sequential logic.中文名,时序逻辑单元.时序逻辑单元是数字IC设计中另一类重要器件,它的特点是任意时刻的输出不仅取决于该时刻的输入,而且还和电路原来的 ...

  8. HDLBits 系列(22) Shift register

    目录 Shift register1 Shift register2 Shift register1 实现下面的电路: module top_module (input clk,input reset ...

  9. Verilog 语法练习:HDL Bits做题笔记(3.2 Circuits Sequential Logic )

    目录 1.Lateches and Flip-Flops 1.1.D flip-flop 1.2.D flip-flops 1.3. DFF with reset 1.4.DFF with reset ...

最新文章

  1. 《Pro ASP.NET MVC 3 Framework》学习笔记之九【Ninject的使用-下】
  2. boostrap 鼠标滚轮滑动图片_Bootstrap幻灯片轮播图支持触屏左右手势滑动的实现方法...
  3. mysql 5.7源码包安装教程_MYSQL5.7源码包编译安装
  4. [LeetCode][JavaScript]Invert Binary Tree 反转二叉树
  5. python零基础学习教程之Python 运算符
  6. Python3 透明网桥算法
  7. 如何找到Partner 相关设置里哪些是SAP 标准deliver的,哪些是我们自己创建的
  8. electron ant-design-vue 不能用_基于 Electron 桌面消息管理客户端iGot
  9. 小程序短视频项目———上传短视频业务流程简介
  10. 使用flash在IPAD2上播放FLV效率不高
  11. 11.python之线程,协程,进程,
  12. jmeter tps指标在哪里看_性能之路——性能测试连载 (3)-性能指标
  13. 今天讲座的感悟--java
  14. linux下的磁盘配额简介暨 linux下加挂硬盘续
  15. centos 升级 glibc和glibcxxx ,解决error: Failed dependencies等问题
  16. 【2022最详细--SVN客户端安装教程】
  17. 基于jqUI的日期选择(‘yy-mm-dd’)
  18. 酷狗音乐榜单歌曲获取解析教程
  19. php怎么把中文转,php如何把汉字转换成拼音
  20. C++特征码查找 附加案例

热门文章

  1. Python中的range函数和arange函数的区别与联系
  2. Spring Web(第一部分)
  3. [Python|Clothoid]Clothoid曲线(回旋曲线)与直角坐标求解的python实现
  4. 导图解文 从梦想到财富(02)你拥有最宝贵的财富是什么?
  5. C++语言程序设计第五版 - 郑莉(第六章课后习题)
  6. 年终考核 对你的上司,你是如何评价的
  7. MySQL数据库编程--函数
  8. python小工具开发_python音乐下载小工具源码(tkinter)
  9. 信号完整性之眼图(eye)理解(一)
  10. 百度大字版APP支持语音搜索,老年人也能玩转互联网