• HDL-Bits 刷题记录 02


    Dff16e

    16 个 D 触发器。有时只修改一组触发器的一部分。
    字节使能输入控制 16 个寄存器中的每个字节是否应在该周期写入。byteena[1]控制高字节d[15:8],而byteena[0]控制低字节d[7:0]。
    resetn是一个同步的低电平有效复位。
    所有 DFF 都应由clk的上升沿触发。

    module top_module (
        input clk,
        input resetn,
        input [1:0] byteena,
        input [15:0] d,
        output [15:0] q
    );
        always @(posedge clk)begin
            if(!resetn)
                q <= 16'd0;
            else begin
                case (byteena)
                2'b10:
                    q <= (q&16'h00FF)|(d&16'hFF00);
                2'b11:
                    q <= d;
                2'b01: 
                    q <= (q&16'hFF00)|(d&16'h00FF);
                2'b00: 
                    q <= q;
                default: q <= q;
                endcase
                
            end
        end
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    m2014 q4a

    写一个D锁

    module top_module (
        input d, 
        input ena,
        output q);
        always@(*)begin
            if(ena)
                q=d;
        end
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    m2014 q4b

    实现下图
    m2014_q4b.png

    module top_module (
        input clk,
        input d, 
        input ar,   // asynchronous reset
        output q);
        always@(posedge clk or posedge ar) begin
            if(ar)
                q <= 1'b0;
            else begin
                q <= d ;
            end
    
        end
    endmodule
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    m2014 q4c

    m2014_q4c.png

    module top_module (
        input clk,
        input d, 
        input r,   // synchronous reset
        output q);
        always@(posedge clk ) begin
            if(r)
                q <= 1'b0;
            else begin
                q <= d ;
            end
    
        end
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    m2014 q4d

    m2014_q4d.png

    module top_module (
        input clk,
        input in, 
        output out);
        //reg out1 ;
        //assign out = out1;
        always@(posedge clk) begin
            out <= out ^ in;
        end
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    Mt2015 muxdff

    假设您要为此电路实现分层 Verilog 代码,使用其中具有触发器和多路复用器的子模块的三个实例化。
    包含一个触发器和多路复用器
    Mt2015_muxdff.png

    他就要一个多路复用器和触发器的拼接底层块 以上

    module top_module (
        input clk,
        input L,
        input r_in,
        input q_in,
        output reg Q);
        wire temp;
        assign temp=L?r_in:q_in;
        always@(posedge clk)begin
            Q=temp;
        end
     endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    Exams/2014 q4a

    也是要一个子模块
    Exams_2014_q4a.png

    module top_module (
        input clk,
        input w, R, E, L,
        output Q
    );
    wire sel1,sel2;
    assign  sel1 = E?w:Q;
    assign  sel2 = L?R:sel1;
    always @(posedge clk) begin
        Q =sel2;
    end
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    ece241 2014 q4
    module top_module (
        input clk,
        input x,
        output z
    ); 
    wire d1,d2,d3;
    wire q1,q2,q3;
    assign z = ~(q1|q2|q3);
    assign d1 = x^q1;
    assign d2 = x&(~q2);
    assign d3 = x|(~q3);
    always@(posedge clk) begin
        q1 <= d1;
        q2 <= d2;
        q3 <= d3;
    end
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    ece241 2013 q7

    JK 触发器 真值表

    JKQ
    00Qold
    010
    101
    11~Qold
    module top_module (
        input clk,
        input j,
        input k,
        output Q); 
    always @ (posedge clk)begin
        if (j^k) 
            Q = j;
        else if (j)
            Q = ~Q;
        else
            Q = Q;
    end
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    Edgedetect

    8位上升沿检测

    module top_module (
        input clk,
        input [7:0] in,
        output [7:0] pedge
    );
    
    reg [7:0] in_past;
    genvar i;
    generate
        for(i = 0;i<8;i++) begin:edgedetect
            always@(posedge clk) begin
                if(in[i]==1&&in_past[i]==0)
                    pedge[i] <= 1'b1;
                else 
                    pedge[i] <= 1'b0;
            end
        end
    endgenerate
    
    always @(posedge clk) begin
        in_past <= in;
    end 
    
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    Edgedetect2

    双边检测 8位

    module top_module (
        input clk,
        input [7:0] in,
        output [7:0] anyedge
    );
    
    reg [7:0] in_past;
    genvar i;
    generate
        for(i = 0;i<8;i++) begin:edgedetect
            always@(posedge clk) begin
                if(in[i]^in_past[i])
                    anyedge[i] <= 1'b1;
                else 
                    anyedge[i] <= 1'b0;
            end
        end
    endgenerate
    
    always @(posedge clk) begin
        in_past <= in;
    end 
    
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    Edgecapture

    边沿检测,触发之后 不复位不清零

    module top_module (
        input clk,
        input reset,
        input [31:0] in,
        output [31:0] out
    );
    
        reg [31:0] in_past;
    genvar i;
    generate
        for(i = 0;i<32;i++) begin:edgedetect
            always@(posedge clk) begin
                if(reset)
                    out[i]<= 1'd0;
                else if(in[i]==0&&in_past[i]==1&&out[i]==0)
                    out[i] <= 1'b1;
                else if(out[i]==1'b1)
                    out[i]<=1'b1;
                else
                    out[i] <= 1'b0;
            end
        end
    endgenerate
    
    always @(posedge clk) begin
        in_past <= in;
    end 
    
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    Dualedge

    构建一个双边触发器。逻辑如下:
    Dualedge.png

    module top_module (
        input clk,
        input d,
        output q
    );
        reg q1,q2;
        always@(posedge clk) begin
            q1<=d;
        end
        always@(negedge clk) begin
            q2<=d;
        end
        assign q = clk?q1:q2;
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Counters

    Count15

    构建一个从 0 到 15(含)计数的 4 位二进制计数器,周期为 16。复位输入是同步的,应将计数器复位为 0。

    module top_module (
        input clk,
        input reset,      // Synchronous active-high reset
        output [3:0] q);
        reg [4:0] cnt_reg = 0;
        assign q = cnt_reg;
        always @ (posedge clk) begin
            if(reset)
                cnt_reg <= 0;
            else if(cnt_reg <15)
                cnt_reg <= cnt_reg + 1;
            else 
                cnt_reg <= 0;
        end
    
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    Count10

    构建一个从 0 到 9(含)计数的十进制计数器,周期为 10。复位输入是同步的,应将计数器复位为 0。

    module top_module (
        input clk,
        input reset,        // Synchronous active-high reset
        output [3:0] q);
        reg [3:0] cnt_reg = 0;
        assign q = cnt_reg;
        always @ (posedge clk) begin
            if(reset)
                cnt_reg <= 0;
            else if(cnt_reg <9)
                cnt_reg <= cnt_reg + 1;
            else 
                cnt_reg <= 0;
        end
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    Count1to10

    制作一个从 1 到 10 的十年计数器,包括 1 到 10。复位输入是同步的,应将计数器复位为 1。

    module top_module (
        input clk,
        input reset,
        output [3:0] q);
        reg [3:0] cnt_reg = 1;
        assign q = cnt_reg;
        always @ (posedge clk) begin
            if(reset)
                cnt_reg <= 1;
            else if(cnt_reg <10)
                cnt_reg <= cnt_reg + 1;
            else 
                cnt_reg <= 1;
        end
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    Countslow

    构建一个从 0 到 9 计数的十进制计数器,周期为 10。复位输入是同步的,应该将计数器复位为 0。我们希望能够暂停计数器,而不是总是在每个时钟周期递增,所以slowena输入指示计数器何时应该增加

    module top_module (
        input clk,
        input slowena,
        input reset,
        output [3:0] q);
        reg [3:0] cnt_reg = 0;
        assign q = cnt_reg;
        always @ (posedge clk) begin
            if(reset)
                cnt_reg <= 0;
            else if(!slowena)
                cnt_reg <= cnt_reg;
            else if(cnt_reg <9)
                cnt_reg <= cnt_reg + 1;
            else 
                cnt_reg <= 0;
        end
    endmodule
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    ece241 2014 q7a

    设计一个具有以下输入和输出的 1-12 计数器:

    • 复位同步高电平有效复位,强制计数器为 1
    • 启用设置高以使计数器运行
    • Clk正边沿触发时钟输入
    • Q[3:0]计数器的输出
    • c_enable, c_load, c_d[3:0]控制信号进入提供的 4 位计数器,因此可以验证正确的操作。
      module count4(
      input clk,
      input enable,
      input load,
      input [3:0] d,
      output reg [3:0] Q
      );
      fxxk ,题目看不懂 ,按照时序硬写的,过了 不知道他想要啥鬼东西
    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?1:(enable?c_load_reg:0);
        reg [3:0] cnt_reg = 1;
        reg c_load_reg ;
        assign Q = cnt_reg;
        assign c_d = c_load?1:0;
        always @ (posedge clk) begin
            if(reset) begin
                cnt_reg <= 1;
                c_load_reg <= 0;
            end
            else if(!enable) begin
                cnt_reg <= cnt_reg;
                if(cnt_reg ==12)
                    c_load_reg <= 1;
                else 
                    c_load_reg <= 0;
            end
            else if(cnt_reg <11) begin 
                cnt_reg <= cnt_reg + 1;
                c_load_reg <= 0;
            end
            else if(cnt_reg =11) begin 
                cnt_reg <= cnt_reg + 1;
                c_load_reg <= 1;
            end
            else    begin 
                cnt_reg <= 1;
                c_load_reg <= 0;
            end
        end
        
        count4 the_counter (clk, c_enable, c_load, c_d);
    
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    Camunda 7.x 系列【48】候选用户和用户组
    表驱动法在STM32中的应用
    文件操作 IO
    探索“科技助实”,上海交通大学、蚂蚁集团等发起第三届ATEC科技精英赛
    【2024最新】Spring面试题
    运维大数据平台的建设与实践探索
    uniapp开发微信小程序:为什么用户授权信息弹窗没有了?
    使用PyTorch搭建VGG模型进行图像风格迁移实战(附源码和数据集)
    微信小程序
    flutter系列之:用来管理复杂状态的State详解
  • 原文地址:https://blog.csdn.net/qq_31764341/article/details/126829470