Priority 8-to-3 encoder in Verilog (case, casex) - authentication

I'm trying to describe a SN54LS348 element (8-line to 3-line priority encoder).
The truth table is:
INPUTS OUTPUTS
E | 0 1 2 3 4 5 6 7 ** A2 A1 A0 | GS EO
///////////////////////////////////////
H | X X X X X X X X ** Z Z Z | H H
L | H H H H H H H H ** Z Z Z | H L
L | X X X X X X X L ** L L L | L H
L | X X X X X X L H ** L L H | L H
L | X X X X X L H H ** L H L | L H
L | X X X X L H H H ** L H H | L H
L | X X X L H H H H ** H L L | L H
L | X X L H H H H H ** H L H | L H
L | X L H H H H H H ** H H L | L H
L | L H H H H H H H ** H H H | L H
Here's my implementation:
module L348 (E, D0, D1, D2, D3, D4, D5, D6, D7, A0, A1, A2, GS, EO);
input E, D0, D1, D2, D3, D4, D5, D6, D7;
output A0, A1, A2, GS, EO;
assign D = {D0, D1, D2, D3, D4, D5, D6, D7};
parameter HIGH_IMPEDANCE = 3'bz;
reg [7:0] MASK_1 = 8'b0000_0001;
reg [7:0] MASK_2 = 8'b0000_0011;
reg [7:0] MASK_3 = 8'b0000_0111;
reg [7:0] MASK_4 = 8'b0000_1111;
reg [7:0] MASK_5 = 8'b0001_1111;
reg [7:0] MASK_6 = 8'b0011_1111;
reg [7:0] MASK_7 = 8'b0111_1111;
reg [7:0] MASK_8 = 8'b1111_1111;
reg [2:0] A;
reg [1:0] GS_EO;
reg [7:0] temp;
reg [7:0] mem [7:0];
initial
begin
mem[0] = MASK_1;
mem[1] = MASK_2;
mem[2] = MASK_3;
mem[3] = MASK_4;
mem[4] = MASK_5;
mem[5] = MASK_6;
mem[6] = MASK_7;
mem[7] = MASK_8;
temp = 8'bxxxx_xxxx;
end
assign {A2, A1, A0} = A;
assign {GS, EO} = GS_EO;
integer i;
always #(*)
begin
for (i = 7; i > 0; i = i - 1)
if (mem[i] & D == mem[i])
begin
temp = mem[i];
i = -1;
end
if (E)
begin
A = HIGH_IMPEDANCE;
GS_EO = 2'b11;
end
else
begin
if (temp == 8'b1111_1111)
begin
A = HIGH_IMPEDANCE;
GS_EO = 2'b10;
end
else
begin
GS_EO = 2'b01;
case (temp)
8'b0000_0001: A = 3'b001;
8'b0000_0011: A = 3'b010;
8'b0000_0111: A = 3'b011;
8'b0000_1111: A = 3'b100;
8'b0001_1111: A = 3'b101;
8'b0011_1111: A = 3'b110;
8'b0111_1111: A = 3'b111;
endcase
end
end
end
endmodule
It fails to achieve the switching of signals A2-A0 which are always in a X-state (except when E = H). I've tried many solutions, but it feels like simulator can't manage 'case' block ( I tried also 'casex' block). There is a bug somewhere, but I can't figure it out. Does anyone have ideas?

You've got quite a few things going on here but your most immediate problem is probably.
assign D = {D0, D1, D2, D3, D4, D5, D6, D7};
This implicitly defined wire is only going to be 1 bit wide and so the high 7b are going to be dropped. Isn't Verilog fun?
There are other logical problems but the easiest way of doing a priority encoder with a case statement is as follows:
casez (in)
4'b???1 : out = 0;
4'b??10 : out = 1;
4'b?100 : out = 2;
4'b1000 : out = 3;
default : out = 0; //no match
endcase
The casez allow you to put in ? for don't care conditions similar to your truth table. The first matching entry is taken which give you the priority behavior.
Adapt as needed for your case for width, direction of priority, width of IO, etc...
Finally as a stylistic concern your early loop termination should use break rather than directly modifying the loop variable.

Related

Multiplying multidimensional array in python

I have two arrays:
L, M, N = 6, 31, 500
A = np.random.random((L, M, N))
B = np.random.random((L, L))
I am trying to get an array C such that:
C = B * A
C has dimension [L, M, N]
I tried answer posted at this link but it hasn't given me the desired output.
A for loop version of above code is:
L, M, N = 6, 31, 500
A = np.random.random((L, M, N))
B = np.random.random((L, L))
z1 = []
for j in range(M):
a = np.squeeze(A[:, j, :])
z1.append(np.dot(B, a))
z2 = np.stack(z1)
I think you are looking for numpy.tensordot() where you can specify along which axes to sum:
np.tensordot(B,A,axes=(1,0))

How to find the value of integer k efficiently for which q divides b ^ k finitely?

We have given two integers b and q, and we want to find the minimum value of an integer 'k' for which q completely divides b^k or k does not exist. Can we find out the value of k efficiently? Not just iterating each value of k (0, 1, 2, 3, ...) and checking (b^k) % q == 0) where q <= k or q >= k.
First of all, k will never equal zero unless q=1. k will never equal one unless q=b.
Next, if you can factorize q and b, then you can reason about them.
If there are any prime factors of b that are not factors of q at all, then k does not exist. Otherwise, k has to be large enough so that every factor of b^k is represented in q.
Here's some pseudo-code:
if (q==1) return 0;
if (q==b) return 1;
// qfactors and bfactors are arrays, one element per factor
let qfactors = prime_factorization(q);
let bfactors = prime_factorization(b);
let kmin=0;
foreach (f in bfactors.unique) {
let bcount = bfactors.count(f);
let qcount = qfactors.count(f);
if (qcount==0 || qcount < bcount) return -1; // k does not exist
kmin_f = ceiling(bcount/qcount);
if (kmin_f > kmin) let kmin = kmin_f;
}
return kmin;
If q = 1 ; k = 0
If b = q ; k = 1
If b > q and factors ; k = 1
If b < q and factors ; k != I
If b != q and not factors ; k != I
We know,
Dividend = Divisor x Quotient + Reminder
=> Dividend = Divisor x Quotient [Here, Reminder = 0]
Now go for calculation of Maxima and Minima as lower the value of Quotient is lower the value of 'k'.
If you consider the Quotient as 1 (lowest but spl case) then your formula for 'k' becomes,
k = log q/log b
I found a solution-
If q divides pow(b,k) then all prime factors of q are prime factors of b. Now we can do iterations q = q ÷ gcd(b,q) while gcd(q,b)≠1. If q≠1 after iterations, there are prime factors of q which are not prime factors of b then k doesn't exist else k = no of iteration.

Gamma distribution, incomplete beta function

I need to calculate Gamma cumulative distribution, and it seems this is fairly equivalent to calculating the incomplete beta function.
Excel does have an inculded calculator, but I found no trace of the used algorithm.
Do any of you know an accurate way to calculate this function?
I tried the following, translated into VB.NET from a website, but it gives stupid results:
Function IncompleteBetaFunc(x As Double, a As Double, b As Double) As Double
If x <= 0 Or x >= 1 Then Return 0
Dim bt As Double
bt = Math.Exp(GammaLn(a + b) - GammaLn(a) - GammaLn(b) + a * Math.Log(x) + b * Math.Log(1.0 - x))
If x < (a + 1.0) / (a + b + 2.0) Then
Return bt * betacf(a, b, x) / a
Else
Return 1.0 - bt * betacf(b, a, 1.0 - x) / b
End If
End Function
Function betacf(x As Double, a As Double, b As Double) As Double
Const MAXIT As Integer = 100
Const EPS As Double = 0.0000003
Const FPMIN As Double = 1.0E-30
Dim aa, c, d, del, h, qab, qam, qap As Double
Dim m, m2 As Integer
qab = a + b
qap = a + 1.0
qam = a - 1.0
c = 1.0
d = 1.0 - qab * x / qap
If (Math.Abs(d) < FPMIN) Then d = FPMIN
d = 1.0 / d
h = d
For m = 1 To MAXIT
m2 = 2 * m
aa = m * (b - m) * x / ((qam + m2) * (a + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
h *= d * c
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
del = d * c
h *= del
If (Math.Abs(del - 1.0) < EPS) Then Exit For
Next
Return h
End Function
Thanks!
Meta.Numerics includes well-tested and performant code for this any many other special functions. Its incomplete Beta function is documented here. The underlying code can be studied here. It also has a full-on Gamma distribution object, which will give moments, generate random variates, and do other distribution-related stuff in addition to computing the CDF. The package available via NuGet; just search for Meta.Numerics in the VS NuGet interface.

Edit text in file(UTF16)

I want replace 1 word in text file (file format is not .txt)
file Unicode is (UTF16)
few text example:
I D = " f f 0 3 4 a 9 2 - d d 9 f - 4 3 7 4 - a 8 a d - f 5 5 4 0 0 2 a 4 1 9 b " I S S U E _ D A T E = " 2 0 1 7 - 0 2 - 1 6 T 1 7 : 2 9 : 1 8 . 9 7 0 2 2 9 4 Z " S E Q U E N C E = " 0 " M A N A G I N G _ A P P L I C A T I O N _ T O K E N = " " > < L I C E N S E P U B L I C _ I D = " 3 A A - U J F - 8 K P " U S E R N A M E = " N d a G 6 Z T w u v I X Z B i t h 8 g o d d Q x E r x 0 + O g M c t 0 2 3 f X K O E w = " P A S S W O R D = " F 9 b n 6 b v w l f I 5 Z A 2 t h M h 9 d d s x Q L w = " T Y P E = " T R I A L " F L A G S = " 4 " D I S P L A Y _ N A M E =
I want change T R I A L to other word
It's not too hard to modify your text file. Use the IO class to assign it to a text file, then use String.Replace(oldValue As String, newValue As String) to change your string. Then use IO again to save the string to the file. This should work so long as your file isn't open and being used in another program - regardless of file extensions.
An example, to help you, could be something such as this:
Dim myFileContents as String = IO.File.ReadAllText("Path\To\My\File\File.extension")
myFileContents = myFileContents.Replace("T R I A L", "Some other word")
IO.File.WriteAllText("Path\To\My\File\File.extension", myFileContents)
Modify the contents to suit your situation - however, this is only a basic implementation. Additionally, it is important to note that String.Replace() will change all occurrences of your word to the new word.

Octave while/for statement -- what's wrong in a code?

This is my Octave code
for K= 1:10
while ( p < 1 )
ceil(log2(K)) + 1/(1-(1-p)^K) %function
p = p + sens;
K
endwhile;
endfor
K
and here is an output:
ans = 10.000
K = 1
ans = 5.0000
K = 1
ans = 3.3333
K = 1
ans = 2.5000
K = 1
ans = 2
K = 1
ans = 1.6667
K = 1
ans = 1.4286
K = 1
ans = 1.2500
K = 1
ans = 1.1111
K = 1
ans = 1
K = 1
K = 10
So, as you can see -- in inner while statement value of K is fixed to 1. What I am supposed to do to vary this value between 1 and 10. Why it is not working? I have no idea why this inner while statement is proceed only once.
ANSWER: There should be p= initial_value after for K=...
There should be p= initial_value after for K=...
That is, like this:
for K = 1:10
p = somevalue;
while ( p < 1 )
...