OpenCores
URL https://opencores.org/ocsvn/irig_regenerator/irig_regenerator/trunk

Subversion Repositories irig_regenerator

[/] [irig_regenerator/] [trunk/] [rtl/] [irig_time_pack.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jclaytons
--------------------------------------------------------------------------
2
-- Package of IRIG time pattern generator components
3
--
4
 
5
library IEEE;
6
use IEEE.STD_LOGIC_1164.ALL;
7
use IEEE.NUMERIC_STD.ALL;
8
 
9
package irig_time_pack is
10
 
11
-- This component includes the capability to output both DC Level and modulated timecode streams.
12
-- It has been coded with 32 bit wide registers
13
  component irig_time_generator
14
    generic (
15
      SYS_CLK_RATE : real; -- Needed for carrier generation
16
      DEF_R_0 : unsigned(31 downto 0); -- Years, Days
17
      DEF_R_1 : unsigned(31 downto 0); -- Hours, Minutes, Seconds
18
      DEF_R_2 : unsigned(31 downto 0); -- Control(31:0)
19
      DEF_R_3 : unsigned(31 downto 0); -- Control(44:32)
20
      DEF_R_4 : unsigned(31 downto 0); -- Rate Setting
21
      DEF_R_5 : unsigned(31 downto 0); -- Fields Used [ctrl,sbs,year]
22
      DEF_R_6 : unsigned(31 downto 0); -- Carrier Frequency
23
      DEF_R_Z : unsigned(31 downto 0)  -- Value returned for nonexistent registers
24
    );
25
    port (
26
 
27
      sys_rst_n   : in std_logic;
28
      sys_clk     : in std_logic;
29
      sys_clk_en  : in std_logic;
30
 
31
      -- Bus interface
32
      adr_i       : in  unsigned(3 downto 0);
33
      sel_i       : in  std_logic;
34
      we_i        : in  std_logic;
35
      dat_i       : in  unsigned(31 downto 0);
36
      dat_o       : out unsigned(31 downto 0);
37
      ack_o       : out std_logic;
38
 
39
      -- Pulse per second time reference
40
      pps_i       : in  std_logic;
41
      pps_ext_i   : in  std_logic;
42
 
43
      -- IRIG select inputs
44
      -- Active only when register 0x4 set to zero.
45
      rate_i      : in  unsigned(2 downto 0); -- Rates    : 0,1=>B, 2=>A, 3=>G, 4=>NASA 36-bit
46
      carrier_i   : in  unsigned(2 downto 0); -- Carriers : 0=DCLS, 1=100Hz, 2=1kHz, 3=10kHz, 4=100kHz
47
      codes_i     : in  unsigned(2 downto 0); -- Codes    : (2)=> control, (1)=> SBS, (0)=> year
48
 
49
      -- Serial IRIG time stream, LVTTL level output
50
      time_dcl_o  : out std_logic;
51
      cap_gnd_o   : out unsigned(2 downto 0);
52
      ref_o       : out std_logic;
53
      carrier_o   : out unsigned(1 downto 0)
54
    );
55
  end component;
56
 
57
-- This component includes the capability to receive DC Level timecode streams.
58
-- External circuitry should low-pass filter the timecode signal to remove the
59
-- carrier frequency, and pass the envelope signal directly
60
-- to this module, where it is treated as a digital input.
61
-- Although it is a receiver, it also acts as a "synchronized transmitter."
62
  component irig_time_regenerator
63
    generic (
64
      SYS_CLK_RATE   : real;
65
      LOCK_THRESHOLD : integer; -- Packets received before entering locked state
66
      DEF_R_0 : unsigned(31 downto 0); -- Years, Days
67
      DEF_R_1 : unsigned(31 downto 0); -- Hours, Minutes, Seconds
68
      DEF_R_2 : unsigned(31 downto 0); -- Control(31:0)
69
      DEF_R_3 : unsigned(31 downto 0); -- Control(44:32)
70
      DEF_R_4 : unsigned(31 downto 0); -- Rate Setting
71
      DEF_R_5 : unsigned(31 downto 0); -- Fields Used [ctrl,sbs,year]
72
      DEF_R_6 : unsigned(31 downto 0); -- Carrier Frequency
73
      DEF_R_A : unsigned(31 downto 0); -- Initial value of carrier detect bias
74
      DEF_R_Z : unsigned(31 downto 0)  -- Value returned for nonexistent registers
75
    );
76
    port (
77
      sys_rst_n     : in std_logic;
78
      sys_clk       : in std_logic;
79
      sys_clk_en    : in std_logic;
80
 
81
      -- Bus interface
82
      adr_i         : in  unsigned(3 downto 0);
83
      sel_i         : in  std_logic;
84
      we_i          : in  std_logic;
85
      dat_i         : in  unsigned(31 downto 0);
86
      dat_o         : out unsigned(31 downto 0);
87
      ack_o         : out std_logic;
88
 
89
      -- IRIG select inputs
90
      -- Active only when register 0x4 set to zero.
91
      rate_i        : in  unsigned(2 downto 0); -- Rates    : 0,1=>B, 2=>A, 3=>G, 4=>NASA 36-bit
92
      carrier_i     : in  unsigned(2 downto 0); -- Carriers : 0=DCLS, 1=100Hz, 2=1kHz, 3=10kHz, 4=100kHz
93
      codes_i       : in  unsigned(2 downto 0); -- Codes    : (2)=> control, (1)=> SBS, (0)=> year
94
 
95
      -- IRIG time output, parallel form
96
      time_o        : out unsigned(67 downto 0);
97
 
98
      -- Carrier Detect Select output, PPS input,
99
      -- IRIG time input, tracking and lock indicators
100
      cdet_sel_o    : out std_logic;
101
      pps_i         : in  std_logic;
102
      time_cdet_i   : in  std_logic;
103
      cdet_val_o    : out unsigned(1 downto 0);
104
      active_o      : out std_logic;
105
      tracking_o    : out std_logic;
106
      lock_o        : out std_logic;
107
      neverlocked_o : out std_logic;
108
 
109
      -- Serial IRIG time stream output, LVTTL level
110
      time_dcl_o    : out std_logic;
111
      cap_gnd_o     : out std_logic;
112
      ref_o         : out std_logic;
113
      carrier_o     : out unsigned(1 downto 0)
114
    );
115
  end component;
116
 
117
end irig_time_pack;
118
 
119
package body irig_time_pack is
120
end irig_time_pack;
121
 
122
 
123
-------------------------------------------------------------------------------
124
-- IRIG time pattern generator core
125
-------------------------------------------------------------------------------
126
--
127
-- Author: John Clayton
128
-- Date  : Oct. 14, 2010 Copied code from irig_time, changed registers to
129
--                       a 32-bit format.
130
--         Nov. 24, 2010 Added DEF_R_Z generic parameter
131
--         Mar. 28, 2012 Moved clk_en_1mhz logic inside this module, updated
132
--                       description.  Added "pps_ext_i" input.  Changed name
133
--                       to "irig_time_generator"
134
--         Apr.  4, 2012 Swapped R0 and R1, so that years & days are first.
135
--                       This arrangement will be more congruent with the
136
--                       IRIG time receiver.
137
--         Apr.  5, 2012 Added support for NASA 36-bit (WWV) format, by
138
--                       extending the rate setting to include a new value,
139
--                       of 4.
140
--         Apr. 30, 2013 Changed SYS_CLK_RATE from integer to real type.
141
--         Dec.  2, 2013 Decreased SQF_BITS from 6 to 4, in order to increase
142
--                       the frequency of the square_fast signal by a factor
143
--                       of 4.
144
--         Mar. 11, 2015 Added leap_year bit to register zero.  Prevented
145
--                       day values higher than 366 from persisting.
146
--
147
-- Description
148
-------------------------------------------------------------------------------
149
-- This time pattern generator outputs DC Level data, plus a pair of digital outputs
150
-- which contain a modulated carrier signal.  The modulated carrier frequency is
151
-- the low-frequency heterodyne signal generated by digital mixing of a 500 kHz 
152
-- squarewave with a 501 kHz (IRIG "B"), 510 kHz (IRIG "A") or 600 kHz (IRIG "G") 
153
-- squarewave, as generated by a DDS.  As such, the digital output needs to be
154
-- low-pass filtered in order to cut down the amplitude of the high-frequency
155
-- heterodyne signal, plus the 3rd and higher order harmonics of the squarewaves
156
-- used.  In order to modulate the amplitude of the resulting low-pass filtered
157
-- carrier over a 3:1 ratio (An AM modulation index of 0.5, meaning 50% modulation
158
-- depth) a 2-bit R-2R resistor ladder network DAC is recommended for use outside
159
-- the FPGA.  Also, in order to keep the modulated signal balanced, a reference
160
-- voltage other than ground is used.  The reference voltage is nominally 1/2 of
161
-- the FPGA VCCo supply voltage (3.3V * 0.5 = 1.65V).  The FPGA generates this
162
-- reference voltage through the use of an output which provides a 50% duty cycle
163
-- squarewave.  The schematic is as follows:
164
--
165
--                    R  1.65V  R     R
166
--         ref_o o---/\/\--.--/\/\--/\/\--.
167
--                         |              |
168
--                    Cref - +            |
169
--                    15uF -              |
170
--                         |              |
171
--                        GND             |
172
--                              R     R   |
173
--  carrier_o(0) o------------/\/\--/\/\--.
174
--    (LSB)                               |
175
--                                      R <
176
--                                        <     Cout
177
--                              R     R   |     4.7uF
178
--  carrier_o(1) o------------/\/\--/\/\--.-----|(-------o AC coupled, Modulated Output
179
--    (MSB)                               |
180
--                          Cfilt         |
181
--                          0.01uF        |
182
--  cap_gnd_o(0) o------------|(----------.
183
--                                        |
184
--                          Cfilt         |
185
--                          0.01uF        |
186
--  cap_gnd_o(1) o------------|(----------.
187
--                                        |
188
--                          Cfilt         |
189
--                          1300pF        |
190
--  cap_gnd_o(2) o------------|(----------.
191
--
192
--  In practice, R=270 ohms has been successfully used.
193
--  The Cfilt values are not exact, but the values shown worked well enough.
194
--
195
-- The two carrier signals are identical, except that the LSB is constantly driving
196
-- the heterodyne signal out, while the MSB is only driving the heterodyne signal
197
-- during times when the irig DC level pulse is high.  The cap_gnd_o outputs serves
198
-- to adjust the corner frequency of the RC low pass filter.  They are not pulsed
199
-- with any high frequency signals.  They simply apply ground or else high impedance,
200
-- thereby effectively bringing the Cfilt capacitors in and out of the circuit.
201
--
202
-- It is recommended to use the Cout capacitor in series with the output to provide
203
-- AC coupling of the signal, and some DC isolation for the FPGA.
204
--
205
-- This core uses a 1MHz clock enable pulse, to count the passage of time
206
-- and output a pulse stream which conforms to IRIG standard 200-04, for the
207
-- following IRIG formats:
208
--
209
--   First  letter: B (100 pps), A (1000 pps) or G (10000 pps)
210
--   First  digit : 0 = DC Level shift output (no modulation)
211
--   Second digit : 0 = No carrier
212
--   Third  digit : [0..7] (BCD plus combinations of Year, CF and SBS fields)
213
--
214
-- The core can generate its own 1 MHz clock enable by dividing the sys_clk
215
-- by an integer divisor.  If no integer divisor can be found, then then
216
-- perhaps an NCO should be used to come arbitrarily close to the desired
217
-- 1 MHz rate.  No NCO is currently provided, so if you run into this situation
218
-- you may have to buckle up, and start coding.
219
--
220
-- Keeping the time accurate over the longer term is accomplished by using
221
-- the pps_i input, a single pulse per second, one sys_clk in duration.
222
-- The pps_i input may be supplied by an external GPS module for accurate time,
223
-- although in the absence of GPS reference, the local system clock can be
224
-- used to create a somewhat less accurate reference.  An internal pps
225
-- generator creates a pps signal by counting one million of the 1 MHz clock
226
-- enable pulses, which is useful as a surrogate when there is no external pps
227
-- reference.  To use the external pps_i input, simply make "pps_ext_i" high.
228
--
229
-- All settings for the core are contained in read/write registers, which
230
-- have default values determined by generics.  In the case of a "code"
231
-- selection which includes straight binary seconds ("SBS"), the control
232
-- bits for the P8 and P9 portions of the output are not used, and the
233
-- internally calculated SBS field is used instead.
234
--
235
-- The year field also displaces control bits when it is selected.  The two
236
-- digit year is fully supported, and will increment properly at the end of
237
-- 23:59:59 on day 364. (No leap year).
238
--
239
-- For rates A and G, there is a tenths of a second BCD digit present in the
240
-- latter end of the P4 period.
241
--
242
-- For rate G, there is a hundredths of a second BCD digit present in the
243
-- former end of the P5 period.
244
--
245
-- If the year is desired, it can be placed in the proper position within
246
-- the control bits, as shown here...
247
--    Format B or A :
248
--      Year_1 (the most significant BCD digit) =>
249
--          control_1(0) & control_0(7 downto 5)
250
--          control_0(4) remains '0'
251
--      Year_0 (the least significant BCD digit) =>
252
--          control_0(3 downto 0)
253
--
254
--    Format G :
255
--      Year_1 (the most significant BCD digit) =>
256
--          control_2(1 downto 0) & control_1(7 downto 6)
257
--          control_1(5) remains '0'
258
--      Year_0 (the least significant BCD digit) =>
259
--          control_1(4 downto 1)
260
--
261
-- Bus interface writes to the registers cannot be given higher priority
262
-- than internal updates for timekeeping based on pps_i, since bus writes
263
-- only affect one value, while timekeeping can occasionally alter all of
264
-- them.  Thus corrupt and undesired behavior could result if bus writes
265
-- were to override the timekeeping update values.  Therefore, the ack_o
266
-- signal is used to ensure that the timekeeping is finished before bus
267
-- writes take place.
268
--
269
-- The registers are summarized as follows:
270
--
271
-- Address   Structure      Function
272
-- -------   ----------   ----------------------------------------
273
--   0x0     [**YY*DDD]    BCD Year, Day of Year (Day is 1..365 or 366)
274
--   0x1     [**HHMMSS]    BCD Hours, Minutes, Seconds. (*=unused nibble)
275
--   0x2     [CCCCCCCC]    Control Bits (31:0)
276
--   0x3     [**CCCCCC]    Control Bits (44:32)
277
--   0x4     (2:0)         Rate Setting (See Notes below)
278
--   0x5     (2:0)         Codes Used [ctrl,SBS,year]
279
--   0x6     (2:0)         Carrier Modulation Frequency
280
--
281
-- Notes:
282
--
283
--   Register 0x0 (Leap Year, Year, Day of Year)
284
--
285
--    Bit 28 controls the leap year setting:
286
--    This register currently has only a single bit for setting leap year
287
--    behavior.  When the leap year bit is set, the day field advances to 366
288
--    before rolling over back to 1.  When the bit is clear, the day field
289
--    to 1 after the 365 day value.  Note that the DDD field can be written
290
--    with values higher than 366, but it will immediately roll over.
291
--
292
--   Register 0x4 (Rate)
293
--    000 => (Use external input for Rate/Form/Carrier/Codes)*
294
--    001 => IRIG-B (1 second updates)
295
--    010 => IRIG-A (0.1 second updates)
296
--    011 => IRIG-G (0.01 second updates)
297
--    100 => NASA 36-bit (WWV) time code
298
--
299
--    The "rate_i" input is used along with the register setting, in such
300
--    a way that the register Rate value being zero, causes the "rate_i" and
301
--    "codes_i" inputs to be used.  If, however, the register Rate is set to
302
--    a non-zero value, then the external input is ignored.
303
--
304
--    When external rate input is used, a setting of "000" results
305
--    in IRIG-B (1 second updates.)  All other settings remain
306
--    as outlined above.
307
--
308
--   Register 0x5 (Codes)
309
--    Each bit position, when set, activates the use of a specific field
310
--    within the output, according to the form:
311
--
312
--       [ctrl,SBS,year]
313
--
314
--    Where ctrl = Control field
315
--           SBS = Straight Binary Seconds
316
--          year = Year field
317
--
318
--     If year and control are both selected, then the year is used in its 
319
--     appropriate spot, preventing the control bits there from being used.
320
--
321
--     For Rate "G", the hundredths of a second field occludes the first four
322
--     control bits, so that they are not used.  The hundredths of a second field
323
--     is encoded in the register area for "year" since year is not used in
324
--     Rate "G"
325
--
326
--   Register 0x6 (Carrier)
327
--   000 => No carrier (DC Level Shift)
328
--   001 => 100 Hz / 10 ms. resolution
329
--   010 => 1 kHz / 1 millisecond resolution
330
--   011 => 10 kHz / 100 microsecond resolution
331
--   100 => 100 kHz / 10 microsecond resolution
332
--
333
-- Internal logic takes the day, hour, minute and second values and derives
334
-- a value for the SBS field (Straight Binary Second).
335
--
336
-- The generics used as defaults for the registers are the full width of the
337
-- data bus.  However, only those bits which are actually present in the actual
338
-- registers are used.
339
 
340
library IEEE;
341
use IEEE.STD_LOGIC_1164.ALL;
342
use IEEE.NUMERIC_STD.ALL;
343
use IEEE.MATH_REAL.ALL;
344
 
345
library work;
346
use work.convert_pack.all;
347
 
348
entity irig_time_generator is
349
    generic (
350
      SYS_CLK_RATE : real := 40000000.0; -- Needed for carrier generation
351
      DEF_R_0 : unsigned(31 downto 0) := str2u("00100123",32);
352
      DEF_R_1 : unsigned(31 downto 0) := str2u("00000000",32);
353
      DEF_R_2 : unsigned(31 downto 0) := str2u("00000000",32);
354
      DEF_R_3 : unsigned(31 downto 0) := str2u("00000000",32);
355
      DEF_R_4 : unsigned(31 downto 0) := str2u("00000001",32);
356
      DEF_R_5 : unsigned(31 downto 0) := str2u("00000007",32);
357
      DEF_R_6 : unsigned(31 downto 0) := str2u("00000002",32);
358
      DEF_R_Z : unsigned(31 downto 0) := str2u("00000000",32)  -- Value returned for nonexistent registers
359
    );
360
    port (
361
 
362
      sys_rst_n   : in std_logic;
363
      sys_clk     : in std_logic;
364
      sys_clk_en  : in std_logic;
365
 
366
      -- Bus interface
367
      adr_i       : in  unsigned(3 downto 0);
368
      sel_i       : in  std_logic;
369
      we_i        : in  std_logic;
370
      dat_i       : in  unsigned(31 downto 0);
371
      dat_o       : out unsigned(31 downto 0);
372
      ack_o       : out std_logic;
373
 
374
      -- Pulse per second time reference
375
      pps_i       : in  std_logic;
376
      pps_ext_i   : in  std_logic;
377
 
378
      -- IRIG select inputs
379
      -- Active only when register 0xC set to zero.
380
      rate_i      : in  unsigned(2 downto 0); -- Rates    : 0,1=>B, 2=>A, 3=>G, 4=> NASA 36-bit
381
      carrier_i   : in  unsigned(2 downto 0); -- Carriers : 0=DCLS, 1=100Hz, 2=1kHz, 3=10kHz, 4=100kHz
382
      codes_i     : in  unsigned(2 downto 0); -- Codes    : (2)=> control, (1)=> SBS, (0)=> year
383
 
384
      -- serial IRIG time stream, LVTTL level output
385
      time_dcl_o  : out std_logic;
386
      cap_gnd_o   : out unsigned(2 downto 0);
387
      ref_o       : out std_logic;
388
      carrier_o   : out unsigned(1 downto 0)
389
    );
390
end irig_time_generator;
391
 
392
 
393
architecture beh of irig_time_generator is
394
 
395
-- Constants
396
constant DDS_WIDTH          : integer := 32;
397
constant INCREMENT_600KHZ   : unsigned(DDS_WIDTH-1 downto 0) := to_unsigned(integer(2.0**DDS_WIDTH/SYS_CLK_RATE*600000.0),DDS_WIDTH);
398
constant INCREMENT_510KHZ   : unsigned(DDS_WIDTH-1 downto 0) := to_unsigned(integer(2.0**DDS_WIDTH/SYS_CLK_RATE*510000.0),DDS_WIDTH);
399
constant INCREMENT_501KHZ   : unsigned(DDS_WIDTH-1 downto 0) := to_unsigned(integer(2.0**DDS_WIDTH/SYS_CLK_RATE*501000.0),DDS_WIDTH);
400
constant INCREMENT_500100HZ : unsigned(DDS_WIDTH-1 downto 0) := to_unsigned(integer(2.0**DDS_WIDTH/SYS_CLK_RATE*500100.0),DDS_WIDTH);
401
 
402
constant DIVISOR_PPS     : real :=  1000000.0;
403
constant DIVISOR_1MHZ    : real :=  SYS_CLK_RATE/DIVISOR_PPS;
404
constant DIV_COUNT_WIDTH : natural :=  bit_width(DIVISOR_1MHZ);
405
constant PPS_COUNT_WIDTH : natural :=  bit_width(DIVISOR_PPS);
406
constant SQF_BITS        : natural :=  4; -- square_fast frequency is Fsys_clk/2^(SQF_BITS+1)
407
 
408
-- Internal signal declarations
409
  -- State Machine
410
type FSM_STATE_TYPE is (RESET, IDLE, P0_REF, PR_REF, P0_DOUT, P1_PLUS_REF, P1_PLUS_DOUT);
411
signal fsm_state        : FSM_STATE_TYPE;
412
signal fsm_ack          : std_logic;
413
 
414
-- Related to SBS calculation
415
  -- SBS Calculation State
416
type SBS_STATE_TYPE is (RESET, IDLE, TIMEKEEPING_DELAY, SBS_10H, SBS_1H, SBS_10M, SBS_1M, SBS_10S);
417
signal sbs_state        : SBS_STATE_TYPE;
418
 
419
-- Related to Registers
420
 
421
signal leap_year        : std_logic;
422
 
423
signal secs_0           : unsigned(3 downto 0);
424
signal secs_1           : unsigned(2 downto 0);
425
signal mins_0           : unsigned(3 downto 0);
426
signal mins_1           : unsigned(2 downto 0);
427
signal hours_0          : unsigned(3 downto 0);
428
signal hours_1          : unsigned(1 downto 0);
429
signal days_0           : unsigned(3 downto 0);
430
signal days_1           : unsigned(3 downto 0);
431
signal days_2           : unsigned(1 downto 0);
432
signal years_0          : unsigned(3 downto 0);
433
signal years_1          : unsigned(3 downto 0);
434
signal tenths           : unsigned(3 downto 0);
435
signal hundredths       : unsigned(3 downto 0);
436
signal micros           : unsigned(13 downto 0); -- Microsecond count
437
 
438
signal sbs              : unsigned(16 downto 0);
439
signal sbs_next         : unsigned(16 downto 0);
440
signal sbs_add_count    : unsigned(3 downto 0);
441
 
442
signal control          : unsigned(44 downto 0);
443
signal reg_rate         : unsigned(2 downto 0);
444
signal reg_carrier      : unsigned(2 downto 0);
445
signal reg_codes        : unsigned(2 downto 0);
446
signal irig_rate        : unsigned(2 downto 0);
447
signal irig_carrier     : unsigned(2 downto 0);
448
signal irig_codes       : unsigned(2 downto 0);
449
 
450
-- Related to pulse per second and 1MHz clock enable
451
signal clk_en_1mhz      : std_logic;
452
signal clk_div_count    : unsigned(DIV_COUNT_WIDTH-1 downto 0);
453
signal pps              : std_logic;
454
signal pps_local        : std_logic;
455
signal pps_count        : unsigned(PPS_COUNT_WIDTH-1 downto 0);
456
 
457
-- Related to generation of the output pulse stream
458
signal pulse_clk_count  : unsigned(9 downto 0); -- Divides by factor of 10, 100 or 1000
459
signal pulse_clk_en     : std_logic;
460
signal pulse_sub_count  : unsigned(3 downto 0); -- Advances when pulse_clk_en=1
461
signal sub_count_nine   : std_logic;
462
signal p_count          : unsigned(3 downto 0); -- Advances when sub_count=9 and pulse_sr_count=9
463
signal cycle_count      : unsigned(6 downto 0); -- Counts output cycles during the current second.
464
signal pulse_sr         : unsigned(8 downto 0);
465
signal pulse_sr_count   : unsigned(3 downto 0); -- Advances when sub_count=9
466
signal sr_load_bus      : unsigned(8 downto 0);
467
signal p0               : unsigned(8 downto 0); -- sr load value
468
signal p1               : unsigned(8 downto 0); -- sr load value
469
signal p2               : unsigned(8 downto 0); -- sr load value
470
signal p3               : unsigned(8 downto 0); -- sr load value
471
signal p4               : unsigned(8 downto 0); -- sr load value
472
signal p5               : unsigned(8 downto 0); -- sr load value
473
signal p6               : unsigned(8 downto 0); -- sr load value
474
signal p7               : unsigned(8 downto 0); -- sr load value
475
signal p8               : unsigned(8 downto 0); -- sr load value
476
signal p9               : unsigned(8 downto 0); -- sr load value
477
signal pulse_extent     : unsigned(3 downto 0);
478
signal time_pulses      : std_logic;
479
 
480
-- Related to generation of the sinewave carrier
481
signal dds_sync          : std_logic;
482
signal sqf_count         : unsigned(SQF_BITS-1 downto 0);
483
signal square_fast       : std_logic;
484
signal square_500khz     : std_logic;
485
signal heterodyne        : std_logic;
486
signal modulated_carrier : std_logic;
487
signal phase_dds         : unsigned(DDS_WIDTH-1 downto 0);
488
signal increment_dds     : unsigned(DDS_WIDTH-1 downto 0);
489
 
490
-----------------------------------------------------------------------------
491
begin
492
 
493
  --------------------------
494
  -- Clock enable generation
495
  -- This creates output pulses at 1MHz rate, for use by the IRIG time generator
496
  -- It also creates a 1 Hz "pps" signal in lieu of such a signal from a GPS reference.
497
  clk_en_1mhz_proc: Process(sys_rst_n,sys_clk)
498
  begin
499
    if (sys_rst_n = '0') then
500
      clk_div_count <= (others=>'0');
501
--      pps_count     <= (others=>'0');
502
      pps_count     <= to_unsigned(integer(DIVISOR_PPS-200.0),pps_count'length); -- For better simulation
503
      clk_en_1mhz   <= '0';
504
      pps_local     <= '0';
505
    elsif (sys_clk'event AND sys_clk='1') then
506
      if (sys_clk_en='1') then
507
        clk_en_1mhz <= '0';  -- Default
508
        pps_local   <= '0';  -- Default
509
        if (clk_div_count=integer(DIVISOR_1MHZ-1.0)) then
510
          clk_div_count <= (others=>'0');
511
          clk_en_1mhz   <= '1';
512
          if (pps_count=integer(DIVISOR_PPS-1.0)) then
513
            pps_count   <= (others=>'0');
514
            pps_local   <= '1';
515
          else
516
            pps_count   <= pps_count+1;
517
          end if;
518
        else
519
          clk_div_count <= clk_div_count+1;
520
        end if;
521
      end if; -- sys_clk_en
522
    end if; -- sys_clk
523
  end process;
524
 
525
-- Select which PPS signal to use
526
pps <= pps_local when pps_ext_i='0' else pps_i;
527
 
528
-- Select which settings to use
529
irig_rate    <= reg_rate    when (reg_rate/=0) else rate_i;
530
irig_carrier <= reg_carrier when (reg_rate/=0) else carrier_i;
531
irig_codes   <= reg_codes   when (reg_rate/=0) else codes_i;
532
 
533
-- Register read mux
534
with (adr_i) select
535
  dat_o <=
536
    "000" & leap_year & "0000" & years_1 & years_0 & "000000" & days_2 & days_1 & days_0 when "0000",  --  "0";
537
    "0000000000" & hours_1 & hours_0 & "0" & mins_1 & mins_0 & "0" & secs_1 & secs_0     when "0001",  --  "1";
538
    control(31 downto 0)                                                                 when "0010",  --  "2";
539
    "0000000000000000000" & control(44 downto 32)                                        when "0011",  --  "3";
540
    u_resize(reg_rate,32)                                                                when "0100",  --  "4";
541
    u_resize(reg_carrier,32)                                                             when "0101",  --  "5";
542
    u_resize(reg_codes,32)                                                               when "0110",  --  "6";
543
    DEF_R_Z                                                                              when others;   -- Default
544
 
545
-- Create acknowledge signal
546
ack_o <= '1' when (sel_i='1' and we_i='0') else fsm_ack;
547
 
548
 
549
  --------------------------
550
  -- Heterodyning Signals
551
  -- The idea here is to multiply or "mix" two "squarewave" sequences
552
  -- by using an XOR gate, to produce a sum and difference frequency.
553
  -- The sum frequency should be easily filtered out by an RC filter
554
  -- external to the FPGA.
555
  proc_square: Process(sys_rst_n,sys_clk)
556
  begin
557
    if (sys_rst_n = '0') then
558
      square_fast <= '0';
559
      sqf_count   <= (others=>'0');
560
    elsif (sys_clk'event AND sys_clk='1') then
561
      if (sys_clk_en='1') then
562
        sqf_count <= sqf_count+1;
563
        if (sqf_count=0) then
564
          square_fast <= not square_fast;
565
        end if;
566
      end if;
567
    end if;
568
  end process;
569
  proc_square_500khz: Process(sys_rst_n,sys_clk)
570
  begin
571
    if (sys_rst_n = '0') then
572
      square_500khz <= '0';
573
    elsif (sys_clk'event AND sys_clk='1') then
574
      if (sys_clk_en='1') then
575
        if (clk_en_1mhz='1') then
576
          square_500khz <= not square_500khz;
577
        end if;
578
      end if;
579
    end if;
580
  end process;
581
  proc_dds: Process(sys_rst_n,sys_clk)
582
  begin
583
    if (sys_rst_n = '0') then
584
      phase_dds <= (phase_dds'length-2=>'1',others=>'0'); -- Set to 1/4 phase point (sinewave "zero" output)
585
    elsif (sys_clk'event AND sys_clk='1') then
586
      if (sys_clk_en='1') then
587
        if (dds_sync='1') then
588
          phase_dds <= (phase_dds'length-2=>'1',others=>'0'); -- Set to 1/4 phase point (sinewave "zero" output)
589
        else
590
          phase_dds <= phase_dds + increment_dds;
591
        end if;
592
      end if; -- sys_clk_en
593
    end if; -- sys_clk
594
  end process;
595
  increment_dds <= INCREMENT_600KHZ when irig_carrier=4 else
596
                   INCREMENT_510KHZ when irig_carrier=3 else
597
                   INCREMENT_501KHZ when irig_carrier=2 else
598
                   INCREMENT_500100HZ;
599
  heterodyne <= square_500khz xor phase_dds(31); -- A sort of digital signal multiplication
600
  modulated_carrier <= heterodyne when time_pulses='1' else square_fast; -- Output midpoint when pulse is low
601
  carrier_o(0) <= '0' when irig_carrier=0 else heterodyne;
602
  carrier_o(1) <= time_pulses when irig_carrier=0 else
603
                  modulated_carrier;
604
  ref_o        <= '0' when irig_carrier=0 else square_fast; -- Used as a "midpoint" voltage reference
605
 
606
  --------------------------
607
  -- Setting for output filter capacitor.
608
  -- The idea here is to ground the particular capacitor needed to
609
  -- low pass filter the output.  Three different outputs are provided.
610
  cap_gnd_o     <= "ZZZ" when irig_carrier=0 or irig_carrier=1 else
611
                   "Z00" when irig_carrier=2 else  -- Two capacitors in parallel
612
                   "Z0Z" when irig_carrier=3 else
613
                   "0ZZ";
614
 
615
-- This process is where the different BCD values are compiled into
616
-- a "straight binary seconds" (SBS) field.
617
-- The calculations are done in a multiply accumulate fashion (MAC)
618
-- wherein the multiply is implemented as a series of additions.
619
-- An SBS accumulator stores the intermediate calculation total,
620
-- while a state variable tracks the current stage of calculation,
621
-- beginning at "tens of hours" and finishing at "seconds."
622
-- All the adds performed here are unsigned...
623
sbs_proc : process(sys_clk,sys_rst_n)
624
begin
625
  if (sys_rst_n='0') then
626
    sbs_state <= RESET;
627
    sbs <= (others=>'0');
628
    sbs_next <= (others=>'0');
629
    sbs_add_count <= (others=>'0');
630
  elsif (sys_clk'event and sys_clk='1') then
631
    if (sys_clk_en='1') then
632
      case (sbs_state) is
633
        when RESET =>
634
          sbs_state <= IDLE;
635
        when IDLE  =>
636
          if (pps='1') then
637
            sbs_state <= TIMEKEEPING_DELAY;
638
          end if;
639
        -- This state waits for changes to the BCD time values to propagate,
640
        -- so they will be stable before this process begins calculation of
641
        -- the SBS field.
642
        when TIMEKEEPING_DELAY =>
643
          sbs <= (others=>'0'); -- Clear accumulator
644
          sbs_next <= u_resize(hours_1,sbs'length);
645
          sbs_add_count <= "1010"; -- Multiply by 10
646
          sbs_state <= SBS_10H;
647
        -- The following series of states performs multiply and accumulate
648
        -- calculations to generate the SBS value.
649
        when SBS_10H =>
650
          if (sbs_add_count=0) then
651
            sbs <= (others=>'0');
652
            sbs_next <= sbs + u_resize(hours_0,sbs'length);
653
            sbs_add_count <= "0110"; -- Multiply by 6
654
            sbs_state <= SBS_1H;
655
          else
656
            sbs <= sbs + sbs_next;
657
            sbs_add_count <= sbs_add_count-1;
658
          end if;
659
        when SBS_1H  =>
660
          if (sbs_add_count=0) then
661
            sbs <= (others=>'0');
662
            sbs_next <= sbs + u_resize(mins_1,sbs'length);
663
            sbs_add_count <= "1010"; -- Multiply by 10
664
            sbs_state <= SBS_10M;
665
          else
666
            sbs <= sbs + sbs_next;
667
            sbs_add_count <= sbs_add_count-1;
668
          end if;
669
        when SBS_10M =>
670
          if (sbs_add_count=0) then
671
            sbs <= (others=>'0');
672
            sbs_next <= sbs + u_resize(mins_0,sbs'length);
673
            sbs_add_count <= "0110"; -- Multiply by 6
674
            sbs_state <= SBS_1M;
675
          else
676
            sbs <= sbs + sbs_next;
677
            sbs_add_count <= sbs_add_count-1;
678
          end if;
679
        when SBS_1M  =>
680
          if (sbs_add_count=0) then
681
            sbs <= (others=>'0');
682
            sbs_next <= sbs + u_resize(secs_1,sbs'length);
683
            sbs_add_count <= "1010"; -- Multiply by 10
684
            sbs_state <= SBS_10S;
685
          else
686
            sbs <= sbs + sbs_next;
687
            sbs_add_count <= sbs_add_count-1;
688
          end if;
689
        when SBS_10S =>
690
          if (sbs_add_count=0) then
691
            sbs <= sbs + u_resize(secs_0,sbs'length); -- Add seconds (no more multiplication.)
692
            sbs_state <= IDLE;
693
          else
694
            sbs <= sbs + sbs_next;
695
            sbs_add_count <= sbs_add_count-1;
696
          end if;
697
        when others =>
698
          null;
699
      end case;
700
    end if; -- sys_clk_en
701
  end if; -- sys_clk
702
end process;
703
 
704
-- sr load values
705
  p0 <= "00000" & secs_0 when (irig_rate=4) else
706
        "0" & secs_1 & "0" & secs_0;
707
  p1 <= "000000" & secs_1 when (irig_rate=4) else
708
        "0" & mins_1 & "0" & mins_0;
709
  p2 <= "00000" & mins_0 when (irig_rate=4) else
710
        "00" & hours_1 & "0" & hours_0;
711
  p3 <= "000000" & mins_1 when (irig_rate=4) else
712
        days_1 & "0" & days_0;
713
  p4 <= "00000" & hours_0 when (irig_rate=4) else
714
        "0000000" & days_2 when (irig_rate=0 or irig_rate=1) else
715
        tenths & "000" & days_2;
716
  p5 <= "0000000" & hours_1 when (irig_rate=4) else
717
        control(8 downto 4) & hundredths when (irig_rate=3) else
718
        years_1 & '0' & years_0 when (irig_codes(2)='1') else
719
        control(8 downto 0) when (irig_codes(0)='1') else
720
        "000000000";
721
  p6 <= "00000" & days_0 when (irig_rate=4) else
722
        years_1 & '0' & years_0 when (irig_rate=3 and irig_codes(2)='1') else
723
        control(17 downto 9) when (irig_codes(0)='1') else
724
        "000000000";
725
  p7 <= "00000" & days_1 when (irig_rate=4) else
726
        control(26 downto 18) when (irig_codes(0)='1') else
727
        "000000000";
728
  p8 <= "0000000" & days_2 when (irig_rate=4) else
729
        sbs(8 downto 0) when (irig_codes(1)='1') else
730
        control(35 downto 27) when (irig_codes(0)='1') else
731
        "000000000";
732
  p9 <= "111110000" when (irig_rate=4) else
733
        '0' & sbs(16 downto 9) when (irig_codes(1)='1') else
734
        control(44 downto 36) when (irig_codes(0)='1') else
735
        "000000000";
736
 
737
-- sr_load_bus mux
738
with (p_count) select
739
  sr_load_bus <=
740
    p0 when "0000",  --  "0";
741
    p1 when "0001",  --  "1";
742
    p2 when "0010",  --  "2";
743
    p3 when "0011",  --  "3";
744
    p4 when "0100",  --  "4";
745
    p5 when "0101",  --  "5";
746
    p6 when "0110",  --  "6";
747
    p7 when "0111",  --  "7";
748
    p8 when "1000",  --  "8";
749
    p9 when "1001",  --  "9";
750
    "000000000" when others;   -- Not needed for synthesis...
751
 
752
-- Generate a clock enable for advancing the output carrier state
753
-- The rate of this pulse is determined by the selected IRIG rate.
754
-- The rate has been selected to allow 10 digital samples per cycle
755
-- of carrier output.
756
-- 
757
-- Selected Carrier    carrier_clk rate   Carrier Frequency
758
-- ----------------    ----------------   -----------------
759
--       000              DC Level             -None-
760
--       001                 1 kHz            100  Hz
761
--       010                10 kHz              1 kHz
762
--       011               100 kHz             10 kHz
763
--       100                 1 MHz            100 kHz
764
--
765
-- Generate a clock enable for advancing the output pulse state
766
-- The rate of this pulse is determined by the selected IRIG rate.
767
-- The rate has been selected to allow output pulse duty cycle
768
-- to be set in increments of 1/10th the total pulse time.
769
-- 
770
-- Selected Rate    pulse_clk rate   Pulse Frequency
771
-- -------------    --------------   -----------------
772
--     000                1 kHz           100 Hz
773
--     001 (B)            1 kHz           100 Hz
774
--     010 (A)           10 kHz             1 kHz
775
--     011 (G)          100 kHz            10 kHz
776
--     100 (NASA 36)      1 kHz           100 Hz
777
 
778
pulse_clk_proc : process(sys_clk,sys_rst_n)
779
begin
780
  if (sys_rst_n='0') then
781
    pulse_clk_count <= (others=>'0');
782
  elsif (sys_clk'event and sys_clk='1') then
783
    if (sys_clk_en='1') then
784
      if (pulse_clk_en='1') then
785
        pulse_clk_count <= (others=>'0');
786
      elsif (clk_en_1mhz='1') then
787
        pulse_clk_count <= pulse_clk_count + 1;
788
      end if;
789
    end if; -- sys_clk_en
790
  end if; -- sys_clk
791
end process;
792
pulse_clk_en <= '1' when (clk_en_1mhz='1' and (irig_rate=0 or irig_rate=1 or irig_rate=4) and pulse_clk_count=999) else
793
                '1' when (clk_en_1mhz='1' and irig_rate=2 and pulse_clk_count=99) else
794
                '1' when (clk_en_1mhz='1' and irig_rate=3 and pulse_clk_count=9) else
795
                '0';
796
 
797
-- This is the state machine.
798
-- It handles updates to the timekeeping registers, based on the "pps" signal,
799
-- and creates the output pulse stream.
800
fsm_proc: process(sys_clk, sys_rst_n)
801
begin
802
  if (sys_rst_n='0') then
803
    fsm_state        <= RESET;   -- asynchronous reset
804
    leap_year        <= '0';
805
    micros           <= (others=>'0');
806
    hundredths       <= (others=>'0');
807
    tenths           <= (others=>'0');
808
    years_1          <= DEF_R_0(23 downto 20);
809
    years_0          <= DEF_R_0(19 downto 16);
810
    days_2           <= DEF_R_0(9 downto 8);
811
    days_1           <= DEF_R_0(7 downto 4);
812
    days_0           <= DEF_R_0(3 downto 0);
813
    hours_1          <= DEF_R_1(21 downto 20);
814
    hours_0          <= DEF_R_1(19 downto 16);
815
    mins_1           <= DEF_R_1(14 downto 12);
816
    mins_0           <= DEF_R_1(11 downto 8);
817
    secs_1           <= DEF_R_1(6 downto 4);
818
    secs_0           <= DEF_R_1(3 downto 0);
819
    control          <= DEF_R_3(12 downto 0) & DEF_R_2;
820
    reg_rate         <= DEF_R_4(2 downto 0);
821
    reg_carrier      <= DEF_R_5(2 downto 0);
822
    reg_codes        <= DEF_R_6(2 downto 0);
823
 
824
    p_count          <= (others=>'0');
825
    pulse_sr         <= (others=>'0');
826
    pulse_sr_count   <= to_unsigned(1,pulse_sr_count'length); -- To avoid sending out an initial stream of reference pulses while waiting for pps_local
827
    pulse_sub_count  <= (others=>'0');
828
    cycle_count      <= (others=>'0');
829
 
830
    fsm_ack          <= '0';
831
    dds_sync         <= '0';
832
 
833
  elsif (sys_clk'event and sys_clk='1') then
834
    if (sys_clk_en='1') then
835
 
836
      -- Default values
837
      fsm_ack <= '0';
838
      dds_sync <= '0';
839
 
840
      -- Handle timekeeping for fractions of seconds, based on 1MHz clock enable.
841
      -- These updates are overridden by pps, which resets all the time count values.
842
      if (clk_en_1mhz='1') then
843
        if (micros=9999) then
844
          micros <= (others=>'0');
845
          if (hundredths=9) then
846
            tenths <= tenths+1;
847
            hundredths <= (others=>'0');
848
            if (tenths=9) then
849
              -- No need to increment seconds here.  pps does that.
850
              tenths <= (others=>'0');
851
            else
852
              tenths <= tenths+1;
853
            end if;
854
          else
855
            hundredths <= hundredths+1;
856
          end if;
857
        else
858
          micros <= micros+1;
859
        end if;
860
      end if;
861
 
862
      -- Handle the timekeeping items, based on pps
863
      if (pps='1') then
864
        secs_0 <= secs_0+1;
865
        tenths <= (others=>'0');
866
        hundredths <= (others=>'0');
867
        micros <= (others=>'0');
868
        if (secs_0=9) then
869
          secs_1 <= secs_1+1;
870
          secs_0 <= (others=>'0');
871
          if (secs_1=5) then
872
            mins_0 <= mins_0+1;
873
            secs_1 <= (others=>'0');
874
            if (mins_0=9) then
875
              mins_1 <= mins_1+1;
876
              mins_0 <= (others=>'0');
877
              if (mins_1=5) then
878
                hours_0 <= hours_0+1;
879
                mins_1  <= (others=>'0');
880
                if (hours_1=2 and hours_0=3) then
881
                  days_0  <= days_0+1;
882
                  hours_1 <= (others=>'0');
883
                  hours_0 <= (others=>'0');
884
                  if (days_2>=3 and days_1>=6 and ((days_0>=5 and leap_year='0') or (days_0>=6 and leap_year='1'))) then
885
                    years_0 <= years_0+1;
886
                    days_0  <= "0001";
887
                    days_1  <= (others=>'0');
888
                    days_2  <= (others=>'0');
889
                    if (years_0=9) then
890
                      years_1 <= years_1+1;
891
                      years_0 <= (others=>'0');
892
                      if (years_1=9) then
893
                        years_1 <= (others=>'0');
894
                      end if;
895
                    end if;
896
                  elsif (days_0=9) then
897
                    days_1 <= days_1+1;
898
                    days_0 <= (others=>'0');
899
                    if (days_1=9) then
900
                      days_2 <= days_2+1;
901
                      days_1 <= (others=>'0');
902
                    end if;
903
                  end if;
904
                elsif (hours_0=9) then
905
                  hours_1 <= hours_1+1;
906
                  hours_0 <= (others=>'0');
907
                end if;
908
              end if;
909
            end if;
910
          end if;
911
        end if;
912
      elsif (sel_i='1' and we_i='1') then -- Handle bus writes to registers
913
      -- Cannot do this during pps, since this only writes one value, while
914
      -- pps timekeeping can potentially affect all the values.  Therefore,
915
      -- generate an acknowledge signal for the register writes... so that
916
      -- incoming bus cycles are extended when they collide with pps...
917
        fsm_ack <= '1';
918
        case (adr_i) is
919
          when "0000" =>
920
            leap_year <= dat_i(28);
921
            years_1 <= dat_i(23 downto 20);
922
            years_0 <= dat_i(19 downto 16);
923
            days_2  <= dat_i(9  downto 8);
924
            days_1  <= dat_i(7  downto 4);
925
            days_0  <= dat_i(3  downto 0);
926
          when "0001" =>
927
            hours_1 <= dat_i(21 downto 20);
928
            hours_0 <= dat_i(19 downto 16);
929
            mins_1  <= dat_i(14 downto 12);
930
            mins_0  <= dat_i(11 downto 8);
931
            secs_1  <= dat_i(6  downto 4);
932
            secs_0  <= dat_i(3  downto 0);
933
          when "0010" =>
934
            control(31 downto 0) <= dat_i;
935
          when "0011" =>
936
            control(44 downto 32) <= dat_i(12 downto 0);
937
          when "0100" =>
938
            reg_rate <= dat_i(2 downto 0);
939
          when "0101" =>
940
            reg_carrier <= dat_i(2 downto 0);
941
          when "0110" =>
942
            reg_codes <= dat_i(2 downto 0);
943
          when others => null;
944
        end case;
945
      end if;
946
 
947
      -- Handle pulse_sub_count
948
      if (pulse_clk_en='1') then
949
        if (sub_count_nine='1') then
950
          pulse_sub_count <= (others=>'0');
951
          dds_sync <= '1'; -- Set the carrier wave phase.
952
        else
953
          pulse_sub_count <= pulse_sub_count + 1;
954
        end if;
955
      end if;
956
 
957
      -- Handle state transitions
958
      case (fsm_state) is
959
 
960
        when RESET =>
961
          fsm_state <= IDLE;
962
 
963
        when IDLE =>
964
          if (pps='1') then
965
            fsm_state <= P0_REF;
966
            p_count   <= (others=>'0');
967
            cycle_count <= (others=>'0');
968
            pulse_sr_count <= (others=>'0');
969
          end if;
970
 
971
        when P0_REF =>
972
          if (pulse_clk_en = '1' and sub_count_nine='1') then
973
            pulse_sr_count <= pulse_sr_count+1;
974
            pulse_sr  <= sr_load_bus;
975
            if (irig_rate=4) then
976
              fsm_state <= P0_DOUT; -- No special reference pulse for IRIG 36-bit 
977
            else
978
              fsm_state <= PR_REF;
979
            end if;
980
          end if;
981
 
982
        when PR_REF =>
983
          if (pulse_clk_en = '1' and sub_count_nine='1') then
984
            pulse_sr_count <= pulse_sr_count+1;
985
            fsm_state <= P0_DOUT;
986
          end if;
987
 
988
        when P0_DOUT =>
989
          if (pulse_clk_en = '1' and sub_count_nine='1') then
990
            if (pulse_sr_count=9) then
991
              pulse_sr_count <= (others=>'0');
992
              p_count   <= p_count+1;
993
              fsm_state <= P1_PLUS_REF;
994
            else
995
              pulse_sr  <= '0' & pulse_sr(8 downto 1);
996
              pulse_sr_count <= pulse_sr_count+1;
997
            end if;
998
          end if;
999
 
1000
        when P1_PLUS_REF =>
1001
          if (pulse_clk_en = '1' and sub_count_nine='1') then
1002
            pulse_sr_count <= pulse_sr_count+1;
1003
            pulse_sr  <= sr_load_bus;
1004
            fsm_state <= P1_PLUS_DOUT;
1005
          end if;
1006
 
1007
        when P1_PLUS_DOUT =>
1008
          if (p_count=9 and pps='1') then -- pps should not occur before p_count=9 anyway...
1009
            fsm_state <= P0_REF;
1010
            p_count   <= (others=>'0');
1011
            cycle_count <= (others=>'0');
1012
            pulse_sr_count <= (others=>'0');
1013
          elsif (pulse_clk_en = '1' and sub_count_nine='1') then
1014
            if (pulse_sr_count=9) then
1015
              if (p_count=9) then
1016
                if (cycle_count=99 and irig_rate=3) then
1017
                  fsm_state <= IDLE;
1018
                elsif (cycle_count=9 and irig_rate=2) then
1019
                  fsm_state <= IDLE;
1020
                elsif (irig_rate=0 or irig_rate=1 or irig_rate=4) then
1021
                  fsm_state <= IDLE;
1022
                else
1023
                  cycle_count <= cycle_count+1;
1024
                  p_count     <= (others=>'0');
1025
                  pulse_sr_count <= (others=>'0');
1026
                  fsm_state <= P0_REF;
1027
                end if;
1028
              else
1029
                pulse_sr_count <= (others=>'0');
1030
                p_count   <= p_count+1;
1031
                pulse_sr  <= sr_load_bus;
1032
                fsm_state <= P1_PLUS_REF;
1033
              end if;
1034
            else
1035
              pulse_sr  <= '0' & pulse_sr(8 downto 1);
1036
              pulse_sr_count <= pulse_sr_count+1;
1037
            end if;
1038
          end if;
1039
 
1040
        --when others => 
1041
        --  fsm_state <= IDLE;
1042
      end case;
1043
 
1044
    end if; -- sys_clk_en
1045
  end if; -- sys_clk
1046
end process;
1047
 
1048
sub_count_nine <= '1' when (pulse_sub_count=9) else '0';
1049
 
1050
-- Form output pulses
1051
pulse_extent <= "0010" when (irig_rate=4 and pulse_sr_count=0 and p_count=0) else
1052
                "0101" when (irig_rate=4 and pulse_sr_count=0) else
1053
                "1000" when (pulse_sr_count=0 or fsm_state=PR_REF) else
1054
                "0101" when (pulse_sr(0)='1') else
1055
                "0010";
1056
 
1057
time_pulses <= '1' when (pulse_sub_count<pulse_extent) else '0';
1058
time_dcl_o  <= time_pulses;
1059
 
1060
end beh;
1061
 
1062
-------------------------------------------------------------------------------
1063
-- IRIG time pattern receiver-generator core
1064
-------------------------------------------------------------------------------
1065
--
1066
-- Author: John Clayton
1067
-- Date  : Apr.  4, 2012 Copied code from irig_time_generator.  Updated
1068
--                       description.
1069
--         Oct. 12, 2012 Added read only status registers, that allow
1070
--                       reporting of the lock condition, as well as the
1071
--                       "slow_err" and "fast_err" signals.  These were
1072
--                       not being used, and were being optimized away
1073
--                       ("pruned") at synthesis time.
1074
--         Apr. 30, 2013 Changed SYS_CLK_RATE from integer to real type.
1075
--         June 12, 2013 Changed low pass filter circuit topology, reducing
1076
--                       the number of "cap_gnd_o" lines.  Increased the
1077
--                       heterodyne input frequency from 500kHz to 2MHz.
1078
--         Oct. 10, 2013 Added the cdet_sel_o line to activate carrier
1079
--                       detect circuitry by register control.  Added leaky
1080
--                       integrator based lowpass filter from
1081
--                       "signal_conditioning_pack.vhd"
1082
--         Oct. 14, 2013 Cleaned up the design, changing the heterodyne
1083
--                       from 2MHz to 1MHz, eliminating "square_fast" and
1084
--                       using the 1MHz squarewave instead.  Eliminated
1085
--                       rx_phase and rx_dds_r1 by using "dds_squarewave"
1086
--                       module.  Changed rx_phase_incr -> rx_freq,
1087
--                       rx_dds_rising -> rx_1mhz_pulse.  Eliminated
1088
--                       "phase_dds" and "increment_dds" by using
1089
--                       dds_squarewave_phase_load module.
1090
--         Nov.  6, 2013 Divided receive and generate logic into separate
1091
--                       processes.  removed rx_lock_state state machine.
1092
--                       Eliminated rx_frame_count. Added rx_c_count,
1093
--                       gave priority to external PPS tracking over
1094
--                       carrier count PPS (IRIG) tracking.  Added
1095
--                       hdyne_base_square so that modulation frequency
1096
--                       is not "warped" or affected by the tracking
1097
--                       mechanism.  Added "set_time" signal.  Removed
1098
--                       rx_fast_err and rx_slow_err.  Removed rx_speed_sense.
1099
--         Dec.  6, 2013 Moved timecode gated pulse stretcher to a higher level,
1100
--                       and introduced time_cdet_i.  Now, "time_dcl_i" really
1101
--                       will be a DC-level timecode input, and "time_cdet_i"
1102
--                       is a squarewave at the carrier rate, during times
1103
--                       when cdet_sel_o is high.  Removed the 
1104
--                       "rx_pulse_stretcher" register bit.
1105
--         Dec.  9, 2013 Removed the time_dcl_i input, in favor of using the
1106
--                       time_cdet_i input always.  Added reg_time_lp lowpass
1107
--                       filter enable bit.
1108
--         Dec. 10, 2013 Revised the data detection logic to use "rx_lo_count"
1109
--                       and tested in hardware.  All seems to be working!
1110
--                       O, happy day!
1111
--         Dec. 11, 2013 Added rx_active_count to separate rx activity detect
1112
--                       from the data receive counter "rx_lo_count."  This
1113
--                       allows activity detect to be on edges, while the
1114
--                       data receive is on levels, thus allowing reception of
1115
--                       true dc-level timecode input data.
1116
--         Nov. 14, 2014 Reduced cdet_val from 4 bits down to 2 bits, and
1117
--                       provided it as an output signal for inclusion in
1118
--                       a status stream.
1119
--         Mar. 11, 2015 Added leap_year bit to register zero.  Prevented
1120
--                       day values higher than 366 from persisting.
1121
--
1122
-- Description
1123
-------------------------------------------------------------------------------
1124
-- This time pattern receiver is also a time pattern generator.
1125
-- More correctly, it is a time pattern generator which synchronizes itself to
1126
-- an incoming IRIG time signal.  Also, it provides parallel output of the
1127
-- current time so that timestamps may be taken when telemetered data is being
1128
-- sent to an archive.
1129
--
1130
-- Note that during playback, "one time" syncronization is used.  The IRIG
1131
-- time is set once, at the start of the playback, to match the first minor
1132
-- frame timestamp.  Thereafter, the freewheeling quartz crystal based
1133
-- IRIG time is sent out along with the playback data.  This is necessary
1134
-- because there is no feasible way to simultaneously adjust the IRIG time
1135
-- and the data playback rate so as to keep them both matched to some outside
1136
-- time reference, without introducing discontinuities in the IRIG time
1137
-- signal.
1138
--
1139
-- The incoming IRIG time signal is applied to a pulse period meter, which is
1140
-- built using a binary counter.  As the period is measured, decisions are 
1141
-- made about what type of bit is being encountered.  A period close to 20 
1142
-- counts is considered a zero, close to 50 counts, a one, and close to 80 
1143
-- counts, a "reference" pulse.  The counter is clocked by a pre-scaler so
1144
-- that it can measure the period in units of 100, 10 or 1 microsecond, for
1145
-- IRIG types B, A and G, respectively.  For rapid acquisition of all types,
1146
-- the default is to use units of 1 microsecond (type G.)  If the count 
1147
-- exceeds 85, then the prescaler is adjusted to provide units of 10 
1148
-- microseconds (type A).  If, once again, the count exceeds 85, then the 
1149
-- prescaler is adjusted to use units of 100 microseconds (type B.)  Since
1150
-- type B is the slowest supported type, if the count once again exceeds 85
1151
-- counts, then a "slow" error condition is flagged.
1152
--
1153
-- Similar converse adjustments are made for period measurements less than
1154
-- 10 counts.  If pulses of under 10 microseconds are found, then a "fast"
1155
-- error condition is flagged.
1156
--
1157
-- This unit is set up to receive DC Level data, and no attempt is made to
1158
-- use the modulating carrier for timing reference information.
1159
--
1160
-- The unit outputs DC Level data, plus a pair of digital outputs which
1161
-- contain a modulated carrier signal.  The modulated carrier frequency is the
1162
-- low-frequency heterodyne signal generated by digital mixing of a 1 MHz
1163
-- squarewave with a 1001 kHz (IRIG "B"), 1010 kHz (IRIG "A") or 1600 kHz
1164
-- (IRIG "G") squarewave, as generated by a DDS.  As such, the digital output
1165
-- needs to be low-pass filtered in order to cut down the amplitude of the
1166
-- high-frequency components.
1167
--
1168
-- In order to modulate the amplitude of the resulting low-pass filtered
1169
-- carrier over a 3:1 ratio (An AM modulation index of 0.5, meaning 50% modulation
1170
-- depth) a 2-bit R-2R resistor ladder network DAC is recommended for use outside
1171
-- the FPGA.  Also, in order to keep the modulated signal balanced, a reference
1172
-- voltage other than ground is used.  The reference voltage is nominally 1/2 of
1173
-- the FPGA VCCo supply voltage (3.3V * 0.5 = 1.65V).  The FPGA generates this
1174
-- reference voltage through the use of an output which provides a 50% duty cycle
1175
-- squarewave.  The schematic is as follows:
1176
--
1177
--                    R  1.65V  R     R
1178
--         ref_o o---/\/\--.--/\/\--/\/\--.
1179
--                         |              |
1180
--                    Cref - +            |
1181
--                    15uF -              |
1182
--                         |              |
1183
--                        GND             |
1184
--                              R     R   |
1185
--  carrier_o(0) o------------/\/\--/\/\--.
1186
--    (LSB)                               |
1187
--                                      R <
1188
--                                        <     Cout
1189
--                              R     R   |     4.7uF
1190
--  carrier_o(1) o------------/\/\--/\/\--.-----|(-------o AC coupled, Modulated Output
1191
--    (MSB)                               |
1192
--                          Cfilt         |
1193
--                          0.1uF         |
1194
--     cap_gnd_o o------.-----|(----------.
1195
--                      |                   
1196
--                      .-------------. 
1197
--                                    |
1198
--                          Cfilt     |
1199
--                          0.01uF    |
1200
--           GND o------------|(------.
1201
--
1202
--  In practice, R=270 ohms has been successfully used.
1203
--  The Cfilt values are not exact, but the values shown worked well enough.
1204
--
1205
-- The two carrier signals are identical, except that the LSB is constantly driving
1206
-- the heterodyne signal out, while the MSB is only driving the heterodyne signal
1207
-- during times when the irig DC level pulse is high, otherwise is outputs a 50%
1208
-- duty cycle signal, similar to ref_o.  The cap_gnd_o outputs serves to adjust
1209
-- the corner frequency of the RC low pass filter.  They are not pulsed with any
1210
-- high frequency signals.  They simply apply ground or else high impedance,
1211
-- thereby effectively bringing the Cfilt capacitors in and out of the circuit.
1212
--
1213
-- It is recommended to use the Cout capacitor in series with the output to provide
1214
-- AC coupling of the signal, and some DC isolation for the FPGA.
1215
--
1216
-- This core uses a 1MHz clock enable pulse, to count the passage of time
1217
-- and output a pulse stream which conforms to IRIG standard 200-04, for the
1218
-- following IRIG formats:
1219
--
1220
--   First  letter: B (100 pps), A (1000 pps) or G (10000 pps)
1221
--   First  digit : 0 = DC Level shift output (no modulation)
1222
--   Second digit : 0 = No carrier
1223
--   Third  digit : [0..7] (BCD plus combinations of Year, CF and SBS fields)
1224
--
1225
-- The core generates its own 1 MHz clock enable by using an NCO, with a phase
1226
-- accumulator register whose size is set by generics.  When locked to an 
1227
-- incoming timecode signal, the "integrated residual" (a.k.a. residusum)
1228
-- is checked and adjusted each second in order to "warp" the timebase rate
1229
-- to match the incoming timecode signal.  In this way, the timecode
1230
-- regenerator tracks variations in the timing of the incoming IRIG signal.
1231
--
1232
-- When not locked to an incoming timecode signal, there is an alternate method
1233
-- for keeping the time accurate over the longer term, namely the use of
1234
-- the pps_i input, a single pulse per second, detected on its rising edge.
1235
-- The pps_i input may be supplied by an external GPS module for accurate time.
1236
-- In the absence of incoming IRIG time signal or outside GPS reference, the 
1237
-- local quartz crystal based system clock is left to "freewheel" on its own,
1238
-- which inevitably results in the buildup of inaccuracies in timekeeping,
1239
-- depending on temperature, aging and physical handling of the quartz crystal
1240
-- itself.
1241
--
1242
-- All settings for the core are contained in read/write registers, which
1243
-- have default values determined by generics.  In the case of a "code"
1244
-- selection which includes straight binary seconds ("SBS"), the control
1245
-- bits for the P8 and P9 portions of the output are not used, and the
1246
-- internally calculated SBS field is used instead.
1247
--
1248
-- The year field also displaces control bits when it is selected.  The two
1249
-- digit year is fully supported, and will increment properly at the end of
1250
-- 23:59:59 on day 364. (No leap year).
1251
--
1252
-- For rates A and G, there is a tenths of a second BCD digit present in the
1253
-- latter end of the P4 period.
1254
--
1255
-- For rate G, there is a hundredths of a second BCD digit present in the
1256
-- former end of the P5 period.
1257
--
1258
-- If the year is desired, it can be placed in the proper position within
1259
-- the control bits, as shown here...
1260
--    Format B or A :
1261
--      Year_1 (the most significant BCD digit) =>
1262
--          control_1(0) & control_0(7 downto 5)
1263
--          control_0(4) remains '0'
1264
--      Year_0 (the least significant BCD digit) =>
1265
--          control_0(3 downto 0)
1266
--
1267
--    Format G :
1268
--      Year_1 (the most significant BCD digit) =>
1269
--          control_2(1 downto 0) & control_1(7 downto 6)
1270
--          control_1(5) remains '0'
1271
--      Year_0 (the least significant BCD digit) =>
1272
--          control_1(4 downto 1)
1273
--
1274
-- Bus interface writes to the registers cannot be given higher priority
1275
-- than internal updates for timekeeping based on pps_i, since bus writes
1276
-- only affect one value, while timekeeping can occasionally alter all of
1277
-- them.  Thus corrupt and undesired behavior could result if bus writes
1278
-- were to override the timekeeping update values.  Therefore, the ack_o
1279
-- signal is used to ensure that the timekeeping is finished before bus
1280
-- writes take place.
1281
--
1282
-- The registers are summarized as follows:
1283
--
1284
-- Address   Structure      Function
1285
-- -------   ----------   ----------------------------------------
1286
--   0x0     [**YY*DDD]    BCD Year, Day of Year (Day is 1..365)
1287
--   0x1     [**HHMMSS]    BCD Hours, Minutes, Seconds. (*=unused nibble)
1288
--   0x2     [CCCCCCCC]    Control Bits (31:0)
1289
--   0x3     [**CCCCCC]    Control Bits (44:32)
1290
--   0x4     (2:0)         Rate Setting (See Notes below)
1291
--   0x5     (2:0)         Carrier Modulation Frequency
1292
--   0x6     (2:0)         Codes Used [ctrl,SBS,year]
1293
--   0x7     (6:0)         Time receiver control and status
1294
--   0x8     (31:0)        Statistical report (read only)
1295
--   0x9     (31:0)        PPS residusum, a 25 bit signed value, (sign extended)
1296
--   0xA     (8:0)         Lowpass filter enable, CDET bias setting
1297
--
1298
-- Notes:
1299
--
1300
--   Register 0x0 (Leap Year, Year, Day of Year)
1301
--
1302
--    Bit 28 controls the leap year setting:
1303
--    This register currently has only a single bit for setting leap year
1304
--    behavior.  When the leap year bit is set, the day field advances to 366
1305
--    before rolling over back to 1.  When the bit is clear, the day field
1306
--    to 1 after the 365 day value.  Note that the DDD field can be written
1307
--    with values higher than 366, but it will immediately roll over.
1308
--
1309
--   Register 0x4 (Rate)
1310
--    000 => (Use external input for Rate/Form/Carrier/Codes)*
1311
--    001 => IRIG-B (1 second updates)
1312
--    010 => IRIG-A (0.1 second updates)
1313
--    011 => IRIG-G (0.01 second updates)
1314
--    100 => NASA 36-bit (WWV) time code
1315
--
1316
--    The "rate_i" input is used along with the register setting, in such
1317
--    a way that the register Rate value being zero, causes the "rate_i" and
1318
--    "codes_i" inputs to be used.  If, however, the register Rate is set to
1319
--    a non-zero value, then the external input is ignored.
1320
--
1321
--    When external rate input is used, a setting of "000" results
1322
--    in IRIG-B (1 second updates.)  All other settings remain
1323
--    as outlined above.
1324
--
1325
--   Register 0x5 (Carrier)
1326
--
1327
--   000 => No carrier (DC Level Shift)
1328
--   001 => 100 Hz / 10 ms. resolution
1329
--   010 => 1 kHz / 1 millisecond resolution
1330
--   011 => 10 kHz / 100 microsecond resolution
1331
--   100 => 100 kHz / 10 microsecond resolution
1332
--
1333
-- Internal logic takes the day, hour, minute and second values and derives
1334
-- a value for the SBS field (Straight Binary Second).
1335
--
1336
--   Register 0x6 (Codes)
1337
--    Each bit position, when set, activates the use of a specific field
1338
--    within the output, according to the form:
1339
--
1340
--       [ctrl,SBS,year]
1341
--
1342
--    Where ctrl = Control field
1343
--           SBS = Straight Binary Seconds
1344
--          year = Year field
1345
--
1346
--     If year and control are both selected, then the year is used in its 
1347
--     appropriate spot, preventing the control bits there from being used.
1348
--
1349
--     For Rate "G", the hundredths of a second field occludes the first four
1350
--     control bits, so that they are not used.  The hundredths of a second field
1351
--     is encoded in the register area for "year" since year is not used in
1352
--     Rate "G"
1353
--
1354
-- Register 0x07
1355
--   Time Receiver Status register
1356
--     bits (1:0) = cdet_val.  These bits are the carrier detect value,
1357
--                  which relates to the detected IRIG timecode speed.
1358
--                     cdet_val   Meaning
1359
--                     --------   -----------------------------
1360
--                         3      IRIG G speed (100kHz carrier)
1361
--                         2      IRIG A speed (10 kHz carrier)
1362
--                         1      IRIG B speed (1  kHz carrier)
1363
--                         0      no carrier detected.
1364
--
1365
--     bits (3:2) = reserved (value is set to "00").
1366
--
1367
--     bit (4) = active status bit.  '1' means activity was detected on the
1368
--               irig_dcl_i input.
1369
--     bit (5) = lock status bit.  '1' indicates the receiver is in the 
1370
--               TRACK_IRIG state
1371
--     bit (6) = IRIG tracking status bit.  Read only bit that indicates the
1372
--               received IRIG timecode is valid for time tracking
1373
--     bit (7) = PPS tracking status bit.  Read only bit that indicates the
1374
--               received PPS signal is valid for time tracking
1375
--     bit (8) = neverlocked status bit.  '1' indicates the unit has not locked
1376
--               since reset.  This bit can only be set after Power On Reset.
1377
--               Read only.
1378
--     bit (9) = rx_nasa36 bit.  This bit is set whenever NASA 36 timecode is
1379
--               detected and used.  Read only.
1380
--     bit (10)= cdet_sel_l bit.  This bit is read only, and indicates when
1381
--               the receiver state machine is asserting carrier detect mode.
1382
--               When this bit is set, external circuitry is configured to 
1383
--               strip out the time information, and only provide a 
1384
--               "zero crossing" detected carrier on the time_cdet_i input.
1385
--               The "zero crossing" detected carrier is a squarewave at the
1386
--               frequency of the carrier.  When this bit is clear, pulses arrive
1387
--               corresponding to the times when the carrier is at high-amplitude.
1388
--     bits (31:24) = rx_pulse_width.  This is the most recently measured pulse
1389
--               low value as a percentage.  Nominal IRIG timecode pulses
1390
--               should be high: 20% for zero, 50% for one, and 80% for ref.
1391
--               The rx_pulse_width is a count of the low time after the
1392
--               trailing edge of the received pulse.  Thus, values >96
1393
--               represent pulses which are too short, >72 are zeros,
1394
--               >48 are ones and >16 are ref pulses.  These thresholds are
1395
--               defined as constants within the code.
1396
--                 
1397
-- Register 0x08
1398
--   Receiver additional information
1399
--     bits (3:0) = receiver current state
1400
--       value   name           meaning
1401
--       -----   -----------    --------------------------------------------------------------
1402
--         0     SETTLE_DOWN    Awaiting capacitor discharge following cdet_sel_o being cleared
1403
--         1     CDET_1         Awaiting carrier speed detect synchronization
1404
--         2     CDET_2         Awaiting carrier speed detect
1405
--         3     SETTLE_UP      Awaiting capacitor charge following cdet_sel_o being set
1406
--         4     SEEK_REF_1     Seeking a longer "reference" type pulse
1407
--         5     SEEK_REF_2     Seeking a second CONSECUTIVE "reference" type pulse
1408
--         6     RECEIVE_PULSES Receiving and decoding IRIG time pulses
1409
--         7     SETTLE_IN      Awaiting capacitor discharge heading into TRACK_IRIG
1410
--         8     TRACK_IRIG     Tracking incoming IRIG time carrier based PPS
1411
--
1412
--     bits (7:4) = reserved
1413
--
1414
--     bits (15:8)  = number of times rx_active has been asserted. Write to clear.
1415
--     bits (23:16) = number of times RECEIVE_PULSES has been entered. Write to clear.
1416
--     bits (31:24) = number of times TRACK_IRIG has been entered. Write to clear.
1417
--
1418
-- Register 0x09
1419
--   Receiver PPS tracking "residusum"
1420
--     This is a signed quantity which represents the speed adjustment to the internally
1421
--     generated IRIG time output.  It is based on measurements against a incoming pulse
1422
--     per second time reference.  There are two PPS references: pps_i and an internal
1423
--     irig_pps which is based on dividing down the received carrier frequency whenever
1424
--     the receiver is in the TRACK_IRIG state.
1425
--     This is a signed quantity, which has been sign extended to 32 bits.  It is limited
1426
--     by the constant RESIDUSUM_BITS to be at most +/- 1/128 the magnitude of the frequency
1427
--     setting (An allowed frequency variation window of +/- 0.7825 percent.)
1428
--
1429
-- Register 0x0A
1430
--   Carrier Detect Bias, and time lowpass filter enable
1431
--     bits (5:0) = Carrier detect bias value.  Ranging from 0x00 to 0x20, this value
1432
--                  drives a PWM output to an RC filter network, creating a voltage used
1433
--                  as a threshold to detect either the timecode carrier, or the carrier
1434
--                  "high amplitude peaks" depending on cdet_sel_o.
1435
--     bit (8)    = time_cdet_i lowpass filter enable.  When set, this bit enables use of
1436
--                  a leaky-integrator based lowpass filter.
1437
--
1438
-- The generics used as defaults for the registers are the full width of the
1439
-- data bus.  However, only those bits which are actually present in the actual
1440
-- registers are used.
1441
 
1442
library IEEE;
1443
use IEEE.STD_LOGIC_1164.ALL;
1444
use IEEE.NUMERIC_STD.ALL;
1445
use IEEE.MATH_REAL.ALL;
1446
 
1447
library work;
1448
use work.dds_pack.all;
1449
use work.pwm_pack.all;
1450
use work.convert_pack.all;
1451
use work.signal_conditioning_pack.all;
1452
 
1453
entity irig_time_regenerator is
1454
    generic (
1455
      SYS_CLK_RATE   : real := 40000000.0; -- Needed for carrier generation
1456
      LOCK_THRESHOLD : integer :=        8; -- Frames received before entering locked state
1457
      DEF_R_0 : unsigned(31 downto 0) := str2u("00100123",32);
1458
      DEF_R_1 : unsigned(31 downto 0) := str2u("00000000",32);
1459
      DEF_R_2 : unsigned(31 downto 0) := str2u("00000000",32);
1460
      DEF_R_3 : unsigned(31 downto 0) := str2u("00000000",32);
1461
      DEF_R_4 : unsigned(31 downto 0) := str2u("00000001",32);
1462
      DEF_R_5 : unsigned(31 downto 0) := str2u("00000002",32);
1463
      DEF_R_6 : unsigned(31 downto 0) := str2u("00000007",32);
1464
      DEF_R_A : unsigned(31 downto 0) := str2u("00000014",32); -- Initial value of carrier detect bias
1465
      DEF_R_Z : unsigned(31 downto 0) := str2u("00000000",32)  -- Value returned for nonexistent registers
1466
    );
1467
    port (
1468
 
1469
      sys_rst_n     : in std_logic;
1470
      sys_clk       : in std_logic;
1471
      sys_clk_en    : in std_logic;
1472
 
1473
      -- Bus interface
1474
      adr_i         : in  unsigned(3 downto 0);
1475
      sel_i         : in  std_logic;
1476
      we_i          : in  std_logic;
1477
      dat_i         : in  unsigned(31 downto 0);
1478
      dat_o         : out unsigned(31 downto 0);
1479
      ack_o         : out std_logic;
1480
 
1481
      -- Pulse per second time reference
1482
 
1483
      -- IRIG select inputs
1484
      -- Active only when register 0x4 set to zero.
1485
      rate_i        : in  unsigned(2 downto 0); -- Rates    : 0,1=>B, 2=>A, 3=>G, 4=>NASA 36-bit
1486
      carrier_i     : in  unsigned(2 downto 0); -- Carriers : 0=DCLS, 1=100Hz, 2=1kHz, 3=10kHz, 4=100kHz
1487
      codes_i       : in  unsigned(2 downto 0); -- Codes    : (2)=> control, (1)=> SBS, (0)=> year
1488
 
1489
      -- IRIG time output, parallel form
1490
      time_o        : out unsigned(67 downto 0);
1491
 
1492
      -- Carrier Detect Select output, PPS input,
1493
      -- IRIG time input, tracking and lock indicators
1494
      cdet_sel_o    : out std_logic;
1495
      pps_i         : in  std_logic;
1496
      time_cdet_i   : in  std_logic;
1497
      cdet_val_o    : out unsigned(1 downto 0);
1498
      active_o      : out std_logic;
1499
      tracking_o    : out std_logic;
1500
      lock_o        : out std_logic;
1501
      neverlocked_o : out std_logic;
1502
 
1503
      -- Serial IRIG time stream output, LVTTL level
1504
      time_dcl_o    : out std_logic;
1505
      cap_gnd_o     : out std_logic;
1506
      ref_o         : out std_logic;
1507
      carrier_o     : out unsigned(1 downto 0)
1508
    );
1509
end irig_time_regenerator;
1510
 
1511
architecture beh of irig_time_regenerator is
1512
 
1513
-- Constants
1514
 
1515
  -- IRIG Generator Constants
1516
    -- DDS size
1517
constant RX_1MHZ_DDS_BITS : natural := 32;
1518
constant HDYNE_DDS_BITS      : integer := 32;
1519
    -- Digital Heterodyne constants
1520
constant INCREMENT_1000KHZ   : unsigned(HDYNE_DDS_BITS-1 downto 0) := to_unsigned(integer(2.0**HDYNE_DDS_BITS/SYS_CLK_RATE*1000000.0),HDYNE_DDS_BITS);
1521
constant INCREMENT_1100KHZ   : unsigned(HDYNE_DDS_BITS-1 downto 0) := to_unsigned(integer(2.0**HDYNE_DDS_BITS/SYS_CLK_RATE*1100000.0),HDYNE_DDS_BITS);
1522
constant INCREMENT_1010KHZ   : unsigned(HDYNE_DDS_BITS-1 downto 0) := to_unsigned(integer(2.0**HDYNE_DDS_BITS/SYS_CLK_RATE*1010000.0),HDYNE_DDS_BITS);
1523
constant INCREMENT_1001KHZ   : unsigned(HDYNE_DDS_BITS-1 downto 0) := to_unsigned(integer(2.0**HDYNE_DDS_BITS/SYS_CLK_RATE*1001000.0),HDYNE_DDS_BITS);
1524
constant INCREMENT_1000100HZ : unsigned(HDYNE_DDS_BITS-1 downto 0) := to_unsigned(integer(2.0**HDYNE_DDS_BITS/SYS_CLK_RATE*1000100.0),HDYNE_DDS_BITS);
1525
 
1526
  -- IRIG receiver constants
1527
constant FRAME_COUNT_BITS : natural := bit_width(LOCK_THRESHOLD);
1528
constant PHASE_INCR_1MHZ  : natural := natural(1000000.0/SYS_CLK_RATE*(2**real(RX_1MHZ_DDS_BITS)));
1529
constant RESIDUSUM_BITS   : natural := RX_1MHZ_DDS_BITS-7; -- Restrict variation to approx. 1% (1/128) of frequency setting
1530
constant RX_EDGE_VAL      : natural := 16;
1531
constant RX_ONE_VAL       : natural := 48;
1532
constant RX_ZERO_VAL      : natural := 72;
1533
constant RX_TOO_SHORT     : natural := 96;
1534
constant RX_C_SEED_B      : integer := 1000;   -- Counts carrier pulses
1535
constant RX_C_SEED_A      : integer := 10000;  -- Counts carrier pulses
1536
constant RX_C_SEED_G      : integer := 100000; -- Counts carrier pulses
1537
constant PPS_R_COUNT_SEED : integer := 999999; -- Counts 1 million microseconds...
1538
constant PPS_TRACK_LIMIT  : integer := 1000;
1539
constant CDET_SETTLE_TIME : integer := 24; -- Milliseconds of settling when cdet_sel_o changes.
1540
--constant CDET_SETTLE_TIME : integer := 0; -- For fast simulation
1541
-- NOTE: It was discovered during testing that, with the enhancements to the
1542
-- IRIG timecode receiver, settling time is not needed in order to obtain a
1543
-- lock on the incoming signal.  Therefore, apart from a slight initial
1544
-- inaccuracy in frequency tracking for <24ms in the TRACK_IRIG state, there
1545
-- is no effect from skipping the settling time.  It will be left at zero ms
1546
-- unless a compelling reason is found to increase it.
1547
 
1548
-- Internal signal declarations
1549
  -- State Machine
1550
type FSM_STATE_TYPE is (RESET, IDLE, P0_REF, PR_REF, P0_DOUT, P1_PLUS_REF, P1_PLUS_DOUT);
1551
signal fsm_state        : FSM_STATE_TYPE;
1552
signal fsm_ack          : std_logic;
1553
signal ack_l            : std_logic;
1554
 
1555
-- Related to SBS calculation
1556
  -- SBS Calculation State
1557
type SBS_STATE_TYPE is (RESET, IDLE, TIMEKEEPING_DELAY, SBS_10H, SBS_1H, SBS_10M, SBS_1M, SBS_10S);
1558
signal sbs_state        : SBS_STATE_TYPE;
1559
 
1560
-- Related to Registers
1561
signal leap_year        : std_logic;
1562
signal secs_0           : unsigned(3 downto 0);
1563
signal secs_1           : unsigned(2 downto 0);
1564
signal mins_0           : unsigned(3 downto 0);
1565
signal mins_1           : unsigned(2 downto 0);
1566
signal hours_0          : unsigned(3 downto 0);
1567
signal hours_1          : unsigned(1 downto 0);
1568
signal days_0           : unsigned(3 downto 0);
1569
signal days_1           : unsigned(3 downto 0);
1570
signal days_2           : unsigned(1 downto 0);
1571
signal years_0          : unsigned(3 downto 0);
1572
signal years_1          : unsigned(3 downto 0);
1573
signal tenths           : unsigned(3 downto 0);
1574
signal hundredths       : unsigned(3 downto 0);
1575
 
1576
signal sbs              : unsigned(16 downto 0);
1577
signal sbs_next         : unsigned(16 downto 0);
1578
signal sbs_add_count    : unsigned(3 downto 0);
1579
 
1580
signal control          : unsigned(44 downto 0);
1581
signal reg_rate         : unsigned(2 downto 0);
1582
signal reg_carrier      : unsigned(2 downto 0);
1583
signal reg_codes        : unsigned(2 downto 0);
1584
signal irig_rate        : unsigned(2 downto 0);
1585
signal irig_carrier     : unsigned(2 downto 0);
1586
signal irig_codes       : unsigned(2 downto 0);
1587
 
1588
-- Related to generation of the output pulse stream
1589
signal tx_1mhz_pulse    : std_logic; -- 1MHz pulse stream which can warp when tracking.
1590
signal set_time         : std_logic;
1591
signal pulse_clk_count  : unsigned(9 downto 0); -- Divides by factor of 10, 100 or 1000
1592
signal pulse_clk_en     : std_logic;
1593
signal pulse_sub_count  : unsigned(3 downto 0); -- Advances when pulse_clk_en=1
1594
signal sub_count_nine   : std_logic;
1595
signal p_count          : unsigned(3 downto 0); -- Advances when sub_count=9 and pulse_sr_count=9
1596
signal cycle_count      : unsigned(6 downto 0); -- Counts output cycles during the current second.
1597
signal pulse_sr         : unsigned(8 downto 0);
1598
signal pulse_sr_count   : unsigned(3 downto 0); -- Advances when sub_count=9
1599
signal sr_load_bus      : unsigned(8 downto 0);
1600
signal p0               : unsigned(8 downto 0); -- sr load value
1601
signal p1               : unsigned(8 downto 0); -- sr load value
1602
signal p2               : unsigned(8 downto 0); -- sr load value
1603
signal p3               : unsigned(8 downto 0); -- sr load value
1604
signal p4               : unsigned(8 downto 0); -- sr load value
1605
signal p5               : unsigned(8 downto 0); -- sr load value
1606
signal p6               : unsigned(8 downto 0); -- sr load value
1607
signal p7               : unsigned(8 downto 0); -- sr load value
1608
signal p8               : unsigned(8 downto 0); -- sr load value
1609
signal p9               : unsigned(8 downto 0); -- sr load value
1610
signal pulse_extent     : unsigned(3 downto 0);
1611
signal time_pulses      : std_logic;
1612
signal pps              : std_logic;
1613
 
1614
-- Related to generation of the sinewave carrier
1615
signal hdyne_dds_sync    : std_logic;
1616
signal hdyne_base_square : std_logic;
1617
signal hdyne_in_square   : std_logic;
1618
signal hdyne_out         : std_logic;
1619
signal hdyne_in_freq     : unsigned(HDYNE_DDS_BITS-1 downto 0);
1620
signal modulated_carrier : std_logic;
1621
 
1622
-- Related to Timecode Receiver
1623
type RX_STATE_TYPE is (WAIT_ACTIVE, SEEK_REF_1, SEEK_REF_2, RECEIVE_PULSES, SETTLE_IN, TRACK_IRIG);
1624
signal rx_state         : RX_STATE_TYPE;
1625
signal rx_nasa36        : std_logic; -- High means NASA 36-bit code detected
1626
signal rx_sr            : unsigned(9 downto 0);
1627
signal rx_scale_factor  : unsigned(7 downto 0);
1628
signal rx_scale_count   : unsigned(7 downto 0);
1629
signal rx_lo_count      : unsigned(7 downto 0);
1630
signal rx_pulse_width   : unsigned(7 downto 0);
1631
signal rx_pulse_count   : unsigned(3 downto 0);
1632
signal rx_group_count   : unsigned(3 downto 0);
1633
signal rx_freq          : unsigned(RX_1MHZ_DDS_BITS-1 downto 0); -- Sets frequency
1634
signal rx_1mhz_pulse    : std_logic;
1635
signal rx_posedge       : std_logic;
1636
signal rx_pstart        : std_logic;
1637
signal rx_hi_count      : unsigned(7 downto 0);
1638
signal rx_raise_count   : unsigned(3 downto 0);
1639
signal rx_active_wdog   : unsigned(7 downto 0);
1640
signal rx_active_count  : unsigned(3 downto 0);
1641
signal rx_active        : std_logic;
1642
signal rx_pps_tracking  : std_logic;
1643
signal rx_irig_tracking : std_logic;
1644
signal rx_lock          : std_logic;
1645
signal micro_bcd_0      : unsigned(3 downto 0);
1646
signal micro_bcd_1      : unsigned(3 downto 0);
1647
signal micro_bcd_2      : unsigned(3 downto 0);
1648
signal micro_bcd_3      : unsigned(3 downto 0);
1649
signal micro_bcd_4      : unsigned(3 downto 0);
1650
signal micro_bcd_5      : unsigned(3 downto 0);
1651
signal rx_c_count       : unsigned(16 downto 0);
1652
signal pps_r_count      : signed(20 downto 0); -- Counts down 1 sec. worth of microseconds.
1653
signal pps_residusum    : signed(RESIDUSUM_BITS-1 downto 0);
1654
signal rx_secs_0        : unsigned(3 downto 0);
1655
signal rx_secs_1        : unsigned(2 downto 0);
1656
signal rx_mins_0        : unsigned(3 downto 0);
1657
signal rx_mins_1        : unsigned(2 downto 0);
1658
signal rx_hours_0       : unsigned(3 downto 0);
1659
signal rx_hours_1       : unsigned(1 downto 0);
1660
signal rx_days_0        : unsigned(3 downto 0);
1661
signal rx_days_1        : unsigned(3 downto 0);
1662
signal rx_days_2        : unsigned(1 downto 0);
1663
signal rx_years_0       : unsigned(3 downto 0);
1664
signal rx_years_1       : unsigned(3 downto 0);
1665
signal rx_neverlocked   : std_logic;
1666
signal cdet_fixed       : std_logic;
1667
signal cdet_val_l       : unsigned(1 downto 0);
1668
signal cdet_sel_l       : std_logic;
1669
signal pulse_1ms        : std_logic;
1670
signal cdet_ms_timer    : unsigned(4 downto 0);
1671
signal cdet_bias        : unsigned(5 downto 0);
1672
signal cdet_dac_clk_en  : std_logic;
1673
signal time_cleaner_input : signed(15 downto 0);
1674
signal time_cleaned     : std_logic;
1675
signal time_rx          : std_logic;
1676
signal time_rx_r1       : std_logic;
1677
signal time_rx_r2       : std_logic;
1678
signal reg_time_lp      : std_logic;
1679
signal pps_r1           : std_logic;
1680
signal pps_r2           : std_logic;
1681
signal rx_state_report  : unsigned(3 downto 0);
1682
signal rx_stat_1        : unsigned(7 downto 0);
1683
signal rx_stat_2        : unsigned(7 downto 0);
1684
signal rx_stat_3        : unsigned(7 downto 0);
1685
 
1686
-----------------------------------------------------------------------------
1687
begin
1688
 
1689
-- Select which settings to use
1690
irig_rate    <= reg_rate    when (reg_rate/=0) else rate_i;
1691
irig_carrier <= reg_carrier when (reg_rate/=0) else carrier_i;
1692
irig_codes   <= reg_codes   when (reg_rate/=0) else codes_i;
1693
 
1694
-- Register read mux
1695
with (adr_i) select
1696
  dat_o <=
1697
    "000" & leap_year & "0000" & years_1 & years_0 & "000000" & days_2 & days_1 & days_0 when "0000",  --  "0";
1698
    "0000000000" & hours_1 & hours_0 & "0" & mins_1 & mins_0 & "0" & secs_1 & secs_0     when "0001",  --  "1";
1699
    control(31 downto 0)                                                                 when "0010",  --  "2";
1700
    "0000000000000000000" & control(44 downto 32)                                        when "0011",  --  "3";
1701
    u_resize(reg_rate,32)                                                                when "0100",  --  "4";
1702
    u_resize(reg_carrier,32)                                                             when "0101",  --  "5";
1703
    "00000000000000000000000000000" & reg_codes                                          when "0110",  --  "6";
1704
    rx_pulse_width & "0000000000000" & cdet_fixed & rx_nasa36 & rx_neverlocked &
1705
               rx_pps_tracking & rx_irig_tracking & rx_lock & rx_active & "00" & cdet_val_l when "0111",  --  "7";
1706
    rx_stat_3 & rx_stat_2 & rx_stat_1 & "0000" & rx_state_report                       when "1000",  --  "8";
1707
    unsigned(s_resize_se(pps_residusum,32))                                            when "1001",  --  "9";
1708
    "00000000000000000000000" & reg_time_lp & u_resize(cdet_bias,8)                    when "1010",  --  "A";
1709
    DEF_R_Z                                                                            when others;   -- Default
1710
 
1711
-- Create acknowledge signal
1712
ack_l <= fsm_ack when (sel_i='1' and we_i='1' and adr_i<7) else -- special ack for tx fsm registers
1713
         '1' when (sel_i='1') else
1714
         '0';
1715
ack_o <= ack_l;
1716
 
1717
-- Produce parallel output
1718
time_o <= years_1 & years_0 & "00" & days_2 & days_1 & days_0 & "00" & hours_1 & hours_0 &
1719
          '0' & mins_1 & mins_0 & '0' & secs_1 & secs_0 & micro_bcd_5 & micro_bcd_4 &
1720
          micro_bcd_3 & micro_bcd_2 & micro_bcd_1 & micro_bcd_0;
1721
 
1722
  --------------------------
1723
  -- Heterodyning Signals
1724
  -- The idea here is to multiply or "mix" two "squarewave" sequences
1725
  -- by using an XOR gate, to produce a sum and difference frequency.
1726
  -- The sum frequency should be easily filtered out by an RC filter
1727
  -- external to the FPGA.
1728
 
1729
  hdyne_base_squarewave : dds_squarewave
1730
    generic map(
1731
      ACC_BITS     => HDYNE_DDS_BITS
1732
    )
1733
    port map(
1734
 
1735
      sys_rst_n    => sys_rst_n,
1736
      sys_clk      => sys_clk,
1737
      sys_clk_en   => sys_clk_en,
1738
 
1739
      -- Frequency setting
1740
      freq_i       => INCREMENT_1000KHZ,
1741
 
1742
      -- Output
1743
      pulse_o      => open,
1744
      squarewave_o => hdyne_base_square
1745
    );
1746
 
1747
  hdyne_in_squarewave : dds_squarewave_phase_load
1748
    generic map(
1749
      ACC_BITS     => HDYNE_DDS_BITS
1750
    )
1751
    port map(
1752
 
1753
      sys_rst_n    => sys_rst_n,
1754
      sys_clk      => sys_clk,
1755
      sys_clk_en   => sys_clk_en,
1756
 
1757
      -- Frequency setting
1758
      freq_i       => hdyne_in_freq,
1759
 
1760
      -- Synchronous load
1761
      phase_i      => ((HDYNE_DDS_BITS-2)=>'1', others=>'0'), -- Set to 1/4 phase point (sinewave "zero" output)
1762
      phase_ld_i   => hdyne_dds_sync,
1763
 
1764
      -- Output
1765
      pulse_o      => open,
1766
      squarewave_o => hdyne_in_square
1767
    );
1768
 
1769
  hdyne_in_freq <= INCREMENT_1100KHZ when irig_carrier=4 else
1770
                   INCREMENT_1010KHZ when irig_carrier=3 else
1771
                   INCREMENT_1001KHZ when irig_carrier=2 else
1772
                   INCREMENT_1000100HZ;
1773
  hdyne_out <= hdyne_base_square xor hdyne_in_square; -- A sort of digital signal multiplication
1774
  modulated_carrier <= hdyne_out when time_pulses='1' else hdyne_base_square; -- Output midpoint when pulse is low
1775
  carrier_o(0) <= '0' when irig_carrier=0 else hdyne_out;
1776
  carrier_o(1) <= time_pulses when irig_carrier=0 else
1777
                  modulated_carrier;
1778
  ref_o        <= '0' when irig_carrier=0 else hdyne_base_square; -- Used as a "midpoint" voltage reference
1779
 
1780
  --------------------------
1781
  -- Setting for output filter capacitor.
1782
  -- The idea here is to ground the larger capacitor for low frequency
1783
  -- carriers, and for the highest frequency, not ground it.  It will
1784
  -- be connected in series with a smaller capacitor then.
1785
  cap_gnd_o     <= '0' when irig_carrier=4 else
1786
                   '1';
1787
 
1788
-- This process is where the different BCD values are compiled into
1789
-- a "straight binary seconds" (SBS) field.
1790
-- The calculations are done in a multiply accumulate fashion (MAC)
1791
-- wherein the multiply is implemented as a series of additions.
1792
-- An SBS accumulator stores the intermediate calculation total,
1793
-- while a state variable tracks the current stage of calculation,
1794
-- beginning at "tens of hours" and finishing at "seconds."
1795
-- All the adds performed here are unsigned...
1796
sbs_proc : process(sys_clk,sys_rst_n)
1797
begin
1798
  if (sys_rst_n='0') then
1799
    sbs_state <= RESET;
1800
    sbs <= (others=>'0');
1801
    sbs_next <= (others=>'0');
1802
    sbs_add_count <= (others=>'0');
1803
  elsif (sys_clk'event and sys_clk='1') then
1804
    if (sys_clk_en='1') then
1805
      case (sbs_state) is
1806
        when RESET =>
1807
          sbs_state <= IDLE;
1808
        when IDLE  =>
1809
          if (pps='1') then
1810
            sbs_state <= TIMEKEEPING_DELAY;
1811
          end if;
1812
        -- This state waits for changes to the BCD time values to propagate,
1813
        -- so they will be stable before this process begins calculation of
1814
        -- the SBS field.
1815
        when TIMEKEEPING_DELAY =>
1816
          sbs <= (others=>'0'); -- Clear accumulator
1817
          sbs_next <= u_resize(hours_1,sbs'length);
1818
          sbs_add_count <= "1010"; -- Multiply by 10
1819
          sbs_state <= SBS_10H;
1820
        -- The following series of states performs multiply and accumulate
1821
        -- calculations to generate the SBS value.
1822
        when SBS_10H =>
1823
          if (sbs_add_count=0) then
1824
            sbs <= (others=>'0');
1825
            sbs_next <= sbs + u_resize(hours_0,sbs'length);
1826
            sbs_add_count <= "0110"; -- Multiply by 6
1827
            sbs_state <= SBS_1H;
1828
          else
1829
            sbs <= sbs + sbs_next;
1830
            sbs_add_count <= sbs_add_count-1;
1831
          end if;
1832
        when SBS_1H  =>
1833
          if (sbs_add_count=0) then
1834
            sbs <= (others=>'0');
1835
            sbs_next <= sbs + u_resize(mins_1,sbs'length);
1836
            sbs_add_count <= "1010"; -- Multiply by 10
1837
            sbs_state <= SBS_10M;
1838
          else
1839
            sbs <= sbs + sbs_next;
1840
            sbs_add_count <= sbs_add_count-1;
1841
          end if;
1842
        when SBS_10M =>
1843
          if (sbs_add_count=0) then
1844
            sbs <= (others=>'0');
1845
            sbs_next <= sbs + u_resize(mins_0,sbs'length);
1846
            sbs_add_count <= "0110"; -- Multiply by 6
1847
            sbs_state <= SBS_1M;
1848
          else
1849
            sbs <= sbs + sbs_next;
1850
            sbs_add_count <= sbs_add_count-1;
1851
          end if;
1852
        when SBS_1M  =>
1853
          if (sbs_add_count=0) then
1854
            sbs <= (others=>'0');
1855
            sbs_next <= sbs + u_resize(secs_1,sbs'length);
1856
            sbs_add_count <= "1010"; -- Multiply by 10
1857
            sbs_state <= SBS_10S;
1858
          else
1859
            sbs <= sbs + sbs_next;
1860
            sbs_add_count <= sbs_add_count-1;
1861
          end if;
1862
        when SBS_10S =>
1863
          if (sbs_add_count=0) then
1864
            sbs <= sbs + u_resize(secs_0,sbs'length); -- Add seconds (no more multiplication.)
1865
            sbs_state <= IDLE;
1866
          else
1867
            sbs <= sbs + sbs_next;
1868
            sbs_add_count <= sbs_add_count-1;
1869
          end if;
1870
        when others =>
1871
          null;
1872
      end case;
1873
    end if; -- sys_clk_en
1874
  end if; -- sys_clk
1875
end process;
1876
 
1877
-- sr load values
1878
  p0 <= "00000" & secs_0 when (irig_rate=4) else
1879
        "0" & secs_1 & "0" & secs_0;
1880
  p1 <= "000000" & secs_1 when (irig_rate=4) else
1881
        "0" & mins_1 & "0" & mins_0;
1882
  p2 <= "00000" & mins_0 when (irig_rate=4) else
1883
        "00" & hours_1 & "0" & hours_0;
1884
  p3 <= "000000" & mins_1 when (irig_rate=4) else
1885
        days_1 & "0" & days_0;
1886
  p4 <= "00000" & hours_0 when (irig_rate=4) else
1887
        "0000000" & days_2 when (irig_rate=0 or irig_rate=1) else
1888
        tenths & "000" & days_2;
1889
  p5 <= "0000000" & hours_1 when (irig_rate=4) else
1890
        control(8 downto 4) & hundredths when (irig_rate=3) else
1891
        years_1 & '0' & years_0 when (irig_codes(2)='1') else
1892
        control(8 downto 0) when (irig_codes(0)='1') else
1893
        "000000000";
1894
  p6 <= "00000" & days_0 when (irig_rate=4) else
1895
        years_1 & '0' & years_0 when (irig_rate=3 and irig_codes(2)='1') else
1896
        control(17 downto 9) when (irig_codes(0)='1') else
1897
        "000000000";
1898
  p7 <= "00000" & days_1 when (irig_rate=4) else
1899
        control(26 downto 18) when (irig_codes(0)='1') else
1900
        "000000000";
1901
  p8 <= "0000000" & days_2 when (irig_rate=4) else
1902
        sbs(8 downto 0) when (irig_codes(1)='1') else
1903
        control(35 downto 27) when (irig_codes(0)='1') else
1904
        "000000000";
1905
  p9 <= "111110000" when (irig_rate=4) else
1906
        '0' & sbs(16 downto 9) when (irig_codes(1)='1') else
1907
        control(44 downto 36) when (irig_codes(0)='1') else
1908
        "000000000";
1909
 
1910
-- sr_load_bus mux
1911
with (p_count) select
1912
  sr_load_bus <=
1913
    p0 when "0000",  --  "0";
1914
    p1 when "0001",  --  "1";
1915
    p2 when "0010",  --  "2";
1916
    p3 when "0011",  --  "3";
1917
    p4 when "0100",  --  "4";
1918
    p5 when "0101",  --  "5";
1919
    p6 when "0110",  --  "6";
1920
    p7 when "0111",  --  "7";
1921
    p8 when "1000",  --  "8";
1922
    p9 when "1001",  --  "9";
1923
    "000000000" when others;   -- Not needed for synthesis...
1924
 
1925
-- Generate a clock enable for advancing the output pulse state
1926
-- The rate of this pulse is determined by the selected IRIG rate.
1927
-- The rate has been selected to allow output pulse duty cycle
1928
-- to be set in increments of 1/10th the total pulse time.
1929
-- 
1930
-- Selected Rate    pulse_clk rate   Pulse Frequency
1931
-- -------------    --------------   -----------------
1932
--     000                1 kHz           100 Hz
1933
--     001 (B)            1 kHz           100 Hz
1934
--     010 (A)           10 kHz             1 kHz
1935
--     011 (G)          100 kHz            10 kHz
1936
--     100 (NASA 36)      1 kHz           100 Hz
1937
--
1938
-- Generate a clock enable for advancing the output carrier state
1939
-- The rate of this pulse is determined by the selected IRIG rate.
1940
-- The rate has been selected to allow 10 digital samples per cycle
1941
-- of carrier output.
1942
-- 
1943
-- Selected Carrier    carrier_clk rate   Carrier Frequency
1944
-- ----------------    ----------------   -----------------
1945
--       000              DC Level             -None-
1946
--       001                 1 kHz            100  Hz
1947
--       010                10 kHz              1 kHz
1948
--       011               100 kHz             10 kHz
1949
--       100                 1 MHz            100 kHz
1950
--
1951
 
1952
pulse_clk_proc : process(sys_clk,sys_rst_n)
1953
begin
1954
  if (sys_rst_n='0') then
1955
    pulse_clk_count <= (others=>'0');
1956
  elsif (sys_clk'event and sys_clk='1') then
1957
    if (sys_clk_en='1') then
1958
      if (pulse_clk_en='1') then
1959
        pulse_clk_count <= (others=>'0');
1960
      elsif (tx_1mhz_pulse='1') then
1961
        pulse_clk_count <= pulse_clk_count + 1;
1962
      end if;
1963
    end if; -- sys_clk_en
1964
  end if; -- sys_clk
1965
end process;
1966
pulse_clk_en <= '1' when (tx_1mhz_pulse='1' and (irig_rate=0 or irig_rate=1 or irig_rate=4) and pulse_clk_count=999) else
1967
                '1' when (tx_1mhz_pulse='1' and irig_rate=2 and pulse_clk_count=99) else
1968
                '1' when (tx_1mhz_pulse='1' and irig_rate=3 and pulse_clk_count=9) else
1969
                '0';
1970
 
1971
-- This is the IRIG timecode generating state machine.
1972
-- It handles updates to the timekeeping registers, based on the "pps" signal,
1973
-- and creates the output pulse stream.
1974
fsm_proc: process(sys_clk, sys_rst_n)
1975
begin
1976
  if (sys_rst_n='0') then
1977
    fsm_state        <= RESET;   -- asynchronous reset
1978
    leap_year        <= '0';
1979
    micro_bcd_0      <= to_unsigned(0,4);
1980
    micro_bcd_1      <= to_unsigned(9,4);
1981
    micro_bcd_2      <= to_unsigned(9,4);
1982
    micro_bcd_3      <= to_unsigned(9,4);
1983
    micro_bcd_4      <= to_unsigned(9,4);
1984
    micro_bcd_5      <= to_unsigned(9,4);
1985
    hundredths       <= (others=>'0');
1986
    tenths           <= (others=>'0');
1987
    days_0           <= DEF_R_0(3 downto 0);
1988
    days_1           <= DEF_R_0(7 downto 4);
1989
    days_2           <= DEF_R_0(9 downto 8);
1990
    years_0          <= DEF_R_0(19 downto 16);
1991
    years_1          <= DEF_R_0(23 downto 20);
1992
    secs_0           <= DEF_R_1(3 downto 0);
1993
    secs_1           <= DEF_R_1(6 downto 4);
1994
    mins_0           <= DEF_R_1(11 downto 8);
1995
    mins_1           <= DEF_R_1(14 downto 12);
1996
    hours_0          <= DEF_R_1(19 downto 16);
1997
    hours_1          <= DEF_R_1(21 downto 20);
1998
    control          <= DEF_R_3(12 downto 0) & DEF_R_2;
1999
    reg_rate         <= DEF_R_4(2 downto 0);
2000
    reg_carrier      <= DEF_R_5(2 downto 0);
2001
    reg_codes        <= DEF_R_6(2 downto 0);
2002
 
2003
    p_count          <= (others=>'0');
2004
    pulse_sr         <= (others=>'0');
2005
    pulse_sr_count   <= (others=>'0');
2006
    pulse_sub_count  <= (others=>'0');
2007
    cycle_count      <= (others=>'0');
2008
 
2009
    fsm_ack          <= '0';
2010
    hdyne_dds_sync   <= '0';
2011
 
2012
  elsif (sys_clk'event and sys_clk='1') then
2013
    if (sys_clk_en='1') then
2014
 
2015
      -- Default values
2016
      fsm_ack <= '0';
2017
      hdyne_dds_sync <= '0';
2018
 
2019
      -- Use DDS to advance the six digit microsecond BCD counter.
2020
      -- This can be overridden by other statements further below.
2021
      if (tx_1mhz_pulse='1') then
2022
        if (micro_bcd_0=9) then
2023
          micro_bcd_0 <= (others=>'0');
2024
          if (micro_bcd_1=9) then
2025
            micro_bcd_1 <= (others=>'0');
2026
            if (micro_bcd_2=9) then
2027
              micro_bcd_2 <= (others=>'0');
2028
              if (micro_bcd_3=9) then
2029
                micro_bcd_3 <= (others=>'0');
2030
                if (micro_bcd_4=9) then
2031
                  micro_bcd_4 <= (others=>'0');
2032
                  if (micro_bcd_5=9) then
2033
                    micro_bcd_5 <= (others=>'0');
2034
                  else
2035
                    micro_bcd_5 <= micro_bcd_5+1;
2036
                  end if;
2037
                else
2038
                  micro_bcd_4 <= micro_bcd_4+1;
2039
                end if;
2040
              else
2041
                micro_bcd_3 <= micro_bcd_3+1;
2042
              end if;
2043
            else
2044
              micro_bcd_2 <= micro_bcd_2+1;
2045
            end if;
2046
          else
2047
            micro_bcd_1 <= micro_bcd_1+1;
2048
          end if;
2049
        else
2050
          micro_bcd_0 <= micro_bcd_0+1;
2051
        end if;
2052
      end if;
2053
      -- Handle timekeeping for fractions of seconds, based on 1MHz clock enable.
2054
      -- These updates are overridden by pps, which resets all the time count values.
2055
      if (tx_1mhz_pulse='1') then
2056
        if (                  -- Check for .01 second transition
2057
            micro_bcd_0=9 and
2058
            micro_bcd_1=9 and
2059
            micro_bcd_2=9 and
2060
            micro_bcd_3=9
2061
            ) then
2062
          if (hundredths=9) then
2063
            tenths <= tenths+1;
2064
            hundredths <= (others=>'0');
2065
            if (tenths=9) then
2066
              -- No need to increment seconds here.  pps does that.
2067
              tenths <= (others=>'0');
2068
            else
2069
              tenths <= tenths+1;
2070
            end if;
2071
          else
2072
            hundredths <= hundredths+1;
2073
          end if;
2074
        end if;
2075
      end if;
2076
 
2077
      -- Handle the timekeeping items, based on pps
2078
      -- The highest priority is "set_time" which immediately resets all time
2079
      -- quantities.  Next highest priority is normal timekeeping, and finally,
2080
      -- the lowest priority is register writes to time quantities.
2081
      if (set_time='1') then
2082
        micro_bcd_0 <= (others=>'0');
2083
        micro_bcd_1 <= (others=>'0');
2084
        micro_bcd_2 <= (others=>'0');
2085
        micro_bcd_3 <= (others=>'0');
2086
        micro_bcd_4 <= (others=>'0');
2087
        micro_bcd_5 <= (others=>'0');
2088
        secs_0  <= rx_secs_0;
2089
        secs_1  <= rx_secs_1;
2090
        mins_0  <= rx_mins_0;
2091
        mins_1  <= rx_mins_1;
2092
        hours_0 <= rx_hours_0;
2093
        hours_1 <= rx_hours_1;
2094
        days_0  <= rx_days_0;
2095
        days_1  <= rx_days_1;
2096
        days_2  <= rx_days_2;
2097
        years_0 <= rx_years_0;
2098
        years_1 <= rx_years_1;
2099
      elsif (pps='1') then
2100
        secs_0 <= secs_0+1;
2101
        tenths <= (others=>'0');
2102
        hundredths <= (others=>'0');
2103
        if (secs_0=9) then
2104
          secs_1 <= secs_1+1;
2105
          secs_0 <= (others=>'0');
2106
          if (secs_1=5) then
2107
            mins_0 <= mins_0+1;
2108
            secs_1 <= (others=>'0');
2109
            if (mins_0=9) then
2110
              mins_1 <= mins_1+1;
2111
              mins_0 <= (others=>'0');
2112
              if (mins_1=5) then
2113
                hours_0 <= hours_0+1;
2114
                mins_1  <= (others=>'0');
2115
                if (hours_1=2 and hours_0=3) then
2116
                  days_0  <= days_0+1;
2117
                  hours_1 <= (others=>'0');
2118
                  hours_0 <= (others=>'0');
2119
                  if (days_2>=3 and days_1>=6 and ((days_0>=5 and leap_year='0') or (days_0>=6 and leap_year='1'))) then
2120
                    years_0 <= years_0+1;
2121
                    days_0  <= "0001";
2122
                    days_1  <= (others=>'0');
2123
                    days_2  <= (others=>'0');
2124
                    if (years_0=9) then
2125
                      years_1 <= years_1+1;
2126
                      years_0 <= (others=>'0');
2127
                      if (years_1=9) then
2128
                        years_1 <= (others=>'0');
2129
                      end if;
2130
                    end if;
2131
                  elsif (days_0=9) then
2132
                    days_1 <= days_1+1;
2133
                    days_0 <= (others=>'0');
2134
                    if (days_1=9) then
2135
                      days_2 <= days_2+1;
2136
                      days_1 <= (others=>'0');
2137
                    end if;
2138
                  end if;
2139
                elsif (hours_0=9) then
2140
                  hours_1 <= hours_1+1;
2141
                  hours_0 <= (others=>'0');
2142
                end if;
2143
              end if;
2144
            end if;
2145
          end if;
2146
        end if;
2147
      elsif (sel_i='1' and we_i='1') then -- Handle bus writes to registers
2148
      -- Cannot do this during pps, since this only writes one value, while
2149
      -- pps timekeeping can potentially affect all the values.  Therefore,
2150
      -- generate an acknowledge signal for the register writes... so that
2151
      -- incoming bus cycles are extended when they collide with pps...
2152
        fsm_ack <= '1';
2153
        case (adr_i) is
2154
          when "0000" =>
2155
            leap_year <= dat_i(28);
2156
            days_0  <= dat_i(3  downto 0);
2157
            days_1  <= dat_i(7  downto 4);
2158
            days_2  <= dat_i(9  downto 8);
2159
            years_0 <= dat_i(19 downto 16);
2160
            years_1 <= dat_i(23 downto 20);
2161
          when "0001" =>
2162
            secs_0  <= dat_i(3  downto 0);
2163
            secs_1  <= dat_i(6  downto 4);
2164
            mins_0  <= dat_i(11 downto 8);
2165
            mins_1  <= dat_i(14 downto 12);
2166
            hours_0 <= dat_i(19 downto 16);
2167
            hours_1 <= dat_i(21 downto 20);
2168
          when "0010" =>
2169
            control(31 downto 0) <= dat_i;
2170
          when "0011" =>
2171
            control(44 downto 32) <= dat_i(12 downto 0);
2172
          when "0100" =>
2173
            reg_rate <= dat_i(2 downto 0);
2174
          when "0101" =>
2175
            reg_carrier <= dat_i(2 downto 0);
2176
          when "0110" =>
2177
            reg_codes <= dat_i(2 downto 0);
2178
          when others => null;
2179
        end case;
2180
      end if;
2181
 
2182
      -- Handle pulse_sub_count
2183
      if (pulse_clk_en='1') then
2184
        if (sub_count_nine='1') then
2185
          pulse_sub_count <= (others=>'0');
2186
          hdyne_dds_sync <= '1'; -- Set the carrier wave phase.
2187
        else
2188
          pulse_sub_count <= pulse_sub_count + 1;
2189
        end if;
2190
      end if;
2191
 
2192
      -- Handle state transitions
2193
      case (fsm_state) is
2194
 
2195
        when RESET =>
2196
          fsm_state <= IDLE;
2197
 
2198
        when IDLE =>
2199
          if (pps='1') then
2200
            fsm_state <= P0_REF;
2201
            p_count   <= (others=>'0');
2202
            cycle_count <= (others=>'0');
2203
            pulse_sr_count <= (others=>'0');
2204
          end if;
2205
 
2206
        when P0_REF =>
2207
          if (pulse_clk_en = '1' and sub_count_nine='1') then
2208
            pulse_sr_count <= pulse_sr_count+1;
2209
            pulse_sr  <= sr_load_bus;
2210
            if (irig_rate=4) then
2211
              fsm_state <= P0_DOUT; -- No special reference pulse for IRIG 36-bit 
2212
            else
2213
              fsm_state <= PR_REF;
2214
            end if;
2215
          end if;
2216
 
2217
        when PR_REF =>
2218
          if (pulse_clk_en = '1' and sub_count_nine='1') then
2219
            pulse_sr_count <= pulse_sr_count+1;
2220
            fsm_state <= P0_DOUT;
2221
          end if;
2222
 
2223
        when P0_DOUT =>
2224
          if (pulse_clk_en = '1' and sub_count_nine='1') then
2225
            if (pulse_sr_count=9) then
2226
              pulse_sr_count <= (others=>'0');
2227
              p_count   <= p_count+1;
2228
              fsm_state <= P1_PLUS_REF;
2229
            else
2230
              pulse_sr  <= '0' & pulse_sr(8 downto 1);
2231
              pulse_sr_count <= pulse_sr_count+1;
2232
            end if;
2233
          end if;
2234
 
2235
        when P1_PLUS_REF =>
2236
          if (pulse_clk_en = '1' and sub_count_nine='1') then
2237
            pulse_sr_count <= pulse_sr_count+1;
2238
            pulse_sr  <= sr_load_bus;
2239
            fsm_state <= P1_PLUS_DOUT;
2240
          end if;
2241
 
2242
        when P1_PLUS_DOUT =>
2243
          if (p_count=9 and pps='1') then -- pps should not occur before p_count=9 anyway...
2244
            fsm_state <= P0_REF;
2245
            p_count   <= (others=>'0');
2246
            cycle_count <= (others=>'0');
2247
            pulse_sr_count <= (others=>'0');
2248
          elsif (pulse_clk_en = '1' and sub_count_nine='1') then
2249
            if (pulse_sr_count=9) then
2250
              if (p_count=9) then
2251
                if (cycle_count=99 and irig_rate=3) then
2252
                  fsm_state <= IDLE;
2253
                elsif (cycle_count=9 and irig_rate=2) then
2254
                  fsm_state <= IDLE;
2255
                elsif (irig_rate=0 or irig_rate=1 or irig_rate=4) then
2256
                  fsm_state <= IDLE;
2257
                else
2258
                  cycle_count <= cycle_count+1;
2259
                  p_count     <= (others=>'0');
2260
                  pulse_sr_count <= (others=>'0');
2261
                  fsm_state <= P0_REF;
2262
                end if;
2263
              else
2264
                pulse_sr_count <= (others=>'0');
2265
                p_count   <= p_count+1;
2266
                pulse_sr  <= sr_load_bus;
2267
                fsm_state <= P1_PLUS_REF;
2268
              end if;
2269
            else
2270
              pulse_sr  <= '0' & pulse_sr(8 downto 1);
2271
              pulse_sr_count <= pulse_sr_count+1;
2272
            end if;
2273
          end if;
2274
 
2275
        --when others => 
2276
        --  fsm_state <= IDLE;
2277
      end case;
2278
 
2279
    end if; -- sys_clk_en
2280
  end if; -- sys_clk
2281
end process;
2282
 
2283
  -- This pulse per second signal is for IRIG timecode generation
2284
  pps <= '1' when (tx_1mhz_pulse='1' and
2285
                    (
2286
                     micro_bcd_0=9 and
2287
                     micro_bcd_1=9 and
2288
                     micro_bcd_2=9 and
2289
                     micro_bcd_3=9 and
2290
                     micro_bcd_4=9 and
2291
                     micro_bcd_5=9
2292
                     )
2293
                   ) else '0';
2294
 
2295
  -- Create signal which indicates last portion of time generator pulse.
2296
  sub_count_nine <= '1' when (pulse_sub_count=9) else '0';
2297
 
2298
  -- Form output pulses
2299
  pulse_extent <= "0010" when (irig_rate=4 and pulse_sr_count=0 and p_count=0) else
2300
                  "0101" when (irig_rate=4 and pulse_sr_count=0) else
2301
                  "1000" when (pulse_sr_count=0 or fsm_state=PR_REF) else
2302
                  "0101" when (pulse_sr(0)='1') else
2303
                  "0010";
2304
 
2305
  time_pulses <= '1' when (pulse_sub_count<pulse_extent) else '0';
2306
  time_dcl_o  <= time_pulses;
2307
 
2308
-- Related to time receiver
2309
----------------------------
2310
 
2311
  --------------------------
2312
  -- generate pulses at 1 MHz nominal rate.
2313
  -- The term nominal is used here, because the actual rate can be "warped" up/down
2314
  -- to track an external PPS reference.
2315
  rx_mega_pulser : dds_squarewave
2316
    generic map(
2317
      ACC_BITS     => RX_1MHZ_DDS_BITS
2318
    )
2319
    port map(
2320
 
2321
      sys_rst_n    => sys_rst_n,
2322
      sys_clk      => sys_clk,
2323
      sys_clk_en   => sys_clk_en,
2324
 
2325
      -- Frequency setting
2326
      freq_i       => rx_freq,
2327
 
2328
      -- Output
2329
      pulse_o      => rx_1mhz_pulse,
2330
      squarewave_o => open
2331
    );
2332
  tx_1mhz_pulse <= rx_1mhz_pulse;
2333
 
2334
  -- Create the frequency setting
2335
  -- It is composed of two terms: the constant term for 1 MHz, and
2336
  -- the fine adjustment term, for varying slightly above or below
2337
  -- the constant value.  The varying term is "pps_residusum".
2338
  rx_freq <= to_unsigned(PHASE_INCR_1MHZ,rx_freq'length) + unsigned(s_resize_se(pps_residusum,rx_freq'length));
2339
 
2340
 
2341
  -- An input signal conditioner for low pass filtering the
2342
  -- carrier squarewave clock signal
2343
  signal_cleanup : multi_stage_leaky_integrator
2344
    generic map(
2345
      STAGES           =>  2,
2346
      LEAK_FACTOR_BITS =>  5, -- Inversely related to LP corner frequency. (Min=1)
2347
      HYSTERESIS_BITS  =>  8,
2348
      INTEGRATOR_BITS  => time_cleaner_input'length
2349
    )
2350
    port map(
2351
      -- System Clock and Clock Enable
2352
      sys_rst_n   => sys_rst_n,
2353
      sys_clk     => sys_clk,
2354
      sys_clk_en  => sys_clk_en,
2355
 
2356
      -- Settings
2357
      input       => time_cleaner_input,
2358
      hysteresis  => str2u("10",8),
2359
 
2360
      -- Integration Result
2361
      integrator  => open,
2362
 
2363
      -- Conditioned Digital Output Signal
2364
      output      => time_cleaned
2365
    );
2366
  time_cleaner_input <= to_signed(64,time_cleaner_input'length) when time_cdet_i='1' else
2367
                        to_signed(-64,time_cleaner_input'length);
2368
 
2369
  -- Select the appropriate time_rx signal
2370
  time_rx <= time_cdet_i when reg_time_lp='0' else time_cleaned;
2371
 
2372
  -- Create IRIG time receiver state report signal
2373
  with (rx_state) select
2374
    rx_state_report <=
2375
      "0011" when WAIT_ACTIVE,
2376
      "0100" when SEEK_REF_1,
2377
      "0101" when SEEK_REF_2,
2378
      "0110" when RECEIVE_PULSES,
2379
      "0111" when SETTLE_IN,
2380
      "1000" when TRACK_IRIG,
2381
      "1111" when others;
2382
 
2383
  -- Assert outputs when in the correct state
2384
  rx_lock       <= '1' when rx_state=TRACK_IRIG else '0';
2385
  lock_o        <= rx_lock;
2386
  active_o      <= rx_active;
2387
--  tracking_o    <= rx_irig_tracking when rx_state=TRACK_IRIG else rx_pps_tracking;
2388
  tracking_o    <= rx_irig_tracking;
2389
  neverlocked_o <= rx_neverlocked;
2390
  cdet_sel_o    <= '1' when rx_state=SETTLE_IN or rx_state=TRACK_IRIG else cdet_sel_l;
2391
 
2392
  dac_clk1 : dds_clk_en_gen
2393
    generic map(
2394
      ACC_BITS  => 24
2395
    )
2396
    port map(
2397
      -- Reset, System Clock and Clock Enable
2398
      sys_rst_n    => sys_rst_n,
2399
      sys_clk      => sys_clk,
2400
      sys_clk_en   => sys_clk_en,
2401
 
2402
      -- Frequency setting
2403
      freq_i       => str2u("001A37",24),
2404
 
2405
      -- Output
2406
      clk_en_o     => cdet_dac_clk_en
2407
    );
2408
 
2409
  dac1 : pwm_unsigned
2410
  generic map(
2411
    PERIOD     => 32,
2412
    COUNT_BITS => 6
2413
  )
2414
  port map(
2415
    -- Reset, System Clock and Clock Enable
2416
    sys_rst_n    => sys_rst_n,
2417
    sys_clk      => sys_clk,
2418
    sys_clk_en   => cdet_dac_clk_en,
2419
 
2420
    -- Input Data
2421
    dat_i        => cdet_bias,
2422
 
2423
    -- Output Signal
2424
    dat_o        => cdet_sel_l
2425
  );
2426
 
2427
  -- Create pulse that indicates the rising edge of receive data
2428
  rx_posedge <= '1' when time_rx_r2='0' and time_rx_r1='1' else '0';
2429
  -- Create pulse that indicates the start of a new timecode pulse
2430
  -- (whether carrier modulated or DC level)
2431
  rx_pstart <= '1' when rx_posedge='1' and rx_lo_count>RX_EDGE_VAL else '0';
2432
 
2433
  -- Select the rx_scale_factor based on carrier detect value
2434
  with (to_integer(cdet_val_l)) select
2435
    rx_scale_factor <=
2436
      to_unsigned(1,rx_scale_factor'length)   when 3,
2437
      to_unsigned(10,rx_scale_factor'length)  when 2,
2438
      to_unsigned(100,rx_scale_factor'length) when 1,
2439
      to_unsigned(100,rx_scale_factor'length) when others;
2440
  -- Provide cdet_val_l as an output signal
2441
  cdet_val_o <= cdet_val_l;
2442
 
2443
-- This is the IRIG timecode receive process
2444
-- It handles updates to the receive time values
2445
rx_proc: process(sys_clk, sys_rst_n)
2446
begin
2447
  if (sys_rst_n='0') then
2448
    rx_state         <= WAIT_ACTIVE;
2449
    rx_nasa36        <= '0';
2450
    rx_pulse_count   <= (others=>'0');
2451
    rx_group_count   <= (others=>'0');
2452
    time_rx_r1       <= '0';
2453
    time_rx_r2       <= '0';
2454
    rx_raise_count   <= (others=>'0');
2455
    rx_active_count  <= (others=>'0');
2456
    rx_active_wdog   <= (others=>'0');
2457
    rx_hi_count      <= (others=>'0');
2458
    rx_lo_count      <= (others=>'0');
2459
    rx_pulse_width   <= (others=>'0');
2460
    pps_r1           <= '0';
2461
    pps_r2           <= '0';
2462
    rx_sr            <= (others=>'0');
2463
    rx_c_count       <= (others=>'0');
2464
    pps_r_count      <= to_signed(PPS_R_COUNT_SEED,pps_r_count'length);
2465
    pps_residusum    <= (others=>'0');
2466
    rx_secs_0        <= (others=>'0');
2467
    rx_secs_1        <= (others=>'0');
2468
    rx_mins_0        <= (others=>'0');
2469
    rx_mins_1        <= (others=>'0');
2470
    rx_hours_0       <= (others=>'0');
2471
    rx_hours_1       <= (others=>'0');
2472
    rx_days_0        <= (others=>'0');
2473
    rx_days_1        <= (others=>'0');
2474
    rx_days_2        <= (others=>'0');
2475
    rx_years_0       <= (others=>'0');
2476
    rx_years_1       <= (others=>'0');
2477
    rx_active        <= '0';
2478
    rx_pps_tracking  <= '0';
2479
    rx_irig_tracking <= '0';
2480
    rx_neverlocked   <= '1';
2481
    cdet_fixed       <= '0';
2482
    cdet_ms_timer    <= (others=>'0');
2483
    cdet_val_l       <= (others=>'0');
2484
    rx_scale_count   <= to_unsigned(1,rx_scale_count'length);
2485
    cdet_bias        <= DEF_R_A(cdet_bias'length-1 downto 0);
2486
    reg_time_lp      <= DEF_R_A(8);
2487
    set_time         <= '0';
2488
    rx_stat_1        <= (others=>'0');
2489
    rx_stat_2        <= (others=>'0');
2490
    rx_stat_3        <= (others=>'0');
2491
 
2492
  elsif (sys_clk'event and sys_clk='1') then
2493
    if (sys_clk_en='1') then
2494
 
2495
      -- Default values
2496
      set_time <= '0'; -- pulses high to set the IRIG generator time
2497
 
2498
      -- Handle carrier detect millisecond timer
2499
      -- Used to time the settling period when cdet_sel_o is changed
2500
      if (pulse_1ms='1') then
2501
        cdet_ms_timer <= cdet_ms_timer+1;
2502
      end if;
2503
 
2504
      -- Service residual counter
2505
      if (rx_1mhz_pulse='1') then
2506
        pps_r_count <= pps_r_count-1;
2507
      end if;
2508
 
2509
      -- Capture incoming carrier pulses
2510
      if (rx_1mhz_pulse='1') then
2511
        time_rx_r1 <= time_rx;
2512
      end if;
2513
      time_rx_r2 <= time_rx_r1;
2514
 
2515
      -- Apply selected scale factor to produce 1 MHz, 100 kHz or 10 kHz rx_sample pulses
2516
      -- rx_posedge is used to synchronize the sample pulse to each new rising edge of
2517
      -- the rx line.
2518
      if (rx_1mhz_pulse='1' and rx_scale_count=1) or rx_posedge='1' then
2519
        rx_scale_count <= rx_scale_factor;
2520
      elsif (rx_1mhz_pulse='1') then
2521
        rx_scale_count <= rx_scale_count-1;
2522
      end if;
2523
 
2524
      -- An up-counter that advances each sample time, but resets when
2525
      -- the rx line is high... rx_lo_count is to be thresholded for data detection.
2526
      -- Keeping rx_lo_count reset when the receive line is high enables
2527
      -- reception of "dc level" irig time data as well as the
2528
      -- "gated carrier pulse" data from the demodulator circuit.      
2529
      if (time_rx_r2='1') then
2530
        rx_lo_count <= (others=>'0');
2531
      elsif (rx_1mhz_pulse='1' and rx_scale_count=1) then
2532
        if (rx_lo_count<255) then -- Do not let counter roll over.
2533
          rx_lo_count <= rx_lo_count+1;
2534
        end if;
2535
      end if;
2536
      -- Capture the pulse width for reporting purposes only.
2537
      if (rx_pstart='1') then
2538
        rx_pulse_width <= rx_lo_count;
2539
      end if;
2540
 
2541
      -- Handle receive shift register
2542
      if (rx_pstart='1') then
2543
        if (rx_lo_count > RX_ZERO_VAL) then
2544
          rx_sr(rx_sr'length-2 downto 0) <= rx_sr(rx_sr'length-1 downto 1);
2545
          rx_sr(rx_sr'length-1) <= '0';
2546
        else
2547
          rx_sr(rx_sr'length-2 downto 0) <= rx_sr(rx_sr'length-1 downto 1);
2548
          rx_sr(rx_sr'length-1) <= '1';
2549
        end if;
2550
      end if;
2551
 
2552
      -- Detect timecode activity here, by examining the rx_lo_count, and
2553
      -- adjusting the sample rate to seek a sensible input signal.
2554
      -- 1) If, at rx_posedge, rx_lo_count>RX_TOO_SHORT even once select a 
2555
      --   lower sample rate.
2556
      -- 2) If, at rx_posedge, rx_lo_count<RX_EDGE_COUNT 10 times in a row,
2557
      --   select a higher sample rate.
2558
      -- 3) Require a duty cycle between 10% and 80%
2559
      -- 4) Once locked, require rx_posedge activity during an N sample
2560
      --      interval.
2561
      -- If, at rx_pstart, all the above conditions are met ten
2562
      --   times in a row, then the input signal and sample rate are
2563
      --   considered correct, and an input "active" condition is declared.
2564
      -- The "active" condition is rescinded if any of the conditions
2565
      --   are not met, at any time.
2566
      if (rx_1mhz_pulse='1' and rx_scale_count=1 and time_rx_r2='1') then
2567
        if (rx_hi_count<((2**rx_hi_count'length)-1)) then -- Do not let counter roll over.
2568
          rx_hi_count <= rx_hi_count+1;
2569
        end if;
2570
      end if;
2571
      -- Count number of times all conditions are met
2572
      if (rx_pstart='1') then
2573
        rx_hi_count <= (others=>'0');
2574
        if (rx_active='0' and rx_hi_count>9 and rx_hi_count<81) then
2575
          rx_active_count <= rx_active_count+1;
2576
          rx_raise_count <= (others=>'0');
2577
          if (rx_active_count=9) then
2578
            rx_active <= '1';
2579
            if (cdet_fixed='0' and cdet_val_l=0) then
2580
              cdet_val_l <= cdet_val_l+1;
2581
            end if;
2582
          end if;
2583
        end if;
2584
      end if;
2585
      -- Handle sample rate adjustments automatically, until a successful
2586
      -- reception of all pulse groups is achieved. Thereafter, only lose
2587
      -- lock due to watchdog.
2588
      if (rx_posedge='1' and not (rx_state=SETTLE_IN or rx_state=TRACK_IRIG)) then
2589
        if (rx_lo_count>RX_TOO_SHORT) then
2590
          rx_active_count <= (others=>'0');
2591
          rx_active <= '0';
2592
          rx_stat_1 <= rx_stat_1+1;
2593
          rx_pulse_width <= to_unsigned(16#AB#,rx_pulse_width'length);
2594
          if (cdet_fixed='0' and cdet_val_l>0) then
2595
            cdet_val_l <= cdet_val_l-1; -- Lower the sample rate
2596
          end if;
2597
        elsif (rx_lo_count<3) then
2598
          rx_raise_count <= rx_raise_count+1;
2599
          rx_active_count <= (others=>'0');
2600
          rx_active <= '0';
2601
          rx_stat_2 <= rx_stat_2+1;
2602
          rx_pulse_width <= to_unsigned(16#AA#,rx_pulse_width'length);
2603
          if (rx_raise_count=9) then
2604
            rx_raise_count <= (others=>'0');
2605
            if (cdet_fixed='0' and cdet_val_l<3) then
2606
              cdet_val_l <= cdet_val_l+1; -- Raise the sample rate
2607
            end if;
2608
          end if;
2609
        end if;
2610
      end if;
2611
      -- Handle activity watchdog
2612
      if (rx_posedge='1') then
2613
        rx_active_wdog <= (others=>'0'); -- reset activity watchdog
2614
      end if;
2615
      if (rx_1mhz_pulse='1' and rx_scale_count=1) then
2616
        rx_active_wdog <= rx_active_wdog+1;
2617
        if (rx_active='1' and rx_active_wdog=200) then
2618
          cdet_val_l <= (others=>'0');
2619
          rx_nasa36  <= '0';
2620
          rx_active_count <= (others=>'0');
2621
          rx_active <= '0';
2622
          rx_stat_3 <= rx_stat_3+1;
2623
        end if;
2624
      end if;
2625
 
2626
      -- Handle state transitions
2627
      case (rx_state) is
2628
 
2629
        when WAIT_ACTIVE =>
2630
          if (rx_active='1') then
2631
            rx_state <= SEEK_REF_1;
2632
          end if;
2633
 
2634
        when SEEK_REF_1 =>
2635
          if (rx_pstart='1') then
2636
            if (rx_sr="0111110000") then
2637
              rx_pulse_count <= to_unsigned(0,rx_pulse_count'length);
2638
              rx_group_count <= to_unsigned(0,rx_group_count'length);
2639
              rx_state  <= RECEIVE_PULSES;
2640
              rx_nasa36 <= '1';
2641
            elsif (rx_lo_count<=RX_ONE_VAL) then
2642
              rx_state  <= SEEK_REF_2;
2643
            end if;
2644
          end if;
2645
 
2646
        when SEEK_REF_2 =>
2647
          if (rx_pstart='1') then
2648
            if (rx_lo_count<=RX_ONE_VAL) then
2649
              rx_pulse_count <= to_unsigned(0,rx_pulse_count'length);
2650
              rx_group_count <= to_unsigned(0,rx_group_count'length);
2651
              rx_state <= RECEIVE_PULSES;
2652
            else
2653
              rx_state <= SEEK_REF_1;
2654
            end if;
2655
          end if;
2656
 
2657
        when RECEIVE_PULSES =>
2658
          if (rx_pstart='1') then
2659
            -- Service the pulse, group and frame counters
2660
            -- Must obtain "group lock" before using received data
2661
            rx_pulse_count <= rx_pulse_count+1;  -- default action
2662
            if (rx_pulse_count=9) then
2663
              rx_pulse_count <= (others=>'0');
2664
              rx_group_count <= rx_group_count+1; -- Default is to advance the count.
2665
              if (rx_group_count=9) then
2666
                rx_group_count <= (others=>'0');
2667
                cdet_ms_timer  <= (others=>'0');
2668
                rx_state <= SETTLE_IN; -- At the end of the full reading, move on
2669
                rx_neverlocked <= '0';
2670
                set_time <= '1';
2671
              end if;
2672
              -- Initialize the time fields as they are received...
2673
              case (to_integer(rx_group_count)) is
2674
                when 0 =>
2675
                  if (rx_sr(4 downto 1)<9) then
2676
                    rx_secs_0 <= rx_sr(4 downto 1)+1;
2677
                  else
2678
                    rx_state <= SEEK_REF_1; -- Adding one during rollover case is too hard.  Just try again later.
2679
                  end if;
2680
                  if (rx_nasa36='0') then
2681
                    rx_secs_1 <= rx_sr(8 downto 6);
2682
                  end if;
2683
                when 1 =>
2684
                  if (rx_nasa36='0') then
2685
                    rx_mins_0 <= rx_sr(3 downto 0);
2686
                    rx_mins_1 <= rx_sr(7 downto 5);
2687
                  else
2688
                    rx_secs_1 <= rx_sr(2 downto 0);
2689
                  end if;
2690
                when 2 =>
2691
                  if (rx_nasa36='0') then
2692
                    rx_hours_0 <= rx_sr(3 downto 0);
2693
                    rx_hours_1 <= rx_sr(6 downto 5);
2694
                  else
2695
                    rx_mins_0 <= rx_sr(3 downto 0);
2696
                  end if;
2697
                when 3 =>
2698
                  if (rx_nasa36='0') then
2699
                    rx_days_0 <= rx_sr(3 downto 0);
2700
                    rx_days_1 <= rx_sr(8 downto 5);
2701
                  else
2702
                    rx_mins_1 <= rx_sr(2 downto 0);
2703
                  end if;
2704
                when 4 =>
2705
                  if (rx_nasa36='0') then
2706
                    rx_days_2 <= rx_sr(1 downto 0);
2707
                  else
2708
                    rx_hours_0 <= rx_sr(3 downto 0);
2709
                  end if;
2710
                when 5 =>
2711
                  if (cdet_val_l/=3) then
2712
                    rx_years_0 <= rx_sr(3 downto 0);
2713
                    rx_years_1 <= rx_sr(8 downto 5);
2714
                  end if;
2715
                  if (rx_nasa36='1') then
2716
                    rx_hours_1 <= rx_sr(1 downto 0);
2717
                  end if;
2718
                when 6 =>
2719
                  if (cdet_val_l=3) then
2720
                    rx_years_0 <= rx_sr(3 downto 0);
2721
                    rx_years_1 <= rx_sr(8 downto 5);
2722
                  end if;
2723
                  if (rx_nasa36='1') then
2724
                    rx_days_0 <= rx_sr(3 downto 0);
2725
                  end if;
2726
                when 7 =>
2727
                  if (rx_nasa36='1') then
2728
                    rx_days_1 <= rx_sr(3 downto 0);
2729
                  end if;
2730
                when 8 =>
2731
                  if (rx_nasa36='1') then
2732
                    rx_days_2 <= rx_sr(1 downto 0);
2733
                  end if;
2734
                when 9 =>
2735
                  null;
2736
                when others =>
2737
                  null;
2738
              end case;
2739
            end if;
2740
          end if; -- if rx_pstart
2741
 
2742
        -- Spend some time waiting for the demodulator circuit to settle.
2743
        -- There are capacitors in it which need to discharge before it works
2744
        -- following a switching event.
2745
        when SETTLE_IN =>
2746
          if (cdet_ms_timer=CDET_SETTLE_TIME) then
2747
            rx_state <= TRACK_IRIG;
2748
            case (to_integer(cdet_val_l)) is
2749
              when 3 =>
2750
                rx_c_count <= to_unsigned(RX_C_SEED_G,rx_c_count'length);
2751
              when 2 =>
2752
                rx_c_count <= to_unsigned(RX_C_SEED_A,rx_c_count'length);
2753
              when 1 =>
2754
                rx_c_count <= to_unsigned(RX_C_SEED_B,rx_c_count'length);
2755
              when others =>
2756
                rx_c_count <= to_unsigned(RX_C_SEED_B,rx_c_count'length);
2757
            end case;
2758
          end if;
2759
 
2760
        when TRACK_IRIG => -- Tracking on received IRIG time signal based PPS
2761
          -- Service carrier pulse countdown.
2762
          -- Counter is reloaded by statements below.
2763
          if (rx_posedge='1') then
2764
            if (rx_c_count=1) then
2765
              case (to_integer(cdet_val_l)) is
2766
                when 3 =>
2767
                  rx_c_count <= to_unsigned(RX_C_SEED_G,rx_c_count'length);
2768
                when 2 =>
2769
                  rx_c_count <= to_unsigned(RX_C_SEED_A,rx_c_count'length);
2770
                when 1 =>
2771
                  rx_c_count <= to_unsigned(RX_C_SEED_B,rx_c_count'length);
2772
                when others =>
2773
                  rx_c_count <= to_unsigned(RX_C_SEED_B,rx_c_count'length);
2774
              end case;
2775
              -- Service time tracking counters
2776
              pps_r_count  <= to_signed(PPS_R_COUNT_SEED,pps_r_count'length);
2777
              if (abs(pps_r_count) > PPS_TRACK_LIMIT) then
2778
                rx_irig_tracking <= '0';
2779
              else
2780
                pps_residusum <= pps_residusum + s_resize_se(s_resize_l(pps_r_count,pps_r_count'length-1),pps_residusum'length); -- divide by two
2781
                rx_irig_tracking <= '1';
2782
              end if;
2783
            else
2784
              rx_c_count <= rx_c_count-1;
2785
            end if;
2786
          end if;
2787
 
2788
        --when others => 
2789
        --  fsm_state <= IDLE;
2790
      end case;
2791
 
2792
      -- Restart acquisition if there is no incoming activity
2793
      -- It is not helpful to increment the stats counter for this...
2794
      if (rx_state/=WAIT_ACTIVE and rx_active='0') then
2795
        pps_r_count  <= to_signed(PPS_R_COUNT_SEED,pps_r_count'length);
2796
        rx_irig_tracking <= '0';
2797
        cdet_ms_timer    <= (others=>'0');
2798
        cdet_val_l <= (others=>'0');
2799
        rx_state <= WAIT_ACTIVE;
2800
      end if;
2801
 
2802
      -- Metastability mitigation chain.
2803
      -- Also used to detect rising edge of pps_i
2804
      pps_r1 <= pps_i;
2805
      pps_r2 <= pps_r1;
2806
 
2807
      -- When not tracking IRIG input, track PPS input if present
2808
      if (rx_state/=TRACK_IRIG and pps_r2='0' and pps_r1='1') then
2809
        if (abs(pps_r_count) > PPS_TRACK_LIMIT) then
2810
          rx_pps_tracking <= '0';
2811
        else
2812
          pps_r_count  <= to_signed(PPS_R_COUNT_SEED,pps_r_count'length);
2813
          pps_residusum <= pps_residusum + s_resize_se(s_resize_l(pps_r_count,pps_r_count'length-1),pps_residusum'length); -- divide by two
2814
          rx_pps_tracking <= '1';
2815
        end if;
2816
      end if;
2817
 
2818
      -- Reset stats registers
2819
      if (sel_i='1' and we_i='1' and ack_l='1') then -- Handle bus writes to registers
2820
        if (adr_i=7) then
2821
          cdet_fixed <= dat_i(10);
2822
          if (cdet_fixed='1') then
2823
            rx_nasa36  <= dat_i(9);
2824
            cdet_val_l <= dat_i(cdet_val_l'length-1 downto 0);
2825
          end if;
2826
        end if;
2827
        if (adr_i=8) then
2828
          rx_stat_1 <= (others=>'0');
2829
          rx_stat_2 <= (others=>'0');
2830
          rx_stat_3 <= (others=>'0');
2831
        end if;
2832
        if (adr_i=10) then
2833
          cdet_bias <= dat_i(cdet_bias'length-1 downto 0);
2834
          reg_time_lp <= dat_i(8);
2835
        end if;
2836
      end if;
2837
 
2838
    end if; -- sys_clk_en
2839
  end if; -- sys_clk
2840
end process;
2841
 
2842
-- A pulse that occurs once every millisecond, based on transmit signals.
2843
pulse_1ms <= '1' when (micro_bcd_0=9 and micro_bcd_1=9 and micro_bcd_2=9 and micro_bcd_3=9 and tx_1mhz_pulse='1') else '0';
2844
 
2845
end beh;
2846
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.