SAE12_2021/CoreWar/core.c

66 lines
1.5 KiB
C
Raw Normal View History

2021-12-17 15:24:07 +01:00
#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) {
}