Skip to content

Instantly share code, notes, and snippets.

@hamzamuric
Last active March 8, 2022 19:21
Show Gist options
  • Save hamzamuric/0e7be6d854682bca464c04fbb362ad82 to your computer and use it in GitHub Desktop.
Save hamzamuric/0e7be6d854682bca464c04fbb362ad82 to your computer and use it in GitHub Desktop.
-- semafor
-- A ---> B ---> C
-- ^ |
-- | |
-- +-------------+
library IEEE;
use IEEE.std_logic_1164.all;
type Stanje is (CRVENO, ZUTO, ZELENO);
entity semafor is
port (
clk: in std_logic;
svetla: out std_logic_vector(2 downto 0));
end semafor;
architecture semafor_arch of semafor is
signal stanje, sledece_stanje: Stanje;
begin
process (clk) begin
if rising_edge(clk) then
stanje <= sledece_stanje;
end if;
end process;
process (stanje) begin
case stanje is
CRVENO =>
svetla <= "100";
sledece_stanje <= ZUTO;
ZUTO =>
svetla <= "010";
sledece_stanje <= ZELENO;
ZELENO =>
svetla <= "001";
sledece_stanje <= CRVENO;
end case;
end process;
end semafor_arch;
-- semafor sa sinhronim resetom
library IEEE;
use IEEE.std_logic_1164.all;
type Stanje is (CRVENO, ZUTO, ZELENO);
entity semafor is
port (
clk: in std_logic;
reset: in std_logic;
svetla: out std_logic_vector(2 downto 0));
end semafor;
architecture semafor_arch of semafor is
signal stanje, sledece_stanje: Stanje;
begin
process (clk) begin
if rising_edge(clk) then
if reset='1' then
stanje <= CRVENO;
else
stanje <= sledece_stanje;
end if;
end if;
end process;
process (stanje) begin
case stanje is
CRVENO =>
svetla <= "100";
sledece_stanje <= ZUTO;
ZUTO =>
svetla <= "010";
sledece_stanje <= ZELENO;
ZELENO =>
svetla <= "001";
sledece_stanje <= CRVENO;
end case;
end process;
end semafor_arch;
-- semafor sa asinhronim resetom
library IEEE;
use IEEE.std_logic_1164.all;
type Stanje is (CRVENO, ZUTO, ZELENO);
entity semafor is
port (
clk: in std_logic;
reset: in std_logic;
svetla: out std_logic_vector(2 downto 0));
end semafor;
architecture semafor_arch of semafor is
signal stanje, sledece_stanje: Stanje;
begin
process (clk, reset) begin
if reset='1' then
stanje <= CRVENO;
elsif rising_edge(clk) then
stanje <= sledece_stanje;
end if;
end process;
process (stanje) begin
case stanje is
CRVENO =>
svetla <= "100";
sledece_stanje <= ZUTO;
ZUTO =>
svetla <= "010";
sledece_stanje <= ZELENO;
ZELENO =>
svetla <= "001";
sledece_stanje <= CRVENO;
end case;
end process;
end semafor_arch;
-- grejanje
-- na svaki klok grejanje proveri je li termometar
-- dao signal da je temperatura odgovarajuca.
-- ako je temperatura odgovarajuca, grejanje se gasi.
-- ako je grejanje vec ugaseno a termometar daje signal
-- da temperatura nije zadovoljavajuca, grejanje se pali
library IEEE;
use IEEE.std_logic_1164.all;
type Stanje is (UGASENO, UPALJENO);
entity grejanje is
port (
clk: in std_logic;
reset: in std_logic; -- trenutno gasi grejanje bez obzira na temperaturu
termometar: in std_logic; -- '1' ako je temperatura zadovoljavajuca, '0' ako nije
greje: out std_logic); -- '1' ako je grejanje upaljeno, '0' ako nije
end grejanje;
architecture grejanje_arch of grejanje is
signal stanje, sledece_stanje: Stanje;
begin
process (clk, reset) begin
if reset='1' then
stanje <= UGASENO;
greje <= '0';
elsif rising_edge(clk) then
stanje <= sledece_stanje;
end if;
end process;
process (stanje) begin
case stanje of
UGASENO =>
greje <= '0';
if termometar='0' then
sledece_stanje <= UPALJENO;
end if;
UPALJENO =>
greje <= '1';
if termometar='1' then
sledece_stanje <= UGASENO;
end if;
end case;
end process;
end grejanje_arch;
-- igrica
-- treba da uzmemo mac i onda da ubijemo zmaja.
-- ako odemo kod zmaja bez maca, gotovi smo, pojede nas.
-- krecemo se tako sto aktiviramo signale 'gore', 'dole', 'levo' i 'desno'.
-- mozemo da se krecemo samo na povezana polja.
--
-- MAPA:
-- +------+
-- | zmaj |
-- +------+
-- ||
-- ||
-- +-----+ +------+
-- | mac |==========| |
-- +-----+ +------+
-- ||
-- ||
-- +------+
-- | mi |
-- +------+
library IEEE;
use IEEE.std_logic_1164.all;
type Stanje is (POCETNO, PRAZNO, MAC, ZMAJ);
entity igrica is
port (
clk: in std_logic;
reset: in std_logic;
gore, dole, levo, desno: in std_logic;
win: out std_logic; -- '1' ako smo ubili zmaja
game_over: out std_logic); -- '1' ako nas je zmaj pojeo
end igrica;
architecture gameplay of igrica is
signal stanje, sledece_stanje: Stanje;
signal imamo_mac: std_logic := '0';
begin
process (clk, reset) begin
if reset='1' then
stanje <= POCETNO;
imamo_mac <= '0';
win <= '0';
game_over <= '0';
elsif rising_edge(clk) then
stanje <= sledece_stanje;
end if;
end process;
process (stanje) begin
case stanje is
POCETNO =>
win <= '0';
game_over <= '0';
if gore='1' then
sledece_stanje <= PRAZNO;
end if;
PRAZNO =>
win <= '0';
game_over <= '0';
if gore='1' then
sledece_stanje <= ZMAJ;
elsif levo='1' then
sledece_stanje <= MAC;
end if;
MAC =>
win <= '0';
game_over <= '0';
if desno='1' then
sledece_stanje <= PRAZNO;
end if;
ZMAJ =>
game_over <= '1';
if imamo_mac='1' then
win <= '1';
elsif imamo_mac='0' then
win <= '0';
end if;
end case;
end process;
end gameplay;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment