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 Antmicro.Renode.Logging;
8 using Antmicro.Renode.Utilities;
9 
10 namespace Antmicro.Renode.Peripherals.SPI.NORFlash
11 {
12     public struct DecodedOperation
13     {
14         public OperationType Operation;
15         public OperationState State;
16         public OperationEraseSize EraseSize;
17         public uint Register;
18         public uint ExecutionAddress;
19         public int CommandBytesHandled;
20         public int DummyBytesRemaining;
21         public int AddressLength
22         {
23             get
24             {
25                 return addressLength;
26             }
27             set
28             {
29                 addressLength = value;
30                 if(addressLength <= 0)
31                 {
32                     Logger.Log(LogLevel.Warning, "Tried to set address length to {0} bytes while decoding operation. Aborting.", addressLength);
33                     return;
34                 }
35                 if(addressLength > 4)
36                 {
37                     Logger.Log(LogLevel.Warning, "Tried to set address length to {0} bytes while decoding operation. Address length was trimmed to 4 bytes.", addressLength);
38                     addressLength = 4;
39                 }
40                 AddressBytes = new byte[addressLength];
41             }
42         }
43 
TryAccumulateAddressAntmicro.Renode.Peripherals.SPI.NORFlash.DecodedOperation44         public bool TryAccumulateAddress(byte data)
45         {
46             AddressBytes[currentAddressByte] = data;
47             currentAddressByte++;
48             if(currentAddressByte == AddressLength)
49             {
50                 ExecutionAddress = BitHelper.ToUInt32(AddressBytes, 0, AddressLength, false);
51                 return true;
52             }
53             return false;
54         }
55 
ToStringAntmicro.Renode.Peripherals.SPI.NORFlash.DecodedOperation56         public override string ToString()
57         {
58             return $"Operation: {Operation}"
59                 .AppendIf(Operation == OperationType.ReadRegister || Operation == OperationType.WriteRegister, $", register: {Register}")
60                 .AppendIf(EraseSize != 0, $", erase size: {EraseSize}")
61                 .AppendIf(AddressLength != 0, $", address length: {AddressLength}")
62                 .ToString();
63         }
64 
65         public enum OperationEraseSize : uint
66         {
67             Die = 1, // starting from 1 to leave 0 as explicitly unused
68             Sector,
69             Subsector32K,
70             Subsector4K
71         }
72 
73         public enum OperationType
74         {
75             None,
76             Read,
77             ReadFast,
78             ReadID,
79             ReadSerialFlashDiscoveryParameter,
80             Program,
81             Erase,
82             ReadRegister,
83             WriteRegister,
84             WriteEnable
85         }
86 
87         public enum OperationState
88         {
89             RecognizeOperation,
90             AccumulateCommandAddressBytes,
91             AccumulateNoDataCommandAddressBytes,
92             HandleCommand,
93             HandleNoDataCommand,
94             HandleImmediateCommand
95         }
96 
97         private byte[] AddressBytes;
98         private int addressLength;
99         private int currentAddressByte;
100     }
101 }
102