Verilog array syntax

痴心易碎 提交于 2019-12-23 19:49:24

问题


I'm new to Verilog, and am having a lot of trouble with it. For example, I want to have an array with eight cells, each of which is 8 bits wide. The following doesn't work:

reg [7:0] transitionTable [0:7];
assign transitionTable[0] = 10;

neither does just doing transitionTable[0] = 10; or transitionTable[0] = 8'h10; Any ideas?

(In case it is not obvious and relevant: I want to make a finite state machine, and specify the state transitions in an array, since that seems easier than a massive case switch.)


回答1:


When using assign you should declare the array as a wire instead of areg.




回答2:


Since your goal is to design an FSM, there is no need to store the state values in an array. This is typically done using Verilog parameter's, a state register and a next_state with a case/endcase statement.

The following paper shows a complete example: FSM Fundamentals




回答3:


If this is targeted towards synthesis:

A little beyond what was answered above, there are standard FSM coding styles that you should adhere to so the tools can perform better optimization. As described in the Cummings paper, one-hot is usually best for FPGA devices and in fact ISE(with default settings) will ignore your encoding and implement whatever it thinks will best utilize the resources on the device. This almost invariably results in a one-hot encoded FSM regardless of the state encoding you chose, provided it recognizes your FSM.




回答4:


OK, so to answer your question, let's dig a little deeper into Verilog syntax.

First of all, to specify a range of bits, either do [MSB:LSB] or [LSB:MSB]. The standard is MSB:LSB but it is really up to you here, but try to be consistent.

Next, in array instantiation we have:

reg WIDTH reg_name NUMBER;

where WIDTH is the "size" of each element and NUMBER is the number of elements in the array.

So, you first want to do:

reg [7:0] transitionTable [7:0];

Then, to assign particular bytes (8 bits = 1 byte), do:

initial begin
    transitionTable[0] = 8'h10;
end

A good book to learn Verilog from is FPGA Prototyping By Verilog Examples by Pong P. Chu.



来源:https://stackoverflow.com/questions/4846898/verilog-array-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!