From 5a0c4f70f12f5e1a75b2f22255f43281dfacb178 Mon Sep 17 00:00:00 2001 From: nova Date: Mon, 11 Mar 2024 23:09:47 +0100 Subject: [PATCH] cleanup, opcode widt change to 4 --- main.c | 86 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 25 deletions(-) diff --git a/main.c b/main.c index 027d0be..5df0ac3 100644 --- a/main.c +++ b/main.c @@ -1,19 +1,39 @@ #include -#include #include "asm_rules.c" -//void trit_to_hex(char *_asm, char **_out){ -void trit_to_hex(char *_asm, char _out[27]){ +void bin_to_hex(char *tryte, char *hex) { + 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 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++) { - tryte[i] = 0; + tryte[i] = 0; } //convert assembly to formated binary representation of ternary for (int i = 0; i < 27; i++) { - printf("%c",_asm[i]); + //printf("%c",_asm[i]); if(_asm[i]==2) { strcat(tryte, "10"); } else if (_asm[i]==1) { @@ -22,45 +42,61 @@ void trit_to_hex(char *_asm, char _out[27]){ strcat(tryte, "00"); } } - - //todo: convert tryte to hex - //*_out = (char*)calloc(8,sizeof(int)); - char *a = tryte; - int num = 0; - do { - int b = *a=='1'?1:0; - num = (num<<1)|b; - a++; - } while (*a); + int j = 0; + for (int i = 0; i < 18; i++) { + t0[i] = tryte[j]; + j++; + } + for (int i = 0; i < 18; i++) { + t1[i] = tryte[j]; + j++; + } + for (int i = 0; i < 18; i++) { + t2[i] = tryte[j]; + j++; + } + for (int i = 0; i < 18; i++) { + t3[i] = tryte[j]; + j++; + } - //sprintf(_out, "%x", num); + bin_to_hex(t0, &opcode); + bin_to_hex(t1, &in0); + bin_to_hex(t2, &in1); + bin_to_hex(t3, &out); } - - int main() { FILE *dest_asm = fopen("assembly.txt", "w"); FILE *src_asm = fopen("src.asm", "r"); unsigned int size=0; fprintf(dest_asm,"00000\n"); - char in_asm[54]; + char in_asm[64]; char out_asm[27]; - fgets(in_asm, 54, src_asm); - char *_out; + char hex_op[5]; + char hex_in0[5]; + char hex_in1[5]; + char hex_out[5]; + + fgets(in_asm, 64, src_asm); + while(!feof(src_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 - fgets(in_asm, 54, src_asm); - free(_out); + fgets(in_asm, 64, src_asm); size++; }