HDLBits从零开始——第171题到178题答案

目录

第171题:Sequential circuit 8

第172题:Sequential circuit 9

第173题:Sequential circuit 10

第174题:Clock

第175题:Testbench1

第176题:AND gate

第177题:Testbench2

第178题:T flip-flop


第171题:Sequential circuit 8

module top_module 
(
    input   clock   ,
    input   a       ,
    output  p       ,
    output  q 
);

always@(negedge clock)
    q <= p;
    
assign  p = clock ? a : p;

endmodule

第172题:Sequential circuit 9

module top_module 
(
    input           clk ,
    input           a   ,
    output  [3:0]   q 
);

always@(posedge clk)
    if(a)
        q <= 4'd4;
    else if(q==4'd6)
        q <= 4'd0;
    else
        q <= q+1'b1;

endmodule

第173题:Sequential circuit 10

module top_module 
(
    input       clk     ,
    input       a       ,
    input       b       ,
    output      q       ,
    output      state  
);

always@(posedge clk)
    if(a==b)
        state <= a;
    else
        state <= state;

assign  q = a==b ? state : ~state;

endmodule

第174题:Clock

`timescale  1ps/1ps
module top_module ( );

reg clk;

initial
    begin
        clk = 0;
    end

always #5   clk = ~clk;

dut dut_inst
( 
    .clk (clk)
) ;

endmodule

第175题:Testbench1

`timescale  1ps/1ps
module top_module ( output reg A, output reg B );

initial
    begin
        A <= 1'b0;
        B <= 1'b0;
        #10
        A <= 1'b1;
        #5
        B <= 1'b1;
        #5
        A <= 1'b0;
        #20
        B <= 1'b0;
    end

endmodule

第176题:AND gate

`timescale 1ps/1ps
module top_module();

reg [1:0]   in;
wire        out;

initial
    begin
        in <= 2'b00;
        #10
        in <= 2'b01;
        #10
        in <= 2'b10;
        #10
        in <= 2'b11;
    end

andgate andgate_inst
(
    .in (in),
    .out(out)
);

endmodule

第177题:Testbench2

`timescale 1ps/1ps
module top_module();

reg         clk ;
reg         in  ;
reg [2:0]   s   ;
wire        out ;

initial
    begin
        clk = 1'b0;
        in <= 1'b0;
        s <= 3'd2;
        #10
        s <= 3'd6;
        #10
        in <= 1'b1;
        s <= 3'd2;
        #10
        in <= 1'b0;
        s <= 3'd7;
        #10
        in <= 1'b1;
        s <= 3'd0;
        #30
        in <= 1'b0;
    end
    
always #5   clk = ~clk;

q7 q7_inst
(
    .clk (clk),
    .in  (in ),
    .s   (s  ),
    .out (out)
);

endmodule

第178题:T flip-flop

`timescale 1ps/1ps
module top_module ();

reg     clk     ;
reg     reset   ;
reg     t       ;
wire    q       ;

initial
    begin
        clk = 1'b0;
        reset <= 1'b1;
        t <= 1'b0;
        #20
        reset <= 1'b0;
        #30
        t <= 1'b1;
    end
    
always #5   clk = ~clk;

tff tff_inst
(
    .clk     (clk  ),    
    .reset   (reset),
    .t       (t    ),
    .q       (q    )
);

endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值