0% found this document useful (0 votes)
169 views8 pages

HDL Description of Alu

This document contains HDL code for an ALU and state machine in both VHDL and Verilog. It first defines packages for operation codes and state types. It then provides a VHDL description of an ALU entity with a 3-bit lookahead adder and behavioral description for arithmetic operations. Corresponding Verilog code describes the ALU with structural assignments for addition and behavioral always block for other operations. The document also contains VHDL and Verilog code for a state machine with 4 states that changes state and output based on current state and input.

Uploaded by

Ravi Hatti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views8 pages

HDL Description of Alu

This document contains HDL code for an ALU and state machine in both VHDL and Verilog. It first defines packages for operation codes and state types. It then provides a VHDL description of an ALU entity with a 3-bit lookahead adder and behavioral description for arithmetic operations. Corresponding Verilog code describes the ALU with structural assignments for addition and behavioral always block for other operations. The document also contains VHDL and Verilog code for a state machine with 4 states that changes state and output based on current state and input.

Uploaded by

Ravi Hatti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Listing 7.7?

HDL Description of an ALUVHDL and Verilog VHDL ALU Description --Here we write the code for a package for user-defined --type and function. library ieee; use ieee.std_logic_1164.all; use IEEE.STD_LOGIC_1164.ALL,IEEE.NUMERIC_STD.ALL; package codes_Arithm is type op is (add, mul, divide, none); -- type op is for the operation codes for the ALU. The operations we -- need are: addition, multiplication, division, and no operation function TO_UNSIGN (b : integer) return unsigned; end; package body codes_Arithm is function TO_UNSIGN (b : integer) return unsigned is --The function converts integer numbers to unsigned. This function --can be omitted if it is included in a vendors package. The vendors -- package, if available, should be attached. variable temp : integer; variable bin : unsigned (5 downto 0); begin temp := b; for j in 0 to 5 loop if (temp MOD 2 = 1) then bin (j) := '1'; else bin (j) := '0'; end if; temp := temp/2; end loop; return bin; end TO_UNSIGN; end codes_Arithm; --Now we write the code for the ALU library IEEE; use IEEE.STD_LOGIC_1164.ALL; use ieee.numeric_std.all; use work.codes_arithm.all;

--The above use statement is to make the user--defined package "codes_arithm.all" visible to this module. entity ALU_mixed is port (a, b : in unsigned (2 downto 0); cin : in std_logic; opc : in op; z : out unsigned (5 downto 0)); --opc is of type "op"; type op is defined in the --user-defined package "codes_arithm" end ALU_mixed; architecture ALU_mixed of ALU_mixed is signal c0, c1 : std_logic; signal p, g : unsigned (2 downto 0); signal temp1 : unsigned (5 downto 0); begin --The following is a data flow-description of a 3-bit lookahead --adder. The sum is stored in the three least significant bits of --temp1. The carry out is stored in temp1(3). g(0) <= a(0) and b(0); g(1) <= a(1) and b(1); g(2) <= a(2) and b(2); p(0) <= a(0) or b(0); p(1) <= a(1) or b(1); p(2) <= a(2) or b(2); c0 <= g(0) or (p(0) and cin); c1 <= g(1) or (p(1) and g(0)) or (p(1) and p(0) and cin); temp1(3) <= g(2) or (p(2) and g(1)) or (p(2) and p(1) and g(0)) or (p(2) and p(1) and p(0) and cin); --temp1(3) is the final carryout of the adders temp1(0) <= (p(0) xor g(0)) xor cin; temp1(1) <= (p(1) xor g(1)) xor c0; temp1(2) <= (p(2) xor g(2)) xor c1; temp1 (5 downto 4) <= "00"; process (a, b, cin, opc, temp1) --The following is a behavioral description for the multiplication

--and division functions of the ALU. variable temp : unsigned (5 downto 0); variable a1, a2, a3 : integer; begin a1 := TO_INTEGER (a); a2 := TO_INTEGER (b); --The predefined function "TO_INTEGER" --converts unsigned to integer. --The function is a member of the VHDL package IEEE.numeric. case opc is when mul => a3 := a1 * a2; temp := TO_UNSIGN(a3); --The function "TO_UNSIGN" is a user-defined function --written in the user-defined package "codes_arithm." when divide => a3 := a1 / a2; temp := TO_UNSIGN(a3); when add => temp := temp1; when none => null; end case; z <= temp; end process; end ALU_mixed; Verilog ALU Description module ALU_mixed (a, b, cin, opc, z); parameter add = 0; parameter mul = 1; parameter divide = 2; parameter nop = 3; input [2:0] a, b; input cin; input [1:0] opc; output [5:0] z; reg [5:0] z; wire [5:0] temp1;

wire [2:0] g, p; wire c0, c1; // The following is data-flow description // for 3-bit lookahead adder assign g[0] = a[0] & b[0]; assign g[1] = a[1] & b[1]; assign g[2] = a[2] & b[2]; assign p[0] = a[0] | b[0]; assign p[1] = a[1] | b[1]; assign p[2] = a[2] | b[2]; assign c0 = g[0] | (p[0] & cin); assign c1 = g[1] | (p[1] & g[0]) | (p[1] & p[0] & cin); assign temp1[3] = g[2] | (p[2] & g[1]) | (p[2] & p[1] & g[0]) | (p[2] & p[1] & p[0] & cin); // temp1[3] is the final carryout of the adders assign temp1[0] = (p[0] ^ g[0]) ^ cin; assign temp1[1] = (p[1] ^ g[1]) ^ c0; assign temp1[2] = (p[2] ^ g[2]) ^ c1; assign temp1[5:4] = 2'b00; //The following is behavioral description always @ (a, b, cin, opc, temp1) begin case (opc) mul : z = a * b; add : z = temp1; divide : z = a / b; nop : z = z; endcase end endmodule

Listing 7.9?HDL Code for the State Machine in Figure 7.7 VHDL and Verilog VHDL State Machine Description library IEEE; use IEEE.STD_LOGIC_1164.all; --First we write a package that includes type tates. s package types is type op is (add, mul, divide, none); type states is (state0, state1, state2, state3); end; -- Now we use the package to write the code for the state machine. library IEEE; use IEEE.STD_LOGIC_1164.ALL; use work.types.all; entity state_machine is port (A, clk : in std_logic; pres_st : buffer states; Z : out std_logic); end state_machine; architecture st_behavioral of state_machine is begin FM : process (clk, pres_st, A) variable present : states := state0; begin if (clk = '1' and clk'event) then case pres_st is when state0 => if A ='1' then present := state1; Z <= '0'; else present := state0; Z <= '1'; end if; when state1 => if A ='1' then present := state2; Z <= '0'; else

present := state3; Z <= '0'; end if; when state2 => if A ='1' then present := state3; Z <= '1'; else present := state0; Z <= '0'; end if; when state3 => if A ='1' then present := state0; Z <= '0'; else present := state2; Z <= '0'; end if; end case; pres_st <= present; end if; end process FM; end st_behavioral; Verilog State Machine Description `define state0 2'b00 `define state1 2'b01 `define state2 2'b10 `define state3 2'b11 // We could have declared these states as parameters. // See Listing 7.7. module state_machine (A, clk, pres_st, Z); input A, clk; output [1:0] pres_st; output Z; reg Z; reg [1:0] present; reg [1:0] pres_st;

initial begin pres_st = 2'b00; end always @ (posedge clk) begin case (pres_st) `state0 : begin if (A == 1) begin present = `state1; Z = 1'b0; end else begin present = `state0; Z = 1'b1; end end `state1 : begin if (A == 1) begin present = `state2; Z = 1'b0; end else begin present = `state3; Z = 1'b0; end end `state2 : begin if (A == 1) begin present = `state3; Z = 1'b1;

end else begin present = `state0; Z = 1'b0; end end `state3 : begin if (A == 1) begin present = `state0; Z = 1'b0; end else begin present = `state2; Z = 1'b0; end end endcase pres_st = present; end endmodule

You might also like