Given the following module declaration:
module ( myinterface.mymodport mybus, ... );
And assuming that myinterface has parameters, how do I specify them?
The interface instantiation happens only in the testbench, but now I want to synthesize the DUT, so the TB disappears.
This is an oversight in the SystemVerilog LRM. There's no syntax to specify a required set of parameters for an interface in a module header.
You might check your synthesis tool to see if they provide any way of specifying parameter overrides for the top-level synthesis instance.
You specify the parameter when you instantiate the interface; you do not specify it in the port list of the module. Given
interface myinterface #(parameter DATA_SIZE = 0);
...
All you need is
module mymodule (myinterface.mymodport mybus);
...
because somewhere else you have
myinterface #(.DATA_SIZE(64)) i();
interface myinterface #(parameter DATA_SIZE = 0);
logic [DATA_SIZE-1:0] AWID;
logic [31:0] AWADDR;
modport mymodport (input AWID, AWADDR);
endinterface
module mymodule (myinterface.mymodport mybus);
initial
$display("mymodule");
endmodule
module top;
myinterface #(.DATA_SIZE(64)) i();
mymodule m (.mybus(i));
endmodule
https://www.edaplayground.com/x/528x
Related
I want to declare a function with a generic type which has to inherit from another generic type that should be an interface, and some other class.
But Kotlin complains and doesn't let me do this.
class SomeClass<T: SomeInterface> {
fun <R>someFunktion(task: R) where R : SomeOtherClass, R : T {}
}
If I just replace the generic type T in the function declaration with SomeInterface, it works.
class SomeClass<T: SomeInterface> {
fun <R>someFunktion(task: R) where R : SomeOtherClass, R : SomeInterface {}
}
So it seems the problem is that Kotlin does not know whether T is an interface or a class. If it would be a class it could not work because Kotlin does not support multi inheritance for classes.
So does someone know a solution? Thank you already in advance for every effort.
Writing your above code example results in the Kotlin compiler to complain:
Type parameter cannot have any other bounds if it's bounded by another type parameter
It says that you cannot have your variable R be bound by a second type parameter T. However, for whatever reason, the problem can just be suppressed: If you add the annotation
#Suppress("BOUNDS_NOT_ALLOWED_IF_BOUNDED_BY_TYPE_PARAMETER")
fun <R>someFunktion(task: R) where R : SomeOtherClass, R : T {}
then Kotlin will stop complaining and everything works just fine.
I am attempting to reorganize some code such that it is self-documenting. If I have a class that I don't intend to instantiate, but rather want to call to perform various tasks all related to the same idea, can I create simple static member functions that are then called by later static member functions (Or tasks in my case)?
class foo;
static protected bit [7:0] Data_A;
static protected bit [7:0] Data_B;
static task bar();
/* Use to print out info. in Data_A and Data_B. */
endtask : bar
static task FooBar();
/* Perform some manipulations on Data_A and Data_B. */
bar();
endtask : FooBar
endclass : foo
Then in my program I would do something like:
program automatic main();
initial
begin
foo::FooBar();
$finish;
end
endprogram
Does this seem sound? Thank you for your time. I have been reading the IEEE System-Verilog standard, but this situation does not seem to come up.
Edit: Do I need to use the "this" keyword, or is that reserved for instantiated classes to refer to the self?
It is perfectly appropriate to be able to call one static method of a class from another. You do not use the this prefix to call a static method. You could prefix your call with foo::bar() just to make it clearer to the reader that you are calling a static method, but the language does not need it.
Any class you do not want constructed should be declared as
virtual class foo;
You can even go further by declaring the constructor as
local function new; endfunction
That keeps people from extended it and constructing, but that might be an overkill.
And finally a suggestion: what you are doing might be better suited for a package
package automatic foo;
bit [7:0] Data_A;
bit [7:0] Data_B;
task bar();
/* Use to print out info. in Data_A and Data_B. */
endtask : bar
task FooBar();
/* Perform some manipulations on Data_A and Data_B. */
bar();
endtask : FooBar
endpackage : foo
module automatic main();
initial
begin
foo::FooBar();
$finish;
end
endmodule
I am programming in Fortran and I need to create an object class that has a dynamic vector as an attribute. Here is a minimal example of what I am trying to achieve.
MODULE my_class
PRIVATE
PUBLIC my_object
! define my object with a dynamic attribute
TYPE my_object
private
integer:: size;
real, allocatable:: tab(:); ! here the dynamic vector
CONTAINS
private
final:: destructor;
END TYPE my_object
! define an interface for object constructor
INTERFACE my_object
module procedure:: constructor;
END INTERFACE my_object
CONTAINS
! define constructor
FUNCTION constructor(size) RESULT(obj)
integer, intent(in):: size;
type(my_object):: obj;
obj%size= size;
allocate(obj%tab(size))
END FUNCTION constructor
! define destructor
SUBROUTINE destructor(this)
class(my_object):: this;
if (allocated(this%tab)) deallocate(this%tab)
END SUBROUTINE
END MODULE my_class
But I am not sure that is the proper way to do it. Is there not a risk of memory leaking with this implementation in case of multiple initializations? With this code for example :
PROGRAM test
USE my_class
IMPLICIT NONE
type(my_object):: obj;
obj= my_object(1000);
obj= my_object(3000);
END PROGRAM test
Has the obj%tab of the first instance been properly deallocated?
Or should I reimplement the equal operator?
Thanks for helping me to find out how Fortran deals with this.
If I create a Kotlin file MyTest.kt
package my.test
fun sayHello(): String = "Hello"
A class MyTestKt will be generated and it can be accessed from java like this:
MyTestKt.sayHello() // Returns "Hello"
MyTestKt myTestKt = new MyTestKt() // Instantiate
I would like to make that constructor private. Is that possible? If so, how?
I know I can use an object to create a singleton, that is not my question. I know I can create a class with a companion object inside, that is also not my question.
You can do something like this
#file:JvmName("Utils")
package demo
fun foo() {}
class Utils private constructor()
When you try to call Utils constructor from Java you get a "Utils has private access"
UPDATE
When using a private constructor() you are not able to access foo function.
I think this could be a design flaw, having a function not associated to any class-object. I have look into several kotlin standard library and I only found extension-functions as you want. In that case, for example, CollectionKt, it cannot be instantiated.
If I try this:
sealed class Attributes
data class Attributes1(
val prop1: String
) : Attributes()
private data class IMyType<A>(
val attributes: A
) where A: Attributes
typealias MyType1 = IMyType<Attributes1>
...I get the error: 'public' typealias exposes 'private' in expanded type IMyType.
What is/are the reason(s) for preventing this?
Note: Using Kotlin 1.1.4
Edit 1
I understand what typealiases are and I understand the implications of the restrictions in place.
What I'm asking is why those restrictions need to be there.
If you consider my example code... I want MyType1 (and maybe others) to be accessible outside of this file, but I don't want the raw/generic IMyType to be used outside of this file.
Is that not a legitimate use case?
By definition typealias is to provide an alternative name for the existing type. So you can't promoting the visibility for the existing type by typealias.
From the grammar of the typealias, if you want to use typealias in your code, you must make sure the visibility of the typealias is lower or equals than the visibility of the existing type. for example:
internal class Bar
private typealias PrivateFoo = Bar
internal typealias InternalFoo = Bar
//v--- error
public typealias PublicFoo = Bar
The visibility of the type alias should be the same or more restricted as the corresponding type.
When you declare IMyType as private in your file, IMyType is a file-private class which can be accessed only within that kotlin file. If you declare the public type alias MyType1 for IMyType, then it is meaningless for IMyType to be private since MyType1 can be access everywhere. So, it is forbidden.
From Kotlin's doc:
Type aliases do not introduce new types. They are equivalent to the corresponding underlying types. When you add typealias Predicate<T> and use Predicate<Int> in your code, the Kotlin compiler always expand it to (Int) -> Boolean.
The main purpose of having typealias is to provide alternative name for existing types. In your case, both IMyType and MyType1 refer to the same class. You cannot access IMyType through MyType1 outside the file since IMyType intends to be private.
Example to show new type is not introduced:
//Kotlin code
fun acceptMyType1(type: MyType1) {}
//Decompiled code, MyType1 is not used
public static final void acceptMyType1(#NotNull IMyType type) {
Intrinsics.checkParameterIsNotNull(type, "type");
}
Sidetracked problem: You cannot expose a private super class through a public sub class also.