• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <arpa/inet.h>  /* for htons() etc.. */
3 
4 static char  tab[8];
5 
6 static void
read4(int o,unsigned val)7 read4( int  o, unsigned val )
8 {
9     unsigned  v = htonl(val);
10     unsigned  v2;
11 
12     tab[o+0] = (char)(v >> 24);
13     tab[o+1] = (char)(v >> 16);
14     tab[o+2] = (char)(v >> 8);
15     tab[o+3] = (char)(v);
16 
17     printf( "read4: offset=%d value=%08x: ", o, val );
18     fflush(stdout);
19 
20     v2 = *(unsigned*)(tab+o);
21 
22     if (v2 != val) {
23         printf( "FAIL (%08x)\n", v2 );
24     } else {
25         printf( "ok\n" );
26     }
27 }
28 
29 static void
writ4(int o,unsigned val)30 writ4( int  o, unsigned val )
31 {
32     unsigned  v = htonl(val);
33     unsigned  v2;
34 
35     printf( "writ4: offset=%d value=%08x: ", o, val );
36     fflush(stdout);
37 
38     *(unsigned*)(tab+o) = v;
39 
40     v2 = ((unsigned)tab[o+0] << 24) |
41          ((unsigned)tab[o+1] << 16) |
42          ((unsigned)tab[o+2] << 8 ) |
43          ((unsigned)tab[o+3]      );
44 
45     if (v2 != val) {
46         printf( "FAIL (%08x)\n", v2 );
47     } else {
48         printf( "ok\n" );
49     }
50 }
51 
52 static void
read2(int o,unsigned val)53 read2( int  o, unsigned val )
54 {
55     unsigned short v = htons(val);
56     unsigned short v2;
57 
58     tab[o+0] = (char)(v >> 8);
59     tab[o+1] = (char)(v);
60 
61     printf( "read2: offset=%d value=%08x: ", o, val );
62     fflush(stdout);
63 
64     v2 = *(unsigned short*)(tab+o);
65 
66     if (v2 != val) {
67         printf( "FAIL (%04x)\n", v2 );
68     } else {
69         printf( "ok\n" );
70     }
71 }
72 
73 static void
writ2(int o,unsigned val)74 writ2( int  o, unsigned val )
75 {
76     unsigned short v = htons(val);
77     unsigned short v2;
78 
79     printf( "writ2: offset=%d value=%08x: ", o, val );
80     fflush(stdout);
81 
82     *(unsigned short*)(tab+o) = v;
83 
84     v2 = ((unsigned)tab[o+0] << 8) |
85          ((unsigned)tab[o+1]       );
86 
87     if (v2 != val) {
88         printf( "FAIL (%08x)\n", v2 );
89     } else {
90         printf( "ok\n" );
91     }
92 }
93 
94 
95 
main(void)96 int  main(void)
97 {
98     read4( 0, 0x12345678 );
99     writ4( 0, 0x12345678 );
100     read4( 1, 0x12345678 );
101     writ4( 1, 0x12345678 );
102     read4( 2, 0x12345678 );
103     writ4( 2, 0x12345678 );
104     read4( 3, 0x12345678 );
105     writ4( 3, 0x12345678 );
106 
107     read2( 0, 0x1234 );
108     writ2( 0, 0x1234 );
109     read2( 1, 0x1234 );
110     writ2( 1, 0x1234 );
111     read2( 2, 0x1234 );
112     writ2( 2, 0x1234 );
113     read2( 3, 0x1234 );
114     writ2( 3, 0x1234 );
115 
116     return 0;
117 }
118