在Verilog实验中 任意数值分频 包括奇数 偶数 小数
module count_odd
(input clk,reset,N,output cout);
reg m,n; reg cout1,cout2;
assign cout=cout1|cout2;
always @(posedge clk)
begin if(!reset) begin cout1<=0;m<=0;end
else
begin if(m==N-1) m<=0;else m<=m+1;
if(m<(N-1)/2) cout1<=1;else cout1<=0;
end end
always @(negedge clk)
begin if(!reset) begin cout2<=0;n<=0;end
else begin
if(n==N-1) n<=0;else n<=n+1;
if(n<(N-1)/2) cout2<=1;else cout2<=0;end
end
endmodule
module count_even
(input clk,reset,N,output reg cout);
reg m;
always @(posedge clk or negedge reset)
begin
if(!reset)
begin m<=0;cout<=0;end
else if(m==N-1)
begin m<=0;cout<=~cout;end
else
begin m<=m+1;end
end
endmodule
module count_dec
(input clk,reset,N,output reg cout);
reg cout1,cout2;
always @(posedge clk,posedge reset)
begin if(reset)begin cout1<=0;cout2<=0;cout<=0;end
else if(cout1<9)
begin
if(cout2<(N/10)-1)begin cout2<=cout2+1;cout<=0;end
else begin cout2<=0;cout1<=cout1+1;cout<=1;end
end
else
begin
if(cout2<N-9*(N/10)-1) begin cout2<=cout2+1;cout<=0;end
else begin cout2<=0;cout1<=cout1+1;cout<=1;end
end
end
endmodule
module count (
input clk,reset,
input N,
output cout
);
reg [1:0] a;
always @*
begin
if( N%2==0&&N%1==0)
begin
a<=2'b00;
end
else if(N%2==1&&N%1==0)
begin
a<=2'b01;
end
else
begin
a<=2'b10;
end
end
always@(a)
begin
case(a)
2'b00: count_even(.clk(clk),.reset(reset),.N(N),.cout(cout));
2'b01: count_odd(.clk(clk),.reset(reset),.N(N),.cout(cout));
default: count_even(.clk(clk),.reset(reset),.N(N),.cout(cout));
endcase
end
endmodule
tb
module count_tb();
reg clk,reset,N;
wire cout;
count i1(
.clk(clk),
.reset(reset),
.N(N),
.cout(cout)
);
parameter DELY=20;
always
begin
clk=1;
#(DELY) clk<=~clk;
end
initial
begin
N<=8;reset<=1;
#(DELY*10)N=11;
#(DELY*10)N=8.1;
#(DELY*10) $stop;
end
endmodule
[USF-XSim-62] 'elaborate' step failed with error(s). Please check the Tcl console output or 'D:/5e/count/count.sim/sim_1/behav/xsim/elaborate.log' file for more information.
[Vivado 12-4473] Detected error while running simulation. Please correct the issue and retry this operation.
[VRFC 10-2989] 'count_even' is not declared ["D:/5e/count/count.srcs/sources_1/new/count.v":96]
[XSIM 43-3322] Static elaboration of top level Verilog design unit(s) in library work failed.
[VRFC 10-2989] 'count_odd' is not declared ["D:/5e/count/count.srcs/sources_1/new/count.v":97]
[VRFC 10-2989] 'count_even' is not declared ["D:/5e/count/count.srcs/sources_1/new/count.v":98]
######最开始是用if-else语句
if( N%2==0&&N%1==0)
begin
count_even(.clk(clk),.reset(reset),.N(N),.cout(cout));
end
else if(N%2==1&&N%1==0)
begin
count_odd(.clk(clk),.reset(reset),.N(N),.cout(cout));
end
else
begin
count_even(.clk(clk),.reset(reset),.N(N),.cout(cout));
end
后来问了才知道 Verilog 好像识别不了这种写法 换了种写法 发现没有(错误波浪线)以为case语句可以用
后来仿真还是仿真不了出错误