Skip to content

Commit

Permalink
Ported the application to C#.
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-beep committed Nov 11, 2023
1 parent 16aed9b commit 2852a56
Show file tree
Hide file tree
Showing 19 changed files with 927 additions and 442 deletions.
450 changes: 450 additions & 0 deletions C#/Program.cs

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions C#/qLiteVM.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions C#/qLiteVM.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34221.43
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "qLiteVM", "qLiteVM.csproj", "{C002CD49-DFA1-4EF4-89E2-3DB5AD5CA967}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C002CD49-DFA1-4EF4-89E2-3DB5AD5CA967}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C002CD49-DFA1-4EF4-89E2-3DB5AD5CA967}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C002CD49-DFA1-4EF4-89E2-3DB5AD5CA967}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C002CD49-DFA1-4EF4-89E2-3DB5AD5CA967}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CBC41D6F-4422-4688-A8FA-882053F184E9}
EndGlobalSection
EndGlobal
File renamed without changes.
98 changes: 49 additions & 49 deletions io_control.c → C/io_control.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
#include "io_control.h"
#include <Windows.h>
#include <conio.h>

HANDLE hStdin = INVALID_HANDLE_VALUE;
DWORD fdwMode, fdwOldMode;

/**
* Disables line input and echo input modes in the console.
*
* This function retrieves the current input mode of the console, disables line input and echo input
* modes, and sets this new mode. It's typically used to allow the console to handle input more
* directly, which is suitable for scenarios where immediate input response is needed. It also
* flushes the console input buffer to ensure a clean state.
*/
void DisableInputBuffering()
{
hStdin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hStdin, &fdwOldMode);
fdwMode = fdwOldMode
^ ENABLE_ECHO_INPUT
^ ENABLE_LINE_INPUT;

SetConsoleMode(hStdin, fdwMode);
FlushConsoleInputBuffer(hStdin);
}

/**
* Restores the original input buffering settings of the console.
*
* This function resets the console mode to its original settings, effectively re-enabling line
* input and echo input modes if they were previously disabled. This is typically called before
* the program exits or when the program no longer requires direct input handling.
*/
void RestoreInputBuffering()
{
SetConsoleMode(hStdin, fdwOldMode);
}

/**
* Checks if a key has been pressed in the console.
*
* This function waits for an event on the standard input (with a specified timeout) and then checks
* if a key has been pressed (non-blocking). It's used to poll the console for keyboard input without
* pausing execution of the program. The WAIT_TIME constant defines the wait duration.
*/
uint16_t CheckKey()
{
return WaitForSingleObject(hStdin, WAIT_TIME) == WAIT_OBJECT_0 && _kbhit();
#include "io_control.h"
#include <Windows.h>
#include <conio.h>

HANDLE hStdin = INVALID_HANDLE_VALUE;
DWORD fdwMode, fdwOldMode;

/**
* Disables line input and echo input modes in the console.
*
* This function retrieves the current input mode of the console, disables line input and echo input
* modes, and sets this new mode. It's typically used to allow the console to handle input more
* directly, which is suitable for scenarios where immediate input response is needed. It also
* flushes the console input buffer to ensure a clean state.
*/
void DisableInputBuffering()
{
hStdin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hStdin, &fdwOldMode);
fdwMode = fdwOldMode
^ ENABLE_ECHO_INPUT
^ ENABLE_LINE_INPUT;

SetConsoleMode(hStdin, fdwMode);
FlushConsoleInputBuffer(hStdin);
}

/**
* Restores the original input buffering settings of the console.
*
* This function resets the console mode to its original settings, effectively re-enabling line
* input and echo input modes if they were previously disabled. This is typically called before
* the program exits or when the program no longer requires direct input handling.
*/
void RestoreInputBuffering()
{
SetConsoleMode(hStdin, fdwOldMode);
}

/**
* Checks if a key has been pressed in the console.
*
* This function waits for an event on the standard input (with a specified timeout) and then checks
* if a key has been pressed (non-blocking). It's used to poll the console for keyboard input without
* pausing execution of the program. The WAIT_TIME constant defines the wait duration.
*/
uint16_t CheckKey()
{
return WaitForSingleObject(hStdin, WAIT_TIME) == WAIT_OBJECT_0 && _kbhit();
}
52 changes: 26 additions & 26 deletions io_control.h → C/io_control.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#ifndef IO_CONTROL_H
#define IO_CONTROL_H
#define WAIT_TIME 1000

#include <stdint.h>

/**
* Disables line input and echo input modes in the console.
*
*/
void DisableInputBuffering();

/**
* Restores the original input buffering settings of the console.
*
*/
void RestoreInputBuffering();

/**
* Checks if a key has been pressed in the console.
*
* @return A nonzero value if a key is pressed; otherwise, 0.
*
*/
uint16_t CheckKey();

#ifndef IO_CONTROL_H
#define IO_CONTROL_H
#define WAIT_TIME 1000

#include <stdint.h>

/**
* Disables line input and echo input modes in the console.
*
*/
void DisableInputBuffering();

/**
* Restores the original input buffering settings of the console.
*
*/
void RestoreInputBuffering();

/**
* Checks if a key has been pressed in the console.
*
* @return A nonzero value if a key is pressed; otherwise, 0.
*
*/
uint16_t CheckKey();

#endif
File renamed without changes.
58 changes: 29 additions & 29 deletions main.h → C/main.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#ifndef MAIN_H
#define MAIN_H

#include <stdint.h>

enum
{
PC_START = 0x3000
};

extern int running;

void HandleADD(uint16_t instr);
void HandleAND(uint16_t instr);
void HandleNOT(uint16_t instr);
void HandleBR(uint16_t instr);
void HandleJMP(uint16_t instr);
void HandleJSR(uint16_t instr);
void HandleLD(uint16_t instr);
void HandleLDI(uint16_t instr);
void HandleLDR(uint16_t instr);
void HandleLEA(uint16_t instr);
void HandleST(uint16_t instr);
void HandleSTI(uint16_t instr);
void HandleSTR(uint16_t instr);
void HandleTRAP(uint16_t instr);

void Exit();

#ifndef MAIN_H
#define MAIN_H

#include <stdint.h>

enum
{
PC_START = 0x3000
};

extern int running;

void HandleADD(uint16_t instr);
void HandleAND(uint16_t instr);
void HandleNOT(uint16_t instr);
void HandleBR(uint16_t instr);
void HandleJMP(uint16_t instr);
void HandleJSR(uint16_t instr);
void HandleLD(uint16_t instr);
void HandleLDI(uint16_t instr);
void HandleLDR(uint16_t instr);
void HandleLEA(uint16_t instr);
void HandleST(uint16_t instr);
void HandleSTI(uint16_t instr);
void HandleSTR(uint16_t instr);
void HandleTRAP(uint16_t instr);

void Exit();

#endif
86 changes: 43 additions & 43 deletions vm_core.c → C/vm_core.c
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
#include "vm_core.h"
#include "io_control.h"
#include "vm_globals.h"
#include <stdio.h>
#include <stdlib.h>

/**
* Handles an interrupt signal.
*
* This function is designed to be called when an interrupt signal is received by the program.
* It performs necessary cleanup tasks such as restoring the input buffering state. It then
* prints a newline character for readability and exits the program with an error code (-2).
* This function is typically used as a signal handler for abrupt termination signals like SIGINT.
*/
void HandleInterrupt(int signal)
{
RestoreInputBuffering();
printf("\n");
exit(-2);
}

/**
* Updates the condition flags in the LC3 virtual machine.
*
* This function updates the condition flags in the R_COND register based on the value of
* the specified register (denoted by 'r'). The flags set are: FL_ZRO if the register value
* is zero, FL_NEG if the register value is negative (sign bit is set), and FL_POS otherwise
* (i.e., the value is positive).
*/
void UpdateFlags(uint16_t r)
{
if (reg[r] == 0)
{
reg[R_COND] = FL_ZRO;
}
else if (reg[r] >> 15)
{
reg[R_COND] = FL_NEG;
}
else
{
reg[R_COND] = FL_POS;
}
#include "vm_core.h"
#include "io_control.h"
#include "vm_globals.h"
#include <stdio.h>
#include <stdlib.h>

/**
* Handles an interrupt signal.
*
* This function is designed to be called when an interrupt signal is received by the program.
* It performs necessary cleanup tasks such as restoring the input buffering state. It then
* prints a newline character for readability and exits the program with an error code (-2).
* This function is typically used as a signal handler for abrupt termination signals like SIGINT.
*/
void HandleInterrupt(int signal)
{
RestoreInputBuffering();
printf("\n");
exit(-2);
}

/**
* Updates the condition flags in the LC3 virtual machine.
*
* This function updates the condition flags in the R_COND register based on the value of
* the specified register (denoted by 'r'). The flags set are: FL_ZRO if the register value
* is zero, FL_NEG if the register value is negative (sign bit is set), and FL_POS otherwise
* (i.e., the value is positive).
*/
void UpdateFlags(uint16_t r)
{
if (reg[r] == 0)
{
reg[R_COND] = FL_ZRO;
}
else if (reg[r] >> 15)
{
reg[R_COND] = FL_NEG;
}
else
{
reg[R_COND] = FL_POS;
}
}
42 changes: 21 additions & 21 deletions vm_core.h → C/vm_core.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#ifndef VM_CORE_H
#define VM_CORE_H

#include <stdint.h>

/**
* Handles an interrupt signal.
*
* @param signal The signal number that triggered the interrupt.
*
*/
void HandleInterrupt(int signal);

/**
* Updates the condition flags in the LC3 virtual machine.
*
* @param r The index of the register whose value is used to set the condition flags.
*
*/
void UpdateFlags(uint16_t r);

#ifndef VM_CORE_H
#define VM_CORE_H

#include <stdint.h>

/**
* Handles an interrupt signal.
*
* @param signal The signal number that triggered the interrupt.
*
*/
void HandleInterrupt(int signal);

/**
* Updates the condition flags in the LC3 virtual machine.
*
* @param r The index of the register whose value is used to set the condition flags.
*
*/
void UpdateFlags(uint16_t r);

#endif
Loading

0 comments on commit 2852a56

Please sign in to comment.