Hi guy, I'm getting stuck in a rut here. I'm very new to VHDL so please be paitent
How can I alter mid to allow for varied bit lengths? If I alter the value from (3 downto 0) it throws up errors of unexpected bit lengths! Any explanation would be much apreciated, thanks in Advance.
LibraryIEEE;UseIEEE.STD_LOGIC_1164.ALL;UseIEEE.NUMERIC_STD.ALL;useieee.std_logic_unsigned.all;Entity ALU isPort( A, B :inSTD_LOGIC_vector(3downto0);
Sel :inSTD_LOGIC_vector(3downto0);
Z:outSTD_LOGIC_vector(3downto0));End ALU;Architecture job of ALU issignal Mid:STD_LOGIC_vector(3downto0);BeginProcess(sel,A,B)Beginif(sel="000")then Mid <= A or B;elsif(sel="001")then Mid <= A and B;elsif(sel="010")then Mid <= A nor B;elsif(sel="011")then Mid <= A nand B;elsif(sel="100")then Mid <= A xor B;elsif(sel="101")then Mid <= A nand B;elsif(sel="110")then Mid <= A xnor B;else Mid <=std_logic_vector(unsigned(A)+unsigned(B));endif;Endprocess;Process(sel,mid)Beginif(sel="111")and(mid <"00101")then Z <="1000";elsif(sel="111")and(mid <"01001")then Z <="0100";elsif(sel="111")and(mid <"01101")then Z <="0010";elsif(sel="111")and(mid <"10001")then Z <="0001";else Z <="0000";endif;Endprocess;End job;
It's not clear what you mean with "varied bit length" and why it should be used in the VHDL example.
The addition
Code:
std_logic_vector(unsigned(A) + unsigned(B));
has a 4-bit result width according to VHDL rules.
There won't be a problem however to generate a carry in the addition and get a 5 bits wide result. One summand has to be extended to 5-bit to do so, e.g. by a '0' & concatanation.
The purpose of the code can't be well determined because the result mid is presently discarded for sel /= "111".
TrickyDicky - Would that not just alter the length of the input port bits?
FvM - I confused as the signal sel is declared as a 4 bit vector which is derived from A, B. If these are added together you are saying I will still only get a 4-bit result currently? In which case using a syntax something along the lines of :
else Mid <= std_logic_vector(unsigned("0" & A) + unsigned("0" & B));
The purpose is for an assignment so it's more of an exercise than functionality.