OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [UartSC.cpp] - Diff between revs 500 and 861

Only display areas with differences | Details | Blame | View Log

Rev 500 Rev 861
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
 
 
// SystemC Uart: implementation
// SystemC Uart: implementation
 
 
// This file is part of the cycle accurate model of the OpenRISC 1000 based
// This file is part of the cycle accurate model of the OpenRISC 1000 based
// system-on-chip, ORPSoC, built using Verilator.
// system-on-chip, ORPSoC, built using Verilator.
 
 
// This program is free software: you can redistribute it and/or modify it
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at your
// the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
// option) any later version.
 
 
// This program is distributed in the hope that it will be useful, but WITHOUT
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
// License for more details.
// License for more details.
 
 
// You should have received a copy of the GNU Lesser General Public License
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
 
 
// $Id: $
// $Id: $
 
 
#include <iostream>
#include <iostream>
#include <iomanip>
#include <iomanip>
 
 
#include "UartSC.h"
#include "UartSC.h"
 
 
//#define UART_SC_DEBUG
//#define UART_SC_DEBUG
 
 
// Keep disabled for now, to stop any portability problems cropping up.
// Keep disabled for now, to stop any portability problems cropping up.
//#define UART_SC_STDIN_ENABLE
//#define UART_SC_STDIN_ENABLE
 
 
#ifdef UART_SC_STDIN_ENABLE
#ifdef UART_SC_STDIN_ENABLE
#include <termios.h>
#include <termios.h>
 
#include <unistd.h>
#endif
#endif
 
 
SC_HAS_PROCESS(UartSC);
SC_HAS_PROCESS(UartSC);
 
 
//! Constructor for the Uart system C model
//! Constructor for the Uart system C model
 
 
//! @param[in] name  Name of this module, passed to the parent constructor.
//! @param[in] name  Name of this module, passed to the parent constructor.
// Todo: Probably some sort of scaler parameter
// Todo: Probably some sort of scaler parameter
 
 
UartSC::UartSC(sc_core::sc_module_name uart):
UartSC::UartSC(sc_core::sc_module_name uart):
        sc_module(uart)
        sc_module(uart)
{
{
#ifdef UART_SC_STDIN_ENABLE
#ifdef UART_SC_STDIN_ENABLE
        SC_THREAD(driveRx);
        SC_THREAD(driveRx);
#endif
#endif
        SC_THREAD(checkTx);
        SC_THREAD(checkTx);
        dont_initialize();
        dont_initialize();
        sensitive << uarttx;
        sensitive << uarttx;
 
 
}                               // UartSC ()
}                               // UartSC ()
 
 
void
void
UartSC::initUart(int uart_baud)
UartSC::initUart(int uart_baud)
{
{
        // Calculate number of ns per UART bit
        // Calculate number of ns per UART bit
        ns_per_bit = (int) ((long long)1000000000/(long long)uart_baud);
        ns_per_bit = (int) ((long long)1000000000/(long long)uart_baud);
        bits_received = 0;
        bits_received = 0;
 
 
        // Init state of RX
        // Init state of RX
        rx_state = 0;
        rx_state = 0;
 
 
        // Set input, ORPSoC's RX, line to high
        // Set input, ORPSoC's RX, line to high
        uartrx.write(true);
        uartrx.write(true);
 
 
 
 
#ifdef UART_SC_DEBUG
#ifdef UART_SC_DEBUG
        printf
        printf
                ("UartSC Initialised: Baud: %d, ns per bit: %d\n",
                ("UartSC Initialised: Baud: %d, ns per bit: %d\n",
                 uart_baud, ns_per_bit);
                 uart_baud, ns_per_bit);
#endif
#endif
}
}
 
 
// Some C from 
// Some C from 
// http://cc.byexamples.com/2007/04/08/non-blocking-user-input-in-loop-without-ncurses/
// http://cc.byexamples.com/2007/04/08/non-blocking-user-input-in-loop-without-ncurses/
//
//
int UartSC::kbhit()
int UartSC::kbhit()
{
{
#ifdef UART_SC_STDIN_ENABLE
#ifdef UART_SC_STDIN_ENABLE
    struct timeval tv;
    struct timeval tv;
    fd_set fds;
    fd_set fds;
    tv.tv_sec = 0;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    tv.tv_usec = 0;
    FD_ZERO(&fds);
    FD_ZERO(&fds);
    FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
    FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
    select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
    select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
    return FD_ISSET(STDIN_FILENO, &fds);
    return FD_ISSET(STDIN_FILENO, &fds);
#else
#else
    return 0;
    return 0;
#endif
#endif
}
}
 
 
#define NB_ENABLE 1
#define NB_ENABLE 1
#define NB_DISABLE 0
#define NB_DISABLE 0
 
 
// The following is apparently VERY Linux-centric. Maybe a ncurses version could
// The following is apparently VERY Linux-centric. Maybe a ncurses version could
// be handy if this gets used on various platforms.
// be handy if this gets used on various platforms.
void UartSC::nonblock(int state)
void UartSC::nonblock(int state)
{
{
#ifdef UART_SC_STDIN_ENABLE
#ifdef UART_SC_STDIN_ENABLE
        struct termios ttystate;
        struct termios ttystate;
 
 
        //get the terminal state
        //get the terminal state
        tcgetattr(STDIN_FILENO, &ttystate);
        tcgetattr(STDIN_FILENO, &ttystate);
 
 
        if (state==NB_ENABLE)
        if (state==NB_ENABLE)
        {
        {
                //turn off canonical mode
                //turn off canonical mode
                ttystate.c_lflag &= ~ICANON;
                ttystate.c_lflag &= ~ICANON;
                //minimum of number input read.
                //minimum of number input read.
                ttystate.c_cc[VMIN] = 1;
                ttystate.c_cc[VMIN] = 1;
        }
        }
        else if (state==NB_DISABLE)
        else if (state==NB_DISABLE)
        {
        {
                //turn on canonical mode
                //turn on canonical mode
                ttystate.c_lflag |= ICANON;
                ttystate.c_lflag |= ICANON;
        }
        }
        //set the terminal attributes.
        //set the terminal attributes.
        tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
        tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
#endif  
#endif  
}
}
 
 
 
 
 
 
void UartSC::driveRx()
void UartSC::driveRx()
{
{
        static char c;
        static char c;
 
 
        UartSC::nonblock(NB_ENABLE);
        UartSC::nonblock(NB_ENABLE);
 
 
        while(1)
        while(1)
        {
        {
                if (rx_state == 0) // Waiting for a character input from user
                if (rx_state == 0) // Waiting for a character input from user
                {
                {
 
 
                        // Do we have a character on input?
                        // Do we have a character on input?
                        //c=cin.peek();
                        //c=cin.peek();
 
 
 
 
                        if (kbhit())
                        if (kbhit())
                        {
                        {
 
 
                                c = fgetc(stdin);
                                c = fgetc(stdin);
#ifdef UART_SC_DEBUG
#ifdef UART_SC_DEBUG
                                cout << "UartSC::driveRX got " << c << endl;
                                cout << "UartSC::driveRX got " << c << endl;
#endif
#endif
                                rx_state++;
                                rx_state++;
                        }
                        }
 
 
                        wait(1000000, SC_NS);
                        wait(1000000, SC_NS);
 
 
                }
                }
                else if (rx_state == 1)
                else if (rx_state == 1)
                {
                {
#ifdef UART_SC_DEBUG
#ifdef UART_SC_DEBUG
                        cout << "UartSC::driveRX start-bit " << c << endl;
                        cout << "UartSC::driveRX start-bit " << c << endl;
#endif
#endif
                        // Start bit - low
                        // Start bit - low
                        uartrx.write(false);
                        uartrx.write(false);
                        rx_state++;
                        rx_state++;
                        // Wait a bit
                        // Wait a bit
                        wait(ns_per_bit, SC_NS);
                        wait(ns_per_bit, SC_NS);
                }
                }
                else if (rx_state > 1 && rx_state < 10)
                else if (rx_state > 1 && rx_state < 10)
                {
                {
#ifdef UART_SC_DEBUG
#ifdef UART_SC_DEBUG
                        cout << "UartSC::driveRX bit " << rx_state-2 << " " <<
                        cout << "UartSC::driveRX bit " << rx_state-2 << " " <<
                             (c & (1 << rx_state-2)) << endl;
                             (c & (1 << rx_state-2)) << endl;
#endif
#endif
 
 
                        if (c & (1 << rx_state-2))
                        if (c & (1 << rx_state-2))
                                uartrx.write(true);
                                uartrx.write(true);
                        else
                        else
                                uartrx.write(false);
                                uartrx.write(false);
 
 
                        rx_state++;
                        rx_state++;
 
 
                        // Wait a bit
                        // Wait a bit
                        wait(ns_per_bit, SC_NS);
                        wait(ns_per_bit, SC_NS);
                }
                }
                else if (rx_state == 10)
                else if (rx_state == 10)
                {
                {
#ifdef UART_SC_DEBUG
#ifdef UART_SC_DEBUG
                        cout << "UartSC::driveRX stop bit" << endl;
                        cout << "UartSC::driveRX stop bit" << endl;
#endif
#endif
                        rx_state = 0;
                        rx_state = 0;
                        // Stop bit
                        // Stop bit
                        uartrx.write(true);
                        uartrx.write(true);
                        // Wait a bit
                        // Wait a bit
                        wait(ns_per_bit + (ns_per_bit/2), SC_NS);
                        wait(ns_per_bit + (ns_per_bit/2), SC_NS);
 
 
                }
                }
        }
        }
}
}
 
 
 
 
// Maybe do this with threads instead?!
// Maybe do this with threads instead?!
void UartSC::checkTx()
void UartSC::checkTx()
{
{
        while(1){
        while(1){
 
 
                // Check the number of bits received
                // Check the number of bits received
                if (bits_received == 0) {
                if (bits_received == 0) {
                        // Check if tx is low
                        // Check if tx is low
                        if ((uarttx.read() & 1) == 0) {
                        if ((uarttx.read() & 1) == 0) {
 
 
                                // Line pulled low, begin receive of new char
                                // Line pulled low, begin receive of new char
                                current_char = 0;
                                current_char = 0;
 
 
                                //counter = 1;                  
                                //counter = 1;                  
 
 
                                bits_received++;        // We got the start bit
                                bits_received++;        // We got the start bit
 
 
                                // Now wait until next bit
                                // Now wait until next bit
                                wait(ns_per_bit, SC_NS);
                                wait(ns_per_bit, SC_NS);
#ifdef UART_SC_DEBUG
#ifdef UART_SC_DEBUG
                                cout << "UartSC checkTx: got start bit at time "
                                cout << "UartSC checkTx: got start bit at time "
                                     << sc_time_stamp() << endl;
                                     << sc_time_stamp() << endl;
#endif
#endif
                        }
                        }
                        else
                        else
                                // Nothing yet - keep waiting
                                // Nothing yet - keep waiting
                                wait(ns_per_bit/2, SC_NS);
                                wait(ns_per_bit/2, SC_NS);
 
 
                } else if (bits_received > 0 && bits_received < 9) {
                } else if (bits_received > 0 && bits_received < 9) {
 
 
                        current_char |= ((uarttx.read() & 1) <<
                        current_char |= ((uarttx.read() & 1) <<
                                         (bits_received - 1));
                                         (bits_received - 1));
 
 
                        // Increment bit number
                        // Increment bit number
                        bits_received++;
                        bits_received++;
 
 
                        // Wait for next bit
                        // Wait for next bit
                        wait(ns_per_bit, SC_NS);
                        wait(ns_per_bit, SC_NS);
 
 
 
 
                } else if (bits_received == 9) {
                } else if (bits_received == 9) {
 
 
                        // Now check for stop bit 1
                        // Now check for stop bit 1
                        if ((uarttx.read() & 1) != 1) {
                        if ((uarttx.read() & 1) != 1) {
                                printf("UART TX framing error at time\n");
                                printf("UART TX framing error at time\n");
                                cout << sc_time_stamp() << endl;
                                cout << sc_time_stamp() << endl;
 
 
                                // Perhaps do something else here to deal with 
                                // Perhaps do something else here to deal with 
                                // this.
                                // this.
                                bits_received = 0;
                                bits_received = 0;
                        }
                        }
                        else
                        else
                        {
                        {
                                // Print the char
                                // Print the char
#ifdef UART_SC_DEBUG
#ifdef UART_SC_DEBUG
                                printf("Char received: 0x%2x time: ",
                                printf("Char received: 0x%2x time: ",
                                       current_char);
                                       current_char);
                                cout << sc_time_stamp() << endl;
                                cout << sc_time_stamp() << endl;
#endif
#endif
                                // cout'ing the char didn't work for some 
                                // cout'ing the char didn't work for some 
                                // systems.
                                // systems.
                                //cout << current_char;
                                //cout << current_char;
                                printf("%c", current_char);
                                printf("%c", current_char);
 
 
                                bits_received = 0;
                                bits_received = 0;
                        }
                        }
                }
                }
        }
        }
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.