2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.conv_std_logic_vector;
4 use ieee.std_logic_unsigned.conv_integer;
7 generic (dsize : integer := 8);
8 port ( set_clk : in std_logic;
9 trig_clk : in std_logic;
14 instruction : in std_logic_vector (dsize - 1 downto 0);
15 exec_cycle : in std_logic_vector (5 downto 0);
16 next_cycle : out std_logic_vector (5 downto 0);
17 status_reg : inout std_logic_vector (dsize - 1 downto 0);
18 inst_we_n : out std_logic;
19 ad_oe_n : out std_logic;
20 pcl_cmd : out std_logic_vector(3 downto 0);
21 pch_cmd : out std_logic_vector(3 downto 0);
22 sp_cmd : out std_logic_vector(3 downto 0);
23 acc_cmd : out std_logic_vector(3 downto 0);
24 x_cmd : out std_logic_vector(3 downto 0);
25 y_cmd : out std_logic_vector(3 downto 0);
27 ;---for parameter check purpose!!!
28 check_bit : out std_logic_vector(1 to 5)
32 architecture rtl of decoder is
34 procedure d_print(msg : string) is
36 use ieee.std_logic_textio.all;
37 variable out_l : line;
40 writeline(output, out_l);
43 ---ival : 0x0000 - 0xffff
44 function conv_hex8(ival : integer) return string is
45 variable tmp1, tmp2 : integer;
46 variable hex_chr: string (1 to 16) := "0123456789abcdef";
48 tmp2 := (ival mod 16 ** 2) / 16 ** 1;
49 tmp1 := ival mod 16 ** 1;
50 return hex_chr(tmp2 + 1) & hex_chr(tmp1 + 1);
54 --00xxx : exec cycle : T0 > T1 > T2 > T3 > T4 > T5 > T6 > T7 > T0
55 constant T0 : std_logic_vector (5 downto 0) := "-00000";
56 constant T1 : std_logic_vector (5 downto 0) := "-00001";
57 constant T2 : std_logic_vector (5 downto 0) := "-00010";
58 constant T3 : std_logic_vector (5 downto 0) := "-00011";
59 constant T4 : std_logic_vector (5 downto 0) := "-00100";
60 constant T5 : std_logic_vector (5 downto 0) := "-00101";
61 constant T6 : std_logic_vector (5 downto 0) := "-00110";
62 constant T7 : std_logic_vector (5 downto 0) := "-00111";
64 --01xxx : reset cycle : R0 > R1 > R2 > R3 > R4 > R5 > T0
65 constant R0 : std_logic_vector (5 downto 0) := "-01000";
66 constant R1 : std_logic_vector (5 downto 0) := "-01001";
67 constant R2 : std_logic_vector (5 downto 0) := "-01010";
68 constant R3 : std_logic_vector (5 downto 0) := "-01011";
69 constant R4 : std_logic_vector (5 downto 0) := "-01100";
70 constant R5 : std_logic_vector (5 downto 0) := "-01101";
72 --10xxx : nmi cycle : N0 > N1 > N2 > N3 > N4 > N5 > T0
73 constant N0 : std_logic_vector (5 downto 0) := "-10000";
74 constant N1 : std_logic_vector (5 downto 0) := "-10001";
75 constant N2 : std_logic_vector (5 downto 0) := "-10010";
76 constant N3 : std_logic_vector (5 downto 0) := "-10011";
77 constant N4 : std_logic_vector (5 downto 0) := "-10100";
78 constant N5 : std_logic_vector (5 downto 0) := "-10101";
80 --11xxx : irq cycle : I0 > I1 > I2 > I3 > I4 > I5 > T0
81 constant I0 : std_logic_vector (5 downto 0) := "-11000";
82 constant I1 : std_logic_vector (5 downto 0) := "-11001";
83 constant I2 : std_logic_vector (5 downto 0) := "-11010";
84 constant I3 : std_logic_vector (5 downto 0) := "-11011";
85 constant I4 : std_logic_vector (5 downto 0) := "-11100";
86 constant I5 : std_logic_vector (5 downto 0) := "-11101";
88 constant ERROR_CYCLE : std_logic_vector (5 downto 0) := "111111";
90 -- SR Flags (bit 7 to bit 0):
95 -- 3 D .... Decimal (use BCD for arithmetics)
96 -- 2 I .... Interrupt (IRQ disable)
99 constant st_N : integer := 7;
100 constant st_V : integer := 6;
101 constant st_B : integer := 4;
102 constant st_D : integer := 3;
103 constant st_I : integer := 2;
104 constant st_Z : integer := 1;
105 constant st_C : integer := 0;
109 main_p : process (set_clk, res_n)
111 -------------------------------------------------------------
112 -------------------------------------------------------------
113 ----------------------- comon routines ----------------------
114 -------------------------------------------------------------
115 -------------------------------------------------------------
117 ----------gate_cmd format
118 ------3 : front port oe_n
119 ------2 : front port we_n
120 ------1 : back port oe_n
121 ------0 : back port we_n
122 procedure front_oe (signal cmd : out std_logic_vector(3 downto 0);
123 val : in std_logic) is
127 procedure front_we (signal cmd : out std_logic_vector(3 downto 0);
128 val : in std_logic) is
132 procedure back_oe (signal cmd : out std_logic_vector(3 downto 0);
133 val : in std_logic) is
137 procedure back_we (signal cmd : out std_logic_vector(3 downto 0);
138 val : in std_logic) is
143 procedure fetch_inst is
147 back_oe(pcl_cmd, '0');
148 back_oe(pch_cmd, '0');
152 d_print(string'("fetch 1"));
155 ---common routine for single byte instruction.
156 procedure single_inst is
160 procedure fetch_imm is
162 d_print("immediate");
165 procedure set_nz_from_bus is
169 procedure set_nz_from_alu is
173 procedure set_nzc_from_alu is
177 --flag on/off instruction
178 procedure set_flag (int_flg : in integer; val : in std_logic) is
183 procedure set_flag0 (val : in std_logic) is
187 procedure fetch_low is
189 d_print("fetch low 2");
192 procedure abs_fetch_high is
194 d_print("abs (xy) 3");
197 procedure abs_latch_out is
201 procedure ea_x_out is
203 -----calucurate and output effective addr
206 --A.2. internal execution on memory data
216 --A.3. store operation.
227 -- A.5.8 branch operations
228 procedure a58_branch (int_flg : in integer; br_cond : in std_logic) is
232 -------------------------------------------------------------
233 -------------------------------------------------------------
234 ---------------- main state machine start.... ---------------
235 -------------------------------------------------------------
236 -------------------------------------------------------------
239 if (res_n = '0') then
240 --pc l/h is reset vector.
244 elsif (res_n'event and res_n = '1') then
249 if (set_clk'event and set_clk = '1' and res_n = '1') then
250 d_print(string'("-"));
252 if exec_cycle = T0 then
257 elsif exec_cycle = T1 or exec_cycle = T2 or exec_cycle = T3 or
258 exec_cycle = T4 or exec_cycle = T5 or exec_cycle = T6 or
262 if exec_cycle = T1 then
263 d_print("decode and execute inst: "
264 & conv_hex8(conv_integer(instruction)));
265 --grab instruction register data.
269 --imelementation is wriiten in the order of hardware manual
273 ----------------------------------------
274 --A.1. Single byte instruction.
275 ----------------------------------------
276 if instruction = conv_std_logic_vector(16#0a#, dsize) then
280 elsif instruction = conv_std_logic_vector(16#18#, dsize) then
285 elsif instruction = conv_std_logic_vector(16#d8#, dsize) then
287 set_flag (st_D, '0');
290 elsif instruction = conv_std_logic_vector(16#58#, dsize) then
293 elsif instruction = conv_std_logic_vector(16#b8#, dsize) then
296 elsif instruction = conv_std_logic_vector(16#ca#, dsize) then
302 elsif instruction = conv_std_logic_vector(16#88#, dsize) then
308 elsif instruction = conv_std_logic_vector(16#e8#, dsize) then
314 elsif instruction = conv_std_logic_vector(16#c8#, dsize) then
317 elsif instruction = conv_std_logic_vector(16#4a#, dsize) then
321 elsif instruction = conv_std_logic_vector(16#ea#, dsize) then
324 elsif instruction = conv_std_logic_vector(16#2a#, dsize) then
328 elsif instruction = conv_std_logic_vector(16#38#, dsize) then
333 elsif instruction = conv_std_logic_vector(16#f8#, dsize) then
335 set_flag (st_D, '1');
338 elsif instruction = conv_std_logic_vector(16#78#, dsize) then
340 set_flag (st_I, '1');
343 elsif instruction = conv_std_logic_vector(16#aa#, dsize) then
347 elsif instruction = conv_std_logic_vector(16#a8#, dsize) then
351 elsif instruction = conv_std_logic_vector(16#ba#, dsize) then
355 elsif instruction = conv_std_logic_vector(16#8a#, dsize) then
359 elsif instruction = conv_std_logic_vector(16#9a#, dsize) then
364 elsif instruction = conv_std_logic_vector(16#98#, dsize) then
370 ----------------------------------------
371 --A.2. internal execution on memory data
372 ----------------------------------------
373 elsif instruction = conv_std_logic_vector(16#69#, dsize) then
377 elsif instruction = conv_std_logic_vector(16#65#, dsize) then
381 elsif instruction = conv_std_logic_vector(16#75#, dsize) then
385 elsif instruction = conv_std_logic_vector(16#6d#, dsize) then
389 elsif instruction = conv_std_logic_vector(16#7d#, dsize) then
393 elsif instruction = conv_std_logic_vector(16#79#, dsize) then
397 elsif instruction = conv_std_logic_vector(16#61#, dsize) then
401 elsif instruction = conv_std_logic_vector(16#71#, dsize) then
405 elsif instruction = conv_std_logic_vector(16#29#, dsize) then
409 elsif instruction = conv_std_logic_vector(16#25#, dsize) then
413 elsif instruction = conv_std_logic_vector(16#35#, dsize) then
417 elsif instruction = conv_std_logic_vector(16#2d#, dsize) then
421 elsif instruction = conv_std_logic_vector(16#3d#, dsize) then
425 elsif instruction = conv_std_logic_vector(16#39#, dsize) then
429 elsif instruction = conv_std_logic_vector(16#21#, dsize) then
433 elsif instruction = conv_std_logic_vector(16#31#, dsize) then
437 elsif instruction = conv_std_logic_vector(16#24#, dsize) then
441 elsif instruction = conv_std_logic_vector(16#2c#, dsize) then
445 elsif instruction = conv_std_logic_vector(16#c9#, dsize) then
451 elsif instruction = conv_std_logic_vector(16#c5#, dsize) then
455 elsif instruction = conv_std_logic_vector(16#d5#, dsize) then
459 elsif instruction = conv_std_logic_vector(16#cd#, dsize) then
463 elsif instruction = conv_std_logic_vector(16#dd#, dsize) then
467 elsif instruction = conv_std_logic_vector(16#d9#, dsize) then
471 elsif instruction = conv_std_logic_vector(16#c1#, dsize) then
475 elsif instruction = conv_std_logic_vector(16#d1#, dsize) then
479 elsif instruction = conv_std_logic_vector(16#e0#, dsize) then
483 elsif instruction = conv_std_logic_vector(16#e4#, dsize) then
487 elsif instruction = conv_std_logic_vector(16#ec#, dsize) then
491 elsif instruction = conv_std_logic_vector(16#c0#, dsize) then
495 elsif instruction = conv_std_logic_vector(16#c4#, dsize) then
499 elsif instruction = conv_std_logic_vector(16#cc#, dsize) then
503 elsif instruction = conv_std_logic_vector(16#49#, dsize) then
507 elsif instruction = conv_std_logic_vector(16#45#, dsize) then
511 elsif instruction = conv_std_logic_vector(16#55#, dsize) then
515 elsif instruction = conv_std_logic_vector(16#4d#, dsize) then
519 elsif instruction = conv_std_logic_vector(16#5d#, dsize) then
523 elsif instruction = conv_std_logic_vector(16#59#, dsize) then
527 elsif instruction = conv_std_logic_vector(16#41#, dsize) then
531 elsif instruction = conv_std_logic_vector(16#51#, dsize) then
535 elsif instruction = conv_std_logic_vector(16#a9#, dsize) then
541 elsif instruction = conv_std_logic_vector(16#a5#, dsize) then
545 elsif instruction = conv_std_logic_vector(16#b5#, dsize) then
549 elsif instruction = conv_std_logic_vector(16#ad#, dsize) then
553 if exec_cycle = T3 then
557 elsif instruction = conv_std_logic_vector(16#bd#, dsize) then
561 if exec_cycle = T3 then
564 elsif exec_cycle = T4 then
567 elsif instruction = conv_std_logic_vector(16#b9#, dsize) then
571 elsif instruction = conv_std_logic_vector(16#a1#, dsize) then
575 elsif instruction = conv_std_logic_vector(16#b1#, dsize) then
579 elsif instruction = conv_std_logic_vector(16#a2#, dsize) then
585 elsif instruction = conv_std_logic_vector(16#a6#, dsize) then
589 elsif instruction = conv_std_logic_vector(16#b6#, dsize) then
593 elsif instruction = conv_std_logic_vector(16#ae#, dsize) then
597 elsif instruction = conv_std_logic_vector(16#be#, dsize) then
601 elsif instruction = conv_std_logic_vector(16#a0#, dsize) then
607 elsif instruction = conv_std_logic_vector(16#a4#, dsize) then
611 elsif instruction = conv_std_logic_vector(16#b4#, dsize) then
615 elsif instruction = conv_std_logic_vector(16#ac#, dsize) then
619 elsif instruction = conv_std_logic_vector(16#bc#, dsize) then
623 elsif instruction = conv_std_logic_vector(16#09#, dsize) then
627 elsif instruction = conv_std_logic_vector(16#05#, dsize) then
631 elsif instruction = conv_std_logic_vector(16#15#, dsize) then
635 elsif instruction = conv_std_logic_vector(16#0d#, dsize) then
639 elsif instruction = conv_std_logic_vector(16#1d#, dsize) then
643 elsif instruction = conv_std_logic_vector(16#19#, dsize) then
647 elsif instruction = conv_std_logic_vector(16#01#, dsize) then
651 elsif instruction = conv_std_logic_vector(16#11#, dsize) then
655 elsif instruction = conv_std_logic_vector(16#e9#, dsize) then
659 elsif instruction = conv_std_logic_vector(16#e5#, dsize) then
663 elsif instruction = conv_std_logic_vector(16#f5#, dsize) then
667 elsif instruction = conv_std_logic_vector(16#ed#, dsize) then
671 elsif instruction = conv_std_logic_vector(16#fd#, dsize) then
675 elsif instruction = conv_std_logic_vector(16#f9#, dsize) then
679 elsif instruction = conv_std_logic_vector(16#e1#, dsize) then
683 elsif instruction = conv_std_logic_vector(16#f1#, dsize) then
689 ----------------------------------------
690 ---A.3. store operation.
691 ----------------------------------------
692 elsif instruction = conv_std_logic_vector(16#85#, dsize) then
696 if exec_cycle = T2 then
699 elsif instruction = conv_std_logic_vector(16#95#, dsize) then
703 elsif instruction = conv_std_logic_vector(16#8d#, dsize) then
707 if exec_cycle = T3 then
710 elsif instruction = conv_std_logic_vector(16#9d#, dsize) then
714 elsif instruction = conv_std_logic_vector(16#99#, dsize) then
718 elsif instruction = conv_std_logic_vector(16#81#, dsize) then
722 elsif instruction = conv_std_logic_vector(16#91#, dsize) then
726 elsif instruction = conv_std_logic_vector(16#86#, dsize) then
730 if exec_cycle = T2 then
733 elsif instruction = conv_std_logic_vector(16#96#, dsize) then
737 elsif instruction = conv_std_logic_vector(16#8e#, dsize) then
741 elsif instruction = conv_std_logic_vector(16#84#, dsize) then
745 elsif instruction = conv_std_logic_vector(16#94#, dsize) then
749 elsif instruction = conv_std_logic_vector(16#8c#, dsize) then
754 ----------------------------------------
755 ---A.4. read-modify-write operation
756 ----------------------------------------
757 elsif instruction = conv_std_logic_vector(16#06#, dsize) then
761 elsif instruction = conv_std_logic_vector(16#16#, dsize) then
765 elsif instruction = conv_std_logic_vector(16#0e#, dsize) then
769 elsif instruction = conv_std_logic_vector(16#1e#, dsize) then
773 elsif instruction = conv_std_logic_vector(16#c6#, dsize) then
777 elsif instruction = conv_std_logic_vector(16#d6#, dsize) then
781 elsif instruction = conv_std_logic_vector(16#ce#, dsize) then
785 elsif instruction = conv_std_logic_vector(16#de#, dsize) then
789 elsif instruction = conv_std_logic_vector(16#e6#, dsize) then
793 elsif instruction = conv_std_logic_vector(16#f6#, dsize) then
797 elsif instruction = conv_std_logic_vector(16#ee#, dsize) then
801 elsif instruction = conv_std_logic_vector(16#fe#, dsize) then
805 elsif instruction = conv_std_logic_vector(16#46#, dsize) then
809 elsif instruction = conv_std_logic_vector(16#56#, dsize) then
813 elsif instruction = conv_std_logic_vector(16#4e#, dsize) then
817 elsif instruction = conv_std_logic_vector(16#5e#, dsize) then
821 elsif instruction = conv_std_logic_vector(16#26#, dsize) then
825 elsif instruction = conv_std_logic_vector(16#36#, dsize) then
829 elsif instruction = conv_std_logic_vector(16#2e#, dsize) then
833 elsif instruction = conv_std_logic_vector(16#3e#, dsize) then
837 elsif instruction = conv_std_logic_vector(16#66#, dsize) then
841 elsif instruction = conv_std_logic_vector(16#76#, dsize) then
845 elsif instruction = conv_std_logic_vector(16#6e#, dsize) then
849 elsif instruction = conv_std_logic_vector(16#7e#, dsize) then
854 ----------------------------------------
855 --A.5. miscellaneous oprations.
856 ----------------------------------------
859 elsif instruction = conv_std_logic_vector(16#08#, dsize) then
862 elsif instruction = conv_std_logic_vector(16#48#, dsize) then
865 elsif instruction = conv_std_logic_vector(16#28#, dsize) then
868 elsif instruction = conv_std_logic_vector(16#68#, dsize) then
872 ----------------------------------------
874 ----------------------------------------
875 elsif instruction = conv_std_logic_vector(16#20#, dsize) then
878 elsif instruction = conv_std_logic_vector(16#00#, dsize) then
880 ----------------------------------------
881 -- A.5.5 return from interrupt
882 ----------------------------------------
883 elsif instruction = conv_std_logic_vector(16#40#, dsize) then
885 ----------------------------------------
887 ----------------------------------------
888 elsif instruction = conv_std_logic_vector(16#4c#, dsize) then
891 elsif instruction = conv_std_logic_vector(16#6c#, dsize) then
895 ----------------------------------------
896 -- A.5.7 return from soubroutine
897 ----------------------------------------
898 elsif instruction = conv_std_logic_vector(16#60#, dsize) then
900 ----------------------------------------
901 -- A.5.8 branch operations
902 ----------------------------------------
903 elsif instruction = conv_std_logic_vector(16#90#, dsize) then
905 elsif instruction = conv_std_logic_vector(16#b0#, dsize) then
907 a58_branch (st_C, '1');
909 elsif instruction = conv_std_logic_vector(16#f0#, dsize) then
911 elsif instruction = conv_std_logic_vector(16#30#, dsize) then
913 elsif instruction = conv_std_logic_vector(16#d0#, dsize) then
915 a58_branch (st_Z, '0');
917 elsif instruction = conv_std_logic_vector(16#10#, dsize) then
919 a58_branch (st_N, '0');
921 elsif instruction = conv_std_logic_vector(16#50#, dsize) then
923 elsif instruction = conv_std_logic_vector(16#70#, dsize) then
927 ---unknown instruction!!!!
929 report "======== unknow instruction "
930 & conv_hex8(conv_integer(instruction));
931 end if; --if instruction = conv_std_logic_vector(16#0a#, dsize)
933 elsif exec_cycle = R0 then
934 d_print(string'("reset"));
945 elsif exec_cycle = R1 then
947 front_we(pch_cmd, '1');
948 back_we(pcl_cmd, '1');
950 elsif exec_cycle = R2 then
953 elsif exec_cycle = R3 then
956 elsif exec_cycle = R4 then
959 elsif exec_cycle = R5 then
962 end if; --if exec_cycle = T0 then
964 end if; --if (set_clk'event and set_clk = '1')