Subversion Repositories KoE_projects

Rev

Blame | Last modification | View Log | Download | RSS feed | ?url?

  1. --------------------------------------------------------------------------------
  2. -- ****
  3. -- T80(c) core. Attempt to finish all undocumented features and provide
  4. --              accurate timings.
  5. -- Version 350.
  6. -- Copyright (c) 2018 Sorgelig
  7. --  Test passed: ZEXDOC, ZEXALL, Z80Full(*), Z80memptr
  8. --  (*) Currently only SCF and CCF instructions aren't passed X/Y flags check as
  9. --      correct implementation is still unclear.
  10. --
  11. -- Compilation fix 2021 by Neal Crook
  12. --
  13. -- ****
  14. -- T80(b) core. In an effort to merge and maintain bug fixes ....
  15. --
  16. -- Ver 303 add undocumented DDCB and FDCB opcodes by TobiFlex 20.04.2010
  17. -- Ver 301 parity flag is just parity for 8080, also overflow for Z80, by Sean Riddle
  18. -- Ver 300 started tidyup.
  19. --
  20. -- MikeJ March 2005
  21. -- Latest version from www.fpgaarcade.com (original www.opencores.org)
  22. --
  23. -- ****
  24. -- Z80 compatible microprocessor core
  25. --
  26. -- Version : 0247
  27. -- Copyright (c) 2001-2002 Daniel Wallner (jesus@opencores.org)
  28. -- All rights reserved
  29. --
  30. -- Redistribution and use in source and synthezised forms, with or without
  31. -- modification, are permitted provided that the following conditions are met:
  32. --
  33. -- Redistributions of source code must retain the above copyright notice,
  34. -- this list of conditions and the following disclaimer.
  35. --
  36. -- Redistributions in synthesized form must reproduce the above copyright
  37. -- notice, this list of conditions and the following disclaimer in the
  38. -- documentation and/or other materials provided with the distribution.
  39. --
  40. -- Neither the name of the author nor the names of other contributors may
  41. -- be used to endorse or promote products derived from this software without
  42. -- specific prior written permission.
  43. --
  44. -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  45. -- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  46. -- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  47. -- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
  48. -- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. -- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. -- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. -- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. -- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. -- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54. -- POSSIBILITY OF SUCH DAMAGE.
  55. --
  56. -- Please report bugs to the author, but before you do so, please
  57. -- make sure that this is not a derivative work and that
  58. -- you have the latest version of this file.
  59. --
  60. -- The latest version of this file can be found at:
  61. --      http://www.opencores.org/cvsweb.shtml/t80/
  62. --
  63. -- Limitations :
  64. --
  65. -- File history :
  66. --
  67. --      0208 : First complete release
  68. --      0210 : Fixed wait and halt
  69. --      0211 : Fixed Refresh addition and IM 1
  70. --      0214 : Fixed mostly flags, only the block instructions now fail the zex regression test
  71. --      0232 : Removed refresh address output for Mode > 1 and added DJNZ M1_n fix by Mike Johnson
  72. --      0235 : Added clock enable and IM 2 fix by Mike Johnson
  73. --      0237 : Changed 8080 I/O address output, added IntE output
  74. --      0238 : Fixed (IX/IY+d) timing and 16 bit ADC and SBC zero flag
  75. --      0240 : Added interrupt ack fix by Mike Johnson, changed (IX/IY+d) timing and changed flags in GB mode
  76. --      0242 : Added I/O wait, fixed refresh address, moved some registers to RAM
  77. --      0247 : Fixed bus req/ack cycle
  78. --      0667 : koe - Fixed I/O/memory/int timings
  79.  
  80. library IEEE;
  81. use IEEE.std_logic_1164.all;
  82. use IEEE.numeric_std.all;
  83. use IEEE.STD_LOGIC_UNSIGNED.all;
  84.  
  85. use work.all;
  86.  
  87. entity T80 is
  88.         generic(
  89.                 Mode   : integer := 0;  -- 0 => Z80, 1 => Fast Z80, 2 => 8080, 3 => GB
  90.                 IOWait : integer := 0;  -- 0 => Single cycle I/O, 1 => Std I/O cycle
  91.                 Flag_C : integer := 0;
  92.                 Flag_N : integer := 1;
  93.                 Flag_P : integer := 2;
  94.                 Flag_X : integer := 3;
  95.                 Flag_H : integer := 4;
  96.                 Flag_Y : integer := 5;
  97.                 Flag_Z : integer := 6;
  98.                 Flag_S : integer := 7
  99.         );
  100.         port(
  101.                 RESET_n    : in  std_logic;
  102.                 CLK_n      : in  std_logic;
  103.                 CEN        : in  std_logic;
  104.                 WAIT_n     : in  std_logic;
  105.                 INT_n      : in  std_logic;
  106.                 NMI_n      : in  std_logic;
  107.                 BUSRQ_n    : in  std_logic;
  108.                 M1_n       : out std_logic;
  109.                 IORQ       : out std_logic;
  110.                 NoRead     : out std_logic;
  111.                 Write      : out std_logic;
  112.                 RFSH_n     : out std_logic;
  113.                 HALT_n     : out std_logic;
  114.                 BUSAK_n    : out std_logic;
  115.                 A          : out std_logic_vector(15 downto 0);
  116.                 DInst      : in  std_logic_vector(7 downto 0);
  117.                 DI         : in  std_logic_vector(7 downto 0);
  118.                 DO         : out std_logic_vector(7 downto 0);
  119.                 MC         : out std_logic_vector(2 downto 0);
  120.                 TS         : out std_logic_vector(2 downto 0);
  121.                 IntCycle_n : out std_logic;
  122.                 IntE       : out std_logic;
  123.                 Stop       : out std_logic;
  124.                 out0       : in  std_logic := '0';  -- 0 => OUT(C),0, 1 => OUT(C),255
  125.                 REG        : out std_logic_vector(211 downto 0); -- IFF2, IFF1, IM, IY, HL', DE', BC', IX, HL, DE, BC, PC, SP, R, I, F', A', F, A
  126.  
  127.                 DIRSet     : in  std_logic := '0';
  128.                 DIR        : in  std_logic_vector(211 downto 0) := (others => '0') -- IFF2, IFF1, IM, IY, HL', DE', BC', IX, HL, DE, BC, PC, SP, R, I, F', A', F, A
  129.         );
  130. end T80;
  131.  
  132. architecture rtl of T80 is
  133.     component T80_MCode
  134.         generic(
  135.             Mode   : integer := 0;
  136.             Flag_C : integer := 0;
  137.             Flag_N : integer := 1;
  138.             Flag_P : integer := 2;
  139.             Flag_X : integer := 3;
  140.             Flag_H : integer := 4;
  141.             Flag_Y : integer := 5;
  142.             Flag_Z : integer := 6;
  143.             Flag_S : integer := 7
  144.         );
  145.         port(
  146.           IR          : in std_logic_vector(7 downto 0);
  147.           ISet        : in std_logic_vector(1 downto 0);
  148.           MCycle      : in std_logic_vector(2 downto 0);
  149.           F           : in std_logic_vector(7 downto 0);
  150.           NMICycle    : in std_logic;
  151.           IntCycle    : in std_logic;
  152.           XY_State    : in std_logic_vector(1 downto 0);
  153.           MCycles     : out std_logic_vector(2 downto 0);
  154.           TStates     : out std_logic_vector(2 downto 0);
  155.           Prefix      : out std_logic_vector(1 downto 0); -- None,CB,ED,DD/FD
  156.           Inc_PC      : out std_logic;
  157.           Inc_WZ      : out std_logic;
  158.           IncDec_16   : out std_logic_vector(3 downto 0); -- BC,DE,HL,SP   0 is inc
  159.           Read_To_Reg : out std_logic;
  160.           Read_To_Acc : out std_logic;
  161.           Set_BusA_To : out std_logic_vector(3 downto 0); -- B,C,D,E,H,L,DI/DB,A,SP(L),SP(M),0,F
  162.           Set_BusB_To : out std_logic_vector(3 downto 0); -- B,C,D,E,H,L,DI,A,SP(L),SP(M),1,F,PC(L),PC(M),0
  163.           ALU_Op      : out std_logic_vector(3 downto 0);
  164.              -- ADD, ADC, SUB, SBC, AND, XOR, OR, CP, ROT, BIT, SET, RES, DAA, RLD, RRD, None
  165.           Save_ALU    : out std_logic;
  166.           PreserveC   : out std_logic;
  167.           Arith16     : out std_logic;
  168.           Set_Addr_To : out std_logic_vector(2 downto 0); -- aNone,aXY,aIOA,aSP,aBC,aDE,aZI
  169.           IORQ        : out std_logic;
  170.           Jump        : out std_logic;
  171.           JumpE       : out std_logic;
  172.           JumpXY      : out std_logic;
  173.           Call        : out std_logic;
  174.           RstP        : out std_logic;
  175.           LDZ         : out std_logic;
  176.           LDW         : out std_logic;
  177.           LDSPHL      : out std_logic;
  178.           Special_LD  : out std_logic_vector(2 downto 0); -- A,I;A,R;I,A;R,A;None
  179.           ExchangeDH  : out std_logic;
  180.           ExchangeRp  : out std_logic;
  181.           ExchangeAF  : out std_logic;
  182.           ExchangeRS  : out std_logic;
  183.           I_DJNZ      : out std_logic;
  184.           I_CPL       : out std_logic;
  185.           I_CCF       : out std_logic;
  186.           I_SCF       : out std_logic;
  187.           I_RETN      : out std_logic;
  188.           I_BT        : out std_logic;
  189.           I_BC        : out std_logic;
  190.           I_BTR       : out std_logic;
  191.           I_RLD       : out std_logic;
  192.           I_RRD       : out std_logic;
  193.           I_INRC      : out std_logic;
  194.           SetWZ       : out std_logic_vector(1 downto 0);
  195.           SetDI       : out std_logic;
  196.           SetEI       : out std_logic;
  197.           IMode       : out std_logic_vector(1 downto 0);
  198.           Halt        : out std_logic;
  199.           NoRead      : out std_logic;
  200.           Write       : out std_logic;
  201.           XYbit_undoc : out std_logic
  202.        );
  203.     end component;
  204.  
  205.     component T80_ALU
  206.         generic(
  207.             Mode : integer := 0;
  208.             Flag_C : integer := 0;
  209.             Flag_N : integer := 1;
  210.             Flag_P : integer := 2;
  211.             Flag_X : integer := 3;
  212.             Flag_H : integer := 4;
  213.             Flag_Y : integer := 5;
  214.             Flag_Z : integer := 6;
  215.             Flag_S : integer := 7
  216.         );
  217.         port(
  218.             Arith16         : in  std_logic;
  219.             Z16             : in  std_logic;
  220.             WZ              : in  std_logic_vector(15 downto 0);
  221.             XY_State            : in  std_logic_vector(1 downto 0);
  222.             ALU_Op          : in  std_logic_vector(3 downto 0);
  223.             IR              : in  std_logic_vector(5 downto 0);
  224.             ISet            : in  std_logic_vector(1 downto 0);
  225.             BusA            : in  std_logic_vector(7 downto 0);
  226.             BusB            : in  std_logic_vector(7 downto 0);
  227.             F_In            : in  std_logic_vector(7 downto 0);
  228.             Q               : out std_logic_vector(7 downto 0);
  229.             F_Out           : out std_logic_vector(7 downto 0)
  230.         );
  231.     end component;
  232.  
  233.     component T80_Reg
  234.         port(
  235.             Clk     : in  std_logic;
  236.             CEN     : in  std_logic;
  237.             WEH     : in  std_logic;
  238.             WEL     : in  std_logic;
  239.             AddrA   : in  std_logic_vector(2 downto 0);
  240.             AddrB   : in  std_logic_vector(2 downto 0);
  241.             AddrC   : in  std_logic_vector(2 downto 0);
  242.             DIH     : in  std_logic_vector(7 downto 0);
  243.             DIL     : in  std_logic_vector(7 downto 0);
  244.             DOAH    : out std_logic_vector(7 downto 0);
  245.             DOAL    : out std_logic_vector(7 downto 0);
  246.             DOBH    : out std_logic_vector(7 downto 0);
  247.             DOBL    : out std_logic_vector(7 downto 0);
  248.             DOCH    : out std_logic_vector(7 downto 0);
  249.             DOCL    : out std_logic_vector(7 downto 0);
  250.             DOR     : out std_logic_vector(127 downto 0);
  251.             DIRSet  : in  std_logic;
  252.             DIR     : in  std_logic_vector(127 downto 0)
  253.         );
  254.     end component;
  255.  
  256.         constant aNone              : std_logic_vector(2 downto 0) := "111";
  257.         constant aBC                : std_logic_vector(2 downto 0) := "000";
  258.         constant aDE                : std_logic_vector(2 downto 0) := "001";
  259.         constant aXY                : std_logic_vector(2 downto 0) := "010";
  260.         constant aIOA               : std_logic_vector(2 downto 0) := "100";
  261.         constant aSP                : std_logic_vector(2 downto 0) := "101";
  262.         constant aZI                : std_logic_vector(2 downto 0) := "110";
  263.  
  264.         -- Registers
  265.         signal ACC, F               : std_logic_vector(7 downto 0);
  266.         signal Ap, Fp               : std_logic_vector(7 downto 0);
  267.         signal I                    : std_logic_vector(7 downto 0);
  268.         signal R                    : unsigned(7 downto 0);
  269.         signal SP, PC               : unsigned(15 downto 0);
  270.  
  271.         signal RegDIH               : std_logic_vector(7 downto 0);
  272.         signal RegDIL               : std_logic_vector(7 downto 0);
  273.         signal RegBusA              : std_logic_vector(15 downto 0);
  274.         signal RegBusB              : std_logic_vector(15 downto 0);
  275.         signal RegBusC              : std_logic_vector(15 downto 0);
  276.         signal RegAddrA_r           : std_logic_vector(2 downto 0);
  277.         signal RegAddrA             : std_logic_vector(2 downto 0);
  278.         signal RegAddrB_r           : std_logic_vector(2 downto 0);
  279.         signal RegAddrB             : std_logic_vector(2 downto 0);
  280.         signal RegAddrC             : std_logic_vector(2 downto 0);
  281.         signal RegWEH               : std_logic;
  282.         signal RegWEL               : std_logic;
  283.         signal Alternate            : std_logic;
  284.  
  285.         -- Help Registers
  286.         signal WZ                   : std_logic_vector(15 downto 0);        -- MEMPTR register
  287.         signal IR                   : std_logic_vector(7 downto 0);         -- Instruction register
  288.         signal ISet                 : std_logic_vector(1 downto 0);         -- Instruction set selector
  289.         signal RegBusA_r            : std_logic_vector(15 downto 0);
  290.  
  291.         signal ID16                 : signed(15 downto 0);
  292.         signal Save_Mux             : std_logic_vector(7 downto 0);
  293.  
  294.         signal TState               : unsigned(2 downto 0);
  295.         signal MCycle               : std_logic_vector(2 downto 0);
  296.         signal IntE_FF1             : std_logic;
  297.         signal IntE_FF2             : std_logic;
  298.         signal Halt_FF              : std_logic;
  299.         signal BusReq_s             : std_logic;
  300.         signal BusAck               : std_logic;
  301.         signal ClkEn                : std_logic;
  302.         signal NMI_s                : std_logic;
  303.         signal IStatus              : std_logic_vector(1 downto 0);
  304.  
  305.         signal DI_Reg               : std_logic_vector(7 downto 0);
  306.         signal T_Res                : std_logic;
  307.         signal XY_State             : std_logic_vector(1 downto 0);
  308.         signal Pre_XY_F_M           : std_logic_vector(2 downto 0);
  309.         signal NextIs_XY_Fetch      : std_logic;
  310.         signal XY_Ind               : std_logic;
  311.         signal No_BTR               : std_logic;
  312.         signal BTR_r                : std_logic;
  313.         signal Auto_Wait            : std_logic;
  314.         signal Auto_Wait_t1         : std_logic;
  315.         signal Auto_Wait_t2         : std_logic;
  316.         signal IncDecZ              : std_logic;
  317.  
  318.         -- ALU signals
  319.         signal BusB                 : std_logic_vector(7 downto 0);
  320.         signal BusA                 : std_logic_vector(7 downto 0);
  321.         signal ALU_Q                : std_logic_vector(7 downto 0);
  322.         signal F_Out                : std_logic_vector(7 downto 0);
  323.  
  324.         -- Registered micro code outputs
  325.         signal Read_To_Reg_r        : std_logic_vector(4 downto 0);
  326.         signal Arith16_r            : std_logic;
  327.         signal Z16_r                : std_logic;
  328.         signal ALU_Op_r             : std_logic_vector(3 downto 0);
  329.         signal Save_ALU_r           : std_logic;
  330.         signal PreserveC_r          : std_logic;
  331.         signal MCycles              : std_logic_vector(2 downto 0);
  332.  
  333.         -- Micro code outputs
  334.         signal MCycles_d            : std_logic_vector(2 downto 0);
  335.         signal TStates              : std_logic_vector(2 downto 0);
  336.         signal IntCycle             : std_logic;
  337.         signal NMICycle             : std_logic;
  338.         signal Inc_PC               : std_logic;
  339.         signal Inc_WZ               : std_logic;
  340.         signal IncDec_16            : std_logic_vector(3 downto 0);
  341.         signal Prefix               : std_logic_vector(1 downto 0);
  342.         signal Read_To_Acc          : std_logic;
  343.         signal Read_To_Reg          : std_logic;
  344.         signal Set_BusB_To          : std_logic_vector(3 downto 0);
  345.         signal Set_BusA_To          : std_logic_vector(3 downto 0);
  346.         signal ALU_Op               : std_logic_vector(3 downto 0);
  347.         signal Save_ALU             : std_logic;
  348.         signal PreserveC            : std_logic;
  349.         signal Arith16              : std_logic;
  350.         signal Set_Addr_To          : std_logic_vector(2 downto 0);
  351.         signal Jump                 : std_logic;
  352.         signal JumpE                : std_logic;
  353.         signal JumpXY               : std_logic;
  354.         signal Call                 : std_logic;
  355.         signal RstP                 : std_logic;
  356.         signal LDZ                  : std_logic;
  357.         signal LDW                  : std_logic;
  358.         signal LDSPHL               : std_logic;
  359.         signal IORQ_i               : std_logic;
  360.         signal Special_LD           : std_logic_vector(2 downto 0);
  361.         signal ExchangeDH           : std_logic;
  362.         signal ExchangeRp           : std_logic;
  363.         signal ExchangeAF           : std_logic;
  364.         signal ExchangeRS           : std_logic;
  365.         signal I_DJNZ               : std_logic;
  366.         signal I_CPL                : std_logic;
  367.         signal I_CCF                : std_logic;
  368.         signal I_SCF                : std_logic;
  369.         signal I_RETN               : std_logic;
  370.         signal I_BT                 : std_logic;
  371.         signal I_BC                 : std_logic;
  372.         signal I_BTR                : std_logic;
  373.         signal I_RLD                : std_logic;
  374.         signal I_RRD                : std_logic;
  375.         signal I_RXDD               : std_logic;
  376.         signal I_INRC               : std_logic;
  377.         signal SetWZ                : std_logic_vector(1 downto 0);
  378.         signal SetDI                : std_logic;
  379.         signal SetEI                : std_logic;
  380.         signal IMode                : std_logic_vector(1 downto 0);
  381.         signal Halt                 : std_logic;
  382.         signal XYbit_undoc          : std_logic;
  383.         signal DOR                  : std_logic_vector(127 downto 0);
  384.  
  385. begin
  386.  
  387.         REG <= IntE_FF2 & IntE_FF1 & IStatus & DOR & std_logic_vector(PC) & std_logic_vector(SP) & std_logic_vector(R) & I & Fp & Ap & F & ACC when Alternate = '0'
  388.                          else IntE_FF2 & IntE_FF1 & IStatus & DOR(127 downto 112) & DOR(47 downto 0) & DOR(63 downto 48) & DOR(111 downto 64) &
  389.                                                 std_logic_vector(PC) & std_logic_vector(SP) & std_logic_vector(R) & I & Fp & Ap & F & ACC;
  390.  
  391.         mcode : T80_MCode
  392.                 generic map(
  393.                         Mode   => Mode,
  394.                         Flag_C => Flag_C,
  395.                         Flag_N => Flag_N,
  396.                         Flag_P => Flag_P,
  397.                         Flag_X => Flag_X,
  398.                         Flag_H => Flag_H,
  399.                         Flag_Y => Flag_Y,
  400.                         Flag_Z => Flag_Z,
  401.                         Flag_S => Flag_S)
  402.                 port map(
  403.                         IR          => IR,
  404.                         ISet        => ISet,
  405.                         MCycle      => MCycle,
  406.                         F           => F,
  407.                         NMICycle    => NMICycle,
  408.                         IntCycle    => IntCycle,
  409.                         XY_State    => XY_State,
  410.                         MCycles     => MCycles_d,
  411.                         TStates     => TStates,
  412.                         Prefix      => Prefix,
  413.                         Inc_PC      => Inc_PC,
  414.                         Inc_WZ      => Inc_WZ,
  415.                         IncDec_16   => IncDec_16,
  416.                         Read_To_Acc => Read_To_Acc,
  417.                         Read_To_Reg => Read_To_Reg,
  418.                         Set_BusB_To => Set_BusB_To,
  419.                         Set_BusA_To => Set_BusA_To,
  420.                         ALU_Op      => ALU_Op,
  421.                         Save_ALU    => Save_ALU,
  422.                         PreserveC   => PreserveC,
  423.                         Arith16     => Arith16,
  424.                         Set_Addr_To => Set_Addr_To,
  425.                         IORQ        => IORQ_i,
  426.                         Jump        => Jump,
  427.                         JumpE       => JumpE,
  428.                         JumpXY      => JumpXY,
  429.                         Call        => Call,
  430.                         RstP        => RstP,
  431.                         LDZ         => LDZ,
  432.                         LDW         => LDW,
  433.                         LDSPHL      => LDSPHL,
  434.                         Special_LD  => Special_LD,
  435.                         ExchangeDH  => ExchangeDH,
  436.                         ExchangeRp  => ExchangeRp,
  437.                         ExchangeAF  => ExchangeAF,
  438.                         ExchangeRS  => ExchangeRS,
  439.                         I_DJNZ      => I_DJNZ,
  440.                         I_CPL       => I_CPL,
  441.                         I_CCF       => I_CCF,
  442.                         I_SCF       => I_SCF,
  443.                         I_RETN      => I_RETN,
  444.                         I_BT        => I_BT,
  445.                         I_BC        => I_BC,
  446.                         I_BTR       => I_BTR,
  447.                         I_RLD       => I_RLD,
  448.                         I_RRD       => I_RRD,
  449.                         I_INRC      => I_INRC,
  450.                         SetWZ       => SetWZ,
  451.                         SetDI       => SetDI,
  452.                         SetEI       => SetEI,
  453.                         IMode       => IMode,
  454.                         Halt        => Halt,
  455.                         NoRead      => NoRead,
  456.                         Write       => Write,
  457.                         XYbit_undoc => XYbit_undoc);
  458.  
  459.         alu : T80_ALU
  460.                 generic map(
  461.                         Mode   => Mode,
  462.                         Flag_C => Flag_C,
  463.                         Flag_N => Flag_N,
  464.                         Flag_P => Flag_P,
  465.                         Flag_X => Flag_X,
  466.                         Flag_H => Flag_H,
  467.                         Flag_Y => Flag_Y,
  468.                         Flag_Z => Flag_Z,
  469.                         Flag_S => Flag_S)
  470.                 port map(
  471.                         Arith16 => Arith16_r,
  472.                         Z16     => Z16_r,
  473.                         WZ      => WZ,
  474.                         XY_State=> XY_State,
  475.                         ALU_Op  => ALU_Op_r,
  476.                         IR      => IR(5 downto 0),
  477.                         ISet    => ISet,
  478.                         BusA    => BusA,
  479.                         BusB    => BusB,
  480.                         F_In    => F,
  481.                         Q       => ALU_Q,
  482.                         F_Out   => F_Out);
  483.  
  484.         ClkEn <= CEN and not BusAck;
  485.  
  486.         T_Res <= '1' when TState = unsigned(TStates) else '0';
  487.  
  488.         NextIs_XY_Fetch <= '1' when XY_State /= "00" and XY_Ind = '0' and
  489.                                                         ((Set_Addr_To = aXY) or
  490.                                                         (MCycle = "001" and IR = "11001011") or
  491.                                                         (MCycle = "001" and IR = "00110110")) else '0';
  492.  
  493.         Save_Mux <= BusB when ExchangeRp = '1' else
  494.                 DI_Reg when Save_ALU_r = '0' else
  495.                 ALU_Q;
  496.  
  497.         process (RESET_n, CLK_n)
  498.                 variable n : std_logic_vector(7 downto 0);
  499.                 variable ioq : std_logic_vector(8 downto 0);
  500.         begin
  501.                 if RESET_n = '0' then
  502.                         PC <= (others => '0');  -- Program Counter
  503.                         A <= (others => '0');
  504.                         WZ <= (others => '0');
  505.                         IR <= "00000000";
  506.                         ISet <= "00";
  507.                         XY_State <= "00";
  508.                         IStatus <= "00";
  509.                         MCycles <= "000";
  510. --                      DO <= "00000000"; -- koe
  511.  
  512.                         ACC <= (others => '1');
  513.                         F <= (others => '1');
  514.                         Ap <= (others => '1');
  515.                         Fp <= (others => '1');
  516.                         I <= (others => '0');
  517.                         R <= (others => '0');
  518.                         SP <= (others => '1');
  519.                         Alternate <= '0';
  520.  
  521.                         Read_To_Reg_r <= "00000";
  522.                         Arith16_r <= '0';
  523.                         BTR_r <= '0';
  524.                         Z16_r <= '0';
  525.                         ALU_Op_r <= "0000";
  526.                         Save_ALU_r <= '0';
  527.                         PreserveC_r <= '0';
  528.                         XY_Ind <= '0';
  529. --                      I_RXDD <= '0'; -- koe
  530.  
  531.                 elsif rising_edge(CLK_n) then
  532.  
  533.                         if DIRSet = '1' then
  534.                                 ACC <= DIR( 7 downto  0);
  535.                                 F   <= DIR(15 downto  8);
  536.                                 Ap  <= DIR(23 downto 16);
  537.                                 Fp  <= DIR(31 downto 24);
  538.                                 I   <= DIR(39 downto 32);
  539.                                 R   <= unsigned(DIR(47 downto 40));
  540.                                 SP  <= unsigned(DIR(63 downto 48));
  541.                                 PC  <= unsigned(DIR(79 downto 64));
  542.                                 A   <= DIR(79 downto 64);
  543.                                 IStatus <= DIR(209 downto 208);
  544.  
  545.                         elsif ClkEn = '1' then
  546.                                 ALU_Op_r <= "0000";
  547.                                 Save_ALU_r <= '0';
  548.                                 Read_To_Reg_r <= "00000";
  549.  
  550.                                 MCycles <= MCycles_d;
  551.  
  552.                                 if IMode /= "11" then
  553.                                         IStatus <= IMode;
  554.                                 end if;
  555.  
  556.                                 Arith16_r <= Arith16;
  557.                                 PreserveC_r <= PreserveC;
  558.                                 if ISet = "10" and ALU_OP(2) = '0' and ALU_OP(0) = '1' and MCycle = "011" then
  559.                                         Z16_r <= '1';
  560.                                 else
  561.                                         Z16_r <= '0';
  562.                                 end if;
  563.  
  564.                                 if MCycle  = "001" and TState(2) = '0' then
  565.                                 -- MCycle = 1 and TState = 1, 2, or 3
  566.  
  567.                                         if TState = 2 and Wait_n = '1' then
  568.                                                 if Mode < 2 then
  569.                                                         A(7 downto 0) <= std_logic_vector(R);
  570.                                                         A(15 downto 8) <= I;
  571.                                                         R(6 downto 0) <= R(6 downto 0) + 1;
  572.                                                 end if;
  573.  
  574.                                                 if Jump = '0' and Call = '0' and NMICycle = '0' and IntCycle = '0' and not (Halt_FF = '1' or Halt = '1') then
  575.                                                         PC <= PC + 1;
  576.                                                 end if;
  577.  
  578.                                                 if IntCycle = '1' and IStatus = "01" then
  579.                                                         IR <= "11111111";
  580.                                                 elsif Halt_FF = '1' or (IntCycle = '1' and IStatus = "10") or NMICycle = '1' then
  581.                                                         IR <= "00000000";
  582.                                                 else
  583.                                                         IR <= DInst;
  584.                                                 end if;
  585.  
  586.                                                 ISet <= "00";
  587.                                                 if Prefix /= "00" then
  588.                                                         if Prefix = "11" then
  589.                                                                 if IR(5) = '1' then
  590.                                                                         XY_State <= "10";
  591.                                                                 else
  592.                                                                         XY_State <= "01";
  593.                                                                 end if;
  594.                                                         else
  595.                                                                 if Prefix = "10" then
  596.                                                                         XY_State <= "00";
  597.                                                                         XY_Ind <= '0';
  598.                                                                 end if;
  599.                                                                 ISet <= Prefix;
  600.                                                         end if;
  601.                                                 else
  602.                                                         XY_State <= "00";
  603.                                                         XY_Ind <= '0';
  604.                                                 end if;
  605.                                         end if;
  606.  
  607.                                 else
  608.                                 -- either (MCycle > 1) OR (MCycle = 1 AND TState > 3)
  609.  
  610.                                         if MCycle = "110" then
  611.                                                 XY_Ind <= '1';
  612.                                                 if Prefix = "01" then
  613.                                                         ISet <= "01";
  614.                                                 end if;
  615.                                         end if;
  616.  
  617.                                         if T_Res = '1' then
  618.                                                 BTR_r <= (I_BT or I_BC or I_BTR) and not No_BTR;
  619.                                                 if Jump = '1' then
  620.                                                         A(15 downto 8) <= DI_Reg;
  621.                                                         A(7 downto 0) <= WZ(7 downto 0);
  622.                                                         PC(15 downto 8) <= unsigned(DI_Reg);
  623.                                                         PC(7 downto 0) <= unsigned(WZ(7 downto 0));
  624.                                                 elsif JumpXY = '1' then
  625.                                                         A <= RegBusC;
  626.                                                         PC <= unsigned(RegBusC);
  627.                                                 elsif Call = '1' or RstP = '1' then
  628.                                                         A <= WZ;
  629.                                                         PC <= unsigned(WZ);
  630.                                                 elsif MCycle = MCycles and NMICycle = '1' then
  631.                                                         A <= "0000000001100110";
  632.                                                         PC <= "0000000001100110";
  633.                                                 elsif MCycle = "011" and IntCycle = '1' and IStatus = "10" then
  634.                                                         A(15 downto 8) <= I;
  635.                                                         A(7 downto 0) <= WZ(7 downto 0);
  636.                                                         PC(15 downto 8) <= unsigned(I);
  637.                                                         PC(7 downto 0) <= unsigned(WZ(7 downto 0));
  638.                                                 else
  639.                                                         case Set_Addr_To is
  640.                                                         when aXY =>
  641.                                                                 if XY_State = "00" then
  642.                                                                         A <= RegBusC;
  643.                                                                 else
  644.                                                                         if NextIs_XY_Fetch = '1' then
  645.                                                                                 A <= std_logic_vector(PC);
  646.                                                                         else
  647.                                                                                 A <= WZ;
  648.                                                                         end if;
  649.                                                                 end if;
  650.                                                         when aIOA =>
  651.                                                                 if Mode = 3 then
  652.                                                                         -- Memory map I/O on GBZ80
  653.                                                                         A(15 downto 8) <= (others => '1');
  654.                                                                 elsif Mode = 2 then
  655.                                                                         -- Duplicate I/O address on 8080
  656.                                                                         A(15 downto 8) <= DI_Reg;
  657.                                                                 else
  658.                                                                         A(15 downto 8) <= ACC;
  659.                                                                 end if;
  660.                                                                 A(7 downto 0) <= DI_Reg;
  661.                                                                 WZ <= (ACC & DI_Reg) + "1";
  662.                                                         when aSP =>
  663.                                                                 A <= std_logic_vector(SP);
  664.                                                         when aBC =>
  665.                                                                 if Mode = 3 and IORQ_i = '1' then
  666.                                                                         -- Memory map I/O on GBZ80
  667.                                                                         A(15 downto 8) <= (others => '1');
  668.                                                                         A(7 downto 0) <= RegBusC(7 downto 0);
  669.                                                                 else
  670.                                                                         A <= RegBusC;
  671.                                                                         if SetWZ = "01" then
  672.                                                                                 WZ <= RegBusC + "1";
  673.                                                                         end if;
  674.                                                                         if SetWZ = "10" then
  675.                                                                                 WZ(7 downto 0) <= RegBusC(7 downto 0) + "1";
  676.                                                                                 WZ(15 downto 8) <= ACC;
  677.                                                                         end if;
  678.                                                                 end if;
  679.                                                         when aDE =>
  680.                                                                 A <= RegBusC;
  681.                                                                 if SetWZ = "10" then
  682.                                                                         WZ(7 downto 0) <= RegBusC(7 downto 0) + "1";
  683.                                                                         WZ(15 downto 8) <= ACC;
  684.                                                                 end if;
  685.                                                         when aZI =>
  686.                                                                 if Inc_WZ = '1' then
  687.                                                                         A <= std_logic_vector(unsigned(WZ) + 1);
  688.                                                                 else
  689.                                                                         A(15 downto 8) <= DI_Reg;
  690.                                                                         A(7 downto 0) <= WZ(7 downto 0);
  691.                                                                         if SetWZ = "10" then
  692.                                                                                 WZ(7 downto 0) <= WZ(7 downto 0) + "1";
  693.                                                                                 WZ(15 downto 8) <= ACC;
  694.                                                                         end if;
  695.                                                                 end if;
  696.                                                         when others =>
  697.                                                                 A <= std_logic_vector(PC);
  698.                                                         end case;
  699.                                                 end if;
  700.  
  701.                                                 if SetWZ = "11" then
  702.                                                         WZ <= std_logic_vector(ID16);
  703.                                                 end if;
  704.  
  705.                                                 Save_ALU_r <= Save_ALU;
  706.                                                 ALU_Op_r <= ALU_Op;
  707.  
  708.                                                 if I_CPL = '1' then
  709.                                                         -- CPL
  710.                                                         ACC <= not ACC;
  711.                                                         F(Flag_Y) <= not ACC(5);
  712.                                                         F(Flag_H) <= '1';
  713.                                                         F(Flag_X) <= not ACC(3);
  714.                                                         F(Flag_N) <= '1';
  715.                                                 end if;
  716.                                                 if I_CCF = '1' then
  717.                                                         -- CCF
  718.                                                         F(Flag_C) <= not F(Flag_C);
  719.                                                         F(Flag_Y) <= ACC(5);
  720.                                                         F(Flag_H) <= F(Flag_C);
  721.                                                         F(Flag_X) <= ACC(3);
  722.                                                         F(Flag_N) <= '0';
  723.                                                 end if;
  724.                                                 if I_SCF = '1' then
  725.                                                         -- SCF
  726.                                                         F(Flag_C) <= '1';
  727.                                                         F(Flag_Y) <= ACC(5);
  728.                                                         F(Flag_H) <= '0';
  729.                                                         F(Flag_X) <= ACC(3);
  730.                                                         F(Flag_N) <= '0';
  731.                                                 end if;
  732.                                         end if;
  733.  
  734.                                         if (TState = 2 and I_BTR = '1' and IR(0) = '1') or (TState = 1 and I_BTR = '1' and IR(0) = '0') then
  735.                                                 ioq := ('0' & DI_Reg) + ('0' & std_logic_vector(ID16(7 downto 0)));
  736.                                                 F(Flag_N) <= DI_Reg(7);
  737.                                                 F(Flag_C) <= ioq(8);
  738.                                                 F(Flag_H) <= ioq(8);
  739.                                                 ioq := (ioq and "000000111") xor ('0'&BusA);
  740.                                                 F(Flag_P) <= not (ioq(0) xor ioq(1) xor ioq(2) xor ioq(3) xor ioq(4) xor ioq(5) xor ioq(6) xor ioq(7));
  741.                                         end if;
  742.  
  743.                                         if TState = 2 and Wait_n = '1' then
  744.                                                 if ISet = "01" and MCycle = "111" then
  745.                                                         IR <= DInst;
  746.                                                 end if;
  747.                                                 if JumpE = '1' then
  748.                                                         PC <= unsigned(signed(PC) + signed(DI_Reg));
  749.                                                         WZ <= std_logic_vector(signed(PC) + signed(DI_Reg));
  750.                                                 elsif Inc_PC = '1' then
  751.                                                         PC <= PC + 1;
  752.                                                 end if;
  753.                                                 if BTR_r = '1' then
  754.                                                         PC <= PC - 2;
  755.                                                 end if;
  756.                                                 if RstP = '1' then
  757.                                                         WZ <= (others =>'0');
  758.                                                         WZ(5 downto 3) <= IR(5 downto 3);
  759.                                                 end if;
  760.                                         end if;
  761.                                         if TState = 3 and MCycle = "110" then
  762.                                                 WZ <= std_logic_vector(signed(RegBusC) + signed(DI_Reg));
  763.                                         end if;
  764.  
  765.                                         if MCycle = "011" and TState = 4 and No_BTR = '0' then
  766.                                                 if I_BT = '1' or I_BC = '1' then
  767.                                                         WZ <= std_logic_vector(PC)-"1";
  768.                                                 end if;
  769.                                         end if;
  770.  
  771.                                         if (TState = 2 and Wait_n = '1') or (TState = 4 and MCycle = "001") then
  772.                                                 if IncDec_16(2 downto 0) = "111" then
  773.                                                         if IncDec_16(3) = '1' then
  774.                                                                 SP <= SP - 1;
  775.                                                         else
  776.                                                                 SP <= SP + 1;
  777.                                                         end if;
  778.                                                 end if;
  779.                                         end if;
  780.  
  781.                                         if LDSPHL = '1' then
  782.                                                 SP <= unsigned(RegBusC);
  783.                                         end if;
  784.                                         if ExchangeAF = '1' then
  785.                                                 Ap <= ACC;
  786.                                                 ACC <= Ap;
  787.                                                 Fp <= F;
  788.                                                 F <= Fp;
  789.                                         end if;
  790.                                         if ExchangeRS = '1' then
  791.                                                 Alternate <= not Alternate;
  792.                                         end if;
  793.                                 end if;
  794.  
  795.                                 if TState = 3 then
  796.                                         if LDZ = '1' then
  797.                                                 WZ(7 downto 0) <= DI_Reg;
  798.                                         end if;
  799.                                         if LDW = '1' then
  800.                                                 WZ(15 downto 8) <= DI_Reg;
  801.                                         end if;
  802.  
  803.                                         if Special_LD(2) = '1' then
  804.                                                 case Special_LD(1 downto 0) is
  805.                                                 when "00" =>
  806.                                                         ACC <= I;
  807.                                                         F(Flag_P) <= IntE_FF2;
  808.                                                         F(Flag_S) <= I(7);
  809.  
  810.                                                         if I = x"00" then
  811.                                                                 F(Flag_Z) <= '1';
  812.                                                         else
  813.                                                                 F(Flag_Z) <= '0';
  814.                                                         end if;
  815.  
  816.                                                         F(Flag_Y) <= I(5);
  817.                                                         F(Flag_H) <= '0';
  818.                                                         F(Flag_X) <= I(3);
  819.                                                         F(Flag_N) <= '0';
  820.  
  821.  
  822.                                                 when "01" =>
  823.                                                         ACC <= std_logic_vector(R);
  824.                                                         F(Flag_P) <= IntE_FF2;
  825.                                                         F(Flag_S) <= R(7);
  826.  
  827.                                                         if R = x"00" then
  828.                                                                 F(Flag_Z) <= '1';
  829.                                                         else
  830.                                                                 F(Flag_Z) <= '0';
  831.                                                         end if;
  832.  
  833.                                                         F(Flag_Y) <= R(5);
  834.                                                         F(Flag_H) <= '0';
  835.                                                         F(Flag_X) <= R(3);
  836.                                                         F(Flag_N) <= '0';
  837.  
  838.                                                 when "10" =>
  839.                                                         I <= ACC;
  840.                                                 when others =>
  841.                                                         R <= unsigned(ACC);
  842.                                                 end case;
  843.                                         end if;
  844.                                 end if;
  845.  
  846.                                 if (I_DJNZ = '0' and Save_ALU_r = '1') or ALU_Op_r = "1001" then
  847.                                         if Mode = 3 then
  848.                                                 F(6) <= F_Out(6);
  849.                                                 F(5) <= F_Out(5);
  850.                                                 F(7) <= F_Out(7);
  851.                                                 if PreserveC_r = '0' then
  852.                                                         F(4) <= F_Out(4);
  853.                                                 end if;
  854.                                         else
  855.                                                 F(7 downto 1) <= F_Out(7 downto 1);
  856.                                                 if PreserveC_r = '0' then
  857.                                                         F(Flag_C) <= F_Out(0);
  858.                                                 end if;
  859.                                         end if;
  860.                                 end if;
  861.                                 if T_Res = '1' and I_INRC = '1' then
  862.                                         F(Flag_H) <= '0';
  863.                                         F(Flag_N) <= '0';
  864.                                         F(Flag_X) <= DI_Reg(3);
  865.                                         F(Flag_Y) <= DI_Reg(5);
  866.                                         if DI_Reg(7 downto 0) = "00000000" then
  867.                                                 F(Flag_Z) <= '1';
  868.                                         else
  869.                                                 F(Flag_Z) <= '0';
  870.                                         end if;
  871.                                         F(Flag_S) <= DI_Reg(7);
  872.                                         F(Flag_P) <= not (DI_Reg(0) xor DI_Reg(1) xor DI_Reg(2) xor DI_Reg(3) xor
  873.                                                 DI_Reg(4) xor DI_Reg(5) xor DI_Reg(6) xor DI_Reg(7));
  874.                                 end if;
  875. -- koe
  876. --                              if TState = 1 and Auto_Wait_t1 = '0' then
  877. --                                      -- Keep D0 from M3 for RLD/RRD (Sorgelig)
  878. --                                      I_RXDD <= I_RLD or I_RRD;
  879. --                                      if I_RXDD='0' then
  880. --                                              DO <= BusB;
  881. --                                      end if;
  882. --                                      if I_RLD = '1' then
  883. --                                              DO(3 downto 0) <= BusA(3 downto 0);
  884. --                                              DO(7 downto 4) <= BusB(3 downto 0);
  885. --                                      end if;
  886. --                                      if I_RRD = '1' then
  887. --                                              DO(3 downto 0) <= BusB(7 downto 4);
  888. --                                              DO(7 downto 4) <= BusA(3 downto 0);
  889. --                                      end if;
  890. --                              end if;
  891.  
  892.                                 if T_Res = '1' then
  893.                                         Read_To_Reg_r(3 downto 0) <= Set_BusA_To;
  894.                                         Read_To_Reg_r(4) <= Read_To_Reg;
  895.                                         if Read_To_Acc = '1' then
  896.                                                 Read_To_Reg_r(3 downto 0) <= "0111";
  897.                                                 Read_To_Reg_r(4) <= '1';
  898.                                         end if;
  899.                                 end if;
  900.  
  901.                                 if TState = 1 and I_BT = '1' then
  902.                                         F(Flag_X) <= ALU_Q(3);
  903.                                         F(Flag_Y) <= ALU_Q(1);
  904.                                         F(Flag_H) <= '0';
  905.                                         F(Flag_N) <= '0';
  906.                                 end if;
  907.                                 if TState = 1 and I_BC = '1' then
  908.                                         n := ALU_Q - ("0000000" & F_Out(Flag_H));
  909.                                         F(Flag_X) <= n(3);
  910.                                         F(Flag_Y) <= n(1);
  911.                                 end if;
  912.                                 if I_BC = '1' or I_BT = '1' then
  913.                                         F(Flag_P) <= IncDecZ;
  914.                                 end if;
  915.  
  916.                                 if (TState = 1 and Save_ALU_r = '0' and Auto_Wait_t1 = '0') or
  917.                                         (Save_ALU_r = '1' and ALU_OP_r /= "0111") then
  918.                                         case Read_To_Reg_r is
  919.                                         when "10111" =>
  920.                                                 ACC <= Save_Mux;
  921. --                                      when "10110" =>
  922. --                                              DO <= Save_Mux; -- koe
  923.                                         when "11000" =>
  924.                                                 SP(7 downto 0) <= unsigned(Save_Mux);
  925.                                         when "11001" =>
  926.                                                 SP(15 downto 8) <= unsigned(Save_Mux);
  927.                                         when "11011" =>
  928.                                                 F <= Save_Mux;
  929.                                         when others =>
  930.                                         end case;
  931. --                                      if XYbit_undoc='1' then -- koe
  932. --                                              DO <= ALU_Q;
  933. --                                      end if;
  934.                                 end if;
  935.                         end if;
  936.                 end if;
  937.                
  938. -- koe
  939. if RESET_n = '0' then DO <= "00000000"; I_RXDD <= '0';
  940.     elsif CLK_n'event and CLK_n = '0' then
  941.         if ClkEn = '1' then
  942.                                 if TState = 1 and Auto_Wait_t1 = '0' then
  943.                                         -- Keep D0 from M3 for RLD/RRD (Sorgelig)
  944.                                         I_RXDD <= I_RLD or I_RRD;
  945.                                         if I_RXDD='0' then
  946.                                                 DO <= BusB;
  947.                                         end if;
  948.                                         if I_RLD = '1' then
  949.                                                 DO(3 downto 0) <= BusA(3 downto 0);
  950.                                                 DO(7 downto 4) <= BusB(3 downto 0);
  951.                                         end if;
  952.                                         if I_RRD = '1' then
  953.                                                 DO(3 downto 0) <= BusB(7 downto 4);
  954.                                                 DO(7 downto 4) <= BusA(3 downto 0);
  955.                                         end if;
  956.                                 end if;
  957.                                 if (((TState = 1 and Save_ALU_r = '0' and Auto_Wait_t1 = '0') or
  958.                                         (Save_ALU_r = '1' and ALU_OP_r /= "0111"))) then
  959.                                                 if (Read_To_Reg_r = "10110") then DO <= Save_Mux; end if;
  960.                                                 if (XYbit_undoc='1') then       DO <= ALU_Q; end if;
  961.                                 end if;          
  962.         end if;
  963. end if;        
  964.                
  965.         end process;
  966.  
  967. ---------------------------------------------------------------------------
  968. --
  969. -- BC('), DE('), HL('), IX and IY
  970. --
  971. ---------------------------------------------------------------------------
  972.         process (CLK_n)
  973.         begin
  974.                 if rising_edge(CLK_n) then
  975.                         if ClkEn = '1' then
  976.                                 -- Bus A / Write
  977.                                 RegAddrA_r <= Alternate & Set_BusA_To(2 downto 1);
  978.                                 if XY_Ind = '0' and XY_State /= "00" and Set_BusA_To(2 downto 1) = "10" then
  979.                                         RegAddrA_r <= XY_State(1) & "11";
  980.                                 end if;
  981.  
  982.                                 -- Bus B
  983.                                 RegAddrB_r <= Alternate & Set_BusB_To(2 downto 1);
  984.                                 if XY_Ind = '0' and XY_State /= "00" and Set_BusB_To(2 downto 1) = "10" then
  985.                                         RegAddrB_r <= XY_State(1) & "11";
  986.                                 end if;
  987.  
  988.                                 -- Address from register
  989.                                 RegAddrC <= Alternate & Set_Addr_To(1 downto 0);
  990.                                 -- Jump (HL), LD SP,HL
  991.                                 if (JumpXY = '1' or LDSPHL = '1') then
  992.                                         RegAddrC <= Alternate & "10";
  993.                                 end if;
  994.                                 if ((JumpXY = '1' or LDSPHL = '1') and XY_State /= "00") or (MCycle = "110") then
  995.                                         RegAddrC <= XY_State(1) & "11";
  996.                                 end if;
  997.  
  998.                                 if I_DJNZ = '1' and Save_ALU_r = '1' and Mode < 2 then
  999.                                         IncDecZ <= F_Out(Flag_Z);
  1000.                                 end if;
  1001.                                 if (TState = 2 or (TState = 3 and MCycle = "001")) and IncDec_16(2 downto 0) = "100" then
  1002.                                         if ID16 = 0 then
  1003.                                                 IncDecZ <= '0';
  1004.                                         else
  1005.                                                 IncDecZ <= '1';
  1006.                                         end if;
  1007.                                 end if;
  1008.  
  1009.                                 RegBusA_r <= RegBusA;
  1010.                         end if;
  1011.                 end if;
  1012.         end process;
  1013.  
  1014.         RegAddrA <=
  1015.                         -- 16 bit increment/decrement
  1016.                         Alternate & IncDec_16(1 downto 0) when (TState = 2 or
  1017.                                 (TState = 3 and MCycle = "001" and IncDec_16(2) = '1')) and XY_State = "00" else
  1018.                         XY_State(1) & "11" when (TState = 2 or
  1019.                                 (TState = 3 and MCycle = "001" and IncDec_16(2) = '1')) and IncDec_16(1 downto 0) = "10" else
  1020.                         -- EX HL,DL
  1021.                         Alternate & "10" when ExchangeDH = '1' and TState = 3 else
  1022.                         Alternate & "01" when ExchangeDH = '1' and TState = 4 else
  1023.                         -- Bus A / Write
  1024.                         RegAddrA_r;
  1025.  
  1026.         RegAddrB <=
  1027.                         -- EX HL,DL
  1028.                         Alternate & "01" when ExchangeDH = '1' and TState = 3 else
  1029.                         -- Bus B
  1030.                         RegAddrB_r;
  1031.  
  1032.         ID16 <= signed(RegBusA) - 1 when IncDec_16(3) = '1' else
  1033.                         signed(RegBusA) + 1;
  1034.  
  1035.         process (Save_ALU_r, Auto_Wait_t1, ALU_OP_r, Read_To_Reg_r,
  1036.                         ExchangeDH, IncDec_16, MCycle, TState, Wait_n)
  1037.         begin
  1038.                 RegWEH <= '0';
  1039.                 RegWEL <= '0';
  1040.                 if (TState = 1 and Save_ALU_r = '0' and Auto_Wait_t1 = '0') or
  1041.                         (Save_ALU_r = '1' and ALU_OP_r /= "0111") then
  1042.                         case Read_To_Reg_r is
  1043.                         when "10000" | "10001" | "10010" | "10011" | "10100" | "10101" =>
  1044.                                 RegWEH <= not Read_To_Reg_r(0);
  1045.                                 RegWEL <= Read_To_Reg_r(0);
  1046.                         when others =>
  1047.                         end case;
  1048.                 end if;
  1049.  
  1050.                 if ExchangeDH = '1' and (TState = 3 or TState = 4) then
  1051.                         RegWEH <= '1';
  1052.                         RegWEL <= '1';
  1053.                 end if;
  1054.  
  1055.                 if IncDec_16(2) = '1' and ((TState = 2 and Wait_n = '1' and MCycle /= "001") or (TState = 3 and MCycle = "001")) then
  1056.                         case IncDec_16(1 downto 0) is
  1057.                         when "00" | "01" | "10" =>
  1058.                                 RegWEH <= '1';
  1059.                                 RegWEL <= '1';
  1060.                         when others =>
  1061.                         end case;
  1062.                 end if;
  1063.         end process;
  1064.  
  1065.         process (Save_Mux, RegBusB, RegBusA_r, ID16,
  1066.                         ExchangeDH, IncDec_16, MCycle, TState, Wait_n)
  1067.         begin
  1068.                 RegDIH <= Save_Mux;
  1069.                 RegDIL <= Save_Mux;
  1070.  
  1071.                 if ExchangeDH = '1' and TState = 3 then
  1072.                         RegDIH <= RegBusB(15 downto 8);
  1073.                         RegDIL <= RegBusB(7 downto 0);
  1074.                 end if;
  1075.                 if ExchangeDH = '1' and TState = 4 then
  1076.                         RegDIH <= RegBusA_r(15 downto 8);
  1077.                         RegDIL <= RegBusA_r(7 downto 0);
  1078.                 end if;
  1079.  
  1080.                 if IncDec_16(2) = '1' and ((TState = 2 and MCycle /= "001") or (TState = 3 and MCycle = "001")) then
  1081.                         RegDIH <= std_logic_vector(ID16(15 downto 8));
  1082.                         RegDIL <= std_logic_vector(ID16(7 downto 0));
  1083.                 end if;
  1084.         end process;
  1085.  
  1086.         Regs : T80_Reg
  1087.                 port map(
  1088.                         Clk => CLK_n,
  1089.                         CEN => ClkEn,
  1090.                         WEH => RegWEH,
  1091.                         WEL => RegWEL,
  1092.                         AddrA => RegAddrA,
  1093.                         AddrB => RegAddrB,
  1094.                         AddrC => RegAddrC,
  1095.                         DIH => RegDIH,
  1096.                         DIL => RegDIL,
  1097.                         DOAH => RegBusA(15 downto 8),
  1098.                         DOAL => RegBusA(7 downto 0),
  1099.                         DOBH => RegBusB(15 downto 8),
  1100.                         DOBL => RegBusB(7 downto 0),
  1101.                         DOCH => RegBusC(15 downto 8),
  1102.                         DOCL => RegBusC(7 downto 0),
  1103.                         DOR  => DOR,
  1104.                         DIRSet => DIRSet,
  1105.                         DIR  => DIR(207 downto 80));
  1106.  
  1107. ---------------------------------------------------------------------------
  1108. --
  1109. -- Buses
  1110. --
  1111. ---------------------------------------------------------------------------
  1112.         process (CLK_n)
  1113.         begin
  1114.                 if rising_edge(CLK_n) then
  1115.                         if ClkEn = '1' then
  1116.                                 case Set_BusB_To is
  1117.                                 when "0111" =>
  1118.                                         BusB <= ACC;
  1119.                                 when "0000" | "0001" | "0010" | "0011" | "0100" | "0101" =>
  1120.                                         if Set_BusB_To(0) = '1' then
  1121.                                                 BusB <= RegBusB(7 downto 0);
  1122.                                         else
  1123.                                                 BusB <= RegBusB(15 downto 8);
  1124.                                         end if;
  1125.                                 when "0110" =>
  1126.                                         BusB <= DI_Reg;
  1127.                                 when "1000" =>
  1128.                                         BusB <= std_logic_vector(SP(7 downto 0));
  1129.                                 when "1001" =>
  1130.                                         BusB <= std_logic_vector(SP(15 downto 8));
  1131.                                 when "1010" =>
  1132.                                         BusB <= "00000001";
  1133.                                 when "1011" =>
  1134.                                         BusB <= F;
  1135.                                 when "1100" =>
  1136.                                         BusB <= std_logic_vector(PC(7 downto 0));
  1137.                                 when "1101" =>
  1138.                                         BusB <= std_logic_vector(PC(15 downto 8));
  1139.                                 when "1110" =>
  1140.                                         if IR = x"71" and out0 = '1' then
  1141.                                                 BusB <= "11111111";
  1142.                                         else
  1143.                                         BusB <= "00000000";
  1144.                                         end if;
  1145.                                 when others =>
  1146.                                         BusB <= "--------";
  1147.                                 end case;
  1148.  
  1149.                                 case Set_BusA_To is
  1150.                                 when "0111" =>
  1151.                                         BusA <= ACC;
  1152.                                 when "0000" | "0001" | "0010" | "0011" | "0100" | "0101" =>
  1153.                                         if Set_BusA_To(0) = '1' then
  1154.                                                 BusA <= RegBusA(7 downto 0);
  1155.                                         else
  1156.                                                 BusA <= RegBusA(15 downto 8);
  1157.                                         end if;
  1158.                                 when "0110" =>
  1159.                                         BusA <= DI_Reg;
  1160.                                 when "1000" =>
  1161.                                         BusA <= std_logic_vector(SP(7 downto 0));
  1162.                                 when "1001" =>
  1163.                                         BusA <= std_logic_vector(SP(15 downto 8));
  1164.                                 when "1010" =>
  1165.                                         BusA <= "00000000";
  1166.                                 when others =>
  1167.                                         BusA <= "--------";
  1168.                                 end case;
  1169.                                 if XYbit_undoc='1' then
  1170.                                         BusA <= DI_Reg;
  1171.                                         BusB <= DI_Reg;
  1172.                                 end if;
  1173.                         end if;
  1174.                 end if;
  1175.         end process;
  1176.  
  1177. ---------------------------------------------------------------------------
  1178. --
  1179. -- Generate external control signals
  1180. --
  1181. ---------------------------------------------------------------------------
  1182.         process (RESET_n,CLK_n)
  1183.         begin
  1184.                 if RESET_n = '0' then
  1185.                         RFSH_n <= '1';
  1186.                 elsif rising_edge(CLK_n) then
  1187.                         if DIRSet = '0' and CEN = '1' then
  1188.                                 if MCycle = "001" and ((TState = 2  and Wait_n = '1') or TState = 3) then
  1189.                                         RFSH_n <= '0';
  1190.                                 else
  1191.                                         RFSH_n <= '1';
  1192.                                 end if;
  1193.                         end if;
  1194.                 end if;
  1195.         end process;
  1196.  
  1197.         MC <= std_logic_vector(MCycle);
  1198.         TS <= std_logic_vector(TState);
  1199.         DI_Reg <= DI;
  1200.         HALT_n <= not Halt_FF;
  1201.         BUSAK_n <= not BusAck;
  1202.         IntCycle_n <= not IntCycle;
  1203.         IntE <= IntE_FF1;
  1204.         IORQ <= IORQ_i;
  1205.         Stop <= I_DJNZ;
  1206.  
  1207. -------------------------------------------------------------------------
  1208. --
  1209. -- Main state machine
  1210. --
  1211. -------------------------------------------------------------------------
  1212.         process (RESET_n, CLK_n)
  1213.                 variable OldNMI_n : std_logic;
  1214.         begin
  1215.                 if RESET_n = '0' then
  1216.                         MCycle <= "001";
  1217.                         TState <= "000";
  1218.                         Pre_XY_F_M <= "000";
  1219.                         Halt_FF <= '0';
  1220.                         BusAck <= '0';
  1221.                         NMICycle <= '0';
  1222.                         IntCycle <= '0';
  1223.                         IntE_FF1 <= '0';
  1224.                         IntE_FF2 <= '0';
  1225.                         No_BTR <= '0';
  1226.                         Auto_Wait_t1 <= '0';
  1227.                         Auto_Wait_t2 <= '0';
  1228.                         M1_n <= '1';
  1229.                         BusReq_s <= '0';
  1230.                         NMI_s <= '0';
  1231.                 elsif rising_edge(CLK_n) then
  1232.  
  1233.                         if DIRSet = '1' then
  1234.                                 IntE_FF2 <= DIR(211);
  1235.                                 IntE_FF1 <= DIR(210);
  1236.                         else
  1237.                                 if NMI_n = '0' and OldNMI_n = '1' then
  1238.                                         NMI_s <= '1';
  1239.                                 end if;
  1240.                                 OldNMI_n := NMI_n;
  1241.  
  1242.                                 if CEN = '1' then
  1243.                                         BusReq_s <= not BUSRQ_n;
  1244.                                         Auto_Wait_t2 <= Auto_Wait_t1;
  1245.                                         if T_Res = '1' then
  1246.                                                 Auto_Wait_t1 <= '0';
  1247.                                                 Auto_Wait_t2 <= '0';
  1248.                                         else
  1249.                                                 Auto_Wait_t1 <= Auto_Wait or IORQ_i;
  1250.                                         end if;
  1251.                                         No_BTR <= (I_BT and (not IR(4) or not F(Flag_P))) or
  1252.                                                         (I_BC and (not IR(4) or F(Flag_Z) or not F(Flag_P))) or
  1253.                                                         (I_BTR and (not IR(4) or F(Flag_Z)));
  1254.                                         if TState = 2 then
  1255.                                                 if SetEI = '1' then
  1256.                                                         IntE_FF1 <= '1';
  1257.                                                         IntE_FF2 <= '1';
  1258.                                                 end if;
  1259.                                                 if I_RETN = '1' then
  1260.                                                         IntE_FF1 <= IntE_FF2;
  1261.                                                 end if;
  1262.                                         end if;
  1263.                                         if TState = 3 then
  1264.                                                 if SetDI = '1' then
  1265.                                                         IntE_FF1 <= '0';
  1266.                                                         IntE_FF2 <= '0';
  1267.                                                 end if;
  1268.                                         end if;
  1269.                                         if IntCycle = '1' or NMICycle = '1' then
  1270.                                                 Halt_FF <= '0';
  1271.                                         end if;
  1272.                                         if MCycle = "001" and TState = 2 and Wait_n = '1' then
  1273.                                                 M1_n <= '1';
  1274.                                         end if;
  1275.                                         if BusReq_s = '1' and BusAck = '1' then
  1276.                                         else
  1277.                                                 BusAck <= '0';
  1278.                                                 if TState = 2 and Wait_n = '0' then
  1279.                                                 elsif T_Res = '1' then
  1280.                                                         if Halt = '1' then
  1281.                                                                 Halt_FF <= '1';
  1282.                                                         end if;
  1283.                                                         if BusReq_s = '1' then
  1284.                                                                 BusAck <= '1';
  1285.                                                         else
  1286.                                                                 TState <= "001";
  1287.                                                                 if NextIs_XY_Fetch = '1' then
  1288.                                                                         MCycle <= "110";
  1289.                                                                         Pre_XY_F_M <= MCycle;
  1290.                                                                         if IR = "00110110" and Mode = 0 then
  1291.                                                                                 Pre_XY_F_M <= "010";
  1292.                                                                         end if;
  1293.                                                                 elsif (MCycle = "111") or (MCycle = "110" and Mode = 1 and ISet /= "01") then
  1294.                                                                         MCycle <= std_logic_vector(unsigned(Pre_XY_F_M) + 1);
  1295.                                                                 elsif (MCycle = MCycles) or No_BTR = '1' or (MCycle = "010" and I_DJNZ = '1' and IncDecZ = '1') then
  1296.                                                                         M1_n <= '0';
  1297.                                                                         MCycle <= "001";
  1298.                                                                         IntCycle <= '0';
  1299.                                                                         NMICycle <= '0';
  1300.                                                                         if NMI_s = '1' and Prefix = "00" then
  1301.                                                                                 NMI_s    <= '0';
  1302.                                                                                 NMICycle <= '1';
  1303.                                                                                 IntE_FF1 <= '0';
  1304.                                                                         elsif IntE_FF1 = '1' and INT_n='0' and Prefix = "00" and SetEI = '0' then
  1305.                                                                                 IntCycle <= '1';
  1306.                                                                                 IntE_FF1 <= '0';
  1307.                                                                                 IntE_FF2 <= '0';
  1308.                                                                         end if;
  1309.                                                                 else
  1310.                                                                         MCycle <= std_logic_vector(unsigned(MCycle) + 1);
  1311.                                                                 end if;
  1312.                                                         end if;
  1313.                                                 else
  1314.                                                         if (Auto_Wait = '1' and Auto_Wait_t2 = '0') nor
  1315.                                                                 (IOWait = 1 and IORQ_i = '1' and Auto_Wait_t1 = '0') then
  1316.                                                                 TState <= TState + 1;
  1317.                                                         end if;
  1318.                                                 end if;
  1319.                                         end if;
  1320.                                         if TState = 0 then
  1321.                                                 M1_n <= '0';
  1322.                                         end if;
  1323.                                 end if;
  1324.                         end if;
  1325.                 end if;
  1326.         end process;
  1327.  
  1328.         Auto_Wait <= '1' when IntCycle = '1' and MCycle = "001" else '0';
  1329. end;
  1330.