This commit is contained in:
baillyj 2021-12-17 15:16:52 +01:00
parent 6dbc75c640
commit 7b744c7240
2 changed files with 73 additions and 0 deletions

66
core.c Normal file
View 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
core.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef MARS_H
#define MARS_H
int setupViruses(long memory[8000]);
void execInst(int address, long instruction);
#endif