Facebook
From Fiery Meerkat, 7 Years ago, written in VHDL.
Embed
Download Paste or View Raw
Hits: 425
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. entity alu is
  5. port (         
  6.                         A : in signed(15 downto 0);
  7.                         B : in signed(15 downto 0);
  8.                         Salu : in bit_vector (3 downto 0);
  9.                         LDF : in bit;
  10.                         clk : in bit;  
  11.                         Y : out signed (15 downto 0);
  12.                         C,Z,S,P : out std_logic
  13.                 );
  14. end entity;
  15. architecture rtl of alu is
  16.    begin
  17.                 process (Salu, A, B, clk)
  18.                         variable res,AA,BB,CC: signed (16 downto 0);
  19.                         variable CF,ZF,SF,PF : std_logic;
  20.                         variable i : integer range 0 to 15;
  21.                         variable pomoc: boolean;
  22.                         variable ilosc: integer range 15 downto 0:=0;
  23.                         variable temp: integer range 99 downto 0;
  24.                         variable temp1: integer range 9 downto 0;
  25.                                 begin
  26.                                         AA(16) := A(15);
  27.                                         AA(15 downto 0) := A;
  28.                                         BB(16) := B(15);
  29.                                         BB(15 downto 0) := B;
  30.                                         CC(0) := CF;
  31.                                         CC(16 downto 1) := "0000000000000000";
  32. case Salu is  
  33. when "0000" => res := AA;                                                                                                                       --MOV arg1,arg2
  34. when "0001" => res := AA+BB;                                                                                                                    --ADD arg1,arg2
  35. when "0010" => res := AA-BB;                                                                                                                    --SUB arg1,arg2
  36. when "0011" => res := AA;                                                                                                               --LD arg1, arg2
  37. when "0100" => res(3 downto 0):= BB(7 downto 4);                                                                --LDHI arg1, arg2
  38. when "0101" => res(3 downto 0):= BB(3 downto 0);                                                                --LDLO arg1, arg2
  39. when "0110" => res := AA or BB;                                                                                                         --OR arg1,arg2
  40. when "0111" => if(AA = BB) then
  41.                                 res:=(others=>'0');
  42.                         else    
  43.                                 res :=(0=>'1',others=>'0');
  44.                         end if;--CMPNE arg1,arg2,wynik
  45. when "1000" => --res(7 downto 0):=  ((AA(7 downto 4)*10)+(AA(3 downto 0))) ;   
  46.                                         --res(15 downto 8) := "00000000";       --BCDtoB arg1
  47.                                         temp:=to_integer((AA(7 downto 4)));
  48.                                         temp:= temp*10;
  49.                                         temp1:=to_integer(AA(3 downto 0));
  50.                                         temp:=temp + temp1;
  51.                                         res:=to_signed(temp,17);
  52.                                        
  53.                                        
  54. when others => res:= null;
  55. end case;
  56.  
  57. Y <= res(15 downto 0);
  58. Z <= ZF; -- Flaga zero
  59. S <= SF; -- Flaga ujemnosci
  60. C <= CF; -- Flaga przeniesienia
  61. P <= PF; -- Flaga parzystosci
  62.  
  63. if (clk'event and clk='1') then
  64.        
  65.         if (LDF='1') then
  66.         ilosc:=0;
  67.                 if (res = "00000000000000000") then ZF:='1';
  68.                         else ZF:='0';
  69.                 end if;
  70.                 if (res(15)='1') then SF:='1';
  71.                         else SF:='0';
  72.                 end if;
  73. CF := res(16)  xor res(15);
  74. for i in 0 to 15 loop
  75.                 if(res(i)  = '1') then
  76.                         ilosc:=ilosc + 1;
  77.                 end if;
  78.                 end loop;
  79.                        
  80.                 if(ilosc mod 2 = 0) then       
  81.                         PF:='1';
  82.                 else PF:='0';
  83.                 end if;
  84.         end if;
  85. end if;
  86.  
  87. end process;
  88.  end rtl;
  89.  
  90.  library ieee;
  91. use ieee.std_logic_1164.all;
  92. use ieee.numeric_std.all;
  93.  
  94. entity Rejestry is
  95.         port(
  96.                         clk : in std_logic;
  97.                         DI : in signed (15 downto 0);
  98.                         BA : in signed (15 downto 0);
  99.                         Sbb : in signed (3 downto 0);
  100.                         Sbc : in signed (3 downto 0);
  101.                         Sba : in signed (3 downto 0);
  102.                         Sid : in signed (2 downto 0);
  103.                         Sa : in signed (2 downto 0);
  104.                         BB : out signed (15 downto 0);
  105.                         BC : out signed (15 downto 0);
  106.                         ADR : out signed (15 downto 0);
  107.                         IRout : out signed (15 downto 0)
  108.                         );
  109. end entity;
  110.  
  111. architecture rtl of Rejestry is
  112. begin
  113.         process (clk, Sbb, Sbc, Sba, Sid, Sa, DI)
  114.         variable IR, TMP, A, B, C, ES,DS,CS: signed (15 downto 0);     
  115.         variable AD, PC, SP, ATMP : signed (15 downto 0); -- zapytac ile bitow
  116.         variable AP1,AP2 : signed (7 downto 0);
  117. begin
  118.         if (clk'event and clk='1') then
  119. case Sid is
  120.                 when "001" => PC := PC + 1;
  121.                 when "010" => SP := SP + 1;
  122.                 when "011" => AD := AD + 1;
  123.                 --when "100" => PC := PC - 1;
  124.                 when "101" => SP := SP - 1;
  125.                 when "110" => AD := AD - 1;
  126.                
  127.                
  128.                 when others =>null;
  129.                
  130.                 end case;
  131.                 case Sba is
  132.                 when "0000" => IR   := BA;
  133.                 when "0001" => TMP  := BA;
  134.                 when "0010" => A    := BA;
  135.                 when "0011" => B    := BA;
  136.                 when "0100" => C    := BA;
  137.                 when "0101" => ES   := BA;
  138.                 when "0110" => DS   := BA;
  139.                 when "0111" => CS   := BA;
  140.                 when "1000" => AD   := BA;
  141.                 when "1001" => PC   := BA;
  142.                 when "1010" => SP   := BA;
  143.                 when "1011" => ATMP := BA;
  144.                 when "1100" => AP1  := BA(7 downto 0);  --tutaj trzeba zapytac
  145.                 when "1101" => AP2  := BA(15 downto 8); -- zapytac
  146.                
  147.                 end case;
  148.                
  149.                 end if;
  150.                 case Sbb is
  151.                 when "0000" => BB <= DI;
  152.                 when "0001" => BB <= TMP;
  153.                 when "0010" => BB <= A;
  154.                 when "0011" => BB <= B;
  155.                 when "0100" => BB <= C;
  156.                 when "0101" => BB <= ES;
  157.                 when "0110" => BB <= DS;
  158.                 when "0111" => BB <= CS;
  159.                 when "1000" => BB <= AD;
  160.                 when "1001" => BB <= PC;
  161.                 when "1010" => BB <= SP;
  162.                 when "1011" => BB <= ATMP;
  163.                 when "1100" => BB(7 downto 0) <= AP1;  --zapytac
  164.                 when "1101" => BB(7 downto 0) <= AP2;  --zapytac
  165.                 end case;
  166.                
  167.                 case Sbc is
  168.                 when "0000" => BC<= DI;
  169.                 when "0001" => BC <= TMP;
  170.                 when "0010" => BC <= A;
  171.                 when "0011" => BC <= B;
  172.                 when "0100" => BC <= C;
  173.                 when "0101" => BC <= ES;
  174.                 when "0110" => BC <= DS;
  175.                 when "0111" => BC <= CS;
  176.                 when "1000" => BC <= AD;
  177.                 when "1001" => BC <= PC;
  178.                 when "1010" => BC <= SP;
  179.                 when "1011" => BC <= ATMP;
  180.                 when "1100" => BC(7 downto 0) <= AP1;  --zapytac
  181.                 when "1101" => BC(7 downto 0) <= AP2;  --zapytac
  182.                 end case;
  183.                
  184.                 case Sa is
  185.                 when "000" => ADR <= AD;
  186.                 when "001" => ADR <= PC;
  187.                 when "010" => ADR <= SP;
  188.                 when "011" => ADR <= ATMP;
  189.                 when "100" => ADR <= ES;
  190.                 when "101" => ADR <= DS;
  191.                 when "110" => ADR <= CS;
  192.  
  193.                 end case;
  194.        
  195.         IRout <= IR;
  196.         end process;
  197. end rtl;
  198.  
  199. --wspolpraca z pamiecia
  200. --nie do konca zrobiona
  201.  
  202. library ieee;
  203. use ieee.std_logic_1164.all;
  204. use ieee.numeric_std.all;
  205. entity busint is
  206. port
  207. (
  208.         ADR : in signed(31 downto 0);
  209.         DO : in signed(15 downto 0);
  210.         SEG : in signed(9 downto 0);
  211.         OFS :in signed(5 downto 0);
  212.         Smar, Smbr, WRin, RDin : in bit;
  213.         AD : out signed (31 downto 0);
  214.         D : inout signed (15 downto 0);
  215.         DI : out signed(15 downto 0);
  216.         QWE : in bit;
  217.         WR, RD : out bit
  218. );
  219. end entity;
  220. architecture rtl of busint is
  221. begin
  222.         process(Smar, ADR, Smbr, DO, D, WRin, RDin)
  223.         variable MBRin, MBRout: signed(15 downto 0);
  224.         variable MAR : signed(31 downto 0);
  225. begin
  226.        
  227.         if(Smar='1')  then
  228.         if(QWE = '1')then MAR(15 downto 6) := SEG;
  229.                                                 MAR(5 downto 0):=OFS;
  230.                                         else MAR := ADR;
  231.                                         end if;
  232.                                         end if;
  233.         if(Smbr='1') then MBRout := DO; end if;
  234.         if (RDin='1') then MBRin := D; end if;
  235.         if (WRin='1') then D <= MBRout;
  236.         else D <= "ZZZZZZZZZZZZZZZZ";
  237. end if;
  238.         DI <= MBRin;
  239.         AD <= MAR;
  240.         WR <= WRin;
  241.         RD <= RDin;
  242. end process;
  243. end rtl;
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254. library ieee;
  255. use ieee.std_logic_1164.all;
  256. use ieee.numeric_std.all;
  257.  
  258. entity control is
  259. port(
  260.                         clk : in std_logic;
  261.                         IR : in signed(15 downto 0);
  262.                         reset, C, Z, S, INT : in std_logic;
  263.                         Salu, Sbb, Sbc, Sba : out bit_vector(3 downto 0);
  264.                         Sid : out bit_vector(2 downto 0);
  265.                         Sa : out bit_vector(2 downto 0);
  266.                         LDF, Smar, Smbr, WR, RD,INTA, MIO : out bit
  267. );
  268. end entity;
  269. architecture rtl of control is
  270. type state_type is (fetch, dec, wait_1, call_1, call_2, call_3, call_4, ret_1, ret_2, ret_3, push, pop_1, pop_2, neg, inc, dec_1, not_1, shr,
  271. shl, mov_r, mov_rm, add, sub, cmp, and_1, or_1, xor_1, in_r, out_IO, jump_1, jump_2, long_jump, r_2_16, mov_a1_a2, bcdtoB, mov, add_a1_a2, sub_a1_a2, ld, ldhi,
  272. ldlo, or_2, cmpne, r_2_32, m9);
  273. signal state : state_type;
  274. begin
  275.         process (clk, reset)
  276.         begin
  277.                 if reset = '1' then
  278.                         state <= fetch;
  279.                 elsif (clk'event and clk='1') then
  280.                         case state is
  281.                                 when fetch=>
  282.                                         state <= dec;
  283.                                 when dec=>
  284.                                         case IR(15 downto 13) is
  285.                                                 when "000" =>
  286.                                                
  287.                                                         case IR(12 downto 11) is
  288.                                                                 when "00" =>
  289.                                                                         if(INT='0') then state <= fetch;
  290.                                                                         else state <= m9;
  291.                                                                         end if;
  292.                                                                 when "01" => state <= wait_1;
  293.                                                                 when "10" => state <= call_1;
  294.                                                                 when "11" => state <= ret_1;
  295.                                                                 when others => state <= fetch;
  296.                                                         end case;
  297.                                                 when "001" =>
  298.                                                         case IR(12 downto 8) is
  299.                                                                 when "00000" => state <= push;
  300.                                                                 when "00001" => state <= pop_1;
  301.                                                                 when "00010" => state <= neg;
  302.                                                                 when "00011" => state <= inc;
  303.                                                                 when "00100" => state <= dec_1;
  304.                                                                 when "00101" => state <= not_1;
  305.                                                                 when "00110" => state <= shr;
  306.                                                                 when "00111" => state <= shl;
  307.                                                                 when "01000" => state <= mov_r;
  308.                                                                 when "01001" => state <= mov_rm;
  309.                                                                 when "01010" => state <= add;
  310.                                                                 when "01011" => state <= sub;
  311.                                                                 when "01100" => state <= cmp;
  312.                                                                 when "01101" => state <= and_1;
  313.                                                                 when "01110" => state <= or_1;
  314.                                                                 when "01111" => state <= xor_1;
  315.                                                                 when "10000" => state <= in_r;
  316.                                                                 when "10001" => state <= out_IO;
  317.                                                                 when "10010" => state <= bcdtoB;
  318.                                                                 when others => state <= fetch;
  319.                                                         end case;      
  320.                                                  
  321.                                                 when "010" => state <= jump_1;
  322.                                                 when "011" => state <= jump_2;
  323.                                                        
  324.                                        
  325.                                         when "100" =>
  326.                             case IR(12 downto 8) is
  327.                                                                         when "00000" => state <= mov;
  328.                                                                         when "00001" => state <= add_a1_a2;
  329.                                                                         when "00010" => state <= sub_a1_a2;
  330.                                                                         when "00011" => state <= ld;
  331.                                                                         when "00100" => state <= ldhi;
  332.                                                                         when "00101" => state <= ldlo;
  333.                                                                         when "00110" => state <= or_2;
  334.                                                                         when "00111" => state <= cmpne;
  335.                                                                         when others => state <= fetch;
  336.                                                                         end case;
  337.        
  338.                                                 when "101" => state <= r_2_32;
  339.                                                 when others => state <= fetch;
  340.                                         end case;
  341.                                 when wait_1=>
  342.                                         if INT = '1' then state <= m9;
  343.                                         else state <= wait_1;
  344.                                         end if;
  345.                                 when call_1 => state <= call_2;
  346.                                 when call_2 => state <= call_3;
  347.                                 when call_3 => state <= call_4;
  348.                                 when call_4 =>
  349.                                         if INT = '1' then state <= m9;
  350.                                         else state <= fetch;
  351.                                         end if;
  352.                         when others => state <= fetch;
  353.                         end case;
  354.                         end if;
  355.         end process;
  356.  
  357.         process (state)
  358.         begin
  359.         case state is
  360.                 when fetch =>
  361.                         Sa <= "001"; Sbb <= "0000"; Sba <= "0000"; Sid <="001"; Sbc <="0000"; MIO <='1';
  362.                         Smar <='1'; Smbr <= '0'; WR <='0'; RD <='1'; Salu <="0000"; LDF <='0'; INTA <='0';
  363.  
  364.                         when dec =>
  365.                         Sa <= "000"; Sbb <= "0000"; Sba <= "0000"; Sid <="000"; Sbc <="0000"; MIO <='1';
  366.                         Smar <='0'; Smbr <= '0'; WR <='0'; RD <='0'; Salu <="0000"; LDF <='0'; INTA <='0';
  367.  
  368.                         when wait_1 =>
  369.                         Sa <= "000"; Sbb <= "0000"; Sba <= "0000"; Sid <="000"; Sbc <="0000"; MIO <='1';
  370.                         Smar <='0'; Smbr <= '0'; WR <='0'; RD <='0'; Salu <="0000"; LDF <='0'; INTA <='0';
  371.  
  372.                         when call_1 =>
  373.                         Sa <= "010"; Sbb <= "1010"; Sba <= "0000"; Sid <="011"; Sbc <="0000"; MIO <='1';
  374.                         Smar <='1'; Smbr <= '1'; WR <='1'; RD <='0'; Salu <="0000"; LDF <='0'; INTA <='0';
  375.                        
  376.                        
  377.                         when mov_a1_a2 =>
  378.                         Sa <= "001"; Sbb<=to_bitvector(std_logic_vector(IR(7 downto 4))) ; Sba <= "0001"; Sid <="001"; Sbc <=to_bitvector(std_logic_vector(IR(3 downto 0))); MIO <='1';
  379.                         Smar <='0'; Smbr <= '0'; WR <='1'; RD <='0'; Salu <="0000"; LDF <='1'; INTA <='0';
  380.                        
  381.                        
  382.                         when add_a1_a2 =>
  383.                         Sa <= "001"; Sbb<=to_bitvector(std_logic_vector(IR(7 downto 4))) ; Sba <= "0001"; Sid <="001"; Sbc <=to_bitvector(std_logic_vector(IR(3 downto 0))); MIO <='1';
  384.                         Smar <='0'; Smbr <= '0'; WR <='1'; RD <='0'; Salu <="0001"; LDF <='1'; INTA <='0';
  385.        
  386.                         when sub_a1_a2 =>
  387.                         Sa <= "001"; Sbb<=to_bitvector(std_logic_vector(IR(7 downto 4))) ; Sba <= "0001"; Sid <="001"; Sbc <=to_bitvector(std_logic_vector(IR(3 downto 0))); MIO <='1';
  388.                         Smar <='0'; Smbr <= '0'; WR <='1'; RD <='0'; Salu <="0010"; LDF <='1'; INTA <='0';
  389.                        
  390.                        
  391.                         when ld =>
  392.                         Sa <= "001"; Sbb<=to_bitvector(std_logic_vector(IR(7 downto 4))) ; Sba <= "0001"; Sid <="001"; Sbc <=to_bitvector(std_logic_vector(IR(3 downto 0))); MIO <='1';
  393.                         Smar <='0'; Smbr <= '0'; WR <='1'; RD <='0'; Salu <="0011"; LDF <='1'; INTA <='0';
  394.                        
  395.                         --ldh1
  396.                         when ldhi =>
  397.                         Sa <= "001"; Sbb<=to_bitvector(std_logic_vector(IR(7 downto 4))) ; Sba <= "0001"; Sid <="001"; Sbc <=to_bitvector(std_logic_vector(IR(3 downto 0))); MIO <='1';
  398.                         Smar <='0'; Smbr <= '0'; WR <='1'; RD <='0'; Salu <="0100"; LDF <='1'; INTA <='0';
  399.                        
  400.                         --ldlo
  401.                         when ldlo =>
  402.                         Sa <= "001"; Sbb<=to_bitvector(std_logic_vector(IR(7 downto 4))) ; Sba <= "0001"; Sid <="001"; Sbc <=to_bitvector(std_logic_vector(IR(3 downto 0))); MIO <='1';
  403.                         Smar <='0'; Smbr <= '0'; WR <='1'; RD <='0'; Salu <="0101"; LDF <='1'; INTA <='0';
  404.                        
  405.                         --or
  406.                         when or_2 =>
  407.                         Sa <= "001"; Sbb<=to_bitvector(std_logic_vector(IR(7 downto 4))) ; Sba <= "0001"; Sid <="001"; Sbc <=to_bitvector(std_logic_vector(IR(3 downto 0))); MIO <='1';
  408.                         Smar <='0'; Smbr <= '0'; WR <='1'; RD <='0'; Salu <="0110"; LDF <='1'; INTA <='0';
  409.                        
  410.                         --cmpne
  411.                         when cmpne =>
  412.                         Sa <= "001"; Sbb<=to_bitvector(std_logic_vector(IR(7 downto 4))) ; Sba <= "0001"; Sid <="001"; Sbc <=to_bitvector(std_logic_vector(IR(3 downto 0))); MIO <='1';
  413.                         Smar <='0'; Smbr <= '0'; WR <='1'; RD <='0'; Salu <="0111"; LDF <='1'; INTA <='0';
  414.                        
  415.                         --BCDtoB
  416.                         when bcdtoB =>
  417.                         Sa <= "001"; Sbb<=to_bitvector(std_logic_vector(IR(7 downto 4))) ; Sba <= "0001"; Sid <="001"; Sbc <="0000"; MIO <='1';
  418.                         Smar <='0'; Smbr <= '0'; WR <='1'; RD <='0'; Salu <="1000"; LDF <='1'; INTA <='0';
  419.                        
  420.  
  421.                         when others =>
  422.                         Sa <= "000"; Sbb <= "0000"; Sba <= "0000"; Sid <="000"; Sbc <="0000"; MIO <='1';
  423.                         Smar <='0'; Smbr <= '0'; WR <='0'; RD <='0'; Salu <="0000";
  424.                         LDF <='0'; INTA <='0';
  425.         end case;
  426.         end process;
  427. end rtl;
  428.