I just got email from one of my TAs telling me that some of you are still
confused about the four device registers and how to use them.  If you feel
confident about your understanding of this, feel free to delete and move on.
Chapter 8, of course, is your reference for the "whole" story.

Now then:

1. One student was setting KBSR[14] in the interrupt service routine because
he somehow thought it was cleared.  No!  The o/s sets the IE bit if it wants
to allow the Keyboard to interrupt the processor.  The o/s clears the IE bit
if it does not want to allow the Keyboard to interrupt the processor.  If the
IE bit (bit[14]) is clear, the program has to poll the ready bit (KBSR[15])
to see if anyone typed a key.  That is most conveniently done by TRAP x23
or TRAP x20, each of which calls on the corresponding o/s trap service 
routine to do it for you.  If bit[14] is set, the program does not have to 
poll.  The keyboard will cause an interrupt when someone types a key (provided 
of course that interrupt is more urgent than the program that is running).

2. Another student polled KSBR[15] inside the interrupt service routine.
Totally unnecessary.  Since the computer is executing the service routine,
someone MUST have typed a key.  Otherwise you would not be in the interrupt
service routine.

3. What to do about DSR and DDR in the service routine?  You know that the
way output to the console terminal works is for the program to store into
DDR and the Display electronics will write that ascii code to the screen.
BUT first the LC-3 has to be sure the previous character written to the 
screen has already been finished.  How can the LC-3 be sure?  Recall 
(Chapter 8) that the electronics of the Display will set the ready bit 
DSR[15] when it has finished writing the character.  Therefore the 
interrupt service routine simply tests DSR[15] until it is a 1 and then
stores the next character to be displayed into DDR.  Look at the TRAP x21
routine if you are not sure.  Note: I do not want you to use the TRAP
instruction in the interrupt service routine.

4. Finally, you will note in the code for TRAP x21 that the trap service 
routine takes what is in R0 and stores it into DDR.  That is because the
convention for using TRAP x21 requires the character you want written to be in
R0 when you call TRAP x21.  In the case of the interrupt service routine,
that is not necessary.  You are not calling a trap service routine to do
the display for you.  You are loading DDR yourself.  Therefore you can load
it from anywhere you wish.

In summary:

1. your program is running, dumping .  .  .  .  etc. to the screen.
2. someone types a key, causing an interrupt.  (Why does it cause an interrupt?)
3. the computer pushes PC, PSR on the stack and executes the interrupt service
4. the interrupt service routine writes stuff to the screen.  The interrupt 
   service routine checks DSR[15] to see if the screen is available for writing.
   It keeps doing this until it is available.  Then it writes to DDR, and 
   continues this process until all the writes have been done.  Then the LC-3
   executes RTI which does two pops, and we are back to original program 
   running (step 1 above).


Good luck finishing this program, and understanding the concepts involved.

See you at the exam Wednesday night.

Yale Patt