Skip to content

Commit

Permalink
remove '0' prefix from hex and binary numbers
Browse files Browse the repository at this point in the history
small improvement to waveform number printing
  • Loading branch information
tomcl committed Aug 21, 2024
1 parent 167e7c0 commit f31c21d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
20 changes: 12 additions & 8 deletions src/Renderer/Simulator/NumberHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ let private hexToBin (hStr: string) : string =
/// if width = 0 work ok with no padding
let addCommasAndZeros (width: int) (printedChars: string) =
let divCeiling n divisor = (n - 1 + divisor) / divisor
let nonZeroDigits = printedChars[2 .. printedChars.Length - 1]
let nonZeroDigits = printedChars[1 .. printedChars.Length - 1]
let numDigits = nonZeroDigits.Length

let bitsPerDigit =
match printedChars[1] with
match printedChars[0] with
| 'x' -> 4
| 'b' -> 1
| r -> failwithf "Wrong use of addZeros: radix Char = %c" r
Expand Down Expand Up @@ -109,11 +109,11 @@ let addZeros64 (width: int) (pFun: int64 -> string) (n: int64) = pFun n |> addCo

let addZeros (width: int) (pFun: int -> string) (n: int) = pFun n |> addCommasAndZeros width

let hex64 (num: int64) = "0x" + num.ToString("X")
let hex64 (num: int64) = "x" + num.ToString("X")

let fillHex64 width = addZeros64 width hex64

let bin64 (num: int64) = "0b" + (hexToBin <| num.ToString("X"))
let bin64 (num: int64) = "b" + (hexToBin <| num.ToString("X"))
let sDec64 (num: int64) = num.ToString()
let dec64 (num: int64) = (uint64 num).ToString()

Expand Down Expand Up @@ -211,6 +211,10 @@ let valToPaddedString (width: int) (radix: NumberBase) (value: int64) : string =
match radix with
| Dec -> dec64 value
| Bin -> fillBin64 width value
| Hex when width <= 4 ->
match value with
| x when x < 10 -> string (char (int '0' + int x))
| x -> string (char (int 'A' + int x - 10))
| Hex
| Bin -> fillHex64 width value
| SDec -> sDec64 value
Expand All @@ -224,10 +228,10 @@ let private padToWidth width (bits: WireData) : WireData =

let fastDataToPaddedString maxChars radix (fd: FastData) =
let getPrefixAndDigits (s: string) =
match s.Length, s.[0], s.[1] with
| n, '-', _ -> "-", s[1 .. n - 1]
| n, '0', 'b'
| n, '0', 'x' -> s.[1..1], s[2 .. n - 1]
match s.Length, s.[0] with
| n, '-' -> "-", s[1 .. n - 1]
| n, 'b'
| n, 'x' -> s.[0..0], s[1 .. n - 1]
| _ -> "", s

let stripLeadingZeros s =
Expand Down
15 changes: 9 additions & 6 deletions src/Renderer/UI/WaveSim/WaveSimSVGs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ let displayUInt32OnWave
: list<ReactElement> =
let textFont = wsModel.WSConfig.FontSize
let textWeight = wsModel.WSConfig.FontWeight
let textSpec = {DrawHelpers.defaultText with FontSize = $"{textFont}"; FontWeight = $"{textWeight}"}

// find all clock cycles where there is a NonBinaryTransition.Change
let changeTransitions =
Expand Down Expand Up @@ -293,7 +294,7 @@ let displayUInt32OnWave
// calculate display widths
let cycleWidth = singleWaveWidth wsModel
let gapWidth = (float gap.Length * cycleWidth) - 2. * Constants.nonBinaryTransLen
let singleWidth = 1.1 * DrawHelpers.getTextWidthInPixels Constants.valueOnWaveText waveValue
let singleWidth = 1.1 * DrawHelpers.getTextWidthInPixels textSpec waveValue
let doubleWidth = 2. * singleWidth + Constants.valueOnWavePadding

match gapWidth with
Expand All @@ -311,8 +312,8 @@ let displayUInt32OnWave
then (cycleWidth - singleWidth) / 2.
else Constants.valueOnWaveEdgePadding
let startPadWidth =
if singleCycleCenterPadWidth < 0.1 * DrawHelpers.getTextWidthInPixels Constants.valueOnWaveText waveValue
then 0.1 * DrawHelpers.getTextWidthInPixels Constants.valueOnWaveText waveValue
if singleCycleCenterPadWidth < 0.1 * DrawHelpers.getTextWidthInPixels textSpec waveValue
then 0.1 * DrawHelpers.getTextWidthInPixels textSpec waveValue
else singleCycleCenterPadWidth
let endPadWidth = (float gap.Length * cycleWidth - startPadWidth - singleWidth)
let startText = makeTextElement (float gap.Start * cycleWidth + startPadWidth) waveValue
Expand All @@ -333,6 +334,8 @@ let displayBigIntOnWave
: list<ReactElement> =
let textFont = wsModel.WSConfig.FontSize
let textWeight = wsModel.WSConfig.FontWeight
let textSpec = {Constants.valueOnWaveText with FontSize = $"{textFont}"; FontWeight = $"{textWeight}"}

// find all clock cycles where there is a NonBinaryTransition.Change
let changeTransitions =
transitions
Expand Down Expand Up @@ -378,7 +381,7 @@ let displayBigIntOnWave
// calculate display widths
let cycleWidth = singleWaveWidth wsModel
let gapWidth = (float gap.Length * cycleWidth) - 2. * Constants.nonBinaryTransLen
let singleWidth = 1.1 * DrawHelpers.getTextWidthInPixels Constants.valueOnWaveText waveValue
let singleWidth = 1.1 * DrawHelpers.getTextWidthInPixels textSpec waveValue
let doubleWidth = 2. * singleWidth + Constants.valueOnWavePadding

match gapWidth with
Expand All @@ -396,8 +399,8 @@ let displayBigIntOnWave
then (cycleWidth - singleWidth) / 2.
else Constants.valueOnWaveEdgePadding
let startPadWidth =
if singleCycleCenterPadWidth < 0.1 * DrawHelpers.getTextWidthInPixels Constants.valueOnWaveText waveValue
then 0.1 * DrawHelpers.getTextWidthInPixels Constants.valueOnWaveText waveValue
if singleCycleCenterPadWidth < 0.1 * DrawHelpers.getTextWidthInPixels textSpec waveValue
then 0.1 * DrawHelpers.getTextWidthInPixels textSpec waveValue
else singleCycleCenterPadWidth
let endPadWidth = (float gap.Length * cycleWidth - startPadWidth - singleWidth)
let startText = makeTextElement (float gap.Start * cycleWidth + startPadWidth) waveValue
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/UI/WaveSim/WaveSimStyle.fs
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,9 @@ let calcNamesColWidth (ws:WaveSimModel) : int =

/// List of Style properties for columns in wave viewer.
let waveSimColumn = [
BorderTop Constants.borderProperties
Height "100%"
Width "100%"
BorderTop Constants.borderProperties
Display DisplayOptions.Grid
GridAutoRows Constants.rowHeight
FontSize Constants.valueColumnFontSize
Expand Down

0 comments on commit f31c21d

Please sign in to comment.