如何在'verilog'中创建一个数组(代码在里面)

时间:2017-01-01 19:17:29

标签: arrays verilog iverilog

我想创建一个名为'a'的数组,并用任何二进制数填充

和它的一维数组 我的代码必须以mips为单位(商店字) '是'我希望将它存储在's'中的值

谢谢,

module ALU(m,s,control,out,zeroflag,array);

input [31:0] m,s;
input [7:0] control;
output reg [31:0] out;
output reg zeroflag;
reg a [0:2]; // this is the array

a[2] = 4'b0000; // filled it..
a[1] = 4'b1001;
a[0] = 4'b0110;

always @(m,s,control,out,zeroflag,array)
begin
case(control)
8'h2B :  if(s==8'h0) a[0] = m;
         out = a[0];
  else if(s==8'h1) a[1] = m;
         out = a[1];
  else             a[2] = m;
         out = a[2];

endcase
end

always @(out)
begin
if(out==0)
zeroflag <= 1;
else
zeroflag <= 0;
end
endmodule

//////////////////////////////////////////

module test;
reg [31:0] m,s;
reg [7:0] control;
wire [31:0] outpt;
wire zeroflag;

ALU rtypeoperations(m,s,control,outpt,zeroflag);
initial
begin
m=32'b0000; s=8'h0; control=8'h2B; //stores m value in array[0] if s=8'h0, stores m value in array[1] if s=8'h1, stores m value in array[2] if s=8'h2,
end

initial
begin
$monitor("At time = %0t ,result = %b, zero flag = %b ",$time ,outpt,zeroflag);
end
endmodule

1 个答案:

答案 0 :(得分:0)

我在你的代码中发现了很多问题。这甚至可以编译吗?

首先,填充数组的操作需要在initial块内。

initial begin
  a[2] = 4'b0000; // filled it..
  a[1] = 4'b1001;
  a[0] = 4'b0110;
end

请注意,FPGA rtl允许initial块(FPGA可以为寄存器的初始值供电)和行为(测试平台)代码,但不适用于ASIC。

其次,您的always @(m,s,control,out,zeroflag,array)包含敏感度列表中的输出。它应该只是输入。

第三 - 你的代码需要一些时钟。现在一切都是按照它的方式编写的组合逻辑。

最后,学会正确缩进代码。以下是编码指南的一个建议:https://github.com/NetFPGA/netfpga/wiki/VerilogCodingGuidelines

相关问题