program MIDI2CVms20clone;
{$NOSHADOW}
{ $S+ Stack check }         { switch not used }
{ $R+ Range check }         { switch not used }
{ $W+ Warnings    }         { Warnings off    }
Device = 90S2313;
Import  Serport;
From System Import ;
Define
        ProcClock   = 4000000;      { Hertz }
        StackSize   = $0020, iData;
        FrameSize   = $0020, iData;
        Rxbuffer=16,Idata;
        Serport=31250,stop1;


Implementation
{$IDATA}

var
             //notenbyte : array [1..16] of byte;
             {notenbyte 01 bis 16 enthält für jedes bit eine gespielte Taste (=128 bits)}
             anzahlnoten : integer;
             {inc bei note on, dec bei note off}
             noteon      : boolean;
             noteoff     : boolean;
             midiold     : byte;
             midi        : byte;
             velocity    : byte;
             ok          : byte;
             notevoltage : byte;
             temp        : byte;
             trigger[@portd,6] : bit;
             glide[@portd,5]   : bit;

begin
  DDRB:=%11111111;  { portb is DA-output for 0808}
  portb:=0;         { 0V }
  DDRD:= %01100000; { portd, bit6 is trigger, bit5 is glide}
  trigger := true;
  glide := true;
  midi :=0;
  noteon:=false;
  noteoff:=false;
  ok:=0;
  velocity:=0;
  notevoltage:=0;
  anzahlnoten:=0;
  //midich:=0; //(pinD  shr 2) and $0F; { DIP SW read }
  {enable interrupts for serial inputs (midi)}
  EnableInts;
  repeat
     if serstat then

        midi:=serinp;

        if (midi and $f0)=$80 then
          noteoff:=true;{note off}
          ok:=1;
        endif;

        if (midi and $f0)=$90 then
          noteon:=true;{note on}
          ok:=1;
        endif;

        if noteon then
          if (ok=2) then
            notevoltage:=midi;
          endif;
          if (ok=3) then
           {now, all bytes are there for proceeding a "note on" - event}
            velocity:=midi;
            if velocity>0 then
              notevoltage:=(notevoltage-48) shl 2;

              portb:=notevoltage;

              inc(anzahlnoten);
              if (anzahlnoten>1) then
                glide:=false;
              else
                glide:=true;
              endif;
              mdelay(2);
              trigger:=false;
              noteon:=false;
            else
              noteon:=false;
              dec(anzahlnoten);
              trigger:=true;
            endif;
          endif;
        endif;

        if noteoff then
          if (ok=2) then
          endif;
          if (ok=3) then
          {now, all bytes are there for proceeding a "note off" - event}
           velocity:=midi;
           dec(anzahlnoten);
           if (anzahlnoten<1) then
              trigger:=true;
              glide:=true;
           endif;
           noteoff:=false;
          endif;
        endif;

        inc(ok);
    endif ; {if serstat}
    if anzahlnoten<0 then anzahlnoten:=0; endif;
    if anzahlnoten>3 then anzahlnoten:=3; endif;
  until false;
end.


