% 	PENTAGON-1024SL ver 2.2

	*** Main CPLD design ***

	Sinclair Research Ltd, 1981-1986
	ATM 1991
	Alone Coder 2005-2006
	King Of Evil 2004-2006

%
Title "P1024SL";
Subdesign P1024SL

(

d[7..0]: bidir;


f14, mreq, iorq, rfsh, rd, port1, port2,
a14, a15, ram[7..0], res, dos : input;		



sync, r, g, b, bright, av[15..0], cas, ras, we, hl,
clk, wait, busrq, int, x2, csos, mem[3..0], y0, y1: output;
)

Variable

dbus[7..0]:tri;

trig[6..0], count1[3..0], count2[3..0], count3[3..0],
count4[3..0], countstr, bordtrg, inttrg, penttrg1, penttrg2,
memtrg, porteff7[4..0], port7ffd[7..0], ramq[7..0], 
ramo[7..0], raml[7..0], shift[7..0], portfe[3..0], flash[4..0],brgout[3..0],
delay, portfetr, hbi_ff:dff;

f7, f35, z, of7, ocas, oras, turbo, x18, stba, stbv,
var1, var2, var3, L, 4trb, perenos1, perenos2, ssi, k,
x2, x3, c3, bordi, bordo, x9, x17, 16col, 384av14,
x15, x16, ksi, x1516, bord, dsp, rf1, 1a, ptg,
v, stb,  ef0, gonki, 7ffd, eff7, wrfe,
y2, 128k, romram, 384x304, rubilnik, zero, x10, x11,
q[7..0], o[7..0], ll[7..0], stba1, c31, vms1, vms,
vvms, br2, flsh, br, bg, bb, 16av13, 16av14, vb, vr,
vg, brg, ob, orr, og, obrg, a1415, portwait,
rd7ffd, rdeff7, old_ksi, new_ksi, bl_ssi :node;

Begin

% Формируем основные синхросигналы (в точности как в пентагон-128, ничего нового) %

trig[0].clk=f14;
of7=trig[0].q;
f7=not(of7);
trig[0].d=f7;

trig[1].clk=(f7);
z=trig[1].q;
f35=not(z);
trig[1].d=f35;

var1=not(f14);
trig[2].clk=var1; % Пентагон, все таки :) %
trig[2].d=f35;
oras=trig[2].q; ras=not(oras);
trig[3].clk=var1; trig[3].d=oras;
ocas=trig[3].q; cas=not(ocas);
%trbtrg.d=not(turbo or (dos and not(romram)));

trbtrg.clk=not(rfsh);
%

%trig[4].d=(turbo or (dos and not(rfsh)));
%
trig[4].d=(turbo or dos);


% В ДОСе турубу выключаем, но не сразу, а дождавшись завершения тактов ожидания %
trig[4].clk=not(rfsh); %of7%
 var2=trig[4].q;

% WAIT будет сформирован при обращении в память вне ПЗУ или
при обращении в 0-ю страницу, если там включено ОЗУ вместо ПЗУ %



l=(mreq nor ((a14 or a15) nor romram));
4trb=hl nand ocas; 

trig[5].clrn=(4trb and res);
trig[5].clk=l; trig[5].d=RFSH;

we=(1a or (rd nand l));

% удлиняем IORQ с помощью WAIT в турбе %

delay.clk = clk;
delay.d=iorq or not(delay.q);
portwait = delay.q;

var3=not(trig[5].q);

IF  var2 	THEN wait=vcc; trig[6].d=f35;
			ELSE wait=var3 and portwait; trig[6].d=of7;
END IF;

trig[6].clk = f14;
clk = trig[6].q;

count1[].clk=z; 
count1[].d=count1[].q+1;
perenos1 = count1[0].q and count1[1].q and count1[2].q and count1[3].q;

perenos2 = count2[0].q and count2[1].q and count2[2].q and count2[3].q;
count2[].clk=(not perenos2) xor perenos1;
if perenos2 then 
count2[].d=1;
else
count2[].d=count2[].q+1;
end if;

ssi=not(count2[0].q or count2[1].q or count2[3].q);

countstr.clk=ssi; x18=not(countstr.q); countstr.d=x18;
count3[].clk=countstr.q; count3[].d=count3[].q+1;

av0=count1[2].q; av1=count1[3].q; av2=count2[0].q; av3=count2[1].q;
av4=count2[2].q; av5=count3[2].q; av6=count3[3].q; av7=count4[0].q;
x10=count3[0].q; x11=count3[1].q;

count4[].clk=not(AV6);
if count4[].q==B"101x" then 
count4[].clrn=count4[3].q nand count4[1].q;
else count4[].clrn=vcc; count4[].d=count4[].q+1;
end if;

x9=count2[3].q; x15=count4[1].q; x16=count4[2].q; x17=count4[3].q;
x1516=x15 and x16; old_ksi=av6 and av7 and x1516; 
inttrg.clk=old_ksi; inttrg.d=gnd; inttrg.prn=((not av2) nand av3);
int=inttrg.q;
384av14=x1516 nor x17; bordi=384av14 nand x9;
bordtrg.clk=av0; bordtrg.d=bordo;
bord=(bordtrg.q or (bordo and 16col));

-- -----------------

hbi_ff.d=gnd; hbi_ff.clk=gnd; 
hbi_ff.prn=not(av0 and av1 and av2 and av3 and (not av4) and (not x9)); 
hbi_ff.clrn=not(av0 and (not av1) and (not av2) and av3 and (not x9)); 
new_ksi=(not x11) and (not av5) and av6 and av7 and x1516; 
bl_ssi=not((not av2) or (not av3) or av4 or x9); 

-- -----------------

memtrg.d=not(bordo); memtrg.clk=hl; memtrg.clrn=x2 nand x3;
rf1=not(memtrg.q); 
hl=penttrg1.q; 1a=not(hl);
penttrg1.clk=cas; penttrg1.clrn=rfsh;
%penttrg1.prn = trig[9].q;%
penttrg1.d=1a and l and rf1;
penttrg2.clk=cas; ptg=penttrg2.q;
penttrg2.d=1a xor ptg; 
v=(not (ptg) or 16col); stb=oras and 1a;
stba=((ef0 and ptg) nand stb);
stbv=v nand stb; 
av12=(v nand (not (x16)));


% "Дешифрация" шифрованного сигнала обращения к портам
это все от бедноты... %

gonki=clk or iorq; 


-- Запись в 7ффд
if port1==vcc and port2==vcc and gonki==gnd and iorq==0 and rd==1 then 7ffd=gnd;
else 7ffd=vcc;
end if;


-- Запись в ефф7
if port1==gnd and port2==gnd and gonki==gnd and iorq==0 and rd==1 then eff7=vcc;
else eff7=gnd;
end if;



-- чтение из 7ффд
rd7ffd = not(portfe[3].q) or A15 or A14 or iorq or rd; -- or not(port1 or not(port2))); -- port1 or not(port2)
rdeff7 = not(portfe[3].q) or not(A15) or not(A15) or iorq or rd;

portfetr.clk = rd7ffd and rdeff7;
portfetr.d = gnd;
portfetr.prn = not(clk);
portfe[3].clrn = portfetr.q;


if not(rd7ffd) then dbus[].in=port7ffd[].q; 
else if not(rdeff7) then 

dbus[0].in=porteff7[0].q;
dbus[2].in=porteff7[1].q;
dbus[3].in=porteff7[2].q;
dbus[4].in=porteff7[3].q;
dbus[6].in=porteff7[4].q;

end if; end if;


dbus[].oe= not(rd7ffd) or not(rdeff7);-- or rde; --ffd or rdeff7;
d[]=dbus[].out;


if port1==gnd and port2==vcc then wrfe=vcc;
else wrfe=gnd;
end if;

porteff7[].clrn=res; porteff7[].clk=eff7;
porteff7[0].d=d0; 16col=porteff7[0].q;
porteff7[1].d=d2; 128k=porteff7[1].q;
porteff7[2].d=d3; romram=porteff7[2].q;
porteff7[3].d=d4; turbo=porteff7[3].q;
porteff7[4].d=d6; 384x304=porteff7[4].q;

port7ffd[].clrn=res; port7ffd[].d=d[]; rubilnik=port7ffd[5].q;
port7ffd[].clk=((rubilnik and 128k) nor 7ffd);--ot(7ffdtrg.q));

% ----------- %

av15=port7ffd[3].q; csos=port7ffd[4].q; 

if a14==gnd or a15==gnd or 128k==vcc then zero=gnd;
else zero=vcc;
end if;

mem[0]=rubilnik and zero;
mem[2]=port7ffd[6].q and zero;
mem[3]=port7ffd[7].q and zero;
mem[1]=1a nor y2;

a1415=a14 nand a15;
if a1415 then y0=a14; y1=a15; y2=a14;
else y0=port7ffd[0].q; y1=port7ffd[1].q; y2=port7ffd[2].q;
end if;

portfe[2..0].clrn=vcc; portfe[].clk=wrfe; portfe[2..0].d=d[2..0];
bg=portfe[2].q; br=portfe[1].q; bb=portfe[0].q;
portfe[3].d=d[7];

x2=count1[0].q; x3=count1[1].q; c3=not(x3);
k=x2 and x3 and f35; 

flash[].clk=old_ksi; 
flash[].d=flash[].q+1;
flsh=flash[4].q;

dsp=old_ksi nor hbi_ff.q; 

if  v 	then 	av8=x18;
				av9=x10;
				av10=x11;
				av11=x15;
		else	av8=x15;
				av9=x16;
				av10=gnd;
				av11=384av14;
end if;

ramq[].d=ram[]; q[]=ramq[].q;
ramq[].clk=stbv; % защелкнули раз %

ramo[].d=ram[]; o[]=ramo[].q;
ramo[].clk=stba1; % защелкнули два %

raml[].d=o[]; ll[]=raml[].q;
raml[].clk=c31; % защелкнули три %

if 16col then vms1=f35; br2=ll[7]; stba1=oras; c31=z;
else vms1=vms; br2=ll6; stba1=stba; c31=c3;
end if;
16av13=16col and x3; ef0=not(16col); 16av14=ef0 or x2;
busrq=ef0 or bordo;

if 384x304 then bordo=gnd; av13=x9; av14=384av14;
else bordo=bordi; av13=16av13; av14=16av14;
end if;

shift[].clk=f7;
if k then shift[].d=q[];
else shift[7..1].d=shift[6..0].q; shift[0].d=gnd;
end if;
vvms=shift[7].q;
vms=((flsh nand ll7) xor vvms);

if vms1 then vb=ll3; vr=ll4; vg=ll5; brg=br2;
else vb=ll0; vr=ll1; vg=ll2; brg=ll6;
end if;

if bord then ob=bb; orr=br; og=bg; obrg=gnd;
else ob=vb; orr=vr; og=vg; obrg=brg;
end if;

brgout[].clrn=dsp; brgout[].clk=f7;
brgout[0].d=ob;  b=brgout[0].q;
brgout[1].d=orr; r=brgout[1].q;
brgout[2].d=og;  g=brgout[2].q;
brgout[3].d=obrg; bright=brgout[3].q;

sync=bl_ssi or (ssi nor new_ksi); 

end;
