-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccsds-timecodes.pk
237 lines (207 loc) · 6.28 KB
/
ccsds-timecodes.pk
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
/**
* @file ccsds-timecodes.pk
* @author Conlan Wesson
* Poke support for CCSDS Time Code Formats (CCSDS 301.0-B-4).
*
* @ingroup ccsds
* @defgroup timecodes CCSDS Time Code Formats
* @{
*/
load bcd;
/// Unsegmented Time Code Level 1 (3.2.2)
var TIME_CODE_CUC_LEVEL_1 = 0b001;
/// Unsegmented Time Code Level 1 (3.2.2)
var TIME_CODE_CUC_LEVEL_2 = 0b010;
/// Day Segmented Time Code (3.3.2)
var TIME_CODE_CDS = 0b100;
/// Calendar Segmented Time Code (3.4.2)
var TIME_CODE_CCS = 0b101;
/// Agency-Defined Time Code (3.6.2)
var TIME_CODE_AGENCY_DEFINED = 0b110;
/**
* CCSDS Time Code P-Field Octet 1.
*/
type Time_Code_Preamble =
struct
{
uint<1> extension_flag;
uint<3> identification;
// Unsegmented Time Code (3.2.2)
if (identification == TIME_CODE_CUC_LEVEL_1 || identification == TIME_CODE_CUC_LEVEL_2)
struct {
uint<2> basic_length;
uint<2> fractional_length;
} cuc;
// Day Segmented Time Code (3.3.2)
if (identification == TIME_CODE_CDS)
struct {
uint<1> epoch;
uint<1> length_of_day;
uint<2> length_of_submilliseconds;
} cds;
// Calendar Segmented Time Code (3.4.2)
if (identification == TIME_CODE_CCS)
struct {
uint<1> variation;
uint<3> resolution : resolution != 0b111;
} ccs;
// Agency-Defined Time Code (3.6.2)
if (identification == TIME_CODE_AGENCY_DEFINED)
struct {
uint<4> t_length;
} agency;
// Preamble is always 1 Byte
byte[0] @ 1#B;
/**
* Pretty printer.
*/
method _print = void:
{
print ("#<\n");
printf (" %<struct-field-name:Time Code Preamble%>:\n");
printf (" %<struct-field-name:Extended%>: %s\n", extension_flag ? "Yes" : "No");
// Identification
printf (" %<struct-field-name:Identification%>: ");
if (identification == TIME_CODE_CUC_LEVEL_1)
print ("Unsegmented Time Code (1958 January 1 epoch)\n");
else if (identification == TIME_CODE_CUC_LEVEL_2)
print ("Unsegmented Time Code (Agency-defined epoch)\n");
else if (identification == TIME_CODE_CDS)
print ("Day Segmented Time Code\n");
else if (identification == TIME_CODE_CCS)
print ("Calendar Segmented Time Code\n");
else if (identification == TIME_CODE_AGENCY_DEFINED)
print ("Agency-Defined Time Code\n");
else
printf ("%<integer:0x%u3x%>\n", identification);
print (">\n");
}
};
assert(Time_Code_Preamble{}'size == 1#B);
/**
* CCSDS Time Codes.
*/
type Time_Code =
struct
{
type Time_Code_PExtension =
struct
{
uint<1> extension_flag : extension_flag == 1T;
uint<7> details;
};
type Time_Code_PLast =
struct
{
uint<1> extension_flag : extension_flag == 0T;
uint<7> details;
};
Time_Code_Preamble p; ///< P-field octet 1
/// @todo There has to be a better way to do this
if (p.extension_flag)
Time_Code_PExtension[] p_extended;
if (p.extension_flag)
Time_Code_PLast p_last;
method get_p = (uint<32> index) Time_Code_PExtension:
{
if (index == 0)
return p as Time_Code_PExtension;
else if (index-1 < p_extended'length)
return p_extended[index-1];
else if (index == p_extended'length)
return p_last as Time_Code_PExtension;
}
// Unsegmented Time Code
if (p.identification == TIME_CODE_CUC_LEVEL_1 || p.identification == TIME_CODE_CUC_LEVEL_2)
struct {
var basic_length = p.cuc.basic_length + 1 + ((p.extension_flag ? (p_extended'length > 0 ? p_extended[0].details : p_last.details) : (0 as uint<7>)) .>> 5);
var fractional_length = p.cuc.fractional_length + (((p.extension_flag ? (p_extended'length > 0 ? (p_extended[0].details & 0x7F) : (p_last.details & 0x7F)) : 0U) .>> 2) & 0b111);
byte[basic_length] basic;
byte[fractional_length] fractional;
};
// Day Segmented Time Code
if (p.identification == TIME_CODE_CDS)
struct {
if (p.cds.length_of_day == 0T)
struct {
uint<16> day;
};
if (p.cds.length_of_day == 1T)
struct {
uint<24> day;
};
uint<32> ms_of_day;
if (p.cds.length_of_submilliseconds == 0b01)
uint<16> microseconds;
if (p.cds.length_of_submilliseconds == 0b10)
uint<32> picoseconds;
};
// Calendar Segmented Time Code
if (p.identification == TIME_CODE_CCS)
struct {
uint<16> yr;
computed uint<16> year;
method get_year = uint<16>:
{
return bcd_to_u16(yr);
}
method set_year = (uint<16> y) void:
{
yr = u16_to_bcd(y);
}
if (p.ccs.variation == 0T)
uint<8> mo;
if (p.ccs.variation == 0T)
uint<8> dom;
if (p.ccs.variation == 1T)
uint<16> doy;
uint<8> h;
uint<8> m;
uint<8> s;
computed uint<8> hour;
method get_hour = uint<8>:
{
return bcd_to_u8(h);
}
method set_hour = (uint<8> hr) void:
{
h = u8_to_bcd(hr);
}
computed uint<8> minute;
method get_minute = uint<8>:
{
return bcd_to_u8(m);
}
method set_minute = (uint<8> min) void:
{
m = u8_to_bcd(min);
}
computed uint<8> second;
method get_second = uint<8>:
{
return bcd_to_u8(s);
}
method set_second = (uint<8> sec) void:
{
s = u8_to_bcd(sec);
}
if (p.ccs.resolution >= 0b001)
uint<8> s2;
if (p.ccs.resolution >= 0b010)
uint<8> s4;
if (p.ccs.resolution >= 0b011)
uint<8> s6;
if (p.ccs.resolution >= 0b100)
uint<8> s8;
if (p.ccs.resolution >= 0b101)
uint<8> s10;
if (p.ccs.resolution >= 0b110)
uint<8> s12;
} ccs;
// Agency-Defined Time Code
if (p.identification == TIME_CODE_AGENCY_DEFINED)
struct {
byte[p.agency.t_length+1] data;
} agency;
};
/** @} */ // defgroup timecodes