Skip to content

Commit

Permalink
feat: Implemented clear and drawing as syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Oct 26, 2024
1 parent 7d9a3ba commit 6f61b11
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 34 deletions.
9 changes: 6 additions & 3 deletions src/MimaSim/MimaSim/Behaviors/DocumentTextBindingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ private void TextChanged(object sender, EventArgs eventArgs)

private void TextPropertyChanged(string text)
{
if (_textEditor != null && _textEditor.Document != null && text != null)
if (_textEditor == null || _textEditor.Document == null || text == null) return;

var caretOffset = _textEditor.CaretOffset;
_textEditor.Document.Text = text;

if (caretOffset < _textEditor.Document.TextLength)
{
var caretOffset = _textEditor.CaretOffset;
_textEditor.Document.Text = text;
_textEditor.CaretOffset = caretOffset;
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/MimaSim/MimaSim/MIMA/Components/CPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,14 @@ public CPU()
BusRegistry.Activate("cu->y");
});

Display.DC.Bus.Subscribe(_ =>
ControlUnit.AddSysCall(SysCall.CLEAR, () =>
{
var isTextMode = CPU.Instance.ControlUnit.HasFlag(Flags.TextMode);
var clear = CPU.Instance.ControlUnit.HasFlag(Flags.CLEAR);
Display.Clear();
});

if (clear)
{
Instance.Display.Clear();
return;
}
ControlUnit.AddSysCall(SysCall.DRAW, () =>
{
var isTextMode = CPU.Instance.ControlUnit.HasFlag(Flags.TextMode);

if (isTextMode)
{
Expand Down
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/MIMA/Components/Display.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void DrawChar(char ch)
var xOffset = CPU.Instance.Display.DX.GetValueWithoutNotification();
var yOffset = CPU.Instance.Display.DY.GetValueWithoutNotification();

Font.DrawChar(xOffset, yOffset, ch);
Font.DrawChar(xOffset, yOffset, ch, DisplayColor.Black);
}

public void SetPixel(short x, short y)
Expand Down
3 changes: 1 addition & 2 deletions src/MimaSim/MimaSim/MIMA/Flags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

public enum Flags
{
TextMode = 0x00,
CLEAR = 0x01,
TextMode = 0x00
}
33 changes: 19 additions & 14 deletions src/MimaSim/MimaSim/MIMA/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,35 +426,40 @@ public short Measure(string s)
return (short)s.Sum(c => Measure(c).width + 1);
}

public void DrawChar(short xOffset, short yOffset, char ch)
public void DrawChar(short xOffset, short yOffset, char ch, DisplayColor color)
{
if (!Characters.TryGetValue(ch, out var character))
{
bool[] boxPixels =
[
true, true, true, true, true, true,
true, false, false, false, false, true,
true, false, true, false, false, true,
true, false, false, false, false, true,
true, true, true, true, true, true
];

DrawChar(6, boxPixels, yOffset, xOffset);
DrawInvalidChar(xOffset, yOffset, color);
}

var (pixels, width) = character;
DrawChar(width, pixels, yOffset, xOffset);
DrawChar(width, pixels, yOffset, xOffset, color);
}

private static void DrawChar(int width, bool[] pixels, short yOffset, short xOffset)
private static void DrawInvalidChar(short xOffset, short yOffset, DisplayColor color)
{
bool[] boxPixels =
[
true, true, true, true, true, true,
true, false, false, false, false, true,
true, false, true, false, false, true,
true, false, false, false, false, true,
true, true, true, true, true, true
];

DrawChar(6, boxPixels, yOffset, xOffset, color);
}

private static void DrawChar(int width, bool[] pixels, short yOffset, short xOffset, DisplayColor color)
{
for (var y = 0; y < 5; y++)
{
for (var x = 0; x < width; x++)
{
if (pixels[(y * width) + x])
{
CPU.Instance.Display.SetPixel((short)(y + yOffset), (short)(x + xOffset), DisplayColor.Black);
CPU.Instance.Display.SetPixel((short)(y + yOffset), (short)(x + xOffset), color);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/MimaSim/MimaSim/MIMA/SysCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public enum SysCall
{

CLEAR,
DRAW,
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

load c
mov accumulator, dc

syscall DRAW
}

setPixel 2, 5, GREEN
11 changes: 6 additions & 5 deletions src/MimaSim/MimaSim/Resources/samples/assembly/TextMode.sample
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
macro clear(color) {
unflag TEXTMODE
flag CLEAR
load color
mov accumulator, dc
unflag CLEAR

syscall CLEAR
}

clk 10
clk 60
clear WHITE

macro drawChar(x, y, c) {
Expand All @@ -20,6 +19,8 @@ macro drawChar(x, y, c) {

load c
mov accumulator, dc

syscall DRAW
}

drawChar 0, 0, 'h'
Expand All @@ -35,4 +36,4 @@ drawChar 6, 6, 'w'
drawChar 12, 6, 'e'

drawChar 18, 6, 'l'
drawChar 22, 6, 't'
drawChar 22, 6, 't'

0 comments on commit 6f61b11

Please sign in to comment.