From 268ad18067e08a5de20e2141e5bdec3bde6646e3 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Fri, 16 Aug 2019 15:38:31 -0700 Subject: [PATCH] Add C64 character conversions to hex dump viewer The conversion mode enum was replaced, so we will lose the previous combo box setting after an upgrade. --- Asm65/Formatter.cs | 39 ++++++++++++++---- SourceGen/ProjectProperties.cs | 2 + SourceGen/Tools/WpfGui/HexDumpViewer.xaml | 10 +++-- SourceGen/Tools/WpfGui/HexDumpViewer.xaml.cs | 43 +++++--------------- 4 files changed, 50 insertions(+), 44 deletions(-) diff --git a/Asm65/Formatter.cs b/Asm65/Formatter.cs index 5c08447..fbbeb90 100644 --- a/Asm65/Formatter.cs +++ b/Asm65/Formatter.cs @@ -81,7 +81,14 @@ namespace Asm65 { // hex dumps public bool mHexDumpAsciiOnly; // disallow non-ASCII chars in hex dumps? - public enum CharConvMode { Unknown = 0, PlainAscii, HighLowAscii }; + public enum CharConvMode { + // TODO(maybe): just pass in a CharEncoding.Convert delegate + Unknown = 0, + Ascii, + LowHighAscii, + C64Petscii, + C64ScreenCode + }; public CharConvMode mHexDumpCharConvMode; // character conversion mode for dumps // This determines what operators are available and what their precedence is. @@ -321,6 +328,8 @@ namespace Asm65 { // Buffer to use when generating hex dump lines. private char[] mHexDumpBuffer; + private CharEncoding.Convert mHexDumpCharConv; + /// /// A 16-character array with 0-9a-f, for hex conversions. The letters will be /// upper or lower case, per the format config. @@ -430,6 +439,25 @@ namespace Asm65 { Debug.WriteLine("NOTE: char delimiters not set"); chrDelim = DelimiterSet.GetDefaultCharDelimiters(); } + + switch (mFormatConfig.mHexDumpCharConvMode) { + case FormatConfig.CharConvMode.Ascii: + mHexDumpCharConv = CharEncoding.ConvertAscii; + break; + case FormatConfig.CharConvMode.LowHighAscii: + mHexDumpCharConv = CharEncoding.ConvertLowAndHighAscii; + break; + case FormatConfig.CharConvMode.C64Petscii: + mHexDumpCharConv = CharEncoding.ConvertC64Petscii; + break; + case FormatConfig.CharConvMode.C64ScreenCode: + mHexDumpCharConv = CharEncoding.ConvertC64ScreenCode; + break; + default: + // most some things don't configure the hex dump; this is fine + mHexDumpCharConv = CharEncoding.ConvertLowAndHighAscii; + break; + } } /// @@ -958,13 +986,8 @@ namespace Asm65 { /// Value to convert. /// Printable character. private char CharConv(byte val) { - char ch; - if (mFormatConfig.mHexDumpCharConvMode == FormatConfig.CharConvMode.HighLowAscii) { - ch = (char)(val & 0x7f); - } else { - ch = (char)val; - } - if (CommonUtil.TextUtil.IsPrintableAscii(ch)) { + char ch = mHexDumpCharConv(val); + if (ch != CharEncoding.UNPRINTABLE_CHAR) { return ch; } else if (mFormatConfig.mHexDumpAsciiOnly) { return '.'; diff --git a/SourceGen/ProjectProperties.cs b/SourceGen/ProjectProperties.cs index 2d34f5e..d8f4b80 100644 --- a/SourceGen/ProjectProperties.cs +++ b/SourceGen/ProjectProperties.cs @@ -36,6 +36,8 @@ namespace SourceGen { /// Some parameters we feed to the analyzers. /// public class AnalysisParameters { + // This is very similar to Formatter.FormatConfig.CharConvMode, but it serves + // a different purpose and might diverge in the future. public enum TextScanMode { Unknown = 0, LowAscii, diff --git a/SourceGen/Tools/WpfGui/HexDumpViewer.xaml b/SourceGen/Tools/WpfGui/HexDumpViewer.xaml index 0d97fa7..02c1ec4 100644 --- a/SourceGen/Tools/WpfGui/HexDumpViewer.xaml +++ b/SourceGen/Tools/WpfGui/HexDumpViewer.xaml @@ -23,13 +23,15 @@ limitations under the License. xmlns:local="clr-namespace:SourceGen.WpfGui" mc:Ignorable="d" Title="Hex Dump Viewer" - Width="542" Height="600" MinWidth="542" MinHeight="180" + Width="542" Height="600" MinWidth="542" MinHeight="180" ResizeMode="CanResizeWithGrip" ShowInTaskbar="True" Loaded="Window_Loaded"> - Basic ASCII - High/Low ASCII + Plain ASCII only + Low/High ASCII + C64 PETSCII + C64 Screen Code @@ -54,7 +56,7 @@ limitations under the License. ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Visible"> - diff --git a/SourceGen/Tools/WpfGui/HexDumpViewer.xaml.cs b/SourceGen/Tools/WpfGui/HexDumpViewer.xaml.cs index 69f54d1..488af83 100644 --- a/SourceGen/Tools/WpfGui/HexDumpViewer.xaml.cs +++ b/SourceGen/Tools/WpfGui/HexDumpViewer.xaml.cs @@ -21,6 +21,7 @@ using System.Windows; using System.Windows.Controls; using Asm65; +using CharConvMode = Asm65.Formatter.FormatConfig.CharConvMode; namespace SourceGen.Tools.WpfGui { /// @@ -67,16 +68,6 @@ namespace SourceGen.Tools.WpfGui { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } - /// - /// Character conversion modes. These determine how we interpret bytes for the - /// ASCII portion of the dump. - /// - public enum CharConvMode { - Unknown = 0, - PlainAscii, - HighLowAscii - } - /// /// Character conversion combo box item. /// @@ -103,10 +94,14 @@ namespace SourceGen.Tools.WpfGui { mFormatter = formatter; CharConvItems = new CharConvItem[] { - new CharConvItem((string)FindResource("str_PlainAscii"), - CharConvMode.PlainAscii), - new CharConvItem((string)FindResource("str_HighLowAscii"), - CharConvMode.HighLowAscii), + new CharConvItem((string)FindResource("str_AsciiOnly"), + CharConvMode.Ascii), + new CharConvItem((string)FindResource("str_LowHighAscii"), + CharConvMode.LowHighAscii), + new CharConvItem((string)FindResource("str_C64Petscii"), + CharConvMode.C64Petscii), + new CharConvItem((string)FindResource("str_C64ScreenCode"), + CharConvMode.C64ScreenCode), }; } @@ -116,7 +111,7 @@ namespace SourceGen.Tools.WpfGui { // Restore conv mode setting. CharConvMode mode = (CharConvMode)AppSettings.Global.GetEnum( - AppSettings.HEXD_CHAR_CONV, typeof(CharConvMode), (int)CharConvMode.PlainAscii); + AppSettings.HEXD_CHAR_CONV, typeof(CharConvMode), (int)CharConvMode.Ascii); int index = 0; for (int i = 0; i < CharConvItems.Length; i++) { if (CharConvItems[i].Mode == mode) { @@ -127,11 +122,6 @@ namespace SourceGen.Tools.WpfGui { charConvComboBox.SelectedIndex = index; } - //private void Window_Closing(object sender, EventArgs e) { - // Debug.WriteLine("Window width: " + ActualWidth); - // Debug.WriteLine("Column width: " + hexDumpData.Columns[0].ActualWidth); - //} - /// /// Sets the filename associated with the data. This is for display purposes only. /// @@ -152,18 +142,7 @@ namespace SourceGen.Tools.WpfGui { // initializing return; } - - switch (item.Mode) { - case CharConvMode.PlainAscii: - config.mHexDumpCharConvMode = Formatter.FormatConfig.CharConvMode.PlainAscii; - break; - case CharConvMode.HighLowAscii: - config.mHexDumpCharConvMode = Formatter.FormatConfig.CharConvMode.HighLowAscii; - break; - default: - Debug.Assert(false); - break; - } + config.mHexDumpCharConvMode = item.Mode; config.mHexDumpAsciiOnly = AsciiOnlyDump;