Error code not working - hdl

Hello I am trying to implement the gate MiniALU but the howard simulator give me this error: "has no source pin". I would be happy if you can help me solve this.
my code-
CHIP MiniALU {
IN
x[16], y[16], // 16-bit inputs
zx, // zero the x input?
zy, // zero the y input?
f; // compute out = x + y (if f == 1) or out = x & y (if == 0)
OUT
out[16]; // 16-bit output
PARTS:
// Zero the x input and y input
Mux16(a[0..15]=x, b[0..15]=false, sel=zx, out[0..15]=x1);
Mux16(a[0..15]=y, b[0..15]=false, sel=zy, out[0..15]=y1);
// Perform f
And16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xandy);
Add16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xaddy);
Mux16(a[0..15]=xandy, b[0..15]=xaddy, sel=f, out[0..15]=out);
}

You are connecting x2 and y2 to the inputs of And16 and Add16 but x2 and y2 are not defined anywhere.
You need to replace x2 and y2 with x1 and y1 in the connections to And16 and Add16.

Correct code:
Mux16(a=x, b=false, sel=zx, out=x1);
Mux16(a=y, b=false, sel=zy, out=y1);
And16(a=x1, b=y1, out=xandy);
Add16(a=x1, b=y1, out=xaddy);
Mux16(a=xandy, b=xaddy, sel=f, out=out);
Your problem was that you wrote y2 and x2 instead of y1 and x2.
Also, there is not need in [0..15]

You wrote:
And16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xandy);
Add16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xaddy);
instead of:
And16(a[0..15]=x1, b[0..15]=y1, out[0..15]=xandy);
Add16(a[0..15]=x1, b[0..15]=y1, out[0..15]=xaddy);

Related

How can I assign a variable with the value of another variable in Kotlin?

In Code A, the parameters x1 and x2 use the same vaule, it works well.
I think I can improve Code A, so I write Code B, but it failed.
How can I assign x2 with the value of x1 ?
Code A
val stepWidth = step * index
it.drawChildnAxis(
x1 = stepWidth.toX, y1 = 0f.toY,
x2 = stepWidth.toX, y2 = yAxisLength.toY
)
fun Canvas.drawChildnAxis(x1:Float, y1:Float,x2:Float,y2:Float){
drawLine(
Offset(x = x1, y = y1),
Offset(x = x2, y = y2),
paintTableAxisChild
)
}
Code B
it.drawChildnAxis(
x1 = step * index.toX, y1 = 0f.toY,
x2 = x1, y2 = yAxisLength.toY
)
//The same
The x1 = ..., x2 = ... etc in your code are not actually assignment statements! They are named arguments.
There is no variable x1, x2 etc that becomes suddenly in scope at the function call, allowing you to assign values to it. This is just a bit of a syntax that lets you say the names of your parameters to make your code more readable, and sometimes resolve overload resolution ambiguities.
The syntax just so happened to be designed to look similar to assignments, making the left hand side look like a new variable just got declared. Would you still have this confusion if the syntax used : instead of =?
it.drawChildnAxis(
x1: stepWidth.toX, y1: 0f.toY,
x2: stepWidth.toX, y2: yAxisLength.toY
)
So x2 = x1 doesn't make sense - there is no such variable as x1 at that position. x1 is only the name of a parameter, which is only in scope when you are inside drawChildnAxis.
If you want to avoid repetition, just create a new variable yourself!
val x = stepWidth.toX
it.drawChildnAxis(
x1 = x, y1 = 0f.toY,
x2 = x, y2 = yAxisLength.toY
)
If you don't want x to be accessible afterwards, use a scope function:
stepWidth.toX.let { x ->
it.drawChildnAxis(
x1 = x, y1 = 0f.toY,
x2 = x, y2 = yAxisLength.toY
)
}
All of this is of course assuming that toX doesn't have side effects - calling its getter twice on the same thing gives the same value.

How can I find all points between point1 and point 2 - Objective c? [duplicate]

I've got two points between which im drawing a line (x1,y1 and x2,y2) but i need to know the coordinates of x3,y3 which is gapSize away from point x2,y2. Any ideas on how to solve this problem (the program is written in objective-c if that is helpful at all)?
You can simply calculate the angle in radians as
double rads = atan2(y2 - y1, x2 - x1);
Then you get the coordinates as follows:
double x3 = x2 + gapSize * cos(rads);
double y3 = y2 + gapSize * sin(rads);
Is this what you meant?
Compute the distance between P1 and P2: d=sqrt( (y2-y1)^2 + (x2-x1)^2)
Then x2 = (d*x1 + gapSize*x3) / (d+gapSize)
So x3 = (x2 * (d+gapSize) - d*x1) / gapSize
Similarly, y3 = (y2 * (d+gapSize) - d*y1) / gapSize
Sorry for the math. I didn't try to code it but it sounds right. I hope this helps.
There are many ways to do this. Simplest (to me) is the following. I'll write it in terms of mathematics since I can't even spell C.
Thus, we wish to find the point C = {x3,y3}, given points A = {x1,y1} and B = {x2,y2}.
The distance between the points is
d = ||B-A|| = sqrt((x2-x1)^2 + (y2-y1)^2)
A unit vector that points along the line is given by
V = (B - A)/d = {(x2 - x1)/d, (y2-y1)/d}
A new point that lies a distance of gapSize away from B, in the direction of that unit vector is
C = B + V*gapSize = {x2 + gapSize*(x2 - x1)/d, y2 + gapSize*(y2 - y1)/d}

Checking if lines intersect and if so return the coordinates

I've written some code below to check if two line segments intersect and if they do to tell me where. As input I have the (x,y) coordinates of both ends of each line. It appeared to be working correctly but now in the scenario where line A (532.87,787.79)(486.34,769.85) and line B (490.89,764.018)(478.98,783.129) it says they intersect at (770.136, 487.08) when the lines don't intersect at all.
Has anyone any idea what is incorrect in the below code?
double dy[2], dx[2], m[2], b[2];
double xint, yint, xi, yi;
WsqT_Location_Message *location_msg_ptr = OPC_NIL;
FIN (intersect (<args>));
dy[0] = y2 - y1;
dx[0] = x2 - x1;
dy[1] = y4 - y3;
dx[1] = x4 - x3;
m[0] = dy[0] / dx[0];
m[1] = dy[1] / dx[1];
b[0] = y1 - m[0] * x1;
b[1] = y3 - m[1] * x3;
if (m[0] != m[1])
{
//slopes not equal, compute intercept
xint = (b[0] - b[1]) / (m[1] - m[0]);
yint = m[1] * xint + b[1];
//is intercept in both line segments?
if ((xint <= max(x1, x2)) && (xint >= min(x1, x2)) &&
(yint <= max(y1, y2)) && (yint >= min(y1, y2)) &&
(xint <= max(x3, x4)) && (xint >= min(x3, x4)) &&
(yint <= max(y3, y4)) && (yint >= min(y3, y4)))
{
if (xi && yi)
{
xi = xint;
yi = yint;
location_msg_ptr = (WsqT_Location_Message*)op_prg_mem_alloc(sizeof(WsqT_Location_Message));
location_msg_ptr->current_latitude = xi;
location_msg_ptr->current_longitude = yi;
}
FRET(location_msg_ptr);
}
}
FRET(location_msg_ptr);
}
There is an absolutely great and simple theory about lines and their intersections that is based on adding an extra dimensions to your points and lines. In this theory a line can be created from two points with one line of code and the point of line intersection can be calculated with one line of code. Moreover, points at the Infinity and lines at the Infinity can be represented with real numbers.
You probably heard about homogeneous representation when a point [x, y] is represented as [x, y, 1] and the line ax+by+c=0 is represented as [a, b, c]?
The transitioning to Cartesian coordinates for a general homogeneous representation of a point [x, y, w] is [x/w, y/w]. This little trick makes all the difference including representation of lines at infinity (e.g. [1, 0, 0]) and making line representation look similar to point one. This introduces a GREAT symmetry into formulas for numerous line/point manipulation and is an
absolute MUST to use in programming. For example,
It is very easy to find line intersections through vector product
p = l1xl2
A line can be created from two points is a similar way:
l=p1xp2
In the code of OpenCV it it just:
line = p1.cross(p2);
p = line1.cross(line2);
Note that there are no marginal cases (such as division by zero or parallel lines) to be concerned with here. My point is, I suggest to rewrite your code to take advantage of this elegant theory about lines and points.
Finally, if you don't use openCV, you can use a 3D point class and create your own cross product function similar to this one:
template<typename _Tp> inline Point3_<_Tp> Point3_<_Tp>::cross(const Point3_<_Tp>& pt) const
{
return Point3_<_Tp>(y*pt.z - z*pt.y, z*pt.x - x*pt.z, x*pt.y - y*pt.x);
}

how to find a point in the path of a line

I've got two points between which im drawing a line (x1,y1 and x2,y2) but i need to know the coordinates of x3,y3 which is gapSize away from point x2,y2. Any ideas on how to solve this problem (the program is written in objective-c if that is helpful at all)?
You can simply calculate the angle in radians as
double rads = atan2(y2 - y1, x2 - x1);
Then you get the coordinates as follows:
double x3 = x2 + gapSize * cos(rads);
double y3 = y2 + gapSize * sin(rads);
Is this what you meant?
Compute the distance between P1 and P2: d=sqrt( (y2-y1)^2 + (x2-x1)^2)
Then x2 = (d*x1 + gapSize*x3) / (d+gapSize)
So x3 = (x2 * (d+gapSize) - d*x1) / gapSize
Similarly, y3 = (y2 * (d+gapSize) - d*y1) / gapSize
Sorry for the math. I didn't try to code it but it sounds right. I hope this helps.
There are many ways to do this. Simplest (to me) is the following. I'll write it in terms of mathematics since I can't even spell C.
Thus, we wish to find the point C = {x3,y3}, given points A = {x1,y1} and B = {x2,y2}.
The distance between the points is
d = ||B-A|| = sqrt((x2-x1)^2 + (y2-y1)^2)
A unit vector that points along the line is given by
V = (B - A)/d = {(x2 - x1)/d, (y2-y1)/d}
A new point that lies a distance of gapSize away from B, in the direction of that unit vector is
C = B + V*gapSize = {x2 + gapSize*(x2 - x1)/d, y2 + gapSize*(y2 - y1)/d}

Generating a random Gaussian double in Objective-C/C

I'm trying to generate a random Gaussian double in Objective-C (the same as random.nextGaussian in Java). However rand_gauss() doesn't seem to work. Anyone know a way of achieving this?
This link shows how to calculate it using the standard random() function.
I should note that you'll likely have to make the ranf() routine that converts the output of random() from [0,MAX_INT] to be from [0,1], but that shouldn't be too difficult.
From the linked article:
The polar form of the Box-Muller transformation is both faster and more robust numerically. The algorithmic description of it is:
float x1, x2, w, y1, y2;
do {
x1 = 2.0 * ranf() - 1.0;
x2 = 2.0 * ranf() - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 );
w = sqrt( (-2.0 * ln( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;