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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [UartSC.cpp] - Blame information for rev 6

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 julius
// ----------------------------------------------------------------------------
2
 
3
// SystemC Uart: implementation
4
 
5
// This file is part of the cycle accurate model of the OpenRISC 1000 based
6
// system-on-chip, ORPSoC, built using Verilator.
7
 
8
// This program is free software: you can redistribute it and/or modify it
9
// under the terms of the GNU Lesser General Public License as published by
10
// the Free Software Foundation, either version 3 of the License, or (at your
11
// option) any later version.
12
 
13
// This program is distributed in the hope that it will be useful, but WITHOUT
14
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16
// License for more details.
17
 
18
// You should have received a copy of the GNU Lesser General Public License
19
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
21
// ----------------------------------------------------------------------------
22
 
23
// $Id: $
24
 
25
#include <iostream>
26
#include <iomanip>
27
#include <cmath>
28
 
29
#include "UartSC.h"
30
 
31
 
32
SC_HAS_PROCESS( UartSC );
33
 
34
//! Constructor for the Uart system C model
35
 
36
//! @param[in] name  Name of this module, passed to the parent constructor.
37
// Todo: Probably some sort of scaler parameter
38
 
39
UartSC::UartSC (sc_core::sc_module_name   name):
40
  sc_module (name)
41
{
42
 
43
  SC_METHOD (checkTx);
44
  dont_initialize();
45
  sensitive << clk.pos();
46
  //sensitive << uarttx;
47
 
48
}       // UartSC ()
49
 
50
void
51
UartSC::initUart (int clk_freq_hz, // Presume in NS
52
                  int uart_baud
53
                  )
54
{
55
  // Calculate number of clocks per UART bit
56
  clocks_per_bit = (int)(clk_freq_hz/uart_baud);
57
}
58
 
59
 
60
// Maybe do this with threads instead?!
61
void
62
UartSC::checkTx () {
63
 
64
  //printf("Uart TX activity: level is : 0x%x\n", uarttx.read()&1);
65
 
66
  // Check the number of bits received
67
  if (bits_received==0)
68
    {
69
      // Check if tx is low
70
      if ((uarttx.read()&1) == 0)
71
        {
72
          // Line pulled low, begin receive of new char
73
          current_char = 0;
74
          // Start 
75
          counter = 1;
76
          bits_received++; // We got the start bit
77
          //cout << "UartSC checkTx: got start bit at time " << sc_time_stamp() << endl;
78
        }
79
    }
80
  else if (bits_received > 0 && bits_received < 9)
81
    {
82
      // Check the counter - see if it's time to sample the line
83
      // We do an extra half-bit delay on first bit read
84
      if ( ((bits_received==1) &&
85
            (counter == (clocks_per_bit + (clocks_per_bit/2)))) ||
86
           ((bits_received > 1) && (counter == clocks_per_bit)) )
87
        {
88
          //printf("UartSC checkTx: read bit %d as 0x%x at time", bits_received, uarttx.read()&1);
89
          //cout << sc_time_stamp() << endl;
90
 
91
          // Shift in the current value of the tx into our char
92
          current_char |= ((uarttx.read() & 1) << (bits_received-1));
93
          // Reset the counter
94
          counter = 1;
95
          // Increment bit number
96
          bits_received++;
97
        }
98
      else
99
        counter++;
100
    }
101
  else if (bits_received == 9)
102
    {
103
      // Now check for stop bit 1
104
      if (counter == clocks_per_bit)
105
        {
106
          // Check that the value is 1 - this should be the stop bit
107
          if ((uarttx.read() & 1) != 1)
108
            {
109
              printf("UART TX framing error at time\n");
110
              cout << sc_time_stamp() << endl;
111
 
112
              // Perhaps do something else here to deal with this
113
              bits_received = 0;
114
              counter = 0;
115
            }
116
          else
117
            {
118
              // Print the char
119
              //printf("Char received: 0x%2x time: ", current_char);
120
              //cout << sc_time_stamp() << endl;
121
              cout << current_char;
122
              bits_received = 0;
123
              counter = 0;
124
            }
125
        }
126
      else
127
        counter++;
128
    }
129
}
130
 
131
 
132
 

powered by: WebSVN 2.1.0

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