-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbesm6_arch.cpp
439 lines (398 loc) · 11.1 KB
/
besm6_arch.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//
// BESM-6 architecture details.
//
// Copyright (c) 2022-2023 Leonid Broukhis, Serge Vakulenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "besm6_arch.h"
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
//
// 48-й разряд -> 1, 47-й -> 2 и т.п.
// Единица 1-го разряда и нулевое слово -> 48,
// как в первоначальном варианте системы команд.
//
unsigned besm6_highest_bit(Word val)
{
int n = 32, cnt = 0;
do {
Word tmp = val;
if (tmp >>= n) {
cnt += n;
val = tmp;
}
} while (n >>= 1);
return 48 - cnt;
}
//
// Сборка значения по маске.
//
Word besm6_pack(Word val, Word mask)
{
Word result;
result = 0;
for (; mask; mask >>= 1, val >>= 1)
if (mask & 1) {
result >>= 1;
if (val & 1)
result |= BIT48;
}
return result;
}
//
// Разборка значения по маске.
//
Word besm6_unpack(Word val, Word mask)
{
Word result;
unsigned i;
result = 0;
for (i = 0; i < 48; ++i) {
result <<= 1;
if (mask & BIT48) {
if (val & BIT48)
result |= 1;
val <<= 1;
}
mask <<= 1;
}
return result;
}
//
// Подсчёт количества единиц в слове.
//
unsigned besm6_count_ones(Word word)
{
unsigned c;
for (c = 0; word; ++c)
word &= word - 1;
return c;
}
//
// Check whether instruction is extracode.
//
bool is_extracode(unsigned opcode)
{
switch (opcode) {
case 050:
case 051:
case 052:
case 053: // э50...э77
case 054:
case 055:
case 056:
case 057:
case 060:
case 061:
case 062:
case 063:
case 064:
case 065:
case 066:
case 067:
case 070:
case 071:
case 072:
case 073:
case 074:
case 075:
case 076:
case 077:
case 0200: // э20
case 0210: // э21
return true;
}
return false;
}
//
// Преобразование вещественного числа в формат БЭСМ-6.
// Всегда возвращает нормализованное число.
//
// Представление чисел в IEEE 754 (double):
// 64 63———53 52————–1
// знак порядок мантисса
// Старший (53-й) бит мантиссы не хранится и всегда равен 1.
//
// Представление чисел в БЭСМ-6:
// 48——–42 41 40————————————————–1
// порядок знак мантисса в доп. коде
//
Word ieee_to_besm6(const double input)
{
// Split into mantissa and exponent.
int exponent;
double mantissa = frexp(input, &exponent);
if (mantissa == 0.0) {
return 0;
}
// Multiply mantissa by 2^40.
mantissa = ldexp(mantissa, 40);
Word word;
if (mantissa > 0) {
// Positive value in range [0.5, 1) * 2⁴⁰
// Get 40 bits of mantissa.
word = (Word)mantissa;
if (mantissa - word >= 0.5) {
// Rounding.
word += 1;
if (word == 1LL << 40) {
// Normalize.
word >>= 1;
exponent += 1;
}
}
if (exponent > 63) {
// Overflow: return the most positive number.
return 07757'7777'7777'7777LL;
}
} else {
// Negative value in range (-1, -0.5] * 2⁴⁰.
// Convert it to [-1, -0.5).
if (mantissa == -(1LL << 39)) {
if (exponent == -64) {
// The smallest negative number.
return 0027'7777'7777'7777LL;
}
mantissa += mantissa;
exponent -= 1;
}
// Account for the sign bit.
// The value becomes positive.
mantissa += 1LL << 40;
// Get 40 bits of mantissa.
word = (Word)mantissa;
if (mantissa - word > 0.5) {
// Rounding.
word += 1;
if (word == 1LL << 40) {
// Normalize.
word >>= 1;
exponent += 1;
}
}
if (exponent > 63) {
// Overflow: return the most negative number.
return 07760'0000'0000'0000LL;
}
word |= 1LL << 40;
}
if (exponent < -64) {
// Underflow.
return 0LL;
}
// Add exponent.
word |= ((Word)(exponent + 64)) << 41;
return word;
}
double besm6_to_ieee(Word word)
{
double mantissa;
int exponent;
// Убираем свертку
word &= BITS48;
// Сдвигаем так, чтобы знак мантиссы пришелся на знак целого;
// таким образом, mantissa равно исходной мантиссе, умноженной на 2**63.
//
mantissa = (double)(((int64_t)word) << (64 - 48 + 7));
exponent = word >> 41;
// Порядок смещен вверх на 64, и мантиссу нужно скорректировать
return ldexp(mantissa, exponent - 64 - 63);
}
//
// Печать машинной инструкции с мнемоникой.
//
void besm6_print_instruction_mnemonics(std::ostream &out, unsigned cmd)
{
auto save_flags = out.flags();
unsigned reg, opcode, addr;
reg = (cmd >> 20) & 017;
if (cmd & ONEBIT(20)) {
opcode = (cmd >> 12) & 0370;
addr = cmd & BITS(15);
} else {
opcode = (cmd >> 12) & 077;
addr = cmd & 07777;
if (cmd & ONEBIT(19))
addr |= 070000;
}
out << besm6_opname(opcode) << std::oct;
if (addr) {
out << ' ';
if (addr >= 077700)
out << '-' << ((addr ^ 077777) + 1);
else
out << addr;
}
if (reg) {
if (!addr)
out << ' ';
out << '(' << reg << ')';
}
// Restore.
out.flags(save_flags);
}
//
// Печать машинной инструкции в восьмеричном виде.
//
void besm6_print_instruction_octal(std::ostream &out, unsigned cmd)
{
auto save_flags = out.flags();
out << std::oct << std::setfill('0') << std::setw(2) << (cmd >> 20) << ' ';
if (cmd & ONEBIT(20)) {
out << std::setfill('0') << std::setw(2) << ((cmd >> 15) & 037) << ' ';
out << std::setfill('0') << std::setw(5) << (cmd & BITS(15));
} else {
out << std::setfill('0') << std::setw(3) << ((cmd >> 12) & 0177) << ' ';
out << std::setfill('0') << std::setw(4) << (cmd & BITS(12));
}
// Restore.
out.flags(save_flags);
}
//
// Print 48-bit value as octal.
//
void besm6_print_word_octal(std::ostream &out, Word value)
{
auto save_flags = out.flags();
out << std::oct;
out << std::setfill('0') << std::setw(4) << ((int)(value >> 36) & BITS(12)) << ' ';
out << std::setfill('0') << std::setw(4) << ((int)(value >> 24) & BITS(12)) << ' ';
out << std::setfill('0') << std::setw(4) << ((int)(value >> 12) & BITS(12)) << ' ';
out << std::setfill('0') << std::setw(4) << ((int)value & BITS(12));
// Restore.
out.flags(save_flags);
}
//
// Print 48-bit value as bytes.
//
void besm6_print_word_bytes(std::ostream &out, Word value)
{
auto save_flags = out.flags();
out << std::oct;
out << std::setfill('0') << std::setw(3) << ((int)(value >> 40) & BITS(8)) << ' ';
out << std::setfill('0') << std::setw(3) << ((int)(value >> 32) & BITS(8)) << ' ';
out << std::setfill('0') << std::setw(3) << ((int)(value >> 24) & BITS(8)) << ' ';
out << std::setfill('0') << std::setw(3) << ((int)(value >> 16) & BITS(8)) << ' ';
out << std::setfill('0') << std::setw(3) << ((int)(value >> 8) & BITS(8)) << ' ';
out << std::setfill('0') << std::setw(3) << ((int)value & BITS(8));
// Restore.
out.flags(save_flags);
}
//
// Convert number to string as octal.
//
std::string to_octal(unsigned val)
{
std::ostringstream buf;
buf << std::oct << val;
return buf.str();
}
//
// Square root function.
//
Word besm6_sqrt(Word input)
{
const double arg = besm6_to_ieee(input);
const double result = std::sqrt(arg);
if (std::isnan(result)) {
throw std::runtime_error("Function sqrt() failed");
}
Word output = ieee_to_besm6(result);
return output;
}
//
// Sine function.
//
Word besm6_sin(Word input)
{
const double result = std::sin(besm6_to_ieee(input));
if (std::isnan(result)) {
throw std::runtime_error("Function sin() failed");
}
return ieee_to_besm6(result);
}
//
// Cosine function.
//
Word besm6_cos(Word input)
{
const double result = std::cos(besm6_to_ieee(input));
if (std::isnan(result)) {
throw std::runtime_error("Function cos() failed");
}
return ieee_to_besm6(result);
}
//
// Arc tangent function.
//
Word besm6_arctan(Word input)
{
const double result = std::atan(besm6_to_ieee(input));
if (std::isnan(result)) {
throw std::runtime_error("Function arctan() failed");
}
return ieee_to_besm6(result);
}
//
// Arc sine function.
//
Word besm6_arcsin(Word input)
{
const double result = std::asin(besm6_to_ieee(input));
if (std::isnan(result)) {
throw std::runtime_error("Function arcsin() failed");
}
return ieee_to_besm6(result);
}
//
// Natural logarithm function.
//
Word besm6_log(Word input)
{
const double result = std::log(besm6_to_ieee(input));
if (std::isnan(result)) {
throw std::runtime_error("Function log() failed");
}
return ieee_to_besm6(result);
}
//
// Exponential function with natural base 'e'.
//
Word besm6_exp(Word input)
{
const double result = std::exp(besm6_to_ieee(input));
if (std::isinf(result)) {
throw std::runtime_error("Function exp() failed");
}
return ieee_to_besm6(result);
}
//
// Largest integral value not greater than argument.
//
Word besm6_floor(Word input)
{
const double result = std::floor(besm6_to_ieee(input));
if (std::isnan(result)) {
throw std::runtime_error("Function floor() failed");
}
return ieee_to_besm6(result);
}