Jump to content
  • 0

Regarding conversion of reg to int


Aditya Gedela

Question

In my code,

always @(posedge clk) begin

//I wrote some algorithm and I am assigning a new value to a register random done with a delay of 5 seconds.

end

integer k;

//and next to convert it into integer i am writing

always @(random done) begin

k=random done;

.if(k==1)

......

led[0]=1;

if (k==2)

........

led[1]=1;

end 

 

so when I  am writing a separate always block for converting into integer all LEDs are glowing at once immediately after programming....

Is there any way I can directly assign to the integer k in the first always block itself?....

Can you please help me out...

 

Thanks in Advance

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

@Aditya Gedela

At this point, k is just a copy of your random_done. always@(random_done) triggers an event whenever random_done changes, so k will be copied and then each led will be updated. The bigger problem is that you do not appear to have a way to set your leds to 0 coded into your always block. How you fix this depends on how many leds you have access to, and how many bits are in the random_done register. To provide a better suggestion, we would need to see more code, but as it stands, if you only have 16 leds (declared as "output reg [15:0] led") and a 32 bit random done register, you could do something like this:

always@(random_done)
	led[15:0] = random_done[31:16];

This should fix the issue of the leds being always on, something a little closer to the style you are already writing in would be this:

always@(random_done) begin
	if (random_done == 0)
    	led = 16'b0;
	else if (random_done == 1)
    	led = 16'b1;
    else if (random_done == 2)
    	led = 16'b10;
    else if (random_done == 3)
    	led = 16'b100;
    ...
end

This code will turn on only the led with bit position equal to the value of random_done. Key again is that you need to set the led bits that you don't want on to be 0s.

Hope this helps,

Arthur

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...