1 //
2 // Copyright (c) 2010-2023 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using System;
8 using Antmicro.Renode.Peripherals.Bus;
9 using System.Collections.Generic;
10 using Antmicro.Renode.Core;
11 using Antmicro.Renode.Logging;
12 using Antmicro.Renode.Peripherals.Miscellaneous;
13 using Antmicro.Migrant;
14 
15 namespace Antmicro.Renode.Peripherals.UART
16 {
17     [AllowedTranslations(AllowedTranslation.ByteToDoubleWord)]
18     public class AxiUartLite : IDoubleWordPeripheral, IUART, IKnownSize
19     {
AxiUartLite()20         public AxiUartLite()
21         {
22             readFifo = new Queue<uint>();
23             IRQ = new GPIO();
24         }
25 
WriteChar(byte value)26         public void WriteChar(byte value)
27         {
28             readFifo.Enqueue(value);
29         }
30 
Reset()31         public void Reset()
32         {
33             readFifo.Clear();
34         }
35 
ReadDoubleWord(long offset)36         public uint ReadDoubleWord(long offset)
37         {
38             switch((Register)offset)
39             {
40             case Register.RxFIFO:
41                 if(readFifo.Count == 0)
42                 {
43                     this.Log(LogLevel.Warning, "Trying to read from empty fifo.");
44                     return 0;
45                 }
46                 return readFifo.Dequeue();
47             case Register.Status:
48                 // Tx FIFO Empty | Rx FIFO Valid Data
49                 return (1u << 2) | (readFifo.Count == 0 ? 0 : 1u);
50             default:
51                 this.LogUnhandledRead(offset);
52                 return 0;
53             }
54         }
55 
WriteDoubleWord(long offset, uint value)56         public void WriteDoubleWord(long offset, uint value)
57         {
58             switch((Register)offset)
59             {
60             case Register.TxFIFO:
61                 CharReceived?.Invoke((byte)value);
62                 break;
63             default:
64                 this.LogUnhandledWrite(offset, value);
65                 break;
66             }
67         }
68 
69         [field: Transient]
70         public event Action<byte> CharReceived;
71 
72         public GPIO IRQ { get; }
73 
74         public long Size { get { return 0x10; } }
75         public Bits StopBits { get { return Bits.One; } }
76         public Parity ParityBit { get { return Parity.None; } }
77         public uint BaudRate { get { return 0; } }
78 
79         private readonly Queue<uint> readFifo;
80 
81         private enum Register
82         {
83             RxFIFO = 0x0,
84             TxFIFO = 0x4,
85             Status = 0x8,
86             Control = 0xC
87         }
88     }
89 }
90