uses spectrum,{asmZ80,}IS18WG93,crt,strng,specproc;{отладчик Z80}
type pstring=^string;
var
HexMode:boolean;
currentPC,maxcurrentPC:word;
LastShape : word;
procedure HideCursor; assembler;
asm
 mov    ah,03h
 int    10h
 mov    LastShape,cx
 mov    ch,20h
 mov    ah,01h
 int    10h
end;
procedure ShowCursor; assembler;
asm
 mov    cx,LastShape
 mov    ah,01h
 int    10h
end;
function wrd(_hi,_lo:byte):word;
begin
wrd:=$100*_hi+_lo;
end;
procedure MyVal(__s:string;var __r:word);
var __w:word;
 __start,__b,__c:byte;
 __i:integer;
begin
__r:=0;
if HexMode then
 begin
 __w:=$1;
 __start:=ord(__s[0]);
 if __start>4 then __start:=4;
 for __c:=__start downto 1 do
  begin
  __b:=pos(__s[__c],HXs);
  if __b>0 then dec(__b);
  inc(__r,__w*__b);
  __w:=__w shl 4;
  end;
 end else val(__s,__r,__i);
end;
function GetKey:char;
var __key:char;
begin
while keypressed do readkey;
__key:=readkey;
if __key=#0 then __key:=readkey;
GetKey:=__key;
end;

procedure chrUp(var __c:char);
begin
if (__c>='a')and(__c<='z')then dec(__c,$20);
end;
procedure strUp(var __s:string);
var i:integer;
begin
for i:=1 to ord(__s[0]) do chrUp(__s[i]);
end;
function reStr(_s:string;_operand_s:pstring):word;
var _r:word;
__s:string;
 function s_reg(_pos:byte):string;
  begin
   s_reg:=copy(_s,_pos,ord(_s[0])-_pos+1);
  end;
begin
_operand_s^:='';
strUp(_s);
if (_s[2]='R') then
 begin
 __s:=s_reg(3);
  if __s='PC'then _r:=sp_reg.PC else
  if __s='SP'then _r:=sp_reg.SP else
  if __s='IX'then _r:=sp_reg.IX else
  if __s='IY'then _r:=sp_reg.IY else
  if __s='AF'then _r:=reg_w.FA else
  if __s='BC'then _r:=reg_w.BC else
  if __s='DE'then _r:=reg_w.DE else
  if __s='HL'then _r:=reg_w.HL else
  if __s='AF`'then _r:=reg__w.FA else
  if __s='BC`'then _r:=reg__w.BC else
  if __s='DE`'then _r:=reg__w.DE else
  if __s='HL`'then _r:=reg__w.HL else
  if __s='A'then _r:=reg.A else
  if __s='F'then _r:=reg.F else
  if __s='B'then _r:=reg.B else
  if __s='C'then _r:=reg.C else
  if __s='D'then _r:=reg.D else
  if __s='E'then _r:=reg.E else
  if __s='H'then _r:=reg.H else
  if __s='L'then _r:=reg.L else
  if __s='A`'then _r:=reg_.A else
  if __s='F`'then _r:=reg_.F else
  if __s='B`'then _r:=reg_.B else
  if __s='C`'then _r:=reg_.C else
  if __s='D`'then _r:=reg_.D else
  if __s='E`'then _r:=reg_.E else
  if __s='H`'then _r:=reg_.H else
  if __s='L`'then _r:=reg_.L else
   begin  _r:=0;__s:='';end;
 _operand_s^:=__s;
 end else MyVal(Copy(_S, 2, ord(_s[0])),_r);
reStr:=_r;
end;
function reStrB(_s:string):byte;
var _r:byte;
begin
_r:=$FF;
if ord(_s[0])<2 then _r:=0;
reStrB:=_r;
end;
(****************************************************************************)
function w_To_s(_w:word):string;
var _s:string;
begin
if HexMode then _s:='$'+whw(_w) else str(_w,_s);
w_To_s:=_s;
end;
(****************************************************************************)
function b_To_s(_b:byte):string;
var _s:string;
begin
if HexMode then _s:='$'+whb(_b) else str(_b,_s);
b_To_s:=_s;
end;
(****************************************************************************)
procedure WriteWord(_writeDat:word);
begin
clreol;
write(w_To_s(_writeDat));
end;
procedure WritelnWord(_writeDat:word);
begin
WriteWord(_writeDat);
writeln;
end;
(****************************************************************************)
function s_mem_w(__adr:word):string;
begin
s_mem_w:=w_To_s(wrd(memory^[__adr+1],memory^[__adr]));
end;
(****************************************************************************)
function s_mem_b(__adr:word):string;
begin
s_mem_b:=b_To_s(memory^[__adr]);
end;
(****************************************************************************)
function s_memAdr_w(__adr:word):string;
var __addr:word;
begin
__addr:=wrd(memory^[__adr+1],memory^[__adr]);
s_memAdr_w:=w_To_s(wrd(memory^[__addr+1],memory^[__addr]));
end;
(****************************************************************************)
function s_idx(__adr:word):string;
var __s:byte;
_s:string;
begin
__s:=mem_b(__adr+1);
if __s>=$80 then
 begin
 _s:=b_To_s($100-__s);
 _s:='-'+_s;
 end else
 begin
 _s:=b_To_s(__s);
 _s:='+'+_s;
 end;
s_idx:=_s
end;
(****************************************************************************)
function s_memAdr_b(__adr:word):string;
var __addr:word;
begin
__addr:=wrd(memory^[__adr+1],memory^[__adr]);
s_memAdr_b:=b_To_s(memory^[__addr]);
end;
(****************************************************************************)
function s_IdxAdr_b(_w_reg,__adr:word):string;
begin
s_IdxAdr_b:=s_mem_b(reIndex(_w_reg,mem_b(__adr+1)));
end;
(****************************************************************************)
function s_SummIdxAdres(_w_reg,__adr:word):string;
begin
s_SummIdxAdres:=sScob1+w_To_s(reIndex(_w_reg,mem_b(__adr+1)))+sScob2;
end;
(****************************************************************************)
(****************************************************************************)
function CodeToAsm(_adr:word;var _size:byte;var sp_str:string):string;
var _err_code:boolean;
_s:string;
c_reg:tRegCod;
w_reg:word;
function s_koswAdr(__adr:word):string;
begin
s_koswAdr:=sScob1+sRegCod[c_reg]+s_idx(__adr)+sScob2;
end;
(****************************************************************************)
procedure d_xxRegN(sXX:string;RegCod:tRegCod);
begin
CodeToAsm:=sXX+sRegCod[RegCod]+sZpt+s_mem_b(_adr+1);
_size:=2;
end;
(****************************************************************************)
procedure d_LDRegNN(RegCod:tRegCod);
begin
CodeToAsm:=sLD+sRegCod[RegCod]+sZpt+s_mem_w(_adr+1);
_size:=3;
end;
(****************************************************************************)
procedure d_LDIdxNN(RegCod:tRegCod);
begin
d_LDRegNN(RegCod);
_size:=4;
end;
(****************************************************************************)
procedure d_xxRegReg(sXX:string;toRegCod,fromRegCod:tRegCod);
begin
CodeToAsm:=sXX+sRegCod[toRegCod]+sZpt+sRegCod[fromRegCod];
_size:=1;
end;
(****************************************************************************)
procedure d_xxRegIR(sXX:string;toRegCod,fromRegCod:tRegCod);
begin
d_xxRegReg(sXX,toRegCod,fromRegCod);
_size:=2;
end;
(****************************************************************************)
procedure d_LDRegAdr(RegCod:tRegCod);
begin
_s:=sScob1+s_mem_w(_adr+1)+sScob2;
CodeToAsm:=sLD+sRegCod[RegCod]+sZpt+_s;
_s:=_s+sRwn+s_memAdr_b(_adr+1);
_size:=3;
end;
(****************************************************************************)
procedure d_LDRpAdr(RegCod:tRegCod);
begin
_s:=sScob1+s_mem_w(_adr+1)+sScob2;
CodeToAsm:=sLD+sRegCod[RegCod]+sZpt+_s;
_s:=_s+sRwn+s_memAdr_w(_adr+1);
_size:=3;
end;
(****************************************************************************)
procedure d_LDRpIdx(RegCod:tRegCod);
begin
d_LDRpAdr(RegCod);
_size:=4;
end;
(****************************************************************************)
procedure d_xxRegArp(sXX:string;toRegCod,adrRegCod:tRegCod;RealAddr:word);
begin
_s:=sScob1+sRegCod[adrRegCod]+sScob2;
CodeToAsm:=sXX+sRegCod[toRegCod]+sZpt+_s;
_s:=_s+sRwn+s_mem_b(RealAddr);
_size:=1;
end;
(****************************************************************************)
procedure d_xxRegIdx(sXX:string;toRegCod:tRegCod;RealAddr:word);
begin
CodeToAsm:=sXX+sRegCod[toRegCod]+sZpt+s_koswAdr(_adr);
_s:=s_SummIdxAdres(RealAddr,_adr)+sRwn+s_IdxAdr_b(RealAddr,_adr);
_size:=3;
end;
(****************************************************************************)
procedure d_EXRpRp(a,b:tRegCod;_strih:string);
begin
CodeToAsm:=sEX+sRegCod[a]+sZpt+sRegCod[b]+_strih;
_size:=1;
end;
(****************************************************************************)
procedure d_LDAdrReg(RegCod:tRegCod);
begin
_s:=sScob1+s_mem_w(_adr+1)+sScob2;
CodeToAsm:=sLD+_s+sZpt+sRegCod[RegCod];
_s:=_s+sRwn+s_memAdr_b(_adr+1);
_size:=3;
end;
(****************************************************************************)
procedure d_LDAdrRp(RegCod:tRegCod);
begin
_s:=sScob1+s_mem_w(_adr+1)+sScob2;
CodeToAsm:=sLD+_s+sZpt+sRegCod[RegCod];
_s:=_s+sRwn+s_memAdr_w(_adr+1);
_size:=3;
end;
(****************************************************************************)
procedure d_LDIdxRp(RegCod:tRegCod);
begin
d_LDAdrRp(RegCod);
_size:=4;
end;
(****************************************************************************)
procedure d_LDArpReg(adrRegCod,fromRegCod:tRegCod;RealAddr:word);
begin
_s:=sScob1+sRegCod[adrRegCod]+sScob2;
CodeToAsm:=sLD+_s+sZpt+sRegCod[fromRegCod];
_s:=_s+sRwn+s_mem_b(RealAddr);
_size:=1;
end;
(****************************************************************************)
procedure d_LDArpN(adrRegCod:tRegCod;RealAddr:word);
begin
_s:=sScob1+sRegCod[adrRegCod]+sScob2;
CodeToAsm:=sLD+_s+sZpt+s_mem_b(_adr+1);
_s:=_s+sRwn+s_mem_b(RealAddr);
_size:=2;
end;
(****************************************************************************)
procedure d_LDIdxReg(fromRegCod:tRegCod;RealAddr:word);
begin
CodeToAsm:=sLD+s_koswAdr(_adr)+sZpt+sRegCod[fromRegCod];
_s:=s_SummIdxAdres(RealAddr,_adr)+sRwn+s_IdxAdr_b(RealAddr,_adr);
_size:=3;
end;
(****************************************************************************)
procedure d_LDIdxN(RealAddr:word);
begin
CodeToAsm:=sLD+s_koswAdr(_adr)+sZpt+s_mem_b(_adr+2);
_s:=s_SummIdxAdres(RealAddr,_adr)+sRwn+s_IdxAdr_b(RealAddr,_adr);
_size:=4;
end;
(****************************************************************************)
procedure d_xReg(sXX:string;RegCod:tRegCod;CmdSize:byte);
begin
CodeToAsm:=sXX+sRegCod[RegCod];
_size:=CmdSize;
end;
(****************************************************************************)
procedure d_xArp(sXX:string;RegCod:tRegCod;RealAddr:word;CmdSize:byte);
begin
_s:=sScob1+sRegCod[RegCod]+sScob2;
CodeToAsm:=sXX+_s;
_s:=_s+sRwn+s_mem_b(RealAddr);
_size:=CmdSize;
end;
(****************************************************************************)
procedure d_xIdx(sXX:string;RealAddr:word;CmdSize:byte);
begin
CodeToAsm:=sXX+s_koswAdr(_adr);
_s:=s_SummIdxAdres(RealAddr,_adr)+sRwn+s_IdxAdr_b(RealAddr,_adr);
_size:=CmdSize;
end;
(****************************************************************************)
procedure d_xN(sXX:string);
begin
CodeToAsm:=sXX+s_mem_b(_adr+1);
_size:=2;
end;
(****************************************************************************)
procedure d_GoNN(sGo:string);
begin
CodeToAsm:=sGo+s_mem_w(_adr+1);
_size:=3;
end;
(****************************************************************************)
procedure d_JpRp(RegAdr:tRegCod);
begin
CodeToAsm:=sJP+sScob1+sRegCod[RegAdr]+sScob2;
_size:=1;
end;
(****************************************************************************)
procedure d_GoIF(sGo:string;ifXX:tIfCod);
begin
CodeToAsm:=sGo+sIfCod[ifXX]+sZpt+s_mem_w(_adr+1);
_size:=3;
end;
(****************************************************************************)
procedure d_JrIf(ifXX:tIfCod);
begin
CodeToAsm:=sJR+sIfCod[ifXX]+sZpt+w_To_s(reIndex(_adr+2,mem_b(_adr+1)));
_size:=2;
end;
(****************************************************************************)
procedure d_Jr;
begin
CodeToAsm:=sJR+w_To_s(reIndex(_adr+2,mem_b(_adr+1)));
_size:=2;
end;
(****************************************************************************)
procedure d_DJNZ;
begin
CodeToAsm:=sDJNZ+w_To_s(reIndex(_adr+2,mem_b(_adr+1)));
_size:=2;
end;
(****************************************************************************)
procedure d_stckReg(sStck:string;RegCod:tRegCod);
begin
CodeToAsm:=sStck+sRegCod[RegCod];
_size:=1;
end;
(****************************************************************************)
procedure d_stckIdx(sStck:string;RegCod:tRegCod);
begin
d_stckReg(sStck,RegCod);
_size:=2;
end;
(****************************************************************************)
procedure d_EXSpReg(RegCod:tRegCod);
begin
_s:=sScob1+sRegCod[rSP]+sScob2;
CodeToAsm:=sEX+_s+sZpt+sRegCod[rHL];
_s:=_s+sRwn+w_To_s(mem_w(sp_reg.SP));
_size:=1;
end;
(****************************************************************************)
procedure d_EXSpIx(RegCod:tRegCod);
begin
d_EXSpReg(RegCod);
_size:=2;
end;
(****************************************************************************)
procedure d_RST(adres:byte);
begin
CodeToAsm:=sRST+b_To_s(adres);
_size:=1;
end;
(****************************************************************************)
procedure d_str(CmdStr:string;CmdSize:byte);
begin
CodeToAsm:=CmdStr;
_size:=CmdSize;
end;
(****************************************************************************)
procedure d_RETif(ifXX:tIfCod);
begin
CodeToAsm:=sRET+sIfCod[ifXX];
_size:=1;
end;
(****************************************************************************)
procedure d_INRegAN;
begin
CodeToAsm:=sIN+sRegCod[rA]+sZpt+sScob1+b_To_s(mem_b(_adr+1))+sScob2;
_size:=2;
end;
(****************************************************************************)
procedure d_IN(RegCod:tRegCod);
begin
CodeToAsm:=sIN+sRegCod[RegCod]+sZpt+sScob1+sRegCod[rC]+sScob2;
_size:=2;
end;
(****************************************************************************)
procedure d_OUTRegAN;
begin
CodeToAsm:=sOUT+sScob1+b_To_s(mem_b(_adr+1))+sScob2+sZpt+sRegCod[rA];
_size:=2;
end;
(****************************************************************************)
procedure d_OUT(RegCod:tRegCod);
begin
CodeToAsm:=sOUT+sScob1+sRegCod[rC]+sScob2+sZpt+sRegCod[RegCod];
_size:=2;
end;
(****************************************************************************)
begin
_s:='';
_err_code:=false;
case memory^[_adr]of
$00:d_str(sNOP,1);
$01:d_LDRegNN(rBC);
$02:d_LDArpReg(rBC,rA,reg_w.BC);
$03:d_xReg(sINC,rBC,1);
$04:d_xReg(sINC,rB,1);
$05:d_xReg(sDEC,rB,1);
$06:d_xxRegN(sLD,rB);
$07:d_str(sRLCA,1);
$08:d_EXRpRp(rAF,rAF,'`');
$09:d_xxRegReg(sADD,rHL,rBC);
$0A:d_xxRegArp(sLD,rA,rBC,reg_w.BC);
$0B:d_xReg(sDEC,rBC,1);
$0C:d_xReg(sINC,rC,1);
$0D:d_xReg(sDEC,rC,1);
$0E:d_xxRegN(sLD,rC);
$0F:d_str(sRRCA,1);
$10:d_DJNZ;
$11:d_LDRegNN(rDE);
$12:d_LDArpReg(rDE,rA,reg_w.DE);
$13:d_xReg(sINC,rDE,1);
$14:d_xReg(sINC,rD,1);
$15:d_xReg(sDEC,rD,1);
$16:d_xxRegN(sLD,rD);
$17:d_str(sRLA,1);
$18:d_Jr;
$19:d_xxRegReg(sADD,rHL,rDE);
$1A:d_xxRegArp(sLD,rA,rDE,reg_w.DE);
$1B:d_xReg(sDEC,rDE,1);
$1C:d_xReg(sINC,rE,1);
$1D:d_xReg(sDEC,rE,1);
$1E:d_xxRegN(sLD,rE);
$1F:d_str(sRRA,1);
$20:d_JrIf(ifNZ);
$21:d_LDRegNN(rHL);
$22:d_LDAdrRp(rHL);
$23:d_xReg(sINC,rHL,1);
$24:d_xReg(sINC,rH,1);
$25:d_xReg(sDEC,rH,1);
$26:d_xxRegN(sLD,rH);
$27:d_str(sDAA,1);
$28:d_JrIf(ifZ);
$29:d_xxRegReg(sADD,rHL,rHL);
$2A:d_LDRpAdr(rHL);
$2B:d_xReg(sDEC,rHL,1);
$2C:d_xReg(sINC,rL,1);
$2D:d_xReg(sDEC,rL,1);
$2E:d_xxRegN(sLD,rL);
$2F:d_str(sCPL,1);
$30:d_JrIf(ifNC);
$31:d_LDRegNN(rSP);
$32:d_LDAdrReg(rA);
$33:d_xReg(sINC,rSP,1);
$34:d_xArp(sINC,rHL,reg_w.HL,1);
$35:d_xArp(sDEC,rHL,reg_w.HL,1);
$36:d_LDArpN(rHL,reg_w.HL);
$37:d_str(sSCF,1);
$38:d_JrIf(ifC);
$39:d_xxRegReg(sADD,rHL,rSP);
$3A:d_LDRegAdr(rA);
$3B:d_xReg(sDEC,rSP,1);
$3C:d_xReg(sINC,rA,1);
$3D:d_xReg(sDEC,rA,1);
$3E:d_xxRegN(sLD,rA);
$3F:d_str(sCCF,1);
$40:d_xxRegReg(sLD,rB,rB);
$41:d_xxRegReg(sLD,rB,rC);
$42:d_xxRegReg(sLD,rB,rD);
$43:d_xxRegReg(sLD,rB,rE);
$44:d_xxRegReg(sLD,rB,rH);
$45:d_xxRegReg(sLD,rB,rL);
$46:d_xxRegArp(sLD,rB,rHL,reg_w.HL);
$47:d_xxRegReg(sLD,rB,rA);
$48:d_xxRegReg(sLD,rC,rB);
$49:d_xxRegReg(sLD,rC,rC);
$4A:d_xxRegReg(sLD,rC,rD);
$4B:d_xxRegReg(sLD,rC,rE);
$4C:d_xxRegReg(sLD,rC,rH);
$4D:d_xxRegReg(sLD,rC,rL);
$4E:d_xxRegArp(sLD,rC,rHL,reg_w.HL);
$4F:d_xxRegReg(sLD,rC,rA);
$50:d_xxRegReg(sLD,rD,rB);
$51:d_xxRegReg(sLD,rD,rC);
$52:d_xxRegReg(sLD,rD,rD);
$53:d_xxRegReg(sLD,rD,rE);
$54:d_xxRegReg(sLD,rD,rH);
$55:d_xxRegReg(sLD,rD,rL);
$56:d_xxRegArp(sLD,rD,rHL,reg_w.HL);
$57:d_xxRegReg(sLD,rD,rA);
$58:d_xxRegReg(sLD,rE,rB);
$59:d_xxRegReg(sLD,rE,rC);
$5A:d_xxRegReg(sLD,rE,rD);
$5B:d_xxRegReg(sLD,rE,rE);
$5C:d_xxRegReg(sLD,rE,rH);
$5D:d_xxRegReg(sLD,rE,rL);
$5E:d_xxRegArp(sLD,rE,rHL,reg_w.HL);
$5F:d_xxRegReg(sLD,rE,rA);
$60:d_xxRegReg(sLD,rH,rB);
$61:d_xxRegReg(sLD,rH,rC);
$62:d_xxRegReg(sLD,rH,rD);
$63:d_xxRegReg(sLD,rH,rE);
$64:d_xxRegReg(sLD,rH,rH);
$65:d_xxRegReg(sLD,rH,rL);
$66:d_xxRegArp(sLD,rH,rHL,reg_w.HL);
$67:d_xxRegReg(sLD,rH,rA);
$68:d_xxRegReg(sLD,rL,rB);
$69:d_xxRegReg(sLD,rL,rC);
$6A:d_xxRegReg(sLD,rL,rD);
$6B:d_xxRegReg(sLD,rL,rE);
$6C:d_xxRegReg(sLD,rL,rH);
$6D:d_xxRegReg(sLD,rL,rL);
$6E:d_xxRegArp(sLD,rL,rHL,reg_w.HL);
$6F:d_xxRegReg(sLD,rL,rA);
$70:d_LDArpReg(rHL,rB,reg_w.HL);
$71:d_LDArpReg(rHL,rC,reg_w.HL);
$72:d_LDArpReg(rHL,rD,reg_w.HL);
$73:d_LDArpReg(rHL,rE,reg_w.HL);
$74:d_LDArpReg(rHL,rH,reg_w.HL);
$75:d_LDArpReg(rHL,rL,reg_w.HL);
$76:d_str(sHALT,1);
$77:d_LDArpReg(rHL,rA,reg_w.HL);
$78:d_xxRegReg(sLD,rA,rB);
$79:d_xxRegReg(sLD,rA,rC);
$7A:d_xxRegReg(sLD,rA,rD);
$7B:d_xxRegReg(sLD,rA,rE);
$7C:d_xxRegReg(sLD,rA,rH);
$7D:d_xxRegReg(sLD,rA,rL);
$7E:d_xxRegArp(sLD,rA,rHL,reg_w.HL);
$7F:d_xxRegReg(sLD,rA,rA);
$80:d_xxRegReg(sADD,rA,rB);
$81:d_xxRegReg(sADD,rA,rC);
$82:d_xxRegReg(sADD,rA,rD);
$83:d_xxRegReg(sADD,rA,rE);
$84:d_xxRegReg(sADD,rA,rH);
$85:d_xxRegReg(sADD,rA,rL);
$86:d_xxRegArp(sADD,rA,rHL,reg_w.HL);
$87:d_xxRegReg(sADD,rA,rA);
$88:d_xxRegReg(sADC,rA,rB);
$89:d_xxRegReg(sADC,rA,rC);
$8A:d_xxRegReg(sADC,rA,rD);
$8B:d_xxRegReg(sADC,rA,rE);
$8C:d_xxRegReg(sADC,rA,rH);
$8D:d_xxRegReg(sADC,rA,rL);
$8E:d_xxRegArp(sADC,rA,rHL,reg_w.HL);
$8F:d_xxRegReg(sADC,rA,rA);
$90:d_xReg(sSUB,rB,1);
$91:d_xReg(sSUB,rC,1);
$92:d_xReg(sSUB,rD,1);
$93:d_xReg(sSUB,rE,1);
$94:d_xReg(sSUB,rH,1);
$95:d_xReg(sSUB,rL,1);
$96:d_xArp(sSUB,rHL,reg_w.HL,1);
$97:d_xReg(sSUB,rA,1);
$98:d_xxRegReg(sSBC,rA,rB);
$99:d_xxRegReg(sSBC,rA,rC);
$9A:d_xxRegReg(sSBC,rA,rD);
$9B:d_xxRegReg(sSBC,rA,rE);
$9C:d_xxRegReg(sSBC,rA,rH);
$9D:d_xxRegReg(sSBC,rA,rL);
$9E:d_xxRegArp(sSBC,rA,rHL,reg_w.HL);
$9F:d_xxRegReg(sSBC,rA,rA);
$A0:d_xReg(sAND,rB,1);
$A1:d_xReg(sAND,rC,1);
$A2:d_xReg(sAND,rD,1);
$A3:d_xReg(sAND,rE,1);
$A4:d_xReg(sAND,rH,1);
$A5:d_xReg(sAND,rL,1);
$A6:d_xArp(sAND,rHL,reg_w.HL,1);
$A7:d_xReg(sAND,rA,1);
$A8:d_xReg(sXOR,rB,1);
$A9:d_xReg(sXOR,rC,1);
$AA:d_xReg(sXOR,rD,1);
$AB:d_xReg(sXOR,rE,1);
$AC:d_xReg(sXOR,rH,1);
$AD:d_xReg(sXOR,rL,1);
$AE:d_xArp(sXOR,rHL,reg_w.HL,1);
$AF:d_xReg(sXOR,rA,1);
$B0:d_xReg(sOR,rB,1);
$B1:d_xReg(sOR,rC,1);
$B2:d_xReg(sOR,rD,1);
$B3:d_xReg(sOR,rE,1);
$B4:d_xReg(sOR,rH,1);
$B5:d_xReg(sOR,rL,1);
$B6:d_xArp(sOR,rHL,reg_w.HL,1);
$B7:d_xReg(sOR,rA,1);
$B8:d_xReg(sCP,rB,1);
$B9:d_xReg(sCP,rC,1);
$BA:d_xReg(sCP,rD,1);
$BB:d_xReg(sCP,rE,1);
$BC:d_xReg(sCP,rH,1);
$BD:d_xReg(sCP,rL,1);
$BE:d_xArp(sCP,rHL,reg_w.HL,1);
$BF:d_xReg(sCP,rA,1);
$C0:d_RETif(ifNZ);
$C1:d_stckReg(sPop,rBC);
$C2:d_GoIF(sJP,ifNZ);
$C3:d_GoNN(sJP);
$C4:d_GoIF(sCALL,ifNZ);
$C5:d_stckReg(sPush,rBC);
$C6:d_xxRegN(sADD,rA);
$C7:d_RST($00);
$C8:d_RETif(ifZ);
$C9:d_str(sRET,1);
$CA:d_GoIF(sJP,ifZ);
$CB:begin{расширение команд}
    inc(_adr);
    case memory^[_adr]of
    $00:d_xReg(sRLC,rB,2);
    $01:d_xReg(sRLC,rC,2);
    $02:d_xReg(sRLC,rD,2);
    $03:d_xReg(sRLC,rE,2);
    $04:d_xReg(sRLC,rH,2);
    $05:d_xReg(sRLC,rL,2);
    $06:d_xArp(sRLC,rHL,reg_w.HL,2);
    $07:d_xReg(sRLC,rA,2);
    $08:d_xReg(sRRC,rB,2);
    $09:d_xReg(sRRC,rC,2);
    $0A:d_xReg(sRRC,rD,2);
    $0B:d_xReg(sRRC,rE,2);
    $0C:d_xReg(sRRC,rH,2);
    $0D:d_xReg(sRRC,rL,2);
    $0E:d_xArp(sRRC,rHL,reg_w.HL,2);
    $0F:d_xReg(sRRC,rA,2);
    $10:d_xReg(sRL,rB,2);
    $11:d_xReg(sRL,rC,2);
    $12:d_xReg(sRL,rD,2);
    $13:d_xReg(sRL,rE,2);
    $14:d_xReg(sRL,rH,2);
    $15:d_xReg(sRL,rL,2);
    $16:d_xArp(sRL,rHL,reg_w.HL,2);
    $17:d_xReg(sRL,rA,2);
    $18:d_xReg(sRR,rB,2);
    $19:d_xReg(sRR,rC,2);
    $1A:d_xReg(sRR,rD,2);
    $1B:d_xReg(sRR,rE,2);
    $1C:d_xReg(sRR,rH,2);
    $1D:d_xReg(sRR,rL,2);
    $1E:d_xArp(sRR,rHL,reg_w.HL,2);
    $1F:d_xReg(sRR,rA,2);
    $20:d_xReg(sSLA,rB,2);
    $21:d_xReg(sSLA,rC,2);
    $22:d_xReg(sSLA,rD,2);
    $23:d_xReg(sSLA,rE,2);
    $24:d_xReg(sSLA,rH,2);
    $25:d_xReg(sSLA,rL,2);
    $26:d_xArp(sSLA,rHL,reg_w.HL,2);
    $27:d_xReg(sSLA,rA,2);
    $28:d_xReg(sSRA,rB,2);
    $29:d_xReg(sSRA,rC,2);
    $2A:d_xReg(sSRA,rD,2);
    $2B:d_xReg(sSRA,rE,2);
    $2C:d_xReg(sSRA,rH,2);
    $2D:d_xReg(sSRA,rL,2);
    $2E:d_xArp(sSRA,rHL,reg_w.HL,2);
    $2F:d_xReg(sSRA,rA,2);
    $38:d_xReg(sSRL,rB,2);
    $39:d_xReg(sSRL,rC,2);
    $3A:d_xReg(sSRL,rD,2);
    $3B:d_xReg(sSRL,rE,2);
    $3C:d_xReg(sSRL,rH,2);
    $3D:d_xReg(sSRL,rL,2);
    $3E:d_xArp(sSRL,rHL,reg_w.HL,2);
    $3F:d_xReg(sSRL,rA,2);
    $40:d_xReg(sBIT+s0,rB,2);
    $41:d_xReg(sBIT+s0,rC,2);
    $42:d_xReg(sBIT+s0,rD,2);
    $43:d_xReg(sBIT+s0,rE,2);
    $44:d_xReg(sBIT+s0,rH,2);
    $45:d_xReg(sBIT+s0,rL,2);
    $46:d_xArp(sBIT+s0,rHL,reg_w.HL,2);
    $47:d_xReg(sBIT+s0,rA,2);
    $48:d_xReg(sBIT+s1,rB,2);
    $49:d_xReg(sBIT+s1,rC,2);
    $4A:d_xReg(sBIT+s1,rD,2);
    $4B:d_xReg(sBIT+s1,rE,2);
    $4C:d_xReg(sBIT+s1,rH,2);
    $4D:d_xReg(sBIT+s1,rL,2);
    $4E:d_xArp(sBIT+s1,rHL,reg_w.HL,2);
    $4F:d_xReg(sBIT+s1,rA,2);
    $50:d_xReg(sBIT+s2,rB,2);
    $51:d_xReg(sBIT+s2,rC,2);
    $52:d_xReg(sBIT+s2,rD,2);
    $53:d_xReg(sBIT+s2,rE,2);
    $54:d_xReg(sBIT+s2,rH,2);
    $55:d_xReg(sBIT+s2,rL,2);
    $56:d_xArp(sBIT+s2,rHL,reg_w.HL,2);
    $57:d_xReg(sBIT+s2,rA,2);
    $58:d_xReg(sBIT+s3,rB,2);
    $59:d_xReg(sBIT+s3,rC,2);
    $5A:d_xReg(sBIT+s3,rD,2);
    $5B:d_xReg(sBIT+s3,rE,2);
    $5C:d_xReg(sBIT+s3,rH,2);
    $5D:d_xReg(sBIT+s3,rL,2);
    $5E:d_xArp(sBIT+s3,rHL,reg_w.HL,2);
    $5F:d_xReg(sBIT+s3,rA,2);
    $60:d_xReg(sBIT+s4,rB,2);
    $61:d_xReg(sBIT+s4,rC,2);
    $62:d_xReg(sBIT+s4,rD,2);
    $63:d_xReg(sBIT+s4,rE,2);
    $64:d_xReg(sBIT+s4,rH,2);
    $65:d_xReg(sBIT+s4,rL,2);
    $66:d_xArp(sBIT+s4,rHL,reg_w.HL,2);
    $67:d_xReg(sBIT+s4,rA,2);
    $68:d_xReg(sBIT+s5,rB,2);
    $69:d_xReg(sBIT+s5,rC,2);
    $6A:d_xReg(sBIT+s5,rD,2);
    $6B:d_xReg(sBIT+s5,rE,2);
    $6C:d_xReg(sBIT+s5,rH,2);
    $6D:d_xReg(sBIT+s5,rL,2);
    $6E:d_xArp(sBIT+s5,rHL,reg_w.HL,2);
    $6F:d_xReg(sBIT+s5,rA,2);
    $70:d_xReg(sBIT+s6,rB,2);
    $71:d_xReg(sBIT+s6,rC,2);
    $72:d_xReg(sBIT+s6,rD,2);
    $73:d_xReg(sBIT+s6,rE,2);
    $74:d_xReg(sBIT+s6,rH,2);
    $75:d_xReg(sBIT+s6,rL,2);
    $76:d_xArp(sBIT+s6,rHL,reg_w.HL,2);
    $77:d_xReg(sBIT+s6,rA,2);
    $78:d_xReg(sBIT+s7,rB,2);
    $79:d_xReg(sBIT+s7,rC,2);
    $7A:d_xReg(sBIT+s7,rD,2);
    $7B:d_xReg(sBIT+s7,rE,2);
    $7C:d_xReg(sBIT+s7,rH,2);
    $7D:d_xReg(sBIT+s7,rL,2);
    $7E:d_xArp(sBIT+s7,rHL,reg_w.HL,2);
    $7F:d_xReg(sBIT+s7,rA,2);
    $80:d_xReg(sRES+s0,rB,2);
    $81:d_xReg(sRES+s0,rC,2);
    $82:d_xReg(sRES+s0,rD,2);
    $83:d_xReg(sRES+s0,rE,2);
    $84:d_xReg(sRES+s0,rH,2);
    $85:d_xReg(sRES+s0,rL,2);
    $86:d_xArp(sRES+s0,rHL,reg_w.HL,2);
    $87:d_xReg(sRES+s0,rA,2);
    $88:d_xReg(sRES+s1,rB,2);
    $89:d_xReg(sRES+s1,rC,2);
    $8A:d_xReg(sRES+s1,rD,2);
    $8B:d_xReg(sRES+s1,rE,2);
    $8C:d_xReg(sRES+s1,rH,2);
    $8D:d_xReg(sRES+s1,rL,2);
    $8E:d_xArp(sRES+s1,rHL,reg_w.HL,2);
    $8F:d_xReg(sRES+s1,rA,2);
    $90:d_xReg(sRES+s2,rB,2);
    $91:d_xReg(sRES+s2,rC,2);
    $92:d_xReg(sRES+s2,rD,2);
    $93:d_xReg(sRES+s2,rE,2);
    $94:d_xReg(sRES+s2,rH,2);
    $95:d_xReg(sRES+s2,rL,2);
    $96:d_xArp(sRES+s2,rHL,reg_w.HL,2);
    $97:d_xReg(sRES+s2,rA,2);
    $98:d_xReg(sRES+s3,rB,2);
    $99:d_xReg(sRES+s3,rC,2);
    $9A:d_xReg(sRES+s3,rD,2);
    $9B:d_xReg(sRES+s3,rE,2);
    $9C:d_xReg(sRES+s3,rH,2);
    $9D:d_xReg(sRES+s3,rL,2);
    $9E:d_xArp(sRES+s3,rHL,reg_w.HL,2);
    $9F:d_xReg(sRES+s3,rA,2);
    $A0:d_xReg(sRES+s4,rB,2);
    $A1:d_xReg(sRES+s4,rC,2);
    $A2:d_xReg(sRES+s4,rD,2);
    $A3:d_xReg(sRES+s4,rE,2);
    $A4:d_xReg(sRES+s4,rH,2);
    $A5:d_xReg(sRES+s4,rL,2);
    $A6:d_xArp(sRES+s4,rHL,reg_w.HL,2);
    $A7:d_xReg(sRES+s4,rA,2);
    $A8:d_xReg(sRES+s5,rB,2);
    $A9:d_xReg(sRES+s5,rC,2);
    $AA:d_xReg(sRES+s5,rD,2);
    $AB:d_xReg(sRES+s5,rE,2);
    $AC:d_xReg(sRES+s5,rH,2);
    $AD:d_xReg(sRES+s5,rL,2);
    $AE:d_xArp(sRES+s5,rHL,reg_w.HL,2);
    $AF:d_xReg(sRES+s5,rA,2);
    $B0:d_xReg(sRES+s6,rB,2);
    $B1:d_xReg(sRES+s6,rC,2);
    $B2:d_xReg(sRES+s6,rD,2);
    $B3:d_xReg(sRES+s6,rE,2);
    $B4:d_xReg(sRES+s6,rH,2);
    $B5:d_xReg(sRES+s6,rL,2);
    $B6:d_xArp(sRES+s6,rHL,reg_w.HL,2);
    $B7:d_xReg(sRES+s6,rA,2);
    $B8:d_xReg(sRES+s7,rB,2);
    $B9:d_xReg(sRES+s7,rC,2);
    $BA:d_xReg(sRES+s7,rD,2);
    $BB:d_xReg(sRES+s7,rE,2);
    $BC:d_xReg(sRES+s7,rH,2);
    $BD:d_xReg(sRES+s7,rL,2);
    $BE:d_xArp(sRES+s7,rHL,reg_w.HL,2);
    $BF:d_xReg(sRES+s7,rA,2);
    $C0:d_xReg(sSET+s0,rB,2);
    $C1:d_xReg(sSET+s0,rC,2);
    $C2:d_xReg(sSET+s0,rD,2);
    $C3:d_xReg(sSET+s0,rE,2);
    $C4:d_xReg(sSET+s0,rH,2);
    $C5:d_xReg(sSET+s0,rL,2);
    $C6:d_xArp(sSET+s0,rHL,reg_w.HL,2);
    $C7:d_xReg(sSET+s0,rA,2);
    $C8:d_xReg(sSET+s1,rB,2);
    $C9:d_xReg(sSET+s1,rC,2);
    $CA:d_xReg(sSET+s1,rD,2);
    $CB:d_xReg(sSET+s1,rE,2);
    $CC:d_xReg(sSET+s1,rH,2);
    $CD:d_xReg(sSET+s1,rL,2);
    $CE:d_xArp(sSET+s1,rHL,reg_w.HL,2);
    $CF:d_xReg(sSET+s1,rA,2);
    $D0:d_xReg(sSET+s2,rB,2);
    $D1:d_xReg(sSET+s2,rC,2);
    $D2:d_xReg(sSET+s2,rD,2);
    $D3:d_xReg(sSET+s2,rE,2);
    $D4:d_xReg(sSET+s2,rH,2);
    $D5:d_xReg(sSET+s2,rL,2);
    $D6:d_xArp(sSET+s2,rHL,reg_w.HL,2);
    $D7:d_xReg(sSET+s2,rA,2);
    $D8:d_xReg(sSET+s3,rB,2);
    $D9:d_xReg(sSET+s3,rC,2);
    $DA:d_xReg(sSET+s3,rD,2);
    $DB:d_xReg(sSET+s3,rE,2);
    $DC:d_xReg(sSET+s3,rH,2);
    $DD:d_xReg(sSET+s3,rL,2);
    $DE:d_xArp(sSET+s3,rHL,reg_w.HL,2);
    $DF:d_xReg(sSET+s3,rA,2);
    $E0:d_xReg(sSET+s4,rB,2);
    $E1:d_xReg(sSET+s4,rC,2);
    $E2:d_xReg(sSET+s4,rD,2);
    $E3:d_xReg(sSET+s4,rE,2);
    $E4:d_xReg(sSET+s4,rH,2);
    $E5:d_xReg(sSET+s4,rL,2);
    $E6:d_xArp(sSET+s4,rHL,reg_w.HL,2);
    $E7:d_xReg(sSET+s4,rA,2);
    $E8:d_xReg(sSET+s5,rB,2);
    $E9:d_xReg(sSET+s5,rC,2);
    $EA:d_xReg(sSET+s5,rD,2);
    $EB:d_xReg(sSET+s5,rE,2);
    $EC:d_xReg(sSET+s5,rH,2);
    $ED:d_xReg(sSET+s5,rL,2);
    $EE:d_xArp(sSET+s5,rHL,reg_w.HL,2);
    $EF:d_xReg(sSET+s5,rA,2);
    $F0:d_xReg(sSET+s6,rB,2);
    $F1:d_xReg(sSET+s6,rC,2);
    $F2:d_xReg(sSET+s6,rD,2);
    $F3:d_xReg(sSET+s6,rE,2);
    $F4:d_xReg(sSET+s6,rH,2);
    $F5:d_xReg(sSET+s6,rL,2);
    $F6:d_xArp(sSET+s6,rHL,reg_w.HL,2);
    $F7:d_xReg(sSET+s6,rA,2);
    $F8:d_xReg(sSET+s7,rB,2);
    $F9:d_xReg(sSET+s7,rC,2);
    $FA:d_xReg(sSET+s7,rD,2);
    $FB:d_xReg(sSET+s7,rE,2);
    $FC:d_xReg(sSET+s7,rH,2);
    $FD:d_xReg(sSET+s7,rL,2);
    $FE:d_xArp(sSET+s7,rHL,reg_w.HL,2);
    $FF:d_xReg(sSET+s7,rA,2);
    else begin _size:=2;_err_code:=true;end;
    end;
    end;
$CC:d_GoIF(sCALL,ifZ);
$CD:d_GoNN(sCALL);
$CE:d_xxRegN(sADC,rA);
$CF:d_RST($08);
$D0:d_RETif(ifNC);
$D1:d_stckReg(sPop,rDE);
$D2:d_GoIF(sJP,ifNC);
$D3:d_OUTRegAN;
$D4:d_GoIF(sCALL,ifNC);
$D5:d_stckReg(sPush,rDE);
$D6:d_xN(sSUB);
$D7:d_RST($10);
$D8:d_RETif(ifC);
$D9:d_str(sEXX,1);
$DA:d_GoIF(sJP,ifC);
$DB:d_INRegAN;
$DC:d_GoIF(sCALL,ifC);
$DD,$FD:begin{операции с IX/IY}
    if memory^[_adr]=$DD then
     begin w_reg:=sp_reg.IX;c_reg:=rIX;end else
     begin w_reg:=sp_reg.IY;c_reg:=rIY;end;
    inc(_adr);
    case memory^[_adr]of
   $09:d_xxRegIR(sADD,c_reg,rBC);
   $19:d_xxRegIR(sADD,c_reg,rDE);
   $21:d_LDIdxNN(c_reg);
   $22:d_LDIdxRp(c_reg);
   $23:d_xReg(sINC,c_reg,2);
   $29:d_xxRegIR(sADD,c_reg,c_reg);
   $2A:d_LDRpIdx(c_reg);
   $2B:d_xReg(sDEC,c_reg,2);
   $34:d_xIdx(sINC,w_reg,3);
   $35:d_xIdx(sDEC,w_reg,3);
   $36:d_LDIdxN(w_reg);
   $39:d_xxRegIR(sADD,c_reg,rSP);
   $46:d_xxRegIdx(sLD,rB,w_reg);
   $4E:d_xxRegIdx(sLD,rC,w_reg);
   $56:d_xxRegIdx(sLD,rD,w_reg);
   $5E:d_xxRegIdx(sLD,rE,w_reg);
   $66:d_xxRegIdx(sLD,rH,w_reg);
   $6E:d_xxRegIdx(sLD,rL,w_reg);
   $70:d_LDIdxReg(rB,w_reg);
   $71:d_LDIdxReg(rC,w_reg);
   $72:d_LDIdxReg(rD,w_reg);
   $73:d_LDIdxReg(rE,w_reg);
   $74:d_LDIdxReg(rH,w_reg);
   $75:d_LDIdxReg(rL,w_reg);
   $77:d_LDIdxReg(rA,w_reg);
   $7E:d_xxRegIdx(sLD,rA,w_reg);
   $86:d_xxRegIdx(sADD,rA,w_reg);
   $8E:d_xxRegIdx(sADC,rA,w_reg);
   $96:d_xIdx(sSUB,w_reg,3);
   $9E:d_xxRegIdx(sSBC,rA,w_reg);
   $A6:d_xIdx(sAND,w_reg,3);
   $AE:d_xIdx(sXOR,w_reg,3);
   $B6:d_xIdx(sOR,w_reg,3);
   $BE:d_xIdx(sCP,w_reg,3);
   $CB:begin
       case memory^[_adr+2]of
       $06:d_xIdx(sRLC,w_reg,4);
       $0E:d_xIdx(sRRC,w_reg,4);
       $16:d_xIdx(sRL,w_reg,4);
       $1E:d_xIdx(sRR,w_reg,4);
       $26:d_xIdx(sSLA,w_reg,4);
       $2E:d_xIdx(sSRA,w_reg,4);
       $3E:d_xIdx(sSRL,w_reg,4);
       $46:d_xIdx(sBIT+s0,w_reg,4);
       $4E:d_xIdx(sBIT+s1,w_reg,4);
       $56:d_xIdx(sBIT+s2,w_reg,4);
       $5E:d_xIdx(sBIT+s3,w_reg,4);
       $66:d_xIdx(sBIT+s4,w_reg,4);
       $6E:d_xIdx(sBIT+s5,w_reg,4);
       $76:d_xIdx(sBIT+s6,w_reg,4);
       $7E:d_xIdx(sBIT+s7,w_reg,4);
       $86:d_xIdx(sRES+s0,w_reg,4);
       $8E:d_xIdx(sRES+s1,w_reg,4);
       $96:d_xIdx(sRES+s2,w_reg,4);
       $9E:d_xIdx(sRES+s3,w_reg,4);
       $A6:d_xIdx(sRES+s4,w_reg,4);
       $AE:d_xIdx(sRES+s5,w_reg,4);
       $B6:d_xIdx(sRES+s6,w_reg,4);
       $BE:d_xIdx(sRES+s7,w_reg,4);
       $C6:d_xIdx(sSET+s0,w_reg,4);
       $CE:d_xIdx(sSET+s1,w_reg,4);
       $D6:d_xIdx(sSET+s2,w_reg,4);
       $DE:d_xIdx(sSET+s3,w_reg,4);
       $E6:d_xIdx(sSET+s4,w_reg,4);
       $EE:d_xIdx(sSET+s5,w_reg,4);
       $F6:d_xIdx(sSET+s6,w_reg,4);
       $FE:d_xIdx(sSET+s7,w_reg,4);
       end;
       end;
   $E1:d_stckIdx(sPop,c_reg);
   $E3:d_EXSpIx(c_reg);
   $E5:d_stckIdx(sPush,c_reg);
   $E9:d_JpRp(c_reg);
   $F9:d_xxRegIR(sLD,rSP,c_reg);
    else begin _size:=2;_err_code:=true;end;
    end;
    end;
$DE:d_xxRegN(sSBC,rA);
$DF:d_RST($18);
$E0:d_RETif(ifPO);
$E1:d_stckReg(sPop,rHL);
$E2:d_GoIF(sJP,ifPO);
$E3:d_EXSpReg(rHL);
$E4:d_GoIF(sCALL,ifPO);
$E5:d_stckReg(sPush,rHL);
$E6:d_xN(sAND);
$E7:d_RST($20);
$E8:d_RETif(ifPE);
$E9:d_JpRp(rHL);
$EA:d_GoIF(sJP,ifPE);
$EB:d_EXRpRp(rDE,rHL,'');
$EC:d_GoIF(sCALL,ifPE);
$ED:begin{расширение команд}
    inc(_adr);
    case memory^[_adr]of
   $40:d_IN(rB);
   $41:d_OUT(rB);
   $42:d_xxRegIR(sSBC,rHL,rBC);
   $43:d_LDIdxRp(rBC);
   $44:d_str(sNEG,2);
   $45:d_str(sRET+'N',2);
   $46:d_str(sIM+'0',2);
   $47:d_xxRegIR(sLD,rI,rA);
   $48:d_IN(rC);
   $49:d_OUT(rC);
   $4A:d_xxRegIR(sADC,rHL,rBC);
   $4B:d_LDRpIdx(rBC);
   $4D:d_str(sRET+'I',2);
   $4F:d_xxRegIR(sLD,rR,rA);
   $50:d_IN(rD);
   $51:d_OUT(rD);
   $52:d_xxRegIR(sSBC,rHL,rDE);
   $53:d_LDIdxRp(rDE);
   $56:d_str(sIM+'1',2);
   $57:d_xxRegIR(sLD,rA,rI);
   $58:d_IN(rE);
   $59:d_OUT(rE);
   $5A:d_xxRegIR(sADC,rHL,rDE);
   $5B:d_LDRpIdx(rDE);
   $5E:d_str(sIM+'2',2);
   $5F:d_xxRegIR(sLD,rA,rR);
   $60:d_IN(rH);
   $61:d_OUT(rH);
   $62:d_xxRegIR(sSBC,rHL,rHL);
   $63:d_LDIdxRp(rHL);
   $67:d_str(sRRD,2);
   $68:d_IN(rL);
   $69:d_OUT(rL);
   $6A:d_xxRegIR(sADC,rHL,rHL);
   $6B:d_LDRpIdx(rHL);
   $6F:d_str(sRLD,2);
   $70:d_IN(rF);
   $72:d_xxRegIR(sSBC,rHL,rSP);
   $73:d_LDIdxRp(rSP);
   $78:d_IN(rA);
   $79:d_OUT(rA);
   $7A:d_xxRegIR(sADC,rHL,rSP);
   $7B:d_LDRpIdx(rSP);
   $A0:d_str(sLDI,2);
   $A1:d_str(sCPI,2);
   $A2:d_str(sINI,2);
   $A3:d_str(sOUTI,2);
   $A8:d_str(sLDD,2);
   $A9:d_str(sCPD,2);
   $AA:d_str(sIND,2);
   $AB:d_str(sOUTD,2);
   $B0:d_str(sLDIR,2);
   $B1:d_str(sCPIR,2);
   $B2:d_str(sINIR,2);
   $B3:d_str(sOTIR,2);
   $B8:d_str(sLDDR,2);
   $B9:d_str(sCPDR,2);
   $BA:d_str(sINDR,2);
   $BB:d_str(sOTDR,2);
    else begin _size:=2;_err_code:=true;end;
    end;
    end;
$EE:d_xN(sXOR);
$EF:d_RST($28);
$F0:d_RETif(ifP);
$F1:d_stckReg(sPop,rAF);
$F2:d_GoIF(sJP,ifP);
$F3:d_str(sDI,1);
$F4:d_GoIF(sCALL,ifP);
$F5:d_stckReg(sPush,rAF);
$F6:d_xN(sOR);
$F7:d_RST($30);
$F8:d_RETif(ifM);
$FA:d_GoIF(sJP,ifM);
$FB:d_str(sEI,1);
$FC:d_GoIF(sCALL,ifM);
$FE:d_xN(sCP);
$F9:d_xxRegReg(sLD,rSP,rHL);
$FF:d_RST($38);
else begin _size:=1;_err_code:=true;end;
end;
if _err_code then
case _size of
1:begin CodeToAsm:='DB '+s_mem_b(_adr);end;
2:begin CodeToAsm:='DW '+s_mem_w(_adr-1);end;
end;
sp_str:=_s;
end;
(****************************************************************************)
(****************************************************************************)
procedure windowAsm;
begin
window(2,2,33,25);
end;
procedure windowSpec;
begin
window(17,1,30,1);
clreol;
end;
procedure writeAsm(__curent_addr:word;var __size_command:byte);
var _s,_spec_str:string;
_b:byte;
old_where_Y:byte;
begin
if sp_reg.PC=__curent_addr then write('*')else write(' ');
WriteWord(__curent_addr);
gotoxy(8,whereY);
_s:=CodeToAsm(__curent_addr,__size_command,_spec_str);
for _b:=0 to __size_command-1 do write(whb(memory^[__curent_addr+_b]));
if(sp_reg.PC=__curent_addr)and(_spec_str<>'')then
 begin
old_where_Y:=whereY;
windowSpec;
write(_spec_str);
windowAsm;
gotoxy(17,old_where_Y);
 end;
gotoxy(17,whereY);
writeln(_s);
end;
(****************************************************************************)
procedure UnAsm(addr_unAsm:word);
var _size_command,_b:byte;
_curent_addr:word;
begin
currentPC:=addr_unAsm;
_curent_addr:=addr_unAsm;
windowSpec;
windowAsm;
for _b:=0 to 22 do
begin
clrEol;
writeAsm(_curent_addr,_size_command);
inc(_curent_addr,_size_command);
end;
maxcurrentPC:=_curent_addr;
end;
(****************************************************************************)
procedure ShowRomMode;
begin
window(57,6,64,7);
textcolor(7);
write('ROM: ');
if TheDosMode then
begin
textcolor(14);
write('Dos')
end else
begin
textcolor(15);
write('Sos');
end;
end;
(****************************************************************************)
procedure ShowInt;
begin
window(57,5,64,6);
if MaskInt then
begin
textcolor(12);
write('Disable')
end else
begin
textcolor(10);
writeln('Enable ');
end;
textcolor(7);
window(60,4,61,4);
write(IM);
window(62,3,67,4);
WritelnWord(IntAddr);
window(62,2,67,2);
Write(Clock);
end;
(****************************************************************************)
procedure ShowReg;
begin
textcolor(7);
window(38,2,43,6);
WritelnWord(reg_w.FA);
WritelnWord(reg_w.BC);
WritelnWord(reg_w.DE);
WritelnWord(reg_w.HL);
window(49,2,54,6);
WritelnWord(reg__w.FA);
WritelnWord(reg__w.BC);
WritelnWord(reg__w.DE);
WritelnWord(reg__w.HL);
window(38,8,43,13);
WritelnWord(sp_reg.IX);
WritelnWord(sp_reg.IY);
WritelnWord(sp_reg.PC);
WritelnWord(sp_reg.SP);
WritelnWord(wrd(sp_reg.I,sp_reg.R));
window(41,6,49,6);
write(writeF(reg.F));
end;
(****************************************************************************)
procedure ShowInfo;
begin
UnAsm(currentPC);
ShowReg;
ShowRomMode;
ShowInt;
end;
(****************************************************************************)
procedure ShowDebuger;
begin
textbackground(0);
textcolor(7);
window(1,1,80,25);
clrscr;
writeln('╔═ Code: ══════╡              ╞══╤ CPU: ══════════════╤═ IntDat: ═══╕Help:     ');
writeln('║                                │AF=       AF`=      │ Time=       │N run     ');
writeln('║                                │BC=       BC`=      │ Addr=       │T trace   ');
writeln('║                                │DE=       DE`=      │ IM=         │Q quit    ');
writeln('║                                │HL=       HL`=      │             │G goto    ');
writeln('║                                │    F=              │             │e enter   ');
writeln('║                                ├ SpReg: ─┬─ Dump: ──┴─────────────┤R register');
writeln('║                                │IX=      │0000:0000000000000000:00│d memory  ');
writeln('║                                │IY=      │0008:1111111111111111:88│a asm     ');
writeln('║                                │PC=      │                        │U unasm   ');
writeln('║                                │SP=      │                        │V view    ');
writeln('║                                │IR=      │                        │F hex/dec ');
writeln('║                                │         │                        │c chr/byte');
writeln('║                                ├ Stack: ─┤                        │i in      ');
writeln('║                                │$FFFE:   │                        │o out     ');
writeln('║                                │  $0000  │                        │s stack   ');
writeln('║                                │  $0000  │                        │w write   ');
writeln('║                                │  $0000  │                        │l load    ');
writeln('║                                │  $0000  │                        │ENTER step');
writeln('║                                │  $0000  │                        │<>d/i PC  ');
writeln('║                                │  $0000  │                        │          ');
writeln('║                                │  $0000  │                        │          ');
writeln('║                                ├ Command:┤                        │ Z80 CPU  ');
writeln('║                                │>        │                        │ > S&D <  ');
write  ('╚════════════════════════════════╧═════════╧════════════════════════╛ (C)2000  ');
ShowInfo;
end;
(****************************************************************************)
var oldPosEditFlag,oldPosEditReg:byte;
procedure changeRegistr(_regS:pstring);
type tKoord=array[0..1]of byte;
const koordReg:array[0..12]of tKoord=
((38,2),(38,3),(38,4),(38,5),(49,2),(49,3),(49,4),(49,5),(38,8),(38,9),(38,10),(38,11),(38,12));
var _CharPos,_RegPos:byte;
_Changing:word;
_key:char;
tmp_reg,tmp_reg_:t_reg_w;
tmp_sp_reg:t_sp_reg;
tmp_flag:byte;
function getChar(_x,_y:byte):char;
 var _w:word;
 begin _w:=((_y-1)*80+(_x-1))*2;getChar:=chr(mem[$B800:_w]);end;
function getNumber(__x,__y,__razryad:byte):word;
var __Ch:word;
_w:word;
_c:char;
_i:integer;
 procedure __nextStep;
 begin
 _c:=getChar(__x+_i,__y);
 MyVal(_c,_w);
 inc(__Ch,_w);
 inc(_i);
 end;
begin
__Ch:=0;
_i:=0;
__nextStep;
while getChar(__x+_i,__y)<>' 'do
begin
__Ch:=__Ch*__razryad;
__nextStep;
end;
getNumber:=__Ch;
end;
procedure _nextCharPos(_maxPos:byte);
  begin
  if _maxPos>0 then begin if _CharPos<_maxPos then inc(_CharPos)end else
   begin
    if (HexMode)and(_CharPos<3) then inc(_CharPos)else
    if not(HexMode)and(_CharPos<4) then inc(_CharPos);
   end;
  gotoxy(_CharPos+1,1);
  end;
procedure _prewCharPos;
  begin
   if _CharPos>0 then dec(_CharPos);
  gotoxy(_CharPos+1,1);
  end;
procedure editFlags(_numberFlag:byte);
var _c0,_c1:char;
begin
window(41,6,49,7);
_CharPos:=_numberFlag;
GotoXY(_CharPos+1,1);
tmp_flag:=reg.F;
repeat
_key:=GetKey;
case _key of
' ':begin
  case 7-_CharPos of
  0{c}:begin _c0:='c';_c1:='C' end;
  1{n}:begin _c0:='n';_c1:='N' end;
  2{v}:begin _c0:='v';_c1:='V' end;
  3,5{0}:begin _c0:='0';_c1:='1' end;
  4{h}:begin _c0:='h';_c1:='H' end;
  6{z}:begin _c0:='z';_c1:='Z' end;
  7{s}:begin _c0:='s';_c1:='S' end;
  end;
  if setsBit(7-_CharPos,tmp_flag)then
   begin
    write(_c0);
    resBit(7-_CharPos,tmp_flag);
   end else
   begin
    write(_c1);
    setBit(7-_CharPos,tmp_flag);
   end;
    GotoXY(_CharPos+1,1);
    end;
#75{left}:_prewCharPos;
#77{right}:_nextCharPos(7);
end;
until (_key=#$0D)or(_key=#$1B);
if _key=#$0D{enter} then reg.F:=tmp_flag;
oldPosEditFlag:=_CharPos;
ShowInfo;
end;

procedure editReg(_numberReg:byte);
 procedure _GoToWins;
  begin
   if HexMode then
    window(koordReg[_RegPos,0]+1,koordReg[_RegPos,1],koordReg[_RegPos,0]+4,koordReg[_RegPos,1]+1)
   else
    window(koordReg[_RegPos,0],koordReg[_RegPos,1],koordReg[_RegPos,0]+4,koordReg[_RegPos,1]+1);
  end;
begin
_RegPos:=_numberReg;
tmp_reg:= reg_w;
tmp_reg_:=reg__w;
tmp_sp_reg:=sp_reg;

repeat
_CharPos:=0;
_GoToWins;
repeat
_key:=GetKey;
case _key of
'0'..'9','a'..'f','A'..'F':
 begin
  chrUp(_key);
  gotoxy(_CharPos+1,1);
  write(_key);
  _nextCharPos(0);
 end;
' ',#8:if not(HexMode) then
 begin
  if _key=' 'then
   begin gotoxy(_CharPos+1,1);write(' ');_nextCharPos(0) end else
   begin gotoxy(_CharPos,1);write(' ');  _prewCharPos end;
 end;
#75{left}:_prewCharPos;
#77{right}:_nextCharPos(0);
end;
until (_key=#$0D)or(_key=#$1B)or(_key=#72)or(_key=#80);

if HexMode then
_Changing:=GetNumber(koordReg[_RegPos,0]+1,koordReg[_RegPos,1],$10)
 else
_Changing:=GetNumber(koordReg[_RegPos,0],koordReg[_RegPos,1],10);

case _RegPos of
0:tmp_reg.FA:=_Changing;
1:tmp_reg.BC:=_Changing;
2:tmp_reg.DE:=_Changing;
3:tmp_reg.HL:=_Changing;
4:tmp_reg_.FA:=_Changing;
5:tmp_reg_.BC:=_Changing;
6:tmp_reg_.DE:=_Changing;
7:tmp_reg_.HL:=_Changing;
8:tmp_sp_reg.IX:=_Changing;
9:tmp_sp_reg.IY:=_Changing;
10:tmp_sp_reg.PC:=_Changing;
11:tmp_sp_reg.SP:=_Changing;
12:begin tmp_sp_reg.I:=hi(_Changing);tmp_sp_reg.R:=lo(_Changing);end;
end;
case _key of
#72{up}:if _RegPos>0 then dec(_RegPos);
#80{down}:if _RegPos<12 then inc(_RegPos);
end;
until (_key=#$0D)or(_key=#$1B);
if _key=#$0D{enter}then
 begin
  reg_w:=tmp_reg;
  reg__w:=tmp_reg_;
  sp_reg:=tmp_sp_reg;
 end;
oldPosEditReg:=_RegPos;
ShowInfo;
end;

begin
if (_regS^='AF')or(_regS^='A') then editReg(0) else
if (_regS^='BC')or(_regS^='B') then editReg(1) else
if (_regS^='DE')or(_regS^='D') then editReg(2) else
if (_regS^='HL')or(_regS^='H') then editReg(3) else
if (_regS^='AF`')or(_regS^='A`') then editReg(4) else
if (_regS^='BC`')or(_regS^='B`') then editReg(5) else
if (_regS^='DE`')or(_regS^='D`') then editReg(6) else
if (_regS^='HL`')or(_regS^='H`') then editReg(7) else
if _regS^='IX' then editReg(8) else
if _regS^='IY' then editReg(9) else
if _regS^='PC' then editReg(10) else
if _regS^='SP' then editReg(11) else
if _regS^='IR' then editReg(12) else
if _regS^='F' then editFlags(oldPosEditFlag)else
editReg(oldPosEditReg);
end;
(****************************************************************************)
procedure GetCommand(var _command:byte;var _operand:word;var _operand_b:byte;_operand_s:pstring);
var _s_command:string;
begin
window(36,24,43,24);
clrscr;
readln(_s_command);
if _s_command='' then _s_command:=' ';
_operand:=reStr(_s_command,_operand_s);
_operand_b:=reStrB(_s_command);
case _s_command[1]of
'q','Q':_command:=0;
'g','G':_command:=1;
'n','N':_command:=2;
'v','V':_command:=3;
'f','F':_command:=4;
'u','U':_command:=5;
't','T':_command:=6;
' ':_command:=7;
'>':_command:=8;
'<':_command:=9;
'r','R':_command:=10;
else _command:=$FF;
end;
end;
(****************************************************************************)
procedure TextMode;
begin
DebugMode:=true;
{EmulScreen^:=RealScreen;}
closegraph;
ShowDebuger;
end;
(****************************************************************************)
procedure GraphMode;
begin
DebugMode:=false;
set_screen;
RealScreen:=EmulScreen^;
end;
(****************************************************************************)
procedure InitEmulScreen;
var _i:word;
begin
new(EmulScreen);
for _i:=0 to sizeOf(EmulScreen^) do
 EmulScreen^[_i]:=255;
end;
(****************************************************************************)
(****************************************************************************)
(****************************************************************************)
type tBuff=array[0..$FFFE]of byte;
procedure SetCRC;
var f:file;
zz,CRC:longint;
w,i:word;
buff:^tBuff;
begin
assign(f,paramstr(0));
reset(f,1);
new(buff);
blockRead(f,buff^[0],fileSize(f),w);
CRC:=0;
for i:=0 to w-1 do
begin
zz:=buff^[i];
CRC:=CRC+zz;
end;
blockWrite(f,CRC,sizeOf(CRC));
dispose(buff);
close(f);
writeln('add CRC Ok');
halt;
end;
(****************************************************************************)
procedure CheckCRC;
var f:file;
zz,CRC,CRC_:longint;
w,i:word;
buff:^tBuff;
begin
assign(f,paramstr(0));
reset(f,1);
new(buff);
blockRead(f,buff^[0],fileSize(f)-sizeOf(CRC),w);
CRC:=0;
for i:=0 to w-1 do
begin
zz:=buff^[i];
CRC:=CRC+zz;
end;
dispose(buff);
blockRead(f,CRC_,sizeOf(CRC_));
close(f);
if CRC<>CRC_ then
 begin
 writeln;
 writeln('ERROR: программный файл возможно поврежден! (CRC error)');
 halt;
 end;
end;
(****************************************************************************)
(****************************************************************************)
(****************************************************************************)
var
CommandSize,operandB,command:byte;
operand:word;
operandS,spec_string:string;
f:file;
begin
if paramstr(1)='SetCRC' then SetCRC;
checkCRC;
clrscr;
writeln('                 !! эмулятор Спектрума !! (Demo Ver1.05)');
writeln;writeln;
writeln('  автор программы: Соломатин Денис Владимирович');
writeln('                            e-mail: denis_2001j@bk.ru');
writeln('  единственный распространитель: Хомяков Иван Валерьевич');
writeln('                            e-mail: ogs_webmaster@mail.ru');
writeln('                               www: skyslayer.narod.ru');
writeln;
writeln('содержание:');
writeln('debugz80.exe 52,399 07-01-04 23:03 - собственно эмулятор Z80');
writeln('dos.rom      16,384 23-11-00 11:21 - теневое ПЗУ с TR-DOS');
writeln('sos.rom      16,384 21-09-00 16:39 - основное ПЗУ с BASICом');
writeln('help.dat     64,000 18-10-00 15:54 - картинка с раскладкой клавиатуры');
writeln('svga256.bgi   3,660  9-02-91  1:30 - до кучи');
writeln('disk0000.tmp 1,409,024             - дисковвод <А>');
writeln('*.zx  - образы спектрумовских дискет');
writeln('make_drv.exe  4,112 07-01-04 23:01 - вставлялка дискет в дисковвод');
writeln;
writeln('те кто являлся счастливым обладателем SPECCY с дисковводом');
writeln(' могут не читать следующие две строки');
writeln('RETURN - переход в SOS');
writeln('RANDOMIZE USR 15616 - переход в DOS');
writeln;
write('               нажмите любую клавишу для продолжения');
while keypressed do readkey;
readkey;
writeln('программа начинает работу в режиме отладчика Z80');
writeln(' описание экрана:');
writeln('|правый нижний угол - логотип');
writeln('|Code - содержимое псевдо-памяти <адрес> <машинный код> <асм>');
writeln(' * отмечает текущее положение PC');
writeln('|CPU - содержимое регистров Z80');
writeln('|SpReg - содержимое спец. регистров');
writeln('|IntDat - информация о состоянии прерываний');
writeln('|ROM - состояние теневого ПЗУ');
writeln('|Command - поле ввода команд отладчика');
writeln(' описание команд:($41<chr(используемые команды)<$5A)');
writeln('n - пуск программы без остановки начиная с PC');
writeln('t - пуск программы до следующей команды (исполнение циклов)');
writeln('q - выход');
writeln('g - например gFFFF исполняет программу до FFFF, grax - до адреса из АХ');
writeln('(адреса вводятся в соответствии с текущей сиситемой счисления!)');
writeln('r - редактирование регистров, rrax- изменения в AX');
writeln('u - дизассемблирование (адрес указывается как в g)');
writeln('v - просмотр экрана ZX');
writeln('f - изменение системы счисления');
writeln('ENTER - исполнение текущей инструкции');
writeln('> - увеличение PC на еденицу            < - уменьшение PC на еденицу');
writeln(' системные клавиши при работе псевдо-спектрума');
writeln('ECS - возврат в режим отладки F1 - раскладка любимой клавиатуры');
write('                 нажмите любую клавишу для продолжения');
while keypressed do readkey;
readkey;

{assign(f,'debug.tmp');
rewrite(f);
close(f);}

InitDrivers;

TraceMode:=false;
HexMode:=true;
HXs:=HX;
oldPosEditReg:=0;
InitEmulScreen;
GraphMode;
Init;
currentPC:=sp_reg.PC;
TextMode;
repeat
GetCommand(command,operand,operandB,@operandS);
case command of
1:{goto NN}
begin
GraphMode;
RunMode:=false;
TraceTo(operand);
TextMode;
end;
2:{run}
begin
GraphMode;
RunMode:=true;
TraceTo(0);
RunMode:=false;
TextMode;
end;
3:{view screen}
begin
GraphMode;
repeat until pressExit;
pressExit:=false;
TextMode;
end;
4:{hex/dec}
begin
if HexMode then HexMode:=false else HexMode:=true;
ShowInfo;
end;
5:{UnAsm}if operandB=0 then UnAsm(maxcurrentPC)else UnAsm(operand);
6:{trace}
begin
HideCursor;
spec_string:=CodeToAsm(sp_reg.PC,CommandSize,spec_string);
TraceMode:=true;
DebugMode:=false;
TraceTo(sp_reg.PC+CommandSize);
DebugMode:=true;
TraceMode:=false;
ShowInfo;
ShowCursor;
end;
7:{step by step}
begin
HideCursor;
spec_string:=CodeToAsm(sp_reg.PC,CommandSize,spec_string);
PressExit:=true;
TraceMode:=true;
DebugMode:=false;
TraceTo(sp_reg.PC+CommandSize);
DebugMode:=true;
TraceMode:=false;
ShowInfo;
ShowCursor;
end;
8:{inc PC}begin inc(sp_reg.PC);ShowInfo;end;
9:{dec PC}begin dec(sp_reg.PC);ShowInfo;end;
10:{change registr}begin changeRegistr(@operandS);end;
end;
until command=0;{quit}

GraphMode;
Done;
DoneDrivers;
dispose(EmulScreen);
writeln('Bye ...');
end.