Skip to content

Commit 29d2fdc

Browse files
committed
Resolving copied code between ph_acc.v and rot_dds.v
1 parent 4ae8da6 commit 29d2fdc

File tree

3 files changed

+47
-45
lines changed

3 files changed

+47
-45
lines changed

dsp/ph_acc.v

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,43 @@
44
// Tuned to allow 32-bit control, divided 20-bit high and 12-bit low,
55
// which gets merged to 32-bit binary when modulo is zero.
66
// But also supports non-binary frequencies: see the modulo input port.
7+
8+
// Note that phase_step_h and phase_step_l combined fit in a 32-bit word.
9+
// This is intentional, to allow atomic updates of the two controls
10+
// in 32-bit systems. Indeed, when modulo==0, those 32 bits can be considered
11+
// a single phase step increment
12+
13+
// The phase generation algorithm
14+
// 0. The phase increments for dds are generated using a technique described
15+
// in these 2 places:
16+
// Section: PROGRAMMABLE MODULUS MODE in:
17+
// https://www.analog.com/media/en/technical-documentation/data-sheets/ad9915.pdf
18+
// (AND) https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
19+
// Basically, increment the phase step at a coarse resolution, accumulate the
20+
// error on the side, and when that error accumulates to the lowest bit of
21+
// the coarse counter, add an extra 1 to the following phase step.
22+
// 1. phase_step_h is the coarse (20 bit) integer truncated phase increment for
23+
// the cordic. There is a 1-bit increment of phase that comes from
24+
// accumulating residue phase.
25+
// 2. This residue phase is accumulating in steps of phase_step_l, in a 12-bit
26+
// counter.
27+
// 3. However, there will be an extra residue even for this 12-bit counter,
28+
// which is the modulus, and this added as an offset when the counter crosses 0
29+
30+
// 12-bit modulo supports largest known periodicity in a suggested LLRF system,
31+
// 1427 for JLab. For more normal periodicities, use a multiple to get finer
32+
// granularity.
33+
// Note that the downloaded modulo control is the 2's complement of the
34+
// mathematical modulus.
35+
// e.g., SSRF IF/F_s ratio 8/11, use
36+
// modulo = 4096 - 372*11 = 4
37+
// phase_step_h = 2^20*8/11 = 762600
38+
// phase_step_l = (2^20*8%11)*372 = 2976
39+
// e.g., Argonne RIA test IF/F_s ratio 9/13, use
40+
// modulo = 4096 - 315*13 = 1
41+
// phase_step_h = 2^20*9/13 = 725937
42+
// phase_step_l = (2^20*9%13)*315 = 945
43+
744
module ph_acc(
845
input clk, // Rising edge clock input; all logic is synchronous in this domain
946
input reset, // Active high, synchronous with clk

dsp/rot_dds.v

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,6 @@
1010
// 8/11 for SSRF
1111
// 9/13 for Argonne RIA
1212
//
13-
// The phase generation algorithm
14-
// 0. The phase increments for dds are generated using a technique described
15-
// in these 2 places:
16-
// Section: PROGRAMMABLE MODULUS MODE in:
17-
// https://www.analog.com/media/en/technical-documentation/data-sheets/ad9915.pdf
18-
// (AND) https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
19-
// Basically, increment the phase step at a coarse resolution, accumulate the
20-
// error on the side, and when that error accumulates to the lowest bit of
21-
// the coarse counter, add an extra 1 to the following phase step.
22-
// 1. phase_step_h is the coarse (20 bit) integer truncated phase increment for
23-
// the cordic. There is a 1-bit increment of phase that comes from
24-
// accumulating residue phase.
25-
// 2. This residue phase is accumulating in steps of phase_step_l, in a 12-bit
26-
// counter.
27-
// 3. However, there will be an extra residue even for this 12-bit counter,
28-
// which is the modulus, and this added as an offset when the counter crosses 0
29-
30-
// 12-bit modulo supports largest known periodicity in a suggested LLRF system,
31-
// 1427 for JLab. For more normal periodicities, use a multiple to get finer
32-
// granularity.
33-
// Note that the downloaded modulo control is the 2's complement of the
34-
// mathematical modulus.
35-
// e.g., SSRF IF/F_s ratio 8/11, use
36-
// modulo = 4096 - 372*11 = 4
37-
// phase_step_h = 2^20*8/11 = 762600
38-
// phase_step_l = (2^20*8%11)*372 = 2976
39-
// e.g., Argonne RIA test IF/F_s ratio 9/13, use
40-
// modulo = 4096 - 315*13 = 1
41-
// phase_step_h = 2^20*9/13 = 725937
42-
// phase_step_l = (2^20*9%13)*315 = 945
43-
44-
// TODO:
45-
// Potentially, isolate phase generation into a separate module.
46-
// Haha, turns out there is ph_acc.v (We should USE it).
4713
// Synthesizes to ??? slices at ??? MHz in XC3Sxxx-4 using XST-??
4814
//
4915

@@ -65,19 +31,18 @@ parameter lo_amp = 18'd79590;
6531
// Sometimes we cheat and use slightly smaller values than above,
6632
// to make other computations fit better.
6733

34+
wire [18:0] phase_acc;
35+
ph_acc ph_acc_i (
36+
.clk(clk), .reset(reset), .en(1'b1), // input
37+
.phase_acc(phase_acc), // output [18:0]
38+
.phase_step_h(phase_step_h), // input [19:0]
39+
.phase_step_l(phase_step_l), // input [11:0]
40+
.modulo(modulo) // input [11:0]
41+
);
6842
// See rot_dds_config
6943

70-
reg carry=0, reset_d=0;
71-
reg [19:0] phase_h=0, phase_step_hp=0;
72-
reg [11:0] phase_l=0;
73-
always @(posedge clk) begin
74-
{carry, phase_l} <= reset ? 13'b0 : ((carry ? modulo : 12'b0) + phase_l + phase_step_l);
75-
phase_step_hp <= phase_step_h;
76-
reset_d <= reset;
77-
phase_h <= reset_d ? 20'b0 : (phase_h + phase_step_hp + carry);
78-
end
7944
cordicg_b22 #(.nstg(20), .width(18), .def_op(0)) trig(.clk(clk), .opin(2'b00),
80-
.xin(lo_amp), .yin(18'd0), .phasein(phase_h[19:1]),
45+
.xin(lo_amp), .yin(18'd0), .phasein(phase_acc),
8146
// 2^17/1.64676 = 79594, use a smaller value to keep CORDIC round-off
8247
// from overflowing the output
8348
.xout(cosa), .yout(sina));

dsp/rules.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bits: $(BITS_)
3030
$(VVP) $< +trace
3131
$(PYTHON) $(word 2, $^) -f $*
3232

33-
rot_dds_tb: cordicg_b22.v
33+
rot_dds_tb: cordicg_b22.v ph_acc.v
3434

3535
mon_12_tb: cordicg_b22.v
3636

0 commit comments

Comments
 (0)