So I am working on a program in LC3 and I am having a issue with masking. I am fine with creating a mask of all 1's of the appropriate length, fine shifting everything to the appropriate field, but I can't for the life of me shift things back over to the right once I am done doing the AND comparison. How do you right shift in LC3? I saw something on iteration comparison, but I don't know how to compare bits in different positions, and to compare them in the same position I would have to shift one to the right eventually anyway. Am I missing something?
To simulate right shift, you just need to make two masks, one for source bit and one for destination bit:
src_mask=0x04; // read from bit position 2
dst_mask=0x01; // write to bit position 0
while(src) { // or while src_mask, because that too will eventually go to zero
if (src & src_mask) dst+=dst_mask; // or dst|=dst_mask
src &= (~src_mask);
dst_mask=dst_mask+dst_mask;
src_mask=src_mask+src_mask;
}
You might not need to do a right shift in this situation at all -- just save the starting field before shifting it left, then restore it from memory afterwards.
Related
I have a wire parameter expression that I am trying to make do one thing when the value is moving in the Negative X direction and another when moving in the Positive X. How do I specify this?
if <moving in positive direction>
then A
else B
I assume it would be something like if the new difference from zero is greater or less than the current one. That would work, but only if you stay in either negative or positive x-space. I just do not know how to write this.
I apologize if this is simple math. I am a bit new to this.
You will need a more advanced approach than wire parameters. Using a script controller is the easiest way. It has to save current position in a variable, let's say prevPos (and the same for frame number, if you want it to work correctly when scrubbing timeslider backwards) - which will be the last step, as you want to first subtract the prevPos from current pos and if the result is positive, do A, else do B.
So I have a word, and I want to loop through and test the left most bit. I have my word and I'm passing it to my subroutine, I know how to build a loop, I'm just not sure how to test the left most bit in the word.
Thanks for any help
The best way to do this is with bit masking -- perform a bitwise AND between the word you want to check and a bit mask with a 1 in any position you wish to test. i.e. in binary:
my word: 11
bitmask: 10
& ==
10
you can see that the 1 in the left side drops out. So to do something similar on a 16bit number:
0x0230 & 0x8000 = 0x0000
0xC020 & 0x8000 = 0x8000 != 0x0000
The important thing to note here is that if the bit is not present the AND returns a 0, and if the bit is present it returns something else. It doesn't matter what it is, just that it's not zero.
Not sure if it applies to your specific task but a simple approach could be performing a logical/arithmetic left shift to the word. This is simply done by adding the word to itself (which is equal to multiplying by 2 and thus shifting all the bits to the left 1 "spot). After doing this, the condition codes will be set (assuming you're using the GPRs) and you can test if the left most bit is a 1 or a 0 by checking if the shifted word is positive OR zero (hence the left most bit is a 0), or negative (hence the left most bit is a 1). Loop over the whole word following this approach and you'll be able to determine the value of each bit in your word. Hope this helps.
Now I'm trying to figure out best method for iterating over bits in FPGA. I'm using some variation of fast powering algorithm, a.k.a exponentiation by squaring (more precisely it's doubling and add algorithm for elliptic curve mathematics). To implement it on hardware, I know I must use FSM which does iteration. My problem is how to properly "handle" moving from bit to bit. My first thought was to switch order of bytes, but when my k = 17 is 32bit, I must discard first 27 bits, so it's rather stupid idea. Another concept was with "moving" 0001000 pattern and bitwise & it with number, but it also requires to find first nonzero bit.
TL&DR
Got for example k = 17 (32bits, so: 17x0 10001) and want to iterate 5 times (that means I start iteration on first "real" bit of number) knowing each bit I iterate over.
Language doesn't matter - I need only the algorithm, not solution in specific language. However, if it is easily done in Verilog, I wouldn't mind. :P
A dedicated combinatorial circuit to find the first nonzero bit, shift it to the first position and tell you the shift amount should be fairly light on resources.
In principle, the compiler should be able to find this solution on its own and improve on it:
if none of the top 16 bits are set, set bit 4 of the shift amount, and shift by 16.
if none of the top 8 bits are set, set bit 3 of the shift amount, and shift by 8.
...
The compiler should be able to find further optimizations on this.
Don not code for FPGA but still:
rewrite algorithm to iterate number x from LSB to MSB
then in each iteration bit shift x right by 1 bit
stop if x==0.
this way you have bit-scan inside your main loop and do not need additional cycles for it.
x!=0 is done easily by ORing all its bits together
C++ code example:
DWORD x = ...;
for (; x != 0; x >>= 1)
{
//here is your iteration loop stuff like:
if (DWORD(x & 1) !=0 ) ...;
}
Something like:
always # *
casex(num)
8XXX_XXXX: k = 32;
4XXX_XXXX: k = 31;
2XXX_XXXX: k = 30;
...
Should give you the value of k.
You can have a shift register which can be parallel loaded so you can write a 1 to the kth bit, so you know when your iterations have ended.
If you loop from 0 to 31 and discard the 27 leading zeros...you aren't necessarily wasting cycles. Depends on whether you've surrounded this with a synchronous process, or a asynchronous one.
One gives you a rather small clocked circuit with a 32 clock latency.
The other gives you a giant rats nest of ANDs and ORs which won't run at a very high frequency.
Depends on what you want. Remember though, that even if you do decide to loop over 32 clocks, you can PIPELINE it such that you start a new calculation every clock. It might take you 32 clocks to get an answer, but you CAN do them at high speed.
I am doing a VLSI Project and I am implementing a Barrel Shifter using a tool called DSCH.The schematic for the same is realized using Transmission Gates.
What the circuit does is, it ROTATES the 8 bit word(8-bit shifter) with as many rotations chosen from a decoder in one clock cycle.
But I want to know the use of a rotator and why is it still called a shifter even though it's rotating.
Also please help me with some applications regarding Rotator which could be added to the present circuit to show it's use?
Rotation is just shifting with the bit exiting from one end fed back into the input at the other end, possibly by way of the carry flag bit. At the level of a simple implementation, it would make sense to have one circuit for both operations, with some additional control lines to select the source at the input side between the output of the other side, 0, or 1. Sign extension during right shift of 2's complement numbers would be another selectable option often built in.
The stackexchange sites aren't really suited to "list" questions including about applications, but a couple come to mind:
If you want a vector to test every bit of another value in turn, and to do so repeatedly, you could just keep rotating an initial one-bit-active value over an over, never having to re-initialize it.
You could swap a two-part (typically double-byte) value to imitate the opposite endieness of encoding by rotating it half way. Or putting it another way, it can be a single-operation swap of the values of two pairable but also independently accessible registers (think AL and AH together making up AX in real mode x86). But this will not work two endian swap a four-part value, such as a 32-bit value on a byte-addressable machine.
Various coding, checksum, and hashing schemes may wish to transform a value
I am writing routines for LC3 simulator, I have successfully wrote Shift Right routine, but I am now stuck with Rotate Right routine, it should move bits right and during each move to the right the least significant bit is copied into the most significant bit. I have AND, NOT, ADD operations, data movement operations, seven registers to store values and whole range of memory. I just need some basic ideas how it could be implemented.
You can test the LSB by performing an AND operation against 0x01. If the result is 1, you want the MSB to become 1, which you can do by ADDing 0x80.