Skip to content

Commit

Permalink
Merge pull request #2 from furesoft/feature/disassembler
Browse files Browse the repository at this point in the history
Feature/disassembler
  • Loading branch information
furesoft authored Oct 3, 2024
2 parents 41051e8 + 9944d5a commit 6073f7f
Show file tree
Hide file tree
Showing 62 changed files with 442 additions and 199 deletions.
6 changes: 2 additions & 4 deletions src/MimaSim/MimaSim/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ public override void OnFrameworkInitializationCompleted()
{
Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly());

CPU.Instance.Init();

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
//DataContext = new MainViewModel()
};
Locator.CurrentMutable.Register<IStorageProvider>(() => desktop.MainWindow.StorageProvider);
Locator.CurrentMutable.Register(() => desktop.MainWindow.StorageProvider);

}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
Expand All @@ -41,7 +39,7 @@ public override void OnFrameworkInitializationCompleted()
{
//DataContext = new MainViewModel()
};
Locator.CurrentMutable.Register<IStorageProvider>(() => TopLevel.GetTopLevel(singleViewPlatform.MainView)!.StorageProvider);
Locator.CurrentMutable.Register(() => TopLevel.GetTopLevel(singleViewPlatform.MainView)!.StorageProvider);
}

var samples = new SampleLoader();
Expand Down
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Controls/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static ICommand CreateOpenCommand(Control content, ReactiveObject viewMod

public static bool GetIsHost(ContentDialog target)
{
return object.ReferenceEquals(_host, target);
return ReferenceEquals(_host, target);
}

public static void Open(object content)
Expand Down
12 changes: 8 additions & 4 deletions src/MimaSim/MimaSim/Controls/ExecutionBar.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
mc:Ignorable="d" d:DesignWidth="325" d:DesignHeight="50" x:CompileBindings="False"
x:Class="MimaSim.Controls.ExecutionBar">
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Spacing="3">
<ToggleButton Name="playpauseBtn" IsChecked="{Binding RunMode, Mode=TwoWay}" ToolTip.Tip="Ausführen"
<Button ToolTip.Tip="Kompilieren" Command="{Binding CompileCommand}">
<PathIcon Data="{StaticResource compile}" Width="20" Height="20" />
</Button>

<ToggleButton Name="playpauseBtn" IsChecked="{Binding RunMode, Mode=TwoWay}" ToolTip.Tip="Ausführen" IsEnabled="{Binding IsCompiled}"
Command="{Binding RunCodeCommand}">
<Grid>
<PathIcon Classes="tbunchecked" Data="{StaticResource play}" Width="20" Height="20" />
<PathIcon Classes="tbchecked" Data="{StaticResource pause}" Width="20" Height="20" />
</Grid>
</ToggleButton>
<Button ToolTip.Tip="Schritt ausführen" IsEnabled="{Binding ElementName=playpauseBtn, Path=IsChecked}"
<Button ToolTip.Tip="Schritt ausführen" IsEnabled="{Binding IsCompiled}"
Command="{Binding StepCommand}">
<PathIcon Data="{StaticResource continue}" Margin="2" Width="20" Height="20" />
</Button>
<Button ToolTip.Tip="Stoppen" IsEnabled="{Binding ElementName=playpauseBtn, Path=IsChecked}"
<Button ToolTip.Tip="Stoppen"
Command="{Binding StopCommand}">
<PathIcon Data="{StaticResource stop}" Margin="2" Width="20" Height="20" />
</Button>

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

<Button ToolTip.Tip="Maschinencode anzeigen" Command="{Binding ViewRawCommand}">
<Button ToolTip.Tip="Disassembly anzeigen" Command="{Binding ViewRawCommand}" IsEnabled="{Binding IsCompiled}">
<PathIcon Data="{StaticResource binary}" Margin="2" Width="20" Height="20" />
</Button>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class AluControl : UserControl
{
public AluControl()
{
this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class BusControl : UserControl

public BusControl()
{
this.InitializeComponent();
InitializeComponent();
}

public Orientation Orientation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class ClockControl : ReactiveUserControl<ExecutionTabViewModel>
{
public ClockControl()
{
this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class ControlUnitControl : UserControl
{
public ControlUnitControl()
{
this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class DataBusControl : UserControl
{
public DataBusControl()
{
this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class MemoryControl : UserControl
{
public MemoryControl()
{
this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ClockSettingsPopupControl()

this.WhenActivated(disposables => { /* Handle view activation etc. */ });

this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:c="clr-namespace:MimaSim.Controls" x:CompileBindings="False"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" MinWidth="450"
x:Class="MimaSim.Controls.MimaComponents.Popups.RawViewPopupControl">
<c:DialogControl CommandText="OK" Header="Maschinencode" Command="{Binding CloseCommand}" MaxWidth="500">
x:Class="MimaSim.Controls.MimaComponents.Popups.DisassemblyViewPopupControl">
<c:DialogControl CommandText="OK" Header="Disassembler" Command="{Binding CloseCommand}" MaxWidth="500">
<StackPanel>
<TextBox Text="{Binding Raw}" TextWrapping="Wrap" IsReadOnly="True" MinWidth="100" MinHeight="350" MaxHeight="350" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" />
</StackPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

namespace MimaSim.Controls.MimaComponents.Popups;

public partial class RawViewPopupControl : ReactiveUserControl<RawPopupViewModel>
public partial class DisassemblyViewPopupControl : ReactiveUserControl<DisassemblyPopupViewModel>
{
public RawViewPopupControl()
public DisassemblyViewPopupControl()
{
DataContext = new RawPopupViewModel();
DataContext = new DisassemblyPopupViewModel();
var cc = DataContext as IActivatableViewModel;
cc.Activator.Activate();

Check warning on line 14 in src/MimaSim/MimaSim/Controls/MimaComponents/Popups/DisassemblyViewPopupControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public ErrorPopupControl()

this.WhenActivated(disposables => { /* Handle view activation etc. */ });

this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial class RegisterControl : UserControl

public RegisterControl()
{
this.InitializeComponent();
InitializeComponent();
}

public string Register
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class AccuWrapper : UserControl
{
public AccuWrapper()
{
this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class AluWrapperControl : UserControl
{
public AluWrapperControl()
{
this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class ControlUnitWrapper : UserControl
{
public ControlUnitWrapper()
{
this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public MemoryWrapper()
/* Handle view activation etc. */
});

this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Controls/MimaControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class MimaControl : UserControl
{
public MimaControl()
{
this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
3 changes: 2 additions & 1 deletion src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<Grid ColumnDefinitions="AUTO,*,AUTO" RowDefinitions="35,*,35">
<c:ExecutionBar Grid.Column="0" Grid.Row="0" Margin="5,3,0,0" />

<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, Mode=TwoWay}" TextWrapping="Wrap" AcceptsTab="True" AcceptsReturn="True" Watermark="Programmcode hier eingeben" MinWidth="150" MaxHeight="450" MinHeight="150" Grid.Row="1" Grid.ColumnSpan="3"
Grid.Column="0" 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}" />
Expand Down
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public ProgramEditorControl()
{
this.WhenActivated(disposables => { /* Handle view activation etc. */ });

this.InitializeComponent();
InitializeComponent();
}

private void InitializeComponent()
Expand Down
59 changes: 59 additions & 0 deletions src/MimaSim/MimaSim/Core/Disassembler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using MimaSim.MIMA;

namespace MimaSim.Core;

public class Disassembler(byte[] program)
{
private static Dictionary<OpCodes, IDisassemblyInstruction> Instructions = new();
private int Position { get; set; }
private byte[] Program { get; } = program;

static Disassembler()
{
var types = Assembly.GetCallingAssembly().GetTypes().Where(_ => _.GetInterfaces().Contains(typeof(IDisassemblyInstruction)));
foreach (var t in types)
{
var instance = (IDisassemblyInstruction)Activator.CreateInstance(t)!;

Instructions.Add(instance.OpCode, instance);
}
}

public byte Fetch()
{
return Program[Position++];
}

public short Fetch16()
{
var first = Fetch();
var second = Fetch();

return BitConverter.ToInt16([first, second], 0);
}

public Registers FetchRegister()
{
return (Registers)Fetch();
}

public string Disassemble()
{
var builder = new StringBuilder();

while (Position < Program.Length)
{
var op = Fetch();
var instruction = Instructions[(OpCodes)op];

instruction.Dissassemble(builder, this);
}

return builder.ToString();
}
}
11 changes: 11 additions & 0 deletions src/MimaSim/MimaSim/Core/IDisassemblyInstruction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text;
using MimaSim.MIMA;

namespace MimaSim.Core;

public interface IDisassemblyInstruction
{
OpCodes OpCode { get; }

void Dissassemble(StringBuilder builder, Disassembler disassembler);
}
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Core/IInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace MimaSim.Core;

public interface IInstruction
{
OpCodes Instruction { get; }
OpCodes OpCode { get; }

bool Invoke(CPU cpu);
}
4 changes: 2 additions & 2 deletions src/MimaSim/MimaSim/Core/Parsing/Tokenizer/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public sealed class Token
public Token(TokenKind Kind, string contents, int start, int end)
{
this.Kind = Kind;
this.Contents = contents;
Contents = contents;
Start = start;
End = end;
Length = end - start;
Expand All @@ -14,7 +14,7 @@ public Token(TokenKind Kind, string contents, int start, int end)
public Token(TokenKind Kind, string contents)
{
this.Kind = Kind;
this.Contents = contents;
Contents = contents;
Length = contents.Length;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Token Current

public void BackTrack(int pos)
{
this._position = pos;
_position = pos;
}

public int BackTrackPos()
Expand Down
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Core/PrecedenceMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class PrecedenceMap : Dictionary<TokenKind, int>
{
public int GetPrecedence(TokenKind opKind)
{
if (this.ContainsKey(opKind))
if (ContainsKey(opKind))
{
return this[opKind];
}
Expand Down
Loading

0 comments on commit 6073f7f

Please sign in to comment.