
/* ////////////////////////////////////////////////////////////////////////////
                                     uat.v
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION:    This is the UAT (UART without the R) part of the Morse code
                decoder.  It's a bare bones thing that does just barely enough
                to get characters sent via an RS-232 port.

                Its three inputs are a baud clock, an 8-bit data port, and a
                strobe.  Client modules put the characters they want to send
                one at a time on the data port and then pulse the strobe line.

                As soon as the strobe is released, the module generates a
                10-bit, N-8-1 style frame on its RS-232 output line.  The
                module also maintains a second output line that is high
                whenever it's busy generating a frame and not really interested
                in receiving another character.

REVISIONS:      29 Sep 07 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */

module UAT (baudClock, dataIn, strobe, rs232out, txBusy);

    input  baudClock;                           // Baud clock
    input  [7 : 0] dataIn;                      // Client puts input bytes here
    input  strobe;                              // Client pulses this line to
                                                //  indicate new byte available
    output rs232out;                            // RS-232 output
    output txBusy;                              // High when transmitter busy

    reg rs232out;                               // See above
    reg txBusy;                                 // See above
    reg [9 : 0] txShifter;                      // Internal Tx shift register
    reg [3 : 0] bitCounter;                     // Count ten bits here

    always @(posedge strobe,
             posedge baudClock) begin
        if (strobe) begin                       // Receive new byte from client
            txShifter[0] <= 0;                  // Set up the start bit
            txShifter[8 : 1] <= dataIn;         // Set up the data
            txShifter[9] <= 1;                  // Set up the stop bit
            bitCounter <= 9;                    // Nine bits are ready to send
            txBusy <= 1;                        // We're busy now
            end                                 // End 'receive new byte'
        else begin                              // Time for a new bit
            if (bitCounter) begin               // Need to send more bits
                txShifter[8 : 0] <=             // Shift remaining bits one
                    txShifter[9 : 1];           //  position to the right
                txShifter[9] <= 0;              // Bit 9 needs to be something
                bitCounter <= bitCounter - 1;   // Count the bit just sent
                end                             // End 'need to send more bits'
            rs232out <= txShifter[0];           // Put next bit on output line
            txBusy <= (bitCounter != 0);
            end                                 // End 'time for a new bit'
        end

    endmodule                                   // End 'module UAT'
