Advertisement
Sothian

VHDL

Jun 17th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 12.45 KB | None | 0 0
  1. ////////////////////////////////////////////////////// CONTROL
  2.  
  3. LIBRARY ieee;
  4. USE ieee.std_logic_1164.ALL;
  5. USE ieee.numeric_std.ALL;
  6. ENTITY control IS
  7.     PORT (
  8.         clk : IN std_logic;
  9.         IR : IN signed(15 DOWNTO 0);
  10.         reset, C, Z, S, INT : IN std_logic;
  11.         Salu, Sbb, Sbc, Sba : OUT bit_vector(3 DOWNTO 0);
  12.         Sid : OUT bit_vector(2 DOWNTO 0);
  13.         Sa : OUT bit_vector(1 DOWNTO 0);
  14.         LDF, Smar, Smbr, WR, RD, INTA, MIO : OUT BIT
  15.     );
  16. END ENTITY;
  17. ARCHITECTURE rtl OF control IS
  18.     TYPE state_type IS (fetch, dec, wait_1, call_1, call_2, call_3, call_4,
  19.     RET_1, RET_2, RET_3, push, pop_1, pop_2, neg, inc, dec_1, not_1, shr, shl,
  20.     m29, mov_r, mov_rm, add, sub, and_1, or_1, xor_1, in_r, out_io, jump_1, jump_2,
  21.     long_jump, r_st16,  r_adr32, m9,
  22.     mov_arg1_arg2, add_arg1_arg2, sub_arg1_arg2, gethx_arg1_arg2,getlx_arg1_arg2,
  23.     shl_arg1_arg2, shr_arg1_arg2, jmp_etykieta);
  24.     SIGNAL state : state_type;
  25. BEGIN
  26.     PROCESS (clk, reset)
  27.     BEGIN
  28.         IF reset = '1' THEN
  29.             state <= fetch;
  30.         ELSIF (clk'EVENT AND clk = '1') THEN
  31.             CASE state IS
  32.                 WHEN fetch =>
  33.                     state <= dec;
  34.                 WHEN dec =>
  35.                     CASE IR(15 DOWNTO 13) IS
  36.                         WHEN "000" =>
  37.                             CASE IR(12 DOWNTO 11) IS
  38.                                 WHEN "00" =>
  39.                                     IF (INT = '0') THEN
  40.                                         state <= fetch;
  41.                                     ELSE
  42.                                         state <= m9;
  43.                                     END IF;
  44.                                 WHEN "01" => state <= wait_1;
  45.                                 WHEN "10" => state <= call_1;
  46.                                 WHEN "11" => state <= RET_1;
  47.                                 WHEN others => state <= fetch;
  48.                         END CASE;
  49.                         WHEN "001" =>
  50.                             CASE IR(12 DOWNTO 8) IS
  51.                                 WHEN "00000" => state <= push;
  52.                                 WHEN "00001" => state <= pop_1;
  53.                                 WHEN "00010" => state <= neg;
  54.                                 WHEN "00011" => state <= inc;
  55.                                 WHEN "00100" => state <= dec_1;
  56.                                 WHEN "00101" => state <= not_1;
  57.                                 WHEN "00110" => state <= shr;
  58.                                 WHEN "00111" => state <= shl;
  59.                                 WHEN "01000" => state <= m29;
  60.                                 WHEN "01001" => state <= mov_r;
  61.                                 WHEN "01010" => state <= mov_rm;
  62.                                 WHEN "01011" => state <= add;
  63.                                 WHEN "01100" => state <= sub;
  64.                                 WHEN "01101" => state <= and_1;
  65.                                 WHEN "01110" => state <= or_1;
  66.                                 WHEN "01111" => state <= xor_1;
  67.                                 WHEN "10000" => state <= in_r;
  68.                                 WHEN "10001" => state <= out_io;
  69.                                 WHEN OTHERS => state <= fetch;
  70.                         END CASE;
  71.                         WHEN "010" => state <= jump_1;
  72.                         WHEN "011" => state <= jump_2;
  73.                        
  74.                     WHEN "100" =>
  75.                         case IR(12 downto 8) is
  76.                             when "000000" => state <= mov_arg1_arg2;
  77.                             when "000001" => state <= add_arg1_arg2;
  78.                             when "000010" => state <= sub_arg1_arg2;
  79.                             when "000011" => state <= gethx_arg1_arg2;
  80.                             when "000100" => state <= getlx_arg1_arg2;
  81.                             when "000101" => state <= shl_arg1_arg2;
  82.                             when "000110" => state <= shr_arg1_arg2;
  83.                             when "000111" => state <= jmp_etykieta;
  84.                             WHEN OTHERS => state <= fetch;
  85.                             END CASE;
  86.                         when "101" => state <= r_adr32;
  87.                         when others => state <= fetch;
  88.                         end case;
  89.                            
  90.                 WHEN wait_1 =>
  91.                     IF INT = '1' THEN
  92.                         state <= m9;
  93.                     ELSE state <= wait_1;
  94.                     END IF;
  95.                 WHEN call_1 => state <= call_2;
  96.                 WHEN call_2 => state <= call_3;
  97.                 WHEN call_3 => state <= call_4;
  98.                 WHEN call_4 =>
  99.                     IF INT = '1' THEN
  100.                         state <= m9;
  101.                     ELSE state <= fetch;
  102.                     END IF;
  103.                 when others => state <= fetch;
  104.             END CASE;
  105.         END IF;
  106.     END PROCESS;
  107.    
  108.     PROCESS (state) BEGIN
  109.     CASE state IS
  110.         WHEN fetch =>
  111.             Sa <= "01"; Sbb <= "0000"; Sba <= "0000"; Sid <= "001"; Sbc <= "0000";
  112.             MIO <= '1'; Smar <= '1'; Smbr <= '0'; WR <= '0'; RD <= '1'; Salu <="0000"; LDF <= '0'; INTA <= '0';
  113.         WHEN dec =>
  114.             Sa <= "00"; Sbb <= "0000"; Sba <= "0000"; Sid <= "000"; Sbc <= "0000";
  115.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '0'; RD <= '0'; Salu <="0000"; LDF <= '0'; INTA <= '0';
  116.         WHEN wait_1 =>
  117.             Sa <= "00"; Sbb <= "0000"; Sba <= "0000"; Sid <= "000"; Sbc <= "0000";
  118.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '0'; RD <= '0'; Salu <="0000"; LDF <= '0'; INTA <= '0';
  119.         WHEN call_1 =>
  120.             Sa <= "10"; Sbb <= "1010"; Sba <= "0000"; Sid <= "011"; Sbc <= "0000";
  121.             MIO <= '1'; Smar <= '1'; Smbr <= '1'; WR <= '1'; RD <= '0'; Salu <="0000"; LDF <= '0'; INTA <= '0';
  122.            
  123.         WHEN mov_arg1_arg2 =>
  124.             Sa <= "01"; Sbb <=to_bitvector(std_logic_vector(IR(7 downto 4))); Sba <= "0001"; Sid <= "001";
  125.             Sbc <= to_bitvector(std_logic_vector(IR(3 downto 0)));
  126.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '1'; RD <= '0'; Salu <="0000"; LDF <= '1'; INTA <= '0';
  127.                
  128.         WHEN add_arg1_arg2 =>
  129.             Sa <= "01"; Sbb <=to_bitvector(std_logic_vector(IR(7 downto 4))); Sba <= "0001"; Sid <= "001";
  130.             Sbc <= to_bitvector(std_logic_vector(IR(3 downto 0)));
  131.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '1'; RD <= '0'; Salu <="0001"; LDF <= '1'; INTA <= '0';
  132.            
  133.         WHEN sub_arg1_arg2 =>
  134.             Sa <= "01"; Sbb <=to_bitvector(std_logic_vector(IR(7 downto 4))); Sba <= "0001"; Sid <= "001";
  135.             Sbc <= to_bitvector(std_logic_vector(IR(3 downto 0)));
  136.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '1'; RD <= '0'; Salu <="0010"; LDF <= '1'; INTA <= '0';
  137.        
  138.        
  139.        
  140.         WHEN gethx_arg1_arg2 =>
  141.             Sa <= "01"; Sbb <=to_bitvector(std_logic_vector(IR(7 downto 4))); Sba <= "0001"; Sid <= "001";
  142.             Sbc <= to_bitvector(std_logic_vector(IR(3 downto 0)));
  143.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '1'; RD <= '0'; Salu <="0011"; LDF <= '1'; INTA <= '0';
  144.        
  145.         WHEN getlx_arg1_arg2 =>
  146.             Sa <= "01"; Sbb <=to_bitvector(std_logic_vector(IR(7 downto 4))); Sba <= "0001"; Sid <= "001";
  147.             Sbc <= to_bitvector(std_logic_vector(IR(3 downto 0)));
  148.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '1'; RD <= '0'; Salu <="0100"; LDF <= '1'; INTA <= '0';
  149.            
  150.         WHEN shl_arg1_arg2 =>
  151.             Sa <= "01"; Sbb <=to_bitvector(std_logic_vector(IR(7 downto 4))); Sba <= "0001"; Sid <= "001";
  152.             Sbc <= to_bitvector(std_logic_vector(IR(3 downto 0)));
  153.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '1'; RD <= '0'; Salu <="0101"; LDF <= '1'; INTA <= '0';
  154.        
  155.         WHEN shr_arg1_arg2 =>
  156.             Sa <= "01"; Sbb <=to_bitvector(std_logic_vector(IR(7 downto 4))); Sba <= "0001"; Sid <= "001";
  157.             Sbc <= to_bitvector(std_logic_vector(IR(3 downto 0)));
  158.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '1'; RD <= '0'; Salu <="0110"; LDF <= '1'; INTA <= '0';
  159.        
  160.         WHEN jmp_etykieta =>
  161.             Sa <= "01"; Sbb <=to_bitvector(std_logic_vector(IR(7 downto 4))); Sba <= "0001"; Sid <= "001";
  162.             Sbc <= to_bitvector(std_logic_vector(IR(3 downto 0)));
  163.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '1'; RD <= '0'; Salu <="0111"; LDF <= '1'; INTA <= '0';
  164.            
  165.            
  166.            
  167.                
  168.                
  169.         WHEN OTHERS =>
  170.             Sa <= "00"; Sbb <= "0000"; Sba <= "0000"; Sid <= "000"; Sbc <= "0000";
  171.             MIO <= '1'; Smar <= '0'; Smbr <= '0'; WR <= '0'; RD <= '0'; Salu <="0000"; LDF <= '0'; INTA <= '0';
  172.         END CASE;
  173.     END PROCESS;
  174. END rtl;
  175.  
  176.  
  177. ////////////////////////////////////////////////////// PROCESOR
  178. library ieee;
  179. use ieee.std_logic_1164.all;
  180. use ieee.numeric_std.all;
  181.  
  182. entity ALU is
  183. port (  A : in signed(7 downto 0);
  184.             B : in signed(7 downto 0);
  185.             Salu : in bit_vector (2 downto 0);
  186.             LDF : in bit;
  187.             clk : in bit;  
  188.             Y : out signed (7 downto 0);
  189.             C,Z,S,P : out std_logic
  190.         );
  191. end entity;
  192.  
  193. architecture rtl of ALU is
  194.    begin
  195.             process (Salu, A, B, clk)
  196.                 variable res, AA, BB,CC: signed (16 downto 0);
  197.                 variable CF,ZF,SF,PF : std_logic;
  198.                 variable i : integer range 0 to 15;
  199.                 variable zmienna : integer;
  200.                
  201.                 begin
  202.                 AA(8) := A(7);
  203.                 AA(7 downto 0) := A;
  204.                 BB(8) := B(7);
  205.                 BB(7 downto 0) := B;
  206.                 CC(0) := CF;
  207.                 CC(8 downto 1) := "00000000";
  208.                
  209.                 case Salu is
  210.                     when "000" => res := AA; -- MOV arg1, arg2
  211.                     when "001" => res := AA + BB; -- ADD arg1, arg2
  212.                     when "010" => res := AA - BB; -- SUB arg1, arg2
  213.                     when "011" => res(to_integer(BB) downto 0) := AA(to_integer(BB) downto 0); -- GETHX arg1, arg2
  214.                    
  215.                     when "100" =>
  216.                         zmienna := to_integer(unsigned(BB(3 downto 0)));
  217.                         res(zmienna downto 0) := AA(zmienna downto 0);
  218.                    
  219.                    
  220.                     when "101" => res:= AA sll to_integer(BB); -- SHL arg1, arg2
  221.                     when "110" => res:= AA srl to_integer(BB);-- SHR arg1, arg2
  222.                     --when "1111" => ; -- JMP etykieta
  223.                  
  224.                    
  225.                     when others => res := null;
  226.                 end case;
  227.             Y <= res(7 downto 0);
  228.             Z <= ZF;
  229.             S <= SF;
  230.             C <= CF;
  231.             P <= PF;
  232.             if (clk'event and clk='1') then
  233.                     if (LDF='1') then
  234.                    
  235.                             if (res = "00000000") then ZF:='1';
  236.                             else ZF:='0';
  237.                             end if;
  238.                            
  239.                             if (res(7)='1') then SF:='1';
  240.                             else SF:='0';
  241.                             end if;
  242.                            
  243.                             CF := res(8) xor res(7);
  244.                            
  245.                             PF := '0';
  246.                             for i in res'range loop
  247.                                 PF:= PF xor res(i);
  248.                             end loop;
  249.                            
  250.                     end if;
  251.             end if;
  252.         end process;
  253. end rtl;
  254.  
  255.  
  256. ////////////////////////////////////////////////////// REJESTRY
  257. library ieee;
  258. use ieee.std_logic_1164.all;
  259. use ieee.numeric_std.all;
  260. entity Rejestry is
  261. port
  262. (
  263.     clk : in std_logic;
  264.     DI : in signed (7 downto 0);
  265.     BA : in signed (7 downto 0);
  266.     Sbb : in signed (3 downto 0);
  267.     Sbc : in signed (3 downto 0);
  268.     Sba : in signed (3 downto 0);
  269.     Sid : in signed (2 downto 0);
  270.     Sa : in signed (1 downto 0);
  271.     BB : out signed (7 downto 0);
  272.     BC : out signed (7 downto 0);
  273.     ADR : out signed (15 downto 0);
  274.     IRout : out signed (7 downto 0)
  275. );
  276. end entity;
  277. architecture rtl of Rejestry is
  278. begin
  279. process (clk, Sbb, Sbc, Sba, Sid, Sa, DI)
  280.         variable IR, TMP, A, B, C, D, E, F: signed (7 downto 0);
  281.         variable AD, PC, SP, ATMP : signed (15 downto 0);
  282.         variable DS, CS : signed (15 downto 0);
  283. begin
  284.         if (clk'event and clk='1') then
  285.             case Sid is
  286.                 when "001" => PC := PC + 1;
  287.                 when "010" => SP := SP + 1;
  288.                 when "011" => AD := AD + 1;
  289.                 when "101" => SP := SP - 1;
  290.                 when "110" => AD := AD - 1;
  291.                 when others =>  null;
  292.             end case;
  293.            
  294.             case Sba is
  295.                 when "0000" => IR := BA;
  296.                 when "0001" => TMP := BA;
  297.                 when "0010" => A := BA;
  298.                 when "0011" => B := BA;
  299.                 when "0100" => C := BA;
  300.                 when "0101" => D := BA;
  301.                 when "0110" => E := BA;
  302.                 when "0111" => F := BA;
  303.                 when "1000" => AD := BA;
  304.                 when "1001" => PC := BA;
  305.                 when "1010" => SP := BA;
  306.                 when "1011" => ATMP := BA;
  307.                 when "1100" => DS := BA;
  308.                 when "1101" => CS := BA;
  309.  
  310.             end case;
  311.         end if;
  312.        
  313.             case Sbb is
  314.                 when "0000" => BB <= DI;
  315.                 when "0001" => BB <= TMP;  
  316.                 when "0010" => BB <= A;
  317.                 when "0011" => BB <= B;
  318.                 when "0100" => BB <= C;
  319.                 when "0101" => BB <= D;
  320.                 when "0110" => BB <= E;
  321.                 when "0111" => BB <= F;
  322.                 when "1000" => BB <= AD;
  323.                 when "1001" => BB <= PC;
  324.                 when "1010" => BB <= SP;
  325.                 when "1011" => BB <= ATMP;
  326.                 when "1100" => BB <= DS;
  327.                 when "1101" => BB <= CS;
  328.  
  329.             end case;
  330.            
  331.             case Sbc is
  332.                 when "0000" => BC <= DI;
  333.                 when "0001" => BC <= TMP;  
  334.                 when "0010" => BC <= A;
  335.                 when "0011" => BC <= B;
  336.                 when "0100" => BC <= C;
  337.                 when "0101" => BC <= D;
  338.                 when "0110" => BC <= E;
  339.                 when "0111" => BC <= F;
  340.                 when "1000" => BC <= AD;
  341.                 when "1001" => BC <= PC;
  342.                 when "1010" => BC <= SP;
  343.                 when "1011" => BC <= ATMP;
  344.                 when "1100" => BC <= DS;
  345.                 when "1101" => BC <= CS;
  346.             end case;
  347.            
  348.             case Sa is
  349.                 when "00" => ADR <= AD;
  350.                 when "01" => ADR <= PC;
  351.                 when "10" => ADR <= SP;
  352.                 when "11" => ADR <= ATMP;
  353.             end case;
  354.            
  355. IRout <= IR;
  356.  
  357. end process;
  358. end rtl;
  359.  
  360.  
  361. ////////////////////////////////////////////////////// ??
  362. library ieee;
  363. use ieee.std_logic_1164.all;
  364. use ieee.numeric_std.all;
  365. entity busint is
  366. port
  367. (
  368.     ADR : in signed(31 downto 0);
  369.     DO : in signed(15 downto 0);
  370.     Smar, Smbr, WRin, RDin : in bit;
  371.     AD : out signed (31 downto 0);
  372.     D : inout signed (15 downto 0);
  373.     DI : out signed(15 downto 0);
  374.     WR, RD : out bit ;
  375.     QWE : in bit;
  376.     SEG : in signed(9 downto 0);
  377.     OFS : in signed(5 downto 0)
  378.    
  379. );
  380. end entity;
  381.  
  382. architecture rtl of busint is
  383.     begin
  384.         process(Smar, ADR, Smbr, DO, D, WRin, RDin)
  385.             variable MBRin, MBRout: signed(15 downto 0);
  386.             variable MAR : signed(31 downto 0);
  387.                 begin
  388.                     if(Smar='1') then
  389.                         if(QWE = '1') then
  390.                             MAR(15 downto 6) := SEG;
  391.                             MAR(5 downto 0) := OFS;
  392.                         else MAR := ADR;
  393.                         end if;
  394.                     end if;
  395.                    
  396.                     if(Smbr='1') then MBRout := DO; end if;
  397.                     if (RDin='1') then MBRin := D; end if;
  398.                     if (WRin='1') then D <= MBRout;
  399.                     else D <= "ZZZZZZZZZZZZZZZZ";
  400.                 end if;
  401.         DI <= MBRin;
  402.         AD <= MAR;
  403.         WR <= WRin;
  404.         RD <= RDin;
  405. end process;
  406. end rtl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement