| 1 |
2 |
jsauermann |
-------------------------------------------------------------------------------
|
| 2 |
|
|
--
|
| 3 |
|
|
-- Copyright (C) 2009, 2010 Dr. Juergen Sauermann
|
| 4 |
|
|
--
|
| 5 |
|
|
-- This code is free software: you can redistribute it and/or modify
|
| 6 |
|
|
-- it under the terms of the GNU General Public License as published by
|
| 7 |
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
| 8 |
|
|
-- (at your option) any later version.
|
| 9 |
|
|
--
|
| 10 |
|
|
-- This code is distributed in the hope that it will be useful,
|
| 11 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 |
|
|
-- GNU General Public License for more details.
|
| 14 |
|
|
--
|
| 15 |
|
|
-- You should have received a copy of the GNU General Public License
|
| 16 |
|
|
-- along with this code (see the file named COPYING).
|
| 17 |
|
|
-- If not, see http://www.gnu.org/licenses/.
|
| 18 |
|
|
--
|
| 19 |
|
|
-------------------------------------------------------------------------------
|
| 20 |
|
|
-------------------------------------------------------------------------------
|
| 21 |
|
|
--
|
| 22 |
|
|
-- Module Name: opc_fetch - Behavioral
|
| 23 |
|
|
-- Create Date: 13:00:44 10/30/2009
|
| 24 |
|
|
-- Description: the opcode fetch stage of a CPU.
|
| 25 |
|
|
--
|
| 26 |
|
|
-------------------------------------------------------------------------------
|
| 27 |
|
|
--
|
| 28 |
|
|
library IEEE;
|
| 29 |
|
|
use IEEE.std_logic_1164.ALL;
|
| 30 |
|
|
use IEEE.std_logic_ARITH.ALL;
|
| 31 |
|
|
use IEEE.std_logic_UNSIGNED.ALL;
|
| 32 |
|
|
|
| 33 |
|
|
entity opc_fetch is
|
| 34 |
|
|
port ( I_CLK : in std_logic;
|
| 35 |
|
|
|
| 36 |
|
|
I_CLR : in std_logic;
|
| 37 |
|
|
I_INTVEC : in std_logic_vector( 5 downto 0);
|
| 38 |
|
|
I_LOAD_PC : in std_logic;
|
| 39 |
|
|
I_NEW_PC : in std_logic_vector(15 downto 0);
|
| 40 |
|
|
I_PM_ADR : in std_logic_vector(11 downto 0);
|
| 41 |
|
|
I_SKIP : in std_logic;
|
| 42 |
|
|
|
| 43 |
|
|
Q_OPC : out std_logic_vector(31 downto 0);
|
| 44 |
|
|
Q_PC : out std_logic_vector(15 downto 0);
|
| 45 |
|
|
Q_PM_DOUT : out std_logic_vector( 7 downto 0);
|
| 46 |
|
|
Q_T0 : out std_logic);
|
| 47 |
|
|
end opc_fetch;
|
| 48 |
|
|
|
| 49 |
|
|
architecture Behavioral of opc_fetch is
|
| 50 |
|
|
|
| 51 |
|
|
component prog_mem
|
| 52 |
|
|
port ( I_CLK : in std_logic;
|
| 53 |
|
|
|
| 54 |
|
|
I_WAIT : in std_logic;
|
| 55 |
|
|
I_PC : in std_logic_vector (15 downto 0);
|
| 56 |
|
|
I_PM_ADR : in std_logic_vector (11 downto 0);
|
| 57 |
|
|
|
| 58 |
|
|
Q_OPC : out std_logic_vector (31 downto 0);
|
| 59 |
|
|
Q_PC : out std_logic_vector (15 downto 0);
|
| 60 |
|
|
Q_PM_DOUT : out std_logic_vector ( 7 downto 0));
|
| 61 |
|
|
end component;
|
| 62 |
|
|
|
| 63 |
|
|
signal P_OPC : std_logic_vector(31 downto 0);
|
| 64 |
|
|
signal P_PC : std_logic_vector(15 downto 0);
|
| 65 |
|
|
|
| 66 |
|
|
signal L_INVALIDATE : std_logic;
|
| 67 |
|
|
signal L_LONG_OP : std_logic;
|
| 68 |
|
|
signal L_NEXT_PC : std_logic_vector(15 downto 0);
|
| 69 |
18 |
jsauermann |
signal L_OPC_10q0_qq0 : std_logic;
|
| 70 |
|
|
signal L_OPC_9_000 : std_logic;
|
| 71 |
|
|
signal L_OPC_9_5_000x_8 : std_logic;
|
| 72 |
|
|
signal L_OPC_9_5_110x_8 : std_logic;
|
| 73 |
|
|
signal L_OPC_9_10x1 : std_logic;
|
| 74 |
|
|
signal L_OPC_F_11 : std_logic;
|
| 75 |
2 |
jsauermann |
signal L_PC : std_logic_vector(15 downto 0);
|
| 76 |
|
|
signal L_T0 : std_logic;
|
| 77 |
|
|
signal L_WAIT : std_logic;
|
| 78 |
|
|
|
| 79 |
|
|
begin
|
| 80 |
|
|
|
| 81 |
|
|
pmem : prog_mem
|
| 82 |
|
|
port map( I_CLK => I_CLK,
|
| 83 |
|
|
|
| 84 |
|
|
I_WAIT => L_WAIT,
|
| 85 |
|
|
I_PC => L_NEXT_PC,
|
| 86 |
|
|
I_PM_ADR => I_PM_ADR,
|
| 87 |
|
|
|
| 88 |
|
|
Q_OPC => P_OPC,
|
| 89 |
|
|
Q_PC => P_PC,
|
| 90 |
|
|
Q_PM_DOUT => Q_PM_DOUT);
|
| 91 |
|
|
|
| 92 |
|
|
lpc: process(I_CLK)
|
| 93 |
|
|
begin
|
| 94 |
|
|
if (rising_edge(I_CLK)) then
|
| 95 |
|
|
L_PC <= L_NEXT_PC;
|
| 96 |
|
|
L_T0 <= not L_WAIT;
|
| 97 |
|
|
end if;
|
| 98 |
|
|
end process;
|
| 99 |
|
|
|
| 100 |
|
|
L_NEXT_PC <= X"0000" when (I_CLR = '1')
|
| 101 |
|
|
else L_PC when (L_WAIT = '1')
|
| 102 |
|
|
else I_NEW_PC when (I_LOAD_PC = '1')
|
| 103 |
|
|
else L_PC + X"0002" when (L_LONG_OP = '1')
|
| 104 |
|
|
else L_PC + X"0001";
|
| 105 |
|
|
|
| 106 |
|
|
-- Two word opcodes:
|
| 107 |
|
|
--
|
| 108 |
|
|
-- 9 3210
|
| 109 |
|
|
-- 1001 000d dddd 0000 kkkk kkkk kkkk kkkk - LDS
|
| 110 |
|
|
-- 1001 001d dddd 0000 kkkk kkkk kkkk kkkk - SDS
|
| 111 |
|
|
-- 1001 010k kkkk 110k kkkk kkkk kkkk kkkk - JMP
|
| 112 |
|
|
-- 1001 010k kkkk 111k kkkk kkkk kkkk kkkk - CALL
|
| 113 |
|
|
--
|
| 114 |
|
|
L_LONG_OP <= '1' when (((P_OPC(15 downto 9) = "1001010") and
|
| 115 |
|
|
(P_OPC( 3 downto 2) = "11")) -- JMP, CALL
|
| 116 |
|
|
or ((P_OPC(15 downto 10) = "100100") and
|
| 117 |
|
|
(P_OPC( 3 downto 0) = "0000"))) -- LDS, STS
|
| 118 |
|
|
else '0';
|
| 119 |
|
|
|
| 120 |
|
|
-- Two cycle opcodes:
|
| 121 |
|
|
--
|
| 122 |
18 |
jsauermann |
-- 10q0 qq0d dddd 1qqq - LDD (Y + q) L_OPC_10q0_qq0
|
| 123 |
|
|
-- 10q0 qq0d dddd 0qqq - LDD (Z + q) L_OPC_10q0_qq0
|
| 124 |
|
|
-- 1001 000d dddd .... - LDS etc. L_OPC_9_000
|
| 125 |
|
|
-- 1001 0101 0000 1000 - RET L_OPC_9_5_000x_8
|
| 126 |
|
|
-- 1001 0101 0001 1000 - RETI L_OPC_9_5_000x_8
|
| 127 |
|
|
-- 1001 0101 1100 1000 - LPM L_OPC_9_5_110x_8
|
| 128 |
|
|
-- 1001 0101 1101 1000 - ELPM L_OPC_9_5_110x_8
|
| 129 |
|
|
-- 1001 1001 AAAA Abbb - SBIC L_OPC_9_10x1
|
| 130 |
|
|
-- 1001 1011 AAAA Abbb - SBIS L_OPC_9_10x1
|
| 131 |
|
|
-- 1111 110r rrrr 0bbb - SBRC L_OPC_F_11
|
| 132 |
|
|
-- 1111 111r rrrr 0bbb - SBRS L_OPC_F_11
|
| 133 |
2 |
jsauermann |
--
|
| 134 |
18 |
jsauermann |
L_OPC_10q0_qq0 <= '1' when ((P_OPC(15 downto 14) = "10" )
|
| 135 |
|
|
and (P_OPC(12) = '0')
|
| 136 |
|
|
and (P_OPC( 9) = '0')) else '0';
|
| 137 |
|
|
L_OPC_9_000 <= '1' when ( P_OPC(15 downto 9) = "1001000") else '0';
|
| 138 |
|
|
L_OPC_9_5_000x_8 <= '1' when ((P_OPC(15 downto 5) = "10010101000")
|
| 139 |
|
|
and (P_OPC( 3 downto 0) = "1000")) else '0';
|
| 140 |
|
|
L_OPC_9_5_110x_8 <= '1' when ((P_OPC(15 downto 5) = "10010101110")
|
| 141 |
|
|
and (P_OPC( 3 downto 0) = "1000")) else '0';
|
| 142 |
|
|
L_OPC_9_10x1 <= '1' when ((P_OPC(15 downto 10) = "100110")
|
| 143 |
|
|
and (P_OPC(8) = '1')) else '0';
|
| 144 |
|
|
L_OPC_F_11 <= '1' when ( P_OPC(15 downto 10) = "111111") else '0';
|
| 145 |
2 |
jsauermann |
|
| 146 |
18 |
jsauermann |
L_WAIT <= L_T0 and (L_OPC_10q0_qq0 or -- LDD
|
| 147 |
|
|
L_OPC_9_000 or -- LDS, LD, LPM, POP
|
| 148 |
|
|
L_OPC_9_5_000x_8 or -- RET, RETI, LPM
|
| 149 |
|
|
L_OPC_9_5_110x_8 or -- LPM, ELPM
|
| 150 |
|
|
L_OPC_9_10x1 or -- SBIC, SBIS
|
| 151 |
|
|
L_OPC_F_11) -- SBRC, SBRS
|
| 152 |
|
|
and (not L_INVALIDATE)
|
| 153 |
|
|
and (not I_INTVEC(5));
|
| 154 |
|
|
|
| 155 |
2 |
jsauermann |
L_INVALIDATE <= I_CLR or I_SKIP;
|
| 156 |
|
|
|
| 157 |
|
|
Q_OPC <= X"00000000" when (L_INVALIDATE = '1')
|
| 158 |
|
|
else P_OPC when (I_INTVEC(5) = '0')
|
| 159 |
|
|
else (X"000000" & "00" & I_INTVEC); -- "interrupt opcode"
|
| 160 |
|
|
|
| 161 |
|
|
Q_PC <= P_PC;
|
| 162 |
|
|
Q_T0 <= L_T0;
|
| 163 |
|
|
|
| 164 |
|
|
end Behavioral;
|
| 165 |
|
|
|