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 dbuf_int_oe_n : out std_logic;
21 dl_al_we_n : out std_logic;
22 dl_ah_we_n : out std_logic;
23 dl_al_oe_n : out std_logic;
24 dl_ah_oe_n : out std_logic;
25 dl_dh_oe_n : out std_logic;
26 pcl_inc_n : out std_logic;
27 pch_inc_n : out std_logic;
28 pcl_cmd : out std_logic_vector(3 downto 0);
29 pch_cmd : out std_logic_vector(3 downto 0);
30 sp_cmd : out std_logic_vector(3 downto 0);
31 sp_oe_n : out std_logic;
32 sp_push_n : out std_logic;
33 sp_pop_n : out std_logic;
34 acc_cmd : out std_logic_vector(3 downto 0);
35 x_cmd : out std_logic_vector(3 downto 0);
36 y_cmd : out std_logic_vector(3 downto 0);
37 abs_xy_n : out std_logic;
38 ea_carry : in std_logic;
39 pg_next_n : out std_logic;
41 zp_xy_n : out std_logic;
42 rel_calc_n : out std_logic;
43 indir_n : out std_logic;
44 indir_x_n : out std_logic;
45 indir_y_n : out std_logic;
46 arith_en_n : out std_logic;
47 stat_dec_oe_n : out std_logic;
48 stat_bus_oe_n : out std_logic;
49 stat_set_flg_n : out std_logic;
50 stat_flg : out std_logic;
51 stat_bus_all_n : out std_logic;
52 stat_bus_nz_n : out std_logic;
53 stat_alu_we_n : out std_logic;
54 r_vec_oe_n : out std_logic;
55 n_vec_oe_n : out std_logic;
56 i_vec_oe_n : out std_logic;
58 ;---for parameter check purpose!!!
59 check_bit : out std_logic_vector(1 to 5)
63 architecture rtl of decoder is
65 component d_flip_flop_bit
76 procedure d_print(msg : string) is
78 use ieee.std_logic_textio.all;
79 variable out_l : line;
82 writeline(output, out_l);
85 ---ival : 0x0000 - 0xffff
86 function conv_hex8(ival : integer) return string is
87 variable tmp1, tmp2 : integer;
88 variable hex_chr: string (1 to 16) := "0123456789abcdef";
90 tmp2 := (ival mod 16 ** 2) / 16 ** 1;
91 tmp1 := ival mod 16 ** 1;
92 return hex_chr(tmp2 + 1) & hex_chr(tmp1 + 1);
96 -- bit 5 : pcl increment carry flag
97 -- bit 4,3 : cycle type: 00 normal, 01 reset , 10 nmi, 11 irq
100 --00xxx : exec cycle : T0 > T1 > T2 > T3 > T4 > T5 > T6 > T0
101 constant T0 : std_logic_vector (5 downto 0) := "000000";
102 constant T1 : std_logic_vector (5 downto 0) := "000001";
103 constant T2 : std_logic_vector (5 downto 0) := "000010";
104 constant T3 : std_logic_vector (5 downto 0) := "000011";
105 constant T4 : std_logic_vector (5 downto 0) := "000100";
106 constant T5 : std_logic_vector (5 downto 0) := "000101";
107 constant T6 : std_logic_vector (5 downto 0) := "000110";
109 --01xxx : reset cycle : R0 > R1 > R2 > R3 > R4 > R5 > T0
110 constant R0 : std_logic_vector (5 downto 0) := "001000";
111 constant R1 : std_logic_vector (5 downto 0) := "001001";
112 constant R2 : std_logic_vector (5 downto 0) := "001010";
113 constant R3 : std_logic_vector (5 downto 0) := "001011";
114 constant R4 : std_logic_vector (5 downto 0) := "001100";
115 constant R5 : std_logic_vector (5 downto 0) := "001101";
117 --10xxx : nmi cycle : T0 > N1 > N2 > N3 > N4 > N5 > T0
118 constant N1 : std_logic_vector (5 downto 0) := "010001";
119 constant N2 : std_logic_vector (5 downto 0) := "010010";
120 constant N3 : std_logic_vector (5 downto 0) := "010011";
121 constant N4 : std_logic_vector (5 downto 0) := "010100";
122 constant N5 : std_logic_vector (5 downto 0) := "010101";
124 --11xxx : irq cycle : T0 > I1 > I2 > I3 > I4 > I5 > T0
125 constant I1 : std_logic_vector (5 downto 0) := "011001";
126 constant I2 : std_logic_vector (5 downto 0) := "011010";
127 constant I3 : std_logic_vector (5 downto 0) := "011011";
128 constant I4 : std_logic_vector (5 downto 0) := "011100";
129 constant I5 : std_logic_vector (5 downto 0) := "011101";
131 constant ERROR_CYCLE : std_logic_vector (5 downto 0) := "111111";
133 -- SR Flags (bit 7 to bit 0):
138 -- 3 D .... Decimal (use BCD for arithmetics)
139 -- 2 I .... Interrupt (IRQ disable)
142 constant st_N : integer := 7;
143 constant st_V : integer := 6;
144 constant st_B : integer := 4;
145 constant st_D : integer := 3;
146 constant st_I : integer := 2;
147 constant st_Z : integer := 1;
148 constant st_C : integer := 0;
151 signal pch_inc_input : std_logic;
154 signal nmi_handled_n : std_logic;
156 -- page boundary handling
157 signal a2_abs_xy_next_cycle : std_logic_vector (5 downto 0);
158 signal a2_indir_y_next_cycle : std_logic_vector (5 downto 0);
159 signal wait_a2_abs_xy_next : std_logic;
160 signal wait_a2_indir_y_next : std_logic;
164 ---pc page next is connected to top bit of exec_cycle
165 pch_inc_input <= not exec_cycle(5);
166 pch_inc_reg : d_flip_flop_bit
167 port map(set_clk, '1', '1', '0', pch_inc_input, pch_inc_n);
169 a2_abs_xy_next_cycle <= T4 when ea_carry = '1' else
171 a2_indir_y_next_cycle <= T5 when ea_carry = '1' else
174 main_p : process (set_clk, res_n, nmi_n, a2_abs_xy_next_cycle, a2_indir_y_next_cycle)
176 -------------------------------------------------------------
177 -------------------------------------------------------------
178 ----------------------- comon routines ----------------------
179 -------------------------------------------------------------
180 -------------------------------------------------------------
182 ----------gate_cmd format
183 ------3 : front port oe_n
184 ------2 : front port we_n
185 ------1 : back port oe_n
186 ------0 : back port we_n
187 procedure front_oe (signal cmd : out std_logic_vector(3 downto 0);
188 val : in std_logic) is
192 procedure front_we (signal cmd : out std_logic_vector(3 downto 0);
193 val : in std_logic) is
197 procedure back_oe (signal cmd : out std_logic_vector(3 downto 0);
198 val : in std_logic) is
202 procedure back_we (signal cmd : out std_logic_vector(3 downto 0);
203 val : in std_logic) is
208 procedure fetch_next is
211 back_oe(pcl_cmd, '0');
212 back_oe(pch_cmd, '0');
213 back_we(pcl_cmd, '0');
214 back_we(pch_cmd, '1');
217 procedure fetch_stop is
220 back_oe(pcl_cmd, '1');
221 back_oe(pch_cmd, '1');
222 back_we(pcl_cmd, '1');
225 procedure read_status is
227 status_reg <= (others => 'Z');
228 stat_dec_oe_n <= '0';
231 procedure disable_pins is
233 --disable the last opration pins.
234 dbuf_int_oe_n <= '1';
258 stat_bus_oe_n <= '1';
259 stat_set_flg_n <= '1';
261 stat_bus_all_n <= '1';
262 stat_bus_nz_n <= '1';
263 stat_alu_we_n <= '1';
269 wait_a2_abs_xy_next <= '0';
270 wait_a2_indir_y_next <= '0';
273 procedure fetch_inst (inc_pcl : in std_logic) is
275 if instruction = conv_std_logic_vector(16#4c#, dsize) then
276 --if prior cycle is jump instruction,
277 --fetch opcode from where the latch is pointing to.
283 --fetch opcode and pcl increment.
291 pcl_inc_n <= inc_pcl;
294 d_print(string'("fetch 1"));
298 ---(along with the page boundary condition, the last
299 ---cycle is bypassed and slided to T0.)
300 procedure t0_cycle is
303 if (nmi_n = '0' and nmi_handled_n = '1') then
304 --start nmi handling...
313 ---common routine for single byte instruction.
314 procedure single_inst is
320 procedure fetch_imm is
322 d_print("immediate");
324 --send data from data bus buffer.
325 --receiver is instruction dependent.
326 dbuf_int_oe_n <= '0';
330 procedure set_nz_from_bus is
332 --status register n/z bit update.
333 stat_bus_nz_n <= '0';
336 procedure set_zc_from_alu is
338 --status register n/z bit update.
339 stat_alu_we_n <= '0';
340 stat_dec_oe_n <= '1';
341 status_reg <= "00000011";
344 procedure set_nz_from_alu is
346 --status register n/z/c bit update.
347 stat_alu_we_n <= '0';
348 stat_dec_oe_n <= '1';
349 status_reg <= "10000010";
352 procedure set_nzc_from_alu is
354 --status register n/z/c bit update.
355 stat_alu_we_n <= '0';
356 stat_dec_oe_n <= '1';
357 status_reg <= "10000011";
360 procedure set_nvz_from_alu is
362 --status register n/z/v bit update.
363 stat_alu_we_n <= '0';
364 stat_dec_oe_n <= '1';
365 status_reg <= "11000010";
368 procedure set_nvzc_from_alu is
370 stat_alu_we_n <= '0';
371 stat_dec_oe_n <= '1';
372 status_reg <= "11000011";
375 --flag on/off instruction
376 procedure set_flag (int_flg : in integer; val : in std_logic) is
378 stat_dec_oe_n <= '1';
379 stat_set_flg_n <= '0';
380 --specify which to set.
381 status_reg(7 downto int_flg + 1)
383 status_reg(int_flg - 1 downto 0)
385 status_reg(int_flg) <= '1';
390 procedure set_flag0 (val : in std_logic) is
392 stat_dec_oe_n <= '1';
393 stat_set_flg_n <= '0';
394 status_reg <= "00000001";
398 procedure fetch_low is
400 d_print("fetch low 2");
401 --fetch next opcode (abs low).
403 --latch abs low data.
404 dbuf_int_oe_n <= '0';
409 procedure abs_fetch_high is
411 d_print("abs (xy) 3");
416 dbuf_int_oe_n <= '0';
421 procedure abs_latch_out is
432 procedure ea_x_out is
434 -----calucurate and output effective addr
439 procedure ea_y_out is
445 --A.2. internal execution on memory data
449 if exec_cycle = T1 then
451 elsif exec_cycle = T2 then
453 dbuf_int_oe_n <= '0';
465 if exec_cycle = T1 then
467 elsif exec_cycle = T2 then
469 elsif exec_cycle = T3 then
471 dbuf_int_oe_n <= '0';
476 procedure a2_page_next is
478 --close open gate if page boundary crossed.
479 back_we(acc_cmd, '1');
480 front_we(acc_cmd, '1');
481 front_we(x_cmd, '1');
482 front_we(y_cmd, '1');
485 procedure a2_abs_xy (is_x : in boolean) is
487 if exec_cycle = T1 then
489 elsif exec_cycle = T2 then
491 elsif exec_cycle = T3 then
495 if (is_x = true) then
500 dbuf_int_oe_n <= '0';
502 wait_a2_abs_xy_next <= '1';
503 next_cycle <= a2_abs_xy_next_cycle;
504 d_print("absx step 1");
505 elsif exec_cycle = T4 then
506 --case page boundary crossed.
508 d_print("absx 5 (page boudary crossed.)");
510 pg_next_n <= not ea_carry;
515 procedure a2_zp_xy (is_x : in boolean) is
517 if exec_cycle = T1 then
519 elsif exec_cycle = T2 then
522 dbuf_int_oe_n <= '0';
529 elsif exec_cycle = T3 then
532 if (is_x = true) then
541 procedure a2_indir_y is
543 if exec_cycle = T1 then
548 elsif exec_cycle = T2 then
556 dbuf_int_oe_n <= '0';
559 elsif exec_cycle = T3 then
563 dbuf_int_oe_n <= '0';
566 elsif exec_cycle = T4 then
568 dbuf_int_oe_n <= '1';
574 dbuf_int_oe_n <= '0';
576 wait_a2_indir_y_next <= '1';
577 next_cycle <= a2_indir_y_next_cycle;
578 elsif exec_cycle = T5 then
579 --case page boundary crossed.
581 d_print("(indir), y (page boudary crossed.)");
583 pg_next_n <= not ea_carry;
588 --A.3. store operation.
592 if exec_cycle = T1 then
594 elsif exec_cycle = T2 then
596 dbuf_int_oe_n <= '1';
609 if exec_cycle = T1 then
611 elsif exec_cycle = T2 then
613 elsif exec_cycle = T3 then
615 dbuf_int_oe_n <= '1';
621 procedure a3_abs_xy (is_x : in boolean) is
623 if exec_cycle = T1 then
625 elsif exec_cycle = T2 then
627 elsif exec_cycle = T3 then
631 dbuf_int_oe_n <= '1';
632 if (is_x = true) then
638 elsif exec_cycle = T4 then
639 pg_next_n <= not ea_carry;
641 if (is_x = true) then
652 procedure a3_indir_y is
654 if exec_cycle = T1 then
659 elsif exec_cycle = T2 then
667 dbuf_int_oe_n <= '0';
670 elsif exec_cycle = T3 then
674 dbuf_int_oe_n <= '0';
677 elsif exec_cycle = T4 then
679 dbuf_int_oe_n <= '1';
687 elsif exec_cycle = T5 then
691 pg_next_n <= not ea_carry;
698 ---A.4. read-modify-write operation
702 if exec_cycle = T1 then
704 elsif exec_cycle = T2 then
706 dbuf_int_oe_n <= '1';
713 elsif exec_cycle = T3 then
716 --keep data in the alu reg.
718 dbuf_int_oe_n <= '0';
720 elsif exec_cycle = T4 then
721 dbuf_int_oe_n <= '1';
723 --t5 cycle writes modified value.
735 if exec_cycle = T1 then
737 elsif exec_cycle = T2 then
739 elsif exec_cycle = T3 then
740 --T3 cycle do nothing.
743 elsif exec_cycle = T4 then
746 --t4 cycle save data in the alu register only.
747 --hardware maunual says write original data,
748 --but this implementation doesn't write because bus shortage....
751 elsif exec_cycle = T5 then
752 dbuf_int_oe_n <= '1';
754 --t5 cycle writes modified value.
761 procedure a4_abs_x is
763 if exec_cycle = T1 then
765 elsif exec_cycle = T2 then
767 elsif exec_cycle = T3 then
768 --T3 cycle discarded.
772 dbuf_int_oe_n <= '0';
775 elsif exec_cycle = T4 then
776 --t4 cycle fetch only.
779 pg_next_n <= not ea_carry;
782 elsif exec_cycle = T5 then
783 --t4 cycle redo fetch and save data in the alu register only.
787 elsif exec_cycle = T6 then
788 --t5 cycle writes modified value.
791 dbuf_int_oe_n <= '1';
799 procedure a51_push is
801 if exec_cycle = T1 then
804 elsif exec_cycle = T2 then
805 back_oe(sp_cmd, '0');
806 back_we(sp_cmd, '0');
815 procedure a52_pull is
817 if exec_cycle = T1 then
821 elsif exec_cycle = T2 then
822 --stack decrement first.
823 back_oe(sp_cmd, '0');
824 back_we(sp_cmd, '0');
829 elsif exec_cycle = T3 then
831 back_we(sp_cmd, '1');
833 ---pop data from stack.
834 back_oe(sp_cmd, '0');
836 dbuf_int_oe_n <= '0';
842 -- A.5.8 branch operations
844 procedure a58_branch (int_flg : in integer; br_cond : in std_logic) is
846 if exec_cycle = T1 then
848 if status_reg(int_flg) = br_cond then
852 dbuf_int_oe_n <= '0';
856 d_print("no branch");
859 elsif exec_cycle = T2 then
862 dbuf_int_oe_n <= '1';
865 --calc relative addr.
869 back_oe(pcl_cmd, '0');
870 back_oe(pch_cmd, '0');
871 back_we(pcl_cmd, '0');
874 elsif exec_cycle = T3 then
875 if ea_carry = '1' then
876 d_print("page crossed.");
877 --page crossed. adh calc.
878 back_we(pcl_cmd, '1');
879 back_oe(pcl_cmd, '0');
880 back_oe(pch_cmd, '0');
881 back_we(pch_cmd, '0');
885 pg_next_n <= not ea_carry;
889 --fetch cycle is done.
895 -------------------------------------------------------------
896 -------------------------------------------------------------
897 ---------------- main state machine start.... ---------------
898 -------------------------------------------------------------
899 -------------------------------------------------------------
902 if (res_n = '0') then
903 --pc l/h is reset vector.
907 elsif (res_n'event and res_n = '1') then
912 if (nmi_n'event and nmi_n = '1') then
913 --reset nmi handle status
914 nmi_handled_n <= '1';
918 if (a2_abs_xy_next_cycle'event) then
919 if (wait_a2_abs_xy_next = '1') then
920 d_print("absx step 2");
921 next_cycle <= a2_abs_xy_next_cycle;
922 if (ea_carry = '1') then
928 if (a2_indir_y_next_cycle'event) then
929 if (wait_a2_indir_y_next = '1') then
930 next_cycle <= a2_indir_y_next_cycle;
931 if (ea_carry = '1') then
937 if (set_clk'event and set_clk = '1' and res_n = '1') then
938 d_print(string'("-"));
940 if exec_cycle = T0 then
944 elsif exec_cycle = T1 or exec_cycle = T2 or exec_cycle = T3 or
945 exec_cycle = T4 or exec_cycle = T5 or exec_cycle = T6 then
948 ---asyncronous page change might happen.
949 back_we(pch_cmd, '1');
951 if exec_cycle = T1 then
952 d_print("decode and execute inst: "
953 & conv_hex8(conv_integer(instruction)));
954 --disable pin for jmp instruction
956 back_we(pcl_cmd, '1');
957 front_we(pch_cmd, '1');
959 --grab instruction register data.
963 --imelementation is wriiten in the order of hardware manual
966 ----------------------------------------
967 --A.1. Single byte instruction.
968 ----------------------------------------
969 if instruction = conv_std_logic_vector(16#0a#, dsize) then
973 back_oe(acc_cmd, '0');
974 front_we(acc_cmd, '0');
978 elsif instruction = conv_std_logic_vector(16#18#, dsize) then
983 elsif instruction = conv_std_logic_vector(16#d8#, dsize) then
985 set_flag (st_D, '0');
988 elsif instruction = conv_std_logic_vector(16#58#, dsize) then
990 set_flag (st_I, '0');
993 elsif instruction = conv_std_logic_vector(16#b8#, dsize) then
995 set_flag (st_V, '0');
998 elsif instruction = conv_std_logic_vector(16#ca#, dsize) then
1001 back_oe(x_cmd, '0');
1002 front_we(x_cmd, '0');
1007 elsif instruction = conv_std_logic_vector(16#88#, dsize) then
1010 back_oe(y_cmd, '0');
1011 front_we(y_cmd, '0');
1016 elsif instruction = conv_std_logic_vector(16#e8#, dsize) then
1019 back_oe(x_cmd, '0');
1020 front_we(x_cmd, '0');
1025 elsif instruction = conv_std_logic_vector(16#c8#, dsize) then
1028 back_oe(y_cmd, '0');
1029 front_we(y_cmd, '0');
1033 elsif instruction = conv_std_logic_vector(16#4a#, dsize) then
1037 back_oe(acc_cmd, '0');
1038 front_we(acc_cmd, '0');
1042 elsif instruction = conv_std_logic_vector(16#ea#, dsize) then
1046 elsif instruction = conv_std_logic_vector(16#2a#, dsize) then
1050 back_oe(acc_cmd, '0');
1051 front_we(acc_cmd, '0');
1055 elsif instruction = conv_std_logic_vector(16#6a#, dsize) then
1059 back_oe(acc_cmd, '0');
1060 front_we(acc_cmd, '0');
1064 elsif instruction = conv_std_logic_vector(16#38#, dsize) then
1069 elsif instruction = conv_std_logic_vector(16#f8#, dsize) then
1071 set_flag (st_D, '1');
1074 elsif instruction = conv_std_logic_vector(16#78#, dsize) then
1076 set_flag (st_I, '1');
1079 elsif instruction = conv_std_logic_vector(16#aa#, dsize) then
1083 front_oe(acc_cmd, '0');
1084 front_we(x_cmd, '0');
1086 elsif instruction = conv_std_logic_vector(16#a8#, dsize) then
1090 front_oe(acc_cmd, '0');
1091 front_we(y_cmd, '0');
1093 elsif instruction = conv_std_logic_vector(16#ba#, dsize) then
1097 elsif instruction = conv_std_logic_vector(16#8a#, dsize) then
1101 front_oe(x_cmd, '0');
1102 front_we(acc_cmd, '0');
1104 elsif instruction = conv_std_logic_vector(16#9a#, dsize) then
1108 front_oe(x_cmd, '0');
1109 front_we(sp_cmd, '0');
1111 elsif instruction = conv_std_logic_vector(16#98#, dsize) then
1115 front_oe(y_cmd, '0');
1116 front_we(acc_cmd, '0');
1120 ----------------------------------------
1121 --A.2. internal execution on memory data
1122 ----------------------------------------
1123 elsif instruction = conv_std_logic_vector(16#69#, dsize) then
1128 back_oe(acc_cmd, '0');
1129 back_we(acc_cmd, '0');
1132 elsif instruction = conv_std_logic_vector(16#65#, dsize) then
1136 if exec_cycle = T2 then
1138 back_oe(acc_cmd, '0');
1139 back_we(acc_cmd, '0');
1143 elsif instruction = conv_std_logic_vector(16#75#, dsize) then
1147 if exec_cycle = T3 then
1149 back_oe(acc_cmd, '0');
1150 back_we(acc_cmd, '0');
1154 elsif instruction = conv_std_logic_vector(16#6d#, dsize) then
1158 if exec_cycle = T3 then
1160 back_oe(acc_cmd, '0');
1161 back_we(acc_cmd, '0');
1165 elsif instruction = conv_std_logic_vector(16#7d#, dsize) then
1169 if exec_cycle = T3 or exec_cycle = T4 then
1171 back_oe(acc_cmd, '0');
1172 back_we(acc_cmd, '0');
1176 elsif instruction = conv_std_logic_vector(16#79#, dsize) then
1180 if exec_cycle = T3 or exec_cycle = T4 then
1182 back_oe(acc_cmd, '0');
1183 back_we(acc_cmd, '0');
1187 elsif instruction = conv_std_logic_vector(16#61#, dsize) then
1191 elsif instruction = conv_std_logic_vector(16#71#, dsize) then
1195 if exec_cycle = T4 or exec_cycle = T5 then
1197 back_oe(acc_cmd, '0');
1198 back_we(acc_cmd, '0');
1202 elsif instruction = conv_std_logic_vector(16#29#, dsize) then
1207 back_oe(acc_cmd, '0');
1208 back_we(acc_cmd, '0');
1211 elsif instruction = conv_std_logic_vector(16#25#, dsize) then
1215 if exec_cycle = T2 then
1217 back_oe(acc_cmd, '0');
1218 back_we(acc_cmd, '0');
1222 elsif instruction = conv_std_logic_vector(16#35#, dsize) then
1226 if exec_cycle = T3 then
1228 back_oe(acc_cmd, '0');
1229 back_we(acc_cmd, '0');
1233 elsif instruction = conv_std_logic_vector(16#2d#, dsize) then
1237 if exec_cycle = T3 then
1239 back_oe(acc_cmd, '0');
1240 back_we(acc_cmd, '0');
1244 elsif instruction = conv_std_logic_vector(16#3d#, dsize) then
1248 if exec_cycle = T3 or exec_cycle = T4 then
1250 back_oe(acc_cmd, '0');
1251 back_we(acc_cmd, '0');
1255 elsif instruction = conv_std_logic_vector(16#39#, dsize) then
1259 if exec_cycle = T3 or exec_cycle = T4 then
1261 back_oe(acc_cmd, '0');
1262 back_we(acc_cmd, '0');
1266 elsif instruction = conv_std_logic_vector(16#21#, dsize) then
1270 elsif instruction = conv_std_logic_vector(16#31#, dsize) then
1274 if exec_cycle = T4 or exec_cycle = T5 then
1276 back_oe(acc_cmd, '0');
1277 back_we(acc_cmd, '0');
1281 elsif instruction = conv_std_logic_vector(16#24#, dsize) then
1285 if exec_cycle = T2 then
1287 back_oe(acc_cmd, '0');
1291 elsif instruction = conv_std_logic_vector(16#2c#, dsize) then
1295 if exec_cycle = T3 then
1297 back_oe(acc_cmd, '0');
1301 elsif instruction = conv_std_logic_vector(16#c9#, dsize) then
1306 back_oe(acc_cmd, '0');
1309 elsif instruction = conv_std_logic_vector(16#c5#, dsize) then
1313 if exec_cycle = T2 then
1315 back_oe(acc_cmd, '0');
1319 elsif instruction = conv_std_logic_vector(16#d5#, dsize) then
1323 elsif instruction = conv_std_logic_vector(16#cd#, dsize) then
1327 elsif instruction = conv_std_logic_vector(16#dd#, dsize) then
1331 elsif instruction = conv_std_logic_vector(16#d9#, dsize) then
1335 if exec_cycle = T3 or exec_cycle = T4 then
1337 back_oe(acc_cmd, '0');
1341 elsif instruction = conv_std_logic_vector(16#c1#, dsize) then
1345 elsif instruction = conv_std_logic_vector(16#d1#, dsize) then
1349 elsif instruction = conv_std_logic_vector(16#e0#, dsize) then
1354 back_oe(x_cmd, '0');
1357 elsif instruction = conv_std_logic_vector(16#e4#, dsize) then
1361 if exec_cycle = T2 then
1363 back_oe(x_cmd, '0');
1367 elsif instruction = conv_std_logic_vector(16#ec#, dsize) then
1371 elsif instruction = conv_std_logic_vector(16#c0#, dsize) then
1376 back_oe(y_cmd, '0');
1379 elsif instruction = conv_std_logic_vector(16#c4#, dsize) then
1383 elsif instruction = conv_std_logic_vector(16#cc#, dsize) then
1387 elsif instruction = conv_std_logic_vector(16#49#, dsize) then
1392 back_oe(acc_cmd, '0');
1393 back_we(acc_cmd, '0');
1396 elsif instruction = conv_std_logic_vector(16#45#, dsize) then
1400 if exec_cycle = T2 then
1402 back_oe(acc_cmd, '0');
1403 back_we(acc_cmd, '0');
1407 elsif instruction = conv_std_logic_vector(16#55#, dsize) then
1411 elsif instruction = conv_std_logic_vector(16#4d#, dsize) then
1415 elsif instruction = conv_std_logic_vector(16#5d#, dsize) then
1419 elsif instruction = conv_std_logic_vector(16#59#, dsize) then
1423 elsif instruction = conv_std_logic_vector(16#41#, dsize) then
1427 elsif instruction = conv_std_logic_vector(16#51#, dsize) then
1431 elsif instruction = conv_std_logic_vector(16#a9#, dsize) then
1435 front_we(acc_cmd, '0');
1438 elsif instruction = conv_std_logic_vector(16#a5#, dsize) then
1442 if exec_cycle = T2 then
1443 front_we(acc_cmd, '0');
1447 elsif instruction = conv_std_logic_vector(16#b5#, dsize) then
1451 if exec_cycle = T3 then
1452 front_we(acc_cmd, '0');
1456 elsif instruction = conv_std_logic_vector(16#ad#, dsize) then
1460 if exec_cycle = T3 then
1462 front_we(acc_cmd, '0');
1465 elsif instruction = conv_std_logic_vector(16#bd#, dsize) then
1469 if exec_cycle = T3 or exec_cycle = T4 then
1471 front_we(acc_cmd, '0');
1475 elsif instruction = conv_std_logic_vector(16#b9#, dsize) then
1479 if exec_cycle = T3 or exec_cycle = T4 then
1481 front_we(acc_cmd, '0');
1485 elsif instruction = conv_std_logic_vector(16#a1#, dsize) then
1489 elsif instruction = conv_std_logic_vector(16#b1#, dsize) then
1493 if exec_cycle = T4 or exec_cycle = T5 then
1495 front_we(acc_cmd, '0');
1499 elsif instruction = conv_std_logic_vector(16#a2#, dsize) then
1504 front_we(x_cmd, '0');
1506 elsif instruction = conv_std_logic_vector(16#a6#, dsize) then
1510 if exec_cycle = T2 then
1511 front_we(x_cmd, '0');
1515 elsif instruction = conv_std_logic_vector(16#b6#, dsize) then
1519 elsif instruction = conv_std_logic_vector(16#ae#, dsize) then
1523 if exec_cycle = T3 then
1525 front_we(x_cmd, '0');
1528 elsif instruction = conv_std_logic_vector(16#be#, dsize) then
1532 if exec_cycle = T3 or exec_cycle = T4 then
1533 front_we(x_cmd, '0');
1537 elsif instruction = conv_std_logic_vector(16#a0#, dsize) then
1542 front_we(y_cmd, '0');
1544 elsif instruction = conv_std_logic_vector(16#a4#, dsize) then
1548 if exec_cycle = T2 then
1549 front_we(y_cmd, '0');
1553 elsif instruction = conv_std_logic_vector(16#b4#, dsize) then
1557 if exec_cycle = T3 then
1558 front_we(y_cmd, '0');
1562 elsif instruction = conv_std_logic_vector(16#ac#, dsize) then
1566 if exec_cycle = T3 then
1568 front_we(y_cmd, '0');
1571 elsif instruction = conv_std_logic_vector(16#bc#, dsize) then
1575 elsif instruction = conv_std_logic_vector(16#09#, dsize) then
1580 back_oe(acc_cmd, '0');
1581 back_we(acc_cmd, '0');
1584 elsif instruction = conv_std_logic_vector(16#05#, dsize) then
1588 if exec_cycle = T2 then
1590 back_oe(acc_cmd, '0');
1591 back_we(acc_cmd, '0');
1595 elsif instruction = conv_std_logic_vector(16#15#, dsize) then
1599 elsif instruction = conv_std_logic_vector(16#0d#, dsize) then
1603 elsif instruction = conv_std_logic_vector(16#1d#, dsize) then
1607 elsif instruction = conv_std_logic_vector(16#19#, dsize) then
1611 elsif instruction = conv_std_logic_vector(16#01#, dsize) then
1615 elsif instruction = conv_std_logic_vector(16#11#, dsize) then
1619 elsif instruction = conv_std_logic_vector(16#e9#, dsize) then
1624 back_oe(acc_cmd, '0');
1625 back_we(acc_cmd, '0');
1628 elsif instruction = conv_std_logic_vector(16#e5#, dsize) then
1632 elsif instruction = conv_std_logic_vector(16#f5#, dsize) then
1636 elsif instruction = conv_std_logic_vector(16#ed#, dsize) then
1640 elsif instruction = conv_std_logic_vector(16#fd#, dsize) then
1644 elsif instruction = conv_std_logic_vector(16#f9#, dsize) then
1648 if exec_cycle = T3 or exec_cycle = T4 then
1650 back_oe(acc_cmd, '0');
1651 back_we(acc_cmd, '0');
1655 elsif instruction = conv_std_logic_vector(16#e1#, dsize) then
1659 elsif instruction = conv_std_logic_vector(16#f1#, dsize) then
1665 ----------------------------------------
1666 ---A.3. store operation.
1667 ----------------------------------------
1668 elsif instruction = conv_std_logic_vector(16#85#, dsize) then
1672 if exec_cycle = T2 then
1673 front_oe(acc_cmd, '0');
1676 elsif instruction = conv_std_logic_vector(16#95#, dsize) then
1680 elsif instruction = conv_std_logic_vector(16#8d#, dsize) then
1684 if exec_cycle = T3 then
1685 front_oe(acc_cmd, '0');
1688 elsif instruction = conv_std_logic_vector(16#9d#, dsize) then
1692 if exec_cycle = T4 then
1693 front_oe(acc_cmd, '0');
1696 elsif instruction = conv_std_logic_vector(16#99#, dsize) then
1700 if exec_cycle = T4 then
1701 front_oe(acc_cmd, '0');
1704 elsif instruction = conv_std_logic_vector(16#81#, dsize) then
1708 elsif instruction = conv_std_logic_vector(16#91#, dsize) then
1712 if exec_cycle = T5 then
1713 front_oe(acc_cmd, '0');
1716 elsif instruction = conv_std_logic_vector(16#86#, dsize) then
1720 if exec_cycle = T2 then
1721 front_oe(x_cmd, '0');
1724 elsif instruction = conv_std_logic_vector(16#96#, dsize) then
1728 elsif instruction = conv_std_logic_vector(16#8e#, dsize) then
1732 if exec_cycle = T3 then
1733 front_oe(x_cmd, '0');
1736 elsif instruction = conv_std_logic_vector(16#84#, dsize) then
1740 if exec_cycle = T2 then
1741 front_oe(y_cmd, '0');
1744 elsif instruction = conv_std_logic_vector(16#94#, dsize) then
1748 elsif instruction = conv_std_logic_vector(16#8c#, dsize) then
1752 if exec_cycle = T3 then
1753 front_oe(y_cmd, '0');
1757 ----------------------------------------
1758 ---A.4. read-modify-write operation
1759 ----------------------------------------
1760 elsif instruction = conv_std_logic_vector(16#06#, dsize) then
1764 elsif instruction = conv_std_logic_vector(16#16#, dsize) then
1768 elsif instruction = conv_std_logic_vector(16#0e#, dsize) then
1772 elsif instruction = conv_std_logic_vector(16#1e#, dsize) then
1776 elsif instruction = conv_std_logic_vector(16#c6#, dsize) then
1780 if exec_cycle = T4 then
1784 elsif instruction = conv_std_logic_vector(16#d6#, dsize) then
1788 elsif instruction = conv_std_logic_vector(16#ce#, dsize) then
1792 if exec_cycle = T5 then
1796 elsif instruction = conv_std_logic_vector(16#de#, dsize) then
1800 elsif instruction = conv_std_logic_vector(16#e6#, dsize) then
1804 if exec_cycle = T4 then
1808 elsif instruction = conv_std_logic_vector(16#f6#, dsize) then
1812 elsif instruction = conv_std_logic_vector(16#ee#, dsize) then
1816 if exec_cycle = T5 then
1820 elsif instruction = conv_std_logic_vector(16#fe#, dsize) then
1824 elsif instruction = conv_std_logic_vector(16#46#, dsize) then
1828 elsif instruction = conv_std_logic_vector(16#56#, dsize) then
1832 elsif instruction = conv_std_logic_vector(16#4e#, dsize) then
1836 elsif instruction = conv_std_logic_vector(16#5e#, dsize) then
1840 elsif instruction = conv_std_logic_vector(16#26#, dsize) then
1844 elsif instruction = conv_std_logic_vector(16#36#, dsize) then
1848 elsif instruction = conv_std_logic_vector(16#2e#, dsize) then
1852 elsif instruction = conv_std_logic_vector(16#3e#, dsize) then
1856 elsif instruction = conv_std_logic_vector(16#66#, dsize) then
1860 -- if exec_cycle = T4 then
1861 -- set_nzc_from_alu;
1864 elsif instruction = conv_std_logic_vector(16#76#, dsize) then
1868 elsif instruction = conv_std_logic_vector(16#6e#, dsize) then
1872 elsif instruction = conv_std_logic_vector(16#7e#, dsize) then
1876 if exec_cycle = T6 then
1881 ----------------------------------------
1882 --A.5. miscellaneous oprations.
1883 ----------------------------------------
1886 elsif instruction = conv_std_logic_vector(16#08#, dsize) then
1889 elsif instruction = conv_std_logic_vector(16#48#, dsize) then
1892 if exec_cycle = T2 then
1893 front_oe(acc_cmd, '0');
1896 elsif instruction = conv_std_logic_vector(16#28#, dsize) then
1899 elsif instruction = conv_std_logic_vector(16#68#, dsize) then
1902 if exec_cycle = T3 then
1903 front_we(acc_cmd, '0');
1907 ----------------------------------------
1909 ----------------------------------------
1910 elsif instruction = conv_std_logic_vector(16#20#, dsize) then
1911 if exec_cycle = T1 then
1912 d_print("jsr abs 2");
1915 dbuf_int_oe_n <= '0';
1919 elsif exec_cycle = T2 then
1922 dbuf_int_oe_n <= '1';
1925 --push return addr high into stack.
1928 front_oe(pch_cmd, '0');
1929 back_oe(sp_cmd, '0');
1930 back_we(sp_cmd, '0');
1933 elsif exec_cycle = T3 then
1935 front_oe(pch_cmd, '1');
1937 --push return addr low into stack.
1940 front_oe(pcl_cmd, '0');
1941 back_oe(sp_cmd, '0');
1942 back_we(sp_cmd, '0');
1946 elsif exec_cycle = T4 then
1950 front_oe(pcl_cmd, '1');
1951 back_oe(sp_cmd, '1');
1952 back_we(sp_cmd, '1');
1956 back_oe(pch_cmd, '0');
1957 back_oe(pcl_cmd, '0');
1958 dbuf_int_oe_n <= '0';
1962 elsif exec_cycle = T5 then
1965 back_oe(pch_cmd, '1');
1966 back_oe(pcl_cmd, '1');
1967 dbuf_int_oe_n <= '1';
1973 front_we(pch_cmd, '0');
1977 back_we(pcl_cmd, '0');
1980 end if; --if exec_cycle = T1 then
1983 elsif instruction = conv_std_logic_vector(16#00#, dsize) then
1985 ----------------------------------------
1986 -- A.5.5 return from interrupt
1987 ----------------------------------------
1988 elsif instruction = conv_std_logic_vector(16#40#, dsize) then
1989 if exec_cycle = T1 then
1993 --pop stack (decrement only)
1994 back_oe(sp_cmd, '0');
1995 back_we(sp_cmd, '0');
2000 elsif exec_cycle = T2 then
2004 back_oe(sp_cmd, '0');
2005 back_we(sp_cmd, '0');
2010 stat_dec_oe_n <= '1';
2011 dbuf_int_oe_n <= '0';
2012 stat_bus_all_n <= '0';
2015 elsif exec_cycle = T3 then
2017 stat_bus_all_n <= '1';
2020 back_oe(sp_cmd, '0');
2021 back_we(sp_cmd, '0');
2026 dbuf_int_oe_n <= '0';
2027 front_we(pcl_cmd, '0');
2030 elsif exec_cycle = T4 then
2032 --stack decrement stop.
2033 back_we(sp_cmd, '1');
2035 front_we(pcl_cmd, '1');
2038 back_oe(sp_cmd, '0');
2041 dbuf_int_oe_n <= '0';
2042 front_we(pch_cmd, '0');
2045 elsif exec_cycle = T5 then
2047 back_oe(sp_cmd, '1');
2050 dbuf_int_oe_n <= '1';
2051 front_we(pch_cmd, '1');
2055 end if; --if exec_cycle = T1 then
2057 ----------------------------------------
2059 ----------------------------------------
2060 elsif instruction = conv_std_logic_vector(16#4c#, dsize) then
2062 if exec_cycle = T1 then
2064 --fetch next opcode (abs low).
2067 --latch abs low data.
2068 dbuf_int_oe_n <= '0';
2071 elsif exec_cycle = T2 then
2079 dbuf_int_oe_n <= '0';
2082 front_we(pch_cmd, '0');
2087 elsif instruction = conv_std_logic_vector(16#6c#, dsize) then
2089 if exec_cycle = T1 then
2091 --fetch next opcode (abs low).
2094 --latch abs low data.
2095 dbuf_int_oe_n <= '0';
2098 elsif exec_cycle = T2 then
2106 dbuf_int_oe_n <= '0';
2110 elsif exec_cycle = T3 then
2117 front_we(pcl_cmd, '0');
2120 elsif exec_cycle = T4 then
2123 front_we(pcl_cmd, '1');
2126 front_we(pch_cmd, '0');
2134 ----------------------------------------
2135 -- A.5.7 return from soubroutine
2136 ----------------------------------------
2137 elsif instruction = conv_std_logic_vector(16#60#, dsize) then
2138 if exec_cycle = T1 then
2142 --pop stack (decrement only)
2143 back_oe(sp_cmd, '0');
2144 back_we(sp_cmd, '0');
2149 elsif exec_cycle = T2 then
2153 back_oe(sp_cmd, '0');
2154 back_we(sp_cmd, '0');
2159 dbuf_int_oe_n <= '0';
2160 front_we(pcl_cmd, '0');
2163 elsif exec_cycle = T3 then
2165 --stack decrement stop.
2166 back_we(sp_cmd, '1');
2168 front_we(pcl_cmd, '1');
2171 back_oe(sp_cmd, '0');
2174 dbuf_int_oe_n <= '0';
2175 front_we(pch_cmd, '0');
2178 elsif exec_cycle = T4 then
2180 back_oe(sp_cmd, '1');
2183 dbuf_int_oe_n <= '1';
2184 front_we(pch_cmd, '1');
2186 --complying h/w manual...
2188 elsif exec_cycle = T5 then
2194 end if; --if exec_cycle = T1 then
2196 ----------------------------------------
2197 -- A.5.8 branch operations
2198 ----------------------------------------
2199 elsif instruction = conv_std_logic_vector(16#90#, dsize) then
2201 a58_branch (st_C, '0');
2203 elsif instruction = conv_std_logic_vector(16#b0#, dsize) then
2205 a58_branch (st_C, '1');
2207 elsif instruction = conv_std_logic_vector(16#f0#, dsize) then
2209 a58_branch (st_Z, '1');
2211 elsif instruction = conv_std_logic_vector(16#30#, dsize) then
2213 a58_branch (st_N, '1');
2215 elsif instruction = conv_std_logic_vector(16#d0#, dsize) then
2217 a58_branch (st_Z, '0');
2219 elsif instruction = conv_std_logic_vector(16#10#, dsize) then
2221 a58_branch (st_N, '0');
2223 elsif instruction = conv_std_logic_vector(16#50#, dsize) then
2225 elsif instruction = conv_std_logic_vector(16#70#, dsize) then
2229 ---unknown instruction!!!!
2231 report "======== unknow instruction "
2232 & conv_hex8(conv_integer(instruction))
2234 end if; --if instruction = conv_std_logic_vector(16#0a#, dsize)
2236 elsif exec_cycle = R0 then
2237 d_print(string'("reset"));
2239 front_we(pch_cmd, '1');
2240 back_we(pcl_cmd, '1');
2242 --initialize port...
2245 dbuf_int_oe_n <= '1';
2272 stat_dec_oe_n <= '1';
2273 stat_bus_oe_n <= '1';
2274 stat_set_flg_n <= '1';
2276 stat_bus_all_n <= '1';
2277 stat_bus_nz_n <= '1';
2278 stat_alu_we_n <= '1';
2283 nmi_handled_n <= '1';
2287 elsif exec_cycle = R1 or exec_cycle = N1 then
2299 --front_oe(pch_cmd, '0');
2300 back_oe(sp_cmd, '0');
2301 back_we(sp_cmd, '0');
2304 if exec_cycle = R1 then
2306 elsif exec_cycle = N1 then
2310 elsif exec_cycle = R2 or exec_cycle = N2 then
2311 front_oe(pch_cmd, '1');
2316 front_oe(pcl_cmd, '0');
2317 back_oe(sp_cmd, '0');
2318 back_we(sp_cmd, '0');
2321 if exec_cycle = R2 then
2323 elsif exec_cycle = N2 then
2327 elsif exec_cycle = R3 or exec_cycle = N3 then
2328 front_oe(pcl_cmd, '1');
2333 stat_bus_oe_n <= '0';
2334 back_oe(sp_cmd, '0');
2335 back_we(sp_cmd, '0');
2338 if exec_cycle = R3 then
2340 elsif exec_cycle = N3 then
2344 elsif exec_cycle = R4 or exec_cycle = N4 then
2345 stat_bus_oe_n <= '1';
2348 front_oe(pcl_cmd, '1');
2349 back_oe(sp_cmd, '1');
2350 back_we(sp_cmd, '1');
2352 --fetch reset vector low
2354 dbuf_int_oe_n <= '0';
2355 front_we(pcl_cmd, '0');
2357 if exec_cycle = R4 then
2360 elsif exec_cycle = N4 then
2365 elsif exec_cycle = R5 or exec_cycle = N5 then
2366 front_we(pcl_cmd, '1');
2368 --fetch reset vector hi
2369 front_we(pch_cmd, '0');
2372 if exec_cycle = N5 then
2373 nmi_handled_n <= '0';
2375 --start execute cycle.
2378 elsif exec_cycle(5) = '1' then
2379 ---pc increment and next page.
2380 d_print(string'("pch next page..."));
2381 --pcl stop increment
2383 back_we(pcl_cmd, '1');
2385 back_we(pch_cmd, '0');
2386 back_oe(pch_cmd, '0');
2388 if ('0' & exec_cycle(4 downto 0) = T0) then
2389 --do the t0 identical routine.
2394 elsif ('0' & exec_cycle(4 downto 0) = T1) then
2395 --if fetch cycle, preserve instrution register
2398 --TODO: must handle for jmp case???
2399 elsif ('0' & exec_cycle(4 downto 0) = T2) then
2400 --disable previous we_n gate.
2401 --t1 cycle is fetch low oprand.
2403 elsif ('0' & exec_cycle(4 downto 0) = T3) then
2404 --t2 cycle is fetch high oprand.
2408 end if; --if exec_cycle = T0 then
2410 end if; --if (set_clk'event and set_clk = '1')