what is the n and n outputs? Digital, or analog (assuming digital but don't like to assume). If so, what is the max numbers... so we can determine how many bits...
You could add the numbers together n number of times. For this you would need an adder with n^2 -1 bits. Thus the adder output would go to a D-register which is also one of the inputs to the adder with n being the other. A counter would perform the addition by clocking the D register n-1 times.
@crutschow
performing (n-1) add operations would be very inefficient.
Same as in SW, you wouldn't do (n-1) add operations, but you'd rather use log(n) shift-left operations.
How would you implement the following by HW?
if n = 1010
then
n^2 = 1010 << 1 + 1010 << 3
I wasn't worried about efficiency, just a simple way to to the task.
Here's a shift-and-add scheme that should work to multiply any two numbers in n steps. You need one n-bit shift register and one 2n-bit shift register. You put the number into the n-bit shift-register LSB first. You put the second number (or same number if you are squaring) into the 2n-bit shift-register MSB first. You look at the last (LSB) bit of the first shift register. If it's 1 you add the contents of the 2n-bit shift register to a 2n-bit accumulator (D-latch) register, if it's a 0 you don't. Then you shift each shift-register one step in their respective registers and look at the last bit of the n-bit register (which is now second to the LSB). Again you add the shift register contents if it's a 1 and not if its a 0. Repeat this N times. The multiplied (or squared) number is now in the accumulator.
This is how it's done in hardware...
1. clear counter 2. enable counter for timing period 3. disable timer... you now have your count = n = multiplicand.
4. Load shift register with counter value = multiplier (note: since we're doing n² then multiplicand = multiplier). 5. Clear Accumulator (Accumulator must be x * 2 bits wide).
6. check shift register LSB. If 1, add multiplicand + accumulator register back into accumulator.
7. shift the Shift register once to the right, shift the accumulator once to the right.
8. Repeat 6-7 x times (x = number of bits in counter/shift register). 9. Shift the accumulator x more times to the right to resync MSB/LSB. Note: If the accumulator is larger than 2x bits, then you have to shift it that many more times. Results in accumulator = n².
I would notice that n^2 = (n-1)^2 + (n-1) + n (Here ^2 stands for square)
Then at every change you just get (n-1)^2, which you already have on the second output, and add (n-1), which you already have on the first output, then you advance the first counter, so that it has n now on the first output, and, once it's done, you add this n to the second counter, so that now it holds (n-1)^2 + (n-1) + n = n^2.
Probably the simplest would be to use a microcontroller. Doing shifts and adds, as well as something to control the sequence will take a small handful of chips. A uC would do it all with one chip.
Just choose a part with enough pins to output N and N^2.
I would notice that n^2 = (n-1)^2 + (n-1) + n (Here ^2 stands for square)
Then at every change you just get (n-1)^2, which you already have on the second output, and add (n-1), which you already have on the first output, then you advance the first counter, so that it has n now on the first output, and, once it's done, you add this n to the second counter, so that now it holds (n-1)^2 + (n-1) + n = n^2.
This is how it's done in hardware...
1. clear counter 2. enable counter for timing period 3. disable timer... you now have your count = n = multiplicand.
4. Load shift register with counter value = multiplier (note: since we're doing n² then multiplicand = multiplier). 5. Clear Accumulator (Accumulator must be x * 2 bits wide).
6. check shift register LSB. If 1, add multiplicand + accumulator register back into accumulator.
7. shift the Shift register once to the right, shift the accumulator once to the right.
8. Repeat 6-7 x times (x = number of bits in counter/shift register). 9. Shift the accumulator x more times to the right to resync MSB/LSB. Note: If the accumulator is larger than 2x bits, then you have to shift it that many more times. Results in accumulator = n².
It doesn't matter if you shift left or right, as long as you do it the exact number of times as you have bits in the register. Every time you shift, you are weighting the added values by the multiplier bit position. When you do math long hand, you're actually shifting right, you work from right to left, you are moving your 'pointer' left, so the data you are working with is actually shifting right...
So you start with the LSB, if a '1', you add that value in, then you shift the result right, and the multiplier... if the new LSB bit is a '1', and you add in your value again, that value adds twice the value to the result as the previously added amount, because the result has been shifted right... and the bit in the multiplier has a weight of twice the previous LSB bit... so like in long hand math, every time you shift your pointer left, the intermediate result shifts left also, giving a weight of 10x to that value, but if you look at your old data in relation to the new data, it has shifted right! Since we're not shifting our multiplicand, we have to shift our result the other direction.
When you do left shifts, you start with the MSB, as that bit has the most weight, and if you're shifting left, then what you're adding in has less weight in the result than the previously added...