J'essaye de tout faire fonctionner
This commit is contained in:
parent
7b744c7240
commit
26be875623
BIN
CoreWar/Instruc
Executable file
BIN
CoreWar/Instruc
Executable file
Binary file not shown.
106
CoreWar/Instruc.c
Normal file
106
CoreWar/Instruc.c
Normal file
@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
9
CoreWar/Instruc.h
Normal file
9
CoreWar/Instruc.h
Normal file
@ -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
|
14
CoreWar/Makefile
Normal file
14
CoreWar/Makefile
Normal file
@ -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
|
1
CoreWar/SAE12_2021
Submodule
1
CoreWar/SAE12_2021
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 7b744c7240255188005f7ce6c7eac66b1b12d7b0
|
BIN
CoreWar/core
Executable file
BIN
CoreWar/core
Executable file
Binary file not shown.
66
CoreWar/core.c
Normal file
66
CoreWar/core.c
Normal file
@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#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) {
|
||||
|
||||
}
|
7
CoreWar/core.h
Normal file
7
CoreWar/core.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef MARS_H
|
||||
#define MARS_H
|
||||
|
||||
int setupViruses(long memory[8000]);
|
||||
void execInst(int address, long instruction);
|
||||
|
||||
#endif
|
20
CoreWar/main.c
Normal file
20
CoreWar/main.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
4
CoreWar/redcode/redcode.ass
Normal file
4
CoreWar/redcode/redcode.ass
Normal file
@ -0,0 +1,4 @@
|
||||
DAT -1
|
||||
ADD #57 -1
|
||||
MOV #0 @-2
|
||||
JMP -2
|
4
CoreWar/redcode/virus1.mars
Normal file
4
CoreWar/redcode/virus1.mars
Normal file
@ -0,0 +1,4 @@
|
||||
7999
|
||||
2377900618552450879
|
||||
1297036692682710846
|
||||
4899918541525876736
|
4
CoreWar/redcode/virus2.mars
Normal file
4
CoreWar/redcode/virus2.mars
Normal file
@ -0,0 +1,4 @@
|
||||
7999
|
||||
2377900618552450879
|
||||
1297036692682710846
|
||||
4899918541525876736
|
106
Instruc.c
Normal file
106
Instruc.c
Normal file
@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
14
Makefile
Normal file
14
Makefile
Normal file
@ -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
|
0
corewar.out
Normal file → Executable file
0
corewar.out
Normal file → Executable file
4
redcode/redcode.ass
Normal file
4
redcode/redcode.ass
Normal file
@ -0,0 +1,4 @@
|
||||
DAT -1
|
||||
ADD #57 -1
|
||||
MOV #0 @-2
|
||||
JMP -2
|
4
redcode/virus1.mars
Normal file
4
redcode/virus1.mars
Normal file
@ -0,0 +1,4 @@
|
||||
7999
|
||||
2377900618552450879
|
||||
1297036692682710846
|
||||
4899918541525876736
|
4
redcode/virus2.mars
Normal file
4
redcode/virus2.mars
Normal file
@ -0,0 +1,4 @@
|
||||
7999
|
||||
2377900618552450879
|
||||
1297036692682710846
|
||||
4899918541525876736
|
Loading…
Reference in New Issue
Block a user