summaryrefslogtreecommitdiffstats
path: root/zpu/hdl/zpu3/src/zpu_pipelined.vhd
blob: 207939d025b9c6795bdf7a3bc0ce3b9dfc548c04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
-- Company: ZPU3
-- Engineer: Øyvind Harboe

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.STD_LOGIC_arith.ALL;

library zylin;
use zylin.zpu_config.all;
use zylin.zpupkg.all;


entity zpu_top is
    Port ( clk : in std_logic;
	 		  areset : in std_logic;
	 		  io_busy : in std_logic;
	 		  io_read : in std_logic_vector(7 downto 0);
	 		  io_write : out std_logic_vector(7 downto 0);
			  io_addr : out std_logic_vector(maxAddrBit downto minAddrBit);
			  io_writeEnable : out std_logic;
			  io_readEnable : out std_logic;
	 		  interrupt : in std_logic;
	 		  break : out std_logic);
end zpu_top;

architecture behave of zpu_top is

signal		readIO : std_logic;



signal memAWriteEnable : std_logic;
signal memAAddr : std_logic_vector(maxAddrBit downto minAddrBit);
signal memAWrite : std_logic_vector(wordSize-1 downto 0);
signal memARead : std_logic_vector(wordSize-1 downto 0);
signal memBWriteEnable : std_logic;
signal memBAddr : std_logic_vector(maxAddrBit downto minAddrBit);
signal memBWrite : std_logic_vector(wordSize-1 downto 0);
signal memBRead : std_logic_vector(wordSize-1 downto 0);


signal	busy 				: std_logic;

signal	begin_inst			: std_logic;



signal trace_opcode		: std_logic_vector(7 downto 0);
signal	trace_pc				: std_logic_vector(maxAddrBit downto 0);
signal	trace_sp				: std_logic_vector(maxAddrBit downto minAddrBit);
signal	trace_topOfStack				: std_logic_vector(wordSize-1 downto 0);
signal	trace_topOfStackB				: std_logic_vector(wordSize-1 downto 0);

type DecodedOpcodeType is 
(
Decoded_Stall				,
Decoded_Nop				,
Decoded_Im					,
Decoded_ImShift			,
Decoded_LoadSP				,
Decoded_StoreSP			,
Decoded_AddSP				,
Decoded_Emulate			,
Decoded_Break				,
Decoded_PushPC				,
Decoded_PushSP				,
Decoded_PopPC				,
Decoded_Add				,
Decoded_Or					,
Decoded_And				,
Decoded_Load				,
Decoded_Not				,
Decoded_Flip				,
Decoded_Store				,
Decoded_Storeb				,
Decoded_PopSP				,
Decoded_Ashiftleft 		,
Decoded_Ashiftright		,
Decoded_Lshiftright		,
Decoded_Eqbranch			,
Decoded_Neqbranch			,
Decoded_Eq					,
Decoded_Neq				,
Decoded_Loadb				,
Decoded_Lessthan			,
Decoded_Lessthanorequal	,
Decoded_Ulessthan			,
Decoded_Ulessthanorequal	,
Decoded_Duplicate			,
Decoded_Duplicate2			,
Decoded_Duplicate3			,
Decoded_MoveDown,			
Decoded_MoveDown2,			
Decoded_MoveDown3,
Decoded_Pushspadd,
Decoded_Callpcrel,
Decoded_Sub
); 


signal decode_pc : std_logic_vector(maxAddrBit downto 0);
signal decode_fetchedPC : std_logic_vector(maxAddrBit downto 0);
signal decode_fetched : std_logic;
signal decode_opcode : std_logic_vector(OpCode_Size-1 downto 0);
signal decode_opcodeWord : std_logic_vector(wordSize-1 downto 0);
signal decode_starved : std_logic;
signal decode_wordStarved : std_logic;
signal decode_willBeStarved : std_logic;
signal decode_idim_flag : std_logic;

signal execute1_stall : std_logic;
signal execute1_fetched : std_logic;
signal execute1_decodedOpcode : DecodedOpcodeType;
signal execute1_fetchedPC : std_logic_vector(maxAddrBit downto 0);
signal execute1_sp : std_logic_vector(maxAddrBit downto minAddrBit);
signal execute1_opcode : std_logic_vector(opCode_Size-1 downto 0);
signal execute1_spOffset : std_logic_vector(4 downto 0);
signal execute1_fetchPC : std_logic_vector(maxAddrBit downto 0);
signal execute1_push1 : std_logic;
signal execute1_push2 : std_logic;
signal execute1_pop1 : std_logic;
signal execute1_pop2 : std_logic;
signal execute1_antialias : std_logic;
signal execute1_savedTopOfStack : std_logic_vector(wordSize-1 downto 0);


signal load_decodedOpcode : DecodedOpcodeType;
signal load_opcode : std_logic_vector(opCode_Size-1 downto 0);
signal load_spOffset : std_logic_vector(4 downto 0);
signal load_stall : std_logic;
signal load_willBeStalled : std_logic;

signal execute2_opcode : std_logic_vector(opCode_Size-1 downto 0);
signal execute2_topOfStack : std_logic_vector(wordSize-1 downto 0);
signal execute2_addResult : std_logic_vector(wordSize-1 downto 0);
signal execute2_topOfStackB : std_logic_vector(wordSize-1 downto 0);
signal execute2_pc : std_logic_vector(maxAddrBit downto 0);
signal execute2_sp : std_logic_vector(maxAddrBit downto minAddrBit);
signal execute2_loading : std_logic;
signal execute2_loadByte : std_logic;
signal execute2_storeByte : std_logic;
signal execute2_loadingDone : std_logic;
signal execute2_decodedOpcode : DecodedOpcodeType;
signal execute2_spOffset : std_logic_vector(4 downto 0);
signal execute2_persistTopOfStack : std_logic;
signal execute2_persistTopOfStackB : std_logic;
signal execute2_resync : std_logic;
signal execute2_resync2 : std_logic;
signal execute2_resync3 : std_logic;
signal execute2_resync4 : std_logic;
signal execute2_resync5 : std_logic;
signal execute2_resync6 : std_logic;
signal execute2_resync7 : std_logic;
signal execute2_resync8 : std_logic;
signal execute2_resync9 : std_logic;
signal execute2_resync10 : std_logic;
			

begin
	traceFileGenerate:
   if Generate_Trace generate
	trace_file: trace port map (
       	clk => clk,
       	begin_inst => begin_inst,
       	pc => trace_pc,
		opcode => trace_opcode,
		sp => trace_sp,
		memA => trace_topOfStack,
		memB => trace_topOfStackB,
		busy => busy
        );
	end generate;


	memory: dualport_ram port map (
       	clk => clk,
	memAWriteEnable => memAWriteEnable,
	memAAddr => memAAddr,
	memAWrite => memAWrite,
	memARead => memARead,
	memBWriteEnable => memBWriteEnable,
	memBAddr => memBAddr,
	memBWrite => memBWrite,
	memBRead => memBRead
        );

	opcodeControl:
	process(clk, areset)
		variable compareA : signed(wordSize-1 downto 0);
		variable compareB : signed(wordSize-1 downto 0);
		variable execute1_doFetch : boolean;
	begin
		if areset = '1' then
			break <= '0';
			begin_inst <= '0';
			memAAddr <= (others => '0');
			memBAddr <= (others => '0');
			memAWriteEnable <= '0';
			memBWriteEnable <= '0';
			memAWrite <= (others => '0');
			memBWrite <= (others => '0');
			
			memBAddr <= (others => '0');
			memBWrite <= (others => '0');


			io_writeEnable <= '0';
			io_readEnable <= '0';
			io_addr <= (others => '0');
			io_write <= (others => '0');
		
			-- stage 1. Don't care since this is driven by stage2
			decode_pc <= (others => '0');
			decode_fetched <= '0';
			decode_starved <= '0';
			decode_opcode <= (others => '0');
			decode_opcodeWord  <= (others => '0');

			-- stage 2.
			execute1_antialias <= '0';
			execute1_fetchPC <= (others => '0');
			execute1_fetched <= '0';
			execute1_decodedOpcode <= Decoded_Stall; 
			execute1_sp <= (2 => '0', others => '1'); 
			execute1_push1 <= '0';
			execute1_push2 <= '0';
			execute1_pop1 <= '0';
			execute1_pop2 <= '0';
			execute1_stall <= '1';

			-- stage 3
			load_decodedOpcode <= Decoded_Stall;
			load_stall <= '1';
			load_willBeStalled <= '1';

			-- stage 4
			decode_idim_flag <= '0';
			execute2_pc <= (others => '0');
			execute2_sp <= (2 => '0', others => '1'); 
			execute2_loading <= '0';
			execute2_loadByte <= '0';
			execute2_storeByte <= '0';
			execute2_loadingDone <= '0';
			execute2_decodedOpcode <= Decoded_Stall;
			execute2_resync <= '1';
			execute2_resync2 <= '0';
			execute2_resync3 <= '0';
			execute2_resync4 <= '0';
			execute2_resync5 <= '0';
			execute2_resync6 <= '0';
			execute2_resync7 <= '0';
			execute2_resync8 <= '0';
			execute2_resync9 <= '0';
			execute2_resync10 <= '0';
			execute2_persistTopOfStack <= '0';
			execute2_persistTopOfStackB <= '0';

			-- stage 5 
			memBWriteEnable <= '0';
			
			
		elsif (clk'event and clk = '1') then
			memAWriteEnable <= '0';
			memBWriteEnable <= '0';
			io_writeEnable <= '0';
			io_readEnable <= '0';
			begin_inst <= '0';

			-- stage0: fetch
			decode_willBeStarved <= '0';
			if (decode_fetched='1') then
				-- resync #4
				decode_opcodeWord <= memARead;
				decode_pc <= decode_fetchedPC;
			elsif (decode_pc(minAddrBit-1 downto 0)=b"11") then
				decode_willBeStarved <= '1';
			else
				-- we can continue decoding.
				decode_pc <= decode_pc + 1;
			end if;
			
			-- stage 0b: move to byte..
			-- resync #5
			decode_starved <= decode_willBeStarved;
			case decode_pc(minAddrBit-1 downto 0) is
				when "00" 	=> decode_opcode <= decode_opcodeWord(31 downto 24);
				when "01" 	=> decode_opcode <= decode_opcodeWord(23 downto 16);
				when "10" 	=> decode_opcode <= decode_opcodeWord(15 downto 8);
				when others	=> decode_opcode <= decode_opcodeWord(7 downto 0);
			end case;
			
			-- stage1: decode 1
			execute1_opcode <= decode_opcode;

			execute1_spOffset(4)<=not decode_opcode(4);
			execute1_spOffset(3 downto 0)<=decode_opcode(3 downto 0);

			execute1_decodedOpcode<=Decoded_Break;

			decode_idim_flag <= '0';

			-- resync #6
			-- resync #1
			if (decode_starved = '1') then
				execute1_decodedOpcode<=Decoded_Stall;
				decode_idim_flag <= decode_idim_flag;
			elsif (decode_opcode(7 downto 7)=OpCode_Im) then
				decode_idim_flag <= '1';
				if (decode_idim_flag = '0') then
					execute1_decodedOpcode<=Decoded_Im;
				else
					execute1_decodedOpcode<=Decoded_ImShift;
				end if;
			elsif (decode_opcode(7 downto 5)=OpCode_StoreSP) then
				if (decode_opcode(4 downto 0)=b"10001") then
					execute1_decodedOpcode<=Decoded_MoveDown;
				elsif (decode_opcode(4 downto 0)=b"10010") then
					execute1_decodedOpcode<=Decoded_MoveDown2;
--				elsif (decode_opcode(4 downto 0)=b"10011") then
--					execute1_decodedOpcode<=Decoded_MoveDown3;
				else
					execute1_decodedOpcode<=Decoded_StoreSP;
				end if;
			elsif (decode_opcode(7 downto 5)=OpCode_LoadSP) then
				if (decode_opcode(4 downto 0)=b"10000") then
					execute1_decodedOpcode<=Decoded_Duplicate;
				elsif (decode_opcode(4 downto 0)=b"10001") then
					execute1_decodedOpcode<=Decoded_Duplicate2;
				elsif (decode_opcode(4 downto 0)=b"10010") then
					execute1_decodedOpcode<=Decoded_Duplicate3;
				else
					execute1_decodedOpcode<=Decoded_LoadSP;
				end if;
			elsif (decode_opcode(7 downto 5)=OpCode_Emulate) then
				execute1_decodedOpcode<=Decoded_Emulate;
				if decode_opcode(5 downto 0)=OpCode_Neqbranch then
					execute1_decodedOpcode <= Decoded_Neqbranch;
				elsif decode_opcode(5 downto 0)=OpCode_Eq then
					execute1_decodedOpcode <= Decoded_Eq;
				elsif decode_opcode(5 downto 0)=OpCode_Lessthan then
					execute1_decodedOpcode <= Decoded_Lessthan;
				elsif decode_opcode(5 downto 0)=OpCode_Ulessthan then
					execute1_decodedOpcode <= Decoded_Ulessthan;
				elsif decode_opcode(5 downto 0)=OpCode_Loadb then
					execute1_decodedOpcode <= Decoded_Loadb;
				elsif decode_opcode(5 downto 0)=OpCode_Storeb then
					execute1_decodedOpcode <= Decoded_Storeb;
				elsif decode_opcode(5 downto 0)=OpCode_Pushspadd then
					execute1_decodedOpcode <= Decoded_Pushspadd;
				elsif decode_opcode(5 downto 0)=OpCode_Callpcrel then
					execute1_decodedOpcode <= Decoded_Callpcrel;
				elsif decode_opcode(5 downto 0)=OpCode_Sub then
					execute1_decodedOpcode <= Decoded_Sub;
				end if;
			elsif (decode_opcode(7 downto 4)=OpCode_AddSP) then
				if (decode_opcode(3 downto 0) = 0) then
					execute1_decodedOpcode<=Decoded_Ashiftleft;
				elsif (decode_opcode(3 downto 0) = 1) then
--					execute1_decodedOpcode<=Decoded_AddSP;
				elsif (decode_opcode(3 downto 0) = 2) then
--					execute1_decodedOpcode<=Decoded_AddSP;
				else
					execute1_decodedOpcode<=Decoded_AddSP;
				end if;
			else
				case decode_opcode(3 downto 0) is
					when OpCode_Nop =>
						execute1_decodedOpcode<=Decoded_Nop;
					when OpCode_PushSP =>
						execute1_decodedOpcode<=Decoded_PushSP;
					when OpCode_PopPC =>
						execute1_decodedOpcode<=Decoded_PopPC;
					when OpCode_Add =>
						execute1_decodedOpcode<=Decoded_Add;
					when OpCode_Or =>
						execute1_decodedOpcode<=Decoded_Or;
					when OpCode_And =>
						execute1_decodedOpcode<=Decoded_And;
					when OpCode_Load =>
						execute1_decodedOpcode<=Decoded_Load;
					when OpCode_Not =>
						execute1_decodedOpcode<=Decoded_Not;
					when OpCode_Flip =>
						execute1_decodedOpcode<=Decoded_Flip;
					when OpCode_Store =>
						execute1_decodedOpcode<=Decoded_Store;
					when OpCode_PopSP =>
						execute1_decodedOpcode<=Decoded_PopSP;
					when others =>
						execute1_decodedOpcode<=Decoded_Break;
				end case;
			end if;


			-- stage 2: execute 1 - load stage.
			-- 
			-- the address must be known without using the value on top of the stack...
			-- resync #3
			execute1_fetched <= '0';
			decode_fetched <= execute1_fetched; -- the value in memAAddr will be valid for 1 cycle only
			decode_fetchedPC <= execute1_fetchedPC;
			
			if (execute1_fetchPC(1 downto 0)/=b"00") then
				execute1_fetchPC <= execute1_fetchPC+1;
			end if;

			execute1_push1 <= '0';
			execute1_push2 <= execute1_push1;
			execute1_pop1 <= '0';
			execute1_pop2 <= execute1_pop1;
			
			if ((execute1_push1 and execute1_push2)='1') then
				memAWrite <= execute2_topOfStack;
			else
				memAWrite <= execute2_topOfStackB;
			end if;
			
			-- resync #7
			case execute1_decodedOpcode is
				when Decoded_Neqbranch | Decoded_MoveDown3 | Decoded_Load | Decoded_Loadb | Decoded_Store | Decoded_Storeb | Decoded_Emulate | Decoded_PopSP | Decoded_PopPC| Decoded_Callpcrel =>
					execute1_stall <= '1';
				when others =>
					-- nothing...
			end case;
			
			execute1_antialias <= load_stall;
			execute1_doFetch := false;
			case execute1_decodedOpcode is
				when Decoded_PushSP | Decoded_Emulate =>
					execute1_sp <= execute1_sp - 1;
					execute1_push1 <= '1';
					execute1_doFetch := true;
				when Decoded_Duplicate3 =>
					memAWriteEnable <= ((execute1_push1 and execute1_push2) or
					                   (execute1_push1 and not execute1_pop2) or
					                   (execute1_push2 and not execute1_pop1)) and 
					                   (not execute1_antialias and not execute1_stall);
					memAAddr <= execute1_sp + 2;
					execute1_sp <= execute1_sp - 1;
					execute1_push1 <= '1';
				when Decoded_Im | Decoded_Duplicate | Decoded_Duplicate2 =>
					execute1_sp <= execute1_sp - 1;
					execute1_push1 <= '1';
					execute1_doFetch := true;
				when Decoded_LoadSP =>
					memAAddr <= execute1_sp+execute1_spOffset;
					execute1_sp <= execute1_sp - 1;
					execute1_push1 <= '1';
				when Decoded_AddSP =>
					memAAddr <= execute1_sp+execute1_spOffset;
				when Decoded_MoveDown2 =>
					execute1_sp <= execute1_sp + 1;
					execute1_pop1 <= '1';
					execute1_doFetch := true;
				when Decoded_Ulessthan | Decoded_Lessthan | Decoded_Eq | Decoded_Neqbranch | Decoded_MoveDown3 | Decoded_MoveDown | Decoded_Add | Decoded_Sub | Decoded_Or | Decoded_And | Decoded_PopPC | Decoded_StoreSP =>
					-- be afraid :-)
					memAWriteEnable <= ((execute1_push1 and execute1_push2) or
					                   (execute1_push1 and not execute1_pop2) or
					                   (execute1_push2 and not execute1_pop1)) and 
					                   (not execute1_antialias and not execute1_stall);
					memAAddr <= execute1_sp + 2;
					execute1_sp <= execute1_sp + 1;
					execute1_pop1 <= '1';
				when others =>
					execute1_doFetch := true;
			end case;

			if execute1_doFetch then
				-- resync #2
				-- some instruction that does not change the stack pointer
				-- and does not need use a memory operand.
				-- We can fetch the next word to be decoded to avoid stalls
				execute1_fetchPC <= execute1_fetchPC+1;
				memAAddr <= execute1_fetchPC(maxAddrBit downto minAddrBit);
				execute1_fetchedPC <= execute1_fetchPC;
				execute1_fetched <= '1';
			end if;
			
			
			-- stage 3: fetching memory takes 1 cycle
			-- here we also verify that we've fetched & decoded the right
			-- opcode.
			-- resync #8
			load_decodedOpcode <= execute1_decodedOpcode;
			load_opcode <= execute1_opcode;
			load_spOffset <= execute1_spOffset;
			load_stall <= execute1_stall;
			-- resync #9
			if (load_stall = '1') then
				execute2_decodedOpcode <= Decoded_Stall;
			else
				execute2_decodedOpcode <= load_decodedOpcode;
			end if;
			execute2_opcode <= load_opcode;
			execute2_spOffset <= load_spOffset;
			
			-- stage 4: execute 2 - we now have both operands. This is the
			-- main execute stage...
			begin_inst <= '1';
			trace_pc <= execute2_pc;
			trace_opcode <= execute2_opcode;
			trace_sp <=	execute2_sp;
			trace_topOfStack <=	execute2_topOfStack;
			trace_topOfStackB <=	execute2_topOfStackB;
			
			execute2_pc <= execute2_pc + 1;
			execute2_loading <= '0';
			memBWriteEnable <= '0';
			
			case execute2_decodedOpcode is
				when Decoded_PopSP =>
					execute2_sp <= execute2_topOfStack(maxAddrBit downto minAddrBit);

					memBWriteEnable <= '1';
					memBAddr <= execute2_sp + 1;
					memBWrite <= execute2_topOfStackB;
					execute2_resync <= '1';
				when Decoded_Callpcrel =>
					execute2_topOfStack <= (others => DontCareValue);
					execute2_topOfStack(maxAddrBit downto 0) <= execute2_pc + 1;
					execute2_pc <= execute2_pc + execute2_topOfStack(maxAddrBit downto 0);
					execute2_persistTopOfStack <= '1';
				when Decoded_PopPC =>
					execute2_pc <= execute2_topOfStack(maxAddrBit downto 0);
					execute2_sp <= execute2_sp + 1;

					memBWriteEnable <= '1';
					memBAddr <= execute2_sp + 1;
					memBWrite <= execute2_topOfStackB;
					execute2_resync <= '1';
				when Decoded_Emulate =>
					execute2_sp <= execute2_sp - 1;
					
					execute2_topOfStack <= (others => DontCareValue);
					execute2_topOfStack(maxAddrBit downto 0) <= execute2_pc + 1;
					execute2_topOfStackB <= execute2_topOfStack;
					
					memBWriteEnable <= '1';
					memBAddr <= execute2_sp+1;
					memBWrite <= execute2_topOfStackB;
					-- The emulate address is:
					--        98 7654 3210
					-- 0000 00aa aaa0 0000
					execute2_pc <= (others => '0');
					execute2_pc(9 downto 5) <= execute2_opcode(4 downto 0);
					execute2_persistTopOfStack <= '1';
				when Decoded_Im =>
					execute2_sp <= execute2_sp - 1;
					for i in wordSize-1 downto 7 loop
						execute2_topOfStack(i) <= execute2_opcode(6);
					end loop;
					execute2_topOfStack(6 downto 0) <= execute2_opcode(6 downto 0);
	
					execute2_topOfStackB <= execute2_topOfStack;
					memBWriteEnable <= '1';
					memBAddr <= execute2_sp + 1;
					memBWrite <= execute2_topOfStackB;
				when Decoded_ImShift =>
					execute2_topOfStack(wordSize-1 downto 7) <= execute2_topOfStack(wordSize-8 downto 0);
					execute2_topOfStack(6 downto 0) <= execute2_opcode(6 downto 0);
				when Decoded_LoadSP =>
					execute2_sp <= execute2_sp - 1;
					execute2_topOfStack <= memARead;
					execute2_topOfStackB <= execute2_topOfStack;
					memBWriteEnable <= '1';
					memBAddr <= execute2_sp + 1;
					memBWrite <= execute2_topOfStackB;
				when Decoded_Break =>
					report "Break instruction encountered" severity failure;
					break <= '1';
				when Decoded_PushSP =>
					execute2_topOfStack <= (others => DontCareValue);
					execute2_topOfStack(maxAddrBit downto minAddrBit) <= execute2_sp;

					execute2_sp <= execute2_sp - 1;
					execute2_topOfStackB <= execute2_topOfStack;
					memBWriteEnable <= '1';
					memBAddr <= execute2_sp + 1;
					memBWrite <= execute2_topOfStackB;
				when Decoded_Add =>
					execute2_sp <= execute2_sp + 1;
					execute2_topOfStack <= execute2_topOfStackB + execute2_topOfStack;
					execute2_topOfStackB <= memARead;
				when Decoded_Sub =>
					execute2_sp <= execute2_sp + 1;
					execute2_topOfStack <= execute2_topOfStackB - execute2_topOfStack;
					execute2_topOfStackB <= memARead;
				when Decoded_AddSP =>
					execute2_topOfStack <= execute2_topOfStack + memARead;
				when Decoded_Or =>
					execute2_sp <= execute2_sp + 1;
					execute2_topOfStack <= execute2_topOfStackB or execute2_topOfStack;
					execute2_topOfStackB <= memARead;
				when Decoded_And =>
					execute2_sp <= execute2_sp + 1;
					execute2_topOfStack <= execute2_topOfStackB and execute2_topOfStack;
					execute2_topOfStackB <= memARead;
				when Decoded_Load | Decoded_Loadb | Decoded_Storeb  =>
					if (execute2_topOfStack(ioBit)='1') then
						io_addr <= execute2_topOfStack(maxAddrBit downto minAddrBit);
						io_readEnable <= '1';
					else
						memAAddr <= execute2_topOfStack(maxAddrBit downto minAddrBit);
						execute1_fetched <= '0';
					end if;
					if (execute2_decodedOpcode = Decoded_Loadb) then
						execute2_loadByte <= '1';
					else
						execute2_loadByte <= '0';
					end if;
					if (execute2_decodedOpcode = Decoded_Storeb) then
						execute2_storeByte <= '1';
					else
						execute2_storebyte <= '0';
					end if;
					execute2_loading <= '1';
				when Decoded_Ashiftleft =>
					execute2_topOfStack(wordSize-1 downto 1) <= execute2_topOfStack(wordSize-2 downto 0);
					execute2_topOfStack(0) <= '0'; 
				when Decoded_MoveDown =>
					execute2_sp <= execute2_sp + 1;
					execute2_topOfStackB <= memARead;
				when Decoded_MoveDown2 =>
					execute2_sp <= execute2_sp + 1;
					execute2_topOfStack <= execute2_topOfStackB;
					execute2_topOfStackB <= execute2_topOfStack;
				when Decoded_MoveDown3 =>
					execute2_sp <= execute2_sp + 1;
					memBWriteEnable <= '1';
					memBAddr <= execute2_sp+execute2_spOffset;
					memBWrite <= execute2_topOfStack;

					execute2_topOfStack <= execute2_topOfStackB;
					execute2_topOfStackB <= memARead;
					execute2_persistTopOfStack <= '1';
				when Decoded_Duplicate =>
					execute2_topOfStackB <= execute2_topOfStack;
					execute2_sp <= execute2_sp - 1;
					memBWriteEnable <= '1';
					memBAddr <= execute2_sp + 1;
					memBWrite <= execute2_topOfStackB;
				when Decoded_Duplicate2 =>
					execute2_topOfStack <= execute2_topOfStackB;
					execute2_topOfStackB <= execute2_topOfStack;
					execute2_sp <= execute2_sp - 1;
					memBWriteEnable <= '1';
					memBAddr <= execute2_sp + 1;
					memBWrite <= execute2_topOfStackB;
				when Decoded_Duplicate3 =>
					execute2_topOfStack <= memARead;
					execute2_topOfStackB <= execute2_topOfStack;
					execute2_sp <= execute2_sp - 1;
					memBWriteEnable <= '1';
					memBAddr <= execute2_sp + 1;
					memBWrite <= execute2_topOfStackB;
				when Decoded_Pushspadd =>
					execute2_topOfStack <= (others => DontCareValue);
					execute2_topOfStack(maxAddrBit downto minAddrBit) <= execute2_sp + execute2_topOfStack(maxAddrBit-minAddrBit downto 0);
				when Decoded_Not =>
					execute2_topOfStack <= not execute2_topOfStack;
				when Decoded_Flip =>
					for i in 0 to wordSize-1 loop
						execute2_topOfStack(i) <= execute2_topOfStack(wordSize-1-i);
		  			end loop;
				when Decoded_Store =>
					execute2_sp <= execute2_sp + 2;
					if (execute2_topOfStack(ioBit)='0') then
						memBAddr <= execute2_topOfStack(maxAddrBit downto minAddrBit);
						memBWrite <= execute2_topOfStackB;
						memBWriteEnable <= '1';
					else
						io_addr <= execute2_topOfStack(maxAddrBit downto minAddrBit);
						io_write <= execute2_topOfStackB(7 downto 0);
						io_writeEnable <= '1';
					end if;
					execute2_resync <= '1';
				when Decoded_StoreSP =>
					execute2_sp <= execute2_sp + 1;
					memBWriteEnable <= '1';
					memBAddr <= execute2_sp+execute2_spOffset;
					memBWrite <= execute2_topOfStack;

					execute2_topOfStack <= execute2_topOfStackB;
					execute2_topOfStackB <= memARead;
                when Decoded_Neqbranch =>
                	execute2_sp <= execute2_sp + 2;
                	if (execute2_topOfStackB/=0) then
                		execute2_pc <= execute2_topOfStack(maxAddrBit downto 0) + execute2_pc;
                	end if;
					execute2_resync <= '1';
                when Decoded_Eq =>
                	execute2_sp <= execute2_sp + 1;
            		execute2_topOfStack <= (others => '0');
                	if (execute2_topOfStack=execute2_topOfStackB) then
                		execute2_topOfStack(0) <= '1';
                	end if;
					execute2_topOfStackB <= memARead;
                when Decoded_Ulessthan =>
                	execute2_sp <= execute2_sp + 1;
            		execute2_topOfStack <= (others => '0');
                	if (execute2_topOfStack<execute2_topOfStackB) then
                		execute2_topOfStack(0) <= '1';
                	end if;
					execute2_topOfStackB <= memARead;
                when Decoded_Lessthan =>
                	execute2_sp <= execute2_sp + 1;
            		execute2_topOfStack <= (others => '0');
            		compareA := signed(execute2_topOfStack);
            		compareB := signed(execute2_topOfStackB);
                	if (compareA<compareB) then
                		execute2_topOfStack(0) <= '1';
                	end if;
					execute2_topOfStackB <= memARead;
				when Decoded_Stall =>
					begin_inst <= '0'; 
					execute2_pc <= execute2_pc;
				when others =>
					-- nop
			end case;
			
			-- load cycle...
			execute2_loadingDone <= execute2_loading;
			if (execute2_loadingDone ='1') then
				if (execute2_topOfStack(ioBit)='1') then
					if (io_busy = '0') then
						execute2_topOfStack <= (others => '0');
						execute2_topOfStack(7 downto 0) <= io_read;
						execute2_persistTopOfStack <= '1';
					else
						execute2_loadingDone <= '1';
					end if;
				else
					if (execute2_storeByte = '1') then
						execute2_sp <= execute2_sp + 2;
						memBWriteEnable <= '1';
						memBAddr <= execute2_topOfStack(maxAddrBit downto minAddrBit);
						memBWrite <= memARead;
						case execute2_topOfStack(minAddrBit-1 downto 0) is
							when "00" 	=> memBWrite(31 downto 24) <= execute2_topOfStackB(7 downto 0);
							when "01" 	=> memBWrite(23 downto 16) <= execute2_topOfStackB(7 downto 0);
							when "10" 	=> memBWrite(15 downto 8) <= execute2_topOfStackB(7 downto 0);
							when others	=> memBWrite(7 downto 0) <= execute2_topOfStackB(7 downto 0);
						end case;
--						case execute2_topOfStack(0 downto 0) is
--							when "1" 	=> memBWrite(15 downto 8) <= execute2_topOfStackB(7 downto 0);
--							when others	=> memBWrite(7 downto 0) <= execute2_topOfStackB(7 downto 0);
--						end case;
						execute2_resync <= '1';
					elsif (execute2_loadByte = '1') then
						execute2_topOfStack <= (others => '0');
						case execute2_topOfStack(minAddrBit-1 downto 0) is
							when "00" 	=> execute2_topOfStack(7 downto 0) <= memARead(31 downto 24);
							when "01" 	=> execute2_topOfStack(7 downto 0) <= memARead(23 downto 16);
							when "10" 	=> execute2_topOfStack(7 downto 0) <= memARead(15 downto 8);
							when others	=> execute2_topOfStack(7 downto 0) <= memARead(7 downto 0);
						end case;
--						case execute2_topOfStack(0 downto 0) is
--							when "1" 	=> execute2_topOfStack(7 downto 0) <= memARead(15 downto 8);
--							when others	=> execute2_topOfStack(7 downto 0) <= memARead(7 downto 0);
--						end case;
						execute2_persistTopOfStack <= '1';
					else
						execute2_topOfStack <= memARead;
						execute2_persistTopOfStack <= '1';
					end if;
				end if;
			end if;

			-- write top of stack...
			execute2_persistTopOfStackB <= execute2_persistTopOfStack;
			if (execute2_persistTopOfStack = '1') then
				execute2_persistTopOfStack <= '0';
				memBWriteEnable <= '1';
				memBAddr <= execute2_sp;
				memBWrite <= execute2_topOfStack;
			end if;
			if (execute2_persistTopOfStackB = '1') then
				memBWriteEnable <= '1';
				memBAddr <= execute2_sp+1;
				memBWrite <= execute2_topOfStackB;
				
				execute2_resync <= '1';
			end if;
			
			-- here we resync the pipeline.
			-- a number of things have to happen on certain cycles 
			execute2_resync2 <= execute2_resync;
			execute2_resync3 <= execute2_resync2;
			execute2_resync4 <= execute2_resync3;
			execute2_resync5 <= execute2_resync4;
			execute2_resync6 <= execute2_resync5;
			execute2_resync7 <= execute2_resync6;
			execute2_resync8 <= execute2_resync7;
			execute2_resync9 <= execute2_resync8;
			execute2_resync10 <= execute2_resync9;
			
			if (execute2_resync = '1' ) then
				-- resync #1
				execute2_resync <= '0';
				decode_starved <= '1';
				memAAddr <= execute2_sp;
			end if;
			if (execute2_resync2 = '1') then
				-- resync #2
				execute1_fetchPC <= execute2_pc;
				memAAddr <= execute2_sp + 1;
			end if;
			if (execute2_resync3 = '1') then
				-- resync #3
				execute2_topOfStack <= memARead;
			end if;
			if (execute2_resync4 = '1') then
				-- resync #4
				-- during this cycle the address is set to the opcode
				execute2_topOfStackB <= memARead;
			end if;
			if (execute2_resync5 = '1') then
				-- resync #5
				execute1_pop1 <= '0';
				execute1_push1 <= '0';
			end if;
			if (execute2_resync6 = '1') then
				-- resync #6
				decode_idim_flag <= '0';
				execute1_pop1 <= '0';
				execute1_push1 <= '0';
			end if;
			if (execute2_resync7 = '1') then
				-- resync #7
				execute1_sp <= execute2_sp;
				execute1_stall <= '0';
			end if;
			if (execute2_resync8 = '1') then
				-- resync #8
--				load_stall <= '0';
			end if;
			if (execute2_resync9 = '1') then
				-- resync #9
			end if;
			if (execute2_resync10 = '1') then
			end if;
			
			


		end if;
	end process;



end behave;
OpenPOWER on IntegriCloud