Skip to content

Commit 0bb8179

Browse files
committed
RamDisk Reowrk
1 parent 5ff7b1a commit 0bb8179

10 files changed

Lines changed: 634 additions & 153 deletions

File tree

ZXBStudio/BuildSystem/ZXProgram.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using ZXBasicStudio.Classes;
77
using ZXBasicStudio.DocumentEditors.ZXRamDisk.Classes;
8+
using ZXBasicStudio.Emulator.Classes;
89

910
namespace ZXBasicStudio.BuildSystem
1011
{
@@ -15,7 +16,7 @@ public class ZXProgram
1516
public ZXMemoryMap? ProgramMap { get; set; }
1617
public ZXMemoryMap? DisassemblyMap { get; set; }
1718
public ZXVariableMap? Variables { get; set; }
18-
public List<ZXRamDisk> RamDisks { get; } = new List<ZXRamDisk>();
19+
public List<ZXBinaryBank> Banks { get; } = new List<ZXBinaryBank>();
1920
public byte[] Binary { get; set; }
2021
public ushort Org { get; set; }
2122
public bool Debug { get; set; }
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel.DataAnnotations;
4+
using System.IO;
45
using System.Linq;
56
using System.Text;
67
using System.Threading.Tasks;
8+
using ZXBasicStudio.Classes;
9+
using ZXBasicStudio.Emulator.Classes;
710

811
namespace ZXBasicStudio.DocumentEditors.ZXRamDisk.Classes
912
{
1013
public class ZXRamDiskFile
1114
{
12-
public required string DiskName { get; set; }
13-
public RamDiskBank Bank { get; set; }
14-
public List<ZXRamDiskContainedFile> Files { get; set; } = new List<ZXRamDiskContainedFile>();
15+
public bool EnableIndirect { get; set; }
16+
public int IndirectBufferSize { get; set; }
17+
public bool RelocateStack { get; set; }
18+
public ZXRamDiskLogicBank[] Banks { get; set; } = new ZXRamDiskLogicBank[]
19+
{
20+
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank4 },
21+
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank6 },
22+
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank1 },
23+
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank3 },
24+
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank7 },
25+
};
1526
}
1627

17-
public class ZXRamDisk
28+
public class ZXRamDiskLogicBank
1829
{
19-
public RamDiskBank Bank { get; set; }
20-
public required byte[] Data { get; set; }
30+
public ZXMemoryBank Bank { get; set; }
31+
public List<ZXRamDiskContainedFile> Files { get; set; } = new List<ZXRamDiskContainedFile>();
2132
}
2233

2334
public class ZXRamDiskContainedFile
2435
{
2536
public required string Name { get; set; }
2637
public required string SourcePath { get; set; }
27-
public required byte[] Content { get; set; }
28-
public int Size => Content?.Length ?? 0;
38+
public byte[] Content { get { return File.ReadAllBytes(Path.Combine(ZXProjectManager.Current.ProjectPath, SourcePath)); } }
39+
public int Size => Content.Length;
2940
}
3041

31-
public enum RamDiskBank
32-
{
33-
Bank1 = 1,
34-
Bank3 = 3,
35-
Bank4 = 4,
36-
Bank6 = 6,
37-
Bank7 = 7
38-
}
42+
3943
}

ZXBStudio/DocumentEditors/ZXRamDisk/Controls/ZXRamDiskEditor.axaml

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,38 @@
66
x:Class="ZXBasicStudio.DocumentEditors.ZXRamDisk.Controls.ZXRamDiskEditor">
77
<Grid RowDefinitions="90,*,40">
88
<Border Margin="5,14,5,5" BorderBrush="White" BorderThickness="1" CornerRadius="5">
9-
<Grid RowDefinitions="5,*" ColumnDefinitions="96,*,64,160">
10-
<TextBlock Grid.Row="1" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Disk name:</TextBlock>
11-
<TextBox Grid.Row="1" Grid.Column="1" Classes="dialog" HorizontalAlignment="Stretch" MaxWidth="Infinity" Margin="5,5,15,5" Name="txtDiskName"></TextBox>
12-
<TextBlock Grid.Row="1" Grid.Column="2" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Target bank:</TextBlock>
13-
<ComboBox Classes="dialog" Grid.Row="1" Grid.Column="3" Name="cbBank" Width="110" SelectedIndex="0" FontSize="10">
14-
<ComboBoxItem>Bank 4</ComboBoxItem>
15-
<ComboBoxItem>Bank 6</ComboBoxItem>
16-
<ComboBoxItem>Bank 1</ComboBoxItem>
17-
<ComboBoxItem>Bank 3</ComboBoxItem>
18-
<ComboBoxItem>Bank 7</ComboBoxItem>
19-
</ComboBox>
9+
<Grid RowDefinitions="5,*" ColumnDefinitions="140,20,148,160,148,20">
10+
<TextBlock Grid.Row="1" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Enable indirect load:</TextBlock>
11+
<CheckBox Grid.Row="1" Grid.Column="1" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right" Name="ckIndirect"></CheckBox>
12+
13+
<TextBlock Grid.Row="1" Grid.Column="2" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Indirect buffer size:</TextBlock>
14+
<Border Height="32" Grid.Row="1" Grid.Column="3" BorderBrush="#ff909090" BorderThickness="2" CornerRadius="5" Padding="-1" Margin="5,0,5,0">
15+
<NumericUpDown Classes="dialog" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ParsingNumberStyle="Integer" Value="64" Minimum="16" Maximum="256" FormatString="####" Name="nudIndSize"></NumericUpDown>
16+
</Border>
17+
<TextBlock Grid.Row="1" Grid.Column="4" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Relocate stack:</TextBlock>
18+
<CheckBox Grid.Row="1" Grid.Column="5" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right" Name="ckRelocate"></CheckBox>
19+
20+
2021
</Grid>
2122
</Border>
2223
<Border Grid.Row="1" Margin="5,14,5,5" BorderBrush="White" BorderThickness="1" CornerRadius="5">
23-
<Grid RowDefinitions="5,24,*" ColumnDefinitions="320,*">
24-
<TextBlock Grid.Row="1" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">Add file</TextBlock>
25-
<TextBlock Grid.Row="1" Grid.Column="1" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">File list</TextBlock>
26-
<ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto" Margin="10">
24+
<Grid RowDefinitions="5,60,24,*" ColumnDefinitions="320,*">
25+
<StackPanel Spacing="10" HorizontalAlignment="Center" Grid.ColumnSpan="2" Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
26+
<TextBlock VerticalAlignment="Center" Margin="0,-10,0,0">Active bank:</TextBlock>
27+
<ComboBox Classes="dialog" Name="cbBank" Width="110" SelectedIndex="0" FontSize="10" Margin="0,-10,0,0">
28+
<ComboBoxItem>Bank 4</ComboBoxItem>
29+
<ComboBoxItem>Bank 6</ComboBoxItem>
30+
<ComboBoxItem>Bank 1</ComboBoxItem>
31+
<ComboBoxItem>Bank 3</ComboBoxItem>
32+
<ComboBoxItem>Bank 7</ComboBoxItem>
33+
</ComboBox>
34+
</StackPanel>
35+
36+
<Separator Grid.Row="1" VerticalAlignment="Bottom" Grid.ColumnSpan="2" Margin="30,0,30,5"></Separator>
37+
38+
<TextBlock Grid.Row="2" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">Add file</TextBlock>
39+
<TextBlock Grid.Row="2" Grid.Column="1" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">File list</TextBlock>
40+
<ScrollViewer Grid.Row="3" VerticalScrollBarVisibility="Auto" Margin="10">
2741
<Grid ColumnDefinitions="56,*,32" RowDefinitions="Auto,Auto,Auto,Auto,16,*">
2842
<TextBlock Classes="dialog" HorizontalAlignment="Right" VerticalAlignment="Center">File:</TextBlock>
2943
<TextBox Grid.Column="1" Classes="dialog" HorizontalAlignment="Stretch" MaxWidth="Infinity" Margin="5,5,0,5" Name="txtFilePath"></TextBox>
@@ -34,7 +48,7 @@
3448
<Button Grid.Row="5" Grid.ColumnSpan="3" Classes="dialog" Margin="10,5,10,5" Height="32" Name="btnAddFile" VerticalAlignment="Bottom">Add file</Button>
3549
</Grid>
3650
</ScrollViewer>
37-
<Grid Grid.Row="2" Grid.Column="1" ColumnDefinitions="*,*,*,48" RowDefinitions="24,28,*,48">
51+
<Grid Grid.Row="3" Grid.Column="1" ColumnDefinitions="*,*,*,48" RowDefinitions="24,28,*,48">
3852
<Border Background="#202020" BorderBrush="White" BorderThickness="1">
3953
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">File name</TextBlock>
4054
</Border>

ZXBStudio/DocumentEditors/ZXRamDisk/Controls/ZXRamDiskEditor.axaml.cs

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Avalonia;
12
using Avalonia.Controls;
23
using Avalonia.Platform.Storage;
34
using AvaloniaEdit.Utils;
@@ -8,6 +9,8 @@
89
using System.Linq;
910
using System.Text.RegularExpressions;
1011
using System.Threading.Tasks;
12+
using ZXBasicStudio.Classes;
13+
using ZXBasicStudio.DebuggingTools.Registers.Binding;
1114
using ZXBasicStudio.DocumentEditors.ZXRamDisk.Classes;
1215
using ZXBasicStudio.DocumentEditors.ZXTapeBuilder.Classes;
1316
using ZXBasicStudio.DocumentModel.Classes;
@@ -18,12 +21,14 @@ namespace ZXBasicStudio.DocumentEditors.ZXRamDisk.Controls
1821
{
1922
public partial class ZXRamDiskEditor : ZXDocumentEditorBase
2023
{
24+
public static StyledProperty<ObservableCollection<ZXRamDiskContainedFile>> FilesProperty = AvaloniaProperty.Register<ZXRamDiskEditor, ObservableCollection<ZXRamDiskContainedFile>>("Files");
25+
2126
#region Private variables
2227
string _docName;
2328
string _docPath;
2429
bool _modified;
2530
bool _internalUpdate;
26-
ObservableCollection<ZXRamDiskContainedFile> _files = new ObservableCollection<ZXRamDiskContainedFile>();
31+
ObservableCollection<ZXRamDiskContainedFile>[] _files = new ObservableCollection<ZXRamDiskContainedFile>[5];
2732
#endregion
2833

2934
#region Events
@@ -34,7 +39,11 @@ public partial class ZXRamDiskEditor : ZXDocumentEditorBase
3439
#endregion
3540

3641
#region Binding properties
37-
public ObservableCollection<ZXRamDiskContainedFile> Files => _files;
42+
public ObservableCollection<ZXRamDiskContainedFile> Files
43+
{
44+
get { return GetValue(FilesProperty); }
45+
set { SetValue(FilesProperty, value); }
46+
}
3847
#endregion
3948

4049
#region ZXDocumentBase properties
@@ -44,16 +53,31 @@ public partial class ZXRamDiskEditor : ZXDocumentEditorBase
4453
#endregion
4554
public ZXRamDiskEditor()
4655
{
56+
for (int buc = 0; buc < 5; buc++)
57+
_files[buc] = new ObservableCollection<ZXRamDiskContainedFile>();
58+
59+
//Order: 4, 6, 1, 3, 7
60+
Files = _files[0];
61+
4762
DataContext = this;
4863
InitializeComponent();
4964
}
5065

5166
public ZXRamDiskEditor(string DocumentPath)
5267
{
68+
for (int buc = 0; buc < 5; buc++)
69+
_files[buc] = new ObservableCollection<ZXRamDiskContainedFile>();
70+
71+
//Order: 4, 6, 1, 3, 7
72+
Files = _files[0];
73+
5374
DataContext = this;
5475
InitializeComponent();
5576

56-
txtDiskName.TextChanged += DocumentChanged;
77+
ckIndirect.IsCheckedChanged += DocumentChanged;
78+
ckRelocate.IsCheckedChanged += DocumentChanged;
79+
nudIndSize.ValueChanged += DocumentChanged;
80+
cbBank.SelectionChanged += ChangeSelectedBank;
5781

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

94+
private void ChangeSelectedBank(object? sender, SelectionChangedEventArgs e)
95+
{
96+
int bank = cbBank.SelectedIndex;
97+
Files = _files[bank];
98+
}
7099

71100
private async void BtnAddFile_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
72101
{
@@ -76,7 +105,9 @@ private async void BtnAddFile_Click(object? sender, Avalonia.Interactivity.Route
76105
return;
77106
}
78107

79-
if (!File.Exists(txtFilePath.Text))
108+
string path = Path.Combine(ZXProjectManager.Current.ProjectPath, txtFilePath.Text);
109+
110+
if (!File.Exists(path))
80111
{
81112
await Window.GetTopLevel(this).ShowError("File not found", "Cannot find the specified file.");
82113
return;
@@ -88,9 +119,9 @@ private async void BtnAddFile_Click(object? sender, Avalonia.Interactivity.Route
88119
return;
89120
}
90121

91-
ZXRamDiskContainedFile file = new ZXRamDiskContainedFile { Name = txtFileName.Text, SourcePath = txtFilePath.Text, Content = File.ReadAllBytes(txtFilePath.Text) };
122+
ZXRamDiskContainedFile file = new ZXRamDiskContainedFile { Name = txtFileName.Text, SourcePath = txtFilePath.Text };
92123

93-
_files.Add(file);
124+
Files.Add(file);
94125

95126
DocumentChanged(this, e);
96127
}
@@ -121,6 +152,8 @@ private async void BtnSelectFile_Click(object? sender, Avalonia.Interactivity.Ro
121152
if (string.IsNullOrWhiteSpace(file))
122153
return;
123154

155+
file = Path.GetRelativePath(ZXProjectManager.Current.ProjectPath, file);
156+
124157
txtFilePath.Text = file;
125158

126159
string fileName = Path.GetFileNameWithoutExtension(txtFilePath.Text);
@@ -132,7 +165,7 @@ private void BtnRemoveFile_Click(object? sender, Avalonia.Interactivity.RoutedEv
132165
{
133166
if (lstFiles.SelectedItem != null)
134167
{
135-
_files.Remove((ZXRamDiskContainedFile)lstFiles.SelectedItem);
168+
Files.Remove((ZXRamDiskContainedFile)lstFiles.SelectedItem);
136169
DocumentChanged(this, e);
137170
}
138171
}
@@ -183,30 +216,18 @@ bool UpdateFileName(string NewPath)
183216

184217
_internalUpdate = true;
185218

186-
txtDiskName.Text = fileContent.DiskName;
219+
ckIndirect.IsChecked = fileContent.EnableIndirect;
220+
ckRelocate.IsChecked = fileContent.RelocateStack;
221+
nudIndSize.Value = fileContent.IndirectBufferSize;
187222

188-
switch (fileContent.Bank)
223+
for (int buc = 0; buc < 5; buc++)
189224
{
190-
case RamDiskBank.Bank1:
191-
cbBank.SelectedIndex = 2;
192-
break;
193-
case RamDiskBank.Bank3:
194-
cbBank.SelectedIndex = 3;
195-
break;
196-
case RamDiskBank.Bank4:
197-
cbBank.SelectedIndex = 0;
198-
break;
199-
case RamDiskBank.Bank6:
200-
cbBank.SelectedIndex = 1;
201-
break;
202-
case RamDiskBank.Bank7:
203-
cbBank.SelectedIndex = 4;
204-
break;
225+
_files[buc].Clear();
226+
_files[buc].AddRange(fileContent.Banks[buc].Files);
205227
}
206228

207-
_files.Clear();
208-
_files.AddRange(fileContent.Files);
209-
229+
cbBank.SelectedIndex = 0;
230+
210231
Task.Run(async () =>
211232
{
212233
await Task.Delay(100);
@@ -219,47 +240,26 @@ bool UpdateFileName(string NewPath)
219240
#region ZXDocumentBase implementation
220241
public override bool SaveDocument(TextWriter OutputLog)
221242
{
222-
if (string.IsNullOrWhiteSpace(txtDiskName.Text))
223-
{
224-
OutputLog.WriteLine("Missing disk name, aborting...");
225-
return false;
226-
}
227-
228-
if(_files.Count == 0)
243+
if(_files.Sum(f => f.Count) == 0)
229244
{
230245
OutputLog.WriteLine("No files on disk, aborting...");
231246
return false;
232247
}
233248

234-
if (_files.Sum(f => f.Size) > 16 * 1024)
249+
if (_files.Any(f => f.Sum(ff => ff.Size) > 16 * 1024))
235250
{
236-
OutputLog.WriteLine("Total size exceeds 16Kb, aborting...");
251+
OutputLog.WriteLine("Bank size exceeds 16Kb, aborting...");
237252
return false;
238253
}
239254

240-
RamDiskBank selBank = new RamDiskBank();
255+
ZXRamDiskFile fileContent = new ZXRamDiskFile();
241256

242-
switch (cbBank.SelectedIndex)
243-
{
244-
case 2:
245-
selBank = RamDiskBank.Bank1;
246-
break;
247-
case 3:
248-
selBank = RamDiskBank.Bank3;
249-
break;
250-
case 0:
251-
selBank = RamDiskBank.Bank4;
252-
break;
253-
case 1:
254-
selBank = RamDiskBank.Bank6;
255-
break;
256-
case 4:
257-
selBank = RamDiskBank.Bank7;
258-
break;
259-
}
257+
for (int buc = 0; buc < 5; buc++)
258+
fileContent.Banks[buc].Files.AddRange(_files[buc]);
260259

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

264264
string content = JsonConvert.SerializeObject(fileContent, Formatting.Indented);
265265
try
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ZXBasicStudio.Emulator.Classes
8+
{
9+
public class ZXBinaryBank
10+
{
11+
public ZXMemoryBank Bank { get; set; }
12+
public required byte[] Data { get; set; }
13+
}
14+
public enum ZXMemoryBank
15+
{
16+
Bank1 = 1,
17+
Bank2 = 2,
18+
Bank3 = 3,
19+
Bank4 = 4,
20+
Bank5 = 5,
21+
Bank6 = 6,
22+
Bank7 = 7
23+
}
24+
25+
}

0 commit comments

Comments
 (0)