Nowcoder Verilog
🕊️

Nowcoder Verilog

标签&分类
Verilog
发表时间
Jul 8, 2022 02:01 AM
描述
牛客网verilog练习题
属性
属性 1
属性 2
日期
Property
 

Verilog入门篇

基础语法

四选一多路器

notion image
notion image
`timescale 1ns/1ns
module mux4_1(
		input [1:0]d1,d2,d3,d0,
    input [1:0]sel,
    output[1:0]mux_out
);
    assign mux_out = sel[0]?(sel[1]?d0:d2):(sel[1]?d1:d3);
    
endmodule
notion image

异步复位的串联T触发器

notion image
`timescale 1ns/1ns
module Tff_2 (
input wire data, clk, rst,
output reg q  
);
//*************code***********//
reg q_0;
    always@(posedge clk or negedge rst) begin
        if(!rst)
            q_0<=1'b0;
        else
            if(data)
                q_0<=~q_0;
            else
                q_0<=q_0;
    end
    
    always@(posedge clk or negedge rst)begin
        if(!rst)
            q<=1'b0;
        else
            if(q_0)
                q<=~q;
            else
                q<=q;
    end

//*************code***********//
endmodule

奇偶校验

notion image
题意整理
1、简单理解奇偶校验
奇校验:原始码流+校验位 总共有奇数个1
偶校验:原始码流+校验位 总共有偶数个1
2、计算奇偶校验的方法  按位求异或得到奇校验结果,对其求反得到偶校验结果
3、连续进行异或 odd = ^bus  对bus进行异或位操作
题解主体
通过异或计算得到结果,对数据进行位运算。
根据激励方程和输出方程以及思路整理,关键电路如下:
notion image
notion image
将电路转换成Verilog代码描述如下
wire odd;
因此实现方式为如下的电路,综合得到:
notion image
notion image
 
`timescale 1ns/1ns
module odd_sel(
input [31:0] bus,
input sel,
output check
);
//*************code***********//
wire odd;
    assign odd = ^bus;
    assign check = sel?odd:~odd;

    

//*************code***********//
endmodule
notion image