unit spectrum;{эмулятор Z80}
interface
uses Dos,Crt,Graph,IS18WG93,specproc;
{|}var tics:longint;tics_w:word absolute tics;
procedure CloseGraph;
procedure TraceTo(addr_trace:word);
procedure Init;
procedure Done;
procedure set_screen;
function writeF(b:byte):string;
function IntAddr:word;
function reIndex(__index:word;__s:byte):word;
function mem_w(_addr:word):word;
function mem_b(_addr:word):byte;
type t_screen=array[0..$F9FF]of byte;
var EmulScreen:^t_screen;
RealScreen:t_screen absolute $A000:$0000;
var TraceMode,RunMode,DebugMode:boolean;
const xGak=30;yGak=4;
type
t_rom=array[0..$3FFF]of byte;
t_memory=array[0..$FFFE]of byte;
t_reg=record F,A,C,B,E,D,L,H:byte end;
t_reg_w=record FA,BC,DE,HL:word end;
t_sp_reg=record I,R:byte;IX,IY,SP,PC:word;end;
t_sp_reg_b=record I,R,IXx,IXi,IYy,IYi,SPp,SPs,PCc,PCp:byte;end;
t_keyboard=array[0..7]of byte;
var
pressHelp,pressExit,err_command:boolean;
Quarz:byte;
memory:^t_memory;
dosRom,sosRom:^t_rom;
keyboard:t_keyboard;
reg,reg_:t_reg;
reg_w:t_reg_w absolute reg;
reg__w:t_reg_w absolute reg_;
sp_reg:t_sp_reg;
sp_reg_b:t_sp_reg_b absolute sp_reg;
Int05Save,Int09Save,Int1CSave:Pointer;
Clock,MaxClock:longint;
kempston,IM,pause:byte;
TheDosMode,NonHalt,MaskInt,NonMaskReady,TimerReady:boolean;
Palette:PaletteType;
implementation
(****************************************************************************)
{$F+}function SVGA:integer;begin;SVGA:=0;end;{$F-}
(****************************************************************************)
{$F+,S-}
procedure NonMaskInt;interrupt;
begin
asm cli end;
NonMaskReady:=true;
asm sti end;
end;

procedure KeyboardInt;interrupt;
var _key:byte;
_my_key:boolean;
begin
if DebugMode then
 begin
  asm
  pushf
  call Int09Save
  end;
  exit;
 end;
asm
in al,$60
mov _key,al
end;
_my_key:=true;
case _key of
$B7:asm int $05 end;
$01:pressExit:=true;{Esc}
$3B:pressHelp:=true;{F1}
$BB:pressHelp:=false;
$2A:ResBit(0,keyboard[0]);{CS}
$AA:SetBit(0,keyboard[0]);
$2C:ResBit(1,keyboard[0]);{Z}
$AC:SetBit(1,keyboard[0]);
$2D:ResBit(2,keyboard[0]);{X}
$AD:SetBit(2,keyboard[0]);
$2E:ResBit(3,keyboard[0]);{C}
$AE:SetBit(3,keyboard[0]);
$2F:ResBit(4,keyboard[0]);{V}
$AF:SetBit(4,keyboard[0]);
$1E:ResBit(0,keyboard[1]);{A}
$9E:SetBit(0,keyboard[1]);
$1F:ResBit(1,keyboard[1]);{S}
$9F:SetBit(1,keyboard[1]);
$20:ResBit(2,keyboard[1]);{D}
$A0:SetBit(2,keyboard[1]);
$21:ResBit(3,keyboard[1]);{F}
$A1:SetBit(3,keyboard[1]);
$22:ResBit(4,keyboard[1]);{G}
$A2:SetBit(4,keyboard[1]);
$10:ResBit(0,keyboard[2]);{Q}
$90:SetBit(0,keyboard[2]);
$11:ResBit(1,keyboard[2]);{W}
$91:SetBit(1,keyboard[2]);
$12:ResBit(2,keyboard[2]);{E}
$92:SetBit(2,keyboard[2]);
$13:ResBit(3,keyboard[2]);{R}
$93:SetBit(3,keyboard[2]);
$14:ResBit(4,keyboard[2]);{T}
$94:SetBit(4,keyboard[2]);
$02:ResBit(0,keyboard[3]);{1}
$82:SetBit(0,keyboard[3]);
$03:ResBit(1,keyboard[3]);{2}
$83:SetBit(1,keyboard[3]);
$04:ResBit(2,keyboard[3]);{3}
$84:SetBit(2,keyboard[3]);
$05:ResBit(3,keyboard[3]);{4}
$85:SetBit(3,keyboard[3]);
$06:ResBit(4,keyboard[3]);{5}
$86:SetBit(4,keyboard[3]);
$07:ResBit(4,keyboard[4]);{6}
$87:SetBit(4,keyboard[4]);
$08:ResBit(3,keyboard[4]);{7}
$88:SetBit(3,keyboard[4]);
$09:ResBit(2,keyboard[4]);{8}
$89:SetBit(2,keyboard[4]);
$0A:ResBit(1,keyboard[4]);{9}
$8A:SetBit(1,keyboard[4]);
$0B:ResBit(0,keyboard[4]);{0}
$8B:SetBit(0,keyboard[4]);
$15:ResBit(4,keyboard[5]);{Y}
$95:SetBit(4,keyboard[5]);
$16:ResBit(3,keyboard[5]);{U}
$96:SetBit(3,keyboard[5]);
$17:ResBit(2,keyboard[5]);{I}
$97:SetBit(2,keyboard[5]);
$18:ResBit(1,keyboard[5]);{O}
$98:SetBit(1,keyboard[5]);
$19:ResBit(0,keyboard[5]);{P}
$99:SetBit(0,keyboard[5]);
$23:ResBit(4,keyboard[6]);{H}
$A3:SetBit(4,keyboard[6]);
$24:ResBit(3,keyboard[6]);{J}
$A4:SetBit(3,keyboard[6]);
$25:ResBit(2,keyboard[6]);{K}
$A5:SetBit(2,keyboard[6]);
$26:ResBit(1,keyboard[6]);{L}
$A6:SetBit(1,keyboard[6]);
$1C:ResBit(0,keyboard[6]);{EN}
$9C:SetBit(0,keyboard[6]);
$30:ResBit(4,keyboard[7]);{B}
$B0:SetBit(4,keyboard[7]);
$31:ResBit(3,keyboard[7]);{N}
$B1:SetBit(3,keyboard[7]);
$32:ResBit(2,keyboard[7]);{M}
$B2:SetBit(2,keyboard[7]);
$36:ResBit(1,keyboard[7]);{SS}
$B6:SetBit(1,keyboard[7]);
$39:ResBit(0,keyboard[7]);{SP}
$B9:SetBit(0,keyboard[7]);
$4D:SetBit(0,kempston);{Right}
$CD:ResBit(0,kempston);
$4B:SetBit(1,kempston);{Left}
$CB:ResBit(1,kempston);
$50:SetBit(2,kempston);{Down}
$D0:ResBit(2,kempston);
$48:SetBit(3,kempston);{Up}
$C8:ResBit(3,kempston);
$1D:SetBit(4,kempston);{Fire}
$9D:ResBit(4,kempston);
else _my_key:=false;
end;
if not(_my_key)then
asm
pushf
call Int09Save
end else
asm
in al,$61
mov ah,al
or al,$80
out $61,al
xchg ah,al
out $61,al

mov al,$20
out $20,al
end;
end;

procedure TimerHandler;interrupt;
begin
asm cli end;
TimerReady:=true;
asm sti end;
end;
{$F-,S+}
(****************************************************************************)
procedure set_screen;
var _i:integer;
ErrCode: Integer;
begin
_i:=InstallUserDriver('svga256',@SVGA);
_i:=0;
initgraph(_i,_i,'');
ErrCode := GraphResult;
if ErrCode <> grOk then
 begin
 Writeln('Graphics error:', GraphErrorMsg(ErrCode));
 halt;
 end;
InitPalette;
end;
(****************************************************************************)
procedure set_interrupt;
var _w:word;
begin
GetIntVec($05,Int05Save);
SetIntVec($05,Addr(NonMaskInt));
GetIntVec($09,Int09Save);
SetIntVec($09,Addr(KeyboardInt));{kbr}
GetIntVec($1C,Int1CSave);
SetIntVec($1C,Addr(TimerHandler));
_w:=round(1.19e6/Quarz);
asm
cli
mov al,$36
out $43,al
mov ax,_w
out $40,al
xchg al,ah
out $40,al
sti
end;
end;
(****************************************************************************)
procedure set_keyboard;
var _b:byte;
begin
for _b:=0 to 7 do keyboard[_b]:=$BF;
kempston:=$00;
end;
(****************************************************************************)
procedure reset_interrupt;
begin
SetIntVec($05,Int05Save);
SetIntVec($09,Int09Save);
SetIntVec($1C,Int1CSave);
asm
cli
mov al,$36
out $43,al
xor al,al{18.2}
out $40,al
out $40,al
sti
end;
end;
(****************************************************************************)
procedure my_putpixel(_x,_y,_c:word);
begin
EmulScreen^[320*_y+_x]:=_c;
if not(traceMode)then putpixel(_x,_y,_c);
end;
(****************************************************************************)
const recol:array[0..7]of byte=(0,1,4,5,2,3,6,7);
procedure setBorder(_color:byte);
var _c:byte;
begin
_c:=recol[_color];
port[$3C8]:=255;
port[$3C9]:=VGAColors[_c].RedVal;
port[$3C9]:=VGAColors[_c].GreenVal;
port[$3C9]:=VGAColors[_c].BlueVal;
end;
(****************************************************************************)
procedure byte_to_screen(screen_add:word;screen_dat:byte);
function getcol(_c:byte):byte;
var cc:byte;
begin
cc:=recol[_c and $07];
if (_c and $40)<>0 then inc(cc,8);
getcol:=cc;
end;
function getbgr(_c:byte):byte;
begin
getbgr:=recol[(_c and $38)shr 3];
end;
var _x,_y,_i,_j:integer;
_b1,_b2,_bgr,_col,_dat:byte;
_mem_adr:word;
begin
if screen_add<$5800 then
 begin
_b1:=hi(screen_add)and $18;
_b2:=(lo(screen_add)shr 5);
  _y:=(_b1 shl 3)or(_b2 shl 3)or(hi(screen_add)and $07);
  _x:=lo(screen_add)and $1F;
_dat:=memory^[$5800or _x or((_b1 or _b2)shl 5)];
_col:=getcol(_dat);
_bgr:=getbgr(_dat);
_x:=_x shl 3;
  for _i:=0 to 7 do
   if setsBit(7-_i,screen_dat)then my_putpixel(xGak+_x+_i,yGak+_y,_col)else my_putpixel(xGak+_x+_i,yGak+_y,_bgr);
 end else
 begin
_b1:=(hi(screen_add)and $03)shl 3;
_b2:=lo(screen_add);
_y:=(_b1 or((_b2 and $E0)shr 5))shl 3;
_x:=(_b2 and $1F)shl 3;
_col:=getcol(screen_dat);
_bgr:=getbgr(screen_dat);
_mem_adr:=$4000or((_b1 shl 8)or _b2);
  for _j:=0 to 7 do
   begin
   _dat:=memory^[_mem_adr];
   inc(_mem_adr,$0100);
   for _i:=0 to 7 do
    if setsBit(7-_i,_dat)then my_putpixel(xGak+_x+_i,yGak+_y+_j,_col)else my_putpixel(xGak+_x+_i,yGak+_y+_j,_bgr);
   end;
 end;
end;
(****************************************************************************)
procedure write_b(mem_addr:word;mem_dat:byte);
begin
if mem_addr<16384 then exit;
if mem_addr<16384+6912 then if memory^[mem_addr]<>mem_dat then byte_to_screen(mem_addr,mem_dat);
memory^[mem_addr]:=mem_dat;
end;
procedure write_w(mem_addr_w:word;mem_dat_w:word);
begin
write_b(mem_addr_w,lo(mem_dat_w));
write_b(mem_addr_w+1,hi(mem_dat_w));
end;
(****************************************************************************)
function mem_w(_addr:word):word;
begin
mem_w:=$100*memory^[_addr+1]+memory^[_addr]
end;
function mem_b(_addr:word):byte;
begin
mem_b:=memory^[_addr]
end;
(****************************************************************************)
function SetsC:boolean;begin if setsBit(0,reg.F)then SetsC:=true else SetsC:=false;end;
procedure SetC;begin setBit(0,reg.F);end;{перенос}
procedure ResetC;begin resBit(0,reg.F);end;
procedure SetN;begin setBit(1,reg.F);end;{сложения/вычитания}
procedure ResetN;begin resBit(1,reg.F);end;
function SetsPV:boolean;begin if setsBit(2,reg.F)then SetsPV:=true else SetsPV:=false;end;
procedure SetPV;begin setBit(2,reg.F);end;{переполнение/четность}
procedure ResetPV;begin resBit(2,reg.F);end;

function SetsH:boolean;begin if setsBit(4,reg.F)then SetsH:=true else SetsH:=false;end;
procedure SetH;begin setBit(4,reg.F);end;{полуперенос}
procedure ResetH;begin resBit(4,reg.F);end;

function SetsZ:boolean;begin if setsBit(6,reg.F)then SetsZ:=true else SetsZ:=false;end;
procedure SetZ;begin setBit(6,reg.F);end;{нуль}
procedure ResetZ;begin resBit(6,reg.F);end;
function SetsS:boolean;begin if setsBit(7,reg.F)then SetsS:=true else SetsS:=false;end;
procedure SetS;begin setBit(7,reg.F);end;{знак}
procedure ResetS;begin resBit(7,reg.F);end;
(****************************************************************************)
procedure ChangeZS(_dat:byte);
begin
if _dat=0 then SetZ else ResetZ;
if _dat>=$80 then SetS else ResetS;
end;
procedure ChangeC(_min,_max:word);
begin
if _max<_min then SetC else ResetC;
end;
procedure ChangeV(__to,__add,__result:byte);
begin
if (__to<$80)and(__add<$80)and(__result>=$80)then SetPV else
 if (__to>=$80)and(__add>=$80)and(__result<$80)then SetPV else ResetPV
end;
procedure ChangeP(__result:byte);
var _i,_d:byte;
begin
_d:=0;
for _i:=0 to 7 do if SetsBit(_i,__result)then inc(_d);
if (_d and $01)=0 then SetPV else ResetPV;
end;
procedure Change35(__result:byte);
begin
reg.F:=(reg.F or $28)and($FF-(($FF-__result)and$28));
end;
procedure ChangeH(__to,__add:byte);
begin
if ((__to and $0F)+(__add and $0F))>$0F then SetH else ResetH;
end;
(****************************************************************************)
procedure push(dat:word);
begin
dec(sp_reg.SP,2);
write_w(sp_reg.SP,dat);
end;
(****************************************************************************)
procedure pop(var dat:word);
begin
dat:=mem_w(sp_reg.SP);
inc(sp_reg.SP,2);
end;
(****************************************************************************)
function IntAddr:word;
var mem_addr:word;
begin
case IM of
0,1:IntAddr:=$0038;
2:begin
  mem_addr:=$100*sp_reg.I+$FF;
  IntAddr:=mem_w(mem_addr);
  end;
end;
end;
procedure InterruptZX;
begin
{NonHalt:=true;{??}
if MaskInt then exit;
NonHalt:=true;
push(sp_reg.PC);
sp_reg.PC:=IntAddr;
end;
(****************************************************************************)
procedure timer(takts:byte);
var x:longint;
b:byte;
w:word;
f:file;
begin
if pressHelp then
 begin
reset_interrupt;
assign(f,'help.dat');
reset(f,1);
blockread(f,RealScreen,320*200,w);
close(f);
set_interrupt;
repeat until not(pressHelp);
RealScreen:=EmulScreen^;
 end;
inc(sp_reg.R,takts shr 2);
inc(Clock,takts);
{+поддержка БИС контроллера дисковода+}
inc(ClockIS18WG93,takts);
if ClockIS18WG93>maxClockIS18WG93{1Мгц}then
 begin
  taktIS18WG93;
  dec(ClockIS18WG93,maxClockIS18WG93);
 end;
{++++++++++++++++++++++++++++++++++++}
if Clock>MaxClock then
 begin
  x:=0;
{задержка по таймеру}  while not(TimerReady)do
   begin
  inc(x);
   end;
(*if not(traceMode)then {RealScreen:=EmulScreen^;}{выброс из Z-буфера}
asm
push es
push ds
push si
mov ax,$A000
mov es,ax
mov di,$0000
mov cx,320*200/4
lds si,EmulScreen;
cld
rep
db $66
movsw
pop si
pop ds
pop es
end;*)
{задержка по таймеру}  if x>0 then inc(pause)else begin if pause>0 then dec(pause) end;
(* asm{привязка к монитору}
 mov dx,$3DA
 @nxt:
 in al,dx
 test al,$08
 jz @nxt
 end;*)
  InterruptZX;
  Clock:=0;
  TimerReady:=false;
 end;
{задержка по таймеру}w:=pause*takts;
asm
mov cx,w
inc cx
@1:loop @1;
end;
if NonMaskReady then
 begin
 NonMaskReady:=false;
 sp_reg.PC:=$0066;
 end;
end;
(****************************************************************************)
procedure setSos;
var _w:word;
begin
for _w:=0 to $3FFF do memory^[_w]:=sosRom^[_w];
TheDosMode:=false;
end;
procedure setDos;
var _w:word;
begin
for _w:=0 to $3FFF do memory^[_w]:=dosRom^[_w];
TheDosMode:=true;
end;

procedure get_mem(_mem:pointer;_osName:string);
var f:file;
begin
assign(f,_osName);
reset(f,1);
blockread(f,_mem^,filesize(f));
close(f);
end;
procedure get_rom;
var w:word;
begin
new(memory);
new(sosRom);
new(dosRom);

get_mem(sosRom,'sos.rom');
get_mem(dosRom,'dos.rom');

for w:=$0 to $FFFF do memory^[w]:=0;
get_mem(memory,'dos.rom'{'sos.rom'{'oc.rom''mugsy.rom'{'demo.rom''zybex.rom'{'art_std.rom'});
TheDosMode:=true;
end;
(****************************************************************************)
procedure addRegN(var _to:byte;_add:byte);
var _result:byte;
begin
_result:=_to+_add;
ChangeZS(_result);
ChangeC(_to,_result);
ChangeV(_to,_add,_result);
Change35(_result);
ChangeH(_to,_add);
ResetN;
_to:=_result;
end;
(****************************************************************************)
procedure incReg(var _to:byte);
var _result:byte;
begin
_result:=_to+1;
ChangeZS(_result);
ChangeV(_to,1,_result);
Change35(_result);
ChangeH(_to,1);
ResetN;
_to:=_result;
end;
(****************************************************************************)
procedure adcRegN(var _to:byte;_add:byte);
var _result,_addd:byte;
_SetsC:boolean;
begin
_addd:=_add;
if SetsC then begin _SetsC:=true;inc(_addd) end else _SetsC:=false;
_result:=_to+_addd;
ChangeZS(_result);
ChangeV(_to,_add,_result);
Change35(_result);
ChangeC(_to,_result);
if (_add=$FF)and _SetsC then SetC;
ChangeH(_to,_addd);
if (((_add and $0F)=$0F)or((_to and $0F)=$0F))and _SetsC then SetH;
ResetN;
_to:=_result;
end;
(****************************************************************************)
procedure decReg(var _to:byte);
var _result:byte;
begin
_result:=_to-1;
ChangeZS(_result);
ChangeV(_result,1,_to);
Change35(_result);
ChangeH(_result,1);
SetN;
_to:=_result;
end;
(****************************************************************************)
procedure subRegN(var _to:byte;_sub:byte);
var _result:byte;
begin
_result:=_to-_sub;
ChangeZS(_result);
ChangeC(_result,_to);
ChangeV(_result,_sub,_to);
Change35(_result);
ChangeH(_result,_sub);
SetN;
_to:=_result;
end;
(****************************************************************************)
procedure sbcRegN(var _to:byte;_sub:byte);
var _result,_subresult:byte;
_SetsC:boolean;
begin
_result:=_to;
if SetsC then _SetsC:=true else _SetsC:=false;
subRegN(_result,_sub);
if _SetsC then
 begin
_subresult:=_result;
dec(_result);
ChangeZS(_result);
Change35(_result);
if not(SetsC)then ChangeC(_result,_subresult);
ChangeV(_result,_sub,_to);
if not(SetsH)then ChangeH(_result,1);
 end;
_to:=_result;
end;
(****************************************************************************)
procedure cpRegN(_to,_cp:byte);
begin
subRegN(_to,_cp);
Change35(_cp);
end;
(****************************************************************************)
procedure ChangeLog(__result:byte);
begin
ChangeZS(__result);
ResetC;
ChangeP(__result);
Change35(__result);
ResetN;
end;
(****************************************************************************)
procedure andRegN(var _to:byte;_and:byte);
var _result:byte;
begin
_result:=_to and _and;
ChangeLog(_result);
SetH;
_to:=_result;
end;
(****************************************************************************)
procedure orRegN(var _to:byte;_and:byte);
var _result:byte;
begin
_result:=_to or _and;
ChangeLog(_result);
ResetH;
_to:=_result;
end;
(****************************************************************************)
procedure xorRegN(var _to:byte;_and:byte);
var _result:byte;
begin
_result:=_to xor _and;
ChangeLog(_result);
ResetH;
_to:=_result;
end;
(****************************************************************************)
function reIndex(__index:word;__s:byte):word;
begin
if __s>=$80 then reIndex:=__index-($100-__s) else reIndex:=__index+__s;
end;
(****************************************************************************)
procedure jr(_sAdr:byte);
begin
if _sAdr>=$80 then dec(sp_reg.PC,$100-_sAdr) else inc(sp_reg.PC,_sAdr);
end;
(****************************************************************************)
procedure Change35HN(_result:byte);
begin
Change35(_result);
ResetN;
ResetH;
end;
procedure ChangeZS35PHN(__result:byte);
begin
ChangeZS(__result);
ChangeP(__result);
Change35HN(__result);
end;
(****************************************************************************)
procedure srl(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
if setsBit(0,_b) then SetC else ResetC;
_b:=_b shr 1;
_dat:=_b;
end;
(****************************************************************************)
procedure sra(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
if setsBit(0,_b) then SetC else ResetC;
_b:=_b shr 1;
if setsBit(6,_b) then setBit(7,_b) else resBit(7,_b);
_dat:=_b;
end;
(****************************************************************************)
procedure sla(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
if setsBit(7,_b) then SetC else ResetC;
_b:=_b shl 1;
_dat:=_b;
end;
(****************************************************************************)
procedure rl(var _dat:byte);
var _b:byte;
_SetsC:boolean;
begin
_b:=_dat;
if SetsC then _SetsC:=true else _SetsC:=false;
if setsBit(7,_b) then SetC else ResetC;
_b:=_b shl 1;
if _SetsC then setBit(0,_b) else resBit(0,_b);
_dat:=_b;
end;
(****************************************************************************)
procedure rr(var _dat:byte);
var _b:byte;
_SetsC:boolean;
begin
_b:=_dat;
if SetsC then _SetsC:=true else _SetsC:=false;
if setsBit(0,_b) then SetC else ResetC;
_b:=_b shr 1;
if _SetsC then setBit(7,_b) else resBit(7,_b);
_dat:=_b;
end;
(****************************************************************************)
procedure rlc(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
if setsBit(7,_b) then SetC else ResetC;
asm
mov al,_b
rol al,1
mov _b,al
end;
ResetH;
ResetN;
Change35(_b);
_dat:=_b;
end;
(****************************************************************************)
procedure rrc(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
if setsBit(0,_b) then SetC else ResetC;
asm
mov al,_b
ror al,1
mov _b,al
end;
_dat:=_b;
end;
(****************************************************************************)
procedure bitBit(_bitNum,_byte:byte);
begin
if setsBit(_bitNum,_byte) then begin ResetPV;ResetZ end else begin SetPV;SetZ end;
Change35(_byte);
ResetS;
SetH;
ResetN;
end;
(****************************************************************************)
function writeB(_dt:byte):string;forward;

procedure writedebug(_str:string;_a0,_a8,_b:byte);
var f:text;
begin
assign(f,'debug.tmp');
append(f);
writeln(f,_str,writeB(_a0),' ',writeB(_a8),' ',writeB(_b),' ',
writeB(hi(sp_reg.pc-1)),writeB(lo(sp_reg.pc-1)));
close(f);
end;

procedure inDat(_a0a7,_a8a15:byte;var _dat:byte);
var _b,_i:byte;
s_dat:string;
begin
_dat:=0;
{if TheDosMode then
writeDebug('inDos:',_a0a7,_a8a15,_dat) else
writeDebug('inSos:',_a0a7,_a8a15,_dat)};
if TheDosMode then{эмуляция БИС 1818 ВГ 93}
 begin
_dat:=inDatIS18WG93(_a0a7);
 exit;
 end;
 case _a0a7 of
$FE:begin
  case _a8a15 of
  $00,$E7:begin
  _b:=$FF;
  for _i:=0 to 7 do _b:=_b and keyboard[_i];
  _dat:=_b;
      end;
  $FE:_dat:=keyboard[0];
  $FD:_dat:=keyboard[1];
  $FB:_dat:=keyboard[2];
  $F7:_dat:=keyboard[3];
  $EF:_dat:=keyboard[4];
  $DF:_dat:=keyboard[5];
  $BF:_dat:=keyboard[6];
  $7F:_dat:=keyboard[7];
  else
  begin
{  err_command:=true;}
  end;
  end;
  ResBit(6,_dat);{EAR}
    end;
$1F:_dat:=kempston;
$FC:_dat:=191;
else
begin
{err_command:=true;}
end;
end
end;
(****************************************************************************)
procedure outDat(_a0a7,_a8a15,_dat:byte);
var _bb:byte;
begin
{if TheDosMode then
writeDebug('otDos:',_a0a7,_a8a15,_dat) else
writeDebug('otSos:',_a0a7,_a8a15,_dat)};

if TheDosMode then{эмуляция БИС 1818 ВГ 93}
 begin
outDatIS18WG93(_a0a7,_dat);
 exit;
 end;

case _a0a7 of
$FE:begin
 if setsBit(4,_dat){ xor setsBit(3,_dat) }then{speacker | MIC}
  asm
   in al,$61;
   and al,not 1;
   or al,2;
   out $61,al
  end else
  asm
   in al,$61;
   and al,not 1;
   and al,not 2;
   out $61,al
  end;
 setBorder(_dat and $07);
    end;
$7F:begin end;
$5F:begin end;
else
begin
{err_command:=true;}
end;
end;
end;
(****************************************************************************)
procedure _LDRegN(var b:byte);
begin
inc(sp_reg.PC);
b:=mem_b(sp_reg.PC);
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _LDRegNN(var w:word;time:word);
begin
inc(sp_reg.PC);
w:=mem_w(sp_reg.PC);
inc(sp_reg.PC,2);
timer(time);
end;
(****************************************************************************)
procedure _LDRegReg(var _to:byte;_from:byte);
begin
_to:=_from;
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _LDfromIR(_from:byte);
begin
reg.A:=_from;
inc(sp_reg.PC);
if MaskInt then ResetPV else SetPV;
ChangeZS(_from);
timer(9);
end;
(****************************************************************************)
procedure _LDtoIR(var _to:byte);
begin
_to:=reg.A;
inc(sp_reg.PC);
timer(9);
end;
(****************************************************************************)
procedure _LDRpRp(var _to:word;_from:word;time:byte);
begin
_to:=_from;
inc(sp_reg.PC,1);
timer(time);
end;
(****************************************************************************)
procedure _LDRpAdr(var _to:word;time:byte);
var _w:word;
begin
inc(sp_reg.PC);
_w:=mem_w(sp_reg.PC);
_to:=mem_w(_w);
inc(sp_reg.PC,2);
timer(time);
end;
(****************************************************************************)
procedure _LDRegAdr(var _to:byte;time:byte);
var _w:word;
begin
inc(sp_reg.PC);
_w:=mem_w(sp_reg.PC);
_to:=mem_b(_w);
inc(sp_reg.PC,2);
timer(time);
end;
(****************************************************************************)
procedure _LDRegArp(var _to:byte;_adr:word);
begin
_to:=mem_b(_adr);
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _LDRegIdx(var _to:byte;_Index:word);
begin
inc(sp_reg.PC);
_to:=mem_b(reIndex(_Index,mem_b(sp_reg.PC)));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _EXRpRp(var _a,_b:word);
var _w:word;
begin
_w:=_a;
_a:=_b;
_b:=_w;
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _EXX;
var reg_tmp:t_reg;
proba:word;
begin
reg_tmp:=reg;
reg:=reg_;
reg_:=reg_tmp;

proba:=reg__w.FA;
reg__w.FA:=reg_w.FA;
reg_w.FA:=proba;

inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _LDAdrReg(_dat:byte;time:byte);
var _w:word;
begin
inc(sp_reg.PC);
_w:=mem_w(sp_reg.PC);
write_b(_w,_dat);
inc(sp_reg.PC,2);
timer(time);
end;
(****************************************************************************)
procedure _LDAdrRp(_dat:word;time:byte);
var _w:word;
begin
inc(sp_reg.PC);
_w:=mem_w(sp_reg.PC);
write_w(_w,_dat);
inc(sp_reg.PC,2);
timer(time);
end;
(****************************************************************************)
procedure _LDArpReg(_adr:word;_dat:byte);
begin
write_b(_adr,_dat);
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _LDArpN(_adr:word);
begin
inc(sp_reg.PC);
write_b(_adr,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(10);
end;
(****************************************************************************)
procedure _LDIdxReg(_Index:word;_dat:byte);
begin
inc(sp_reg.PC);
write_b(reIndex(_Index,mem_b(sp_reg.PC)),_dat);
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _LDIdxN(_Index:word);
var _adr:word;
begin
inc(sp_reg.PC);
_adr:=reIndex(_Index,mem_b(sp_reg.PC));
inc(sp_reg.PC);
write_b(_adr,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _ADDRegAN;
begin
inc(sp_reg.PC);
addRegN(reg.A,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _ADDRegAReg(_reg:byte);
begin
addRegN(reg.A,_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _ADDRegAArp(_adr:word);
begin
addRegN(reg.A,mem_b(_adr));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _ADDRegAIdx(_Index:word);
begin
inc(sp_reg.PC);
addRegN(reg.A,mem_b(reIndex(_Index,mem_b(sp_reg.PC))));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _ADDRpRp(var _to:word;_add:word;time:byte);
var _result:word;
begin
_result:=_to+_add;
ChangeC(_to,_result);
ResetN;
Change35(hi(_result));
{ChangeH(hi(_to),hi(lo(_to)+lo(_add)));???}
_to:=_result;
inc(sp_reg.PC);
timer(time);
end;
(****************************************************************************)
procedure _INCReg(var _reg:byte);
var _result:byte;
begin
incReg(_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _INCArp(_adr:word);
var _dat:byte;
begin
_dat:=mem_b(_adr);
incReg(_dat);
write_b(_adr,_dat);
inc(sp_reg.PC);
timer(11);
end;
(****************************************************************************)
procedure _INCAIdx(_Index:word);
var _dat:byte;
_adr:word;
begin
inc(sp_reg.PC);
_adr:=reIndex(_Index,mem_b(sp_reg.PC));
_dat:=mem_b(_adr);
incReg(_dat);
write_b(_adr,_dat);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _INCRp(var _dat:word;time:byte);
begin
inc(_dat);
inc(sp_reg.PC);
timer(time);
end;
(****************************************************************************)
procedure _ADCRegAN;
begin
inc(sp_reg.PC);
adcRegN(reg.A,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _ADCRegAReg(_reg:byte);
begin
adcRegN(reg.A,_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _ADCRegAArp(_adr:word);
begin
adcRegN(reg.A,mem_b(_adr));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _ADCRegAIdx(_Index:word);
begin
inc(sp_reg.PC);
adcRegN(reg.A,mem_b(reIndex(_Index,mem_b(sp_reg.PC))));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _ADCHlRp(_reg:word);
begin
adcRegN(reg.L,lo(_reg));
adcRegN(reg.H,hi(_reg));
if reg_w.HL=0 then SetZ else ResetZ;
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _SUBRegAN;
begin
inc(sp_reg.PC);
subRegN(reg.A,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _SUBRegAReg(_reg:byte);
begin
subRegN(reg.A,_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _SUBRegAArp(_adr:word);
begin
subRegN(reg.A,mem_b(_adr));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _SUBRegAIdx(_Index:word);
begin
inc(sp_reg.PC);
subRegN(reg.A,mem_b(reIndex(_Index,mem_b(sp_reg.PC))));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _DECReg(var _reg:byte);
begin
decReg(_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _DECArp(_adr:word);
var _dat:byte;
begin
_dat:=mem_b(_adr);
decReg(_dat);
write_b(_adr,_dat);
inc(sp_reg.PC);
timer(11);
end;
(****************************************************************************)
procedure _DECAIdx(_Index:word);
var _dat:byte;
_adr:word;
begin
inc(sp_reg.PC);
_adr:=reIndex(_Index,mem_b(sp_reg.PC));
_dat:=mem_b(_adr);
decReg(_dat);
write_b(_adr,_dat);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _DECRp(var _dat:word;time:byte);
begin
dec(_dat);
inc(sp_reg.PC);
timer(time);
end;
(****************************************************************************)
procedure _SBCRegAN;
begin
inc(sp_reg.PC);
sbcRegN(reg.A,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _SBCRegAReg(_reg:byte);
begin
sbcRegN(reg.A,_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _SBCRegAArp(_adr:word);
begin
sbcRegN(reg.A,mem_b(_adr));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _SBCRegAIdx(_Index:word);
begin
inc(sp_reg.PC);
sbcRegN(reg.A,mem_b(reIndex(_Index,mem_b(sp_reg.PC))));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _SBCHlRp(_reg:word);
begin
sbcRegN(reg.L,lo(_reg));
sbcRegN(reg.H,hi(_reg));
if reg_w.HL=0 then SetZ else ResetZ;
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _CPRegAN;
begin
inc(sp_reg.PC);
cpRegN(reg.A,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _CPRegAReg(_reg:byte);
begin
cpRegN(reg.A,_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _CPRegAArp(_adr:word);
begin
cpRegN(reg.A,mem_b(_adr));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _CPRegAIdx(_Index:word);
begin
inc(sp_reg.PC);
cpRegN(reg.A,mem_b(reIndex(_Index,mem_b(sp_reg.PC))));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _ANDRegAN;
begin
inc(sp_reg.PC);
andRegN(reg.A,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _ANDRegAReg(_reg:byte);
begin
andRegN(reg.A,_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _ANDRegAArp(_adr:word);
begin
andRegN(reg.A,mem_b(_adr));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _ANDRegAIdx(_Index:word);
begin
inc(sp_reg.PC);
andRegN(reg.A,mem_b(reIndex(_Index,mem_b(sp_reg.PC))));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _ORRegAN;
begin
inc(sp_reg.PC);
orRegN(reg.A,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _ORRegAReg(_reg:byte);
begin
orRegN(reg.A,_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _ORRegAArp(_adr:word);
begin
orRegN(reg.A,mem_b(_adr));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _ORRegAIdx(_Index:word);
begin
inc(sp_reg.PC);
orRegN(reg.A,mem_b(reIndex(_Index,mem_b(sp_reg.PC))));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _XORRegAN;
begin
inc(sp_reg.PC);
xorRegN(reg.A,mem_b(sp_reg.PC));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _XORRegAReg(_reg:byte);
begin
xorRegN(reg.A,_reg);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _XORRegAArp(_adr:word);
begin
xorRegN(reg.A,mem_b(_adr));
inc(sp_reg.PC);
timer(7);
end;
(****************************************************************************)
procedure _XORRegAIdx(_Index:word);
begin
inc(sp_reg.PC);
xorRegN(reg.A,mem_b(reIndex(_Index,mem_b(sp_reg.PC))));
inc(sp_reg.PC);
timer(19);
end;
(****************************************************************************)
procedure _JPNN;
begin
inc(sp_reg.PC);
sp_reg.PC:=mem_w(sp_reg.PC);
timer(10);
end;
(****************************************************************************)
procedure _JPArp(_adr:word;time:byte);
begin
sp_reg.PC:=_adr;
timer(time);
end;
(****************************************************************************)
procedure _JPNNif(_if:boolean);
begin
inc(sp_reg.PC);
if _if then sp_reg.PC:=mem_w(sp_reg.PC) else inc(sp_reg.PC,2);
timer(10);
end;
(****************************************************************************)
procedure _JRS;
var _b:byte;
begin
inc(sp_reg.PC);
_b:=mem_b(sp_reg.PC);
inc(sp_reg.PC);
jr(_b);
timer(7);
end;
(****************************************************************************)
procedure _JRSif(_if:boolean);
var _b:byte;
begin
inc(sp_reg.PC);
_b:=mem_b(sp_reg.PC);
inc(sp_reg.PC);
if _if then
 begin
 jr(_b);
 timer(12);
 end else timer(7);
end;
(****************************************************************************)
procedure _DJNZ;
var _b:byte;
begin
dec(reg.B);
inc(sp_reg.PC);
_b:=mem_b(sp_reg.PC);
inc(sp_reg.PC);
if reg.B<>0 then
 begin
 jr(_b);
 timer(13);
 end else timer(8);
end;
(****************************************************************************)
procedure _PUSH(w,time:word);
begin
push(w);
inc(sp_reg.PC);
timer(time);
end;
(****************************************************************************)
procedure _POP(var w:word;time:word);
begin
pop(w);
inc(sp_reg.PC);
timer(time);
end;
(****************************************************************************)
procedure _EXsp(var _dat:word;time:word);
var _w:word;
begin
pop(_w);
push(_dat);
_dat:=_w;
inc(sp_reg.PC);
timer(time);
end;
(****************************************************************************)
procedure _RST(_adr:word);
begin
inc(sp_reg.PC);
push(sp_reg.PC);
sp_reg.PC:=_adr;
timer(11);
end;
(****************************************************************************)
procedure _CALL;
var _adr:word;
begin
inc(sp_reg.PC);
_adr:=mem_w(sp_reg.PC);
inc(sp_reg.PC,2);
push(sp_reg.PC);
sp_reg.PC:=_adr;
timer(17);
end;
(****************************************************************************)
procedure _CALLif(_if:boolean);
var _adr:word;
begin
inc(sp_reg.PC);
_adr:=mem_w(sp_reg.PC);
inc(sp_reg.PC,2);
if _if then
 begin
 push(sp_reg.PC);
 sp_reg.PC:=_adr;
 timer(17);
 end else timer(10);
end;
(****************************************************************************)
procedure _RET;
begin
pop(sp_reg.PC);
timer(10);
end;
(****************************************************************************)
procedure _RETif(_if:boolean);
var _adr:word;
begin
inc(sp_reg.PC);
if _if then
 begin
 pop(sp_reg.PC);
 timer(11);
 end else timer(5);
end;
(****************************************************************************)
procedure _SRLReg(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
srl(_b);
ChangeZS35PHN(_b);
_dat:=_b;
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _SRLArp(_adr:word);
var _b:byte;
begin
_b:=mem_b(_adr);
srl(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _SRLIdx(_Index:word);
var _b:byte;
_adr:word;
begin
_adr:=reIndex(_Index,mem_b(sp_reg.PC-1));
_b:=mem_b(_adr);
srl(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _SRAReg(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
sra(_b);
ChangeZS35PHN(_b);
_dat:=_b;
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _SRAArp(_adr:word);
var _b:byte;
begin
_b:=mem_b(_adr);
sra(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _SRAIdx(_Index:word);
var _b:byte;
_adr:word;
begin
_adr:=reIndex(_Index,mem_b(sp_reg.PC-1));
_b:=mem_b(_adr);
sra(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _SLAReg(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
sla(_b);
ChangeZS35PHN(_b);
_dat:=_b;
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _SLAArp(_adr:word);
var _b:byte;
begin
_b:=mem_b(_adr);
sla(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _SLAIdx(_Index:word);
var _b:byte;
_adr:word;
begin
_adr:=reIndex(_Index,mem_b(sp_reg.PC-1));
_b:=mem_b(_adr);
sla(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _RLReg(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
rl(_b);
ChangeZS35PHN(_b);
_dat:=_b;
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _RLArp(_adr:word);
var _b:byte;
begin
_b:=mem_b(_adr);
rl(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _RLIdx(_Index:word);
var _b:byte;
_adr:word;
begin
_adr:=reIndex(_Index,mem_b(sp_reg.PC-1));
_b:=mem_b(_adr);
rl(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _RRReg(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
rr(_b);
ChangeZS35PHN(_b);
_dat:=_b;
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _RRArp(_adr:word);
var _b:byte;
begin
_b:=mem_b(_adr);
rr(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _RRIdx(_Index:word);
var _b:byte;
_adr:word;
begin
_adr:=reIndex(_Index,mem_b(sp_reg.PC-1));
_b:=mem_b(_adr);
rr(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _RLCReg(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
rlc(_b);
ChangeZS35PHN(_b);
_dat:=_b;
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _RLCArp(_adr:word);
var _b:byte;
begin
_b:=mem_b(_adr);
rlc(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _RLCIdx(_Index:word);
var _b:byte;
_adr:word;
begin
_adr:=reIndex(_Index,mem_b(sp_reg.PC-1));
_b:=mem_b(_adr);
rlc(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _RRCReg(var _dat:byte);
var _b:byte;
begin
_b:=_dat;
rrc(_b);
ChangeZS35PHN(_b);
_dat:=_b;
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _RRCArp(_adr:word);
var _b:byte;
begin
_b:=mem_b(_adr);
rrc(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _RRCIdx(_Index:word);
var _b:byte;
_adr:word;
begin
_adr:=reIndex(_Index,mem_b(sp_reg.PC-1));
_b:=mem_b(_adr);
rrc(_b);
ChangeZS35PHN(_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _RLA;
begin
rl(reg.A);
Change35HN(reg.A);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _RRA;
begin
rr(reg.A);
Change35HN(reg.A);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _RLCA;
begin
rlc(reg.A);
Change35HN(reg.A);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _RRCA;
begin
rrc(reg.A);
Change35HN(reg.A);
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _SET(_bit:byte;var _to:byte);
begin
setBit(_bit,_to);
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _SEThl(_bit:byte);
var _b:byte;
begin
_b:=mem_b(reg_w.HL);
setBit(_bit,_b);
write_b(reg_w.HL,_b);
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _SETix(_bit:byte;_Index:word);
var _b:byte;
_adr:word;
begin
_adr:=reIndex(_Index,mem_b(sp_reg.PC-1));
_b:=mem_b(_adr);
setBit(_bit,_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _RES(_bit:byte;var _to:byte);
begin
resBit(_bit,_to);
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _REShl(_bit:byte);
var _b:byte;
begin
_b:=mem_b(reg_w.HL);
resBit(_bit,_b);
write_b(reg_w.HL,_b);
inc(sp_reg.PC);
timer(15);
end;
(****************************************************************************)
procedure _RESix(_bit:byte;_Index:word);
var _b:byte;
_adr:word;
begin
_adr:=reIndex(_Index,mem_b(sp_reg.PC-1));
_b:=mem_b(_adr);
resBit(_bit,_b);
write_b(_adr,_b);
inc(sp_reg.PC);
timer(23);
end;
(****************************************************************************)
procedure _BIT(_bits:byte;var _to:byte);
begin
bitBit(_bits,_to);
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _BIThl(_bit:byte);
begin
bitBit(_bit,mem_b(reg_w.HL));
inc(sp_reg.PC);
timer(12);
end;
(****************************************************************************)
procedure _BITix(_bit:byte;_Index:word);
begin
bitBit(_bit,mem_b(reIndex(_Index,mem_b(sp_reg.PC-1))));
inc(sp_reg.PC);
timer(20);
end;
(****************************************************************************)
procedure _LDIR;
var oldMask:boolean;
begin
{!}oldMask:=MaskInt;
{!}MaskInt:=true;
SetPV;
inc(sp_reg.PC);
repeat
write_b(reg_w.DE,mem_b(reg_w.HL));
inc(reg_w.HL);
inc(reg_w.DE);
dec(reg_w.BC);
timer(21);
until reg_w.BC=0;
ResetPV;
timer(16);
{!}MaskInt:=oldMask;
end;
(****************************************************************************)
procedure _LDDR;
var oldMask:boolean;
begin
{!}oldMask:=MaskInt;
{!}MaskInt:=true;
SetPV;
inc(sp_reg.PC);
repeat
write_b(reg_w.DE,mem_b(reg_w.HL));
dec(reg_w.HL);
dec(reg_w.DE);
dec(reg_w.BC);
timer(21);
until reg_w.BC=0;
ResetPV;
timer(16);
{!}MaskInt:=oldMask;
end;
(****************************************************************************)
procedure _LDI;
begin
write_b(reg_w.DE,mem_b(reg_w.HL));
inc(reg_w.HL);
inc(reg_w.DE);
dec(reg_w.BC);
if reg_w.BC=0 then ResetPV else SetPV;
inc(sp_reg.PC);
timer(16);
end;
(****************************************************************************)
procedure _LDD;
begin
write_b(reg_w.DE,mem_b(reg_w.HL));
dec(reg_w.HL);
dec(reg_w.DE);
dec(reg_w.BC);
if reg_w.BC=0 then ResetPV else SetPV;
inc(sp_reg.PC);
timer(16);
end;
(****************************************************************************)
procedure _CPIR;
begin
SetPV;
ResetZ;
SetS;
inc(sp_reg.PC);
repeat
dec(reg_w.BC);
if reg.A=mem_b(reg_w.HL) then
 begin
 SetZ;
 ResetS;
 break;
 end;
inc(reg_w.HL);
if reg_w.BC=0 then
 begin
 ResetPV;
 timer(16);
 break;
 end else timer(21);
until false;
end;
(****************************************************************************)
procedure _CPDR;
begin
SetPV;
ResetZ;
SetS;
inc(sp_reg.PC);
repeat
dec(reg_w.BC);
if reg.A=mem_b(reg_w.HL) then
 begin
 SetZ;
 ResetS;
 break;
 end;
dec(reg_w.HL);
if reg_w.BC=0 then
 begin
 ResetPV;
 timer(16);
 break;
 end else timer(21);
until false;
end;
(****************************************************************************)
procedure _CPI;
begin
dec(reg_w.BC);
if reg.A=mem_b(reg_w.HL) then SetZ else
 begin
 ResetZ;
 inc(reg_w.HL);
 end;
if reg_w.BC=0 then ResetPV else SetPV;
inc(sp_reg.PC);
timer(16);
end;
(****************************************************************************)
procedure _CPD;
begin
dec(reg_w.BC);
if reg.A=mem_b(reg_w.HL) then SetZ else
 begin
 ResetZ;
 dec(reg_w.HL);
 end;
if reg_w.BC=0 then ResetPV else SetPV;
inc(sp_reg.PC);
timer(16);
end;
(****************************************************************************)
procedure _INRegAN;
begin
inc(sp_reg.PC);
inDat(mem_b(sp_reg.PC),reg.A,reg.A);
inc(sp_reg.PC);
timer(11);
end;
(****************************************************************************)
procedure _INRegC(var _dat:byte);
var _b:byte;
begin
inDat(reg.C,reg.B,_b);
ChangeZS(_b);
ChangeP(_b);
Change35(_b);
_dat:=_b;
inc(sp_reg.PC);
timer(12);
end;
(****************************************************************************)
procedure _INI;
var _b:byte;
begin
inDat(reg.C,reg.B,_b);
write_b(reg_w.HL,_b);
inc(reg_w.HL);
dec(reg.B);
ChangeZS(reg.B);
inc(sp_reg.PC);
timer(16);
end;
(****************************************************************************)
procedure _OUTRegAN;
begin
inc(sp_reg.PC);
outDat(mem_b(sp_reg.PC),reg.A,reg.A);
inc(sp_reg.PC);
timer(11);
end;
(****************************************************************************)
procedure _OUTRegC(_dat:byte);
var _b:byte;
begin
_b:=_dat;
outDat(reg.C,reg.B,_b);
ChangeZS(_b);
ChangeP(_b);
Change35(_b);
inc(sp_reg.PC);
timer(12);
end;
(****************************************************************************)
procedure _OUTI;
begin
outDat(reg.C,reg.B,mem_b(reg_w.HL));
inc(reg_w.HL);
dec(reg.B);
ChangeZS(reg.B);
inc(sp_reg.PC);
timer(16);
end;
(****************************************************************************)
procedure _EI;
begin
MaskInt:=false;
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _DI;
begin
MaskInt:=true;
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _IMode(mode_num:byte);
begin
IM:=mode_num;
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _RetI;
begin
MaskInt:=false;
pop(sp_reg.PC);
timer(14);
end;
(****************************************************************************)
procedure _RetN;
begin
pop(sp_reg.PC);
timer(14);
end;
(****************************************************************************)
procedure _NOP;
begin
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _CPL;
begin
reg.A:=$FF-reg.A;
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _NEG;
var _sb:byte;
begin
_sb:=$00;
subRegN(_sb,reg.A);
reg.A:=_sb;
inc(sp_reg.PC);
timer(8);
end;
(****************************************************************************)
procedure _SCF;
begin
SetC;
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _CCF;
begin
if SetsC then ResetC else SetC;
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _HALT;
begin
SetC;
inc(sp_reg.PC);
NonHalt:=false;
repeat
timer(4);
until NonHalt;
end;
(****************************************************************************)
procedure _DAA;
var p,a:boolean;
begin
asm
pushf
pop bx
mov bl,reg.F
push bx
popf
mov al,reg.A
daa
mov reg.A,al
pushf
pop bx
mov reg.F,bl
end;
inc(sp_reg.PC);
timer(4);
end;
(****************************************************************************)
procedure _RLD;
var _c,_b,_m:byte;
begin
_b:=reg.A and $0F;
reg.A:=reg.A and $F0;
_m:=mem_b(reg_w.HL);
reg.A:=reg.A or (_m shr 4);
_m:=(_m shl 4)or _b;
write_b(reg_w.HL,_m);
if reg.A=0 then SetZ else resetZ;
setH;
ReSetC;
inc(sp_reg.PC);
timer(18);
end;
(****************************************************************************)
procedure _RRD;
var _b,_m:byte;
begin
_b:=reg.A and $0F;
reg.A:=reg.A and $F0;
_m:=mem_b(reg_w.HL);
reg.A:=reg.A or (_m and $0F);
_m:=(_m shr 4)or (_b shl 4);
write_b(reg_w.HL,_m);
if reg.A=0 then SetZ else resetZ;
ResetH;
SetC;
inc(sp_reg.PC);
timer(18);
end;
(****************************************************************************)
(****************************************************************************)
{var st:string;
oldMemB:byte;}
(****************************************************************************)
procedure TraceTo(addr_trace:word);
begin
(*asm
{| RDTSC}db $0F,$31
{| MOV [TICS],EAX}db $66;mov tics_w,ax
{| CLD}cld
{| 8 NOP}nop;nop;nop;nop;nop;nop;nop;nop
{|instuction to test >}
end;*)
repeat
case memory^[sp_reg.PC]of
$00:_NOP;
$01:_LDRegNN(reg_w.BC,10);
$02:_LDArpReg(reg_w.BC,reg.A);
$03:_INCRp(reg_w.BC,6);
$04:_INCReg(reg.B);
$05:_DECReg(reg.B);
$06:_LDRegN(reg.B);
$07:_RLCA;
$08:_EXRpRp(reg_w.FA,reg__w.FA);
$09:_ADDRpRp(reg_w.HL,reg_w.BC,11);
$0A:_LDRegArp(reg.A,reg_w.BC);
$0B:_DECRp(reg_w.BC,6);
$0C:_INCReg(reg.C);
$0D:_DECReg(reg.C);
$0E:_LDRegN(reg.C);
$0F:_RRCA;
$10:_DJNZ;
$11:_LDRegNN(reg_w.DE,10);
$12:_LDArpReg(reg_w.DE,reg.A);
$13:_INCRp(reg_w.DE,6);
$14:_INCReg(reg.D);
$15:_DECReg(reg.D);
$16:_LDRegN(reg.D);
$17:_RLA;
$18:_JRS;
$19:_ADDRpRp(reg_w.HL,reg_w.DE,11);
$1A:_LDRegArp(reg.A,reg_w.DE);
$1B:_DECRp(reg_w.DE,6);
$1C:_INCReg(reg.E);
$1D:_DECReg(reg.E);
$1E:_LDRegN(reg.E);
$1F:_RRA;
$20:_JRSif(not(SetsZ));
$21:_LDRegNN(reg_w.HL,10);
$22:_LDAdrRp(reg_w.HL,16);
$23:_INCRp(reg_w.HL,6);
$24:_INCReg(reg.H);
$25:_DECReg(reg.H);
$26:_LDRegN(reg.H);
$27:_DAA;
$28:_JRSif(SetsZ);
$29:_ADDRpRp(reg_w.HL,reg_w.HL,11);
$2A:_LDRpAdr(reg_w.HL,16);
$2B:_DECRp(reg_w.HL,6);
$2C:_INCReg(reg.L);
$2D:_DECReg(reg.L);
$2E:_LDRegN(reg.L);
$2F:_CPL;
$30:_JRSif(not(SetsC));
$31:_LDRegNN(sp_reg.SP,10);
$32:_LDAdrReg(reg.A,13);
$33:_INCRp(sp_reg.SP,6);
$34:_INCArp(reg_w.HL);
$35:_DECArp(reg_w.HL);
$36:_LDArpN(reg_w.HL);
$37:_SCF;
$38:_JRSif(SetsC);
$39:_ADDRpRp(reg_w.HL,sp_reg.SP,11);
$3A:_LDRegAdr(reg.A,13);
$3B:_DECRp(sp_reg.SP,6);
$3C:_INCReg(reg.A);
$3D:_DECReg(reg.A);
$3E:_LDRegN(reg.A);
$3F:_CCF;
$40:_LDRegReg(reg.B,reg.B);
$41:_LDRegReg(reg.B,reg.C);
$42:_LDRegReg(reg.B,reg.D);
$43:_LDRegReg(reg.B,reg.E);
$44:_LDRegReg(reg.B,reg.H);
$45:_LDRegReg(reg.B,reg.L);
$46:_LDRegArp(reg.B,reg_w.HL);
$47:_LDRegReg(reg.B,reg.A);
$48:_LDRegReg(reg.C,reg.B);
$49:_LDRegReg(reg.C,reg.C);
$4A:_LDRegReg(reg.C,reg.D);
$4B:_LDRegReg(reg.C,reg.E);
$4C:_LDRegReg(reg.C,reg.H);
$4D:_LDRegReg(reg.C,reg.L);
$4E:_LDRegArp(reg.C,reg_w.HL);
$4F:_LDRegReg(reg.C,reg.A);
$50:_LDRegReg(reg.D,reg.B);
$51:_LDRegReg(reg.D,reg.C);
$52:_LDRegReg(reg.D,reg.D);
$53:_LDRegReg(reg.D,reg.E);
$54:_LDRegReg(reg.D,reg.H);
$55:_LDRegReg(reg.D,reg.L);
$56:_LDRegArp(reg.D,reg_w.HL);
$57:_LDRegReg(reg.D,reg.A);
$58:_LDRegReg(reg.E,reg.B);
$59:_LDRegReg(reg.E,reg.C);
$5A:_LDRegReg(reg.E,reg.D);
$5B:_LDRegReg(reg.E,reg.E);
$5C:_LDRegReg(reg.E,reg.H);
$5D:_LDRegReg(reg.E,reg.L);
$5E:_LDRegArp(reg.E,reg_w.HL);
$5F:_LDRegReg(reg.E,reg.A);
$60:_LDRegReg(reg.H,reg.B);
$61:_LDRegReg(reg.H,reg.C);
$62:_LDRegReg(reg.H,reg.D);
$63:_LDRegReg(reg.H,reg.E);
$64:_LDRegReg(reg.H,reg.H);
$65:_LDRegReg(reg.H,reg.L);
$66:_LDRegArp(reg.H,reg_w.HL);
$67:_LDRegReg(reg.H,reg.A);
$68:_LDRegReg(reg.L,reg.B);
$69:_LDRegReg(reg.L,reg.C);
$6A:_LDRegReg(reg.L,reg.D);
$6B:_LDRegReg(reg.L,reg.E);
$6C:_LDRegReg(reg.L,reg.H);
$6D:_LDRegReg(reg.L,reg.L);
$6E:_LDRegArp(reg.L,reg_w.HL);
$6F:_LDRegReg(reg.L,reg.A);
$70:_LDArpReg(reg_w.HL,reg.B);
$71:_LDArpReg(reg_w.HL,reg.C);
$72:_LDArpReg(reg_w.HL,reg.D);
$73:_LDArpReg(reg_w.HL,reg.E);
$74:_LDArpReg(reg_w.HL,reg.H);
$75:_LDArpReg(reg_w.HL,reg.L);
$76:_HALT;
$77:_LDArpReg(reg_w.HL,reg.A);
$78:_LDRegReg(reg.A,reg.B);
$79:_LDRegReg(reg.A,reg.C);
$7A:_LDRegReg(reg.A,reg.D);
$7B:_LDRegReg(reg.A,reg.E);
$7C:_LDRegReg(reg.A,reg.H);
$7D:_LDRegReg(reg.A,reg.L);
$7E:_LDRegArp(reg.A,reg_w.HL);
$7F:_LDRegReg(reg.A,reg.A);
$80:_ADDRegAReg(reg.B);
$81:_ADDRegAReg(reg.C);
$82:_ADDRegAReg(reg.D);
$83:_ADDRegAReg(reg.E);
$84:_ADDRegAReg(reg.H);
$85:_ADDRegAReg(reg.L);
$86:_ADDRegAArp(reg_w.HL);
$87:_ADDRegAReg(reg.A);
$88:_ADCRegAReg(reg.B);
$89:_ADCRegAReg(reg.C);
$8A:_ADCRegAReg(reg.D);
$8B:_ADCRegAReg(reg.E);
$8C:_ADCRegAReg(reg.H);
$8D:_ADCRegAReg(reg.L);
$8E:_ADCRegAArp(reg_w.HL);
$8F:_ADCRegAReg(reg.A);
$90:_SUBRegAReg(reg.B);
$91:_SUBRegAReg(reg.C);
$92:_SUBRegAReg(reg.D);
$93:_SUBRegAReg(reg.E);
$94:_SUBRegAReg(reg.H);
$95:_SUBRegAReg(reg.L);
$96:_SUBRegAArp(reg_w.HL);
$97:_SUBRegAReg(reg.A);
$98:_SBCRegAReg(reg.B);
$99:_SBCRegAReg(reg.C);
$9A:_SBCRegAReg(reg.D);
$9B:_SBCRegAReg(reg.E);
$9C:_SBCRegAReg(reg.H);
$9D:_SBCRegAReg(reg.L);
$9E:_SBCRegAArp(reg_w.HL);
$9F:_SBCRegAReg(reg.A);
$A0:_ANDRegAReg(reg.B);
$A1:_ANDRegAReg(reg.C);
$A2:_ANDRegAReg(reg.D);
$A3:_ANDRegAReg(reg.E);
$A4:_ANDRegAReg(reg.H);
$A5:_ANDRegAReg(reg.L);
$A6:_ANDRegAArp(reg_w.HL);
$A7:_ANDRegAReg(reg.A);
$A8:_XORRegAReg(reg.B);
$A9:_XORRegAReg(reg.C);
$AA:_XORRegAReg(reg.D);
$AB:_XORRegAReg(reg.E);
$AC:_XORRegAReg(reg.H);
$AD:_XORRegAReg(reg.L);
$AE:_XORRegAArp(reg_w.HL);
$AF:_XORRegAReg(reg.A);
$B0:_ORRegAReg(reg.B);
$B1:_ORRegAReg(reg.C);
$B2:_ORRegAReg(reg.D);
$B3:_ORRegAReg(reg.E);
$B4:_ORRegAReg(reg.H);
$B5:_ORRegAReg(reg.L);
$B6:_ORRegAArp(reg_w.HL);
$B7:_ORRegAReg(reg.A);
$B8:_CPRegAReg(reg.B);
$B9:_CPRegAReg(reg.C);
$BA:_CPRegAReg(reg.D);
$BB:_CPRegAReg(reg.E);
$BC:_CPRegAReg(reg.H);
$BD:_CPRegAReg(reg.L);
$BE:_CPRegAArp(reg_w.HL);
$BF:_CPRegAReg(reg.A);
$C0:_RETif(not(SetsZ));
$C1:_POP(reg_w.BC,10);
$C2:_JPNNif(not(SetsZ));
$C3:_JPNN;
$C4:_CALLif(not(SetsZ));
$C5:_PUSH(reg_w.BC,11);
$C6:_ADDRegAN;
$C7:_RST($00);
$C8:_RETif(SetsZ);
$C9:_RET;
$CA:_JPNNif(SetsZ);
$CB:begin{расширение команд}
     inc(sp_reg.PC);
     case memory^[sp_reg.PC]of
     $00:_RLCReg(reg.B);
     $01:_RLCReg(reg.C);
     $02:_RLCReg(reg.D);
     $03:_RLCReg(reg.E);
     $04:_RLCReg(reg.H);
     $05:_RLCReg(reg.L);
     $06:_RLCArp(reg_w.HL);
     $07:_RLCReg(reg.A);
     $08:_RRCReg(reg.B);
     $09:_RRCReg(reg.C);
     $0A:_RRCReg(reg.D);
     $0B:_RRCReg(reg.E);
     $0C:_RRCReg(reg.H);
     $0D:_RRCReg(reg.L);
     $0E:_RRCArp(reg_w.HL);
     $0F:_RRCReg(reg.A);
     $10:_RLReg(reg.B);
     $11:_RLReg(reg.C);
     $12:_RLReg(reg.D);
     $13:_RLReg(reg.E);
     $14:_RLReg(reg.H);
     $15:_RLReg(reg.L);
     $16:_RLArp(reg_w.HL);
     $17:_RLReg(reg.A);
     $18:_RRReg(reg.B);
     $19:_RRReg(reg.C);
     $1A:_RRReg(reg.D);
     $1B:_RRReg(reg.E);
     $1C:_RRReg(reg.H);
     $1D:_RRReg(reg.L);
     $1E:_RRArp(reg_w.HL);
     $1F:_RRReg(reg.A);
     $20:_SLAReg(reg.B);
     $21:_SLAReg(reg.C);
     $22:_SLAReg(reg.D);
     $23:_SLAReg(reg.E);
     $24:_SLAReg(reg.H);
     $25:_SLAReg(reg.L);
     $26:_SLAArp(reg_w.HL);
     $27:_SLAReg(reg.A);
     $28:_SRAReg(reg.B);
     $29:_SRAReg(reg.C);
     $2A:_SRAReg(reg.D);
     $2B:_SRAReg(reg.E);
     $2C:_SRAReg(reg.H);
     $2D:_SRAReg(reg.L);
     $2E:_SRAArp(reg_w.HL);
     $2F:_SRAReg(reg.A);
     $38:_SRLReg(reg.B);
     $39:_SRLReg(reg.C);
     $3A:_SRLReg(reg.D);
     $3B:_SRLReg(reg.E);
     $3C:_SRLReg(reg.H);
     $3D:_SRLReg(reg.L);
     $3E:_SRLArp(reg_w.HL);
     $3F:_SRLReg(reg.A);
     $40:_BIT(0,reg.B);
     $41:_BIT(0,reg.C);
     $42:_BIT(0,reg.D);
     $43:_BIT(0,reg.E);
     $44:_BIT(0,reg.H);
     $45:_BIT(0,reg.L);
     $46:_BIThl(0);
     $47:_BIT(0,reg.A);
     $48:_BIT(1,reg.B);
     $49:_BIT(1,reg.C);
     $4A:_BIT(1,reg.D);
     $4B:_BIT(1,reg.E);
     $4C:_BIT(1,reg.H);
     $4D:_BIT(1,reg.L);
     $4E:_BIThl(1);
     $4F:_BIT(1,reg.A);
     $50:_BIT(2,reg.B);
     $51:_BIT(2,reg.C);
     $52:_BIT(2,reg.D);
     $53:_BIT(2,reg.E);
     $54:_BIT(2,reg.H);
     $55:_BIT(2,reg.L);
     $56:_BIThl(2);
     $57:_BIT(2,reg.A);
     $58:_BIT(3,reg.B);
     $59:_BIT(3,reg.C);
     $5A:_BIT(3,reg.D);
     $5B:_BIT(3,reg.E);
     $5C:_BIT(3,reg.H);
     $5D:_BIT(3,reg.L);
     $5E:_BIThl(3);
     $5F:_BIT(3,reg.A);
     $60:_BIT(4,reg.B);
     $61:_BIT(4,reg.C);
     $62:_BIT(4,reg.D);
     $63:_BIT(4,reg.E);
     $64:_BIT(4,reg.H);
     $65:_BIT(4,reg.L);
     $66:_BIThl(4);
     $67:_BIT(4,reg.A);
     $68:_BIT(5,reg.B);
     $69:_BIT(5,reg.C);
     $6A:_BIT(5,reg.D);
     $6B:_BIT(5,reg.E);
     $6C:_BIT(5,reg.H);
     $6D:_BIT(5,reg.L);
     $6E:_BIThl(5);
     $6F:_BIT(5,reg.A);
     $70:_BIT(6,reg.B);
     $71:_BIT(6,reg.C);
     $72:_BIT(6,reg.D);
     $73:_BIT(6,reg.E);
     $74:_BIT(6,reg.H);
     $75:_BIT(6,reg.L);
     $76:_BIThl(6);
     $77:_BIT(6,reg.A);
     $78:_BIT(7,reg.B);
     $79:_BIT(7,reg.C);
     $7A:_BIT(7,reg.D);
     $7B:_BIT(7,reg.E);
     $7C:_BIT(7,reg.H);
     $7D:_BIT(7,reg.L);
     $7E:_BIThl(7);
     $7F:_BIT(7,reg.A);
     $80:_RES(0,reg.B);
     $81:_RES(0,reg.C);
     $82:_RES(0,reg.D);
     $83:_RES(0,reg.E);
     $84:_RES(0,reg.H);
     $85:_RES(0,reg.L);
     $86:_REShl(0);
     $87:_RES(0,reg.A);
     $88:_RES(1,reg.B);
     $89:_RES(1,reg.C);
     $8A:_RES(1,reg.D);
     $8B:_RES(1,reg.E);
     $8C:_RES(1,reg.H);
     $8D:_RES(1,reg.L);
     $8E:_REShl(1);
     $8F:_RES(1,reg.A);
     $90:_RES(2,reg.B);
     $91:_RES(2,reg.C);
     $92:_RES(2,reg.D);
     $93:_RES(2,reg.E);
     $94:_RES(2,reg.H);
     $95:_RES(2,reg.L);
     $96:_REShl(2);
     $97:_RES(2,reg.A);
     $98:_RES(3,reg.B);
     $99:_RES(3,reg.C);
     $9A:_RES(3,reg.D);
     $9B:_RES(3,reg.E);
     $9C:_RES(3,reg.H);
     $9D:_RES(3,reg.L);
     $9E:_REShl(3);
     $9F:_RES(3,reg.A);
     $A0:_RES(4,reg.B);
     $A1:_RES(4,reg.C);
     $A2:_RES(4,reg.D);
     $A3:_RES(4,reg.E);
     $A4:_RES(4,reg.H);
     $A5:_RES(4,reg.L);
     $A6:_REShl(4);
     $A7:_RES(4,reg.A);
     $A8:_RES(5,reg.B);
     $A9:_RES(5,reg.C);
     $AA:_RES(5,reg.D);
     $AB:_RES(5,reg.E);
     $AC:_RES(5,reg.H);
     $AD:_RES(5,reg.L);
     $AE:_REShl(5);
     $AF:_RES(5,reg.A);
     $B0:_RES(6,reg.B);
     $B1:_RES(6,reg.C);
     $B2:_RES(6,reg.D);
     $B3:_RES(6,reg.E);
     $B4:_RES(6,reg.H);
     $B5:_RES(6,reg.L);
     $B6:_REShl(6);
     $B7:_RES(6,reg.A);
     $B8:_RES(7,reg.B);
     $B9:_RES(7,reg.C);
     $BA:_RES(7,reg.D);
     $BB:_RES(7,reg.E);
     $BC:_RES(7,reg.H);
     $BD:_RES(7,reg.L);
     $BE:_REShl(7);
     $BF:_RES(7,reg.A);
     $C0:_SET(0,reg.B);
     $C1:_SET(0,reg.C);
     $C2:_SET(0,reg.D);
     $C3:_SET(0,reg.E);
     $C4:_SET(0,reg.H);
     $C5:_SET(0,reg.L);
     $C6:_SEThl(0);
     $C7:_SET(0,reg.A);
     $C8:_SET(1,reg.B);
     $C9:_SET(1,reg.C);
     $CA:_SET(1,reg.D);
     $CB:_SET(1,reg.E);
     $CC:_SET(1,reg.H);
     $CD:_SET(1,reg.L);
     $CE:_SEThl(1);
     $CF:_SET(1,reg.A);
     $D0:_SET(2,reg.B);
     $D1:_SET(2,reg.C);
     $D2:_SET(2,reg.D);
     $D3:_SET(2,reg.E);
     $D4:_SET(2,reg.H);
     $D5:_SET(2,reg.L);
     $D6:_SEThl(2);
     $D7:_SET(2,reg.A);
     $D8:_SET(3,reg.B);
     $D9:_SET(3,reg.C);
     $DA:_SET(3,reg.D);
     $DB:_SET(3,reg.E);
     $DC:_SET(3,reg.H);
     $DD:_SET(3,reg.L);
     $DE:_SEThl(3);
     $DF:_SET(3,reg.A);
     $E0:_SET(4,reg.B);
     $E1:_SET(4,reg.C);
     $E2:_SET(4,reg.D);
     $E3:_SET(4,reg.E);
     $E4:_SET(4,reg.H);
     $E5:_SET(4,reg.L);
     $E6:_SEThl(4);
     $E7:_SET(4,reg.A);
     $E8:_SET(5,reg.B);
     $E9:_SET(5,reg.C);
     $EA:_SET(5,reg.D);
     $EB:_SET(5,reg.E);
     $EC:_SET(5,reg.H);
     $ED:_SET(5,reg.L);
     $EE:_SEThl(5);
     $EF:_SET(5,reg.A);
     $F0:_SET(6,reg.B);
     $F1:_SET(6,reg.C);
     $F2:_SET(6,reg.D);
     $F3:_SET(6,reg.E);
     $F4:_SET(6,reg.H);
     $F5:_SET(6,reg.L);
     $F6:_SEThl(6);
     $F7:_SET(6,reg.A);
     $F8:_SET(7,reg.B);
     $F9:_SET(7,reg.C);
     $FA:_SET(7,reg.D);
     $FB:_SET(7,reg.E);
     $FC:_SET(7,reg.H);
     $FD:_SET(7,reg.L);
     $FE:_SEThl(7);
     $FF:_SET(7,reg.A);
     else
      begin
      dec(sp_reg.PC);
      err_command:=true;
      end;
     end;
    end;
$CC:_CALLif(SetsZ);
$CD:_CALL;
$CE:_ADCRegAN;
$CF:_RST($08);
$D0:_RETif(not(SetsC));
$D1:_POP(reg_w.DE,10);
$D2:_JPNNif(not(SetsC));
$D3:_OUTRegAN;
$D4:_CALLif(not(SetsC));
$D5:_PUSH(reg_w.DE,11);
$D6:_SUBRegAN;
$D7:_RST($10);
$D8:_RETif(SetsC);
$D9:_EXX;
$DA:_JPNNif(SetsC);
$DB:_INRegAN;
$DC:_CALLif(SetsC);
$DD:begin{IX вместо HL}
     inc(sp_reg.PC);
     case memory^[sp_reg.PC]of
     $09:_ADDRpRp(sp_reg.IX,reg_w.BC,15);
     $19:_ADDRpRp(sp_reg.IX,reg_w.DE,15);
     $21:_LDRegNN(sp_reg.IX,14);
     $22:_LDAdrRp(sp_reg.IX,20);
     $23:_INCRp(sp_reg.IX,10);
{+}  $24:_INCReg(sp_reg_b.IXi);
{+}  $25:_DECReg(sp_reg_b.IXi);
{+}  $26:_LDRegN(sp_reg_b.IXi);
     $29:_ADDRpRp(sp_reg.IX,sp_reg.IX,15);
     $2A:_LDRpAdr(sp_reg.IX,20);
     $2B:_DECRp(sp_reg.IX,10);
{+}  $2C:_INCReg(sp_reg_b.IXx);
{+}  $2D:_DECReg(sp_reg_b.IXx);
{+}  $2E:_LDRegN(sp_reg_b.IXx);
     $34:_INCAIdx(sp_reg.IX);
     $35:_DECAIdx(sp_reg.IX);
     $36:_LDIdxN(sp_reg.IX);
     $39:_ADDRpRp(sp_reg.IX,sp_reg.SP,15);
{+}  $44:_LDRegReg(reg.B,sp_reg_b.IXi);
{+}  $45:_LDRegReg(reg.B,sp_reg_b.IXx);
     $46:_LDRegIdx(reg.B,sp_reg.IX);
{+}  $4C:_LDRegReg(reg.C,sp_reg_b.IXi);
{+}  $4D:_LDRegReg(reg.C,sp_reg_b.IXx);
     $4E:_LDRegIdx(reg.C,sp_reg.IX);
{+}  $54:_LDRegReg(reg.D,sp_reg_b.IXi);
{+}  $55:_LDRegReg(reg.D,sp_reg_b.IXx);
     $56:_LDRegIdx(reg.D,sp_reg.IX);
{+}  $5C:_LDRegReg(reg.E,sp_reg_b.IXi);
{+}  $5D:_LDRegReg(reg.E,sp_reg_b.IXx);
     $5E:_LDRegIdx(reg.E,sp_reg.IX);
{+}  $60:_LDRegReg(sp_reg_b.IXi,reg.B);
{+}  $61:_LDRegReg(sp_reg_b.IXi,reg.C);
{+}  $62:_LDRegReg(sp_reg_b.IXi,reg.D);
{+}  $63:_LDRegReg(sp_reg_b.IXi,reg.E);
{+}  $64:_LDRegReg(sp_reg_b.IXi,sp_reg_b.IXi);
{+}  $65:_LDRegReg(sp_reg_b.IXi,sp_reg_b.IXx);
     $66:_LDRegIdx(reg.H,sp_reg.IX);
{+}  $67:_LDRegReg(sp_reg_b.IXi,reg.A);
{+}  $68:_LDRegReg(sp_reg_b.IXx,reg.B);
{+}  $69:_LDRegReg(sp_reg_b.IXx,reg.C);
{+}  $6A:_LDRegReg(sp_reg_b.IXx,reg.D);
{+}  $6B:_LDRegReg(sp_reg_b.IXx,reg.E);
{+}  $6C:_LDRegReg(sp_reg_b.IXx,sp_reg_b.IXi);
{+}  $6D:_LDRegReg(sp_reg_b.IXx,sp_reg_b.IXx);
     $6E:_LDRegIdx(reg.L,sp_reg.IX);
{+}  $6F:_LDRegReg(sp_reg_b.IXx,reg.A);
     $70:_LDIdxReg(sp_reg.IX,reg.B);
     $71:_LDIdxReg(sp_reg.IX,reg.C);
     $72:_LDIdxReg(sp_reg.IX,reg.D);
     $73:_LDIdxReg(sp_reg.IX,reg.E);
     $74:_LDIdxReg(sp_reg.IX,reg.H);
     $75:_LDIdxReg(sp_reg.IX,reg.L);
     $77:_LDIdxReg(sp_reg.IX,reg.A);
{+}  $7C:_LDRegReg(reg.A,sp_reg_b.IXi);
{+}  $7D:_LDRegReg(reg.A,sp_reg_b.IXx);
     $7E:_LDRegIdx(reg.A,sp_reg.IX);
{+}  $84:_ADDRegAReg(sp_reg_b.IXi);
{+}  $85:_ADDRegAReg(sp_reg_b.IXx);
     $86:_ADDRegAIdx(sp_reg.IX);
{+}  $8C:_ADCRegAReg(sp_reg_b.IXi);
{+}  $8D:_ADCRegAReg(sp_reg_b.IXx);
     $8E:_ADCRegAIdx(sp_reg.IX);
{+}  $94:_SUBRegAReg(sp_reg_b.IXi);
{+}  $95:_SUBRegAReg(sp_reg_b.IXx);
     $96:_SUBRegAIdx(sp_reg.IX);
{+}  $9C:_SBCRegAReg(sp_reg_b.IXi);
{+}  $9D:_SBCRegAReg(sp_reg_b.IXx);
     $9E:_SBCRegAIdx(sp_reg.IX);
{+}  $A4:_ANDRegAReg(sp_reg_b.IXi);
{+}  $A5:_ANDRegAReg(sp_reg_b.IXx);
     $A6:_ANDRegAIdx(sp_reg.IX);
{+}  $AC:_XORRegAReg(sp_reg_b.IXi);
{+}  $AD:_XORRegAReg(sp_reg_b.IXx);
     $AE:_XORRegAIdx(sp_reg.IX);
{+}  $B4:_ORRegAReg(sp_reg_b.IXi);
{+}  $B5:_ORRegAReg(sp_reg_b.IXx);
     $B6:_ORRegAIdx(sp_reg.IX);
{+}  $BC:_CPRegAReg(sp_reg_b.IXi);
{+}  $BD:_CPRegAReg(sp_reg_b.IXx);
     $BE:_CPRegAIdx(sp_reg.IX);
     $CB:begin
          inc(sp_reg.PC,2);
          case memory^[sp_reg.PC] of
          $06:_RLCIdx(sp_reg.IX);
          $0E:_RRCIdx(sp_reg.IX);
          $16:_RLIdx(sp_reg.IX);
          $1E:_RRIdx(sp_reg.IX);
          $26:_SLAIdx(sp_reg.IX);
          $2E:_SRAIdx(sp_reg.IX);
          $3E:_SRLIdx(sp_reg.IX);
          $46:_BITix(0,sp_reg.IX);
          $4E:_BITix(1,sp_reg.IX);
          $56:_BITix(2,sp_reg.IX);
          $5E:_BITix(3,sp_reg.IX);
          $66:_BITix(4,sp_reg.IX);
          $6E:_BITix(5,sp_reg.IX);
          $76:_BITix(6,sp_reg.IX);
          $7E:_BITix(7,sp_reg.IX);
          $86:_RESix(0,sp_reg.IX);
          $8E:_RESix(1,sp_reg.IX);
          $96:_RESix(2,sp_reg.IX);
          $9E:_RESix(3,sp_reg.IX);
          $A6:_RESix(4,sp_reg.IX);
          $AE:_RESix(5,sp_reg.IX);
          $B6:_RESix(6,sp_reg.IX);
          $BE:_RESix(7,sp_reg.IX);
          $C6:_SETix(0,sp_reg.IX);
          $CE:_SETix(1,sp_reg.IX);
          $D6:_SETix(2,sp_reg.IX);
          $DE:_SETix(3,sp_reg.IX);
          $E6:_SETix(4,sp_reg.IX);
          $EE:_SETix(5,sp_reg.IX);
          $F6:_SETix(6,sp_reg.IX);
          $FE:_SETix(7,sp_reg.IX);
          else
           begin
           dec(sp_reg.PC,2);
           err_command:=true;
           end;
          end;
         end;
     $E1:_POP(sp_reg.IX,14);
     $E3:_EXsp(sp_reg.IX,23);
     $E5:_PUSH(sp_reg.IX,15);
     $E9:_JPArp(sp_reg.IX,8);
     $F9:_LDRpRp(sp_reg.SP,sp_reg.IX,10);
     else
      begin
      dec(sp_reg.PC);
      err_command:=true;
      end;
     end;
    end;
$DE:_SBCRegAN;
$DF:_RST($18);
$E0:_RETif(not(SetsPV));
$E1:_POP(reg_w.HL,10);
$E2:_JPNNif(not(SetsPV));
$E3:_EXsp(reg_w.HL,19);
$E4:_CALLif(not(SetsPV));
$E5:_PUSH(reg_w.HL,11);
$E6:_ANDRegAN;
$E7:_RST($20);
$E8:_RETif(SetsPV);
$E9:_JPArp(reg_w.HL,4);
$EA:_JPNNif(SetsPV);
$EB:_EXRpRp(reg_w.DE,reg_w.HL);
$EC:_CALLif(SetsPV);
$ED:begin{расширение команд}
     inc(sp_reg.PC);
     case memory^[sp_reg.PC]of
     $40:_INRegC(reg.B);
     $41:_OUTRegC(reg.B);
     $42:_SBCHlRp(reg_w.BC);
     $43:_LDAdrRp(reg_w.BC,20);
     $44:_NEG;
     $45:_RetN;
     $46:_IMode(0);
     $47:_LDtoIR(sp_reg.I);
     $48:_INRegC(reg.C);
     $49:_OUTRegC(reg.C);
     $4A:_ADCHlRp(reg_w.BC);
     $4B:_LDRpAdr(reg_w.BC,20);
     $4D:_RetI;
     $4F:_LDtoIR(sp_reg.R);
     $50:_INRegC(reg.D);
     $51:_OUTRegC(reg.D);
     $52:_SBCHlRp(reg_w.DE);
     $53:_LDAdrRp(reg_w.DE,20);
     $56:_IMode(1);
     $57:_LDfromIR(sp_reg.I);
     $58:_INRegC(reg.E);
     $59:_OUTRegC(reg.E);
     $5A:_ADCHlRp(reg_w.DE);
     $5B:_LDRpAdr(reg_w.DE,20);
     $5E:_IMode(2);
     $5F:_LDfromIR(sp_reg.R);
     $60:_INRegC(reg.H);
     $61:_OUTRegC(reg.H);
     $62:_SBCHlRp(reg_w.HL);
     $63:_LDAdrRp(reg_w.HL,20);
     $67:_RRD;
     $68:_INRegC(reg.L);
     $69:_OUTRegC(reg.L);
     $6A:_ADCHlRp(reg_w.HL);
     $6B:_LDRpAdr(reg_w.HL,20);
     $6F:_RLD;
     $70:_INRegC(reg.F);
     $72:_SBCHlRp(sp_reg.SP);
     $73:_LDAdrRp(sp_reg.SP,20);
     $78:_INRegC(reg.A);
     $79:_OUTRegC(reg.A);
     $7A:_ADCHlRp(sp_reg.SP);
     $7B:_LDRpAdr(sp_reg.SP,20);
     $A0:_LDI;
     $A1:_CPI;
     $A2:_INI;
     $A3:_OUTI;
     $A8:_LDD;
     $A9:_CPD;
     $B0:_LDIR;
     $B1:_CPIR;
     $B8:_LDDR;
     $B9:_CPDR;
     else
      begin
      dec(sp_reg.PC);
      err_command:=true;
      end;
     end;
    end;
$EE:_XORRegAN;
$EF:_RST($28);
$F0:_RETif(not(SetsS));
$F1:_POP(reg_w.FA,10);
$F2:_JPNNif(not(SetsS));
$F3:_DI;
$F4:_CALLif(not(SetsS));
$F5:_PUSH(reg_w.FA,11);
$F6:_ORRegAN;
$F7:_RST($30);
$F8:_RETif(SetsS);
$F9:_LDRpRp(sp_reg.SP,reg_w.HL,6);
$FA:_JPNNif(SetsS);
$FB:_EI;
$FC:_CALLif(SetsS);
$FD:begin{операции с IY}
     inc(sp_reg.PC);
     case memory^[sp_reg.PC]of
   $02:_LDArpReg(reg_w.BC,reg.A);{для MUGSY}
     $09:_ADDRpRp(sp_reg.IY,reg_w.BC,15);
     $19:_ADDRpRp(sp_reg.IY,reg_w.DE,15);
     $21:_LDRegNN(sp_reg.IY,14);
     $22:_LDAdrRp(sp_reg.IY,20);
     $23:_INCRp(sp_reg.IY,10);
{+}  $24:_INCReg(sp_reg_b.IYi);
{+}  $25:_DECReg(sp_reg_b.IYi);
{+}  $26:_LDRegN(sp_reg_b.IYi);
     $29:_ADDRpRp(sp_reg.IY,sp_reg.IY,15);
     $2A:_LDRpAdr(sp_reg.IY,20);
     $2B:_DECRp(sp_reg.IY,10);
{+}  $2C:_INCReg(sp_reg_b.IYy);
{+}  $2D:_DECReg(sp_reg_b.IYy);
{+}  $2E:_LDRegN(sp_reg_b.IYy);
     $34:_INCAIdx(sp_reg.IY);
     $35:_DECAIdx(sp_reg.IY);
     $36:_LDIdxN(sp_reg.IY);
     $39:_ADDRpRp(sp_reg.IY,sp_reg.SP,15);
{+}  $44:_LDRegReg(reg.B,sp_reg_b.IYi);
{+}  $45:_LDRegReg(reg.B,sp_reg_b.IYy);
     $46:_LDRegIdx(reg.B,sp_reg.IY);
{+}  $4C:_LDRegReg(reg.C,sp_reg_b.IYi);
{+}  $4D:_LDRegReg(reg.C,sp_reg_b.IYy);
     $4E:_LDRegIdx(reg.C,sp_reg.IY);
{+}  $54:_LDRegReg(reg.D,sp_reg_b.IYi);
{+}  $55:_LDRegReg(reg.D,sp_reg_b.IYy);
     $56:_LDRegIdx(reg.D,sp_reg.IY);
{+}  $5C:_LDRegReg(reg.E,sp_reg_b.IYi);
{+}  $5D:_LDRegReg(reg.E,sp_reg_b.IYy);
     $5E:_LDRegIdx(reg.E,sp_reg.IY);
{+}  $60:_LDRegReg(sp_reg_b.IYi,reg.B);
{+}  $61:_LDRegReg(sp_reg_b.IYi,reg.C);
{+}  $62:_LDRegReg(sp_reg_b.IYi,reg.D);
{+}  $63:_LDRegReg(sp_reg_b.IYi,reg.E);
{+}  $64:_LDRegReg(sp_reg_b.IYi,sp_reg_b.IYi);
{+}  $65:_LDRegReg(sp_reg_b.IYi,sp_reg_b.IYy);
     $66:_LDRegIdx(reg.H,sp_reg.IY);
{+}  $67:_LDRegReg(sp_reg_b.IYi,reg.A);
{+}  $68:_LDRegReg(sp_reg_b.IYy,reg.B);
{+}  $69:_LDRegReg(sp_reg_b.IYy,reg.C);
{+}  $6A:_LDRegReg(sp_reg_b.IYy,reg.D);
{+}  $6B:_LDRegReg(sp_reg_b.IYy,reg.E);
{+}  $6C:_LDRegReg(sp_reg_b.IYy,sp_reg_b.IYi);
{+}  $6D:_LDRegReg(sp_reg_b.IYy,sp_reg_b.IYy);
     $6E:_LDRegIdx(reg.L,sp_reg.IY);
{+}  $6F:_LDRegReg(sp_reg_b.IYy,reg.A);
     $70:_LDIdxReg(sp_reg.IY,reg.B);
     $71:_LDIdxReg(sp_reg.IY,reg.C);
     $72:_LDIdxReg(sp_reg.IY,reg.D);
     $73:_LDIdxReg(sp_reg.IY,reg.E);
     $74:_LDIdxReg(sp_reg.IY,reg.H);
     $75:_LDIdxReg(sp_reg.IY,reg.L);
     $77:_LDIdxReg(sp_reg.IY,reg.A);
{+}  $7C:_LDRegReg(reg.A,sp_reg_b.IYi);
{+}  $7D:_LDRegReg(reg.A,sp_reg_b.IYy);
     $7E:_LDRegIdx(reg.A,sp_reg.IY);
{+}  $84:_ADDRegAReg(sp_reg_b.IYi);
{+}  $85:_ADDRegAReg(sp_reg_b.IYy);
     $86:_ADDRegAIdx(sp_reg.IY);
{+}  $8C:_ADCRegAReg(sp_reg_b.IYi);
{+}  $8D:_ADCRegAReg(sp_reg_b.IYy);
     $8E:_ADCRegAIdx(sp_reg.IY);
{+}  $94:_SUBRegAReg(sp_reg_b.IYi);
{+}  $95:_SUBRegAReg(sp_reg_b.IYy);
     $96:_SUBRegAIdx(sp_reg.IY);
{+}  $9C:_SBCRegAReg(sp_reg_b.IYi);
{+}  $9D:_SBCRegAReg(sp_reg_b.IYy);
     $9E:_SBCRegAIdx(sp_reg.IY);
{+}  $A4:_ANDRegAReg(sp_reg_b.IYi);
{+}  $A5:_ANDRegAReg(sp_reg_b.IYy);
     $A6:_ANDRegAIdx(sp_reg.IY);
{+}  $AC:_XORRegAReg(sp_reg_b.IYi);
{+}  $AD:_XORRegAReg(sp_reg_b.IYy);
     $AE:_XORRegAIdx(sp_reg.IY);
{+}  $B4:_ORRegAReg(sp_reg_b.IYi);
{+}  $B5:_ORRegAReg(sp_reg_b.IYy);
     $B6:_ORRegAIdx(sp_reg.IY);
{+}  $BC:_CPRegAReg(sp_reg_b.IYi);
{+}  $BD:_CPRegAReg(sp_reg_b.IYy);
     $BE:_CPRegAIdx(sp_reg.IY);
     $CB:begin
          inc(sp_reg.PC,2);
          case memory^[sp_reg.PC] of
          $06:_RLCIdx(sp_reg.IY);
          $0E:_RRCIdx(sp_reg.IY);
          $16:_RLIdx(sp_reg.IY);
          $1E:_RRIdx(sp_reg.IY);
          $26:_SLAIdx(sp_reg.IY);
          $2E:_SRAIdx(sp_reg.IY);
          $3E:_SRLIdx(sp_reg.IY);
          $46:_BITix(0,sp_reg.IY);
          $4E:_BITix(1,sp_reg.IY);
          $56:_BITix(2,sp_reg.IY);
          $5E:_BITix(3,sp_reg.IY);
          $66:_BITix(4,sp_reg.IY);
          $6E:_BITix(5,sp_reg.IY);
          $76:_BITix(6,sp_reg.IY);
          $7E:_BITix(7,sp_reg.IY);
          $86:_RESix(0,sp_reg.IY);
          $8E:_RESix(1,sp_reg.IY);
          $96:_RESix(2,sp_reg.IY);
          $9E:_RESix(3,sp_reg.IY);
          $A6:_RESix(4,sp_reg.IY);
          $AE:_RESix(5,sp_reg.IY);
          $B6:_RESix(6,sp_reg.IY);
          $BE:_RESix(7,sp_reg.IY);
          $C6:_SETix(0,sp_reg.IY);
          $CE:_SETix(1,sp_reg.IY);
          $D6:_SETix(2,sp_reg.IY);
          $DE:_SETix(3,sp_reg.IY);
          $E6:_SETix(4,sp_reg.IY);
          $EE:_SETix(5,sp_reg.IY);
          $F6:_SETix(6,sp_reg.IY);
          $FE:_SETix(7,sp_reg.IY);
          else
           begin
           dec(sp_reg.PC,2);
           err_command:=true;
           end;
          end;
         end;
     $E1:_POP(sp_reg.IY,14);
     $E3:_EXsp(sp_reg.IY,23);
     $E5:_PUSH(sp_reg.IY,15);
     $E9:_JPArp(sp_reg.IY,8);
     $F9:_LDRpRp(sp_reg.SP,sp_reg.IY,10);
     else
      begin
      dec(sp_reg.PC);
      err_command:=true;
      end;
     end;
    end;
$FE:_CPRegAN;
$FF:_RST($38);
else err_command:=true;
end;

if err_command then
begin
asm nop end
end;

if TheDosMode then
if sp_reg.pc>=16384 then setSos;

if not(TheDosMode) then
if (sp_reg.pc>=$3D00)and(sp_reg.pc<=$3DFF)then setDos;
{if (sp_reg.pc>=15616)and(sp_reg.pc<=15871) then
begin
if sp_reg.pc=15635 then
 case reg.C of
 $0A:begin
     reg.C:=255;
     write_b(23823,255);
     end;
 end;
_ret;
end;}
{if mem_b(23560)>129 then write_b(23560,mem_b(23560)-129+31);
if mem_b(23560)<>oldMemB then
begin
oldMemB:=mem_b(23560);
bar(0,0,30,10);
setcolor(15);
str(mem_b(23560),st);
outtextxy(0,0,st);
end;}
{if not(MaskInt)then
 begin
 write_b(23659,2);
 write_b(23660,24);
 end;!!!}
until (pressExit)or((sp_reg.PC=addr_trace)and(not(RunMode)));
(*asm
{|end instruction to test}
{| CLC}clc
{| RDTSC}db $0F,$31
{| SUB EAX,[TICS]}db $66;sub ax,tics_w
{| SUB EAX,OverHead=16}db $66,$83,$E8,16
{| MOV [TICS],EAX}db $66;mov tics_w,ax
{|BreakPoint}nop
end;*)
pressExit:=false;
end;
(****************************************************************************)
(****************************************************************************)
(****************************************************************************)
var a,b,r,f:byte;
fl:text;
_s:string;
var s:string;
const HX='0123456789ABCDEF';
function writeB(_dt:byte):string;
begin
s:=HX;
writeB:='$'+s[(_dt shr 4)+1]+s[(_dt and $0F)+1];
end;
function writeF(b:byte):string;
var s:string;
begin
s:='';
if (b and $80)=0 then s:=s+'s' else s:=s+'S';
if (b and $40)=0 then s:=s+'z' else s:=s+'Z';
if (b and $20)=0 then s:=s+'0' else s:=s+'1';
if (b and $10)=0 then s:=s+'h' else s:=s+'H';
if (b and $08)=0 then s:=s+'0' else s:=s+'1';
if (b and $04)=0 then s:=s+'v' else s:=s+'V';
if (b and $02)=0 then s:=s+'n' else s:=s+'N';
if (b and $01)=0 then s:=s+'c' else s:=s+'C';
writeF:=s;
end;
var i:integer;
const damp:array[0..15]of byte=
($DD,$CB,$60,$C6,$2a,$00,$00,$01,$00,$58,$3E,$F8,$02,$C3,$00,$60);
procedure Init;
begin
pressHelp:=false;
pressExit:=false;
RunMode:=false;
DebugMode:=false;
err_command:=false;
writeln('My Spectrum ...');
get_rom;
Quarz:=50{Hz};
MaxClock:=round(4e6/Quarz);{4Мгц}
set_interrupt;
set_keyboard;
NonMaskReady:=false;
IM:=1;
{
_DI;
s:=HX;
assign(fl,'c:\my_sbcf2.dat');
rewrite(fl);
reg.F:=$FF;
for a:=0 to $FF do
 for b:=0 to $FF do
  begin
reg.A:=b;
reg.C:=b;
reg.B:=a;
reg_w.HL:=2;
_SBCHlRp(reg_w.BC);
r:=reg.A;
_s:='A:=';
_s:=_s+writeB(a);
_s:=_s+' B:=';
_s:=_s+writeB(b);
_s:=_s+' R:=';
_s:=_s+writeB(r);
_s:=_s+' F:=';
_s:=_s+writeF(reg.f);
writeln(fl,_s);
  end;

reset_interrupt;
dispose(memory);
halt;
 }

(*for i:=23232 to 23263 do memory^[i]:=0;{mugsy}
sp_reg.PC:={ast31190{zubex24500 mugsy}24576{oc54820};
sp_reg.SP:=24499;
{zubex reg_w.HL:=28160;
push(reg_w.HL);
{for i:=24576 to 25000 do
 memory^[i]:=damp[i-24576];
 }
{sp_reg.IY:=$5C3A;}
*)
sp_reg.PC:=$0000;
end;
(****************************************************************************)
(****************************************************************************)
procedure CloseGraph;
begin
Graph.CloseGraph;
end;
(****************************************************************************)
procedure Done;
begin
reset_interrupt;
dispose(dosRom);
dispose(sosRom);
dispose(memory);
closegraph;
end;
end.