public static void InfoMemoryString(this ILogger l, IMemoryCore memory, ushort segment, ushort offset) { /* * 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F * _SEG:_OFF [ T E S T T E S T T E S T] * */ var sbOutput = new StringBuilder(); sbOutput.Append(" "); //Print Header for (var i = 0; i < 0xF; i++) { sbOutput.Append($" {(byte)offset + i:X1}"); } l.Info(sbOutput); sbOutput.Clear(); sbOutput.Append($"{segment:X4}:{offset:X4} ["); for (var i = 0; i < 0xF; i++) { sbOutput.Append($" {(char)memory.GetByte(segment, (ushort) (offset+i))}"); } sbOutput.Append("]"); l.Info(sbOutput); }
static Program() { mbbsEmuMemoryCore = new MemoryCore(); mbbsEmuCpuRegisters = new CpuRegisters(); mbbsEmuCpuCore = new CpuCore(); mbbsEmuCpuCore.Reset(mbbsEmuMemoryCore, mbbsEmuCpuRegisters, null); }
protected CpuTestBase() { mbbsEmuMemoryCore = new MemoryCore(); mbbsEmuCpuRegisters = new CpuRegisters(); mbbsEmuCpuCore = new CpuCore(); mbbsEmuCpuCore.Reset(mbbsEmuMemoryCore, mbbsEmuCpuRegisters, null); }
public ExecutionUnit(IMemoryCore moduleMemory, Dictionary <ushort, IExportedModule> exportedModuleDictionary) { ModuleCpu = new CpuCore(); ModuleCpuRegisters = new CpuRegisters(); ModuleMemory = moduleMemory; ExportedModuleDictionary = exportedModuleDictionary; ModuleCpu.Reset(ModuleMemory, ModuleCpuRegisters, ExternalFunctionDelegate); }
public Int21h_Tests() { var streamWriter = new StreamWriter(_consoleOutput) { AutoFlush = true }; _serviceResolver = new ServiceResolver(_fakeClock); _memory = new RealModeMemoryCore(_serviceResolver.GetService <ILogger>()); _int21 = new Int21h(_registers, _memory, _fakeClock, _serviceResolver.GetService <ILogger>(), _serviceResolver.GetService <IFileUtility>(), new TextReaderStream(new StreamReader(_consoleInput)), new TextWriterStream(streamWriter), new TextWriterStream(streamWriter)); }
public int WriteTo(IMemoryCore memoryCore, FarPtr ptr) { memoryCore.SetWord(ptr, record_length); memoryCore.SetWord(ptr + 2, page_size); memoryCore.SetWord(ptr + 4, number_of_keys); memoryCore.SetDWord(ptr + 6, number_of_records); memoryCore.SetWord(ptr + 10, flags); memoryCore.SetWord(ptr + 12, reserved); memoryCore.SetWord(ptr + 14, unused_pages); return(16); }
public int WriteTo(IMemoryCore memoryCore, FarPtr ptr) { memoryCore.SetWord(ptr, position); memoryCore.SetWord(ptr + 2, length); memoryCore.SetWord(ptr + 4, flags); memoryCore.SetDWord(ptr + 6, number_of_keys); memoryCore.SetByte(ptr + 10, data_type); memoryCore.SetByte(ptr + 11, null_value); memoryCore.SetWord(ptr + 12, unused); memoryCore.SetByte(ptr + 14, number_only_if_explicit_key_flag_is_set); memoryCore.SetByte(ptr + 15, acs_number); return(16); }
private void Execute(string[] args) { var realMode = args.Length == 1 && (args[0].Equals("-realmode") || args[0].Equals("-real")); if (realMode) { memoryCore = realModeMemoryCore = new RealModeMemoryCore(logger: null); } else { memoryCore = protectedModeMemoryCore = new ProtectedModeMemoryCore(null); } mbbsEmuCpuCore = new CpuCore(logger: null); mbbsEmuCpuRegisters = (ICpuRegisters)mbbsEmuCpuCore; mbbsEmuCpuCore.Reset(memoryCore, null, null, null); // Reset mbbsEmuCpuRegisters.Zero(); mbbsEmuCpuCore.Reset(); memoryCore.Clear(); mbbsEmuCpuCore.Registers.CS = 0x1000; mbbsEmuCpuCore.Registers.DS = 2; mbbsEmuCpuCore.Registers.IP = 0; var instructions = new Assembler(16); var label_start = instructions.CreateLabel(); var label_loop = instructions.CreateLabel(); instructions.Label(ref label_start); instructions.mov(__word_ptr[0], 1); instructions.Label(ref label_loop); instructions.mov(ax, __word_ptr[0]); instructions.cmp(ax, 0x7FFF); instructions.je(label_start); instructions.inc(__word_ptr[0]); instructions.jmp(label_loop); CreateCodeSegment(instructions); CreateDataSegment(new ReadOnlySpan <byte>()); _isRunning = true; new Thread(RunThread).Start(); new Thread(MonitorThread).Start(); Console.ReadKey(); _isRunning = false; }
protected ExportedModuleTestBase(string modulePath) { _serviceResolver = new ServiceResolver(fakeClock, SessionBuilder.ForTest($"MBBSDb_{RANDOM.Next()}")); var textVariableService = _serviceResolver.GetService <ITextVariableService>(); mbbsEmuMemoryCore = mbbsEmuProtectedModeMemoryCore = new ProtectedModeMemoryCore(_serviceResolver.GetService <ILogger>()); mbbsEmuCpuCore = new CpuCore(_serviceResolver.GetService <ILogger>()); mbbsEmuCpuRegisters = mbbsEmuCpuCore; var testModuleConfig = new ModuleConfiguration { ModulePath = modulePath, ModuleEnabled = true }; mbbsModule = new MbbsModule(FileUtility.CreateForTest(), fakeClock, _serviceResolver.GetService <ILogger>(), testModuleConfig, mbbsEmuProtectedModeMemoryCore); testSessions = new PointerDictionary <SessionBase>(); testSessions.Allocate(new TestSession(null, textVariableService)); testSessions.Allocate(new TestSession(null, textVariableService)); majorbbs = new HostProcess.ExportedModules.Majorbbs( _serviceResolver.GetService <IClock>(), _serviceResolver.GetService <ILogger>(), _serviceResolver.GetService <AppSettings>(), _serviceResolver.GetService <IFileUtility>(), _serviceResolver.GetService <IGlobalCache>(), mbbsModule, testSessions, _serviceResolver.GetService <IAccountKeyRepository>(), _serviceResolver.GetService <IAccountRepository>(), textVariableService); galgsbl = new HostProcess.ExportedModules.Galgsbl( _serviceResolver.GetService <IClock>(), _serviceResolver.GetService <ILogger>(), _serviceResolver.GetService <AppSettings>(), _serviceResolver.GetService <IFileUtility>(), _serviceResolver.GetService <IGlobalCache>(), mbbsModule, testSessions, textVariableService); mbbsEmuCpuCore.Reset( mbbsEmuMemoryCore, (ordinal, functionOrdinal) => ExportedFunctionDelegate(ordinal, functionOrdinal, offsetsOnly: false), null, null); }
protected CpuTestBase() { mbbsEmuMemoryCore = mbbsEmuProtectedModeMemoryCore = new ProtectedModeMemoryCore(logger: null); mbbsEmuCpuCore = new CpuCore(logger: null); mbbsEmuCpuRegisters = mbbsEmuCpuCore; pit = new ProgrammableIntervalTimer(logger: null, fakeClock, mbbsEmuCpuCore); mbbsEmuCpuCore.Reset( mbbsEmuMemoryCore, null, null, new Dictionary <int, IIOPort> { { 0x40, pit }, { 0x41, pit }, { 0x42, pit }, { 0x43, pit }, }); }
public static void InfoStack(this ILogger l, ICpuRegisters registers, IMemoryCore memory) { var output = new StringBuilder(); l.Info("------------------------------------------"); l.Info($"SP: {registers.SP:X4} BP: {registers.BP:X4}"); l.Info("------------------------------------------"); for (var i = ushort.MaxValue; i >= registers.SP; i -= 2) { if (i == registers.SP && i == registers.BP) { output.Append("BP/SP-->"); } if (i != registers.SP && i == registers.BP) { output.Append(" BP-->"); } if (i == registers.SP && i != registers.BP) { output.Append(" SP-->"); } if (i != registers.SP && i != registers.BP) { output.Append(" "); } output.Append( $"{i:X4} [ {memory.GetWord(registers.SS, (ushort) (i-1)):D5} 0x{memory.GetWord(registers.SS, (ushort) (i-1)):X4} ] {i-1:X4}"); l.Info(output); output.Clear(); } }
/// <summary> /// Constructor for MbbsModule /// /// Pass in an empty/blank moduleIdentifier for a Unit Test/Fake Module /// </summary> /// <param name="logger"></param> /// <param name="moduleIdentifier">Will be null in a test</param> /// <param name="path"></param> /// <param name="memoryCore"></param> /// <param name="fileUtility"></param> public MbbsModule(IFileUtility fileUtility, IClock clock, ILogger logger, string moduleIdentifier, string path = "", MemoryCore memoryCore = null) { _fileUtility = fileUtility; _logger = logger; _clock = clock; ModuleIdentifier = moduleIdentifier; ModuleDlls = new List <MbbsDll>(); //Sanitize and setup Path if (string.IsNullOrEmpty(path)) { path = Directory.GetCurrentDirectory(); } if (!Path.EndsInDirectorySeparator(path)) { path += Path.DirectorySeparatorChar; } ModulePath = path; // will be null in tests if (string.IsNullOrEmpty(ModuleIdentifier)) { Mdf = MdfFile.createForTest(); ModuleDlls.Add(new MbbsDll(fileUtility, logger) { File = NEFile.createForTest() }); } else { //Verify MDF File Exists var mdfFile = fileUtility.FindFile(ModulePath, $"{ModuleIdentifier}.MDF"); var fullMdfFilePath = Path.Combine(ModulePath, mdfFile); if (!System.IO.File.Exists(fullMdfFilePath)) { throw new FileNotFoundException($"Unable to locate Module: {fullMdfFilePath}"); } Mdf = new MdfFile(fullMdfFilePath); var moduleDll = new MbbsDll(fileUtility, logger); moduleDll.Load(Mdf.DLLFiles[0].Trim(), ModulePath); ModuleDlls.Add(moduleDll); if (Mdf.Requires.Count > 0) { foreach (var r in Mdf.Requires) { var requiredDll = new MbbsDll(fileUtility, logger); if (requiredDll.Load(r.Trim(), ModulePath)) { requiredDll.SegmentOffset = (ushort)(ModuleDlls.Sum(x => x.File.SegmentTable.Count) + 1); ModuleDlls.Add(requiredDll); } } } if (Mdf.MSGFiles.Count > 0) { Msgs = new List <MsgFile>(Mdf.MSGFiles.Count); foreach (var m in Mdf.MSGFiles) { Msgs.Add(new MsgFile(ModulePath, m)); } } } //Set Initial Values RtkickRoutines = new PointerDictionary <RealTimeRoutine>(); RtihdlrRoutines = new PointerDictionary <RealTimeRoutine>(); TaskRoutines = new PointerDictionary <RealTimeRoutine>(); TextVariables = new Dictionary <string, FarPtr>(); GlobalCommandHandlers = new List <FarPtr>(); ExportedModuleDictionary = new Dictionary <ushort, IExportedModule>(6); ExecutionUnits = new Queue <ExecutionUnit>(2); Memory = memoryCore ?? new MemoryCore(); //Declare PSP Segment var psp = new PSPStruct { NextSegOffset = 0x9FFF, EnvSeg = 0xFFFF }; Memory.AddSegment(0x4000); Memory.SetArray(0x4000, 0, psp.Data); Memory.AllocateVariable("Int21h-PSP", sizeof(ushort)); Memory.SetWord("Int21h-PSP", 0x4000); //Find _INIT_ values if any foreach (var dll in ModuleDlls) { //If it's a Test, setup a fake _INIT_ if (string.IsNullOrEmpty(ModuleIdentifier)) { dll.EntryPoints["_INIT_"] = null; return; } //Setup _INIT_ Entrypoint FarPtr initEntryPointPointer; var initResidentName = dll.File.ResidentNameTable.FirstOrDefault(x => x.Name.StartsWith("_INIT__")); if (initResidentName == null) { //This only happens with MajorMUD -- I have no idea why it's a special little snowflake ¯\_(ツ)_/¯ _logger.Warn($"({moduleIdentifier}) Unable to locate _INIT_ in Resident Name Table, checking Non-Resident Name Table..."); var initNonResidentName = dll.File.NonResidentNameTable.FirstOrDefault(x => x.Name.StartsWith("_INIT__")); if (initNonResidentName == null) { throw new Exception("Unable to locate _INIT__ entry in Resident Name Table"); } var initEntryPoint = dll.File.EntryTable.First(x => x.Ordinal == initNonResidentName.IndexIntoEntryTable); initEntryPointPointer = new FarPtr((ushort)(initEntryPoint.SegmentNumber + dll.SegmentOffset), initEntryPoint.Offset); } else { var initEntryPoint = dll.File.EntryTable.First(x => x.Ordinal == initResidentName.IndexIntoEntryTable); initEntryPointPointer = new FarPtr((ushort)(initEntryPoint.SegmentNumber + dll.SegmentOffset), initEntryPoint.Offset); } _logger.Debug($"({ModuleIdentifier}) Located _INIT__: {initEntryPointPointer}"); dll.EntryPoints["_INIT_"] = initEntryPointPointer; } }
/// <summary> /// Constructor for MbbsModule /// /// Pass in an empty/blank moduleIdentifier for a Unit Test/Fake Module /// </summary> /// <param name="moduleIdentifier"></param> /// <param name="path"></param> /// <param name="memoryCore"></param> public MbbsModule(string moduleIdentifier, string path = "", MemoryCore memoryCore = null) { ModuleIdentifier = moduleIdentifier; //Sanitize and setup Path if (string.IsNullOrEmpty(path)) { path = Directory.GetCurrentDirectory(); } if (!path.EndsWith(Path.DirectorySeparatorChar)) { path += Path.DirectorySeparatorChar; } ModulePath = path; //Verify MDF File Exists if (!string.IsNullOrEmpty(ModuleIdentifier) && !System.IO.File.Exists($"{ModulePath}{ModuleIdentifier}.MDF")) { throw new FileNotFoundException($"Unable to locate Module: {ModulePath}{ModuleIdentifier}.MDF"); } Mdf = !string.IsNullOrEmpty(ModuleIdentifier) ? new MdfFile($"{ModulePath}{ModuleIdentifier}.MDF") : MdfFile.createForTest(); File = !string.IsNullOrEmpty(ModuleIdentifier) ? new NEFile($"{ModulePath}{Mdf.DLLFiles[0].Trim()}.DLL") : NEFile.createForTest(); if (Mdf.MSGFiles.Count > 0) { Msgs = new List <MsgFile>(Mdf.MSGFiles.Count); foreach (var m in Mdf.MSGFiles) { Msgs.Add(new MsgFile(ModulePath, m)); } } //Set Initial Values EntryPoints = new Dictionary <string, IntPtr16>(); RtkickRoutines = new PointerDictionary <RealTimeRoutine>(); RtihdlrRoutines = new PointerDictionary <RealTimeRoutine>(); TaskRoutines = new PointerDictionary <RealTimeRoutine>(); TextVariables = new Dictionary <string, IntPtr16>(); ExecutionUnits = new Queue <ExecutionUnit>(2); ExportedModuleDictionary = new Dictionary <ushort, IExportedModule>(4); GlobalCommandHandlers = new List <IntPtr16>(); Memory = memoryCore ?? new MemoryCore(); //If it's a Test, setup a fake _INIT_ if (string.IsNullOrEmpty(ModuleIdentifier)) { EntryPoints["_INIT_"] = null; return; } //Setup _INIT_ Entrypoint IntPtr16 initEntryPointPointer; var initResidentName = File.ResidentNameTable.FirstOrDefault(x => x.Name.StartsWith("_INIT__")); if (initResidentName == null) { //This only happens with MajorMUD -- I have no idea why it's a special little snowflake ¯\_(ツ)_/¯ _logger.Warn("Unable to locate _INIT_ in Resident Name Table, checking Non-Resident Name Table..."); var initNonResidentName = File.NonResidentNameTable.FirstOrDefault(x => x.Name.StartsWith("_INIT__")); if (initNonResidentName == null) { throw new Exception("Unable to locate _INIT__ entry in Resident Name Table"); } var initEntryPoint = File.EntryTable.First(x => x.Ordinal == initNonResidentName.IndexIntoEntryTable); initEntryPointPointer = new IntPtr16(initEntryPoint.SegmentNumber, initEntryPoint.Offset); } else { var initEntryPoint = File.EntryTable.First(x => x.Ordinal == initResidentName.IndexIntoEntryTable); initEntryPointPointer = new IntPtr16(initEntryPoint.SegmentNumber, initEntryPoint.Offset); } _logger.Info($"Located _INIT__: {initEntryPointPointer}"); EntryPoints["_INIT_"] = initEntryPointPointer; }
/// <summary> /// Constructor for MbbsModule /// /// Pass in an empty/blank moduleIdentifier for a Unit Test/Fake Module /// </summary> /// <param name="fileUtility"></param> /// <param name="clock"></param> /// <param name="logger"></param> /// <param name="moduleConfig"></param> /// <param name="memoryCore"></param> public MbbsModule(IFileUtility fileUtility, IClock clock, ILogger logger, ModuleConfiguration moduleConfig, ProtectedModeMemoryCore memoryCore = null) { _fileUtility = fileUtility; _logger = logger; _clock = clock; ModuleConfig = moduleConfig; ModuleIdentifier = moduleConfig.ModuleIdentifier; ModuleDlls = new List <MbbsDll>(); //Sanitize and setup Path if (string.IsNullOrEmpty(moduleConfig.ModulePath)) { moduleConfig.ModulePath = Directory.GetCurrentDirectory(); } if (!Path.EndsInDirectorySeparator(moduleConfig.ModulePath)) { moduleConfig.ModulePath += Path.DirectorySeparatorChar; } ModulePath = moduleConfig.ModulePath; // will be null in tests if (string.IsNullOrEmpty(ModuleIdentifier)) { Mdf = MdfFile.createForTest(); ModuleDlls.Add(new MbbsDll(fileUtility, logger) { File = NEFile.createForTest() }); } else { //Verify Module Path Exists if (!Directory.Exists(ModulePath)) { _logger.Error($"Unable to find the specified directory for the module {ModuleIdentifier.ToUpper()}: {ModulePath}"); _logger.Error("Please verify your Command Line Argument or the path specified in your Module JSON File and try again."); throw new DirectoryNotFoundException($"Unable to locate {ModulePath}"); } //Verify MDF File Exists var mdfFile = fileUtility.FindFile(ModulePath, $"{ModuleIdentifier}.MDF"); var fullMdfFilePath = Path.Combine(ModulePath, mdfFile); if (!File.Exists(fullMdfFilePath)) { _logger.Error($"Unable to locate {fullMdfFilePath}"); _logger.Error($"Please verify your Command Line Argument or the Module JSON File to ensure {ModuleIdentifier} is the correct Module Identifier and that the Module is installed properly."); throw new FileNotFoundException($"Unable to locate Module: {fullMdfFilePath}"); } Mdf = new MdfFile(fullMdfFilePath); LoadModuleDll(Mdf.DLLFiles[0].Trim()); if (Mdf.MSGFiles.Count > 0) { Msgs = new List <MsgFile>(Mdf.MSGFiles.Count); foreach (var m in Mdf.MSGFiles) { Msgs.Add(new MsgFile(_fileUtility, ModulePath, m)); } } } //Set Initial Values RtkickRoutines = new PointerDictionary <RealTimeRoutine>(); RtihdlrRoutines = new PointerDictionary <RealTimeRoutine>(); TaskRoutines = new PointerDictionary <RealTimeRoutine>(); GlobalCommandHandlers = new List <FarPtr>(); ExportedModuleDictionary = new Dictionary <ushort, IExportedModule>(6); ExecutionUnits = new Queue <ExecutionUnit>(2); Memory = memoryCore ?? new ProtectedModeMemoryCore(logger); ProtectedMemory = (ProtectedModeMemoryCore)Memory; //Declare PSP Segment var psp = new PSPStruct { NextSegOffset = 0x9FFF, EnvSeg = 0xFFFF }; ProtectedMemory.AddSegment(0x4000); Memory.SetArray(0x4000, 0, psp.Data); Memory.AllocateVariable("Int21h-PSP", sizeof(ushort)); Memory.SetWord("Int21h-PSP", 0x4000); //Find _INIT_ values if any foreach (var dll in ModuleDlls) { //If it's a Test, setup a fake _INIT_ if (string.IsNullOrEmpty(ModuleIdentifier)) { dll.EntryPoints["_INIT_"] = null; return; } //Setup _INIT_ Entrypoint FarPtr initEntryPointPointer; var initResidentName = dll.File.ResidentNameTable.FirstOrDefault(x => x.Name.StartsWith("_INIT__")); if (initResidentName == null) { //This only happens with MajorMUD -- I have no idea why it's a special little snowflake ¯\_(ツ)_/¯ _logger.Warn($"({moduleConfig.ModuleIdentifier}) Unable to locate _INIT_ in Resident Name Table, checking Non-Resident Name Table..."); var initNonResidentName = dll.File.NonResidentNameTable.FirstOrDefault(x => x.Name.StartsWith("_INIT__")); if (initNonResidentName == null) { _logger.Error($"Unable to locate _INIT__ entry in Resident Name Table for {dll.File.FileName}"); continue; } var initEntryPoint = dll.File.EntryTable.First(x => x.Ordinal == initNonResidentName.IndexIntoEntryTable); initEntryPointPointer = new FarPtr((ushort)(initEntryPoint.SegmentNumber + dll.SegmentOffset), initEntryPoint.Offset); } else { var initEntryPoint = dll.File.EntryTable.First(x => x.Ordinal == initResidentName.IndexIntoEntryTable); initEntryPointPointer = new FarPtr((ushort)(initEntryPoint.SegmentNumber + dll.SegmentOffset), initEntryPoint.Offset); } _logger.Debug($"({ModuleIdentifier}) Located _INIT__: {initEntryPointPointer}"); dll.EntryPoints["_INIT_"] = initEntryPointPointer; } }