reorganized project and implemented basic tests for toBinary
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "util/registers.h"
|
||||||
|
#include "util/functions.h"
|
||||||
|
|
||||||
|
int help() {
|
||||||
|
printf(
|
||||||
|
"\nA CLI-based tool to convert MIPS assembly instructions in a .asm file into hexadecimal instructions in a .hex file.\n"
|
||||||
|
"\nUsage:\n"
|
||||||
|
" mipsToHex -h\n"
|
||||||
|
" mipsToHex --help\n"
|
||||||
|
" mipsToHex [ file ]\n"
|
||||||
|
" mipsToHex --asm [ asmFile ]\n"
|
||||||
|
" mipsToHex --asm [ asmFile ] --hex [ hexFile ]\n"
|
||||||
|
);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int error() {
|
||||||
|
printf("There must be at least one argument.");
|
||||||
|
help();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* args[]) {
|
||||||
|
if(argc < 2) return error();
|
||||||
|
if(strcmp(args[1], "-h") == 0 || strcmp(args[1], "--help") == 0) return help();
|
||||||
|
|
||||||
|
initFunctions();
|
||||||
|
|
||||||
|
for(int i = 0; i < 32; i++)
|
||||||
|
printf("%d: %s\n", i, REGISTER_STRING[i]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "../util/formats.h"
|
||||||
|
|
||||||
|
int test_binaryToHex() {
|
||||||
|
return 1; // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_toBinary_0_size5() {
|
||||||
|
char * binaryString = toBinary(0, 5);
|
||||||
|
char * expectedOutput = "00000";
|
||||||
|
|
||||||
|
int cmp = strcmp(binaryString, expectedOutput);
|
||||||
|
|
||||||
|
if(cmp < 0 || cmp > 0) return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_toBinary_0_size6() {
|
||||||
|
char * binaryString = toBinary(0, 6);
|
||||||
|
char * expectedOutput = "000000";
|
||||||
|
|
||||||
|
int cmp = strcmp(binaryString, expectedOutput);
|
||||||
|
|
||||||
|
if(cmp < 0 || cmp > 0) return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_toBinary_20_size6() {
|
||||||
|
char * binaryString = toBinary(20, 6);
|
||||||
|
char * expectedOutput = "010100";
|
||||||
|
|
||||||
|
int cmp = strcmp(binaryString, expectedOutput);
|
||||||
|
|
||||||
|
if(cmp < 0 || cmp > 0) return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_toBinary_10_size6() {
|
||||||
|
char * binaryString = toBinary(10, 6);
|
||||||
|
char * expectedOutput = "001010";
|
||||||
|
|
||||||
|
int cmp = strcmp(binaryString, expectedOutput);
|
||||||
|
|
||||||
|
if(cmp < 0 || cmp > 0) return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_toBinary_17_size16() {
|
||||||
|
char * binaryString = toBinary(17, 16);
|
||||||
|
char * expectedOutput = "0000000000010001";
|
||||||
|
|
||||||
|
int cmp = strcmp(binaryString, expectedOutput);
|
||||||
|
|
||||||
|
if(cmp < 0 || cmp > 0) return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_rToBinary() {
|
||||||
|
r* rFormat = malloc(sizeof(r));
|
||||||
|
return 1; // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_iToHex() {
|
||||||
|
i* iFormat = malloc(sizeof(i));
|
||||||
|
return 1; // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("\nTesting formats.h functionality.\n");
|
||||||
|
|
||||||
|
printf("\ntoBinary_0_size5: %d\n", test_toBinary_0_size5());
|
||||||
|
printf("\ntoBinary_0_size6: %d\n", test_toBinary_0_size6());
|
||||||
|
printf("\ntoBinary_10_size6: %d\n", test_toBinary_10_size6());
|
||||||
|
printf("\ntoBinary_20_size6: %d\n", test_toBinary_20_size6());
|
||||||
|
printf("\ntoBinary_17_size16: %d\n", test_toBinary_17_size16());
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
// Utility function to convert any given binary instruction into hexadecimal.
|
||||||
|
char* binaryToHex(char* bin) {
|
||||||
|
return "0x00000000";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Utility function to convert a number to binary
|
||||||
|
char* toBinary(int num, int size) {
|
||||||
|
char bin[size];
|
||||||
|
|
||||||
|
return "000000"; // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
// Represents the R instruction format.
|
||||||
|
typedef struct r {
|
||||||
|
int opcode; // 6 bits
|
||||||
|
int opcode_size;
|
||||||
|
int rs; // 5 bits
|
||||||
|
int rs_size;
|
||||||
|
int rt; // 5 bits
|
||||||
|
int rt_size;
|
||||||
|
int rd; // 5 bits
|
||||||
|
int rd_size;
|
||||||
|
int shamt; // 5 bits
|
||||||
|
int shamt_size;
|
||||||
|
char* functHex; // 6 bits
|
||||||
|
int functHex_size;
|
||||||
|
} r;
|
||||||
|
|
||||||
|
char* rToBinary(r* format) {
|
||||||
|
char binary[32];
|
||||||
|
|
||||||
|
char* opcode = toBinary(format->opcode, format->opcode_size);
|
||||||
|
if(format->opcode == 0)
|
||||||
|
for(int i = 0; i < 6; i++) binary[i] = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return binary;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Represents the I instruction format.
|
||||||
|
typedef struct i {
|
||||||
|
int opcode; // 6 bits
|
||||||
|
int rs; // 4 bits
|
||||||
|
int rt; // 4 bits
|
||||||
|
int immediate; // 16 bits
|
||||||
|
} i;
|
||||||
|
|
||||||
|
// Utility function to convert any given I instruction into hexadecimal.
|
||||||
|
char* iToHex(i* format) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Represents the J instruction format.
|
||||||
|
typedef struct j {
|
||||||
|
int opcode; // 6 bits
|
||||||
|
int address; // 26 bits
|
||||||
|
} j;
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "formats.h"
|
||||||
|
|
||||||
|
r* rFormat;
|
||||||
|
i* iFormat;
|
||||||
|
|
||||||
|
void initFunctions() {
|
||||||
|
rFormat = (r*)malloc(sizeof(r));
|
||||||
|
iFormat = (i*)malloc(sizeof(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
char* add(int * args[]) {
|
||||||
|
rFormat->opcode = 0;
|
||||||
|
rFormat->rd = *args[0];
|
||||||
|
rFormat->rs = *args[1];
|
||||||
|
rFormat->rt = *args[2];
|
||||||
|
rFormat->shamt = *args[4];
|
||||||
|
rFormat->functHex = "0x20";
|
||||||
|
|
||||||
|
return rToBinary(rFormat);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* addi(int * args[]) {
|
||||||
|
rFormat->opcode = 0;
|
||||||
|
rFormat->rs = *args[0];
|
||||||
|
rFormat->rt = *args[1];
|
||||||
|
rFormat->functHex = "0x8";
|
||||||
|
|
||||||
|
return rToBinary(rFormat);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* sub(int * args[]) {
|
||||||
|
rFormat->opcode = 0;
|
||||||
|
rFormat->rd = *args[0];
|
||||||
|
rFormat->rs = *args[1];
|
||||||
|
rFormat->rt = *args[2];
|
||||||
|
rFormat->shamt = *args[4];
|
||||||
|
rFormat->functHex = "0x22";
|
||||||
|
|
||||||
|
return rToBinary(rFormat);
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#define FOREACH_REGISTER(REGISTER) \
|
||||||
|
REGISTER(zero) \
|
||||||
|
REGISTER(at) \
|
||||||
|
REGISTER(v0) \
|
||||||
|
REGISTER(v1) \
|
||||||
|
REGISTER(a0) \
|
||||||
|
REGISTER(a1) \
|
||||||
|
REGISTER(a2) \
|
||||||
|
REGISTER(a3) \
|
||||||
|
REGISTER(t0) \
|
||||||
|
REGISTER(t1) \
|
||||||
|
REGISTER(t2) \
|
||||||
|
REGISTER(t3) \
|
||||||
|
REGISTER(t4) \
|
||||||
|
REGISTER(t5) \
|
||||||
|
REGISTER(t6) \
|
||||||
|
REGISTER(t7) \
|
||||||
|
REGISTER(s0) \
|
||||||
|
REGISTER(s1) \
|
||||||
|
REGISTER(s2) \
|
||||||
|
REGISTER(s3) \
|
||||||
|
REGISTER(s4) \
|
||||||
|
REGISTER(s5) \
|
||||||
|
REGISTER(s6) \
|
||||||
|
REGISTER(s7) \
|
||||||
|
REGISTER(t8) \
|
||||||
|
REGISTER(t9) \
|
||||||
|
REGISTER(k0) \
|
||||||
|
REGISTER(k1) \
|
||||||
|
REGISTER(gp) \
|
||||||
|
REGISTER(sp) \
|
||||||
|
REGISTER(fp) \
|
||||||
|
REGISTER(ra) \
|
||||||
|
|
||||||
|
#define GENERATE_ENUM(ENUM) ENUM,
|
||||||
|
#define GENERATE_STRING(STRING) #STRING,
|
||||||
|
|
||||||
|
enum Register {
|
||||||
|
FOREACH_REGISTER(GENERATE_ENUM)
|
||||||
|
};
|
||||||
|
|
||||||
|
static char* REGISTER_STRING[] = {
|
||||||
|
FOREACH_REGISTER(GENERATE_STRING)
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user