首页 > 其他分享 >牛客网刷题三

牛客网刷题三

时间:2023-06-25 23:23:25浏览次数:40  
标签:wire start clk 牛客 网刷题 rst input reg

牛客网刷题21-24

这块主要是时序逻辑

第21题

根据状态转移表实现时序电路_牛客题霸_牛客网 (nowcoder.com)

image-20230625005017439

`timescale 1ns/1ns

module seq_circuit(
      input                A   ,
      input                clk ,
      input                rst_n,
 
      output   wire        Y   
);

parameter IDLE = 2'd0;
parameter S1 = 2'd1;
parameter S2 = 2'd2;
parameter S3 = 2'd3;

reg [1:0] state_c;
reg [1:0] state_n;
wire idl2s1_start;
wire idl2s3_start;
wire s12s2_start;
wire s12idl_start;
wire s22s3_start;
wire s22s1_start;
wire s32idl_start;
wire s32s2_start;           

//第一段:同步时序always模块,格式化描述次态寄存器迁移到现态寄存器(不需更改)
always@(posedge clk or negedge rst_n)begin
    if(!rst_n)begin
        state_c <= IDLE;
        state_n <= IDLE;
    end
    else begin
        state_c <= state_n;
    end
end
                    
//第二段:组合逻辑always模块,描述状态转移条件判断
always@(*)begin
    case(state_c)
        IDLE:begin
            if(idl2s1_start)begin
                state_n = S1;
            end
            else if(idl2s3_start)begin
                state_n = S3;
            end
            else begin
                state_n = state_c;
            end
        end
        S1:begin
            if(s12s2_start)begin
                state_n = S2;
            end
            else if(s12idl_start)begin
                state_n = IDLE;
            end
            else begin
                state_n = state_c;
            end
        end
        S2:begin
            if(s22s3_start)begin
                state_n = S3;
            end
            else if(s22s1_start)begin
                state_n = S1;
            end
            else begin
                state_n = state_c;
            end
        end
        S3:begin
            if(s32s2_start)begin
                state_n = S2;
            end
            else if(s32idl_start)begin
                state_n = IDLE;
            end
            else begin
                state_n = state_c;
            end
        end
        default:begin
            state_n = IDLE;
        end
    endcase
end
                    
//第三段:设计转移条件
assign idl2s1_start  = state_c==IDLE && A ==0;
assign idl2s3_start  = state_c==IDLE && A == 1;
assign s12s2_start = state_c==S1    && A ==0;
assign s12idl_start = state_c==S1    && A ==1;
assign s22s3_start  = state_c==S2    && A ==0;
assign s22s1_start  = state_c==S2    && A ==1;
assign s32idl_start  = state_c==S3    && A ==0;
assign s32s2_start  = state_c==S3    && A ==1;

assign Y = (state_c == 2'b11) ? 1 : 0;
endmodule

22题

根据状态转移图实现时序电路_牛客题霸_牛客网 (nowcoder.com)

image-20230625005104965

`timescale 1ns/1ns

module seq_circuit(
   input                C   ,
   input                clk ,
   input                rst_n,
 
   output   wire        Y   
);


parameter IDLE = 2'd0;
parameter S1 = 2'd1;
parameter S2 = 2'd2;
parameter S3 = 2'd3;
reg [1:0] state_c;
reg [1:0] state_n;
wire idl2s1_start;
wire s12s3_start;
wire s22idl_start;
wire s32s2_start;                    
//第一段:同步时序always模块,格式化描述次态寄存器迁移到现态寄存器(不需更改)
always@(posedge clk or negedge rst_n)begin
    if(!rst_n)begin
        state_c <= IDLE;
        state_n <= IDLE;
    end
    else begin
        state_c <= state_n;
    end
end
                    
//第二段:组合逻辑always模块,描述状态转移条件判断
always@(*)begin
    case(state_c)
        IDLE:begin
            if(idl2s1_start)begin
                state_n = S1;
            end
            else begin
                state_n = state_c;
            end
        end
        S1:begin
            if(s12s3_start)begin
                state_n = S3;
            end
            else begin
                state_n = state_c;
            end
        end
        S2:begin
            if(s22idl_start)begin
                state_n = IDLE;
            end
            else begin
                state_n = state_c;
            end
        end
        S3:begin
            if(s32s2_start)begin
                state_n = S2;
            end
            else begin
                state_n = state_c;
            end
        end
        default:begin
            state_n = IDLE;
        end
    endcase
end
                    
//第三段:设计转移条件
assign idl2s1_start  = state_c==IDLE && C==1 ;
assign s12s3_start = state_c==S1    && C ==0;
assign s22idl_start  = state_c==S2    &&  C == 0;
assign s32s2_start  = state_c==S3    &&  C == 1;

//第四段:同步时序always模块,格式化描述寄存器输出(可有多个输出)
assign Y = (state_c==S3) || (state_c==S2 && C == 1);
// always  @(posedge clk or negedge rst_n)begin
//     if(rst_n==1'b0)begin
//         Y <= 0;
//     end
//     else if((state_c==S2) || (state_c==S3 && C == 1)) begin
//         Y <= 1;
//     end
//     else begin
//         Y <= 0;
//     end
// end
endmodule

23题

ROM的简单实现_牛客题霸_牛客网 (nowcoder.com)

image-20230625011626705

`timescale 1ns/1ns
module rom(
	input clk,
	input rst_n,
	input [7:0]addr,
	
	output [3:0]data
);

reg [3:0] rom [7:0];

//4'd
always  @(posedge clk or negedge rst_n)begin
    if(rst_n==1'b0)begin
        rom[0] <= 0;
        rom[1] <= 2;
        rom[2] <= 4;
        rom[3] <= 6;
        rom[4] <= 8;
        rom[5] <= 10;
        rom[6] <= 12;
        rom[7] <= 14;
    end
end


assign data = rom[addr];

endmodule

24题

边沿检测_牛客题霸_牛客网 (nowcoder.com)

image-20230625011650326

`timescale 1ns/1ns
module edge_detect(
	input clk,
	input rst_n,
	input a,
	
	output reg rise,
	output reg down
);
	reg a_tmp;

    always  @(posedge clk or negedge rst_n)begin
        if(rst_n==1'b0)begin
            a_tmp <= 0;
        end
        else begin
            a_tmp <= a;
        end
    end

    //rise
    always  @(posedge clk or negedge rst_n)begin
        if(rst_n==1'b0)begin
            rise <= 0;
        end
        else if (a == 1 && a_tmp == 0) begin
            rise <= 1;
        end
        else begin
            rise <= 0;
        end
    end


    //down
    always  @(posedge clk or negedge rst_n)begin
        if(rst_n==1'b0)begin
            down <= 0;
        end
        else if (a == 0 && a_tmp == 1) begin
            down <= 1;
        end
        else begin
            down <= 0;
        end
    end
endmodule

标签:wire,start,clk,牛客,网刷题,rst,input,reg
From: https://www.cnblogs.com/doincli/p/17504240.html

相关文章

  • 牛客网刷题4
    25-2825题输入序列连续的序列检测_牛客题霸_牛客网(nowcoder.com)`timescale1ns/1nsmodulesequence_detect( inputclk, inputrst_n, inputa, outputregmatch );reg[8:0]tmp;//存储always@(posedgeclkornegedgerst_n)beginif(!rst_n)begin......
  • 牛客网刷题二
    牛客网FPGA题库刷题之快速入门题库(一)9~13题14-20没啥用就是看图写,不需要做了第九题题目链接使用子模块实现三输入数的大小比较代码`timescale1ns/1nsmodulemain_mod(inputclk,inputrst_n,input[7:0]a,input[7:0]b,input[7:0]c,output[7:0]d);......
  • 牛客题解-mixup2混乱的奶牛(状压dp)
    题解-mixup2混乱的奶牛[原题连接](1026-mixup2混乱的奶牛_2021秋季算法入门班第八章习题:动态规划2(nowcoder.com))题目描述混乱的奶牛[DonPiele,2007]FarmerJohn的N(4<=N<=16)头奶牛中的每一头都有一个唯一的编号S_i(1<=S_i<=25,000).奶牛为她们的编号感到骄傲......
  • 牛客竞赛刷题模板
    牛客竞赛自用,便于复制for(letT=parseInt(readline());T>0;T--){const[n,m]=readline().split('',2).map(v=>parseInt(v));constnums=readline().split('',n).map(v=>parseInt(v));letsum=0;constsub......
  • transform (牛客多校) (双指针+二分+ 中位数妙用+前缀和相减维护)
    题目大意:n个商店在一条直线上, 有一个xi然后有ai个商品你可以把商店的物品移动到另一个商店,代价为:abs(xi-xj)在代价不超过T的情况下你可以选择一个商店来让其他商店的物品都移到这个商店,问最多移动多少个物品  思路:双指针维护一个最大的区间,因......
  • farm (牛客多校) (二维树状+数学式子优化+rand()去除特殊情况)
    题目大意:给出一个n*m的田地矩阵,每个格子上种着一种植物。给格子施肥t次,每一次给出五个数字,x1,y1,x2,y2,k,要施肥的区域坐标和要施的肥料种类。如果植物和施肥种类不匹配,植物会死亡。问最终会死多少个植物。 思路:判断一个植物死不死, 判断植物种类*施肥次数==施肥种类总和某......
  • car (牛客多校) (情景找规律,抠细节)
    题目大意:给一个正方形棋盘,你现在可以在棋盘的边缘防止车,然后车只能向正对的方向走,(角落可以往2边走)2个车相遇会G给m个破环的方块,车经过就G问最多可以放多少个车] 思路:注意奇偶分规律,偶数2*n,奇数2*n-1注意放置破环的方块,在奇数最中间的时候,......
  • Longest Path (牛客多校) (换根DP+斜率优化)
    换根dp:第一次dfs处理儿子点的权值第二次dfs处理父亲点,和兄弟节点的权值处理兄弟节点的时候,利用父亲节点统一处理,利用stl存储斜率优化:为什么会用到斜率优化:在遇到转移式子是fixfj的时候,不是分开的,(分开的,直接用单调队列处理)(通常会遇到平方式子)把......
  • 牛客网刷题一
    牛客网FPGA题库刷题之快速入门题库(一)1~8题第一题题目链接:四选一多路器代码:`timescale1ns/1nsmodulemux4_1(input[1:0]d1,d2,d3,d0,input[1:0]sel,output[1:0]mux_out);//*************code***********//reg[1:0]mux_out_tmp;always@(*)begin......
  • 牛客小白月赛73
    A最小的数字#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongint32_tmain(){ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);intn;cin>>n;cout<<((n+2ll)/3ll)*3ll<<"\n";......