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

牛客网刷题一

时间:2023-06-06 22:22:30浏览次数:51  
标签:tmp 题目 1ns 牛客 网刷题 rst input reg

牛客网FPGA题库刷题之快速入门题库(一)1~8题

第一题

题目链接:

四选一多路器

image-20230606220715206

代码:

`timescale 1ns/1ns
module mux4_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
        case(sel)
            2'b00:mux_out_tmp = d3;
            2'b01:mux_out_tmp = d2;
            2'b10:mux_out_tmp = d1;
            2'b11:mux_out_tmp = d0;
        endcase
    end
            assign mux_out = mux_out_tmp;
 
//*************code***********//
endmodule

题目解析:

用case语句实现简单的四路选择,always语句里面需要用reg,所以新建一个reg的temp变量即可

第二题

题目链接

异步复位的串联T触发器

image-20230606220738974

代码

`timescale 1ns/1ns
module Tff_2 (
input wire data, clk, rst,
output reg q
);
//*************code***********//
reg  tmp;
    always@(posedge clk or negedge rst)begin
        if(rst == 1'b0)begin
         tmp <= 0 ;
        end
        else begin
         tmp <= tmp ^ data;
        end
    end
 
     always@(posedge clk or negedge rst)begin
        if(rst == 1'b0)begin
         q <= 0 ;
        end
        else begin
         q <=tmp ^q;
        end
    end
//*************code***********//
endmodule

题目解析

首先了解T触发器的特性方程

Qn+1 = T Qn ' +T ' Qn = T⊕Qn (其中Qn为现态,Qn+1为次态) 也就是异或

然后题目是两个T触发器,所以第一个的输出是第二个的输入

网友的解析:

1、T触发器是进入的值为1的时候,寄存的值发生翻转
2、注意异步复位
3、需要注意寄存器翻转的逻辑,第二寄存器是否翻转取决于第一个寄存器是否为1,前者输出情况有三种:在data输入控制为1下从0到1到0不断翻转,data为0锁在1,data为0锁在0
ps: 考虑rst为1且完成了初始化,rst 低电平复位高电平有效

第三题:

题目链接

奇偶校验

image-20230606220753500

代码

`timescale 1ns/1ns
module odd_sel(
input [31:0] bus,
input sel,
output check
);
//*************code***********//
    reg tmp;
    always@(*)begin
        case(sel)
            1'b1:tmp = ^bus;
            1'b0:tmp = ~(^bus);
        endcase
    end
    assign check =tmp;
 
//*************code***********//
endmodule

题目解析

需要了解什么是奇偶校验

奇校验:对输入数据添加1位0或者1,使得添加后的数包含奇数个1;

比如100,有奇数个1,那么奇校验结果就是0,这样补完0以后还是奇数个1;

偶校验:对输入数据添加1位0或者1,使得添加后的数包含偶数个1;

对bus进行异或,如果有奇数个1,输出就是1。如果有偶数个1,输出就是0

这个题目,应该是出题人搞反了,按照出题的意思,应该不能叫奇偶校验,应该是叫奇偶检测:

奇检测:输入的数据里有奇数个1就输出1;

偶检测:输入的数据里有偶数个1就输出1;

第四题

题目链接

移位运算与乘法

image-20230606220808544

代码

`timescale 1ns/1ns
module multi_sel(
input [7:0]d ,
input clk,
input rst,
output reg input_grant,
output reg [10:0]out
);
//*************code***********//
 
//wire Declaration
wire add_cnt;
wire end_cnt;
 
//reg Declaration
reg [1:0] cnt;
reg [7:0] tmp;
 
 
//计数器
always @(posedge clk or negedge rst)begin
    if(!rst)begin
        cnt <= 0;
    end
    else if(add_cnt)begin
        if(end_cnt)
            cnt <= 0;
        else
            cnt <= cnt + 1;
    end
end
 
assign add_cnt = 1 ;     
assign end_cnt = add_cnt && cnt==3 ; 
 
//输出信号
always  @(posedge clk or negedge rst)begin
    if(rst==1'b0)begin
        input_grant <=0;
    end
    else if(cnt==0)begin
        input_grant <=1;
    end
    else begin
        input_grant <=0;
    end
end
 
//数据缓存
always  @(posedge clk or negedge rst)begin
    if(rst==1'b0)begin
        tmp <= 0;
    end
    else if(cnt==0) begin
         tmp <= d;
    end
end
 
//信号输出
always  @(posedge clk or negedge rst)begin
    if(rst==1'b0)begin
        out <= 0;
    end
    else begin
        case(cnt)
            2'b00:out <= d;
            2'b01:out <= (tmp<<1)+tmp;
            2'b10:out <= (tmp<<3)-tmp;
            2'b11:out <= (tmp<<3);
            default:out <=0;
        endcase
    end
end
 
//*************code***********//
endmodule
 
 

题目解析

因为每个时钟周期分别输出该数乘1/3/7/8,所以需要一个计数器计算4个周期来输出数据。输出信号在计数器为0时候有效。观察波形示意图发现输出信号并不是立即跳变而是4个为一个周期来进行跳变,所以需要缓存数据。输出信号的乘法利用移位来做,可以有效增加时序性能(直接乘综合出来的加法器含有过多组合逻辑,时序性能很差)

第五题

题目链接

位拆分与运算

image-20230606220822471

代码

`timescale 1ns/1ns
 
module data_cal(
input clk,
input rst,
input [15:0]d,
input [1:0]sel,
 
output [4:0]out,
output validout
);
//*************code***********//
 
//wire declaration
 
//reg declaration
reg [15:0] tmp;
reg valid;
reg [4:0] signal_out;
 
//有效信号
always  @(posedge clk or negedge rst)begin
    if(rst==1'b0)begin
        valid <= 0;
    end
    else if(0==sel) begin
        valid <= 0;
    end
    else begin
        valid <= 1;
    end
end
 
assign validout = valid;
 
//数据缓存
always  @(posedge clk or negedge rst)begin
    if(rst==1'b0)begin
         tmp <= 0;
    end
    else if(0==sel) begin
         tmp <= d;
    end
end
 
//信号输出
always  @(posedge clk or negedge rst)begin
    if(rst==1'b0)begin
       signal_out <= 0;
    end
    else begin
        case(sel)
            2'd0: signal_out <= 0;
            2'd1: signal_out <= tmp[3:0] + tmp[7:4];
            2'd2: signal_out <= tmp[3:0] + tmp[11:8];
            2'd3: signal_out <= tmp[3:0] + tmp[15:12];
        endcase
    end
end
 
assign out =signal_out;
 
//*************code***********//
endmodule
 
 

题目解析

有效信号是输出时候,也就是sel不为1的时候是高电平。sel为0的时候是数据输入,所以缓存进来。信号输出就是根据题目的意思来就ok了。注意给的都是输入输出wire,不能直接在always块里面。需要声明寄存器。

第六题

题目链接

多功能数据处理器

image-20230606220842210

代码

`timescale 1ns/1ns
module data_select(
    input clk,
    input rst_n,
    input signed[7:0]a,
    input signed[7:0]b,
    input [1:0]select,
    output reg signed [8:0]c
);
 
always @(posedge clk or negedge rst_n)
    if(!rst_n)
        c <= 9'd0;
    else case(select)
    2'b00:    c <= a;
    2'b01:    c <= b;
    2'b10:    c <= a+b;
    2'b11:    c <= a-b;
    default: c <= 9'd0;
    endcase
 
endmodule

题目解析

没啥解析的地方,多路选择器,直接根据题意写就好了。有兴趣可以拓展一下有符号数和无符号数的区别

第七题:

题目链接

求两个数的差值

image-20230606220900959

代码

`timescale 1ns/1ns
module data_minus(
    input clk,
    input rst_n,
    input [7:0]a,
    input [7:0]b,
 
    output  reg [8:0]c
);
 
always @(posedge clk or negedge rst_n)
    if(!rst_n)
        c <= 0;
    else if(a > b)
        c <= a-b;
    else
        c <= b-a;
endmodule

题目解析

两个判断语句输出就好了

第八题

题目链接

使用generate…for语句简化代码

image-20230606220915793

代码

`timescale 1ns/1ns
module gen_for_module(
    input [7:0] data_in,
    output [7:0] data_out
);
 
    generate
        genvar i;
        for(i = 0; i <8; i=i+1)
            begin:assign_test//名字随便起,但是必须有
                assign data_out[i] = data_in[7 - i];
            end
    endgenerate
 
endmodule

题目解析

注意考察generate语句的使用,需要注意两点,第一个需要genval产生数i,第二个就是for循环的begin必须有一个名字。

参考资料

探索者的刷题视频就很好

FPGA探索者FPGA刷题视频

标签:tmp,题目,1ns,牛客,网刷题,rst,input,reg
From: https://www.cnblogs.com/doincli/p/17461897.html

相关文章

  • 牛客小白月赛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";......
  • 牛客想开了大赛2 题解
    题目链接:https://ac.nowcoder.com/acm/contest/907#question A.【六】平面公式:(n*n+n)/2+1,n为直线数目B.【一】n的约数枚举质因子和每个质因子的个数,显然个数肯定从多到少。#include<bits/stdc++.h>typedeflonglongll;usingnamespacestd;constintmx=1e4+10;in......
  • 牛客小白月赛73
    A.最小的数字题目:分析:简单枚举一下,找到第一个大于等于n的且是3的倍数的数代码:#include<bits/stdc++.h>usingnamespacestd;intmain(){intn;cin>>n;boolloop=true;if(n%3==0)loop=false;while(lo......
  • LRU牛客比较简单的实现
    https://www.nowcoder.com/practice/5dfded165916435d9defb053c63f1e84?tpId=295&tqId=2427094&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2FojpublicclassSolution{Map<Integer,Integer>map;intcapacit......
  • 牛客练习赛108
    风间分析:暴力实现:inta[N],b[N];voidsolve(){res=0;scanf("%lld",&n);for(inti=1;i<=n;i++)scanf("%lld",&a[i]);for(inti=1;i<=n;i++)scanf("%lld",&b[i]);......
  • maximum clique 1 (牛客多校) (转化为图论, 二分图的最大独立集)
    题面大意:给出N个数, 找出最大的子集size使得子集中,任意2个数的二进制下,每一位,至少有2位不同 思路:N只有5000,可以直接暴力建边,转化位图论2种建边方式:一种是能在一个集合的2个数,建一条边,(没有办法去处理这个问题)一种是不能在一个集合的就建一条......
  • subsequence1 (牛客多校) (2个串比大小, DP, 组合数)
    题面大意:给定2个字符串,问有多少个子字符串S,是大于t的 思路数据范围很小,因此考虑n^2做法分2步,位数s>位数t的时候然后位数相等的时候利用DP,处理,分别就是枚举前k个数和s相同,然后k+1个数比t大就可以. 具体思路自己想想,和那个比较像   cons......
  • 【牛客小白72】E 二分答案
    https://ac.nowcoder.com/acm/contest/56825/E题意给你\(10^{10}\)个数(数组an个数,数组bm个数,数是a*b的集合),有\(Q\)(Q为常数)个询问,每次问你第\(x\)小的数是多少思路最暴力的思路肯定是把所有数放在一起,然后排序。易知时间复杂度为\(nlogn(n=1e10)\),会超时。继续思......
  • Interval(20年牛客多校5)
    传送门题意有一个数列\(A(0\leqslantA_i\lt2^{30})\)长度为\(N(1\leqslantN\leqslant10^5)\),\(F(l,r):=A_l\&A_{l+1}\&\cdots\&A_r\),\(S(l,r):=\left\{F(a,b)|\min(l,r)\leqslanta\leqslantb\leqslant\max(l,r)\right\}\),有\(Q(1\le......
  • 牛客 55994 2023牛客五一集训派对day3 D Points Construction Problem
    D-PointsConstructionProblem_2023牛客五一集训派对day3(nowcoder.com)将图上恰好\(n\)个点染成黑色,使得图上相邻的黑白点对数量恰好为\(m\)考虑\(n\)个黑点如果不相邻,则两个点的贡献互不影响考虑相邻的情况,我们把相邻的点连边,则贡献为每一个连通块的贡献的和,我们用......