First Commit of my working state
[simh.git] / PDP1 / pdp1_clk.c
CommitLineData
196ba1fc
PH
1/* pdp1_clk.c: PDP-1D clock simulator\r
2\r
3 Copyright (c) 2006, Robert M. Supnik\r
4\r
5 Permission is hereby granted, free of charge, to any person obtaining a\r
6 copy of this software and associated documentation files (the "Software"),\r
7 to deal in the Software without restriction, including without limitation\r
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
9 and/or sell copies of the Software, and to permit persons to whom the\r
10 Software is furnished to do so, subject to the following conditions:\r
11\r
12 The above copyright notice and this permission notice shall be included in\r
13 all copies or substantial portions of the Software.\r
14\r
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL\r
18 ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21\r
22 Except as contained in this notice, the name of Robert M Supnik shall not be\r
23 bused in advertising or otherwise to promote the sale, use or other dealings\r
24 in this Software without prior written authorization from Robert M Supnik.\r
25\r
26 clk PDP-1D clock\r
27\r
28 Note that the clock is run at 1/8 of real speed (125Hz instead of 1Khz), to\r
29 provide for eventual implementation of idling.\r
30*/\r
31\r
32#include "pdp1_defs.h"\r
33\r
34#define CLK_HWRE_TPS 1000 /* hardware freq */\r
35#define CLK_TPS 125 /* sim freq */\r
36#define CLK_CNTS (CLK_HWRE_TPS / CLK_TPS) /* counts per tick */\r
37#define CLK_C1MIN (1000 * 60) /* counts per min */\r
38#define CLK_C32MS 32 /* counts per 32ms */\r
39\r
40int32 clk32ms_sbs = 0; /* 32ms SBS level */\r
41int32 clk1min_sbs = 0; /* 1min SBS level */\r
42int32 clk_cntr = 0;\r
43int32 tmxr_poll = 5000;\r
44\r
45extern int32 stop_inst;\r
46\r
47t_stat clk_svc (UNIT *uptr);\r
48t_stat clk_reset (DEVICE *dptr);\r
49\r
50/* CLK data structures\r
51\r
52 clk_dev CLK device descriptor\r
53 clk_unit CLK unit\r
54 clk_reg CLK register list\r
55*/\r
56\r
57UNIT clk_unit = {\r
58 UDATA (&clk_svc, 0, 0), 5000\r
59 };\r
60\r
61REG clk_reg[] = {\r
62 { ORDATA (CNTR, clk_cntr, 16) },\r
63 { DRDATA (SBS32LVL, clk32ms_sbs, 4), REG_HRO },\r
64 { DRDATA (SBS1MLVL, clk1min_sbs, 4), REG_HRO },\r
65 { NULL }\r
66 };\r
67\r
68MTAB clk_mod[] = {\r
69 { MTAB_XTD|MTAB_VDV, 0, "SBS32MSLVL", "SBS32MSLVL",\r
70 &dev_set_sbs, &dev_show_sbs, (void *) &clk32ms_sbs },\r
71 { MTAB_XTD|MTAB_VDV, 0, "SBS1MINLVL", "SBS1MINLVL",\r
72 &dev_set_sbs, &dev_show_sbs, (void *) &clk1min_sbs },\r
73 { 0 }\r
74 };\r
75\r
76DEVICE clk_dev = {\r
77 "CLK", &clk_unit, clk_reg, clk_mod,\r
78 1, 10, 31, 1, 8, 8,\r
79 NULL, NULL, &clk_reset,\r
80 NULL, NULL, NULL,\r
81 NULL, DEV_DISABLE | DEV_DIS\r
82 };\r
83\r
84/* Clock IOT routine */\r
85\r
86int32 clk (int32 inst, int32 dev, int32 dat)\r
87{\r
88int32 used, incr;\r
89\r
90if (clk_dev.flags & DEV_DIS) /* disabled? */\r
91 return (stop_inst << IOT_V_REASON) | dat; /* illegal inst */\r
92used = tmxr_poll - (sim_is_active (&clk_unit) - 1);\r
93incr = (used * CLK_CNTS) / tmxr_poll;\r
94return clk_cntr + incr;\r
95}\r
96\r
97/* Unit service, generate appropriate interrupts */\r
98\r
99t_stat clk_svc (UNIT *uptr)\r
100{\r
101if (clk_dev.flags & DEV_DIS) return SCPE_OK; /* disabled? */\r
102tmxr_poll = sim_rtcn_calb (CLK_TPS, TMR_CLK); /* calibrate clock */\r
103sim_activate (&clk_unit, tmxr_poll); /* reactivate unit */\r
104clk_cntr = clk_cntr + CLK_CNTS; /* incr counter */\r
105if ((clk_cntr % CLK_C32MS) == 0) /* 32ms interval? */\r
106 dev_req_int (clk32ms_sbs); /* req intr */\r
107if (clk_cntr >= CLK_C1MIN) { /* 1min interval? */\r
108 dev_req_int (clk1min_sbs); /* req intr */\r
109 clk_cntr = 0; /* reset counter */\r
110 }\r
111return SCPE_OK;\r
112}\r
113\r
114/* Reset routine */\r
115\r
116t_stat clk_reset (DEVICE *dptr)\r
117{\r
118if (clk_dev.flags & DEV_DIS) sim_cancel (&clk_unit); /* disabled? */\r
119else {\r
120 tmxr_poll = sim_rtcn_init (clk_unit.wait, TMR_CLK);\r
121 sim_activate_abs (&clk_unit, tmxr_poll); /* activate unit */\r
122 }\r
123clk_cntr = 0; /* clear counter */\r
124return SCPE_OK;\r
125}\r