2019-06-16 23:34:47 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
2019-07-05 22:05:42 +00:00
|
|
|
|
using System.ComponentModel;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
using System.Diagnostics;
|
2019-07-05 22:05:42 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
2019-10-22 20:52:19 +00:00
|
|
|
|
using Asm65;
|
|
|
|
|
|
2019-07-20 20:28:10 +00:00
|
|
|
|
namespace SourceGen.WpfGui {
|
2019-06-16 23:34:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Edit Address dialog.
|
|
|
|
|
/// </summary>
|
2019-07-05 22:05:42 +00:00
|
|
|
|
public partial class EditAddress : Window, INotifyPropertyChanged {
|
2019-06-16 23:34:47 +00:00
|
|
|
|
/// <summary>
|
2019-12-26 01:15:37 +00:00
|
|
|
|
/// Address typed by user. Only valid after the dialog returns OK. Will be set to
|
|
|
|
|
/// AddressMap.NO_ENTRY_ADDR if the user is attempting to delete the address.
|
2019-06-16 23:34:47 +00:00
|
|
|
|
/// </summary>
|
2019-12-26 01:15:37 +00:00
|
|
|
|
public int NewAddress { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Offset being edited.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int mFirstOffset;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Offset after the end of the selection, or -1 if only one line is selected.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int mNextOffset;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Address after the end of the selection, or -1 if only one line is selected.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int mNextAddress;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Maximum allowed address value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int mMaxAddressValue;
|
|
|
|
|
|
2019-10-22 20:52:19 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// What the address would be if there were no addresses set after the initial one.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int mBaseAddr;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Text formatter.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private Formatter mFormatter;
|
|
|
|
|
|
2019-12-26 01:15:37 +00:00
|
|
|
|
public string FirstOffsetStr {
|
|
|
|
|
get { return mFormatter.FormatOffset24(mFirstOffset); }
|
|
|
|
|
}
|
|
|
|
|
public string NextOffsetStr {
|
|
|
|
|
get { return mFormatter.FormatOffset24(mNextOffset); }
|
|
|
|
|
}
|
|
|
|
|
public string NextAddressStr {
|
|
|
|
|
get { return '$' + mFormatter.FormatAddress(mNextAddress, mNextAddress > 0xffff); }
|
|
|
|
|
}
|
|
|
|
|
public string BytesSelectedStr {
|
|
|
|
|
get {
|
|
|
|
|
int count = mNextOffset - mFirstOffset;
|
|
|
|
|
return count.ToString() + " (" + mFormatter.FormatHexValue(count, 2) + ")";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 23:34:47 +00:00
|
|
|
|
/// <summary>
|
2019-12-26 01:15:37 +00:00
|
|
|
|
/// Address input TextBox.
|
2019-06-16 23:34:47 +00:00
|
|
|
|
/// </summary>
|
2019-12-26 01:15:37 +00:00
|
|
|
|
public string AddressText {
|
|
|
|
|
get { return mAddressText; }
|
|
|
|
|
set { mAddressText = value; OnPropertyChanged(); UpdateControls(); }
|
|
|
|
|
}
|
|
|
|
|
private string mAddressText;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
|
2019-07-05 22:05:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set to true when input is valid. Controls whether the OK button is enabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsValid {
|
|
|
|
|
get { return mIsValid; }
|
2019-10-22 20:52:19 +00:00
|
|
|
|
set { mIsValid = value; OnPropertyChanged(); }
|
2019-07-05 22:05:42 +00:00
|
|
|
|
}
|
|
|
|
|
private bool mIsValid;
|
|
|
|
|
|
2019-12-26 01:15:37 +00:00
|
|
|
|
public Visibility NextAddressVis {
|
|
|
|
|
get { return mNextAddressVis; }
|
|
|
|
|
set { mNextAddressVis = value; OnPropertyChanged(); }
|
2019-10-22 20:52:19 +00:00
|
|
|
|
}
|
2019-12-26 01:15:37 +00:00
|
|
|
|
public Visibility mNextAddressVis = Visibility.Collapsed;
|
|
|
|
|
|
2019-10-22 20:52:19 +00:00
|
|
|
|
public string LoadAddressText {
|
|
|
|
|
get { return mLoadAddressText; }
|
|
|
|
|
set { mLoadAddressText = value; OnPropertyChanged(); }
|
|
|
|
|
}
|
|
|
|
|
public string mLoadAddressText = string.Empty;
|
|
|
|
|
|
2019-07-05 22:05:42 +00:00
|
|
|
|
// INotifyPropertyChanged implementation
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 23:34:47 +00:00
|
|
|
|
|
2019-12-26 01:15:37 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="owner">Parent window.</param>
|
|
|
|
|
/// <param name="firstOffset">Offset at top of selection.</param>
|
|
|
|
|
/// <param name="nextOffset">Offset past bottom of selection, or -1 if only one
|
|
|
|
|
/// line is selected.</param>
|
|
|
|
|
/// <param name="project">Project reference.</param>
|
|
|
|
|
/// <param name="formatter">Text formatter object.</param>
|
|
|
|
|
public EditAddress(Window owner, int firstOffset, int nextOffset, int nextAddr,
|
|
|
|
|
DisasmProject project, Formatter formatter) {
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
Owner = owner;
|
|
|
|
|
DataContext = this;
|
|
|
|
|
|
|
|
|
|
mFirstOffset = firstOffset;
|
|
|
|
|
mNextOffset = nextOffset;
|
|
|
|
|
mNextAddress = nextAddr;
|
2019-10-22 20:52:19 +00:00
|
|
|
|
mFormatter = formatter;
|
2019-12-26 01:15:37 +00:00
|
|
|
|
mMaxAddressValue = project.CpuDef.MaxAddressValue;
|
2019-10-22 20:52:19 +00:00
|
|
|
|
|
2019-12-26 01:15:37 +00:00
|
|
|
|
// Compute load address, i.e. where the byte would have been placed if the entire
|
|
|
|
|
// file were loaded at the address of the first address map entry. We assume
|
|
|
|
|
// offsets wrap at the bank boundary.
|
|
|
|
|
int fileStartAddr = project.AddrMap.OffsetToAddress(0);
|
|
|
|
|
mBaseAddr = ((fileStartAddr + firstOffset) & 0xffff) | (fileStartAddr & 0xff0000);
|
2019-06-16 23:34:47 +00:00
|
|
|
|
|
2019-12-26 01:15:37 +00:00
|
|
|
|
int firstAddr = project.GetAnattrib(firstOffset).Address;
|
|
|
|
|
Debug.Assert(project.AddrMap.OffsetToAddress(firstOffset) == firstAddr);
|
|
|
|
|
|
|
|
|
|
AddressText = Asm65.Address.AddressToString(firstAddr, false);
|
|
|
|
|
|
|
|
|
|
LoadAddressText = '$' + mFormatter.FormatAddress(mBaseAddr, mBaseAddr > 0xffff);
|
|
|
|
|
|
|
|
|
|
if (nextOffset >= 0) {
|
|
|
|
|
NextAddressVis = Visibility.Visible;
|
2019-10-22 20:52:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 01:15:37 +00:00
|
|
|
|
NewAddress = -2;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 22:24:39 +00:00
|
|
|
|
private void Window_ContentRendered(object sender, EventArgs e) {
|
2019-07-07 00:24:42 +00:00
|
|
|
|
addrTextBox.SelectAll();
|
|
|
|
|
addrTextBox.Focus();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 23:34:47 +00:00
|
|
|
|
private void OkButton_Click(object sender, RoutedEventArgs e) {
|
|
|
|
|
if (AddressText.Length == 0) {
|
2019-12-26 01:15:37 +00:00
|
|
|
|
NewAddress = CommonUtil.AddressMap.NO_ENTRY_ADDR;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
} else {
|
2019-12-26 01:15:37 +00:00
|
|
|
|
bool ok = Asm65.Address.ParseAddress(AddressText, mMaxAddressValue, out int addr);
|
|
|
|
|
Debug.Assert(ok);
|
|
|
|
|
NewAddress = addr;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
DialogResult = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles a TextChanged event on the address text box.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Must have UpdateSourceTrigger=PropertyChanged set for this to work. The default
|
|
|
|
|
/// for TextBox is LostFocus.
|
|
|
|
|
/// </remarks>
|
2019-12-26 01:15:37 +00:00
|
|
|
|
private void UpdateControls() {
|
|
|
|
|
IsValid = (AddressText.Length == 0) ||
|
|
|
|
|
Asm65.Address.ParseAddress(AddressText, mMaxAddressValue, out int unused);
|
2019-06-16 23:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-07-05 22:05:42 +00:00
|
|
|
|
// This might be better with validation rules, but it's sort of awkward to pass parameters
|
|
|
|
|
// (like MaxAddressValue) in.
|
2019-06-16 23:34:47 +00:00
|
|
|
|
// https://social.technet.microsoft.com/wiki/contents/articles/31422.wpf-passing-a-data-bound-value-to-a-validation-rule.aspx
|
|
|
|
|
//
|
2019-07-05 22:05:42 +00:00
|
|
|
|
// Speaking of awkward, updating the OK button's IsEnable value through validation
|
|
|
|
|
// requires MultiDataTrigger.
|
2019-06-16 23:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public class AddressValidationRule : ValidationRule {
|
|
|
|
|
// public int MaxAddress { get; set; }
|
|
|
|
|
|
|
|
|
|
// public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
|
|
|
|
|
// string text = value.ToString();
|
|
|
|
|
// Debug.WriteLine("VALIDATE " + text);
|
|
|
|
|
// if ((text.Length == 0) ||
|
|
|
|
|
// Asm65.Address.ParseAddress(text, MaxAddress, out int unused)) {
|
|
|
|
|
// return new ValidationResult(true, null);
|
|
|
|
|
// } else {
|
|
|
|
|
// return new ValidationResult(false, "Invalid address");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
}
|