"

MODULE P3028

TITLE 'P3028A'

"NOTE: For all versions, make sure you have the global input constraint"
"set to HOLD rather than PULLUP or PULLDOWN. Set the input constraint in the"
"constraint editor, and when compiling this code, instruct the compiler to"
"merge the constraints in this file (pin numbers) with the constraints from"
"the constraint editor (input hold)."

"Version 1 [19-SEP-13] Based upon P3019A02. We change pin numbers to suit the"
"layout of the A3028. We modify the auxilliary channel code to support two main"
"channels at the same sample rate, or one channel either X or Y."

"Version 2 [31-DEC-13] Change the ring oscillator calibration so as to guarantee"
"the mark-space ratio is 50%, sacrificing frequency resolition."

"Version 3 [24-FEB-14] Correct the channel selection on the ADC so that X is the"
"first channel ID and Y is the second channel ID."

"Version 4 [21-MAR-14] Add conditional compilation clauses to support compiling"
"for the new A302801C layout, which has some changes to the logic chip pinout."
"We set CENTERED_BATTERY to 0 for the A302801A and  A302801B layouts, and to 1 for"
"the A302801C layout."

"Version 5 [01-JAN-15] Add version-letter compilation codes to deal with higher"
"sample rates as well as single and dual channel transmitters. We remove the"
"off-center battery option as it is obsolete."

"[08-JAN-15] We now clock the frequency register with a special VCK signal, which"
"is delayed by two FCK periods from TCK. Previously, we allowed slightly less"
"than one FCK period, which proved to be insufficient in some circuits"

"[26-MAR-15] Add support for A3028C and A3028H."

"Version 6 [08-APR-15] With VERSION = 4 or 5, frequency_low = 7, and x_id = 11 we"
"have F2 generated incorrectly from FCK out of the chip. We introduce combinatorial"
"node FHI, which determines when the output frequency should be HI or LO. We use"
"FHI to set the frequency register bits, which we clock of VCK as before. This"
"seems to solve the logic race condition that existed when we were trying to run the"
"frequency register off its own outputs."

"Version 7 [16-JUL-15] Add another gate to the ring oscillator, bringing the total"
"number of gates in the ring to three. This slows down the fast clock (FCK) enough"
"to allow the clock divider do operate without glitches. The TCK period now increases"
"almost linearly with fck_divisor. The new values of fck_divisor will be lower than"
"the previous values."

"[29-JUL-15] With frequency_low = 7 and version = 1 current consumption is 200 uA"
"instead of 80 uA. The TP1 output is noisy. We replace FHI with BIT, after the scheme"
"we implemented in the A3030D firmware, and later we xor BIT with TCK to generate the"
"output frequency DAC value. When we re-name the FHI signal, we find that it was"
"declared in V06 firmware as an output pin at no fixed location. We make it a node and"
"re-compile. The TP1 output now looks good and current consumption is 80 uA. We go back"
"to V06 and convert FHI into a node. With frequenc_low = 7 current consumption is 80 uA."
"But we note that the TP1 output has glitches. In our V07 firmware, where we generate"
"the frequency DAC value with the VCK clock, the TP1 output has no glitches. We find"
"that we must keep the BIT node or else the F1..F4 calculation becomes so large with"
"x_id = 7 that it cannot settle in time for the VCK edge."

"Version 8 [11-SEP-15] Add support for some new versions for reliability tests."
 
"Version 9 [03-NOV-15] Now take fck_divisor and set up ring oscillator length"
"and the divisor for the transmit clock counter so as to give finer resolution"
"in obtaining TCK period."

"Version 10 [17-NOV-15] We add support for the A3028AV5 circuit, which has a 160-Hz"
"low-pass filter on the bottom-side amplifier, and an 80-Hz low-pass filter on the"
"top-side amplifier. We can now make an 80-Hz single-channel or 160-Hz single-channel"
"device with the same circuit, provided we can use the Y input alone when we want to"
"make an 80-Hz device."

"Version 11 [29-JAN-16] Extend ring oscillator to a maximum of 13-gates so as to"
"provide division by 26 for TCK period."

"Versin 12 [13-OCT-16] Enhance the four-bit consistency code at the end so that it"
"implements the Completion Code protocol, in which a Set Identifier is combined"
"with the Channel Number (ID) to produce the Completion Code."

"Version 13 [07-FEB-17] Add support for eight-bit channel numbers, in which we"
"calculate the set number and channel id from the larger set numbers. Add support"
"for the A3028M dual-channel 2048 SPS transmitter."

"Version 14 [31-MAY-17] Add support for A3028V 496 SPS on X and 16 SPS on Y. The"
"samples are transmittted at 512 SPS, with one in 32 being a Y sample. Change several"
"node and set names and re-work the YSEL calculation by adding YCONV. We calculate"
"transmit scatter automatically from the value of ck_divisor. Include compiler"
"directives to eliminate uneccessary nodes in the hope that we will always be able"
"to fit the compiled code into sixty-four gates. The maximum number of gates required"
"is 61 for VERSION = 9, fck_divisor = 26. The minimum number is 43 for VERSION = 11,"
"fck_divisor = 22. The new VERSION = 12 requires no more than 60 gates."

"Version 15 [03-OCT-17] Add regular_sampling parameter to sample ADC at regular"
"intervals while still scattering the transmission instant."

declarations


"Configuration Parameters"
"========================"

"Version | Number | Description"
"------------------------------"  
"   A       2      dual-channel 512 SPS, 48 mA-hr"
"   B       1      single-channel 512 SPS, 48 mA-hr, using X"
"   C       8      single-channel 256 SPS, 48 mA-hr, using Y."
"   D       2      dual-channel 512 SPS, 255 mA-hr"
"   E       1      single-channel 512 SPS, 255 mA-hr, using X"
"   F       4      dual-channel 1024 SPS, 48 mA-hr"
"   G       4      dual-channel 1024 SPS, 255 mA-hr"
"   H       6      dual-channel 256 SPS, 48 mA-hr"
"   J       2      dual-channel 512 SPS, 48 mA-hr, EEG/EMG"
"   K       9      single-channel 128 SPS, 48 mA-hr, using Y."
"   L       4      dual-channel 1024 SPS, 1000 mA-hr."
"   M       11     dual-channel 2048 SPS, 48 mA-hr holder"
"   N       1      single-channel 512 SPS, 48 mA-hr holder, using X"
"   P       1      single-channel 512 SPS, 11 mA-hr"
"   Q       4      dual-channel 1024 SPS, 560 mA-hr"
"   U       13     dual-channel 512 SPS, 255 mA-hr, no high-pass filter"
"   V       12     dual-channel 496 SPS X, 16 SPS Y, 48 mA-hr."


"Number | Description"
"--------------------"  
"  1      single-channel 512 SPS, using X"
"  2      dual-channel 512 SPS"
"  3      single-channel 1024 SPS, using X"
"  4      dual-channel 1024 SPS"
"  5      single-channel 256 SPS, using X" 
"  6      dual-channel 256 SPS"
"  7      dual-chanbel 128 SPS"
"  8      single-channel 256 SPS, using Y."
"  9      single-channel 128 SPS, using Y."
"  10     dual-channel 128 SPS"
"  11     dual-channel 2048 SPS"
"  12     dual-channel 496 SPS X, 16 SPS Y."
"  13     dual-channel 512 SPS, regular sampling."


"Set the transmitter version number"
VERSION = 13;

"The base channel number is the channel number of the first transmit signal"
"If only X is enabled, it is the X channel number. If only y is enabled, it"
"is the Y channel number. If both are enabled, X is the base number and Y is"
"the base number plus one. The base channel number can be 1-14, 17-30, 33-46,"
"49-62, 65-78, 81-94, 97-110, 113-126, 129-142, 145-158, 161-174, 177-190,"
"193-206, 209-222. These ranges correspond to sets 0-13 respectively,"
base_channel_num = 37;

"The set number is the channel number divided by sixteen."
set_num = base_channel_num / 16;

"The base identifier is the channel number modulo sixteen."
base_id = base_channel_num % 16;


"Calibration Parameters"
"======================"

"Fast Clock Divisor, use to set TCK period in range 195-215 ns. Supported"
"range for fck_divisor is 8 to 30."
fck_divisor = 25; 

"Frequency Low, use to center transmit spectrum in range 913-918 MHz."
frequency_low = 7;


"Parameters" 
"=========="

"Version-Dependent Parameters Set Automatically"
@IF (VERSION == 1) {
  ck_divisor=64; "Total Sample Rate 512 SPS
  enable_x=1; "X Input Enabled 512 SPS"
  enable_y=0; "Y Input Disabled 0 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 2) {
  ck_divisor=32; "Total Sample Rate 1024 SPS
  enable_x=1; "X Input Enabled 512 SPS"
  enable_y=1; "Y Input Enabled 512 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 3) {
  ck_divisor=32; "Total Sample Rate 1024 SPS"
  enable_x=1; "X Input Enabled 1024 SPS"
  enable_y=0; "Y Input Disabled 0 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 4) {
  ck_divisor=16; "Total Sample Rate 2048 SPS"
  enable_x=1; "X Input Enabled 1024 SPS"
  enable_y=1; "Y Input Enabled 1024 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 5) {
  ck_divisor=128; "Total Sample Rate 256 SPS"
  enable_x=1; "X Input Enabled 256 SPS"
  enable_y=0; "Y Input Disabled 0 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 6) {
  ck_divisor=64; "Total Sample Rate 512 SPS"
  enable_x=1; "X Input Enabled 256 SPS"
  enable_y=1; "Y Input Enabled 256 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 7) {
  ck_divisor=128; "Total Sample Rate 256 SPS"
  enable_x=1; "X Input Enabled 128 SPS"
  enable_y=1; "Y Input Enabled 128 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 8) {
  ck_divisor=128; "Total Sample Rate 256 SPS"
  enable_x=0; "X Input Disabled 0 SPS"
  enable_y=1; "Y Input Enabled 256 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 9) {
  ck_divisor=256; "Total Sample Rate 128 SPS"
  enable_x=1; "X Input Enabled 128 SPS"
  enable_y=0; "Y Input Disabled 0 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 10) {
  ck_divisor=128; "Total Sample Rate 256 SPS"
  enable_x=1; "X Input Enabled 128 SPS"
  enable_y=1; "Y Input Enabled 128 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 11) {
  ck_divisor=8; "Total Sample Rate 4096 SPS"
  enable_x=1; "X Input Enabled 2048 SPS"
  enable_y=1; "Y Input Enabled 2048 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 12) {
  ck_divisor=64; "Total Sample Rate 512 SPS"
  enable_x=1; "X Input Enabled 496 SPS"
  enable_y=31; "Y Input Enabled 16 SPS"
  regular_sampling=0; "Sample with scatter"
}
@IF (VERSION == 13) {
  ck_divisor=32; "Total Sample Rate 1024 SPS
  enable_x=1; "X Input Enabled 512 SPS"
  enable_y=1; "Y Input Enabled 512 SPS"
  regular_sampling=1; "Sample regularly even with scatter"
}


"Set the transmit clock divisor, tck_divisor, and the ring oscillator length,"
"ring_length, to suit fck_divisor. The TCK period will be two gate delays"
"multiplied by tck_divisor multiplied by ring_length. For 7.5-ns chips, two"
"internal gate delays are roughly 9.3 ns. The ring length must be at least 3."
"A ring length of 2 runs too fast, causing glitches and counter failure."
"The maximum ring length in this code is 13, but ultimately is limited by the"
"available logic outputs. The tck_divisor must be 2 or greater. We need at least"
"two divider states to create a symmetric transmit clock signal. And tck_divisor"
"must also be less than 32 because we have at most five divisor bits in this"
"code. As a result of these restrictions, some fck_divisor values do not have"
"their own unique and correct combination of tck_divisor * ring_length. These are"
"values 11, 13, 17, 19, 23, 29, 31, and 34."
@IF (fck_divisor == 8)  {tck_divisor = 2; ring_length = 4;}
@IF (fck_divisor == 9)  {tck_divisor = 3; ring_length = 3;}
@IF (fck_divisor == 10) {tck_divisor = 2; ring_length = 5;}
@IF (fck_divisor == 11) {tck_divisor = 2; ring_length = 5;}
@IF (fck_divisor == 12) {tck_divisor = 4; ring_length = 3;}
@IF (fck_divisor == 13) {tck_divisor = 4; ring_length = 3;}
@IF (fck_divisor == 14) {tck_divisor = 2; ring_length = 7;}
@IF (fck_divisor == 15) {tck_divisor = 5; ring_length = 3;}
@IF (fck_divisor == 16) {tck_divisor = 4; ring_length = 4;}
@IF (fck_divisor == 17) {tck_divisor = 4; ring_length = 4;}
@IF (fck_divisor == 18) {tck_divisor = 6; ring_length = 3;}
@IF (fck_divisor == 19) {tck_divisor = 6; ring_length = 3;}
@IF (fck_divisor == 20) {tck_divisor = 5; ring_length = 4;}
@IF (fck_divisor == 21) {tck_divisor = 7; ring_length = 3;}
@IF (fck_divisor == 22) {tck_divisor = 2; ring_length = 11;}
@IF (fck_divisor == 23) {tck_divisor = 8; ring_length = 3;}
@IF (fck_divisor == 24) {tck_divisor = 8; ring_length = 3;}
@IF (fck_divisor == 25) {tck_divisor = 5; ring_length = 5;}
@IF (fck_divisor == 26) {tck_divisor = 2; ring_length = 13;}
@IF (fck_divisor == 27) {tck_divisor = 9; ring_length = 3;}
@IF (fck_divisor == 28) {tck_divisor = 7; ring_length = 4;}
@IF (fck_divisor == 29) {tck_divisor = 7; ring_length = 4;}
@IF (fck_divisor == 30) {tck_divisor = 6; ring_length = 5;}
@IF (fck_divisor == 31) {tck_divisor = 6; ring_length = 5;}
@IF (fck_divisor == 32) {tck_divisor = 8; ring_length = 4;}
@IF (fck_divisor == 33) {tck_divisor = 11; ring_length = 3;}
@IF (fck_divisor == 34) {tck_divisor = 11; ring_length = 3;}
@IF (fck_divisor == 35) {tck_divisor = 7; ring_length = 5;}
@IF (fck_divisor == 36) {tck_divisor = 9; ring_length = 4;}

"Other Parameters"
frequency_step=2; "HI frequency - LO frequency"
enable_rf=1; "Turns on RF oscillator during transmission"
@IF (enable_x == 1) {
  x_id = base_id;
  y_id = base_id + 1;
} 
@IF (enable_x == 0) {
  x_id = base_id + 1;
  y_id = base_id;
}

"Channel ID"
I3..I0 node istype 'com'; "Transmitter ID nodes"
id = [I3..I0];

"Completion Code"
CC3..CC0 node istype 'com'; "Completion Code Bits"
cc =[CC3..CC0];


"Inputs and Outputs"
"=================="

CK pin K9; "Clock From 32-kHz Oscillator"
F4..F0 pin C10,D10,E10,G10,H10 istype 'reg'; "DAC for frequency"
!SHDN pin A1 istype 'com'; "Shutdown Control for Transmitter"
TP1 pin A7 istype 'com'; "Test Point"
TP2 pin F1 istype 'com'; "Test Point"
TP3 pin G1 istype 'com'; "Test Point"
CONV pin H1 istype 'com'; "Convert for ADC"
SDO pin K4; "Serial Data Out for ADC"
SCK pin K5 istype 'com'; "Serial Clock for ADC"
SDI pin H4 istype 'com,pos'; "Serial Data In for ADC"


"Nodes"
"====="

FCK node istype 'com,keep'; "Fast Clock"
TCK node istype 'reg,keep'; "Transmission Clock"
ECK node istype 'reg,keep'; "End Clock"
VCK node istype 'reg,keep'; "VCO Clock"
ST0..ST8 node istype 'reg'; "Sample Timer"
SCNT0..SCNT8 node istype 'reg'; "Sample Counter"
R1..R12 node istype 'com,keep'; "Ring Oscillator Bit"
TXS0..TXS5 node istype 'reg,pos'; "Transmitter State"
ACTIVE node istype 'reg,keep'; "Active period of 32-kHz"
TXD node istype 'com,keep'; "Transmitter Done"
TCKD0..TCKD4 node istype 'reg'; "Transmit Clock Divider"
TCKDZ node istype 'reg,keep'; "Transmit Clock Divider Zero"
ADC0..ADC3 node istype 'reg'; "ADC Bits"
TTS0..TTS3 node istype 'reg'; "Transmit Time Shift"
SDOS node istype 'reg,keep'; "SDO Synchronized"
YSEL node istype 'reg'; "Select Channel Y for Transmission"
YCONV node istype 'reg'; "Select Channel Y for Conversion"
BIT node istype 'com,keep'; "The output bit value"


"Sets"
"===="

"Sample Timer, depends upon ck_divisor to eliminate unused bits."
"Likewise, the active time, which is the moment during the period"
"at which we transmit a sample, depends upon how many bits we have"
"in the Sample Timer."
@IF (ck_divisor == 4) {
  "Frequency 8192 SPS, scatter is +-1 ticks.
  st = [ST1..ST0];
  active_time = [0,TTS0];
}
@IF (ck_divisor == 8) {
  "Frequency 4096 SPS, scatter is +-2 ticks.
  st = [ST2..ST0];
  active_time = [0,TTS1,TTS0];
}
@IF (ck_divisor == 16) {
  "Frequency 2048 SPS, scatter is +-4 ticks.
  st = [ST3..ST0];
  active_time = [0,TTS2,TTS1,TTS0];
}
@IF (ck_divisor == 32) {
  "Frequency 1024 SPS, scatter is +-8 ticks.
  st = [ST4..ST0];
  active_time = [0,TTS3,TTS2,TTS1,TTS0];
}
@IF (ck_divisor == 64) {
  "Frequency 512 SPS, scatter is +-8 ticks.
  st = [ST5..ST0];
  active_time = [0,0,TTS3,TTS2,TTS1,TTS0];
}
@IF (ck_divisor == 128) {
  "Frequency 256 SPS, scatter is +-8 ticks.
  st = [ST6..ST0];
  active_time = [0,0,0,TTS3,TTS2,TTS1,TTS0];
}
@IF (ck_divisor == 256) {
  "Frequency 128 SPS, scatter is +-8 ticks.
  st = [ST7..ST0];
  active_time = [0,0,0,0,TTS3,TTS2,TTS1,TTS0];
}
@IF (ck_divisor == 512) {
  "Frequency 64 SPS, scatter is +-8 ticks.
  st = [ST8..ST0];
  active_time = [0,0,0,0,0,TTS3,TTS2,TTS1,TTS0];
}

"Sample Counter, depends upon enable_y to eliminate unused bits."
@IF (enable_y <= 15) {
  scnt =[SCNT3..SCNT0];
}
@IF (enable_y == 31) {
  scnt =[SCNT4..SCNT0];
}
@IF (enable_y == 63) {
  scnt =[SCNT5..SCNT0];
}
@IF (enable_y == 127) {
  scnt =[SCNT6..SCNT0];
}
@IF (enable_y == 255) {
  scnt =[SCNT7..SCNT0];
}
@IF (enable_y == 511) {
  scnt =[SCNT8..SCNT0];
}

"Transmit Clock Divider, depends upon tck_divisor to eliminate unused"
"bits"
@IF (tck_divisor <= 4) {
  tckd = [TCKD1..TCKD0];
}
@IF (tck_divisor >= 5) & (tck_divisor <= 8) {
  tckd = [TCKD2..TCKD0];
}
@IF (tck_divisor >= 9) & (tck_divisor <= 16) {
  tckd = [TCKD3..TCKD0];
}
@IF (tck_divisor >= 17) {
  tckd = [TCKD4..TCKD0];
}

txs = [TXS5..TXS0]; "Transmitter State"
adc_bits = [ADC3..ADC0]; "ADC Bits"
transmit_time_shift = [TTS3..TTS0]; "Transmit Time Shift"
frequency = [F4..F0]; "Frequency Voltage for Five-Bit DAC"


"Constants"
"========="

num_sync_bits=11; "Number of synchronizing bits at transmission start."
num_id_bits = 4; "Number of ID bits"
num_start_bits = 1; "Transmitted zero to mark data start"
num_stop_bits = 2; "Not transmitted, for txs termination"
num_data_bits = 16; "Number of ADC data bits"
num_xmit_bits = "Number of transmission bit periods"
    num_sync_bits
  + num_start_bits
  + num_id_bits
  + num_data_bits
  + num_id_bits; 
txs_done = "Final state of txs machine"
    num_xmit_bits
  + num_stop_bits; 
first_sync_bit = 1;
first_start_bit = first_sync_bit + num_sync_bits;
first_id_bit = first_start_bit + num_start_bits;
first_data_bit = first_id_bit + num_id_bits;
first_iid_bit = first_data_bit + num_data_bits;
start_sck = "The txs state for first SCK falling edge"
    first_data_bit - 1;
end_sck = "The txs state for last SCK falling edge"
    start_sck + num_data_bits - 1;


equations

"The Sample Timer runs off the 32.678-kHz clock and counts up to"
"ck_divisor-1 to give a sample period of 32.768 kHz divided"
"by ck_divisor."
st.clk=CK;
when (st==ck_divisor-1) then {
  st:=0;
} else {
  st:=st+1;
}

"The ECK clock occurs at the end of each sample period."
ECK.clk=CK;
ECK:=(st==ck_divisor-2);

"The Sample Counter counts sample periods and allows us to decide"
"which input to digitize and which channel number to apply to each"
"sample transmission. We increment the counter at the end of each"
"sample period. When the counter reaches enable_y, it returns to zero."
scnt.clk=ECK;
when (scnt == enable_y) then {
  scnt:=0;
} else {
  scnt:=scnt+1;
}

"We assert YSEL when the next transmit cycle's sample transmission"
"should receive the Y-input channel number."
YSEL.clk=ECK;
YSEL:=((scnt == 1) & (enable_y > 0)) # (enable_x == 0);

"We assert YCONV when the conversion that occurs at the end of the"
"next sample transmission should be a conversion of the Y-input."
YCONV.clk=ECK;
YCONV:= ((scnt == 0) & (enable_y > 0)) # (enable_x == 0);

"When ACTIVE is asserted, we begin a burst transmission."
"When it is unasserted, we reset the burst transmission"
"state machine. We must make sure that ACTIVE remains true"
"for long enough for the burst transmission to complete."
"ACTIVE becomes true when the Sample Timer reaches the active"
"time set at the end of the previous sample period. This"
"active time is made up of the lower four bits of the sample"
"transmitted in the previous sample period. Because these"
"four bits are dominated by noise, they are random and so"
"produce a random disturbance of the transmit instant, which"
"avoids systematic collisions between transmitters."
ACTIVE.clk=CK;
ACTIVE:=(st==active_time);

"TXD is true when the transmitter completes its burst"
"transmission."
TXD=(txs==txs_done);

"We change the channel ID depending upon whether we are"
"transmitting X or Y."
when YSEL then {
  id = y_id;
  cc = 15 - y_id + set_num;
} else {
  id = x_id;
  cc = 15 - x_id + set_num;
}

"The ring oscillator turns on when ACTIVE and remains"
"on until TXD. Each gate in the ring adds 2 ns to the"
"delay around the ring. The period of the oscillation is"
"4 ns multiplied by the number of gates."
@IF (ring_length == 2) {
  [FCK,R1]=[R1,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 3) {
  [FCK,R1..R2]=[R1..R2,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 4) {
  [FCK,R1..R3]=[R1..R3,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 5) {
  [FCK,R1..R4]=[R1..R4,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 6) {
  [FCK,R1..R5]=[R1..R5,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 7) {
  [FCK,R1..R6]=[R1..R6,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 8) {
  [FCK,R1..R7]=[R1..R7,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 9) {
  [FCK,R1..R8]=[R1..R8,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 10) {
  [FCK,R1..R9]=[R1..R9,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 11) {
  [FCK,R1..R10]=[R1..R10,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 12) {
  [FCK,R1..R11]=[R1..R11,!FCK & ACTIVE & !TXD];
}
@IF (ring_length == 13) {
  [FCK,R1..R12]=[R1..R12,!FCK & ACTIVE & !TXD];
}


"The transmit clock divider runs off FCK and divides FCK down"
"to 5 MHz by correct choice of fck_divisor during transmitter"
"calibration. We compute two constants from fck_divisor. One"
"is tck_divisor, which sets the transmit clock period as a"
"multiple of the fast clock period. The other is ring_length,"
"which sets the number of gates in the ring oscillator that"
"generates the fast clock. We enable the transmit clock divider"
"only when the transmitter is active."
tckd.aclr=!ACTIVE;
tckd.clk=FCK;
when (tckd==tck_divisor-1) then {
  tckd:=0;
} else {
  tckd:=tckd+1;
}

"We detect the transmit clock divider being zero with TCKDZ. We"
"clear TCKDZ to zero when the transmitter is inactive."
TCKDZ.aclr=!ACTIVE;
TCKDZ.clk=FCK;
TCKDZ:=(tckd==0);

"The transmit clock should be close to or a little less than 5 MHz,"
"with a duty cycle of exactly 50%. Each time the transmit clock counts"
"down to zero, we invert the transmit clock."
TCK.aclr=!ACTIVE;
TCK.clk=TCKDZ;
TCK:=!TCK;

"The transmitter state machine steps through all its"
"states when ACTIVE is asserted, and then stops in its"
"final state, waiting for !ACTIVE, which will reset the"
"transmitter state to zero."
txs.aclr=!ACTIVE;
txs.clk=TCK;
when (txs==txs_done) then txs:=txs
else txs:=txs+1;

"Transmit sixteen ADC bits."
when (txs>0) & (txs=1)&(txs=first_data_bit)&(txs=start_sck) & (txs<=end_sck) & !TCK;

"We configure the ADC for single-ended input by setting"
"SDI to 1 on the first rising edge of SCK after CONV goes"
"low."
when txs==start_sck then SDI=1;

"We select ADC channel that will be digitized at the end"
"of the CONV pulse with the value of SDI on the second"
"rising edge of SCK after CONV goes low. With SDI = 0, the"
"next sample from the ADC will be of X. But note that this"
"selection does not affect the sample being read out by SCK"
"during the CONV low pulse. The YSEL signal tells us that"
"Y is being read out now, and YCONV means we should digitize"
"Y at the end of this transmission."
when txs==start_sck+1 then SDI=YCONV;

"Test Points"
TP1=(frequency==frequency_low+frequency_step);
TP2=YSEL;
TP3=YCONV;

END