Init lab6

lab5top has been copied over for quick-reference
This commit is contained in:
Blizzard Finnegan 2023-10-17 11:56:18 -04:00
parent 52557ad8d4
commit ef484311a7
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
6 changed files with 480 additions and 0 deletions

43
lab6/src/alu.vhd Executable file
View file

@ -0,0 +1,43 @@
-------------------------------------------------------------------------------
-- Dr. Kaputa
-- arithmatic logic unit
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity alu is
port (
clk : in std_logic;
reset : in std_logic;
a : in std_logic_vector(7 downto 0);
b : in std_logic_vector(7 downto 0);
op : in std_logic_vector(1 downto 0); -- 00: add, 01: sub, 10: mult, 11: div
result : out std_logic_vector(7 downto 0)
);
end alu;
architecture beh of alu is
signal result_temp : std_logic_vector(15 downto 0):= (others => '0');
begin
process(clk,reset)
begin
if (reset = '1') then
result <= (others => '0');
elsif (clk'event and clk = '1') then
if (op = "00") then
result <= std_logic_vector(unsigned(a) + unsigned(b));
elsif (op = "01") then
result <= std_logic_vector(unsigned(a) - unsigned(b));
elsif (op = "10") then
result_temp <= std_logic_vector(unsigned(a) * unsigned(b));
result <= result_temp(7 downto 0);
elsif (op = "11") then
result_temp <= std_logic_vector(unsigned("00000000" & a) / unsigned("00000000" & b));
result <= result_temp(7 downto 0);
end if;
end if;
end process;
end beh;

View file

@ -0,0 +1,37 @@
-------------------------------------------------------------------------------
-- Blizzard Finnegan
-- S/R flip-flop
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity double_buffer is
generic (
bits : integer := 4
);
port(
d : in std_logic_vector(bits - 1 downto 0);
clk : in std_logic;
reset : in std_logic;
q : out std_logic_vector(bits - 1 downto 0)
);
end entity double_buffer;
architecture arch of double_buffer is
signal d_buf : std_logic_vector(bits - 1 downto 0);
begin
process(clk, reset)
begin
if(reset = '1') then
q <= (others => '0');
d_buf <= (others => '0');
elsif (clk'event and clk = '1') then
d_buf <= d;
q <= d_buf;
end if;
end process;
end arch;

76
lab6/src/double_dabble.vhd Executable file
View file

@ -0,0 +1,76 @@
-------------------------------------------------------------------------------
-- Dr. Kaputa
-- double_dabble demo
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity double_dabble is
port (
result_padded : in std_logic_vector(11 downto 0);
ones : out std_logic_vector(3 downto 0);
tens : out std_logic_vector(3 downto 0);
hundreds : out std_logic_vector(3 downto 0)
);
end double_dabble;
architecture beh of double_dabble is
begin
bcd1: process(result_padded)
-- temporary variable
variable temp : STD_LOGIC_VECTOR (11 downto 0);
-- variable to store the output BCD number
-- organized as follows
-- thousands = bcd(15 downto 12)
-- hundreds = bcd(11 downto 8)
-- tens = bcd(7 downto 4)
-- units = bcd(3 downto 0)
variable bcd : UNSIGNED (15 downto 0) := (others => '0');
-- by
-- https://en.wikipedia.org/wiki/Double_dabble
begin
-- zero the bcd variable
bcd := (others => '0');
-- read input into temp variable
temp(11 downto 0) := result_padded;
-- cycle 12 times as we have 12 input bits
-- this could be optimized, we dont need to check and add 3 for the
-- first 3 iterations as the number can never be >4
for i in 0 to 11 loop
if bcd(3 downto 0) > 4 then
bcd(3 downto 0) := bcd(3 downto 0) + 3;
end if;
if bcd(7 downto 4) > 4 then
bcd(7 downto 4) := bcd(7 downto 4) + 3;
end if;
if bcd(11 downto 8) > 4 then
bcd(11 downto 8) := bcd(11 downto 8) + 3;
end if;
-- thousands can't be >4 for a 12-bit input number
-- so don't need to do anything to upper 4 bits of bcd
-- shift bcd left by 1 bit, copy MSB of temp into LSB of bcd
bcd := bcd(14 downto 0) & temp(11);
-- shift temp left by 1 bit
temp := temp(10 downto 0) & '0';
end loop;
-- set outputs
ones <= STD_LOGIC_VECTOR(bcd(3 downto 0));
tens <= STD_LOGIC_VECTOR(bcd(7 downto 4));
hundreds <= STD_LOGIC_VECTOR(bcd(11 downto 8));
--thousands <= STD_LOGIC_VECTOR(bcd(15 downto 12));
end process bcd1;
end beh;

238
lab6/src/lab5top.vhd Executable file
View file

@ -0,0 +1,238 @@
-------------------------------------------------------------------------------
-- Blizzard Finnegan
-- 8-bit 2 function calc w/ double-dabble [top-level design]
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity top is
port(
clk : in std_logic;
reset : in std_logic;
input_switches : in std_logic_vector(7 downto 0);
state_cycle : in std_logic;
state_indicator : out std_logic_vector(3 downto 0);
hundreds_disp : out std_logic_vector(6 downto 0);
tens_disp : out std_logic_vector(6 downto 0);
ones_disp : out std_logic_vector(6 downto 0)
);
end top;
architecture behaviour of top is
component double_dabble is
port (
result_padded : in std_logic_vector(11 downto 0);
ones : out std_logic_vector(3 downto 0);
tens : out std_logic_vector(3 downto 0);
hundreds : out std_logic_vector(3 downto 0)
);
end component;
component generic_adder_beh is
generic (
bits : integer := 4
);
port (
a : in std_logic_vector(bits-1 downto 0);
b : in std_logic_vector(bits-1 downto 0);
cin : in std_logic;
sum : out std_logic_vector(bits-1 downto 0);
cout : out std_logic
);
end component;
component double_buffer is
generic (
bits : integer := 3
);
port(
d : in std_logic_vector(bits - 1 downto 0);
clk : in std_logic;
reset : in std_logic;
q : out std_logic_vector(bits - 1 downto 0)
);
end component;
component seven_seg is
port (
clk : in std_logic;
reset : in std_logic;
bcd : in std_logic_vector(3 downto 0);
seven_seg_out : out std_logic_vector(6 downto 0)
);
end component;
signal a_store, b_store, show_add, show_sub, add_carry, sub_carry, state_cycle_z, state_cycle_zz, state_cycle_zzz, state_cycle_en : std_logic;
signal count_out: std_logic_vector(1 downto 0);
signal ones, tens, hundreds : std_logic_vector(3 downto 0);
signal input_zz : std_logic_vector(7 downto 0);
signal a, b, sub_b, add_out, sub_out: std_logic_vector(8 downto 0);
signal padded_add, padded_sub, padded_input, mux_disp: std_logic_vector(11 downto 0);
begin
padded_add <= "000" & add_out;
padded_sub <= "000" & sub_out;
padded_input <= "0000" & input_zz;
sub_b <= not b;
state_indicator <= ( 0 => a_store,
1 => b_store,
2 => show_add,
3 => show_sub);
state_cycle_en <= state_cycle_zzz and (not state_cycle_zz);
a_buffer: process(clk,a_store,reset)
begin
if (reset = '1') then
a <= ( others => '0' );
elsif (rising_edge(clk)) then
if ( a_store = '1' ) then
a <= '0' & input_zz;
end if;
end if;
end process;
b_buffer: process(clk,b_store,reset)
begin
if (reset = '1') then
b <= ( others => '0' );
elsif (rising_edge(clk)) then
if ( b_store = '1' ) then
b <= '0' & input_zz;
end if;
end if;
end process;
input_buffer: double_buffer
generic map (
bits => 8
)
port map(
d => input_switches,
q => input_zz,
clk => clk,
reset => reset
);
adder: generic_adder_beh
generic map(
bits => 9
)
port map(
a => a,
b => b,
cin => '0',
sum => add_out,
cout => add_carry
);
subtractor: generic_adder_beh
generic map(
bits => 9
)
port map(
a => a,
b => sub_b,
cin => '1',
sum => sub_out,
cout => sub_carry
);
state_cycle_buffer: process(clk,state_cycle)
begin
if (reset = '1') then
state_cycle_z <= '0';
state_cycle_zz <= '0';
state_cycle_zzz <= '0';
elsif rising_edge(clk) then
state_cycle_z <= state_cycle;
state_cycle_zz <= state_cycle_z;
state_cycle_zzz <= state_cycle_zz;
end if;
end process;
counter: process(state_cycle_en)
begin
if(reset = '1') then
count_out <= (others => '0');
elsif (rising_edge(state_cycle_en)) then
count_out <= std_logic_vector(unsigned(count_out) + 1);
end if;
end process;
selector: process(count_out)
begin
case count_out is
when "00" =>
a_store <= '1';
b_store <= '0';
show_add <= '0';
show_sub <= '0';
when "01" =>
a_store <= '0';
b_store <= '1';
show_add <= '0';
show_sub <= '0';
when "10" =>
a_store <= '0';
b_store <= '0';
show_add <= '1';
show_sub <= '0';
when "11" =>
a_store <= '0';
b_store <= '0';
show_add <= '0';
show_sub <= '1';
when others =>
a_store <= '1';
b_store <= '0';
show_add <= '0';
show_sub <= '0';
end case;
end process;
mux: process(a_store, b_store, show_add, show_sub)
begin
if (a_store = '1' or b_store = '1') then
mux_disp <= padded_input;
elsif (show_add = '1') then
mux_disp <= padded_add;
elsif (show_sub = '1') then
mux_disp <= padded_sub;
end if;
end process;
bcd: double_dabble
port map(
result_padded => mux_disp,
ones => ones,
tens => tens,
hundreds => hundreds
);
show_hundreds: seven_seg
port map (
clk => clk,
reset => reset,
bcd => hundreds,
seven_seg_out => hundreds_disp
);
show_tens: seven_seg
port map (
clk => clk,
reset => reset,
bcd => tens,
seven_seg_out => tens_disp
);
show_ones: seven_seg
port map (
clk => clk,
reset => reset,
bcd => ones,
seven_seg_out => ones_disp
);
end behaviour;

38
lab6/src/memory.vhd Executable file
View file

@ -0,0 +1,38 @@
-------------------------------------------------------------------------------
-- Dr. Kaputa
-- memory
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity memory is
generic (addr_width : integer := 2;
data_width : integer := 4);
port (
clk : in std_logic;
we : in std_logic;
addr : in std_logic_vector(addr_width - 1 downto 0);
din : in std_logic_vector(data_width - 1 downto 0);
dout : out std_logic_vector(data_width - 1 downto 0)
);
end memory;
architecture beh of memory is
-- signal declarations
type ram_type is array ((2 ** addr_width -1) downto 0) of std_logic_vector(data_width -1 downto 0);
signal RAM : ram_type := (others => (others => '0'));
begin
process(clk)
begin
if (clk'event and clk = '1') then
if (we = '1') then
RAM(to_integer(unsigned(addr))) <= din;
end if;
dout <= RAM(to_integer(unsigned(addr)));
end if;
end process;
end beh;

48
lab6/src/seven_seg.vhd Normal file
View file

@ -0,0 +1,48 @@
-------------------------------------------------------------------------------
-- Blizzard Finnegan
-- Binary Coded Decimal to 7-Segment; single digit translation (0-F)
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity seven_seg is
port (
clk : in std_logic;
reset : in std_logic;
bcd : in std_logic_vector(3 downto 0);
seven_seg_out : out std_logic_vector(6 downto 0)
);
end seven_seg;
architecture beh of seven_seg is
begin
process(clk,reset)
begin
if (reset = '1') then
seven_seg_out <= "1111111";
elsif (clk'event and clk = '1') then
case bcd is
when "0000" => seven_seg_out <= "1000000";
when "0001" => seven_seg_out <= "1111001";
when "0010" => seven_seg_out <= "0100100";
when "0011" => seven_seg_out <= "0110000";
when "0100" => seven_seg_out <= "0011001";
when "0101" => seven_seg_out <= "0010010";
when "0110" => seven_seg_out <= "0000010";
when "0111" => seven_seg_out <= "1111000";
when "1000" => seven_seg_out <= "0000000";
when "1001" => seven_seg_out <= "0011000";
when "1010" => seven_seg_out <= "0001000";
when "1011" => seven_seg_out <= "0100001";
when "1100" => seven_seg_out <= "0110011";
when "1101" => seven_seg_out <= "0000011";
when "1110" => seven_seg_out <= "0000110";
when "1111" => seven_seg_out <= "0001110";
when others => seven_seg_out <= "1111111";
end case;
end if;
end process;
end beh;