program EightyColumnMode;

uses crt, graph;

type TForBitChar = array[0..4] of byte;

const
  CHAR_A                : TForBitChar = (
    %11101110,%10101010,%11101110,%10101010,%10101010
  );
  CHAR_B                : TForBitChar = (
    %11001100,%10101010,%11101110,%10101010,%11101110
  );
  CHAR_C                : TForBitChar = (
    %11101110,%10001000,%10001000,%10001000,%11101110
  );
  CHAR_D                : TForBitChar = (
    %11001100,%10101010,%10101010,%10101010,%11001100
  );
  CHAR_E                : TForBitChar = (
    %11101110,%10001000,%11101110,%10001000,%11101110
  );
  CHAR_F                : TForBitChar = (
    %11101110,%10001000,%11101110,%10001000,%10001000
  );
  CHAR_G                : TForBitChar = (
    %11101110,%10001000,%10101010,%10101010,%11101110
  );
  CHAR_H                : TForBitChar = (
    %10101010,%10101010,%11101110,%10101010,%10101010
  );   
  CHAR_SPACE            : TForBitChar = (
    0,0,0,0,0
  );
  FOR_BIT_CHARS          : array[0..7] of word = (
    CHAR_A,CHAR_B,CHAR_C,CHAR_D,CHAR_E,CHAR_F,CHAR_G,CHAR_H
  );  

var
  ba, tba               : word;
  col, row, i0b, even   : byte;
  pressedKey            : byte;
  charToDraw            : word;

begin
  // There are 192 rows of 320 dots in the full screen mode.
  InitGraph(8);
  ba := dpeek(88);
  tba := ba - 1;
  SetColor(1);
  
  col := 1;
  row := 1;
  
  WriteLn('Start typing a,b,c,d,e,f,g ...');
  
  repeat
    pressedKey := Ord(ReadKey) - 97;
    charToDraw := FOR_BIT_CHARS[pressedKey];
    
    WriteLn(col, '. ', Chr(pressedKey + 97), ' --> ', pressedKey + 97);
    
    if ((pressedKey >= 0) and (pressedKey < 8)) then begin
      if (Odd(col)) then begin
        Inc(tba);
        even := $f0;
      end else even := $0f;
      for i0b := 0 to 4 do begin
        poke(tba, peek(tba) or (peek(charToDraw) and even));
        Inc(tba, 40); Inc(charToDraw);
      end;      
      Dec(tba, 200); Inc(col);
      if (col = 81) then begin 
        Inc(tba, 200); Inc(row);
        col := 1;
      end;
    end;
  until false;
end.
