Skip to content

Commit

Permalink
feat: Add Stack
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Oct 12, 2024
1 parent a0956ab commit 7eef586
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="MimaSim.Controls.MimaComponents.AluControl" Width="125" Height="125">
<Grid RowDefinitions="25,*,25" ColumnDefinitions="25,25,25">

<Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" Grid.ColumnSpan="3">
<TextBlock Text="ALU" TextAlignment="Center" VerticalAlignment="Center" />
</Border>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="MimaSim.Controls.MimaComponents.DataBusControl">
<StackPanel Orientation="Horizontal">
<ProgressBar Width="332" Height="50" Foreground="Red" Background="Gray" b:BusRegistry.Id="main" />
<ProgressBar Width="400" Height="50" Foreground="Red" Background="Gray" b:BusRegistry.Id="main" />
</StackPanel>
</UserControl>
10 changes: 10 additions & 0 deletions src/MimaSim/MimaSim/Controls/MimaComponents/StackControl.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="MimaSim.Controls.MimaComponents.StackControl">
<Border BorderBrush="Black" BorderThickness="1" ToolTip.Tip="Stack">

</Border>
</UserControl>
13 changes: 13 additions & 0 deletions src/MimaSim/MimaSim/Controls/MimaComponents/StackControl.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace MimaSim.Controls.MimaComponents;

public partial class StackControl : UserControl
{
public StackControl()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

<c:ClockControl Name="clock" Tag="{me:GetClockFrequency}" Canvas.Top="5" Canvas.Left="5" Width="35" Height="35" ToolTip.Tip="{Binding ElementName=clock, Path=Tag}" />

<c:BusControl b:BusRegistry.Id="controlunit_iar" Canvas.Left="45" Canvas.Top="220" Width="15" Height="25" State="None" />
<c:BusControl b:BusRegistry.Id="controlunit_iar" Canvas.Left="15" Canvas.Top="220" Width="15" Height="25" State="None" />
<c:RegisterControl Register="IAR" Canvas.Left="5" Canvas.Top="245" me:RegisterConnector.IsConnected="True" />

<c:RegisterControl Register="IAR" Canvas.Left="35" Canvas.Top="245" me:RegisterConnector.IsConnected="True" />
<c:BusControl b:BusRegistry.Id="controlunit_sp" Canvas.Left="65" Canvas.Top="220" Width="15" Height="25" State="None" />
<c:RegisterControl Register="SP" Canvas.Left="55" Canvas.Top="245" me:RegisterConnector.IsConnected="True" />
</Canvas>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mimaComponents="clr-namespace:MimaSim.Controls.MimaComponents"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="MimaSim.Controls.MimaComponents.Wrappers.StackWrapper">
<StackPanel Spacing="5" Orientation="Vertical">
<TextBlock Text="Stack" TextAlignment="Center" VerticalAlignment="Center" />
<mimaComponents:StackControl Width="70" Height="140" />
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace MimaSim.Controls.MimaComponents.Wrappers;

public partial class StackWrapper : UserControl
{
public StackWrapper()
{
InitializeComponent();
}
}
2 changes: 2 additions & 0 deletions src/MimaSim/MimaSim/Controls/MimaControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<c:DataBusControl Height="25" Canvas.Top="192" Canvas.Left="110" />

<w:ControlUnitWrapper Canvas.Top="20" Canvas.Left="13" />

<w:StackWrapper Canvas.Top="55" Canvas.Left="510" />
</Canvas>
</Border>
</UserControl>
32 changes: 19 additions & 13 deletions src/MimaSim/MimaSim/MIMA/Components/CPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,40 @@ namespace MimaSim.MIMA.Components;

public class CPU
{
public static CPU Instance = new();
public readonly static CPU Instance = new();

public Register Accumulator = new("Accumulator");
public ALU ALU;
public Clock Clock = new(250);
public ControlUnit ControlUnit = new();
public Bus DataBus = new();
public readonly Register Accumulator = new("Accumulator");
public readonly ALU ALU;
public readonly Clock Clock = new(250);
public readonly ControlUnit ControlUnit = new();
public readonly Bus DataBus = new();

public static Dictionary<OpCodes, IInstruction> Instructions = new();
public Memory Memory = new((int)Math.Pow(2, 8));
public readonly static Dictionary<OpCodes, IInstruction> Instructions = new();
public readonly Memory Memory = new((int)Math.Pow(2, 8));
public readonly Stack Stack;

public Register SAR = new("SAR");
public Register SDR = new("SDR");
public readonly Register SAR = new("SAR");
public readonly Register SDR = new("SDR");

public Register X = new("X");
public readonly Register X = new("X");

public Register Y = new("Y");
public readonly Register Y = new("Y");

public byte[] Program { get; set; }
public byte[]? Program { get; set; }

public CPU()
{
ALU = new ALU(this);
Stack = new Stack(this);

ControlUnit.IAR.Bus.Subscribe(_ =>
{
BusRegistry.ActivateBus("controlunit_iar");
});
ControlUnit.SP.Bus.Subscribe(_ =>
{
BusRegistry.ActivateBus("controlunit_sp");
});

X.Bus.Subscribe(_ =>
{
Expand Down
1 change: 1 addition & 0 deletions src/MimaSim/MimaSim/MIMA/Components/ControlUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class ControlUnit
{
public Bus AccuBus = new();
public Register IAR = new("IAR");
public Register SP = new("SP", 49);
}
28 changes: 28 additions & 0 deletions src/MimaSim/MimaSim/MIMA/Components/Stack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace MimaSim.MIMA.Components;

public class Stack(CPU cpu)
{
public short[] Data = new short[50];

public void Push()
{
var sp = cpu.ControlUnit.SP.GetValue();
var value = cpu.Accumulator.GetValue();

Data[sp] = value;
sp--;

cpu.ControlUnit.SP.SetValue(sp);
}

public void Pop()
{
var sp = cpu.ControlUnit.SP.GetValue();
var value = Data[sp];

cpu.Accumulator.SetValue(value);

sp++;
cpu.ControlUnit.SP.SetValue(sp);
}
}
21 changes: 21 additions & 0 deletions src/MimaSim/MimaSim/MIMA/Instructions/Stack/PopInstruction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text;
using MimaSim.Core;
using MimaSim.MIMA.Components;

namespace MimaSim.MIMA.Instructions.Stack;

public class PopInstruction : IInstruction, IDisassemblyInstruction
{
public OpCodes OpCode => OpCodes.POP;

public bool Invoke(CPU cpu)
{
cpu.Stack.Pop();
return true;
}

public void Dissassemble(StringBuilder builder, Disassembler disassembler)
{
builder.AppendLine("pop");
}
}
21 changes: 21 additions & 0 deletions src/MimaSim/MimaSim/MIMA/Instructions/Stack/PushInstruction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text;
using MimaSim.Core;
using MimaSim.MIMA.Components;

namespace MimaSim.MIMA.Instructions.Stack;

public class PushInstruction : IInstruction, IDisassemblyInstruction
{
public OpCodes OpCode => OpCodes.PUSH;

public bool Invoke(CPU cpu)
{
cpu.Stack.Push();
return true;
}

public void Dissassemble(StringBuilder builder, Disassembler disassembler)
{
builder.AppendLine("push");
}
}
2 changes: 2 additions & 0 deletions src/MimaSim/MimaSim/MIMA/Mnemnonics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public enum Mnemnonics
CMPGT,
ADD,
SUB,
PUSH,
POP
}
3 changes: 3 additions & 0 deletions src/MimaSim/MimaSim/MIMA/OpCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ public enum OpCodes
MOV_MEM_REG = 0x41,
MOV_REG_MEM = 0x42,
MOV_MEM_MEM = 0x43,

PUSH = 0x44,
POP = 0x45,
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ private OpCodes SelectOpCode(InstructionNode instruction)
Mnemnonics.CMPGT => OpCodes.CMPGT,
Mnemnonics.ADD => OpCodes.ADD,
Mnemnonics.SUB => OpCodes.SUB,
Mnemnonics.PUSH => OpCodes.PUSH,
Mnemnonics.POP => OpCodes.POP,
_ => throw new ArgumentOutOfRangeException(nameof(instruction.Mnemnonic), $@"No opcode defined for mnemonic: {instruction.Mnemnonic}")
};
}
Expand Down
7 changes: 5 additions & 2 deletions src/MimaSim/MimaSim/Resources/Highligting/Assembler.xshd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<SyntaxDefinition name="Custom Highlighting" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<SyntaxDefinition name="Assembler" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Comment" foreground="Green" />
<Color name="String" foreground="Blue" />

Expand All @@ -13,7 +13,10 @@
<Word>exit</Word>

<Word>mov</Word>
<Word>load</Word>
<Word>load</Word>

<Word>push</Word>
<Word>pop</Word>

<Word>cmplt</Word>
<Word>cmple</Word>
Expand Down
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Resources/Highligting/Hex.xshd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<SyntaxDefinition name="Custom Highlighting" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<SyntaxDefinition name="Maschinencode" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Comment" foreground="Green" />
<Color name="String" foreground="Blue" />

Expand Down
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Resources/Highligting/Hochsprache.xshd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<SyntaxDefinition name="Custom Highlighting" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<SyntaxDefinition name="Hochsprache" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Comment" foreground="Green" />
<Color name="String" foreground="Blue" />

Expand Down
4 changes: 2 additions & 2 deletions src/MimaSim/MimaSim/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<rc:BreakpointProvider>
<rc:Show Breakpoint="Xs">
<StackPanel Orientation="Vertical">
<c:MimaControl Height="300" HorizontalAlignment="Center" Width="500" />
<c:MimaControl Height="300" HorizontalAlignment="Center" Width="600" />
<c:ProgramEditorControl Height="300" Margin="5" />
</StackPanel>
</rc:Show>
Expand All @@ -19,7 +19,7 @@
<DockPanel>
<Grid ColumnDefinitions="450,*" DockPanel.Dock="Top" VerticalAlignment="Top">
<c:ProgramEditorControl Grid.Column="0" Height="450" Margin="5" VerticalAlignment="Top" />
<c:MimaControl Grid.Column="1" Height="300" HorizontalAlignment="Center" Width="500" VerticalAlignment="Center" />
<c:MimaControl Grid.Column="1" Height="300" HorizontalAlignment="Center" Width="600" VerticalAlignment="Center" />
</Grid>
</DockPanel>
</rc:Show>
Expand Down

0 comments on commit 7eef586

Please sign in to comment.