Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

verilog code to automatically activate the outputs of demultiplexer

Status
Not open for further replies.

ecasha

Junior Member level 2
Joined
Feb 28, 2017
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
197
Hey,I have written verilog code for the 1:2 demux,I want to activate the outputs one after the other without giving select inputs manually.When I run it ,its showing the only f2 output.I want to see both outputs i.e first when select =0 and select=1 one after the other.How to rectify it?


Code:
module trrrrr(FIS,f0,f1);
input FIS;
wire FIS;
output f0,f1;
reg f0,f1;
integer slct=0;
 always @(FIS) begin
 for(slct=0;slct <= 1'b1;slct = slct+1)begin
if (slct== 1'b0)begin
	  f0=FIS;
     f1=0; 
		end
   else begin
    f0=0;
    f1=FIS;
    end
end
end
endmodule
Capture.JPG
Above is the output what I am getting.
 

Don't use a for loop, this is not software, it's Verilog which is used to describe hardware, there are no for loops in hardware.

Use a counter or a shift register in your testbench or just assign the stimulus to your inputs with # delays separating the assignments in an initial block. Just don't do this in your demux code.

Problems with your code:
  • You used a for loop to generate slct.
  • The select shouldn't be generated in your demux code in the first place, it is an input to this module.
  • You've defined slct as an integer but are doing a compare to a single bit (perhaps you should learn VHDL it won't allow bad stuff like this).
  • You are missing slct from your sensitivity list of the always, and why aren't you using always @* to avoid this missing stuff in the sensitivity list?
  • Not using the Verilog 2001 module port (C-like) declarations. I haven't seen a simulator/synthesis tool that can't handle that for many years now. i.e. it's supported, there is no need to use the antiquated and repetitive syntax.

I would write the following:

Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module trrrrr (
  input      slct,
  input      fis,
  output reg f0,
  output reg f1
);
 
  // using if
  always @* begin
    if (slct == 1'b0)  {f1, f0} <= {1'b0, fis};
    else               {f1, f0} <= {fis, 1'b0};
  end
 
  // using case
  always @* begin
    case (slct)
      1'b0 : {f1, f0} <= {1'b0, fis};
      1'b1 : {f1, f0} <= {fis, 1'b0};
    endcase
  end
 
endmodule

 
  • Like
Reactions: ecasha

    ecasha

    Points: 2
    Helpful Answer Positive Rating
Hey,I have written verilog code for the 1:2 demux,I want to activate the outputs one after the other without giving select inputs manually.When I run it ,its showing the only f2 output.I want to see both outputs i.e first when select =0 and select=1 one after the other.How to rectify it?


Code:
module trrrrr(FIS,f0,f1);
input FIS;
wire FIS;
output f0,f1;
reg f0,f1;
integer slct=0;
 always @(FIS) begin
 for(slct=0;slct <= 1'b1;slct = slct+1)begin
if (slct== 1'b0)begin
	  f0=FIS;
     f1=0; 
		end
   else begin
    f0=0;
    f1=FIS;
    end
end
end
endmodule
View attachment 137382
Above is the output what I am getting.

you need to read some tutorials or take some classes. this is not how hardware is described. trying to get it right by simulating and finding the right solution by chance is not the way to go.

you can look at these examples: https://www.asic-world.com/examples/verilog/mux.html
they are not perfect, but they are good enough for what you are trying to do
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top