VHDL macro for vector indices - macros

I am searching for a macro, constant or attribute, whatever it is, replaces "(x downto y)". To explain in detail for example complex numbers,
there is a generic value WIDTH,
signal num : std_logic_vector(2*WIDTH downto 0);
**pseudo**
re = 2*WIDTH-1 downto WIDTH;
im = WIDTH-1 downto 0;
**pseudo**
x <= num(re);
y <= num(im); etc
also it could be some attribute like range. eg
x <= num(num're);
y <= num(num'im);
I use it a lot and it should be great for better readability and writing clean code

A subtype can be used to declare an integer range like:
-- **pseudo** re = 2*WIDTH-1 downto WIDTH; im = WIDTH-1 downto 0; **pseudo**
subtype re is natural range 2*WIDTH-1 downto WIDTH;
subtype im is natural range WIDTH-1 downto 0;
Then it is possible to do:
x <= num(re);
y <= num(im);
With x and y declared as std_logic_vector(WIDTH-1 downto 0), or std_logic_vector(re) and std_logic_vector(im).
Btw. num should probably have highest index as 2*WIDTH-1, and not 2*WIDTH (off-by-one error).

Maybe not exactly what you had in mind, but a VHDL record would seem to be a good fit for what you are trying to do.
You define your record type:
type mycomplex_t is record
re : std_logic_vector(WIDTH-1 downto 0);
im : std_logic_vector(WIDTH-1 downto 0);
end record;
Then, you declare your signal to be of this type:
signal num : mycomplex_t;
And you use it as such:
x <= num.re;
y <= num.im;
You can also assign it like this :
num <= (re => x2, im => y2);
Or, like this:
num.re <= x2;
num.im <= y2;
Or all at once from another signal of the same type:
signal other_complex : mycomplex_t;
-- (...)
num <= other_complex;

Related

Incorrect plotting of ramp sequence in Matlab

I am trying to plot a discrete ramp sequence using Matlab, within the interval [-10, 10].
This is my code:
function Ramp()
rampseq(-10, 10);
end
function rampseq (n1, n2)
n = (n1:1:n2);
stem (n, pw(n));
end
function y = pw(n)
if (n < 0)
y = 0;
return;
else
y = n;
return;
end
end
I define the behavior of the sequence in the pw(n) function (short form for 'piecewise'). Note that if n < 0, the output should be 0 or else if n >= 0, the output should be n itself. However, the plot turns out to be:
This isn't the ramp sequence as the Y-values are not 0 when n < 0, contrary to the behavior defined in the pw(n) function. Where am I going wrong? Is the if statement being skipped for some reason? The Command Window does not show any error.
You are passing the whole vector n into the pw() function. However, the pw() function then compares n to 0, which is a scalar.
You could adjust your pw() function to operate on vector inputs like this:
function y = pw(n)
y = n;
y(n < 0) = 0;
end

How to cope with the error of the following piece of system verilog code?

schematic
I would like to write system verilog code to implement this schematic (using 3 2:4 decoder and 64 3-input and gate to achieve a 6:64 decoder), and the following is my piece of code:
module Ex4( input logic [5:0] D,
output logic [63:0] y
);
genvar i;
genvar j;
genvar k;
integer n = 0;
logic [3:0] y1, y2, y3;
dec2 d1(D[1:0], y1);
dec2 d2(D[3:2], y2);
dec2 d3(D[5:4], y3);
generate
begin
for(i = 0; i < 3; i = i + 1) begin:flp1
for (j = 0; j < 3; j = j + 1) begin:flp2
for(k = 0; k < 3; k = k + 1) begin:flp3
and3_new a_n(y1[i], y2[j], y3[k], y[n]);
n=n+1; // error message comes from this line
end
end
end
end
endgenerate
endmodule
Note: "dec2" and "and3_new" are two modules written in advance with no problems
However, I got the following error during compilation, can anybody give me some hint? Thank you.
Error (10170): Verilog HDL syntax error at Ex4.sv(22) near text: "=";
expecting ".", or "(". Check for and fix any syntax errors that appear
immediately before or at the specified keyword. The Intel FPGA
Knowledge Database contains many articles with specific details on
how to resolve this error. Visit the Knowledge Database at
https://www.altera.com/support/support-resources/knowledge-
base/search.html and search for this specific error message number.
You can't put a procedural assignment statement in that position. Instead of [n], use [i*16+j*4+k]. You can also do
module Ex4( input logic [5:0] D,
output logic [63:0] y
);
logic [3:0] y1, y2, y3;
dec2 d1(D[1:0], y1);
dec2 d2(D[3:2], y2);
dec2 d3(D[5:4], y3);
for(genvar i = 0; i < 4; i++) begin:flp1
for (genvar j = 0; j < 4; j++) begin:flp2
for(genvar k = 0; k < 4; k = k + 1) begin:flp3
parameter n = i*16+j*4+k;
and3_new a_n(y1[i], y2[j], y3[k], y[n]);
end :flp3
end : flp2
end : flp1
endmodule

How to convert logical conditions to a variable of a function

I would like to achieve the above for the following:
Rn = 0.009; % Resolution of simulation (in m^3)
Xs = -1 : Rn : 1;
Ys = -1 : Rn : 1;
Zs = 0 : Rn : 1;
[X Y Z] = meshgrid(Xs, Ys, Zs);
alpha = atan2(Z,X);
ze = x.^2 + y.^2; % define some condition
m = 0.59; % manual input
cond = (pi/3 <= alpha) & ...
(alpha <= (2*pi/3)) & ...
(m <= Z) & ...
(Z <= ze); % more conditions
xl = nnz(cond); % the number of non-zero elements
f = abs(xl*1000 - 90) % guessing m to get f as low as possible
How do I turn m into a variable for some f function so I can call fminsearch to quickly find the corresponding m for f ≈ 0?
In order to use m as a variable, you need to define a function handle. So you need to write:
cond = #(m) ((pi/3) <= alpha) & (alpha <= (2*pi/3)) & (m <= Z) & (Z <= ze);
However, you cannot use a function handle in the nnz routine, since it only accepts matrices as inputs. But, the solution to the problem is that you only have Boolean variables in cond. This means, you can simply sum over cond and get the same result as with nnz.
The only issue I see is how to implement the sum in fminsearch. Unfortunately, I do not have access to fminsearch, however I would assume that you can do something with reshape and then multiply with dot (i.e. .*) with the unity vector to get a sum. But you'll have to try that one out, not sure about it.

Generating and Verifying 1024-bit Primes in VHDL

I'm implementing RSA encryption and need to generate random 1024-bit primes.
I can't use INTEGER signals because of the limit, and so I'm using STD_LOGIC_VECTOR and convert it to UNSIGNED when I need to perform arithmetic operations.
I began by using UNIFORM to generate 32 random 32-bit numbers which I then copy over into a 1024-bit STD_LOGIC_VECTOR.
I then set the most significant and least significant bits to '1' to ensure it is 1024-bits and odd.
I then check for primality using an implementation of the Miller Rabin algorithm, which is where my problem lies.
This is where I generate the random primes:
function GEN_1024_PRIME return STD_LOGIC_VECTOR is
VARIABLE s1, s2 : POSITIVE;
VARIABLE random : REAL;
VARIABLE small_random : STD_LOGIC_VECTOR (31 downto 0);
VARIABLE large_random : STD_LOGIC_VECTOR (1023 downto 0);
VARIABLE prime : STD_LOGIC := '0';
begin
while prime /= '1' loop
for I in 0 to 31 loop
UNIFORM(s1, s2, random);
small_random := STD_LOGIC_VECTOR(to_unsigned(INTEGER(TRUNC(random * REAL(2147483647))), 32));
large_random (I*32 + 31 downto I*32) := small_random;
end loop;
large_random(0) := '1';
large_random(1023) := '1';
prime := MILLER_RABIN (large_random);
end loop;
return large_random;
end function;
And my implementation of Miller Rabin:
function MILLER_RABIN (prime : STD_LOGIC_VECTOR (1023 downto 0)) return STD_LOGIC is
VARIABLE t : INTEGER := 4;
VARIABLE temp, r, a, x, j, n: UNSIGNED (1023 downto 0);
VARIABLE small_random : UNSIGNED (31 downto 0);
VARIABLE large_random : UNSIGNED (1023 downto 0);
VARIABLE s1, s2 : POSITIVE;
VARIABLE random : REAL;
begin
n := UNSIGNED(prime);
if n MOD 2 = 0 OR n MOD 3 = 0 then
return '0';
else
-- calculate n - 1 = 2^s * r such that r is odd
r := n - 1;
while r MOD 2 = 0 loop
r := r / 2;
end loop;
for I in 1 to t loop
-- choose random a, 2 <= a <= n-2
for I in 0 to 31 loop
UNIFORM(s1, s2, random);
small_random := to_unsigned(INTEGER(TRUNC(random * REAL(2147483647))), 32);
large_random (I*32 + 31 downto I*32) := small_random;
end loop;
a := large_random;
temp := r;
x := MOD_3(a, temp, n);
while (temp /= (n - 1) AND x /= 1 AND x /= (n - 1)) loop
x := (x * x) MOD n;
temp := temp * 2;
end loop;
if x /= (n - 1) AND temp MOD 2 = 0 then
return '0';
end if;
end loop;
return '1';
end if;
end function;
function MOD_3 (a, b, c : UNSIGNED (1023 downto 0)) return UNSIGNED is
VARIABLE x : UNSIGNED (1023 downto 0) := TO_UNSIGNED(1, 1024);
VARIABLE y : UNSIGNED (1023 downto 0) := a;
VARIABLE b_temp : UNSIGNED (1023 downto 0) := b;
begin
while b_temp > 0 loop
if b_temp MOD 2 = 1 then
x := (x * y) MOD c;
end if;
y := (y * y) MOD c;
b_temp := b_temp / 2;
end loop;
return x MOD c;
end function;
I convert the input to UNSIGNED in order to perform arithmetic operations, which seemed like it would work until I realized there will be instances in which the product of 2 values will be larger than 1024 bits. For example, in this while loop:
while (temp /= (n - 1) AND x /= 1 AND x /= (n - 1)) loop
x := (x * x) MOD n;
temp := temp * 2;
end loop;
The resultant of temp := temp * 2; is 2048 bits.
I feel like I could get this to work by messing around with the sizes of my UNSIGNED variables, but I think it is getting messier than it needs to be, so I'm wondering if I am approaching this the wrong way? Is there a simpler way to generate 1024-bit primes? Is there another primality test that would be better suited for my problem?

where is cula "culaSgesv" answer for X?

I just downloaded Cula and I want to use it's implemented functions for solving system of linear equation I looked into Examples Directory and I saw below code but it's very confusing when they want to obtain X solution of A*X=B they just copy B in X and since A is identity diagonal matrix so the answer IS, "B" and in this line of code nothing happens
status = culaSgesv(N, NRHS, A, N, IPIV, X, N);
(changing X to B didn't help!)
would you please tell me whats going on? Please tell me how can I get the answer "X" from this?
if anyone need any further information please just tell me.
#ifdef CULA_PREMIUM
void culaDoubleExample()
{
#ifdef NDEBUG
int N = 4096;
#else
int N = 780;
#endif
int NRHS = 1;
int i;
culaStatus status;
culaDouble* A = NULL;
culaDouble* B = NULL;
culaDouble* X = NULL;
culaInt* IPIV = NULL;
culaDouble one = 1.0;
culaDouble thresh = 1e-6;
culaDouble diff;
printf("-------------------\n");
printf(" DGESV\n");
printf("-------------------\n");
printf("Allocating Matrices\n");
A = (culaDouble*)malloc(N*N*sizeof(culaDouble));
B = (culaDouble*)malloc(N*sizeof(culaDouble));
X = (culaDouble*)malloc(N*sizeof(culaDouble));
IPIV = (culaInt*)malloc(N*sizeof(culaInt));
if(!A || !B || !IPIV)
exit(EXIT_FAILURE);
printf("Initializing CULA\n");
status = culaInitialize();
checkStatus(status);
// Set A to the identity matrix
memset(A, 0, N*N*sizeof(culaDouble));
for(i = 0; i < N; ++i)
A[i*N+i] = one;
// Set B to a random matrix (see note at top)
for(i = 0; i < N; ++i)
B[i] = (culaDouble)rand();
memcpy(X, B, N*sizeof(culaDouble));
memset(IPIV, 0, N*sizeof(culaInt));
printf("Calling culaDgesv\n");
DWORD dw1 = GetTickCount();
status = culaDgesv(N, NRHS, A, N, IPIV, X, N);
DWORD dw2 = GetTickCount();
cout<<"Time difference is "<<(dw2-dw1)<<" milliSeconds"<<endl;
if(status == culaInsufficientComputeCapability)
{
printf("No Double precision support available, skipping example\n");
free(A);
free(B);
free(IPIV);
culaShutdown();
return;
}
checkStatus(status);
printf("Verifying Result\n");
for(i = 0; i < N; ++i)
{
diff = X[i] - B[i];
if(diff < 0.0)
diff = -diff;
if(diff > thresh)
printf("Result check failed: i=%d X[i]=%f B[i]=%f", i, X[i], B[i]);
}
printf("Shutting down CULA\n\n");
culaShutdown();
free(A);
free(B);
free(IPIV);
}
You mention Sgesv but the sample code you have shown is for Dgesv. Nevertheless, the answer is the same.
According to the Netlib LAPACK reference, the B matrix of RHS vectors is passed to the function as the 6th parameter:
[in,out] B
B is DOUBLE PRECISION array, dimension (LDB,NRHS)
On entry, the N-by-NRHS matrix of right hand side matrix B.
On exit, if INFO = 0, the N-by-NRHS solution matrix X.
And the X matrix is returned in the same parameter location. So B when passed to the function contains the NxNRHS right-hand-side vectors, and the same parameter returns the X result.
In the code you have shown, they are actually passing a variable called X and after the result is returned (in the same variable X) they are comparing it against a variable named B which is perhaps confusing, but the concept is the same.
Since the A matrix in the example is the identity matrix, the correct solution of Ax = b is x=b
For the general case, you should pass your B matrix of RHS vectors in the 6th parameter location. Upon completion of the function, the result (X) will be returned in the same parameter.