Skip to content
Snippets Groups Projects
Commit ebe24786 authored by BRONES Romain's avatar BRONES Romain
Browse files

Update simulation source files

* Correction on testcase
* Add data source files
* update the python script that creates the data files
parent 7ff3bbb1
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
This diff is collapsed.
# This script generate simulation data for the PI corrector + matrix multiplication.
import numpy as np
#############################################################################
# Response matrix, ref orbit and data input model
#############################################################################
# The inverted response matrix, we use an actual true dataset
respmat = np.load("respmat.npy")
# write to file
with open("respmat.txt", "w") as fp:
for r in respmat.flatten():
fp.write("{}\n".format(r))
# The orbit reference, again this is from true data
refmat = np.load("reforbit.npy")
# write to file
with open("reforbit.txt", "w") as fp:
for r in refmat:
fp.write("{}\n".format(r))
# BPM data generation
def generate_bpm_data(size, DC=0, A=0, F=100, N=0):
""" Generate dummy BPM data, based on a simple model of DC + A.sin(F) + N.noise """
sig = DC + A*np.sin(2*np.pi*F*np.arange(size)*1e-4+np.random.rand()*2*np.pi) + np.random.default_rng().normal(0, N, size)
return sig.astype("int32")
#############################################################################
# Parameters
#############################################################################
N_BPM=respmat.shape[1]*2
N_PSC=respmat.shape[0]
size=200
N_MM_RND=23
#############################################################################
# Generate data input
#############################################################################
bpmdata=np.empty((N_BPM, size), dtype="int32")
for i, DC, A, F, N in zip(
np.arange(N_BPM, dtype="int"),
np.random.default_rng().uniform(-1e6, 1e6, N_BPM),
np.random.default_rng().uniform(0, 200e3, N_BPM),
np.random.default_rng().uniform(2,2000, N_BPM),
np.random.default_rng().uniform(0, 20e3, N_BPM),):
bpmdata[i] = generate_bpm_data(size, DC, A, F, N)
# write to file
# Warning, write x then y for each bpm, not all x then all y as the array is packed.
with open("bpmdata.txt", "w") as fp:
for i in range(size):
for j in range(N_BPM//2):
fp.write("{} {} ".format(bpmdata[j,i], bpmdata[j+N_BPM//2,i]))
fp.write("\n")
#fp.write(" ".join(bpmdata[:,i].astype("str"))+"\n")
#############################################################################
# Compute correction
#############################################################################
# change respmat shape, tiled
trespmat = np.zeros((N_PSC, N_BPM), dtype="int64")
trespmat[:50,:122] = respmat[:50]
trespmat[50:,122:] = respmat[50:]
KP=2**16
KI=0
## -----------------------
# Model computation
# Step 1: error computation
oe=bpmdata-refmat[:,np.newaxis]
# Step 2: matrix multiplication
mm=np.empty((N_PSC, size), dtype="int32")
for i in range(size):
# Stage 1 : matmul
_mm= np.matmul(oe[:,i].astype("int64"), trespmat.T, dtype='int64')
mm[:,i] = _mm>>N_MM_RND
# Step 3: correction computation
mp=np.empty((N_PSC, size), dtype="int64")
mi=np.empty((N_PSC, size), dtype="int64")
mc=np.empty((N_PSC, size), dtype="int16")
mp = KP*mm.astype("int64")
mi = (KI*np.cumsum(mm.astype("int64"), axis=1))>>8
mc=np.clip(np.round((mp+mi)/2**34), -2**16, 2**16-1).astype("int16")
#############################################################################
# Write partial and results to files
#############################################################################
with open("orbiterror.txt", "w") as fp:
for i in range(size):
fp.write(" ".join(oe[:,i].astype("str"))+"\n")
with open("matmult.txt", "w") as fp:
for i in range(size):
fp.write(" ".join(mm[:,i].astype("str"))+"\n")
with open("propcorr.txt", "w") as fp:
for i in range(size):
fp.write(" ".join(mp[:,i].astype("str"))+"\n")
with open("corrout.txt", "w") as fp:
for i in range(size):
fp.write(" ".join(mc[:,i].astype("str"))+"\n")
5924
-19513
-19215
-109237
166240
-17317
-29652
51684
-36096
10417
13553
-48182
56484
35785
-63860
-36578
26888
1812
17673
-7529
-2824
444
-7663
25311
13813
-116752
-12794
135788
-57971
60328
-10796
15247
43916
-2356
-61268
35304
-26151
14359
6450
-31643
27162
-81035
67784
8051
-5744
-3565
7233
-2388
5018
-141529
150918
-17066
9092
24315
-13489
-78421
-34118
120665
-66214
71776
-3121
-10735
0
18683
73414
-22096
12599
7585
-17340
-60454
84521
47472
-19815
-31093
9897
47932
-32127
-50513
77108
-27969
32349
2203
-29078
1604
472
71123
34934
-104513
-187018
-4384
207231
-11309
-51219
-80190
28310
22970
-65401
-8336
44439
22292
-1902
-58967
61628
-28933
9561
13568
-22958
37462
-28275
-46372
66362
-22683
21927
23446
-44726
-15260
-717
22042
54698
-49896
-26576
36176
2194
16582
-43351
-6451
49933
121301
-88409
-84130
39667
15069
-110892
64454
-20732
-143171
84724
41970
-66063
-22054
34896
18009
-13075
-97395
103559
-56993
15551
19594
-16486
-5374
-6443
15291
5442
22966
-44614
33330
13688
-12719
-10597
-3072
-3690
19273
-31025
24029
-3728
-125894
157350
-133177
80812
-15318
-1793
111655
-76776
-61527
75597
-67768
57843
8331
990
-708
34586
-35389
6260
-16556
0
-2505
7727
8349
8600
-99933
129288
-10724
26510
13369
-19628
-131669
122541
-70058
35157
-9766
-8635
109495
-59782
-190849
227237
-198030
117447
9437
10349
14143
-82199
-10355
-90313
-17543
-179938
55141
-3413
-73363
39905
45716
12933
-136703
159985
5151
-19170
58888
-24430
-29785
74101
-129160
128524
22061
-47092
-37834
35677
-144977
150025
-107548
83089
6596
-18316
18807
62785
-65506
This diff is collapsed.
......@@ -14,22 +14,22 @@ architecture basic of TestCtrl is
--------------------------
function f_addr(addr:natural) return std_logic_vector is
begin
return std_logic_vector(to_unsigned(addr, AXI_ADDR_WIDTH));
return std_logic_vector(to_unsigned(addr, 32));
end function;
function f_sdata(data:integer) return std_logic_vector is
begin
return std_logic_vector(to_signed(data, AXI_DATA_WIDTH));
return std_logic_vector(to_signed(data, 32));
end function;
function f_data(data:natural) return std_logic_vector is
begin
return std_logic_vector(to_unsigned(data, AXI_DATA_WIDTH));
return std_logic_vector(to_unsigned(data, 32));
end function;
function f_bpmpkt(id:natural; x:integer; y:integer) return std_logic_vector is
begin
return std_logic_vector(to_unsigned(id, C_W_BPMID)) & std_logic_vector(to_signed(x, C_W_BPMPOS)) & std_logic_vector(to_signed(y, C_W_BPMPOS));
return std_logic_vector(to_unsigned(id, C_W_BPMID)) & std_logic_vector(to_signed(y, C_W_BPMPOS)) & std_logic_vector(to_signed(x, C_W_BPMPOS));
end function;
......@@ -74,6 +74,10 @@ begin
------------------------------------------------------------
ManagerProc : process
variable Data : std_logic_vector(AXI_DATA_WIDTH-1 downto 0) ;
file read_file : text;
variable line_v : line;
variable int_v : integer;
begin
wait until nReset = '1';
WaitForClock(ManagerRec, 2);
......@@ -85,29 +89,43 @@ begin
log("==--- Configure the DUT ---==", INFO);
log("+-- Global Config", INFO);
-- Correction coefficients
Write(ManagerRec, f_addr(12), f_data(16#0000E641#));
Write(ManagerRec, f_addr(16), f_data(16#000083B2#));
Write(ManagerRec, f_addr(12), f_data(16#00010000#));
Write(ManagerRec, f_addr(16), f_data(16#00000000#));
-- Enable
Write(ManagerRec, f_addr(8), f_sdata(5));
-- Set all orbit reference to 0
log("+-- Writing orbit reference...", INFO);
-- Load reference orbit from file
file_open(read_file, "reforbit.txt", read_mode);
-- X ref orbit
for I in 0 to C_N_MM_BPM-1 loop
--Write(ManagerRec, f_addr(16#400#+I*4), f_sdata(0));
--Write(ManagerRec, f_addr(16#800#+I*4), f_sdata(0));
readline(read_file, line_v);
read(line_v, int_v);
Write(ManagerRec, f_addr(16#400#+I*4), f_sdata(int_v));
end loop;
-- Set Matrix Coefs, diagonal
-- Y ref orbit
for I in 0 to C_N_MM_BPM-1 loop
for J in 0 to C_N_MM_PSC-1 loop
if I = J then
Write(ManagerRec, f_addr(16#E00#+I*512+J*4), f_sdata(32768));
else
--Write(ManagerRec, f_addr(16#E00#+I*512+J*4), f_sdata(0));
end if;
readline(read_file, line_v);
read(line_v, int_v);
Write(ManagerRec, f_addr(16#800#+I*4), f_sdata(int_v));
end loop;
file_close(read_file);
log("+-- Writing inv. resp. matrix coefficients...", INFO);
-- Set Matrix Coefs
file_open(read_file, "respmat.txt", read_mode);
for J in 0 to C_N_MM_PSC-1 loop
for I in 0 to C_N_MM_BPM-1 loop
readline(read_file, line_v);
read(line_v, int_v);
Write(ManagerRec, f_addr(16#E00#+J*512+I*4), f_sdata(int_v));
end loop;
end loop;
log("+-- Configuration done", INFO);
WaitForBarrier(ConfigDone);
-- Global Enable
......@@ -122,34 +140,80 @@ begin
-- Generate transactions for AxiTransmitter
------------------------------------------------------------
TransmitterProc : process
variable Data : std_logic_vector(DATA_TX_WIDTH-1 downto 0);
variable OffSet : integer ;
variable TransactionCount : integer;
variable ErrorCount : integer;
variable CurTime : time ;
variable TxAlertLogID : AlertLogIDType ;
file read_file : text;
variable line_v : line;
variable intx_v : integer;
variable inty_v : integer;
variable nturn : natural := 0;
begin
wait until nReset = '1' ;
WaitForClock(StreamTxRec, 2) ;
-- Load data input file
file_open(read_file, "bpmdata.txt", read_mode);
WaitForBarrier(ConfigDone) ;
log("Sending bpm packets", INFO);
for NTURN in 0 to 3 loop
for I in 3 to 124 loop
Send(StreamTxRec, f_bpmpkt(I, -457874+3833*I, 125679-81098*I), std_logic_vector(to_unsigned(NTURN,8)&'0'));
log("+-- Sending bpm packets", INFO);
--while (not endfile(read_file)) loop
while nturn < 20 loop
readline(read_file, line_v);
for I in 0 to 121 loop
read(line_v, intx_v);
read(line_v, inty_v);
Send(StreamTxRec, f_bpmpkt(I, intx_v, inty_v), std_logic_vector(to_unsigned(nturn,8)&'0'));
end loop;
nturn := nturn+1;
-- Simulate interpacket delay
WaitForClock(StreamTxRec, 150) ;
end loop;
file_close(read_file);
WaitForBarrier(TestDone) ;
wait ;
end process TransmitterProc ;
------------------------------------------------------------
-- AxiReceiverProc
-- Generate transactions for AxiReceiver
------------------------------------------------------------
ReceiverProc : process
variable ExpData, RxData : std_logic_vector(C_W_COR+C_W_PSCID-1 downto 0);
file read_file : text;
variable line_v : line;
variable int_v : integer;
begin
wait until nReset = '1' ;
WaitForClock(StreamRxRec, 2) ;
-- Load data input file
file_open(read_file, "corrout.txt", read_mode);
-- Check output
while true loop
readline(read_file, line_v);
for J in 0 to C_N_MM_PSC-1 loop
Get(StreamRxRec, RxData) ;
read(line_v, int_v);
ExpData := std_logic_vector(to_unsigned(0, C_W_PSCID)) & std_logic_vector(to_signed(int_v, C_W_COR));
wait for 0 ns;
AffirmIfEqual(RxData, ExpData, "");
end loop;
end loop;
wait;
end process ReceiverProc;
end basic;
Configuration tc_basic of tb_corr_matrixpi is
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment