unit console;

interface

const
    clBlack = $0;
    clBlue = $1;
    clGreen = $2;
    clCyan = $3;
    clRed = $4;
    clMagenta = $5;
    clBrown = $6;
    clLightGray = $7;
    clDarkGray = $8;
    clLightBlue = $9;
    clLightGreen = $A;
    clLightCyan = $B;
    clLightRed = $C;
    clLightMagenta = $D;
    clLightBrown = $E;
    clWhite = $F;


const
  ControlReg : word = $03D4;
  DataReg : word = $03D5;
  VidMem : PChar = PChar($B8000);

var
  CursorPosX: Word;
  CursorPosY: Word;
  Offset : Word;
  // Color attribute
  Attrib: Word;
  // Blank (space) character for current color
  Blank: Word;
  BlinkState: Bool;


procedure GoToXY(const X,Y: Word);
procedure BlinkOn;
procedure PrintString(const Data : string);
procedure PrintChar(const c : char);
procedure SetTextColor(const Bg,Fg: byte);
procedure ClearScreen;
procedure InitScreen;

implementation

uses Ports;

procedure GoToXY(const X,Y: Word);
begin
  if X < 80 then CursorPosX := X
  if Y < 25 then CursorPosY := Y
  Offset := (CursorPosY * 80 + CursorPosX) shl 1;
  Blink;
end;

procedure BlinkOn;
begin
  if CursorPosY >= 24 then Scroll;
  WritePort(ControlReg,$14);
  WritePort(DataReg,(Offset shr 9));
  WritePort(ControlReg,$15);
  WritePort(DataReg,Offset shr 1);
  BlinkState = True;
end;

procedure BlinkOff;
begin
  if BlinkState = True then
  begin
    WritePort(ControlReg,$14);
    WritePort(DataReg, 0xFF);
    WritePort(ControlReg,$15);
    WritePort(DataReg, 0xFF);
  end
  BlinkState = False;
end;


procedure Scroll(const Word : lines);
var
  i : byte;
  lines : Word;
begin
  lines = CursorPosY - 24;
  if lines >= 25 then FillWord((VidMem,80*25,Blank);
  else
    for i := 0 to (25 - lines)*160 do
      VidMem[i] := VidMem[i+160];
    for i := lines*80 to 24*80 do
      begin
        VidMem[2*i+0] := char($20); { space}
        VidMem[2*i+1] := char($07); { attribute associated with character cell }
      end;
  end
  CursorPosY := 24;
  Offset := (CursorPosY * 80 + CursorPosX) shl 1;
end;


procedure PrintString(const Data : string);
var
  i : integer;
begin
  BlinkOff;
  for i := 1 to Length(Data) do
    doChar(Data[i]);
end;


procedure printChar(const c : char);
begin
  BlinkOff;
  doChar(c);
end


procedure doChar(const c : char);
begin
  case c of
    // Backspaces
   #08 : begin
        if Offset > 0 then
          begin
          Offset := Offset - 2;
          VidMem[Offset] := ' ';
          VidMem[Offset + 1] := Char(Attrib);
          if CursorPosX > 0 then Dec(CursorPosX);
          else
            begin
            CursorPosX := 79;
            Dec(CursorPosY);
            end
          end
        end
    // Tabs, only to a position which is divisible by 8
    #09 : begin
        CursorPosX := (CursorPosX+8) and not 7;
        Offset := (CursorPosY * 80 + CursorPosX) shl 1;
        end
    { Newlines, DOS and BIOS way ( consider as if a carriage
      return is also there) }
    #10 : begin
      CursorPosX := 0;
      Inc(CursorPosY);
      Offset := (CursorPosY * 80 + CursorPosX) shl 1;
      end;
    // Carriage return
    #13 : begin
      CursorPosX := 0;
      Offset := (CursorPosY * 80 + CursorPosX) shl 1;
      end
    // Space
    #32 : begin
      Offset := Offset + 2;
      if CursorPosX >= 80 then begin
        CursorPosX := 0;
        Inc(CursorPosY);
        Offset := (CursorPosY * 80 + CursorPosX) shl 1;
        end
      end
    // Printable characters
    #33..#255 : begin
      if CursorPosY >= 24 then Scroll;
      VidMem[Offset] := c;
      VidMem[Offset + 1] := Char(Attrib);
      Offset := Offset + 2;
      if CursorPosX >= 80 then begin
        CursorPosX := 0;
        Inc(CursorPosY);
        Offset := (CursorPosY * 80 + CursorPosX) shl 1;
        end
      end
    end;
  end;
end;


procedure SetTextColor(const Bg,Fg : byte);
begin
  Attrib := (Bg shl 4) or (Fg and $0F);
  Blank := $20 or (Attrib shl 8)
end;


procedure ClearScreen;
var
  i: Byte;
begin
  FillWord((VidMem,80*25,Blank);
  CursorPosX := 0;
  CursorPosY := 0;
  Offset := 0;
  BlinkOn;
end;

procedure InitScreen;
begin
  SetTextColor(clBlack, clWhite);
  ClearScreen;
end;

