首页 > 其他分享 >HDLbits第二天

HDLbits第二天

时间:2022-11-23 21:01:00浏览次数:37  
标签:wire HDLbits module 第二天 input output assign out

出于对FPGA学习巩固的目的,同时也希望能锻炼自己对于Verilog的题目分析,让自己对HDL代码的理解加深,所以想坚持写一下关于HDLbits网站刷题的系列,计划是工作日每日5题目+分析,周末每日十题+分析(如果题目繁琐会减轻数量,以能够分析准确并理解为主)


Vectors

Vectors

Vectors

wire [99:0] my_vector; // Declare a 100-element vector
assign out = my_vector[10]; // Part-select one bit out of the vector
image.png
Vector0

module top_module ( 
    input wire [2:0] vec,
    output wire [2:0] outv,
    output wire o2,
    output wire o1,
    output wire o0  ); // Module body starts after module declaration

    assign outv[2:0] = vec[2:0];
    assign o2 = vec[2];
    assign o1 = vec[1];
    assign o0 = vec[0];
    
endmodule

vector向量,定义的时候长度声明在前,使用的时候长度声明在后。

Vectors in more detail

`default_nettype none     // Disable implicit nets. Reduces some types of bugs.
module top_module( 
    input wire [15:0] in,
    output wire [7:0] out_hi,
    output wire [7:0] out_lo );

    assign out_hi = in[15:8];
    assign out_lo = in[7:0];
    
endmodule

向量是可以进行拆分赋值的,上面是一个小练习对高低位的分割。

Vector part select

Part-select can be used on both the left side and right side of an assignment

module top_module( 
    input [31:0] in,
    output [31:0] out );//

    assign out = {in[7:0], in[15:8], in[23:16], in[31:24]};

endmodule

在这个方法之中我使用了一个{}符号作为组合符号,也可以一个assign语句一个assign语句的进行逐段赋值。

Bitwise operators

image.png
Vectorgates
Even though you cannot assign to a wire more than once, you can use a part select on the left-hand-side of an assign. You don't need to assign to the entire vector all in one statement.

module top_module( 
    input [2:0] a,
    input [2:0] b,
    output [2:0] out_or_bitwise,
    output out_or_logical,
    output [5:0] out_not
);

    assign out_or_bitwise = a[2:0] | b[2:0];
    assign out_or_logical = a[2:0] || b[2:0];
    assign out_not[2:0] = ~a[2:0];
    assign out_not[5:3] = ~b[2:0];
    
endmodule

对于多位的逻辑运算来说,写明位宽可能更好。

Gates4

module top_module( 
    input [3:0] in,
    output out_and,
    output out_or,
    output out_xor
);

    assign out_and = & in;
    assign out_or = | in;
    assign out_xor = ^ in;
    
endmodule

Vector concatenation operator

image.png
Vector3

module top_module (
    input [4:0] a, b, c, d, e, f,
    output [7:0] w, x, y, z );//

    assign {w, x, y, z} = {a, b, c, d, e, f, 2'b11};

endmodule

Vector reversal1

assign out[7:0] = in[0:7]; does not work because Verilog does not allow vector bit ordering to be flipped.
The concatenation operator may save a bit of coding, allowing for 1 assign statement instead of 8.

module top_module( 
    input [7:0] in,
    output [7:0] out
);

    assign out = {in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]};
    
endmodule

Verilog本身是没有矢量反转的函数的,但是可以通过组合(串联运算符)的方式实现数据的反转。

Replication operator

module top_module (
    input [7:0] in,
    output [31:0] out );//

    assign out = {{24{in[7]}}, in};

endmodule

其实和Python一样也可以通过数字来表示复制。

More replication

image.png
Vector5

module top_module (
    input a, b, c, d, e,
    output [24:0] out );//

    assign out = ~{{5{a}}, {5{b}}, {5{c}}, {5{d}}, {5{e}}} ^ {5{a, b, c, d, e}};
    
endmodule

其实读懂题目的话就比较容易做了。

标签:wire,HDLbits,module,第二天,input,output,assign,out
From: https://www.cnblogs.com/havi/p/16919774.html

相关文章

  • HDLbits第一天
    出于对FPGA学习巩固的目的,同时也希望能锻炼自己对于Verilog的题目分析,让自己对HDL代码的理解加深,所以想坚持写一下关于HDLbits网站刷题的系列,计划是工作日每日5题目+分析,周......
  • HDLBits-Mux9to1v问题
    知识点always@(*)case(sel)2'b00:beginsout_t=p0;end2'b01:sout_t......
  • HDLBits-Mt2015_q4问题
    知识点无第一次回答moduletop_module(inputx,inputy,outputz);wireaz1,az2,bz1,bz2;AIA1(.x(x),.y(y),.z(az1));AIA2(.x(x),.y......
  • HDLBits-Bcdadd100问题
    HDLBits-Bcdadd100问题知识点reg[31:0]vect;vect[0+:8];#等同vect[7:0]vect[15-:8];#等同vect[15:8]reg[0:31]vect;vect[0+:8];......
  • 代码随想录算法训练营第二天| 977.有序数组的平方、209.长度最小的子数组、59.螺旋矩
    977.有序数组的平方题目建议:本题关键在于理解双指针思想题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/文章讲解:https://programmercarl.com/09......
  • 自学 TypeScript 第二天 编译选项
    前言:昨天我们学习了TS的数据类型,不知道大家回去以后练习没练习,如果你练习了一定会发现一个问题,我们的TS好像和JS不太一样JS写完之后直接就可以放到页面上,就可以用......
  • 复习java基础的第二天
    java流程控制1Scanner对象基本语法Scannerscanner=newScanner(System.in);next();一定要读取到有效字符后才可以结束输入。对输入有效字符之前遇到的空......
  • 隔离第二天
    昨晚睡的巨好,一觉睡到8点半,连早八的签到都忘了,好突然跟做梦一样,还是没有桌子没有凳子,听了一节的网课,全是都疼,接着躺床上。......
  • 学习JAVA的第二天-JAVA开发工具-ideaIC 免费+汉化Chinese ​(Simplified)​ Language
    学习JAVA的第二天开发工具安装开发工具我本来下载的 ideaIU收费的问了一下我同事,他用的社区版,使用体验ok,最重要的是免费。下载IntelliJIDEA:JetBrains功能强大、......
  • 零基础自学javase黑马课程第二天
    零基础自学javase黑马课程第二天✨欢迎关注......