Skip to content

Commit

Permalink
RAM disk system ready for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gusmanb committed Jan 16, 2024
1 parent 0bb8179 commit 565739b
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 16 deletions.
5 changes: 5 additions & 0 deletions ZXBStudio/DocumentEditors/NextDows/log/ExportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ namespace ZXBasicStudio.DocumentEditors.NextDows.log
/// </summary>
public class ExportManager : IZXDocumentBuilder
{

public Guid Id => Guid.Parse("2372bad1-0176-4881-8c2e-3b8c506cbaaf");

public Guid[]? DependsOn => null;

private FileTypes fileType = FileTypes.Undefined;

public bool Initialize(FileTypes fileType)
Expand Down
4 changes: 4 additions & 0 deletions ZXBStudio/DocumentEditors/ZXGraphics/log/ExportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ namespace ZXBasicStudio.DocumentEditors.ZXGraphics.log
/// </summary>
public class ExportManager : IZXDocumentBuilder
{
public Guid Id => Guid.Parse("4596ac56-9cd4-4553-b191-acdaa5ee3898");

public Guid[]? DependsOn => null;

private FileTypes fileType = FileTypes.Undefined;

public bool Initialize(FileTypes fileType)
Expand Down
7 changes: 4 additions & 3 deletions ZXBStudio/DocumentEditors/ZXRamDisk/Classes/ZXRamDiskFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ namespace ZXBasicStudio.DocumentEditors.ZXRamDisk.Classes
{
public class ZXRamDiskFile
{
public bool EnableIndirect { get; set; }
public int IndirectBufferSize { get; set; }
public bool RelocateStack { get; set; }
public bool EnableIndirect { get; set; } = false;
public int IndirectBufferSize { get; set; } = 64;
public bool RelocateStack { get; set; } = false;
public bool PreserveBin { get; set; } = false;
public ZXRamDiskLogicBank[] Banks { get; set; } = new ZXRamDiskLogicBank[]
{
new ZXRamDiskLogicBank{ Bank = ZXMemoryBank.Bank4 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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="140,20,148,160,148,20">
<Grid RowDefinitions="5,*" ColumnDefinitions="140,20,130,80,100,20,120,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>

Expand All @@ -16,8 +16,9 @@
</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>



<TextBlock Grid.Row="1" Grid.Column="6" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Preseve binaries:</TextBlock>
<CheckBox Grid.Row="1" Grid.Column="7" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right" Name="ckPreserve"></CheckBox>
</Grid>
</Border>
<Border Grid.Row="1" Margin="5,14,5,5" BorderBrush="White" BorderThickness="1" CornerRadius="5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public ZXRamDiskEditor(string DocumentPath)
ckIndirect.IsCheckedChanged += DocumentChanged;
ckRelocate.IsCheckedChanged += DocumentChanged;
nudIndSize.ValueChanged += DocumentChanged;
ckPreserve.IsCheckedChanged += DocumentChanged;

cbBank.SelectionChanged += ChangeSelectedBank;

btnSelectFile.Click += BtnSelectFile_Click;
Expand Down Expand Up @@ -219,6 +221,7 @@ bool UpdateFileName(string NewPath)
ckIndirect.IsChecked = fileContent.EnableIndirect;
ckRelocate.IsChecked = fileContent.RelocateStack;
nudIndSize.Value = fileContent.IndirectBufferSize;
ckPreserve.IsChecked = fileContent.PreserveBin;

for (int buc = 0; buc < 5; buc++)
{
Expand Down Expand Up @@ -260,6 +263,7 @@ public override bool SaveDocument(TextWriter OutputLog)
fileContent.IndirectBufferSize = (int)(nudIndSize.Value ?? 0);
fileContent.EnableIndirect = ckIndirect.IsChecked ?? false;
fileContent.RelocateStack = ckRelocate.IsChecked ?? false;
fileContent.PreserveBin = ckPreserve.IsChecked ?? false;

string content = JsonConvert.SerializeObject(fileContent, Formatting.Indented);
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ public class ZXTapeBuilderFile
public string? ScreenName { get; set; }
public string? ScreenFile { get; set; }
public ZXTapeBuilderDataBlock[]? DataBlocks { get; set; }
public bool IncludeRAMDisk { get; set; }
public ZXRAMDiskOrder RAMDiskOrder { get; set; }
}

public enum ZXRAMDiskOrder
{
Before,
After
}

public class ZXTapeBuilderPoke
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<UserControl.Resources>
<local:BoolToColorConverter x:Key="BoolToColorConverter" />
</UserControl.Resources>
<Grid RowDefinitions="160,120,*,40">
<Grid RowDefinitions="160,120,*,60,40">
<Border Margin="5,14,5,5" BorderBrush="White" BorderThickness="1" CornerRadius="5">
<Grid RowDefinitions="5,*,*,*" ColumnDefinitions="96,*">
<TextBlock Grid.Row="1" Classes="dialog" VerticalAlignment="Center" HorizontalAlignment="Right">Program name:</TextBlock>
Expand Down Expand Up @@ -78,8 +78,8 @@
<Grid RowDefinitions="5,24,*" ColumnDefinitions="320,*">
<TextBlock Grid.Row="1" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">Add block</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Classes="dialog" HorizontalAlignment="Center" VerticalAlignment="Center">Block list</TextBlock>
<ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto" Margin="10">
<Grid ColumnDefinitions="56,*,32" RowDefinitions="Auto,Auto,Auto,Auto,16,*">
<ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto" Margin="10,5,10,10">
<Grid ColumnDefinitions="56,*,32" RowDefinitions="Auto,Auto,Auto,Auto,*">
<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="txtBlockFile"></TextBox>
<Button Grid.Column="2" Classes="dialog" Margin="10,5,10,5" Width="32" Height="32" Name="btnSelectBlock">...</Button>
Expand All @@ -91,7 +91,7 @@
</Border>
<TextBlock Grid.Row="3" Classes="dialog" HorizontalAlignment="Right" VerticalAlignment="Center">Basic load:</TextBlock>
<CheckBox Classes="dialog" Grid.Row="3" Grid.Column="1" Margin="10" Name="ckBasicLoad"></CheckBox>
<Button Grid.Row="5" Grid.ColumnSpan="3" Classes="dialog" Margin="10,5,10,5" Height="32" Name="btnAddBlock" VerticalAlignment="Bottom">Add block</Button>
<Button Grid.Row="4" Grid.ColumnSpan="3" Classes="dialog" Margin="10,5,10,5" Height="32" Name="btnAddBlock" VerticalAlignment="Bottom">Add block</Button>
</Grid>
</ScrollViewer>
<Grid Grid.Row="2" Grid.Column="1" ColumnDefinitions="*,*,*,48" RowDefinitions="24,28,*,48">
Expand Down Expand Up @@ -128,7 +128,19 @@
</Grid>
</Border>
<TextBlock Grid.Row="2" Margin="15,0,0,0" Padding="5" Background="#202020" HorizontalAlignment="Left" VerticalAlignment="Top">Data blocks</TextBlock>
<Grid ColumnDefinitions="*,*" Grid.Row="3">
<Border Grid.Row="3" Margin="5,14,5,5" BorderBrush="White" BorderThickness="1" CornerRadius="5">
<Grid ColumnDefinitions="140,32,100,150,*">
<TextBlock VerticalAlignment="Center" Margin="15,0,0,0" HorizontalAlignment="Right">Include RAM disk blocks:</TextBlock>
<CheckBox Name="ckRAMDisk" Grid.Column="1" VerticalAlignment="Center" Margin="5" HorizontalAlignment="Left"></CheckBox>
<TextBlock Grid.Column="2" VerticalAlignment="Center" Margin="15,0,0,0" HorizontalAlignment="Right">Inclusion order:</TextBlock>
<ComboBox Grid.Column="3" Classes="dialog" SelectedIndex="0" Margin="5,0,0,0" Name="cbRAMDiskOrder">
<ComboBoxItem>Before data blocks</ComboBoxItem>
<ComboBoxItem>After data blocks</ComboBoxItem>
</ComboBox>
</Grid>
</Border>
<TextBlock Grid.Row="3" Margin="15,0,0,0" Padding="5" Background="#202020" HorizontalAlignment="Left" VerticalAlignment="Top">RAM disk</TextBlock>
<Grid ColumnDefinitions="*,*" Grid.Row="4">
<Button Classes="dialog" Name="btnSave">Save changes</Button>
<Button Classes="dialog" Grid.Column="1" Name="btnDiscard">Discard changes</Button>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public ZXTapeBuilderEditor(string DocumentPath)
txtPokesAfter.TextChanged += DocumentChanged;
txtScreenName.TextChanged += DocumentChanged;
txtScreenFile.TextChanged += DocumentChanged;
ckRAMDisk.IsCheckedChanged += DocumentChanged;
cbRAMDiskOrder.SelectionChanged += DocumentChanged;

btnSelectScreen.Click += BtnSelectScreen_Click;
btnSelectBlock.Click += BtnSelectBlock_Click;
Expand Down Expand Up @@ -278,7 +280,7 @@ bool UpdateFileName(string NewPath)
cbPaper.SelectedIndex = fileContent.Paper;
ckBorder.IsChecked = fileContent.UseBorder;
cbBorder.SelectedIndex = fileContent.Border;

if (fileContent.PokesBeforeLoad != null)
{
txtPokesBefore.Text = string.Join(';', fileContent.PokesBeforeLoad.Select(p => p.Address.ToString() + "," + p.Value.ToString()));
Expand Down Expand Up @@ -307,6 +309,13 @@ bool UpdateFileName(string NewPath)
_blocks.Add(block);
}

//RAM disk
ckRAMDisk.IsChecked = fileContent.IncludeRAMDisk;
if (fileContent.RAMDiskOrder == ZXRAMDiskOrder.Before)
cbRAMDiskOrder.SelectedIndex = 0;
else
cbRAMDiskOrder.SelectedIndex = 1;

Task.Run(async () =>
{
await Task.Delay(100);
Expand Down Expand Up @@ -414,6 +423,8 @@ public override bool SaveDocument(TextWriter OutputLog)
ScreenFile = txtScreenFile.Text,
ScreenName = txtScreenName.Text,
DataBlocks = _blocks.ToArray(),
IncludeRAMDisk = ckRAMDisk.IsChecked ?? false,
RAMDiskOrder = cbRAMDiskOrder.SelectedIndex == 0 ? ZXRAMDiskOrder.Before : ZXRAMDiskOrder.After
};

string content = JsonConvert.SerializeObject(fileContent, Formatting.Indented);
Expand Down
33 changes: 31 additions & 2 deletions ZXBStudio/DocumentModel/Classes/ZXDocumentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,41 @@ public static IEnumerable<string> GetDocumentCategories()

public static IEnumerable<IZXDocumentBuilder> GetPrecompilationDocumentBuilders()
{
return _docTypes.Where(d => d.DocumentBuilder != null && (d.DocumentBuildStage?.HasFlag(Enums.ZXBuildStage.PreBuild) ?? false)).Select(d => d.DocumentBuilder);
var builders = _docTypes.Where(d => d.DocumentBuilder != null && (d.DocumentBuildStage?.HasFlag(Enums.ZXBuildStage.PreBuild) ?? false)).Select(d => d.DocumentBuilder);

var sortedBuilders = SortByDependency(builders, builder => builders.Where(b => b != null && builder != null && builder.DependsOn != null && builder.DependsOn.Contains(b.Id)));

return sortedBuilders;
}

public static IEnumerable<IZXDocumentBuilder> GetPostcompilationDocumentBuilders()
{
return _docTypes.Where(d => d.DocumentBuilder != null && (d.DocumentBuildStage?.HasFlag(Enums.ZXBuildStage.PostBuild) ?? false)).Select(d => d.DocumentBuilder);
var builders = _docTypes.Where(d => d.DocumentBuilder != null && (d.DocumentBuildStage?.HasFlag(Enums.ZXBuildStage.PostBuild) ?? false)).Select(d => d.DocumentBuilder);

var sortedBuilders = SortByDependency(builders, builder => builders.Where(b => b != null && builder != null && builder.DependsOn != null && builder.DependsOn.Contains(b.Id)));

return sortedBuilders;
}

private static IEnumerable<T> SortByDependency<T>(this IEnumerable<T> nodes,
Func<T, IEnumerable<T>> connected)
{
var elems = nodes.ToDictionary(node => node,
node => new HashSet<T>(connected(node)));
while (elems.Count > 0)
{
var elem = elems.FirstOrDefault(x => x.Value.Count == 0);
if (elem.Key == null)
{
throw new ArgumentException("Cyclic connections are not allowed");
}
elems.Remove(elem.Key);
foreach (var selem in elems)
{
selem.Value.Remove(elem.Key);
}
yield return elem.Key;
}
}

public static IZXDocumentType GetDocumentTypeInstance(Type DocumentType)
Expand Down
10 changes: 10 additions & 0 deletions ZXBStudio/DocumentModel/Interfaces/IZXDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ namespace ZXBasicStudio.DocumentModel.Interfaces
/// </summary>
public interface IZXDocumentBuilder
{
/// <summary>
/// Id of the builder
/// </summary>
public Guid Id { get; }

/// <summary>
/// List of dependencies (only in the same stage)
/// </summary>
public Guid[]? DependsOn { get; }

/// <summary>
/// Builds all the documents of the handled type belonging to the project.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions ZXBStudio/Emulator/Classes/ZXBinaryBank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace ZXBasicStudio.Emulator.Classes
{
public class ZXBinaryBank
{
public string? Identifier { get; set; }
public ZXMemoryBank Bank { get; set; }
public required byte[] Data { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ namespace ZXBasicStudio.IntegratedDocumentTypes.Resources.ZXRamDisk
public class ZXRamDiskBuilder : IZXDocumentBuilder
{

public Guid Id => Guid.Parse("42257fd1-d649-4334-813a-de3b81a54654");

public Guid[]? DependsOn => null;

static readonly string mainTemplate = @"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INCLUDE THIS FILE AT THE FIRST LINE IN THE MAIN PROGRAM TO '
'ENSURE THE CODE IS NOT STORED IN THE BANKING AREA '
Expand Down Expand Up @@ -439,9 +443,14 @@ private bool InjectDisk(string buildPath, ZXProgram compiledProgram, TextWriter
if (bank.Files.Count == 0)
continue;

byte[] binFile = File.ReadAllBytes(diskPath.Substring(0, diskPath.Length - 4) + $"_{(int)bank.Bank}.zxrbin");
string bankFile = diskPath.Substring(0, diskPath.Length - 4) + $"_{(int)bank.Bank}.zxrbin";

byte[] binFile = File.ReadAllBytes(bankFile);

compiledProgram.Banks.Add(new ZXBinaryBank { Identifier = Path.GetFileNameWithoutExtension(bankFile), Bank = bank.Bank, Data = binFile });

compiledProgram.Banks.Add(new ZXBinaryBank { Bank = bank.Bank, Data = binFile });
if (!diskFile.PreserveBin)
File.Delete(bankFile);
}
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
using ZXBasicStudio.DocumentModel.Classes;
using ZXBasicStudio.DocumentModel.Enums;
using ZXBasicStudio.DocumentModel.Interfaces;
using ZXBasicStudio.Emulator.Classes;
using I = ZXBasicStudio.Common.ZXSinclairBasic.ZXSinclairBasicInstruction;

namespace ZXBasicStudio.IntegratedDocumentTypes.TapeDocuments.ZXTapeBuilder
{
public class ZXTapeBuilderBuilder : IZXDocumentBuilder
{

public Guid Id => Guid.Parse("8e100c13-6c01-464c-83ab-6f8e91cf0840");

public Guid[] DependsOn => new[] { Guid.Parse("42257fd1-d649-4334-813a-de3b81a54654") };

public bool Build(string BuildPath, ZXBuildStage Stage, ZXBuildType BuildType, ZXProgram? CompiledProgram, TextWriter OutputLog)
{
if (Stage != ZXBuildStage.PostBuild || BuildType != ZXBuildType.Release || CompiledProgram == null)
Expand Down Expand Up @@ -147,6 +153,24 @@ public bool Build(string BuildPath, ZXBuildStage Stage, ZXBuildType BuildType, Z

tFile.Blocks.Add(TAPBlock.CreateDataBlock(buildFile.ProgramName, CompiledProgram.Binary, CompiledProgram.Org));

if (buildFile.IncludeRAMDisk && buildFile.RAMDiskOrder == ZXRAMDiskOrder.Before)
{
OutputLog.WriteLine("Adding RAM disk blocks");

if(CompiledProgram.Banks.Count == 0)
{
OutputLog.WriteLine("No RAM disk blocks found! Aborting build...");
return false;
}

foreach(var bank in CompiledProgram.Banks)
{
OutputLog.WriteLine($"Adding RAM disk bank {(int)bank.Bank}...");
string bankName = bank.Identifier ?? bank.Bank.ToString();
tFile.Blocks.Add(TAPBlock.CreateDataBlock(bankName, bank.Data, 0xC000));
}
}

if (buildFile.DataBlocks != null && buildFile.DataBlocks.Length > 0)
{
OutputLog.WriteLine("Adding data blocks...");
Expand Down Expand Up @@ -177,6 +201,24 @@ public bool Build(string BuildPath, ZXBuildStage Stage, ZXBuildType BuildType, Z
}
}

if (buildFile.IncludeRAMDisk && buildFile.RAMDiskOrder == ZXRAMDiskOrder.After)
{
OutputLog.WriteLine("Adding RAM disk blocks");

if (CompiledProgram.Banks.Count == 0)
{
OutputLog.WriteLine("No RAM disk blocks found! Aborting build...");
return false;
}

foreach (var bank in CompiledProgram.Banks)
{
OutputLog.WriteLine($"Adding RAM disk bank {(int)bank.Bank}...");
string bankName = bank.Identifier ?? bank.Bank.ToString();
tFile.Blocks.Add(TAPBlock.CreateDataBlock(bankName, bank.Data, 0xC000));
}
}

OutputLog.WriteLine("Saving file to disk...");

try
Expand Down

0 comments on commit 565739b

Please sign in to comment.