1 /* pdp10_tim.c: PDP-10 tim subsystem simulator
3 Copyright (c) 1993-2007, Robert M Supnik
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of Robert M Supnik shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from Robert M Supnik.
28 18-Jun-07 RMS Added UNIT_IDLE flag
29 03-Nov-06 RMS Rewritten to support idling
30 29-Oct-06 RMS Added clock coscheduling function
31 02-Feb-04 RMS Exported variables needed by Ethernet simulator
32 29-Jan-02 RMS New data structures
33 06-Jan-02 RMS Added enable/disable support
34 02-Dec-01 RMS Fixed bug in ITS PC sampling (found by Dave Conroy)
35 31-Aug-01 RMS Changed int64 to t_int64 for Windoze
36 17-Jul-01 RMS Moved function prototype
37 04-Jul-01 RMS Added DZ11 support
40 #include "pdp10_defs.h"
45 #define TIM_HW_FREQ 4100000 /* 4.1Mhz */
46 #define TIM_HWRE_MASK 07777
47 #define UNIT_V_Y2K (UNIT_V_UF + 0) /* Y2K compliant OS */
48 #define UNIT_Y2K (1u << UNIT_V_Y2K)
50 /* Clock mode TOPS-10/ITS */
52 #define TIM_TPS_T10 60
53 #define TIM_WAIT_T10 8000
54 #define TIM_MULT_T10 1
55 #define TIM_ITS_QUANT (TIM_HW_FREQ / TIM_TPS_T10)
57 /* Clock mode TOPS-20/KLAD */
59 #define TIM_TPS_T20 1001
60 #define TIM_WAIT_T20 500
61 #define TIM_MULT_T20 16
63 /* Probability function for TOPS-20 idlelock */
65 #define PROB(x) (((rand() * 100) / RAND_MAX) >= (x))
67 d10 tim_base
[2] = { 0, 0 }; /* 71b timebase */
68 d10 tim_ttg
= 0; /* time to go */
69 d10 tim_period
= 0; /* period */
70 d10 quant
= 0; /* ITS quantum */
71 int32 tim_mult
= TIM_MULT_T10
; /* tmxr poll mult */
72 int32 tim_t20_prob
= 33; /* TOPS-20 prob */
74 /* Exported variables */
76 int32 clk_tps
= TIM_TPS_T10
; /* clock ticks/sec */
77 int32 tmr_poll
= TIM_WAIT_T10
; /* clock poll */
78 int32 tmxr_poll
= TIM_WAIT_T10
* TIM_MULT_T10
; /* term mux poll */
80 extern int32 apr_flg
, pi_act
;
84 extern int32 t20_idlelock
;
87 t_stat
tcu_rd (int32
*data
, int32 PA
, int32 access
);
88 t_stat
tim_svc (UNIT
*uptr
);
89 t_stat
tim_reset (DEVICE
*dptr
);
90 void tim_incr_base (d10
*base
, d10 incr
);
92 extern d10
Read (a10 ea
, int32 prv
);
93 extern d10
ReadM (a10 ea
, int32 prv
);
94 extern void Write (a10 ea
, d10 val
, int32 prv
);
95 extern void WriteP (a10 ea
, d10 val
);
96 extern int32
pi_eval (void);
97 extern t_stat
wr_nop (int32 data
, int32 PA
, int32 access
);
99 /* TIM data structures
101 tim_dev TIM device descriptor
102 tim_unit TIM unit descriptor
103 tim_reg TIM register list
106 DIB tcu_dib
= { IOBA_TCU
, IOLN_TCU
, &tcu_rd
, &wr_nop
, 0 };
108 UNIT tim_unit
= { UDATA (&tim_svc
, UNIT_IDLE
, 0), TIM_WAIT_T10
};
111 { BRDATA (TIMEBASE
, tim_base
, 8, 36, 2) },
112 { ORDATA (TTG
, tim_ttg
, 36) },
113 { ORDATA (PERIOD
, tim_period
, 36) },
114 { ORDATA (QUANT
, quant
, 36) },
115 { DRDATA (TIME
, tim_unit
.wait
, 24), REG_NZ
+ PV_LEFT
},
116 { DRDATA (PROB
, tim_t20_prob
, 6), REG_NZ
+ PV_LEFT
+ REG_HIDDEN
},
117 { DRDATA (POLL
, tmr_poll
, 32), REG_HRO
+ PV_LEFT
},
118 { DRDATA (MUXPOLL
, tmxr_poll
, 32), REG_HRO
+ PV_LEFT
},
119 { DRDATA (MULT
, tim_mult
, 6), REG_HRO
+ PV_LEFT
},
120 { DRDATA (TPS
, clk_tps
, 12), REG_HRO
+ PV_LEFT
},
125 { UNIT_Y2K
, 0, "non Y2K OS", "NOY2K", NULL
},
126 { UNIT_Y2K
, UNIT_Y2K
, "Y2K OS", "Y2K", NULL
},
127 { MTAB_XTD
|MTAB_VDV
, 000, "ADDRESS", NULL
,
128 NULL
, &show_addr
, NULL
},
133 "TIM", &tim_unit
, tim_reg
, tim_mod
,
135 NULL
, NULL
, &tim_reset
,
140 /* Timer instructions */
142 /* Timer - if the timer is running at less than hardware frequency,
143 need to interpolate the value by calculating how much of the current
144 clock tick has elapsed, and what that equates to in msec. */
146 t_bool
rdtim (a10 ea
, int32 prv
)
150 ReadM (INCA (ea
), prv
); /* check 2nd word */
151 tempbase
[0] = tim_base
[0]; /* copy time base */
152 tempbase
[1] = tim_base
[1];
153 if (tim_mult
!= TIM_MULT_T20
) { /* interpolate? */
156 used
= tmr_poll
- (sim_is_active (&tim_unit
) - 1);
157 incr
= (d10
) (((double) used
* TIM_HW_FREQ
) /
158 ((double) tmr_poll
* (double) clk_tps
));
159 tim_incr_base (tempbase
, incr
);
161 tempbase
[0] = tempbase
[0] & ~((d10
) TIM_HWRE_MASK
); /* clear low 12b */
162 Write (ea
, tempbase
[0], prv
);
163 Write (INCA(ea
), tempbase
[1], prv
);
167 t_bool
wrtim (a10 ea
, int32 prv
)
169 tim_base
[0] = Read (ea
, prv
);
170 tim_base
[1] = CLRS (Read (INCA (ea
), prv
));
174 t_bool
rdint (a10 ea
, int32 prv
)
176 Write (ea
, tim_period
, prv
);
180 t_bool
wrint (a10 ea
, int32 prv
)
182 tim_period
= Read (ea
, prv
);
183 tim_ttg
= tim_period
;
187 /* Timer service - the timer is only serviced when the 'ttg' register
188 has reached 0 based on the expected frequency of clock interrupts. */
190 t_stat
tim_svc (UNIT
*uptr
)
192 if (cpu_unit
.flags
& UNIT_KLAD
) /* diags? */
193 tmr_poll
= uptr
->wait
; /* fixed clock */
194 else tmr_poll
= sim_rtc_calb (clk_tps
); /* else calibrate */
195 sim_activate (uptr
, tmr_poll
); /* reactivate unit */
196 tmxr_poll
= tmr_poll
* tim_mult
; /* set mux poll */
197 tim_incr_base (tim_base
, tim_period
); /* incr time base */
198 tim_ttg
= tim_period
; /* reload */
199 apr_flg
= apr_flg
| APRF_TIM
; /* request interrupt */
200 if (Q_ITS
) { /* ITS? */
202 quant
= (quant
+ TIM_ITS_QUANT
) & DMASK
;
203 if (TSTS (pcst
)) { /* PC sampling? */
204 WriteP ((a10
) pcst
& AMASK
, pager_PC
); /* store sample */
205 pcst
= AOB (pcst
); /* add 1,,1 */
208 else if (t20_idlelock
&& PROB (100 - tim_t20_prob
))
213 /* Clock coscheduling routine */
215 int32
clk_cosched (int32 wait
)
219 if (tim_mult
== TIM_MULT_T20
) return wait
;
220 t
= sim_is_active (&tim_unit
);
221 return (t
? t
- 1: wait
);
224 void tim_incr_base (d10
*base
, d10 incr
)
226 base
[1] = base
[1] + incr
; /* add on incr */
227 base
[0] = base
[0] + (base
[1] >> 35); /* carry to high */
228 base
[0] = base
[0] & DMASK
; /* mask high */
229 base
[1] = base
[1] & MMASK
; /* mask low */
235 t_stat
tim_reset (DEVICE
*dptr
)
237 tim_period
= 0; /* clear timer */
239 apr_flg
= apr_flg
& ~APRF_TIM
; /* clear interrupt */
240 tmr_poll
= sim_rtc_init (tim_unit
.wait
); /* init timer */
241 sim_activate_abs (&tim_unit
, tmr_poll
); /* activate unit */
242 tmxr_poll
= tmr_poll
* tim_mult
; /* set mux poll */
246 /* Set timer parameters from CPU model */
248 t_stat
tim_set_mod (UNIT
*uptr
, int32 val
, char *cptr
, void *desc
)
250 if (val
& (UNIT_T20
|UNIT_KLAD
)) {
251 clk_tps
= TIM_TPS_T20
;
252 uptr
->wait
= TIM_WAIT_T20
;
253 tmr_poll
= TIM_WAIT_T20
;
254 tim_mult
= TIM_MULT_T20
;
255 uptr
->flags
= uptr
->flags
| UNIT_Y2K
;
258 clk_tps
= TIM_TPS_T10
;
259 uptr
->wait
= TIM_WAIT_T10
;
260 tmr_poll
= TIM_WAIT_T10
;
261 tim_mult
= TIM_MULT_T10
;
262 if (Q_ITS
) uptr
->flags
= uptr
->flags
| UNIT_Y2K
;
263 else uptr
->flags
= uptr
->flags
& ~UNIT_Y2K
;
265 tmxr_poll
= tmr_poll
* tim_mult
;
269 /* Time of year clock */
271 t_stat
tcu_rd (int32
*data
, int32 PA
, int32 access
)
276 curtim
= time (NULL
); /* get time */
277 tptr
= localtime (&curtim
); /* decompose */
278 if (tptr
== NULL
) return SCPE_NXM
; /* Y2K prob? */
279 if ((tptr
->tm_year
> 99) && !(tim_unit
.flags
& UNIT_Y2K
))
282 switch ((PA
>> 1) & 03) { /* decode PA<3:1> */
284 case 0: /* year/month/day */
285 *data
= (((tptr
->tm_year
) & 0177) << 9) |
286 (((tptr
->tm_mon
+ 1) & 017) << 5) |
287 ((tptr
->tm_mday
) & 037);
290 case 1: /* hour/minute */
291 *data
= (((tptr
->tm_hour
) & 037) << 8) |
292 ((tptr
->tm_min
) & 077);
296 *data
= (tptr
->tm_sec
) & 077;
304 return SCPE_NXM
; /* can't get here */