*赋值语句执行完后,块才结束;
*B的值在赋值语句结束时立即发生改变;
*在时序电路中使用时可能发生意想不到的后果。
非阻塞赋值方式:如:(a<=b)
四位全加器代码
module adder4 (count,sum,ina,inb,inc); //以分号结束
output [3:0] sum; //表示四位输出数据
output count ; //省略位宽表示一位
input [3:0] ina,inb;
input inc;
assign {count,sum}=ina+inb+inc;
Endmodule //连写不能加分号
一位计数器代码
module shiweijishu (reset,clk,out);
output [3:0] out;
input clk,reset;
reg out; //
always @(posedge clk) //
begin
if (reset) out<=0; //
else if (out==9) out<=0;
else out<=out+1;
out<=out;
end
Endmodule
If语句
(1)if(表达式)语句 例如:if(a(2)If(表达式)语句1;
Else 语句2;
例如:if(aElse out2<=in2;
(3) if(表达式1) 语句1 ;
Else if(表达式2)语句2;
Else if(表达式3)语句3;
……
Else if(表达式n)语句n;
Else 语句m
例如:if(aElse If(a==b) out2<=in2;
Else out3<=in3;
Reg
module nand2(y,a,b);
output y;
input a,b;
reg y;
always @(a,b)
begin
case ({a,b})
2'b00:y=1;
2'b01:y=1;
双输入与非门
2'b10:y=1;
2'b11:y=0;
default:y='bx;
endcase
end
Endmodule
Case 语句:
module nor_2 (y,a,b);
input a,b;
output y;
reg y;
always @(a,b)
二输入与非门
begin
if ({a,b}==00) y<=1;
else if ({a,b}==01) y<=0;
else if ({a,b}==10) y<=0;
else y<=0;
end
endmodule
代码:
module yima3_8(a,q);
input [2:0]a;
output [7:0]q;
reg [7:0]q;
三八译码器
always @(a)
begin
case (a[2:0])
3'b000:q[7:0]<=00000000;
3'b001:q[7:0]<=00000010;
3'b010:q[7:0]<=00000100;
3'b011:q[7:0]<=00001000;
3'b100:q[7:0]<=00010000;
3'b101:q[7:0]<=00100000;
3'b110:q[7:0]<=01000000;
3'b111:q[7:0]<=10000000;
endcase
end
endmodule
数码管显示
代码:
module key_led7(key,led7,led7_sc);
input [7:0]key;
output [7:0]led7;
output [2:0]led7_sc;
reg [7:0]led7;
reg [2:0]led7_sc;
always @(key)
begin
case (key[7:0])
8'b11111110:begin led7_sc[2:0]<=000;led7[7:0]<=8'b00000110;end
8'b11111101:begin led7_sc[2:0]<=001;led7[7:0]<=8'b01011011;end
8'b11111011:begin led7_sc[2:0]<=010;led7[7:0]<=8'b01001111;end
8'b11110111:begin led7_sc[2:0]<=011;led7[7:0]<=8'b01100110;end
8'b11101111:begin led7_sc[2:0]<=100;led7[7:0]<=8'b01101101;end
8'b11011111:begin led7_sc[2:0]<=101;led7[7:0]<=8'b01111101;end
8'b10111111:begin led7_sc[2:0]<=110;led7[7:0]<=8'b00000111;end
8'b01111111:begin led7_sc[2:0]<=111;led7[7:0]<=8'b01111111;end
8'b11111111:begin led7_sc[2:0]<=111;led7[7:0]<=8'b00000000;end
default: begin led7_sc[2:0]<='bx;led7[7:0]<='bx;end
endcase
end
endmodule
八选一多路选择器
代码:
module mux8 (y,a0,a1,a2,a3,a4,a5,a6,a7,b,g);
output y;
input a0,a1,a2,a3,a4,a5,a6,a7;
input [2:0]b;
input g;
reg y;
always @(b,a0,a1,a2,a3,a4,a5,a6,a7,g)
begin
if (g==0) y<=0;
else
case (b[2:0])
3'b000:y<=a0;
3'b001:y<=a1;
3'b010:y<=a2;
3'b011:y<=a3;
3'b100:y<=a4;
3'b101:y<=a5;
3'b110:y<=a6;
3'b111:y<=a7;
endcase
end
Endmodule
数据分配器
代码:
module demux4 (a,b,y0,y1,y2,y3);
input a;
input [1:0]b;
output y0,y1,y2,y3;
reg y0,y1,y2,y3;
always @(a,b)
begin
case (b[1:0])
2'b00:y0<=a;
2'b01:y1<=a;
2'b10:y2<=a;
2'b11:y3<=a;
endcase
end
endmodule
数据比较器
代码:
module comparator (a,b,y0,y1,y2);
input [3:0]a;
input [3:0]b;
output y0,y1,y2;
reg y0,y1,y2;
always @(a,b)
begin
if (a>b)
begin
y0=1;y1=0;y2=0;
end
else if (a==b)
begin
y0=0;y1=1;y2=0;
end
else if (abegin
y0=0;y1=0;y2=1;
end
end
endmodule
代码:
半加器
方法一:块语句
module half_add(a,b,s,c);
input a,b;
output s,c;
reg s,c;
always @(a,b)
begin
case ({a,b})
2'b00:begin s=0;c=0;end
2'b01:begin s=1;c=0;end
2'b10:begin s=1;c=0;end
2'b11:begin s=0;c=1;end
endcase
end
endmodule
方法二:数据流
module half_add(a,b,s,c);
input a,b;
output s,c;
assign s=a^b;
assign c=a&b;
endmodule
方法三:元件调用
module half_add(a,b,s,c);
input a,b;
output s,c;
and(c,a,b);
xor(s,a,b);
endmodule
因篇幅问题不能全部显示,请点此查看更多更全内容