Bitfields and Logic Basics CS/COE 0447 (term 2181) Jarrett Billingsley
Class announcements hi :) how did you feel about the exam how's the project goin BREAK THINGS UP INTO FUNCTIONS, OH MY LORD let's look at mine to refresh your memory on how it's supposed to look/work 10/3/2017 CS/COE 0447 term 2181
bonk Read the dang project description. When moving the ball, move along one axis at a time, checking for collision along each axis separately. This will make it much easier. Ball is moving up-right. I'd say: Look right. Is there anything in the way? No? Then we can move there. Look up. Is there anything in the way? Yes? Destroy the block, and flip the vertical velocity. 10/3/2017 CS/COE 0447 term 2181
Checking for block collisions No looping necessary. No "checking each side" necessary. What are the row and column coordinates of each of these blocks? What are the x and y coordinates? If you have an x/y coordinate, can you turn that into a row/col? Once you do that, it's easy. get_block_address! AHAAAA! 10/3/2017 CS/COE 0447 term 2181
Lab 2 feedback Do not write a program all at once and then test it. Never turn in a program that doesn't compile. If your program is crashing, use the blue "step backwards" button after the crash to see what happened. What's the only places you should push/pop registers? DON'T do it around jal. get_block_address is supposed to… get the address of a block. This way you can either lb or sb to that address. 10/3/2017 CS/COE 0447 term 2181
Bitfields 10/3/2017 CS/COE 0447 term 2181
That's this color, in RGB565. The masters of meaning 23 32 19 15 11 10 We can give bit patterns any meaning we want. What if we wanted to store multiple integers in a 16-bit value? 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 red green blue decimal? 23 32 19 That's this color, in RGB565. 10/3/2017 CS/COE 0447 term 2181
I wanna turn the light on!! I have a sequence of 0s. I wanna turn one of them into a 1. What bitwise operation can I use to do that? ? 1 1 10/3/2017 CS/COE 0447 term 2181
I wanna turn the light off!! I wanna turn one of the 1s into a 0. What bitwise operation can I use to do that? 1 1 1 ? 1 1 1 1 1 10/3/2017 CS/COE 0447 term 2181
Turning off the first three, leaving the others alone Again, what operation can we do this with? 1 1 1 1 ? 1 1 1 1 1 10/3/2017 CS/COE 0447 term 2181
Bit shifting Besides AND, OR, and NOT, we can move bits around, too. 1 1 0 0 1 1 1 1 If we shift these bits left by 1… 1 1 0 0 1 1 1 1 0 We stick a 0 at the bottom. 1 1 0 0 1 1 1 1 0 0 Again! 1 1 0 0 1 1 1 1 0 0 0 AGAIN! 1 1 0 0 1 1 1 1 0 0 0 0 AGAIN!!!! 10/3/2017 CS/COE 0447 term 2181
Left-shifting in C/Java and MIPS (animated) C and Java use the << operator for left shift. B = A << 4; // B = A shifted left 4 bits MIPS has the sll (Shift Left Logical) instruction. sll t2, t0, 4 # t2 = t0 << 4 If the bottom 4 bits of the result are now 0s… …what happened to the top 4 bits? 0011 0000 0000 1111 1100 1101 1100 1111 0000 Bit Bucket 10/3/2017 CS/COE 0447 term 2181
<_< >_> <_< <_< >_> <_< We can shift right, too. 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 C/Java use >>, MIPS uses srl (Shift Right Logical). 10/3/2017 CS/COE 0447 term 2181
Let's look at this again. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 red green blue 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 10/3/2017 CS/COE 0447 term 2181
… Hmm. 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 1 1 1 1 0 0 0 0 0 1 0 0 1 1 10/3/2017 CS/COE 0447 term 2181
Left-shifting and ORing If you have the values of the fields, and you want to put them together into a bitfield, Shift each value left to the correct bit position OR the shifted values together For RGB565, Red is shifted left 11, green is shifted left 5, blue isn't shifted. color = (red << 11) | (green << 5) | blue; 10/3/2017 CS/COE 0447 term 2181
Going the other way Let's go from the bitfield to three separate values. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 We wanna put the red at bit position 0. 1 What about green? 1 Uh oh. 10/3/2017 CS/COE 0447 term 2181
Masquerade In order to isolate only the bits we want, we need a mask. This is a specially-constructed value that has: 1s in the bits that we want to keep 0s in the bits that we want to discard Which bits do we want to keep? Which do we want to discard? 1 & 1 The specially-constructed value is called a mask. 1 10/3/2017 CS/COE 0447 term 2181
Coming up with the mask value If you want to mask a 3 bit value, the mask is 1112. If you want to mask a 5 bit value, it's…? What's 23 in binary? 25? What do you notice about these numbers? 10/3/2017 CS/COE 0447 term 2181
Right-shifting and ANDing If you have a bitfield and you want to extract one or more values, Shift the bitfield right to put the desired value at bit position 0 AND that with 2n-1, where n is the number of bits in the value. For RGB565, The red and blue masks are 25-1 = 31 (or 0x1F) The green mask is 26-1 = 63 (or 0x3F) red = (color >> 11) & 0x1F; green = (color >> 5) & 0x3F; blue = color & 0x1F; 10/3/2017 CS/COE 0447 term 2181
Logic Basics 10/3/2017 CS/COE 0447 term 2181
Transistors Input Input Control Control Output Output A transistor is like a little valve, or switch. The input, output, and control are all single bits. The bits are represented as voltages (maybe 3.3V = 1, 0V = 0). N-type: sends input to output if control is a 1 Input Output Control P-type: sends input to output if control is a 0 Input Output Control Now just put 3 billion of them together! Who said EE was hard? 10/3/2017 CS/COE 0447 term 2181
This little bubble means "invert." Gates We can combine transistors in interesting ways to make gates. A gate implements one of the boolean logic functions. Let's start with the simplest: the NOT gate! Q is the output. A is the input. A Q This little bubble means "invert." 10/3/2017 CS/COE 0447 term 2181
AND, OR, and… XOR? A B Q 1 A B Q 1 A Q B A Q B A Q B We know about AND and OR. But what's XOR? A Q B AND gate A Q B OR gate A Q B XOR gate A B Q 1 A B Q 1 eXclusive OR means "one or the other, but NOT BOTH." 10/3/2017 CS/COE 0447 term 2181
If you give an electrical engineer a NAND gate… If you stick a NOT gate after an AND gate, you get a NAND gate. This kind of gate has a cool property: it's universal. In other words, you can build an entire computer with NANDs. But this isn't how real circuits are designed, at least anymore. Digital logic courses use them cause NAND gate chips are cheap. But in Logisim, we have infinite gates for free :D Use the kind of gate you need for the situation at hand. 10/3/2017 CS/COE 0447 term 2181
Time to bundle up If we want to, say, NOT a 32-bit value, we can draw it like: 32 It's a lot nicer than drawing 32 wires with 32 NOT gates. Logisim calls these wire bundles. It doesn't draw the slashes and numbers though… 10/3/2017 CS/COE 0447 term 2181