separate preprocessor directives into smaller libraries
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
TVM is a virtual machine for LC-3 based operating systems and it is used for educational purposes.
|
||||
Copyright (c) under MIT license
|
||||
Written by Erick Ahmed, 2022
|
||||
*/
|
||||
|
||||
/* List of all the libraries needed to run tyvm.c */
|
||||
|
||||
//#define __UNIX // uncomment ONLY if compiling for unix-based operative system
|
||||
|
||||
/* universal libraries */
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
|
||||
/* unix only libraries */
|
||||
#ifdef __UNIX
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
/* sys libraries */
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/termios.h>
|
||||
#include <sys/mman.h>
|
||||
/* windows only libraries */
|
||||
#else
|
||||
#include <Windows.h>
|
||||
#include <conio.h>
|
||||
#endif
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
TVM is a virtual machine for LC-3 based operating systems and it is used for educational purposes.
|
||||
Copyright (c) under MIT license
|
||||
Written by Erick Ahmed, 2022
|
||||
*/
|
||||
|
||||
|
||||
/* memory mapped register tables */
|
||||
enum mmr {
|
||||
MMR_KSR = 0xFE00, // keyboard status
|
||||
MMR_KDR = 0xFE02 // keyboard data
|
||||
};
|
||||
|
||||
/* Initializing 10 registers of which:
|
||||
8 general purpose, 1 program counter, 1 conditional */
|
||||
enum registers {
|
||||
RG_000 = 0,
|
||||
RG_001,
|
||||
RG_010,
|
||||
RG_011,
|
||||
RG_100,
|
||||
RG_101,
|
||||
RG_110,
|
||||
RG_111,
|
||||
RG_PC, // program counter
|
||||
RG_COND, // condition flag
|
||||
RG_COUNT
|
||||
};
|
||||
|
||||
/* Trap codes used for OP_TRAP */
|
||||
enum trapcodes {
|
||||
TRAP_GETC = 0x20, // get charcter from keyboard
|
||||
TRAP_OUT = 0x21, // output a character
|
||||
TRAP_PUTS = 0x22, // output a word string
|
||||
TRAP_IN = 0x23, // get charcter from keyboard and echo to terminal
|
||||
TRAP_PUTSP = 0x24, // output a byte string
|
||||
TRAP_HALT = 0x25 // halt program
|
||||
};
|
||||
|
||||
/* Initializing memory and register storages */
|
||||
uint16_t memory[UINT16_MAX];
|
||||
uint16_t reg[RG_COUNT];
|
||||
|
||||
/* Creating instruction set opcodes */
|
||||
enum opcodes { // [name, 8-bit value]
|
||||
OP_BR = 0, // branch, 0000
|
||||
OP_ADD, // add, 0001
|
||||
OP_LD, // load, 0010
|
||||
OP_ST, // store, 0011
|
||||
OP_JSR, // jump register, 0100
|
||||
OP_AND, // bitwise and, 0101
|
||||
OP_LDR, // load register, 0110
|
||||
OP_STR, // store register, 0111
|
||||
OP_RTI, // unused opcode
|
||||
OP_NOT, // bitwise not, 1001
|
||||
OP_LDI, // indirect load, 1010
|
||||
OP_STI, // indirect store, 1011
|
||||
OP_JMP, // jump, 1100
|
||||
OP_RES, // reserved opcode,
|
||||
OP_LEA, // load effective address, 1110
|
||||
OP_TRAP, // execute trap, 1111
|
||||
};
|
||||
|
||||
/* Creating condition flags */
|
||||
enum flags {
|
||||
FL_P = 1, // Positive
|
||||
FL_Z = 1 << 1, // Zero
|
||||
FL_N = 1 << 2, // Negative
|
||||
};
|
||||
+7
-96
@@ -4,33 +4,10 @@
|
||||
Written by Erick Ahmed, 2022
|
||||
*/
|
||||
|
||||
//#define __UNIX // uncomment ONLY if compiling on unix-based operative system
|
||||
|
||||
/* universal libraries */
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
|
||||
/* unix only libraries */
|
||||
#ifdef __UNIX
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
/* sys libraries */
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/termios.h>
|
||||
#include <sys/mman.h>
|
||||
/* windows only libraries */
|
||||
#else
|
||||
#include <Windows.h>
|
||||
#include <conio.h>
|
||||
#endif
|
||||
#include "includes.h"
|
||||
#include "registers.h"
|
||||
|
||||
#ifndef __UNIX
|
||||
/* windows only declaration */
|
||||
HANDLE hStdin = INVALID_HANDLE_VALUE;
|
||||
#endif
|
||||
|
||||
@@ -38,69 +15,6 @@
|
||||
#define FALSE 0
|
||||
|
||||
|
||||
/* memory mapped register tables */
|
||||
enum mmr {
|
||||
MMR_KSR = 0xFE00, // keyboard status
|
||||
MMR_KDR = 0xFE02 // keyboard data
|
||||
};
|
||||
|
||||
/* Initializing 10 registers of which:
|
||||
8 general purpose, 1 program counter, 1 conditional */
|
||||
enum registers {
|
||||
RG_000 = 0,
|
||||
RG_001,
|
||||
RG_010,
|
||||
RG_011,
|
||||
RG_100,
|
||||
RG_101,
|
||||
RG_110,
|
||||
RG_111,
|
||||
RG_PC, // program counter
|
||||
RG_COND, // condition flag
|
||||
RG_COUNT
|
||||
};
|
||||
|
||||
/* Trap codes used for OP_TRAP */
|
||||
enum trapcodes {
|
||||
TRAP_GETC = 0x20, // get charcter from keyboard
|
||||
TRAP_OUT = 0x21, // output a character
|
||||
TRAP_PUTS = 0x22, // output a word string
|
||||
TRAP_IN = 0x23, // get charcter from keyboard and echo to terminal
|
||||
TRAP_PUTSP = 0x24, // output a byte string
|
||||
TRAP_HALT = 0x25 // halt program
|
||||
};
|
||||
|
||||
/* Initializing memory and register storages */
|
||||
uint16_t memory[UINT16_MAX];
|
||||
uint16_t reg[RG_COUNT];
|
||||
|
||||
/* Creating instruction set opcodes */
|
||||
enum opcodes { // [name, 8-bit value]
|
||||
OP_BR = 0, // branch, 0000
|
||||
OP_ADD, // add, 0001
|
||||
OP_LD, // load, 0010
|
||||
OP_ST, // store, 0011
|
||||
OP_JSR, // jump register, 0100
|
||||
OP_AND, // bitwise and, 0101
|
||||
OP_LDR, // load register, 0110
|
||||
OP_STR, // store register, 0111
|
||||
OP_RTI, // unused opcode
|
||||
OP_NOT, // bitwise not, 1001
|
||||
OP_LDI, // indirect load, 1010
|
||||
OP_STI, // indirect store, 1011
|
||||
OP_JMP, // jump, 1100
|
||||
OP_RES, // reserved opcode,
|
||||
OP_LEA, // load effective address, 1110
|
||||
OP_TRAP, // execute trap, 1111
|
||||
};
|
||||
|
||||
/* Creating condition flags */
|
||||
enum flags {
|
||||
FL_P = 1, // Positive
|
||||
FL_Z = 1 << 1, // Zero
|
||||
FL_N = 1 << 2, // Negative
|
||||
};
|
||||
|
||||
/* Sign extension function for immediate add mode (imm5[0:4])
|
||||
transforms 5bit number to 8bit number preserving sign*/
|
||||
uint16_t sign_extend(uint16_t n, int bit_count) {
|
||||
@@ -215,14 +129,11 @@ int mem_read(uint16_t address) {
|
||||
}
|
||||
#endif
|
||||
|
||||
//#ifdef __UNIX
|
||||
void handle_interrupt(int signal) {
|
||||
restore_input_buffering();
|
||||
printf("\n");
|
||||
exit(-2);
|
||||
}
|
||||
//#endif
|
||||
|
||||
void handle_interrupt(int signal) {
|
||||
restore_input_buffering();
|
||||
printf("\n");
|
||||
exit(-2);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
|
||||
Reference in New Issue
Block a user