该专栏下文章为本人学习时的笔记及对一些知识点的理解,无法保证正确与否,有误之处还望指出。
程序:
module add_8(cout,sum,a,b,cin);
output cout;
output [7:0] sum;
input cin;
input [7:0]a,b;
assign {cout,sum}=a+b+cin;
endmodule
测试程序
`timescale 1ns/1ns
module add_8_tb();
reg cin;
reg [7:0]a,b;
wire cout;
wire [7:0]sum;
add_8 t1(
.cin(cin),
.a(a),
.b(b),
.cout(cout),
.sum(sum));
initial
begin
cin = 1;
a = 8'd109;
b = 8'd10;
end
endmodule
程序:
`define plus 3'd0
`define minus 3'd1
`define band 3'd2
`define bor 3'd3
`define unegate 3'd4
module test1(out,opcode,a,b);
output [7:0]out;
input [2:0]opcode;
input [7:0]a,b;
reg [7:0]out;
always@(opcode or a or b)
begin
case(opcode)
`plus:out=a+b;
`minus:out=a-b;
`band:out=a&b;
`bor:out=a|b;
`unegate:out=~a;
default:out=8'hx;
endcase
end
endmodule
测试程序:
`timescale 1ns/1ns
module test1_tb();
reg [7:0]a,b;
reg [2:0]opcode;
wire [7:0]out;
test1 t1(
.out(out),
.opcode(opcode),
.a(a),
.b(b));
initial
begin
a = 8'd19;
b = 8'd10;
#10 opcode = 3'd0;
#10 opcode = 3'd1;
#10 opcode = 3'd2;
#10 opcode = 3'd3;
#10 opcode = 3'd4;
end
endmodule
程序:
module sort(ra,rb,rc,rd,a,b,c,d);
parameter t=3;
output [t:0]ra,rb,rc,rd;
input [t:0]a,b,c,d;
reg [t:0]ra,rb,rc,rd;
always@(a or b or c or d)
begin:local
reg [t:0]va,vb,vc,vd;
{va,vb,vc,vd}={a,b,c,d};
sort2(va,vc);
sort2(vb,vd);
sort2(va,vb);
sort2(vc,vd);
sort2(vb,vc);
{ra,rb,rc,rd}={va,vb,vc,vd};
end
task sort2;
inout [t:0]x,y;
reg [t:0] tmp;
if(x>y)
begin
tmp = x;
x = y;
y = tmp;
end
endtask
endmodule
测试程序:
`timescale 1ns/1ns
module sort_tb();
reg [3:0]a,b,c,d;
wire [3:0]ra,rb,rc,rd;
sort t1(
.a(a),
.b(b),
.c(c),
.d(d),
.ra(ra),
.rb(rb),
.rc(rc),
.rd(rd));
initial
begin
a=4'd3;
b=4'd7;
c=4'd1;
d=4'd4;
end
endmodule
程序:
module compare(equal,a,b);
parameter size=1;
output equal;
input [size-1:0]a,b;
assign equal=(a==b)?1:0;
endmodule
测试程序:
`timescale 1ns/1ns
module compare_tb();
reg a,b;
wire equal;
compare t1(
.equal(equal),
.a(a),
.b(b));
initial
begin
a=0;
b=1;
#10 a=1;
end
endmodule
程序:
module decoder(out,in);
output [7:0]out;
input [2:0]in;
assign out=1'b1<
测试程序:
`timescale 1ns/1ns
module decoder_tb();
reg [2:0]in;
wire [7:0]out;
decoder t1(
.out(out),
.in(in));
initial
in=3'b001;
#10 in=3'b010;
#10 in=3'b011;
#10 in=3'b100;
endmodule