diff --git a/CoreWar/Instruc b/CoreWar/Instruc new file mode 100755 index 0000000..aaed576 Binary files /dev/null and b/CoreWar/Instruc differ diff --git a/CoreWar/Instruc.c b/CoreWar/Instruc.c new file mode 100644 index 0000000..a8ebedb --- /dev/null +++ b/CoreWar/Instruc.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include + +#include "Instruc.h" + +void printBits(size_t const size, void const * const ptr) +{ + unsigned char *b = (unsigned char*) ptr; + unsigned char byte; + int i, j; + + for (i = size-1; i >= 0; i--) { + for (j = 7; j >= 0; j--) { + byte = (b[i] >> j) & 1; + printf("%u", byte); + } + } + puts(""); +} + +long parseInst(char* instruction, char* argA, char* argB) { + long binaryInstruction; + + char a[100], b[100]; + + strcpy(a, argA); + strcpy(b, argB); + + if (!strcmp(instruction, "MOV")) binaryInstruction = 1; + else if (!strcmp(instruction, "ADD")) binaryInstruction = 2; + else if (!strcmp(instruction, "SUB")) binaryInstruction = 3; + else if (!strcmp(instruction, "JMP")) binaryInstruction = 4; + else if (!strcmp(instruction, "JMZ")) binaryInstruction = 5; + else if (!strcmp(instruction, "JMG")) binaryInstruction = 6; + else if (!strcmp(instruction, "DJZ")) binaryInstruction = 7; + else if (!strcmp(instruction, "CMP")) binaryInstruction = 8; + else if (!strcmp(instruction, "DAT")) binaryInstruction = 0; + binaryInstruction = binaryInstruction << 2; + + if (strlen(a) > 1) { + if (a[0] == '@') { + binaryInstruction += 2; + a[0] = ' '; + } else if (a[0] != '#') { + binaryInstruction += 1; + } else a[0] = ' '; + } + binaryInstruction = binaryInstruction << 2; + + if (strlen(b) > 1 && strcmp(instruction, "DAT")) { + if (b[0] == '@') { + binaryInstruction += 2; + b[0] = ' '; + } else if (b[0] != '#') { + binaryInstruction += 1; + } else b[0] = ' '; + } + binaryInstruction = binaryInstruction << 28; + + int da = atoi(a) % 8000; + int db = atoi(b) % 8000; + + while (da < 0) da += 8000; + while (db < 0) db += 8000; + + if (strlen(a) > 1) binaryInstruction += (da & 268435455); + binaryInstruction = binaryInstruction << 28; + if (strlen(b) > 1) binaryInstruction += (db & 268435455); + + return binaryInstruction; +} + +int convertFile(char* filename, char* destination) { + FILE* sourceFile = fopen(filename, "r"); + FILE* destFile = fopen(destination, "w"); + + char funcName[4], argA[10], argB[10]; + long code; + + fseek(sourceFile, 0L, SEEK_SET); + + if (sourceFile && destFile) { + while (!feof(sourceFile)) { + fscanf(sourceFile, "%s", funcName); + + strcpy(argA, ""); + strcpy(argB, ""); + + if (!strcmp(funcName, "JMP")) { + fscanf(sourceFile, "%s", argA); + } else if (!strcmp(funcName, "DAT")) { + fscanf(sourceFile, "%s", argB); + } else { + fscanf(sourceFile, "%s %s", argA, argB); + } + + code = parseInst(funcName, argA, argB); + fprintf(destFile, "%ld\n", code); + } + } else return 0; + + fclose(sourceFile); + fclose(destFile); +} \ No newline at end of file diff --git a/CoreWar/Instruc.h b/CoreWar/Instruc.h new file mode 100644 index 0000000..110701c --- /dev/null +++ b/CoreWar/Instruc.h @@ -0,0 +1,9 @@ +#ifndef CONVERTER_H +#define CONVERTER_H + +void printBits(size_t const size, void const * const ptr); +long parseInst(char* instruction, char* argA, char* argB); + +int convertFile(char* filename, char* destination); + +#endif \ No newline at end of file diff --git a/CoreWar/Makefile b/CoreWar/Makefile new file mode 100644 index 0000000..7f266ed --- /dev/null +++ b/CoreWar/Makefile @@ -0,0 +1,14 @@ +corewar : Instruc.o core.o + gcc -o corewar.out main.c Instruc.o core.o + ./corewar.out + +Instruc.o : Instruc.c Instruc.h + gcc -c Instruc.c + +core.o : core.c core.h + gcc -c core.c + +clean : + rm -f *.o + rm -f redcode/*.mars + rm -f corewar.out \ No newline at end of file diff --git a/CoreWar/SAE12_2021 b/CoreWar/SAE12_2021 new file mode 160000 index 0000000..7b744c7 --- /dev/null +++ b/CoreWar/SAE12_2021 @@ -0,0 +1 @@ +Subproject commit 7b744c7240255188005f7ce6c7eac66b1b12d7b0 diff --git a/CoreWar/core b/CoreWar/core new file mode 100755 index 0000000..07f5fe0 Binary files /dev/null and b/CoreWar/core differ diff --git a/CoreWar/core.c b/CoreWar/core.c new file mode 100644 index 0000000..f9367f1 --- /dev/null +++ b/CoreWar/core.c @@ -0,0 +1,66 @@ +#include +#include +#include + +#include "core.h" + +int virus1Address, virus2Address; + +int setupViruses(long memory[8000]) { + srand(time(NULL)); + int randAddress = rand() % 8000; + int virusLines = 0; + long instruction; + + virus1Address = randAddress; + + FILE* virusFile = fopen("redcode/virus1.mars", "r"); + + if (virusFile) { + fseek(virusFile, 0L, SEEK_SET); + while (!feof(virusFile)) { + instruction = 0; + fscanf(virusFile, "%ld", &instruction); + memory[(randAddress + virusLines) % 8000] = instruction; + virusLines++; + } + } else { + puts("Could not load Virus 1."); + return 0; + } + + fclose(virusFile); + srand(rand()); + + virusFile = fopen("redcode/virus2.mars", "r"); + + if (virusFile) { + long trash; + while(!feof(virusFile)) { + fscanf(virusFile, "%ld", &trash); + virusLines++; + } + + fseek(virusFile, 0L, SEEK_SET); + + randAddress = (randAddress + rand()) % (8000 - virusLines); + virus2Address = randAddress; + virusLines = 0; + + while (!feof(virusFile)) { + instruction = 0; + fscanf(virusFile, "%ld", &instruction); + memory[(randAddress + virusLines) % 8000] = instruction; + virusLines++; + } + } else { + puts("Could not load Virus 1."); + return 0; + } + + fclose(virusFile); +} + +void execInst(int address, long instruction) { + +} \ No newline at end of file diff --git a/CoreWar/core.h b/CoreWar/core.h new file mode 100644 index 0000000..22f476e --- /dev/null +++ b/CoreWar/core.h @@ -0,0 +1,7 @@ +#ifndef MARS_H +#define MARS_H + +int setupViruses(long memory[8000]); +void execInst(int address, long instruction); + +#endif \ No newline at end of file diff --git a/CoreWar/main.c b/CoreWar/main.c new file mode 100644 index 0000000..545b24c --- /dev/null +++ b/CoreWar/main.c @@ -0,0 +1,20 @@ +#include +#include +#include + +#include "Instruc.h" +#include "core.h" + +int main (int argc, char* argv[]){ + long mainMemory[8000] = {}; + convertFile("redcode/redcode.ass","redcode/virus1.mars"); + convertFile("redcode/redcode.ass","redcode/virus2.mars"); + setupViruses(mainMemory); + + for (int i = 0; i < 8000; ++i) { + if (mainMemory[i] != 0) { + printf("%d -> %ld\n", i, mainMemory[i]); + } + } + +} \ No newline at end of file diff --git a/CoreWar/redcode/redcode.ass b/CoreWar/redcode/redcode.ass new file mode 100644 index 0000000..b3c3b5e --- /dev/null +++ b/CoreWar/redcode/redcode.ass @@ -0,0 +1,4 @@ +DAT -1 +ADD #57 -1 +MOV #0 @-2 +JMP -2 \ No newline at end of file diff --git a/CoreWar/redcode/virus1.mars b/CoreWar/redcode/virus1.mars new file mode 100644 index 0000000..119a107 --- /dev/null +++ b/CoreWar/redcode/virus1.mars @@ -0,0 +1,4 @@ +7999 +2377900618552450879 +1297036692682710846 +4899918541525876736 diff --git a/CoreWar/redcode/virus2.mars b/CoreWar/redcode/virus2.mars new file mode 100644 index 0000000..119a107 --- /dev/null +++ b/CoreWar/redcode/virus2.mars @@ -0,0 +1,4 @@ +7999 +2377900618552450879 +1297036692682710846 +4899918541525876736 diff --git a/Instruc b/Instruc new file mode 100755 index 0000000..aaed576 Binary files /dev/null and b/Instruc differ diff --git a/Instruc.c b/Instruc.c new file mode 100644 index 0000000..a8ebedb --- /dev/null +++ b/Instruc.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include + +#include "Instruc.h" + +void printBits(size_t const size, void const * const ptr) +{ + unsigned char *b = (unsigned char*) ptr; + unsigned char byte; + int i, j; + + for (i = size-1; i >= 0; i--) { + for (j = 7; j >= 0; j--) { + byte = (b[i] >> j) & 1; + printf("%u", byte); + } + } + puts(""); +} + +long parseInst(char* instruction, char* argA, char* argB) { + long binaryInstruction; + + char a[100], b[100]; + + strcpy(a, argA); + strcpy(b, argB); + + if (!strcmp(instruction, "MOV")) binaryInstruction = 1; + else if (!strcmp(instruction, "ADD")) binaryInstruction = 2; + else if (!strcmp(instruction, "SUB")) binaryInstruction = 3; + else if (!strcmp(instruction, "JMP")) binaryInstruction = 4; + else if (!strcmp(instruction, "JMZ")) binaryInstruction = 5; + else if (!strcmp(instruction, "JMG")) binaryInstruction = 6; + else if (!strcmp(instruction, "DJZ")) binaryInstruction = 7; + else if (!strcmp(instruction, "CMP")) binaryInstruction = 8; + else if (!strcmp(instruction, "DAT")) binaryInstruction = 0; + binaryInstruction = binaryInstruction << 2; + + if (strlen(a) > 1) { + if (a[0] == '@') { + binaryInstruction += 2; + a[0] = ' '; + } else if (a[0] != '#') { + binaryInstruction += 1; + } else a[0] = ' '; + } + binaryInstruction = binaryInstruction << 2; + + if (strlen(b) > 1 && strcmp(instruction, "DAT")) { + if (b[0] == '@') { + binaryInstruction += 2; + b[0] = ' '; + } else if (b[0] != '#') { + binaryInstruction += 1; + } else b[0] = ' '; + } + binaryInstruction = binaryInstruction << 28; + + int da = atoi(a) % 8000; + int db = atoi(b) % 8000; + + while (da < 0) da += 8000; + while (db < 0) db += 8000; + + if (strlen(a) > 1) binaryInstruction += (da & 268435455); + binaryInstruction = binaryInstruction << 28; + if (strlen(b) > 1) binaryInstruction += (db & 268435455); + + return binaryInstruction; +} + +int convertFile(char* filename, char* destination) { + FILE* sourceFile = fopen(filename, "r"); + FILE* destFile = fopen(destination, "w"); + + char funcName[4], argA[10], argB[10]; + long code; + + fseek(sourceFile, 0L, SEEK_SET); + + if (sourceFile && destFile) { + while (!feof(sourceFile)) { + fscanf(sourceFile, "%s", funcName); + + strcpy(argA, ""); + strcpy(argB, ""); + + if (!strcmp(funcName, "JMP")) { + fscanf(sourceFile, "%s", argA); + } else if (!strcmp(funcName, "DAT")) { + fscanf(sourceFile, "%s", argB); + } else { + fscanf(sourceFile, "%s %s", argA, argB); + } + + code = parseInst(funcName, argA, argB); + fprintf(destFile, "%ld\n", code); + } + } else return 0; + + fclose(sourceFile); + fclose(destFile); +} \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7f266ed --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +corewar : Instruc.o core.o + gcc -o corewar.out main.c Instruc.o core.o + ./corewar.out + +Instruc.o : Instruc.c Instruc.h + gcc -c Instruc.c + +core.o : core.c core.h + gcc -c core.c + +clean : + rm -f *.o + rm -f redcode/*.mars + rm -f corewar.out \ No newline at end of file diff --git a/core b/core old mode 100644 new mode 100755 diff --git a/corewar.out b/corewar.out old mode 100644 new mode 100755 diff --git a/redcode/redcode.ass b/redcode/redcode.ass new file mode 100644 index 0000000..b3c3b5e --- /dev/null +++ b/redcode/redcode.ass @@ -0,0 +1,4 @@ +DAT -1 +ADD #57 -1 +MOV #0 @-2 +JMP -2 \ No newline at end of file diff --git a/redcode/virus1.mars b/redcode/virus1.mars new file mode 100644 index 0000000..119a107 --- /dev/null +++ b/redcode/virus1.mars @@ -0,0 +1,4 @@ +7999 +2377900618552450879 +1297036692682710846 +4899918541525876736 diff --git a/redcode/virus2.mars b/redcode/virus2.mars new file mode 100644 index 0000000..119a107 --- /dev/null +++ b/redcode/virus2.mars @@ -0,0 +1,4 @@ +7999 +2377900618552450879 +1297036692682710846 +4899918541525876736