Parth Posted February 24, 2014 Report Share Posted February 24, 2014 Hi , For below code , for case-1 and case-2 output is different , module my_module; int a=0; initial begin for(int i=0;i<5;i++) begin fork // Check differance here , between 2 statements // ##case 1 (declaration and assignment). automatic int j= i; // and automatic int j; // ##case 2 declare j=i; // then assign begin $display("Chk auto %d",j); end join_none end end endmodule : my_module For case-1 output is 0 1 2 3 4 For case-2 output is 5 5 5 5 5 Can anyone explain me how?? Thanks , Parth Quote Link to comment Share on other sites More sharing options...
bhunter1972 Posted February 24, 2014 Report Share Posted February 24, 2014 Put the assignment of j=i into the begin/end (for that matter, put the declaration there, too, to be safe). In case 2, you have forked off two separate processes: one assigns j to i and the other prints j. All of these take zero time, and the order in which they occur is indeterminate. Parth 1 Quote Link to comment Share on other sites More sharing options...
dave_59 Posted February 25, 2014 Report Share Posted February 25, 2014 bhunter, that is incorrect; only case 1 will work. The initialization of automatic variables happens as the scope they declared in come into existance(activated). Each iteration of the for loop creates a new fork/join_none block with a unique value of i. That unique value of i is used to initialize the local copy of j created for each fork/join_none block. If you put the variable assigment into procedural code of the child threads, the ordering of the execution of those child threads are indetermiate, and at best, deferred until the parent thread blocks or terminates. Parth 1 Quote Link to comment Share on other sites More sharing options...
uwes Posted February 25, 2014 Report Share Posted February 25, 2014 hi, what you essentially wrote in case#2 is module my_module; int a=0; initial begin for(int i=0;i<5;i++) begin fork // Check differance here , between 2 statements // ##case 1 (declaration and assignment). automatic int j= i; // and automatic int j; // ##case 2 declare begin j=i; // then assign end begin $display("Chk auto %d",j); end join_none end end endmodule : my_module note that the $display and the "j=i" are actually parallel threads (a race). if you switch the order of the two blocks your result might be different (if your simulator chooses this) begin $display("Chk auto %d",j); end begin j=i; // then assign end so for what you want to accomplish case#1 is the right path. /uwe Parth 1 Quote Link to comment Share on other sites More sharing options...
Parth Posted February 27, 2014 Author Report Share Posted February 27, 2014 Thanks bhunter , dave and uwe . What I conclude is , automatic int j= i; does not create process in fork_join but always executed before $display , so case-1 works and not case-2. In case-2 also if we remove automatic type then result would be same as case-1. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.