various bugfixes, refractoring

This commit is contained in:
nova 2024-04-30 23:31:16 +02:00
parent bfd7fd3016
commit 763c447cf5
2 changed files with 99 additions and 69 deletions

View File

@ -7,8 +7,11 @@ void trit_flip(char *in, char *instruction) {
in[i] = instruction[i];
}
}
int asm_rules(char *in, char _asm[27], char _asm_addr[27]){
int asm_rules(char *in, char _asm[27]){
for (int i = 0; i < 27; i++) {
_asm[i] = 0;
}
int pass = 0;
char *word = strtok(in, " ");
@ -60,7 +63,14 @@ int asm_rules(char *in, char _asm[27], char _asm_addr[27]){
trit_flip(_asm, "+00000-000000");
} else if (strstr(word, "mov")) {
trit_flip(_asm, "+000000000000");
} else if (strstr(word, "Nst")) { //underflow test
trit_flip(_asm, "-------------");
} else if (strstr(word, "Pst")) { //overflow test
trit_flip(_asm, "+++++++++++++");
}
}
word = strtok(NULL, " ");
@ -68,3 +78,4 @@ int asm_rules(char *in, char _asm[27], char _asm_addr[27]){
pass = 0;
return 0;
}

153
main.c
View File

@ -1,72 +1,15 @@
#include <stdio.h>
#include "asm_rules.c"
void bin_to_hex(char *tryte, int *hex) {
char *a = tryte;
long num = 0;
do {
long b = *a=='1'?1:0;
num = (num<<1)|b;
a++;
} while (*a);
*hex = num;
}
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){
char tryte[108] = "\0";
char t0[26] = "\0";
char t1[26] = "\0";
char t2[26] = "\0";
char t3[26] = "\0";
int size = sizeof(_asm)/sizeof(_asm[0]);
//convert assembly to formated binary representation of ternary
for (int i = 0; i < sizeof(_asm)/4; i++) {
//printf("%c",_asm[i]);
if(_asm[i]=='-') {
strcat(tryte, "10");
} else if (_asm[i]=='+') {
strcat(tryte, "01");
} else {
strcat(tryte, "00");
}
}
bin_to_hex(t0, opcode);
bin_to_hex(t1, in0);
bin_to_hex(t2, in1);
bin_to_hex(t3, out);
}
int main(int argc, char **argv) {
void bin_to_hex(char *tryte, int *hex);
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out);
int params(int argc, char **argv);
FILE *dest_asm;
FILE *src_asm;
if (argc > 2) {
for (int i = 1 ; i < argc; i++) {
if (strcmp("-i", argv[i]) == 0 || strcmp("--input", argv[i]) == 0) {
src_asm = fopen(argv[i+1], "r");
i=i+1;
} else if (strcmp("-o", argv[i]) == 0 || strcmp("--output", argv[i]) == 0) {
dest_asm = fopen(argv[i+1], "w");
i=i+1;
} else if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
printf("Usage: tasm [options] file...\n");
printf("Options:\n");
printf("-h,--help Prints this information\n");
printf("-i,--input <arg> Path of input file\n");
printf("-o,--output <arg> Path of output file\n");
return 0;
} else {
printf("unknown argument: %s\n", argv[i]);
return 1;
}
}
} else {
printf("tasm: no input file\n");
int main(int argc, char **argv) {
int p = params(argc, argv);
if (p==1) {
return 1;
}
@ -94,16 +37,13 @@ int main(int argc, char **argv) {
asm_to_hex(out_asm, &hex_op, &hex_in0, &hex_in1, &hex_out);
printf("%.5x %.5x %.5x %.5x\n", hex_op, hex_in0, hex_in1, hex_out);
// Write the hex to a file
fprintf(dest_asm,"%.5x ", hex_op);
fprintf(dest_asm,"%.5x ", hex_in0);
fprintf(dest_asm,"%.5x ", hex_in1);
fprintf(dest_asm,"%.5x \n", hex_out);
// Write the hex to a file
size = size + 3; //1 word = 3trytes
}
}
printf("\n%u trytes\n\n", size);
@ -116,3 +56,82 @@ int main(int argc, char **argv) {
return 0;
}
int params(int argc, char **argv){
if (argc > 2) {
for (int i = 1 ; i < argc; i++) {
if (strcmp("-i", argv[i]) == 0 || strcmp("--input", argv[i]) == 0) {
src_asm = fopen(argv[i+1], "r");
i=i+1;
} else if (strcmp("-o", argv[i]) == 0 || strcmp("--output", argv[i]) == 0) {
dest_asm = fopen(argv[i+1], "w");
i=i+1;
} else if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
printf("Usage: tasm [options] file...\n");
printf("Options:\n");
printf("-h,--help Prints this information\n");
printf("-i,--input <arg> Path of input file\n");
printf("-o,--output <arg> Path of output file\n");
return 1;
} else {
printf("unknown argument: %s\n", argv[i]);
return 1;
}
}
} else {
printf("tasm: no input file\n");
return 1;
}
return 0;
}
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){
char tryte[54];
char t0[27];
char t1[26];
char t2[26];
char t3[26];
for (int i = 0; i < 18; i++) {
t0[i] = 0;
t1[i] = 0;
t2[i] = 0;
t3[i] = 0;
}
tryte[0] = '\0';
//convert assembly to formated binary representation of ternary
int i = 0;
do {
if(_asm[0]=='-') {
strcat(tryte, "10");
} else if (_asm[0]=='+') {
strcat(tryte, "01");
} else {
strcat(tryte, "00");
}
i++;
} while (i<13);
int j = 0;
for (int i = 0; i < 27; i++) {
t0[i] = tryte[j];
j++;
}
j++;
bin_to_hex(t0, opcode);
bin_to_hex(t1, in0);
bin_to_hex(t2, in1);
bin_to_hex(t3, out);
}
void bin_to_hex(char *tryte, int *hex) {
for (int i = 0; i < 26; ++i){
if (tryte[i] == '1' ){
*hex = (*hex << 1)|1;
} else {
*hex = (*hex << 1)|0;
}
}
}