Skip to content

Commit

Permalink
Feature/samples (#1)
Browse files Browse the repository at this point in the history
* init

* Add SampleNames

* Cleanup

* fix: NullReference

* feat: Add Automatic Sample Registration From Resources
  • Loading branch information
furesoft authored Oct 3, 2024
1 parent eab548b commit 41051e8
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 66 deletions.
7 changes: 7 additions & 0 deletions src/MimaSim/MimaSim/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Platform.Storage;
using MimaSim.Core;
using MimaSim.MIMA.Components;
using MimaSim.Samples;
using MimaSim.Views;
using ReactiveUI;
using Splat;
Expand Down Expand Up @@ -42,6 +44,11 @@ public override void OnFrameworkInitializationCompleted()
Locator.CurrentMutable.Register<IStorageProvider>(() => TopLevel.GetTopLevel(singleViewPlatform.MainView)!.StorageProvider);
}

var samples = new SampleLoader();
samples.FromResources(GetType().Assembly);

Locator.CurrentMutable.RegisterConstant(samples);

base.OnFrameworkInitializationCompleted();
}
}
3 changes: 1 addition & 2 deletions src/MimaSim/MimaSim/Controls/ExecutionBar.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

<Rectangle Width="2" Fill="Black" Margin="5,0,5,0" />

<Button ToolTip.Tip="Maschinencode anzeigen" Command="{Binding ViewRawCommand}"
IsEnabled="{Binding ElementName=playpauseBtn, Path=IsChecked}">
<Button ToolTip.Tip="Maschinencode anzeigen" Command="{Binding ViewRawCommand}">
<PathIcon Data="{StaticResource binary}" Margin="2" Width="20" Height="20" />
</Button>

Expand Down
11 changes: 4 additions & 7 deletions src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
xmlns:c="clr-namespace:MimaSim.Controls;assembly=MimaSim" x:CompileBindings="False"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="MimaSim.Controls.ProgramEditorControl">
<Grid ColumnDefinitions="AUTO,*,AUTO" RowDefinitions="35,*">
<Grid ColumnDefinitions="AUTO,*,AUTO" RowDefinitions="35,*,35">
<c:ExecutionBar Grid.Column="0" Grid.Row="0" Margin="5,3,0,0" />

<ComboBox Name="languageCB" Grid.Row="0" Grid.Column="2" MinWidth="110" HorizontalAlignment="Right" VerticalAlignment="Bottom" SelectedItem="{Binding SelectedLanguage}">
<ComboBoxItem>Maschinencode</ComboBoxItem>
<ComboBoxItem>Assembly</ComboBoxItem>
<ComboBoxItem>Hochsprache</ComboBoxItem>
</ComboBox>
<TextBox Text="{Binding Source, Mode=TwoWay}" TextWrapping="Wrap" AcceptsTab="True" AcceptsReturn="True" Watermark="Programmcode hier eingeben" MinWidth="150" MaxHeight="450" MinHeight="150" Grid.Row="1" Grid.ColumnSpan="3" Margin="0,5,0,5" />

<TextBox Text="{Binding Source}" TextWrapping="Wrap" AcceptsTab="True" AcceptsReturn="True" Watermark="Programmcode hier eingeben" MinWidth="150" MaxHeight="450" MinHeight="150" Grid.Row="1" Grid.ColumnSpan="3" Margin="0,5,0,5" />
<ComboBox Grid.Row="2" Grid.Column="0" MinWidth="110" HorizontalAlignment="Right" VerticalAlignment="Bottom" ItemsSource="{Binding LanguageNames}" SelectedItem="{Binding SelectedLanguage, Mode=TwoWay}" />
<ComboBox Grid.Row="2" Grid.Column="1" MinWidth="110" HorizontalAlignment="Right" VerticalAlignment="Bottom" ItemsSource="{Binding SampleNames, Mode=TwoWay}" SelectedItem="{Binding SelectedSample, Mode=TwoWay}" />
</Grid>
</UserControl>
9 changes: 0 additions & 9 deletions src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,5 @@ public ProgramEditorControl()
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);

Initialized += ProgramEditorControl_Initialized;
}

private void ProgramEditorControl_Initialized(object sender, System.EventArgs e)
{
var cb = this.Find<ComboBox>("languageCB");

cb.SelectedIndex = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load 0x01
mov accumulator, x
load 0x02
mov accumulator, y
add
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var z = 1 + 2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
04 01 00 40 01 02 04 02 00 40 01 03 08
51 changes: 51 additions & 0 deletions src/MimaSim/MimaSim/Samples/SampleLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using MimaSim.Core;

namespace MimaSim.Samples;

public class SampleLoader
{
private Dictionary<(LanguageName lang, string name), string> _samples = new();
public void Register(LanguageName language, string name, string src)
{
_samples.TryAdd((language, name), src);
}

private const string BaseResourcePath = "MimaSim.Resources.samples.";
public void FromResources(Assembly assembly)
{
foreach (LanguageName language in Enum.GetValues(typeof(LanguageName)))
{
var languagePath = $"{BaseResourcePath}{language.ToString().ToLower()}.";
var resourceNames = assembly.GetManifestResourceNames();

foreach (var resourceName in resourceNames)
{
if (resourceName.StartsWith(languagePath) && resourceName.EndsWith(".sample"))
{
var sampleName = Path.GetFileNameWithoutExtension(resourceName).Split('.').Last();

using var stream = assembly.GetManifestResourceStream(resourceName);
using var reader = new StreamReader(stream!);
var sampleContent = reader.ReadToEnd();

Register(language, sampleName, sampleContent);
}
}
}
}

public string? GetSample(LanguageName language, string? name)
{
return name is null ? null : _samples.GetValueOrDefault((language, name));
}

public IEnumerable<string> GetSampleNamesFor(LanguageName language)
{
return _samples.Keys.Where(_ => _.lang == language).Select(_ => _.name);
}
}
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Tabs/ExecutionTab.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:c="clr-namespace:MimaSim.Controls"
mc:Ignorable="d" d:DesignWidth="850" d:DesignHeight="450"
x:Class="MimaSim.Tabs.ExecutionTab">
<Grid ColumnDefinitions="450,*" VerticalAlignment="Top">
<Grid ColumnDefinitions="450,*">
<c:ProgramEditorControl Grid.Column="0" Height="450" Margin="5" />
<c:MimaControl Grid.Column="1" Height="300" HorizontalAlignment="Center" Width="500" />
</Grid>
Expand Down
103 changes: 67 additions & 36 deletions src/MimaSim/MimaSim/ViewModels/ExecutionTabViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using MimaSim.Controls;
using MimaSim.Controls.MimaComponents.Popups;
using MimaSim.Core;
Expand All @@ -12,7 +11,9 @@
using System.IO;
using System.Linq;
using System.Windows.Input;
using System.Collections.ObjectModel;
using Avalonia.Platform.Storage;
using MimaSim.Samples;
using MimaSim.ViewModels.Mima;
using Splat;

Expand All @@ -21,8 +22,67 @@ namespace MimaSim.ViewModels;
public class ExecutionTabViewModel : ReactiveObject, IActivatableViewModel
{
private bool _runMode;
private object _selectedLanguage;
private LanguageName _selectedLanguage;
private string _selectedSample;
private string _source;
private string[] _sampleNames;

public ObservableCollection<LanguageName> LanguageNames { get; }
public ViewModelActivator Activator => new();
public ICommand LoadCommand { get; set; }
public ICommand OpenClockSettingsCommand { get; set; }
public ICommand OpenErrorPopupCommand { get; set; }
public ICommand OpenMemoryPopupCommand { get; set; }
public ICommand RunCodeCommand { get; set; }

public bool RunMode
{
get => _runMode;
set => this.RaiseAndSetIfChanged(ref _runMode, value);
}

public ICommand SaveCommand { get; set; }

public LanguageName SelectedLanguage
{
get => _selectedLanguage;
set
{
this.RaiseAndSetIfChanged(ref _selectedLanguage, value);

SampleNames = Locator.Current.GetService<SampleLoader>().GetSampleNamesFor(_selectedLanguage).ToArray();

SelectedSample = null;
}
}

public string SelectedSample
{
get => _selectedSample;
set
{
this.RaiseAndSetIfChanged(ref _selectedSample, value);

Source = Locator.Current.GetService<SampleLoader>().GetSample(SelectedLanguage, SelectedSample);
}
}

public string[] SampleNames
{
get => _sampleNames;
set => this.RaiseAndSetIfChanged(ref _sampleNames, value);
}

public string Source
{
get => _source;
set => this.RaiseAndSetIfChanged(ref _source, value);
}

public ICommand StepCommand { get; set; }

public ICommand StopCommand { get; set; }
public ICommand ViewRawCommand { get; set; }

public ExecutionTabViewModel()
{
Expand All @@ -33,6 +93,9 @@ public ExecutionTabViewModel()
StepCommand = ReactiveCommand.Create(() => CPU.Instance.Step());
StopCommand = ReactiveCommand.Create(() => CPU.Instance.Clock.Stop());

LanguageNames = new ObservableCollection<LanguageName>(Enum.GetNames<LanguageName>().Select(_ => Enum.Parse<LanguageName>(_)));
SelectedLanguage = LanguageNames.FirstOrDefault();

ViewRawCommand = ReactiveCommand.Create(() =>
{
DialogService.Open(new RawViewPopupControl(), new RawPopupViewModel());
Expand All @@ -49,7 +112,7 @@ public ExecutionTabViewModel()
Title = "Programm laden"
});
var reader = new StreamReader(await filenames.First().OpenReadAsync());
using var reader = new StreamReader(await filenames.First().OpenReadAsync());
Source = reader.ReadToEnd();
});
Expand All @@ -68,7 +131,7 @@ public ExecutionTabViewModel()
{
if (RunMode)
{
var translator = SourceTextTranslatorSelector.Select((LanguageName)Enum.Parse(typeof(LanguageName), ((ComboBoxItem)SelectedLanguage).Content.ToString()));
var translator = SourceTextTranslatorSelector.Select(SelectedLanguage);
DiagnosticBag diagnostics = new();
CPU.Instance.Program = translator.ToRaw(Source, ref diagnostics);
Expand Down Expand Up @@ -108,36 +171,4 @@ public ExecutionTabViewModel()
CPU.Instance.Clock.Stop();
});
}

public ViewModelActivator Activator => new();
public ICommand LoadCommand { get; set; }
public ICommand OpenClockSettingsCommand { get; set; }
public ICommand OpenErrorPopupCommand { get; set; }
public ICommand OpenMemoryPopupCommand { get; set; }
public ICommand RunCodeCommand { get; set; }

public bool RunMode
{
get => _runMode;
set => this.RaiseAndSetIfChanged(ref _runMode, value);
}

public ICommand SaveCommand { get; set; }

public object SelectedLanguage
{
get => _selectedLanguage;
set => this.RaiseAndSetIfChanged(ref _selectedLanguage, value);
}

public string Source
{
get => _source;
set => this.RaiseAndSetIfChanged(ref _source, value);
}

public ICommand StepCommand { get; set; }

public ICommand StopCommand { get; set; }
public ICommand ViewRawCommand { get; set; }
}
11 changes: 0 additions & 11 deletions src/MimaSim/MimaSim/ViewModels/LicensePopupViewModel.cs

This file was deleted.

0 comments on commit 41051e8

Please sign in to comment.