我在Verilog中编写16 * 4 RAM的代码。对于每个存储器的二进制单元,我使用的是SR触发器。最初,每个单元格分配1< bx。我使用整数循环以及存储使用变量k访问的内存索引的值。 Verilog不允许我为循环变量赋值k。
module memory(addr, read_data, rw, write_data, clk);
// read_data is the data read
// rw specifies read or write operation. 1 for read and 0 for write
// write data is the data to be written
// addr is the address to be accessed
task SRFlipFlop;
input d,r,s,clk; // d is the value initially stored
output q;
begin
case({s,r})
{1'b0,1'b0}: q=d;
{1'b0,1'b1}: q=1'b0;
{1'b1,1'b0}: q=1'b1;
{1'b1,1'b1}: q=1'bx;
endcase
end
endtask
task decoder; // a 4 to 16 line decoder
input [3:0] A;
input E;
output [15:0] D;
if (!E)
D <= 16'b0000000000000000;
else
begin
case (A)
4'b0000 : D <= 16'b0000000000000001;
4'b0001 : D <= 16'b0000000000000010;
4'b0010 : D <= 16'b0000000000000100;
4'b0011 : D <= 16'b0000000000001000;
4'b0100 : D <= 16'b0000000000010000;
4'b0101 : D <= 16'b0000000000100000;
4'b0110 : D <= 16'b0000000001000000;
4'b0111 : D <= 16'b0000000010000000;
4'b1000 : D <= 16'b0000000100000000;
4'b1001 : D <= 16'b0000001000000000;
4'b1010 : D <= 16'b0000010000000000;
4'b1011 : D <= 16'b0000100000000000;
4'b1100 : D <= 16'b0001000000000000;
4'b1101 : D <= 16'b0010000000000000;
4'b1110 : D <= 16'b0100000000000000;
4'b1111 : D <= 16'b1000000000000000;
endcase
end
endtask
output reg [3:0] read_data;
input [3:0] write_data, addr;
input rw, clk;
reg [3:0] memory [15:0];
reg [3:0] r [15:0];
reg [3:0] s [15:0];
reg [3:0] select [15:0];
reg [15:0] out;
integer k; // gives error
integer i,j;
initial
begin
for (i = 0; i <= 15; i=i+1)
begin
for (j = 0; j <= 3; j=j+1)
begin
memory[i][j] = 1'bx;
r[i][j] = 1'b0;
s[i][j] = 1'b0;
select[i][j] = 1'b0;
end
end
end
always @(rw or write_data or addr)
begin
k = 16;
decoder(addr, 1'b1, out);
for (i = 0; i <= 15; i=i+1)
begin
if (out[i] == 1'b1)
k = i;
end
for (i = 0; i <= 3; i=i+1)
begin
select[k][i] = 1'b1;
end
for (i = 0; i <= 3; i=i+1)
begin
s[k][i] = write_data[i] & !rw & select[k][i];
r[k][i] = !write_data[i] & !rw & select[k][i];
end
end
always @(posedge clk)
begin
if (k == 16)
begin
for(i = 0; i <= 3; i=i+1)
read_data[i] = 1'bx;
end
else
for(i = 0; i <= 3; i=i+1)
begin
SRFlipFlop(memory[k][i],r[k][i],s[k][i],clk,memory[k][i]);
read_data[i] = memory[k][i];
end
end
endmodule
当我在Xilinx中运行时,我得到以下输出。我怎样才能摆脱这个错误?
ERROR:Xst:528 - Multi-source in Unit <memory> on signal <_const0017>
答案 0 :(得分:1)
正如@ sharvil111在评论中指出的那样,你有很多违法行为。
always
块内实例化模块。使用generate
块,或单独实例化它们。always@(posedge clk)
(带if
语句)。如果您需要组合逻辑,请使用always@(*)
。<=
块中使用非阻止分配(always @(posedge clk)
)。使用阻止分配(=
)可能很危险。也可能存在其他错误,但这些错误主要是其中一些错误。