Skip to content

Commit

Permalink
Tried to fix #39
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown6656 committed Jan 6, 2017
1 parent 4fed7e2 commit 7ff09f8
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 67 deletions.
Binary file modified .vs/MCPU/v15/.suo
Binary file not shown.
2 changes: 1 addition & 1 deletion MCPU IDE/HighlightnerForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions MCPU IDE/HighlightnerForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,11 @@ end func

.main
mov [1] 20
call print 1
loop:
call print 1
decr [1]
cmp [1]
call print 1
jnz loop
jpos loop
halt</value>
</data>
</root>
10 changes: 6 additions & 4 deletions MCPU IDE/ProcessorWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
</ListView>
</GroupBox>
<GroupBox Header="{DynamicResource pw_gb_user}" Height="590" Canvas.Left="979" Canvas.Top="10" Width="695" BorderBrush="{DynamicResource FG}">
<TextBox x:Name="tb_raw_user" Background="#303030" Height="548" Margin="10" Padding="2" IsReadOnly="True" VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" VerticalContentAlignment="Top" TextWrapping="Wrap"/>
<TextBox x:Name="tb_raw_user" Background="#303030" Height="548" Margin="10" Padding="2" IsReadOnly="True" VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" VerticalContentAlignment="Top" TextWrapping="Wrap"/>
</GroupBox>
<GroupBox Header="{DynamicResource pw_gb_calls}" Canvas.Left="10" Canvas.Top="380" Width="606" BorderBrush="{DynamicResource FG}">
<ListView x:Name="lst_call" Style="{StaticResource st_data}" Height="180" Margin="10">
Expand Down Expand Up @@ -123,7 +123,7 @@
</ListView>
</GroupBox>
<GroupBox Header="{DynamicResource pw_gb_callspc}" Canvas.Left="621" Canvas.Top="380" Width="353" BorderBrush="{DynamicResource FG}" Height="220">
<TextBox x:Name="tb_raw_calls" Background="#303030" Height="180" Margin="10" Padding="2" IsReadOnly="True" VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" VerticalContentAlignment="Top" TextWrapping="Wrap"/>
<TextBox x:Name="tb_raw_calls" Background="#303030" Height="180" Margin="10" Padding="2" IsReadOnly="True" VerticalScrollBarVisibility="Visible" VerticalContentAlignment="Top" TextWrapping="Wrap"/>
</GroupBox>
<GroupBox Header="{DynamicResource pw_gb_instr}" Canvas.Left="10" Canvas.Top="605" Width="606" BorderBrush="{DynamicResource FG}" Height="296">
<ListView x:Name="lst_instr" Style="{StaticResource st_data}" Height="255" Margin="10">
Expand Down Expand Up @@ -154,10 +154,12 @@
</ListView>
</GroupBox>
<GroupBox Header="{DynamicResource pw_gb_instrsp}" Canvas.Left="621" Canvas.Top="605" Width="353" BorderBrush="{DynamicResource FG}" Height="296">
<TextBox x:Name="tb_raw_instr" Background="#303030" Height="255" Margin="10" Padding="2" IsReadOnly="True" VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" VerticalContentAlignment="Top" TextWrapping="Wrap"/>
<TextBox x:Name="tb_raw_instr" Background="#303030" Height="255" Margin="10" Padding="2" IsReadOnly="True" VerticalScrollBarVisibility="Visible" VerticalContentAlignment="Top" TextWrapping="Wrap"/>
</GroupBox>
<GroupBox Header="{DynamicResource pw_gb_output}" Canvas.Left="979" Canvas.Top="605" Width="695" BorderBrush="{DynamicResource FG}" Height="296">
<TextBlock x:Name="tb_outp" FontFamily="Consolas" Background="#303030" Height="255" Margin="10" Padding="2" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" TextWrapping="Wrap"/>
<ScrollViewer x:Name="sc_outp" Height="255" Margin="10">
<TextBlock x:Name="tb_outp" FontFamily="Consolas" Background="#303030" Padding="2" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" TextWrapping="Wrap"/>
</ScrollViewer>
</GroupBox>
</Canvas>
</Window>
161 changes: 102 additions & 59 deletions MCPU IDE/ProcessorWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@ public partial class ProcessorWindow
: Window
, ILanguageSensitiveWindow
{
internal Queue<Task> tasks = new Queue<Task>();
internal MainWindow mwin;

internal bool active;


~ProcessorWindow() => active = false;

public ProcessorWindow(MainWindow mainWindow)
{
InitializeComponent();

mwin = mainWindow;
active = true;

new Task(delegate
{
while (active)
if (tasks.Count > 0)
tasks.Dequeue().Start();
}).Start();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
Expand All @@ -37,47 +49,58 @@ private void Window_Loaded(object sender, RoutedEventArgs e)

Proc_InstructionExecuted(mwin.proc, null);
}

internal unsafe void Proc_InstructionExecuted(Processor p, Instruction args) => Dispatcher.Invoke(delegate
{
lst_io.Items.Clear();
lst_call.Items.Clear();
lst_instr.Items.Clear();

internal unsafe void Proc_InstructionExecuted(Processor p, Instruction args)
{
if (p == null)
{
tb_bp.Text =
tb_sp.Text =
tb_ip.Text =
tb_memsz.Text =
tb_cpuid.Text =
tb_instrc.Text =
tb_tick.Text = "0x--------";
tb_flags.Text =
tb_info.Text = "---------------- : --------";
tb_raw_calls.Text =
tb_raw_user.Text =
tb_raw_instr.Text = "--------";
}
DoUIStuff(delegate
{
lst_io.Items.Clear();
lst_call.Items.Clear();
lst_instr.Items.Clear();

tb_bp.Text =
tb_sp.Text =
tb_ip.Text =
tb_memsz.Text =
tb_cpuid.Text =
tb_instrc.Text =
tb_tick.Text = "0x--------";
tb_flags.Text =
tb_info.Text = "---------------- : --------";
tb_raw_calls.Text =
tb_raw_user.Text =
tb_raw_instr.Text = "--------";
});
else
{
Instruction[] instr = p.Instructions;
FunctionCall[] calls = p.CallStack;
List<IOPortData> _io = new List<IOPortData>();
List<StackframeData> _stack = new List<StackframeData>();
List<InstructionData> _instr = new List<InstructionData>();
StringBuilder sbstack = new StringBuilder();
StringBuilder sbuser = new StringBuilder();
int eip = p.IP, num = 0;
string bp = $"0x{p.StackBaseAddress:x8}";
string sp = $"0x{p.StackPointerAddress:x8}";
string cupid = $"0x{p.CPUID:x8}";
string flags = $"{Convert.ToString((ushort)p.Flags, 2).PadLeft(16, '0')} : {p.Flags}";
string info = $"{Convert.ToString((ushort)p.InformationFlags, 2).PadLeft(16, '0')} : {p.InformationFlags}";
string instrc = $"0x{instr.Length:x8}";
string ip = $"0x{eip:x8}";
string memsz = $"0x{p.Size:x8}";
string tick = $"0x{p.Ticks:x8}";
string instrs = string.Join(" ", from b in Instruction.SerializeMultiple(instr) select b.ToString("x2"));

for (int i = 0; i < p.StackSize * 4; i++)
sbstack.Append($"{((byte*)p.StackPointer)[i]:x2} ");

tb_bp.Text = $"0x{p.StackBaseAddress:x8}";
tb_sp.Text = $"0x{p.StackPointerAddress:x8}";
tb_cpuid.Text = $"0x{p.CPUID:x8}";
tb_flags.Text = $"{Convert.ToString((ushort)p.Flags, 2).PadLeft(16, '0')} : {p.Flags}";
tb_info.Text = $"{Convert.ToString((ushort)p.InformationFlags, 2).PadLeft(16, '0')} : {p.InformationFlags}";
tb_instrc.Text = $"0x{instr.Length:x8}";
tb_ip.Text = $"0x{p.IP:x8}";
tb_memsz.Text = $"0x{p.Size:x8}";
tb_tick.Text = $"0x{p.Ticks:x8}";

int num = 0;
for (int i = 0, l = p.Size; i < l; i++)
sbuser.Append($"{p[i]:x8} ");

foreach (IOPort port in p.IO)
lst_io.Items.Add(new IOPortData
_io.Add(new IOPortData
{
Direction = port.Direction,
Raw = Convert.ToString(port.Raw, 2).PadLeft(8, '0').Insert(4, "."),
Expand All @@ -88,7 +111,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
num = 0;

foreach (FunctionCall call in calls)
lst_call.Items.Add(new StackframeData
_stack.Add(new StackframeData
{
Parameters = $" - {string.Join(", ", from i in call.Arguments select $"{i:x8}h")}",
ParameterCount = call.Arguments.Length,
Expand All @@ -102,7 +125,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
num = -1;

foreach (Instruction i in instr)
lst_instr.Items.Add(new InstructionData
_instr.Add(new InstructionData
{
Code = $"0x{i.OPCode.Number:x4}",
Line = $"0x{++num:x8}",
Expand All @@ -112,35 +135,48 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
select arg.ToShortString()),
// Keyword = instr.OPCode.IsKeyword ? new BitmapImage(new Uri("Resources/")) : null,
});

lst_io.SelectedIndex = -1;
lst_instr.SelectedIndex = p.IP >= lst_instr.Items.Count ? -1 : p.IP;
lst_instr.ScrollIntoView(lst_instr.SelectedItem);
lst_call.Items.MoveCurrentToFirst();

StringBuilder sb = new StringBuilder();

for (int i = 0; i < p.StackSize * 4; i++)
sb.Append($"{((byte*)p.StackPointer)[i]:x2} ");

tb_raw_calls.Text = sb.ToString();

sb.Clear();

for (int i = 0, l = p.Size; i < l; i++)
sb.Append($"{p[i]:x8} ");

tb_raw_user.Text = sb.ToString();
tb_raw_instr.Text = string.Join(" ", from b in Instruction.SerializeMultiple(instr) select b.ToString("x2"));

DoUIStuff(delegate
{
lst_io.Items.Clear();
lst_call.Items.Clear();
lst_instr.Items.Clear();

tb_bp.Text = bp;
tb_sp.Text = sp;
tb_cpuid.Text = cupid;
tb_flags.Text = flags;
tb_info.Text = info;
tb_instrc.Text = instrc;
tb_ip.Text = ip;
tb_memsz.Text = memsz;
tb_tick.Text = tick;

foreach (IOPortData i in _io)
lst_io.Items.Add(i);
foreach (StackframeData c in _stack)
lst_call.Items.Add(c);
foreach (InstructionData i in _instr)
lst_instr.Items.Add(i);

lst_io.SelectedIndex = -1;
lst_instr.SelectedIndex = eip >= lst_instr.Items.Count ? -1 : eip;
lst_instr.ScrollIntoView(lst_instr.SelectedItem);
lst_call.Items.MoveCurrentToFirst();
tb_raw_calls.Text = sbstack.ToString();
tb_raw_user.Text = sbuser.ToString();
tb_raw_instr.Text = instrs;
});
}
});
}

internal void Proc_OnTextOutput(Processor p, string args) => Dispatcher.Invoke(delegate
internal void Proc_OnTextOutput(Processor p, string args) => DoUIStuff(delegate
{
tb_outp.Inlines.Add(new Run(args)
{
Foreground = Brushes.WhiteSmoke,
});
sc_outp.ScrollToBottom();
});

internal void Proc_ProcessorReset(Processor p)
Expand All @@ -150,13 +186,14 @@ internal void Proc_ProcessorReset(Processor p)
Proc_InstructionExecuted(p, null);
}

internal void Proc_OnError(Processor p, Exception ex) => Dispatcher.Invoke(delegate
internal void Proc_OnError(Processor p, Exception ex) => DoUIStuff(delegate
{
tb_outp.Inlines.Add(new Run(ex.Message)
tb_outp.Inlines.Add(new Run(ex.Message + '\n')
{
Foreground = Brushes.Red,
FontWeight = FontWeights.Bold,
});
sc_outp.ScrollToBottom();
});

public void OnLanguageChanged(string code) => Proc_InstructionExecuted(mwin.proc, null);
Expand All @@ -170,6 +207,12 @@ private void Window_Closing(object sender, CancelEventArgs e)
Hide();
}
}

public void DoUIStuff(Action act)
{
if (act != null)
tasks.Enqueue(new Task(() => Dispatcher.Invoke(act)));
}
}

public class IOPortData
Expand Down

0 comments on commit 7ff09f8

Please sign in to comment.