root/src/dps8/dps8_disk.c

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

DEFINITIONS

This source file includes following definitions.
  1. disk_show_nunits
  2. disk_set_nunits
  3. disk_show_type
  4. disk_set_type
  5. dsk_show_device_name
  6. dsk_set_device_name
  7. signal_disk_ready
  8. disk_set_ready
  9. unloadDisk
  10. loadDisk
  11. disk_reset
  12. disk_attach
  13. disk_init
  14. diskSeek64
  15. diskSeek512
  16. diskSeekSpecial
  17. diskRead
  18. diskWrite
  19. readStatusRegister
  20. diskRdCtrlReg
  21. read_configuration
  22. read_and_clear_statistics
  23. dsk_iom_cmd
  24. ipc_show_nunits
  25. ipc_set_nunits
  26. ipc_show_device_name
  27. ipc_set_device_name
  28. msp_show_nunits
  29. msp_set_nunits
  30. msp_show_device_name
  31. msp_set_device_name

   1 /*
   2  * vim: filetype=c:tabstop=4:ai:expandtab
   3  * SPDX-License-Identifier: ICU
   4  * SPDX-License-Identifier: Multics
   5  * scspell-id: 22079097-f62e-11ec-b987-80ee73e9b8e7
   6  *
   7  * ---------------------------------------------------------------------------
   8  *
   9  * Copyright (c) 2007-2013 Michael Mondy
  10  * Copyright (c) 2012-2016 Harry Reed
  11  * Copyright (c) 2013-2018 Charles Anthony
  12  * Copyright (c) 2021-2025 The DPS8M Development Team
  13  *
  14  * This software is made available under the terms of the ICU License.
  15  * See the LICENSE.md file at the top-level directory of this distribution.
  16  *
  17  * ---------------------------------------------------------------------------
  18  *
  19  * This source file may contain code comments that adapt, include, and/or
  20  * incorporate Multics program code and/or documentation distributed under
  21  * the Multics License.  In the event of any discrepancy between code
  22  * comments herein and the original Multics materials, the original Multics
  23  * materials should be considered authoritative unless otherwise noted.
  24  * For more details and historical background, see the LICENSE.md file at
  25  * the top-level directory of this distribution.
  26  *
  27  * ---------------------------------------------------------------------------
  28  */
  29 
  30 //-V::536
  31 
  32 // source/library_dir_dir/system_library_1/source/bound_volume_rldr_ut_.s.archive/rdisk_.pl1
  33 // source/library_dir_dir/system_library_1/source/bound_rcp_.s.archive/rcp_disk_.pl1
  34 
  35 #include <stdio.h>
  36 
  37 #include "dps8.h"
  38 #include "dps8_iom.h"
  39 #include "dps8_disk.h"
  40 #include "dps8_sys.h"
  41 #include "dps8_cable.h"
  42 #include "dps8_cpu.h"
  43 #include "dps8_faults.h"
  44 #include "dps8_scu.h"
  45 #include "../simh/sim_disk.h"
  46 #include "dps8_utils.h"
  47 
  48 #if defined(LOCKLESS)
  49 # include "threadz.h"
  50 #endif /* if defined(LOCKLESS) */
  51 
  52 #define DBG_CTR 1
  53 
  54 //
  55 // A possible disk data packing algorithm
  56 //
  57 // Currently sectors are 512 * word36. Word36 is 64bits; that 56% utilization,
  58 // A computationally efficient packing would improve disk throughput and storage
  59 // efficiency
  60 //
  61 //     word36 in[512]
  62 //     struct dsksec
  63 //       {
  64 //         uint32 low32[512];
  65 //         uint8 high4[512];
  66 //       } out;
  67 //     uint32 * map = (uint32 *) in;
  68 //     uint8 * highmap = (uint8 *) in;
  69 //
  70 //     for (uint i = 0; i < 512; i ++)
  71 //       {
  72 //         out . low32[i] = map[i * 2];
  73 //         out . high4[i] = (uint8) (map[i * 2 + 1]);
  74 //       }
  75 //
  76 // This 36/40 -- 90% utilization; at the cost of the scatter/gather and
  77 // the cost of emulated disk sectors size not a multiple of host disk
  78 // sector size.
  79 //
  80 
  81 // dau_type stat_mpc_.pl1
  82 //
  83 //  dcl  ddev_model (0:223) char (6) static options (constant)
  84 //          init ((84) (""), "  190A", (52) (""), "  190B", "   401", "  190B", (14) (""), "   451", (31) (""), "   402",
  85 //          (13) (""), "   500", "   501", "   500", "   501", "   500", "   501", "   500", "   501", (9) (""), "   509",
  86 //          "", "   509", "", "   509", "", "   509");
  87 
  88 //  84   0:83   ""
  89 //   1  84      "190A"
  90 //  52  85:135  ""
  91 //   1  137     "190B
  92 //   1  138     "401"
  93 //   1  139     "190B"
  94 //  14  140:153 ""
  95 //   1  154     "451"
  96 //  31  155:185 ""
  97 //   1  186     "402"
  98 //  13  175:199 ""
  99 //   1  200     "500"
 100 //   1  201     "501"
 101 //   1  202     "500"
 102 //   1  203     "501"
 103 //   1  204     "500"
 104 //   1  205     "501"
 105 //   1  206     "500"
 106 //   1  207     "501"
 107 //   9  208:216 ""
 108 //   1  217     "509"
 109 //   1  218     ""
 110 //   1  219     "509"
 111 //   1  220     ""
 112 //   1  221     "509"
 113 //   1  222     ""
 114 //   1  223     "509"
 115 
 116 // fs_dev.sect_per_dev:
 117 //           vfd       36/4000000          Bulk
 118 //           vfd       36/814*40*19        MSU0500
 119 //           vfd       36/814*40*19        MSU0450
 120 //           vfd       36/410*40*19        MSU0400
 121 //           vfd       36/410*31*19        DSU190
 122 //           vfd       36/202*18*20        DSU181
 123 //           vfd       36/840*64*20        MSU0501
 124 //           vfd       36/885*255          FIPS 3380
 125 //           vfd       36/1770*255         FIPS 3381
 126 
 127 /* table of # last sector number for each device */
 128 //dcl  last_sect_num (9) fixed bin (24) static options (constant) init
 129 //     (0, 618639, 616359, 309319, 239722, 71999, 1075199, 225674, 451349);
 130 /* table of # of last sector on device (includes T&D cylinders) */
 131 //dcl  last_physical_sect_num (9) fixed bin (24) static options (constant) init
 132 //     (0, 639919, 619399, 312359, 242249, 72359, 1077759, 225674, 451859);
 133 
 134 //          last     t&d last
 135 // D500     618639    639919
 136 // D451     616359    619399
 137 // D400     309319    312359
 138 // D190     239722    242249
 139 // D181      71999     72359
 140 // D501    1075199   1077759
 141 // 3380     225674    225674
 142 // 3381     451349    451859
 143 
 144 struct diskType_t diskTypes[] = {
 145   { "3381",  451350,  0, false, seek_512, 512,   0 }, // disk_init assumes 3381 is at index 0
 146   { "d500",  618640,  1, false, seek_64,   64, 200 },
 147   { "d451",  616360,  1, true,  seek_64,   64, 154 },
 148   { "d400",  309320,  1, true,  seek_64,   64,  84 }, // d400 is a d190 with "high-efficiency format (40 sectors/track)"
 149   { "d190",  239723,  1, true,  seek_64,   64,  84 }, // 190A 84, 190B 137
 150   { "d181",   72000,  1, true,  seek_64,   64,   0 }, // no idea what the dau idx is
 151   { "d501", 1075200,  1, false, seek_64,   64, 201 },
 152   { "3380",  225675,  0, false, seek_512, 512,   0 }, // 338x is never attached to a dau
 153 };
 154 #define N_DISK_TYPES (sizeof (diskTypes) / sizeof (struct diskType_t))
 155 
 156 static uint tAndDCapac[N_DISK_TYPES] = { 451860, 639920, 619400, 312360, 242250, 72360, 1077760, 225675 };
 157 
 158 #define M3381_SECTORS (1770*255)
 159 //#define M3381_SECTORS 6895616
 160 // records per subdev: 74930 (127 * 590)
 161 // number of sub-volumes: 3
 162 // records per dev: 3 * 74930 = 224790
 163 // cyl/sv: 590
 164 // cyl: 1770 (3*590)
 165 // rec/cyl 127
 166 // tracks/cyl 15
 167 // sector size: 512
 168 // sectors: 451858
 169 // data: 3367 MB, 3447808 KB, 6895616 sectors,
 170 //  3530555392 bytes, 98070983 records?
 171 
 172 #define N_DISK_UNITS 2 // default
 173 
 174 struct dsk_state dsk_states [N_DSK_UNITS_MAX];
 175 
 176 //-- // extern t_stat disk_svc(UNIT *up);
 177 
 178 // ./library_dir_dir/include/fs_dev_types.incl.alm
 179 //
 180 // From IBM GA27-1661-3_IBM_3880_Storage_Control_Description_May80, pg 4.4:
 181 //
 182 //  The Seek command transfers the six-byte seek address from the channel to
 183 //  the storage director....
 184 //
 185 //     Bytes 0-5: 0 0 C C 0 H
 186 //
 187 //       Model       Cmax   Hmax
 188 //       3330-1       410     18
 189 //       3330-11      814     18
 190 //       3340 (35MB)  348     11
 191 //       3340 (70MB)  697     11
 192 //       3344         697     11
 193 //       3350         559     29
 194 //
 195 //  Search Identifier Equal [CC HH R]
 196 //
 197 
 198 // ./library_dir_dir/system_library_1/source/bound_page_control.s.archive/disk_control.pl1
 199 //
 200 // dcl     devadd             fixed bin (18);              /* record number part of device address */
 201 //
 202 //  /* Compute physical sector address from input info.  Physical sector result
 203 //   accounts for unused sectors per cylinder. */
 204 //
 205 //        if pvte.is_sv then do;                  /* convert the subvolume devadd to the real devadd */
 206 //             record_offset = mod (devadd, pvte.records_per_cyl);
 207 //             devadd = ((devadd - record_offset) * pvte.num_of_svs) + pvte.record_factor + record_offset;
 208 //        end;
 209 //        sector = devadd * sect_per_rec (pvte.device_type);/* raw sector. */
 210 //        cylinder = divide (sector, pvtdi.usable_sect_per_cyl, 12, 0);
 211 //        sector = sector + cylinder * pvtdi.unused_sect_per_cyl;
 212 //        sector = sector + sect_off;                     /* sector offset, if any. */
 213 //
 214 
 215 // DB37rs DSS190 Disk Subsystem, pg. 27:
 216 //
 217 //   Seek instruction:
 218 //     Sector count limit, bits 0-11: These bits define the binary sector count.
 219 //     All zeros is a maximum count of 4096.
 220 //   Track indicator, bits 12-13:
 221 //     These bits indicate a complete track as good, defective, or alternate.
 222 //       00 = primary track - good
 223 //       01 = alternate track - good
 224 //       10 = defective track - alternate track assigned
 225 //       11 = defective track - no alternate track assigned
 226 //
 227 //   Sector address, bits 16-35
 228 //
 229 //      0                                                  35
 230 //      XXXX  XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX 0000
 231 //        BYTE 0         1           2           3           4
 232 //
 233 //  Seek        011100
 234 //  Read        010101
 235 //  Read ASCII  010011
 236 //  Write       011001
 237 //  Write ASCII 011010
 238 //  Write and compare
 239 //              011011
 240 //  Request status
 241 //              000000
 242 //  reset status
 243 //              100000
 244 //  bootload control store
 245 //              001000
 246 //  itr boot    001001
 247 //
 248 
 249 #if defined(POLTS_DISK_TESTING)
 250 static int nCmds = 0;
 251 #endif /* if defined(POLTS_DISK_TESTING) */
 252 
 253 #define UNIT_FLAGS ( UNIT_FIX | UNIT_ATTABLE | UNIT_ROABLE | \
 254                      UNIT_DISABLE | UNIT_IDLE | DKUF_F_STD )
 255 UNIT dsk_unit[N_DSK_UNITS_MAX] = {
 256 #if defined(NO_C_ELLIPSIS)
 257   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 258   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 259   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 260   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 261   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 262   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 263   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 264   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 265   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 266   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 267   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 268   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 269   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 270   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 271   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 272   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 273   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 274   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 275   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 276   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 277   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 278   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 279   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 280   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 281   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 282   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 283   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 284   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 285   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 286   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 287   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 288   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 289   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 290   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 291   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 292   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 293   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 294   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 295   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 296   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 297   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 298   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 299   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 300   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 301   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 302   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 303   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 304   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 305   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 306   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 307   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 308   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 309   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 310   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 311   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 312   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 313   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 314   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 315   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 316   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 317   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 318   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 319   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 320   { UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL }
 321 #else
 322   [0 ... N_DSK_UNITS_MAX-1] = {
 323     UDATA (/* & disk_svc */ NULL, UNIT_FLAGS, M3381_SECTORS), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
 324   }
 325 #endif
 326 };
 327 
 328 #define DSK_UNIT_IDX(uptr) ((uptr) - dsk_unit)
 329 
 330 static DEBTAB disk_dt[] =
 331   {
 332     { "TRACE",  DBG_TRACE,  NULL },
 333     { "NOTIFY", DBG_NOTIFY, NULL },
 334     { "INFO",   DBG_INFO,   NULL },
 335     { "ERR",    DBG_ERR,    NULL },
 336     { "WARN",   DBG_WARN,   NULL },
 337     { "DEBUG",  DBG_DEBUG,  NULL },
 338     { "ALL",    DBG_ALL,    NULL }, // Don't move as it messes up DBG message
 339     { NULL,     0,          NULL }
 340   };
 341 
 342 static t_stat disk_show_nunits (UNUSED FILE * st, UNUSED UNIT * uptr, UNUSED int val, UNUSED const void * desc)
     /* [previous][next][first][last][top][bottom][index][help] */
 343   {
 344     sim_printf("Number of DISK units in system is %d\r\n", dsk_dev . numunits);
 345     return SCPE_OK;
 346   }
 347 
 348 static t_stat disk_set_nunits (UNUSED UNIT * uptr, UNUSED int32 value, const char * cptr, UNUSED void * desc)
     /* [previous][next][first][last][top][bottom][index][help] */
 349   {
 350     if (! cptr)
 351       return SCPE_ARG;
 352     int n = atoi (cptr);
 353     if (n < 1 || n > N_DSK_UNITS_MAX)
 354       return SCPE_ARG;
 355     dsk_dev . numunits = (uint32) n;
 356     return SCPE_OK;
 357   }
 358 
 359 static t_stat disk_show_type (UNUSED FILE * st, UNUSED UNIT * uptr, UNUSED int val, UNUSED const void * desc)
     /* [previous][next][first][last][top][bottom][index][help] */
 360   {
 361     int diskUnitIdx = (int) DSK_UNIT_IDX (uptr);
 362     if (diskUnitIdx < 0 || diskUnitIdx >= N_DSK_UNITS_MAX)
 363       {
 364         sim_printf ("error: Invalid unit number %ld\r\n", (long) diskUnitIdx);
 365         return SCPE_ARG;
 366       }
 367 
 368     sim_printf("type     : %s", diskTypes[dsk_states[diskUnitIdx].typeIdx].typename);
 369 
 370     return SCPE_OK;
 371   }
 372 
 373 static t_stat disk_set_type (UNUSED UNIT * uptr, UNUSED int32 value, const char * cptr, UNUSED void * desc)
     /* [previous][next][first][last][top][bottom][index][help] */
 374   {
 375     int diskUnitIdx = (int) DSK_UNIT_IDX (uptr);
 376     if (diskUnitIdx < 0 || diskUnitIdx >= N_DSK_UNITS_MAX)
 377       {
 378         sim_printf ("error: Invalid unit number %ld\r\n", (long) diskUnitIdx);
 379         return SCPE_ARG;
 380       }
 381 
 382     uint i;
 383     for (i = 0; i < N_DISK_TYPES; i++)
 384       {
 385         if (strcasecmp (cptr, diskTypes[i].typename) == 0)
 386           break;
 387       }
 388     if (i >= N_DISK_TYPES)
 389      {
 390        sim_printf ("Disk type %s unrecognized, expected one of "
 391                    "%s %s %s %s %s %s %s %s\r\n",
 392                    cptr,
 393                    diskTypes[0].typename,
 394                    diskTypes[1].typename,
 395                    diskTypes[2].typename,
 396                    diskTypes[3].typename,
 397                    diskTypes[4].typename,
 398                    diskTypes[5].typename,
 399                    diskTypes[6].typename,
 400                    diskTypes[7].typename);
 401         return SCPE_ARG;
 402       }
 403     dsk_states[diskUnitIdx].typeIdx = i;
 404     dsk_unit[diskUnitIdx].capac = (t_addr) diskTypes[i].capac;
 405     dsk_states[diskUnitIdx].tAndDCapac = tAndDCapac[i];
 406     return SCPE_OK;
 407   }
 408 
 409 static t_stat dsk_show_device_name (UNUSED FILE * st, UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
 410                                     UNUSED int val, UNUSED const void * desc)
 411   {
 412     int n = (int) DSK_UNIT_IDX (uptr);
 413     if (n < 0 || n >= N_DSK_UNITS_MAX)
 414       return SCPE_ARG;
 415     sim_printf("name     : %s", dsk_states[n].device_name);
 416     return SCPE_OK;
 417   }
 418 
 419 static t_stat dsk_set_device_name (UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
 420                                    const char * cptr, UNUSED void * desc)
 421   {
 422     int n = (int) DSK_UNIT_IDX (uptr);
 423     if (n < 0 || n >= N_DSK_UNITS_MAX)
 424       return SCPE_ARG;
 425     if (cptr)
 426       {
 427         strncpy (dsk_states[n].device_name, cptr, MAX_DEV_NAME_LEN-1);
 428         dsk_states[n].device_name[MAX_DEV_NAME_LEN-1] = 0;
 429       }
 430     else
 431       dsk_states[n].device_name[0] = 0;
 432     return SCPE_OK;
 433   }
 434 
 435 //
 436 // Looking at rcp_disk.pl1, it appears that the special interrupt for disks
 437 // only causes rcp to poll the device for status; there don't appear to
 438 // be any status specific bits to send here; signal ready is a misnomer;
 439 // it is just signal.
 440 
 441 t_stat signal_disk_ready (uint dsk_unit_idx) {
     /* [previous][next][first][last][top][bottom][index][help] */
 442   // Don't signal if the sim is not actually running....
 443   if (! sim_is_running)
 444     return SCPE_OK;
 445   // if substr (special_status_word, 20, 1) ^= "1"b | substr (special_status_word, 13, 6) ^= "00"b3
 446   // if substr (special_status_word, 34, 3) ^= "001"b
 447   // Note the 34,3 spans 34,35,36; therefore the bits are 1..36, not 0..35
 448   // 20,1 is bit 19
 449   // 13,6, is bits 12..17
 450   // status0 is 19..26
 451   // status1 is 28..35
 452   // so substr (w, 20, 1) is bit 0 of status0
 453   //    substr (w, 13, 6) is the low 6 bits of dev_no
 454   //    substr (w, 34, 3) is the low 3 bits of status 1
 455       //sim_printf ("%s %d %o\r\n", disk_filename, ro,  mt_unit[dsk_unit_idx] . flags);
 456       //sim_printf ("special int %d %o\r\n", dsk_unit_idx, mt_unit[dsk_unit_idx] . flags);
 457 
 458   uint ctlr_unit_idx = cables->dsk_to_ctlr[dsk_unit_idx].ctlr_unit_idx;
 459   enum ctlr_type_e ctlr_type = cables->dsk_to_ctlr[dsk_unit_idx].ctlr_type;
 460   if (ctlr_type != CTLR_T_MSP && ctlr_type != CTLR_T_IPC) {
 461     // If None, assume that the cabling hasn't happened yet.
 462     if (ctlr_type != CTLR_T_NONE) {
 463       sim_warn ("loadDisk lost\r\n");
 464       return SCPE_ARG;
 465     }
 466     return SCPE_OK;
 467   }
 468 
 469 
 470 
 471 
 472 
 473 
 474 
 475 
 476 
 477 
 478 
 479 
 480 
 481 
 482 
 483 
 484 
 485 
 486 
 487 
 488 
 489 
 490 
 491 
 492 
 493 
 494 
 495 
 496 
 497 
 498 
 499 
 500 
 501 
 502 
 503 
 504 
 505 
 506 
 507   for (uint ctlr_port_num = 0; ctlr_port_num < MAX_CTLR_PORTS; ctlr_port_num ++) {
 508     if (ctlr_type == CTLR_T_MSP) {
 509       if (cables->msp_to_iom[ctlr_unit_idx][ctlr_port_num].in_use) {
 510          uint iom_unit_idx = cables->msp_to_iom[ctlr_unit_idx][ctlr_port_num].iom_unit_idx;
 511          uint chan_num = cables->msp_to_iom[ctlr_unit_idx][ctlr_port_num].chan_num;
 512          uint dev_code = cables->dsk_to_ctlr[dsk_unit_idx].dev_code;
 513 
 514          send_special_interrupt (iom_unit_idx, chan_num, dev_code, 0x40, 01 /* disk pack ready */);
 515          return SCPE_OK;
 516        }
 517      } else {
 518        if (cables->ipc_to_iom[ctlr_unit_idx][ctlr_port_num].in_use) {
 519          uint iom_unit_idx = cables->ipc_to_iom[ctlr_unit_idx][ctlr_port_num].iom_unit_idx;
 520          uint chan_num = cables->ipc_to_iom[ctlr_unit_idx][ctlr_port_num].chan_num;
 521          uint dev_code = cables->dsk_to_ctlr[dsk_unit_idx].dev_code;
 522 
 523          send_special_interrupt (iom_unit_idx, chan_num, dev_code, 0x40, 01 /* disk pack ready */);
 524          return SCPE_OK;
 525        }
 526      }
 527    }
 528    return SCPE_ARG;
 529 
 530 }
 531 
 532 static t_stat disk_set_ready (UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
 533                               UNUSED const char * cptr,
 534                               UNUSED void * desc)
 535   {
 536     long disk_unit_idx = DSK_UNIT_IDX (uptr);
 537     if (disk_unit_idx >= (long) dsk_dev.numunits)
 538       {
 539         sim_warn ("%s: error: Invalid unit number %ld\r\n", __func__, (long) disk_unit_idx);
 540         return SCPE_ARG;
 541       }
 542     return signal_disk_ready ((uint) disk_unit_idx);
 543   }
 544 
 545 t_stat unloadDisk (uint dsk_unit_idx) {
     /* [previous][next][first][last][top][bottom][index][help] */
 546   if (dsk_unit [dsk_unit_idx] . flags & UNIT_ATT) {
 547     t_stat stat = sim_disk_detach (& dsk_unit [dsk_unit_idx]);
 548     if (stat != SCPE_OK) {
 549       sim_warn ("%s: sim_disk_detach returned %ld\r\n", __func__, (long) stat);
 550       return SCPE_ARG;
 551     }
 552   }
 553   return signal_disk_ready ((uint) dsk_unit_idx);
 554 }
 555 
 556 t_stat loadDisk (uint dsk_unit_idx, const char * disk_filename, bool ro) {
     /* [previous][next][first][last][top][bottom][index][help] */
 557   if (ro)
 558     dsk_unit[dsk_unit_idx].flags |= MTUF_WRP;
 559   else
 560     dsk_unit[dsk_unit_idx].flags &= ~ MTUF_WRP;
 561   t_stat stat = attach_unit (& dsk_unit [dsk_unit_idx], disk_filename);
 562   if (stat != SCPE_OK) {
 563     sim_printf ("%s: sim_disk_attach returned %d\r\n", __func__, stat);
 564     return stat;
 565   }
 566   return signal_disk_ready ((uint) dsk_unit_idx);
 567 }
 568 
 569 #define UNIT_WATCH UNIT_V_UF
 570 
 571 static MTAB disk_mod[] =
 572   {
 573 #if !defined(SPEED)
 574     { UNIT_WATCH, 1, "WATCH",   "WATCH",   0, 0, NULL, NULL },
 575     { UNIT_WATCH, 0, "NOWATCH", "NOWATCH", 0, 0, NULL, NULL },
 576 #endif /* if !defined(SPEED) */
 577     {
 578       MTAB_dev_value,                                   /* Mask               */
 579       0,                                                /* Match              */
 580       "NUNITS",                                         /* Print string       */
 581       "NUNITS",                                         /* Match string       */
 582       disk_set_nunits,                                  /* Validation routine */
 583       disk_show_nunits,                                 /* Display routine    */
 584       "Number of DISK units in the system",             /* Value descriptor   */
 585       NULL                                              /* Help               */
 586     },
 587     {
 588       MTAB_unit_value_show,                             /* Mask               */
 589       0,                                                /* Match              */
 590       "TYPE",                                           /* Print string       */
 591       "TYPE",                                           /* Match string       */
 592       disk_set_type,                                    /* Validation routine */
 593       disk_show_type,                                   /* Display routine    */
 594       "disk type",                                      /* Value descriptor   */
 595       "D500, D451, D400, D190, D181, D501, 3380, 3381", /* Help               */
 596     },
 597     {
 598       MTAB_XTD | MTAB_VUN | MTAB_VALR | MTAB_NC,        /* Mask               */
 599       0,                                                /* Match              */
 600       "NAME",                                           /* Print string       */
 601       "NAME",                                           /* Match string       */
 602       dsk_set_device_name,                              /* Validation routine */
 603       dsk_show_device_name,                             /* Display routine    */
 604       "Set the device name",                            /* Value descriptor   */
 605       NULL                                              /* Help               */
 606     },
 607     {
 608       MTAB_XTD | MTAB_VUN | MTAB_NMO | MTAB_VALR,       /* Mask               */
 609       0,                                                /* Match              */
 610       "READY",                                          /* Print string       */
 611       "READY",                                          /* Match string       */
 612       disk_set_ready,                                   /* Validation routine */
 613       NULL,                                             /* Display routine    */
 614       NULL,                                             /* Value descriptor   */
 615       NULL                                              /* Help string        */
 616     },
 617     MTAB_eol
 618   };
 619 
 620 static t_stat disk_reset (UNUSED DEVICE * dptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 621   {
 622     return SCPE_OK;
 623   }
 624 
 625 static t_stat disk_attach (UNIT *uptr, CONST char *cptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 626   {
 627     int diskUnitIdx = (int) DSK_UNIT_IDX (uptr);
 628     if (diskUnitIdx < 0 || diskUnitIdx >= N_DSK_UNITS_MAX)
 629       {
 630         sim_printf ("error: Invalid unit number %ld\r\n", (long) diskUnitIdx);
 631         return SCPE_ARG;
 632       }
 633 
 634     return loadDisk ((uint) diskUnitIdx, cptr, false);
 635   }
 636 
 637 // No disks known to Multics had more than 2^24 sectors...
 638 DEVICE dsk_dev = {
 639     "DISK",                /* Name                */
 640     dsk_unit,              /* Units               */
 641     NULL,                  /* Registers           */
 642     disk_mod,              /* Modifiers           */
 643     N_DISK_UNITS,          /* #Units              */
 644     10,                    /* Address radix       */
 645     24,                    /* Address width       */
 646     1,                     /* Address increment   */
 647     8,                     /* Data radix          */
 648     36,                    /* Data width          */
 649     NULL,                  /* Examine             */
 650     NULL,                  /* Deposit             */
 651     disk_reset,            /* Reset               */
 652     NULL,                  /* Boot                */
 653     disk_attach,           /* Attach              */
 654     NULL /*disk_detach*/,  /* Detach              */
 655     NULL,                  /* Context             */
 656     DEV_DEBUG,             /* Flags               */
 657     0,                     /* Debug control flags */
 658     disk_dt,               /* Debug flag names    */
 659     NULL,                  /* Memory size change  */
 660     NULL,                  /* Logical name        */
 661     NULL,                  /* Help                */
 662     NULL,                  /* Attach help         */
 663     NULL,                  /* Attach context      */
 664     NULL,                  /* Description         */
 665     NULL                   /* End                 */
 666 };
 667 
 668 /*
 669  * disk_init()
 670  */
 671 
 672 // Once-only initialization
 673 
 674 void disk_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 675   {
 676     // Sets diskTypeIdx to 0: 3381
 677     (void)memset (dsk_states, 0, sizeof (dsk_states));
 678     // Zero'd typeIdx defaults each unit to diskTypes[0] (3381)
 679     for (uint i = 0; i < N_DSK_UNITS_MAX; i ++)
 680         dsk_states [i].tAndDCapac = tAndDCapac [dsk_states [i].typeIdx];
 681 #if defined(LOCKLESS)
 682 # if defined(__FreeBSD__)
 683         pthread_mutexattr_t scu_attr;
 684         pthread_mutexattr_init (& scu_attr);
 685         pthread_mutexattr_settype (& scu_attr, PTHREAD_MUTEX_ADAPTIVE_NP);
 686 # endif /* if defined(__FreeBSD__) */
 687     for (uint i = 0; i < N_DSK_UNITS_MAX; i ++)
 688       {
 689 # if defined(__FreeBSD__)
 690         pthread_mutex_init (& dsk_states[i].dsk_lock, & scu_attr);
 691 # else
 692         pthread_mutex_init (& dsk_states[i].dsk_lock, NULL);
 693 # endif /* if defined(__FreeBSD__) */
 694       }
 695 #endif /* if defined(LOCKLESS) */
 696   }
 697 
 698 static iom_cmd_rc_t diskSeek64 (uint devUnitIdx, uint iomUnitIdx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
 699   {
 700     iom_chan_data_t * p            = & iom_chan_data[iomUnitIdx][chan];
 701     struct dsk_state * disk_statep = & dsk_states[devUnitIdx];
 702 
 703     uint typeIdx                     = disk_statep->typeIdx;
 704     if (diskTypes[typeIdx].seekSize != seek_64)
 705       sim_warn ("%s: disk%u sent a SEEK_64 but is 512 sized\r\n", __func__, typeIdx);
 706 
 707     uint tally = p->DDCW_TALLY;
 708 
 709     // Seek specific processing
 710 
 711     if (tally != 1)
 712       {
 713         sim_printf ("disk seek dazed by tally %d != 1\r\n", tally);
 714         p->stati = 04510; // Cmd reject, invalid inst. seq.
 715         return IOM_CMD_ERROR;
 716       }
 717 
 718     word36 seekData[1];
 719     uint count;
 720     iom_indirect_data_service (iomUnitIdx, chan, seekData, &count, false);
 721     // POLTS claims that seek data doesn't count as an I/O xfer
 722     p->initiate = true;
 723     if (count != 1)
 724       sim_warn ("%s: count %d not 1\r\n", __func__, count);
 725 
 726 #if defined(POLTS_DISK_TESTING)
 727     if_sim_debug (DBG_TRACE, & dsk_dev) { sim_printf ("// Seek address %012"PRIo64"\r\n", seekData[0]); }
 728 #endif
 729 
 730 // disk_control.pl1:
 731 //   quentry.sector = bit (sector, 21);  /* Save the disk device address. */
 732 // suggests seeks are 21 bits. The largest sector number is D501 1077759; 4040777 in base 8;
 733 // 21 bits.
 734 
 735     seekData[0] &= MASK21;
 736     if (seekData[0] >= diskTypes[typeIdx].capac)
 737       {
 738         disk_statep->seekValid = false;
 739         p->stati               = 04304; // Invalid seek address
 740         return IOM_CMD_ERROR;
 741       }
 742     disk_statep->seekValid    = true;
 743     disk_statep->seekPosition = (uint) seekData[0];
 744     p->stati = 04000; // Channel ready
 745     return IOM_CMD_PROCEED;
 746   }
 747 
 748 static int diskSeek512 (uint devUnitIdx, uint iomUnitIdx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
 749   {
 750     iom_chan_data_t * p            = & iom_chan_data[iomUnitIdx][chan];
 751     struct dsk_state * disk_statep = & dsk_states[devUnitIdx];
 752     uint typeIdx                   = disk_statep->typeIdx;
 753     if (diskTypes[typeIdx].seekSize != seek_512)
 754       sim_warn ("%s: disk%u sent a SEEK_512 but is 64 sized\r\n", __func__, typeIdx);
 755 
 756     uint tally = p->DDCW_TALLY;
 757 
 758     // Seek specific processing
 759 
 760     if (tally != 1)
 761       {
 762         sim_printf ("disk seek dazed by tally %d != 1\r\n", tally);
 763         //p->stati = 04510; // Cmd reject, invalid inst. seq.
 764         //p->chanStatus = chanStatIncorrectDCW;
 765         //return -1;
 766         tally = 1;
 767       }
 768 
 769     word36 seekData[1];
 770     uint count;
 771     iom_indirect_data_service (iomUnitIdx, chan, seekData, &count, false);
 772     // POLTS claims that seek data doesn't count as an I/O xfer
 773     p->initiate = true;
 774     if (count != 1)
 775       sim_warn ("%s: count %d not 1\r\n", __func__, count);
 776 
 777     seekData[0] &= MASK21;
 778     if (seekData[0] >= diskTypes[typeIdx].capac)
 779       {
 780         disk_statep->seekValid = false;
 781         p->stati = 04304; // Invalid seek address
 782         return -1;
 783       }
 784     disk_statep->seekValid    = true;
 785     disk_statep->seekPosition = (uint)seekData[0];
 786     p->stati                  = 04000; // Channel ready
 787     return 0;
 788   }
 789 
 790 static iom_cmd_rc_t diskSeekSpecial (uint devUnitIdx, uint iomUnitIdx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
 791   {
 792     iom_chan_data_t * p            = & iom_chan_data[iomUnitIdx][chan];
 793     struct dsk_state * disk_statep = & dsk_states[devUnitIdx];
 794 
 795     //if (diskTypes[disk_statep->typeIdx].seekSize != seek_64)
 796       //sim_warn ("%s: disk%u sent a SEEK_64 but is 512 sized\r\n", __func__, disk_statep->typeIdx);
 797 
 798     uint tally = p->DDCW_TALLY;
 799 
 800     // Seek specific processing
 801 
 802     if (tally != 1)
 803       {
 804         sim_printf ("disk seek dazed by tally %d != 1\r\n", tally);
 805         //p->stati = 04510; // Cmd reject, invalid inst. seq.
 806         //p->chanStatus = chanStatIncorrectDCW;
 807         //return IOM_CMD_ERROR;
 808         tally = 1;
 809       }
 810 
 811     word36 seekData[1];
 812     uint count;
 813     iom_indirect_data_service (iomUnitIdx, chan, seekData, &count, false);
 814     // POLTS claims that seek data doesn't count as an I/O xfer
 815     p->initiate  = true;
 816     if (count   != 1)
 817       sim_warn ("%s: count %d not 1\r\n", __func__, count);
 818 
 819 #if defined(POLTS_DISK_TESTING)
 820     if_sim_debug (DBG_TRACE, & dsk_dev)
 821       {
 822         sim_printf ("// Seek address %012"PRIo64"\r\n", seekData[0]);
 823       }
 824 #endif
 825 
 826     seekData[0] &= MASK21;
 827     if (seekData[0] >= disk_statep->tAndDCapac)
 828       {
 829         p->stati               = 04304; // Invalid seek address
 830         disk_statep->seekValid = false;
 831         //p->chanStatus = chanStatIncomplete;
 832         return IOM_CMD_ERROR;
 833       }
 834     disk_statep->seekValid    = true;
 835     disk_statep->seekPosition = (uint) seekData[0];
 836     p->stati                  = 04000; // Channel ready
 837     return IOM_CMD_PROCEED;
 838   }
 839 
 840 static int diskRead (uint devUnitIdx, uint iomUnitIdx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
 841   {
 842     iom_chan_data_t * p            = & iom_chan_data[iomUnitIdx][chan];
 843     UNIT * unitp                   = & dsk_unit[devUnitIdx];
 844     struct dsk_state * disk_statep = & dsk_states[devUnitIdx];
 845     uint typeIdx                   = disk_statep->typeIdx;
 846     uint sectorSizeWords           = diskTypes[typeIdx].sectorSizeWords;
 847     uint sectorSizeBytes           = ((36 * sectorSizeWords) / 8);
 848 
 849     if (! unitp->fileref) {
 850       p->stati = 04240; // device offline
 851 #if defined(POLTS_TESTING)
 852 if (chan == 014)
 853     if_sim_debug (DBG_TRACE, & dsk_dev) sim_printf ("// diskRead device offline\r\n");
 854 #endif
 855       return -1;
 856     }
 857     if (! disk_statep->seekValid) {
 858       p->stati               = 04510; // Invalid instruction sequence
 859       disk_statep->seekValid = false;
 860 #if defined(POLTS_TESTING)
 861 if (chan == 014)
 862     if_sim_debug (DBG_TRACE, & dsk_dev) sim_printf ("// diskRead seek invalid\r\n");
 863 #endif
 864       return IOM_CMD_ERROR;
 865     }
 866     uint tally = p->DDCW_TALLY;
 867     if (tally == 0)
 868       {
 869         tally = 4096;
 870       }
 871 
 872     int rc = fseek (unitp->fileref,
 873                 (long) ((long)disk_statep->seekPosition * (long)sectorSizeBytes),
 874                 SEEK_SET);
 875     if (rc)
 876       {
 877         sim_warn ("%s: fseek (read) returned %d, errno %d\r\n", __func__, rc, errno);
 878         p->stati = 04202; // attn, seek incomplete
 879 #if defined(POLTS_TESTING)
 880 if (chan == 014)
 881     if_sim_debug (DBG_TRACE, & dsk_dev) sim_printf ("// diskRead seek incomplete\r\n");
 882 #endif
 883         return -1;
 884       }
 885 
 886     // Convert from word36 format to packed72 format
 887 
 888     // round tally up to sector boundary
 889 
 890     // this math assumes tally is even.
 891 
 892     uint tallySectors = (tally + sectorSizeWords - 1) /
 893                          sectorSizeWords;
 894     uint tallyWords   = tallySectors * sectorSizeWords;
 895     //uint tallyBytes = tallySectors * sectorSizeBytes;
 896     uint p72ByteCnt   = (tallyWords * 36) / 8;
 897     uint8 diskBuffer[p72ByteCnt];
 898     (void)memset (diskBuffer, 0, sizeof (diskBuffer));
 899 
 900     (void)fflush (unitp->fileref);
 901     rc = (int) fread (diskBuffer, sectorSizeBytes,
 902                 tallySectors,
 903                 unitp->fileref);
 904 
 905     if (rc == 0) // EOF or error
 906       {
 907         if (ferror (unitp->fileref))
 908           {
 909             p->stati = 04202; // attn, seek incomplete
 910             //p->chanStatus = chanStatIncorrectDCW;
 911 #if defined(POLTS_TESTING)
 912 if (chan == 014)
 913     if_sim_debug (DBG_TRACE, & dsk_dev) sim_printf ("// diskRead seek incomplete2\r\n");
 914 #endif
 915             return -1;
 916           }
 917         // We ignore short reads-- we assume that they are reads
 918         // past the write highwater mark, and return zero data,
 919         // just as if the disk had been formatted with zeros.
 920       }
 921     disk_statep->seekPosition += tallySectors;
 922 
 923     uint wordsProcessed = 0;
 924     word36 buffer[tally];
 925     for (uint i = 0; i < tally; i ++)
 926       {
 927         word36 w;
 928         extractWord36FromBuffer (diskBuffer, p72ByteCnt, & wordsProcessed,
 929                                  & w);
 930         buffer[i] = w;
 931       }
 932     iom_indirect_data_service (iomUnitIdx, chan, buffer,
 933                             & wordsProcessed, true);
 934     p->stati = 04000;
 935 #if defined(POLTS_TESTING)
 936 if (chan == 014)
 937     if_sim_debug (DBG_TRACE, & dsk_dev) sim_printf ("// diskRead ok\r\n");
 938 #endif
 939     return 0;
 940   }
 941 
 942 static int diskWrite (uint devUnitIdx, uint iomUnitIdx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
 943   {
 944     iom_chan_data_t * p            = & iom_chan_data[iomUnitIdx][chan];
 945     UNIT * unitp                   = & dsk_unit[devUnitIdx];
 946     struct dsk_state * disk_statep = & dsk_states[devUnitIdx];
 947     uint typeIdx                   = disk_statep->typeIdx;
 948     uint sectorSizeWords           = diskTypes[typeIdx].sectorSizeWords;
 949     uint sectorSizeBytes           = ((36 * sectorSizeWords) / 8);
 950 
 951     if (! unitp->fileref) {
 952       p->stati = 04240; // device offline
 953       return -1;
 954     }
 955 
 956     if (! disk_statep->seekValid) {
 957       p->stati               = 04510; // Invalid instruction sequence
 958       disk_statep->seekValid = false;
 959       return IOM_CMD_ERROR;
 960     }
 961 
 962     uint tally = p->DDCW_TALLY;
 963 
 964     if (tally == 0)
 965       {
 966         tally = 4096;
 967       }
 968 
 969     int rc = fseek (unitp->fileref,
 970                 (long) ((long)disk_statep->seekPosition * (long)sectorSizeBytes),
 971                 SEEK_SET);
 972     if (rc)
 973       {
 974         sim_printf ("fseek (read) returned %d, errno %d\r\n", rc, errno);
 975         p->stati = 04202; // attn, seek incomplete
 976         return -1;
 977       }
 978 
 979     // Convert from word36 format to packed72 format
 980 
 981     // round tally up to sector boundary
 982 
 983     // this math assumes tally is even.
 984 
 985     uint tallySectors = (tally + sectorSizeWords - 1) /
 986                          sectorSizeWords;
 987     uint tallyWords   = tallySectors * sectorSizeWords;
 988     uint p72ByteCnt   = (tallyWords * 36) / 8;
 989     uint8 diskBuffer[p72ByteCnt];
 990     (void)memset (diskBuffer, 0, sizeof (diskBuffer));
 991     uint wordsProcessed = 0;
 992 
 993     word36 buffer[tally];
 994     iom_indirect_data_service (iomUnitIdx, chan, buffer,
 995                             & wordsProcessed, false);
 996     wordsProcessed = 0;
 997     for (uint i = 0; i < tally; i ++)
 998       {
 999         insertWord36toBuffer (diskBuffer, p72ByteCnt, & wordsProcessed,
1000                               buffer[i]);
1001       }
1002 
1003     rc = (int) fwrite (diskBuffer, sectorSizeBytes,
1004                  tallySectors,
1005                  unitp->fileref);
1006     (void)fflush (unitp->fileref);
1007 
1008     if (rc != (int) tallySectors)
1009       {
1010         sim_printf ("fwrite returned %d, errno %d\r\n", rc, errno);
1011         p->stati      = 04202; // attn, seek incomplete
1012         p->chanStatus = chanStatIncorrectDCW;
1013         return -1;
1014       }
1015 
1016     disk_statep->seekPosition += tallySectors;
1017 
1018     p->stati = 04000;
1019     return 0;
1020   }
1021 
1022 static int readStatusRegister (uint devUnitIdx, uint iomUnitIdx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
1023   {
1024     iom_chan_data_t * p = & iom_chan_data[iomUnitIdx][chan];
1025     UNIT * unitp        = & dsk_unit[devUnitIdx];
1026 
1027     uint tally = p->DDCW_TALLY;
1028 
1029     // Multics reads detailed status after an I/O error with a tally of 6
1030     // (ioi_masked: dcw.tally = "0006"b3).
1031     if (tally != 2 && tally != 4 && tally != 6)
1032       {
1033         sim_warn ("%s: RSR expected tally of 2, 4 or 6, is %d\r\n",
1034                   __func__, tally);
1035       }
1036     if (tally == 0)
1037       {
1038         tally = 4096;
1039       }
1040 
1041 // XXX need status register data format
1042 // system_library_tools/source/bound_io_tools_.s.archive/analyze_detail_stat_.pl1  anal_fips_disk_().
1043 
1044 #if defined(TESTING)
1045     sim_warn ("Need status register data format\r\n");
1046 #endif
1047     word36 buffer[tally];
1048     (void)memset (buffer, 0, sizeof (buffer));
1049 #if defined(POLTS_DISK_TESTING)
1050 buffer[0] = nCmds;
1051 #endif
1052     uint wordsProcessed = tally;
1053     iom_indirect_data_service (iomUnitIdx, chan, buffer,
1054                             & wordsProcessed, true);
1055     p->charPos = 0;
1056     p->stati   = 04000;
1057     if (! unitp->fileref)
1058       p->stati = 04240; // device offline
1059     return 0;
1060   }
1061 
1062 static int diskRdCtrlReg (uint dev_unit_idx, uint iom_unit_idx, uint chan) {
     /* [previous][next][first][last][top][bottom][index][help] */
1063   iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
1064   UNIT * unitp        = & dsk_unit[dev_unit_idx];
1065   p->charPos          = 0;
1066   p->stati            = 04000;
1067   if (! unitp->fileref)
1068     p->stati = 04240; // device offline
1069   return 0;
1070 }
1071 
1072 static int read_configuration (uint dev_unit_idx, uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
1073   {
1074     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
1075     UNIT * unitp        = & dsk_unit[dev_unit_idx];
1076 
1077     uint tally = p->DDCW_TALLY;
1078 
1079 // poll_mpc.pl1
1080 //
1081 // dcl  1 dau_buf aligned based (workp),   /* The IOI buffer segment */
1082 //        2 cf_idcw bit (36),              /* Read Configuration (24o) */
1083 //        2 cf_dcw bit (36),               /* Addr=dau_buf.data(0), tally=65 */
1084 //        2 st_idcw bit (36),              /* Read/Clear Statistics (16o) */
1085 //        2 st_dcw bit (36),               /* Address=dau_buf.data(130), tally=315 */
1086 //        2 data (0:759) bit (18) unal;    /* Config & statistics area */
1087 
1088 // XXX need status register data format
1089 // system_library_tools/source/bound_io_tools_.s.archive/analyze_detail_stat_.pl1  anal_fips_disk_().
1090 
1091 // poll_mpc.pl1
1092 //
1093 // dcl  1 dau_buf aligned based (workp),   /* The IOI buffer segment */
1094 //        2 cf_idcw bit (36),              /* Read Configuration (24o) */
1095 //        2 cf_dcw bit (36),               /* Addr=dau_buf.data(0), tally=65 */
1096 //        2 st_idcw bit (36),              /* Read/Clear Statistics (16o) */
1097 //        2 st_dcw bit (36),               /* Address=dau_buf.data(130), tally=315 */
1098 //        2 data (0:759) bit (18) unal;    /* Config & statistics area */
1099 
1100 //  dau_buf.data:
1101 //    0                                35
1102 //   +----------------+------------------+
1103 //   |    data(0)     |   data(1)        |
1104 //   +----------------+------------------+
1105 //   |    data(2)     |   data(3)        |
1106 //   +----------------+------------------+
1107 
1108 // dcl  dau_data (0:759) bit (16) unal;                        /* DAU config and stats */
1109 
1110 //
1111 //          do i = 0 to 759;
1112 //            substr (dau_data (i), 1, 8) = substr (dau_buf.data (i), 2, 8);
1113 //            substr (dau_data (i), 9, 8) = substr (dau_buf.data (i), 11, 8);
1114 //          end;
1115 //
1116 //   substr (dau_buf.data (i), 2, 8)
1117 //
1118 //    0 1 2 3 4 5 6 7 8     ....       17
1119 //   +-----------------------------------+
1120 //   | |X|X|X|X|X|X|X|X| |Y|Y|Y|Y|Y|Y|Y|Y|
1121 //   +-----------------------------------+
1122 //
1123 //   substr (dau_data (i), 1, 8)
1124 //
1125 //    0 1 2 3 4 5 6 7 8   ....     15
1126 //   +-------------------------------+
1127 //   |X|X|X|X|X|X|X|X|Y|Y|Y|Y|Y|Y|Y|Y|
1128 //   +-------------------------------+
1129 
1130 // dcl  1 dau_char based (image_ptr) unaligned,   /* Config data */
1131 //       2 type bit (8),                          /* = 12 HEX */
1132 //       2 hw_rev bit (8) unal,                   /* DAU rev */
1133 //       2 fw_maj_rev bit (8) unal,               /* firmware rev letter */
1134 //       2 fw_sub_rev bit (8) unal;               /* firmware rev number */
1135 //       2 dev (64),                              /* seq'ed by dev# */
1136 //                       /* all 4 bytes zero, if device NEVER configured */
1137 //         3 type fixed bin (8) uns unal,         /* device type */
1138 //         3 number fixed bin (8) uns unal,       /* device number, =FF if not configured */
1139 //         3 summary_status bit (8) unal,         /* device SS reg */
1140 //         3 port_number fixed bin (8) uns unal;  /* device DAU port */
1141 
1142 // We know that we are an MSP and not an IPC as this command is only issued
1143 // to MSPs.
1144 
1145     uint ctlr_unit_idx = get_ctlr_idx (iom_unit_idx, chan);
1146     struct ctlr_to_dev_s * dev_p;
1147     if (cables->iom_to_ctlr[iom_unit_idx][chan].ctlr_type == CTLR_T_IPC)
1148       dev_p = cables->ipc_to_dsk[ctlr_unit_idx];
1149     else
1150       dev_p = cables->msp_to_dsk[ctlr_unit_idx];
1151 
1152 // XXX Temp
1153     word36 buffer[tally];
1154     (void)memset (buffer, 0, sizeof (buffer));
1155     putbits36_9 (& buffer[0],  0, 0x12);
1156     putbits36_9 (& buffer[0],  9, 1);    // h/w revision
1157     putbits36_9 (& buffer[0], 18, '1');  // fw maj revision
1158     putbits36_9 (& buffer[0], 27, 'a');  // fw sub revision
1159 
1160     for (word9 dev_num = 0; dev_num < N_DEV_CODES; dev_num ++)
1161       {
1162          if (! dev_p[dev_num].in_use)
1163            continue;
1164          uint dsk_unit_idx = dev_p[dev_num].unit_idx;
1165          // word9 dau_type    = (word9) diskTypes[dsk_unit_idx].dau_type;
1166          // ubsan/asan
1167          word9 dau_type    = (word9) diskTypes[dsk_states[dsk_unit_idx].typeIdx].dau_type;
1168          putbits36_9 (& buffer[1+dev_num],  0, dau_type); // dev.type
1169          putbits36_9 (& buffer[1+dev_num],  9, dev_num);  // dev.number
1170          putbits36_9 (& buffer[1+dev_num], 18, 0);        // dev.summary_status // XXX
1171          putbits36_9 (& buffer[1+dev_num], 27, dev_num);  // dev.port_number
1172      }
1173 
1174     uint wordsProcessed = tally;
1175     iom_indirect_data_service (iom_unit_idx, chan, buffer,
1176                             & wordsProcessed, true);
1177     p->charPos = 0;
1178     p->stati   = 04000;
1179     if (! unitp->fileref)
1180       p->stati = 04240; // device offline
1181     return 0;
1182   }
1183 
1184 static int read_and_clear_statistics (uint dev_unit_idx, uint iom_unit_idx, uint chan)
     /* [previous][next][first][last][top][bottom][index][help] */
1185   {
1186 #if defined(TESTING)
1187     cpu_state_t * cpup  = _cpup;
1188 #endif
1189     iom_chan_data_t * p = & iom_chan_data[iom_unit_idx][chan];
1190     UNIT * unitp        = & dsk_unit[dev_unit_idx];
1191 
1192     sim_debug (DBG_NOTIFY, & dsk_dev, "Read %d\r\n", dev_unit_idx);
1193 
1194     p->charPos = 0;
1195     p->stati   = 04000;
1196     if (! unitp->fileref)
1197       p->stati = 04240; // device offline
1198     return 0;
1199   }
1200 
1201 // source/library_dir_dir/system_library_1/source/bound_rcp_.s.archive/rcp_disk_.pl1
1202 //
1203 //  dcl set_standby_command    bit (6) internal static init ("72"b3);
1204 //  dcl request_status_command bit (6) internal static init ("00"b3);
1205 //  dcl read_command           bit (6) internal static init ("25"b3);
1206 //  dcl reset_status_command   bit (6) internal static init ("40"b3);
1207 
1208 iom_cmd_rc_t dsk_iom_cmd (uint iomUnitIdx, uint chan) {
     /* [previous][next][first][last][top][bottom][index][help] */
1209 #if defined(POLTS_DISK_TESTING)
1210 nCmds ++;
1211 #endif
1212 #if defined(TESTING)
1213   cpu_state_t * cpup  = _cpup;
1214 #endif
1215   iom_chan_data_t * p = & iom_chan_data[iomUnitIdx][chan];
1216   uint ctlr_unit_idx  = get_ctlr_idx (iomUnitIdx, chan);
1217   uint devUnitIdx;
1218 
1219 #if defined(POLTS_DISK_TESTING)
1220 if (chan == 014)   {if_sim_debug (DBG_TRACE, & dsk_dev) { dumpDCW (p->DCW, 0); }}
1221 #endif
1222   if (cables->iom_to_ctlr[iomUnitIdx][chan].ctlr_type == CTLR_T_IPC)
1223     devUnitIdx = cables->ipc_to_dsk[ctlr_unit_idx][p->IDCW_DEV_CODE].unit_idx;
1224   else if (cables->iom_to_ctlr[iomUnitIdx][chan].ctlr_type == CTLR_T_MSP)
1225     devUnitIdx = cables->msp_to_dsk[ctlr_unit_idx][p->IDCW_DEV_CODE].unit_idx;
1226   else {
1227     sim_warn ("%s: Can't find controller (%d)\r\n", __func__, cables->iom_to_ctlr[iomUnitIdx][chan].ctlr_type);
1228     return IOM_CMD_ERROR;
1229   }
1230 
1231   UNIT * unitp = & dsk_unit[devUnitIdx];
1232   struct dsk_state * disk_statep = & dsk_states[devUnitIdx];
1233 
1234   iom_cmd_rc_t rc = IOM_CMD_PROCEED;
1235 
1236 #if defined(LOCKLESS)
1237   lock_ptr (& dsk_states->dsk_lock); //-V619
1238 #endif
1239 
1240   // IDCW?
1241   if (p->DCW_18_20_CP == 7) {
1242     // IDCW
1243     disk_statep->io_mode = disk_no_mode;
1244     switch (p->IDCW_DEV_CMD) {
1245       case 000: // CMD 00 Request status
1246 #if defined(POLTS_DISK_TESTING)
1247 //if (chan == 014)
1248 #endif /* if defined(POLTS_DISK_TESTING) */
1249 #if !defined(POLTS_DISK_TESTING)
1250         if_sim_debug (DBG_TRACE, & dsk_dev) {
1251           sim_printf ("// Disk Request Status\r\n");
1252         }
1253 #endif /* if !defined(POLTS_DISK_TESTING) */
1254         //disk_statep->io_mode = disk_no_mode;
1255         p->stati = 04000;
1256         if (! unitp->fileref)
1257           p->stati = 04240; // device offline
1258         break;
1259 
1260       case 016: // CMD 16 Read and Clear Statistics -- Model 800
1261 #if defined(POLTS_DISK_TESTING)
1262 if (chan == 014)
1263 #endif /* if defined(POLTS_DISK_TESTING) */
1264         if_sim_debug (DBG_TRACE, & dsk_dev) {
1265           sim_printf ("// Disk Read And Clear Statistics\r\n");
1266         }
1267         disk_statep->io_mode = disk_rd_clr_stats;
1268         p->recordResidue --;
1269         p->stati             = 04000;
1270         if (! unitp->fileref)
1271           p->stati = 04240; // device offline
1272         break;
1273 
1274       case 022: // CMD 22 Read Status Register
1275 #if defined(POLTS_DISK_TESTING)
1276 if (chan == 014)
1277 #endif
1278         if_sim_debug (DBG_TRACE, & dsk_dev) {
1279           sim_printf ("// Disk Read Status Register\r\n");
1280         }
1281         disk_statep->io_mode = disk_rd_status_reg;
1282         p->recordResidue --;
1283         p->stati             = 04000;
1284         if (! unitp->fileref)
1285           p->stati = 04240; // device offline
1286         break;
1287 
1288       case 024: // CMD 24 Read configuration -- Model 800
1289 #if defined(POLTS_DISK_TESTING)
1290 if (chan == 014)
1291 #endif
1292         if_sim_debug (DBG_TRACE, & dsk_dev) {
1293           sim_printf ("// Disk Read Configuration\r\n");
1294         }
1295         disk_statep->io_mode = disk_rd_config;
1296         p->recordResidue --;
1297         p->stati             = 04000;
1298         if (! unitp->fileref)
1299           p->stati = 04240; // device offline
1300         break;
1301 
1302       case 025: // CMD 25 READ
1303 #if defined(POLTS_DISK_TESTING)
1304 if (chan == 014)
1305 #endif
1306         if_sim_debug (DBG_TRACE, & dsk_dev) {
1307           sim_printf ("// Disk Read\r\n");
1308         }
1309         disk_statep->io_mode = disk_rd;
1310         p->recordResidue --;
1311         p->stati             = 04000;
1312         if (! unitp->fileref)
1313           p->stati = 04240; // device offline
1314         break;
1315 
1316       case 026: // CMD 26 READ CONTROL REGISTER
1317 #if defined(POLTS_DISK_TESTING)
1318 if (chan == 014)
1319 #endif
1320         if_sim_debug (DBG_TRACE, & dsk_dev) {
1321           sim_printf ("// Disk Read Control Register\r\n");
1322         }
1323         disk_statep->io_mode = disk_rd_ctrl_reg;
1324         p->recordResidue --;
1325         p->stati             = 04000;
1326         if (! unitp->fileref)
1327           p->stati = 04240; // device offline
1328         break;
1329 
1330       case 030: // CMD 30 SEEK_512
1331 #if defined(POLTS_DISK_TESTING)
1332 if (chan == 014)
1333 #endif
1334         if_sim_debug (DBG_TRACE, & dsk_dev) {
1335           sim_printf ("// Disk Seek 512\r\n");
1336         }
1337         disk_statep->io_mode = disk_seek_512;
1338         p->recordResidue --;
1339         p->stati             = 04000;
1340         if (! unitp->fileref)
1341           p->stati = 04240; // device offline
1342         break;
1343 
1344       case 031: // CMD 31 WRITE
1345       case 033: // CMD 31 WRITE AND COMPARE
1346 #if defined(POLTS_DISK_TESTING)
1347 if (chan == 014)
1348 #endif
1349         if_sim_debug (DBG_TRACE, & dsk_dev) {
1350           sim_printf ("// Disk Write\r\n");
1351         }
1352         disk_statep->io_mode = disk_wr;
1353         p->recordResidue --;
1354         p->stati             = 04000;
1355         if (! unitp->fileref)
1356           p->stati = 04240; // device offline
1357         break;
1358 
1359       case 034: // CMD 34 SEEK_64
1360 #if defined(POLTS_DISK_TESTING)
1361 if (chan == 014)
1362 #endif
1363         if_sim_debug (DBG_TRACE, & dsk_dev) {
1364           sim_printf ("// Disk Seek 64\r\n");
1365         }
1366         disk_statep->io_mode = disk_seek_64;
1367         p->recordResidue --;
1368         p->stati             = 04000;
1369         if (! unitp->fileref)
1370           p->stati = 04240; // device offline
1371         break;
1372 
1373       case 036: // CMD 36 SPECIAL SEEK (T&D) // Make it work like SEEK_64 and
1374                                              // hope for the best
1375 #if defined(POLTS_DISK_TESTING)
1376 if (chan == 014)
1377 #endif
1378         if_sim_debug (DBG_TRACE, & dsk_dev) {
1379           sim_printf ("// Disk Special Seek\r\n");
1380         }
1381         disk_statep->io_mode = disk_special_seek;
1382         p->recordResidue --;
1383         p->stati             = 04000;
1384         if (! unitp->fileref)
1385           p->stati = 04240; // device offline
1386         break;
1387 
1388       case 040: // CMD 40 Reset status
1389 #if defined(POLTS_DISK_TESTING)
1390 if (chan == 014)
1391 #endif
1392         if_sim_debug (DBG_TRACE, & dsk_dev) {
1393           sim_printf ("// Disk Reset Status\r\n");
1394         }
1395         p->stati    = 04000;
1396         // XXX POLTS wants this; I don't understand why.
1397         p->recordResidue --;
1398         p->initiate = false; // According to POLTS
1399         p->isRead   = false;
1400         // T&D probing
1401         if (p->IDCW_DEV_CODE == 077) {
1402           p->stati = 04502; // invalid device code
1403 #if defined(LOCKLESS)
1404           unlock_ptr (& dsk_states->dsk_lock); //-V619
1405 #endif
1406           return IOM_CMD_DISCONNECT;
1407         }
1408         if (! unitp->fileref)
1409           p->stati = 04240; // device offline
1410         break;
1411 
1412       case 042: // CMD 42 RESTORE
1413 #if defined(POLTS_DISK_TESTING)
1414 if (chan == 014)
1415 #endif
1416         if_sim_debug (DBG_TRACE, & dsk_dev) {
1417           sim_printf ("// Disk Restore\r\n");
1418         }
1419         p->stati    = 04000;
1420         // XXX POLTS wants this; I don't understand why.
1421         p->recordResidue --;
1422         p->initiate = false; // According to POLTS
1423         if (! unitp->fileref)
1424           p->stati = 04240; // device offline
1425 
1426         disk_statep->seekValid = false;
1427 
1428         break;
1429 
1430       case 072: // CMD 72 SET STANDBY
1431 #if defined(POLTS_DISK_TESTING)
1432 if (chan == 014)
1433 #endif
1434         if_sim_debug (DBG_TRACE, & dsk_dev) {
1435           sim_printf ("// Disk Set Standby\r\n");
1436         }
1437         p->stati = 04000;
1438         if (! unitp->fileref)
1439           p->stati = 04240; // device offline
1440         break;
1441 
1442       default:
1443 #if defined(POLTS_DISK_TESTING)
1444 if (chan == 014)
1445 #endif
1446         if_sim_debug (DBG_TRACE, & dsk_dev) {
1447           sim_printf ("// Disk unknown command %o\r\n", p->IDCW_DEV_CMD);
1448         }
1449         p->stati      = 04501;
1450         p->chanStatus = chanStatIncorrectDCW;
1451         if (p->IDCW_DEV_CMD != 051) // ignore bootload console probe
1452           sim_warn ("%s: Unrecognized device command %02o\r\n", __func__, p->IDCW_DEV_CMD);
1453         rc =  IOM_CMD_ERROR;
1454         goto done;
1455     }
1456     goto done;
1457   } // IDCW
1458 
1459   // Not IDCW; TDCW are captured in IOM, so must be IOTD, IOTP or IOTNP
1460   switch (disk_statep->io_mode) {
1461     case disk_no_mode:
1462 #if defined(POLTS_DISK_TESTING)
1463 if (chan == 014)
1464 #endif
1465       if_sim_debug (DBG_TRACE, & dsk_dev) {
1466         sim_printf ("// Disk IOT No Mode\r\n");
1467       }
1468 // It appears that in some cases Multics builds a dcw list from a generic
1469 // "single IDCW plus optional DDCW" template. That template sets the IDCW
1470 // channel command to "record", regardless of whether or not the
1471 // instruction needs an DDCW. In particular, disk_ctl pings the disk by
1472 // sending a "Reset status" with "record" and an apparently uninitialized
1473 // IOTD. The payload channel will send the IOTD because of the "record";
1474 // the Reset Status command left IO mode at "no mode" so we don't know what
1475 // to do with it. Since this appears benign, we will assume that the
1476 // original H/W ignored, and so shall we.
1477 
1478       //sim_warn ("%s: Unexpected IOTx\r\n", __func__);
1479       //rc = IOM_CMD_ERROR;
1480       goto done;
1481 
1482     case disk_rd_clr_stats: {
1483 #if defined(POLTS_DISK_TESTING)
1484 if (chan == 014)
1485 #endif
1486         if_sim_debug (DBG_TRACE, & dsk_dev) {
1487           sim_printf ("// Disk IOT Rd And Clr Stats\r\n");
1488         }
1489         int rc1 = read_and_clear_statistics (devUnitIdx, iomUnitIdx, chan);
1490         if (rc1) {
1491           rc = IOM_CMD_ERROR;
1492           goto done;
1493         }
1494       }
1495       break;
1496 
1497     case disk_rd_status_reg: {
1498 #if defined(POLTS_DISK_TESTING)
1499 if (chan == 014)
1500 #endif
1501         if_sim_debug (DBG_TRACE, & dsk_dev) {
1502           sim_printf ("// Disk IOT Rd Status Reg\r\n");
1503         }
1504         int rc1 = readStatusRegister (devUnitIdx, iomUnitIdx, chan);
1505         if (rc1) {
1506           rc = IOM_CMD_ERROR;
1507           goto done;
1508         }
1509       }
1510       break;
1511 
1512     case disk_rd_config: {
1513 #if defined(POLTS_DISK_TESTING)
1514 if (chan == 014)
1515 #endif
1516         if_sim_debug (DBG_TRACE, & dsk_dev) {
1517           sim_printf ("// Disk IOT Rd Config\r\n");
1518         }
1519         // XXX missing. see poll_mpc.pl1, poll_mpc_data.incl.pl1
1520         int rc1 = read_configuration (devUnitIdx, iomUnitIdx, chan);
1521         if (rc1) {
1522           rc = IOM_CMD_ERROR;
1523           goto done;
1524         }
1525       }
1526       break;
1527 
1528     case disk_rd: {
1529 #if defined(POLTS_DISK_TESTING)
1530 if (chan == 014)
1531 #endif
1532         if_sim_debug (DBG_TRACE, & dsk_dev) {
1533           sim_printf ("// Disk IOT Read\r\n");
1534         }
1535         int rc1 = diskRead (devUnitIdx, iomUnitIdx, chan);
1536         if (rc1) {
1537           rc = IOM_CMD_ERROR;
1538           goto done;
1539         }
1540       }
1541       break;
1542 
1543     case disk_rd_ctrl_reg: {
1544 #if defined(POLTS_DISK_TESTING)
1545 if (chan == 014)
1546 #endif
1547         if_sim_debug (DBG_TRACE, & dsk_dev) {
1548           sim_printf ("// Disk IOT Read Control Register\r\n");
1549         }
1550         int rc1 = diskRdCtrlReg (devUnitIdx, iomUnitIdx, chan);
1551         if (rc1) {
1552           rc = IOM_CMD_ERROR;
1553           goto done;
1554         }
1555       }
1556       break;
1557 
1558     case disk_seek_512: {
1559 #if defined(POLTS_DISK_TESTING)
1560 if (chan == 014)
1561 #endif
1562         if_sim_debug (DBG_TRACE, & dsk_dev) {
1563           sim_printf ("// Disk IOT Seek 512\r\n");
1564         }
1565         int rc1 = diskSeek512 (devUnitIdx, iomUnitIdx, chan);
1566         if (rc1) {
1567           rc = IOM_CMD_ERROR;
1568           goto done;
1569         }
1570       }
1571       break;
1572 
1573     case disk_wr: {
1574 #if defined(POLTS_DISK_TESTING)
1575 if (chan == 014)
1576 #endif
1577         if_sim_debug (DBG_TRACE, & dsk_dev) {
1578           sim_printf ("// Disk IOT Write\r\n");
1579         }
1580         int rc1 = diskWrite (devUnitIdx, iomUnitIdx, chan);
1581         if (rc1) {
1582           rc = IOM_CMD_ERROR;
1583           goto done;
1584         }
1585       }
1586       break;
1587 
1588     case disk_seek_64: {
1589 #if defined(POLTS_DISK_TESTING)
1590 if (chan == 014)
1591 #endif
1592         if_sim_debug (DBG_TRACE, & dsk_dev) {
1593           sim_printf ("// Disk IOT Seek 64\r\n");
1594         }
1595         int rc1 = diskSeek64 (devUnitIdx, iomUnitIdx, chan);
1596         if (rc1) {
1597           rc = IOM_CMD_ERROR;
1598           goto done;
1599         }
1600       }
1601       break;
1602 
1603     case disk_special_seek: { // Make it work like SEEK_64 and hope for the best
1604 #if defined(POLTS_DISK_TESTING)
1605 if (chan == 014)
1606 #endif
1607         if_sim_debug (DBG_TRACE, & dsk_dev) {
1608           sim_printf ("// Disk IOT special seek\r\n");
1609         }
1610         iom_cmd_rc_t rc1 = diskSeekSpecial (devUnitIdx, iomUnitIdx, chan);
1611         if (rc1) {
1612           rc = IOM_CMD_ERROR;
1613           goto done;
1614         }
1615       }
1616       break;
1617   }
1618 
1619 done:
1620 #if defined(LOCKLESS)
1621   unlock_ptr (& dsk_states->dsk_lock); //-V619
1622 #endif
1623   return rc;
1624 }
1625 
1626 //////////
1627 //////////
1628 //////////
1629 ///
1630 /// IPC
1631 ///
1632 
1633 #define IPC_UNIT_IDX(uptr) ((uptr) - ipc_unit)
1634 #define N_IPC_UNITS 1 // default
1635 
1636 static struct ipc_state
1637   {
1638     char device_name[MAX_DEV_NAME_LEN];
1639   } ipc_states[N_IPC_UNITS_MAX];
1640 
1641 UNIT ipc_unit[N_IPC_UNITS_MAX] = {
1642 #if defined(NO_C_ELLIPSIS)
1643   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1644   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1645   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1646   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1647   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1648   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1649   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1650   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1651   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1652   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1653   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1654   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1655   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1656   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1657   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1658   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL }
1659 #else
1660   [0 ... N_IPC_UNITS_MAX-1] = {
1661     UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
1662   }
1663 #endif
1664 };
1665 
1666 static t_stat ipc_show_nunits (UNUSED FILE * st, UNUSED UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1667                                UNUSED int val, UNUSED const void * desc)
1668   {
1669     sim_printf("Number of IPC units in system is %d\r\n", ipc_dev.numunits);
1670     return SCPE_OK;
1671   }
1672 
1673 static t_stat ipc_set_nunits (UNUSED UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
1674                               const char * cptr, UNUSED void * desc)
1675   {
1676     if (! cptr)
1677       return SCPE_ARG;
1678     int n = atoi (cptr);
1679     if (n < 0 || n > N_DSK_UNITS_MAX)
1680       return SCPE_ARG;
1681     ipc_dev.numunits = (uint32) n;
1682     return SCPE_OK;
1683   }
1684 
1685 static t_stat ipc_show_device_name (UNUSED FILE * st, UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1686                                     UNUSED int val, UNUSED const void * desc)
1687   {
1688     int n = (int) IPC_UNIT_IDX (uptr);
1689     if (n < 0 || n >= N_IPC_UNITS_MAX)
1690       return SCPE_ARG;
1691     if (ipc_states[n].device_name[0] != 0)
1692       sim_printf("name     : %s", ipc_states[n].device_name);
1693     else
1694       sim_printf("name     : default");
1695     return SCPE_OK;
1696   }
1697 
1698 static t_stat ipc_set_device_name (UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
1699                                    const char * cptr, UNUSED void * desc)
1700   {
1701     int n = (int) IPC_UNIT_IDX (uptr);
1702     if (n < 0 || n >= N_IPC_UNITS_MAX)
1703       return SCPE_ARG;
1704     if (cptr)
1705       {
1706         strncpy (ipc_states[n].device_name, cptr, MAX_DEV_NAME_LEN-1);
1707         ipc_states[n].device_name[MAX_DEV_NAME_LEN-1] = 0;
1708       }
1709     else
1710       ipc_states[n].device_name[0] = 0;
1711     return SCPE_OK;
1712   }
1713 
1714 static MTAB ipc_mod[] =
1715   {
1716     {
1717       MTAB_dev_value,                            /* Mask               */
1718       0,                                         /* Match              */
1719       "NUNITS",                                  /* Print string       */
1720       "NUNITS",                                  /* Match string       */
1721       ipc_set_nunits,                            /* Validation routine */
1722       ipc_show_nunits,                           /* Display routine    */
1723       "Number of DISK units in the system",      /* Value descriptor   */
1724       NULL                                       /* Help               */
1725     },
1726     {
1727       MTAB_XTD | MTAB_VUN | MTAB_VALR | MTAB_NC, /* Mask               */
1728       0,                                         /* Match              */
1729       "NAME",                                    /* Print string       */
1730       "NAME",                                    /* Match string       */
1731       ipc_set_device_name,                       /* Validation routine */
1732       ipc_show_device_name,                      /* Display routine    */
1733       "Set the device name",                     /* Value descriptor   */
1734       NULL                                       /* Help               */
1735     },
1736     MTAB_eol
1737   };
1738 
1739 DEVICE ipc_dev =
1740    {
1741     "IPC",                 /* Name                */
1742     ipc_unit,              /* Units               */
1743     NULL,                  /* Registers           */
1744     ipc_mod,               /* Modifiers           */
1745     N_IPC_UNITS,           /* #Units              */
1746     10,                    /* Address radix       */
1747     24,                    /* Address width       */
1748     1,                     /* Address increment   */
1749     8,                     /* Data radix          */
1750     36,                    /* Data width          */
1751     NULL,                  /* Examine             */
1752     NULL,                  /* Deposit             */
1753     NULL,                  /* Reset               */
1754     NULL,                  /* Boot                */
1755     NULL,                  /* Attach              */
1756     NULL /*disk_detach*/,  /* Detach              */
1757     NULL,                  /* Context             */
1758     0,                     /* Flags               */
1759     0,                     /* Debug control flags */
1760     NULL,                  /* Debug flag names    */
1761     NULL,                  /* Memory size change  */
1762     NULL,                  /* Logical name        */
1763     NULL,                  /* Help                */
1764     NULL,                  /* Attach help         */
1765     NULL,                  /* Attach context      */
1766     NULL,                  /* Description         */
1767     NULL                   /* End                 */
1768   };
1769 
1770 //////////
1771 //////////
1772 //////////
1773 ///
1774 /// MSP
1775 ///
1776 
1777 #define MSP_UNIT_IDX(uptr) ((uptr) - msp_unit)
1778 #define N_MSP_UNITS 1 // default
1779 
1780 struct msp_state_s msp_states [N_MSP_UNITS_MAX];
1781 
1782 UNIT msp_unit[N_MSP_UNITS_MAX] =
1783   {
1784 #if defined(NO_C_ELLIPSIS)
1785   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1786   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1787   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1788   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1789   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1790   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1791   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1792   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1793   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1794   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1795   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1796   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1797   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1798   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1799   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
1800   { UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL }
1801 #else
1802   [0 ... N_MSP_UNITS_MAX-1] = {
1803     UDATA (NULL, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
1804   }
1805 #endif
1806 };
1807 
1808 static t_stat msp_show_nunits (UNUSED FILE * st, UNUSED UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1809                                UNUSED int val, UNUSED const void * desc)
1810   {
1811     sim_printf("Number of MSP units in system is %d\r\n", msp_dev.numunits);
1812     return SCPE_OK;
1813   }
1814 
1815 static t_stat msp_set_nunits (UNUSED UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
1816                               const char * cptr, UNUSED void * desc)
1817   {
1818     if (! cptr)
1819       return SCPE_ARG;
1820     int n = atoi (cptr);
1821     if (n < 0 || n > N_DSK_UNITS_MAX)
1822       return SCPE_ARG;
1823     msp_dev.numunits = (uint32) n;
1824     return SCPE_OK;
1825   }
1826 
1827 static t_stat msp_show_device_name (UNUSED FILE * st, UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1828                                     UNUSED int val, UNUSED const void * desc)
1829   {
1830     int n = (int) MSP_UNIT_IDX (uptr);
1831     if (n < 0 || n >= N_MSP_UNITS_MAX)
1832       return SCPE_ARG;
1833     if (msp_states[n].device_name[0] != 0)
1834       sim_printf("name     : %s", msp_states[n].device_name);
1835     else
1836       sim_printf("name     : default");
1837     return SCPE_OK;
1838   }
1839 
1840 static t_stat msp_set_device_name (UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
1841                                    const char * cptr, UNUSED void * desc)
1842   {
1843     int n = (int) MSP_UNIT_IDX (uptr);
1844     if (n < 0 || n >= N_MSP_UNITS_MAX)
1845       return SCPE_ARG;
1846     if (cptr)
1847       {
1848         strncpy (msp_states[n].device_name, cptr, MAX_DEV_NAME_LEN-1);
1849         msp_states[n].device_name[MAX_DEV_NAME_LEN-1] = 0;
1850       }
1851     else
1852       msp_states[n].device_name[0] = 0;
1853     return SCPE_OK;
1854   }
1855 
1856 static MTAB msp_mod[] =
1857   {
1858     {
1859       MTAB_dev_value,                        /* Mask               */
1860       0,                                     /* Match              */
1861       "NUNITS",                              /* Print string       */
1862       "NUNITS",                              /* Match string       */
1863       msp_set_nunits,                        /* Validation routine */
1864       msp_show_nunits,                       /* Display routine    */
1865       "Number of DISK units in the system",  /* Value descriptor   */
1866       NULL                                   /* Help               */
1867     },
1868     {
1869       MTAB_XTD | MTAB_VUN | \
1870       MTAB_VALR | MTAB_NC,                   /* Mask               */
1871       0,                                     /* Match              */
1872       "NAME",                                /* Print string       */
1873       "NAME",                                /* Match string       */
1874       msp_set_device_name,                   /* Validation routine */
1875       msp_show_device_name,                  /* Display routine    */
1876       "Set the device name",                 /* Value descriptor   */
1877       NULL                                   /* Help               */
1878     },
1879     MTAB_eol
1880   };
1881 
1882 DEVICE msp_dev =
1883    {
1884     "MSP",                 /* Name                */
1885     msp_unit,              /* Units               */
1886     NULL,                  /* Registers           */
1887     msp_mod,               /* Modifiers           */
1888     N_MSP_UNITS,           /* #Units              */
1889     10,                    /* Address radix       */
1890     24,                    /* Address width       */
1891     1,                     /* Address increment   */
1892     8,                     /* Data radix          */
1893     36,                    /* Data width          */
1894     NULL,                  /* Examine             */
1895     NULL,                  /* Deposit             */
1896     NULL,                  /* Reset               */
1897     NULL,                  /* Boot                */
1898     NULL,                  /* Attach              */
1899     NULL /*disk_detach*/,  /* Detach              */
1900     NULL,                  /* Context             */
1901     0,                     /* Flags               */
1902     0,                     /* Debug control flags */
1903     NULL,                  /* Debug flag names    */
1904     NULL,                  /* Memory size change  */
1905     NULL,                  /* Logical name        */
1906     NULL,                  /* Help                */
1907     NULL,                  /* Attach help         */
1908     NULL,                  /* Attach context      */
1909     NULL,                  /* Description         */
1910     NULL                   /* End                 */
1911   };

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