root/src/dps8/dps8_iom.c

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

DEFINITIONS

This source file includes following definitions.
  1. iom_build_addr_map
  2. iom_addr_map_changed
  3. iom_interlace_addr
  4. iom_map_addr
  5. iom_cache_purge
  6. iom_core_read
  7. iom_core_read2
  8. iom_core_write
  9. iom_core_write2
  10. iom_core_read_lock
  11. iom_core_write_unlock
  12. iom_action
  13. iom_show_mbx
  14. iom_show_units
  15. iom_set_units
  16. iom_show_config
  17. iom_set_config
  18. iom_reset_unit
  19. iom_unit_reset_idx
  20. iom_reset
  21. iom_boot_store
  22. init_memory_iom
  23. boot_svc
  24. iom_boot
  25. mbxLoc
  26. status_service
  27. build_AUXPTW_address
  28. build_DDSPTW_address
  29. fetch_DDSPTW
  30. build_IDSPTW_address
  31. fetch_IDSPTW
  32. build_LPWPTW_address
  33. fetch_LPWPTW
  34. iom_direct_data_service
  35. iom_indirect_data_service
  36. tdcw_chan_mode
  37. update_chan_mode
  38. write_LPW
  39. iom_advance_lpw
  40. iom_continue_channel
  41. dumpDCW
  42. dumpLPW
  43. fetch_and_parse_LPW
  44. unpack_DCW
  45. pack_DCW
  46. pack_LPW
  47. fetch_and_parse_PCW
  48. fetch_and_parse_DCW
  49. peek_list_word
  50. iom_peek_next_dcw
  51. send_general_interrupt
  52. iom_fault
  53. iom_list_service
  54. doPayloadChannel
  55. doWrapAroundChan
  56. doConnectChan
  57. send_marker_interrupt
  58. send_special_interrupt
  59. send_terminate_interrupt
  60. iom_interrupt
  61. chan_thread_main
  62. iom_thread_main
  63. iom_init
  64. do_boot
  65. iomProcess
  66. iomChar

   1 /*
   2  * vim: filetype=c:tabstop=4:ai:expandtab
   3  * SPDX-License-Identifier: ICU
   4  * SPDX-License-Identifier: Multics
   5  * scspell-id: 89f10936-f62e-11ec-b310-80ee73e9b8e7
   6  *
   7  * ---------------------------------------------------------------------------
   8  *
   9  * Copyright (c) 2007-2013 Michael Mondy
  10  * Copyright (c) 2012-2016 Harry Reed
  11  * Copyright (c) 2013-2022 Charles Anthony
  12  * Copyright (c) 2026 Eric Swenson
  13  * Copyright (c) 2026 Jeffrey H. Johnson
  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 //
  33 // IOM rework # 7
  34 //
  35 
  36 // Implementation notes:
  37 //
  38 // The IOM can be configured to operate in GCOS, Extended GCOS, and Paged modes.
  39 // Only the Paged mode is supported.
  40 //
  41 // Backup list service not implemented
  42 // Wraparound channel: direct data, interrupt and status services and the
  43 //   data PCWs (doWrapAroundChan); list and indirect data services are not
  44 // Snapshot channel not implemented
  45 // Scratchpad Access channel not implemented
  46 
  47 // Direct data service
  48 
  49 // Pg: B20 "Address Extension bits will be used for core selection when the
  50 //          "the IOM is [...] in Paged mode for a non-paged channel.
  51 
  52 // Definitions:
  53 //  unit number -- ambiguous: May indicate either the Multics
  54 //                 designation for IOMA, IOMB, etc. or the scp
  55 //                 UNIT table offset.
  56 //    unit_idx --  always refers to the scp unit table offset.
  57 //
  58 //
  59 
  60 // Functional layout:
  61 
  62 // CPU emulator:
  63 //   CIOC command -> SCU cable routing to IOM emulator "iom_connect"
  64 //
  65 // IOM emulator:
  66 //   iom_connect performs the connect channel operations of fetching
  67 //   the PCW, parsing out the channel number and initial DCW.
  68 //
  69 //   The channel number and initial DCW are passed to doPayloadChannel
  70 //
  71 //   Using the cable tables, doPayloadChannel determines the handler and
  72 //   unit_idx for that channel number and calls
  73 //     iom_unit[channel_number].iom_cmd (unit_idx, DCW)
  74 //   It then walks the channel DCW list, passing DCWs to the handler.
  75 //
  76 
  77 //        make_idcw IDCW_name,
  78 //                  Command,
  79 //                  Device,
  80 //                  {record,multirecord,character,nondata},
  81 //                  {proceed,terminate,marker},
  82 //                  [ChanData,]
  83 //                  [Extension,]
  84 //                  [mask,]
  85 //
  86 //   record      0
  87 //   nondata     2
  88 //   multirecord 6
  89 //   character   8
  90 //
  91 //   terminate   0
  92 //   proceed     2
  93 //   marker      3
  94 //
  95 // Observed channel command dialects
  96 //
  97 // IOM boot
  98 // CIOC 0
  99 // Connect channel LPW 000000040000 (case c)
 100 //   DCW pointer 000000
 101 //     RES              0
 102 //     REL              0
 103 //     AE               0
 104 //     NC 21            1
 105 //     TAL 22           0
 106 //     REL              0
 107 //     TALLY         0000
 108 //
 109 // PCW 000000720201 012000000000
 110 //   Chan info     0012
 111 //   Addr ext        00
 112 //   111              7
 113 //   M                0
 114 //   Chan info  22046447
 115 //   Pg Tbl Ptr  000000
 116 //   PTP              0
 117 //   PGE              0
 118 //   AUX              0
 119 //
 120 // MTP Tape channel LPW 000004020002 (case b)
 121 //   DCW pointer 000004
 122 //     RES              0
 123 //     REL              0
 124 //     AE               0
 125 //     NC 21            0
 126 //     TAL 22           1
 127 //     REL              0
 128 //     TALLY         0002
 129 //
 130 // DCW list dump:
 131 //   IDCW 000000720201
 132 //     cmd             00 Request Status
 133 //     dev code        00
 134 //     ctrl             2 (proceed)
 135 //     chancmd          2 (nondata)
 136 //   IDCW 050000700000
 137 //     cmd             05 Rd Bin Rcrd
 138 //     dev code        00
 139 //     ctrl             0 (terminate)
 140 //     chancmd          0 (record)
 141 //   IOTD (0) 000030000000
 142 //     tally       0000
 143 //     addr            30
 144 //     cp               0
 145 //   IOTD (0) 400000000000000000000
 146 //     tally       0000
 147 //     addr            00
 148 //     cp               0
 149 //   IOTD (0) 400000000000000000000
 150 //     tally       0000
 151 //     addr            00
 152 //     cp               0
 153 //
 154 // Tape commands done:
 155 //   Tape Request Status
 156 //   Tape Read Binary Record
 157 //   IOTD (0) 000030000000
 158 //     tally       0000
 159 //     addr            30
 160 //     cp               0
 161 //   Tape IOT Read
 162 //     Tape 0 reads record 1
 163 //   ctrl == 0 in chan 10 (12) DCW
 164 
 165 // Bootload check tape controller firmware loaded test
 166 //
 167 // CIOC 138
 168 // Connect channel LPW 000000040000 (case c)
 169 //   DCW pointer 000000
 170 //     RES              0
 171 //     REL              0
 172 //     AE               0
 173 //     NC 21            1
 174 //     TAL 22           0
 175 //     REL              0
 176 //     TALLY         0000
 177 //
 178 // PCW 000000720201 012000000000
 179 //   Chan info     0012
 180 //   Addr ext        00
 181 //   111              7
 182 //   M                0
 183 //   Chan info    22046447
 184 //   Pg Tbl Ptr  000000
 185 //   PTP              0
 186 //   PGE              0
 187 //   AUX              0
 188 //
 189 // MTP Tape channel LPW 000540007777 (case a)
 190 //   DCW pointer 000540
 191 //     RES              0
 192 //     REL              0
 193 //     AE               0
 194 //     NC 21            0
 195 //     TAL 22           0
 196 //     REL              0
 197 //     TALLY         7777
 198 //
 199 // DCW list dump:
 200 //   IDCW 000000720201
 201 //     cmd             00 Request Status
 202 //     dev code        00
 203 //     ctrl             2 (proceed)
 204 //     chancmd          2 (nondata)
 205 //   IDCW 510000700200
 206 //     cmd             51 Reset Dev Stat/Alert
 207 //     dev code        00
 208 //     ctrl             0 (terminate)
 209 //     chancmd          2 (nondata)
 210 //   IDCW 000000700200
 211 //     cmd             00 Request Status
 212 //     dev code        00
 213 //     ctrl             0 (terminate)
 214 //     chancmd          2 (nondata)
 215 //   IOTD (0) 200000000000
 216 //     tally       0000
 217 //     addr            200000
 218 //     cp               0
 219 //
 220 // Tape commands done:
 221 //   Tape Request Status
 222 //   Tape Reset Device Status
 223 //   ctrl == 0 in chan 10 (12) DCW
 224 
 225 // Bootload tape read
 226 //
 227 // CIOC 7999826
 228 // Connect channel LPW 000000040000 (case c)
 229 //   DCW pointer 000000
 230 //     RES              0
 231 //     REL              0
 232 //     AE               0
 233 //     NC 21            1
 234 //     TAL 22           0
 235 //     REL              0
 236 //     TALLY         0000
 237 //
 238 // PCW 000000720201 012000000000
 239 //   Chan info     0012
 240 //   Addr ext        00
 241 //   111              7
 242 //   M                0
 243 //   Chan info    22046447
 244 //   Pg Tbl Ptr  000000
 245 //   PTP              0
 246 //   PGE              0
 247 //   AUX              0
 248 //
 249 // MTP Tape channel LPW 000621007777 (case a)
 250 //   DCW pointer 000621
 251 //     RES              0
 252 //     REL              0
 253 //     AE               0
 254 //     NC 21            0
 255 //     TAL 22           0
 256 //     REL              0
 257 //     TALLY         7777
 258 //
 259 // DCW list dump:
 260 //   IDCW 000000720201
 261 //     cmd             00 Request Status
 262 //     dev code        00
 263 //     ctrl             2 (proceed)
 264 //     chancmd          2 (nondata)
 265 //   IDCW 050000700000
 266 //     cmd             05 Rd Bin Rcrd
 267 //     dev code        00
 268 //     ctrl             0 (terminate)
 269 //     chancmd          0 (record)
 270 //   IOTD (0) 060001002020
 271 //     tally       2020
 272 //     addr            60001
 273 //     cp               0
 274 //   IOTD (0) 024000000000
 275 //     tally       0000
 276 //     addr            24000
 277 //     cp               0
 278 //
 279 // Tape commands done:
 280 //   Tape Request Status
 281 //   Tape Read Binary Record
 282 //   IOTD (0) 060001002020
 283 //     tally       2020
 284 //     addr            60001
 285 //     cp               0
 286 //   Tape IOT Read
 287 //   ctrl == 0 in chan 10 (12) DCW
 288 
 289 // Bootload searching for console
 290 //
 291 // CIOC 8191828
 292 // Connect channel LPW 037312020000 (case b)
 293 //   DCW pointer 037312
 294 //     RES              0
 295 //     REL              0
 296 //     AE               0
 297 //     NC 21            0
 298 //     TAL 22           1
 299 //     REL              0
 300 //     TALLY         0000
 301 //
 302 // PCW 510000700200 010000000000
 303 //   Chan info     0010
 304 //   Addr ext        00
 305 //   111              7
 306 //   M                0
 307 //   Chan info 22046447
 308 //   Pg Tbl Ptr  000000
 309 //   PTP              0
 310 //   PGE              0
 311 //   AUX              0
 312 //
 313 
 314 // Bootload finding console
 315 //
 316 // CIOC 8324312
 317 // Connect channel LPW 037312020000 (case b)
 318 //   DCW pointer 037312
 319 //     RES              0
 320 //     REL              0
 321 //     AE               0
 322 //     NC 21            0
 323 //     TAL 22           1
 324 //     REL              0
 325 //     TALLY         0000
 326 //
 327 // PCW 510000700200 036000000000
 328 //   Chan info     0036
 329 //   Addr ext        00
 330 //   111              7
 331 //   M                0
 332 //   Chan info 22046447
 333 //   Pg Tbl Ptr  000000
 334 //   PTP              0
 335 //   PGE              0
 336 //   AUX              0
 337 //
 338 // CONSOLE: ALERT
 339 
 340 // Bootload console read ID
 341 //
 342 // Connect channel LPW 037312020000 (case b)
 343 //   DCW pointer 037312
 344 //     RES              0
 345 //     REL              0
 346 //     AE               0
 347 //     NC 21            0
 348 //     TAL 22           1
 349 //     REL              0
 350 //     TALLY         0000
 351 //
 352 // PCW 570000700200 036000000000
 353 //   Chan info     0036
 354 //   Addr ext        00
 355 //   111              7
 356 //   M                0
 357 //   Chan info 22046447
 358 //   Pg Tbl Ptr  000000
 359 //   PTP              0
 360 //   PGE              0
 361 //   AUX              0
 362 //
 363 
 364 // Bootload console write ASCII
 365 //
 366 // Connect channel LPW 037312020000 (case b)
 367 //   DCW pointer 037312
 368 //     RES              0
 369 //     REL              0
 370 //     AE               0
 371 //     NC 21            0
 372 //     TAL 22           1
 373 //     REL              0
 374 //     TALLY         0000
 375 //
 376 // PCW 330000700000 036000000000
 377 //   Chan info     0036
 378 //   Addr ext        00
 379 //   111              7
 380 //   M                0
 381 //   Chan info 22046447
 382 //   Pg Tbl Ptr  000000
 383 //   PTP              0
 384 //   PGE              0
 385 //   AUX              0
 386 //
 387 // IOTD (0) 033624000022
 388 //   tally       0022
 389 //   addr            33624
 390 //   cp               0
 391 // Console channel LPW 033357007777 (case a)
 392 //   DCW pointer 033357
 393 //     RES              0
 394 //     REL              0
 395 //     AE               0
 396 //     NC 21            0
 397 //     TAL 22           0
 398 //     REL              0
 399 //     TALLY         7777
 400 //
 401 // DCW list dump:
 402 //   IDCW 330000700000
 403 //     cmd             33 Write ASCII
 404 //     dev code        00
 405 //     ctrl             0 (terminate)
 406 //     chancmd          0 (record)
 407 //   IOTD (0) 033624000022
 408 //     tally       0022
 409 //     addr            33624
 410 //     cp               0
 411 //   IDCW 510000700000
 412 //     cmd             51 Reset Dev Stat/Alert
 413 //     dev code        00
 414 //     ctrl             0 (terminate)
 415 //     chancmd          0 (record)
 416 
 417 //
 418 //  T&D Read block 3
 419 //    Connect Channel LPW 006312040001 (case c)
 420 //      DCW pointer 006312
 421 //      RES              0
 422 //      REL              0
 423 //      AE               0
 424 //      NC 21            1
 425 //      TAL 22           0
 426 //      REL              0
 427 //      TALLY         0001
 428 //    PCW 050000700001 012000000000
 429 //      Chan info     0500
 430 //      Addr ext        00
 431 //      111              7
 432 //      M                0
 433 //      Chan info    00001
 434 //      Chan            12
 435 //      Pg Tbl Ptr  000000
 436 //      PTP              0
 437 //      PGE              0
 438 //      AUX              0
 439 //    IDCW 050000700001
 440 //      cmd             05 (Read Binary Record)
 441 //      dev             00
 442 //      ctrl             0
 443 //      chancmd          0
 444 //
 445 //    Payload channel
 446 //      IOTD 010000000000
 447 //        tally       0000
 448 //        addr      010000
 449 //
 450 
 451 // Table 3.2.1 Updating of LPW address and tally and indication of PTRO and TRO
 452 //
 453 //  case   LPW 21   LPW 22  TALLY  UPDATE   INDICATE    INDICATE
 454 //         NC       TALLY   VALUE  ADDR &   PTRO TO     TRO
 455 //                                 TALLY    CONN. CH.   FAULT
 456 //    a.     0       0       any    yes       no         no
 457 //    b.     0       1       > 2    yes       no         no
 458 //                            1     yes       yes        no
 459 //                            0     no        no         yes
 460 //    c.     1       x       any    no        yes        no
 461 //
 462 // Case a. is GCOS common Peripheral Interface payloads channels;
 463 // a tally of 0 is not a fault since the tally field will not be
 464 // used for that purpose.
 465 //
 466 // Case b. is the configuration expected of standard operation with real-time
 467 // applications and/or direct data channels.
 468 //
 469 // Case c. will be used normally only with the connect channel servicing CPI
 470 // type payload channels. [CAC: "INDICATE PTRO TO CONN. CH." means that the
 471 // connect channel should not call list service to obtain additional PCW's]
 472 //
 473 // If case a. is used with the connect channels, a "System Fault: Tally
 474 // Control Error, Connect Channel" will be generated. Case c. will be
 475 // used in the bootload program.
 476 
 477 // LPW bits
 478 //
 479 //  0-17  DCW pointer
 480 //  18    RES - Restrict IDCW's [and] changing AE flag
 481 //  19    REL - IOM controlled image of REL bit
 482 //  20    AE - Address extension flag
 483 //  21    NC
 484 //  22    TAL
 485 //  23    REL
 486 //  24-35 TALLY
 487 //
 488 
 489 // LPWX (sometimes called LPWE)
 490 //
 491 //  0-8   Lower bound
 492 //  9-17  Size
 493 //  18-35 Pointer to DCW of most recent instruction
 494 //
 495 
 496 // PCW bits
 497 //
 498 // word 1
 499 //   0-11 Channel information
 500 //  12-17 Address extension
 501 //  18-20 111
 502 //  21    M
 503 //  22-35 Channel Information
 504 //
 505 // word 2
 506 //  36-38 SBZ
 507 //  39-44 Channel Number
 508 //  45-62 Page table ptr
 509 //  63    PTP
 510 //  64    PGE
 511 //  65    AUX
 512 //  66-21 not used
 513 
 514 // IDCW
 515 //
 516 //   0-11 Channel information
 517 //  12-17 Address extension
 518 //  18-20 111
 519 //  21    EC
 520 //  22-35 Channel Information
 521 //
 522 
 523 // TDCW
 524 //
 525 // Paged, PCW 64 = 0
 526 //
 527 //   0-17 DCW pointer
 528 //  18-20 Not 111
 529 //  21    0
 530 //  22    1
 531 //  23    0
 532 //  24-32 SBZ
 533 //  33    EC
 534 //  34    RES
 535 //  35    REL
 536 //
 537 // Paged, PCW 64 = 1
 538 //
 539 //   0-17 DCW pointer
 540 //  18-20 Not 111
 541 //  21    0
 542 //  22    1
 543 //  23    0
 544 //  24-30 SBZ
 545 //  31    SEG
 546 //  32    PDTA
 547 //  33    PDCW
 548 //  34    RES
 549 //  35    REL
 550 //
 551 
 552 #include "dps8.h"
 553 #include "dps8_cpu.h"
 554 #include "dps8_rt.h"
 555 #include "dps8_priv.h"
 556 #include "dps8_sys.h"
 557 #include "dps8_faults.h"
 558 #include "dps8_scu.h"
 559 #include "dps8_iom.h"
 560 #include "dps8_cable.h"
 561 #include "dps8_console.h"
 562 #include "dps8_fnp2.h"
 563 #include "dps8_utils.h"
 564 #include "dps8_cache.h"
 565 #if defined(LOCKLESS)
 566 # include "threadz.h"
 567 #endif
 568 
 569 #define DBG_CTR 1
 570 
 571 // Nomenclature
 572 //
 573 //  IDX index   refers to emulator unit
 574 //  NUM         refers to the number that the unit is configured as ("IOMA,
 575 //              IOMB,..."). Encoded in the low to bits of configSwMultiplexBaseAddress
 576 
 577 // Default
 578 #define N_IOM_UNITS 1
 579 
 580 #define IOM_UNIT_IDX(uptr) ((uptr) - iom_unit)
 581 
 582 #define IOM_SYSTEM_FAULT_CHAN 1U
 583 #define IOM_CONNECT_CHAN 2U
 584 #define IOM_WAC_CHAN 4U
 585 #define IOM_SPECIAL_STATUS_CHAN 6U
 586 
 587 iom_chan_data_t iom_chan_data[N_IOM_UNITS_MAX][MAX_CHANNELS];
 588 
 589 typedef enum iom_status_t
 590   {
 591     iomStatNormal         = 0,
 592     iomStatLPWTRO         = 1,
 593     iomStat2TDCW          = 2,
 594     iomStatBoundaryError  = 3,
 595     iomStatAERestricted   = 4,
 596     iomStatIDCWRestricted = 5,
 597     iomStatCPDiscrepancy  = 6,
 598     iomStatParityErr      = 7
 599   } iom_status_t;
 600 
 601 enum config_sw_OS_t
 602   {
 603     CONFIG_SW_STD_GCOS,
 604     CONFIG_SW_EXT_GCOS,
 605     CONFIG_SW_MULTICS  // "Paged"
 606   };
 607 
 608 enum config_sw_model_t
 609   {
 610     CONFIG_SW_MODEL_IOM,
 611     CONFIG_SW_MODEL_IMU
 612   };
 613 
 614 // Boot device: CARD/TAPE;
 615 enum config_sw_bootload_device_e { CONFIG_SW_BLCT_CARD, CONFIG_SW_BLCT_TAPE };
 616 
 617 // IOM memory address mapping (useMap): the IOM's port switches (address
 618 // assignment, store size, half size, enable) and its cables to the SCUs
 619 // select the SCU, and so the zone of M (4M per SCU, as the CPU's port
 620 // mapping keeps them), that holds an address. Mapped in 16K units, the
 621 // smallest port (32K, half size).
 622 #define IOM_MAP_UNIT  16384u
 623 #define IOM_MAP_UNITS (MEM_SIZE_MAX / IOM_MAP_UNIT)
 624 #define IOM_ZONE_SZ   (MEM_SIZE_MAX / 4u)
 625 
 626 typedef struct
 627   {
 628     // Configuration switches
 629 
 630     // Interrupt multiplex base address: 12 toggles
 631     word12 configSwIomBaseAddress;
 632 
 633     // Mailbox base aka IOM base address: 9 toggles
 634     // Note: The IOM number is encoded in the lower two bits
 635 
 636     // AM81, pg 60 shows an image of a Level 68 IOM configuration panel
 637     // The switches are arranged and labeled
 638     //
 639     //  12   13   14   15   16   17   18   --   --  --  IOM
 640     //                                                  NUMBER
 641     //   X    X    X    X    X    X    X                X     X
 642     //
 643 
 644     word9 configSwMultiplexBaseAddress;
 645 
 646     enum config_sw_model_t config_sw_model; // IOM or IMU
 647 
 648     // OS: Three position switch: GCOS, EXT GCOS, Multics
 649     enum config_sw_OS_t config_sw_OS; // = CONFIG_SW_MULTICS;
 650 
 651     // Bootload device: Toggle switch CARD/TAPE
 652     enum config_sw_bootload_device_e configSwBootloadCardTape; // = CONFIG_SW_BLCT_TAPE;
 653 
 654     // Bootload tape IOM channel: 6 toggles
 655     word6 configSwBootloadMagtapeChan; // = 0;
 656 
 657     // Bootload cardreader IOM channel: 6 toggles
 658     word6 configSwBootloadCardrdrChan; // = 1;
 659 
 660     // Bootload: pushbutton
 661 
 662     // Sysinit: pushbutton
 663 
 664     // Bootload SCU port: 3 toggle AKA "ZERO BASE S.C. PORT NO"
 665     // "the port number of the SC through which which connects are to
 666     // be sent to the IOM
 667     word3 configSwBootloadPort; // = 0;
 668 
 669     // 8 Ports: CPU/IOM connectivity
 670     // Port configuration: 3 toggles/port
 671     // Which SCU number is this port attached to
 672     uint configSwPortAddress[N_IOM_PORTS]; // = { 0, 1, 2, 3, 4, 5, 6, 7 };
 673 
 674     // Port interlace: 1 toggle/port
 675     uint configSwPortInterface[N_IOM_PORTS]; // = { 0, 0, 0, 0, 0, 0, 0, 0 };
 676 
 677     // Port enable: 1 toggle/port
 678     uint configSwPortEnable[N_IOM_PORTS]; // = { 0, 0, 0, 0, 0, 0, 0, 0 };
 679 
 680     // Port system initialize enable: 1 toggle/port // XXX What is this
 681     uint configSwPortSysinitEnable[N_IOM_PORTS]; // = { 0, 0, 0, 0, 0, 0, 0, 0 };
 682 
 683     // Port half-size: 1 toggle/port // XXX what is this
 684     uint configSwPortHalfsize[N_IOM_PORTS]; // = { 0, 0, 0, 0, 0, 0, 0, 0 };
 685     // Port store size: 1 8 pos. rotary/port
 686     uint configSwPortStoresize[N_IOM_PORTS]; // = { 0, 0, 0, 0, 0, 0, 0, 0 };
 687 
 688     // Map memory addresses through the port switches and cables (set iom
 689     // config=usemap=1); off: the address is the location in M, whatever
 690     // the switches say. Like the CPU's useMap, it is off by default, so
 691     // configurations whose IOM switches were never kept consistent with
 692     // the memory work as before.
 693     bool useMap;
 694     bool addrMapValid;   // addrMap is up to date with the switches/cables
 695     bool addrMapWarned;  // "address on no enabled port" was reported
 696     int addrMap[IOM_MAP_UNITS];        // location in M of each unit; -1: none
 697     int8_t addrMapPort[IOM_MAP_UNITS]; // IOM port of each unit
 698 
 699     // other switches:
 700     //   alarm disable
 701     //   test/normal
 702     iom_status_t iomStatus;
 703 
 704     uint invokingScuUnitIdx; // the unit number of the SCU that did the connect.
 705 
 706     coreLockState_t  iomCoreLockState;
 707 
 708     // Wraparound channel (WAC, channel 004) registers; 43A239854 3.9
 709     word36 wacData[2];   // data registers #1 and #2
 710     word36 wacStatus;    // status register
 711 
 712   } iom_unit_data_t;
 713 
 714 static iom_unit_data_t iom_unit_data[N_IOM_UNITS_MAX];
 715 
 716 typedef enum iomSysFaults_t
 717   {
 718     // List from 4.5.1; descr from AN87, 3-9
 719     iomNoFlt         = 000,
 720     iomIllChanFlt    = 001, // PCW to chan with chan number >= 40
 721                             // or channel without scratchpad installed
 722     iomIllSrvReqFlt  = 002, // A channel requested a service request code
 723                             // of zero, a channel number of zero, or
 724                             // a channel number >= 40
 725     // =003,                // Parity error scratchpad
 726     iomBndryVioFlt   = 003, // pg B36
 727     iom256KFlt       = 004, // 256K overflow -- address decremented to zero,
 728                             // but not tally
 729     iomLPWTRPConnFlt = 005, // tally was zero for an update LPW (LPW bit
 730                             // 21==0) when the LPW was fetched for the
 731                             // connect channel
 732     iomNotPCWFlt     = 006, // DCW for conn channel had bits 18..20 != 111b
 733     iomCP1Flt        = 007, // DCW was TDCW or had bits 18..20 == 111b
 734     // = 010,               // CP/CS-BAD-DATA DCW fetch for a 9 bit channel
 735                             // contained an illegal character position
 736     // = 011,               // NO-MEM-RES No response to an interrupt from
 737                             // a system controller within 16.5 usec.
 738     // = 012,               // PRTY-ERR-MEM Parity error on the read
 739                             // data when accessing a system controller.
 740     iomIllTalCChnFlt = 013, // LPW bits 21-22 == 00 when LPW was fetched
 741                             // for the connect channel
 742     // = 014,               // PTP-Fault: PTP-Flag= zero or PTP address
 743                             // overflow.
 744     iomPTWFlagFault  = 015, // PTW-Flag-Fault: Page Present flag zero, or
 745                             // Write Control Bit 0, or Housekeeping bit set,
 746     // = 016,               // ILL-LPW-STD LPW had bit 20 on in GCOS mode
 747     iomNoPortFault   = 017, // NO-PRT-SEL No port selected during attempt
 748                             // to access memory.
 749   } iomSysFaults_t;
 750 
 751 typedef enum iomFaultServiceRequest
 752   {
 753     // Combined SR/M/D
 754     iomFsrFirstList =  (1 << 2) | 2,
 755     iomFsrList =       (1 << 2) | 0,
 756     iomFsrStatus =     (2 << 2) | 0,
 757     iomFsrIntr =       (3 << 2) | 0,
 758     iomFsrSPIndLoad =  (4 << 2) | 0,
 759     iomFsrDPIndLoad =  (4 << 2) | 1,
 760     iomFsrSPIndStore = (5 << 2) | 0,
 761     iomFsrDPIndStore = (5 << 2) | 1,
 762     iomFsrSPDirLoad =  (6 << 2) | 0,
 763     iomFsrDPDirLoad =  (6 << 2) | 1,
 764     iomFsrSPDirStore = (7 << 2) | 0,
 765     iomFsrDPDirStore = (7 << 2) | 1
 766   } iomFaultServiceRequest;
 767 
 768 #if defined(IO_THREADZ)
 769 __thread uint this_iom_idx;
 770 __thread uint this_chan_num;
 771 #endif
 772 
 773 #if defined(TESTING)
 774 static char * cmdNames [] =
 775   {
 776     "Request Status",        //  0
 777     "",                      //  1
 778     "Rd Ctrlr Mem",          //  2
 779     "Rd 9 Rcrd",             //  3
 780     "",                      //  4
 781     "Rd Bin Rcrd",           //  5
 782     "Initiate Rd Data Xfer", //  6
 783     "",                      //  7
 784     "",                      // 10
 785     "",                      // 11
 786     "",                      // 12
 787     "Wr 9 Rcrd",             // 13
 788     "",                      // 14
 789     "Wr Bin Rcrd",           // 15
 790     "Initiate Wr Data Xfer", // 16
 791     "",                      // 17
 792     "Release Ctlr",          // 20
 793     "",                      // 21
 794     "RdStatReg",             // 22
 795     "",                      // 23
 796     "",                      // 24
 797     "Read",                  // 25
 798     "",                      // 26
 799     "",                      // 27
 800     "Seek 512",              // 30
 801     "",                      // 31
 802     "MTP Wr Mem",            // 32
 803     "Write ASCII",           // 33
 804     "",                      // 34
 805     "",                      // 35
 806     "",                      // 36
 807     "",                      // 37
 808     "Reset Status",          // 40
 809     "Set 6250 CPI",          // 41
 810     "Restore",               // 42
 811     "",                      // 43
 812     "Forward Skip Rcrd",     // 44
 813     "Forward Skip File",     // 45
 814     "Backward Skip Rcrd",    // 46
 815     "Backspace File",        // 47
 816     "Request Status",        // 50
 817     "Reset Dev Stat/Alert",  // 51
 818     "",                      // 52
 819     "",                      // 53
 820     "",                      // 54
 821     "Write EOF",             // 55
 822     "",                      // 56
 823     "Survey Devs/ReadID",    // 57
 824     "Set 800 BPI",           // 60
 825     "Set 556 BPI",           // 61
 826     "",                      // 62
 827     "Set File Permit",       // 63
 828     "Set 200 BPI",           // 64
 829     "Set 1600 CPI",          // 65
 830     "",                      // 66
 831     "",                      // 67
 832     "Rewind",                // 70
 833     "",                      // 71
 834     "Rewind/Unload",         // 72
 835     "",                      // 73
 836     "",                      // 74
 837     "",                      // 75
 838     "",                      // 76
 839     "",                      // 77
 840   };
 841 #endif
 842 
 843 // Build the IOM's address map from its port switches and cables.
 844 static void iom_build_addr_map (uint iom_unit_idx)
     /* [previous][next][first][last][top][bottom][index][help] */
 845   {
 846     iom_unit_data_t * p = iom_unit_data + iom_unit_idx;
 847     for (uint u = 0; u < IOM_MAP_UNITS; u ++)
 848       {
 849         p->addrMap[u]     = -1;
 850         p->addrMapPort[u] = -1;
 851       }
 852     for (uint port = 0; port < N_IOM_PORTS; port ++)
 853       {
 854         if (! p->configSwPortEnable[port])
 855           continue;
 856         struct iom_to_scu_s * c = & cables->iom_to_scu[iom_unit_idx][port];
 857         if (! c->in_use || c->scu_unit_idx >= MEM_SIZE_MAX / IOM_ZONE_SZ)
 858           continue;
 859         if (p->useMap && p->configSwPortInterface[port])
 860           sim_warn ("IOM%c port %u: port interlace is not modelled; "
 861                     "mapped as not interlaced\r\n",
 862                     'A' + iom_unit_idx, port);
 863         uint size = 32768u << (p->configSwPortStoresize[port] & 7u);
 864         if (p->configSwPortHalfsize[port])
 865           size /= 2;
 866         uint base = size * p->configSwPortAddress[port];
 867         for (uint off = 0; off < size; off += IOM_MAP_UNIT)
 868           {
 869             uint addr = base + off;
 870             if (addr >= MEM_SIZE_MAX)
 871               break;
 872             uint u = addr / IOM_MAP_UNIT;
 873             if (p->addrMap[u] >= 0)
 874               {
 875                 if (p->useMap)
 876                   sim_warn ("IOM%c port %u: addresses from %08o are already "
 877                             "on port %d\r\n", 'A' + iom_unit_idx, port, addr,
 878                             p->addrMapPort[u]);
 879                 break;
 880               }
 881             p->addrMap[u]     = (int) (c->scu_unit_idx * IOM_ZONE_SZ + off);
 882             p->addrMapPort[u] = (int8_t) port;
 883           }
 884       }
 885     p->addrMapValid = true;
 886   }
 887 
 888 // The switches or cables changed: rebuild the map before its next use.
 889 void iom_addr_map_changed (uint iom_unit_idx)
     /* [previous][next][first][last][top][bottom][index][help] */
 890   {
 891     if (iom_unit_idx < N_IOM_UNITS_MAX)
 892       iom_unit_data[iom_unit_idx].addrMapValid = false;
 893   }
 894 
 895 // The location in M of the IOM's memory address addr on port PORT, whose
 896 // SCU interlaces its stores A and B (config register INT, T&D only): the
 897 // stores alternate double words, so a word is not kept at its offset in
 898 // the port's zone of M. It is kept where the CPUs' sc_fine_map keeps it
 899 // (setup_scbank_map_tandd): the SCU address (the address the SCU
 900 // receives) selects the store by its 2 bit and the word in the store by
 901 // the rest; store A is kept from 0 in the zone and store B from the lower
 902 // store size (2M at most), whatever LWR says; and the whole is kept
 903 // relative to the port's first SCU address, mod 4M. (PM894 05A has the
 904 // IOM's wraparound channel store into an interlaced store and the CPU
 905 // read the word back.) A word in no store is used at its offset.
 906 static uint iom_interlace_addr (uint iom_unit_idx, uint port, word24 addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 907   {
 908     iom_unit_data_t * p = iom_unit_data + iom_unit_idx;
 909     uint scu_unit_idx = cables->iom_to_scu[iom_unit_idx][port].scu_unit_idx;
 910     scu_t * up = scu + scu_unit_idx;
 911     uint size = 32768u << (p->configSwPortStoresize[port] & 7u);
 912     if (p->configSwPortHalfsize[port])
 913       size /= 2;
 914     uint base_wds     = size * p->configSwPortAddress[port];
 915     uint scu_base_wds = base_wds % (4u << 20);
 916     uint scu_addr     = scu_base_wds + (addr - base_wds);
 917     uint lower_wds    = (1u << up->lower_store_size) * 32768u;
 918     uint stride_wds   = lower_wds > (2u << 20) ? (2u << 20) : lower_wds;
 919     uint store = (scu_addr >> 1) & 1;
 920     uint word  = ((scu_addr >> 2) << 1) | (scu_addr & 1);
 921     if (scu_addr >= 2 * lower_wds || word >= lower_wds)
 922       return scu_unit_idx * IOM_ZONE_SZ + (addr - base_wds) % IOM_ZONE_SZ;
 923     uint home_store = up->lwr ? 1 - store : store;  // 0 = A, 1 = B
 924     uint home = home_store * stride_wds + word;
 925     return scu_unit_idx * IOM_ZONE_SZ + (home + (4u << 20) - scu_base_wds) % (4u << 20);
 926   }
 927 
 928 // The location in M of the IOM's memory address addr. An address that no
 929 // enabled port holds is used unmapped, with a warning (the first time),
 930 // rather than taking the IOM down.
 931 uint iom_map_addr (uint iom_unit_idx, word24 addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 932   {
 933     iom_unit_data_t * p = iom_unit_data + iom_unit_idx;
 934     if (! p->useMap)
 935       return addr;
 936     if (! p->addrMapValid)
 937       iom_build_addr_map (iom_unit_idx);
 938     uint u = addr / IOM_MAP_UNIT;
 939     int base = u < IOM_MAP_UNITS ? p->addrMap[u] : -1;
 940     if (base < 0)
 941       {
 942         if (! p->addrMapWarned)
 943           {
 944             sim_warn ("IOM%c: address %08o is on no enabled port; "
 945                       "used unmapped\r\n", 'A' + iom_unit_idx, addr);
 946             p->addrMapWarned = true;
 947           }
 948         return addr;
 949       }
 950     uint port = (uint) p->addrMapPort[u];
 951     struct iom_to_scu_s * c = & cables->iom_to_scu[iom_unit_idx][port];
 952     if (scu[c->scu_unit_idx].interlace)
 953       return iom_interlace_addr (iom_unit_idx, port, addr);
 954     return (uint) base + addr % IOM_MAP_UNIT;
 955   }
 956 
 957 // An IOM store to addr (a location in M): purge the block from every
 958 // CPU's cache. The caches are keyed by the location in M, so this is
 959 // right whatever the IOM's and the CPUs' port maps are; on the hardware
 960 // only CPUs on the SCU holding the word could cache it, and they are all
 961 // among these. (A CPU whose cache is off returns at once.)
 962 static void iom_cache_purge (word24 addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 963   {
 964     cachePurgeAll (addr);
 965   }
 966 
 967 void iom_core_read (uint iom_unit_idx, word24 addr, word36 *data, UNUSED const char * ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
 968   {
 969     addr = (word24) iom_map_addr (iom_unit_idx, addr);
 970 #if defined(LOCKLESS)
 971 # if !defined(SUNLINT)
 972     word36 v;
 973     LOAD_ACQ_CORE_WORD(v, addr);
 974     * data = v & DMASK;
 975 # endif /* if !defined(SUNLINT) */
 976 #else
 977     * data = M[addr] & DMASK;
 978 #endif
 979   }
 980 
 981 void iom_core_read2 (uint iom_unit_idx, word24 addr, word36 *even, word36 *odd, UNUSED const char * ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
 982   {
 983     word24 addr1 = (word24) iom_map_addr (iom_unit_idx, (addr + 1) & MASK24);
 984     addr = (word24) iom_map_addr (iom_unit_idx, addr);
 985 #if defined(LOCKLESS)
 986 # if !defined(SUNLINT)
 987     word36 v;
 988     LOAD_ACQ_CORE_WORD(v, addr);
 989     * even = v & DMASK;
 990     LOAD_ACQ_CORE_WORD(v, addr1);
 991     * odd = v & DMASK;
 992 # endif /* if !defined(SUNLINT) */
 993 #else
 994     * even = M[addr]  & DMASK;
 995     * odd =  M[addr1] & DMASK;
 996 #endif
 997   }
 998 
 999 void iom_core_write (uint iom_unit_idx, word24 addr, word36 data, UNUSED const char * ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
1000   {
1001     addr = (word24) iom_map_addr (iom_unit_idx, addr);
1002 #if defined(LOCKLESS)
1003     LOCK_CORE_WORD(addr, & iom_unit_data[iom_unit_idx].iomCoreLockState);
1004 # if !defined(SUNLINT)
1005     STORE_REL_CORE_WORD(addr, data);
1006 # endif /* if !defined(SUNLINT) */
1007 #else
1008     M[addr] = data & DMASK;
1009 #endif
1010     iom_cache_purge (addr);
1011   }
1012 
1013 void iom_core_write2 (uint iom_unit_idx, word24 addr, word36 even, word36 odd, UNUSED const char * ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
1014   {
1015     word24 addr1 = (word24) iom_map_addr (iom_unit_idx, (addr + 1) & MASK24);
1016     addr = (word24) iom_map_addr (iom_unit_idx, addr);
1017 #if defined(LOCKLESS)
1018     LOCK_CORE_WORD(addr, & iom_unit_data[iom_unit_idx].iomCoreLockState);
1019 # if !defined(SUNLINT)
1020     STORE_REL_CORE_WORD(addr, even);
1021 # endif /* if !defined(SUNLINT) */
1022     LOCK_CORE_WORD(addr1, & iom_unit_data[iom_unit_idx].iomCoreLockState);
1023 # if !defined(SUNLINT)
1024     STORE_REL_CORE_WORD(addr1, odd);
1025 # endif /* if !defined(SUNLINT) */
1026 #else
1027     M[addr]  = even;
1028     M[addr1] = odd;
1029 #endif
1030     iom_cache_purge (addr);
1031     iom_cache_purge (addr1);
1032   }
1033 
1034 void iom_core_read_lock (uint iom_unit_idx, word24 addr, word36 *data, UNUSED const char * ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
1035   {
1036     addr = (word24) iom_map_addr (iom_unit_idx, addr);
1037 #if defined(LOCKLESS)
1038     LOCK_CORE_WORD(addr, & iom_unit_data[iom_unit_idx].iomCoreLockState);
1039 # if !defined(SUNLINT)
1040     word36 v;
1041     LOAD_ACQ_CORE_WORD(v, addr);
1042     * data = v & DMASK;
1043 # endif /* if !defined(SUNLINT) */
1044 #else
1045     * data = M[addr] & DMASK;
1046 #endif
1047   }
1048 
1049 void iom_core_write_unlock (uint iom_unit_idx, word24 addr, word36 data, UNUSED const char * ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
1050   {
1051     addr = (word24) iom_map_addr (iom_unit_idx, addr);
1052 #if defined(LOCKLESS)
1053 # if !defined(SUNLINT)
1054     STORE_REL_CORE_WORD(addr, data);
1055 # endif /* if !defined(SUNLINT) */
1056 #else
1057     M[addr] = data & DMASK;
1058 #endif
1059     iom_cache_purge (addr);
1060   }
1061 
1062 static t_stat iom_action (UNIT *up)
     /* [previous][next][first][last][top][bottom][index][help] */
1063   {
1064     // Recover the stash parameters
1065     uint scu_unit_idx = (uint) (up -> u3);
1066     uint iom_unit_idx = (uint) (up -> u4);
1067     iom_interrupt (scu_unit_idx, iom_unit_idx);
1068     return SCPE_OK;
1069   }
1070 
1071 static UNIT iom_unit[N_IOM_UNITS_MAX] = {
1072 #if defined(NO_C_ELLIPSIS)
1073   { UDATA (iom_action, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1074   { UDATA (iom_action, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1075   { UDATA (iom_action, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1076   { UDATA (iom_action, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL }
1077 #else
1078   [0 ... N_IOM_UNITS_MAX - 1] = {
1079     UDATA (iom_action, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
1080   }
1081 #endif
1082 };
1083 
1084 static t_stat iom_show_mbx (UNUSED FILE * st,
     /* [previous][next][first][last][top][bottom][index][help] */
1085                             UNUSED UNIT * uptr, UNUSED int val,
1086                             UNUSED const void * desc)
1087   {
1088     return SCPE_OK;
1089   }
1090 
1091 static t_stat iom_show_units (UNUSED FILE * st, UNUSED UNIT * uptr, UNUSED int val, UNUSED const void * desc)
     /* [previous][next][first][last][top][bottom][index][help] */
1092   {
1093     sim_printf ("Number of IOM units in system is %d\r\n", iom_dev.numunits);
1094     return SCPE_OK;
1095   }
1096 
1097 static t_stat iom_set_units (UNUSED UNIT * uptr, UNUSED int value, const char * cptr, UNUSED void * desc)
     /* [previous][next][first][last][top][bottom][index][help] */
1098   {
1099     if (! cptr)
1100       return SCPE_ARG;
1101     int n = atoi (cptr);
1102     if (n < 1 || n > N_IOM_UNITS_MAX)
1103       return SCPE_ARG;
1104     // This was true for early Multics releases.
1105     //if (n > 2)
1106       //sim_printf ("Warning: Multics supports 2 IOMs maximum\r\n");
1107     iom_dev.numunits = (unsigned) n;
1108     return SCPE_OK;
1109   }
1110 
1111 static t_stat iom_show_config (UNUSED FILE * st, UNIT * uptr, UNUSED int val,
     /* [previous][next][first][last][top][bottom][index][help] */
1112                              UNUSED const void * desc)
1113   {
1114     uint iom_unit_idx = (uint) IOM_UNIT_IDX (uptr);
1115     if (iom_unit_idx >= iom_dev.numunits)
1116       {
1117         sim_printf ("error: Invalid unit number %lu\r\n", (unsigned long) iom_unit_idx);
1118         return SCPE_ARG;
1119       }
1120 
1121     sim_printf ("IOM unit number %lu\r\n", (unsigned long) iom_unit_idx);
1122     iom_unit_data_t * p = iom_unit_data + iom_unit_idx;
1123 
1124     char * os = "<out of range>";
1125     switch (p -> config_sw_OS)
1126       {
1127         case CONFIG_SW_STD_GCOS:
1128           os = "Standard GCOS";
1129           break;
1130         case CONFIG_SW_EXT_GCOS:
1131           os = "Extended GCOS";
1132           break;
1133         case CONFIG_SW_MULTICS:
1134           os = "Multics";
1135           break;
1136       }
1137     char * blct = "<out of range>";
1138     switch (p -> configSwBootloadCardTape)
1139       {
1140         case CONFIG_SW_BLCT_CARD:
1141           blct = "CARD";
1142           break;
1143         case CONFIG_SW_BLCT_TAPE:
1144           blct = "TAPE";
1145           break;
1146       }
1147 
1148     sim_printf ("Allowed Operating System: %s\r\n", os);
1149     sim_printf ("IOM Base Address:         %03o(8)\r\n", p -> configSwIomBaseAddress);
1150     sim_printf ("Multiplex Base Address:   %04o(8)\r\n", p -> configSwMultiplexBaseAddress);
1151     sim_printf ("Bootload Card/Tape:       %s\r\n", blct);
1152     sim_printf ("Bootload Tape Channel:    %02o(8)\r\n", p -> configSwBootloadMagtapeChan);
1153     sim_printf ("Bootload Card Channel:    %02o(8)\r\n", p -> configSwBootloadCardrdrChan);
1154     sim_printf ("Bootload Port:            %02o(8)\r\n", p -> configSwBootloadPort);
1155     sim_printf ("Port Address:            ");
1156     int i;
1157     for (i = 0; i < N_IOM_PORTS; i ++)
1158       sim_printf (" %03o", p -> configSwPortAddress[i]);
1159     sim_printf ("\r\n");
1160     sim_printf ("Port Interlace:          ");
1161     for (i = 0; i < N_IOM_PORTS; i ++)
1162       sim_printf (" %3o", p -> configSwPortInterface[i]);
1163     sim_printf ("\r\n");
1164     sim_printf ("Port Enable:             ");
1165     for (i = 0; i < N_IOM_PORTS; i ++)
1166       sim_printf (" %3o", p -> configSwPortEnable[i]);
1167     sim_printf ("\r\n");
1168     sim_printf ("Port Sysinit Enable:     ");
1169     for (i = 0; i < N_IOM_PORTS; i ++)
1170       sim_printf (" %3o", p -> configSwPortSysinitEnable[i]);
1171     sim_printf ("\r\n");
1172     sim_printf ("Port Halfsize:           ");
1173     for (i = 0; i < N_IOM_PORTS; i ++)
1174       sim_printf (" %3o", p -> configSwPortHalfsize[i]);
1175     sim_printf ("\r\n");
1176     sim_printf ("Port Storesize:          ");
1177     for (i = 0; i < N_IOM_PORTS; i ++)
1178       sim_printf (" %3o", p -> configSwPortStoresize[i]);
1179     sim_printf ("\r\n");
1180     sim_printf ("Use port map:             %s\r\n", p -> useMap ? "yes" : "no");
1181 
1182     return SCPE_OK;
1183   }
1184 
1185 //
1186 // set iom0 config=<blah> [;<blah>]
1187 //
1188 //    blah = iom_base=n
1189 //           multiplex_base=n
1190 //           os=gcos | gcosext | multics
1191 //           boot=card | tape
1192 //           tapechan=n
1193 //           cardchan=n
1194 //           scuport=n
1195 //           port=n   // set port number for below commands
1196 //             addr=n
1197 //             interlace=n
1198 //             enable=n
1199 //             initenable=n
1200 //             halfsize=n
1201 //           usemap=n   // map addresses through the port switches
1202 //             storesize=n
1203 //          bootskip=n // Hack: forward skip n records after reading boot record
1204 
1205 static config_value_list_t cfg_model_list[] =
1206   {
1207     { "iom", CONFIG_SW_MODEL_IOM },
1208     { "imu", CONFIG_SW_MODEL_IMU }
1209   };
1210 
1211 static config_value_list_t cfg_os_list[] =
1212   {
1213     { "gcos",    CONFIG_SW_STD_GCOS },
1214     { "gcosext", CONFIG_SW_EXT_GCOS },
1215     { "multics", CONFIG_SW_MULTICS  },
1216     { NULL,      0 }
1217   };
1218 
1219 static config_value_list_t cfg_boot_list[] =
1220   {
1221     { "card", CONFIG_SW_BLCT_CARD },
1222     { "tape", CONFIG_SW_BLCT_TAPE },
1223     { NULL,   0 }
1224   };
1225 
1226 static config_value_list_t cfg_base_list[] =
1227   {
1228     { "multics",  014 },
1229     { "multics1", 014 }, // boot iom
1230     { "multics2", 020 },
1231     { "multics3", 024 },
1232     { "multics4", 030 },
1233     { NULL, 0 }
1234   };
1235 
1236 static config_value_list_t cfg_size_list[] =
1237   {
1238     { "32",    0 },
1239     { "64",    1 },
1240     { "128",   2 },
1241     { "256",   3 },
1242     { "512",   4 },
1243     { "1024",  5 },
1244     { "2048",  6 },
1245     { "4096",  7 },
1246     { "32K",   0 },
1247     { "64K",   1 },
1248     { "128K",  2 },
1249     { "256K",  3 },
1250     { "512K",  4 },
1251     { "1024K", 5 },
1252     { "2048K", 6 },
1253     { "4096K", 7 },
1254     { "1M",    5 },
1255     { "2M",    6 },
1256     { "4M",    7 },
1257     { NULL,    0 }
1258   };
1259 
1260 static config_value_list_t cfg_on_off[] =
1261   {
1262     { "off",     0 },
1263     { "on",      1 },
1264     { "disable", 0 },
1265     { "enable",  1 },
1266     { NULL,      0 }
1267   };
1268 
1269 static config_list_t iom_config_list[] =
1270   {
1271     { "model",          1, 0,               cfg_model_list },
1272     { "os",             1, 0,               cfg_os_list    },
1273     { "boot",           1, 0,               cfg_boot_list  },
1274     { "iom_base",       0, 07777,           cfg_base_list  },
1275     { "multiplex_base", 0, 0777,            NULL           },
1276     { "tapechan",       0, 077,             NULL           },
1277     { "cardchan",       0, 077,             NULL           },
1278     { "scuport",        0, 07,              NULL           },
1279     { "port",           0, N_IOM_PORTS - 1, NULL           },
1280     { "addr",           0, 7,               NULL           },
1281     { "interlace",      0, 1,               NULL           },
1282     { "enable",         0, 1,               NULL           },
1283     { "initenable",     0, 1,               NULL           },
1284     { "halfsize",       0, 1,               NULL           },
1285     { "store_size",     0, 7,               cfg_size_list  },
1286     { "usemap",         0, 1,               cfg_on_off     },
1287     { NULL,             0, 0,               NULL           }
1288   };
1289 
1290 static t_stat iom_set_config (UNIT * uptr, UNUSED int value, const char * cptr, UNUSED void * desc)
     /* [previous][next][first][last][top][bottom][index][help] */
1291   {
1292     uint iom_unit_idx = (uint) IOM_UNIT_IDX (uptr);
1293     if (iom_unit_idx >= iom_dev.numunits)
1294       {
1295         sim_printf ("error: %s: Invalid unit number %ld\r\n", __func__, (long) iom_unit_idx);
1296         return SCPE_ARG;
1297       }
1298 
1299     iom_unit_data_t * p = iom_unit_data + iom_unit_idx;
1300 
1301     static uint port_num = 0;
1302 
1303     config_state_t cfg_state = { NULL, NULL };
1304 
1305     iom_addr_map_changed (iom_unit_idx);
1306     for (;;)
1307       {
1308         int64_t v;
1309         int rc = cfg_parse (__func__, cptr, iom_config_list, & cfg_state, & v);
1310 
1311         if (rc == -1) // done
1312           break;
1313 
1314         if (rc == -2) // error
1315           {
1316             cfg_parse_done (& cfg_state);
1317             continue;
1318           }
1319         const char * name = iom_config_list[rc].name;
1320 
1321         if (strcmp (name, "model") == 0)
1322           {
1323             p -> config_sw_model = (enum config_sw_model_t) v;
1324             continue;
1325           }
1326 
1327         if (strcmp (name, "os") == 0)
1328           {
1329             p -> config_sw_OS = (enum config_sw_OS_t) v;
1330             continue;
1331           }
1332 
1333         if (strcmp (name, "boot") == 0)
1334           {
1335             p -> configSwBootloadCardTape = (enum config_sw_bootload_device_e) v;
1336             continue;
1337           }
1338 
1339         if (strcmp (name, "iom_base") == 0)
1340           {
1341             p -> configSwIomBaseAddress = (word12) v;
1342             continue;
1343           }
1344 
1345         if (strcmp (name, "multiplex_base") == 0)
1346           {
1347             // The IOM number is in the low 2 bits
1348             // The address is in the high 7 bits which are mapped
1349             // to bits 12 to 18 of a 24 bit address
1350             //
1351 //  0  1  2  3  4  5  6  7  8
1352 //  x  x  x  x  x  x  x  y  y
1353 //
1354 //  Address
1355 //  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 29 20 21 22 23 24
1356 //  0  0  0  0  0  0  0  0  0  0  0  0  x  x  x  x  x  x  x  0  0  0  0  0  0
1357 //
1358 // IOM number
1359 //
1360 //  0  1
1361 //  y  y
1362 
1363             p -> configSwMultiplexBaseAddress = (word9) v;
1364             continue;
1365           }
1366 
1367         if (strcmp (name, "tapechan") == 0)
1368           {
1369             p -> configSwBootloadMagtapeChan = (word6) v;
1370             continue;
1371           }
1372 
1373         if (strcmp (name, "cardchan") == 0)
1374           {
1375             p -> configSwBootloadCardrdrChan = (word6) v;
1376             continue;
1377           }
1378 
1379         if (strcmp (name, "scuport") == 0)
1380           {
1381             p -> configSwBootloadPort = (word3) v;
1382             continue;
1383           }
1384 
1385         if (strcmp (name, "port") == 0)
1386           {
1387             port_num = (uint) v;
1388             continue;
1389           }
1390 
1391         if (strcmp (name, "addr") == 0)
1392           {
1393             p -> configSwPortAddress[port_num] = (uint) v;
1394             continue;
1395           }
1396 
1397         if (strcmp (name, "interlace") == 0)
1398           {
1399             p -> configSwPortInterface[port_num] = (uint) v;
1400             continue;
1401           }
1402 
1403         if (strcmp (name, "enable") == 0)
1404           {
1405             p -> configSwPortEnable[port_num] = (uint) v;
1406             continue;
1407           }
1408 
1409         if (strcmp (name, "initenable") == 0)
1410           {
1411             p -> configSwPortSysinitEnable[port_num] = (uint) v;
1412             continue;
1413           }
1414 
1415         if (strcmp (name, "halfsize") == 0)
1416           {
1417             p -> configSwPortHalfsize[port_num] = (uint) v;
1418             continue;
1419           }
1420 
1421         if (strcmp (name, "store_size") == 0)
1422           {
1423             p -> configSwPortStoresize[port_num] = (uint) v;
1424             continue;
1425           }
1426 
1427         if (strcmp (name, "usemap") == 0)
1428           {
1429             p -> useMap = v != 0;
1430             continue;
1431           }
1432 
1433         sim_printf ("error: %s: Invalid cfg_parse rc <%ld>\r\n", __func__, (long) rc);
1434         cfg_parse_done (& cfg_state);
1435         return SCPE_ARG;
1436       } // process statements
1437     cfg_parse_done (& cfg_state);
1438     return SCPE_OK;
1439   }
1440 
1441 static t_stat iom_reset_unit (UNIT * uptr, UNUSED int32 value, UNUSED const char * cptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1442                        UNUSED void * desc)
1443   {
1444     uint iom_unit_idx = (uint) (uptr - iom_unit);
1445     iom_unit_reset_idx (iom_unit_idx);
1446     return SCPE_OK;
1447   }
1448 
1449 static MTAB iom_mod[] =
1450   {
1451     {
1452        MTAB_XTD | MTAB_VUN | \
1453        MTAB_NMO | MTAB_NC,       /* Mask               */
1454       0,                         /* Match              */
1455       "MBX",                     /* Print string       */
1456       NULL,                      /* Match string       */
1457       NULL,                      /* Validation routine */
1458       iom_show_mbx,              /* Display routine    */
1459       NULL,                      /* Value descriptor   */
1460       NULL                       /* Help string        */
1461     },
1462     {
1463       MTAB_XTD | MTAB_VUN | \
1464       MTAB_NMO | MTAB_VALR,      /* Mask               */
1465       0,                         /* Match              */
1466       "CONFIG",                  /* Print string       */
1467       "CONFIG",                  /* Match string       */
1468       iom_set_config,            /* Validation routine */
1469       iom_show_config,           /* Display routine    */
1470       NULL,                      /* Value descriptor   */
1471       NULL                       /* Help string        */
1472     },
1473     {
1474       MTAB_XTD | MTAB_VUN | \
1475       MTAB_NMO | MTAB_VALR,      /* Mask               */
1476       0,                         /* Match              */
1477       (char *) "RESET",          /* Print string       */
1478       (char *) "RESET",          /* Match string       */
1479       iom_reset_unit,            /* Validation routine */
1480       NULL,                      /* Display routine    */
1481       (char *) "reset IOM unit", /* Value descriptor   */
1482       NULL                       /* Help               */
1483     },
1484     {
1485       MTAB_XTD | MTAB_VDV | \
1486       MTAB_NMO | MTAB_VALR,                 /* Mask               */
1487       0,                                    /* Match              */
1488       "NUNITS",                             /* Print string       */
1489       "NUNITS",                             /* Match string       */
1490       iom_set_units,                        /* Validation routine */
1491       iom_show_units,                       /* Display routine    */
1492       "Number of IOM units in the system",  /* Value descriptor   */
1493       NULL                                  /* Help string        */
1494     },
1495     {
1496       0, 0, NULL, NULL, 0, 0, NULL, NULL
1497     }
1498   };
1499 
1500 static DEBTAB iom_dt[] =
1501   {
1502     { "NOTIFY", DBG_NOTIFY, NULL },
1503     { "INFO",   DBG_INFO,   NULL },
1504     { "ERR",    DBG_ERR,    NULL },
1505     { "WARN",   DBG_WARN,   NULL },
1506     { "DEBUG",  DBG_DEBUG,  NULL },
1507     { "TRACE",  DBG_TRACE,  NULL },
1508     { "ALL",    DBG_ALL,    NULL }, // Don't move as it messes up DBG message
1509     { NULL,     0,          NULL }
1510   };
1511 
1512 //
1513 // iom_unit_reset_idx ()
1514 //
1515 //  Reset -- Reset to initial state -- clear all device flags and cancel any
1516 //  any outstanding timing operations. Used by RESET, RUN, and BOOT commands
1517 //
1518 //  Note that all reset()s run after once-only init().
1519 //
1520 //
1521 
1522 t_stat iom_unit_reset_idx (uint iom_unit_idx)
     /* [previous][next][first][last][top][bottom][index][help] */
1523   {
1524     if (iom_unit_idx < N_IOM_UNITS_MAX)
1525       iom_build_addr_map (iom_unit_idx);
1526     return SCPE_OK;
1527   }
1528 
1529 static t_stat iom_reset (UNUSED DEVICE * dptr)
     /* [previous][next][first][last][top][bottom][index][help] */
1530   {
1531     //sim_debug (DBG_INFO, & iom_dev, "%s: running.\r\n", __func__);
1532 
1533     for (uint iom_unit_idx = 0; iom_unit_idx < iom_dev.numunits; iom_unit_idx ++)
1534       {
1535         iom_unit_reset_idx (iom_unit_idx);
1536       }
1537     return SCPE_OK;
1538   }
1539 
1540 static t_stat boot_svc (UNIT * unitp);
1541 static UNIT boot_channel_unit[N_IOM_UNITS_MAX] = {
1542 #if defined(NO_C_ELLIPSIS)
1543   { UDATA (& boot_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1544   { UDATA (& boot_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1545   { UDATA (& boot_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1546   { UDATA (& boot_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL }
1547 #else
1548   [0 ... N_IOM_UNITS_MAX - 1] = {
1549     UDATA (& boot_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
1550   }
1551 #endif
1552 };
1553 
1554 // The bootload program goes straight into memory: purge each word it
1555 // stores from every CPU's cache, as any IOM store does. (The BOOT command
1556 // resets the CPUs, emptying their caches, but a front panel boot does
1557 // not.)
1558 static void iom_boot_store (uint iom_unit_idx, word24 addr, word36 data)
     /* [previous][next][first][last][top][bottom][index][help] */
1559   {
1560     uint real = iom_map_addr (iom_unit_idx, addr);
1561     M[real] = data;
1562     iom_cache_purge ((word24) real);
1563   }
1564 
1565 /*
1566  * init_memory_iom ()
1567  *
1568  * Load a few words into memory.   Simulates pressing the BOOTLOAD button
1569  * on an IOM or equivalent.
1570  *
1571  * All values are from bootload_tape_label.alm.  See the comments at the
1572  * top of that file.  See also doc #43A239854.
1573  *
1574  * NOTE: The values used here are for an IOM, not an IOX.
1575  * See init_memory_iox () below.
1576  *
1577  */
1578 
1579 static void init_memory_iom (uint iom_unit_idx)
     /* [previous][next][first][last][top][bottom][index][help] */
1580   {
1581     // The presence of a 0 in the top six bits of word 0 denote an IOM boot
1582     // from an IOX boot
1583 
1584     // " The channel number ("Chan#") is set by the switches on the IOM to be
1585     // " the channel for the tape subsystem holding the bootload tape. The
1586     // " drive number for the bootload tape is set by switches on the tape
1587     // " MPC itself.
1588 
1589     //sim_debug (DBG_INFO, & iom_dev,
1590       //"%s: Performing load of eleven words from IOM %c bootchannel to memory.\r\n",
1591       //__func__, iomChar (iom_unit_idx));
1592 
1593     word12 base = iom_unit_data[iom_unit_idx].configSwIomBaseAddress;
1594 
1595     // bootload_io.alm insists that pi_base match
1596     // template_slt_$iom_mailbox_absloc
1597 
1598     //uint pi_base = iom_unit_data[iom_unit_idx].configSwMultiplexBaseAddress & ~3;
1599     word36 pi_base = (((word36)  iom_unit_data[iom_unit_idx].configSwMultiplexBaseAddress)  << 3) |
1600                      (((word36) (iom_unit_data[iom_unit_idx].configSwIomBaseAddress & 07700U)) << 6) ;
1601     word3 iom_num = ((word36) iom_unit_data[iom_unit_idx].configSwMultiplexBaseAddress) & 3;
1602     word36 cmd = 5;       // 6 bits; 05 for tape, 01 for cards
1603     word36 dev = 0;       // 6 bits: drive number
1604 
1605     // is-IMU flag
1606     word36 imu = iom_unit_data[iom_unit_idx].config_sw_model == CONFIG_SW_MODEL_IMU ? 1 : 0;       // 1 bit
1607 
1608     // Description of the bootload channel from 43A239854
1609     //    Legend
1610     //    BB - Bootload channel #
1611     //    C - Cmd (1 or 5)
1612     //    N - IOM #
1613     //    P - Port #
1614     //    XXXX00 - Base Addr -- 01400
1615     //    XXYYYY0 Program Interrupt Base
1616 
1617     enum config_sw_bootload_device_e bootdev = iom_unit_data[iom_unit_idx].configSwBootloadCardTape;
1618 
1619     word6 bootchan;
1620     if (bootdev == CONFIG_SW_BLCT_CARD)
1621       bootchan = iom_unit_data[iom_unit_idx].configSwBootloadCardrdrChan;
1622     else // CONFIG_SW_BLCT_TAPE
1623       bootchan = iom_unit_data[iom_unit_idx].configSwBootloadMagtapeChan;
1624 
1625     // 1
1626     // system fault vector; DIS 0 instruction (imu bit not mentioned by
1627     // 43A239854)
1628 
1629     word36 dis0 = 0616200;
1630 
1631     iom_boot_store (iom_unit_idx, 010 + 2 * iom_num + 0, (imu << 34) | dis0);
1632 
1633     // Simulator specific hack; the simulator flags memory words as
1634     // being "uninitialized"; because the fault pair is fetched as a
1635     // pair, the odd word's uninitialized state may generate a
1636     // run-time warning, even though it is not executed.
1637     //
1638     // By zeroing it here, we clear the flag and avoid the message
1639     // Since the memory will have already been set to zero by
1640     // the "Initialize and clear", this will have no affect
1641     // on the boot operation.
1642 
1643     iom_boot_store (iom_unit_idx, 010 + 2 * iom_num + 1, 0);
1644 
1645     // 2
1646     // terminate interrupt vector (overwritten by bootload)
1647 
1648     iom_boot_store (iom_unit_idx, 030 + 2 * iom_num, dis0);
1649 
1650     // 3
1651     // tally word for sys fault status
1652 
1653     // XXX CAC: Clang -Wsign-conversion claims 'base<<6' is int
1654     word24 base_addr =  (word24) base << 6; // 01400
1655     iom_boot_store (iom_unit_idx, base_addr + 7, ((word36) base_addr << 18) | 02000002);
1656 
1657     // 4
1658     // Fault channel DCW
1659 
1660     // bootload_tape_label.alm says 04000, 43A239854 says 040000.  Since
1661     // 43A239854 says "no change", 40000 is correct; 4000 would be a
1662     // large tally
1663 
1664     // Connect channel LPW; points to PCW at 000000
1665     iom_boot_store (iom_unit_idx, base_addr + 010, 040000);
1666 
1667     // 5
1668     // Boot device LPW; points to IDCW at 000003
1669 
1670     word24 mbx = base_addr + 4u * bootchan;
1671     iom_boot_store (iom_unit_idx, mbx, 03020003);
1672 
1673     // 6
1674     // Second IDCW: IOTD to loc 30 (startup fault vector)
1675 
1676     iom_boot_store (iom_unit_idx, 4, 030 << 18);
1677 
1678     // 7
1679     // Default SCW points at unused first mailbox.
1680 
1681     // T&D tape overwrites this before the first status is saved, though.
1682     // CAC: But the status is never saved, only a $CON occurs, never
1683     // a status service
1684 
1685     // SCW
1686 
1687     iom_boot_store (iom_unit_idx, mbx + 2, ((word36)base_addr << 18));
1688 
1689     // 8
1690     // 1st word of bootload channel PCW
1691 
1692     iom_boot_store (iom_unit_idx, 0, 0720201);
1693 
1694     // 9
1695     // 2nd word of PCW pair
1696 
1697     // SCU port
1698     word3 port = iom_unit_data[iom_unit_idx].configSwBootloadPort; // 3 bits;
1699 
1700     // Why does bootload_tape_label.alm claim that a port number belongs in
1701     // the low bits of the 2nd word of the PCW?  The lower 27 bits of the
1702     // odd word of a PCW should be all zero.
1703 
1704     //[CAC] Later, bootload_tape_label.alm does:
1705     //
1706     //     cioc    bootload_info+1 " port # stuck in PCW
1707     //     lda     0,x5            " check for status
1708     //
1709     // So this is a bootloader kludge to pass the bootload SCU number
1710     //
1711 
1712     //[CAC] From Rev01.AN70.archive:
1713     //  In BOS compatibility mode, the BOS BOOT command simulates the IOM,
1714     //  leaving the same information.  However, it also leaves a config deck
1715     //  and flagbox (although bce has its own flagbox) in the usual locations.
1716     //  This allows Bootload Multics to return to BOS if there is a BOS to
1717     //  return to.  The presence of BOS is indicated by the tape drive number
1718     //  being non-zero in the idcw in the "IOM" provided information.  (This is
1719     //  normally zero until firmware is loaded into the bootload tape MPC.)
1720 
1721     iom_boot_store (iom_unit_idx, 1, ((word36) (bootchan) << 27) | port);
1722 
1723     // 10
1724     // word after PCW (used by program)
1725 
1726     iom_boot_store (iom_unit_idx, 2, ((word36) base_addr << 18) | (pi_base) | iom_num);
1727 
1728     // 11
1729     // IDCW for read binary
1730 
1731     iom_boot_store (iom_unit_idx, 3, (cmd << 30) | (dev << 24) | 0700000);
1732   }
1733 
1734 static t_stat boot_svc (UNIT * unitp)
     /* [previous][next][first][last][top][bottom][index][help] */
1735   {
1736     uint iom_unit_idx = (uint) (unitp - boot_channel_unit);
1737     // the docs say press sysinit, then boot; we don't have an
1738     // explicit "sysinit", so we treat "reset iom" as sysinit.
1739     // The docs don't say what the behavior is if you don't press
1740     // sysinit first so we won't worry about it.
1741 
1742     //sim_debug (DBG_DEBUG, & iom_dev, "%s: starting on IOM %c\r\n",
1743     //__func__, iomChar (iom_unit_idx));
1744 
1745     // This is needed to reset the interrupt mask registers; Multics tampers
1746     // with runtime values, and mucks up rebooting on multi-CPU systems.
1747     //scu_reset (NULL);
1748     for (int port_num = 0; port_num < N_SCU_PORTS; port_num ++)
1749       {
1750         if (! cables->iom_to_scu[iom_unit_idx][port_num].in_use)
1751           continue;
1752         uint scu_unit_idx = cables->iom_to_scu[iom_unit_idx][port_num].scu_unit_idx;
1753         scu_unit_reset ((int) scu_unit_idx);
1754       }
1755 
1756     // initialize memory with boot program
1757     init_memory_iom (iom_unit_idx);
1758 
1759     // Start the remote console listener
1760     startRemoteConsole ();
1761 
1762     // Start the FNP dialup listener
1763     startFNPListener ();
1764 
1765     // simulate $CON
1766     // XXX Making the assumption that low memory is connected to port 0, ..., high to 3
1767     if (! cables->iom_to_scu[iom_unit_idx][0].in_use)
1768       {
1769         sim_warn ("boot iom can't find a SCU\r\n");
1770         return SCPE_ARG;
1771       }
1772     uint scu_unit_idx = cables->iom_to_scu[iom_unit_idx][0].scu_unit_idx;
1773     iom_interrupt (scu_unit_idx, iom_unit_idx);
1774 
1775     //sim_debug (DBG_DEBUG, &iom_dev, "%s finished\r\n", __func__);
1776 
1777     // returning OK from the BOOT command causes the CPU start
1778     return SCPE_OK;
1779   }
1780 
1781 static t_stat iom_boot (int unitNum, UNUSED DEVICE * dptr)
     /* [previous][next][first][last][top][bottom][index][help] */
1782   {
1783     if (unitNum < 0 || unitNum >= (int) iom_dev.numunits)
1784       {
1785         sim_printf ("%s: Invalid unit number %d\r\n", __func__, unitNum);
1786         return SCPE_ARG;
1787       }
1788     //uint iom_unit_idx = (uint) unitNum;
1789     boot_svc (& boot_channel_unit[unitNum]);
1790     return SCPE_OK;
1791   }
1792 
1793 DEVICE iom_dev =
1794   {
1795     "IOM",        /* name                */
1796     iom_unit,     /* units               */
1797     NULL,         /* registers           */
1798     iom_mod,      /* modifiers           */
1799     N_IOM_UNITS,  /* #units              */
1800     10,           /* address radix       */
1801     8,            /* address width       */
1802     1,            /* address increment   */
1803     8,            /* data radix          */
1804     8,            /* data width          */
1805     NULL,         /* examine routine     */
1806     NULL,         /* deposit routine     */
1807     iom_reset,    /* reset routine       */
1808     iom_boot,     /* boot routine        */
1809     NULL,         /* attach routine      */
1810     NULL,         /* detach routine      */
1811     NULL,         /* context             */
1812     DEV_DEBUG,    /* flags               */
1813     0,            /* debug control flags */
1814     iom_dt,       /* debug flag names    */
1815     NULL,         /* memory size change  */
1816     NULL,         /* logical name        */
1817     NULL,         /* help                */
1818     NULL,         /* attach help         */
1819     NULL,         /* help context        */
1820     NULL,         /* description         */
1821     NULL          /* end                 */
1822   };
1823 
1824 static uint mbxLoc (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
1825   {
1826 // IDX is correct here as computation is based on physical unit
1827 // configuration switches
1828     word12 base      = iom_unit_data[iom_unit_idx].configSwIomBaseAddress;
1829     word24 base_addr = ((word24) base) << 6; // 01400
1830     word24 mbx       = base_addr + 4 * chan;
1831     //sim_debug (DBG_DEBUG, & iom_dev, "%s: IOM %c, chan %d is %012o\r\n",
1832       //__func__, iomChar (iom_unit_idx), chan, mbx);
1833     return mbx;
1834   }
1835 
1836 /*
1837  * status_service ()
1838  *
1839  * Write status info into a status mailbox.
1840  *
1841  * BUG: Only partially implemented.
1842  * WARNING: The diag tape will crash because we don't write a non-zero
1843  * value to the low 4 bits of the first status word.  See comments
1844  * at the top of mt.c. [CAC] Not true. The IIOC writes those bits to
1845  * tell the bootloader code whether the boot came from an IOM or IIOC.
1846  * The connect channel does not write status bits. The diag tape crash
1847  * was due so some other issue.
1848  *
1849  */
1850 
1851 // According to gtss_io_status_words.incl.pl1
1852 //
1853 //,     3 WORD1
1854 //,       4 Termination_indicator         bit(01)unal
1855 //,       4 Power_bit                     bit(01)unal
1856 //,       4 Major_status                  bit(04)unal
1857 //,       4 Substatus                     bit(06)unal
1858 //,       4 PSI_channel_odd_even_ind      bit(01)unal
1859 //,       4 Marker_bit_interrupt          bit(01)unal
1860 //,       4 Reserved                      bit(01)unal
1861 //,       4 Lost_interrupt_bit            bit(01)unal
1862 //,       4 Initiate_interrupt_ind        bit(01)unal
1863 //,       4 Abort_indicator               bit(01)unal
1864 //,       4 IOM_status                    bit(06)unal
1865 //,       4 Address_extension_bits        bit(06)unal
1866 //,       4 Record_count_residue          bit(06)unal
1867 //
1868 //,      3 WORD2
1869 //,       4 Data_address_residue          bit(18)unal
1870 //,       4 Character_count               bit(03)unal
1871 //,       4 Read_Write_control_bit        bit(01)unal
1872 //,       4 Action_code                   bit(02)unal
1873 //,       4 Word_count_residue            bit(12)unal
1874 
1875 // iom_stat.incl.pl1
1876 //
1877 // (2 t bit (1),             /* set to "1"b by IOM                         */
1878 //  2 power bit (1),         /* non-zero if peripheral absent or power off */
1879 //  2 major bit (4),         /* major status                               */
1880 //  2 sub bit (6),           /* substatus                                  */
1881 //  2 eo bit (1),            /* even/odd bit                               */
1882 //  2 marker bit (1),        /* non-zero if marker status                  */
1883 //  2 soft bit (2),          /* software status                            */
1884 //  2 initiate bit (1),      /* initiate bit                               */
1885 //  2 abort bit (1),         /* software abort bit                         */
1886 //  2 channel_stat bit (3),  /* IOM channel status                         */
1887 //  2 central_stat bit (3),  /* IOM central status                         */
1888 //  2 mbz bit (6),
1889 //  2 rcount bit (6),        /* record count residue                       */
1890 //
1891 //  2 address bit (18),      /* DCW address residue                        */
1892 //  2 char_pos bit (3),      /* character position residue                 */
1893 //  2 r bit (1),             /* non-zero if reading                        */
1894 //  2 type bit (2),          /* type of last DCW                           */
1895 //  2 tally bit (12)) unal;  /* DCW tally residue                          */
1896 
1897 // Searching the Multics source indicates that
1898 //   eo is  used by tape
1899 //   marker is used by tape and printer
1900 //   soft is set by tolts as /* set "timeout" */'
1901 //   soft is reported by tape, but not used.
1902 //   initiate is used by tape and printer
1903 //   abort is reported by tape, but not used
1904 //   char_pos is used by tape, console
1905 
1906 // pg B26: "The DCW residues stored in the status in page mode will
1907 // represent the next absolute address (bits 6-23) of data prior to
1908 // the application of the Page Table Word"
1909 
1910 static int status_service (uint iom_unit_idx, uint chan, bool marker)
     /* [previous][next][first][last][top][bottom][index][help] */
1911   {
1912 #if defined(TESTING)
1913     cpu_state_t * cpup = _cpup;
1914 #endif
1915     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
1916     // See page 33 and AN87 for format of y-pair of status info
1917 
1918     // BUG: much of the following is not tracked
1919 
1920     word36 word1, word2;
1921     word1 = 0;
1922     putbits36_12 (& word1, 0, p -> stati);
1923     // isOdd can be set to zero; see
1924     //   https://ringzero.wikidot.com/wiki:cac-2015-10-22
1925     //putbits36_1 (& word1, 12, p -> isOdd ? 0 : 1);
1926     putbits36_1 (& word1, 13, marker ? 1 : 0);
1927     putbits36_2 (& word1, 14, 0); // software status
1928     putbits36_1 (& word1, 16, p -> initiate ? 1 : 0);
1929     putbits36_1 (& word1, 17, 0); // software abort bit
1930     putbits36_3 (& word1, 18, (word3) p -> chanStatus);
1931     //putbits36_3 (& word1, 21, iom_unit_data[iom_unit_idx].iomStatus);
1932     putbits36_3 (& word1, 21, 0);
1933 
1934 
1935 
1936 
1937     putbits36_6 (& word1, 30, p -> recordResidue);
1938 
1939     word2 = 0;
1940 
1941 
1942 
1943 
1944 
1945     putbits36_3  (& word2, 18, p -> charPos);
1946     putbits36_1  (& word2, 21, p -> isRead ? 1 : 0);
1947     putbits36_12 (& word2, 24, p -> tallyResidue);
1948 
1949     // BUG: need to write to mailbox queue
1950 
1951     // T&D tape does *not* expect us to cache original SCW, it expects us to
1952     // use the SCW loaded from tape.
1953 
1954     uint chanloc   = mbxLoc (iom_unit_idx, chan);
1955     word24 scwAddr = chanloc + IOM_MBX_SCW;
1956     word36 scw;
1957     iom_core_read_lock (iom_unit_idx, scwAddr, & scw, __func__);
1958     word18 addr    = getbits36_18 (scw, 0);   // absolute
1959     uint lq        = getbits36_2 (scw, 18);
1960     uint tally     = getbits36_12 (scw, 24);
1961 
1962     sim_debug (DBG_DEBUG, & iom_dev, "%s: Status: 0%012"PRIo64" 0%012"PRIo64"\r\n",
1963                __func__, word1, word2);
1964     if (lq == 3)
1965       {
1966         sim_debug (DBG_WARN, &iom_dev,
1967                    "%s: SCW for channel %d has illegal LQ\r\n",
1968                    __func__, chan);
1969         lq = 0;
1970       }
1971     iom_core_write2 (iom_unit_idx, addr, word1, word2, __func__);
1972 
1973     // XXX: PVS-Studio believes that tally is always == 0!?
1974     if (tally > 0 || (tally == 0 && lq != 0)) //-V560
1975       {
1976         switch (lq)
1977           {
1978             case 0:
1979               // list
1980               if (tally != 0)
1981                 {
1982                   tally --;
1983                   addr += 2;
1984                 }
1985               break;
1986 
1987             case 1:
1988               // 4 entry (8 word) queue
1989               if (tally % 8 == 1 /* || tally % 8 == -1 */)
1990                 addr -= 8;
1991               else
1992                 addr += 2;
1993               tally --;
1994               break;
1995 
1996             case 2:
1997               // 16 entry (32 word) queue
1998               if (tally % 32 == 1 /* || tally % 32 == -1 */)
1999                 addr -= 32;
2000               else
2001                 addr += 2;
2002               tally --;
2003               break;
2004           }
2005 
2006        // tally is 12 bit math
2007        if (tally & ~07777U)
2008           {
2009             sim_debug (DBG_WARN, & iom_dev,
2010                        "%s: Tally SCW address 0%o under/over flow\r\n",
2011                        __func__, tally);
2012             tally &= 07777;
2013           }
2014 
2015         putbits36_12 (& scw, 24, (word12) tally);
2016         putbits36_18 (& scw, 0, addr);
2017       }
2018 
2019     iom_core_write_unlock (iom_unit_idx, scwAddr, scw, __func__);
2020 
2021     // BUG: update SCW in core
2022     return 0;
2023   }
2024 
2025 static word24 UNUSED build_AUXPTW_address (uint iom_unit_idx, int chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2026   {
2027 //    0      5 6            16 17  18              21  22  23
2028 //   ---------------------------------------------------------
2029 //   | Zeroes | IOM Base Address  |                          |
2030 //   ---------------------------------------------------------
2031 //                         |    Channel Number       | 1 | 1 |
2032 //                         -----------------------------------
2033 // XXX Assuming 16 and 17 are or'ed. Pg B4 doesn't specify
2034 
2035     word12 IOMBaseAddress  = iom_unit_data[iom_unit_idx].configSwIomBaseAddress;
2036     word24 addr            = (((word24) IOMBaseAddress) & MASK12) << 6;
2037     addr                  |= ((uint) chan & MASK6) << 2;
2038     addr                  |= 03;
2039     return addr;
2040   }
2041 
2042 static word24 build_DDSPTW_address (word18 PCW_PAGE_TABLE_PTR, word8 pageNumber)
     /* [previous][next][first][last][top][bottom][index][help] */
2043   {
2044 //    0      5 6        15  16  17  18                       23
2045 //   ----------------------------------------------------------
2046 //   | Page Table Pointer | 0 | 0 |                           |
2047 //   ----------------------------------------------------------
2048 // or
2049 //                        -------------------------------------
2050 //                        |  Direct chan addr 6-13            |
2051 //                        -------------------------------------
2052     if (PCW_PAGE_TABLE_PTR & 3)
2053       sim_warn ("%s: pcw_ptp %#o page_no  %#o\r\n", __func__, PCW_PAGE_TABLE_PTR, pageNumber);
2054     word24 addr = (((word24) PCW_PAGE_TABLE_PTR) & MASK18) << 6;
2055     addr |= pageNumber;
2056     return addr;
2057   }
2058 
2059 // Fetch Direct Data Service Page Table Word
2060 
2061 static void fetch_DDSPTW (uint iom_unit_idx, int chan, word18 addr)
     /* [previous][next][first][last][top][bottom][index][help] */
2062   {
2063     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2064     word24 pgte         = build_DDSPTW_address (p -> PCW_PAGE_TABLE_PTR,
2065                                       (addr >> 10) & MASK8);
2066     iom_core_read (iom_unit_idx, pgte, (word36 *) & p -> PTW_DCW, __func__);
2067     if ((p -> PTW_DCW & 0740000777747) != 04llu)
2068       sim_warn ("%s: chan %ld addr %#llo pgte %08llo ptw %012llo\r\n",
2069                 __func__, (long)chan, (unsigned long long)addr,
2070                 (unsigned long long)pgte, (unsigned long long)p -> PTW_DCW);
2071   }
2072 
2073 static word24 build_IDSPTW_address (word18 PCW_PAGE_TABLE_PTR, word1 seg, word8 pageNumber)
     /* [previous][next][first][last][top][bottom][index][help] */
2074   {
2075 //    0      5 6        15  16  17  18                       23
2076 //   ----------------------------------------------------------
2077 //   | Page Table Pointer         |                           |
2078 //   ----------------------------------------------------------
2079 // plus
2080 //                    ----
2081 //                    | S |
2082 //                    | E |
2083 //                    | G |
2084 //                    -----
2085 // plus
2086 //                        -------------------------------------
2087 //                        |         DCW 0-7                   |
2088 //                        -------------------------------------
2089 
2090     word24 addr  = (((word24) PCW_PAGE_TABLE_PTR) & MASK18) << 6;
2091     addr        += (((word24) seg) & 01) << 8;
2092     addr        += pageNumber;
2093     return addr;
2094   }
2095 
2096 // Fetch Indirect Data Service Page Table Word
2097 
2098 static void fetch_IDSPTW (uint iom_unit_idx, int chan, word18 addr)
     /* [previous][next][first][last][top][bottom][index][help] */
2099   {
2100     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2101     word24 pgte         = build_IDSPTW_address (p -> PCW_PAGE_TABLE_PTR,
2102                                       p -> SEG,
2103                                       (addr >> 10) & MASK8);
2104     iom_core_read (iom_unit_idx, pgte, (word36 *) & p -> PTW_DCW, __func__);
2105     if ((p -> PTW_DCW & 0740000777747) != 04llu)
2106       sim_warn ("%s: chan %d addr %#o ptw %012llo\r\n",
2107                 __func__, chan, addr, (unsigned long long)p -> PTW_DCW);
2108   }
2109 
2110 static word24 build_LPWPTW_address (word18 PCW_PAGE_TABLE_PTR, word1 seg, word8 pageNumber)
     /* [previous][next][first][last][top][bottom][index][help] */
2111   {
2112 //    0      5 6        15  16  17  18                       23
2113 //   ----------------------------------------------------------
2114 //   | Page Table Pointer         |                           |
2115 //   ----------------------------------------------------------
2116 // plus
2117 //                    ----
2118 //                    | S |
2119 //                    | E |
2120 //                    | G |
2121 //                    -----
2122 // plus
2123 //                        -------------------------------------
2124 //                        |         LPW 0-7                   |
2125 //                        -------------------------------------
2126 
2127     word24 addr  = (((word24) PCW_PAGE_TABLE_PTR) & MASK18) << 6;
2128     addr        += (((word24) seg) & 01) << 8;
2129     addr        += pageNumber;
2130     return addr;
2131   }
2132 
2133 static void fetch_LPWPTW (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2134   {
2135     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2136     word24 addr         = build_LPWPTW_address (p -> PCW_PAGE_TABLE_PTR,
2137                                       p -> SEG,
2138                                       (p -> LPW_DCW_PTR >> 10) & MASK8);
2139     iom_core_read (iom_unit_idx, addr, (word36 *) & p -> PTW_LPW, __func__);
2140     if ((p -> PTW_LPW & 0740000777747) != 04llu)
2141       sim_warn ("%s: chan %d addr %#o ptw %012llo\r\n",
2142                 __func__, chan, addr, (unsigned long long)p -> PTW_LPW);
2143   }
2144 
2145 // 'write' means peripheral write; i.e. the peripheral is writing to core after
2146 // reading media.
2147 
2148 void iom_direct_data_service (uint iom_unit_idx, uint chan, word24 daddr, word36 * data,
     /* [previous][next][first][last][top][bottom][index][help] */
2149                               iom_direct_data_service_op op)
2150   {
2151     // The direct data service consists of one core storage cycle (Read Clear, Read
2152     // Restore or Clear Write, Double or Single Precision) using an absolute
2153     // 24-bit address supplied by the channel. This service is used by
2154     // "peripherals" capable of providing addresses from an external source,
2155     // such as the 355 Direct Interface Adapter.
2156 
2157     // The PCW received for the channel contains a Page Table Pointer
2158     // which is used as an absolute address pointing to the Page Table which
2159     // has been set up by software for the Direct Channel. All addresses
2160     // for the Direct Channel will be accessed using this Page Table if paging
2161     // has been requested in the PCW.
2162 
2163     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2164 
2165     if (p -> masked)
2166       return;
2167 
2168     if (p -> PCW_63_PTP && p -> PCW_64_PGE)
2169       {
2170         fetch_DDSPTW (iom_unit_idx, (int) chan, daddr);
2171         daddr = ((uint) getbits36_14 (p -> PTW_DCW, 4) << 10) | (daddr & MASK10);
2172       }
2173 
2174     if (op == direct_store)
2175       iom_core_write (iom_unit_idx, daddr, * data, __func__);
2176     else if (op == direct_load)
2177       iom_core_read (iom_unit_idx, daddr, data, __func__);
2178     else if (op == direct_read_clear)
2179       {
2180         iom_core_read_lock (iom_unit_idx, daddr, data, __func__);
2181         iom_core_write_unlock (iom_unit_idx, daddr, 0, __func__);
2182       }
2183   }
2184 
2185 // 'tally' is the transfer size request by Multics.
2186 // For write, '*cnt' is the number of words in 'data'.
2187 // For read, '*cnt' will be set to the number of words transferred.
2188 // Caller responsibility to allocate 'data' large enough to accommodate
2189 // 'tally' words.
2190 void iom_indirect_data_service (uint iom_unit_idx, uint chan, word36 * data, uint * cnt, bool write) {
     /* [previous][next][first][last][top][bottom][index][help] */
2191   iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2192 
2193   if (p->masked)
2194     return;
2195 
2196   // tape_ioi_io.pl1  "If the initiate bit is set in the status, no data was
2197   //                   transferred (no tape movement occurred)."
2198   p->initiate = false;
2199 
2200   uint tally = p->DDCW_TALLY;
2201   uint daddr = p->DDCW_ADDR;
2202   if (tally == 0) {
2203     tally = 4096;
2204   }
2205   p->tallyResidue = (word12) tally;
2206 
2207   if (write) {
2208     uint c = * cnt;
2209     while (p->tallyResidue) {
2210       if (c == 0)
2211         break; // read buffer exhausted; returns w/tallyResidue != 0
2212 
2213       if (! IS_IONTP (p)) {
2214         if (p->PCW_63_PTP && p->PCW_64_PGE) {
2215           fetch_IDSPTW (iom_unit_idx, (int) chan, daddr);
2216           word24 addr = ((word24) (getbits36_14 (p -> PTW_DCW, 4) << 10)) | (daddr & MASK10);
2217           iom_core_write (iom_unit_idx, addr, * data, __func__);
2218         } else {
2219           if (daddr > MASK18) { // 256K overflow
2220             sim_warn ("%s 256K ovf\r\n", __func__); // XXX
2221             daddr &= MASK18;
2222           }
2223 
2224           // If PTP is not set, we are in cm1e or cm2e. Both are 'EXT DCW', so
2225           // we can elide the mode check here.
2226           uint daddr2 = daddr | (uint) p->ADDR_EXT << 18;
2227           iom_core_write (iom_unit_idx, daddr2, * data, __func__);
2228         }
2229       }
2230       daddr ++;
2231       data ++;
2232       p->tallyResidue --;
2233       c --;
2234     }
2235   } else { // read
2236     uint c = 0;
2237     while (p -> tallyResidue) {
2238       if (IS_IONTP (p)) {
2239         * data = 0;
2240       } else {
2241         if (p -> PCW_63_PTP  && p -> PCW_64_PGE) {
2242           fetch_IDSPTW (iom_unit_idx, (int) chan, daddr);
2243           word24 addr = ((word24) (getbits36_14 (p -> PTW_DCW, 4) << 10)) | (daddr & MASK10);
2244           iom_core_read (iom_unit_idx, addr, data, __func__);
2245         } else {
2246           // XXX assuming DCW_ABS
2247           if (daddr > MASK18) { // 256K overflow
2248             sim_warn ("%s 256K ovf\r\n", __func__); // XXX
2249             daddr &= MASK18;
2250           }
2251 
2252           // If PTP is not set, we are in cm1e or cm2e. Both are 'EXT DCW', so
2253           // we can elide the mode check here.
2254           uint daddr2 = daddr | (uint) p -> ADDR_EXT << 18;
2255           iom_core_read (iom_unit_idx, daddr2, data, __func__);
2256         }
2257       }
2258       daddr ++;
2259       p->tallyResidue --;
2260       data ++;
2261       c ++;
2262     }
2263     * cnt = c;
2264   }
2265 }
2266 
2267 // The channel mode after a TDCW with the given PDTA, PDCW/EC and REL bits
2268 // (TDCW bits 32, 33, 35), from mode 'mode'.  Shared by update_chan_mode
2269 // and iom_peek_next_dcw.
2270 
2271 static int tdcw_chan_mode (int mode, word1 pdta, word1 pdcw, word1 rel)
     /* [previous][next][first][last][top][bottom][index][help] */
2272   {
2273     switch (mode)
2274       {
2275         case cm1:
2276           if (pdta)
2277             {
2278               mode = cm2;
2279               break;
2280             }
2281           if (pdcw)
2282             if (rel)
2283               mode = cm4; // 33, 35
2284             else
2285               mode = cm3b; // 33, !35
2286           else
2287             if (rel)
2288               mode = cm3a; // !33, 35
2289             else
2290               mode = cm2; // !33, !35
2291           break;
2292 
2293         case cm2:
2294           if (pdcw)
2295             if (rel)
2296               mode = cm4; // 33, 35
2297             else
2298               mode = cm3b; // 33, !35
2299           else
2300             if (rel)
2301               mode = cm3a; // !33, 35
2302             else
2303               mode = cm2; // !33, !35
2304           break;
2305 
2306         case cm3a:
2307           if (pdcw)
2308             mode = cm4;
2309           break;
2310 
2311         case cm3b:
2312           if (rel)
2313             mode = cm4;
2314           break;
2315 
2316         case cm4:
2317           mode = cm5;
2318           break;
2319 
2320         case cm5:
2321           break;
2322 
2323         case cm1e:
2324           {
2325             if (pdcw)
2326               mode = cm2e;
2327           }
2328           break;
2329 
2330         case cm2e:
2331           break;
2332       } // switch
2333     return mode;
2334   }
2335 
2336 static void update_chan_mode (uint iom_unit_idx, uint chan, bool tdcw)
     /* [previous][next][first][last][top][bottom][index][help] */
2337   {
2338     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2339     if (chan == IOM_CONNECT_CHAN)
2340       {
2341         p -> chanMode = cm1;
2342         return;
2343       }
2344 
2345     if (! tdcw)
2346       {
2347         if (p -> PCW_64_PGE == 0)
2348           {
2349             if (p -> LPW_20_AE == 0)
2350               {
2351                     p -> chanMode = cm1e;  // AE 0
2352               }
2353             else
2354               {
2355                     p -> chanMode = cm2e;  // AE 1
2356               }
2357 
2358           }
2359         else
2360           {
2361             if (p -> LPW_20_AE == 0)
2362               {
2363                 if (p -> LPW_23_REL == 0)
2364                   {
2365                     p -> chanMode = cm1;  // AE 0, REL 0
2366                   }
2367                 else
2368                   {
2369                     p -> chanMode = cm3a;  // AE 0, REL 1
2370                   }
2371 
2372               }
2373             else  // AE 1
2374               {
2375                 if (p -> LPW_23_REL == 0)
2376                   {
2377                     p -> chanMode = cm3b;  // AE 1, REL 0
2378                   }
2379                 else
2380                   {
2381                     p -> chanMode = cm4;  // AE 1, REL 1
2382                   }
2383 
2384               }
2385           }
2386       }
2387     else // tdcw
2388       {
2389         p -> chanMode = tdcw_chan_mode (p -> chanMode, p -> TDCW_32_PDTA,
2390                                         p -> TDCW_33_PDCW, p -> TDCW_35_REL);
2391       } // tdcw
2392   }
2393 
2394 static void write_LPW (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2395   {
2396     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2397 
2398     uint chanLoc = mbxLoc (iom_unit_idx, chan);
2399     iom_core_write (iom_unit_idx, chanLoc + IOM_MBX_LPW, p -> LPW, __func__);
2400     //sim_debug (DBG_DEBUG, & iom_dev,
2401                //"%s: chan %d lpw %012"PRIo64"\r\n",
2402                //__func__, chan, p -> LPW);
2403     if (chan != IOM_CONNECT_CHAN)
2404       {
2405         iom_core_write (iom_unit_idx, chanLoc + IOM_MBX_LPWX, p -> LPWX, __func__);
2406       }
2407   }
2408 
2409 /*
2410  * iom_advance_lpw ()
2411  *
2412  * Advance the channel LPW by one word and write it back to the channel
2413  * mailbox.  Called by the MGP device after delivering async packet data
2414  * via iom_indirect_data_service(), so that io_manager$get_status returns
2415  * next_lpw_offset = LPW_DCW_PTR + 1 instead of LPW_DCW_PTR.  This causes
2416  * ioi_masked to compute:
2417  *   auto_istat.offset = next_lpw_offset - 1 = old LPW_DCW_PTR
2418  * which (after divide-by-2) gives stop_buffer_number = buffer_number + 1,
2419  * allowing mgp_read_dcm_'s processing loop to fire.
2420  */
2421 void iom_advance_lpw (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2422   {
2423     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2424     p -> LPW_DCW_PTR = (p -> LPW_DCW_PTR + 1u) & MASK18;
2425     putbits36_18 ((word36 *) & p -> LPW, 0, p -> LPW_DCW_PTR);
2426     write_LPW (iom_unit_idx, chan);
2427   }
2428 
2429 /*
2430  * iom_continue_channel ()
2431  *
2432  * Continue a payload channel that is currently in IOM_CMD_PENDING state
2433  * after the device has written data to the current workspace buffer.
2434  *
2435  * This is called by the MGP device after writing an incoming packet to the
2436  * current DCM workspace buffer[N].  It replicates one iteration of
2437  * doPayloadChannel's DCW list loop:
2438  *
2439  *   1. iom_list_service fetches IDCW[N+1] from the DCW list (advancing
2440  *      LPW_DCW_PTR to point to DDCW[N+1]).
2441  *
2442  *   2. d->iom_cmd dispatches IDCW[N+1] to the device handler.  For the
2443  *      MGP read command (cmd=001), the handler calls get_ddcw() which
2444  *      calls iom_list_service() again to fetch DDCW[N+1], setting
2445  *      DDCW_ADDR to workspace buffer[N+1] for the next incoming packet.
2446  *      LPW_DCW_PTR is then at IDCW[N+2].
2447  *
2448  *   3. send_marker_interrupt() signals Multics that buffer[N] is ready.
2449  *      mgp_read_dcm_ computes:
2450  *        stat.offset  = LPW_DCW_PTR_offset - 1 = 2*(N+2) - 1 = 2N+3
2451  *        stop_buffer  = divide(stat.offset, 2)  = N+1
2452  *      Since the DCM's buffer_number is N, the processing loop fires once,
2453  *      consuming buffer[N], then increments buffer_number to N+1.
2454  *
2455  * Because start_io sees running=true (marker interrupt), it does NOT call
2456  * ioi_$connect; the channel remains pending with DDCW_ADDR pointing to
2457  * buffer[N+1], ready for the next packet delivery.
2458  *
2459  * TDCW wrap-around is handled transparently by iom_list_service.
2460  */
2461 int iom_continue_channel (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2462   {
2463     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2464     struct iom_to_ctlr_s * d = & cables->iom_to_ctlr[iom_unit_idx][chan];
2465 
2466     if ((! d->in_use) || (! d->iom_cmd))
2467       {
2468         sim_warn ("%s: channel %d not in use\r\n", __func__, chan);
2469         return -1;
2470       }
2471 
2472     // Fetch the next IDCW (for buffer[N+1]) from the DCW list.
2473     // iom_list_service reads the DCW at LPW_DCW_PTR and advances the pointer.
2474     bool ptro, send, uff;
2475     int rc = iom_list_service (iom_unit_idx, chan, & ptro, & send, & uff);
2476     if (rc < 0)
2477       {
2478         sim_warn ("%s: list service failed for channel %d\r\n", __func__, chan);
2479         return -2;
2480       }
2481 
2482     // uff=true means the IOM tally ran out or a similar status condition was
2483     // flagged.  However, iom_list_service still fetches the correct IDCW and
2484     // sets send=true in all cases where the DCW dispatch is valid.  The uff
2485     // bit is a status indicator for the program's status queue, not a signal
2486     // that the DCW is unusable.  When send=false the !send check below will
2487     // abort; when send=true we should proceed.  Just log and continue.
2488     if (uff)
2489       {
2490         sim_warn ("%s: iom_list_service returned uff=%o for channel %d (proceeding)\r\n",
2491                   __func__, uff, chan);
2492       }
2493 
2494     if (! send)
2495       {
2496         sim_warn ("%s: iom_list_service returned send=%o for channel %d\r\n", __func__, send, chan);
2497         return -4;
2498       }
2499 
2500     // We expect an IDCW here (the marker+continue IDCW for the next buffer).
2501     // If we got a TDCW, iom_list_service already followed it and the DCW it
2502     // found after the transfer is what we have now.
2503     if (! IS_IDCW (p))
2504       {
2505         sim_warn ("%s: expected IDCW, got non-IDCW for channel %d\r\n",
2506                   __func__, chan);
2507         return -5;
2508       }
2509 
2510     // Set up channel state for an in-loop IDCW (not a PCW).
2511     p -> isPCW       = false;
2512     p -> tallyResidue = 0;
2513     p -> isRead      = true;
2514     p -> charPos     = 0;
2515     p -> chanStatus  = chanStatNormal;
2516 
2517     // Dispatch the IDCW to the device handler.  For MGP cmd=001 (read),
2518     // this calls get_ddcw() which fetches DDCW[N+1] and sets DDCW_ADDR to
2519     // workspace buffer[N+1].  The handler returns IOM_CMD_PENDING to keep
2520     // the channel active.
2521     int rc2 = d->iom_cmd (iom_unit_idx, chan);
2522 
2523     if (rc2 < 0)
2524       {
2525         sim_warn ("%s: iom_cmd failed for channel %d\r\n", __func__, chan);
2526         // we'll send a terminate interrupt in mgp_process_event
2527         return -6;
2528       }
2529 
2530     if (rc2 == IOM_CMD_DISCONNECT)
2531       {
2532         // we'll send a terminate interrupt in mgp_process_event
2533         return -7;
2534       }
2535 
2536     // rc2 == IOM_CMD_PENDING: DDCW_ADDR now points to buffer[N+1].
2537     // Send marker interrupt so mgp_read_dcm_ processes buffer[N].
2538     send_marker_interrupt (iom_unit_idx, (int) chan);
2539     return 0;
2540   }
2541 
2542 #if defined(TESTING)
2543 void dumpDCW (word36 DCW, word1 LPW_23_REL) {
     /* [previous][next][first][last][top][bottom][index][help] */
2544   static char * charCtrls[4] =
2545     { "terminate", "undefined", "proceed", "marker" };
2546   static char * chanCmds[16] =
2547     { "record",    "undefined", "nondata",     "undefined",
2548       "undefined", "undefined", "multirecord", "undefined",
2549       "character", "undefined", "undefined",   "undefined",
2550       "undefined", "undefined", "undefined",   "undefined"
2551     };
2552   word3 DCW_18_20_CP =      getbits36_3 (DCW, 18);
2553 
2554   if (DCW_18_20_CP == 07) { // IDCW
2555     word6 IDCW_DEV_CMD  =     getbits36_6 (DCW,  0);
2556     word6 IDCW_DEV_CODE =     getbits36_6 (DCW,  6);
2557  /* word6 IDCW_AE =           getbits36_6 (DCW,  12);
2558   * word1 IDCW_EC;
2559   * if (LPW_23_REL)
2560   *   IDCW_EC = 0;
2561   * else
2562   *   IDCW_EC =               getbits36_1 (DCW, 21); */
2563     word2 IDCW_CHAN_CTRL =    getbits36_2 (DCW, 22);
2564     word6 IDCW_CHAN_CMD  =    getbits36_6 (DCW, 24);
2565     word6 IDCW_COUNT     =    getbits36_6 (DCW, 30);
2566     sim_printf ("//   IDCW %012llo\r\n", DCW);
2567     sim_printf ("//     cmd             %02o %s\r\n", IDCW_DEV_CMD, cmdNames[IDCW_DEV_CMD]);
2568     sim_printf ("//     dev code        %02o\r\n", IDCW_DEV_CODE);
2569     sim_printf ("//     ctrl             %o (%s)\r\n", IDCW_CHAN_CTRL, charCtrls[IDCW_CHAN_CTRL]);
2570     sim_printf ("//     chancmd          %o (%s)\r\n", IDCW_CHAN_CMD, IDCW_CHAN_CMD < 16 ? chanCmds[IDCW_CHAN_CMD] : "unknown");
2571     sim_printf ("//     count            %o\r\n", IDCW_COUNT);
2572   } else { // TDCW or DDCW
2573     word18 TDCW_DATA_ADDRESS = getbits36_18 (DCW,  0);
2574     word1  TDCW_31_SEG       = getbits36_1  (DCW, 31);
2575     word1  TDCW_32_PDTA      = getbits36_1  (DCW, 32);
2576     word1  TDCW_33_PDCW      = getbits36_1  (DCW, 33);
2577     word1  TDCW_33_EC        = getbits36_1  (DCW, 33);
2578     word1  TDCW_34_RES       = getbits36_1  (DCW, 34);
2579     word1  TDCW_35_REL       = getbits36_1  (DCW, 35);
2580 
2581     word12 DDCW_TALLY        = getbits36_12 (DCW, 24);
2582     word18 DDCW_ADDR         = getbits36_18 (DCW,  0);
2583     word2  DDCW_22_23_TYPE   = getbits36_2  (DCW, 22);
2584     static char * types[4]   = { "IOTD", "IOTP", "TDCW", "IONTP" };
2585     if (DDCW_22_23_TYPE == 2) {
2586       sim_printf ("//   TDCW (2) %012llo\r\n", DCW);
2587       sim_printf ("//     dcw ptr   %06o\r\n", TDCW_DATA_ADDRESS);
2588       sim_printf ("//     seg            %o\r\n", TDCW_31_SEG);
2589       sim_printf ("//     pdta           %o\r\n", TDCW_32_PDTA);
2590       sim_printf ("//     pdcw           %o\r\n", TDCW_33_PDCW);
2591       sim_printf ("//     ec             %o\r\n", TDCW_33_EC);
2592       sim_printf ("//     res            %o\r\n", TDCW_34_RES);
2593       sim_printf ("//     rel            %o\r\n", TDCW_35_REL);
2594     } else {
2595       sim_printf ("//   %s (%o) %012llo\r\n", types[DDCW_22_23_TYPE], DDCW_22_23_TYPE, DCW);
2596       sim_printf ("//     tally       %04o\r\n", DDCW_TALLY);
2597       sim_printf ("//     addr            %02o\r\n", DDCW_ADDR);
2598       sim_printf ("//     cp               %o\r\n", getbits36_3 (DCW, 18));
2599     }
2600   }
2601 }
2602 
2603 static void dumpLPW (uint iom_unit_idx, uint chan) {
     /* [previous][next][first][last][top][bottom][index][help] */
2604   iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2605   char updateCase;
2606   if (p->LPW_21_NC)
2607     updateCase = 'c';
2608   else if (p->LPW_22_TAL)
2609     updateCase = 'b';
2610   else
2611     updateCase = 'a';
2612   static char * chanName[64] = {
2613         "0.",           //  0.   0
2614         "1.",           //  1.   1
2615         "Connect",      //  2.   2
2616         "3.",           //  3.   3
2617         "4.",           //  4.   4
2618         "5.",           //  5.   5
2619         "6.",           //  6.   6
2620         "7.",           //  7.   7
2621         "8.",           //  8.  10
2622         "9.",           //  9.  11
2623         "MTP Tape",     // 10.  12
2624         "IPC Disk",     // 11.  13
2625         "MPC Disk",     // 12.  14
2626         "Card reader",  // 13.  15
2627         "Card punch",   // 14.  16
2628         "Printer",      // 15.  17
2629         "FNP",          // 16.  20
2630         "FNP",          // 17.  21
2631         "FNP",          // 18.  22
2632         "FNP",          // 19.  23
2633         "FNP",          // 20.  24
2634         "FNP",          // 21.  25
2635         "FNP",          // 22.  26
2636         "FNP",          // 23.  27
2637         "24.",          // 24.  30
2638         "25.",          // 25.  31
2639         "ABSI",         // 26.  32
2640         "27.",          // 27.  33
2641         "28.",          // 28.  34
2642         "29.",          // 29.  35
2643         "Console",      // 30.  36
2644         "31.",          // 31.  37
2645         "Socket",       // 32.  40
2646         "Socket",       // 33.  41
2647         "Socket",       // 34.  42
2648         "Socket",       // 35.  43
2649         "Socket",       // 36.  44
2650         "Socket",       // 37.  45
2651         "Socket",       // 38.  46
2652         "39.",
2653         "40.",
2654         "41.",
2655         "42.",
2656         "43.",
2657         "44.",
2658         "45.",
2659         "46.",
2660         "47.",
2661         "48.",
2662         "49.",
2663         "50.",
2664         "51.",
2665         "52.",
2666         "53.",
2667         "54.",
2668         "55.",
2669         "56.",
2670         "57.",
2671         "58.",
2672         "59.",
2673         "60.",
2674         "61.",
2675         "62.",
2676         "63."
2677   };
2678   sim_printf ("// %s channel LPW %012llo (case %c)\r\n", chanName[chan], p->LPW, updateCase);
2679   sim_printf ("//   DCW pointer %06o\r\n",      p->LPW_DCW_PTR);
2680   sim_printf ("//     RES              %o\r\n", p->LPW_18_RES);
2681   sim_printf ("//     REL              %o\r\n", p->LPW_19_REL);
2682   sim_printf ("//     AE               %o\r\n", p->LPW_20_AE);
2683   sim_printf ("//     NC 21            %o\r\n", p->LPW_21_NC);
2684   sim_printf ("//     TAL 22           %o\r\n", p->LPW_22_TAL);
2685   sim_printf ("//     REL              %o\r\n", p->LPW_23_REL);
2686   sim_printf ("//     TALLY         %04o\r\n",  p->LPW_TALLY);
2687   sim_printf ("// \r\n");
2688 }
2689 #endif
2690 
2691 static void fetch_and_parse_LPW (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2692   {
2693 #if defined(TESTING)
2694     cpu_state_t * cpup = _cpup;
2695 #endif
2696     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2697     uint chanLoc        = mbxLoc (iom_unit_idx, chan);
2698 
2699     iom_core_read (iom_unit_idx, chanLoc + IOM_MBX_LPW, (word36 *) & p -> LPW, __func__);
2700     sim_debug (DBG_DEBUG, & iom_dev,
2701                "%s: lpw %08o %012"PRIo64"\r\n", __func__, chanLoc + IOM_MBX_LPW, p->LPW);
2702 
2703     p -> LPW_DCW_PTR = getbits36_18 (p -> LPW,  0);
2704     p -> LPW_18_RES  = getbits36_1  (p -> LPW, 18);
2705     p -> LPW_19_REL  = getbits36_1  (p -> LPW, 19);
2706     p -> LPW_20_AE   = getbits36_1  (p -> LPW, 20);
2707     p -> LPW_21_NC   = getbits36_1  (p -> LPW, 21);
2708     p -> LPW_22_TAL  = getbits36_1  (p -> LPW, 22);
2709     p -> LPW_23_REL  = getbits36_1  (p -> LPW, 23);
2710     p -> LPW_TALLY   = getbits36_12 (p -> LPW, 24);
2711 
2712     if (chan == IOM_CONNECT_CHAN)
2713       {
2714         p -> LPWX = 0;
2715         p -> LPWX_BOUND = 0;
2716         p -> LPWX_SIZE = 0;
2717       }
2718     else
2719       {
2720         iom_core_read (iom_unit_idx, chanLoc + IOM_MBX_LPWX, (word36 *) & p -> LPWX, __func__);
2721         p -> LPWX_BOUND = getbits36_18 (p -> LPWX, 0);
2722         p -> LPWX_SIZE  = getbits36_18 (p -> LPWX, 18);
2723       }
2724     update_chan_mode (iom_unit_idx, chan, false);
2725   }
2726 
2727 static void unpack_DCW (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2728   {
2729 #if defined(TESTING)
2730     cpu_state_t * cpup = _cpup;
2731 #endif
2732     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2733     p -> DCW_18_20_CP =      getbits36_3 (p -> DCW, 18);
2734 
2735     if (IS_IDCW (p)) // IDCW
2736       {
2737         p -> IDCW_DEV_CMD   = getbits36_6 (p -> DCW, 0);
2738         p -> IDCW_DEV_CODE  = getbits36_6 (p -> DCW, 6);
2739         p -> IDCW_AE        = getbits36_6 (p -> DCW, 12);
2740         if (p -> LPW_23_REL)
2741           p -> IDCW_EC      = 0;
2742         else
2743           p -> IDCW_EC      = getbits36_1 (p -> DCW, 21);
2744         if (p -> IDCW_EC)
2745           p -> SEG          = 1; // pat. step 45
2746         p -> IDCW_CHAN_CTRL = getbits36_2 (p -> DCW, 22);
2747         p -> IDCW_CHAN_CMD  = getbits36_6 (p -> DCW, 24);
2748         p -> IDCW_COUNT     = getbits36_6 (p -> DCW, 30);
2749         p->recordResidue = p->IDCW_COUNT;
2750 #if defined(TESTING)
2751         sim_debug (DBG_DEBUG, & iom_dev,
2752                    "%s: IDCW %012llo cmd %02o (%s) dev %02o ctrl %o chancmd %o\r\n",
2753                    __func__, p->DCW, p->IDCW_DEV_CMD, cmdNames[p->IDCW_DEV_CMD],
2754                    p->IDCW_DEV_CODE, p->IDCW_CHAN_CTRL, p->IDCW_CHAN_CMD);
2755 #endif
2756       }
2757     else // TDCW or DDCW
2758       {
2759         p -> TDCW_DATA_ADDRESS = getbits36_18 (p -> DCW,  0);
2760         p -> TDCW_31_SEG       = getbits36_1  (p -> DCW, 31);
2761         p -> TDCW_32_PDTA      = getbits36_1  (p -> DCW, 32);
2762         p -> TDCW_33_PDCW      = getbits36_1  (p -> DCW, 33);
2763         p -> TDCW_33_EC        = getbits36_1  (p -> DCW, 33);
2764         p -> TDCW_34_RES       = getbits36_1  (p -> DCW, 34);
2765         p -> TDCW_35_REL       = getbits36_1  (p -> DCW, 35);
2766         p -> DDCW_TALLY        = getbits36_12 (p -> DCW, 24);
2767         p -> DDCW_ADDR         = getbits36_18 (p -> DCW,  0);
2768         p -> DDCW_22_23_TYPE   = getbits36_2  (p -> DCW, 22);
2769         sim_debug (DBG_DEBUG, & iom_dev,
2770                    "%s: TDCW/DDCW %012llo tally %04o addr %06o type %o\r\n",
2771                    __func__, p->DCW, p->DDCW_TALLY, p->DDCW_ADDR, p->DDCW_22_23_TYPE);
2772 // POLTS_TESTING
2773 
2774 
2775 
2776 
2777 
2778 
2779 
2780 
2781 
2782 
2783 
2784 
2785 
2786 
2787 
2788 
2789 
2790 
2791 
2792 
2793       }
2794   }
2795 
2796 static void pack_DCW (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2797   {
2798     // DCW_18_20_CP is the only field ever changed.
2799     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2800     putbits36_3 ((word36 *) & p -> DCW, 18, p -> DCW_18_20_CP);
2801   }
2802 
2803 static void pack_LPW (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2804   {
2805     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2806     putbits36_18 ((word36 *) & p-> LPW,   0, p -> LPW_DCW_PTR);
2807     putbits36_1  ((word36 *) & p-> LPW,  18, p -> LPW_18_RES);
2808     putbits36_1  ((word36 *) & p-> LPW,  19, p -> LPW_19_REL);
2809     putbits36_1  ((word36 *) & p-> LPW,  20, p -> LPW_20_AE);
2810     putbits36_1  ((word36 *) & p-> LPW,  21, p -> LPW_21_NC);
2811     putbits36_1  ((word36 *) & p-> LPW,  22, p -> LPW_22_TAL);
2812     putbits36_1  ((word36 *) & p-> LPW,  23, p -> LPW_23_REL);
2813     putbits36_12 ((word36 *) & p-> LPW,  24, p -> LPW_TALLY);
2814     putbits36_18 ((word36 *) & p-> LPWX,  0, p -> LPWX_BOUND);
2815     putbits36_18 ((word36 *) & p-> LPWX, 18, p -> LPWX_SIZE);
2816   }
2817 
2818 static void fetch_and_parse_PCW (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
2819   {
2820     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2821 
2822     iom_core_read2 (iom_unit_idx, p -> LPW_DCW_PTR, (word36 *) & p -> PCW0, (word36 *) & p -> PCW1, __func__);
2823     p -> PCW_CHAN           = getbits36_6  (p -> PCW1,  3);
2824     p -> PCW_AE             = getbits36_6  (p -> PCW0, 12);
2825     p -> PCW_21_MSK         = getbits36_1  (p -> PCW0, 21);
2826     p -> PCW_PAGE_TABLE_PTR = getbits36_18 (p -> PCW1,  9);
2827     p -> PCW_63_PTP         = getbits36_1  (p -> PCW1, 27);
2828     p -> PCW_64_PGE         = getbits36_1  (p -> PCW1, 28);
2829     p -> PCW_65_AUX         = getbits36_1  (p -> PCW1, 29);
2830     if (p -> PCW_65_AUX)
2831       sim_warn ("PCW_65_AUX\r\n");
2832     p -> DCW = p -> PCW0;
2833     unpack_DCW (iom_unit_idx, chan);
2834   }
2835 
2836 static void fetch_and_parse_DCW (uint iom_unit_idx, uint chan, UNUSED bool read_only)
     /* [previous][next][first][last][top][bottom][index][help] */
2837   {
2838 #if defined(TESTING)
2839     cpu_state_t * cpup = _cpup;
2840 #endif
2841     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2842     word24 addr         = p -> LPW_DCW_PTR & MASK18;
2843 
2844     sim_debug (DBG_DEBUG, & iom_dev, "%s: addr %08o\r\n", __func__, addr);
2845     switch (p -> chanMode)
2846       {
2847         // LPW ABS
2848         case cm1:
2849         case cm1e:
2850           {
2851             iom_core_read (iom_unit_idx, addr, (word36 *) & p -> DCW, __func__);
2852           }
2853           break;
2854 
2855         // LPW EXT
2856         case cm2e:
2857           {
2858             // LPXW_BOUND is mod 2; ie. val is * 2
2859             //addr |= ((word24) p -> LPWX_BOUND << 18);
2860             addr += ((word24) p -> LPWX_BOUND << 1);
2861             iom_core_read (iom_unit_idx, addr, (word36 *) & p -> DCW, __func__);
2862           }
2863           break;
2864 
2865         case cm2:
2866         case cm3b:
2867           {
2868             sim_warn ("fetch_and_parse_DCW LPW paged\r\n");
2869           }
2870           break;
2871 
2872         case cm3a:
2873         case cm4:
2874         case cm5:
2875           {
2876             fetch_LPWPTW (iom_unit_idx, chan);
2877             // Calculate effective address
2878             // PTW 4-17 || LPW 8-17
2879             word24 addr_ = ((word24) (getbits36_14 (p -> PTW_LPW, 4) << 10)) | ((p -> LPW_DCW_PTR) & MASK10);
2880             iom_core_read (iom_unit_idx, addr_, (word36 *) & p -> DCW, __func__);
2881           }
2882           break;
2883       }
2884     unpack_DCW (iom_unit_idx, chan);
2885 
2886     if (IS_IDCW (p))
2887       sim_debug (DBG_DEBUG, & iom_dev,
2888                  "%s: chan %d idcw: dev_cmd %#o dev_code %#o ae %#o ec %d control %#o chan_cmd %#o data %#o\r\n",
2889                  __func__, p -> PCW_CHAN, p -> IDCW_DEV_CMD, p -> IDCW_DEV_CODE, p -> IDCW_AE,
2890                  p -> IDCW_EC, p -> IDCW_CHAN_CTRL, p -> IDCW_CHAN_CMD, p -> IDCW_COUNT);
2891     else
2892       if (p -> DDCW_22_23_TYPE == 02)
2893         sim_debug (DBG_DEBUG, & iom_dev,
2894                    "%s: chan %d tdcw: address %#o seg %d pdta %d pdcw/ec %d res %d rel %d\r\n",
2895                    __func__, p -> PCW_CHAN, p -> TDCW_DATA_ADDRESS, p -> TDCW_31_SEG,
2896                    p -> TDCW_32_PDTA, p -> TDCW_33_PDCW, p -> TDCW_34_RES, p -> TDCW_35_REL);
2897       else
2898         sim_debug (DBG_DEBUG, & iom_dev,
2899                    "%s: chan %d ddcw: address %#o char_pos %#o type %#o tally %#o\r\n",
2900                    __func__, p -> PCW_CHAN, p -> DDCW_ADDR, p -> DCW_18_20_CP,
2901                    p -> DDCW_22_23_TYPE, p -> DDCW_TALLY);
2902   }
2903 
2904 // Read the list word at 'addr' (an LPW_DCW_PTR value) as
2905 // fetch_and_parse_DCW would in channel mode 'mode', without changing the
2906 // channel's DCW or PTW state.  Returns 0 on success, -1 if the address
2907 // can't be translated (invalid PTW) or the mode is one fetch_and_parse_DCW
2908 // doesn't support for the list (cm2, cm3b: "LPW paged").
2909 
2910 static int peek_list_word (uint iom_unit_idx, uint chan, int mode, word18 addr, word36 * data)
     /* [previous][next][first][last][top][bottom][index][help] */
2911   {
2912     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2913     switch (mode)
2914       {
2915         case cm1:
2916         case cm1e:
2917           iom_core_read (iom_unit_idx, addr, data, __func__);
2918           return 0;
2919 
2920         case cm2e:
2921           iom_core_read (iom_unit_idx, (word24) addr + ((word24) p -> LPWX_BOUND << 1), data, __func__);
2922           return 0;
2923 
2924         case cm3a:
2925         case cm4:
2926         case cm5:
2927           {
2928             word36 ptw;
2929             word24 pgte = build_LPWPTW_address (p -> PCW_PAGE_TABLE_PTR, p -> SEG,
2930                                                 (addr >> 10) & MASK8);
2931             iom_core_read (iom_unit_idx, pgte, & ptw, __func__);
2932             if ((ptw & 0740000777747) != 04llu)
2933               return -1;
2934             iom_core_read (iom_unit_idx,
2935                            ((word24) (getbits36_14 (ptw, 4) << 10)) | (addr & MASK10),
2936                            data, __func__);
2937             return 0;
2938           }
2939 
2940         default:
2941           return -1;
2942       }
2943   }
2944 
2945 // Return in '*dcw' the next IDCW or DDCW the channel's list service would
2946 // process: the word at LPW_DCW_PTR or, if that is a TDCW, the word it
2947 // transfers to.  Lets a device look at a DCW it has not yet fetched (e.g.
2948 // a halt IDCW the software has just stored) without changing the
2949 // channel's state.  A TDCW is modelled as iom_list_service applies it,
2950 // on local copies: the new address, the channel mode after the transfer
2951 // (tdcw_chan_mode), and LPW RES ORed with TDCW RES.  Returns -1 where the
2952 // list service would not deliver a DCW or the word can't be read: an
2953 // untranslatable address or unsupported mode, a TDCW to a TDCW, an AC
2954 // change error (RES with TDCW EC/PDCW), or an IDCW under RES.  A TDCW
2955 // with SEG (bit 31), which iom_list_service only warns about, is also
2956 // refused, since its address can't be modelled.
2957 
2958 int iom_peek_next_dcw (uint iom_unit_idx, uint chan, word36 * dcw)
     /* [previous][next][first][last][top][bottom][index][help] */
2959   {
2960     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
2961     int    mode = p -> chanMode;
2962     word18 addr = p -> LPW_DCW_PTR;
2963     word1  res  = p -> LPW_18_RES;
2964     word36 w;
2965 
2966     if (peek_list_word (iom_unit_idx, chan, mode, addr, & w) != 0)
2967       return -1;
2968     if (getbits36_3 (w, 18) != 07 && getbits36_2 (w, 22) == 02)    // TDCW
2969       {
2970         word1 seg  = getbits36_1 (w, 31);
2971         word1 pdta = getbits36_1 (w, 32);
2972         word1 pdcw = getbits36_1 (w, 33);
2973         word1 tres = getbits36_1 (w, 34);
2974         word1 rel  = getbits36_1 (w, 35);
2975         res |= tres;
2976         if (seg || (res && pdcw))
2977           return -1;
2978         mode = tdcw_chan_mode (mode, pdta, pdcw, rel);
2979         addr = getbits36_18 (w, 0);
2980         if (peek_list_word (iom_unit_idx, chan, mode, addr, & w) != 0)
2981           return -1;
2982         if (getbits36_3 (w, 18) != 07 && getbits36_2 (w, 22) == 02)
2983           return -1;                                                // second TDCW
2984       }
2985     if (getbits36_3 (w, 18) == 07 && res)
2986       return -1;                                                    // IDCW under RES
2987     * dcw = w;
2988     return 0;
2989   }
2990 
2991 /*
2992  * send_general_interrupt ()
2993  *
2994  * Send an interrupt from the IOM to the CPU.
2995  *
2996  */
2997 
2998 int send_general_interrupt (uint iom_unit_idx, uint chan, enum iomImwPics pic)
     /* [previous][next][first][last][top][bottom][index][help] */
2999   {
3000 #if defined(TESTING)
3001     cpu_state_t * cpup = _cpup;
3002 #endif
3003     uint imw_addr;
3004     uint chan_group    = chan < 32 ? 1 : 0;
3005     uint chan_in_group = chan & 037;
3006 
3007     uint iomUnitNum =
3008       iom_unit_data[iom_unit_idx].configSwMultiplexBaseAddress & 3u;
3009     uint interrupt_num = iomUnitNum | (chan_group << 2) | ((uint) pic << 3);
3010     // Section 3.2.7 defines the upper bits of the IMW address as
3011     // being defined by the mailbox base address switches and the
3012     // multiplex base address switches.
3013     // However, AN-70 reports that the IMW starts at 01200.  If AN-70 is
3014     // correct, the bits defined by the mailbox base address switches would
3015     // have to always be zero.  We'll go with AN-70.  This is equivalent to
3016     // using bit value 0010100 for the bits defined by the multiplex base
3017     // address switches and zeros for the bits defined by the mailbox base
3018     // address switches.
3019     //imw_addr += 01200;  // all remaining bits
3020     uint pi_base = iom_unit_data[iom_unit_idx].configSwMultiplexBaseAddress & ~3u;
3021     imw_addr = (pi_base << 3) | interrupt_num;
3022 
3023     sim_debug (DBG_DEBUG, & iom_dev,
3024                "%s: IOM %c, channel %d (%#o), level %d; "
3025                "Interrupt %d (%#o).\r\n",
3026                __func__, iomChar (iom_unit_idx), chan, chan, pic, interrupt_num,
3027                interrupt_num);
3028     word36 imw;
3029     iom_core_read_lock (iom_unit_idx, imw_addr, &imw, __func__);
3030     // The 5 least significant bits of the channel determine a bit to be
3031     // turned on.
3032     //sim_debug (DBG_DEBUG, & iom_dev,
3033                //"%s: IMW at %#o was %012"PRIo64"; setting bit %d\r\n",
3034                //__func__, imw_addr, imw, chan_in_group);
3035     putbits36_1 (& imw, chan_in_group, 1);
3036     iom_core_write_unlock (iom_unit_idx, imw_addr, imw, __func__);
3037     return scu_set_interrupt (iom_unit_data[iom_unit_idx].invokingScuUnitIdx, interrupt_num);
3038   }
3039 
3040 static void iom_fault (uint iom_unit_idx, uint chan, UNUSED const char * who,
     /* [previous][next][first][last][top][bottom][index][help] */
3041                       iomFaultServiceRequest req,
3042                       iomSysFaults_t signal)
3043   {
3044 #if defined(TESTING)
3045     cpu_state_t * cpup = _cpup;
3046 #endif
3047     // T&D: the executive's survey connects to each IOM with an illegal
3048     // connect channel LPW tally and expects the system fault (stored and
3049     // interrupted below); not worth a warning there
3050     if (! tandd_io_mode ())
3051       sim_warn ("iom_fault %s: IOM %c channel %o, fault %o\r\n", who,
3052                 'A' + iom_unit_idx, chan, (uint) signal);
3053 
3054     // iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
3055     // TODO:
3056     // For a system fault:
3057     // Store the indicated fault into a system fault word (3.2.6) in
3058     // the system fault channel -- use a list service to get a DCW to do so
3059     // For a user fault, use the normal channel status mechanisms
3060 
3061     // sys fault masks channel
3062 
3063     // signal gets put in bits 30..35, but we need fault code for 26..29
3064 
3065     // BUG: mostly unimplemented
3066 
3067     // User Faults: Pg 78: "A user fault is an abnormal conditions that can
3068     // be caused by a user program operating in the slave mode in the
3069     // processor."
3070     //  and
3071     // "User faults can be detected by the IOM Central of by a channel. If
3072     // a user fault is detected by the IOM Central, the fault is indicated to
3073     // the channel, and the channel is responsible for reporting the user
3074     // fault as status is its regular status queue.
3075 
3076     // This code only handles system faults
3077     //
3078 
3079     sim_debug (DBG_DEBUG, & iom_dev,
3080                "%s: chan %d %s req %#o signal %#o\r\n",
3081                __func__, chan, who, req, signal);
3082 
3083     word36 faultWord = 0;
3084     putbits36_9 (& faultWord, 9, (word9) chan);
3085     putbits36_5 (& faultWord, 18, (word5) req);
3086     // IAC, bits 26..29
3087     putbits36_6 (& faultWord, 30, (word6) signal);
3088 
3089     uint chanloc = mbxLoc (iom_unit_idx, IOM_SYSTEM_FAULT_CHAN);
3090 
3091     word36 lpw;
3092     iom_core_read (iom_unit_idx, chanloc + IOM_MBX_LPW, & lpw, __func__);
3093 
3094     word36 scw;
3095     iom_core_read (iom_unit_idx, chanloc + IOM_MBX_SCW, & scw, __func__);
3096 
3097     word36 dcw;
3098     iom_core_read_lock (iom_unit_idx, chanloc + IOM_MBX_DCW, & dcw, __func__);
3099 
3100     //sim_debug (DBG_DEBUG, & iom_dev,
3101                //"%s: lpw %012"PRIo64" scw %012"PRIo64" dcw %012"PRIo64"\r\n",
3102                //__func__, lpw, scw, dcw);
3103 
3104     iom_core_write (iom_unit_idx, (dcw >> 18) & MASK18, faultWord, __func__);
3105 
3106     uint tally = dcw & MASK12;
3107     if (tally > 1)
3108       {
3109         dcw -= 01llu;        //-V536  // tally --
3110         dcw += 01000000llu;  //-V536  // addr ++
3111       }
3112     else
3113       dcw = scw; // reset to beginning of queue
3114     iom_core_write_unlock (iom_unit_idx, chanloc + IOM_MBX_DCW, dcw, __func__);
3115 
3116     send_general_interrupt (iom_unit_idx, IOM_SYSTEM_FAULT_CHAN, imwSystemFaultPic);
3117   }
3118 
3119 //  0 ok
3120 // -1 fault
3121 // There is a path through the code where no DCW is sent (IDCW && LPW_18_RES)
3122 // Does the -1 return cover that?
3123 
3124 int iom_list_service (uint iom_unit_idx, uint chan,
     /* [previous][next][first][last][top][bottom][index][help] */
3125                            bool * ptro, bool * sendp, bool * uffp)
3126   {
3127     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
3128 
3129 // initialize
3130 
3131     bool isConnChan = chan == IOM_CONNECT_CHAN;
3132     * ptro          = false; // assume not PTRO
3133     bool uff        = false; // user fault flag
3134     bool send       = false;
3135 
3136 // Figure 4.3.1
3137 
3138 // START
3139 
3140     // FIRST SERVICE?
3141 
3142     if (p -> lsFirst)
3143       {
3144         // PULL LPW AND LPW EXT. FROM CORE MAILBOX
3145 
3146         fetch_and_parse_LPW (iom_unit_idx, chan);
3147         p -> wasTDCW = false;
3148         p -> SEG     = 0; // pat. FIG. 2, step 44
3149       }
3150     // else lpw and lpwx are in chanData;
3151 
3152     // CONNECT CHANNEL?
3153 
3154     if (isConnChan)
3155       { // connect channel
3156 
3157         // LPW 21 (NC), 22 (TAL) is {00, 01, 1x}?
3158 
3159         if (p -> LPW_21_NC == 0 && p -> LPW_22_TAL == 0)
3160               {
3161                 iom_fault (iom_unit_idx, IOM_CONNECT_CHAN, __func__,
3162                           p -> lsFirst ? iomFsrFirstList : iomFsrList,
3163                           iomIllTalCChnFlt);
3164                 return -1;
3165               }
3166 
3167         if (p -> LPW_21_NC == 0 && p -> LPW_22_TAL == 1)
3168           { // 01
3169 
3170             // TALLY is {0, 1, >1}?
3171 
3172             if (p -> LPW_TALLY == 0)
3173               {
3174                 iom_fault (iom_unit_idx, IOM_CONNECT_CHAN, __func__,
3175                           p -> lsFirst ? iomFsrFirstList : iomFsrList,
3176                           iomIllTalCChnFlt);
3177                 return -1;
3178               }
3179 
3180             if (p -> LPW_TALLY > 1)
3181               { // 00
3182 
3183                 // 256K OVERFLOW?
3184                 if (p -> LPW_20_AE == 0 &&
3185                     (((word36) p -> LPW_DCW_PTR) + ((word36) p -> LPW_TALLY)) >
3186                     01000000llu)
3187                   {
3188                     iom_fault (iom_unit_idx, IOM_CONNECT_CHAN, __func__,
3189                               p -> lsFirst ? iomFsrFirstList : iomFsrList,
3190                               iom256KFlt);
3191                     return -1;
3192                   }
3193               }
3194             else if (p -> LPW_TALLY == 1)
3195               * ptro = true;
3196           }
3197         else // 1x
3198           * ptro = true;
3199 
3200         // PULL PCW FROM CORE
3201 
3202 // B
3203 
3204         fetch_and_parse_PCW (iom_unit_idx, chan); // fills in DCW*
3205 
3206         // PCW 18-20 == 111?
3207         if (IS_NOT_IDCW (p))
3208           {
3209             iom_fault (iom_unit_idx, IOM_CONNECT_CHAN, __func__,
3210                       p -> lsFirst ? iomFsrFirstList : iomFsrList,
3211                       iomNotPCWFlt);
3212             return -1;
3213           }
3214 
3215         // SELECT CHANNEL
3216 
3217         // chan = p -> PCW_CHAN;
3218 
3219 // detect unused slot as fault
3220 // if (no handler in slot)
3221 //  goto FAULT;
3222 
3223        // SEND PCW TO CHANNEL
3224 
3225         // p -> DCW = p -> DCW;
3226         send = true;
3227 
3228         goto D;
3229       }
3230 
3231     // Not connect channel
3232 
3233     // LPW 21 (NC), 22 (TAL) is {00, 01, 1x}?
3234 
3235     if (p -> LPW_21_NC == 0 && p -> LPW_22_TAL == 0)
3236       {
3237         // XXX see pat. 46-51 re: SEG
3238         // 256K OVERFLOW?
3239         if (p -> LPW_20_AE == 0 &&
3240             (((word36) p -> LPW_DCW_PTR) + ((word36) p -> LPW_TALLY)) >
3241             01000000llu)
3242           {
3243             iom_fault (iom_unit_idx, IOM_CONNECT_CHAN, __func__,
3244                       p -> lsFirst ? iomFsrFirstList : iomFsrList,
3245                       iom256KFlt);
3246             return -1;
3247           }
3248        }
3249     else if (p -> LPW_21_NC == 0 && p -> LPW_22_TAL == 1)
3250       { // 01
3251 A:;
3252         // TALLY is {0, 1, >1}?
3253 
3254         // Per the IOM EPS: TAL=0 means "a tally of zero will not cause a
3255         // fault indication".  Only set uff when TAL=1 (tally-zero faults
3256         // enabled).  When TAL=0 (circular DCW lists, e.g. the MGP read
3257         // channel), reaching TALLY=0 after a TDCW wrap is normal and must
3258         // not cause uff - doing so leaves DDCW_ADDR stale from the TDCW
3259         // unpack (= 0) and prevents mgp_cmd(001) from refreshing it.
3260         if (p -> LPW_TALLY == 0 && p -> LPW_22_TAL)
3261           {
3262             sim_warn("%s: setting uff for chan %d because LPW_TALLY was 0. LPW=%llo.\r\n",
3263                      __func__, chan, p -> LPW);
3264             uff = true;
3265           }
3266         else if (p -> LPW_TALLY > 1)
3267           { // 00
3268 
3269             // XXX see pat. 46-51 re: SEG
3270             // 256K OVERFLOW?
3271             if (p -> LPW_20_AE == 0 &&
3272                 (((word36) p -> LPW_DCW_PTR) + ((word36) p -> LPW_TALLY)) >
3273                 01000000llu)
3274               {
3275                 iom_fault (iom_unit_idx, IOM_CONNECT_CHAN, __func__,
3276                           p -> lsFirst ? iomFsrFirstList : iomFsrList,
3277                           iom256KFlt);
3278                 return -1;
3279               }
3280           }
3281       }
3282 
3283     // LPW 20? -- LPW_20 checked by fetch_and_parse_DCW
3284 
3285     // PULL DCW FROM CORE
3286     fetch_and_parse_DCW (iom_unit_idx, chan, false);
3287 
3288 // C
3289 
3290     if (p -> lsFirst)
3291       p -> LPWX_SIZE = p -> LPW_DCW_PTR;
3292 
3293 // pg B16: "If the IOM is paged [yes] and PCW bit 64 is off, LPW bit 23
3294 //          is ignored by the hardware. If bit 64 is set, LPW bit 23 causes
3295 //          the data to become segmented."
3296 // Handled in fetch_and_parse_LPW
3297 
3298     // LPW 23 REL?
3299 
3300     // TDCW ?
3301 
3302     if (IS_TDCW (p))
3303       {
3304         // SECOND TDCW?
3305         if (p -> wasTDCW)
3306           {
3307             sim_warn("%s: setting uff for chan %d because p->wasTDCW was %d\r\n",
3308                      __func__, chan, p->wasTDCW);
3309             uff = true;
3310             goto uffSet;
3311           }
3312         p -> wasTDCW = true;
3313 
3314         // PUT ADDRESS N LPW
3315 
3316         p -> LPW_DCW_PTR = p -> TDCW_DATA_ADDRESS;
3317         // OR TDCW 33, 34, 35 INTO LPW 20, 18, 23
3318         // XXX is 33 bogus? its semantics change based on PCW 64...
3319         // should be okay; pg B21 says that this is correct; implies that the
3320         // semantics are handled in the LPW code. Check...
3321         p -> LPW_20_AE  |= p -> TDCW_33_EC; // TDCW_33_PDCW
3322         p -> LPW_18_RES |= p -> TDCW_34_RES;
3323         p -> LPW_23_REL |= p -> TDCW_35_REL;
3324 
3325 // Pg B21: (TDCW_31_SEG)
3326 // "SEG = This bit furnishes the 19th address bit (MSD) of a TDCW address
3327 //  used for locating the DCW list in a 512 word page table. It will have
3328 //  meaning only in the TDCW where:
3329 //   (a) the DCW list is already paged and the TDCW calls for the
3330 //       DCW [to be] segmented
3331 //  or
3332 //   (b) the data is already segmented and the TDCW calls for the
3333 //       DCW list to be paged
3334 //  or
3335 //   (c) neither data is segmented nor DCW list is paged and the
3336 //       TDCW calls for both.
3337 //  and
3338 //   (d) an auxiliary PTW in not being used.
3339 
3340 //   (a) the DCW list is already paged   --  LPW PAGED: 3b, 4
3341 //       and the TDCW calls for the
3342 //       DCW [list to be] segmented      --  LPW SEG:       5
3343 
3344 //   (b) the data is already segmented   --  DCW SEG:   3a, 4
3345 //       and the TDCW calls for the
3346 //       DCW list to be paged            --  DCW PAGED:  2, 3b
3347 
3348 //   (c) neither data is segmented       -- DCW !SEG     1, 2, 3b
3349 //       nor DCW list is paged           -- LPW !PAGED   1, 2, 3a
3350 //                                       --              1, 2
3351 //       and the TDCW calls for both.    -- DCW SEG & LPW PAGED
3352 //                                                       4
3353 
3354 //put that weird SEG logic in here
3355 
3356         if (p -> TDCW_31_SEG)
3357           sim_warn ("TDCW_31_SEG\r\n");
3358 
3359         update_chan_mode (iom_unit_idx, chan, true);
3360 
3361         // Decrement tally
3362         p->LPW_TALLY = ((word12) (((word12s) p->LPW_TALLY) - 1)) & MASK12;
3363 
3364         pack_LPW (iom_unit_idx, chan);
3365 
3366         // AC CHANGE ERROR? (LPW 18 == 1 && DCW 33 == 1)
3367 
3368         if (p -> LPW_18_RES && p -> TDCW_33_EC) // same as TDCW_33_PDCW
3369           {
3370             sim_warn("%s: setting uff for chan %d because p->LPW_18_RES and p->TDCW_33_EC were set\r\n",
3371                      __func__, chan);
3372             uff = true;
3373             goto uffSet;
3374           }
3375 
3376         goto A;
3377       }
3378     else if (IS_IDCW (p))     // IDCW ?
3379       {
3380         // LPW_18_RES?
3381 
3382         if (p -> LPW_18_RES)
3383           {
3384             // SET USER FAULT FLAG
3385             sim_warn("%s: setting uff for chan %d because LPW_18_RES was %o\r\n",
3386                      __func__, chan, p->LPW_18_RES);
3387             uff = true; // XXX Why? uff isn't not examined later.
3388             // send = false; implicit...
3389           }
3390         else
3391           {
3392             // SEND IDCW TO CHANNEL
3393             // p -> DCW = p -> DCW;
3394             send = true;
3395           }
3396         p -> LPWX_SIZE = p -> LPW_DCW_PTR;
3397         goto D;
3398       }
3399 
3400     p -> wasTDCW = false;
3401     // NOT TDCW
3402 
3403     // CP VIOLATION?
3404 
3405     // 43A239854 3.2.3.3 "The byte size, defined by the channel, determines
3406     // what CP values are valid..."
3407 
3408     // If we get here, the DCW is not a IDCW and not a TDCW, therefore
3409     // it must be a DDCW. If the list service knew the sub-word size
3410     // size of the device, it could check the for valid values. Let
3411     // the device handler do that later.
3412 
3413     // if (cp decrepancy)
3414     //   {
3415     //      user_fault_flag = iomCsCpDiscrepancy;
3416     //      goto user_fault;
3417     //   }
3418     // if (cp violation)
3419     //   {
3420     //     uff = true;
3421     //     goto uffSet;
3422     //   }
3423 
3424     // USER FAULT FLAG SET?
3425 
3426     if (uff)
3427       {
3428 uffSet:;
3429         // PUT 7 into DCW 18-20
3430         p -> DCW_18_20_CP = 07u;
3431         pack_DCW (iom_unit_idx, chan);
3432       }
3433 
3434     // WRITE DCW IN [SCRATCH PAD] MAILBOX
3435 
3436     // p -> DVW  = P -> DCW;
3437 
3438     // SEND DCW TO CHANNEL
3439 
3440     // p -> DCW = p -> DCW;
3441     send = true;
3442 
3443     // goto D;
3444 
3445 D:;
3446 
3447     // SEND FLAGS TO CHANNEL
3448 
3449     * uffp  = uff;
3450     * sendp = send;
3451 
3452     // LPW 21 ?
3453 
3454     if (p -> LPW_21_NC == 0) // UPDATE
3455      {
3456        // UPDATE LPW ADDRESS & TALLY
3457        if (isConnChan)
3458          p -> LPW_DCW_PTR = (p -> LPW_DCW_PTR + 2u) & MASK18;
3459        else
3460          p -> LPW_DCW_PTR = (p -> LPW_DCW_PTR + 1u) & MASK18;
3461        // ubsan
3462        p->LPW_TALLY = ((word12) (((word12s) p->LPW_TALLY) - 1)) & MASK12;
3463        pack_LPW (iom_unit_idx, chan);
3464      }
3465 
3466     // IDCW OR FIRST LIST
3467     if (p -> DDCW_22_23_TYPE == 07u || p -> lsFirst)
3468       {
3469         // WRITE LPW & LPW EXT. INTO BOTH SCRATCHPAD AND CORE MAILBOXES
3470         // scratch pad
3471         // p -> lpw = p -> lpw
3472         // core
3473         write_LPW (iom_unit_idx, chan);
3474       }
3475     else
3476       {
3477         if (p -> LPW_21_NC == 0 ||
3478             (IS_TDCW (p)))
3479           {
3480             // WRITE LPW INTO BOTH SCRATCHPAD AND CORE MAILBOXES
3481             // scratch pad
3482             // p -> lpw = p -> lpw
3483             // core
3484             write_LPW (iom_unit_idx, chan);
3485           }
3486       }
3487 
3488     p -> lsFirst = false;
3489     // END
3490 
3491     return 0;
3492   }
3493 
3494 // 0 ok
3495 // -1 uff
3496 static int doPayloadChannel (uint iomUnitIdx, uint chan) {
     /* [previous][next][first][last][top][bottom][index][help] */
3497 #if defined(TESTING)
3498   cpu_state_t * cpup = _cpup;
3499   sim_debug (DBG_DEBUG, & iom_dev, "%s: Payload channel %c%02o\r\n", __func__, iomChar (iomUnitIdx), chan);
3500 #endif
3501 // A dubious assumption being made is that the device code will always
3502 // be in bits 6-12 of the DCw. Normally, the controller would
3503 // decipher the device code and route to the device, but we
3504 // have elided the controllers and must do that ourselves.
3505 
3506 // Loop logic
3507 //
3508 //   The DCW list can be terminated two ways; either by having the
3509 //   tally run out, or a IDCW with control set to zero.
3510 //
3511 //   The device handler will absorb DDCWs
3512 //
3513 // loop until:
3514 //
3515 //  listService sets ptro, indicating that no more DCWs are available. or
3516 //     control is 0, indicating last IDCW
3517 
3518   iom_chan_data_t * p = & iom_chan_data[iomUnitIdx][chan];
3519 
3520 #if defined(TESTING)
3521   word36 PCW_DCW        = p->DCW;
3522   word1  PCW_LPW_23_REL = p->LPW_23_REL;
3523 #endif
3524   p -> chanMode   = cm1;
3525   p -> LPW_18_RES = 0;
3526   p -> LPW_20_AE  = 0;
3527   p -> LPW_23_REL = 0;
3528 
3529   unpack_DCW (iomUnitIdx, chan);
3530 
3531   p->isPCW = true;
3532 
3533   // Masked is set in doConnectChannel
3534   //p->masked = !!p->PCW_21_MSK;
3535   struct iom_to_ctlr_s * d = & cables->iom_to_ctlr[iomUnitIdx][chan];
3536 
3537 // A device command of 051 in the PCW is only meaningful to the operator console;
3538 // all other channels should ignore it. We use (somewhat bogusly) a chanType of
3539 // chanTypeCPI to indicate the operator console.
3540   if (d->chan_type != chan_type_CPI && p -> IDCW_DEV_CMD == 051) {
3541     p->stati = 04501;
3542     send_terminate_interrupt (iomUnitIdx, chan);
3543     return 0;
3544   }
3545 
3546   if ((!d->in_use) || (!d->iom_cmd)) {
3547     p -> stati = 06000; // t, power off/missing
3548 #if defined(POLTS_TESTING)
3549 if (chan == 014)      if_sim_debug (DBG_TRACE, & iom_dev) sim_printf \
3550                         ("// terminate 10. ctrl == 0 in chan %d (%o) DCW\r\n", chan, chan);
3551 #endif
3552     goto terminate;
3553   }
3554 
3555 // 3.2.2. "Bits 12-17 [of the PCW] contain the address extension which is maintained by
3556 //         the channel for subsequent use by the IOM in generating a 24-bit
3557 //         address for list of data services for the extended address modes."
3558 // see also 3.2.3.1
3559   p -> ADDR_EXT     = p -> PCW_AE;
3560 
3561   p -> lsFirst      = true;
3562 
3563   p -> tallyResidue = 0;
3564   p -> isRead       = true;
3565   p -> charPos      = 0;
3566 // As far as I can tell, initiate false means that an IOTx succeeded in
3567 // transferring data; assume it didn't since that is the most common
3568 // code path.
3569   p -> initiate     = true;
3570   p -> chanStatus   = chanStatNormal;
3571 
3572 //
3573 // Send the PCW's DCW
3574 //
3575   int rc = d->iom_cmd (iomUnitIdx, chan);
3576 
3577   if (rc < 0) {
3578     p -> dev_code = getbits36_6 (p -> DCW, 6);
3579 #if defined(POLTS_TESTING)
3580 if (chan == 014)      if_sim_debug (DBG_TRACE, & iom_dev) \
3581                         sim_printf ("// terminate 9. ctrl == 0 in chan %d (%o) DCW\r\n", chan, chan);
3582 #endif
3583     goto terminate;
3584   }
3585 
3586   if (IS_IDCW (p) && p->IDCW_CHAN_CTRL == CHAN_CTRL_MARKER) { // IDCW marker bit set
3587     send_marker_interrupt (iomUnitIdx, (int) chan);
3588   }
3589 
3590   if (rc == IOM_CMD_DISCONNECT) {
3591 #if defined(POLTS_TESTING)
3592 if (chan == 014)      if_sim_debug (DBG_TRACE, & iom_dev) sim_printf \
3593                         ("// terminate 8. ctrl == 0 in chan %d (%o) DCW\r\n", chan, chan);
3594 #endif
3595     goto terminate;
3596   }
3597 
3598   if (p->masked) {
3599 #if defined(POLTS_TESTING)
3600 if (chan == 014)      if_sim_debug (DBG_TRACE, & iom_dev) sim_printf \
3601                         ("// terminate 7. ctrl == 0 in chan %d (%o) DCW\r\n", chan, chan);
3602 #endif
3603     goto terminate;
3604   }
3605 
3606   bool ptro, send, uff;
3607   bool terminate = false;
3608   p->isPCW       = false;
3609 
3610 #if defined(TESTING)
3611   bool first = true;
3612 #endif
3613 
3614   (void)terminate;
3615   bool idcw_terminate = p -> IDCW_CHAN_CTRL == CHAN_CTRL_TERMINATE;
3616   do {
3617     int rc2 = iom_list_service (iomUnitIdx, chan, & ptro, & send, & uff);
3618     if (rc2 < 0) {
3619 // XXX set status flags
3620       sim_warn ("%s list service failed\r\n", __func__);
3621       return -1;
3622     }
3623     if (uff) {
3624       // We get a uff if the LPW tally hit 0
3625 #if defined(POLTS_TESTING)
3626 if (chan == 014)      if_sim_debug (DBG_TRACE, & iom_dev) \
3627                         sim_printf ("// terminate 6. ctrl == 0 in chan %d (%o) DCW\r\n", chan, chan);
3628 #endif
3629       goto terminate;
3630     }
3631     // List service failed to get a DCW
3632     if (! send) {
3633       sim_warn ("%s nothing to send\r\n", __func__);
3634       return 1;
3635     }
3636 
3637 #if defined(TESTING)
3638 # if defined(POLTS_TESTING)
3639 if (iomUnitIdx == 1 && chan == 020)
3640 # endif
3641     if_sim_debug (DBG_TRACE, & iom_dev) {
3642       if (first) {
3643         first = false;
3644         dumpLPW (iomUnitIdx, chan);
3645         sim_printf ("// DCW list dump: \r\n");
3646         dumpDCW (PCW_DCW, PCW_LPW_23_REL);
3647         for (int i = 0; i < 8; i ++)
3648           dumpDCW (M[p->LPW_DCW_PTR - 1 + i], p->LPW_23_REL);
3649         sim_printf ("// \r\n");
3650       }
3651     }
3652 #endif
3653 
3654 // 3.2.3.1 "If EC - Bit 21 = 1, The channel will replace the present address
3655 //          extension with the new address extension in bits 12-17. ... In
3656 //          Multics and NSA systems, EC is inhibited from the payload channel
3657 //          if LPW bit 23 = 1.
3658 
3659     if (IS_IDCW (p)) { // IDCW
3660       idcw_terminate = p -> IDCW_CHAN_CTRL == CHAN_CTRL_TERMINATE;
3661       if (p -> LPW_23_REL == 0 && p -> IDCW_EC == 1)
3662         p -> ADDR_EXT = getbits36_6 (p -> DCW, 12);
3663 
3664 //      p -> recordResidue = 0;
3665       p -> tallyResidue = 0;
3666       p -> isRead       = true;
3667       p -> charPos      = 0;
3668       p -> chanStatus   = chanStatNormal;
3669     }
3670 
3671 // Send the DCW list's DCW
3672 
3673     rc2 = d->iom_cmd (iomUnitIdx, chan);
3674 
3675     if (rc2 < 0) {
3676       p -> dev_code = getbits36_6 (p -> DCW, 6);
3677 #if defined(POLTS_TESTING)
3678 if (chan == 014)      if_sim_debug (DBG_TRACE, & iom_dev) \
3679                         sim_printf ("// terminate 5. ctrl == 0 in chan %d (%o) DCW\r\n", chan, chan);
3680 #endif
3681       goto terminate;
3682     }
3683 
3684     if (rc2 == IOM_CMD_DISCONNECT) {
3685       terminate = true;
3686 #if defined(POLTS_TESTING)
3687 if (chan == 014)      if_sim_debug (DBG_TRACE, & iom_dev) sim_printf \
3688                         ("// terminate 4. ctrl == 0 in chan %d (%o) DCW\r\n", chan, chan);
3689 #endif
3690     }
3691 
3692     if (rc2 == IOM_CMD_PENDING) // handler still processing command, don't set
3693       goto pending;             // terminate interrupt.
3694 
3695     // If IDCW and terminate and nondata
3696     if (IS_IDCW (p) && p->IDCW_CHAN_CTRL == CHAN_CTRL_TERMINATE && p->IDCW_CHAN_CMD == CHAN_CMD_NONDATA) {
3697 #if defined(POLTS_TESTING)
3698 if (chan == 014)      if_sim_debug (DBG_TRACE, & iom_dev) \
3699                         sim_printf ("// terminate 1. ctrl == 0 in chan %d (%o) DCW\r\n", chan, chan);
3700 #endif
3701       goto terminate;
3702     }
3703     // If IOTD and last IDCW was terminate
3704     if (IS_IOTD (p) && idcw_terminate && rc2 != IOM_CMD_RESIDUE) {
3705 #if defined(POLTS_TESTING)
3706 //if (iomUnitIdx == 1 && chan == 020)      if_sim_debug (DBG_TRACE, & iom_dev)
3707 //                                           sim_printf ("// ctrl == 0 in chan %d (%o) IOTP\r\n", chan, chan);
3708 if (chan == 014)      if_sim_debug (DBG_TRACE, & iom_dev) \
3709                         sim_printf ("// terminate 2. ctrl == 0 in chan %d (%o) DCW\r\n", chan, chan);
3710 #endif
3711       goto terminate;
3712     }
3713 
3714     // IOM_CMD_RESIDUE: Continue pushing DCWS until the record residue is 0
3715 
3716     if (IS_NOT_IDCW (p) && rc2 == IOM_CMD_RESIDUE) {
3717       if (p->recordResidue)
3718         p->recordResidue --;
3719       if (p->recordResidue == 0)
3720         goto terminate;
3721     }
3722   } while (! terminate);
3723 
3724 terminate:;
3725 #if defined(POLTS_TESTING)
3726 if (iomUnitIdx == 1 && chan == 020) sim_printf ("stati %04o\r\n", p->stati);
3727 #endif
3728   send_terminate_interrupt (iomUnitIdx, chan);
3729   return 0;
3730 
3731 pending:;
3732   return 0;
3733 }
3734 
3735 // doConnectChan ()
3736 //
3737 // Process the "connect channel".  This is what the IOM does when it
3738 // receives a $CON signal.
3739 //
3740 // Only called by iom_interrupt ()
3741 //
3742 // The connect channel requests one or more "list services" and processes the
3743 // resulting PCW control words.
3744 //
3745 
3746 /*
3747  * doWrapAroundChan ()
3748  *
3749  * The wraparound channel (WAC), channel 004: a T&D channel that makes the
3750  * IOM Central perform one service per PCW "in lieu of" the test channel
3751  * named in the PCW. 43A239854 (6000B IOM EPS-1) 3.9. PM894 uses its direct
3752  * data load, read-clear and store services to make the IOM read or write
3753  * a word the CPU has cached (write notification).
3754  *
3755  * Even PCW word:
3756  *   0-17   bits 6-23 of the absolute address for direct data service
3757  *          (bits 12-17 are address bits 0-5 if bit 26 is on); bits 13-15
3758  *          are the interrupt cell for an interrupt service
3759  *   18-20  111
3760  *   21-23  service request: 1 list, 2 status, 3 interrupt, 4/5 indirect
3761  *          data load/store, 6/7 direct data load/store
3762  *   24     first list service; read clear for a single direct load
3763  *   25     byte size (indirect data)
3764  *   26     address extension control
3765  *   27-29  action: 0/1 single precision action PCW for data register 1/2,
3766  *          2 double precision action PCW, 3 force a bus parity error on an
3767  *          indirect store, 4/5 data PCW for data register 1/2, 6 data PCW
3768  *          for the status register, 7 load the bus flags into the status
3769  *          register
3770  *   30-35  test channel number
3771  * Data PCWs load the register from the 36 bits of the (even) PCW word and
3772  * make no request to Central. There is no automatic status store or
3773  * interrupt.
3774  */
3775 
3776 static void doWrapAroundChan (uint iom_unit_idx, word36 pcw0) {
     /* [previous][next][first][last][top][bottom][index][help] */
3777 #if defined(TESTING)
3778   cpu_state_t * cpup = _cpup;
3779 #endif
3780   iom_unit_data_t * u = & iom_unit_data[iom_unit_idx];
3781   word18 addrField = getbits36_18 (pcw0, 0);
3782   uint   src       = getbits36_3 (pcw0, 21);
3783   word1  mod       = getbits36_1 (pcw0, 24);
3784   word1  aec       = getbits36_1 (pcw0, 26);
3785   uint   action    = getbits36_3 (pcw0, 27);
3786   uint   testChan  = getbits36_6 (pcw0, 30);
3787 
3788   sim_debug (DBG_DEBUG, & iom_dev,
3789              "%s: IOM %c PCW %012llo src %o mod %o aec %o action %o chan %02o\r\n",
3790              __func__, iomChar (iom_unit_idx), (unsigned long long) pcw0,
3791              src, mod, aec, action, testChan);
3792 
3793   switch (action) {
3794     case 4: // data PCW for data register #1
3795     case 5: // data PCW for data register #2
3796       u->wacData[action - 4] = pcw0;
3797       return;
3798     case 6: // data PCW for the status register
3799       u->wacStatus = pcw0;
3800       return;
3801     case 7: // bus flags to the status register; no request to Central
3802       u->wacStatus = (word36) testChan;
3803       return;
3804     case 3: // force a bus parity error on an indirect data store
3805       sim_warn ("%s: WAC forced parity error not implemented\r\n", __func__);
3806       return;
3807     default: // 0, 1, 2: action PCW
3808       break;
3809   }
3810 
3811   bool dbl = action == 2;
3812   word24 addr = addrField;
3813   if (aec)
3814     addr = (word24) ((getbits36_6 (pcw0, 12) << 18) | addrField);
3815   // I/O bus signals left in the status register: DTA (bit 1) for a data
3816   // service, DBL (bit 2) for double precision; the test channel in 30-35.
3817   word36 bus = (word36) testChan;
3818 
3819   switch (src) {
3820     case 6: // direct data load
3821       if (dbl) {
3822         addr &= ~1u;
3823         iom_core_read2 (iom_unit_idx, addr, & u->wacData[0], & u->wacData[1], __func__);
3824       } else if (mod) { // read clear
3825         iom_core_read_lock (iom_unit_idx, addr, & u->wacData[action], __func__);
3826         iom_core_write_unlock (iom_unit_idx, addr, 0, __func__);
3827       } else {
3828         iom_core_read (iom_unit_idx, addr, & u->wacData[action], __func__);
3829       }
3830       putbits36_1 (& bus, 1, 1);
3831       putbits36_1 (& bus, 2, dbl ? 1 : 0);
3832       break;
3833 
3834     case 7: // direct data store
3835       if (dbl) {
3836         addr &= ~1u;
3837         iom_core_write2 (iom_unit_idx, addr, u->wacData[0], u->wacData[1], __func__);
3838       } else {
3839         iom_core_write (iom_unit_idx, addr, u->wacData[action], __func__);
3840       }
3841       putbits36_1 (& bus, 1, 1);
3842       putbits36_1 (& bus, 2, dbl ? 1 : 0);
3843       break;
3844 
3845     case 3: // program interrupt service: cell in bits 13-15, hit = test channel
3846       send_general_interrupt (iom_unit_idx, testChan, (enum iomImwPics) getbits36_3 (pcw0, 13));
3847       break;
3848 
3849     case 2: { // status service: store the status register via the test channel's SCW
3850         word36 scw;
3851         uint mbx = mbxLoc (iom_unit_idx, testChan);
3852         iom_core_read (iom_unit_idx, mbx + 2, & scw, __func__);
3853         iom_core_write (iom_unit_idx, getbits36_18 (scw, 0), u->wacStatus, __func__);
3854       }
3855       return; // the status register is what was stored; leave it
3856 
3857     default: // 0 invalid, 1 list service, 4/5 indirect data service
3858       sim_warn ("%s: WAC service request %o not implemented (PCW %012llo)\r\n",
3859                 __func__, src, (unsigned long long) pcw0);
3860       return;
3861   }
3862   u->wacStatus = bus;
3863 }
3864 
3865 static int doConnectChan (uint iom_unit_idx) {
     /* [previous][next][first][last][top][bottom][index][help] */
3866   // ... the connect channel obtains a list service from the IOM Central.
3867   // During this service the IOM Central will do a double precision read
3868   // from the core, under the control of the LPW for the connect channel.
3869   //
3870   // If the IOM Central does not indicate a PTRO during the list service,
3871   // the connect channel obtains another list service.
3872   //
3873   // The connect channel does not interrupt or store status.
3874   //
3875   // The DCW and SCW mailboxes for the connect channel are not used by
3876   // the IOM.
3877 #if defined(TESTING)
3878   cpu_state_t * cpup = _cpup;
3879 #endif
3880   sim_debug (DBG_DEBUG, & iom_dev, "%s: Connect channel\r\n", __func__);
3881   iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][IOM_CONNECT_CHAN];
3882   p -> lsFirst = true;
3883 #if defined(TESTING) && defined(POLTS_TESTING)
3884   bool first = true;
3885 #endif
3886   bool ptro, send, uff;
3887   do {
3888     // Fetch the next PCW
3889     int rc = iom_list_service (iom_unit_idx, IOM_CONNECT_CHAN, & ptro, & send, & uff);
3890     if (rc < 0) {
3891       if (! tandd_io_mode ()) // see iom_fault
3892         sim_warn ("connect channel connect failed\r\n");
3893       return -1;
3894     }
3895     if (uff) {
3896       sim_warn ("connect channel ignoring uff\r\n"); // XXX
3897     }
3898     if (! send) {
3899       sim_warn ("connect channel nothing to send\r\n");
3900     } else {
3901 #if defined(xTESTING)
3902       if_sim_debug (DBG_TRACE, & iom_dev) {
3903         if (first) {
3904           first = false;
3905           sim_printf ("// CIOC %lld\r\n", cpu.cycleCnt);
3906           dumpLPW (iom_unit_idx, IOM_CONNECT_CHAN);
3907           sim_printf ("// PCW %012llo %012llo\r\n",    p->PCW0, p->PCW1);
3908           sim_printf ("//   Chan info     %04o\r\n",   p->PCW_CHAN);
3909           sim_printf ("//   Addr ext        %02o\r\n", p->PCW_AE);
3910           sim_printf ("//   111              %o\r\n",  getbits36_3 (p->PCW0, 18));
3911           sim_printf ("//   M                %o\r\n",  p->PCW_21_MSK);
3912           sim_printf ("//   Chan info %08o\r\n",       getbits36_14 (p->PCW0, 22));
3913           sim_printf ("//   Pg Tbl Ptr  %06o\r\n",     p->PCW_PAGE_TABLE_PTR);
3914           sim_printf ("//   PTP              %o\r\n",  p->PCW_63_PTP);
3915           sim_printf ("//   PGE              %o\r\n",  p->PCW_64_PGE);
3916           sim_printf ("//   AUX              %o\r\n",  p->PCW_65_AUX);
3917           sim_printf ("//\r\n");
3918         }
3919       }
3920 #endif
3921       // A PCW for the wraparound channel is serviced at once. Its service
3922       // request bits overlap the PCW mask bit (21), so it must not go
3923       // through the payload channel logic below.
3924       if (p -> PCW_CHAN == IOM_WAC_CHAN &&
3925           ! cables->iom_to_ctlr[iom_unit_idx][IOM_WAC_CHAN].in_use) {
3926         doWrapAroundChan (iom_unit_idx, p -> PCW0);
3927         continue;
3928       }
3929 
3930       // Copy the PCW's DCW to the payload channel
3931       iom_chan_data_t * q = & iom_chan_data[iom_unit_idx][p -> PCW_CHAN];
3932 
3933       q -> PCW0               = p -> PCW0;
3934       q -> PCW1               = p -> PCW1;
3935       q -> PCW_CHAN           = p -> PCW_CHAN;
3936       q -> PCW_AE             = p -> PCW_AE;
3937       q -> PCW_PAGE_TABLE_PTR = p -> PCW_PAGE_TABLE_PTR;
3938       q -> PCW_63_PTP         = p -> PCW_63_PTP;
3939       q -> PCW_64_PGE         = p -> PCW_64_PGE;
3940       q -> PCW_65_AUX         = p -> PCW_65_AUX;
3941       q -> PCW_21_MSK         = p -> PCW_21_MSK;
3942       q -> DCW                = p -> DCW;
3943 
3944       sim_debug (DBG_DEBUG, & iom_dev, "%s: PCW %012llo %012llo chan %02o\r\n", __func__, q->PCW0, q->PCW1, q->PCW_CHAN);
3945 
3946       q -> masked = p -> PCW_21_MSK;
3947       if (q -> masked) {
3948         if (q -> in_use)
3949           sim_warn ("%s: chan %d masked while in use\r\n", __func__, p -> PCW_CHAN);
3950         q -> in_use = false;
3951         q -> start  = false;
3952       } else {
3953         if (q -> in_use)
3954           sim_warn ("%s: chan %d connect while in use\r\n", __func__, p -> PCW_CHAN);
3955         q -> in_use = true;
3956         q -> start  = true;
3957 #if defined(IO_THREADZ)
3958         setChnConnect (iom_unit_idx, p -> PCW_CHAN);
3959 #else
3960 # if !defined(IO_ASYNC_PAYLOAD_CHAN) && !defined(IO_ASYNC_PAYLOAD_CHAN_THREAD)
3961         doPayloadChannel (iom_unit_idx, p -> PCW_CHAN);
3962 # endif
3963 # if defined(IO_ASYNC_PAYLOAD_CHAN_THREAD)
3964         pthread_cond_signal (& iomCond);
3965 # endif
3966 #endif
3967       }
3968     }
3969   } while (! ptro);
3970   return 0; // XXX
3971 }
3972 
3973 /*
3974  * send_marker_interrupt ()
3975  *
3976  * Send a "marker" interrupt to the CPU.
3977  *
3978  * Channels send marker interrupts to indicate normal completion of
3979  * a PCW or IDCW if the control field of the PCW/IDCW has a value
3980  * of three.
3981  */
3982 
3983 int send_marker_interrupt (uint iom_unit_idx, int chan)
     /* [previous][next][first][last][top][bottom][index][help] */
3984   {
3985     if (iom_chan_data [iom_unit_idx] [chan] . masked)
3986       return(0);
3987     status_service (iom_unit_idx, (uint) chan, true);
3988     return send_general_interrupt (iom_unit_idx, (uint) chan, imwMarkerPic);
3989   }
3990 
3991 /*
3992  * send_special_interrupt ()
3993  *
3994  * Send a "special" interrupt to the CPU.
3995  *
3996  */
3997 
3998 int send_special_interrupt (uint iom_unit_idx, uint chan, uint devCode,
     /* [previous][next][first][last][top][bottom][index][help] */
3999                             word8 status0, word8 status1)
4000   {
4001 //if (iom_unit_idx == 0 && chan == 013) sim_printf ("spec int %03o %013o\r\n", status0, status1);
4002     uint chanloc = mbxLoc (iom_unit_idx, IOM_SPECIAL_STATUS_CHAN);
4003 
4004     if (iom_chan_data [iom_unit_idx] [chan] . masked)
4005       return(0);
4006 
4007 #if defined(LOCKLESS)
4008     lock_iom();
4009 #endif
4010 
4011 // Multics uses an 12(8) word circular queue, managed by clever manipulation
4012 // of the LPW and DCW.
4013 // Rather then goes through the mechanics of parsing the LPW and DCW,
4014 // we will just assume that everything is set up the way we expect,
4015 // and update the circular queue.
4016     word36 lpw;
4017     iom_core_read (iom_unit_idx, chanloc + IOM_MBX_LPW, & lpw, __func__);
4018 
4019     word36 scw;
4020     iom_core_read (iom_unit_idx, chanloc + IOM_MBX_SCW, & scw, __func__);
4021 
4022     word36 dcw;
4023     iom_core_read_lock (iom_unit_idx, chanloc + IOM_MBX_DCW, & dcw, __func__);
4024 
4025     word36 status  = 0400000000000ull;
4026     status        |= (((word36) chan)    & MASK6) << 27;
4027     status        |= (((word36) devCode) & MASK8) << 18;
4028     status        |= (((word36) status0) & MASK8) <<  9;
4029     status        |= (((word36) status1) & MASK8) <<  0;
4030     iom_core_write (iom_unit_idx, (dcw >> 18) & MASK18, status, __func__);
4031 
4032     uint tally = dcw & MASK12;
4033     if (tally > 1)
4034       {
4035         dcw -= 01llu;  // tally --
4036         dcw += 01000000llu; // addr ++
4037       }
4038     else
4039       dcw = scw; // reset to beginning of queue
4040     iom_core_write_unlock (iom_unit_idx, chanloc + IOM_MBX_DCW, dcw, __func__);
4041 
4042 #if defined(LOCKLESS)
4043     unlock_iom();
4044 #endif
4045 
4046     send_general_interrupt (iom_unit_idx, IOM_SPECIAL_STATUS_CHAN, imwSpecialPic);
4047     return 0;
4048   }
4049 
4050 /*
4051  * send_terminate_interrupt ()
4052  *
4053  * Send a "terminate" interrupt to the CPU.
4054  *
4055  * Channels send a terminate interrupt after doing a status service.
4056  *
4057  */
4058 
4059 int send_terminate_interrupt (uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
4060   {
4061     if (iom_chan_data [iom_unit_idx] [chan] . masked)
4062       return 0;
4063     status_service (iom_unit_idx, chan, false);
4064     if (iom_chan_data [iom_unit_idx] [chan] . in_use == false)
4065       sim_warn ("%s: chan %d not in use\r\n", __func__, chan);
4066     iom_chan_data [iom_unit_idx] [chan] . in_use = false;
4067     send_general_interrupt (iom_unit_idx, chan, imwTerminatePic);
4068     return 0;
4069   }
4070 
4071 void iom_interrupt (uint scu_unit_idx, uint iom_unit_idx)
     /* [previous][next][first][last][top][bottom][index][help] */
4072   {
4073     cpu_state_t * cpup = _cpup;
4074     sim_debug (DBG_DEBUG, & iom_dev,
4075                "%s: IOM %c starting. [%"PRId64"] %05o:%08o\r\n",
4076                __func__, iomChar (iom_unit_idx), cpu.cycleCnt, cpu.PPR.PSR, cpu.PPR.IC);
4077 
4078     iom_unit_data[iom_unit_idx].invokingScuUnitIdx = scu_unit_idx;
4079 
4080 #if defined(IO_THREADZ)
4081     setIOMInterrupt (iom_unit_idx);
4082     iomDoneWait (iom_unit_idx);
4083 #else
4084     int ret = doConnectChan (iom_unit_idx);
4085 
4086     sim_debug (DBG_DEBUG, & iom_dev,
4087                "%s: IOM %c finished; doConnectChan returned %d.\r\n",
4088                __func__, iomChar (iom_unit_idx), ret);
4089 #endif
4090     // XXX doConnectChan return value ignored
4091   }
4092 
4093 #if defined(IO_THREADZ)
4094 void * chan_thread_main (void * arg)
     /* [previous][next][first][last][top][bottom][index][help] */
4095   {
4096     uint myid     = (uint) * (int *) arg;
4097     this_iom_idx  = (uint) myid / MAX_CHANNELS;
4098     this_chan_num = (uint) myid % MAX_CHANNELS;
4099 
4100 // Set CPU context to allow sim_debug to work
4101 
4102     set_cpu_idx (0);
4103 
4104     sim_msg ("\rIOM %c Channel %u thread created\r\n", this_iom_idx + 'a', this_chan_num);
4105 # if defined(TESTING)
4106     printPtid(pthread_self());
4107 # endif /* if defined(TESTING) */
4108     if (realtime_ok) {
4109       set_realtime_priority (pthread_self(), realtime_max_priority() - 1);
4110       check_realtime_priority (pthread_self(), realtime_max_priority() - 1);
4111     } else {
4112 # if !defined(__QNX__)
4113       (void)sim_os_set_thread_priority (PRIORITY_ABOVE_NORMAL);
4114 # endif
4115     }
4116 
4117     setSignals ();
4118     while (1)
4119       {
4120 //sim_printf("IOM %c Channel %u thread waiting\r\n", this_iom_idx + 'a', this_chan_num);
4121         chnConnectWait ();
4122 //sim_printf("IOM %c Channel %u thread running\r\n", this_iom_idx + 'a', this_chan_num);
4123         doPayloadChannel (this_iom_idx, this_chan_num);
4124         chnConnectDone ();
4125       }
4126   }
4127 
4128 void * iom_thread_main (void * arg)
     /* [previous][next][first][last][top][bottom][index][help] */
4129   {
4130     int myid = * (int *) arg;
4131     this_iom_idx = (uint) myid;
4132 
4133 // Set CPU context to allow sim_debug to work
4134 
4135     set_cpu_idx (0);
4136 
4137     sim_printf("IOM %c thread created\r\n", 'a' + myid);
4138 
4139     setSignals ();
4140     while (1)
4141       {
4142 //sim_printf("IOM %c thread waiting\r\n", 'a' + myid);
4143         iomInterruptWait ();
4144 //sim_printf("IOM %c thread running\r\n", 'a' + myid);
4145         int ret = doConnectChan (this_iom_idx);
4146 
4147         sim_debug (DBG_DEBUG, & iom_dev,
4148                    "%s: IOM %c finished; doConnectChan returned %d.\r\n",
4149                    __func__, iomChar (myid), ret);
4150         iomInterruptDone ();
4151       }
4152   }
4153 #endif
4154 
4155 /*
4156  * iom_init ()
4157  *
4158  *  Once-only initialization
4159  */
4160 
4161 void iom_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
4162   {
4163     //sim_debug (DBG_INFO, & iom_dev, "%s: running.\r\n", __func__);
4164   }
4165 
4166 #if defined(PANEL68)
4167 void do_boot (void)
     /* [previous][next][first][last][top][bottom][index][help] */
4168   {
4169     boot_svc (& boot_channel_unit[0]);
4170   }
4171 #endif
4172 
4173 #if defined(IO_ASYNC_PAYLOAD_CHAN) || defined(IO_ASYNC_PAYLOAD_CHAN_THREAD)
4174 void iomProcess (void)
     /* [previous][next][first][last][top][bottom][index][help] */
4175   {
4176     for (uint i = 0; i < N_IOM_UNITS_MAX; i++)
4177       for (uint j = 0; j < MAX_CHANNELS; j++)
4178         {
4179           iom_chan_data_t * p = &iom_chan_data [i] [j];
4180           if (p -> start)
4181             {
4182               p -> start = false;
4183               doPayloadChannel (i, j);
4184             }
4185         }
4186   }
4187 #endif
4188 
4189 char iomChar (uint iomUnitIdx)
     /* [previous][next][first][last][top][bottom][index][help] */
4190   {
4191     return (iom_unit_data[iomUnitIdx].configSwMultiplexBaseAddress & 3) + 'A';
4192   }

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