Bai Tap Verilog HDL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1.

Viết chương trình Verilog HDL mô tả mạch đếm lên-xuống có đặc điểm sau:
- Đầu vào Clear: nếu =1. bộ đếm sẽ xóa và reset về 0.
- Đầu vào U/D:
o =1, đếm lên
o = 0, đếm xuống
- Bộ đếm sẽ đếm tại sườn âm của xung clock.
- Bộ đếm sẽ đếm lên hoặc xuống trong khoảng từ 0 đến N, N là số 4 bit.
module counterup(a,clk,N);
input clk;
input[3:0]N;
output[3:0]a;
reg[3:0]a;
initial a=4'b0000;
always@(negedge clk) a=(a==N)?4'b0000:a+1'b1;
endmodule
module tst_counterup;//TEST_BENCH
reg clk;
reg[3:0]N;
wire[3:0]a;
counterup c1(a,clk,N);
initial
begin
clk = 0;
N = 4'b1011;
end
always #2 clk=~clk;
initial $monitor($time,"a=%b,clk=%b,N=%b",a,clk,N);
endmodule
//====================================================
module counterdn(a,clk,N);
input clk;
input[3:0]N;
output[3:0]a;
reg[3:0]a;
initial a =4'b0000;
always@(negedge clk) a=(a==4'b0000)?N:a-1'b1;
endmodule
module tst_counterdn();//TEST_BENCH
reg clk;
reg[3:0]N;
wire[3:0]a;
counterdn cc(a,clk,N);
initial
begin
N = 4'b1010;
Clk = 0;
end
always #2 clk=~clk;
initial $monitor($time,"a=%b,clk=%b,N=%b",a,clk,N);
initial #55 $stop;
endmodule
//=====================================================
module updcounter(a,clk,N,u_d);
input clk,u_d;
input[3:0]N;
output[3:0]a;
reg[3:0]a;
initial a =4'b0000;
always@(negedge clk)
a=(u_d)?((a==N)?4'b0000:a+1'b1):((a==4'b0000)?N:a- 1'b1);
endmodule
module tst_updcounter();//TEST_BENCH
reg clk,u_d;
reg[3:0]N;
wire[3:0]a;
updcounter c2(a,clk,N,u_d);
initial
begin
N = 4'b0111;
u_d = 1'b0;
clk = 0;
end
always #2 clk=~clk;
always #34u_d=~u_d;
initial $monitor
($time,"clk=%b,N=%b,u_d=%b,a=%b",clk,N,u_d,a);
initial #64 $stop;
endmodule

2. Viết chương trình Verilog HDL mô tả thanh ghi dịch 8 bit và viết test band mô
phỏng . Thanh ghi sẽ dịch 1 bit sang phải nếu r_l= 1 và dịch trái nếu r_l=0.
Câu lệnh : always@(negedge clk) a=(r_l)?(a>>1'b1):(a<<1'b1);
module shifrlter(a,clk,r_l);
input clk,r_l;
output [7:0]a;
reg[7:0]a;
initial a= 8'h01;
always@(negedge clk)
begin
a=(r_l)?(a>>1'b1):(a<<1'b1);
end
endmodule
module tst_shifrlter;//test-bench
reg clk,r_l;
wire [7:0]a;
shifrlter shrr(a,clk,r_l);
initial
begin
clk =1'b1;
r_l = 0;
end
always #2 clk =~clk;
initial #16 r_l =~r_l;
initial
$monitor($time,"clk=%b,r_l = %b,a =%b ",clk,r_l,a);
initial #30 $stop;
endmodule
Bài 3. Memory Block: Have a 1 kb size memory with a 10-bit Memory Address
Register. Use clock beta for memory read and memory write. Use Wr and Rd as two
separate control input lines. The operations to be realized are:
- Wr=1: Write into the location specified by the MAR.
- RD=1: Read from location specified by MAR.
- Wr=0 & Rd=0: Condition to be satisfied to write into the MAR.
Data input and data output are to be through an 8-bit-wide bus “ba.”
Bài 4: Mô tả thiết kế bộ đếm lên mod n và test band. Sau mỗi xung đồng hồ bộ đếm tăng
lên 1. khi bộ đếm đạt giá trị n, bộ đếm sẽ reset về 0. Giá trị ban đầu của n được xác định
trong module và có thể thay đổi được. .
//counter using if else if;
module countif(a,clk);
output[7:0]a;
input clk;
reg[7:0]a,n;
initial
begin
n=8'h0a;
a=8'b00000000;
#45 n=8'h23;
end
always@(posedge clk)
begin
$write ("time=%0d ",$time);
if(a==n)
a=8'h00;
else a=a+1'b1;
end
endmodule
module tst_countif();//test-bench
reg clk;
wire[7:0]a;
countif c1(a,clk);
initial clk =1'b0;
always
#2 clk=~clk;
initial
$monitor(" n=%h, a=%h",c1.n,a);
initial #200 $stop;
endmodule

3.

You might also like