Archived
1
0

split files for better overall performance

This commit is contained in:
Erick
2022-02-07 15:04:29 +01:00
parent c0d3b57177
commit 5b70a9e547
5 changed files with 173 additions and 141 deletions
+126
View File
@@ -0,0 +1,126 @@
//FIXME: all includes maybe unnecessary
#include "preprocessor.h"
#include "registers.c"
#include "lc3_lib.h"
uint16_t sign_extend(uint16_t n, int bit_count) {
if((n >> (bit_count - 1)) & 1) {
n |= (0xFFFF << bit_count);
}
return n;
}
int swap16(uint16_t x) {
return (x << 8) || (x >> 8);
}
void update_flags(uint16_t r) {
if(reg[r] == 0) reg[RG_COND] = FL_Z;
else if(reg[r] >> 15) reg[RG_COND] = FL_N;
else reg[RG_COND] = FL_P;
}
/* function to load assembly programs*/
void read_image_file(FILE* file) {
/* image placement memory location */
uint16_t origin;
fread(&origin, sizeof(origin), 1, file);
origin = swap16(origin);
uint16_t max_read = UINT16_MAX - origin;
uint16_t* p = memory + origin;
size_t read = fread(p, sizeof(uint16_t), max_read, file);
/* swapping to little endian */
while (read-- > 0) {
*p = swap16(*p);
++p;
}
}
int read_image(const char* file) {
FILE* image= fopen(file,"rb");
if(!image){
return 0;
}
uint16_t origin;
fread(&origin,sizeof(origin),1,image);
origin = swap16(origin);
uint16_t max_read = UINT16_MAX - origin;
uint16_t* i = memory + origin;
size_t read = fread(i,sizeof(uint16_t),max_read,image);
while(read-- > 0){
*i = swap16(*i);
++i;
}
return 1;
}
/* Defining functions for Unix or Windows based systems */
#ifdef __UNIX
uint16_t check_key() {
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(1, &readfds, NULL, NULL, &timeout) != 0;
struct termios original_tio;
void disable_input_buffering() {
tcgetattr(STDIN_FILENO, &original_tio);
struct termios new_tio = original_tio;
new_tio.c_lflag &= ~ICANON & ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
}
void restore_input_buffering() {
tcsetattr(STDIN_FILENO, TCSANOW, &original_tio);
}
}
#else
uint16_t check_key() {
return WaitForSingleObject(hStdin, 1000) == WAIT_OBJECT_0 && _kbhit();
}
DWORD fdwMode, fdwOldMode;
void disable_input_buffering() {
hStdin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hStdin, &fdwOldMode); // save old mode
fdwMode = fdwOldMode
^ ENABLE_ECHO_INPUT // no input echo
^ ENABLE_LINE_INPUT; // return when one or more characters are available
SetConsoleMode(hStdin, fdwMode); // set new mode
FlushConsoleInputBuffer(hStdin); // clear buffer
}
void restore_input_buffering() {
SetConsoleMode(hStdin, fdwOldMode);
}
#endif
void mem_write(uint16_t address, uint16_t val) {
memory[address] = val;
}
int mem_read(uint16_t address) {
if(address == MMR_KSR) {
if(check_key()) {
memory[MMR_KSR] = 1 << 15;
memory[MMR_KDR] = getchar();
} else memory[MMR_KSR] = 0;
}
return memory[address];
}
void handle_interrupt(int signal) {
restore_input_buffering();
printf("\n");
exit(-2);
}
+32
View File
@@ -0,0 +1,32 @@
/* Library of functions used in tyvm.c */
/* 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);
/* Swap to big endian */
int swap16(uint16_t x);
/* Flag update function
Every time a value is written to a register the flag will be updated */
void update_flags(uint16_t r);
/* Load assembly file*/
void read_image_file(FILE* file);
/* Read loaded assembly file */
int read_image(const char* file);
/* Input buffering */
void disable_input_buffering();
void restore_input_buffering();
/* Write to memory address */
void mem_write(uint16_t address, uint16_t val);
/* Read memory address */
int mem_read(uint16_t address);
/* Handle interrupt */
void handle_interrupt(int signal);
+11 -2
View File
@@ -1,4 +1,6 @@
/* List of all the libraries needed to run tyvm.c */
/* preprocessor directves needed to run tyvm.c */
#define __UNIX // used to modify code whether compiling on a Unix-based OS or a Windows machine
/* universal libraries */
#include <stdint.h>
@@ -21,4 +23,11 @@
#else
#include <Windows.h>
#include <conio.h>
#endif
#endif
#ifndef __UNIX
HANDLE hStdin = INVALID_HANDLE_VALUE;
#endif
#define TRUE 1
#define FALSE 0
View File
+4 -139
View File
@@ -4,151 +4,16 @@
Open-source software distributed under GNU GPL v.3 license
*/
#define __UNIX // used to modify code whether compiling on a Unix-based OS or a Windows machine
#include "includes.h"
#include "registers.h"
#ifndef __UNIX
HANDLE hStdin = INVALID_HANDLE_VALUE;
#endif
#define TRUE 1
#define FALSE 0
#include "preprocessor.h"
#include "registers.c"
#include "lc3_lib.h"
/* 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) {
if((n >> (bit_count - 1)) & 1) {
n |= (0xFFFF << bit_count);
}
return n;
}
/* function to swap to big endian */
int swap16(uint16_t x) {
return (x << 8) || (x >> 8);
}
/* Flag update function
Every time a value is written to a register the flag will be updated */
void update_flags(uint16_t r) {
if(reg[r] == 0) reg[RG_COND] = FL_Z;
else if(reg[r] >> 15) reg[RG_COND] = FL_N;
else reg[RG_COND] = FL_P;
}
/* function to load assembly programs*/
void read_image_file(FILE* file) {
/* image placement memory location */
uint16_t origin;
fread(&origin, sizeof(origin), 1, file);
origin = swap16(origin);
uint16_t max_read = UINT16_MAX - origin;
uint16_t* p = memory + origin;
size_t read = fread(p, sizeof(uint16_t), max_read, file);
/* swapping to little endian */
while (read-- > 0) {
*p = swap16(*p);
++p;
}
}
int read_image(const char* file) {
FILE* image= fopen(file,"rb");
if(!image){
return 0;
}
uint16_t origin;
fread(&origin,sizeof(origin),1,image);
origin = swap16(origin);
uint16_t max_read = UINT16_MAX - origin;
uint16_t* i = memory + origin;
size_t read = fread(i,sizeof(uint16_t),max_read,image);
while(read-- > 0){
*i = swap16(*i);
++i;
}
return 1;
}
#ifdef __UNIX
uint16_t check_key() {
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(1, &readfds, NULL, NULL, &timeout) != 0;
}
#else
uint16_t check_key() {
return WaitForSingleObject(hStdin, 1000) == WAIT_OBJECT_0 && _kbhit();
}
#endif
void mem_write(uint16_t address, uint16_t val) {
memory[address] = val;
}
int mem_read(uint16_t address) {
if(address == MMR_KSR) {
if(check_key()) {
memory[MMR_KSR] = 1 << 15;
memory[MMR_KDR] = getchar();
} else memory[MMR_KSR] = 0;
}
return memory[address];
}
#ifdef __UNIX
struct termios original_tio;
void disable_input_buffering() {
tcgetattr(STDIN_FILENO, &original_tio);
struct termios new_tio = original_tio;
new_tio.c_lflag &= ~ICANON & ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
}
void restore_input_buffering() {
tcsetattr(STDIN_FILENO, TCSANOW, &original_tio);
}
#else
DWORD fdwMode, fdwOldMode;
void disable_input_buffering() {
hStdin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hStdin, &fdwOldMode); // save old mode
fdwMode = fdwOldMode
^ ENABLE_ECHO_INPUT // no input echo
^ ENABLE_LINE_INPUT; // return when one or more characters are available
SetConsoleMode(hStdin, fdwMode); // set new mode
FlushConsoleInputBuffer(hStdin); // clear buffer
}
void restore_input_buffering() {
SetConsoleMode(hStdin, fdwOldMode);
}
#endif
void handle_interrupt(int signal) {
restore_input_buffering();
printf("\n");
exit(-2);
}
int main(int argc, const char* argv[]) {
if(argc != 2) {
printf("usage: [image-file1] ...\n");
printf("loading image: %s\n", argv[2]);
exit(2);
}