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

Subversion Repositories c16

[/] [c16/] [trunk/] [vhdl/] [cpu_engine.vhd] - Blame information for rev 9

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

Line No. Rev Author Line
1 2 jsauermann
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.STD_LOGIC_ARITH.ALL;
4
use IEEE.STD_LOGIC_UNSIGNED.ALL;
5
 
6
--  Uncomment the following lines to use the declarations that are
7
--  provided for instantiating Xilinx primitive components.
8
--library UNISIM;
9
--use UNISIM.VComponents.all;
10
 
11
use work.cpu_pack.ALL;
12
 
13
entity cpu_engine is
14 9 jsauermann
        PORT(   -- WISHBONE interface
15
                        CLK_I   : in  std_logic;
16
                        DAT_I   : in  std_logic_vector( 7 downto 0);
17
                        DAT_O   : out std_logic_vector( 7 downto 0);
18
                        RST_I   : in  std_logic;
19
                        ACK_I   : in  std_logic;
20
                        ADR_O   : out std_logic_vector(15 downto 0);
21
                        CYC_O   : out std_logic;
22
                        STB_O   : out std_logic;
23
                        TGA_O   : out std_logic_vector( 0 downto 0);              -- '1' if I/O
24
                        WE_O    : out std_logic;
25 2 jsauermann
 
26 9 jsauermann
                        INT     : in  std_logic;
27
                        HALT    : out std_logic;
28 2 jsauermann
 
29 9 jsauermann
                        -- debug signals
30
                        --
31
                        Q_PC    : out std_logic_vector(15 downto 0);
32
                        Q_OPC   : out std_logic_vector( 7 downto 0);
33
                        Q_CAT   : out op_category;
34
                        Q_IMM   : out std_logic_vector(15 downto 0);
35
                        Q_CYC   : out cycle;
36 2 jsauermann
 
37
                        -- select signals
38
                        Q_SX    : out std_logic_vector(1 downto 0);
39
                        Q_SY    : out std_logic_vector(3 downto 0);
40
                        Q_OP    : out std_logic_vector(4 downto 0);
41
                        Q_SA    : out std_logic_vector(4 downto 0);
42
                        Q_SMQ   : out std_logic;
43
 
44
                        -- write enable/select signal
45 9 jsauermann
                        Q_WE_RR : out std_logic;
46
                        Q_WE_LL : out std_logic;
47
                        Q_WE_SP : out SP_OP;
48 2 jsauermann
 
49 9 jsauermann
                        Q_RR    : out std_logic_vector(15 downto 0);
50
                        Q_LL    : out std_logic_vector(15 downto 0);
51
                        Q_SP    : out std_logic_vector(15 downto 0)
52 2 jsauermann
                );
53
end cpu_engine;
54
 
55
architecture Behavioral of cpu_engine is
56
 
57 9 jsauermann
        -- Unfortunately, the on-chip memory needs a clock to read data.
58
        -- Therefore we cannot make it wishbone compliant without a speed penalty.
59
        -- We avoid this problem by making the on-chip memory part of the CPU.
60
        -- However, as a consequence, you cannot DMA to the on-chip memory.
61
        --
62
        -- The on-chip memory is 8K, so that you can run a test SoC without external
63
        -- memory. For bigger applications, you should use external ROM and RAM and
64
        -- remove the internal memory entirely (setting EXTERN accordingly).
65
        --
66 2 jsauermann
        COMPONENT memory
67
        PORT(   CLK_I : IN  std_logic;
68
                        T2    : IN  std_logic;
69
                        CE    : IN  std_logic;
70
                        PC    : IN  std_logic_vector(15 downto 0);
71
                        ADR   : IN  std_logic_vector(15 downto 0);
72
                        WR    : IN  std_logic;
73
                        WDAT  : IN  std_logic_vector(7 downto 0);
74
 
75
                        OPC   : OUT std_logic_vector(7 downto 0);
76
                        RDAT  : OUT std_logic_vector(7 downto 0)
77
                );
78
        END COMPONENT;
79
 
80
        COMPONENT opcode_fetch
81
        PORT(   CLK_I  : IN  std_logic;
82
                        T2     : IN  std_logic;
83
                        CLR    : IN  std_logic;
84
                        CE     : IN  std_logic;
85
                        PC_OP  : IN  std_logic_vector(2 downto 0);
86
                        JDATA  : IN  std_logic_vector(15 downto 0);
87
                        RR     : IN  std_logic_vector(15 downto 0);
88
                        RDATA  : IN  std_logic_vector(7 downto 0);
89
                        PC     : OUT std_logic_vector(15 downto 0)
90
                );
91
        END COMPONENT;
92
 
93
        COMPONENT opcode_decoder
94
        PORT(   CLK_I  : IN  std_logic;
95
                        T2     : IN  std_logic;
96
                        CLR    : IN  std_logic;
97
                        CE     : IN  std_logic;
98
                        OPCODE : in std_logic_vector(7 downto 0);
99
                        OP_CYC : in cycle;
100
                        INT    : in std_logic;
101
                        RRZ    : in std_logic;
102
 
103
                        OP_CAT : out op_category;
104
 
105
                        -- select signals
106
                        D_SX    : out std_logic_vector(1 downto 0);              -- ALU select X
107
                        D_SY    : out std_logic_vector(3 downto 0);              -- ALU select Y
108
                        D_OP    : out std_logic_vector(4 downto 0);              -- ALU operation
109
                        D_SA    : out std_logic_vector(4 downto 0);              -- select address
110
                        D_SMQ   : out std_logic;
111
 
112
                        -- write enable/select signal
113
                        D_WE_RR  : out std_logic;
114
                        D_WE_LL  : out std_logic;
115
                        D_WE_SP  : out SP_OP;
116 9 jsauermann
                        D_RD_O   : out std_logic;
117
                        D_WE_O   : out std_logic;
118
                        D_LOCK   : out std_logic;
119 2 jsauermann
 
120
                        -- input/output
121 9 jsauermann
                        D_IO     : out std_logic;
122 2 jsauermann
 
123
                        PC_OP  : out std_logic_vector(2 downto 0);
124
 
125
                        LAST_M : out std_logic;
126
                        HLT    : out std_logic
127
                );
128
        END COMPONENT;
129
 
130
        COMPONENT data_core
131
        PORT(   CLK_I : in  std_logic;
132
                        T2    : in  std_logic;
133
                        CLR   : in  std_logic;
134
                        CE    : in  std_logic;
135
 
136
                        -- select signals
137
                        SX    : in  std_logic_vector( 1 downto 0);
138
                        SY    : in  std_logic_vector( 3 downto 0);
139
                        OP    : in  std_logic_vector( 4 downto 0);               -- alu op
140
                        PC    : in  std_logic_vector(15 downto 0);               -- PC
141
                        QU    : in  std_logic_vector( 3 downto 0);               -- quick operand
142
                        SA    : in  std_logic_vector(4 downto 0);                        -- select address
143
                        SMQ   : in  std_logic;                                                  -- select MQ (H/L)
144
 
145
                        -- write enable/select signal
146
                        WE_RR  : in  std_logic;
147
                        WE_LL  : in  std_logic;
148
                        WE_SP  : in  SP_OP;
149
 
150
                        IMM : in  std_logic_vector(15 downto 0);         -- immediate data
151 9 jsauermann
                        RDAT : in  std_logic_vector( 7 downto 0);                -- data from memory/IO
152
                        ADR   : out std_logic_vector(15 downto 0);               -- memory/IO address
153
                        MQ    : out std_logic_vector( 7 downto 0);               -- data to memory/IO
154 2 jsauermann
 
155
                        Q_RR  : out std_logic_vector(15 downto 0);
156
                        Q_LL  : out std_logic_vector(15 downto 0);
157
                        Q_SP  : out std_logic_vector(15 downto 0)
158
                );
159
        END COMPONENT;
160
 
161
        -- global signals
162
        signal CE      : std_logic;
163 9 jsauermann
        signal T2      : std_logic;
164 2 jsauermann
 
165
        -- memory signals
166 9 jsauermann
        signal  WDAT     : std_logic_vector(7 downto 0);
167
        signal  RDAT     : std_logic_vector(7 downto 0);
168 2 jsauermann
        signal  M_PC     : std_logic_vector(15 downto 0);
169
        signal  M_OPC    : std_logic_vector(7 downto 0);
170
 
171
        -- decoder signals
172
        --
173
        signal  D_CAT    : op_category;
174
        signal  D_OPC    : std_logic_vector(7 downto 0);
175
        signal  D_CYC    : cycle;
176
        signal  D_PC     : std_logic_vector(15 downto 0);        -- debug signal
177
        signal  D_PC_OP  : std_logic_vector( 2 downto 0);
178
        signal  D_LAST_M : std_logic;
179 9 jsauermann
        signal  D_IO     : std_logic;
180
 
181 2 jsauermann
        -- select signals
182
        signal  D_SX    : std_logic_vector(1 downto 0);
183
        signal  D_SY    : std_logic_vector(3 downto 0);
184
        signal  D_OP    : std_logic_vector(4 downto 0);
185
        signal  D_SA    : std_logic_vector(4 downto 0);
186
        signal  D_SMQ   : std_logic;
187 9 jsauermann
 
188 2 jsauermann
        -- write enable/select signals
189
        signal  D_WE_RR  : std_logic;
190
        signal  D_WE_LL  : std_logic;
191
        signal  D_WE_SP  : SP_OP;
192 9 jsauermann
        signal  D_RD_O   : std_logic;
193
        signal  D_WE_O   : std_logic;
194
        signal  D_LOCK   : std_logic;   -- first cycle
195 2 jsauermann
 
196 9 jsauermann
        signal  LM_WE    : std_logic;
197
 
198 2 jsauermann
        -- core signals
199
        --
200
        signal  C_IMM  : std_logic_vector(15 downto 0);
201
        signal  ADR    : std_logic_vector(15 downto 0);
202
 
203
        signal  C_CYC    : cycle;                                                               -- debug signal
204
        signal  C_PC     : std_logic_vector(15 downto 0);                -- debug signal
205
        signal  C_OPC    : std_logic_vector( 7 downto 0);                -- debug signal
206
        signal  C_RR     : std_logic_vector(15 downto 0);
207
 
208
        signal  RRZ      : std_logic;
209
        signal  OC_JD    : std_logic_vector(15 downto 0);
210
        signal  C_MQ     : std_logic_vector(7 downto 0);
211
 
212
        -- select signals
213
        signal  C_SX     : std_logic_vector(1 downto 0);
214
        signal  C_SY     : std_logic_vector(3 downto 0);
215
        signal  C_OP     : std_logic_vector(4 downto 0);
216
        signal  C_SA     : std_logic_vector(4 downto 0);
217
        signal  C_SMQ    : std_logic;
218
        signal  C_WE_RR  : std_logic;
219
        signal  C_WE_LL  : std_logic;
220
        signal  C_WE_SP  : SP_OP;
221
 
222
        signal XM_OPC    : std_logic_vector(7 downto 0);
223
        signal LM_OPC    : std_logic_vector(7 downto 0);
224
        signal LM_RDAT   : std_logic_vector(7 downto 0);
225 9 jsauermann
        signal XM_RDAT   : std_logic_vector(7 downto 0);
226
        signal  C_IO     : std_logic;
227
        signal  C_RD_O   : std_logic;
228
        signal  C_WE_O   : std_logic;
229 2 jsauermann
 
230 9 jsauermann
        -- signals to remember, whether the previous read cycle
231
        -- addressed internal memory or external memory
232
        --
233
        signal OPCS      : std_logic;   -- '1' if opcode from external memory
234
        signal RDATS     : std_logic;   -- '1' if data   from external memory
235
        signal EXTERN    : std_logic;   -- '1' if opcode or data from external memory
236
 
237 2 jsauermann
begin
238
 
239
        memo: memory
240
        PORT MAP(       CLK_I => CLK_I,
241 9 jsauermann
                                T2    => T2,
242 2 jsauermann
                                CE    => CE,
243
 
244
                                -- read in T1
245
                                PC    => M_PC,
246
                                OPC   => LM_OPC,
247
 
248
                                -- read or written in T2
249
                                ADR   => ADR,
250 9 jsauermann
                                WR    => LM_WE,
251
                                WDAT  => WDAT,
252 2 jsauermann
                                RDAT  => LM_RDAT
253
                        );
254
 
255
        ocf: opcode_fetch
256
         PORT MAP(      CLK_I    => CLK_I,
257 9 jsauermann
                                T2       => T2,
258
                                CLR      => RST_I,
259 2 jsauermann
                                CE       => CE,
260
                                PC_OP    => D_PC_OP,
261
                                JDATA    => OC_JD,
262
                                RR       => C_RR,
263 9 jsauermann
                                RDATA    => RDAT,
264 2 jsauermann
                                PC       => M_PC
265
                        );
266
 
267
        opdec: opcode_decoder
268
        PORT MAP(       CLK_I    => CLK_I,
269 9 jsauermann
                                T2       => T2,
270
                                CLR      => RST_I,
271 2 jsauermann
                                CE       => CE,
272
                                OPCODE  => D_OPC,
273
                                OP_CYC  => D_CYC,
274
                                INT     => INT,
275
                                RRZ     => RRZ,
276
 
277
                                OP_CAT  => D_CAT,
278 9 jsauermann
 
279 2 jsauermann
                                -- select signals
280
                                D_SX    => D_SX,
281
                                D_SY    => D_SY,
282
                                D_OP    => D_OP,
283
                                D_SA    => D_SA,
284
                                D_SMQ   => D_SMQ,
285
 
286
                                -- write enable/select signal
287
                                D_WE_RR => D_WE_RR,
288
                                D_WE_LL => D_WE_LL,
289
                                D_WE_SP => D_WE_SP,
290 9 jsauermann
                                D_RD_O  => D_RD_O,
291
                                D_WE_O  => D_WE_O,
292
                                D_LOCK  => D_LOCK,
293 2 jsauermann
 
294 9 jsauermann
                                D_IO    => D_IO,
295 2 jsauermann
 
296
                                PC_OP   => D_PC_OP,
297
                                LAST_M  => D_LAST_M,
298
                                HLT     => HALT
299
        );
300
 
301
        dcore: data_core
302
        PORT MAP(       CLK_I  => CLK_I,
303 9 jsauermann
                                T2     => T2,
304
                                CLR    => RST_I,
305 2 jsauermann
                                CE     => CE,
306
 
307
                                -- select signals
308
                                SX     => C_SX,
309
                                SY     => C_SY,
310
                                OP     => C_OP,
311
                                PC     => C_PC,
312
                                QU     => C_OPC(3 downto 0),
313
                                SA     => C_SA,
314
                                SMQ    => C_SMQ,
315
 
316
                                -- write enable/select signal
317
                                WE_RR  => C_WE_RR,
318
                                WE_LL  => C_WE_LL,
319
                                WE_SP  => C_WE_SP,
320
 
321
                                IMM    => C_IMM,
322 9 jsauermann
                                RDAT   => RDAT,
323 2 jsauermann
                                ADR    => ADR,
324 9 jsauermann
                                MQ     => WDAT,
325 2 jsauermann
 
326
                                Q_RR   => C_RR,
327
                                Q_LL   => Q_LL,
328
                                Q_SP   => Q_SP
329 9 jsauermann
                        );
330 2 jsauermann
 
331 9 jsauermann
        CE       <= ACK_I or not EXTERN;
332
        TGA_O(0) <= T2 and C_IO;
333
        WE_O     <= T2 and C_WE_O;
334
        STB_O    <= EXTERN;
335
        CYC_O    <= EXTERN;
336 2 jsauermann
 
337
        Q_RR   <= C_RR;
338
        RRZ    <= '1' when (C_RR = X"0000") else '0';
339
        OC_JD  <= M_OPC & C_IMM(7 downto 0);
340
 
341
        Q_PC   <= C_PC;
342
        Q_OPC  <= C_OPC;
343
        Q_CYC  <= C_CYC;
344
        Q_IMM  <= C_IMM;
345
 
346
        -- select signals
347
        Q_SX    <= C_SX;
348
        Q_SY    <= C_SY;
349
        Q_OP    <= C_OP;
350
        Q_SA    <= C_SA;
351
        Q_SMQ   <= C_SMQ;
352
 
353 9 jsauermann
        -- write enable/select signal (debug)
354 2 jsauermann
        Q_WE_RR <= C_WE_RR;
355
        Q_WE_LL <= C_WE_LL;
356
        Q_WE_SP <= C_WE_SP;
357
 
358 9 jsauermann
        DAT_O   <= WDAT;
359 2 jsauermann
 
360
        process(CLK_I)
361
        begin
362
                if (rising_edge(CLK_I)) then
363 9 jsauermann
                        if (RST_I = '1') then   T2 <= '0';
364
                        else                                    T2 <= not T2;
365
                        end if;
366 2 jsauermann
                end if;
367
        end process;
368
 
369 9 jsauermann
        process(T2, M_PC, ADR, C_IO)
370 2 jsauermann
        begin
371 9 jsauermann
                if (T2 = '0') then                                                                       -- opcode fetch
372
                        EXTERN <= M_PC(15) or M_PC(14) or M_PC(13);             -- 8Kx8  internal memory
373
-- A            EXTERN <= M_PC(15) or M_PC(14) or M_PC(13) or   -- 512x8 internal memory
374
-- A                              M_PC(12) or M_PC(11) or M_PC(10) or M_PC(9)
375
-- B            EXTERN <= '1';                                                                  -- no    internal memory
376
                else                                                                                            -- data or I/O
377
                        EXTERN <= (ADR(15) or ADR(14) or ADR(13) or             -- 8Kx8  internal memory
378
-- A            EXTERN <= (ADR(15) or ADR(14) or ADR(13) or             -- 512x8  internal memory
379
-- A                               ADR(12) or ADR(11) or ADR(10) or ADR(9) or
380
-- B            EXTERN <= ('1' or                                                               -- no    internal memory
381
                                          C_IO) and (C_RD_O or C_WE_O);
382 2 jsauermann
                end if;
383
        end process;
384
 
385 9 jsauermann
        -- remember whether access is to internal or to external (incl I/O) memory.
386
        -- clock read data to XM_OPCODE in T1 or to XM_RDAT in T2
387
        --
388 2 jsauermann
        process(CLK_I)
389
        begin
390
                if (rising_edge(CLK_I)) then
391 9 jsauermann
                        if (T2 = '0') then
392
                                OPCS   <= EXTERN;
393
                                XM_OPC <= DAT_I;
394
                        else
395
                                RDATS   <= EXTERN;
396
                                XM_RDAT <= DAT_I;
397 2 jsauermann
                        end if;
398
                end if;
399
        end process;
400
 
401 9 jsauermann
        M_OPC <= LM_OPC  when (OPCS = '0')  else XM_OPC;
402
        ADR_O <= M_PC    when (T2 = '0')    else ADR;
403
        RDAT  <= LM_RDAT when (RDATS = '0') else XM_RDAT;
404 2 jsauermann
 
405
        process(CLK_I)
406
        begin
407
                if (rising_edge(CLK_I)) then
408 9 jsauermann
                        if (RST_I = '1') then
409
                                D_PC    <= X"0000";
410
                                D_OPC   <= X"01";
411
                                D_CYC   <= M1;
412 2 jsauermann
 
413 9 jsauermann
                                C_PC    <= X"0000";
414
                                C_OPC   <= X"01";
415
                                C_CYC   <= M1;
416
                                C_IMM   <= X"FFFF";
417 2 jsauermann
 
418 9 jsauermann
                                C_SX    <= "00";
419
                                C_SY    <= "0000";
420
                                C_OP    <= "00000";
421
                                C_SA    <= "00000";
422
                                C_SMQ   <= '0';
423
                                C_WE_RR <= '0';
424
                                C_WE_LL <= '0';
425
                                C_WE_SP <= SP_NOP;
426
                                C_IO    <= '0';
427
                                C_RD_O  <= '0';
428
                                C_WE_O  <= '0';
429
                                LM_WE   <= '0';
430
                        elsif (CE = '1' and T2 = '1') then
431
                                C_CYC   <= D_CYC;
432
                                Q_CAT   <= D_CAT;
433
                                C_PC    <= D_PC;
434
                                C_OPC   <= D_OPC;
435
                                C_SX    <= D_SX;
436
                                C_SY    <= D_SY;
437
                                C_OP    <= D_OP;
438
                                C_SA    <= D_SA;
439
                                C_SMQ   <= D_SMQ;
440
                                C_WE_RR <= D_WE_RR;
441
                                C_WE_LL <= D_WE_LL;
442
                                C_WE_SP <= D_WE_SP;
443
                                C_IO    <= D_IO;
444
                                C_RD_O  <= D_RD_O;
445
                                C_WE_O  <= D_WE_O;
446
                                LM_WE   <= D_WE_O and not D_IO;
447 2 jsauermann
 
448 9 jsauermann
                                if (D_LAST_M = '1') then        -- D goes to M1
449
                                        -- signals valid for entire opcode...
450
                                        D_OPC <= M_OPC;
451
                                        D_PC  <= M_PC;
452
                                        D_CYC <= M1;
453
                                else
454
                                        case D_CYC is
455
                                                when M1 =>      D_CYC <= M2;    -- C goes to M1
456
                                                                        C_IMM <= X"00" & M_OPC;
457
                                                when M2 =>      D_CYC <= M3;
458
                                                                        C_IMM(15 downto 8) <= M_OPC;
459
                                                when M3 =>      D_CYC <= M4;
460
                                                when M4 =>      D_CYC <= M5;
461
                                                when M5 =>      D_CYC <= M1;
462
                                        end case;
463 2 jsauermann
                                end if;
464
                        end if;
465
                end if;
466
        end process;
467
 
468
end Behavioral;

powered by: WebSVN 2.1.0

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