Skip to content

Commit

Permalink
RamDisk Reowrk
Browse files Browse the repository at this point in the history
  • Loading branch information
gusmanb committed Jan 15, 2024
1 parent 5ff7b1a commit 0bb8179
Show file tree
Hide file tree
Showing 10 changed files with 634 additions and 153 deletions.
3 changes: 2 additions & 1 deletion ZXBStudio/BuildSystem/ZXProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using ZXBasicStudio.Classes;
using ZXBasicStudio.DocumentEditors.ZXRamDisk.Classes;
using ZXBasicStudio.Emulator.Classes;

namespace ZXBasicStudio.BuildSystem
{
Expand All @@ -15,7 +16,7 @@ public class ZXProgram
public ZXMemoryMap? ProgramMap { get; set; }
public ZXMemoryMap? DisassemblyMap { get; set; }
public ZXVariableMap? Variables { get; set; }
public List<ZXRamDisk> RamDisks { get; } = new List<ZXRamDisk>();
public List<ZXBinaryBank> Banks { get; } = new List<ZXBinaryBank>();
public byte[] Binary { get; set; }
public ushort Org { get; set; }
public bool Debug { get; set; }
Expand Down
36 changes: 20 additions & 16 deletions ZXBStudio/DocumentEditors/ZXRamDisk/Classes/ZXRamDiskFile.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXBasicStudio.Classes;
using ZXBasicStudio.Emulator.Classes;

namespace ZXBasicStudio.DocumentEditors.ZXRamDisk.Classes
{
public class ZXRamDiskFile
{
public required string DiskName { get; set; }
public RamDiskBank Bank { get; set; }
public List<ZXRamDiskContainedFile> Files { get; set; } = new List<ZXRamDiskContainedFile>();
public bool EnableIndirect { get; set; }
public int IndirectBufferSize { get; set; }
public bool RelocateStack { get; set; }
public ZXRamDiskLogicBank[] Banks { get; set; } = new ZXRamDiskLogicBank[]
{
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank4 },
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank6 },
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank1 },
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank3 },
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank7 },
};
}

public class ZXRamDisk
public class ZXRamDiskLogicBank
{
public RamDiskBank Bank { get; set; }
public required byte[] Data { get; set; }
public ZXMemoryBank Bank { get; set; }
public List<ZXRamDiskContainedFile> Files { get; set; } = new List<ZXRamDiskContainedFile>();
}

public class ZXRamDiskContainedFile
{
public required string Name { get; set; }
public required string SourcePath { get; set; }
public required byte[] Content { get; set; }
public int Size => Content?.Length ?? 0;
public byte[] Content { get { return File.ReadAllBytes(Path.Combine(ZXProjectManager.Current.ProjectPath, SourcePath)); } }
public int Size => Content.Length;
}

public enum RamDiskBank
{
Bank1 = 1,
Bank3 = 3,
Bank4 = 4,
Bank6 = 6,
Bank7 = 7
}

}
46 changes: 30 additions & 16 deletions ZXBStudio/DocumentEditors/ZXRamDisk/Controls/ZXRamDiskEditor.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,38 @@
x:Class="ZXBasicStudio.DocumentEditors.ZXRamDisk.Controls.ZXRamDiskEditor">
<Grid RowDefinitions="90,*,40">
<Border Margin="5,14,5,5" BorderBrush="White" BorderThickness="1" CornerRadius="5">
<Grid RowDefinitions="5,*" ColumnDefinitions="96,*,64,160">
<TextBlock Grid.Row="1" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Disk name:</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Classes="dialog" HorizontalAlignment="Stretch" MaxWidth="Infinity" Margin="5,5,15,5" Name="txtDiskName"></TextBox>
<TextBlock Grid.Row="1" Grid.Column="2" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Target bank:</TextBlock>
<ComboBox Classes="dialog" Grid.Row="1" Grid.Column="3" Name="cbBank" Width="110" SelectedIndex="0" FontSize="10">
<ComboBoxItem>Bank 4</ComboBoxItem>
<ComboBoxItem>Bank 6</ComboBoxItem>
<ComboBoxItem>Bank 1</ComboBoxItem>
<ComboBoxItem>Bank 3</ComboBoxItem>
<ComboBoxItem>Bank 7</ComboBoxItem>
</ComboBox>
<Grid RowDefinitions="5,*" ColumnDefinitions="140,20,148,160,148,20">
<TextBlock Grid.Row="1" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Enable indirect load:</TextBlock>
<CheckBox Grid.Row="1" Grid.Column="1" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right" Name="ckIndirect"></CheckBox>

<TextBlock Grid.Row="1" Grid.Column="2" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Indirect buffer size:</TextBlock>
<Border Height="32" Grid.Row="1" Grid.Column="3" BorderBrush="#ff909090" BorderThickness="2" CornerRadius="5" Padding="-1" Margin="5,0,5,0">
<NumericUpDown Classes="dialog" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ParsingNumberStyle="Integer" Value="64" Minimum="16" Maximum="256" FormatString="####" Name="nudIndSize"></NumericUpDown>
</Border>
<TextBlock Grid.Row="1" Grid.Column="4" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Relocate stack:</TextBlock>
<CheckBox Grid.Row="1" Grid.Column="5" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right" Name="ckRelocate"></CheckBox>


</Grid>
</Border>
<Border Grid.Row="1" Margin="5,14,5,5" BorderBrush="White" BorderThickness="1" CornerRadius="5">
<Grid RowDefinitions="5,24,*" ColumnDefinitions="320,*">
<TextBlock Grid.Row="1" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">Add file</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">File list</TextBlock>
<ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto" Margin="10">
<Grid RowDefinitions="5,60,24,*" ColumnDefinitions="320,*">
<StackPanel Spacing="10" HorizontalAlignment="Center" Grid.ColumnSpan="2" Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Margin="0,-10,0,0">Active bank:</TextBlock>
<ComboBox Classes="dialog" Name="cbBank" Width="110" SelectedIndex="0" FontSize="10" Margin="0,-10,0,0">
<ComboBoxItem>Bank 4</ComboBoxItem>
<ComboBoxItem>Bank 6</ComboBoxItem>
<ComboBoxItem>Bank 1</ComboBoxItem>
<ComboBoxItem>Bank 3</ComboBoxItem>
<ComboBoxItem>Bank 7</ComboBoxItem>
</ComboBox>
</StackPanel>

<Separator Grid.Row="1" VerticalAlignment="Bottom" Grid.ColumnSpan="2" Margin="30,0,30,5"></Separator>

<TextBlock Grid.Row="2" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">Add file</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">File list</TextBlock>
<ScrollViewer Grid.Row="3" VerticalScrollBarVisibility="Auto" Margin="10">
<Grid ColumnDefinitions="56,*,32" RowDefinitions="Auto,Auto,Auto,Auto,16,*">
<TextBlock Classes="dialog" HorizontalAlignment="Right" VerticalAlignment="Center">File:</TextBlock>
<TextBox Grid.Column="1" Classes="dialog" HorizontalAlignment="Stretch" MaxWidth="Infinity" Margin="5,5,0,5" Name="txtFilePath"></TextBox>
Expand All @@ -34,7 +48,7 @@
<Button Grid.Row="5" Grid.ColumnSpan="3" Classes="dialog" Margin="10,5,10,5" Height="32" Name="btnAddFile" VerticalAlignment="Bottom">Add file</Button>
</Grid>
</ScrollViewer>
<Grid Grid.Row="2" Grid.Column="1" ColumnDefinitions="*,*,*,48" RowDefinitions="24,28,*,48">
<Grid Grid.Row="3" Grid.Column="1" ColumnDefinitions="*,*,*,48" RowDefinitions="24,28,*,48">
<Border Background="#202020" BorderBrush="White" BorderThickness="1">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">File name</TextBlock>
</Border>
Expand Down
114 changes: 57 additions & 57 deletions ZXBStudio/DocumentEditors/ZXRamDisk/Controls/ZXRamDiskEditor.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using AvaloniaEdit.Utils;
Expand All @@ -8,6 +9,8 @@
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ZXBasicStudio.Classes;
using ZXBasicStudio.DebuggingTools.Registers.Binding;
using ZXBasicStudio.DocumentEditors.ZXRamDisk.Classes;
using ZXBasicStudio.DocumentEditors.ZXTapeBuilder.Classes;
using ZXBasicStudio.DocumentModel.Classes;
Expand All @@ -18,12 +21,14 @@ namespace ZXBasicStudio.DocumentEditors.ZXRamDisk.Controls
{
public partial class ZXRamDiskEditor : ZXDocumentEditorBase
{
public static StyledProperty<ObservableCollection<ZXRamDiskContainedFile>> FilesProperty = AvaloniaProperty.Register<ZXRamDiskEditor, ObservableCollection<ZXRamDiskContainedFile>>("Files");

#region Private variables
string _docName;
string _docPath;
bool _modified;
bool _internalUpdate;
ObservableCollection<ZXRamDiskContainedFile> _files = new ObservableCollection<ZXRamDiskContainedFile>();
ObservableCollection<ZXRamDiskContainedFile>[] _files = new ObservableCollection<ZXRamDiskContainedFile>[5];
#endregion

#region Events
Expand All @@ -34,7 +39,11 @@ public partial class ZXRamDiskEditor : ZXDocumentEditorBase
#endregion

#region Binding properties
public ObservableCollection<ZXRamDiskContainedFile> Files => _files;
public ObservableCollection<ZXRamDiskContainedFile> Files
{
get { return GetValue(FilesProperty); }
set { SetValue(FilesProperty, value); }
}
#endregion

#region ZXDocumentBase properties
Expand All @@ -44,16 +53,31 @@ public partial class ZXRamDiskEditor : ZXDocumentEditorBase
#endregion
public ZXRamDiskEditor()
{
for (int buc = 0; buc < 5; buc++)
_files[buc] = new ObservableCollection<ZXRamDiskContainedFile>();

//Order: 4, 6, 1, 3, 7
Files = _files[0];

DataContext = this;
InitializeComponent();
}

public ZXRamDiskEditor(string DocumentPath)
{
for (int buc = 0; buc < 5; buc++)
_files[buc] = new ObservableCollection<ZXRamDiskContainedFile>();

//Order: 4, 6, 1, 3, 7
Files = _files[0];

DataContext = this;
InitializeComponent();

txtDiskName.TextChanged += DocumentChanged;
ckIndirect.IsCheckedChanged += DocumentChanged;
ckRelocate.IsCheckedChanged += DocumentChanged;
nudIndSize.ValueChanged += DocumentChanged;
cbBank.SelectionChanged += ChangeSelectedBank;

btnSelectFile.Click += BtnSelectFile_Click;
btnAddFile.Click += BtnAddFile_Click;
Expand All @@ -67,6 +91,11 @@ public ZXRamDiskEditor(string DocumentPath)
throw new Exception("Error opening document");
}

private void ChangeSelectedBank(object? sender, SelectionChangedEventArgs e)
{
int bank = cbBank.SelectedIndex;
Files = _files[bank];
}

private async void BtnAddFile_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
Expand All @@ -76,7 +105,9 @@ private async void BtnAddFile_Click(object? sender, Avalonia.Interactivity.Route
return;
}

if (!File.Exists(txtFilePath.Text))
string path = Path.Combine(ZXProjectManager.Current.ProjectPath, txtFilePath.Text);

if (!File.Exists(path))
{
await Window.GetTopLevel(this).ShowError("File not found", "Cannot find the specified file.");
return;
Expand All @@ -88,9 +119,9 @@ private async void BtnAddFile_Click(object? sender, Avalonia.Interactivity.Route
return;
}

ZXRamDiskContainedFile file = new ZXRamDiskContainedFile { Name = txtFileName.Text, SourcePath = txtFilePath.Text, Content = File.ReadAllBytes(txtFilePath.Text) };
ZXRamDiskContainedFile file = new ZXRamDiskContainedFile { Name = txtFileName.Text, SourcePath = txtFilePath.Text };

_files.Add(file);
Files.Add(file);

DocumentChanged(this, e);
}
Expand Down Expand Up @@ -121,6 +152,8 @@ private async void BtnSelectFile_Click(object? sender, Avalonia.Interactivity.Ro
if (string.IsNullOrWhiteSpace(file))
return;

file = Path.GetRelativePath(ZXProjectManager.Current.ProjectPath, file);

txtFilePath.Text = file;

string fileName = Path.GetFileNameWithoutExtension(txtFilePath.Text);
Expand All @@ -132,7 +165,7 @@ private void BtnRemoveFile_Click(object? sender, Avalonia.Interactivity.RoutedEv
{
if (lstFiles.SelectedItem != null)
{
_files.Remove((ZXRamDiskContainedFile)lstFiles.SelectedItem);
Files.Remove((ZXRamDiskContainedFile)lstFiles.SelectedItem);
DocumentChanged(this, e);
}
}
Expand Down Expand Up @@ -183,30 +216,18 @@ bool UpdateFileName(string NewPath)

_internalUpdate = true;

txtDiskName.Text = fileContent.DiskName;
ckIndirect.IsChecked = fileContent.EnableIndirect;
ckRelocate.IsChecked = fileContent.RelocateStack;
nudIndSize.Value = fileContent.IndirectBufferSize;

switch (fileContent.Bank)
for (int buc = 0; buc < 5; buc++)
{
case RamDiskBank.Bank1:
cbBank.SelectedIndex = 2;
break;
case RamDiskBank.Bank3:
cbBank.SelectedIndex = 3;
break;
case RamDiskBank.Bank4:
cbBank.SelectedIndex = 0;
break;
case RamDiskBank.Bank6:
cbBank.SelectedIndex = 1;
break;
case RamDiskBank.Bank7:
cbBank.SelectedIndex = 4;
break;
_files[buc].Clear();
_files[buc].AddRange(fileContent.Banks[buc].Files);
}

_files.Clear();
_files.AddRange(fileContent.Files);

cbBank.SelectedIndex = 0;

Task.Run(async () =>
{
await Task.Delay(100);
Expand All @@ -219,47 +240,26 @@ bool UpdateFileName(string NewPath)
#region ZXDocumentBase implementation
public override bool SaveDocument(TextWriter OutputLog)
{
if (string.IsNullOrWhiteSpace(txtDiskName.Text))
{
OutputLog.WriteLine("Missing disk name, aborting...");
return false;
}

if(_files.Count == 0)
if(_files.Sum(f => f.Count) == 0)
{
OutputLog.WriteLine("No files on disk, aborting...");
return false;
}

if (_files.Sum(f => f.Size) > 16 * 1024)
if (_files.Any(f => f.Sum(ff => ff.Size) > 16 * 1024))
{
OutputLog.WriteLine("Total size exceeds 16Kb, aborting...");
OutputLog.WriteLine("Bank size exceeds 16Kb, aborting...");
return false;
}

RamDiskBank selBank = new RamDiskBank();
ZXRamDiskFile fileContent = new ZXRamDiskFile();

switch (cbBank.SelectedIndex)
{
case 2:
selBank = RamDiskBank.Bank1;
break;
case 3:
selBank = RamDiskBank.Bank3;
break;
case 0:
selBank = RamDiskBank.Bank4;
break;
case 1:
selBank = RamDiskBank.Bank6;
break;
case 4:
selBank = RamDiskBank.Bank7;
break;
}
for (int buc = 0; buc < 5; buc++)
fileContent.Banks[buc].Files.AddRange(_files[buc]);

//TODO: Make paths relative to project
ZXRamDiskFile fileContent = new ZXRamDiskFile { DiskName = txtDiskName.Text, Bank = selBank, Files = _files.ToList() };
fileContent.IndirectBufferSize = (int)(nudIndSize.Value ?? 0);
fileContent.EnableIndirect = ckIndirect.IsChecked ?? false;
fileContent.RelocateStack = ckRelocate.IsChecked ?? false;

string content = JsonConvert.SerializeObject(fileContent, Formatting.Indented);
try
Expand Down
25 changes: 25 additions & 0 deletions ZXBStudio/Emulator/Classes/ZXBinaryBank.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZXBasicStudio.Emulator.Classes
{
public class ZXBinaryBank
{
public ZXMemoryBank Bank { get; set; }
public required byte[] Data { get; set; }
}
public enum ZXMemoryBank
{
Bank1 = 1,
Bank2 = 2,
Bank3 = 3,
Bank4 = 4,
Bank5 = 5,
Bank6 = 6,
Bank7 = 7
}

}
Loading

0 comments on commit 0bb8179

Please sign in to comment.