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

Subversion Repositories tinycpu

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

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 23 earlz
    Firstfetch3,
88 19 earlz
    Execute,
89
    WaitForMemory,
90
    HoldMemory
91
  );
92 20 earlz
  signal state: ProcessorState;
93
  signal HeldState: ProcessorState; --state the processor was in when HOLD was activated
94 19 earlz
 
95
  --carryout signals
96
  signal CarryCS: std_logic;
97
  signal CarrySS: std_logic;
98
  signal IPAddend: std_logic_vector(7 downto 0);
99
  signal SPAddend: std_logic_vector(7 downto 0);
100
  signal IPCarryOut: std_logic_vector(7 downto 0);
101
  signal CSCarryOut: std_logic_vector(7 downto 0);
102 25 earlz
  signal SPCarryOut: std_logic_vector(7 downto 0);
103
  signal SSCarryOut: std_logic_vector(7 downto 0);
104
 
105 19 earlz
  --register signals
106
  signal regWE:regwritetype;
107
  signal regIn: regdatatype;
108
  signal regOut: regdatatype;
109
  --fetch signals
110
  signal fetchEN: std_logic;
111
  signal IR: std_logic_vector(15 downto 0);
112 25 earlz
  --alu signals
113
  signal AluOp: std_logic_vector(4 downto 0);
114
  signal AluIn1: std_logic_vector(7 downto 0);
115
  signal AluIn2: std_logic_vector(7 downto 0);
116
  signal AluOut: std_logic_vector(7 downto 0);
117
  signal TR: std_logic;
118 19 earlz
 
119
  --control signals
120
  signal InReset: std_logic;
121 25 earlz
  signal OpAddress: std_logic_vector(15 downto 0); --memory address to use for operation of an instruction
122
  signal OpData: std_logic_vector(15 downto 0); --data to write or will load to here
123
  signal OpWW: std_logic;
124
  signal OpWE: std_logic;
125 19 earlz
 
126
  --opcode shortcut signals
127
  signal opmain: std_logic_vector(3 downto 0);
128
  signal opimmd: std_logic_vector(7 downto 0);
129
  signal opcond1: std_logic; --first conditional bit
130
  signal opcond2: std_logic; --second conditional bit
131
  signal opreg1: std_logic_vector(2 downto 0);
132
  signal opreg2: std_logic_vector(2 downto 0);
133
  signal opreg3: std_logic_vector(2 downto 0);
134
  signal opseges: std_logic; --use ES segment
135 25 earlz
 
136
  signal regbank: std_logic;
137 19 earlz
 
138 20 earlz
  signal fetcheraddress: std_logic_vector(15 downto 0);
139 25 earlz
 
140
  --temporary signals
141
  signal tempreg1: std_logic_vector(3 downto 0);
142
  signal tempreg2: std_logic_vector(3 downto 0);
143
  signal tempreg3: std_logic_vector(3 downto 0);
144
  signal FetchMemAddr: std_logic_vector(15 downto 0);
145
 
146
 
147 19 earlz
begin
148 20 earlz
  reg: registerfile port map(
149 19 earlz
    WriteEnable => regWE,
150
    DataIn => regIn,
151
    Clock => Clock,
152
    DataOut => regOut
153
  );
154 20 earlz
  carryovercs: carryover port map(
155 19 earlz
    EnableCarry => CarryCS,
156 21 earlz
    DataIn => regIn(REGIP),
157
    SegmentIn => regIn(REGCS),
158 20 earlz
    Addend => IPAddend,
159
    DataOut => IPCarryOut,
160 21 earlz
    SegmentOut => CSCarryOut,
161
    Clock => Clock
162 19 earlz
  );
163 25 earlz
  carryoverss: carryover port map(
164
    EnableCarry => CarrySS,
165
    DataIn => regIn(REGSP),
166
    SegmentIn => RegIn(REGSS),
167
    Addend => SPAddend,
168
    DataOut => SPCarryOut,
169
    SegmentOut => SSCarryOut,
170
    Clock => Clock
171
  );
172 20 earlz
  fetcher: fetch port map(
173 19 earlz
    Enable => fetchEN,
174 20 earlz
    AddressIn => fetcheraddress,
175 19 earlz
    Clock => Clock,
176
    DataIn => MemIn,
177
    IROut => IR,
178 25 earlz
    AddressOut => FetchMemAddr
179 19 earlz
  );
180 25 earlz
  cpualu: alu port map(
181
    Op => AluOp,
182
    DataIn1 => AluIn1,
183
    DataIn2 => AluIn2,
184
    DataOut => AluOut,
185
    TR => TR
186
  );
187 21 earlz
  fetcheraddress <= regIn(REGCS) & regIn(REGIP);
188 25 earlz
  MemAddr <= OpAddress when state=WaitForMemory else FetchMemAddr;
189
  MemOut <= OpData when (state=WaitForMemory and OpWE='1') else x"0000";
190
  MemWE <= OpWE when state=WaitForMemory else '0';
191
  MemWW <= OpWW when state=WaitForMemory else '0';
192
  OpData <= MemIn when (state=WaitForMemory and OpWE='0') else "ZZZZZZZZZZZZZZZZ";
193 20 earlz
  --opcode shortcuts
194 19 earlz
  opmain <= IR(15 downto 12);
195
  opimmd <= IR(7 downto 0);
196
  opcond1 <= IR(8);
197
  opcond2 <= IR(7);
198
  opreg1 <= IR(11 downto 9);
199
  opreg3 <= IR(2 downto 0);
200
  opreg2 <= IR(5 downto 3);
201
  opseges <= IR(6);
202 20 earlz
  --debug ports
203
  DebugCS <= regOut(REGCS);
204
  DebugIP <= regOut(REGIP);
205
  DebugR0 <= regOut(0);
206
  DebugIR <= IR;
207 25 earlz
  DebugTR <= TR;
208
  --register addresses with registerbank baked in
209
  tempreg1 <= ('1' & opreg1) when (regbank='1' and opreg1(2)='0') else '0' & opreg1;
210
  tempreg2 <= ('1' & opreg2) when (regbank='1' and opreg2(2)='0') else '0' & opreg2;
211
  tempreg3 <= ('1' & opreg3) when (regbank='1' and opreg3(2)='0') else '0' & opreg3;
212 19 earlz
 
213 21 earlz
 
214 25 earlz
 
215 21 earlz
  decode: process(Clock, Hold, state, IR, inreset, reset, regin, regout, IPCarryOut, CSCarryOut)
216 19 earlz
  begin
217
    if rising_edge(Clock) then
218 21 earlz
 
219
    --states
220 20 earlz
      if reset='1' and hold='0' then
221 19 earlz
        InReset <= '1';
222
        state <= ResetProcessor;
223 20 earlz
        HoldAck <= '0';
224 21 earlz
        CarryCS <= '1';
225
        CarrySS <= '0';
226
        regWE <= (others => '1');
227
        regIn <= (others => "00000000");
228
        regIn(REGCS) <= x"01";
229
        IPAddend <= x"00";
230 25 earlz
        SPAddend <= x"00";
231
        AluOp <= "10001"; --reset TR in ALU
232
        regbank <= '0';
233 21 earlz
        fetchEN <= '1';
234 25 earlz
        OpData <= "ZZZZZZZZZZZZZZZZ";
235
        OpAddress <= x"0000";
236
        OpWE <= '0';
237
        opWW <= '0';
238 19 earlz
        --finish up
239
      elsif InReset='1' and reset='0' and Hold='0' then --reset is done, start executing
240
        InReset <= '0';
241 21 earlz
        fetchEN <= '1';
242
        state <= FirstFetch1;
243 19 earlz
      elsif Hold = '1' and (state=HoldMemory or state=Execute or state=ResetProcessor) then
244 20 earlz
        --do not hold immediately if waiting on memory or if waiting on the first fetch of an instruction after reset
245 19 earlz
        state <= HoldMemory;
246
        HoldAck <= '1';
247 21 earlz
        FetchEN <= '0';
248
        MemAddr <= "ZZZZZZZZZZZZZZZZ";
249
        MemOut <= "ZZZZZZZZZZZZZZZZ";
250
        MemWE <= 'Z';
251
        MemWW <= 'Z';
252 19 earlz
      elsif Hold='0' and state=HoldMemory then
253 20 earlz
        if reset='1' or InReset='1' then
254
          state <= ResetProcessor;
255
        else
256
          state <= Execute;
257
        end if;
258 21 earlz
        FetchEN <= '1';
259
      elsif state=FirstFetch1 then --we have to let IR get loaded before we can execute.
260 20 earlz
        --regWE <= (others => '0');
261 21 earlz
        fetchEN <= '1'; --already enabled, but anyway
262 23 earlz
        --regWE <= (others => '0');
263 21 earlz
        IPAddend <= x"02";
264
        SPAddend <= x"00"; --no addend unless pushing or popping
265
        RegWE <= (others => '0');
266
        regIn(REGIP) <= IPCarryOut;
267
        regWE(REGIP) <= '1';
268
        regWE(REGCS) <= '1';
269
        regIn(REGCS) <= CSCarryOut;
270 23 earlz
        state <= Execute;
271
      elsif state=FirstFetch2 then
272
        state <= FirstFetch3;
273
 
274
      elsif state=FirstFetch3 then
275
        state <= Execute;
276 25 earlz
      elsif state=WaitForMemory then
277
        state <= Execute;
278
        FetchEn <= '1';
279
        IpAddend <= x"02";
280 19 earlz
      end if;
281 21 earlz
 
282
 
283 19 earlz
      if state=Execute then
284 20 earlz
        fetchEN <= '1';
285 19 earlz
        --reset to "usual"
286 20 earlz
        IPAddend <= x"02";
287
        SPAddend <= x"00"; --no addend unless pushing or popping
288 19 earlz
        RegWE <= (others => '0');
289 21 earlz
        regIn(REGIP) <= IPCarryOut;
290
        regWE(REGIP) <= '1';
291
        regWE(REGCS) <= '1';
292
        regIn(REGCS) <= CSCarryOut;
293 25 earlz
        regIn(REGSP) <= SPCarryOut; --with addend being 0, it'll just write SP to SP so it won't change, but this makes code easier for me
294
        regIn(REGSS) <= SSCarryOut;
295
        regWE(REGSP) <= '1';
296
        regWE(REGSS) <= '1';
297
        OpAddress <= "ZZZZZZZZZZZZZZZZ";
298 21 earlz
 
299 19 earlz
        --actual decoding
300 25 earlz
        if opcond1='0' or (opcond1='1' and TR='1') then
301
          case opmain is
302
            when "0000" => --mov reg,imm
303
              regIn(to_integer(unsigned(tempreg1))) <= opimmd;
304
              regWE(to_integer(unsigned(tempreg1))) <= '1';
305
            when "0001" => --mov [reg],imm
306
              OpAddress <= regOut(REGDS) & regOut(to_integer(unsigned(tempreg1)));
307
              OpWE <= '1';
308
              OpData <= x"00" & opimmd;
309
              OpWW <= '0';
310
              state <= WaitForMemory;
311
              IPAddend <= x"00"; --disable all this because we have to wait a cycle to write memory
312
              FetchEN <= '0';
313
            when others =>
314
              --synthesis off
315
              report "Not implemented" severity error;
316
              --synthesis on
317
          end case;
318
        end if;
319 19 earlz
      end if;
320 21 earlz
 
321
 
322
 
323
 
324 19 earlz
    end if;
325
  end process;
326
 
327
 
328
 
329
 
330
 
331
 
332
 
333
 
334
 
335
end Behavioral;

powered by: WebSVN 2.1.0

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