-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
43 lines (36 loc) · 1.05 KB
/
input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "input.h"
/**
* Opens the file and reads it into the memory 32 bits at a time
* @param fileName Name of the binary file
* @param memory Array which stores the instructions
*/
void readBinaryFile(char *fileName, uint8_t *memory) {
FILE *fp;
fp = fopen(fileName, "rb");
if (fp == NULL) {
perror("readBinaryFile");
exit(EXIT_FAILURE);
}
fseek(fp, 0, SEEK_END);
int fileLength = (int) ftell(fp);
rewind(fp);
size_t numElems = (size_t) fileLength / INSTR_SIZE;
fread(memory, INSTR_SIZE, numElems, fp);
fclose(fp);
}
/**
* Opens the file and writes the 32 bit instruction to the file
* @param fileName Name of the file to be written to
* @param instructions The 32 bit instruction
* @param numInstr The number of instructions to be written to the file
*/
void writeBinaryFile(char *fileName, uint32_t *instructions, int numInstr) {
FILE *fp;
fp = fopen(fileName, "w");
if (fp == NULL) {
perror("writeBinaryFile");
exit(EXIT_FAILURE);
}
fwrite(instructions, sizeof(uint32_t), (size_t) numInstr, fp);
fclose(fp);
}