Shop OBEX P1 Docs P2 Docs Learn Events
Convert my brain to 16bit please! — Parallax Forums

Convert my brain to 16bit please!

Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
edited 2011-01-15 15:56 in Propeller 1
Could someone take a moment to explain why/how the following line of code works?

word %%01333310

The Propeller Manual doesn't seem to address binary to word.

My understanding of this would go a a long way to converting my 8bit brain to 16bits.

Thanks
OBC

Comments

  • jazzedjazzed Posts: 11,803
    edited 2011-01-15 10:45
    Quartenary numbers. Somebody check it:

    %%01333310 =
    %%01_33_33_10 =
    %0001_1111_1111_0100 =
    $1ff4

    dibbles :)
  • potatoheadpotatohead Posts: 10,261
    edited 2011-01-15 10:48
    That's it exactly.

    Two bits per digit OBC, means 8 digits = 16 bits. It's like a two color display vs a 4 color one.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-01-15 10:51
    Ok, I'm starting to see it. So how to I convert a byte upward?

    word[p][0]:=c0

    for

    word %%00000000

    Just converting my decimal to Quaternary isn't getting done, but
    I'm AM getting an education.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-15 10:57
    I wasn't sure I'd ever use quarternary numbers and then my friend at Disneyland needed some code for a little 8-bit LED array that was part of a circuit I helped him create. Since there were plenty of cogs I added a dimming driver for the LEDs and let him build light patterns in a table like this:
    long    %%0_0_2_0_0_0_0_1, 70
    

    ... where the first value (quarternary) specifies the lighting level for each of the outputs. A lookup is used to set the actual channel brightness. In the end it was much better than simple on/off LED bits.
  • potatoheadpotatohead Posts: 10,261
    edited 2011-01-15 11:12
    OBC, if you want to convert a byte upward, it's really easy to make a look up table, with 16 entries.

    Half a byte is a nibble, which is one Hex digit. The table takes 4 bits, and then outputs one byte. You can use a bit mask to select the translated "color".

    0000 = 0000_0000
    0001 = 0000_0011
    .
    ,
    1101 = 1111_0011
    1110 = 1111_1100
    1111 = 1111_1111

    So you get two bits, both set to one, based on the value of a nibble, two per byte. This makes things kind of easy, because when you see $f1, you can look at the table, and see that the output would be: 1111_1111_0000_0011

    From there, setup some 16 bit masks to turn off some of those ones, based on whether or not you want color 1, 2, 3.

    Color 01 = 0101_0101_0101_0101
    Color 02 = 1010_1010_1010_1010
    Color 03 = 1111_1111_1111_1111

    Then just and the result of your table output with the color mask, leaving you with a byte properly converted to a 16 bit graphics entry.

    Tableout := tableout & Color.

    Remember "and" only outputs a 1, if both arguments are 1. Or, outputs a 1, if either of them are 1, and xor toggles the state of the bits.

    And can be used to insure that a specific digit is a zero, regardless of the input using a zero to mark that digit. Or can be used to insure it's a one, and you use a 1 for that digit, and xor will toggle any digit where you've supplied a 1 in your mask argument.

    I've got that in my tutorial that I'm updating. (and it's slow, but I'm nearly there)

    BTW: You can use the bitmasks to isolate the part of the byte you are converting. Just shift and mask.

    So, your byte is $F1. And it with $0F, and you get the lower nibble. use it with the table to get your output. This is your lower byte of the word.

    Then take that same source byte, and with $f0, and you have the upper nibble. Shift it right 4 bits, plug it in to the table, then shift the product of that left 8 bits to get the upper byte of the word.

    Finish up by using OR to combine the lower byte and upper byte values into one word.

    Word = lower byte OR upper byte. (that's basically a add, if you don't have stray bits laying around)

    Edit:

    Source byte is $E1, which is %11100001.

    Lookup lower nibble: $E1 AND $0F = %0001

    Use table: %0001 = %00_00_00_11 = %%0003

    Lookup upper nibble: $E1 AND $F0 = $E0 = %1110_0000

    Shift it right for the table lookup $E0 >> 4 = $0e = %0000_1110

    Use table: %1110 = %11_11_11_00 = %%3330

    This needs to be combined, as it's the upper byte of a 16 bit word, so shift left!

    %%3330 << 8 = %%3330_0000

    Now the way is clear to OR them together to complete the desired word

    %%3330_0000 OR %%0003 = %%3330_0003 = %11111100_00000011.

    Finally, mask off color. Right now, both bits are set, so it's always color three. Maybe you want color 2?

    %11111100_00000011 AND %10101010_10101010 = $10101000_00000010

    (only the bits with a one in both places will be a one, otherwise zero)

    That's also:

    %%3330_0003 AND %%2222_2222 = %%2220_0002

    Since you were fetching milk, I thought I would edit some. Hope you didn't print it!

    Bit masks are how we operate on digits. On any CPU, there are operand sizes. Could be 5 bits, 8, 9, 16, whatever. We pack the data into the word sizes, and then we use masks to get at parts of that data, shifting this way and that to align it for computation, or display, etc...

    That's the core of what is going on here.

    You could, if you wanted to, use a much smaller table, say four entries, and just do the operation more times. Process one digit, shift stuff, combine, process another digit, shift, combine. The trade off is speed VS table size in HUB.

    In Potatotext, I've got a big table, that just holds all 256 values for 8 bit to 16 bit conversion to a specific color. That's literally a load, index on the table, store to do the conversion, but the table is big, but it's fast too.

    So it looks like a lot, and it is to explain. Once you do it a time or two, this boils down to a few SPIN statements, and will look easy.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-01-15 11:51
    Gotta grab some milk... It's gonna take me a little bit to digest that!

    Thanks guys!

    OBC
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-01-15 12:11
    OBC: your brain is much slower than it was back in your 30's (a few days ago haha) :)
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-01-15 15:56
    @potatohead:

    An excellent tutorial!

    @Cluso99:

    Thanks for the reminder.. :p It'll still soak into this old brain eventually. :)

    OBC
Sign In or Register to comment.