4/27/2007

Some tips for vi

1. handle multiple files:
:bn - jump to next file
:bp - jump to previous file
:bw - close current file
Ctrl ^ - toggle tween current and previous files
:e# - jump to previous file
:b1 - jump to the first file

1. column edit (visual mode):
v - visual mode
Ctrl v - visual-block mode
V - visual-line

Embed Perl into C/C++

1. Compile the interpreter:

gcc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts`

`perl -MExtUtils::Embed -e ccopts -e ldopts` is used to set the options fro gcc

interp.c:
#include /* from the Perl distribution */
#include /* from the Perl distribution */

static PerlInterpreter *my_perl; /*** The Perl interpreter ***/

int main(int argc, char **argv, char **env)
{
 PERL_SYS_INIT3(&argc,&argv,&env);
 my_perl = perl_alloc();
 perl_construct(my_perl);
 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
 perl_parse(my_perl, NULL, argc, argv, (char **)NULL);
 perl_run(my_perl);
 perl_destruct(my_perl);
 perl_free(my_perl);
 PERL_SYS_TERM();
}

2. Run the interpreter:

$interp
print "Pretty Good Perl \n";
print "10890 - 9801 is ", 10890 - 9801;

Pretty Good Perl
10890 - 9801 is 1089
$

4/21/2007

Logic Calucation in Veirlog

~z = x;
~X = X;

0&Z = 0&X = 0;
1&Z = 1&X = X;

Z&1 = Z&Z = Z&X = X;
X&1 = X&Z = X&X = X;

Z|0 = Z|Z = Z|X = X;
X|0 = X|Z = X|X = X;

Z^* = X^* = X;
Z~^* = X~^* = X;

4/09/2007

Ways to assign signals in Verilog

1. Continuous assignment (assign)
 wire a;
 assign a = 1'b1; //behaves like a wire

2. Procedural assignemnt (always)
 a). blocking assignment
  reg a, b;
  a = 1'b1; //will block next assignment only in sequential blocks
  b = 1'b0;
 b). non-blocking assignment
  reg a, b;
  a <= 1'b1; //all non-blocking assignemnts will be executed at    b <= 1'b0; //the END of this update event.
  Because non-blocking assignments are executed at the end of update event, if they are mixed with blocking assigments, non-blocking assignments will be executed after all blocking assignments.
  If multiple non-blocking assignments are used to assign the same variable in a block, the order of assignments will be hornored.
  reg a;
  a <= 1'b1;
  a <= 1'b0; //a is 0 finally   If multiple non-blocking/blocking assignments are used to assign the same variable in different blocks, and the order of their executions can not be decided, then the final value of this variable can not be determined.
  reg a;
  initial a <= 1'b1;
  initial a <= 1'b0; //a is uncertain
  However, if the order of their executions can be determined, the final value of the variable is determined.
  reg a;
  initial #8 a <= #8 1'b1; //@16, a is set to 1, scheduled @8
  initial #10 a <= #6 1'b0; //@16, a is set to 0, scheduled @10  
  a would be 0 finally because the second assignemnt is scheduled later.
3. Procedural continuous assignment (assign/deassign, force/release)
  assign procedural continuous assignment will override all previous procedural assignments to the same variable. deassign will cancel this assignment.
  force/release have the similar function with the exception that they can be used on wires as well.
  module dff (q,d,clear, preset, clock);
  input d, clear, preset, clock;
  output q;
  reg q;

  always @(clear or preset)
    if(!clear) assign q = 0;
    else if (!preset) assign q = 1;
    else deassign q;

  always @(posedge clock)
    q = d;
  endmodule

3/27/2007

Most useful VPI code blocks

1. Register a system call
void hello_register()
{
 s_vpi_systf_data tf_data;

 tf_data.type = vpiSysTask;
 tf_data.tfname = "$hello";
 tf_data.calltf = hello;
 tf_data.compiletf = NULL;
 tf_data.sizetf = NULL;
 vpi_register_systf(&tf_data);
}

2. Obtain system task arguments

 vpiHandle systf_handle, arg_iterator, arg_handle, net_handle;
 s_vpi_value current_value;

 /* obtain a handle to the system task instance */
 systf_handle = vpi_handle(vpiSysTfCall, NULL);

 /* obtain handle to system task argument */
 arg_iterator = vpi_iterate(vpiArgument, systf_handle);
 net_handle = vpi_scan(arg_iterator);
 vpi_free_object(arg_iterator); /* free iterator memory */

3/23/2007

Reset CF GPS receiver

1) Download WinFast Navigator at . Copy and paste this URL onto your browser. ftp://ftp.winfast.com.tw/gps/Tools/WinFast GPS-Install.zip

2) Then, unzip the file but you only need to move/copy "Navigator.ARM.CAB" to your PDA. This file is located in "WinFast Navigator CE" folder.

3) Once you have copied this cab file to your PDA, just use File Explorer to locate it and tap once on it to launch the installtion program.

4) To launch the program, click on Start | Navigator.

5) Insert your CF Card to the CF slot now and observe the Device plug-in! pop up windows and note down the COM number. Click OK to close out of the pop up window.

6) Click on Tools | Port Setting… | tap on OK 3 times to get safely get rid of the error message.
a. GPS Portocol=NMEA,
b. Port=Modem COM X , X is the number you observed on step 5
c. Baud Rate=4800.

7) Click OK

8) Click on Tools | Make sure Factory radio button is selected under Reset Mode, leave everything else as default and click OK.

9) Reset to default completes. Click on View | Signal Level to observe the GPS lock status.

3/17/2007

FSM Coding Convention

Two-always blocks with continuous assignment outputs:

module fsm1a (ds, rd, go, ws, clk, rst_n);
 output ds, rd;
 input go, ws;
 input clk, rst_n;
 parameter [1:0] IDLE = 2'b00,
          READ = 2'b01,
          DLY = 2'b10,
          DONE = 2'b11;
 reg [1:0] state, next;

 always @(posedge clk or negedge rst_n)
  if (!rst_n) state <= IDLE;
  else state <= next;

 always @(state or go or ws) begin
  next = 2'bx;
  case (state)
  IDLE: if (go) next = READ;
     else next = IDLE;
  READ: next = DLY;
  DLY: if (ws) next = READ;
     else next = DONE;
  DONE: next = IDLE;
  endcase
 end

 assign rd = (state==READ state==DLY);
 assign ds = (state==DONE);
endmodule

Two-always blocks with combined output assignments:

module fsm1 (ds, rd, go, ws, clk, rst_n);
 output ds, rd;
 input go, ws;
 input clk, rst_n;
 reg ds, rd;
 parameter [1:0] IDLE = 2'b00,
          READ = 2'b01,
          DLY = 2'b10,
          DONE = 2'b11;
 reg [1:0] state, next;

 always @(posedge clk or negedge rst_n)
  if (!rst_n) state <= IDLE;
  else state <= next;

 always @(state or go or ws) begin
  next = 2'bx;
  ds = 1'b0;
  rd = 1'b0;
  case (state)
   IDLE: if (go) next = READ;
      else next = IDLE;
   READ: begin rd = 1'b1;
          next = DLY;
      end
   DLY: begin rd = 1'b1;
         if (ws) next = READ;
         else next = DONE;
      end
   DONE: begin ds = 1'b1;
       next = IDLE;
      end
  endcase
 end
endmodule

Three-always block coding style

module fsm1b (ds, rd, go, ws, clk, rst_n);
 output ds, rd;
 input go, ws;
 input clk, rst_n;
 reg ds, rd;
 parameter [1:0] IDLE = 2'b00,
          READ = 2'b01,
          DLY = 2'b10,
          DONE = 2'b11;
 reg [1:0] state, next;

 always @(posedge clk or negedge rst_n)
  if (!rst_n) state <= IDLE;
  else state <= next;

 always @(state or go or ws) begin
  next = 2'bx;
  case (state)
  IDLE: if (go) next = READ;
     else next = IDLE;
  READ: next = DLY;
  DLY: if (ws) next = READ;
     else next = DONE;
  DONE: next = IDLE;
  endcase
 end

 always @(posedge clk or negedge rst_n)
  if (!rst_n) begin
   ds <= 1'b0;
   rd <= 1'b0;
  end
  else begin
   ds <= 1'b0;
   rd <= 1'b0;
   case (state)
   IDLE: if (go) rd <= 1'b1;
   READ: rd <= 1'b1;
   DLY: if (ws) rd <= 1'b1;
      else ds <= 1'b1;
   endcase
  end
 endmodule

Encoding Styles for FSM

  • Binary encoding is good for creating small (less than 16 states) FSMs in FPGAs. Larger FSMs require too much Next state logic and perform slower when binary encoded.
  • One-Hot encoding is good for building larger FSMs since it requires very little Next state logic. OHE is more reliable than Binary encoding, because fewer bits change at once making the circuit less likely to glitch. OHE leaves a lot of unused states in the FSM and also uses a lot of registers (but there are a lot of registers in FPGAs).
  • Gray encoding of your state machine uses less resources than OHE and also gets good speed. Gray encoding is also the most reliable encoding technique, because only one bit changes per transition. However, Gray encoding requires the designer to be very aware of the coding technique chosen when simulating the FSM, and this is often a pain.

2/20/2007

Function pointer and function returning a pointer

C/C++ Function Pointers
1. Define:
int (*pFunction)(int, float, char) = NULL;
2. Assign:
int func1(int a, float b, char c) {
...
}
pFunction = &func1;
3. Use:
(*pFunction)(1, 2.5, 'c');

Function that returns a pointer
1. Define:
int* func1(int a, float b, char c) {
return &a;
}
2. Use:
int *pA;
pA = func1(1, 2.5, 'c');

加sdf的postsimulation

Add the following code in the testbench of your design:

initial
begin
$sdf_annotate("sdf_file.sdf", uut);
end