HDLBits 在提供 Verilog 基础语法教程的同时,还能够在线仿真 Verilog 模块。
以下是各单元解法答案。希望可以帮助您了解 Verilog 的工作原理。
前言
HDLBits 在提供 Verilog 基础语法教程的同时,还能够在线仿真 Verilog 模块。
一、入门 Getting Started
⚠️注意:顶层的模块名称和端口名称 top_module 不能更改,否则会出现仿真错误。
1.Step one
module top_module ( output one );
// Insert your code here
assign one = 1'b1;
endmodule
2.Zero
module top_module ( zero );
output zero;
endmodule
二、Verilog 语言 Verilog Language
第一部分:Basics
1. Wire
module top_module( input in, output out );
assign out = in;
endmodule
2. Wire 4
module top_module(
input a,b,c,
output w,x,y,z );
assign w = a;
assign x = b;
assign y = b;
assign z = c;
endmodule
//或者使用拼接运算符{}
module top_module(
input a,b,c,
output w,x,y,z );
assign {
w,x,y,z} = {
a,b,b,c};
endmodule
3. Notgate
module top_module( input in, output out );
assign out = ~ in;
endmodule
4. Andgate
module top_module(
input a,
input b,
output out );
assign out = a & b;
endmodule
5. Norgate
module top_module(
input a,
input b,
output out );
assign out = ~ (a | b);
endmodule
6. Xnorgate
module top_module(
input a,
input b,
output out );
assign out = ~ (a ^ b);
endmodule
7. Wire decl
当没有使用`default_nettype none 时,一个变量没有被定义就使用,系统会默认该变量为wire型,结果有warning,但无error。
当使用了`default_nettype none 时,一个变量没有定义就使用,由于编译指令的存在,系统会报error,从而检查出书写错误。
`default_nettype none
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n );
wire ab,cd;
assign ab = a & b;
assign cd = c & d;
assign out = ab | cd;
assign out_n = ~ out;
endmodule
8. 7458
module top_module (
input p1a, p1b, p1c, p1d, p1e, p1f,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y );
wire p1abc,p1def,p2ab,p2cd;
assign p1abc = p1a & p1b & p1c;
assign p1def = p1d & p1e & p1f;
assign p1y = p1abc | p1def;
assign p2ab = p2a & p2b;
assign p2cd = p2c & p2d;
assign p2y = p2ab | p2cd;
endmodule
第二部分:Vectors
1. Vector 0
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 = vec;
assign o2 = vec[2];
assign o1 = vec[1];
assign o0 = vec[0];
endmodule
2. Vector 1
`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
3. Vector 2
module top_module(
input [31:0] in,
output [31:0] out );//
// assign out[31:24] = ...;
assign out = {
in[7:0], in[15:8], in[23:16], in[31:24]};
endmodule
4. Vectorgates
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 | b; //按位或
assign out_or_logical = a || b;
assign out_not[5:3] = ~b;
assign out_not[2:0] = ~a;
endmodule
5. Gates 4
module top_module(
input [3:0] in,
output out_and,
output out_or,
output out_xor
);
assign out_and = in[0] & in[1] & in[2] & in[3];
assign out_or = in[0] | in[1] | in[2] | in[3];
assign out_xor = in[0] ^ in[1] ^ in[2] ^ in[3];
endmodule
6. Vector 3
module top_module (
input [4:0] a, b, c, d, e, f,
output [7:0] w, x, y, z );//
// assign { ... } = { ... };
// assign {w[7:0], x[7:0], y[7:0], z[7:0]} = {a[4:0], b[4:0], c[4:0], d[4:0], e[4:0], f[4:0], 2'b11};
//assign {w, x, y, z} = {a, b, c, d, e, f, 2'b11};
assign w = {
a,b[4:2]};
assign x = {
b[1:0],c,d[4]};
assign y = {
d[3:0],e[4:1]};
assign z = {
e[0],f,2'b11};
endmodule
7. Vectorr
给定一个 8 位输入向量 [7:0],反转其位顺序输出。
module top_module(
input [7:0] in,
output [7:0] out
);
//assign {out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7]} = in;
assign out = {
in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]};
endmodule
8. Vector 4
构建一个将 8 位数字符号扩展为 32 位的电路。这需要连接 24 个符号位副本(即复制位 [7] 24 次),然后是 8 位数字本身。
module top_module (
input [7:0] in,
output [31:0] out );//
// assign out = { replicate-sign-bit , the-input };
assign out = {
{
24{
in[7]}}, in};
endmodule
9. Vector 5
module top_module (
input a, b, c, d, e,
output [24:0] out );//
// The output is XNOR of two vectors created by
// concatenating and replicating the five inputs.
// assign out = ~{ ... } ^ { ... };
//assign out = ~{
{5{a}} ,{5{b}},{5{c}} ,{5{d}},{5{e}}} ^ {5{a, b, c, d, e}};
assign out[24:20] = ~ {
5{
a}} ^ {
a, b, c, d, e};
assign out[19:15] = ~ {
5{
b}} ^ {
a, b, c, d, e};
assign out[14:10] = ~ {
5{
c}} ^ {
a, b, c, d, e};
assign out[9:5] = ~ {
5{
d}} ^ {
a, b, c, d, e};
assign out[4:0] = ~ {
5{
e}} ^ {
a, b, c, d, e};
endmodule
第三部分:Modules: Hierarchy
1. Module
module top_module ( input a, input b, output out );
mod_a name ( .out(out), .in1(a), .in2(b) );
endmodule
2. Module pos
module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a name(out1,out2,a,b,c,d);
endmodule
3. Module name
module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a name(.out1(out1), .out2(out2),.in1(a), .in2(b) , .in3(c) , .in4(d) );
endmodule
4. Module shift
module top_module (
input clk,
input d,
output q
);
wire a, b; // 声明两个wire变量,命名为a, b
// 对my_dff进行了三次实例化,用了三个不用的名字 (d1, d2, and d3).
// 端口使用了位置连接的方式( input clk, input d, output q)
my_dff d1 ( clk, d, a );
my_dff d2 ( clk, a, b );
my_dff d3 ( clk, b, q );
endmodule
5. Module shift 8
module top_module (
input clk,
input [7:0] d,
input [1:0] sel,
output [7:0] q
);
wire [7:0] o1, o2, o3; // 声明每一个触发器的输出
// 对 my_dff进行了三次实例化
my_dff8 d1 ( clk, d, o1 );
my_dff8 d2 ( clk, o1, o2 );
my_dff8 d3 ( clk, o2, o3 );
// 4选1
always @(*) //