1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using System.Collections.Generic;
10 using Antmicro.Migrant;
11 
12 namespace Antmicro.Renode.Peripherals.UART
13 {
14     public class SemihostingUart : IPeripheral, IUART
15     {
SemihostingUart()16         public SemihostingUart()
17         {
18             Reset();
19         }
20 
21         [field: Transient]
22         public event Action<byte> CharReceived;
23 
24         public Bits StopBits
25         {
26                 get
27                 {
28                         return 0;
29                 }
30         }
31 
32         public Parity ParityBit
33         {
34             get
35             {
36                 return Parity.None;
37             }
38         }
39 
40         public uint BaudRate
41         {
42             get
43             {
44                 return 9600;
45             }
46         }
47 
Reset()48         public void Reset()
49         {
50             readFifo = new Queue<uint>(receiveFifoSize);    // typed chars are stored here
51         }
52 
WriteChar(byte value)53         public void WriteChar(byte value) // char is typed
54         {
55             lock(UartLock)
56             {
57                 readFifo.Enqueue(value);
58             }
59         }
60 
SemihostingGetByte()61 	public byte SemihostingGetByte() {
62 		lock(UartLock) {
63 			return readFifo.Count > 0 ? (byte)readFifo.Dequeue() : (byte)0;
64 		}
65 	}
66 
SemihostingWriteString(string s)67         public void SemihostingWriteString(string s)
68         {
69             lock(UartLock) {
70 	            for (int i = 0; i < s.Length; i++) {
71                         OnCharReceived(Convert.ToByte(s[i]));
72 		    }
73 	    }
74         }
75 
OnCharReceived(byte b)76         private void OnCharReceived(byte b)
77         {
78             var handler = CharReceived;
79             if(handler != null)
80             {
81                 handler(b);
82             }
83 
84         }
85 
86         private object UartLock = new object();
87         private Queue<uint> readFifo;
88         private const int receiveFifoSize = 16;
89     }
90 }
91 
92