Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Last active October 5, 2016 08:20
Show Gist options
  • Select an option

  • Save toptensoftware/51cffedbb4158bbbb508c98d5b9a64da to your computer and use it in GitHub Desktop.

Select an option

Save toptensoftware/51cffedbb4158bbbb508c98d5b9a64da to your computer and use it in GitHub Desktop.
Connecting the CPU to the Global Heap
// Read a byte from the global heap
public byte ReadByte(ushort seg, ushort offset)
{
// Crack the selector
var sel = GetSelector(seg);
try
{
// Read it
return sel.allocation.buffer[((seg >> 3) - sel.selectorIndex) << 16 | offset];
}
catch (NullReferenceException)
{
throw new Sharp86.SegmentNotPresentException();
}
catch (IndexOutOfRangeException)
{
throw new Sharp86.GeneralProtectionFaultException();
}
}
// Write a byte to the global heap
public void WriteByte(ushort seg, ushort offset, byte value)
{
// Crack the selector
var sel = GetSelector(seg);
// Don't allow writes to read-only/code selectors
if (sel.readOnly || sel.isCode)
throw new Sharp86.GeneralProtectionFaultException();
try
{
// Write it
sel.allocation.buffer[((seg >> 3) - sel.selectorIndex) << 16 | offset] = value;
}
catch (NullReferenceException)
{
throw new Sharp86.SegmentNotPresentException();
}
catch (IndexOutOfRangeException)
{
throw new Sharp86.GeneralProtectionFaultException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment