D Flip Flop problem. inputs: A, B, clock outputs: Q - cpu-architecture

I am trying to biuld a D flip-flop on logic.ly and should make the following circuit
inputs: A, B, clock
outputs: Q
Q should get '1' only when previous values of inputs A and B were respectively '1' and '0', and now its '1' and 1'.
Can anyone help?
This is how i began

Related

If we have three logical inputs A, B, C generating: (A'B+AC) and (AB+A'C), is there any way we can generate A from the two output?

So we have three inputs A, B, C generating the following:
output1: A'B + AC
and
output2: AB + A'C
Can we, using some logical gates obtain the input A from the two outputs?

Optimizing tensor multiplications

I've got a real-time image processing program I'm trying to optimize, and it all boils down to matrix multiplications. Consider 3 tensors I'm calculating in the initialization stage:
A = np.arange(35 * 51 * 59).reshape([35, 51, 59])
B = np.arange(37 * 51 * 51 * 59).reshape([37, 51, 51, 59])
C = np.arange(59 * 27).reshape([59, 27])
Each frame, I'm getting a new data in the form of a fourth tensor:
M = np.arange(35 * 37 * 59).reshape([35, 37, 59]).
Currently, I'm calculating D = np.einsum('xyf,xtf,ytpf,fr->tpr', M, A, B, C), where D is my desired result, and it's the major bottleneck of the program. There are two directions I'm trying to follow in order to optimize it.
First I tried coming up with a tensor T, a function of A, B, C, D that I can pre-calculate, and then it'll all boil to D = np.tensordot(M, T, axes=..). I wasn't successful. I spent a lot of time on it, is it even possible at all?
Moreover, the program itself is written in MATLAB. As it doesn't have a built-in tensor multiplication function (einsum or tensordot equivilent), I'm currently using the tprod toolbox, and doing:
temp1 = etprod('dcb', A, 'abc', M, 'adc');
temp2 = etprod('dbc', B, 'abcd', temp1, 'adb');
D = etprod('cdb', C, 'ab', temp2, 'acd');
As the default dot product function in MATLAB (for 2D matrices) is much faster then etprod, I though about reshaping A, B, C, D to 2D arrays in a way that I will able to multiple 2D matrices using the default function, without hand-written for loops. I wasn't successful with that either.
Any thoughts? thanks!
If this operation is done many times with different values of M we could define
D0 = np.einsum('xft,fr->tpr',A, B, C)
The whole operation could be broken into binary steps:
D0=np.einsum('xtf,ytpf->xyptf',A,B)
D0=np.einsum('xyptf,fr->xyftpr',D0,C)
D=np.einsum('tprxfy,xfy->tpr',D0,M)
The final operation uses D0 and M and can be coded as a matrix vector operation. In Matlab it would be
D=reshape(D0.[],numel(M))*M(:);
which could then be reordered as desired.
We could write this order as (((A,B),C),M)
It might be better, however, to use ((M,C),A,B)
D=np.einsum('xyf,fr->xyfr',M,C)
D0=np.einsum('xyfr,xtf->ytfr',D,A)
D=np.einsum('ytfr,ytpf->tpr',D,B)
This ordering of operations has intermediate arrays with only 4 indices rather than one with 6. If each operation is much faster than the single one this may be an advantage.

Harris HDL example 4.13

I read a book called "Digital design and computers architecture" written by Harris and I have a question about example 4.13 (logic gates with delays).
In that example we build a model for the expression Y = !A*!B*!C + A*!B*!C + A*!B*C. And also, we add a few delays to it: 1ns for inverters, three-input AND gates have a delay of 2ns, three-input OR gates have a delay of 4 ns.
Now, the .sv-code below:
*timescale 1ns/1ps
module example(input a,b,c
output y);
logic ab,bb,cb,n1,n2,n3;
assign #1 {ab,bb,cb} = ~{a,b,c};
assign #2 n1 = ab & bb & cb;
assign #2 n2 = a & bb & cb;
assign #2 n3 = a & bb & c;
assign #4 y = n1 | n2 | n3;
endmodule
So, the question is: what is the logic of such form of programming 3 operands (!A*!B*!C , A*!B*!C , A*!B*C). I don't understand what's happening on the lines from 4 to 8.
Can anyone explain please? Why there are such operands like ab, bb and cb?
ab, bb, and cb are all the logical inverses of a, b, and c (ie, !A, !B, and !C from your logical expression). The "b" or "_B" suffix is often used to indicate the inverse or inverse assertion level.
The assign expression each represent an operation from the original boolean equation:
assign #1 {ab,bb,cb} = ~{a,b,c};
This expression can be thought of as 3 NOT gates with inputs a, b, and c, with outputs ab, bb, and cb respectively. It uses the Verilog bitwise inverse operator ~ as well as the Verilog concatenation operator {} to do the inverse all in one line rather than 3 separate assign expressions.
From there, the assignments of n1, n2, and n3 all correspond to the 3, 3-way and operations in the equation and simply serve as intermediate values in the expression. n1 gets assigned !A * !B * !C, n2 gets A * !B * !C and n3 gets A * !B * C. Notice that each of these has the delay #n for the given gate, with the inverses gets #1; n1, n2 and n3 getting #2 and finally the output y being assigned with the #4 delay as its the final 3-way OR of the ANDed values n1, n2, n3.

How to run a loop in matlab even a struct condition satisfies and warning message?

Lets say I have a struct with three 1x1 struct elements with 5 fields in them respectively, the struct is GroupA and 1x1 struct elements are A,B,C and each has 5 fields in them say ID, E, F, G and h. I Need to check for each of them if h is same in any both and give a warning saying h is same in A & B. For example:
struct GroupA
A B C
Id ID ID
E E E
F F F
G G G
h h h
I wrote
for B_card=1:size(GroupA,2)-1
for C_card=(B_card+1):size(GroupA,2)
if strcmp(GroupA(B_card).h,GroupA(C_card).h)==1
warning('The h is same in',GroupA(B_card).ID,'&',GroupA(C_card).ID);
end
end
end
I got two problems: one the loop end (not sure whether is ending or not, can't understand) when an if condition satisfies and the warning message is only showing " The h is same in". I am quite new to Matlab so explained as good as I can, please let me know if you need some more explanation and thanks for your help.
for B_card=1:size(GroupA,2)-1
for C_card=(B_card+1):size(GroupA,2)
if strcmp(GroupA(B_card).h,GroupA(C_card).h)==1
warning('The h is same in',num2str(GroupA(B_card).ID),'&',num2str(GroupA(C_card).ID));
end
end
end
its working like this also & thanks every one

Matrix patterns

I have two 9x9 matrices, A and B.
I would like to create a large matrix C with the following pattern
A B B B B B
B A B B B B
B B A B B B
B B B A B B
B B B B A B
B B B B B A
As you can see, the A matrices are on the diagonal, the B are everywhere else. I'm trying to create a code so that this pattern continues no matter how great the dimensions are.
E.g. 10 matrices x 10 matrices still has matrix A along the diagonal and B's everywhere else.
Best to use horzcat and vertcat or something else like blkdiag? I'd rather not convert these matrices to cells as matrix A and B already contain information.
Thank you everyone for taking the time to read.
How about (refined)
maskcell = repmat( {ones(size(A))}, 1, 10 );
maskdiag = blkdiag( maskcell{:} );
AA = repmat( {A}, 1, 10 );
AD = blkdiag( AA{:} );
BB = repmat( B, 10, 10 );
C = BB .* (maskdiag == 0) + AD
Following on from the entirely valid comments below, I added the 'mask' to ensure that the correct pieces are selected from C.
C=B(~eye(size(B)))+A(eye(size(A))) should get you what you want. Could be a faster way to combine the use of eye though...
kron(eye(10), A) + kron(~eye(10), B)