{Program for control of 3-wire software control port of DF1704}

Program three_wire_DF1704;

const ML = $90;{latch}
      MC = $91;{clock}
      MD = $92;{data}
      M0 = $0100;{address left, mode0}
      M1 = $0300;{address right, mode1}
      M2 = $0469;{address and data mode2}
      M3 = $0601;{address and data mode3}

var controls, left, right, ldata, rdata : integer;
    preset : byte;

Procedure send (data : integer);
var i,j : integer;
begin
 setbit (MC);{clock high}
 setbit (ML);{latch high}
 for i := 0 to 15 do
  begin
  invertbit (MC);{clock low}
  if data < 0 then setbit (MD) else clearbit (MD);{MSB to output}
  for j := 0 to 2 do {delay : clock up in center of data}
   begin
   end; 
  invertbit (MC);{clock puls up}
  data := data * 2;{next bit}
  end;
 invertbit (ML);{latch puls}
 for i := 0 to 7 do
  begin
  end; 
 invertbit (ML);{latch high}
 clearbit (MC);{clock low}
 clearbit (MD);{data low}
end;

Procedure init;
begin
 writeport (%11111111, 3);
 preset := not(port(3)) and %00111110;{read preset, 5 bit only}
 preset := ((preset * 4) + 7) and $FF;{change preset to full 8 bit}
 ldata := preset;
 rdata := preset;
 left := M0 + preset; {calculate left volume}
 right := M1 + preset; {calculate right volume}
 send (left);{set left volume}
 send (right);{set right volume}
 send (M3);
	{mode3:left en right independent}
 send (M2);
	{mode2:no mute and de-emphasis control}
end;

Begin {start main program}
 writeport (%11111000, 1);
 wait (2);
 init;
 repeat
 begin
  controls := port(1) and %11111000;
  if controls = %11011000 then init;{check preset key}
  if controls = %01110000 then {mute !}
  begin
   ldata := $00;
   rdata := $00;
   send ($0100); {left channel mute}
   send ($0300); {right channel mute}
  end;
   repeat
   begin
   if controls = %11110000 then {check volume up key}
   begin
    if (ldata < $FF) and (rdata < $FF) then
    begin
     ldata := ldata + 1;
     rdata := rdata + 1;
     left := M0 + ldata;
     right := M1 + rdata;
     send (left);
     send (right);    
    end;
   end;
   controls := port(1) and %11111000;
  end;
  until controls <> %11110000;
  repeat
  begin
   if controls = %11101000 then {check volume down key}
   begin
    if (ldata > $00) and (rdata > $00) then
    begin
     ldata := ldata - 1;
     rdata := rdata - 1;
     left := M0 + ldata;
     right := M1 + rdata;
     send (left);
     send (right);    
    end;
   end;
   controls := port(1) and %11111000;
  end;
  until controls <> %11101000;
  repeat
  begin 
   if controls = %01111000 then {check balance left key}
   begin
    if rdata > $00 then
    begin
     if (ldata < rdata) and (ldata < $FF) then
     begin
      ldata := ldata + 1;
      left := M0 + ldata;
      send (left);
     end
     else
      rdata := rdata - 1;
      right := M1 + rdata;
      send (right);    
    end
   end;
   controls := port(1) and %11111000;
  end;
  until controls <> %01111000;
  repeat
  begin
   if controls = %10111000 then {check balance right key}
   begin
    if ldata > $00 then
    begin
     if (rdata < ldata) and (rdata < $FF) then
     begin
      rdata := rdata + 1;
      right := M1 + rdata;
      send (right);
     end
     else
      ldata := ldata - 1;
      left := M0 + ldata;
      send (left);
    end
   end;
   controls := port(1) and %11111000;
  end;
  until controls <> %10111000;
 end;
 until false;
end.
