alias argument in hierarchy in system verilog - multidimensional-array

Is there way to make this alias statement work , as iob is bidirectional we cannot use assign statement. out of all the available iob pins only specific pins are connected to ddr memory dq bus. the other way we are doing is include iob.svh during instantiation of child module below is the snippets of required and implemented
module top(
input [63:0] ddr_dq
);
localparam NUM_HB = 24;
logic [NUM_HB-1:0][4-1:0][8-1:0] iob;
alias iob[0][0][0] = ddr_dq[0];
phy u_phy (
. iob (iob)
);
endmodule
module phy (
inout [NUM_HB-1:0][4-1:0][8-1:0] iob
);
endmodule
this is the work around implemented, but to get a the location of pin at single glance above alias statement will be useful
module top(
input [63:0] ddr_dq
);
localparam NUM_HB = 24;
logic [NUM_HB-1:0][4-1:0][8-1:0] iob;
//alias iob[0][0][0] = ddr_dq[0];
phy u_phy (
`include iob.svh
);
endmodule
Contents of include iob.svh file is captured below
.iob( {
nc_iob_pin[0]
,ddr_dq[0]
,ddr_dq[1]
,ddr_dq[2]
,ddr_dq[3]
,ddr_dq[4]
,ddr_dq[5]
})

I am not sure which problem you saw. Alias statements only work with 'nets', so declaring iob as just logic, meaning var logic prevents it from being compiled. I did modify your code to make it compilable:
parameter NUM_HB = 24;
module top(
input [63:0] ddr_dq
);
wire logic [NUM_HB-1:0][4-1:0][8-1:0] iob; // 'logic' is optional here
alias iob[0][0][0] = ddr_dq[0];
phy u_phy (
. iob (iob)
);
endmodule
module phy (
inout [NUM_HB-1:0][4-1:0][8-1:0] iob
);
endmodule

Related

How to make OnCalculate() function in Custom Indicator use GPU in MQL5 / OpenCL?

I have created an Indicator using MQL5.
After Profiling, the program I read that 99% of my CPU is used by my OnCalculate().
Here is my function:
int OnCalculate( const int rates_total,
const int prev_calculated,
const int begin,
const double &price[]
)
{
//--- check for bars count
float tempprice[];
ArrayResize( tempprice, ArraySize( price ) );
if ( rates_total < InpMAPeriod - 1 + begin ) return( 0 ); // not enough bars for calculation
//--- first calculation or number of bars was changed
if ( prev_calculated == 0 ) ArrayInitialize( ExtLineBuffer, 0 );
ArrayCopy( tempprice, price );
//--- sets first bar from what index will be draw
PlotIndexSetInteger( 0, PLOT_DRAW_BEGIN, InpMAPeriod - 1 + begin );
switch( InpMAMethod )
{
case MODE_EMA: Execute_Me( price,
"CalculateEMA",
rates_total,
prev_calculated,
begin
);
break;
case MODE_LWMA: Execute_Me( price,
"CalculateLWMA",
rates_total,
prev_calculated,
begin
);
break;
case MODE_SMMA: Execute_Me( price,
"CalculateSmoothedMA",
rates_total,
prev_calculated,
begin
);
break;
case MODE_SMA: Execute_Me( price,
"CalculateSimpleMA",
rates_total,
prev_calculated,
begin
);
break;
}
return( rates_total );
}
Here is the result of profiling:
Kindly, let me know, how I can make the OnCalculate() work on GPU instead of CPU.
Which compute device, i.e. whether or not your program uses the CPU can be controlled via the CLContextCreate call:
int CLContextCreate(int device)
Where device could be e.g. CL_USE_GPU_ONLY, or a specific device number.
How to find out the number is described here.
However: What I can see from the profile is, that most of the time is spent Creating and Freeing OpenCL contexts. If the profile represents some kind of call stack, I'd assume that an OpenCL context is created and freed for every computation instead of doing it once during program initialisation and deinitialisation.
This seems to cost you 85 % of your runtime. So make sure, that you create OpenCL Context, Program, Kernel and Buffer objects during initialisation. For repeating computations, you only need do set kernel arguments, read/write buffer objects, and enqueue the kernel for execution
Hope that helps.

build a 8bit ALU using verilog

I'm trying to build an 8bit datapath in an ALU that can add, sub, OR, AND two operands.
I want to use a case statement for each of the operations in the code but I keep getting error messages.
This is what it looks like so far:
module alu (
input [7:0] xa,xb,
input [7:0] op_sel,
input wire ctrl,
output reg 0Zero, 0Carry,
//0Zero infers latch: can only be assigned to 1/ always reg
output reg [7:0] result_out,
);
always #(*)
8'hE0 :
//4 bit for now
begin
out = 8'b0;
0Carry = 1'b0;
//calculate value
case (1) //alu controlled by ctrl signal
8'hA0: out = xa&xb;
//
8'hB0: (0Carry ,out) = xa+xb;
//
8'hC0: (0Zero , 0Carry, out) = xa-xb;
//
8'hD0: out = ~(xa|xb);
//
endcase
end
Your case expression is 1, you should change that into some variable. Here is an example case statement:
reg [1:0] address;
case (address)
2'b00 : statement1;
2'b01, 2'b10 : statement2;
default : statement3;
endcase
If the address value is 2'b00 then statement1 will be executed. Statement2 is executed when address value equals 2'b01 or 2'b10. Otherwise statement3 is executed.

Trying to perform addition of two fractional fixed_point operands in VERILOG, I'm stuck in this error

I'm trying to write a Verilog synthesizable program for adding two fractional fixed_point numbers. This is the test bench:
module sum_test;
// Inputs
reg [12:-20] oper1;
reg [12:-20] oper2;
reg cin;
// Outputs
wire [12:-20] sum_result;
// Instantiate the Unit Under Test (UUT)
sum_fix uut (
.oper1(oper1),
.oper2(oper2),
.cin(cin),
.sum_result(sum_result)
);
initial begin
// Initialize Inputs
oper1 = 0;
oper2 = 0;
cin = 0;
// Wait 100 ns for global reset to finish
#100;
// Add stimulus here
oper1 = 12.5;
oper2 = 5.4;
end
initial begin
$monitor("oper1=%d,oper2=%d,sum_result=%d \n",oper1,oper2,sum_result);
end
endmodule
/// This is the addition module
module sum_fix(
input [12:-20] oper1,
input [12:-20] oper2,
input cin,
output [12:-20] sum_result
);
assign sum_result = oper1 + oper2 + cin;
endmodule
Simulator print this,
oper1= 0,oper2= 0,sum_result= 0
oper1= 13,oper2= 5,sum_result= 18
It seems I'm not introducing the numbers correctly in the testbench or something, it might be that simulator can't work with this notation? BTW I made this module inspired in the book "Digital Design (Verilog)" of Ashenden. He speaks about this fixed-point fractional notation and even make an addition like me with the operator "+", so I don't know what's wrong, he didn't make test bench for this example, though. Thank you everybody, this forum rocks.
Try using :
reg [32:0] oper1;
reg [32:0] oper2;
instead of :
reg [12:-20] oper1;
reg [12:-20] oper2;
And stimulus is :
oper1 = 12.5 * 2**20; //shift 20 binary places
oper2 = 5.4 * 2**20;

Can R extension safely allocate memory when it comes to exceptional conditions?

I am about to write an extension package for R in C++ and wonder how dynamic memory management is intended to be used without risk of memory leaks. I have read
http://cran.r-project.org/doc/manuals/R-exts.html#Memory-allocation
http://cran.r-project.org/doc/manuals/R-exts.html#Garbage-Collection
and immediately get to three questions:
Does R gracefully unwind the C++ stack frame in case of R-exceptions, e.g. when R_alloc runs out of memory or Rf_error is called due to some other condition? – Otherwise, how am I supposed to clean up already R_alloc'ed and PROTECTed or simply Calloc'ed memory? For example, will
#include<R.h>
// […]
void someMethod () {
char *buffer1 = NULL;
char *buffer2 = NULL;
try {
ClassA a;
buffer1 = R_Calloc( 10000, char );
buffer2 = R_Calloc( 10000, char );
// […]
} finally {
try {
if ( NULL != buffer1 ) {
R_Free( buffer1 );
}
} finally {
if ( NULL != buffer2 ) {
R_Free( buffer2 );
}
}
}
}
guarantee to call the destructor ~ClassA for a and R_Free for buffer1 and buffer2? And if not, what would be the R textbook way to guarantee that?
Could standard C++ (nowadays deprecated) std::auto_ptr or modern std::unique_ptr be employed to simplify the memory allocation idiom?
Is there a proven C++ idiom/best practice to use R's memory allocation in the C++ standard template library, e.g. some suitable allocator template, so that STL classes allocate their memory from the R heap?
Since Rf_error will indeed skip the C++ stack frame and thus bypass destructor calls, I found it necessary to undertake more documentation research. In particular a look into the RODBC package and experimentation monitoring memory use to confirm the findings, made me arrive at:
1: Immediately store pointer in an R external pointer and register a finaliser for that.
The idiom is illustrated in the following somewhat simplistic example:
#define STRICT_R_HEADERS true
#include <string>
#include <R.h>
#include <Rinternals.h> // defines SEXP
using namespace std;
class A {
string name;
public:
A ( const char * const name ) : name( name ) { Rprintf( "Construct %s\n", name ); }
~A () { Rprintf( "Destruct %s\n", name.c_str() ); }
const char* whoami () const { return name.c_str(); }
};
extern "C" {
void finaliseAhandle ( SEXP handle ) {
A* pointer = static_cast<A*>( R_ExternalPtrAddr( handle ) );
if ( NULL != pointer ) {
pointer->~A();
R_Free( pointer );
R_ClearExternalPtr( handle );
}
}
SEXP createAhandle ( const SEXP name ) {
A* pointer = R_Calloc( 1, A );
SEXP handle = PROTECT( R_MakeExternalPtr(
pointer,
R_NilValue, // for this simple example no use of tag and prot
R_NilValue
) );
try {
new(pointer) A( CHAR( STRING_ELT( name, 0 ) ) );
R_RegisterCFinalizerEx( handle, finaliseAhandle, TRUE );
} catch (...) {
R_Free( pointer );
R_ClearExternalPtr( handle );
Rf_error( "construction of A(\"%s\") failed", CHAR( STRING_ELT( name, 0 ) ) );
}
// … more code may follow here, including calls to Rf_error.
UNPROTECT(1);
return handle;
}
SEXP nameAhandle ( const SEXP handle ) {
A* pointer = static_cast<A*>( R_ExternalPtrAddr( handle ) );
if( NULL != pointer ) {
return mkChar( pointer->whoami() );
}
return R_NilValue;
}
SEXP destroyAhandle ( const SEXP handle ) {
if( NULL != R_ExternalPtrAddr( handle ) ) {
finaliseAhandle( handle );
}
return R_NilValue;
}
}
The assignment of NULL to the pointer in R_ClearExternalPtr( handle ); prevents double calling of R_Free( pointer );`.
Mind that there is still some assumption needed for the suggested idiom to safely work: If the constructor must not fail in the sense of R, i.e. by calling Rf_error. If this cannot be avoided, my advice would be to postpone the constructor invocation to after the finaliser registration so that the finaliser will in any case be able to R_Free the memory. However, logic must be included in order not to call the destructor ~A unless the A object has been validly constructed. In easy cases, e.g. when A comprises only primitive fields, this may not be an issue, but in more complicated cases, I suggest to wrap A into a struct which can then remember whether the A constructor completed successfully, and then allocate memory for that struct. Of course, we must still rely on the A constructor to gracefully fail, freeing all memory it had allocated, regardless of whether this was done by C_alloc or malloc or the like. (Experimentation showed that memory from R_alloc is automatically freed in case of Rf_error.)
2: No.
Neither class has anything to do with registering R external pointer finalisers.
3: Yes.
As far as I have seen, it is considered best practice to cleanly separate the reigns of C++ and R. Rcpp encourages the use of wrappers (https://stat.ethz.ch/pipermail/r-devel/2010-May/057387.html, cxxfunction in http://dirk.eddelbuettel.com/code/rcpp.html) so that C++ exceptions will not hit the R engine.
In my opinion, an allocator could be programmed to use R_Calloc and R_Free. However, to counter the effects of potential Rf_error during such calls, the allocator would require some interface to garbage collection. I imagine locally tying the allocator to a PROTECTed SEXP of type externalptr which has a finaliser registered by R_RegisterCFinalizerEx and points to a local memory manager which can free memory in case of Rf_error.

special pointer value ((void*)1)

I am trying to use a special pointer with a guaranteed invalid address with gcc. Here is what I do:
#define MY_VALUE_OK ((void*)1);
...
int* data;
...
void* d = MY_VALUE_OK;
if( data != ((void*)1) ) // compiles ok
if( data != d ) // compiles ok
if( data != MY_VALUE_OK ) // error!
printf( " %d", *data );
Any ideas?
Ideally I'd like to define this pointer as static const in a class.
BTW, this is my old code that used to compile with Microsoft Visual Studio just fine.
Does your #define include a semi-colon (like your example does)? If so that would allow the assignment to work, but the if statement would error out because after the text substitution, there would be a semi-colon inside the conditional.
You have defined a semicolon with your #define
so...
if(data != MY_VALUE_OK)
Actually turns into:
if(data != ((void*)1);)
There is an obvious error there
Your define should be:
#define MY_VALUE_OK ((void*)1)
That should fix your problem :)

Resources