verilog牛客网刷题代码汇总(上)(2)

简介: 1. Verilog快速入门1. 基础语法VL1 四选一多路器VL2 异步复位的串联T触发器LV3 奇偶校验VL4 移位运算与乘法LV5 位拆分与运算VL6 多功能数据处理器VL7 求两个数的差值VL8 使用generate…for语句简化代码VL9 使用子模块实现三输入数的大小比较VL10 使用函数实现数据大小端转换02 组合逻辑VL11 4位数值比较器电路VL12 4bit超前进位加法器电路VL13 优先编码器电路①VL14 用优先编码器①实现键盘编码电路VL15 优先编码器ⅠVL16 使用8线-3线优先编码器Ⅰ实现16线-4线优先编码器

VL8 使用generate…for语句简化代码

VL8 使用generate…for语句简化代码

`timescale 1ns/1ns
module gen_for_module(
    input              [   7:0]         data_in                    ,
    output             [   7:0]         data_out                    
);
    genvar i;
    generate
        for(i = 0; i < 8; i = i + 1)
        begin : bit_reverse
            assign data_out[i] = data_in[7 - i];
        end
    endgenerate
endmodule

VL9 使用子模块实现三输入数的大小比较

VL9 使用子模块实现三输入数的大小比较

`timescale 1ns/1ns
module main_mod(
    input                               clk                        ,
    input                               rst_n                      ,
    input              [   7:0]         a                          ,
    input              [   7:0]         b                          ,
    input              [   7:0]         c                          ,
    output             [   7:0]         d                           
);
wire                   [   7:0]         m,n                        ;
sub_module sub_ab(
    .clk                               (clk                       ),
    .rst_n                             (rst_n                     ),
    .a                                 (a                         ),
    .b                                 (b                         ),
    .c                                 (m                         ) 
);
sub_module sub_mc(
    .clk                               (clk                       ),
    .rst_n                             (rst_n                     ),
    .a                                 (b                        ),
    .b                                 (c                         ),
    .c                                 (n                         ) 
);
sub_module sub_mn(
    .clk                               (clk                       ),
    .rst_n                             (rst_n                     ),
    .a                                 (m                        ),
    .b                                 (n                         ),
    .c                                 (d                         ) 
);
endmodule
module sub_module (
    input                               clk                        ,
    input                               rst_n                      ,
    input              [   7:0]         a                          ,
    input              [   7:0]         b                          ,
    output reg         [   7:0]         c                           
);
always @(posedge clk or negedge rst_n) begin
    if(!rst_n)begin
         c <= 8'd0;
    end else begin
        if(a <= b) begin
            c <= a;
        end
        else begin
            c <= b;
        end
    end
end
endmodule

VL10 使用函数实现数据大小端转换

VL10 使用函数实现数据大小端转换

`timescale 1ns/1ns
module function_mod(
    input                               clk                        ,
    input                               rst_n                      ,
    input              [   3:0]         a                          ,
    input              [   3:0]         b                          ,
    output             [   3:0]         c                          ,
    output             [   3:0]         d                           
);
function [3:0] reverse;
    input              [   3:0]         data_in                    ;
    integer i;
    begin
        for (i = 0;i<4 ;i = i + 1 ) begin
            reverse[i] = data_in[3-i];
        end
    end
endfunction
assign c = reverse(a);
assign d = reverse(b);
endmodule

02 组合逻辑

VL11 4位数值比较器电路

VL11 4位数值比较器电路

`timescale 1ns/1ns
module comparator_4(
  input   [3:0]       A     ,
  input    [3:0]    B     ,
  output   wire   Y2    , //A>B
  output   wire        Y1    , //A=B
    output   wire        Y0      //A<B
);
reg Y2_tmp,Y1_tmp,Y0_tmp;
always @(A or B) begin
    if(A > B) begin
        Y2_tmp = 1;
        Y1_tmp = 0;
        Y0_tmp = 0;
    end
    else if (A < B) begin
        Y2_tmp = 0;
        Y1_tmp = 0;
        Y0_tmp = 1;       
    end
    else if(A == B) begin
        Y2_tmp = 0;
        Y1_tmp = 1;
        Y0_tmp = 0;       
    end
end
assign Y2 = Y2_tmp;
assign Y1 = Y1_tmp;
assign Y0 = Y0_tmp;
endmodule

VL12 4bit超前进位加法器电路

VL12 4bit超前进位加法器电路

`timescale 1ns/1ns
module lca_4(
  input   [3:0]       A_in  ,
  input     [3:0]   B_in  ,
    input                   C_1   ,
  output   wire     CO    ,
  output   wire [3:0]     S
);
wire [3:0] G_i;
wire [3:0] P_i;
wire [3:0] C_i; 
assign G_i = A_in & B_in;
assign P_i = A_in ^ B_in;
assign C_i[0] = G_i[0] | P_i[0]&C_1;
assign C_i[1] = G_i[1] | P_i[1]&G_i[0] | P_i[1]&P_i[0]&C_1;
assign C_i[2] = G_i[2] | P_i[2]&G_i[1] | P_i[2]&P_i[1]&G_i[0] | P_i[2]&P_i[1]&P_i[0]&C_1;
assign C_i[3] = G_i[3] | P_i[3]&G_i[2] | P_i[3]&P_i[2]&G_i[1] | P_i[3]&P_i[2]&P_i[1]&(G_i[0] | P_i[0]&C_1);
assign S[0] = P_i[0] ^ C_1;
assign S[1] = P_i[1] ^ C_i[0];
assign S[2] = P_i[2] ^ C_i[1];
assign S[3] = P_i[3] ^ C_i[2];
assign CO = C_i[3];
endmodule
相关文章
|
2月前
|
存储 编译器 C语言
【C语言刷题每日一题#牛客网BC107】矩阵转置
【C语言刷题每日一题#牛客网BC107】矩阵转置
|
3月前
|
存储 算法 C语言
C语言初学者超详细习题库1(含题目以及代码讲解)
C语言初学者超详细习题库1(含题目以及代码讲解)
64 1
|
8月前
|
算法 C语言
C语言 每日一题 牛客网习题 10.20 day2
C语言 每日一题 牛客网习题 10.20 day2
40 0
|
9月前
|
人工智能 算法 机器人
迷宫问题(C语言实现)(牛客网百度笔试真题)
迷宫问题(C语言实现)(牛客网百度笔试真题)
82 0
|
芯片
牛客网Verilog刷题(2)
牛客网Verilog刷题(2)
90 0
|
C语言
牛客网Verilog刷题(1)
牛客网Verilog刷题(1)
68 0
|
存储 算法 C语言
[数据结构与算法(严蔚敏 C语言第二版)]第1章 绪论(课后习题+答案解析)
[数据结构与算法(严蔚敏 C语言第二版)]第1章 绪论(课后习题+答案解析)
|
前端开发
牛客网DAY2(编程题)
牛客网DAY2(编程题)
53 0
|
算法 C语言
想说说关于在刷题网站(牛客 、C语言网、力扣)上测试样例过了但是OJ判错这档子事
想说说关于在刷题网站(牛客 、C语言网、力扣)上测试样例过了但是OJ判错这档子事
|
存储 算法 异构计算
牛客网verilog刷题知识点盘点(75道题的版本)
还有几个坑没填,知识点整理总结自互联网,本人csdn博客搬运
544 0