cleanup, opcode widt change to 4

This commit is contained in:
nova 2024-03-11 23:09:47 +01:00
parent 997ca65f08
commit 5a0c4f70f1

84
main.c
View File

@ -1,11 +1,31 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "asm_rules.c" #include "asm_rules.c"
//void trit_to_hex(char *_asm, char **_out){ void bin_to_hex(char *tryte, char *hex) {
void trit_to_hex(char *_asm, char _out[27]){ char *a = tryte;
int num = 0;
do {
int b = *a=='1'?1:0;
num = (num<<1)|b;
a++;
} while (*a);
sprintf(hex, "%x", num);
printf("%d\n", num);
}
void trit_to_bin(char *_asm, char opcode, char in0, char in1, char out){
char tryte[54]; char tryte[54];
char t0[18];
char t1[18];
char t2[18];
char t3[18];
for (int i = 0; i < 18; i++) {
t0[i] = 0;
t1[i] = 0;
t2[i] = 0;
t3[i] = 0;
}
for (int i = 0; i < 54; i++) { for (int i = 0; i < 54; i++) {
tryte[i] = 0; tryte[i] = 0;
} }
@ -13,7 +33,7 @@ void trit_to_hex(char *_asm, char _out[27]){
//convert assembly to formated binary representation of ternary //convert assembly to formated binary representation of ternary
for (int i = 0; i < 27; i++) { for (int i = 0; i < 27; i++) {
printf("%c",_asm[i]); //printf("%c",_asm[i]);
if(_asm[i]==2) { if(_asm[i]==2) {
strcat(tryte, "10"); strcat(tryte, "10");
} else if (_asm[i]==1) { } else if (_asm[i]==1) {
@ -22,21 +42,29 @@ void trit_to_hex(char *_asm, char _out[27]){
strcat(tryte, "00"); strcat(tryte, "00");
} }
} }
int j = 0;
//todo: convert tryte to hex for (int i = 0; i < 18; i++) {
//*_out = (char*)calloc(8,sizeof(int)); t0[i] = tryte[j];
char *a = tryte; j++;
int num = 0; }
do { for (int i = 0; i < 18; i++) {
int b = *a=='1'?1:0; t1[i] = tryte[j];
num = (num<<1)|b; j++;
a++; }
} while (*a); for (int i = 0; i < 18; i++) {
t2[i] = tryte[j];
//sprintf(_out, "%x", num); j++;
}
for (int i = 0; i < 18; i++) {
t3[i] = tryte[j];
j++;
} }
bin_to_hex(t0, &opcode);
bin_to_hex(t1, &in0);
bin_to_hex(t2, &in1);
bin_to_hex(t3, &out);
}
int main() { int main() {
@ -45,22 +73,30 @@ int main() {
unsigned int size=0; unsigned int size=0;
fprintf(dest_asm,"00000\n"); fprintf(dest_asm,"00000\n");
char in_asm[54]; char in_asm[64];
char out_asm[27]; char out_asm[27];
fgets(in_asm, 54, src_asm); char hex_op[5];
char *_out; char hex_in0[5];
char hex_in1[5];
char hex_out[5];
fgets(in_asm, 64, src_asm);
while(!feof(src_asm)) { while(!feof(src_asm)) {
asm_rules(in_asm, out_asm); asm_rules(in_asm, out_asm);
trit_to_hex(out_asm, _out); trit_to_bin(out_asm, *hex_op, *hex_in0, *hex_in1, *hex_out);
fprintf(dest_asm,"%s ", _out);
fprintf(dest_asm,"%.5s ", hex_op);
fprintf(dest_asm,"%.5s ", hex_in0);
fprintf(dest_asm,"%.5s ", hex_in1);
fprintf(dest_asm,"%.5s \n", hex_out);
// Write the hex to a file // Write the hex to a file
fgets(in_asm, 54, src_asm); fgets(in_asm, 64, src_asm);
free(_out);
size++; size++;
} }