File: id_uvc.c

package info (click to toggle)
simh 3.8.1-6.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,672 kB
  • sloc: ansic: 209,820; makefile: 326
file content (385 lines) | stat: -rw-r--r-- 14,270 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* id_uvc.c: Interdata universal clock

   Copyright (c) 2001-2008, Robert M. Supnik

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

   Except as contained in this notice, the name of Robert M Supnik shall not be
   used in advertising or otherwise to promote the sale, use or other dealings
   in this Software without prior written authorization from Robert M Supnik.

   pic          precision incremental clock
   lfc          line frequency clock

   18-Jun-07    RMS     Added UNIT_IDLE flag
   18-Oct-06    RMS     Changed LFC to be free running, export tmr_poll
   23-Jul-05    RMS     Fixed {} error in OC
   01-Mar-03    RMS     Added SET/SHOW LFC FREQ support
                        Changed precision clock algorithm for V7 UNIX
*/

#include "id_defs.h"
#include <ctype.h>

/* Device definitions */

#define UNIT_V_DIAG     (UNIT_V_UF + 0)                 /* diag mode */
#define UNIT_DIAG       (1 << UNIT_V_DIAG)

#define STA_OVF         0x08                            /* PIC overflow */
#define CMD_STRT        0x20                            /* start */
#define PIC_V_RATE      12                              /* rate */
#define PIC_M_RATE      0xF
#define PIC_RATE        (PIC_M_RATE << PIC_V_RATE)
#define PIC_CTR         0x0FFF                          /* PIC counters */
#define GET_RATE(x)     (((x) >> PIC_V_RATE) & PIC_M_RATE)
#define GET_CTR(x)      ((x) & PIC_CTR)
#define PIC_TPS         1000

extern uint32 int_req[INTSZ], int_enb[INTSZ];

int32 pic_db = 0;                                       /* output buf */
int32 pic_ric = 0;                                      /* reset count */
int32 pic_cic = 0;                                      /* current count */
uint32 pic_save = 0;                                    /* saved time */
uint32 pic_ovf = 0;                                     /* overflow */
uint32 pic_rdp = 0;
uint32 pic_wdp = 0;
uint32 pic_cnti = 0;                                    /* instr/timer */
uint32 pic_arm = 0;                                     /* int arm */
uint32 pic_decr = 1;                                    /* decrement */
uint16 pic_time[4] = { 1, 10, 100, 1000 };              /* delays */
uint16 pic_usec[4] = { 1, 10, 100, 1000 };              /* usec per tick */
static int32 pic_map[16] = {                            /* map rate to delay */
    0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
    };

DEVICE pic_dev;
uint32 pic (uint32 dev, uint32 op, uint32 dat);
t_stat pic_svc (UNIT *uptr);
t_stat pic_reset (DEVICE *dptr);
void pic_sched (t_bool strt);
uint32 pic_rd_cic (void);

int32 lfc_tps = 120;                                    /* ticks per */
int32 lfc_poll = 8000;
uint32 lfc_arm = 0;                                     /* int arm */

DEVICE lfc_dev;
uint32 lfc (uint32 dev, uint32 op, uint32 dat);
t_stat lfc_svc (UNIT *uptr);
t_stat lfc_reset (DEVICE *dptr);
t_stat lfc_set_freq (UNIT *uptr, int32 val, char *cptr, void *desc);
t_stat lfc_show_freq (FILE *st, UNIT *uptr, int32 val, void *desc);

/* PIC data structures

   pic_dev      PIC device descriptor
   pic_unit     PIC unit descriptor
   pic_reg      PIC register list
*/

DIB pic_dib = { d_PIC, -1, v_PIC, NULL, &pic, NULL };

UNIT pic_unit = { UDATA (&pic_svc, UNIT_IDLE, 0), 1000 };

REG pic_reg[] = {
    { HRDATA (BUF, pic_db, 16) },
    { HRDATA (RIC, pic_ric, 16) },
    { HRDATA (CIC, pic_cic, 12) },
    { FLDATA (RDP, pic_rdp, 0) },
    { FLDATA (WDP, pic_wdp, 0) },
    { FLDATA (OVF, pic_ovf, 0) },
    { FLDATA (IREQ, int_req[l_PIC], i_PIC) },
    { FLDATA (IENB, int_enb[l_PIC], i_PIC) },
    { FLDATA (IARM, pic_arm, 0) },
    { BRDATA (TIME, pic_time, 10, 16, 4), REG_NZ + PV_LEFT },
    { DRDATA (SAVE, pic_save, 32), REG_HRO + PV_LEFT },
    { DRDATA (DECR, pic_decr, 16), REG_HRO + PV_LEFT },
    { FLDATA (MODE, pic_cnti, 0), REG_HRO },
    { HRDATA (DEVNO, pic_dib.dno, 8), REG_HRO },
    { NULL }
    };

MTAB pic_mod[] = {
    { UNIT_DIAG, UNIT_DIAG, "diagnostic mode", "DIAG", NULL },
    { UNIT_DIAG, 0, NULL, "NORMAL", NULL },
    { MTAB_XTD|MTAB_VDV, 0, "DEVNO", "DEVNO",
      &set_dev, &show_dev, NULL },
    { 0 }
    };

DEVICE pic_dev = {
    "PIC", &pic_unit, pic_reg, pic_mod,
    1, 0, 0, 0, 0, 0,
    NULL, NULL, &pic_reset,
    NULL, NULL, NULL,
    &pic_dib, DEV_DISABLE
    };

/* LFC data structures

   lfc_dev      LFC device descriptor
   lfc_unit     LFC unit descriptor
   lfc_reg      LFC register list
*/

DIB lfc_dib = { d_LFC, -1, v_LFC, NULL, &lfc, NULL };

UNIT lfc_unit = { UDATA (&lfc_svc, UNIT_IDLE, 0), 8333 };

REG lfc_reg[] = {
    { FLDATA (IREQ, int_req[l_LFC], i_LFC) },
    { FLDATA (IENB, int_enb[l_LFC], i_LFC) },
    { FLDATA (IARM, lfc_arm, 0) },
    { DRDATA (TIME, lfc_unit.wait, 24), REG_NZ + PV_LEFT },
    { DRDATA (TPS, lfc_tps, 8), PV_LEFT + REG_HRO },
    { HRDATA (DEVNO, lfc_dib.dno, 8), REG_HRO },
    { NULL }
    };

MTAB lfc_mod[] = {
    { MTAB_XTD|MTAB_VDV, 100, NULL, "50HZ",
      &lfc_set_freq, NULL, NULL },
    { MTAB_XTD|MTAB_VDV, 120, NULL, "60HZ",
      &lfc_set_freq, NULL, NULL },
    { MTAB_XTD|MTAB_VDV, 0, "FREQUENCY", NULL,
      NULL, &lfc_show_freq, NULL },
    { MTAB_XTD|MTAB_VDV, 0, "DEVNO", "DEVNO",
      &set_dev, &show_dev, NULL },
    { 0 }
    };

DEVICE lfc_dev = {
    "LFC", &lfc_unit, lfc_reg, lfc_mod,
    1, 0, 0, 0, 0, 0,
    NULL, NULL, &lfc_reset,
    NULL, NULL, NULL,
    &lfc_dib, DEV_DISABLE
    };

/* Precision clock: IO routine */

uint32 pic (uint32 dev, uint32 op, uint32 dat)
{
int32 t;

switch (op) {                                           /* case IO op */

    case IO_ADR:                                        /* select */
        return HW;                                      /* HW capable */

    case IO_RH:                                         /* read halfword */
        pic_rdp = 0;                                    /* clr ptr */
        return pic_rd_cic ();   

    case IO_RD:                                         /* read */
        t = pic_rd_cic ();                              /* get cic */
        if (pic_rdp)                                    /* 2nd? get lo */
            t = t & DMASK8;
        else t = (t >> 8) & DMASK8;                     /* 1st? get hi */
        pic_rdp = pic_rdp ^ 1;                          /* flip byte ptr */
        return t;

    case IO_WH:                                         /* write halfword */
        pic_wdp = 0;                                    /* clr ptr */
        pic_db = dat;
        break;

    case IO_WD:                                         /* write */
        if (pic_wdp)
            pic_db = (pic_db & 0xFF00) | dat;
        else pic_db = (pic_db & 0xFF) | (dat << 8);
        pic_wdp = pic_wdp ^ 1;                          /* flip byte ptr */
        break;

    case IO_SS:                                         /* sense status */
        if (pic_ovf) {                                  /* overflow? */
            pic_ovf = 0;                                /* clear flag */
            CLR_INT (v_PIC);                            /* clear intr */
            return STA_OVF;
            }
        return 0;

    case IO_OC:                                         /* output cmd */
        pic_arm = int_chg (v_PIC, dat, pic_arm);        /* upd int ctrl */
        if (dat & CMD_STRT) {                           /* start? */
            pic_ric = pic_db;                           /* new ric */
            pic_cic = GET_CTR (pic_ric);                /* new cic */
            pic_ovf = 0;                                /* clear flag */
            sim_cancel (&pic_unit);                     /* stop clock */
            pic_rdp = pic_wdp = 0;                      /* init ptrs */
            if (pic_ric & PIC_RATE)                     /* any rate? */
                pic_sched (TRUE);
            }                                           /* end if start */
        break;
        }                                               /* end case */

return 0;
}

/* Unit service */

t_stat pic_svc (UNIT *uptr)
{
t_bool rate_chg = FALSE;

if (pic_cnti)                                           /* one shot? */
    pic_cic = 0;
pic_cic = pic_cic - pic_decr;                           /* decrement */
if (pic_cic <= 0) {                                     /* overflow? */
    if (pic_wdp)                                        /* broken wr? set flag */
        pic_ovf = 1;
    if (pic_arm)                                        /* if armed, intr */
        SET_INT (v_PIC);
    if (GET_RATE (pic_ric) != GET_RATE (pic_db))        /* rate change? */
        rate_chg = TRUE;
    pic_ric = pic_db;                                   /* new ric */
    pic_cic = GET_CTR (pic_ric);                        /* new cic */
    if ((pic_ric & PIC_RATE) == 0)
        return SCPE_OK;
    }
pic_sched (rate_chg);
return SCPE_OK;
}

/* Schedule next interval

   If eff rate < 1ms, or diagnostic mode, count instructions
   If eff rate = 1ms, and not diagnostic mode, use timer
*/

void pic_sched (t_bool strt)
{
int32 r, t, intv, intv_usec;

pic_save = sim_grtime ();                               /* save start */
r = pic_map[GET_RATE (pic_ric)];                        /* get mapped rate */
intv = pic_cic? pic_cic: 1;                             /* get cntr */
intv_usec = intv * pic_usec[r];                         /* cvt to usec */
if (!(pic_unit.flags & UNIT_DIAG) &&                    /* not diag? */
    ((intv_usec % 1000) == 0)) {                        /* 1ms multiple? */
        pic_cnti = 0;                                   /* clr mode */
        pic_decr = pic_usec[3 - r];                     /* set decrement */
        if (strt)                                       /* init or */
            t = sim_rtcn_init (pic_time[3], TMR_PIC);
        else t = sim_rtcn_calb (PIC_TPS, TMR_PIC);      /* calibrate */
        }
else {
    pic_cnti = 1;                                       /* set mode */
    pic_decr = 1;                                       /* decr = 1 */
    t = pic_time[r] * intv;                             /* interval */
    if (t == 1)                                         /* for diagn */
        t++;
    }
sim_activate (&pic_unit, t);                            /* activate */
return;
}
            
/* Read (interpolated) current interval */

uint32 pic_rd_cic (void)
{
if (sim_is_active (&pic_unit) && pic_cnti) {            /* running, one shot? */
    uint32 delta = sim_grtime () - pic_save;            /* interval */
    uint32 tm = pic_time[pic_map[GET_RATE (pic_ric)]];  /* ticks/intv */
    delta = delta / tm;                                 /* ticks elapsed */
    if (delta >= ((uint32) pic_cic))                    /* cap value */
        return 0;
    return pic_cic - delta;
    }
return pic_cic;
}

/* Reset routine */

t_stat pic_reset (DEVICE *dptr)
{
sim_cancel (&pic_unit);                                 /* cancel unit */
pic_ric = pic_cic = 0;
pic_db = 0;
pic_ovf = 0;                                            /* clear state */
pic_cnti = 0;
pic_decr = 1;
pic_rdp = pic_wdp = 0;
CLR_INT (v_PIC);                                        /* clear int */
CLR_ENB (v_PIC);                                        /* disable int */
pic_arm = 0;                                            /* disarm int */
return SCPE_OK;
}

/* Line clock: IO routine */

uint32 lfc (uint32 dev, uint32 op, uint32 dat)
{
switch (op) {                                           /* case IO op */

    case IO_ADR:                                        /* select */
        return BY;                                      /* byte only */

    case IO_OC:                                         /* command */
        lfc_arm = int_chg (v_LFC, dat, lfc_arm);        /* upd int ctrl */
        break;
        }
return 0;
}

/* Unit service */

t_stat lfc_svc (UNIT *uptr)
{
lfc_poll = sim_rtcn_calb (lfc_tps, TMR_LFC);            /* calibrate */
sim_activate (uptr, lfc_poll);                          /* reactivate */
if (lfc_arm) {                                          /* armed? */
    SET_INT (v_LFC);                                    /* req intr */
    }
return SCPE_OK;
}

/* Reset routine */

t_stat lfc_reset (DEVICE *dptr)
{
lfc_poll = sim_rtcn_init (lfc_unit.wait, TMR_LFC);
sim_activate_abs (&lfc_unit, lfc_poll);                 /* init clock */
CLR_INT (v_LFC);                                        /* clear int */
CLR_ENB (v_LFC);                                        /* disable int */
lfc_arm = 0;                                            /* disarm int */
return SCPE_OK;
}

/* Set frequency */

t_stat lfc_set_freq (UNIT *uptr, int32 val, char *cptr, void *desc)
{
if (cptr)
    return SCPE_ARG;
if ((val != 100) && (val != 120))
    return SCPE_IERR;
lfc_tps = val;
return SCPE_OK;
}

/* Show frequency */

t_stat lfc_show_freq (FILE *st, UNIT *uptr, int32 val, void *desc)
{
fprintf (st, (lfc_tps == 100)? "50Hz": "60Hz");
return SCPE_OK;
}