2015-11-10 40 views
0

我想學習的Verilog ,我得到了我的代碼的問題: 當我試圖加我的號碼,如 第一個值CLK問題>第二個值CLK> 3值clk>第四值clk>這裏應該是我的平均值,但由於某些未知的原因,我需要再次按下時鐘2次以獲得大幅值 ,我不知道爲什麼。 (我認爲這是在我的avereger代碼有問題) 爲什麼我有這樣的延遲 我avereger必須得到4 - 10位的號碼,告訴我它的averege(我的發光二極管)的Verilog(註冊代碼延遲或錯誤?)

這裏是我的代碼 和我包括我的矢量形式[

image

]

module avereger (data_in,Led1,Led2,Led3,clk,rst); 
input clk,rst; 
input [9:0]data_in; 
output[6:0] Led1,Led2,Led3; 
reg [9:0] data[3:0]; 
reg [11:0] sum,avg_reg; 
always @ (posedge clk) 
begin 
if(rst==1'b1) 
begin //all values will be 0 
data[0]<=0; 
data[1]<=0; 
data[2]<=0; 
data[3]<=0;  
sum<=0; 
avg_reg<=0; 
end 
else 
begin 
data[3]<=data[2]; 
data[2]<=data[1]; 
data[1]<=data[0]; 
data[0]<=data_in; 
sum<= data[0] + data[1] + data[2] + data[3]; 
avg_reg<=sum[11:2]; 
end 
end 
decoder_driver BCD(avg_reg,Led1,Led2,Led3); 
endmodule 

module decoder_driver(A,Led1,Led2,Led3); 
input [9:0] A; 
output [6:0] Led1,Led2,Led3; 
reg [3:0] B,C,D; 
wire [3:0] wireB,wireC,wireD; 
assign wireB=B; 
assign wireC=C; 
assign wireD=D; 
display1 HUN(wireB,Led1); 
display1 TEN(wireC,Led2); 
display1 ONE(wireD,Led3); 
always @ (A) 
begin 
B=A/7'd100; 
C=A%7'd100/7'd10; 
D=A%7'd100%7'd10; 
end 
endmodule 

module display1 (a,Led); 
input [3:0] a; 
output [6:0] Led; 
reg [6:0] Led; 
always @ (a) 
begin 
case(a) 
4'b0000: Led <= 7'b1000000; //0 
4'b0001: Led <= 7'b1111001; //1 
4'b0010: Led <= 7'b0100100; //2 
4'b0011: Led <= 7'b0110000; //3 
4'b0100: Led <= 7'b0011001; //4 
4'b0101: Led <= 7'b0010010; //5 
4'b0110: Led <= 7'b0000010; //6 
4'b0111: Led <= 7'b1111000; //7 
4'b1000: Led <= 7'b0000000; //8 
4'b1001: Led <= 7'b0010000; //9 
4'b1010: Led <= 7'b0001110; //F 
default: Led <= 7'b1111111; //- 
endcase 
end 
endmodule 
+0

歡迎來到Stackoverflow。我希望你能得到一個快速的答案。 –

回答

1

原因代碼正在6個總週期,而不是4,因爲計算sumavg_reg每個週期需要一個週期,因此與在4個數據項中移位所需的4個週期相結合,您將獲得6個週期。

如果您觀察模擬過程中發生的情況,您會注意到4個項目的移位需要4個週期。然而,sum是,在邊緣的第四個值是data[0],前三項的總和,無論是在data[4]這可能都是零,如果你剛剛重置。並且avg_reg是從右邊緣移位兩次之後的sum的值。在下一個週期sum變得正確,然後avg_reg後的週期變得正確。

如果你想刪除這些週期,你可以簡單地有sum combinationally確定,並不真正需要avg_reg可言:

module avereger (data_in,Led1,Led2,Led3,clk,rst); 
    input clk, rst; 
    input [9:0] data_in; 
    output[6:0] Led1,Led2,Led3; 

    reg [9:0] data[3:0]; 
    wire [11:0] sum; 

    always @ (posedge clk) begin 
    if(rst==1'b1) begin //all values will be 0 
     data[0]<='0; 
     data[1]<='0; 
     data[2]<='0; 
     data[3]<='0;  
    end 
    else begin 
     data[3]<=data[2]; 
     data[2]<=data[1]; 
     data[1]<=data[0]; 
     data[0]<=data_in; 
    end 
    end 

    // Find sum combinationally 
    assign sum = data[0] + data[1] + data[2] + data[3]; 

    // Do >>2 here 
    decoder_driver BCD(.A(sum[11:2]), 
        .Led1(Led1), 
        .Led2(Led2), 
        .Led3(Led3)); 

endmodule 

側面說明,你應該避免寫你自己的敏感名單作爲其非常容易犯錯誤,請在您的其他模塊中使用always @(*)而不是always @(A)

+0

哦,我明白了,謝謝! – noobINverilog