OpenCores
URL https://opencores.org/ocsvn/tinycpu/tinycpu/trunk

Subversion Repositories tinycpu

[/] [tinycpu/] [trunk/] [src/] [core.vhd] - Blame information for rev 22

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 earlz
--Core module. 
2
--This module is basically connects everything and decodes the opcodes.
3
--The only thing above this is toplevel.vhd which actually sets the pinout for the FPGA
4
 
5
 
6
library IEEE;
7
use IEEE.STD_LOGIC_1164.ALL;
8
use IEEE.NUMERIC_STD.ALL;
9
use work.tinycpu.all;
10
 
11
entity core is
12
  port(
13
    --memory interface 
14
    MemAddr: out std_logic_vector(15 downto 0); --memory address (in bytes)
15
    MemWW: out std_logic; --memory writeword
16
    MemWE: out std_logic; --memory writeenable
17 20 earlz
    MemIn: in std_logic_vector(15 downto 0);
18
    MemOut: out std_logic_vector(15 downto 0);
19 19 earlz
    --general interface
20
    Clock: in std_logic;
21
    Reset: in std_logic; --When this is high, CPU will reset within 1 clock cycles. 
22
    --Enable: in std_logic; --When this is high, the CPU executes as normal, when low the CPU stops at the next clock cycle(maintaining all state)
23
    Hold: in std_logic; --when high, CPU pauses execution and places Memory interfaces into high impendance state so the memory can be used by other components
24
    HoldAck: out std_logic; --when high, CPU acknowledged hold and buses are in high Z
25
    --todo: port interface
26
 
27
    --debug ports:
28
    DebugIR: out std_logic_vector(15 downto 0); --current instruction
29 20 earlz
    DebugIP: out std_logic_vector(7 downto 0); --current IP
30
    DebugCS: out std_logic_vector(7 downto 0); --current code segment
31 19 earlz
    DebugTR: out std_logic; --current value of TR
32 20 earlz
    DebugR0: out std_logic_vector(7 downto 0)
33 19 earlz
   );
34
end core;
35
 
36
architecture Behavioral of core is
37
  component fetch is
38
    port(
39
      Enable: in std_logic;
40
      AddressIn: in std_logic_vector(15 downto 0);
41
      Clock: in std_logic;
42
      DataIn: in std_logic_vector(15 downto 0); --interface from memory
43
      IROut: out std_logic_vector(15 downto 0);
44
      AddressOut: out std_logic_vector(15 downto 0) --interface to memory
45
    );
46
  end component;
47
  component alu is
48
    port(
49
      Op: in std_logic_vector(4 downto 0);
50
      DataIn1: in std_logic_vector(7 downto 0);
51
      DataIn2: in std_logic_vector(7 downto 0);
52
      DataOut: out std_logic_vector(7 downto 0);
53
      TR: out std_logic
54
    );
55
  end component;
56
  component carryover is
57
    port(
58
      EnableCarry: in std_logic; --When disabled, SegmentIn goes to SegmentOut
59
      DataIn: in std_logic_vector(7 downto 0);
60
      SegmentIn: in std_logic_vector(7 downto 0);
61
      Addend: in std_logic_vector(7 downto 0); --How much to increase DataIn by (as a signed number). Believe it or not, that's the actual word for what we need.
62
      DataOut: out std_logic_vector(7 downto 0);
63 21 earlz
      SegmentOut: out std_logic_vector(7 downto 0);
64
      Clock: in std_logic
65 19 earlz
    );
66
  end component;
67
  component registerfile is
68
  port(
69
    WriteEnable: in regwritetype;
70
    DataIn: in regdatatype;
71
    Clock: in std_logic;
72
    DataOut: out regdatatype
73
  );
74
  end component;
75
 
76
  constant REGIP: integer := 7;
77
  constant REGSP: integer := 6;
78
  constant REGSS: integer := 15;
79
  constant REGES: integer := 14;
80
  constant REGDS: integer := 13;
81
  constant REGCS: integer := 12;
82
 
83
  type ProcessorState is (
84
    ResetProcessor,
85 21 earlz
    FirstFetch1, --the fetcher needs two clock cycles to catch up
86
    FirstFetch2,
87 19 earlz
    Execute,
88
    WaitForMemory,
89
    HoldMemory
90
  );
91 20 earlz
  signal state: ProcessorState;
92
  signal HeldState: ProcessorState; --state the processor was in when HOLD was activated
93 19 earlz
 
94
  --carryout signals
95
  signal CarryCS: std_logic;
96
  signal CarrySS: std_logic;
97
  signal IPAddend: std_logic_vector(7 downto 0);
98
  signal SPAddend: std_logic_vector(7 downto 0);
99
  signal IPCarryOut: std_logic_vector(7 downto 0);
100
  signal CSCarryOut: std_logic_vector(7 downto 0);
101
  --register signals
102
  signal regWE:regwritetype;
103
  signal regIn: regdatatype;
104
  signal regOut: regdatatype;
105
  --fetch signals
106
  signal fetchEN: std_logic;
107
  signal IR: std_logic_vector(15 downto 0);
108
 
109
  --control signals
110
  signal InReset: std_logic;
111
 
112
  --opcode shortcut signals
113
  signal opmain: std_logic_vector(3 downto 0);
114
  signal opimmd: std_logic_vector(7 downto 0);
115
  signal opcond1: std_logic; --first conditional bit
116
  signal opcond2: std_logic; --second conditional bit
117
  signal opreg1: std_logic_vector(2 downto 0);
118
  signal opreg2: std_logic_vector(2 downto 0);
119
  signal opreg3: std_logic_vector(2 downto 0);
120
  signal opseges: std_logic; --use ES segment
121
 
122 20 earlz
  signal fetcheraddress: std_logic_vector(15 downto 0);
123 19 earlz
begin
124 20 earlz
  reg: registerfile port map(
125 19 earlz
    WriteEnable => regWE,
126
    DataIn => regIn,
127
    Clock => Clock,
128
    DataOut => regOut
129
  );
130 20 earlz
  carryovercs: carryover port map(
131 19 earlz
    EnableCarry => CarryCS,
132 21 earlz
    DataIn => regIn(REGIP),
133
    SegmentIn => regIn(REGCS),
134 20 earlz
    Addend => IPAddend,
135
    DataOut => IPCarryOut,
136 21 earlz
    SegmentOut => CSCarryOut,
137
    Clock => Clock
138 19 earlz
  );
139 20 earlz
  fetcher: fetch port map(
140 19 earlz
    Enable => fetchEN,
141 20 earlz
    AddressIn => fetcheraddress,
142 19 earlz
    Clock => Clock,
143
    DataIn => MemIn,
144
    IROut => IR,
145
    AddressOut => MemAddr --this component supports tristate, so no worries about an intermediate signal
146
  );
147 21 earlz
  fetcheraddress <= regIn(REGCS) & regIn(REGIP);
148 20 earlz
 
149
 
150
  --opcode shortcuts
151 19 earlz
  opmain <= IR(15 downto 12);
152
  opimmd <= IR(7 downto 0);
153
  opcond1 <= IR(8);
154
  opcond2 <= IR(7);
155
  opreg1 <= IR(11 downto 9);
156
  opreg3 <= IR(2 downto 0);
157
  opreg2 <= IR(5 downto 3);
158
  opseges <= IR(6);
159 20 earlz
  --debug ports
160
  DebugCS <= regOut(REGCS);
161
  DebugIP <= regOut(REGIP);
162
  DebugR0 <= regOut(0);
163
  DebugIR <= IR;
164
 
165 19 earlz
 
166 21 earlz
 
167
  decode: process(Clock, Hold, state, IR, inreset, reset, regin, regout, IPCarryOut, CSCarryOut)
168 19 earlz
  begin
169
    if rising_edge(Clock) then
170 21 earlz
 
171
    --states
172 20 earlz
      if reset='1' and hold='0' then
173 19 earlz
        InReset <= '1';
174
        state <= ResetProcessor;
175 20 earlz
        HoldAck <= '0';
176 21 earlz
        CarryCS <= '1';
177
        CarrySS <= '0';
178
        regWE <= (others => '1');
179
        regIn <= (others => "00000000");
180
        regIn(REGCS) <= x"01";
181
        IPAddend <= x"00";
182
        fetchEN <= '1';
183 19 earlz
        --finish up
184
      elsif InReset='1' and reset='0' and Hold='0' then --reset is done, start executing
185
        InReset <= '0';
186 21 earlz
        fetchEN <= '1';
187
        state <= FirstFetch1;
188 19 earlz
      elsif Hold = '1' and (state=HoldMemory or state=Execute or state=ResetProcessor) then
189 20 earlz
        --do not hold immediately if waiting on memory or if waiting on the first fetch of an instruction after reset
190 19 earlz
        state <= HoldMemory;
191
        HoldAck <= '1';
192 21 earlz
        FetchEN <= '0';
193
        MemAddr <= "ZZZZZZZZZZZZZZZZ";
194
        MemOut <= "ZZZZZZZZZZZZZZZZ";
195
        MemWE <= 'Z';
196
        MemWW <= 'Z';
197 19 earlz
      elsif Hold='0' and state=HoldMemory then
198 20 earlz
        if reset='1' or InReset='1' then
199
          state <= ResetProcessor;
200
        else
201
          state <= Execute;
202
        end if;
203 21 earlz
        FetchEN <= '1';
204
      elsif state=FirstFetch1 then --we have to let IR get loaded before we can execute.
205 20 earlz
        --regWE <= (others => '0');
206 21 earlz
        fetchEN <= '1'; --already enabled, but anyway
207
        regWE <= (others => '0');
208
        state <= FirstFetch2;
209
      elsif state=FirstFetch2 then
210
        state <= Execute;
211
        IPAddend <= x"02";
212
        SPAddend <= x"00"; --no addend unless pushing or popping
213
        RegWE <= (others => '0');
214
        regIn(REGIP) <= IPCarryOut;
215
        regWE(REGIP) <= '1';
216
        regWE(REGCS) <= '1';
217
        regIn(REGCS) <= CSCarryOut;
218 19 earlz
      end if;
219 21 earlz
 
220
 
221 19 earlz
      if state=Execute then
222 20 earlz
        fetchEN <= '1';
223 19 earlz
        --reset to "usual"
224 20 earlz
        IPAddend <= x"02";
225
        SPAddend <= x"00"; --no addend unless pushing or popping
226 19 earlz
        RegWE <= (others => '0');
227 21 earlz
        regIn(REGIP) <= IPCarryOut;
228
        regWE(REGIP) <= '1';
229
        regWE(REGCS) <= '1';
230
        regIn(REGCS) <= CSCarryOut;
231
 
232 20 earlz
        MemWE <= '0';
233
        MemWW <= '0';
234 19 earlz
 
235
        --actual decoding
236
        case opmain is
237
          when "0000" => --mov reg,imm
238
            RegIn(to_integer(unsigned(opreg1))) <= opimmd;
239
            RegWE(to_integer(unsigned(opreg1))) <= '1';
240
          when others =>
241
            --synthesis off
242
            report "Not implemented" severity error;
243
            --synthesis on
244
        end case;
245
      end if;
246 21 earlz
 
247
 
248
 
249
 
250 19 earlz
    end if;
251
  end process;
252
 
253
 
254
 
255
 
256
 
257
 
258
 
259
 
260
 
261
end Behavioral;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.