Compare commits

..

No commits in common. "bfd7fd3016e70170f8b5a6785c2f28d1e0e64340" and "fb59048075954244deb6c31236e926c970c8bbb0" have entirely different histories.

2 changed files with 103 additions and 45 deletions

View File

@ -1,70 +1,101 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
void trit_flip(char *in, char *instruction) { void asm_rules(char *in, char _asm[27]){
for (int i = 0; i<(sizeof(in)/sizeof(in[0])); i++) {
in[i] = instruction[i]; for (int i=0; i<27; i++) {
_asm[i] = 0;
} }
}
int asm_rules(char *in, char _asm[27], char _asm_addr[27]){
int pass = 0;
char *word = strtok(in, " "); char *word = strtok(in, " ");
while(word != NULL) { while(word != NULL) {
//instruction set
//aaaaaa bbbbbbb ccccccc ddddddd
//a = opcode, b = input 0, c = input 1, d = output
if (strcmp(word, "nop") == 0) { if (strcmp(word, "nop") == 0) {
//keeps the default value (0) if nop //keeps the default value (0) if nop
} else if (word[0] == ';') { } else if (word[0] == ';') {
//comments //comments
break; break;
} else { } else {
//cases make larger checks harder to read
if(word[0] == 'i') { if(word[0] == 'i') {
//integer //integer
_asm[7] = '0'; _asm[1] = 1;
} else if(word[0] == 'f') { } else if(word[0] == 'f') {
//floating //floating
_asm[7] = '-'; _asm[1] = 2;
} }
if (word[0] == '$' || word[0] == '@') { if (word[0] == '$') {
//constants
_asm[0] = 2; //set constant loading (on input 1)
//cases make larger checks harder to read } else if (word[0] == '@') {
//address location
//beware: beginning yandev style
} else if (strstr(word, "max")) { } else if (strstr(word, "max")) {
trit_flip(_asm, "+00000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 0;
_asm[5] = 1;
} else if (strstr(word, "min")) { } else if (strstr(word, "min")) {
trit_flip(_asm, "-00000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 0;
_asm[5] = 2;
} else if (strstr(word, "any")) { } else if (strstr(word, "any")) {
trit_flip(_asm, "000000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 2;
_asm[5] = 2;
} else if (strstr(word, "cons")) { } else if (strstr(word, "cons")) {
trit_flip(_asm, "-+0000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 1;
_asm[5] = 1;
} else if (strstr(word, "add")) { } else if (strstr(word, "add")) {
trit_flip(_asm, "+-+000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 0;
_asm[5] = 1;
} else if (strstr(word, "sub")) { } else if (strstr(word, "sub")) {
trit_flip(_asm, "+--000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 0;
_asm[5] = 2;
} else if (strstr(word, "mul")) { } else if (strstr(word, "mul")) {
trit_flip(_asm, "+0+000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 1;
_asm[5] = 0;
} else if (strstr(word, "div")) { } else if (strstr(word, "div")) {
trit_flip(_asm, "+0-000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 2;
_asm[5] = 0;
} else if (strstr(word, "dec")) { } else if (strstr(word, "dec")) {
trit_flip(_asm, "--0000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 2;
_asm[4] = 2;
_asm[5] = 0;
} else if (strstr(word, "inc")) { } else if (strstr(word, "inc")) {
trit_flip(_asm, "-+0000+000000"); _asm[2] = 2; //set layer to arithmetics and logic
} else if (strstr(word, "iou")) { _asm[3] = 2;
trit_flip(_asm, "--0000-000000"); _asm[4] = 2;
} else if (strstr(word, "ieq")) { _asm[5] = 1;
trit_flip(_asm, "+-0000-000000"); } else if (strstr(word, "not")) {
} else if (strstr(word, "jmp")) { _asm[2] = 2; //set layer to arithmetics and logic
trit_flip(_asm, "000000-000000"); _asm[3] = 2;
} else if (strstr(word, "jec")) { _asm[4] = 2;
trit_flip(_asm, "+00000-000000"); _asm[5] = 2;
} else if (strstr(word, "mov")) {
trit_flip(_asm, "+000000000000");
} }
} }
word = strtok(NULL, " "); word = strtok(NULL, " ");
} }
pass = 0;
return 0;
} }

49
main.c
View File

@ -3,36 +3,62 @@
void bin_to_hex(char *tryte, int *hex) { void bin_to_hex(char *tryte, int *hex) {
char *a = tryte; char *a = tryte;
long num = 0; int num = 0;
do { do {
long b = *a=='1'?1:0; int b = *a=='1'?1:0;
num = (num<<1)|b; num = (num<<1)|b;
a++; a++;
} while (*a); } while (*a);
*hex = num; *hex = num;
//printf("%d\n",num);
} }
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){ void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){
char tryte[108] = "\0"; char tryte[54];
char t0[26] = "\0"; char t0[18];
char t1[26] = "\0"; char t1[18];
char t2[26] = "\0"; char t2[18];
char t3[26] = "\0"; 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;
}
int size = sizeof(_asm)/sizeof(_asm[0]);
//convert assembly to formated binary representation of ternary //convert assembly to formated binary representation of ternary
for (int i = 0; i < sizeof(_asm)/4; i++) { for (int i = 0; i < 27; i++) {
//printf("%c",_asm[i]); //printf("%c",_asm[i]);
if(_asm[i]=='-') { if(_asm[i]==2) {
strcat(tryte, "10"); strcat(tryte, "10");
} else if (_asm[i]=='+') { } else if (_asm[i]==1) {
strcat(tryte, "01"); strcat(tryte, "01");
} else { } else {
strcat(tryte, "00"); strcat(tryte, "00");
} }
} }
int j = 0;
for (int i = 0; i < 12; i++) {
t0[i] = tryte[j];
j++;
}
for (int i = 0; i < 14; i++) {
t1[i] = tryte[j];
j++;
}
for (int i = 0; i < 14; i++) {
t2[i] = tryte[j];
j++;
}
for (int i = 0; i < 14; i++) {
t3[i] = tryte[j];
j++;
}
bin_to_hex(t0, opcode); bin_to_hex(t0, opcode);
bin_to_hex(t1, in0); bin_to_hex(t1, in0);
@ -85,6 +111,7 @@ int main(int argc, char **argv) {
hex_in1 = 0; hex_in1 = 0;
hex_out = 0; hex_out = 0;
printf("%s/n",in_asm);
if (in_asm[strlen(in_asm)-1] == '\n') { if (in_asm[strlen(in_asm)-1] == '\n') {
in_asm[strlen(in_asm)-1] = '\0'; //cleans newline from string in_asm[strlen(in_asm)-1] = '\0'; //cleans newline from string
} }