module fa_behavioral(a,b,ci,s,co);//考虑进位的加法器模块
input a,b;//输入端口,默认为wire型的
input ci;
output s;//输出端口
output co;
reg s;//寄存器,此时由默认的wire型转成了reg型
reg co;
always @(a or b or ci)//a,b,ci为变化量,只要一个发生变化就执行always内部程序
begin //begin,end类似于c语言里的{}
{co,s} = a + b + ci;//{}为拼接,很有用,co即为进位
end
endmodule
module adder(a,b,cin,cout,sum);
parameter bit_width=8;//类似const int
input [bit_width-1:0] a,b;//定义8位wire型变量
input cin;
output [bit_width-1:0] sum;
output cout;
reg sum,cout;
always @(cin or a or b)
begin
{cout,sum}=cin+a+b;
end
endmodule
module substractor(a,b,cin,cout,sum);
parameter bit_width=8;
input [bit_width-1:0] a,b;
input cin;
output [bit_width-1:0] sum;
output cout;
reg sum,cout;
always @(a or b or cin)
begin
{cout,sum}=a-b-cin;//重点是这些公式
end
endmodule
(说实话,这个初学的话还挺难的,尽量看懂理解吧)
涉及到双符号位补码,双符号位有助于判断是否溢出。
module add_sub(a,b,control,cout,overflow,sum);
parameter bit_width=4;
input [bit_width-1:0] a,b;
input control;
output[bit_width-1:0] sum;
output cout,overflow;
reg overflow,cout;
reg [bit_width-1:0] sum;
reg [bit_width:0] a2,b2,sum2;
always @(a or b or control)
begin
b2[bit_width]=b[bit_width-1];
b2[bit_width-1:0]=b[bit_width-1:0];
a2[bit_width]=a[bit_width-1];
a2[bit_width-1:0]=a[bit_width-1:0];
if(!control)
begin
{cout,sum2}=a2+b2;
end
else
begin
{cout,sum2}=a2+(~b2)+1;
end
overflow=sum2[bit_width]^sum2[bit_width-1];
sum[bit_width-1:0]=sum2[bit_width-1:0];
end
endmodule
//设计一个输入输出均为高电平有效的3位二进制优先编码器
//I[7]的优先权最高,I[0]的优先权最低
module encoder8_3_test(I,Y);
input [7:0] I;
output reg[2:0] Y;
always @(I)
begin
if(I >= 8'b00000000 && I < 8'b00000010)
Y = 3'b000;
if(I >= 8'b00000010 && I < 8'b00000100)
Y = 3'b001;
if(I >= 8'b00000100 && I < 8'b00001000)
Y = 3'b010;
if(I >= 8'b00001000 && I < 8'b00010000)
Y = 3'b011;
if(I >= 8'b00010000 && I < 8'b00100000)
Y = 3'b100;
if(I >= 8'b00100000 && I < 8'b01000000)
Y = 3'b101;
if(I >= 8'b01000000 && I < 8'b10000000)
Y = 3'b110;
if(I >= 8'b10000000)
Y = 3'b111;
end
endmodule
//设计具有一位使能端的3线-8线译码器。当使能端为0时,8位输出信号全为0;
//如果一位使能信号为1,则输出高电平有效的译码信号。
module decoder3e_test(a,ena,y);
input [2:0] a;
input ena;
output reg[7:0] y;
always @(ena or a)
begin
if(ena==1'b0)
begin
y=8'b000000000;
end
else
begin
case(a)
3'b000:y=8'b00000001;
3'b001:y=8'b00000010;
3'b010:y=8'b00000100;
3'b011:y=8'b00001000;
3'b100:y=8'b00010000;
3'b101:y=8'b00100000;
3'b110:y=8'b01000000;
3'b111:y=8'b10000000;
default:y=8'b00000000;
endcase
end
end
endmodule
module JG3(ABC,X,Y);
input [2:0] ABC;
output reg X, Y;
reg [1:0] cnt;
integer i;
always@(ABC)
if(ABC==0)
begin
X=1'b0;
Y=1'b1;
end
else if(ABC[2]==1'b0)
begin
X=0;
Y=0;
end
else
begin
cnt=0;
for(i=0;i<3;i=i+1)
begin
if(ABC[i])
cnt=cnt+1;
end
if(cnt[1]==1)
X=1'b0;
Y=1'b0;
end
endmodule
module mux21(a,b,s,y);
input a,b,s;
output y;
reg y;
always @(a,b,s)
begin
y = s ? b : a;
end
endmodule
module hadder_test(a,b,cout,sum);
input a,b;
output reg cout,sum;
always @(a or b)
begin
{cout,sum}=a+b;
end
endmodule
module fadder_test(a,b,ci,s,co);//考虑进位的加法器模块
input a,b,ci;
output reg s,co;
always @(a or b or ci)
begin
{co,s}=a+b+ci;
end
endmodule
module JG3(ABC,X,Y);
input [2:0] ABC;
output reg X, Y;
reg [1:0] cnt;
integer i;
always@(ABC)
if(ABC==0)
begin
X=1'b0;
Y=1'b1;
end
else if(ABC[2]==1'b0)
begin
X=0;
Y=0;
end
else
begin
cnt=0;
for(i=0;i<3;i=i+1)
begin
if(ABC[i])
cnt=cnt+1;
end
if(cnt[1]==1)
X=1'b0;
Y=1'b0;
end
endmodule
module count_test(en,clk,clr,cout,outy);
input en,clk,clr;
output [3:0]outy;
output cout;
reg [3:0]outy;
reg [2:0] cnt;
integer i;
always @ (posedge clk or posedge clr)
begin
if(clr)
outy<=4'b0000;
else if(en)
begin
cnt=3'b000;
for(i=0;i<4;i=i+1)
if(outy[i])
cnt=cnt+1;
if(cnt==3'b100)
begin
outy<=4'b0000;
end
else
outy<=outy+1;
end
end
assign cout=((outy==4'b1111)&&(en))?1:0;
endmodule
(我们当时考试考了这个)
module shift_test(din,s,srsi,slsi,clk,clr,dout);
input[7:0] din;
input[1:0] s;
input srsi,slsi,clk,clr;
output [7:0] dout;
reg [7:0] dout;
always@(posedge clk or negedge clr)
begin
if(!clr) dout <= 0;
else if(s == 2'b01)
begin
dout[0] <= dout[1];
dout[1] <= dout[2];
dout[2] <= dout[3];
dout[3] <= dout[4];
dout[4] <= dout[5];
dout[5] <= dout[6];
dout[6] <= dout[7];
dout[7] <= srsi;
end
else if(s == 2'b10)
begin
dout[7] <= dout[6];
dout[6] <= dout[5];
dout[5] <= dout[4];
dout[4] <= dout[3];
dout[3] <= dout[2];
dout[2] <= dout[1];
dout[1] <= dout[0];
dout[0] <= slsi;
end
else if(s == 2'b11)
dout[7:0] = din[7:0];
else
dout[7:0]=dout[7:0];
end
endmodule
这题确实太难了点,可以放弃。
module counter6bit_test(ENA,CLR,F_IN,Q);
input ENA;
input CLR;
input F_IN;
output [23:0] Q;
reg [23:0] Q;
reg F_OUT;
always @ (posedge F_IN)
begin
if (CLR == 1'b1)
Q <= 24'b0000_0000_0000_0000_0000_0000;
else
if(ENA == 1'b0)
Q[3:0] <= Q[3:0];
else
if (Q[3:0] < 4'b1001)
Q[3:0] <= Q[3:0] +4'b0001;
else
begin
Q[3:0] <= 4'b0000;
if (Q[7:4] < 4'b1001)
Q[7:4] <= Q[7:4] + 4'b0001;
else
begin
Q[7:4] <= 4'b0000;
if (Q[11:8] < 4'b1001)
Q[11:8] <= Q[11:8] + 4'b0001;
else
begin
Q[11:8] <= 4'b0000;
if (Q[15:12] < 4'b1001)
Q[15:12] <= Q[15:12] + 4'b0001;
else
begin
Q[15:12] <= 4'b0000;
if (Q[19:16] < 4'b1001)
Q[19:16] <= Q[19:16] + 4'b0001;
else
begin
Q[19:16] <= 4'b0000;
if (Q[23:20] < 4'b1001)
Q[23:20] <= Q[23:20] + 4'b0001;
else
Q[23:20] <= 4'b0000;
end
end
end
end
end
end
endmodule
module latch24_test( d, clk,q );
input[23:0] d;
input clk;
output [23:0] q;
reg[23:0] q;
always @(posedge clk)
q<=d;
endmodule
第七个和第八个可以不需要了解,太难了。
module dff4_test(d,clk,clrn,q);
// 请在下面添加代码,完成4位寄存器逻辑功能
parameter width=4;
input [width-1:0] d;
input clk,clrn;
output [width-1:0] q;
reg [width-1:0] q;
always @(negedge clrn or posedge clk)
if (clrn==0) begin q<=0; end
else q<=d;
endmodule
module detected_test(ds,setd,clk,clrn,dc,c);
input ds,clk,clrn;
input [7:0] setd;
output [3:0] c;
output dc;
reg [3:0] c;
parameter s0=4'b0000,s1=4'b0001,s2=4'b0010,s3=4'b0011,
s4=4'b0100,s5=4'b0101,s6=4'b0110,s7=4'b0111,s8=4'b1000;
always@(posedge clk or negedge clrn)
begin
if(!clrn)
c<=s0;
else
case(c)
s0:if(ds==setd[7])c<=s1;else c<=s0;
s1:if(ds==setd[6])c<=s2;else c<=s0;
s2:if(ds==setd[5])c<=s3;else c<=s0;
s3:if(ds==setd[4])c<=s4;else c<=s0;
s4:if(ds==setd[3])c<=s5;else c<=s0;
s5:if(ds==setd[2])c<=s6;else c<=s0;
s6:if(ds==setd[1])c<=s7;else c<=s0;
s7:if(ds==setd[0])c<=s8;else c<=s0;
default:c<=s0;
endcase
end
assign dc=(c==s8)?1:0;
endmodule
module tf_ctrl_test(clk,enb,lock,clr);
input clk;
output enb,lock,clr;
reg enb,lock,clr;
reg [3:0]count;
initial begin enb=0;lock=0;clr=0;count=0;end
always @(posedge clk)
begin
count<=count+4'b0001;
if (count<=7)
begin enb<=1;clr<=0;lock<=0;end
else if (count==9)
begin enb<=0;clr<=0;lock<=1;end
else if (count==13)
begin enb<=0;clr<=1;lock<=0;end
else if (count==15)
begin enb<=0;clr<=0;lock<=0;count<=0;end
else
begin enb<=0;clr<=0;lock<=0;end
end
endmodule
module szplj_test(f_in,clk,q);
input clk,f_in;
output [23:0] q ;
wire [23:0]d;
wire clr,enb,lock;
tf_ctrl tf_ctrl(clk,enb,lock,clr);
counter6bit counter6bit(enb,clr,f_in,d);
latch24 latch24(d,lock,q);
endmodule
module tf_ctrl(clk,enb,lock,clr);
input clk;
output enb,lock,clr;
reg enb,lock,clr;
reg [3:0]count;
initial begin enb=0;lock=0;clr=0;count=0;end
always @(posedge clk)
begin
count<=count+4'b0001;
if (count<=7)
begin enb<=1;clr<=0;lock<=0;end
else if (count==9)
begin enb<=0;clr<=0;lock<=1;end
else if (count==13)
begin enb<=0;clr<=1;lock<=0;end
else if (count==15)
begin enb<=0;clr<=0;lock<=0;count<=0;end
else
begin enb<=0;clr<=0;lock<=0;end
end
endmodule
module counter6bit(ENA,CLR,F_IN,Q);
input ENA;
input CLR;
input F_IN;
output [23:0] Q;
reg [23:0] Q;
reg F_OUT;
always @ (posedge F_IN)
begin
if (CLR == 1'b1)
Q <= 24'b0000_0000_0000_0000_0000_0000;
else
if(ENA == 1'b0)
Q[3:0] <= Q[3:0];
else
if (Q[3:0] < 4'b1001)
Q[3:0] <= Q[3:0] +4'b0001;
else
// 请在下面添加代码,完成6位十进制计数器
/* Begin */
begin
Q[3:0] <= 4'b0000;
if (Q[7:4] < 4'b1001)
Q[7:4] <= Q[7:4] + 4'b0001;
else
begin
Q[7:4] <= 4'b0000;
if (Q[11:8] < 4'b1001)
Q[11:8] <= Q[11:8] + 4'b0001;
else
begin
Q[11:8] <= 4'b0000;
if (Q[15:12] < 4'b1001)
Q[15:12] <= Q[15:12] + 4'b0001;
else
begin
Q[15:12] <= 4'b0000;
if (Q[19:16] < 4'b1001)
Q[19:16] <= Q[19:16] + 4'b0001;
else
begin
Q[19:16] <= 4'b0000;
if (Q[23:20] < 4'b1001)
Q[23:20] <= Q[23:20] + 4'b0001;
else
Q[23:20] <= 4'b0000;
end
end
end
end
end
/* End */
end
endmodule
module latch24( d, clk,q );
output [23:0] q;
reg [23:0] q;
input[23:0] d;
input clk;
always @( posedge clk )
// 请在下面添加代码,完成24位寄存器
/* Begin */
q <= d;
/* End */
endmodule