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

Subversion Repositories pss

[/] [pss/] [trunk/] [pss/] [hdl/] [pss/] [zpu_uc/] [zpu_core/] [zpu_core.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 AlexAntono
-- ZPU
2
--
3
-- Copyright 2004-2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
4
-- Copyright 2008 alvieboy - Álvaro Lopes - alvieboy@alvie.com
5
-- 
6
-- The FreeBSD license
7
-- 
8
-- Redistribution and use in source and binary forms, with or without
9
-- modification, are permitted provided that the following conditions
10
-- are met:
11
-- 
12
-- 1. Redistributions of source code must retain the above copyright
13
--    notice, this list of conditions and the following disclaimer.
14
-- 2. Redistributions in binary form must reproduce the above
15
--    copyright notice, this list of conditions and the following
16
--    disclaimer in the documentation and/or other materials
17
--    provided with the distribution.
18
-- 
19
-- THIS SOFTWARE IS PROVIDED BY THE ZPU PROJECT ``AS IS'' AND ANY
20
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22
-- PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
-- ZPU PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24
-- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30
-- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-- 
32
-- The views and conclusions contained in the software and documentation
33
-- are those of the authors and should not be interpreted as representing
34
-- official policies, either expressed or implied, of the ZPU Project.
35
 
36
library IEEE;
37
use IEEE.STD_LOGIC_1164.ALL;
38
use IEEE.STD_LOGIC_UNSIGNED.ALL;
39
use IEEE.STD_LOGIC_arith.ALL;
40
use ieee.numeric_std.all;
41
 
42
library work;
43
use work.zpu_config.all;
44
use work.zpupkg.all;
45
 
46
 
47
-- mem_writeEnable - set to '1' for a single cycle to send off a write request.
48
--                   mem_write is valid only while mem_writeEnable='1'.
49
-- mem_readEnable - set to '1' for a single cycle to send off a read request.
50
-- 
51
-- mem_busy - It is illegal to send off a read/write request when mem_busy='1'.
52
--            Set to '0' when mem_read  is valid after a read request.
53
--            If it goes to '1'(busy), it is on the cycle after mem_read/writeEnable
54
--            is '1'.
55
-- mem_addr - address for read/write request
56
-- mem_read - read data. Valid only on the cycle after mem_busy='0' after 
57
--            mem_readEnable='1' for a single cycle.
58
-- mem_write - data to write
59
-- mem_writeMask - set to '1' for those bits that are to be written to memory upon
60
--                 write request
61
-- break - set to '1' when CPU hits break instruction
62
-- interrupt - set to '1' until interrupts are cleared by CPU. 
63
 
64
 
65
 
66
entity zpu_core is
67
        generic (
68
                stack_address                   : integer := 1016
69
        );
70
    Port ( clk : in std_logic;
71
                          sreset : in std_logic;
72
                          enable : in std_logic;
73
                          cpu_present : out std_logic;
74
                          pc_bo  : out std_logic_vector(31 downto 0);
75
 
76
                          mem_req : out std_logic;
77
                          mem_we : out std_logic;
78
                          mem_ack : in std_logic;
79
                          mem_read : in std_logic_vector(wordSize-1 downto 0);
80
                          mem_write : out std_logic_vector(wordSize-1 downto 0);
81
                          out_mem_addr : out std_logic_vector(maxAddrBitIncIO downto 0);
82
                          mem_writeMask: out std_logic_vector(wordBytes-1 downto 0);
83
 
84
                          interrupt : in std_logic;
85
                          interrupt_ack : out std_logic;
86
                          break_o : out std_logic;
87
                          zpu_status : out std_logic_vector(63 downto 0));
88
end zpu_core;
89
 
90
architecture behave of zpu_core is
91
 
92
type InsnType is
93
(
94
State_AddTop,
95
State_Dup,
96
State_DupStackB,
97
State_Pop,
98
State_Popdown,
99
State_Add,
100
State_Or,
101
State_And,
102
State_Store,
103
State_AddSP,
104
State_Shift,
105
State_Nop,
106
State_Im,
107
State_LoadSP,
108
State_StoreSP,
109
State_Emulate,
110
State_Load,
111
State_PushPC,
112
State_PushSP,
113
State_PopPC,
114
State_PopPCRel,
115
State_Not,
116
State_Flip,
117
State_PopSP,
118
State_Neqbranch,
119
State_Eq,
120
State_Loadb,
121
State_Mult,
122
State_Lessthan,
123
State_Lessthanorequal,
124
State_Ulessthanorequal,
125
State_Ulessthan,
126
State_Pushspadd,
127
State_Call,
128
State_Callpcrel,
129
State_Sub,
130
State_Break,
131
State_Storeb,
132
State_Interrupt,
133
State_InsnFetch
134
);
135
 
136
type StateType is
137
(
138
State_Idle, -- using first state first on the list out of paranoia 
139
State_Load2,
140
State_Popped,
141
State_LoadSP2,
142
State_LoadSP3,
143
State_AddSP2,
144
State_Fetch,
145
State_Execute,
146
State_Decode,
147
State_Decode2,
148
State_Resync,
149
 
150
State_StoreSP2,
151
State_Resync2,
152
State_Resync3,
153
State_Loadb2,
154
State_Storeb2,
155
State_Mult2,
156
State_Mult3,
157
State_Mult5,
158
State_Mult6,
159
State_Mult4,
160
State_BinaryOpResult
161
);
162
 
163
signal sp_address_default               : std_logic_vector(31 downto 0);
164
 
165
signal  pc                              : std_logic_vector(maxAddrBitIncIO downto 0);
166
signal  sp                              : std_logic_vector(maxAddrBitIncIO downto minAddrBit);
167
signal  incSp                   : std_logic_vector(maxAddrBitIncIO downto minAddrBit);
168
signal  incIncSp                        : std_logic_vector(maxAddrBitIncIO downto minAddrBit);
169
signal  decSp                   : std_logic_vector(maxAddrBitIncIO downto minAddrBit);
170
signal  stackA                  : std_logic_vector(wordSize-1 downto 0);
171
signal  binaryOpResult          : std_logic_vector(wordSize-1 downto 0);
172
signal  multResult2             : std_logic_vector(wordSize-1 downto 0);
173
signal  multResult3             : std_logic_vector(wordSize-1 downto 0);
174
signal  multResult              : std_logic_vector(wordSize-1 downto 0);
175
signal  multA           : std_logic_vector(wordSize-1 downto 0);
176
signal  multB           : std_logic_vector(wordSize-1 downto 0);
177
signal  stackB                  : std_logic_vector(wordSize-1 downto 0);
178
signal  idim_flag                       : std_logic;
179
signal  busy                            : std_logic;
180
signal mem_readEnable : std_logic;
181
signal mem_addr : std_logic_vector(maxAddrBitIncIO downto minAddrBit);
182
signal mem_delayAddr : std_logic_vector(maxAddrBitIncIO downto minAddrBit);
183
signal mem_delayReadEnable : std_logic;
184
signal mem_busy : std_logic;
185
signal decodeWord : std_logic_vector(wordSize-1 downto 0);
186
 
187
 
188
signal state : StateType;
189
signal insn : InsnType;
190
type InsnArray is array(0 to wordBytes-1) of InsnType;
191
signal decodedOpcode : InsnArray;
192
 
193
type OpcodeArray is array(0 to wordBytes-1) of std_logic_vector(7 downto 0);
194
 
195
signal opcode : OpcodeArray;
196
 
197
 
198
 
199
 
200
signal  begin_inst                      : std_logic;
201
signal trace_opcode             : std_logic_vector(7 downto 0);
202
signal  trace_pc                                : std_logic_vector(maxAddrBitIncIO downto 0);
203
signal  trace_sp                                : std_logic_vector(maxAddrBitIncIO downto minAddrBit);
204
signal  trace_topOfStack                                : std_logic_vector(wordSize-1 downto 0);
205
signal  trace_topOfStackB                               : std_logic_vector(wordSize-1 downto 0);
206
 
207
signal  out_mem_req : std_logic;
208
 
209
signal inInterrupt : std_logic;
210
 
211
-- state machine.
212
 
213
begin
214
        cpu_present <= '1';
215
        sp_address_default <= std_logic_vector(to_unsigned(stack_address, 32));
216
 
217
        pc_bo <= pc;
218
        interrupt_ack <= inInterrupt;
219
 
220
        zpu_status(maxAddrBitIncIO downto 0) <= trace_pc;
221
        --zpu_status(31) <= '1';
222
        --zpu_status(39 downto 32) <= trace_opcode;
223
        --zpu_status(40) <= '1' when (state = State_Idle) else '0';
224
        --zpu_status(62) <= '1';
225
 
226
        --traceFileGenerate:
227
   --if Generate_Trace generate
228
        --trace_file: trace port map (
229
    --          clk => clk,
230
    --          begin_inst => begin_inst,
231
    --          pc => trace_pc,
232
        --      opcode => trace_opcode,
233
        --      sp => trace_sp,
234
        --      memA => trace_topOfStack,
235
        --      memB => trace_topOfStackB,
236
        --      busy => busy,
237
        --      intsp => (others => 'U')
238
    --    );
239
        --end generate;
240
 
241
 
242
        -- the memory subsystem will tell us one cycle later whether or 
243
        -- not it is busy
244
        out_mem_addr(maxAddrBitIncIO downto minAddrBit) <= mem_addr;
245
        out_mem_addr(minAddrBit-1 downto 0) <= (others => '0');
246
        mem_req <= out_mem_req;
247
 
248
        incSp <= sp + 1;
249
        incIncSp <= sp + 2;
250
        decSp <= sp - 1;
251
 
252
        mem_busy <= out_mem_req and not mem_ack; -- '1' when the memory is busy
253
 
254
        opcodeControl:
255
        process(clk)
256
                variable tOpcode : std_logic_vector(OpCode_Size-1 downto 0);
257
                variable spOffset : std_logic_vector(4 downto 0);
258
                variable tSpOffset : std_logic_vector(4 downto 0);
259
                variable nextPC : std_logic_vector(maxAddrBitIncIO downto 0);
260
                variable tNextState : InsnType;
261
                variable tDecodedOpcode : InsnArray;
262
                variable tMultResult : std_logic_vector(wordSize*2-1 downto 0);
263
        begin
264
 
265
                if (clk'event and clk = '1') then
266
                        if sreset = '1' then
267
                                state <= State_Idle;
268
                                break_o <= '0';
269
                                sp <= sp_address_default(31 downto 2);
270
 
271
                                pc <= (others => '0');
272
                                idim_flag <= '0';
273
                                begin_inst <= '0';
274
                                mem_we <= '0';
275
                                multA <= (others => '0');
276
                                multB <= (others => '0');
277
                                mem_writeMask <= (others => '1');
278
                                out_mem_req <= '0';
279
                                mem_addr <= (others => DontCareValue);
280
                                mem_write <= (others => DontCareValue);
281
                                inInterrupt <= '0';
282
                        else
283
                                -- we must multiply unconditionally to get pipelined multiplication
284
                        tMultResult := multA * multB;
285
                        multResult3 <= multResult2;
286
                        multResult2 <= multResult;
287
                        multResult <= tMultResult(wordSize-1 downto 0);
288
 
289
 
290
                                spOffset(4):=not opcode(conv_integer(pc(byteBits-1 downto 0)))(4);
291
                                spOffset(3 downto 0):=opcode(conv_integer(pc(byteBits-1 downto 0)))(3 downto 0);
292
                                nextPC := pc + 1;
293
 
294
                                -- prepare trace snapshot
295
                                trace_opcode <= opcode(conv_integer(pc(byteBits-1 downto 0)));
296
                                --trace_pc <= pc;
297
                                trace_sp <=     sp;
298
                                trace_topOfStack <= stackA;
299
                                trace_topOfStackB <= stackB;
300
                                begin_inst <= '0';
301
 
302
                                -- we terminate the requeset as soon as we get acknowledge
303
                                if mem_ack = '1' then
304
                                        out_mem_req <= '0';
305
                                        mem_we <= '0';
306
                                end if;
307
 
308
                                if interrupt='0' then
309
                                        inInterrupt <= '0'; -- no longer in an interrupt
310
                                end if;
311
 
312
                                case state is
313
                                        when State_Idle =>
314
                                                if enable='1' then
315
                                                        state <= State_Resync;
316
                                                end if;
317
                                        -- Initial state of ZPU, fetch top of stack + first instruction 
318
                                        when State_Resync =>
319
                                                if mem_busy='0' then
320
                                                        mem_addr <= sp;
321
                                                        out_mem_req <= '1';
322
                                                        state <= State_Resync2;
323
                                                end if;
324
                                        when State_Resync2 =>
325
                                                if mem_busy='0' then
326
                                                        stackA <= mem_read;
327
                                                        mem_addr <= incSp;
328
                                                        out_mem_req <= '1';
329
                                                        state <= State_Resync3;
330
                                                end if;
331
                                        when State_Resync3 =>
332
                                                if mem_busy='0' then
333
                                                        stackB <= mem_read;
334
                                                        mem_addr <= pc(maxAddrBitIncIO downto minAddrBit);
335
                                                        out_mem_req <= '1';
336
                                                        state <= State_Decode;
337
                                                end if;
338
                                        when State_Decode =>
339
                                                if mem_busy='0' then
340
                                                        decodeWord <= mem_read;
341
                                                        state <= State_Decode2;
342
                                                end if;
343
                                        when State_Decode2 =>
344
                                                -- decode 4 instructions in parallel
345
                                                for i in 0 to wordBytes-1 loop
346
                                                        tOpcode := decodeWord((wordBytes-1-i+1)*8-1 downto (wordBytes-1-i)*8);
347
 
348
                                                        tSpOffset(4):=not tOpcode(4);
349
                                                        tSpOffset(3 downto 0):=tOpcode(3 downto 0);
350
 
351
                                                        opcode(i) <= tOpcode;
352
                                                        if (tOpcode(7 downto 7)=OpCode_Im) then
353
                                                                tNextState:=State_Im;
354
                                                        elsif (tOpcode(7 downto 5)=OpCode_StoreSP) then
355
                                                                if tSpOffset = 0 then
356
                                                                        tNextState := State_Pop;
357
                                                                elsif tSpOffset=1 then
358
                                                                        tNextState := State_PopDown;
359
                                                                else
360
                                                                        tNextState :=State_StoreSP;
361
                                                                end if;
362
                                                        elsif (tOpcode(7 downto 5)=OpCode_LoadSP) then
363
                                                                if tSpOffset = 0 then
364
                                                                        tNextState :=State_Dup;
365
                                                                elsif tSpOffset = 1 then
366
                                                                        tNextState :=State_DupStackB;
367
                                                                else
368
                                                                        tNextState :=State_LoadSP;
369
                                                                end if;
370
                                                        elsif (tOpcode(7 downto 5)=OpCode_Emulate) then
371
                                                                tNextState :=State_Emulate;
372
                                                                if tOpcode(5 downto 0)=OpCode_Neqbranch then
373
                                                                        tNextState :=State_Neqbranch;
374
                                                                elsif tOpcode(5 downto 0)=OpCode_Eq then
375
                                                                        tNextState :=State_Eq;
376
                                                                elsif tOpcode(5 downto 0)=OpCode_Lessthan then
377
                                                                        tNextState :=State_Lessthan;
378
                                                                elsif tOpcode(5 downto 0)=OpCode_Lessthanorequal then
379
                                                                        --tNextState :=State_Lessthanorequal;
380
                                                                elsif tOpcode(5 downto 0)=OpCode_Ulessthan then
381
                                                                        tNextState :=State_Ulessthan;
382
                                                                elsif tOpcode(5 downto 0)=OpCode_Ulessthanorequal then
383
                                                                        --tNextState :=State_Ulessthanorequal;
384
                                                                elsif tOpcode(5 downto 0)=OpCode_Loadb then
385
                                                                        tNextState :=State_Loadb;
386
                                                                elsif tOpcode(5 downto 0)=OpCode_Mult then
387
                                                                        tNextState :=State_Mult;
388
                                                                elsif tOpcode(5 downto 0)=OpCode_Storeb then
389
                                                                        tNextState :=State_Storeb;
390
                                                                elsif tOpcode(5 downto 0)=OpCode_Pushspadd then
391
                                                                        tNextState :=State_Pushspadd;
392
                                                                elsif tOpcode(5 downto 0)=OpCode_Callpcrel then
393
                                                                        tNextState :=State_Callpcrel;
394
                                                                elsif tOpcode(5 downto 0)=OpCode_Call then
395
                                                                        --tNextState :=State_Call;
396
                                                                elsif tOpcode(5 downto 0)=OpCode_Sub then
397
                                                                        tNextState :=State_Sub;
398
                                                                elsif tOpcode(5 downto 0)=OpCode_PopPCRel then
399
                                                                        --tNextState :=State_PopPCRel;
400
                                                                end if;
401
                                                        elsif (tOpcode(7 downto 4)=OpCode_AddSP) then
402
                                                                if tSpOffset = 0 then
403
                                                                        tNextState := State_Shift;
404
                                                                elsif tSpOffset = 1 then
405
                                                                        tNextState := State_AddTop;
406
                                                                else
407
                                                                        tNextState :=State_AddSP;
408
                                                                end if;
409
                                                        else
410
                                                                case tOpcode(3 downto 0) is
411
                                                                        when OpCode_Nop =>
412
                                                                                tNextState :=State_Nop;
413
                                                                        when OpCode_PushSP =>
414
                                                                                tNextState :=State_PushSP;
415
                                                                        when OpCode_PopPC =>
416
                                                                                tNextState :=State_PopPC;
417
                                                                        when OpCode_Add =>
418
                                                                                tNextState :=State_Add;
419
                                                                        when OpCode_Or =>
420
                                                                                tNextState :=State_Or;
421
                                                                        when OpCode_And =>
422
                                                                                tNextState :=State_And;
423
                                                                        when OpCode_Load =>
424
                                                                                tNextState :=State_Load;
425
                                                                        when OpCode_Not =>
426
                                                                                tNextState :=State_Not;
427
                                                                        when OpCode_Flip =>
428
                                                                                tNextState :=State_Flip;
429
                                                                        when OpCode_Store =>
430
                                                                                tNextState :=State_Store;
431
                                                                        when OpCode_PopSP =>
432
                                                                                tNextState :=State_PopSP;
433
                                                                        when others =>
434
                                                                                tNextState := State_Break;
435
 
436
                                                                end case;
437
                                                        end if;
438
                                                        tDecodedOpcode(i) := tNextState;
439
 
440
                                                end loop;
441
 
442
                                                insn <= tDecodedOpcode(conv_integer(pc(byteBits-1 downto 0)));
443
 
444
                                                -- once we wrap, we need to fetch
445
                                                tDecodedOpcode(0) := State_InsnFetch;
446
 
447
                                                decodedOpcode <= tDecodedOpcode;
448
                                                state <= State_Execute;
449
 
450
 
451
 
452
                                                -- Each instruction must:
453
                                                --
454
                                                -- 1. set idim_flag
455
                                                -- 2. increase pc if applicable
456
                                                -- 3. set next state if appliable
457
                                                -- 4. do it's operation
458
 
459
                                        when State_Execute =>
460
                                                insn <= decodedOpcode(conv_integer(nextPC(byteBits-1 downto 0)));
461
 
462
                                                case insn is
463
                                                        when State_InsnFetch =>
464
                                                                state <= State_Fetch;
465
                                                        when State_Im =>
466
                                                                if mem_busy='0' then
467
                                                                        begin_inst <= '1';
468
                                                                        idim_flag <= '1';
469
                                                                        pc <= pc + 1;
470
 
471
                                                                        if idim_flag='1' then
472
                                                                                stackA(wordSize-1 downto 7) <= stackA(wordSize-8 downto 0);
473
                                                                                stackA(6 downto 0) <= opcode(conv_integer(pc(byteBits-1 downto 0)))(6 downto 0);
474
                                                                        else
475
                                                                                out_mem_req <= '1';
476
                                                                                mem_we <= '1';
477
                                                                                mem_addr <= incSp;
478
                                                                                mem_write <= stackB;
479
                                                                                stackB <= stackA;
480
                                                                                sp <= decSp;
481
                                                                                for i in wordSize-1 downto 7 loop
482
                                                                                        stackA(i) <= opcode(conv_integer(pc(byteBits-1 downto 0)))(6);
483
                                                                                end loop;
484
                                                                                stackA(6 downto 0) <= opcode(conv_integer(pc(byteBits-1 downto 0)))(6 downto 0);
485
                                                                        end if;
486
                                                                else
487
                                                                        insn <= insn;
488
                                                                end if;
489
                                                        when State_StoreSP =>
490
                                                                if mem_busy='0' then
491
                                                                        begin_inst <= '1';
492
                                                                        idim_flag <= '0';
493
                                                                        state <= State_StoreSP2;
494
 
495
                                                                        out_mem_req <= '1';
496
                                                                        mem_we <= '1';
497
                                                                        mem_addr <= sp+spOffset;
498
                                                                        mem_write <= stackA;
499
                                                                        stackA <= stackB;
500
                                                                        sp <= incSp;
501
                                                                else
502
                                                                        insn <= insn;
503
                                                                end if;
504
 
505
 
506
                                                        when State_LoadSP =>
507
                                                                if mem_busy='0' then
508
                                                                        begin_inst <= '1';
509
                                                                        idim_flag <= '0';
510
                                                                        state <= State_LoadSP2;
511
 
512
                                                                        sp <= decSp;
513
                                                                        out_mem_req <= '1';
514
                                                                        mem_we <= '1';
515
                                                                        mem_addr <= incSp;
516
                                                                        mem_write <= stackB;
517
                                                                else
518
                                                                        insn <= insn;
519
                                                                end if;
520
                                                        when State_Emulate =>
521
                                                                if mem_busy='0' then
522
                                                                        begin_inst <= '1';
523
                                                                        idim_flag <= '0';
524
                                                                        sp <= decSp;
525
                                                                        out_mem_req <= '1';
526
                                                                        mem_we <= '1';
527
                                                                        mem_addr <= incSp;
528
                                                                        mem_write <= stackB;
529
                                                                        stackA <= (others => DontCareValue);
530
                                                                        stackA(maxAddrBitIncIO downto 0) <= pc + 1;
531
                                                                        stackB <= stackA;
532
 
533
                                                                        -- The emulate address is:
534
                                                                        --        98 7654 3210
535
                                                                        -- 0000 00aa aaa0 0000
536
                                                                        pc <= (others => '0');
537
                                                                        pc(9 downto 5) <= opcode(conv_integer(pc(byteBits-1 downto 0)))(4 downto 0);
538
                                                                        state <= State_Fetch;
539
                                                                else
540
                                                                        insn <= insn;
541
                                                                end if;
542
                                                        when State_Callpcrel =>
543
                                                                if mem_busy='0' then
544
                                                                        begin_inst <= '1';
545
                                                                        idim_flag <= '0';
546
                                                                        stackA <= (others => DontCareValue);
547
                                                                        stackA(maxAddrBitIncIO downto 0) <= pc + 1;
548
 
549
                                                                        pc <= pc + stackA(maxAddrBitIncIO downto 0);
550
                                                                        state <= State_Fetch;
551
                                                                else
552
                                                                        insn <= insn;
553
                                                                end if;
554
                                                        when State_Call =>
555
                                                                if mem_busy='0' then
556
                                                                        begin_inst <= '1';
557
                                                                        idim_flag <= '0';
558
                                                                        stackA <= (others => DontCareValue);
559
                                                                        stackA(maxAddrBitIncIO downto 0) <= pc + 1;
560
                                                                        pc <= stackA(maxAddrBitIncIO downto 0);
561
                                                                        state <= State_Fetch;
562
                                                                else
563
                                                                        insn <= insn;
564
                                                                end if;
565
                                                        when State_AddSP =>
566
                                                                if mem_busy='0' then
567
                                                                        begin_inst <= '1';
568
                                                                        idim_flag <= '0';
569
                                                                        state <= State_AddSP2;
570
 
571
                                                                        out_mem_req <= '1';
572
                                                                        mem_addr <= sp+spOffset;
573
                                                                else
574
                                                                        insn <= insn;
575
                                                                end if;
576
                                                        when State_PushSP =>
577
                                                                if mem_busy='0' then
578
                                                                        begin_inst <= '1';
579
                                                                        idim_flag <= '0';
580
                                                                        pc <= pc + 1;
581
 
582
                                                                        sp <= decSp;
583
                                                                        stackA <= (others => '0');
584
                                                                        stackA(maxAddrBitIncIO downto minAddrBit) <= sp;
585
                                                                        stackB <= stackA;
586
                                                                        out_mem_req <= '1';
587
                                                                        mem_we <= '1';
588
                                                                        mem_addr <= incSp;
589
                                                                        mem_write <= stackB;
590
                                                                else
591
                                                                        insn <= insn;
592
                                                                end if;
593
                                                        when State_PopPC =>
594
                                                                if mem_busy='0' then
595
                                                                        begin_inst <= '1';
596
                                                                        idim_flag <= '0';
597
                                                                        pc <= stackA(maxAddrBitIncIO downto 0);
598
                                                                        sp <= incSp;
599
 
600
                                                                        out_mem_req <= '1';
601
                                                                        mem_we <= '1';
602
                                                                        mem_addr <= incSp;
603
                                                                        mem_write <= stackB;
604
                                                                        state <= State_Resync;
605
                                                                else
606
                                                                        insn <= insn;
607
                                                                end if;
608
                                                        when State_PopPCRel =>
609
                                                                if mem_busy='0' then
610
                                                                        begin_inst <= '1';
611
                                                                        idim_flag <= '0';
612
                                                                        pc <= stackA(maxAddrBitIncIO downto 0) + pc;
613
                                                                        sp <= incSp;
614
 
615
                                                                        out_mem_req <= '1';
616
                                                                        mem_we <= '1';
617
                                                                        mem_addr <= incSp;
618
                                                                        mem_write <= stackB;
619
                                                                        state <= State_Resync;
620
                                                                else
621
                                                                        insn <= insn;
622
                                                                end if;
623
                                                        when State_Add =>
624
                                                                if mem_busy='0' then
625
                                                                        begin_inst <= '1';
626
                                                                        idim_flag <= '0';
627
                                                                        stackA <= stackA + stackB;
628
 
629
                                                                        out_mem_req <= '1';
630
                                                                        mem_addr <= incIncSp;
631
                                                                        sp <= incSp;
632
                                                                        state <= State_Popped;
633
                                                                else
634
                                                                        insn <= insn;
635
                                                                end if;
636
                                                        when State_Sub =>
637
                                                                begin_inst <= '1';
638
                                                                idim_flag <= '0';
639
                                                                binaryOpResult <= stackB - stackA;
640
                                                                state <= State_BinaryOpResult;
641
                                                        when State_Pop =>
642
                                                                if mem_busy='0' then
643
                                                                        begin_inst <= '1';
644
                                                                        idim_flag <= '0';
645
                                                                        mem_addr <= incIncSp;
646
                                                                        out_mem_req <= '1';
647
                                                                        sp <= incSp;
648
                                                                        stackA <= stackB;
649
                                                                        state <= State_Popped;
650
                                                                else
651
                                                                        insn <= insn;
652
                                                                end if;
653
                                                        when State_PopDown =>
654
                                                                if mem_busy='0' then
655
                                                                        -- PopDown leaves top of stack unchanged
656
                                                                        begin_inst <= '1';
657
                                                                        idim_flag <= '0';
658
                                                                        mem_addr <= incIncSp;
659
                                                                        out_mem_req <= '1';
660
                                                                        sp <= incSp;
661
                                                                        state <= State_Popped;
662
                                                                else
663
                                                                        insn <= insn;
664
                                                                end if;
665
                                                        when State_Or =>
666
                                                                if mem_busy='0' then
667
                                                                        begin_inst <= '1';
668
                                                                        idim_flag <= '0';
669
                                                                        stackA <= stackA or stackB;
670
                                                                        out_mem_req <= '1';
671
                                                                        mem_addr <= incIncSp;
672
                                                                        sp <= incSp;
673
                                                                        state <= State_Popped;
674
                                                                else
675
                                                                        insn <= insn;
676
                                                                end if;
677
                                                        when State_And =>
678
                                                                if mem_busy='0' then
679
                                                                        begin_inst <= '1';
680
                                                                        idim_flag <= '0';
681
 
682
                                                                        stackA <= stackA and stackB;
683
                                                                        out_mem_req <= '1';
684
                                                                        mem_addr <= incIncSp;
685
                                                                        sp <= incSp;
686
                                                                        state <= State_Popped;
687
                                                                else
688
                                                                        insn <= insn;
689
                                                                end if;
690
                                                        when State_Eq =>
691
                                                                begin_inst <= '1';
692
                                                                idim_flag <= '0';
693
 
694
                                                binaryOpResult <= (others => '0');
695
                                                if (stackA=stackB) then
696
                                                        binaryOpResult(0) <= '1';
697
                                                end if;
698
                                                                state <= State_BinaryOpResult;
699
                                        when State_Ulessthan =>
700
                                                                begin_inst <= '1';
701
                                                                idim_flag <= '0';
702
 
703
                                                binaryOpResult <= (others => '0');
704
                                                if (stackA<stackB) then
705
                                                        binaryOpResult(0) <= '1';
706
                                                end if;
707
                                                                state <= State_BinaryOpResult;
708
                                        when State_Ulessthanorequal =>
709
                                                                begin_inst <= '1';
710
                                                                idim_flag <= '0';
711
 
712
                                                binaryOpResult <= (others => '0');
713
                                                if (stackA<=stackB) then
714
                                                        binaryOpResult(0) <= '1';
715
                                                end if;
716
                                                                state <= State_BinaryOpResult;
717
                                        when State_Lessthan =>
718
                                                                begin_inst <= '1';
719
                                                                idim_flag <= '0';
720
 
721
                                                binaryOpResult <= (others => '0');
722
                                                if (ieee.std_logic_arith.signed(stackA)<ieee.std_logic_arith.signed(stackB)) then
723
                                                        binaryOpResult(0) <= '1';
724
                                                end if;
725
                                                                state <= State_BinaryOpResult;
726
                                        when State_Lessthanorequal =>
727
                                                                begin_inst <= '1';
728
                                                                idim_flag <= '0';
729
 
730
                                                binaryOpResult <= (others => '0');
731
                                                if (ieee.std_logic_arith.signed(stackA)<=ieee.std_logic_arith.signed(stackB)) then
732
                                                        binaryOpResult(0) <= '1';
733
                                                end if;
734
                                                                state <= State_BinaryOpResult;
735
                                                        when State_Load =>
736
                                                                if mem_busy='0' then
737
                                                                        begin_inst <= '1';
738
                                                                        idim_flag <= '0';
739
                                                                        state <= State_Load2;
740
 
741
                                                                        mem_addr <= stackA(maxAddrBitIncIO downto minAddrBit);
742
                                                                        out_mem_req <= '1';
743
                                                                else
744
                                                                        insn <= insn;
745
                                                                end if;
746
 
747
                                                        when State_Dup =>
748
                                                                if mem_busy='0' then
749
                                                                        begin_inst <= '1';
750
                                                                        idim_flag <= '0';
751
                                                                        pc <= pc + 1;
752
 
753
                                                                        sp <= decSp;
754
                                                                        stackB <= stackA;
755
                                                                        mem_write <= stackB;
756
                                                                        mem_addr <= incSp;
757
                                                                        out_mem_req <= '1';
758
                                                                        mem_we <= '1';
759
                                                                else
760
                                                                        insn <= insn;
761
                                                                end if;
762
                                                        when State_DupStackB =>
763
                                                                if mem_busy='0' then
764
                                                                        begin_inst <= '1';
765
                                                                        idim_flag <= '0';
766
                                                                        pc <= pc + 1;
767
 
768
                                                                        sp <= decSp;
769
                                                                        stackA <= stackB;
770
                                                                        stackB <= stackA;
771
                                                                        mem_write <= stackB;
772
                                                                        mem_addr <= incSp;
773
                                                                        out_mem_req <= '1';
774
                                                                        mem_we <= '1';
775
                                                                else
776
                                                                        insn <= insn;
777
                                                                end if;
778
                                                        when State_Store =>
779
                                                                if mem_busy='0' then
780
                                                                        begin_inst <= '1';
781
                                                                        idim_flag <= '0';
782
                                                                        pc <= pc + 1;
783
                                                                        mem_addr <= stackA(maxAddrBitIncIO downto minAddrBit);
784
                                                                        mem_write <= stackB;
785
                                                                        out_mem_req <= '1';
786
                                                                        mem_we <= '1';
787
                                                                        sp <= incIncSp;
788
                                                                        state <= State_Resync;
789
                                                                else
790
                                                                        insn <= insn;
791
                                                                end if;
792
                                                        when State_PopSP =>
793
                                                                if mem_busy='0' then
794
                                                                        begin_inst <= '1';
795
                                                                        idim_flag <= '0';
796
                                                                        pc <= pc + 1;
797
 
798
                                                                        mem_write <= stackB;
799
                                                                        mem_addr <= incSp;
800
                                                                        out_mem_req <= '1';
801
                                                                        mem_we <= '1';
802
                                                                        sp <= stackA(maxAddrBitIncIO downto minAddrBit);
803
                                                                        state <= State_Resync;
804
                                                                else
805
                                                                        insn <= insn;
806
                                                                end if;
807
                                                        when State_Nop =>
808
                                                                begin_inst <= '1';
809
                                                                idim_flag <= '0';
810
                                                                pc <= pc + 1;
811
                                                        when State_Not =>
812
                                                                begin_inst <= '1';
813
                                                                idim_flag <= '0';
814
                                                                pc <= pc + 1;
815
 
816
                                                                stackA <= not stackA;
817
                                                        when State_Flip =>
818
                                                                begin_inst <= '1';
819
                                                                idim_flag <= '0';
820
                                                                pc <= pc + 1;
821
 
822
                                                                for i in 0 to wordSize-1 loop
823
                                                                        stackA(i) <= stackA(wordSize-1-i);
824
                                                                end loop;
825
                                                        when State_AddTop =>
826
                                                                begin_inst <= '1';
827
                                                                idim_flag <= '0';
828
                                                                pc <= pc + 1;
829
 
830
                                                                stackA <= stackA + stackB;
831
                                                        when State_Shift =>
832
                                                                begin_inst <= '1';
833
                                                                idim_flag <= '0';
834
                                                                pc <= pc + 1;
835
 
836
                                                                stackA(wordSize-1 downto 1) <= stackA(wordSize-2 downto 0);
837
                                                                stackA(0) <= '0';
838
                                                        when State_Pushspadd =>
839
                                                                begin_inst <= '1';
840
                                                                idim_flag <= '0';
841
                                                                pc <= pc + 1;
842
 
843
                                                                stackA <= (others => '0');
844
                                                                stackA(maxAddrBitIncIO downto minAddrBit) <= stackA(maxAddrBitIncIO-minAddrBit downto 0)+sp;
845
                                                        when State_Neqbranch =>
846
                                                                -- branches are almost always taken as they form loops
847
                                                                begin_inst <= '1';
848
                                                                idim_flag <= '0';
849
                                                                sp <= incIncSp;
850
                                                if (stackB/=0) then
851
                                                        pc <= stackA(maxAddrBitIncIO downto 0) + pc;
852
                                                else
853
                                                        pc <= pc + 1;
854
                                                end if;
855
                                                -- need to fetch stack again.                           
856
                                                                state <= State_Resync;
857
                                        when State_Mult =>
858
                                                                begin_inst <= '1';
859
                                                                idim_flag <= '0';
860
 
861
                                                                multA <= stackA;
862
                                                                multB <= stackB;
863
                                                                state <= State_Mult2;
864
                                                        when State_Break =>
865
                                                                report "Break instruction encountered" severity failure;
866
                                                                break_o <= '1';
867
 
868
                                                        when State_Loadb =>
869
                                                                if mem_busy='0' then
870
                                                                        begin_inst <= '1';
871
                                                                        idim_flag <= '0';
872
                                                                        state <= State_Loadb2;
873
 
874
                                                                        mem_addr <= stackA(maxAddrBitIncIO downto minAddrBit);
875
                                                                        out_mem_req <= '1';
876
                                                                else
877
                                                                        insn <= insn;
878
                                                                end if;
879
                                                        when State_Storeb =>
880
                                                                if mem_busy='0' then
881
                                                                        begin_inst <= '1';
882
                                                                        idim_flag <= '0';
883
                                                                        state <= State_Storeb2;
884
 
885
                                                                        mem_addr <= stackA(maxAddrBitIncIO downto minAddrBit);
886
                                                                        out_mem_req <= '1';
887
                                                                else
888
                                                                        insn <= insn;
889
                                                                end if;
890
 
891
                                                        when others =>
892
        --                                                      sp <= (others => DontCareValue);
893
                                                                report "Illegal instruction" severity failure;
894
                                                                break_o <= '1';
895
                                                end case;
896
 
897
 
898
                                        when State_StoreSP2 =>
899
                                                if mem_busy='0' then
900
                                                        mem_addr <= incSp;
901
                                                        out_mem_req <= '1';
902
                                                        state <= State_Popped;
903
                                                end if;
904
                                        when State_LoadSP2 =>
905
                                                if mem_busy='0' then
906
                                                        state <= State_LoadSP3;
907
                                                        out_mem_req <= '1';
908
                                                        mem_addr <= sp+spOffset+1;
909
                                                end if;
910
                                        when State_LoadSP3 =>
911
                                                if mem_busy='0' then
912
                                                        pc <= pc + 1;
913
                                                        state <= State_Execute;
914
                                                        stackB <= stackA;
915
                                                        stackA <= mem_read;
916
                                                end if;
917
                                        when State_AddSP2 =>
918
                                                if mem_busy='0' then
919
                                                        pc <= pc + 1;
920
                                                        state <= State_Execute;
921
                                                        stackA <= stackA + mem_read;
922
                                                end if;
923
                                        when State_Load2 =>
924
                                                if mem_busy='0' then
925
                                                        stackA <= mem_read;
926
                                                        pc <= pc + 1;
927
                                                        state <= State_Execute;
928
                                                end if;
929
                                        when State_Loadb2 =>
930
                                                if mem_busy='0' then
931
                                                        stackA <= (others => '0');
932
                                                        stackA(7 downto 0) <= mem_read(((wordBytes-1-conv_integer(stackA(byteBits-1 downto 0)))*8+7) downto (wordBytes-1-conv_integer(stackA(byteBits-1 downto 0)))*8);
933
                                                        pc <= pc + 1;
934
                                                        state <= State_Execute;
935
                                                end if;
936
                                        when State_Storeb2 =>
937
                                                if mem_busy='0' then
938
                                                        mem_addr <= stackA(maxAddrBitIncIO downto minAddrBit);
939
                                                        mem_write <= mem_read;
940
                                                        mem_write(((wordBytes-1-conv_integer(stackA(byteBits-1 downto 0)))*8+7) downto (wordBytes-1-conv_integer(stackA(byteBits-1 downto 0)))*8) <= stackB(7 downto 0) ;
941
                                                        out_mem_req <= '1';
942
                                                        mem_we <= '1';
943
                                                        pc <= pc + 1;
944
                                                        sp <= incIncSp;
945
                                                        state <= State_Resync;
946
                                                end if;
947
                                        when State_Fetch =>
948
                                                if mem_busy='0' then
949
                                                        if interrupt='1' and inInterrupt='0' and idim_flag='0' then
950
                                                                -- We got an interrupt
951
                                                                inInterrupt <= '1';
952
 
953
                                                                sp <= decSp;
954
                                                                out_mem_req <= '1';
955
                                                                mem_we <= '1';
956
                                                                mem_addr <= incSp;
957
                                                                mem_write <= stackB;
958
                                                                stackA <= (others => DontCareValue);
959
                                                                stackA(maxAddrBitIncIO downto 0) <= pc;
960
                                                                stackB <= stackA;
961
 
962
                                                                pc <= conv_std_logic_vector(32, maxAddrBitIncIo+1); -- interrupt address
963
 
964
                                                                report "ZPU jumped to interrupt!" severity note;
965
                                                        else
966
                                                                mem_addr <= pc(maxAddrBitIncIO downto minAddrBit);
967
                                                                out_mem_req <= '1';
968
                                                                state <= State_Decode;
969
                                                        end if;
970
                                                end if;
971
                        when State_Mult2 =>
972
                                                state <= State_Mult3;
973
                        when State_Mult3 =>
974
                                                state <= State_Mult4;
975
                        when State_Mult4 =>
976
                                                state <= State_Mult5;
977
                        when State_Mult5 =>
978
                                stackA <= multResult3;
979
                                state <= State_Mult6;
980
                        when State_Mult6 =>
981
                                                if mem_busy='0' then
982
                                                        out_mem_req <= '1';
983
                                                        mem_addr <= incIncSp;
984
                                                        sp <= incSp;
985
                                                        state <= State_Popped;
986
                                                end if;
987
                                        when State_BinaryOpResult =>
988
                                                if mem_busy='0' then
989
                                                        -- NB!!!! we know that the memory isn't busy at this point!!!!
990
                                                        out_mem_req <= '1';
991
                                                        mem_addr <= incIncSp;
992
                                                        sp <= incSp;
993
                                                        stackA <= binaryOpResult;
994
                                                        state <= State_Popped;
995
                                                end if;
996
                                        when State_Popped =>
997
                                                if mem_busy='0' then
998
                                                        pc <= pc + 1;
999
                                                        stackB <= mem_read;
1000
                                                        state <= State_Execute;
1001
                                                end if;
1002
                                        when others =>
1003
        --                                      sp <= (others => DontCareValue);
1004
                                                report "Illegal state" severity failure;
1005
                                                break_o <= '1';
1006
                                end case;
1007
                        end if;
1008
                end if;
1009
        end process;
1010
 
1011
 
1012
 
1013
end behave;

powered by: WebSVN 2.1.0

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