Skip to content

Commit

Permalink
feat: Add SysCalls
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Oct 26, 2024
1 parent d0a7273 commit 7d9a3ba
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
x:Class="MimaSim.Controls.MimaComponents.Wrappers.ControlUnitWrapper">
<Canvas>
<c:RegisterControl Register="FLAG" Width="45" Canvas.Left="100" Canvas.Top="15" me:RegisterConnector.IsConnected="True" />
<c:RegisterControl Register="SCR" Width="45" Canvas.Left="100" Canvas.Top="45" me:RegisterConnector.IsConnected="True" />

<c:ControlUnitControl Canvas.Top="0" Canvas.Left="0" Width="100" Height="220" />
<c:ControlUnitControl Canvas.Top="0" Canvas.Left="0" Width="100" Height="220" />

<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: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="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: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: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" />
<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>
20 changes: 19 additions & 1 deletion src/MimaSim/MimaSim/MIMA/Components/ControlUnit.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
namespace MimaSim.MIMA.Components;
using System;
using System.Collections.Generic;

namespace MimaSim.MIMA.Components;

public class ControlUnit
{
public Bus AccuBus = new();
public Register FLAG = new("FLAG");
public Register IAR = new("IAR");
public Register SP = new("SP", 49);
public Register SCR = new("SCR");

private Dictionary<SysCall, Action> _syscalls = new();

public void AddSysCall(SysCall syscall, Action action)
{
_syscalls.TryAdd(syscall, action);
}

public void InvokeSysCall(SysCall syscall)
{
_syscalls.TryGetValue(syscall, out Action? action);

action!();
}

public bool HasFlag(Flags flag)
{
Expand Down
22 changes: 22 additions & 0 deletions src/MimaSim/MimaSim/MIMA/Instructions/SysCallInstruction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text;
using MimaSim.Core;
using MimaSim.MIMA.Components;

namespace MimaSim.MIMA.Instructions;

public class SysCallInstruction : IInstruction, IDisassemblyInstruction
{
public OpCodes OpCode => OpCodes.SYSCALL;

public bool Invoke(CPU cpu)
{
cpu.ControlUnit.InvokeSysCall((SysCall)cpu.Fetch16());

return true;
}

public void Dissassemble(StringBuilder builder, Disassembler disassembler)
{
builder.AppendLine($"syscall 0x{disassembler.Fetch16():X2}");
}
}
3 changes: 2 additions & 1 deletion src/MimaSim/MimaSim/MIMA/Mnemnonics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public enum Mnemnonics
CLK,
FLAG,
UNFLAG,
HASFLAG
HASFLAG,
SYSCALL
}
5 changes: 3 additions & 2 deletions src/MimaSim/MimaSim/MIMA/OpCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public enum OpCodes

//Shifting
LSHIFT = 0x20,

RSHIFT = 0x21,

// Logical
Expand Down Expand Up @@ -53,5 +52,7 @@ public enum OpCodes

FLAG = 0x46,
HASFLAG = 0x47,
UNFLAG = 0x48
UNFLAG = 0x48,

SYSCALL = 0x49,
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public AssemblyVisitor()
{
Scope.Root.Define(flag.ToUpper(), (short)Enum.Parse<Flags>(flag));
}

foreach (var syscall in Enum.GetNames<SysCall>())
{
Scope.Root.Define(syscall.ToUpper(), (short)Enum.Parse<SysCall>(syscall));
}
}

private void VisitMacroInvocation(InstructionNode invocation, Scope scope)
Expand Down Expand Up @@ -187,27 +192,12 @@ public void Visit(InstructionNode instruction, Scope scope)

private OpCodes SelectOpCode(InstructionNode instruction, Scope scope, Mnemnonics mnemnonic)
{
return mnemnonic switch
{
Mnemnonics.MOV => SelectMovOpCode(instruction, scope),
Mnemnonics.LOAD => OpCodes.LOAD,
Mnemnonics.JMP => OpCodes.JMP,
Mnemnonics.JMPC => OpCodes.JMPC,
Mnemnonics.CMPNE => OpCodes.CMPNEQ,
Mnemnonics.CMPE => OpCodes.CMPEQ,
Mnemnonics.CMPLT => OpCodes.CMPLT,
Mnemnonics.CMPGT => OpCodes.CMPGT,
Mnemnonics.ADD => OpCodes.ADD,
Mnemnonics.SUB => OpCodes.SUB,
Mnemnonics.PUSH => OpCodes.PUSH,
Mnemnonics.POP => OpCodes.POP,
Mnemnonics.EXIT => OpCodes.EXIT,
Mnemnonics.CLK => OpCodes.CLK,
Mnemnonics.FLAG => OpCodes.FLAG,
Mnemnonics.HASFLAG => OpCodes.HASFLAG,
Mnemnonics.UNFLAG => OpCodes.UNFLAG,
_ => throw new ArgumentOutOfRangeException(nameof(instruction.Mnemnonic), $@"No opcode defined for mnemonic: {instruction.Mnemnonic}")
};
if (mnemnonic == Mnemnonics.MOV)
{
return SelectMovOpCode(instruction, scope);
}

return Enum.Parse<OpCodes>(mnemnonic.ToString());
}

private OpCodes SelectMovOpCode(InstructionNode instruction, Scope scope)
Expand Down
1 change: 1 addition & 0 deletions src/MimaSim/MimaSim/MIMA/Registers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public enum Registers
DX,
DY,
FLAG,
SCR // Register to store syscall result
}
6 changes: 6 additions & 0 deletions src/MimaSim/MimaSim/MIMA/SysCall.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MimaSim.MIMA;

public enum SysCall
{

}
2 changes: 2 additions & 0 deletions src/MimaSim/MimaSim/Resources/Highligting/Assembler.xshd
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<Word>unflag</Word>
<Word>hasflag</Word>

<Word>syscall</Word>

<Word>macro</Word>
</Keywords>

Expand Down

0 comments on commit 7d9a3ba

Please sign in to comment.