Skip to content

Commit

Permalink
Merge pull request #73 from algosup/vproc-improve
Browse files Browse the repository at this point in the history
Virtual processor and assembler stable version
  • Loading branch information
MistzSoftware authored Feb 23, 2024
2 parents d367dc5 + 5f7bb2d commit 5b744fc
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set(CMAKE_C_COMPILER "gcc")

project(
Interpreter-AT2
VERSION 0.5.0
VERSION 1.0.0
LANGUAGES CXX C
)

Expand Down
2 changes: 1 addition & 1 deletion src/2at2.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "error.h"
#include "binExporter.h"

#define VERSION "0.5.0"
#define VERSION "1.0.0"
#define BIN_NAME "bin.2at2"

int main(int argc, char *argv[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void errorFileIssues(const char *filename, asm_error_t *errData){
char *errType = "File Issues Error";
char errDetails[64];

sprintf(errDetails, "Error: the targeted file '%s' contains errors and couldn't be converted properly", filename);
sprintf(errDetails, "Error: the targeted file '%s' contains errors", filename);

displayError(errType, errDetails, NULL, errorFile, errData);
}
Expand Down
122 changes: 115 additions & 7 deletions src/vProc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
This file is part of VAT2.
This file contains the implementation of the virtual processor.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Expand All @@ -11,13 +10,17 @@

#define LINE_MAX_BITS 16
#define VAR_LIST_SIZE 256
#define MAX_CALL_STACK 256
#define CLOCK_TICKS 1/20

// variables
vProcVar_t vProcVars[VAR_LIST_SIZE];

int lastVarIdx = 0;

fpos_t cursorPos[MAX_CALL_STACK];
int callStackSize = 0;

// registers
vRegister_t rg0 = {true, 0};
vRegister_t rg1 = {true, 0};
Expand All @@ -28,6 +31,33 @@ vRegister_t rg5 = {true, 0};
vRegister_t rg6 = {true, 0};
vRegister_t rg7 = {true, 0};

bool readFile(char *filename, asm_error_t *errData){
//Check if the file is present
FILE *file = fopen(filename, "rb");
if(file == NULL){
errorfnf(filename, errData);
}
// Run program
char line[LINE_MAX_BITS];

// Set as carry for action on next instruction
carry_t carry = {0, false};

int time = 0;
int lastTime = 0;
int latentTicks = 0;
while(fgets(line, LINE_MAX_BITS + 1, file)) {
setClock(time, lastTime, latentTicks);
instruction_t inst = charBinToInst(line);
if(!run(inst, &carry, file, filename, errData)){
fclose(file);
return false;
}
}
fclose(file);
return true;
}

void setClock(int time, int lastTime, int latentTicks){
sleep(CLOCK_TICKS);
++latentTicks;
Expand All @@ -47,9 +77,9 @@ instruction_t charBinToInst(char *bin){
instruction_t instruction = {0, 0, 0};

// Define masks for each field
unsigned int instMask = 0b11111;
unsigned int regMask = 0b111;
unsigned int argMask = 0b0000000011111111;
unsigned int instMask = 31; // 0b11111
unsigned int regMask = 7; // 0b111
unsigned int argMask = 255; // 0b0000000011111111

// Extract each field using bitwise AND and shift operations
instruction.inst = (inst >> 11) & instMask;
Expand All @@ -59,8 +89,9 @@ instruction_t charBinToInst(char *bin){
return instruction;
}

bool run(instruction_t inst, carry_t *carry, asm_error_t *errData){
bool run(instruction_t inst, carry_t *carry, FILE *file, char *filename, asm_error_t *errData){
vRegister_t *reg;
fpos_t pos;
// check if there is a carry
if(carry->isUsed && inst.inst != 24){
inst.arg = carry->nextArg;
Expand All @@ -75,11 +106,23 @@ bool run(instruction_t inst, carry_t *carry, asm_error_t *errData){
reg->value = inst.arg;
return true;
case 1: //goto
pos = searchLabel(inst.arg, filename, errData);
// goto label
fsetpos(file, &pos);
return true;
case 2: //call
// get call position
fgetpos(file, &pos);
if(!addCallPos(pos)){
return false;
}
// search label
pos = searchLabel(inst.arg, filename, errData);
// goto label
fsetpos(file, &pos);
return true;
case 3: //int
return runInt(inst, carry, errData);
return runInt(inst, carry);
case 4: //push
return true;
case 5: //xor
Expand Down Expand Up @@ -166,6 +209,13 @@ bool run(instruction_t inst, carry_t *carry, asm_error_t *errData){
}
return opMod(reg, inst.arg, errData);
case 22: //ret
// get call next position
pos = cursorPos[0];
if(!removeCallPos()){
return false;
}
// go to new position
fsetpos(file, &pos);
return true;
case 23: //mov from var
// set carry to var data
Expand Down Expand Up @@ -223,7 +273,7 @@ bool run(instruction_t inst, carry_t *carry, asm_error_t *errData){
}
}

bool runInt(instruction_t inst, carry_t *carry, asm_error_t *errData){
bool runInt(instruction_t inst, carry_t *carry){
switch(inst.arg){
case 0: //nigeru
return false;
Expand Down Expand Up @@ -292,6 +342,35 @@ vRegister_t *getRegister(int reg){
}
}

fpos_t searchLabel(int labId, char *filename, asm_error_t *errData){
FILE *file = fopen(filename, "rb");
if(file == NULL){
errorfnf(filename, errData);
exit(EXIT_FAILURE);
}
char line[LINE_MAX_BITS];
fpos_t pos;
while(fgets(line, LINE_MAX_BITS + 1, file)) {
if(line[0] == '1'){
int inst = (int)strtoul(line, NULL, 2);
unsigned int instMask = 31; // 0b11111
unsigned int argMask = 255; // 0b0000000011111111

int instId = (inst >> 11) & instMask;
int arg = inst & argMask;

if(instId == 18 && arg == labId){
fgetpos(file, &pos);
fclose(file);
return pos;
}
}
}
fclose(file);
exit(EXIT_FAILURE);
}


bool opAdd(vRegister_t *reg, unsigned int arg, asm_error_t *errData){
if(reg->writable){
reg->value += arg;
Expand Down Expand Up @@ -413,6 +492,35 @@ bool opShr(vRegister_t *reg, unsigned int arg, asm_error_t *errData){
}
}

bool addCallPos(fpos_t pos) {
if (callStackSize >= MAX_CALL_STACK) {
fprintf(stderr, "Call stack overflow\n");
return false;
}

for (int i = callStackSize; i > 0; i--) {
cursorPos[i] = cursorPos[i - 1];
}

cursorPos[0] = pos;
++callStackSize;
return true;
}

bool removeCallPos() {
if (callStackSize == 0) {
fprintf(stderr, "Call stack is empty\n");
return false;
}

for (int i = 0; i < callStackSize - 1; i++) {
cursorPos[i] = cursorPos[i + 1];
}

--callStackSize;
return true;
}

void printVar(int idx){
printf("Var %d: ", idx);
for(int i = 0; i < vProcVars[idx].size; i++){
Expand Down
46 changes: 43 additions & 3 deletions src/vProc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

Expand Down Expand Up @@ -35,6 +36,17 @@ typedef struct {
} vProcVar_t;


/*
Read a file
params:
char *filename: the file to read
asm_error_t *errData: Error history
returns:
bool: if the file have been read
*/
bool readFile(char *filename, asm_error_t *errData);


/*
Set the speed of the clock based on ticks
params:
Expand All @@ -59,22 +71,23 @@ instruction_t charBinToInst(char *bin);
params:
instruction_t inst: the instruction to redirect to
carry_t *carry: Carry for action on next instruction
FILE *file: File to write the output
char *filename: the filename of the file
asm_error_t *errData: Error history
returns:
bool: if the instructions have been run
*/
bool run(instruction_t inst, carry_t *carry, asm_error_t *errData);
bool run(instruction_t inst, carry_t *carry, FILE *file, char *filename, asm_error_t *errData);

/*
Run interrupt operation
params:
instruction_t inst: the instruction to redirect to
carry_t *carry: Carry for action on next instruction
asm_error_t *errData: Error history
returns:
bool: if the instructions have been run
*/
bool runInt(instruction_t inst, carry_t *carry, asm_error_t *errData);
bool runInt(instruction_t inst, carry_t *carry);

/*
Get a register
Expand All @@ -85,6 +98,17 @@ bool runInt(instruction_t inst, carry_t *carry, asm_error_t *errData);
*/
vRegister_t *getRegister(int reg);

/*
Search label position in a file
params:
int labId: the label to search
char *filename: the filename of the file
asm_error_t *errData: Error history
returns:
fpos_t: the position of the label
*/
fpos_t searchLabel(int labId, char *filename, asm_error_t *errData);

/*
Add a value to a register
params:
Expand Down Expand Up @@ -205,6 +229,22 @@ bool opShl(vRegister_t *reg, unsigned int arg, asm_error_t *errData);
*/
bool opShr(vRegister_t *reg, unsigned int arg, asm_error_t *errData);

/*
Add call position to the call stack
params:
fpos_t pos: the position to add
returns:
bool: if the position have been added
*/
bool addCallPos(fpos_t pos);

/*
Remove first element from the call stack
returns:
bool: if the position have been removed
*/
bool removeCallPos();

/*
print a variable data
params:
Expand Down
31 changes: 6 additions & 25 deletions src/vTerminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "vTerminal.h"

#define LINE_MAX_BITS 16
#define VERSION "1.0.0"

void runVTerminal(asm_error_t *errData){
char *command = (char *)malloc(100 * sizeof(char));
Expand Down Expand Up @@ -39,36 +39,17 @@ bool runCmd(char *command, asm_error_t *errData){
printf("Commands:\n");
printf("exit - to exit the terminal\n");
printf("clear - to clear the terminal\n");
printf("version - to display the version\n");
printf("help - to display the help\n");
}
else if(strcmp(command, "clear") == 0){
system("clear");
}
else if(strcmp(command, "version") == 0){
printf("Version: %s\n", VERSION);
}
else{
//Check if the file is present
FILE *file = fopen(command, "rb");
if(file == NULL){
errorfnf(command, errData);
}
// Run program
char line[LINE_MAX_BITS];

// Set as carry for action on next instruction
carry_t carry = {0, false};

int time = 0;
int lastTime = 0;
int latentTicks = 0;
while(fgets(line, sizeof(line) + 1, file)) {
setClock(time, lastTime, latentTicks);

instruction_t inst = charBinToInst(line);
if(!run(inst, &carry, errData)){
fclose(file);
return false;
}
}
fclose(file);
readFile(command, errData);
}
return true;
}

0 comments on commit 5b744fc

Please sign in to comment.