1 /*-
2  * Copyright (c) 2014 Poul-Henning Kamp
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * Simulated timebase
27  * ==================
28  *
29  * Very simple minded:  Time advances when TB_Sleep() is called only.
30  *
31  */
32 
33 #include <math.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "ntimed.h"
38 
39 static struct timestamp st_now;
40 
41 static double freq = 0;
42 static double freq0 = 0;
43 
44 static double adj_offset = 0;
45 static double adj_duration = 0;
46 static double adj_freq = 0;
47 
48 /*
49  * This variable is public and represent the amount of time the simulated
50  * clock has been tweaked by the TB_Step() and TB_Adjust() functions.
51  *
52  * This can be used to "(re-)model" previously recorded event series
53  * onto the ST timebase.
54  */
55 
56 double Time_Sim_delta = 0;
57 
58 /**********************************************************************/
59 
60 static struct timestamp *
st_Now(struct timestamp * storage)61 st_Now(struct timestamp *storage)
62 {
63 	if (storage == NULL)
64 		ALLOC_OBJ(storage, TIMESTAMP_MAGIC);
65 	AN(storage);
66 	*storage = st_now;
67 	return (storage);
68 }
69 
70 /**********************************************************************/
71 
72 static int
st_Sleep(double dur)73 st_Sleep(double dur)
74 {
75 
76 	TS_Add(&st_now, dur);
77 	Time_Sim_delta += dur * freq;
78 	return (0);
79 }
80 
81 /**********************************************************************/
82 
__match_proto__(tb_step_f)83 static void __match_proto__(tb_step_f)
84 st_Step(struct ocx *ocx, double offset)
85 {
86 
87 	Debug(ocx, "SIMSTEP %.3e\n", offset);
88 	Time_Sim_delta += offset;
89 	TB_generation++;
90 }
91 
92 /**********************************************************************/
93 
__match_proto__(tb_adjust_f)94 static void __match_proto__(tb_adjust_f)
95 st_Adjust(struct ocx *ocx, double offset, double duration, double frequency)
96 {
97 
98 	(void)ocx;
99 	adj_offset = offset;
100 	adj_duration = floor(duration);
101 	if (adj_offset > 0.0 && adj_duration == 0.0)
102 		adj_duration = 1.0;
103 	adj_freq = frequency;
104 }
105 
106 /**********************************************************************/
107 
__match_proto__(todo_f)108 static enum todo_e __match_proto__(todo_f)
109 st_kern_pll(struct ocx *ocx, struct todolist *tdl, void *priv)
110 {
111 	double d;
112 
113 	(void)ocx;
114 	AN(tdl);
115 	AZ(priv);
116 	freq = freq0 + adj_freq;
117 	if (adj_duration > 0.0) {
118 		d = adj_offset / adj_duration;
119 		freq += d;
120 		adj_offset -= d;
121 		adj_duration -= 1.0;
122 	}
123 	Put(ocx, OCX_TRACE, "SIMPLL %.3e %.3e %.3e\n",
124 	    adj_freq, adj_offset, adj_duration);
125 	return (TODO_OK);
126 }
127 
128 /**********************************************************************
129  * Mechanism to artificially bump simulated clock around.
130  */
131 
132 struct st_bump {
133 	unsigned		magic;
134 #define ST_BUMP_MAGIC		0xc8981be3
135 	double			bfreq;
136 	double			bphase;
137 };
138 
__match_proto__(todo_f)139 static enum todo_e __match_proto__(todo_f)
140 st_kern_bump(struct ocx *ocx, struct todolist *tdl, void *priv)
141 {
142 	struct st_bump *stb;
143 
144 	(void)ocx;
145 	(void)tdl;
146 	CAST_OBJ_NOTNULL(stb, priv, ST_BUMP_MAGIC);
147 
148 	freq0 += stb->bfreq;
149 	Time_Sim_delta += stb->bphase;
150 	FREE_OBJ(stb);
151 	return (TODO_OK);
152 }
153 
154 void
Time_Sim_Bump(struct todolist * tdl,double when,double bfreq,double bphase)155 Time_Sim_Bump(struct todolist *tdl, double when, double bfreq, double bphase)
156 {
157 	struct st_bump *stb;
158 
159 	ALLOC_OBJ(stb, ST_BUMP_MAGIC);
160 	AN(stb);
161 	stb->bfreq = bfreq;
162 	stb->bphase = bphase;
163 	(void)TODO_ScheduleRel(tdl, st_kern_bump, stb, when, 0.0, "BUMP");
164 }
165 
166 /**********************************************************************/
167 
168 void
Time_Sim(struct todolist * tdl)169 Time_Sim(struct todolist *tdl)
170 {
171 
172 	INIT_OBJ(&st_now, TIMESTAMP_MAGIC);
173 	TS_Add(&st_now, 1e6);
174 	TB_Now = st_Now;
175 	TB_Sleep = st_Sleep;
176 	TB_Step = st_Step;
177 	TB_Adjust = st_Adjust;
178 	(void)TODO_ScheduleRel(tdl, st_kern_pll, NULL, 0.0, 1.0, "SIMPLL");
179 }
180