Skip to content

Instantly share code, notes, and snippets.

View toptensoftware's full-sized avatar

Brad Robinson toptensoftware

View GitHub Profile
@toptensoftware
toptensoftware / RangeAllocator.cs
Last active October 5, 2016 05:56
Win3mu's RangeAllocator
// Manages allocations within an "address space".
public class RangeAllocator<T>
{
// Constructor
public RangeAllocator(int addressSpaceSize);
// Info accessors
public int AddressSpaceSize { get; set; }
public int FreeSpace { get; }
public int EntryCount { get; }
@toptensoftware
toptensoftware / GlobalHeapIBus.cs
Last active October 5, 2016 08:20
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];
@toptensoftware
toptensoftware / RunWinMain.cs
Created October 6, 2016 00:38
Win3mu - Calling the program entry point
// Setup the CPU's instruction pointer
_cpu.cs = selectorFromSegmentIndex(_module.NeHeader.entryPointCS);
_cpu.ip = _module.NeHeader.entryPointIP;
// Run till done
while (!_stopCondition)
{
_cpu.Step()
}
@toptensoftware
toptensoftware / ExitProcess.cs
Last active October 8, 2016 23:54
Handling DOS Exit Process call.
public override void RaiseInterrupt(byte interruptNumber)
{
if (interruptNumber == 0x21 && _cpu.ah == 0x4C)
{
_stopCondition = true;
}
}
@toptensoftware
toptensoftware / ThunkHandler.cs
Last active October 9, 2016 01:08
Handling int 80h thunk calls
public override void RaiseInterrupt(byte interruptNumber)
{
switch (interruptNumber)
{
case 0x21:
// DOS Call
if (_cpu.ah == 0x4C)
_stopCondition = true;
break;
List<Action> _systemThunkHanders = new List<Action>();
public uint CreateSystemThunk(Action handler, ushort popStack)
{
// Capture address of this thunk
ushort address = _systemCodeGenPos;
// Store the handler
ushort thunkIndex = (ushort)_systemThunkHanders.Count;
_systemThunkHanders.Add(handler);
uint _sysRetThunk;
void CreateSysRetThunk()
{
// Store address
_sysRetThunk = (uint)(_systemCodeSelector << 16 | _systemCodeGenPos);
// Get memory buffer
byte[] mem = _globalHeap.GetBuffer(_systemCodeSelector);
// INT 81h
public void CallVM(uint lpfnProc)
{
// Save the old IP
var oldCS = cs;
var oldIP = ip;
// Setup the new IP
cs = lpfnProc.Hiword();
ip = lpfnProc.Loword();
@toptensoftware
toptensoftware / StandardModuleRegistration.cs
Last active October 11, 2016 00:35
Module Class Heirarchy
// Base functionality common to all modules
abstract class ModuleBase
{
}
// Module loaded from 16-bit .exe or .dll
class Module16 : ModuleBase
{
}
@toptensoftware
toptensoftware / LocateAndOpenModule.cs
Created October 11, 2016 00:53
Locating and Opening 16-bit Modules
// Locate the module (append ".dll" + look in search path)
var locatedModule = LocateModule(fileOrModuleName, parentPath);
if (locatedModule == null)
throw new VirtualException(string.Format("Can't find module '{0}'", fileOrModuleName));
// Load it
var nativeModule = new Module16(locatedModule);
// elsewhere....