#INCLUDE "semaphore.inc"
#INCLUDE "crew.inc"

#INCLUDE "consts.inc"
#USE "course.lib"

#INCLUDE "display.inc"
#INCLUDE "control_info.inc"

PROC display (CHAN OF DISPLAY in, 
              CREW.CONTROL.INFO control.info,
              CHAN OF BYTE out)

  VAL INT thinking IS 0:      -- indices into the col array (see below)
  VAL INT wanna.read IS 1:
  VAL INT reading IS 2:
  VAL INT done.read IS 3:
  VAL INT wanna.write IS 4:
  VAL INT writing IS 5:
  VAL INT done.write IS 6:
  VAL INT time IS 7:
  VAL INT second IS 8:
  VAL INT read.percent IS 9:

  VAL []BYTE col IS [4, 19, 31, 72, 45, 59, 72, 50, 12, 36]:

  VAL BYTE row.shift IS 5:
  VAL BYTE row.info IS 16:

  SEQ

    --{{{  initialise
    
    INT seconds, read.percentage:
    
    SEQ
    
      claim.read.crew (control.info[crew])
      seconds := control.info[seconds]
      read.percentage := control.info[read.percentage]
      release.read.crew (control.info[crew])
    
      cursor.x.y (1, 1, out)
      erase.eos (out)
      cursor.x.y (2, 2, out)
      out.string ("Thinking     Wanna Read    Reading     *
                  *Wanna Write     Writing    Local View", 0, out)
      cursor.x.y (2, 3, out)
      out.string ("========     ==========    =======     *
                  *===========     =======    ==========", 0, out)
      cursor.x.y (2, row.info, out)
      out.string ("Seconds = ", 0, out)
      out.number (seconds, 7, out)
      cursor.x.y (24, row.info, out)
      out.string ("Read %age = ", 0, out)
      out.number (read.percentage, 3, out)
      cursor.x.y (col[done.read], row.info, out)
      cursor.x.y (43, row.info, out)
      out.string ("Time = ", 0, out)
      cursor.x.y (59, row.info, out)
      out.string ("Blackboard = ", 0, out)
      cursor.x.y (col[done.read], row.info, out)
      out ! '?'
      out ! '?'
      out ! '?'
      out ! FLUSH
    
      cursor.x.y (1, row.info + 2, out)
      out.string ("=============================================================================", 0, out)
      cursor.x.y (7, row.info + 4, out)
      out.string ("Keyboard controls:", 0, out)
      cursor.x.y (7, row.info + 6, out)
      out.string ("  S : increment philosophers*' second (slows down the action)", 0, out)
      cursor.x.y (7, row.info + 7, out)
      out.string ("  s : decrement philosophers*' second (speeds up the action)", 0, out)
      cursor.x.y (7, row.info + 8, out)
      out.string ("  R : increment philosophers*' read (as opposed to write) %age", 0, out)
      cursor.x.y (7, row.info + 9, out)
      out.string ("  r : decrement philosophers*' read (as opposed to write) %age", 0, out)
      cursor.x.y (7, row.info + 10, out)
      out.string ("  f : freeze action (next keystroke releases)",  0, out)
      cursor.x.y (1, row.info + 12, out)
      out.string ("=============================================================================", 0, out)
      out ! FLUSH
    
    --}}}

    --{{{  main cycle
    WHILE TRUE
      in ? CASE
        --{{{  thinking; id
        BYTE id:
        thinking; id
          SEQ
            cursor.x.y (col[thinking], id + row.shift, out)
            out.string (":-)", 0, out)
            out ! FLUSH
        --}}}
        --{{{  wanna.read; id
        BYTE id:
        wanna.read; id
          SEQ
            cursor.x.y (col[thinking], id + row.shift, out)
            out.string ("   ", 0, out)
            cursor.x.y (col[wanna.read], id + row.shift, out)
            out.string (":-(", 0, out)
            out ! FLUSH
        --}}}
        --{{{  reading; id
        BYTE id:
        reading; id
          SEQ
            cursor.x.y (col[wanna.read], id + row.shift, out)
            out.string ("   ", 0, out)
            cursor.x.y (col[reading], id + row.shift, out)
            out.string (":-)", 0, out)
            out ! FLUSH
        --}}}
        --{{{  done.read; id; scribble
        BYTE id:
        BYTE scribble:
        done.read; id; scribble
          SEQ
            cursor.x.y (col[reading], id + row.shift, out)
            out.string ("   ", 0, out)
            cursor.x.y (col[done.read], id + row.shift, out)
            out ! scribble
            out ! scribble
            out ! scribble
            out ! FLUSH
        --}}}
        --{{{  wanna.write; id
        BYTE id:
        BYTE scribble:
        wanna.write; id; scribble
          SEQ
            cursor.x.y (col[thinking], id + row.shift, out)
            out.string ("   ", 0, out)
            cursor.x.y (col[wanna.write], id + row.shift, out)
            out ! scribble
            out ! scribble
            out ! scribble
            out ! FLUSH
        --}}}
        --{{{  writing; id; scribble
        BYTE id:
        BYTE scribble:
        writing; id; scribble
          SEQ
            cursor.x.y (col[wanna.write], id + row.shift, out)
            out.string ("   ", 0, out)
            cursor.x.y (col[writing], id + row.shift, out)
            out ! scribble
            out ! scribble
            out ! scribble
            out ! FLUSH
        --}}}
        --{{{  done.write; id; scribble
        BYTE id:
        BYTE scribble:
        done.write; id; scribble
          SEQ
            cursor.x.y (col[writing], id + row.shift, out)
            out.string ("   ", 0, out)
            cursor.x.y (col[done.write], id + row.shift, out)
            out ! scribble
            out ! scribble
            out ! scribble
            cursor.x.y (col[done.read], row.info, out)
            out ! scribble
            out ! scribble
            out ! scribble
            out ! FLUSH
        --}}}
        --{{{  time; now
        INT now:
        time; now
          SEQ
            cursor.x.y (col[time], row.info, out)
            out.number (now, 0, out)
            out ! FLUSH
        --}}}
        --{{{  control
        control
          INT seconds, read.percentage:
          SEQ
            claim.read.crew (control.info[crew])
            seconds := control.info[seconds]
            read.percentage := control.info[read.percentage]
            release.read.crew (control.info[crew])
            cursor.x.y (col[second], row.info, out)
            out.number (seconds, 7, out)
            cursor.x.y (col[read.percent], row.info, out)
            out.number (read.percentage, 3, out)
            out ! FLUSH
        --}}}
    --}}}

:
