donderdag 27 december 2012

shifting time

   
 #include <stdio.h>  
   
 int main(int argc, const char * argv[])  
 {  
     
   // 0000 0011  
   // 0011 0000  
   // 0011 0010  
   // 0011 0010 0000 0000 0000  
   // in hex 32000  
     
   unsigned int x = (((3 << 4) | 2) << 12);  
     
   printf("value of (((3 << 4) | 2) << 12) = %x",x);  
     
   return 0;  
 }  
   

fake embedded address

    
 #include <stdio.h>  
 #include <memory.h>  
 #include <stdlib.h>  
 #include <stdint.h>  
   
 // the 64 bit pointer point to a 32 bit value not yet known  
 uint32_t * base;  
   
 // to mimic embedded behaviour  
 uint64_t sdram_base;  
   
 // can be used until after allocation of base !  
 #define wr_mode(x) (*((volatile uint64_t *) (sdram_base | (x))))  
   
 int main(int argc, const char * argv[])  
 {  
   
     
   // get 1K of heap memory to fake sdram memory zone -> default blanked out with zero  
   base = malloc(1024);  
   
   // put 64 bit pointer into uint64_t to fake the constant base address of the sdram  
   sdram_base = ( uint64_t ) base;  
     
   // show the address -> will be 64bit on 64 bit OS target  
   printf("address = %p\n",base);  
   
   // show the address as a value  
   printf("address = %llx\n",sdram_base);  
   
   // show the value default takes the first 4 bytes of memory zone  
   printf("address = %x\n",*base);  
     
   // assign value -> be ware little endian on intel cpu  
   *base = 0xDEADBEAF;  
     
   // show the value of the first 4 bytes (uint32_t *) of memory zone  
   printf("address = %x\n",*base);  
   
   // equal on the above  
   printf("address = %llx\n\n",wr_mode(0));  
   
   printf("output below will differ on big endian targets !\n\n");  
   
   // 1 byte further in memory  
   printf("address = %llx\n",wr_mode(1));  
   
   // 2 byte further in memory  
   printf("address = %llx\n",wr_mode(2));  
   
   // 3 byte further in memory  
   printf("address = %llx\n",wr_mode(3));  
   
   // 4 byte further in memory will be zero again  
   printf("address = %llx\n",wr_mode(4));  
   
   free(base);  
     
   return 0;  
 }