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 9 namespace Antmicro.Renode.Peripherals.Timers 10 { 11 public static class BCDHelper 12 { EncodeToBCD(byte numericValue)13 public static byte EncodeToBCD(byte numericValue) 14 { 15 if(numericValue > 99 || numericValue < 0) 16 { 17 throw new ArgumentException($"Invalid value: {numericValue}"); 18 } 19 20 var nibbleLow = numericValue % 10; 21 var nibbleHigh = numericValue / 10; 22 return (byte)((nibbleHigh << 4) | nibbleLow); 23 } 24 DecodeFromBCD(byte bcdValue)25 public static byte DecodeFromBCD(byte bcdValue) 26 { 27 var units = bcdValue & 0x0F; 28 var tens = (bcdValue & 0xF0) >> 4; 29 return (byte)((tens * 10) + units); 30 } 31 } 32 } 33