------------------------------------------------------------------------
-- IAY0105 - Digitaalsüsteemid. Kodutöö #1. Vigane näidislahendus.
------------------------------------------------------------------------
-- (C) Peeter Ellervee - 2015 - Tallinn
------------------------------------------------------------------------
-- Lähteülesanne
--  abcd klmn
--  0000 1-00  [1000]
--  0001 01-0  [0100]
--  0010 11-1  [1111]
--  0011 0-01  [0101]
--  0100 1110  [1110]
--  0101 1010  [1010]
--  0110 -111  [1111]
--  0111 01-0  [0110]
--  1000 0011  [0011]
--  1001 -10-  [0100]
--  1010 -0-1  [0011]
--  1011 1001  [1001]
--  1100 11-0  [1110]
--  1101 0-10  [0010]
--  1110 -000  [0000]
--  1111 1011  [1011]
------------------------------------------------------------------------
-- Minimeerimise tulemus (espresso #2 & Karnaugh kaart)
--  abcd klmn
--  0--0 1000
--  0-10 0011
--  0-1- 0100
--  -001 0100
--  1-11 1001
--  010- 1000
--  -100 1110
--  -1-1 0010
--  10-0 0011
--  -01- 0001
------------------------------------------------------------------------

entity test is
end test;

architecture bench of test is
  signal a, b, c, d, k_err, l_err, m_err, n_err: bit;
  signal k0, k2, l0, l2, m0, m2, n0, n2: bit;
  signal ai, bi, ci, di: bit;
  signal t0i, t2i, t4i, t6x, t6i, t7i, t9i: bit;
  signal t51, t52, t53i, t54, t1t8i, t3t6i, t5t6i: bit;
begin
  -- Sisendsignaalid (sammuga 10 ns)
  a <= '0' after 0 ns, '1' after 80 ns, '0' after 160 ns;
  b <= '0' after 0 ns, '1' after 40 ns, '0' after 80 ns, '1' after 120 ns;
  c <= '0' after 0 ns, '1' after 20 ns, '0' after 40 ns, '1' after 60 ns,
       '0' after 80 ns, '1' after 100 ns, '0' after 120 ns, '1' after 140 ns;
  d <= '0' after 0 ns, '1' after 10 ns, '0' after 20 ns, '1' after 30 ns,
       '0' after 40 ns, '1' after 50 ns, '0' after 60 ns, '1' after 70 ns,
       '0' after 80 ns, '1' after 90 ns, '0' after 100 ns, '1' after 110 ns,
       '0' after 120 ns, '1' after 130 ns, '0' after 140 ns, '1' after 150 ns;

  -- Minimeerimise tulemus
  k0 <= ((not a) and (not d)) or (a and c and d) or
        ((not a) and b and (not c)) or (b and (not c) and (not d));
  l0 <= ((not a) and c) or ((not b) and (not c) and d) or
        (b and (not c) and (not d));
  m0 <= ((not a) and c and (not d)) or (b and (not c) and (not d)) or
        (b and d) or (a and (not b) and (not d));
  n0 <= ((not a) and c and (not d)) or (a and c and d) or
        (a and (not b) and (not d)) or ((not b) and c);

  -- Optimeerimise tulemus (lähtudes vahevariandist #2)
  ai <= not (a and a);          bi <= not (b and b);
  ci <= not (c and c);          di <= not (d and d);

  t0i <= a or d;                t2i <= not (ai and c);
  t4i <= not (a and c and d);   t6x <= not (c or d);
  t6i <= not (b and t6x);       t7i <= not (b and d);
  t9i <= not (bi and c);

  t51 <= not (a and d);         t52 <= b xor d;
  t53i <= not (a and bi);       t54 <= t2i and t53i; -- error

  t1t8i <= not (di and t54);    t3t6i <= not (t52 and ci);
  t5t6i <= not (t51 and b and ci);

  k2 <= not (t0i and t4i and t5t6i);
  l2 <= not (t2i and t3t6i);
  m2 <= not (t1t8i and t6i and t7i);
  n2 <= not (t1t8i and t4i and t9i);

  -- Kontrollimine...
  k_err <= k0 xor k2;  l_err <= l0 xor l2;
  m_err <= m0 xor m2;  n_err <= n0 xor n2;

end bench;
