From 26be8756237d89f0f829c854fc7a6b5c4e454702 Mon Sep 17 00:00:00 2001 From: baillyj Date: Fri, 17 Dec 2021 15:24:07 +0100 Subject: [PATCH] J'essaye de tout faire fonctionner --- CoreWar/Instruc | Bin 0 -> 16120 bytes CoreWar/Instruc.c | 106 ++++++++++++++++++++++++++++++++++++ CoreWar/Instruc.h | 9 +++ CoreWar/Makefile | 14 +++++ CoreWar/SAE12_2021 | 1 + CoreWar/core | Bin 0 -> 16248 bytes CoreWar/core.c | 66 ++++++++++++++++++++++ CoreWar/core.h | 7 +++ CoreWar/main.c | 20 +++++++ CoreWar/redcode/redcode.ass | 4 ++ CoreWar/redcode/virus1.mars | 4 ++ CoreWar/redcode/virus2.mars | 4 ++ Instruc | Bin 0 -> 16120 bytes Instruc.c | 106 ++++++++++++++++++++++++++++++++++++ Makefile | 14 +++++ core | Bin corewar.out | Bin redcode/redcode.ass | 4 ++ redcode/virus1.mars | 4 ++ redcode/virus2.mars | 4 ++ 20 files changed, 367 insertions(+) create mode 100755 CoreWar/Instruc create mode 100644 CoreWar/Instruc.c create mode 100644 CoreWar/Instruc.h create mode 100644 CoreWar/Makefile create mode 160000 CoreWar/SAE12_2021 create mode 100755 CoreWar/core create mode 100644 CoreWar/core.c create mode 100644 CoreWar/core.h create mode 100644 CoreWar/main.c create mode 100644 CoreWar/redcode/redcode.ass create mode 100644 CoreWar/redcode/virus1.mars create mode 100644 CoreWar/redcode/virus2.mars create mode 100755 Instruc create mode 100644 Instruc.c create mode 100644 Makefile mode change 100644 => 100755 core mode change 100644 => 100755 corewar.out create mode 100644 redcode/redcode.ass create mode 100644 redcode/virus1.mars create mode 100644 redcode/virus2.mars diff --git a/CoreWar/Instruc b/CoreWar/Instruc new file mode 100755 index 0000000000000000000000000000000000000000..aaed576b20c9b7b7e3feb0c13bd09ee6b7fdc1ef GIT binary patch literal 16120 zcmeHOe{3699e;M3CG8p~?b^<3yX98eDH)2J^hZcpL(hqmoQu_A1F)a@7KmT4mN-jXi)O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t09FLk4kCL!mG*bkk*H9uUwezj~zxStIPo&Do{5%b6WYl@}F zxMVd;zs`7G1__Q)y6sEzUEXKu)aN$iRj{qM+4KB*u?0vdtY@{B+srS30LKpZ&+}W} zH#>(X2L;DJ7PjN-_%3j$!uGuXZk8riO=x5*Y|r>U$n5q)CFNEN6>2iD9itbF?e>al zX)0vTaXRfwZ7@b(96!(JTFIXKVSVh&Uh1fSQZI?vp681!V0%0A*kK(+jT|mgVE7!y scRG%Tp9`yj +#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 0000000000000000000000000000000000000000..07f5fe05c5e9b29e0a88823d7d5ae9d2ae35e304 GIT binary patch literal 16248 zcmeHOYitzP6~4Q+!FhOXh@Co+geif@5WLvLxK5DEti5Z-m3b6w(kj4sy}PzI?8Die zZEaLiR5yqfaf-^1^pQ&CN7YtJrB>|^r2ME65j0VL7%C-hqY5`lT9Z<05lzk9!XzH{%Kxih}Q-22r~PnXx@5uD1zX9bo#!@cjj~$IwijbRwKq)2IxM1Yo^g>dRdN$12M*)w7rnD)GHME{-kc9Cv_BHailwpg@aqiwB?tu6j++P_IQjQwhY z1IN_19eu)kL`)*bdXMlFmAzC36PeJq77h$M>W{7b!t_k~28Y2&DPf4dmY3j>E5)}i!f#xJ|2psr9M1Fr5T*Q|1YU6( z6SukqFLXU?gd+#_$nZgZFdR>akxV>g42rCgG13R2Ix-s82ji)5B7P`_Y$Q1XEL`pg zDl>+j498Of%Xqb0#kQXAj!u1(f0KW6A>ShO?%wTsG?s}C#j{2%)4RPhkxs>W!vl#J zcpFNlQ{+vzOiLQd{(E5z;6H0D&}|6U)GIXLcpT?e#P33j-Ne<#<5dD|4oDyHoT29g z-`D-itcp1YUqigATJ>zt<>{>!kM|mm1x_-?QgM+Q^m_u zvV0NCIEA}BDa&tQ8K-WSe+p%6uldj2`3ncM{JYxtdvm*bdnPLOg1t6TbqU;P6YUqk z*sSXS-SrbtJOvqTqT)LUPs|&2(A9@A4_&>>oT*-Q1kA-*La={U+HZaaVcl&lKc}6! zx>Y+fU#5A^X%}uAwcy}}-*QlC&J0$s3NGlee@EIIK`U~P_i5wp7r|0!`S*<~?RdKn zc(ZO2sM!dd%(;qkAUyle4*IKC;hPQk8C=L?ywKm}A+YTO9}s9YfA~!;pPP(o$II6? zNY|nH=1J{zAQ)60*{n2b`OwwD^P!u~0)DS3aOQniw29DFE&rC5KR^4*ZPNsk8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j;{3x!uCro#Ai~n<_ZIxZ9%S7K-Bi97PU< zKRcE*!UKS?_e0_^=U_`nEHfhfa^wxAa{iG_dL))H#%$C;E)Ls&;!&ak9o>yMHvMQm z9L^34e{?JbO;~7TteP*yGTC@KRaDTSE)z?HQ9!wogdzMg7Jef(3V9rpP?wgM(H|S8 zQ8*k0Etf2XH5@Gk=fQC}8IM4#Y48O$nvXV(jr zjGHWMqap+A7`-62>v=xgMtbh2Q(q{97=3a7c^;^dp4(x2Y|EaisC_aoRI%PUS7*H) zdF&O$km)Cf3Jo}X4&%Ec_lKXK_&(2lhioAop@I9f!#TIhhB!?wrGE$iFhRQCA#fDM FKLGgM&tw1q literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..aaed576b20c9b7b7e3feb0c13bd09ee6b7fdc1ef GIT binary patch literal 16120 zcmeHOe{3699e;M3CG8p~?b^<3yX98eDH)2J^hZcpL(hqmoQu_A1F)a@7KmT4mN-jXi)OQ@m(6h^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L!mG*bkk*H9uUwezj~zxStIPo&Do{5%b6WYl@}F zxMVd;zs`7G1__Q)y6sEzUEXKu)aN$iRj{qM+4KB*u?0vdtY@{B+srS30LKpZ&+}W} zH#>(X2L;DJ7PjN-_%3j$!uGuXZk8riO=x5*Y|r>U$n5q)CFNEN6>2iD9itbF?e>al zX)0vTaXRfwZ7@b(96!(JTFIXKVSVh&Uh1fSQZI?vp681!V0%0A*kK(+jT|mgVE7!y scRG%Tp9`yj +#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