草庐IT

【牛客】1 基础语法

magnolia666 2023-03-28 原文

VL1 四选一多路器

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

VL2 异步复位的串联T触发器

题目已经提示了是两个串联的T触发器,只要记得T触发器在输入高电平时,输出翻转即可。

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

VL3 奇偶校验

按位异或即可。

`timescale 1ns/1ns
module odd_sel(
input [31:0] bus,
input sel,
output check
);
//*************code***********//
assign check = sel?(^bus):(~^bus);

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

VL4 移位运算与乘法

写完才看到要求用移位运算,不过也简单,把乘法分解成移位相加即可。

注意把输入的d先保存起来。

`timescale 1ns/1ns
module multi_sel(
input [7:0]d ,
input clk,
input rst,
output reg input_grant,
output reg [10:0]out
);
//*************code***********//
reg [1:0]state;
reg [7:0]d_r;
always@(posedge clk or negedge rst)
begin
    if(~rst)begin
        input_grant <= 1'b0;
        out <= 'd0;
        state <= 'd0;
        d_r <= 'd0;
    end
    else begin
        if(state == 0)begin
            input_grant <= 1'b1;
            out <= d;
            d_r <= d;
            state <= 1;
        end else if(state == 1)begin
            input_grant <= 1'b0;
            out <= 3 * d_r;
            state <= 2;
        end else if(state == 2)begin
            out <= 7 * d_r;
            state <= 3;            
        end else if(state == 3)begin
            out <= 8 * d_r;
            state <= 0;            
        end
    end
end

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

VL5 位拆分与运算

在sel==0时把d保存起来。

`timescale 1ns/1ns

module data_cal(
input clk,
input rst,
input [15:0]d,
input [1:0]sel,

output reg [4:0]out,
output reg validout
);
//*************code***********//
reg [15:0]d_r;
always@(*)
begin
    case(sel)
    0:begin
        validout=0;
        out=0;
    end
    1:begin
        validout=1;
        out=d_r[3:0]+d_r[7:4];
    end
    2:begin
        validout=1;
        out=d_r[3:0]+d_r[11:8];
    end
    3:begin
        validout=1;
        out=d_r[3:0]+d_r[15:12];
    end
    endcase
end

always@(posedge clk or negedge rst)
begin
    if(~rst)
        d_r <= 'd0;
    else begin if(sel == 0)
        d_r <= d;
    end
end

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

VL6 多功能数据处理器

`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)
begin
    if(~rst_n)
    c=0;
    else begin
    case(select)
        0:c=a;
        1:c=b;
        2:c=a+b;
        3:c=a-b;
    endcase
    end
end
endmodule

VL7 求两个数的差值

`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)
begin
    if(~rst_n)
        c <= 'd0;
    else begin
        if(a>b)
            c<=a-b;
        else
            c<=b-a;
    end
end
endmodule

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

这道题要求使用generate for语句,这个和for循环并不相同,首先generate for必须要使用genvar定义索引变量,其次generate for可以用来将一个模块例化多次,而for循环通常只是用于赋值。

这道题其实用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 
    assign data_out[i]=data_in[7-i]; 
end
endgenerate
endmodule

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]temp1;
wire [7:0]temp2;
child_mod u0(
    .clk(clk),
    .rst_n(rst_n),
    .a(a),
    .b(b),
    .c(temp1)
);
child_mod u1(
    .clk(clk),
    .rst_n(rst_n),
    .a(a),
    .b(c),
    .c(temp2)
);
child_mod u2(
    .clk(clk),
    .rst_n(rst_n),
    .a(temp1),
    .b(temp2),
    .c(d)
);
endmodule

module child_mod(
    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)
        c <= 'd0;
    else begin
        if(a<b)
            c<=a;
        else
            c<=b;
    end
end
endmodule

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

这题要求用function,注意一下function怎么写就行了。

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

 

有关【牛客】1 基础语法的更多相关文章

  1. ruby - 树顶语法无限循环 - 2

    我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He

  2. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  3. ruby - 覆盖相似的方法,更短的语法 - 2

    在Ruby类中,我重写了三个方法,并且在每个方法中,我基本上做同样的事情:classExampleClassdefconfirmation_required?is_allowed&&superenddefpostpone_email_change?is_allowed&&superenddefreconfirmation_required?is_allowed&&superendend有更简洁的语法吗?如何缩短代码? 最佳答案 如何使用别名?classExampleClassdefconfirmation_required?is_a

  4. ruby 语法糖 : dealing with nils - 2

    可能已经问过了,但我找不到它。这里有2个常见的情况(对我来说,在编程Rails时......)用ruby​​编写是令人沮丧的:"astring".match(/abc(.+)abc/)[1]在这种情况下,我得到一个错误,因为字符串不匹配,因此在nil上调用[]运算符。我想找到的是比以下内容更好的替代方法:temp="astring".match(/abc(.+)abc/);temp.nil??nil:temp[1]简而言之,如果不匹配,则简单地返回nil而不会出错第二种情况是这样的:var=something.very.long.and.tedious.to.writevar=some

  5. ruby - Ruby 语法糖有 "rules"吗? - 2

    我正在学习Ruby的基础知识(刚刚开始),我遇到了Hash.[]method.它被引入a=["foo",1,"bar",2]=>["foo",1,"bar",2]Hash[*a]=>{"foo"=>1,"bar"=>2}稍加思索,我发现Hash[*a]等同于Hash.[](*a)或Hash.[]*一个。我的问题是为什么会这样。是什么让您将*a放在方括号内,是否有某种规则可以在何时何地使用“it”?编辑:我的措辞似乎造成了一些困惑。我不是在问数组扩展。我明白了。我的问题基本上是:如果[]是方法名称,为什么可以将参数放在括号内?这看起来几乎——但不完全是——就像说如果你有一个方法Foo.d

  6. postman接口测试工具-基础使用教程 - 2

    1.postman介绍Postman一款非常流行的API调试工具。其实,开发人员用的更多。因为测试人员做接口测试会有更多选择,例如Jmeter、soapUI等。不过,对于开发过程中去调试接口,Postman确实足够的简单方便,而且功能强大。2.下载安装官网地址:https://www.postman.com/下载完成后双击安装吧,安装过程极其简单,无需任何操作3.使用教程这里以百度为例,工具使用简单,填写URL地址即可发送请求,在下方查看响应结果和响应状态码常用方法都有支持请求方法:getpostputdeleteGet、Post、Put与Delete的作用get:请求方法一般是用于数据查询,

  7. 软件测试基础 - 2

    Ⅰ软件测试基础一、软件测试基础理论1、软件测试的必要性所有的产品或者服务上线都需要测试2、测试的发展过程3、什么是软件测试找bug,发现缺陷4、测试的定义使用人工或自动的手段来运行或者测试某个系统的过程。目的在于检测它是否满足规定的需求。弄清预期结果和实际结果的差别。5、测试的目的以最小的人力、物力和时间找出软件中潜在的错误和缺陷6、测试的原则28原则:20%的主要功能要重点测(eg:支付宝的支付功能,其他功能都是次要的)80%的错误存在于20%的代码中7、测试标准8、测试的基本要求功能测试性能测试安全性测试兼容性测试易用性测试外观界面测试可靠性测试二、质量模型衡量一个优秀软件的维度①功能性功

  8. ES基础入门 - 2

    ES一、简介1、ElasticStackES技术栈:ElasticSearch:存数据+搜索;QL;Kibana:Web可视化平台,分析。LogStash:日志收集,Log4j:产生日志;log.info(xxx)。。。。使用场景:metrics:指标监控…2、基本概念Index(索引)动词:保存(插入)名词:类似MySQL数据库,给数据Type(类型)已废弃,以前类似MySQL的表现在用索引对数据分类Document(文档)真正要保存的一个JSON数据{name:"tcx"}二、入门实战{"name":"DESKTOP-1TSVGKG","cluster_name":"elasticsear

  9. ruby - 如何让Ruby捕获线程中的语法错误 - 2

    我正在尝试使用ruby​​编写一个双线程客户端,一个线程从套接字读取数据并将其打印出来,另一个线程读取本地数据并将其发送到远程服务器。我发现的问题是Ruby似乎无法捕获线程内的错误,这是一个示例:#!/usr/bin/rubyThread.new{loop{$stdout.puts"hi"abc.putsefsleep1}}loop{sleep1}显然,如果我在线程外键入abc.putsef,代码将永远不会运行,因为Ruby将报告“undefinedvariableabc”。但是,如果它在一个线程内,则没有错误报告。我的问题是,如何让Ruby捕获这样的错误?或者至少,报告线程中的错误?

  10. 牛客网专项练习30天Pytnon篇第02天 - 2

    1.在Python3中,下列关于数学运算结果正确的是:(B)a=10b=3print(a//b)print(a%b)print(a/b)A.3,3,3.3333...B.3,1,3.3333...C.3.3333...,3.3333...,3D.3.3333...,1,3.3333...解析:    在Python中,//表示地板除(向下取整),%表示取余,/表示除(Python2向下取整返回3)2.如下程序Python2会打印多少个数:(D)k=1000whilek>1:    print(k)k=k/2A.1000 B.10C.11D.9解析:    按照题意每次循环K/2,直到K值小于等

随机推荐