Shop OBEX P1 Docs P2 Docs Learn Events
Dumb question about "bit 0" in Propeller Manual. — Parallax Forums

Dumb question about "bit 0" in Propeller Manual.

The SHR page (p 348 v1.1 manual) specifies that if the WC effect is specified, the C flag is set to the Value's original bit 0.

Is that the MSB or LSB of the d-field (value)??

Kinda confused on the terminology.

Thanks in advance.
--Terry

Comments

  • Bit 0 is the LSB.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2017-01-14 19:00
    Terminology then -

    d-field - this is a 9-bit field in the "instruction" specifying the destination of the 32-bit value. The instruction is 32-bits long that has two 9-bit fields that specifies a source and destination.

    So it is the destination value that is pointed to by the d-field that is operated on, not the d-field itself.

    SHR - shifts the 32-bits of a long right by one or more bits where bit 0 is the least significant bit (least value). Now because SHR can shift multiple bits right and not just one bit then it is necessary to state what sets the carry bit or C flag. For instance, if I performed 4 single SHR operations one bit at a time then finally the carry bit would be equal to the "last" bit 0 that was shifted, that is b3. But that is not what happens when you tell SHR to do so by 4 bits in one operation, it is simply whatever bit 0 of the destination value was before the operation

    Assume that destination labeled "dest" is set to this value initially, so we will use a mov op to show this:
    mov dest,%0100_1111_1010_0101
    shr dest,#1
    So C = 1 because the rightmost bit of dest which is bit 0 is 1.

    Even if we shifted by #4 instead of #1 the C value would still be equal to the original bit 0, that is 1.
    mov dest,%0100_1111_1010_0101
    shr dest,#4 ' C = 1 (original bit 0)


    But if we had done this instead:
    mov dest,%0100_1111_1010_0101
    shr dest,#1 ' C = 1 (original bit 0)
    shr dest,#1 ' C = 0 (original bit 1)
    shr dest,#1 ' C = 1 (original bit 2)
    shr dest,#1 ' C = 0 (original bit 3)

    You can see that C = the original bit 3 instead which is what some programmers might think C is set to if #4 had been specified in one operation, but that is not the case.

    Hope I haven't confused you.
  • You need WC at the end of your shift instructions -- no? Otherwise C will not be affected.
  • JonnyMac wrote: »
    You need WC at the end of your shift instructions -- no? Otherwise C will not be affected.

    Absolutely, I think I should have formatted any PASM code properly although it was 4 in the morning here :)

  • Thanks guys! When I looked at SHL and saw that C would be populated with bit31 (MSB) when WC is specified, I realized it was the LSB for SHR.

    I converted the following code to SPIN, so I could generate a CRC8 using the Maxim algorithm.
    The following lines are what I was curious about. Seems that could have been done with an AND, as t1 was no longer used. That's what I did in spin.
    shr     t1, #1 wc
              if_c          xor     value, #$8C
    
    'lifted from Parallax's OneWire.spin
    '------------------------------------------------------------------------------
    ' parameters: byte count, address pointer
    ' return:     crc8
    '------------------------------------------------------------------------------
    
    cmd_crc8                rdlong  dataCnt, t2             ' get number of bytes
                            add     t2, #4                  ' get data pointer
                            rdlong  dataPtr, t2
    
                            mov     value, #0               ' clear CRC
    
    :nextByte               rdbyte  addr, dataPtr           ' get next byte
                            add     dataPtr, #1
                            mov     bitCnt, #8
    
    :nextBit                mov     t1, addr                ' x^8 + x^5 + x^4 + 1 
                            shr     addr, #1
                            xor     t1, value
                            shr     value, #1
                            shr     t1, #1 wc
              if_c          xor     value, #$8C
                            djnz    bitCnt, #:nextBit  
                            djnz    dataCnt, #:nextByte
                            jmp     #endCommand
    
    ' My spin version
    
    pub crc8_maxim(array,numchar) | crcvalue, addr, t1, i
      crcvalue := 0
      i := 0
        repeat numchar
          addr :=  byte[array][i]
           i++
          repeat 8
            t1 := addr
            addr ->= 1
            t1 ^= crcvalue
            crcvalue ->= 1
            if (t1 & 1) == 1
              crcvalue ^= $8C
          
      return crcvalue   
    

    As an aside, Jon, I am using this and a slightly modified version (1 stop bit instead of 2) of your IR routines on the Parallax convention badge to control some Disney "Magic Ears" :) Super fun stuff!
  • JonnyMacJonnyMac Posts: 9,104
    edited 2017-01-15 21:35
    This is the Spin CRC method I use in my own 1-Wire object; it was written by Micah Dowty. My PASM 1W code uses a CRC routine by Cam Thompson (with wrapper code by me so I can call it from Spin).
    pub crc8x(p_src, n) : crc | b                                     
                                                                      
    '' Returns CRC8 of n bytes at p_src
    '' -- implementation by Micah Dowty                               
                                                           
      repeat n                                                        
        b := byte[p_src++]                                            
        repeat 8                                                      
          if (crc ^ b) & 1                                            
            crc := (crc >> 1) ^ $8C                                   
          else                                                        
            crc >>= 1
          b >>= 1
    
    ' ----------------------                                         
    ' Calculate CRC8                                                 
    ' * code by Cam Thompson                                         
    ' ----------------------                                         
    '                                                                
    owcalccrc               mov     value, #0                       ' clear workspace 
                            shr     bytecount, #8           wz      ' clear command from count
            if_z            jmp     #savecrc                         
                            rdlong  hubpntr, iopntr                 ' get address of array
                                                                     
    crcbyte                 rdbyte  t1, hubpntr                     ' read byte from array
                            add     hubpntr, #1                     ' point to next
                            mov     bitcount, #8                     
                                                                     
    crcbit                  mov     t2, t1                          ' x^8 + x^5 + x^4 + 1  
                            shr     t1, #1                           
                            xor     t2, value                        
                            shr     value, #1                        
                            shr     t2, #1                wc         
            if_c            xor     value, #$8C                      
                            djnz    bitcount, #crcbit                
                            djnz    bytecount, #crcbyte              
                                                                     
    savecrc                 wrlong  value, iopntr                   ' update hub
                            jmp     #cmdexit
    
    As an aside, Jon, I am using this and a slightly modified version (1 stop bit instead of 2) of your IR routines on the Parallax convention badge to control some Disney "Magic Ears" Super fun stuff!
    I, too, have a set of those and have controlled them from a Propeller. I work with the tech crew at the park (mostly custom Propeller code), and in the future there may be a Propeller-powered prop in the kiddie section of the primary park that will control those ears.
Sign In or Register to comment.