I'm writing the hdl code for a DMux based on the Nand2Tetris course.
CHIP DMux {
IN in, sel;
OUT a, b;
PARTS:
And(a = sel, b = in, out = b);
Not(in = sel, out = selNot);
And(a = in, b = selNot, out = a);
}
For some reason, this code fails on the test script values of in = 1 and sel = 0. It evaluates a and b both to 0 in this case.
I have written out the gates multiple times, and I can't figure out why the result is not a = 1 and b = 0
Can someone explain to me what is happening?
I have a feeling your Not implementation may have a problem.
Try replacing the Not with a Nand:
Nand(a=sel,b=sel,out=notSel); // notSel = ! sel
If this works, then your Not.hdl is incorrect.
Also, on a style point, it's clearer if you define your intermediates before your final outputs (ie: put the Nand first), and be consistent in your input ordering (ie: a=in, b=sel or notSel, out = a or b). Helps reduce the chance you will misread something.
Not sure that anything is wrong with your code. It looks to be the same as mine, which works. Have you tested your other And and Not gates?
My code:
Not(in=sel, out=notsel);
And(a=notsel, b=in, out=a);
And(a=in, b=sel, out=b);
Related
I'm new to CodeQL, and still trying to wrap my head around it. On a semi-frequent basis, I find myself wanting for a language construct that supports specifying a "fallback value", to implement the following logic:
foot Foo(...) {
result = A
or
not eists(foot t | t = A) and
result = B
or
not eists(foot t | t = A) and
not eists(foot t | t = B) and
result = C
}
// aka
foot Foo(...) {
if eists(foot t | t = A) then
result = A
else if eists(foot t | t = B) then
result = B
else
result = C
}
Does CodeQL provide a way to rephrase this in a more elegant way? I've browsed the docs over and over again for something like the following, but to no avail:
foot Foo(...) {
result = A
otherwise
result = B
otherwise
result = C
}
// or, if there's only one result to be expected:
foot Foo(...) {
result = first([ A, B, C ])
}
I feel like my little imperative programmer's brain must be missing something that's been staring at my face the whole time.
At the moment there does not seem to be such language construct. There are however discussions for requesting this (or similar) features (#5348, #5573).
Note that in your example code you could simplify your exists(foot t | t = A) to just exists(A).
The following Modelica model checks and simulates.
model boolCheck_OK1
Real a = sin(time);
Real b = cos(time);
Real c;
//protected
// Boolean isInReg = inRegionCheck(a, b);
equation
c = if inRegionCheck(a, b) then 1.3*a^b else 0.7*b^a;
end boolCheck_OK1;
The function inRegionCheck() returns a Boolean, here is a simplified version:
function inRegionCheck
input Real a;
input Real b;
output Boolean c;
algorithm
c := a>b;
end inRegionCheck;
In the actual code, the function has more inputs and a longer name and is several lines long and the same check is used several times, so for readability I would like to introduce an intermediate variable as shown in the commented protected section, but that results in an error "Non-real equation in continuous time are not legal".
Any suggestions for an elegant workaround?
Works in SimulationX (with protected Boolean variable isInReg) if the function inRegionCheck is annotated by annotation(GenerateEvents=true);. In Dymola, you need to set annotation(Inline=true,GenerateEvents=true); to make it working.
The function call introduces a noEvent in your equation for isInReg.
This is what Dymola 2019 FD01 reports if the boolean is used:
Non-real equation in continuous time are not legal:
isInReg = noEvent(a > b);
Hence, your equation reduces to
isInReg = noEvent(a > b)
which is not allowed, as boolean values can only change on events.
You have to get rid of the function call and thus the noEvent.
Maybe there is a better solution, but you could try to define the check in a block instead of a function. At least for your minimal example it works perfectly fine.
Then your code could look like this:
model boolCheck_OK1
Real a = sin(time);
Real b = cos(time);
Real c;
protected
InRegionCheck check(a=a, b=b);
Boolean isInReg=check.c;
equation
c = if isInReg then 1.3*a^b else 0.7*b^a;
end boolCheck_OK1;
block InRegionCheck
input Real a;
input Real b;
output Boolean c;
equation
c = a>b;
end InRegionCheck;
Based on the fact that there is no function to convert to boolean but only a block, i would suggest marco's answer is the way to go.
With a workaround you can still do it inside a function, but not with the type Boolean. Instead use Real and compare in the if-clause if it's greater zero. For showing the switching behaviour of the boolean this works fine. If you rely on the function and don't use the boolean too often, this could be an option.
model boolCheck_OK1
Real a = sin(time);
Real b = cos(time);
Real c;
function inRegionCheck
input Real a;
input Real b;
output Real c;
algorithm
c := if a>b then 1 else 0;
end inRegionCheck;
protected
Real isInReg = inRegionCheck(a, b);
equation
c = if inRegionCheck(a, b)>Modelica.Constants.eps then 1.3*a^b else 0.7*b^a;
end boolCheck_OK1;
So far I have this code for a 2-bit comparator.
module twobitcomparator(xgtyin,xety,xltyin,x1,x0,y1,y0,xgty,xety,xlty);
//I/O
output xgty, xety, xlty; //xgty - x>y, xlty - x<y, xety - x=y
input x1, x0, y1, y0, xgtyin, xetyin, xltyin;
//specify circuit behavior
assign r = (xgyin);
assign s = (xlyin);
assign t = (xetyin);//not sure if I need an xetyin
assign a = (x1&~y1);
assign b = (x1&x0&~y0);
assign c = (x0&~y1&~y0);
assign xgty = (a|b|c|r);//X>Y
assign d = (~x0&~y0);
assign e = (x0&y0);
assign f = (x1&y1);
assign g = (~x1&~y1);
assign xety = ((d|e)&(f|g));//X=Y
assign h = (~x1&~x0&y0);
assign i = (~x1&y1);
assign j = (~x0&y1&y0);
assign xlty = (h|i|j|s);//X<Y
endmodule
Does this look good? I wrote a testbench for it and looked at the wave and the outputs were correct for the inputs, but I'm not sure if it's the most efficient way.
For the cascading, I know that the highest bit comparator's result (if it is an inequality) will just need to be sent down through the rest of the comparators and that will be the final result. If they are equal, then I just have to find the highest bit comparator where there is an inequality and that needs to be cascaded like I mentioned.
I am stuck on getting them to cascade, I am very new to Verilog and I have no clue how I should get each comparator's result into the next one. Here is my attempt.
module ncompare#( parameter n = 2)(input [2*n-1:0] xgyin, xlyin,
input [2*n-1:0] x1, x0, y1, y0,
output [2*n-1:0] xgy, xey, xly,
output xqyout);
wire xqyin;
assign xqyin = 1'b0;
twobitcomparator s1(.xgyin(xgyin[xqyin]), .xlyin(xlyin[xqyin]),
.x1(x1[2*n-1]), .x0(x0[2*n-2]), .y1(y1[2*n-1]), .y0(y0[2*n-2]),
.xgy(xgy[ripple0]), .xey(xey[ripple1]), .xly(xly[ripple2]));
twobitcomparator s0(.xgyin(xgyin[ripple0]), .xlyin(xlyin[ripple2]),
.x1(x1[1]), .x0(x0[0]), .y1(y1[1]), .y0(y0[0]),
.xgy(xgy[ripple3]), .xey(xey[ripple4]), .xly(xly[ripple5]));
endmodule
I think I need to use a generate statement since I need to make it work for any parameter n but I have no clue how to use generate since all examples I've looked at only have one output and I have three (that are also the next comparator's inputs! Agh!)
Thanks for any and all help!
The 2-bit comparator module can be rewritten as
module twobitcomparator(xgtyin,xltyin,x,y,xgty,xlty,xety);
output xgty, xety, xlty;
input xgtyin, xltyin;
input [1:0] x,y;
assign xgty = xgtyin | (~xltyin & ((x[1] > y[1]) | ((x[1] == y[1]) & (x[0] > y[0]))));
assign xlty = xltyin | (~xgtyin & ((x[1] < y[1]) | ((x[1] == y[1]) & (x[0] < y[0]))));
assign xety = ~(xlty | xgty);
endmodule
I have treated the two bit input as a bus instead of treating them as individual bits (Verilog allows you to do that). I have trimmed out the numerous intermediate results that were present in your code. This makes the code easier to understand as you do not have to keep track of all those temporary wires.
I then simulated this module using Icarus Verilog on EDA Playground. The link is here
https://www.edaplayground.com/x/5KRL
A four-bit comparator that uses these twobitcomparators can be written as follows.
module fourbitcomparator(xgtyin,xltyin,x,y,xgty,xlty,xety);
output xgty, xety, xlty;
input xgtyin, xltyin;
input [3:0] x,y;
wire xgty_1,xlty_1,xety_1;
twobitcomparator u_1 (
.xgtyin(xgtyin),
.xltyin(xltyin),
.x(x[3:2]),
.y(y[3:2]),
.xgty(xgty_1),
.xlty(xlty_1),
.xety(xety_1)
);
twobitcomparator u_0 (
.xgtyin(xgty_1),
.xltyin(xlty_1),
.x(x[1:0]),
.y(y[1:0]),
.xgty(xgty),
.xlty(xlty),
.xety(xety)
);
endmodule
Finally a 2n bit comparator using twobitcomparators, can be generalised as follows
module twoN_bitcomparator #(
parameter N = 2
)(
input xgtyin,
input xltyin,
input [(2*N-1):0]x,
input [(2*N-1):0]y,
output xgty,
output xlty,
output xety
);
wire [N:0] xgty_w,xlty_w,xety_w;
assign xgty_w[N] = xgtyin;
assign xlty_w[N] = xltyin;
generate
genvar i;
for (i=0;i<=(N-1);i=i+1)
begin:TWOBITGEN
twobitcomparator u_1 (
.xgtyin(xgty_w[i+1]),
.xltyin(xlty_w[i+1]),
.x(x[(2*i+1) : (2*i)]),
.y(y[(2*i+1) : (2*i)]),
.xgty(xgty_w[i]),
.xlty(xlty_w[i]),
.xety(xety_w[i])
);
end
endgenerate
assign xgty = xgty_w[0];
assign xlty = xlty_w[0];
assign xety = xety_w[0];
endmodule
The simulation of this generalised module is also available on EDA playground at https://www.edaplayground.com/x/2fbr
The waveform for the small testbench is also available at https://www.edaplayground.com/w/x/27X
I have a pointer to a parent class and I want to assign a new child object to that pointer conditionally. Right now, the syntax I have is rather lengthly:
std::unique_ptr<ParentClass> parentPtr;
if (...) {
parentPtr = std::unique_ptr<ParentClass>(new ChildClass1());
} else {
parentPtr = std::unique_ptr<ParentClass>(new ChildClass2());
}
Is there a good way of making this more readable / less lengthly?
Two possibilities would be:
std::unique_ptr<ParentClass> parentPtr(condition ?
(ParentClass*)new ChildClass1() :
(ParentClass*)new ChildClass2());
If condition is complicated, just assign a boolean to it and then write the construction. This solution only works for a binary condition though.
Another is to embrace C++14, and use
parentPtr = std::make_unique<ChildClass>();
First off, the "obvious" solution C ? new X : new Y does not work, since even if X and Y have a common base class A, the types X * and Y * have no common type. This is actually not so surprising after all if you consider that a class can have many bases (direct or indirect) and a given type may appear as a base multiple times.
You could make the conditional operator work by inserting a cast:
A * = C ? static_cast<A *>(new X) : static_cast<A *>(new Y);
But this would quickly get long and tedious to read when you try to apply this to your real situation.
However, as for std::unique_ptr, it offers the reset function which can be used to good effect here:
std::unique_ptr<A> p;
if (C)
{
p.reset(new X);
}
else
{
p.reset(new Y);
}
Now even if the actual new expressions are long, this is still nicely readable.
I wrote code that executes a function it receives from the (future) client, in a loop with some parameters. will call it func(name it).
Inside the function the client usually generate expression in the same variables(by GetUncertainty - each variable must be cleared before use). To do so , the simple idea is to use Block. Later , a code is executed that handles di and i outside the function.So, di and i must be globals(there could be more, it is flexible).
BTW, I know it is not efficient, but efficiency is not an issue.
func[v_, a_, r_] :=
(V = v; A = a; R = r;
Block[{V, A, R},i = V A + A 10 + R 100; di = GetUncertainty[i, {V, A, R}];] ;
Print[di])
The problem is that the client must reset the vars by hand. That means that the function parameters can't be V_,A_,R_ , otherwise The vars in the block will be replace by the values. I didn't manage to overcome this in any other way.
Another question in a similar issue. if I have
vars = {V,A,R,DR} , then
Block[vars , ..code.. ] , throws error that it is not a list.whereas Block[ {V,A,R,DR},..code..] works. How to overcome this?
Thanks.
its hard to unravel what you are trying to do, but the best approach may be to simply never assign values to symbols that need to be used as pure symbols in some context. Then you don't even need the Block[].
func[v_, a_, r_] := (
i = V A + A 10 + R 100;
di = GetUncertainty[i, {V, A, R}];
Print[di /. {V->v,A->a,R->r])
starting your own symbol names with Caps is frowned upon by the way as you risk conflict with built in symbols.
Note also there is a dedicated site mathematica.stackexchange.com
If I understand your application what you need are Formal Symbols. These are a set of Symbols with the Attribute Protected so that they cannot accidentally be assigned a value. They may be entered with e.g. Esc$AEsc for Formal Capital A. You can then use ReplaceAll (short form /.) as george showed to substitute your desired values.
Your code would be something like this:
func[v_, a_, r_] :=
Module[{i, di},
i = \[FormalCapitalV] \[FormalCapitalA] + \[FormalCapitalA] 10 + \[FormalCapitalR] 100;
di = GetUncertainty[i, {\[FormalCapitalV], \[FormalCapitalA], \[FormalCapitalR]}];
di /. {\[FormalCapitalV] -> v, \[FormalCapitalA] -> a, \[FormalCapitalR] -> r}
]
That looks horrible here but in a Notebook it looks like this:
I included Module to show how you should properly localize utility Symbols such as i and di but this particuarly simple function could also be written without them:
Your second question regarding "vars = {V,A,R,DR} then Block[vars , ..code.. ]" is answered here: How to set Block local variables by code?
Dedicated StackExchange site: