title: Polling I/O (MIPS)
body: <p>I am attempting to write a program in MIPS that uses polling to read a character from the keyboard and then displays it using the builtin Keyboard and Display MMIO Simulator. Unfortunately, I am having trouble grasping the concept behind the registers used and the control bits, but have been trying to figure it out from examples online.</p>
  
  <p>Here is what I have written so far:</p>
  
  <pre><code>            .data
  
              .text
              .globl main
  
  main:
              .eqv    RCR 0xffff0000      # Receiver Control Register     (Ready Bit)
              .eqv    RDR 0xffff0004      # Receiver Data Register        (Key Pressed - ASCII)
              .eqv    TCR 0xffff0008      # Transmitter Control Register  (Ready Bit)
              .eqv    TDR 0xffff000c      # Transmitter Data Register     (Key Displayed- ASCII)
  
  keyWait:    lw      $t0, RCR
              andi    $t0, $t0, 1
              beq     $t0, $zero, keyWait
  
              lb      $a0, RDR
  </code></pre>
  
  <p>What I believe is happening here is that <code>keyWait</code> continues to loop until the ready bit is set to 1 (*), and then the ASCII value of the key is stored in <code>$a0</code>. Is this correct?</p>
  
  <p>If this is correct, how can I then print the character on the display using the same polling technique with the TCR and TDR?</p>
  
  <p>*I don't understand why this is needed to be checked with the <code>andi</code> operation - if <code>$t0</code> is 1 (or 0), it is <code>and</code>ed with 1, which equals 1 (or 0), which is then stored in <code>$t0</code>? So the value of <code>$t0</code> stays as it was prior to this operation regardless?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is my attempt at passing the key pressed data to the display.</p>
  
  <pre><code>                .data
  
                  .text
                  .globl main
  
  main:
                  .eqv    RCR 0xffff0000      # Receiver Control Register     (Ready Bit)
                  .eqv    RDR 0xffff0004      # Receiver Data Register    (Key Pressed - ASCII)
                  .eqv    TCR 0xffff0008      # Transmitter Control Register  (Ready Bit)
                  .eqv    TDR 0xffff000c      # Transmitter Data Register (Key Displayed- ASCII)
  
  keyWait:        lw      $t0, RCR
                  andi    $t0, $t0, 1
                  beq     $t0, $zero, keyWait
  
                  lbu     $a0, RDR
  
  displayWait:    lw      $t1, TCR
                  andi    $t1, $t1, 1
                  beq     $t1, $zero, displayWait
  
                  sb      $a1, TDR
  
                  li      $v0, 11
                  syscall 
  
                  j       keyWait
  </code></pre>
  
  <p>This is just a test program which should print the character on the MMIO Display Simulator, and also in the console within MIPS. It is doing the latter, but not on the display simulator. In the display simulator, <code>Cursor:</code> is incrementing each time a key is pressed, but nothing is being printed. What am I missing?</p>