Skip to content

Commit

Permalink
Merge branch 'master' into pps_lock_hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
ldoolitt committed Jan 29, 2024
2 parents fa08bcd + cc3fa4b commit 2078d26
Show file tree
Hide file tree
Showing 33 changed files with 148 additions and 47 deletions.
112 changes: 93 additions & 19 deletions badger/tests/spi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,84 @@ def clear_status(s):
return


def prog_status(s):
good = True
clear_status(s)
status, cnf = read_status_config(s)
if (status & 0x3) != 0:
logging.info("Status 0x%2.2x: clear errors first" % status)
good = False
# require TBPROT set, BPNV clear
# ignore DNU and TBPARM at least for now
if (cnf & 0x28) != 0x20:
logging.info("CONFIG_REG 0x%2x OTP bits not good for programming!" % cnf)
good = False

return good, status, cnf


def set_block_protect(s, protect=True):
good = True
clear_status(s)
status, cnf = read_status_config(s)
if protect:
status1 = status | 0x1C # attempt to force all BP bits to one
else:
status1 = status & 0xE3 # attempt to force all BP bits to zero

write_status(s, status1)
time.sleep(0.3) # empirically needed,
read_id(s) # otherwise status2 comes back not updated
status2, cnf = read_status_config(s)
if status2 != status1:
logging.info("Failed to set status from 0x%2.2x to 0x%2.2x, now 0x%2.2x" % (status, status1, status2))
logging.info("Check Write-Protect switch")
good = False

return good, status, cnf


def enable_wp(s):
return set_block_protect(s, protect=True)


def disable_wp(s):
return set_block_protect(s, protect=False)


def set_prog(s, prog=True):
prog_good, _, _ = prog_status(s)
if not prog_good:
return False

protect = not prog
write_good, _, _ = set_block_protect(s, protect=protect)
if not write_good:
return False

return True


def enable_prog(s):
set_prog(s, prog=True)


def disable_prog(s):
set_prog(s, prog=False)


def check_prog(s):
good, status, cnf = prog_status(s)
logging.info('Status Reg: 0x%02x, Config Reg: 0x%02x' % (status, cnf))

if good and ((status & 0x1C) == 0):
print("Flash is writable")
return True
else:
print("Flash is NOT writable")
return False


# Not currently used
def reset_chip(s):
p = RESET_CHIP
Expand Down Expand Up @@ -450,7 +528,9 @@ def main():
help='Clear status (CLSR)')
parser.add_argument('--force_write_enable', action='store_true',
help='Configure flash to write normally protected blocks; only works if WE# pin is high')
parser.add_argument('--check_prog', action='store_true', help="Check if flash is writable")
if EXPERT:
parser.add_argument('--disable_wp', action='store_true', help="Disable Write-Protect mechanism")
parser.add_argument('--status_write', type=lambda x: int(x, 0),
help='A value to be written to status register (Experts only)')
parser.add_argument('--config_write', type=lambda x: int(x, 0),
Expand Down Expand Up @@ -501,29 +581,17 @@ def main():
if size > 7*1024*1024:
print("Too big!")
exit(1)
# XXX refactor this into a function
clear_status(sock)
status, cnf = read_status_config(sock)
if (status & 0x3) != 0:
print("Status 0x%2.2x: clear errors first" % status)
exit(1)
# require TBPROT set, BPNV clear
# ignore DNU and TBPARM at least for now
if (cnf & 0x28) != 0x20:
print("CONFIG_REG 0x%2x OTP bits not good for programming!" % cnf)

prog_good, _, _ = prog_status(sock)
if not prog_good:
exit(1)

if args.force_write_enable:
if args.verify:
print("Don't force and verify at the same time")
exit(1)
status1 = status & 0xE3 # attempt force all BP bits to zero
write_status(sock, status1)
time.sleep(0.3) # empirically needed,
read_id(sock) # otherwise status2 comes back not updated
status2, cnf = read_status_config(sock)
if status2 != status1:
print("Failed to set status from 0x%2.2x to 0x%2.2x, now 0x%2.2x" % (status, status1, status2))
print("Check Write-Protect switch")
write_good = disable_wp(sock)
if not write_good:
exit(1)
if args.verify:
ok = remote_verify(sock, prog_file, ad, size)
Expand All @@ -533,7 +601,13 @@ def main():
remote_erase(sock, ad, size)
remote_program(sock, prog_file, ad, size)
if args.force_write_enable:
write_status(sock, status) # back to what it was
enable_wp(sock) # back to what it was

if args.check_prog:
check_prog(sock)

if EXPERT and args.disable_wp is not None:
enable_prog(sock)

if args.erase:
remote_erase(sock, ad, args.erase)
Expand Down
3 changes: 1 addition & 2 deletions cmoc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ include $(BADGER_DIR)/rules.mk

NEWAD_ARGS += -y

VFLAGS += -Wno-macro-redefinition
TEST_BENCH = xy_pi_clip_tb fdbk_core_tb tgen_tb circle_buf_tb cryomodule_tb cryomodule_badger_tb rf_controller_tb

RTEFI_CLIENT_LIST = hello.v speed_test.v mem_gateway.v
Expand Down Expand Up @@ -55,7 +54,7 @@ cryomodule_check: cryomodule.dat
$(AUTOGEN_DIR)/config_romx.v:
mkdir -p $(AUTOGEN_DIR); $(PYTHON) $(BUILD_DIR)/config_crunch.py --OUTPUT=$@

VFLAGS_DEP += -y $(BADGER_DIR) -Wno-macro-redefinition
VFLAGS_DEP += -y $(BADGER_DIR)

cryomodule_badger_tb.v: $(RTEFI_V) $(AUTOGEN_DIR)/config_romx.v cordicg_b22.v cryomodule_auto

Expand Down
3 changes: 3 additions & 0 deletions cmoc/cryomodule.v
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,7 @@ always @(posedge lb_clk) begin
end
assign lb_out = lb_out_r;

`undef SAT
`undef UNIFORM

endmodule
3 changes: 3 additions & 0 deletions dsp/ccfilt.v
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ always @(posedge clk) begin
end
reg valid3=0;
always @(posedge clk) valid3 <= valid2;
`undef UNIFORM

`ifdef SIMULATE
reg [3:0] ch_id=0;
Expand All @@ -81,6 +82,8 @@ always @(posedge clk) begin
valid4 <= valid3;
end

`undef SAT

// Instantiate half-band filter .. or not
wire [outw-1:0] d5;
wire valid5;
Expand Down
2 changes: 1 addition & 1 deletion dsp/chirp/chirp_driver.v
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module chirp_driver #(
.opin (2'b00),
.xin (18'b0),
.yin (cordic_amp),
.phasein ({cordic_phase}),
.phasein (cordic_phase),
.xout (cordic_cos),
.yout (cordic_sin),
.phaseout ()
Expand Down
1 change: 1 addition & 0 deletions dsp/complex_mul.v
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ always @(posedge clk) begin
end
assign z = zr[dw:1];
assign z_all = mux_r;
`undef SAT

// This gate input isn't really used, but describes the length of this
// pipeline to let users keep track of the data flow.
Expand Down
1 change: 1 addition & 0 deletions dsp/complex_mul_flat.v
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ always @(posedge clk) begin
I_small <= `SAT(z_I_sel, 19, 18);
Q_small <= `SAT(z_Q_sel, 19, 18);
end
`undef SAT

assign z_I_all = z_I_all_i;
assign z_Q_all = z_Q_all_i;
Expand Down
3 changes: 2 additions & 1 deletion dsp/cpxmul_fullspeed.v
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module cpxmul_fullspeed #(
reg signed [DWI*2-1:0] sum1=0, sum2=0, sum3=0;
reg signed [DWI*2-OUT_SHIFT-1:0] sum1_r=0, sum2_r=0;

`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})
`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})
always @(posedge clk) begin
mul1 <= re_a*re_b;
mul2 <= im_a*im_b;
Expand All @@ -43,5 +43,6 @@ module cpxmul_fullspeed #(

assign re_out = `SAT(sum1_r, DWI*2-OUT_SHIFT-1, OWI-1);
assign im_out = `SAT(sum2_r, DWI*2-OUT_SHIFT-1, OWI-1);
`undef SAT

endmodule
3 changes: 3 additions & 0 deletions dsp/digaree/sf_main.v
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ assign r = sat;
assign valid_o = valid_r;
assign sat_happened = valid_r & sat_happened_r;

`undef SAT
`undef UNSAT

endmodule

// ALU and register file, no instruction sequencing
Expand Down
2 changes: 2 additions & 0 deletions dsp/fdownconvert.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ always @(posedge clk) begin
time_err_r <= (mod2 ^ ~last_mod2) | ~a_gate;
end

`undef SAT

assign o_data = iq_out0;
assign o_gate = 1'b1;
assign o_trig = mod2;
Expand Down
2 changes: 2 additions & 0 deletions dsp/flevel_set.v
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ always @(posedge clk) begin
time_err_r <= ~i_gate | ~q_gate;
end

`undef SAT

assign o_data = sum2[16:1];
assign o_gate = 1'b1;
assign o_trig = 1'b0;
Expand Down
1 change: 1 addition & 0 deletions dsp/fwashout.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ end
`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})
wire signed [a_dw+cut-1:0] clipped=`SAT(sub,a_dw+cut,a_dw+cut-1);
assign o_data = clipped[a_dw+cut-1:cut];
`undef SAT

// Intended for raw ADC inputs.
// Could go back and make this module handle other data patterns
Expand Down
2 changes: 2 additions & 0 deletions dsp/hosted/lp.v
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ always @(posedge clk) begin
end
assign y = yr[18+shift:1];

`undef SAT

endmodule


Expand Down
1 change: 1 addition & 0 deletions dsp/hosted/lp_2notch.v
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ wire [22:0] y_sum = y1+y2+y3;
`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})
reg [21:0] y_out=0;
always @(posedge clk) y_out <= `SAT(y_sum, 22, 21);
`undef SAT

// Very stupid extra delay, only here to keep historical I-Q relationship.
// XXX get rid of this!
Expand Down
2 changes: 0 additions & 2 deletions dsp/hosted/mp_proc.v
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ assign setmp_addr = {1'b0, ~state[0]};
assign coeff_addr = {~state2[1],state2[0]};
assign lim_addr = {state6[2],state4[0]};

`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})

reg [7:0] stb=0;
always @(posedge clk) stb <= {stb[6:0],sync};

Expand Down
15 changes: 8 additions & 7 deletions dsp/hosted/non_iq_interleaved_piloop.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
// DELAY: 9 cycles of delay at @clk
`timescale 1ns / 1ns

// Universal definition; note: old and new are msb numbers, not bit widths.
// 0. assert old > new
// 1. Should really be called downsize and check for saturate
// 2. if x[old:new] are all ones or zeros: then it's safe to resize the signal
// 3. Else: Rail the signal
`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})

// TODO: Potentially parameterize input and output signal widths
// : Rename Ref_i/q to Setpoint_i/q
Expand Down Expand Up @@ -57,9 +51,15 @@ initial begin
end

parameter KW = 18; // Bit-width of PI gains

// Assuming K's are w bits wide

// Universal definition; note: old and new are msb numbers, not bit widths.
// 0. assert old > new
// 1. Should really be called downsize and check for saturate
// 2. if x[old:new] are all ones or zeros: then it's safe to resize the signal
// 3. Else: Rail the signal
`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})

// Clamp errors - long story, not efficient, but see llc-suite fdbk_loop.v
wire signed [17:0] err1_i = $signed(`SAT(err_i, 18, 13)) <<< 3;
wire signed [17:0] err1_q = $signed(`SAT(err_q, 18, 13)) <<< 3;
Expand Down Expand Up @@ -96,4 +96,5 @@ always @ (posedge clk) begin
pi_out_i <= `SAT(pi_out_I_s, 18, 17);
pi_out_q <= `SAT(pi_out_Q_s, 18, 17);
end
`undef SAT
endmodule
2 changes: 1 addition & 1 deletion dsp/iq_modulator.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module iq_modulator #(
// also plan that abs(ampi+ampq*i) < 1

// Universal definition; note: old and new are msb numbers, not bit widths.
`undef SAT
`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})
reg signed [2*WIDTH-1:0] p1=0, p2=0; // product registers
wire signed [WIDTH:0] p1s = p1[2*WIDTH-2:WIDTH-2];
Expand All @@ -30,5 +29,6 @@ always @(posedge clk) begin
d3 <= `SAT(sum, WIDTH+1, WIDTH);
end
assign d_out = d3[WIDTH:1];
`undef SAT

endmodule
1 change: 1 addition & 0 deletions dsp/ll_prop.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ always @(posedge clk) begin
sum_iq <= prod_iq_lim + drive_iq;
sum_iq_s <= `SAT(sum_iq,18,17);
end
`undef SAT

assign out_iq=sum_iq_s;

Expand Down
5 changes: 4 additions & 1 deletion dsp/multiply_accumulate.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
`timescale 1ns / 1ns

`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})
/// TL;DR
/// accumulated += ((constant * signal) >>> downscale) + correction
///
Expand All @@ -25,6 +24,8 @@ parameter KW = 18; // Constant Width
parameter SW = 18; // Signal Width
parameter OW = 21; // OutputWidth: Desired bitwidth of the accumulator

`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})

reg signed [KW+SW-1: 0] integrator_in_large=0;
reg signed [OW-1: 0] integrator_sum=0;

Expand All @@ -40,6 +41,8 @@ always @ (posedge clk) begin
end
end

`undef SAT

assign accumulated = integrator_sum;

endmodule
3 changes: 2 additions & 1 deletion dsp/upconv.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ wire signed [15:0] i2i, i2q;
interp1 inti(.clk(clk), .x(d2i), .y(i2i));
interp1 intq(.clk(clk), .x(d2q), .y(i2q));

`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x : {x[old],{new{~x[old]}}})
`define SAT(x,old,new) ((~|x[old:new] | &x[old:new]) ? x[new:0] : {x[old],{new{~x[old]}}})
reg signed [31:0] cosp=0, sinp=0;
wire signed [17:0] cosp_msb = cosp[30:13];
wire signed [17:0] sinp_msb = sinp[30:13];
Expand All @@ -52,5 +52,6 @@ assign out_d = sum2[16:1];

assign cos_interp = i2i;
assign sin_interp = i2q;
`undef SAT

endmodule
1 change: 1 addition & 0 deletions dsp/xy_pi_clip.v
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ always @(posedge clk) begin
end

wire signed [29:0] mr_sat = `SAT(mr_ff,42,29);
`undef SAT

reg signed [35:0] mr=0;
reg signed [30:0] lim1=0;
Expand Down
Loading

0 comments on commit 2078d26

Please sign in to comment.