Forum Discussion
LUT help!!!
I have wrote this code for implementing a neural network.What it does is to take some values(inputs) and some weights and computes the SUM(values*weigths).What i want to do is to check if the total sum(values*weights)< threshold and correct the weights in order to sum(values*weights)>=threshold.I think that i have to store the weights on a look up table and change the values in order to sum(values*weights)>=threshold.How i can do this?.My code is :
LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_ARITH.ALL; USE IEEE.STD_LOGIC_UNSIGNED.ALL; USE IEEE.STD_LOGIC_SIGNED.ALL; ENTITY ANN IS GENERIC ( m : INTEGER := 4; -- Number of inputs or weights b : INTEGER := 8); --Number of bits per input or weight PORT ( x1,x2,x3,x4: IN UNSIGNED(b-1 DOWNTO 0); w : IN UNSIGNED(b-1 DOWNTO 0); clk: IN STD_LOGIC; y : buffer UNSIGNED(2*b-1 DOWNTO 0); id : buffer bit); END ANN; ARCHITECTURE NEURAL OF ANN IS TYPE weights IS ARRAY (1 TO m) OF UNSIGNED(b-1 DOWNTO 0); TYPE inputs IS ARRAY (1 TO m) OF UNSIGNED(b-1 DOWNTO 0); BEGIN PROCESS(clk,w,x1,x2,x3,x4) VARIABLE weight : weights; VARIABLE input : inputs; VARIABLE prod,acc : UNSIGNED(2*b-1 DOWNTO 0); VARIABLE sub : UNSIGNED(2*b-1 DOWNTO 0); BEGIN IF (clk'EVENT AND CLK='1') THEN weight:=w&weight(1 TO m-1); END IF; input(1):=x1; input(2):=x2; input(3):=x3; input(4):=x4; acc:=(OTHERS=>'0'); FOR j IN 1 TO m LOOP prod:=input(j)*weight(j); acc:=acc+prod; END LOOP; y<=acc; END IF; END PROCESS; END NEURAL;70 Replies
- Altera_Forum
Honored Contributor
- Altera_Forum
Honored Contributor
That code is going to map to... nothing.
Your module doesn't even have an output, so it will be reduced to absolutely nothing. Ignoring that elephant in the room, your code doesn't make sense anyway. VHDL "variables" are, let's say, temporary things: they are created each time a "process" is triggered. Which means, for exameple, that "count" will always be "1" -- the "count := count + 1" statement will not carry on to the next clock cycle. Ignoring that second elephant in the room, your code makes 16 assignments in a given clock cycle. If that were to be mapped to memory blocks, it would mean 16 16x8 memory blocks. At the very least, 8 16x8 memory blocks, since the M4K/M9K blocks are dual ported. Damn, you need a good book on VHDL and digital electronics. - Altera_Forum
Honored Contributor
Here is an example code of inserting an 16x16 image.In every clock cycle i read the first 16 values,then other 16 etc.
--suppose we want to insert an 16x16 image.Each element is an integer 0 to 255 LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_ARITH.ALL; ENTITY input IS PORT( x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16 : IN INTEGER RANGE 0 TO 255; clk,load : IN STD_LOGIC); END input; ARCHITECTURE behavioral OF input IS TYPE row IS ARRAY(1 TO 16) OF INTEGER RANGE 0 TO 255; BEGIN PROCESS(clk) VARIABLE row1,row2,row3,row4,row5,row6,row7,row8,row9,row10,row11,row12,row13,row14,row15,row16 : row;--it could be done with the use of signal as well VARIABLE count : INTEGER:=(1); BEGIN IF(clk'EVENT AND clk='1') THEN IF(load='1') THEN IF(count<17) THEN row1(count):=x1; row2(count):=x2; row3(count):=x3; row4(count):=x4; row5(count):=x5; row6(count):=x6; row7(count):=x7; row8(count):=x8; row9(count):=x9; row10(count):=x10; row11(count):=x11; row12(count):=x12; row13(count):=x13; row14(count):=x14; row15(count):=x15; row16(count):=x16; count:=count+1; END IF; END IF; END IF; END PROCESS; END behavioral; There is a need for the values to be stored,because in specific clock cycles i want add/substract some values so i must have them somewhere stored. - Altera_Forum
Honored Contributor
A LUT is a look up table. input values are translated into other values, so effectivly it is a ROM.
- Altera_Forum
Honored Contributor
An array in a HDL code can have different meanings:
- it can describe a collection of individual register bits. The bits can be accesses individually without restrictions, but the "wiring" consumes a lot of FPGA resources - it can describe FPGA internal RAM, if the requirements for RAM inference are met. Internal RAM can have maximal two ports, so only two register entities can be accessed simultaneously. In some cases, multiple "array" elements can be arranged as one RAM word, limited of course by the available RAM word width respectively the total amount of RAM. Unfortunately, you didn't yet give a clear example of intended array usage, so it's difficult to decide about a possible solution. In the last code example you posted (on page three of this thread) the array is neither supplied with individual data nor needs to store it. - Altera_Forum
Honored Contributor
Ok i will try to explain you what i have done in order to understand and see if i can do something in my case.I want to insert an 20x20 image.The values of the image are the same type(integer) so i decide to insert it as an 20x20 array.The way i follow to do this is to declare 20 one dimensional arrays which would be the rows of the array.So i only need 20 inputs(each of them for one row).So i need 20 clock cycles to insert the entire image.What i did is to insert every clock cycle one value for each input and full the rows.For example in the first clock i will now the first element for each of the rows,in the second clock the second element of each row etc.I test it and works great.Am i doing something wrong?Is there any more efficient way to do this?Thanks
- Altera_Forum
Honored Contributor
Creating a component to do the operation won't save resources by itself -- the component will just be instantiated as many times as you need.
However, in many designs, arithmetic operations aren't required to produce a result every clock and the arithmetic logic can be "shared" among multiple needs. To do this, you need to draw a dependency diagram with the operations you need and then try to schedule them in order to reduce the number of add/sub that are performed in each clock cycle. - Altera_Forum
Honored Contributor
Another question i would like to ask is this : Say that i use add/substraction of 4 elements too many times in my code.It would be more efficient to make a component that does this add/substraction so i can save of alus?Thank you very much.
- Altera_Forum
Honored Contributor
Beware of arrays in HDL, though.
VHDL/Verilog allow you to use arrays in ways that can't actually be implemented in hardware or that would take too many logic resources to do so. You need to understand what resources you actually have and how to "map" your problem to them. In practice, you probably want to have them mapped over to the synchronous RAM blocks. This can be achieved either by using (properly) arrays, or by explicitly using RAM/ROM primitives. - Altera_Forum
Honored Contributor
Thanks for your quick response.My question maybe was stupid,but i want to briefly describe the behavior of a look up table.I was not looking for the definition of the word.From the answer you mentioned i suppose i was right about the use of arrays as look up tables.Thanks again