HDL-Bits 刷题记录 02

简介: 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

m2014 q4a

写一个D锁

module top_module (
    input d, 
    input ena,
    output q);
    always@(*)begin
        if(ena)
            q=d;
    end
endmodule

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

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

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

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

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

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

ece241 2013 q7

JK 触发器 真值表

J K Q
0 0 Qold
0 1 0
1 0 1
1 1 ~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

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

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

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

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

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

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

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

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

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月前
|
存储 安全 数据安全/隐私保护
C++位之奥秘(Mysteries of Bits)
C++位之奥秘(Mysteries of Bits)
45 0
C++位之奥秘(Mysteries of Bits)
|
5月前
|
存储 C语言
【CSAPP随笔】CH3:Bits, Bytes, and Integers
【CSAPP随笔】CH3:Bits, Bytes, and Integers
44 0
|
4月前
|
编译器
#define 宏定义看这一篇文章就够了
#define 宏定义看这一篇文章就够了
115 0
|
6月前
|
存储 芯片
STM32速成笔记(十一)—EEPROM(AT24C02)
本文详细介绍了什么是AT24C02,介绍了它的引脚,读/写时序,给出了应用实例和详细的程序设计。最后,简单介绍了AT24C02的应用场景。
153 0
STM32速成笔记(十一)—EEPROM(AT24C02)
|
7月前
|
Go
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
28 0
|
C语言
嵌入式C语言基础:一文读懂#define与typedef的区别
嵌入式C语言基础:一文读懂#define与typedef的区别
123 0
|
数据格式
HDL-Bits 刷题记录 04
HDL-Bits 刷题记录 04 搬运本人博客
90 0
HDL-Bits 刷题记录 04
HDL-Bits 刷题记录 03
HDL-Bits 刷题记录 03
73 0
HDL-Bits 刷题记录 03
|
存储 测试技术 异构计算
HDL-Bits 刷题记录 01
HDL-Bits 刷题记录 01
307 0
HDL-Bits 刷题记录 01