Forum Index
Bluespec Forums
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

passing Fmt to BDPI

Post new topic   Reply to topic Forum Index -> Tools: Bluesim
View previous topic :: View next topic  
Author Message

Joined: 17 Aug 2007
Posts: 69
Location: University of Texas at Austin

PostPosted: Mon Oct 27, 2008 1:22 am    Post subject: passing Fmt to BDPI Reply with quote


[Long story cut short: currently, it is not possible to pass Fmt arguments to a BDPI function. Is there a workaround for this?]

I have a design which dumps debug logs into files (using $fopen, $fwrite etc) with lots of $format and fshow() expressions. However, I find the capabilities of $fwrite to be insufficient for my purposes, especially since these dumps have started taking a majority of my available disk space Smile

In particular, I want to be able to:
a) Pipe debug logs directly into gzip.
b) Write debug logs to in-memory circular buffers. This will mean that only the most recent few megabytes of data are available, but the simulation should run much faster since it avoids disk IO.

All this is easy to do if I could write my own BDPI functions in C, but bluespec won't let me pass an Fmt type to it... Is there any other way to do this?

Thanks! Smile
Back to top
View user's profile Send private message Visit poster's website

Joined: 18 Jul 2007
Posts: 71

PostPosted: Mon Oct 27, 2008 11:08 am    Post subject: Example of logging strings through BDPI Reply with quote


Here is an example of a technique (not the prettiest, admittedly) for getting formatted strings (not Fmt structures) out through BDPI. It requires that the maximum size of the string be known in advance, and there is some non-obvious fiddling with null-termination and memory layout. But that only has to be done once, and then it can be used without much hassle.

Please let me know if you have any questions or encounter any problems.

Thank you,

Here is the BSV module which imports the C logging function and uses it:

// C function which handles string reversal and logging
import "BDPI" function Action c_log_fn(UInt#(32) len, Bit#(n) x);
// Wrapper to include buffer size argument and null terminator.
// The string is written into the buffer in the opposite order
// to a normal C string (as per the Verilog standard), so we
// preface the string with a 0 byte so that is is null-terminated
// in the C world.  The string is then reversed within the
// c_log_fn function.
function Action log(Bit#(n) value) provisos(Add#(n,1,buf_sz));
      let null_terminated = {8'h00,value};
      c_log_fn(fromInteger(valueOf(buf_sz)), null_terminated);
endfunction: log
(* synthesize *)
module mkExample();
   Reg#(UInt#(32)) x <- mkReg(0);
   Reg#(Bool) b <- mkReg(False);
   // This uses $swriteAV to format data into a Bit#(n) value.
   // Note that n must be large enough to hold all of the
   // required bits for the largest possible value.
   rule log_stuff;
      Bit#(160) ascii <- $swriteAV("x = %0d", x);
      Bit#(32) ascii2 <- $swriteAV("%s", b ? "Yes" : "No");
   rule incr;
      x <= x + 1;
   rule toggle if (x % 7 == 2);
      b <= !b;
   rule done if (x > 100);

Here is the C code for the logging function itself:

#include <stdio.h>
#include <string.h>

void c_log_fn(unsigned int len, unsigned int* buf)
  char str[len];
  const char* src = (const char*) buf;
  unsigned int n = strlen(src);
  unsigned int i;
  for (i = 0; i < n; ++i)
    str[i] = src[n-i-1];
  str[n] = '\0';
  printf("LOG: %s\n", str);
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Forum Index -> Tools: Bluesim All times are GMT - 4 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum topic RSS feed 

Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP