CS452 - Real-Time Programming - Winter 2015

Lecture 2 - Polling Loops

Public Service Annoucements

  1. E-mail only to uwaterloo addresses; respond on newsgroup
  2. Due date for assignment 0
  3. Obsolete copy of first
  4. Document on UARTs moved
  5. Caches, optimization, clock speed, FIFOs. Any one of these turned on is likely to lead to obscure bugs.
  6. Libraries: memcpy in particular
  7. Nettops: may need rebooting, two (?) don't work even when rebooted. This is usually enough when students are working in pairs, but may not be enough for assignment 0. Fraser is trying to get several on loan from other labs for this week.
  8. University github: git.uwaterloo.ca
  9. Comments about first.pdf

Practical Details: pdf


"Smart" terminals

In the late 1970s Digitial Equipment Corporation (now part of HP by way of Compaq) introduced a "smart" terminal called the VT100; its smart feature was cursor addressing. There were keys added to its keyboard that sent several characters at once. The first character was always ESCAPE, so these key sequences were called escape codes.

The first escape codes moved the cursor around the display. They were used more often by programs that by users. Without cursor adderssing it was necessary to write 80 X 24 characters to make a change to the display, even if only 2 characters were changing. With cursor addressing an escape codes could put the cursor in the right place and send the characters to be changed.

Escape codes vary from one terminal program to another with a common core which is the set created for the VT100.


Timers

How does one keep time in a computer?

In practice, how do you find out the time for a0?

In practice, how do you know that you are keeping accurate time?


Busy Waiting

This is used to synchronize with an external event, minimizing response time. This bit of code waits for something to be ready then does something as soon at whatever it is is ready.

    #define FOREVER for( ; ; )
    FOREVER {
      if ( ready( ) ) do-it( );
    }
  
Actually, the code above does not necessarily work. Almost every compiler that optimizes even a little will convert the above code to
    if ( ready( ) ) do-it( );
  
The compiler does not know that a value inside ready( ) will change without an assignment.

Note. The volatile keyword is one way of controlling the compiler.

Worst case response time

Let's suppose

    int ready( ) {
      return *status-addr >> # & 1;
  
which the compiler will in-line.

Exercise for the reader. What is the longest possible time between the ready-bit setting and execution of the first instruction of do-it. Case 1: do-it inlined; case 2: do-it run as a function.

The problem with busy-waiting

What if the CPU has to three things at once?

E.g.,

  1. collect/send bytes coming from/to one serial port,
  2. collect/send bytes coming from/to another serial port, and
  3. maintain a clock in the terminal window.

To understand this problem we need to do a little mental arithmetic to see, for example, how long the CPU would spend busy-waiting.

Digression: How many bits in a character?