Commits

carl douglas committed 9f55ed6

write-bits works

Comments (0)

Files changed (5)

               "bit_stream_read_bits" 
               (c-pointer bit-stream) unsigned-int))
 
-(define read-8-bits
-    (lambda()
-        (let ((ch (read-char)))
-            (cond ((eof-object? ch) #f)
-                  (else (char->integer ch))))))
-
-(define bit-stream (c-initialize-bit-stream read-8-bits))
-
-(define (bit-stream-read n)
-  (c-bit-stream-read-bits bit-stream n))
-
-(define (llrp-read-message-header)
-  (list (cons 'reserved (bit-stream-read 3))
-        (cons 'version  (bit-stream-read 3))
-        (cons 'type     (bit-stream-read 10))
-        (cons 'length   (bit-stream-read 32))
-        (cons 'id       (bit-stream-read 32))))
-
-(define (llrp-read-tlv-parameter-header)
-  (list (cons 'reserved (bit-stream-read 6))
-        (cons 'type     (bit-stream-read 10))
-        (cons 'length   (bit-stream-read 16))))
-;(define blob '${ 04 3d 00 00 00 44 00 00 00 00 00 f0 00 3a 8d a4 } )
-
-(printf "~A~%" (llrp-read-message-header))
-(printf "~A~%" (llrp-read-tlv-parameter-header))
-
-(c-finalize-bit-stream bit-stream)
+(define c-bit-stream-write-bits
+  (foreign-safe-lambda
+    void
+    "bit_stream_write_bits"
+    (c-pointer bit-stream) unsigned-int unsigned-integer64))
 
-(compile -s -O2 -d1 bitstream.scm -j bitstream)
-(compile -s bitstream.import.scm -O2 -d0)
-(compile -c -O2 -d1 bitstream.scm -unit bitstream -j bitstream)
+(compile -s bitstream.scm codec.c)
 
 (install-extension
   ; Name of your extension:
   'bitstream
   ; Files to install for your extension:
-  '("bitstream.o" "bitstream.so" "bitstream.import.so")
+  '("bitstream.so")
   ; Assoc list with properties for your extension:
   '((version 0.1)
-    (static "bitstream.o"))) ;; for static linking
+;    (static "bitstream.o")
+)) ;; for static linking
 
+(require-library bitstream)
+
+(define read-8-bits
+  (lambda()
+    (let ((ch (read-char)))
+      (cond ((eof-object? ch) #f)
+            (else (char->integer ch))))))
+
+(define write-8-bits
+  (lambda(ch)
+    (printf "~X:" ch)))
+
+(define bit-stream-reader (c-initialize-bit-stream read-8-bits))
+(define bit-stream-writer (c-initialize-bit-stream write-8-bits))
+
+(define (bit-stream-read n)
+  (c-bit-stream-read-bits bit-stream-reader n))
+(define (bit-stream-write n v)
+  (c-bit-stream-write-bits bit-stream-writer n v))
+
+(define (llrp-read-message-header)
+  (list (cons 'reserved (bit-stream-read 3))
+        (cons 'version  (bit-stream-read 3))
+        (cons 'type     (bit-stream-read 10))
+        (cons 'length   (bit-stream-read 32))
+        (cons 'id       (bit-stream-read 32))))
+
+(define (llrp-write-message-header)
+  (begin
+    (bit-stream-write 3  0)
+    (bit-stream-write 3  1)
+    (bit-stream-write 10 61)
+    (bit-stream-write 32 68)
+    (bit-stream-write 32 0)))
+
+(define (llrp-read-tlv-parameter-header)
+  (list (cons 'reserved (bit-stream-read 6))
+        (cons 'type     (bit-stream-read 10))
+        (cons 'length   (bit-stream-read 16))))
+
+(printf "~A~%" (llrp-read-message-header))
+(printf "~A~%" (llrp-read-tlv-parameter-header))
+
+(llrp-write-message-header)
+(printf "~%")
+
+(c-finalize-bit-stream bit-stream-reader)
+(c-finalize-bit-stream bit-stream-writer)
+
+
+
 void 
 bit_stream_write_bits
 (bit_stream *st, unsigned int length, unsigned long long value) {
-  unsigned i = 0;
-  unsigned int bit;
-  for (i = 0; i < length; i++, st->offset++) {
-    bit = value & (1 << (length - 1 - i));
-    if ((st->offset % 8) == 0) {
+  unsigned int index    = 0;
+  unsigned int bit_offset  = 0;
+  unsigned int bit = length - 1;
+  for (index = 0; index < length; index++) {
+    unsigned long long b     = 0;
+    if ((st->offset > 7)) {
+      C_save(C_fix(st->octet));
+      C_callback(st->thunk, 1);
+      st->offset = 0;
+      st->octet = 0;
     }
+    bit_offset  = 7 - st->offset;
+    b = (value & (1 << (bit))) >> (bit);
+    if (b == 0) st->octet &= ~(1 << bit_offset);
+    else        st->octet |=  (1 << bit_offset);
+    bit--;
+    st->offset++;
   }
 }
 
 
 
 typedef struct bit_stream {
-  unsigned octet;
-  unsigned offset;
+  unsigned char octet;
+  unsigned int offset;
 
   C_word thunk;
 } bit_stream;