Skip to content

Commit

Permalink
feat: Add Ability To Draw Pixel
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Oct 14, 2024
1 parent 2a63942 commit 70cea60
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using Avalonia;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Layout;
using Avalonia.Media;
using MimaSim.MIMA.Components;

namespace MimaSim.Controls.MimaComponents;

public partial class DisplayControl : UserControl
{
public Dictionary<(int, int), Border> Pixels { get; set; } = [];

public DisplayControl()
{
InitializeComponent();
Expand All @@ -16,6 +20,8 @@ public DisplayControl()
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
var grid = this.Find<Grid>("grid");
CPU.Instance.Display.SetDisplay(this);

var stack = new StackPanel()
{
Spacing = 0,
Expand All @@ -40,8 +46,7 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
Height = 25,
Background = Brushes.White
};
Grid.SetRow(cell, row);
Grid.SetColumn(cell, col);
Pixels.Add((col, row), cell);

colStack.Children.Add(cell);
}
Expand Down
4 changes: 1 addition & 3 deletions src/MimaSim/MimaSim/MIMA/Components/CPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public class CPU
public readonly Register SAR = new("SAR");
public readonly Register SDR = new("SDR");

public readonly Register DC = new("DC"); // Display Color
public readonly Register DX = new("DX"); // Display x-pos
public readonly Register DY = new("DY"); // Display y-pos
public readonly Display Display = new();

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

Expand Down
49 changes: 47 additions & 2 deletions src/MimaSim/MimaSim/MIMA/Components/Display.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
namespace MimaSim.MIMA.Components;
using System;
using Avalonia.Media;
using MimaSim.Controls.MimaComponents;

namespace MimaSim.MIMA.Components;

public class Display
{

public readonly Register DC = new("DC"); // Display Color
public readonly Register DX = new("DX"); // Display x-pos
public readonly Register DY = new("DY"); // Display y-pos

private DisplayControl _displayControl;

public void SetDisplay(DisplayControl displayControl)
{
_displayControl = displayControl;
}

public void SetPixel()
{
var colorIndex = DC.GetValueWithoutNotification();
var x = DX.GetValueWithoutNotification();
var y = DY.GetValueWithoutNotification();

var color = (DisplayColor)colorIndex;

_displayControl.Pixels[(x, y)].Background = GetBrush(color);
}

private IBrush GetBrush(DisplayColor color)
{
return color switch
{
DisplayColor.Red => Brushes.Red,
DisplayColor.Green => Brushes.Green,
DisplayColor.Blue => Brushes.Blue,
DisplayColor.Yellow => Brushes.Yellow,
DisplayColor.Cyan => Brushes.Cyan,
DisplayColor.Magenta => Brushes.Magenta,
DisplayColor.Orange => Brushes.Orange,
DisplayColor.Purple => Brushes.Purple,
DisplayColor.Pink => Brushes.Pink,
DisplayColor.Brown => Brushes.Brown,
DisplayColor.Gray => Brushes.Gray,
DisplayColor.Black => Brushes.Black,
DisplayColor.White => Brushes.White,
_ => throw new ArgumentOutOfRangeException(nameof(color), color, null)
};
}
}
18 changes: 18 additions & 0 deletions src/MimaSim/MimaSim/MIMA/Components/DisplayColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace MimaSim.MIMA.Components;

public enum DisplayColor
{
Red,
Green,
Blue,
Yellow,
Cyan,
Magenta,
Orange,
Purple,
Pink,
Brown,
Gray,
Black,
White
}

0 comments on commit 70cea60

Please sign in to comment.