diff --git a/main.c b/main.c deleted file mode 100644 index e98958f..0000000 --- a/main.c +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int main() { - return 0; -} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..e7cb514 --- /dev/null +++ b/src/main.c @@ -0,0 +1,37 @@ +#include +#include +#include + +#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; +} diff --git a/src/test/testFormats.c b/src/test/testFormats.c new file mode 100644 index 0000000..3c194c5 --- /dev/null +++ b/src/test/testFormats.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#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()); +} diff --git a/src/util/formats.h b/src/util/formats.h new file mode 100644 index 0000000..f3defd3 --- /dev/null +++ b/src/util/formats.h @@ -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; diff --git a/src/util/functions.h b/src/util/functions.h new file mode 100644 index 0000000..15de0f4 --- /dev/null +++ b/src/util/functions.h @@ -0,0 +1,42 @@ +#include +#include +#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); +} diff --git a/src/util/registers.h b/src/util/registers.h new file mode 100644 index 0000000..01b9d41 --- /dev/null +++ b/src/util/registers.h @@ -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) +};