This repository was archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterun.cpp
302 lines (235 loc) · 4.49 KB
/
terun.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Toga Enhanced emulator
// Oleg Mazonka 2010
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <vector>
using std::string;
using std::vector;
using std::cout;
using std::cin;
using std::istream;
using std::ostream;
using std::pair;
namespace g{
int base=32, base_k, base_w;
bool intio = false;
bool conin = false;
bool dbg = false;
}
class Memory
{
vector<int> m;
public:
void add(int x){ m.push_back(x); }
int operator[](int ip) const;
bool invert(int a);
bool empty() const { return m.empty(); }
bool getbit(int a) const;
};
void usage();
void setbase();
Memory readbcc(istream &is);
void execute(Memory &m);
string tos(int x){ std::ostringstream os; os<<x; return os.str(); }
inline string tos(size_t x){ return tos(int(x)); }
int main(int ac, char *av[])
{
try{
if( ac < 2 ) usage();
int input_i = 1;
for(;;)
{
string s;
if( input_i < ac ) s = string(av[input_i]);
if( s.empty() || s[0]!='-' ) break;
else if( s=="-i" ) g::intio=true;
else if( s=="-c" ) g::conin=true;
else if( s=="-d" ) g::dbg=true;
else usage();
if( ++input_i >= ac && !g::conin ) usage();
}
setbase();
string input_name("te.run");
if( input_i < ac ) input_name = av[input_i];
std::istream * in = &std::cin;
std::ifstream fin;
if( !g::conin )
{
fin.open(input_name.c_str());
if( !fin )
{
cout<<"cannot open input file '"<<input_name<<"'\n";
return 1;
}
in = &fin;
}
Memory mem = readbcc(*in);
if( !mem.empty() ) execute(mem);
}
catch(string s)
{
cout<<"Error: "<<s<<'\n';
return 3;
}
catch(...)
{
cout<<"Unknown error\n";
return 4;
}
}
void usage()
{
cout<<"Useage: terun [-i] [-c] [-d] input.run\n";
cout<<"-i integer (but byte limited) input/output, default char\n";
cout<<"-c input from stdin\n";
cout<<"-d debug (Hint: use -t in assembler)\n";
exit(0);
}
void setbase()
{
g::base_w = g::base-1;
if( g::base == 8 ) g::base_k = 3;
else if( g::base == 16 ) g::base_k = 4;
else if( g::base == 32 ) g::base_k = 5;
else
usage();
}
Memory readbcc(istream &is)
{
Memory m;
for(;;)
{
int i=0;
is>>i;
if( !is )
break;
m.add(i);
}
return m;
}
bool toglebit(Memory &m, int a);
void execute(Memory &m)
{
int ip=0;
for(;;)
{
if(g::dbg) cout<<ip<<": "<<m[ip]<<" "<<m[ip+g::base]<<" ("
<<(m.getbit(m[ip])?"1":"0")<<")\n";
int a = m[ip]; ip+=g::base;
if ( toglebit(m,a) ) ip = m[ip];
else ip+=g::base;
if( ip < 0 )
return;
}
}
bool output(bool bit);
bool input();
bool toglebit(Memory &m, int a)
{
if( a==-1 ) return output(1);
if( a==-2 ) return output(0);
if( a==-3 ) return input();
return m.invert(a);
}
int Memory::operator[](int ip) const
{
if( ip % g::base )
throw string()+"jump to non-aligned address not supported ip="+tos(ip);
int i = ip / g::base;
if( i>= (int)m.size() || i<0 )
throw string()+"bad ip; ip="+tos(ip)+" mem_size="+tos(m.size());
return m[i];
}
bool Memory::invert(int b)
{
int i = b / g::base;
int j = ( 1 << ( b % g::base ) );
if( i==(int)m.size() ) m.push_back(0);
if( i>= (int)m.size() || i<0 )
throw string()+"bad access; B="+tos(b)+" mem_size="+tos(m.size());
if( m[i] & j )
m[i] &= ~j;
else
m[i] |= j;
return ( m[i] & j );
}
bool Memory::getbit(int a) const
{
if( a<0 )
return false;
int i = a / g::base;
int j = a % g::base;
if( i>= (int)m.size() || i<0 )
throw string()+"bad access; A="+tos(a)+" mem_size="+tos(m.size());
int mem = m[i];
if( mem & (1<<j) ) return true;
return false;
}
void input(char &c);
bool input()
{
bool bit;
static vector<bool> buf;
if( buf.empty() )
{
char c=EOF;
input(c);
if( c==EOF ) return false;
for( int i=0; i<8; i++ )
buf.push_back( (c & ( 1 << (7-i) ))?true:false );
}
bit = buf.back();
buf.pop_back();
return bit;
}
void output(char c);
bool output(bool bit)
{
const bool ret = true;
static vector<bool> buf;
if( buf.size() < 8 )
buf.push_back(bit);
if( buf.size() < 8 )
return ret;
char c=0;
for( int i=0; i<8; i++ )
{
int j = ( 1 << i );
if( buf[i] )
c = char(c|j);
else
c = char(c&~j);
}
output(c);
buf.clear();
return ret;
}
void input(char &c)
{
if( g::conin )
throw string()+"No console input when code read from stdin (-c)";
if( g::intio )
{
string s;
cin>>s;
if( !cin ) return;
int x = atoi(s.c_str());
c = (char)x;
return;
}
cin.get(c);
}
void output(char c)
{
if( g::intio )
{
int x = (unsigned)(unsigned char)c;
cout<<x<<' '<<std::flush;
return;
}
cout<<c<<std::flush;
}