文章目录
前言
HDL- bits 记录
HDL-bits
一、Verilog language
1. basic
1. Four wires
Create a module with 3 inputs and 4 outputs that behaves like wires that makes these connections:
a -> w
b -> x
b -> y
c -> z

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
2. Inverter
Create a module that implements a NOT gate.

module top_module( input in, output out );
assign out = ~in;
endmodule
3. NOR gate

Verilog has separate bitwise-OR (|) and logical-OR (||) operators
module top_module(
input a,
input b,
output out );
assign out = !(a || b);
or assign out = !(a | b);
endmodule
4. XNOR gate

The bitwise-XOR operator is ^. There is no logical-XOR operator.
module top_module(
input a,
input b,
output out );
assign out = ~(a^b);
endmodule
5. Declaring wires

module top_module (
input in, // Declare an input wire named "in"
output out // Declare an output wire named "out"
);
wire not_in; // Declare a wire named "not_in"
assign out = ~ not_in; // Assign a value to out (create a NOT gate).
assign not_in = ~ in; // Assign a value to not_in (create another NOT gate).
endmodule // End of module "top_module"

`default_nettype none
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n );
wire two_wires, one_wires;
assign two_wires = a & b;
assign one_wires = two_wires || (c & d);
assign out_n = ~one_wires;
assign out = one_wires;
endmodule
wire w1, w2; // Declare two wires (named w1 and w2)
assign w1 = a&b; // First AND gate
assign w2 = c&d; // Second AND gate
assign out = w1|w2; // OR gate: Feeds both 'out' and the NOT gate
assign out_n = ~out; // NOT gate
6. 7458 chip

module top_module (
input p1a, p1b, p1c, p1d, p1e, p1f,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y );
wire w1,w2,w3,w4;
assign w1 = p1a & p1b & p1c;
assign w2 = p1d & p1e & p1f;
assign w3 = p2a & p2b;
assign w4 = p2c & p2d;
assign p2y = w3 || w4;
assign p1y = w1 || w2;
endmodule
2. Vectors
1. Vectors
wire [99:0] my_vector; // Declare a 100-element vector
assign out = my_vector[10]; // Part-select one bit out of the vector

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 o0 = vec[0];
assign o1 = vec[1];
assign o2 = vec[2];
assign {
o2,o1,o0} = vec;
endmodule
2. Vectors in more detail
Declaring Vectors
wire [7:0] w; // 8-bit wire
reg [4:1] x; // 4-bit reg
output reg [0:0] y; // 1-bit reg that is also an output port (this is still a vector)
input wire [3:-2] z; // 6-bit wire input (negative ranges are allowed)
output [3:0] a; // 4-bit output wire. Type is 'wire' unless specified otherwise.
wire [0:7] b; // 8-bit wire where b[0] is the most-significant bit.
Implicit nets
wire [2:0] a, c; // Two vectors
assign a = 3'b101; // a = 101
assign b = a; // b = 1 implicitly-created wire
assign c = b; // c = 001 <-- bug
my_module i1 (d,e); // d and e are implicitly *one-bit wide* if not declared.
// This could be a bug *if the port was intended to be a vector*.
Adding `default_nettype none would make the second line of code an error, which makes the bug more visible.
Unpacked vs. Packed Arrays
reg [7:0] mem [255:0]; // 256 unpacked elements, each of which is a 8-bit packed vector of reg.
reg mem2 [28:0]; // 29 unpacked elements, each of which is a 1-bit reg.
Accessing Vector Elements: Part-Select
w[3:0] // Only the lower 4 bits of w
x[1] // The lowest bit of x
x[1:1] // ...also the lowest bit of x
z[-1:-2] // Two lowest bits of z
b[3:0] // Illegal. Vector part-select must match the direction of the declaration.
b[0:3] // The *upper* 4 bits of b.
assign w[3:0] = b[0:3]; // Assign upper 4 bits of b to lower 4 bits of w. w[3]=b[0], w[2]=b[1], etc.
A Bit of Practice
Build a combinational circuit that splits an input half-word (16 bits, [15:0] ) into lower [7:0] and upper [15:8] bytes.
`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];
// Concatenation operator also works: assign {out_hi, out_lo} = in;
endmodule
3. Vector part select
A 32-bit vector can be viewed as containing 4 bytes (bits [31:24], [23:16], etc.). Build a circuit that will reverse the byte ordering of the 4-byte word.
AaaaaaaaBbbbbbbbCcccccccDddddddd => DdddddddCcccccccBbbbbbbbAaaaaaaa
module top_module (
input [31:0] in,
output [31:0] out
);
assign out[31:24] = in[ 7: 0];
assign out[23:16] = in[15: 8];
assign out[15: 8] = in[23:16];
assign out[ 7: 0] = in[31:24];
endmodule
4. bitwise operators

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[2:0] = ~a; // Part-select on left side is o.
assign out_not[5:3] = ~b; //Assigning to [5:3] does not conflict with [2:0]
endmodule
4. Four-input gates
Build a combinational circuit with four inputs, in[3:0].
There are 3 outputs:
out_and: output of a 4-input AND gate.
out_or: output of a 4-input OR gate.
out_xor: output of a 4-input XOR gate.
module top_module(
input [3:0] in,
output out_and,
output out_or,
output out_xor
);
assign out_and = ∈
assign out_or = |in;`在这里插入代码片`
assign out_xor = ^in;
endmodule
5. vector concatenate operator
{
3'b111, 3'b000} => 6'b111000
{
1'b1, 1'b0, 3'b101} => 5'b10101
{
4'ha, 4'd10} => 8'b10101010 // 4'ha and 4'd10 are both 4'b1010 in binary
input [15:0] in;
output [23:0] out;
assign {
out[7:0], out[15:8]} = in; // Swap two bytes. Right side and left side are both 16-bit vectors.
assign out[15:0] = {
in[7:0], in[15:8]}; // This is the same thing.
assign out = {
in[7:0], in[15:8]}; // This is different. The 16-bit vector on the right is extended to
// match the 24-bit vector on the left, so out[23:16] are zero.
// In the first two examples, out[23:16] are not assigned.

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
6. Vector reversal 1
Given an 8-bit input vector [7:0], reverse its bit ordering.
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;
/*
// I know you're dying to know how to use a loop to do this:
// Create a combinational always block. This creates combinational logic that computes the same result
// as sequential code. for-loops describe circuit *behaviour*, not *structure*, so they can only be used
// inside procedural blocks (e.g., always block).

最低0.47元/天 解锁文章
676

被折叠的 条评论
为什么被折叠?



