/
bfvm_old.c
159 lines (146 loc) · 3.22 KB
/
bfvm_old.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
*/
#include "lib_easy/easy.h"
#define MEM_SIZE 65534
#define MACRO_VARS int MACRO_i = 0;\
int v = 0;
#define GETB() loaded_file[MACRO_i];MACRO_i++
#define PUTB() loaded_file[MACRO_i];MACRO_i++
byte inC();
void outC(byte c);
int main(int argc, char *argv[])
{
MACRO_VARS // :.
int times,f_name_i = 1;
byte b,nible,*loaded_file,stack_i;
struct space *f;
uint stack[256];
/* BF VM vars */
byte *mem;
uint pos = 0;
/* end */
if(argc == 3){
f_name_i = 2;
if(argv[1][0] == '-' && argv[1][1] == 'v')
v = 1;
}
f = loadfile(argv[f_name_i]);
loaded_file = f->p;
mem = alloc_mem(MEM_SIZE);
nible = 0; // starting by nible 0
times = 1; // run a command, at least once
b = GETB();
if(v) printf("b = 0x%x\n", b);
while(b){
if(b & BIT7){
if(v) printf("Isn't a char\n");
if(nible){
if(v) printf("Second nible\n");
if(b & BIT3){ // it is XXXX1XXXb?
if(v) printf("Is a number of times to repeat\n");
switch(b & 0x07){ // 00000111b
case 0: times = 0; break;
case 1: times = 2; break;
case 2: times = 4; break;
case 3: times = 8; break;
case 4: times = 16; break;
case 5: times = 32; break;
case 6: times = 64; break;
case 7: times = 128; break;
}
if(times == 0){
}
if(v) printf("Repeat %i\n", times);
}
else{
if(v) printf("Is a command\n");
times = 1;
b <<= 4;
b |= 0x80; // set first bt
}
}
else{
if(v) printf("First nible\n");
times = 1;
}
for(; times >= 1; times--){
if(v) printf("Executando a %i_a vez\n", times);
switch(b & 0x70){ // 01110000b
case 0x00: // ,
//if(v) printf("Digite um caracter:\n");
mem[pos] = inC();
break;
case 0x10: // +
if(times > 1){
mem[pos] += times;
if(v) printf("%i x +\n", times);
times = 0;
}
else{
if(v) printf("+\n");
mem[pos]++;
}
break;
case 0x20: // >
if(v) printf(">\n");
if(pos >= MEM_SIZE){
printf("Fatal programer stupid!\nStop! traing to acess mem after end of BF machine\n");
exit(1);
}
pos++;
break;
case 0x30: // [
if(v) printf("[\n");
if(mem[pos])
stack[stack_i++] = MACRO_i;
else
while(b != ']') b = GETB();
break;
case 0x40: // .
outC(mem[pos]);
if(v) printf(" = 0x%x\n", mem[pos]);
break;
case 0x50: // -
if(times > 1){
mem[pos] -= times;
if(v) printf("%i x -\n", times);
times = 0;
}
else{
if(v) printf("-\n");
mem[pos]--;
}
break;
case 0x60: // <
if(v) printf("<\n");
if(pos <= 0){
printf("Fatal programer stupid!\nStop! traing to acess mem before star of BF machine\n");
exit(1);
}
pos--;
break;
case 0x70: // ]
if(v) printf("]\n");
MACRO_i = stack[stack_i--];
break;
}
}
if(!nible) nible = 1;
else{
b = GETB();
if(v) printf("b = 0x%x\n", b);
nible = 0;
}
}
else{
if(v) printf("%c", b); //printf("Is a char: %c\n", b);
nible = 0;
b = GETB();
}
}
//printf("%s", mem);
printf("\n");
return(0);
}
byte inC(){ return(getc(stdin)); }
void outC(byte c){ putchar(c); }