MODULE HW;

IMPORT sys := SYSTEM, KOSAPI;

PROCEDURE WindowRedrawStatus(p: INTEGER);
VAR res: INTEGER;
BEGIN
  res := KOSAPI.sysfunc2(12, p)
END WindowRedrawStatus;

PROCEDURE DefineAndDrawWindow(x, y, w, h, color, style, hcolor, hstyle, htext: INTEGER);
VAR res: INTEGER;
BEGIN
  res := KOSAPI.sysfunc6(0, x * 65536 + w, y * 65536 + h, color + LSL(style, 24), hcolor + LSL(hstyle, 24), htext)
END DefineAndDrawWindow;

PROCEDURE WriteTextToWindow(x, y, color: INTEGER; text: ARRAY OF CHAR);
VAR res: INTEGER;
BEGIN
  res := KOSAPI.sysfunc6(4, x * 65536 + y, color + LSL(48, 24), sys.ADR(text[0]), LENGTH(text), 0)
END WriteTextToWindow;

PROCEDURE WaitForEvent(): INTEGER;
  RETURN KOSAPI.sysfunc1(10)
END WaitForEvent;

PROCEDURE ExitApp;
VAR res: INTEGER;
BEGIN
  res := KOSAPI.sysfunc1(-1)
END ExitApp;

PROCEDURE draw_window(header, text: ARRAY OF CHAR);
BEGIN
  WindowRedrawStatus(1);
  DefineAndDrawWindow(200, 200, 200, 100, 0FFFFFFH, 51, 0, 0, sys.ADR(header));
  WriteTextToWindow(10, 10, 0FF0000H, text);
  WindowRedrawStatus(2);
END draw_window;

PROCEDURE Main(header, text: ARRAY OF CHAR);
BEGIN
  WHILE TRUE DO
    CASE WaitForEvent() OF
    |1: draw_window(header, text)
    |3: ExitApp
    ELSE
    END
  END
END Main;

BEGIN
  Main("HW", "Hello, world!")
END HW.