root/src/dps8/dps8_eis.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get4
  2. get6
  3. get9
  4. put4
  5. put6
  6. put9
  7. getCrAR
  8. getMFReg18
  9. getMFReg36
  10. EISWriteCache
  11. EISReadCache
  12. EISWriteIdx
  13. EISReadIdx
  14. EISRead
  15. EISReadPage
  16. EISWritePage
  17. EISget469
  18. EISput469
  19. EISget49
  20. EISgetBitRWN
  21. setupOperandDescriptorCache
  22. setupOperandDescriptor
  23. setupEISoperands
  24. parseAlphanumericOperandDescriptor
  25. parseArgOperandDescriptor
  26. parseNumericOperandDescriptor
  27. parseBitstringOperandDescriptor
  28. cleanupOperandDescriptor
  29. tandd_eis_interruptible
  30. tandd_eis_sample
  31. a4bd
  32. s4bd
  33. axbd
  34. abd
  35. awd
  36. sbd
  37. swd
  38. s9bd
  39. asxbd
  40. cmpc
  41. scd
  42. scdr
  43. scm
  44. scmr
  45. xlate
  46. tct
  47. tctr
  48. isGBCDOvp
  49. mlr
  50. mrl
  51. EISloadInputBufferNumeric
  52. EISloadInputBufferAlphnumeric
  53. EISwriteOutputBufferToMemory
  54. writeToOutputBuffer
  55. mopCHT
  56. mopENF
  57. mopIGN
  58. mopINSA
  59. mopINSB
  60. mopINSM
  61. mopINSN
  62. mopINSP
  63. mopLTE
  64. mopMFLC
  65. mopMFLS
  66. mopMORS
  67. mopMVC
  68. mopMSES
  69. mopMVZA
  70. mopMVZB
  71. mopSES
  72. EISgetMop
  73. mopExecutor
  74. mve
  75. mvne
  76. mvt
  77. cmpn
  78. EISwrite4
  79. EISwrite9
  80. EISwrite49
  81. mvn
  82. csl
  83. getBitOffsets
  84. EISgetBitRWNR
  85. csr
  86. sztl
  87. sztr
  88. EISgetBit
  89. cmpb
  90. sign9n
  91. signExt9
  92. load9x
  93. btd
  94. dtb
  95. eisWindow
  96. eisWindowOperands
  97. eisWindowLow
  98. eisFloatAlignTrunc
  99. eisFloatWindowOperands
  100. ad2d
  101. ad3d
  102. sb2d
  103. sb3d
  104. mp2d
  105. mp3d
  106. dv2d
  107. dv3d

   1 /*
   2  * vim: filetype=c:tabstop=4:ai:expandtab
   3  * SPDX-License-Identifier: ICU
   4  * SPDX-License-Identifier: Multics
   5  * scspell-id: 312e3076-f62e-11ec-a7f3-80ee73e9b8e7
   6  *
   7  * ---------------------------------------------------------------------------
   8  *
   9  * Copyright (c) 2007-2013 Michael Mondy
  10  * Copyright (c) 2012-2016 Harry Reed
  11  * Copyright (c) 2013-2016 Charles Anthony
  12  * Copyright (c) 2015-2016 Craig Ruff
  13  * Copyright (c) 2016 Michal Tomek
  14  * Copyright (c) 2021-2026 The DPS8M Development Team
  15  *
  16  * This software is made available under the terms of the ICU License.
  17  * See the LICENSE.md file at the top-level directory of this distribution.
  18  *
  19  * ---------------------------------------------------------------------------
  20  *
  21  * This source file may contain code comments that adapt, include, and/or
  22  * incorporate Multics program code and/or documentation distributed under
  23  * the Multics License.  In the event of any discrepancy between code
  24  * comments herein and the original Multics materials, the original Multics
  25  * materials should be considered authoritative unless otherwise noted.
  26  * For more details and historical background, see the LICENSE.md file at
  27  * the top-level directory of this distribution.
  28  *
  29  * ---------------------------------------------------------------------------
  30  */
  31 
  32 #if defined(EIS_PTR)
  33 // Cached operand data...
  34 //  Alphanumeric Operand
  35 //    Address 18 bits  --> DkW
  36 //    CN 3 bits --> DkB
  37 //    TA 2 bits
  38 //    Length 12 bits
  39 //  Numeric Operand
  40 //    Address 18 bits  --> DkW
  41 //    CN 3 bits --> DkB
  42 //    TN 1 bits
  43 //    S  2 bits
  44 //    SF 6 bits
  45 //    Length 6 bits
  46 //  Bit-string Operand
  47 //    Address 18 bits  --> DkW
  48 //    C  2 bits --> DkB
  49 //    B  4 bits --> DkB
  50 //    Length 12 bits
  51 #endif /* defined(EIS_PTR) */
  52 
  53 #include <ctype.h>
  54 
  55 #include "dps8.h"
  56 #include "dps8_sys.h"
  57 #include "dps8_iom.h"
  58 #include "dps8_cable.h"
  59 #include "dps8_cpu.h"
  60 #include "dps8_faults.h"
  61 #include "dps8_scu.h"
  62 #include "dps8_iefp.h"
  63 #include "dps8_decimal.h"
  64 #include "dps8_ins.h"
  65 #include "dps8_eis.h"
  66 #include "dps8_utils.h"
  67 
  68 #define DBG_CTR cpu.cycleCnt
  69 
  70 //  Restart status
  71 //
  72 //  a6bd   n/a
  73 //  a4bd   n/a
  74 //  a9bd   n/a
  75 //  abd    n/a
  76 //  awd    n/a
  77 //  s4bd   n/a
  78 //  s6bd   n/a
  79 //  s9bd   n/a
  80 //  sbd    n/a
  81 //  swd    n/a
  82 //  cmpc   done
  83 //  scd    done
  84 //  scdr   done
  85 //  scm    done
  86 //  scmr   done
  87 //  tct    done
  88 //  tctr   done
  89 //  mlr    done
  90 //  mrl    done
  91 //  mve
  92 //  mvne
  93 //  mvt    done
  94 //  cmpn
  95 //  mvn
  96 //  csl    done
  97 //  csr    done
  98 //  cmpb
  99 //  btd
 100 //  dtb
 101 //  ad2d
 102 //  ad3d
 103 //  sb2d
 104 //  sb3d
 105 //  mp2d
 106 //  mp3d
 107 //  dv2d
 108 //  dv3d
 109 
 110 // Local optimization
 111 #define ABD_BITS
 112 
 113 // Enable EIS operand setup refactoring code -- crashes Multics late in boot.
 114 //#define EIS_SETUP
 115 
 116 //  EISWriteCache  -- flush the cache
 117 //
 118 //
 119 //  EISWriteIdx (cpup, p, n, data, flush); -- write to cache at p->addr [n];
 120 //  EISRead (cpup, p) -- read to cache from p->addr
 121 //  EISReadIdx (cpup, p, n)  -- read to cache from p->addr[n];
 122 //  EISReadN (p, n, dst) -- read N words to dst;
 123 
 124 //  EISget469 (cpup, k, i)
 125 //  EISput469 (cpup, k, i, c)
 126 
 127 //  EISget49 (cpup, k, *pos, tn) get p->addr[*pos++]
 128 
 129 // AL39, Figure 2-3
 130 static word4 get4 (word36 w, int pos)
     /* [previous][next][first][last][top][bottom][index][help] */
 131   {
 132     switch (pos)
 133       {
 134         case 0:
 135           return getbits36_4 (w, 1);
 136 
 137         case 1:
 138           return getbits36_4 (w, 5);
 139 
 140         case 2:
 141           return getbits36_4 (w, 10);
 142 
 143         case 3:
 144           return getbits36_4 (w, 14);
 145 
 146         case 4:
 147           return getbits36_4 (w, 19);
 148 
 149         case 5:
 150           return getbits36_4 (w, 23);
 151 
 152         case 6:
 153           return getbits36_4 (w, 28);
 154 
 155         case 7:
 156           return getbits36_4 (w, 32);
 157 
 158       }
 159     sim_printf ("get4(): How'd we get here?\r\n");
 160     return 0;
 161 }
 162 
 163 // AL39, Figure 2-4
 164 static word4 get6 (word36 w, int pos)
     /* [previous][next][first][last][top][bottom][index][help] */
 165   {
 166     switch (pos)
 167       {
 168         case 0:
 169          return getbits36_6 (w, 0);
 170 
 171         case 1:
 172          return getbits36_6 (w, 6);
 173 
 174         case 2:
 175          return getbits36_6 (w, 12);
 176 
 177         case 3:
 178          return getbits36_6 (w, 18);
 179 
 180         case 4:
 181          return getbits36_6 (w, 24);
 182 
 183         case 5:
 184          return getbits36_6 (w, 30);
 185 
 186       }
 187     sim_printf ("get6(): How'd we get here?\r\n");
 188     return 0;
 189   }
 190 
 191 // AL39, Figure 2-5
 192 static word9 get9(word36 w, int pos)
     /* [previous][next][first][last][top][bottom][index][help] */
 193   {
 194     switch (pos)
 195       {
 196         case 0:
 197          return getbits36_9 (w, 0);
 198 
 199         case 1:
 200          return getbits36_9 (w, 9);
 201 
 202         case 2:
 203          return getbits36_9 (w, 18);
 204 
 205         case 3:
 206          return getbits36_9 (w, 27);
 207 
 208       }
 209     sim_printf ("get9(): How'd we get here?\r\n");
 210     return 0;
 211   }
 212 
 213 // AL39, Figure 2-3
 214 static word36 put4 (word36 w, int pos, word4 c)
     /* [previous][next][first][last][top][bottom][index][help] */
 215   {
 216 // AL-39 pg 13: "The 0 bits at it positions 0, 9, 18, and 27 are forced to be 0
 217 // by the processor on data transfers to main memory ..."
 218 //
 219 // The code uses 5 bit sets for the even bytes to force the 0 writes.
 220 
 221     c &= MASK4;
 222     switch (pos)
 223       {
 224         case 0:
 225           //return setbits36_4 (w, 1, c);
 226           return setbits36_5 (w, 0, c);
 227 
 228         case 1:
 229           return setbits36_4 (w, 5, c);
 230 
 231         case 2:
 232           //return setbits36_4 (w, 10, c);
 233           return setbits36_5 (w, 9, c);
 234 
 235         case 3:
 236           return setbits36_4 (w, 14, c);
 237 
 238         case 4:
 239           //return setbits36_4 (w, 19, c);
 240           return setbits36_5 (w, 18, c);
 241 
 242         case 5:
 243           return setbits36_4 (w, 23, c);
 244 
 245         case 6:
 246           //return setbits36_4 (w, 28, c);
 247           return setbits36_5 (w, 27, c);
 248 
 249         case 7:
 250           return setbits36_4 (w, 32, c);
 251       }
 252     sim_printf ("put4(): How'd we get here?\r\n");
 253     return 0;
 254   }
 255 
 256 // AL39, Figure 2-4
 257 static word36 put6 (word36 w, int pos, word6 c)
     /* [previous][next][first][last][top][bottom][index][help] */
 258   {
 259     switch (pos)
 260       {
 261         case 0:
 262           //return bitfieldInsert36 (w, c, 30, 6);
 263           return setbits36_6 (w, 0, c);
 264 
 265         case 1:
 266           //`return bitfieldInsert36 (w, c, 24, 6);
 267           return setbits36_6 (w, 6, c);
 268 
 269         case 2:
 270           //return bitfieldInsert36 (w, c, 18, 6);
 271           return setbits36_6 (w, 12, c);
 272 
 273         case 3:
 274           //return bitfieldInsert36 (w, c, 12, 6);
 275           return setbits36_6 (w, 18, c);
 276 
 277         case 4:
 278           //return bitfieldInsert36 (w, c, 6, 6);
 279           return setbits36_6 (w, 24, c);
 280 
 281         case 5:
 282           //return bitfieldInsert36 (w, c, 0, 6);
 283           return setbits36_6 (w, 30, c);
 284 
 285       }
 286     sim_printf ("put6(): How'd we get here?\r\n");
 287     return 0;
 288   }
 289 
 290 // AL39, Figure 2-5
 291 static word36 put9 (word36 w, int pos, word9 c)
     /* [previous][next][first][last][top][bottom][index][help] */
 292   {
 293     switch (pos)
 294       {
 295         case 0:
 296           //return bitfieldInsert36 (w, c, 27, 9);
 297           return setbits36_9 (w, 0, c);
 298 
 299         case 1:
 300           //return bitfieldInsert36 (w, c, 18, 9);
 301           return setbits36_9 (w, 9, c);
 302 
 303         case 2:
 304           //return bitfieldInsert36 (w, c, 9, 9);
 305           return setbits36_9 (w, 18, c);
 306 
 307         case 3:
 308           //return bitfieldInsert36 (w, c, 0, 9);
 309           return setbits36_9 (w, 27, c);
 310 
 311       }
 312     sim_printf ("put9(): How'd we get here?\r\n");
 313     return 0;
 314   }
 315 
 316 /*
 317  * get register value indicated by reg for Address Register operations
 318  * (not for use with address modifications)
 319  */
 320 
 321 static word36 getCrAR (cpu_state_t * cpup, word4 reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 322   {
 323     if (reg == 0)
 324       return 0;
 325 
 326     if (reg & 010) /* Xn */
 327       return cpu.rX [X (reg)];
 328 
 329     switch (reg)
 330       {
 331         case TD_N:
 332           return 0;
 333 
 334         case TD_AU: // C(A)0,17
 335 #if defined(TESTING)
 336           HDBGRegAR ("au");
 337 #endif
 338           return GETHI (cpu.rA);
 339 
 340         case TD_QU: //  C(Q)0,17
 341 #if defined(TESTING)
 342           HDBGRegAR ("qu");
 343 #endif
 344           return GETHI (cpu.rQ);
 345 
 346         case TD_IC: // C(PPR.IC)
 347           return cpu.PPR.IC;
 348 
 349         case TD_AL: // C(A)18,35
 350 #if defined(TESTING)
 351           HDBGRegAR ("al");
 352 #endif
 353           return cpu.rA; // See AL36, Table 4-1
 354 
 355         case TD_QL: // C(Q)18,35
 356 #if defined(TESTING)
 357           HDBGRegAR ("ql");
 358 #endif
 359           return cpu.rQ; // See AL36, Table 4-1
 360       }
 361     return 0;
 362   }
 363 
 364 // getMFReg
 365 //  RType reflects the AL-39 R-type and C(op. desc.)32,35 columns
 366 //
 367 //  Table 4-1. R-type Modifiers for REG Fields
 368 //
 369 //                   Meaning as used in:
 370 //
 371 //  Octal  R-type  MF.REG   Indirect operand    C(operand descriptor)32,35
 372 //  Code                    descriptor-pointer
 373 //  00         n       n          n                      IPR
 374 //  01        au      au          au                      au
 375 //  02        qu      qu          qu                      qu
 376 //  03        du     IPR         IPR                      du (a)
 377 //  04        ic      ic          ic                      ic (b)
 378 //  05        al       a (c)      al                       a (c)
 379 //  06        ql       q (c)      ql                       a (c)
 380 //  07        dl     IPR         IPR                     IPR
 381 //  1n        xn      xn          xn                      xn
 382 //
 383 
 384 static word18 getMFReg18 (cpu_state_t * cpup, uint n, bool allowDU, bool allowNIC, fault_ipr_subtype_ *mod_fault)
     /* [previous][next][first][last][top][bottom][index][help] */
 385   {
 386     switch (n)
 387       {
 388         case 0: // n
 389           if (! allowNIC)
 390             {
 391               //sim_printf ("getMFReg18 n\r\n");
 392               *mod_fault |= FR_ILL_MOD;
 393               //doFault (FAULT_IPR, fst_ill_mod, "getMFReg18 n");
 394             }
 395           return 0;
 396 
 397         case 1: // au
 398 #if defined(TESTING)
 399           HDBGRegAR ("au");
 400 #endif
 401           return GETHI (cpu.rA);
 402 
 403         case 2: // qu
 404 #if defined(TESTING)
 405           HDBGRegAR ("qu");
 406 #endif
 407           return GETHI (cpu.rQ);
 408 
 409         case 3: // du
 410           // du is a special case for SCD, SCDR, SCM, and SCMR
 411 // XXX needs attention; doesn't work with old code; triggered by
 412 // XXX parseOperandDescriptor;
 413           if (! allowDU)
 414             {
 415 
 416 
 417 
 418 
 419 
 420 
 421 
 422 
 423 
 424 
 425 
 426 
 427               //sim_printf ("getMFReg18 du\r\n");
 428               *mod_fault |= FR_ILL_MOD;
 429               //doFault (FAULT_IPR, fst_ill_mod, "getMFReg18 du");
 430             }
 431           return 0;
 432 
 433         case 4: // ic - The ic modifier is permitted in
 434                 // C (od)32,35 only if MFk.RL = 0, that is, if the contents of
 435                 // the register is an address offset, not the designation of
 436                 // a register containing the operand length.
 437                 // Note that AL39 is wrong saying "is permitted in MFk.REG and C(od)32,35". Only C(od)32,35 is correct. cf RJ78
 438           if (! allowNIC)
 439             {
 440               //sim_printf ("getMFReg18 n\r\n");
 441               *mod_fault |= FR_ILL_MOD;
 442               //doFault (FAULT_IPR, fst_ill_mod, "getMFReg18 ic");
 443             }
 444           return cpu.PPR.IC;
 445 
 446         case 5: // al / a
 447 #if defined(TESTING)
 448           HDBGRegAR ("al/a");
 449 #endif
 450           return GETLO (cpu.rA);
 451 
 452         case 6: // ql / a
 453 #if defined(TESTING)
 454           HDBGRegAR ("ql/a");
 455 #endif
 456           return GETLO (cpu.rQ);
 457 
 458         case 7: // dl
 459           *mod_fault |= FR_ILL_MOD;
 460           //doFault (FAULT_IPR, fst_ill_mod, "getMFReg18 dl");
 461           return 0;
 462 
 463         case 8:
 464         case 9:
 465         case 10:
 466         case 11:
 467         case 12:
 468         case 13:
 469         case 14:
 470         case 15:
 471           return cpu.rX [n - 8];
 472       }
 473     sim_printf ("getMFReg18(cpup, ): How'd we get here? n=%d\r\n", n);
 474     return 0;
 475   }
 476 
 477 static word36 getMFReg36 (cpu_state_t * cpup, uint n, bool allowDU, bool allowNIC, fault_ipr_subtype_ *mod_fault)
     /* [previous][next][first][last][top][bottom][index][help] */
 478   {
 479     switch (n)
 480       {
 481         case 0: // n
 482          if (! allowNIC)
 483            {
 484              //sim_printf ("getMFReg36 n\r\n");
 485              *mod_fault |= FR_ILL_MOD;
 486              //doFault (FAULT_IPR, fst_ill_mod, "getMFReg36 n");
 487            }
 488           return 0;
 489         case 1: // au
 490 #if defined(TESTING)
 491           HDBGRegAR ("au");
 492 #endif
 493           return GETHI (cpu.rA);
 494 
 495         case 2: // qu
 496 #if defined(TESTING)
 497           HDBGRegAR ("qu");
 498 #endif
 499           return GETHI (cpu.rQ);
 500 
 501         case 3: // du
 502           // du is a special case for SCD, SCDR, SCM, and SCMR
 503           if (! allowDU)
 504            *mod_fault |= FR_ILL_MOD;
 505            //doFault (FAULT_IPR, fst_ill_mod, "getMFReg36 du");
 506           return 0;
 507 
 508         case 4: // ic - The ic modifier is permitted in MFk.REG and
 509                 // C (od)32,35 only if MFk.RL = 0, that is, if the contents of
 510                 // the register is an address offset, not the designation of
 511                 // a register containing the operand length.
 512                 // Note that AL39 is wrong saying "is permitted in MFk.REG and C(od)32,35". Only C(od)32,35 is correct. cf RJ78
 513           if (! allowNIC)
 514             {
 515               //sim_printf ("getMFReg36 n\r\n");
 516               *mod_fault |= FR_ILL_MOD;
 517               //doFault (FAULT_IPR, fst_ill_mod, "getMFReg36 ic");
 518             }
 519           return cpu.PPR.IC;
 520 
 521         case 5: // al / a
 522 #if defined(TESTING)
 523           HDBGRegAR ("al/a");
 524 #endif
 525           return cpu.rA;
 526 
 527         case 6: // ql / a
 528 #if defined(TESTING)
 529           HDBGRegAR ("ql/a");
 530 #endif
 531             return cpu.rQ;
 532 
 533         case 7: // dl
 534              *mod_fault |= FR_ILL_MOD;
 535              //doFault (FAULT_IPR, fst_ill_mod, "getMFReg36 dl");
 536              return 0;
 537 
 538         case 8:
 539         case 9:
 540         case 10:
 541         case 11:
 542         case 12:
 543         case 13:
 544         case 14:
 545         case 15:
 546             return cpu.rX [n - 8];
 547       }
 548     sim_printf ("getMFReg36(cpup, ): How'd we get here? n=%d\r\n", n);
 549     return 0;
 550   }
 551 
 552 #define EISADDR_IDX(p) ((p) - cpu.currentEISinstruction.addr)
 553 
 554 static void EISWriteCache (cpu_state_t * cpup, EISaddr * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 555   {
 556     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISWriteCache addr %06o\r\n", p->cachedAddr);
 557     word3 saveTRR = cpu.TPR.TRR;
 558 
 559     if (p -> cacheValid && p -> cacheDirty)
 560       {
 561         if (p -> mat == viaPR)
 562           {
 563             cpu.TPR.TRR = p -> RNR;
 564             cpu.TPR.TSR = p -> SNR;
 565             cpu.cu.XSF = 0;
 566             if_sim_debug (DBG_TRACEEXT, & cpu_dev)
 567               {
 568                 for (uint i = 0; i < 8; i ++)
 569                   if (p->wordDirty[i])
 570                     {
 571                   sim_debug (DBG_TRACEEXT, & cpu_dev,
 572                              "%s: writeCache (PR) %012"PRIo64"@%o:%06o\r\n",
 573                              __func__, p -> cachedParagraph [i], p -> SNR, p -> cachedAddr + i);
 574                    }
 575               }
 576 { long eisaddr_idx = EISADDR_IDX (p);
 577 sim_debug (DBG_TRACEEXT, & cpu_dev, "EIS %ld Write8 TRR %o TSR %05o\r\n", eisaddr_idx, cpu.TPR.TRR, cpu.TPR.TSR); }
 578             for (uint i = 0; i < 8; i ++)
 579               if (p->wordDirty[i])
 580                 {
 581                   Write1 (cpup, p->cachedAddr+i, p -> cachedParagraph[i], true);
 582                   p->wordDirty[i] = false;
 583                 }
 584           }
 585         else
 586           {
 587             //if (get_addr_mode() == APPEND_mode)
 588               //{
 589             cpu.TPR.TRR = cpu.PPR.PRR;
 590             cpu.TPR.TSR = cpu.PPR.PSR;
 591             cpu.cu.XSF = 0;
 592               //}
 593 
 594             if_sim_debug (DBG_TRACEEXT, & cpu_dev)
 595               {
 596                 for (uint i = 0; i < 8; i ++)
 597                   if (p->wordDirty[i])
 598                     {
 599                   sim_debug (DBG_TRACEEXT, & cpu_dev,
 600                              "%s: writeCache %012"PRIo64"@%o:%06o\r\n",
 601                              __func__, p -> cachedParagraph [i], cpu.TPR.TSR, p -> cachedAddr + i);
 602                      }
 603               }
 604 { long eisaddr_idx = EISADDR_IDX (p);
 605 sim_debug (DBG_TRACEEXT, & cpu_dev, "EIS %ld Write8 NO PR TRR %o TSR %05o\r\n", eisaddr_idx, cpu.TPR.TRR, cpu.TPR.TSR); }
 606             for (uint i = 0; i < 8; i ++)
 607               if (p->wordDirty[i])
 608                 {
 609                   Write1 (cpup, p->cachedAddr+i, p -> cachedParagraph[i], false);
 610                   p->wordDirty[i] = false;
 611                 }
 612           }
 613       }
 614     p -> cacheDirty = false;
 615     cpu.TPR.TRR = saveTRR;
 616   }
 617 
 618 static void EISReadCache (cpu_state_t * cpup, EISaddr * p, word18 address)
     /* [previous][next][first][last][top][bottom][index][help] */
 619   {
 620     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISReadCache addr %06o\r\n", address);
 621     word3 saveTRR = cpu.TPR.TRR;
 622 
 623     address &= AMASK;
 624 
 625     word18 paragraphAddress = address & paragraphMask;
 626     //word3 paragraphOffset = address & paragraphOffsetMask;
 627 
 628     if (p -> cacheValid && p -> cachedAddr == paragraphAddress)
 629       {
 630         return;
 631       }
 632 
 633     if (p -> cacheValid && p -> cacheDirty && p -> cachedAddr != paragraphAddress)
 634       {
 635         EISWriteCache (cpup, p);
 636       }
 637 
 638     if (p -> mat == viaPR)
 639       {
 640         cpu.TPR.TRR = p -> RNR;
 641         cpu.TPR.TSR = p -> SNR;
 642         cpu.cu.XSF = 0;
 643 { long eisaddr_idx = EISADDR_IDX (p);
 644 sim_debug (DBG_TRACEEXT, & cpu_dev, "EIS %ld Read8 TRR %o TSR %05o\r\n", eisaddr_idx, cpu.TPR.TRR, cpu.TPR.TSR); }
 645         Read8 (cpup, paragraphAddress, p -> cachedParagraph, true);
 646 
 647         if_sim_debug (DBG_TRACEEXT, & cpu_dev)
 648           {
 649             for (uint i = 0; i < 8; i ++)
 650               sim_debug (DBG_TRACEEXT, & cpu_dev,
 651                          "%s: readCache (PR) %012"PRIo64"@%o:%06o\r\n",
 652                            __func__, p -> cachedParagraph [i], p -> SNR, paragraphAddress + i);
 653           }
 654       }
 655     else
 656       {
 657         //if (get_addr_mode() == APPEND_mode)
 658           //{
 659         cpu.TPR.TRR = cpu.PPR.PRR;
 660         cpu.TPR.TSR = cpu.PPR.PSR;
 661         cpu.cu.XSF = 0;
 662           //}
 663 
 664 { long eisaddr_idx = EISADDR_IDX (p);
 665 sim_debug (DBG_TRACEEXT, & cpu_dev, "EIS %ld Read8 NO PR TRR %o TSR %05o\r\n", eisaddr_idx, cpu.TPR.TRR, cpu.TPR.TSR); }
 666         Read8 (cpup, paragraphAddress, p -> cachedParagraph, false);
 667         if_sim_debug (DBG_TRACEEXT, & cpu_dev)
 668           {
 669             for (uint i = 0; i < 8; i ++)
 670               sim_debug (DBG_TRACEEXT, & cpu_dev,
 671                          "%s: readCache %012"PRIo64"@%o:%06o\r\n",
 672                          __func__, p -> cachedParagraph [i], cpu.TPR.TSR, paragraphAddress + i);
 673           }
 674       }
 675     p -> cacheValid = true;
 676     p -> cacheDirty = false;
 677     for (uint i = 0; i < 8; i ++)
 678       p->wordDirty[i] = false;
 679     p -> cachedAddr = paragraphAddress;
 680     cpu.TPR.TRR = saveTRR;
 681   }
 682 
 683 static void EISWriteIdx (cpu_state_t * cpup, EISaddr *p, uint n, word36 data, bool flush)
     /* [previous][next][first][last][top][bottom][index][help] */
 684 {
 685 #if defined(EIS_PTR)
 686     long eisaddr_idx = EISADDR_IDX (p);
 687 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
 688     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISWriteIdx addr %06o n %u\r\n", cpu.du.Dk_PTR_W[eisaddr_idx], n);
 689     word18 addressN = (cpu.du.Dk_PTR_W[eisaddr_idx] + n) & AMASK;
 690 #else
 691     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISWriteIdx addr %06o n %u\r\n", p->address, n);
 692     word18 addressN = p -> address + n;
 693 #endif
 694     addressN &= AMASK;
 695 
 696     word18 paragraphAddress = addressN & paragraphMask;
 697     word3 paragraphOffset = addressN & paragraphOffsetMask;
 698 
 699     if (p -> cacheValid && p -> cacheDirty && p -> cachedAddr != paragraphAddress)
 700       {
 701         EISWriteCache (cpup, p);
 702       }
 703     if ((! p -> cacheValid) || p -> cachedAddr != paragraphAddress)
 704       {
 705         EISReadCache (cpup, p, paragraphAddress);
 706       }
 707     p -> cacheDirty = true;
 708     p -> wordDirty[paragraphOffset] = true;
 709     p -> cachedParagraph [paragraphOffset] = data;
 710     p -> cachedAddr = paragraphAddress;
 711     // This a little brute force; if we fault on the next read, the cached value
 712     // is lost. There might be a way to logic it up so that when the next read
 713     // word offset changes, then we write the cache before doing the read. For
 714     // right now, be pessimistic.
 715     if (flush)
 716       {
 717         EISWriteCache (cpup, p);
 718       }
 719 }
 720 
 721 static word36 EISReadIdx (cpu_state_t * cpup, EISaddr * p, uint n)
     /* [previous][next][first][last][top][bottom][index][help] */
 722   {
 723 #if defined(EIS_PTR)
 724     long eisaddr_idx = EISADDR_IDX (p);
 725 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
 726 
 727     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISReadIdx addr %06o n %u\r\n", cpu.du.Dk_PTR_W[eisaddr_idx], n);
 728     word18 addressN = (cpu.du.Dk_PTR_W[eisaddr_idx] + n) & AMASK;
 729 #else
 730     long eisaddr_idx = EISADDR_IDX (p);
 731     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISReadIdx %ld addr %06o n %u\r\n", eisaddr_idx, p->address, n);
 732     word18 addressN = p -> address + n;
 733 #endif
 734     addressN &= AMASK;
 735 
 736     word18 paragraphAddress = addressN & paragraphMask;
 737     word3 paragraphOffset = addressN & paragraphOffsetMask;
 738 
 739     if (p -> cacheValid && p -> cachedAddr == paragraphAddress)
 740       {
 741         return p -> cachedParagraph [paragraphOffset];
 742       }
 743     if (p -> cacheValid && p -> cacheDirty)
 744       {
 745         EISWriteCache (cpup, p);
 746       }
 747     EISReadCache (cpup, p, paragraphAddress);
 748     return p -> cachedParagraph [paragraphOffset];
 749   }
 750 
 751 static word36 EISRead (cpu_state_t * cpup, EISaddr * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 752   {
 753 #if defined(EIS_PTR)
 754     long eisaddr_idx = EISADDR_IDX (p);
 755 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
 756 
 757     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISRead addr %06o\r\n", cpu.du.Dk_PTR_W[eisaddr_idx]);
 758 #else
 759     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISRead addr %06o\r\n", p->address);
 760 #endif
 761     return EISReadIdx (cpup, p, 0);
 762   }
 763 
 764 
 765 
 766 
 767 
 768 
 769 
 770 
 771 
 772 
 773 
 774 
 775 
 776 
 777 
 778 
 779 
 780 
 781 static void EISReadPage (cpu_state_t * cpup, EISaddr * p, uint n, word36 * data)
     /* [previous][next][first][last][top][bottom][index][help] */
 782   {
 783 #if defined(EIS_PTR)
 784     long eisaddr_idx = EISADDR_IDX (p);
 785 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
 786     word18 addressN = (cpu.du.Dk_PTR_W[eisaddr_idx] + n) & AMASK;
 787 #else
 788     word18 addressN = p -> address + n;
 789 #endif
 790     addressN &= AMASK;
 791 
 792     sim_debug (DBG_TRACEEXT, & cpu_dev, "%s addr %06o\r\n", __func__, addressN);
 793     if ((addressN & PGMK) != 0)
 794       {
 795         sim_warn ("EISReadPage not aligned %06o\r\n", addressN);
 796         addressN &= (word18) ~PGMK;
 797       }
 798 
 799     word3 saveTRR = cpu.TPR.TRR;
 800 
 801     if (p -> mat == viaPR)
 802       {
 803         cpu.TPR.TRR = p -> RNR;
 804         cpu.TPR.TSR = p -> SNR;
 805         cpu.cu.XSF = 0;
 806         ReadPage (cpup, addressN, data, true);
 807 
 808         if_sim_debug (DBG_TRACEEXT, & cpu_dev)
 809           {
 810             for (uint i = 0; i < PGSZ; i ++)
 811 #if defined(EIS_PTR)
 812               sim_debug (DBG_TRACEEXT, & cpu_dev,
 813                          "%s: (PR) %012"PRIo64"@%o:%06o\r\n",
 814                            __func__, data [i], cpu.TPR.TSR, addressN + i);
 815 #else
 816               sim_debug (DBG_TRACEEXT, & cpu_dev,
 817                          "%s: (PR) %012"PRIo64"@%o:%06o\r\n",
 818                            __func__, data [i], p -> SNR, addressN + i);
 819 #endif
 820           }
 821       }
 822     else
 823       {
 824         //if (get_addr_mode() == APPEND_mode)
 825           //{
 826         cpu.TPR.TRR = cpu.PPR.PRR;
 827         cpu.TPR.TSR = cpu.PPR.PSR;
 828         cpu.cu.XSF = 0;
 829           //}
 830 
 831         ReadPage (cpup, addressN, data, false);
 832         if_sim_debug (DBG_TRACEEXT, & cpu_dev)
 833           {
 834             for (uint i = 0; i < PGSZ; i ++)
 835               sim_debug (DBG_TRACEEXT, & cpu_dev,
 836                          "%s: %012"PRIo64"@%o:%06o\r\n",
 837                          __func__, data [i], cpu.TPR.TSR, addressN + i);
 838           }
 839       }
 840     cpu.TPR.TRR = saveTRR;
 841   }
 842 
 843 static void EISWritePage (cpu_state_t * cpup, EISaddr * p, uint n, word36 * data)
     /* [previous][next][first][last][top][bottom][index][help] */
 844   {
 845 #if defined(EIS_PTR)
 846     long eisaddr_idx = EISADDR_IDX (p);
 847 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
 848     word18 addressN = (cpu.du.Dk_PTR_W[eisaddr_idx] + n) & AMASK;
 849 #else
 850     word18 addressN = p -> address + n;
 851 #endif
 852     addressN &= AMASK;
 853 
 854     sim_debug (DBG_TRACEEXT, & cpu_dev, "%s addr %06o\r\n", __func__, addressN);
 855     if ((addressN & PGMK) != 0)
 856       {
 857         sim_warn ("EISWritePage not aligned %06o\r\n", addressN);
 858         addressN &= (uint) ~PGMK;
 859       }
 860 
 861     word3 saveTRR = cpu.TPR.TRR;
 862 
 863     if (p -> mat == viaPR)
 864       {
 865         cpu.TPR.TRR = p -> RNR;
 866         cpu.TPR.TSR = p -> SNR;
 867         cpu.cu.XSF = 0;
 868         WritePage (cpup, addressN, data, true);
 869 
 870         if_sim_debug (DBG_TRACEEXT, & cpu_dev)
 871           {
 872             for (uint i = 0; i < PGSZ; i ++)
 873 #if defined(EIS_PTR)
 874               sim_debug (DBG_TRACEEXT, & cpu_dev,
 875                          "%s: (PR) %012"PRIo64"@%o:%06o\r\n",
 876                            __func__, data [i], cpu.TPR.TSR, addressN + i);
 877 #else
 878               sim_debug (DBG_TRACEEXT, & cpu_dev,
 879                          "%s: (PR) %012"PRIo64"@%o:%06o\r\n",
 880                            __func__, data [i], p -> SNR, addressN + i);
 881 #endif
 882           }
 883       }
 884     else
 885       {
 886         //if (get_addr_mode() == APPEND_mode)
 887           //{
 888         cpu.TPR.TRR = cpu.PPR.PRR;
 889         cpu.TPR.TSR = cpu.PPR.PSR;
 890         cpu.cu.XSF = 0;
 891           //}
 892 
 893         WritePage (cpup, addressN, data, false);
 894         if_sim_debug (DBG_TRACEEXT, & cpu_dev)
 895           {
 896             for (uint i = 0; i < PGSZ; i ++)
 897               sim_debug (DBG_TRACEEXT, & cpu_dev,
 898                          "%s: %012"PRIo64"@%o:%06o\r\n",
 899                          __func__, data [i], cpu.TPR.TSR, addressN + i);
 900           }
 901       }
 902     cpu.TPR.TRR = saveTRR;
 903   }
 904 
 905 static word9 EISget469 (cpu_state_t * cpup, int k, uint i)
     /* [previous][next][first][last][top][bottom][index][help] */
 906   {
 907     EISstruct * e = & cpu.currentEISinstruction;
 908 
 909     uint nPos = 4; // CTA9
 910 #if defined(EIS_PTR3)
 911     switch (cpu.du.TAk[k-1])
 912 #else
 913     switch (e -> TA [k - 1])
 914 #endif
 915       {
 916         case CTA4:
 917             nPos = 8;
 918             break;
 919 
 920         case CTA6:
 921             nPos = 6;
 922             break;
 923       }
 924 
 925     word18 address = e -> WN [k - 1];
 926     uint nChars = i + e -> CN [k - 1];
 927 
 928     address += nChars / nPos;
 929     uint residue = nChars % nPos;
 930 
 931     PNL (cpu.du.Dk_PTR_W[k-1] = address);
 932 #if defined(EIS_PTR)
 933     cpu.du.Dk_PTR_W[k-1] = address;
 934 #else
 935     e -> addr [k - 1].address = address;
 936 #endif
 937     word36 data = EISRead (cpup, & e -> addr [k - 1]);    // read it from memory
 938 
 939     word9 c = 0;
 940 #if defined(EIS_PTR3)
 941     switch (cpu.du.TAk[k-1])
 942 #else
 943     switch (e -> TA [k - 1])
 944 #endif
 945       {
 946         case CTA4:
 947           c = (word9) get4 (data, (int) residue);
 948           break;
 949 
 950         case CTA6:
 951           c = (word9) get6 (data, (int) residue);
 952           break;
 953 
 954         case CTA9:
 955           c = get9 (data, (int) residue);
 956           break;
 957       }
 958 #if defined(EIS_PTR3)
 959     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISGet469 : k: %u TAk %u coffset %u c %o\r\n", k, cpu.du.TAk[k - 1], residue, c);
 960 #else
 961     sim_debug (DBG_TRACEEXT, & cpu_dev, "EISGet469 : k: %u TAk %u coffset %u c %o\r\n", k, e -> TA [k - 1], residue, c);
 962 #endif
 963 
 964     return c;
 965   }
 966 
 967 static void EISput469 (cpu_state_t * cpup, int k, uint i, word9 c469)
     /* [previous][next][first][last][top][bottom][index][help] */
 968   {
 969     EISstruct * e = & cpu.currentEISinstruction;
 970 
 971     uint nPos = 4; // CTA9
 972 #if defined(EIS_PTR3)
 973     switch (cpu.du.TAk[k-1])
 974 #else
 975     switch (e -> TA [k - 1])
 976 #endif
 977       {
 978         case CTA4:
 979           nPos = 8;
 980           break;
 981 
 982         case CTA6:
 983           nPos = 6;
 984           break;
 985       }
 986 
 987     word18 address = e -> WN [k - 1];
 988     uint nChars = i + e -> CN [k - 1];
 989 
 990     address += nChars / nPos;
 991     uint residue = nChars % nPos;
 992 
 993     PNL (cpu.du.Dk_PTR_W[k-1] = address);
 994 #if defined(EIS_PTR)
 995     cpu.du.Dk_PTR_W[k-1] = address;
 996 #else
 997     e -> addr [k - 1].address = address;
 998 #endif
 999     word36 data = EISRead (cpup, & e -> addr [k - 1]);    // read it from memory
1000 
1001     word36 w = 0;
1002 #if defined(EIS_PTR3)
1003     switch (cpu.du.TAk[k-1])
1004 #else
1005     switch (e -> TA [k - 1])
1006 #endif
1007       {
1008         case CTA4:
1009           w = put4 (data, (int) residue, (word4) c469);
1010           break;
1011 
1012         case CTA6:
1013           w = put6 (data, (int) residue, (word6) c469);
1014           break;
1015 
1016         case CTA9:
1017           w = put9 (data, (int) residue, c469);
1018           break;
1019       }
1020     EISWriteIdx (cpup, & e -> addr [k - 1], 0, w, true);
1021   }
1022 
1023 /*
1024  * return a 4- or 9-bit character at memory "*address" and position "*pos".
1025  * Increment pos (and address if necessary)
1026  */
1027 
1028 static word9 EISget49 (cpu_state_t * cpup, EISaddr * p, int * pos, int tn)
     /* [previous][next][first][last][top][bottom][index][help] */
1029   {
1030     int maxPos = tn == CTN4 ? 7 : 3;
1031 
1032     if (* pos > maxPos)        // overflows to next word?
1033       {   // yep....
1034         * pos = 0;        // reset to 1st byte
1035         // bump source to next address
1036 #if defined(EIS_PTR)
1037         long eisaddr_idx = EISADDR_IDX (p);
1038 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
1039         cpu.du.Dk_PTR_W[eisaddr_idx] = (cpu.du.Dk_PTR_W[eisaddr_idx] + 1) & AMASK;
1040 #else
1041         p -> address = (p -> address + 1) & AMASK;
1042 #endif
1043         p -> data = EISRead (cpup, p);    // read it from memory
1044       }
1045     else
1046       {
1047         p -> data = EISRead (cpup, p);   // read data word from memory
1048       }
1049 
1050     word9 c = 0;
1051     switch (tn)
1052       {
1053         case CTN4:
1054           c = get4 (p -> data, * pos);
1055           break;
1056         case CTN9:
1057           c = get9 (p -> data, * pos);
1058           break;
1059       }
1060 
1061     (* pos) ++;
1062     return c;
1063   }
1064 
1065 static bool EISgetBitRWN (cpu_state_t * cpup, EISaddr * p, bool flush)
     /* [previous][next][first][last][top][bottom][index][help] */
1066   {
1067 #if defined(EIS_PTR)
1068     long eisaddr_idx = EISADDR_IDX (p);
1069 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
1070 
1071 #endif
1072     int baseCharPosn = (p -> cPos * 9);     // 9-bit char bit position
1073     int baseBitPosn = baseCharPosn + p -> bPos;
1074     baseBitPosn += (int) cpu.du.CHTALLY;
1075 
1076     int bitPosn = baseBitPosn % 36;
1077     int woff = baseBitPosn / 36;
1078 
1079 #if defined(EIS_PTR)
1080     word18 saveAddr = cpu.du.Dk_PTR_W[eisaddr_idx];
1081     cpu.du.Dk_PTR_W[eisaddr_idx] += (uint) woff;
1082     cpu.du.Dk_PTR_W[eisaddr_idx] &= AMASK;
1083 #else
1084     word18 saveAddr = p -> address;
1085     p -> address += (uint) woff;
1086 #endif
1087 
1088     p -> data = EISRead (cpup, p); // read data word from memory
1089 
1090     if (p -> mode == eRWreadBit)
1091       {
1092         p -> bit = getbits36_1 (p -> data, (uint) bitPosn);
1093       }
1094     else if (p -> mode == eRWwriteBit)
1095       {
1096         p -> data = setbits36_1 (p -> data, (uint) bitPosn, p -> bit);
1097 
1098         EISWriteIdx (cpup, p, 0, p -> data, flush); // write data word to memory
1099       }
1100 
1101     p->last_bit_posn = bitPosn;
1102 
1103 #if defined(EIS_PTR)
1104     cpu.du.Dk_PTR_W[eisaddr_idx] = saveAddr;
1105 #else
1106     p -> address = saveAddr;
1107 #endif
1108     return p -> bit;
1109   }
1110 
1111 static void setupOperandDescriptorCache (cpu_state_t * cpup, int k)
     /* [previous][next][first][last][top][bottom][index][help] */
1112   {
1113     EISstruct * e = & cpu.currentEISinstruction;
1114     e -> addr [k - 1]. cacheValid = false;
1115   }
1116 
1117 //
1118 // 5.2.10.5  Operand Descriptor Address Preparation Flowchart
1119 //
1120 // A flowchart of the operations involved in operand descriptor address
1121 // preparation is shown in Figure 5-2. The chart depicts the address
1122 // preparation for operand descriptor 1 of a multiword instruction as described
1123 // by modification field 1 (MF1). A similar type address preparation would be
1124 // carried out for each operand descriptor as specified by its MF code.
1125 //    (Bull Nova 9000 pg 5-40  67 A2 RJ78 REV02)
1126 //
1127 // 1. The multiword instruction is obtained from memory.
1128 //
1129 // 2. The indirect (ID) bit of MF1 is queried to determine if the descriptor
1130 // for operand 1 is present or is an indirect word.
1131 //
1132 // 3. This step is reached only if an indirect word was in the operand
1133 // descriptor location. Address modification for the indirect word is now
1134 // performed. If the AR bit of the indirect word is 1, address register
1135 // modification step 4 is performed.
1136 //
1137 // 4. The y field of the indirect word is added to the contents of the
1138 // specified address register.
1139 //
1140 // 5. A check is now made to determine if the REG field of the indirect word
1141 // specifies that a register type modification be performed.
1142 //
1143 // 6. The indirect address as modified by the address register is now modified
1144 // by the contents of the specified register, producing the effective address
1145 // of the operand descriptor.
1146 //
1147 // 7. The operand descriptor is obtained from the location determined by the
1148 // generated effective address in item 6.
1149 //
1150 
1151 static void setupOperandDescriptor (cpu_state_t * cpup, int k, fault_ipr_subtype_ *mod_fault)
     /* [previous][next][first][last][top][bottom][index][help] */
1152   {
1153     EISstruct * e = & cpu.currentEISinstruction;
1154     switch (k)
1155       {
1156         case 1:
1157           PNL (L68_ (DU_CYCLE_FA_I1;))
1158           PNL (L68_ (DU_CYCLE_GDLDA;))
1159           e -> MF1 = getbits36_7 (cpu.cu.IWB, 29);
1160           break;
1161         case 2:
1162           PNL (L68_ (DU_CYCLE_FA_I2;))
1163           PNL (L68_ (DU_CYCLE_GDLDB;))
1164           e -> MF2 = getbits36_7 (cpu.cu.IWB, 11);
1165           break;
1166         case 3:
1167           PNL (L68_ (DU_CYCLE_FA_I3;))
1168           PNL (L68_ (DU_CYCLE_GDLDC;))
1169           e -> MF3 = getbits36_7 (cpu.cu.IWB,  2);
1170           break;
1171       }
1172 
1173     word18 MFk = e -> MF [k - 1];
1174 
1175     if (MFk & MFkID)
1176     {
1177         PNL (L68_ (if (k == 1)
1178                      DU_CYCLE_LDWRT1;
1179                    if (k == 2)
1180                      DU_CYCLE_LDWRT2;))
1181 
1182         word36 opDesc = e -> op [k - 1];
1183 
1184 // 18-28 MBZ check breaks Multics; line 161 of sys_trouble.alm contains
1185 //
1186 // 000103 aa 040100 1006 20    160   mlr     (id),(pr),fill(040) copy error message
1187 // 000104 0a 000126 0002 05    161   arg     trouble_messages-1,al
1188 // 000105 aa 300063 200063     162   desc9a  bb|fgbx.message+3(1),64-13
1189 //
1190 // bit 28 of 000104 is set
1191 //
1192 
1193 
1194 
1195 
1196 
1197 
1198 
1199 
1200 
1201 
1202 
1203 
1204 
1205 
1206 
1207 
1208 
1209 
1210 
1211 
1212 
1213 
1214         // Bits 30, 31 MBZ
1215         // RJ78 p. 5-39, ISOLTS 840 07a,07b
1216         if (opDesc & 060)
1217           {
1218             *mod_fault |= FR_ILL_MOD;
1219             //doFault (FAULT_IPR, fst_ill_mod, "setupOperandDescriptor 30,31 MBZ");
1220           }
1221 
1222         // fill operand according to MFk....
1223         word18 address = GETHI (opDesc);
1224         PNL (cpu.du.Dk_PTR_W[k-1] = address);
1225 #if defined(EIS_PTR)
1226         cpu.du.Dk_PTR_W[k-1] = address;
1227 #else
1228         e -> addr [k - 1].address = address;
1229 #endif
1230 
1231         // Indirect descriptor control. If ID = 1 for Mfk, then the kth word
1232         // following the instruction word is an indirect pointer to the operand
1233         // descriptor for the kth operand; otherwise, that word is the operand
1234         // descriptor.
1235         //
1236         // If MFk.ID = 1, then the kth word following an EIS multiword
1237         // instruction word is not an operand descriptor, but is an indirect
1238         // pointer to an operand descriptor and is interpreted as shown in
1239         // Figure 4-5.
1240 
1241         // Mike Mondy michael.mondy@coffeebird.net sez' ...
1242         // EIS indirect pointers to operand descriptors use PR registers.
1243         // However, operand descriptors use AR registers according to the
1244         // description of the AR registers and the description of EIS operand
1245         // descriptors. However, the description of the MF field
1246         // claims that operands use PR registers. The AR doesn't have a
1247         // segment field. Emulation confirms that operand descriptors
1248         // need to be fetched via segments given in PR registers.
1249 
1250         bool a = opDesc & (1 << 6);
1251         if (a)
1252           {
1253             // A 3-bit pointer register number (n) and a 15-bit offset relative
1254             // to C(PRn.WORDNO) if A = 1 (all modes)
1255             word3 n = (word3) getbits18 (address, 0, 3);
1256             CPTUR (cptUsePRn + n);
1257             word15 offset = address & MASK15;  // 15-bit signed number
1258             address = (cpu.AR [n].WORDNO + SIGNEXT15_18 (offset)) & AMASK;
1259 
1260             PNL (cpu.du.Dk_PTR_W[k-1] = address);
1261 #if defined(EIS_PTR)
1262             cpu.du.Dk_PTR_W[k-1] = address;
1263 #else
1264             e -> addr [k - 1].address = address;
1265 #endif
1266             cpu.cu.TSN_PRNO[k-1] = n;
1267             cpu.cu.TSN_VALID[k-1] = 1;
1268             e -> addr [k - 1].SNR = cpu.PR [n].SNR;
1269             e -> addr [k - 1].RNR = max3 (cpu.PR [n].RNR,
1270                                             cpu.TPR.TRR,
1271                                             cpu.PPR.PRR);
1272 
1273             e -> addr [k - 1].mat = viaPR;   // ARs involved
1274 sim_debug (DBG_TRACEEXT, & cpu_dev, "AR n %u k %u\r\n", n, k - 1);
1275           }
1276         else
1277           {
1278             e->addr [k - 1].mat = OperandRead;      // no ARs involved yet
1279 sim_debug (DBG_TRACEEXT, & cpu_dev, "No ARb %u\r\n", k - 1);
1280           }
1281 
1282         // Address modifier for ADDRESS. All register modifiers except du and
1283         // dl may be used. If the ic modifier is used, then ADDRESS is an
1284         // 18-bit offset relative to value of the instruction counter for the
1285         // instruction word. C(REG) is always interpreted as a word offset. REG
1286 
1287         uint reg = opDesc & 017;
1288         // XXX RH03/RJ78 say a,q modifiers are also available here. AL39 says al/ql only
1289         address += getMFReg18 (cpup, reg, false, true, mod_fault); // ID=1: disallow du, allow n,ic
1290         address &= AMASK;
1291 
1292         PNL (cpu.du.Dk_PTR_W[k-1] = address);
1293 
1294 #if defined(EIS_PTR)
1295         cpu.du.Dk_PTR_W[k-1] = address;
1296 #else
1297         e -> addr [k - 1].address = address;
1298 #endif
1299 
1300         // read EIS operand .. this should be an indirectread
1301         e -> op [k - 1] = EISRead (cpup, & e -> addr [k - 1]);
1302     }
1303     else
1304     {
1305           e->addr [k - 1].mat = OperandRead;      // no ARs involved yet
1306 sim_debug (DBG_TRACEEXT, & cpu_dev, "No ARa %u\r\n", k - 1);
1307     }
1308     setupOperandDescriptorCache (cpup, k);
1309 }
1310 
1311 void setupEISoperands (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
1312   {
1313     PNL (cpu.du.POP = 0);
1314     PNL (cpu.du.POL = 0);
1315 
1316 #if defined(EIS_SETUP)
1317     for (int i = 0; i < 3; i ++)
1318       {
1319         if (i < cpu.currentInstruction.info -> ndes)
1320           setupOperandDescriptor (cpup, i + 1);
1321         else
1322           setupOperandDescriptorCache (cpup, i + 1);
1323       }
1324 #endif
1325   }
1326 
1327 static void parseAlphanumericOperandDescriptor (cpu_state_t * cpup, uint k, uint useTA, bool allowDU, fault_ipr_subtype_ *mod_fault)
     /* [previous][next][first][last][top][bottom][index][help] */
1328   {
1329     EISstruct * e = & cpu.currentEISinstruction;
1330     word18 MFk = e -> MF [k - 1];
1331 
1332     PNL (L68_ (if (k == 1)
1333       DU_CYCLE_ANLD1;
1334     else if (k == 2)
1335       DU_CYCLE_ANLD2;
1336     else if (k == 3)
1337       DU_CYCLE_ANSTR;))
1338 
1339     PNL (cpu.du.POP = 1);
1340 
1341     word36 opDesc = e -> op [k - 1];
1342 
1343     word8 ARn_CHAR = 0;
1344     word6 ARn_BITNO = 0;
1345 
1346     word18 address = GETHI (opDesc);
1347 
1348 #if defined(EIS_PTR3)
1349     if (useTA != k)
1350       cpu.du.TAk[k-1] = cpu.du.TAk[useTA-1];
1351     else
1352       cpu.du.TAk[k-1] = getbits36_2 (opDesc, 21);    // type alphanumeric
1353 #else
1354     if (useTA != k)
1355       e -> TA [k - 1] = e -> TA [useTA - 1];
1356     else
1357       e -> TA [k - 1] = getbits36_2 (opDesc, 21);    // type alphanumeric
1358 #endif
1359 
1360 #if defined(PANEL68)
1361     if (k == 1) // Use data from first operand
1362       {
1363         switch (e->TA[0])
1364           {
1365             case CTA9:
1366               cpu.dataMode = 0102; // 9 bit an
1367               cpu.ou.opsz = is_9 >> 12;
1368               break;
1369             case CTA6:
1370               cpu.dataMode = 0042; // 6 bit an
1371               cpu.ou.opsz = is_6 >> 12;
1372               break;
1373             case CTA4:
1374               cpu.dataMode = 0022; // 4 bit an
1375               cpu.ou.opsz = is_4 >> 12;
1376               break;
1377           }
1378       }
1379 #endif
1380 
1381 // 8. Modification of the operand descriptor address begins. This step is
1382 // reached directly from 2 if no indirection is involved. The AR bit of MF1 is
1383 // checked to determine if address register modification is specified.
1384 //
1385 // 9. Address register modification is performed on the operand descriptor as
1386 // described under "Address Modification with Address Registers" above. The
1387 // character and bit positions of the specified address register are used in
1388 // one of two ways, depending on the type of operand descriptor, i.e., whether
1389 // the type is a bit string, a numeric, or an alphanumeric descriptor.
1390 //
1391 // 10. The REG field of MF1 is checked for a legal code. If DU is specified in
1392 // the REG field of MF2 in one of the four multiword instructions (SCD, SCDR,
1393 // SCM, or SCMR) for which DU is legal, the CN field is ignored and the
1394 // character or characters are arranged within the 18 bits of the word address
1395 // portion of the operand descriptor.
1396 //
1397 // 11. The count contained in the register specified by the REG field code is
1398 // appropriately converted and added to the operand address.
1399 //
1400 // 12. The operand is retrieved from the calculated effective address location.
1401 //
1402 
1403     if (MFk & MFkAR)
1404       {
1405         // if MKf contains ar then it Means Y-charn is not the memory address
1406         // of the data but is a reference to a pointer register pointing to the
1407         // data.
1408         word3 n = (word3) getbits18 (address, 0, 3);
1409         CPTUR (cptUsePRn + n);
1410         word18 offset = SIGNEXT15_18 ((word15) address);  // 15-bit signed number
1411         address = (cpu.AR [n].WORDNO + offset) & AMASK;
1412 
1413         ARn_CHAR = GET_AR_CHAR (n); // AR[n].CHAR;
1414         ARn_BITNO = GET_AR_BITNO (n); // AR[n].BITNO;
1415         // An AR BITNO of 9-15 (LAREG) counts as 8, as for bit strings
1416         // (pas.905 L34325); it matters only for 6-bit data (ISOLTS PS905
1417         // mrl: CHAR 1 BITNO 10 started the operand one 6-bit character
1418         // late).  4-bit (BITNO >= 5) and 9-bit (BITNO unused) are the same.
1419         if (ARn_BITNO > 8)
1420           ARn_BITNO = 8;
1421 
1422         cpu.cu.TSN_PRNO[k-1] = n;
1423         cpu.cu.TSN_VALID[k-1] = 1;
1424         e -> addr [k - 1].SNR = cpu.PR [n].SNR;
1425         e -> addr [k - 1].RNR = max3 (cpu.PR [n].RNR, cpu.TPR.TRR, cpu.PPR.PRR);
1426 
1427         e -> addr [k - 1].mat = viaPR;   // ARs involved
1428 sim_debug (DBG_TRACEEXT, & cpu_dev, "AR n %u k %u\r\n", n, k - 1);
1429       }
1430 
1431     PNL (cpu.du.POL = 1);
1432 
1433     uint CN = getbits36_3 (opDesc, 18);    // character number
1434 
1435     sim_debug (DBG_TRACEEXT, & cpu_dev, "initial CN%u %u\r\n", k, CN);
1436 
1437     if (MFk & MFkRL)
1438     {
1439         uint reg = opDesc & 017;
1440 // XXX Handle N too big intelligently....
1441         e -> N [k - 1] = (uint) getMFReg36 (cpup, reg, false, false, mod_fault); // RL=1: disallow du,n,ic
1442 #if defined(EIS_PTR3)
1443         switch (cpu.du.TAk[k-1])
1444 #else
1445         switch (e -> TA [k - 1])
1446 #endif
1447           {
1448             case CTA4:
1449               e -> N [k - 1] &= 017777777; // 22-bits of length
1450               break;
1451 
1452             case CTA6:
1453             case CTA9:
1454               e -> N [k - 1] &= 07777777;  // 21-bits of length.
1455               break;
1456 
1457             default:
1458               L68_ (doFault (FAULT_IPR, fst_ill_proc, "parseAlphanumericOperandDescriptor TA 3");)
1459               // DPS8M
1460               *mod_fault |= FR_ILL_PROC;
1461               break;
1462           }
1463       }
1464     else
1465       e -> N [k - 1] = opDesc & 07777;
1466 
1467     //if (e->N [k - 1] == 0)
1468       //doFault (FAULT_IPR, FR_ILL_PROC, "parseAlphanumericOperandDescriptor N 0");
1469 
1470     sim_debug (DBG_TRACEEXT, & cpu_dev, "N%u %o\r\n", k, e->N[k-1]);
1471 
1472     word36 r = getMFReg36 (cpup, MFk & 017, allowDU, true, mod_fault); // allow du based on instruction, allow n,ic
1473 
1474     if ((MFk & 017) == 4)   // reg == IC ?
1475       {
1476         address += r;
1477         address &= AMASK;
1478         r = 0;
1479       }
1480 
1481     // If seems that the effect address calcs given in AL39 p.6-27 are not
1482     // quite right. E.g. For CTA4/CTN4 because of the 4 "slop" bits you need
1483     // to do 32-bit calcs not 36-bit!
1484     uint effBITNO = 0;
1485     uint effCHAR = 0;
1486     uint effWORDNO = 0;
1487 
1488 #if defined(EIS_PTR3)
1489     switch (cpu.du.TAk[k-1])
1490 #else
1491     switch (e -> TA [k - 1])
1492 #endif
1493       {
1494         case CTA4:
1495           {
1496             // Calculate character number of ARn CHAR and BITNO
1497             // Two 4-bit chars per 9-bit char; BITNO 5-15 is the second
1498             // (for BITNO 0-8 this is (9 * CHAR + BITNO) * 2 / 9; BITNO 9
1499             // from LAREG: PS905 01A)
1500             uint arn_char4 = ARn_CHAR * 2u + (ARn_BITNO >= 5u ? 1u : 0u);
1501             // 8 chars per word plus the number of chars in r, plus the
1502             // number of chars in ARn CHAR/BITNO plus the CN from the operand
1503 // XXX Handle 'r' too big intelligently...
1504             uint nchars = address * 8 + (uint) r + arn_char4 + CN;
1505 
1506             effWORDNO = nchars / 8; // 8 chars/word
1507             effCHAR = nchars % 8; // effCHAR is the 4 bit char number, not
1508                                   // the 9-bit char no
1509             effBITNO = (nchars & 1) ? 5 : 0;
1510 
1511             effWORDNO &= AMASK;
1512 
1513             e -> CN [k - 1] = effCHAR;
1514             e -> WN [k - 1] = effWORDNO;
1515 
1516             sim_debug (DBG_TRACEEXT, & cpu_dev, "CN%d set to %d by CTA4\r\n",
1517                        k, e -> CN [k - 1]);
1518           }
1519           break;
1520 
1521         case CTA6:
1522           if (CN >= 6) {
1523             L68_ (doFault (FAULT_IPR, fst_ill_proc, "parseAlphanumericOperandDescriptor TAn CTA6 CN >= 6");)
1524             // DPS8M
1525             *mod_fault |= FR_ILL_PROC;
1526           }
1527           {
1528           // MVNE's receiving string (operand 3): a 6-bit string's register
1529           // (REG) count is taken in 9-bit characters, not 6-bit ones.
1530           // With 6-bit units, a PS955 random case (MVNE, op3 6-bit with RL
1531           // and an X7 offset of 300465) wrapped to PS955's own code and
1532           // overwrote it; with 9-bit units the output lands in PS955's
1533           // operand buffer, as its other operands do, and PS955 runs
1534           // cleanly.
1535           uint rbits = (k == 3 &&
1536                         cpu.currentInstruction.opcode == 024 &&
1537                         cpu.currentInstruction.opcodeX == 1) ? 9u : 6u;
1538           effBITNO = (9u * ARn_CHAR + rbits * r + ARn_BITNO) % 9u;
1539           effCHAR = ((6u * CN +
1540                       9u * ARn_CHAR +
1541                       rbits * r + ARn_BITNO) % 36u) / 6u;//9;
1542           effWORDNO = (uint) (address +
1543                            (6u * CN +
1544                             9u * ARn_CHAR +
1545                             rbits * r +
1546                             ARn_BITNO) / 36u);
1547           }
1548           effWORDNO &= AMASK;
1549 
1550           e -> CN [k - 1] = effCHAR;   // ??????
1551           e -> WN [k - 1] = effWORDNO;
1552           sim_debug (DBG_TRACEEXT, & cpu_dev, "CN%d set to %d by CTA6\r\n",
1553                      k, e -> CN [k - 1]);
1554           break;
1555 
1556         case CTA9:
1557           if (CN & 01) {
1558             L68_ (doFault(FAULT_IPR, fst_ill_proc, "parseAlphanumericOperandDescriptor CTA9 & CN odd");)
1559             // DPS8M
1560             *mod_fault |= FR_ILL_PROC;
1561           }
1562           CN = (CN >> 1);
1563 
1564           effBITNO = 0;
1565           effCHAR = (CN + ARn_CHAR + r) % 4;
1566           sim_debug (DBG_TRACEEXT, & cpu_dev,
1567                      "effCHAR %d = (CN %d + ARn_CHAR %d + r %"PRId64") %% 4)\r\n",
1568                      effCHAR, CN, ARn_CHAR, r);
1569           // 9-bit data starts at a character: ARn.BITNO is not used (it
1570           // only mattered past 8; BITNO 9 from LAREG: PS905 01A)
1571           effWORDNO = (uint) (address +
1572                            ((9u * CN +
1573                              9u * ARn_CHAR +
1574                              9u * r) / 36u));
1575           effWORDNO &= AMASK;
1576 
1577           e -> CN [k - 1] = effCHAR;   // ??????
1578           e -> WN [k - 1] = effWORDNO;
1579           sim_debug (DBG_TRACEEXT, & cpu_dev, "CN%d set to %d by CTA9\r\n",
1580                      k, e -> CN [k - 1]);
1581           break;
1582 
1583         default:
1584           L68_ (doFault (FAULT_IPR, fst_ill_proc, "parseAlphanumericOperandDescriptor TA1 3");)
1585           // DPS8M
1586           *mod_fault |= FR_ILL_PROC;
1587           break;
1588     }
1589 
1590     EISaddr * a = & e -> addr [k - 1];
1591     PNL (cpu.du.Dk_PTR_W[k-1] = effWORDNO);
1592 #if defined(EIS_PTR)
1593     cpu.du.Dk_PTR_W[k-1] = effWORDNO;
1594 #else
1595     a -> address = effWORDNO;
1596 #endif
1597     a -> cPos= (int) effCHAR;
1598     a -> bPos = (int) effBITNO;
1599 
1600 #if !defined(EIS_PTR3)
1601     // a->_type = eisTA;
1602     a -> TA = (int) e -> TA [k - 1];
1603 #endif
1604   }
1605 
1606 static void parseArgOperandDescriptor (cpu_state_t * cpup, uint k, fault_ipr_subtype_ *mod_fault)
     /* [previous][next][first][last][top][bottom][index][help] */
1607   {
1608     PNL (L68_ (if (k == 1)
1609       DU_CYCLE_NLD1;
1610     else if (k == 2)
1611       DU_CYCLE_NLD2;
1612     else if (k == 3)
1613       DU_CYCLE_GSTR;))
1614 
1615     EISstruct * e = & cpu.currentEISinstruction;
1616     word36 opDesc = e -> op [k - 1];
1617     word18 y = GETHI (opDesc);
1618     word1 yA = GET_A (opDesc);
1619 
1620     uint yREG = opDesc & 0xf;
1621 
1622     word36 r = getMFReg36 (cpup, yREG, false, true, mod_fault); // disallow du, allow n,ic
1623 
1624     word8 ARn_CHAR = 0;
1625     word6 ARn_BITNO = 0;
1626 
1627     PNL (cpu.du.POP = 1);
1628 
1629     if (yA)
1630       {
1631         // if operand contains A (bit-29 set) then it Means Y-char9n is not
1632         // the memory address of the data but is a reference to a pointer
1633         // register pointing to the data.
1634         word3 n = GET_ARN (opDesc);
1635         CPTUR (cptUsePRn + n);
1636         word15 offset = y & MASK15;  // 15-bit signed number
1637         y = (cpu.AR [n].WORDNO + SIGNEXT15_18 (offset)) & AMASK;
1638 
1639         ARn_CHAR = GET_AR_CHAR (n); // AR[n].CHAR;
1640         ARn_BITNO = GET_AR_BITNO (n); // AR[n].BITNO;
1641 
1642         cpu.cu.TSN_PRNO[k-1] = n;
1643         cpu.cu.TSN_VALID[k-1] = 1;
1644         e -> addr [k - 1].SNR = cpu.PR[n].SNR;
1645         e -> addr [k - 1].RNR = max3 (cpu.PR [n].RNR, cpu.TPR.TRR, cpu.PPR.PRR);
1646         e -> addr [k - 1].mat = viaPR;
1647       }
1648 
1649     // A word address: ARn.CHAR and ARn.BITNO don't carry into the word
1650     // (CHAR 3 BITNO 10 from LAREG gave the AR's own word: PS905 SCMR)
1651     (void) ARn_CHAR;
1652     (void) ARn_BITNO;
1653     y += (word18) r;
1654     y &= AMASK;
1655 
1656     PNL (cpu.du.Dk_PTR_W[k-1] = y);
1657 
1658 #if defined(EIS_PTR)
1659     cpu.du.Dk_PTR_W[k-1] = y;
1660 #else
1661     e -> addr [k - 1].address = y;
1662 #endif
1663   }
1664 
1665 static void parseNumericOperandDescriptor (cpu_state_t * cpup, int k, fault_ipr_subtype_ *mod_fault)
     /* [previous][next][first][last][top][bottom][index][help] */
1666 {
1667     PNL (L68_ (if (k == 1)
1668       DU_CYCLE_NLD1;
1669     else if (k == 2)
1670       DU_CYCLE_NLD2;
1671     else if (k == 3)
1672       DU_CYCLE_GSTR;))
1673 
1674     EISstruct * e = & cpu.currentEISinstruction;
1675     word18 MFk = e->MF[k-1];
1676 
1677     PNL (cpu.du.POP = 1);
1678 
1679     word36 opDesc = e->op[k-1];
1680 
1681     word8 ARn_CHAR = 0;
1682     word6 ARn_BITNO = 0;
1683 
1684     word18 address = GETHI(opDesc);
1685     if (MFk & MFkAR)
1686     {
1687         // if MKf contains ar then it Means Y-charn is not the memory address
1688         // of the data but is a reference to a pointer register pointing to the
1689         // data.
1690         word3 n = (word3) getbits18 (address, 0, 3);
1691         CPTUR (cptUsePRn + n);
1692         word15 offset = address & MASK15;  // 15-bit signed number
1693         address = (cpu.AR[n].WORDNO + SIGNEXT15_18(offset)) & AMASK;
1694 
1695         ARn_CHAR = GET_AR_CHAR (n); // AR[n].CHAR;
1696         ARn_BITNO = GET_AR_BITNO (n); // AR[n].BITNO;
1697 
1698         cpu.cu.TSN_PRNO[k-1] = n;
1699         cpu.cu.TSN_VALID[k-1] = 1;
1700         e->addr[k-1].SNR = cpu.PR[n].SNR;
1701         e->addr[k-1].RNR = max3(cpu.PR[n].RNR, cpu.TPR.TRR, cpu.PPR.PRR);
1702 
1703         e->addr[k-1].mat = viaPR;   // ARs involved
1704     }
1705 
1706     PNL (cpu.du.POL = 1);
1707 
1708     word3 CN = getbits36_3 (opDesc, 18);    // character number
1709     e->TN[k-1] = getbits36_1 (opDesc, 21); // type numeric
1710 
1711 #if defined(PANEL68)
1712     if (k == 1)
1713       {
1714         if (e->TN[0])
1715           cpu.dataMode = 0021; // 4 bit numeric
1716         else
1717           cpu.dataMode = 0101; // 9 bit numeric
1718       }
1719 #endif
1720 
1721     e->S[k-1]  = getbits36_2 (opDesc, 22);    // Sign and decimal type of data
1722     e->SF[k-1] = SIGNEXT6_int (getbits36_6 (opDesc, 24));    // Scaling factor.
1723 
1724     // Operand length. If MFk.RL = 0, this field contains the operand length in
1725     // digits. If MFk.RL = 1, it contains the REG code for the register holding
1726     // the operand length and C(REG) is treated as a 0 modulo 64 number. See
1727     // Table 4-1 and EIS modification fields (MF) above for a discussion of
1728     // register codes.
1729 
1730     if (MFk & MFkRL)
1731     {
1732         uint reg = opDesc & 017;
1733         e->N[k-1] = getMFReg18 (cpup,reg, false, false, mod_fault) & 077; // RL=1: disallow du,n,ic
1734     }
1735     else
1736         e->N[k-1] = opDesc & 077;
1737 
1738     sim_debug (DBG_TRACEEXT, & cpu_dev, "parseNumericOperandDescriptor(cpup, ): N%u %0o\r\n", k, e->N[k-1]);
1739 
1740     word36 r = getMFReg36(cpup, MFk & 017, false, true, mod_fault); // disallow du, allow n, ic
1741     if ((MFk & 017) == 4)   // reg == IC ?
1742     {
1743         address += r;
1744         address &= AMASK;
1745         r = 0;
1746     }
1747 
1748 // handled in numeric instructions
1749 
1750 
1751 
1752 
1753 
1754 
1755 
1756 
1757 
1758 
1759 // Causes:
1760 //DBG(662088814)> CPU0 FAULT: Fault 10(012), sub 4294967296(040000000000), dfc N, 'parseNumericOperandDescriptor N=1 S=0|1|2'
1761 //DBG(662088814)> CPU0 FAULT: 00257:004574 bound_process_env_:command_query_+04574
1762 //DBG(662088814)> CPU0 FAULT:       664 end print_question;
1763 //DBG(662088814)> CPU0 FAULT: 00257:004574 4 000100301500 (BTD PR0|100) 000100 301(1) 0 0 0 00
1764 
1765     uint effBITNO = 0;
1766     uint effCHAR = 0;
1767     uint effWORDNO = 0;
1768 
1769     // If seems that the effect address calcs given in AL39 p.6-27 are not
1770     // quite right.
1771     // E.g. For CTA4/CTN4 because of the 4 "slop" bits you need to do 32-bit
1772     // calcs not 36-bit!
1773 
1774     switch (e->TN[k-1])
1775     {
1776         case CTN4:
1777           {
1778             // Calculate character number of ARn CHAR and BITNO
1779             // Two 4-bit chars per 9-bit char; BITNO 5-15 is the second
1780             // (for BITNO 0-8 this is (9 * CHAR + BITNO) * 2 / 9; BITNO 9
1781             // from LAREG: PS905 01A)
1782             uint arn_char4 = ARn_CHAR * 2u + (ARn_BITNO >= 5u ? 1u : 0u);
1783             //// The odd chars start at the 6th bit, not the 5th
1784             //if (bitoffset & 1) // if odd
1785             //  arn_char4 ++;
1786             // 8 chars per word plus the number of chars in r, plus the number of chars in ARn CHAR/BITNO
1787 // XXX Handle 'r' too big intelligently...
1788             uint nchars = (uint) (address * 8u + r + arn_char4 + CN);
1789 
1790             effWORDNO = nchars / 8u; // 8 chars/word
1791             effCHAR = nchars % 8u; // effCHAR is the 4 bit char number, not the 9-bit char no
1792             effBITNO = (nchars & 1u) ? 5u : 0u;
1793             effWORDNO &= AMASK;
1794 
1795             e->CN[k-1] = effCHAR;        // ?????
1796           }
1797           break;
1798 
1799         case CTN9:
1800             if (CN & 1u) {
1801               L68_ (doFault(FAULT_IPR, fst_ill_proc, "parseNumericOperandDescriptor CTA9 & CN odd");)
1802               // DPS8M
1803               *mod_fault |= FR_ILL_PROC;
1804             }
1805             CN = (CN >> 1u) & 03u;
1806 
1807             effBITNO = 0;
1808             effCHAR = ((word36) CN + (word36) ARn_CHAR + r) % 4u;
1809             // ARn.BITNO is not used for 9-bit data (as for CTA9)
1810             effWORDNO = (uint) (address + (9u*CN + 9u*ARn_CHAR + 9u*r) / 36);
1811             effWORDNO &= AMASK;
1812 
1813             e->CN[k-1] = effCHAR;        // ?????
1814 
1815             break;
1816         default:
1817 #if defined(EIS_PTR3)
1818             sim_printf ("parseNumericOperandDescriptor(cpup, ta=%d) How'd we get here 2?\r\n",
1819                         cpu.du.TAk[k-1]);
1820 #else
1821             sim_printf ("parseNumericOperandDescriptor(cpup, ta=%d) How'd we get here 2?\r\n",
1822                         e->TA[k-1]);
1823 #endif
1824             break;
1825     }
1826 
1827     EISaddr *a = &e->addr[k-1];
1828     PNL (cpu.du.Dk_PTR_W[k-1] = effWORDNO);
1829 #if defined(EIS_PTR)
1830     cpu.du.Dk_PTR_W[k-1] = effWORDNO;
1831 #else
1832     a->address = effWORDNO;
1833 #endif
1834     a->cPos = (int) effCHAR;
1835     a->bPos = (int) effBITNO;
1836 
1837     // a->_type = eisTN;
1838     a->TN = (int) e->TN[k-1];
1839 
1840 #if defined(EIS_PTR)
1841     sim_debug (DBG_TRACEEXT, & cpu_dev,
1842                "parseNumericOperandDescriptor(cpup, ): address:%06o cPos:%d bPos:%d N%u %u\r\n",
1843                cpu.du.Dk_PTR_W[k-1], a->cPos, a->bPos, k, e->N[k-1]);
1844 #else
1845     sim_debug (DBG_TRACEEXT, & cpu_dev,
1846                "parseNumericOperandDescriptor(cpup, ): address:%06o cPos:%d bPos:%d N%u %u\r\n",
1847                a->address, a->cPos, a->bPos, k, e->N[k-1]);
1848 #endif
1849 }
1850 
1851 static void parseBitstringOperandDescriptor (cpu_state_t * cpup, int k, fault_ipr_subtype_ *mod_fault)
     /* [previous][next][first][last][top][bottom][index][help] */
1852 {
1853     PNL (L68_ (if (k == 1)
1854       DU_CYCLE_ANLD1;
1855     else if (k == 2)
1856       DU_CYCLE_ANLD2;
1857     else if (k == 3)
1858       DU_CYCLE_ANSTR;))
1859 
1860     EISstruct * e = & cpu.currentEISinstruction;
1861     word18 MFk = e->MF[k-1];
1862     word36 opDesc = e->op[k-1];
1863 
1864 #if defined(PANEL68)
1865     if (k == 1)
1866       cpu.dataMode = 0010; // 1 bit not alpha, not alpha numeric
1867 #endif
1868     word8 ARn_CHAR = 0;
1869     word6 ARn_BITNO = 0;
1870 
1871     PNL (cpu.du.POP = 1);
1872 
1873     word18 address = GETHI(opDesc);
1874     if (MFk & MFkAR)
1875     {
1876         // if MKf contains ar then it Means Y-charn is not the memory address
1877         // of the data but is a reference to a pointer register pointing to the
1878         // data.
1879         word3 n = (word3) getbits18 (address, 0, 3);
1880         CPTUR (cptUsePRn + n);
1881         word15 offset = address & MASK15;  // 15-bit signed number
1882         address = (cpu.AR[n].WORDNO + SIGNEXT15_18(offset)) & AMASK;
1883 
1884         sim_debug (DBG_TRACEEXT, & cpu_dev, "bitstring k %d AR%d\r\n", k, n);
1885 
1886         ARn_CHAR = GET_AR_CHAR (n); // AR[n].CHAR;
1887         ARn_BITNO = GET_AR_BITNO (n); // AR[n].BITNO;
1888         // An AR BITNO of 9-15 (LAREG) counts as 8 (PS905's model of the
1889         // operand address, pas.905 L34325; ISOLTS PS905 csr)
1890         if (ARn_BITNO > 8)
1891           ARn_BITNO = 8;
1892         cpu.cu.TSN_PRNO[k-1] = n;
1893         cpu.cu.TSN_VALID[k-1] = 1;
1894         e->addr[k-1].SNR = cpu.PR[n].SNR;
1895         e->addr[k-1].RNR = max3(cpu.PR[n].RNR, cpu.TPR.TRR, cpu.PPR.PRR);
1896         e->addr[k-1].mat = viaPR;   // ARs involved
1897     }
1898     PNL (cpu.du.POL = 1);
1899 
1900     //Operand length. If MFk.RL = 0, this field contains the string length of
1901     //the operand. If MFk.RL = 1, this field contains the code for a register
1902     //holding the operand string length. See Table 4-1 and EIS modification
1903     //fields (MF) above for a discussion of register codes.
1904     if (MFk & MFkRL)
1905     {
1906         uint reg = opDesc & 017;
1907         e->N[k-1] = getMFReg36(cpup, reg, false, false, mod_fault) & 077777777;  // RL=1: disallow du,n,ic
1908         sim_debug (DBG_TRACEEXT, & cpu_dev, "bitstring k %d RL reg %u val %"PRIo64"\r\n", k, reg, (word36)e->N[k-1]);
1909     }
1910     else
1911     {
1912         e ->N[k-1] = opDesc & 07777;
1913     }
1914 
1915     sim_debug (DBG_TRACEEXT, & cpu_dev, "bitstring k %d opdesc %012"PRIo64"\r\n", k, opDesc);
1916     sim_debug (DBG_TRACEEXT, & cpu_dev, "N%u %u\r\n", k, e->N[k-1]);
1917 
1918     word4 B = getbits36_4(opDesc, 20);    // bit# from descriptor
1919     word2 C = getbits36_2 (opDesc, 18);     // char# from descriptor
1920 
1921     if (B >= 9) {
1922       L68_ (doFault (FAULT_IPR, fst_ill_proc, "parseBitstringOperandDescriptor B >= 9");)
1923       // DPS8M
1924       *mod_fault |= FR_ILL_PROC;
1925     }
1926 
1927     word36 r = getMFReg36(cpup, MFk & 017, false, true, mod_fault);  // disallow du, allow n,ic
1928     if ((MFk & 017) == 4)   // reg == IC ?
1929     {
1930         // If reg == IC, then R is in words, not bits.
1931         //r *= 36;
1932         address += r;
1933         address &= AMASK;
1934         r = 0;
1935     }
1936 
1937     uint effBITNO = (9u*ARn_CHAR + r + ARn_BITNO + B + 9u*C) % 9u;
1938     uint effCHAR = ((9u*ARn_CHAR + r + ARn_BITNO + B + 9u*C) % 36u) / 9u;
1939     uint effWORDNO = (uint) (address + (9u*ARn_CHAR + r + ARn_BITNO + B + 9u*C) / 36u);
1940     effWORDNO &= AMASK;
1941 
1942     e->B[k-1] = effBITNO;
1943     e->C[k-1] = effCHAR;
1944 
1945     EISaddr *a = &e->addr[k-1];
1946     PNL (cpu.du.Dk_PTR_W[k-1] = effWORDNO);
1947 #if defined(EIS_PTR)
1948     cpu.du.Dk_PTR_W[k-1] = effWORDNO;
1949 #else
1950     a->address = effWORDNO;
1951 #endif
1952     a->cPos = (int) effCHAR;
1953     a->bPos = (int) effBITNO;
1954 }
1955 
1956 static void cleanupOperandDescriptor (cpu_state_t * cpup, int k)
     /* [previous][next][first][last][top][bottom][index][help] */
1957   {
1958     EISstruct * e = & cpu.currentEISinstruction;
1959     if (e -> addr [k - 1].cacheValid && e -> addr [k - 1].cacheDirty)
1960       {
1961         EISWriteCache(cpup, & e -> addr [k - 1]);
1962       }
1963     e -> addr [k - 1].cacheDirty = false;
1964   }
1965 
1966 // T&D: the EIS instructions that can be interrupted part way (PM842
1967 // tests each with an interrupt and with timer runouts).
1968 bool tandd_eis_interruptible (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
1969   {
1970     DCDstruct * i = & cpu.currentInstruction;
1971     if (! i -> opcodeX)
1972       return false;
1973     switch (i -> opcode)
1974       {
1975         case 0100: // mlr
1976         case 0101: // mrl
1977         case 0160: // mvt
1978         case 0106: // cmpc
1979         case 0120: // scd
1980         case 0121: // scdr
1981         case 0124: // scm
1982         case 0125: // scmr
1983         case 0164: // tct
1984         case 0165: // tctr
1985         case 0060: // csl
1986         case 0061: // csr
1987         case 0064: // sztl
1988         case 0065: // sztr
1989         case 0066: // cmpb
1990           return true;
1991       }
1992     return false;
1993   }
1994 
1995 // characters per word for operand type ta
1996 #define TDPER(ta) ((ta) == CTA6 ? 6u : (ta) == CTA4 ? 8u : 4u)
1997 
1998 // T&D: called before an interruptible EIS instruction processes unit
1999 // (character or bit) 'unit'. Every two words' worth of units (perWord
2000 // units per word) costs one cycle of TR time (PM842 allows 1 to 9 timer
2001 // runouts in a 96 character mrl with TR = 1); at those points, after
2002 // the first unit of this execution, the instruction samples group 7
2003 // faults and interrupts unless it has the inhibit bit. One that is present stops it with MIF
2004 // on, the IC at the instruction and CHTALLY at 'unit'; executed again
2005 // with MIF on it carries on from there (PM842).
2006 void tandd_eis_sample (cpu_state_t * cpup, uint unit, uint perWord)
     /* [previous][next][first][last][top][bottom][index][help] */
2007   {
2008     if (! cpu.tweaks.tandd_mode || cpu.currentInstruction.i ||
2009         unit <= cpu.tanddEISstart || unit % (2 * perWord))
2010       return;
2011     if (++ cpu.rTRlsb >= 4)
2012       {
2013         cpu.rTRlsb = 0;
2014         cpu.rTR --;
2015         cpu.lufTicks ++;
2016         cpu.tanddTRTicks ++;
2017       }
2018     if (cpu.rTR & ~MASK27)
2019       {
2020         cpu.rTR &= MASK27;
2021         if (cpu.tweaks.tro_enable)
2022           setG7fault (current_running_cpu_idx, FAULT_TRO);
2023       }
2024     advanceG7Faults (cpup);
2025     bool noCheckTR = is_priv_mode (cpup);
2026     // Not the L68 floating faults (OC, CU-HR overflow, address traps):
2027     // they occur only between instructions, "after completion of the next
2028     // odd instruction" (PM842 01C: a CU-HR trap pair inside an MLR)
2029     bool g7 = noCheckTR ? (cpu.g7Faults & ~(1u << FAULT_TRO)) != 0
2030                         : cpu.g7Faults != 0;
2031     if (! g7 && ! sample_interrupts (cpup))
2032       return;
2033     for (int k = 1; k <= 3; k ++)
2034       cleanupOperandDescriptor (cpup, k);
2035     cpu.du.CHTALLY = unit;
2036     SC_I_MIF (1);
2037     if (g7)
2038       doG7Fault (cpup, ! noCheckTR);
2039     cpu.interrupt_flag = true;
2040     cpu.cycle = INTERRUPT_cycle;
2041     longjmp (cpu.jmpMain, JMP_REENTRY);
2042   }
2043 
2044 // For a4bd/s4bd, the world is made of 32 bit words, so the address space
2045 // is 2^18 * 32 bits
2046 #define n4bits (1 << 23)
2047 // For a4bd/s4bd, the world is made of 8 4-bitcharacter words, so the address space
2048 // is 2^18 * 8 characters
2049 #define n4chars (1 << 21)
2050 // For axbd/sxbd, the world is made of 36 bits words, so the address space
2051 // is 2^18 * 36 bits
2052 #define nxbits ((1 << 18) * 36)
2053 
2054 // 2 * (s->BITNO / 9) + (s->BITNO % 9) / 4;
2055 static unsigned int cntFromBit[36] = {
2056     0, 0, 0, 0, 0, 1, 1, 1, 1,
2057     2, 2, 2, 2, 2, 3, 3, 3, 3,
2058     4, 4, 4, 4, 4, 5, 5, 5, 5,
2059     6, 6, 6, 6, 6, 7, 7, 7, 7
2060 };
2061 
2062 static word6 bitFromCnt[8] = {1, 5, 10, 14, 19, 23, 28, 32};
2063 
2064 void a4bd (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
2065   {
2066     // 8 4-bit characters/word
2067 
2068     uint ARn = GET_ARN (cpu.cu.IWB);
2069     CPTUR (cptUsePRn + ARn);
2070     int32_t address = SIGNEXT15_32 (GET_OFFSET (cpu.cu.IWB));
2071 //if (current_running_cpu_idx)
2072 //sim_printf ("a4bd address %o %d.\r\n", address, address);
2073 
2074     word4 reg = GET_TD (cpu.cu.IWB); // 4-bit register modification (None except
2075                                      // au, qu, al, ql, xn)
2076     // r is the count of 4bit characters
2077     word36 ur = getCrAR (cpup, reg);
2078     int32 r = SIGNEXT22_32 ((word22) ur);
2079 //if (current_running_cpu_idx)
2080 //sim_printf ("a4bd r %o %d.\r\n", r, r);
2081 
2082     uint augend = 0; // in 4bit characters
2083     if (GET_A (cpu.cu.IWB))
2084        {
2085 //if (current_running_cpu_idx)
2086 //sim_printf ("a4bd AR%d WORDNO %o %d. CHAR %o BITNO %o\r\n",
2087 //            cpu.AR[ARn].WORDNO, cpu.AR[ARn].WORDNO, cpu.AR[ARn].WORDNO,
2088 //            cpu.AR[ARn].CHAR, cpu.AR[ARn].BITNO);
2089 
2090          //augend = cpu.AR[ARn].WORDNO * 32u + cntFromBit [GET_AR_BITNO (ARn)];
2091          // force to 4 bit character boundary
2092          //augend = augend & ~3;
2093          //augend = cpu.AR[ARn].WORDNO * 8 + cpu.AR[ARn].CHAR * 2;
2094          augend = cpu.AR[ARn].WORDNO * 8u + GET_AR_CHAR (ARn) * 2u;
2095 
2096          //if (cpu.AR[ARn].BITNO >= 5)
2097          if (GET_AR_BITNO (ARn) >= 5u)
2098            augend ++;
2099        }
2100 
2101 //if (current_running_cpu_idx)
2102 //sim_printf ("a4bd augend %o %d.\r\n", augend, augend);
2103 
2104     int32_t addend = address * 8 + r;  // in characters
2105 
2106 //if (current_running_cpu_idx)
2107 //sim_printf ("a4bd addend %o %d.\r\n", addend, addend);
2108 
2109     int32_t sum = (int32_t) augend + addend;
2110 //if (current_running_cpu_idx)
2111 //sim_printf ("a4bd sum %o %d.\r\n", sum, sum);
2112 
2113     // Handle over/under flow
2114     while (sum < 0)
2115       sum += n4chars;
2116     sum = sum % n4chars;
2117 //if (current_running_cpu_idx)
2118 //sim_printf ("a4bd sum %o %d.\r\n", sum, sum);
2119 
2120     cpu.AR[ARn].WORDNO = (word18) (sum / 8) & AMASK;
2121 //if (current_running_cpu_idx)
2122 //sim_printf ("a4bd WORDNO %o %d.\r\n", cpu.AR[ARn].WORDNO, cpu.AR[ARn].WORDNO);
2123 
2124 //    // 0aaaabbbb0ccccdddd0eeeeffff0gggghhhh
2125 //    //             111111 11112222 22222233
2126 //    //  01234567 89012345 67890123 45678901   // 4 bit notation offset
2127 //    static int tab [32] = { 1,  2,  3,  4,  5,  6,  7,  8,
2128 //                           10, 11, 12, 13, 14, 15, 16, 17,
2129 //                           19, 20, 21, 22, 23, 24, 25, 26,
2130 //                           28, 29, 30, 31, 32, 33, 34, 35};
2131 //
2132     //uint bitno = sum % 32;
2133 //    AR [ARn].BITNO = tab [bitno];
2134     //cpu.AR [ARn].BITNO = bitFromCnt[bitno % 8];
2135     //SET_PR_BITNO (ARn, bitFromCnt[bitno % 8]);
2136     uint char4no = (uint) (sum % 8);
2137 //if (current_running_cpu_idx)
2138 //sim_printf ("a4bd char4no %d.\r\n", char4no);
2139 
2140     SET_AR_CHAR_BITNO (ARn, (word2) (char4no / 2), (char4no % 2) ? 5 : 0);
2141 #if defined(TESTING)
2142     HDBGRegARW (ARn, "a4bd");
2143 #endif
2144 //if (current_running_cpu_idx)
2145 //sim_printf ("a4bd CHAR %o %d.\r\n", cpu.AR[ARn].CHAR, cpu.AR[ARn].CHAR);
2146 //if (current_running_cpu_idx)
2147 //sim_printf ("a4bd BITNO %o %d.\r\n", cpu.AR[ARn].BITNO, cpu.AR[ARn].BITNO);
2148   }
2149 
2150 void s4bd (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
2151   {
2152     uint ARn = GET_ARN (cpu.cu.IWB);
2153     CPTUR (cptUsePRn + ARn);
2154     int32_t address = SIGNEXT15_32 (GET_OFFSET (cpu.cu.IWB));
2155     word4 reg = GET_TD (cpu.cu.IWB); // 4-bit register modification (None except
2156                                   // au, qu, al, ql, xn)
2157     // r is the count of characters
2158     word36 ur = getCrAR (cpup, reg);
2159     int32 r = SIGNEXT22_32 ((word22) ur);
2160 
2161     uint minuend = 0;
2162     if (GET_A (cpu.cu.IWB))
2163        {
2164          //minuend = cpu.AR [ARn].WORDNO * 32 + cntFromBit [GET_PR_BITNO (ARn)];
2165          minuend = cpu.AR [ARn].WORDNO * 32 + cntFromBit [GET_AR_CHAR (ARn) * 9 + GET_AR_BITNO (ARn)];
2166          // force to 4 bit character boundary
2167          minuend = minuend & (unsigned int) ~3;
2168        }
2169     int32_t subtractend = address * 32 + r * 4;
2170     int32_t difference = (int32_t) minuend - subtractend;
2171 
2172     // Handle over/under flow
2173     while (difference < 0)
2174       difference += n4bits;
2175     difference = difference % n4bits;
2176 
2177     cpu.AR [ARn].WORDNO = (word18) (difference / 32) & AMASK;
2178 
2179 //    // 0aaaabbbb0ccccdddd0eeeeffff0gggghhhh
2180 //    //             111111 11112222 22222233
2181 //    //  01234567 89012345 67890123 45678901   // 4 bit notation offset
2182 //    static int tab [32] = { 1,  2,  3,  4,  5,  6,  7,  8,
2183 //                       10, 11, 12, 13, 14, 15, 16, 17,
2184 //                       19, 20, 21, 22, 23, 24, 25, 26,
2185 //                       28, 29, 30, 31, 32, 33, 34, 35};
2186 //
2187 
2188     uint bitno = (uint) (difference % 32);
2189 //    cpu.AR [ARn].BITNO = tab [bitno];
2190     // SET_PR_BITNO (ARn, bitFromCnt[bitno % 8]);
2191     SET_AR_CHAR_BITNO (ARn, bitFromCnt[bitno % 8] / 9, bitFromCnt[bitno % 8] % 9);
2192 #if defined(TESTING)
2193     HDBGRegARW (ARn, "s4bd");
2194 #endif
2195   }
2196 
2197 void axbd (cpu_state_t * cpup, uint sz)
     /* [previous][next][first][last][top][bottom][index][help] */
2198   {
2199     uint ARn = GET_ARN (cpu.cu.IWB);
2200     CPTUR (cptUsePRn + ARn);
2201     int32_t address = SIGNEXT15_32 (GET_OFFSET (cpu.cu.IWB));
2202     word6 reg = GET_TD (cpu.cu.IWB); // 4-bit register modification (None except
2203                                      // au, qu, al, ql, xn)
2204     // r is the count of characters
2205     word36 rcnt = getCrAR (cpup, reg);
2206     int32_t r;
2207 
2208     if (sz == 1)
2209       r = SIGNEXT24_32 ((word24) rcnt);
2210     else if (sz == 4)
2211       r = SIGNEXT22_32 ((word22) rcnt);
2212     else if (sz == 6)
2213       r = SIGNEXT21_32 ((word21) rcnt);
2214     else if (sz == 9)
2215       r = SIGNEXT21_32 ((word21) rcnt);
2216     else // if (sz == 36)
2217       r = SIGNEXT18_32 ((word18) rcnt);
2218 
2219     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2220                "axbd sz %d ARn 0%o address 0%o reg 0%o r 0%o\r\n",
2221                sz, ARn, address, reg, r);
2222 
2223     uint augend = 0;
2224     if (GET_A (cpu.cu.IWB))
2225       {
2226        sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2227                   "axbd ARn %d WORDNO %o CHAR %o BITNO %0o %d.\r\n",
2228                   ARn, cpu.PAR[ARn].WORDNO, GET_AR_CHAR (ARn),
2229                   GET_AR_BITNO (ARn), GET_AR_BITNO (ARn));
2230        augend = cpu.AR[ARn].WORDNO * 36u + GET_AR_CHAR (ARn) * 9u + GET_AR_BITNO (ARn);
2231       }
2232     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2233                "axbd augend 0%o\r\n",
2234                augend);
2235     // force to character boundary
2236     //if (sz == 9 || sz == 36 || GET_A (cpu.cu.IWB))
2237     if (sz == 9 || GET_A (cpu.cu.IWB))
2238       {
2239         augend = (augend / sz) * sz;
2240         sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2241                    "axbd force augend 0%o\r\n",
2242                    augend);
2243       }
2244 // If sz == 9, this is an a9bd instruction; ISOLTS says that r is in characters, not bits.
2245 // wow. That breaks the boot bad.
2246 //    if (sz == 9)
2247 //      {
2248 //        r *= 9;
2249 //if (current_running_cpu_idx)
2250 //sim_printf ("axbd force chars 0%o %d. bits\r\n", r, r);
2251 //      }
2252 
2253     int32_t addend = address * 36 + r * (int32_t) sz;
2254     int32_t sum = (int32_t) augend + addend;
2255 
2256     // Handle over/under flow
2257     while (sum < 0)
2258       sum += nxbits;
2259     sum = sum % nxbits;
2260 
2261     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev, "axbd augend 0%o addend 0%o sum 0%o\r\n", augend, addend, sum);
2262 
2263     cpu.AR [ARn].WORDNO = (word18) (sum / 36) & AMASK;
2264     //SET_PR_BITNO (ARn, sum % 36);
2265 #if defined(TESTING)
2266     HDBGRegARR (ARn, "axbd");
2267 #endif
2268     SET_AR_CHAR_BITNO (ARn, (word2)((sum % 36) / 9), (word2)(sum % 9));
2269 #if defined(TESTING)
2270     HDBGRegARW (ARn, "axbd");
2271 #endif
2272   }
2273 
2274 
2275 void abd (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
2276   {
2277     uint ARn = GET_ARN (cpu.cu.IWB);
2278     CPTUR (cptUsePRn + ARn);
2279 
2280     word18 address = SIGNEXT15_18 (GET_OFFSET (cpu.cu.IWB));
2281 //if (current_running_cpu_idx)
2282 //sim_printf ("address %o\r\n", address);
2283     word4 reg = (word4) GET_TD (cpu.cu.IWB);
2284     // r is the count of bits (0 - 2^18 * 36 -1); 24 bits
2285     word24 r = getCrAR (cpup, (word4) reg) & MASK24;
2286 //if (current_running_cpu_idx)
2287 //sim_printf ("r 0%o %d.\r\n", r, r);
2288 //if (current_running_cpu_idx)
2289 //sim_printf ("abd WORDNO 0%o %d. CHAR %o BITNO 0%o %d.\r\n",
2290 //            cpu.AR[ARn].WORDNO, cpu.AR[ARn].WORDNO, cpu.AR[ARn].CHAR,
2291 //            cpu.AR[ARn].BITNO, cpu.AR[ARn].BITNO);
2292 
2293     //if (cpu.AR[ARn].BITNO > 8)
2294       //cpu.AR[ARn].BITNO = 8;
2295     if (GET_AR_BITNO (ARn) > 8)
2296       SET_AR_CHAR_BITNO (ARn, GET_AR_CHAR (ARn), 8);
2297 
2298     if (GET_A (cpu.cu.IWB))
2299       {
2300 //if (current_running_cpu_idx)
2301 //sim_printf ("A 1\r\n");
2302         //word24 bits = 9 * cpu.AR[ARn].CHAR + cpu.AR[ARn].BITNO + r;
2303         word24 bits = 9u * GET_AR_CHAR (ARn) + GET_AR_BITNO (ARn) + r;
2304 //if (current_running_cpu_idx)
2305 //sim_printf ("bits 0%o %d.\r\n", bits, bits);
2306         cpu.AR[ARn].WORDNO = (cpu.AR[ARn].WORDNO + address +
2307                               bits / 36) & MASK18;
2308         if (r % 36)
2309           {
2310             //cpu.AR[ARn].CHAR = (bits % 36) / 9;
2311             //cpu.AR[ARn].BITNO = bits % 9;
2312             SET_AR_CHAR_BITNO (ARn, (bits % 36) / 9,
2313                                     bits % 9);
2314           }
2315       }
2316     else
2317       {
2318 //if (current_running_cpu_idx)
2319 //sim_printf ("A 0\r\n");
2320         cpu.AR[ARn].WORDNO = (address + r / 36) & MASK18;
2321         if (r % 36)
2322           {
2323             //cpu.AR[ARn].CHAR = (r % 36) / 9;
2324             //cpu.AR[ARn].BITNO = r % 9;
2325             SET_AR_CHAR_BITNO (ARn, (r % 36) / 9,
2326                                     r % 9);
2327           }
2328       }
2329 # if defined(TESTING)
2330     HDBGRegARW (ARn, "abd");
2331 # endif
2332 //if (current_running_cpu_idx)
2333 //sim_printf ("abd WORDNO 0%o %d. CHAR %o BITNO 0%o %d.\r\n",
2334 //            cpu.AR[ARn].WORDNO, cpu.AR[ARn].WORDNO, cpu.AR[ARn].CHAR,
2335 //            cpu.AR[ARn].BITNO, cpu.AR[ARn].BITNO);
2336   }
2337 
2338 
2339 
2340 
2341 
2342 
2343 
2344 
2345 
2346 
2347 
2348 
2349 
2350 
2351 
2352 
2353 
2354 
2355 
2356 
2357 
2358 
2359 
2360 
2361 
2362 
2363 
2364 
2365 
2366 
2367 
2368 
2369 
2370 
2371 
2372 
2373 
2374 
2375 
2376 
2377 
2378 
2379 
2380 
2381 
2382 
2383 
2384 
2385 
2386 
2387 
2388 
2389 
2390 
2391 
2392 
2393 
2394 
2395 
2396 
2397 
2398 
2399 
2400 
2401 
2402 
2403 
2404 
2405 
2406 
2407 
2408 
2409 
2410 
2411 
2412 
2413 
2414 
2415 
2416 
2417 
2418 
2419 
2420 
2421 
2422 
2423 
2424 
2425 
2426 
2427 
2428 
2429 
2430 
2431 
2432 
2433 
2434 
2435 
2436 
2437 
2438 
2439 
2440 
2441 
2442 
2443 
2444 
2445 
2446 
2447 
2448 
2449 
2450 
2451 
2452 
2453 
2454 
2455 
2456 
2457 
2458 
2459 
2460 
2461 
2462 
2463 
2464 
2465 
2466 
2467 
2468 
2469 
2470 
2471 
2472 
2473 
2474 
2475 
2476 
2477 
2478 
2479 
2480 
2481 
2482 
2483 
2484 
2485 void awd (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
2486   {
2487     uint ARn = GET_ARN (cpu.cu.IWB);
2488     CPTUR (cptUsePRn + ARn);
2489     int32_t address = SIGNEXT15_32 (GET_OFFSET (cpu.cu.IWB));
2490     // 4-bit register modification (None except
2491     // au, qu, al, ql, xn)
2492     word4 reg = (word4) GET_TD (cpu.cu.IWB);
2493     // r is the count of characters
2494 // XXX This code is assuming that 'r' has 18 bits of data....
2495     int32_t r = (int32_t) (getCrAR (cpup, reg) & MASK18);
2496     r = SIGNEXT18_32 ((word18) r);
2497 
2498     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2499                "awd ARn 0%o address 0%o reg 0%o r 0%o\r\n",
2500                ARn, address, reg, r);
2501 
2502     uint augend = 0;
2503     if (GET_A (cpu.cu.IWB))
2504       {
2505        sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2506                   "awd ARn %d WORDNO %o CHAR %o BITNO %0o %d.\r\n",
2507                   ARn, cpu.PAR[ARn].WORDNO, GET_AR_CHAR (ARn),
2508                   GET_AR_BITNO (ARn), GET_AR_BITNO (ARn));
2509 
2510        //augend = cpu.AR [ARn].WORDNO * 36 + GET_AR_CHAR (ARn) * 9 + GET_AR_BITNO (ARn);
2511        augend = cpu.AR [ARn].WORDNO;
2512       }
2513 
2514     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2515                "awd augend 0%o\r\n",
2516                augend);
2517 
2518     int32_t addend = address + r;
2519     int32_t sum = (int32_t) augend + addend;
2520 
2521     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2522                "awd augend 0%o addend 0%o sum 0%o\r\n",
2523                augend, addend, sum);
2524 
2525     cpu.AR[ARn].WORDNO = (word18) sum & AMASK;
2526     SET_AR_CHAR_BITNO (ARn, 0, 0);
2527 #if defined(TESTING)
2528     HDBGRegARW (ARn, "awd");
2529 #endif
2530   }
2531 
2532 void sbd (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
2533   {
2534     uint ARn = GET_ARN (cpu.cu.IWB);
2535 
2536     word18 address = SIGNEXT15_18 (GET_OFFSET (cpu.cu.IWB));
2537     word4 reg = (word4) GET_TD (cpu.cu.IWB);
2538     // r is the count of bits (0 - 2^18 * 36 -1); 24 bits
2539     word24 r = getCrAR (cpup, (word4) reg) & MASK24;
2540     if (GET_AR_BITNO (ARn) > 8)
2541       SET_AR_CHAR_BITNO (ARn, GET_AR_CHAR (ARn), 8);
2542 
2543     if (GET_A (cpu.cu.IWB))
2544       {
2545         word24 bits = 9u * GET_AR_CHAR (ARn) + GET_AR_BITNO (ARn) - r;
2546         cpu.AR[ARn].WORDNO = (cpu.AR[ARn].WORDNO -
2547                              address + bits / 36) & MASK18;
2548         if (r % 36)
2549           {
2550             SET_AR_CHAR_BITNO (ARn, (- ((bits % 36) / 9)) & MASK2,
2551                                     (- (bits % 9)) & MASK4);
2552           }
2553       }
2554     else
2555       {
2556         cpu.AR[ARn].WORDNO = (- (address + r / 36)) & MASK18;
2557         if (r % 36)
2558           {
2559             SET_AR_CHAR_BITNO (ARn, (- ((r % 36) / 9)) & MASK2,
2560                                     (- (r % 9)) & MASK4);
2561           }
2562       }
2563 #if defined(TESTING)
2564     HDBGRegARW (ARn, "sbd");
2565 #endif
2566   }
2567 
2568 void swd (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
2569   {
2570     uint ARn = GET_ARN (cpu.cu.IWB);
2571     CPTUR (cptUsePRn + ARn);
2572     int32_t address = SIGNEXT15_32 (GET_OFFSET (cpu.cu.IWB));
2573     // 4-bit register modification (None except
2574     // au, qu, al, ql, xn)
2575     word4 reg = (word4) GET_TD (cpu.cu.IWB);
2576     // r is the count of characters
2577 // XXX This code is assuming that 'r' has 18 bits of data....
2578     int32_t r = (int32_t) (getCrAR (cpup, reg) & MASK18);
2579     r = SIGNEXT18_32 ((word18) r);
2580 
2581     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2582                "swd ARn 0%o address 0%o reg 0%o r 0%o\r\n",
2583                ARn, address, reg, r);
2584 
2585     uint minued = 0;
2586     if (GET_A (cpu.cu.IWB))
2587       {
2588        sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2589                   "swd ARn %d WORDNO %o CHAR %o BITNO %0o %d.\r\n",
2590                   ARn, cpu.PAR[ARn].WORDNO, GET_AR_CHAR (ARn),
2591                   GET_AR_BITNO (ARn), GET_AR_BITNO (ARn));
2592 
2593        //minued = cpu.AR [ARn].WORDNO * 36 + GET_AR_BITNO (ARn);
2594        minued = cpu.AR [ARn].WORDNO;
2595       }
2596 
2597     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2598                "swd minued 0%o\r\n", minued);
2599 
2600     int32_t subtractend = address + r;
2601     int32_t difference = (int32_t) minued - subtractend;
2602 
2603     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev,
2604                "swd minued 0%o subtractend 0%o difference 0%o\r\n",
2605                minued, subtractend, difference);
2606 
2607     cpu.AR [ARn].WORDNO = (word18) difference & AMASK;
2608     SET_AR_CHAR_BITNO (ARn, 0, 0);
2609 #if defined(TESTING)
2610     HDBGRegARW (ARn, "swd");
2611 #endif
2612   }
2613 
2614 void s9bd (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
2615   {
2616     uint ARn = GET_ARN (cpu.cu.IWB);
2617     CPTUR (cptUsePRn + ARn);
2618     word18 address = SIGNEXT15_18 (GET_OFFSET (cpu.cu.IWB));
2619     // 4-bit register modification (None except
2620     // au, qu, al, ql, xn)
2621     word4 reg = (word4) GET_TD (cpu.cu.IWB);
2622 
2623     // r is the count of 9-bit characters
2624     word21 r = getCrAR (cpup, reg) & MASK21;;
2625 
2626     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev, "s9bd r 0%o\r\n", r);
2627 
2628     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev, "s9bd ARn 0%o address 0%o reg 0%o r 0%o\r\n", ARn, address, reg, r);
2629 
2630     if (GET_A (cpu.cu.IWB))
2631       {
2632         //cpu.AR[ARn].WORDNO = (cpu.AR[ARn].WORDNO -
2633         //                      address +
2634         //                      (cpu.AR[ARn].CHAR - r) / 4) & MASK18;
2635         cpu.AR[ARn].WORDNO = (cpu.AR[ARn].WORDNO -
2636                               address +
2637                               (GET_AR_CHAR (ARn) - r) / 4) & MASK18;
2638         //if (r % 36)
2639           //{
2640             //cpu.AR[ARn].CHAR = ((cpu.AR[ARn].CHAR - r) % 4) & MASK2;
2641             //cpu.AR[ARn].CHAR = (cpu.AR[ARn].CHAR - r)  & MASK2;
2642             SET_AR_CHAR_BITNO (ARn, (GET_AR_CHAR (ARn) - r)  & MASK2, 0);
2643           //}
2644       }
2645     else
2646       {
2647         cpu.AR[ARn].WORDNO = (- (address + (r + 3) / 4)) & MASK18;
2648         //if (r % 36)
2649           //{
2650             //cpu.AR[ARn].CHAR = (-r) & MASK2;
2651             SET_AR_CHAR_BITNO (ARn, (-r) & MASK2, 0);
2652           //}
2653       }
2654     //cpu.AR[ARn].BITNO = 0;
2655 #if defined(TESTING)
2656     HDBGRegARW (ARn, "s9bd");
2657 #endif
2658 //if (current_running_cpu_idx)
2659 //sim_printf ("s9bd WORDNO 0%o %d. CHAR %o BITNO 0%o %d.\r\n",
2660 //            cpu.AR[ARn].WORDNO, cpu.AR[ARn].WORDNO, cpu.AR[ARn].CHAR,
2661 //            cpu.AR[ARn].BITNO, cpu.AR[ARn].BITNO);
2662   }
2663 
2664 //
2665 // Address Register arithmetic
2666 //
2667 // This code handles Address Register arithmetic
2668 //
2669 // asxbd (  ,       )
2670 // ABD     1   false
2671 // A4BD    4   false
2672 // A6BD    6   false
2673 // A9BD    9   false
2674 // AWD    36   false
2675 // SBD     1   true
2676 // S4BD    4   true
2677 // S6BD    6   true
2678 // S9BD    9   true
2679 // SWD    36   true
2680 //
2681 
2682 // The general approach is do all of the math as unsigned number of bits,
2683 // modulo 2^18 * 36 (the number of bits in a segment).
2684 //
2685 // To handle subtraction underflow, a preemptive borrow is done if the
2686 // the operation will underflow.
2687 //
2688 // Notes:
2689 //   According to ISOLTS 805, WORDNO is unsigned; this disagrees with AL-39
2690 
2691 void asxbd (cpu_state_t * cpup, uint sz, bool sub)
     /* [previous][next][first][last][top][bottom][index][help] */
2692   {
2693     // Map charno:bitno to bit offset for 4 bit char set
2694     uint map4 [64] =
2695       {      // 9-bit    4-bit
2696           0, // 0  0      0
2697           0, // 0  1      0
2698           0, // 0  2      0
2699           0, // 0  3      0
2700           0, // 0  4      0
2701           5, // 0  5      1
2702           5, // 0  6      1
2703           5, // 0  7      1
2704           5, // 0  8      1
2705           5, // 0  9  ill     guess
2706           5, // 0 10  ill     guess
2707           5, // 0 11  ill     guess
2708           5, // 0 12  ill     guess
2709           5, // 0 13  ill     guess
2710           5, // 0 14  ill     guess
2711           5, // 0 15  ill     guess
2712           9, // 1  0      2
2713           9, // 1  1      2
2714           9, // 1  2      2
2715           9, // 1  3      2
2716           9, // 1  4      2
2717          14, // 1  5      3
2718          14, // 1  6      3
2719          14, // 1  7      3
2720          14, // 1  8      3
2721          14, // 1  9  ill     guess
2722          14, // 1 10  ill ISOLTS 805 loop point 010226 sxbd test no 28 (4bit)
2723          14, // 1 11  ill     guess
2724          14, // 1 12  ill     guess
2725          14, // 1 13  ill     guess
2726          14, // 1 14  ill     guess
2727          14, // 1 15  ill     guess
2728          18, // 2  0      4
2729          18, // 2  1      4
2730          18, // 2  2      4
2731          18, // 2  3      4
2732          18, // 2  4      4
2733          23, // 2  5      5
2734          23, // 2  6      5
2735          23, // 2  7      5
2736          23, // 2  8      5
2737          23, // 2  9  ill     guess
2738          23, // 2 10  ill     guess
2739          23, // 2 11  ill     guess
2740          23, // 2 12  ill   ISOLTS 805 loop point 010226 sxbd test no 26 (4bit)
2741          23, // 2 13  ill     guess
2742          23, // 2 14  ill     guess
2743          23, // 2 15  ill     guess
2744          27, // 3  0      6
2745          27, // 3  1      6
2746          27, // 3  2      6
2747          27, // 3  3      6
2748          27, // 3  4      6
2749          32, // 3  5      7
2750          32, // 3  6      7
2751          32, // 3  7      7
2752          32, // 3  8      7
2753          32, // 3  9  ill     guess
2754          32, // 3 10  ill     guess
2755          32, // 3 11  ill     guess
2756          32, // 3 12  ill     guess
2757          32, // 3 13  ill     guess
2758          32, // 3 14  ill   ISOLTS 805 loop point 010226 sxbd test no 24 (4bit)
2759          32  // 3 15  ill     guess
2760       };
2761     // Map charno:bitno to bit offset for 6 bit char set
2762     uint map6 [64] =
2763       {      // 9-bit    6-bit
2764           0, // 0  0      0
2765           1, // 0  1      0
2766           2, // 0  2      0
2767           3, // 0  3      0
2768           4, // 0  4      0
2769           5, // 0  5      0
2770           6, // 0  6      1
2771           7, // 0  7      1
2772           8, // 0  8      1
2773           6, // 0  9  ill  ISOLTS 805 loop point 010100 sxbd test no 12
2774           6, // 0 10  ill     guess
2775           6, // 0 11  ill     guess
2776           6, // 0 12  ill     guess
2777           6, // 0 13  ill     guess
2778           6, // 0 14  ill     guess
2779           6, // 0 15  ill     guess
2780           9, // 1  0      1
2781          10, // 1  1      1
2782          11, // 1  2      1
2783          12, // 1  3      2
2784          13, // 1  4      2
2785          14, // 1  5      2
2786          15, // 1  6      2
2787          16, // 1  7      2
2788          17, // 1  8      2
2789          12, // 1  9  ill     guess
2790          12, // 1 10  ill     guess
2791          12, // 1 11  ill ISOLTS 805 loop point 010100 sxbd test no 12
2792          12, // 1 12  ill     guess
2793          12, // 1 13  ill     guess
2794          12, // 1 14  ill     guess
2795          12, // 1 15  ill     guess
2796          18, // 2  0      3
2797          19, // 2  1      3
2798          20, // 2  2      3
2799          21, // 2  3      3
2800          22, // 2  4      3
2801          23, // 2  5      3
2802          24, // 2  6      4
2803          25, // 2  7      4
2804          26, // 2  8      4
2805          24, // 2  9  ill     guess
2806          24, // 2 10  ill     guess
2807          24, // 2 11  ill     guess
2808          24, // 2 12  ill     guess
2809          24, // 2 13  ill   ISOLTS 805 loop point 010100 sxbd test no 10
2810          24, // 2 14  ill     guess
2811          24, // 2 15  ill     guess
2812          27, // 3  0      4
2813          28, // 3  1      4
2814          29, // 3  2      4
2815          30, // 3  3      5
2816          31, // 3  4      5
2817          32, // 3  5      5
2818          33, // 3  6      5
2819          34, // 3  7      5
2820          35, // 3  8      5
2821          30, // 3  9  ill     guess
2822          30, // 3 10  ill     guess
2823          30, // 3 11  ill     guess
2824          30, // 3 12  ill     guess
2825          30, // 3 13  ill     guess
2826          30, // 3 14  ill     guess
2827          30  // 3 15  ill   ISOLTS 805 loop point 010100 sxbd test no 8 (6bit)
2828       };
2829     // Map charno:bitno to 1 and 9 bit offset
2830     uint map9 [64] =
2831       {      // 9-bit
2832           0, // 0  0
2833           1, // 0  1
2834           2, // 0  2
2835           3, // 0  3
2836           4, // 0  4
2837           5, // 0  5
2838           6, // 0  6
2839           7, // 0  7
2840           8, // 0  8
2841           8, // 0  9  ill     guess
2842           8, // 0 10  ill     guess
2843           8, // 0 11  ill     guess
2844           8, // 0 12  ill     guess
2845           8, // 0 13  ill     guess
2846           8, // 0 14  ill     guess
2847           8, // 0 15  ill     guess
2848           9, // 1  0
2849          10, // 1  1
2850          11, // 1  2
2851          12, // 1  3
2852          13, // 1  4
2853          14, // 1  5
2854          15, // 1  6
2855          16, // 1  7
2856          17, // 1  8
2857          17, // 1  9  ill     guess
2858          17, // 1 10  ill     guess
2859          17, // 1 11  ill     guess
2860          17, // 1 12  ill     guess
2861          17, // 1 13  ill     guess
2862          17, // 1 14  ill     guess
2863          17, // 1 15  ill     guess
2864          18, // 2  0
2865          19, // 2  1
2866          20, // 2  2
2867          21, // 2  3
2868          22, // 2  4
2869          23, // 2  5
2870          24, // 2  6
2871          25, // 2  7
2872          26, // 2  8
2873          26, // 2  9  ill     guess
2874          26, // 2 10  ill     guess
2875          26, // 2 11  ill     guess
2876          26, // 2 12  ill     guess
2877          26, // 2 13  ill     guess
2878          26, // 2 14  ill     guess
2879          26, // 2 15  ill     guess
2880          27, // 3  0
2881          28, // 3  1
2882          29, // 3  2
2883          30, // 3  3
2884          31, // 3  4
2885          32, // 3  5
2886          33, // 3  6
2887          34, // 3  7
2888          35, // 3  8
2889          35, // 3  9  ill     guess
2890          35, // 3 10  ill     guess
2891          35, // 3 11  ill     guess
2892          35, // 3 12  ill     guess
2893          35, // 3 13  ill     guess
2894          35, // 3 14  ill     guess
2895          35  // 3 15  ill     guess
2896       };
2897 
2898 //
2899 // Extract the operand data from the instruction
2900 //
2901 
2902     uint ARn = GET_ARN (cpu.cu.IWB);
2903     uint address = SIGNEXT15_18 (GET_OFFSET (cpu.cu.IWB));
2904     word4 reg = (word4) GET_TD (cpu.cu.IWB); // 4-bit register modification (None except
2905                                   // au, qu, al, ql, xn)
2906 
2907 //
2908 // Calculate r
2909 //
2910 
2911     // r is the count of characters (or bits if sz is 1; words if sz == 36)
2912     word36 rcnt = getCrAR (cpup, reg);
2913 
2914     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev, "asxbd sz %d r 0%"PRIo64"\r\n", sz, rcnt);
2915 
2916     // Crop rcnt into r based on the operand size.
2917     uint r = 0;
2918 
2919     if (sz == 1)
2920       r = (uint) (rcnt & MASK24);
2921     else if (sz == 4)
2922       r = (uint) (rcnt & MASK22);
2923     else if (sz == 6)
2924       r = (uint) (rcnt & MASK21);
2925     else if (sz == 9)
2926       r = (uint) (rcnt & MASK21);
2927     else // if (sz == 36)
2928       r = (uint) (rcnt & MASK18);
2929 
2930     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev, "asxbd sz %d ARn 0%o address 0%o reg 0%o r 0%o\r\n", sz, ARn, address, reg, r);
2931 
2932 //
2933 // Calculate augend
2934 //
2935 
2936     // If A is set, the instruction is AR = AR op operand; if not, AR = 0 op operand.
2937     uint augend = 0;
2938     if (GET_A (cpu.cu.IWB))
2939       {
2940         // For AWD/SWD, leave CHAR/BITNO alone
2941         if (sz == 36)
2942           {
2943             augend = cpu.AR[ARn].WORDNO * 36u;
2944           }
2945         else
2946           {
2947             uint bitno = GET_AR_BITNO (ARn);
2948             uint charno = GET_AR_CHAR (ARn);
2949 
2950             // The behavior of the DU for cases of CHAR > 9 is not defined; some values
2951             // are tested by ISOLTS, and have been recorded in the mapx tables; the
2952             // missing values are guessed at.
2953 
2954             uint * map;
2955             if (sz == 4)
2956               map = map4;
2957             else if (sz == 6)
2958               map = map6;
2959             else
2960               map = map9;
2961 
2962             augend = cpu.AR[ARn].WORDNO * 36u + map [charno * 16 + bitno];
2963             augend = augend % nxbits;
2964           }
2965       }
2966 
2967 //
2968 // Calculate addend
2969 //
2970 
2971     uint addend = 0;
2972     if (sz == 4)
2973       {
2974         // r is the number of 4 bit characters; each character is actually
2975         // 4.5 bits long
2976         addend = address * 36u + (r * 9) / 2;
2977 
2978         // round the odd character up one bit
2979         // (the odd character starts at bit n/2 + 1, not n/2.)
2980         if ((! sub) && r % 2) // r is odd
2981           addend ++;
2982       }
2983     else
2984       addend = address * 36u + r * sz;
2985 
2986     // Handle overflow
2987     addend = addend % nxbits;
2988 
2989 //
2990 // Calculate sum or difference
2991 //
2992 
2993     uint sum = 0;
2994     if (sub)
2995       {
2996         // Prevent underflow
2997         if (addend > augend)
2998           augend += nxbits;
2999         sum = augend - addend;
3000       }
3001     else
3002       {
3003         sum = augend + addend;
3004         sum %= nxbits;
3005       }
3006 
3007     sim_debug (DBG_TRACEEXT|DBG_CAC, & cpu_dev, "asxbd augend 0%o addend 0%o sum 0%o\r\n", augend, addend, sum);
3008 
3009 //
3010 // Adjust to character boundary
3011 //
3012 
3013     if (sz == 6 || sz == 9)
3014       {
3015         sum = (sum / sz) * sz;
3016       }
3017 
3018 //
3019 // Convert sum to WORDNO/CHAR/BITNO
3020 //
3021 
3022     cpu.AR [ARn].WORDNO = (word18) (sum / 36u) & AMASK;
3023 
3024     // If AWD/SWD clear CHAR/BITNO
3025 
3026     if (sz == 36)
3027       {
3028         SET_AR_CHAR_BITNO (ARn, 0, 0);
3029       }
3030     else
3031       {
3032         if (sz == 4)
3033           {
3034             static uint tab [36] [2] =
3035               {
3036                 // char bitno  offset  4-bit charno
3037                 { 0, 0 }, // 0   0
3038                 { 0, 0 }, // 1
3039                 { 0, 0 }, // 2
3040                 { 0, 0 }, // 3
3041                 { 0, 0 }, // 4
3042 
3043                 { 0, 5 }, // 5   1
3044                 { 0, 5 }, // 6
3045                 { 0, 5 }, // 7
3046                 { 0, 5 }, // 8
3047 
3048                 { 1, 0 }, // 9   2
3049                 { 1, 0 }, // 10
3050                 { 1, 0 }, // 11
3051                 { 1, 0 }, // 12
3052                 { 1, 0 }, // 13
3053 
3054                 { 1, 5 }, // 15  3
3055                 { 1, 5 }, // 15
3056                 { 1, 5 }, // 16
3057                 { 1, 5 }, // 17
3058 
3059                 { 2, 0 }, // 18  4
3060                 { 2, 0 }, // 19
3061                 { 2, 0 }, // 20
3062                 { 2, 0 }, // 21
3063                 { 2, 0 }, // 22
3064 
3065                 { 2, 5 }, // 23  5
3066                 { 2, 5 }, // 24
3067                 { 2, 5 }, // 25
3068                 { 2, 5 }, // 26
3069 
3070                 { 3, 0 }, // 27  6
3071                 { 3, 0 }, // 28
3072                 { 3, 0 }, // 29
3073                 { 3, 0 }, // 30
3074                 { 3, 0 }, // 31
3075 
3076                 { 3, 5 }, // 32  7
3077                 { 3, 5 }, // 33
3078                 { 3, 5 }, // 34
3079                 { 3, 5 }  // 35
3080               };
3081             uint charno = tab [sum % 36u] [0];
3082             uint bitno = tab [sum % 36u] [1];
3083             SET_AR_CHAR_BITNO (ARn, (word2) charno, (word4) bitno);
3084           }
3085         else
3086           {
3087             uint charno = (sum % 36u) / 9;
3088             uint bitno = sum % 9;
3089             SET_AR_CHAR_BITNO (ARn, (word2) charno, (word4) bitno);
3090           }
3091       }
3092 #if defined(TESTING)
3093     HDBGRegARW (ARn, "asxbd");
3094 #endif
3095   }
3096 
3097 void cmpc (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
3098   {
3099     EISstruct * e = & cpu.currentEISinstruction;
3100 
3101     // For i = 1, 2, ..., minimum (N1,N2)
3102     //    C(Y-charn1)i-1 :: C(Y-charn2)i-1
3103     // If N1 < N2, then for i = N1+1, N1+2, ..., N2
3104     //    C(FILL) :: C(Y-charn2)i-1
3105     // If N1 > N2, then for i = N2+1, N2+2, ..., N1
3106     //    C(Y-charn1)i-1 :: C(FILL)
3107     //
3108     // Indicators:
3109     //     Zero: If C(Y-charn1)i-1 = C(Y-charn2)i-1 for all i, then ON;
3110     //       otherwise, OFF
3111     //     Carry: If C(Y-charn1)i-1 < C(Y-charn2)i-1 for any i, then OFF;
3112     //       otherwise ON
3113 
3114     // Both strings are treated as the data type given for the left-hand
3115     // string, TA1. A data type given for the right-hand string, TA2, is
3116     // ignored.
3117     //
3118     // Comparison is made on full 9-bit fields. If the given data type is not
3119     // 9-bit (TA1 ≠ 0), then characters from C(Y-charn1) and C(Y-charn2) are
3120     // high- order zero filled. All 9 bits of C(FILL) are used.
3121     //
3122     // Instruction execution proceeds until an inequality is found or the
3123     // larger string length count is exhausted.
3124 
3125     fault_ipr_subtype_ mod_fault = 0;
3126 
3127 #if !defined(EIS_SETUP)
3128     setupOperandDescriptor (cpup, 1, &mod_fault);
3129     setupOperandDescriptor (cpup, 2, &mod_fault);
3130 #endif
3131     parseAlphanumericOperandDescriptor (cpup, 1, 1, false, &mod_fault);
3132     parseAlphanumericOperandDescriptor (cpup, 2, 1, false, &mod_fault);
3133 
3134     L68_ (
3135       // L68 raises it immediately
3136       if (mod_fault)
3137         {
3138           doFault (FAULT_IPR,
3139                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3140                    "Illegal modifier");
3141         }
3142     )
3143 
3144     // Bits 9-10 MBZ
3145     if (IWB_IRODD & 0000600000000)
3146       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "cmpc 9-10 MBZ");
3147 
3148     // Bit 23 of OP1 MBZ
3149     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
3150       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "cmpc op1 23 MBZ");
3151 
3152 // ISOLTS ps846    test-07a    dec add test
3153 // Sets TA2 to the same as TA1. AL39 says TA2 ignored.
3154 // Try only check bit 23.
3155 // ISOLTS 880 test-04c sets bit 23.
3156 
3157 
3158 
3159 
3160 
3161 
3162 
3163 
3164     DPS8M_ (
3165       // DPS8M raises it delayed
3166       if (mod_fault)
3167         {
3168           doFault (FAULT_IPR,
3169                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3170                    "Illegal modifier");
3171         }
3172     )
3173 
3174     word9 fill = getbits36_9 (cpu.cu.IWB, 0);
3175 
3176     SET_I_ZERO;  // set ZERO flag assuming strings are equal ...
3177     SET_I_CARRY; // set CARRY flag assuming strings are equal ...
3178 
3179     PNL (L68_ (if (max (e->N1, e->N2) < 128)
3180       DU_CYCLE_FLEN_128;))
3181 
3182     for (; cpu.du.CHTALLY < min (e->N1, e->N2); cpu.du.CHTALLY ++)
3183       {
3184         tandd_eis_sample (cpup, cpu.du.CHTALLY, TDPER (e -> TA1));
3185         word9 c1 = EISget469 (cpup, 1, cpu.du.CHTALLY); // get Y-char1n
3186         word9 c2 = EISget469 (cpup, 2, cpu.du.CHTALLY); // get Y-char2n
3187 sim_debug (DBG_TRACEEXT, & cpu_dev, "cmpc tally %d c1 %03o c2 %03o\r\n", cpu.du.CHTALLY, c1, c2);
3188         if (c1 != c2)
3189           {
3190             CLR_I_ZERO;  // an inequality found
3191             SC_I_CARRY (c1 > c2);
3192             cleanupOperandDescriptor (cpup, 1);
3193             cleanupOperandDescriptor (cpup, 2);
3194             return;
3195           }
3196       }
3197 
3198     if (e -> N1 < e -> N2)
3199       {
3200         for( ; cpu.du.CHTALLY < e->N2; cpu.du.CHTALLY ++)
3201           {
3202             tandd_eis_sample (cpup, cpu.du.CHTALLY, TDPER (e -> TA1));
3203             word9 c1 = fill;     // use fill for Y-char1n
3204             word9 c2 = EISget469 (cpup, 2, cpu.du.CHTALLY); // get Y-char2n
3205 
3206             if (c1 != c2)
3207               {
3208                 CLR_I_ZERO;  // an inequality found
3209                 SC_I_CARRY (c1 > c2);
3210                 cleanupOperandDescriptor (cpup, 1);
3211                 cleanupOperandDescriptor (cpup, 2);
3212                 return;
3213               }
3214           }
3215       }
3216     else if (e->N1 > e->N2)
3217       {
3218         for ( ; cpu.du.CHTALLY < e->N1; cpu.du.CHTALLY ++)
3219           {
3220             tandd_eis_sample (cpup, cpu.du.CHTALLY, TDPER (e -> TA1));
3221             word9 c1 = EISget469 (cpup, 1, cpu.du.CHTALLY); // get Y-char1n
3222             word9 c2 = fill;   // use fill for Y-char2n
3223 
3224             if (c1 != c2)
3225               {
3226                 CLR_I_ZERO;  // an inequality found
3227                 SC_I_CARRY (c1 > c2);
3228                 cleanupOperandDescriptor (cpup, 1);
3229                 cleanupOperandDescriptor (cpup, 2);
3230                 return;
3231               }
3232           }
3233       }
3234     // else ==
3235     cleanupOperandDescriptor (cpup, 1);
3236     cleanupOperandDescriptor (cpup, 2);
3237   }
3238 
3239 /*
3240  * SCD - Scan Characters Double
3241  */
3242 
3243 void scd (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
3244   {
3245     EISstruct * e = & cpu.currentEISinstruction;
3246 
3247     // For i = 1, 2, ..., N1-1
3248     //   C(Y-charn1)i-1,i :: C(Y-charn2)0,1
3249     // On instruction completion, if a match was found:
3250     //   00...0 -> C(Y3)0,11
3251     //   i-1 -> C(Y3)12,35
3252     // If no match was found:
3253     //   00...0 -> C(Y3)0,11
3254     //      N1-1 -> C(Y3)12,35
3255     //
3256 
3257     // The REG field of MF1 is checked for a legal code. If DU is specified in
3258     // the REG field of MF2 in one of the four multiword instructions (SCD,
3259     // SCDR, SCM, or SCMR) for which DU is legal, the CN field is ignored and
3260     // the character or characters are arranged within the 18 bits of the word
3261     // address portion of the operand descriptor.
3262 
3263     fault_ipr_subtype_ mod_fault = 0;
3264 
3265 #if !defined(EIS_SETUP)
3266     setupOperandDescriptor (cpup, 1, &mod_fault);
3267     setupOperandDescriptor (cpup, 2, &mod_fault);
3268     setupOperandDescriptorCache (cpup,3);
3269 #endif
3270 
3271     parseAlphanumericOperandDescriptor (cpup, 1, 1, false, &mod_fault);
3272     parseAlphanumericOperandDescriptor (cpup, 2, 1, true, &mod_fault); // use TA1
3273     parseArgOperandDescriptor (cpup, 3, &mod_fault);
3274 
3275     L68_ (
3276       // L68 raises it immediately
3277       if (mod_fault)
3278         {
3279           doFault (FAULT_IPR,
3280                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3281                    "Illegal modifier");
3282         }
3283     )
3284 
3285     // Bits 0-10 MBZ
3286     if (IWB_IRODD & 0777600000000)
3287       {
3288         //sim_printf ("scd %12"PRIo64"\r\n", IWB_IRODD);
3289         doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "scd 0-10 MBZ");
3290       }
3291 
3292     // Bit 23 of OP1 MBZ
3293     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
3294       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "scd op1 23 MBZ");
3295 
3296     // Bits 18-28. 30-31 of OP3 MBZ
3297     if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000777660)
3298       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "scd op3 18-28. 30-31 MBZ");
3299 
3300     DPS8M_ (
3301       // DPS8M raises it delayed
3302       if (mod_fault)
3303         {
3304           doFault (FAULT_IPR,
3305                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3306                    "Illegal modifier");
3307         }
3308     )
3309 
3310     // Both the string and the test character pair are treated as the data type
3311     // given for the string, TA1. A data type given for the test character
3312     // pair, TA2, is ignored.
3313 
3314     // fetch 'test' char - double
3315     // If MF2.ID = 0 and MF2.REG = du, then the second word following the
3316     // instruction word does not contain an operand descriptor for the test
3317     // character; instead, it contains the test character as a direct upper
3318     // operand in bits 0,8.
3319 
3320     word9 c1 = 0;
3321     word9 c2 = 0;
3322 
3323     if (! (e -> MF2 & MFkID) && ((e -> MF2 & MFkREGMASK) == 3))  // MF2.du
3324       {
3325         // per Bull RJ78, p. 5-45
3326 #if defined(EIS_PTR3)
3327         switch (TA1) // Use TA1, not TA2
3328 #else
3329         switch (e -> TA1) // Use TA1, not TA2
3330 #endif
3331         {
3332             case CTA4:
3333 #if defined(EIS_PTR)
3334               c1 = (cpu.du.D2_PTR_W >> 13) & 017;
3335               c2 = (cpu.du.D2_PTR_W >>  9) & 017;
3336 #else
3337               c1 = (e -> ADDR2.address >> 13) & 017;
3338               c2 = (e -> ADDR2.address >>  9) & 017;
3339 #endif
3340               break;
3341 
3342             case CTA6:
3343 #if defined(EIS_PTR)
3344               c1 = (cpu.du.D2_PTR_W >> 12) & 077;
3345               c2 = (cpu.du.D2_PTR_W >>  6) & 077;
3346 #else
3347               c1 = (e -> ADDR2.address >> 12) & 077;
3348               c2 = (e -> ADDR2.address >>  6) & 077;
3349 #endif
3350               break;
3351 
3352             case CTA9:
3353 #if defined(EIS_PTR)
3354               c1 = (cpu.du.D2_PTR_W >> 9) & 0777;
3355               c2 = (cpu.du.D2_PTR_W     ) & 0777;
3356 #else
3357               c1 = (e -> ADDR2.address >> 9) & 0777;
3358               c2 = (e -> ADDR2.address     ) & 0777;
3359 #endif
3360               break;
3361           }
3362       }
3363     else
3364       {
3365         c1 = EISget469 (cpup, 2, 0);
3366         c2 = EISget469 (cpup, 2, 1);
3367       }
3368 
3369 #if defined(EIS_PTR3)
3370     switch (TA1) // Use TA1, not TA2
3371 #else
3372     switch (e -> TA1) // Use TA1, not TA2
3373 #endif
3374       {
3375         case CTA4:
3376           c1 &= 017;    // keep 4-bits
3377           c2 &= 017;    // keep 4-bits
3378           break;
3379 
3380         case CTA6:
3381           c1 &= 077;    // keep 6-bits
3382           c2 &= 077;    // keep 6-bits
3383           break;
3384 
3385         case CTA9:
3386           c1 &= 0777;   // keep 9-bits
3387           c2 &= 0777;   // keep 9-bits
3388           break;
3389       }
3390 
3391     PNL (L68_ (if (e->N1 < 128)
3392       DU_CYCLE_FLEN_128;))
3393 
3394     word9 yCharn11;
3395     word9 yCharn12;
3396     // L68, T&D: the decimal unit keeps the operand 1 residual length
3397     // (characters not yet scanned) in its image; SPL stores it in word 3
3398     // bits 12-35 and LPL reloads it. A resumed scan (MIF, CHTALLY kept)
3399     // ends at CHTALLY + residual, so a handler can shorten the string
3400     // (PM842 16A/17A subtract 3770000/7760000 from SPL word 3 and expect
3401     // a count of 7777/17777 with tally runout).
3402     bool l68Res = cpu.tweaks.l68_mode && cpu.tweaks.tandd_mode;
3403     if (l68Res && cpu.tanddEISstart > 0)
3404       e -> N1 = cpu.du.CHTALLY + cpu.du.D1_RES;
3405     if (e -> N1)
3406       {
3407         uint limit = e -> N1 - 1;
3408         for ( ; cpu.du.CHTALLY < limit; cpu.du.CHTALLY ++)
3409           {
3410             if (l68Res)
3411               cpu.du.D1_RES = (e -> N1 - cpu.du.CHTALLY) & MASK24;
3412             tandd_eis_sample (cpup, cpu.du.CHTALLY, 3); // 6 chars/cycle: PM842 05B-07B <= 9 TROs, PA887 12A TRO inside
3413             yCharn11 = EISget469 (cpup, 1, cpu.du.CHTALLY);
3414             yCharn12 = EISget469 (cpup, 1, cpu.du.CHTALLY + 1);
3415             if (yCharn11 == c1 && yCharn12 == c2)
3416               break;
3417           }
3418         SC_I_TALLY (cpu.du.CHTALLY == limit);
3419       }
3420     else
3421       {
3422         SET_I_TALLY;
3423       }
3424 
3425     //word36 CY3 = bitfieldInsert36 (0, cpu.du.CHTALLY, 0, 24);
3426     word36 CY3 = setbits36_24 (0, 12, cpu.du.CHTALLY);
3427     EISWriteIdx (cpup, & e -> ADDR3, 0, CY3, true);
3428 
3429     cleanupOperandDescriptor (cpup, 1);
3430     cleanupOperandDescriptor (cpup, 2);
3431     cleanupOperandDescriptor (cpup, 3);
3432  }
3433 
3434 /*
3435  * SCDR - Scan Characters Double Reverse
3436  */
3437 
3438 void scdr (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
3439   {
3440     EISstruct * e = & cpu.currentEISinstruction;
3441 
3442     // For i = 1, 2, ..., N1-1
3443     //   C(Y-charn1)N1-i-1,N1-i :: C(Y-charn2)0,1
3444     // On instruction completion, if a match was found:
3445     //   00...0 -> C(Y3)0,11
3446     //   i-1 -> C(Y3)12,35
3447     // If no match was found:
3448     //   00...0 -> C(Y3)0,11
3449     //      N1-1 -> C(Y3)12,35
3450     //
3451 
3452     // The REG field of MF1 is checked for a legal code. If DU is specified in
3453     // the REG field of MF2 in one of the four multiword instructions (SCD,
3454     // SCDR, SCM, or SCMR) for which DU is legal, the CN field is ignored and
3455     // the character or characters are arranged within the 18 bits of the word
3456     // address portion of the operand descriptor.
3457 
3458     fault_ipr_subtype_ mod_fault = 0;
3459 
3460 #if !defined(EIS_SETUP)
3461     setupOperandDescriptor(cpup, 1, &mod_fault);
3462     setupOperandDescriptor(cpup, 2, &mod_fault);
3463     setupOperandDescriptorCache(cpup,3);
3464 #endif
3465 
3466     parseAlphanumericOperandDescriptor(cpup, 1, 1, false, &mod_fault);
3467     parseAlphanumericOperandDescriptor(cpup, 2, 1, true, &mod_fault); // Use TA1
3468     parseArgOperandDescriptor (cpup, 3, &mod_fault);
3469 
3470     L68_ (
3471       // L68 raises it immediately
3472       if (mod_fault)
3473         {
3474           doFault (FAULT_IPR,
3475                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3476                    "Illegal modifier");
3477         }
3478     )
3479 
3480     // Bits 0-10 MBZ
3481     if (IWB_IRODD & 0777600000000)
3482       {
3483         //sim_printf ("scdr %12"PRIo64"\r\n", IWB_IRODD);
3484         doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "scdr 0-10 MBZ");
3485       }
3486 
3487     // Bit 23 of OP1 MBZ
3488     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
3489       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "scdr op1 23 MBZ");
3490 
3491     // Bits 18-28. 30-31 of OP3 MBZ
3492     if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000777660)
3493       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "scdr op3 18-28. 30-31 MBZ");
3494 
3495     DPS8M_ (
3496       // DPS8M raises it delayed
3497       if (mod_fault)
3498         {
3499           doFault (FAULT_IPR,
3500                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3501                    "Illegal modifier");
3502         }
3503     )
3504 
3505     // Both the string and the test character pair are treated as the data type
3506     // given for the string, TA1. A data type given for the test character
3507     // pair, TA2, is ignored.
3508 
3509     // fetch 'test' char - double
3510     // If MF2.ID = 0 and MF2.REG = du, then the second word following the
3511     // instruction word does not contain an operand descriptor for the test
3512     // character; instead, it contains the test character as a direct upper
3513     // operand in bits 0,8.
3514 
3515     word9 c1 = 0;
3516     word9 c2 = 0;
3517 
3518     if (! (e -> MF2 & MFkID) && ((e -> MF2 & MFkREGMASK) == 3))  // MF2.du
3519       {
3520         // per Bull RJ78, p. 5-45
3521 #if defined(EIS_PTR3)
3522         switch (TA1) // Use TA1, not TA2
3523 #else
3524         switch (e -> TA1)
3525 #endif
3526           {
3527             case CTA4:
3528 #if defined(EIS_PTR)
3529               c1 = (cpu.du.D2_PTR_W >> 13) & 017;
3530               c2 = (cpu.du.D2_PTR_W >>  9) & 017;
3531 #else
3532               c1 = (e -> ADDR2.address >> 13) & 017;
3533               c2 = (e -> ADDR2.address >>  9) & 017;
3534 #endif
3535               break;
3536 
3537             case CTA6:
3538 #if defined(EIS_PTR)
3539               c1 = (cpu.du.D2_PTR_W >> 12) & 077;
3540               c2 = (cpu.du.D2_PTR_W >>  6) & 077;
3541 #else
3542               c1 = (e -> ADDR2.address >> 12) & 077;
3543               c2 = (e -> ADDR2.address >>  6) & 077;
3544 #endif
3545               break;
3546 
3547             case CTA9:
3548 #if defined(EIS_PTR)
3549               c1 = (cpu.du.D2_PTR_W >> 9) & 0777;
3550               c2 = (cpu.du.D2_PTR_W     ) & 0777;
3551 #else
3552               c1 = (e -> ADDR2.address >> 9) & 0777;
3553               c2 = (e -> ADDR2.address     ) & 0777;
3554 #endif
3555               break;
3556           }
3557       }
3558     else
3559       {
3560         c1 = EISget469 (cpup, 2, 0);
3561         c2 = EISget469 (cpup, 2, 1);
3562       }
3563 
3564 #if defined(EIS_PTR3)
3565     switch (TA1) // Use TA1, not TA2
3566 #else
3567     switch (e -> TA1) // Use TA1, not TA2
3568 #endif
3569       {
3570         case CTA4:
3571           c1 &= 017;    // keep 4-bits
3572           c2 &= 017;    // keep 4-bits
3573           break;
3574 
3575         case CTA6:
3576           c1 &= 077;    // keep 6-bits
3577           c2 &= 077;    // keep 6-bits
3578           break;
3579 
3580         case CTA9:
3581           c1 &= 0777;   // keep 9-bits
3582           c2 &= 0777;   // keep 9-bits
3583           break;
3584       }
3585 
3586     word9 yCharn11;
3587     word9 yCharn12;
3588 
3589     PNL (L68_ (if (e->N1 < 128)
3590       DU_CYCLE_FLEN_128;))
3591 
3592     if (e -> N1)
3593       {
3594         uint limit = e -> N1 - 1;
3595 
3596         for ( ; cpu.du.CHTALLY < limit; cpu.du.CHTALLY ++)
3597           {
3598             tandd_eis_sample (cpup, cpu.du.CHTALLY, 3); // 6 chars/cycle: PM842 05B-07B <= 9 TROs, PA887 12A TRO inside
3599             yCharn11 = EISget469 (cpup, 1, limit - cpu.du.CHTALLY - 1);
3600             yCharn12 = EISget469 (cpup, 1, limit - cpu.du.CHTALLY);
3601 
3602             if (yCharn11 == c1 && yCharn12 == c2)
3603                 break;
3604           }
3605         SC_I_TALLY (cpu.du.CHTALLY == limit);
3606       }
3607     else
3608       {
3609         SET_I_TALLY;
3610       }
3611 
3612     //word36 CY3 = bitfieldInsert36(0, cpu.du.CHTALLY, 0, 24);
3613     word36 CY3 = setbits36_24 (0, 12, cpu.du.CHTALLY);
3614     EISWriteIdx (cpup, & e -> ADDR3, 0, CY3, true);
3615 
3616     cleanupOperandDescriptor (cpup, 1);
3617     cleanupOperandDescriptor (cpup, 2);
3618     cleanupOperandDescriptor (cpup, 3);
3619   }
3620 
3621 /*
3622  * SCM - Scan with Mask
3623  */
3624 
3625 void scm (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
3626   {
3627     EISstruct * e = & cpu.currentEISinstruction;
3628 
3629     // For characters i = 1, 2, ..., N1
3630     //   For bits j = 0, 1, ..., 8
3631     //      C(Z)j = ~C(MASK)j & ((C(Y-charn1)i-1 )j (+) (C(Y-charn2)0)j)
3632     //      If C(Z)0,8 = 00...0, then
3633     //           00...0 -> C(Y3)0,11
3634     //           i-1 -> C(Y3)12,35
3635     //      otherwise, continue scan of C(Y-charn1)
3636     // If a masked character match was not found, then
3637     //   00...0 -> C(Y3)0,11
3638     //   N1 -> C(Y3)12,35
3639 
3640     // Starting at location YC1, the L1 type TA1 characters are masked and
3641     // compared with the assumed type TA1 character contained either in
3642     // location YC2 or in bits 0-8 or 0-5 of the address field of operand
3643     // descriptor 2 (when the REG field of MF2 specifies DU modification). The
3644     // mask is right-justified in bit positions 0-8 of the instruction word.
3645     // Each bit position of the mask that is a 1 prevents that bit position in
3646     // the two characters from entering into the compare.
3647 
3648     // The masked compare operation continues until either a match is found or
3649     // the tally (L1) is exhausted. For each unsuccessful match, a count is
3650     // incremented by 1. When a match is found or when the L1 tally runs out,
3651     // this count is stored right-justified in bits 12-35 of location Y3 and
3652     // bits 0-11 of Y3 are zeroed. The contents of location YC2 and the source
3653     // string remain unchanged. The RL bit of the MF2 field is not used.
3654 
3655     // The REG field of MF1 is checked for a legal code. If DU is specified in
3656     // the REG field of MF2 in one of the four multiword instructions (SCD,
3657     // SCDR, SCM, or SCMR) for which DU is legal, the CN field is ignored and
3658     // the character or characters are arranged within the 18 bits of the word
3659     // address portion of the operand descriptor.
3660 
3661     fault_ipr_subtype_ mod_fault = 0;
3662 
3663 #if !defined(EIS_SETUP)
3664     setupOperandDescriptor (cpup, 1, &mod_fault);
3665     setupOperandDescriptor (cpup, 2, &mod_fault);
3666     setupOperandDescriptorCache (cpup,3);
3667 #endif
3668 
3669     parseAlphanumericOperandDescriptor (cpup, 1, 1, false, &mod_fault);
3670     parseAlphanumericOperandDescriptor (cpup, 2, 1, true, &mod_fault);
3671     parseArgOperandDescriptor (cpup, 3, &mod_fault);
3672 
3673     L68_ (
3674       // L68 raises it immediately
3675       if (mod_fault)
3676         {
3677           doFault (FAULT_IPR,
3678                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3679                    "Illegal modifier");
3680         }
3681     )
3682 
3683     // Bits 9-10 MBZ
3684     if (IWB_IRODD & 0000600000000)
3685       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "scm 9-10 MBZ");
3686 
3687     // Bit 23 of OP1 MBZ
3688     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
3689       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "scm op1 23 MBZ");
3690 
3691     // Bits 18-28, 39-31 of OP3 MBZ
3692     if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000777660)
3693       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "scm op3 18-28, 39-31 MBZ");
3694 
3695     DPS8M_ (
3696       // DPS8M raises it delayed
3697       if (mod_fault)
3698         {
3699           doFault (FAULT_IPR,
3700                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3701                    "Illegal modifier");
3702         }
3703     )
3704 
3705     // Both the string and the test character pair are treated as the data type
3706     // given for the string, TA1. A data type given for the test character
3707     // pair, TA2, is ignored.
3708 
3709     // get 'mask'
3710     uint mask = (uint) getbits36_9 (cpu.cu.IWB, 0);
3711 
3712     // fetch 'test' char
3713     // If MF2.ID = 0 and MF2.REG = du, then the second word following the
3714     // instruction word does not contain an operand descriptor for the test
3715     // character; instead, it contains the test character as a direct upper
3716     // operand in bits 0,8.
3717 
3718     word9 ctest = 0;
3719     if (! (e -> MF2 & MFkID) && ((e -> MF2 & MFkREGMASK) == 3))  // MF2.du
3720       {
3721         word18 duo = GETHI (e -> OP2);
3722         // per Bull RJ78, p. 5-45
3723 #if defined(EIS_PTR3)
3724         switch (TA1) // Use TA1, not TA2
3725 #else
3726         switch (e -> TA1)
3727 #endif
3728           {
3729             case CTA4:
3730               ctest = (duo >> 13) & 017;
3731               break;
3732             case CTA6:
3733               ctest = (duo >> 12) & 077;
3734               break;
3735             case CTA9:
3736               ctest = (duo >> 9) & 0777;
3737               break;
3738           }
3739       }
3740     else
3741       {
3742         ctest = EISget469 (cpup, 2, 0);
3743       }
3744 
3745 #if defined(EIS_PTR3)
3746     switch (TA1) // Use TA1, not TA2
3747 #else
3748     switch (e -> TA1) // use TA1, not TA2
3749 #endif
3750       {
3751         case CTA4:
3752             ctest &= 017;    // keep 4-bits
3753             break;
3754         case CTA6:
3755             ctest &= 077;    // keep 6-bits
3756             break;
3757         case CTA9:
3758             ctest &= 0777;   // keep 9-bits
3759       }
3760 
3761     PNL (L68_ (if (e->N1 < 128)
3762       DU_CYCLE_FLEN_128;))
3763 
3764     uint limit = e -> N1;
3765 
3766     for ( ; cpu.du.CHTALLY < limit; cpu.du.CHTALLY ++)
3767       {
3768         tandd_eis_sample (cpup, cpu.du.CHTALLY, 3); // 6 chars/cycle: PM842 05B-07B <= 9 TROs, PA887 12A TRO inside
3769         word9 yCharn1 = EISget469 (cpup, 1, cpu.du.CHTALLY);
3770         word9 c = ((~mask) & (yCharn1 ^ ctest)) & 0777;
3771         if (c == 0)
3772           {
3773             //00...0 -> C(Y3)0,11
3774             //i-1 -> C(Y3)12,35
3775             //Y3 = bitfieldInsert36(Y3, cpu.du.CHTALLY, 0, 24);
3776             break;
3777           }
3778       }
3779     //word36 CY3 = bitfieldInsert36 (0, cpu.du.CHTALLY, 0, 24);
3780     word36 CY3 = setbits36_24 (0, 12, cpu.du.CHTALLY);
3781 
3782     SC_I_TALLY (cpu.du.CHTALLY == limit);
3783 
3784     EISWriteIdx (cpup, & e -> ADDR3, 0, CY3, true);
3785 
3786     cleanupOperandDescriptor (cpup, 1);
3787     cleanupOperandDescriptor (cpup, 2);
3788     cleanupOperandDescriptor (cpup, 3);
3789   }
3790 /*
3791  * SCMR - Scan with Mask in Reverse
3792  */
3793 
3794 void scmr (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
3795   {
3796     EISstruct * e = & cpu.currentEISinstruction;
3797 
3798     // For characters i = 1, 2, ..., N1
3799     //   For bits j = 0, 1, ..., 8
3800     //      C(Z)j = ~C(MASK)j & ((C(Y-charn1)i-1 )j (+) (C(Y-charn2)0)j)
3801     //      If C(Z)0,8 = 00...0, then
3802     //           00...0 -> C(Y3)0,11
3803     //           i-1 -> C(Y3)12,35
3804     //      otherwise, continue scan of C(Y-charn1)
3805     // If a masked character match was not found, then
3806     //   00...0 -> C(Y3)0,11
3807     //   N1 -> C(Y3)12,35
3808 
3809     // Starting at location YC1, the L1 type TA1 characters are masked and
3810     // compared with the assumed type TA1 character contained either in
3811     // location YC2 or in bits 0-8 or 0-5 of the address field of operand
3812     // descriptor 2 (when the REG field of MF2 specifies DU modification). The
3813     // mask is right-justified in bit positions 0-8 of the instruction word.
3814     // Each bit position of the mask that is a 1 prevents that bit position in
3815     // the two characters from entering into the compare.
3816 
3817     // The masked compare operation continues until either a match is found or
3818     // the tally (L1) is exhausted. For each unsuccessful match, a count is
3819     // incremented by 1. When a match is found or when the L1 tally runs out,
3820     // this count is stored right-justified in bits 12-35 of location Y3 and
3821     // bits 0-11 of Y3 are zeroed. The contents of location YC2 and the source
3822     // string remain unchanged. The RL bit of the MF2 field is not used.
3823 
3824     // The REG field of MF1 is checked for a legal code. If DU is specified in
3825     // the REG field of MF2 in one of the four multiword instructions (SCD,
3826     // SCDR, SCM, or SCMR) for which DU is legal, the CN field is ignored and
3827     // the character or characters are arranged within the 18 bits of the word
3828     // address portion of the operand descriptor.
3829 
3830     fault_ipr_subtype_ mod_fault = 0;
3831 
3832 #if !defined(EIS_SETUP)
3833     setupOperandDescriptor (cpup, 1, &mod_fault);
3834     setupOperandDescriptor (cpup, 2, &mod_fault);
3835     setupOperandDescriptorCache (cpup,3);
3836 #endif
3837 
3838     parseAlphanumericOperandDescriptor (cpup, 1, 1, false, &mod_fault);
3839     parseAlphanumericOperandDescriptor (cpup, 2, 1, true, &mod_fault);
3840     parseArgOperandDescriptor (cpup, 3, &mod_fault);
3841 
3842     L68_ (
3843       // L68 raises it immediately
3844       if (mod_fault)
3845         {
3846           doFault (FAULT_IPR,
3847                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3848                    "Illegal modifier");
3849         }
3850     )
3851 
3852     // Bits 9-10 MBZ
3853     if (IWB_IRODD & 0000600000000)
3854       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "scmr 9-10 MBZ");
3855 
3856     // Bit 23 of OP1 MBZ
3857     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
3858       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "scmr op1 23 MBZ");
3859 
3860     // Bits 18 of OP3 MBZ
3861     //if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000400000)
3862     //  doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "scmr op3 18 MBZ");
3863 
3864     // Bits 18-28, 39-31 of OP3 MBZ
3865     if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000777660)
3866       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "scmr op3 18-28, 39-31 MBZ");
3867 
3868     DPS8M_ (
3869       // DPS8M raises it delayed
3870       if (mod_fault)
3871         {
3872           doFault (FAULT_IPR,
3873                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
3874                    "Illegal modifier");
3875         }
3876     )
3877 
3878     // Both the string and the test character pair are treated as the data type
3879     // given for the string, TA1. A data type given for the test character
3880     // pair, TA2, is ignored.
3881 
3882     // get 'mask'
3883     uint mask = (uint) getbits36_9 (cpu.cu.IWB, 0);
3884 
3885     // fetch 'test' char
3886     // If MF2.ID = 0 and MF2.REG = du, then the second word following the
3887     // instruction word does not contain an operand descriptor for the test
3888     // character; instead, it contains the test character as a direct upper
3889     // operand in bits 0,8.
3890 
3891     word9 ctest = 0;
3892     if (! (e -> MF2 & MFkID) && ((e -> MF2 & MFkREGMASK) == 3))  // MF2.du
3893       {
3894         word18 duo = GETHI (e -> OP2);
3895         // per Bull RJ78, p. 5-45
3896 #if defined(EIS_PTR3)
3897         switch (TA1) // Use TA1, not TA2
3898 #else
3899         switch (e -> TA1)
3900 #endif
3901           {
3902             case CTA4:
3903               ctest = (duo >> 13) & 017;
3904               break;
3905             case CTA6:
3906               ctest = (duo >> 12) & 077;
3907               break;
3908             case CTA9:
3909               ctest = (duo >> 9) & 0777;
3910               break;
3911           }
3912       }
3913     else
3914       {
3915         ctest = EISget469 (cpup, 2, 0);
3916       }
3917 
3918 #if defined(EIS_PTR3)
3919     switch (TA1) // Use TA1, not TA2
3920 #else
3921     switch (e -> TA1) // use TA1, not TA2
3922 #endif
3923       {
3924         case CTA4:
3925             ctest &= 017;    // keep 4-bits
3926             break;
3927         case CTA6:
3928             ctest &= 077;    // keep 6-bits
3929             break;
3930         case CTA9:
3931             ctest &= 0777;   // keep 9-bits
3932       }
3933 
3934     PNL (L68_ (if (e->N1 < 128)
3935       DU_CYCLE_FLEN_128;))
3936 
3937     uint limit = e -> N1;
3938     for ( ; cpu.du.CHTALLY < limit; cpu.du.CHTALLY ++)
3939       {
3940         tandd_eis_sample (cpup, cpu.du.CHTALLY, 3); // 6 chars/cycle: PM842 05B-07B <= 9 TROs, PA887 12A TRO inside
3941         word9 yCharn1 = EISget469 (cpup, 1, limit - cpu.du.CHTALLY - 1);
3942         word9 c = ((~mask) & (yCharn1 ^ ctest)) & 0777;
3943         if (c == 0)
3944           {
3945             //00...0 -> C(Y3)0,11
3946             //i-1 -> C(Y3)12,35
3947             //Y3 = bitfieldInsert36(Y3, cpu.du.CHTALLY, 0, 24);
3948             break;
3949           }
3950       }
3951     //word36 CY3 = bitfieldInsert36 (0, cpu.du.CHTALLY, 0, 24);
3952     word36 CY3 = setbits36_24 (0, 12, cpu.du.CHTALLY);
3953 
3954     SC_I_TALLY (cpu.du.CHTALLY == limit);
3955 
3956     EISWriteIdx (cpup, & e -> ADDR3, 0, CY3, true);
3957 
3958     cleanupOperandDescriptor (cpup, 1);
3959     cleanupOperandDescriptor (cpup, 2);
3960     cleanupOperandDescriptor (cpup, 3);
3961   }
3962 
3963 /*
3964  * TCT - Test Character and Translate
3965  */
3966 
3967 
3968 
3969 
3970 
3971 
3972 
3973 
3974 
3975 
3976 
3977 
3978 
3979 
3980 
3981 
3982 
3983 
3984 
3985 
3986 
3987 
3988 static word9 xlate (cpu_state_t * cpup, EISaddr * xlatTbl, uint dstTA, uint c)
     /* [previous][next][first][last][top][bottom][index][help] */
3989   {
3990     uint idx = (c / 4) & 0177;      // max 128-words (7-bit index)
3991     word36 entry = EISReadIdx(cpup, xlatTbl, idx);
3992 
3993     uint pos9 = c % 4;      // lower 2-bits
3994     word9 cout = GETBYTE (entry, pos9);
3995     switch (dstTA)
3996       {
3997         case CTA4:
3998           return cout & 017;
3999         case CTA6:
4000           return cout & 077;
4001         case CTA9:
4002           return cout;
4003       }
4004     return 0;
4005   }
4006 
4007 void tct (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
4008   {
4009     EISstruct * e = & cpu.currentEISinstruction;
4010 
4011     // For i = 1, 2, ..., N1
4012     //   m = C(Y-charn1)i-1
4013     //   If C(Y-char92)m ≠ 00...0, then
4014     //     C(Y-char92)m -> C(Y3)0,8
4015     //     000 -> C(Y3)9,11
4016     //     i-1 -> C(Y3)12,35
4017     //   otherwise, continue scan of C(Y-charn1)
4018     // If a non-zero table entry was not found, then 00...0 -> C(Y3)0,11
4019     // N1 -> C(Y3)12,35
4020     //
4021     // Indicators: Tally Run Out. If the string length count exhausts, then ON;
4022     // otherwise, OFF
4023     //
4024     // If the data type of the string to be scanned is not 9-bit (TA1 ≠ 0),
4025     // then characters from C(Y-charn1) are high-order zero filled in forming
4026     // the table index, m.
4027     // Instruction execution proceeds until a non-zero table entry is found or
4028     // the string length count is exhausted.
4029     // The character number of Y-char92 must be zero, i.e., Y-char92 must start
4030     // on a word boundary.
4031 
4032     fault_ipr_subtype_ mod_fault = 0;
4033 
4034 #if !defined(EIS_SETUP)
4035     setupOperandDescriptor (cpup, 1, &mod_fault);
4036     setupOperandDescriptorCache (cpup,2);
4037     setupOperandDescriptorCache (cpup,3);
4038 #endif
4039 
4040     parseAlphanumericOperandDescriptor (cpup, 1, 1, false, &mod_fault);
4041     parseArgOperandDescriptor (cpup, 2, &mod_fault);
4042     parseArgOperandDescriptor (cpup, 3, &mod_fault);
4043 
4044     L68_ (
4045       // L68 raises it immediately
4046       if (mod_fault)
4047         {
4048           doFault (FAULT_IPR,
4049                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
4050                    "Illegal modifier");
4051         }
4052     )
4053 
4054     // Bits 0-17 MBZ
4055     if (IWB_IRODD & 0777777000000)
4056       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "tct 0-17 MBZ");
4057 
4058     // Bit 23 of OP1 MBZ
4059     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
4060       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "tct op1 23 MBZ");
4061 
4062     // Bits 18-28, 39-31 of OP2 MBZ
4063     if (!(e->MF[1] & MFkID) && e -> op [1]  & 0000000777660)
4064       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "tct op2 18-28, 39-31 MBZ");
4065 
4066     // Bits 18-28, 39-31 of OP3 MBZ
4067     if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000777660)
4068       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "tct op3 18-28, 39-31 MBZ");
4069 
4070     DPS8M_ (
4071       // DPS8M raises it delayed
4072       if (mod_fault)
4073         {
4074           doFault (FAULT_IPR,
4075                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
4076                    "Illegal modifier");
4077         }
4078     )
4079 
4080 #if defined(EIS_PTR3)
4081     sim_debug (DBG_TRACEEXT, & cpu_dev,
4082                "TCT CN1: %d TA1: %d\r\n", e -> CN1, TA1);
4083 #else
4084     sim_debug (DBG_TRACEEXT, & cpu_dev,
4085                "TCT CN1: %d TA1: %d\r\n", e -> CN1, e -> TA1);
4086 #endif
4087 
4088     uint srcSZ = 0;
4089 
4090 #if defined(EIS_PTR3)
4091     switch (TA1)
4092 #else
4093     switch (e -> TA1)
4094 #endif
4095       {
4096         case CTA4:
4097             srcSZ = 4;
4098             break;
4099         case CTA6:
4100             srcSZ = 6;
4101             break;
4102         case CTA9:
4103             srcSZ = 9;
4104             break;
4105       }
4106 
4107     // ISOLTS-878 01h asserts no prepaging
4108 
4109 
4110 
4111 
4112 
4113 
4114 
4115 
4116 
4117 
4118 
4119 
4120 
4121 
4122 
4123 
4124 
4125 
4126 
4127 
4128 
4129 
4130 
4131 
4132 
4133 
4134 
4135 
4136 
4137 
4138 
4139 
4140 
4141 
4142 
4143 
4144 
4145 
4146     word36 CY3 = 0;
4147 
4148     sim_debug (DBG_TRACEEXT, & cpu_dev,
4149                "TCT N1 %d\r\n", e -> N1);
4150 
4151     PNL (L68_ (if (e->N1 < 128)
4152       DU_CYCLE_FLEN_128;))
4153 
4154     for ( ; cpu.du.CHTALLY < e -> N1; cpu.du.CHTALLY ++)
4155       {
4156         tandd_eis_sample (cpup, cpu.du.CHTALLY, 2); // a table read per char
4157         word9 c = EISget469 (cpup, 1, cpu.du.CHTALLY); // get src char
4158 
4159         uint m = 0;
4160 
4161         switch (srcSZ)
4162           {
4163             case 4:
4164               m = c & 017;    // truncate upper 2-bits
4165               break;
4166             case 6:
4167               m = c & 077;    // truncate upper 3-bits
4168               break;
4169             case 9:
4170               m = c;          // keep all 9-bits
4171               break;              // should already be 0-filled
4172           }
4173 
4174         word9 cout = xlate (cpup, &e->ADDR2, CTA9, m);
4175 
4176         sim_debug (DBG_TRACEEXT, & cpu_dev,
4177                    "TCT c %03o %c cout %03o %c\r\n",
4178                    m, isprint ((int) m) ? '?' : (char) m,
4179                    cout, isprint ((int) cout) ? '?' : (char) cout);
4180 
4181         if (cout)
4182           {
4183             // CY3 = bitfieldInsert36 (0, cout, 27, 9); // C(Y-char92)m -> C(Y3)0,8
4184             CY3 = setbits36_9 (0, 0, cout);
4185             break;
4186           }
4187       }
4188 
4189     SC_I_TALLY (cpu.du.CHTALLY == e -> N1);
4190 
4191     //CY3 = bitfieldInsert36 (CY3, cpu.du.CHTALLY, 0, 24);
4192     putbits36_24 (& CY3, 12, cpu.du.CHTALLY);
4193     EISWriteIdx (cpup, & e -> ADDR3, 0, CY3, true);
4194 
4195     cleanupOperandDescriptor (cpup, 1);
4196     cleanupOperandDescriptor (cpup, 2);
4197     cleanupOperandDescriptor (cpup, 3);
4198   }
4199 
4200 void tctr (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
4201   {
4202     EISstruct * e = & cpu.currentEISinstruction;
4203 
4204     // For i = 1, 2, ..., N1
4205     //   m = C(Y-charn1)N1-i
4206     //   If C(Y-char92)m ≠ 00...0, then
4207     //     C(Y-char92)m -> C(Y3)0,8
4208     //     000 -> C(Y3)9,11
4209     //     i-1 -> C(Y3)12,35
4210     //   otherwise, continue scan of C(Y-charn1) If a non-zero table entry was
4211     //   not found, then
4212     // 00...0 -> C(Y3)0,11
4213     // N1 -> C(Y3)12,35
4214     //
4215     // Indicators: Tally Run Out. If the string length count exhausts, then ON;
4216     // otherwise, OFF
4217     //
4218     // If the data type of the string to be scanned is not 9-bit (TA1 ≠ 0),
4219     // then characters from C(Y-charn1) are high-order zero filled in forming
4220     // the table index, m.
4221 
4222     // Instruction execution proceeds until a non-zero table entry is found or
4223     // the string length count is exhausted.
4224 
4225     // The character number of Y-char92 must be zero, i.e., Y-char92 must start
4226     // on a word boundary.
4227 
4228     fault_ipr_subtype_ mod_fault = 0;
4229 
4230 #if !defined(EIS_SETUP)
4231     setupOperandDescriptor (cpup, 1, &mod_fault);
4232     setupOperandDescriptorCache (cpup,2);
4233     setupOperandDescriptorCache (cpup,3);
4234 #endif
4235 
4236     parseAlphanumericOperandDescriptor (cpup, 1, 1, false, &mod_fault);
4237     parseArgOperandDescriptor (cpup, 2, &mod_fault);
4238     parseArgOperandDescriptor (cpup, 3, &mod_fault);
4239 
4240     L68_ (
4241       // L68 raises it immediately
4242       if (mod_fault)
4243         {
4244           doFault (FAULT_IPR,
4245                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
4246                    "Illegal modifier");
4247         }
4248     )
4249 
4250     // Bits 0-17 MBZ
4251     if (IWB_IRODD & 0777777000000)
4252       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "tctr 0-17 MBZ");
4253 
4254     // Bit 23 of OP1 MBZ
4255     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
4256       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "tctr op1 23 MBZ");
4257 
4258     // Bits 18-28, 39-31 of OP2 MBZ
4259     if (!(e->MF[1] & MFkID) && e -> op [1]  & 0000000777660)
4260       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "tctr op2 18-28, 39-31 MBZ");
4261 
4262     // Bits 18-28, 39-31 of OP3 MBZ
4263     if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000777660)
4264       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "tctr op3 18-28, 39-31 MBZ");
4265 
4266     DPS8M_ (
4267       // DPS8M raises it delayed
4268       if (mod_fault)
4269         {
4270           doFault (FAULT_IPR,
4271                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
4272                    "Illegal modifier");
4273         }
4274     )
4275 
4276 #if defined(EIS_PTR3)
4277     sim_debug (DBG_TRACEEXT, & cpu_dev,
4278                "TCTR CN1: %d TA1: %d\r\n", e -> CN1, TA1);
4279 #else
4280     sim_debug (DBG_TRACEEXT, & cpu_dev,
4281                "TCTR CN1: %d TA1: %d\r\n", e -> CN1, e -> TA1);
4282 #endif
4283 
4284     uint srcSZ = 0;
4285 
4286 #if defined(EIS_PTR3)
4287     switch (TA1)
4288 #else
4289     switch (e -> TA1)
4290 #endif
4291       {
4292         case CTA4:
4293             srcSZ = 4;
4294             break;
4295         case CTA6:
4296             srcSZ = 6;
4297             break;
4298         case CTA9:
4299             srcSZ = 9;
4300             break;
4301       }
4302 
4303     // ISOLTS-878 01i asserts no prepaging
4304 
4305 
4306 
4307 
4308 
4309 
4310 
4311 
4312 
4313 
4314 
4315 
4316 
4317 
4318 
4319 
4320 
4321 
4322 
4323 
4324 
4325 
4326 
4327 
4328 
4329 
4330 
4331 
4332 
4333 
4334 
4335 
4336 
4337 
4338 
4339 
4340 
4341 
4342     word36 CY3 = 0;
4343 
4344     sim_debug (DBG_TRACEEXT, & cpu_dev,
4345                "TCT N1 %d\r\n", e -> N1);
4346 
4347     PNL (L68_ (if (e->N1 < 128)
4348       DU_CYCLE_FLEN_128;))
4349 
4350     uint limit = e -> N1;
4351     for ( ; cpu.du.CHTALLY < limit; cpu.du.CHTALLY ++)
4352       {
4353         tandd_eis_sample (cpup, cpu.du.CHTALLY, 2); // a table read per char
4354         word9 c = EISget469 (cpup, 1, limit - cpu.du.CHTALLY - 1); // get src char
4355 
4356         uint m = 0;
4357 
4358         switch (srcSZ)
4359           {
4360             case 4:
4361               m = c & 017;    // truncate upper 2-bits
4362               break;
4363             case 6:
4364               m = c & 077;    // truncate upper 3-bits
4365               break;
4366             case 9:
4367               m = c;          // keep all 9-bits
4368               break;              // should already be 0-filled
4369           }
4370 
4371         word9 cout = xlate (cpup, &e->ADDR2, CTA9, m);
4372 
4373         sim_debug (DBG_TRACEEXT, & cpu_dev,
4374                    "TCT c %03o %c cout %03o %c\r\n",
4375                    m, isprint ((int) m) ? '?' : (char) m,
4376                    cout, isprint ((int) cout) ? '?' : (char) cout);
4377 
4378         if (cout)
4379           {
4380             //CY3 = bitfieldInsert36 (0, cout, 27, 9); // C(Y-char92)m -> C(Y3)0,8
4381             CY3 = setbits36_9 (0, 0, cout);
4382             break;
4383           }
4384       }
4385 
4386     SC_I_TALLY (cpu.du.CHTALLY == e -> N1);
4387 
4388     //CY3 = bitfieldInsert36 (CY3, cpu.du.CHTALLY, 0, 24);
4389     putbits36_24 (& CY3, 12, cpu.du.CHTALLY);
4390     EISWriteIdx (cpup, & e -> ADDR3, 0, CY3, true);
4391 
4392     cleanupOperandDescriptor (cpup, 1);
4393     cleanupOperandDescriptor (cpup, 2);
4394     cleanupOperandDescriptor (cpup, 3);
4395   }
4396 
4397 /*
4398  * MLR - Move Alphanumeric Left to Right
4399  *
4400  * (Nice, simple instruction if it weren't for the stupid overpunch stuff that ruined it!!!!)
4401  */
4402 
4403 /*
4404  * does 6-bit char represent a GEBCD negative overpunch? if so, which numeral?
4405  * Refer to Bull NovaScale 9000 RJ78 Rev2 p11-178
4406  */
4407 
4408 
4409 
4410 
4411 
4412 
4413 
4414 
4415 
4416 
4417 
4418 
4419 
4420 
4421 
4422 
4423 
4424 
4425 
4426 
4427 
4428 
4429 
4430 
4431 
4432 // RJ78 p.11-178,D-6
4433 static bool isGBCDOvp (uint c, bool * isNeg)
     /* [previous][next][first][last][top][bottom][index][help] */
4434   {
4435     if (c & 020)
4436       {
4437         * isNeg = false;
4438         return true;
4439       }
4440     if (c & 040)
4441       {
4442         * isNeg = true;
4443         return true;
4444       }
4445     return false;
4446   }
4447 
4448 // Applies to both MLR and MRL
4449 //
4450 // If L1 is greater than L2, the least significant (L1-L2) characters are not moved and
4451 // the Truncation indicator is set. If L1 is less than L2, bits 0-8, 3-8, or 5-8 of the FILL
4452 // character (depending on TA2) are inserted as the least significant (L2-L1)
4453 // characters. If L1 is less than L2, bit 0 of C(FILL) = 1, TA1 = 01, and TA2 = 10
4454 // (6-4 move); the hardware looks for a 6-bit overpunched sign. If a negative
4455 // overpunch sign is found, a negative sign (octal 15) is inserted as the last FILL
4456 // character. If a negative overpunch sign is not found, a positive sign (octal 14) is
4457 // inserted as the last FILL character
4458 
4459 void mlr (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
4460   {
4461     EISstruct * e = & cpu.currentEISinstruction;
4462 
4463     // For i = 1, 2, ..., minimum (N1,N2)
4464     //     C(Y-charn1)N1-i -> C(Y-charn2)N2-i
4465     // If N1 < N2, then for i = N1+1, N1+2, ..., N2
4466     //    C(FILL) -> C(Y-charn2)N2-i
4467     // Indicators: Truncation. If N1 > N2 then ON; otherwise OFF
4468 
4469     fault_ipr_subtype_ mod_fault = 0;
4470 
4471 #if !defined(EIS_SETUP)
4472     setupOperandDescriptor (cpup, 1, &mod_fault);
4473     setupOperandDescriptor (cpup, 2, &mod_fault);
4474     //setupOperandDescriptorCache (cpup,3);
4475 #endif
4476 
4477     parseAlphanumericOperandDescriptor(cpup, 1, 1, false, &mod_fault);
4478     parseAlphanumericOperandDescriptor(cpup, 2, 2, false, &mod_fault);
4479 
4480     L68_ (
4481       // L68 raises it immediately
4482       if (mod_fault)
4483         {
4484           doFault (FAULT_IPR,
4485                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
4486                    "Illegal modifier");
4487         }
4488     )
4489 
4490     // Bit 10 MBZ
4491     if (IWB_IRODD & 0000200000000)
4492       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "mlr 10 MBZ");
4493 
4494     // Bit 23 of OP1 MBZ
4495     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
4496       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mlr op1 23 MBZ");
4497 
4498     // Bit 23 of OP2 MBZ
4499     if (!(e->MF[1] & MFkID) && e -> op [1]  & 0000000010000)
4500       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mlr op2 23 MBZ");
4501 
4502     DPS8M_ (
4503       // DPS8M raises it delayed
4504       if (mod_fault)
4505         {
4506           doFault (FAULT_IPR,
4507                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
4508                    "Illegal modifier");
4509         }
4510     )
4511 
4512     int srcSZ = 0, dstSZ = 0;
4513 
4514 #if defined(EIS_PTR3)
4515     switch (TA1)
4516 #else
4517     switch (e -> TA1)
4518 #endif
4519       {
4520         case CTA4:
4521           srcSZ = 4;
4522           break;
4523         case CTA6:
4524           srcSZ = 6;
4525           break;
4526         case CTA9:
4527           srcSZ = 9;
4528           break;
4529       }
4530 
4531 #if defined(EIS_PTR3)
4532     switch (TA2)
4533 #else
4534     switch (e -> TA2)
4535 #endif
4536       {
4537         case CTA4:
4538           dstSZ = 4;
4539           break;
4540         case CTA6:
4541           dstSZ = 6;
4542           break;
4543         case CTA9:
4544           dstSZ = 9;
4545           break;
4546       }
4547 
4548     word1 T = getbits36_1 (cpu.cu.IWB, 9);
4549 
4550     word9 fill = getbits36_9 (cpu.cu.IWB, 0);
4551     word9 fillT = fill;  // possibly truncated fill pattern
4552 
4553     // play with fill if we need to use it
4554     switch (dstSZ)
4555       {
4556         case 4:
4557           fillT = fill & 017;    // truncate upper 5-bits
4558           break;
4559         case 6:
4560           fillT = fill & 077;    // truncate upper 3-bits
4561           break;
4562       }
4563 
4564     // If N1 > N2, then (N1-N2) leading characters of C(Y-charn1) are not moved
4565     // and the truncation indicator is set ON.
4566 
4567     // If N1 < N2 and TA2 = 2 (4-bit data) or 1 (6-bit data), then FILL
4568     // characters are high-order truncated as they are moved to C(Y-charn2). No
4569     // character conversion takes place.
4570 
4571     // The user of string replication or overlaying is warned that the decimal
4572     // unit addresses the main memory in unaligned (not on modulo 8 boundary)
4573     // units of Y-block8 words and that the overlaid string, C(Y-charn2), is
4574     // not returned to main memory until the unit of Y-block8 words is filled or
4575     // the instruction completes.
4576 
4577     // If T = 1 and the truncation indicator is set ON by execution of the
4578     // instruction, then a truncation (overflow) fault occurs.
4579 
4580     // Attempted execution with the xed instruction causes an illegal procedure
4581     // fault.
4582 
4583     // Attempted repetition with the rpt, rpd, or rpl instructions causes an
4584     // illegal procedure fault.
4585 
4586     PNL (L68_ (if (max (e->N1, e->N2) < 128)
4587       DU_CYCLE_FLEN_128;))
4588 
4589 #if defined(EIS_PTR3)
4590     bool ovp = (e -> N1 < e -> N2) && (fill & 0400) && (TA1 == 1) &&
4591                (TA2 == 2); // (6-4 move)
4592 #else
4593     bool ovp = (e -> N1 < e -> N2) && (fill & 0400) && (e -> TA1 == 1) &&
4594                (e -> TA2 == 2); // (6-4 move)
4595 #endif
4596     //word9 on;     // number overpunch represents (if any)
4597     bool isNeg = false;
4598     //bool bOvp = false;  // true when a negative overpunch character has been
4599                         // found @ N1-1
4600 
4601 #if defined(EIS_PTR3)
4602     sim_debug (DBG_TRACEEXT, & cpu_dev,
4603                "MLR TALLY %u TA1 %u TA2 %u N1 %u N2 %u CN1 %u CN2 %u\r\n",
4604                cpu.du.CHTALLY, TA1, TA2, e -> N1, e -> N2, e -> CN1, e -> CN2);
4605 #else
4606     sim_debug (DBG_TRACEEXT, & cpu_dev,
4607                "MLR TALLY %u TA1 %u TA2 %u N1 %u N2 %u CN1 %u CN2 %u\r\n",
4608                cpu.du.CHTALLY, e -> TA1, e -> TA2, e -> N1, e -> N2, e -> CN1, e -> CN2);
4609 #endif
4610 
4611 //
4612 // Multics frequently uses certain code sequences which are easily detected
4613 // and optimized; eg. it uses the MLR instruction to copy or zero segments.
4614 //
4615 // The MLR implementation is correct, not efficient. Copy invokes 12 append
4616 // cycles per word, and fill 8.
4617 //
4618 
4619 //
4620 // Page copy
4621 //
4622 
4623     if ((cpu.du.CHTALLY % PGSZ) == 0 &&
4624 #if defined(EIS_PTR3)
4625         TA1 == CTA9 &&  // src and dst are both char 9
4626         TA2 == CTA9 &&
4627 #else
4628         e -> TA1 == CTA9 &&  // src and dst are both char 9
4629         e -> TA2 == CTA9 &&
4630 #endif
4631         (e -> N1 % (PGSZ * 4)) == 0 &&  // a page
4632         e -> N2 == e -> N1 && // the src is the same size as the dest.
4633         e -> CN1 == 0 &&  // and it starts at a word boundary // BITNO?
4634         e -> CN2 == 0 &&
4635 #if defined(EIS_PTR)
4636         (cpu.du.D1_PTR_W & PGMK) == 0 &&
4637         (cpu.du.D2_PTR_W & PGMK) == 0)
4638 #else
4639         (e -> ADDR1.address & PGMK) == 0 &&
4640         (e -> ADDR2.address & PGMK) == 0)
4641 #endif
4642       {
4643         sim_debug (DBG_TRACEEXT, & cpu_dev, "MLR special case #3\r\n");
4644         while (cpu.du.CHTALLY < e -> N1)
4645           {
4646             word36 pg [PGSZ];
4647             EISReadPage (cpup, & e -> ADDR1, cpu.du.CHTALLY / 4, pg);
4648             EISWritePage (cpup, & e -> ADDR2, cpu.du.CHTALLY / 4, pg);
4649             cpu.du.CHTALLY += PGSZ * 4;
4650           }
4651         cleanupOperandDescriptor (cpup, 1);
4652         cleanupOperandDescriptor (cpup, 2);
4653         // truncation fault check does need to be checked for here since
4654         // it is known that N1 == N2
4655         CLR_I_TRUNC;
4656         return;
4657       }
4658 
4659 //
4660 // Page zero
4661 //
4662 
4663     if ((cpu.du.CHTALLY % PGSZ) == 0 &&
4664 #if defined(EIS_PTR3)
4665         TA1 == CTA9 &&  // src and dst are both char 9
4666         TA2 == CTA9 &&
4667 #else
4668         e -> TA1 == CTA9 &&  // src and dst are both char 9
4669         e -> TA2 == CTA9 &&
4670 #endif
4671         e -> N1 == 0 && // the source is entirely fill
4672         (e -> N2 % (PGSZ * 4)) == 0 &&  // a page
4673         e -> CN1 == 0 &&  // and it starts at a word boundary // BITNO?
4674         e -> CN2 == 0 &&
4675 #if defined(EIS_PTR)
4676         (cpu.du.D1_PTR_W & PGMK) == 0 &&
4677         (cpu.du.D2_PTR_W& PGMK) == 0)
4678 #else
4679         (e -> ADDR1.address & PGMK) == 0 &&
4680         (e -> ADDR2.address & PGMK) == 0)
4681 #endif
4682       {
4683         sim_debug (DBG_TRACEEXT, & cpu_dev, "MLR special case #4\r\n");
4684         word36 pg [PGSZ];
4685         if (fill)
4686           {
4687             word36 w = (word36) fill | ((word36) fill << 9) | ((word36) fill << 18) | ((word36) fill << 27);
4688             for (uint i = 0; i < PGSZ; i ++)
4689               pg [i] = w;
4690           }
4691         else
4692           {
4693            (void)memset (pg, 0, sizeof (pg));
4694           }
4695         while (cpu.du.CHTALLY < e -> N2)
4696           {
4697             EISWritePage (cpup, & e -> ADDR2, cpu.du.CHTALLY / 4, pg);
4698             cpu.du.CHTALLY += PGSZ * 4;
4699           }
4700         cleanupOperandDescriptor (cpup, 1);
4701         cleanupOperandDescriptor (cpup, 2);
4702         // truncation fault check does need to be checked for here since
4703         // it is known that N1 == N2
4704         CLR_I_TRUNC;
4705         return;
4706       }
4707 
4708 // Test for the case of aligned word move; and do things a word at a time,
4709 // instead of a byte at a time...
4710 
4711 #if defined(EIS_PTR3)
4712     if (TA1 == CTA9 &&  // src and dst are both char 9
4713         TA2 == CTA9 &&
4714 #else
4715     if (e -> TA1 == CTA9 &&  // src and dst are both char 9
4716         e -> TA2 == CTA9 &&
4717 #endif
4718         e -> N1 % 4 == 0 &&  // a whole number of words in the src
4719         e -> N2 == e -> N1 && // the src is the same size as the dest.
4720         e -> CN1 == 0 &&  // and it starts at a word boundary // BITNO?
4721         e -> CN2 == 0)
4722       {
4723         sim_debug (DBG_TRACEEXT, & cpu_dev, "MLR special case #1\r\n");
4724         for ( ; cpu.du.CHTALLY < e -> N2; cpu.du.CHTALLY += 4)
4725           {
4726             tandd_eis_sample (cpup, cpu.du.CHTALLY, 4);
4727             uint n = cpu.du.CHTALLY / 4;
4728             word36 w = EISReadIdx (cpup, & e -> ADDR1, n);
4729             EISWriteIdx (cpup, & e -> ADDR2, n, w, true);
4730           }
4731         cleanupOperandDescriptor (cpup, 1);
4732         cleanupOperandDescriptor (cpup, 2);
4733         // truncation fault check does need to be checked for here since
4734         // it is known that N1 == N2
4735         CLR_I_TRUNC;
4736         return;
4737       }
4738 
4739 // Test for the case of aligned word fill; and do things a word at a time,
4740 // instead of a byte at a time...
4741 
4742 #if defined(EIS_PTR3)
4743     if (TA1 == CTA9 && // src and dst are both char 9
4744         TA2 == CTA9 &&
4745 #else
4746     if (e -> TA1 == CTA9 && // src and dst are both char 9
4747         e -> TA2 == CTA9 &&
4748 #endif
4749         e -> N1 == 0 && // the source is entirely fill
4750         e -> N2 % 4 == 0 && // a whole number of words in the dest
4751         e -> CN1 == 0 &&  // and it starts at a word boundary // BITNO?
4752         e -> CN2 == 0)
4753       {
4754         sim_debug (DBG_TRACEEXT, & cpu_dev, "MLR special case #2\r\n");
4755         word36 w = (word36) fill | ((word36) fill << 9) | ((word36) fill << 18) | ((word36) fill << 27);
4756         for ( ; cpu.du.CHTALLY < e -> N2; cpu.du.CHTALLY += 4)
4757           {
4758             tandd_eis_sample (cpup, cpu.du.CHTALLY, 4);
4759             uint n = cpu.du.CHTALLY / 4;
4760             EISWriteIdx (cpup, & e -> ADDR2, n, w, true);
4761           }
4762         cleanupOperandDescriptor (cpup, 1);
4763         cleanupOperandDescriptor (cpup, 2);
4764         // truncation fault check does need to be checked for here since
4765         // it is known that N1 <= N2
4766         CLR_I_TRUNC;
4767         return;
4768       }
4769 
4770     uint tdPer = TDPER (e -> TA2);
4771     for ( ; cpu.du.CHTALLY < min (e->N1, e->N2); cpu.du.CHTALLY ++)
4772       {
4773         tandd_eis_sample (cpup, cpu.du.CHTALLY, tdPer);
4774         word9 c = EISget469 (cpup, 1, cpu.du.CHTALLY); // get src char
4775         word9 cout = 0;
4776 
4777 #if defined(EIS_PTR3)
4778         if (TA1 == TA2)
4779 #else
4780         if (e -> TA1 == e -> TA2)
4781 #endif
4782           EISput469 (cpup, 2, cpu.du.CHTALLY, c);
4783         else
4784           {
4785             // If data types are dissimilar (TA1 ≠ TA2), each character is
4786             // high-order truncated or zero filled, as appropriate, as it is
4787             // moved. No character conversion takes place.
4788             cout = c;
4789             switch (srcSZ)
4790               {
4791                 case 6:
4792                   switch(dstSZ)
4793                     {
4794                       case 4:
4795                         cout = c & 017;    // truncate upper 2-bits
4796                         break;
4797                       case 9:
4798                         break;              // should already be 0-filled
4799                     }
4800                   break;
4801                 case 9:
4802                   switch(dstSZ)
4803                     {
4804                       case 4:
4805                         cout = c & 017;    // truncate upper 5-bits
4806                         break;
4807                       case 6:
4808                         cout = c & 077;    // truncate upper 3-bits
4809                         break;
4810                     }
4811                   break;
4812               }
4813 
4814             // If N1 < N2, C(FILL)0 = 1, TA1 = 1, and TA2 = 2 (6-4 move), then
4815             // C(Y-charn1)N1-1 is examined for a GBCD overpunch sign. If a
4816             // negative overpunch sign is found, then the minus sign character
4817             // is placed in C(Y-charn2)N2-1; otherwise, a plus sign character
4818             // is placed in C(Y-charn2)N2-1.
4819 
4820             if (ovp && (cpu.du.CHTALLY == e -> N1 - 1))
4821               {
4822                 // C(FILL)0 = 1 means that there *is* an overpunch char here.
4823                 // ISOLTS-838 01e, RJ78 p. 11-126
4824                 isGBCDOvp (c, & isNeg);
4825               }
4826             EISput469 (cpup, 2, cpu.du.CHTALLY, cout);
4827           }
4828       }
4829 
4830     // If N1 < N2, then for i = N1+1, N1+2, ..., N2
4831     //    C(FILL) -> C(Y-charn2)N2-i
4832     // If N1 < N2 and TA2 = 2 (4-bit data) or 1 (6-bit data), then FILL
4833     // characters are high-order truncated as they are moved to C(Y-charn2). No
4834     // character conversion takes place.
4835 
4836     if (e -> N1 < e -> N2)
4837       {
4838         for ( ; cpu.du.CHTALLY < e -> N2 ; cpu.du.CHTALLY ++)
4839           {
4840             tandd_eis_sample (cpup, cpu.du.CHTALLY, tdPer);
4841             // if there's an overpunch then the sign will be the last of the fill
4842             if (ovp && (cpu.du.CHTALLY == e -> N2 - 1))
4843               {
4844                 if (isNeg)
4845                   EISput469 (cpup, 2, cpu.du.CHTALLY, 015); // 015 is decimal -
4846                 else
4847                   EISput469 (cpup, 2, cpu.du.CHTALLY, 014); // 014 is decimal +
4848               }
4849             else
4850               EISput469 (cpup, 2, cpu.du.CHTALLY, fillT);
4851           }
4852     }
4853     cleanupOperandDescriptor (cpup, 1);
4854     cleanupOperandDescriptor (cpup, 2);
4855 
4856     if (e -> N1 > e -> N2)
4857       {
4858         SET_I_TRUNC;
4859         if (T && ! TST_I_OMASK)
4860           doFault (FAULT_OFL, fst_zero, "mlr truncation fault");
4861       }
4862     else
4863       CLR_I_TRUNC;
4864   }
4865 
4866 void mrl (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
4867   {
4868     EISstruct * e = & cpu.currentEISinstruction;
4869 
4870     // For i = 1, 2, ..., minimum (N1,N2)
4871     //     C(Y-charn1)N1-i -> C(Y-charn2)N2-i
4872     // If N1 < N2, then for i = N1+1, N1+2, ..., N2
4873     //    C(FILL) -> C(Y-charn2)N2-i
4874     // Indicators: Truncation. If N1 > N2 then ON; otherwise OFF
4875 
4876     fault_ipr_subtype_ mod_fault = 0;
4877 
4878 #if !defined(EIS_SETUP)
4879     setupOperandDescriptor (cpup, 1, &mod_fault);
4880     setupOperandDescriptor (cpup, 2, &mod_fault);
4881     //setupOperandDescriptorCache (cpup,3);
4882 #endif
4883 
4884     parseAlphanumericOperandDescriptor(cpup, 1, 1, false, &mod_fault);
4885     parseAlphanumericOperandDescriptor(cpup, 2, 2, false, &mod_fault);
4886 
4887     L68_ (
4888       // L68 raises it immediately
4889       if (mod_fault)
4890         {
4891           doFault (FAULT_IPR,
4892                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
4893                    "Illegal modifier");
4894         }
4895     )
4896 
4897     // Bit 10 MBZ
4898     if (IWB_IRODD & 0000200000000)
4899       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "mrl 10 MBZ");
4900 
4901     // Bit 23 of OP1 MBZ
4902     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
4903       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mrl op1 23 MBZ");
4904 
4905     // Bit 23 of OP2 MBZ
4906     if (!(e->MF[1] & MFkID) && e -> op [1]  & 0000000010000)
4907       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mrl op2 23 MBZ");
4908 
4909     DPS8M_ (
4910       // DPS8M raises it delayed
4911       if (mod_fault)
4912         {
4913           doFault (FAULT_IPR,
4914                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
4915                    "Illegal modifier");
4916         }
4917     )
4918 
4919     int srcSZ = 0, dstSZ = 0;
4920 
4921 #if defined(EIS_PTR3)
4922     switch (TA1)
4923 #else
4924     switch (e -> TA1)
4925 #endif
4926       {
4927         case CTA4:
4928           srcSZ = 4;
4929           break;
4930         case CTA6:
4931           srcSZ = 6;
4932           break;
4933         case CTA9:
4934           srcSZ = 9;
4935           break;
4936       }
4937 
4938 #if defined(EIS_PTR3)
4939     switch (TA2)
4940 #else
4941     switch (e -> TA2)
4942 #endif
4943       {
4944         case CTA4:
4945           dstSZ = 4;
4946           break;
4947         case CTA6:
4948           dstSZ = 6;
4949           break;
4950         case CTA9:
4951           dstSZ = 9;
4952           break;
4953       }
4954 
4955     word1 T = getbits36_1 (cpu.cu.IWB, 9);
4956 
4957     word9 fill = getbits36_9 (cpu.cu.IWB, 0);
4958     word9 fillT = fill;  // possibly truncated fill pattern
4959 
4960     // play with fill if we need to use it
4961     switch (dstSZ)
4962       {
4963         case 4:
4964           fillT = fill & 017;    // truncate upper 5-bits
4965           break;
4966         case 6:
4967           fillT = fill & 077;    // truncate upper 3-bits
4968           break;
4969       }
4970 
4971     // If N1 > N2, then (N1-N2) leading characters of C(Y-charn1) are not moved
4972     // and the truncation indicator is set ON.
4973 
4974     // If N1 < N2 and TA2 = 2 (4-bit data) or 1 (6-bit data), then FILL
4975     // characters are high-order truncated as they are moved to C(Y-charn2). No
4976     // character conversion takes place.
4977 
4978     // The user of string replication or overlaying is warned that the decimal
4979     // unit addresses the main memory in unaligned (not on modulo 8 boundary)
4980     // units of Y-block8 words and that the overlaid string, C(Y-charn2), is
4981     // not returned to main memory until the unit of Y-block8 words is filled or
4982     // the instruction completes.
4983 
4984     // If T = 1 and the truncation indicator is set ON by execution of the
4985     // instruction, then a truncation (overflow) fault occurs.
4986 
4987     // Attempted execution with the xed instruction causes an illegal procedure
4988     // fault.
4989 
4990     // Attempted repetition with the rpt, rpd, or rpl instructions causes an
4991     // illegal procedure fault.
4992 
4993 #if defined(EIS_PTR3)
4994     bool ovp = (e -> N1 < e -> N2) && (fill & 0400) && (TA1 == 1) &&
4995                (TA2 == 2); // (6-4 move)
4996 #else
4997     bool ovp = (e -> N1 < e -> N2) && (fill & 0400) && (e -> TA1 == 1) &&
4998                (e -> TA2 == 2); // (6-4 move)
4999 #endif
5000     bool isNeg = false;
5001     //bool bOvp = false;  // true when a negative overpunch character has been
5002                         // found @ N1-1
5003 
5004     PNL (L68_ (if (max (e->N1, e->N2) < 128)
5005       DU_CYCLE_FLEN_128;))
5006 
5007 //
5008 // Test for the case of aligned word move; and do things a word at a time,
5009 // instead of a byte at a time...
5010 
5011 #if defined(EIS_PTR3)
5012     if (TA1 == CTA9 &&  // src and dst are both char 9
5013         TA2 == CTA9 &&
5014 #else
5015     if (e -> TA1 == CTA9 &&  // src and dst are both char 9
5016         e -> TA2 == CTA9 &&
5017 #endif
5018         e -> N1 % 4 == 0 &&  // a whole number of words in the src
5019         e -> N2 == e -> N1 && // the src is the same size as the dest.
5020         e -> CN1 == 0 &&  // and it starts at a word boundary // BITNO?
5021         e -> CN2 == 0)
5022       {
5023         sim_debug (DBG_TRACEEXT, & cpu_dev, "MRL special case #1\r\n");
5024         uint limit = e -> N2;
5025         for ( ; cpu.du.CHTALLY < limit; cpu.du.CHTALLY += 4)
5026           {
5027             tandd_eis_sample (cpup, cpu.du.CHTALLY, TDPER (e -> TA2));
5028             uint n = (limit - cpu.du.CHTALLY - 1) / 4;
5029             word36 w = EISReadIdx (cpup, & e -> ADDR1, n);
5030             EISWriteIdx (cpup, & e -> ADDR2, n, w, true);
5031           }
5032         cleanupOperandDescriptor (cpup, 1);
5033         cleanupOperandDescriptor (cpup, 2);
5034         // truncation fault check does need to be checked for here since
5035         // it is known that N1 == N2
5036         CLR_I_TRUNC;
5037         return;
5038       }
5039 
5040 // Test for the case of aligned word fill; and do things a word at a time,
5041 // instead of a byte at a time...
5042 
5043 #if defined(EIS_PTR3)
5044     if (TA1 == CTA9 && // src and dst are both char 9
5045         TA2 == CTA9 &&
5046 #else
5047     if (e -> TA1 == CTA9 && // src and dst are both char 9
5048         e -> TA2 == CTA9 &&
5049 #endif
5050         e -> N1 == 0 && // the source is entirely fill
5051         e -> N2 % 4 == 0 && // a whole number of words in the dest
5052         e -> CN1 == 0 &&  // and it starts at a word boundary // BITNO?
5053         e -> CN2 == 0)
5054       {
5055         sim_debug (DBG_TRACEEXT, & cpu_dev, "MRL special case #2\r\n");
5056         word36 w = (word36) fill |
5057                   ((word36) fill << 9) |
5058                   ((word36) fill << 18) |
5059                   ((word36) fill << 27);
5060         uint limit = e -> N2;
5061         for ( ; cpu.du.CHTALLY < e -> N2; cpu.du.CHTALLY += 4)
5062           {
5063             tandd_eis_sample (cpup, cpu.du.CHTALLY, TDPER (e -> TA2));
5064             uint n = (limit - cpu.du.CHTALLY - 1) / 4;
5065             EISWriteIdx (cpup, & e -> ADDR2, n, w, true);
5066           }
5067         cleanupOperandDescriptor (cpup, 1);
5068         cleanupOperandDescriptor (cpup, 2);
5069         // truncation fault check does need to be checked for here since
5070         // it is known that N1 <= N2
5071         CLR_I_TRUNC;
5072         return;
5073       }
5074 
5075     for ( ; cpu.du.CHTALLY < min (e -> N1, e -> N2); cpu.du.CHTALLY ++)
5076       {
5077         tandd_eis_sample (cpup, cpu.du.CHTALLY, TDPER (e -> TA2));
5078         word9 c = EISget469 (cpup, 1, e -> N1 - cpu.du.CHTALLY - 1); // get src char
5079         word9 cout = 0;
5080 
5081 #if defined(EIS_PTR3)
5082         if (TA1 == TA2)
5083 #else
5084         if (e -> TA1 == e -> TA2)
5085 #endif
5086           EISput469 (cpup, 2, e -> N2 - cpu.du.CHTALLY - 1, c);
5087         else
5088           {
5089             // If data types are dissimilar (TA1 ≠ TA2), each character is
5090             // high-order truncated or zero filled, as appropriate, as it is
5091             // moved. No character conversion takes place.
5092             cout = c;
5093             switch (srcSZ)
5094               {
5095                 case 6:
5096                   switch(dstSZ)
5097                     {
5098                       case 4:
5099                         cout = c & 017;    // truncate upper 2-bits
5100                         break;
5101                       case 9:
5102                         break;              // should already be 0-filled
5103                     }
5104                   break;
5105                 case 9:
5106                   switch(dstSZ)
5107                     {
5108                       case 4:
5109                         cout = c & 017;    // truncate upper 5-bits
5110                         break;
5111                       case 6:
5112                         cout = c & 077;    // truncate upper 3-bits
5113                         break;
5114                     }
5115                   break;
5116               }
5117 
5118             // If N1 < N2, C(FILL)0 = 1, TA1 = 1, and TA2 = 2 (6-4 move), then
5119             // C(Y-charn1)N1-1 is examined for a GBCD overpunch sign. If a
5120             // negative overpunch sign is found, then the minus sign character
5121             // is placed in C(Y-charn2)N2-1; otherwise, a plus sign character
5122             // is placed in C(Y-charn2)N2-1.
5123 
5124 // ISOLTS 838 01f, RJ78 p.11-154 - the rightmost digit is examined for overpunch.
5125             if (ovp && (cpu.du.CHTALLY == 0))
5126               {
5127                 // C(FILL)0 = 1 means that there *is* an overpunch char here.
5128                 isGBCDOvp (c, & isNeg);
5129               }
5130             EISput469 (cpup, 2, e -> N2 - cpu.du.CHTALLY - 1, cout);
5131           }
5132       }
5133 
5134     // If N1 < N2, then for i = N1+1, N1+2, ..., N2
5135     //    C(FILL) -> C(Y-charn2)N2-i
5136     // If N1 < N2 and TA2 = 2 (4-bit data) or 1 (6-bit data), then FILL
5137     // characters are high-order truncated as they are moved to C(Y-charn2). No
5138     // character conversion takes place.
5139 
5140     if (e -> N1 < e -> N2)
5141       {
5142         for ( ; cpu.du.CHTALLY < e -> N2 ; cpu.du.CHTALLY ++)
5143           {
5144             tandd_eis_sample (cpup, cpu.du.CHTALLY, TDPER (e -> TA2));
5145             // if there's an overpunch then the sign will be the last of the fill
5146             if (ovp && (cpu.du.CHTALLY == e -> N2 - 1))
5147               {
5148                 if (isNeg)
5149                   EISput469 (cpup, 2, e -> N2 - cpu.du.CHTALLY - 1, 015); // 015 is decimal -
5150                 else
5151                   EISput469 (cpup, 2, e -> N2 - cpu.du.CHTALLY - 1, 014); // 014 is decimal +
5152               }
5153             else
5154               {
5155                  EISput469 (cpup, 2, e -> N2 - cpu.du.CHTALLY - 1, fillT);
5156               }
5157           }
5158     }
5159     cleanupOperandDescriptor (cpup, 1);
5160     cleanupOperandDescriptor (cpup, 2);
5161 
5162     if (e -> N1 > e -> N2)
5163       {
5164         SET_I_TRUNC;
5165         if (T && ! TST_I_OMASK)
5166           doFault (FAULT_OFL, fst_zero, "mrl truncation fault");
5167       }
5168     else
5169       CLR_I_TRUNC;
5170   }
5171 
5172 // decimalZero
5173 //
5174 //  Try 1:
5175 //
5176 // This makes MVE 1-28 and all of MVNE work
5177 // Fails MVE 29-37 (6->6)
5178 //    #define decimalZero (e->srcTA != CTA4 ? '0' : 0)
5179 //
5180 // Try 2
5181 // This makes MVE 1-10 and 20-37 and all of MVNE work
5182 // Fails MVE 11-19 (6->9)
5183 //    #define decimalZero (e->srcTA == CTA9 ? '0' : 0)
5184 //
5185 // Try 4
5186 //
5187 #define isDecimalZero(c) ((e->srcTA == CTA9) ? \
5188                           ((c) == '0') : \
5189                           (((c) & 017) == 0))
5190 
5191 /*
5192  * Load the entire sending string number (maximum length 63 characters) into
5193  * the decimal unit input buffer as 4-bit digits (high-order truncating 9-bit
5194  * data). Strip the sign and exponent characters (if any), put them aside into
5195  * special holding registers and decrease the input buffer count accordingly.
5196  */
5197 
5198 static void EISloadInputBufferNumeric (cpu_state_t * cpup, int k)
     /* [previous][next][first][last][top][bottom][index][help] */
5199 {
5200     EISstruct * e = & cpu.currentEISinstruction;
5201 
5202     word9 *p = e->inBuffer; // p points to position in inBuffer where 4-bit chars are stored
5203     (void)memset(e->inBuffer, 0, sizeof(e->inBuffer));   // initialize to all 0's
5204 
5205     int pos = (int) e->CN[k-1];
5206 
5207     int TN = (int) e->TN[k-1];
5208     int S = (int) e->S[k-1];  // This is where MVNE gets really nasty.
5209     // I spit on the designers of this instruction set (and of COBOL.) >Ptui!<
5210 
5211     int N = (int) e->N[k-1];  // number of chars in src string
5212 
5213     EISaddr *a = &e->addr[k-1];
5214 
5215     e->sign = 1;
5216     e->exponent = 0;
5217 
5218     for(int n = 0 ; n < N ; n += 1)
5219     {
5220         word9 c = EISget49(cpup, a, &pos, TN);
5221         sim_debug (DBG_TRACEEXT, & cpu_dev, "src: %d: %o\r\n", n, c);
5222 
5223         /*
5224          * Here we need to distinguish between 4 type of numbers.
5225          *
5226          * CSFL - Floating-point, leading sign
5227          * CSLS - Scaled fixed-point, leading sign
5228          * CSTS - Scaled fixed-point, trailing sign
5229          * CSNS - Scaled fixed-point, unsigned
5230          */
5231         switch(S)
5232         {
5233             case CSFL:  // this is the real evil one ....
5234                 // Floating-point:
5235                 // [sign=c0] c1x10(n-3) + c2x10(n-4) + ... + c(n-3) [exponent=8
5236                 // bits]
5237                 //
5238                 // where:
5239                 //
5240                 //  ci is the decimal value of the byte in the ith byte
5241                 //  position.
5242                 //
5243                 //  [sign=ci] indicates that ci is interpreted as a sign byte.
5244                 //
5245                 //  [exponent=8 bits] indicates that the exponent value is
5246                 //  taken from the last 8 bits of the string. If the data is in
5247                 //  9-bit bytes, the exponent is bits 1-8 of c(n-1). If the
5248                 //  data is in 4- bit bytes, the exponent is the binary value
5249                 //  of the concatenation of c(n-2) and c(n-1).
5250 
5251                 if (n == 0) // first had better be a sign ....
5252                 {
5253                     c &= 0xf;   // hack off all but lower 4 bits
5254 
5255                     if (c < 012 || c > 017) //-V560
5256                       doFault (FAULT_IPR,
5257                                fst_ill_dig,
5258                                "loadInputBufferNumeric(1): illegal char in "
5259                                "input");
5260 
5261                     if (c == 015)   // '-'
5262                         e->sign = -1;
5263 
5264                     e->srcTally -= 1;   // 1 less source char
5265                 }
5266                 else if (TN == CTN9 && n == N-1)    // the 9-bit exponent (of which only 8-bits are used)
5267                 {
5268                     e->exponent = (signed char)(c & 0377); // want to do a sign extend
5269                     e->srcTally -= 1;   // 1 less source char
5270                 }
5271                 else if (TN == CTN4 && n == N-2)    // the 1st 4-chars of the 8-bit exponent
5272                 {
5273                     e->exponent = (c & 0xf);// << 4;
5274                     e->exponent <<= 4;
5275                     e->srcTally -= 1;   // 1 less source char
5276                 }
5277                 else if (TN == CTN4 && n == N-1)    // the 2nd 4-chars of the 8-bit exponent
5278                 {
5279                     e->exponent |= (c & 0xf);
5280 
5281                     signed char ce = (signed char) (e->exponent & 0xff);
5282                     e->exponent = ce;
5283 
5284                     e->srcTally -= 1;   // 1 less source char
5285                 }
5286                 else
5287                 {
5288                     c &= 0xf;   // hack off all but lower 4 bits
5289                     if (c > 011)                        // TODO: generate ill proc fault
5290                         doFault(FAULT_IPR, fst_ill_dig, "loadInputBufferNumeric(2): illegal char in input");
5291 
5292                     *p++ = c; // store 4-bit char in buffer
5293                 }
5294                 break;
5295 
5296             case CSLS:
5297                 // Only the byte values [0,11]8 are legal in digit positions
5298                 //    and only the byte values [12,17]8 are legal in sign positions.
5299                 //    Detection of an illegal byte value causes an illegal procedure fault
5300                 c &= 0xf;   // hack off all but lower 4 bits
5301 
5302                 if (n == 0) // first had better be a sign ....
5303                 {
5304                     if (c < 012 || c > 017) //-V560     // TODO: generate ill proc fault
5305                         doFault(FAULT_IPR, fst_ill_dig, "loadInputBufferNumeric(3): illegal char in input");
5306 
5307                     if (c == 015)  // '-'
5308                         e->sign = -1;
5309                     e->srcTally -= 1;   // 1 less source char
5310                 }
5311                 else
5312                 {
5313                     if (c > 011) //-V560
5314                         doFault(FAULT_IPR, fst_ill_dig, "loadInputBufferNumeric(4): illegal char in input");
5315                     *p++ = c; // store 4-bit char in buffer
5316                 }
5317                 break;
5318 
5319             case CSTS:
5320                 c &= 0xf;   // hack off all but lower 4 bits
5321 
5322                 if (n == N-1) // last had better be a sign ....
5323                 {
5324                     if (c < 012 || c > 017) //-V560
5325                          doFault(FAULT_IPR, fst_ill_dig, "loadInputBufferNumeric(5): illegal char in input");
5326                     if (c == 015)   // '-'
5327                         e->sign = -1;
5328                     e->srcTally -= 1;   // 1 less source char
5329                 }
5330                 else
5331                 {
5332                     if (c > 011)
5333                         doFault(FAULT_IPR, fst_ill_dig, "loadInputBufferNumeric(6): illegal char in input");
5334                     *p++ = c; // store 4-bit char in buffer
5335                 }
5336                 break;
5337 
5338             case CSNS:
5339                 c &= 0xf; // hack off all but lower 4 bits
5340 
5341                 *p++ = c; // the "easy" one
5342                 break;
5343         }
5344     }
5345     if_sim_debug (DBG_TRACEEXT, & cpu_dev)
5346       {
5347         sim_debug (DBG_TRACEEXT, & cpu_dev, "inBuffer:");
5348         for (word9 *q = e->inBuffer; q < p; q ++)
5349           sim_debug (DBG_TRACEEXT, & cpu_dev, " %02o", * q);
5350         sim_debug (DBG_TRACEEXT, & cpu_dev, "\r\n");
5351       }
5352 }
5353 
5354 /*
5355  * Load decimal unit input buffer with sending string characters. Data is read
5356  * from main memory in unaligned units (not modulo 8 boundary) of Y-block8
5357  * words. The number of characters loaded is the minimum of the remaining
5358  * sending string count, the remaining receiving string count, and 64.
5359  */
5360 
5361 static void EISloadInputBufferAlphnumeric (cpu_state_t * cpup, int k)
     /* [previous][next][first][last][top][bottom][index][help] */
5362   {
5363     EISstruct * e = & cpu.currentEISinstruction;
5364     // p points to position in inBuffer where 4-bit chars are stored
5365     word9 * p = e -> inBuffer;
5366     (void)memset (e -> inBuffer, 0, sizeof (e -> inBuffer)); // initialize to all 0's
5367 
5368     // minimum of the remaining sending string count, the remaining receiving
5369     // string count, and 64.
5370     // uint N = min3 (e -> N1, e -> N3, 64);
5371     // The above AL39 rule doesn't work with IGN and possibly other MOPs as
5372     // well. Workaround: Load all source, but max 63 characters (as RJ78
5373     // suggests).
5374     uint N = min (e-> N1, 63);
5375 
5376     for (uint n = 0 ; n < N ; n ++)
5377       {
5378         word9 c = EISget469 (cpup, k, n);
5379         * p ++ = c;
5380       }
5381 }
5382 
5383 static void EISwriteOutputBufferToMemory (cpu_state_t * cpup, int k)
     /* [previous][next][first][last][top][bottom][index][help] */
5384   {
5385     EISstruct * e = & cpu.currentEISinstruction;
5386 
5387     for (uint n = 0 ; n < (uint) e -> dstTally; n ++)
5388       {
5389         word9 c49 = e -> outBuffer [n];
5390         EISput469 (cpup, k, n, c49);
5391       }
5392   }
5393 
5394 static void writeToOutputBuffer (cpu_state_t *cpup, word9 **dstAddr, int szSrc, int szDst, word9 c49)
     /* [previous][next][first][last][top][bottom][index][help] */
5395 {
5396     EISstruct * e = & cpu.currentEISinstruction;
5397     // 4. If an edit insertion table entry or MOP insertion character is to be
5398     // stored, ANDed, or ORed into a receiving string of 4- or 6-bit
5399     // characters, high-order truncate the character accordingly.
5400     // 5. (MVNE) If the receiving string is 9-bit characters, high-order fill
5401     // the (4-bit) digits from the input buffer with bits 0-4 of character 8 of
5402     // the edit insertion table. If the receiving string is 6-bit characters,
5403     // high-order fill the digits with "00"b.
5404 
5405     if (e -> mvne)
5406       {
5407         switch (szSrc)   // XXX according to rule 1 of Numeric Edit,
5408                          // input should be 4bits. this needs cleaning
5409           {
5410             case 4:
5411               switch (szDst)
5412                 {
5413                   case 4:
5414                     ** dstAddr = c49 & 0xf;
5415                     break;
5416                   case 6:
5417                     ** dstAddr = c49 & 077;   // high-order fill the digits with "00"b.
5418                     break;
5419                   case 9:
5420                     ** dstAddr = c49 | (e -> editInsertionTable [7] & 0760);
5421                     break;
5422                 }
5423               break;
5424             case 6:
5425               switch (szDst)
5426                 {
5427                   case 4:
5428                     ** dstAddr = c49 & 0xf;    // write only-4-bits
5429                     break;
5430                   case 6:
5431                     ** dstAddr = c49; //-V1037 // XXX is this safe? shouldn't it be & 077 ???
5432                     break;
5433                   case 9:
5434                     ** dstAddr = c49; //-V1037
5435                     break;
5436                 }
5437               break;
5438             // Note: case szSrc == 9 is also used for writing edit table
5439             // entries and MOP insertion characters which shall NOT be
5440             // transformed by rule 5
5441             case 9:
5442               switch(szDst)
5443                 {
5444                   case 4:
5445                     ** dstAddr = c49 & 0xf;    // write only-4-bits
5446                     break;
5447                   case 6:
5448                     ** dstAddr = c49 & 077;   // write only 6-bits
5449                     break;
5450                   case 9:
5451                     ** dstAddr = c49;
5452                     break;
5453                 }
5454               break;
5455           }
5456       }
5457     else // mve
5458       {
5459         switch(szDst)
5460           {
5461             case 4:
5462               ** dstAddr = c49 & 0xf;    // write only-4-bits
5463               break;
5464             case 6:
5465               ** dstAddr = c49 & 077;   // write only 6-bits
5466               break;
5467             case 9:
5468               ** dstAddr = c49;
5469               break;
5470           }
5471       }
5472     e->dstTally -= 1;
5473     *dstAddr += 1;
5474 }
5475 
5476 /*!
5477  * This is the Micro Operation Executor/Interpreter
5478  */
5479 
5480 static char* defaultEditInsertionTable = " *+-$,.0";
5481 
5482 // Edit Flags
5483 //
5484 // The processor provides the following four edit flags for use by the micro
5485 // operations.
5486 //
5487 // bool mopES = false; // End Suppression flag; initially OFF, set ON by a
5488 // micro operation when zero-suppression ends.
5489 //
5490 // bool mopSN = false;
5491 // Sign flag; initially set OFF if the sending string has an alphanumeric
5492 // descriptor or an unsigned numeric descriptor. If the sending string has a
5493 // signed numeric descriptor, the sign is initially read from the sending
5494 // string from the digit position defined by the sign and the decimal type
5495 // field (S); SN is set OFF if positive, ON if negative. If all digits are
5496 // zero, the data is assumed positive and the SN flag is set OFF, even when the
5497 // sign is negative.
5498 //
5499 //bool mopBZ = false; i
5500 // Blank-when-zero flag; initially set OFF and set ON by either the ENF or SES
5501 // micro operation. If, at the completion of a move (L1 exhausted), both the Z
5502 // and BZ flags are ON, the receiving string is filled with character 1 of the
5503 // edit insertion table.
5504 
5505 /*!
5506  * CHT Micro Operation - Change Table
5507  * EXPLANATION: The edit insertion table is replaced by the string of eight
5508  * 9-bit characters immediately following the CHT micro operation.
5509  * FLAGS: None affected
5510  * NOTE: C(IF) is not interpreted for this operation.
5511  */
5512 
5513 static int mopCHT (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
5514 {
5515     EISstruct * e = & cpu.currentEISinstruction;
5516     (void)memset(&e->editInsertionTable, 0, sizeof(e->editInsertionTable)); // XXX do we really need this?
5517     for(int i = 0 ; i < 8 ; i += 1)
5518     {
5519         if (e->mopTally == 0)
5520         {
5521             e->_faults |= FAULT_IPR;
5522             break;
5523         }
5524 #if defined(EIS_PTR2)
5525         word9 entry = EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9);  // get mop table entries
5526 #else
5527         word9 entry = EISget49(cpup, e->mopAddress, &e->mopPos, CTN9);  // get mop table entries
5528 #endif
5529         e->editInsertionTable[i] = entry & 0777;            // keep to 9-bits
5530         e->mopTally -= 1;
5531     }
5532     return 0;
5533 }
5534 
5535 /*!
5536  * ENF Micro Operation - End Floating Suppression
5537  * EXPLANATION:
5538  *  Bit 0 of IF, IF(0), specifies the nature of the floating suppression. Bit 1
5539  *  of IF, IF(1), specifies if blank when zero option is used.
5540  * For IF(0) = 0 (end floating-sign operation),
5541  * - If ES is OFF and SN is OFF, then edit insertion table entry 3 is moved to
5542  * the receiving field and ES is set ON.
5543  * - If ES is OFF and SN is ON, then edit insertion table entry 4 is moved to
5544  * the receiving field and ES is set ON.
5545  * - If ES is ON, no action is taken.
5546  * For IF(0) = 1 (end floating currency symbol operation),
5547  * - If ES is OFF, then edit insertion table entry 5 is moved to the receiving
5548  * field and ES is set ON.
5549  * - If ES is ON, no action is taken.
5550  * For IF(1) = 1 (blank when zero): the BZ flag is set ON. For IF(1) = 0 (no
5551  * blank when zero): no action is taken.
5552  * FLAGS: (Flags not listed are not affected)
5553  *      ES - If OFF, then set ON
5554  *      BZ - If bit 1 of C(IF) = 1, then set ON; otherwise, unchanged
5555  */
5556 
5557 static int mopENF (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
5558 {
5559     EISstruct * e = & cpu.currentEISinstruction;
5560     // For IF(0) = 0 (end floating-sign operation),
5561     if (!(e->mopIF & 010))
5562     {
5563         // If ES is OFF and SN is OFF, then edit insertion table entry 3 is moved to the receiving field and ES is set ON.
5564         if (!e->mopES && !e->mopSN)
5565         {
5566             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[2]);
5567             e->mopES = true;
5568         }
5569         // If ES is OFF and SN is ON, then edit insertion table entry 4 is moved to the receiving field and ES is set ON.
5570         if (!e->mopES && e->mopSN)
5571         {
5572             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[3]);
5573             e->mopES = true;
5574         }
5575         // If ES is ON, no action is taken.
5576     } else { // IF(0) = 1 (end floating currency symbol operation),
5577         if (!e->mopES)
5578         {
5579             // If ES is OFF, then edit insertion table entry 5 is moved to the receiving field and ES is set ON.
5580             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[4]);
5581             e->mopES = true;
5582         }
5583         // If ES is ON, no action is taken.
5584     }
5585 
5586     // For IF(1) = 1 (blank when zero): the BZ flag is set ON. For IF(1) = 0 (no blank when zero): no action is taken.
5587     if (e->mopIF & 04)
5588         e->mopBZ = true;
5589 
5590     return 0;
5591 }
5592 
5593 /*!
5594  * IGN Micro Operation - Ignore Source Characters
5595  * EXPLANATION:
5596  * IF specifies the number of characters to be ignored, where IF = 0 specifies
5597  * 16 characters.
5598  * The next IF characters in the source data field are ignored and the sending
5599  * tally is reduced accordingly.
5600  * FLAGS: None affected
5601  */
5602 
5603 static int mopIGN (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
5604 {
5605     EISstruct * e = & cpu.currentEISinstruction;
5606 // AL-39 doesn't specify the == 0 test, but NovaScale does;
5607 // also ISOLTS ps830 test-04a seems to rely on it.
5608     if (e->mopIF == 0)
5609         e->mopIF = 16;
5610 
5611     for(int n = 0 ; n < e->mopIF ; n += 1)
5612     {
5613         if (e->dstTally == 0)
5614             break;
5615         if (e->srcTally == 0)
5616         {
5617             // IGN doesn't allow BZ, raise IPR straight away
5618             e->_faults |= FAULT_IPR;
5619             break;
5620         }
5621 
5622         e->srcTally -= 1;
5623         e->in += 1;
5624     }
5625     return 0;
5626 }
5627 
5628 /*!
5629  * INSA Micro Operation - Insert Asterisk on Suppression
5630  * EXPLANATION:
5631  * This MOP is the same as INSB except that if ES is OFF, then edit insertion
5632  * table entry 2 is moved to the receiving field.
5633  * FLAGS: None affected
5634  * NOTE: If C(IF) = 9-15, an IPR fault occurs.
5635  */
5636 
5637 static int mopINSA (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
5638 {
5639     EISstruct * e = & cpu.currentEISinstruction;
5640     // If C(IF) = 9-15, an IPR fault occurs.
5641     if (e->mopIF >= 9 /* && e->mopIF <= 15 */)
5642     {
5643         e->_faults |= FAULT_IPR;
5644         return 0;
5645     }
5646 
5647 
5648     // If ES is OFF, then edit insertion table entry 1 is moved to the
5649     // receiving field. If IF = 0, then the next 9 bits are also skipped. If IF
5650     // is not 0, the next 9 bits are treated as a MOP.
5651 
5652     if (!e->mopES)
5653       {
5654         writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[1]);
5655 
5656         if (e->mopIF == 0)
5657           {
5658             if (e->mopTally == 0)
5659               {
5660                 e->_faults |= FAULT_IPR;
5661                 return 0;
5662               }
5663 # if defined(EIS_PTR2)
5664             EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9);
5665 # else
5666             EISget49(cpup, e->mopAddress, &e->mopPos, CTN9);
5667 # endif
5668             e->mopTally -= 1;
5669           }
5670       }
5671 
5672     // If ES is ON and IF = 0, then the 9-bit character immediately following
5673     // the INSB micro-instruction is moved to the receiving field
5674     else
5675       {
5676         if (e->mopIF == 0)
5677           {
5678             if (e->mopTally == 0)
5679               {
5680                 e->_faults |= FAULT_IPR;
5681                 return 0;
5682               }
5683 # if defined(EIS_PTR2)
5684             word9 c = EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9);
5685 # else
5686             word9 c = EISget49(cpup, e->mopAddress, &e->mopPos, CTN9);
5687 # endif
5688             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, c);
5689             e->mopTally -= 1;
5690           }
5691     // If ES is ON and IF<>0, then IF specifies which edit insertion table
5692     // entry (1-8) is to be moved to the receiving field.
5693         else
5694           {
5695             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[e->mopIF-1]);
5696           }
5697       }
5698 
5699 
5700 
5701 
5702 
5703 
5704 
5705 
5706 
5707 
5708 
5709 
5710 
5711 
5712 
5713 
5714 
5715 
5716 
5717 
5718 
5719 
5720 
5721 
5722 
5723 
5724 
5725 
5726 
5727 
5728 
5729 
5730 
5731 
5732 
5733 
5734 
5735 
5736 
5737 
5738 
5739 
5740 
5741 
5742 
5743 
5744 
5745 
5746 
5747     return 0;
5748 }
5749 
5750 /*!
5751  * INSB Micro Operation - Insert Blank on Suppression
5752  * EXPLANATION:
5753  * IF specifies which edit insertion table entry is inserted.
5754  * If IF = 0, the 9 bits immediately following the INSB micro operation are
5755  * treated as a 9-bit character (not a MOP) and are moved or skipped according
5756  * to ES.
5757  * - If ES is OFF, then edit insertion table entry 1 is moved to the receiving
5758  * field. If IF = 0, then the next 9 bits are also skipped. If IF is not 0, the
5759  * next 9 bits are treated as a MOP.
5760  * - If ES is ON and IF = 0, then the 9-bit character immediately following the
5761  * INSB micro-instruction is moved to the receiving field.
5762  * - If ES is ON and IF<>0, then IF specifies which edit insertion table entry
5763  * (1-8) is to be moved to the receiving field.
5764  * FLAGS: None affected
5765  * NOTE: If C(IF) = 9-15, an IPR fault occurs.
5766  */
5767 
5768 static int mopINSB (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
5769 {
5770     EISstruct * e = & cpu.currentEISinstruction;
5771     // If C(IF) = 9-15, an IPR fault occurs.
5772     if (e->mopIF >= 9 /* && e->mopIF <= 15 */)
5773     {
5774         e->_faults |= FAULT_IPR;
5775         return 0;
5776     }
5777 
5778     if (!e->mopES)
5779     {
5780         // If ES is OFF, then edit insertion table entry 1 is moved to the
5781         // receiving field. If IF = 0, then the next 9 bits are also skipped.
5782         // If IF is not 0, the next 9 bits are treated as a MOP.
5783         writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
5784 
5785         if (e->mopIF == 0)
5786         {
5787             if (e->mopTally == 0)
5788             {
5789                 e->_faults |= FAULT_IPR;
5790                 return 0;
5791             }
5792 #if defined(EIS_PTR2)
5793             EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9);
5794 #else
5795             EISget49(cpup, e->mopAddress, &e->mopPos, CTN9);
5796 #endif
5797             e->mopTally -= 1;
5798         }
5799 
5800     } else {
5801         // ES is ON
5802 
5803         // If C(IF) != 0
5804         if (e->mopIF)
5805         {
5806             // If ES is ON and IF<>0, then IF specifies which edit
5807             // insertion table entry (1-8) is to be moved to the receiving
5808             // field.
5809             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[e->mopIF - 1]);
5810         } else {
5811             // If ES is ON and IF = 0, then the 9-bit character immediately
5812             // following the INSB micro-instruction is moved to the
5813             // receiving field.
5814             if (e->mopTally == 0)
5815             {
5816                 e->_faults |= FAULT_IPR;
5817                 return 0;
5818             }
5819 #if defined(EIS_PTR2)
5820             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9));
5821             //EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9);
5822 #else
5823             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, EISget49(cpup, e->mopAddress, &e->mopPos, CTN9));
5824             //EISget49(cpup, e->mopAddress, &e->mopPos, CTN9);
5825 #endif
5826             e->mopTally -= 1;
5827 
5828         }
5829     }
5830     return 0;
5831 }
5832 
5833 /*!
5834  * INSM Micro Operation - Insert Table Entry One Multiple
5835  * EXPLANATION:
5836  * IF specifies the number of receiving characters affected, where IF = 0
5837  * specifies 16 characters.
5838  * Edit insertion table entry 1 is moved to the next IF (1-16) receiving field
5839  * characters.
5840  * FLAGS: None affected
5841  */
5842 
5843 static int mopINSM (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
5844 {
5845     EISstruct * e = & cpu.currentEISinstruction;
5846     if (e->mopIF == 0)
5847         e->mopIF = 16;
5848     for(int n = 0 ; n < e->mopIF ; n += 1)
5849     {
5850         if (e->dstTally == 0)
5851           break;
5852         writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
5853     }
5854     return 0;
5855 }
5856 
5857 /*!
5858  * INSN Micro Operation - Insert on Negative
5859  * EXPLANATION:
5860  * IF specifies which edit insertion table entry is inserted. If IF = 0, the 9
5861  * bits immediately following the INSN micro operation are treated as a 9-bit
5862  * character (not a MOP) and are moved or skipped according to SN.
5863  * - If SN is OFF, then edit insertion table entry 1 is moved to the receiving
5864  * field. If IF = 0, then the next 9 bits are also skipped. If IF is not 0, the
5865  * next 9 bits are treated as a MOP.
5866  * - If SN is ON and IF = 0, then the 9-bit character immediately following the
5867  * INSN micro-instruction is moved to the receiving field.
5868  * - If SN is ON and IF <> 0, then IF specifies which edit insertion table
5869  * entry (1-8) is to be moved to the receiving field.
5870  * FLAGS: None affected
5871  * NOTE: If C(IF) = 9-15, an IPR fault occurs.
5872  */
5873 
5874 static int mopINSN (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
5875 {
5876     EISstruct * e = & cpu.currentEISinstruction;
5877     // If C(IF) = 9-15, an IPR fault occurs.
5878     if (e->mopIF >= 9 /* && e->mopIF <= 15 */)
5879     {
5880         e->_faults |= FAULT_IPR;
5881         return 0;
5882     }
5883 
5884     // If IF = 0, the 9 bits immediately following the INSN micro operation are
5885     // treated as a 9-bit character (not a MOP) and are moved or skipped
5886     // according to SN.
5887 
5888     if (e->mopIF == 0)
5889     {
5890         if (e->mopTally == 0)
5891         {
5892             e->_faults |= FAULT_IPR;
5893             return 0;
5894         }
5895       if (!e->mopSN)
5896         {
5897             //If SN is OFF, then edit insertion table entry 1 is moved to the
5898             //receiving field. If IF = 0, then the next 9 bits are also
5899             //skipped. If IF is not 0, the next 9 bits are treated as a MOP.
5900 #if defined(EIS_PTR2)
5901             EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9);
5902 #else
5903             EISget49(cpup, e->mopAddress, &e->mopPos, CTN9);
5904 #endif
5905             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
5906             e->mopTally -= 1;
5907         } else {
5908             // If SN is ON and IF = 0, then the 9-bit character immediately
5909             // following the INSN micro-instruction is moved to the receiving
5910             // field.
5911 #if defined(EIS_PTR2)
5912             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9));
5913 #else
5914             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, EISget49(cpup, e->mopAddress, &e->mopPos, CTN9));
5915 #endif
5916 
5917             e->mopTally -= 1;
5918         }
5919     }
5920     else
5921     {
5922         if (e->mopSN)
5923         {
5924             //If SN is ON and IF <> 0, then IF specifies which edit insertion
5925             //table entry (1-8) is to be moved to the receiving field.
5926             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[e->mopIF - 1]);
5927         } else {
5928             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
5929         }
5930     }
5931     return 0;
5932 }
5933 
5934 /*!
5935  * INSP Micro Operation - Insert on Positive
5936  * EXPLANATION:
5937  * INSP is the same as INSN except that the responses for the SN values are
5938  * reversed.
5939  * FLAGS: None affected
5940  * NOTE: If C(IF) = 9-15, an IPR fault occurs.
5941  */
5942 
5943 static int mopINSP (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
5944 {
5945     EISstruct * e = & cpu.currentEISinstruction;
5946     // If C(IF) = 9-15, an IPR fault occurs.
5947     if (e->mopIF >= 9 /* && e->mopIF <= 15 */)
5948     {
5949         e->_faults |= FAULT_IPR;
5950         return 0;
5951     }
5952 
5953     if (e->mopIF == 0)
5954     {
5955         if (e->mopTally == 0)
5956         {
5957             e->_faults |= FAULT_IPR;
5958             return 0;
5959         }
5960         if (e->mopSN)
5961         {
5962 #if defined(EIS_PTR2)
5963             EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9);
5964 #else
5965             EISget49(cpup, e->mopAddress, &e->mopPos, CTN9);
5966 #endif
5967             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
5968             e->mopTally -= 1;
5969         } else {
5970 #if defined(EIS_PTR2)
5971             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9));
5972 #else
5973             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, EISget49(cpup, e->mopAddress, &e->mopPos, CTN9));
5974 #endif
5975             e->mopTally -= 1;
5976         }
5977     }
5978     else
5979     {
5980         if (!e->mopSN)
5981         {
5982             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[e->mopIF - 1]);
5983         } else {
5984             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
5985         }
5986     }
5987 
5988     return 0;
5989 }
5990 
5991 /*!
5992  * LTE Micro Operation - Load Table Entry
5993  * EXPLANATION:
5994  * IF specifies the edit insertion table entry to be replaced.
5995  * The edit insertion table entry specified by IF is replaced by the 9-bit
5996  * character immediately following the LTE microinstruction.
5997  * FLAGS: None affected
5998  * NOTE: If C(IF) = 0 or C(IF) = 9-15, an Illegal Procedure fault occurs.
5999  */
6000 
6001 static int mopLTE (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6002 {
6003     EISstruct * e = & cpu.currentEISinstruction;
6004     if (e->mopIF == 0 || (e->mopIF >= 9 && e->mopIF <= 15))
6005     {
6006         e->_faults |= FAULT_IPR;
6007         return 0;
6008     }
6009     if (e->mopTally == 0)
6010     {
6011         e->_faults |= FAULT_IPR;
6012         return 0;
6013     }
6014 #if defined(EIS_PTR2)
6015     word9 next = EISget49(cpup, &e->ADDR2, &e->mopPos, CTN9);
6016 #else
6017     word9 next = EISget49(cpup, e->mopAddress, &e->mopPos, CTN9);
6018 #endif
6019     e->mopTally -= 1;
6020 
6021     //e->editInsertionTable[e->mopIF - 1] = next;
6022     if (e->mopIF - 1 >= 0 &&
6023         e->mopIF - 1 < sizeof(e->editInsertionTable) / sizeof(e->editInsertionTable[0])) {
6024           e->editInsertionTable[e->mopIF - 1] = next;
6025     } else {
6026         e->_faults |= FAULT_IPR;
6027         sim_warn("mopIF %d OOB in %s!\r\n", e->mopIF, __func__);
6028         return 0;
6029     }
6030     sim_debug (DBG_TRACEEXT, & cpu_dev, "LTE IT[%d]<=%d\r\n", e -> mopIF - 1, next);
6031     return 0;
6032 }
6033 
6034 /*!
6035  * MFLC Micro Operation - Move with Floating Currency Symbol Insertion
6036  * EXPLANATION:
6037  * IF specifies the number of characters of the sending field upon which the
6038  * operation is performed, where IF = 0 specifies 16 characters.
6039  * Starting with the next available sending field character, the next IF
6040  * characters are individually fetched and the following conditional actions
6041  * occur.
6042  * - If ES is OFF and the character is zero, edit insertion table entry 1 is
6043  * moved to the receiving field in place of the character.
6044  * - If ES is OFF and the character is not zero, then edit insertion table
6045  * entry 5 is moved to the receiving field, the character is also moved to the
6046  * receiving field, and ES is set ON.
6047  * - If ES is ON, the character is moved to the receiving field.
6048  * The number of characters placed in the receiving field is data-dependent. If
6049  * the entire sending field is zero, IF characters are placed in the receiving
6050  * field. However, if the sending field contains a nonzero character, IF+1
6051  * characters (the insertion character plus the characters from the sending
6052  * field) are placed in the receiving field.
6053  * An IPR fault occurs when the sending field is exhausted before the receiving
6054  * field is filled. In order to provide space in the receiving field for an
6055  * inserted currency symbol, the receiving field must have a string length one
6056  * character longer than the sending field. When the sending field is all
6057  * zeros, no currency symbol is inserted by the MFLC micro operation and the
6058  * receiving field is not filled when the sending field is exhausted. The user
6059  * should provide an ENF (ENF,12) micro operation after a MFLC micro operation
6060  * that has as its character count the number of characters in the sending
6061  * field. The ENF micro operation is engaged only when the MFLC micro operation
6062  * fails to fill the receiving field. Then it supplies a currency symbol to
6063  * fill the receiving field and blanks out the entire field.
6064  * FLAGS: (Flags not listed are not affected.)
6065  * ES If OFF and any of C(Y) is less than decimal zero, then ON; otherwise, it
6066  * is unchanged.
6067  * NOTE: Since the number of characters moved to the receiving string is
6068  * data-dependent, a possible IPR fault may be avoided by ensuring that the Z
6069  * and BZ flags are ON.
6070  */
6071 
6072 static int mopMFLC (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6073 {
6074     EISstruct * e = & cpu.currentEISinstruction;
6075     if (e->mopIF == 0)
6076         e->mopIF = 16;
6077 
6078     //  Starting with the next available sending field character, the next IF
6079     //  characters are individually fetched and the following conditional
6080     //  actions occur.
6081     sim_debug (DBG_TRACEEXT, & cpu_dev, "MFLC IF %d, srcTally %d, dstTally %d\r\n", e->mopIF, e->srcTally, e->dstTally);
6082     for(int n = 0 ; n < e->mopIF ; n += 1)
6083     {
6084         sim_debug (DBG_TRACEEXT, & cpu_dev, "MFLC n %d, srcTally %d, dstTally %d\r\n", n, e->srcTally, e->dstTally);
6085         if (e->dstTally == 0)
6086             break;
6087         if (e->srcTally == 0)
6088             return -1;
6089         // If ES is OFF and the character is zero, edit insertion table entry 1
6090         // is moved to the receiving field in place of the character.
6091         // If ES is OFF and the character is not zero, then edit insertion
6092         // table entry 5 is moved to the receiving field, the character is also
6093         // moved to the receiving field, and ES is set ON.
6094 
6095         word9 c = *(e->in);
6096         sim_debug (DBG_TRACEEXT, & cpu_dev, "MFLC c %d (0%o)\r\n", c, c);
6097         if (!e->mopES) { // e->mopES is OFF
6098 
6099             sim_debug (DBG_TRACEEXT, & cpu_dev, "MFLC ES off\r\n");
6100             if (isDecimalZero (c)) {
6101                 sim_debug (DBG_TRACEEXT, & cpu_dev, "MFLC is zero\r\n");
6102                 // edit insertion table entry 1 is moved to the receiving field
6103                 // in place of the character.
6104                 writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
6105                 e->in += 1;
6106                 e->srcTally -= 1;
6107             } else {
6108                 sim_debug (DBG_TRACEEXT, & cpu_dev, "MFLC is not zero\r\n");
6109                 // then edit insertion table entry 5 is moved to the receiving
6110                 // field, the character is also moved to the receiving field,
6111                 // and ES is set ON.
6112                 writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[4]);
6113 
6114                 writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6115                 e->mopZ = false; // iszero() tested above.
6116                 e->in += 1;
6117                 e->srcTally -= 1;
6118 
6119                 e->mopES = true;
6120             }
6121         } else {
6122             sim_debug (DBG_TRACEEXT, & cpu_dev, "MFLC ES on\r\n");
6123             // If ES is ON, the character is moved to the receiving field.
6124             writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6125 
6126             if (! isDecimalZero (c))
6127                 e->mopZ = false;
6128             e->in += 1;
6129             e->srcTally -= 1;
6130         }
6131     }
6132 
6133     return 0;
6134 }
6135 
6136 /*!
6137  * MFLS Micro Operation - Move with Floating Sign Insertion
6138  * EXPLANATION:
6139  * IF specifies the number of characters of the sending field upon which the
6140  * operation is performed, where IF = 0 specifies 16 characters.
6141  * Starting with the next available sending field character, the next IF
6142  * characters are individually fetched and the following conditional actions
6143  * occur.
6144  * - If ES is OFF and the character is zero, edit insertion table entry 1 is
6145  * moved to the receiving field in place of the character.
6146  * - If ES is OFF, the character is not zero, and SN is OFF; then edit
6147  * insertion table entry 3 is moved to the receiving field; the character is
6148  * also moved to the receiving field, and ES is set ON.
6149  * - If ES is OFF, the character is nonzero, and SN is ON; edit insertion table
6150  * entry 4 is moved to the receiving field; the character is also moved to the
6151  * receiving field, and ES is set ON.
6152  * - If ES is ON, the character is moved to the receiving field.
6153  * The number of characters placed in the receiving field is data-dependent. If
6154  * the entire sending field is zero, IF characters are placed in the receiving
6155  * field. However, if the sending field contains a nonzero character, IF+1
6156  * characters (the insertion character plus the characters from the sending
6157  * field) are placed in the receiving field.
6158  * An IPR fault occurs when the sending field is exhausted before the receiving
6159  * field is filled. In order to provide space in the receiving field for an
6160  * inserted sign, the receiving field must have a string length one character
6161  * longer than the sending field. When the sending field is all zeros, no sign
6162  * is inserted by the MFLS micro operation and the receiving field is not
6163  * filled when the sending field is exhausted. The user should provide an ENF
6164  * (ENF,4) micro operation after a MFLS micro operation that has as its
6165  * character count the number of characters in the sending field. The ENF micro
6166  * operation is engaged only when the MFLS micro operation fails to fill the
6167  * receiving field; then, it supplies a sign character to fill the receiving
6168  * field and blanks out the entire field.
6169  *
6170  * FLAGS: (Flags not listed are not affected.)
6171  *     ES If OFF and any of C(Y) is less than decimal zero, then ON; otherwise,
6172  *     it is unchanged.
6173  * NOTE: Since the number of characters moved to the receiving string is
6174  * data-dependent, a possible Illegal Procedure fault may be avoided by
6175  * ensuring that the Z and BZ flags are ON.
6176  */
6177 
6178 static int mopMFLS (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6179 {
6180     EISstruct * e = & cpu.currentEISinstruction;
6181     if (e->mopIF == 0)
6182         e->mopIF = 16;
6183 
6184     for(int n = 0 ; n < e->mopIF; n += 1)
6185     {
6186         if (e->dstTally == 0)
6187             break;
6188         if (e->srcTally == 0)
6189             return -1;
6190 
6191         word9 c = *(e->in);
6192         sim_debug (DBG_TRACEEXT, & cpu_dev, "MFLS n %d c %o\r\n", n, c);
6193         if (!e->mopES) { // e->mopES is OFF
6194             if (isDecimalZero (c))
6195             {
6196                 // edit insertion table entry 1 is moved to the receiving field
6197                 // in place of the character.
6198                 sim_debug (DBG_TRACEEXT, & cpu_dev,
6199                            "ES is off, c is zero; edit insertion table entry 1"
6200                            " is moved to the receiving field in place of the character.\r\n");
6201                 writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
6202                 e->in += 1;
6203                 e->srcTally -= 1;
6204             } else {
6205                 // c is non-zero
6206                 if (!e->mopSN)
6207                 {
6208                     // then edit insertion table entry 3 is moved to the
6209                     // receiving field; the character is also moved to the
6210                     // receiving field, and ES is set ON.
6211                     sim_debug (DBG_TRACEEXT, & cpu_dev,
6212                                "ES is off, c is non-zero, SN is off; edit insertion table entry 3"
6213                                " is moved to the receiving field; the character is also moved to"
6214                                " the receiving field, and ES is set ON.\r\n");
6215                     writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[2]);
6216 
6217                     e->in += 1;
6218                     e->srcTally -= 1;
6219                     e->mopZ = false; // iszero tested above
6220 
6221 
6222 
6223 
6224 
6225 
6226 
6227 
6228                     writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6229 
6230                     e->mopES = true;
6231                 } else {
6232                     //  SN is ON; edit insertion table entry 4 is moved to the
6233                     //  receiving field; the character is also moved to the
6234                     //  receiving field, and ES is set ON.
6235                     sim_debug (DBG_TRACEEXT, & cpu_dev,
6236                                "ES is off, c is non-zero, SN is OFF; edit insertion table entry 4"
6237                                " is moved to the receiving field; the character is also moved to"
6238                                " the receiving field, and ES is set ON.\r\n");
6239                     writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[3]);
6240 
6241                     e->in += 1;
6242                     e->srcTally -= 1;
6243                     e->mopZ = false; // iszero tested above
6244 
6245                     writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6246 
6247                     e->mopES = true;
6248                 }
6249             }
6250         } else {
6251             // If ES is ON, the character is moved to the receiving field.
6252             sim_debug (DBG_TRACEEXT, & cpu_dev, "ES is ON, the character is moved to the receiving field.\r\n");
6253             writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6254 
6255             if (! isDecimalZero (c))
6256                 e->mopZ = false;
6257             e->in += 1;
6258             e->srcTally -= 1;
6259         }
6260     }
6261 
6262     // NOTE: Since the number of characters moved to the receiving string is
6263     // data-dependent, a possible Illegal Procedure fault may be avoided by
6264     // ensuring that the Z and BZ flags are ON.
6265 
6266     return 0;
6267 }
6268 
6269 /*!
6270  * MORS Micro Operation - Move and OR Sign
6271  * EXPLANATION:
6272  * IF specifies the number of characters of the sending field upon which the
6273  * operation is performed, where IF = 0 specifies 16 characters.
6274  * Starting with the next available sending field character, the next IF
6275  * characters are individually fetched and the following conditional actions
6276  * occur.
6277  * - If SN is OFF, the next IF characters in the source data field are moved to
6278  * the receiving data field and, during the move, edit insertion table entry 3
6279  * is ORed to each character.
6280  * - If SN is ON, the next IF characters in the source data field are moved to
6281  * the receiving data field and, during the move, edit insertion table entry 4
6282  * is ORed to each character.
6283  * MORS can be used to generate a negative overpunch for a receiving field to
6284  * be used later as a sending field.
6285  * FLAGS: None affected
6286  */
6287 
6288 static int mopMORS (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6289 {
6290     EISstruct * e = & cpu.currentEISinstruction;
6291     if (e->mopIF == 0)
6292         e->mopIF = 16;
6293 
6294     sim_debug (DBG_TRACEEXT, & cpu_dev, "MORS mopIF %d src %d dst %d\r\n", e->mopIF, e->srcTally, e->dstTally);
6295     for(int n = 0 ; n < e->mopIF ; n += 1)
6296     {
6297 // The micro operation sequence is terminated normally when the receiving
6298 // string length becomes exhausted. The micro operation sequence is terminated
6299 // abnormally (with an illegal procedure fault) if a move from an exhausted
6300 // sending string or the use of an exhausted MOP string is attempted.
6301 
6302         if (e->dstTally == 0)
6303             break;
6304         if (e->srcTally == 0)
6305             return -1;
6306 
6307         // XXX this is probably wrong regarding the ORing, but it's a start ....
6308         word9 c = (*e->in | (!e->mopSN ? e->editInsertionTable[2] : e->editInsertionTable[3]));
6309         if (! isDecimalZero (*e->in))
6310             e->mopZ = false;
6311         e->in += 1;
6312         e->srcTally -= 1;
6313 
6314         writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6315     }
6316 
6317     return 0;
6318 }
6319 
6320 /*!
6321  * MVC Micro Operation - Move Source Characters
6322  * EXPLANATION:
6323  * IF specifies the number of characters to be moved, where IF = 0 specifies 16
6324  * characters.
6325  * The next IF characters in the source data field are moved to the receiving
6326  * data field.
6327  * FLAGS: None affected
6328  */
6329 
6330 static int mopMVC (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6331 {
6332     EISstruct * e = & cpu.currentEISinstruction;
6333     if (e->mopIF == 0)
6334         e->mopIF = 16;
6335 
6336     sim_debug (DBG_TRACEEXT, & cpu_dev, "MVC mopIF %d\r\n", e->mopIF);
6337 
6338     for(int n = 0 ; n < e->mopIF ; n += 1)
6339     {
6340         sim_debug (DBG_TRACEEXT, & cpu_dev, "MVC n %d srcTally %d dstTally %d\r\n", n, e->srcTally, e->dstTally);
6341 // GD's test_float shows that data exhaustion is not a fault.
6342 //#if 0
6343         if (e->dstTally == 0)
6344             break;
6345         if (e->srcTally == 0)
6346             return -1;
6347 
6348         sim_debug (DBG_TRACEEXT, & cpu_dev, "MVC write to output buffer %o\r\n", *e->in);
6349         writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, *e->in);
6350         if (! isDecimalZero (*e->in))
6351             e->mopZ = false;
6352         e->in += 1;
6353 
6354         e->srcTally -= 1;
6355     }
6356 
6357     sim_debug (DBG_TRACEEXT, & cpu_dev, "MVC done\r\n");
6358     return 0;
6359 }
6360 
6361 /*!
6362  * MSES Micro Operation - Move and Set Sign
6363  * EXPLANATION:
6364  * IF specifies the number of characters of the sending field upon which the
6365  * operation is performed, where IF = 0 specifies 16 characters. For MVE,
6366  * starting with the next available sending field character, the next IF
6367  * characters are individually fetched and the following conditional actions
6368  * occur.
6369  * Starting with the first character during the move, a comparative AND is made
6370  * first with edit insertion table entry 3. If the result is nonzero, the first
6371  * character and the rest of the characters are moved without further
6372  * comparative ANDs. If the result is zero, a comparative AND is made between
6373  * the character being moved and edit insertion table entry 4 If that result is
6374  * nonzero, the SN indicator is set ON (indicating negative) and the first
6375  * character and the rest of the characters are moved without further
6376  * comparative ANDs. If the result is zero, the second character is treated
6377  * like the first. This process continues until one of the comparative AND
6378  * results is nonzero or until all characters are moved.
6379  * For MVNE instruction, the sign (SN) flag is already set and IF characters
6380  * are moved to the destination field (MSES is equivalent to the MVC
6381  * instruction).
6382  * FLAGS: (Flags not listed are not affected.)
6383  * SN If edit insertion table entry 4 is found in C(Y-1), then ON; otherwise,
6384  * it is unchanged.
6385  */
6386 
6387 static int mopMSES (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6388 {
6389     EISstruct * e = & cpu.currentEISinstruction;
6390     if (e->mvne == true)
6391         return mopMVC (cpup);   // XXX I think!
6392 
6393     if (e->mopIF == 0)
6394         e->mopIF = 16;
6395 
6396     int overpunch = false;
6397 
6398     for(int n = 0 ; n < e->mopIF ; n += 1)
6399     {
6400         if (e->dstTally == 0)
6401             break;
6402         if (e->srcTally == 0)
6403             return -1;
6404 
6405         //Starting with the first character during the move, a comparative AND
6406         //is made first with edit insertion table entry 3. If the result is
6407         //nonzero, the first character and the rest of the characters are moved
6408         //without further comparative ANDs. If the result is zero, a
6409         //comparative AND is made between the character being moved and edit
6410         //insertion table entry 4 If that result is nonzero, the SN indicator
6411         //is set ON (indicating negative) and the first character and the rest
6412         //of the characters are moved without further comparative ANDs. If the
6413         //result is zero, the second character is treated like the first. This
6414         //process continues until one of the comparative AND results is nonzero
6415         //or until all characters are moved.
6416 
6417         word9 c = *(e->in);
6418 
6419         if (!overpunch) {
6420             if (c & e->editInsertionTable[2])  // XXX only lower 4-bits are considered
6421                 overpunch = true;
6422 
6423             else if (c & e->editInsertionTable[3])  // XXX only lower 4-bits are considered
6424             {
6425                 e->mopSN = true;
6426                 overpunch = true;
6427             }
6428         }
6429 
6430         e->in += 1;
6431         e->srcTally -= 1;   // XXX is this correct? No chars have been consumed, but ......
6432         if (! isDecimalZero (c))
6433             e->mopZ = false;
6434         writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6435     }
6436 
6437     return 0;
6438 }
6439 
6440 /*!
6441  * MVZA Micro Operation - Move with Zero Suppression and Asterisk Replacement
6442  * EXPLANATION:
6443  * MVZA is the same as MVZB except that if ES is OFF and the character is zero,
6444  * then edit insertion table entry 2 is moved to the receiving field.
6445  * FLAGS: (Flags not listed are not affected.)
6446  * ES If OFF and any of C(Y) is less than decimal zero, then ON; otherwise, it
6447  * is unchanged.
6448  */
6449 
6450 static int mopMVZA (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6451 {
6452     EISstruct * e = & cpu.currentEISinstruction;
6453     if (e->mopIF == 0)
6454         e->mopIF = 16;
6455 
6456     for(int n = 0 ; n < e->mopIF ; n += 1)
6457     {
6458         if (e->dstTally == 0)
6459             break;
6460         if (e->srcTally == 0)
6461             return -1;
6462 
6463         word9 c = *e->in;
6464         e->in += 1;
6465         e->srcTally -= 1;
6466 
6467         //if (!e->mopES && c == 0)
6468         // XXX See srcTA comment in MVNE
6469         if (!e->mopES && isDecimalZero (c))
6470         {
6471             //If ES is OFF and the character is zero, then edit insertion table
6472             //entry 2 is moved to the receiving field in place of the
6473             //character.
6474             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[1]);
6475         //} else if (!e->mopES && c != 0)
6476         // XXX See srcTA comment in MVNE
6477         }
6478         else if ((! e->mopES) && (! isDecimalZero (c)))
6479         {
6480             //If ES is OFF and the character is not zero, then the character is
6481             //moved to the receiving field and ES is set ON.
6482             e->mopZ = false;
6483             writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6484 
6485             e->mopES = true;
6486         } else if (e->mopES)
6487         {
6488             //If ES is ON, the character is moved to the receiving field.
6489             if (! isDecimalZero (c))
6490                 e->mopZ = false;
6491             writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6492         }
6493     }
6494 
6495     return 0;
6496 }
6497 
6498 /*!
6499  * MVZB Micro Operation - Move with Zero Suppression and Blank Replacement
6500  * EXPLANATION:
6501  * IF specifies the number of characters of the sending field upon which the
6502  * operation is performed, where IF = 0 specifies 16 characters.
6503  * Starting with the next available sending field character, the next IF
6504  * characters are individually fetched and the following conditional actions
6505  * occur.
6506  * - If ES is OFF and the character is zero, then edit insertion table entry 1
6507  * is moved to the receiving field in place of the character.
6508  * - If ES is OFF and the character is not zero, then the character is moved to
6509  * the receiving field and ES is set ON.
6510  * - If ES is ON, the character is moved to the receiving field.
6511  * FLAGS: (Flags not listed are not affected.)
6512  *   ES If OFF and any of C(Y) is less than decimal zero, then ON; otherwise,
6513  *   it is unchanged.
6514  */
6515 
6516 static int mopMVZB (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6517 {
6518     EISstruct * e = & cpu.currentEISinstruction;
6519     if (e->mopIF == 0)
6520         e->mopIF = 16;
6521 
6522     for(int n = 0 ; n < e->mopIF ; n += 1)
6523     {
6524         if (e->dstTally == 0)
6525             break;
6526         if (e->srcTally == 0)
6527           return -1;
6528 
6529         word9 c = *e->in;
6530         e->srcTally -= 1;
6531         e->in += 1;
6532 
6533         //if (!e->mopES && c == 0)
6534         // XXX See srcTA comment in MVNE
6535         if ((!e->mopES) && isDecimalZero (c))
6536         {
6537             //If ES is OFF and the character is zero, then edit insertion table
6538             //entry 1 is moved to the receiving field in place of the
6539             //character.
6540             writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
6541         //} else if (!e->mopES && c != 0)
6542         // XXX See srcTA comment in MVNE
6543         }
6544         if ((! e->mopES) && (! isDecimalZero (c)))
6545         {
6546             //If ES is OFF and the character is not zero, then the character is
6547             //moved to the receiving field and ES is set ON.
6548             e->mopZ = false;
6549             writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6550 
6551             e->mopES = true;
6552         } else if (e->mopES)
6553         {
6554             //If ES is ON, the character is moved to the receiving field.
6555             if (! isDecimalZero (c))
6556                 e->mopZ = false;
6557             writeToOutputBuffer(cpup, &e->out, e->srcSZ, e->dstSZ, c);
6558         }
6559     }
6560 
6561     return 0;
6562 }
6563 
6564 /*!
6565  * SES Micro Operation - Set End Suppression
6566  * EXPLANATION:
6567  * Bit 0 of IF (IF(0)) specifies the setting of the ES switch.
6568  * If IF(0) = 0, the ES flag is set OFF. If IF(0) = 1, the ES flag is set ON.
6569  * Bit 1 of IF (IF(1)) specifies the setting of the blank-when-zero option.
6570  * If IF(1) = 0, no action is taken.
6571  * If IF(1) = 1, the BZ flag is set ON.
6572  * FLAGS: (Flags not listed are not affected.)
6573  * ES set by this micro operation
6574  * BZ If bit 1 of C(IF) = 1, then ON; otherwise, it is unchanged.
6575  */
6576 
6577 static int mopSES (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6578 {
6579     EISstruct * e = & cpu.currentEISinstruction;
6580     if (e->mopIF & 010)
6581         e->mopES = true;
6582     else
6583         e->mopES = false;
6584 
6585     if (e->mopIF & 04)
6586         e->mopBZ = true;
6587 
6588     return 0;
6589 }
6590 
6591 // Table 4-9. Micro Operation Code Assignment Map
6592 #if !defined(QUIET_UNUSED)
6593 static char * mopCodes [040] =
6594   {
6595     //            0       1       2       3       4       5       6       7
6596     /* 00 */      0, "insm",  "enf",  "ses", "mvzb", "mvza", "mfls", "mflc",
6597     /* 10 */ "insb", "insa", "insn", "insp",  "ign",  "mvc", "mses", "mors",
6598     /* 20 */  "lte",  "cht",      0,      0,      0,      0,      0,      0,
6599     /* 30 */      0,      0,      0,      0,      0,      0,      0,      0
6600   };
6601 #endif
6602 
6603 static MOP_struct mopTab[040] = {
6604     {NULL, 0},
6605     {"insm", mopINSM },
6606     {"enf",  mopENF  },
6607     {"ses",  mopSES  },
6608     {"mvzb", mopMVZB },
6609     {"mvza", mopMVZA },
6610     {"mfls", mopMFLS },
6611     {"mflc", mopMFLC },
6612     {"insb", mopINSB },
6613     {"insa", mopINSA },
6614     {"insn", mopINSN },
6615     {"insp", mopINSP },
6616     {"ign",  mopIGN  },
6617     {"mvc",  mopMVC  },
6618     {"mses", mopMSES },
6619     {"mors", mopMORS },
6620     {"lte",  mopLTE  },
6621     {"cht",  mopCHT  },
6622     {NULL, 0},
6623     {NULL, 0},
6624     {NULL, 0},
6625     {NULL, 0},
6626     {NULL, 0},
6627     {NULL, 0},
6628     {NULL, 0},
6629     {NULL, 0},
6630     {NULL, 0},
6631     {NULL, 0},
6632     {NULL, 0},
6633     {NULL, 0},
6634     {NULL, 0},
6635     {NULL, 0}
6636 };
6637 
6638 /*!
6639  * fetch MOP from e->mopAddr/e->mopPos ...
6640  */
6641 
6642 static MOP_struct* EISgetMop (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6643 {
6644     EISstruct * e = & cpu.currentEISinstruction;
6645     //static word18 lastAddress;  // try to keep memory access' down
6646     //static word36 data;
6647 
6648     if (e == NULL) //-V547
6649     //{
6650     //    p->lastAddress = -1;
6651     //    p->data = 0;
6652         return NULL;
6653     //}
6654 
6655 #if defined(EIS_PTR2)
6656     EISaddr *p = &e->ADDR2;
6657 #else
6658     EISaddr *p = e->mopAddress;
6659 #endif
6660 
6661     //if (p->lastAddress != p->address)                 // read from memory if different address
6662         p->data = EISRead(cpup, p);   // read data word from memory
6663 
6664     if (e->mopPos > 3)   // overflows to next word?
6665     {   // yep....
6666         e->mopPos = 0;   // reset to 1st byte
6667 #if defined(EIS_PTR2)
6668         cpu.du.Dk_PTR_W[KMOP] = (cpu.du.Dk_PTR_W[KMOP] + 1) & AMASK;     // bump source to next address
6669         p->data = EISRead(cpup, &e->ADDR2);   // read it from memory
6670 #else
6671         PNL (cpu.du.Dk_PTR_W[1] = (cpu.du.Dk_PTR_W[1] + 1) & AMASK);     // bump source to next address
6672         PNL (p->data = EISRead(cpup, e->mopAddress));   // read it from memory
6673 # if defined(EIS_PTR)
6674         cpu.du.Dk_PTR_W[1] = (cpu.du.Dk_PTR_W[1] + 1) & AMASK;     // bump source to next address
6675         p->data = EISRead(cpup, e->mopAddress);   // read it from memory
6676 # else
6677         e->mopAddress->address = (e->mopAddress->address + 1) & AMASK;     // bump source to next address
6678         p->data = EISRead(cpup, e->mopAddress);   // read it from memory
6679 # endif
6680 #endif
6681     }
6682 
6683     word9 mop9  = (word9) get9 (p -> data, e -> mopPos); // get 9-bit mop
6684     word5 mop   = (mop9 >> 4) & 037;
6685     e->mopIF = mop9 & 0xf;
6686 
6687     MOP_struct *m = &mopTab[mop];
6688     sim_debug (DBG_TRACEEXT, & cpu_dev, "MOP %s(%o) %o\r\n", m -> mopName, mop, e->mopIF);
6689     e->m = m;
6690     if (e->m == NULL || e->m->f == NULL) //-V560
6691     {
6692         sim_debug (DBG_TRACEEXT, & cpu_dev, "getMop(e->m == NULL || e->m->f == NULL): mop:%d IF:%d\r\n", mop, e->mopIF);
6693         return NULL;
6694     }
6695 
6696     e->mopPos += 1;
6697     e->mopTally -= 1;
6698 
6699     //p->lastAddress = p->address;
6700 
6701     return m;
6702 }
6703 
6704 #if defined(EIS_PTR2)
6705 static void mopExecutor (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6706 #else
6707 static void mopExecutor (cpu_state_t * cpup, int kMop)
6708 #endif
6709   {
6710     EISstruct * e = & cpu.currentEISinstruction;
6711     PNL (L68_ (DU_CYCLE_FEXOP;))
6712 #if defined(EIS_PTR2)
6713     e->mopTally = (int) e->N[KMOP];        // number of micro-ops
6714     e->mopPos   = (int) e->CN[KMOP];        // starting at char pos CN
6715 #else
6716     e->mopAddress = &e->addr[kMop-1];
6717     e->mopTally = (int) e->N[kMop-1];        // number of micro-ops
6718     e->mopPos   = (int) e->CN[kMop-1];        // starting at char pos CN
6719 #endif
6720 
6721     word9 *p9 = e->editInsertionTable; // re-initialize edit insertion table
6722     char *q = defaultEditInsertionTable;
6723     while((*p9++ = (word9) (*q++)))
6724         ;
6725 
6726     e->in = e->inBuffer;    // reset input buffer pointer
6727     e->out = e->outBuffer;  // reset output buffer pointer
6728 
6729     e->_faults = 0; // No faults (yet!)
6730 
6731     // execute dstTally micro operations
6732     // The micro operation sequence is terminated normally when the receiving
6733     // string length becomes exhausted. The micro operation sequence is
6734     // terminated abnormally (with an illegal procedure fault) if a move from
6735     // an exhausted sending string or the use of an exhausted MOP string is
6736     // attempted.
6737 
6738     while (e->dstTally)
6739     {
6740         sim_debug (DBG_TRACEEXT, & cpu_dev,
6741                    "mopExecutor srcTally %d dstTally %d mopTally %d\r\n",
6742                    e->srcTally, e->dstTally, e->mopTally);
6743         MOP_struct *m = EISgetMop(cpup);
6744         if (! m)
6745           {
6746             sim_debug (DBG_TRACEEXT, & cpu_dev, "mopExecutor EISgetMop forced break\r\n");
6747             e->_faults |= FAULT_IPR;   // XXX ill proc fault
6748             break;
6749           }
6750         int mres = m->f(cpup);    // execute mop
6751 
6752         // PVS-Studio claims: Expression 'e->_faults & FAULT_IPR' is always false.
6753         if (e->_faults & FAULT_IPR) //-V547
6754             break; // hard IPR raised by a MOP
6755 
6756         // RJ78 specifies "if at completion of a move (L1 exhausted)", AL39
6757         // doesn't define "completion of a move".
6758         // But ISOLTS-845 asserts a case where L1 is NOT exhausted. Therefore I
6759         // assume "L2 exhausted" or "L1 or L2 exhausted" is the correct
6760         // interpretation. Both these options pass ISOLTS-845.
6761         // "L3 exhausted" is also an option but unlikely since that would fire
6762         // the BZ check even upon normal termination.
6763         // XXX DH03 7-295 suggests that there might be a difference between MVE
6764         // and MVNE. It might well be that DPS88/9000 behaves differently than
6765         // DPS8.
6766 
6767 
6768 
6769 
6770 
6771 
6772 
6773         // immediate (L1 or L2) srcTally test
6774         // perhaps more adherent to documentation
6775         if (e->mopTally == 0 || mres)
6776           {
6777             sim_debug (DBG_TRACEEXT, & cpu_dev,
6778                        "mopExecutor N1 or N2 exhausted\r\n");
6779 
6780             if (e->mopZ && e->mopBZ)
6781               {
6782                 e->out = e->outBuffer; //-V1048  // reset output buffer pointer
6783                 e->dstTally = (int) e->N3;       // number of chars in dst (max 63)
6784                 while (e->dstTally)
6785                   {
6786                     writeToOutputBuffer(cpup, &e->out, 9, e->dstSZ, e->editInsertionTable[0]);
6787                   }
6788               }
6789             else if (mres || e->dstTally) //-V560
6790               { // N1 or N2 exhausted and BZ wasn't enabled
6791                 e->_faults |= FAULT_IPR;
6792               } // otherwise normal termination
6793             break;
6794           }
6795     }
6796 
6797     sim_debug (DBG_TRACEEXT, & cpu_dev, "mop faults %o src %d dst %d mop %d\r\n",
6798                e->_faults, e->srcTally, e->dstTally, e->mopTally);
6799 
6800 //"The micro-operation sequence is terminated normally when the receiving string
6801 // length is exhausted. The micro-operation sequence is terminated abnormally (with
6802 // an IPR fault) if an attempt is made to move from an exhausted sending string or to
6803 // use an exhausted MOP string.
6804 
6805 // ISOLTS 845 is happy with no check at all
6806 
6807 
6808 
6809 
6810 
6811 
6812 
6813 
6814 
6815 
6816 
6817 
6818 
6819 
6820 
6821 
6822 
6823 
6824 
6825 
6826 
6827 
6828 
6829 
6830 
6831 
6832 
6833 
6834 
6835 
6836 
6837 
6838 
6839 
6840 
6841 
6842 
6843 
6844 
6845 
6846     if (e -> _faults)
6847       doFault (FAULT_IPR, fst_ill_proc, "mopExecutor");
6848 }
6849 
6850 void mve (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6851   {
6852     EISstruct * e = & cpu.currentEISinstruction;
6853 
6854     sim_debug(DBG_TRACEEXT, & cpu_dev, "mve\r\n");
6855 
6856     fault_ipr_subtype_ mod_fault = 0;
6857 
6858 #if !defined(EIS_SETUP)
6859     setupOperandDescriptor(cpup, 1, &mod_fault);
6860     setupOperandDescriptor(cpup, 2, &mod_fault);
6861     setupOperandDescriptor(cpup, 3, &mod_fault);
6862 #endif
6863 
6864     parseAlphanumericOperandDescriptor(cpup, 1, 1, false, &mod_fault);
6865     parseAlphanumericOperandDescriptor(cpup, 2, 2, false, &mod_fault);
6866     parseAlphanumericOperandDescriptor(cpup, 3, 3, false, &mod_fault);
6867 
6868     L68_ (
6869       // L68 raises it immediately
6870       if (mod_fault)
6871         {
6872           doFault (FAULT_IPR,
6873                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
6874                    "Illegal modifier");
6875         }
6876     )
6877 
6878     // Bits 0, 1, 9, and 10 MBZ
6879     // According to RJ78, bit 9 is T, but is not mentioned in the text.
6880     if (IWB_IRODD & 0600600000000)
6881       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "mve: 0, 1, 9, 10 MBZ");
6882 
6883     // Bit 23 of OP1 MBZ
6884     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
6885       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mve op1 23 MBZ");
6886 
6887 
6888 
6889 
6890 
6891 
6892     // only bit 23 according to RH03. this was fixed in DPS9000
6893     if (!(e->MF[1] & MFkID) && e -> op [1]  & 0000000010000)
6894       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mve op2 23 MBZ");
6895 
6896     // Bit 23 of OP3 MBZ
6897     if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000010000)
6898       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mve op3 23 MBZ");
6899 
6900     DPS8M_ (
6901       // DPS8M raises it delayed
6902       if (mod_fault)
6903         {
6904           doFault (FAULT_IPR,
6905                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
6906                    "Illegal modifier");
6907         }
6908     )
6909 
6910     // initialize mop flags. Probably best done elsewhere.
6911     e->mopES = false; // End Suppression flag
6912     e->mopSN = false; // Sign flag
6913     e->mopBZ = false; // Blank-when-zero flag
6914     e->mopZ  = true;  // Zero flag
6915 
6916     e->srcTally = (int) e->N1;  // number of chars in src (max 63)
6917     e->dstTally = (int) e->N3;  // number of chars in dst (max 63)
6918 
6919 #if defined(EIS_PTR3)
6920     e->srcTA = (int) TA1;    // type of chars in src
6921 #else
6922     e->srcTA = (int) e->TA1;    // type of chars in src
6923 #endif
6924 
6925     switch (e -> srcTA)
6926       {
6927         case CTA4:
6928           e -> srcSZ = 4;
6929           break;
6930         case CTA6:
6931           e -> srcSZ = 6;
6932           break;
6933         case CTA9:
6934           e -> srcSZ = 9;
6935           break;
6936       }
6937 
6938 #if defined(EIS_PTR3)
6939     uint dstTA = TA3;    // type of chars in dst
6940 #else
6941     uint dstTA = e -> TA3;    // type of chars in dst
6942 #endif
6943 
6944     switch (dstTA)
6945       {
6946         case CTA4:
6947           e -> dstSZ = 4;
6948           break;
6949         case CTA6:
6950           e -> dstSZ = 6;
6951           break;
6952         case CTA9:
6953           e -> dstSZ = 9;
6954           break;
6955       }
6956 
6957     // 1. load sending string into inputBuffer
6958     EISloadInputBufferAlphnumeric (cpup, 1);   // according to MF1
6959 
6960     // 2. Execute micro operation string, starting with first (4-bit) digit.
6961     e -> mvne = false;
6962 
6963 #if defined(EIS_PTR2)
6964     mopExecutor (cpup);
6965 #else
6966     mopExecutor (cpup, 2);
6967 #endif
6968 
6969     e -> dstTally = (int) e -> N3;  // restore dstTally for output
6970 
6971     EISwriteOutputBufferToMemory (cpup, 3);
6972     cleanupOperandDescriptor (cpup, 1);
6973     cleanupOperandDescriptor (cpup, 2);
6974     cleanupOperandDescriptor (cpup, 3);
6975   }
6976 
6977 void mvne (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
6978   {
6979     EISstruct * e = & cpu.currentEISinstruction;
6980 
6981     fault_ipr_subtype_ mod_fault = 0;
6982 
6983 #if !defined(EIS_SETUP)
6984     setupOperandDescriptor (cpup, 1, &mod_fault);
6985     setupOperandDescriptor (cpup, 2, &mod_fault);
6986     setupOperandDescriptor (cpup, 3, &mod_fault);
6987 #endif
6988 
6989     parseNumericOperandDescriptor (cpup, 1, &mod_fault);
6990     parseAlphanumericOperandDescriptor (cpup, 2, 2, false, &mod_fault);
6991     parseAlphanumericOperandDescriptor (cpup, 3, 3, false, &mod_fault);
6992 
6993     L68_ (
6994       // L68 raises it immediately
6995       if (mod_fault)
6996         {
6997           doFault (FAULT_IPR,
6998                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
6999                    "Illegal modifier");
7000         }
7001     )
7002 
7003     // Bits 0, 1, 9, and 10 MBZ
7004     if (IWB_IRODD & 0600600000000)
7005       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "mvne: 0, 1, 9, 10 MBZ");
7006 
7007     // Bit 24-29 of OP1 MBZ
7008     // Multics has been observed to use 600162017511, cf RJ78
7009     //if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000007700)
7010       //doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mvne op1 24-29 MBZ");
7011 
7012 
7013 
7014 
7015 
7016 
7017     // only bits 21-23 according to RJ78, maybe even less on DPS8
7018     if (!(e->MF[1] & MFkID) && e -> op [1]  & 0000000070000)
7019       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mvne op2 21-23 MBZ");
7020 
7021 
7022 
7023 
7024 
7025 
7026     // only bit 23 according to RJ78
7027     if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000010000)
7028       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mvne op3 23 MBZ");
7029 
7030     DPS8M_ (
7031       // DPS8M raises it delayed
7032       if (mod_fault)
7033         {
7034           doFault (FAULT_IPR,
7035                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
7036                    "Illegal modifier");
7037         }
7038     )
7039 
7040     uint srcTN = e -> TN1;    // type of chars in src
7041 
7042     int n1 = 0;
7043 
7044     /*
7045      * Here we need to distinguish between 4 type of numbers.
7046      *
7047      * CSFL - Floating-point, leading sign
7048      * CSLS - Scaled fixed-point, leading sign
7049      * CSTS - Scaled fixed-point, trailing sign
7050      * CSNS - Scaled fixed-point, unsigned
7051      */
7052 
7053     // determine precision
7054     switch(e->S1)
7055     {
7056         case CSFL:
7057             n1 = (int) e->N1 - 1; // need to account for the - sign
7058             if (srcTN == CTN4)
7059                 n1 -= 2;    // 2 4-bit digits exponent
7060             else
7061                 n1 -= 1;    // 1 9-bit digit exponent
7062             break;
7063 
7064         case CSLS:
7065         case CSTS:
7066             n1 = (int) e->N1 - 1; // only 1 sign
7067             break;
7068 
7069         case CSNS:
7070             n1 = (int) e->N1;     // no sign
7071             break;  // no sign wysiwyg
7072     }
7073 
7074     if (n1 < 1)
7075         doFault (FAULT_IPR, fst_ill_proc, "mvne adjusted n1<1");
7076 
7077     // Putting this check in pAOD breaks Multics boot
7078     // From DH03 it seems that DPS8 does not check this explicitly, but L2 exhaust occurs which raises IPR anyway
7079     // So we may as well keep it here.
7080     if (e->N[1] == 0)
7081       doFault (FAULT_IPR, fst_ill_proc, "mvne N2 0");
7082 
7083     // this is a flaw in DPS8/70 which was corrected in DPS88 and later
7084     // ISOLTS-841 07h, RH03 p.7-295
7085     if (e->N[2] == 0)
7086       doFault (FAULT_IPR, fst_ill_proc, "mvne N3 0");
7087 
7088 //if ((e -> op [0]  & 0000000007700) ||
7089 //    (e -> op [1]  & 0000000077700) ||
7090 //    (e -> op [2]  & 0000000017700))
7091 //sim_printf ("%012"PRIo64"\r\n%012"PRIo64"\r\n%012"PRIo64"\r\n%012"PRIo64"\r\n", cpu.cu.IWB, e->op[0], e->op[1], e-> op[2]);
7092 //if (e -> op [0]  & 0000000007700) sim_printf ("op1\r\n");
7093 //if (e -> op [1]  & 0000000077700) sim_printf ("op2\r\n");
7094 //if (e -> op [2]  & 0000000017700) sim_printf ("op3\r\n");
7095 //000140  aa  100 004 024 500   mvne      (pr),(ic),(pr)
7096 //000141  aa  6 00162 01 7511   desc9ls   pr6|114,9,-3
7097 //000142  aa   000236 00 0007   desc9a    158,7               000376 = 403040144040
7098 //000143  aa  6 00134 00 0012   desc9a    pr6|92,10           vcpu
7099 //
7100 // The desc8ls is sign-extending the -3.
7101 
7102     // initialize mop flags. Probably best done elsewhere.
7103     e->mopES = false; // End Suppression flag
7104     e->mopSN = false; // Sign flag
7105     e->mopBZ = false; // Blank-when-zero flag
7106     e->mopZ  = true;  // Zero flag
7107 
7108     e -> srcTally = (int) e -> N1;  // number of chars in src (max 63)
7109     e -> dstTally = (int) e -> N3;  // number of chars in dst (max 63)
7110 
7111 // XXX Temp hack to get MOP to work. Merge TA/TN?
7112 // The MOP operators look at srcTA to make 9bit/not 9-bit decisions about
7113 // the contents of inBuffer; parseNumericOperandDescriptor(cpup, ) always puts
7114 // 4 bit data in inBuffer, so signal the MOPS code of that.
7115     e->srcTA = CTA4;    // type of chars in src
7116 
7117     switch(srcTN)
7118     {
7119         case CTN4:
7120             //e->srcAddr = e->YChar41;
7121             e->srcSZ = 4; //-V1037  // stored as 4-bit decimals
7122             break;
7123         case CTN9:
7124             //e->srcAddr = e->YChar91;
7125             e->srcSZ = 4; //-V1037  // 'cause everything is stored as 4-bit decimals
7126             break;
7127     }
7128 
7129 #if defined(EIS_PTR3)
7130     uint dstTA = TA3;     // type of chars in dst
7131 #else
7132     uint dstTA = e->TA3;  // type of chars in dst
7133 #endif
7134     switch(dstTA)
7135     {
7136         case CTA4:
7137             //e->dstAddr = e->YChar43;
7138             e->dstSZ = 4;
7139             break;
7140         case CTA6:
7141             //e->dstAddr = e->YChar63;
7142             e->dstSZ = 6;
7143             break;
7144         case CTA9:
7145             //e->dstAddr = e->YChar93;
7146             e->dstSZ = 9;
7147             break;
7148     }
7149 
7150 #if defined(EIS_PTR3)
7151     sim_debug (DBG_TRACEEXT, & cpu_dev,
7152       "mvne N1 %d N2 %d N3 %d TN1 %d CN1 %d TA3 %d CN3 %d\r\n",
7153       e->N1, e->N2, e->N3, e->TN1, e->CN1, TA3, e->CN3);
7154 #else
7155     sim_debug (DBG_TRACEEXT, & cpu_dev,
7156       "mvne N1 %d N2 %d N3 %d TN1 %d CN1 %d TA3 %d CN3 %d\r\n",
7157       e->N1, e->N2, e->N3, e->TN1, e->CN1, e->TA3, e->CN3);
7158 #endif
7159 
7160     // 1. load sending string into inputBuffer
7161     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
7162 
7163     // 2. Test sign and, if required, set the SN flag. (Sign flag; initially
7164     // set OFF if the sending string has an alphanumeric descriptor or an
7165     // unsigned numeric descriptor. If the sending string has a signed numeric
7166     // descriptor, the sign is initially read from the sending string from the
7167     // digit position defined by the sign and the decimal type field (S); SN is
7168     // set OFF if positive, ON if negative. If all digits are zero, the data is
7169     // assumed positive and the SN flag is set OFF, even when the sign is
7170     // negative.)
7171 
7172     int sum = 0;
7173     for(int n = 0 ; n < e -> srcTally ; n ++)
7174         sum += e -> inBuffer [n];
7175     if ((e -> sign == -1) && sum)
7176         e -> mopSN = true;
7177 
7178     // 3. Execute micro operation string, starting with first (4-bit) digit.
7179     e -> mvne = true;
7180 
7181 #if defined(EIS_PTR2)
7182     mopExecutor (cpup);
7183 #else
7184     mopExecutor (cpup, 2);
7185 #endif
7186 
7187     e -> dstTally = (int) e -> N3;  // restore dstTally for output
7188 
7189     EISwriteOutputBufferToMemory (cpup, 3);
7190     cleanupOperandDescriptor (cpup, 1);
7191     cleanupOperandDescriptor (cpup, 2);
7192     cleanupOperandDescriptor (cpup, 3);
7193   }
7194 
7195 /*
7196  * MVT - Move Alphanumeric with Translation
7197  */
7198 
7199 void mvt (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
7200   {
7201     EISstruct * e = & cpu.currentEISinstruction;
7202 
7203     // For i = 1, 2, ..., minimum (N1,N2)
7204     //    m = C(Y-charn1)i-1
7205     //    C(Y-char93)m -> C(Y-charn2)i-1
7206     // If N1 < N2, then for i = N1+1, N1+2, ..., N2
7207     //    m = C(FILL)
7208     //    C(Y-char93)m -> C(Y-charn2)i-1
7209 
7210     // Indicators: Truncation. If N1 > N2 then ON; otherwise OFF
7211 
7212     fault_ipr_subtype_ mod_fault = 0;
7213 
7214 #if !defined(EIS_SETUP)
7215     setupOperandDescriptor (cpup, 1, &mod_fault);
7216     setupOperandDescriptor (cpup, 2, &mod_fault);
7217     setupOperandDescriptorCache (cpup,3);
7218 #endif
7219 
7220     parseAlphanumericOperandDescriptor (cpup, 1, 1, false, &mod_fault);
7221     parseAlphanumericOperandDescriptor (cpup, 2, 2, false, &mod_fault);
7222     parseArgOperandDescriptor (cpup, 3, &mod_fault);
7223 
7224     L68_ (
7225       // L68 raises it immediately
7226       if (mod_fault)
7227         {
7228           doFault (FAULT_IPR,
7229                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
7230                    "Illegal modifier");
7231         }
7232     )
7233 
7234 // ISOLTS 808 test-03b sets bit 0, 1
7235 // ISOLTS 808 test-03b sets bit 0, 1, 9
7236 
7237     // Bits 10 MBZ
7238     if (IWB_IRODD & 0000200000000)
7239       {
7240         //sim_printf ("mvt %012"PRIo64"", IWB_IRODD);
7241         doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "mvt 10 MBZ");
7242       }
7243 
7244 
7245 
7246 
7247 
7248 
7249 
7250 
7251 
7252     // Bit 23 of OP1 MBZ
7253     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000010000)
7254       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mvt op1 23 MBZ");
7255 
7256 // This breaks eis_tester mvt 110
7257 
7258 
7259 
7260 
7261 
7262 
7263     // Bits 18-28 of OP3 MBZ
7264     if (!(e->MF[2] & MFkID) && e -> op [2]  & 0000000777600)
7265       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "mvt op3 18-28 MBZ");
7266 
7267     DPS8M_ (
7268       // DPS8M raises it delayed
7269       if (mod_fault)
7270         {
7271           doFault (FAULT_IPR,
7272                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
7273                    "Illegal modifier");
7274         }
7275     )
7276 
7277 #if defined(EIS_PTR3)
7278     e->srcTA = (int) TA1;
7279     uint dstTA = TA2;
7280 
7281     switch (TA1)
7282 #else
7283     e->srcTA = (int) e->TA1;
7284     uint dstTA = e->TA2;
7285 
7286     switch (e -> TA1)
7287 #endif
7288       {
7289         case CTA4:
7290           e -> srcSZ = 4;
7291           break;
7292         case CTA6:
7293           e -> srcSZ = 6;
7294           break;
7295         case CTA9:
7296           e -> srcSZ = 9;
7297          break;
7298       }
7299 
7300 #if defined(EIS_PTR3)
7301     switch (TA2)
7302 #else
7303     switch (e -> TA2)
7304 #endif
7305       {
7306         case CTA4:
7307           e -> dstSZ = 4;
7308           break;
7309         case CTA6:
7310           e -> dstSZ = 6;
7311           break;
7312         case CTA9:
7313           e -> dstSZ = 9;
7314           break;
7315       }
7316 
7317     //  Prepage Check in a Multiword Instruction
7318     //  The MVT, TCT, TCTR, and CMPCT instruction have a prepage check. The
7319     //  size of the translate table is determined by the TA1 data type as shown
7320     //  in the table below. Before the instruction is executed, a check is made
7321     //  for allocation in memory for the page for the translate table. If the
7322     //  page is not in memory, a Missing Page fault occurs before execution of
7323     //  the instruction. (Bull RJ78 p.7-75)
7324 
7325     // TA1              TRANSLATE TABLE SIZE
7326     // 4-BIT CHARACTER      4 WORDS
7327     // 6-BIT CHARACTER     16 WORDS
7328     // 9-BIT CHARACTER    128 WORDS
7329 
7330     uint xlatSize = 0;   // size of xlation table in words .....
7331 #if defined(EIS_PTR3)
7332     switch(TA1)
7333 #else
7334     switch(e->TA1)
7335 #endif
7336     {
7337         case CTA4:
7338             xlatSize = 4;
7339             break;
7340         case CTA6:
7341             xlatSize = 16;
7342             break;
7343         case CTA9:
7344             xlatSize = 128;
7345             break;
7346     }
7347 
7348 
7349 
7350 
7351 
7352 
7353 
7354 
7355 
7356     // ISOLTS 878 01c - op1 and xlate table are prepaged, in that order
7357     // prepage op1
7358     int lastpageidx = ((int)e->N1 + (int)e->CN1 -1) / e->srcSZ;
7359     if (lastpageidx>0)
7360         EISReadIdx(cpup, &e->ADDR1, (uint)lastpageidx);
7361     // prepage xlate table
7362     if (xlatSize > 0)
7363     {
7364         EISReadIdx(cpup, &e->ADDR3, 0);
7365         EISReadIdx(cpup, &e->ADDR3, xlatSize-1);
7366     }
7367 
7368     word1 T = getbits36_1 (cpu.cu.IWB, 9);
7369 
7370     word9 fill = getbits36_9 (cpu.cu.IWB, 0);
7371     word9 fillT = fill;  // possibly truncated fill pattern
7372     // play with fill if we need to use it
7373     switch(e->srcSZ)
7374     {
7375         case 4:
7376             fillT = fill & 017;    // truncate upper 5-bits
7377             break;
7378         case 6:
7379             fillT = fill & 077;    // truncate upper 3-bits
7380             break;
7381     }
7382 
7383     sim_debug (DBG_TRACEEXT, & cpu_dev,
7384       "%s srcCN:%d dstCN:%d srcSZ:%d dstSZ:%d T:%d fill:%03o/%03o N1:%d N2:%d",
7385       __func__, e -> CN1, e -> CN2, e -> srcSZ, e -> dstSZ, T,
7386       fill, fillT, e -> N1, e -> N2);
7387 
7388     PNL (L68_ (if (max (e->N1, e->N2) < 128)
7389       DU_CYCLE_FLEN_128;))
7390 
7391     for ( ; cpu.du.CHTALLY < min(e->N1, e->N2); cpu.du.CHTALLY ++)
7392     {
7393         tandd_eis_sample (cpup, cpu.du.CHTALLY, TDPER (e -> TA2));
7394         word9 c = EISget469(cpup, 1, cpu.du.CHTALLY); // get src char
7395         int cidx = 0;
7396 
7397 #if defined(EIS_PTR3)
7398         if (TA1 == TA2)
7399 #else
7400         if (e->TA1 == e->TA2)
7401 #endif
7402             EISput469(cpup, 2, cpu.du.CHTALLY, xlate (cpup, &e->ADDR3, dstTA, c));
7403         else
7404         {
7405             // If data types are dissimilar (TA1 ≠ TA2), each character is high-order
7406             //   truncated or zero filled, as appropriate, as it is moved.
7407             //   No character conversion takes place.
7408             cidx = c;
7409 
7410             word9 cout = xlate(cpup, &e->ADDR3, dstTA, (uint) cidx);
7411 
7412 //            switch(e->dstSZ)
7413 //            {
7414 //                case 4:
7415 //                    cout &= 017;    // truncate upper 5-bits
7416 //                    break;
7417 //                case 6:
7418 //                    cout &= 077;    // truncate upper 3-bits
7419 //                    break;
7420 //            }
7421 
7422             switch (e->srcSZ)
7423             {
7424                 case 6:
7425                     switch(e->dstSZ)
7426                     {
7427                         case 4:
7428                             cout &= 017;    // truncate upper 2-bits
7429                             break;
7430                         case 9:
7431                             break;              // should already be 0-filled
7432                     }
7433                     break;
7434                 case 9:
7435                     switch(e->dstSZ)
7436                     {
7437                         case 4:
7438                             cout &= 017;    // truncate upper 5-bits
7439                             break;
7440                         case 6:
7441                             cout &= 077;    // truncate upper 3-bits
7442                             break;
7443                     }
7444                     break;
7445             }
7446 
7447             EISput469 (cpup, 2, cpu.du.CHTALLY, cout);
7448         }
7449     }
7450 
7451     // If N1 < N2, then for i = N1+1, N1+2, ..., N2
7452     //    m = C(FILL)
7453     //    C(Y-char93)m -> C(Y-charn2)N2-i
7454 
7455     if (e->N1 < e->N2)
7456     {
7457         // The whole 9-bit FILL is the index, even when the table is shorter
7458         // (a 6-bit or 4-bit source): AL39 "m = C(FILL)", and ISOLTS PS905's
7459         // own MVT simulator translates FILL unmasked (it masks only the
7460         // translated character, for the destination size, as below).
7461         // Masking FILL to the source size, as MLR/MRL must for a fill they
7462         // store directly, made PS905's MVT case fail.
7463         word9 cfill = xlate(cpup, &e->ADDR3, dstTA, fill);
7464         switch (e->srcSZ)
7465         {
7466             case 6:
7467                 switch(e->dstSZ)
7468                 {
7469                     case 4:
7470                         cfill &= 017;    // truncate upper 2-bits
7471                         break;
7472                     case 9:
7473                         break;              // should already be 0-filled
7474                 }
7475                 break;
7476             case 9:
7477                 switch(e->dstSZ)
7478                 {
7479                     case 4:
7480                         cfill &= 017;    // truncate upper 5-bits
7481                         break;
7482                     case 6:
7483                         cfill &= 077;    // truncate upper 3-bits
7484                         break;
7485                 }
7486                 break;
7487         }
7488 
7489         for( ; cpu.du.CHTALLY < e->N2 ; cpu.du.CHTALLY ++)
7490           {
7491             tandd_eis_sample (cpup, cpu.du.CHTALLY, TDPER (e -> TA2));
7492             EISput469 (cpup, 2, cpu.du.CHTALLY, cfill);
7493           }
7494     }
7495 
7496     cleanupOperandDescriptor (cpup, 1);
7497     cleanupOperandDescriptor (cpup, 2);
7498     cleanupOperandDescriptor (cpup, 3);
7499 
7500     if (e->N1 > e->N2)
7501       {
7502         SET_I_TRUNC;
7503         if (T && ! TST_I_OMASK)
7504           doFault(FAULT_OFL, fst_zero, "mvt truncation fault");
7505       }
7506     else
7507       CLR_I_TRUNC;
7508   }
7509 
7510 /*
7511  * cmpn - Compare Numeric
7512  */
7513 
7514 void cmpn (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
7515 {
7516     EISstruct * e = & cpu.currentEISinstruction;
7517 
7518     // C(Y-charn1) :: C(Y-charn2) as numeric values
7519 
7520     // Zero If C(Y-charn1) = C(Y-charn2), then ON; otherwise OFF
7521     // Negative If C(Y-charn1) > C(Y-charn2), then ON; otherwise OFF
7522     // Carry If | C(Y-charn1) | > | C(Y-charn2) | , then OFF, otherwise ON
7523 
7524     fault_ipr_subtype_ mod_fault = 0;
7525 
7526 #if !defined(EIS_SETUP)
7527     setupOperandDescriptor(cpup, 1, &mod_fault);
7528     setupOperandDescriptor(cpup, 2, &mod_fault);
7529 #endif
7530 
7531     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
7532     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
7533 
7534     L68_ (
7535       // L68 raises it immediately
7536       if (mod_fault)
7537         {
7538           doFault (FAULT_IPR,
7539                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
7540                    "Illegal modifier");
7541         }
7542     )
7543 
7544     // Bits 0-10 MBZ
7545     if (IWB_IRODD & 0777600000000)
7546       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "cmpn 0-10 MBZ");
7547 
7548     DPS8M_ (
7549       // DPS8M raises it delayed
7550       if (mod_fault)
7551         {
7552           doFault (FAULT_IPR,
7553                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
7554                    "Illegal modifier");
7555         }
7556     )
7557 
7558     uint srcTN = e->TN1;    // type of chars in src
7559 
7560     int n1 = 0, n2 = 0, sc1 = 0, sc2 = 0;
7561 
7562     /*
7563      * Here we need to distinguish between 4 type of numbers.
7564      *
7565      * CSFL - Floating-point, leading sign
7566      * CSLS - Scaled fixed-point, leading sign
7567      * CSTS - Scaled fixed-point, trailing sign
7568      * CSNS - Scaled fixed-point, unsigned
7569      */
7570 
7571     // determine precision
7572     switch(e->S1)
7573     {
7574         case CSFL:
7575             n1 = (int) e->N1 - 1; // need to account for the - sign
7576             if (srcTN == CTN4)
7577                 n1 -= 2;    // 2 4-bit digits exponent
7578             else
7579                 n1 -= 1;    // 1 9-bit digit exponent
7580             sc1 = 0;        // no scaling factor
7581             break;
7582 
7583         case CSLS:
7584         case CSTS:
7585             n1 = (int) e->N1 - 1; // only 1 sign
7586             sc1 = -e->SF1;
7587             break;
7588 
7589         case CSNS:
7590             n1 = (int) e->N1;     // no sign
7591             sc1 = -e->SF1;
7592             break;  // no sign wysiwyg
7593     }
7594 
7595     if (n1 < 1)
7596         doFault (FAULT_IPR, fst_ill_proc, "cmpn adjusted n1<1");
7597 
7598     switch(e->S2)
7599     {
7600         case CSFL:
7601             n2 = (int) e->N2 - 1; // need to account for the sign
7602             if (e->TN2 == CTN4)
7603                 n2 -= 2;    // 2 4-bit digit exponent
7604             else
7605                 n2 -= 1;    // 1 9-bit digit exponent
7606             sc2 = 0;        // no scaling factor
7607             break;
7608 
7609         case CSLS:
7610         case CSTS:
7611             n2 = (int) e->N2 - 1; // 1 sign
7612             sc2 = -e->SF2;
7613             break;
7614 
7615         case CSNS:
7616             n2 = (int) e->N2;     // no sign
7617             sc2 = -e->SF2;
7618             break;  // no sign wysiwyg
7619     }
7620 
7621     if (n2 < 1)
7622         doFault (FAULT_IPR, fst_ill_proc, "cmpn adjusted n2<1");
7623 
7624     decContext set;
7625     //decContextDefault(&set, DEC_INIT_BASE);         // initialize
7626     decContextDefaultDPS8(&set);
7627 
7628     set.traps=0;
7629 
7630     decNumber _1, _2, _3;
7631 
7632     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
7633 
7634     decNumber *op1 = decBCD9ToNumber(e->inBuffer, n1, sc1, &_1);
7635     if (e->sign == -1)
7636         op1->bits |= DECNEG;
7637     if (e->S1 == CSFL)
7638         op1->exponent = e->exponent;
7639 
7640     EISloadInputBufferNumeric (cpup, 2);   // according to MF2
7641 
7642     decNumber *op2 = decBCD9ToNumber(e->inBuffer, n2, sc2, &_2);
7643     if (e->sign == -1)
7644         op2->bits |= DECNEG;
7645     if (e->S2 == CSFL)
7646         op2->exponent = e->exponent;
7647 
7648     // signed-compare
7649     decNumber *cmp = decNumberCompare(&_3, op1, op2, &set); // compare signed op1 :: op2
7650     int cSigned = decNumberToInt32(cmp, &set);
7651 
7652     // take absolute value of operands
7653     op1 = decNumberAbs(op1, op1, &set);
7654     op2 = decNumberAbs(op2, op2, &set);
7655 
7656     // magnitude-compare
7657     decNumber *mcmp = decNumberCompare(&_3, op1, op2, &set); // compare signed op1 :: op2
7658     int cMag = decNumberToInt32(mcmp, &set);
7659 
7660     // Zero If C(Y-charn1) = C(Y-charn2), then ON; otherwise OFF
7661     // Negative If C(Y-charn1) > C(Y-charn2), then ON; otherwise OFF
7662     // Carry If | C(Y-charn1) | > | C(Y-charn2) | , then OFF, otherwise ON
7663 
7664     SC_I_ZERO (cSigned == 0);
7665     SC_I_NEG (cSigned == 1);
7666     SC_I_CARRY (cMag != 1);
7667 
7668     cleanupOperandDescriptor (cpup, 1);
7669     cleanupOperandDescriptor (cpup, 2);
7670 }
7671 
7672 /*
7673  * mvn - move numeric (initial version was deleted by house gnomes)
7674  */
7675 
7676 /*
7677  * write 4-bit chars to memory @ pos ...
7678  */
7679 
7680 static void EISwrite4(cpu_state_t * cpup, EISaddr *p, int *pos, word4 char4)
     /* [previous][next][first][last][top][bottom][index][help] */
7681 {
7682     word36 w;
7683     if (*pos > 7)    // out-of-range?
7684     {
7685         *pos = 0;    // reset to 1st byte
7686 #if defined(EIS_PTR)
7687         long eisaddr_idx = EISADDR_IDX (p);
7688 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
7689         cpu.du.Dk_PTR_W[eisaddr_idx] = (cpu.du.Dk_PTR_W[eisaddr_idx] + 1) & AMASK;     // bump source to next address
7690 #else
7691         p->address = (p->address + 1) & AMASK;        // goto next dstAddr in memory
7692 #endif
7693     }
7694 
7695     w = EISRead(cpup, p);      // read dst memory into w
7696 
7697 // AL39, Figure 2-3
7698 // The even positions are written as 5 bits, to force the 0 bits at bit
7699 // positions 0, 9, 18 and 27, as put4 does (AL39 pg 13). Writing only 4 bits
7700 // left those bits as they were, which ISOLTS PS905 caught on a DV2D result.
7701     char4 &= MASK4;
7702     switch (*pos)
7703     {
7704         case 0:
7705             //w = bitfieldInsert36(w, char4, 31, 5);
7706             w = setbits36_5 (w, 0, char4);
7707             break;
7708         case 1:
7709             //w = bitfieldInsert36(w, char4, 27, 4);
7710             w = setbits36_4 (w, 5, char4);
7711             break;
7712         case 2:
7713             //w = bitfieldInsert36(w, char4, 22, 5);
7714             w = setbits36_5 (w, 9, char4);
7715             break;
7716         case 3:
7717             //w = bitfieldInsert36(w, char4, 18, 4);
7718             w = setbits36_4 (w, 14, char4);
7719             break;
7720         case 4:
7721             //w = bitfieldInsert36(w, char4, 13, 5);
7722             w = setbits36_5 (w, 18, char4);
7723             break;
7724         case 5:
7725             //w = bitfieldInsert36(w, char4, 9, 4);
7726             w = setbits36_4 (w, 23, char4);
7727             break;
7728         case 6:
7729             //w = bitfieldInsert36(w, char4, 4, 5);
7730             w = setbits36_5 (w, 27, char4);
7731             break;
7732         case 7:
7733             //w = bitfieldInsert36(w, char4, 0, 4);
7734             w = setbits36_4 (w, 32, char4);
7735             break;
7736     }
7737 
7738     EISWriteIdx(cpup, p, 0, w, true); // XXX this is the inefficient part!
7739 
7740     *pos += 1;       // to next char.
7741 }
7742 
7743 /*
7744  * write 9-bit bytes to memory @ pos ...
7745  */
7746 
7747 static void EISwrite9(cpu_state_t *cpup, EISaddr *p, int *pos, word9 char9)
     /* [previous][next][first][last][top][bottom][index][help] */
7748 {
7749     word36 w;
7750     if (*pos > 3)    // out-of-range?
7751     {
7752         *pos = 0;    // reset to 1st byte
7753 #if defined(EIS_PTR)
7754         long eisaddr_idx = EISADDR_IDX (p);
7755 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
7756         cpu.du.Dk_PTR_W[eisaddr_idx] = (cpu.du.Dk_PTR_W[eisaddr_idx] + 1) & AMASK;     // bump source to next address
7757 #else
7758         p->address = (p->address + 1) & AMASK;       // goto next dstAddr in memory
7759 #endif
7760     }
7761 
7762     w = EISRead(cpup, p);      // read dst memory into w
7763 
7764 // AL39, Figure 2-5
7765     switch (*pos)
7766     {
7767         case 0:
7768             //w = bitfieldInsert36(w, char9, 27, 9);
7769             w = setbits36_9 (w, 0, char9);
7770             break;
7771         case 1:
7772             //w = bitfieldInsert36(w, char9, 18, 9);
7773             w = setbits36_9 (w, 9, char9);
7774             break;
7775         case 2:
7776             //w = bitfieldInsert36(w, char9, 9, 9);
7777             w = setbits36_9 (w, 18, char9);
7778             break;
7779         case 3:
7780             //w = bitfieldInsert36(w, char9, 0, 9);
7781             w = setbits36_9 (w, 27, char9);
7782             break;
7783     }
7784 
7785     EISWriteIdx (cpup, p, 0, w, true); // XXX this is the inefficient part!
7786 
7787     *pos += 1;       // to next byte.
7788 }
7789 
7790 /*
7791  * write a 4-, or 9-bit numeric char to dstAddr ....
7792  */
7793 
7794 static void EISwrite49(cpu_state_t * cpup, EISaddr *p, int *pos, int tn, word9 c49)
     /* [previous][next][first][last][top][bottom][index][help] */
7795 {
7796     switch(tn)
7797     {
7798         case CTN4:
7799             EISwrite4(cpup, p, pos, (word4) c49);
7800             return;
7801         case CTN9:
7802             EISwrite9(cpup, p, pos, c49);
7803             return;
7804     }
7805 }
7806 
7807 void mvn (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
7808 {
7809     /*
7810      * EXPLANATION:
7811      * Starting at location YC1, the decimal number of data type TN1 and sign
7812      * and decimal type S1 is moved, properly scaled, to the decimal number of
7813      * data type TN2 and sign and decimal type S2 that starts at location YC2.
7814      * If S2 indicates a fixed-point format, the results are stored as L2
7815      * digits using scale factor SF2, and thereby may cause
7816      * most-significant-digit overflow and/or least- significant-digit
7817      * truncation.
7818      * If P = 1, positive signed 4-bit results are stored using octal 13 as the
7819      * plus sign. Rounding is legal for both fixed-point and floating-point
7820      * formats. If P = 0, positive signed 4-bit results are stored using octal
7821      * 14 as the plus sign.
7822      * Provided that string 1 and string 2 are not overlapped, the contents of
7823      * the decimal number that starts in location YC1 remain unchanged.
7824      */
7825 
7826     EISstruct * e = & cpu.currentEISinstruction;
7827 
7828     fault_ipr_subtype_ mod_fault = 0;
7829 
7830 #if !defined(EIS_SETUP)
7831     setupOperandDescriptor(cpup, 1, &mod_fault);
7832     setupOperandDescriptor(cpup, 2, &mod_fault);
7833 #endif
7834 
7835     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
7836     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
7837 
7838     L68_ (
7839       // L68 raises it immediately
7840       if (mod_fault)
7841         {
7842           doFault (FAULT_IPR,
7843                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
7844                    "Illegal modifier");
7845         }
7846     )
7847 
7848     // Bits 2-8 MBZ
7849     if (IWB_IRODD & 0377000000000)
7850      doFault (FAULT_IPR,
7851               (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault},
7852               "mvn 2-8 MBZ");
7853 
7854     DPS8M_ (
7855       // DPS8M raises it delayed
7856       if (mod_fault)
7857         {
7858           doFault (FAULT_IPR,
7859                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
7860                    "Illegal modifier");
7861         }
7862     )
7863 
7864     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character
7865                                               //  control
7866     word1 T = getbits36_1 (cpu.cu.IWB, 9);
7867     bool R = getbits36_1 (cpu.cu.IWB, 10) != 0;  // rounding bit
7868     PNL (L68_ (if (R)
7869       DU_CYCLE_FRND;))
7870 
7871     uint srcTN = e->TN1;    // type of chars in src
7872 
7873     uint dstTN = e->TN2;    // type of chars in dst
7874     uint dstCN = e->CN2;    // starting at char pos CN
7875 
7876     sim_debug (DBG_CAC, & cpu_dev,
7877                "mvn(1): TN1 %d CN1 %d N1 %d TN2 %d CN2 %d N2 %d\r\n",
7878                e->TN1, e->CN1, e->N1, e->TN2, e->CN2, e->N2);
7879     sim_debug (DBG_CAC, & cpu_dev,
7880                "mvn(2): SF1 %d              SF2 %d\r\n",
7881                e->SF1, e->SF2);
7882     sim_debug (DBG_CAC, & cpu_dev,
7883                "mvn(3): OP1 %012"PRIo64" OP2 %012"PRIo64"\r\n",
7884                e->OP1, e->OP2);
7885 
7886     int n1 = 0, n2 = 0, sc1 = 0;
7887 
7888     /*
7889      * Here we need to distinguish between 4 type of numbers.
7890      *
7891      * CSFL - Floating-point, leading sign
7892      * CSLS - Scaled fixed-point, leading sign
7893      * CSTS - Scaled fixed-point, trailing sign
7894      * CSNS - Scaled fixed-point, unsigned
7895      */
7896 
7897     // determine precision
7898     switch(e->S1)
7899     {
7900         case CSFL:
7901             n1 = (int) e->N1 - 1; // need to account for the - sign
7902             if (srcTN == CTN4)
7903                 n1 -= 2;    // 2 4-bit digits exponent
7904             else
7905                 n1 -= 1;    // 1 9-bit digit exponent
7906             sc1 = 0;        // no scaling factor
7907             break;
7908 
7909         case CSLS:
7910         case CSTS:
7911             n1 = (int) e->N1 - 1; // only 1 sign
7912             sc1 = -e->SF1;
7913             break;
7914 
7915         case CSNS:
7916             n1 = (int) e->N1;     // no sign
7917             sc1 = -e->SF1;
7918             break;  // no sign wysiwyg
7919     }
7920 
7921     sim_debug (DBG_CAC, & cpu_dev, "n1 %d sc1 %d\r\n", n1, sc1);
7922 
7923     // RJ78: An Illegal Procedure fault occurs if:
7924     // The values for the number of characters (N1 or N2) of the data
7925     // descriptors are not large enough to hold the number of characters
7926     // required for the specified sign and/or exponent, plus at least one
7927     // digit.
7928 
7929     if (n1 < 1)
7930         doFault (FAULT_IPR, fst_ill_proc, "mvn adjusted n1<1");
7931 
7932     switch(e->S2)
7933     {
7934         case CSFL:
7935             n2 = (int) e->N2 - 1; // need to account for the sign
7936             if (dstTN == CTN4)
7937                 n2 -= 2;    // 2 4-bit digit exponent
7938             else
7939                 n2 -= 1;    // 1 9-bit digit exponent
7940             break;
7941 
7942         case CSLS:
7943         case CSTS:
7944             n2 = (int) e->N2 - 1; // 1 sign
7945             break;
7946 
7947         case CSNS:
7948             n2 = (int) e->N2;     // no sign
7949             break;          // no sign wysiwyg
7950     }
7951 
7952     sim_debug (DBG_CAC, & cpu_dev, "n2 %d\r\n", n2);
7953 
7954     if (n2 < 1)
7955         doFault (FAULT_IPR, fst_ill_proc, "mvn adjusted n2<1");
7956 
7957     decContext set;
7958     decContextDefaultDPS8(&set);
7959     set.traps=0;
7960 
7961     decNumber _1;
7962 
7963     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
7964 
7965     decNumber *op1 = decBCD9ToNumber (e->inBuffer, n1, sc1, &_1);
7966 
7967     if (e->sign == -1)
7968         op1->bits |= DECNEG;
7969     if (e->S1 == CSFL)
7970         op1->exponent = e->exponent;
7971     if (decNumberIsZero (op1))
7972         op1->exponent = 127;
7973 
7974     if_sim_debug (DBG_CAC, & cpu_dev)
7975     {
7976         PRINTDEC ("mvn input (op1)", op1);
7977     }
7978 
7979     bool Ovr = false, EOvr = false, Trunc = false;
7980 
7981     uint8_t out [256];
7982     char * res = formatDecimal (out, & set, op1, n2, (int) e->S2, e->SF2, R,
7983                                 & Ovr, & Trunc);
7984 
7985     sim_debug (DBG_CAC, & cpu_dev, "mvn res: '%s'\r\n", res);
7986 
7987     // now write to memory in proper format.....
7988 
7989     //word18 dstAddr = e->dstAddr;
7990     int pos = (int) dstCN;
7991 
7992     // 1st, take care of any leading sign .......
7993     switch(e->S2)
7994     {
7995         case CSFL:  // floating-point, leading sign.
7996         case CSLS:  // fixed-point, leading sign
7997             switch(dstTN)
7998             {
7999                 case CTN4:
8000  // If TN2 and S2 specify a 4-bit signed number and P = 1, then the 13(8) plus
8001  // sign character is placed appropriately if the result of the operation is
8002  // positive.
8003                     if (e->P)
8004                         // special +
8005                         EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8006                                    (decNumberIsNegative (op1) &&
8007                                     ! decNumberIsZero(op1)) ? 015 : 013);
8008                     else
8009                         // default +
8010                         EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8011                                     (decNumberIsNegative (op1) &&
8012                                      ! decNumberIsZero (op1)) ? 015 : 014);
8013                     break;
8014                 case CTN9:
8015                     EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8016                                 (decNumberIsNegative (op1) &&
8017                                  ! decNumberIsZero (op1)) ? '-' : '+');
8018                     break;
8019             }
8020             break;
8021 
8022         case CSTS:  // nuttin' to do here .....
8023         case CSNS:
8024             break;  // no sign wysiwyg
8025     }
8026 
8027     // 2nd, write the digits .....
8028     for (int i = 0 ; i < n2 ; i ++)
8029         switch (dstTN)
8030         {
8031             case CTN4:
8032                 EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8033                             (word9) (res[i] - '0'));
8034                 break;
8035             case CTN9:
8036                 EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN, (word9) res[i]);
8037                 break;
8038         }
8039 
8040     // 3rd, take care of any trailing sign or exponent ...
8041     switch(e->S2)
8042     {
8043         case CSTS:  // write trailing sign ....
8044             switch(dstTN)
8045             {
8046                 case CTN4:
8047 // If TN2 and S2 specify a 4-bit signed number and P = 1, then the 13(8) plus
8048 // sign character is placed appropriately if the result of the operation is
8049 // positive.
8050                     if (e->P)
8051                         // special +
8052                         EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8053                                     (decNumberIsNegative (op1) &&
8054                                      ! decNumberIsZero(op1)) ? 015 :  013);
8055                     else
8056                         // default +
8057                         EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8058                                     (decNumberIsNegative (op1) &&
8059                                      ! decNumberIsZero (op1)) ? 015 :  014);
8060                     break;
8061 
8062                 case CTN9:
8063                     EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8064                                 (decNumberIsNegative (op1) &&
8065                                  ! decNumberIsZero(op1)) ? '-' : '+');
8066                     break;
8067             }
8068             break;
8069 
8070         case CSFL:  // floating-point, leading sign.
8071             // write the exponent
8072             switch(dstTN)
8073             {
8074                 case CTN4:
8075                     EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8076                                 (op1->exponent >> 4) & 0xf); // upper 4-bits
8077                     EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8078                                 op1->exponent       & 0xf); // lower 4-bits
8079                     break;
8080                 case CTN9:
8081                     EISwrite49 (cpup, & e->ADDR2, & pos, (int) dstTN,
8082                                  op1->exponent & 0xff); // write 8-bit exponent
8083                 break;
8084             }
8085             break;
8086 
8087         case CSLS:  // fixed point, leading sign - already done
8088         case CSNS:  // fixed point, unsigned - nuttin' needed to do
8089             break;
8090     }
8091 
8092     // set flags, etc ...
8093     if (e->S2 == CSFL)
8094     {
8095         if (op1->exponent > 127)
8096         {
8097             SET_I_EOFL;
8098             EOvr = true;
8099         }
8100         if (op1->exponent < -128)
8101         {
8102             SET_I_EUFL;
8103             EOvr = true;
8104         }
8105     }
8106 
8107 sim_debug (DBG_CAC, & cpu_dev, "is neg %o\r\n", decNumberIsNegative(op1));
8108 sim_debug (DBG_CAC, & cpu_dev, "is zero %o\r\n", decNumberIsZero(op1));
8109 sim_debug (DBG_CAC, & cpu_dev, "R %o\r\n", R);
8110 sim_debug (DBG_CAC, & cpu_dev, "Trunc %o\r\n", Trunc);
8111 sim_debug (DBG_CAC, & cpu_dev, "TRUNC %o\r\n", TST_I_TRUNC);
8112 sim_debug (DBG_CAC, & cpu_dev, "OMASK %o\r\n", TST_I_OMASK);
8113 sim_debug (DBG_CAC, & cpu_dev, "tstOVFfault %o\r\n", tstOVFfault (cpup));
8114 sim_debug (DBG_CAC, & cpu_dev, "T %o\r\n", T);
8115 sim_debug (DBG_CAC, & cpu_dev, "EOvr %o\r\n", EOvr);
8116 sim_debug (DBG_CAC, & cpu_dev, "Ovr %o\r\n", Ovr);
8117     // set negative indicator if op3 < 0
8118     SC_I_NEG (decNumberIsNegative(op1) && !decNumberIsZero(op1));
8119 
8120     // set zero indicator if op3 == 0
8121     SC_I_ZERO (decNumberIsZero(op1));
8122 
8123     // If the truncation condition exists without rounding, then ON;
8124     // otherwise OFF
8125     SC_I_TRUNC (!R && Trunc);
8126 
8127     cleanupOperandDescriptor (cpup, 1);
8128     cleanupOperandDescriptor (cpup, 2);
8129 
8130     if (TST_I_TRUNC && T && tstOVFfault (cpup))
8131         doFault (FAULT_OFL, fst_zero, "mvn truncation(overflow) fault");
8132     if (EOvr && tstOVFfault (cpup))
8133         doFault (FAULT_OFL, fst_zero, "mvn over/underflow fault");
8134     if (Ovr)
8135     {
8136         SET_I_OFLOW;
8137         if (tstOVFfault (cpup))
8138           doFault (FAULT_OFL, fst_zero, "mvn overflow fault");
8139     }
8140 }
8141 
8142 void csl (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
8143   {
8144     EISstruct * e = & cpu.currentEISinstruction;
8145 
8146     // For i = bits 1, 2, ..., minimum (N1,N2)
8147     //   m = C(Y-bit1)i-1 || C(Y-bit2)i-1 (a 2-bit number)
8148     //   C(BOLR)m -> C(Y-bit2)i-1
8149     // If N1 < N2, then for i = N1+l, N1+2, ..., N2
8150     //   m = C(F) || C(Y-bit2)i-1 (a 2-bit number)
8151     //   C(BOLR)m -> C(Y-bit2)i-1
8152     //
8153     // INDICATORS: (Indicators not listed are not affected)
8154     //     Zero If C(Y-bit2) = 00...0, then ON; otherwise OFF
8155     //     Truncation If N1 > N2, then ON; otherwise OFF
8156     //
8157     // NOTES: If N1 > N2, the low order (N1-N2) bits of C(Y-bit1) are not
8158     // processed and the truncation indicator is set ON.
8159     //
8160     // If T = 1 and the truncation indicator is set ON by execution of the
8161     // instruction, then a truncation (overflow) fault occurs.
8162     //
8163     // BOLR
8164     // If first operand    and    second operand    then result
8165     // bit is:                    bit is:           is from bit:
8166     //        0                          0                      5
8167     //        0                          1                      6
8168     //        1                          0                      7
8169     //        1                          1                      8
8170     //
8171     // The Boolean operations most commonly used are
8172     //                  BOLR Field Bits
8173     // Operation        5      6      7      8
8174     //
8175     // MOVE             0      0      1      1
8176     // AND              0      0      0      1
8177     // OR               0      1      1      1
8178     // NAND             1      1      1      0
8179     // EXCLUSIVE OR     0      1      1      0
8180     // Clear            0      0      0      0
8181     // Invert           1      1      0      0
8182     //
8183 
8184 // 0 0 0 0  Clear
8185 // 0 0 0 1  a AND b
8186 // 0 0 1 0  a AND !b
8187 // 0 0 1 1  a
8188 // 0 1 0 0  !a AND b
8189 // 0 1 0 1  b
8190 // 0 1 1 0  a XOR b
8191 // 0 1 1 1  a OR b
8192 // 1 0 0 0  !a AND !b     !(a OR b)
8193 // 1 0 0 1  a == b        !(a XOR b)
8194 // 1 0 1 0  !b
8195 // 1 0 1 1  !b OR A
8196 // 1 1 0 0  !a
8197 // 1 1 0 1  !b AND a
8198 // 1 1 1 0  a NAND b
8199 // 1 1 1 1  Set
8200 
8201     fault_ipr_subtype_ mod_fault = 0;
8202 
8203 #if !defined(EIS_SETUP)
8204     setupOperandDescriptor (cpup, 1, & mod_fault);
8205     setupOperandDescriptor (cpup, 2, & mod_fault);
8206 #endif
8207 
8208     parseBitstringOperandDescriptor (cpup, 1, & mod_fault);
8209     parseBitstringOperandDescriptor (cpup, 2, & mod_fault);
8210 
8211     L68_ (
8212       // L68 raises it immediately
8213       if (mod_fault)
8214           doFault (FAULT_IPR,
8215                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
8216                    "Illegal modifier");
8217     )
8218 
8219     // Bits 1-4 and 10 MBZ
8220     if (IWB_IRODD & 0360200000000)
8221       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "csl 1-4,10 MBZ");
8222 
8223     DPS8M_ (
8224       // DPS8M raises it delayed
8225       if (mod_fault)
8226           doFault (FAULT_IPR,
8227                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
8228                    "Illegal modifier");
8229     )
8230 
8231     e->ADDR1.cPos = (int) e->C1;
8232     e->ADDR2.cPos = (int) e->C2;
8233 
8234     e->ADDR1.bPos = (int) e->B1;
8235     e->ADDR2.bPos = (int) e->B2;
8236 
8237     bool F = getbits36_1 (cpu.cu.IWB, 0) != 0;   // fill bit
8238     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;   // T (enablefault) bit
8239 
8240     uint BOLR = getbits36_4 (cpu.cu.IWB, 5);   // T (enablefault) bit
8241     bool B5 = !! (BOLR & 8);
8242     bool B6 = !! (BOLR & 4);
8243     bool B7 = !! (BOLR & 2);
8244     bool B8 = !! (BOLR & 1);
8245 
8246     e->ADDR1.mode = eRWreadBit;
8247 
8248 #if !defined(EIS_PTR)
8249     sim_debug (DBG_TRACEEXT, & cpu_dev,
8250                "CSL N1 %d N2 %d\r\n"
8251                "CSL C1 %d C2 %d B1 %d B2 %d F %o T %d\r\n"
8252                "CSL BOLR %u%u%u%u\r\n"
8253                "CSL op1 SNR %06o WORDNO %06o CHAR %d BITNO %d\r\n"
8254                "CSL op2 SNR %06o WORDNO %06o CHAR %d BITNO %d\r\n",
8255                e -> N1, e -> N2,
8256                e -> C1, e -> C2, e -> B1, e -> B2, F, T,
8257                B5, B6, B7, B8,
8258                e -> addr [0].SNR, e -> addr [0].address,
8259                e -> addr [0].cPos, e -> addr [0].bPos,
8260                e -> addr [1].SNR, e -> addr [1].address,
8261                e -> addr [1].cPos, e -> addr [1].bPos);
8262 #endif
8263 
8264     bool bR = false; // result bit
8265 
8266     PNL (L68_ (if (max (e->N1, e->N2) < 128)
8267                  DU_CYCLE_FLEN_128;))
8268 
8269     for( ; cpu.du.CHTALLY < min(e->N1, e->N2); cpu.du.CHTALLY += 1)
8270       {
8271         tandd_eis_sample (cpup, cpu.du.CHTALLY, 36);
8272         bool b1 = EISgetBitRWN(cpup, &e->ADDR1, true);
8273         e->ADDR2.mode = eRWreadBit;
8274         bool b2 = EISgetBitRWN(cpup, &e->ADDR2, true);
8275 
8276         if (b1) if (b2) bR = B8; else bR = B7; else if (b2) bR = B6; else bR = B5;
8277 
8278         if (bR)
8279           {
8280             //CLR_I_ZERO);
8281             cpu.du.Z = 0;
8282           }
8283 
8284         // write out modified bit
8285         e->ADDR2.bit = bR ? 1 : 0;              // set bit contents to write
8286         e->ADDR2.mode = eRWwriteBit;    // we want to write the bit
8287         // if ADDR1 is on a word boundary, it might fault on the next loop,
8288         // so we flush the write in case.
8289         EISgetBitRWN(cpup, &e->ADDR2, e->ADDR1.last_bit_posn == 35);    // write bit w/ addr increment to memory
8290     }
8291 
8292     if (e->N1 < e->N2)
8293       {
8294         for(; cpu.du.CHTALLY < e->N2; cpu.du.CHTALLY += 1)
8295           {
8296             tandd_eis_sample (cpup, cpu.du.CHTALLY, 36);
8297             bool b1 = F;
8298 
8299             e->ADDR2.mode = eRWreadBit;
8300             bool b2 = EISgetBitRWN(cpup, &e->ADDR2, true);
8301 
8302             if (b1) if (b2) bR = B8; else bR = B7; else if (b2) bR = B6; else bR = B5;
8303 
8304             if (bR)
8305               {
8306                 //CLR_I_ZERO;
8307                 cpu.du.Z = 0;
8308               }
8309 
8310             // write out modified bit
8311             e->ADDR2.bit = bR ? 1 : 0;
8312             e->ADDR2.mode = eRWwriteBit;
8313             // if ADDR1 is on a word boundary, it might fault on the next loop,
8314             // so we flush the write in case.
8315             EISgetBitRWN(cpup, &e->ADDR2, e->ADDR1.last_bit_posn == 35);    // write bit w/ addr increment to memory
8316           }
8317       }
8318 
8319     EISWriteCache (cpup, &e->ADDR2);
8320 
8321     cleanupOperandDescriptor (cpup, 1);
8322     cleanupOperandDescriptor (cpup, 2);
8323 
8324     SC_I_ZERO (cpu.du.Z);
8325     if (e->N1 > e->N2)
8326       {
8327         // NOTES: If N1 > N2, the low order (N1-N2) bits of C(Y-bit1) are not
8328         // processed and the truncation indicator is set ON.
8329         //
8330         // If T = 1 and the truncation indicator is set ON by execution of the
8331         // instruction, then a truncation (overflow) fault occurs.
8332 
8333         SET_I_TRUNC;
8334         if (T && tstOVFfault (cpup))
8335           doFault(FAULT_OFL, fst_zero, "csl truncation fault");
8336       }
8337     else
8338       {
8339         CLR_I_TRUNC;
8340       }
8341   }
8342 
8343 /*
8344  * return B (bit position), C (char position) and word offset given:
8345  *  'length' # of bits, etc ....
8346  *  'initC' initial char position (C)
8347  *  'initB' initial bit position
8348  */
8349 
8350 static void getBitOffsets(int length, int initC, int initB, int *nWords, int *newC, int *newB)
     /* [previous][next][first][last][top][bottom][index][help] */
8351 {
8352     if (length == 0)
8353         return;
8354 
8355     int endBit = (length + 9 * initC + initB - 1) % 36;
8356 
8357     //int numWords = (length + 35) / 36;  // how many additional words will the bits take up?
8358     int numWords = (length + 9 * initC + initB + 35) / 36;  // how many additional words will the bits take up?
8359     int lastWordOffset = numWords - 1;
8360 
8361     if (lastWordOffset > 0)          // more that the 1 word needed?
8362         *nWords = lastWordOffset;  // # of additional words
8363     else
8364         *nWords = 0;    // no additional words needed
8365 
8366     *newC = endBit / 9; // last character number
8367     *newB = endBit % 9; // last bit number
8368 }
8369 
8370 static bool EISgetBitRWNR (cpu_state_t * cpup, EISaddr * p, bool flush)
     /* [previous][next][first][last][top][bottom][index][help] */
8371   {
8372     int baseCharPosn = (p -> cPos * 9);     // 9-bit char bit position
8373     int baseBitPosn = baseCharPosn + p -> bPos;
8374     baseBitPosn -= (int) cpu.du.CHTALLY;
8375 
8376     int bitPosn = baseBitPosn % 36;
8377     int woff = baseBitPosn / 36;
8378     while (bitPosn < 0)
8379       {
8380         bitPosn += 36;
8381         woff -= 1;
8382       }
8383 
8384 /* if (bitPosn < 0) { */
8385 /* sim_printf ("cPos %d bPos %d\r\n", p->cPos, p->bPos); */
8386 /* sim_printf ("baseCharPosn %d baseBitPosn %d\r\n", baseCharPosn, baseBitPosn); */
8387 /* sim_printf ("CHTALLY %d baseBitPosn %d\r\n", cpu.du.CHTALLY, baseBitPosn); */
8388 /* sim_printf ("bitPosn %d woff %d\r\n", bitPosn, woff); */
8389 /* sim_warn ("EISgetBitRWNR oops\r\n"); */
8390 /* return false; */
8391 /* } */
8392 
8393 #if defined(EIS_PTR)
8394     long eisaddr_idx = EISADDR_IDX (p);
8395 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
8396     word18 saveAddr = cpu.du.Dk_PTR_W[eisaddr_idx];
8397     cpu.du.Dk_PTR_W[eisaddr_idx] += (word18) woff;
8398     cpu.du.Dk_PTR_W[eisaddr_idx] &= AMASK;
8399 #else
8400     word18 saveAddr = p -> address;
8401     //p -> address += (word18) woff;
8402     //ubsan
8403     p->address = (word18) (((word18s) p->address) + (word18s) woff);
8404 #endif
8405 
8406     p -> data = EISRead (cpup, p); // read data word from memory
8407 
8408     if (p -> mode == eRWreadBit)
8409       {
8410         p -> bit = getbits36_1 (p -> data, (uint) bitPosn);
8411       }
8412     else if (p -> mode == eRWwriteBit)
8413       {
8414         //p -> data = bitfieldInsert36 (p -> data, p -> bit, bitPosn, 1);
8415         p -> data = setbits36_1 (p -> data, (uint) bitPosn, p -> bit);
8416 
8417         EISWriteIdx (cpup, p, 0, p -> data, flush); // write data word to memory
8418       }
8419 
8420     p->last_bit_posn = bitPosn;
8421 
8422 #if defined(EIS_PTR)
8423     cpu.du.Dk_PTR_W[eisaddr_idx] = saveAddr;
8424 #else
8425     p -> address = saveAddr;
8426 #endif
8427     return p -> bit;
8428   }
8429 
8430 void csr (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
8431   {
8432     EISstruct * e = & cpu.currentEISinstruction;
8433 
8434     // For i = bits 1, 2, ..., minimum (N1,N2)
8435     //   m = C(Y-bit1)N1-i || C(Y-bit2)N2-i (a 2-bit number)
8436     //   C(BOLR)m -> C( Y-bit2)N2-i
8437     // If N1 < N2, then for i = N1+i, N1+2, ..., N2
8438     //   m = C(F) || C(Y-bit2)N2-i (a 2-bit number)
8439     //    C(BOLR)m -> C( Y-bit2)N2-i
8440     // INDICATORS: (Indicators not listed are not affected)
8441     //     Zero If C(Y-bit2) = 00...0, then ON; otherwise OFF
8442     //     Truncation If N1 > N2, then ON; otherwise OFF
8443     //
8444     // NOTES: If N1 > N2, the low order (N1-N2) bits of C(Y-bit1) are not
8445     // processed and the truncation indicator is set ON.
8446     //
8447     // If T = 1 and the truncation indicator is set ON by execution of the
8448     // instruction, then a truncation (overflow) fault occurs.
8449     //
8450     // BOLR
8451     // If first operand    and    second operand    then result
8452     // bit is:                    bit is:           is from bit:
8453     //        0                          0                      5
8454     //        0                          1                      6
8455     //        1                          0                      7
8456     //        1                          1                      8
8457     //
8458     // The Boolean operations most commonly used are
8459     //                  BOLR Field Bits
8460     // Operation        5      6      7      8
8461     //
8462     // MOVE             0      0      1      1
8463     // AND              0      0      0      1
8464     // OR               0      1      1      1
8465     // NAND             1      1      1      0
8466     // EXCLUSIVE OR     0      1      1      0
8467     // Clear            0      0      0      0
8468     // Invert           1      1      0      0
8469     //
8470 
8471     fault_ipr_subtype_ mod_fault = 0;
8472 
8473 #if !defined(EIS_SETUP)
8474     setupOperandDescriptor(cpup, 1, &mod_fault);
8475     setupOperandDescriptor(cpup, 2, &mod_fault);
8476 #endif
8477 
8478     parseBitstringOperandDescriptor(cpup, 1, &mod_fault);
8479     parseBitstringOperandDescriptor(cpup, 2, &mod_fault);
8480 
8481     L68_ (
8482       // L68 raises it immediately
8483       if (mod_fault)
8484           doFault (FAULT_IPR,
8485                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
8486                    "Illegal modifier");
8487     )
8488 
8489     // Bits 1-4 and 10 MBZ
8490     if (IWB_IRODD & 0360200000000)
8491       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "csr 1-4,10 MBZ");
8492 
8493     DPS8M_ (
8494       // DPS8M raises it delayed
8495       if (mod_fault)
8496           doFault (FAULT_IPR,
8497                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
8498                    "Illegal modifier");
8499     )
8500 
8501     e->ADDR1.cPos = (int) e->C1;
8502     e->ADDR2.cPos = (int) e->C2;
8503 
8504     e->ADDR1.bPos = (int) e->B1;
8505     e->ADDR2.bPos = (int) e->B2;
8506 
8507     // get new char/bit offsets
8508     int numWords1=0, numWords2=0;
8509 
8510     getBitOffsets((int) e->N1, (int) e->C1, (int) e->B1, &numWords1, &e->ADDR1.cPos, &e->ADDR1.bPos);
8511     PNL (cpu.du.D1_PTR_W += (word18) numWords1);
8512     PNL (cpu.du.D1_PTR_W &= AMASK);
8513 #if defined(EIS_PTR)
8514     cpu.du.D1_PTR_W += (word18) numWords1;
8515     cpu.du.D1_PTR_W &= AMASK;
8516 #else
8517     e->ADDR1.address += (word18) numWords1;
8518 #endif
8519 
8520     sim_debug (DBG_TRACEEXT, & cpu_dev,
8521                "CSR N1 %d C1 %d B1 %d numWords1 %d cPos %d bPos %d\r\n",
8522                e->N1, e->C1, e->B1, numWords1, e->ADDR1.cPos, e->ADDR1.bPos);
8523     getBitOffsets((int) e->N2, (int) e->C2, (int) e->B2, &numWords2, &e->ADDR2.cPos, &e->ADDR2.bPos);
8524     sim_debug (DBG_TRACEEXT, & cpu_dev,
8525                "CSR N2 %d C2 %d B2 %d numWords2 %d cPos %d bPos %d\r\n",
8526                e->N2, e->C2, e->B2, numWords2, e->ADDR2.cPos, e->ADDR2.bPos);
8527     PNL (cpu.du.D2_PTR_W += (word18) numWords1);
8528     PNL (cpu.du.D2_PTR_W &= AMASK);
8529 #if defined(EIS_PTR)
8530     cpu.du.D2_PTR_W += (word18) numWords1;
8531     cpu.du.D2_PTR_W &= AMASK;
8532 #else
8533     e->ADDR2.address += (word18) numWords2;
8534 #endif
8535 
8536     bool F = getbits36_1 (cpu.cu.IWB, 0) != 0;   // fill bit
8537     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;   // T (enablefault) bit
8538 
8539     uint BOLR = getbits36_4 (cpu.cu.IWB, 5);   // T (enablefault) bit
8540     bool B5 = !! (BOLR & 8);
8541     bool B6 = !! (BOLR & 4);
8542     bool B7 = !! (BOLR & 2);
8543     bool B8 = !! (BOLR & 1);
8544 
8545     e->ADDR1.mode = eRWreadBit;
8546 
8547     CLR_I_TRUNC;     // assume N1 <= N2
8548 
8549     bool bR = false; // result bit
8550 
8551     PNL (L68_ (if (max (e->N1, e->N2) < 128)
8552                  DU_CYCLE_FLEN_128;))
8553 
8554     for( ; cpu.du.CHTALLY < min(e->N1, e->N2); cpu.du.CHTALLY += 1)
8555       {
8556         tandd_eis_sample (cpup, cpu.du.CHTALLY, 36);
8557         bool b1 = EISgetBitRWNR(cpup, &e->ADDR1, true);
8558 
8559         e->ADDR2.mode = eRWreadBit;
8560         bool b2 = EISgetBitRWNR(cpup, &e->ADDR2, true);
8561 
8562         if (b1) if (b2) bR = B8; else bR = B7; else if (b2) bR = B6; else bR = B5;
8563 
8564         if (bR)
8565           cpu.du.Z = 0;
8566 
8567         // write out modified bit
8568         e->ADDR2.bit = bR ? 1 : 0;              // set bit contents to write
8569         e->ADDR2.mode = eRWwriteBit;    // we want to write the bit
8570         // if ADDR1 is on a word boundary, it might fault on the next loop,
8571         // so we flush the write in case.
8572         EISgetBitRWNR(cpup, &e->ADDR2, e->ADDR1.last_bit_posn == 0);
8573       }
8574 
8575     if (e->N1 < e->N2)
8576       {
8577         for(; cpu.du.CHTALLY < e->N2; cpu.du.CHTALLY += 1)
8578           {
8579             tandd_eis_sample (cpup, cpu.du.CHTALLY, 36);
8580             bool b1 = F;
8581 
8582             e->ADDR2.mode = eRWreadBit;
8583             bool b2 = EISgetBitRWNR(cpup, &e->ADDR2, true);
8584 
8585             if (b1) if (b2) bR = B8; else bR = B7; else if (b2) bR = B6; else bR = B5;
8586 
8587             if (bR)
8588               {
8589                 //CLR_I_ZERO;
8590                 cpu.du.Z = 0;
8591               }
8592 
8593             // write out modified bit
8594             e->ADDR2.bit = bR ? 1 : 0;
8595             e->ADDR2.mode = eRWwriteBit;
8596             // if ADDR1 is on a word boundary, it might fault on the next loop,
8597             // so we flush the write in case.
8598             EISgetBitRWNR(cpup, &e->ADDR2, e->ADDR1.last_bit_posn == 0);
8599           }
8600       }
8601 
8602     EISWriteCache (cpup, &e->ADDR2);
8603 
8604     cleanupOperandDescriptor (cpup, 1);
8605     cleanupOperandDescriptor (cpup, 2);
8606 
8607     SC_I_ZERO (cpu.du.Z);
8608     if (e->N1 > e->N2)
8609       {
8610         // NOTES: If N1 > N2, the low order (N1-N2) bits of C(Y-bit1) are not
8611         // processed and the truncation indicator is set ON.
8612         //
8613         // If T = 1 and the truncation indicator is set ON by execution of the
8614         // instruction, then a truncation (overflow) fault occurs.
8615 
8616         SET_I_TRUNC;
8617         if (T && tstOVFfault (cpup))
8618           doFault(FAULT_OFL, fst_zero, "csr truncation fault");
8619       }
8620     else
8621       {
8622         CLR_I_TRUNC;
8623       }
8624   }
8625 
8626 void sztl (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
8627   {
8628     // The execution of this instruction is identical to the Combine
8629     // Bit Strings Left (csl) instruction except that C(BOLR)m is
8630     // not placed into C(Y-bit2)i-1.
8631 
8632     EISstruct * e = & cpu.currentEISinstruction;
8633 
8634     // For i = bits 1, 2, ..., minimum (N1,N2)
8635     //   m = C(Y-bit1)i-1 || C(Y-bit2)i-1 (a 2-bit number)
8636     //   C(BOLR)m -> C(Y-bit2)i-1
8637     // If N1 < N2, then for i = N1+l, N1+2, ..., N2
8638     //   m = C(F) || C(Y-bit2)i-1 (a 2-bit number)
8639     //   C(BOLR)m -> C(Y-bit2)i-1
8640     //
8641     // INDICATORS: (Indicators not listed are not affected)
8642     //     Zero If C(Y-bit2) = 00...0, then ON; otherwise OFF
8643     //     Truncation If N1 > N2, then ON; otherwise OFF
8644     //
8645     // NOTES: If N1 > N2, the low order (N1-N2) bits of C(Y-bit1) are not
8646     // processed and the truncation indicator is set ON.
8647     //
8648     // If T = 1 and the truncation indicator is set ON by execution of the
8649     // instruction, then a truncation (overflow) fault occurs.
8650     //
8651     // BOLR
8652     // If first operand    and    second operand    then result
8653     // bit is:                    bit is:           is from bit:
8654     //        0                          0                      5
8655     //        0                          1                      6
8656     //        1                          0                      7
8657     //        1                          1                      8
8658     //
8659     // The Boolean operations most commonly used are
8660     //                  BOLR Field Bits
8661     // Operation        5      6      7      8
8662     //
8663     // MOVE             0      0      1      1
8664     // AND              0      0      0      1
8665     // OR               0      1      1      1
8666     // NAND             1      1      1      0
8667     // EXCLUSIVE OR     0      1      1      0
8668     // Clear            0      0      0      0
8669     // Invert           1      1      0      0
8670     //
8671 
8672 // 0 0 0 0  Clear
8673 // 0 0 0 1  a AND b
8674 // 0 0 1 0  a AND !b
8675 // 0 0 1 1  a
8676 // 0 1 0 0  !a AND b
8677 // 0 1 0 1  b
8678 // 0 1 1 0  a XOR b
8679 // 0 1 1 1  a OR b
8680 // 1 0 0 0  !a AND !b     !(a OR b)
8681 // 1 0 0 1  a == b        !(a XOR b)
8682 // 1 0 1 0  !b
8683 // 1 0 1 1  !b OR A
8684 // 1 1 0 0  !a
8685 // 1 1 0 1  !b AND a
8686 // 1 1 1 0  a NAND b
8687 // 1 1 1 1  Set
8688 
8689     fault_ipr_subtype_ mod_fault = 0;
8690 
8691 #if !defined(EIS_SETUP)
8692     setupOperandDescriptor (cpup, 1, &mod_fault);
8693     setupOperandDescriptor (cpup, 2, &mod_fault);
8694 #endif
8695 
8696     parseBitstringOperandDescriptor (cpup, 1, &mod_fault);
8697     parseBitstringOperandDescriptor (cpup, 2, &mod_fault);
8698 
8699     L68_ (
8700       // L68 raises it immediately
8701       if (mod_fault)
8702           doFault (FAULT_IPR,
8703                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
8704                    "Illegal modifier");
8705     )
8706 
8707     // Bits 1-4 and 10 MBZ
8708     if (IWB_IRODD & 0360200000000)
8709       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "csl 1-4,10 MBZ");
8710 
8711     DPS8M_ (
8712       // DPS8M raises it delayed
8713       if (mod_fault)
8714           doFault (FAULT_IPR,
8715                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
8716                    "Illegal modifier");
8717     )
8718 
8719     e->ADDR1.cPos = (int) e->C1;
8720     e->ADDR2.cPos = (int) e->C2;
8721 
8722     e->ADDR1.bPos = (int) e->B1;
8723     e->ADDR2.bPos = (int) e->B2;
8724 
8725     bool F = getbits36_1 (cpu.cu.IWB, 0) != 0;   // fill bit
8726     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;   // T (enablefault) bit
8727 
8728     uint BOLR = getbits36_4 (cpu.cu.IWB, 5);   // T (enablefault) bit
8729     bool B5 = !! (BOLR & 8);
8730     bool B6 = !! (BOLR & 4);
8731     bool B7 = !! (BOLR & 2);
8732     bool B8 = !! (BOLR & 1);
8733 
8734     e->ADDR1.mode = eRWreadBit;
8735     e->ADDR2.mode = eRWreadBit;
8736 
8737 #if !defined(EIS_PTR)
8738     sim_debug (DBG_TRACEEXT, & cpu_dev,
8739                "SZTL N1 %d N2 %d\r\n"
8740                "SZTL C1 %d C2 %d B1 %d B2 %d F %o T %d\r\n"
8741                "SZTL BOLR %u%u%u%u\r\n"
8742                "SZTL op1 SNR %06o WORDNO %06o CHAR %d BITNO %d\r\n"
8743                "SZTL op2 SNR %06o WORDNO %06o CHAR %d BITNO %d\r\n",
8744                e -> N1, e -> N2,
8745                e -> C1, e -> C2, e -> B1, e -> B2, F, T,
8746                B5, B6, B7, B8,
8747                e -> addr [0].SNR, e -> addr [0].address,
8748                e -> addr [0].cPos, e -> addr [0].bPos,
8749                e -> addr [1].SNR, e -> addr [1].address,
8750                e -> addr [1].cPos, e -> addr [1].bPos);
8751 #endif
8752 
8753     bool bR = false; // result bit
8754 
8755     PNL (L68_ (if (max (e->N1, e->N2) < 128)
8756                  DU_CYCLE_FLEN_128;))
8757 
8758     for( ; cpu.du.CHTALLY < min (e->N1, e->N2); cpu.du.CHTALLY += 1)
8759     {
8760         tandd_eis_sample (cpup, cpu.du.CHTALLY, 36);
8761         bool b1 = EISgetBitRWN (cpup, & e->ADDR1, true);
8762         bool b2 = EISgetBitRWN (cpup, & e->ADDR2, true);
8763 
8764         if (b1) if (b2) bR = B8; else bR = B7; else if (b2) bR = B6; else bR = B5;
8765 
8766         if (bR)
8767           {
8768             //CLR_I_ZERO);
8769             cpu.du.Z = 0;
8770             break;
8771           }
8772       }
8773 
8774     if (e->N1 < e->N2)
8775       {
8776         for (; cpu.du.CHTALLY < e->N2; cpu.du.CHTALLY += 1)
8777           {
8778             tandd_eis_sample (cpup, cpu.du.CHTALLY, 36);
8779             bool b1 = F;
8780             bool b2 = EISgetBitRWN (cpup, & e->ADDR2, true);
8781 
8782             if (b1) if (b2) bR = B8; else bR = B7; else if (b2) bR = B6; else bR = B5;
8783 
8784             if (bR)
8785               {
8786                 //CLR_I_ZERO;
8787                 cpu.du.Z = 0;
8788                 break;
8789               }
8790           }
8791       }
8792 
8793     cleanupOperandDescriptor (cpup, 1);
8794     cleanupOperandDescriptor (cpup, 2);
8795 
8796     SC_I_ZERO (cpu.du.Z);
8797     if (e->N1 > e->N2)
8798       {
8799         // NOTES: If N1 > N2, the low order (N1-N2) bits of C(Y-bit1) are not
8800         // processed and the truncation indicator is set ON.
8801         //
8802         // If T = 1 and the truncation indicator is set ON by execution of the
8803         // instruction, then a truncation (overflow) fault occurs.
8804 
8805         SET_I_TRUNC;
8806         if (T && tstOVFfault (cpup))
8807           doFault(FAULT_OFL, fst_zero, "csl truncation fault");
8808       }
8809     else
8810       {
8811         CLR_I_TRUNC;
8812       }
8813   }
8814 
8815 void sztr (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
8816   {
8817     // The execution of this instruction is identical to the Combine
8818     // Bit Strings Left (csl) instruction except that C(BOLR)m is
8819     // not placed into C(Y-bit2)i-1.
8820 
8821     EISstruct * e = & cpu.currentEISinstruction;
8822 
8823     // For i = bits 1, 2, ..., minimum (N1,N2)
8824     //   m = C(Y-bit1)N1-i || C(Y-bit2)N2-i (a 2-bit number)
8825     //   C(BOLR)m -> C( Y-bit2)N2-i
8826     // If N1 < N2, then for i = N1+i, N1+2, ..., N2
8827     //   m = C(F) || C(Y-bit2)N2-i (a 2-bit number)
8828     //    C(BOLR)m -> C( Y-bit2)N2-i
8829     // INDICATORS: (Indicators not listed are not affected)
8830     //     Zero If C(Y-bit2) = 00...0, then ON; otherwise OFF
8831     //     Truncation If N1 > N2, then ON; otherwise OFF
8832     //
8833     // NOTES: If N1 > N2, the low order (N1-N2) bits of C(Y-bit1) are not
8834     // processed and the truncation indicator is set ON.
8835     //
8836     // If T = 1 and the truncation indicator is set ON by execution of the
8837     // instruction, then a truncation (overflow) fault occurs.
8838     //
8839     // BOLR
8840     // If first operand    and    second operand    then result
8841     // bit is:                    bit is:           is from bit:
8842     //        0                          0                      5
8843     //        0                          1                      6
8844     //        1                          0                      7
8845     //        1                          1                      8
8846     //
8847     // The Boolean operations most commonly used are
8848     //                  BOLR Field Bits
8849     // Operation        5      6      7      8
8850     //
8851     // MOVE             0      0      1      1
8852     // AND              0      0      0      1
8853     // OR               0      1      1      1
8854     // NAND             1      1      1      0
8855     // EXCLUSIVE OR     0      1      1      0
8856     // Clear            0      0      0      0
8857     // Invert           1      1      0      0
8858     //
8859 
8860     fault_ipr_subtype_ mod_fault = 0;
8861 
8862 #if !defined(EIS_SETUP)
8863     setupOperandDescriptor(cpup, 1, &mod_fault);
8864     setupOperandDescriptor(cpup, 2, &mod_fault);
8865 #endif
8866 
8867     parseBitstringOperandDescriptor(cpup, 1, &mod_fault);
8868     parseBitstringOperandDescriptor(cpup, 2, &mod_fault);
8869 
8870     L68_ (
8871       // L68 raises it immediately
8872       if (mod_fault)
8873           doFault (FAULT_IPR,
8874                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
8875                    "Illegal modifier");
8876     )
8877 
8878     // Bits 1-4 and 10 MBZ
8879     if (IWB_IRODD & 0360200000000)
8880       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "csr 1-4,10 MBZ");
8881 
8882     DPS8M_ (
8883       // DPS8M raises it delayed
8884       if (mod_fault)
8885           doFault (FAULT_IPR,
8886                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
8887                    "Illegal modifier");
8888     )
8889 
8890     e->ADDR1.cPos = (int) e->C1;
8891     e->ADDR2.cPos = (int) e->C2;
8892 
8893     e->ADDR1.bPos = (int) e->B1;
8894     e->ADDR2.bPos = (int) e->B2;
8895 
8896     // get new char/bit offsets
8897     int numWords1=0, numWords2=0;
8898 
8899     getBitOffsets((int) e->N1, (int) e->C1, (int) e->B1, &numWords1, &e->ADDR1.cPos, &e->ADDR1.bPos);
8900     PNL (cpu.du.D1_PTR_W += (word18) numWords1);
8901     PNL (cpu.du.D1_PTR_W &= AMASK);
8902 #if defined(EIS_PTR)
8903     cpu.du.D1_PTR_W += (word18) numWords1;
8904     cpu.du.D1_PTR_W &= AMASK;
8905 #else
8906     e->ADDR1.address += (word18) numWords1;
8907 #endif
8908 
8909     sim_debug (DBG_TRACEEXT, & cpu_dev,
8910                "CSR N1 %d C1 %d B1 %d numWords1 %d cPos %d bPos %d\r\n",
8911                e->N1, e->C1, e->B1, numWords1, e->ADDR1.cPos, e->ADDR1.bPos);
8912     getBitOffsets((int) e->N2, (int) e->C2, (int) e->B2, &numWords2, &e->ADDR2.cPos, &e->ADDR2.bPos);
8913     sim_debug (DBG_TRACEEXT, & cpu_dev,
8914                "CSR N2 %d C2 %d B2 %d numWords2 %d cPos %d bPos %d\r\n",
8915                e->N2, e->C2, e->B2, numWords2, e->ADDR2.cPos, e->ADDR2.bPos);
8916     PNL (cpu.du.D2_PTR_W += (word18) numWords1);
8917     PNL (cpu.du.D2_PTR_W &= AMASK);
8918 #if defined(EIS_PTR)
8919     cpu.du.D2_PTR_W += (word18) numWords1;
8920     cpu.du.D2_PTR_W &= AMASK;
8921 #else
8922     e->ADDR2.address += (word18) numWords2;
8923 #endif
8924 
8925     bool F = getbits36_1 (cpu.cu.IWB, 0) != 0;   // fill bit
8926     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;   // T (enablefault) bit
8927 
8928     uint BOLR = getbits36_4 (cpu.cu.IWB, 5);   // T (enablefault) bit
8929     bool B5 = !! (BOLR & 8);
8930     bool B6 = !! (BOLR & 4);
8931     bool B7 = !! (BOLR & 2);
8932     bool B8 = !! (BOLR & 1);
8933 
8934     e->ADDR1.mode = eRWreadBit;
8935 
8936     CLR_I_TRUNC;     // assume N1 <= N2
8937 
8938     bool bR = false; // result bit
8939 
8940     PNL (L68_ (if (max (e->N1, e->N2) < 128)
8941                  DU_CYCLE_FLEN_128;))
8942 
8943     for( ; cpu.du.CHTALLY < min(e->N1, e->N2); cpu.du.CHTALLY += 1)
8944       {
8945         tandd_eis_sample (cpup, cpu.du.CHTALLY, 36);
8946         bool b1 = EISgetBitRWNR(cpup, &e->ADDR1, true);
8947 
8948         e->ADDR2.mode = eRWreadBit;
8949         bool b2 = EISgetBitRWNR(cpup, &e->ADDR2, true);
8950 
8951         if (b1) if (b2) bR = B8; else bR = B7; else if (b2) bR = B6; else bR = B5;
8952 
8953         if (bR)
8954           {
8955             cpu.du.Z = 0;
8956             break;
8957           }
8958 
8959       }
8960 
8961     if (e->N1 < e->N2)
8962       {
8963         for(; cpu.du.CHTALLY < e->N2; cpu.du.CHTALLY += 1)
8964           {
8965             tandd_eis_sample (cpup, cpu.du.CHTALLY, 36);
8966             bool b1 = F;
8967 
8968             e->ADDR2.mode = eRWreadBit;
8969             bool b2 = EISgetBitRWNR(cpup, &e->ADDR2, true);
8970 
8971             if (b1) if (b2) bR = B8; else bR = B7; else if (b2) bR = B6; else bR = B5;
8972 
8973             if (bR)
8974             {
8975                 //CLR_I_ZERO;
8976                 cpu.du.Z = 0;
8977                 break;
8978             }
8979 
8980           }
8981       }
8982 
8983     cleanupOperandDescriptor (cpup, 1);
8984     cleanupOperandDescriptor (cpup, 2);
8985 
8986     SC_I_ZERO (cpu.du.Z);
8987     if (e->N1 > e->N2)
8988       {
8989         // NOTES: If N1 > N2, the low order (N1-N2) bits of C(Y-bit1) are not
8990         // processed and the truncation indicator is set ON.
8991         //
8992         // If T = 1 and the truncation indicator is set ON by execution of the
8993         // instruction, then a truncation (overflow) fault occurs.
8994 
8995         SET_I_TRUNC;
8996         if (T && tstOVFfault (cpup))
8997           doFault(FAULT_OFL, fst_zero, "csr truncation fault");
8998       }
8999     else
9000       {
9001         CLR_I_TRUNC;
9002       }
9003   }
9004 
9005 /*
9006  * CMPB - Compare Bit Strings
9007  */
9008 
9009 /*
9010  * get a bit from memory ....
9011  */
9012 // XXX this is terribly inefficient, but it'll do for now ......
9013 
9014 static bool EISgetBit(cpu_state_t * cpup, EISaddr *p, int *cpos, int *bpos)
     /* [previous][next][first][last][top][bottom][index][help] */
9015 {
9016 #if defined(EIS_PTR)
9017     long eisaddr_idx = EISADDR_IDX (p);
9018 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
9019 #endif
9020 
9021     if (!p)
9022     {
9023         //lastAddress = -1;
9024         return 0;
9025     }
9026 
9027     if (*bpos > 8)      // bits 0-8
9028     {
9029         *bpos = 0;
9030         *cpos += 1;
9031         if (*cpos > 3)  // chars 0-3
9032         {
9033             *cpos = 0;
9034 #if defined(EIS_PTR)
9035             cpu.du.Dk_PTR_W[eisaddr_idx] += 1;
9036             cpu.du.Dk_PTR_W[eisaddr_idx] &= AMASK;
9037 #else
9038             p->address += 1;
9039             p->address &= AMASK;
9040 #endif
9041         }
9042     }
9043 
9044     p->data = EISRead(cpup, p); // read data word from memory
9045 
9046     int charPosn = *cpos * 9;
9047     int bitPosn = charPosn + *bpos;
9048     bool b = getbits36_1 (p->data, (uint) bitPosn) != 0;
9049 
9050     *bpos += 1;
9051 
9052     return b;
9053 }
9054 
9055 void cmpb (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
9056 {
9057     EISstruct * e = & cpu.currentEISinstruction;
9058 
9059     // For i = 1, 2, ..., minimum (N1,N2)
9060     //   C(Y-bit1)i-1 :: C(Y-bit2)i-1
9061     // If N1 < N2, then for i = N1+1, N1+2, ..., N2
9062     //   C(FILL) :: C(Y-bit2)i-1
9063     // If N1 > N2, then for i = N2+l, N2+2, ..., N1
9064     //   C(Y-bit1)i-1 :: C(FILL)
9065     //
9066     // Indicators:
9067     //    Zero:  If C(Y-bit1)i = C(Y-bit2)i for all i, then ON; otherwise, OFF
9068     //    Carry: If C(Y-bit1)i < C(Y-bit2)i for any i, then OFF; otherwise ON
9069 
9070     fault_ipr_subtype_ mod_fault = 0;
9071 
9072 #if !defined(EIS_SETUP)
9073     setupOperandDescriptor(cpup, 1, &mod_fault);
9074     setupOperandDescriptor(cpup, 2, &mod_fault);
9075 #endif
9076 
9077     parseBitstringOperandDescriptor(cpup, 1, &mod_fault);
9078     parseBitstringOperandDescriptor(cpup, 2, &mod_fault);
9079 
9080     L68_ (
9081       // L68 raises it immediately
9082       if (mod_fault)
9083         {
9084           doFault (FAULT_IPR,
9085                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
9086                    "Illegal modifier");
9087         }
9088     )
9089 
9090     // Bits 1-8 and 10 MBZ
9091     if (IWB_IRODD & 0377200000000)
9092       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "cmpb 1-8,10 MBZ");
9093 
9094     DPS8M_ (
9095       // DPS8M raises it delayed
9096       if (mod_fault)
9097         {
9098           doFault (FAULT_IPR,
9099                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
9100                    "Illegal modifier");
9101         }
9102     )
9103 
9104     int charPosn1 = (int) e->C1;
9105     int charPosn2 = (int) e->C2;
9106 
9107     int bitPosn1 = (int) e->B1;
9108     int bitPosn2 = (int) e->B2;
9109 
9110     bool F = getbits36_1 (cpu.cu.IWB, 0) != 0;   // fill bit
9111 
9112     SET_I_ZERO;  // assume all =
9113     SET_I_CARRY; // assume all >=
9114 
9115 sim_debug (DBG_TRACEEXT, & cpu_dev, "cmpb N1 %d N2 %d\r\n", e -> N1, e -> N2);
9116 
9117 
9118 
9119 
9120 
9121 
9122 
9123 
9124 
9125 
9126 
9127 
9128 
9129 
9130 
9131 
9132 
9133     PNL (L68_ (if (max (e->N1, e->N2) < 128)
9134       DU_CYCLE_FLEN_128;))
9135 
9136     uint i = 0;
9137     // T&D: resuming after an interrupt (PM842); bits before CHTALLY
9138     // compared equal
9139     if (cpu.tweaks.tandd_mode)
9140       for ( ; i < cpu.du.CHTALLY; i ++)
9141         {
9142           if (i < e->N1)
9143             (void) EISgetBit (cpup, &e->ADDR1, &charPosn1, &bitPosn1);
9144           if (i < e->N2)
9145             (void) EISgetBit (cpup, &e->ADDR2, &charPosn2, &bitPosn2);
9146         }
9147     for( ; i < min(e->N1, e->N2) ; i += 1)
9148     {
9149         tandd_eis_sample (cpup, i, 36);
9150         bool b1 = EISgetBit (cpup, &e->ADDR1, &charPosn1, &bitPosn1);
9151         bool b2 = EISgetBit (cpup, &e->ADDR2, &charPosn2, &bitPosn2);
9152 
9153 sim_debug (DBG_TRACEEXT, & cpu_dev, "cmpb(min(e->N1, e->N2)) i %d b1 %d b2 %d\r\n", i, b1, b2);
9154         if (b1 != b2)
9155         {
9156             CLR_I_ZERO;
9157             if (!b1 && b2)  // 0 < 1
9158                 CLR_I_CARRY;
9159 
9160             cleanupOperandDescriptor (cpup, 1);
9161             cleanupOperandDescriptor (cpup, 2);
9162 
9163             return;
9164         }
9165 
9166     }
9167     if (e->N1 < e->N2)
9168     {
9169         for(; i < e->N2 ; i += 1)
9170         {
9171             tandd_eis_sample (cpup, i, 36);
9172             bool b1 = F;
9173             bool b2 = EISgetBit(cpup, &e->ADDR2, &charPosn2, &bitPosn2);
9174 sim_debug (DBG_TRACEEXT, & cpu_dev, "cmpb(e->N1 < e->N2) i %d b1fill %d b2 %d\r\n", i, b1, b2);
9175 
9176             if (b1 != b2)
9177             {
9178                 CLR_I_ZERO;
9179                 if (!b1 && b2)  // 0 < 1
9180                     CLR_I_CARRY;
9181 
9182                 cleanupOperandDescriptor (cpup, 1);
9183                 cleanupOperandDescriptor (cpup, 2);
9184 
9185                 return;
9186             }
9187         }
9188     } else if (e->N1 > e->N2)
9189     {
9190         for(; i < e->N1 ; i += 1)
9191         {
9192             tandd_eis_sample (cpup, i, 36);
9193             bool b1 = EISgetBit(cpup, &e->ADDR1, &charPosn1, &bitPosn1);
9194             bool b2 = F;
9195 sim_debug (DBG_TRACEEXT, & cpu_dev, "cmpb(e->N1 > e->N2) i %d b1 %d b2fill %d\r\n", i, b1, b2);
9196 
9197             if (b1 != b2)
9198             {
9199                 CLR_I_ZERO;
9200                 if (!b1 && b2)  // 0 < 1
9201                     CLR_I_CARRY;
9202 
9203                 cleanupOperandDescriptor (cpup, 1);
9204                 cleanupOperandDescriptor (cpup, 2);
9205 
9206                 return;
9207             }
9208         }
9209     }
9210     cleanupOperandDescriptor (cpup, 1);
9211     cleanupOperandDescriptor (cpup, 2);
9212 }
9213 
9214 
9215 
9216 
9217 
9218 
9219 
9220 
9221 
9222 
9223 
9224 
9225 
9226 
9227 
9228 
9229 
9230 
9231 
9232 
9233 
9234 
9235 
9236 
9237 
9238 
9239 
9240 
9241 
9242 
9243 
9244 
9245 
9246 
9247 
9248 
9249 
9250 
9251 
9252 
9253 
9254 
9255 
9256 
9257 
9258 
9259 
9260 
9261 
9262 
9263 
9264 
9265 
9266 
9267 
9268 
9269 
9270 
9271 
9272 
9273 
9274 
9275 
9276 
9277 
9278 
9279 
9280 
9281 
9282 
9283 
9284 
9285 
9286 
9287 
9288 
9289 
9290 
9291 
9292 
9293 
9294 
9295 
9296 
9297 
9298 
9299 
9300 
9301 
9302 
9303 
9304 
9305 
9306 
9307 
9308 
9309 
9310 
9311 
9312 
9313 
9314 
9315 
9316 
9317 
9318 
9319 
9320 
9321 
9322 
9323 
9324 
9325 
9326 
9327 
9328 
9329 
9330 
9331 
9332 
9333 
9334 
9335 
9336 
9337 
9338 
9339 
9340 
9341 
9342 
9343 
9344 
9345 
9346 
9347 
9348 
9349 
9350 
9351 
9352 
9353 
9354 
9355 
9356 
9357 
9358 
9359 
9360 
9361 
9362 
9363 
9364 
9365 
9366 
9367 
9368 
9369 
9370 
9371 
9372 
9373 
9374 
9375 
9376 
9377 
9378 
9379 
9380 
9381 
9382 
9383 
9384 
9385 
9386 
9387 
9388 
9389 
9390 
9391 
9392 
9393 
9394 
9395 
9396 
9397 
9398 
9399 
9400 
9401 
9402 
9403 
9404 
9405 
9406 
9407 
9408 
9409 
9410 
9411 
9412 
9413 
9414 
9415 
9416 
9417 
9418 
9419 
9420 
9421 
9422 
9423 
9424 
9425 
9426 
9427 
9428 
9429 
9430 
9431 
9432 
9433 
9434 
9435 /*
9436  * determine sign of N*9-bit length word
9437  */
9438 static bool sign9n(word72 n128, int N)
     /* [previous][next][first][last][top][bottom][index][help] */
9439 {
9440     // sign bit of  9-bit is bit 8  (1 << 8)
9441     // sign bit of 18-bit is bit 17 (1 << 17)
9442     // .
9443     // .
9444     // .
9445     // sign bit of 72-bit is bit 71 (1 << 71)
9446 
9447     if (N < 1 || N > 8) // XXX largest int we'll play with is 72-bits? Makes sense
9448         return false;
9449 
9450 #if defined(NEED_128)
9451     word72 sgnmask = lshift_128 (construct_128 (0, 1), (uint) (N * 9 - 1));
9452     return isnonzero_128 (and_128 (sgnmask, n128));
9453 #else
9454     word72 sgnmask = (word72)1 << ((N * 9) - 1);
9455 
9456     return (bool)(sgnmask & n128);
9457 #endif /* if defined(NEED_128) */
9458 }
9459 
9460 /*
9461  * sign extend a N*9 length word to a (word72) 128-bit word
9462  */
9463 static word72s signExt9(word72 n128, int N)
     /* [previous][next][first][last][top][bottom][index][help] */
9464 {
9465     // ext mask for  9-bit = 037777777777777777777777777777777777777400  8 0's
9466     // ext mask for 18-bit = 037777777777777777777777777777777777400000 17 0's
9467     // ext mask for 36-bit = 037777777777777777777777777777400000000000 35 0's
9468     // etc...
9469 
9470     int bits = (N * 9) - 1;
9471     if (sign9n(n128, N))
9472     {
9473 #if defined(NEED_128)
9474         uint128 extBits = lshift_128 (construct_128 (MASK64, MASK64), (uint) bits);
9475         uint128 or = or_128 (n128, extBits);
9476         return cast_s128 (or);
9477 #else
9478         uint128 extBits = ((uint128)-1 << bits);
9479         return (word72s) (n128 | extBits);
9480 #endif /* if defined(NEED_128) */
9481     }
9482 #if defined(NEED_128)
9483     uint128 zeroBits = complement_128 (lshift_128 (construct_128 (MASK64, MASK64), (uint) bits));
9484     uint128 and = and_128 (n128, zeroBits);
9485     return cast_s128 (and);
9486 #else
9487     uint128 zeroBits = ~((uint128)-1 << bits);
9488     return (word72s) (n128 & zeroBits);
9489 #endif /* if defined(NEED_128) */
9490 }
9491 
9492 /*
9493  * load a 9*n bit integer into e->x ...
9494  */
9495 
9496 static void load9x(cpu_state_t * cpup, int n, EISaddr *addr, int pos)
     /* [previous][next][first][last][top][bottom][index][help] */
9497 {
9498     EISstruct * e = & cpu.currentEISinstruction;
9499 #if defined(NEED_128)
9500     word72 x = construct_128 (0, 0);
9501 #else
9502     word72 x = 0;
9503 #endif /* if defined(NEED_128) */
9504 #if defined(EIS_PTR)
9505     long eisaddr_idx = EISADDR_IDX (addr);
9506 if (eisaddr_idx < 0 || eisaddr_idx > 2) { sim_warn ("IDX1"); return }
9507 #endif
9508 
9509     word36 data = EISRead(cpup, addr);
9510 
9511     int m = n;
9512     while (m)
9513     {
9514 #if defined(NEED_128)
9515         x = lshift_128 (x, 9);
9516 #else
9517         x <<= 9;         // make room for next 9-bit byte
9518 #endif /* if defined(NEED_128) */
9519 
9520         if (pos > 3)        // overflows to next word?
9521         {   // yep....
9522             pos = 0;        // reset to 1st byte
9523 #if EIS_PTR
9524             cpu.du.Dk_PTR_W[eisaddr_idx] = (cpu.du.Dk_PTR_W[eisaddr_idx] + 1) & AMASK;          // bump source to next address
9525 #else
9526             addr->address = (addr->address + 1) & AMASK;          // bump source to next address
9527 #endif
9528             data = EISRead(cpup, addr);    // read it from memory
9529         }
9530 
9531 #if defined(NEED_128)
9532         x = or_128 (x, construct_128 (0, GETBYTE (data, pos)));
9533 #else
9534         x |= GETBYTE(data, pos);   // fetch byte at position pos and 'or' it in
9535 #endif /* if defined(NEED_128) */
9536 
9537         pos += 1;           // onto next position
9538 
9539         m -= 1;             // decrement byte counter
9540     }
9541     e->x = signExt9(x, n);  // form proper 2's-complement integer
9542 }
9543 
9544 
9545 
9546 
9547 
9548 
9549 
9550 
9551 
9552 
9553 
9554 
9555 
9556 
9557 
9558 
9559 
9560 
9561 
9562 
9563 
9564 
9565 
9566 
9567 
9568 
9569 
9570 
9571 
9572 
9573 
9574 
9575 
9576 
9577 
9578 
9579 
9580 
9581 
9582 
9583 
9584 
9585 
9586 
9587 
9588 
9589 
9590 
9591 
9592 
9593 
9594 
9595 
9596 
9597 
9598 
9599 
9600 
9601 
9602 
9603 
9604 
9605 
9606 
9607 
9608 
9609 
9610 
9611 
9612 
9613 
9614 
9615 
9616 
9617 
9618 
9619 
9620 
9621 
9622 
9623 
9624 
9625 
9626 
9627 
9628 
9629 
9630 
9631 
9632 
9633 
9634 
9635 
9636 
9637 
9638 
9639 
9640 
9641 
9642 
9643 
9644 
9645 
9646 
9647 
9648 
9649 
9650 
9651 
9652 
9653 
9654 
9655 
9656 
9657 
9658 
9659 
9660 
9661 
9662 
9663 
9664 void btd (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
9665 {
9666     EISstruct * e = & cpu.currentEISinstruction;
9667 
9668     // C(Y-char91) converted to decimal -> C(Y-charn2)
9669     /*!
9670      * C(Y-char91) contains a twos complement binary integer aligned on 9-bit
9671      * character boundaries with length 0 < N1 <= 8.
9672      *
9673      * If TN2 and S2 specify a 4-bit signed number and P = 1, then if
9674      * C(Y-char91) is positive (bit 0 of C(Y-char91)0 = 0), then the 13(8) plus
9675      * sign character is moved to C(Y-charn2) as appropriate.
9676      *
9677      *   The scaling factor of C(Y-charn2), SF2, must be 0.
9678      *
9679      *   If N2 is not large enough to hold the digits generated by conversion
9680      *   of C(Y-char91) an overflow condition exists; the overflow indicator is
9681      *   set ON and an overflow fault occurs. This implies that an unsigned
9682      *   fixed-point receiving field has a minimum length of 1 character and a
9683      *   signed fixed- point field, 2 characters.
9684      *
9685      * If MFk.RL = 1, then Nk does not contain the operand length; instead; it
9686      * contains a register code for a register holding the operand length.
9687      *
9688      * If MFk.ID = 1, then the kth word following the instruction word does not
9689      * contain an operand descriptor; instead, it contains an indirect pointer
9690      * to the operand descriptor.
9691      *
9692      * C(Y-char91) and C(Y-charn2) may be overlapping strings; no check is made.
9693      *
9694      * Attempted conversion to a floating-point number (S2 = 0) or attempted
9695      * use of a scaling factor (SF2 ≠ 0) causes an illegal procedure fault.
9696      *
9697      * If N1 = 0 or N1 > 8 an illegal procedure fault occurs.
9698      *
9699      * Attempted execution with the xed instruction causes an illegal procedure fault.
9700      *
9701      * Attempted repetition with the rpt, rpd, or rpl instructions causes an illegal procedure fault.
9702      *
9703      */
9704 
9705     // C(string 1) -> C(string 2) (converted)
9706 
9707     // The two's complement binary integer starting at location YC1 is
9708     // converted into a signed string of decimal characters of data type TN2,
9709     // sign and decimal type S2 (S2 = 00 is illegal) and scale factor 0; and is
9710     // stored, right-justified, as a string of length L2 starting at location
9711     // YC2. If the string generated is longer than L2, the high-order excess is
9712     // truncated and the overflow indicator is set. If strings 1 and 2 are not
9713     // overlapped, the contents of string 1 remain unchanged. The length of
9714     // string 1 (L1) is given as the number of 9-bit segments that make up the
9715     // string. L1 is equal to or is less than 8. Thus, the binary string to be
9716     // converted can be 9, 18, 27, 36, 45, 54, 63, or 72 bits long. CN1
9717     // designates a 9-bit character boundary. If P=1, positive signed 4-bit
9718     // results are stored using octal 13 as the plus sign. If P=0, positive
9719     // signed 4-bit results are stored with octal 14 as the plus sign.
9720 
9721     fault_ipr_subtype_ mod_fault = 0;
9722 
9723 #if !defined(EIS_SETUP)
9724     setupOperandDescriptor(cpup, 1, &mod_fault);
9725     setupOperandDescriptor(cpup, 2, &mod_fault);
9726 #endif
9727 
9728     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
9729     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
9730 
9731     L68_ (
9732       // L68 raises it immediately
9733       if (mod_fault)
9734         {
9735           doFault (FAULT_IPR,
9736                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
9737                    "Illegal modifier");
9738         }
9739     )
9740 
9741     // Bits 1-10 MBZ
9742     if (IWB_IRODD & 0377600000000)
9743       {
9744         //sim_printf ("sb2d %012"PRIo64"\r\n", IWB_IRODD);
9745         doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "btd 0-8 MBZ");
9746       }
9747 
9748     // Bits 21-29 of OP1 MBZ
9749     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000077700)
9750       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "btd op1 21-29 MBZ");
9751 
9752     // Bits 24-29 of OP2 MBZ
9753     if (!(e->MF[1] & MFkID) && e -> op [1]  & 0000000007700)
9754       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "btd op2 24-29 MBZ");
9755 
9756     if (e->S[1] == 0)
9757       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "btd op2 S=0");
9758 
9759     DPS8M_ (
9760       // DPS8 raises it delayed
9761       if (mod_fault)
9762         {
9763           doFault (FAULT_IPR,
9764                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
9765                    "Illegal modifier");
9766         }
9767     )
9768 
9769     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character control
9770 
9771     if (e->N1 == 0 || e->N1 > 8)
9772         doFault(FAULT_IPR, fst_ill_proc, "btd(1): N1 == 0 || N1 > 8");
9773 
9774     uint dstTN = e->TN2;    // type of chars in dst
9775     uint dstCN = e->CN2;    // starting at char pos CN
9776 
9777     int n2 = 0;
9778 
9779     switch(e->S2)
9780     {
9781         case CSLS:
9782         case CSTS:
9783             n2 = (int) e->N2 - 1; // 1 sign
9784             break;
9785 
9786         case CSNS:
9787             n2 = (int) e->N2;     // no sign
9788             break;          // no sign wysiwyg
9789     }
9790 
9791     sim_debug (DBG_CAC, & cpu_dev,
9792       "n2 %d\r\n",
9793       n2);
9794 
9795     if (n2 < 1)
9796         doFault (FAULT_IPR, fst_ill_proc, "btd adjusted n2<1");
9797 
9798     decContext set;
9799     decContextDefaultDPS8(&set);
9800     set.traps=0;
9801 
9802     load9x(cpup, (int) e->N1, &e->ADDR1, (int) e->CN1);
9803 
9804     // handle sign
9805     e->sign = 1;
9806 #if defined(NEED_128)
9807     word72 x = cast_128 (e->x);
9808     if (islt_s128 (e->x, construct_s128 (0, 0)))
9809       {
9810         e->sign = -1;
9811         x = and_128 (negate_128 (x), MASK72);
9812 
9813       }
9814 
9815     // convert to decimal string, workaround missing sprintf uint128
9816     char tmp[32];
9817     tmp[31] = 0;
9818     int i;
9819     for (i=30;i>=0;i--) {
9820         //tmp[i] = x%10 + '0';
9821         //x /= 10;
9822         uint16_t r;
9823         x = divide_128_16 (x, 10, & r);
9824         tmp[i] = (char) r + '0';
9825         if (iszero_128 (x))
9826             break;
9827     }
9828 #else
9829     word72 x = (word72)e->x;
9830     if (e->x < 0) {
9831         e->sign = -1;
9832         //x = (-x) & MASK72;
9833         // ubsan
9834         x = ((word72) (- (word72s) x)) & MASK72;
9835     }
9836 
9837     // convert to decimal string, workaround missing sprintf uint128
9838     char tmp[32];
9839     tmp[31] = 0;
9840     int i;
9841     for (i=30;i>=0;i--) {
9842         tmp[i] = x%10 + '0';
9843         x /= 10;
9844         if (x == 0)
9845             break;
9846     }
9847 #endif /* if defined(NEED_128) */
9848 
9849     decNumber _1;
9850     decNumber *op1 = decNumberFromString(&_1, tmp+i, &set);
9851     if (e->sign == -1)
9852         op1->bits |= DECNEG;
9853 
9854     bool Ovr = false, Trunc = false;
9855 
9856     uint8_t out [256];
9857     char * res = formatDecimal (out, &set, op1, n2, (int) e->S2, e->SF2, 0, &Ovr, &Trunc);
9858 
9859     // now write to memory in proper format.....
9860 
9861     //word18 dstAddr = e->dstAddr;
9862     int pos = (int) dstCN;
9863 
9864     // 1st, take care of any leading sign .......
9865     switch(e->S2)
9866     {
9867         case CSLS:  // fixed-point, leading sign
9868             switch(dstTN)
9869             {
9870                 case CTN4:
9871                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
9872                               //   then the 13(8) plus sign character is placed appropriately
9873                               //   if the result of the operation is positive.
9874                         EISwrite49(cpup, &e->ADDR2, &pos, (int) dstTN,
9875                                    (decNumberIsNegative(op1) && !decNumberIsZero(op1)) ? 015 : 013);  // special +
9876                     else
9877                         EISwrite49(cpup, &e->ADDR2, &pos, (int) dstTN,
9878                                    (decNumberIsNegative(op1) && !decNumberIsZero(op1)) ? 015 : 014);  // default +
9879                     break;
9880                 case CTN9:
9881                     EISwrite49(cpup, &e->ADDR2, &pos, (int) dstTN,
9882                                (decNumberIsNegative(op1) && !decNumberIsZero(op1)) ? '-' : '+');
9883                     break;
9884             }
9885             break;
9886 
9887         case CSTS:  // nuttin' to do here .....
9888         case CSNS:
9889             break;  // no sign wysiwyg
9890     }
9891 
9892     // 2nd, write the digits .....
9893     for(int i = 0 ; i < n2 ; i++)
9894         switch(dstTN)
9895         {
9896             case CTN4:
9897                 EISwrite49(cpup, &e->ADDR2, &pos, (int) dstTN, (word9) (res[i] - '0'));
9898                 break;
9899             case CTN9:
9900                 EISwrite49(cpup, &e->ADDR2, &pos, (int) dstTN, (word9) res[i]);
9901                 break;
9902         }
9903 
9904     // 3rd, take care of any trailing sign or exponent ...
9905     switch(e->S2)
9906     {
9907         case CSTS:  // write trailing sign ....
9908             switch(dstTN)
9909             {
9910                 case CTN4:
9911                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
9912                               //   then the 13(8) plus sign character is placed appropriately
9913                               //   if the result of the operation is positive.
9914                         EISwrite49(cpup, &e->ADDR2, &pos, (int) dstTN,
9915                                    (decNumberIsNegative(op1) && !decNumberIsZero(op1)) ? 015 :  013);  // special +
9916                     else
9917                         EISwrite49(cpup, &e->ADDR2, &pos, (int) dstTN,
9918                                    (decNumberIsNegative(op1) && !decNumberIsZero(op1)) ? 015 :  014);  // default +
9919                     break;
9920 
9921                 case CTN9:
9922                     EISwrite49(cpup, &e->ADDR2, &pos, (int) dstTN,
9923                                (decNumberIsNegative(op1) && !decNumberIsZero(op1)) ? '-' : '+');
9924                     break;
9925             }
9926             break;
9927 
9928         case CSLS:  // fixed point, leading sign - already done
9929         case CSNS:  // fixed point, unsigned - nuttin' needed to do
9930             break;
9931     }
9932 
9933     SC_I_NEG (decNumberIsNegative(op1) && !decNumberIsZero(op1));  // set negative indicator if op3 < 0
9934     SC_I_ZERO (decNumberIsZero(op1));     // set zero indicator if op3 == 0
9935 
9936     cleanupOperandDescriptor (cpup, 1);
9937     cleanupOperandDescriptor (cpup, 2);
9938 
9939     if (Ovr)
9940     {
9941         SET_I_OFLOW;
9942         if (tstOVFfault (cpup))
9943           doFault(FAULT_OFL, fst_zero, "btd overflow fault");
9944     }
9945 }
9946 
9947 
9948 
9949 
9950 
9951 
9952 
9953 
9954 
9955 
9956 
9957 
9958 
9959 
9960 
9961 
9962 
9963 
9964 
9965 
9966 
9967 
9968 
9969 
9970 
9971 
9972 
9973 
9974 
9975 
9976 
9977 
9978 
9979 
9980 
9981 
9982 
9983 
9984 
9985 
9986 
9987 
9988 
9989 
9990 
9991 
9992 
9993 
9994 
9995 
9996 
9997 
9998 
9999 
10000 
10001 
10002 
10003 
10004 
10005 
10006 
10007 
10008 
10009 
10010 
10011 
10012 
10013 
10014 
10015 
10016 
10017 
10018 
10019 
10020 
10021 
10022 
10023 
10024 
10025 
10026 
10027 
10028 
10029 
10030 
10031 
10032 
10033 
10034 
10035 
10036 
10037 
10038 
10039 
10040 
10041 
10042 
10043 
10044 
10045 
10046 
10047 
10048 
10049 
10050 
10051 
10052 
10053 
10054 
10055 
10056 
10057 
10058 
10059 
10060 
10061 
10062 
10063 
10064 
10065 
10066 
10067 
10068 
10069 
10070 
10071 
10072 
10073 
10074 
10075 
10076 
10077 
10078 
10079 
10080 
10081 
10082 
10083 
10084 
10085 
10086 
10087 
10088 
10089 
10090 
10091 
10092 
10093 
10094 
10095 
10096 
10097 
10098 
10099 
10100 
10101 
10102 
10103 
10104 
10105 
10106 
10107 
10108 
10109 
10110 
10111 
10112 
10113 
10114 
10115 
10116 
10117 
10118 
10119 
10120 
10121 
10122 
10123 
10124 
10125 
10126 
10127 
10128 
10129 
10130 
10131 
10132 
10133 
10134 
10135 
10136 
10137 
10138 
10139 
10140 
10141 
10142 
10143 
10144 
10145 
10146 
10147 
10148 
10149 
10150 
10151 
10152 
10153 void dtb (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
10154 {
10155     EISstruct * e = & cpu.currentEISinstruction;
10156 
10157     fault_ipr_subtype_ mod_fault = 0;
10158 
10159 #if !defined(EIS_SETUP)
10160     setupOperandDescriptor(cpup, 1, &mod_fault);
10161     setupOperandDescriptor(cpup, 2, &mod_fault);
10162 #endif
10163 
10164     PNL (L68_ (DU_CYCLE_DGDB;))
10165 
10166     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
10167     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
10168 
10169     L68_ (
10170       // L68 raises it immediately
10171       if (mod_fault)
10172         {
10173           doFault (FAULT_IPR,
10174                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
10175                    "Illegal modifier");
10176         }
10177     )
10178 
10179     // Bits 0 to 10 of the instruction Must Be Zero. So Say We ISOLTS.
10180     uint mbz = (uint) getbits36 (IWB_IRODD, 0, 11);
10181     if (mbz)
10182       {
10183         doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "dtb(): 0-10 MBZ");
10184       }
10185 
10186     // Bits 24-29 of OP1 MBZ
10187     if (!(e->MF[0] & MFkID) && e -> op [0]  & 0000000007700)
10188       {
10189         doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "dtb op1 24-29 MBZ");
10190       }
10191 
10192     // Bits 21-29 of OP2 MBZ
10193     if (!(e->MF[1] & MFkID) && e -> op [1]  & 0000000077700)
10194       {
10195         doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "dtb op2 21-29 MBZ");
10196        }
10197 
10198     // Attempted conversion of a floating-point number (S1 = 0) or attempted
10199     // use of a scaling factor (SF1 ≠ 0) causes an illegal procedure fault.
10200     if (e->S1 == 0 || e->SF1 != 0)
10201     {
10202         doFault(FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_PROC|mod_fault}, "dtb():  S1=0 or SF1!=0");
10203     }
10204 
10205     DPS8M_ (
10206       // DPS8M raises it delayed
10207       if (mod_fault)
10208         {
10209           doFault (FAULT_IPR,
10210                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
10211                    "Illegal modifier");
10212         }
10213     )
10214 
10215     // If N2 = 0 or N2 > 8 an illegal procedure fault occurs.
10216     if (e->N2 == 0 || e->N2 > 8)
10217     {
10218         doFault(FAULT_IPR, fst_ill_proc, "dtb():  N2 = 0 or N2 > 8 etc.");
10219     }
10220 
10221     int n1 = 0;
10222 
10223     /*
10224      * Here we need to distinguish between 4 type of numbers.
10225      *
10226      * CSFL - Floating-point, leading sign
10227      * CSLS - Scaled fixed-point, leading sign
10228      * CSTS - Scaled fixed-point, trailing sign
10229      * CSNS - Scaled fixed-point, unsigned
10230      */
10231 
10232     // determine precision
10233     switch(e->S1)
10234     {
10235         case CSLS:
10236         case CSTS:
10237             n1 = (int) e->N1 - 1; // only 1 sign
10238             break;
10239 
10240         case CSNS:
10241             n1 = (int) e->N1;     // no sign
10242             break;  // no sign wysiwyg
10243     }
10244     // RJ78: An Illegal Procedure fault occurs if:
10245     // N1 is not large enough to specify the number of characters required for the
10246     // specified sign and/or exponent, plus at least one digit.
10247 
10248     if (n1 < 1)
10249         doFault (FAULT_IPR, fst_ill_proc, "dtb adjusted n1<1");
10250 
10251     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
10252 
10253     // prepare output mask
10254 #if defined(NEED_128)
10255     word72 msk = subtract_128 (lshift_128 (construct_128 (0, 1), (9*e->N2-1)),construct_128 (0, 1));
10256 #else
10257     word72 msk = ((word72)1<<(9*e->N2-1))-1; // excluding sign
10258 #endif /* if defined(NEED_128) */
10259 
10260 
10261 
10262 
10263 
10264 
10265 
10266 
10267 
10268 
10269 
10270 
10271 
10272 
10273     // input is unscaled fixed point, so just get the digits
10274     bool Ovr = false;
10275 
10276     // Only the low-order 32 digits are converted (ISOLTS PS905: DTB of 36
10277     // digits into 6 characters; the hardware result is the value of the
10278     // last 32 digits, mod 2**54).  A nonzero digit above them is still an
10279     // overflow (10**32 > 2**71, the largest result).
10280     int first = 0;
10281     if (n1 > 32)
10282       {
10283         first = n1 - 32;
10284         for (int i = 0; i < first; i ++)
10285           if (e->inBuffer[i])
10286             Ovr = true;
10287       }
10288 #if defined(NEED_128)
10289     word72 x = construct_128 (0, 0);
10290     for (int i = first; i < n1; i++) {
10291         //x *= 10;
10292         x = multiply_128 (x, construct_128 (0, 10));
10293         //x += e->inBuffer[i];
10294         x = add_128 (x, construct_128 (0, (uint) e->inBuffer[i]));
10295         //Ovr |= x>msk?1:0;
10296         Ovr |= isgt_128 (x, msk) ? 1 : 0;
10297         //x &= msk; // multiplication and addition mod msk+1
10298         x = and_128 (x, msk); // multiplication and addition mod msk+1
10299     }
10300     if (e->sign == -1)
10301         //x = -x; // no need to mask it
10302         x = negate_128 (x); // no need to mask it
10303 
10304 #else
10305     word72 x = 0;
10306     for (int i = first; i < n1; i++) {
10307         x *= 10;
10308         x += e->inBuffer[i];
10309         //sim_printf("%d %012"PRIo64" %012"PRIo64"\r\n",e->inBuffer[i],(word36)((x >> 36) & DMASK), (word36)(x & DMASK));
10310         Ovr |= x>msk?1:0;
10311         x &= msk; // multiplication and addition mod msk+1
10312     }
10313     if (e->sign == -1)
10314         //x = -x; // no need to mask it
10315         // ubsan
10316         x = (word72) (- (word72s) x); // no need to mask it
10317 
10318     //sim_printf ("dtb out %012"PRIo64" %012"PRIo64"\r\n", (word36)((x >> 36) & DMASK), (word36)(x & DMASK));
10319 #endif /* if defined(NEED_128) */
10320     int pos = (int)e->CN2;
10321 
10322     // now write to memory in proper format.....
10323 
10324     int shift = 9*((int)e->N2-1);
10325     for(int i = 0; i < (int)e->N2; i++) {
10326 #if defined(NEED_128)
10327         EISwrite9(cpup, &e->ADDR2, &pos, (word9) rshift_128 (x, (uint) shift).l & 0777);
10328 #else
10329         EISwrite9(cpup, &e->ADDR2, &pos, (word9) (x >> shift )& 0777);
10330 #endif /* if defined(NEED_128) */
10331         shift -= 9;
10332     }
10333 
10334     SC_I_NEG (e->sign == -1);  // set negative indicator
10335 #if defined(NEED_128)
10336     SC_I_ZERO (iszero_128 (x)); // set zero indicator
10337 #else
10338     SC_I_ZERO (x==0);     // set zero indicator
10339 #endif /* if defined(NEED_128) */
10340 
10341     cleanupOperandDescriptor (cpup, 1);
10342     cleanupOperandDescriptor (cpup, 2);
10343 
10344     if (Ovr)
10345     {
10346         SET_I_OFLOW;
10347         if (tstOVFfault (cpup))
10348           doFault(FAULT_OFL, fst_zero, "dtb overflow fault");
10349     }
10350 }
10351 
10352 /*
10353  * decimal EIS instructions ...
10354  */
10355 
10356 #define ASC(x)  ((x) + '0')
10357 
10358 // Internal register overflow of fixed-point add/subtract (RJ78; ISOLTS 846
10359 // 07c, 10a, 11b): a result spanning more than 63 digits after operand
10360 // alignment sets overflow (PS846 10a: a 64-digit sum overflows), but the
10361 // register keeps 64 digits, losing only the most significant digits beyond
10362 // them (ISOLTS PS905: ad2d of a 65-digit aligned sum dropped 1 leading
10363 // digit, ad3d of a 68-digit one dropped 4).
10364 #define FIXED_OVERFLOW_DIGITS 63
10365 #define FIXED_ALIGN_DIGITS 64
10366 
10367 // Fixed-point add/subtract keeps only the 64 digit positions above the
10368 // lowest operand digit (see above).  A floating operand with a large
10369 // exponent can span more than the 126 digits decNumber computes exactly,
10370 // which rounded away the low-order digits the hardware keeps (ISOLTS PS905
10371 // ad3d: E+162 + E+57, E+142 + E+14, E+176 + E+30 into fixed-point results).
10372 // So each operand's digits at positions >= limit are discarded first (for
10373 // every span, see eisWindowOperands); returns true if nonzero digits were
10374 // discarded (overflow).
10375 static bool eisWindow (decNumber * x, int32_t limit)
     /* [previous][next][first][last][top][bottom][index][help] */
10376   {
10377     if (decNumberIsZero (x) || x->exponent + x->digits <= limit)
10378       return false;
10379     uint8_t bcd[DECNUMDIGITS + 1];
10380     decNumberGetBCD (x, bcd);
10381     int drop = x->digits - (limit - x->exponent);
10382     if (drop > x->digits)
10383       drop = x->digits;
10384     bool lost = false;
10385     for (int i = 0; i < drop; i ++)
10386       if (bcd[i])
10387         lost = true;
10388     int j = drop;
10389     while (j < x->digits && bcd[j] == 0)
10390       j ++;
10391     bool neg = decNumberIsNegative (x);
10392     int32_t ex = x->exponent;
10393     if (j == x->digits)
10394       decNumberZero (x);
10395     else
10396       {
10397         // decNumberSetBCD finds the most significant unit from the
10398         // current digit count, so set the new count first
10399         uint32_t n = (uint32_t) (x->digits - j);
10400         x->digits = (int32_t) n;
10401         decNumberSetBCD (x, bcd + j, n);
10402       }
10403     x->exponent = ex;
10404     if (neg)
10405       x->bits |= DECNEG;
10406     return lost;
10407   }
10408 
10409 // Apply eisWindow to both operands.  Not only when decNumber couldn't
10410 // compute the aligned span exactly: the hardware discards the operand
10411 // digits outside the 64 positions before the arithmetic (ISOLTS PS905
10412 // sb2d: op2 28 digits at 10^0 minus op1 E+66 gave op2 unchanged with
10413 // overflow, not op2 - op1's low digits, negative; sb3d likewise gave +0).
10414 // A zero operand doesn't place the window (T&D PS846 07C: a 62-digit op1
10415 // at 10^4 plus a zero op2 at 10^1 into a fixed result has no overflow).
10416 static bool eisWindowOperands (decNumber * a, decNumber * b)
     /* [previous][next][first][last][top][bottom][index][help] */
10417   {
10418     bool za = decNumberIsZero (a), zb = decNumberIsZero (b);
10419     if (za && zb)
10420       return false;
10421     int32_t lo = za ? b->exponent : zb ? a->exponent : min (a->exponent, b->exponent);
10422     bool la = eisWindow (a, lo + FIXED_ALIGN_DIGITS);
10423     bool lb = eisWindow (b, lo + FIXED_ALIGN_DIGITS);
10424     return la || lb;
10425   }
10426 
10427 // Floating-result add/subtract: the hardware aligns the operands in a
10428 // register of 63 digit positions counted down from the most significant
10429 // operand digit; operand digits below it are discarded before the
10430 // arithmetic (ISOLTS PS905 sb2d: a tiny positive op2 minus a 17-digit op1
10431 // gave exactly -op1, where subtracting it first and truncating gave
10432 // ...38999...9).  The truncation indicator is eisFloatAlignTrunc's.
10433 #define FLOAT_ALIGN_DIGITS 63
10434 
10435 static void eisWindowLow (decNumber * x, int32_t low)
     /* [previous][next][first][last][top][bottom][index][help] */
10436   {
10437     if (decNumberIsZero (x) || x->exponent >= low)
10438       return;
10439     bool neg = decNumberIsNegative (x);
10440     int drop = low - x->exponent;
10441     if (drop >= x->digits)
10442       {
10443         decNumberZero (x);
10444         x->exponent = low;
10445         return;
10446       }
10447     uint8_t bcd[DECNUMDIGITS + 1];
10448     decNumberGetBCD (x, bcd);
10449     // (decNumberSetBCD uses the current digit count: set the new one first)
10450     x->digits -= drop;
10451     decNumberSetBCD (x, bcd, (uint32_t) x->digits);
10452     x->exponent = low;
10453     if (neg)
10454       x->bits |= DECNEG;
10455   }
10456 
10457 // The truncation indicator of a floating-result add/subtract, as PS905's
10458 // model of the instruction computes it (pas.905 L31003, L33250, L32121,
10459 // L32303): with each operand's top position = exponent + digits (trailing
10460 // zeros included), truncation is set when
10461 // the distance between the two tops, plus 1 for the carry, plus the digit
10462 // count of the operand with the lower top (op1 on a tie) reaches 65,
10463 // i.e. that operand doesn't fit in the 64-digit register.  It doesn't
10464 // matter whether the digits that don't fit are zero, and it doesn't
10465 // depend on R.  With R = 1 nothing else sets it (ISOLTS PS905: ad2d and
10466 // sb2d cases with R = 1 set it only in this situation).  A zero operand
10467 // never sets it: PS905's model gives a zero operand top 127 (L32303),
10468 // which would, but the T&D tape's PS810 test 03M ("AD3D floating zero
10469 // test operand one") expects no truncation.
10470 static bool eisFloatAlignTrunc (decNumber * a, decNumber * b)
     /* [previous][next][first][last][top][bottom][index][help] */
10471   {
10472     if (decNumberIsZero (a) || decNumberIsZero (b))
10473       return false;
10474     int32_t ta = a->exponent + a->digits;
10475     int32_t tb = b->exponent + b->digits;
10476     decNumber * lo = tb >= ta ? a : b;
10477     int32_t shift = tb >= ta ? tb - ta : ta - tb;
10478     return shift + 1 + lo->digits >= 65;
10479   }
10480 
10481 static void eisFloatWindowOperands (decNumber * a, decNumber * b)
     /* [previous][next][first][last][top][bottom][index][help] */
10482   {
10483     int32_t hi;
10484     if (decNumberIsZero (a) && decNumberIsZero (b))
10485       return;
10486     if (decNumberIsZero (a))
10487       hi = b->exponent + b->digits;
10488     else if (decNumberIsZero (b))
10489       hi = a->exponent + a->digits;
10490     else
10491       hi = max (a->exponent + a->digits, b->exponent + b->digits);
10492     eisWindowLow (a, hi - FLOAT_ALIGN_DIGITS);
10493     eisWindowLow (b, hi - FLOAT_ALIGN_DIGITS);
10494   }
10495 
10496 /*
10497  * ad2d - Add Using Two Decimal Operands
10498  */
10499 
10500 void ad2d (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
10501 {
10502     EISstruct * e = & cpu.currentEISinstruction;
10503 
10504     fault_ipr_subtype_ mod_fault = 0;
10505 
10506 #if !defined(EIS_SETUP)
10507     setupOperandDescriptor(cpup, 1, &mod_fault);
10508     setupOperandDescriptor(cpup, 2, &mod_fault);
10509     setupOperandDescriptorCache(cpup,3);
10510 #endif
10511 
10512     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
10513     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
10514 
10515     L68_ (
10516       // L68 raises it immediately
10517       if (mod_fault)
10518         {
10519           doFault (FAULT_IPR,
10520                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
10521                    "Illegal modifier");
10522         }
10523     )
10524 
10525     // Bits 1-8 MBZ
10526     if (IWB_IRODD & 0377000000000)
10527       doFault (FAULT_IPR, fst_ill_op, "ad2d 1-8 MBZ");
10528 
10529     DPS8M_ (
10530       // DPS8M raises it delayed
10531       if (mod_fault)
10532         {
10533           doFault (FAULT_IPR,
10534                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
10535                    "Illegal modifier");
10536         }
10537     )
10538 
10539     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character control
10540     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;  // truncation bit
10541     bool R = getbits36_1 (cpu.cu.IWB, 10) != 0;  // rounding bit
10542 
10543     PNL (L68_ (if (R)
10544       DU_CYCLE_FRND;))
10545 
10546     uint srcTN = e->TN1;    // type of chars in src
10547 
10548     uint dstTN = e->TN2;    // type of chars in dst
10549     uint dstCN = e->CN2;    // starting at char pos CN
10550 
10551     e->ADDR3 = e->ADDR2;
10552 
10553     int n1 = 0, n2 = 0, sc1 = 0, sc2 = 0;
10554 
10555     /*
10556      * Here we need to distinguish between 4 type of numbers.
10557      *
10558      * CSFL - Floating-point, leading sign
10559      * CSLS - Scaled fixed-point, leading sign
10560      * CSTS - Scaled fixed-point, trailing sign
10561      * CSNS - Scaled fixed-point, unsigned
10562      */
10563 
10564     // determine precision
10565     switch(e->S1)
10566     {
10567         case CSFL:
10568             n1 = (int) e->N1 - 1; // need to account for the - sign
10569             if (srcTN == CTN4)
10570                 n1 -= 2;    // 2 4-bit digits exponent
10571             else
10572                 n1 -= 1;    // 1 9-bit digit exponent
10573             sc1 = 0;        // no scaling factor
10574             break;
10575 
10576         case CSLS:
10577         case CSTS:
10578             n1 = (int) e->N1 - 1; // only 1 sign
10579             sc1 = -e->SF1;
10580             break;
10581 
10582         case CSNS:
10583             n1 = (int) e->N1;     // no sign
10584             sc1 = -e->SF1;
10585             break;  // no sign wysiwyg
10586     }
10587 
10588     if (n1 < 1)
10589         doFault (FAULT_IPR, fst_ill_proc, "ad2d adjusted n1<1");
10590 
10591     switch(e->S2)
10592     {
10593         case CSFL:
10594             n2 = (int) e->N2 - 1; // need to account for the sign
10595             if (e->TN2 == CTN4)
10596                 n2 -= 2;    // 2 4-bit digit exponent
10597             else
10598                 n2 -= 1;    // 1 9-bit digit exponent
10599             sc2 = 0;        // no scaling factor
10600             break;
10601 
10602         case CSLS:
10603         case CSTS:
10604             n2 = (int) e->N2 - 1; // 1 sign
10605             sc2 = -e->SF2;
10606             break;
10607 
10608         case CSNS:
10609             n2 = (int) e->N2;     // no sign
10610             sc2 = -e->SF2;
10611             break;  // no sign wysiwyg
10612     }
10613 
10614     if (n2 < 1)
10615         doFault (FAULT_IPR, fst_ill_proc, "ad2d adjusted n2<1");
10616 
10617     decContext set;
10618     //decContextDefault(&set, DEC_INIT_BASE);         // initialize
10619     decContextDefaultDPS8(&set);
10620 
10621     set.traps=0;
10622 
10623     decNumber _1, _2, _3;
10624 
10625     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
10626 
10627     decNumber *op1 = decBCD9ToNumber(e->inBuffer, n1, sc1, &_1);
10628     if (e->sign == -1)
10629         op1->bits |= DECNEG;
10630     if (e->S1 == CSFL)
10631         op1->exponent = e->exponent;
10632 
10633     EISloadInputBufferNumeric (cpup, 2);   // according to MF2
10634 
10635     decNumber *op2 = decBCD9ToNumber(e->inBuffer, n2, sc2, &_2);
10636     if (e->sign == -1)
10637         op2->bits |= DECNEG;
10638     if (e->S2 == CSFL)
10639         op2->exponent = e->exponent;
10640 
10641     // The aligned result can be longer than the context's 65 digits (e.g. a
10642     // 42-digit operand at SF 16 and a 56-digit one at SF -25 span 83
10643     // digits). Rounding it to 65 digits here lost low-order digits that the
10644     // hardware keeps (ISOLTS PS905 sb2d); compute it exactly (126 digits
10645     // covers any fixed-point alignment) and let the rules below drop
10646     // digits. Only for a fixed-point result: with a floating result
10647     // (PS905 sb2d, 9-bit floating op2 with exponent -128 minus a 1-digit
10648     // 4-bit op1) the hardware matches the 65-digit context, and the exact
10649     // value differs from it.
10650     decNumber *op3;
10651     bool winLost = false;
10652     bool fltAlignTrunc = false;
10653     if (e->S2 != CSFL)
10654     {
10655       enum rounding safeR = decContextGetRounding(&set);
10656       int safe = set.digits;
10657       set.digits = DECNUMDIGITS;
10658       decContextSetRounding(&set, DEC_ROUND_DOWN);
10659       winLost = eisWindowOperands (op1, op2);
10660       op3 = decNumberAdd(&_3, op1, op2, &set);
10661       set.digits = safe;
10662       decContextSetRounding(&set, safeR);
10663     }
10664     else
10665     {
10666       fltAlignTrunc = eisFloatAlignTrunc (op1, op2);
10667       eisFloatWindowOperands (op1, op2);
10668       op3 = decNumberAdd(&_3, op1, op2, &set);
10669     }
10670 
10671     // ISOLTS 846 07c, 10a, 11b internal register overflow - see ad3d
10672     bool iOvr = winLost;
10673     if (op3->digits > 63 && e->S2 == CSFL) {
10674         // Floating-point result: the hardware keeps the most significant
10675         // digits and doesn't overflow; the result is truncated (ISOLTS PS905,
10676         // ad3d with a floating result: s/b kept the leading digits, no
10677         // overflow indicator). formatDecimal then fits it to the result's
10678         // digits and sets truncation.
10679         enum rounding safeR = decContextGetRounding(&set);
10680         int safe = set.digits;
10681         decContextSetRounding(&set, DEC_ROUND_DOWN);
10682         set.digits = 63;
10683         decNumberPlus(op3, op3, &set);
10684         set.digits = safe;
10685         decContextSetRounding(&set, safeR);
10686     } else if (op3->digits > FIXED_OVERFLOW_DIGITS) {
10687         uint8_t pr[256];
10688         // if sf<=0, trailing zeroes can't be shifted out
10689         // if sf> 0, (some of) trailing zeroes can be shifted out
10690         int sf = e->S2==CSFL?op3->exponent:e->SF2;
10691 
10692         int ctz = 0;
10693         if (sf>0) {     // optimize: we don't care when sf>0
10694             decNumberGetBCD(op3,pr);
10695             for (int i=op3->digits-1;i>=0 && pr[i]==0;i--)
10696                  ctz ++;
10697         }
10698 
10699         int span = op3->digits - min(max(sf,0),ctz);
10700         if (span > FIXED_OVERFLOW_DIGITS)
10701             iOvr = 1;
10702         if (span > FIXED_ALIGN_DIGITS) {
10703             enum rounding safeR = decContextGetRounding(&set);         // save rounding mode
10704             int safe = set.digits;
10705             decNumber tmp;
10706 
10707             // discard MS digits
10708             decContextSetRounding(&set, DEC_ROUND_DOWN);     // Round towards 0 (truncation).
10709             set.digits = span - FIXED_ALIGN_DIGITS;
10710             decNumberPlus(&tmp, op3, &set);
10711             set.digits = safe;
10712 
10713             decNumberSubtract(op3, op3, &tmp, &set);
10714 
10715             //decNumberToString(op3,(char*)pr); sim_printf("discarded: %s\r\n",pr);
10716 
10717             decContextSetRounding(&set, safeR);
10718         }
10719     }
10720 
10721     // A floating-point result's truncation: see eisFloatAlignTrunc.
10722     bool Ovr = false, EOvr = false, Trunc = false;
10723 
10724     uint8_t out [256];
10725     char *res = formatDecimal(out, &set, op3, n2, (int) e->S2, e->SF2, R, &Ovr, &Trunc);
10726     if (fltAlignTrunc)
10727         Trunc = true;
10728 
10729     Ovr |= iOvr;
10730 
10731     if (decNumberIsZero(op3))
10732         op3->exponent = 127;
10733 
10734     //(void)printf("%s\r\n", res);
10735 
10736     // now write to memory in proper format.....
10737 
10738     //word18 dstAddr = e->dstAddr;
10739     int pos = (int) dstCN;
10740 
10741     // 1st, take care of any leading sign .......
10742     switch(e->S2)
10743     {
10744         case CSFL:  // floating-point, leading sign.
10745         case CSLS:  // fixed-point, leading sign
10746             switch(dstTN)
10747             {
10748                 case CTN4:
10749                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
10750                               //   then the 13(8) plus sign character is placed appropriately
10751                               //   if the result of the operation is positive.
10752                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
10753                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
10754                     else
10755                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
10756                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
10757                     break;
10758                 case CTN9:
10759                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
10760                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
10761                     break;
10762             }
10763             break;
10764 
10765         case CSTS:  // nuttin' to do here .....
10766         case CSNS:
10767             break;  // no sign wysiwyg
10768     }
10769 
10770     // 2nd, write the digits .....
10771     for(int j = 0 ; j < n2 ; j++)
10772         switch(dstTN)
10773         {
10774             case CTN4:
10775                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) (res[j] - '0'));
10776                 break;
10777             case CTN9:
10778                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) res[j]);
10779                 break;
10780         }
10781 
10782     // 3rd, take care of any trailing sign or exponent ...
10783     switch(e->S2)
10784     {
10785         case CSTS:  // write trailing sign ....
10786             switch(dstTN)
10787             {
10788                 case CTN4:
10789                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
10790                               //   then the 13(8) plus sign character is placed appropriately
10791                               //   if the result of the operation is positive.
10792                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
10793                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
10794                     else
10795                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
10796                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
10797                     break;
10798                 case CTN9:
10799                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
10800                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
10801                     break;
10802             }
10803             break;
10804 
10805         case CSFL:  // floating-point, leading sign.
10806             // write the exponent
10807             switch(dstTN)
10808             {
10809                 case CTN4:
10810                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (op3->exponent >> 4) & 0xf); // upper 4-bits
10811                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,  op3->exponent       & 0xf); // lower 4-bits
10812                     break;
10813                 case CTN9:
10814                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, op3->exponent & 0xff);    // write 8-bit exponent
10815                     break;
10816             }
10817             break;
10818 
10819         case CSLS:  // fixed point, leading sign - already done
10820         case CSNS:  // fixed point, unsigned - nuttin' needed to do
10821             break;
10822     }
10823 
10824     // set flags, etc ...
10825     if (e->S2 == CSFL)
10826     {
10827         if (op3->exponent > 127)
10828         {
10829             SET_I_EOFL;
10830             EOvr = true;
10831         }
10832         if (op3->exponent < -128)
10833         {
10834             SET_I_EUFL;
10835             EOvr = true;
10836         }
10837     }
10838 
10839     SC_I_NEG (decNumberIsNegative(op3) && !decNumberIsZero(op3));  // set negative indicator if op3 < 0
10840     SC_I_ZERO (decNumberIsZero(op3));     // set zero indicator if op3 == 0
10841 
10842     SC_I_TRUNC (Trunc); // formatDecimal sets it only without rounding, except for a floating result dropping digits (ISOLTS PS905)
10843 
10844     cleanupOperandDescriptor (cpup, 1);
10845     cleanupOperandDescriptor (cpup, 2);
10846     cleanupOperandDescriptor (cpup, 3);
10847 
10848     if (TST_I_TRUNC && T && tstOVFfault (cpup))
10849       doFault(FAULT_OFL, fst_zero, "ad2d truncation(overflow) fault");
10850     if (EOvr && tstOVFfault (cpup))
10851         doFault(FAULT_OFL, fst_zero, "ad2d over/underflow fault");
10852     if (Ovr)
10853     {
10854         SET_I_OFLOW;
10855         if (tstOVFfault (cpup))
10856           doFault(FAULT_OFL, fst_zero, "ad2d overflow fault");
10857     }
10858 }
10859 
10860 
10861 
10862 
10863 
10864 
10865 
10866 
10867 
10868 
10869 
10870 
10871 
10872 
10873 
10874 
10875 
10876 
10877 
10878 
10879 
10880 
10881 
10882 
10883 
10884 
10885 
10886 
10887 
10888 
10889 
10890 
10891 
10892 
10893 
10894 
10895 
10896 
10897 
10898 
10899 
10900 
10901 /*
10902  * ad3d - Add Using Three Decimal Operands
10903  */
10904 
10905 void ad3d (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
10906 {
10907     EISstruct * e = & cpu.currentEISinstruction;
10908 
10909     fault_ipr_subtype_ mod_fault = 0;
10910 
10911 #if !defined(EIS_SETUP)
10912     setupOperandDescriptor(cpup, 1, &mod_fault);
10913     setupOperandDescriptor(cpup, 2, &mod_fault);
10914     setupOperandDescriptor(cpup, 3, &mod_fault);
10915 #endif
10916 
10917     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
10918     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
10919     parseNumericOperandDescriptor(cpup, 3, &mod_fault);
10920 
10921     L68_ (
10922       // L68 raises it immediately
10923       if (mod_fault)
10924         {
10925           doFault (FAULT_IPR,
10926                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
10927                    "Illegal modifier");
10928         }
10929     )
10930 
10931     // Bit 1 MBZ
10932     if (IWB_IRODD & 0200000000000)
10933       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "ad3d(): 1 MBZ");
10934 
10935     DPS8M_ (
10936       // DPS8M raises it delayed
10937       if (mod_fault)
10938         {
10939           doFault (FAULT_IPR,
10940                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
10941                    "Illegal modifier");
10942         }
10943     )
10944 
10945     // initialize mop flags. Probably best done elsewhere.
10946     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character control
10947     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;  // truncation bit
10948     bool R = getbits36_1 (cpu.cu.IWB, 10) != 0;  // rounding bit
10949 
10950     PNL (L68_ (if (R)
10951       DU_CYCLE_FRND;))
10952 
10953     uint srcTN = e->TN1;    // type of chars in src
10954 
10955     uint dstTN = e->TN3;    // type of chars in dst
10956     uint dstCN = e->CN3;    // starting at char pos CN
10957 
10958     int n1 = 0, n2 = 0, n3 = 0, sc1 = 0, sc2 = 0;
10959 
10960     /*
10961      * Here we need to distinguish between 4 type of numbers.
10962      *
10963      * CSFL - Floating-point, leading sign
10964      * CSLS - Scaled fixed-point, leading sign
10965      * CSTS - Scaled fixed-point, trailing sign
10966      * CSNS - Scaled fixed-point, unsigned
10967      */
10968 
10969     // determine precision
10970     switch(e->S1)
10971     {
10972         case CSFL:
10973             n1 = (int) e->N1 - 1; // need to account for the - sign
10974             if (srcTN == CTN4)
10975                 n1 -= 2;    // 2 4-bit digits exponent
10976             else
10977                 n1 -= 1;    // 1 9-bit digit exponent
10978             sc1 = 0;        // no scaling factor
10979             break;
10980 
10981         case CSLS:
10982         case CSTS:
10983             n1 = (int) e->N1 - 1; // only 1 sign
10984             sc1 = -e->SF1;
10985             break;
10986 
10987         case CSNS:
10988             n1 = (int) e->N1;     // no sign
10989             sc1 = -e->SF1;
10990             break;  // no sign wysiwyg
10991     }
10992 
10993     if (n1 < 1)
10994         doFault (FAULT_IPR, fst_ill_proc, "ad3d adjusted n1<1");
10995 
10996     switch(e->S2)
10997     {
10998         case CSFL:
10999             n2 = (int) e->N2 - 1; // need to account for the sign
11000             if (e->TN2 == CTN4)
11001                 n2 -= 2;    // 2 4-bit digit exponent
11002             else
11003                 n2 -= 1;    // 1 9-bit digit exponent
11004             sc2 = 0;        // no scaling factor
11005             break;
11006 
11007         case CSLS:
11008         case CSTS:
11009             n2 = (int) e->N2 - 1; // 1 sign
11010             sc2 = -e->SF2;
11011             break;
11012 
11013         case CSNS:
11014             n2 = (int) e->N2;     // no sign
11015             sc2 = -e->SF2;
11016             break;  // no sign wysiwyg
11017     }
11018 
11019     if (n2 < 1)
11020         doFault (FAULT_IPR, fst_ill_proc, "ad3d adjusted n2<1");
11021 
11022     switch(e->S3)
11023     {
11024         case CSFL:
11025             n3 = (int) e->N3 - 1; // need to account for the sign
11026             if (dstTN == CTN4)
11027                 n3 -= 2;    // 2 4-bit digit exponent
11028             else
11029                 n3 -= 1;    // 1 9-bit digit exponent
11030             break;
11031 
11032         case CSLS:
11033         case CSTS:
11034             n3 = (int) e->N3 - 1; // 1 sign
11035             break;
11036 
11037         case CSNS:
11038             n3 = (int) e->N3;     // no sign
11039             break;  // no sign wysiwyg
11040     }
11041 
11042     if (n3 < 1)
11043         doFault (FAULT_IPR, fst_ill_proc, "ad3d adjusted n3<1");
11044 
11045     decContext set;
11046     //decContextDefault(&set, DEC_INIT_BASE);         // initialize
11047     decContextDefaultDPS8(&set);
11048     set.traps=0;
11049 
11050     decNumber _1, _2, _3;
11051 
11052     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
11053 
11054     decNumber *op1 = decBCD9ToNumber(e->inBuffer, n1, sc1, &_1);
11055     if (e->sign == -1)
11056         op1->bits |= DECNEG;
11057     if (e->S1 == CSFL)
11058         op1->exponent = e->exponent;
11059 
11060     EISloadInputBufferNumeric (cpup, 2);   // according to MF2
11061 
11062     decNumber *op2 = decBCD9ToNumber(e->inBuffer, n2, sc2, &_2);
11063     if (e->sign == -1)
11064         op2->bits |= DECNEG;
11065     if (e->S2 == CSFL)
11066         op2->exponent = e->exponent;
11067 
11068     // The aligned result can be longer than the context's 65 digits (e.g. a
11069     // 42-digit operand at SF 16 and a 56-digit one at SF -25 span 83
11070     // digits). Rounding it to 65 digits here lost low-order digits that the
11071     // hardware keeps (ISOLTS PS905 sb2d); compute it exactly (126 digits
11072     // covers any fixed-point alignment) and let the rules below drop
11073     // digits. Only for a fixed-point result: with a floating result
11074     // (PS905 sb2d, 9-bit floating op2 with exponent -128 minus a 1-digit
11075     // 4-bit op1) the hardware matches the 65-digit context, and the exact
11076     // value differs from it.
11077     decNumber *op3;
11078     bool winLost = false;
11079     bool fltAlignTrunc = false;
11080     if (e->S3 != CSFL)
11081     {
11082       enum rounding safeR = decContextGetRounding(&set);
11083       int safe = set.digits;
11084       set.digits = DECNUMDIGITS;
11085       decContextSetRounding(&set, DEC_ROUND_DOWN);
11086       winLost = eisWindowOperands (op1, op2);
11087       op3 = decNumberAdd(&_3, op1, op2, &set);
11088       set.digits = safe;
11089       decContextSetRounding(&set, safeR);
11090     }
11091     else
11092     {
11093       fltAlignTrunc = eisFloatAlignTrunc (op1, op2);
11094       eisFloatWindowOperands (op1, op2);
11095       op3 = decNumberAdd(&_3, op1, op2, &set);
11096     }
11097 
11098     // RJ78: significant digits in the result may be lost if:
11099     // The difference between the scaling factors (exponents) of the source
11100     // operands is large enough to cause the expected length of the intermediate
11101     // result to exceed 63 digits after decimal point alignment of source operands, followed by addition.
11102     // ISOLTS 846 07c, 10a, 11b internal register overflow
11103     // trailing zeros are not counted towards the limit
11104 
11105     // XXX it is not clear which digits are lost, but I suppose it should be the most significant.
11106     // ISOLTS doesn't check for this.
11107 
11108     // XXX the algorithm should be similar/the same as dv3d NQ? It is to some extent already...
11109     bool iOvr = winLost;
11110     if (op3->digits > 63 && e->S3 == CSFL) {
11111         // Floating-point result: the hardware keeps the most significant
11112         // digits and doesn't overflow; the result is truncated (ISOLTS PS905,
11113         // ad3d with a floating result: s/b kept the leading digits, no
11114         // overflow indicator). formatDecimal then fits it to the result's
11115         // digits and sets truncation.
11116         enum rounding safeR = decContextGetRounding(&set);
11117         int safe = set.digits;
11118         decContextSetRounding(&set, DEC_ROUND_DOWN);
11119         set.digits = 63;
11120         decNumberPlus(op3, op3, &set);
11121         set.digits = safe;
11122         decContextSetRounding(&set, safeR);
11123     } else if (op3->digits > FIXED_OVERFLOW_DIGITS) {
11124         uint8_t pr[256];
11125         // if sf<=0, trailing zeroes can't be shifted out
11126         // if sf> 0, (some of) trailing zeroes can be shifted out
11127         int sf = e->S3==CSFL?op3->exponent:e->SF3;
11128 
11129         int ctz = 0;
11130         if (sf>0) {     // optimize: we don't care when sf>0
11131             decNumberGetBCD(op3,pr);
11132             for (int i=op3->digits-1;i>=0 && pr[i]==0;i--)
11133                  ctz ++;
11134         }
11135 
11136         int span = op3->digits - min(max(sf,0),ctz);
11137         if (span > FIXED_OVERFLOW_DIGITS)
11138             iOvr = 1;
11139         if (span > FIXED_ALIGN_DIGITS) {
11140             enum rounding safeR = decContextGetRounding(&set);         // save rounding mode
11141             int safe = set.digits;
11142             decNumber tmp;
11143 
11144             // discard MS digits
11145             decContextSetRounding(&set, DEC_ROUND_DOWN);     // Round towards 0 (truncation).
11146             set.digits = span - FIXED_ALIGN_DIGITS;
11147             decNumberPlus(&tmp, op3, &set);
11148             set.digits = safe;
11149 
11150             decNumberSubtract(op3, op3, &tmp, &set);
11151 
11152             //decNumberToString(op3,(char*)pr); sim_printf("discarded: %s\r\n",pr);
11153 
11154             decContextSetRounding(&set, safeR);
11155         }
11156     }
11157 
11158     // A floating-point result's truncation: see eisFloatAlignTrunc.
11159     bool Ovr = false, EOvr = false, Trunc = false;
11160 
11161     uint8_t out [256];
11162     char *res = formatDecimal(out, &set, op3, n3, (int) e->S3, e->SF3, R, &Ovr, &Trunc);
11163     if (fltAlignTrunc)
11164         Trunc = true;
11165 
11166     Ovr |= iOvr;
11167 
11168     if (decNumberIsZero(op3))
11169         op3->exponent = 127;
11170 
11171     //(void)printf("%s\r\n", res);
11172 
11173     // now write to memory in proper format.....
11174 
11175     //word18 dstAddr = e->dstAddr;
11176     int pos = (int) dstCN;
11177 
11178     // 1st, take care of any leading sign .......
11179     switch(e->S3)
11180     {
11181         case CSFL:  // floating-point, leading sign.
11182         case CSLS:  // fixed-point, leading sign
11183             switch(dstTN)
11184             {
11185             case CTN4:
11186                 if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
11187                           //   then the 13(8) plus sign character is placed appropriately
11188                           //   if the result of the operation is positive.
11189                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11190                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
11191                 else
11192                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11193                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
11194                 break;
11195             case CTN9:
11196                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11197                            (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
11198                 break;
11199             }
11200             break;
11201 
11202         case CSTS:  // nuttin' to do here .....
11203         case CSNS:
11204             break;  // no sign wysiwyg
11205     }
11206 
11207     // 2nd, write the digits .....
11208     for(int i = 0 ; i < n3 ; i++)
11209         switch(dstTN)
11210         {
11211         case CTN4:
11212             EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) (res[i] - '0'));
11213             break;
11214         case CTN9:
11215             EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) res[i]);
11216             break;
11217         }
11218 
11219     // 3rd, take care of any trailing sign or exponent ...
11220     switch(e->S3)
11221     {
11222         case CSTS:  // write trailing sign ....
11223             switch(dstTN)
11224             {
11225             case CTN4:
11226                 if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
11227                           //   then the 13(8) plus sign character is placed appropriately
11228                           //   if the result of the operation is positive.
11229                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11230                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
11231                 else
11232                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11233                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
11234                 break;
11235             case CTN9:
11236                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11237                            (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
11238                 break;
11239             }
11240             break;
11241 
11242         case CSFL:  // floating-point, leading sign.
11243             // write the exponent
11244             switch(dstTN)
11245             {
11246             case CTN4:
11247                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (op3->exponent >> 4) & 0xf); // upper 4-bits
11248                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,  op3->exponent       & 0xf); // lower 4-bits
11249 
11250                     break;
11251             case CTN9:
11252                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, op3->exponent & 0xff);    // write 8-bit exponent
11253                 break;
11254             }
11255             break;
11256 
11257         case CSLS:  // fixed point, leading sign - already done
11258         case CSNS:  // fixed point, unsigned - nuttin' needed to do
11259             break;
11260     }
11261 
11262     // set flags, etc ...
11263     if (e->S3 == CSFL)
11264     {
11265         if (op3->exponent > 127)
11266         {
11267             SET_I_EOFL;
11268             EOvr = true;
11269         }
11270         if (op3->exponent < -128)
11271         {
11272             SET_I_EUFL;
11273             EOvr = true;
11274         }
11275     }
11276 
11277     SC_I_NEG (decNumberIsNegative(op3) && !decNumberIsZero(op3));  // set negative indicator if op3 < 0
11278     SC_I_ZERO (decNumberIsZero(op3));     // set zero indicator if op3 == 0
11279 
11280     SC_I_TRUNC (Trunc); // formatDecimal sets it only without rounding, except for a floating result dropping digits (ISOLTS PS905)
11281 
11282     cleanupOperandDescriptor (cpup, 1);
11283     cleanupOperandDescriptor (cpup, 2);
11284     cleanupOperandDescriptor (cpup, 3);
11285 
11286     if (TST_I_TRUNC && T && tstOVFfault (cpup))
11287       doFault(FAULT_OFL, fst_zero, "ad3d truncation(overflow) fault");
11288     if (EOvr && tstOVFfault (cpup))
11289         doFault(FAULT_OFL, fst_zero, "ad3d over/underflow fault");
11290     if (Ovr)
11291     {
11292         SET_I_OFLOW;
11293         if (tstOVFfault (cpup))
11294           doFault(FAULT_OFL, fst_zero, "ad3d overflow fault");
11295     }
11296 }
11297 
11298 /*
11299  * sb2d - Subtract Using Two Decimal Operands
11300  */
11301 
11302 void sb2d (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
11303 {
11304     EISstruct * e = & cpu.currentEISinstruction;
11305 
11306     fault_ipr_subtype_ mod_fault = 0;
11307 
11308 #if !defined(EIS_SETUP)
11309     setupOperandDescriptor(cpup, 1, &mod_fault);
11310     setupOperandDescriptor(cpup, 2, &mod_fault);
11311     setupOperandDescriptorCache(cpup,3);
11312 #endif
11313 
11314     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
11315     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
11316 
11317     L68_ (
11318       // L68 raises it immediately
11319       if (mod_fault)
11320         {
11321           doFault (FAULT_IPR,
11322                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
11323                    "Illegal modifier");
11324         }
11325     )
11326 
11327     // Bits 1-8 MBZ
11328     if (IWB_IRODD & 0377000000000)
11329       {
11330         //sim_printf ("sb2d %012"PRIo64"\r\n", IWB_IRODD);
11331         doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "sb2d 0-8 MBZ");
11332       }
11333 
11334     DPS8M_ (
11335       // DPS8M raises it delayed
11336       if (mod_fault)
11337         {
11338           doFault (FAULT_IPR,
11339                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
11340                    "Illegal modifier");
11341         }
11342     )
11343 
11344     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character control
11345     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;  // truncation bit
11346     bool R = getbits36_1 (cpu.cu.IWB, 10) != 0;  // rounding bit
11347 
11348     PNL (L68_ (if (R)
11349       DU_CYCLE_FRND;))
11350 
11351     uint srcTN = e->TN1;    // type of chars in src
11352 
11353     uint dstTN = e->TN2;    // type of chars in dst
11354     uint dstCN = e->CN2;    // starting at char pos CN
11355 
11356     e->ADDR3 = e->ADDR2;
11357 
11358     int n1 = 0, n2 = 0, sc1 = 0, sc2 = 0;
11359 
11360     /*
11361      * Here we need to distinguish between 4 type of numbers.
11362      *
11363      * CSFL - Floating-point, leading sign
11364      * CSLS - Scaled fixed-point, leading sign
11365      * CSTS - Scaled fixed-point, trailing sign
11366      * CSNS - Scaled fixed-point, unsigned
11367      */
11368 
11369     // determine precision
11370     switch(e->S1)
11371     {
11372         case CSFL:
11373             n1 = (int) e->N1 - 1; // need to account for the - sign
11374             if (srcTN == CTN4)
11375                 n1 -= 2;    // 2 4-bit digits exponent
11376             else
11377                 n1 -= 1;    // 1 9-bit digit exponent
11378             sc1 = 0;        // no scaling factor
11379             break;
11380 
11381         case CSLS:
11382         case CSTS:
11383             n1 = (int) e->N1 - 1; // only 1 sign
11384             sc1 = -e->SF1;
11385             break;
11386 
11387         case CSNS:
11388             n1 = (int) e->N1;     // no sign
11389             sc1 = -e->SF1;
11390             break;  // no sign wysiwyg
11391     }
11392 
11393     if (n1 < 1)
11394         doFault (FAULT_IPR, fst_ill_proc, "sb2d adjusted n1<1");
11395 
11396     switch(e->S2)
11397     {
11398         case CSFL:
11399             n2 = (int) e->N2 - 1; // need to account for the sign
11400             if (e->TN2 == CTN4)
11401                 n2 -= 2;    // 2 4-bit digit exponent
11402             else
11403                 n2 -= 1;    // 1 9-bit digit exponent
11404             sc2 = 0;        // no scaling factor
11405             break;
11406 
11407         case CSLS:
11408         case CSTS:
11409             n2 = (int) e->N2 - 1; // 1 sign
11410             sc2 = -e->SF2;
11411             break;
11412 
11413         case CSNS:
11414             n2 = (int) e->N2;     // no sign
11415             sc2 = -e->SF2;
11416             break;  // no sign wysiwyg
11417     }
11418 
11419     if (n2 < 1)
11420         doFault (FAULT_IPR, fst_ill_proc, "sb2d adjusted n2<1");
11421 
11422     decContext set;
11423     //decContextDefault(&set, DEC_INIT_BASE);         // initialize
11424     decContextDefaultDPS8(&set);
11425     set.traps=0;
11426 
11427     decNumber _1, _2, _3;
11428 
11429     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
11430 
11431     decNumber *op1 = decBCD9ToNumber(e->inBuffer, n1, sc1, &_1);
11432     if (e->sign == -1)
11433         op1->bits |= DECNEG;
11434     if (e->S1 == CSFL)
11435         op1->exponent = e->exponent;
11436 
11437     EISloadInputBufferNumeric (cpup, 2);   // according to MF2
11438 
11439     decNumber *op2 = decBCD9ToNumber(e->inBuffer, n2, sc2, &_2);
11440     if (e->sign == -1)
11441         op2->bits |= DECNEG;
11442     if (e->S2 == CSFL)
11443         op2->exponent = e->exponent;
11444 
11445     // The aligned result can be longer than the context's 65 digits (e.g. a
11446     // 42-digit operand at SF 16 and a 56-digit one at SF -25 span 83
11447     // digits). Rounding it to 65 digits here lost low-order digits that the
11448     // hardware keeps (ISOLTS PS905 sb2d); compute it exactly (126 digits
11449     // covers any fixed-point alignment) and let the rules below drop
11450     // digits. Only for a fixed-point result: with a floating result
11451     // (PS905 sb2d, 9-bit floating op2 with exponent -128 minus a 1-digit
11452     // 4-bit op1) the hardware matches the 65-digit context, and the exact
11453     // value differs from it.
11454     decNumber *op3;
11455     bool winLost = false;
11456     bool fltAlignTrunc = false;
11457     if (e->S2 != CSFL)
11458     {
11459       enum rounding safeR = decContextGetRounding(&set);
11460       int safe = set.digits;
11461       set.digits = DECNUMDIGITS;
11462       decContextSetRounding(&set, DEC_ROUND_DOWN);
11463       winLost = eisWindowOperands (op1, op2);
11464       op3 = decNumberSubtract(&_3, op2, op1, &set);
11465       set.digits = safe;
11466       decContextSetRounding(&set, safeR);
11467     }
11468     else
11469     {
11470       fltAlignTrunc = eisFloatAlignTrunc (op1, op2);
11471       eisFloatWindowOperands (op1, op2);
11472       op3 = decNumberSubtract(&_3, op2, op1, &set);
11473     }
11474 
11475     // ISOLTS 846 07c, 10a, 11b internal register overflow - see ad3d
11476     bool iOvr = winLost;
11477     if (op3->digits > 63 && e->S2 == CSFL) {
11478         // Floating-point result: the hardware keeps the most significant
11479         // digits and doesn't overflow; the result is truncated (ISOLTS PS905,
11480         // ad3d with a floating result: s/b kept the leading digits, no
11481         // overflow indicator). formatDecimal then fits it to the result's
11482         // digits and sets truncation.
11483         enum rounding safeR = decContextGetRounding(&set);
11484         int safe = set.digits;
11485         decContextSetRounding(&set, DEC_ROUND_DOWN);
11486         set.digits = 63;
11487         decNumberPlus(op3, op3, &set);
11488         set.digits = safe;
11489         decContextSetRounding(&set, safeR);
11490     } else if (op3->digits > FIXED_OVERFLOW_DIGITS) {
11491         uint8_t pr[256];
11492         // if sf<=0, trailing zeroes can't be shifted out
11493         // if sf> 0, (some of) trailing zeroes can be shifted out
11494         int sf = e->S2==CSFL?op3->exponent:e->SF2;
11495 
11496         int ctz = 0;
11497         if (sf>0) {     // optimize: we don't care when sf>0
11498             decNumberGetBCD(op3,pr);
11499             for (int i=op3->digits-1;i>=0 && pr[i]==0;i--)
11500                  ctz ++;
11501         }
11502 
11503         int span = op3->digits - min(max(sf,0),ctz);
11504         if (span > FIXED_OVERFLOW_DIGITS)
11505             iOvr = 1;
11506         if (span > FIXED_ALIGN_DIGITS) {
11507             enum rounding safeR = decContextGetRounding(&set);         // save rounding mode
11508             int safe = set.digits;
11509             decNumber tmp;
11510 
11511             // discard MS digits
11512             decContextSetRounding(&set, DEC_ROUND_DOWN);     // Round towards 0 (truncation).
11513             set.digits = span - FIXED_ALIGN_DIGITS;
11514             decNumberPlus(&tmp, op3, &set);
11515             set.digits = safe;
11516 
11517             decNumberSubtract(op3, op3, &tmp, &set);
11518 
11519             //decNumberToString(op3,(char*)pr); sim_printf("discarded: %s\r\n",pr);
11520 
11521             decContextSetRounding(&set, safeR);
11522         }
11523     }
11524 
11525     // A floating-point result's truncation: see eisFloatAlignTrunc.
11526     bool Ovr = false, EOvr = false, Trunc = false;
11527 
11528     uint8_t out [256];
11529     char *res = formatDecimal(out, &set, op3, n2, (int) e->S2, e->SF2, R, &Ovr, &Trunc);
11530     if (fltAlignTrunc)
11531         Trunc = true;
11532 
11533     Ovr |= iOvr;
11534 
11535     if (decNumberIsZero(op3))
11536         op3->exponent = 127;
11537 
11538     //(void)printf("%s\r\n", res);
11539 
11540     // now write to memory in proper format.....
11541 
11542     //word18 dstAddr = e->dstAddr;
11543     int pos = (int) dstCN;
11544 
11545     // 1st, take care of any leading sign .......
11546     switch(e->S2)
11547     {
11548         case CSFL:  // floating-point, leading sign.
11549         case CSLS:  // fixed-point, leading sign
11550             switch(dstTN)
11551             {
11552                 case CTN4:
11553                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
11554                               //   then the 13(8) plus sign character is placed appropriately
11555                               //   if the result of the operation is positive.
11556                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11557                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
11558                     else
11559                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11560                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
11561                     break;
11562                 case CTN9:
11563                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11564                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
11565                     break;
11566             }
11567             break;
11568 
11569         case CSTS:  // nuttin' to do here .....
11570         case CSNS:
11571             break;  // no sign wysiwyg
11572     }
11573 
11574     // 2nd, write the digits .....
11575     for(int i = 0 ; i < n2 ; i++)
11576         switch(dstTN)
11577         {
11578             case CTN4:
11579                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) (res[i] - '0'));
11580                 break;
11581             case CTN9:
11582                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) res[i]);
11583                 break;
11584         }
11585 
11586     // 3rd, take care of any trailing sign or exponent ...
11587     switch(e->S2)
11588     {
11589         case CSTS:  // write trailing sign ....
11590             switch(dstTN)
11591             {
11592                 case CTN4:
11593                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
11594                               //   then the 13(8) plus sign character is placed appropriately
11595                               //   if the result of the operation is positive.
11596                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11597                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
11598                     else
11599                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11600                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
11601                     break;
11602                 case CTN9:
11603                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11604                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
11605                     break;
11606             }
11607             break;
11608 
11609         case CSFL:  // floating-point, leading sign.
11610             // write the exponent
11611             switch(dstTN)
11612             {
11613                 case CTN4:
11614                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (op3->exponent >> 4) & 0xf); // upper 4-bits
11615                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,  op3->exponent       & 0xf); // lower 4-bits
11616 
11617                     break;
11618                 case CTN9:
11619                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, op3->exponent & 0xff);    // write 8-bit exponent
11620                     break;
11621             }
11622             break;
11623 
11624         case CSLS:  // fixed point, leading sign - already done
11625         case CSNS:  // fixed point, unsigned - nuttin' needed to do
11626             break;
11627     }
11628 
11629     // set flags, etc ...
11630     if (e->S2 == CSFL)
11631     {
11632         if (op3->exponent > 127)
11633         {
11634             SET_I_EOFL;
11635             EOvr = true;
11636         }
11637         if (op3->exponent < -128)
11638         {
11639             SET_I_EUFL;
11640             EOvr = true;
11641         }
11642     }
11643 
11644     SC_I_NEG (decNumberIsNegative(op3) && !decNumberIsZero(op3));  // set negative indicator if op3 < 0
11645     SC_I_ZERO (decNumberIsZero(op3));     // set zero indicator if op3 == 0
11646 
11647     SC_I_TRUNC (Trunc); // formatDecimal sets it only without rounding, except for a floating result dropping digits (ISOLTS PS905)
11648 
11649     cleanupOperandDescriptor (cpup, 1);
11650     cleanupOperandDescriptor (cpup, 2);
11651     cleanupOperandDescriptor (cpup, 3);
11652 
11653     if (TST_I_TRUNC && T && tstOVFfault (cpup))
11654       doFault(FAULT_OFL, fst_zero, "sb2d truncation(overflow) fault");
11655     if (EOvr && tstOVFfault (cpup))
11656         doFault(FAULT_OFL, fst_zero, "sb2d over/underflow fault");
11657     if (Ovr)
11658     {
11659         SET_I_OFLOW;
11660         if (tstOVFfault (cpup))
11661           doFault(FAULT_OFL, fst_zero, "sb2d overflow fault");
11662     }
11663 }
11664 
11665 /*
11666  * sb3d - Subtract Using Three Decimal Operands
11667  */
11668 
11669 void sb3d (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
11670 {
11671     EISstruct * e = & cpu.currentEISinstruction;
11672 
11673     fault_ipr_subtype_ mod_fault = 0;
11674 
11675 #if !defined(EIS_SETUP)
11676     setupOperandDescriptor(cpup, 1, &mod_fault);
11677     setupOperandDescriptor(cpup, 2, &mod_fault);
11678     setupOperandDescriptor(cpup, 3, &mod_fault);
11679 #endif
11680 
11681     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
11682     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
11683     parseNumericOperandDescriptor(cpup, 3, &mod_fault);
11684 
11685     L68_ (
11686       // L68 raises it immediately
11687       if (mod_fault)
11688         {
11689           doFault (FAULT_IPR,
11690                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
11691                    "Illegal modifier");
11692         }
11693     )
11694 
11695     // Bit 1 MBZ
11696     if (IWB_IRODD & 0200000000000)
11697       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "sb3d(): 1 MBZ");
11698 
11699     DPS8M_ (
11700       // DPS8M raises it delayed
11701       if (mod_fault)
11702         {
11703           doFault (FAULT_IPR,
11704                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
11705                    "Illegal modifier");
11706         }
11707     )
11708 
11709     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character control
11710     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;  // truncation bit
11711     bool R = getbits36_1 (cpu.cu.IWB, 10) != 0;  // rounding bit
11712 
11713     PNL (L68_ (if (R)
11714       DU_CYCLE_FRND;))
11715 
11716     uint srcTN = e->TN1;    // type of chars in src
11717 
11718     uint dstTN = e->TN3;    // type of chars in dst
11719     uint dstCN = e->CN3;    // starting at char pos CN
11720 
11721     int n1 = 0, n2 = 0, n3 = 0, sc1 = 0, sc2 = 0;
11722 
11723     /*
11724      * Here we need to distinguish between 4 type of numbers.
11725      *
11726      * CSFL - Floating-point, leading sign
11727      * CSLS - Scaled fixed-point, leading sign
11728      * CSTS - Scaled fixed-point, trailing sign
11729      * CSNS - Scaled fixed-point, unsigned
11730      */
11731 
11732     // determine precision
11733     switch(e->S1)
11734     {
11735         case CSFL:
11736             n1 = (int) e->N1 - 1; // need to account for the - sign
11737             if (srcTN == CTN4)
11738                 n1 -= 2;    // 2 4-bit digits exponent
11739             else
11740                 n1 -= 1;    // 1 9-bit digit exponent
11741             sc1 = 0;        // no scaling factor
11742             break;
11743 
11744         case CSLS:
11745         case CSTS:
11746             n1 = (int) e->N1 - 1; // only 1 sign
11747             sc1 = -e->SF1;
11748             break;
11749 
11750         case CSNS:
11751             n1 = (int) e->N1;     // no sign
11752             sc1 = -e->SF1;
11753             break;  // no sign wysiwyg
11754     }
11755 
11756     if (n1 < 1)
11757         doFault (FAULT_IPR, fst_ill_proc, "sb3d adjusted n1<1");
11758 
11759     switch(e->S2)
11760     {
11761         case CSFL:
11762             n2 = (int) e->N2 - 1; // need to account for the sign
11763             if (e->TN2 == CTN4)
11764                 n2 -= 2;    // 2 4-bit digit exponent
11765             else
11766                 n2 -= 1;    // 1 9-bit digit exponent
11767             sc2 = 0;        // no scaling factor
11768             break;
11769 
11770         case CSLS:
11771         case CSTS:
11772             n2 = (int) e->N2 - 1; // 1 sign
11773             sc2 = -e->SF2;
11774             break;
11775 
11776         case CSNS:
11777             n2 = (int) e->N2;     // no sign
11778             sc2 = -e->SF2;
11779             break;  // no sign wysiwyg
11780     }
11781 
11782     if (n2 < 1)
11783         doFault (FAULT_IPR, fst_ill_proc, "sb3d adjusted n2<1");
11784 
11785     switch(e->S3)
11786     {
11787         case CSFL:
11788             n3 = (int) e->N3 - 1; // need to account for the sign
11789             if (dstTN == CTN4)
11790                 n3 -= 2;    // 2 4-bit digit exponent
11791             else
11792                 n3 -= 1;    // 1 9-bit digit exponent
11793             break;
11794 
11795         case CSLS:
11796         case CSTS:
11797             n3 = (int) e->N3 - 1; // 1 sign
11798             break;
11799 
11800         case CSNS:
11801             n3 = (int) e->N3;     // no sign
11802             break;  // no sign wysiwyg
11803     }
11804 
11805     if (n3 < 1)
11806         doFault (FAULT_IPR, fst_ill_proc, "sb3d adjusted n3<1");
11807 
11808     decContext set;
11809     //decContextDefault(&set, DEC_INIT_BASE);         // initialize
11810     decContextDefaultDPS8(&set);
11811 
11812     set.traps=0;
11813 
11814     decNumber _1, _2, _3;
11815 
11816     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
11817 
11818     decNumber *op1 = decBCD9ToNumber(e->inBuffer, n1, sc1, &_1);
11819     if (e->sign == -1)
11820         op1->bits |= DECNEG;
11821     if (e->S1 == CSFL)
11822         op1->exponent = e->exponent;
11823 
11824     EISloadInputBufferNumeric (cpup, 2);   // according to MF2
11825 
11826     decNumber *op2 = decBCD9ToNumber(e->inBuffer, n2, sc2, &_2);
11827     if (e->sign == -1)
11828         op2->bits |= DECNEG;
11829     if (e->S2 == CSFL)
11830         op2->exponent = e->exponent;
11831 
11832     // The aligned result can be longer than the context's 65 digits (e.g. a
11833     // 42-digit operand at SF 16 and a 56-digit one at SF -25 span 83
11834     // digits). Rounding it to 65 digits here lost low-order digits that the
11835     // hardware keeps (ISOLTS PS905 sb2d); compute it exactly (126 digits
11836     // covers any fixed-point alignment) and let the rules below drop
11837     // digits. Only for a fixed-point result: with a floating result
11838     // (PS905 sb2d, 9-bit floating op2 with exponent -128 minus a 1-digit
11839     // 4-bit op1) the hardware matches the 65-digit context, and the exact
11840     // value differs from it.
11841     decNumber *op3;
11842     bool winLost = false;
11843     bool fltAlignTrunc = false;
11844     if (e->S3 != CSFL)
11845     {
11846       enum rounding safeR = decContextGetRounding(&set);
11847       int safe = set.digits;
11848       set.digits = DECNUMDIGITS;
11849       decContextSetRounding(&set, DEC_ROUND_DOWN);
11850       winLost = eisWindowOperands (op1, op2);
11851       op3 = decNumberSubtract(&_3, op2, op1, &set);
11852       set.digits = safe;
11853       decContextSetRounding(&set, safeR);
11854     }
11855     else
11856     {
11857       fltAlignTrunc = eisFloatAlignTrunc (op1, op2);
11858       eisFloatWindowOperands (op1, op2);
11859       op3 = decNumberSubtract(&_3, op2, op1, &set);
11860     }
11861 
11862     // ISOLTS 846 07c, 10a, 11b internal register overflow - see ad3d
11863     bool iOvr = winLost;
11864     if (op3->digits > 63 && e->S3 == CSFL) {
11865         // Floating-point result: the hardware keeps the most significant
11866         // digits and doesn't overflow; the result is truncated (ISOLTS PS905,
11867         // ad3d with a floating result: s/b kept the leading digits, no
11868         // overflow indicator). formatDecimal then fits it to the result's
11869         // digits and sets truncation.
11870         enum rounding safeR = decContextGetRounding(&set);
11871         int safe = set.digits;
11872         decContextSetRounding(&set, DEC_ROUND_DOWN);
11873         set.digits = 63;
11874         decNumberPlus(op3, op3, &set);
11875         set.digits = safe;
11876         decContextSetRounding(&set, safeR);
11877     } else if (op3->digits > FIXED_OVERFLOW_DIGITS) {
11878         uint8_t pr[256];
11879         // if sf<=0, trailing zeroes can't be shifted out
11880         // if sf> 0, (some of) trailing zeroes can be shifted out
11881         int sf = e->S3==CSFL?op3->exponent:e->SF3;
11882 
11883         int ctz = 0;
11884         if (sf>0) {     // optimize: we don't care when sf>0
11885             decNumberGetBCD(op3,pr);
11886             for (int i=op3->digits-1;i>=0 && pr[i]==0;i--)
11887                  ctz ++;
11888         }
11889 
11890         int span = op3->digits - min(max(sf,0),ctz);
11891         if (span > FIXED_OVERFLOW_DIGITS)
11892             iOvr = 1;
11893         if (span > FIXED_ALIGN_DIGITS) {
11894             enum rounding safeR = decContextGetRounding(&set);         // save rounding mode
11895             int safe = set.digits;
11896             decNumber tmp;
11897 
11898             // discard MS digits
11899             decContextSetRounding(&set, DEC_ROUND_DOWN);     // Round towards 0 (truncation).
11900             set.digits = span - FIXED_ALIGN_DIGITS;
11901             decNumberPlus(&tmp, op3, &set);
11902             set.digits = safe;
11903 
11904             decNumberSubtract(op3, op3, &tmp, &set);
11905 
11906             //decNumberToString(op3,(char*)pr); sim_printf("discarded: %s\r\n",pr);
11907 
11908             decContextSetRounding(&set, safeR);
11909         }
11910     }
11911 
11912     // A floating-point result's truncation: see eisFloatAlignTrunc.
11913     bool Ovr = false, EOvr = false, Trunc = false;
11914 
11915     uint8_t out [256];
11916     char *res = formatDecimal(out, &set, op3, n3, (int) e->S3, e->SF3, R, &Ovr, &Trunc);
11917     if (fltAlignTrunc)
11918         Trunc = true;
11919 
11920     Ovr |= iOvr;
11921 
11922     if (decNumberIsZero(op3))
11923         op3->exponent = 127;
11924 
11925     // now write to memory in proper format.....
11926 
11927     //word18 dstAddr = e->dstAddr;
11928     int pos = (int) dstCN;
11929 
11930     // 1st, take care of any leading sign .......
11931     switch(e->S3)
11932     {
11933         case CSFL:  // floating-point, leading sign.
11934         case CSLS:  // fixed-point, leading sign
11935             switch(dstTN)
11936         {
11937             case CTN4:
11938                 if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
11939                           //   then the 13(8) plus sign character is placed appropriately
11940                           //   if the result of the operation is positive.
11941                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11942                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
11943                 else
11944                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11945                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
11946                 break;
11947             case CTN9:
11948                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11949                            (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
11950                 break;
11951         }
11952             break;
11953 
11954         case CSTS:  // nuttin' to do here .....
11955         case CSNS:
11956             break;  // no sign wysiwyg
11957     }
11958 
11959     // 2nd, write the digits .....
11960     for(int i = 0 ; i < n3 ; i++)
11961         switch(dstTN)
11962     {
11963         case CTN4:
11964             EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) (res[i] - '0'));
11965             break;
11966         case CTN9:
11967             EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) res[i]);
11968             break;
11969     }
11970 
11971     // 3rd, take care of any trailing sign or exponent ...
11972     switch(e->S3)
11973     {
11974         case CSTS:  // write trailing sign ....
11975             switch(dstTN)
11976             {
11977                 case CTN4:
11978                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
11979                               //   then the 13(8) plus sign character is placed appropriately
11980                               //   if the result of the operation is positive.
11981                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11982                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
11983                     else
11984                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11985                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
11986                     break;
11987                 case CTN9:
11988                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
11989                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
11990                     break;
11991             }
11992             break;
11993 
11994         case CSFL:  // floating-point, leading sign.
11995             // write the exponent
11996             switch(dstTN)
11997             {
11998                 case CTN4:
11999                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (op3->exponent >> 4) & 0xf); // upper 4-bits
12000                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,  op3->exponent       & 0xf); // lower 4-bits
12001 
12002                     break;
12003                 case CTN9:
12004                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, op3->exponent & 0xff);    // write 8-bit exponent
12005                     break;
12006             }
12007             break;
12008 
12009         case CSLS:  // fixed point, leading sign - already done
12010         case CSNS:  // fixed point, unsigned - nuttin' needed to do
12011             break;
12012     }
12013 
12014     // set flags, etc ...
12015     if (e->S3 == CSFL)
12016     {
12017         if (op3->exponent > 127)
12018         {
12019             SET_I_EOFL;
12020             EOvr = true;
12021         }
12022         if (op3->exponent < -128)
12023         {
12024             SET_I_EUFL;
12025             EOvr = true;
12026         }
12027     }
12028 
12029     SC_I_NEG (decNumberIsNegative(op3) && !decNumberIsZero(op3));  // set negative indicator if op3 < 0
12030     SC_I_ZERO (decNumberIsZero(op3));     // set zero indicator if op3 == 0
12031 
12032     SC_I_TRUNC (Trunc); // formatDecimal sets it only without rounding, except for a floating result dropping digits (ISOLTS PS905)
12033 
12034     cleanupOperandDescriptor (cpup, 1);
12035     cleanupOperandDescriptor (cpup, 2);
12036     cleanupOperandDescriptor (cpup, 3);
12037 
12038     if (TST_I_TRUNC && T && tstOVFfault (cpup))
12039       doFault(FAULT_OFL, fst_zero, "sb3d truncation(overflow) fault");
12040     if (EOvr && tstOVFfault (cpup))
12041         doFault(FAULT_OFL, fst_zero, "sb3d over/underflow fault");
12042     if (Ovr)
12043     {
12044         SET_I_OFLOW;
12045         if (tstOVFfault (cpup))
12046           doFault(FAULT_OFL, fst_zero, "sb3d overflow fault");
12047     }
12048 }
12049 
12050 /*
12051  * mp2d - Multiply Using Two Decimal Operands
12052  */
12053 
12054 void mp2d (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
12055 {
12056     EISstruct * e = & cpu.currentEISinstruction;
12057 
12058     fault_ipr_subtype_ mod_fault = 0;
12059 
12060 #if !defined(EIS_SETUP)
12061     setupOperandDescriptor(cpup, 1, &mod_fault);
12062     setupOperandDescriptor(cpup, 2, &mod_fault);
12063     setupOperandDescriptorCache(cpup,3);
12064 #endif
12065 
12066     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
12067     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
12068 
12069     L68_ (
12070       // L68 raises it immediately
12071       if (mod_fault)
12072         {
12073           doFault (FAULT_IPR,
12074                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
12075                    "Illegal modifier");
12076         }
12077     )
12078 
12079     // Bits 1-8 MBZ
12080     if (IWB_IRODD & 0377000000000)
12081       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "mp2d 1-8 MBZ");
12082 
12083     DPS8M_ (
12084       // DPS8M raises it delayed
12085       if (mod_fault)
12086         {
12087           doFault (FAULT_IPR,
12088                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
12089                    "Illegal modifier");
12090         }
12091     )
12092 
12093     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character control
12094     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;  // truncation bit
12095     bool R = getbits36_1 (cpu.cu.IWB, 10) != 0;  // rounding bit
12096 
12097     PNL (L68_ (if (R)
12098       DU_CYCLE_FRND;))
12099 
12100     uint srcTN = e->TN1;    // type of chars in src
12101 
12102     uint dstTN = e->TN2;    // type of chars in dst
12103     uint dstCN = e->CN2;    // starting at char pos CN
12104 
12105     e->ADDR3 = e->ADDR2;
12106 
12107     int n1 = 0, n2 = 0, sc1 = 0, sc2 = 0;
12108 
12109     /*
12110      * Here we need to distinguish between 4 type of numbers.
12111      *
12112      * CSFL - Floating-point, leading sign
12113      * CSLS - Scaled fixed-point, leading sign
12114      * CSTS - Scaled fixed-point, trailing sign
12115      * CSNS - Scaled fixed-point, unsigned
12116      */
12117 
12118     // determine precision
12119     switch(e->S1)
12120     {
12121         case CSFL:
12122             n1 = (int) e->N1 - 1; // need to account for the - sign
12123             if (srcTN == CTN4)
12124                 n1 -= 2;    // 2 4-bit digits exponent
12125             else
12126                 n1 -= 1;    // 1 9-bit digit exponent
12127             sc1 = 0;        // no scaling factor
12128             break;
12129 
12130         case CSLS:
12131         case CSTS:
12132             n1 = (int) e->N1 - 1; // only 1 sign
12133             sc1 = -e->SF1;
12134             break;
12135 
12136         case CSNS:
12137             n1 = (int) e->N1;     // no sign
12138             sc1 = -e->SF1;
12139             break;  // no sign wysiwyg
12140     }
12141 
12142     if (n1 < 1)
12143         doFault (FAULT_IPR, fst_ill_proc, "mp2d adjusted n1<1");
12144 
12145     switch(e->S2)
12146     {
12147         case CSFL:
12148             n2 = (int) e->N2 - 1; // need to account for the sign
12149             if (e->TN2 == CTN4)
12150                 n2 -= 2;    // 2 4-bit digit exponent
12151             else
12152                 n2 -= 1;    // 1 9-bit digit exponent
12153             sc2 = 0;        // no scaling factor
12154             break;
12155 
12156         case CSLS:
12157         case CSTS:
12158             n2 = (int) e->N2 - 1; // 1 sign
12159             sc2 = -e->SF2;
12160             break;
12161 
12162         case CSNS:
12163             n2 = (int) e->N2;     // no sign
12164             sc2 = -e->SF2;
12165             break;  // no sign wysiwyg
12166     }
12167 
12168     if (n2 < 1)
12169         doFault (FAULT_IPR, fst_ill_proc, "mp2d adjusted n2<1");
12170 
12171     decContext set;
12172     decContextDefaultDPS8Mul(&set); // 126 digits for multiply
12173 
12174     set.traps=0;
12175 
12176     decNumber _1, _2, _3;
12177 
12178     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
12179 
12180     decNumber *op1 = decBCD9ToNumber(e->inBuffer, n1, sc1, &_1);
12181     if (e->sign == -1)
12182         op1->bits |= DECNEG;
12183     if (e->S1 == CSFL)
12184         op1->exponent = e->exponent;
12185 
12186     EISloadInputBufferNumeric (cpup, 2);   // according to MF2
12187 
12188     decNumber *op2 = decBCD9ToNumber(e->inBuffer, n2, sc2, &_2);
12189     if (e->sign == -1)
12190         op2->bits |= DECNEG;
12191     if (e->S2 == CSFL)
12192         op2->exponent = e->exponent;
12193 
12194     decNumber *op3 = decNumberMultiply(&_3, op1, op2, &set);
12195 
12196     bool Ovr = false, EOvr = false, Trunc = false;
12197 
12198     uint8_t out [256];
12199     char *res = formatDecimal(out, &set, op3, n2, (int) e->S2, e->SF2, R, &Ovr, &Trunc);
12200 
12201     if (decNumberIsZero(op3))
12202         op3->exponent = 127;
12203 
12204     // now write to memory in proper format.....
12205 
12206     //word18 dstAddr = e->dstAddr;
12207     int pos = (int) dstCN;
12208 
12209     // 1st, take care of any leading sign .......
12210     switch(e->S2)
12211     {
12212         case CSFL:  // floating-point, leading sign.
12213         case CSLS:  // fixed-point, leading sign
12214             switch(dstTN)
12215         {
12216             case CTN4:
12217                 if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
12218                           //   then the 13(8) plus sign character is placed appropriately
12219                           //   if the result of the operation is positive.
12220                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12221                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
12222                 else
12223                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12224                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
12225                 break;
12226             case CTN9:
12227                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12228                            (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
12229                 break;
12230         }
12231             break;
12232 
12233         case CSTS:  // nuttin' to do here .....
12234         case CSNS:
12235             break;  // no sign wysiwyg
12236     }
12237 
12238     // 2nd, write the digits .....
12239     for(int i = 0 ; i < n2 ; i++)
12240         switch(dstTN)
12241     {
12242         case CTN4:
12243             EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) (res[i] - '0'));
12244             break;
12245         case CTN9:
12246             EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) res[i]);
12247             break;
12248     }
12249 
12250     // 3rd, take care of any trailing sign or exponent ...
12251     switch(e->S2)
12252     {
12253         case CSTS:  // write trailing sign ....
12254             switch(dstTN)
12255         {
12256             case CTN4:
12257                 if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
12258                           //   then the 13(8) plus sign character is placed appropriately
12259                           //   if the result of the operation is positive.
12260                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12261                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
12262                 else
12263                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12264                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
12265                 break;
12266             case CTN9:
12267                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12268                            (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
12269                 break;
12270         }
12271             break;
12272 
12273         case CSFL:  // floating-point, leading sign.
12274             // write the exponent
12275             switch(dstTN)
12276             {
12277                 case CTN4:
12278                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (op3->exponent >> 4) & 0xf); // upper 4-bits
12279                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,  op3->exponent       & 0xf); // lower 4-bits
12280 
12281                     break;
12282                 case CTN9:
12283                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, op3->exponent & 0xff);    // write 8-bit exponent
12284                     break;
12285             }
12286             break;
12287 
12288         case CSLS:  // fixed point, leading sign - already done
12289         case CSNS:  // fixed point, unsigned - nuttin' needed to do
12290             break;
12291     }
12292 
12293     // set flags, etc ...
12294     if (e->S2 == CSFL)
12295     {
12296         if (op3->exponent > 127)
12297         {
12298             SET_I_EOFL;
12299             EOvr = true;
12300         }
12301         if (op3->exponent < -128)
12302         {
12303             SET_I_EUFL;
12304             EOvr = true;
12305         }
12306     }
12307 
12308     SC_I_NEG (decNumberIsNegative(op3) && !decNumberIsZero(op3));  // set negative indicator if op3 < 0
12309     SC_I_ZERO (decNumberIsZero(op3));     // set zero indicator if op3 == 0
12310 
12311     SC_I_TRUNC (!R && Trunc); // If the truncation condition exists without rounding, then ON; otherwise OFF
12312 
12313     cleanupOperandDescriptor (cpup, 1);
12314     cleanupOperandDescriptor (cpup, 2);
12315     cleanupOperandDescriptor (cpup, 3);
12316 
12317     if (TST_I_TRUNC && T && tstOVFfault (cpup))
12318       doFault(FAULT_OFL, fst_zero, "mp2d truncation(overflow) fault");
12319     if (EOvr && tstOVFfault (cpup))
12320         doFault(FAULT_OFL, fst_zero, "mp2d over/underflow fault");
12321     if (Ovr)
12322     {
12323         SET_I_OFLOW;
12324         if (tstOVFfault (cpup))
12325           doFault(FAULT_OFL, fst_zero, "mp2d overflow fault");
12326     }
12327 }
12328 
12329 /*
12330  * mp3d - Multiply Using Three Decimal Operands
12331  */
12332 
12333 void mp3d (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
12334 {
12335     EISstruct * e = & cpu.currentEISinstruction;
12336 
12337     fault_ipr_subtype_ mod_fault = 0;
12338 
12339 #if !defined(EIS_SETUP)
12340     setupOperandDescriptor(cpup, 1, &mod_fault);
12341     setupOperandDescriptor(cpup, 2, &mod_fault);
12342     setupOperandDescriptor(cpup, 3, &mod_fault);
12343 #endif
12344 
12345     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
12346     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
12347     parseNumericOperandDescriptor(cpup, 3, &mod_fault);
12348 
12349     L68_ (
12350       // L68 raises it immediately
12351       if (mod_fault)
12352         {
12353           doFault (FAULT_IPR,
12354                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
12355                    "Illegal modifier");
12356         }
12357     )
12358 
12359     // Bit 1 MBZ
12360     if (IWB_IRODD & 0200000000000)
12361       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "mp3d(): 1 MBZ");
12362 
12363     DPS8M_ (
12364       // DPS8M raises it delayed
12365       if (mod_fault)
12366         {
12367           doFault (FAULT_IPR,
12368                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
12369                    "Illegal modifier");
12370         }
12371     )
12372 
12373     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character control
12374     bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;  // truncation bit
12375     bool R = getbits36_1 (cpu.cu.IWB, 10) != 0;  // rounding bit
12376 
12377     PNL (L68_ (if (R)
12378       DU_CYCLE_FRND;))
12379 
12380     uint srcTN = e->TN1;    // type of chars in src
12381 
12382     uint dstTN = e->TN3;    // type of chars in dst
12383     uint dstCN = e->CN3;    // starting at char pos CN
12384 
12385     int n1 = 0, n2 = 0, n3 = 0, sc1 = 0, sc2 = 0;
12386 
12387     /*
12388      * Here we need to distinguish between 4 type of numbers.
12389      *
12390      * CSFL - Floating-point, leading sign
12391      * CSLS - Scaled fixed-point, leading sign
12392      * CSTS - Scaled fixed-point, trailing sign
12393      * CSNS - Scaled fixed-point, unsigned
12394      */
12395 
12396     // determine precision
12397     switch(e->S1)
12398     {
12399         case CSFL:
12400             n1 = (int) e->N1 - 1; // need to account for the - sign
12401             if (srcTN == CTN4)
12402                 n1 -= 2;    // 2 4-bit digits exponent
12403             else
12404                 n1 -= 1;    // 1 9-bit digit exponent
12405             sc1 = 0;        // no scaling factor
12406             break;
12407 
12408         case CSLS:
12409         case CSTS:
12410             n1 = (int) e->N1 - 1; // only 1 sign
12411             sc1 = -e->SF1;
12412             break;
12413 
12414         case CSNS:
12415             n1 = (int) e->N1;     // no sign
12416             sc1 = -e->SF1;
12417             break;  // no sign wysiwyg
12418     }
12419 
12420     if (n1 < 1)
12421         doFault (FAULT_IPR, fst_ill_proc, "mp3d adjusted n1<1");
12422 
12423     switch(e->S2)
12424     {
12425         case CSFL:
12426             n2 = (int) e->N2 - 1; // need to account for the sign
12427             if (e->TN2 == CTN4)
12428                 n2 -= 2;    // 2 4-bit digit exponent
12429             else
12430                 n2 -= 1;    // 1 9-bit digit exponent
12431             sc2 = 0;        // no scaling factor
12432             break;
12433 
12434         case CSLS:
12435         case CSTS:
12436             n2 = (int) e->N2 - 1; // 1 sign
12437             sc2 = -e->SF2;
12438             break;
12439 
12440         case CSNS:
12441             n2 = (int) e->N2;     // no sign
12442             sc2 = -e->SF2;
12443             break;  // no sign wysiwyg
12444     }
12445 
12446     if (n2 < 1)
12447         doFault (FAULT_IPR, fst_ill_proc, "mp3d adjusted n2<1");
12448 
12449     switch(e->S3)
12450     {
12451         case CSFL:
12452             n3 = (int) e->N3 - 1; // need to account for the sign
12453             if (dstTN == CTN4)
12454                 n3 -= 2;    // 2 4-bit digit exponent
12455             else
12456                 n3 -= 1;    // 1 9-bit digit exponent
12457             break;
12458 
12459         case CSLS:
12460         case CSTS:
12461             n3 = (int) e->N3 - 1; // 1 sign
12462             break;
12463 
12464         case CSNS:
12465             n3 = (int) e->N3;     // no sign
12466             break;  // no sign wysiwyg
12467     }
12468 
12469     if (n3 < 1)
12470         doFault (FAULT_IPR, fst_ill_proc, "mp3d adjusted n3<1");
12471 
12472     decContext set;
12473     //decContextDefault(&set, DEC_INIT_BASE);         // initialize
12474     decContextDefaultDPS8Mul(&set);     // 126 digits for multiply
12475 
12476     set.traps=0;
12477 
12478     decNumber _1, _2, _3;
12479 
12480     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
12481 
12482     decNumber *op1 = decBCD9ToNumber(e->inBuffer, n1, sc1, &_1);
12483     if (e->sign == -1)
12484         op1->bits |= DECNEG;
12485     if (e->S1 == CSFL)
12486         op1->exponent = e->exponent;
12487 
12488     EISloadInputBufferNumeric (cpup, 2);   // according to MF2
12489 
12490     decNumber *op2 = decBCD9ToNumber(e->inBuffer, n2, sc2, &_2);
12491     if (e->sign == -1)
12492         op2->bits |= DECNEG;
12493     if (e->S2 == CSFL)
12494         op2->exponent = e->exponent;
12495 
12496     decNumber *op3 = decNumberMultiply(&_3, op1, op2, &set);
12497 
12498 //    char c1[1024];
12499 //    char c2[1024];
12500 //    char c3[1024];
12501 //
12502 //    decNumberToString(op1, c1);
12503 //    sim_printf("c1:%s\r\n", c1);
12504 //    decNumberToString(op2, c2);
12505 //    sim_printf("c2:%s\r\n", c2);
12506 //    decNumberToString(op3, c3);
12507 //    sim_printf("c3:%s\r\n", c3);
12508 
12509     bool Ovr = false, EOvr = false, Trunc = false;
12510 
12511     uint8_t out [256];
12512     char *res = formatDecimal(out, &set, op3, n3, (int) e->S3, e->SF3, R, &Ovr, &Trunc);
12513 
12514     if (decNumberIsZero(op3))
12515         op3->exponent = 127;
12516 
12517     // now write to memory in proper format.....
12518 
12519     //word18 dstAddr = e->dstAddr;
12520     int pos = (int) dstCN;
12521 
12522     // 1st, take care of any leading sign .......
12523     switch(e->S3)
12524     {
12525         case CSFL:  // floating-point, leading sign.
12526         case CSLS:  // fixed-point, leading sign
12527             switch(dstTN)
12528             {
12529             case CTN4:
12530               if (e->P) // If TN2 and S2 specify a 4-bit signed number and P
12531                           // = 1, then the 13(8) plus sign character is placed
12532                           // appropriately if the result of the operation is
12533                           // positive.
12534                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12535                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
12536                 else
12537                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12538                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
12539                 break;
12540             case CTN9:
12541                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12542                            (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
12543                 break;
12544             }
12545             break;
12546 
12547         case CSTS:  // nuttin' to do here .....
12548         case CSNS:
12549             break;  // no sign wysiwyg
12550     }
12551 
12552     // 2nd, write the digits .....
12553     for(int i = 0 ; i < n3 ; i++)
12554         switch(dstTN)
12555         {
12556             case CTN4:
12557                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) (res[i] - '0'));
12558                 break;
12559             case CTN9:
12560                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) res[i]);
12561                 break;
12562         }
12563 
12564         // 3rd, take care of any trailing sign or exponent ...
12565     switch(e->S3)
12566     {
12567         case CSTS:  // write trailing sign ....
12568             switch(dstTN)
12569             {
12570                 case CTN4:
12571                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
12572                               //  then the 13(8) plus sign character is placed appropriately
12573                               //  if the result of the operation is positive.
12574                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12575                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
12576                     else
12577                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12578                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
12579                     break;
12580                 case CTN9:
12581                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
12582                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
12583                     break;
12584             }
12585             break;
12586 
12587         case CSFL:  // floating-point, leading sign.
12588             // write the exponent
12589             switch(dstTN)
12590             {
12591                 case CTN4:
12592                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (op3->exponent >> 4) & 0xf); // upper 4-bits
12593                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,  op3->exponent       & 0xf); // lower 4-bits
12594                     break;
12595                 case CTN9:
12596                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, op3->exponent & 0xff);    // write 8-bit exponent
12597                     break;
12598             }
12599             break;
12600 
12601         case CSLS:  // fixed point, leading sign - already done
12602         case CSNS:  // fixed point, unsigned - nuttin' needed to do
12603             break;
12604     }
12605 
12606     // set flags, etc ...
12607     if (e->S3 == CSFL)
12608     {
12609         if (op3->exponent > 127)
12610         {
12611             SET_I_EOFL;
12612             EOvr = true;
12613         }
12614         if (op3->exponent < -128)
12615         {
12616             SET_I_EUFL;
12617             EOvr = true;
12618         }
12619     }
12620 
12621     SC_I_NEG (decNumberIsNegative(op3) && !decNumberIsZero(op3));  // set negative indicator if op3 < 0
12622     SC_I_ZERO (decNumberIsZero(op3));     // set zero indicator if op3 == 0
12623 
12624     SC_I_TRUNC (!R && Trunc); // If the truncation condition exists without rounding, then ON; otherwise OFF
12625 
12626     cleanupOperandDescriptor (cpup, 1);
12627     cleanupOperandDescriptor (cpup, 2);
12628     cleanupOperandDescriptor (cpup, 3);
12629 
12630     if (TST_I_TRUNC && T && tstOVFfault (cpup))
12631       doFault(FAULT_OFL, fst_zero, "mp3d truncation(overflow) fault");
12632     if (EOvr && tstOVFfault (cpup))
12633         doFault(FAULT_OFL, fst_zero, "mp3d over/underflow fault");
12634     if (Ovr)
12635     {
12636         SET_I_OFLOW;
12637         if (tstOVFfault (cpup))
12638           doFault(FAULT_OFL, fst_zero, "mp3d overflow fault");
12639     }
12640 }
12641 
12642 
12643 
12644 
12645 
12646 
12647 
12648 
12649 
12650 
12651 
12652 
12653 
12654 
12655 
12656 
12657 
12658 
12659 
12660 
12661 
12662 
12663 
12664 
12665 
12666 
12667 
12668 
12669 
12670 
12671 
12672 
12673 
12674 
12675 
12676 
12677 
12678 
12679 
12680 
12681 
12682 
12683 
12684 
12685 
12686 
12687 
12688 
12689 
12690 
12691 
12692 
12693 
12694 
12695 
12696 
12697 
12698 
12699 
12700 
12701 
12702 
12703 
12704 
12705 
12706 
12707 
12708 
12709 
12710 
12711 
12712 
12713 
12714 
12715 
12716 
12717 
12718 
12719 
12720 
12721 
12722 
12723 
12724 
12725 
12726 
12727 
12728 
12729 
12730 
12731 
12732 
12733 
12734 
12735 
12736 
12737 
12738 
12739 
12740 
12741 
12742 
12743 
12744 
12745 
12746 
12747 
12748 
12749 
12750 
12751 
12752 
12753 
12754 
12755 
12756 
12757 
12758 
12759 
12760 
12761 
12762 
12763 
12764 
12765 
12766 
12767 
12768 
12769 
12770 
12771 
12772 
12773 
12774 
12775 
12776 
12777 
12778 
12779 
12780 
12781 
12782 
12783 
12784 
12785 
12786 
12787 
12788 
12789 
12790 
12791 
12792 
12793 
12794 
12795 
12796 
12797 
12798 
12799 
12800 
12801 
12802 
12803 
12804 
12805 
12806 
12807 
12808 
12809 
12810 
12811 
12812 
12813 
12814 
12815 
12816 
12817 
12818 
12819 
12820 
12821 
12822 
12823 
12824 
12825 
12826 
12827 
12828 
12829 
12830 
12831 
12832 
12833 
12834 
12835 
12836 
12837 
12838 
12839 
12840 
12841 
12842 
12843 
12844 
12845 
12846 
12847 
12848 
12849 
12850 
12851 
12852 
12853 
12854 
12855 
12856 
12857 
12858 
12859 
12860 
12861 
12862 
12863 
12864 
12865 
12866 
12867 
12868 
12869 
12870 
12871 
12872 
12873 
12874 
12875 
12876 
12877 
12878 
12879 
12880 
12881 
12882 
12883 
12884 
12885 
12886 
12887 
12888 
12889 
12890 
12891 
12892 
12893 
12894 
12895 
12896 
12897 
12898 
12899 
12900 
12901 
12902 
12903 
12904 
12905 
12906 
12907 
12908 
12909 
12910 
12911 
12912 
12913 
12914 
12915 
12916 
12917 
12918 
12919 
12920 
12921 
12922 
12923 
12924 
12925 
12926 
12927 
12928 
12929 
12930 
12931 
12932 
12933 
12934 
12935 
12936 
12937 
12938 
12939 
12940 
12941 
12942 
12943 
12944 
12945 
12946 
12947 
12948 
12949 
12950 
12951 
12952 
12953 
12954 
12955 
12956 
12957 
12958 
12959 
12960 
12961 
12962 
12963 
12964 
12965 
12966 
12967 
12968 
12969 
12970 
12971 
12972 
12973 
12974 
12975 
12976 
12977 
12978 
12979 
12980 
12981 
12982 
12983 
12984 
12985 
12986 
12987 
12988 
12989 
12990 
12991 
12992 
12993 
12994 
12995 
12996 
12997 
12998 
12999 
13000 
13001 
13002 
13003 
13004 
13005 
13006 
13007 
13008 
13009 
13010 
13011 
13012 
13013 
13014 
13015 
13016 
13017 
13018 
13019 
13020 
13021 
13022 
13023 
13024 
13025 
13026 
13027 
13028 
13029 
13030 
13031 
13032 
13033 
13034 
13035 
13036 
13037 
13038 
13039 
13040 
13041 
13042 
13043 
13044 
13045 
13046 
13047 
13048 
13049 
13050 
13051 
13052 
13053 
13054 
13055 
13056 
13057 
13058 
13059 
13060 
13061 
13062 
13063 
13064 
13065 
13066 
13067 
13068 
13069 
13070 
13071 
13072 
13073 
13074 
13075 
13076 
13077 
13078 
13079 
13080 
13081 
13082 
13083 
13084 
13085 
13086 
13087 
13088 
13089 
13090 
13091 
13092 
13093 
13094 
13095 
13096 
13097 
13098 
13099 
13100 
13101 
13102 
13103 
13104 
13105 
13106 
13107 
13108 
13109 
13110 
13111 
13112 
13113 
13114 
13115 
13116 
13117 
13118 
13119 
13120 
13121 
13122 
13123 
13124 
13125 
13126 
13127 
13128 
13129 
13130 
13131 
13132 
13133 
13134 
13135 
13136 
13137 
13138 
13139 
13140 
13141 
13142 
13143 
13144 
13145 
13146 
13147 
13148 
13149 
13150 
13151 
13152 
13153 
13154 
13155 
13156 
13157 
13158 
13159 
13160 
13161 
13162 
13163 
13164 
13165 
13166 
13167 
13168 
13169 
13170 
13171 
13172 
13173 
13174 
13175 
13176 
13177 
13178 
13179 
13180 
13181 
13182 
13183 
13184 
13185 
13186 
13187 
13188 
13189 
13190 
13191 
13192 
13193 
13194 
13195 
13196 
13197 
13198 
13199 
13200 
13201 
13202 
13203 
13204 
13205 
13206 
13207 
13208 
13209 
13210 
13211 
13212 
13213 
13214 
13215 
13216 
13217 
13218 
13219 
13220 
13221 
13222 
13223 
13224 
13225 
13226 
13227 
13228 
13229 
13230 
13231 
13232 
13233 
13234 
13235 
13236 
13237 
13238 
13239 
13240 
13241 
13242 
13243 
13244 
13245 
13246 
13247 
13248 
13249 
13250 
13251 
13252 
13253 
13254 
13255 
13256 
13257 
13258 
13259 
13260 
13261 
13262 
13263 
13264 
13265 
13266 
13267 
13268 
13269 
13270 
13271 
13272 
13273 
13274 
13275 
13276 
13277 
13278 
13279 
13280 
13281 
13282 
13283 
13284 
13285 
13286 
13287 
13288 
13289 
13290 
13291 
13292 
13293 
13294 
13295 
13296 
13297 
13298 
13299 
13300 
13301 
13302 
13303 
13304 
13305 
13306 
13307 
13308 
13309 
13310 
13311 
13312 
13313 
13314 
13315 
13316 
13317 
13318 
13319 
13320 
13321 
13322 
13323 
13324 
13325 
13326 
13327 
13328 
13329 
13330 
13331 
13332 
13333 
13334 
13335 
13336 
13337 
13338 
13339 
13340 
13341 
13342 
13343 
13344 
13345 
13346 
13347 
13348 
13349 
13350 
13351 
13352 
13353 
13354 
13355 
13356 
13357 
13358 
13359 
13360 
13361 
13362 
13363 
13364 
13365 
13366 
13367 
13368 
13369 
13370 
13371 
13372 
13373 
13374 
13375 
13376 
13377 
13378 
13379 
13380 
13381 
13382 
13383 
13384 
13385 
13386 
13387 
13388 
13389 
13390 
13391 /*
13392  * dv2d - Divide Using Two Decimal Operands
13393  */
13394 
13395 void dv2d (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
13396 {
13397     EISstruct * e = & cpu.currentEISinstruction;
13398 
13399     fault_ipr_subtype_ mod_fault = 0;
13400 
13401 #if !defined(EIS_SETUP)
13402     setupOperandDescriptor(cpup, 1, &mod_fault);
13403     setupOperandDescriptor(cpup, 2, &mod_fault);
13404     setupOperandDescriptorCache(cpup, 3);
13405 #endif
13406 
13407     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
13408     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
13409 
13410     L68_ (
13411       // L68 raises it immediately
13412       if (mod_fault)
13413         {
13414           doFault (FAULT_IPR,
13415                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
13416                    "Illegal modifier");
13417         }
13418     )
13419 
13420     // Bits 1-8 MBZ
13421     // ISOLTS test 840 and RJ78 says bit 9 (T) MBZ as well
13422     if (IWB_IRODD & 0377400000000)
13423       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "dv2d 1-9 MBZ");
13424 
13425     DPS8M_ (
13426       // DPS8M raises it delayed
13427       if (mod_fault)
13428         {
13429           doFault (FAULT_IPR,
13430                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
13431                    "Illegal modifier");
13432         }
13433     )
13434 
13435     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character control
13436     //bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;  // truncation bit
13437     bool R = getbits36_1 (cpu.cu.IWB, 10) != 0;  // rounding bit
13438 
13439     PNL (L68_ (if (R)
13440       DU_CYCLE_FRND;))
13441 
13442     uint srcTN = e->TN1;    // type of chars in src
13443 
13444     uint dstTN = e->TN2;    // type of chars in dst
13445     uint dstCN = e->CN2;    // starting at char pos CN
13446 
13447     e->ADDR3 = e->ADDR2;
13448 
13449     int n1 = 0, n2 = 0, sc1 = 0, sc2 = 0;
13450 
13451     /*
13452      * Here we need to distinguish between 4 type of numbers.
13453      *
13454      * CSFL - Floating-point, leading sign
13455      * CSLS - Scaled fixed-point, leading sign
13456      * CSTS - Scaled fixed-point, trailing sign
13457      * CSNS - Scaled fixed-point, unsigned
13458      */
13459 
13460     // determine precision
13461     switch(e->S1)
13462     {
13463         case CSFL:
13464             n1 = (int) e->N1 - 1; // need to account for the - sign
13465             if (srcTN == CTN4)
13466                 n1 -= 2;    // 2 4-bit digits exponent
13467             else
13468                 n1 -= 1;    // 1 9-bit digit exponent
13469             sc1 = 0;        // no scaling factor
13470             break;
13471 
13472         case CSLS:
13473         case CSTS:
13474             n1 = (int) e->N1 - 1; // only 1 sign
13475             sc1 = -e->SF1;
13476             break;
13477 
13478         case CSNS:
13479             n1 = (int) e->N1;     // no sign
13480             sc1 = -e->SF1;
13481             break;  // no sign wysiwyg
13482     }
13483 
13484     if (n1 < 1)
13485         doFault (FAULT_IPR, fst_ill_proc, "dv2d adjusted n1<1");
13486 
13487     switch(e->S2)
13488     {
13489         case CSFL:
13490             n2 = (int) e->N2 - 1; // need to account for the sign
13491             if (e->TN2 == CTN4)
13492                 n2 -= 2;    // 2 4-bit digit exponent
13493             else
13494                 n2 -= 1;    // 1 9-bit digit exponent
13495             sc2 = 0;        // no scaling factor
13496             break;
13497 
13498         case CSLS:
13499         case CSTS:
13500             n2 = (int) e->N2 - 1; // 1 sign
13501             sc2 = -e->SF2;
13502             break;
13503 
13504         case CSNS:
13505             n2 = (int) e->N2;     // no sign
13506             sc2 = -e->SF2;
13507             break;  // no sign wysiwyg
13508     }
13509 
13510     if (n2 < 1)
13511         doFault (FAULT_IPR, fst_ill_proc, "dv2d adjusted n2<1");
13512 
13513     decContext set;
13514     decContextDefaultDPS8(&set);
13515 
13516     set.traps=0;
13517 
13518     decNumber _1, _2, _3;
13519 
13520     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
13521 
13522     decNumber *op1 = decBCD9ToNumber(e->inBuffer, n1, sc1, &_1);    // divisor
13523     if (e->sign == -1)
13524         op1->bits |= DECNEG;
13525     if (e->S1 == CSFL)
13526         op1->exponent = e->exponent;
13527 
13528     // check for divide by 0!
13529     if (decNumberIsZero(op1))
13530     {
13531         doFault(FAULT_DIV, fst_zero, "dv2d division by 0");
13532     }
13533 
13534     word9   inBufferop1 [64];
13535     memcpy (inBufferop1,e->inBuffer,sizeof(inBufferop1)); // save for clz1 calculation later
13536 
13537     EISloadInputBufferNumeric (cpup, 2);   // according to MF2
13538 
13539     decNumber *op2 = decBCD9ToNumber(e->inBuffer, n2, sc2, &_2);    // dividend
13540     if (e->sign == -1)
13541         op2->bits |= DECNEG;
13542     if (e->S2 == CSFL)
13543         op2->exponent = e->exponent;
13544 
13545     int NQ;
13546     if (e->S2 == CSFL)
13547     {
13548         NQ = n2;
13549     }
13550     else
13551     {
13552         // count leading zeroes
13553         // TODO optimize - can these be somehow extracted from decNumbers?
13554         int clz1, clz2, i;
13555         for (i=0; i < op1->digits; i++)
13556             if (inBufferop1[i]!=0)
13557                 break;
13558         clz1 = i;
13559         for (i=0; i < op2->digits; i++)
13560             if (e->inBuffer[i]!=0) // this still holds op2 digits
13561                 break;
13562         clz2 = i;
13563         sim_debug (DBG_TRACEEXT, & cpu_dev, "dv2d: clz1 %d clz2 %d\r\n",clz1,clz2);
13564 
13565         // XXX are clz also valid for CSFL dividend / divisor? probably yes
13566         // XXX seems that exponents and scale factors are used interchangeably here ? (RJ78)
13567         NQ = (n2-clz2+1) - (n1-clz1) + (-(e->S1==CSFL?op1->exponent:(int)e->SF1));
13568 
13569 sim_debug (DBG_TRACEEXT, & cpu_dev, "dv2d S1 %d S2 %d N1 %d N2 %d clz1 %d clz2 %d E1 %d E2 %d SF2 %d NQ %d\r\n",
13570            e->S1,e->S2,e->N1,e->N2,clz1,clz2,op1->exponent,op2->exponent,e->SF2,NQ);
13571     }
13572     if (NQ > 63)
13573         doFault(FAULT_DIV, fst_zero, "dv2d NQ>63");
13574     // Note: NQ is currently unused apart from this FAULT_DIV check.
13575     // decNumber produces more digits than required, but they are then rounded/truncated
13576 
13577     // Yes, they're switched. op1=divisor
13578     decNumber *op3 = decNumberDivide(&_3, op2, op1, &set);
13579     // Note DPS88 and DPS9000 are different when NQ <= 0
13580     // This is a flaw in the DPS8/70 hardware which was corrected in later models
13581     // ISOLTS-817 05b
13582 
13583     PRINTDEC("op2", op2);
13584     PRINTDEC("op1", op1);
13585     PRINTDEC("op3", op3);
13586 
13587     // let's check division results to see for anomalous conditions
13588     if (
13589         (set.status & DEC_Division_undefined) ||    // 0/0 will become NaN
13590         (set.status & DEC_Invalid_operation) ||
13591         (set.status & DEC_Division_by_zero)
13592         ) { sim_debug (DBG_TRACEEXT, & cpu_dev, "oops! dv2d anomalous results"); } // divide by zero has already been checked before
13593 
13594     if (e->S2 == CSFL)
13595     {
13596         // justify CSFL left
13597         // This is a flaw in the DPS8/70 hardware which was corrected in later models
13598         // Note DPS88 and DPS9000 are different
13599         // ISOLTS-817 06c,06e
13600 
13601         decNumber _sf;
13602 
13603         if (n2 - op3->digits > 0)
13604         {
13605             decNumberFromInt32(&_sf, op3->exponent - (n2 - op3->digits));
13606             PRINTDEC("Value 1", op3)
13607             PRINTDEC("Value sf", &_sf)
13608             op3 = decNumberRescale(op3, op3, &_sf, &set);
13609             PRINTDEC("Value 2", op3)
13610         }
13611     }
13612 
13613     bool Ovr = false, EOvr = false, Trunc = false;
13614     uint8_t out[256];
13615 
13616     // CSFL: If the divisor is greater than the dividend after operand
13617     // alignment, the leading zero digit produced is counted and the effective
13618     // precision of the result is reduced by one.
13619     // This is a flaw in the DPS8/70 hardware which was corrected in later models
13620     // Note DPS88 and DPS9000 are different
13621     //
13622     // "greater after operand alignment" means scale until most-significant digits
13623     //   are nonzero, then compare magnitudes ignoring exponents
13624     // This passes ISOLTS-817 06e, ET 458,461,483,486
13625     char *res;
13626     if (e->S2 == CSFL) {
13627         decNumber _1a;
13628         decNumber _2a;
13629         decNumber _sf;
13630         if (op1->digits >= op2->digits) {
13631             // scale op2
13632             decNumberCopy(&_1a, op1);
13633             decNumberFromInt32(&_sf, op1->digits - op2->digits);
13634             decNumberShift(&_2a, op2, &_sf, &set);
13635         } else if (op1->digits < op2->digits) {
13636             // scale op1
13637             decNumberFromInt32(&_sf, op2->digits - op1->digits);
13638             decNumberShift(&_1a, op1, &_sf, &set);
13639             decNumberCopy(&_2a, op2);
13640         }
13641         _1a.exponent = 0;
13642         _2a.exponent = 0;
13643 
13644         PRINTDEC("dv2d: op1a", &_1a);
13645         PRINTDEC("dv2d: op2a", &_2a);
13646         sim_debug (DBG_TRACEEXT, & cpu_dev, "dv2d: exp1 %d exp2 %d digits op1 %d op2 %d op1a %d op2a %d\r\n",
13647                    op1->exponent,op2->exponent,op1->digits,op2->digits,_1a.digits,_2a.digits);
13648 
13649         if (decCompareMAG(&_1a, &_2a, &set) > 0) {
13650             // shorten the result field to get proper rounding
13651             res = formatDecimal(out, &set, op3, n2 -1, (int) e->S2, e->SF2, R, &Ovr, &Trunc);
13652 
13653             // prepend zero digit
13654             // ET 458,483 float=float/float, ET 461,486 float=fixed/fixed
13655             for (int i = n2; i > 0; i--) // incl.zero terminator
13656                  res[i] = res[i-1];
13657             res[0] = '0';
13658             sim_debug (DBG_TRACEEXT, & cpu_dev, "dv2d: addzero n2 %d %s exp %d\r\n",n2,res,op3->exponent);
13659         } else {
13660             // full n2 digits are returned
13661             res = formatDecimal(out, &set, op3, n2, (int) e->S2, e->SF2, R, &Ovr, &Trunc);
13662         }
13663     } else {
13664         // same as all other decimal instructions
13665         res = formatDecimal(out, &set, op3, n2, (int) e->S2, e->SF2, R, &Ovr, &Trunc);
13666     }
13667 
13668     if (decNumberIsZero(op3))
13669         op3->exponent = 127;
13670 
13671     // now write to memory in proper format.....
13672 
13673     int pos = (int) dstCN;
13674 
13675     // 1st, take care of any leading sign .......
13676     switch(e->S2)
13677     {
13678         case CSFL:  // floating-point, leading sign.
13679         case CSLS:  // fixed-point, leading sign
13680             switch(dstTN)
13681             {
13682                 case CTN4:
13683                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
13684                               //   then the 13(8) plus sign character is placed appropriately
13685                               //   if the result of the operation is positive.
13686                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
13687                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
13688                     else
13689                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
13690                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
13691                     break;
13692                 case CTN9:
13693                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
13694                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
13695                     break;
13696             }
13697             break;
13698 
13699         case CSTS:  // nuttin' to do here .....
13700         case CSNS:
13701             break;  // no sign wysiwyg
13702     }
13703 
13704     // 2nd, write the digits .....
13705     for(int i = 0 ; i < n2 ; i++)
13706         switch(dstTN)
13707         {
13708             case CTN4:
13709                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) (res[i] - '0'));
13710                 break;
13711             case CTN9:
13712                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) res[i]);
13713                 break;
13714         }
13715 
13716     // 3rd, take care of any trailing sign or exponent ...
13717     switch(e->S2)
13718     {
13719         case CSTS:  // write trailing sign ....
13720             switch(dstTN)
13721             {
13722                 case CTN4:
13723                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
13724                               //   then the 13(8) plus sign character is placed appropriately
13725                               //   if the result of the operation is positive.
13726                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
13727                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
13728                     else
13729                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
13730                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
13731                     break;
13732                 case CTN9:
13733                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
13734                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
13735                     break;
13736             }
13737             break;
13738 
13739         case CSFL:  // floating-point, leading sign.
13740             // write the exponent
13741             switch(dstTN)
13742             {
13743                 case CTN4:
13744                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (op3->exponent >> 4) & 0xf); // upper 4-bits
13745                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,  op3->exponent       & 0xf); // lower 4-bits
13746                     break;
13747                 case CTN9:
13748                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, op3->exponent & 0xff);    // write 8-bit exponent
13749                     break;
13750             }
13751             break;
13752 
13753         case CSLS:  // fixed point, leading sign - already done
13754         case CSNS:  // fixed point, unsigned - nuttin' needed to do
13755             break;
13756     }
13757 
13758     // set flags, etc ...
13759     if (e->S2 == CSFL)
13760     {
13761         if (op3->exponent > 127)
13762         {
13763             SET_I_EOFL;
13764             EOvr = true;
13765         }
13766         if (op3->exponent < -128)
13767         {
13768             SET_I_EUFL;
13769             EOvr = true;
13770         }
13771     }
13772 
13773     SC_I_NEG (decNumberIsNegative(op3) && !decNumberIsZero(op3));  // set negative indicator if op3 < 0
13774     SC_I_ZERO (decNumberIsZero(op3));     // set zero indicator if op3 == 0
13775 
13776     //SC_I_TRUNC (!R && Trunc); // no truncation flag for divide
13777 
13778     cleanupOperandDescriptor (cpup, 1);
13779     cleanupOperandDescriptor (cpup, 2);
13780     cleanupOperandDescriptor (cpup, 3);
13781 
13782     //if (TST_I_TRUNC && T && tstOVFfault (cpup))
13783     //  doFault(FAULT_OFL, fst_zero, "dv2d truncation(overflow) fault");
13784     if (EOvr && tstOVFfault (cpup))
13785         doFault(FAULT_OFL, fst_zero, "dv2d over/underflow fault");
13786     if (Ovr)
13787     {
13788         SET_I_OFLOW;
13789         if (tstOVFfault (cpup))
13790           doFault(FAULT_OFL, fst_zero, "dv2d overflow fault");
13791     }
13792 }
13793 
13794 /*
13795  * dv3d - Divide Using Three Decimal Operands
13796  */
13797 
13798 void dv3d (cpu_state_t * cpup)
     /* [previous][next][first][last][top][bottom][index][help] */
13799 
13800 {
13801     EISstruct * e = & cpu.currentEISinstruction;
13802 
13803     fault_ipr_subtype_ mod_fault = 0;
13804 
13805 #if !defined(EIS_SETUP)
13806     setupOperandDescriptor(cpup, 1, &mod_fault);
13807     setupOperandDescriptor(cpup, 2, &mod_fault);
13808     setupOperandDescriptor(cpup, 3, &mod_fault);
13809 #endif
13810 
13811     parseNumericOperandDescriptor(cpup, 1, &mod_fault);
13812     parseNumericOperandDescriptor(cpup, 2, &mod_fault);
13813     parseNumericOperandDescriptor(cpup, 3, &mod_fault);
13814 
13815     L68_ (
13816       // L68 raises it immediately
13817       if (mod_fault)
13818         {
13819           doFault (FAULT_IPR,
13820                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
13821                    "Illegal modifier");
13822         }
13823     )
13824 
13825     // Bit 1 MBZ
13826     // ISOLTS test 840 and RJ78 says bit 9 (T) MBZ
13827     if (IWB_IRODD & 0200400000000)
13828       doFault (FAULT_IPR, (_fault_subtype) {.fault_ipr_subtype=FR_ILL_OP|mod_fault}, "dv3d(): 1,9 MBZ");
13829 
13830     DPS8M_ (
13831       // DPS8M raises it delayed
13832       if (mod_fault)
13833         {
13834           doFault (FAULT_IPR,
13835                    (_fault_subtype) {.fault_ipr_subtype=mod_fault},
13836                    "Illegal modifier");
13837         }
13838     )
13839 
13840     e->P = getbits36_1 (cpu.cu.IWB, 0) != 0;  // 4-bit data sign character control
13841     //bool T = getbits36_1 (cpu.cu.IWB, 9) != 0;  // truncation bit
13842     bool R = getbits36_1 (cpu.cu.IWB, 10) != 0;  // rounding bit
13843 
13844     PNL (L68_ (if (R)
13845       DU_CYCLE_FRND;))
13846 
13847     uint srcTN = e->TN1;    // type of chars in src
13848 
13849     uint dstTN = e->TN3;    // type of chars in dst
13850     uint dstCN = e->CN3;    // starting at char pos CN
13851 
13852     int n1 = 0, n2 = 0, n3 = 0, sc1 = 0, sc2 = 0;
13853 
13854     /*
13855      * Here we need to distinguish between 4 type of numbers.
13856      *
13857      * CSFL - Floating-point, leading sign
13858      * CSLS - Scaled fixed-point, leading sign
13859      * CSTS - Scaled fixed-point, trailing sign
13860      * CSNS - Scaled fixed-point, unsigned
13861      */
13862 
13863     // determine precision
13864     switch(e->S1)
13865     {
13866         case CSFL:
13867             n1 = (int) e->N1 - 1; // need to account for the - sign
13868             if (srcTN == CTN4)
13869                 n1 -= 2;    // 2 4-bit digits exponent
13870             else
13871                 n1 -= 1;    // 1 9-bit digit exponent
13872             sc1 = 0;        // no scaling factor
13873             break;
13874 
13875         case CSLS:
13876         case CSTS:
13877             n1 = (int) e->N1 - 1; // only 1 sign
13878             sc1 = -e->SF1;
13879             break;
13880 
13881         case CSNS:
13882             n1 = (int) e->N1;     // no sign
13883             sc1 = -e->SF1;
13884             break;  // no sign wysiwyg
13885     }
13886 
13887     if (n1 < 1)
13888         doFault (FAULT_IPR, fst_ill_proc, "dv3d adjusted n1<1");
13889 
13890     switch(e->S2)
13891     {
13892         case CSFL:
13893             n2 = (int) e->N2 - 1; // need to account for the sign
13894             if (e->TN2 == CTN4)
13895                 n2 -= 2;    // 2 4-bit digit exponent
13896             else
13897                 n2 -= 1;    // 1 9-bit digit exponent
13898             sc2 = 0;        // no scaling factor
13899             break;
13900 
13901         case CSLS:
13902         case CSTS:
13903             n2 = (int) e->N2 - 1; // 1 sign
13904             sc2 = -e->SF2;
13905             break;
13906 
13907         case CSNS:
13908             n2 = (int) e->N2;     // no sign
13909             sc2 = -e->SF2;
13910             break;  // no sign wysiwyg
13911     }
13912 
13913     if (n2 < 1)
13914         doFault (FAULT_IPR, fst_ill_proc, "dv3d adjusted n2<1");
13915 
13916     switch(e->S3)
13917     {
13918         case CSFL:
13919             n3 = (int) e->N3 - 1; // need to account for the sign
13920             if (dstTN == CTN4)
13921                 n3 -= 2;    // 2 4-bit digit exponent
13922             else
13923                 n3 -= 1;    // 1 9-bit digit exponent
13924             break;
13925 
13926         case CSLS:
13927         case CSTS:
13928             n3 = (int) e->N3 - 1; // 1 sign
13929             break;
13930 
13931         case CSNS:
13932             n3 = (int) e->N3;     // no sign
13933             break;  // no sign wysiwyg
13934     }
13935     if (n3 < 1)
13936         doFault (FAULT_IPR, fst_ill_proc, "dv3d adjusted n3<1");
13937 
13938     decContext set;
13939     decContextDefaultDPS8(&set);
13940 
13941     set.traps=0;
13942 
13943     decNumber _1, _2, _3;
13944 
13945     EISloadInputBufferNumeric (cpup, 1);   // according to MF1
13946 
13947     decNumber *op1 = decBCD9ToNumber(e->inBuffer, n1, sc1, &_1);
13948     //PRINTDEC("op1", op1);
13949     if (e->sign == -1)
13950         op1->bits |= DECNEG;
13951     if (e->S1 == CSFL)
13952         op1->exponent = e->exponent;
13953 
13954     // check for divide by 0!
13955     if (decNumberIsZero(op1))
13956     {
13957         doFault(FAULT_DIV, fst_zero, "dv3d division by 0");
13958     }
13959 
13960     word9   inBufferop1 [64];
13961     memcpy (inBufferop1,e->inBuffer,sizeof(inBufferop1)); // save for clz1 calculation later
13962 
13963     EISloadInputBufferNumeric (cpup, 2);   // according to MF2
13964 
13965     decNumber *op2 = decBCD9ToNumber(e->inBuffer, n2, sc2, &_2);
13966     if (e->sign == -1)
13967         op2->bits |= DECNEG;
13968     if (e->S2 == CSFL)
13969         op2->exponent = e->exponent;
13970 
13971     // The number of required quotient digits, NQ, is determined before
13972     // division begins as follows:
13973     //  1) Floating-point quotient
13974     //      NQ = N3
13975     //  2) Fixed-point quotient
13976     //    NQ = (N2-LZ2+1) - (N1-LZ1) + (E2-E1-SF3)
13977     //     where: Nn = given operand field length
13978     //        LZn = leading zero count for operand n
13979     //        En = exponent of operand n
13980     //        SF3 = scaling factor of quotient
13981     // 3) Rounding
13982     //    If rounding is specified (R = 1), then one extra quotient digit is
13983     //    produced.
13984     // Note: rule 3 is already handled by formatDecimal rounding
13985     // Nn doesn't represent full field length, but length without sign and exponent (RJ78/DH03 seems like)
13986 
13987     int NQ;
13988     if (e->S3 == CSFL)
13989     {
13990         NQ = n3;
13991     }
13992     else
13993     {
13994         // count leading zeroes
13995         // TODO optimize - can these be somehow extracted from decNumbers?
13996         int clz1, clz2, i;
13997         for (i=0; i < op1->digits; i++)
13998             if (inBufferop1[i]!=0)
13999                 break;
14000         clz1 = i;
14001         for (i=0; i < op2->digits; i++)
14002             if (e->inBuffer[i]!=0) // this still holds op2 digits
14003                 break;
14004         clz2 = i;
14005         sim_debug (DBG_TRACEEXT, & cpu_dev, "dv3d: clz1 %d clz2 %d\r\n",clz1,clz2);
14006 
14007         // XXX are clz also valid for CSFL dividend / divisor? probably yes
14008         // XXX seems that exponents and scale factors are used interchangeably here ? (RJ78)
14009         NQ = (n2-clz2+1) - (n1-clz1) + \
14010                 ((e->S2==CSFL?op2->exponent:(int)e->SF2)-(e->S1==CSFL?op1->exponent:(int)e->SF1)-(int)e->SF3);
14011 
14012 sim_debug (DBG_TRACEEXT, & cpu_dev, "dv3d S1 %d S2 %d N1 %d N2 %d clz1 %d clz2 %d E1 %d E2 %d SF3 %d NQ %d\r\n",
14013            e->S1,e->S2,e->N1,e->N2,clz1,clz2,op1->exponent,op2->exponent,e->SF3,NQ);
14014     }
14015     if (NQ > 63)
14016         doFault(FAULT_DIV, fst_zero, "dv3d NQ>63");
14017     // Note: NQ is currently unused apart from this FAULT_DIV check.
14018     //   decNumber produces more digits than required, but they are then rounded/truncated
14019 
14020     // Yes, they're switched. op1=divisor
14021     decNumber *op3 = decNumberDivide(&_3, op2, op1, &set);
14022     // Note DPS88 and DPS9000 are different when NQ <= 0
14023     // This is a flaw in the DPS8/70 hardware which was corrected in later models
14024     // ISOLTS-817 05b
14025 
14026     PRINTDEC("op2", op2);
14027     PRINTDEC("op1", op1);
14028     PRINTDEC("op3", op3);
14029 
14030     // let's check division results to see for anomalous conditions
14031     if (
14032         (set.status & DEC_Division_undefined) ||    // 0/0 will become NaN
14033         (set.status & DEC_Invalid_operation) ||
14034         (set.status & DEC_Division_by_zero)
14035         ) { sim_debug (DBG_TRACEEXT, & cpu_dev, "oops! dv3d anomalous results"); } // divide by zero has already been checked before
14036 
14037     if (e->S3 == CSFL)
14038     {
14039         // justify CSFL left
14040         // This is a flaw in the DPS8/70 hardware which was corrected in later models
14041         // Note DPS88 and DPS9000 are different
14042         // ISOLTS-817 06c,06e
14043 
14044         decNumber _sf;
14045 
14046         if (n3 - op3->digits > 0)
14047         {
14048             decNumberFromInt32(&_sf, op3->exponent - (n3 - op3->digits));
14049             PRINTDEC("Value 1", op3)
14050             PRINTDEC("Value sf", &_sf)
14051             op3 = decNumberRescale(op3, op3, &_sf, &set);
14052             PRINTDEC("Value 2", op3)
14053         }
14054     }
14055 
14056     bool Ovr = false, EOvr = false, Trunc = false;
14057     uint8_t out[256];
14058 
14059     // CSFL: If the divisor is greater than the dividend after operand
14060     // alignment, the leading zero digit produced is counted and the effective
14061     // precision of the result is reduced by one.
14062     // This is a flaw in the DPS8/70 hardware which was corrected in later models
14063     // Note DPS88 and DPS9000 are different
14064     //
14065     // "greater after operand alignment" means scale until most-significant digits
14066     //   are nonzero, then compare magnitudes ignoring exponents
14067     // This passes ISOLTS-817 06e, ET 458,461,483,486
14068     char *res;
14069     if (e->S3 == CSFL) {
14070         decNumber _1a;
14071         decNumber _2a;
14072         decNumber _sf;
14073         if (op1->digits >= op2->digits) {
14074             // scale op2
14075             decNumberCopy(&_1a, op1);
14076             decNumberFromInt32(&_sf, op1->digits - op2->digits);
14077             decNumberShift(&_2a, op2, &_sf, &set);
14078         } else if (op1->digits < op2->digits) {
14079             // scale op1
14080             decNumberFromInt32(&_sf, op2->digits - op1->digits);
14081             decNumberShift(&_1a, op1, &_sf, &set);
14082             decNumberCopy(&_2a, op2);
14083         }
14084         _1a.exponent = 0;
14085         _2a.exponent = 0;
14086 
14087         PRINTDEC("dv3d: op1a", &_1a);
14088         PRINTDEC("dv3d: op2a", &_2a);
14089         sim_debug (DBG_TRACEEXT, & cpu_dev,
14090                    "dv3d: exp1 %d exp2 %d digits op1 %d op2 %d op1a %d op2a %d\r\n",
14091                    op1->exponent ,op2->exponent, op1->digits,
14092                    op2->digits, _1a.digits, _2a.digits);
14093 
14094         if (decCompareMAG(&_1a, &_2a, &set) > 0) {
14095             // shorten the result field to get proper rounding
14096             res = formatDecimal(out, &set, op3, n3 -1, (int) e->S3, e->SF3, R, &Ovr, &Trunc);
14097 
14098             // prepend zero digit
14099             // ET 458,483 float=float/float, ET 461,486 float=fixed/fixed
14100             for (int i = n3; i > 0; i--) // incl.zero terminator
14101                  res[i] = res[i-1];
14102             res[0] = '0';
14103             sim_debug (DBG_TRACEEXT, & cpu_dev, "dv3d: addzero n3 %d %s exp %d\r\n",n3,res,op3->exponent);
14104         } else {
14105             // full n3 digits are returned
14106             res = formatDecimal(out, &set, op3, n3, (int) e->S3, e->SF3, R, &Ovr, &Trunc);
14107         }
14108     } else {
14109         // same as all other decimal instructions
14110         res = formatDecimal(out, &set, op3, n3, (int) e->S3, e->SF3, R, &Ovr, &Trunc);
14111     }
14112 
14113     if (decNumberIsZero(op3))
14114         op3->exponent = 127;
14115 
14116     //(void)printf("%s\r\n", res);
14117 
14118     // now write to memory in proper format.....
14119 
14120     int pos = (int) dstCN;
14121 
14122     // 1st, take care of any leading sign .......
14123     switch(e->S3)
14124     {
14125         case CSFL:  // floating-point, leading sign.
14126         case CSLS:  // fixed-point, leading sign
14127             switch(dstTN)
14128             {
14129                 case CTN4:
14130                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
14131                               //   then the 13(8) plus sign character is placed appropriately
14132                               //   if the result of the operation is positive.
14133                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
14134                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
14135                     else
14136                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
14137                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
14138                     break;
14139                 case CTN9:
14140                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
14141                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
14142                     break;
14143             }
14144             break;
14145 
14146         case CSTS:  // nuttin' to do here .....
14147         case CSNS:
14148             break;  // no sign wysiwyg
14149     }
14150 
14151     // 2nd, write the digits .....
14152     for(int i = 0 ; i < n3 ; i++)
14153         switch(dstTN)
14154         {
14155             case CTN4:
14156                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) (res[i] - '0'));
14157                 break;
14158             case CTN9:
14159                 EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (word9) res[i]);
14160                 break;
14161             }
14162 
14163     // 3rd, take care of any trailing sign or exponent ...
14164     switch(e->S3)
14165     {
14166         case CSTS:  // write trailing sign ....
14167             switch(dstTN)
14168             {
14169                 case CTN4:
14170                     if (e->P) //If TN2 and S2 specify a 4-bit signed number and P = 1,
14171                               //   then the 13(8) plus sign character is placed appropriately if
14172                               //   the result of the operation is positive.
14173                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
14174                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  013);  // special +
14175                     else
14176                         EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
14177                                    (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? 015 :  014);  // default +
14178                     break;
14179                 case CTN9:
14180                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,
14181                                (decNumberIsNegative(op3) && !decNumberIsZero(op3)) ? '-' : '+');
14182                 break;
14183             }
14184             break;
14185 
14186         case CSFL:  // floating-point, leading sign.
14187             // write the exponent
14188             switch(dstTN)
14189             {
14190                 case CTN4:
14191                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, (op3->exponent >> 4) & 0xf); // upper 4-bits
14192                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN,  op3->exponent       & 0xf); // lower 4-bits
14193                     break;
14194                 case CTN9:
14195                     EISwrite49(cpup, &e->ADDR3, &pos, (int) dstTN, op3->exponent & 0xff);    // write 8-bit exponent
14196                     break;
14197             }
14198             break;
14199 
14200         case CSLS:  // fixed point, leading sign - already done
14201         case CSNS:  // fixed point, unsigned - nuttin' needed to do
14202             break;
14203     }
14204 
14205     // set flags, etc ...
14206     if (e->S3 == CSFL)
14207     {
14208         if (op3->exponent > 127)
14209         {
14210             SET_I_EOFL;
14211             EOvr = true;
14212         }
14213         if (op3->exponent < -128)
14214         {
14215             SET_I_EUFL;
14216             EOvr = true;
14217         }
14218     }
14219 
14220     SC_I_NEG (decNumberIsNegative(op3) && !decNumberIsZero(op3));  // set negative indicator if op3 < 0
14221     SC_I_ZERO (decNumberIsZero(op3));     // set zero indicator if op3 == 0
14222 
14223     // SC_I_TRUNC(!R && Trunc); // no truncation flag for divide
14224 
14225     cleanupOperandDescriptor (cpup, 1);
14226     cleanupOperandDescriptor (cpup, 2);
14227     cleanupOperandDescriptor (cpup, 3);
14228 
14229     //if (TST_I_TRUNC && T && tstOVFfault (cpup))
14230     //  doFault(FAULT_OFL, fst_zero, "dv3d truncation(overflow) fault");
14231     if (EOvr && tstOVFfault (cpup))
14232         doFault(FAULT_OFL, fst_zero, "dv3d over/underflow fault");
14233     if (Ovr)
14234     {
14235         SET_I_OFLOW;
14236         if (tstOVFfault (cpup))
14237           doFault(FAULT_OFL, fst_zero, "dv3d overflow fault");
14238     }
14239 }

/* [previous][next][first][last][top][bottom][index][help] */