Archive for the 'Programming' Category

Writing binary data with Verilator

This is definitely one I know the masses are waiting to find out how to do. 🙂

So you are simulating a verilog design with Verilator and you want to output part of your design data to a file in binary format. (Example you're outputting an image.)

So lets say you want to do this:

  1.  
  2. fd=$fopen("somefile.dat", "wb");
  3. $fwrite ( fd, "%c", data );
  4.  

And you expect a file with binary data but instead you get a text file.

Well in Verilator you can embed c statements. So you can do this instead:

  1.  
  2. $c( "fwrite( (void*) &", data,", 1, 1, (FILE*)", fd, ");" );
  3.  

There. Fixed.

Printing on one line with Python

Ok so I've been using Python for a long time and have written a lot of lines of Python code. But this one is pretty simple and I'm kind of embarrassed that somehow I didn't know about it.

In the past, I've used sys.stdout when I wanted to print multiple things on one line, e.g., you want to print periods to show progress.

  1.  
  2. import sys
  3. for i in range (10):
  4. sys.stdout.write ( "%d.." % i )
  5.  

I also knew you could use print with more than one item.

  1.  
  2. print 1,2,3, "Hi"
  3.  

But the other day I inadvertently found that you can continue to print on one line if you just leave the trailing comma....

  1.  
  2. for i in range(10):
  3. print "%d.." % i, # no newline appended
  4. print # add a newline after the loop.
  5.  

And who cares you say??? Well now I don't have to import sys and use sys.stdout when a simple print can do just fine 🙂

Fun with C++11 Lambdas

So I got a c++11 book. I've been following a few of the language differences and features for a while, but I decided to break down and actually understand it all. I decided to have some fun with lambdas today. Lambdas are a very useful language feature that lots of dynamic languages have (JavaScript, Python, <insert your favorite there is plenty>). For some additional readying you might read about Closures and Functors.

Anyway, so here you go:

Basic syntax

  1.  
  2. [...] {...} // simplest form
  3. // or showing all the optional arguments
  4. [...] (...) mutable throwSpec -> retType {...}
  5.  

What does that all mean?

The brackets are a parameter list of values you can pass in for usage in the lambda from the context in which the lambda is being created. You could pass in local variables or this for instance. You can pass by a value or reference. Unlike other languages, if you pass by reference and the value is deleted, your lambda might have unexpected results or crash.

The parenthesis are the parameters you pass in when you call the lambda. Declared like a parameter list for any other function. auto is nice but if you need the type (like to return a lambda from a function) you can use the functional header.

Example:

  1.  
  2. #include <functional>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. function<int(int,int)> getTheLambda()
  7. {
  8. int x=7;
  9. // will return an undefined value because x is going to be removed from
  10. // the stack when this function exits.
  11. return [&x] (int a, int b) { return x+a+b; };
  12. }
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16.  
  17. auto m = [] (int y) { cout << "lamda value: " << y << endl; };
  18. // call it
  19. m(3);
  20.  
  21. auto l=getTheLambda();
  22. cout << "Lambda Result: " << l(1,2) << endl;
  23.  
  24. return 0;
  25. }
  26.  

For me it printed:

  1.  
  2. ./test
  3. lamda value: 3
  4. Lambda Result: 32770
  5.  

If you change the first lamba in the function to "x" instead of "&x" you'll get the expected result of 10.

I can't wait to pass a lambda to a thread and use it as some type of callback!

git diff with color words

I had these files with very very long lines of text on each line. It was paragraphs worth of words but not word wrapped. These files were stored in a git repository. Now and then, someone would change a word. Diff with line based output was difficult to find what actually changed.

I figured there must be a way to see just the words. A little searching around the docs and I found it.

  1.  
  2. > git diff --word-diff=color
  3.  

Perfect. I still got ling scrolling lines though. You can override that with your $GIT_PAGER variable or I just piped the output manually for a one time fix:

  1.  
  2. > git diff --word-diff=color | less -r
  3.  

Much easier to see the changes!

Quick convert raw g711 uLaw audio to a .au file

I had reason to playback some raw g.711 audio data. I made the following script to convert the data to a .au file that is playable.

From the AU file spec.. which is way simple by the way:
You just need to add 6 32-bit header flags.

raw2au.py:

  1. import struct
  2.  
  3. header = [ 0x2e736e64, 24, 0xffffffff, 1, 8000, 1 ]
  4. o=open('out.au','wb')
  5. o.write ( struct.pack ( ">IIIIII", *header ) )
  6.  
  7. raw = open('in.raw','rb').read()
  8. o.write(raw)
  9. o.close()
  10.  
  11.  

You can probably see that this would be extremely simple to use with other audio formats too. Modify as you need.
Header Bytes:

  1. Const AU identifier: 0x2e736e64
  2. Size of Header: 24.
  3. Size of File: use 0xffffffff for unknown, or you could replace with the actual size.
  4. Audio format. 1 is uLaw. From the spec choose whatever you need if it isn't uLaw.
  5. Sample rate. uLaw is always 8000.
  6. Channels. 1 channel in this case.

Rendering to a texture with iOS 5 texture cache api.

There are a couple examples of how to take image input, perhaps from the iPhone camera, or from your own image, fast map those image to an OpenGl texture, and then render them with an OpenGL shader. This post is for those of you who don't necessarily want to render the image on the screen, but want to perform some OpenGL operations and then read the image back out. Luckily, the same API that allows you to map the images to textures, also allows you to read data back out from the textures without having to use glReadPixels(...) or some other method that takes a long time. Here's how it's done...

Continue reading 'Rendering to a texture with iOS 5 texture cache api.'



css.php