I'm trying to make brick break in VHDL.
Everything went well but I have a weird problem.
In a piece of my code you see below I change the value to '0' at index (y,x) in my array when the ball reaches the edges of a brick. The problem is that all the values in my array change to 0, even when the ball is not moving (When the program start up, the ball is not moving).
process(Ball_x1,Ball_x2,Ball_y1,Ball_y2) begin
x1 <= 280 + (x * 55); --edges of my brick
x2 <= 280 + ((x + 1) * 55);
y1 <= 108 + (y * 13);
y2 <= 108 + ((y + 1) * 13);
waardeArray := level1(y,x);
if (vcnt >= y1) and (vcnt <= y2) then
if (Ball_x1 >= x1) and (Ball_x1 <= x2) then -- edges of my ball
if waardeArray = '1' then
level1(y,x) := '0';
end if;
end if;
end if;
end process;
The declaration of my array is
type levels is array ( 0 to 3, 0 to 5 ) of bit;
shared variable level1 : levels := (
('1','0','0','0','0','1'),
('0','1','0','0','1','0'),
('0','0','1','1','0','0'),
('0','0','1','1','0','0')
);
Next variable is used to store the value of the array at place (y,x)
shared variable waardeArray: bit;
It's the intention to use Ball_x2 etc too but I just tried with 1 edge of my ball to test the code.
Update
process(Ball_x1,Ball_x2,Ball_y1,Ball_y2) begin
x1 <= 280 + (x * 55); -- edges of my brick
x2 <= 280 + ((x + 1) * 55);
y1 <= 108 + (y * 13);
y2 <= 108 + ((y + 1) * 13);
if (vcnt >= y1) and (vcnt <= y2) then --vcnt is from another component where I send my data to my screen
if (Ball_x1 >= x1) and (Ball_x1 <= x2) then -- left edge of my ball
if level1(y)(x) = '1' then
level1(y)(x) <= '1'; --I do this to test if the elements with value 1 stay 1 and the 0 stay 0 ata initialization, but every element in my array change to 1.
else
level1(y)(x) <= level1(y)(x);
end if;
end if;
end if;
end process;
Declarations
signal x: integer range 0 to 6 := 0; --Six is used for something else.
signal y: integer range 0 to 3 := 0;
signal x1: integer range 279 to 616;
signal x2: integer range 279 to 616;
signal y1: integer range 107 to 228;
signal y2: integer range 107 to 228;
signal color: std_logic_vector (7 downto 0) := "00000111";
type levels is array ( 0 to 3) of std_logic_vector (5 downto 0);
signal level1 : levels := ("101101",
"010010",
"001100",
"001100");
This is my intention
Don't use shared variables. If you need to communicate a value between processes, use a signal. You can't be sure of the order at which updates compared to reads occur using shared variables. Well, you can, but you have to use protected types.
Also, in your process, you are looking at the values of x, y and vcnt. But those signals (I hope they are signals not shared variables :) are not in the sensitivity list of the process, so when they change, your process will not be triggered.
Related
How to loop through all bars I have on the graphic and check its RSI?
Code:
sum = 0
if ta.rsi(close, 7) >= 80
sum += 5
else if ta.rsi(close, 7) <= 60
sum -= 5
plot(sum, color=color.green)
It's not working correctly, because on each new bar the sum value sets back to 0. My goal is to see on my graphic the line which increases or decreases in the result of the if statement.
Use the var keyword.
var is the keyword used for assigning and one-time initializing of the
variable.
var sum = 0
if ta.rsi(close, 7) >= 80
sum := sum + 5
else if ta.rsi(close, 7) <= 60
sum := sum - 5
plot(sum, color=color.green)
So I am given a key that is in the format XYYYYZ, where X is a char from 'A'-'Z', YYYY is and int from 0 to 9999, and Z is a char from 'A'-'C'. I am suppose to make a unique hash function without any collisions.
I was told the smallest someone has made is a table size of 780,000 but I have no idea how.
The one I can think of is X-'A' to get a number from 0 to 26 and multiplying that by 100,000 then multiplying YYYY by 10 and then add (Z - 'A')
So Z1025A would be 2,610,250 and L4444C would be 1,144,443
And the make possible combo is 2699993 and / 2,700,000 would have about a 29% usage rate.
But is there any other way to reduce the the size of the table?
just do
((Z - 'A') * 26 + (X - 'A')) * 10000 + YYYY
The smallest possible hash table size for a key in this format is 780000, because there are 26 ways to choose the first character, 10 ways to choose each of the next four, and 3 ways to choose the final character. That is, there are 26 * 10 * 10 * 10 * 10 * 3 = 780000 possible keys. To find a hash function, think of the hash key like a counter. Rearrange the elements like this:
ZXYYYY
Starting with all elements at zero, each of the 'Y' elements rolls over after reaching 9. 'X' rolls over after reaching 25, and 'Z' rolls over after reaching 2. So, we can assign a number to the four 'Y' elements with:
y4 y3 y2 y1 --> y1 + (y2 * 10) + (y3 * 100) + (y4 * 1000)
This part of the key is just a base 10 counter. The remaining pair of elements forms a base 26 counter, and you can assign a number to this pair by assigning a number from 0 to 25 to the first value ('X'), 26 times a number from 0 to 25 to the second, and adding the results:
z x --> x + (z * 26)
For y4 y3 y2 y1 we will get a value from 0 to 9999, and for z x we will get a value from 0 to 675. If we multiply this value by 10000, we can add the value obtained for y4 y3 y2 y1 to get a unique value for the key. That is, the four low order positions count from 0 to 9 in ones, 0 to 90 in tens, 0 to 900 in hundreds, and 0 to 9000 in thousands, while the two high order positions can be viewed as counting from 0 to 6750000 in ten-thousands. This gives a possible 6760000 unique keys with this hash function. But since your specific case limits 'z' to three characters, we only have 3 * 26 = 78 possibilities for z x, and so there are 780000 unique hashes obtainable with this method, and the hash function can then be written:
hval = y1 + (y2 * 10) + (y3 * 100) + (y4 * 1000) + (x + z * 26) * 10000
where y1, y2, y3, y4, x, and z all represent integer values. Or, using C chars:
int y1, y2, y3, y4;
char x, z;
long hval;
hval = y1 + (y2 * 10) + (y3 * 100) + (y4 * 1000) + ((x - 'A') + (z - 'A') * 26) * 10000;
I should add that, converting the characters of the Latin alphabet to integers in this way is not guaranteed to work by the standard, but so long as you have an ASCII or UTF-8 character set it will work.
So basically I'm converting the array index to 2d coordinates.
What I'm trying is the 2D reverse here: https://softwareengineering.stackexchange.com/a/212813/199992
But on the division part for the x (in my case the width is 3), it's returning a double/float, which make sense since (2 / 3) isn't a whole number.
Should I be rounding x?
My code (Lua) is:
for i=1, 9 do
local x = i % 3
local y = i / 3
end
Yeah, you should be rounding x (down), the same for y:
local x = math.floor(i % 3)
local x = math.floor(i / 3)
But you should either count from 0..8 instead (and add 1 to x and y to be 1-based), or subtract 1 from i like this (this is how I usually do it in lua):
for i=1, 9 do
local x = math.floor((i - 1) % 3) + 1 -- 1, 2 or 3
local y = math.floor((i - 1) / 3) + 1 -- 1, 2 or 3
end
I've found the following macro in a utility header in our codebase:
#define CEILING(x,y) (((x) + (y) - 1) / (y))
Which (with help from this answer) I've parsed as:
// Return the smallest multiple N of y such that:
// x <= y * N
But, no matter how much I stare at how this macro is used in our codebase, I can't understand the value of such an operation. None of the usages are commented, which seems to indicate it is something obvious.
Can anyone offer an English explanation of a use-case for this macro? It's probably blindingly obvious, I just can't see it...
Say you want to allocate memory in chunks (think: cache lines, disk sectors); how much memory will it take to hold an integral number of chunks that will contain the X bytes? If the chuck size is Y, then the answer is: CEILING(X,Y)
When you use an integer division in C like this
y = a / b
you get a result of division rounded towards zero, i.e. 5 / 2 == 2, -5 / 2 == -2. Sometimes it's desirable to round it another way so that 5 / 2 == 3, for example, if you want to take minimal integer array size to hold n bytes, you would want n / sizeof(int) rounded up, because you want space to hold that extra bytes.
So this macro does exactly this: CEILING(5,2) == 3, but note that it works for positive y only, so be careful.
Hmm... English example... You can only buy bananas in bunches of 5. You have 47 people who want a banana. How many bunches do you need? Answer = CEILING(47,5) = ((47 + 5) - 1) / 5 = 51 / 5 = 10 (dropping the remainder - integer division).
Let's try some test values
CEILING(6, 3) = (6 + 3 -1) / 3 = 8 / 3 = 2 // integer division
CEILING(7, 3) = (7 + 3 -1) / 3 = 9 / 3 = 3
CEILING(8, 3) = (8 + 3 -1) / 3 = 10 / 3 = 3
CEILING(9, 3) = (9 + 3 -1) / 3 = 11 / 3 = 3
CEILING(10, 3) = (9 + 3 -1) / 3 = 12 / 3 = 4
As you see, the result of the macro is an integer, the smallest possible z which satisfies: z * y >= x.
We can try with symbolics, as well:
CEILING(k*y, y) = (k*y + y -1) / y = ((k+1)*y - 1) / y = k
CEILING(k*y + 1, y) = ((k*y + 1) + y -1) / y = ((k+1)*y) / y = k + 1
CEILING(k*y + 2, y) = ((k*y + 2) + y -1) / y = ((k+1)*y + 1) / y = k + 1
....
CEILING(k*y + y - 1, y) = ((k*y + y - 1) + y -1) / y = ((k+1)*y + y - 2) / y = k + 1
CEILING(k*y + y, y) = ((k*y + y) + y -1) / y = ((k+1)*y + y - 1) / y = k + 1
CEILING(k*y + y + 1, y) = ((k*y + y + 1) + y -1) / y = ((k+2)*y) / y = k + 2
You canuse this to allocate memory with a size multiple of a constant, to determine how many tiles are needed to fill a screen, etc.
Watch out, though. This works only for positive y.
Hope it helps.
CEILING(x,y) gives you, assuming y > 0, the ceiling of x/y (mathematical division). One use case for that would be a prime sieve starting at an offset x, where you'd mark all multiples of the prime y in the sieve range as composites.
I often hear the term "linear interpolation" in context with animations in WPF. What exactly does "linear interpolation" mean? Could you give me an example where to use "linear interpolation"?
Linear means lines (straight ones).
Interpolation is the act of finding a point within two other points. Contrast this with extrapolation, which is finding a point beyond the ends of a line.
So linear interpolation is the use of a straight line to find a point between two others.
For example:
*(5,10)
/
/
/
/
*(0,0)
You can use the two endpoints with linear interpolation to get the points along the line:
(1,2)
(2,4)
(3,6)
(4,8)
and linear extrapolation to get (for example):
(1000,2000)
(-1e27,-2e27)
In animation, let's say you have a bouncing ball that travels from the (x,y) position of (60,22) to (198,12) in 10 seconds.
With an animation rate of 10 frames per second, you can calculate it's position at any time with:
x0 = 60, y0 = 22
x1 = 198, y1 = 12
frames = 100
for t = 0 to frames:
x = (x1 - x0) * (t / frames) + x0
y = (y1 - y0) * (t / frames) + y0
Those two formulae at the bottom are examples of linear interpolation. At 50% (where t == 50):
x = (198 - 60) * (50 / 100) + 60
= 138 * 0.5 + 60
= 69 + 60
= 129
y = (12 - 22) * (50 / 100) + 22
= -10 * 0.5 + 22
= -5 + 22
= 17
and (129,17) is the midpoint between the starting and ending positions.
E.g. when you want a storyboard to move an element from one position to another using a fixed speed, then you'd use linear interpolation between the start and end positions.